1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 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, 59 Temple Place - Suite 330, 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 #define CALLED_AS_BUILT_IN(NODE) \
52 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
54 #ifndef PAD_VARARGS_DOWN
55 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
58 /* Define the names of the builtin function types and codes. */
59 const char *const built_in_class_names
[4]
60 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
62 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM) #X,
63 const char *const built_in_names
[(int) END_BUILTINS
] =
65 #include "builtins.def"
69 /* Setup an array of _DECL trees, make sure each element is
70 initialized to NULL_TREE. */
71 tree built_in_decls
[(int) END_BUILTINS
];
72 /* Declarations used when constructing the builtin implicitly in the compiler.
73 It may be NULL_TREE when this is invalid (for instance runtime is not
74 required to implement the function call in all cases). */
75 tree implicit_built_in_decls
[(int) END_BUILTINS
];
77 static int get_pointer_alignment (tree
, unsigned int);
78 static const char *c_getstr (tree
);
79 static rtx
c_readstr (const char *, enum machine_mode
);
80 static int target_char_cast (tree
, char *);
81 static rtx
get_memory_rtx (tree
);
82 static tree
build_string_literal (int, const char *);
83 static int apply_args_size (void);
84 static int apply_result_size (void);
85 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
86 static rtx
result_vector (int, rtx
);
88 static rtx
expand_builtin_setjmp (tree
, rtx
);
89 static void expand_builtin_update_setjmp_buf (rtx
);
90 static void expand_builtin_prefetch (tree
);
91 static rtx
expand_builtin_apply_args (void);
92 static rtx
expand_builtin_apply_args_1 (void);
93 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
94 static void expand_builtin_return (rtx
);
95 static enum type_class
type_to_class (tree
);
96 static rtx
expand_builtin_classify_type (tree
);
97 static void expand_errno_check (tree
, rtx
);
98 static rtx
expand_builtin_mathfn (tree
, rtx
, rtx
);
99 static rtx
expand_builtin_mathfn_2 (tree
, rtx
, rtx
);
100 static rtx
expand_builtin_mathfn_3 (tree
, rtx
, rtx
);
101 static rtx
expand_builtin_args_info (tree
);
102 static rtx
expand_builtin_next_arg (void);
103 static rtx
expand_builtin_va_start (tree
);
104 static rtx
expand_builtin_va_end (tree
);
105 static rtx
expand_builtin_va_copy (tree
);
106 static rtx
expand_builtin_memcmp (tree
, tree
, rtx
, enum machine_mode
);
107 static rtx
expand_builtin_strcmp (tree
, rtx
, enum machine_mode
);
108 static rtx
expand_builtin_strncmp (tree
, rtx
, enum machine_mode
);
109 static rtx
builtin_memcpy_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
110 static rtx
expand_builtin_strcat (tree
, tree
, rtx
, enum machine_mode
);
111 static rtx
expand_builtin_strncat (tree
, rtx
, enum machine_mode
);
112 static rtx
expand_builtin_strspn (tree
, rtx
, enum machine_mode
);
113 static rtx
expand_builtin_strcspn (tree
, rtx
, enum machine_mode
);
114 static rtx
expand_builtin_memcpy (tree
, rtx
, enum machine_mode
);
115 static rtx
expand_builtin_mempcpy (tree
, tree
, rtx
, enum machine_mode
, int);
116 static rtx
expand_builtin_memmove (tree
, tree
, rtx
, enum machine_mode
);
117 static rtx
expand_builtin_bcopy (tree
, tree
);
118 static rtx
expand_builtin_strcpy (tree
, rtx
, enum machine_mode
);
119 static rtx
expand_builtin_stpcpy (tree
, rtx
, enum machine_mode
);
120 static rtx
builtin_strncpy_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
121 static rtx
expand_builtin_strncpy (tree
, rtx
, enum machine_mode
);
122 static rtx
builtin_memset_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
123 static rtx
builtin_memset_gen_str (void *, HOST_WIDE_INT
, enum machine_mode
);
124 static rtx
expand_builtin_memset (tree
, rtx
, enum machine_mode
);
125 static rtx
expand_builtin_bzero (tree
);
126 static rtx
expand_builtin_strlen (tree
, rtx
, enum machine_mode
);
127 static rtx
expand_builtin_strstr (tree
, rtx
, enum machine_mode
);
128 static rtx
expand_builtin_strpbrk (tree
, rtx
, enum machine_mode
);
129 static rtx
expand_builtin_strchr (tree
, rtx
, enum machine_mode
);
130 static rtx
expand_builtin_strrchr (tree
, rtx
, enum machine_mode
);
131 static rtx
expand_builtin_alloca (tree
, rtx
);
132 static rtx
expand_builtin_unop (enum machine_mode
, tree
, rtx
, rtx
, optab
);
133 static rtx
expand_builtin_frame_address (tree
, tree
);
134 static rtx
expand_builtin_fputs (tree
, rtx
, bool);
135 static rtx
expand_builtin_printf (tree
, rtx
, enum machine_mode
, bool);
136 static rtx
expand_builtin_fprintf (tree
, rtx
, enum machine_mode
, bool);
137 static rtx
expand_builtin_sprintf (tree
, rtx
, enum machine_mode
);
138 static tree
stabilize_va_list (tree
, int);
139 static rtx
expand_builtin_expect (tree
, rtx
);
140 static tree
fold_builtin_constant_p (tree
);
141 static tree
fold_builtin_classify_type (tree
);
142 static tree
fold_builtin_strlen (tree
);
143 static tree
fold_builtin_inf (tree
, int);
144 static tree
fold_builtin_nan (tree
, tree
, int);
145 static int validate_arglist (tree
, ...);
146 static bool integer_valued_real_p (tree
);
147 static tree
fold_trunc_transparent_mathfn (tree
);
148 static bool readonly_data_expr (tree
);
149 static rtx
expand_builtin_fabs (tree
, rtx
, rtx
);
150 static rtx
expand_builtin_signbit (tree
, rtx
);
151 static tree
fold_builtin_cabs (tree
, tree
);
152 static tree
fold_builtin_sqrt (tree
, tree
);
153 static tree
fold_builtin_cbrt (tree
, tree
);
154 static tree
fold_builtin_pow (tree
, tree
, tree
);
155 static tree
fold_builtin_sin (tree
);
156 static tree
fold_builtin_cos (tree
, tree
, tree
);
157 static tree
fold_builtin_tan (tree
);
158 static tree
fold_builtin_atan (tree
, tree
);
159 static tree
fold_builtin_trunc (tree
);
160 static tree
fold_builtin_floor (tree
);
161 static tree
fold_builtin_ceil (tree
);
162 static tree
fold_builtin_round (tree
);
163 static tree
fold_builtin_bitop (tree
);
164 static tree
fold_builtin_memcpy (tree
);
165 static tree
fold_builtin_mempcpy (tree
, tree
, int);
166 static tree
fold_builtin_memmove (tree
, tree
);
167 static tree
fold_builtin_strchr (tree
);
168 static tree
fold_builtin_memcmp (tree
);
169 static tree
fold_builtin_strcmp (tree
);
170 static tree
fold_builtin_strncmp (tree
);
171 static tree
fold_builtin_signbit (tree
);
172 static tree
fold_builtin_copysign (tree
, tree
);
173 static tree
fold_builtin_isascii (tree
);
174 static tree
fold_builtin_toascii (tree
);
175 static tree
fold_builtin_isdigit (tree
);
176 static tree
fold_builtin_fabs (tree
, tree
);
177 static tree
fold_builtin_abs (tree
, tree
);
178 static tree
fold_builtin_unordered_cmp (tree
, enum tree_code
, enum tree_code
);
179 static tree
fold_builtin_1 (tree
, bool);
181 static tree
fold_builtin_strpbrk (tree
);
182 static tree
fold_builtin_strstr (tree
);
183 static tree
fold_builtin_strrchr (tree
);
184 static tree
fold_builtin_strcat (tree
);
185 static tree
fold_builtin_strncat (tree
);
186 static tree
fold_builtin_strspn (tree
);
187 static tree
fold_builtin_strcspn (tree
);
188 static tree
fold_builtin_sprintf (tree
, int);
191 /* Return the alignment in bits of EXP, a pointer valued expression.
192 But don't return more than MAX_ALIGN no matter what.
193 The alignment returned is, by default, the alignment of the thing that
194 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
196 Otherwise, look at the expression to see if we can do better, i.e., if the
197 expression is actually pointing at an object whose alignment is tighter. */
200 get_pointer_alignment (tree exp
, unsigned int max_align
)
202 unsigned int align
, inner
;
204 if (! POINTER_TYPE_P (TREE_TYPE (exp
)))
207 align
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp
)));
208 align
= MIN (align
, max_align
);
212 switch (TREE_CODE (exp
))
216 case NON_LVALUE_EXPR
:
217 exp
= TREE_OPERAND (exp
, 0);
218 if (! POINTER_TYPE_P (TREE_TYPE (exp
)))
221 inner
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp
)));
222 align
= MIN (inner
, max_align
);
226 /* If sum of pointer + int, restrict our maximum alignment to that
227 imposed by the integer. If not, we can't do any better than
229 if (! host_integerp (TREE_OPERAND (exp
, 1), 1))
232 while (((tree_low_cst (TREE_OPERAND (exp
, 1), 1))
233 & (max_align
/ BITS_PER_UNIT
- 1))
237 exp
= TREE_OPERAND (exp
, 0);
241 /* See what we are pointing at and look at its alignment. */
242 exp
= TREE_OPERAND (exp
, 0);
243 if (TREE_CODE (exp
) == FUNCTION_DECL
)
244 align
= FUNCTION_BOUNDARY
;
245 else if (DECL_P (exp
))
246 align
= DECL_ALIGN (exp
);
247 #ifdef CONSTANT_ALIGNMENT
248 else if (CONSTANT_CLASS_P (exp
))
249 align
= CONSTANT_ALIGNMENT (exp
, align
);
251 return MIN (align
, max_align
);
259 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
260 way, because it could contain a zero byte in the middle.
261 TREE_STRING_LENGTH is the size of the character array, not the string.
263 ONLY_VALUE should be nonzero if the result is not going to be emitted
264 into the instruction stream and zero if it is going to be expanded.
265 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
266 is returned, otherwise NULL, since
267 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
268 evaluate the side-effects.
270 The value returned is of type `ssizetype'.
272 Unfortunately, string_constant can't access the values of const char
273 arrays with initializers, so neither can we do so here. */
276 c_strlen (tree src
, int only_value
)
279 HOST_WIDE_INT offset
;
284 if (TREE_CODE (src
) == COND_EXPR
285 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
289 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
);
290 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
);
291 if (tree_int_cst_equal (len1
, len2
))
295 if (TREE_CODE (src
) == COMPOUND_EXPR
296 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
297 return c_strlen (TREE_OPERAND (src
, 1), only_value
);
299 src
= string_constant (src
, &offset_node
);
303 max
= TREE_STRING_LENGTH (src
) - 1;
304 ptr
= TREE_STRING_POINTER (src
);
306 if (offset_node
&& TREE_CODE (offset_node
) != INTEGER_CST
)
308 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
309 compute the offset to the following null if we don't know where to
310 start searching for it. */
313 for (i
= 0; i
< max
; i
++)
317 /* We don't know the starting offset, but we do know that the string
318 has no internal zero bytes. We can assume that the offset falls
319 within the bounds of the string; otherwise, the programmer deserves
320 what he gets. Subtract the offset from the length of the string,
321 and return that. This would perhaps not be valid if we were dealing
322 with named arrays in addition to literal string constants. */
324 return size_diffop (size_int (max
), offset_node
);
327 /* We have a known offset into the string. Start searching there for
328 a null character if we can represent it as a single HOST_WIDE_INT. */
329 if (offset_node
== 0)
331 else if (! host_integerp (offset_node
, 0))
334 offset
= tree_low_cst (offset_node
, 0);
336 /* If the offset is known to be out of bounds, warn, and call strlen at
338 if (offset
< 0 || offset
> max
)
340 warning ("offset outside bounds of constant string");
344 /* Use strlen to search for the first zero byte. Since any strings
345 constructed with build_string will have nulls appended, we win even
346 if we get handed something like (char[4])"abcd".
348 Since OFFSET is our starting index into the string, no further
349 calculation is needed. */
350 return ssize_int (strlen (ptr
+ offset
));
353 /* Return a char pointer for a C string if it is a string constant
354 or sum of string constant and integer constant. */
361 src
= string_constant (src
, &offset_node
);
365 if (offset_node
== 0)
366 return TREE_STRING_POINTER (src
);
367 else if (!host_integerp (offset_node
, 1)
368 || compare_tree_int (offset_node
, TREE_STRING_LENGTH (src
) - 1) > 0)
371 return TREE_STRING_POINTER (src
) + tree_low_cst (offset_node
, 1);
374 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
375 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
378 c_readstr (const char *str
, enum machine_mode mode
)
384 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
389 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
392 if (WORDS_BIG_ENDIAN
)
393 j
= GET_MODE_SIZE (mode
) - i
- 1;
394 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
395 && GET_MODE_SIZE (mode
) > UNITS_PER_WORD
)
396 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
398 gcc_assert (j
<= 2 * HOST_BITS_PER_WIDE_INT
);
401 ch
= (unsigned char) str
[i
];
402 c
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
404 return immed_double_const (c
[0], c
[1], mode
);
407 /* Cast a target constant CST to target CHAR and if that value fits into
408 host char type, return zero and put that value into variable pointed by
412 target_char_cast (tree cst
, char *p
)
414 unsigned HOST_WIDE_INT val
, hostval
;
416 if (!host_integerp (cst
, 1)
417 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
420 val
= tree_low_cst (cst
, 1);
421 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
422 val
&= (((unsigned HOST_WIDE_INT
) 1) << CHAR_TYPE_SIZE
) - 1;
425 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
426 hostval
&= (((unsigned HOST_WIDE_INT
) 1) << HOST_BITS_PER_CHAR
) - 1;
435 /* Similar to save_expr, but assumes that arbitrary code is not executed
436 in between the multiple evaluations. In particular, we assume that a
437 non-addressable local variable will not be modified. */
440 builtin_save_expr (tree exp
)
442 if (TREE_ADDRESSABLE (exp
) == 0
443 && (TREE_CODE (exp
) == PARM_DECL
444 || (TREE_CODE (exp
) == VAR_DECL
&& !TREE_STATIC (exp
))))
447 return save_expr (exp
);
450 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
451 times to get the address of either a higher stack frame, or a return
452 address located within it (depending on FNDECL_CODE). */
455 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
,
460 /* Some machines need special handling before we can access
461 arbitrary frames. For example, on the sparc, we must first flush
462 all register windows to the stack. */
463 #ifdef SETUP_FRAME_ADDRESSES
465 SETUP_FRAME_ADDRESSES ();
468 /* On the sparc, the return address is not in the frame, it is in a
469 register. There is no way to access it off of the current frame
470 pointer, but it can be accessed off the previous frame pointer by
471 reading the value from the register window save area. */
472 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
473 if (fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
477 /* Scan back COUNT frames to the specified frame. */
478 for (i
= 0; i
< count
; i
++)
480 /* Assume the dynamic chain pointer is in the word that the
481 frame address points to, unless otherwise specified. */
482 #ifdef DYNAMIC_CHAIN_ADDRESS
483 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
485 tem
= memory_address (Pmode
, tem
);
486 tem
= gen_rtx_MEM (Pmode
, tem
);
487 set_mem_alias_set (tem
, get_frame_alias_set ());
488 tem
= copy_to_reg (tem
);
491 /* For __builtin_frame_address, return what we've got. */
492 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
495 /* For __builtin_return_address, Get the return address from that
497 #ifdef RETURN_ADDR_RTX
498 tem
= RETURN_ADDR_RTX (count
, tem
);
500 tem
= memory_address (Pmode
,
501 plus_constant (tem
, GET_MODE_SIZE (Pmode
)));
502 tem
= gen_rtx_MEM (Pmode
, tem
);
503 set_mem_alias_set (tem
, get_frame_alias_set ());
508 /* Alias set used for setjmp buffer. */
509 static HOST_WIDE_INT setjmp_alias_set
= -1;
511 /* Construct the leading half of a __builtin_setjmp call. Control will
512 return to RECEIVER_LABEL. This is used directly by sjlj exception
516 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
518 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
522 if (setjmp_alias_set
== -1)
523 setjmp_alias_set
= new_alias_set ();
525 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
527 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
529 /* We store the frame pointer and the address of receiver_label in
530 the buffer and use the rest of it for the stack save area, which
531 is machine-dependent. */
533 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
534 set_mem_alias_set (mem
, setjmp_alias_set
);
535 emit_move_insn (mem
, targetm
.builtin_setjmp_frame_value ());
537 mem
= gen_rtx_MEM (Pmode
, plus_constant (buf_addr
, GET_MODE_SIZE (Pmode
))),
538 set_mem_alias_set (mem
, setjmp_alias_set
);
540 emit_move_insn (validize_mem (mem
),
541 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
543 stack_save
= gen_rtx_MEM (sa_mode
,
544 plus_constant (buf_addr
,
545 2 * GET_MODE_SIZE (Pmode
)));
546 set_mem_alias_set (stack_save
, setjmp_alias_set
);
547 emit_stack_save (SAVE_NONLOCAL
, &stack_save
, NULL_RTX
);
549 /* If there is further processing to do, do it. */
550 #ifdef HAVE_builtin_setjmp_setup
551 if (HAVE_builtin_setjmp_setup
)
552 emit_insn (gen_builtin_setjmp_setup (buf_addr
));
555 /* Tell optimize_save_area_alloca that extra work is going to
556 need to go on during alloca. */
557 current_function_calls_setjmp
= 1;
559 /* Set this so all the registers get saved in our frame; we need to be
560 able to copy the saved values for any registers from frames we unwind. */
561 current_function_has_nonlocal_label
= 1;
564 /* Construct the trailing part of a __builtin_setjmp call.
565 This is used directly by sjlj exception handling code. */
568 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED
)
570 /* Clobber the FP when we get here, so we have to make sure it's
571 marked as used by this function. */
572 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
574 /* Mark the static chain as clobbered here so life information
575 doesn't get messed up for it. */
576 emit_insn (gen_rtx_CLOBBER (VOIDmode
, static_chain_rtx
));
578 /* Now put in the code to restore the frame pointer, and argument
579 pointer, if needed. */
580 #ifdef HAVE_nonlocal_goto
581 if (! HAVE_nonlocal_goto
)
583 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
585 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
586 if (fixed_regs
[ARG_POINTER_REGNUM
])
588 #ifdef ELIMINABLE_REGS
590 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
592 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
593 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
594 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
597 if (i
== ARRAY_SIZE (elim_regs
))
600 /* Now restore our arg pointer from the address at which it
601 was saved in our stack frame. */
602 emit_move_insn (virtual_incoming_args_rtx
,
603 copy_to_reg (get_arg_pointer_save_area (cfun
)));
608 #ifdef HAVE_builtin_setjmp_receiver
609 if (HAVE_builtin_setjmp_receiver
)
610 emit_insn (gen_builtin_setjmp_receiver (receiver_label
));
613 #ifdef HAVE_nonlocal_goto_receiver
614 if (HAVE_nonlocal_goto_receiver
)
615 emit_insn (gen_nonlocal_goto_receiver ());
620 /* @@@ This is a kludge. Not all machine descriptions define a blockage
621 insn, but we must not allow the code we just generated to be reordered
622 by scheduling. Specifically, the update of the frame pointer must
623 happen immediately, not later. So emit an ASM_INPUT to act as blockage
625 emit_insn (gen_rtx_ASM_INPUT (VOIDmode
, ""));
628 /* __builtin_setjmp is passed a pointer to an array of five words (not
629 all will be used on all machines). It operates similarly to the C
630 library function of the same name, but is more efficient. Much of
631 the code below (and for longjmp) is copied from the handling of
634 NOTE: This is intended for use by GNAT and the exception handling
635 scheme in the compiler and will only work in the method used by
639 expand_builtin_setjmp (tree arglist
, rtx target
)
641 rtx buf_addr
, next_lab
, cont_lab
;
643 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
646 if (target
== 0 || !REG_P (target
)
647 || REGNO (target
) < FIRST_PSEUDO_REGISTER
)
648 target
= gen_reg_rtx (TYPE_MODE (integer_type_node
));
650 buf_addr
= expand_expr (TREE_VALUE (arglist
), NULL_RTX
, VOIDmode
, 0);
652 next_lab
= gen_label_rtx ();
653 cont_lab
= gen_label_rtx ();
655 expand_builtin_setjmp_setup (buf_addr
, next_lab
);
657 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
658 ensure that pending stack adjustments are flushed. */
659 emit_move_insn (target
, const0_rtx
);
660 emit_jump (cont_lab
);
662 emit_label (next_lab
);
664 expand_builtin_setjmp_receiver (next_lab
);
666 /* Set TARGET to one. */
667 emit_move_insn (target
, const1_rtx
);
668 emit_label (cont_lab
);
670 /* Tell flow about the strange goings on. Putting `next_lab' on
671 `nonlocal_goto_handler_labels' to indicates that function
672 calls may traverse the arc back to this label. */
674 current_function_has_nonlocal_label
= 1;
675 nonlocal_goto_handler_labels
676 = gen_rtx_EXPR_LIST (VOIDmode
, next_lab
, nonlocal_goto_handler_labels
);
681 /* __builtin_longjmp is passed a pointer to an array of five words (not
682 all will be used on all machines). It operates similarly to the C
683 library function of the same name, but is more efficient. Much of
684 the code below is copied from the handling of non-local gotos.
686 NOTE: This is intended for use by GNAT and the exception handling
687 scheme in the compiler and will only work in the method used by
691 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
693 rtx fp
, lab
, stack
, insn
, last
;
694 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
696 if (setjmp_alias_set
== -1)
697 setjmp_alias_set
= new_alias_set ();
699 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
701 buf_addr
= force_reg (Pmode
, buf_addr
);
703 /* We used to store value in static_chain_rtx, but that fails if pointers
704 are smaller than integers. We instead require that the user must pass
705 a second argument of 1, because that is what builtin_setjmp will
706 return. This also makes EH slightly more efficient, since we are no
707 longer copying around a value that we don't care about. */
708 gcc_assert (value
== const1_rtx
);
710 last
= get_last_insn ();
711 #ifdef HAVE_builtin_longjmp
712 if (HAVE_builtin_longjmp
)
713 emit_insn (gen_builtin_longjmp (buf_addr
));
717 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
718 lab
= gen_rtx_MEM (Pmode
, plus_constant (buf_addr
,
719 GET_MODE_SIZE (Pmode
)));
721 stack
= gen_rtx_MEM (sa_mode
, plus_constant (buf_addr
,
722 2 * GET_MODE_SIZE (Pmode
)));
723 set_mem_alias_set (fp
, setjmp_alias_set
);
724 set_mem_alias_set (lab
, setjmp_alias_set
);
725 set_mem_alias_set (stack
, setjmp_alias_set
);
727 /* Pick up FP, label, and SP from the block and jump. This code is
728 from expand_goto in stmt.c; see there for detailed comments. */
729 #if HAVE_nonlocal_goto
730 if (HAVE_nonlocal_goto
)
731 /* We have to pass a value to the nonlocal_goto pattern that will
732 get copied into the static_chain pointer, but it does not matter
733 what that value is, because builtin_setjmp does not use it. */
734 emit_insn (gen_nonlocal_goto (value
, lab
, stack
, fp
));
738 lab
= copy_to_reg (lab
);
740 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
741 gen_rtx_MEM (BLKmode
,
742 gen_rtx_SCRATCH (VOIDmode
))));
743 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
744 gen_rtx_MEM (BLKmode
,
745 hard_frame_pointer_rtx
)));
747 emit_move_insn (hard_frame_pointer_rtx
, fp
);
748 emit_stack_restore (SAVE_NONLOCAL
, stack
, NULL_RTX
);
750 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
751 emit_insn (gen_rtx_USE (VOIDmode
, stack_pointer_rtx
));
752 emit_indirect_jump (lab
);
756 /* Search backwards and mark the jump insn as a non-local goto.
757 Note that this precludes the use of __builtin_longjmp to a
758 __builtin_setjmp target in the same function. However, we've
759 already cautioned the user that these functions are for
760 internal exception handling use only. */
761 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
763 gcc_assert (insn
!= last
);
767 REG_NOTES (insn
) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO
, const0_rtx
,
771 else if (CALL_P (insn
))
776 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
777 and the address of the save area. */
780 expand_builtin_nonlocal_goto (tree arglist
)
782 tree t_label
, t_save_area
;
783 rtx r_label
, r_save_area
, r_fp
, r_sp
, insn
;
785 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
788 t_label
= TREE_VALUE (arglist
);
789 arglist
= TREE_CHAIN (arglist
);
790 t_save_area
= TREE_VALUE (arglist
);
792 r_label
= expand_expr (t_label
, NULL_RTX
, VOIDmode
, 0);
793 r_label
= convert_memory_address (Pmode
, r_label
);
794 r_save_area
= expand_expr (t_save_area
, NULL_RTX
, VOIDmode
, 0);
795 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
796 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
797 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
798 plus_constant (r_save_area
, GET_MODE_SIZE (Pmode
)));
800 current_function_has_nonlocal_goto
= 1;
802 #if HAVE_nonlocal_goto
803 /* ??? We no longer need to pass the static chain value, afaik. */
804 if (HAVE_nonlocal_goto
)
805 emit_insn (gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
809 r_label
= copy_to_reg (r_label
);
811 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
812 gen_rtx_MEM (BLKmode
,
813 gen_rtx_SCRATCH (VOIDmode
))));
815 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
816 gen_rtx_MEM (BLKmode
,
817 hard_frame_pointer_rtx
)));
819 /* Restore frame pointer for containing function.
820 This sets the actual hard register used for the frame pointer
821 to the location of the function's incoming static chain info.
822 The non-local goto handler will then adjust it to contain the
823 proper value and reload the argument pointer, if needed. */
824 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
825 emit_stack_restore (SAVE_NONLOCAL
, r_sp
, NULL_RTX
);
827 /* USE of hard_frame_pointer_rtx added for consistency;
828 not clear if really needed. */
829 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
830 emit_insn (gen_rtx_USE (VOIDmode
, stack_pointer_rtx
));
831 emit_indirect_jump (r_label
);
834 /* Search backwards to the jump insn and mark it as a
836 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
840 REG_NOTES (insn
) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO
,
841 const0_rtx
, REG_NOTES (insn
));
844 else if (CALL_P (insn
))
851 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
852 (not all will be used on all machines) that was passed to __builtin_setjmp.
853 It updates the stack pointer in that block to correspond to the current
857 expand_builtin_update_setjmp_buf (rtx buf_addr
)
859 enum machine_mode sa_mode
= Pmode
;
863 #ifdef HAVE_save_stack_nonlocal
864 if (HAVE_save_stack_nonlocal
)
865 sa_mode
= insn_data
[(int) CODE_FOR_save_stack_nonlocal
].operand
[0].mode
;
867 #ifdef STACK_SAVEAREA_MODE
868 sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
872 = gen_rtx_MEM (sa_mode
,
875 plus_constant (buf_addr
, 2 * GET_MODE_SIZE (Pmode
))));
879 emit_insn (gen_setjmp ());
882 emit_stack_save (SAVE_NONLOCAL
, &stack_save
, NULL_RTX
);
885 /* Expand a call to __builtin_prefetch. For a target that does not support
886 data prefetch, evaluate the memory address argument in case it has side
890 expand_builtin_prefetch (tree arglist
)
892 tree arg0
, arg1
, arg2
;
895 if (!validate_arglist (arglist
, POINTER_TYPE
, 0))
898 arg0
= TREE_VALUE (arglist
);
899 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
900 zero (read) and argument 2 (locality) defaults to 3 (high degree of
902 if (TREE_CHAIN (arglist
))
904 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
905 if (TREE_CHAIN (TREE_CHAIN (arglist
)))
906 arg2
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
908 arg2
= build_int_cst (NULL_TREE
, 3);
912 arg1
= integer_zero_node
;
913 arg2
= build_int_cst (NULL_TREE
, 3);
916 /* Argument 0 is an address. */
917 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
919 /* Argument 1 (read/write flag) must be a compile-time constant int. */
920 if (TREE_CODE (arg1
) != INTEGER_CST
)
922 error ("second argument to %<__builtin_prefetch%> must be a constant");
923 arg1
= integer_zero_node
;
925 op1
= expand_expr (arg1
, NULL_RTX
, VOIDmode
, 0);
926 /* Argument 1 must be either zero or one. */
927 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
929 warning ("invalid second argument to %<__builtin_prefetch%>;"
934 /* Argument 2 (locality) must be a compile-time constant int. */
935 if (TREE_CODE (arg2
) != INTEGER_CST
)
937 error ("third argument to %<__builtin_prefetch%> must be a constant");
938 arg2
= integer_zero_node
;
940 op2
= expand_expr (arg2
, NULL_RTX
, VOIDmode
, 0);
941 /* Argument 2 must be 0, 1, 2, or 3. */
942 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
944 warning ("invalid third argument to %<__builtin_prefetch%>; using zero");
951 if ((! (*insn_data
[(int) CODE_FOR_prefetch
].operand
[0].predicate
)
953 insn_data
[(int) CODE_FOR_prefetch
].operand
[0].mode
))
954 || (GET_MODE (op0
) != Pmode
))
956 op0
= convert_memory_address (Pmode
, op0
);
957 op0
= force_reg (Pmode
, op0
);
959 emit_insn (gen_prefetch (op0
, op1
, op2
));
963 /* Don't do anything with direct references to volatile memory, but
964 generate code to handle other side effects. */
965 if (!MEM_P (op0
) && side_effects_p (op0
))
969 /* Get a MEM rtx for expression EXP which is the address of an operand
970 to be used to be used in a string instruction (cmpstrsi, movmemsi, ..). */
973 get_memory_rtx (tree exp
)
975 rtx addr
= expand_expr (exp
, NULL_RTX
, ptr_mode
, EXPAND_SUM
);
978 addr
= convert_memory_address (Pmode
, addr
);
980 mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
982 /* Get an expression we can use to find the attributes to assign to MEM.
983 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
984 we can. First remove any nops. */
985 while ((TREE_CODE (exp
) == NOP_EXPR
|| TREE_CODE (exp
) == CONVERT_EXPR
986 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
987 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
988 exp
= TREE_OPERAND (exp
, 0);
990 if (TREE_CODE (exp
) == ADDR_EXPR
)
991 exp
= TREE_OPERAND (exp
, 0);
992 else if (POINTER_TYPE_P (TREE_TYPE (exp
)))
993 exp
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (exp
)), exp
);
997 /* Honor attributes derived from exp, except for the alias set
998 (as builtin stringops may alias with anything) and the size
999 (as stringops may access multiple array elements). */
1002 set_mem_attributes (mem
, exp
, 0);
1003 set_mem_alias_set (mem
, 0);
1004 set_mem_size (mem
, NULL_RTX
);
1010 /* Built-in functions to perform an untyped call and return. */
1012 /* For each register that may be used for calling a function, this
1013 gives a mode used to copy the register's value. VOIDmode indicates
1014 the register is not used for calling a function. If the machine
1015 has register windows, this gives only the outbound registers.
1016 INCOMING_REGNO gives the corresponding inbound register. */
1017 static enum machine_mode apply_args_mode
[FIRST_PSEUDO_REGISTER
];
1019 /* For each register that may be used for returning values, this gives
1020 a mode used to copy the register's value. VOIDmode indicates the
1021 register is not used for returning values. If the machine has
1022 register windows, this gives only the outbound registers.
1023 INCOMING_REGNO gives the corresponding inbound register. */
1024 static enum machine_mode apply_result_mode
[FIRST_PSEUDO_REGISTER
];
1026 /* For each register that may be used for calling a function, this
1027 gives the offset of that register into the block returned by
1028 __builtin_apply_args. 0 indicates that the register is not
1029 used for calling a function. */
1030 static int apply_args_reg_offset
[FIRST_PSEUDO_REGISTER
];
1032 /* Return the size required for the block returned by __builtin_apply_args,
1033 and initialize apply_args_mode. */
1036 apply_args_size (void)
1038 static int size
= -1;
1041 enum machine_mode mode
;
1043 /* The values computed by this function never change. */
1046 /* The first value is the incoming arg-pointer. */
1047 size
= GET_MODE_SIZE (Pmode
);
1049 /* The second value is the structure value address unless this is
1050 passed as an "invisible" first argument. */
1051 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1052 size
+= GET_MODE_SIZE (Pmode
);
1054 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1055 if (FUNCTION_ARG_REGNO_P (regno
))
1057 mode
= reg_raw_mode
[regno
];
1059 gcc_assert (mode
!= VOIDmode
);
1061 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1062 if (size
% align
!= 0)
1063 size
= CEIL (size
, align
) * align
;
1064 apply_args_reg_offset
[regno
] = size
;
1065 size
+= GET_MODE_SIZE (mode
);
1066 apply_args_mode
[regno
] = mode
;
1070 apply_args_mode
[regno
] = VOIDmode
;
1071 apply_args_reg_offset
[regno
] = 0;
1077 /* Return the size required for the block returned by __builtin_apply,
1078 and initialize apply_result_mode. */
1081 apply_result_size (void)
1083 static int size
= -1;
1085 enum machine_mode mode
;
1087 /* The values computed by this function never change. */
1092 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1093 if (FUNCTION_VALUE_REGNO_P (regno
))
1095 mode
= reg_raw_mode
[regno
];
1097 gcc_assert (mode
!= VOIDmode
);
1099 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1100 if (size
% align
!= 0)
1101 size
= CEIL (size
, align
) * align
;
1102 size
+= GET_MODE_SIZE (mode
);
1103 apply_result_mode
[regno
] = mode
;
1106 apply_result_mode
[regno
] = VOIDmode
;
1108 /* Allow targets that use untyped_call and untyped_return to override
1109 the size so that machine-specific information can be stored here. */
1110 #ifdef APPLY_RESULT_SIZE
1111 size
= APPLY_RESULT_SIZE
;
1117 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1118 /* Create a vector describing the result block RESULT. If SAVEP is true,
1119 the result block is used to save the values; otherwise it is used to
1120 restore the values. */
1123 result_vector (int savep
, rtx result
)
1125 int regno
, size
, align
, nelts
;
1126 enum machine_mode mode
;
1128 rtx
*savevec
= alloca (FIRST_PSEUDO_REGISTER
* sizeof (rtx
));
1131 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1132 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1134 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1135 if (size
% align
!= 0)
1136 size
= CEIL (size
, align
) * align
;
1137 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
1138 mem
= adjust_address (result
, mode
, size
);
1139 savevec
[nelts
++] = (savep
1140 ? gen_rtx_SET (VOIDmode
, mem
, reg
)
1141 : gen_rtx_SET (VOIDmode
, reg
, mem
));
1142 size
+= GET_MODE_SIZE (mode
);
1144 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
1146 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1148 /* Save the state required to perform an untyped call with the same
1149 arguments as were passed to the current function. */
1152 expand_builtin_apply_args_1 (void)
1155 int size
, align
, regno
;
1156 enum machine_mode mode
;
1157 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
1159 /* Create a block where the arg-pointer, structure value address,
1160 and argument registers can be saved. */
1161 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
1163 /* Walk past the arg-pointer and structure value address. */
1164 size
= GET_MODE_SIZE (Pmode
);
1165 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1166 size
+= GET_MODE_SIZE (Pmode
);
1168 /* Save each register used in calling a function to the block. */
1169 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1170 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1172 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1173 if (size
% align
!= 0)
1174 size
= CEIL (size
, align
) * align
;
1176 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1178 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
1179 size
+= GET_MODE_SIZE (mode
);
1182 /* Save the arg pointer to the block. */
1183 tem
= copy_to_reg (virtual_incoming_args_rtx
);
1184 #ifdef STACK_GROWS_DOWNWARD
1185 /* We need the pointer as the caller actually passed them to us, not
1186 as we might have pretended they were passed. Make sure it's a valid
1187 operand, as emit_move_insn isn't expected to handle a PLUS. */
1189 = force_operand (plus_constant (tem
, current_function_pretend_args_size
),
1192 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
1194 size
= GET_MODE_SIZE (Pmode
);
1196 /* Save the structure value address unless this is passed as an
1197 "invisible" first argument. */
1198 if (struct_incoming_value
)
1200 emit_move_insn (adjust_address (registers
, Pmode
, size
),
1201 copy_to_reg (struct_incoming_value
));
1202 size
+= GET_MODE_SIZE (Pmode
);
1205 /* Return the address of the block. */
1206 return copy_addr_to_reg (XEXP (registers
, 0));
1209 /* __builtin_apply_args returns block of memory allocated on
1210 the stack into which is stored the arg pointer, structure
1211 value address, static chain, and all the registers that might
1212 possibly be used in performing a function call. The code is
1213 moved to the start of the function so the incoming values are
1217 expand_builtin_apply_args (void)
1219 /* Don't do __builtin_apply_args more than once in a function.
1220 Save the result of the first call and reuse it. */
1221 if (apply_args_value
!= 0)
1222 return apply_args_value
;
1224 /* When this function is called, it means that registers must be
1225 saved on entry to this function. So we migrate the
1226 call to the first insn of this function. */
1231 temp
= expand_builtin_apply_args_1 ();
1235 apply_args_value
= temp
;
1237 /* Put the insns after the NOTE that starts the function.
1238 If this is inside a start_sequence, make the outer-level insn
1239 chain current, so the code is placed at the start of the
1241 push_topmost_sequence ();
1242 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
1243 pop_topmost_sequence ();
1248 /* Perform an untyped call and save the state required to perform an
1249 untyped return of whatever value was returned by the given function. */
1252 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
1254 int size
, align
, regno
;
1255 enum machine_mode mode
;
1256 rtx incoming_args
, result
, reg
, dest
, src
, call_insn
;
1257 rtx old_stack_level
= 0;
1258 rtx call_fusage
= 0;
1259 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
1261 arguments
= convert_memory_address (Pmode
, arguments
);
1263 /* Create a block where the return registers can be saved. */
1264 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
1266 /* Fetch the arg pointer from the ARGUMENTS block. */
1267 incoming_args
= gen_reg_rtx (Pmode
);
1268 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
1269 #ifndef STACK_GROWS_DOWNWARD
1270 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
1271 incoming_args
, 0, OPTAB_LIB_WIDEN
);
1274 /* Push a new argument block and copy the arguments. Do not allow
1275 the (potential) memcpy call below to interfere with our stack
1277 do_pending_stack_adjust ();
1280 /* Save the stack with nonlocal if available. */
1281 #ifdef HAVE_save_stack_nonlocal
1282 if (HAVE_save_stack_nonlocal
)
1283 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
, NULL_RTX
);
1286 emit_stack_save (SAVE_BLOCK
, &old_stack_level
, NULL_RTX
);
1288 /* Allocate a block of memory onto the stack and copy the memory
1289 arguments to the outgoing arguments address. */
1290 allocate_dynamic_stack_space (argsize
, 0, BITS_PER_UNIT
);
1291 dest
= virtual_outgoing_args_rtx
;
1292 #ifndef STACK_GROWS_DOWNWARD
1293 if (GET_CODE (argsize
) == CONST_INT
)
1294 dest
= plus_constant (dest
, -INTVAL (argsize
));
1296 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
1298 dest
= gen_rtx_MEM (BLKmode
, dest
);
1299 set_mem_align (dest
, PARM_BOUNDARY
);
1300 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
1301 set_mem_align (src
, PARM_BOUNDARY
);
1302 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
1304 /* Refer to the argument block. */
1306 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
1307 set_mem_align (arguments
, PARM_BOUNDARY
);
1309 /* Walk past the arg-pointer and structure value address. */
1310 size
= GET_MODE_SIZE (Pmode
);
1312 size
+= GET_MODE_SIZE (Pmode
);
1314 /* Restore each of the registers previously saved. Make USE insns
1315 for each of these registers for use in making the call. */
1316 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1317 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1319 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1320 if (size
% align
!= 0)
1321 size
= CEIL (size
, align
) * align
;
1322 reg
= gen_rtx_REG (mode
, regno
);
1323 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
1324 use_reg (&call_fusage
, reg
);
1325 size
+= GET_MODE_SIZE (mode
);
1328 /* Restore the structure value address unless this is passed as an
1329 "invisible" first argument. */
1330 size
= GET_MODE_SIZE (Pmode
);
1333 rtx value
= gen_reg_rtx (Pmode
);
1334 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
1335 emit_move_insn (struct_value
, value
);
1336 if (REG_P (struct_value
))
1337 use_reg (&call_fusage
, struct_value
);
1338 size
+= GET_MODE_SIZE (Pmode
);
1341 /* All arguments and registers used for the call are set up by now! */
1342 function
= prepare_call_address (function
, NULL
, &call_fusage
, 0, 0);
1344 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1345 and we don't want to load it into a register as an optimization,
1346 because prepare_call_address already did it if it should be done. */
1347 if (GET_CODE (function
) != SYMBOL_REF
)
1348 function
= memory_address (FUNCTION_MODE
, function
);
1350 /* Generate the actual call instruction and save the return value. */
1351 #ifdef HAVE_untyped_call
1352 if (HAVE_untyped_call
)
1353 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE
, function
),
1354 result
, result_vector (1, result
)));
1357 #ifdef HAVE_call_value
1358 if (HAVE_call_value
)
1362 /* Locate the unique return register. It is not possible to
1363 express a call that sets more than one return register using
1364 call_value; use untyped_call for that. In fact, untyped_call
1365 only needs to save the return registers in the given block. */
1366 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1367 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1369 gcc_assert (!valreg
); /* HAVE_untyped_call required. */
1371 valreg
= gen_rtx_REG (mode
, regno
);
1374 emit_call_insn (GEN_CALL_VALUE (valreg
,
1375 gen_rtx_MEM (FUNCTION_MODE
, function
),
1376 const0_rtx
, NULL_RTX
, const0_rtx
));
1378 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
1384 /* Find the CALL insn we just emitted, and attach the register usage
1386 call_insn
= last_call_insn ();
1387 add_function_usage_to (call_insn
, call_fusage
);
1389 /* Restore the stack. */
1390 #ifdef HAVE_save_stack_nonlocal
1391 if (HAVE_save_stack_nonlocal
)
1392 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
, NULL_RTX
);
1395 emit_stack_restore (SAVE_BLOCK
, old_stack_level
, NULL_RTX
);
1399 /* Return the address of the result block. */
1400 result
= copy_addr_to_reg (XEXP (result
, 0));
1401 return convert_memory_address (ptr_mode
, result
);
1404 /* Perform an untyped return. */
1407 expand_builtin_return (rtx result
)
1409 int size
, align
, regno
;
1410 enum machine_mode mode
;
1412 rtx call_fusage
= 0;
1414 result
= convert_memory_address (Pmode
, result
);
1416 apply_result_size ();
1417 result
= gen_rtx_MEM (BLKmode
, result
);
1419 #ifdef HAVE_untyped_return
1420 if (HAVE_untyped_return
)
1422 emit_jump_insn (gen_untyped_return (result
, result_vector (0, result
)));
1428 /* Restore the return value and note that each value is used. */
1430 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1431 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1433 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1434 if (size
% align
!= 0)
1435 size
= CEIL (size
, align
) * align
;
1436 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1437 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
1439 push_to_sequence (call_fusage
);
1440 emit_insn (gen_rtx_USE (VOIDmode
, reg
));
1441 call_fusage
= get_insns ();
1443 size
+= GET_MODE_SIZE (mode
);
1446 /* Put the USE insns before the return. */
1447 emit_insn (call_fusage
);
1449 /* Return whatever values was restored by jumping directly to the end
1451 expand_naked_return ();
1454 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1456 static enum type_class
1457 type_to_class (tree type
)
1459 switch (TREE_CODE (type
))
1461 case VOID_TYPE
: return void_type_class
;
1462 case INTEGER_TYPE
: return integer_type_class
;
1463 case CHAR_TYPE
: return char_type_class
;
1464 case ENUMERAL_TYPE
: return enumeral_type_class
;
1465 case BOOLEAN_TYPE
: return boolean_type_class
;
1466 case POINTER_TYPE
: return pointer_type_class
;
1467 case REFERENCE_TYPE
: return reference_type_class
;
1468 case OFFSET_TYPE
: return offset_type_class
;
1469 case REAL_TYPE
: return real_type_class
;
1470 case COMPLEX_TYPE
: return complex_type_class
;
1471 case FUNCTION_TYPE
: return function_type_class
;
1472 case METHOD_TYPE
: return method_type_class
;
1473 case RECORD_TYPE
: return record_type_class
;
1475 case QUAL_UNION_TYPE
: return union_type_class
;
1476 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
1477 ? string_type_class
: array_type_class
);
1478 case FILE_TYPE
: return file_type_class
;
1479 case LANG_TYPE
: return lang_type_class
;
1480 default: return no_type_class
;
1484 /* Expand a call to __builtin_classify_type with arguments found in
1488 expand_builtin_classify_type (tree arglist
)
1491 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist
))));
1492 return GEN_INT (no_type_class
);
1495 /* This helper macro, meant to be used in mathfn_built_in below,
1496 determines which among a set of three builtin math functions is
1497 appropriate for a given type mode. The `F' and `L' cases are
1498 automatically generated from the `double' case. */
1499 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1500 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1501 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1502 fcodel = BUILT_IN_MATHFN##L ; break;
1504 /* Return mathematic function equivalent to FN but operating directly
1505 on TYPE, if available. If we can't do the conversion, return zero. */
1507 mathfn_built_in (tree type
, enum built_in_function fn
)
1509 enum built_in_function fcode
, fcodef
, fcodel
;
1513 CASE_MATHFN (BUILT_IN_ACOS
)
1514 CASE_MATHFN (BUILT_IN_ACOSH
)
1515 CASE_MATHFN (BUILT_IN_ASIN
)
1516 CASE_MATHFN (BUILT_IN_ASINH
)
1517 CASE_MATHFN (BUILT_IN_ATAN
)
1518 CASE_MATHFN (BUILT_IN_ATAN2
)
1519 CASE_MATHFN (BUILT_IN_ATANH
)
1520 CASE_MATHFN (BUILT_IN_CBRT
)
1521 CASE_MATHFN (BUILT_IN_CEIL
)
1522 CASE_MATHFN (BUILT_IN_COPYSIGN
)
1523 CASE_MATHFN (BUILT_IN_COS
)
1524 CASE_MATHFN (BUILT_IN_COSH
)
1525 CASE_MATHFN (BUILT_IN_DREM
)
1526 CASE_MATHFN (BUILT_IN_ERF
)
1527 CASE_MATHFN (BUILT_IN_ERFC
)
1528 CASE_MATHFN (BUILT_IN_EXP
)
1529 CASE_MATHFN (BUILT_IN_EXP10
)
1530 CASE_MATHFN (BUILT_IN_EXP2
)
1531 CASE_MATHFN (BUILT_IN_EXPM1
)
1532 CASE_MATHFN (BUILT_IN_FABS
)
1533 CASE_MATHFN (BUILT_IN_FDIM
)
1534 CASE_MATHFN (BUILT_IN_FLOOR
)
1535 CASE_MATHFN (BUILT_IN_FMA
)
1536 CASE_MATHFN (BUILT_IN_FMAX
)
1537 CASE_MATHFN (BUILT_IN_FMIN
)
1538 CASE_MATHFN (BUILT_IN_FMOD
)
1539 CASE_MATHFN (BUILT_IN_FREXP
)
1540 CASE_MATHFN (BUILT_IN_GAMMA
)
1541 CASE_MATHFN (BUILT_IN_HUGE_VAL
)
1542 CASE_MATHFN (BUILT_IN_HYPOT
)
1543 CASE_MATHFN (BUILT_IN_ILOGB
)
1544 CASE_MATHFN (BUILT_IN_INF
)
1545 CASE_MATHFN (BUILT_IN_J0
)
1546 CASE_MATHFN (BUILT_IN_J1
)
1547 CASE_MATHFN (BUILT_IN_JN
)
1548 CASE_MATHFN (BUILT_IN_LDEXP
)
1549 CASE_MATHFN (BUILT_IN_LGAMMA
)
1550 CASE_MATHFN (BUILT_IN_LLRINT
)
1551 CASE_MATHFN (BUILT_IN_LLROUND
)
1552 CASE_MATHFN (BUILT_IN_LOG
)
1553 CASE_MATHFN (BUILT_IN_LOG10
)
1554 CASE_MATHFN (BUILT_IN_LOG1P
)
1555 CASE_MATHFN (BUILT_IN_LOG2
)
1556 CASE_MATHFN (BUILT_IN_LOGB
)
1557 CASE_MATHFN (BUILT_IN_LRINT
)
1558 CASE_MATHFN (BUILT_IN_LROUND
)
1559 CASE_MATHFN (BUILT_IN_MODF
)
1560 CASE_MATHFN (BUILT_IN_NAN
)
1561 CASE_MATHFN (BUILT_IN_NANS
)
1562 CASE_MATHFN (BUILT_IN_NEARBYINT
)
1563 CASE_MATHFN (BUILT_IN_NEXTAFTER
)
1564 CASE_MATHFN (BUILT_IN_NEXTTOWARD
)
1565 CASE_MATHFN (BUILT_IN_POW
)
1566 CASE_MATHFN (BUILT_IN_POW10
)
1567 CASE_MATHFN (BUILT_IN_REMAINDER
)
1568 CASE_MATHFN (BUILT_IN_REMQUO
)
1569 CASE_MATHFN (BUILT_IN_RINT
)
1570 CASE_MATHFN (BUILT_IN_ROUND
)
1571 CASE_MATHFN (BUILT_IN_SCALB
)
1572 CASE_MATHFN (BUILT_IN_SCALBLN
)
1573 CASE_MATHFN (BUILT_IN_SCALBN
)
1574 CASE_MATHFN (BUILT_IN_SIGNIFICAND
)
1575 CASE_MATHFN (BUILT_IN_SIN
)
1576 CASE_MATHFN (BUILT_IN_SINCOS
)
1577 CASE_MATHFN (BUILT_IN_SINH
)
1578 CASE_MATHFN (BUILT_IN_SQRT
)
1579 CASE_MATHFN (BUILT_IN_TAN
)
1580 CASE_MATHFN (BUILT_IN_TANH
)
1581 CASE_MATHFN (BUILT_IN_TGAMMA
)
1582 CASE_MATHFN (BUILT_IN_TRUNC
)
1583 CASE_MATHFN (BUILT_IN_Y0
)
1584 CASE_MATHFN (BUILT_IN_Y1
)
1585 CASE_MATHFN (BUILT_IN_YN
)
1591 if (TYPE_MAIN_VARIANT (type
) == double_type_node
)
1592 return implicit_built_in_decls
[fcode
];
1593 else if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1594 return implicit_built_in_decls
[fcodef
];
1595 else if (TYPE_MAIN_VARIANT (type
) == long_double_type_node
)
1596 return implicit_built_in_decls
[fcodel
];
1601 /* If errno must be maintained, expand the RTL to check if the result,
1602 TARGET, of a built-in function call, EXP, is NaN, and if so set
1606 expand_errno_check (tree exp
, rtx target
)
1608 rtx lab
= gen_label_rtx ();
1610 /* Test the result; if it is NaN, set errno=EDOM because
1611 the argument was not in the domain. */
1612 emit_cmp_and_jump_insns (target
, target
, EQ
, 0, GET_MODE (target
),
1616 /* If this built-in doesn't throw an exception, set errno directly. */
1617 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp
, 0), 0)))
1619 #ifdef GEN_ERRNO_RTX
1620 rtx errno_rtx
= GEN_ERRNO_RTX
;
1623 = gen_rtx_MEM (word_mode
, gen_rtx_SYMBOL_REF (Pmode
, "errno"));
1625 emit_move_insn (errno_rtx
, GEN_INT (TARGET_EDOM
));
1631 /* We can't set errno=EDOM directly; let the library call do it.
1632 Pop the arguments right away in case the call gets deleted. */
1634 expand_call (exp
, target
, 0);
1640 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1641 Return 0 if a normal call should be emitted rather than expanding the
1642 function in-line. EXP is the expression that is a call to the builtin
1643 function; if convenient, the result should be placed in TARGET.
1644 SUBTARGET may be used as the target for computing one of EXP's operands. */
1647 expand_builtin_mathfn (tree exp
, rtx target
, rtx subtarget
)
1649 optab builtin_optab
;
1650 rtx op0
, insns
, before_call
;
1651 tree fndecl
= get_callee_fndecl (exp
);
1652 tree arglist
= TREE_OPERAND (exp
, 1);
1653 enum machine_mode mode
;
1654 bool errno_set
= false;
1657 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
1660 arg
= TREE_VALUE (arglist
);
1662 switch (DECL_FUNCTION_CODE (fndecl
))
1665 case BUILT_IN_SQRTF
:
1666 case BUILT_IN_SQRTL
:
1667 errno_set
= ! tree_expr_nonnegative_p (arg
);
1668 builtin_optab
= sqrt_optab
;
1673 errno_set
= true; builtin_optab
= exp_optab
; break;
1674 case BUILT_IN_EXP10
:
1675 case BUILT_IN_EXP10F
:
1676 case BUILT_IN_EXP10L
:
1677 case BUILT_IN_POW10
:
1678 case BUILT_IN_POW10F
:
1679 case BUILT_IN_POW10L
:
1680 errno_set
= true; builtin_optab
= exp10_optab
; break;
1682 case BUILT_IN_EXP2F
:
1683 case BUILT_IN_EXP2L
:
1684 errno_set
= true; builtin_optab
= exp2_optab
; break;
1685 case BUILT_IN_EXPM1
:
1686 case BUILT_IN_EXPM1F
:
1687 case BUILT_IN_EXPM1L
:
1688 errno_set
= true; builtin_optab
= expm1_optab
; break;
1690 case BUILT_IN_LOGBF
:
1691 case BUILT_IN_LOGBL
:
1692 errno_set
= true; builtin_optab
= logb_optab
; break;
1693 case BUILT_IN_ILOGB
:
1694 case BUILT_IN_ILOGBF
:
1695 case BUILT_IN_ILOGBL
:
1696 errno_set
= true; builtin_optab
= ilogb_optab
; break;
1700 errno_set
= true; builtin_optab
= log_optab
; break;
1701 case BUILT_IN_LOG10
:
1702 case BUILT_IN_LOG10F
:
1703 case BUILT_IN_LOG10L
:
1704 errno_set
= true; builtin_optab
= log10_optab
; break;
1706 case BUILT_IN_LOG2F
:
1707 case BUILT_IN_LOG2L
:
1708 errno_set
= true; builtin_optab
= log2_optab
; break;
1709 case BUILT_IN_LOG1P
:
1710 case BUILT_IN_LOG1PF
:
1711 case BUILT_IN_LOG1PL
:
1712 errno_set
= true; builtin_optab
= log1p_optab
; break;
1714 case BUILT_IN_ASINF
:
1715 case BUILT_IN_ASINL
:
1716 builtin_optab
= asin_optab
; break;
1718 case BUILT_IN_ACOSF
:
1719 case BUILT_IN_ACOSL
:
1720 builtin_optab
= acos_optab
; break;
1724 builtin_optab
= tan_optab
; break;
1726 case BUILT_IN_ATANF
:
1727 case BUILT_IN_ATANL
:
1728 builtin_optab
= atan_optab
; break;
1729 case BUILT_IN_FLOOR
:
1730 case BUILT_IN_FLOORF
:
1731 case BUILT_IN_FLOORL
:
1732 builtin_optab
= floor_optab
; break;
1734 case BUILT_IN_CEILF
:
1735 case BUILT_IN_CEILL
:
1736 builtin_optab
= ceil_optab
; break;
1737 case BUILT_IN_TRUNC
:
1738 case BUILT_IN_TRUNCF
:
1739 case BUILT_IN_TRUNCL
:
1740 builtin_optab
= btrunc_optab
; break;
1741 case BUILT_IN_ROUND
:
1742 case BUILT_IN_ROUNDF
:
1743 case BUILT_IN_ROUNDL
:
1744 builtin_optab
= round_optab
; break;
1745 case BUILT_IN_NEARBYINT
:
1746 case BUILT_IN_NEARBYINTF
:
1747 case BUILT_IN_NEARBYINTL
:
1748 builtin_optab
= nearbyint_optab
; break;
1750 case BUILT_IN_RINTF
:
1751 case BUILT_IN_RINTL
:
1752 builtin_optab
= rint_optab
; break;
1757 /* Make a suitable register to place result in. */
1758 mode
= TYPE_MODE (TREE_TYPE (exp
));
1760 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
1763 /* Before working hard, check whether the instruction is available. */
1764 if (builtin_optab
->handlers
[(int) mode
].insn_code
!= CODE_FOR_nothing
)
1766 target
= gen_reg_rtx (mode
);
1768 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1769 need to expand the argument again. This way, we will not perform
1770 side-effects more the once. */
1771 narg
= builtin_save_expr (arg
);
1775 arglist
= build_tree_list (NULL_TREE
, arg
);
1776 exp
= build_function_call_expr (fndecl
, arglist
);
1779 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
1783 /* Compute into TARGET.
1784 Set TARGET to wherever the result comes back. */
1785 target
= expand_unop (mode
, builtin_optab
, op0
, target
, 0);
1790 expand_errno_check (exp
, target
);
1792 /* Output the entire sequence. */
1793 insns
= get_insns ();
1799 /* If we were unable to expand via the builtin, stop the sequence
1800 (without outputting the insns) and call to the library function
1801 with the stabilized argument list. */
1805 before_call
= get_last_insn ();
1807 target
= expand_call (exp
, target
, target
== const0_rtx
);
1809 /* If this is a sqrt operation and we don't care about errno, try to
1810 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1811 This allows the semantics of the libcall to be visible to the RTL
1813 if (builtin_optab
== sqrt_optab
&& !errno_set
)
1815 /* Search backwards through the insns emitted by expand_call looking
1816 for the instruction with the REG_RETVAL note. */
1817 rtx last
= get_last_insn ();
1818 while (last
!= before_call
)
1820 if (find_reg_note (last
, REG_RETVAL
, NULL
))
1822 rtx note
= find_reg_note (last
, REG_EQUAL
, NULL
);
1823 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1824 two elements, i.e. symbol_ref(sqrt) and the operand. */
1826 && GET_CODE (note
) == EXPR_LIST
1827 && GET_CODE (XEXP (note
, 0)) == EXPR_LIST
1828 && XEXP (XEXP (note
, 0), 1) != NULL_RTX
1829 && XEXP (XEXP (XEXP (note
, 0), 1), 1) == NULL_RTX
)
1831 rtx operand
= XEXP (XEXP (XEXP (note
, 0), 1), 0);
1832 /* Check operand is a register with expected mode. */
1835 && GET_MODE (operand
) == mode
)
1837 /* Replace the REG_EQUAL note with a SQRT rtx. */
1838 rtx equiv
= gen_rtx_SQRT (mode
, operand
);
1839 set_unique_reg_note (last
, REG_EQUAL
, equiv
);
1844 last
= PREV_INSN (last
);
1851 /* Expand a call to the builtin binary math functions (pow and atan2).
1852 Return 0 if a normal call should be emitted rather than expanding the
1853 function in-line. EXP is the expression that is a call to the builtin
1854 function; if convenient, the result should be placed in TARGET.
1855 SUBTARGET may be used as the target for computing one of EXP's
1859 expand_builtin_mathfn_2 (tree exp
, rtx target
, rtx subtarget
)
1861 optab builtin_optab
;
1862 rtx op0
, op1
, insns
;
1863 tree fndecl
= get_callee_fndecl (exp
);
1864 tree arglist
= TREE_OPERAND (exp
, 1);
1865 tree arg0
, arg1
, temp
, narg
;
1866 enum machine_mode mode
;
1867 bool errno_set
= true;
1870 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
1873 arg0
= TREE_VALUE (arglist
);
1874 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
1876 switch (DECL_FUNCTION_CODE (fndecl
))
1881 builtin_optab
= pow_optab
; break;
1882 case BUILT_IN_ATAN2
:
1883 case BUILT_IN_ATAN2F
:
1884 case BUILT_IN_ATAN2L
:
1885 builtin_optab
= atan2_optab
; break;
1887 case BUILT_IN_FMODF
:
1888 case BUILT_IN_FMODL
:
1889 builtin_optab
= fmod_optab
; break;
1891 case BUILT_IN_DREMF
:
1892 case BUILT_IN_DREML
:
1893 builtin_optab
= drem_optab
; break;
1898 /* Make a suitable register to place result in. */
1899 mode
= TYPE_MODE (TREE_TYPE (exp
));
1901 /* Before working hard, check whether the instruction is available. */
1902 if (builtin_optab
->handlers
[(int) mode
].insn_code
== CODE_FOR_nothing
)
1905 target
= gen_reg_rtx (mode
);
1907 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
1910 /* Always stabilize the argument list. */
1911 narg
= builtin_save_expr (arg1
);
1915 temp
= build_tree_list (NULL_TREE
, narg
);
1919 temp
= TREE_CHAIN (arglist
);
1921 narg
= builtin_save_expr (arg0
);
1925 arglist
= tree_cons (NULL_TREE
, narg
, temp
);
1929 arglist
= tree_cons (NULL_TREE
, arg0
, temp
);
1932 exp
= build_function_call_expr (fndecl
, arglist
);
1934 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, 0);
1935 op1
= expand_expr (arg1
, 0, VOIDmode
, 0);
1939 /* Compute into TARGET.
1940 Set TARGET to wherever the result comes back. */
1941 target
= expand_binop (mode
, builtin_optab
, op0
, op1
,
1942 target
, 0, OPTAB_DIRECT
);
1944 /* If we were unable to expand via the builtin, stop the sequence
1945 (without outputting the insns) and call to the library function
1946 with the stabilized argument list. */
1950 return expand_call (exp
, target
, target
== const0_rtx
);
1954 expand_errno_check (exp
, target
);
1956 /* Output the entire sequence. */
1957 insns
= get_insns ();
1964 /* Expand a call to the builtin sin and cos math functions.
1965 Return 0 if a normal call should be emitted rather than expanding the
1966 function in-line. EXP is the expression that is a call to the builtin
1967 function; if convenient, the result should be placed in TARGET.
1968 SUBTARGET may be used as the target for computing one of EXP's
1972 expand_builtin_mathfn_3 (tree exp
, rtx target
, rtx subtarget
)
1974 optab builtin_optab
;
1975 rtx op0
, insns
, before_call
;
1976 tree fndecl
= get_callee_fndecl (exp
);
1977 tree arglist
= TREE_OPERAND (exp
, 1);
1978 enum machine_mode mode
;
1979 bool errno_set
= false;
1982 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
1985 arg
= TREE_VALUE (arglist
);
1987 switch (DECL_FUNCTION_CODE (fndecl
))
1995 builtin_optab
= sincos_optab
; break;
2000 /* Make a suitable register to place result in. */
2001 mode
= TYPE_MODE (TREE_TYPE (exp
));
2003 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2006 /* Check if sincos insn is available, otherwise fallback
2007 to sin or cos insn. */
2008 if (builtin_optab
->handlers
[(int) mode
].insn_code
== CODE_FOR_nothing
) {
2009 switch (DECL_FUNCTION_CODE (fndecl
))
2014 builtin_optab
= sin_optab
; break;
2018 builtin_optab
= cos_optab
; break;
2024 /* Before working hard, check whether the instruction is available. */
2025 if (builtin_optab
->handlers
[(int) mode
].insn_code
!= CODE_FOR_nothing
)
2027 target
= gen_reg_rtx (mode
);
2029 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2030 need to expand the argument again. This way, we will not perform
2031 side-effects more the once. */
2032 narg
= save_expr (arg
);
2036 arglist
= build_tree_list (NULL_TREE
, arg
);
2037 exp
= build_function_call_expr (fndecl
, arglist
);
2040 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
2044 /* Compute into TARGET.
2045 Set TARGET to wherever the result comes back. */
2046 if (builtin_optab
== sincos_optab
)
2050 switch (DECL_FUNCTION_CODE (fndecl
))
2055 result
= expand_twoval_unop (builtin_optab
, op0
, 0, target
, 0);
2060 result
= expand_twoval_unop (builtin_optab
, op0
, target
, 0, 0);
2065 gcc_assert (result
);
2069 target
= expand_unop (mode
, builtin_optab
, op0
, target
, 0);
2075 expand_errno_check (exp
, target
);
2077 /* Output the entire sequence. */
2078 insns
= get_insns ();
2084 /* If we were unable to expand via the builtin, stop the sequence
2085 (without outputting the insns) and call to the library function
2086 with the stabilized argument list. */
2090 before_call
= get_last_insn ();
2092 target
= expand_call (exp
, target
, target
== const0_rtx
);
2097 /* To evaluate powi(x,n), the floating point value x raised to the
2098 constant integer exponent n, we use a hybrid algorithm that
2099 combines the "window method" with look-up tables. For an
2100 introduction to exponentiation algorithms and "addition chains",
2101 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2102 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2103 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2104 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2106 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2107 multiplications to inline before calling the system library's pow
2108 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2109 so this default never requires calling pow, powf or powl. */
2111 #ifndef POWI_MAX_MULTS
2112 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2115 /* The size of the "optimal power tree" lookup table. All
2116 exponents less than this value are simply looked up in the
2117 powi_table below. This threshold is also used to size the
2118 cache of pseudo registers that hold intermediate results. */
2119 #define POWI_TABLE_SIZE 256
2121 /* The size, in bits of the window, used in the "window method"
2122 exponentiation algorithm. This is equivalent to a radix of
2123 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2124 #define POWI_WINDOW_SIZE 3
2126 /* The following table is an efficient representation of an
2127 "optimal power tree". For each value, i, the corresponding
2128 value, j, in the table states than an optimal evaluation
2129 sequence for calculating pow(x,i) can be found by evaluating
2130 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2131 100 integers is given in Knuth's "Seminumerical algorithms". */
2133 static const unsigned char powi_table
[POWI_TABLE_SIZE
] =
2135 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2136 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2137 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2138 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2139 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2140 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2141 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2142 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2143 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2144 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2145 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2146 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2147 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2148 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2149 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2150 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2151 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2152 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2153 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2154 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2155 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2156 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2157 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2158 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2159 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2160 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2161 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2162 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2163 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2164 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2165 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2166 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2170 /* Return the number of multiplications required to calculate
2171 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2172 subroutine of powi_cost. CACHE is an array indicating
2173 which exponents have already been calculated. */
2176 powi_lookup_cost (unsigned HOST_WIDE_INT n
, bool *cache
)
2178 /* If we've already calculated this exponent, then this evaluation
2179 doesn't require any additional multiplications. */
2184 return powi_lookup_cost (n
- powi_table
[n
], cache
)
2185 + powi_lookup_cost (powi_table
[n
], cache
) + 1;
2188 /* Return the number of multiplications required to calculate
2189 powi(x,n) for an arbitrary x, given the exponent N. This
2190 function needs to be kept in sync with expand_powi below. */
2193 powi_cost (HOST_WIDE_INT n
)
2195 bool cache
[POWI_TABLE_SIZE
];
2196 unsigned HOST_WIDE_INT digit
;
2197 unsigned HOST_WIDE_INT val
;
2203 /* Ignore the reciprocal when calculating the cost. */
2204 val
= (n
< 0) ? -n
: n
;
2206 /* Initialize the exponent cache. */
2207 memset (cache
, 0, POWI_TABLE_SIZE
* sizeof (bool));
2212 while (val
>= POWI_TABLE_SIZE
)
2216 digit
= val
& ((1 << POWI_WINDOW_SIZE
) - 1);
2217 result
+= powi_lookup_cost (digit
, cache
)
2218 + POWI_WINDOW_SIZE
+ 1;
2219 val
>>= POWI_WINDOW_SIZE
;
2228 return result
+ powi_lookup_cost (val
, cache
);
2231 /* Recursive subroutine of expand_powi. This function takes the array,
2232 CACHE, of already calculated exponents and an exponent N and returns
2233 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2236 expand_powi_1 (enum machine_mode mode
, unsigned HOST_WIDE_INT n
, rtx
*cache
)
2238 unsigned HOST_WIDE_INT digit
;
2242 if (n
< POWI_TABLE_SIZE
)
2247 target
= gen_reg_rtx (mode
);
2250 op0
= expand_powi_1 (mode
, n
- powi_table
[n
], cache
);
2251 op1
= expand_powi_1 (mode
, powi_table
[n
], cache
);
2255 target
= gen_reg_rtx (mode
);
2256 digit
= n
& ((1 << POWI_WINDOW_SIZE
) - 1);
2257 op0
= expand_powi_1 (mode
, n
- digit
, cache
);
2258 op1
= expand_powi_1 (mode
, digit
, cache
);
2262 target
= gen_reg_rtx (mode
);
2263 op0
= expand_powi_1 (mode
, n
>> 1, cache
);
2267 result
= expand_mult (mode
, op0
, op1
, target
, 0);
2268 if (result
!= target
)
2269 emit_move_insn (target
, result
);
2273 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2274 floating point operand in mode MODE, and N is the exponent. This
2275 function needs to be kept in sync with powi_cost above. */
2278 expand_powi (rtx x
, enum machine_mode mode
, HOST_WIDE_INT n
)
2280 unsigned HOST_WIDE_INT val
;
2281 rtx cache
[POWI_TABLE_SIZE
];
2285 return CONST1_RTX (mode
);
2287 val
= (n
< 0) ? -n
: n
;
2289 memset (cache
, 0, sizeof (cache
));
2292 result
= expand_powi_1 (mode
, (n
< 0) ? -n
: n
, cache
);
2294 /* If the original exponent was negative, reciprocate the result. */
2296 result
= expand_binop (mode
, sdiv_optab
, CONST1_RTX (mode
),
2297 result
, NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
2302 /* Expand a call to the pow built-in mathematical function. Return 0 if
2303 a normal call should be emitted rather than expanding the function
2304 in-line. EXP is the expression that is a call to the builtin
2305 function; if convenient, the result should be placed in TARGET. */
2308 expand_builtin_pow (tree exp
, rtx target
, rtx subtarget
)
2310 tree arglist
= TREE_OPERAND (exp
, 1);
2313 if (! validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
2316 arg0
= TREE_VALUE (arglist
);
2317 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
2319 if (TREE_CODE (arg1
) == REAL_CST
2320 && ! TREE_CONSTANT_OVERFLOW (arg1
))
2322 REAL_VALUE_TYPE cint
;
2326 c
= TREE_REAL_CST (arg1
);
2327 n
= real_to_integer (&c
);
2328 real_from_integer (&cint
, VOIDmode
, n
, n
< 0 ? -1 : 0, 0);
2329 if (real_identical (&c
, &cint
))
2331 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2332 Otherwise, check the number of multiplications required.
2333 Note that pow never sets errno for an integer exponent. */
2334 if ((n
>= -1 && n
<= 2)
2335 || (flag_unsafe_math_optimizations
2337 && powi_cost (n
) <= POWI_MAX_MULTS
))
2339 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
2340 rtx op
= expand_expr (arg0
, subtarget
, VOIDmode
, 0);
2341 op
= force_reg (mode
, op
);
2342 return expand_powi (op
, mode
, n
);
2347 if (! flag_unsafe_math_optimizations
)
2349 return expand_builtin_mathfn_2 (exp
, target
, subtarget
);
2352 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2353 if we failed the caller should emit a normal call, otherwise
2354 try to get the result in TARGET, if convenient. */
2357 expand_builtin_strlen (tree arglist
, rtx target
,
2358 enum machine_mode target_mode
)
2360 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
2365 tree len
, src
= TREE_VALUE (arglist
);
2366 rtx result
, src_reg
, char_rtx
, before_strlen
;
2367 enum machine_mode insn_mode
= target_mode
, char_mode
;
2368 enum insn_code icode
= CODE_FOR_nothing
;
2371 /* If the length can be computed at compile-time, return it. */
2372 len
= c_strlen (src
, 0);
2374 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2376 /* If the length can be computed at compile-time and is constant
2377 integer, but there are side-effects in src, evaluate
2378 src for side-effects, then return len.
2379 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2380 can be optimized into: i++; x = 3; */
2381 len
= c_strlen (src
, 1);
2382 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
2384 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
2385 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2388 align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
2390 /* If SRC is not a pointer type, don't do this operation inline. */
2394 /* Bail out if we can't compute strlen in the right mode. */
2395 while (insn_mode
!= VOIDmode
)
2397 icode
= strlen_optab
->handlers
[(int) insn_mode
].insn_code
;
2398 if (icode
!= CODE_FOR_nothing
)
2401 insn_mode
= GET_MODE_WIDER_MODE (insn_mode
);
2403 if (insn_mode
== VOIDmode
)
2406 /* Make a place to write the result of the instruction. */
2410 && GET_MODE (result
) == insn_mode
2411 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
2412 result
= gen_reg_rtx (insn_mode
);
2414 /* Make a place to hold the source address. We will not expand
2415 the actual source until we are sure that the expansion will
2416 not fail -- there are trees that cannot be expanded twice. */
2417 src_reg
= gen_reg_rtx (Pmode
);
2419 /* Mark the beginning of the strlen sequence so we can emit the
2420 source operand later. */
2421 before_strlen
= get_last_insn ();
2423 char_rtx
= const0_rtx
;
2424 char_mode
= insn_data
[(int) icode
].operand
[2].mode
;
2425 if (! (*insn_data
[(int) icode
].operand
[2].predicate
) (char_rtx
,
2427 char_rtx
= copy_to_mode_reg (char_mode
, char_rtx
);
2429 pat
= GEN_FCN (icode
) (result
, gen_rtx_MEM (BLKmode
, src_reg
),
2430 char_rtx
, GEN_INT (align
));
2435 /* Now that we are assured of success, expand the source. */
2437 pat
= expand_expr (src
, src_reg
, ptr_mode
, EXPAND_NORMAL
);
2439 emit_move_insn (src_reg
, pat
);
2444 emit_insn_after (pat
, before_strlen
);
2446 emit_insn_before (pat
, get_insns ());
2448 /* Return the value in the proper mode for this function. */
2449 if (GET_MODE (result
) == target_mode
)
2451 else if (target
!= 0)
2452 convert_move (target
, result
, 0);
2454 target
= convert_to_mode (target_mode
, result
, 0);
2460 /* Expand a call to the strstr builtin. Return 0 if we failed the
2461 caller should emit a normal call, otherwise try to get the result
2462 in TARGET, if convenient (and in mode MODE if that's convenient). */
2465 expand_builtin_strstr (tree arglist
, rtx target
, enum machine_mode mode
)
2467 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2469 tree result
= fold_builtin_strstr (arglist
);
2471 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2476 /* Expand a call to the strchr builtin. Return 0 if we failed the
2477 caller should emit a normal call, otherwise try to get the result
2478 in TARGET, if convenient (and in mode MODE if that's convenient). */
2481 expand_builtin_strchr (tree arglist
, rtx target
, enum machine_mode mode
)
2483 if (validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2485 tree result
= fold_builtin_strchr (arglist
);
2487 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2489 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2494 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2495 caller should emit a normal call, otherwise try to get the result
2496 in TARGET, if convenient (and in mode MODE if that's convenient). */
2499 expand_builtin_strrchr (tree arglist
, rtx target
, enum machine_mode mode
)
2501 if (validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2503 tree result
= fold_builtin_strrchr (arglist
);
2505 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2510 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2511 caller should emit a normal call, otherwise try to get the result
2512 in TARGET, if convenient (and in mode MODE if that's convenient). */
2515 expand_builtin_strpbrk (tree arglist
, rtx target
, enum machine_mode mode
)
2517 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2519 tree result
= fold_builtin_strpbrk (arglist
);
2521 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2526 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2527 bytes from constant string DATA + OFFSET and return it as target
2531 builtin_memcpy_read_str (void *data
, HOST_WIDE_INT offset
,
2532 enum machine_mode mode
)
2534 const char *str
= (const char *) data
;
2536 gcc_assert (offset
>= 0
2537 && ((unsigned HOST_WIDE_INT
) offset
+ GET_MODE_SIZE (mode
)
2538 <= strlen (str
) + 1));
2540 return c_readstr (str
+ offset
, mode
);
2543 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2544 Return 0 if we failed, the caller should emit a normal call,
2545 otherwise try to get the result in TARGET, if convenient (and in
2546 mode MODE if that's convenient). */
2548 expand_builtin_memcpy (tree exp
, rtx target
, enum machine_mode mode
)
2550 tree arglist
= TREE_OPERAND (exp
, 1);
2551 if (!validate_arglist (arglist
,
2552 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2556 tree dest
= TREE_VALUE (arglist
);
2557 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
2558 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
2559 const char *src_str
;
2560 unsigned int src_align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
2561 unsigned int dest_align
2562 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
2563 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
2564 tree result
= fold_builtin_memcpy (exp
);
2567 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2569 /* If DEST is not a pointer type, call the normal function. */
2570 if (dest_align
== 0)
2573 /* If either SRC is not a pointer type, don't do this
2574 operation in-line. */
2578 dest_mem
= get_memory_rtx (dest
);
2579 set_mem_align (dest_mem
, dest_align
);
2580 len_rtx
= expand_expr (len
, NULL_RTX
, VOIDmode
, 0);
2581 src_str
= c_getstr (src
);
2583 /* If SRC is a string constant and block move would be done
2584 by pieces, we can avoid loading the string from memory
2585 and only stored the computed constants. */
2587 && GET_CODE (len_rtx
) == CONST_INT
2588 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
2589 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
2590 (void *) src_str
, dest_align
))
2592 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
2593 builtin_memcpy_read_str
,
2594 (void *) src_str
, dest_align
, 0);
2595 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
2596 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
2600 src_mem
= get_memory_rtx (src
);
2601 set_mem_align (src_mem
, src_align
);
2603 /* Copy word part most expediently. */
2604 dest_addr
= emit_block_move (dest_mem
, src_mem
, len_rtx
,
2609 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
2610 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
2616 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2617 Return 0 if we failed the caller should emit a normal call,
2618 otherwise try to get the result in TARGET, if convenient (and in
2619 mode MODE if that's convenient). If ENDP is 0 return the
2620 destination pointer, if ENDP is 1 return the end pointer ala
2621 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2625 expand_builtin_mempcpy (tree arglist
, tree type
, rtx target
, enum machine_mode mode
,
2628 if (!validate_arglist (arglist
,
2629 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2631 /* If return value is ignored, transform mempcpy into memcpy. */
2632 else if (target
== const0_rtx
)
2634 tree fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
2639 return expand_expr (build_function_call_expr (fn
, arglist
),
2640 target
, mode
, EXPAND_NORMAL
);
2644 tree dest
= TREE_VALUE (arglist
);
2645 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
2646 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
2647 const char *src_str
;
2648 unsigned int src_align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
2649 unsigned int dest_align
2650 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
2651 rtx dest_mem
, src_mem
, len_rtx
;
2652 tree result
= fold_builtin_mempcpy (arglist
, type
, endp
);
2655 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2657 /* If either SRC or DEST is not a pointer type, don't do this
2658 operation in-line. */
2659 if (dest_align
== 0 || src_align
== 0)
2662 /* If LEN is not constant, call the normal function. */
2663 if (! host_integerp (len
, 1))
2666 len_rtx
= expand_expr (len
, NULL_RTX
, VOIDmode
, 0);
2667 src_str
= c_getstr (src
);
2669 /* If SRC is a string constant and block move would be done
2670 by pieces, we can avoid loading the string from memory
2671 and only stored the computed constants. */
2673 && GET_CODE (len_rtx
) == CONST_INT
2674 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
2675 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
2676 (void *) src_str
, dest_align
))
2678 dest_mem
= get_memory_rtx (dest
);
2679 set_mem_align (dest_mem
, dest_align
);
2680 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
2681 builtin_memcpy_read_str
,
2682 (void *) src_str
, dest_align
, endp
);
2683 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
2684 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
2688 if (GET_CODE (len_rtx
) == CONST_INT
2689 && can_move_by_pieces (INTVAL (len_rtx
),
2690 MIN (dest_align
, src_align
)))
2692 dest_mem
= get_memory_rtx (dest
);
2693 set_mem_align (dest_mem
, dest_align
);
2694 src_mem
= get_memory_rtx (src
);
2695 set_mem_align (src_mem
, src_align
);
2696 dest_mem
= move_by_pieces (dest_mem
, src_mem
, INTVAL (len_rtx
),
2697 MIN (dest_align
, src_align
), endp
);
2698 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
2699 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
2707 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2708 if we failed the caller should emit a normal call. */
2711 expand_builtin_memmove (tree arglist
, tree type
, rtx target
,
2712 enum machine_mode mode
)
2714 if (!validate_arglist (arglist
,
2715 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2719 tree dest
= TREE_VALUE (arglist
);
2720 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
2721 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
2723 unsigned int src_align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
2724 unsigned int dest_align
2725 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
2726 tree result
= fold_builtin_memmove (arglist
, type
);
2729 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2731 /* If DEST is not a pointer type, call the normal function. */
2732 if (dest_align
== 0)
2735 /* If either SRC is not a pointer type, don't do this
2736 operation in-line. */
2740 /* If src is categorized for a readonly section we can use
2742 if (readonly_data_expr (src
))
2744 tree
const fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
2747 return expand_expr (build_function_call_expr (fn
, arglist
),
2748 target
, mode
, EXPAND_NORMAL
);
2751 /* If length is 1 and we can expand memcpy call inline,
2752 it is ok to use memcpy as well. */
2753 if (integer_onep (len
))
2755 rtx ret
= expand_builtin_mempcpy (arglist
, type
, target
, mode
,
2761 /* Otherwise, call the normal function. */
2766 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2767 if we failed the caller should emit a normal call. */
2770 expand_builtin_bcopy (tree arglist
, tree type
)
2772 tree src
, dest
, size
, newarglist
;
2774 if (!validate_arglist (arglist
,
2775 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2778 src
= TREE_VALUE (arglist
);
2779 dest
= TREE_VALUE (TREE_CHAIN (arglist
));
2780 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
2782 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2783 memmove(ptr y, ptr x, size_t z). This is done this way
2784 so that if it isn't expanded inline, we fallback to
2785 calling bcopy instead of memmove. */
2787 newarglist
= build_tree_list (NULL_TREE
, fold_convert (sizetype
, size
));
2788 newarglist
= tree_cons (NULL_TREE
, src
, newarglist
);
2789 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
2791 return expand_builtin_memmove (newarglist
, type
, const0_rtx
, VOIDmode
);
2795 # define HAVE_movstr 0
2796 # define CODE_FOR_movstr CODE_FOR_nothing
2799 /* Expand into a movstr instruction, if one is available. Return 0 if
2800 we failed, the caller should emit a normal call, otherwise try to
2801 get the result in TARGET, if convenient. If ENDP is 0 return the
2802 destination pointer, if ENDP is 1 return the end pointer ala
2803 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2807 expand_movstr (tree dest
, tree src
, rtx target
, int endp
)
2813 const struct insn_data
* data
;
2818 dest_mem
= get_memory_rtx (dest
);
2819 src_mem
= get_memory_rtx (src
);
2822 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
2823 dest_mem
= replace_equiv_address (dest_mem
, target
);
2824 end
= gen_reg_rtx (Pmode
);
2828 if (target
== 0 || target
== const0_rtx
)
2830 end
= gen_reg_rtx (Pmode
);
2838 data
= insn_data
+ CODE_FOR_movstr
;
2840 if (data
->operand
[0].mode
!= VOIDmode
)
2841 end
= gen_lowpart (data
->operand
[0].mode
, end
);
2843 insn
= data
->genfun (end
, dest_mem
, src_mem
);
2849 /* movstr is supposed to set end to the address of the NUL
2850 terminator. If the caller requested a mempcpy-like return value,
2852 if (endp
== 1 && target
!= const0_rtx
)
2854 rtx tem
= plus_constant (gen_lowpart (GET_MODE (target
), end
), 1);
2855 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
2861 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2862 if we failed the caller should emit a normal call, otherwise try to get
2863 the result in TARGET, if convenient (and in mode MODE if that's
2867 expand_builtin_strcpy (tree exp
, rtx target
, enum machine_mode mode
)
2869 tree arglist
= TREE_OPERAND (exp
, 1);
2870 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2872 tree result
= fold_builtin_strcpy (exp
, 0);
2874 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2876 return expand_movstr (TREE_VALUE (arglist
),
2877 TREE_VALUE (TREE_CHAIN (arglist
)),
2878 target
, /*endp=*/0);
2883 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
2884 Return 0 if we failed the caller should emit a normal call,
2885 otherwise try to get the result in TARGET, if convenient (and in
2886 mode MODE if that's convenient). */
2889 expand_builtin_stpcpy (tree exp
, rtx target
, enum machine_mode mode
)
2891 tree arglist
= TREE_OPERAND (exp
, 1);
2892 /* If return value is ignored, transform stpcpy into strcpy. */
2893 if (target
== const0_rtx
)
2895 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
2899 return expand_expr (build_function_call_expr (fn
, arglist
),
2900 target
, mode
, EXPAND_NORMAL
);
2903 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2907 tree dst
, src
, len
, lenp1
;
2911 /* Ensure we get an actual string whose length can be evaluated at
2912 compile-time, not an expression containing a string. This is
2913 because the latter will potentially produce pessimized code
2914 when used to produce the return value. */
2915 src
= TREE_VALUE (TREE_CHAIN (arglist
));
2916 if (! c_getstr (src
) || ! (len
= c_strlen (src
, 0)))
2917 return expand_movstr (TREE_VALUE (arglist
),
2918 TREE_VALUE (TREE_CHAIN (arglist
)),
2919 target
, /*endp=*/2);
2921 dst
= TREE_VALUE (arglist
);
2922 lenp1
= size_binop (PLUS_EXPR
, len
, ssize_int (1));
2923 narglist
= build_tree_list (NULL_TREE
, lenp1
);
2924 narglist
= tree_cons (NULL_TREE
, src
, narglist
);
2925 narglist
= tree_cons (NULL_TREE
, dst
, narglist
);
2926 ret
= expand_builtin_mempcpy (narglist
, TREE_TYPE (exp
),
2927 target
, mode
, /*endp=*/2);
2932 if (TREE_CODE (len
) == INTEGER_CST
)
2934 rtx len_rtx
= expand_expr (len
, NULL_RTX
, VOIDmode
, 0);
2936 if (GET_CODE (len_rtx
) == CONST_INT
)
2938 ret
= expand_builtin_strcpy (exp
, target
, mode
);
2944 if (mode
!= VOIDmode
)
2945 target
= gen_reg_rtx (mode
);
2947 target
= gen_reg_rtx (GET_MODE (ret
));
2949 if (GET_MODE (target
) != GET_MODE (ret
))
2950 ret
= gen_lowpart (GET_MODE (target
), ret
);
2952 ret
= plus_constant (ret
, INTVAL (len_rtx
));
2953 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
2961 return expand_movstr (TREE_VALUE (arglist
),
2962 TREE_VALUE (TREE_CHAIN (arglist
)),
2963 target
, /*endp=*/2);
2967 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2968 bytes from constant string DATA + OFFSET and return it as target
2972 builtin_strncpy_read_str (void *data
, HOST_WIDE_INT offset
,
2973 enum machine_mode mode
)
2975 const char *str
= (const char *) data
;
2977 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
2980 return c_readstr (str
+ offset
, mode
);
2983 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
2984 if we failed the caller should emit a normal call. */
2987 expand_builtin_strncpy (tree exp
, rtx target
, enum machine_mode mode
)
2989 tree arglist
= TREE_OPERAND (exp
, 1);
2990 if (validate_arglist (arglist
,
2991 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2993 tree slen
= c_strlen (TREE_VALUE (TREE_CHAIN (arglist
)), 1);
2994 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
2995 tree result
= fold_builtin_strncpy (exp
, slen
);
2998 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3000 /* We must be passed a constant len and src parameter. */
3001 if (!host_integerp (len
, 1) || !slen
|| !host_integerp (slen
, 1))
3004 slen
= size_binop (PLUS_EXPR
, slen
, ssize_int (1));
3006 /* We're required to pad with trailing zeros if the requested
3007 len is greater than strlen(s2)+1. In that case try to
3008 use store_by_pieces, if it fails, punt. */
3009 if (tree_int_cst_lt (slen
, len
))
3011 tree dest
= TREE_VALUE (arglist
);
3012 unsigned int dest_align
3013 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
3014 const char *p
= c_getstr (TREE_VALUE (TREE_CHAIN (arglist
)));
3017 if (!p
|| dest_align
== 0 || !host_integerp (len
, 1)
3018 || !can_store_by_pieces (tree_low_cst (len
, 1),
3019 builtin_strncpy_read_str
,
3020 (void *) p
, dest_align
))
3023 dest_mem
= get_memory_rtx (dest
);
3024 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3025 builtin_strncpy_read_str
,
3026 (void *) p
, dest_align
, 0);
3027 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3028 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3035 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3036 bytes from constant string DATA + OFFSET and return it as target
3040 builtin_memset_read_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3041 enum machine_mode mode
)
3043 const char *c
= (const char *) data
;
3044 char *p
= alloca (GET_MODE_SIZE (mode
));
3046 memset (p
, *c
, GET_MODE_SIZE (mode
));
3048 return c_readstr (p
, mode
);
3051 /* Callback routine for store_by_pieces. Return the RTL of a register
3052 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3053 char value given in the RTL register data. For example, if mode is
3054 4 bytes wide, return the RTL for 0x01010101*data. */
3057 builtin_memset_gen_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3058 enum machine_mode mode
)
3064 size
= GET_MODE_SIZE (mode
);
3069 memset (p
, 1, size
);
3070 coeff
= c_readstr (p
, mode
);
3072 target
= convert_to_mode (mode
, (rtx
) data
, 1);
3073 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
3074 return force_reg (mode
, target
);
3077 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3078 if we failed the caller should emit a normal call, otherwise try to get
3079 the result in TARGET, if convenient (and in mode MODE if that's
3083 expand_builtin_memset (tree arglist
, rtx target
, enum machine_mode mode
)
3085 if (!validate_arglist (arglist
,
3086 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3090 tree dest
= TREE_VALUE (arglist
);
3091 tree val
= TREE_VALUE (TREE_CHAIN (arglist
));
3092 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3095 unsigned int dest_align
3096 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
3097 rtx dest_mem
, dest_addr
, len_rtx
;
3099 /* If DEST is not a pointer type, don't do this
3100 operation in-line. */
3101 if (dest_align
== 0)
3104 /* If the LEN parameter is zero, return DEST. */
3105 if (integer_zerop (len
))
3107 /* Evaluate and ignore VAL in case it has side-effects. */
3108 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3109 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
3112 if (TREE_CODE (val
) != INTEGER_CST
)
3116 if (!host_integerp (len
, 1))
3119 if (optimize_size
&& tree_low_cst (len
, 1) > 1)
3122 /* Assume that we can memset by pieces if we can store the
3123 * the coefficients by pieces (in the required modes).
3124 * We can't pass builtin_memset_gen_str as that emits RTL. */
3126 if (!can_store_by_pieces (tree_low_cst (len
, 1),
3127 builtin_memset_read_str
,
3131 val
= fold (build1 (CONVERT_EXPR
, unsigned_char_type_node
, val
));
3132 val_rtx
= expand_expr (val
, NULL_RTX
, VOIDmode
, 0);
3133 val_rtx
= force_reg (TYPE_MODE (unsigned_char_type_node
),
3135 dest_mem
= get_memory_rtx (dest
);
3136 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3137 builtin_memset_gen_str
,
3138 val_rtx
, dest_align
, 0);
3139 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3140 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3144 if (target_char_cast (val
, &c
))
3149 if (!host_integerp (len
, 1))
3151 if (!can_store_by_pieces (tree_low_cst (len
, 1),
3152 builtin_memset_read_str
, &c
,
3156 dest_mem
= get_memory_rtx (dest
);
3157 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3158 builtin_memset_read_str
,
3160 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3161 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3165 len_rtx
= expand_expr (len
, NULL_RTX
, VOIDmode
, 0);
3167 dest_mem
= get_memory_rtx (dest
);
3168 set_mem_align (dest_mem
, dest_align
);
3169 dest_addr
= clear_storage (dest_mem
, len_rtx
);
3173 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3174 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3181 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3182 if we failed the caller should emit a normal call. */
3185 expand_builtin_bzero (tree arglist
)
3187 tree dest
, size
, newarglist
;
3189 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3192 dest
= TREE_VALUE (arglist
);
3193 size
= TREE_VALUE (TREE_CHAIN (arglist
));
3195 /* New argument list transforming bzero(ptr x, int y) to
3196 memset(ptr x, int 0, size_t y). This is done this way
3197 so that if it isn't expanded inline, we fallback to
3198 calling bzero instead of memset. */
3200 newarglist
= build_tree_list (NULL_TREE
, fold_convert (sizetype
, size
));
3201 newarglist
= tree_cons (NULL_TREE
, integer_zero_node
, newarglist
);
3202 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
3204 return expand_builtin_memset (newarglist
, const0_rtx
, VOIDmode
);
3207 /* Expand expression EXP, which is a call to the memcmp built-in function.
3208 ARGLIST is the argument list for this call. Return 0 if we failed and the
3209 caller should emit a normal call, otherwise try to get the result in
3210 TARGET, if convenient (and in mode MODE, if that's convenient). */
3213 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED
, tree arglist
, rtx target
,
3214 enum machine_mode mode
)
3216 if (!validate_arglist (arglist
,
3217 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3221 tree result
= fold_builtin_memcmp (arglist
);
3223 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3226 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3228 tree arg1
= TREE_VALUE (arglist
);
3229 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
3230 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3231 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3236 = get_pointer_alignment (arg1
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3238 = get_pointer_alignment (arg2
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3239 enum machine_mode insn_mode
;
3241 #ifdef HAVE_cmpmemsi
3243 insn_mode
= insn_data
[(int) CODE_FOR_cmpmemsi
].operand
[0].mode
;
3246 #ifdef HAVE_cmpstrsi
3248 insn_mode
= insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
3253 /* If we don't have POINTER_TYPE, call the function. */
3254 if (arg1_align
== 0 || arg2_align
== 0)
3257 /* Make a place to write the result of the instruction. */
3260 && REG_P (result
) && GET_MODE (result
) == insn_mode
3261 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3262 result
= gen_reg_rtx (insn_mode
);
3264 arg1_rtx
= get_memory_rtx (arg1
);
3265 arg2_rtx
= get_memory_rtx (arg2
);
3266 arg3_rtx
= expand_expr (len
, NULL_RTX
, VOIDmode
, 0);
3268 /* Set MEM_SIZE as appropriate. */
3269 if (GET_CODE (arg3_rtx
) == CONST_INT
)
3271 set_mem_size (arg1_rtx
, arg3_rtx
);
3272 set_mem_size (arg2_rtx
, arg3_rtx
);
3275 #ifdef HAVE_cmpmemsi
3277 insn
= gen_cmpmemsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3278 GEN_INT (MIN (arg1_align
, arg2_align
)));
3281 #ifdef HAVE_cmpstrsi
3283 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3284 GEN_INT (MIN (arg1_align
, arg2_align
)));
3292 emit_library_call_value (memcmp_libfunc
, result
, LCT_PURE_MAKE_BLOCK
,
3293 TYPE_MODE (integer_type_node
), 3,
3294 XEXP (arg1_rtx
, 0), Pmode
,
3295 XEXP (arg2_rtx
, 0), Pmode
,
3296 convert_to_mode (TYPE_MODE (sizetype
), arg3_rtx
,
3297 TYPE_UNSIGNED (sizetype
)),
3298 TYPE_MODE (sizetype
));
3300 /* Return the value in the proper mode for this function. */
3301 mode
= TYPE_MODE (TREE_TYPE (exp
));
3302 if (GET_MODE (result
) == mode
)
3304 else if (target
!= 0)
3306 convert_move (target
, result
, 0);
3310 return convert_to_mode (mode
, result
, 0);
3317 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3318 if we failed the caller should emit a normal call, otherwise try to get
3319 the result in TARGET, if convenient. */
3322 expand_builtin_strcmp (tree exp
, rtx target
, enum machine_mode mode
)
3324 tree arglist
= TREE_OPERAND (exp
, 1);
3326 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3330 tree result
= fold_builtin_strcmp (arglist
);
3332 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3335 #ifdef HAVE_cmpstrsi
3338 tree arg1
= TREE_VALUE (arglist
);
3339 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
3340 tree len
, len1
, len2
;
3341 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3346 = get_pointer_alignment (arg1
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3348 = get_pointer_alignment (arg2
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3349 enum machine_mode insn_mode
3350 = insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
3352 len1
= c_strlen (arg1
, 1);
3353 len2
= c_strlen (arg2
, 1);
3356 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
3358 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
3360 /* If we don't have a constant length for the first, use the length
3361 of the second, if we know it. We don't require a constant for
3362 this case; some cost analysis could be done if both are available
3363 but neither is constant. For now, assume they're equally cheap,
3364 unless one has side effects. If both strings have constant lengths,
3371 else if (TREE_SIDE_EFFECTS (len1
))
3373 else if (TREE_SIDE_EFFECTS (len2
))
3375 else if (TREE_CODE (len1
) != INTEGER_CST
)
3377 else if (TREE_CODE (len2
) != INTEGER_CST
)
3379 else if (tree_int_cst_lt (len1
, len2
))
3384 /* If both arguments have side effects, we cannot optimize. */
3385 if (!len
|| TREE_SIDE_EFFECTS (len
))
3388 /* If we don't have POINTER_TYPE, call the function. */
3389 if (arg1_align
== 0 || arg2_align
== 0)
3392 /* Make a place to write the result of the instruction. */
3395 && REG_P (result
) && GET_MODE (result
) == insn_mode
3396 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3397 result
= gen_reg_rtx (insn_mode
);
3399 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3400 arg1
= builtin_save_expr (arg1
);
3401 arg2
= builtin_save_expr (arg2
);
3403 arg1_rtx
= get_memory_rtx (arg1
);
3404 arg2_rtx
= get_memory_rtx (arg2
);
3405 arg3_rtx
= expand_expr (len
, NULL_RTX
, VOIDmode
, 0);
3406 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3407 GEN_INT (MIN (arg1_align
, arg2_align
)));
3412 /* Return the value in the proper mode for this function. */
3413 mode
= TYPE_MODE (TREE_TYPE (exp
));
3414 if (GET_MODE (result
) == mode
)
3417 return convert_to_mode (mode
, result
, 0);
3418 convert_move (target
, result
, 0);
3422 /* Expand the library call ourselves using a stabilized argument
3423 list to avoid re-evaluating the function's arguments twice. */
3424 arglist
= build_tree_list (NULL_TREE
, arg2
);
3425 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
3426 fndecl
= get_callee_fndecl (exp
);
3427 exp
= build_function_call_expr (fndecl
, arglist
);
3428 return expand_call (exp
, target
, target
== const0_rtx
);
3434 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3435 if we failed the caller should emit a normal call, otherwise try to get
3436 the result in TARGET, if convenient. */
3439 expand_builtin_strncmp (tree exp
, rtx target
, enum machine_mode mode
)
3441 tree arglist
= TREE_OPERAND (exp
, 1);
3443 if (!validate_arglist (arglist
,
3444 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3448 tree result
= fold_builtin_strncmp (arglist
);
3450 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3453 /* If c_strlen can determine an expression for one of the string
3454 lengths, and it doesn't have side effects, then emit cmpstrsi
3455 using length MIN(strlen(string)+1, arg3). */
3456 #ifdef HAVE_cmpstrsi
3459 tree arg1
= TREE_VALUE (arglist
);
3460 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
3461 tree arg3
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3462 tree len
, len1
, len2
;
3463 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3468 = get_pointer_alignment (arg1
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3470 = get_pointer_alignment (arg2
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3471 enum machine_mode insn_mode
3472 = insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
3474 len1
= c_strlen (arg1
, 1);
3475 len2
= c_strlen (arg2
, 1);
3478 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
3480 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
3482 /* If we don't have a constant length for the first, use the length
3483 of the second, if we know it. We don't require a constant for
3484 this case; some cost analysis could be done if both are available
3485 but neither is constant. For now, assume they're equally cheap,
3486 unless one has side effects. If both strings have constant lengths,
3493 else if (TREE_SIDE_EFFECTS (len1
))
3495 else if (TREE_SIDE_EFFECTS (len2
))
3497 else if (TREE_CODE (len1
) != INTEGER_CST
)
3499 else if (TREE_CODE (len2
) != INTEGER_CST
)
3501 else if (tree_int_cst_lt (len1
, len2
))
3506 /* If both arguments have side effects, we cannot optimize. */
3507 if (!len
|| TREE_SIDE_EFFECTS (len
))
3510 /* The actual new length parameter is MIN(len,arg3). */
3511 len
= fold (build2 (MIN_EXPR
, TREE_TYPE (len
), len
,
3512 fold_convert (TREE_TYPE (len
), arg3
)));
3514 /* If we don't have POINTER_TYPE, call the function. */
3515 if (arg1_align
== 0 || arg2_align
== 0)
3518 /* Make a place to write the result of the instruction. */
3521 && REG_P (result
) && GET_MODE (result
) == insn_mode
3522 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3523 result
= gen_reg_rtx (insn_mode
);
3525 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3526 arg1
= builtin_save_expr (arg1
);
3527 arg2
= builtin_save_expr (arg2
);
3528 len
= builtin_save_expr (len
);
3530 arg1_rtx
= get_memory_rtx (arg1
);
3531 arg2_rtx
= get_memory_rtx (arg2
);
3532 arg3_rtx
= expand_expr (len
, NULL_RTX
, VOIDmode
, 0);
3533 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3534 GEN_INT (MIN (arg1_align
, arg2_align
)));
3539 /* Return the value in the proper mode for this function. */
3540 mode
= TYPE_MODE (TREE_TYPE (exp
));
3541 if (GET_MODE (result
) == mode
)
3544 return convert_to_mode (mode
, result
, 0);
3545 convert_move (target
, result
, 0);
3549 /* Expand the library call ourselves using a stabilized argument
3550 list to avoid re-evaluating the function's arguments twice. */
3551 arglist
= build_tree_list (NULL_TREE
, len
);
3552 arglist
= tree_cons (NULL_TREE
, arg2
, arglist
);
3553 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
3554 fndecl
= get_callee_fndecl (exp
);
3555 exp
= build_function_call_expr (fndecl
, arglist
);
3556 return expand_call (exp
, target
, target
== const0_rtx
);
3562 /* Expand expression EXP, which is a call to the strcat builtin.
3563 Return 0 if we failed the caller should emit a normal call,
3564 otherwise try to get the result in TARGET, if convenient. */
3567 expand_builtin_strcat (tree arglist
, tree type
, rtx target
, enum machine_mode mode
)
3569 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3573 tree dst
= TREE_VALUE (arglist
),
3574 src
= TREE_VALUE (TREE_CHAIN (arglist
));
3575 const char *p
= c_getstr (src
);
3579 /* If the string length is zero, return the dst parameter. */
3581 return expand_expr (dst
, target
, mode
, EXPAND_NORMAL
);
3582 else if (!optimize_size
)
3584 /* Otherwise if !optimize_size, see if we can store by
3585 pieces into (dst + strlen(dst)). */
3586 tree newdst
, arglist
,
3587 strlen_fn
= implicit_built_in_decls
[BUILT_IN_STRLEN
];
3589 /* This is the length argument. */
3590 arglist
= build_tree_list (NULL_TREE
,
3591 fold (size_binop (PLUS_EXPR
,
3594 /* Prepend src argument. */
3595 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
3597 /* We're going to use dst more than once. */
3598 dst
= builtin_save_expr (dst
);
3600 /* Create strlen (dst). */
3602 fold (build_function_call_expr (strlen_fn
,
3603 build_tree_list (NULL_TREE
,
3605 /* Create (dst + strlen (dst)). */
3606 newdst
= fold (build2 (PLUS_EXPR
, TREE_TYPE (dst
), dst
, newdst
));
3608 /* Prepend the new dst argument. */
3609 arglist
= tree_cons (NULL_TREE
, newdst
, arglist
);
3611 /* We don't want to get turned into a memcpy if the
3612 target is const0_rtx, i.e. when the return value
3613 isn't used. That would produce pessimized code so
3614 pass in a target of zero, it should never actually be
3615 used. If this was successful return the original
3616 dst, not the result of mempcpy. */
3617 if (expand_builtin_mempcpy (arglist
, type
, /*target=*/0, mode
, /*endp=*/0))
3618 return expand_expr (dst
, target
, mode
, EXPAND_NORMAL
);
3628 /* Expand expression EXP, which is a call to the strncat builtin.
3629 Return 0 if we failed the caller should emit a normal call,
3630 otherwise try to get the result in TARGET, if convenient. */
3633 expand_builtin_strncat (tree arglist
, rtx target
, enum machine_mode mode
)
3635 if (validate_arglist (arglist
,
3636 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3638 tree result
= fold_builtin_strncat (arglist
);
3640 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3645 /* Expand expression EXP, which is a call to the strspn builtin.
3646 Return 0 if we failed the caller should emit a normal call,
3647 otherwise try to get the result in TARGET, if convenient. */
3650 expand_builtin_strspn (tree arglist
, rtx target
, enum machine_mode mode
)
3652 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3654 tree result
= fold_builtin_strspn (arglist
);
3656 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3661 /* Expand expression EXP, which is a call to the strcspn builtin.
3662 Return 0 if we failed the caller should emit a normal call,
3663 otherwise try to get the result in TARGET, if convenient. */
3666 expand_builtin_strcspn (tree arglist
, rtx target
, enum machine_mode mode
)
3668 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3670 tree result
= fold_builtin_strcspn (arglist
);
3672 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3677 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3678 if that's convenient. */
3681 expand_builtin_saveregs (void)
3685 /* Don't do __builtin_saveregs more than once in a function.
3686 Save the result of the first call and reuse it. */
3687 if (saveregs_value
!= 0)
3688 return saveregs_value
;
3690 /* When this function is called, it means that registers must be
3691 saved on entry to this function. So we migrate the call to the
3692 first insn of this function. */
3696 /* Do whatever the machine needs done in this case. */
3697 val
= targetm
.calls
.expand_builtin_saveregs ();
3702 saveregs_value
= val
;
3704 /* Put the insns after the NOTE that starts the function. If this
3705 is inside a start_sequence, make the outer-level insn chain current, so
3706 the code is placed at the start of the function. */
3707 push_topmost_sequence ();
3708 emit_insn_after (seq
, entry_of_function ());
3709 pop_topmost_sequence ();
3714 /* __builtin_args_info (N) returns word N of the arg space info
3715 for the current function. The number and meanings of words
3716 is controlled by the definition of CUMULATIVE_ARGS. */
3719 expand_builtin_args_info (tree arglist
)
3721 int nwords
= sizeof (CUMULATIVE_ARGS
) / sizeof (int);
3722 int *word_ptr
= (int *) ¤t_function_args_info
;
3724 gcc_assert (sizeof (CUMULATIVE_ARGS
) % sizeof (int) == 0);
3728 if (!host_integerp (TREE_VALUE (arglist
), 0))
3729 error ("argument of %<__builtin_args_info%> must be constant");
3732 HOST_WIDE_INT wordnum
= tree_low_cst (TREE_VALUE (arglist
), 0);
3734 if (wordnum
< 0 || wordnum
>= nwords
)
3735 error ("argument of %<__builtin_args_info%> out of range");
3737 return GEN_INT (word_ptr
[wordnum
]);
3741 error ("missing argument in %<__builtin_args_info%>");
3746 /* Expand a call to __builtin_next_arg. */
3749 expand_builtin_next_arg (void)
3751 /* Checking arguments is already done in fold_builtin_next_arg
3752 that must be called before this function. */
3753 return expand_binop (Pmode
, add_optab
,
3754 current_function_internal_arg_pointer
,
3755 current_function_arg_offset_rtx
,
3756 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
3759 /* Make it easier for the backends by protecting the valist argument
3760 from multiple evaluations. */
3763 stabilize_va_list (tree valist
, int needs_lvalue
)
3765 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
3767 if (TREE_SIDE_EFFECTS (valist
))
3768 valist
= save_expr (valist
);
3770 /* For this case, the backends will be expecting a pointer to
3771 TREE_TYPE (va_list_type_node), but it's possible we've
3772 actually been given an array (an actual va_list_type_node).
3774 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
3776 tree p1
= build_pointer_type (TREE_TYPE (va_list_type_node
));
3777 valist
= build_fold_addr_expr_with_type (valist
, p1
);
3786 if (! TREE_SIDE_EFFECTS (valist
))
3789 pt
= build_pointer_type (va_list_type_node
);
3790 valist
= fold (build1 (ADDR_EXPR
, pt
, valist
));
3791 TREE_SIDE_EFFECTS (valist
) = 1;
3794 if (TREE_SIDE_EFFECTS (valist
))
3795 valist
= save_expr (valist
);
3796 valist
= build_fold_indirect_ref (valist
);
3802 /* The "standard" definition of va_list is void*. */
3805 std_build_builtin_va_list (void)
3807 return ptr_type_node
;
3810 /* The "standard" implementation of va_start: just assign `nextarg' to
3814 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
3818 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist
,
3819 make_tree (ptr_type_node
, nextarg
));
3820 TREE_SIDE_EFFECTS (t
) = 1;
3822 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3825 /* Expand ARGLIST, from a call to __builtin_va_start. */
3828 expand_builtin_va_start (tree arglist
)
3833 chain
= TREE_CHAIN (arglist
);
3837 error ("too few arguments to function %<va_start%>");
3841 if (fold_builtin_next_arg (chain
))
3844 nextarg
= expand_builtin_next_arg ();
3845 valist
= stabilize_va_list (TREE_VALUE (arglist
), 1);
3847 #ifdef EXPAND_BUILTIN_VA_START
3848 EXPAND_BUILTIN_VA_START (valist
, nextarg
);
3850 std_expand_builtin_va_start (valist
, nextarg
);
3856 /* The "standard" implementation of va_arg: read the value from the
3857 current (padded) address and increment by the (padded) size. */
3860 std_gimplify_va_arg_expr (tree valist
, tree type
, tree
*pre_p
, tree
*post_p
)
3862 tree addr
, t
, type_size
, rounded_size
, valist_tmp
;
3863 unsigned HOST_WIDE_INT align
, boundary
;
3866 #ifdef ARGS_GROW_DOWNWARD
3867 /* All of the alignment and movement below is for args-grow-up machines.
3868 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
3869 implement their own specialized gimplify_va_arg_expr routines. */
3873 indirect
= pass_by_reference (NULL
, TYPE_MODE (type
), type
, false);
3875 type
= build_pointer_type (type
);
3877 align
= PARM_BOUNDARY
/ BITS_PER_UNIT
;
3878 boundary
= FUNCTION_ARG_BOUNDARY (TYPE_MODE (type
), type
) / BITS_PER_UNIT
;
3880 /* Hoist the valist value into a temporary for the moment. */
3881 valist_tmp
= get_initialized_tmp_var (valist
, pre_p
, NULL
);
3883 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
3884 requires greater alignment, we must perform dynamic alignment. */
3885 if (boundary
> align
)
3887 t
= fold_convert (TREE_TYPE (valist
), size_int (boundary
- 1));
3888 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist_tmp
,
3889 build2 (PLUS_EXPR
, TREE_TYPE (valist
), valist_tmp
, t
));
3890 gimplify_and_add (t
, pre_p
);
3892 t
= fold_convert (TREE_TYPE (valist
), size_int (-boundary
));
3893 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist_tmp
,
3894 build2 (BIT_AND_EXPR
, TREE_TYPE (valist
), valist_tmp
, t
));
3895 gimplify_and_add (t
, pre_p
);
3900 /* If the actual alignment is less than the alignment of the type,
3901 adjust the type accordingly so that we don't assume strict alignment
3902 when deferencing the pointer. */
3903 boundary
*= BITS_PER_UNIT
;
3904 if (boundary
< TYPE_ALIGN (type
))
3906 type
= build_variant_type_copy (type
);
3907 TYPE_ALIGN (type
) = boundary
;
3910 /* Compute the rounded size of the type. */
3911 type_size
= size_in_bytes (type
);
3912 rounded_size
= round_up (type_size
, align
);
3914 /* Reduce rounded_size so it's sharable with the postqueue. */
3915 gimplify_expr (&rounded_size
, pre_p
, post_p
, is_gimple_val
, fb_rvalue
);
3919 if (PAD_VARARGS_DOWN
&& !integer_zerop (rounded_size
))
3921 /* Small args are padded downward. */
3922 t
= fold (build2 (GT_EXPR
, sizetype
, rounded_size
, size_int (align
)));
3923 t
= fold (build3 (COND_EXPR
, sizetype
, t
, size_zero_node
,
3924 size_binop (MINUS_EXPR
, rounded_size
, type_size
)));
3925 t
= fold_convert (TREE_TYPE (addr
), t
);
3926 addr
= fold (build2 (PLUS_EXPR
, TREE_TYPE (addr
), addr
, t
));
3929 /* Compute new value for AP. */
3930 t
= fold_convert (TREE_TYPE (valist
), rounded_size
);
3931 t
= build2 (PLUS_EXPR
, TREE_TYPE (valist
), valist_tmp
, t
);
3932 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist
, t
);
3933 gimplify_and_add (t
, pre_p
);
3935 addr
= fold_convert (build_pointer_type (type
), addr
);
3938 addr
= build_va_arg_indirect_ref (addr
);
3940 return build_va_arg_indirect_ref (addr
);
3943 /* Build an indirect-ref expression over the given TREE, which represents a
3944 piece of a va_arg() expansion. */
3946 build_va_arg_indirect_ref (tree addr
)
3948 addr
= build_fold_indirect_ref (addr
);
3950 if (flag_mudflap
) /* Don't instrument va_arg INDIRECT_REF. */
3956 /* Return a dummy expression of type TYPE in order to keep going after an
3960 dummy_object (tree type
)
3962 tree t
= convert (build_pointer_type (type
), null_pointer_node
);
3963 return build1 (INDIRECT_REF
, type
, t
);
3966 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
3967 builtin function, but a very special sort of operator. */
3969 enum gimplify_status
3970 gimplify_va_arg_expr (tree
*expr_p
, tree
*pre_p
, tree
*post_p
)
3972 tree promoted_type
, want_va_type
, have_va_type
;
3973 tree valist
= TREE_OPERAND (*expr_p
, 0);
3974 tree type
= TREE_TYPE (*expr_p
);
3977 /* Verify that valist is of the proper type. */
3978 want_va_type
= va_list_type_node
;
3979 have_va_type
= TREE_TYPE (valist
);
3981 if (have_va_type
== error_mark_node
)
3984 if (TREE_CODE (want_va_type
) == ARRAY_TYPE
)
3986 /* If va_list is an array type, the argument may have decayed
3987 to a pointer type, e.g. by being passed to another function.
3988 In that case, unwrap both types so that we can compare the
3989 underlying records. */
3990 if (TREE_CODE (have_va_type
) == ARRAY_TYPE
3991 || POINTER_TYPE_P (have_va_type
))
3993 want_va_type
= TREE_TYPE (want_va_type
);
3994 have_va_type
= TREE_TYPE (have_va_type
);
3998 if (TYPE_MAIN_VARIANT (want_va_type
) != TYPE_MAIN_VARIANT (have_va_type
))
4000 error ("first argument to %<va_arg%> not of type %<va_list%>");
4004 /* Generate a diagnostic for requesting data of a type that cannot
4005 be passed through `...' due to type promotion at the call site. */
4006 else if ((promoted_type
= lang_hooks
.types
.type_promotes_to (type
))
4009 static bool gave_help
;
4011 /* Unfortunately, this is merely undefined, rather than a constraint
4012 violation, so we cannot make this an error. If this call is never
4013 executed, the program is still strictly conforming. */
4014 warning ("%qT is promoted to %qT when passed through %<...%>",
4015 type
, promoted_type
);
4019 warning ("(so you should pass %qT not %qT to %<va_arg%>)",
4020 promoted_type
, type
);
4023 /* We can, however, treat "undefined" any way we please.
4024 Call abort to encourage the user to fix the program. */
4025 inform ("if this code is reached, the program will abort");
4026 t
= build_function_call_expr (implicit_built_in_decls
[BUILT_IN_TRAP
],
4028 append_to_statement_list (t
, pre_p
);
4030 /* This is dead code, but go ahead and finish so that the
4031 mode of the result comes out right. */
4032 *expr_p
= dummy_object (type
);
4037 /* Make it easier for the backends by protecting the valist argument
4038 from multiple evaluations. */
4039 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
4041 /* For this case, the backends will be expecting a pointer to
4042 TREE_TYPE (va_list_type_node), but it's possible we've
4043 actually been given an array (an actual va_list_type_node).
4045 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4047 tree p1
= build_pointer_type (TREE_TYPE (va_list_type_node
));
4048 valist
= build_fold_addr_expr_with_type (valist
, p1
);
4050 gimplify_expr (&valist
, pre_p
, post_p
, is_gimple_val
, fb_rvalue
);
4053 gimplify_expr (&valist
, pre_p
, post_p
, is_gimple_min_lval
, fb_lvalue
);
4055 if (!targetm
.gimplify_va_arg_expr
)
4056 /* Once most targets are converted this should abort. */
4059 *expr_p
= targetm
.gimplify_va_arg_expr (valist
, type
, pre_p
, post_p
);
4064 /* Expand ARGLIST, from a call to __builtin_va_end. */
4067 expand_builtin_va_end (tree arglist
)
4069 tree valist
= TREE_VALUE (arglist
);
4071 /* Evaluate for side effects, if needed. I hate macros that don't
4073 if (TREE_SIDE_EFFECTS (valist
))
4074 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4079 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4080 builtin rather than just as an assignment in stdarg.h because of the
4081 nastiness of array-type va_list types. */
4084 expand_builtin_va_copy (tree arglist
)
4088 dst
= TREE_VALUE (arglist
);
4089 src
= TREE_VALUE (TREE_CHAIN (arglist
));
4091 dst
= stabilize_va_list (dst
, 1);
4092 src
= stabilize_va_list (src
, 0);
4094 if (TREE_CODE (va_list_type_node
) != ARRAY_TYPE
)
4096 t
= build2 (MODIFY_EXPR
, va_list_type_node
, dst
, src
);
4097 TREE_SIDE_EFFECTS (t
) = 1;
4098 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4102 rtx dstb
, srcb
, size
;
4104 /* Evaluate to pointers. */
4105 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4106 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4107 size
= expand_expr (TYPE_SIZE_UNIT (va_list_type_node
), NULL_RTX
,
4108 VOIDmode
, EXPAND_NORMAL
);
4110 dstb
= convert_memory_address (Pmode
, dstb
);
4111 srcb
= convert_memory_address (Pmode
, srcb
);
4113 /* "Dereference" to BLKmode memories. */
4114 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
4115 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
4116 set_mem_align (dstb
, TYPE_ALIGN (va_list_type_node
));
4117 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
4118 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
4119 set_mem_align (srcb
, TYPE_ALIGN (va_list_type_node
));
4122 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
4128 /* Expand a call to one of the builtin functions __builtin_frame_address or
4129 __builtin_return_address. */
4132 expand_builtin_frame_address (tree fndecl
, tree arglist
)
4134 /* The argument must be a nonnegative integer constant.
4135 It counts the number of frames to scan up the stack.
4136 The value is the return address saved in that frame. */
4138 /* Warning about missing arg was already issued. */
4140 else if (! host_integerp (TREE_VALUE (arglist
), 1))
4142 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4143 error ("invalid argument to %<__builtin_frame_address%>");
4145 error ("invalid argument to %<__builtin_return_address%>");
4151 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
),
4152 tree_low_cst (TREE_VALUE (arglist
), 1),
4153 hard_frame_pointer_rtx
);
4155 /* Some ports cannot access arbitrary stack frames. */
4158 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4159 warning ("unsupported argument to %<__builtin_frame_address%>");
4161 warning ("unsupported argument to %<__builtin_return_address%>");
4165 /* For __builtin_frame_address, return what we've got. */
4166 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4170 && ! CONSTANT_P (tem
))
4171 tem
= copy_to_mode_reg (Pmode
, tem
);
4176 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4177 we failed and the caller should emit a normal call, otherwise try to get
4178 the result in TARGET, if convenient. */
4181 expand_builtin_alloca (tree arglist
, rtx target
)
4186 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4187 should always expand to function calls. These can be intercepted
4192 if (!validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
4195 /* Compute the argument. */
4196 op0
= expand_expr (TREE_VALUE (arglist
), NULL_RTX
, VOIDmode
, 0);
4198 /* Allocate the desired space. */
4199 result
= allocate_dynamic_stack_space (op0
, target
, BITS_PER_UNIT
);
4200 result
= convert_memory_address (ptr_mode
, result
);
4205 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4206 Return 0 if a normal call should be emitted rather than expanding the
4207 function in-line. If convenient, the result should be placed in TARGET.
4208 SUBTARGET may be used as the target for computing one of EXP's operands. */
4211 expand_builtin_unop (enum machine_mode target_mode
, tree arglist
, rtx target
,
4212 rtx subtarget
, optab op_optab
)
4215 if (!validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
4218 /* Compute the argument. */
4219 op0
= expand_expr (TREE_VALUE (arglist
), subtarget
, VOIDmode
, 0);
4220 /* Compute op, into TARGET if possible.
4221 Set TARGET to wherever the result comes back. */
4222 target
= expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist
))),
4223 op_optab
, op0
, target
, 1);
4224 gcc_assert (target
);
4226 return convert_to_mode (target_mode
, target
, 0);
4229 /* If the string passed to fputs is a constant and is one character
4230 long, we attempt to transform this call into __builtin_fputc(). */
4233 expand_builtin_fputs (tree arglist
, rtx target
, bool unlocked
)
4235 /* Verify the arguments in the original call. */
4236 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4238 tree result
= fold_builtin_fputs (arglist
, (target
== const0_rtx
),
4239 unlocked
, NULL_TREE
);
4241 return expand_expr (result
, target
, VOIDmode
, EXPAND_NORMAL
);
4246 /* Expand a call to __builtin_expect. We return our argument and emit a
4247 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4248 a non-jump context. */
4251 expand_builtin_expect (tree arglist
, rtx target
)
4256 if (arglist
== NULL_TREE
4257 || TREE_CHAIN (arglist
) == NULL_TREE
)
4259 exp
= TREE_VALUE (arglist
);
4260 c
= TREE_VALUE (TREE_CHAIN (arglist
));
4262 if (TREE_CODE (c
) != INTEGER_CST
)
4264 error ("second argument to %<__builtin_expect%> must be a constant");
4265 c
= integer_zero_node
;
4268 target
= expand_expr (exp
, target
, VOIDmode
, EXPAND_NORMAL
);
4270 /* Don't bother with expected value notes for integral constants. */
4271 if (flag_guess_branch_prob
&& GET_CODE (target
) != CONST_INT
)
4273 /* We do need to force this into a register so that we can be
4274 moderately sure to be able to correctly interpret the branch
4276 target
= force_reg (GET_MODE (target
), target
);
4278 rtx_c
= expand_expr (c
, NULL_RTX
, GET_MODE (target
), EXPAND_NORMAL
);
4280 note
= emit_note (NOTE_INSN_EXPECTED_VALUE
);
4281 NOTE_EXPECTED_VALUE (note
) = gen_rtx_EQ (VOIDmode
, target
, rtx_c
);
4287 /* Like expand_builtin_expect, except do this in a jump context. This is
4288 called from do_jump if the conditional is a __builtin_expect. Return either
4289 a list of insns to emit the jump or NULL if we cannot optimize
4290 __builtin_expect. We need to optimize this at jump time so that machines
4291 like the PowerPC don't turn the test into a SCC operation, and then jump
4292 based on the test being 0/1. */
4295 expand_builtin_expect_jump (tree exp
, rtx if_false_label
, rtx if_true_label
)
4297 tree arglist
= TREE_OPERAND (exp
, 1);
4298 tree arg0
= TREE_VALUE (arglist
);
4299 tree arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
4302 /* Only handle __builtin_expect (test, 0) and
4303 __builtin_expect (test, 1). */
4304 if (TREE_CODE (TREE_TYPE (arg1
)) == INTEGER_TYPE
4305 && (integer_zerop (arg1
) || integer_onep (arg1
)))
4307 rtx insn
, drop_through_label
, temp
;
4309 /* Expand the jump insns. */
4311 do_jump (arg0
, if_false_label
, if_true_label
);
4314 drop_through_label
= get_last_insn ();
4315 if (drop_through_label
&& NOTE_P (drop_through_label
))
4316 drop_through_label
= prev_nonnote_insn (drop_through_label
);
4317 if (drop_through_label
&& !LABEL_P (drop_through_label
))
4318 drop_through_label
= NULL_RTX
;
4321 if (! if_true_label
)
4322 if_true_label
= drop_through_label
;
4323 if (! if_false_label
)
4324 if_false_label
= drop_through_label
;
4326 /* Go through and add the expect's to each of the conditional jumps. */
4328 while (insn
!= NULL_RTX
)
4330 rtx next
= NEXT_INSN (insn
);
4332 if (JUMP_P (insn
) && any_condjump_p (insn
))
4334 rtx ifelse
= SET_SRC (pc_set (insn
));
4335 rtx then_dest
= XEXP (ifelse
, 1);
4336 rtx else_dest
= XEXP (ifelse
, 2);
4339 /* First check if we recognize any of the labels. */
4340 if (GET_CODE (then_dest
) == LABEL_REF
4341 && XEXP (then_dest
, 0) == if_true_label
)
4343 else if (GET_CODE (then_dest
) == LABEL_REF
4344 && XEXP (then_dest
, 0) == if_false_label
)
4346 else if (GET_CODE (else_dest
) == LABEL_REF
4347 && XEXP (else_dest
, 0) == if_false_label
)
4349 else if (GET_CODE (else_dest
) == LABEL_REF
4350 && XEXP (else_dest
, 0) == if_true_label
)
4352 /* Otherwise check where we drop through. */
4353 else if (else_dest
== pc_rtx
)
4355 if (next
&& NOTE_P (next
))
4356 next
= next_nonnote_insn (next
);
4358 if (next
&& JUMP_P (next
)
4359 && any_uncondjump_p (next
))
4360 temp
= XEXP (SET_SRC (pc_set (next
)), 0);
4364 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4365 else that can't possibly match either target label. */
4366 if (temp
== if_false_label
)
4368 else if (temp
== if_true_label
)
4371 else if (then_dest
== pc_rtx
)
4373 if (next
&& NOTE_P (next
))
4374 next
= next_nonnote_insn (next
);
4376 if (next
&& JUMP_P (next
)
4377 && any_uncondjump_p (next
))
4378 temp
= XEXP (SET_SRC (pc_set (next
)), 0);
4382 if (temp
== if_false_label
)
4384 else if (temp
== if_true_label
)
4390 /* If the test is expected to fail, reverse the
4392 if (integer_zerop (arg1
))
4394 predict_insn_def (insn
, PRED_BUILTIN_EXPECT
, taken
);
4406 expand_builtin_trap (void)
4410 emit_insn (gen_trap ());
4413 emit_library_call (abort_libfunc
, LCT_NORETURN
, VOIDmode
, 0);
4417 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4418 Return 0 if a normal call should be emitted rather than expanding
4419 the function inline. If convenient, the result should be placed
4420 in TARGET. SUBTARGET may be used as the target for computing
4424 expand_builtin_fabs (tree arglist
, rtx target
, rtx subtarget
)
4426 enum machine_mode mode
;
4430 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
4433 arg
= TREE_VALUE (arglist
);
4434 mode
= TYPE_MODE (TREE_TYPE (arg
));
4435 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
4436 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
4439 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4440 Return NULL is a normal call should be emitted rather than expanding the
4441 function inline. If convenient, the result should be placed in TARGET.
4442 SUBTARGET may be used as the target for computing the operand. */
4445 expand_builtin_copysign (tree arglist
, rtx target
, rtx subtarget
)
4450 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
4453 arg
= TREE_VALUE (arglist
);
4454 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
4456 arg
= TREE_VALUE (TREE_CHAIN (arglist
));
4457 op1
= expand_expr (arg
, NULL
, VOIDmode
, 0);
4459 return expand_copysign (op0
, op1
, target
);
4462 /* Create a new constant string literal and return a char* pointer to it.
4463 The STRING_CST value is the LEN characters at STR. */
4465 build_string_literal (int len
, const char *str
)
4467 tree t
, elem
, index
, type
;
4469 t
= build_string (len
, str
);
4470 elem
= build_type_variant (char_type_node
, 1, 0);
4471 index
= build_index_type (build_int_cst (NULL_TREE
, len
- 1));
4472 type
= build_array_type (elem
, index
);
4473 TREE_TYPE (t
) = type
;
4474 TREE_CONSTANT (t
) = 1;
4475 TREE_INVARIANT (t
) = 1;
4476 TREE_READONLY (t
) = 1;
4477 TREE_STATIC (t
) = 1;
4479 type
= build_pointer_type (type
);
4480 t
= build1 (ADDR_EXPR
, type
, t
);
4482 type
= build_pointer_type (elem
);
4483 t
= build1 (NOP_EXPR
, type
, t
);
4487 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4488 Return 0 if a normal call should be emitted rather than transforming
4489 the function inline. If convenient, the result should be placed in
4490 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4493 expand_builtin_printf (tree arglist
, rtx target
, enum machine_mode mode
,
4496 tree fn_putchar
= unlocked
4497 ? implicit_built_in_decls
[BUILT_IN_PUTCHAR_UNLOCKED
]
4498 : implicit_built_in_decls
[BUILT_IN_PUTCHAR
];
4499 tree fn_puts
= unlocked
? implicit_built_in_decls
[BUILT_IN_PUTS_UNLOCKED
]
4500 : implicit_built_in_decls
[BUILT_IN_PUTS
];
4501 const char *fmt_str
;
4504 /* If the return value is used, don't do the transformation. */
4505 if (target
!= const0_rtx
)
4508 /* Verify the required arguments in the original call. */
4511 fmt
= TREE_VALUE (arglist
);
4512 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
4514 arglist
= TREE_CHAIN (arglist
);
4516 /* Check whether the format is a literal string constant. */
4517 fmt_str
= c_getstr (fmt
);
4518 if (fmt_str
== NULL
)
4521 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4522 if (strcmp (fmt_str
, "%s\n") == 0)
4525 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
4526 || TREE_CHAIN (arglist
))
4530 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4531 else if (strcmp (fmt_str
, "%c") == 0)
4534 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
4535 || TREE_CHAIN (arglist
))
4541 /* We can't handle anything else with % args or %% ... yet. */
4542 if (strchr (fmt_str
, '%'))
4548 /* If the format specifier was "", printf does nothing. */
4549 if (fmt_str
[0] == '\0')
4551 /* If the format specifier has length of 1, call putchar. */
4552 if (fmt_str
[1] == '\0')
4554 /* Given printf("c"), (where c is any one character,)
4555 convert "c"[0] to an int and pass that to the replacement
4557 arg
= build_int_cst (NULL_TREE
, fmt_str
[0]);
4558 arglist
= build_tree_list (NULL_TREE
, arg
);
4563 /* If the format specifier was "string\n", call puts("string"). */
4564 size_t len
= strlen (fmt_str
);
4565 if (fmt_str
[len
- 1] == '\n')
4567 /* Create a NUL-terminated string that's one char shorter
4568 than the original, stripping off the trailing '\n'. */
4569 char *newstr
= alloca (len
);
4570 memcpy (newstr
, fmt_str
, len
- 1);
4571 newstr
[len
- 1] = 0;
4573 arg
= build_string_literal (len
, newstr
);
4574 arglist
= build_tree_list (NULL_TREE
, arg
);
4578 /* We'd like to arrange to call fputs(string,stdout) here,
4579 but we need stdout and don't have a way to get it yet. */
4586 return expand_expr (build_function_call_expr (fn
, arglist
),
4587 target
, mode
, EXPAND_NORMAL
);
4590 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4591 Return 0 if a normal call should be emitted rather than transforming
4592 the function inline. If convenient, the result should be placed in
4593 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4596 expand_builtin_fprintf (tree arglist
, rtx target
, enum machine_mode mode
,
4599 tree fn_fputc
= unlocked
? implicit_built_in_decls
[BUILT_IN_FPUTC_UNLOCKED
]
4600 : implicit_built_in_decls
[BUILT_IN_FPUTC
];
4601 tree fn_fputs
= unlocked
? implicit_built_in_decls
[BUILT_IN_FPUTS_UNLOCKED
]
4602 : implicit_built_in_decls
[BUILT_IN_FPUTS
];
4603 const char *fmt_str
;
4604 tree fn
, fmt
, fp
, arg
;
4606 /* If the return value is used, don't do the transformation. */
4607 if (target
!= const0_rtx
)
4610 /* Verify the required arguments in the original call. */
4613 fp
= TREE_VALUE (arglist
);
4614 if (! POINTER_TYPE_P (TREE_TYPE (fp
)))
4616 arglist
= TREE_CHAIN (arglist
);
4619 fmt
= TREE_VALUE (arglist
);
4620 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
4622 arglist
= TREE_CHAIN (arglist
);
4624 /* Check whether the format is a literal string constant. */
4625 fmt_str
= c_getstr (fmt
);
4626 if (fmt_str
== NULL
)
4629 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4630 if (strcmp (fmt_str
, "%s") == 0)
4633 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
4634 || TREE_CHAIN (arglist
))
4636 arg
= TREE_VALUE (arglist
);
4637 arglist
= build_tree_list (NULL_TREE
, fp
);
4638 arglist
= tree_cons (NULL_TREE
, arg
, arglist
);
4641 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
4642 else if (strcmp (fmt_str
, "%c") == 0)
4645 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
4646 || TREE_CHAIN (arglist
))
4648 arg
= TREE_VALUE (arglist
);
4649 arglist
= build_tree_list (NULL_TREE
, fp
);
4650 arglist
= tree_cons (NULL_TREE
, arg
, arglist
);
4655 /* We can't handle anything else with % args or %% ... yet. */
4656 if (strchr (fmt_str
, '%'))
4662 /* If the format specifier was "", fprintf does nothing. */
4663 if (fmt_str
[0] == '\0')
4665 /* Evaluate and ignore FILE* argument for side-effects. */
4666 expand_expr (fp
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4670 /* When "string" doesn't contain %, replace all cases of
4671 fprintf(stream,string) with fputs(string,stream). The fputs
4672 builtin will take care of special cases like length == 1. */
4673 arglist
= build_tree_list (NULL_TREE
, fp
);
4674 arglist
= tree_cons (NULL_TREE
, fmt
, arglist
);
4680 return expand_expr (build_function_call_expr (fn
, arglist
),
4681 target
, mode
, EXPAND_NORMAL
);
4684 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
4685 a normal call should be emitted rather than expanding the function
4686 inline. If convenient, the result should be placed in TARGET with
4690 expand_builtin_sprintf (tree arglist
, rtx target
, enum machine_mode mode
)
4692 tree orig_arglist
, dest
, fmt
;
4693 const char *fmt_str
;
4695 orig_arglist
= arglist
;
4697 /* Verify the required arguments in the original call. */
4700 dest
= TREE_VALUE (arglist
);
4701 if (! POINTER_TYPE_P (TREE_TYPE (dest
)))
4703 arglist
= TREE_CHAIN (arglist
);
4706 fmt
= TREE_VALUE (arglist
);
4707 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
4709 arglist
= TREE_CHAIN (arglist
);
4711 /* Check whether the format is a literal string constant. */
4712 fmt_str
= c_getstr (fmt
);
4713 if (fmt_str
== NULL
)
4716 /* If the format doesn't contain % args or %%, use strcpy. */
4717 if (strchr (fmt_str
, '%') == 0)
4719 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
4722 if (arglist
|| ! fn
)
4724 expand_expr (build_function_call_expr (fn
, orig_arglist
),
4725 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4726 if (target
== const0_rtx
)
4728 exp
= build_int_cst (NULL_TREE
, strlen (fmt_str
));
4729 return expand_expr (exp
, target
, mode
, EXPAND_NORMAL
);
4731 /* If the format is "%s", use strcpy if the result isn't used. */
4732 else if (strcmp (fmt_str
, "%s") == 0)
4735 fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
4740 if (! arglist
|| TREE_CHAIN (arglist
))
4742 arg
= TREE_VALUE (arglist
);
4743 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
4746 if (target
!= const0_rtx
)
4748 len
= c_strlen (arg
, 1);
4749 if (! len
|| TREE_CODE (len
) != INTEGER_CST
)
4755 arglist
= build_tree_list (NULL_TREE
, arg
);
4756 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
4757 expand_expr (build_function_call_expr (fn
, arglist
),
4758 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4760 if (target
== const0_rtx
)
4762 return expand_expr (len
, target
, mode
, EXPAND_NORMAL
);
4768 /* Expand a call to either the entry or exit function profiler. */
4771 expand_builtin_profile_func (bool exitp
)
4775 this = DECL_RTL (current_function_decl
);
4776 gcc_assert (MEM_P (this));
4777 this = XEXP (this, 0);
4780 which
= profile_function_exit_libfunc
;
4782 which
= profile_function_entry_libfunc
;
4784 emit_library_call (which
, LCT_NORMAL
, VOIDmode
, 2, this, Pmode
,
4785 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS
,
4786 0, hard_frame_pointer_rtx
),
4792 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4795 round_trampoline_addr (rtx tramp
)
4797 rtx temp
, addend
, mask
;
4799 /* If we don't need too much alignment, we'll have been guaranteed
4800 proper alignment by get_trampoline_type. */
4801 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
4804 /* Round address up to desired boundary. */
4805 temp
= gen_reg_rtx (Pmode
);
4806 addend
= GEN_INT (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1);
4807 mask
= GEN_INT (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
);
4809 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
4810 temp
, 0, OPTAB_LIB_WIDEN
);
4811 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
4812 temp
, 0, OPTAB_LIB_WIDEN
);
4818 expand_builtin_init_trampoline (tree arglist
)
4820 tree t_tramp
, t_func
, t_chain
;
4821 rtx r_tramp
, r_func
, r_chain
;
4822 #ifdef TRAMPOLINE_TEMPLATE
4826 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
,
4827 POINTER_TYPE
, VOID_TYPE
))
4830 t_tramp
= TREE_VALUE (arglist
);
4831 arglist
= TREE_CHAIN (arglist
);
4832 t_func
= TREE_VALUE (arglist
);
4833 arglist
= TREE_CHAIN (arglist
);
4834 t_chain
= TREE_VALUE (arglist
);
4836 r_tramp
= expand_expr (t_tramp
, NULL_RTX
, VOIDmode
, 0);
4837 r_func
= expand_expr (t_func
, NULL_RTX
, VOIDmode
, 0);
4838 r_chain
= expand_expr (t_chain
, NULL_RTX
, VOIDmode
, 0);
4840 /* Generate insns to initialize the trampoline. */
4841 r_tramp
= round_trampoline_addr (r_tramp
);
4842 #ifdef TRAMPOLINE_TEMPLATE
4843 blktramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
4844 set_mem_align (blktramp
, TRAMPOLINE_ALIGNMENT
);
4845 emit_block_move (blktramp
, assemble_trampoline_template (),
4846 GEN_INT (TRAMPOLINE_SIZE
), BLOCK_OP_NORMAL
);
4848 trampolines_created
= 1;
4849 INITIALIZE_TRAMPOLINE (r_tramp
, r_func
, r_chain
);
4855 expand_builtin_adjust_trampoline (tree arglist
)
4859 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
4862 tramp
= expand_expr (TREE_VALUE (arglist
), NULL_RTX
, VOIDmode
, 0);
4863 tramp
= round_trampoline_addr (tramp
);
4864 #ifdef TRAMPOLINE_ADJUST_ADDRESS
4865 TRAMPOLINE_ADJUST_ADDRESS (tramp
);
4871 /* Expand a call to the built-in signbit, signbitf or signbitl function.
4872 Return NULL_RTX if a normal call should be emitted rather than expanding
4873 the function in-line. EXP is the expression that is a call to the builtin
4874 function; if convenient, the result should be placed in TARGET. */
4877 expand_builtin_signbit (tree exp
, rtx target
)
4879 const struct real_format
*fmt
;
4880 enum machine_mode fmode
, imode
, rmode
;
4881 HOST_WIDE_INT hi
, lo
;
4886 arglist
= TREE_OPERAND (exp
, 1);
4887 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
4890 arg
= TREE_VALUE (arglist
);
4891 fmode
= TYPE_MODE (TREE_TYPE (arg
));
4892 rmode
= TYPE_MODE (TREE_TYPE (exp
));
4893 fmt
= REAL_MODE_FORMAT (fmode
);
4895 /* For floating point formats without a sign bit, implement signbit
4897 if (fmt
->signbit
< 0)
4899 /* But we can't do this if the format supports signed zero. */
4900 if (fmt
->has_signed_zero
&& HONOR_SIGNED_ZEROS (fmode
))
4903 arg
= fold (build2 (LT_EXPR
, TREE_TYPE (exp
), arg
,
4904 build_real (TREE_TYPE (arg
), dconst0
)));
4905 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4908 imode
= int_mode_for_mode (fmode
);
4909 if (imode
== BLKmode
)
4912 bitpos
= fmt
->signbit
;
4913 /* Handle targets with different FP word orders. */
4914 if (FLOAT_WORDS_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
4916 int nwords
= GET_MODE_BITSIZE (fmode
) / BITS_PER_WORD
;
4917 int word
= nwords
- (bitpos
/ BITS_PER_WORD
) - 1;
4918 bitpos
= word
* BITS_PER_WORD
+ bitpos
% BITS_PER_WORD
;
4921 /* If the sign bit is not in the lowpart and the floating point format
4922 is wider than an integer, check that is twice the size of an integer
4923 so that we can use gen_highpart below. */
4924 if (bitpos
>= GET_MODE_BITSIZE (rmode
)
4925 && GET_MODE_BITSIZE (imode
) != 2 * GET_MODE_BITSIZE (rmode
))
4928 temp
= expand_expr (arg
, NULL_RTX
, VOIDmode
, 0);
4929 temp
= gen_lowpart (imode
, temp
);
4931 if (GET_MODE_BITSIZE (imode
) > GET_MODE_BITSIZE (rmode
))
4933 if (BYTES_BIG_ENDIAN
)
4934 bitpos
= GET_MODE_BITSIZE (imode
) - 1 - bitpos
;
4935 temp
= copy_to_mode_reg (imode
, temp
);
4936 temp
= extract_bit_field (temp
, 1, bitpos
, 1,
4937 NULL_RTX
, rmode
, rmode
);
4941 if (GET_MODE_BITSIZE (imode
) < GET_MODE_BITSIZE (rmode
))
4942 temp
= gen_lowpart (rmode
, temp
);
4943 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
4946 lo
= (HOST_WIDE_INT
) 1 << bitpos
;
4950 hi
= (HOST_WIDE_INT
) 1 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
4954 temp
= force_reg (rmode
, temp
);
4955 temp
= expand_binop (rmode
, and_optab
, temp
,
4956 immed_double_const (lo
, hi
, rmode
),
4957 target
, 1, OPTAB_LIB_WIDEN
);
4962 /* Expand fork or exec calls. TARGET is the desired target of the
4963 call. ARGLIST is the list of arguments of the call. FN is the
4964 identificator of the actual function. IGNORE is nonzero if the
4965 value is to be ignored. */
4968 expand_builtin_fork_or_exec (tree fn
, tree arglist
, rtx target
, int ignore
)
4973 /* If we are not profiling, just call the function. */
4974 if (!profile_arc_flag
)
4977 /* Otherwise call the wrapper. This should be equivalent for the rest of
4978 compiler, so the code does not diverge, and the wrapper may run the
4979 code necessary for keeping the profiling sane. */
4981 switch (DECL_FUNCTION_CODE (fn
))
4984 id
= get_identifier ("__gcov_fork");
4987 case BUILT_IN_EXECL
:
4988 id
= get_identifier ("__gcov_execl");
4991 case BUILT_IN_EXECV
:
4992 id
= get_identifier ("__gcov_execv");
4995 case BUILT_IN_EXECLP
:
4996 id
= get_identifier ("__gcov_execlp");
4999 case BUILT_IN_EXECLE
:
5000 id
= get_identifier ("__gcov_execle");
5003 case BUILT_IN_EXECVP
:
5004 id
= get_identifier ("__gcov_execvp");
5007 case BUILT_IN_EXECVE
:
5008 id
= get_identifier ("__gcov_execve");
5015 decl
= build_decl (FUNCTION_DECL
, id
, TREE_TYPE (fn
));
5016 DECL_EXTERNAL (decl
) = 1;
5017 TREE_PUBLIC (decl
) = 1;
5018 DECL_ARTIFICIAL (decl
) = 1;
5019 TREE_NOTHROW (decl
) = 1;
5020 call
= build_function_call_expr (decl
, arglist
);
5022 return expand_call (call
, target
, ignore
);
5025 /* Expand an expression EXP that calls a built-in function,
5026 with result going to TARGET if that's convenient
5027 (and in mode MODE if that's convenient).
5028 SUBTARGET may be used as the target for computing one of EXP's operands.
5029 IGNORE is nonzero if the value is to be ignored. */
5032 expand_builtin (tree exp
, rtx target
, rtx subtarget
, enum machine_mode mode
,
5035 tree fndecl
= get_callee_fndecl (exp
);
5036 tree arglist
= TREE_OPERAND (exp
, 1);
5037 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5038 enum machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
5040 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5041 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
5043 /* When not optimizing, generate calls to library functions for a certain
5046 && !CALLED_AS_BUILT_IN (fndecl
)
5047 && DECL_ASSEMBLER_NAME_SET_P (fndecl
)
5048 && fcode
!= BUILT_IN_ALLOCA
)
5049 return expand_call (exp
, target
, ignore
);
5051 /* The built-in function expanders test for target == const0_rtx
5052 to determine whether the function's result will be ignored. */
5054 target
= const0_rtx
;
5056 /* If the result of a pure or const built-in function is ignored, and
5057 none of its arguments are volatile, we can avoid expanding the
5058 built-in call and just evaluate the arguments for side-effects. */
5059 if (target
== const0_rtx
5060 && (DECL_IS_PURE (fndecl
) || TREE_READONLY (fndecl
)))
5062 bool volatilep
= false;
5065 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
5066 if (TREE_THIS_VOLATILE (TREE_VALUE (arg
)))
5074 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
5075 expand_expr (TREE_VALUE (arg
), const0_rtx
,
5076 VOIDmode
, EXPAND_NORMAL
);
5084 case BUILT_IN_FABSF
:
5085 case BUILT_IN_FABSL
:
5086 target
= expand_builtin_fabs (arglist
, target
, subtarget
);
5091 case BUILT_IN_COPYSIGN
:
5092 case BUILT_IN_COPYSIGNF
:
5093 case BUILT_IN_COPYSIGNL
:
5094 target
= expand_builtin_copysign (arglist
, target
, subtarget
);
5099 /* Just do a normal library call if we were unable to fold
5102 case BUILT_IN_CABSF
:
5103 case BUILT_IN_CABSL
:
5109 case BUILT_IN_EXP10
:
5110 case BUILT_IN_EXP10F
:
5111 case BUILT_IN_EXP10L
:
5112 case BUILT_IN_POW10
:
5113 case BUILT_IN_POW10F
:
5114 case BUILT_IN_POW10L
:
5116 case BUILT_IN_EXP2F
:
5117 case BUILT_IN_EXP2L
:
5118 case BUILT_IN_EXPM1
:
5119 case BUILT_IN_EXPM1F
:
5120 case BUILT_IN_EXPM1L
:
5122 case BUILT_IN_LOGBF
:
5123 case BUILT_IN_LOGBL
:
5124 case BUILT_IN_ILOGB
:
5125 case BUILT_IN_ILOGBF
:
5126 case BUILT_IN_ILOGBL
:
5130 case BUILT_IN_LOG10
:
5131 case BUILT_IN_LOG10F
:
5132 case BUILT_IN_LOG10L
:
5134 case BUILT_IN_LOG2F
:
5135 case BUILT_IN_LOG2L
:
5136 case BUILT_IN_LOG1P
:
5137 case BUILT_IN_LOG1PF
:
5138 case BUILT_IN_LOG1PL
:
5143 case BUILT_IN_ASINF
:
5144 case BUILT_IN_ASINL
:
5146 case BUILT_IN_ACOSF
:
5147 case BUILT_IN_ACOSL
:
5149 case BUILT_IN_ATANF
:
5150 case BUILT_IN_ATANL
:
5151 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5152 because of possible accuracy problems. */
5153 if (! flag_unsafe_math_optimizations
)
5156 case BUILT_IN_SQRTF
:
5157 case BUILT_IN_SQRTL
:
5158 case BUILT_IN_FLOOR
:
5159 case BUILT_IN_FLOORF
:
5160 case BUILT_IN_FLOORL
:
5162 case BUILT_IN_CEILF
:
5163 case BUILT_IN_CEILL
:
5164 case BUILT_IN_TRUNC
:
5165 case BUILT_IN_TRUNCF
:
5166 case BUILT_IN_TRUNCL
:
5167 case BUILT_IN_ROUND
:
5168 case BUILT_IN_ROUNDF
:
5169 case BUILT_IN_ROUNDL
:
5170 case BUILT_IN_NEARBYINT
:
5171 case BUILT_IN_NEARBYINTF
:
5172 case BUILT_IN_NEARBYINTL
:
5174 case BUILT_IN_RINTF
:
5175 case BUILT_IN_RINTL
:
5176 target
= expand_builtin_mathfn (exp
, target
, subtarget
);
5184 target
= expand_builtin_pow (exp
, target
, subtarget
);
5189 case BUILT_IN_ATAN2
:
5190 case BUILT_IN_ATAN2F
:
5191 case BUILT_IN_ATAN2L
:
5193 case BUILT_IN_FMODF
:
5194 case BUILT_IN_FMODL
:
5196 case BUILT_IN_DREMF
:
5197 case BUILT_IN_DREML
:
5198 if (! flag_unsafe_math_optimizations
)
5200 target
= expand_builtin_mathfn_2 (exp
, target
, subtarget
);
5211 if (! flag_unsafe_math_optimizations
)
5213 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
5218 case BUILT_IN_APPLY_ARGS
:
5219 return expand_builtin_apply_args ();
5221 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5222 FUNCTION with a copy of the parameters described by
5223 ARGUMENTS, and ARGSIZE. It returns a block of memory
5224 allocated on the stack into which is stored all the registers
5225 that might possibly be used for returning the result of a
5226 function. ARGUMENTS is the value returned by
5227 __builtin_apply_args. ARGSIZE is the number of bytes of
5228 arguments that must be copied. ??? How should this value be
5229 computed? We'll also need a safe worst case value for varargs
5231 case BUILT_IN_APPLY
:
5232 if (!validate_arglist (arglist
, POINTER_TYPE
,
5233 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
5234 && !validate_arglist (arglist
, REFERENCE_TYPE
,
5235 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
5243 for (t
= arglist
, i
= 0; t
; t
= TREE_CHAIN (t
), i
++)
5244 ops
[i
] = expand_expr (TREE_VALUE (t
), NULL_RTX
, VOIDmode
, 0);
5246 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
5249 /* __builtin_return (RESULT) causes the function to return the
5250 value described by RESULT. RESULT is address of the block of
5251 memory returned by __builtin_apply. */
5252 case BUILT_IN_RETURN
:
5253 if (validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
5254 expand_builtin_return (expand_expr (TREE_VALUE (arglist
),
5255 NULL_RTX
, VOIDmode
, 0));
5258 case BUILT_IN_SAVEREGS
:
5259 return expand_builtin_saveregs ();
5261 case BUILT_IN_ARGS_INFO
:
5262 return expand_builtin_args_info (arglist
);
5264 /* Return the address of the first anonymous stack arg. */
5265 case BUILT_IN_NEXT_ARG
:
5266 if (fold_builtin_next_arg (arglist
))
5268 return expand_builtin_next_arg ();
5270 case BUILT_IN_CLASSIFY_TYPE
:
5271 return expand_builtin_classify_type (arglist
);
5273 case BUILT_IN_CONSTANT_P
:
5276 case BUILT_IN_FRAME_ADDRESS
:
5277 case BUILT_IN_RETURN_ADDRESS
:
5278 return expand_builtin_frame_address (fndecl
, arglist
);
5280 /* Returns the address of the area where the structure is returned.
5282 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
5284 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
5285 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
5288 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
5290 case BUILT_IN_ALLOCA
:
5291 target
= expand_builtin_alloca (arglist
, target
);
5296 case BUILT_IN_STACK_SAVE
:
5297 return expand_stack_save ();
5299 case BUILT_IN_STACK_RESTORE
:
5300 expand_stack_restore (TREE_VALUE (arglist
));
5305 case BUILT_IN_FFSLL
:
5306 case BUILT_IN_FFSIMAX
:
5307 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5308 subtarget
, ffs_optab
);
5315 case BUILT_IN_CLZLL
:
5316 case BUILT_IN_CLZIMAX
:
5317 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5318 subtarget
, clz_optab
);
5325 case BUILT_IN_CTZLL
:
5326 case BUILT_IN_CTZIMAX
:
5327 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5328 subtarget
, ctz_optab
);
5333 case BUILT_IN_POPCOUNT
:
5334 case BUILT_IN_POPCOUNTL
:
5335 case BUILT_IN_POPCOUNTLL
:
5336 case BUILT_IN_POPCOUNTIMAX
:
5337 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5338 subtarget
, popcount_optab
);
5343 case BUILT_IN_PARITY
:
5344 case BUILT_IN_PARITYL
:
5345 case BUILT_IN_PARITYLL
:
5346 case BUILT_IN_PARITYIMAX
:
5347 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5348 subtarget
, parity_optab
);
5353 case BUILT_IN_STRLEN
:
5354 target
= expand_builtin_strlen (arglist
, target
, target_mode
);
5359 case BUILT_IN_STRCPY
:
5360 target
= expand_builtin_strcpy (exp
, target
, mode
);
5365 case BUILT_IN_STRNCPY
:
5366 target
= expand_builtin_strncpy (exp
, target
, mode
);
5371 case BUILT_IN_STPCPY
:
5372 target
= expand_builtin_stpcpy (exp
, target
, mode
);
5377 case BUILT_IN_STRCAT
:
5378 target
= expand_builtin_strcat (arglist
, TREE_TYPE (exp
), target
, mode
);
5383 case BUILT_IN_STRNCAT
:
5384 target
= expand_builtin_strncat (arglist
, target
, mode
);
5389 case BUILT_IN_STRSPN
:
5390 target
= expand_builtin_strspn (arglist
, target
, mode
);
5395 case BUILT_IN_STRCSPN
:
5396 target
= expand_builtin_strcspn (arglist
, target
, mode
);
5401 case BUILT_IN_STRSTR
:
5402 target
= expand_builtin_strstr (arglist
, target
, mode
);
5407 case BUILT_IN_STRPBRK
:
5408 target
= expand_builtin_strpbrk (arglist
, target
, mode
);
5413 case BUILT_IN_INDEX
:
5414 case BUILT_IN_STRCHR
:
5415 target
= expand_builtin_strchr (arglist
, target
, mode
);
5420 case BUILT_IN_RINDEX
:
5421 case BUILT_IN_STRRCHR
:
5422 target
= expand_builtin_strrchr (arglist
, target
, mode
);
5427 case BUILT_IN_MEMCPY
:
5428 target
= expand_builtin_memcpy (exp
, target
, mode
);
5433 case BUILT_IN_MEMPCPY
:
5434 target
= expand_builtin_mempcpy (arglist
, TREE_TYPE (exp
), target
, mode
, /*endp=*/ 1);
5439 case BUILT_IN_MEMMOVE
:
5440 target
= expand_builtin_memmove (arglist
, TREE_TYPE (exp
), target
, mode
);
5445 case BUILT_IN_BCOPY
:
5446 target
= expand_builtin_bcopy (arglist
, TREE_TYPE (exp
));
5451 case BUILT_IN_MEMSET
:
5452 target
= expand_builtin_memset (arglist
, target
, mode
);
5457 case BUILT_IN_BZERO
:
5458 target
= expand_builtin_bzero (arglist
);
5463 case BUILT_IN_STRCMP
:
5464 target
= expand_builtin_strcmp (exp
, target
, mode
);
5469 case BUILT_IN_STRNCMP
:
5470 target
= expand_builtin_strncmp (exp
, target
, mode
);
5476 case BUILT_IN_MEMCMP
:
5477 target
= expand_builtin_memcmp (exp
, arglist
, target
, mode
);
5482 case BUILT_IN_SETJMP
:
5483 target
= expand_builtin_setjmp (arglist
, target
);
5488 /* __builtin_longjmp is passed a pointer to an array of five words.
5489 It's similar to the C library longjmp function but works with
5490 __builtin_setjmp above. */
5491 case BUILT_IN_LONGJMP
:
5492 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
5496 rtx buf_addr
= expand_expr (TREE_VALUE (arglist
), subtarget
,
5498 rtx value
= expand_expr (TREE_VALUE (TREE_CHAIN (arglist
)),
5499 NULL_RTX
, VOIDmode
, 0);
5501 if (value
!= const1_rtx
)
5503 error ("%<__builtin_longjmp%> second argument must be 1");
5507 expand_builtin_longjmp (buf_addr
, value
);
5511 case BUILT_IN_NONLOCAL_GOTO
:
5512 target
= expand_builtin_nonlocal_goto (arglist
);
5517 /* This updates the setjmp buffer that is its argument with the value
5518 of the current stack pointer. */
5519 case BUILT_IN_UPDATE_SETJMP_BUF
:
5520 if (validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
5523 = expand_expr (TREE_VALUE (arglist
), NULL_RTX
, VOIDmode
, 0);
5525 expand_builtin_update_setjmp_buf (buf_addr
);
5531 expand_builtin_trap ();
5534 case BUILT_IN_PRINTF
:
5535 target
= expand_builtin_printf (arglist
, target
, mode
, false);
5540 case BUILT_IN_PRINTF_UNLOCKED
:
5541 target
= expand_builtin_printf (arglist
, target
, mode
, true);
5546 case BUILT_IN_FPUTS
:
5547 target
= expand_builtin_fputs (arglist
, target
, false);
5551 case BUILT_IN_FPUTS_UNLOCKED
:
5552 target
= expand_builtin_fputs (arglist
, target
, true);
5557 case BUILT_IN_FPRINTF
:
5558 target
= expand_builtin_fprintf (arglist
, target
, mode
, false);
5563 case BUILT_IN_FPRINTF_UNLOCKED
:
5564 target
= expand_builtin_fprintf (arglist
, target
, mode
, true);
5569 case BUILT_IN_SPRINTF
:
5570 target
= expand_builtin_sprintf (arglist
, target
, mode
);
5575 case BUILT_IN_SIGNBIT
:
5576 case BUILT_IN_SIGNBITF
:
5577 case BUILT_IN_SIGNBITL
:
5578 target
= expand_builtin_signbit (exp
, target
);
5583 /* Various hooks for the DWARF 2 __throw routine. */
5584 case BUILT_IN_UNWIND_INIT
:
5585 expand_builtin_unwind_init ();
5587 case BUILT_IN_DWARF_CFA
:
5588 return virtual_cfa_rtx
;
5589 #ifdef DWARF2_UNWIND_INFO
5590 case BUILT_IN_DWARF_SP_COLUMN
:
5591 return expand_builtin_dwarf_sp_column ();
5592 case BUILT_IN_INIT_DWARF_REG_SIZES
:
5593 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist
));
5596 case BUILT_IN_FROB_RETURN_ADDR
:
5597 return expand_builtin_frob_return_addr (TREE_VALUE (arglist
));
5598 case BUILT_IN_EXTRACT_RETURN_ADDR
:
5599 return expand_builtin_extract_return_addr (TREE_VALUE (arglist
));
5600 case BUILT_IN_EH_RETURN
:
5601 expand_builtin_eh_return (TREE_VALUE (arglist
),
5602 TREE_VALUE (TREE_CHAIN (arglist
)));
5604 #ifdef EH_RETURN_DATA_REGNO
5605 case BUILT_IN_EH_RETURN_DATA_REGNO
:
5606 return expand_builtin_eh_return_data_regno (arglist
);
5608 case BUILT_IN_EXTEND_POINTER
:
5609 return expand_builtin_extend_pointer (TREE_VALUE (arglist
));
5611 case BUILT_IN_VA_START
:
5612 case BUILT_IN_STDARG_START
:
5613 return expand_builtin_va_start (arglist
);
5614 case BUILT_IN_VA_END
:
5615 return expand_builtin_va_end (arglist
);
5616 case BUILT_IN_VA_COPY
:
5617 return expand_builtin_va_copy (arglist
);
5618 case BUILT_IN_EXPECT
:
5619 return expand_builtin_expect (arglist
, target
);
5620 case BUILT_IN_PREFETCH
:
5621 expand_builtin_prefetch (arglist
);
5624 case BUILT_IN_PROFILE_FUNC_ENTER
:
5625 return expand_builtin_profile_func (false);
5626 case BUILT_IN_PROFILE_FUNC_EXIT
:
5627 return expand_builtin_profile_func (true);
5629 case BUILT_IN_INIT_TRAMPOLINE
:
5630 return expand_builtin_init_trampoline (arglist
);
5631 case BUILT_IN_ADJUST_TRAMPOLINE
:
5632 return expand_builtin_adjust_trampoline (arglist
);
5635 case BUILT_IN_EXECL
:
5636 case BUILT_IN_EXECV
:
5637 case BUILT_IN_EXECLP
:
5638 case BUILT_IN_EXECLE
:
5639 case BUILT_IN_EXECVP
:
5640 case BUILT_IN_EXECVE
:
5641 target
= expand_builtin_fork_or_exec (fndecl
, arglist
, target
, ignore
);
5646 default: /* just do library call, if unknown builtin */
5650 /* The switch statement above can drop through to cause the function
5651 to be called normally. */
5652 return expand_call (exp
, target
, ignore
);
5655 /* Determine whether a tree node represents a call to a built-in
5656 function. If the tree T is a call to a built-in function with
5657 the right number of arguments of the appropriate types, return
5658 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
5659 Otherwise the return value is END_BUILTINS. */
5661 enum built_in_function
5662 builtin_mathfn_code (tree t
)
5664 tree fndecl
, arglist
, parmlist
;
5665 tree argtype
, parmtype
;
5667 if (TREE_CODE (t
) != CALL_EXPR
5668 || TREE_CODE (TREE_OPERAND (t
, 0)) != ADDR_EXPR
)
5669 return END_BUILTINS
;
5671 fndecl
= get_callee_fndecl (t
);
5672 if (fndecl
== NULL_TREE
5673 || TREE_CODE (fndecl
) != FUNCTION_DECL
5674 || ! DECL_BUILT_IN (fndecl
)
5675 || DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5676 return END_BUILTINS
;
5678 arglist
= TREE_OPERAND (t
, 1);
5679 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
5680 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
5682 /* If a function doesn't take a variable number of arguments,
5683 the last element in the list will have type `void'. */
5684 parmtype
= TREE_VALUE (parmlist
);
5685 if (VOID_TYPE_P (parmtype
))
5688 return END_BUILTINS
;
5689 return DECL_FUNCTION_CODE (fndecl
);
5693 return END_BUILTINS
;
5695 argtype
= TREE_TYPE (TREE_VALUE (arglist
));
5697 if (SCALAR_FLOAT_TYPE_P (parmtype
))
5699 if (! SCALAR_FLOAT_TYPE_P (argtype
))
5700 return END_BUILTINS
;
5702 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
5704 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
5705 return END_BUILTINS
;
5707 else if (POINTER_TYPE_P (parmtype
))
5709 if (! POINTER_TYPE_P (argtype
))
5710 return END_BUILTINS
;
5712 else if (INTEGRAL_TYPE_P (parmtype
))
5714 if (! INTEGRAL_TYPE_P (argtype
))
5715 return END_BUILTINS
;
5718 return END_BUILTINS
;
5720 arglist
= TREE_CHAIN (arglist
);
5723 /* Variable-length argument list. */
5724 return DECL_FUNCTION_CODE (fndecl
);
5727 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5728 constant. ARGLIST is the argument list of the call. */
5731 fold_builtin_constant_p (tree arglist
)
5736 arglist
= TREE_VALUE (arglist
);
5738 /* We return 1 for a numeric type that's known to be a constant
5739 value at compile-time or for an aggregate type that's a
5740 literal constant. */
5741 STRIP_NOPS (arglist
);
5743 /* If we know this is a constant, emit the constant of one. */
5744 if (CONSTANT_CLASS_P (arglist
)
5745 || (TREE_CODE (arglist
) == CONSTRUCTOR
5746 && TREE_CONSTANT (arglist
))
5747 || (TREE_CODE (arglist
) == ADDR_EXPR
5748 && TREE_CODE (TREE_OPERAND (arglist
, 0)) == STRING_CST
))
5749 return integer_one_node
;
5751 /* If this expression has side effects, show we don't know it to be a
5752 constant. Likewise if it's a pointer or aggregate type since in
5753 those case we only want literals, since those are only optimized
5754 when generating RTL, not later.
5755 And finally, if we are compiling an initializer, not code, we
5756 need to return a definite result now; there's not going to be any
5757 more optimization done. */
5758 if (TREE_SIDE_EFFECTS (arglist
)
5759 || AGGREGATE_TYPE_P (TREE_TYPE (arglist
))
5760 || POINTER_TYPE_P (TREE_TYPE (arglist
))
5762 return integer_zero_node
;
5767 /* Fold a call to __builtin_expect, if we expect that a comparison against
5768 the argument will fold to a constant. In practice, this means a true
5769 constant or the address of a non-weak symbol. ARGLIST is the argument
5770 list of the call. */
5773 fold_builtin_expect (tree arglist
)
5780 arg
= TREE_VALUE (arglist
);
5782 /* If the argument isn't invariant, then there's nothing we can do. */
5783 if (!TREE_INVARIANT (arg
))
5786 /* If we're looking at an address of a weak decl, then do not fold. */
5789 if (TREE_CODE (inner
) == ADDR_EXPR
)
5793 inner
= TREE_OPERAND (inner
, 0);
5795 while (TREE_CODE (inner
) == COMPONENT_REF
5796 || TREE_CODE (inner
) == ARRAY_REF
);
5797 if (DECL_P (inner
) && DECL_WEAK (inner
))
5801 /* Otherwise, ARG already has the proper type for the return value. */
5805 /* Fold a call to __builtin_classify_type. */
5808 fold_builtin_classify_type (tree arglist
)
5811 return build_int_cst (NULL_TREE
, no_type_class
);
5813 return build_int_cst (NULL_TREE
,
5814 type_to_class (TREE_TYPE (TREE_VALUE (arglist
))));
5817 /* Fold a call to __builtin_strlen. */
5820 fold_builtin_strlen (tree arglist
)
5822 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
5826 tree len
= c_strlen (TREE_VALUE (arglist
), 0);
5830 /* Convert from the internal "sizetype" type to "size_t". */
5832 len
= fold_convert (size_type_node
, len
);
5840 /* Fold a call to __builtin_inf or __builtin_huge_val. */
5843 fold_builtin_inf (tree type
, int warn
)
5845 REAL_VALUE_TYPE real
;
5847 /* __builtin_inff is intended to be usable to define INFINITY on all
5848 targets. If an infinity is not available, INFINITY expands "to a
5849 positive constant of type float that overflows at translation
5850 time", footnote "In this case, using INFINITY will violate the
5851 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
5852 Thus we pedwarn to ensure this constraint violation is
5854 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
5855 pedwarn ("target format does not support infinity");
5858 return build_real (type
, real
);
5861 /* Fold a call to __builtin_nan or __builtin_nans. */
5864 fold_builtin_nan (tree arglist
, tree type
, int quiet
)
5866 REAL_VALUE_TYPE real
;
5869 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
5871 str
= c_getstr (TREE_VALUE (arglist
));
5875 if (!real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
5878 return build_real (type
, real
);
5881 /* Return true if the floating point expression T has an integer value.
5882 We also allow +Inf, -Inf and NaN to be considered integer values. */
5885 integer_valued_real_p (tree t
)
5887 switch (TREE_CODE (t
))
5894 case NON_LVALUE_EXPR
:
5895 return integer_valued_real_p (TREE_OPERAND (t
, 0));
5900 return integer_valued_real_p (TREE_OPERAND (t
, 1));
5907 return integer_valued_real_p (TREE_OPERAND (t
, 0))
5908 && integer_valued_real_p (TREE_OPERAND (t
, 1));
5911 return integer_valued_real_p (TREE_OPERAND (t
, 1))
5912 && integer_valued_real_p (TREE_OPERAND (t
, 2));
5915 if (! TREE_CONSTANT_OVERFLOW (t
))
5917 REAL_VALUE_TYPE c
, cint
;
5919 c
= TREE_REAL_CST (t
);
5920 real_trunc (&cint
, TYPE_MODE (TREE_TYPE (t
)), &c
);
5921 return real_identical (&c
, &cint
);
5926 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
5927 if (TREE_CODE (type
) == INTEGER_TYPE
)
5929 if (TREE_CODE (type
) == REAL_TYPE
)
5930 return integer_valued_real_p (TREE_OPERAND (t
, 0));
5935 switch (builtin_mathfn_code (t
))
5938 case BUILT_IN_CEILF
:
5939 case BUILT_IN_CEILL
:
5940 case BUILT_IN_FLOOR
:
5941 case BUILT_IN_FLOORF
:
5942 case BUILT_IN_FLOORL
:
5943 case BUILT_IN_NEARBYINT
:
5944 case BUILT_IN_NEARBYINTF
:
5945 case BUILT_IN_NEARBYINTL
:
5947 case BUILT_IN_RINTF
:
5948 case BUILT_IN_RINTL
:
5949 case BUILT_IN_ROUND
:
5950 case BUILT_IN_ROUNDF
:
5951 case BUILT_IN_ROUNDL
:
5952 case BUILT_IN_TRUNC
:
5953 case BUILT_IN_TRUNCF
:
5954 case BUILT_IN_TRUNCL
:
5968 /* EXP is assumed to be builtin call where truncation can be propagated
5969 across (for instance floor((double)f) == (double)floorf (f).
5970 Do the transformation. */
5973 fold_trunc_transparent_mathfn (tree exp
)
5975 tree fndecl
= get_callee_fndecl (exp
);
5976 tree arglist
= TREE_OPERAND (exp
, 1);
5977 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5980 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
5983 arg
= TREE_VALUE (arglist
);
5984 /* Integer rounding functions are idempotent. */
5985 if (fcode
== builtin_mathfn_code (arg
))
5988 /* If argument is already integer valued, and we don't need to worry
5989 about setting errno, there's no need to perform rounding. */
5990 if (! flag_errno_math
&& integer_valued_real_p (arg
))
5995 tree arg0
= strip_float_extensions (arg
);
5996 tree ftype
= TREE_TYPE (exp
);
5997 tree newtype
= TREE_TYPE (arg0
);
6000 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
6001 && (decl
= mathfn_built_in (newtype
, fcode
)))
6004 build_tree_list (NULL_TREE
, fold_convert (newtype
, arg0
));
6005 return fold_convert (ftype
,
6006 build_function_call_expr (decl
, arglist
));
6012 /* EXP is assumed to be builtin call which can narrow the FP type of
6013 the argument, for instance lround((double)f) -> lroundf (f). */
6016 fold_fixed_mathfn (tree exp
)
6018 tree fndecl
= get_callee_fndecl (exp
);
6019 tree arglist
= TREE_OPERAND (exp
, 1);
6020 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
6023 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6026 arg
= TREE_VALUE (arglist
);
6028 /* If argument is already integer valued, and we don't need to worry
6029 about setting errno, there's no need to perform rounding. */
6030 if (! flag_errno_math
&& integer_valued_real_p (arg
))
6031 return fold (build1 (FIX_TRUNC_EXPR
, TREE_TYPE (exp
), arg
));
6035 tree ftype
= TREE_TYPE (arg
);
6036 tree arg0
= strip_float_extensions (arg
);
6037 tree newtype
= TREE_TYPE (arg0
);
6040 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
6041 && (decl
= mathfn_built_in (newtype
, fcode
)))
6044 build_tree_list (NULL_TREE
, fold_convert (newtype
, arg0
));
6045 return build_function_call_expr (decl
, arglist
);
6051 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6052 is the argument list and TYPE is the return type. Return
6053 NULL_TREE if no if no simplification can be made. */
6056 fold_builtin_cabs (tree arglist
, tree type
)
6060 if (!arglist
|| TREE_CHAIN (arglist
))
6063 arg
= TREE_VALUE (arglist
);
6064 if (TREE_CODE (TREE_TYPE (arg
)) != COMPLEX_TYPE
6065 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
6068 /* Evaluate cabs of a constant at compile-time. */
6069 if (flag_unsafe_math_optimizations
6070 && TREE_CODE (arg
) == COMPLEX_CST
6071 && TREE_CODE (TREE_REALPART (arg
)) == REAL_CST
6072 && TREE_CODE (TREE_IMAGPART (arg
)) == REAL_CST
6073 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg
))
6074 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg
)))
6076 REAL_VALUE_TYPE r
, i
;
6078 r
= TREE_REAL_CST (TREE_REALPART (arg
));
6079 i
= TREE_REAL_CST (TREE_IMAGPART (arg
));
6081 real_arithmetic (&r
, MULT_EXPR
, &r
, &r
);
6082 real_arithmetic (&i
, MULT_EXPR
, &i
, &i
);
6083 real_arithmetic (&r
, PLUS_EXPR
, &r
, &i
);
6084 if (real_sqrt (&r
, TYPE_MODE (type
), &r
)
6085 || ! flag_trapping_math
)
6086 return build_real (type
, r
);
6089 /* If either part is zero, cabs is fabs of the other. */
6090 if (TREE_CODE (arg
) == COMPLEX_EXPR
6091 && real_zerop (TREE_OPERAND (arg
, 0)))
6092 return fold (build1 (ABS_EXPR
, type
, TREE_OPERAND (arg
, 1)));
6093 if (TREE_CODE (arg
) == COMPLEX_EXPR
6094 && real_zerop (TREE_OPERAND (arg
, 1)))
6095 return fold (build1 (ABS_EXPR
, type
, TREE_OPERAND (arg
, 0)));
6097 /* Don't do this when optimizing for size. */
6098 if (flag_unsafe_math_optimizations
6099 && optimize
&& !optimize_size
)
6101 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
6103 if (sqrtfn
!= NULL_TREE
)
6105 tree rpart
, ipart
, result
, arglist
;
6107 arg
= builtin_save_expr (arg
);
6109 rpart
= fold (build1 (REALPART_EXPR
, type
, arg
));
6110 ipart
= fold (build1 (IMAGPART_EXPR
, type
, arg
));
6112 rpart
= builtin_save_expr (rpart
);
6113 ipart
= builtin_save_expr (ipart
);
6115 result
= fold (build2 (PLUS_EXPR
, type
,
6116 fold (build2 (MULT_EXPR
, type
,
6118 fold (build2 (MULT_EXPR
, type
,
6121 arglist
= build_tree_list (NULL_TREE
, result
);
6122 return build_function_call_expr (sqrtfn
, arglist
);
6129 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
6130 NULL_TREE if no simplification can be made. */
6133 fold_builtin_sqrt (tree arglist
, tree type
)
6136 enum built_in_function fcode
;
6137 tree arg
= TREE_VALUE (arglist
);
6139 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6142 /* Optimize sqrt of constant value. */
6143 if (TREE_CODE (arg
) == REAL_CST
6144 && ! TREE_CONSTANT_OVERFLOW (arg
))
6146 REAL_VALUE_TYPE r
, x
;
6148 x
= TREE_REAL_CST (arg
);
6149 if (real_sqrt (&r
, TYPE_MODE (type
), &x
)
6150 || (!flag_trapping_math
&& !flag_errno_math
))
6151 return build_real (type
, r
);
6154 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
6155 fcode
= builtin_mathfn_code (arg
);
6156 if (flag_unsafe_math_optimizations
&& BUILTIN_EXPONENT_P (fcode
))
6158 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
6159 arg
= fold (build2 (MULT_EXPR
, type
,
6160 TREE_VALUE (TREE_OPERAND (arg
, 1)),
6161 build_real (type
, dconsthalf
)));
6162 arglist
= build_tree_list (NULL_TREE
, arg
);
6163 return build_function_call_expr (expfn
, arglist
);
6166 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
6167 if (flag_unsafe_math_optimizations
&& BUILTIN_ROOT_P (fcode
))
6169 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
6173 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
6175 /* The inner root was either sqrt or cbrt. */
6176 REAL_VALUE_TYPE dconstroot
=
6177 BUILTIN_SQRT_P (fcode
) ? dconsthalf
: dconstthird
;
6179 /* Adjust for the outer root. */
6180 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
6181 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
6182 tree_root
= build_real (type
, dconstroot
);
6183 arglist
= tree_cons (NULL_TREE
, arg0
,
6184 build_tree_list (NULL_TREE
, tree_root
));
6185 return build_function_call_expr (powfn
, arglist
);
6189 /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5). */
6190 if (flag_unsafe_math_optimizations
6191 && (fcode
== BUILT_IN_POW
6192 || fcode
== BUILT_IN_POWF
6193 || fcode
== BUILT_IN_POWL
))
6195 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
6196 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
6197 tree arg1
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg
, 1)));
6198 tree narg1
= fold (build2 (MULT_EXPR
, type
, arg1
,
6199 build_real (type
, dconsthalf
)));
6200 arglist
= tree_cons (NULL_TREE
, arg0
,
6201 build_tree_list (NULL_TREE
, narg1
));
6202 return build_function_call_expr (powfn
, arglist
);
6208 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
6209 NULL_TREE if no simplification can be made. */
6211 fold_builtin_cbrt (tree arglist
, tree type
)
6213 tree arg
= TREE_VALUE (arglist
);
6214 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
6216 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6219 /* Optimize cbrt of constant value. */
6220 if (real_zerop (arg
) || real_onep (arg
) || real_minus_onep (arg
))
6223 /* Optimize cbrt(expN(x)) -> expN(x/3). */
6224 if (flag_unsafe_math_optimizations
&& BUILTIN_EXPONENT_P (fcode
))
6226 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
6227 const REAL_VALUE_TYPE third_trunc
=
6228 real_value_truncate (TYPE_MODE (type
), dconstthird
);
6229 arg
= fold (build2 (MULT_EXPR
, type
,
6230 TREE_VALUE (TREE_OPERAND (arg
, 1)),
6231 build_real (type
, third_trunc
)));
6232 arglist
= build_tree_list (NULL_TREE
, arg
);
6233 return build_function_call_expr (expfn
, arglist
);
6236 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
6237 /* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
6238 x is negative pow will error but cbrt won't. */
6239 if (flag_unsafe_math_optimizations
&& BUILTIN_SQRT_P (fcode
))
6241 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
6245 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
6247 REAL_VALUE_TYPE dconstroot
= dconstthird
;
6249 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
6250 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
6251 tree_root
= build_real (type
, dconstroot
);
6252 arglist
= tree_cons (NULL_TREE
, arg0
,
6253 build_tree_list (NULL_TREE
, tree_root
));
6254 return build_function_call_expr (powfn
, arglist
);
6261 /* Fold function call to builtin sin, sinf, or sinl. Return
6262 NULL_TREE if no simplification can be made. */
6264 fold_builtin_sin (tree arglist
)
6266 tree arg
= TREE_VALUE (arglist
);
6268 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6271 /* Optimize sin (0.0) = 0.0. */
6272 if (real_zerop (arg
))
6278 /* Fold function call to builtin cos, cosf, or cosl. Return
6279 NULL_TREE if no simplification can be made. */
6281 fold_builtin_cos (tree arglist
, tree type
, tree fndecl
)
6283 tree arg
= TREE_VALUE (arglist
);
6285 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6288 /* Optimize cos (0.0) = 1.0. */
6289 if (real_zerop (arg
))
6290 return build_real (type
, dconst1
);
6292 /* Optimize cos(-x) into cos (x). */
6293 if (TREE_CODE (arg
) == NEGATE_EXPR
)
6295 tree args
= build_tree_list (NULL_TREE
,
6296 TREE_OPERAND (arg
, 0));
6297 return build_function_call_expr (fndecl
, args
);
6303 /* Fold function call to builtin tan, tanf, or tanl. Return
6304 NULL_TREE if no simplification can be made. */
6306 fold_builtin_tan (tree arglist
)
6308 enum built_in_function fcode
;
6309 tree arg
= TREE_VALUE (arglist
);
6311 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6314 /* Optimize tan(0.0) = 0.0. */
6315 if (real_zerop (arg
))
6318 /* Optimize tan(atan(x)) = x. */
6319 fcode
= builtin_mathfn_code (arg
);
6320 if (flag_unsafe_math_optimizations
6321 && (fcode
== BUILT_IN_ATAN
6322 || fcode
== BUILT_IN_ATANF
6323 || fcode
== BUILT_IN_ATANL
))
6324 return TREE_VALUE (TREE_OPERAND (arg
, 1));
6329 /* Fold function call to builtin atan, atanf, or atanl. Return
6330 NULL_TREE if no simplification can be made. */
6333 fold_builtin_atan (tree arglist
, tree type
)
6336 tree arg
= TREE_VALUE (arglist
);
6338 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6341 /* Optimize atan(0.0) = 0.0. */
6342 if (real_zerop (arg
))
6345 /* Optimize atan(1.0) = pi/4. */
6346 if (real_onep (arg
))
6348 REAL_VALUE_TYPE cst
;
6350 real_convert (&cst
, TYPE_MODE (type
), &dconstpi
);
6351 SET_REAL_EXP (&cst
, REAL_EXP (&cst
) - 2);
6352 return build_real (type
, cst
);
6358 /* Fold function call to builtin trunc, truncf or truncl. Return
6359 NULL_TREE if no simplification can be made. */
6362 fold_builtin_trunc (tree exp
)
6364 tree arglist
= TREE_OPERAND (exp
, 1);
6367 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6370 /* Optimize trunc of constant value. */
6371 arg
= TREE_VALUE (arglist
);
6372 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
6374 REAL_VALUE_TYPE r
, x
;
6375 tree type
= TREE_TYPE (exp
);
6377 x
= TREE_REAL_CST (arg
);
6378 real_trunc (&r
, TYPE_MODE (type
), &x
);
6379 return build_real (type
, r
);
6382 return fold_trunc_transparent_mathfn (exp
);
6385 /* Fold function call to builtin floor, floorf or floorl. Return
6386 NULL_TREE if no simplification can be made. */
6389 fold_builtin_floor (tree exp
)
6391 tree arglist
= TREE_OPERAND (exp
, 1);
6394 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6397 /* Optimize floor of constant value. */
6398 arg
= TREE_VALUE (arglist
);
6399 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
6403 x
= TREE_REAL_CST (arg
);
6404 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
6406 tree type
= TREE_TYPE (exp
);
6409 real_floor (&r
, TYPE_MODE (type
), &x
);
6410 return build_real (type
, r
);
6414 return fold_trunc_transparent_mathfn (exp
);
6417 /* Fold function call to builtin ceil, ceilf or ceill. Return
6418 NULL_TREE if no simplification can be made. */
6421 fold_builtin_ceil (tree exp
)
6423 tree arglist
= TREE_OPERAND (exp
, 1);
6426 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6429 /* Optimize ceil of constant value. */
6430 arg
= TREE_VALUE (arglist
);
6431 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
6435 x
= TREE_REAL_CST (arg
);
6436 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
6438 tree type
= TREE_TYPE (exp
);
6441 real_ceil (&r
, TYPE_MODE (type
), &x
);
6442 return build_real (type
, r
);
6446 return fold_trunc_transparent_mathfn (exp
);
6449 /* Fold function call to builtin round, roundf or roundl. Return
6450 NULL_TREE if no simplification can be made. */
6453 fold_builtin_round (tree exp
)
6455 tree arglist
= TREE_OPERAND (exp
, 1);
6458 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6461 /* Optimize round of constant value. */
6462 arg
= TREE_VALUE (arglist
);
6463 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
6467 x
= TREE_REAL_CST (arg
);
6468 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
6470 tree type
= TREE_TYPE (exp
);
6473 real_round (&r
, TYPE_MODE (type
), &x
);
6474 return build_real (type
, r
);
6478 return fold_trunc_transparent_mathfn (exp
);
6481 /* Fold function call to builtin lround, lroundf or lroundl (or the
6482 corresponding long long versions). Return NULL_TREE if no
6483 simplification can be made. */
6486 fold_builtin_lround (tree exp
)
6488 tree arglist
= TREE_OPERAND (exp
, 1);
6491 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6494 /* Optimize lround of constant value. */
6495 arg
= TREE_VALUE (arglist
);
6496 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
6498 const REAL_VALUE_TYPE x
= TREE_REAL_CST (arg
);
6500 if (! REAL_VALUE_ISNAN (x
) && ! REAL_VALUE_ISINF (x
))
6502 tree itype
= TREE_TYPE (exp
), ftype
= TREE_TYPE (arg
), result
;
6503 HOST_WIDE_INT hi
, lo
;
6506 real_round (&r
, TYPE_MODE (ftype
), &x
);
6507 REAL_VALUE_TO_INT (&lo
, &hi
, r
);
6508 result
= build_int_cst_wide (NULL_TREE
, lo
, hi
);
6509 if (int_fits_type_p (result
, itype
))
6510 return fold_convert (itype
, result
);
6514 return fold_fixed_mathfn (exp
);
6517 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
6518 and their long and long long variants (i.e. ffsl and ffsll).
6519 Return NULL_TREE if no simplification can be made. */
6522 fold_builtin_bitop (tree exp
)
6524 tree fndecl
= get_callee_fndecl (exp
);
6525 tree arglist
= TREE_OPERAND (exp
, 1);
6528 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
6531 /* Optimize for constant argument. */
6532 arg
= TREE_VALUE (arglist
);
6533 if (TREE_CODE (arg
) == INTEGER_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
6535 HOST_WIDE_INT hi
, width
, result
;
6536 unsigned HOST_WIDE_INT lo
;
6539 type
= TREE_TYPE (arg
);
6540 width
= TYPE_PRECISION (type
);
6541 lo
= TREE_INT_CST_LOW (arg
);
6543 /* Clear all the bits that are beyond the type's precision. */
6544 if (width
> HOST_BITS_PER_WIDE_INT
)
6546 hi
= TREE_INT_CST_HIGH (arg
);
6547 if (width
< 2 * HOST_BITS_PER_WIDE_INT
)
6548 hi
&= ~((HOST_WIDE_INT
) (-1) >> (width
- HOST_BITS_PER_WIDE_INT
));
6553 if (width
< HOST_BITS_PER_WIDE_INT
)
6554 lo
&= ~((unsigned HOST_WIDE_INT
) (-1) << width
);
6557 switch (DECL_FUNCTION_CODE (fndecl
))
6561 case BUILT_IN_FFSLL
:
6563 result
= exact_log2 (lo
& -lo
) + 1;
6565 result
= HOST_BITS_PER_WIDE_INT
+ exact_log2 (hi
& -hi
) + 1;
6572 case BUILT_IN_CLZLL
:
6574 result
= width
- floor_log2 (hi
) - 1 - HOST_BITS_PER_WIDE_INT
;
6576 result
= width
- floor_log2 (lo
) - 1;
6577 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
6583 case BUILT_IN_CTZLL
:
6585 result
= exact_log2 (lo
& -lo
);
6587 result
= HOST_BITS_PER_WIDE_INT
+ exact_log2 (hi
& -hi
);
6588 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
6592 case BUILT_IN_POPCOUNT
:
6593 case BUILT_IN_POPCOUNTL
:
6594 case BUILT_IN_POPCOUNTLL
:
6597 result
++, lo
&= lo
- 1;
6599 result
++, hi
&= hi
- 1;
6602 case BUILT_IN_PARITY
:
6603 case BUILT_IN_PARITYL
:
6604 case BUILT_IN_PARITYLL
:
6607 result
++, lo
&= lo
- 1;
6609 result
++, hi
&= hi
- 1;
6617 return build_int_cst (TREE_TYPE (exp
), result
);
6623 /* Return true if EXPR is the real constant contained in VALUE. */
6626 real_dconstp (tree expr
, const REAL_VALUE_TYPE
*value
)
6630 return ((TREE_CODE (expr
) == REAL_CST
6631 && ! TREE_CONSTANT_OVERFLOW (expr
)
6632 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr
), *value
))
6633 || (TREE_CODE (expr
) == COMPLEX_CST
6634 && real_dconstp (TREE_REALPART (expr
), value
)
6635 && real_zerop (TREE_IMAGPART (expr
))));
6638 /* A subroutine of fold_builtin to fold the various logarithmic
6639 functions. EXP is the CALL_EXPR of a call to a builtin logN
6640 function. VALUE is the base of the logN function. */
6643 fold_builtin_logarithm (tree exp
, const REAL_VALUE_TYPE
*value
)
6645 tree arglist
= TREE_OPERAND (exp
, 1);
6647 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6649 tree fndecl
= get_callee_fndecl (exp
);
6650 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
6651 tree arg
= TREE_VALUE (arglist
);
6652 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
6654 /* Optimize logN(1.0) = 0.0. */
6655 if (real_onep (arg
))
6656 return build_real (type
, dconst0
);
6658 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
6659 exactly, then only do this if flag_unsafe_math_optimizations. */
6660 if (exact_real_truncate (TYPE_MODE (type
), value
)
6661 || flag_unsafe_math_optimizations
)
6663 const REAL_VALUE_TYPE value_truncate
=
6664 real_value_truncate (TYPE_MODE (type
), *value
);
6665 if (real_dconstp (arg
, &value_truncate
))
6666 return build_real (type
, dconst1
);
6669 /* Special case, optimize logN(expN(x)) = x. */
6670 if (flag_unsafe_math_optimizations
6671 && ((value
== &dconste
6672 && (fcode
== BUILT_IN_EXP
6673 || fcode
== BUILT_IN_EXPF
6674 || fcode
== BUILT_IN_EXPL
))
6675 || (value
== &dconst2
6676 && (fcode
== BUILT_IN_EXP2
6677 || fcode
== BUILT_IN_EXP2F
6678 || fcode
== BUILT_IN_EXP2L
))
6679 || (value
== &dconst10
&& (BUILTIN_EXP10_P (fcode
)))))
6680 return fold_convert (type
, TREE_VALUE (TREE_OPERAND (arg
, 1)));
6682 /* Optimize logN(func()) for various exponential functions. We
6683 want to determine the value "x" and the power "exponent" in
6684 order to transform logN(x**exponent) into exponent*logN(x). */
6685 if (flag_unsafe_math_optimizations
)
6687 tree exponent
= 0, x
= 0;
6694 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
6695 x
= build_real (type
,
6696 real_value_truncate (TYPE_MODE (type
), dconste
));
6697 exponent
= TREE_VALUE (TREE_OPERAND (arg
, 1));
6700 case BUILT_IN_EXP2F
:
6701 case BUILT_IN_EXP2L
:
6702 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
6703 x
= build_real (type
, dconst2
);
6704 exponent
= TREE_VALUE (TREE_OPERAND (arg
, 1));
6706 case BUILT_IN_EXP10
:
6707 case BUILT_IN_EXP10F
:
6708 case BUILT_IN_EXP10L
:
6709 case BUILT_IN_POW10
:
6710 case BUILT_IN_POW10F
:
6711 case BUILT_IN_POW10L
:
6712 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
6713 x
= build_real (type
, dconst10
);
6714 exponent
= TREE_VALUE (TREE_OPERAND (arg
, 1));
6717 case BUILT_IN_SQRTF
:
6718 case BUILT_IN_SQRTL
:
6719 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
6720 x
= TREE_VALUE (TREE_OPERAND (arg
, 1));
6721 exponent
= build_real (type
, dconsthalf
);
6724 case BUILT_IN_CBRTF
:
6725 case BUILT_IN_CBRTL
:
6726 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
6727 x
= TREE_VALUE (TREE_OPERAND (arg
, 1));
6728 exponent
= build_real (type
, real_value_truncate (TYPE_MODE (type
),
6734 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
6735 x
= TREE_VALUE (TREE_OPERAND (arg
, 1));
6736 exponent
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg
, 1)));
6742 /* Now perform the optimization. */
6746 arglist
= build_tree_list (NULL_TREE
, x
);
6747 logfn
= build_function_call_expr (fndecl
, arglist
);
6748 return fold (build2 (MULT_EXPR
, type
, exponent
, logfn
));
6756 /* Fold a builtin function call to pow, powf, or powl. Return
6757 NULL_TREE if no simplification can be made. */
6759 fold_builtin_pow (tree fndecl
, tree arglist
, tree type
)
6761 enum built_in_function fcode
;
6762 tree arg0
= TREE_VALUE (arglist
);
6763 tree arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
6765 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
6768 /* Optimize pow(1.0,y) = 1.0. */
6769 if (real_onep (arg0
))
6770 return omit_one_operand (type
, build_real (type
, dconst1
), arg1
);
6772 if (TREE_CODE (arg1
) == REAL_CST
6773 && ! TREE_CONSTANT_OVERFLOW (arg1
))
6776 c
= TREE_REAL_CST (arg1
);
6778 /* Optimize pow(x,0.0) = 1.0. */
6779 if (REAL_VALUES_EQUAL (c
, dconst0
))
6780 return omit_one_operand (type
, build_real (type
, dconst1
),
6783 /* Optimize pow(x,1.0) = x. */
6784 if (REAL_VALUES_EQUAL (c
, dconst1
))
6787 /* Optimize pow(x,-1.0) = 1.0/x. */
6788 if (REAL_VALUES_EQUAL (c
, dconstm1
))
6789 return fold (build2 (RDIV_EXPR
, type
,
6790 build_real (type
, dconst1
), arg0
));
6792 /* Optimize pow(x,0.5) = sqrt(x). */
6793 if (flag_unsafe_math_optimizations
6794 && REAL_VALUES_EQUAL (c
, dconsthalf
))
6796 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
6798 if (sqrtfn
!= NULL_TREE
)
6800 tree arglist
= build_tree_list (NULL_TREE
, arg0
);
6801 return build_function_call_expr (sqrtfn
, arglist
);
6805 /* Attempt to evaluate pow at compile-time. */
6806 if (TREE_CODE (arg0
) == REAL_CST
6807 && ! TREE_CONSTANT_OVERFLOW (arg0
))
6809 REAL_VALUE_TYPE cint
;
6812 n
= real_to_integer (&c
);
6813 real_from_integer (&cint
, VOIDmode
, n
,
6815 if (real_identical (&c
, &cint
))
6820 x
= TREE_REAL_CST (arg0
);
6821 inexact
= real_powi (&x
, TYPE_MODE (type
), &x
, n
);
6822 if (flag_unsafe_math_optimizations
|| !inexact
)
6823 return build_real (type
, x
);
6828 /* Optimize pow(expN(x),y) = expN(x*y). */
6829 fcode
= builtin_mathfn_code (arg0
);
6830 if (flag_unsafe_math_optimizations
&& BUILTIN_EXPONENT_P (fcode
))
6832 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
6833 tree arg
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
6834 arg
= fold (build2 (MULT_EXPR
, type
, arg
, arg1
));
6835 arglist
= build_tree_list (NULL_TREE
, arg
);
6836 return build_function_call_expr (expfn
, arglist
);
6839 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
6840 if (flag_unsafe_math_optimizations
&& BUILTIN_SQRT_P (fcode
))
6842 tree narg0
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
6843 tree narg1
= fold (build2 (MULT_EXPR
, type
, arg1
,
6844 build_real (type
, dconsthalf
)));
6846 arglist
= tree_cons (NULL_TREE
, narg0
,
6847 build_tree_list (NULL_TREE
, narg1
));
6848 return build_function_call_expr (fndecl
, arglist
);
6851 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
6852 if (flag_unsafe_math_optimizations
6853 && (fcode
== BUILT_IN_POW
6854 || fcode
== BUILT_IN_POWF
6855 || fcode
== BUILT_IN_POWL
))
6857 tree arg00
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
6858 tree arg01
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0
, 1)));
6859 tree narg1
= fold (build2 (MULT_EXPR
, type
, arg01
, arg1
));
6860 arglist
= tree_cons (NULL_TREE
, arg00
,
6861 build_tree_list (NULL_TREE
, narg1
));
6862 return build_function_call_expr (fndecl
, arglist
);
6867 /* A subroutine of fold_builtin to fold the various exponent
6868 functions. EXP is the CALL_EXPR of a call to a builtin function.
6869 VALUE is the value which will be raised to a power. */
6872 fold_builtin_exponent (tree exp
, const REAL_VALUE_TYPE
*value
)
6874 tree arglist
= TREE_OPERAND (exp
, 1);
6876 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6878 tree fndecl
= get_callee_fndecl (exp
);
6879 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
6880 tree arg
= TREE_VALUE (arglist
);
6882 /* Optimize exp*(0.0) = 1.0. */
6883 if (real_zerop (arg
))
6884 return build_real (type
, dconst1
);
6886 /* Optimize expN(1.0) = N. */
6887 if (real_onep (arg
))
6889 REAL_VALUE_TYPE cst
;
6891 real_convert (&cst
, TYPE_MODE (type
), value
);
6892 return build_real (type
, cst
);
6895 /* Attempt to evaluate expN(integer) at compile-time. */
6896 if (flag_unsafe_math_optimizations
6897 && TREE_CODE (arg
) == REAL_CST
6898 && ! TREE_CONSTANT_OVERFLOW (arg
))
6900 REAL_VALUE_TYPE cint
;
6904 c
= TREE_REAL_CST (arg
);
6905 n
= real_to_integer (&c
);
6906 real_from_integer (&cint
, VOIDmode
, n
,
6908 if (real_identical (&c
, &cint
))
6912 real_powi (&x
, TYPE_MODE (type
), value
, n
);
6913 return build_real (type
, x
);
6917 /* Optimize expN(logN(x)) = x. */
6918 if (flag_unsafe_math_optimizations
)
6920 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
6922 if ((value
== &dconste
6923 && (fcode
== BUILT_IN_LOG
6924 || fcode
== BUILT_IN_LOGF
6925 || fcode
== BUILT_IN_LOGL
))
6926 || (value
== &dconst2
6927 && (fcode
== BUILT_IN_LOG2
6928 || fcode
== BUILT_IN_LOG2F
6929 || fcode
== BUILT_IN_LOG2L
))
6930 || (value
== &dconst10
6931 && (fcode
== BUILT_IN_LOG10
6932 || fcode
== BUILT_IN_LOG10F
6933 || fcode
== BUILT_IN_LOG10L
)))
6934 return fold_convert (type
, TREE_VALUE (TREE_OPERAND (arg
, 1)));
6941 /* Fold function call to builtin memcpy. Return
6942 NULL_TREE if no simplification can be made. */
6945 fold_builtin_memcpy (tree exp
)
6947 tree arglist
= TREE_OPERAND (exp
, 1);
6948 tree dest
, src
, len
;
6950 if (!validate_arglist (arglist
,
6951 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6954 dest
= TREE_VALUE (arglist
);
6955 src
= TREE_VALUE (TREE_CHAIN (arglist
));
6956 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
6958 /* If the LEN parameter is zero, return DEST. */
6959 if (integer_zerop (len
))
6960 return omit_one_operand (TREE_TYPE (exp
), dest
, src
);
6962 /* If SRC and DEST are the same (and not volatile), return DEST. */
6963 if (operand_equal_p (src
, dest
, 0))
6964 return omit_one_operand (TREE_TYPE (exp
), dest
, len
);
6969 /* Fold function call to builtin mempcpy. Return
6970 NULL_TREE if no simplification can be made. */
6973 fold_builtin_mempcpy (tree arglist
, tree type
, int endp
)
6975 if (validate_arglist (arglist
,
6976 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6978 tree dest
= TREE_VALUE (arglist
);
6979 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
6980 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
6982 /* If the LEN parameter is zero, return DEST. */
6983 if (integer_zerop (len
))
6984 return omit_one_operand (type
, dest
, src
);
6986 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
6987 if (operand_equal_p (src
, dest
, 0))
6990 return omit_one_operand (type
, dest
, len
);
6993 len
= fold (build2 (MINUS_EXPR
, TREE_TYPE (len
), len
,
6996 len
= fold_convert (TREE_TYPE (dest
), len
);
6997 len
= fold (build2 (PLUS_EXPR
, TREE_TYPE (dest
), dest
, len
));
6998 return fold_convert (type
, len
);
7004 /* Fold function call to builtin memmove. Return
7005 NULL_TREE if no simplification can be made. */
7008 fold_builtin_memmove (tree arglist
, tree type
)
7010 tree dest
, src
, len
;
7012 if (!validate_arglist (arglist
,
7013 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7016 dest
= TREE_VALUE (arglist
);
7017 src
= TREE_VALUE (TREE_CHAIN (arglist
));
7018 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
7020 /* If the LEN parameter is zero, return DEST. */
7021 if (integer_zerop (len
))
7022 return omit_one_operand (type
, dest
, src
);
7024 /* If SRC and DEST are the same (and not volatile), return DEST. */
7025 if (operand_equal_p (src
, dest
, 0))
7026 return omit_one_operand (type
, dest
, len
);
7031 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
7032 the length of the string to be copied. Return NULL_TREE if no
7033 simplification can be made. */
7036 fold_builtin_strcpy (tree exp
, tree len
)
7038 tree arglist
= TREE_OPERAND (exp
, 1);
7041 if (!validate_arglist (arglist
,
7042 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
7045 dest
= TREE_VALUE (arglist
);
7046 src
= TREE_VALUE (TREE_CHAIN (arglist
));
7048 /* If SRC and DEST are the same (and not volatile), return DEST. */
7049 if (operand_equal_p (src
, dest
, 0))
7050 return fold_convert (TREE_TYPE (exp
), dest
);
7055 fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
7061 len
= c_strlen (src
, 1);
7062 if (! len
|| TREE_SIDE_EFFECTS (len
))
7066 len
= size_binop (PLUS_EXPR
, len
, ssize_int (1));
7067 arglist
= build_tree_list (NULL_TREE
, len
);
7068 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
7069 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
7070 return fold_convert (TREE_TYPE (exp
),
7071 build_function_call_expr (fn
, arglist
));
7074 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
7075 the length of the source string. Return NULL_TREE if no simplification
7079 fold_builtin_strncpy (tree exp
, tree slen
)
7081 tree arglist
= TREE_OPERAND (exp
, 1);
7082 tree dest
, src
, len
, fn
;
7084 if (!validate_arglist (arglist
,
7085 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7088 dest
= TREE_VALUE (arglist
);
7089 src
= TREE_VALUE (TREE_CHAIN (arglist
));
7090 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
7092 /* If the LEN parameter is zero, return DEST. */
7093 if (integer_zerop (len
))
7094 return omit_one_operand (TREE_TYPE (exp
), dest
, src
);
7096 /* We can't compare slen with len as constants below if len is not a
7098 if (len
== 0 || TREE_CODE (len
) != INTEGER_CST
)
7102 slen
= c_strlen (src
, 1);
7104 /* Now, we must be passed a constant src ptr parameter. */
7105 if (slen
== 0 || TREE_CODE (slen
) != INTEGER_CST
)
7108 slen
= size_binop (PLUS_EXPR
, slen
, ssize_int (1));
7110 /* We do not support simplification of this case, though we do
7111 support it when expanding trees into RTL. */
7112 /* FIXME: generate a call to __builtin_memset. */
7113 if (tree_int_cst_lt (slen
, len
))
7116 /* OK transform into builtin memcpy. */
7117 fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
7120 return fold_convert (TREE_TYPE (exp
),
7121 build_function_call_expr (fn
, arglist
));
7124 /* Fold function call to builtin memcmp. Return
7125 NULL_TREE if no simplification can be made. */
7128 fold_builtin_memcmp (tree arglist
)
7130 tree arg1
, arg2
, len
;
7131 const char *p1
, *p2
;
7133 if (!validate_arglist (arglist
,
7134 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7137 arg1
= TREE_VALUE (arglist
);
7138 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
7139 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
7141 /* If the LEN parameter is zero, return zero. */
7142 if (integer_zerop (len
))
7143 return omit_two_operands (integer_type_node
, integer_zero_node
,
7146 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7147 if (operand_equal_p (arg1
, arg2
, 0))
7148 return omit_one_operand (integer_type_node
, integer_zero_node
, len
);
7150 p1
= c_getstr (arg1
);
7151 p2
= c_getstr (arg2
);
7153 /* If all arguments are constant, and the value of len is not greater
7154 than the lengths of arg1 and arg2, evaluate at compile-time. */
7155 if (host_integerp (len
, 1) && p1
&& p2
7156 && compare_tree_int (len
, strlen (p1
) + 1) <= 0
7157 && compare_tree_int (len
, strlen (p2
) + 1) <= 0)
7159 const int r
= memcmp (p1
, p2
, tree_low_cst (len
, 1));
7162 return integer_one_node
;
7164 return integer_minus_one_node
;
7166 return integer_zero_node
;
7169 /* If len parameter is one, return an expression corresponding to
7170 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7171 if (host_integerp (len
, 1) && tree_low_cst (len
, 1) == 1)
7173 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7174 tree cst_uchar_ptr_node
= build_pointer_type (cst_uchar_node
);
7175 tree ind1
= fold_convert (integer_type_node
,
7176 build1 (INDIRECT_REF
, cst_uchar_node
,
7177 fold_convert (cst_uchar_ptr_node
,
7179 tree ind2
= fold_convert (integer_type_node
,
7180 build1 (INDIRECT_REF
, cst_uchar_node
,
7181 fold_convert (cst_uchar_ptr_node
,
7183 return fold (build2 (MINUS_EXPR
, integer_type_node
, ind1
, ind2
));
7189 /* Fold function call to builtin strcmp. Return
7190 NULL_TREE if no simplification can be made. */
7193 fold_builtin_strcmp (tree arglist
)
7196 const char *p1
, *p2
;
7198 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
7201 arg1
= TREE_VALUE (arglist
);
7202 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
7204 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7205 if (operand_equal_p (arg1
, arg2
, 0))
7206 return integer_zero_node
;
7208 p1
= c_getstr (arg1
);
7209 p2
= c_getstr (arg2
);
7213 const int i
= strcmp (p1
, p2
);
7215 return integer_minus_one_node
;
7217 return integer_one_node
;
7219 return integer_zero_node
;
7222 /* If the second arg is "", return *(const unsigned char*)arg1. */
7223 if (p2
&& *p2
== '\0')
7225 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7226 tree cst_uchar_ptr_node
= build_pointer_type (cst_uchar_node
);
7227 return fold_convert (integer_type_node
,
7228 build1 (INDIRECT_REF
, cst_uchar_node
,
7229 fold_convert (cst_uchar_ptr_node
,
7233 /* If the first arg is "", return -*(const unsigned char*)arg2. */
7234 if (p1
&& *p1
== '\0')
7236 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7237 tree cst_uchar_ptr_node
= build_pointer_type (cst_uchar_node
);
7238 tree temp
= fold_convert (integer_type_node
,
7239 build1 (INDIRECT_REF
, cst_uchar_node
,
7240 fold_convert (cst_uchar_ptr_node
,
7242 return fold (build1 (NEGATE_EXPR
, integer_type_node
, temp
));
7248 /* Fold function call to builtin strncmp. Return
7249 NULL_TREE if no simplification can be made. */
7252 fold_builtin_strncmp (tree arglist
)
7254 tree arg1
, arg2
, len
;
7255 const char *p1
, *p2
;
7257 if (!validate_arglist (arglist
,
7258 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7261 arg1
= TREE_VALUE (arglist
);
7262 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
7263 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
7265 /* If the LEN parameter is zero, return zero. */
7266 if (integer_zerop (len
))
7267 return omit_two_operands (integer_type_node
, integer_zero_node
,
7270 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7271 if (operand_equal_p (arg1
, arg2
, 0))
7272 return omit_one_operand (integer_type_node
, integer_zero_node
, len
);
7274 p1
= c_getstr (arg1
);
7275 p2
= c_getstr (arg2
);
7277 if (host_integerp (len
, 1) && p1
&& p2
)
7279 const int i
= strncmp (p1
, p2
, tree_low_cst (len
, 1));
7281 return integer_one_node
;
7283 return integer_minus_one_node
;
7285 return integer_zero_node
;
7288 /* If the second arg is "", and the length is greater than zero,
7289 return *(const unsigned char*)arg1. */
7290 if (p2
&& *p2
== '\0'
7291 && TREE_CODE (len
) == INTEGER_CST
7292 && tree_int_cst_sgn (len
) == 1)
7294 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7295 tree cst_uchar_ptr_node
= build_pointer_type (cst_uchar_node
);
7296 return fold_convert (integer_type_node
,
7297 build1 (INDIRECT_REF
, cst_uchar_node
,
7298 fold_convert (cst_uchar_ptr_node
,
7302 /* If the first arg is "", and the length is greater than zero,
7303 return -*(const unsigned char*)arg2. */
7304 if (p1
&& *p1
== '\0'
7305 && TREE_CODE (len
) == INTEGER_CST
7306 && tree_int_cst_sgn (len
) == 1)
7308 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7309 tree cst_uchar_ptr_node
= build_pointer_type (cst_uchar_node
);
7310 tree temp
= fold_convert (integer_type_node
,
7311 build1 (INDIRECT_REF
, cst_uchar_node
,
7312 fold_convert (cst_uchar_ptr_node
,
7314 return fold (build1 (NEGATE_EXPR
, integer_type_node
, temp
));
7317 /* If len parameter is one, return an expression corresponding to
7318 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7319 if (host_integerp (len
, 1) && tree_low_cst (len
, 1) == 1)
7321 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7322 tree cst_uchar_ptr_node
= build_pointer_type (cst_uchar_node
);
7323 tree ind1
= fold_convert (integer_type_node
,
7324 build1 (INDIRECT_REF
, cst_uchar_node
,
7325 fold_convert (cst_uchar_ptr_node
,
7327 tree ind2
= fold_convert (integer_type_node
,
7328 build1 (INDIRECT_REF
, cst_uchar_node
,
7329 fold_convert (cst_uchar_ptr_node
,
7331 return fold (build2 (MINUS_EXPR
, integer_type_node
, ind1
, ind2
));
7337 /* Fold function call to builtin signbit, signbitf or signbitl. Return
7338 NULL_TREE if no simplification can be made. */
7341 fold_builtin_signbit (tree exp
)
7343 tree arglist
= TREE_OPERAND (exp
, 1);
7346 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7349 arg
= TREE_VALUE (arglist
);
7351 /* If ARG is a compile-time constant, determine the result. */
7352 if (TREE_CODE (arg
) == REAL_CST
7353 && !TREE_CONSTANT_OVERFLOW (arg
))
7357 c
= TREE_REAL_CST (arg
);
7358 temp
= REAL_VALUE_NEGATIVE (c
) ? integer_one_node
: integer_zero_node
;
7359 return fold_convert (TREE_TYPE (exp
), temp
);
7362 /* If ARG is non-negative, the result is always zero. */
7363 if (tree_expr_nonnegative_p (arg
))
7364 return omit_one_operand (TREE_TYPE (exp
), integer_zero_node
, arg
);
7366 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
7367 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg
))))
7368 return fold (build2 (LT_EXPR
, TREE_TYPE (exp
), arg
,
7369 build_real (TREE_TYPE (arg
), dconst0
)));
7374 /* Fold function call to builtin copysign, copysignf or copysignl.
7375 Return NULL_TREE if no simplification can be made. */
7378 fold_builtin_copysign (tree arglist
, tree type
)
7382 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
7385 arg1
= TREE_VALUE (arglist
);
7386 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
7388 /* copysign(X,X) is X. */
7389 if (operand_equal_p (arg1
, arg2
, 0))
7390 return fold_convert (type
, arg1
);
7392 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
7393 if (TREE_CODE (arg1
) == REAL_CST
7394 && TREE_CODE (arg2
) == REAL_CST
7395 && !TREE_CONSTANT_OVERFLOW (arg1
)
7396 && !TREE_CONSTANT_OVERFLOW (arg2
))
7398 REAL_VALUE_TYPE c1
, c2
;
7400 c1
= TREE_REAL_CST (arg1
);
7401 c2
= TREE_REAL_CST (arg2
);
7402 real_copysign (&c1
, &c2
);
7403 return build_real (type
, c1
);
7407 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
7408 Remember to evaluate Y for side-effects. */
7409 if (tree_expr_nonnegative_p (arg2
))
7410 return omit_one_operand (type
,
7411 fold (build1 (ABS_EXPR
, type
, arg1
)),
7417 /* Fold a call to builtin isascii. */
7420 fold_builtin_isascii (tree arglist
)
7422 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
7426 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7427 tree arg
= TREE_VALUE (arglist
);
7429 arg
= build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
7430 build_int_cst (NULL_TREE
,
7431 ~ (unsigned HOST_WIDE_INT
) 0x7f));
7432 arg
= fold (build2 (EQ_EXPR
, integer_type_node
,
7433 arg
, integer_zero_node
));
7435 if (in_gimple_form
&& !TREE_CONSTANT (arg
))
7442 /* Fold a call to builtin toascii. */
7445 fold_builtin_toascii (tree arglist
)
7447 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
7451 /* Transform toascii(c) -> (c & 0x7f). */
7452 tree arg
= TREE_VALUE (arglist
);
7454 return fold (build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
7455 build_int_cst (NULL_TREE
, 0x7f)));
7459 /* Fold a call to builtin isdigit. */
7462 fold_builtin_isdigit (tree arglist
)
7464 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
7468 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7469 /* According to the C standard, isdigit is unaffected by locale. */
7470 tree arg
= TREE_VALUE (arglist
);
7471 arg
= fold_convert (unsigned_type_node
, arg
);
7472 arg
= build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
7473 build_int_cst (unsigned_type_node
, TARGET_DIGIT0
));
7474 arg
= build2 (LE_EXPR
, integer_type_node
, arg
,
7475 build_int_cst (unsigned_type_node
, 9));
7477 if (in_gimple_form
&& !TREE_CONSTANT (arg
))
7484 /* Fold a call to fabs, fabsf or fabsl. */
7487 fold_builtin_fabs (tree arglist
, tree type
)
7491 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7494 arg
= TREE_VALUE (arglist
);
7495 arg
= fold_convert (type
, arg
);
7496 if (TREE_CODE (arg
) == REAL_CST
)
7497 return fold_abs_const (arg
, type
);
7498 return fold (build1 (ABS_EXPR
, type
, arg
));
7501 /* Fold a call to abs, labs, llabs or imaxabs. */
7504 fold_builtin_abs (tree arglist
, tree type
)
7508 if (!validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
7511 arg
= TREE_VALUE (arglist
);
7512 arg
= fold_convert (type
, arg
);
7513 if (TREE_CODE (arg
) == INTEGER_CST
)
7514 return fold_abs_const (arg
, type
);
7515 return fold (build1 (ABS_EXPR
, type
, arg
));
7518 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
7519 EXP is the CALL_EXPR for the call. */
7522 fold_builtin_classify (tree exp
, int builtin_index
)
7524 tree fndecl
= get_callee_fndecl (exp
);
7525 tree arglist
= TREE_OPERAND (exp
, 1);
7526 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7530 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7532 /* Check that we have exactly one argument. */
7535 error ("too few arguments to function %qs",
7536 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
7537 return error_mark_node
;
7539 else if (TREE_CHAIN (arglist
) != 0)
7541 error ("too many arguments to function %qs",
7542 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
7543 return error_mark_node
;
7547 error ("non-floating-point argument to function %qs",
7548 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
7549 return error_mark_node
;
7553 arg
= TREE_VALUE (arglist
);
7554 switch (builtin_index
)
7556 case BUILT_IN_ISINF
:
7557 if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
7558 return omit_one_operand (type
, integer_zero_node
, arg
);
7560 if (TREE_CODE (arg
) == REAL_CST
)
7562 r
= TREE_REAL_CST (arg
);
7563 if (real_isinf (&r
))
7564 return real_compare (GT_EXPR
, &r
, &dconst0
)
7565 ? integer_one_node
: integer_minus_one_node
;
7567 return integer_zero_node
;
7572 case BUILT_IN_FINITE
:
7573 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg
)))
7574 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
7575 return omit_one_operand (type
, integer_zero_node
, arg
);
7577 if (TREE_CODE (arg
) == REAL_CST
)
7579 r
= TREE_REAL_CST (arg
);
7580 return real_isinf (&r
) || real_isnan (&r
)
7581 ? integer_zero_node
: integer_one_node
;
7586 case BUILT_IN_ISNAN
:
7587 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg
))))
7588 return omit_one_operand (type
, integer_zero_node
, arg
);
7590 if (TREE_CODE (arg
) == REAL_CST
)
7592 r
= TREE_REAL_CST (arg
);
7593 return real_isnan (&r
) ? integer_one_node
: integer_zero_node
;
7596 arg
= builtin_save_expr (arg
);
7597 return fold (build2 (UNORDERED_EXPR
, type
, arg
, arg
));
7604 /* Fold a call to an unordered comparison function such as
7605 __builtin_isgreater(). EXP is the CALL_EXPR for the call.
7606 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
7607 the opposite of the desired result. UNORDERED_CODE is used
7608 for modes that can hold NaNs and ORDERED_CODE is used for
7612 fold_builtin_unordered_cmp (tree exp
,
7613 enum tree_code unordered_code
,
7614 enum tree_code ordered_code
)
7616 tree fndecl
= get_callee_fndecl (exp
);
7617 tree arglist
= TREE_OPERAND (exp
, 1);
7618 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7619 enum tree_code code
;
7622 enum tree_code code0
, code1
;
7623 tree cmp_type
= NULL_TREE
;
7625 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
7627 /* Check that we have exactly two arguments. */
7628 if (arglist
== 0 || TREE_CHAIN (arglist
) == 0)
7630 error ("too few arguments to function %qs",
7631 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
7632 return error_mark_node
;
7634 else if (TREE_CHAIN (TREE_CHAIN (arglist
)) != 0)
7636 error ("too many arguments to function %qs",
7637 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
7638 return error_mark_node
;
7642 arg0
= TREE_VALUE (arglist
);
7643 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
7645 type0
= TREE_TYPE (arg0
);
7646 type1
= TREE_TYPE (arg1
);
7648 code0
= TREE_CODE (type0
);
7649 code1
= TREE_CODE (type1
);
7651 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
7652 /* Choose the wider of two real types. */
7653 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
7655 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
7657 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
7661 error ("non-floating-point argument to function %qs",
7662 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
7663 return error_mark_node
;
7666 arg0
= fold_convert (cmp_type
, arg0
);
7667 arg1
= fold_convert (cmp_type
, arg1
);
7669 if (unordered_code
== UNORDERED_EXPR
)
7671 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
7672 return omit_two_operands (type
, integer_zero_node
, arg0
, arg1
);
7673 return fold (build2 (UNORDERED_EXPR
, type
, arg0
, arg1
));
7676 code
= MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0
))) ? unordered_code
7678 return fold (build1 (TRUTH_NOT_EXPR
, type
,
7679 fold (build2 (code
, type
, arg0
, arg1
))));
7682 /* Used by constant folding to simplify calls to builtin functions. EXP is
7683 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
7684 result of the function call is ignored. This function returns NULL_TREE
7685 if no simplification was possible. */
7688 fold_builtin_1 (tree exp
, bool ignore
)
7690 tree fndecl
= get_callee_fndecl (exp
);
7691 tree arglist
= TREE_OPERAND (exp
, 1);
7692 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7694 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
7695 return targetm
.fold_builtin (exp
, ignore
);
7697 switch (DECL_FUNCTION_CODE (fndecl
))
7699 case BUILT_IN_FPUTS
:
7700 return fold_builtin_fputs (arglist
, ignore
, false, NULL_TREE
);
7702 case BUILT_IN_FPUTS_UNLOCKED
:
7703 return fold_builtin_fputs (arglist
, ignore
, true, NULL_TREE
);
7705 case BUILT_IN_STRSTR
:
7706 return fold_builtin_strstr (arglist
);
7708 case BUILT_IN_STRCAT
:
7709 return fold_builtin_strcat (arglist
);
7711 case BUILT_IN_STRNCAT
:
7712 return fold_builtin_strncat (arglist
);
7714 case BUILT_IN_STRSPN
:
7715 return fold_builtin_strspn (arglist
);
7717 case BUILT_IN_STRCSPN
:
7718 return fold_builtin_strcspn (arglist
);
7720 case BUILT_IN_STRCHR
:
7721 case BUILT_IN_INDEX
:
7722 return fold_builtin_strchr (arglist
);
7724 case BUILT_IN_STRRCHR
:
7725 case BUILT_IN_RINDEX
:
7726 return fold_builtin_strrchr (arglist
);
7728 case BUILT_IN_STRCPY
:
7729 return fold_builtin_strcpy (exp
, NULL_TREE
);
7731 case BUILT_IN_STRNCPY
:
7732 return fold_builtin_strncpy (exp
, NULL_TREE
);
7734 case BUILT_IN_STRCMP
:
7735 return fold_builtin_strcmp (arglist
);
7737 case BUILT_IN_STRNCMP
:
7738 return fold_builtin_strncmp (arglist
);
7740 case BUILT_IN_STRPBRK
:
7741 return fold_builtin_strpbrk (arglist
);
7744 case BUILT_IN_MEMCMP
:
7745 return fold_builtin_memcmp (arglist
);
7747 case BUILT_IN_SPRINTF
:
7748 return fold_builtin_sprintf (arglist
, ignore
);
7750 case BUILT_IN_CONSTANT_P
:
7754 val
= fold_builtin_constant_p (arglist
);
7755 /* Gimplification will pull the CALL_EXPR for the builtin out of
7756 an if condition. When not optimizing, we'll not CSE it back.
7757 To avoid link error types of regressions, return false now. */
7758 if (!val
&& !optimize
)
7759 val
= integer_zero_node
;
7764 case BUILT_IN_EXPECT
:
7765 return fold_builtin_expect (arglist
);
7767 case BUILT_IN_CLASSIFY_TYPE
:
7768 return fold_builtin_classify_type (arglist
);
7770 case BUILT_IN_STRLEN
:
7771 return fold_builtin_strlen (arglist
);
7774 case BUILT_IN_FABSF
:
7775 case BUILT_IN_FABSL
:
7776 return fold_builtin_fabs (arglist
, type
);
7780 case BUILT_IN_LLABS
:
7781 case BUILT_IN_IMAXABS
:
7782 return fold_builtin_abs (arglist
, type
);
7785 case BUILT_IN_CONJF
:
7786 case BUILT_IN_CONJL
:
7787 if (validate_arglist (arglist
, COMPLEX_TYPE
, VOID_TYPE
))
7788 return fold (build1 (CONJ_EXPR
, type
, TREE_VALUE (arglist
)));
7791 case BUILT_IN_CREAL
:
7792 case BUILT_IN_CREALF
:
7793 case BUILT_IN_CREALL
:
7794 if (validate_arglist (arglist
, COMPLEX_TYPE
, VOID_TYPE
))
7795 return non_lvalue (fold (build1 (REALPART_EXPR
, type
,
7796 TREE_VALUE (arglist
))));
7799 case BUILT_IN_CIMAG
:
7800 case BUILT_IN_CIMAGF
:
7801 case BUILT_IN_CIMAGL
:
7802 if (validate_arglist (arglist
, COMPLEX_TYPE
, VOID_TYPE
))
7803 return non_lvalue (fold (build1 (IMAGPART_EXPR
, type
,
7804 TREE_VALUE (arglist
))));
7808 case BUILT_IN_CABSF
:
7809 case BUILT_IN_CABSL
:
7810 return fold_builtin_cabs (arglist
, type
);
7813 case BUILT_IN_SQRTF
:
7814 case BUILT_IN_SQRTL
:
7815 return fold_builtin_sqrt (arglist
, type
);
7818 case BUILT_IN_CBRTF
:
7819 case BUILT_IN_CBRTL
:
7820 return fold_builtin_cbrt (arglist
, type
);
7825 return fold_builtin_sin (arglist
);
7830 return fold_builtin_cos (arglist
, type
, fndecl
);
7835 return fold_builtin_exponent (exp
, &dconste
);
7838 case BUILT_IN_EXP2F
:
7839 case BUILT_IN_EXP2L
:
7840 return fold_builtin_exponent (exp
, &dconst2
);
7842 case BUILT_IN_EXP10
:
7843 case BUILT_IN_EXP10F
:
7844 case BUILT_IN_EXP10L
:
7845 case BUILT_IN_POW10
:
7846 case BUILT_IN_POW10F
:
7847 case BUILT_IN_POW10L
:
7848 return fold_builtin_exponent (exp
, &dconst10
);
7853 return fold_builtin_logarithm (exp
, &dconste
);
7856 case BUILT_IN_LOG2F
:
7857 case BUILT_IN_LOG2L
:
7858 return fold_builtin_logarithm (exp
, &dconst2
);
7860 case BUILT_IN_LOG10
:
7861 case BUILT_IN_LOG10F
:
7862 case BUILT_IN_LOG10L
:
7863 return fold_builtin_logarithm (exp
, &dconst10
);
7868 return fold_builtin_tan (arglist
);
7871 case BUILT_IN_ATANF
:
7872 case BUILT_IN_ATANL
:
7873 return fold_builtin_atan (arglist
, type
);
7878 return fold_builtin_pow (fndecl
, arglist
, type
);
7883 return fold_builtin_inf (type
, true);
7885 case BUILT_IN_HUGE_VAL
:
7886 case BUILT_IN_HUGE_VALF
:
7887 case BUILT_IN_HUGE_VALL
:
7888 return fold_builtin_inf (type
, false);
7893 return fold_builtin_nan (arglist
, type
, true);
7896 case BUILT_IN_NANSF
:
7897 case BUILT_IN_NANSL
:
7898 return fold_builtin_nan (arglist
, type
, false);
7900 case BUILT_IN_FLOOR
:
7901 case BUILT_IN_FLOORF
:
7902 case BUILT_IN_FLOORL
:
7903 return fold_builtin_floor (exp
);
7906 case BUILT_IN_CEILF
:
7907 case BUILT_IN_CEILL
:
7908 return fold_builtin_ceil (exp
);
7910 case BUILT_IN_TRUNC
:
7911 case BUILT_IN_TRUNCF
:
7912 case BUILT_IN_TRUNCL
:
7913 return fold_builtin_trunc (exp
);
7915 case BUILT_IN_ROUND
:
7916 case BUILT_IN_ROUNDF
:
7917 case BUILT_IN_ROUNDL
:
7918 return fold_builtin_round (exp
);
7920 case BUILT_IN_NEARBYINT
:
7921 case BUILT_IN_NEARBYINTF
:
7922 case BUILT_IN_NEARBYINTL
:
7924 case BUILT_IN_RINTF
:
7925 case BUILT_IN_RINTL
:
7926 return fold_trunc_transparent_mathfn (exp
);
7928 case BUILT_IN_LROUND
:
7929 case BUILT_IN_LROUNDF
:
7930 case BUILT_IN_LROUNDL
:
7931 case BUILT_IN_LLROUND
:
7932 case BUILT_IN_LLROUNDF
:
7933 case BUILT_IN_LLROUNDL
:
7934 return fold_builtin_lround (exp
);
7936 case BUILT_IN_LRINT
:
7937 case BUILT_IN_LRINTF
:
7938 case BUILT_IN_LRINTL
:
7939 case BUILT_IN_LLRINT
:
7940 case BUILT_IN_LLRINTF
:
7941 case BUILT_IN_LLRINTL
:
7942 return fold_fixed_mathfn (exp
);
7946 case BUILT_IN_FFSLL
:
7949 case BUILT_IN_CLZLL
:
7952 case BUILT_IN_CTZLL
:
7953 case BUILT_IN_POPCOUNT
:
7954 case BUILT_IN_POPCOUNTL
:
7955 case BUILT_IN_POPCOUNTLL
:
7956 case BUILT_IN_PARITY
:
7957 case BUILT_IN_PARITYL
:
7958 case BUILT_IN_PARITYLL
:
7959 return fold_builtin_bitop (exp
);
7961 case BUILT_IN_MEMCPY
:
7962 return fold_builtin_memcpy (exp
);
7964 case BUILT_IN_MEMPCPY
:
7965 return fold_builtin_mempcpy (arglist
, type
, /*endp=*/1);
7967 case BUILT_IN_MEMMOVE
:
7968 return fold_builtin_memmove (arglist
, type
);
7970 case BUILT_IN_SIGNBIT
:
7971 case BUILT_IN_SIGNBITF
:
7972 case BUILT_IN_SIGNBITL
:
7973 return fold_builtin_signbit (exp
);
7975 case BUILT_IN_ISASCII
:
7976 return fold_builtin_isascii (arglist
);
7978 case BUILT_IN_TOASCII
:
7979 return fold_builtin_toascii (arglist
);
7981 case BUILT_IN_ISDIGIT
:
7982 return fold_builtin_isdigit (arglist
);
7984 case BUILT_IN_COPYSIGN
:
7985 case BUILT_IN_COPYSIGNF
:
7986 case BUILT_IN_COPYSIGNL
:
7987 return fold_builtin_copysign (arglist
, type
);
7989 case BUILT_IN_FINITE
:
7990 case BUILT_IN_FINITEF
:
7991 case BUILT_IN_FINITEL
:
7992 return fold_builtin_classify (exp
, BUILT_IN_FINITE
);
7994 case BUILT_IN_ISINF
:
7995 case BUILT_IN_ISINFF
:
7996 case BUILT_IN_ISINFL
:
7997 return fold_builtin_classify (exp
, BUILT_IN_ISINF
);
7999 case BUILT_IN_ISNAN
:
8000 case BUILT_IN_ISNANF
:
8001 case BUILT_IN_ISNANL
:
8002 return fold_builtin_classify (exp
, BUILT_IN_ISNAN
);
8004 case BUILT_IN_ISGREATER
:
8005 return fold_builtin_unordered_cmp (exp
, UNLE_EXPR
, LE_EXPR
);
8006 case BUILT_IN_ISGREATEREQUAL
:
8007 return fold_builtin_unordered_cmp (exp
, UNLT_EXPR
, LT_EXPR
);
8008 case BUILT_IN_ISLESS
:
8009 return fold_builtin_unordered_cmp (exp
, UNGE_EXPR
, GE_EXPR
);
8010 case BUILT_IN_ISLESSEQUAL
:
8011 return fold_builtin_unordered_cmp (exp
, UNGT_EXPR
, GT_EXPR
);
8012 case BUILT_IN_ISLESSGREATER
:
8013 return fold_builtin_unordered_cmp (exp
, UNEQ_EXPR
, EQ_EXPR
);
8014 case BUILT_IN_ISUNORDERED
:
8015 return fold_builtin_unordered_cmp (exp
, UNORDERED_EXPR
, NOP_EXPR
);
8017 /* We do the folding for va_start in the expander. */
8018 case BUILT_IN_VA_START
:
8028 /* A wrapper function for builtin folding that prevents warnings for
8029 "statement without effect" and the like, caused by removing the
8030 call node earlier than the warning is generated. */
8033 fold_builtin (tree exp
, bool ignore
)
8035 exp
= fold_builtin_1 (exp
, ignore
);
8038 /* ??? Don't clobber shared nodes such as integer_zero_node. */
8039 if (CONSTANT_CLASS_P (exp
))
8040 exp
= build1 (NOP_EXPR
, TREE_TYPE (exp
), exp
);
8041 TREE_NO_WARNING (exp
) = 1;
8047 /* Conveniently construct a function call expression. */
8050 build_function_call_expr (tree fn
, tree arglist
)
8054 call_expr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
8055 call_expr
= build3 (CALL_EXPR
, TREE_TYPE (TREE_TYPE (fn
)),
8056 call_expr
, arglist
, NULL_TREE
);
8057 return fold (call_expr
);
8060 /* This function validates the types of a function call argument list
8061 represented as a tree chain of parameters against a specified list
8062 of tree_codes. If the last specifier is a 0, that represents an
8063 ellipses, otherwise the last specifier must be a VOID_TYPE. */
8066 validate_arglist (tree arglist
, ...)
8068 enum tree_code code
;
8072 va_start (ap
, arglist
);
8076 code
= va_arg (ap
, enum tree_code
);
8080 /* This signifies an ellipses, any further arguments are all ok. */
8084 /* This signifies an endlink, if no arguments remain, return
8085 true, otherwise return false. */
8089 /* If no parameters remain or the parameter's code does not
8090 match the specified code, return false. Otherwise continue
8091 checking any remaining arguments. */
8094 if (code
== POINTER_TYPE
)
8096 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
))))
8099 else if (code
!= TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))))
8103 arglist
= TREE_CHAIN (arglist
);
8107 /* We need gotos here since we can only have one VA_CLOSE in a
8115 /* Default target-specific builtin expander that does nothing. */
8118 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
8119 rtx target ATTRIBUTE_UNUSED
,
8120 rtx subtarget ATTRIBUTE_UNUSED
,
8121 enum machine_mode mode ATTRIBUTE_UNUSED
,
8122 int ignore ATTRIBUTE_UNUSED
)
8127 /* Returns true is EXP represents data that would potentially reside
8128 in a readonly section. */
8131 readonly_data_expr (tree exp
)
8135 if (TREE_CODE (exp
) != ADDR_EXPR
)
8138 exp
= get_base_address (TREE_OPERAND (exp
, 0));
8142 /* Make sure we call decl_readonly_section only for trees it
8143 can handle (since it returns true for everything it doesn't
8145 if (TREE_CODE (exp
) == STRING_CST
8146 || TREE_CODE (exp
) == CONSTRUCTOR
8147 || (TREE_CODE (exp
) == VAR_DECL
&& TREE_STATIC (exp
)))
8148 return decl_readonly_section (exp
, 0);
8153 /* Simplify a call to the strstr builtin.
8155 Return 0 if no simplification was possible, otherwise return the
8156 simplified form of the call as a tree.
8158 The simplified form may be a constant or other expression which
8159 computes the same value, but in a more efficient manner (including
8160 calls to other builtin functions).
8162 The call may contain arguments which need to be evaluated, but
8163 which are not useful to determine the result of the call. In
8164 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8165 COMPOUND_EXPR will be an argument which must be evaluated.
8166 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8167 COMPOUND_EXPR in the chain will contain the tree for the simplified
8168 form of the builtin function call. */
8171 fold_builtin_strstr (tree arglist
)
8173 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8177 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
8179 const char *p1
, *p2
;
8188 const char *r
= strstr (p1
, p2
);
8191 return build_int_cst (TREE_TYPE (s1
), 0);
8193 /* Return an offset into the constant string argument. */
8194 return fold (build2 (PLUS_EXPR
, TREE_TYPE (s1
),
8195 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
)));
8204 fn
= implicit_built_in_decls
[BUILT_IN_STRCHR
];
8208 /* New argument list transforming strstr(s1, s2) to
8209 strchr(s1, s2[0]). */
8210 arglist
= build_tree_list (NULL_TREE
,
8211 build_int_cst (NULL_TREE
, p2
[0]));
8212 arglist
= tree_cons (NULL_TREE
, s1
, arglist
);
8213 return build_function_call_expr (fn
, arglist
);
8217 /* Simplify a call to the strchr builtin.
8219 Return 0 if no simplification was possible, otherwise return the
8220 simplified form of the call as a tree.
8222 The simplified form may be a constant or other expression which
8223 computes the same value, but in a more efficient manner (including
8224 calls to other builtin functions).
8226 The call may contain arguments which need to be evaluated, but
8227 which are not useful to determine the result of the call. In
8228 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8229 COMPOUND_EXPR will be an argument which must be evaluated.
8230 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8231 COMPOUND_EXPR in the chain will contain the tree for the simplified
8232 form of the builtin function call. */
8235 fold_builtin_strchr (tree arglist
)
8237 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8241 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
8244 if (TREE_CODE (s2
) != INTEGER_CST
)
8253 if (target_char_cast (s2
, &c
))
8259 return build_int_cst (TREE_TYPE (s1
), 0);
8261 /* Return an offset into the constant string argument. */
8262 return fold (build2 (PLUS_EXPR
, TREE_TYPE (s1
),
8263 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
)));
8269 /* Simplify a call to the strrchr builtin.
8271 Return 0 if no simplification was possible, otherwise return the
8272 simplified form of the call as a tree.
8274 The simplified form may be a constant or other expression which
8275 computes the same value, but in a more efficient manner (including
8276 calls to other builtin functions).
8278 The call may contain arguments which need to be evaluated, but
8279 which are not useful to determine the result of the call. In
8280 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8281 COMPOUND_EXPR will be an argument which must be evaluated.
8282 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8283 COMPOUND_EXPR in the chain will contain the tree for the simplified
8284 form of the builtin function call. */
8287 fold_builtin_strrchr (tree arglist
)
8289 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8293 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
8297 if (TREE_CODE (s2
) != INTEGER_CST
)
8306 if (target_char_cast (s2
, &c
))
8309 r
= strrchr (p1
, c
);
8312 return build_int_cst (TREE_TYPE (s1
), 0);
8314 /* Return an offset into the constant string argument. */
8315 return fold (build2 (PLUS_EXPR
, TREE_TYPE (s1
),
8316 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
)));
8319 if (! integer_zerop (s2
))
8322 fn
= implicit_built_in_decls
[BUILT_IN_STRCHR
];
8326 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
8327 return build_function_call_expr (fn
, arglist
);
8331 /* Simplify a call to the strpbrk builtin.
8333 Return 0 if no simplification was possible, otherwise return the
8334 simplified form of the call as a tree.
8336 The simplified form may be a constant or other expression which
8337 computes the same value, but in a more efficient manner (including
8338 calls to other builtin functions).
8340 The call may contain arguments which need to be evaluated, but
8341 which are not useful to determine the result of the call. In
8342 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8343 COMPOUND_EXPR will be an argument which must be evaluated.
8344 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8345 COMPOUND_EXPR in the chain will contain the tree for the simplified
8346 form of the builtin function call. */
8349 fold_builtin_strpbrk (tree arglist
)
8351 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8355 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
8357 const char *p1
, *p2
;
8366 const char *r
= strpbrk (p1
, p2
);
8369 return build_int_cst (TREE_TYPE (s1
), 0);
8371 /* Return an offset into the constant string argument. */
8372 return fold (build2 (PLUS_EXPR
, TREE_TYPE (s1
),
8373 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
)));
8377 /* strpbrk(x, "") == NULL.
8378 Evaluate and ignore s1 in case it had side-effects. */
8379 return omit_one_operand (TREE_TYPE (s1
), integer_zero_node
, s1
);
8382 return 0; /* Really call strpbrk. */
8384 fn
= implicit_built_in_decls
[BUILT_IN_STRCHR
];
8388 /* New argument list transforming strpbrk(s1, s2) to
8389 strchr(s1, s2[0]). */
8390 arglist
= build_tree_list (NULL_TREE
,
8391 build_int_cst (NULL_TREE
, p2
[0]));
8392 arglist
= tree_cons (NULL_TREE
, s1
, arglist
);
8393 return build_function_call_expr (fn
, arglist
);
8397 /* Simplify a call to the strcat builtin.
8399 Return 0 if no simplification was possible, otherwise return the
8400 simplified form of the call as a tree.
8402 The simplified form may be a constant or other expression which
8403 computes the same value, but in a more efficient manner (including
8404 calls to other builtin functions).
8406 The call may contain arguments which need to be evaluated, but
8407 which are not useful to determine the result of the call. In
8408 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8409 COMPOUND_EXPR will be an argument which must be evaluated.
8410 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8411 COMPOUND_EXPR in the chain will contain the tree for the simplified
8412 form of the builtin function call. */
8415 fold_builtin_strcat (tree arglist
)
8417 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8421 tree dst
= TREE_VALUE (arglist
),
8422 src
= TREE_VALUE (TREE_CHAIN (arglist
));
8423 const char *p
= c_getstr (src
);
8425 /* If the string length is zero, return the dst parameter. */
8426 if (p
&& *p
== '\0')
8433 /* Simplify a call to the strncat builtin.
8435 Return 0 if no simplification was possible, otherwise return the
8436 simplified form of the call as a tree.
8438 The simplified form may be a constant or other expression which
8439 computes the same value, but in a more efficient manner (including
8440 calls to other builtin functions).
8442 The call may contain arguments which need to be evaluated, but
8443 which are not useful to determine the result of the call. In
8444 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8445 COMPOUND_EXPR will be an argument which must be evaluated.
8446 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8447 COMPOUND_EXPR in the chain will contain the tree for the simplified
8448 form of the builtin function call. */
8451 fold_builtin_strncat (tree arglist
)
8453 if (!validate_arglist (arglist
,
8454 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8458 tree dst
= TREE_VALUE (arglist
);
8459 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
8460 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8461 const char *p
= c_getstr (src
);
8463 /* If the requested length is zero, or the src parameter string
8464 length is zero, return the dst parameter. */
8465 if (integer_zerop (len
) || (p
&& *p
== '\0'))
8466 return omit_two_operands (TREE_TYPE (dst
), dst
, src
, len
);
8468 /* If the requested len is greater than or equal to the string
8469 length, call strcat. */
8470 if (TREE_CODE (len
) == INTEGER_CST
&& p
8471 && compare_tree_int (len
, strlen (p
)) >= 0)
8474 = tree_cons (NULL_TREE
, dst
, build_tree_list (NULL_TREE
, src
));
8475 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCAT
];
8477 /* If the replacement _DECL isn't initialized, don't do the
8482 return build_function_call_expr (fn
, newarglist
);
8488 /* Simplify a call to the strspn builtin.
8490 Return 0 if no simplification was possible, otherwise return the
8491 simplified form of the call as a tree.
8493 The simplified form may be a constant or other expression which
8494 computes the same value, but in a more efficient manner (including
8495 calls to other builtin functions).
8497 The call may contain arguments which need to be evaluated, but
8498 which are not useful to determine the result of the call. In
8499 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8500 COMPOUND_EXPR will be an argument which must be evaluated.
8501 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8502 COMPOUND_EXPR in the chain will contain the tree for the simplified
8503 form of the builtin function call. */
8506 fold_builtin_strspn (tree arglist
)
8508 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8512 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
8513 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
8515 /* If both arguments are constants, evaluate at compile-time. */
8518 const size_t r
= strspn (p1
, p2
);
8519 return size_int (r
);
8522 /* If either argument is "", return 0. */
8523 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
8524 /* Evaluate and ignore both arguments in case either one has
8526 return omit_two_operands (integer_type_node
, integer_zero_node
,
8532 /* Simplify a call to the strcspn builtin.
8534 Return 0 if no simplification was possible, otherwise return the
8535 simplified form of the call as a tree.
8537 The simplified form may be a constant or other expression which
8538 computes the same value, but in a more efficient manner (including
8539 calls to other builtin functions).
8541 The call may contain arguments which need to be evaluated, but
8542 which are not useful to determine the result of the call. In
8543 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8544 COMPOUND_EXPR will be an argument which must be evaluated.
8545 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8546 COMPOUND_EXPR in the chain will contain the tree for the simplified
8547 form of the builtin function call. */
8550 fold_builtin_strcspn (tree arglist
)
8552 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8556 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
8557 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
8559 /* If both arguments are constants, evaluate at compile-time. */
8562 const size_t r
= strcspn (p1
, p2
);
8563 return size_int (r
);
8566 /* If the first argument is "", return 0. */
8567 if (p1
&& *p1
== '\0')
8569 /* Evaluate and ignore argument s2 in case it has
8571 return omit_one_operand (integer_type_node
,
8572 integer_zero_node
, s2
);
8575 /* If the second argument is "", return __builtin_strlen(s1). */
8576 if (p2
&& *p2
== '\0')
8578 tree newarglist
= build_tree_list (NULL_TREE
, s1
),
8579 fn
= implicit_built_in_decls
[BUILT_IN_STRLEN
];
8581 /* If the replacement _DECL isn't initialized, don't do the
8586 return build_function_call_expr (fn
, newarglist
);
8592 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
8593 by the builtin will be ignored. UNLOCKED is true is true if this
8594 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
8595 the known length of the string. Return NULL_TREE if no simplification
8599 fold_builtin_fputs (tree arglist
, bool ignore
, bool unlocked
, tree len
)
8602 tree fn_fputc
= unlocked
? implicit_built_in_decls
[BUILT_IN_FPUTC_UNLOCKED
]
8603 : implicit_built_in_decls
[BUILT_IN_FPUTC
];
8604 tree fn_fwrite
= unlocked
? implicit_built_in_decls
[BUILT_IN_FWRITE_UNLOCKED
]
8605 : implicit_built_in_decls
[BUILT_IN_FWRITE
];
8607 /* If the return value is used, or the replacement _DECL isn't
8608 initialized, don't do the transformation. */
8609 if (!ignore
|| !fn_fputc
|| !fn_fwrite
)
8612 /* Verify the arguments in the original call. */
8613 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8617 len
= c_strlen (TREE_VALUE (arglist
), 0);
8619 /* Get the length of the string passed to fputs. If the length
8620 can't be determined, punt. */
8622 || TREE_CODE (len
) != INTEGER_CST
)
8625 switch (compare_tree_int (len
, 1))
8627 case -1: /* length is 0, delete the call entirely . */
8628 return omit_one_operand (integer_type_node
, integer_zero_node
,
8629 TREE_VALUE (TREE_CHAIN (arglist
)));
8631 case 0: /* length is 1, call fputc. */
8633 const char *p
= c_getstr (TREE_VALUE (arglist
));
8637 /* New argument list transforming fputs(string, stream) to
8638 fputc(string[0], stream). */
8639 arglist
= build_tree_list (NULL_TREE
,
8640 TREE_VALUE (TREE_CHAIN (arglist
)));
8641 arglist
= tree_cons (NULL_TREE
,
8642 build_int_cst (NULL_TREE
, p
[0]),
8649 case 1: /* length is greater than 1, call fwrite. */
8653 /* If optimizing for size keep fputs. */
8656 string_arg
= TREE_VALUE (arglist
);
8657 /* New argument list transforming fputs(string, stream) to
8658 fwrite(string, 1, len, stream). */
8659 arglist
= build_tree_list (NULL_TREE
,
8660 TREE_VALUE (TREE_CHAIN (arglist
)));
8661 arglist
= tree_cons (NULL_TREE
, len
, arglist
);
8662 arglist
= tree_cons (NULL_TREE
, size_one_node
, arglist
);
8663 arglist
= tree_cons (NULL_TREE
, string_arg
, arglist
);
8671 /* These optimizations are only performed when the result is ignored,
8672 hence there's no need to cast the result to integer_type_node. */
8673 return build_function_call_expr (fn
, arglist
);
8676 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
8677 produced. False otherwise. This is done so that we don't output the error
8678 or warning twice or three times. */
8680 fold_builtin_next_arg (tree arglist
)
8682 tree fntype
= TREE_TYPE (current_function_decl
);
8684 if (TYPE_ARG_TYPES (fntype
) == 0
8685 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype
)))
8688 error ("%<va_start%> used in function with fixed args");
8693 /* Evidently an out of date version of <stdarg.h>; can't validate
8694 va_start's second argument, but can still work as intended. */
8695 warning ("%<__builtin_next_arg%> called without an argument");
8698 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
8699 when we checked the arguments and if needed issued a warning. */
8700 else if (!TREE_CHAIN (arglist
)
8701 || !integer_zerop (TREE_VALUE (arglist
))
8702 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist
)))
8703 || TREE_CHAIN (TREE_CHAIN (arglist
)))
8705 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
8706 tree arg
= TREE_VALUE (arglist
);
8708 if (TREE_CHAIN (arglist
))
8710 error ("%<va_start%> used with too many arguments");
8714 /* Strip off all nops for the sake of the comparison. This
8715 is not quite the same as STRIP_NOPS. It does more.
8716 We must also strip off INDIRECT_EXPR for C++ reference
8718 while (TREE_CODE (arg
) == NOP_EXPR
8719 || TREE_CODE (arg
) == CONVERT_EXPR
8720 || TREE_CODE (arg
) == NON_LVALUE_EXPR
8721 || TREE_CODE (arg
) == INDIRECT_REF
)
8722 arg
= TREE_OPERAND (arg
, 0);
8723 if (arg
!= last_parm
)
8725 /* FIXME: Sometimes with the tree optimizers we can get the
8726 not the last argument even though the user used the last
8727 argument. We just warn and set the arg to be the last
8728 argument so that we will get wrong-code because of
8730 warning ("second parameter of %<va_start%> not last named argument");
8732 /* We want to verify the second parameter just once before the tree
8733 optimizers are run and then avoid keeping it in the tree,
8734 as otherwise we could warn even for correct code like:
8735 void foo (int i, ...)
8736 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
8737 TREE_VALUE (arglist
) = integer_zero_node
;
8738 TREE_CHAIN (arglist
) = build_tree_list (NULL
, integer_zero_node
);
8744 /* Simplify a call to the sprintf builtin.
8746 Return 0 if no simplification was possible, otherwise return the
8747 simplified form of the call as a tree. If IGNORED is true, it means that
8748 the caller does not use the returned value of the function. */
8751 fold_builtin_sprintf (tree arglist
, int ignored
)
8753 tree call
, retval
, dest
, fmt
;
8754 const char *fmt_str
= NULL
;
8756 /* Verify the required arguments in the original call. We deal with two
8757 types of sprintf() calls: 'sprintf (str, fmt)' and
8758 'sprintf (dest, "%s", orig)'. */
8759 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
)
8760 && !validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, POINTER_TYPE
,
8764 /* Get the destination string and the format specifier. */
8765 dest
= TREE_VALUE (arglist
);
8766 fmt
= TREE_VALUE (TREE_CHAIN (arglist
));
8768 /* Check whether the format is a literal string constant. */
8769 fmt_str
= c_getstr (fmt
);
8770 if (fmt_str
== NULL
)
8776 /* If the format doesn't contain % args or %%, use strcpy. */
8777 if (strchr (fmt_str
, '%') == NULL
)
8779 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
8784 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
8785 'format' is known to contain no % formats. */
8786 arglist
= build_tree_list (NULL_TREE
, fmt
);
8787 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
8788 call
= build_function_call_expr (fn
, arglist
);
8790 retval
= build_int_cst (NULL_TREE
, strlen (fmt_str
));
8793 /* If the format is "%s", use strcpy if the result isn't used. */
8794 else if (fmt_str
&& strcmp (fmt_str
, "%s") == 0)
8797 fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
8802 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
8803 orig
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8804 arglist
= build_tree_list (NULL_TREE
, orig
);
8805 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
8808 retval
= c_strlen (orig
, 1);
8809 if (!retval
|| TREE_CODE (retval
) != INTEGER_CST
)
8812 call
= build_function_call_expr (fn
, arglist
);
8818 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls
[BUILT_IN_SPRINTF
])),
8820 return build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), call
, retval
);