1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "coretypes.h"
30 #include "tree-gimple.h"
33 #include "hard-reg-set.h"
36 #include "insn-config.h"
42 #include "typeclass.h"
47 #include "langhooks.h"
48 #include "basic-block.h"
49 #include "tree-mudflap.h"
51 #ifndef PAD_VARARGS_DOWN
52 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
55 /* Define the names of the builtin function types and codes. */
56 const char *const built_in_class_names
[4]
57 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
59 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
60 const char * built_in_names
[(int) END_BUILTINS
] =
62 #include "builtins.def"
66 /* Setup an array of _DECL trees, make sure each element is
67 initialized to NULL_TREE. */
68 tree built_in_decls
[(int) END_BUILTINS
];
69 /* Declarations used when constructing the builtin implicitly in the compiler.
70 It may be NULL_TREE when this is invalid (for instance runtime is not
71 required to implement the function call in all cases). */
72 tree implicit_built_in_decls
[(int) END_BUILTINS
];
74 static int get_pointer_alignment (tree
, unsigned int);
75 static const char *c_getstr (tree
);
76 static rtx
c_readstr (const char *, enum machine_mode
);
77 static int target_char_cast (tree
, char *);
78 static rtx
get_memory_rtx (tree
, tree
);
79 static int apply_args_size (void);
80 static int apply_result_size (void);
81 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
82 static rtx
result_vector (int, rtx
);
84 static void expand_builtin_update_setjmp_buf (rtx
);
85 static void expand_builtin_prefetch (tree
);
86 static rtx
expand_builtin_apply_args (void);
87 static rtx
expand_builtin_apply_args_1 (void);
88 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
89 static void expand_builtin_return (rtx
);
90 static enum type_class
type_to_class (tree
);
91 static rtx
expand_builtin_classify_type (tree
);
92 static void expand_errno_check (tree
, rtx
);
93 static rtx
expand_builtin_mathfn (tree
, rtx
, rtx
);
94 static rtx
expand_builtin_mathfn_2 (tree
, rtx
, rtx
);
95 static rtx
expand_builtin_mathfn_3 (tree
, rtx
, rtx
);
96 static rtx
expand_builtin_sincos (tree
);
97 static rtx
expand_builtin_int_roundingfn (tree
, rtx
, rtx
);
98 static rtx
expand_builtin_args_info (tree
);
99 static rtx
expand_builtin_next_arg (void);
100 static rtx
expand_builtin_va_start (tree
);
101 static rtx
expand_builtin_va_end (tree
);
102 static rtx
expand_builtin_va_copy (tree
);
103 static rtx
expand_builtin_memcmp (tree
, tree
, rtx
, enum machine_mode
);
104 static rtx
expand_builtin_strcmp (tree
, rtx
, enum machine_mode
);
105 static rtx
expand_builtin_strncmp (tree
, rtx
, enum machine_mode
);
106 static rtx
builtin_memcpy_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
107 static rtx
expand_builtin_strcat (tree
, tree
, rtx
, enum machine_mode
);
108 static rtx
expand_builtin_strncat (tree
, rtx
, enum machine_mode
);
109 static rtx
expand_builtin_strspn (tree
, rtx
, enum machine_mode
);
110 static rtx
expand_builtin_strcspn (tree
, rtx
, enum machine_mode
);
111 static rtx
expand_builtin_memcpy (tree
, rtx
, enum machine_mode
);
112 static rtx
expand_builtin_mempcpy (tree
, tree
, rtx
, enum machine_mode
, int);
113 static rtx
expand_builtin_memmove (tree
, tree
, rtx
, enum machine_mode
, tree
);
114 static rtx
expand_builtin_bcopy (tree
);
115 static rtx
expand_builtin_strcpy (tree
, tree
, rtx
, enum machine_mode
);
116 static rtx
expand_builtin_stpcpy (tree
, rtx
, enum machine_mode
);
117 static rtx
builtin_strncpy_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
118 static rtx
expand_builtin_strncpy (tree
, rtx
, enum machine_mode
);
119 static rtx
builtin_memset_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
120 static rtx
builtin_memset_gen_str (void *, HOST_WIDE_INT
, enum machine_mode
);
121 static rtx
expand_builtin_memset (tree
, rtx
, enum machine_mode
, tree
);
122 static rtx
expand_builtin_bzero (tree
);
123 static rtx
expand_builtin_strlen (tree
, rtx
, enum machine_mode
);
124 static rtx
expand_builtin_strstr (tree
, tree
, rtx
, enum machine_mode
);
125 static rtx
expand_builtin_strpbrk (tree
, tree
, rtx
, enum machine_mode
);
126 static rtx
expand_builtin_strchr (tree
, tree
, rtx
, enum machine_mode
);
127 static rtx
expand_builtin_strrchr (tree
, tree
, rtx
, enum machine_mode
);
128 static rtx
expand_builtin_alloca (tree
, rtx
);
129 static rtx
expand_builtin_unop (enum machine_mode
, tree
, rtx
, rtx
, optab
);
130 static rtx
expand_builtin_frame_address (tree
, tree
);
131 static rtx
expand_builtin_fputs (tree
, rtx
, bool);
132 static rtx
expand_builtin_printf (tree
, rtx
, enum machine_mode
, bool);
133 static rtx
expand_builtin_fprintf (tree
, rtx
, enum machine_mode
, bool);
134 static rtx
expand_builtin_sprintf (tree
, rtx
, enum machine_mode
);
135 static tree
stabilize_va_list (tree
, int);
136 static rtx
expand_builtin_expect (tree
, rtx
);
137 static tree
fold_builtin_constant_p (tree
);
138 static tree
fold_builtin_classify_type (tree
);
139 static tree
fold_builtin_strlen (tree
);
140 static tree
fold_builtin_inf (tree
, int);
141 static tree
fold_builtin_nan (tree
, tree
, int);
142 static int validate_arglist (tree
, ...);
143 static bool integer_valued_real_p (tree
);
144 static tree
fold_trunc_transparent_mathfn (tree
, tree
);
145 static bool readonly_data_expr (tree
);
146 static rtx
expand_builtin_fabs (tree
, rtx
, rtx
);
147 static rtx
expand_builtin_signbit (tree
, rtx
);
148 static tree
fold_builtin_sqrt (tree
, tree
);
149 static tree
fold_builtin_cbrt (tree
, tree
);
150 static tree
fold_builtin_pow (tree
, tree
, tree
);
151 static tree
fold_builtin_powi (tree
, tree
, tree
);
152 static tree
fold_builtin_sin (tree
);
153 static tree
fold_builtin_cos (tree
, tree
, tree
);
154 static tree
fold_builtin_tan (tree
);
155 static tree
fold_builtin_atan (tree
, tree
);
156 static tree
fold_builtin_trunc (tree
, tree
);
157 static tree
fold_builtin_floor (tree
, tree
);
158 static tree
fold_builtin_ceil (tree
, tree
);
159 static tree
fold_builtin_round (tree
, tree
);
160 static tree
fold_builtin_int_roundingfn (tree
, tree
);
161 static tree
fold_builtin_bitop (tree
, tree
);
162 static tree
fold_builtin_memory_op (tree
, tree
, bool, int);
163 static tree
fold_builtin_strchr (tree
, tree
);
164 static tree
fold_builtin_memcmp (tree
);
165 static tree
fold_builtin_strcmp (tree
);
166 static tree
fold_builtin_strncmp (tree
);
167 static tree
fold_builtin_signbit (tree
, tree
);
168 static tree
fold_builtin_copysign (tree
, tree
, tree
);
169 static tree
fold_builtin_isascii (tree
);
170 static tree
fold_builtin_toascii (tree
);
171 static tree
fold_builtin_isdigit (tree
);
172 static tree
fold_builtin_fabs (tree
, tree
);
173 static tree
fold_builtin_abs (tree
, tree
);
174 static tree
fold_builtin_unordered_cmp (tree
, tree
, enum tree_code
,
176 static tree
fold_builtin_1 (tree
, tree
, bool);
178 static tree
fold_builtin_strpbrk (tree
, tree
);
179 static tree
fold_builtin_strstr (tree
, tree
);
180 static tree
fold_builtin_strrchr (tree
, tree
);
181 static tree
fold_builtin_strcat (tree
);
182 static tree
fold_builtin_strncat (tree
);
183 static tree
fold_builtin_strspn (tree
);
184 static tree
fold_builtin_strcspn (tree
);
185 static tree
fold_builtin_sprintf (tree
, int);
187 static rtx
expand_builtin_object_size (tree
);
188 static rtx
expand_builtin_memory_chk (tree
, rtx
, enum machine_mode
,
189 enum built_in_function
);
190 static void maybe_emit_chk_warning (tree
, enum built_in_function
);
191 static void maybe_emit_sprintf_chk_warning (tree
, enum built_in_function
);
192 static tree
fold_builtin_object_size (tree
);
193 static tree
fold_builtin_strcat_chk (tree
, tree
);
194 static tree
fold_builtin_strncat_chk (tree
, tree
);
195 static tree
fold_builtin_sprintf_chk (tree
, enum built_in_function
);
196 static tree
fold_builtin_printf (tree
, tree
, bool, enum built_in_function
);
197 static tree
fold_builtin_fprintf (tree
, tree
, bool, enum built_in_function
);
198 static bool init_target_chars (void);
200 static unsigned HOST_WIDE_INT target_newline
;
201 static unsigned HOST_WIDE_INT target_percent
;
202 static unsigned HOST_WIDE_INT target_c
;
203 static unsigned HOST_WIDE_INT target_s
;
204 static char target_percent_c
[3];
205 static char target_percent_s
[3];
206 static char target_percent_s_newline
[4];
208 /* Return true if NODE should be considered for inline expansion regardless
209 of the optimization level. This means whenever a function is invoked with
210 its "internal" name, which normally contains the prefix "__builtin". */
212 static bool called_as_built_in (tree node
)
214 const char *name
= IDENTIFIER_POINTER (DECL_NAME (node
));
215 if (strncmp (name
, "__builtin_", 10) == 0)
217 if (strncmp (name
, "__sync_", 7) == 0)
222 /* Return the alignment in bits of EXP, a pointer valued expression.
223 But don't return more than MAX_ALIGN no matter what.
224 The alignment returned is, by default, the alignment of the thing that
225 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
227 Otherwise, look at the expression to see if we can do better, i.e., if the
228 expression is actually pointing at an object whose alignment is tighter. */
231 get_pointer_alignment (tree exp
, unsigned int max_align
)
233 unsigned int align
, inner
;
235 if (! POINTER_TYPE_P (TREE_TYPE (exp
)))
238 align
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp
)));
239 align
= MIN (align
, max_align
);
243 switch (TREE_CODE (exp
))
247 case NON_LVALUE_EXPR
:
248 exp
= TREE_OPERAND (exp
, 0);
249 if (! POINTER_TYPE_P (TREE_TYPE (exp
)))
252 inner
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp
)));
253 align
= MIN (inner
, max_align
);
257 /* If sum of pointer + int, restrict our maximum alignment to that
258 imposed by the integer. If not, we can't do any better than
260 if (! host_integerp (TREE_OPERAND (exp
, 1), 1))
263 while (((tree_low_cst (TREE_OPERAND (exp
, 1), 1))
264 & (max_align
/ BITS_PER_UNIT
- 1))
268 exp
= TREE_OPERAND (exp
, 0);
272 /* See what we are pointing at and look at its alignment. */
273 exp
= TREE_OPERAND (exp
, 0);
275 if (handled_component_p (exp
))
277 HOST_WIDE_INT bitsize
, bitpos
;
279 enum machine_mode mode
;
280 int unsignedp
, volatilep
;
282 exp
= get_inner_reference (exp
, &bitsize
, &bitpos
, &offset
,
283 &mode
, &unsignedp
, &volatilep
, true);
285 inner
= MIN (inner
, (unsigned) (bitpos
& -bitpos
));
286 if (offset
&& TREE_CODE (offset
) == PLUS_EXPR
287 && host_integerp (TREE_OPERAND (offset
, 1), 1))
289 /* Any overflow in calculating offset_bits won't change
292 = ((unsigned) tree_low_cst (TREE_OPERAND (offset
, 1), 1)
296 inner
= MIN (inner
, (offset_bits
& -offset_bits
));
297 offset
= TREE_OPERAND (offset
, 0);
299 if (offset
&& TREE_CODE (offset
) == MULT_EXPR
300 && host_integerp (TREE_OPERAND (offset
, 1), 1))
302 /* Any overflow in calculating offset_factor won't change
304 unsigned offset_factor
305 = ((unsigned) tree_low_cst (TREE_OPERAND (offset
, 1), 1)
309 inner
= MIN (inner
, (offset_factor
& -offset_factor
));
312 inner
= MIN (inner
, BITS_PER_UNIT
);
314 if (TREE_CODE (exp
) == FUNCTION_DECL
)
315 align
= FUNCTION_BOUNDARY
;
316 else if (DECL_P (exp
))
317 align
= MIN (inner
, DECL_ALIGN (exp
));
318 #ifdef CONSTANT_ALIGNMENT
319 else if (CONSTANT_CLASS_P (exp
))
320 align
= MIN (inner
, (unsigned)CONSTANT_ALIGNMENT (exp
, align
));
322 else if (TREE_CODE (exp
) == VIEW_CONVERT_EXPR
323 || TREE_CODE (exp
) == INDIRECT_REF
)
324 align
= MIN (TYPE_ALIGN (TREE_TYPE (exp
)), inner
);
326 align
= MIN (align
, inner
);
327 return MIN (align
, max_align
);
335 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
336 way, because it could contain a zero byte in the middle.
337 TREE_STRING_LENGTH is the size of the character array, not the string.
339 ONLY_VALUE should be nonzero if the result is not going to be emitted
340 into the instruction stream and zero if it is going to be expanded.
341 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
342 is returned, otherwise NULL, since
343 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
344 evaluate the side-effects.
346 The value returned is of type `ssizetype'.
348 Unfortunately, string_constant can't access the values of const char
349 arrays with initializers, so neither can we do so here. */
352 c_strlen (tree src
, int only_value
)
355 HOST_WIDE_INT offset
;
360 if (TREE_CODE (src
) == COND_EXPR
361 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
365 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
);
366 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
);
367 if (tree_int_cst_equal (len1
, len2
))
371 if (TREE_CODE (src
) == COMPOUND_EXPR
372 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
373 return c_strlen (TREE_OPERAND (src
, 1), only_value
);
375 src
= string_constant (src
, &offset_node
);
379 max
= TREE_STRING_LENGTH (src
) - 1;
380 ptr
= TREE_STRING_POINTER (src
);
382 if (offset_node
&& TREE_CODE (offset_node
) != INTEGER_CST
)
384 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
385 compute the offset to the following null if we don't know where to
386 start searching for it. */
389 for (i
= 0; i
< max
; i
++)
393 /* We don't know the starting offset, but we do know that the string
394 has no internal zero bytes. We can assume that the offset falls
395 within the bounds of the string; otherwise, the programmer deserves
396 what he gets. Subtract the offset from the length of the string,
397 and return that. This would perhaps not be valid if we were dealing
398 with named arrays in addition to literal string constants. */
400 return size_diffop (size_int (max
), offset_node
);
403 /* We have a known offset into the string. Start searching there for
404 a null character if we can represent it as a single HOST_WIDE_INT. */
405 if (offset_node
== 0)
407 else if (! host_integerp (offset_node
, 0))
410 offset
= tree_low_cst (offset_node
, 0);
412 /* If the offset is known to be out of bounds, warn, and call strlen at
414 if (offset
< 0 || offset
> max
)
416 warning (0, "offset outside bounds of constant string");
420 /* Use strlen to search for the first zero byte. Since any strings
421 constructed with build_string will have nulls appended, we win even
422 if we get handed something like (char[4])"abcd".
424 Since OFFSET is our starting index into the string, no further
425 calculation is needed. */
426 return ssize_int (strlen (ptr
+ offset
));
429 /* Return a char pointer for a C string if it is a string constant
430 or sum of string constant and integer constant. */
437 src
= string_constant (src
, &offset_node
);
441 if (offset_node
== 0)
442 return TREE_STRING_POINTER (src
);
443 else if (!host_integerp (offset_node
, 1)
444 || compare_tree_int (offset_node
, TREE_STRING_LENGTH (src
) - 1) > 0)
447 return TREE_STRING_POINTER (src
) + tree_low_cst (offset_node
, 1);
450 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
451 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
454 c_readstr (const char *str
, enum machine_mode mode
)
460 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
465 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
468 if (WORDS_BIG_ENDIAN
)
469 j
= GET_MODE_SIZE (mode
) - i
- 1;
470 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
471 && GET_MODE_SIZE (mode
) > UNITS_PER_WORD
)
472 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
474 gcc_assert (j
<= 2 * HOST_BITS_PER_WIDE_INT
);
477 ch
= (unsigned char) str
[i
];
478 c
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
480 return immed_double_const (c
[0], c
[1], mode
);
483 /* Cast a target constant CST to target CHAR and if that value fits into
484 host char type, return zero and put that value into variable pointed to by
488 target_char_cast (tree cst
, char *p
)
490 unsigned HOST_WIDE_INT val
, hostval
;
492 if (!host_integerp (cst
, 1)
493 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
496 val
= tree_low_cst (cst
, 1);
497 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
498 val
&= (((unsigned HOST_WIDE_INT
) 1) << CHAR_TYPE_SIZE
) - 1;
501 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
502 hostval
&= (((unsigned HOST_WIDE_INT
) 1) << HOST_BITS_PER_CHAR
) - 1;
511 /* Similar to save_expr, but assumes that arbitrary code is not executed
512 in between the multiple evaluations. In particular, we assume that a
513 non-addressable local variable will not be modified. */
516 builtin_save_expr (tree exp
)
518 if (TREE_ADDRESSABLE (exp
) == 0
519 && (TREE_CODE (exp
) == PARM_DECL
520 || (TREE_CODE (exp
) == VAR_DECL
&& !TREE_STATIC (exp
))))
523 return save_expr (exp
);
526 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
527 times to get the address of either a higher stack frame, or a return
528 address located within it (depending on FNDECL_CODE). */
531 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
)
535 #ifdef INITIAL_FRAME_ADDRESS_RTX
536 rtx tem
= INITIAL_FRAME_ADDRESS_RTX
;
540 /* For a zero count with __builtin_return_address, we don't care what
541 frame address we return, because target-specific definitions will
542 override us. Therefore frame pointer elimination is OK, and using
543 the soft frame pointer is OK.
545 For a non-zero count, or a zero count with __builtin_frame_address,
546 we require a stable offset from the current frame pointer to the
547 previous one, so we must use the hard frame pointer, and
548 we must disable frame pointer elimination. */
549 if (count
== 0 && fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
550 tem
= frame_pointer_rtx
;
553 tem
= hard_frame_pointer_rtx
;
555 /* Tell reload not to eliminate the frame pointer. */
556 current_function_accesses_prior_frames
= 1;
560 /* Some machines need special handling before we can access
561 arbitrary frames. For example, on the SPARC, we must first flush
562 all register windows to the stack. */
563 #ifdef SETUP_FRAME_ADDRESSES
565 SETUP_FRAME_ADDRESSES ();
568 /* On the SPARC, the return address is not in the frame, it is in a
569 register. There is no way to access it off of the current frame
570 pointer, but it can be accessed off the previous frame pointer by
571 reading the value from the register window save area. */
572 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
573 if (fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
577 /* Scan back COUNT frames to the specified frame. */
578 for (i
= 0; i
< count
; i
++)
580 /* Assume the dynamic chain pointer is in the word that the
581 frame address points to, unless otherwise specified. */
582 #ifdef DYNAMIC_CHAIN_ADDRESS
583 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
585 tem
= memory_address (Pmode
, tem
);
586 tem
= gen_frame_mem (Pmode
, tem
);
587 tem
= copy_to_reg (tem
);
590 /* For __builtin_frame_address, return what we've got. But, on
591 the SPARC for example, we may have to add a bias. */
592 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
593 #ifdef FRAME_ADDR_RTX
594 return FRAME_ADDR_RTX (tem
);
599 /* For __builtin_return_address, get the return address from that frame. */
600 #ifdef RETURN_ADDR_RTX
601 tem
= RETURN_ADDR_RTX (count
, tem
);
603 tem
= memory_address (Pmode
,
604 plus_constant (tem
, GET_MODE_SIZE (Pmode
)));
605 tem
= gen_frame_mem (Pmode
, tem
);
610 /* Alias set used for setjmp buffer. */
611 static HOST_WIDE_INT setjmp_alias_set
= -1;
613 /* Construct the leading half of a __builtin_setjmp call. Control will
614 return to RECEIVER_LABEL. This is also called directly by the SJLJ
615 exception handling code. */
618 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
620 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
624 if (setjmp_alias_set
== -1)
625 setjmp_alias_set
= new_alias_set ();
627 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
629 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
631 /* We store the frame pointer and the address of receiver_label in
632 the buffer and use the rest of it for the stack save area, which
633 is machine-dependent. */
635 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
636 set_mem_alias_set (mem
, setjmp_alias_set
);
637 emit_move_insn (mem
, targetm
.builtin_setjmp_frame_value ());
639 mem
= gen_rtx_MEM (Pmode
, plus_constant (buf_addr
, GET_MODE_SIZE (Pmode
))),
640 set_mem_alias_set (mem
, setjmp_alias_set
);
642 emit_move_insn (validize_mem (mem
),
643 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
645 stack_save
= gen_rtx_MEM (sa_mode
,
646 plus_constant (buf_addr
,
647 2 * GET_MODE_SIZE (Pmode
)));
648 set_mem_alias_set (stack_save
, setjmp_alias_set
);
649 emit_stack_save (SAVE_NONLOCAL
, &stack_save
, NULL_RTX
);
651 /* If there is further processing to do, do it. */
652 #ifdef HAVE_builtin_setjmp_setup
653 if (HAVE_builtin_setjmp_setup
)
654 emit_insn (gen_builtin_setjmp_setup (buf_addr
));
657 /* Tell optimize_save_area_alloca that extra work is going to
658 need to go on during alloca. */
659 current_function_calls_setjmp
= 1;
661 /* Set this so all the registers get saved in our frame; we need to be
662 able to copy the saved values for any registers from frames we unwind. */
663 current_function_has_nonlocal_label
= 1;
666 /* Construct the trailing part of a __builtin_setjmp call. This is
667 also called directly by the SJLJ exception handling code. */
670 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED
)
672 /* Clobber the FP when we get here, so we have to make sure it's
673 marked as used by this function. */
674 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
676 /* Mark the static chain as clobbered here so life information
677 doesn't get messed up for it. */
678 emit_insn (gen_rtx_CLOBBER (VOIDmode
, static_chain_rtx
));
680 /* Now put in the code to restore the frame pointer, and argument
681 pointer, if needed. */
682 #ifdef HAVE_nonlocal_goto
683 if (! HAVE_nonlocal_goto
)
686 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
687 /* This might change the hard frame pointer in ways that aren't
688 apparent to early optimization passes, so force a clobber. */
689 emit_insn (gen_rtx_CLOBBER (VOIDmode
, hard_frame_pointer_rtx
));
692 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
693 if (fixed_regs
[ARG_POINTER_REGNUM
])
695 #ifdef ELIMINABLE_REGS
697 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
699 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
700 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
701 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
704 if (i
== ARRAY_SIZE (elim_regs
))
707 /* Now restore our arg pointer from the address at which it
708 was saved in our stack frame. */
709 emit_move_insn (virtual_incoming_args_rtx
,
710 copy_to_reg (get_arg_pointer_save_area (cfun
)));
715 #ifdef HAVE_builtin_setjmp_receiver
716 if (HAVE_builtin_setjmp_receiver
)
717 emit_insn (gen_builtin_setjmp_receiver (receiver_label
));
720 #ifdef HAVE_nonlocal_goto_receiver
721 if (HAVE_nonlocal_goto_receiver
)
722 emit_insn (gen_nonlocal_goto_receiver ());
727 /* @@@ This is a kludge. Not all machine descriptions define a blockage
728 insn, but we must not allow the code we just generated to be reordered
729 by scheduling. Specifically, the update of the frame pointer must
730 happen immediately, not later. So emit an ASM_INPUT to act as blockage
732 emit_insn (gen_rtx_ASM_INPUT (VOIDmode
, ""));
735 /* __builtin_longjmp is passed a pointer to an array of five words (not
736 all will be used on all machines). It operates similarly to the C
737 library function of the same name, but is more efficient. Much of
738 the code below is copied from the handling of non-local gotos. */
741 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
743 rtx fp
, lab
, stack
, insn
, last
;
744 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
746 if (setjmp_alias_set
== -1)
747 setjmp_alias_set
= new_alias_set ();
749 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
751 buf_addr
= force_reg (Pmode
, buf_addr
);
753 /* We used to store value in static_chain_rtx, but that fails if pointers
754 are smaller than integers. We instead require that the user must pass
755 a second argument of 1, because that is what builtin_setjmp will
756 return. This also makes EH slightly more efficient, since we are no
757 longer copying around a value that we don't care about. */
758 gcc_assert (value
== const1_rtx
);
760 last
= get_last_insn ();
761 #ifdef HAVE_builtin_longjmp
762 if (HAVE_builtin_longjmp
)
763 emit_insn (gen_builtin_longjmp (buf_addr
));
767 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
768 lab
= gen_rtx_MEM (Pmode
, plus_constant (buf_addr
,
769 GET_MODE_SIZE (Pmode
)));
771 stack
= gen_rtx_MEM (sa_mode
, plus_constant (buf_addr
,
772 2 * GET_MODE_SIZE (Pmode
)));
773 set_mem_alias_set (fp
, setjmp_alias_set
);
774 set_mem_alias_set (lab
, setjmp_alias_set
);
775 set_mem_alias_set (stack
, setjmp_alias_set
);
777 /* Pick up FP, label, and SP from the block and jump. This code is
778 from expand_goto in stmt.c; see there for detailed comments. */
779 #ifdef HAVE_nonlocal_goto
780 if (HAVE_nonlocal_goto
)
781 /* We have to pass a value to the nonlocal_goto pattern that will
782 get copied into the static_chain pointer, but it does not matter
783 what that value is, because builtin_setjmp does not use it. */
784 emit_insn (gen_nonlocal_goto (value
, lab
, stack
, fp
));
788 lab
= copy_to_reg (lab
);
790 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
791 gen_rtx_MEM (BLKmode
,
792 gen_rtx_SCRATCH (VOIDmode
))));
793 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
794 gen_rtx_MEM (BLKmode
,
795 hard_frame_pointer_rtx
)));
797 emit_move_insn (hard_frame_pointer_rtx
, fp
);
798 emit_stack_restore (SAVE_NONLOCAL
, stack
, NULL_RTX
);
800 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
801 emit_insn (gen_rtx_USE (VOIDmode
, stack_pointer_rtx
));
802 emit_indirect_jump (lab
);
806 /* Search backwards and mark the jump insn as a non-local goto.
807 Note that this precludes the use of __builtin_longjmp to a
808 __builtin_setjmp target in the same function. However, we've
809 already cautioned the user that these functions are for
810 internal exception handling use only. */
811 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
813 gcc_assert (insn
!= last
);
817 REG_NOTES (insn
) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO
, const0_rtx
,
821 else if (CALL_P (insn
))
826 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
827 and the address of the save area. */
830 expand_builtin_nonlocal_goto (tree arglist
)
832 tree t_label
, t_save_area
;
833 rtx r_label
, r_save_area
, r_fp
, r_sp
, insn
;
835 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
838 t_label
= TREE_VALUE (arglist
);
839 arglist
= TREE_CHAIN (arglist
);
840 t_save_area
= TREE_VALUE (arglist
);
842 r_label
= expand_normal (t_label
);
843 r_label
= convert_memory_address (Pmode
, r_label
);
844 r_save_area
= expand_normal (t_save_area
);
845 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
846 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
847 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
848 plus_constant (r_save_area
, GET_MODE_SIZE (Pmode
)));
850 current_function_has_nonlocal_goto
= 1;
852 #ifdef HAVE_nonlocal_goto
853 /* ??? We no longer need to pass the static chain value, afaik. */
854 if (HAVE_nonlocal_goto
)
855 emit_insn (gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
859 r_label
= copy_to_reg (r_label
);
861 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
862 gen_rtx_MEM (BLKmode
,
863 gen_rtx_SCRATCH (VOIDmode
))));
865 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
866 gen_rtx_MEM (BLKmode
,
867 hard_frame_pointer_rtx
)));
869 /* Restore frame pointer for containing function.
870 This sets the actual hard register used for the frame pointer
871 to the location of the function's incoming static chain info.
872 The non-local goto handler will then adjust it to contain the
873 proper value and reload the argument pointer, if needed. */
874 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
875 emit_stack_restore (SAVE_NONLOCAL
, r_sp
, NULL_RTX
);
877 /* USE of hard_frame_pointer_rtx added for consistency;
878 not clear if really needed. */
879 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
880 emit_insn (gen_rtx_USE (VOIDmode
, stack_pointer_rtx
));
881 emit_indirect_jump (r_label
);
884 /* Search backwards to the jump insn and mark it as a
886 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
890 REG_NOTES (insn
) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO
,
891 const0_rtx
, REG_NOTES (insn
));
894 else if (CALL_P (insn
))
901 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
902 (not all will be used on all machines) that was passed to __builtin_setjmp.
903 It updates the stack pointer in that block to correspond to the current
907 expand_builtin_update_setjmp_buf (rtx buf_addr
)
909 enum machine_mode sa_mode
= Pmode
;
913 #ifdef HAVE_save_stack_nonlocal
914 if (HAVE_save_stack_nonlocal
)
915 sa_mode
= insn_data
[(int) CODE_FOR_save_stack_nonlocal
].operand
[0].mode
;
917 #ifdef STACK_SAVEAREA_MODE
918 sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
922 = gen_rtx_MEM (sa_mode
,
925 plus_constant (buf_addr
, 2 * GET_MODE_SIZE (Pmode
))));
929 emit_insn (gen_setjmp ());
932 emit_stack_save (SAVE_NONLOCAL
, &stack_save
, NULL_RTX
);
935 /* Expand a call to __builtin_prefetch. For a target that does not support
936 data prefetch, evaluate the memory address argument in case it has side
940 expand_builtin_prefetch (tree arglist
)
942 tree arg0
, arg1
, arg2
;
945 if (!validate_arglist (arglist
, POINTER_TYPE
, 0))
948 arg0
= TREE_VALUE (arglist
);
949 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
950 zero (read) and argument 2 (locality) defaults to 3 (high degree of
952 if (TREE_CHAIN (arglist
))
954 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
955 if (TREE_CHAIN (TREE_CHAIN (arglist
)))
956 arg2
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
958 arg2
= build_int_cst (NULL_TREE
, 3);
962 arg1
= integer_zero_node
;
963 arg2
= build_int_cst (NULL_TREE
, 3);
966 /* Argument 0 is an address. */
967 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
969 /* Argument 1 (read/write flag) must be a compile-time constant int. */
970 if (TREE_CODE (arg1
) != INTEGER_CST
)
972 error ("second argument to %<__builtin_prefetch%> must be a constant");
973 arg1
= integer_zero_node
;
975 op1
= expand_normal (arg1
);
976 /* Argument 1 must be either zero or one. */
977 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
979 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
984 /* Argument 2 (locality) must be a compile-time constant int. */
985 if (TREE_CODE (arg2
) != INTEGER_CST
)
987 error ("third argument to %<__builtin_prefetch%> must be a constant");
988 arg2
= integer_zero_node
;
990 op2
= expand_normal (arg2
);
991 /* Argument 2 must be 0, 1, 2, or 3. */
992 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
994 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1001 if ((! (*insn_data
[(int) CODE_FOR_prefetch
].operand
[0].predicate
)
1003 insn_data
[(int) CODE_FOR_prefetch
].operand
[0].mode
))
1004 || (GET_MODE (op0
) != Pmode
))
1006 op0
= convert_memory_address (Pmode
, op0
);
1007 op0
= force_reg (Pmode
, op0
);
1009 emit_insn (gen_prefetch (op0
, op1
, op2
));
1013 /* Don't do anything with direct references to volatile memory, but
1014 generate code to handle other side effects. */
1015 if (!MEM_P (op0
) && side_effects_p (op0
))
1019 /* Get a MEM rtx for expression EXP which is the address of an operand
1020 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1021 the maximum length of the block of memory that might be accessed or
1025 get_memory_rtx (tree exp
, tree len
)
1027 rtx addr
= expand_expr (exp
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
1028 rtx mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
1030 /* Get an expression we can use to find the attributes to assign to MEM.
1031 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1032 we can. First remove any nops. */
1033 while ((TREE_CODE (exp
) == NOP_EXPR
|| TREE_CODE (exp
) == CONVERT_EXPR
1034 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
1035 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
1036 exp
= TREE_OPERAND (exp
, 0);
1038 if (TREE_CODE (exp
) == ADDR_EXPR
)
1039 exp
= TREE_OPERAND (exp
, 0);
1040 else if (POINTER_TYPE_P (TREE_TYPE (exp
)))
1041 exp
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (exp
)), exp
);
1045 /* Honor attributes derived from exp, except for the alias set
1046 (as builtin stringops may alias with anything) and the size
1047 (as stringops may access multiple array elements). */
1050 set_mem_attributes (mem
, exp
, 0);
1052 /* Allow the string and memory builtins to overflow from one
1053 field into another, see http://gcc.gnu.org/PR23561.
1054 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1055 memory accessed by the string or memory builtin will fit
1056 within the field. */
1057 if (MEM_EXPR (mem
) && TREE_CODE (MEM_EXPR (mem
)) == COMPONENT_REF
)
1059 tree mem_expr
= MEM_EXPR (mem
);
1060 HOST_WIDE_INT offset
= -1, length
= -1;
1063 while (TREE_CODE (inner
) == ARRAY_REF
1064 || TREE_CODE (inner
) == NOP_EXPR
1065 || TREE_CODE (inner
) == CONVERT_EXPR
1066 || TREE_CODE (inner
) == NON_LVALUE_EXPR
1067 || TREE_CODE (inner
) == VIEW_CONVERT_EXPR
1068 || TREE_CODE (inner
) == SAVE_EXPR
)
1069 inner
= TREE_OPERAND (inner
, 0);
1071 gcc_assert (TREE_CODE (inner
) == COMPONENT_REF
);
1073 if (MEM_OFFSET (mem
)
1074 && GET_CODE (MEM_OFFSET (mem
)) == CONST_INT
)
1075 offset
= INTVAL (MEM_OFFSET (mem
));
1077 if (offset
>= 0 && len
&& host_integerp (len
, 0))
1078 length
= tree_low_cst (len
, 0);
1080 while (TREE_CODE (inner
) == COMPONENT_REF
)
1082 tree field
= TREE_OPERAND (inner
, 1);
1083 gcc_assert (! DECL_BIT_FIELD (field
));
1084 gcc_assert (TREE_CODE (mem_expr
) == COMPONENT_REF
);
1085 gcc_assert (field
== TREE_OPERAND (mem_expr
, 1));
1088 && TYPE_SIZE_UNIT (TREE_TYPE (inner
))
1089 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner
)), 0))
1092 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner
)), 0);
1093 /* If we can prove the memory starting at XEXP (mem, 0)
1094 and ending at XEXP (mem, 0) + LENGTH will fit into
1095 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1098 && offset
+ length
<= size
)
1103 && host_integerp (DECL_FIELD_OFFSET (field
), 0))
1104 offset
+= tree_low_cst (DECL_FIELD_OFFSET (field
), 0)
1105 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1)
1113 mem_expr
= TREE_OPERAND (mem_expr
, 0);
1114 inner
= TREE_OPERAND (inner
, 0);
1117 if (mem_expr
== NULL
)
1119 if (mem_expr
!= MEM_EXPR (mem
))
1121 set_mem_expr (mem
, mem_expr
);
1122 set_mem_offset (mem
, offset
>= 0 ? GEN_INT (offset
) : NULL_RTX
);
1125 set_mem_alias_set (mem
, 0);
1126 set_mem_size (mem
, NULL_RTX
);
1132 /* Built-in functions to perform an untyped call and return. */
1134 /* For each register that may be used for calling a function, this
1135 gives a mode used to copy the register's value. VOIDmode indicates
1136 the register is not used for calling a function. If the machine
1137 has register windows, this gives only the outbound registers.
1138 INCOMING_REGNO gives the corresponding inbound register. */
1139 static enum machine_mode apply_args_mode
[FIRST_PSEUDO_REGISTER
];
1141 /* For each register that may be used for returning values, this gives
1142 a mode used to copy the register's value. VOIDmode indicates the
1143 register is not used for returning values. If the machine has
1144 register windows, this gives only the outbound registers.
1145 INCOMING_REGNO gives the corresponding inbound register. */
1146 static enum machine_mode apply_result_mode
[FIRST_PSEUDO_REGISTER
];
1148 /* For each register that may be used for calling a function, this
1149 gives the offset of that register into the block returned by
1150 __builtin_apply_args. 0 indicates that the register is not
1151 used for calling a function. */
1152 static int apply_args_reg_offset
[FIRST_PSEUDO_REGISTER
];
1154 /* Return the size required for the block returned by __builtin_apply_args,
1155 and initialize apply_args_mode. */
1158 apply_args_size (void)
1160 static int size
= -1;
1163 enum machine_mode mode
;
1165 /* The values computed by this function never change. */
1168 /* The first value is the incoming arg-pointer. */
1169 size
= GET_MODE_SIZE (Pmode
);
1171 /* The second value is the structure value address unless this is
1172 passed as an "invisible" first argument. */
1173 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1174 size
+= GET_MODE_SIZE (Pmode
);
1176 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1177 if (FUNCTION_ARG_REGNO_P (regno
))
1179 mode
= reg_raw_mode
[regno
];
1181 gcc_assert (mode
!= VOIDmode
);
1183 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1184 if (size
% align
!= 0)
1185 size
= CEIL (size
, align
) * align
;
1186 apply_args_reg_offset
[regno
] = size
;
1187 size
+= GET_MODE_SIZE (mode
);
1188 apply_args_mode
[regno
] = mode
;
1192 apply_args_mode
[regno
] = VOIDmode
;
1193 apply_args_reg_offset
[regno
] = 0;
1199 /* Return the size required for the block returned by __builtin_apply,
1200 and initialize apply_result_mode. */
1203 apply_result_size (void)
1205 static int size
= -1;
1207 enum machine_mode mode
;
1209 /* The values computed by this function never change. */
1214 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1215 if (FUNCTION_VALUE_REGNO_P (regno
))
1217 mode
= reg_raw_mode
[regno
];
1219 gcc_assert (mode
!= VOIDmode
);
1221 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1222 if (size
% align
!= 0)
1223 size
= CEIL (size
, align
) * align
;
1224 size
+= GET_MODE_SIZE (mode
);
1225 apply_result_mode
[regno
] = mode
;
1228 apply_result_mode
[regno
] = VOIDmode
;
1230 /* Allow targets that use untyped_call and untyped_return to override
1231 the size so that machine-specific information can be stored here. */
1232 #ifdef APPLY_RESULT_SIZE
1233 size
= APPLY_RESULT_SIZE
;
1239 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1240 /* Create a vector describing the result block RESULT. If SAVEP is true,
1241 the result block is used to save the values; otherwise it is used to
1242 restore the values. */
1245 result_vector (int savep
, rtx result
)
1247 int regno
, size
, align
, nelts
;
1248 enum machine_mode mode
;
1250 rtx
*savevec
= alloca (FIRST_PSEUDO_REGISTER
* sizeof (rtx
));
1253 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1254 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1256 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1257 if (size
% align
!= 0)
1258 size
= CEIL (size
, align
) * align
;
1259 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
1260 mem
= adjust_address (result
, mode
, size
);
1261 savevec
[nelts
++] = (savep
1262 ? gen_rtx_SET (VOIDmode
, mem
, reg
)
1263 : gen_rtx_SET (VOIDmode
, reg
, mem
));
1264 size
+= GET_MODE_SIZE (mode
);
1266 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
1268 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1270 /* Save the state required to perform an untyped call with the same
1271 arguments as were passed to the current function. */
1274 expand_builtin_apply_args_1 (void)
1277 int size
, align
, regno
;
1278 enum machine_mode mode
;
1279 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
1281 /* Create a block where the arg-pointer, structure value address,
1282 and argument registers can be saved. */
1283 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
1285 /* Walk past the arg-pointer and structure value address. */
1286 size
= GET_MODE_SIZE (Pmode
);
1287 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1288 size
+= GET_MODE_SIZE (Pmode
);
1290 /* Save each register used in calling a function to the block. */
1291 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1292 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1294 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1295 if (size
% align
!= 0)
1296 size
= CEIL (size
, align
) * align
;
1298 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1300 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
1301 size
+= GET_MODE_SIZE (mode
);
1304 /* Save the arg pointer to the block. */
1305 tem
= copy_to_reg (virtual_incoming_args_rtx
);
1306 #ifdef STACK_GROWS_DOWNWARD
1307 /* We need the pointer as the caller actually passed them to us, not
1308 as we might have pretended they were passed. Make sure it's a valid
1309 operand, as emit_move_insn isn't expected to handle a PLUS. */
1311 = force_operand (plus_constant (tem
, current_function_pretend_args_size
),
1314 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
1316 size
= GET_MODE_SIZE (Pmode
);
1318 /* Save the structure value address unless this is passed as an
1319 "invisible" first argument. */
1320 if (struct_incoming_value
)
1322 emit_move_insn (adjust_address (registers
, Pmode
, size
),
1323 copy_to_reg (struct_incoming_value
));
1324 size
+= GET_MODE_SIZE (Pmode
);
1327 /* Return the address of the block. */
1328 return copy_addr_to_reg (XEXP (registers
, 0));
1331 /* __builtin_apply_args returns block of memory allocated on
1332 the stack into which is stored the arg pointer, structure
1333 value address, static chain, and all the registers that might
1334 possibly be used in performing a function call. The code is
1335 moved to the start of the function so the incoming values are
1339 expand_builtin_apply_args (void)
1341 /* Don't do __builtin_apply_args more than once in a function.
1342 Save the result of the first call and reuse it. */
1343 if (apply_args_value
!= 0)
1344 return apply_args_value
;
1346 /* When this function is called, it means that registers must be
1347 saved on entry to this function. So we migrate the
1348 call to the first insn of this function. */
1353 temp
= expand_builtin_apply_args_1 ();
1357 apply_args_value
= temp
;
1359 /* Put the insns after the NOTE that starts the function.
1360 If this is inside a start_sequence, make the outer-level insn
1361 chain current, so the code is placed at the start of the
1363 push_topmost_sequence ();
1364 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
1365 pop_topmost_sequence ();
1370 /* Perform an untyped call and save the state required to perform an
1371 untyped return of whatever value was returned by the given function. */
1374 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
1376 int size
, align
, regno
;
1377 enum machine_mode mode
;
1378 rtx incoming_args
, result
, reg
, dest
, src
, call_insn
;
1379 rtx old_stack_level
= 0;
1380 rtx call_fusage
= 0;
1381 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
1383 arguments
= convert_memory_address (Pmode
, arguments
);
1385 /* Create a block where the return registers can be saved. */
1386 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
1388 /* Fetch the arg pointer from the ARGUMENTS block. */
1389 incoming_args
= gen_reg_rtx (Pmode
);
1390 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
1391 #ifndef STACK_GROWS_DOWNWARD
1392 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
1393 incoming_args
, 0, OPTAB_LIB_WIDEN
);
1396 /* Push a new argument block and copy the arguments. Do not allow
1397 the (potential) memcpy call below to interfere with our stack
1399 do_pending_stack_adjust ();
1402 /* Save the stack with nonlocal if available. */
1403 #ifdef HAVE_save_stack_nonlocal
1404 if (HAVE_save_stack_nonlocal
)
1405 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
, NULL_RTX
);
1408 emit_stack_save (SAVE_BLOCK
, &old_stack_level
, NULL_RTX
);
1410 /* Allocate a block of memory onto the stack and copy the memory
1411 arguments to the outgoing arguments address. */
1412 allocate_dynamic_stack_space (argsize
, 0, BITS_PER_UNIT
);
1413 dest
= virtual_outgoing_args_rtx
;
1414 #ifndef STACK_GROWS_DOWNWARD
1415 if (GET_CODE (argsize
) == CONST_INT
)
1416 dest
= plus_constant (dest
, -INTVAL (argsize
));
1418 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
1420 dest
= gen_rtx_MEM (BLKmode
, dest
);
1421 set_mem_align (dest
, PARM_BOUNDARY
);
1422 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
1423 set_mem_align (src
, PARM_BOUNDARY
);
1424 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
1426 /* Refer to the argument block. */
1428 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
1429 set_mem_align (arguments
, PARM_BOUNDARY
);
1431 /* Walk past the arg-pointer and structure value address. */
1432 size
= GET_MODE_SIZE (Pmode
);
1434 size
+= GET_MODE_SIZE (Pmode
);
1436 /* Restore each of the registers previously saved. Make USE insns
1437 for each of these registers for use in making the call. */
1438 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1439 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1441 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1442 if (size
% align
!= 0)
1443 size
= CEIL (size
, align
) * align
;
1444 reg
= gen_rtx_REG (mode
, regno
);
1445 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
1446 use_reg (&call_fusage
, reg
);
1447 size
+= GET_MODE_SIZE (mode
);
1450 /* Restore the structure value address unless this is passed as an
1451 "invisible" first argument. */
1452 size
= GET_MODE_SIZE (Pmode
);
1455 rtx value
= gen_reg_rtx (Pmode
);
1456 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
1457 emit_move_insn (struct_value
, value
);
1458 if (REG_P (struct_value
))
1459 use_reg (&call_fusage
, struct_value
);
1460 size
+= GET_MODE_SIZE (Pmode
);
1463 /* All arguments and registers used for the call are set up by now! */
1464 function
= prepare_call_address (function
, NULL
, &call_fusage
, 0, 0);
1466 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1467 and we don't want to load it into a register as an optimization,
1468 because prepare_call_address already did it if it should be done. */
1469 if (GET_CODE (function
) != SYMBOL_REF
)
1470 function
= memory_address (FUNCTION_MODE
, function
);
1472 /* Generate the actual call instruction and save the return value. */
1473 #ifdef HAVE_untyped_call
1474 if (HAVE_untyped_call
)
1475 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE
, function
),
1476 result
, result_vector (1, result
)));
1479 #ifdef HAVE_call_value
1480 if (HAVE_call_value
)
1484 /* Locate the unique return register. It is not possible to
1485 express a call that sets more than one return register using
1486 call_value; use untyped_call for that. In fact, untyped_call
1487 only needs to save the return registers in the given block. */
1488 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1489 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1491 gcc_assert (!valreg
); /* HAVE_untyped_call required. */
1493 valreg
= gen_rtx_REG (mode
, regno
);
1496 emit_call_insn (GEN_CALL_VALUE (valreg
,
1497 gen_rtx_MEM (FUNCTION_MODE
, function
),
1498 const0_rtx
, NULL_RTX
, const0_rtx
));
1500 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
1506 /* Find the CALL insn we just emitted, and attach the register usage
1508 call_insn
= last_call_insn ();
1509 add_function_usage_to (call_insn
, call_fusage
);
1511 /* Restore the stack. */
1512 #ifdef HAVE_save_stack_nonlocal
1513 if (HAVE_save_stack_nonlocal
)
1514 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
, NULL_RTX
);
1517 emit_stack_restore (SAVE_BLOCK
, old_stack_level
, NULL_RTX
);
1521 /* Return the address of the result block. */
1522 result
= copy_addr_to_reg (XEXP (result
, 0));
1523 return convert_memory_address (ptr_mode
, result
);
1526 /* Perform an untyped return. */
1529 expand_builtin_return (rtx result
)
1531 int size
, align
, regno
;
1532 enum machine_mode mode
;
1534 rtx call_fusage
= 0;
1536 result
= convert_memory_address (Pmode
, result
);
1538 apply_result_size ();
1539 result
= gen_rtx_MEM (BLKmode
, result
);
1541 #ifdef HAVE_untyped_return
1542 if (HAVE_untyped_return
)
1544 emit_jump_insn (gen_untyped_return (result
, result_vector (0, result
)));
1550 /* Restore the return value and note that each value is used. */
1552 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1553 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1555 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1556 if (size
% align
!= 0)
1557 size
= CEIL (size
, align
) * align
;
1558 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1559 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
1561 push_to_sequence (call_fusage
);
1562 emit_insn (gen_rtx_USE (VOIDmode
, reg
));
1563 call_fusage
= get_insns ();
1565 size
+= GET_MODE_SIZE (mode
);
1568 /* Put the USE insns before the return. */
1569 emit_insn (call_fusage
);
1571 /* Return whatever values was restored by jumping directly to the end
1573 expand_naked_return ();
1576 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1578 static enum type_class
1579 type_to_class (tree type
)
1581 switch (TREE_CODE (type
))
1583 case VOID_TYPE
: return void_type_class
;
1584 case INTEGER_TYPE
: return integer_type_class
;
1585 case ENUMERAL_TYPE
: return enumeral_type_class
;
1586 case BOOLEAN_TYPE
: return boolean_type_class
;
1587 case POINTER_TYPE
: return pointer_type_class
;
1588 case REFERENCE_TYPE
: return reference_type_class
;
1589 case OFFSET_TYPE
: return offset_type_class
;
1590 case REAL_TYPE
: return real_type_class
;
1591 case COMPLEX_TYPE
: return complex_type_class
;
1592 case FUNCTION_TYPE
: return function_type_class
;
1593 case METHOD_TYPE
: return method_type_class
;
1594 case RECORD_TYPE
: return record_type_class
;
1596 case QUAL_UNION_TYPE
: return union_type_class
;
1597 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
1598 ? string_type_class
: array_type_class
);
1599 case LANG_TYPE
: return lang_type_class
;
1600 default: return no_type_class
;
1604 /* Expand a call to __builtin_classify_type with arguments found in
1608 expand_builtin_classify_type (tree arglist
)
1611 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist
))));
1612 return GEN_INT (no_type_class
);
1615 /* This helper macro, meant to be used in mathfn_built_in below,
1616 determines which among a set of three builtin math functions is
1617 appropriate for a given type mode. The `F' and `L' cases are
1618 automatically generated from the `double' case. */
1619 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1620 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1621 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1622 fcodel = BUILT_IN_MATHFN##L ; break;
1624 /* Return mathematic function equivalent to FN but operating directly
1625 on TYPE, if available. If we can't do the conversion, return zero. */
1627 mathfn_built_in (tree type
, enum built_in_function fn
)
1629 enum built_in_function fcode
, fcodef
, fcodel
;
1633 CASE_MATHFN (BUILT_IN_ACOS
)
1634 CASE_MATHFN (BUILT_IN_ACOSH
)
1635 CASE_MATHFN (BUILT_IN_ASIN
)
1636 CASE_MATHFN (BUILT_IN_ASINH
)
1637 CASE_MATHFN (BUILT_IN_ATAN
)
1638 CASE_MATHFN (BUILT_IN_ATAN2
)
1639 CASE_MATHFN (BUILT_IN_ATANH
)
1640 CASE_MATHFN (BUILT_IN_CBRT
)
1641 CASE_MATHFN (BUILT_IN_CEIL
)
1642 CASE_MATHFN (BUILT_IN_COPYSIGN
)
1643 CASE_MATHFN (BUILT_IN_COS
)
1644 CASE_MATHFN (BUILT_IN_COSH
)
1645 CASE_MATHFN (BUILT_IN_DREM
)
1646 CASE_MATHFN (BUILT_IN_ERF
)
1647 CASE_MATHFN (BUILT_IN_ERFC
)
1648 CASE_MATHFN (BUILT_IN_EXP
)
1649 CASE_MATHFN (BUILT_IN_EXP10
)
1650 CASE_MATHFN (BUILT_IN_EXP2
)
1651 CASE_MATHFN (BUILT_IN_EXPM1
)
1652 CASE_MATHFN (BUILT_IN_FABS
)
1653 CASE_MATHFN (BUILT_IN_FDIM
)
1654 CASE_MATHFN (BUILT_IN_FLOOR
)
1655 CASE_MATHFN (BUILT_IN_FMA
)
1656 CASE_MATHFN (BUILT_IN_FMAX
)
1657 CASE_MATHFN (BUILT_IN_FMIN
)
1658 CASE_MATHFN (BUILT_IN_FMOD
)
1659 CASE_MATHFN (BUILT_IN_FREXP
)
1660 CASE_MATHFN (BUILT_IN_GAMMA
)
1661 CASE_MATHFN (BUILT_IN_HUGE_VAL
)
1662 CASE_MATHFN (BUILT_IN_HYPOT
)
1663 CASE_MATHFN (BUILT_IN_ILOGB
)
1664 CASE_MATHFN (BUILT_IN_INF
)
1665 CASE_MATHFN (BUILT_IN_J0
)
1666 CASE_MATHFN (BUILT_IN_J1
)
1667 CASE_MATHFN (BUILT_IN_JN
)
1668 CASE_MATHFN (BUILT_IN_LCEIL
)
1669 CASE_MATHFN (BUILT_IN_LDEXP
)
1670 CASE_MATHFN (BUILT_IN_LFLOOR
)
1671 CASE_MATHFN (BUILT_IN_LGAMMA
)
1672 CASE_MATHFN (BUILT_IN_LLCEIL
)
1673 CASE_MATHFN (BUILT_IN_LLFLOOR
)
1674 CASE_MATHFN (BUILT_IN_LLRINT
)
1675 CASE_MATHFN (BUILT_IN_LLROUND
)
1676 CASE_MATHFN (BUILT_IN_LOG
)
1677 CASE_MATHFN (BUILT_IN_LOG10
)
1678 CASE_MATHFN (BUILT_IN_LOG1P
)
1679 CASE_MATHFN (BUILT_IN_LOG2
)
1680 CASE_MATHFN (BUILT_IN_LOGB
)
1681 CASE_MATHFN (BUILT_IN_LRINT
)
1682 CASE_MATHFN (BUILT_IN_LROUND
)
1683 CASE_MATHFN (BUILT_IN_MODF
)
1684 CASE_MATHFN (BUILT_IN_NAN
)
1685 CASE_MATHFN (BUILT_IN_NANS
)
1686 CASE_MATHFN (BUILT_IN_NEARBYINT
)
1687 CASE_MATHFN (BUILT_IN_NEXTAFTER
)
1688 CASE_MATHFN (BUILT_IN_NEXTTOWARD
)
1689 CASE_MATHFN (BUILT_IN_POW
)
1690 CASE_MATHFN (BUILT_IN_POWI
)
1691 CASE_MATHFN (BUILT_IN_POW10
)
1692 CASE_MATHFN (BUILT_IN_REMAINDER
)
1693 CASE_MATHFN (BUILT_IN_REMQUO
)
1694 CASE_MATHFN (BUILT_IN_RINT
)
1695 CASE_MATHFN (BUILT_IN_ROUND
)
1696 CASE_MATHFN (BUILT_IN_SCALB
)
1697 CASE_MATHFN (BUILT_IN_SCALBLN
)
1698 CASE_MATHFN (BUILT_IN_SCALBN
)
1699 CASE_MATHFN (BUILT_IN_SIGNIFICAND
)
1700 CASE_MATHFN (BUILT_IN_SIN
)
1701 CASE_MATHFN (BUILT_IN_SINCOS
)
1702 CASE_MATHFN (BUILT_IN_SINH
)
1703 CASE_MATHFN (BUILT_IN_SQRT
)
1704 CASE_MATHFN (BUILT_IN_TAN
)
1705 CASE_MATHFN (BUILT_IN_TANH
)
1706 CASE_MATHFN (BUILT_IN_TGAMMA
)
1707 CASE_MATHFN (BUILT_IN_TRUNC
)
1708 CASE_MATHFN (BUILT_IN_Y0
)
1709 CASE_MATHFN (BUILT_IN_Y1
)
1710 CASE_MATHFN (BUILT_IN_YN
)
1716 if (TYPE_MAIN_VARIANT (type
) == double_type_node
)
1717 return implicit_built_in_decls
[fcode
];
1718 else if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1719 return implicit_built_in_decls
[fcodef
];
1720 else if (TYPE_MAIN_VARIANT (type
) == long_double_type_node
)
1721 return implicit_built_in_decls
[fcodel
];
1726 /* If errno must be maintained, expand the RTL to check if the result,
1727 TARGET, of a built-in function call, EXP, is NaN, and if so set
1731 expand_errno_check (tree exp
, rtx target
)
1733 rtx lab
= gen_label_rtx ();
1735 /* Test the result; if it is NaN, set errno=EDOM because
1736 the argument was not in the domain. */
1737 emit_cmp_and_jump_insns (target
, target
, EQ
, 0, GET_MODE (target
),
1741 /* If this built-in doesn't throw an exception, set errno directly. */
1742 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp
, 0), 0)))
1744 #ifdef GEN_ERRNO_RTX
1745 rtx errno_rtx
= GEN_ERRNO_RTX
;
1748 = gen_rtx_MEM (word_mode
, gen_rtx_SYMBOL_REF (Pmode
, "errno"));
1750 emit_move_insn (errno_rtx
, GEN_INT (TARGET_EDOM
));
1756 /* We can't set errno=EDOM directly; let the library call do it.
1757 Pop the arguments right away in case the call gets deleted. */
1759 expand_call (exp
, target
, 0);
1765 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1766 Return 0 if a normal call should be emitted rather than expanding the
1767 function in-line. EXP is the expression that is a call to the builtin
1768 function; if convenient, the result should be placed in TARGET.
1769 SUBTARGET may be used as the target for computing one of EXP's operands. */
1772 expand_builtin_mathfn (tree exp
, rtx target
, rtx subtarget
)
1774 optab builtin_optab
;
1775 rtx op0
, insns
, before_call
;
1776 tree fndecl
= get_callee_fndecl (exp
);
1777 tree arglist
= TREE_OPERAND (exp
, 1);
1778 enum machine_mode mode
;
1779 bool errno_set
= false;
1782 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
1785 arg
= TREE_VALUE (arglist
);
1787 switch (DECL_FUNCTION_CODE (fndecl
))
1789 CASE_FLT_FN (BUILT_IN_SQRT
):
1790 errno_set
= ! tree_expr_nonnegative_p (arg
);
1791 builtin_optab
= sqrt_optab
;
1793 CASE_FLT_FN (BUILT_IN_EXP
):
1794 errno_set
= true; builtin_optab
= exp_optab
; break;
1795 CASE_FLT_FN (BUILT_IN_EXP10
):
1796 CASE_FLT_FN (BUILT_IN_POW10
):
1797 errno_set
= true; builtin_optab
= exp10_optab
; break;
1798 CASE_FLT_FN (BUILT_IN_EXP2
):
1799 errno_set
= true; builtin_optab
= exp2_optab
; break;
1800 CASE_FLT_FN (BUILT_IN_EXPM1
):
1801 errno_set
= true; builtin_optab
= expm1_optab
; break;
1802 CASE_FLT_FN (BUILT_IN_LOGB
):
1803 errno_set
= true; builtin_optab
= logb_optab
; break;
1804 CASE_FLT_FN (BUILT_IN_ILOGB
):
1805 errno_set
= true; builtin_optab
= ilogb_optab
; break;
1806 CASE_FLT_FN (BUILT_IN_LOG
):
1807 errno_set
= true; builtin_optab
= log_optab
; break;
1808 CASE_FLT_FN (BUILT_IN_LOG10
):
1809 errno_set
= true; builtin_optab
= log10_optab
; break;
1810 CASE_FLT_FN (BUILT_IN_LOG2
):
1811 errno_set
= true; builtin_optab
= log2_optab
; break;
1812 CASE_FLT_FN (BUILT_IN_LOG1P
):
1813 errno_set
= true; builtin_optab
= log1p_optab
; break;
1814 CASE_FLT_FN (BUILT_IN_ASIN
):
1815 builtin_optab
= asin_optab
; break;
1816 CASE_FLT_FN (BUILT_IN_ACOS
):
1817 builtin_optab
= acos_optab
; break;
1818 CASE_FLT_FN (BUILT_IN_TAN
):
1819 builtin_optab
= tan_optab
; break;
1820 CASE_FLT_FN (BUILT_IN_ATAN
):
1821 builtin_optab
= atan_optab
; break;
1822 CASE_FLT_FN (BUILT_IN_FLOOR
):
1823 builtin_optab
= floor_optab
; break;
1824 CASE_FLT_FN (BUILT_IN_CEIL
):
1825 builtin_optab
= ceil_optab
; break;
1826 CASE_FLT_FN (BUILT_IN_TRUNC
):
1827 builtin_optab
= btrunc_optab
; break;
1828 CASE_FLT_FN (BUILT_IN_ROUND
):
1829 builtin_optab
= round_optab
; break;
1830 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
1831 builtin_optab
= nearbyint_optab
; break;
1832 CASE_FLT_FN (BUILT_IN_RINT
):
1833 builtin_optab
= rint_optab
; break;
1834 CASE_FLT_FN (BUILT_IN_LRINT
):
1835 CASE_FLT_FN (BUILT_IN_LLRINT
):
1836 builtin_optab
= lrint_optab
; break;
1841 /* Make a suitable register to place result in. */
1842 mode
= TYPE_MODE (TREE_TYPE (exp
));
1844 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
1847 /* Before working hard, check whether the instruction is available. */
1848 if (builtin_optab
->handlers
[(int) mode
].insn_code
!= CODE_FOR_nothing
)
1850 target
= gen_reg_rtx (mode
);
1852 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1853 need to expand the argument again. This way, we will not perform
1854 side-effects more the once. */
1855 narg
= builtin_save_expr (arg
);
1859 arglist
= build_tree_list (NULL_TREE
, arg
);
1860 exp
= build_function_call_expr (fndecl
, arglist
);
1863 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
1867 /* Compute into TARGET.
1868 Set TARGET to wherever the result comes back. */
1869 target
= expand_unop (mode
, builtin_optab
, op0
, target
, 0);
1874 expand_errno_check (exp
, target
);
1876 /* Output the entire sequence. */
1877 insns
= get_insns ();
1883 /* If we were unable to expand via the builtin, stop the sequence
1884 (without outputting the insns) and call to the library function
1885 with the stabilized argument list. */
1889 before_call
= get_last_insn ();
1891 target
= expand_call (exp
, target
, target
== const0_rtx
);
1893 /* If this is a sqrt operation and we don't care about errno, try to
1894 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1895 This allows the semantics of the libcall to be visible to the RTL
1897 if (builtin_optab
== sqrt_optab
&& !errno_set
)
1899 /* Search backwards through the insns emitted by expand_call looking
1900 for the instruction with the REG_RETVAL note. */
1901 rtx last
= get_last_insn ();
1902 while (last
!= before_call
)
1904 if (find_reg_note (last
, REG_RETVAL
, NULL
))
1906 rtx note
= find_reg_note (last
, REG_EQUAL
, NULL
);
1907 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1908 two elements, i.e. symbol_ref(sqrt) and the operand. */
1910 && GET_CODE (note
) == EXPR_LIST
1911 && GET_CODE (XEXP (note
, 0)) == EXPR_LIST
1912 && XEXP (XEXP (note
, 0), 1) != NULL_RTX
1913 && XEXP (XEXP (XEXP (note
, 0), 1), 1) == NULL_RTX
)
1915 rtx operand
= XEXP (XEXP (XEXP (note
, 0), 1), 0);
1916 /* Check operand is a register with expected mode. */
1919 && GET_MODE (operand
) == mode
)
1921 /* Replace the REG_EQUAL note with a SQRT rtx. */
1922 rtx equiv
= gen_rtx_SQRT (mode
, operand
);
1923 set_unique_reg_note (last
, REG_EQUAL
, equiv
);
1928 last
= PREV_INSN (last
);
1935 /* Expand a call to the builtin binary math functions (pow and atan2).
1936 Return 0 if a normal call should be emitted rather than expanding the
1937 function in-line. EXP is the expression that is a call to the builtin
1938 function; if convenient, the result should be placed in TARGET.
1939 SUBTARGET may be used as the target for computing one of EXP's
1943 expand_builtin_mathfn_2 (tree exp
, rtx target
, rtx subtarget
)
1945 optab builtin_optab
;
1946 rtx op0
, op1
, insns
;
1947 int op1_type
= REAL_TYPE
;
1948 tree fndecl
= get_callee_fndecl (exp
);
1949 tree arglist
= TREE_OPERAND (exp
, 1);
1950 tree arg0
, arg1
, temp
, narg
;
1951 enum machine_mode mode
;
1952 bool errno_set
= true;
1955 if ((DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_LDEXP
)
1956 || (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_LDEXPF
)
1957 || (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_LDEXPL
))
1958 op1_type
= INTEGER_TYPE
;
1960 if (!validate_arglist (arglist
, REAL_TYPE
, op1_type
, VOID_TYPE
))
1963 arg0
= TREE_VALUE (arglist
);
1964 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
1966 switch (DECL_FUNCTION_CODE (fndecl
))
1968 CASE_FLT_FN (BUILT_IN_POW
):
1969 builtin_optab
= pow_optab
; break;
1970 CASE_FLT_FN (BUILT_IN_ATAN2
):
1971 builtin_optab
= atan2_optab
; break;
1972 CASE_FLT_FN (BUILT_IN_LDEXP
):
1973 builtin_optab
= ldexp_optab
; break;
1974 CASE_FLT_FN (BUILT_IN_FMOD
):
1975 builtin_optab
= fmod_optab
; break;
1976 CASE_FLT_FN (BUILT_IN_DREM
):
1977 builtin_optab
= drem_optab
; break;
1982 /* Make a suitable register to place result in. */
1983 mode
= TYPE_MODE (TREE_TYPE (exp
));
1985 /* Before working hard, check whether the instruction is available. */
1986 if (builtin_optab
->handlers
[(int) mode
].insn_code
== CODE_FOR_nothing
)
1989 target
= gen_reg_rtx (mode
);
1991 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
1994 /* Always stabilize the argument list. */
1995 narg
= builtin_save_expr (arg1
);
1999 temp
= build_tree_list (NULL_TREE
, narg
);
2003 temp
= TREE_CHAIN (arglist
);
2005 narg
= builtin_save_expr (arg0
);
2009 arglist
= tree_cons (NULL_TREE
, narg
, temp
);
2013 arglist
= tree_cons (NULL_TREE
, arg0
, temp
);
2016 exp
= build_function_call_expr (fndecl
, arglist
);
2018 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2019 op1
= expand_normal (arg1
);
2023 /* Compute into TARGET.
2024 Set TARGET to wherever the result comes back. */
2025 target
= expand_binop (mode
, builtin_optab
, op0
, op1
,
2026 target
, 0, OPTAB_DIRECT
);
2028 /* If we were unable to expand via the builtin, stop the sequence
2029 (without outputting the insns) and call to the library function
2030 with the stabilized argument list. */
2034 return expand_call (exp
, target
, target
== const0_rtx
);
2038 expand_errno_check (exp
, target
);
2040 /* Output the entire sequence. */
2041 insns
= get_insns ();
2048 /* Expand a call to the builtin sin and cos math functions.
2049 Return 0 if a normal call should be emitted rather than expanding the
2050 function in-line. EXP is the expression that is a call to the builtin
2051 function; if convenient, the result should be placed in TARGET.
2052 SUBTARGET may be used as the target for computing one of EXP's
2056 expand_builtin_mathfn_3 (tree exp
, rtx target
, rtx subtarget
)
2058 optab builtin_optab
;
2060 tree fndecl
= get_callee_fndecl (exp
);
2061 tree arglist
= TREE_OPERAND (exp
, 1);
2062 enum machine_mode mode
;
2065 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
2068 arg
= TREE_VALUE (arglist
);
2070 switch (DECL_FUNCTION_CODE (fndecl
))
2072 CASE_FLT_FN (BUILT_IN_SIN
):
2073 CASE_FLT_FN (BUILT_IN_COS
):
2074 builtin_optab
= sincos_optab
; break;
2079 /* Make a suitable register to place result in. */
2080 mode
= TYPE_MODE (TREE_TYPE (exp
));
2082 /* Check if sincos insn is available, otherwise fallback
2083 to sin or cos insn. */
2084 if (builtin_optab
->handlers
[(int) mode
].insn_code
== CODE_FOR_nothing
) {
2085 switch (DECL_FUNCTION_CODE (fndecl
))
2087 CASE_FLT_FN (BUILT_IN_SIN
):
2088 builtin_optab
= sin_optab
; break;
2089 CASE_FLT_FN (BUILT_IN_COS
):
2090 builtin_optab
= cos_optab
; break;
2096 /* Before working hard, check whether the instruction is available. */
2097 if (builtin_optab
->handlers
[(int) mode
].insn_code
!= CODE_FOR_nothing
)
2099 target
= gen_reg_rtx (mode
);
2101 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2102 need to expand the argument again. This way, we will not perform
2103 side-effects more the once. */
2104 narg
= save_expr (arg
);
2108 arglist
= build_tree_list (NULL_TREE
, arg
);
2109 exp
= build_function_call_expr (fndecl
, arglist
);
2112 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
2116 /* Compute into TARGET.
2117 Set TARGET to wherever the result comes back. */
2118 if (builtin_optab
== sincos_optab
)
2122 switch (DECL_FUNCTION_CODE (fndecl
))
2124 CASE_FLT_FN (BUILT_IN_SIN
):
2125 result
= expand_twoval_unop (builtin_optab
, op0
, 0, target
, 0);
2127 CASE_FLT_FN (BUILT_IN_COS
):
2128 result
= expand_twoval_unop (builtin_optab
, op0
, target
, 0, 0);
2133 gcc_assert (result
);
2137 target
= expand_unop (mode
, builtin_optab
, op0
, target
, 0);
2142 /* Output the entire sequence. */
2143 insns
= get_insns ();
2149 /* If we were unable to expand via the builtin, stop the sequence
2150 (without outputting the insns) and call to the library function
2151 with the stabilized argument list. */
2155 target
= expand_call (exp
, target
, target
== const0_rtx
);
2160 /* Expand a call to the builtin sincos math function.
2161 Return 0 if a normal call should be emitted rather than expanding the
2162 function in-line. EXP is the expression that is a call to the builtin
2166 expand_builtin_sincos (tree exp
)
2168 rtx op0
, op1
, op2
, target1
, target2
;
2169 tree arglist
= TREE_OPERAND (exp
, 1);
2170 enum machine_mode mode
;
2171 tree arg
, sinp
, cosp
;
2174 if (!validate_arglist (arglist
, REAL_TYPE
,
2175 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2178 arg
= TREE_VALUE (arglist
);
2179 sinp
= TREE_VALUE (TREE_CHAIN (arglist
));
2180 cosp
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
2182 /* Make a suitable register to place result in. */
2183 mode
= TYPE_MODE (TREE_TYPE (arg
));
2185 /* Check if sincos insn is available, otherwise emit the call. */
2186 if (sincos_optab
->handlers
[(int) mode
].insn_code
== CODE_FOR_nothing
)
2189 target1
= gen_reg_rtx (mode
);
2190 target2
= gen_reg_rtx (mode
);
2192 op0
= expand_normal (arg
);
2193 op1
= expand_normal (build_fold_indirect_ref (sinp
));
2194 op2
= expand_normal (build_fold_indirect_ref (cosp
));
2196 /* Compute into target1 and target2.
2197 Set TARGET to wherever the result comes back. */
2198 result
= expand_twoval_unop (sincos_optab
, op0
, target2
, target1
, 0);
2199 gcc_assert (result
);
2201 /* Move target1 and target2 to the memory locations indicated
2203 emit_move_insn (op1
, target1
);
2204 emit_move_insn (op2
, target2
);
2209 /* Expand a call to one of the builtin rounding functions (lfloor).
2210 If expanding via optab fails, lower expression to (int)(floor(x)).
2211 EXP is the expression that is a call to the builtin function;
2212 if convenient, the result should be placed in TARGET. SUBTARGET may
2213 be used as the target for computing one of EXP's operands. */
2216 expand_builtin_int_roundingfn (tree exp
, rtx target
, rtx subtarget
)
2218 optab builtin_optab
;
2219 rtx op0
, insns
, tmp
;
2220 tree fndecl
= get_callee_fndecl (exp
);
2221 tree arglist
= TREE_OPERAND (exp
, 1);
2222 enum built_in_function fallback_fn
;
2223 tree fallback_fndecl
;
2224 enum machine_mode mode
;
2227 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
2230 arg
= TREE_VALUE (arglist
);
2232 switch (DECL_FUNCTION_CODE (fndecl
))
2234 CASE_FLT_FN (BUILT_IN_LCEIL
):
2235 CASE_FLT_FN (BUILT_IN_LLCEIL
):
2236 builtin_optab
= lceil_optab
;
2237 fallback_fn
= BUILT_IN_CEIL
;
2240 CASE_FLT_FN (BUILT_IN_LFLOOR
):
2241 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
2242 builtin_optab
= lfloor_optab
;
2243 fallback_fn
= BUILT_IN_FLOOR
;
2250 /* Make a suitable register to place result in. */
2251 mode
= TYPE_MODE (TREE_TYPE (exp
));
2253 /* Before working hard, check whether the instruction is available. */
2254 if (builtin_optab
->handlers
[(int) mode
].insn_code
!= CODE_FOR_nothing
)
2256 target
= gen_reg_rtx (mode
);
2258 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2259 need to expand the argument again. This way, we will not perform
2260 side-effects more the once. */
2261 narg
= builtin_save_expr (arg
);
2265 arglist
= build_tree_list (NULL_TREE
, arg
);
2266 exp
= build_function_call_expr (fndecl
, arglist
);
2269 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
2273 /* Compute into TARGET.
2274 Set TARGET to wherever the result comes back. */
2275 target
= expand_unop (mode
, builtin_optab
, op0
, target
, 0);
2279 /* Output the entire sequence. */
2280 insns
= get_insns ();
2286 /* If we were unable to expand via the builtin, stop the sequence
2287 (without outputting the insns). */
2291 /* Fall back to floating point rounding optab. */
2292 fallback_fndecl
= mathfn_built_in (TREE_TYPE (arg
), fallback_fn
);
2293 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2294 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2295 gcc_assert (fallback_fndecl
!= NULL_TREE
);
2296 exp
= build_function_call_expr (fallback_fndecl
, arglist
);
2298 tmp
= expand_normal (exp
);
2300 /* Truncate the result of floating point optab to integer
2301 via expand_fix (). */
2302 target
= gen_reg_rtx (mode
);
2303 expand_fix (target
, tmp
, 0);
2308 /* To evaluate powi(x,n), the floating point value x raised to the
2309 constant integer exponent n, we use a hybrid algorithm that
2310 combines the "window method" with look-up tables. For an
2311 introduction to exponentiation algorithms and "addition chains",
2312 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2313 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2314 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2315 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2317 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2318 multiplications to inline before calling the system library's pow
2319 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2320 so this default never requires calling pow, powf or powl. */
2322 #ifndef POWI_MAX_MULTS
2323 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2326 /* The size of the "optimal power tree" lookup table. All
2327 exponents less than this value are simply looked up in the
2328 powi_table below. This threshold is also used to size the
2329 cache of pseudo registers that hold intermediate results. */
2330 #define POWI_TABLE_SIZE 256
2332 /* The size, in bits of the window, used in the "window method"
2333 exponentiation algorithm. This is equivalent to a radix of
2334 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2335 #define POWI_WINDOW_SIZE 3
2337 /* The following table is an efficient representation of an
2338 "optimal power tree". For each value, i, the corresponding
2339 value, j, in the table states than an optimal evaluation
2340 sequence for calculating pow(x,i) can be found by evaluating
2341 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2342 100 integers is given in Knuth's "Seminumerical algorithms". */
2344 static const unsigned char powi_table
[POWI_TABLE_SIZE
] =
2346 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2347 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2348 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2349 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2350 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2351 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2352 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2353 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2354 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2355 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2356 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2357 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2358 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2359 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2360 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2361 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2362 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2363 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2364 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2365 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2366 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2367 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2368 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2369 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2370 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2371 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2372 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2373 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2374 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2375 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2376 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2377 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2381 /* Return the number of multiplications required to calculate
2382 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2383 subroutine of powi_cost. CACHE is an array indicating
2384 which exponents have already been calculated. */
2387 powi_lookup_cost (unsigned HOST_WIDE_INT n
, bool *cache
)
2389 /* If we've already calculated this exponent, then this evaluation
2390 doesn't require any additional multiplications. */
2395 return powi_lookup_cost (n
- powi_table
[n
], cache
)
2396 + powi_lookup_cost (powi_table
[n
], cache
) + 1;
2399 /* Return the number of multiplications required to calculate
2400 powi(x,n) for an arbitrary x, given the exponent N. This
2401 function needs to be kept in sync with expand_powi below. */
2404 powi_cost (HOST_WIDE_INT n
)
2406 bool cache
[POWI_TABLE_SIZE
];
2407 unsigned HOST_WIDE_INT digit
;
2408 unsigned HOST_WIDE_INT val
;
2414 /* Ignore the reciprocal when calculating the cost. */
2415 val
= (n
< 0) ? -n
: n
;
2417 /* Initialize the exponent cache. */
2418 memset (cache
, 0, POWI_TABLE_SIZE
* sizeof (bool));
2423 while (val
>= POWI_TABLE_SIZE
)
2427 digit
= val
& ((1 << POWI_WINDOW_SIZE
) - 1);
2428 result
+= powi_lookup_cost (digit
, cache
)
2429 + POWI_WINDOW_SIZE
+ 1;
2430 val
>>= POWI_WINDOW_SIZE
;
2439 return result
+ powi_lookup_cost (val
, cache
);
2442 /* Recursive subroutine of expand_powi. This function takes the array,
2443 CACHE, of already calculated exponents and an exponent N and returns
2444 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2447 expand_powi_1 (enum machine_mode mode
, unsigned HOST_WIDE_INT n
, rtx
*cache
)
2449 unsigned HOST_WIDE_INT digit
;
2453 if (n
< POWI_TABLE_SIZE
)
2458 target
= gen_reg_rtx (mode
);
2461 op0
= expand_powi_1 (mode
, n
- powi_table
[n
], cache
);
2462 op1
= expand_powi_1 (mode
, powi_table
[n
], cache
);
2466 target
= gen_reg_rtx (mode
);
2467 digit
= n
& ((1 << POWI_WINDOW_SIZE
) - 1);
2468 op0
= expand_powi_1 (mode
, n
- digit
, cache
);
2469 op1
= expand_powi_1 (mode
, digit
, cache
);
2473 target
= gen_reg_rtx (mode
);
2474 op0
= expand_powi_1 (mode
, n
>> 1, cache
);
2478 result
= expand_mult (mode
, op0
, op1
, target
, 0);
2479 if (result
!= target
)
2480 emit_move_insn (target
, result
);
2484 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2485 floating point operand in mode MODE, and N is the exponent. This
2486 function needs to be kept in sync with powi_cost above. */
2489 expand_powi (rtx x
, enum machine_mode mode
, HOST_WIDE_INT n
)
2491 unsigned HOST_WIDE_INT val
;
2492 rtx cache
[POWI_TABLE_SIZE
];
2496 return CONST1_RTX (mode
);
2498 val
= (n
< 0) ? -n
: n
;
2500 memset (cache
, 0, sizeof (cache
));
2503 result
= expand_powi_1 (mode
, (n
< 0) ? -n
: n
, cache
);
2505 /* If the original exponent was negative, reciprocate the result. */
2507 result
= expand_binop (mode
, sdiv_optab
, CONST1_RTX (mode
),
2508 result
, NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
2513 /* Expand a call to the pow built-in mathematical function. Return 0 if
2514 a normal call should be emitted rather than expanding the function
2515 in-line. EXP is the expression that is a call to the builtin
2516 function; if convenient, the result should be placed in TARGET. */
2519 expand_builtin_pow (tree exp
, rtx target
, rtx subtarget
)
2521 tree arglist
= TREE_OPERAND (exp
, 1);
2524 if (! validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
2527 arg0
= TREE_VALUE (arglist
);
2528 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
2530 if (TREE_CODE (arg1
) == REAL_CST
2531 && ! TREE_CONSTANT_OVERFLOW (arg1
))
2533 REAL_VALUE_TYPE cint
;
2537 c
= TREE_REAL_CST (arg1
);
2538 n
= real_to_integer (&c
);
2539 real_from_integer (&cint
, VOIDmode
, n
, n
< 0 ? -1 : 0, 0);
2540 if (real_identical (&c
, &cint
))
2542 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2543 Otherwise, check the number of multiplications required.
2544 Note that pow never sets errno for an integer exponent. */
2545 if ((n
>= -1 && n
<= 2)
2546 || (flag_unsafe_math_optimizations
2548 && powi_cost (n
) <= POWI_MAX_MULTS
))
2550 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
2551 rtx op
= expand_expr (arg0
, subtarget
, VOIDmode
, 0);
2552 op
= force_reg (mode
, op
);
2553 return expand_powi (op
, mode
, n
);
2558 if (! flag_unsafe_math_optimizations
)
2560 return expand_builtin_mathfn_2 (exp
, target
, subtarget
);
2563 /* Expand a call to the powi built-in mathematical function. Return 0 if
2564 a normal call should be emitted rather than expanding the function
2565 in-line. EXP is the expression that is a call to the builtin
2566 function; if convenient, the result should be placed in TARGET. */
2569 expand_builtin_powi (tree exp
, rtx target
, rtx subtarget
)
2571 tree arglist
= TREE_OPERAND (exp
, 1);
2574 enum machine_mode mode
;
2575 enum machine_mode mode2
;
2577 if (! validate_arglist (arglist
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2580 arg0
= TREE_VALUE (arglist
);
2581 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
2582 mode
= TYPE_MODE (TREE_TYPE (exp
));
2584 /* Handle constant power. */
2586 if (TREE_CODE (arg1
) == INTEGER_CST
2587 && ! TREE_CONSTANT_OVERFLOW (arg1
))
2589 HOST_WIDE_INT n
= TREE_INT_CST_LOW (arg1
);
2591 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2592 Otherwise, check the number of multiplications required. */
2593 if ((TREE_INT_CST_HIGH (arg1
) == 0
2594 || TREE_INT_CST_HIGH (arg1
) == -1)
2595 && ((n
>= -1 && n
<= 2)
2597 && powi_cost (n
) <= POWI_MAX_MULTS
)))
2599 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, 0);
2600 op0
= force_reg (mode
, op0
);
2601 return expand_powi (op0
, mode
, n
);
2605 /* Emit a libcall to libgcc. */
2607 /* Mode of the 2nd argument must match that of an int. */
2608 mode2
= mode_for_size (INT_TYPE_SIZE
, MODE_INT
, 0);
2610 if (target
== NULL_RTX
)
2611 target
= gen_reg_rtx (mode
);
2613 op0
= expand_expr (arg0
, subtarget
, mode
, 0);
2614 if (GET_MODE (op0
) != mode
)
2615 op0
= convert_to_mode (mode
, op0
, 0);
2616 op1
= expand_expr (arg1
, 0, mode2
, 0);
2617 if (GET_MODE (op1
) != mode2
)
2618 op1
= convert_to_mode (mode2
, op1
, 0);
2620 target
= emit_library_call_value (powi_optab
->handlers
[(int) mode
].libfunc
,
2621 target
, LCT_CONST_MAKE_BLOCK
, mode
, 2,
2622 op0
, mode
, op1
, mode2
);
2627 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2628 if we failed the caller should emit a normal call, otherwise
2629 try to get the result in TARGET, if convenient. */
2632 expand_builtin_strlen (tree arglist
, rtx target
,
2633 enum machine_mode target_mode
)
2635 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
2640 tree len
, src
= TREE_VALUE (arglist
);
2641 rtx result
, src_reg
, char_rtx
, before_strlen
;
2642 enum machine_mode insn_mode
= target_mode
, char_mode
;
2643 enum insn_code icode
= CODE_FOR_nothing
;
2646 /* If the length can be computed at compile-time, return it. */
2647 len
= c_strlen (src
, 0);
2649 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2651 /* If the length can be computed at compile-time and is constant
2652 integer, but there are side-effects in src, evaluate
2653 src for side-effects, then return len.
2654 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2655 can be optimized into: i++; x = 3; */
2656 len
= c_strlen (src
, 1);
2657 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
2659 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
2660 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2663 align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
2665 /* If SRC is not a pointer type, don't do this operation inline. */
2669 /* Bail out if we can't compute strlen in the right mode. */
2670 while (insn_mode
!= VOIDmode
)
2672 icode
= strlen_optab
->handlers
[(int) insn_mode
].insn_code
;
2673 if (icode
!= CODE_FOR_nothing
)
2676 insn_mode
= GET_MODE_WIDER_MODE (insn_mode
);
2678 if (insn_mode
== VOIDmode
)
2681 /* Make a place to write the result of the instruction. */
2685 && GET_MODE (result
) == insn_mode
2686 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
2687 result
= gen_reg_rtx (insn_mode
);
2689 /* Make a place to hold the source address. We will not expand
2690 the actual source until we are sure that the expansion will
2691 not fail -- there are trees that cannot be expanded twice. */
2692 src_reg
= gen_reg_rtx (Pmode
);
2694 /* Mark the beginning of the strlen sequence so we can emit the
2695 source operand later. */
2696 before_strlen
= get_last_insn ();
2698 char_rtx
= const0_rtx
;
2699 char_mode
= insn_data
[(int) icode
].operand
[2].mode
;
2700 if (! (*insn_data
[(int) icode
].operand
[2].predicate
) (char_rtx
,
2702 char_rtx
= copy_to_mode_reg (char_mode
, char_rtx
);
2704 pat
= GEN_FCN (icode
) (result
, gen_rtx_MEM (BLKmode
, src_reg
),
2705 char_rtx
, GEN_INT (align
));
2710 /* Now that we are assured of success, expand the source. */
2712 pat
= expand_expr (src
, src_reg
, ptr_mode
, EXPAND_NORMAL
);
2714 emit_move_insn (src_reg
, pat
);
2719 emit_insn_after (pat
, before_strlen
);
2721 emit_insn_before (pat
, get_insns ());
2723 /* Return the value in the proper mode for this function. */
2724 if (GET_MODE (result
) == target_mode
)
2726 else if (target
!= 0)
2727 convert_move (target
, result
, 0);
2729 target
= convert_to_mode (target_mode
, result
, 0);
2735 /* Expand a call to the strstr builtin. Return 0 if we failed the
2736 caller should emit a normal call, otherwise try to get the result
2737 in TARGET, if convenient (and in mode MODE if that's convenient). */
2740 expand_builtin_strstr (tree arglist
, tree type
, rtx target
, enum machine_mode mode
)
2742 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2744 tree result
= fold_builtin_strstr (arglist
, type
);
2746 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2751 /* Expand a call to the strchr builtin. Return 0 if we failed the
2752 caller should emit a normal call, otherwise try to get the result
2753 in TARGET, if convenient (and in mode MODE if that's convenient). */
2756 expand_builtin_strchr (tree arglist
, tree type
, rtx target
, enum machine_mode mode
)
2758 if (validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2760 tree result
= fold_builtin_strchr (arglist
, type
);
2762 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2764 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2769 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2770 caller should emit a normal call, otherwise try to get the result
2771 in TARGET, if convenient (and in mode MODE if that's convenient). */
2774 expand_builtin_strrchr (tree arglist
, tree type
, rtx target
, enum machine_mode mode
)
2776 if (validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2778 tree result
= fold_builtin_strrchr (arglist
, type
);
2780 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2785 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2786 caller should emit a normal call, otherwise try to get the result
2787 in TARGET, if convenient (and in mode MODE if that's convenient). */
2790 expand_builtin_strpbrk (tree arglist
, tree type
, rtx target
, enum machine_mode mode
)
2792 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2794 tree result
= fold_builtin_strpbrk (arglist
, type
);
2796 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2801 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2802 bytes from constant string DATA + OFFSET and return it as target
2806 builtin_memcpy_read_str (void *data
, HOST_WIDE_INT offset
,
2807 enum machine_mode mode
)
2809 const char *str
= (const char *) data
;
2811 gcc_assert (offset
>= 0
2812 && ((unsigned HOST_WIDE_INT
) offset
+ GET_MODE_SIZE (mode
)
2813 <= strlen (str
) + 1));
2815 return c_readstr (str
+ offset
, mode
);
2818 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2819 Return 0 if we failed, the caller should emit a normal call,
2820 otherwise try to get the result in TARGET, if convenient (and in
2821 mode MODE if that's convenient). */
2823 expand_builtin_memcpy (tree exp
, rtx target
, enum machine_mode mode
)
2825 tree fndecl
= get_callee_fndecl (exp
);
2826 tree arglist
= TREE_OPERAND (exp
, 1);
2827 if (!validate_arglist (arglist
,
2828 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2832 tree dest
= TREE_VALUE (arglist
);
2833 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
2834 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
2835 const char *src_str
;
2836 unsigned int src_align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
2837 unsigned int dest_align
2838 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
2839 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
2840 tree result
= fold_builtin_memory_op (arglist
, TREE_TYPE (TREE_TYPE (fndecl
)),
2845 while (TREE_CODE (result
) == COMPOUND_EXPR
)
2847 expand_expr (TREE_OPERAND (result
, 0), const0_rtx
, VOIDmode
,
2849 result
= TREE_OPERAND (result
, 1);
2851 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2854 /* If DEST is not a pointer type, call the normal function. */
2855 if (dest_align
== 0)
2858 /* If either SRC is not a pointer type, don't do this
2859 operation in-line. */
2863 dest_mem
= get_memory_rtx (dest
, len
);
2864 set_mem_align (dest_mem
, dest_align
);
2865 len_rtx
= expand_normal (len
);
2866 src_str
= c_getstr (src
);
2868 /* If SRC is a string constant and block move would be done
2869 by pieces, we can avoid loading the string from memory
2870 and only stored the computed constants. */
2872 && GET_CODE (len_rtx
) == CONST_INT
2873 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
2874 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
2875 (void *) src_str
, dest_align
))
2877 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
2878 builtin_memcpy_read_str
,
2879 (void *) src_str
, dest_align
, 0);
2880 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
2881 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
2885 src_mem
= get_memory_rtx (src
, len
);
2886 set_mem_align (src_mem
, src_align
);
2888 /* Copy word part most expediently. */
2889 dest_addr
= emit_block_move (dest_mem
, src_mem
, len_rtx
,
2890 CALL_EXPR_TAILCALL (exp
)
2891 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
);
2895 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
2896 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
2902 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2903 Return 0 if we failed; the caller should emit a normal call,
2904 otherwise try to get the result in TARGET, if convenient (and in
2905 mode MODE if that's convenient). If ENDP is 0 return the
2906 destination pointer, if ENDP is 1 return the end pointer ala
2907 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2911 expand_builtin_mempcpy (tree arglist
, tree type
, rtx target
, enum machine_mode mode
,
2914 if (!validate_arglist (arglist
,
2915 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2917 /* If return value is ignored, transform mempcpy into memcpy. */
2918 else if (target
== const0_rtx
)
2920 tree fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
2925 return expand_expr (build_function_call_expr (fn
, arglist
),
2926 target
, mode
, EXPAND_NORMAL
);
2930 tree dest
= TREE_VALUE (arglist
);
2931 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
2932 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
2933 const char *src_str
;
2934 unsigned int src_align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
2935 unsigned int dest_align
2936 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
2937 rtx dest_mem
, src_mem
, len_rtx
;
2938 tree result
= fold_builtin_memory_op (arglist
, type
, false, endp
);
2942 while (TREE_CODE (result
) == COMPOUND_EXPR
)
2944 expand_expr (TREE_OPERAND (result
, 0), const0_rtx
, VOIDmode
,
2946 result
= TREE_OPERAND (result
, 1);
2948 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2951 /* If either SRC or DEST is not a pointer type, don't do this
2952 operation in-line. */
2953 if (dest_align
== 0 || src_align
== 0)
2956 /* If LEN is not constant, call the normal function. */
2957 if (! host_integerp (len
, 1))
2960 len_rtx
= expand_normal (len
);
2961 src_str
= c_getstr (src
);
2963 /* If SRC is a string constant and block move would be done
2964 by pieces, we can avoid loading the string from memory
2965 and only stored the computed constants. */
2967 && GET_CODE (len_rtx
) == CONST_INT
2968 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
2969 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
2970 (void *) src_str
, dest_align
))
2972 dest_mem
= get_memory_rtx (dest
, len
);
2973 set_mem_align (dest_mem
, dest_align
);
2974 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
2975 builtin_memcpy_read_str
,
2976 (void *) src_str
, dest_align
, endp
);
2977 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
2978 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
2982 if (GET_CODE (len_rtx
) == CONST_INT
2983 && can_move_by_pieces (INTVAL (len_rtx
),
2984 MIN (dest_align
, src_align
)))
2986 dest_mem
= get_memory_rtx (dest
, len
);
2987 set_mem_align (dest_mem
, dest_align
);
2988 src_mem
= get_memory_rtx (src
, len
);
2989 set_mem_align (src_mem
, src_align
);
2990 dest_mem
= move_by_pieces (dest_mem
, src_mem
, INTVAL (len_rtx
),
2991 MIN (dest_align
, src_align
), endp
);
2992 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
2993 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3001 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
3002 if we failed; the caller should emit a normal call. */
3005 expand_builtin_memmove (tree arglist
, tree type
, rtx target
,
3006 enum machine_mode mode
, tree orig_exp
)
3008 if (!validate_arglist (arglist
,
3009 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3013 tree dest
= TREE_VALUE (arglist
);
3014 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
3015 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3017 unsigned int src_align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
3018 unsigned int dest_align
3019 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
3020 tree result
= fold_builtin_memory_op (arglist
, type
, false, /*endp=*/3);
3024 while (TREE_CODE (result
) == COMPOUND_EXPR
)
3026 expand_expr (TREE_OPERAND (result
, 0), const0_rtx
, VOIDmode
,
3028 result
= TREE_OPERAND (result
, 1);
3030 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3033 /* If DEST is not a pointer type, call the normal function. */
3034 if (dest_align
== 0)
3037 /* If either SRC is not a pointer type, don't do this
3038 operation in-line. */
3042 /* If src is categorized for a readonly section we can use
3044 if (readonly_data_expr (src
))
3046 tree fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
3049 fn
= build_function_call_expr (fn
, arglist
);
3050 if (TREE_CODE (fn
) == CALL_EXPR
)
3051 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
3052 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
3055 /* If length is 1 and we can expand memcpy call inline,
3056 it is ok to use memcpy as well. */
3057 if (integer_onep (len
))
3059 rtx ret
= expand_builtin_mempcpy (arglist
, type
, target
, mode
,
3065 /* Otherwise, call the normal function. */
3070 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
3071 if we failed the caller should emit a normal call. */
3074 expand_builtin_bcopy (tree exp
)
3076 tree arglist
= TREE_OPERAND (exp
, 1);
3077 tree type
= TREE_TYPE (exp
);
3078 tree src
, dest
, size
, newarglist
;
3080 if (!validate_arglist (arglist
,
3081 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3084 src
= TREE_VALUE (arglist
);
3085 dest
= TREE_VALUE (TREE_CHAIN (arglist
));
3086 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3088 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3089 memmove(ptr y, ptr x, size_t z). This is done this way
3090 so that if it isn't expanded inline, we fallback to
3091 calling bcopy instead of memmove. */
3093 newarglist
= build_tree_list (NULL_TREE
, fold_convert (sizetype
, size
));
3094 newarglist
= tree_cons (NULL_TREE
, src
, newarglist
);
3095 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
3097 return expand_builtin_memmove (newarglist
, type
, const0_rtx
, VOIDmode
, exp
);
3101 # define HAVE_movstr 0
3102 # define CODE_FOR_movstr CODE_FOR_nothing
3105 /* Expand into a movstr instruction, if one is available. Return 0 if
3106 we failed, the caller should emit a normal call, otherwise try to
3107 get the result in TARGET, if convenient. If ENDP is 0 return the
3108 destination pointer, if ENDP is 1 return the end pointer ala
3109 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3113 expand_movstr (tree dest
, tree src
, rtx target
, int endp
)
3119 const struct insn_data
* data
;
3124 dest_mem
= get_memory_rtx (dest
, NULL
);
3125 src_mem
= get_memory_rtx (src
, NULL
);
3128 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
3129 dest_mem
= replace_equiv_address (dest_mem
, target
);
3130 end
= gen_reg_rtx (Pmode
);
3134 if (target
== 0 || target
== const0_rtx
)
3136 end
= gen_reg_rtx (Pmode
);
3144 data
= insn_data
+ CODE_FOR_movstr
;
3146 if (data
->operand
[0].mode
!= VOIDmode
)
3147 end
= gen_lowpart (data
->operand
[0].mode
, end
);
3149 insn
= data
->genfun (end
, dest_mem
, src_mem
);
3155 /* movstr is supposed to set end to the address of the NUL
3156 terminator. If the caller requested a mempcpy-like return value,
3158 if (endp
== 1 && target
!= const0_rtx
)
3160 rtx tem
= plus_constant (gen_lowpart (GET_MODE (target
), end
), 1);
3161 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
3167 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3168 if we failed the caller should emit a normal call, otherwise try to get
3169 the result in TARGET, if convenient (and in mode MODE if that's
3173 expand_builtin_strcpy (tree fndecl
, tree arglist
, rtx target
, enum machine_mode mode
)
3175 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3177 tree result
= fold_builtin_strcpy (fndecl
, arglist
, 0);
3180 while (TREE_CODE (result
) == COMPOUND_EXPR
)
3182 expand_expr (TREE_OPERAND (result
, 0), const0_rtx
, VOIDmode
,
3184 result
= TREE_OPERAND (result
, 1);
3186 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3189 return expand_movstr (TREE_VALUE (arglist
),
3190 TREE_VALUE (TREE_CHAIN (arglist
)),
3191 target
, /*endp=*/0);
3196 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3197 Return 0 if we failed the caller should emit a normal call,
3198 otherwise try to get the result in TARGET, if convenient (and in
3199 mode MODE if that's convenient). */
3202 expand_builtin_stpcpy (tree exp
, rtx target
, enum machine_mode mode
)
3204 tree arglist
= TREE_OPERAND (exp
, 1);
3205 /* If return value is ignored, transform stpcpy into strcpy. */
3206 if (target
== const0_rtx
)
3208 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
3212 return expand_expr (build_function_call_expr (fn
, arglist
),
3213 target
, mode
, EXPAND_NORMAL
);
3216 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3220 tree dst
, src
, len
, lenp1
;
3224 /* Ensure we get an actual string whose length can be evaluated at
3225 compile-time, not an expression containing a string. This is
3226 because the latter will potentially produce pessimized code
3227 when used to produce the return value. */
3228 src
= TREE_VALUE (TREE_CHAIN (arglist
));
3229 if (! c_getstr (src
) || ! (len
= c_strlen (src
, 0)))
3230 return expand_movstr (TREE_VALUE (arglist
),
3231 TREE_VALUE (TREE_CHAIN (arglist
)),
3232 target
, /*endp=*/2);
3234 dst
= TREE_VALUE (arglist
);
3235 lenp1
= size_binop (PLUS_EXPR
, len
, ssize_int (1));
3236 narglist
= build_tree_list (NULL_TREE
, lenp1
);
3237 narglist
= tree_cons (NULL_TREE
, src
, narglist
);
3238 narglist
= tree_cons (NULL_TREE
, dst
, narglist
);
3239 ret
= expand_builtin_mempcpy (narglist
, TREE_TYPE (exp
),
3240 target
, mode
, /*endp=*/2);
3245 if (TREE_CODE (len
) == INTEGER_CST
)
3247 rtx len_rtx
= expand_normal (len
);
3249 if (GET_CODE (len_rtx
) == CONST_INT
)
3251 ret
= expand_builtin_strcpy (get_callee_fndecl (exp
),
3252 arglist
, target
, mode
);
3258 if (mode
!= VOIDmode
)
3259 target
= gen_reg_rtx (mode
);
3261 target
= gen_reg_rtx (GET_MODE (ret
));
3263 if (GET_MODE (target
) != GET_MODE (ret
))
3264 ret
= gen_lowpart (GET_MODE (target
), ret
);
3266 ret
= plus_constant (ret
, INTVAL (len_rtx
));
3267 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
3275 return expand_movstr (TREE_VALUE (arglist
),
3276 TREE_VALUE (TREE_CHAIN (arglist
)),
3277 target
, /*endp=*/2);
3281 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3282 bytes from constant string DATA + OFFSET and return it as target
3286 builtin_strncpy_read_str (void *data
, HOST_WIDE_INT offset
,
3287 enum machine_mode mode
)
3289 const char *str
= (const char *) data
;
3291 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
3294 return c_readstr (str
+ offset
, mode
);
3297 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3298 if we failed the caller should emit a normal call. */
3301 expand_builtin_strncpy (tree exp
, rtx target
, enum machine_mode mode
)
3303 tree fndecl
= get_callee_fndecl (exp
);
3304 tree arglist
= TREE_OPERAND (exp
, 1);
3305 if (validate_arglist (arglist
,
3306 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3308 tree slen
= c_strlen (TREE_VALUE (TREE_CHAIN (arglist
)), 1);
3309 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3310 tree result
= fold_builtin_strncpy (fndecl
, arglist
, slen
);
3314 while (TREE_CODE (result
) == COMPOUND_EXPR
)
3316 expand_expr (TREE_OPERAND (result
, 0), const0_rtx
, VOIDmode
,
3318 result
= TREE_OPERAND (result
, 1);
3320 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3323 /* We must be passed a constant len and src parameter. */
3324 if (!host_integerp (len
, 1) || !slen
|| !host_integerp (slen
, 1))
3327 slen
= size_binop (PLUS_EXPR
, slen
, ssize_int (1));
3329 /* We're required to pad with trailing zeros if the requested
3330 len is greater than strlen(s2)+1. In that case try to
3331 use store_by_pieces, if it fails, punt. */
3332 if (tree_int_cst_lt (slen
, len
))
3334 tree dest
= TREE_VALUE (arglist
);
3335 unsigned int dest_align
3336 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
3337 const char *p
= c_getstr (TREE_VALUE (TREE_CHAIN (arglist
)));
3340 if (!p
|| dest_align
== 0 || !host_integerp (len
, 1)
3341 || !can_store_by_pieces (tree_low_cst (len
, 1),
3342 builtin_strncpy_read_str
,
3343 (void *) p
, dest_align
))
3346 dest_mem
= get_memory_rtx (dest
, len
);
3347 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3348 builtin_strncpy_read_str
,
3349 (void *) p
, dest_align
, 0);
3350 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3351 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3358 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3359 bytes from constant string DATA + OFFSET and return it as target
3363 builtin_memset_read_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3364 enum machine_mode mode
)
3366 const char *c
= (const char *) data
;
3367 char *p
= alloca (GET_MODE_SIZE (mode
));
3369 memset (p
, *c
, GET_MODE_SIZE (mode
));
3371 return c_readstr (p
, mode
);
3374 /* Callback routine for store_by_pieces. Return the RTL of a register
3375 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3376 char value given in the RTL register data. For example, if mode is
3377 4 bytes wide, return the RTL for 0x01010101*data. */
3380 builtin_memset_gen_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3381 enum machine_mode mode
)
3387 size
= GET_MODE_SIZE (mode
);
3392 memset (p
, 1, size
);
3393 coeff
= c_readstr (p
, mode
);
3395 target
= convert_to_mode (mode
, (rtx
) data
, 1);
3396 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
3397 return force_reg (mode
, target
);
3400 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3401 if we failed the caller should emit a normal call, otherwise try to get
3402 the result in TARGET, if convenient (and in mode MODE if that's
3406 expand_builtin_memset (tree arglist
, rtx target
, enum machine_mode mode
,
3409 if (!validate_arglist (arglist
,
3410 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3414 tree dest
= TREE_VALUE (arglist
);
3415 tree val
= TREE_VALUE (TREE_CHAIN (arglist
));
3416 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3418 enum built_in_function fcode
;
3420 unsigned int dest_align
;
3421 rtx dest_mem
, dest_addr
, len_rtx
;
3423 dest_align
= get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
3425 /* If DEST is not a pointer type, don't do this
3426 operation in-line. */
3427 if (dest_align
== 0)
3430 /* If the LEN parameter is zero, return DEST. */
3431 if (integer_zerop (len
))
3433 /* Evaluate and ignore VAL in case it has side-effects. */
3434 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3435 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
3438 /* Stabilize the arguments in case we fail. */
3439 dest
= builtin_save_expr (dest
);
3440 val
= builtin_save_expr (val
);
3441 len
= builtin_save_expr (len
);
3443 len_rtx
= expand_normal (len
);
3444 dest_mem
= get_memory_rtx (dest
, len
);
3446 if (TREE_CODE (val
) != INTEGER_CST
)
3450 val_rtx
= expand_normal (val
);
3451 val_rtx
= convert_to_mode (TYPE_MODE (unsigned_char_type_node
),
3454 /* Assume that we can memset by pieces if we can store the
3455 * the coefficients by pieces (in the required modes).
3456 * We can't pass builtin_memset_gen_str as that emits RTL. */
3458 if (host_integerp (len
, 1)
3459 && !(optimize_size
&& tree_low_cst (len
, 1) > 1)
3460 && can_store_by_pieces (tree_low_cst (len
, 1),
3461 builtin_memset_read_str
, &c
, dest_align
))
3463 val_rtx
= force_reg (TYPE_MODE (unsigned_char_type_node
),
3465 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3466 builtin_memset_gen_str
, val_rtx
, dest_align
, 0);
3468 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, val_rtx
,
3472 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3473 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3477 if (target_char_cast (val
, &c
))
3482 if (host_integerp (len
, 1)
3483 && !(optimize_size
&& tree_low_cst (len
, 1) > 1)
3484 && can_store_by_pieces (tree_low_cst (len
, 1),
3485 builtin_memset_read_str
, &c
, dest_align
))
3486 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3487 builtin_memset_read_str
, &c
, dest_align
, 0);
3488 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, GEN_INT (c
),
3492 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3493 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3497 set_mem_align (dest_mem
, dest_align
);
3498 dest_addr
= clear_storage (dest_mem
, len_rtx
,
3499 CALL_EXPR_TAILCALL (orig_exp
)
3500 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
);
3504 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3505 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3511 fndecl
= get_callee_fndecl (orig_exp
);
3512 fcode
= DECL_FUNCTION_CODE (fndecl
);
3513 gcc_assert (fcode
== BUILT_IN_MEMSET
|| fcode
== BUILT_IN_BZERO
);
3514 arglist
= build_tree_list (NULL_TREE
, len
);
3515 if (fcode
== BUILT_IN_MEMSET
)
3516 arglist
= tree_cons (NULL_TREE
, val
, arglist
);
3517 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
3518 fn
= build_function_call_expr (fndecl
, arglist
);
3519 if (TREE_CODE (fn
) == CALL_EXPR
)
3520 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
3521 return expand_call (fn
, target
, target
== const0_rtx
);
3525 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3526 if we failed the caller should emit a normal call. */
3529 expand_builtin_bzero (tree exp
)
3531 tree arglist
= TREE_OPERAND (exp
, 1);
3532 tree dest
, size
, newarglist
;
3534 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3537 dest
= TREE_VALUE (arglist
);
3538 size
= TREE_VALUE (TREE_CHAIN (arglist
));
3540 /* New argument list transforming bzero(ptr x, int y) to
3541 memset(ptr x, int 0, size_t y). This is done this way
3542 so that if it isn't expanded inline, we fallback to
3543 calling bzero instead of memset. */
3545 newarglist
= build_tree_list (NULL_TREE
, fold_convert (sizetype
, size
));
3546 newarglist
= tree_cons (NULL_TREE
, integer_zero_node
, newarglist
);
3547 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
3549 return expand_builtin_memset (newarglist
, const0_rtx
, VOIDmode
, exp
);
3552 /* Expand expression EXP, which is a call to the memcmp built-in function.
3553 ARGLIST is the argument list for this call. Return 0 if we failed and the
3554 caller should emit a normal call, otherwise try to get the result in
3555 TARGET, if convenient (and in mode MODE, if that's convenient). */
3558 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED
, tree arglist
, rtx target
,
3559 enum machine_mode mode
)
3561 if (!validate_arglist (arglist
,
3562 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3566 tree result
= fold_builtin_memcmp (arglist
);
3568 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3571 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3573 tree arg1
= TREE_VALUE (arglist
);
3574 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
3575 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3576 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3581 = get_pointer_alignment (arg1
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3583 = get_pointer_alignment (arg2
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3584 enum machine_mode insn_mode
;
3586 #ifdef HAVE_cmpmemsi
3588 insn_mode
= insn_data
[(int) CODE_FOR_cmpmemsi
].operand
[0].mode
;
3591 #ifdef HAVE_cmpstrnsi
3593 insn_mode
= insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
3598 /* If we don't have POINTER_TYPE, call the function. */
3599 if (arg1_align
== 0 || arg2_align
== 0)
3602 /* Make a place to write the result of the instruction. */
3605 && REG_P (result
) && GET_MODE (result
) == insn_mode
3606 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3607 result
= gen_reg_rtx (insn_mode
);
3609 arg1_rtx
= get_memory_rtx (arg1
, len
);
3610 arg2_rtx
= get_memory_rtx (arg2
, len
);
3611 arg3_rtx
= expand_normal (len
);
3613 /* Set MEM_SIZE as appropriate. */
3614 if (GET_CODE (arg3_rtx
) == CONST_INT
)
3616 set_mem_size (arg1_rtx
, arg3_rtx
);
3617 set_mem_size (arg2_rtx
, arg3_rtx
);
3620 #ifdef HAVE_cmpmemsi
3622 insn
= gen_cmpmemsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3623 GEN_INT (MIN (arg1_align
, arg2_align
)));
3626 #ifdef HAVE_cmpstrnsi
3628 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3629 GEN_INT (MIN (arg1_align
, arg2_align
)));
3637 emit_library_call_value (memcmp_libfunc
, result
, LCT_PURE_MAKE_BLOCK
,
3638 TYPE_MODE (integer_type_node
), 3,
3639 XEXP (arg1_rtx
, 0), Pmode
,
3640 XEXP (arg2_rtx
, 0), Pmode
,
3641 convert_to_mode (TYPE_MODE (sizetype
), arg3_rtx
,
3642 TYPE_UNSIGNED (sizetype
)),
3643 TYPE_MODE (sizetype
));
3645 /* Return the value in the proper mode for this function. */
3646 mode
= TYPE_MODE (TREE_TYPE (exp
));
3647 if (GET_MODE (result
) == mode
)
3649 else if (target
!= 0)
3651 convert_move (target
, result
, 0);
3655 return convert_to_mode (mode
, result
, 0);
3662 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3663 if we failed the caller should emit a normal call, otherwise try to get
3664 the result in TARGET, if convenient. */
3667 expand_builtin_strcmp (tree exp
, rtx target
, enum machine_mode mode
)
3669 tree arglist
= TREE_OPERAND (exp
, 1);
3671 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3675 tree result
= fold_builtin_strcmp (arglist
);
3677 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3680 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3681 if (cmpstr_optab
[SImode
] != CODE_FOR_nothing
3682 || cmpstrn_optab
[SImode
] != CODE_FOR_nothing
)
3684 rtx arg1_rtx
, arg2_rtx
;
3685 rtx result
, insn
= NULL_RTX
;
3688 tree arg1
= TREE_VALUE (arglist
);
3689 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
3691 = get_pointer_alignment (arg1
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3693 = get_pointer_alignment (arg2
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3695 /* If we don't have POINTER_TYPE, call the function. */
3696 if (arg1_align
== 0 || arg2_align
== 0)
3699 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3700 arg1
= builtin_save_expr (arg1
);
3701 arg2
= builtin_save_expr (arg2
);
3703 arg1_rtx
= get_memory_rtx (arg1
, NULL
);
3704 arg2_rtx
= get_memory_rtx (arg2
, NULL
);
3706 #ifdef HAVE_cmpstrsi
3707 /* Try to call cmpstrsi. */
3710 enum machine_mode insn_mode
3711 = insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
3713 /* Make a place to write the result of the instruction. */
3716 && REG_P (result
) && GET_MODE (result
) == insn_mode
3717 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3718 result
= gen_reg_rtx (insn_mode
);
3720 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
,
3721 GEN_INT (MIN (arg1_align
, arg2_align
)));
3724 #ifdef HAVE_cmpstrnsi
3725 /* Try to determine at least one length and call cmpstrnsi. */
3726 if (!insn
&& HAVE_cmpstrnsi
)
3731 enum machine_mode insn_mode
3732 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
3733 tree len1
= c_strlen (arg1
, 1);
3734 tree len2
= c_strlen (arg2
, 1);
3737 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
3739 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
3741 /* If we don't have a constant length for the first, use the length
3742 of the second, if we know it. We don't require a constant for
3743 this case; some cost analysis could be done if both are available
3744 but neither is constant. For now, assume they're equally cheap,
3745 unless one has side effects. If both strings have constant lengths,
3752 else if (TREE_SIDE_EFFECTS (len1
))
3754 else if (TREE_SIDE_EFFECTS (len2
))
3756 else if (TREE_CODE (len1
) != INTEGER_CST
)
3758 else if (TREE_CODE (len2
) != INTEGER_CST
)
3760 else if (tree_int_cst_lt (len1
, len2
))
3765 /* If both arguments have side effects, we cannot optimize. */
3766 if (!len
|| TREE_SIDE_EFFECTS (len
))
3769 arg3_rtx
= expand_normal (len
);
3771 /* Make a place to write the result of the instruction. */
3774 && REG_P (result
) && GET_MODE (result
) == insn_mode
3775 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3776 result
= gen_reg_rtx (insn_mode
);
3778 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3779 GEN_INT (MIN (arg1_align
, arg2_align
)));
3787 /* Return the value in the proper mode for this function. */
3788 mode
= TYPE_MODE (TREE_TYPE (exp
));
3789 if (GET_MODE (result
) == mode
)
3792 return convert_to_mode (mode
, result
, 0);
3793 convert_move (target
, result
, 0);
3797 /* Expand the library call ourselves using a stabilized argument
3798 list to avoid re-evaluating the function's arguments twice. */
3799 #ifdef HAVE_cmpstrnsi
3802 arglist
= build_tree_list (NULL_TREE
, arg2
);
3803 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
3804 fndecl
= get_callee_fndecl (exp
);
3805 fn
= build_function_call_expr (fndecl
, arglist
);
3806 if (TREE_CODE (fn
) == CALL_EXPR
)
3807 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
3808 return expand_call (fn
, target
, target
== const0_rtx
);
3814 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3815 if we failed the caller should emit a normal call, otherwise try to get
3816 the result in TARGET, if convenient. */
3819 expand_builtin_strncmp (tree exp
, rtx target
, enum machine_mode mode
)
3821 tree arglist
= TREE_OPERAND (exp
, 1);
3823 if (!validate_arglist (arglist
,
3824 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3828 tree result
= fold_builtin_strncmp (arglist
);
3830 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3833 /* If c_strlen can determine an expression for one of the string
3834 lengths, and it doesn't have side effects, then emit cmpstrnsi
3835 using length MIN(strlen(string)+1, arg3). */
3836 #ifdef HAVE_cmpstrnsi
3839 tree arg1
= TREE_VALUE (arglist
);
3840 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
3841 tree arg3
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3842 tree len
, len1
, len2
;
3843 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3848 = get_pointer_alignment (arg1
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3850 = get_pointer_alignment (arg2
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3851 enum machine_mode insn_mode
3852 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
3854 len1
= c_strlen (arg1
, 1);
3855 len2
= c_strlen (arg2
, 1);
3858 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
3860 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
3862 /* If we don't have a constant length for the first, use the length
3863 of the second, if we know it. We don't require a constant for
3864 this case; some cost analysis could be done if both are available
3865 but neither is constant. For now, assume they're equally cheap,
3866 unless one has side effects. If both strings have constant lengths,
3873 else if (TREE_SIDE_EFFECTS (len1
))
3875 else if (TREE_SIDE_EFFECTS (len2
))
3877 else if (TREE_CODE (len1
) != INTEGER_CST
)
3879 else if (TREE_CODE (len2
) != INTEGER_CST
)
3881 else if (tree_int_cst_lt (len1
, len2
))
3886 /* If both arguments have side effects, we cannot optimize. */
3887 if (!len
|| TREE_SIDE_EFFECTS (len
))
3890 /* The actual new length parameter is MIN(len,arg3). */
3891 len
= fold_build2 (MIN_EXPR
, TREE_TYPE (len
), len
,
3892 fold_convert (TREE_TYPE (len
), arg3
));
3894 /* If we don't have POINTER_TYPE, call the function. */
3895 if (arg1_align
== 0 || arg2_align
== 0)
3898 /* Make a place to write the result of the instruction. */
3901 && REG_P (result
) && GET_MODE (result
) == insn_mode
3902 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3903 result
= gen_reg_rtx (insn_mode
);
3905 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3906 arg1
= builtin_save_expr (arg1
);
3907 arg2
= builtin_save_expr (arg2
);
3908 len
= builtin_save_expr (len
);
3910 arg1_rtx
= get_memory_rtx (arg1
, len
);
3911 arg2_rtx
= get_memory_rtx (arg2
, len
);
3912 arg3_rtx
= expand_normal (len
);
3913 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3914 GEN_INT (MIN (arg1_align
, arg2_align
)));
3919 /* Return the value in the proper mode for this function. */
3920 mode
= TYPE_MODE (TREE_TYPE (exp
));
3921 if (GET_MODE (result
) == mode
)
3924 return convert_to_mode (mode
, result
, 0);
3925 convert_move (target
, result
, 0);
3929 /* Expand the library call ourselves using a stabilized argument
3930 list to avoid re-evaluating the function's arguments twice. */
3931 arglist
= build_tree_list (NULL_TREE
, len
);
3932 arglist
= tree_cons (NULL_TREE
, arg2
, arglist
);
3933 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
3934 fndecl
= get_callee_fndecl (exp
);
3935 fn
= build_function_call_expr (fndecl
, arglist
);
3936 if (TREE_CODE (fn
) == CALL_EXPR
)
3937 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
3938 return expand_call (fn
, target
, target
== const0_rtx
);
3944 /* Expand expression EXP, which is a call to the strcat builtin.
3945 Return 0 if we failed the caller should emit a normal call,
3946 otherwise try to get the result in TARGET, if convenient. */
3949 expand_builtin_strcat (tree fndecl
, tree arglist
, rtx target
, enum machine_mode mode
)
3951 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3955 tree dst
= TREE_VALUE (arglist
),
3956 src
= TREE_VALUE (TREE_CHAIN (arglist
));
3957 const char *p
= c_getstr (src
);
3959 /* If the string length is zero, return the dst parameter. */
3960 if (p
&& *p
== '\0')
3961 return expand_expr (dst
, target
, mode
, EXPAND_NORMAL
);
3965 /* See if we can store by pieces into (dst + strlen(dst)). */
3966 tree newsrc
, newdst
,
3967 strlen_fn
= implicit_built_in_decls
[BUILT_IN_STRLEN
];
3970 /* Stabilize the argument list. */
3971 newsrc
= builtin_save_expr (src
);
3973 arglist
= build_tree_list (NULL_TREE
, newsrc
);
3975 arglist
= TREE_CHAIN (arglist
); /* Reusing arglist if safe. */
3977 dst
= builtin_save_expr (dst
);
3981 /* Create strlen (dst). */
3983 build_function_call_expr (strlen_fn
,
3984 build_tree_list (NULL_TREE
, dst
));
3985 /* Create (dst + (cast) strlen (dst)). */
3986 newdst
= fold_convert (TREE_TYPE (dst
), newdst
);
3987 newdst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (dst
), dst
, newdst
);
3989 newdst
= builtin_save_expr (newdst
);
3990 arglist
= tree_cons (NULL_TREE
, newdst
, arglist
);
3992 if (!expand_builtin_strcpy (fndecl
, arglist
, target
, mode
))
3994 end_sequence (); /* Stop sequence. */
3998 /* Output the entire sequence. */
3999 insns
= get_insns ();
4003 return expand_expr (dst
, target
, mode
, EXPAND_NORMAL
);
4010 /* Expand expression EXP, which is a call to the strncat builtin.
4011 Return 0 if we failed the caller should emit a normal call,
4012 otherwise try to get the result in TARGET, if convenient. */
4015 expand_builtin_strncat (tree arglist
, rtx target
, enum machine_mode mode
)
4017 if (validate_arglist (arglist
,
4018 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4020 tree result
= fold_builtin_strncat (arglist
);
4022 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
4027 /* Expand expression EXP, which is a call to the strspn builtin.
4028 Return 0 if we failed the caller should emit a normal call,
4029 otherwise try to get the result in TARGET, if convenient. */
4032 expand_builtin_strspn (tree arglist
, rtx target
, enum machine_mode mode
)
4034 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4036 tree result
= fold_builtin_strspn (arglist
);
4038 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
4043 /* Expand expression EXP, which is a call to the strcspn builtin.
4044 Return 0 if we failed the caller should emit a normal call,
4045 otherwise try to get the result in TARGET, if convenient. */
4048 expand_builtin_strcspn (tree arglist
, rtx target
, enum machine_mode mode
)
4050 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4052 tree result
= fold_builtin_strcspn (arglist
);
4054 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
4059 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4060 if that's convenient. */
4063 expand_builtin_saveregs (void)
4067 /* Don't do __builtin_saveregs more than once in a function.
4068 Save the result of the first call and reuse it. */
4069 if (saveregs_value
!= 0)
4070 return saveregs_value
;
4072 /* When this function is called, it means that registers must be
4073 saved on entry to this function. So we migrate the call to the
4074 first insn of this function. */
4078 /* Do whatever the machine needs done in this case. */
4079 val
= targetm
.calls
.expand_builtin_saveregs ();
4084 saveregs_value
= val
;
4086 /* Put the insns after the NOTE that starts the function. If this
4087 is inside a start_sequence, make the outer-level insn chain current, so
4088 the code is placed at the start of the function. */
4089 push_topmost_sequence ();
4090 emit_insn_after (seq
, entry_of_function ());
4091 pop_topmost_sequence ();
4096 /* __builtin_args_info (N) returns word N of the arg space info
4097 for the current function. The number and meanings of words
4098 is controlled by the definition of CUMULATIVE_ARGS. */
4101 expand_builtin_args_info (tree arglist
)
4103 int nwords
= sizeof (CUMULATIVE_ARGS
) / sizeof (int);
4104 int *word_ptr
= (int *) ¤t_function_args_info
;
4106 gcc_assert (sizeof (CUMULATIVE_ARGS
) % sizeof (int) == 0);
4110 if (!host_integerp (TREE_VALUE (arglist
), 0))
4111 error ("argument of %<__builtin_args_info%> must be constant");
4114 HOST_WIDE_INT wordnum
= tree_low_cst (TREE_VALUE (arglist
), 0);
4116 if (wordnum
< 0 || wordnum
>= nwords
)
4117 error ("argument of %<__builtin_args_info%> out of range");
4119 return GEN_INT (word_ptr
[wordnum
]);
4123 error ("missing argument in %<__builtin_args_info%>");
4128 /* Expand a call to __builtin_next_arg. */
4131 expand_builtin_next_arg (void)
4133 /* Checking arguments is already done in fold_builtin_next_arg
4134 that must be called before this function. */
4135 return expand_binop (Pmode
, add_optab
,
4136 current_function_internal_arg_pointer
,
4137 current_function_arg_offset_rtx
,
4138 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
4141 /* Make it easier for the backends by protecting the valist argument
4142 from multiple evaluations. */
4145 stabilize_va_list (tree valist
, int needs_lvalue
)
4147 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
4149 if (TREE_SIDE_EFFECTS (valist
))
4150 valist
= save_expr (valist
);
4152 /* For this case, the backends will be expecting a pointer to
4153 TREE_TYPE (va_list_type_node), but it's possible we've
4154 actually been given an array (an actual va_list_type_node).
4156 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4158 tree p1
= build_pointer_type (TREE_TYPE (va_list_type_node
));
4159 valist
= build_fold_addr_expr_with_type (valist
, p1
);
4168 if (! TREE_SIDE_EFFECTS (valist
))
4171 pt
= build_pointer_type (va_list_type_node
);
4172 valist
= fold_build1 (ADDR_EXPR
, pt
, valist
);
4173 TREE_SIDE_EFFECTS (valist
) = 1;
4176 if (TREE_SIDE_EFFECTS (valist
))
4177 valist
= save_expr (valist
);
4178 valist
= build_fold_indirect_ref (valist
);
4184 /* The "standard" definition of va_list is void*. */
4187 std_build_builtin_va_list (void)
4189 return ptr_type_node
;
4192 /* The "standard" implementation of va_start: just assign `nextarg' to
4196 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
4200 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist
,
4201 make_tree (ptr_type_node
, nextarg
));
4202 TREE_SIDE_EFFECTS (t
) = 1;
4204 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4207 /* Expand ARGLIST, from a call to __builtin_va_start. */
4210 expand_builtin_va_start (tree arglist
)
4215 chain
= TREE_CHAIN (arglist
);
4219 error ("too few arguments to function %<va_start%>");
4223 if (fold_builtin_next_arg (chain
))
4226 nextarg
= expand_builtin_next_arg ();
4227 valist
= stabilize_va_list (TREE_VALUE (arglist
), 1);
4229 #ifdef EXPAND_BUILTIN_VA_START
4230 EXPAND_BUILTIN_VA_START (valist
, nextarg
);
4232 std_expand_builtin_va_start (valist
, nextarg
);
4238 /* The "standard" implementation of va_arg: read the value from the
4239 current (padded) address and increment by the (padded) size. */
4242 std_gimplify_va_arg_expr (tree valist
, tree type
, tree
*pre_p
, tree
*post_p
)
4244 tree addr
, t
, type_size
, rounded_size
, valist_tmp
;
4245 unsigned HOST_WIDE_INT align
, boundary
;
4248 #ifdef ARGS_GROW_DOWNWARD
4249 /* All of the alignment and movement below is for args-grow-up machines.
4250 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4251 implement their own specialized gimplify_va_arg_expr routines. */
4255 indirect
= pass_by_reference (NULL
, TYPE_MODE (type
), type
, false);
4257 type
= build_pointer_type (type
);
4259 align
= PARM_BOUNDARY
/ BITS_PER_UNIT
;
4260 boundary
= FUNCTION_ARG_BOUNDARY (TYPE_MODE (type
), type
) / BITS_PER_UNIT
;
4262 /* Hoist the valist value into a temporary for the moment. */
4263 valist_tmp
= get_initialized_tmp_var (valist
, pre_p
, NULL
);
4265 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4266 requires greater alignment, we must perform dynamic alignment. */
4267 if (boundary
> align
4268 && !integer_zerop (TYPE_SIZE (type
)))
4270 t
= fold_convert (TREE_TYPE (valist
), size_int (boundary
- 1));
4271 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist_tmp
,
4272 build2 (PLUS_EXPR
, TREE_TYPE (valist
), valist_tmp
, t
));
4273 gimplify_and_add (t
, pre_p
);
4275 t
= fold_convert (TREE_TYPE (valist
), size_int (-boundary
));
4276 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist_tmp
,
4277 build2 (BIT_AND_EXPR
, TREE_TYPE (valist
), valist_tmp
, t
));
4278 gimplify_and_add (t
, pre_p
);
4283 /* If the actual alignment is less than the alignment of the type,
4284 adjust the type accordingly so that we don't assume strict alignment
4285 when deferencing the pointer. */
4286 boundary
*= BITS_PER_UNIT
;
4287 if (boundary
< TYPE_ALIGN (type
))
4289 type
= build_variant_type_copy (type
);
4290 TYPE_ALIGN (type
) = boundary
;
4293 /* Compute the rounded size of the type. */
4294 type_size
= size_in_bytes (type
);
4295 rounded_size
= round_up (type_size
, align
);
4297 /* Reduce rounded_size so it's sharable with the postqueue. */
4298 gimplify_expr (&rounded_size
, pre_p
, post_p
, is_gimple_val
, fb_rvalue
);
4302 if (PAD_VARARGS_DOWN
&& !integer_zerop (rounded_size
))
4304 /* Small args are padded downward. */
4305 t
= fold_build2 (GT_EXPR
, sizetype
, rounded_size
, size_int (align
));
4306 t
= fold_build3 (COND_EXPR
, sizetype
, t
, size_zero_node
,
4307 size_binop (MINUS_EXPR
, rounded_size
, type_size
));
4308 t
= fold_convert (TREE_TYPE (addr
), t
);
4309 addr
= fold_build2 (PLUS_EXPR
, TREE_TYPE (addr
), addr
, t
);
4312 /* Compute new value for AP. */
4313 t
= fold_convert (TREE_TYPE (valist
), rounded_size
);
4314 t
= build2 (PLUS_EXPR
, TREE_TYPE (valist
), valist_tmp
, t
);
4315 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist
, t
);
4316 gimplify_and_add (t
, pre_p
);
4318 addr
= fold_convert (build_pointer_type (type
), addr
);
4321 addr
= build_va_arg_indirect_ref (addr
);
4323 return build_va_arg_indirect_ref (addr
);
4326 /* Build an indirect-ref expression over the given TREE, which represents a
4327 piece of a va_arg() expansion. */
4329 build_va_arg_indirect_ref (tree addr
)
4331 addr
= build_fold_indirect_ref (addr
);
4333 if (flag_mudflap
) /* Don't instrument va_arg INDIRECT_REF. */
4339 /* Return a dummy expression of type TYPE in order to keep going after an
4343 dummy_object (tree type
)
4345 tree t
= build_int_cst (build_pointer_type (type
), 0);
4346 return build1 (INDIRECT_REF
, type
, t
);
4349 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4350 builtin function, but a very special sort of operator. */
4352 enum gimplify_status
4353 gimplify_va_arg_expr (tree
*expr_p
, tree
*pre_p
, tree
*post_p
)
4355 tree promoted_type
, want_va_type
, have_va_type
;
4356 tree valist
= TREE_OPERAND (*expr_p
, 0);
4357 tree type
= TREE_TYPE (*expr_p
);
4360 /* Verify that valist is of the proper type. */
4361 want_va_type
= va_list_type_node
;
4362 have_va_type
= TREE_TYPE (valist
);
4364 if (have_va_type
== error_mark_node
)
4367 if (TREE_CODE (want_va_type
) == ARRAY_TYPE
)
4369 /* If va_list is an array type, the argument may have decayed
4370 to a pointer type, e.g. by being passed to another function.
4371 In that case, unwrap both types so that we can compare the
4372 underlying records. */
4373 if (TREE_CODE (have_va_type
) == ARRAY_TYPE
4374 || POINTER_TYPE_P (have_va_type
))
4376 want_va_type
= TREE_TYPE (want_va_type
);
4377 have_va_type
= TREE_TYPE (have_va_type
);
4381 if (TYPE_MAIN_VARIANT (want_va_type
) != TYPE_MAIN_VARIANT (have_va_type
))
4383 error ("first argument to %<va_arg%> not of type %<va_list%>");
4387 /* Generate a diagnostic for requesting data of a type that cannot
4388 be passed through `...' due to type promotion at the call site. */
4389 else if ((promoted_type
= lang_hooks
.types
.type_promotes_to (type
))
4392 static bool gave_help
;
4394 /* Unfortunately, this is merely undefined, rather than a constraint
4395 violation, so we cannot make this an error. If this call is never
4396 executed, the program is still strictly conforming. */
4397 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4398 type
, promoted_type
);
4402 warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4403 promoted_type
, type
);
4406 /* We can, however, treat "undefined" any way we please.
4407 Call abort to encourage the user to fix the program. */
4408 inform ("if this code is reached, the program will abort");
4409 t
= build_function_call_expr (implicit_built_in_decls
[BUILT_IN_TRAP
],
4411 append_to_statement_list (t
, pre_p
);
4413 /* This is dead code, but go ahead and finish so that the
4414 mode of the result comes out right. */
4415 *expr_p
= dummy_object (type
);
4420 /* Make it easier for the backends by protecting the valist argument
4421 from multiple evaluations. */
4422 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
4424 /* For this case, the backends will be expecting a pointer to
4425 TREE_TYPE (va_list_type_node), but it's possible we've
4426 actually been given an array (an actual va_list_type_node).
4428 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4430 tree p1
= build_pointer_type (TREE_TYPE (va_list_type_node
));
4431 valist
= build_fold_addr_expr_with_type (valist
, p1
);
4433 gimplify_expr (&valist
, pre_p
, post_p
, is_gimple_val
, fb_rvalue
);
4436 gimplify_expr (&valist
, pre_p
, post_p
, is_gimple_min_lval
, fb_lvalue
);
4438 if (!targetm
.gimplify_va_arg_expr
)
4439 /* FIXME:Once most targets are converted we should merely
4440 assert this is non-null. */
4443 *expr_p
= targetm
.gimplify_va_arg_expr (valist
, type
, pre_p
, post_p
);
4448 /* Expand ARGLIST, from a call to __builtin_va_end. */
4451 expand_builtin_va_end (tree arglist
)
4453 tree valist
= TREE_VALUE (arglist
);
4455 /* Evaluate for side effects, if needed. I hate macros that don't
4457 if (TREE_SIDE_EFFECTS (valist
))
4458 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4463 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4464 builtin rather than just as an assignment in stdarg.h because of the
4465 nastiness of array-type va_list types. */
4468 expand_builtin_va_copy (tree arglist
)
4472 dst
= TREE_VALUE (arglist
);
4473 src
= TREE_VALUE (TREE_CHAIN (arglist
));
4475 dst
= stabilize_va_list (dst
, 1);
4476 src
= stabilize_va_list (src
, 0);
4478 if (TREE_CODE (va_list_type_node
) != ARRAY_TYPE
)
4480 t
= build2 (MODIFY_EXPR
, va_list_type_node
, dst
, src
);
4481 TREE_SIDE_EFFECTS (t
) = 1;
4482 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4486 rtx dstb
, srcb
, size
;
4488 /* Evaluate to pointers. */
4489 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4490 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4491 size
= expand_expr (TYPE_SIZE_UNIT (va_list_type_node
), NULL_RTX
,
4492 VOIDmode
, EXPAND_NORMAL
);
4494 dstb
= convert_memory_address (Pmode
, dstb
);
4495 srcb
= convert_memory_address (Pmode
, srcb
);
4497 /* "Dereference" to BLKmode memories. */
4498 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
4499 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
4500 set_mem_align (dstb
, TYPE_ALIGN (va_list_type_node
));
4501 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
4502 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
4503 set_mem_align (srcb
, TYPE_ALIGN (va_list_type_node
));
4506 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
4512 /* Expand a call to one of the builtin functions __builtin_frame_address or
4513 __builtin_return_address. */
4516 expand_builtin_frame_address (tree fndecl
, tree arglist
)
4518 /* The argument must be a nonnegative integer constant.
4519 It counts the number of frames to scan up the stack.
4520 The value is the return address saved in that frame. */
4522 /* Warning about missing arg was already issued. */
4524 else if (! host_integerp (TREE_VALUE (arglist
), 1))
4526 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4527 error ("invalid argument to %<__builtin_frame_address%>");
4529 error ("invalid argument to %<__builtin_return_address%>");
4535 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
),
4536 tree_low_cst (TREE_VALUE (arglist
), 1));
4538 /* Some ports cannot access arbitrary stack frames. */
4541 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4542 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4544 warning (0, "unsupported argument to %<__builtin_return_address%>");
4548 /* For __builtin_frame_address, return what we've got. */
4549 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4553 && ! CONSTANT_P (tem
))
4554 tem
= copy_to_mode_reg (Pmode
, tem
);
4559 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4560 we failed and the caller should emit a normal call, otherwise try to get
4561 the result in TARGET, if convenient. */
4564 expand_builtin_alloca (tree arglist
, rtx target
)
4569 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4570 should always expand to function calls. These can be intercepted
4575 if (!validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
4578 /* Compute the argument. */
4579 op0
= expand_normal (TREE_VALUE (arglist
));
4581 /* Allocate the desired space. */
4582 result
= allocate_dynamic_stack_space (op0
, target
, BITS_PER_UNIT
);
4583 result
= convert_memory_address (ptr_mode
, result
);
4588 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4589 Return 0 if a normal call should be emitted rather than expanding the
4590 function in-line. If convenient, the result should be placed in TARGET.
4591 SUBTARGET may be used as the target for computing one of EXP's operands. */
4594 expand_builtin_unop (enum machine_mode target_mode
, tree arglist
, rtx target
,
4595 rtx subtarget
, optab op_optab
)
4598 if (!validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
4601 /* Compute the argument. */
4602 op0
= expand_expr (TREE_VALUE (arglist
), subtarget
, VOIDmode
, 0);
4603 /* Compute op, into TARGET if possible.
4604 Set TARGET to wherever the result comes back. */
4605 target
= expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist
))),
4606 op_optab
, op0
, target
, 1);
4607 gcc_assert (target
);
4609 return convert_to_mode (target_mode
, target
, 0);
4612 /* If the string passed to fputs is a constant and is one character
4613 long, we attempt to transform this call into __builtin_fputc(). */
4616 expand_builtin_fputs (tree arglist
, rtx target
, bool unlocked
)
4618 /* Verify the arguments in the original call. */
4619 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4621 tree result
= fold_builtin_fputs (arglist
, (target
== const0_rtx
),
4622 unlocked
, NULL_TREE
);
4624 return expand_expr (result
, target
, VOIDmode
, EXPAND_NORMAL
);
4629 /* Expand a call to __builtin_expect. We return our argument and emit a
4630 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4631 a non-jump context. */
4634 expand_builtin_expect (tree arglist
, rtx target
)
4639 if (arglist
== NULL_TREE
4640 || TREE_CHAIN (arglist
) == NULL_TREE
)
4642 exp
= TREE_VALUE (arglist
);
4643 c
= TREE_VALUE (TREE_CHAIN (arglist
));
4645 if (TREE_CODE (c
) != INTEGER_CST
)
4647 error ("second argument to %<__builtin_expect%> must be a constant");
4648 c
= integer_zero_node
;
4651 target
= expand_expr (exp
, target
, VOIDmode
, EXPAND_NORMAL
);
4653 /* Don't bother with expected value notes for integral constants. */
4654 if (flag_guess_branch_prob
&& GET_CODE (target
) != CONST_INT
)
4656 /* We do need to force this into a register so that we can be
4657 moderately sure to be able to correctly interpret the branch
4659 target
= force_reg (GET_MODE (target
), target
);
4661 rtx_c
= expand_expr (c
, NULL_RTX
, GET_MODE (target
), EXPAND_NORMAL
);
4663 note
= emit_note (NOTE_INSN_EXPECTED_VALUE
);
4664 NOTE_EXPECTED_VALUE (note
) = gen_rtx_EQ (VOIDmode
, target
, rtx_c
);
4670 /* Like expand_builtin_expect, except do this in a jump context. This is
4671 called from do_jump if the conditional is a __builtin_expect. Return either
4672 a list of insns to emit the jump or NULL if we cannot optimize
4673 __builtin_expect. We need to optimize this at jump time so that machines
4674 like the PowerPC don't turn the test into a SCC operation, and then jump
4675 based on the test being 0/1. */
4678 expand_builtin_expect_jump (tree exp
, rtx if_false_label
, rtx if_true_label
)
4680 tree arglist
= TREE_OPERAND (exp
, 1);
4681 tree arg0
= TREE_VALUE (arglist
);
4682 tree arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
4685 /* Only handle __builtin_expect (test, 0) and
4686 __builtin_expect (test, 1). */
4687 if (TREE_CODE (TREE_TYPE (arg1
)) == INTEGER_TYPE
4688 && (integer_zerop (arg1
) || integer_onep (arg1
)))
4690 rtx insn
, drop_through_label
, temp
;
4692 /* Expand the jump insns. */
4694 do_jump (arg0
, if_false_label
, if_true_label
);
4697 drop_through_label
= get_last_insn ();
4698 if (drop_through_label
&& NOTE_P (drop_through_label
))
4699 drop_through_label
= prev_nonnote_insn (drop_through_label
);
4700 if (drop_through_label
&& !LABEL_P (drop_through_label
))
4701 drop_through_label
= NULL_RTX
;
4704 if (! if_true_label
)
4705 if_true_label
= drop_through_label
;
4706 if (! if_false_label
)
4707 if_false_label
= drop_through_label
;
4709 /* Go through and add the expect's to each of the conditional jumps. */
4711 while (insn
!= NULL_RTX
)
4713 rtx next
= NEXT_INSN (insn
);
4715 if (JUMP_P (insn
) && any_condjump_p (insn
))
4717 rtx ifelse
= SET_SRC (pc_set (insn
));
4718 rtx then_dest
= XEXP (ifelse
, 1);
4719 rtx else_dest
= XEXP (ifelse
, 2);
4722 /* First check if we recognize any of the labels. */
4723 if (GET_CODE (then_dest
) == LABEL_REF
4724 && XEXP (then_dest
, 0) == if_true_label
)
4726 else if (GET_CODE (then_dest
) == LABEL_REF
4727 && XEXP (then_dest
, 0) == if_false_label
)
4729 else if (GET_CODE (else_dest
) == LABEL_REF
4730 && XEXP (else_dest
, 0) == if_false_label
)
4732 else if (GET_CODE (else_dest
) == LABEL_REF
4733 && XEXP (else_dest
, 0) == if_true_label
)
4735 /* Otherwise check where we drop through. */
4736 else if (else_dest
== pc_rtx
)
4738 if (next
&& NOTE_P (next
))
4739 next
= next_nonnote_insn (next
);
4741 if (next
&& JUMP_P (next
)
4742 && any_uncondjump_p (next
))
4743 temp
= XEXP (SET_SRC (pc_set (next
)), 0);
4747 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4748 else that can't possibly match either target label. */
4749 if (temp
== if_false_label
)
4751 else if (temp
== if_true_label
)
4754 else if (then_dest
== pc_rtx
)
4756 if (next
&& NOTE_P (next
))
4757 next
= next_nonnote_insn (next
);
4759 if (next
&& JUMP_P (next
)
4760 && any_uncondjump_p (next
))
4761 temp
= XEXP (SET_SRC (pc_set (next
)), 0);
4765 if (temp
== if_false_label
)
4767 else if (temp
== if_true_label
)
4773 /* If the test is expected to fail, reverse the
4775 if (integer_zerop (arg1
))
4777 predict_insn_def (insn
, PRED_BUILTIN_EXPECT
, taken
);
4789 expand_builtin_trap (void)
4793 emit_insn (gen_trap ());
4796 emit_library_call (abort_libfunc
, LCT_NORETURN
, VOIDmode
, 0);
4800 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4801 Return 0 if a normal call should be emitted rather than expanding
4802 the function inline. If convenient, the result should be placed
4803 in TARGET. SUBTARGET may be used as the target for computing
4807 expand_builtin_fabs (tree arglist
, rtx target
, rtx subtarget
)
4809 enum machine_mode mode
;
4813 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
4816 arg
= TREE_VALUE (arglist
);
4817 mode
= TYPE_MODE (TREE_TYPE (arg
));
4818 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
4819 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
4822 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4823 Return NULL is a normal call should be emitted rather than expanding the
4824 function inline. If convenient, the result should be placed in TARGET.
4825 SUBTARGET may be used as the target for computing the operand. */
4828 expand_builtin_copysign (tree arglist
, rtx target
, rtx subtarget
)
4833 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
4836 arg
= TREE_VALUE (arglist
);
4837 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4839 arg
= TREE_VALUE (TREE_CHAIN (arglist
));
4840 op1
= expand_normal (arg
);
4842 return expand_copysign (op0
, op1
, target
);
4845 /* Create a new constant string literal and return a char* pointer to it.
4846 The STRING_CST value is the LEN characters at STR. */
4848 build_string_literal (int len
, const char *str
)
4850 tree t
, elem
, index
, type
;
4852 t
= build_string (len
, str
);
4853 elem
= build_type_variant (char_type_node
, 1, 0);
4854 index
= build_index_type (build_int_cst (NULL_TREE
, len
- 1));
4855 type
= build_array_type (elem
, index
);
4856 TREE_TYPE (t
) = type
;
4857 TREE_CONSTANT (t
) = 1;
4858 TREE_INVARIANT (t
) = 1;
4859 TREE_READONLY (t
) = 1;
4860 TREE_STATIC (t
) = 1;
4862 type
= build_pointer_type (type
);
4863 t
= build1 (ADDR_EXPR
, type
, t
);
4865 type
= build_pointer_type (elem
);
4866 t
= build1 (NOP_EXPR
, type
, t
);
4870 /* Expand EXP, a call to printf or printf_unlocked.
4871 Return 0 if a normal call should be emitted rather than transforming
4872 the function inline. If convenient, the result should be placed in
4873 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4876 expand_builtin_printf (tree exp
, rtx target
, enum machine_mode mode
,
4879 tree arglist
= TREE_OPERAND (exp
, 1);
4880 /* If we're using an unlocked function, assume the other unlocked
4881 functions exist explicitly. */
4882 tree
const fn_putchar
= unlocked
? built_in_decls
[BUILT_IN_PUTCHAR_UNLOCKED
]
4883 : implicit_built_in_decls
[BUILT_IN_PUTCHAR
];
4884 tree
const fn_puts
= unlocked
? built_in_decls
[BUILT_IN_PUTS_UNLOCKED
]
4885 : implicit_built_in_decls
[BUILT_IN_PUTS
];
4886 const char *fmt_str
;
4889 /* If the return value is used, don't do the transformation. */
4890 if (target
!= const0_rtx
)
4893 /* Verify the required arguments in the original call. */
4896 fmt
= TREE_VALUE (arglist
);
4897 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
4899 arglist
= TREE_CHAIN (arglist
);
4901 /* Check whether the format is a literal string constant. */
4902 fmt_str
= c_getstr (fmt
);
4903 if (fmt_str
== NULL
)
4906 if (!init_target_chars())
4909 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4910 if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
4913 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
4914 || TREE_CHAIN (arglist
))
4918 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4919 else if (strcmp (fmt_str
, target_percent_c
) == 0)
4922 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
4923 || TREE_CHAIN (arglist
))
4929 /* We can't handle anything else with % args or %% ... yet. */
4930 if (strchr (fmt_str
, target_percent
))
4936 /* If the format specifier was "", printf does nothing. */
4937 if (fmt_str
[0] == '\0')
4939 /* If the format specifier has length of 1, call putchar. */
4940 if (fmt_str
[1] == '\0')
4942 /* Given printf("c"), (where c is any one character,)
4943 convert "c"[0] to an int and pass that to the replacement
4945 arg
= build_int_cst (NULL_TREE
, fmt_str
[0]);
4946 arglist
= build_tree_list (NULL_TREE
, arg
);
4951 /* If the format specifier was "string\n", call puts("string"). */
4952 size_t len
= strlen (fmt_str
);
4953 if ((unsigned char)fmt_str
[len
- 1] == target_newline
)
4955 /* Create a NUL-terminated string that's one char shorter
4956 than the original, stripping off the trailing '\n'. */
4957 char *newstr
= alloca (len
);
4958 memcpy (newstr
, fmt_str
, len
- 1);
4959 newstr
[len
- 1] = 0;
4961 arg
= build_string_literal (len
, newstr
);
4962 arglist
= build_tree_list (NULL_TREE
, arg
);
4966 /* We'd like to arrange to call fputs(string,stdout) here,
4967 but we need stdout and don't have a way to get it yet. */
4974 fn
= build_function_call_expr (fn
, arglist
);
4975 if (TREE_CODE (fn
) == CALL_EXPR
)
4976 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4977 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
4980 /* Expand EXP, a call to fprintf or fprintf_unlocked.
4981 Return 0 if a normal call should be emitted rather than transforming
4982 the function inline. If convenient, the result should be placed in
4983 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4986 expand_builtin_fprintf (tree exp
, rtx target
, enum machine_mode mode
,
4989 tree arglist
= TREE_OPERAND (exp
, 1);
4990 /* If we're using an unlocked function, assume the other unlocked
4991 functions exist explicitly. */
4992 tree
const fn_fputc
= unlocked
? built_in_decls
[BUILT_IN_FPUTC_UNLOCKED
]
4993 : implicit_built_in_decls
[BUILT_IN_FPUTC
];
4994 tree
const fn_fputs
= unlocked
? built_in_decls
[BUILT_IN_FPUTS_UNLOCKED
]
4995 : implicit_built_in_decls
[BUILT_IN_FPUTS
];
4996 const char *fmt_str
;
4997 tree fn
, fmt
, fp
, arg
;
4999 /* If the return value is used, don't do the transformation. */
5000 if (target
!= const0_rtx
)
5003 /* Verify the required arguments in the original call. */
5006 fp
= TREE_VALUE (arglist
);
5007 if (! POINTER_TYPE_P (TREE_TYPE (fp
)))
5009 arglist
= TREE_CHAIN (arglist
);
5012 fmt
= TREE_VALUE (arglist
);
5013 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
5015 arglist
= TREE_CHAIN (arglist
);
5017 /* Check whether the format is a literal string constant. */
5018 fmt_str
= c_getstr (fmt
);
5019 if (fmt_str
== NULL
)
5022 if (!init_target_chars())
5025 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5026 if (strcmp (fmt_str
, target_percent_s
) == 0)
5029 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
5030 || TREE_CHAIN (arglist
))
5032 arg
= TREE_VALUE (arglist
);
5033 arglist
= build_tree_list (NULL_TREE
, fp
);
5034 arglist
= tree_cons (NULL_TREE
, arg
, arglist
);
5037 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5038 else if (strcmp (fmt_str
, target_percent_c
) == 0)
5041 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
5042 || TREE_CHAIN (arglist
))
5044 arg
= TREE_VALUE (arglist
);
5045 arglist
= build_tree_list (NULL_TREE
, fp
);
5046 arglist
= tree_cons (NULL_TREE
, arg
, arglist
);
5051 /* We can't handle anything else with % args or %% ... yet. */
5052 if (strchr (fmt_str
, target_percent
))
5058 /* If the format specifier was "", fprintf does nothing. */
5059 if (fmt_str
[0] == '\0')
5061 /* Evaluate and ignore FILE* argument for side-effects. */
5062 expand_expr (fp
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5066 /* When "string" doesn't contain %, replace all cases of
5067 fprintf(stream,string) with fputs(string,stream). The fputs
5068 builtin will take care of special cases like length == 1. */
5069 arglist
= build_tree_list (NULL_TREE
, fp
);
5070 arglist
= tree_cons (NULL_TREE
, fmt
, arglist
);
5076 fn
= build_function_call_expr (fn
, arglist
);
5077 if (TREE_CODE (fn
) == CALL_EXPR
)
5078 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
5079 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
5082 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5083 a normal call should be emitted rather than expanding the function
5084 inline. If convenient, the result should be placed in TARGET with
5088 expand_builtin_sprintf (tree arglist
, rtx target
, enum machine_mode mode
)
5090 tree orig_arglist
, dest
, fmt
;
5091 const char *fmt_str
;
5093 orig_arglist
= arglist
;
5095 /* Verify the required arguments in the original call. */
5098 dest
= TREE_VALUE (arglist
);
5099 if (! POINTER_TYPE_P (TREE_TYPE (dest
)))
5101 arglist
= TREE_CHAIN (arglist
);
5104 fmt
= TREE_VALUE (arglist
);
5105 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
5107 arglist
= TREE_CHAIN (arglist
);
5109 /* Check whether the format is a literal string constant. */
5110 fmt_str
= c_getstr (fmt
);
5111 if (fmt_str
== NULL
)
5114 if (!init_target_chars())
5117 /* If the format doesn't contain % args or %%, use strcpy. */
5118 if (strchr (fmt_str
, target_percent
) == 0)
5120 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
5123 if (arglist
|| ! fn
)
5125 expand_expr (build_function_call_expr (fn
, orig_arglist
),
5126 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5127 if (target
== const0_rtx
)
5129 exp
= build_int_cst (NULL_TREE
, strlen (fmt_str
));
5130 return expand_expr (exp
, target
, mode
, EXPAND_NORMAL
);
5132 /* If the format is "%s", use strcpy if the result isn't used. */
5133 else if (strcmp (fmt_str
, target_percent_s
) == 0)
5136 fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
5141 if (! arglist
|| TREE_CHAIN (arglist
))
5143 arg
= TREE_VALUE (arglist
);
5144 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
5147 if (target
!= const0_rtx
)
5149 len
= c_strlen (arg
, 1);
5150 if (! len
|| TREE_CODE (len
) != INTEGER_CST
)
5156 arglist
= build_tree_list (NULL_TREE
, arg
);
5157 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
5158 expand_expr (build_function_call_expr (fn
, arglist
),
5159 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5161 if (target
== const0_rtx
)
5163 return expand_expr (len
, target
, mode
, EXPAND_NORMAL
);
5169 /* Expand a call to either the entry or exit function profiler. */
5172 expand_builtin_profile_func (bool exitp
)
5176 this = DECL_RTL (current_function_decl
);
5177 gcc_assert (MEM_P (this));
5178 this = XEXP (this, 0);
5181 which
= profile_function_exit_libfunc
;
5183 which
= profile_function_entry_libfunc
;
5185 emit_library_call (which
, LCT_NORMAL
, VOIDmode
, 2, this, Pmode
,
5186 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS
,
5193 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5196 round_trampoline_addr (rtx tramp
)
5198 rtx temp
, addend
, mask
;
5200 /* If we don't need too much alignment, we'll have been guaranteed
5201 proper alignment by get_trampoline_type. */
5202 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
5205 /* Round address up to desired boundary. */
5206 temp
= gen_reg_rtx (Pmode
);
5207 addend
= GEN_INT (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1);
5208 mask
= GEN_INT (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
);
5210 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
5211 temp
, 0, OPTAB_LIB_WIDEN
);
5212 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
5213 temp
, 0, OPTAB_LIB_WIDEN
);
5219 expand_builtin_init_trampoline (tree arglist
)
5221 tree t_tramp
, t_func
, t_chain
;
5222 rtx r_tramp
, r_func
, r_chain
;
5223 #ifdef TRAMPOLINE_TEMPLATE
5227 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
,
5228 POINTER_TYPE
, VOID_TYPE
))
5231 t_tramp
= TREE_VALUE (arglist
);
5232 arglist
= TREE_CHAIN (arglist
);
5233 t_func
= TREE_VALUE (arglist
);
5234 arglist
= TREE_CHAIN (arglist
);
5235 t_chain
= TREE_VALUE (arglist
);
5237 r_tramp
= expand_normal (t_tramp
);
5238 r_func
= expand_normal (t_func
);
5239 r_chain
= expand_normal (t_chain
);
5241 /* Generate insns to initialize the trampoline. */
5242 r_tramp
= round_trampoline_addr (r_tramp
);
5243 #ifdef TRAMPOLINE_TEMPLATE
5244 blktramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
5245 set_mem_align (blktramp
, TRAMPOLINE_ALIGNMENT
);
5246 emit_block_move (blktramp
, assemble_trampoline_template (),
5247 GEN_INT (TRAMPOLINE_SIZE
), BLOCK_OP_NORMAL
);
5249 trampolines_created
= 1;
5250 INITIALIZE_TRAMPOLINE (r_tramp
, r_func
, r_chain
);
5256 expand_builtin_adjust_trampoline (tree arglist
)
5260 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
5263 tramp
= expand_normal (TREE_VALUE (arglist
));
5264 tramp
= round_trampoline_addr (tramp
);
5265 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5266 TRAMPOLINE_ADJUST_ADDRESS (tramp
);
5272 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5273 Return NULL_RTX if a normal call should be emitted rather than expanding
5274 the function in-line. EXP is the expression that is a call to the builtin
5275 function; if convenient, the result should be placed in TARGET. */
5278 expand_builtin_signbit (tree exp
, rtx target
)
5280 const struct real_format
*fmt
;
5281 enum machine_mode fmode
, imode
, rmode
;
5282 HOST_WIDE_INT hi
, lo
;
5287 arglist
= TREE_OPERAND (exp
, 1);
5288 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
5291 arg
= TREE_VALUE (arglist
);
5292 fmode
= TYPE_MODE (TREE_TYPE (arg
));
5293 rmode
= TYPE_MODE (TREE_TYPE (exp
));
5294 fmt
= REAL_MODE_FORMAT (fmode
);
5296 /* For floating point formats without a sign bit, implement signbit
5298 bitpos
= fmt
->signbit_ro
;
5301 /* But we can't do this if the format supports signed zero. */
5302 if (fmt
->has_signed_zero
&& HONOR_SIGNED_ZEROS (fmode
))
5305 arg
= fold_build2 (LT_EXPR
, TREE_TYPE (exp
), arg
,
5306 build_real (TREE_TYPE (arg
), dconst0
));
5307 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
5310 temp
= expand_normal (arg
);
5311 if (GET_MODE_SIZE (fmode
) <= UNITS_PER_WORD
)
5313 imode
= int_mode_for_mode (fmode
);
5314 if (imode
== BLKmode
)
5316 temp
= gen_lowpart (imode
, temp
);
5321 /* Handle targets with different FP word orders. */
5322 if (FLOAT_WORDS_BIG_ENDIAN
)
5323 word
= (GET_MODE_BITSIZE (fmode
) - bitpos
) / BITS_PER_WORD
;
5325 word
= bitpos
/ BITS_PER_WORD
;
5326 temp
= operand_subword_force (temp
, word
, fmode
);
5327 bitpos
= bitpos
% BITS_PER_WORD
;
5330 /* Force the intermediate word_mode (or narrower) result into a
5331 register. This avoids attempting to create paradoxical SUBREGs
5332 of floating point modes below. */
5333 temp
= force_reg (imode
, temp
);
5335 /* If the bitpos is within the "result mode" lowpart, the operation
5336 can be implement with a single bitwise AND. Otherwise, we need
5337 a right shift and an AND. */
5339 if (bitpos
< GET_MODE_BITSIZE (rmode
))
5341 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
5344 lo
= (HOST_WIDE_INT
) 1 << bitpos
;
5348 hi
= (HOST_WIDE_INT
) 1 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
5353 temp
= gen_lowpart (rmode
, temp
);
5354 temp
= expand_binop (rmode
, and_optab
, temp
,
5355 immed_double_const (lo
, hi
, rmode
),
5356 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5360 /* Perform a logical right shift to place the signbit in the least
5361 significant bit, then truncate the result to the desired mode
5362 and mask just this bit. */
5363 temp
= expand_shift (RSHIFT_EXPR
, imode
, temp
,
5364 build_int_cst (NULL_TREE
, bitpos
), NULL_RTX
, 1);
5365 temp
= gen_lowpart (rmode
, temp
);
5366 temp
= expand_binop (rmode
, and_optab
, temp
, const1_rtx
,
5367 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5373 /* Expand fork or exec calls. TARGET is the desired target of the
5374 call. ARGLIST is the list of arguments of the call. FN is the
5375 identificator of the actual function. IGNORE is nonzero if the
5376 value is to be ignored. */
5379 expand_builtin_fork_or_exec (tree fn
, tree arglist
, rtx target
, int ignore
)
5384 /* If we are not profiling, just call the function. */
5385 if (!profile_arc_flag
)
5388 /* Otherwise call the wrapper. This should be equivalent for the rest of
5389 compiler, so the code does not diverge, and the wrapper may run the
5390 code necessary for keeping the profiling sane. */
5392 switch (DECL_FUNCTION_CODE (fn
))
5395 id
= get_identifier ("__gcov_fork");
5398 case BUILT_IN_EXECL
:
5399 id
= get_identifier ("__gcov_execl");
5402 case BUILT_IN_EXECV
:
5403 id
= get_identifier ("__gcov_execv");
5406 case BUILT_IN_EXECLP
:
5407 id
= get_identifier ("__gcov_execlp");
5410 case BUILT_IN_EXECLE
:
5411 id
= get_identifier ("__gcov_execle");
5414 case BUILT_IN_EXECVP
:
5415 id
= get_identifier ("__gcov_execvp");
5418 case BUILT_IN_EXECVE
:
5419 id
= get_identifier ("__gcov_execve");
5426 decl
= build_decl (FUNCTION_DECL
, id
, TREE_TYPE (fn
));
5427 DECL_EXTERNAL (decl
) = 1;
5428 TREE_PUBLIC (decl
) = 1;
5429 DECL_ARTIFICIAL (decl
) = 1;
5430 TREE_NOTHROW (decl
) = 1;
5431 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
5432 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
5433 call
= build_function_call_expr (decl
, arglist
);
5435 return expand_call (call
, target
, ignore
);
5439 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5440 the pointer in these functions is void*, the tree optimizers may remove
5441 casts. The mode computed in expand_builtin isn't reliable either, due
5442 to __sync_bool_compare_and_swap.
5444 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5445 group of builtins. This gives us log2 of the mode size. */
5447 static inline enum machine_mode
5448 get_builtin_sync_mode (int fcode_diff
)
5450 /* The size is not negotiable, so ask not to get BLKmode in return
5451 if the target indicates that a smaller size would be better. */
5452 return mode_for_size (BITS_PER_UNIT
<< fcode_diff
, MODE_INT
, 0);
5455 /* Expand the memory expression LOC and return the appropriate memory operand
5456 for the builtin_sync operations. */
5459 get_builtin_sync_mem (tree loc
, enum machine_mode mode
)
5463 addr
= expand_expr (loc
, NULL
, Pmode
, EXPAND_SUM
);
5465 /* Note that we explicitly do not want any alias information for this
5466 memory, so that we kill all other live memories. Otherwise we don't
5467 satisfy the full barrier semantics of the intrinsic. */
5468 mem
= validize_mem (gen_rtx_MEM (mode
, addr
));
5470 set_mem_align (mem
, get_pointer_alignment (loc
, BIGGEST_ALIGNMENT
));
5471 set_mem_alias_set (mem
, ALIAS_SET_MEMORY_BARRIER
);
5472 MEM_VOLATILE_P (mem
) = 1;
5477 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5478 ARGLIST is the operands list to the function. CODE is the rtx code
5479 that corresponds to the arithmetic or logical operation from the name;
5480 an exception here is that NOT actually means NAND. TARGET is an optional
5481 place for us to store the results; AFTER is true if this is the
5482 fetch_and_xxx form. IGNORE is true if we don't actually care about
5483 the result of the operation at all. */
5486 expand_builtin_sync_operation (enum machine_mode mode
, tree arglist
,
5487 enum rtx_code code
, bool after
,
5488 rtx target
, bool ignore
)
5492 /* Expand the operands. */
5493 mem
= get_builtin_sync_mem (TREE_VALUE (arglist
), mode
);
5495 arglist
= TREE_CHAIN (arglist
);
5496 val
= expand_expr (TREE_VALUE (arglist
), NULL
, mode
, EXPAND_NORMAL
);
5497 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5498 val
= convert_to_mode (mode
, val
, 1);
5501 return expand_sync_operation (mem
, val
, code
);
5503 return expand_sync_fetch_operation (mem
, val
, code
, after
, target
);
5506 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5507 intrinsics. ARGLIST is the operands list to the function. IS_BOOL is
5508 true if this is the boolean form. TARGET is a place for us to store the
5509 results; this is NOT optional if IS_BOOL is true. */
5512 expand_builtin_compare_and_swap (enum machine_mode mode
, tree arglist
,
5513 bool is_bool
, rtx target
)
5515 rtx old_val
, new_val
, mem
;
5517 /* Expand the operands. */
5518 mem
= get_builtin_sync_mem (TREE_VALUE (arglist
), mode
);
5520 arglist
= TREE_CHAIN (arglist
);
5521 old_val
= expand_expr (TREE_VALUE (arglist
), NULL
, mode
, EXPAND_NORMAL
);
5522 /* If OLD_VAL is promoted to a wider mode, convert it back to MODE. */
5523 old_val
= convert_to_mode (mode
, old_val
, 1);
5525 arglist
= TREE_CHAIN (arglist
);
5526 new_val
= expand_expr (TREE_VALUE (arglist
), NULL
, mode
, EXPAND_NORMAL
);
5527 /* If NEW_VAL is promoted to a wider mode, convert it back to MODE. */
5528 new_val
= convert_to_mode (mode
, new_val
, 1);
5531 return expand_bool_compare_and_swap (mem
, old_val
, new_val
, target
);
5533 return expand_val_compare_and_swap (mem
, old_val
, new_val
, target
);
5536 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5537 general form is actually an atomic exchange, and some targets only
5538 support a reduced form with the second argument being a constant 1.
5539 ARGLIST is the operands list to the function; TARGET is an optional
5540 place for us to store the results. */
5543 expand_builtin_lock_test_and_set (enum machine_mode mode
, tree arglist
,
5548 /* Expand the operands. */
5549 mem
= get_builtin_sync_mem (TREE_VALUE (arglist
), mode
);
5551 arglist
= TREE_CHAIN (arglist
);
5552 val
= expand_expr (TREE_VALUE (arglist
), NULL
, mode
, EXPAND_NORMAL
);
5553 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5554 val
= convert_to_mode (mode
, val
, 1);
5556 return expand_sync_lock_test_and_set (mem
, val
, target
);
5559 /* Expand the __sync_synchronize intrinsic. */
5562 expand_builtin_synchronize (void)
5566 #ifdef HAVE_memory_barrier
5567 if (HAVE_memory_barrier
)
5569 emit_insn (gen_memory_barrier ());
5574 /* If no explicit memory barrier instruction is available, create an
5575 empty asm stmt with a memory clobber. */
5576 x
= build4 (ASM_EXPR
, void_type_node
, build_string (0, ""), NULL
, NULL
,
5577 tree_cons (NULL
, build_string (6, "memory"), NULL
));
5578 ASM_VOLATILE_P (x
) = 1;
5579 expand_asm_expr (x
);
5582 /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list
5586 expand_builtin_lock_release (enum machine_mode mode
, tree arglist
)
5588 enum insn_code icode
;
5590 rtx val
= const0_rtx
;
5592 /* Expand the operands. */
5593 mem
= get_builtin_sync_mem (TREE_VALUE (arglist
), mode
);
5595 /* If there is an explicit operation in the md file, use it. */
5596 icode
= sync_lock_release
[mode
];
5597 if (icode
!= CODE_FOR_nothing
)
5599 if (!insn_data
[icode
].operand
[1].predicate (val
, mode
))
5600 val
= force_reg (mode
, val
);
5602 insn
= GEN_FCN (icode
) (mem
, val
);
5610 /* Otherwise we can implement this operation by emitting a barrier
5611 followed by a store of zero. */
5612 expand_builtin_synchronize ();
5613 emit_move_insn (mem
, val
);
5616 /* Expand an expression EXP that calls a built-in function,
5617 with result going to TARGET if that's convenient
5618 (and in mode MODE if that's convenient).
5619 SUBTARGET may be used as the target for computing one of EXP's operands.
5620 IGNORE is nonzero if the value is to be ignored. */
5623 expand_builtin (tree exp
, rtx target
, rtx subtarget
, enum machine_mode mode
,
5626 tree fndecl
= get_callee_fndecl (exp
);
5627 tree arglist
= TREE_OPERAND (exp
, 1);
5628 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5629 enum machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
5631 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5632 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
5634 /* When not optimizing, generate calls to library functions for a certain
5637 && !called_as_built_in (fndecl
)
5638 && DECL_ASSEMBLER_NAME_SET_P (fndecl
)
5639 && fcode
!= BUILT_IN_ALLOCA
)
5640 return expand_call (exp
, target
, ignore
);
5642 /* The built-in function expanders test for target == const0_rtx
5643 to determine whether the function's result will be ignored. */
5645 target
= const0_rtx
;
5647 /* If the result of a pure or const built-in function is ignored, and
5648 none of its arguments are volatile, we can avoid expanding the
5649 built-in call and just evaluate the arguments for side-effects. */
5650 if (target
== const0_rtx
5651 && (DECL_IS_PURE (fndecl
) || TREE_READONLY (fndecl
)))
5653 bool volatilep
= false;
5656 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
5657 if (TREE_THIS_VOLATILE (TREE_VALUE (arg
)))
5665 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
5666 expand_expr (TREE_VALUE (arg
), const0_rtx
,
5667 VOIDmode
, EXPAND_NORMAL
);
5674 CASE_FLT_FN (BUILT_IN_FABS
):
5675 target
= expand_builtin_fabs (arglist
, target
, subtarget
);
5680 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
5681 target
= expand_builtin_copysign (arglist
, target
, subtarget
);
5686 /* Just do a normal library call if we were unable to fold
5688 CASE_FLT_FN (BUILT_IN_CABS
):
5691 CASE_FLT_FN (BUILT_IN_EXP
):
5692 CASE_FLT_FN (BUILT_IN_EXP10
):
5693 CASE_FLT_FN (BUILT_IN_POW10
):
5694 CASE_FLT_FN (BUILT_IN_EXP2
):
5695 CASE_FLT_FN (BUILT_IN_EXPM1
):
5696 CASE_FLT_FN (BUILT_IN_LOGB
):
5697 CASE_FLT_FN (BUILT_IN_ILOGB
):
5698 CASE_FLT_FN (BUILT_IN_LOG
):
5699 CASE_FLT_FN (BUILT_IN_LOG10
):
5700 CASE_FLT_FN (BUILT_IN_LOG2
):
5701 CASE_FLT_FN (BUILT_IN_LOG1P
):
5702 CASE_FLT_FN (BUILT_IN_TAN
):
5703 CASE_FLT_FN (BUILT_IN_ASIN
):
5704 CASE_FLT_FN (BUILT_IN_ACOS
):
5705 CASE_FLT_FN (BUILT_IN_ATAN
):
5706 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5707 because of possible accuracy problems. */
5708 if (! flag_unsafe_math_optimizations
)
5710 CASE_FLT_FN (BUILT_IN_SQRT
):
5711 CASE_FLT_FN (BUILT_IN_FLOOR
):
5712 CASE_FLT_FN (BUILT_IN_CEIL
):
5713 CASE_FLT_FN (BUILT_IN_TRUNC
):
5714 CASE_FLT_FN (BUILT_IN_ROUND
):
5715 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
5716 CASE_FLT_FN (BUILT_IN_RINT
):
5717 CASE_FLT_FN (BUILT_IN_LRINT
):
5718 CASE_FLT_FN (BUILT_IN_LLRINT
):
5719 target
= expand_builtin_mathfn (exp
, target
, subtarget
);
5724 CASE_FLT_FN (BUILT_IN_LCEIL
):
5725 CASE_FLT_FN (BUILT_IN_LLCEIL
):
5726 CASE_FLT_FN (BUILT_IN_LFLOOR
):
5727 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
5728 target
= expand_builtin_int_roundingfn (exp
, target
, subtarget
);
5733 CASE_FLT_FN (BUILT_IN_POW
):
5734 target
= expand_builtin_pow (exp
, target
, subtarget
);
5739 CASE_FLT_FN (BUILT_IN_POWI
):
5740 target
= expand_builtin_powi (exp
, target
, subtarget
);
5745 CASE_FLT_FN (BUILT_IN_ATAN2
):
5746 CASE_FLT_FN (BUILT_IN_LDEXP
):
5747 CASE_FLT_FN (BUILT_IN_FMOD
):
5748 CASE_FLT_FN (BUILT_IN_DREM
):
5749 if (! flag_unsafe_math_optimizations
)
5751 target
= expand_builtin_mathfn_2 (exp
, target
, subtarget
);
5756 CASE_FLT_FN (BUILT_IN_SIN
):
5757 CASE_FLT_FN (BUILT_IN_COS
):
5758 if (! flag_unsafe_math_optimizations
)
5760 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
5765 CASE_FLT_FN (BUILT_IN_SINCOS
):
5766 if (! flag_unsafe_math_optimizations
)
5768 target
= expand_builtin_sincos (exp
);
5773 case BUILT_IN_APPLY_ARGS
:
5774 return expand_builtin_apply_args ();
5776 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5777 FUNCTION with a copy of the parameters described by
5778 ARGUMENTS, and ARGSIZE. It returns a block of memory
5779 allocated on the stack into which is stored all the registers
5780 that might possibly be used for returning the result of a
5781 function. ARGUMENTS is the value returned by
5782 __builtin_apply_args. ARGSIZE is the number of bytes of
5783 arguments that must be copied. ??? How should this value be
5784 computed? We'll also need a safe worst case value for varargs
5786 case BUILT_IN_APPLY
:
5787 if (!validate_arglist (arglist
, POINTER_TYPE
,
5788 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
5789 && !validate_arglist (arglist
, REFERENCE_TYPE
,
5790 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
5798 for (t
= arglist
, i
= 0; t
; t
= TREE_CHAIN (t
), i
++)
5799 ops
[i
] = expand_normal (TREE_VALUE (t
));
5801 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
5804 /* __builtin_return (RESULT) causes the function to return the
5805 value described by RESULT. RESULT is address of the block of
5806 memory returned by __builtin_apply. */
5807 case BUILT_IN_RETURN
:
5808 if (validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
5809 expand_builtin_return (expand_normal (TREE_VALUE (arglist
)));
5812 case BUILT_IN_SAVEREGS
:
5813 return expand_builtin_saveregs ();
5815 case BUILT_IN_ARGS_INFO
:
5816 return expand_builtin_args_info (arglist
);
5818 /* Return the address of the first anonymous stack arg. */
5819 case BUILT_IN_NEXT_ARG
:
5820 if (fold_builtin_next_arg (arglist
))
5822 return expand_builtin_next_arg ();
5824 case BUILT_IN_CLASSIFY_TYPE
:
5825 return expand_builtin_classify_type (arglist
);
5827 case BUILT_IN_CONSTANT_P
:
5830 case BUILT_IN_FRAME_ADDRESS
:
5831 case BUILT_IN_RETURN_ADDRESS
:
5832 return expand_builtin_frame_address (fndecl
, arglist
);
5834 /* Returns the address of the area where the structure is returned.
5836 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
5838 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
5839 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
5842 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
5844 case BUILT_IN_ALLOCA
:
5845 target
= expand_builtin_alloca (arglist
, target
);
5850 case BUILT_IN_STACK_SAVE
:
5851 return expand_stack_save ();
5853 case BUILT_IN_STACK_RESTORE
:
5854 expand_stack_restore (TREE_VALUE (arglist
));
5857 CASE_INT_FN (BUILT_IN_FFS
):
5858 case BUILT_IN_FFSIMAX
:
5859 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5860 subtarget
, ffs_optab
);
5865 CASE_INT_FN (BUILT_IN_CLZ
):
5866 case BUILT_IN_CLZIMAX
:
5867 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5868 subtarget
, clz_optab
);
5873 CASE_INT_FN (BUILT_IN_CTZ
):
5874 case BUILT_IN_CTZIMAX
:
5875 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5876 subtarget
, ctz_optab
);
5881 CASE_INT_FN (BUILT_IN_POPCOUNT
):
5882 case BUILT_IN_POPCOUNTIMAX
:
5883 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5884 subtarget
, popcount_optab
);
5889 CASE_INT_FN (BUILT_IN_PARITY
):
5890 case BUILT_IN_PARITYIMAX
:
5891 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5892 subtarget
, parity_optab
);
5897 case BUILT_IN_STRLEN
:
5898 target
= expand_builtin_strlen (arglist
, target
, target_mode
);
5903 case BUILT_IN_STRCPY
:
5904 target
= expand_builtin_strcpy (fndecl
, arglist
, target
, mode
);
5909 case BUILT_IN_STRNCPY
:
5910 target
= expand_builtin_strncpy (exp
, target
, mode
);
5915 case BUILT_IN_STPCPY
:
5916 target
= expand_builtin_stpcpy (exp
, target
, mode
);
5921 case BUILT_IN_STRCAT
:
5922 target
= expand_builtin_strcat (fndecl
, arglist
, target
, mode
);
5927 case BUILT_IN_STRNCAT
:
5928 target
= expand_builtin_strncat (arglist
, target
, mode
);
5933 case BUILT_IN_STRSPN
:
5934 target
= expand_builtin_strspn (arglist
, target
, mode
);
5939 case BUILT_IN_STRCSPN
:
5940 target
= expand_builtin_strcspn (arglist
, target
, mode
);
5945 case BUILT_IN_STRSTR
:
5946 target
= expand_builtin_strstr (arglist
, TREE_TYPE (exp
), target
, mode
);
5951 case BUILT_IN_STRPBRK
:
5952 target
= expand_builtin_strpbrk (arglist
, TREE_TYPE (exp
), target
, mode
);
5957 case BUILT_IN_INDEX
:
5958 case BUILT_IN_STRCHR
:
5959 target
= expand_builtin_strchr (arglist
, TREE_TYPE (exp
), target
, mode
);
5964 case BUILT_IN_RINDEX
:
5965 case BUILT_IN_STRRCHR
:
5966 target
= expand_builtin_strrchr (arglist
, TREE_TYPE (exp
), target
, mode
);
5971 case BUILT_IN_MEMCPY
:
5972 target
= expand_builtin_memcpy (exp
, target
, mode
);
5977 case BUILT_IN_MEMPCPY
:
5978 target
= expand_builtin_mempcpy (arglist
, TREE_TYPE (exp
), target
, mode
, /*endp=*/ 1);
5983 case BUILT_IN_MEMMOVE
:
5984 target
= expand_builtin_memmove (arglist
, TREE_TYPE (exp
), target
,
5990 case BUILT_IN_BCOPY
:
5991 target
= expand_builtin_bcopy (exp
);
5996 case BUILT_IN_MEMSET
:
5997 target
= expand_builtin_memset (arglist
, target
, mode
, exp
);
6002 case BUILT_IN_BZERO
:
6003 target
= expand_builtin_bzero (exp
);
6008 case BUILT_IN_STRCMP
:
6009 target
= expand_builtin_strcmp (exp
, target
, mode
);
6014 case BUILT_IN_STRNCMP
:
6015 target
= expand_builtin_strncmp (exp
, target
, mode
);
6021 case BUILT_IN_MEMCMP
:
6022 target
= expand_builtin_memcmp (exp
, arglist
, target
, mode
);
6027 case BUILT_IN_SETJMP
:
6028 /* This should have been lowered to the builtins below. */
6031 case BUILT_IN_SETJMP_SETUP
:
6032 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6033 and the receiver label. */
6034 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
6036 rtx buf_addr
= expand_expr (TREE_VALUE (arglist
), subtarget
,
6037 VOIDmode
, EXPAND_NORMAL
);
6038 tree label
= TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist
)), 0);
6039 rtx label_r
= label_rtx (label
);
6041 /* This is copied from the handling of non-local gotos. */
6042 expand_builtin_setjmp_setup (buf_addr
, label_r
);
6043 nonlocal_goto_handler_labels
6044 = gen_rtx_EXPR_LIST (VOIDmode
, label_r
,
6045 nonlocal_goto_handler_labels
);
6046 /* ??? Do not let expand_label treat us as such since we would
6047 not want to be both on the list of non-local labels and on
6048 the list of forced labels. */
6049 FORCED_LABEL (label
) = 0;
6054 case BUILT_IN_SETJMP_DISPATCHER
:
6055 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6056 if (validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
6058 tree label
= TREE_OPERAND (TREE_VALUE (arglist
), 0);
6059 rtx label_r
= label_rtx (label
);
6061 /* Remove the dispatcher label from the list of non-local labels
6062 since the receiver labels have been added to it above. */
6063 remove_node_from_expr_list (label_r
, &nonlocal_goto_handler_labels
);
6068 case BUILT_IN_SETJMP_RECEIVER
:
6069 /* __builtin_setjmp_receiver is passed the receiver label. */
6070 if (validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
6072 tree label
= TREE_OPERAND (TREE_VALUE (arglist
), 0);
6073 rtx label_r
= label_rtx (label
);
6075 expand_builtin_setjmp_receiver (label_r
);
6080 /* __builtin_longjmp is passed a pointer to an array of five words.
6081 It's similar to the C library longjmp function but works with
6082 __builtin_setjmp above. */
6083 case BUILT_IN_LONGJMP
:
6084 if (validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6086 rtx buf_addr
= expand_expr (TREE_VALUE (arglist
), subtarget
,
6087 VOIDmode
, EXPAND_NORMAL
);
6088 rtx value
= expand_normal (TREE_VALUE (TREE_CHAIN (arglist
)));
6090 if (value
!= const1_rtx
)
6092 error ("%<__builtin_longjmp%> second argument must be 1");
6096 expand_builtin_longjmp (buf_addr
, value
);
6101 case BUILT_IN_NONLOCAL_GOTO
:
6102 target
= expand_builtin_nonlocal_goto (arglist
);
6107 /* This updates the setjmp buffer that is its argument with the value
6108 of the current stack pointer. */
6109 case BUILT_IN_UPDATE_SETJMP_BUF
:
6110 if (validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
6113 = expand_normal (TREE_VALUE (arglist
));
6115 expand_builtin_update_setjmp_buf (buf_addr
);
6121 expand_builtin_trap ();
6124 case BUILT_IN_PRINTF
:
6125 target
= expand_builtin_printf (exp
, target
, mode
, false);
6130 case BUILT_IN_PRINTF_UNLOCKED
:
6131 target
= expand_builtin_printf (exp
, target
, mode
, true);
6136 case BUILT_IN_FPUTS
:
6137 target
= expand_builtin_fputs (arglist
, target
, false);
6141 case BUILT_IN_FPUTS_UNLOCKED
:
6142 target
= expand_builtin_fputs (arglist
, target
, true);
6147 case BUILT_IN_FPRINTF
:
6148 target
= expand_builtin_fprintf (exp
, target
, mode
, false);
6153 case BUILT_IN_FPRINTF_UNLOCKED
:
6154 target
= expand_builtin_fprintf (exp
, target
, mode
, true);
6159 case BUILT_IN_SPRINTF
:
6160 target
= expand_builtin_sprintf (arglist
, target
, mode
);
6165 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
6166 target
= expand_builtin_signbit (exp
, target
);
6171 /* Various hooks for the DWARF 2 __throw routine. */
6172 case BUILT_IN_UNWIND_INIT
:
6173 expand_builtin_unwind_init ();
6175 case BUILT_IN_DWARF_CFA
:
6176 return virtual_cfa_rtx
;
6177 #ifdef DWARF2_UNWIND_INFO
6178 case BUILT_IN_DWARF_SP_COLUMN
:
6179 return expand_builtin_dwarf_sp_column ();
6180 case BUILT_IN_INIT_DWARF_REG_SIZES
:
6181 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist
));
6184 case BUILT_IN_FROB_RETURN_ADDR
:
6185 return expand_builtin_frob_return_addr (TREE_VALUE (arglist
));
6186 case BUILT_IN_EXTRACT_RETURN_ADDR
:
6187 return expand_builtin_extract_return_addr (TREE_VALUE (arglist
));
6188 case BUILT_IN_EH_RETURN
:
6189 expand_builtin_eh_return (TREE_VALUE (arglist
),
6190 TREE_VALUE (TREE_CHAIN (arglist
)));
6192 #ifdef EH_RETURN_DATA_REGNO
6193 case BUILT_IN_EH_RETURN_DATA_REGNO
:
6194 return expand_builtin_eh_return_data_regno (arglist
);
6196 case BUILT_IN_EXTEND_POINTER
:
6197 return expand_builtin_extend_pointer (TREE_VALUE (arglist
));
6199 case BUILT_IN_VA_START
:
6200 case BUILT_IN_STDARG_START
:
6201 return expand_builtin_va_start (arglist
);
6202 case BUILT_IN_VA_END
:
6203 return expand_builtin_va_end (arglist
);
6204 case BUILT_IN_VA_COPY
:
6205 return expand_builtin_va_copy (arglist
);
6206 case BUILT_IN_EXPECT
:
6207 return expand_builtin_expect (arglist
, target
);
6208 case BUILT_IN_PREFETCH
:
6209 expand_builtin_prefetch (arglist
);
6212 case BUILT_IN_PROFILE_FUNC_ENTER
:
6213 return expand_builtin_profile_func (false);
6214 case BUILT_IN_PROFILE_FUNC_EXIT
:
6215 return expand_builtin_profile_func (true);
6217 case BUILT_IN_INIT_TRAMPOLINE
:
6218 return expand_builtin_init_trampoline (arglist
);
6219 case BUILT_IN_ADJUST_TRAMPOLINE
:
6220 return expand_builtin_adjust_trampoline (arglist
);
6223 case BUILT_IN_EXECL
:
6224 case BUILT_IN_EXECV
:
6225 case BUILT_IN_EXECLP
:
6226 case BUILT_IN_EXECLE
:
6227 case BUILT_IN_EXECVP
:
6228 case BUILT_IN_EXECVE
:
6229 target
= expand_builtin_fork_or_exec (fndecl
, arglist
, target
, ignore
);
6234 case BUILT_IN_FETCH_AND_ADD_1
:
6235 case BUILT_IN_FETCH_AND_ADD_2
:
6236 case BUILT_IN_FETCH_AND_ADD_4
:
6237 case BUILT_IN_FETCH_AND_ADD_8
:
6238 case BUILT_IN_FETCH_AND_ADD_16
:
6239 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_ADD_1
);
6240 target
= expand_builtin_sync_operation (mode
, arglist
, PLUS
,
6241 false, target
, ignore
);
6246 case BUILT_IN_FETCH_AND_SUB_1
:
6247 case BUILT_IN_FETCH_AND_SUB_2
:
6248 case BUILT_IN_FETCH_AND_SUB_4
:
6249 case BUILT_IN_FETCH_AND_SUB_8
:
6250 case BUILT_IN_FETCH_AND_SUB_16
:
6251 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_SUB_1
);
6252 target
= expand_builtin_sync_operation (mode
, arglist
, MINUS
,
6253 false, target
, ignore
);
6258 case BUILT_IN_FETCH_AND_OR_1
:
6259 case BUILT_IN_FETCH_AND_OR_2
:
6260 case BUILT_IN_FETCH_AND_OR_4
:
6261 case BUILT_IN_FETCH_AND_OR_8
:
6262 case BUILT_IN_FETCH_AND_OR_16
:
6263 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_OR_1
);
6264 target
= expand_builtin_sync_operation (mode
, arglist
, IOR
,
6265 false, target
, ignore
);
6270 case BUILT_IN_FETCH_AND_AND_1
:
6271 case BUILT_IN_FETCH_AND_AND_2
:
6272 case BUILT_IN_FETCH_AND_AND_4
:
6273 case BUILT_IN_FETCH_AND_AND_8
:
6274 case BUILT_IN_FETCH_AND_AND_16
:
6275 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_AND_1
);
6276 target
= expand_builtin_sync_operation (mode
, arglist
, AND
,
6277 false, target
, ignore
);
6282 case BUILT_IN_FETCH_AND_XOR_1
:
6283 case BUILT_IN_FETCH_AND_XOR_2
:
6284 case BUILT_IN_FETCH_AND_XOR_4
:
6285 case BUILT_IN_FETCH_AND_XOR_8
:
6286 case BUILT_IN_FETCH_AND_XOR_16
:
6287 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_XOR_1
);
6288 target
= expand_builtin_sync_operation (mode
, arglist
, XOR
,
6289 false, target
, ignore
);
6294 case BUILT_IN_FETCH_AND_NAND_1
:
6295 case BUILT_IN_FETCH_AND_NAND_2
:
6296 case BUILT_IN_FETCH_AND_NAND_4
:
6297 case BUILT_IN_FETCH_AND_NAND_8
:
6298 case BUILT_IN_FETCH_AND_NAND_16
:
6299 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_NAND_1
);
6300 target
= expand_builtin_sync_operation (mode
, arglist
, NOT
,
6301 false, target
, ignore
);
6306 case BUILT_IN_ADD_AND_FETCH_1
:
6307 case BUILT_IN_ADD_AND_FETCH_2
:
6308 case BUILT_IN_ADD_AND_FETCH_4
:
6309 case BUILT_IN_ADD_AND_FETCH_8
:
6310 case BUILT_IN_ADD_AND_FETCH_16
:
6311 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ADD_AND_FETCH_1
);
6312 target
= expand_builtin_sync_operation (mode
, arglist
, PLUS
,
6313 true, target
, ignore
);
6318 case BUILT_IN_SUB_AND_FETCH_1
:
6319 case BUILT_IN_SUB_AND_FETCH_2
:
6320 case BUILT_IN_SUB_AND_FETCH_4
:
6321 case BUILT_IN_SUB_AND_FETCH_8
:
6322 case BUILT_IN_SUB_AND_FETCH_16
:
6323 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SUB_AND_FETCH_1
);
6324 target
= expand_builtin_sync_operation (mode
, arglist
, MINUS
,
6325 true, target
, ignore
);
6330 case BUILT_IN_OR_AND_FETCH_1
:
6331 case BUILT_IN_OR_AND_FETCH_2
:
6332 case BUILT_IN_OR_AND_FETCH_4
:
6333 case BUILT_IN_OR_AND_FETCH_8
:
6334 case BUILT_IN_OR_AND_FETCH_16
:
6335 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_OR_AND_FETCH_1
);
6336 target
= expand_builtin_sync_operation (mode
, arglist
, IOR
,
6337 true, target
, ignore
);
6342 case BUILT_IN_AND_AND_FETCH_1
:
6343 case BUILT_IN_AND_AND_FETCH_2
:
6344 case BUILT_IN_AND_AND_FETCH_4
:
6345 case BUILT_IN_AND_AND_FETCH_8
:
6346 case BUILT_IN_AND_AND_FETCH_16
:
6347 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_AND_AND_FETCH_1
);
6348 target
= expand_builtin_sync_operation (mode
, arglist
, AND
,
6349 true, target
, ignore
);
6354 case BUILT_IN_XOR_AND_FETCH_1
:
6355 case BUILT_IN_XOR_AND_FETCH_2
:
6356 case BUILT_IN_XOR_AND_FETCH_4
:
6357 case BUILT_IN_XOR_AND_FETCH_8
:
6358 case BUILT_IN_XOR_AND_FETCH_16
:
6359 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_XOR_AND_FETCH_1
);
6360 target
= expand_builtin_sync_operation (mode
, arglist
, XOR
,
6361 true, target
, ignore
);
6366 case BUILT_IN_NAND_AND_FETCH_1
:
6367 case BUILT_IN_NAND_AND_FETCH_2
:
6368 case BUILT_IN_NAND_AND_FETCH_4
:
6369 case BUILT_IN_NAND_AND_FETCH_8
:
6370 case BUILT_IN_NAND_AND_FETCH_16
:
6371 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_NAND_AND_FETCH_1
);
6372 target
= expand_builtin_sync_operation (mode
, arglist
, NOT
,
6373 true, target
, ignore
);
6378 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1
:
6379 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2
:
6380 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4
:
6381 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8
:
6382 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16
:
6383 if (mode
== VOIDmode
)
6384 mode
= TYPE_MODE (boolean_type_node
);
6385 if (!target
|| !register_operand (target
, mode
))
6386 target
= gen_reg_rtx (mode
);
6388 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_BOOL_COMPARE_AND_SWAP_1
);
6389 target
= expand_builtin_compare_and_swap (mode
, arglist
, true, target
);
6394 case BUILT_IN_VAL_COMPARE_AND_SWAP_1
:
6395 case BUILT_IN_VAL_COMPARE_AND_SWAP_2
:
6396 case BUILT_IN_VAL_COMPARE_AND_SWAP_4
:
6397 case BUILT_IN_VAL_COMPARE_AND_SWAP_8
:
6398 case BUILT_IN_VAL_COMPARE_AND_SWAP_16
:
6399 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_VAL_COMPARE_AND_SWAP_1
);
6400 target
= expand_builtin_compare_and_swap (mode
, arglist
, false, target
);
6405 case BUILT_IN_LOCK_TEST_AND_SET_1
:
6406 case BUILT_IN_LOCK_TEST_AND_SET_2
:
6407 case BUILT_IN_LOCK_TEST_AND_SET_4
:
6408 case BUILT_IN_LOCK_TEST_AND_SET_8
:
6409 case BUILT_IN_LOCK_TEST_AND_SET_16
:
6410 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_LOCK_TEST_AND_SET_1
);
6411 target
= expand_builtin_lock_test_and_set (mode
, arglist
, target
);
6416 case BUILT_IN_LOCK_RELEASE_1
:
6417 case BUILT_IN_LOCK_RELEASE_2
:
6418 case BUILT_IN_LOCK_RELEASE_4
:
6419 case BUILT_IN_LOCK_RELEASE_8
:
6420 case BUILT_IN_LOCK_RELEASE_16
:
6421 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_LOCK_RELEASE_1
);
6422 expand_builtin_lock_release (mode
, arglist
);
6425 case BUILT_IN_SYNCHRONIZE
:
6426 expand_builtin_synchronize ();
6429 case BUILT_IN_OBJECT_SIZE
:
6430 return expand_builtin_object_size (exp
);
6432 case BUILT_IN_MEMCPY_CHK
:
6433 case BUILT_IN_MEMPCPY_CHK
:
6434 case BUILT_IN_MEMMOVE_CHK
:
6435 case BUILT_IN_MEMSET_CHK
:
6436 target
= expand_builtin_memory_chk (exp
, target
, mode
, fcode
);
6441 case BUILT_IN_STRCPY_CHK
:
6442 case BUILT_IN_STPCPY_CHK
:
6443 case BUILT_IN_STRNCPY_CHK
:
6444 case BUILT_IN_STRCAT_CHK
:
6445 case BUILT_IN_SNPRINTF_CHK
:
6446 case BUILT_IN_VSNPRINTF_CHK
:
6447 maybe_emit_chk_warning (exp
, fcode
);
6450 case BUILT_IN_SPRINTF_CHK
:
6451 case BUILT_IN_VSPRINTF_CHK
:
6452 maybe_emit_sprintf_chk_warning (exp
, fcode
);
6455 default: /* just do library call, if unknown builtin */
6459 /* The switch statement above can drop through to cause the function
6460 to be called normally. */
6461 return expand_call (exp
, target
, ignore
);
6464 /* Determine whether a tree node represents a call to a built-in
6465 function. If the tree T is a call to a built-in function with
6466 the right number of arguments of the appropriate types, return
6467 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6468 Otherwise the return value is END_BUILTINS. */
6470 enum built_in_function
6471 builtin_mathfn_code (tree t
)
6473 tree fndecl
, arglist
, parmlist
;
6474 tree argtype
, parmtype
;
6476 if (TREE_CODE (t
) != CALL_EXPR
6477 || TREE_CODE (TREE_OPERAND (t
, 0)) != ADDR_EXPR
)
6478 return END_BUILTINS
;
6480 fndecl
= get_callee_fndecl (t
);
6481 if (fndecl
== NULL_TREE
6482 || TREE_CODE (fndecl
) != FUNCTION_DECL
6483 || ! DECL_BUILT_IN (fndecl
)
6484 || DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
6485 return END_BUILTINS
;
6487 arglist
= TREE_OPERAND (t
, 1);
6488 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
6489 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
6491 /* If a function doesn't take a variable number of arguments,
6492 the last element in the list will have type `void'. */
6493 parmtype
= TREE_VALUE (parmlist
);
6494 if (VOID_TYPE_P (parmtype
))
6497 return END_BUILTINS
;
6498 return DECL_FUNCTION_CODE (fndecl
);
6502 return END_BUILTINS
;
6504 argtype
= TREE_TYPE (TREE_VALUE (arglist
));
6506 if (SCALAR_FLOAT_TYPE_P (parmtype
))
6508 if (! SCALAR_FLOAT_TYPE_P (argtype
))
6509 return END_BUILTINS
;
6511 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
6513 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
6514 return END_BUILTINS
;
6516 else if (POINTER_TYPE_P (parmtype
))
6518 if (! POINTER_TYPE_P (argtype
))
6519 return END_BUILTINS
;
6521 else if (INTEGRAL_TYPE_P (parmtype
))
6523 if (! INTEGRAL_TYPE_P (argtype
))
6524 return END_BUILTINS
;
6527 return END_BUILTINS
;
6529 arglist
= TREE_CHAIN (arglist
);
6532 /* Variable-length argument list. */
6533 return DECL_FUNCTION_CODE (fndecl
);
6536 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6537 constant. ARGLIST is the argument list of the call. */
6540 fold_builtin_constant_p (tree arglist
)
6545 arglist
= TREE_VALUE (arglist
);
6547 /* We return 1 for a numeric type that's known to be a constant
6548 value at compile-time or for an aggregate type that's a
6549 literal constant. */
6550 STRIP_NOPS (arglist
);
6552 /* If we know this is a constant, emit the constant of one. */
6553 if (CONSTANT_CLASS_P (arglist
)
6554 || (TREE_CODE (arglist
) == CONSTRUCTOR
6555 && TREE_CONSTANT (arglist
)))
6556 return integer_one_node
;
6557 if (TREE_CODE (arglist
) == ADDR_EXPR
)
6559 tree op
= TREE_OPERAND (arglist
, 0);
6560 if (TREE_CODE (op
) == STRING_CST
6561 || (TREE_CODE (op
) == ARRAY_REF
6562 && integer_zerop (TREE_OPERAND (op
, 1))
6563 && TREE_CODE (TREE_OPERAND (op
, 0)) == STRING_CST
))
6564 return integer_one_node
;
6567 /* If this expression has side effects, show we don't know it to be a
6568 constant. Likewise if it's a pointer or aggregate type since in
6569 those case we only want literals, since those are only optimized
6570 when generating RTL, not later.
6571 And finally, if we are compiling an initializer, not code, we
6572 need to return a definite result now; there's not going to be any
6573 more optimization done. */
6574 if (TREE_SIDE_EFFECTS (arglist
)
6575 || AGGREGATE_TYPE_P (TREE_TYPE (arglist
))
6576 || POINTER_TYPE_P (TREE_TYPE (arglist
))
6578 || folding_initializer
)
6579 return integer_zero_node
;
6584 /* Fold a call to __builtin_expect, if we expect that a comparison against
6585 the argument will fold to a constant. In practice, this means a true
6586 constant or the address of a non-weak symbol. ARGLIST is the argument
6587 list of the call. */
6590 fold_builtin_expect (tree arglist
)
6597 arg
= TREE_VALUE (arglist
);
6599 /* If the argument isn't invariant, then there's nothing we can do. */
6600 if (!TREE_INVARIANT (arg
))
6603 /* If we're looking at an address of a weak decl, then do not fold. */
6606 if (TREE_CODE (inner
) == ADDR_EXPR
)
6610 inner
= TREE_OPERAND (inner
, 0);
6612 while (TREE_CODE (inner
) == COMPONENT_REF
6613 || TREE_CODE (inner
) == ARRAY_REF
);
6614 if (DECL_P (inner
) && DECL_WEAK (inner
))
6618 /* Otherwise, ARG already has the proper type for the return value. */
6622 /* Fold a call to __builtin_classify_type. */
6625 fold_builtin_classify_type (tree arglist
)
6628 return build_int_cst (NULL_TREE
, no_type_class
);
6630 return build_int_cst (NULL_TREE
,
6631 type_to_class (TREE_TYPE (TREE_VALUE (arglist
))));
6634 /* Fold a call to __builtin_strlen. */
6637 fold_builtin_strlen (tree arglist
)
6639 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
6643 tree len
= c_strlen (TREE_VALUE (arglist
), 0);
6647 /* Convert from the internal "sizetype" type to "size_t". */
6649 len
= fold_convert (size_type_node
, len
);
6657 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6660 fold_builtin_inf (tree type
, int warn
)
6662 REAL_VALUE_TYPE real
;
6664 /* __builtin_inff is intended to be usable to define INFINITY on all
6665 targets. If an infinity is not available, INFINITY expands "to a
6666 positive constant of type float that overflows at translation
6667 time", footnote "In this case, using INFINITY will violate the
6668 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6669 Thus we pedwarn to ensure this constraint violation is
6671 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
6672 pedwarn ("target format does not support infinity");
6675 return build_real (type
, real
);
6678 /* Fold a call to __builtin_nan or __builtin_nans. */
6681 fold_builtin_nan (tree arglist
, tree type
, int quiet
)
6683 REAL_VALUE_TYPE real
;
6686 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
6688 str
= c_getstr (TREE_VALUE (arglist
));
6692 if (!real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
6695 return build_real (type
, real
);
6698 /* Return true if the floating point expression T has an integer value.
6699 We also allow +Inf, -Inf and NaN to be considered integer values. */
6702 integer_valued_real_p (tree t
)
6704 switch (TREE_CODE (t
))
6711 case NON_LVALUE_EXPR
:
6712 return integer_valued_real_p (TREE_OPERAND (t
, 0));
6717 return integer_valued_real_p (TREE_OPERAND (t
, 1));
6724 return integer_valued_real_p (TREE_OPERAND (t
, 0))
6725 && integer_valued_real_p (TREE_OPERAND (t
, 1));
6728 return integer_valued_real_p (TREE_OPERAND (t
, 1))
6729 && integer_valued_real_p (TREE_OPERAND (t
, 2));
6732 if (! TREE_CONSTANT_OVERFLOW (t
))
6734 REAL_VALUE_TYPE c
, cint
;
6736 c
= TREE_REAL_CST (t
);
6737 real_trunc (&cint
, TYPE_MODE (TREE_TYPE (t
)), &c
);
6738 return real_identical (&c
, &cint
);
6744 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
6745 if (TREE_CODE (type
) == INTEGER_TYPE
)
6747 if (TREE_CODE (type
) == REAL_TYPE
)
6748 return integer_valued_real_p (TREE_OPERAND (t
, 0));
6753 switch (builtin_mathfn_code (t
))
6755 CASE_FLT_FN (BUILT_IN_CEIL
):
6756 CASE_FLT_FN (BUILT_IN_FLOOR
):
6757 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
6758 CASE_FLT_FN (BUILT_IN_RINT
):
6759 CASE_FLT_FN (BUILT_IN_ROUND
):
6760 CASE_FLT_FN (BUILT_IN_TRUNC
):
6774 /* EXP is assumed to be builtin call where truncation can be propagated
6775 across (for instance floor((double)f) == (double)floorf (f).
6776 Do the transformation. */
6779 fold_trunc_transparent_mathfn (tree fndecl
, tree arglist
)
6781 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
6784 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6787 arg
= TREE_VALUE (arglist
);
6788 /* Integer rounding functions are idempotent. */
6789 if (fcode
== builtin_mathfn_code (arg
))
6792 /* If argument is already integer valued, and we don't need to worry
6793 about setting errno, there's no need to perform rounding. */
6794 if (! flag_errno_math
&& integer_valued_real_p (arg
))
6799 tree arg0
= strip_float_extensions (arg
);
6800 tree ftype
= TREE_TYPE (TREE_TYPE (fndecl
));
6801 tree newtype
= TREE_TYPE (arg0
);
6804 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
6805 && (decl
= mathfn_built_in (newtype
, fcode
)))
6808 build_tree_list (NULL_TREE
, fold_convert (newtype
, arg0
));
6809 return fold_convert (ftype
,
6810 build_function_call_expr (decl
, arglist
));
6816 /* EXP is assumed to be builtin call which can narrow the FP type of
6817 the argument, for instance lround((double)f) -> lroundf (f). */
6820 fold_fixed_mathfn (tree fndecl
, tree arglist
)
6822 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
6825 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6828 arg
= TREE_VALUE (arglist
);
6830 /* If argument is already integer valued, and we don't need to worry
6831 about setting errno, there's no need to perform rounding. */
6832 if (! flag_errno_math
&& integer_valued_real_p (arg
))
6833 return fold_build1 (FIX_TRUNC_EXPR
, TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
6837 tree ftype
= TREE_TYPE (arg
);
6838 tree arg0
= strip_float_extensions (arg
);
6839 tree newtype
= TREE_TYPE (arg0
);
6842 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
6843 && (decl
= mathfn_built_in (newtype
, fcode
)))
6846 build_tree_list (NULL_TREE
, fold_convert (newtype
, arg0
));
6847 return build_function_call_expr (decl
, arglist
);
6851 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6852 sizeof (long long) == sizeof (long). */
6853 if (TYPE_PRECISION (long_long_integer_type_node
)
6854 == TYPE_PRECISION (long_integer_type_node
))
6856 tree newfn
= NULL_TREE
;
6859 CASE_FLT_FN (BUILT_IN_LLCEIL
):
6860 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
6863 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
6864 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
6867 CASE_FLT_FN (BUILT_IN_LLROUND
):
6868 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
6871 CASE_FLT_FN (BUILT_IN_LLRINT
):
6872 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
6881 tree newcall
= build_function_call_expr (newfn
, arglist
);
6882 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
6889 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6890 is the argument list, TYPE is the return type and FNDECL is the
6891 original function DECL. Return NULL_TREE if no if no simplification
6895 fold_builtin_cabs (tree arglist
, tree type
, tree fndecl
)
6899 if (!arglist
|| TREE_CHAIN (arglist
))
6902 arg
= TREE_VALUE (arglist
);
6903 if (TREE_CODE (TREE_TYPE (arg
)) != COMPLEX_TYPE
6904 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
6907 /* Evaluate cabs of a constant at compile-time. */
6908 if (flag_unsafe_math_optimizations
6909 && TREE_CODE (arg
) == COMPLEX_CST
6910 && TREE_CODE (TREE_REALPART (arg
)) == REAL_CST
6911 && TREE_CODE (TREE_IMAGPART (arg
)) == REAL_CST
6912 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg
))
6913 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg
)))
6915 REAL_VALUE_TYPE r
, i
;
6917 r
= TREE_REAL_CST (TREE_REALPART (arg
));
6918 i
= TREE_REAL_CST (TREE_IMAGPART (arg
));
6920 real_arithmetic (&r
, MULT_EXPR
, &r
, &r
);
6921 real_arithmetic (&i
, MULT_EXPR
, &i
, &i
);
6922 real_arithmetic (&r
, PLUS_EXPR
, &r
, &i
);
6923 if (real_sqrt (&r
, TYPE_MODE (type
), &r
)
6924 || ! flag_trapping_math
)
6925 return build_real (type
, r
);
6928 /* If either part is zero, cabs is fabs of the other. */
6929 if (TREE_CODE (arg
) == COMPLEX_EXPR
6930 && real_zerop (TREE_OPERAND (arg
, 0)))
6931 return fold_build1 (ABS_EXPR
, type
, TREE_OPERAND (arg
, 1));
6932 if (TREE_CODE (arg
) == COMPLEX_EXPR
6933 && real_zerop (TREE_OPERAND (arg
, 1)))
6934 return fold_build1 (ABS_EXPR
, type
, TREE_OPERAND (arg
, 0));
6936 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
6937 if (TREE_CODE (arg
) == NEGATE_EXPR
6938 || TREE_CODE (arg
) == CONJ_EXPR
)
6940 tree arglist
= build_tree_list (NULL_TREE
, TREE_OPERAND (arg
, 0));
6941 return build_function_call_expr (fndecl
, arglist
);
6944 /* Don't do this when optimizing for size. */
6945 if (flag_unsafe_math_optimizations
6946 && optimize
&& !optimize_size
)
6948 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
6950 if (sqrtfn
!= NULL_TREE
)
6952 tree rpart
, ipart
, result
, arglist
;
6954 arg
= builtin_save_expr (arg
);
6956 rpart
= fold_build1 (REALPART_EXPR
, type
, arg
);
6957 ipart
= fold_build1 (IMAGPART_EXPR
, type
, arg
);
6959 rpart
= builtin_save_expr (rpart
);
6960 ipart
= builtin_save_expr (ipart
);
6962 result
= fold_build2 (PLUS_EXPR
, type
,
6963 fold_build2 (MULT_EXPR
, type
,
6965 fold_build2 (MULT_EXPR
, type
,
6968 arglist
= build_tree_list (NULL_TREE
, result
);
6969 return build_function_call_expr (sqrtfn
, arglist
);
6976 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
6977 NULL_TREE if no simplification can be made. */
6980 fold_builtin_sqrt (tree arglist
, tree type
)
6983 enum built_in_function fcode
;
6984 tree arg
= TREE_VALUE (arglist
);
6986 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6989 /* Optimize sqrt of constant value. */
6990 if (TREE_CODE (arg
) == REAL_CST
6991 && ! TREE_CONSTANT_OVERFLOW (arg
))
6993 REAL_VALUE_TYPE r
, x
;
6995 x
= TREE_REAL_CST (arg
);
6996 if (real_sqrt (&r
, TYPE_MODE (type
), &x
)
6997 || (!flag_trapping_math
&& !flag_errno_math
))
6998 return build_real (type
, r
);
7001 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7002 fcode
= builtin_mathfn_code (arg
);
7003 if (flag_unsafe_math_optimizations
&& BUILTIN_EXPONENT_P (fcode
))
7005 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
7006 arg
= fold_build2 (MULT_EXPR
, type
,
7007 TREE_VALUE (TREE_OPERAND (arg
, 1)),
7008 build_real (type
, dconsthalf
));
7009 arglist
= build_tree_list (NULL_TREE
, arg
);
7010 return build_function_call_expr (expfn
, arglist
);
7013 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7014 if (flag_unsafe_math_optimizations
&& BUILTIN_ROOT_P (fcode
))
7016 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7020 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7022 /* The inner root was either sqrt or cbrt. */
7023 REAL_VALUE_TYPE dconstroot
=
7024 BUILTIN_SQRT_P (fcode
) ? dconsthalf
: dconstthird
;
7026 /* Adjust for the outer root. */
7027 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7028 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7029 tree_root
= build_real (type
, dconstroot
);
7030 arglist
= tree_cons (NULL_TREE
, arg0
,
7031 build_tree_list (NULL_TREE
, tree_root
));
7032 return build_function_call_expr (powfn
, arglist
);
7036 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7037 if (flag_unsafe_math_optimizations
7038 && (fcode
== BUILT_IN_POW
7039 || fcode
== BUILT_IN_POWF
7040 || fcode
== BUILT_IN_POWL
))
7042 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
7043 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7044 tree arg1
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg
, 1)));
7046 if (!tree_expr_nonnegative_p (arg0
))
7047 arg0
= build1 (ABS_EXPR
, type
, arg0
);
7048 narg1
= fold_build2 (MULT_EXPR
, type
, arg1
,
7049 build_real (type
, dconsthalf
));
7050 arglist
= tree_cons (NULL_TREE
, arg0
,
7051 build_tree_list (NULL_TREE
, narg1
));
7052 return build_function_call_expr (powfn
, arglist
);
7058 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
7059 NULL_TREE if no simplification can be made. */
7061 fold_builtin_cbrt (tree arglist
, tree type
)
7063 tree arg
= TREE_VALUE (arglist
);
7064 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
7066 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7069 /* Optimize cbrt of constant value. */
7070 if (real_zerop (arg
) || real_onep (arg
) || real_minus_onep (arg
))
7073 if (flag_unsafe_math_optimizations
)
7075 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7076 if (BUILTIN_EXPONENT_P (fcode
))
7078 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
7079 const REAL_VALUE_TYPE third_trunc
=
7080 real_value_truncate (TYPE_MODE (type
), dconstthird
);
7081 arg
= fold_build2 (MULT_EXPR
, type
,
7082 TREE_VALUE (TREE_OPERAND (arg
, 1)),
7083 build_real (type
, third_trunc
));
7084 arglist
= build_tree_list (NULL_TREE
, arg
);
7085 return build_function_call_expr (expfn
, arglist
);
7088 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7089 if (BUILTIN_SQRT_P (fcode
))
7091 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7095 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7097 REAL_VALUE_TYPE dconstroot
= dconstthird
;
7099 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7100 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7101 tree_root
= build_real (type
, dconstroot
);
7102 arglist
= tree_cons (NULL_TREE
, arg0
,
7103 build_tree_list (NULL_TREE
, tree_root
));
7104 return build_function_call_expr (powfn
, arglist
);
7108 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7109 if (BUILTIN_CBRT_P (fcode
))
7111 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7112 if (tree_expr_nonnegative_p (arg0
))
7114 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7119 REAL_VALUE_TYPE dconstroot
;
7121 real_arithmetic (&dconstroot
, MULT_EXPR
, &dconstthird
, &dconstthird
);
7122 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7123 tree_root
= build_real (type
, dconstroot
);
7124 arglist
= tree_cons (NULL_TREE
, arg0
,
7125 build_tree_list (NULL_TREE
, tree_root
));
7126 return build_function_call_expr (powfn
, arglist
);
7131 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7132 if (fcode
== BUILT_IN_POW
|| fcode
== BUILT_IN_POWF
7133 || fcode
== BUILT_IN_POWL
)
7135 tree arg00
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7136 tree arg01
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg
, 1)));
7137 if (tree_expr_nonnegative_p (arg00
))
7139 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
7140 const REAL_VALUE_TYPE dconstroot
7141 = real_value_truncate (TYPE_MODE (type
), dconstthird
);
7142 tree narg01
= fold_build2 (MULT_EXPR
, type
, arg01
,
7143 build_real (type
, dconstroot
));
7144 arglist
= tree_cons (NULL_TREE
, arg00
,
7145 build_tree_list (NULL_TREE
, narg01
));
7146 return build_function_call_expr (powfn
, arglist
);
7153 /* Fold function call to builtin sin, sinf, or sinl. Return
7154 NULL_TREE if no simplification can be made. */
7156 fold_builtin_sin (tree arglist
)
7158 tree arg
= TREE_VALUE (arglist
);
7160 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7163 /* Optimize sin (0.0) = 0.0. */
7164 if (real_zerop (arg
))
7170 /* Fold function call to builtin cos, cosf, or cosl. Return
7171 NULL_TREE if no simplification can be made. */
7173 fold_builtin_cos (tree arglist
, tree type
, tree fndecl
)
7175 tree arg
= TREE_VALUE (arglist
);
7177 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7180 /* Optimize cos (0.0) = 1.0. */
7181 if (real_zerop (arg
))
7182 return build_real (type
, dconst1
);
7184 /* Optimize cos(-x) into cos (x). */
7185 if (TREE_CODE (arg
) == NEGATE_EXPR
)
7187 tree args
= build_tree_list (NULL_TREE
,
7188 TREE_OPERAND (arg
, 0));
7189 return build_function_call_expr (fndecl
, args
);
7195 /* Fold function call to builtin tan, tanf, or tanl. Return
7196 NULL_TREE if no simplification can be made. */
7198 fold_builtin_tan (tree arglist
)
7200 enum built_in_function fcode
;
7201 tree arg
= TREE_VALUE (arglist
);
7203 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7206 /* Optimize tan(0.0) = 0.0. */
7207 if (real_zerop (arg
))
7210 /* Optimize tan(atan(x)) = x. */
7211 fcode
= builtin_mathfn_code (arg
);
7212 if (flag_unsafe_math_optimizations
7213 && (fcode
== BUILT_IN_ATAN
7214 || fcode
== BUILT_IN_ATANF
7215 || fcode
== BUILT_IN_ATANL
))
7216 return TREE_VALUE (TREE_OPERAND (arg
, 1));
7221 /* Fold function call to builtin atan, atanf, or atanl. Return
7222 NULL_TREE if no simplification can be made. */
7225 fold_builtin_atan (tree arglist
, tree type
)
7228 tree arg
= TREE_VALUE (arglist
);
7230 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7233 /* Optimize atan(0.0) = 0.0. */
7234 if (real_zerop (arg
))
7237 /* Optimize atan(1.0) = pi/4. */
7238 if (real_onep (arg
))
7240 REAL_VALUE_TYPE cst
;
7242 real_convert (&cst
, TYPE_MODE (type
), &dconstpi
);
7243 SET_REAL_EXP (&cst
, REAL_EXP (&cst
) - 2);
7244 return build_real (type
, cst
);
7250 /* Fold function call to builtin trunc, truncf or truncl. Return
7251 NULL_TREE if no simplification can be made. */
7254 fold_builtin_trunc (tree fndecl
, tree arglist
)
7258 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7261 /* Optimize trunc of constant value. */
7262 arg
= TREE_VALUE (arglist
);
7263 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7265 REAL_VALUE_TYPE r
, x
;
7266 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7268 x
= TREE_REAL_CST (arg
);
7269 real_trunc (&r
, TYPE_MODE (type
), &x
);
7270 return build_real (type
, r
);
7273 return fold_trunc_transparent_mathfn (fndecl
, arglist
);
7276 /* Fold function call to builtin floor, floorf or floorl. Return
7277 NULL_TREE if no simplification can be made. */
7280 fold_builtin_floor (tree fndecl
, tree arglist
)
7284 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7287 /* Optimize floor of constant value. */
7288 arg
= TREE_VALUE (arglist
);
7289 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7293 x
= TREE_REAL_CST (arg
);
7294 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7296 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7299 real_floor (&r
, TYPE_MODE (type
), &x
);
7300 return build_real (type
, r
);
7304 return fold_trunc_transparent_mathfn (fndecl
, arglist
);
7307 /* Fold function call to builtin ceil, ceilf or ceill. Return
7308 NULL_TREE if no simplification can be made. */
7311 fold_builtin_ceil (tree fndecl
, tree arglist
)
7315 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7318 /* Optimize ceil of constant value. */
7319 arg
= TREE_VALUE (arglist
);
7320 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7324 x
= TREE_REAL_CST (arg
);
7325 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7327 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7330 real_ceil (&r
, TYPE_MODE (type
), &x
);
7331 return build_real (type
, r
);
7335 return fold_trunc_transparent_mathfn (fndecl
, arglist
);
7338 /* Fold function call to builtin round, roundf or roundl. Return
7339 NULL_TREE if no simplification can be made. */
7342 fold_builtin_round (tree fndecl
, tree arglist
)
7346 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7349 /* Optimize round of constant value. */
7350 arg
= TREE_VALUE (arglist
);
7351 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7355 x
= TREE_REAL_CST (arg
);
7356 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7358 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7361 real_round (&r
, TYPE_MODE (type
), &x
);
7362 return build_real (type
, r
);
7366 return fold_trunc_transparent_mathfn (fndecl
, arglist
);
7369 /* Fold function call to builtin lround, lroundf or lroundl (or the
7370 corresponding long long versions) and other rounding functions.
7371 Return NULL_TREE if no simplification can be made. */
7374 fold_builtin_int_roundingfn (tree fndecl
, tree arglist
)
7378 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7381 /* Optimize lround of constant value. */
7382 arg
= TREE_VALUE (arglist
);
7383 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7385 const REAL_VALUE_TYPE x
= TREE_REAL_CST (arg
);
7387 if (! REAL_VALUE_ISNAN (x
) && ! REAL_VALUE_ISINF (x
))
7389 tree itype
= TREE_TYPE (TREE_TYPE (fndecl
));
7390 tree ftype
= TREE_TYPE (arg
), result
;
7391 HOST_WIDE_INT hi
, lo
;
7394 switch (DECL_FUNCTION_CODE (fndecl
))
7396 CASE_FLT_FN (BUILT_IN_LFLOOR
):
7397 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7398 real_floor (&r
, TYPE_MODE (ftype
), &x
);
7401 CASE_FLT_FN (BUILT_IN_LCEIL
):
7402 CASE_FLT_FN (BUILT_IN_LLCEIL
):
7403 real_ceil (&r
, TYPE_MODE (ftype
), &x
);
7406 CASE_FLT_FN (BUILT_IN_LROUND
):
7407 CASE_FLT_FN (BUILT_IN_LLROUND
):
7408 real_round (&r
, TYPE_MODE (ftype
), &x
);
7415 REAL_VALUE_TO_INT (&lo
, &hi
, r
);
7416 result
= build_int_cst_wide (NULL_TREE
, lo
, hi
);
7417 if (int_fits_type_p (result
, itype
))
7418 return fold_convert (itype
, result
);
7422 return fold_fixed_mathfn (fndecl
, arglist
);
7425 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7426 and their long and long long variants (i.e. ffsl and ffsll).
7427 Return NULL_TREE if no simplification can be made. */
7430 fold_builtin_bitop (tree fndecl
, tree arglist
)
7434 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
7437 /* Optimize for constant argument. */
7438 arg
= TREE_VALUE (arglist
);
7439 if (TREE_CODE (arg
) == INTEGER_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7441 HOST_WIDE_INT hi
, width
, result
;
7442 unsigned HOST_WIDE_INT lo
;
7445 type
= TREE_TYPE (arg
);
7446 width
= TYPE_PRECISION (type
);
7447 lo
= TREE_INT_CST_LOW (arg
);
7449 /* Clear all the bits that are beyond the type's precision. */
7450 if (width
> HOST_BITS_PER_WIDE_INT
)
7452 hi
= TREE_INT_CST_HIGH (arg
);
7453 if (width
< 2 * HOST_BITS_PER_WIDE_INT
)
7454 hi
&= ~((HOST_WIDE_INT
) (-1) >> (width
- HOST_BITS_PER_WIDE_INT
));
7459 if (width
< HOST_BITS_PER_WIDE_INT
)
7460 lo
&= ~((unsigned HOST_WIDE_INT
) (-1) << width
);
7463 switch (DECL_FUNCTION_CODE (fndecl
))
7465 CASE_INT_FN (BUILT_IN_FFS
):
7467 result
= exact_log2 (lo
& -lo
) + 1;
7469 result
= HOST_BITS_PER_WIDE_INT
+ exact_log2 (hi
& -hi
) + 1;
7474 CASE_INT_FN (BUILT_IN_CLZ
):
7476 result
= width
- floor_log2 (hi
) - 1 - HOST_BITS_PER_WIDE_INT
;
7478 result
= width
- floor_log2 (lo
) - 1;
7479 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
7483 CASE_INT_FN (BUILT_IN_CTZ
):
7485 result
= exact_log2 (lo
& -lo
);
7487 result
= HOST_BITS_PER_WIDE_INT
+ exact_log2 (hi
& -hi
);
7488 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
7492 CASE_INT_FN (BUILT_IN_POPCOUNT
):
7495 result
++, lo
&= lo
- 1;
7497 result
++, hi
&= hi
- 1;
7500 CASE_INT_FN (BUILT_IN_PARITY
):
7503 result
++, lo
&= lo
- 1;
7505 result
++, hi
&= hi
- 1;
7513 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), result
);
7519 /* Return true if EXPR is the real constant contained in VALUE. */
7522 real_dconstp (tree expr
, const REAL_VALUE_TYPE
*value
)
7526 return ((TREE_CODE (expr
) == REAL_CST
7527 && ! TREE_CONSTANT_OVERFLOW (expr
)
7528 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr
), *value
))
7529 || (TREE_CODE (expr
) == COMPLEX_CST
7530 && real_dconstp (TREE_REALPART (expr
), value
)
7531 && real_zerop (TREE_IMAGPART (expr
))));
7534 /* A subroutine of fold_builtin to fold the various logarithmic
7535 functions. EXP is the CALL_EXPR of a call to a builtin logN
7536 function. VALUE is the base of the logN function. */
7539 fold_builtin_logarithm (tree fndecl
, tree arglist
,
7540 const REAL_VALUE_TYPE
*value
)
7542 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7544 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7545 tree arg
= TREE_VALUE (arglist
);
7546 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
7548 /* Optimize logN(1.0) = 0.0. */
7549 if (real_onep (arg
))
7550 return build_real (type
, dconst0
);
7552 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
7553 exactly, then only do this if flag_unsafe_math_optimizations. */
7554 if (exact_real_truncate (TYPE_MODE (type
), value
)
7555 || flag_unsafe_math_optimizations
)
7557 const REAL_VALUE_TYPE value_truncate
=
7558 real_value_truncate (TYPE_MODE (type
), *value
);
7559 if (real_dconstp (arg
, &value_truncate
))
7560 return build_real (type
, dconst1
);
7563 /* Special case, optimize logN(expN(x)) = x. */
7564 if (flag_unsafe_math_optimizations
7565 && ((value
== &dconste
7566 && (fcode
== BUILT_IN_EXP
7567 || fcode
== BUILT_IN_EXPF
7568 || fcode
== BUILT_IN_EXPL
))
7569 || (value
== &dconst2
7570 && (fcode
== BUILT_IN_EXP2
7571 || fcode
== BUILT_IN_EXP2F
7572 || fcode
== BUILT_IN_EXP2L
))
7573 || (value
== &dconst10
&& (BUILTIN_EXP10_P (fcode
)))))
7574 return fold_convert (type
, TREE_VALUE (TREE_OPERAND (arg
, 1)));
7576 /* Optimize logN(func()) for various exponential functions. We
7577 want to determine the value "x" and the power "exponent" in
7578 order to transform logN(x**exponent) into exponent*logN(x). */
7579 if (flag_unsafe_math_optimizations
)
7581 tree exponent
= 0, x
= 0;
7585 CASE_FLT_FN (BUILT_IN_EXP
):
7586 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7587 x
= build_real (type
,
7588 real_value_truncate (TYPE_MODE (type
), dconste
));
7589 exponent
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7591 CASE_FLT_FN (BUILT_IN_EXP2
):
7592 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7593 x
= build_real (type
, dconst2
);
7594 exponent
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7596 CASE_FLT_FN (BUILT_IN_EXP10
):
7597 CASE_FLT_FN (BUILT_IN_POW10
):
7598 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7599 x
= build_real (type
, dconst10
);
7600 exponent
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7602 CASE_FLT_FN (BUILT_IN_SQRT
):
7603 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7604 x
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7605 exponent
= build_real (type
, dconsthalf
);
7607 CASE_FLT_FN (BUILT_IN_CBRT
):
7608 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7609 x
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7610 exponent
= build_real (type
, real_value_truncate (TYPE_MODE (type
),
7613 CASE_FLT_FN (BUILT_IN_POW
):
7614 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7615 x
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7616 exponent
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg
, 1)));
7622 /* Now perform the optimization. */
7626 arglist
= build_tree_list (NULL_TREE
, x
);
7627 logfn
= build_function_call_expr (fndecl
, arglist
);
7628 return fold_build2 (MULT_EXPR
, type
, exponent
, logfn
);
7636 /* Fold a builtin function call to pow, powf, or powl. Return
7637 NULL_TREE if no simplification can be made. */
7639 fold_builtin_pow (tree fndecl
, tree arglist
, tree type
)
7641 tree arg0
= TREE_VALUE (arglist
);
7642 tree arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
7644 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
7647 /* Optimize pow(1.0,y) = 1.0. */
7648 if (real_onep (arg0
))
7649 return omit_one_operand (type
, build_real (type
, dconst1
), arg1
);
7651 if (TREE_CODE (arg1
) == REAL_CST
7652 && ! TREE_CONSTANT_OVERFLOW (arg1
))
7654 REAL_VALUE_TYPE cint
;
7658 c
= TREE_REAL_CST (arg1
);
7660 /* Optimize pow(x,0.0) = 1.0. */
7661 if (REAL_VALUES_EQUAL (c
, dconst0
))
7662 return omit_one_operand (type
, build_real (type
, dconst1
),
7665 /* Optimize pow(x,1.0) = x. */
7666 if (REAL_VALUES_EQUAL (c
, dconst1
))
7669 /* Optimize pow(x,-1.0) = 1.0/x. */
7670 if (REAL_VALUES_EQUAL (c
, dconstm1
))
7671 return fold_build2 (RDIV_EXPR
, type
,
7672 build_real (type
, dconst1
), arg0
);
7674 /* Optimize pow(x,0.5) = sqrt(x). */
7675 if (flag_unsafe_math_optimizations
7676 && REAL_VALUES_EQUAL (c
, dconsthalf
))
7678 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
7680 if (sqrtfn
!= NULL_TREE
)
7682 tree arglist
= build_tree_list (NULL_TREE
, arg0
);
7683 return build_function_call_expr (sqrtfn
, arglist
);
7687 /* Check for an integer exponent. */
7688 n
= real_to_integer (&c
);
7689 real_from_integer (&cint
, VOIDmode
, n
, n
< 0 ? -1 : 0, 0);
7690 if (real_identical (&c
, &cint
))
7692 /* Attempt to evaluate pow at compile-time. */
7693 if (TREE_CODE (arg0
) == REAL_CST
7694 && ! TREE_CONSTANT_OVERFLOW (arg0
))
7699 x
= TREE_REAL_CST (arg0
);
7700 inexact
= real_powi (&x
, TYPE_MODE (type
), &x
, n
);
7701 if (flag_unsafe_math_optimizations
|| !inexact
)
7702 return build_real (type
, x
);
7705 /* Strip sign ops from even integer powers. */
7706 if ((n
& 1) == 0 && flag_unsafe_math_optimizations
)
7708 tree narg0
= fold_strip_sign_ops (arg0
);
7711 arglist
= build_tree_list (NULL_TREE
, arg1
);
7712 arglist
= tree_cons (NULL_TREE
, narg0
, arglist
);
7713 return build_function_call_expr (fndecl
, arglist
);
7719 if (flag_unsafe_math_optimizations
)
7721 const enum built_in_function fcode
= builtin_mathfn_code (arg0
);
7723 /* Optimize pow(expN(x),y) = expN(x*y). */
7724 if (BUILTIN_EXPONENT_P (fcode
))
7726 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
7727 tree arg
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
7728 arg
= fold_build2 (MULT_EXPR
, type
, arg
, arg1
);
7729 arglist
= build_tree_list (NULL_TREE
, arg
);
7730 return build_function_call_expr (expfn
, arglist
);
7733 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7734 if (BUILTIN_SQRT_P (fcode
))
7736 tree narg0
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
7737 tree narg1
= fold_build2 (MULT_EXPR
, type
, arg1
,
7738 build_real (type
, dconsthalf
));
7740 arglist
= tree_cons (NULL_TREE
, narg0
,
7741 build_tree_list (NULL_TREE
, narg1
));
7742 return build_function_call_expr (fndecl
, arglist
);
7745 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7746 if (BUILTIN_CBRT_P (fcode
))
7748 tree arg
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
7749 if (tree_expr_nonnegative_p (arg
))
7751 const REAL_VALUE_TYPE dconstroot
7752 = real_value_truncate (TYPE_MODE (type
), dconstthird
);
7753 tree narg1
= fold_build2 (MULT_EXPR
, type
, arg1
,
7754 build_real (type
, dconstroot
));
7755 arglist
= tree_cons (NULL_TREE
, arg
,
7756 build_tree_list (NULL_TREE
, narg1
));
7757 return build_function_call_expr (fndecl
, arglist
);
7761 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7762 if (fcode
== BUILT_IN_POW
|| fcode
== BUILT_IN_POWF
7763 || fcode
== BUILT_IN_POWL
)
7765 tree arg00
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
7766 tree arg01
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0
, 1)));
7767 tree narg1
= fold_build2 (MULT_EXPR
, type
, arg01
, arg1
);
7768 arglist
= tree_cons (NULL_TREE
, arg00
,
7769 build_tree_list (NULL_TREE
, narg1
));
7770 return build_function_call_expr (fndecl
, arglist
);
7777 /* Fold a builtin function call to powi, powif, or powil. Return
7778 NULL_TREE if no simplification can be made. */
7780 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED
, tree arglist
, tree type
)
7782 tree arg0
= TREE_VALUE (arglist
);
7783 tree arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
7785 if (!validate_arglist (arglist
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7788 /* Optimize pow(1.0,y) = 1.0. */
7789 if (real_onep (arg0
))
7790 return omit_one_operand (type
, build_real (type
, dconst1
), arg1
);
7792 if (host_integerp (arg1
, 0))
7794 HOST_WIDE_INT c
= TREE_INT_CST_LOW (arg1
);
7796 /* Evaluate powi at compile-time. */
7797 if (TREE_CODE (arg0
) == REAL_CST
7798 && ! TREE_CONSTANT_OVERFLOW (arg0
))
7801 x
= TREE_REAL_CST (arg0
);
7802 real_powi (&x
, TYPE_MODE (type
), &x
, c
);
7803 return build_real (type
, x
);
7806 /* Optimize pow(x,0) = 1.0. */
7808 return omit_one_operand (type
, build_real (type
, dconst1
),
7811 /* Optimize pow(x,1) = x. */
7815 /* Optimize pow(x,-1) = 1.0/x. */
7817 return fold_build2 (RDIV_EXPR
, type
,
7818 build_real (type
, dconst1
), arg0
);
7824 /* A subroutine of fold_builtin to fold the various exponent
7825 functions. EXP is the CALL_EXPR of a call to a builtin function.
7826 VALUE is the value which will be raised to a power. */
7829 fold_builtin_exponent (tree fndecl
, tree arglist
,
7830 const REAL_VALUE_TYPE
*value
)
7832 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7834 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7835 tree arg
= TREE_VALUE (arglist
);
7837 /* Optimize exp*(0.0) = 1.0. */
7838 if (real_zerop (arg
))
7839 return build_real (type
, dconst1
);
7841 /* Optimize expN(1.0) = N. */
7842 if (real_onep (arg
))
7844 REAL_VALUE_TYPE cst
;
7846 real_convert (&cst
, TYPE_MODE (type
), value
);
7847 return build_real (type
, cst
);
7850 /* Attempt to evaluate expN(integer) at compile-time. */
7851 if (flag_unsafe_math_optimizations
7852 && TREE_CODE (arg
) == REAL_CST
7853 && ! TREE_CONSTANT_OVERFLOW (arg
))
7855 REAL_VALUE_TYPE cint
;
7859 c
= TREE_REAL_CST (arg
);
7860 n
= real_to_integer (&c
);
7861 real_from_integer (&cint
, VOIDmode
, n
,
7863 if (real_identical (&c
, &cint
))
7867 real_powi (&x
, TYPE_MODE (type
), value
, n
);
7868 return build_real (type
, x
);
7872 /* Optimize expN(logN(x)) = x. */
7873 if (flag_unsafe_math_optimizations
)
7875 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
7877 if ((value
== &dconste
7878 && (fcode
== BUILT_IN_LOG
7879 || fcode
== BUILT_IN_LOGF
7880 || fcode
== BUILT_IN_LOGL
))
7881 || (value
== &dconst2
7882 && (fcode
== BUILT_IN_LOG2
7883 || fcode
== BUILT_IN_LOG2F
7884 || fcode
== BUILT_IN_LOG2L
))
7885 || (value
== &dconst10
7886 && (fcode
== BUILT_IN_LOG10
7887 || fcode
== BUILT_IN_LOG10F
7888 || fcode
== BUILT_IN_LOG10L
)))
7889 return fold_convert (type
, TREE_VALUE (TREE_OPERAND (arg
, 1)));
7896 /* Return true if VAR is a VAR_DECL or a component thereof. */
7899 var_decl_component_p (tree var
)
7902 while (handled_component_p (inner
))
7903 inner
= TREE_OPERAND (inner
, 0);
7904 return SSA_VAR_P (inner
);
7907 /* Fold function call to builtin memset. Return
7908 NULL_TREE if no simplification can be made. */
7911 fold_builtin_memset (tree arglist
, tree type
, bool ignore
)
7913 tree dest
, c
, len
, var
, ret
;
7914 unsigned HOST_WIDE_INT length
, cval
;
7916 if (!validate_arglist (arglist
,
7917 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7920 dest
= TREE_VALUE (arglist
);
7921 c
= TREE_VALUE (TREE_CHAIN (arglist
));
7922 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
7924 if (! host_integerp (len
, 1))
7927 /* If the LEN parameter is zero, return DEST. */
7928 if (integer_zerop (len
))
7929 return omit_one_operand (type
, dest
, c
);
7931 if (! host_integerp (c
, 1) || TREE_SIDE_EFFECTS (dest
))
7936 if (TREE_CODE (var
) != ADDR_EXPR
)
7939 var
= TREE_OPERAND (var
, 0);
7940 if (TREE_THIS_VOLATILE (var
))
7943 if (!INTEGRAL_TYPE_P (TREE_TYPE (var
))
7944 && !POINTER_TYPE_P (TREE_TYPE (var
)))
7947 if (! var_decl_component_p (var
))
7950 length
= tree_low_cst (len
, 1);
7951 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var
))) != length
7952 || get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
7956 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
7959 if (integer_zerop (c
))
7963 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
7966 cval
= tree_low_cst (c
, 1);
7970 cval
|= (cval
<< 31) << 1;
7973 ret
= build_int_cst_type (TREE_TYPE (var
), cval
);
7974 ret
= build2 (MODIFY_EXPR
, TREE_TYPE (var
), var
, ret
);
7978 return omit_one_operand (type
, dest
, ret
);
7981 /* Fold function call to builtin memset. Return
7982 NULL_TREE if no simplification can be made. */
7985 fold_builtin_bzero (tree arglist
, bool ignore
)
7987 tree dest
, size
, newarglist
;
7989 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7995 dest
= TREE_VALUE (arglist
);
7996 size
= TREE_VALUE (TREE_CHAIN (arglist
));
7998 /* New argument list transforming bzero(ptr x, int y) to
7999 memset(ptr x, int 0, size_t y). This is done this way
8000 so that if it isn't expanded inline, we fallback to
8001 calling bzero instead of memset. */
8003 newarglist
= build_tree_list (NULL_TREE
, fold_convert (sizetype
, size
));
8004 newarglist
= tree_cons (NULL_TREE
, integer_zero_node
, newarglist
);
8005 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
8006 return fold_builtin_memset (newarglist
, void_type_node
, ignore
);
8009 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8010 NULL_TREE if no simplification can be made.
8011 If ENDP is 0, return DEST (like memcpy).
8012 If ENDP is 1, return DEST+LEN (like mempcpy).
8013 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8014 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8018 fold_builtin_memory_op (tree arglist
, tree type
, bool ignore
, int endp
)
8020 tree dest
, src
, len
, destvar
, srcvar
, expr
;
8021 unsigned HOST_WIDE_INT length
;
8023 if (! validate_arglist (arglist
,
8024 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8027 dest
= TREE_VALUE (arglist
);
8028 src
= TREE_VALUE (TREE_CHAIN (arglist
));
8029 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8031 /* If the LEN parameter is zero, return DEST. */
8032 if (integer_zerop (len
))
8033 return omit_one_operand (type
, dest
, src
);
8035 /* If SRC and DEST are the same (and not volatile), return
8036 DEST{,+LEN,+LEN-1}. */
8037 if (operand_equal_p (src
, dest
, 0))
8041 if (! host_integerp (len
, 1))
8044 if (TREE_SIDE_EFFECTS (dest
) || TREE_SIDE_EFFECTS (src
))
8048 STRIP_NOPS (destvar
);
8049 if (TREE_CODE (destvar
) != ADDR_EXPR
)
8052 destvar
= TREE_OPERAND (destvar
, 0);
8053 if (TREE_THIS_VOLATILE (destvar
))
8056 if (!INTEGRAL_TYPE_P (TREE_TYPE (destvar
))
8057 && !POINTER_TYPE_P (TREE_TYPE (destvar
))
8058 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (destvar
)))
8061 if (! var_decl_component_p (destvar
))
8065 STRIP_NOPS (srcvar
);
8066 if (TREE_CODE (srcvar
) != ADDR_EXPR
)
8069 srcvar
= TREE_OPERAND (srcvar
, 0);
8070 if (TREE_THIS_VOLATILE (srcvar
))
8073 if (!INTEGRAL_TYPE_P (TREE_TYPE (srcvar
))
8074 && !POINTER_TYPE_P (TREE_TYPE (srcvar
))
8075 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (srcvar
)))
8078 if (! var_decl_component_p (srcvar
))
8081 length
= tree_low_cst (len
, 1);
8082 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (destvar
))) != length
8083 || get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
8085 || GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (srcvar
))) != length
8086 || get_pointer_alignment (src
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
8090 if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar
))
8091 || POINTER_TYPE_P (TREE_TYPE (srcvar
)))
8092 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar
))
8093 || POINTER_TYPE_P (TREE_TYPE (destvar
))))
8094 expr
= fold_convert (TREE_TYPE (destvar
), srcvar
);
8096 expr
= fold_build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (destvar
), srcvar
);
8097 expr
= build2 (MODIFY_EXPR
, TREE_TYPE (destvar
), destvar
, expr
);
8103 if (endp
== 0 || endp
== 3)
8104 return omit_one_operand (type
, dest
, expr
);
8110 len
= fold_build2 (MINUS_EXPR
, TREE_TYPE (len
), len
,
8113 len
= fold_convert (TREE_TYPE (dest
), len
);
8114 dest
= fold_build2 (PLUS_EXPR
, TREE_TYPE (dest
), dest
, len
);
8115 dest
= fold_convert (type
, dest
);
8117 dest
= omit_one_operand (type
, dest
, expr
);
8121 /* Fold function call to builtin bcopy. Return NULL_TREE if no
8122 simplification can be made. */
8125 fold_builtin_bcopy (tree arglist
, bool ignore
)
8127 tree src
, dest
, size
, newarglist
;
8129 if (!validate_arglist (arglist
,
8130 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8136 src
= TREE_VALUE (arglist
);
8137 dest
= TREE_VALUE (TREE_CHAIN (arglist
));
8138 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8140 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8141 memmove(ptr y, ptr x, size_t z). This is done this way
8142 so that if it isn't expanded inline, we fallback to
8143 calling bcopy instead of memmove. */
8145 newarglist
= build_tree_list (NULL_TREE
, fold_convert (sizetype
, size
));
8146 newarglist
= tree_cons (NULL_TREE
, src
, newarglist
);
8147 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
8149 return fold_builtin_memory_op (newarglist
, void_type_node
, true, /*endp=*/3);
8152 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
8153 the length of the string to be copied. Return NULL_TREE if no
8154 simplification can be made. */
8157 fold_builtin_strcpy (tree fndecl
, tree arglist
, tree len
)
8161 if (!validate_arglist (arglist
,
8162 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8165 dest
= TREE_VALUE (arglist
);
8166 src
= TREE_VALUE (TREE_CHAIN (arglist
));
8168 /* If SRC and DEST are the same (and not volatile), return DEST. */
8169 if (operand_equal_p (src
, dest
, 0))
8170 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), dest
);
8175 fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
8181 len
= c_strlen (src
, 1);
8182 if (! len
|| TREE_SIDE_EFFECTS (len
))
8186 len
= size_binop (PLUS_EXPR
, len
, ssize_int (1));
8187 arglist
= build_tree_list (NULL_TREE
, len
);
8188 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
8189 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
8190 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)),
8191 build_function_call_expr (fn
, arglist
));
8194 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
8195 the length of the source string. Return NULL_TREE if no simplification
8199 fold_builtin_strncpy (tree fndecl
, tree arglist
, tree slen
)
8201 tree dest
, src
, len
, fn
;
8203 if (!validate_arglist (arglist
,
8204 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8207 dest
= TREE_VALUE (arglist
);
8208 src
= TREE_VALUE (TREE_CHAIN (arglist
));
8209 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8211 /* If the LEN parameter is zero, return DEST. */
8212 if (integer_zerop (len
))
8213 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
8215 /* We can't compare slen with len as constants below if len is not a
8217 if (len
== 0 || TREE_CODE (len
) != INTEGER_CST
)
8221 slen
= c_strlen (src
, 1);
8223 /* Now, we must be passed a constant src ptr parameter. */
8224 if (slen
== 0 || TREE_CODE (slen
) != INTEGER_CST
)
8227 slen
= size_binop (PLUS_EXPR
, slen
, ssize_int (1));
8229 /* We do not support simplification of this case, though we do
8230 support it when expanding trees into RTL. */
8231 /* FIXME: generate a call to __builtin_memset. */
8232 if (tree_int_cst_lt (slen
, len
))
8235 /* OK transform into builtin memcpy. */
8236 fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
8239 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)),
8240 build_function_call_expr (fn
, arglist
));
8243 /* Fold function call to builtin memcmp. Return
8244 NULL_TREE if no simplification can be made. */
8247 fold_builtin_memcmp (tree arglist
)
8249 tree arg1
, arg2
, len
;
8250 const char *p1
, *p2
;
8252 if (!validate_arglist (arglist
,
8253 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8256 arg1
= TREE_VALUE (arglist
);
8257 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
8258 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8260 /* If the LEN parameter is zero, return zero. */
8261 if (integer_zerop (len
))
8262 return omit_two_operands (integer_type_node
, integer_zero_node
,
8265 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8266 if (operand_equal_p (arg1
, arg2
, 0))
8267 return omit_one_operand (integer_type_node
, integer_zero_node
, len
);
8269 p1
= c_getstr (arg1
);
8270 p2
= c_getstr (arg2
);
8272 /* If all arguments are constant, and the value of len is not greater
8273 than the lengths of arg1 and arg2, evaluate at compile-time. */
8274 if (host_integerp (len
, 1) && p1
&& p2
8275 && compare_tree_int (len
, strlen (p1
) + 1) <= 0
8276 && compare_tree_int (len
, strlen (p2
) + 1) <= 0)
8278 const int r
= memcmp (p1
, p2
, tree_low_cst (len
, 1));
8281 return integer_one_node
;
8283 return integer_minus_one_node
;
8285 return integer_zero_node
;
8288 /* If len parameter is one, return an expression corresponding to
8289 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8290 if (host_integerp (len
, 1) && tree_low_cst (len
, 1) == 1)
8292 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8293 tree cst_uchar_ptr_node
8294 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8296 tree ind1
= fold_convert (integer_type_node
,
8297 build1 (INDIRECT_REF
, cst_uchar_node
,
8298 fold_convert (cst_uchar_ptr_node
,
8300 tree ind2
= fold_convert (integer_type_node
,
8301 build1 (INDIRECT_REF
, cst_uchar_node
,
8302 fold_convert (cst_uchar_ptr_node
,
8304 return fold_build2 (MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8310 /* Fold function call to builtin strcmp. Return
8311 NULL_TREE if no simplification can be made. */
8314 fold_builtin_strcmp (tree arglist
)
8317 const char *p1
, *p2
;
8319 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8322 arg1
= TREE_VALUE (arglist
);
8323 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
8325 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8326 if (operand_equal_p (arg1
, arg2
, 0))
8327 return integer_zero_node
;
8329 p1
= c_getstr (arg1
);
8330 p2
= c_getstr (arg2
);
8334 const int i
= strcmp (p1
, p2
);
8336 return integer_minus_one_node
;
8338 return integer_one_node
;
8340 return integer_zero_node
;
8343 /* If the second arg is "", return *(const unsigned char*)arg1. */
8344 if (p2
&& *p2
== '\0')
8346 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8347 tree cst_uchar_ptr_node
8348 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8350 return fold_convert (integer_type_node
,
8351 build1 (INDIRECT_REF
, cst_uchar_node
,
8352 fold_convert (cst_uchar_ptr_node
,
8356 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8357 if (p1
&& *p1
== '\0')
8359 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8360 tree cst_uchar_ptr_node
8361 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8363 tree temp
= fold_convert (integer_type_node
,
8364 build1 (INDIRECT_REF
, cst_uchar_node
,
8365 fold_convert (cst_uchar_ptr_node
,
8367 return fold_build1 (NEGATE_EXPR
, integer_type_node
, temp
);
8373 /* Fold function call to builtin strncmp. Return
8374 NULL_TREE if no simplification can be made. */
8377 fold_builtin_strncmp (tree arglist
)
8379 tree arg1
, arg2
, len
;
8380 const char *p1
, *p2
;
8382 if (!validate_arglist (arglist
,
8383 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8386 arg1
= TREE_VALUE (arglist
);
8387 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
8388 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8390 /* If the LEN parameter is zero, return zero. */
8391 if (integer_zerop (len
))
8392 return omit_two_operands (integer_type_node
, integer_zero_node
,
8395 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8396 if (operand_equal_p (arg1
, arg2
, 0))
8397 return omit_one_operand (integer_type_node
, integer_zero_node
, len
);
8399 p1
= c_getstr (arg1
);
8400 p2
= c_getstr (arg2
);
8402 if (host_integerp (len
, 1) && p1
&& p2
)
8404 const int i
= strncmp (p1
, p2
, tree_low_cst (len
, 1));
8406 return integer_one_node
;
8408 return integer_minus_one_node
;
8410 return integer_zero_node
;
8413 /* If the second arg is "", and the length is greater than zero,
8414 return *(const unsigned char*)arg1. */
8415 if (p2
&& *p2
== '\0'
8416 && TREE_CODE (len
) == INTEGER_CST
8417 && tree_int_cst_sgn (len
) == 1)
8419 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8420 tree cst_uchar_ptr_node
8421 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8423 return fold_convert (integer_type_node
,
8424 build1 (INDIRECT_REF
, cst_uchar_node
,
8425 fold_convert (cst_uchar_ptr_node
,
8429 /* If the first arg is "", and the length is greater than zero,
8430 return -*(const unsigned char*)arg2. */
8431 if (p1
&& *p1
== '\0'
8432 && TREE_CODE (len
) == INTEGER_CST
8433 && tree_int_cst_sgn (len
) == 1)
8435 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8436 tree cst_uchar_ptr_node
8437 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8439 tree temp
= fold_convert (integer_type_node
,
8440 build1 (INDIRECT_REF
, cst_uchar_node
,
8441 fold_convert (cst_uchar_ptr_node
,
8443 return fold_build1 (NEGATE_EXPR
, integer_type_node
, temp
);
8446 /* If len parameter is one, return an expression corresponding to
8447 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8448 if (host_integerp (len
, 1) && tree_low_cst (len
, 1) == 1)
8450 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8451 tree cst_uchar_ptr_node
8452 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8454 tree ind1
= fold_convert (integer_type_node
,
8455 build1 (INDIRECT_REF
, cst_uchar_node
,
8456 fold_convert (cst_uchar_ptr_node
,
8458 tree ind2
= fold_convert (integer_type_node
,
8459 build1 (INDIRECT_REF
, cst_uchar_node
,
8460 fold_convert (cst_uchar_ptr_node
,
8462 return fold_build2 (MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8468 /* Fold function call to builtin signbit, signbitf or signbitl. Return
8469 NULL_TREE if no simplification can be made. */
8472 fold_builtin_signbit (tree fndecl
, tree arglist
)
8474 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8477 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
8480 arg
= TREE_VALUE (arglist
);
8482 /* If ARG is a compile-time constant, determine the result. */
8483 if (TREE_CODE (arg
) == REAL_CST
8484 && !TREE_CONSTANT_OVERFLOW (arg
))
8488 c
= TREE_REAL_CST (arg
);
8489 temp
= REAL_VALUE_NEGATIVE (c
) ? integer_one_node
: integer_zero_node
;
8490 return fold_convert (type
, temp
);
8493 /* If ARG is non-negative, the result is always zero. */
8494 if (tree_expr_nonnegative_p (arg
))
8495 return omit_one_operand (type
, integer_zero_node
, arg
);
8497 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8498 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg
))))
8499 return fold_build2 (LT_EXPR
, type
, arg
,
8500 build_real (TREE_TYPE (arg
), dconst0
));
8505 /* Fold function call to builtin copysign, copysignf or copysignl.
8506 Return NULL_TREE if no simplification can be made. */
8509 fold_builtin_copysign (tree fndecl
, tree arglist
, tree type
)
8511 tree arg1
, arg2
, tem
;
8513 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
8516 arg1
= TREE_VALUE (arglist
);
8517 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
8519 /* copysign(X,X) is X. */
8520 if (operand_equal_p (arg1
, arg2
, 0))
8521 return fold_convert (type
, arg1
);
8523 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8524 if (TREE_CODE (arg1
) == REAL_CST
8525 && TREE_CODE (arg2
) == REAL_CST
8526 && !TREE_CONSTANT_OVERFLOW (arg1
)
8527 && !TREE_CONSTANT_OVERFLOW (arg2
))
8529 REAL_VALUE_TYPE c1
, c2
;
8531 c1
= TREE_REAL_CST (arg1
);
8532 c2
= TREE_REAL_CST (arg2
);
8533 /* c1.sign := c2.sign. */
8534 real_copysign (&c1
, &c2
);
8535 return build_real (type
, c1
);
8538 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8539 Remember to evaluate Y for side-effects. */
8540 if (tree_expr_nonnegative_p (arg2
))
8541 return omit_one_operand (type
,
8542 fold_build1 (ABS_EXPR
, type
, arg1
),
8545 /* Strip sign changing operations for the first argument. */
8546 tem
= fold_strip_sign_ops (arg1
);
8549 arglist
= tree_cons (NULL_TREE
, tem
, TREE_CHAIN (arglist
));
8550 return build_function_call_expr (fndecl
, arglist
);
8556 /* Fold a call to builtin isascii. */
8559 fold_builtin_isascii (tree arglist
)
8561 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
8565 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8566 tree arg
= TREE_VALUE (arglist
);
8568 arg
= build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
8569 build_int_cst (NULL_TREE
,
8570 ~ (unsigned HOST_WIDE_INT
) 0x7f));
8571 arg
= fold_build2 (EQ_EXPR
, integer_type_node
,
8572 arg
, integer_zero_node
);
8574 if (in_gimple_form
&& !TREE_CONSTANT (arg
))
8581 /* Fold a call to builtin toascii. */
8584 fold_builtin_toascii (tree arglist
)
8586 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
8590 /* Transform toascii(c) -> (c & 0x7f). */
8591 tree arg
= TREE_VALUE (arglist
);
8593 return fold_build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
8594 build_int_cst (NULL_TREE
, 0x7f));
8598 /* Fold a call to builtin isdigit. */
8601 fold_builtin_isdigit (tree arglist
)
8603 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
8607 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8608 /* According to the C standard, isdigit is unaffected by locale.
8609 However, it definitely is affected by the target character set. */
8611 unsigned HOST_WIDE_INT target_digit0
8612 = lang_hooks
.to_target_charset ('0');
8614 if (target_digit0
== 0)
8617 arg
= fold_convert (unsigned_type_node
, TREE_VALUE (arglist
));
8618 arg
= build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
8619 build_int_cst (unsigned_type_node
, target_digit0
));
8620 arg
= fold_build2 (LE_EXPR
, integer_type_node
, arg
,
8621 build_int_cst (unsigned_type_node
, 9));
8622 if (in_gimple_form
&& !TREE_CONSTANT (arg
))
8629 /* Fold a call to fabs, fabsf or fabsl. */
8632 fold_builtin_fabs (tree arglist
, tree type
)
8636 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
8639 arg
= TREE_VALUE (arglist
);
8640 arg
= fold_convert (type
, arg
);
8641 if (TREE_CODE (arg
) == REAL_CST
)
8642 return fold_abs_const (arg
, type
);
8643 return fold_build1 (ABS_EXPR
, type
, arg
);
8646 /* Fold a call to abs, labs, llabs or imaxabs. */
8649 fold_builtin_abs (tree arglist
, tree type
)
8653 if (!validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
8656 arg
= TREE_VALUE (arglist
);
8657 arg
= fold_convert (type
, arg
);
8658 if (TREE_CODE (arg
) == INTEGER_CST
)
8659 return fold_abs_const (arg
, type
);
8660 return fold_build1 (ABS_EXPR
, type
, arg
);
8663 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8664 EXP is the CALL_EXPR for the call. */
8667 fold_builtin_classify (tree fndecl
, tree arglist
, int builtin_index
)
8669 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8673 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
8675 /* Check that we have exactly one argument. */
8678 error ("too few arguments to function %qs",
8679 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8680 return error_mark_node
;
8682 else if (TREE_CHAIN (arglist
) != 0)
8684 error ("too many arguments to function %qs",
8685 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8686 return error_mark_node
;
8690 error ("non-floating-point argument to function %qs",
8691 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8692 return error_mark_node
;
8696 arg
= TREE_VALUE (arglist
);
8697 switch (builtin_index
)
8699 case BUILT_IN_ISINF
:
8700 if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
8701 return omit_one_operand (type
, integer_zero_node
, arg
);
8703 if (TREE_CODE (arg
) == REAL_CST
)
8705 r
= TREE_REAL_CST (arg
);
8706 if (real_isinf (&r
))
8707 return real_compare (GT_EXPR
, &r
, &dconst0
)
8708 ? integer_one_node
: integer_minus_one_node
;
8710 return integer_zero_node
;
8715 case BUILT_IN_FINITE
:
8716 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg
)))
8717 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
8718 return omit_one_operand (type
, integer_zero_node
, arg
);
8720 if (TREE_CODE (arg
) == REAL_CST
)
8722 r
= TREE_REAL_CST (arg
);
8723 return real_isinf (&r
) || real_isnan (&r
)
8724 ? integer_zero_node
: integer_one_node
;
8729 case BUILT_IN_ISNAN
:
8730 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg
))))
8731 return omit_one_operand (type
, integer_zero_node
, arg
);
8733 if (TREE_CODE (arg
) == REAL_CST
)
8735 r
= TREE_REAL_CST (arg
);
8736 return real_isnan (&r
) ? integer_one_node
: integer_zero_node
;
8739 arg
= builtin_save_expr (arg
);
8740 return fold_build2 (UNORDERED_EXPR
, type
, arg
, arg
);
8747 /* Fold a call to an unordered comparison function such as
8748 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8749 being called and ARGLIST is the argument list for the call.
8750 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8751 the opposite of the desired result. UNORDERED_CODE is used
8752 for modes that can hold NaNs and ORDERED_CODE is used for
8756 fold_builtin_unordered_cmp (tree fndecl
, tree arglist
,
8757 enum tree_code unordered_code
,
8758 enum tree_code ordered_code
)
8760 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8761 enum tree_code code
;
8764 enum tree_code code0
, code1
;
8765 tree cmp_type
= NULL_TREE
;
8767 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
8769 /* Check that we have exactly two arguments. */
8770 if (arglist
== 0 || TREE_CHAIN (arglist
) == 0)
8772 error ("too few arguments to function %qs",
8773 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8774 return error_mark_node
;
8776 else if (TREE_CHAIN (TREE_CHAIN (arglist
)) != 0)
8778 error ("too many arguments to function %qs",
8779 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8780 return error_mark_node
;
8784 arg0
= TREE_VALUE (arglist
);
8785 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
8787 type0
= TREE_TYPE (arg0
);
8788 type1
= TREE_TYPE (arg1
);
8790 code0
= TREE_CODE (type0
);
8791 code1
= TREE_CODE (type1
);
8793 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
8794 /* Choose the wider of two real types. */
8795 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
8797 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
8799 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
8803 error ("non-floating-point argument to function %qs",
8804 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8805 return error_mark_node
;
8808 arg0
= fold_convert (cmp_type
, arg0
);
8809 arg1
= fold_convert (cmp_type
, arg1
);
8811 if (unordered_code
== UNORDERED_EXPR
)
8813 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
8814 return omit_two_operands (type
, integer_zero_node
, arg0
, arg1
);
8815 return fold_build2 (UNORDERED_EXPR
, type
, arg0
, arg1
);
8818 code
= MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0
))) ? unordered_code
8820 return fold_build1 (TRUTH_NOT_EXPR
, type
,
8821 fold_build2 (code
, type
, arg0
, arg1
));
8824 /* Used by constant folding to simplify calls to builtin functions. EXP is
8825 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
8826 result of the function call is ignored. This function returns NULL_TREE
8827 if no simplification was possible. */
8830 fold_builtin_1 (tree fndecl
, tree arglist
, bool ignore
)
8832 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8833 enum built_in_function fcode
;
8835 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
8836 return targetm
.fold_builtin (fndecl
, arglist
, ignore
);
8838 fcode
= DECL_FUNCTION_CODE (fndecl
);
8841 case BUILT_IN_FPUTS
:
8842 return fold_builtin_fputs (arglist
, ignore
, false, NULL_TREE
);
8844 case BUILT_IN_FPUTS_UNLOCKED
:
8845 return fold_builtin_fputs (arglist
, ignore
, true, NULL_TREE
);
8847 case BUILT_IN_STRSTR
:
8848 return fold_builtin_strstr (arglist
, type
);
8850 case BUILT_IN_STRCAT
:
8851 return fold_builtin_strcat (arglist
);
8853 case BUILT_IN_STRNCAT
:
8854 return fold_builtin_strncat (arglist
);
8856 case BUILT_IN_STRSPN
:
8857 return fold_builtin_strspn (arglist
);
8859 case BUILT_IN_STRCSPN
:
8860 return fold_builtin_strcspn (arglist
);
8862 case BUILT_IN_STRCHR
:
8863 case BUILT_IN_INDEX
:
8864 return fold_builtin_strchr (arglist
, type
);
8866 case BUILT_IN_STRRCHR
:
8867 case BUILT_IN_RINDEX
:
8868 return fold_builtin_strrchr (arglist
, type
);
8870 case BUILT_IN_STRCPY
:
8871 return fold_builtin_strcpy (fndecl
, arglist
, NULL_TREE
);
8873 case BUILT_IN_STRNCPY
:
8874 return fold_builtin_strncpy (fndecl
, arglist
, NULL_TREE
);
8876 case BUILT_IN_STRCMP
:
8877 return fold_builtin_strcmp (arglist
);
8879 case BUILT_IN_STRNCMP
:
8880 return fold_builtin_strncmp (arglist
);
8882 case BUILT_IN_STRPBRK
:
8883 return fold_builtin_strpbrk (arglist
, type
);
8886 case BUILT_IN_MEMCMP
:
8887 return fold_builtin_memcmp (arglist
);
8889 case BUILT_IN_SPRINTF
:
8890 return fold_builtin_sprintf (arglist
, ignore
);
8892 case BUILT_IN_CONSTANT_P
:
8896 val
= fold_builtin_constant_p (arglist
);
8897 /* Gimplification will pull the CALL_EXPR for the builtin out of
8898 an if condition. When not optimizing, we'll not CSE it back.
8899 To avoid link error types of regressions, return false now. */
8900 if (!val
&& !optimize
)
8901 val
= integer_zero_node
;
8906 case BUILT_IN_EXPECT
:
8907 return fold_builtin_expect (arglist
);
8909 case BUILT_IN_CLASSIFY_TYPE
:
8910 return fold_builtin_classify_type (arglist
);
8912 case BUILT_IN_STRLEN
:
8913 return fold_builtin_strlen (arglist
);
8915 CASE_FLT_FN (BUILT_IN_FABS
):
8916 return fold_builtin_fabs (arglist
, type
);
8920 case BUILT_IN_LLABS
:
8921 case BUILT_IN_IMAXABS
:
8922 return fold_builtin_abs (arglist
, type
);
8924 CASE_FLT_FN (BUILT_IN_CONJ
):
8925 if (validate_arglist (arglist
, COMPLEX_TYPE
, VOID_TYPE
))
8926 return fold_build1 (CONJ_EXPR
, type
, TREE_VALUE (arglist
));
8929 CASE_FLT_FN (BUILT_IN_CREAL
):
8930 if (validate_arglist (arglist
, COMPLEX_TYPE
, VOID_TYPE
))
8931 return non_lvalue (fold_build1 (REALPART_EXPR
, type
,
8932 TREE_VALUE (arglist
)));
8935 CASE_FLT_FN (BUILT_IN_CIMAG
):
8936 if (validate_arglist (arglist
, COMPLEX_TYPE
, VOID_TYPE
))
8937 return non_lvalue (fold_build1 (IMAGPART_EXPR
, type
,
8938 TREE_VALUE (arglist
)));
8941 CASE_FLT_FN (BUILT_IN_CABS
):
8942 return fold_builtin_cabs (arglist
, type
, fndecl
);
8944 CASE_FLT_FN (BUILT_IN_SQRT
):
8945 return fold_builtin_sqrt (arglist
, type
);
8947 CASE_FLT_FN (BUILT_IN_CBRT
):
8948 return fold_builtin_cbrt (arglist
, type
);
8950 CASE_FLT_FN (BUILT_IN_SIN
):
8951 return fold_builtin_sin (arglist
);
8953 CASE_FLT_FN (BUILT_IN_COS
):
8954 return fold_builtin_cos (arglist
, type
, fndecl
);
8956 CASE_FLT_FN (BUILT_IN_EXP
):
8957 return fold_builtin_exponent (fndecl
, arglist
, &dconste
);
8959 CASE_FLT_FN (BUILT_IN_EXP2
):
8960 return fold_builtin_exponent (fndecl
, arglist
, &dconst2
);
8962 CASE_FLT_FN (BUILT_IN_EXP10
):
8963 CASE_FLT_FN (BUILT_IN_POW10
):
8964 return fold_builtin_exponent (fndecl
, arglist
, &dconst10
);
8966 CASE_FLT_FN (BUILT_IN_LOG
):
8967 return fold_builtin_logarithm (fndecl
, arglist
, &dconste
);
8969 CASE_FLT_FN (BUILT_IN_LOG2
):
8970 return fold_builtin_logarithm (fndecl
, arglist
, &dconst2
);
8972 CASE_FLT_FN (BUILT_IN_LOG10
):
8973 return fold_builtin_logarithm (fndecl
, arglist
, &dconst10
);
8975 CASE_FLT_FN (BUILT_IN_TAN
):
8976 return fold_builtin_tan (arglist
);
8978 CASE_FLT_FN (BUILT_IN_ATAN
):
8979 return fold_builtin_atan (arglist
, type
);
8981 CASE_FLT_FN (BUILT_IN_POW
):
8982 return fold_builtin_pow (fndecl
, arglist
, type
);
8984 CASE_FLT_FN (BUILT_IN_POWI
):
8985 return fold_builtin_powi (fndecl
, arglist
, type
);
8987 CASE_FLT_FN (BUILT_IN_INF
):
8988 case BUILT_IN_INFD32
:
8989 case BUILT_IN_INFD64
:
8990 case BUILT_IN_INFD128
:
8991 return fold_builtin_inf (type
, true);
8993 CASE_FLT_FN (BUILT_IN_HUGE_VAL
):
8994 return fold_builtin_inf (type
, false);
8996 CASE_FLT_FN (BUILT_IN_NAN
):
8997 case BUILT_IN_NAND32
:
8998 case BUILT_IN_NAND64
:
8999 case BUILT_IN_NAND128
:
9000 return fold_builtin_nan (arglist
, type
, true);
9002 CASE_FLT_FN (BUILT_IN_NANS
):
9003 return fold_builtin_nan (arglist
, type
, false);
9005 CASE_FLT_FN (BUILT_IN_FLOOR
):
9006 return fold_builtin_floor (fndecl
, arglist
);
9008 CASE_FLT_FN (BUILT_IN_CEIL
):
9009 return fold_builtin_ceil (fndecl
, arglist
);
9011 CASE_FLT_FN (BUILT_IN_TRUNC
):
9012 return fold_builtin_trunc (fndecl
, arglist
);
9014 CASE_FLT_FN (BUILT_IN_ROUND
):
9015 return fold_builtin_round (fndecl
, arglist
);
9017 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
9018 CASE_FLT_FN (BUILT_IN_RINT
):
9019 return fold_trunc_transparent_mathfn (fndecl
, arglist
);
9021 CASE_FLT_FN (BUILT_IN_LCEIL
):
9022 CASE_FLT_FN (BUILT_IN_LLCEIL
):
9023 CASE_FLT_FN (BUILT_IN_LFLOOR
):
9024 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
9025 CASE_FLT_FN (BUILT_IN_LROUND
):
9026 CASE_FLT_FN (BUILT_IN_LLROUND
):
9027 return fold_builtin_int_roundingfn (fndecl
, arglist
);
9029 CASE_FLT_FN (BUILT_IN_LRINT
):
9030 CASE_FLT_FN (BUILT_IN_LLRINT
):
9031 return fold_fixed_mathfn (fndecl
, arglist
);
9033 CASE_INT_FN (BUILT_IN_FFS
):
9034 CASE_INT_FN (BUILT_IN_CLZ
):
9035 CASE_INT_FN (BUILT_IN_CTZ
):
9036 CASE_INT_FN (BUILT_IN_POPCOUNT
):
9037 CASE_INT_FN (BUILT_IN_PARITY
):
9038 return fold_builtin_bitop (fndecl
, arglist
);
9040 case BUILT_IN_MEMSET
:
9041 return fold_builtin_memset (arglist
, type
, ignore
);
9043 case BUILT_IN_MEMCPY
:
9044 return fold_builtin_memory_op (arglist
, type
, ignore
, /*endp=*/0);
9046 case BUILT_IN_MEMPCPY
:
9047 return fold_builtin_memory_op (arglist
, type
, ignore
, /*endp=*/1);
9049 case BUILT_IN_MEMMOVE
:
9050 return fold_builtin_memory_op (arglist
, type
, ignore
, /*endp=*/3);
9052 case BUILT_IN_BZERO
:
9053 return fold_builtin_bzero (arglist
, ignore
);
9055 case BUILT_IN_BCOPY
:
9056 return fold_builtin_bcopy (arglist
, ignore
);
9058 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
9059 return fold_builtin_signbit (fndecl
, arglist
);
9061 case BUILT_IN_ISASCII
:
9062 return fold_builtin_isascii (arglist
);
9064 case BUILT_IN_TOASCII
:
9065 return fold_builtin_toascii (arglist
);
9067 case BUILT_IN_ISDIGIT
:
9068 return fold_builtin_isdigit (arglist
);
9070 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
9071 return fold_builtin_copysign (fndecl
, arglist
, type
);
9073 CASE_FLT_FN (BUILT_IN_FINITE
):
9074 case BUILT_IN_FINITED32
:
9075 case BUILT_IN_FINITED64
:
9076 case BUILT_IN_FINITED128
:
9077 return fold_builtin_classify (fndecl
, arglist
, BUILT_IN_FINITE
);
9079 CASE_FLT_FN (BUILT_IN_ISINF
):
9080 case BUILT_IN_ISINFD32
:
9081 case BUILT_IN_ISINFD64
:
9082 case BUILT_IN_ISINFD128
:
9083 return fold_builtin_classify (fndecl
, arglist
, BUILT_IN_ISINF
);
9085 CASE_FLT_FN (BUILT_IN_ISNAN
):
9086 case BUILT_IN_ISNAND32
:
9087 case BUILT_IN_ISNAND64
:
9088 case BUILT_IN_ISNAND128
:
9089 return fold_builtin_classify (fndecl
, arglist
, BUILT_IN_ISNAN
);
9091 case BUILT_IN_ISGREATER
:
9092 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNLE_EXPR
, LE_EXPR
);
9093 case BUILT_IN_ISGREATEREQUAL
:
9094 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNLT_EXPR
, LT_EXPR
);
9095 case BUILT_IN_ISLESS
:
9096 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNGE_EXPR
, GE_EXPR
);
9097 case BUILT_IN_ISLESSEQUAL
:
9098 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNGT_EXPR
, GT_EXPR
);
9099 case BUILT_IN_ISLESSGREATER
:
9100 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNEQ_EXPR
, EQ_EXPR
);
9101 case BUILT_IN_ISUNORDERED
:
9102 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNORDERED_EXPR
,
9105 /* We do the folding for va_start in the expander. */
9106 case BUILT_IN_VA_START
:
9109 case BUILT_IN_OBJECT_SIZE
:
9110 return fold_builtin_object_size (arglist
);
9111 case BUILT_IN_MEMCPY_CHK
:
9112 case BUILT_IN_MEMPCPY_CHK
:
9113 case BUILT_IN_MEMMOVE_CHK
:
9114 case BUILT_IN_MEMSET_CHK
:
9115 return fold_builtin_memory_chk (fndecl
, arglist
, NULL_TREE
, ignore
,
9116 DECL_FUNCTION_CODE (fndecl
));
9117 case BUILT_IN_STRCPY_CHK
:
9118 case BUILT_IN_STPCPY_CHK
:
9119 return fold_builtin_stxcpy_chk (fndecl
, arglist
, NULL_TREE
, ignore
,
9120 DECL_FUNCTION_CODE (fndecl
));
9121 case BUILT_IN_STRNCPY_CHK
:
9122 return fold_builtin_strncpy_chk (arglist
, NULL_TREE
);
9123 case BUILT_IN_STRCAT_CHK
:
9124 return fold_builtin_strcat_chk (fndecl
, arglist
);
9125 case BUILT_IN_STRNCAT_CHK
:
9126 return fold_builtin_strncat_chk (fndecl
, arglist
);
9127 case BUILT_IN_SPRINTF_CHK
:
9128 case BUILT_IN_VSPRINTF_CHK
:
9129 return fold_builtin_sprintf_chk (arglist
, DECL_FUNCTION_CODE (fndecl
));
9130 case BUILT_IN_SNPRINTF_CHK
:
9131 case BUILT_IN_VSNPRINTF_CHK
:
9132 return fold_builtin_snprintf_chk (arglist
, NULL_TREE
,
9133 DECL_FUNCTION_CODE (fndecl
));
9135 case BUILT_IN_PRINTF
:
9136 case BUILT_IN_PRINTF_UNLOCKED
:
9137 case BUILT_IN_VPRINTF
:
9138 case BUILT_IN_PRINTF_CHK
:
9139 case BUILT_IN_VPRINTF_CHK
:
9140 return fold_builtin_printf (fndecl
, arglist
, ignore
,
9141 DECL_FUNCTION_CODE (fndecl
));
9143 case BUILT_IN_FPRINTF
:
9144 case BUILT_IN_FPRINTF_UNLOCKED
:
9145 case BUILT_IN_VFPRINTF
:
9146 case BUILT_IN_FPRINTF_CHK
:
9147 case BUILT_IN_VFPRINTF_CHK
:
9148 return fold_builtin_fprintf (fndecl
, arglist
, ignore
,
9149 DECL_FUNCTION_CODE (fndecl
));
9158 /* A wrapper function for builtin folding that prevents warnings for
9159 "statement without effect" and the like, caused by removing the
9160 call node earlier than the warning is generated. */
9163 fold_builtin (tree fndecl
, tree arglist
, bool ignore
)
9165 tree exp
= fold_builtin_1 (fndecl
, arglist
, ignore
);
9168 exp
= build1 (NOP_EXPR
, TREE_TYPE (exp
), exp
);
9169 TREE_NO_WARNING (exp
) = 1;
9175 /* Conveniently construct a function call expression. */
9178 build_function_call_expr (tree fn
, tree arglist
)
9182 call_expr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
9183 return fold_build3 (CALL_EXPR
, TREE_TYPE (TREE_TYPE (fn
)),
9184 call_expr
, arglist
, NULL_TREE
);
9187 /* This function validates the types of a function call argument list
9188 represented as a tree chain of parameters against a specified list
9189 of tree_codes. If the last specifier is a 0, that represents an
9190 ellipses, otherwise the last specifier must be a VOID_TYPE. */
9193 validate_arglist (tree arglist
, ...)
9195 enum tree_code code
;
9199 va_start (ap
, arglist
);
9203 code
= va_arg (ap
, enum tree_code
);
9207 /* This signifies an ellipses, any further arguments are all ok. */
9211 /* This signifies an endlink, if no arguments remain, return
9212 true, otherwise return false. */
9216 /* If no parameters remain or the parameter's code does not
9217 match the specified code, return false. Otherwise continue
9218 checking any remaining arguments. */
9221 if (code
== POINTER_TYPE
)
9223 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
))))
9226 else if (code
!= TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))))
9230 arglist
= TREE_CHAIN (arglist
);
9234 /* We need gotos here since we can only have one VA_CLOSE in a
9242 /* Default target-specific builtin expander that does nothing. */
9245 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
9246 rtx target ATTRIBUTE_UNUSED
,
9247 rtx subtarget ATTRIBUTE_UNUSED
,
9248 enum machine_mode mode ATTRIBUTE_UNUSED
,
9249 int ignore ATTRIBUTE_UNUSED
)
9254 /* Returns true is EXP represents data that would potentially reside
9255 in a readonly section. */
9258 readonly_data_expr (tree exp
)
9262 if (TREE_CODE (exp
) != ADDR_EXPR
)
9265 exp
= get_base_address (TREE_OPERAND (exp
, 0));
9269 /* Make sure we call decl_readonly_section only for trees it
9270 can handle (since it returns true for everything it doesn't
9272 if (TREE_CODE (exp
) == STRING_CST
9273 || TREE_CODE (exp
) == CONSTRUCTOR
9274 || (TREE_CODE (exp
) == VAR_DECL
&& TREE_STATIC (exp
)))
9275 return decl_readonly_section (exp
, 0);
9280 /* Simplify a call to the strstr builtin.
9282 Return 0 if no simplification was possible, otherwise return the
9283 simplified form of the call as a tree.
9285 The simplified form may be a constant or other expression which
9286 computes the same value, but in a more efficient manner (including
9287 calls to other builtin functions).
9289 The call may contain arguments which need to be evaluated, but
9290 which are not useful to determine the result of the call. In
9291 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9292 COMPOUND_EXPR will be an argument which must be evaluated.
9293 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9294 COMPOUND_EXPR in the chain will contain the tree for the simplified
9295 form of the builtin function call. */
9298 fold_builtin_strstr (tree arglist
, tree type
)
9300 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9304 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9306 const char *p1
, *p2
;
9315 const char *r
= strstr (p1
, p2
);
9319 return build_int_cst (TREE_TYPE (s1
), 0);
9321 /* Return an offset into the constant string argument. */
9322 tem
= fold_build2 (PLUS_EXPR
, TREE_TYPE (s1
),
9323 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
));
9324 return fold_convert (type
, tem
);
9327 /* The argument is const char *, and the result is char *, so we need
9328 a type conversion here to avoid a warning. */
9330 return fold_convert (type
, s1
);
9335 fn
= implicit_built_in_decls
[BUILT_IN_STRCHR
];
9339 /* New argument list transforming strstr(s1, s2) to
9340 strchr(s1, s2[0]). */
9341 arglist
= build_tree_list (NULL_TREE
,
9342 build_int_cst (NULL_TREE
, p2
[0]));
9343 arglist
= tree_cons (NULL_TREE
, s1
, arglist
);
9344 return build_function_call_expr (fn
, arglist
);
9348 /* Simplify a call to the strchr builtin.
9350 Return 0 if no simplification was possible, otherwise return the
9351 simplified form of the call as a tree.
9353 The simplified form may be a constant or other expression which
9354 computes the same value, but in a more efficient manner (including
9355 calls to other builtin functions).
9357 The call may contain arguments which need to be evaluated, but
9358 which are not useful to determine the result of the call. In
9359 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9360 COMPOUND_EXPR will be an argument which must be evaluated.
9361 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9362 COMPOUND_EXPR in the chain will contain the tree for the simplified
9363 form of the builtin function call. */
9366 fold_builtin_strchr (tree arglist
, tree type
)
9368 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
9372 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9375 if (TREE_CODE (s2
) != INTEGER_CST
)
9385 if (target_char_cast (s2
, &c
))
9391 return build_int_cst (TREE_TYPE (s1
), 0);
9393 /* Return an offset into the constant string argument. */
9394 tem
= fold_build2 (PLUS_EXPR
, TREE_TYPE (s1
),
9395 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
));
9396 return fold_convert (type
, tem
);
9402 /* Simplify a call to the strrchr builtin.
9404 Return 0 if no simplification was possible, otherwise return the
9405 simplified form of the call as a tree.
9407 The simplified form may be a constant or other expression which
9408 computes the same value, but in a more efficient manner (including
9409 calls to other builtin functions).
9411 The call may contain arguments which need to be evaluated, but
9412 which are not useful to determine the result of the call. In
9413 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9414 COMPOUND_EXPR will be an argument which must be evaluated.
9415 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9416 COMPOUND_EXPR in the chain will contain the tree for the simplified
9417 form of the builtin function call. */
9420 fold_builtin_strrchr (tree arglist
, tree type
)
9422 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
9426 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9430 if (TREE_CODE (s2
) != INTEGER_CST
)
9440 if (target_char_cast (s2
, &c
))
9443 r
= strrchr (p1
, c
);
9446 return build_int_cst (TREE_TYPE (s1
), 0);
9448 /* Return an offset into the constant string argument. */
9449 tem
= fold_build2 (PLUS_EXPR
, TREE_TYPE (s1
),
9450 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
));
9451 return fold_convert (type
, tem
);
9454 if (! integer_zerop (s2
))
9457 fn
= implicit_built_in_decls
[BUILT_IN_STRCHR
];
9461 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9462 return build_function_call_expr (fn
, arglist
);
9466 /* Simplify a call to the strpbrk builtin.
9468 Return 0 if no simplification was possible, otherwise return the
9469 simplified form of the call as a tree.
9471 The simplified form may be a constant or other expression which
9472 computes the same value, but in a more efficient manner (including
9473 calls to other builtin functions).
9475 The call may contain arguments which need to be evaluated, but
9476 which are not useful to determine the result of the call. In
9477 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9478 COMPOUND_EXPR will be an argument which must be evaluated.
9479 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9480 COMPOUND_EXPR in the chain will contain the tree for the simplified
9481 form of the builtin function call. */
9484 fold_builtin_strpbrk (tree arglist
, tree type
)
9486 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9490 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9492 const char *p1
, *p2
;
9501 const char *r
= strpbrk (p1
, p2
);
9505 return build_int_cst (TREE_TYPE (s1
), 0);
9507 /* Return an offset into the constant string argument. */
9508 tem
= fold_build2 (PLUS_EXPR
, TREE_TYPE (s1
),
9509 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
));
9510 return fold_convert (type
, tem
);
9514 /* strpbrk(x, "") == NULL.
9515 Evaluate and ignore s1 in case it had side-effects. */
9516 return omit_one_operand (TREE_TYPE (s1
), integer_zero_node
, s1
);
9519 return 0; /* Really call strpbrk. */
9521 fn
= implicit_built_in_decls
[BUILT_IN_STRCHR
];
9525 /* New argument list transforming strpbrk(s1, s2) to
9526 strchr(s1, s2[0]). */
9527 arglist
= build_tree_list (NULL_TREE
,
9528 build_int_cst (NULL_TREE
, p2
[0]));
9529 arglist
= tree_cons (NULL_TREE
, s1
, arglist
);
9530 return build_function_call_expr (fn
, arglist
);
9534 /* Simplify a call to the strcat builtin.
9536 Return 0 if no simplification was possible, otherwise return the
9537 simplified form of the call as a tree.
9539 The simplified form may be a constant or other expression which
9540 computes the same value, but in a more efficient manner (including
9541 calls to other builtin functions).
9543 The call may contain arguments which need to be evaluated, but
9544 which are not useful to determine the result of the call. In
9545 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9546 COMPOUND_EXPR will be an argument which must be evaluated.
9547 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9548 COMPOUND_EXPR in the chain will contain the tree for the simplified
9549 form of the builtin function call. */
9552 fold_builtin_strcat (tree arglist
)
9554 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9558 tree dst
= TREE_VALUE (arglist
),
9559 src
= TREE_VALUE (TREE_CHAIN (arglist
));
9560 const char *p
= c_getstr (src
);
9562 /* If the string length is zero, return the dst parameter. */
9563 if (p
&& *p
== '\0')
9570 /* Simplify a call to the strncat builtin.
9572 Return 0 if no simplification was possible, otherwise return the
9573 simplified form of the call as a tree.
9575 The simplified form may be a constant or other expression which
9576 computes the same value, but in a more efficient manner (including
9577 calls to other builtin functions).
9579 The call may contain arguments which need to be evaluated, but
9580 which are not useful to determine the result of the call. In
9581 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9582 COMPOUND_EXPR will be an argument which must be evaluated.
9583 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9584 COMPOUND_EXPR in the chain will contain the tree for the simplified
9585 form of the builtin function call. */
9588 fold_builtin_strncat (tree arglist
)
9590 if (!validate_arglist (arglist
,
9591 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
9595 tree dst
= TREE_VALUE (arglist
);
9596 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
9597 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
9598 const char *p
= c_getstr (src
);
9600 /* If the requested length is zero, or the src parameter string
9601 length is zero, return the dst parameter. */
9602 if (integer_zerop (len
) || (p
&& *p
== '\0'))
9603 return omit_two_operands (TREE_TYPE (dst
), dst
, src
, len
);
9605 /* If the requested len is greater than or equal to the string
9606 length, call strcat. */
9607 if (TREE_CODE (len
) == INTEGER_CST
&& p
9608 && compare_tree_int (len
, strlen (p
)) >= 0)
9611 = tree_cons (NULL_TREE
, dst
, build_tree_list (NULL_TREE
, src
));
9612 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCAT
];
9614 /* If the replacement _DECL isn't initialized, don't do the
9619 return build_function_call_expr (fn
, newarglist
);
9625 /* Simplify a call to the strspn builtin.
9627 Return 0 if no simplification was possible, otherwise return the
9628 simplified form of the call as a tree.
9630 The simplified form may be a constant or other expression which
9631 computes the same value, but in a more efficient manner (including
9632 calls to other builtin functions).
9634 The call may contain arguments which need to be evaluated, but
9635 which are not useful to determine the result of the call. In
9636 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9637 COMPOUND_EXPR will be an argument which must be evaluated.
9638 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9639 COMPOUND_EXPR in the chain will contain the tree for the simplified
9640 form of the builtin function call. */
9643 fold_builtin_strspn (tree arglist
)
9645 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9649 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9650 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
9652 /* If both arguments are constants, evaluate at compile-time. */
9655 const size_t r
= strspn (p1
, p2
);
9656 return size_int (r
);
9659 /* If either argument is "", return 0. */
9660 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
9661 /* Evaluate and ignore both arguments in case either one has
9663 return omit_two_operands (integer_type_node
, integer_zero_node
,
9669 /* Simplify a call to the strcspn builtin.
9671 Return 0 if no simplification was possible, otherwise return the
9672 simplified form of the call as a tree.
9674 The simplified form may be a constant or other expression which
9675 computes the same value, but in a more efficient manner (including
9676 calls to other builtin functions).
9678 The call may contain arguments which need to be evaluated, but
9679 which are not useful to determine the result of the call. In
9680 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9681 COMPOUND_EXPR will be an argument which must be evaluated.
9682 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9683 COMPOUND_EXPR in the chain will contain the tree for the simplified
9684 form of the builtin function call. */
9687 fold_builtin_strcspn (tree arglist
)
9689 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9693 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9694 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
9696 /* If both arguments are constants, evaluate at compile-time. */
9699 const size_t r
= strcspn (p1
, p2
);
9700 return size_int (r
);
9703 /* If the first argument is "", return 0. */
9704 if (p1
&& *p1
== '\0')
9706 /* Evaluate and ignore argument s2 in case it has
9708 return omit_one_operand (integer_type_node
,
9709 integer_zero_node
, s2
);
9712 /* If the second argument is "", return __builtin_strlen(s1). */
9713 if (p2
&& *p2
== '\0')
9715 tree newarglist
= build_tree_list (NULL_TREE
, s1
),
9716 fn
= implicit_built_in_decls
[BUILT_IN_STRLEN
];
9718 /* If the replacement _DECL isn't initialized, don't do the
9723 return build_function_call_expr (fn
, newarglist
);
9729 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
9730 by the builtin will be ignored. UNLOCKED is true is true if this
9731 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
9732 the known length of the string. Return NULL_TREE if no simplification
9736 fold_builtin_fputs (tree arglist
, bool ignore
, bool unlocked
, tree len
)
9739 /* If we're using an unlocked function, assume the other unlocked
9740 functions exist explicitly. */
9741 tree
const fn_fputc
= unlocked
? built_in_decls
[BUILT_IN_FPUTC_UNLOCKED
]
9742 : implicit_built_in_decls
[BUILT_IN_FPUTC
];
9743 tree
const fn_fwrite
= unlocked
? built_in_decls
[BUILT_IN_FWRITE_UNLOCKED
]
9744 : implicit_built_in_decls
[BUILT_IN_FWRITE
];
9746 /* If the return value is used, don't do the transformation. */
9750 /* Verify the arguments in the original call. */
9751 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9755 len
= c_strlen (TREE_VALUE (arglist
), 0);
9757 /* Get the length of the string passed to fputs. If the length
9758 can't be determined, punt. */
9760 || TREE_CODE (len
) != INTEGER_CST
)
9763 switch (compare_tree_int (len
, 1))
9765 case -1: /* length is 0, delete the call entirely . */
9766 return omit_one_operand (integer_type_node
, integer_zero_node
,
9767 TREE_VALUE (TREE_CHAIN (arglist
)));
9769 case 0: /* length is 1, call fputc. */
9771 const char *p
= c_getstr (TREE_VALUE (arglist
));
9775 /* New argument list transforming fputs(string, stream) to
9776 fputc(string[0], stream). */
9777 arglist
= build_tree_list (NULL_TREE
,
9778 TREE_VALUE (TREE_CHAIN (arglist
)));
9779 arglist
= tree_cons (NULL_TREE
,
9780 build_int_cst (NULL_TREE
, p
[0]),
9787 case 1: /* length is greater than 1, call fwrite. */
9791 /* If optimizing for size keep fputs. */
9794 string_arg
= TREE_VALUE (arglist
);
9795 /* New argument list transforming fputs(string, stream) to
9796 fwrite(string, 1, len, stream). */
9797 arglist
= build_tree_list (NULL_TREE
,
9798 TREE_VALUE (TREE_CHAIN (arglist
)));
9799 arglist
= tree_cons (NULL_TREE
, len
, arglist
);
9800 arglist
= tree_cons (NULL_TREE
, size_one_node
, arglist
);
9801 arglist
= tree_cons (NULL_TREE
, string_arg
, arglist
);
9809 /* If the replacement _DECL isn't initialized, don't do the
9814 /* These optimizations are only performed when the result is ignored,
9815 hence there's no need to cast the result to integer_type_node. */
9816 return build_function_call_expr (fn
, arglist
);
9819 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
9820 produced. False otherwise. This is done so that we don't output the error
9821 or warning twice or three times. */
9823 fold_builtin_next_arg (tree arglist
)
9825 tree fntype
= TREE_TYPE (current_function_decl
);
9827 if (TYPE_ARG_TYPES (fntype
) == 0
9828 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype
)))
9831 error ("%<va_start%> used in function with fixed args");
9836 /* Evidently an out of date version of <stdarg.h>; can't validate
9837 va_start's second argument, but can still work as intended. */
9838 warning (0, "%<__builtin_next_arg%> called without an argument");
9841 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9842 when we checked the arguments and if needed issued a warning. */
9843 else if (!TREE_CHAIN (arglist
)
9844 || !integer_zerop (TREE_VALUE (arglist
))
9845 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist
)))
9846 || TREE_CHAIN (TREE_CHAIN (arglist
)))
9848 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
9849 tree arg
= TREE_VALUE (arglist
);
9851 if (TREE_CHAIN (arglist
))
9853 error ("%<va_start%> used with too many arguments");
9857 /* Strip off all nops for the sake of the comparison. This
9858 is not quite the same as STRIP_NOPS. It does more.
9859 We must also strip off INDIRECT_EXPR for C++ reference
9861 while (TREE_CODE (arg
) == NOP_EXPR
9862 || TREE_CODE (arg
) == CONVERT_EXPR
9863 || TREE_CODE (arg
) == NON_LVALUE_EXPR
9864 || TREE_CODE (arg
) == INDIRECT_REF
)
9865 arg
= TREE_OPERAND (arg
, 0);
9866 if (arg
!= last_parm
)
9868 /* FIXME: Sometimes with the tree optimizers we can get the
9869 not the last argument even though the user used the last
9870 argument. We just warn and set the arg to be the last
9871 argument so that we will get wrong-code because of
9873 warning (0, "second parameter of %<va_start%> not last named argument");
9875 /* We want to verify the second parameter just once before the tree
9876 optimizers are run and then avoid keeping it in the tree,
9877 as otherwise we could warn even for correct code like:
9878 void foo (int i, ...)
9879 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9880 TREE_VALUE (arglist
) = integer_zero_node
;
9881 TREE_CHAIN (arglist
) = build_tree_list (NULL
, integer_zero_node
);
9887 /* Simplify a call to the sprintf builtin.
9889 Return 0 if no simplification was possible, otherwise return the
9890 simplified form of the call as a tree. If IGNORED is true, it means that
9891 the caller does not use the returned value of the function. */
9894 fold_builtin_sprintf (tree arglist
, int ignored
)
9896 tree call
, retval
, dest
, fmt
;
9897 const char *fmt_str
= NULL
;
9899 /* Verify the required arguments in the original call. We deal with two
9900 types of sprintf() calls: 'sprintf (str, fmt)' and
9901 'sprintf (dest, "%s", orig)'. */
9902 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
)
9903 && !validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, POINTER_TYPE
,
9907 /* Get the destination string and the format specifier. */
9908 dest
= TREE_VALUE (arglist
);
9909 fmt
= TREE_VALUE (TREE_CHAIN (arglist
));
9911 /* Check whether the format is a literal string constant. */
9912 fmt_str
= c_getstr (fmt
);
9913 if (fmt_str
== NULL
)
9919 if (!init_target_chars())
9922 /* If the format doesn't contain % args or %%, use strcpy. */
9923 if (strchr (fmt_str
, target_percent
) == NULL
)
9925 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
9930 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
9931 'format' is known to contain no % formats. */
9932 arglist
= build_tree_list (NULL_TREE
, fmt
);
9933 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
9934 call
= build_function_call_expr (fn
, arglist
);
9936 retval
= build_int_cst (NULL_TREE
, strlen (fmt_str
));
9939 /* If the format is "%s", use strcpy if the result isn't used. */
9940 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
9943 fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
9948 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
9949 orig
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
9950 arglist
= build_tree_list (NULL_TREE
, orig
);
9951 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
9954 retval
= c_strlen (orig
, 1);
9955 if (!retval
|| TREE_CODE (retval
) != INTEGER_CST
)
9958 call
= build_function_call_expr (fn
, arglist
);
9963 retval
= fold_convert
9964 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls
[BUILT_IN_SPRINTF
])),
9966 return build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), call
, retval
);
9972 /* Expand a call to __builtin_object_size. */
9975 expand_builtin_object_size (tree exp
)
9978 int object_size_type
;
9979 tree fndecl
= get_callee_fndecl (exp
);
9980 tree arglist
= TREE_OPERAND (exp
, 1);
9981 location_t locus
= EXPR_LOCATION (exp
);
9983 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
9985 error ("%Hfirst argument of %D must be a pointer, second integer constant",
9987 expand_builtin_trap ();
9991 ost
= TREE_VALUE (TREE_CHAIN (arglist
));
9994 if (TREE_CODE (ost
) != INTEGER_CST
9995 || tree_int_cst_sgn (ost
) < 0
9996 || compare_tree_int (ost
, 3) > 0)
9998 error ("%Hlast argument of %D is not integer constant between 0 and 3",
10000 expand_builtin_trap ();
10004 object_size_type
= tree_low_cst (ost
, 0);
10006 return object_size_type
< 2 ? constm1_rtx
: const0_rtx
;
10009 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10010 FCODE is the BUILT_IN_* to use.
10011 Return 0 if we failed; the caller should emit a normal call,
10012 otherwise try to get the result in TARGET, if convenient (and in
10013 mode MODE if that's convenient). */
10016 expand_builtin_memory_chk (tree exp
, rtx target
, enum machine_mode mode
,
10017 enum built_in_function fcode
)
10019 tree arglist
= TREE_OPERAND (exp
, 1);
10020 tree dest
, src
, len
, size
;
10022 if (!validate_arglist (arglist
,
10024 fcode
== BUILT_IN_MEMSET_CHK
10025 ? INTEGER_TYPE
: POINTER_TYPE
,
10026 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10029 dest
= TREE_VALUE (arglist
);
10030 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10031 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10032 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist
))));
10034 if (! host_integerp (size
, 1))
10037 if (host_integerp (len
, 1) || integer_all_onesp (size
))
10041 if (! integer_all_onesp (size
) && tree_int_cst_lt (size
, len
))
10043 location_t locus
= EXPR_LOCATION (exp
);
10044 warning (0, "%Hcall to %D will always overflow destination buffer",
10045 &locus
, get_callee_fndecl (exp
));
10049 arglist
= build_tree_list (NULL_TREE
, len
);
10050 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10051 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10054 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10055 mem{cpy,pcpy,move,set} is available. */
10058 case BUILT_IN_MEMCPY_CHK
:
10059 fn
= built_in_decls
[BUILT_IN_MEMCPY
];
10061 case BUILT_IN_MEMPCPY_CHK
:
10062 fn
= built_in_decls
[BUILT_IN_MEMPCPY
];
10064 case BUILT_IN_MEMMOVE_CHK
:
10065 fn
= built_in_decls
[BUILT_IN_MEMMOVE
];
10067 case BUILT_IN_MEMSET_CHK
:
10068 fn
= built_in_decls
[BUILT_IN_MEMSET
];
10077 fn
= build_function_call_expr (fn
, arglist
);
10078 if (TREE_CODE (fn
) == CALL_EXPR
)
10079 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
10080 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
10082 else if (fcode
== BUILT_IN_MEMSET_CHK
)
10086 unsigned int dest_align
10087 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
10089 /* If DEST is not a pointer type, call the normal function. */
10090 if (dest_align
== 0)
10093 /* If SRC and DEST are the same (and not volatile), do nothing. */
10094 if (operand_equal_p (src
, dest
, 0))
10098 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
10100 /* Evaluate and ignore LEN in case it has side-effects. */
10101 expand_expr (len
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
10102 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
10105 len
= fold_convert (TREE_TYPE (dest
), len
);
10106 expr
= fold_build2 (PLUS_EXPR
, TREE_TYPE (dest
), dest
, len
);
10107 return expand_expr (expr
, target
, mode
, EXPAND_NORMAL
);
10110 /* __memmove_chk special case. */
10111 if (fcode
== BUILT_IN_MEMMOVE_CHK
)
10113 unsigned int src_align
10114 = get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
10116 if (src_align
== 0)
10119 /* If src is categorized for a readonly section we can use
10120 normal __memcpy_chk. */
10121 if (readonly_data_expr (src
))
10123 tree fn
= built_in_decls
[BUILT_IN_MEMCPY_CHK
];
10126 fn
= build_function_call_expr (fn
, arglist
);
10127 if (TREE_CODE (fn
) == CALL_EXPR
)
10128 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
10129 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
10136 /* Emit warning if a buffer overflow is detected at compile time. */
10139 maybe_emit_chk_warning (tree exp
, enum built_in_function fcode
)
10141 int arg_mask
, is_strlen
= 0;
10142 tree arglist
= TREE_OPERAND (exp
, 1), a
;
10148 case BUILT_IN_STRCPY_CHK
:
10149 case BUILT_IN_STPCPY_CHK
:
10150 /* For __strcat_chk the warning will be emitted only if overflowing
10151 by at least strlen (dest) + 1 bytes. */
10152 case BUILT_IN_STRCAT_CHK
:
10156 case BUILT_IN_STRNCPY_CHK
:
10159 case BUILT_IN_SNPRINTF_CHK
:
10160 case BUILT_IN_VSNPRINTF_CHK
:
10164 gcc_unreachable ();
10169 for (a
= arglist
; a
&& arg_mask
; a
= TREE_CHAIN (a
), arg_mask
>>= 1)
10181 len
= TREE_VALUE (len
);
10182 size
= TREE_VALUE (size
);
10184 if (! host_integerp (size
, 1) || integer_all_onesp (size
))
10189 len
= c_strlen (len
, 1);
10190 if (! len
|| ! host_integerp (len
, 1) || tree_int_cst_lt (len
, size
))
10193 else if (! host_integerp (len
, 1) || ! tree_int_cst_lt (size
, len
))
10196 locus
= EXPR_LOCATION (exp
);
10197 warning (0, "%Hcall to %D will always overflow destination buffer",
10198 &locus
, get_callee_fndecl (exp
));
10201 /* Emit warning if a buffer overflow is detected at compile time
10202 in __sprintf_chk/__vsprintf_chk calls. */
10205 maybe_emit_sprintf_chk_warning (tree exp
, enum built_in_function fcode
)
10207 tree arglist
= TREE_OPERAND (exp
, 1);
10208 tree dest
, size
, len
, fmt
, flag
;
10209 const char *fmt_str
;
10211 /* Verify the required arguments in the original call. */
10214 dest
= TREE_VALUE (arglist
);
10215 arglist
= TREE_CHAIN (arglist
);
10218 flag
= TREE_VALUE (arglist
);
10219 arglist
= TREE_CHAIN (arglist
);
10222 size
= TREE_VALUE (arglist
);
10223 arglist
= TREE_CHAIN (arglist
);
10226 fmt
= TREE_VALUE (arglist
);
10227 arglist
= TREE_CHAIN (arglist
);
10229 if (! host_integerp (size
, 1) || integer_all_onesp (size
))
10232 /* Check whether the format is a literal string constant. */
10233 fmt_str
= c_getstr (fmt
);
10234 if (fmt_str
== NULL
)
10237 if (!init_target_chars())
10240 /* If the format doesn't contain % args or %%, we know its size. */
10241 if (strchr (fmt_str
, target_percent
) == 0)
10242 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
10243 /* If the format is "%s" and first ... argument is a string literal,
10245 else if (fcode
== BUILT_IN_SPRINTF_CHK
&& strcmp (fmt_str
, target_percent_s
) == 0)
10251 arg
= TREE_VALUE (arglist
);
10252 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
10255 len
= c_strlen (arg
, 1);
10256 if (!len
|| ! host_integerp (len
, 1))
10262 if (! tree_int_cst_lt (len
, size
))
10264 location_t locus
= EXPR_LOCATION (exp
);
10265 warning (0, "%Hcall to %D will always overflow destination buffer",
10266 &locus
, get_callee_fndecl (exp
));
10270 /* Fold a call to __builtin_object_size, if possible. */
10273 fold_builtin_object_size (tree arglist
)
10275 tree ptr
, ost
, ret
= 0;
10276 int object_size_type
;
10278 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10281 ptr
= TREE_VALUE (arglist
);
10282 ost
= TREE_VALUE (TREE_CHAIN (arglist
));
10285 if (TREE_CODE (ost
) != INTEGER_CST
10286 || tree_int_cst_sgn (ost
) < 0
10287 || compare_tree_int (ost
, 3) > 0)
10290 object_size_type
= tree_low_cst (ost
, 0);
10292 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10293 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10294 and (size_t) 0 for types 2 and 3. */
10295 if (TREE_SIDE_EFFECTS (ptr
))
10296 return fold_convert (size_type_node
,
10297 object_size_type
< 2
10298 ? integer_minus_one_node
: integer_zero_node
);
10300 if (TREE_CODE (ptr
) == ADDR_EXPR
)
10301 ret
= build_int_cstu (size_type_node
,
10302 compute_builtin_object_size (ptr
, object_size_type
));
10304 else if (TREE_CODE (ptr
) == SSA_NAME
)
10306 unsigned HOST_WIDE_INT bytes
;
10308 /* If object size is not known yet, delay folding until
10309 later. Maybe subsequent passes will help determining
10311 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
10312 if (bytes
!= (unsigned HOST_WIDE_INT
) (object_size_type
< 2
10314 ret
= build_int_cstu (size_type_node
, bytes
);
10319 ret
= force_fit_type (ret
, -1, false, false);
10320 if (TREE_CONSTANT_OVERFLOW (ret
))
10327 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10328 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10329 code of the builtin. If MAXLEN is not NULL, it is maximum length
10330 passed as third argument. */
10333 fold_builtin_memory_chk (tree fndecl
, tree arglist
, tree maxlen
, bool ignore
,
10334 enum built_in_function fcode
)
10336 tree dest
, src
, len
, size
, fn
;
10338 if (!validate_arglist (arglist
,
10340 fcode
== BUILT_IN_MEMSET_CHK
10341 ? INTEGER_TYPE
: POINTER_TYPE
,
10342 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10345 dest
= TREE_VALUE (arglist
);
10346 /* Actually val for __memset_chk, but it doesn't matter. */
10347 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10348 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10349 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist
))));
10351 /* If SRC and DEST are the same (and not volatile), return DEST
10352 (resp. DEST+LEN for __mempcpy_chk). */
10353 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
10355 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
10356 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl
)), dest
, len
);
10359 tree temp
= fold_convert (TREE_TYPE (dest
), len
);
10360 temp
= fold_build2 (PLUS_EXPR
, TREE_TYPE (dest
), dest
, temp
);
10361 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), temp
);
10365 if (! host_integerp (size
, 1))
10368 if (! integer_all_onesp (size
))
10370 if (! host_integerp (len
, 1))
10372 /* If LEN is not constant, try MAXLEN too.
10373 For MAXLEN only allow optimizing into non-_ocs function
10374 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10375 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
10377 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
10379 /* (void) __mempcpy_chk () can be optimized into
10380 (void) __memcpy_chk (). */
10381 fn
= built_in_decls
[BUILT_IN_MEMCPY_CHK
];
10385 return build_function_call_expr (fn
, arglist
);
10393 if (tree_int_cst_lt (size
, maxlen
))
10397 arglist
= build_tree_list (NULL_TREE
, len
);
10398 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10399 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10402 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10403 mem{cpy,pcpy,move,set} is available. */
10406 case BUILT_IN_MEMCPY_CHK
:
10407 fn
= built_in_decls
[BUILT_IN_MEMCPY
];
10409 case BUILT_IN_MEMPCPY_CHK
:
10410 fn
= built_in_decls
[BUILT_IN_MEMPCPY
];
10412 case BUILT_IN_MEMMOVE_CHK
:
10413 fn
= built_in_decls
[BUILT_IN_MEMMOVE
];
10415 case BUILT_IN_MEMSET_CHK
:
10416 fn
= built_in_decls
[BUILT_IN_MEMSET
];
10425 return build_function_call_expr (fn
, arglist
);
10428 /* Fold a call to the __st[rp]cpy_chk builtin.
10429 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10430 code of the builtin. If MAXLEN is not NULL, it is maximum length of
10431 strings passed as second argument. */
10434 fold_builtin_stxcpy_chk (tree fndecl
, tree arglist
, tree maxlen
, bool ignore
,
10435 enum built_in_function fcode
)
10437 tree dest
, src
, size
, len
, fn
;
10439 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
,
10443 dest
= TREE_VALUE (arglist
);
10444 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10445 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10447 /* If SRC and DEST are the same (and not volatile), return DEST. */
10448 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
10449 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), dest
);
10451 if (! host_integerp (size
, 1))
10454 if (! integer_all_onesp (size
))
10456 len
= c_strlen (src
, 1);
10457 if (! len
|| ! host_integerp (len
, 1))
10459 /* If LEN is not constant, try MAXLEN too.
10460 For MAXLEN only allow optimizing into non-_ocs function
10461 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10462 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
10464 if (fcode
== BUILT_IN_STPCPY_CHK
)
10469 /* If return value of __stpcpy_chk is ignored,
10470 optimize into __strcpy_chk. */
10471 fn
= built_in_decls
[BUILT_IN_STRCPY_CHK
];
10475 return build_function_call_expr (fn
, arglist
);
10478 if (! len
|| TREE_SIDE_EFFECTS (len
))
10481 /* If c_strlen returned something, but not a constant,
10482 transform __strcpy_chk into __memcpy_chk. */
10483 fn
= built_in_decls
[BUILT_IN_MEMCPY_CHK
];
10487 len
= size_binop (PLUS_EXPR
, len
, ssize_int (1));
10488 arglist
= build_tree_list (NULL_TREE
, size
);
10489 arglist
= tree_cons (NULL_TREE
, len
, arglist
);
10490 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10491 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10492 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)),
10493 build_function_call_expr (fn
, arglist
));
10499 if (! tree_int_cst_lt (maxlen
, size
))
10503 arglist
= build_tree_list (NULL_TREE
, src
);
10504 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10506 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
10507 fn
= built_in_decls
[fcode
== BUILT_IN_STPCPY_CHK
10508 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
];
10512 return build_function_call_expr (fn
, arglist
);
10515 /* Fold a call to the __strncpy_chk builtin.
10516 If MAXLEN is not NULL, it is maximum length passed as third argument. */
10519 fold_builtin_strncpy_chk (tree arglist
, tree maxlen
)
10521 tree dest
, src
, size
, len
, fn
;
10523 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
,
10524 INTEGER_TYPE
, VOID_TYPE
))
10527 dest
= TREE_VALUE (arglist
);
10528 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10529 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10530 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist
))));
10532 if (! host_integerp (size
, 1))
10535 if (! integer_all_onesp (size
))
10537 if (! host_integerp (len
, 1))
10539 /* If LEN is not constant, try MAXLEN too.
10540 For MAXLEN only allow optimizing into non-_ocs function
10541 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10542 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
10548 if (tree_int_cst_lt (size
, maxlen
))
10552 arglist
= build_tree_list (NULL_TREE
, len
);
10553 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10554 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10556 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
10557 fn
= built_in_decls
[BUILT_IN_STRNCPY
];
10561 return build_function_call_expr (fn
, arglist
);
10564 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */
10567 fold_builtin_strcat_chk (tree fndecl
, tree arglist
)
10569 tree dest
, src
, size
, fn
;
10572 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
,
10576 dest
= TREE_VALUE (arglist
);
10577 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10578 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10580 p
= c_getstr (src
);
10581 /* If the SRC parameter is "", return DEST. */
10582 if (p
&& *p
== '\0')
10583 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
10585 if (! host_integerp (size
, 1) || ! integer_all_onesp (size
))
10588 arglist
= build_tree_list (NULL_TREE
, src
);
10589 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10591 /* If __builtin_strcat_chk is used, assume strcat is available. */
10592 fn
= built_in_decls
[BUILT_IN_STRCAT
];
10596 return build_function_call_expr (fn
, arglist
);
10599 /* Fold a call to the __strncat_chk builtin EXP. */
10602 fold_builtin_strncat_chk (tree fndecl
, tree arglist
)
10604 tree dest
, src
, size
, len
, fn
;
10607 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
,
10608 INTEGER_TYPE
, VOID_TYPE
))
10611 dest
= TREE_VALUE (arglist
);
10612 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10613 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10614 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist
))));
10616 p
= c_getstr (src
);
10617 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
10618 if (p
&& *p
== '\0')
10619 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl
)), dest
, len
);
10620 else if (integer_zerop (len
))
10621 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
10623 if (! host_integerp (size
, 1))
10626 if (! integer_all_onesp (size
))
10628 tree src_len
= c_strlen (src
, 1);
10630 && host_integerp (src_len
, 1)
10631 && host_integerp (len
, 1)
10632 && ! tree_int_cst_lt (len
, src_len
))
10634 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
10635 fn
= built_in_decls
[BUILT_IN_STRCAT_CHK
];
10639 arglist
= build_tree_list (NULL_TREE
, size
);
10640 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10641 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10642 return build_function_call_expr (fn
, arglist
);
10647 arglist
= build_tree_list (NULL_TREE
, len
);
10648 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10649 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10651 /* If __builtin_strncat_chk is used, assume strncat is available. */
10652 fn
= built_in_decls
[BUILT_IN_STRNCAT
];
10656 return build_function_call_expr (fn
, arglist
);
10659 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if
10660 a normal call should be emitted rather than expanding the function
10661 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
10664 fold_builtin_sprintf_chk (tree arglist
, enum built_in_function fcode
)
10666 tree dest
, size
, len
, fn
, fmt
, flag
;
10667 const char *fmt_str
;
10669 /* Verify the required arguments in the original call. */
10672 dest
= TREE_VALUE (arglist
);
10673 if (! POINTER_TYPE_P (TREE_TYPE (dest
)))
10675 arglist
= TREE_CHAIN (arglist
);
10678 flag
= TREE_VALUE (arglist
);
10679 if (TREE_CODE (TREE_TYPE (flag
)) != INTEGER_TYPE
)
10681 arglist
= TREE_CHAIN (arglist
);
10684 size
= TREE_VALUE (arglist
);
10685 if (TREE_CODE (TREE_TYPE (size
)) != INTEGER_TYPE
)
10687 arglist
= TREE_CHAIN (arglist
);
10690 fmt
= TREE_VALUE (arglist
);
10691 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
10693 arglist
= TREE_CHAIN (arglist
);
10695 if (! host_integerp (size
, 1))
10700 if (!init_target_chars())
10703 /* Check whether the format is a literal string constant. */
10704 fmt_str
= c_getstr (fmt
);
10705 if (fmt_str
!= NULL
)
10707 /* If the format doesn't contain % args or %%, we know the size. */
10708 if (strchr (fmt_str
, target_percent
) == 0)
10710 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| arglist
== NULL_TREE
)
10711 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
10713 /* If the format is "%s" and first ... argument is a string literal,
10714 we know the size too. */
10715 else if (fcode
== BUILT_IN_SPRINTF_CHK
&& strcmp (fmt_str
, target_percent_s
) == 0)
10719 if (arglist
&& !TREE_CHAIN (arglist
))
10721 arg
= TREE_VALUE (arglist
);
10722 if (POINTER_TYPE_P (TREE_TYPE (arg
)))
10724 len
= c_strlen (arg
, 1);
10725 if (! len
|| ! host_integerp (len
, 1))
10732 if (! integer_all_onesp (size
))
10734 if (! len
|| ! tree_int_cst_lt (len
, size
))
10738 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
10739 or if format doesn't contain % chars or is "%s". */
10740 if (! integer_zerop (flag
))
10742 if (fmt_str
== NULL
)
10744 if (strchr (fmt_str
, target_percent
) != NULL
&& strcmp (fmt_str
, target_percent_s
))
10748 arglist
= tree_cons (NULL_TREE
, fmt
, arglist
);
10749 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10751 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
10752 fn
= built_in_decls
[fcode
== BUILT_IN_VSPRINTF_CHK
10753 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
];
10757 return build_function_call_expr (fn
, arglist
);
10760 /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if
10761 a normal call should be emitted rather than expanding the function
10762 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
10763 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
10764 passed as second argument. */
10767 fold_builtin_snprintf_chk (tree arglist
, tree maxlen
,
10768 enum built_in_function fcode
)
10770 tree dest
, size
, len
, fn
, fmt
, flag
;
10771 const char *fmt_str
;
10773 /* Verify the required arguments in the original call. */
10776 dest
= TREE_VALUE (arglist
);
10777 if (! POINTER_TYPE_P (TREE_TYPE (dest
)))
10779 arglist
= TREE_CHAIN (arglist
);
10782 len
= TREE_VALUE (arglist
);
10783 if (TREE_CODE (TREE_TYPE (len
)) != INTEGER_TYPE
)
10785 arglist
= TREE_CHAIN (arglist
);
10788 flag
= TREE_VALUE (arglist
);
10789 if (TREE_CODE (TREE_TYPE (len
)) != INTEGER_TYPE
)
10791 arglist
= TREE_CHAIN (arglist
);
10794 size
= TREE_VALUE (arglist
);
10795 if (TREE_CODE (TREE_TYPE (size
)) != INTEGER_TYPE
)
10797 arglist
= TREE_CHAIN (arglist
);
10800 fmt
= TREE_VALUE (arglist
);
10801 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
10803 arglist
= TREE_CHAIN (arglist
);
10805 if (! host_integerp (size
, 1))
10808 if (! integer_all_onesp (size
))
10810 if (! host_integerp (len
, 1))
10812 /* If LEN is not constant, try MAXLEN too.
10813 For MAXLEN only allow optimizing into non-_ocs function
10814 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10815 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
10821 if (tree_int_cst_lt (size
, maxlen
))
10825 if (!init_target_chars())
10828 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
10829 or if format doesn't contain % chars or is "%s". */
10830 if (! integer_zerop (flag
))
10832 fmt_str
= c_getstr (fmt
);
10833 if (fmt_str
== NULL
)
10835 if (strchr (fmt_str
, target_percent
) != NULL
&& strcmp (fmt_str
, target_percent_s
))
10839 arglist
= tree_cons (NULL_TREE
, fmt
, arglist
);
10840 arglist
= tree_cons (NULL_TREE
, len
, arglist
);
10841 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10843 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
10845 fn
= built_in_decls
[fcode
== BUILT_IN_VSNPRINTF_CHK
10846 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
];
10850 return build_function_call_expr (fn
, arglist
);
10853 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
10855 Return 0 if no simplification was possible, otherwise return the
10856 simplified form of the call as a tree. FCODE is the BUILT_IN_*
10857 code of the function to be simplified. */
10860 fold_builtin_printf (tree fndecl
, tree arglist
, bool ignore
,
10861 enum built_in_function fcode
)
10863 tree fmt
, fn
= NULL_TREE
, fn_putchar
, fn_puts
, arg
, call
;
10864 const char *fmt_str
= NULL
;
10866 /* If the return value is used, don't do the transformation. */
10870 /* Verify the required arguments in the original call. */
10871 if (fcode
== BUILT_IN_PRINTF_CHK
|| fcode
== BUILT_IN_VPRINTF_CHK
)
10877 flag
= TREE_VALUE (arglist
);
10878 if (TREE_CODE (TREE_TYPE (flag
)) != INTEGER_TYPE
10879 || TREE_SIDE_EFFECTS (flag
))
10881 arglist
= TREE_CHAIN (arglist
);
10886 fmt
= TREE_VALUE (arglist
);
10887 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
10889 arglist
= TREE_CHAIN (arglist
);
10891 /* Check whether the format is a literal string constant. */
10892 fmt_str
= c_getstr (fmt
);
10893 if (fmt_str
== NULL
)
10896 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
10898 /* If we're using an unlocked function, assume the other
10899 unlocked functions exist explicitly. */
10900 fn_putchar
= built_in_decls
[BUILT_IN_PUTCHAR_UNLOCKED
];
10901 fn_puts
= built_in_decls
[BUILT_IN_PUTS_UNLOCKED
];
10905 fn_putchar
= implicit_built_in_decls
[BUILT_IN_PUTCHAR
];
10906 fn_puts
= implicit_built_in_decls
[BUILT_IN_PUTS
];
10909 if (!init_target_chars())
10912 if (strcmp (fmt_str
, target_percent_s
) == 0 || strchr (fmt_str
, target_percent
) == NULL
)
10916 if (strcmp (fmt_str
, target_percent_s
) == 0)
10918 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
10922 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
10923 || TREE_CHAIN (arglist
))
10926 str
= c_getstr (TREE_VALUE (arglist
));
10932 /* The format specifier doesn't contain any '%' characters. */
10933 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
10939 /* If the string was "", printf does nothing. */
10940 if (str
[0] == '\0')
10941 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
10943 /* If the string has length of 1, call putchar. */
10944 if (str
[1] == '\0')
10946 /* Given printf("c"), (where c is any one character,)
10947 convert "c"[0] to an int and pass that to the replacement
10949 arg
= build_int_cst (NULL_TREE
, str
[0]);
10950 arglist
= build_tree_list (NULL_TREE
, arg
);
10955 /* If the string was "string\n", call puts("string"). */
10956 size_t len
= strlen (str
);
10957 if ((unsigned char)str
[len
- 1] == target_newline
)
10959 /* Create a NUL-terminated string that's one char shorter
10960 than the original, stripping off the trailing '\n'. */
10961 char *newstr
= alloca (len
);
10962 memcpy (newstr
, str
, len
- 1);
10963 newstr
[len
- 1] = 0;
10965 arg
= build_string_literal (len
, newstr
);
10966 arglist
= build_tree_list (NULL_TREE
, arg
);
10970 /* We'd like to arrange to call fputs(string,stdout) here,
10971 but we need stdout and don't have a way to get it yet. */
10976 /* The other optimizations can be done only on the non-va_list variants. */
10977 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
10980 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
10981 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
10984 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
10985 || TREE_CHAIN (arglist
))
10990 /* If the format specifier was "%c", call __builtin_putchar(arg). */
10991 else if (strcmp (fmt_str
, target_percent_c
) == 0)
10994 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
10995 || TREE_CHAIN (arglist
))
11003 call
= build_function_call_expr (fn
, arglist
);
11004 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), call
);
11007 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11009 Return 0 if no simplification was possible, otherwise return the
11010 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11011 code of the function to be simplified. */
11014 fold_builtin_fprintf (tree fndecl
, tree arglist
, bool ignore
,
11015 enum built_in_function fcode
)
11017 tree fp
, fmt
, fn
= NULL_TREE
, fn_fputc
, fn_fputs
, arg
, call
;
11018 const char *fmt_str
= NULL
;
11020 /* If the return value is used, don't do the transformation. */
11024 /* Verify the required arguments in the original call. */
11027 fp
= TREE_VALUE (arglist
);
11028 if (! POINTER_TYPE_P (TREE_TYPE (fp
)))
11030 arglist
= TREE_CHAIN (arglist
);
11032 if (fcode
== BUILT_IN_FPRINTF_CHK
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
11038 flag
= TREE_VALUE (arglist
);
11039 if (TREE_CODE (TREE_TYPE (flag
)) != INTEGER_TYPE
11040 || TREE_SIDE_EFFECTS (flag
))
11042 arglist
= TREE_CHAIN (arglist
);
11047 fmt
= TREE_VALUE (arglist
);
11048 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
11050 arglist
= TREE_CHAIN (arglist
);
11052 /* Check whether the format is a literal string constant. */
11053 fmt_str
= c_getstr (fmt
);
11054 if (fmt_str
== NULL
)
11057 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
11059 /* If we're using an unlocked function, assume the other
11060 unlocked functions exist explicitly. */
11061 fn_fputc
= built_in_decls
[BUILT_IN_FPUTC_UNLOCKED
];
11062 fn_fputs
= built_in_decls
[BUILT_IN_FPUTS_UNLOCKED
];
11066 fn_fputc
= implicit_built_in_decls
[BUILT_IN_FPUTC
];
11067 fn_fputs
= implicit_built_in_decls
[BUILT_IN_FPUTS
];
11070 if (!init_target_chars())
11073 /* If the format doesn't contain % args or %%, use strcpy. */
11074 if (strchr (fmt_str
, target_percent
) == NULL
)
11076 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
11080 /* If the format specifier was "", fprintf does nothing. */
11081 if (fmt_str
[0] == '\0')
11083 /* If FP has side-effects, just wait until gimplification is
11085 if (TREE_SIDE_EFFECTS (fp
))
11088 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
11091 /* When "string" doesn't contain %, replace all cases of
11092 fprintf (fp, string) with fputs (string, fp). The fputs
11093 builtin will take care of special cases like length == 1. */
11094 arglist
= build_tree_list (NULL_TREE
, fp
);
11095 arglist
= tree_cons (NULL_TREE
, fmt
, arglist
);
11099 /* The other optimizations can be done only on the non-va_list variants. */
11100 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
11103 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11104 else if (strcmp (fmt_str
, target_percent_s
) == 0)
11107 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
11108 || TREE_CHAIN (arglist
))
11110 arg
= TREE_VALUE (arglist
);
11111 arglist
= build_tree_list (NULL_TREE
, fp
);
11112 arglist
= tree_cons (NULL_TREE
, arg
, arglist
);
11116 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11117 else if (strcmp (fmt_str
, target_percent_c
) == 0)
11120 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
11121 || TREE_CHAIN (arglist
))
11123 arg
= TREE_VALUE (arglist
);
11124 arglist
= build_tree_list (NULL_TREE
, fp
);
11125 arglist
= tree_cons (NULL_TREE
, arg
, arglist
);
11132 call
= build_function_call_expr (fn
, arglist
);
11133 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), call
);
11136 /* Initialize format string characters in the target charset. */
11139 init_target_chars (void)
11144 target_newline
= lang_hooks
.to_target_charset ('\n');
11145 target_percent
= lang_hooks
.to_target_charset ('%');
11146 target_c
= lang_hooks
.to_target_charset ('c');
11147 target_s
= lang_hooks
.to_target_charset ('s');
11148 if (target_newline
== 0 || target_percent
== 0 || target_c
== 0
11152 target_percent_c
[0] = target_percent
;
11153 target_percent_c
[1] = target_c
;
11154 target_percent_c
[2] = '\0';
11156 target_percent_s
[0] = target_percent
;
11157 target_percent_s
[1] = target_s
;
11158 target_percent_s
[2] = '\0';
11160 target_percent_s_newline
[0] = target_percent
;
11161 target_percent_s_newline
[1] = target_s
;
11162 target_percent_s_newline
[2] = target_newline
;
11163 target_percent_s_newline
[3] = '\0';