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"
50 #include "tree-flow.h"
52 #ifndef PAD_VARARGS_DOWN
53 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
56 /* Define the names of the builtin function types and codes. */
57 const char *const built_in_class_names
[4]
58 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
60 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
61 const char * built_in_names
[(int) END_BUILTINS
] =
63 #include "builtins.def"
67 /* Setup an array of _DECL trees, make sure each element is
68 initialized to NULL_TREE. */
69 tree built_in_decls
[(int) END_BUILTINS
];
70 /* Declarations used when constructing the builtin implicitly in the compiler.
71 It may be NULL_TREE when this is invalid (for instance runtime is not
72 required to implement the function call in all cases). */
73 tree implicit_built_in_decls
[(int) END_BUILTINS
];
75 static const char *c_getstr (tree
);
76 static rtx
c_readstr (const char *, enum machine_mode
);
77 static int target_char_cast (tree
, char *);
78 static rtx
get_memory_rtx (tree
, tree
);
79 static int apply_args_size (void);
80 static int apply_result_size (void);
81 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
82 static rtx
result_vector (int, rtx
);
84 static void expand_builtin_update_setjmp_buf (rtx
);
85 static void expand_builtin_prefetch (tree
);
86 static rtx
expand_builtin_apply_args (void);
87 static rtx
expand_builtin_apply_args_1 (void);
88 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
89 static void expand_builtin_return (rtx
);
90 static enum type_class
type_to_class (tree
);
91 static rtx
expand_builtin_classify_type (tree
);
92 static void expand_errno_check (tree
, rtx
);
93 static rtx
expand_builtin_mathfn (tree
, rtx
, rtx
);
94 static rtx
expand_builtin_mathfn_2 (tree
, rtx
, rtx
);
95 static rtx
expand_builtin_mathfn_3 (tree
, rtx
, rtx
);
96 static rtx
expand_builtin_sincos (tree
);
97 static rtx
expand_builtin_int_roundingfn (tree
, rtx
, rtx
);
98 static rtx
expand_builtin_int_roundingfn_2 (tree
, rtx
, rtx
);
99 static rtx
expand_builtin_args_info (tree
);
100 static rtx
expand_builtin_next_arg (void);
101 static rtx
expand_builtin_va_start (tree
);
102 static rtx
expand_builtin_va_end (tree
);
103 static rtx
expand_builtin_va_copy (tree
);
104 static rtx
expand_builtin_memcmp (tree
, tree
, rtx
, enum machine_mode
);
105 static rtx
expand_builtin_strcmp (tree
, rtx
, enum machine_mode
);
106 static rtx
expand_builtin_strncmp (tree
, rtx
, enum machine_mode
);
107 static rtx
builtin_memcpy_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
108 static rtx
expand_builtin_strcat (tree
, tree
, rtx
, enum machine_mode
);
109 static rtx
expand_builtin_strncat (tree
, rtx
, enum machine_mode
);
110 static rtx
expand_builtin_strspn (tree
, rtx
, enum machine_mode
);
111 static rtx
expand_builtin_strcspn (tree
, rtx
, enum machine_mode
);
112 static rtx
expand_builtin_memcpy (tree
, rtx
, enum machine_mode
);
113 static rtx
expand_builtin_mempcpy (tree
, tree
, rtx
, enum machine_mode
, int);
114 static rtx
expand_builtin_memmove (tree
, tree
, rtx
, enum machine_mode
);
115 static rtx
expand_builtin_bcopy (tree
);
116 static rtx
expand_builtin_strcpy (tree
, tree
, rtx
, enum machine_mode
);
117 static rtx
expand_builtin_stpcpy (tree
, rtx
, enum machine_mode
);
118 static rtx
builtin_strncpy_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
119 static rtx
expand_builtin_strncpy (tree
, rtx
, enum machine_mode
);
120 static rtx
builtin_memset_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 bool integer_valued_real_p (tree
);
143 static tree
fold_trunc_transparent_mathfn (tree
, tree
);
144 static bool readonly_data_expr (tree
);
145 static rtx
expand_builtin_fabs (tree
, rtx
, rtx
);
146 static rtx
expand_builtin_signbit (tree
, rtx
);
147 static tree
fold_builtin_sqrt (tree
, tree
);
148 static tree
fold_builtin_cbrt (tree
, tree
);
149 static tree
fold_builtin_pow (tree
, tree
, tree
);
150 static tree
fold_builtin_powi (tree
, tree
, tree
);
151 static tree
fold_builtin_cos (tree
, tree
, tree
);
152 static tree
fold_builtin_cosh (tree
, tree
, tree
);
153 static tree
fold_builtin_tan (tree
, tree
);
154 static tree
fold_builtin_trunc (tree
, tree
);
155 static tree
fold_builtin_floor (tree
, tree
);
156 static tree
fold_builtin_ceil (tree
, tree
);
157 static tree
fold_builtin_round (tree
, tree
);
158 static tree
fold_builtin_int_roundingfn (tree
, tree
);
159 static tree
fold_builtin_bitop (tree
, tree
);
160 static tree
fold_builtin_memory_op (tree
, tree
, bool, int);
161 static tree
fold_builtin_strchr (tree
, tree
);
162 static tree
fold_builtin_memcmp (tree
);
163 static tree
fold_builtin_strcmp (tree
);
164 static tree
fold_builtin_strncmp (tree
);
165 static tree
fold_builtin_signbit (tree
, tree
);
166 static tree
fold_builtin_copysign (tree
, tree
, tree
);
167 static tree
fold_builtin_isascii (tree
);
168 static tree
fold_builtin_toascii (tree
);
169 static tree
fold_builtin_isdigit (tree
);
170 static tree
fold_builtin_fabs (tree
, tree
);
171 static tree
fold_builtin_abs (tree
, tree
);
172 static tree
fold_builtin_unordered_cmp (tree
, tree
, enum tree_code
,
174 static tree
fold_builtin_1 (tree
, tree
, bool);
176 static tree
fold_builtin_strpbrk (tree
, tree
);
177 static tree
fold_builtin_strstr (tree
, tree
);
178 static tree
fold_builtin_strrchr (tree
, tree
);
179 static tree
fold_builtin_strcat (tree
);
180 static tree
fold_builtin_strncat (tree
);
181 static tree
fold_builtin_strspn (tree
);
182 static tree
fold_builtin_strcspn (tree
);
183 static tree
fold_builtin_sprintf (tree
, int);
185 static rtx
expand_builtin_object_size (tree
);
186 static rtx
expand_builtin_memory_chk (tree
, rtx
, enum machine_mode
,
187 enum built_in_function
);
188 static void maybe_emit_chk_warning (tree
, enum built_in_function
);
189 static void maybe_emit_sprintf_chk_warning (tree
, enum built_in_function
);
190 static tree
fold_builtin_object_size (tree
);
191 static tree
fold_builtin_strcat_chk (tree
, tree
);
192 static tree
fold_builtin_strncat_chk (tree
, tree
);
193 static tree
fold_builtin_sprintf_chk (tree
, enum built_in_function
);
194 static tree
fold_builtin_printf (tree
, tree
, bool, enum built_in_function
);
195 static tree
fold_builtin_fprintf (tree
, tree
, bool, enum built_in_function
);
196 static bool init_target_chars (void);
198 static unsigned HOST_WIDE_INT target_newline
;
199 static unsigned HOST_WIDE_INT target_percent
;
200 static unsigned HOST_WIDE_INT target_c
;
201 static unsigned HOST_WIDE_INT target_s
;
202 static char target_percent_c
[3];
203 static char target_percent_s
[3];
204 static char target_percent_s_newline
[4];
205 static tree
do_mpfr_arg1 (tree
, tree
, int (*)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
206 const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, bool);
207 static tree
do_mpfr_arg2 (tree
, tree
, tree
,
208 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
209 static tree
do_mpfr_arg3 (tree
, tree
, tree
, tree
,
210 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
211 static tree
do_mpfr_sincos (tree
, tree
, tree
);
213 /* Return true if NODE should be considered for inline expansion regardless
214 of the optimization level. This means whenever a function is invoked with
215 its "internal" name, which normally contains the prefix "__builtin". */
217 static bool called_as_built_in (tree node
)
219 const char *name
= IDENTIFIER_POINTER (DECL_NAME (node
));
220 if (strncmp (name
, "__builtin_", 10) == 0)
222 if (strncmp (name
, "__sync_", 7) == 0)
227 /* Return the alignment in bits of EXP, a pointer valued expression.
228 But don't return more than MAX_ALIGN no matter what.
229 The alignment returned is, by default, the alignment of the thing that
230 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
232 Otherwise, look at the expression to see if we can do better, i.e., if the
233 expression is actually pointing at an object whose alignment is tighter. */
236 get_pointer_alignment (tree exp
, unsigned int max_align
)
238 unsigned int align
, inner
;
240 /* We rely on TER to compute accurate alignment information. */
241 if (!(optimize
&& flag_tree_ter
))
244 if (!POINTER_TYPE_P (TREE_TYPE (exp
)))
247 align
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp
)));
248 align
= MIN (align
, max_align
);
252 switch (TREE_CODE (exp
))
256 case NON_LVALUE_EXPR
:
257 exp
= TREE_OPERAND (exp
, 0);
258 if (! POINTER_TYPE_P (TREE_TYPE (exp
)))
261 inner
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp
)));
262 align
= MIN (inner
, max_align
);
266 /* If sum of pointer + int, restrict our maximum alignment to that
267 imposed by the integer. If not, we can't do any better than
269 if (! host_integerp (TREE_OPERAND (exp
, 1), 1))
272 while (((tree_low_cst (TREE_OPERAND (exp
, 1), 1))
273 & (max_align
/ BITS_PER_UNIT
- 1))
277 exp
= TREE_OPERAND (exp
, 0);
281 /* See what we are pointing at and look at its alignment. */
282 exp
= TREE_OPERAND (exp
, 0);
284 if (handled_component_p (exp
))
286 HOST_WIDE_INT bitsize
, bitpos
;
288 enum machine_mode mode
;
289 int unsignedp
, volatilep
;
291 exp
= get_inner_reference (exp
, &bitsize
, &bitpos
, &offset
,
292 &mode
, &unsignedp
, &volatilep
, true);
294 inner
= MIN (inner
, (unsigned) (bitpos
& -bitpos
));
295 if (offset
&& TREE_CODE (offset
) == PLUS_EXPR
296 && host_integerp (TREE_OPERAND (offset
, 1), 1))
298 /* Any overflow in calculating offset_bits won't change
301 = ((unsigned) tree_low_cst (TREE_OPERAND (offset
, 1), 1)
305 inner
= MIN (inner
, (offset_bits
& -offset_bits
));
306 offset
= TREE_OPERAND (offset
, 0);
308 if (offset
&& TREE_CODE (offset
) == MULT_EXPR
309 && host_integerp (TREE_OPERAND (offset
, 1), 1))
311 /* Any overflow in calculating offset_factor won't change
313 unsigned offset_factor
314 = ((unsigned) tree_low_cst (TREE_OPERAND (offset
, 1), 1)
318 inner
= MIN (inner
, (offset_factor
& -offset_factor
));
321 inner
= MIN (inner
, BITS_PER_UNIT
);
323 if (TREE_CODE (exp
) == FUNCTION_DECL
)
324 align
= FUNCTION_BOUNDARY
;
325 else if (DECL_P (exp
))
326 align
= MIN (inner
, DECL_ALIGN (exp
));
327 #ifdef CONSTANT_ALIGNMENT
328 else if (CONSTANT_CLASS_P (exp
))
329 align
= MIN (inner
, (unsigned)CONSTANT_ALIGNMENT (exp
, align
));
331 else if (TREE_CODE (exp
) == VIEW_CONVERT_EXPR
332 || TREE_CODE (exp
) == INDIRECT_REF
)
333 align
= MIN (TYPE_ALIGN (TREE_TYPE (exp
)), inner
);
335 align
= MIN (align
, inner
);
336 return MIN (align
, max_align
);
344 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
345 way, because it could contain a zero byte in the middle.
346 TREE_STRING_LENGTH is the size of the character array, not the string.
348 ONLY_VALUE should be nonzero if the result is not going to be emitted
349 into the instruction stream and zero if it is going to be expanded.
350 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
351 is returned, otherwise NULL, since
352 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
353 evaluate the side-effects.
355 The value returned is of type `ssizetype'.
357 Unfortunately, string_constant can't access the values of const char
358 arrays with initializers, so neither can we do so here. */
361 c_strlen (tree src
, int only_value
)
364 HOST_WIDE_INT offset
;
369 if (TREE_CODE (src
) == COND_EXPR
370 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
374 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
);
375 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
);
376 if (tree_int_cst_equal (len1
, len2
))
380 if (TREE_CODE (src
) == COMPOUND_EXPR
381 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
382 return c_strlen (TREE_OPERAND (src
, 1), only_value
);
384 src
= string_constant (src
, &offset_node
);
388 max
= TREE_STRING_LENGTH (src
) - 1;
389 ptr
= TREE_STRING_POINTER (src
);
391 if (offset_node
&& TREE_CODE (offset_node
) != INTEGER_CST
)
393 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
394 compute the offset to the following null if we don't know where to
395 start searching for it. */
398 for (i
= 0; i
< max
; i
++)
402 /* We don't know the starting offset, but we do know that the string
403 has no internal zero bytes. We can assume that the offset falls
404 within the bounds of the string; otherwise, the programmer deserves
405 what he gets. Subtract the offset from the length of the string,
406 and return that. This would perhaps not be valid if we were dealing
407 with named arrays in addition to literal string constants. */
409 return size_diffop (size_int (max
), offset_node
);
412 /* We have a known offset into the string. Start searching there for
413 a null character if we can represent it as a single HOST_WIDE_INT. */
414 if (offset_node
== 0)
416 else if (! host_integerp (offset_node
, 0))
419 offset
= tree_low_cst (offset_node
, 0);
421 /* If the offset is known to be out of bounds, warn, and call strlen at
423 if (offset
< 0 || offset
> max
)
425 warning (0, "offset outside bounds of constant string");
429 /* Use strlen to search for the first zero byte. Since any strings
430 constructed with build_string will have nulls appended, we win even
431 if we get handed something like (char[4])"abcd".
433 Since OFFSET is our starting index into the string, no further
434 calculation is needed. */
435 return ssize_int (strlen (ptr
+ offset
));
438 /* Return a char pointer for a C string if it is a string constant
439 or sum of string constant and integer constant. */
446 src
= string_constant (src
, &offset_node
);
450 if (offset_node
== 0)
451 return TREE_STRING_POINTER (src
);
452 else if (!host_integerp (offset_node
, 1)
453 || compare_tree_int (offset_node
, TREE_STRING_LENGTH (src
) - 1) > 0)
456 return TREE_STRING_POINTER (src
) + tree_low_cst (offset_node
, 1);
459 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
460 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
463 c_readstr (const char *str
, enum machine_mode mode
)
469 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
474 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
477 if (WORDS_BIG_ENDIAN
)
478 j
= GET_MODE_SIZE (mode
) - i
- 1;
479 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
480 && GET_MODE_SIZE (mode
) > UNITS_PER_WORD
)
481 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
483 gcc_assert (j
<= 2 * HOST_BITS_PER_WIDE_INT
);
486 ch
= (unsigned char) str
[i
];
487 c
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
489 return immed_double_const (c
[0], c
[1], mode
);
492 /* Cast a target constant CST to target CHAR and if that value fits into
493 host char type, return zero and put that value into variable pointed to by
497 target_char_cast (tree cst
, char *p
)
499 unsigned HOST_WIDE_INT val
, hostval
;
501 if (!host_integerp (cst
, 1)
502 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
505 val
= tree_low_cst (cst
, 1);
506 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
507 val
&= (((unsigned HOST_WIDE_INT
) 1) << CHAR_TYPE_SIZE
) - 1;
510 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
511 hostval
&= (((unsigned HOST_WIDE_INT
) 1) << HOST_BITS_PER_CHAR
) - 1;
520 /* Similar to save_expr, but assumes that arbitrary code is not executed
521 in between the multiple evaluations. In particular, we assume that a
522 non-addressable local variable will not be modified. */
525 builtin_save_expr (tree exp
)
527 if (TREE_ADDRESSABLE (exp
) == 0
528 && (TREE_CODE (exp
) == PARM_DECL
529 || (TREE_CODE (exp
) == VAR_DECL
&& !TREE_STATIC (exp
))))
532 return save_expr (exp
);
535 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
536 times to get the address of either a higher stack frame, or a return
537 address located within it (depending on FNDECL_CODE). */
540 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
)
544 #ifdef INITIAL_FRAME_ADDRESS_RTX
545 rtx tem
= INITIAL_FRAME_ADDRESS_RTX
;
549 /* For a zero count with __builtin_return_address, we don't care what
550 frame address we return, because target-specific definitions will
551 override us. Therefore frame pointer elimination is OK, and using
552 the soft frame pointer is OK.
554 For a nonzero count, or a zero count with __builtin_frame_address,
555 we require a stable offset from the current frame pointer to the
556 previous one, so we must use the hard frame pointer, and
557 we must disable frame pointer elimination. */
558 if (count
== 0 && fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
559 tem
= frame_pointer_rtx
;
562 tem
= hard_frame_pointer_rtx
;
564 /* Tell reload not to eliminate the frame pointer. */
565 current_function_accesses_prior_frames
= 1;
569 /* Some machines need special handling before we can access
570 arbitrary frames. For example, on the SPARC, we must first flush
571 all register windows to the stack. */
572 #ifdef SETUP_FRAME_ADDRESSES
574 SETUP_FRAME_ADDRESSES ();
577 /* On the SPARC, the return address is not in the frame, it is in a
578 register. There is no way to access it off of the current frame
579 pointer, but it can be accessed off the previous frame pointer by
580 reading the value from the register window save area. */
581 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
582 if (fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
586 /* Scan back COUNT frames to the specified frame. */
587 for (i
= 0; i
< count
; i
++)
589 /* Assume the dynamic chain pointer is in the word that the
590 frame address points to, unless otherwise specified. */
591 #ifdef DYNAMIC_CHAIN_ADDRESS
592 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
594 tem
= memory_address (Pmode
, tem
);
595 tem
= gen_frame_mem (Pmode
, tem
);
596 tem
= copy_to_reg (tem
);
599 /* For __builtin_frame_address, return what we've got. But, on
600 the SPARC for example, we may have to add a bias. */
601 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
602 #ifdef FRAME_ADDR_RTX
603 return FRAME_ADDR_RTX (tem
);
608 /* For __builtin_return_address, get the return address from that frame. */
609 #ifdef RETURN_ADDR_RTX
610 tem
= RETURN_ADDR_RTX (count
, tem
);
612 tem
= memory_address (Pmode
,
613 plus_constant (tem
, GET_MODE_SIZE (Pmode
)));
614 tem
= gen_frame_mem (Pmode
, tem
);
619 /* Alias set used for setjmp buffer. */
620 static HOST_WIDE_INT setjmp_alias_set
= -1;
622 /* Construct the leading half of a __builtin_setjmp call. Control will
623 return to RECEIVER_LABEL. This is also called directly by the SJLJ
624 exception handling code. */
627 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
629 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
633 if (setjmp_alias_set
== -1)
634 setjmp_alias_set
= new_alias_set ();
636 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
638 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
640 /* We store the frame pointer and the address of receiver_label in
641 the buffer and use the rest of it for the stack save area, which
642 is machine-dependent. */
644 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
645 set_mem_alias_set (mem
, setjmp_alias_set
);
646 emit_move_insn (mem
, targetm
.builtin_setjmp_frame_value ());
648 mem
= gen_rtx_MEM (Pmode
, plus_constant (buf_addr
, GET_MODE_SIZE (Pmode
))),
649 set_mem_alias_set (mem
, setjmp_alias_set
);
651 emit_move_insn (validize_mem (mem
),
652 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
654 stack_save
= gen_rtx_MEM (sa_mode
,
655 plus_constant (buf_addr
,
656 2 * GET_MODE_SIZE (Pmode
)));
657 set_mem_alias_set (stack_save
, setjmp_alias_set
);
658 emit_stack_save (SAVE_NONLOCAL
, &stack_save
, NULL_RTX
);
660 /* If there is further processing to do, do it. */
661 #ifdef HAVE_builtin_setjmp_setup
662 if (HAVE_builtin_setjmp_setup
)
663 emit_insn (gen_builtin_setjmp_setup (buf_addr
));
666 /* Tell optimize_save_area_alloca that extra work is going to
667 need to go on during alloca. */
668 current_function_calls_setjmp
= 1;
670 /* Set this so all the registers get saved in our frame; we need to be
671 able to copy the saved values for any registers from frames we unwind. */
672 current_function_has_nonlocal_label
= 1;
675 /* Construct the trailing part of a __builtin_setjmp call. This is
676 also called directly by the SJLJ exception handling code. */
679 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED
)
681 /* Clobber the FP when we get here, so we have to make sure it's
682 marked as used by this function. */
683 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
685 /* Mark the static chain as clobbered here so life information
686 doesn't get messed up for it. */
687 emit_insn (gen_rtx_CLOBBER (VOIDmode
, static_chain_rtx
));
689 /* Now put in the code to restore the frame pointer, and argument
690 pointer, if needed. */
691 #ifdef HAVE_nonlocal_goto
692 if (! HAVE_nonlocal_goto
)
695 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
696 /* This might change the hard frame pointer in ways that aren't
697 apparent to early optimization passes, so force a clobber. */
698 emit_insn (gen_rtx_CLOBBER (VOIDmode
, hard_frame_pointer_rtx
));
701 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
702 if (fixed_regs
[ARG_POINTER_REGNUM
])
704 #ifdef ELIMINABLE_REGS
706 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
708 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
709 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
710 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
713 if (i
== ARRAY_SIZE (elim_regs
))
716 /* Now restore our arg pointer from the address at which it
717 was saved in our stack frame. */
718 emit_move_insn (virtual_incoming_args_rtx
,
719 copy_to_reg (get_arg_pointer_save_area (cfun
)));
724 #ifdef HAVE_builtin_setjmp_receiver
725 if (HAVE_builtin_setjmp_receiver
)
726 emit_insn (gen_builtin_setjmp_receiver (receiver_label
));
729 #ifdef HAVE_nonlocal_goto_receiver
730 if (HAVE_nonlocal_goto_receiver
)
731 emit_insn (gen_nonlocal_goto_receiver ());
736 /* @@@ This is a kludge. Not all machine descriptions define a blockage
737 insn, but we must not allow the code we just generated to be reordered
738 by scheduling. Specifically, the update of the frame pointer must
739 happen immediately, not later. So emit an ASM_INPUT to act as blockage
741 emit_insn (gen_rtx_ASM_INPUT (VOIDmode
, ""));
744 /* __builtin_longjmp is passed a pointer to an array of five words (not
745 all will be used on all machines). It operates similarly to the C
746 library function of the same name, but is more efficient. Much of
747 the code below is copied from the handling of non-local gotos. */
750 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
752 rtx fp
, lab
, stack
, insn
, last
;
753 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
755 if (setjmp_alias_set
== -1)
756 setjmp_alias_set
= new_alias_set ();
758 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
760 buf_addr
= force_reg (Pmode
, buf_addr
);
762 /* We used to store value in static_chain_rtx, but that fails if pointers
763 are smaller than integers. We instead require that the user must pass
764 a second argument of 1, because that is what builtin_setjmp will
765 return. This also makes EH slightly more efficient, since we are no
766 longer copying around a value that we don't care about. */
767 gcc_assert (value
== const1_rtx
);
769 last
= get_last_insn ();
770 #ifdef HAVE_builtin_longjmp
771 if (HAVE_builtin_longjmp
)
772 emit_insn (gen_builtin_longjmp (buf_addr
));
776 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
777 lab
= gen_rtx_MEM (Pmode
, plus_constant (buf_addr
,
778 GET_MODE_SIZE (Pmode
)));
780 stack
= gen_rtx_MEM (sa_mode
, plus_constant (buf_addr
,
781 2 * GET_MODE_SIZE (Pmode
)));
782 set_mem_alias_set (fp
, setjmp_alias_set
);
783 set_mem_alias_set (lab
, setjmp_alias_set
);
784 set_mem_alias_set (stack
, setjmp_alias_set
);
786 /* Pick up FP, label, and SP from the block and jump. This code is
787 from expand_goto in stmt.c; see there for detailed comments. */
788 #ifdef HAVE_nonlocal_goto
789 if (HAVE_nonlocal_goto
)
790 /* We have to pass a value to the nonlocal_goto pattern that will
791 get copied into the static_chain pointer, but it does not matter
792 what that value is, because builtin_setjmp does not use it. */
793 emit_insn (gen_nonlocal_goto (value
, lab
, stack
, fp
));
797 lab
= copy_to_reg (lab
);
799 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
800 gen_rtx_MEM (BLKmode
,
801 gen_rtx_SCRATCH (VOIDmode
))));
802 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
803 gen_rtx_MEM (BLKmode
,
804 hard_frame_pointer_rtx
)));
806 emit_move_insn (hard_frame_pointer_rtx
, fp
);
807 emit_stack_restore (SAVE_NONLOCAL
, stack
, NULL_RTX
);
809 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
810 emit_insn (gen_rtx_USE (VOIDmode
, stack_pointer_rtx
));
811 emit_indirect_jump (lab
);
815 /* Search backwards and mark the jump insn as a non-local goto.
816 Note that this precludes the use of __builtin_longjmp to a
817 __builtin_setjmp target in the same function. However, we've
818 already cautioned the user that these functions are for
819 internal exception handling use only. */
820 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
822 gcc_assert (insn
!= last
);
826 REG_NOTES (insn
) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO
, const0_rtx
,
830 else if (CALL_P (insn
))
835 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
836 and the address of the save area. */
839 expand_builtin_nonlocal_goto (tree arglist
)
841 tree t_label
, t_save_area
;
842 rtx r_label
, r_save_area
, r_fp
, r_sp
, insn
;
844 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
847 t_label
= TREE_VALUE (arglist
);
848 arglist
= TREE_CHAIN (arglist
);
849 t_save_area
= TREE_VALUE (arglist
);
851 r_label
= expand_normal (t_label
);
852 r_label
= convert_memory_address (Pmode
, r_label
);
853 r_save_area
= expand_normal (t_save_area
);
854 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
855 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
856 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
857 plus_constant (r_save_area
, GET_MODE_SIZE (Pmode
)));
859 current_function_has_nonlocal_goto
= 1;
861 #ifdef HAVE_nonlocal_goto
862 /* ??? We no longer need to pass the static chain value, afaik. */
863 if (HAVE_nonlocal_goto
)
864 emit_insn (gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
868 r_label
= copy_to_reg (r_label
);
870 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
871 gen_rtx_MEM (BLKmode
,
872 gen_rtx_SCRATCH (VOIDmode
))));
874 emit_insn (gen_rtx_CLOBBER (VOIDmode
,
875 gen_rtx_MEM (BLKmode
,
876 hard_frame_pointer_rtx
)));
878 /* Restore frame pointer for containing function.
879 This sets the actual hard register used for the frame pointer
880 to the location of the function's incoming static chain info.
881 The non-local goto handler will then adjust it to contain the
882 proper value and reload the argument pointer, if needed. */
883 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
884 emit_stack_restore (SAVE_NONLOCAL
, r_sp
, NULL_RTX
);
886 /* USE of hard_frame_pointer_rtx added for consistency;
887 not clear if really needed. */
888 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
889 emit_insn (gen_rtx_USE (VOIDmode
, stack_pointer_rtx
));
890 emit_indirect_jump (r_label
);
893 /* Search backwards to the jump insn and mark it as a
895 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
899 REG_NOTES (insn
) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO
,
900 const0_rtx
, REG_NOTES (insn
));
903 else if (CALL_P (insn
))
910 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
911 (not all will be used on all machines) that was passed to __builtin_setjmp.
912 It updates the stack pointer in that block to correspond to the current
916 expand_builtin_update_setjmp_buf (rtx buf_addr
)
918 enum machine_mode sa_mode
= Pmode
;
922 #ifdef HAVE_save_stack_nonlocal
923 if (HAVE_save_stack_nonlocal
)
924 sa_mode
= insn_data
[(int) CODE_FOR_save_stack_nonlocal
].operand
[0].mode
;
926 #ifdef STACK_SAVEAREA_MODE
927 sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
931 = gen_rtx_MEM (sa_mode
,
934 plus_constant (buf_addr
, 2 * GET_MODE_SIZE (Pmode
))));
938 emit_insn (gen_setjmp ());
941 emit_stack_save (SAVE_NONLOCAL
, &stack_save
, NULL_RTX
);
944 /* Expand a call to __builtin_prefetch. For a target that does not support
945 data prefetch, evaluate the memory address argument in case it has side
949 expand_builtin_prefetch (tree arglist
)
951 tree arg0
, arg1
, arg2
;
954 if (!validate_arglist (arglist
, POINTER_TYPE
, 0))
957 arg0
= TREE_VALUE (arglist
);
958 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
959 zero (read) and argument 2 (locality) defaults to 3 (high degree of
961 if (TREE_CHAIN (arglist
))
963 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
964 if (TREE_CHAIN (TREE_CHAIN (arglist
)))
965 arg2
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
967 arg2
= build_int_cst (NULL_TREE
, 3);
971 arg1
= integer_zero_node
;
972 arg2
= build_int_cst (NULL_TREE
, 3);
975 /* Argument 0 is an address. */
976 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
978 /* Argument 1 (read/write flag) must be a compile-time constant int. */
979 if (TREE_CODE (arg1
) != INTEGER_CST
)
981 error ("second argument to %<__builtin_prefetch%> must be a constant");
982 arg1
= integer_zero_node
;
984 op1
= expand_normal (arg1
);
985 /* Argument 1 must be either zero or one. */
986 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
988 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
993 /* Argument 2 (locality) must be a compile-time constant int. */
994 if (TREE_CODE (arg2
) != INTEGER_CST
)
996 error ("third argument to %<__builtin_prefetch%> must be a constant");
997 arg2
= integer_zero_node
;
999 op2
= expand_normal (arg2
);
1000 /* Argument 2 must be 0, 1, 2, or 3. */
1001 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
1003 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1007 #ifdef HAVE_prefetch
1010 if ((! (*insn_data
[(int) CODE_FOR_prefetch
].operand
[0].predicate
)
1012 insn_data
[(int) CODE_FOR_prefetch
].operand
[0].mode
))
1013 || (GET_MODE (op0
) != Pmode
))
1015 op0
= convert_memory_address (Pmode
, op0
);
1016 op0
= force_reg (Pmode
, op0
);
1018 emit_insn (gen_prefetch (op0
, op1
, op2
));
1022 /* Don't do anything with direct references to volatile memory, but
1023 generate code to handle other side effects. */
1024 if (!MEM_P (op0
) && side_effects_p (op0
))
1028 /* Get a MEM rtx for expression EXP which is the address of an operand
1029 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1030 the maximum length of the block of memory that might be accessed or
1034 get_memory_rtx (tree exp
, tree len
)
1036 rtx addr
= expand_expr (exp
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
1037 rtx mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
1039 /* Get an expression we can use to find the attributes to assign to MEM.
1040 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1041 we can. First remove any nops. */
1042 while ((TREE_CODE (exp
) == NOP_EXPR
|| TREE_CODE (exp
) == CONVERT_EXPR
1043 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
1044 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
1045 exp
= TREE_OPERAND (exp
, 0);
1047 if (TREE_CODE (exp
) == ADDR_EXPR
)
1048 exp
= TREE_OPERAND (exp
, 0);
1049 else if (POINTER_TYPE_P (TREE_TYPE (exp
)))
1050 exp
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (exp
)), exp
);
1054 /* Honor attributes derived from exp, except for the alias set
1055 (as builtin stringops may alias with anything) and the size
1056 (as stringops may access multiple array elements). */
1059 set_mem_attributes (mem
, exp
, 0);
1061 /* Allow the string and memory builtins to overflow from one
1062 field into another, see http://gcc.gnu.org/PR23561.
1063 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1064 memory accessed by the string or memory builtin will fit
1065 within the field. */
1066 if (MEM_EXPR (mem
) && TREE_CODE (MEM_EXPR (mem
)) == COMPONENT_REF
)
1068 tree mem_expr
= MEM_EXPR (mem
);
1069 HOST_WIDE_INT offset
= -1, length
= -1;
1072 while (TREE_CODE (inner
) == ARRAY_REF
1073 || TREE_CODE (inner
) == NOP_EXPR
1074 || TREE_CODE (inner
) == CONVERT_EXPR
1075 || TREE_CODE (inner
) == NON_LVALUE_EXPR
1076 || TREE_CODE (inner
) == VIEW_CONVERT_EXPR
1077 || TREE_CODE (inner
) == SAVE_EXPR
)
1078 inner
= TREE_OPERAND (inner
, 0);
1080 gcc_assert (TREE_CODE (inner
) == COMPONENT_REF
);
1082 if (MEM_OFFSET (mem
)
1083 && GET_CODE (MEM_OFFSET (mem
)) == CONST_INT
)
1084 offset
= INTVAL (MEM_OFFSET (mem
));
1086 if (offset
>= 0 && len
&& host_integerp (len
, 0))
1087 length
= tree_low_cst (len
, 0);
1089 while (TREE_CODE (inner
) == COMPONENT_REF
)
1091 tree field
= TREE_OPERAND (inner
, 1);
1092 gcc_assert (! DECL_BIT_FIELD (field
));
1093 gcc_assert (TREE_CODE (mem_expr
) == COMPONENT_REF
);
1094 gcc_assert (field
== TREE_OPERAND (mem_expr
, 1));
1097 && TYPE_SIZE_UNIT (TREE_TYPE (inner
))
1098 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner
)), 0))
1101 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner
)), 0);
1102 /* If we can prove the memory starting at XEXP (mem, 0)
1103 and ending at XEXP (mem, 0) + LENGTH will fit into
1104 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1107 && offset
+ length
<= size
)
1112 && host_integerp (DECL_FIELD_OFFSET (field
), 0))
1113 offset
+= tree_low_cst (DECL_FIELD_OFFSET (field
), 0)
1114 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1)
1122 mem_expr
= TREE_OPERAND (mem_expr
, 0);
1123 inner
= TREE_OPERAND (inner
, 0);
1126 if (mem_expr
== NULL
)
1128 if (mem_expr
!= MEM_EXPR (mem
))
1130 set_mem_expr (mem
, mem_expr
);
1131 set_mem_offset (mem
, offset
>= 0 ? GEN_INT (offset
) : NULL_RTX
);
1134 set_mem_alias_set (mem
, 0);
1135 set_mem_size (mem
, NULL_RTX
);
1141 /* Built-in functions to perform an untyped call and return. */
1143 /* For each register that may be used for calling a function, this
1144 gives a mode used to copy the register's value. VOIDmode indicates
1145 the register is not used for calling a function. If the machine
1146 has register windows, this gives only the outbound registers.
1147 INCOMING_REGNO gives the corresponding inbound register. */
1148 static enum machine_mode apply_args_mode
[FIRST_PSEUDO_REGISTER
];
1150 /* For each register that may be used for returning values, this gives
1151 a mode used to copy the register's value. VOIDmode indicates the
1152 register is not used for returning values. If the machine has
1153 register windows, this gives only the outbound registers.
1154 INCOMING_REGNO gives the corresponding inbound register. */
1155 static enum machine_mode apply_result_mode
[FIRST_PSEUDO_REGISTER
];
1157 /* For each register that may be used for calling a function, this
1158 gives the offset of that register into the block returned by
1159 __builtin_apply_args. 0 indicates that the register is not
1160 used for calling a function. */
1161 static int apply_args_reg_offset
[FIRST_PSEUDO_REGISTER
];
1163 /* Return the size required for the block returned by __builtin_apply_args,
1164 and initialize apply_args_mode. */
1167 apply_args_size (void)
1169 static int size
= -1;
1172 enum machine_mode mode
;
1174 /* The values computed by this function never change. */
1177 /* The first value is the incoming arg-pointer. */
1178 size
= GET_MODE_SIZE (Pmode
);
1180 /* The second value is the structure value address unless this is
1181 passed as an "invisible" first argument. */
1182 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1183 size
+= GET_MODE_SIZE (Pmode
);
1185 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1186 if (FUNCTION_ARG_REGNO_P (regno
))
1188 mode
= reg_raw_mode
[regno
];
1190 gcc_assert (mode
!= VOIDmode
);
1192 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1193 if (size
% align
!= 0)
1194 size
= CEIL (size
, align
) * align
;
1195 apply_args_reg_offset
[regno
] = size
;
1196 size
+= GET_MODE_SIZE (mode
);
1197 apply_args_mode
[regno
] = mode
;
1201 apply_args_mode
[regno
] = VOIDmode
;
1202 apply_args_reg_offset
[regno
] = 0;
1208 /* Return the size required for the block returned by __builtin_apply,
1209 and initialize apply_result_mode. */
1212 apply_result_size (void)
1214 static int size
= -1;
1216 enum machine_mode mode
;
1218 /* The values computed by this function never change. */
1223 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1224 if (FUNCTION_VALUE_REGNO_P (regno
))
1226 mode
= reg_raw_mode
[regno
];
1228 gcc_assert (mode
!= VOIDmode
);
1230 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1231 if (size
% align
!= 0)
1232 size
= CEIL (size
, align
) * align
;
1233 size
+= GET_MODE_SIZE (mode
);
1234 apply_result_mode
[regno
] = mode
;
1237 apply_result_mode
[regno
] = VOIDmode
;
1239 /* Allow targets that use untyped_call and untyped_return to override
1240 the size so that machine-specific information can be stored here. */
1241 #ifdef APPLY_RESULT_SIZE
1242 size
= APPLY_RESULT_SIZE
;
1248 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1249 /* Create a vector describing the result block RESULT. If SAVEP is true,
1250 the result block is used to save the values; otherwise it is used to
1251 restore the values. */
1254 result_vector (int savep
, rtx result
)
1256 int regno
, size
, align
, nelts
;
1257 enum machine_mode mode
;
1259 rtx
*savevec
= alloca (FIRST_PSEUDO_REGISTER
* sizeof (rtx
));
1262 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1263 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1265 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1266 if (size
% align
!= 0)
1267 size
= CEIL (size
, align
) * align
;
1268 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
1269 mem
= adjust_address (result
, mode
, size
);
1270 savevec
[nelts
++] = (savep
1271 ? gen_rtx_SET (VOIDmode
, mem
, reg
)
1272 : gen_rtx_SET (VOIDmode
, reg
, mem
));
1273 size
+= GET_MODE_SIZE (mode
);
1275 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
1277 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1279 /* Save the state required to perform an untyped call with the same
1280 arguments as were passed to the current function. */
1283 expand_builtin_apply_args_1 (void)
1286 int size
, align
, regno
;
1287 enum machine_mode mode
;
1288 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
1290 /* Create a block where the arg-pointer, structure value address,
1291 and argument registers can be saved. */
1292 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
1294 /* Walk past the arg-pointer and structure value address. */
1295 size
= GET_MODE_SIZE (Pmode
);
1296 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1297 size
+= GET_MODE_SIZE (Pmode
);
1299 /* Save each register used in calling a function to the block. */
1300 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1301 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1303 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1304 if (size
% align
!= 0)
1305 size
= CEIL (size
, align
) * align
;
1307 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1309 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
1310 size
+= GET_MODE_SIZE (mode
);
1313 /* Save the arg pointer to the block. */
1314 tem
= copy_to_reg (virtual_incoming_args_rtx
);
1315 #ifdef STACK_GROWS_DOWNWARD
1316 /* We need the pointer as the caller actually passed them to us, not
1317 as we might have pretended they were passed. Make sure it's a valid
1318 operand, as emit_move_insn isn't expected to handle a PLUS. */
1320 = force_operand (plus_constant (tem
, current_function_pretend_args_size
),
1323 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
1325 size
= GET_MODE_SIZE (Pmode
);
1327 /* Save the structure value address unless this is passed as an
1328 "invisible" first argument. */
1329 if (struct_incoming_value
)
1331 emit_move_insn (adjust_address (registers
, Pmode
, size
),
1332 copy_to_reg (struct_incoming_value
));
1333 size
+= GET_MODE_SIZE (Pmode
);
1336 /* Return the address of the block. */
1337 return copy_addr_to_reg (XEXP (registers
, 0));
1340 /* __builtin_apply_args returns block of memory allocated on
1341 the stack into which is stored the arg pointer, structure
1342 value address, static chain, and all the registers that might
1343 possibly be used in performing a function call. The code is
1344 moved to the start of the function so the incoming values are
1348 expand_builtin_apply_args (void)
1350 /* Don't do __builtin_apply_args more than once in a function.
1351 Save the result of the first call and reuse it. */
1352 if (apply_args_value
!= 0)
1353 return apply_args_value
;
1355 /* When this function is called, it means that registers must be
1356 saved on entry to this function. So we migrate the
1357 call to the first insn of this function. */
1362 temp
= expand_builtin_apply_args_1 ();
1366 apply_args_value
= temp
;
1368 /* Put the insns after the NOTE that starts the function.
1369 If this is inside a start_sequence, make the outer-level insn
1370 chain current, so the code is placed at the start of the
1372 push_topmost_sequence ();
1373 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
1374 pop_topmost_sequence ();
1379 /* Perform an untyped call and save the state required to perform an
1380 untyped return of whatever value was returned by the given function. */
1383 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
1385 int size
, align
, regno
;
1386 enum machine_mode mode
;
1387 rtx incoming_args
, result
, reg
, dest
, src
, call_insn
;
1388 rtx old_stack_level
= 0;
1389 rtx call_fusage
= 0;
1390 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
1392 arguments
= convert_memory_address (Pmode
, arguments
);
1394 /* Create a block where the return registers can be saved. */
1395 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
1397 /* Fetch the arg pointer from the ARGUMENTS block. */
1398 incoming_args
= gen_reg_rtx (Pmode
);
1399 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
1400 #ifndef STACK_GROWS_DOWNWARD
1401 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
1402 incoming_args
, 0, OPTAB_LIB_WIDEN
);
1405 /* Push a new argument block and copy the arguments. Do not allow
1406 the (potential) memcpy call below to interfere with our stack
1408 do_pending_stack_adjust ();
1411 /* Save the stack with nonlocal if available. */
1412 #ifdef HAVE_save_stack_nonlocal
1413 if (HAVE_save_stack_nonlocal
)
1414 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
, NULL_RTX
);
1417 emit_stack_save (SAVE_BLOCK
, &old_stack_level
, NULL_RTX
);
1419 /* Allocate a block of memory onto the stack and copy the memory
1420 arguments to the outgoing arguments address. */
1421 allocate_dynamic_stack_space (argsize
, 0, BITS_PER_UNIT
);
1422 dest
= virtual_outgoing_args_rtx
;
1423 #ifndef STACK_GROWS_DOWNWARD
1424 if (GET_CODE (argsize
) == CONST_INT
)
1425 dest
= plus_constant (dest
, -INTVAL (argsize
));
1427 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
1429 dest
= gen_rtx_MEM (BLKmode
, dest
);
1430 set_mem_align (dest
, PARM_BOUNDARY
);
1431 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
1432 set_mem_align (src
, PARM_BOUNDARY
);
1433 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
1435 /* Refer to the argument block. */
1437 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
1438 set_mem_align (arguments
, PARM_BOUNDARY
);
1440 /* Walk past the arg-pointer and structure value address. */
1441 size
= GET_MODE_SIZE (Pmode
);
1443 size
+= GET_MODE_SIZE (Pmode
);
1445 /* Restore each of the registers previously saved. Make USE insns
1446 for each of these registers for use in making the call. */
1447 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1448 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1450 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1451 if (size
% align
!= 0)
1452 size
= CEIL (size
, align
) * align
;
1453 reg
= gen_rtx_REG (mode
, regno
);
1454 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
1455 use_reg (&call_fusage
, reg
);
1456 size
+= GET_MODE_SIZE (mode
);
1459 /* Restore the structure value address unless this is passed as an
1460 "invisible" first argument. */
1461 size
= GET_MODE_SIZE (Pmode
);
1464 rtx value
= gen_reg_rtx (Pmode
);
1465 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
1466 emit_move_insn (struct_value
, value
);
1467 if (REG_P (struct_value
))
1468 use_reg (&call_fusage
, struct_value
);
1469 size
+= GET_MODE_SIZE (Pmode
);
1472 /* All arguments and registers used for the call are set up by now! */
1473 function
= prepare_call_address (function
, NULL
, &call_fusage
, 0, 0);
1475 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1476 and we don't want to load it into a register as an optimization,
1477 because prepare_call_address already did it if it should be done. */
1478 if (GET_CODE (function
) != SYMBOL_REF
)
1479 function
= memory_address (FUNCTION_MODE
, function
);
1481 /* Generate the actual call instruction and save the return value. */
1482 #ifdef HAVE_untyped_call
1483 if (HAVE_untyped_call
)
1484 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE
, function
),
1485 result
, result_vector (1, result
)));
1488 #ifdef HAVE_call_value
1489 if (HAVE_call_value
)
1493 /* Locate the unique return register. It is not possible to
1494 express a call that sets more than one return register using
1495 call_value; use untyped_call for that. In fact, untyped_call
1496 only needs to save the return registers in the given block. */
1497 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1498 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1500 gcc_assert (!valreg
); /* HAVE_untyped_call required. */
1502 valreg
= gen_rtx_REG (mode
, regno
);
1505 emit_call_insn (GEN_CALL_VALUE (valreg
,
1506 gen_rtx_MEM (FUNCTION_MODE
, function
),
1507 const0_rtx
, NULL_RTX
, const0_rtx
));
1509 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
1515 /* Find the CALL insn we just emitted, and attach the register usage
1517 call_insn
= last_call_insn ();
1518 add_function_usage_to (call_insn
, call_fusage
);
1520 /* Restore the stack. */
1521 #ifdef HAVE_save_stack_nonlocal
1522 if (HAVE_save_stack_nonlocal
)
1523 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
, NULL_RTX
);
1526 emit_stack_restore (SAVE_BLOCK
, old_stack_level
, NULL_RTX
);
1530 /* Return the address of the result block. */
1531 result
= copy_addr_to_reg (XEXP (result
, 0));
1532 return convert_memory_address (ptr_mode
, result
);
1535 /* Perform an untyped return. */
1538 expand_builtin_return (rtx result
)
1540 int size
, align
, regno
;
1541 enum machine_mode mode
;
1543 rtx call_fusage
= 0;
1545 result
= convert_memory_address (Pmode
, result
);
1547 apply_result_size ();
1548 result
= gen_rtx_MEM (BLKmode
, result
);
1550 #ifdef HAVE_untyped_return
1551 if (HAVE_untyped_return
)
1553 emit_jump_insn (gen_untyped_return (result
, result_vector (0, result
)));
1559 /* Restore the return value and note that each value is used. */
1561 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1562 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1564 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1565 if (size
% align
!= 0)
1566 size
= CEIL (size
, align
) * align
;
1567 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1568 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
1570 push_to_sequence (call_fusage
);
1571 emit_insn (gen_rtx_USE (VOIDmode
, reg
));
1572 call_fusage
= get_insns ();
1574 size
+= GET_MODE_SIZE (mode
);
1577 /* Put the USE insns before the return. */
1578 emit_insn (call_fusage
);
1580 /* Return whatever values was restored by jumping directly to the end
1582 expand_naked_return ();
1585 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1587 static enum type_class
1588 type_to_class (tree type
)
1590 switch (TREE_CODE (type
))
1592 case VOID_TYPE
: return void_type_class
;
1593 case INTEGER_TYPE
: return integer_type_class
;
1594 case ENUMERAL_TYPE
: return enumeral_type_class
;
1595 case BOOLEAN_TYPE
: return boolean_type_class
;
1596 case POINTER_TYPE
: return pointer_type_class
;
1597 case REFERENCE_TYPE
: return reference_type_class
;
1598 case OFFSET_TYPE
: return offset_type_class
;
1599 case REAL_TYPE
: return real_type_class
;
1600 case COMPLEX_TYPE
: return complex_type_class
;
1601 case FUNCTION_TYPE
: return function_type_class
;
1602 case METHOD_TYPE
: return method_type_class
;
1603 case RECORD_TYPE
: return record_type_class
;
1605 case QUAL_UNION_TYPE
: return union_type_class
;
1606 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
1607 ? string_type_class
: array_type_class
);
1608 case LANG_TYPE
: return lang_type_class
;
1609 default: return no_type_class
;
1613 /* Expand a call to __builtin_classify_type with arguments found in
1617 expand_builtin_classify_type (tree arglist
)
1620 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist
))));
1621 return GEN_INT (no_type_class
);
1624 /* This helper macro, meant to be used in mathfn_built_in below,
1625 determines which among a set of three builtin math functions is
1626 appropriate for a given type mode. The `F' and `L' cases are
1627 automatically generated from the `double' case. */
1628 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1629 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1630 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1631 fcodel = BUILT_IN_MATHFN##L ; break;
1633 /* Return mathematic function equivalent to FN but operating directly
1634 on TYPE, if available. If we can't do the conversion, return zero. */
1636 mathfn_built_in (tree type
, enum built_in_function fn
)
1638 enum built_in_function fcode
, fcodef
, fcodel
;
1642 CASE_MATHFN (BUILT_IN_ACOS
)
1643 CASE_MATHFN (BUILT_IN_ACOSH
)
1644 CASE_MATHFN (BUILT_IN_ASIN
)
1645 CASE_MATHFN (BUILT_IN_ASINH
)
1646 CASE_MATHFN (BUILT_IN_ATAN
)
1647 CASE_MATHFN (BUILT_IN_ATAN2
)
1648 CASE_MATHFN (BUILT_IN_ATANH
)
1649 CASE_MATHFN (BUILT_IN_CBRT
)
1650 CASE_MATHFN (BUILT_IN_CEIL
)
1651 CASE_MATHFN (BUILT_IN_COPYSIGN
)
1652 CASE_MATHFN (BUILT_IN_COS
)
1653 CASE_MATHFN (BUILT_IN_COSH
)
1654 CASE_MATHFN (BUILT_IN_DREM
)
1655 CASE_MATHFN (BUILT_IN_ERF
)
1656 CASE_MATHFN (BUILT_IN_ERFC
)
1657 CASE_MATHFN (BUILT_IN_EXP
)
1658 CASE_MATHFN (BUILT_IN_EXP10
)
1659 CASE_MATHFN (BUILT_IN_EXP2
)
1660 CASE_MATHFN (BUILT_IN_EXPM1
)
1661 CASE_MATHFN (BUILT_IN_FABS
)
1662 CASE_MATHFN (BUILT_IN_FDIM
)
1663 CASE_MATHFN (BUILT_IN_FLOOR
)
1664 CASE_MATHFN (BUILT_IN_FMA
)
1665 CASE_MATHFN (BUILT_IN_FMAX
)
1666 CASE_MATHFN (BUILT_IN_FMIN
)
1667 CASE_MATHFN (BUILT_IN_FMOD
)
1668 CASE_MATHFN (BUILT_IN_FREXP
)
1669 CASE_MATHFN (BUILT_IN_GAMMA
)
1670 CASE_MATHFN (BUILT_IN_HUGE_VAL
)
1671 CASE_MATHFN (BUILT_IN_HYPOT
)
1672 CASE_MATHFN (BUILT_IN_ILOGB
)
1673 CASE_MATHFN (BUILT_IN_INF
)
1674 CASE_MATHFN (BUILT_IN_J0
)
1675 CASE_MATHFN (BUILT_IN_J1
)
1676 CASE_MATHFN (BUILT_IN_JN
)
1677 CASE_MATHFN (BUILT_IN_LCEIL
)
1678 CASE_MATHFN (BUILT_IN_LDEXP
)
1679 CASE_MATHFN (BUILT_IN_LFLOOR
)
1680 CASE_MATHFN (BUILT_IN_LGAMMA
)
1681 CASE_MATHFN (BUILT_IN_LLCEIL
)
1682 CASE_MATHFN (BUILT_IN_LLFLOOR
)
1683 CASE_MATHFN (BUILT_IN_LLRINT
)
1684 CASE_MATHFN (BUILT_IN_LLROUND
)
1685 CASE_MATHFN (BUILT_IN_LOG
)
1686 CASE_MATHFN (BUILT_IN_LOG10
)
1687 CASE_MATHFN (BUILT_IN_LOG1P
)
1688 CASE_MATHFN (BUILT_IN_LOG2
)
1689 CASE_MATHFN (BUILT_IN_LOGB
)
1690 CASE_MATHFN (BUILT_IN_LRINT
)
1691 CASE_MATHFN (BUILT_IN_LROUND
)
1692 CASE_MATHFN (BUILT_IN_MODF
)
1693 CASE_MATHFN (BUILT_IN_NAN
)
1694 CASE_MATHFN (BUILT_IN_NANS
)
1695 CASE_MATHFN (BUILT_IN_NEARBYINT
)
1696 CASE_MATHFN (BUILT_IN_NEXTAFTER
)
1697 CASE_MATHFN (BUILT_IN_NEXTTOWARD
)
1698 CASE_MATHFN (BUILT_IN_POW
)
1699 CASE_MATHFN (BUILT_IN_POWI
)
1700 CASE_MATHFN (BUILT_IN_POW10
)
1701 CASE_MATHFN (BUILT_IN_REMAINDER
)
1702 CASE_MATHFN (BUILT_IN_REMQUO
)
1703 CASE_MATHFN (BUILT_IN_RINT
)
1704 CASE_MATHFN (BUILT_IN_ROUND
)
1705 CASE_MATHFN (BUILT_IN_SCALB
)
1706 CASE_MATHFN (BUILT_IN_SCALBLN
)
1707 CASE_MATHFN (BUILT_IN_SCALBN
)
1708 CASE_MATHFN (BUILT_IN_SIGNIFICAND
)
1709 CASE_MATHFN (BUILT_IN_SIN
)
1710 CASE_MATHFN (BUILT_IN_SINCOS
)
1711 CASE_MATHFN (BUILT_IN_SINH
)
1712 CASE_MATHFN (BUILT_IN_SQRT
)
1713 CASE_MATHFN (BUILT_IN_TAN
)
1714 CASE_MATHFN (BUILT_IN_TANH
)
1715 CASE_MATHFN (BUILT_IN_TGAMMA
)
1716 CASE_MATHFN (BUILT_IN_TRUNC
)
1717 CASE_MATHFN (BUILT_IN_Y0
)
1718 CASE_MATHFN (BUILT_IN_Y1
)
1719 CASE_MATHFN (BUILT_IN_YN
)
1725 if (TYPE_MAIN_VARIANT (type
) == double_type_node
)
1726 return implicit_built_in_decls
[fcode
];
1727 else if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1728 return implicit_built_in_decls
[fcodef
];
1729 else if (TYPE_MAIN_VARIANT (type
) == long_double_type_node
)
1730 return implicit_built_in_decls
[fcodel
];
1735 /* If errno must be maintained, expand the RTL to check if the result,
1736 TARGET, of a built-in function call, EXP, is NaN, and if so set
1740 expand_errno_check (tree exp
, rtx target
)
1742 rtx lab
= gen_label_rtx ();
1744 /* Test the result; if it is NaN, set errno=EDOM because
1745 the argument was not in the domain. */
1746 emit_cmp_and_jump_insns (target
, target
, EQ
, 0, GET_MODE (target
),
1750 /* If this built-in doesn't throw an exception, set errno directly. */
1751 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp
, 0), 0)))
1753 #ifdef GEN_ERRNO_RTX
1754 rtx errno_rtx
= GEN_ERRNO_RTX
;
1757 = gen_rtx_MEM (word_mode
, gen_rtx_SYMBOL_REF (Pmode
, "errno"));
1759 emit_move_insn (errno_rtx
, GEN_INT (TARGET_EDOM
));
1765 /* We can't set errno=EDOM directly; let the library call do it.
1766 Pop the arguments right away in case the call gets deleted. */
1768 expand_call (exp
, target
, 0);
1774 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1775 Return 0 if a normal call should be emitted rather than expanding the
1776 function in-line. EXP is the expression that is a call to the builtin
1777 function; if convenient, the result should be placed in TARGET.
1778 SUBTARGET may be used as the target for computing one of EXP's operands. */
1781 expand_builtin_mathfn (tree exp
, rtx target
, rtx subtarget
)
1783 optab builtin_optab
;
1784 rtx op0
, insns
, before_call
;
1785 tree fndecl
= get_callee_fndecl (exp
);
1786 tree arglist
= TREE_OPERAND (exp
, 1);
1787 enum machine_mode mode
;
1788 bool errno_set
= false;
1791 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
1794 arg
= TREE_VALUE (arglist
);
1796 switch (DECL_FUNCTION_CODE (fndecl
))
1798 CASE_FLT_FN (BUILT_IN_SQRT
):
1799 errno_set
= ! tree_expr_nonnegative_p (arg
);
1800 builtin_optab
= sqrt_optab
;
1802 CASE_FLT_FN (BUILT_IN_EXP
):
1803 errno_set
= true; builtin_optab
= exp_optab
; break;
1804 CASE_FLT_FN (BUILT_IN_EXP10
):
1805 CASE_FLT_FN (BUILT_IN_POW10
):
1806 errno_set
= true; builtin_optab
= exp10_optab
; break;
1807 CASE_FLT_FN (BUILT_IN_EXP2
):
1808 errno_set
= true; builtin_optab
= exp2_optab
; break;
1809 CASE_FLT_FN (BUILT_IN_EXPM1
):
1810 errno_set
= true; builtin_optab
= expm1_optab
; break;
1811 CASE_FLT_FN (BUILT_IN_LOGB
):
1812 errno_set
= true; builtin_optab
= logb_optab
; break;
1813 CASE_FLT_FN (BUILT_IN_ILOGB
):
1814 errno_set
= true; builtin_optab
= ilogb_optab
; break;
1815 CASE_FLT_FN (BUILT_IN_LOG
):
1816 errno_set
= true; builtin_optab
= log_optab
; break;
1817 CASE_FLT_FN (BUILT_IN_LOG10
):
1818 errno_set
= true; builtin_optab
= log10_optab
; break;
1819 CASE_FLT_FN (BUILT_IN_LOG2
):
1820 errno_set
= true; builtin_optab
= log2_optab
; break;
1821 CASE_FLT_FN (BUILT_IN_LOG1P
):
1822 errno_set
= true; builtin_optab
= log1p_optab
; break;
1823 CASE_FLT_FN (BUILT_IN_ASIN
):
1824 builtin_optab
= asin_optab
; break;
1825 CASE_FLT_FN (BUILT_IN_ACOS
):
1826 builtin_optab
= acos_optab
; break;
1827 CASE_FLT_FN (BUILT_IN_TAN
):
1828 builtin_optab
= tan_optab
; break;
1829 CASE_FLT_FN (BUILT_IN_ATAN
):
1830 builtin_optab
= atan_optab
; break;
1831 CASE_FLT_FN (BUILT_IN_FLOOR
):
1832 builtin_optab
= floor_optab
; break;
1833 CASE_FLT_FN (BUILT_IN_CEIL
):
1834 builtin_optab
= ceil_optab
; break;
1835 CASE_FLT_FN (BUILT_IN_TRUNC
):
1836 builtin_optab
= btrunc_optab
; break;
1837 CASE_FLT_FN (BUILT_IN_ROUND
):
1838 builtin_optab
= round_optab
; break;
1839 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
1840 builtin_optab
= nearbyint_optab
;
1841 if (flag_trapping_math
)
1843 /* Else fallthrough and expand as rint. */
1844 CASE_FLT_FN (BUILT_IN_RINT
):
1845 builtin_optab
= rint_optab
; break;
1850 /* Make a suitable register to place result in. */
1851 mode
= TYPE_MODE (TREE_TYPE (exp
));
1853 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
1856 /* Before working hard, check whether the instruction is available. */
1857 if (builtin_optab
->handlers
[(int) mode
].insn_code
!= CODE_FOR_nothing
)
1859 target
= gen_reg_rtx (mode
);
1861 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1862 need to expand the argument again. This way, we will not perform
1863 side-effects more the once. */
1864 narg
= builtin_save_expr (arg
);
1868 arglist
= build_tree_list (NULL_TREE
, arg
);
1869 exp
= build_function_call_expr (fndecl
, arglist
);
1872 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
1876 /* Compute into TARGET.
1877 Set TARGET to wherever the result comes back. */
1878 target
= expand_unop (mode
, builtin_optab
, op0
, target
, 0);
1883 expand_errno_check (exp
, target
);
1885 /* Output the entire sequence. */
1886 insns
= get_insns ();
1892 /* If we were unable to expand via the builtin, stop the sequence
1893 (without outputting the insns) and call to the library function
1894 with the stabilized argument list. */
1898 before_call
= get_last_insn ();
1900 target
= expand_call (exp
, target
, target
== const0_rtx
);
1902 /* If this is a sqrt operation and we don't care about errno, try to
1903 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1904 This allows the semantics of the libcall to be visible to the RTL
1906 if (builtin_optab
== sqrt_optab
&& !errno_set
)
1908 /* Search backwards through the insns emitted by expand_call looking
1909 for the instruction with the REG_RETVAL note. */
1910 rtx last
= get_last_insn ();
1911 while (last
!= before_call
)
1913 if (find_reg_note (last
, REG_RETVAL
, NULL
))
1915 rtx note
= find_reg_note (last
, REG_EQUAL
, NULL
);
1916 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1917 two elements, i.e. symbol_ref(sqrt) and the operand. */
1919 && GET_CODE (note
) == EXPR_LIST
1920 && GET_CODE (XEXP (note
, 0)) == EXPR_LIST
1921 && XEXP (XEXP (note
, 0), 1) != NULL_RTX
1922 && XEXP (XEXP (XEXP (note
, 0), 1), 1) == NULL_RTX
)
1924 rtx operand
= XEXP (XEXP (XEXP (note
, 0), 1), 0);
1925 /* Check operand is a register with expected mode. */
1928 && GET_MODE (operand
) == mode
)
1930 /* Replace the REG_EQUAL note with a SQRT rtx. */
1931 rtx equiv
= gen_rtx_SQRT (mode
, operand
);
1932 set_unique_reg_note (last
, REG_EQUAL
, equiv
);
1937 last
= PREV_INSN (last
);
1944 /* Expand a call to the builtin binary math functions (pow and atan2).
1945 Return 0 if a normal call should be emitted rather than expanding the
1946 function in-line. EXP is the expression that is a call to the builtin
1947 function; if convenient, the result should be placed in TARGET.
1948 SUBTARGET may be used as the target for computing one of EXP's
1952 expand_builtin_mathfn_2 (tree exp
, rtx target
, rtx subtarget
)
1954 optab builtin_optab
;
1955 rtx op0
, op1
, insns
;
1956 int op1_type
= REAL_TYPE
;
1957 tree fndecl
= get_callee_fndecl (exp
);
1958 tree arglist
= TREE_OPERAND (exp
, 1);
1959 tree arg0
, arg1
, temp
, narg
;
1960 enum machine_mode mode
;
1961 bool errno_set
= true;
1964 if ((DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_LDEXP
)
1965 || (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_LDEXPF
)
1966 || (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_LDEXPL
))
1967 op1_type
= INTEGER_TYPE
;
1969 if (!validate_arglist (arglist
, REAL_TYPE
, op1_type
, VOID_TYPE
))
1972 arg0
= TREE_VALUE (arglist
);
1973 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
1975 switch (DECL_FUNCTION_CODE (fndecl
))
1977 CASE_FLT_FN (BUILT_IN_POW
):
1978 builtin_optab
= pow_optab
; break;
1979 CASE_FLT_FN (BUILT_IN_ATAN2
):
1980 builtin_optab
= atan2_optab
; break;
1981 CASE_FLT_FN (BUILT_IN_LDEXP
):
1982 builtin_optab
= ldexp_optab
; break;
1983 CASE_FLT_FN (BUILT_IN_FMOD
):
1984 builtin_optab
= fmod_optab
; break;
1985 CASE_FLT_FN (BUILT_IN_REMAINDER
):
1986 CASE_FLT_FN (BUILT_IN_DREM
):
1987 builtin_optab
= remainder_optab
; break;
1992 /* Make a suitable register to place result in. */
1993 mode
= TYPE_MODE (TREE_TYPE (exp
));
1995 /* Before working hard, check whether the instruction is available. */
1996 if (builtin_optab
->handlers
[(int) mode
].insn_code
== CODE_FOR_nothing
)
1999 target
= gen_reg_rtx (mode
);
2001 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2004 /* Always stabilize the argument list. */
2005 narg
= builtin_save_expr (arg1
);
2009 temp
= build_tree_list (NULL_TREE
, narg
);
2013 temp
= TREE_CHAIN (arglist
);
2015 narg
= builtin_save_expr (arg0
);
2019 arglist
= tree_cons (NULL_TREE
, narg
, temp
);
2023 arglist
= tree_cons (NULL_TREE
, arg0
, temp
);
2026 exp
= build_function_call_expr (fndecl
, arglist
);
2028 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2029 op1
= expand_normal (arg1
);
2033 /* Compute into TARGET.
2034 Set TARGET to wherever the result comes back. */
2035 target
= expand_binop (mode
, builtin_optab
, op0
, op1
,
2036 target
, 0, OPTAB_DIRECT
);
2038 /* If we were unable to expand via the builtin, stop the sequence
2039 (without outputting the insns) and call to the library function
2040 with the stabilized argument list. */
2044 return expand_call (exp
, target
, target
== const0_rtx
);
2048 expand_errno_check (exp
, target
);
2050 /* Output the entire sequence. */
2051 insns
= get_insns ();
2058 /* Expand a call to the builtin sin and cos math functions.
2059 Return 0 if a normal call should be emitted rather than expanding the
2060 function in-line. EXP is the expression that is a call to the builtin
2061 function; if convenient, the result should be placed in TARGET.
2062 SUBTARGET may be used as the target for computing one of EXP's
2066 expand_builtin_mathfn_3 (tree exp
, rtx target
, rtx subtarget
)
2068 optab builtin_optab
;
2070 tree fndecl
= get_callee_fndecl (exp
);
2071 tree arglist
= TREE_OPERAND (exp
, 1);
2072 enum machine_mode mode
;
2075 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
2078 arg
= TREE_VALUE (arglist
);
2080 switch (DECL_FUNCTION_CODE (fndecl
))
2082 CASE_FLT_FN (BUILT_IN_SIN
):
2083 CASE_FLT_FN (BUILT_IN_COS
):
2084 builtin_optab
= sincos_optab
; break;
2089 /* Make a suitable register to place result in. */
2090 mode
= TYPE_MODE (TREE_TYPE (exp
));
2092 /* Check if sincos insn is available, otherwise fallback
2093 to sin or cos insn. */
2094 if (builtin_optab
->handlers
[(int) mode
].insn_code
== CODE_FOR_nothing
)
2095 switch (DECL_FUNCTION_CODE (fndecl
))
2097 CASE_FLT_FN (BUILT_IN_SIN
):
2098 builtin_optab
= sin_optab
; break;
2099 CASE_FLT_FN (BUILT_IN_COS
):
2100 builtin_optab
= cos_optab
; break;
2105 /* Before working hard, check whether the instruction is available. */
2106 if (builtin_optab
->handlers
[(int) mode
].insn_code
!= CODE_FOR_nothing
)
2108 target
= gen_reg_rtx (mode
);
2110 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2111 need to expand the argument again. This way, we will not perform
2112 side-effects more the once. */
2113 narg
= save_expr (arg
);
2117 arglist
= build_tree_list (NULL_TREE
, arg
);
2118 exp
= build_function_call_expr (fndecl
, arglist
);
2121 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
2125 /* Compute into TARGET.
2126 Set TARGET to wherever the result comes back. */
2127 if (builtin_optab
== sincos_optab
)
2131 switch (DECL_FUNCTION_CODE (fndecl
))
2133 CASE_FLT_FN (BUILT_IN_SIN
):
2134 result
= expand_twoval_unop (builtin_optab
, op0
, 0, target
, 0);
2136 CASE_FLT_FN (BUILT_IN_COS
):
2137 result
= expand_twoval_unop (builtin_optab
, op0
, target
, 0, 0);
2142 gcc_assert (result
);
2146 target
= expand_unop (mode
, builtin_optab
, op0
, target
, 0);
2151 /* Output the entire sequence. */
2152 insns
= get_insns ();
2158 /* If we were unable to expand via the builtin, stop the sequence
2159 (without outputting the insns) and call to the library function
2160 with the stabilized argument list. */
2164 target
= expand_call (exp
, target
, target
== const0_rtx
);
2169 /* Expand a call to the builtin sincos math function.
2170 Return 0 if a normal call should be emitted rather than expanding the
2171 function in-line. EXP is the expression that is a call to the builtin
2175 expand_builtin_sincos (tree exp
)
2177 rtx op0
, op1
, op2
, target1
, target2
;
2178 tree arglist
= TREE_OPERAND (exp
, 1);
2179 enum machine_mode mode
;
2180 tree arg
, sinp
, cosp
;
2183 if (!validate_arglist (arglist
, REAL_TYPE
,
2184 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2187 arg
= TREE_VALUE (arglist
);
2188 sinp
= TREE_VALUE (TREE_CHAIN (arglist
));
2189 cosp
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
2191 /* Make a suitable register to place result in. */
2192 mode
= TYPE_MODE (TREE_TYPE (arg
));
2194 /* Check if sincos insn is available, otherwise emit the call. */
2195 if (sincos_optab
->handlers
[(int) mode
].insn_code
== CODE_FOR_nothing
)
2198 target1
= gen_reg_rtx (mode
);
2199 target2
= gen_reg_rtx (mode
);
2201 op0
= expand_normal (arg
);
2202 op1
= expand_normal (build_fold_indirect_ref (sinp
));
2203 op2
= expand_normal (build_fold_indirect_ref (cosp
));
2205 /* Compute into target1 and target2.
2206 Set TARGET to wherever the result comes back. */
2207 result
= expand_twoval_unop (sincos_optab
, op0
, target2
, target1
, 0);
2208 gcc_assert (result
);
2210 /* Move target1 and target2 to the memory locations indicated
2212 emit_move_insn (op1
, target1
);
2213 emit_move_insn (op2
, target2
);
2218 /* Expand a call to one of the builtin rounding functions gcc defines
2219 as an extension (lfloor and lceil). As these are gcc extensions we
2220 do not need to worry about setting errno to EDOM.
2221 If expanding via optab fails, lower expression to (int)(floor(x)).
2222 EXP is the expression that is a call to the builtin function;
2223 if convenient, the result should be placed in TARGET. SUBTARGET may
2224 be used as the target for computing one of EXP's operands. */
2227 expand_builtin_int_roundingfn (tree exp
, rtx target
, rtx subtarget
)
2229 convert_optab builtin_optab
;
2230 rtx op0
, insns
, tmp
;
2231 tree fndecl
= get_callee_fndecl (exp
);
2232 tree arglist
= TREE_OPERAND (exp
, 1);
2233 enum built_in_function fallback_fn
;
2234 tree fallback_fndecl
;
2235 enum machine_mode mode
;
2238 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
2241 arg
= TREE_VALUE (arglist
);
2243 switch (DECL_FUNCTION_CODE (fndecl
))
2245 CASE_FLT_FN (BUILT_IN_LCEIL
):
2246 CASE_FLT_FN (BUILT_IN_LLCEIL
):
2247 builtin_optab
= lceil_optab
;
2248 fallback_fn
= BUILT_IN_CEIL
;
2251 CASE_FLT_FN (BUILT_IN_LFLOOR
):
2252 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
2253 builtin_optab
= lfloor_optab
;
2254 fallback_fn
= BUILT_IN_FLOOR
;
2261 /* Make a suitable register to place result in. */
2262 mode
= TYPE_MODE (TREE_TYPE (exp
));
2264 target
= gen_reg_rtx (mode
);
2266 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2267 need to expand the argument again. This way, we will not perform
2268 side-effects more the once. */
2269 narg
= builtin_save_expr (arg
);
2273 arglist
= build_tree_list (NULL_TREE
, arg
);
2274 exp
= build_function_call_expr (fndecl
, arglist
);
2277 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
2281 /* Compute into TARGET. */
2282 if (expand_sfix_optab (target
, op0
, builtin_optab
))
2284 /* Output the entire sequence. */
2285 insns
= get_insns ();
2291 /* If we were unable to expand via the builtin, stop the sequence
2292 (without outputting the insns). */
2295 /* Fall back to floating point rounding optab. */
2296 fallback_fndecl
= mathfn_built_in (TREE_TYPE (arg
), fallback_fn
);
2297 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2298 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2299 gcc_assert (fallback_fndecl
!= NULL_TREE
);
2300 exp
= build_function_call_expr (fallback_fndecl
, arglist
);
2302 tmp
= expand_normal (exp
);
2304 /* Truncate the result of floating point optab to integer
2305 via expand_fix (). */
2306 target
= gen_reg_rtx (mode
);
2307 expand_fix (target
, tmp
, 0);
2312 /* Expand a call to one of the builtin math functions doing integer
2314 Return 0 if a normal call should be emitted rather than expanding the
2315 function in-line. EXP is the expression that is a call to the builtin
2316 function; if convenient, the result should be placed in TARGET.
2317 SUBTARGET may be used as the target for computing one of EXP's operands. */
2320 expand_builtin_int_roundingfn_2 (tree exp
, rtx target
, rtx subtarget
)
2322 convert_optab builtin_optab
;
2324 tree fndecl
= get_callee_fndecl (exp
);
2325 tree arglist
= TREE_OPERAND (exp
, 1);
2326 enum machine_mode mode
;
2329 /* There's no easy way to detect the case we need to set EDOM. */
2330 if (flag_errno_math
)
2333 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
2336 arg
= TREE_VALUE (arglist
);
2338 switch (DECL_FUNCTION_CODE (fndecl
))
2340 CASE_FLT_FN (BUILT_IN_LRINT
):
2341 CASE_FLT_FN (BUILT_IN_LLRINT
):
2342 builtin_optab
= lrint_optab
; break;
2343 CASE_FLT_FN (BUILT_IN_LROUND
):
2344 CASE_FLT_FN (BUILT_IN_LLROUND
):
2345 builtin_optab
= lround_optab
; break;
2350 /* Make a suitable register to place result in. */
2351 mode
= TYPE_MODE (TREE_TYPE (exp
));
2353 target
= gen_reg_rtx (mode
);
2355 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2356 need to expand the argument again. This way, we will not perform
2357 side-effects more the once. */
2358 narg
= builtin_save_expr (arg
);
2362 arglist
= build_tree_list (NULL_TREE
, arg
);
2363 exp
= build_function_call_expr (fndecl
, arglist
);
2366 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
2370 if (expand_sfix_optab (target
, op0
, builtin_optab
))
2372 /* Output the entire sequence. */
2373 insns
= get_insns ();
2379 /* If we were unable to expand via the builtin, stop the sequence
2380 (without outputting the insns) and call to the library function
2381 with the stabilized argument list. */
2384 target
= expand_call (exp
, target
, target
== const0_rtx
);
2389 /* To evaluate powi(x,n), the floating point value x raised to the
2390 constant integer exponent n, we use a hybrid algorithm that
2391 combines the "window method" with look-up tables. For an
2392 introduction to exponentiation algorithms and "addition chains",
2393 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2394 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2395 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2396 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2398 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2399 multiplications to inline before calling the system library's pow
2400 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2401 so this default never requires calling pow, powf or powl. */
2403 #ifndef POWI_MAX_MULTS
2404 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2407 /* The size of the "optimal power tree" lookup table. All
2408 exponents less than this value are simply looked up in the
2409 powi_table below. This threshold is also used to size the
2410 cache of pseudo registers that hold intermediate results. */
2411 #define POWI_TABLE_SIZE 256
2413 /* The size, in bits of the window, used in the "window method"
2414 exponentiation algorithm. This is equivalent to a radix of
2415 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2416 #define POWI_WINDOW_SIZE 3
2418 /* The following table is an efficient representation of an
2419 "optimal power tree". For each value, i, the corresponding
2420 value, j, in the table states than an optimal evaluation
2421 sequence for calculating pow(x,i) can be found by evaluating
2422 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2423 100 integers is given in Knuth's "Seminumerical algorithms". */
2425 static const unsigned char powi_table
[POWI_TABLE_SIZE
] =
2427 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2428 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2429 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2430 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2431 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2432 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2433 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2434 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2435 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2436 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2437 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2438 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2439 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2440 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2441 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2442 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2443 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2444 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2445 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2446 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2447 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2448 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2449 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2450 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2451 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2452 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2453 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2454 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2455 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2456 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2457 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2458 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2462 /* Return the number of multiplications required to calculate
2463 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2464 subroutine of powi_cost. CACHE is an array indicating
2465 which exponents have already been calculated. */
2468 powi_lookup_cost (unsigned HOST_WIDE_INT n
, bool *cache
)
2470 /* If we've already calculated this exponent, then this evaluation
2471 doesn't require any additional multiplications. */
2476 return powi_lookup_cost (n
- powi_table
[n
], cache
)
2477 + powi_lookup_cost (powi_table
[n
], cache
) + 1;
2480 /* Return the number of multiplications required to calculate
2481 powi(x,n) for an arbitrary x, given the exponent N. This
2482 function needs to be kept in sync with expand_powi below. */
2485 powi_cost (HOST_WIDE_INT n
)
2487 bool cache
[POWI_TABLE_SIZE
];
2488 unsigned HOST_WIDE_INT digit
;
2489 unsigned HOST_WIDE_INT val
;
2495 /* Ignore the reciprocal when calculating the cost. */
2496 val
= (n
< 0) ? -n
: n
;
2498 /* Initialize the exponent cache. */
2499 memset (cache
, 0, POWI_TABLE_SIZE
* sizeof (bool));
2504 while (val
>= POWI_TABLE_SIZE
)
2508 digit
= val
& ((1 << POWI_WINDOW_SIZE
) - 1);
2509 result
+= powi_lookup_cost (digit
, cache
)
2510 + POWI_WINDOW_SIZE
+ 1;
2511 val
>>= POWI_WINDOW_SIZE
;
2520 return result
+ powi_lookup_cost (val
, cache
);
2523 /* Recursive subroutine of expand_powi. This function takes the array,
2524 CACHE, of already calculated exponents and an exponent N and returns
2525 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2528 expand_powi_1 (enum machine_mode mode
, unsigned HOST_WIDE_INT n
, rtx
*cache
)
2530 unsigned HOST_WIDE_INT digit
;
2534 if (n
< POWI_TABLE_SIZE
)
2539 target
= gen_reg_rtx (mode
);
2542 op0
= expand_powi_1 (mode
, n
- powi_table
[n
], cache
);
2543 op1
= expand_powi_1 (mode
, powi_table
[n
], cache
);
2547 target
= gen_reg_rtx (mode
);
2548 digit
= n
& ((1 << POWI_WINDOW_SIZE
) - 1);
2549 op0
= expand_powi_1 (mode
, n
- digit
, cache
);
2550 op1
= expand_powi_1 (mode
, digit
, cache
);
2554 target
= gen_reg_rtx (mode
);
2555 op0
= expand_powi_1 (mode
, n
>> 1, cache
);
2559 result
= expand_mult (mode
, op0
, op1
, target
, 0);
2560 if (result
!= target
)
2561 emit_move_insn (target
, result
);
2565 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2566 floating point operand in mode MODE, and N is the exponent. This
2567 function needs to be kept in sync with powi_cost above. */
2570 expand_powi (rtx x
, enum machine_mode mode
, HOST_WIDE_INT n
)
2572 unsigned HOST_WIDE_INT val
;
2573 rtx cache
[POWI_TABLE_SIZE
];
2577 return CONST1_RTX (mode
);
2579 val
= (n
< 0) ? -n
: n
;
2581 memset (cache
, 0, sizeof (cache
));
2584 result
= expand_powi_1 (mode
, (n
< 0) ? -n
: n
, cache
);
2586 /* If the original exponent was negative, reciprocate the result. */
2588 result
= expand_binop (mode
, sdiv_optab
, CONST1_RTX (mode
),
2589 result
, NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
2594 /* Expand a call to the pow built-in mathematical function. Return 0 if
2595 a normal call should be emitted rather than expanding the function
2596 in-line. EXP is the expression that is a call to the builtin
2597 function; if convenient, the result should be placed in TARGET. */
2600 expand_builtin_pow (tree exp
, rtx target
, rtx subtarget
)
2602 tree arg0
, arg1
, fn
, narg0
, narglist
;
2603 tree arglist
= TREE_OPERAND (exp
, 1);
2604 tree type
= TREE_TYPE (exp
);
2605 REAL_VALUE_TYPE cint
, c
, c2
;
2608 enum machine_mode mode
= TYPE_MODE (type
);
2610 if (! validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
2613 arg0
= TREE_VALUE (arglist
);
2614 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
2616 if (TREE_CODE (arg1
) != REAL_CST
2617 || TREE_CONSTANT_OVERFLOW (arg1
))
2618 return expand_builtin_mathfn_2 (exp
, target
, subtarget
);
2620 /* Handle constant exponents. */
2622 /* For integer valued exponents we can expand to an optimal multiplication
2623 sequence using expand_powi. */
2624 c
= TREE_REAL_CST (arg1
);
2625 n
= real_to_integer (&c
);
2626 real_from_integer (&cint
, VOIDmode
, n
, n
< 0 ? -1 : 0, 0);
2627 if (real_identical (&c
, &cint
)
2628 && ((n
>= -1 && n
<= 2)
2629 || (flag_unsafe_math_optimizations
2631 && powi_cost (n
) <= POWI_MAX_MULTS
)))
2633 op
= expand_expr (arg0
, subtarget
, VOIDmode
, 0);
2636 op
= force_reg (mode
, op
);
2637 op
= expand_powi (op
, mode
, n
);
2642 narg0
= builtin_save_expr (arg0
);
2643 narglist
= build_tree_list (NULL_TREE
, narg0
);
2645 /* If the exponent is not integer valued, check if it is half of an integer.
2646 In this case we can expand to sqrt (x) * x**(n/2). */
2647 fn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
2648 if (fn
!= NULL_TREE
)
2650 real_arithmetic (&c2
, MULT_EXPR
, &c
, &dconst2
);
2651 n
= real_to_integer (&c2
);
2652 real_from_integer (&cint
, VOIDmode
, n
, n
< 0 ? -1 : 0, 0);
2653 if (real_identical (&c2
, &cint
)
2654 && ((flag_unsafe_math_optimizations
2656 && powi_cost (n
/2) <= POWI_MAX_MULTS
)
2659 tree call_expr
= build_function_call_expr (fn
, narglist
);
2660 op
= expand_builtin (call_expr
, NULL_RTX
, subtarget
, mode
, 0);
2663 op2
= expand_expr (narg0
, subtarget
, VOIDmode
, 0);
2664 op2
= force_reg (mode
, op2
);
2665 op2
= expand_powi (op2
, mode
, abs (n
/ 2));
2666 op
= expand_simple_binop (mode
, MULT
, op
, op2
, NULL_RTX
,
2667 0, OPTAB_LIB_WIDEN
);
2668 /* If the original exponent was negative, reciprocate the
2671 op
= expand_binop (mode
, sdiv_optab
, CONST1_RTX (mode
),
2672 op
, NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
2678 /* Try if the exponent is a third of an integer. In this case
2679 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
2680 different from pow (x, 1./3.) due to rounding and behavior
2681 with negative x we need to constrain this transformation to
2682 unsafe math and positive x or finite math. */
2683 fn
= mathfn_built_in (type
, BUILT_IN_CBRT
);
2685 && flag_unsafe_math_optimizations
2686 && (tree_expr_nonnegative_p (arg0
)
2687 || !HONOR_NANS (mode
)))
2689 real_arithmetic (&c2
, MULT_EXPR
, &c
, &dconst3
);
2690 real_round (&c2
, mode
, &c2
);
2691 n
= real_to_integer (&c2
);
2692 real_from_integer (&cint
, VOIDmode
, n
, n
< 0 ? -1 : 0, 0);
2693 real_arithmetic (&c2
, RDIV_EXPR
, &cint
, &dconst3
);
2694 real_convert (&c2
, mode
, &c2
);
2695 if (real_identical (&c2
, &c
)
2697 && powi_cost (n
/3) <= POWI_MAX_MULTS
)
2700 tree call_expr
= build_function_call_expr (fn
, narglist
);
2701 op
= expand_builtin (call_expr
, NULL_RTX
, subtarget
, mode
, 0);
2702 if (abs (n
) % 3 == 2)
2703 op
= expand_simple_binop (mode
, MULT
, op
, op
, op
,
2704 0, OPTAB_LIB_WIDEN
);
2707 op2
= expand_expr (narg0
, subtarget
, VOIDmode
, 0);
2708 op2
= force_reg (mode
, op2
);
2709 op2
= expand_powi (op2
, mode
, abs (n
/ 3));
2710 op
= expand_simple_binop (mode
, MULT
, op
, op2
, NULL_RTX
,
2711 0, OPTAB_LIB_WIDEN
);
2712 /* If the original exponent was negative, reciprocate the
2715 op
= expand_binop (mode
, sdiv_optab
, CONST1_RTX (mode
),
2716 op
, NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
2722 /* Fall back to optab expansion. */
2723 return expand_builtin_mathfn_2 (exp
, target
, subtarget
);
2726 /* Expand a call to the powi built-in mathematical function. Return 0 if
2727 a normal call should be emitted rather than expanding the function
2728 in-line. EXP is the expression that is a call to the builtin
2729 function; if convenient, the result should be placed in TARGET. */
2732 expand_builtin_powi (tree exp
, rtx target
, rtx subtarget
)
2734 tree arglist
= TREE_OPERAND (exp
, 1);
2737 enum machine_mode mode
;
2738 enum machine_mode mode2
;
2740 if (! validate_arglist (arglist
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2743 arg0
= TREE_VALUE (arglist
);
2744 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
2745 mode
= TYPE_MODE (TREE_TYPE (exp
));
2747 /* Handle constant power. */
2749 if (TREE_CODE (arg1
) == INTEGER_CST
2750 && ! TREE_CONSTANT_OVERFLOW (arg1
))
2752 HOST_WIDE_INT n
= TREE_INT_CST_LOW (arg1
);
2754 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2755 Otherwise, check the number of multiplications required. */
2756 if ((TREE_INT_CST_HIGH (arg1
) == 0
2757 || TREE_INT_CST_HIGH (arg1
) == -1)
2758 && ((n
>= -1 && n
<= 2)
2760 && powi_cost (n
) <= POWI_MAX_MULTS
)))
2762 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, 0);
2763 op0
= force_reg (mode
, op0
);
2764 return expand_powi (op0
, mode
, n
);
2768 /* Emit a libcall to libgcc. */
2770 /* Mode of the 2nd argument must match that of an int. */
2771 mode2
= mode_for_size (INT_TYPE_SIZE
, MODE_INT
, 0);
2773 if (target
== NULL_RTX
)
2774 target
= gen_reg_rtx (mode
);
2776 op0
= expand_expr (arg0
, subtarget
, mode
, 0);
2777 if (GET_MODE (op0
) != mode
)
2778 op0
= convert_to_mode (mode
, op0
, 0);
2779 op1
= expand_expr (arg1
, 0, mode2
, 0);
2780 if (GET_MODE (op1
) != mode2
)
2781 op1
= convert_to_mode (mode2
, op1
, 0);
2783 target
= emit_library_call_value (powi_optab
->handlers
[(int) mode
].libfunc
,
2784 target
, LCT_CONST_MAKE_BLOCK
, mode
, 2,
2785 op0
, mode
, op1
, mode2
);
2790 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2791 if we failed the caller should emit a normal call, otherwise
2792 try to get the result in TARGET, if convenient. */
2795 expand_builtin_strlen (tree arglist
, rtx target
,
2796 enum machine_mode target_mode
)
2798 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
2803 tree len
, src
= TREE_VALUE (arglist
);
2804 rtx result
, src_reg
, char_rtx
, before_strlen
;
2805 enum machine_mode insn_mode
= target_mode
, char_mode
;
2806 enum insn_code icode
= CODE_FOR_nothing
;
2809 /* If the length can be computed at compile-time, return it. */
2810 len
= c_strlen (src
, 0);
2812 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2814 /* If the length can be computed at compile-time and is constant
2815 integer, but there are side-effects in src, evaluate
2816 src for side-effects, then return len.
2817 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2818 can be optimized into: i++; x = 3; */
2819 len
= c_strlen (src
, 1);
2820 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
2822 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
2823 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2826 align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
2828 /* If SRC is not a pointer type, don't do this operation inline. */
2832 /* Bail out if we can't compute strlen in the right mode. */
2833 while (insn_mode
!= VOIDmode
)
2835 icode
= strlen_optab
->handlers
[(int) insn_mode
].insn_code
;
2836 if (icode
!= CODE_FOR_nothing
)
2839 insn_mode
= GET_MODE_WIDER_MODE (insn_mode
);
2841 if (insn_mode
== VOIDmode
)
2844 /* Make a place to write the result of the instruction. */
2848 && GET_MODE (result
) == insn_mode
2849 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
2850 result
= gen_reg_rtx (insn_mode
);
2852 /* Make a place to hold the source address. We will not expand
2853 the actual source until we are sure that the expansion will
2854 not fail -- there are trees that cannot be expanded twice. */
2855 src_reg
= gen_reg_rtx (Pmode
);
2857 /* Mark the beginning of the strlen sequence so we can emit the
2858 source operand later. */
2859 before_strlen
= get_last_insn ();
2861 char_rtx
= const0_rtx
;
2862 char_mode
= insn_data
[(int) icode
].operand
[2].mode
;
2863 if (! (*insn_data
[(int) icode
].operand
[2].predicate
) (char_rtx
,
2865 char_rtx
= copy_to_mode_reg (char_mode
, char_rtx
);
2867 pat
= GEN_FCN (icode
) (result
, gen_rtx_MEM (BLKmode
, src_reg
),
2868 char_rtx
, GEN_INT (align
));
2873 /* Now that we are assured of success, expand the source. */
2875 pat
= expand_expr (src
, src_reg
, ptr_mode
, EXPAND_NORMAL
);
2877 emit_move_insn (src_reg
, pat
);
2882 emit_insn_after (pat
, before_strlen
);
2884 emit_insn_before (pat
, get_insns ());
2886 /* Return the value in the proper mode for this function. */
2887 if (GET_MODE (result
) == target_mode
)
2889 else if (target
!= 0)
2890 convert_move (target
, result
, 0);
2892 target
= convert_to_mode (target_mode
, result
, 0);
2898 /* Expand a call to the strstr builtin. Return 0 if we failed the
2899 caller should emit a normal call, otherwise try to get the result
2900 in TARGET, if convenient (and in mode MODE if that's convenient). */
2903 expand_builtin_strstr (tree arglist
, tree type
, rtx target
, enum machine_mode mode
)
2905 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2907 tree result
= fold_builtin_strstr (arglist
, type
);
2909 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2914 /* Expand a call to the strchr builtin. Return 0 if we failed the
2915 caller should emit a normal call, otherwise try to get the result
2916 in TARGET, if convenient (and in mode MODE if that's convenient). */
2919 expand_builtin_strchr (tree arglist
, tree type
, rtx target
, enum machine_mode mode
)
2921 if (validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2923 tree result
= fold_builtin_strchr (arglist
, type
);
2925 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2927 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2932 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2933 caller should emit a normal call, otherwise try to get the result
2934 in TARGET, if convenient (and in mode MODE if that's convenient). */
2937 expand_builtin_strrchr (tree arglist
, tree type
, rtx target
, enum machine_mode mode
)
2939 if (validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2941 tree result
= fold_builtin_strrchr (arglist
, type
);
2943 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2948 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2949 caller should emit a normal call, otherwise try to get the result
2950 in TARGET, if convenient (and in mode MODE if that's convenient). */
2953 expand_builtin_strpbrk (tree arglist
, tree type
, rtx target
, enum machine_mode mode
)
2955 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2957 tree result
= fold_builtin_strpbrk (arglist
, type
);
2959 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
2964 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2965 bytes from constant string DATA + OFFSET and return it as target
2969 builtin_memcpy_read_str (void *data
, HOST_WIDE_INT offset
,
2970 enum machine_mode mode
)
2972 const char *str
= (const char *) data
;
2974 gcc_assert (offset
>= 0
2975 && ((unsigned HOST_WIDE_INT
) offset
+ GET_MODE_SIZE (mode
)
2976 <= strlen (str
) + 1));
2978 return c_readstr (str
+ offset
, mode
);
2981 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2982 Return 0 if we failed, the caller should emit a normal call,
2983 otherwise try to get the result in TARGET, if convenient (and in
2984 mode MODE if that's convenient). */
2986 expand_builtin_memcpy (tree exp
, rtx target
, enum machine_mode mode
)
2988 tree fndecl
= get_callee_fndecl (exp
);
2989 tree arglist
= TREE_OPERAND (exp
, 1);
2990 if (!validate_arglist (arglist
,
2991 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2995 tree dest
= TREE_VALUE (arglist
);
2996 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
2997 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
2998 const char *src_str
;
2999 unsigned int src_align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
3000 unsigned int dest_align
3001 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
3002 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
3003 tree result
= fold_builtin_memory_op (arglist
, TREE_TYPE (TREE_TYPE (fndecl
)),
3008 while (TREE_CODE (result
) == COMPOUND_EXPR
)
3010 expand_expr (TREE_OPERAND (result
, 0), const0_rtx
, VOIDmode
,
3012 result
= TREE_OPERAND (result
, 1);
3014 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3017 /* If DEST is not a pointer type, call the normal function. */
3018 if (dest_align
== 0)
3021 /* If either SRC is not a pointer type, don't do this
3022 operation in-line. */
3026 dest_mem
= get_memory_rtx (dest
, len
);
3027 set_mem_align (dest_mem
, dest_align
);
3028 len_rtx
= expand_normal (len
);
3029 src_str
= c_getstr (src
);
3031 /* If SRC is a string constant and block move would be done
3032 by pieces, we can avoid loading the string from memory
3033 and only stored the computed constants. */
3035 && GET_CODE (len_rtx
) == CONST_INT
3036 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3037 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3038 (void *) src_str
, dest_align
))
3040 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3041 builtin_memcpy_read_str
,
3042 (void *) src_str
, dest_align
, 0);
3043 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3044 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3048 src_mem
= get_memory_rtx (src
, len
);
3049 set_mem_align (src_mem
, src_align
);
3051 /* Copy word part most expediently. */
3052 dest_addr
= emit_block_move (dest_mem
, src_mem
, len_rtx
,
3053 CALL_EXPR_TAILCALL (exp
)
3054 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
);
3058 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3059 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3065 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
3066 Return 0 if we failed; the caller should emit a normal call,
3067 otherwise try to get the result in TARGET, if convenient (and in
3068 mode MODE if that's convenient). If ENDP is 0 return the
3069 destination pointer, if ENDP is 1 return the end pointer ala
3070 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3074 expand_builtin_mempcpy (tree arglist
, tree type
, rtx target
, enum machine_mode mode
,
3077 if (!validate_arglist (arglist
,
3078 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3080 /* If return value is ignored, transform mempcpy into memcpy. */
3081 else if (target
== const0_rtx
)
3083 tree fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
3088 return expand_expr (build_function_call_expr (fn
, arglist
),
3089 target
, mode
, EXPAND_NORMAL
);
3093 tree dest
= TREE_VALUE (arglist
);
3094 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
3095 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3096 const char *src_str
;
3097 unsigned int src_align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
3098 unsigned int dest_align
3099 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
3100 rtx dest_mem
, src_mem
, len_rtx
;
3101 tree result
= fold_builtin_memory_op (arglist
, type
, false, endp
);
3105 while (TREE_CODE (result
) == COMPOUND_EXPR
)
3107 expand_expr (TREE_OPERAND (result
, 0), const0_rtx
, VOIDmode
,
3109 result
= TREE_OPERAND (result
, 1);
3111 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3114 /* If either SRC or DEST is not a pointer type, don't do this
3115 operation in-line. */
3116 if (dest_align
== 0 || src_align
== 0)
3119 /* If LEN is not constant, call the normal function. */
3120 if (! host_integerp (len
, 1))
3123 len_rtx
= expand_normal (len
);
3124 src_str
= c_getstr (src
);
3126 /* If SRC is a string constant and block move would be done
3127 by pieces, we can avoid loading the string from memory
3128 and only stored the computed constants. */
3130 && GET_CODE (len_rtx
) == CONST_INT
3131 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3132 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3133 (void *) src_str
, dest_align
))
3135 dest_mem
= get_memory_rtx (dest
, len
);
3136 set_mem_align (dest_mem
, dest_align
);
3137 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3138 builtin_memcpy_read_str
,
3139 (void *) src_str
, dest_align
, endp
);
3140 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3141 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3145 if (GET_CODE (len_rtx
) == CONST_INT
3146 && can_move_by_pieces (INTVAL (len_rtx
),
3147 MIN (dest_align
, src_align
)))
3149 dest_mem
= get_memory_rtx (dest
, len
);
3150 set_mem_align (dest_mem
, dest_align
);
3151 src_mem
= get_memory_rtx (src
, len
);
3152 set_mem_align (src_mem
, src_align
);
3153 dest_mem
= move_by_pieces (dest_mem
, src_mem
, INTVAL (len_rtx
),
3154 MIN (dest_align
, src_align
), endp
);
3155 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3156 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3164 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
3165 if we failed; the caller should emit a normal call. */
3168 expand_builtin_memmove (tree arglist
, tree type
, rtx target
,
3169 enum machine_mode mode
)
3171 if (!validate_arglist (arglist
,
3172 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3176 tree result
= fold_builtin_memory_op (arglist
, type
, false, /*endp=*/3);
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 /* Otherwise, call the normal function. */
3194 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
3195 if we failed the caller should emit a normal call. */
3198 expand_builtin_bcopy (tree exp
)
3200 tree arglist
= TREE_OPERAND (exp
, 1);
3201 tree type
= TREE_TYPE (exp
);
3202 tree src
, dest
, size
, newarglist
;
3204 if (!validate_arglist (arglist
,
3205 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3208 src
= TREE_VALUE (arglist
);
3209 dest
= TREE_VALUE (TREE_CHAIN (arglist
));
3210 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3212 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3213 memmove(ptr y, ptr x, size_t z). This is done this way
3214 so that if it isn't expanded inline, we fallback to
3215 calling bcopy instead of memmove. */
3217 newarglist
= build_tree_list (NULL_TREE
, fold_convert (sizetype
, size
));
3218 newarglist
= tree_cons (NULL_TREE
, src
, newarglist
);
3219 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
3221 return expand_builtin_memmove (newarglist
, type
, const0_rtx
, VOIDmode
);
3225 # define HAVE_movstr 0
3226 # define CODE_FOR_movstr CODE_FOR_nothing
3229 /* Expand into a movstr instruction, if one is available. Return 0 if
3230 we failed, the caller should emit a normal call, otherwise try to
3231 get the result in TARGET, if convenient. If ENDP is 0 return the
3232 destination pointer, if ENDP is 1 return the end pointer ala
3233 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3237 expand_movstr (tree dest
, tree src
, rtx target
, int endp
)
3243 const struct insn_data
* data
;
3248 dest_mem
= get_memory_rtx (dest
, NULL
);
3249 src_mem
= get_memory_rtx (src
, NULL
);
3252 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
3253 dest_mem
= replace_equiv_address (dest_mem
, target
);
3254 end
= gen_reg_rtx (Pmode
);
3258 if (target
== 0 || target
== const0_rtx
)
3260 end
= gen_reg_rtx (Pmode
);
3268 data
= insn_data
+ CODE_FOR_movstr
;
3270 if (data
->operand
[0].mode
!= VOIDmode
)
3271 end
= gen_lowpart (data
->operand
[0].mode
, end
);
3273 insn
= data
->genfun (end
, dest_mem
, src_mem
);
3279 /* movstr is supposed to set end to the address of the NUL
3280 terminator. If the caller requested a mempcpy-like return value,
3282 if (endp
== 1 && target
!= const0_rtx
)
3284 rtx tem
= plus_constant (gen_lowpart (GET_MODE (target
), end
), 1);
3285 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
3291 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3292 if we failed the caller should emit a normal call, otherwise try to get
3293 the result in TARGET, if convenient (and in mode MODE if that's
3297 expand_builtin_strcpy (tree fndecl
, tree arglist
, rtx target
, enum machine_mode mode
)
3299 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3301 tree result
= fold_builtin_strcpy (fndecl
, arglist
, 0);
3304 while (TREE_CODE (result
) == COMPOUND_EXPR
)
3306 expand_expr (TREE_OPERAND (result
, 0), const0_rtx
, VOIDmode
,
3308 result
= TREE_OPERAND (result
, 1);
3310 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3313 return expand_movstr (TREE_VALUE (arglist
),
3314 TREE_VALUE (TREE_CHAIN (arglist
)),
3315 target
, /*endp=*/0);
3320 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3321 Return 0 if we failed the caller should emit a normal call,
3322 otherwise try to get the result in TARGET, if convenient (and in
3323 mode MODE if that's convenient). */
3326 expand_builtin_stpcpy (tree exp
, rtx target
, enum machine_mode mode
)
3328 tree arglist
= TREE_OPERAND (exp
, 1);
3329 /* If return value is ignored, transform stpcpy into strcpy. */
3330 if (target
== const0_rtx
)
3332 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
3336 return expand_expr (build_function_call_expr (fn
, arglist
),
3337 target
, mode
, EXPAND_NORMAL
);
3340 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3344 tree dst
, src
, len
, lenp1
;
3348 /* Ensure we get an actual string whose length can be evaluated at
3349 compile-time, not an expression containing a string. This is
3350 because the latter will potentially produce pessimized code
3351 when used to produce the return value. */
3352 src
= TREE_VALUE (TREE_CHAIN (arglist
));
3353 if (! c_getstr (src
) || ! (len
= c_strlen (src
, 0)))
3354 return expand_movstr (TREE_VALUE (arglist
),
3355 TREE_VALUE (TREE_CHAIN (arglist
)),
3356 target
, /*endp=*/2);
3358 dst
= TREE_VALUE (arglist
);
3359 lenp1
= size_binop (PLUS_EXPR
, len
, ssize_int (1));
3360 narglist
= build_tree_list (NULL_TREE
, lenp1
);
3361 narglist
= tree_cons (NULL_TREE
, src
, narglist
);
3362 narglist
= tree_cons (NULL_TREE
, dst
, narglist
);
3363 ret
= expand_builtin_mempcpy (narglist
, TREE_TYPE (exp
),
3364 target
, mode
, /*endp=*/2);
3369 if (TREE_CODE (len
) == INTEGER_CST
)
3371 rtx len_rtx
= expand_normal (len
);
3373 if (GET_CODE (len_rtx
) == CONST_INT
)
3375 ret
= expand_builtin_strcpy (get_callee_fndecl (exp
),
3376 arglist
, target
, mode
);
3382 if (mode
!= VOIDmode
)
3383 target
= gen_reg_rtx (mode
);
3385 target
= gen_reg_rtx (GET_MODE (ret
));
3387 if (GET_MODE (target
) != GET_MODE (ret
))
3388 ret
= gen_lowpart (GET_MODE (target
), ret
);
3390 ret
= plus_constant (ret
, INTVAL (len_rtx
));
3391 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
3399 return expand_movstr (TREE_VALUE (arglist
),
3400 TREE_VALUE (TREE_CHAIN (arglist
)),
3401 target
, /*endp=*/2);
3405 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3406 bytes from constant string DATA + OFFSET and return it as target
3410 builtin_strncpy_read_str (void *data
, HOST_WIDE_INT offset
,
3411 enum machine_mode mode
)
3413 const char *str
= (const char *) data
;
3415 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
3418 return c_readstr (str
+ offset
, mode
);
3421 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3422 if we failed the caller should emit a normal call. */
3425 expand_builtin_strncpy (tree exp
, rtx target
, enum machine_mode mode
)
3427 tree fndecl
= get_callee_fndecl (exp
);
3428 tree arglist
= TREE_OPERAND (exp
, 1);
3429 if (validate_arglist (arglist
,
3430 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3432 tree slen
= c_strlen (TREE_VALUE (TREE_CHAIN (arglist
)), 1);
3433 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3434 tree result
= fold_builtin_strncpy (fndecl
, arglist
, slen
);
3438 while (TREE_CODE (result
) == COMPOUND_EXPR
)
3440 expand_expr (TREE_OPERAND (result
, 0), const0_rtx
, VOIDmode
,
3442 result
= TREE_OPERAND (result
, 1);
3444 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3447 /* We must be passed a constant len and src parameter. */
3448 if (!host_integerp (len
, 1) || !slen
|| !host_integerp (slen
, 1))
3451 slen
= size_binop (PLUS_EXPR
, slen
, ssize_int (1));
3453 /* We're required to pad with trailing zeros if the requested
3454 len is greater than strlen(s2)+1. In that case try to
3455 use store_by_pieces, if it fails, punt. */
3456 if (tree_int_cst_lt (slen
, len
))
3458 tree dest
= TREE_VALUE (arglist
);
3459 unsigned int dest_align
3460 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
3461 const char *p
= c_getstr (TREE_VALUE (TREE_CHAIN (arglist
)));
3464 if (!p
|| dest_align
== 0 || !host_integerp (len
, 1)
3465 || !can_store_by_pieces (tree_low_cst (len
, 1),
3466 builtin_strncpy_read_str
,
3467 (void *) p
, dest_align
))
3470 dest_mem
= get_memory_rtx (dest
, len
);
3471 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3472 builtin_strncpy_read_str
,
3473 (void *) p
, dest_align
, 0);
3474 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3475 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3482 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3483 bytes from constant string DATA + OFFSET and return it as target
3487 builtin_memset_read_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3488 enum machine_mode mode
)
3490 const char *c
= (const char *) data
;
3491 char *p
= alloca (GET_MODE_SIZE (mode
));
3493 memset (p
, *c
, GET_MODE_SIZE (mode
));
3495 return c_readstr (p
, mode
);
3498 /* Callback routine for store_by_pieces. Return the RTL of a register
3499 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3500 char value given in the RTL register data. For example, if mode is
3501 4 bytes wide, return the RTL for 0x01010101*data. */
3504 builtin_memset_gen_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3505 enum machine_mode mode
)
3511 size
= GET_MODE_SIZE (mode
);
3516 memset (p
, 1, size
);
3517 coeff
= c_readstr (p
, mode
);
3519 target
= convert_to_mode (mode
, (rtx
) data
, 1);
3520 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
3521 return force_reg (mode
, target
);
3524 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3525 if we failed the caller should emit a normal call, otherwise try to get
3526 the result in TARGET, if convenient (and in mode MODE if that's
3530 expand_builtin_memset (tree arglist
, rtx target
, enum machine_mode mode
,
3533 if (!validate_arglist (arglist
,
3534 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3538 tree dest
= TREE_VALUE (arglist
);
3539 tree val
= TREE_VALUE (TREE_CHAIN (arglist
));
3540 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3542 enum built_in_function fcode
;
3544 unsigned int dest_align
;
3545 rtx dest_mem
, dest_addr
, len_rtx
;
3547 dest_align
= get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
3549 /* If DEST is not a pointer type, don't do this
3550 operation in-line. */
3551 if (dest_align
== 0)
3554 /* If the LEN parameter is zero, return DEST. */
3555 if (integer_zerop (len
))
3557 /* Evaluate and ignore VAL in case it has side-effects. */
3558 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3559 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
3562 /* Stabilize the arguments in case we fail. */
3563 dest
= builtin_save_expr (dest
);
3564 val
= builtin_save_expr (val
);
3565 len
= builtin_save_expr (len
);
3567 len_rtx
= expand_normal (len
);
3568 dest_mem
= get_memory_rtx (dest
, len
);
3570 if (TREE_CODE (val
) != INTEGER_CST
)
3574 val_rtx
= expand_normal (val
);
3575 val_rtx
= convert_to_mode (TYPE_MODE (unsigned_char_type_node
),
3578 /* Assume that we can memset by pieces if we can store the
3579 * the coefficients by pieces (in the required modes).
3580 * We can't pass builtin_memset_gen_str as that emits RTL. */
3582 if (host_integerp (len
, 1)
3583 && !(optimize_size
&& tree_low_cst (len
, 1) > 1)
3584 && can_store_by_pieces (tree_low_cst (len
, 1),
3585 builtin_memset_read_str
, &c
, dest_align
))
3587 val_rtx
= force_reg (TYPE_MODE (unsigned_char_type_node
),
3589 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3590 builtin_memset_gen_str
, val_rtx
, dest_align
, 0);
3592 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, val_rtx
,
3596 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3597 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3601 if (target_char_cast (val
, &c
))
3606 if (host_integerp (len
, 1)
3607 && !(optimize_size
&& tree_low_cst (len
, 1) > 1)
3608 && can_store_by_pieces (tree_low_cst (len
, 1),
3609 builtin_memset_read_str
, &c
, dest_align
))
3610 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3611 builtin_memset_read_str
, &c
, dest_align
, 0);
3612 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, GEN_INT (c
),
3616 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3617 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3621 set_mem_align (dest_mem
, dest_align
);
3622 dest_addr
= clear_storage (dest_mem
, len_rtx
,
3623 CALL_EXPR_TAILCALL (orig_exp
)
3624 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
);
3628 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3629 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3635 fndecl
= get_callee_fndecl (orig_exp
);
3636 fcode
= DECL_FUNCTION_CODE (fndecl
);
3637 gcc_assert (fcode
== BUILT_IN_MEMSET
|| fcode
== BUILT_IN_BZERO
);
3638 arglist
= build_tree_list (NULL_TREE
, len
);
3639 if (fcode
== BUILT_IN_MEMSET
)
3640 arglist
= tree_cons (NULL_TREE
, val
, arglist
);
3641 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
3642 fn
= build_function_call_expr (fndecl
, arglist
);
3643 if (TREE_CODE (fn
) == CALL_EXPR
)
3644 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
3645 return expand_call (fn
, target
, target
== const0_rtx
);
3649 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3650 if we failed the caller should emit a normal call. */
3653 expand_builtin_bzero (tree exp
)
3655 tree arglist
= TREE_OPERAND (exp
, 1);
3656 tree dest
, size
, newarglist
;
3658 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3661 dest
= TREE_VALUE (arglist
);
3662 size
= TREE_VALUE (TREE_CHAIN (arglist
));
3664 /* New argument list transforming bzero(ptr x, int y) to
3665 memset(ptr x, int 0, size_t y). This is done this way
3666 so that if it isn't expanded inline, we fallback to
3667 calling bzero instead of memset. */
3669 newarglist
= build_tree_list (NULL_TREE
, fold_convert (sizetype
, size
));
3670 newarglist
= tree_cons (NULL_TREE
, integer_zero_node
, newarglist
);
3671 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
3673 return expand_builtin_memset (newarglist
, const0_rtx
, VOIDmode
, exp
);
3676 /* Expand expression EXP, which is a call to the memcmp built-in function.
3677 ARGLIST is the argument list for this call. Return 0 if we failed and the
3678 caller should emit a normal call, otherwise try to get the result in
3679 TARGET, if convenient (and in mode MODE, if that's convenient). */
3682 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED
, tree arglist
, rtx target
,
3683 enum machine_mode mode
)
3685 if (!validate_arglist (arglist
,
3686 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3690 tree result
= fold_builtin_memcmp (arglist
);
3692 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3695 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3697 tree arg1
= TREE_VALUE (arglist
);
3698 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
3699 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3700 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3705 = get_pointer_alignment (arg1
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3707 = get_pointer_alignment (arg2
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3708 enum machine_mode insn_mode
;
3710 #ifdef HAVE_cmpmemsi
3712 insn_mode
= insn_data
[(int) CODE_FOR_cmpmemsi
].operand
[0].mode
;
3715 #ifdef HAVE_cmpstrnsi
3717 insn_mode
= insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
3722 /* If we don't have POINTER_TYPE, call the function. */
3723 if (arg1_align
== 0 || arg2_align
== 0)
3726 /* Make a place to write the result of the instruction. */
3729 && REG_P (result
) && GET_MODE (result
) == insn_mode
3730 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3731 result
= gen_reg_rtx (insn_mode
);
3733 arg1_rtx
= get_memory_rtx (arg1
, len
);
3734 arg2_rtx
= get_memory_rtx (arg2
, len
);
3735 arg3_rtx
= expand_normal (len
);
3737 /* Set MEM_SIZE as appropriate. */
3738 if (GET_CODE (arg3_rtx
) == CONST_INT
)
3740 set_mem_size (arg1_rtx
, arg3_rtx
);
3741 set_mem_size (arg2_rtx
, arg3_rtx
);
3744 #ifdef HAVE_cmpmemsi
3746 insn
= gen_cmpmemsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3747 GEN_INT (MIN (arg1_align
, arg2_align
)));
3750 #ifdef HAVE_cmpstrnsi
3752 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3753 GEN_INT (MIN (arg1_align
, arg2_align
)));
3761 emit_library_call_value (memcmp_libfunc
, result
, LCT_PURE_MAKE_BLOCK
,
3762 TYPE_MODE (integer_type_node
), 3,
3763 XEXP (arg1_rtx
, 0), Pmode
,
3764 XEXP (arg2_rtx
, 0), Pmode
,
3765 convert_to_mode (TYPE_MODE (sizetype
), arg3_rtx
,
3766 TYPE_UNSIGNED (sizetype
)),
3767 TYPE_MODE (sizetype
));
3769 /* Return the value in the proper mode for this function. */
3770 mode
= TYPE_MODE (TREE_TYPE (exp
));
3771 if (GET_MODE (result
) == mode
)
3773 else if (target
!= 0)
3775 convert_move (target
, result
, 0);
3779 return convert_to_mode (mode
, result
, 0);
3786 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3787 if we failed the caller should emit a normal call, otherwise try to get
3788 the result in TARGET, if convenient. */
3791 expand_builtin_strcmp (tree exp
, rtx target
, enum machine_mode mode
)
3793 tree arglist
= TREE_OPERAND (exp
, 1);
3795 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3799 tree result
= fold_builtin_strcmp (arglist
);
3801 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3804 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3805 if (cmpstr_optab
[SImode
] != CODE_FOR_nothing
3806 || cmpstrn_optab
[SImode
] != CODE_FOR_nothing
)
3808 rtx arg1_rtx
, arg2_rtx
;
3809 rtx result
, insn
= NULL_RTX
;
3812 tree arg1
= TREE_VALUE (arglist
);
3813 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
3815 = get_pointer_alignment (arg1
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3817 = get_pointer_alignment (arg2
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3819 /* If we don't have POINTER_TYPE, call the function. */
3820 if (arg1_align
== 0 || arg2_align
== 0)
3823 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3824 arg1
= builtin_save_expr (arg1
);
3825 arg2
= builtin_save_expr (arg2
);
3827 arg1_rtx
= get_memory_rtx (arg1
, NULL
);
3828 arg2_rtx
= get_memory_rtx (arg2
, NULL
);
3830 #ifdef HAVE_cmpstrsi
3831 /* Try to call cmpstrsi. */
3834 enum machine_mode insn_mode
3835 = insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
3837 /* Make a place to write the result of the instruction. */
3840 && REG_P (result
) && GET_MODE (result
) == insn_mode
3841 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3842 result
= gen_reg_rtx (insn_mode
);
3844 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
,
3845 GEN_INT (MIN (arg1_align
, arg2_align
)));
3848 #ifdef HAVE_cmpstrnsi
3849 /* Try to determine at least one length and call cmpstrnsi. */
3850 if (!insn
&& HAVE_cmpstrnsi
)
3855 enum machine_mode insn_mode
3856 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
3857 tree len1
= c_strlen (arg1
, 1);
3858 tree len2
= c_strlen (arg2
, 1);
3861 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
3863 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
3865 /* If we don't have a constant length for the first, use the length
3866 of the second, if we know it. We don't require a constant for
3867 this case; some cost analysis could be done if both are available
3868 but neither is constant. For now, assume they're equally cheap,
3869 unless one has side effects. If both strings have constant lengths,
3876 else if (TREE_SIDE_EFFECTS (len1
))
3878 else if (TREE_SIDE_EFFECTS (len2
))
3880 else if (TREE_CODE (len1
) != INTEGER_CST
)
3882 else if (TREE_CODE (len2
) != INTEGER_CST
)
3884 else if (tree_int_cst_lt (len1
, len2
))
3889 /* If both arguments have side effects, we cannot optimize. */
3890 if (!len
|| TREE_SIDE_EFFECTS (len
))
3893 arg3_rtx
= expand_normal (len
);
3895 /* Make a place to write the result of the instruction. */
3898 && REG_P (result
) && GET_MODE (result
) == insn_mode
3899 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3900 result
= gen_reg_rtx (insn_mode
);
3902 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3903 GEN_INT (MIN (arg1_align
, arg2_align
)));
3911 /* Return the value in the proper mode for this function. */
3912 mode
= TYPE_MODE (TREE_TYPE (exp
));
3913 if (GET_MODE (result
) == mode
)
3916 return convert_to_mode (mode
, result
, 0);
3917 convert_move (target
, result
, 0);
3921 /* Expand the library call ourselves using a stabilized argument
3922 list to avoid re-evaluating the function's arguments twice. */
3923 #ifdef HAVE_cmpstrnsi
3926 arglist
= build_tree_list (NULL_TREE
, arg2
);
3927 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
3928 fndecl
= get_callee_fndecl (exp
);
3929 fn
= build_function_call_expr (fndecl
, arglist
);
3930 if (TREE_CODE (fn
) == CALL_EXPR
)
3931 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
3932 return expand_call (fn
, target
, target
== const0_rtx
);
3938 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3939 if we failed the caller should emit a normal call, otherwise try to get
3940 the result in TARGET, if convenient. */
3943 expand_builtin_strncmp (tree exp
, rtx target
, enum machine_mode mode
)
3945 tree arglist
= TREE_OPERAND (exp
, 1);
3947 if (!validate_arglist (arglist
,
3948 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3952 tree result
= fold_builtin_strncmp (arglist
);
3954 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3957 /* If c_strlen can determine an expression for one of the string
3958 lengths, and it doesn't have side effects, then emit cmpstrnsi
3959 using length MIN(strlen(string)+1, arg3). */
3960 #ifdef HAVE_cmpstrnsi
3963 tree arg1
= TREE_VALUE (arglist
);
3964 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
3965 tree arg3
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
3966 tree len
, len1
, len2
;
3967 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3972 = get_pointer_alignment (arg1
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3974 = get_pointer_alignment (arg2
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
3975 enum machine_mode insn_mode
3976 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
3978 len1
= c_strlen (arg1
, 1);
3979 len2
= c_strlen (arg2
, 1);
3982 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
3984 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
3986 /* If we don't have a constant length for the first, use the length
3987 of the second, if we know it. We don't require a constant for
3988 this case; some cost analysis could be done if both are available
3989 but neither is constant. For now, assume they're equally cheap,
3990 unless one has side effects. If both strings have constant lengths,
3997 else if (TREE_SIDE_EFFECTS (len1
))
3999 else if (TREE_SIDE_EFFECTS (len2
))
4001 else if (TREE_CODE (len1
) != INTEGER_CST
)
4003 else if (TREE_CODE (len2
) != INTEGER_CST
)
4005 else if (tree_int_cst_lt (len1
, len2
))
4010 /* If both arguments have side effects, we cannot optimize. */
4011 if (!len
|| TREE_SIDE_EFFECTS (len
))
4014 /* The actual new length parameter is MIN(len,arg3). */
4015 len
= fold_build2 (MIN_EXPR
, TREE_TYPE (len
), len
,
4016 fold_convert (TREE_TYPE (len
), arg3
));
4018 /* If we don't have POINTER_TYPE, call the function. */
4019 if (arg1_align
== 0 || arg2_align
== 0)
4022 /* Make a place to write the result of the instruction. */
4025 && REG_P (result
) && GET_MODE (result
) == insn_mode
4026 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
4027 result
= gen_reg_rtx (insn_mode
);
4029 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4030 arg1
= builtin_save_expr (arg1
);
4031 arg2
= builtin_save_expr (arg2
);
4032 len
= builtin_save_expr (len
);
4034 arg1_rtx
= get_memory_rtx (arg1
, len
);
4035 arg2_rtx
= get_memory_rtx (arg2
, len
);
4036 arg3_rtx
= expand_normal (len
);
4037 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
4038 GEN_INT (MIN (arg1_align
, arg2_align
)));
4043 /* Return the value in the proper mode for this function. */
4044 mode
= TYPE_MODE (TREE_TYPE (exp
));
4045 if (GET_MODE (result
) == mode
)
4048 return convert_to_mode (mode
, result
, 0);
4049 convert_move (target
, result
, 0);
4053 /* Expand the library call ourselves using a stabilized argument
4054 list to avoid re-evaluating the function's arguments twice. */
4055 arglist
= build_tree_list (NULL_TREE
, len
);
4056 arglist
= tree_cons (NULL_TREE
, arg2
, arglist
);
4057 arglist
= tree_cons (NULL_TREE
, arg1
, arglist
);
4058 fndecl
= get_callee_fndecl (exp
);
4059 fn
= build_function_call_expr (fndecl
, arglist
);
4060 if (TREE_CODE (fn
) == CALL_EXPR
)
4061 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4062 return expand_call (fn
, target
, target
== const0_rtx
);
4068 /* Expand expression EXP, which is a call to the strcat builtin.
4069 Return 0 if we failed the caller should emit a normal call,
4070 otherwise try to get the result in TARGET, if convenient. */
4073 expand_builtin_strcat (tree fndecl
, tree arglist
, rtx target
, enum machine_mode mode
)
4075 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4079 tree dst
= TREE_VALUE (arglist
),
4080 src
= TREE_VALUE (TREE_CHAIN (arglist
));
4081 const char *p
= c_getstr (src
);
4083 /* If the string length is zero, return the dst parameter. */
4084 if (p
&& *p
== '\0')
4085 return expand_expr (dst
, target
, mode
, EXPAND_NORMAL
);
4089 /* See if we can store by pieces into (dst + strlen(dst)). */
4090 tree newsrc
, newdst
,
4091 strlen_fn
= implicit_built_in_decls
[BUILT_IN_STRLEN
];
4094 /* Stabilize the argument list. */
4095 newsrc
= builtin_save_expr (src
);
4097 arglist
= build_tree_list (NULL_TREE
, newsrc
);
4099 arglist
= TREE_CHAIN (arglist
); /* Reusing arglist if safe. */
4101 dst
= builtin_save_expr (dst
);
4105 /* Create strlen (dst). */
4107 build_function_call_expr (strlen_fn
,
4108 build_tree_list (NULL_TREE
, dst
));
4109 /* Create (dst + (cast) strlen (dst)). */
4110 newdst
= fold_convert (TREE_TYPE (dst
), newdst
);
4111 newdst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (dst
), dst
, newdst
);
4113 newdst
= builtin_save_expr (newdst
);
4114 arglist
= tree_cons (NULL_TREE
, newdst
, arglist
);
4116 if (!expand_builtin_strcpy (fndecl
, arglist
, target
, mode
))
4118 end_sequence (); /* Stop sequence. */
4122 /* Output the entire sequence. */
4123 insns
= get_insns ();
4127 return expand_expr (dst
, target
, mode
, EXPAND_NORMAL
);
4134 /* Expand expression EXP, which is a call to the strncat builtin.
4135 Return 0 if we failed the caller should emit a normal call,
4136 otherwise try to get the result in TARGET, if convenient. */
4139 expand_builtin_strncat (tree arglist
, rtx target
, enum machine_mode mode
)
4141 if (validate_arglist (arglist
,
4142 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4144 tree result
= fold_builtin_strncat (arglist
);
4146 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
4151 /* Expand expression EXP, which is a call to the strspn builtin.
4152 Return 0 if we failed the caller should emit a normal call,
4153 otherwise try to get the result in TARGET, if convenient. */
4156 expand_builtin_strspn (tree arglist
, rtx target
, enum machine_mode mode
)
4158 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4160 tree result
= fold_builtin_strspn (arglist
);
4162 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
4167 /* Expand expression EXP, which is a call to the strcspn builtin.
4168 Return 0 if we failed the caller should emit a normal call,
4169 otherwise try to get the result in TARGET, if convenient. */
4172 expand_builtin_strcspn (tree arglist
, rtx target
, enum machine_mode mode
)
4174 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4176 tree result
= fold_builtin_strcspn (arglist
);
4178 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
4183 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4184 if that's convenient. */
4187 expand_builtin_saveregs (void)
4191 /* Don't do __builtin_saveregs more than once in a function.
4192 Save the result of the first call and reuse it. */
4193 if (saveregs_value
!= 0)
4194 return saveregs_value
;
4196 /* When this function is called, it means that registers must be
4197 saved on entry to this function. So we migrate the call to the
4198 first insn of this function. */
4202 /* Do whatever the machine needs done in this case. */
4203 val
= targetm
.calls
.expand_builtin_saveregs ();
4208 saveregs_value
= val
;
4210 /* Put the insns after the NOTE that starts the function. If this
4211 is inside a start_sequence, make the outer-level insn chain current, so
4212 the code is placed at the start of the function. */
4213 push_topmost_sequence ();
4214 emit_insn_after (seq
, entry_of_function ());
4215 pop_topmost_sequence ();
4220 /* __builtin_args_info (N) returns word N of the arg space info
4221 for the current function. The number and meanings of words
4222 is controlled by the definition of CUMULATIVE_ARGS. */
4225 expand_builtin_args_info (tree arglist
)
4227 int nwords
= sizeof (CUMULATIVE_ARGS
) / sizeof (int);
4228 int *word_ptr
= (int *) ¤t_function_args_info
;
4230 gcc_assert (sizeof (CUMULATIVE_ARGS
) % sizeof (int) == 0);
4234 if (!host_integerp (TREE_VALUE (arglist
), 0))
4235 error ("argument of %<__builtin_args_info%> must be constant");
4238 HOST_WIDE_INT wordnum
= tree_low_cst (TREE_VALUE (arglist
), 0);
4240 if (wordnum
< 0 || wordnum
>= nwords
)
4241 error ("argument of %<__builtin_args_info%> out of range");
4243 return GEN_INT (word_ptr
[wordnum
]);
4247 error ("missing argument in %<__builtin_args_info%>");
4252 /* Expand a call to __builtin_next_arg. */
4255 expand_builtin_next_arg (void)
4257 /* Checking arguments is already done in fold_builtin_next_arg
4258 that must be called before this function. */
4259 return expand_binop (Pmode
, add_optab
,
4260 current_function_internal_arg_pointer
,
4261 current_function_arg_offset_rtx
,
4262 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
4265 /* Make it easier for the backends by protecting the valist argument
4266 from multiple evaluations. */
4269 stabilize_va_list (tree valist
, int needs_lvalue
)
4271 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
4273 if (TREE_SIDE_EFFECTS (valist
))
4274 valist
= save_expr (valist
);
4276 /* For this case, the backends will be expecting a pointer to
4277 TREE_TYPE (va_list_type_node), but it's possible we've
4278 actually been given an array (an actual va_list_type_node).
4280 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4282 tree p1
= build_pointer_type (TREE_TYPE (va_list_type_node
));
4283 valist
= build_fold_addr_expr_with_type (valist
, p1
);
4292 if (! TREE_SIDE_EFFECTS (valist
))
4295 pt
= build_pointer_type (va_list_type_node
);
4296 valist
= fold_build1 (ADDR_EXPR
, pt
, valist
);
4297 TREE_SIDE_EFFECTS (valist
) = 1;
4300 if (TREE_SIDE_EFFECTS (valist
))
4301 valist
= save_expr (valist
);
4302 valist
= build_fold_indirect_ref (valist
);
4308 /* The "standard" definition of va_list is void*. */
4311 std_build_builtin_va_list (void)
4313 return ptr_type_node
;
4316 /* The "standard" implementation of va_start: just assign `nextarg' to
4320 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
4324 t
= build2 (GIMPLE_MODIFY_STMT
, TREE_TYPE (valist
), valist
,
4325 make_tree (ptr_type_node
, nextarg
));
4326 TREE_SIDE_EFFECTS (t
) = 1;
4328 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4331 /* Expand ARGLIST, from a call to __builtin_va_start. */
4334 expand_builtin_va_start (tree arglist
)
4339 chain
= TREE_CHAIN (arglist
);
4343 error ("too few arguments to function %<va_start%>");
4347 if (fold_builtin_next_arg (chain
))
4350 nextarg
= expand_builtin_next_arg ();
4351 valist
= stabilize_va_list (TREE_VALUE (arglist
), 1);
4353 #ifdef EXPAND_BUILTIN_VA_START
4354 EXPAND_BUILTIN_VA_START (valist
, nextarg
);
4356 std_expand_builtin_va_start (valist
, nextarg
);
4362 /* The "standard" implementation of va_arg: read the value from the
4363 current (padded) address and increment by the (padded) size. */
4366 std_gimplify_va_arg_expr (tree valist
, tree type
, tree
*pre_p
, tree
*post_p
)
4368 tree addr
, t
, type_size
, rounded_size
, valist_tmp
;
4369 unsigned HOST_WIDE_INT align
, boundary
;
4372 #ifdef ARGS_GROW_DOWNWARD
4373 /* All of the alignment and movement below is for args-grow-up machines.
4374 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4375 implement their own specialized gimplify_va_arg_expr routines. */
4379 indirect
= pass_by_reference (NULL
, TYPE_MODE (type
), type
, false);
4381 type
= build_pointer_type (type
);
4383 align
= PARM_BOUNDARY
/ BITS_PER_UNIT
;
4384 boundary
= FUNCTION_ARG_BOUNDARY (TYPE_MODE (type
), type
) / BITS_PER_UNIT
;
4386 /* Hoist the valist value into a temporary for the moment. */
4387 valist_tmp
= get_initialized_tmp_var (valist
, pre_p
, NULL
);
4389 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4390 requires greater alignment, we must perform dynamic alignment. */
4391 if (boundary
> align
4392 && !integer_zerop (TYPE_SIZE (type
)))
4394 t
= fold_convert (TREE_TYPE (valist
), size_int (boundary
- 1));
4395 t
= build2 (GIMPLE_MODIFY_STMT
, TREE_TYPE (valist
), valist_tmp
,
4396 build2 (PLUS_EXPR
, TREE_TYPE (valist
), valist_tmp
, t
));
4397 gimplify_and_add (t
, pre_p
);
4399 t
= fold_convert (TREE_TYPE (valist
), size_int (-boundary
));
4400 t
= build2 (GIMPLE_MODIFY_STMT
, TREE_TYPE (valist
), valist_tmp
,
4401 build2 (BIT_AND_EXPR
, TREE_TYPE (valist
), valist_tmp
, t
));
4402 gimplify_and_add (t
, pre_p
);
4407 /* If the actual alignment is less than the alignment of the type,
4408 adjust the type accordingly so that we don't assume strict alignment
4409 when deferencing the pointer. */
4410 boundary
*= BITS_PER_UNIT
;
4411 if (boundary
< TYPE_ALIGN (type
))
4413 type
= build_variant_type_copy (type
);
4414 TYPE_ALIGN (type
) = boundary
;
4417 /* Compute the rounded size of the type. */
4418 type_size
= size_in_bytes (type
);
4419 rounded_size
= round_up (type_size
, align
);
4421 /* Reduce rounded_size so it's sharable with the postqueue. */
4422 gimplify_expr (&rounded_size
, pre_p
, post_p
, is_gimple_val
, fb_rvalue
);
4426 if (PAD_VARARGS_DOWN
&& !integer_zerop (rounded_size
))
4428 /* Small args are padded downward. */
4429 t
= fold_build2 (GT_EXPR
, sizetype
, rounded_size
, size_int (align
));
4430 t
= fold_build3 (COND_EXPR
, sizetype
, t
, size_zero_node
,
4431 size_binop (MINUS_EXPR
, rounded_size
, type_size
));
4432 t
= fold_convert (TREE_TYPE (addr
), t
);
4433 addr
= fold_build2 (PLUS_EXPR
, TREE_TYPE (addr
), addr
, t
);
4436 /* Compute new value for AP. */
4437 t
= fold_convert (TREE_TYPE (valist
), rounded_size
);
4438 t
= build2 (PLUS_EXPR
, TREE_TYPE (valist
), valist_tmp
, t
);
4439 t
= build2 (GIMPLE_MODIFY_STMT
, TREE_TYPE (valist
), valist
, t
);
4440 gimplify_and_add (t
, pre_p
);
4442 addr
= fold_convert (build_pointer_type (type
), addr
);
4445 addr
= build_va_arg_indirect_ref (addr
);
4447 return build_va_arg_indirect_ref (addr
);
4450 /* Build an indirect-ref expression over the given TREE, which represents a
4451 piece of a va_arg() expansion. */
4453 build_va_arg_indirect_ref (tree addr
)
4455 addr
= build_fold_indirect_ref (addr
);
4457 if (flag_mudflap
) /* Don't instrument va_arg INDIRECT_REF. */
4463 /* Return a dummy expression of type TYPE in order to keep going after an
4467 dummy_object (tree type
)
4469 tree t
= build_int_cst (build_pointer_type (type
), 0);
4470 return build1 (INDIRECT_REF
, type
, t
);
4473 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4474 builtin function, but a very special sort of operator. */
4476 enum gimplify_status
4477 gimplify_va_arg_expr (tree
*expr_p
, tree
*pre_p
, tree
*post_p
)
4479 tree promoted_type
, want_va_type
, have_va_type
;
4480 tree valist
= TREE_OPERAND (*expr_p
, 0);
4481 tree type
= TREE_TYPE (*expr_p
);
4484 /* Verify that valist is of the proper type. */
4485 want_va_type
= va_list_type_node
;
4486 have_va_type
= TREE_TYPE (valist
);
4488 if (have_va_type
== error_mark_node
)
4491 if (TREE_CODE (want_va_type
) == ARRAY_TYPE
)
4493 /* If va_list is an array type, the argument may have decayed
4494 to a pointer type, e.g. by being passed to another function.
4495 In that case, unwrap both types so that we can compare the
4496 underlying records. */
4497 if (TREE_CODE (have_va_type
) == ARRAY_TYPE
4498 || POINTER_TYPE_P (have_va_type
))
4500 want_va_type
= TREE_TYPE (want_va_type
);
4501 have_va_type
= TREE_TYPE (have_va_type
);
4505 if (TYPE_MAIN_VARIANT (want_va_type
) != TYPE_MAIN_VARIANT (have_va_type
))
4507 error ("first argument to %<va_arg%> not of type %<va_list%>");
4511 /* Generate a diagnostic for requesting data of a type that cannot
4512 be passed through `...' due to type promotion at the call site. */
4513 else if ((promoted_type
= lang_hooks
.types
.type_promotes_to (type
))
4516 static bool gave_help
;
4518 /* Unfortunately, this is merely undefined, rather than a constraint
4519 violation, so we cannot make this an error. If this call is never
4520 executed, the program is still strictly conforming. */
4521 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4522 type
, promoted_type
);
4526 warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4527 promoted_type
, type
);
4530 /* We can, however, treat "undefined" any way we please.
4531 Call abort to encourage the user to fix the program. */
4532 inform ("if this code is reached, the program will abort");
4533 t
= build_function_call_expr (implicit_built_in_decls
[BUILT_IN_TRAP
],
4535 append_to_statement_list (t
, pre_p
);
4537 /* This is dead code, but go ahead and finish so that the
4538 mode of the result comes out right. */
4539 *expr_p
= dummy_object (type
);
4544 /* Make it easier for the backends by protecting the valist argument
4545 from multiple evaluations. */
4546 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
4548 /* For this case, the backends will be expecting a pointer to
4549 TREE_TYPE (va_list_type_node), but it's possible we've
4550 actually been given an array (an actual va_list_type_node).
4552 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4554 tree p1
= build_pointer_type (TREE_TYPE (va_list_type_node
));
4555 valist
= build_fold_addr_expr_with_type (valist
, p1
);
4557 gimplify_expr (&valist
, pre_p
, post_p
, is_gimple_val
, fb_rvalue
);
4560 gimplify_expr (&valist
, pre_p
, post_p
, is_gimple_min_lval
, fb_lvalue
);
4562 if (!targetm
.gimplify_va_arg_expr
)
4563 /* FIXME:Once most targets are converted we should merely
4564 assert this is non-null. */
4567 *expr_p
= targetm
.gimplify_va_arg_expr (valist
, type
, pre_p
, post_p
);
4572 /* Expand ARGLIST, from a call to __builtin_va_end. */
4575 expand_builtin_va_end (tree arglist
)
4577 tree valist
= TREE_VALUE (arglist
);
4579 /* Evaluate for side effects, if needed. I hate macros that don't
4581 if (TREE_SIDE_EFFECTS (valist
))
4582 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4587 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4588 builtin rather than just as an assignment in stdarg.h because of the
4589 nastiness of array-type va_list types. */
4592 expand_builtin_va_copy (tree arglist
)
4596 dst
= TREE_VALUE (arglist
);
4597 src
= TREE_VALUE (TREE_CHAIN (arglist
));
4599 dst
= stabilize_va_list (dst
, 1);
4600 src
= stabilize_va_list (src
, 0);
4602 if (TREE_CODE (va_list_type_node
) != ARRAY_TYPE
)
4604 t
= build2 (GIMPLE_MODIFY_STMT
, va_list_type_node
, dst
, src
);
4605 TREE_SIDE_EFFECTS (t
) = 1;
4606 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4610 rtx dstb
, srcb
, size
;
4612 /* Evaluate to pointers. */
4613 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4614 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4615 size
= expand_expr (TYPE_SIZE_UNIT (va_list_type_node
), NULL_RTX
,
4616 VOIDmode
, EXPAND_NORMAL
);
4618 dstb
= convert_memory_address (Pmode
, dstb
);
4619 srcb
= convert_memory_address (Pmode
, srcb
);
4621 /* "Dereference" to BLKmode memories. */
4622 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
4623 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
4624 set_mem_align (dstb
, TYPE_ALIGN (va_list_type_node
));
4625 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
4626 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
4627 set_mem_align (srcb
, TYPE_ALIGN (va_list_type_node
));
4630 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
4636 /* Expand a call to one of the builtin functions __builtin_frame_address or
4637 __builtin_return_address. */
4640 expand_builtin_frame_address (tree fndecl
, tree arglist
)
4642 /* The argument must be a nonnegative integer constant.
4643 It counts the number of frames to scan up the stack.
4644 The value is the return address saved in that frame. */
4646 /* Warning about missing arg was already issued. */
4648 else if (! host_integerp (TREE_VALUE (arglist
), 1))
4650 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4651 error ("invalid argument to %<__builtin_frame_address%>");
4653 error ("invalid argument to %<__builtin_return_address%>");
4659 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
),
4660 tree_low_cst (TREE_VALUE (arglist
), 1));
4662 /* Some ports cannot access arbitrary stack frames. */
4665 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4666 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4668 warning (0, "unsupported argument to %<__builtin_return_address%>");
4672 /* For __builtin_frame_address, return what we've got. */
4673 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4677 && ! CONSTANT_P (tem
))
4678 tem
= copy_to_mode_reg (Pmode
, tem
);
4683 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4684 we failed and the caller should emit a normal call, otherwise try to get
4685 the result in TARGET, if convenient. */
4688 expand_builtin_alloca (tree arglist
, rtx target
)
4693 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4694 should always expand to function calls. These can be intercepted
4699 if (!validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
4702 /* Compute the argument. */
4703 op0
= expand_normal (TREE_VALUE (arglist
));
4705 /* Allocate the desired space. */
4706 result
= allocate_dynamic_stack_space (op0
, target
, BITS_PER_UNIT
);
4707 result
= convert_memory_address (ptr_mode
, result
);
4712 /* Expand a call to a bswap builtin. The arguments are in ARGLIST. MODE
4713 is the mode to expand with. */
4716 expand_builtin_bswap (tree arglist
, rtx target
, rtx subtarget
)
4718 enum machine_mode mode
;
4722 if (!validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
4725 arg
= TREE_VALUE (arglist
);
4726 mode
= TYPE_MODE (TREE_TYPE (arg
));
4727 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
4729 target
= expand_unop (mode
, bswap_optab
, op0
, target
, 1);
4731 gcc_assert (target
);
4733 return convert_to_mode (mode
, target
, 0);
4736 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4737 Return 0 if a normal call should be emitted rather than expanding the
4738 function in-line. If convenient, the result should be placed in TARGET.
4739 SUBTARGET may be used as the target for computing one of EXP's operands. */
4742 expand_builtin_unop (enum machine_mode target_mode
, tree arglist
, rtx target
,
4743 rtx subtarget
, optab op_optab
)
4746 if (!validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
4749 /* Compute the argument. */
4750 op0
= expand_expr (TREE_VALUE (arglist
), subtarget
, VOIDmode
, 0);
4751 /* Compute op, into TARGET if possible.
4752 Set TARGET to wherever the result comes back. */
4753 target
= expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist
))),
4754 op_optab
, op0
, target
, 1);
4755 gcc_assert (target
);
4757 return convert_to_mode (target_mode
, target
, 0);
4760 /* If the string passed to fputs is a constant and is one character
4761 long, we attempt to transform this call into __builtin_fputc(). */
4764 expand_builtin_fputs (tree arglist
, rtx target
, bool unlocked
)
4766 /* Verify the arguments in the original call. */
4767 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4769 tree result
= fold_builtin_fputs (arglist
, (target
== const0_rtx
),
4770 unlocked
, NULL_TREE
);
4772 return expand_expr (result
, target
, VOIDmode
, EXPAND_NORMAL
);
4777 /* Expand a call to __builtin_expect. We just return our argument
4778 as the builtin_expect semantic should've been already executed by
4779 tree branch prediction pass. */
4782 expand_builtin_expect (tree arglist
, rtx target
)
4786 if (arglist
== NULL_TREE
4787 || TREE_CHAIN (arglist
) == NULL_TREE
)
4789 exp
= TREE_VALUE (arglist
);
4790 c
= TREE_VALUE (TREE_CHAIN (arglist
));
4792 target
= expand_expr (exp
, target
, VOIDmode
, EXPAND_NORMAL
);
4793 /* When guessing was done, the hints should be already stripped away. */
4794 gcc_assert (!flag_guess_branch_prob
);
4799 expand_builtin_trap (void)
4803 emit_insn (gen_trap ());
4806 emit_library_call (abort_libfunc
, LCT_NORETURN
, VOIDmode
, 0);
4810 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4811 Return 0 if a normal call should be emitted rather than expanding
4812 the function inline. If convenient, the result should be placed
4813 in TARGET. SUBTARGET may be used as the target for computing
4817 expand_builtin_fabs (tree arglist
, rtx target
, rtx subtarget
)
4819 enum machine_mode mode
;
4823 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
4826 arg
= TREE_VALUE (arglist
);
4827 mode
= TYPE_MODE (TREE_TYPE (arg
));
4828 op0
= expand_expr (arg
, subtarget
, VOIDmode
, 0);
4829 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
4832 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4833 Return NULL is a normal call should be emitted rather than expanding the
4834 function inline. If convenient, the result should be placed in TARGET.
4835 SUBTARGET may be used as the target for computing the operand. */
4838 expand_builtin_copysign (tree arglist
, rtx target
, rtx subtarget
)
4843 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
4846 arg
= TREE_VALUE (arglist
);
4847 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4849 arg
= TREE_VALUE (TREE_CHAIN (arglist
));
4850 op1
= expand_normal (arg
);
4852 return expand_copysign (op0
, op1
, target
);
4855 /* Create a new constant string literal and return a char* pointer to it.
4856 The STRING_CST value is the LEN characters at STR. */
4858 build_string_literal (int len
, const char *str
)
4860 tree t
, elem
, index
, type
;
4862 t
= build_string (len
, str
);
4863 elem
= build_type_variant (char_type_node
, 1, 0);
4864 index
= build_index_type (build_int_cst (NULL_TREE
, len
- 1));
4865 type
= build_array_type (elem
, index
);
4866 TREE_TYPE (t
) = type
;
4867 TREE_CONSTANT (t
) = 1;
4868 TREE_INVARIANT (t
) = 1;
4869 TREE_READONLY (t
) = 1;
4870 TREE_STATIC (t
) = 1;
4872 type
= build_pointer_type (type
);
4873 t
= build1 (ADDR_EXPR
, type
, t
);
4875 type
= build_pointer_type (elem
);
4876 t
= build1 (NOP_EXPR
, type
, t
);
4880 /* Expand EXP, a call to printf or printf_unlocked.
4881 Return 0 if a normal call should be emitted rather than transforming
4882 the function inline. If convenient, the result should be placed in
4883 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4886 expand_builtin_printf (tree exp
, rtx target
, enum machine_mode mode
,
4889 tree arglist
= TREE_OPERAND (exp
, 1);
4890 /* If we're using an unlocked function, assume the other unlocked
4891 functions exist explicitly. */
4892 tree
const fn_putchar
= unlocked
? built_in_decls
[BUILT_IN_PUTCHAR_UNLOCKED
]
4893 : implicit_built_in_decls
[BUILT_IN_PUTCHAR
];
4894 tree
const fn_puts
= unlocked
? built_in_decls
[BUILT_IN_PUTS_UNLOCKED
]
4895 : implicit_built_in_decls
[BUILT_IN_PUTS
];
4896 const char *fmt_str
;
4899 /* If the return value is used, don't do the transformation. */
4900 if (target
!= const0_rtx
)
4903 /* Verify the required arguments in the original call. */
4906 fmt
= TREE_VALUE (arglist
);
4907 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
4909 arglist
= TREE_CHAIN (arglist
);
4911 /* Check whether the format is a literal string constant. */
4912 fmt_str
= c_getstr (fmt
);
4913 if (fmt_str
== NULL
)
4916 if (!init_target_chars())
4919 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4920 if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
4923 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
4924 || TREE_CHAIN (arglist
))
4928 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4929 else if (strcmp (fmt_str
, target_percent_c
) == 0)
4932 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
4933 || TREE_CHAIN (arglist
))
4939 /* We can't handle anything else with % args or %% ... yet. */
4940 if (strchr (fmt_str
, target_percent
))
4946 /* If the format specifier was "", printf does nothing. */
4947 if (fmt_str
[0] == '\0')
4949 /* If the format specifier has length of 1, call putchar. */
4950 if (fmt_str
[1] == '\0')
4952 /* Given printf("c"), (where c is any one character,)
4953 convert "c"[0] to an int and pass that to the replacement
4955 arg
= build_int_cst (NULL_TREE
, fmt_str
[0]);
4956 arglist
= build_tree_list (NULL_TREE
, arg
);
4961 /* If the format specifier was "string\n", call puts("string"). */
4962 size_t len
= strlen (fmt_str
);
4963 if ((unsigned char)fmt_str
[len
- 1] == target_newline
)
4965 /* Create a NUL-terminated string that's one char shorter
4966 than the original, stripping off the trailing '\n'. */
4967 char *newstr
= alloca (len
);
4968 memcpy (newstr
, fmt_str
, len
- 1);
4969 newstr
[len
- 1] = 0;
4971 arg
= build_string_literal (len
, newstr
);
4972 arglist
= build_tree_list (NULL_TREE
, arg
);
4976 /* We'd like to arrange to call fputs(string,stdout) here,
4977 but we need stdout and don't have a way to get it yet. */
4984 fn
= build_function_call_expr (fn
, arglist
);
4985 if (TREE_CODE (fn
) == CALL_EXPR
)
4986 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4987 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
4990 /* Expand EXP, a call to fprintf or fprintf_unlocked.
4991 Return 0 if a normal call should be emitted rather than transforming
4992 the function inline. If convenient, the result should be placed in
4993 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4996 expand_builtin_fprintf (tree exp
, rtx target
, enum machine_mode mode
,
4999 tree arglist
= TREE_OPERAND (exp
, 1);
5000 /* If we're using an unlocked function, assume the other unlocked
5001 functions exist explicitly. */
5002 tree
const fn_fputc
= unlocked
? built_in_decls
[BUILT_IN_FPUTC_UNLOCKED
]
5003 : implicit_built_in_decls
[BUILT_IN_FPUTC
];
5004 tree
const fn_fputs
= unlocked
? built_in_decls
[BUILT_IN_FPUTS_UNLOCKED
]
5005 : implicit_built_in_decls
[BUILT_IN_FPUTS
];
5006 const char *fmt_str
;
5007 tree fn
, fmt
, fp
, arg
;
5009 /* If the return value is used, don't do the transformation. */
5010 if (target
!= const0_rtx
)
5013 /* Verify the required arguments in the original call. */
5016 fp
= TREE_VALUE (arglist
);
5017 if (! POINTER_TYPE_P (TREE_TYPE (fp
)))
5019 arglist
= TREE_CHAIN (arglist
);
5022 fmt
= TREE_VALUE (arglist
);
5023 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
5025 arglist
= TREE_CHAIN (arglist
);
5027 /* Check whether the format is a literal string constant. */
5028 fmt_str
= c_getstr (fmt
);
5029 if (fmt_str
== NULL
)
5032 if (!init_target_chars())
5035 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5036 if (strcmp (fmt_str
, target_percent_s
) == 0)
5039 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
5040 || TREE_CHAIN (arglist
))
5042 arg
= TREE_VALUE (arglist
);
5043 arglist
= build_tree_list (NULL_TREE
, fp
);
5044 arglist
= tree_cons (NULL_TREE
, arg
, arglist
);
5047 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5048 else if (strcmp (fmt_str
, target_percent_c
) == 0)
5051 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
5052 || TREE_CHAIN (arglist
))
5054 arg
= TREE_VALUE (arglist
);
5055 arglist
= build_tree_list (NULL_TREE
, fp
);
5056 arglist
= tree_cons (NULL_TREE
, arg
, arglist
);
5061 /* We can't handle anything else with % args or %% ... yet. */
5062 if (strchr (fmt_str
, target_percent
))
5068 /* If the format specifier was "", fprintf does nothing. */
5069 if (fmt_str
[0] == '\0')
5071 /* Evaluate and ignore FILE* argument for side-effects. */
5072 expand_expr (fp
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5076 /* When "string" doesn't contain %, replace all cases of
5077 fprintf(stream,string) with fputs(string,stream). The fputs
5078 builtin will take care of special cases like length == 1. */
5079 arglist
= build_tree_list (NULL_TREE
, fp
);
5080 arglist
= tree_cons (NULL_TREE
, fmt
, arglist
);
5086 fn
= build_function_call_expr (fn
, arglist
);
5087 if (TREE_CODE (fn
) == CALL_EXPR
)
5088 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
5089 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
5092 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5093 a normal call should be emitted rather than expanding the function
5094 inline. If convenient, the result should be placed in TARGET with
5098 expand_builtin_sprintf (tree arglist
, rtx target
, enum machine_mode mode
)
5100 tree orig_arglist
, dest
, fmt
;
5101 const char *fmt_str
;
5103 orig_arglist
= arglist
;
5105 /* Verify the required arguments in the original call. */
5108 dest
= TREE_VALUE (arglist
);
5109 if (! POINTER_TYPE_P (TREE_TYPE (dest
)))
5111 arglist
= TREE_CHAIN (arglist
);
5114 fmt
= TREE_VALUE (arglist
);
5115 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
5117 arglist
= TREE_CHAIN (arglist
);
5119 /* Check whether the format is a literal string constant. */
5120 fmt_str
= c_getstr (fmt
);
5121 if (fmt_str
== NULL
)
5124 if (!init_target_chars())
5127 /* If the format doesn't contain % args or %%, use strcpy. */
5128 if (strchr (fmt_str
, target_percent
) == 0)
5130 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
5133 if (arglist
|| ! fn
)
5135 expand_expr (build_function_call_expr (fn
, orig_arglist
),
5136 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5137 if (target
== const0_rtx
)
5139 exp
= build_int_cst (NULL_TREE
, strlen (fmt_str
));
5140 return expand_expr (exp
, target
, mode
, EXPAND_NORMAL
);
5142 /* If the format is "%s", use strcpy if the result isn't used. */
5143 else if (strcmp (fmt_str
, target_percent_s
) == 0)
5146 fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
5151 if (! arglist
|| TREE_CHAIN (arglist
))
5153 arg
= TREE_VALUE (arglist
);
5154 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
5157 if (target
!= const0_rtx
)
5159 len
= c_strlen (arg
, 1);
5160 if (! len
|| TREE_CODE (len
) != INTEGER_CST
)
5166 arglist
= build_tree_list (NULL_TREE
, arg
);
5167 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
5168 expand_expr (build_function_call_expr (fn
, arglist
),
5169 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5171 if (target
== const0_rtx
)
5173 return expand_expr (len
, target
, mode
, EXPAND_NORMAL
);
5179 /* Expand a call to either the entry or exit function profiler. */
5182 expand_builtin_profile_func (bool exitp
)
5186 this = DECL_RTL (current_function_decl
);
5187 gcc_assert (MEM_P (this));
5188 this = XEXP (this, 0);
5191 which
= profile_function_exit_libfunc
;
5193 which
= profile_function_entry_libfunc
;
5195 emit_library_call (which
, LCT_NORMAL
, VOIDmode
, 2, this, Pmode
,
5196 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS
,
5203 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5206 round_trampoline_addr (rtx tramp
)
5208 rtx temp
, addend
, mask
;
5210 /* If we don't need too much alignment, we'll have been guaranteed
5211 proper alignment by get_trampoline_type. */
5212 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
5215 /* Round address up to desired boundary. */
5216 temp
= gen_reg_rtx (Pmode
);
5217 addend
= GEN_INT (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1);
5218 mask
= GEN_INT (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
);
5220 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
5221 temp
, 0, OPTAB_LIB_WIDEN
);
5222 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
5223 temp
, 0, OPTAB_LIB_WIDEN
);
5229 expand_builtin_init_trampoline (tree arglist
)
5231 tree t_tramp
, t_func
, t_chain
;
5232 rtx r_tramp
, r_func
, r_chain
;
5233 #ifdef TRAMPOLINE_TEMPLATE
5237 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
,
5238 POINTER_TYPE
, VOID_TYPE
))
5241 t_tramp
= TREE_VALUE (arglist
);
5242 arglist
= TREE_CHAIN (arglist
);
5243 t_func
= TREE_VALUE (arglist
);
5244 arglist
= TREE_CHAIN (arglist
);
5245 t_chain
= TREE_VALUE (arglist
);
5247 r_tramp
= expand_normal (t_tramp
);
5248 r_func
= expand_normal (t_func
);
5249 r_chain
= expand_normal (t_chain
);
5251 /* Generate insns to initialize the trampoline. */
5252 r_tramp
= round_trampoline_addr (r_tramp
);
5253 #ifdef TRAMPOLINE_TEMPLATE
5254 blktramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
5255 set_mem_align (blktramp
, TRAMPOLINE_ALIGNMENT
);
5256 emit_block_move (blktramp
, assemble_trampoline_template (),
5257 GEN_INT (TRAMPOLINE_SIZE
), BLOCK_OP_NORMAL
);
5259 trampolines_created
= 1;
5260 INITIALIZE_TRAMPOLINE (r_tramp
, r_func
, r_chain
);
5266 expand_builtin_adjust_trampoline (tree arglist
)
5270 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
5273 tramp
= expand_normal (TREE_VALUE (arglist
));
5274 tramp
= round_trampoline_addr (tramp
);
5275 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5276 TRAMPOLINE_ADJUST_ADDRESS (tramp
);
5282 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5283 Return NULL_RTX if a normal call should be emitted rather than expanding
5284 the function in-line. EXP is the expression that is a call to the builtin
5285 function; if convenient, the result should be placed in TARGET. */
5288 expand_builtin_signbit (tree exp
, rtx target
)
5290 const struct real_format
*fmt
;
5291 enum machine_mode fmode
, imode
, rmode
;
5292 HOST_WIDE_INT hi
, lo
;
5297 arglist
= TREE_OPERAND (exp
, 1);
5298 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
5301 arg
= TREE_VALUE (arglist
);
5302 fmode
= TYPE_MODE (TREE_TYPE (arg
));
5303 rmode
= TYPE_MODE (TREE_TYPE (exp
));
5304 fmt
= REAL_MODE_FORMAT (fmode
);
5306 /* For floating point formats without a sign bit, implement signbit
5308 bitpos
= fmt
->signbit_ro
;
5311 /* But we can't do this if the format supports signed zero. */
5312 if (fmt
->has_signed_zero
&& HONOR_SIGNED_ZEROS (fmode
))
5315 arg
= fold_build2 (LT_EXPR
, TREE_TYPE (exp
), arg
,
5316 build_real (TREE_TYPE (arg
), dconst0
));
5317 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
5320 temp
= expand_normal (arg
);
5321 if (GET_MODE_SIZE (fmode
) <= UNITS_PER_WORD
)
5323 imode
= int_mode_for_mode (fmode
);
5324 if (imode
== BLKmode
)
5326 temp
= gen_lowpart (imode
, temp
);
5331 /* Handle targets with different FP word orders. */
5332 if (FLOAT_WORDS_BIG_ENDIAN
)
5333 word
= (GET_MODE_BITSIZE (fmode
) - bitpos
) / BITS_PER_WORD
;
5335 word
= bitpos
/ BITS_PER_WORD
;
5336 temp
= operand_subword_force (temp
, word
, fmode
);
5337 bitpos
= bitpos
% BITS_PER_WORD
;
5340 /* Force the intermediate word_mode (or narrower) result into a
5341 register. This avoids attempting to create paradoxical SUBREGs
5342 of floating point modes below. */
5343 temp
= force_reg (imode
, temp
);
5345 /* If the bitpos is within the "result mode" lowpart, the operation
5346 can be implement with a single bitwise AND. Otherwise, we need
5347 a right shift and an AND. */
5349 if (bitpos
< GET_MODE_BITSIZE (rmode
))
5351 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
5354 lo
= (HOST_WIDE_INT
) 1 << bitpos
;
5358 hi
= (HOST_WIDE_INT
) 1 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
5363 temp
= gen_lowpart (rmode
, temp
);
5364 temp
= expand_binop (rmode
, and_optab
, temp
,
5365 immed_double_const (lo
, hi
, rmode
),
5366 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5370 /* Perform a logical right shift to place the signbit in the least
5371 significant bit, then truncate the result to the desired mode
5372 and mask just this bit. */
5373 temp
= expand_shift (RSHIFT_EXPR
, imode
, temp
,
5374 build_int_cst (NULL_TREE
, bitpos
), NULL_RTX
, 1);
5375 temp
= gen_lowpart (rmode
, temp
);
5376 temp
= expand_binop (rmode
, and_optab
, temp
, const1_rtx
,
5377 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5383 /* Expand fork or exec calls. TARGET is the desired target of the
5384 call. ARGLIST is the list of arguments of the call. FN is the
5385 identificator of the actual function. IGNORE is nonzero if the
5386 value is to be ignored. */
5389 expand_builtin_fork_or_exec (tree fn
, tree arglist
, rtx target
, int ignore
)
5394 /* If we are not profiling, just call the function. */
5395 if (!profile_arc_flag
)
5398 /* Otherwise call the wrapper. This should be equivalent for the rest of
5399 compiler, so the code does not diverge, and the wrapper may run the
5400 code necessary for keeping the profiling sane. */
5402 switch (DECL_FUNCTION_CODE (fn
))
5405 id
= get_identifier ("__gcov_fork");
5408 case BUILT_IN_EXECL
:
5409 id
= get_identifier ("__gcov_execl");
5412 case BUILT_IN_EXECV
:
5413 id
= get_identifier ("__gcov_execv");
5416 case BUILT_IN_EXECLP
:
5417 id
= get_identifier ("__gcov_execlp");
5420 case BUILT_IN_EXECLE
:
5421 id
= get_identifier ("__gcov_execle");
5424 case BUILT_IN_EXECVP
:
5425 id
= get_identifier ("__gcov_execvp");
5428 case BUILT_IN_EXECVE
:
5429 id
= get_identifier ("__gcov_execve");
5436 decl
= build_decl (FUNCTION_DECL
, id
, TREE_TYPE (fn
));
5437 DECL_EXTERNAL (decl
) = 1;
5438 TREE_PUBLIC (decl
) = 1;
5439 DECL_ARTIFICIAL (decl
) = 1;
5440 TREE_NOTHROW (decl
) = 1;
5441 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
5442 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
5443 call
= build_function_call_expr (decl
, arglist
);
5445 return expand_call (call
, target
, ignore
);
5449 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5450 the pointer in these functions is void*, the tree optimizers may remove
5451 casts. The mode computed in expand_builtin isn't reliable either, due
5452 to __sync_bool_compare_and_swap.
5454 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5455 group of builtins. This gives us log2 of the mode size. */
5457 static inline enum machine_mode
5458 get_builtin_sync_mode (int fcode_diff
)
5460 /* The size is not negotiable, so ask not to get BLKmode in return
5461 if the target indicates that a smaller size would be better. */
5462 return mode_for_size (BITS_PER_UNIT
<< fcode_diff
, MODE_INT
, 0);
5465 /* Expand the memory expression LOC and return the appropriate memory operand
5466 for the builtin_sync operations. */
5469 get_builtin_sync_mem (tree loc
, enum machine_mode mode
)
5473 addr
= expand_expr (loc
, NULL
, Pmode
, EXPAND_SUM
);
5475 /* Note that we explicitly do not want any alias information for this
5476 memory, so that we kill all other live memories. Otherwise we don't
5477 satisfy the full barrier semantics of the intrinsic. */
5478 mem
= validize_mem (gen_rtx_MEM (mode
, addr
));
5480 set_mem_align (mem
, get_pointer_alignment (loc
, BIGGEST_ALIGNMENT
));
5481 set_mem_alias_set (mem
, ALIAS_SET_MEMORY_BARRIER
);
5482 MEM_VOLATILE_P (mem
) = 1;
5487 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5488 ARGLIST is the operands list to the function. CODE is the rtx code
5489 that corresponds to the arithmetic or logical operation from the name;
5490 an exception here is that NOT actually means NAND. TARGET is an optional
5491 place for us to store the results; AFTER is true if this is the
5492 fetch_and_xxx form. IGNORE is true if we don't actually care about
5493 the result of the operation at all. */
5496 expand_builtin_sync_operation (enum machine_mode mode
, tree arglist
,
5497 enum rtx_code code
, bool after
,
5498 rtx target
, bool ignore
)
5502 /* Expand the operands. */
5503 mem
= get_builtin_sync_mem (TREE_VALUE (arglist
), mode
);
5505 arglist
= TREE_CHAIN (arglist
);
5506 val
= expand_expr (TREE_VALUE (arglist
), NULL
, mode
, EXPAND_NORMAL
);
5507 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5508 val
= convert_to_mode (mode
, val
, 1);
5511 return expand_sync_operation (mem
, val
, code
);
5513 return expand_sync_fetch_operation (mem
, val
, code
, after
, target
);
5516 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5517 intrinsics. ARGLIST is the operands list to the function. IS_BOOL is
5518 true if this is the boolean form. TARGET is a place for us to store the
5519 results; this is NOT optional if IS_BOOL is true. */
5522 expand_builtin_compare_and_swap (enum machine_mode mode
, tree arglist
,
5523 bool is_bool
, rtx target
)
5525 rtx old_val
, new_val
, mem
;
5527 /* Expand the operands. */
5528 mem
= get_builtin_sync_mem (TREE_VALUE (arglist
), mode
);
5530 arglist
= TREE_CHAIN (arglist
);
5531 old_val
= expand_expr (TREE_VALUE (arglist
), NULL
, mode
, EXPAND_NORMAL
);
5532 /* If OLD_VAL is promoted to a wider mode, convert it back to MODE. */
5533 old_val
= convert_to_mode (mode
, old_val
, 1);
5535 arglist
= TREE_CHAIN (arglist
);
5536 new_val
= expand_expr (TREE_VALUE (arglist
), NULL
, mode
, EXPAND_NORMAL
);
5537 /* If NEW_VAL is promoted to a wider mode, convert it back to MODE. */
5538 new_val
= convert_to_mode (mode
, new_val
, 1);
5541 return expand_bool_compare_and_swap (mem
, old_val
, new_val
, target
);
5543 return expand_val_compare_and_swap (mem
, old_val
, new_val
, target
);
5546 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5547 general form is actually an atomic exchange, and some targets only
5548 support a reduced form with the second argument being a constant 1.
5549 ARGLIST is the operands list to the function; TARGET is an optional
5550 place for us to store the results. */
5553 expand_builtin_lock_test_and_set (enum machine_mode mode
, tree arglist
,
5558 /* Expand the operands. */
5559 mem
= get_builtin_sync_mem (TREE_VALUE (arglist
), mode
);
5561 arglist
= TREE_CHAIN (arglist
);
5562 val
= expand_expr (TREE_VALUE (arglist
), NULL
, mode
, EXPAND_NORMAL
);
5563 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5564 val
= convert_to_mode (mode
, val
, 1);
5566 return expand_sync_lock_test_and_set (mem
, val
, target
);
5569 /* Expand the __sync_synchronize intrinsic. */
5572 expand_builtin_synchronize (void)
5576 #ifdef HAVE_memory_barrier
5577 if (HAVE_memory_barrier
)
5579 emit_insn (gen_memory_barrier ());
5584 /* If no explicit memory barrier instruction is available, create an
5585 empty asm stmt with a memory clobber. */
5586 x
= build4 (ASM_EXPR
, void_type_node
, build_string (0, ""), NULL
, NULL
,
5587 tree_cons (NULL
, build_string (6, "memory"), NULL
));
5588 ASM_VOLATILE_P (x
) = 1;
5589 expand_asm_expr (x
);
5592 /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list
5596 expand_builtin_lock_release (enum machine_mode mode
, tree arglist
)
5598 enum insn_code icode
;
5600 rtx val
= const0_rtx
;
5602 /* Expand the operands. */
5603 mem
= get_builtin_sync_mem (TREE_VALUE (arglist
), mode
);
5605 /* If there is an explicit operation in the md file, use it. */
5606 icode
= sync_lock_release
[mode
];
5607 if (icode
!= CODE_FOR_nothing
)
5609 if (!insn_data
[icode
].operand
[1].predicate (val
, mode
))
5610 val
= force_reg (mode
, val
);
5612 insn
= GEN_FCN (icode
) (mem
, val
);
5620 /* Otherwise we can implement this operation by emitting a barrier
5621 followed by a store of zero. */
5622 expand_builtin_synchronize ();
5623 emit_move_insn (mem
, val
);
5626 /* Expand an expression EXP that calls a built-in function,
5627 with result going to TARGET if that's convenient
5628 (and in mode MODE if that's convenient).
5629 SUBTARGET may be used as the target for computing one of EXP's operands.
5630 IGNORE is nonzero if the value is to be ignored. */
5633 expand_builtin (tree exp
, rtx target
, rtx subtarget
, enum machine_mode mode
,
5636 tree fndecl
= get_callee_fndecl (exp
);
5637 tree arglist
= TREE_OPERAND (exp
, 1);
5638 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5639 enum machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
5641 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5642 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
5644 /* When not optimizing, generate calls to library functions for a certain
5647 && !called_as_built_in (fndecl
)
5648 && DECL_ASSEMBLER_NAME_SET_P (fndecl
)
5649 && fcode
!= BUILT_IN_ALLOCA
)
5650 return expand_call (exp
, target
, ignore
);
5652 /* The built-in function expanders test for target == const0_rtx
5653 to determine whether the function's result will be ignored. */
5655 target
= const0_rtx
;
5657 /* If the result of a pure or const built-in function is ignored, and
5658 none of its arguments are volatile, we can avoid expanding the
5659 built-in call and just evaluate the arguments for side-effects. */
5660 if (target
== const0_rtx
5661 && (DECL_IS_PURE (fndecl
) || TREE_READONLY (fndecl
)))
5663 bool volatilep
= false;
5666 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
5667 if (TREE_THIS_VOLATILE (TREE_VALUE (arg
)))
5675 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
5676 expand_expr (TREE_VALUE (arg
), const0_rtx
,
5677 VOIDmode
, EXPAND_NORMAL
);
5684 CASE_FLT_FN (BUILT_IN_FABS
):
5685 target
= expand_builtin_fabs (arglist
, target
, subtarget
);
5690 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
5691 target
= expand_builtin_copysign (arglist
, target
, subtarget
);
5696 /* Just do a normal library call if we were unable to fold
5698 CASE_FLT_FN (BUILT_IN_CABS
):
5701 CASE_FLT_FN (BUILT_IN_EXP
):
5702 CASE_FLT_FN (BUILT_IN_EXP10
):
5703 CASE_FLT_FN (BUILT_IN_POW10
):
5704 CASE_FLT_FN (BUILT_IN_EXP2
):
5705 CASE_FLT_FN (BUILT_IN_EXPM1
):
5706 CASE_FLT_FN (BUILT_IN_LOGB
):
5707 CASE_FLT_FN (BUILT_IN_ILOGB
):
5708 CASE_FLT_FN (BUILT_IN_LOG
):
5709 CASE_FLT_FN (BUILT_IN_LOG10
):
5710 CASE_FLT_FN (BUILT_IN_LOG2
):
5711 CASE_FLT_FN (BUILT_IN_LOG1P
):
5712 CASE_FLT_FN (BUILT_IN_TAN
):
5713 CASE_FLT_FN (BUILT_IN_ASIN
):
5714 CASE_FLT_FN (BUILT_IN_ACOS
):
5715 CASE_FLT_FN (BUILT_IN_ATAN
):
5716 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5717 because of possible accuracy problems. */
5718 if (! flag_unsafe_math_optimizations
)
5720 CASE_FLT_FN (BUILT_IN_SQRT
):
5721 CASE_FLT_FN (BUILT_IN_FLOOR
):
5722 CASE_FLT_FN (BUILT_IN_CEIL
):
5723 CASE_FLT_FN (BUILT_IN_TRUNC
):
5724 CASE_FLT_FN (BUILT_IN_ROUND
):
5725 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
5726 CASE_FLT_FN (BUILT_IN_RINT
):
5727 target
= expand_builtin_mathfn (exp
, target
, subtarget
);
5732 CASE_FLT_FN (BUILT_IN_LCEIL
):
5733 CASE_FLT_FN (BUILT_IN_LLCEIL
):
5734 CASE_FLT_FN (BUILT_IN_LFLOOR
):
5735 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
5736 target
= expand_builtin_int_roundingfn (exp
, target
, subtarget
);
5741 CASE_FLT_FN (BUILT_IN_LRINT
):
5742 CASE_FLT_FN (BUILT_IN_LLRINT
):
5743 CASE_FLT_FN (BUILT_IN_LROUND
):
5744 CASE_FLT_FN (BUILT_IN_LLROUND
):
5745 target
= expand_builtin_int_roundingfn_2 (exp
, target
, subtarget
);
5750 CASE_FLT_FN (BUILT_IN_POW
):
5751 target
= expand_builtin_pow (exp
, target
, subtarget
);
5756 CASE_FLT_FN (BUILT_IN_POWI
):
5757 target
= expand_builtin_powi (exp
, target
, subtarget
);
5762 CASE_FLT_FN (BUILT_IN_ATAN2
):
5763 CASE_FLT_FN (BUILT_IN_LDEXP
):
5764 if (! flag_unsafe_math_optimizations
)
5767 CASE_FLT_FN (BUILT_IN_FMOD
):
5768 CASE_FLT_FN (BUILT_IN_REMAINDER
):
5769 CASE_FLT_FN (BUILT_IN_DREM
):
5770 target
= expand_builtin_mathfn_2 (exp
, target
, subtarget
);
5775 CASE_FLT_FN (BUILT_IN_SIN
):
5776 CASE_FLT_FN (BUILT_IN_COS
):
5777 if (! flag_unsafe_math_optimizations
)
5779 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
5784 CASE_FLT_FN (BUILT_IN_SINCOS
):
5785 if (! flag_unsafe_math_optimizations
)
5787 target
= expand_builtin_sincos (exp
);
5792 case BUILT_IN_APPLY_ARGS
:
5793 return expand_builtin_apply_args ();
5795 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5796 FUNCTION with a copy of the parameters described by
5797 ARGUMENTS, and ARGSIZE. It returns a block of memory
5798 allocated on the stack into which is stored all the registers
5799 that might possibly be used for returning the result of a
5800 function. ARGUMENTS is the value returned by
5801 __builtin_apply_args. ARGSIZE is the number of bytes of
5802 arguments that must be copied. ??? How should this value be
5803 computed? We'll also need a safe worst case value for varargs
5805 case BUILT_IN_APPLY
:
5806 if (!validate_arglist (arglist
, POINTER_TYPE
,
5807 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
5808 && !validate_arglist (arglist
, REFERENCE_TYPE
,
5809 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
5817 for (t
= arglist
, i
= 0; t
; t
= TREE_CHAIN (t
), i
++)
5818 ops
[i
] = expand_normal (TREE_VALUE (t
));
5820 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
5823 /* __builtin_return (RESULT) causes the function to return the
5824 value described by RESULT. RESULT is address of the block of
5825 memory returned by __builtin_apply. */
5826 case BUILT_IN_RETURN
:
5827 if (validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
5828 expand_builtin_return (expand_normal (TREE_VALUE (arglist
)));
5831 case BUILT_IN_SAVEREGS
:
5832 return expand_builtin_saveregs ();
5834 case BUILT_IN_ARGS_INFO
:
5835 return expand_builtin_args_info (arglist
);
5837 /* Return the address of the first anonymous stack arg. */
5838 case BUILT_IN_NEXT_ARG
:
5839 if (fold_builtin_next_arg (arglist
))
5841 return expand_builtin_next_arg ();
5843 case BUILT_IN_CLASSIFY_TYPE
:
5844 return expand_builtin_classify_type (arglist
);
5846 case BUILT_IN_CONSTANT_P
:
5849 case BUILT_IN_FRAME_ADDRESS
:
5850 case BUILT_IN_RETURN_ADDRESS
:
5851 return expand_builtin_frame_address (fndecl
, arglist
);
5853 /* Returns the address of the area where the structure is returned.
5855 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
5857 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
5858 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
5861 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
5863 case BUILT_IN_ALLOCA
:
5864 target
= expand_builtin_alloca (arglist
, target
);
5869 case BUILT_IN_STACK_SAVE
:
5870 return expand_stack_save ();
5872 case BUILT_IN_STACK_RESTORE
:
5873 expand_stack_restore (TREE_VALUE (arglist
));
5876 case BUILT_IN_BSWAP32
:
5877 case BUILT_IN_BSWAP64
:
5878 target
= expand_builtin_bswap (arglist
, target
, subtarget
);
5884 CASE_INT_FN (BUILT_IN_FFS
):
5885 case BUILT_IN_FFSIMAX
:
5886 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5887 subtarget
, ffs_optab
);
5892 CASE_INT_FN (BUILT_IN_CLZ
):
5893 case BUILT_IN_CLZIMAX
:
5894 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5895 subtarget
, clz_optab
);
5900 CASE_INT_FN (BUILT_IN_CTZ
):
5901 case BUILT_IN_CTZIMAX
:
5902 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5903 subtarget
, ctz_optab
);
5908 CASE_INT_FN (BUILT_IN_POPCOUNT
):
5909 case BUILT_IN_POPCOUNTIMAX
:
5910 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5911 subtarget
, popcount_optab
);
5916 CASE_INT_FN (BUILT_IN_PARITY
):
5917 case BUILT_IN_PARITYIMAX
:
5918 target
= expand_builtin_unop (target_mode
, arglist
, target
,
5919 subtarget
, parity_optab
);
5924 case BUILT_IN_STRLEN
:
5925 target
= expand_builtin_strlen (arglist
, target
, target_mode
);
5930 case BUILT_IN_STRCPY
:
5931 target
= expand_builtin_strcpy (fndecl
, arglist
, target
, mode
);
5936 case BUILT_IN_STRNCPY
:
5937 target
= expand_builtin_strncpy (exp
, target
, mode
);
5942 case BUILT_IN_STPCPY
:
5943 target
= expand_builtin_stpcpy (exp
, target
, mode
);
5948 case BUILT_IN_STRCAT
:
5949 target
= expand_builtin_strcat (fndecl
, arglist
, target
, mode
);
5954 case BUILT_IN_STRNCAT
:
5955 target
= expand_builtin_strncat (arglist
, target
, mode
);
5960 case BUILT_IN_STRSPN
:
5961 target
= expand_builtin_strspn (arglist
, target
, mode
);
5966 case BUILT_IN_STRCSPN
:
5967 target
= expand_builtin_strcspn (arglist
, target
, mode
);
5972 case BUILT_IN_STRSTR
:
5973 target
= expand_builtin_strstr (arglist
, TREE_TYPE (exp
), target
, mode
);
5978 case BUILT_IN_STRPBRK
:
5979 target
= expand_builtin_strpbrk (arglist
, TREE_TYPE (exp
), target
, mode
);
5984 case BUILT_IN_INDEX
:
5985 case BUILT_IN_STRCHR
:
5986 target
= expand_builtin_strchr (arglist
, TREE_TYPE (exp
), target
, mode
);
5991 case BUILT_IN_RINDEX
:
5992 case BUILT_IN_STRRCHR
:
5993 target
= expand_builtin_strrchr (arglist
, TREE_TYPE (exp
), target
, mode
);
5998 case BUILT_IN_MEMCPY
:
5999 target
= expand_builtin_memcpy (exp
, target
, mode
);
6004 case BUILT_IN_MEMPCPY
:
6005 target
= expand_builtin_mempcpy (arglist
, TREE_TYPE (exp
), target
, mode
, /*endp=*/ 1);
6010 case BUILT_IN_MEMMOVE
:
6011 target
= expand_builtin_memmove (arglist
, TREE_TYPE (exp
), target
,
6017 case BUILT_IN_BCOPY
:
6018 target
= expand_builtin_bcopy (exp
);
6023 case BUILT_IN_MEMSET
:
6024 target
= expand_builtin_memset (arglist
, target
, mode
, exp
);
6029 case BUILT_IN_BZERO
:
6030 target
= expand_builtin_bzero (exp
);
6035 case BUILT_IN_STRCMP
:
6036 target
= expand_builtin_strcmp (exp
, target
, mode
);
6041 case BUILT_IN_STRNCMP
:
6042 target
= expand_builtin_strncmp (exp
, target
, mode
);
6048 case BUILT_IN_MEMCMP
:
6049 target
= expand_builtin_memcmp (exp
, arglist
, target
, mode
);
6054 case BUILT_IN_SETJMP
:
6055 /* This should have been lowered to the builtins below. */
6058 case BUILT_IN_SETJMP_SETUP
:
6059 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6060 and the receiver label. */
6061 if (validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
6063 rtx buf_addr
= expand_expr (TREE_VALUE (arglist
), subtarget
,
6064 VOIDmode
, EXPAND_NORMAL
);
6065 tree label
= TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist
)), 0);
6066 rtx label_r
= label_rtx (label
);
6068 /* This is copied from the handling of non-local gotos. */
6069 expand_builtin_setjmp_setup (buf_addr
, label_r
);
6070 nonlocal_goto_handler_labels
6071 = gen_rtx_EXPR_LIST (VOIDmode
, label_r
,
6072 nonlocal_goto_handler_labels
);
6073 /* ??? Do not let expand_label treat us as such since we would
6074 not want to be both on the list of non-local labels and on
6075 the list of forced labels. */
6076 FORCED_LABEL (label
) = 0;
6081 case BUILT_IN_SETJMP_DISPATCHER
:
6082 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6083 if (validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
6085 tree label
= TREE_OPERAND (TREE_VALUE (arglist
), 0);
6086 rtx label_r
= label_rtx (label
);
6088 /* Remove the dispatcher label from the list of non-local labels
6089 since the receiver labels have been added to it above. */
6090 remove_node_from_expr_list (label_r
, &nonlocal_goto_handler_labels
);
6095 case BUILT_IN_SETJMP_RECEIVER
:
6096 /* __builtin_setjmp_receiver is passed the receiver label. */
6097 if (validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
6099 tree label
= TREE_OPERAND (TREE_VALUE (arglist
), 0);
6100 rtx label_r
= label_rtx (label
);
6102 expand_builtin_setjmp_receiver (label_r
);
6107 /* __builtin_longjmp is passed a pointer to an array of five words.
6108 It's similar to the C library longjmp function but works with
6109 __builtin_setjmp above. */
6110 case BUILT_IN_LONGJMP
:
6111 if (validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6113 rtx buf_addr
= expand_expr (TREE_VALUE (arglist
), subtarget
,
6114 VOIDmode
, EXPAND_NORMAL
);
6115 rtx value
= expand_normal (TREE_VALUE (TREE_CHAIN (arglist
)));
6117 if (value
!= const1_rtx
)
6119 error ("%<__builtin_longjmp%> second argument must be 1");
6123 expand_builtin_longjmp (buf_addr
, value
);
6128 case BUILT_IN_NONLOCAL_GOTO
:
6129 target
= expand_builtin_nonlocal_goto (arglist
);
6134 /* This updates the setjmp buffer that is its argument with the value
6135 of the current stack pointer. */
6136 case BUILT_IN_UPDATE_SETJMP_BUF
:
6137 if (validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
6140 = expand_normal (TREE_VALUE (arglist
));
6142 expand_builtin_update_setjmp_buf (buf_addr
);
6148 expand_builtin_trap ();
6151 case BUILT_IN_PRINTF
:
6152 target
= expand_builtin_printf (exp
, target
, mode
, false);
6157 case BUILT_IN_PRINTF_UNLOCKED
:
6158 target
= expand_builtin_printf (exp
, target
, mode
, true);
6163 case BUILT_IN_FPUTS
:
6164 target
= expand_builtin_fputs (arglist
, target
, false);
6168 case BUILT_IN_FPUTS_UNLOCKED
:
6169 target
= expand_builtin_fputs (arglist
, target
, true);
6174 case BUILT_IN_FPRINTF
:
6175 target
= expand_builtin_fprintf (exp
, target
, mode
, false);
6180 case BUILT_IN_FPRINTF_UNLOCKED
:
6181 target
= expand_builtin_fprintf (exp
, target
, mode
, true);
6186 case BUILT_IN_SPRINTF
:
6187 target
= expand_builtin_sprintf (arglist
, target
, mode
);
6192 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
6193 target
= expand_builtin_signbit (exp
, target
);
6198 /* Various hooks for the DWARF 2 __throw routine. */
6199 case BUILT_IN_UNWIND_INIT
:
6200 expand_builtin_unwind_init ();
6202 case BUILT_IN_DWARF_CFA
:
6203 return virtual_cfa_rtx
;
6204 #ifdef DWARF2_UNWIND_INFO
6205 case BUILT_IN_DWARF_SP_COLUMN
:
6206 return expand_builtin_dwarf_sp_column ();
6207 case BUILT_IN_INIT_DWARF_REG_SIZES
:
6208 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist
));
6211 case BUILT_IN_FROB_RETURN_ADDR
:
6212 return expand_builtin_frob_return_addr (TREE_VALUE (arglist
));
6213 case BUILT_IN_EXTRACT_RETURN_ADDR
:
6214 return expand_builtin_extract_return_addr (TREE_VALUE (arglist
));
6215 case BUILT_IN_EH_RETURN
:
6216 expand_builtin_eh_return (TREE_VALUE (arglist
),
6217 TREE_VALUE (TREE_CHAIN (arglist
)));
6219 #ifdef EH_RETURN_DATA_REGNO
6220 case BUILT_IN_EH_RETURN_DATA_REGNO
:
6221 return expand_builtin_eh_return_data_regno (arglist
);
6223 case BUILT_IN_EXTEND_POINTER
:
6224 return expand_builtin_extend_pointer (TREE_VALUE (arglist
));
6226 case BUILT_IN_VA_START
:
6227 case BUILT_IN_STDARG_START
:
6228 return expand_builtin_va_start (arglist
);
6229 case BUILT_IN_VA_END
:
6230 return expand_builtin_va_end (arglist
);
6231 case BUILT_IN_VA_COPY
:
6232 return expand_builtin_va_copy (arglist
);
6233 case BUILT_IN_EXPECT
:
6234 return expand_builtin_expect (arglist
, target
);
6235 case BUILT_IN_PREFETCH
:
6236 expand_builtin_prefetch (arglist
);
6239 case BUILT_IN_PROFILE_FUNC_ENTER
:
6240 return expand_builtin_profile_func (false);
6241 case BUILT_IN_PROFILE_FUNC_EXIT
:
6242 return expand_builtin_profile_func (true);
6244 case BUILT_IN_INIT_TRAMPOLINE
:
6245 return expand_builtin_init_trampoline (arglist
);
6246 case BUILT_IN_ADJUST_TRAMPOLINE
:
6247 return expand_builtin_adjust_trampoline (arglist
);
6250 case BUILT_IN_EXECL
:
6251 case BUILT_IN_EXECV
:
6252 case BUILT_IN_EXECLP
:
6253 case BUILT_IN_EXECLE
:
6254 case BUILT_IN_EXECVP
:
6255 case BUILT_IN_EXECVE
:
6256 target
= expand_builtin_fork_or_exec (fndecl
, arglist
, target
, ignore
);
6261 case BUILT_IN_FETCH_AND_ADD_1
:
6262 case BUILT_IN_FETCH_AND_ADD_2
:
6263 case BUILT_IN_FETCH_AND_ADD_4
:
6264 case BUILT_IN_FETCH_AND_ADD_8
:
6265 case BUILT_IN_FETCH_AND_ADD_16
:
6266 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_ADD_1
);
6267 target
= expand_builtin_sync_operation (mode
, arglist
, PLUS
,
6268 false, target
, ignore
);
6273 case BUILT_IN_FETCH_AND_SUB_1
:
6274 case BUILT_IN_FETCH_AND_SUB_2
:
6275 case BUILT_IN_FETCH_AND_SUB_4
:
6276 case BUILT_IN_FETCH_AND_SUB_8
:
6277 case BUILT_IN_FETCH_AND_SUB_16
:
6278 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_SUB_1
);
6279 target
= expand_builtin_sync_operation (mode
, arglist
, MINUS
,
6280 false, target
, ignore
);
6285 case BUILT_IN_FETCH_AND_OR_1
:
6286 case BUILT_IN_FETCH_AND_OR_2
:
6287 case BUILT_IN_FETCH_AND_OR_4
:
6288 case BUILT_IN_FETCH_AND_OR_8
:
6289 case BUILT_IN_FETCH_AND_OR_16
:
6290 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_OR_1
);
6291 target
= expand_builtin_sync_operation (mode
, arglist
, IOR
,
6292 false, target
, ignore
);
6297 case BUILT_IN_FETCH_AND_AND_1
:
6298 case BUILT_IN_FETCH_AND_AND_2
:
6299 case BUILT_IN_FETCH_AND_AND_4
:
6300 case BUILT_IN_FETCH_AND_AND_8
:
6301 case BUILT_IN_FETCH_AND_AND_16
:
6302 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_AND_1
);
6303 target
= expand_builtin_sync_operation (mode
, arglist
, AND
,
6304 false, target
, ignore
);
6309 case BUILT_IN_FETCH_AND_XOR_1
:
6310 case BUILT_IN_FETCH_AND_XOR_2
:
6311 case BUILT_IN_FETCH_AND_XOR_4
:
6312 case BUILT_IN_FETCH_AND_XOR_8
:
6313 case BUILT_IN_FETCH_AND_XOR_16
:
6314 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_XOR_1
);
6315 target
= expand_builtin_sync_operation (mode
, arglist
, XOR
,
6316 false, target
, ignore
);
6321 case BUILT_IN_FETCH_AND_NAND_1
:
6322 case BUILT_IN_FETCH_AND_NAND_2
:
6323 case BUILT_IN_FETCH_AND_NAND_4
:
6324 case BUILT_IN_FETCH_AND_NAND_8
:
6325 case BUILT_IN_FETCH_AND_NAND_16
:
6326 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_FETCH_AND_NAND_1
);
6327 target
= expand_builtin_sync_operation (mode
, arglist
, NOT
,
6328 false, target
, ignore
);
6333 case BUILT_IN_ADD_AND_FETCH_1
:
6334 case BUILT_IN_ADD_AND_FETCH_2
:
6335 case BUILT_IN_ADD_AND_FETCH_4
:
6336 case BUILT_IN_ADD_AND_FETCH_8
:
6337 case BUILT_IN_ADD_AND_FETCH_16
:
6338 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ADD_AND_FETCH_1
);
6339 target
= expand_builtin_sync_operation (mode
, arglist
, PLUS
,
6340 true, target
, ignore
);
6345 case BUILT_IN_SUB_AND_FETCH_1
:
6346 case BUILT_IN_SUB_AND_FETCH_2
:
6347 case BUILT_IN_SUB_AND_FETCH_4
:
6348 case BUILT_IN_SUB_AND_FETCH_8
:
6349 case BUILT_IN_SUB_AND_FETCH_16
:
6350 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SUB_AND_FETCH_1
);
6351 target
= expand_builtin_sync_operation (mode
, arglist
, MINUS
,
6352 true, target
, ignore
);
6357 case BUILT_IN_OR_AND_FETCH_1
:
6358 case BUILT_IN_OR_AND_FETCH_2
:
6359 case BUILT_IN_OR_AND_FETCH_4
:
6360 case BUILT_IN_OR_AND_FETCH_8
:
6361 case BUILT_IN_OR_AND_FETCH_16
:
6362 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_OR_AND_FETCH_1
);
6363 target
= expand_builtin_sync_operation (mode
, arglist
, IOR
,
6364 true, target
, ignore
);
6369 case BUILT_IN_AND_AND_FETCH_1
:
6370 case BUILT_IN_AND_AND_FETCH_2
:
6371 case BUILT_IN_AND_AND_FETCH_4
:
6372 case BUILT_IN_AND_AND_FETCH_8
:
6373 case BUILT_IN_AND_AND_FETCH_16
:
6374 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_AND_AND_FETCH_1
);
6375 target
= expand_builtin_sync_operation (mode
, arglist
, AND
,
6376 true, target
, ignore
);
6381 case BUILT_IN_XOR_AND_FETCH_1
:
6382 case BUILT_IN_XOR_AND_FETCH_2
:
6383 case BUILT_IN_XOR_AND_FETCH_4
:
6384 case BUILT_IN_XOR_AND_FETCH_8
:
6385 case BUILT_IN_XOR_AND_FETCH_16
:
6386 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_XOR_AND_FETCH_1
);
6387 target
= expand_builtin_sync_operation (mode
, arglist
, XOR
,
6388 true, target
, ignore
);
6393 case BUILT_IN_NAND_AND_FETCH_1
:
6394 case BUILT_IN_NAND_AND_FETCH_2
:
6395 case BUILT_IN_NAND_AND_FETCH_4
:
6396 case BUILT_IN_NAND_AND_FETCH_8
:
6397 case BUILT_IN_NAND_AND_FETCH_16
:
6398 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_NAND_AND_FETCH_1
);
6399 target
= expand_builtin_sync_operation (mode
, arglist
, NOT
,
6400 true, target
, ignore
);
6405 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1
:
6406 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2
:
6407 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4
:
6408 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8
:
6409 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16
:
6410 if (mode
== VOIDmode
)
6411 mode
= TYPE_MODE (boolean_type_node
);
6412 if (!target
|| !register_operand (target
, mode
))
6413 target
= gen_reg_rtx (mode
);
6415 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_BOOL_COMPARE_AND_SWAP_1
);
6416 target
= expand_builtin_compare_and_swap (mode
, arglist
, true, target
);
6421 case BUILT_IN_VAL_COMPARE_AND_SWAP_1
:
6422 case BUILT_IN_VAL_COMPARE_AND_SWAP_2
:
6423 case BUILT_IN_VAL_COMPARE_AND_SWAP_4
:
6424 case BUILT_IN_VAL_COMPARE_AND_SWAP_8
:
6425 case BUILT_IN_VAL_COMPARE_AND_SWAP_16
:
6426 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_VAL_COMPARE_AND_SWAP_1
);
6427 target
= expand_builtin_compare_and_swap (mode
, arglist
, false, target
);
6432 case BUILT_IN_LOCK_TEST_AND_SET_1
:
6433 case BUILT_IN_LOCK_TEST_AND_SET_2
:
6434 case BUILT_IN_LOCK_TEST_AND_SET_4
:
6435 case BUILT_IN_LOCK_TEST_AND_SET_8
:
6436 case BUILT_IN_LOCK_TEST_AND_SET_16
:
6437 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_LOCK_TEST_AND_SET_1
);
6438 target
= expand_builtin_lock_test_and_set (mode
, arglist
, target
);
6443 case BUILT_IN_LOCK_RELEASE_1
:
6444 case BUILT_IN_LOCK_RELEASE_2
:
6445 case BUILT_IN_LOCK_RELEASE_4
:
6446 case BUILT_IN_LOCK_RELEASE_8
:
6447 case BUILT_IN_LOCK_RELEASE_16
:
6448 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_LOCK_RELEASE_1
);
6449 expand_builtin_lock_release (mode
, arglist
);
6452 case BUILT_IN_SYNCHRONIZE
:
6453 expand_builtin_synchronize ();
6456 case BUILT_IN_OBJECT_SIZE
:
6457 return expand_builtin_object_size (exp
);
6459 case BUILT_IN_MEMCPY_CHK
:
6460 case BUILT_IN_MEMPCPY_CHK
:
6461 case BUILT_IN_MEMMOVE_CHK
:
6462 case BUILT_IN_MEMSET_CHK
:
6463 target
= expand_builtin_memory_chk (exp
, target
, mode
, fcode
);
6468 case BUILT_IN_STRCPY_CHK
:
6469 case BUILT_IN_STPCPY_CHK
:
6470 case BUILT_IN_STRNCPY_CHK
:
6471 case BUILT_IN_STRCAT_CHK
:
6472 case BUILT_IN_STRNCAT_CHK
:
6473 case BUILT_IN_SNPRINTF_CHK
:
6474 case BUILT_IN_VSNPRINTF_CHK
:
6475 maybe_emit_chk_warning (exp
, fcode
);
6478 case BUILT_IN_SPRINTF_CHK
:
6479 case BUILT_IN_VSPRINTF_CHK
:
6480 maybe_emit_sprintf_chk_warning (exp
, fcode
);
6483 default: /* just do library call, if unknown builtin */
6487 /* The switch statement above can drop through to cause the function
6488 to be called normally. */
6489 return expand_call (exp
, target
, ignore
);
6492 /* Determine whether a tree node represents a call to a built-in
6493 function. If the tree T is a call to a built-in function with
6494 the right number of arguments of the appropriate types, return
6495 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6496 Otherwise the return value is END_BUILTINS. */
6498 enum built_in_function
6499 builtin_mathfn_code (tree t
)
6501 tree fndecl
, arglist
, parmlist
;
6502 tree argtype
, parmtype
;
6504 if (TREE_CODE (t
) != CALL_EXPR
6505 || TREE_CODE (TREE_OPERAND (t
, 0)) != ADDR_EXPR
)
6506 return END_BUILTINS
;
6508 fndecl
= get_callee_fndecl (t
);
6509 if (fndecl
== NULL_TREE
6510 || TREE_CODE (fndecl
) != FUNCTION_DECL
6511 || ! DECL_BUILT_IN (fndecl
)
6512 || DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
6513 return END_BUILTINS
;
6515 arglist
= TREE_OPERAND (t
, 1);
6516 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
6517 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
6519 /* If a function doesn't take a variable number of arguments,
6520 the last element in the list will have type `void'. */
6521 parmtype
= TREE_VALUE (parmlist
);
6522 if (VOID_TYPE_P (parmtype
))
6525 return END_BUILTINS
;
6526 return DECL_FUNCTION_CODE (fndecl
);
6530 return END_BUILTINS
;
6532 argtype
= TREE_TYPE (TREE_VALUE (arglist
));
6534 if (SCALAR_FLOAT_TYPE_P (parmtype
))
6536 if (! SCALAR_FLOAT_TYPE_P (argtype
))
6537 return END_BUILTINS
;
6539 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
6541 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
6542 return END_BUILTINS
;
6544 else if (POINTER_TYPE_P (parmtype
))
6546 if (! POINTER_TYPE_P (argtype
))
6547 return END_BUILTINS
;
6549 else if (INTEGRAL_TYPE_P (parmtype
))
6551 if (! INTEGRAL_TYPE_P (argtype
))
6552 return END_BUILTINS
;
6555 return END_BUILTINS
;
6557 arglist
= TREE_CHAIN (arglist
);
6560 /* Variable-length argument list. */
6561 return DECL_FUNCTION_CODE (fndecl
);
6564 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6565 constant. ARGLIST is the argument list of the call. */
6568 fold_builtin_constant_p (tree arglist
)
6573 arglist
= TREE_VALUE (arglist
);
6575 /* We return 1 for a numeric type that's known to be a constant
6576 value at compile-time or for an aggregate type that's a
6577 literal constant. */
6578 STRIP_NOPS (arglist
);
6580 /* If we know this is a constant, emit the constant of one. */
6581 if (CONSTANT_CLASS_P (arglist
)
6582 || (TREE_CODE (arglist
) == CONSTRUCTOR
6583 && TREE_CONSTANT (arglist
)))
6584 return integer_one_node
;
6585 if (TREE_CODE (arglist
) == ADDR_EXPR
)
6587 tree op
= TREE_OPERAND (arglist
, 0);
6588 if (TREE_CODE (op
) == STRING_CST
6589 || (TREE_CODE (op
) == ARRAY_REF
6590 && integer_zerop (TREE_OPERAND (op
, 1))
6591 && TREE_CODE (TREE_OPERAND (op
, 0)) == STRING_CST
))
6592 return integer_one_node
;
6595 /* If this expression has side effects, show we don't know it to be a
6596 constant. Likewise if it's a pointer or aggregate type since in
6597 those case we only want literals, since those are only optimized
6598 when generating RTL, not later.
6599 And finally, if we are compiling an initializer, not code, we
6600 need to return a definite result now; there's not going to be any
6601 more optimization done. */
6602 if (TREE_SIDE_EFFECTS (arglist
)
6603 || AGGREGATE_TYPE_P (TREE_TYPE (arglist
))
6604 || POINTER_TYPE_P (TREE_TYPE (arglist
))
6606 || folding_initializer
)
6607 return integer_zero_node
;
6612 /* Fold a call to __builtin_expect, if we expect that a comparison against
6613 the argument will fold to a constant. In practice, this means a true
6614 constant or the address of a non-weak symbol. ARGLIST is the argument
6615 list of the call. */
6618 fold_builtin_expect (tree arglist
)
6625 arg
= TREE_VALUE (arglist
);
6627 /* If the argument isn't invariant, then there's nothing we can do. */
6628 if (!TREE_INVARIANT (arg
))
6631 /* If we're looking at an address of a weak decl, then do not fold. */
6634 if (TREE_CODE (inner
) == ADDR_EXPR
)
6638 inner
= TREE_OPERAND (inner
, 0);
6640 while (TREE_CODE (inner
) == COMPONENT_REF
6641 || TREE_CODE (inner
) == ARRAY_REF
);
6642 if (DECL_P (inner
) && DECL_WEAK (inner
))
6646 /* Otherwise, ARG already has the proper type for the return value. */
6650 /* Fold a call to __builtin_classify_type. */
6653 fold_builtin_classify_type (tree arglist
)
6656 return build_int_cst (NULL_TREE
, no_type_class
);
6658 return build_int_cst (NULL_TREE
,
6659 type_to_class (TREE_TYPE (TREE_VALUE (arglist
))));
6662 /* Fold a call to __builtin_strlen. */
6665 fold_builtin_strlen (tree arglist
)
6667 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
6671 tree len
= c_strlen (TREE_VALUE (arglist
), 0);
6675 /* Convert from the internal "sizetype" type to "size_t". */
6677 len
= fold_convert (size_type_node
, len
);
6685 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6688 fold_builtin_inf (tree type
, int warn
)
6690 REAL_VALUE_TYPE real
;
6692 /* __builtin_inff is intended to be usable to define INFINITY on all
6693 targets. If an infinity is not available, INFINITY expands "to a
6694 positive constant of type float that overflows at translation
6695 time", footnote "In this case, using INFINITY will violate the
6696 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6697 Thus we pedwarn to ensure this constraint violation is
6699 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
6700 pedwarn ("target format does not support infinity");
6703 return build_real (type
, real
);
6706 /* Fold a call to __builtin_nan or __builtin_nans. */
6709 fold_builtin_nan (tree arglist
, tree type
, int quiet
)
6711 REAL_VALUE_TYPE real
;
6714 if (!validate_arglist (arglist
, POINTER_TYPE
, VOID_TYPE
))
6716 str
= c_getstr (TREE_VALUE (arglist
));
6720 if (!real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
6723 return build_real (type
, real
);
6726 /* Return true if the floating point expression T has an integer value.
6727 We also allow +Inf, -Inf and NaN to be considered integer values. */
6730 integer_valued_real_p (tree t
)
6732 switch (TREE_CODE (t
))
6739 case NON_LVALUE_EXPR
:
6740 return integer_valued_real_p (TREE_OPERAND (t
, 0));
6743 case GIMPLE_MODIFY_STMT
:
6745 return integer_valued_real_p (GENERIC_TREE_OPERAND (t
, 1));
6752 return integer_valued_real_p (TREE_OPERAND (t
, 0))
6753 && integer_valued_real_p (TREE_OPERAND (t
, 1));
6756 return integer_valued_real_p (TREE_OPERAND (t
, 1))
6757 && integer_valued_real_p (TREE_OPERAND (t
, 2));
6760 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
6764 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
6765 if (TREE_CODE (type
) == INTEGER_TYPE
)
6767 if (TREE_CODE (type
) == REAL_TYPE
)
6768 return integer_valued_real_p (TREE_OPERAND (t
, 0));
6773 switch (builtin_mathfn_code (t
))
6775 CASE_FLT_FN (BUILT_IN_CEIL
):
6776 CASE_FLT_FN (BUILT_IN_FLOOR
):
6777 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
6778 CASE_FLT_FN (BUILT_IN_RINT
):
6779 CASE_FLT_FN (BUILT_IN_ROUND
):
6780 CASE_FLT_FN (BUILT_IN_TRUNC
):
6783 CASE_FLT_FN (BUILT_IN_FMIN
):
6784 CASE_FLT_FN (BUILT_IN_FMAX
):
6785 return integer_valued_real_p (TREE_VALUE (TREE_OPERAND (t
, 1)))
6786 && integer_valued_real_p (TREE_VALUE (TREE_CHAIN (TREE_OPERAND (t
, 1))));
6799 /* EXP is assumed to be builtin call where truncation can be propagated
6800 across (for instance floor((double)f) == (double)floorf (f).
6801 Do the transformation. */
6804 fold_trunc_transparent_mathfn (tree fndecl
, tree arglist
)
6806 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
6809 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6812 arg
= TREE_VALUE (arglist
);
6813 /* Integer rounding functions are idempotent. */
6814 if (fcode
== builtin_mathfn_code (arg
))
6817 /* If argument is already integer valued, and we don't need to worry
6818 about setting errno, there's no need to perform rounding. */
6819 if (! flag_errno_math
&& integer_valued_real_p (arg
))
6824 tree arg0
= strip_float_extensions (arg
);
6825 tree ftype
= TREE_TYPE (TREE_TYPE (fndecl
));
6826 tree newtype
= TREE_TYPE (arg0
);
6829 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
6830 && (decl
= mathfn_built_in (newtype
, fcode
)))
6833 build_tree_list (NULL_TREE
, fold_convert (newtype
, arg0
));
6834 return fold_convert (ftype
,
6835 build_function_call_expr (decl
, arglist
));
6841 /* EXP is assumed to be builtin call which can narrow the FP type of
6842 the argument, for instance lround((double)f) -> lroundf (f). */
6845 fold_fixed_mathfn (tree fndecl
, tree arglist
)
6847 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
6850 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
6853 arg
= TREE_VALUE (arglist
);
6855 /* If argument is already integer valued, and we don't need to worry
6856 about setting errno, there's no need to perform rounding. */
6857 if (! flag_errno_math
&& integer_valued_real_p (arg
))
6858 return fold_build1 (FIX_TRUNC_EXPR
, TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
6862 tree ftype
= TREE_TYPE (arg
);
6863 tree arg0
= strip_float_extensions (arg
);
6864 tree newtype
= TREE_TYPE (arg0
);
6867 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
6868 && (decl
= mathfn_built_in (newtype
, fcode
)))
6871 build_tree_list (NULL_TREE
, fold_convert (newtype
, arg0
));
6872 return build_function_call_expr (decl
, arglist
);
6876 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6877 sizeof (long long) == sizeof (long). */
6878 if (TYPE_PRECISION (long_long_integer_type_node
)
6879 == TYPE_PRECISION (long_integer_type_node
))
6881 tree newfn
= NULL_TREE
;
6884 CASE_FLT_FN (BUILT_IN_LLCEIL
):
6885 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
6888 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
6889 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
6892 CASE_FLT_FN (BUILT_IN_LLROUND
):
6893 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
6896 CASE_FLT_FN (BUILT_IN_LLRINT
):
6897 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
6906 tree newcall
= build_function_call_expr (newfn
, arglist
);
6907 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
6914 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6915 is the argument list, TYPE is the return type and FNDECL is the
6916 original function DECL. Return NULL_TREE if no if no simplification
6920 fold_builtin_cabs (tree arglist
, tree type
, tree fndecl
)
6924 if (!arglist
|| TREE_CHAIN (arglist
))
6927 arg
= TREE_VALUE (arglist
);
6928 if (TREE_CODE (TREE_TYPE (arg
)) != COMPLEX_TYPE
6929 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
6932 /* Evaluate cabs of a constant at compile-time. */
6933 if (flag_unsafe_math_optimizations
6934 && TREE_CODE (arg
) == COMPLEX_CST
6935 && TREE_CODE (TREE_REALPART (arg
)) == REAL_CST
6936 && TREE_CODE (TREE_IMAGPART (arg
)) == REAL_CST
6937 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg
))
6938 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg
)))
6940 REAL_VALUE_TYPE r
, i
;
6942 r
= TREE_REAL_CST (TREE_REALPART (arg
));
6943 i
= TREE_REAL_CST (TREE_IMAGPART (arg
));
6945 real_arithmetic (&r
, MULT_EXPR
, &r
, &r
);
6946 real_arithmetic (&i
, MULT_EXPR
, &i
, &i
);
6947 real_arithmetic (&r
, PLUS_EXPR
, &r
, &i
);
6948 if (real_sqrt (&r
, TYPE_MODE (type
), &r
)
6949 || ! flag_trapping_math
)
6950 return build_real (type
, r
);
6953 /* If either part is zero, cabs is fabs of the other. */
6954 if (TREE_CODE (arg
) == COMPLEX_EXPR
6955 && real_zerop (TREE_OPERAND (arg
, 0)))
6956 return fold_build1 (ABS_EXPR
, type
, TREE_OPERAND (arg
, 1));
6957 if (TREE_CODE (arg
) == COMPLEX_EXPR
6958 && real_zerop (TREE_OPERAND (arg
, 1)))
6959 return fold_build1 (ABS_EXPR
, type
, TREE_OPERAND (arg
, 0));
6961 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
6962 if (TREE_CODE (arg
) == NEGATE_EXPR
6963 || TREE_CODE (arg
) == CONJ_EXPR
)
6965 tree arglist
= build_tree_list (NULL_TREE
, TREE_OPERAND (arg
, 0));
6966 return build_function_call_expr (fndecl
, arglist
);
6969 /* Don't do this when optimizing for size. */
6970 if (flag_unsafe_math_optimizations
6971 && optimize
&& !optimize_size
)
6973 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
6975 if (sqrtfn
!= NULL_TREE
)
6977 tree rpart
, ipart
, result
, arglist
;
6979 arg
= builtin_save_expr (arg
);
6981 rpart
= fold_build1 (REALPART_EXPR
, type
, arg
);
6982 ipart
= fold_build1 (IMAGPART_EXPR
, type
, arg
);
6984 rpart
= builtin_save_expr (rpart
);
6985 ipart
= builtin_save_expr (ipart
);
6987 result
= fold_build2 (PLUS_EXPR
, type
,
6988 fold_build2 (MULT_EXPR
, type
,
6990 fold_build2 (MULT_EXPR
, type
,
6993 arglist
= build_tree_list (NULL_TREE
, result
);
6994 return build_function_call_expr (sqrtfn
, arglist
);
7001 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
7002 NULL_TREE if no simplification can be made. */
7005 fold_builtin_sqrt (tree arglist
, tree type
)
7008 enum built_in_function fcode
;
7009 tree arg
= TREE_VALUE (arglist
);
7011 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7014 /* Optimize sqrt of constant value. */
7015 if (TREE_CODE (arg
) == REAL_CST
7016 && ! TREE_CONSTANT_OVERFLOW (arg
))
7018 REAL_VALUE_TYPE r
, x
;
7020 x
= TREE_REAL_CST (arg
);
7021 if (real_sqrt (&r
, TYPE_MODE (type
), &x
)
7022 || (!flag_trapping_math
&& !flag_errno_math
))
7023 return build_real (type
, r
);
7026 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7027 fcode
= builtin_mathfn_code (arg
);
7028 if (flag_unsafe_math_optimizations
&& BUILTIN_EXPONENT_P (fcode
))
7030 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
7031 arg
= fold_build2 (MULT_EXPR
, type
,
7032 TREE_VALUE (TREE_OPERAND (arg
, 1)),
7033 build_real (type
, dconsthalf
));
7034 arglist
= build_tree_list (NULL_TREE
, arg
);
7035 return build_function_call_expr (expfn
, arglist
);
7038 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7039 if (flag_unsafe_math_optimizations
&& BUILTIN_ROOT_P (fcode
))
7041 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7045 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7047 /* The inner root was either sqrt or cbrt. */
7048 REAL_VALUE_TYPE dconstroot
=
7049 BUILTIN_SQRT_P (fcode
) ? dconsthalf
: dconstthird
;
7051 /* Adjust for the outer root. */
7052 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7053 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7054 tree_root
= build_real (type
, dconstroot
);
7055 arglist
= tree_cons (NULL_TREE
, arg0
,
7056 build_tree_list (NULL_TREE
, tree_root
));
7057 return build_function_call_expr (powfn
, arglist
);
7061 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7062 if (flag_unsafe_math_optimizations
7063 && (fcode
== BUILT_IN_POW
7064 || fcode
== BUILT_IN_POWF
7065 || fcode
== BUILT_IN_POWL
))
7067 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
7068 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7069 tree arg1
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg
, 1)));
7071 if (!tree_expr_nonnegative_p (arg0
))
7072 arg0
= build1 (ABS_EXPR
, type
, arg0
);
7073 narg1
= fold_build2 (MULT_EXPR
, type
, arg1
,
7074 build_real (type
, dconsthalf
));
7075 arglist
= tree_cons (NULL_TREE
, arg0
,
7076 build_tree_list (NULL_TREE
, narg1
));
7077 return build_function_call_expr (powfn
, arglist
);
7083 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
7084 NULL_TREE if no simplification can be made. */
7086 fold_builtin_cbrt (tree arglist
, tree type
)
7088 tree arg
= TREE_VALUE (arglist
);
7089 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
7092 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7095 /* Calculate the result when the argument is a constant. */
7096 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cbrt
, NULL
, NULL
, 0)))
7099 if (flag_unsafe_math_optimizations
)
7101 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7102 if (BUILTIN_EXPONENT_P (fcode
))
7104 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
7105 const REAL_VALUE_TYPE third_trunc
=
7106 real_value_truncate (TYPE_MODE (type
), dconstthird
);
7107 arg
= fold_build2 (MULT_EXPR
, type
,
7108 TREE_VALUE (TREE_OPERAND (arg
, 1)),
7109 build_real (type
, third_trunc
));
7110 arglist
= build_tree_list (NULL_TREE
, arg
);
7111 return build_function_call_expr (expfn
, arglist
);
7114 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7115 if (BUILTIN_SQRT_P (fcode
))
7117 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7121 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7123 REAL_VALUE_TYPE dconstroot
= dconstthird
;
7125 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7126 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7127 tree_root
= build_real (type
, dconstroot
);
7128 arglist
= tree_cons (NULL_TREE
, arg0
,
7129 build_tree_list (NULL_TREE
, tree_root
));
7130 return build_function_call_expr (powfn
, arglist
);
7134 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7135 if (BUILTIN_CBRT_P (fcode
))
7137 tree arg0
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7138 if (tree_expr_nonnegative_p (arg0
))
7140 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7145 REAL_VALUE_TYPE dconstroot
;
7147 real_arithmetic (&dconstroot
, MULT_EXPR
, &dconstthird
, &dconstthird
);
7148 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7149 tree_root
= build_real (type
, dconstroot
);
7150 arglist
= tree_cons (NULL_TREE
, arg0
,
7151 build_tree_list (NULL_TREE
, tree_root
));
7152 return build_function_call_expr (powfn
, arglist
);
7157 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7158 if (fcode
== BUILT_IN_POW
|| fcode
== BUILT_IN_POWF
7159 || fcode
== BUILT_IN_POWL
)
7161 tree arg00
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7162 tree arg01
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg
, 1)));
7163 if (tree_expr_nonnegative_p (arg00
))
7165 tree powfn
= TREE_OPERAND (TREE_OPERAND (arg
, 0), 0);
7166 const REAL_VALUE_TYPE dconstroot
7167 = real_value_truncate (TYPE_MODE (type
), dconstthird
);
7168 tree narg01
= fold_build2 (MULT_EXPR
, type
, arg01
,
7169 build_real (type
, dconstroot
));
7170 arglist
= tree_cons (NULL_TREE
, arg00
,
7171 build_tree_list (NULL_TREE
, narg01
));
7172 return build_function_call_expr (powfn
, arglist
);
7179 /* Fold function call to builtin cos, cosf, or cosl. Return
7180 NULL_TREE if no simplification can be made. */
7182 fold_builtin_cos (tree arglist
, tree type
, tree fndecl
)
7184 tree arg
= TREE_VALUE (arglist
);
7187 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7190 /* Calculate the result when the argument is a constant. */
7191 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cos
, NULL
, NULL
, 0)))
7194 /* Optimize cos(-x) into cos (x). */
7195 if ((narg
= fold_strip_sign_ops (arg
)))
7196 return build_function_call_expr (fndecl
,
7197 build_tree_list (NULL_TREE
, narg
));
7202 /* Fold function call to builtin cosh, coshf, or coshl. Return
7203 NULL_TREE if no simplification can be made. */
7205 fold_builtin_cosh (tree arglist
, tree type
, tree fndecl
)
7207 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7209 tree arg
= TREE_VALUE (arglist
);
7212 /* Calculate the result when the argument is a constant. */
7213 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cosh
, NULL
, NULL
, 0)))
7216 /* Optimize cosh(-x) into cosh (x). */
7217 if ((narg
= fold_strip_sign_ops (arg
)))
7218 return build_function_call_expr (fndecl
,
7219 build_tree_list (NULL_TREE
, narg
));
7225 /* Fold function call to builtin tan, tanf, or tanl. Return
7226 NULL_TREE if no simplification can be made. */
7228 fold_builtin_tan (tree arglist
, tree type
)
7230 enum built_in_function fcode
;
7231 tree arg
= TREE_VALUE (arglist
);
7234 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7237 /* Calculate the result when the argument is a constant. */
7238 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_tan
, NULL
, NULL
, 0)))
7241 /* Optimize tan(atan(x)) = x. */
7242 fcode
= builtin_mathfn_code (arg
);
7243 if (flag_unsafe_math_optimizations
7244 && (fcode
== BUILT_IN_ATAN
7245 || fcode
== BUILT_IN_ATANF
7246 || fcode
== BUILT_IN_ATANL
))
7247 return TREE_VALUE (TREE_OPERAND (arg
, 1));
7252 /* Fold function call to builtin trunc, truncf or truncl. Return
7253 NULL_TREE if no simplification can be made. */
7256 fold_builtin_trunc (tree fndecl
, tree arglist
)
7260 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7263 /* Optimize trunc of constant value. */
7264 arg
= TREE_VALUE (arglist
);
7265 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7267 REAL_VALUE_TYPE r
, x
;
7268 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7270 x
= TREE_REAL_CST (arg
);
7271 real_trunc (&r
, TYPE_MODE (type
), &x
);
7272 return build_real (type
, r
);
7275 return fold_trunc_transparent_mathfn (fndecl
, arglist
);
7278 /* Fold function call to builtin floor, floorf or floorl. Return
7279 NULL_TREE if no simplification can be made. */
7282 fold_builtin_floor (tree fndecl
, tree arglist
)
7286 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7289 /* Optimize floor of constant value. */
7290 arg
= TREE_VALUE (arglist
);
7291 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7295 x
= TREE_REAL_CST (arg
);
7296 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7298 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7301 real_floor (&r
, TYPE_MODE (type
), &x
);
7302 return build_real (type
, r
);
7306 /* Fold floor (x) where x is nonnegative to trunc (x). */
7307 if (tree_expr_nonnegative_p (arg
))
7309 tree truncfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_TRUNC
);
7311 return build_function_call_expr (truncfn
, arglist
);
7314 return fold_trunc_transparent_mathfn (fndecl
, arglist
);
7317 /* Fold function call to builtin ceil, ceilf or ceill. Return
7318 NULL_TREE if no simplification can be made. */
7321 fold_builtin_ceil (tree fndecl
, tree arglist
)
7325 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7328 /* Optimize ceil of constant value. */
7329 arg
= TREE_VALUE (arglist
);
7330 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7334 x
= TREE_REAL_CST (arg
);
7335 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7337 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7340 real_ceil (&r
, TYPE_MODE (type
), &x
);
7341 return build_real (type
, r
);
7345 return fold_trunc_transparent_mathfn (fndecl
, arglist
);
7348 /* Fold function call to builtin round, roundf or roundl. Return
7349 NULL_TREE if no simplification can be made. */
7352 fold_builtin_round (tree fndecl
, tree arglist
)
7356 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7359 /* Optimize round of constant value. */
7360 arg
= TREE_VALUE (arglist
);
7361 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7365 x
= TREE_REAL_CST (arg
);
7366 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7368 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7371 real_round (&r
, TYPE_MODE (type
), &x
);
7372 return build_real (type
, r
);
7376 return fold_trunc_transparent_mathfn (fndecl
, arglist
);
7379 /* Fold function call to builtin lround, lroundf or lroundl (or the
7380 corresponding long long versions) and other rounding functions.
7381 Return NULL_TREE if no simplification can be made. */
7384 fold_builtin_int_roundingfn (tree fndecl
, tree arglist
)
7388 if (! validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7391 /* Optimize lround of constant value. */
7392 arg
= TREE_VALUE (arglist
);
7393 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7395 const REAL_VALUE_TYPE x
= TREE_REAL_CST (arg
);
7397 if (! REAL_VALUE_ISNAN (x
) && ! REAL_VALUE_ISINF (x
))
7399 tree itype
= TREE_TYPE (TREE_TYPE (fndecl
));
7400 tree ftype
= TREE_TYPE (arg
), result
;
7401 HOST_WIDE_INT hi
, lo
;
7404 switch (DECL_FUNCTION_CODE (fndecl
))
7406 CASE_FLT_FN (BUILT_IN_LFLOOR
):
7407 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7408 real_floor (&r
, TYPE_MODE (ftype
), &x
);
7411 CASE_FLT_FN (BUILT_IN_LCEIL
):
7412 CASE_FLT_FN (BUILT_IN_LLCEIL
):
7413 real_ceil (&r
, TYPE_MODE (ftype
), &x
);
7416 CASE_FLT_FN (BUILT_IN_LROUND
):
7417 CASE_FLT_FN (BUILT_IN_LLROUND
):
7418 real_round (&r
, TYPE_MODE (ftype
), &x
);
7425 REAL_VALUE_TO_INT (&lo
, &hi
, r
);
7426 result
= build_int_cst_wide (NULL_TREE
, lo
, hi
);
7427 if (int_fits_type_p (result
, itype
))
7428 return fold_convert (itype
, result
);
7432 switch (DECL_FUNCTION_CODE (fndecl
))
7434 CASE_FLT_FN (BUILT_IN_LFLOOR
):
7435 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7436 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7437 if (tree_expr_nonnegative_p (arg
))
7438 return fold_build1 (FIX_TRUNC_EXPR
, TREE_TYPE (TREE_TYPE (fndecl
)),
7444 return fold_fixed_mathfn (fndecl
, arglist
);
7447 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7448 and their long and long long variants (i.e. ffsl and ffsll).
7449 Return NULL_TREE if no simplification can be made. */
7452 fold_builtin_bitop (tree fndecl
, tree arglist
)
7456 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
7459 /* Optimize for constant argument. */
7460 arg
= TREE_VALUE (arglist
);
7461 if (TREE_CODE (arg
) == INTEGER_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7463 HOST_WIDE_INT hi
, width
, result
;
7464 unsigned HOST_WIDE_INT lo
;
7467 type
= TREE_TYPE (arg
);
7468 width
= TYPE_PRECISION (type
);
7469 lo
= TREE_INT_CST_LOW (arg
);
7471 /* Clear all the bits that are beyond the type's precision. */
7472 if (width
> HOST_BITS_PER_WIDE_INT
)
7474 hi
= TREE_INT_CST_HIGH (arg
);
7475 if (width
< 2 * HOST_BITS_PER_WIDE_INT
)
7476 hi
&= ~((HOST_WIDE_INT
) (-1) >> (width
- HOST_BITS_PER_WIDE_INT
));
7481 if (width
< HOST_BITS_PER_WIDE_INT
)
7482 lo
&= ~((unsigned HOST_WIDE_INT
) (-1) << width
);
7485 switch (DECL_FUNCTION_CODE (fndecl
))
7487 CASE_INT_FN (BUILT_IN_FFS
):
7489 result
= exact_log2 (lo
& -lo
) + 1;
7491 result
= HOST_BITS_PER_WIDE_INT
+ exact_log2 (hi
& -hi
) + 1;
7496 CASE_INT_FN (BUILT_IN_CLZ
):
7498 result
= width
- floor_log2 (hi
) - 1 - HOST_BITS_PER_WIDE_INT
;
7500 result
= width
- floor_log2 (lo
) - 1;
7501 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
7505 CASE_INT_FN (BUILT_IN_CTZ
):
7507 result
= exact_log2 (lo
& -lo
);
7509 result
= HOST_BITS_PER_WIDE_INT
+ exact_log2 (hi
& -hi
);
7510 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
7514 CASE_INT_FN (BUILT_IN_POPCOUNT
):
7517 result
++, lo
&= lo
- 1;
7519 result
++, hi
&= hi
- 1;
7522 CASE_INT_FN (BUILT_IN_PARITY
):
7525 result
++, lo
&= lo
- 1;
7527 result
++, hi
&= hi
- 1;
7535 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), result
);
7541 /* Fold function call to builtin_bswap and the long and long long
7542 variants. Return NULL_TREE if no simplification can be made. */
7544 fold_builtin_bswap (tree fndecl
, tree arglist
)
7548 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
7551 /* Optimize constant value. */
7552 arg
= TREE_VALUE (arglist
);
7553 if (TREE_CODE (arg
) == INTEGER_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
7555 HOST_WIDE_INT hi
, width
, r_hi
= 0;
7556 unsigned HOST_WIDE_INT lo
, r_lo
= 0;
7559 type
= TREE_TYPE (arg
);
7560 width
= TYPE_PRECISION (type
);
7561 lo
= TREE_INT_CST_LOW (arg
);
7562 hi
= TREE_INT_CST_HIGH (arg
);
7564 switch (DECL_FUNCTION_CODE (fndecl
))
7566 case BUILT_IN_BSWAP32
:
7567 case BUILT_IN_BSWAP64
:
7571 for (s
= 0; s
< width
; s
+= 8)
7573 int d
= width
- s
- 8;
7574 unsigned HOST_WIDE_INT byte
;
7576 if (s
< HOST_BITS_PER_WIDE_INT
)
7577 byte
= (lo
>> s
) & 0xff;
7579 byte
= (hi
>> (s
- HOST_BITS_PER_WIDE_INT
)) & 0xff;
7581 if (d
< HOST_BITS_PER_WIDE_INT
)
7584 r_hi
|= byte
<< (d
- HOST_BITS_PER_WIDE_INT
);
7594 if (width
< HOST_BITS_PER_WIDE_INT
)
7595 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), r_lo
);
7597 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl
)), r_lo
, r_hi
);
7602 /* Return true if EXPR is the real constant contained in VALUE. */
7605 real_dconstp (tree expr
, const REAL_VALUE_TYPE
*value
)
7609 return ((TREE_CODE (expr
) == REAL_CST
7610 && ! TREE_CONSTANT_OVERFLOW (expr
)
7611 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr
), *value
))
7612 || (TREE_CODE (expr
) == COMPLEX_CST
7613 && real_dconstp (TREE_REALPART (expr
), value
)
7614 && real_zerop (TREE_IMAGPART (expr
))));
7617 /* A subroutine of fold_builtin to fold the various logarithmic
7618 functions. Return NULL_TREE if no simplification can me made.
7619 FUNC is the corresponding MPFR logarithm function. */
7622 fold_builtin_logarithm (tree fndecl
, tree arglist
,
7623 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
))
7625 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7627 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7628 tree arg
= TREE_VALUE (arglist
);
7630 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
7632 /* Optimize log(e) = 1.0. We're never passed an exact 'e',
7633 instead we'll look for 'e' truncated to MODE. So only do
7634 this if flag_unsafe_math_optimizations is set. */
7635 if (flag_unsafe_math_optimizations
&& func
== mpfr_log
)
7637 const REAL_VALUE_TYPE e_truncated
=
7638 real_value_truncate (TYPE_MODE (type
), dconste
);
7639 if (real_dconstp (arg
, &e_truncated
))
7640 return build_real (type
, dconst1
);
7643 /* Calculate the result when the argument is a constant. */
7644 if ((res
= do_mpfr_arg1 (arg
, type
, func
, &dconst0
, NULL
, false)))
7647 /* Special case, optimize logN(expN(x)) = x. */
7648 if (flag_unsafe_math_optimizations
7649 && ((func
== mpfr_log
7650 && (fcode
== BUILT_IN_EXP
7651 || fcode
== BUILT_IN_EXPF
7652 || fcode
== BUILT_IN_EXPL
))
7653 || (func
== mpfr_log2
7654 && (fcode
== BUILT_IN_EXP2
7655 || fcode
== BUILT_IN_EXP2F
7656 || fcode
== BUILT_IN_EXP2L
))
7657 || (func
== mpfr_log10
&& (BUILTIN_EXP10_P (fcode
)))))
7658 return fold_convert (type
, TREE_VALUE (TREE_OPERAND (arg
, 1)));
7660 /* Optimize logN(func()) for various exponential functions. We
7661 want to determine the value "x" and the power "exponent" in
7662 order to transform logN(x**exponent) into exponent*logN(x). */
7663 if (flag_unsafe_math_optimizations
)
7665 tree exponent
= 0, x
= 0;
7669 CASE_FLT_FN (BUILT_IN_EXP
):
7670 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7671 x
= build_real (type
,
7672 real_value_truncate (TYPE_MODE (type
), dconste
));
7673 exponent
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7675 CASE_FLT_FN (BUILT_IN_EXP2
):
7676 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7677 x
= build_real (type
, dconst2
);
7678 exponent
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7680 CASE_FLT_FN (BUILT_IN_EXP10
):
7681 CASE_FLT_FN (BUILT_IN_POW10
):
7682 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7683 x
= build_real (type
, dconst10
);
7684 exponent
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7686 CASE_FLT_FN (BUILT_IN_SQRT
):
7687 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7688 x
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7689 exponent
= build_real (type
, dconsthalf
);
7691 CASE_FLT_FN (BUILT_IN_CBRT
):
7692 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7693 x
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7694 exponent
= build_real (type
, real_value_truncate (TYPE_MODE (type
),
7697 CASE_FLT_FN (BUILT_IN_POW
):
7698 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7699 x
= TREE_VALUE (TREE_OPERAND (arg
, 1));
7700 exponent
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg
, 1)));
7706 /* Now perform the optimization. */
7710 arglist
= build_tree_list (NULL_TREE
, x
);
7711 logfn
= build_function_call_expr (fndecl
, arglist
);
7712 return fold_build2 (MULT_EXPR
, type
, exponent
, logfn
);
7720 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7721 NULL_TREE if no simplification can be made. */
7724 fold_builtin_hypot (tree fndecl
, tree arglist
, tree type
)
7726 tree arg0
= TREE_VALUE (arglist
);
7727 tree arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
7728 tree res
, narg0
, narg1
;
7730 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
7733 /* Calculate the result when the argument is a constant. */
7734 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_hypot
)))
7737 /* If either argument to hypot has a negate or abs, strip that off.
7738 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7739 narg0
= fold_strip_sign_ops (arg0
);
7740 narg1
= fold_strip_sign_ops (arg1
);
7743 tree narglist
= tree_cons (NULL_TREE
, narg0
? narg0
: arg0
,
7744 build_tree_list (NULL_TREE
,
7745 narg1
? narg1
: arg1
));
7746 return build_function_call_expr (fndecl
, narglist
);
7749 /* If either argument is zero, hypot is fabs of the other. */
7750 if (real_zerop (arg0
))
7751 return fold_build1 (ABS_EXPR
, type
, arg1
);
7752 else if (real_zerop (arg1
))
7753 return fold_build1 (ABS_EXPR
, type
, arg0
);
7755 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7756 if (flag_unsafe_math_optimizations
7757 && operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
7759 REAL_VALUE_TYPE sqrt2
;
7761 real_sqrt (&sqrt2
, TYPE_MODE (type
), &dconst2
);
7762 return fold_build2 (MULT_EXPR
, type
,
7763 fold_build1 (ABS_EXPR
, type
, arg0
),
7764 build_real (type
, sqrt2
));
7771 /* Fold a builtin function call to pow, powf, or powl. Return
7772 NULL_TREE if no simplification can be made. */
7774 fold_builtin_pow (tree fndecl
, tree arglist
, tree type
)
7776 tree arg0
= TREE_VALUE (arglist
);
7777 tree arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
7780 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
7783 /* Calculate the result when the argument is a constant. */
7784 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_pow
)))
7787 /* Optimize pow(1.0,y) = 1.0. */
7788 if (real_onep (arg0
))
7789 return omit_one_operand (type
, build_real (type
, dconst1
), arg1
);
7791 if (TREE_CODE (arg1
) == REAL_CST
7792 && ! TREE_CONSTANT_OVERFLOW (arg1
))
7794 REAL_VALUE_TYPE cint
;
7798 c
= TREE_REAL_CST (arg1
);
7800 /* Optimize pow(x,0.0) = 1.0. */
7801 if (REAL_VALUES_EQUAL (c
, dconst0
))
7802 return omit_one_operand (type
, build_real (type
, dconst1
),
7805 /* Optimize pow(x,1.0) = x. */
7806 if (REAL_VALUES_EQUAL (c
, dconst1
))
7809 /* Optimize pow(x,-1.0) = 1.0/x. */
7810 if (REAL_VALUES_EQUAL (c
, dconstm1
))
7811 return fold_build2 (RDIV_EXPR
, type
,
7812 build_real (type
, dconst1
), arg0
);
7814 /* Optimize pow(x,0.5) = sqrt(x). */
7815 if (flag_unsafe_math_optimizations
7816 && REAL_VALUES_EQUAL (c
, dconsthalf
))
7818 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
7820 if (sqrtfn
!= NULL_TREE
)
7822 tree arglist
= build_tree_list (NULL_TREE
, arg0
);
7823 return build_function_call_expr (sqrtfn
, arglist
);
7827 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
7828 if (flag_unsafe_math_optimizations
)
7830 const REAL_VALUE_TYPE dconstroot
7831 = real_value_truncate (TYPE_MODE (type
), dconstthird
);
7833 if (REAL_VALUES_EQUAL (c
, dconstroot
))
7835 tree cbrtfn
= mathfn_built_in (type
, BUILT_IN_CBRT
);
7836 if (cbrtfn
!= NULL_TREE
)
7838 tree arglist
= build_tree_list (NULL_TREE
, arg0
);
7839 return build_function_call_expr (cbrtfn
, arglist
);
7844 /* Check for an integer exponent. */
7845 n
= real_to_integer (&c
);
7846 real_from_integer (&cint
, VOIDmode
, n
, n
< 0 ? -1 : 0, 0);
7847 if (real_identical (&c
, &cint
))
7849 /* Attempt to evaluate pow at compile-time. */
7850 if (TREE_CODE (arg0
) == REAL_CST
7851 && ! TREE_CONSTANT_OVERFLOW (arg0
))
7856 x
= TREE_REAL_CST (arg0
);
7857 inexact
= real_powi (&x
, TYPE_MODE (type
), &x
, n
);
7858 if (flag_unsafe_math_optimizations
|| !inexact
)
7859 return build_real (type
, x
);
7862 /* Strip sign ops from even integer powers. */
7863 if ((n
& 1) == 0 && flag_unsafe_math_optimizations
)
7865 tree narg0
= fold_strip_sign_ops (arg0
);
7868 arglist
= build_tree_list (NULL_TREE
, arg1
);
7869 arglist
= tree_cons (NULL_TREE
, narg0
, arglist
);
7870 return build_function_call_expr (fndecl
, arglist
);
7876 if (flag_unsafe_math_optimizations
)
7878 const enum built_in_function fcode
= builtin_mathfn_code (arg0
);
7880 /* Optimize pow(expN(x),y) = expN(x*y). */
7881 if (BUILTIN_EXPONENT_P (fcode
))
7883 tree expfn
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
7884 tree arg
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
7885 arg
= fold_build2 (MULT_EXPR
, type
, arg
, arg1
);
7886 arglist
= build_tree_list (NULL_TREE
, arg
);
7887 return build_function_call_expr (expfn
, arglist
);
7890 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7891 if (BUILTIN_SQRT_P (fcode
))
7893 tree narg0
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
7894 tree narg1
= fold_build2 (MULT_EXPR
, type
, arg1
,
7895 build_real (type
, dconsthalf
));
7897 arglist
= tree_cons (NULL_TREE
, narg0
,
7898 build_tree_list (NULL_TREE
, narg1
));
7899 return build_function_call_expr (fndecl
, arglist
);
7902 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7903 if (BUILTIN_CBRT_P (fcode
))
7905 tree arg
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
7906 if (tree_expr_nonnegative_p (arg
))
7908 const REAL_VALUE_TYPE dconstroot
7909 = real_value_truncate (TYPE_MODE (type
), dconstthird
);
7910 tree narg1
= fold_build2 (MULT_EXPR
, type
, arg1
,
7911 build_real (type
, dconstroot
));
7912 arglist
= tree_cons (NULL_TREE
, arg
,
7913 build_tree_list (NULL_TREE
, narg1
));
7914 return build_function_call_expr (fndecl
, arglist
);
7918 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7919 if (fcode
== BUILT_IN_POW
|| fcode
== BUILT_IN_POWF
7920 || fcode
== BUILT_IN_POWL
)
7922 tree arg00
= TREE_VALUE (TREE_OPERAND (arg0
, 1));
7923 tree arg01
= TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0
, 1)));
7924 tree narg1
= fold_build2 (MULT_EXPR
, type
, arg01
, arg1
);
7925 arglist
= tree_cons (NULL_TREE
, arg00
,
7926 build_tree_list (NULL_TREE
, narg1
));
7927 return build_function_call_expr (fndecl
, arglist
);
7934 /* Fold a builtin function call to powi, powif, or powil. Return
7935 NULL_TREE if no simplification can be made. */
7937 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED
, tree arglist
, tree type
)
7939 tree arg0
= TREE_VALUE (arglist
);
7940 tree arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
7942 if (!validate_arglist (arglist
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7945 /* Optimize pow(1.0,y) = 1.0. */
7946 if (real_onep (arg0
))
7947 return omit_one_operand (type
, build_real (type
, dconst1
), arg1
);
7949 if (host_integerp (arg1
, 0))
7951 HOST_WIDE_INT c
= TREE_INT_CST_LOW (arg1
);
7953 /* Evaluate powi at compile-time. */
7954 if (TREE_CODE (arg0
) == REAL_CST
7955 && ! TREE_CONSTANT_OVERFLOW (arg0
))
7958 x
= TREE_REAL_CST (arg0
);
7959 real_powi (&x
, TYPE_MODE (type
), &x
, c
);
7960 return build_real (type
, x
);
7963 /* Optimize pow(x,0) = 1.0. */
7965 return omit_one_operand (type
, build_real (type
, dconst1
),
7968 /* Optimize pow(x,1) = x. */
7972 /* Optimize pow(x,-1) = 1.0/x. */
7974 return fold_build2 (RDIV_EXPR
, type
,
7975 build_real (type
, dconst1
), arg0
);
7981 /* A subroutine of fold_builtin to fold the various exponent
7982 functions. Return NULL_TREE if no simplification can me made.
7983 FUNC is the corresponding MPFR exponent function. */
7986 fold_builtin_exponent (tree fndecl
, tree arglist
,
7987 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
))
7989 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
7991 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7992 tree arg
= TREE_VALUE (arglist
);
7995 /* Calculate the result when the argument is a constant. */
7996 if ((res
= do_mpfr_arg1 (arg
, type
, func
, NULL
, NULL
, 0)))
7999 /* Optimize expN(logN(x)) = x. */
8000 if (flag_unsafe_math_optimizations
)
8002 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
8004 if ((func
== mpfr_exp
8005 && (fcode
== BUILT_IN_LOG
8006 || fcode
== BUILT_IN_LOGF
8007 || fcode
== BUILT_IN_LOGL
))
8008 || (func
== mpfr_exp2
8009 && (fcode
== BUILT_IN_LOG2
8010 || fcode
== BUILT_IN_LOG2F
8011 || fcode
== BUILT_IN_LOG2L
))
8012 || (func
== mpfr_exp10
8013 && (fcode
== BUILT_IN_LOG10
8014 || fcode
== BUILT_IN_LOG10F
8015 || fcode
== BUILT_IN_LOG10L
)))
8016 return fold_convert (type
, TREE_VALUE (TREE_OPERAND (arg
, 1)));
8023 /* Return true if VAR is a VAR_DECL or a component thereof. */
8026 var_decl_component_p (tree var
)
8029 while (handled_component_p (inner
))
8030 inner
= TREE_OPERAND (inner
, 0);
8031 return SSA_VAR_P (inner
);
8034 /* Fold function call to builtin memset. Return
8035 NULL_TREE if no simplification can be made. */
8038 fold_builtin_memset (tree arglist
, tree type
, bool ignore
)
8040 tree dest
, c
, len
, var
, ret
;
8041 unsigned HOST_WIDE_INT length
, cval
;
8043 if (!validate_arglist (arglist
,
8044 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8047 dest
= TREE_VALUE (arglist
);
8048 c
= TREE_VALUE (TREE_CHAIN (arglist
));
8049 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8051 if (! host_integerp (len
, 1))
8054 /* If the LEN parameter is zero, return DEST. */
8055 if (integer_zerop (len
))
8056 return omit_one_operand (type
, dest
, c
);
8058 if (! host_integerp (c
, 1) || TREE_SIDE_EFFECTS (dest
))
8063 if (TREE_CODE (var
) != ADDR_EXPR
)
8066 var
= TREE_OPERAND (var
, 0);
8067 if (TREE_THIS_VOLATILE (var
))
8070 if (!INTEGRAL_TYPE_P (TREE_TYPE (var
))
8071 && !POINTER_TYPE_P (TREE_TYPE (var
)))
8074 if (! var_decl_component_p (var
))
8077 length
= tree_low_cst (len
, 1);
8078 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var
))) != length
8079 || get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
8083 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
8086 if (integer_zerop (c
))
8090 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
8093 cval
= tree_low_cst (c
, 1);
8097 cval
|= (cval
<< 31) << 1;
8100 ret
= build_int_cst_type (TREE_TYPE (var
), cval
);
8101 ret
= build2 (GIMPLE_MODIFY_STMT
, TREE_TYPE (var
), var
, ret
);
8105 return omit_one_operand (type
, dest
, ret
);
8108 /* Fold function call to builtin memset. Return
8109 NULL_TREE if no simplification can be made. */
8112 fold_builtin_bzero (tree arglist
, bool ignore
)
8114 tree dest
, size
, newarglist
;
8116 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8122 dest
= TREE_VALUE (arglist
);
8123 size
= TREE_VALUE (TREE_CHAIN (arglist
));
8125 /* New argument list transforming bzero(ptr x, int y) to
8126 memset(ptr x, int 0, size_t y). This is done this way
8127 so that if it isn't expanded inline, we fallback to
8128 calling bzero instead of memset. */
8130 newarglist
= build_tree_list (NULL_TREE
, fold_convert (sizetype
, size
));
8131 newarglist
= tree_cons (NULL_TREE
, integer_zero_node
, newarglist
);
8132 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
8133 return fold_builtin_memset (newarglist
, void_type_node
, ignore
);
8136 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8137 NULL_TREE if no simplification can be made.
8138 If ENDP is 0, return DEST (like memcpy).
8139 If ENDP is 1, return DEST+LEN (like mempcpy).
8140 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8141 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8145 fold_builtin_memory_op (tree arglist
, tree type
, bool ignore
, int endp
)
8147 tree dest
, src
, len
, destvar
, srcvar
, expr
;
8149 if (! validate_arglist (arglist
,
8150 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8153 dest
= TREE_VALUE (arglist
);
8154 src
= TREE_VALUE (TREE_CHAIN (arglist
));
8155 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8157 /* If the LEN parameter is zero, return DEST. */
8158 if (integer_zerop (len
))
8159 return omit_one_operand (type
, dest
, src
);
8161 /* If SRC and DEST are the same (and not volatile), return
8162 DEST{,+LEN,+LEN-1}. */
8163 if (operand_equal_p (src
, dest
, 0))
8167 tree srctype
, desttype
;
8170 int src_align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
8171 int dest_align
= get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
8173 /* Both DEST and SRC must be pointer types.
8174 ??? This is what old code did. Is the testing for pointer types
8177 If either SRC is readonly or length is 1, we can use memcpy. */
8178 if (dest_align
&& src_align
8179 && (readonly_data_expr (src
)
8180 || (host_integerp (len
, 1)
8181 && (MIN (src_align
, dest_align
) / BITS_PER_UNIT
>=
8182 tree_low_cst (len
, 1)))))
8184 tree fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
8187 return build_function_call_expr (fn
, arglist
);
8192 if (!host_integerp (len
, 0))
8195 This logic lose for arguments like (type *)malloc (sizeof (type)),
8196 since we strip the casts of up to VOID return value from malloc.
8197 Perhaps we ought to inherit type from non-VOID argument here? */
8200 srctype
= TREE_TYPE (TREE_TYPE (src
));
8201 desttype
= TREE_TYPE (TREE_TYPE (dest
));
8202 if (!srctype
|| !desttype
8203 || !TYPE_SIZE_UNIT (srctype
)
8204 || !TYPE_SIZE_UNIT (desttype
)
8205 || TREE_CODE (TYPE_SIZE_UNIT (srctype
)) != INTEGER_CST
8206 || TREE_CODE (TYPE_SIZE_UNIT (desttype
)) != INTEGER_CST
8207 || !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
)
8208 || !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
8211 if (get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
)
8212 < (int) TYPE_ALIGN (desttype
)
8213 || (get_pointer_alignment (src
, BIGGEST_ALIGNMENT
)
8214 < (int) TYPE_ALIGN (srctype
)))
8218 dest
= builtin_save_expr (dest
);
8220 srcvar
= build_fold_indirect_ref (src
);
8221 if (TREE_THIS_VOLATILE (srcvar
))
8223 if (!tree_int_cst_equal (lang_hooks
.expr_size (srcvar
), len
))
8225 /* With memcpy, it is possible to bypass aliasing rules, so without
8226 this check i. e. execute/20060930-2.c would be misoptimized, because
8227 it use conflicting alias set to hold argument for the memcpy call.
8228 This check is probably unnecesary with -fno-strict-aliasing.
8229 Similarly for destvar. See also PR29286. */
8230 if (!var_decl_component_p (srcvar
)
8231 /* Accept: memcpy (*char_var, "test", 1); that simplify
8233 || is_gimple_min_invariant (srcvar
)
8234 || readonly_data_expr (src
))
8237 destvar
= build_fold_indirect_ref (dest
);
8238 if (TREE_THIS_VOLATILE (destvar
))
8240 if (!tree_int_cst_equal (lang_hooks
.expr_size (destvar
), len
))
8242 if (!var_decl_component_p (destvar
))
8245 if (srctype
== desttype
8246 || (gimple_in_ssa_p (cfun
)
8247 && tree_ssa_useless_type_conversion_1 (desttype
, srctype
)))
8249 else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar
))
8250 || POINTER_TYPE_P (TREE_TYPE (srcvar
)))
8251 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar
))
8252 || POINTER_TYPE_P (TREE_TYPE (destvar
))))
8253 expr
= fold_convert (TREE_TYPE (destvar
), srcvar
);
8255 expr
= fold_build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (destvar
), srcvar
);
8256 expr
= build2 (GIMPLE_MODIFY_STMT
, TREE_TYPE (destvar
), destvar
, expr
);
8262 if (endp
== 0 || endp
== 3)
8263 return omit_one_operand (type
, dest
, expr
);
8269 len
= fold_build2 (MINUS_EXPR
, TREE_TYPE (len
), len
,
8272 len
= fold_convert (TREE_TYPE (dest
), len
);
8273 dest
= fold_build2 (PLUS_EXPR
, TREE_TYPE (dest
), dest
, len
);
8274 dest
= fold_convert (type
, dest
);
8276 dest
= omit_one_operand (type
, dest
, expr
);
8280 /* Fold function call to builtin bcopy. Return NULL_TREE if no
8281 simplification can be made. */
8284 fold_builtin_bcopy (tree arglist
, bool ignore
)
8286 tree src
, dest
, size
, newarglist
;
8288 if (!validate_arglist (arglist
,
8289 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8295 src
= TREE_VALUE (arglist
);
8296 dest
= TREE_VALUE (TREE_CHAIN (arglist
));
8297 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8299 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8300 memmove(ptr y, ptr x, size_t z). This is done this way
8301 so that if it isn't expanded inline, we fallback to
8302 calling bcopy instead of memmove. */
8304 newarglist
= build_tree_list (NULL_TREE
, fold_convert (sizetype
, size
));
8305 newarglist
= tree_cons (NULL_TREE
, src
, newarglist
);
8306 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
8308 return fold_builtin_memory_op (newarglist
, void_type_node
, true, /*endp=*/3);
8311 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
8312 the length of the string to be copied. Return NULL_TREE if no
8313 simplification can be made. */
8316 fold_builtin_strcpy (tree fndecl
, tree arglist
, tree len
)
8320 if (!validate_arglist (arglist
,
8321 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8324 dest
= TREE_VALUE (arglist
);
8325 src
= TREE_VALUE (TREE_CHAIN (arglist
));
8327 /* If SRC and DEST are the same (and not volatile), return DEST. */
8328 if (operand_equal_p (src
, dest
, 0))
8329 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), dest
);
8334 fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
8340 len
= c_strlen (src
, 1);
8341 if (! len
|| TREE_SIDE_EFFECTS (len
))
8345 len
= size_binop (PLUS_EXPR
, len
, ssize_int (1));
8346 arglist
= build_tree_list (NULL_TREE
, len
);
8347 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
8348 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
8349 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)),
8350 build_function_call_expr (fn
, arglist
));
8353 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
8354 the length of the source string. Return NULL_TREE if no simplification
8358 fold_builtin_strncpy (tree fndecl
, tree arglist
, tree slen
)
8360 tree dest
, src
, len
, fn
;
8362 if (!validate_arglist (arglist
,
8363 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8366 dest
= TREE_VALUE (arglist
);
8367 src
= TREE_VALUE (TREE_CHAIN (arglist
));
8368 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8370 /* If the LEN parameter is zero, return DEST. */
8371 if (integer_zerop (len
))
8372 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
8374 /* We can't compare slen with len as constants below if len is not a
8376 if (len
== 0 || TREE_CODE (len
) != INTEGER_CST
)
8380 slen
= c_strlen (src
, 1);
8382 /* Now, we must be passed a constant src ptr parameter. */
8383 if (slen
== 0 || TREE_CODE (slen
) != INTEGER_CST
)
8386 slen
= size_binop (PLUS_EXPR
, slen
, ssize_int (1));
8388 /* We do not support simplification of this case, though we do
8389 support it when expanding trees into RTL. */
8390 /* FIXME: generate a call to __builtin_memset. */
8391 if (tree_int_cst_lt (slen
, len
))
8394 /* OK transform into builtin memcpy. */
8395 fn
= implicit_built_in_decls
[BUILT_IN_MEMCPY
];
8398 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)),
8399 build_function_call_expr (fn
, arglist
));
8402 /* Fold function call to builtin memcmp. Return
8403 NULL_TREE if no simplification can be made. */
8406 fold_builtin_memcmp (tree arglist
)
8408 tree arg1
, arg2
, len
;
8409 const char *p1
, *p2
;
8411 if (!validate_arglist (arglist
,
8412 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8415 arg1
= TREE_VALUE (arglist
);
8416 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
8417 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8419 /* If the LEN parameter is zero, return zero. */
8420 if (integer_zerop (len
))
8421 return omit_two_operands (integer_type_node
, integer_zero_node
,
8424 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8425 if (operand_equal_p (arg1
, arg2
, 0))
8426 return omit_one_operand (integer_type_node
, integer_zero_node
, len
);
8428 p1
= c_getstr (arg1
);
8429 p2
= c_getstr (arg2
);
8431 /* If all arguments are constant, and the value of len is not greater
8432 than the lengths of arg1 and arg2, evaluate at compile-time. */
8433 if (host_integerp (len
, 1) && p1
&& p2
8434 && compare_tree_int (len
, strlen (p1
) + 1) <= 0
8435 && compare_tree_int (len
, strlen (p2
) + 1) <= 0)
8437 const int r
= memcmp (p1
, p2
, tree_low_cst (len
, 1));
8440 return integer_one_node
;
8442 return integer_minus_one_node
;
8444 return integer_zero_node
;
8447 /* If len parameter is one, return an expression corresponding to
8448 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8449 if (host_integerp (len
, 1) && tree_low_cst (len
, 1) == 1)
8451 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8452 tree cst_uchar_ptr_node
8453 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8455 tree ind1
= fold_convert (integer_type_node
,
8456 build1 (INDIRECT_REF
, cst_uchar_node
,
8457 fold_convert (cst_uchar_ptr_node
,
8459 tree ind2
= fold_convert (integer_type_node
,
8460 build1 (INDIRECT_REF
, cst_uchar_node
,
8461 fold_convert (cst_uchar_ptr_node
,
8463 return fold_build2 (MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8469 /* Fold function call to builtin strcmp. Return
8470 NULL_TREE if no simplification can be made. */
8473 fold_builtin_strcmp (tree arglist
)
8476 const char *p1
, *p2
;
8478 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
8481 arg1
= TREE_VALUE (arglist
);
8482 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
8484 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8485 if (operand_equal_p (arg1
, arg2
, 0))
8486 return integer_zero_node
;
8488 p1
= c_getstr (arg1
);
8489 p2
= c_getstr (arg2
);
8493 const int i
= strcmp (p1
, p2
);
8495 return integer_minus_one_node
;
8497 return integer_one_node
;
8499 return integer_zero_node
;
8502 /* If the second arg is "", return *(const unsigned char*)arg1. */
8503 if (p2
&& *p2
== '\0')
8505 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8506 tree cst_uchar_ptr_node
8507 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8509 return fold_convert (integer_type_node
,
8510 build1 (INDIRECT_REF
, cst_uchar_node
,
8511 fold_convert (cst_uchar_ptr_node
,
8515 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8516 if (p1
&& *p1
== '\0')
8518 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8519 tree cst_uchar_ptr_node
8520 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8522 tree temp
= fold_convert (integer_type_node
,
8523 build1 (INDIRECT_REF
, cst_uchar_node
,
8524 fold_convert (cst_uchar_ptr_node
,
8526 return fold_build1 (NEGATE_EXPR
, integer_type_node
, temp
);
8532 /* Fold function call to builtin strncmp. Return
8533 NULL_TREE if no simplification can be made. */
8536 fold_builtin_strncmp (tree arglist
)
8538 tree arg1
, arg2
, len
;
8539 const char *p1
, *p2
;
8541 if (!validate_arglist (arglist
,
8542 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
8545 arg1
= TREE_VALUE (arglist
);
8546 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
8547 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
8549 /* If the LEN parameter is zero, return zero. */
8550 if (integer_zerop (len
))
8551 return omit_two_operands (integer_type_node
, integer_zero_node
,
8554 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8555 if (operand_equal_p (arg1
, arg2
, 0))
8556 return omit_one_operand (integer_type_node
, integer_zero_node
, len
);
8558 p1
= c_getstr (arg1
);
8559 p2
= c_getstr (arg2
);
8561 if (host_integerp (len
, 1) && p1
&& p2
)
8563 const int i
= strncmp (p1
, p2
, tree_low_cst (len
, 1));
8565 return integer_one_node
;
8567 return integer_minus_one_node
;
8569 return integer_zero_node
;
8572 /* If the second arg is "", and the length is greater than zero,
8573 return *(const unsigned char*)arg1. */
8574 if (p2
&& *p2
== '\0'
8575 && TREE_CODE (len
) == INTEGER_CST
8576 && tree_int_cst_sgn (len
) == 1)
8578 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8579 tree cst_uchar_ptr_node
8580 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8582 return fold_convert (integer_type_node
,
8583 build1 (INDIRECT_REF
, cst_uchar_node
,
8584 fold_convert (cst_uchar_ptr_node
,
8588 /* If the first arg is "", and the length is greater than zero,
8589 return -*(const unsigned char*)arg2. */
8590 if (p1
&& *p1
== '\0'
8591 && TREE_CODE (len
) == INTEGER_CST
8592 && tree_int_cst_sgn (len
) == 1)
8594 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8595 tree cst_uchar_ptr_node
8596 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8598 tree temp
= fold_convert (integer_type_node
,
8599 build1 (INDIRECT_REF
, cst_uchar_node
,
8600 fold_convert (cst_uchar_ptr_node
,
8602 return fold_build1 (NEGATE_EXPR
, integer_type_node
, temp
);
8605 /* If len parameter is one, return an expression corresponding to
8606 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8607 if (host_integerp (len
, 1) && tree_low_cst (len
, 1) == 1)
8609 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8610 tree cst_uchar_ptr_node
8611 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8613 tree ind1
= fold_convert (integer_type_node
,
8614 build1 (INDIRECT_REF
, cst_uchar_node
,
8615 fold_convert (cst_uchar_ptr_node
,
8617 tree ind2
= fold_convert (integer_type_node
,
8618 build1 (INDIRECT_REF
, cst_uchar_node
,
8619 fold_convert (cst_uchar_ptr_node
,
8621 return fold_build2 (MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8627 /* Fold function call to builtin signbit, signbitf or signbitl. Return
8628 NULL_TREE if no simplification can be made. */
8631 fold_builtin_signbit (tree fndecl
, tree arglist
)
8633 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8636 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
8639 arg
= TREE_VALUE (arglist
);
8641 /* If ARG is a compile-time constant, determine the result. */
8642 if (TREE_CODE (arg
) == REAL_CST
8643 && !TREE_CONSTANT_OVERFLOW (arg
))
8647 c
= TREE_REAL_CST (arg
);
8648 temp
= REAL_VALUE_NEGATIVE (c
) ? integer_one_node
: integer_zero_node
;
8649 return fold_convert (type
, temp
);
8652 /* If ARG is non-negative, the result is always zero. */
8653 if (tree_expr_nonnegative_p (arg
))
8654 return omit_one_operand (type
, integer_zero_node
, arg
);
8656 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8657 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg
))))
8658 return fold_build2 (LT_EXPR
, type
, arg
,
8659 build_real (TREE_TYPE (arg
), dconst0
));
8664 /* Fold function call to builtin copysign, copysignf or copysignl.
8665 Return NULL_TREE if no simplification can be made. */
8668 fold_builtin_copysign (tree fndecl
, tree arglist
, tree type
)
8670 tree arg1
, arg2
, tem
;
8672 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
8675 arg1
= TREE_VALUE (arglist
);
8676 arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
8678 /* copysign(X,X) is X. */
8679 if (operand_equal_p (arg1
, arg2
, 0))
8680 return fold_convert (type
, arg1
);
8682 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8683 if (TREE_CODE (arg1
) == REAL_CST
8684 && TREE_CODE (arg2
) == REAL_CST
8685 && !TREE_CONSTANT_OVERFLOW (arg1
)
8686 && !TREE_CONSTANT_OVERFLOW (arg2
))
8688 REAL_VALUE_TYPE c1
, c2
;
8690 c1
= TREE_REAL_CST (arg1
);
8691 c2
= TREE_REAL_CST (arg2
);
8692 /* c1.sign := c2.sign. */
8693 real_copysign (&c1
, &c2
);
8694 return build_real (type
, c1
);
8697 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8698 Remember to evaluate Y for side-effects. */
8699 if (tree_expr_nonnegative_p (arg2
))
8700 return omit_one_operand (type
,
8701 fold_build1 (ABS_EXPR
, type
, arg1
),
8704 /* Strip sign changing operations for the first argument. */
8705 tem
= fold_strip_sign_ops (arg1
);
8708 arglist
= tree_cons (NULL_TREE
, tem
, TREE_CHAIN (arglist
));
8709 return build_function_call_expr (fndecl
, arglist
);
8715 /* Fold a call to builtin isascii. */
8718 fold_builtin_isascii (tree arglist
)
8720 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
8724 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8725 tree arg
= TREE_VALUE (arglist
);
8727 arg
= build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
8728 build_int_cst (NULL_TREE
,
8729 ~ (unsigned HOST_WIDE_INT
) 0x7f));
8730 return fold_build2 (EQ_EXPR
, integer_type_node
,
8731 arg
, integer_zero_node
);
8735 /* Fold a call to builtin toascii. */
8738 fold_builtin_toascii (tree arglist
)
8740 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
8744 /* Transform toascii(c) -> (c & 0x7f). */
8745 tree arg
= TREE_VALUE (arglist
);
8747 return fold_build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
8748 build_int_cst (NULL_TREE
, 0x7f));
8752 /* Fold a call to builtin isdigit. */
8755 fold_builtin_isdigit (tree arglist
)
8757 if (! validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
8761 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8762 /* According to the C standard, isdigit is unaffected by locale.
8763 However, it definitely is affected by the target character set. */
8765 unsigned HOST_WIDE_INT target_digit0
8766 = lang_hooks
.to_target_charset ('0');
8768 if (target_digit0
== 0)
8771 arg
= fold_convert (unsigned_type_node
, TREE_VALUE (arglist
));
8772 arg
= build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
8773 build_int_cst (unsigned_type_node
, target_digit0
));
8774 return fold_build2 (LE_EXPR
, integer_type_node
, arg
,
8775 build_int_cst (unsigned_type_node
, 9));
8779 /* Fold a call to fabs, fabsf or fabsl. */
8782 fold_builtin_fabs (tree arglist
, tree type
)
8786 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
8789 arg
= TREE_VALUE (arglist
);
8790 arg
= fold_convert (type
, arg
);
8791 if (TREE_CODE (arg
) == REAL_CST
)
8792 return fold_abs_const (arg
, type
);
8793 return fold_build1 (ABS_EXPR
, type
, arg
);
8796 /* Fold a call to abs, labs, llabs or imaxabs. */
8799 fold_builtin_abs (tree arglist
, tree type
)
8803 if (!validate_arglist (arglist
, INTEGER_TYPE
, VOID_TYPE
))
8806 arg
= TREE_VALUE (arglist
);
8807 arg
= fold_convert (type
, arg
);
8808 if (TREE_CODE (arg
) == INTEGER_CST
)
8809 return fold_abs_const (arg
, type
);
8810 return fold_build1 (ABS_EXPR
, type
, arg
);
8813 /* Fold a call to builtin fmin or fmax. */
8816 fold_builtin_fmin_fmax (tree arglist
, tree type
, bool max
)
8818 if (validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
8820 tree arg0
= TREE_VALUE (arglist
);
8821 tree arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
8822 /* Calculate the result when the argument is a constant. */
8823 tree res
= do_mpfr_arg2 (arg0
, arg1
, type
, (max
? mpfr_max
: mpfr_min
));
8828 /* If either argument is NaN, return the other one. Avoid the
8829 transformation if we get (and honor) a signalling NaN. Using
8830 omit_one_operand() ensures we create a non-lvalue. */
8831 if (TREE_CODE (arg0
) == REAL_CST
8832 && real_isnan (&TREE_REAL_CST (arg0
))
8833 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
8834 || ! TREE_REAL_CST (arg0
).signalling
))
8835 return omit_one_operand (type
, arg1
, arg0
);
8836 if (TREE_CODE (arg1
) == REAL_CST
8837 && real_isnan (&TREE_REAL_CST (arg1
))
8838 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
8839 || ! TREE_REAL_CST (arg1
).signalling
))
8840 return omit_one_operand (type
, arg0
, arg1
);
8842 /* Transform fmin/fmax(x,x) -> x. */
8843 if (operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
8844 return omit_one_operand (type
, arg0
, arg1
);
8846 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
8847 functions to return the numeric arg if the other one is NaN.
8848 These tree codes don't honor that, so only transform if
8849 -ffinite-math-only is set. C99 doesn't require -0.0 to be
8850 handled, so we don't have to worry about it either. */
8851 if (flag_finite_math_only
)
8852 return fold_build2 ((max
? MAX_EXPR
: MIN_EXPR
), type
,
8853 fold_convert (type
, arg0
),
8854 fold_convert (type
, arg1
));
8859 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8860 EXP is the CALL_EXPR for the call. */
8863 fold_builtin_classify (tree fndecl
, tree arglist
, int builtin_index
)
8865 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8869 if (!validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
8871 /* Check that we have exactly one argument. */
8874 error ("too few arguments to function %qs",
8875 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8876 return error_mark_node
;
8878 else if (TREE_CHAIN (arglist
) != 0)
8880 error ("too many arguments to function %qs",
8881 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8882 return error_mark_node
;
8886 error ("non-floating-point argument to function %qs",
8887 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8888 return error_mark_node
;
8892 arg
= TREE_VALUE (arglist
);
8893 switch (builtin_index
)
8895 case BUILT_IN_ISINF
:
8896 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
8897 return omit_one_operand (type
, integer_zero_node
, arg
);
8899 if (TREE_CODE (arg
) == REAL_CST
)
8901 r
= TREE_REAL_CST (arg
);
8902 if (real_isinf (&r
))
8903 return real_compare (GT_EXPR
, &r
, &dconst0
)
8904 ? integer_one_node
: integer_minus_one_node
;
8906 return integer_zero_node
;
8911 case BUILT_IN_FINITE
:
8912 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg
)))
8913 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
8914 return omit_one_operand (type
, integer_one_node
, arg
);
8916 if (TREE_CODE (arg
) == REAL_CST
)
8918 r
= TREE_REAL_CST (arg
);
8919 return real_isinf (&r
) || real_isnan (&r
)
8920 ? integer_zero_node
: integer_one_node
;
8925 case BUILT_IN_ISNAN
:
8926 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg
))))
8927 return omit_one_operand (type
, integer_zero_node
, arg
);
8929 if (TREE_CODE (arg
) == REAL_CST
)
8931 r
= TREE_REAL_CST (arg
);
8932 return real_isnan (&r
) ? integer_one_node
: integer_zero_node
;
8935 arg
= builtin_save_expr (arg
);
8936 return fold_build2 (UNORDERED_EXPR
, type
, arg
, arg
);
8943 /* Fold a call to an unordered comparison function such as
8944 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8945 being called and ARGLIST is the argument list for the call.
8946 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8947 the opposite of the desired result. UNORDERED_CODE is used
8948 for modes that can hold NaNs and ORDERED_CODE is used for
8952 fold_builtin_unordered_cmp (tree fndecl
, tree arglist
,
8953 enum tree_code unordered_code
,
8954 enum tree_code ordered_code
)
8956 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8957 enum tree_code code
;
8960 enum tree_code code0
, code1
;
8961 tree cmp_type
= NULL_TREE
;
8963 if (!validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
8965 /* Check that we have exactly two arguments. */
8966 if (arglist
== 0 || TREE_CHAIN (arglist
) == 0)
8968 error ("too few arguments to function %qs",
8969 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8970 return error_mark_node
;
8972 else if (TREE_CHAIN (TREE_CHAIN (arglist
)) != 0)
8974 error ("too many arguments to function %qs",
8975 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
8976 return error_mark_node
;
8980 arg0
= TREE_VALUE (arglist
);
8981 arg1
= TREE_VALUE (TREE_CHAIN (arglist
));
8983 type0
= TREE_TYPE (arg0
);
8984 type1
= TREE_TYPE (arg1
);
8986 code0
= TREE_CODE (type0
);
8987 code1
= TREE_CODE (type1
);
8989 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
8990 /* Choose the wider of two real types. */
8991 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
8993 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
8995 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
8999 error ("non-floating-point argument to function %qs",
9000 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
9001 return error_mark_node
;
9004 arg0
= fold_convert (cmp_type
, arg0
);
9005 arg1
= fold_convert (cmp_type
, arg1
);
9007 if (unordered_code
== UNORDERED_EXPR
)
9009 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
9010 return omit_two_operands (type
, integer_zero_node
, arg0
, arg1
);
9011 return fold_build2 (UNORDERED_EXPR
, type
, arg0
, arg1
);
9014 code
= HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))) ? unordered_code
9016 return fold_build1 (TRUTH_NOT_EXPR
, type
,
9017 fold_build2 (code
, type
, arg0
, arg1
));
9020 /* Used by constant folding to simplify calls to builtin functions. EXP is
9021 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
9022 result of the function call is ignored. This function returns NULL_TREE
9023 if no simplification was possible. */
9026 fold_builtin_1 (tree fndecl
, tree arglist
, bool ignore
)
9028 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9029 enum built_in_function fcode
;
9031 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
9032 return targetm
.fold_builtin (fndecl
, arglist
, ignore
);
9034 fcode
= DECL_FUNCTION_CODE (fndecl
);
9037 case BUILT_IN_FPUTS
:
9038 return fold_builtin_fputs (arglist
, ignore
, false, NULL_TREE
);
9040 case BUILT_IN_FPUTS_UNLOCKED
:
9041 return fold_builtin_fputs (arglist
, ignore
, true, NULL_TREE
);
9043 case BUILT_IN_STRSTR
:
9044 return fold_builtin_strstr (arglist
, type
);
9046 case BUILT_IN_STRCAT
:
9047 return fold_builtin_strcat (arglist
);
9049 case BUILT_IN_STRNCAT
:
9050 return fold_builtin_strncat (arglist
);
9052 case BUILT_IN_STRSPN
:
9053 return fold_builtin_strspn (arglist
);
9055 case BUILT_IN_STRCSPN
:
9056 return fold_builtin_strcspn (arglist
);
9058 case BUILT_IN_STRCHR
:
9059 case BUILT_IN_INDEX
:
9060 return fold_builtin_strchr (arglist
, type
);
9062 case BUILT_IN_STRRCHR
:
9063 case BUILT_IN_RINDEX
:
9064 return fold_builtin_strrchr (arglist
, type
);
9066 case BUILT_IN_STRCPY
:
9067 return fold_builtin_strcpy (fndecl
, arglist
, NULL_TREE
);
9069 case BUILT_IN_STRNCPY
:
9070 return fold_builtin_strncpy (fndecl
, arglist
, NULL_TREE
);
9072 case BUILT_IN_STRCMP
:
9073 return fold_builtin_strcmp (arglist
);
9075 case BUILT_IN_STRNCMP
:
9076 return fold_builtin_strncmp (arglist
);
9078 case BUILT_IN_STRPBRK
:
9079 return fold_builtin_strpbrk (arglist
, type
);
9082 case BUILT_IN_MEMCMP
:
9083 return fold_builtin_memcmp (arglist
);
9085 case BUILT_IN_SPRINTF
:
9086 return fold_builtin_sprintf (arglist
, ignore
);
9088 case BUILT_IN_CONSTANT_P
:
9092 val
= fold_builtin_constant_p (arglist
);
9093 /* Gimplification will pull the CALL_EXPR for the builtin out of
9094 an if condition. When not optimizing, we'll not CSE it back.
9095 To avoid link error types of regressions, return false now. */
9096 if (!val
&& !optimize
)
9097 val
= integer_zero_node
;
9102 case BUILT_IN_EXPECT
:
9103 return fold_builtin_expect (arglist
);
9105 case BUILT_IN_CLASSIFY_TYPE
:
9106 return fold_builtin_classify_type (arglist
);
9108 case BUILT_IN_STRLEN
:
9109 return fold_builtin_strlen (arglist
);
9111 CASE_FLT_FN (BUILT_IN_FABS
):
9112 return fold_builtin_fabs (arglist
, type
);
9116 case BUILT_IN_LLABS
:
9117 case BUILT_IN_IMAXABS
:
9118 return fold_builtin_abs (arglist
, type
);
9120 CASE_FLT_FN (BUILT_IN_CONJ
):
9121 if (validate_arglist (arglist
, COMPLEX_TYPE
, VOID_TYPE
))
9122 return fold_build1 (CONJ_EXPR
, type
, TREE_VALUE (arglist
));
9125 CASE_FLT_FN (BUILT_IN_CREAL
):
9126 if (validate_arglist (arglist
, COMPLEX_TYPE
, VOID_TYPE
))
9127 return non_lvalue (fold_build1 (REALPART_EXPR
, type
,
9128 TREE_VALUE (arglist
)));
9131 CASE_FLT_FN (BUILT_IN_CIMAG
):
9132 if (validate_arglist (arglist
, COMPLEX_TYPE
, VOID_TYPE
))
9133 return non_lvalue (fold_build1 (IMAGPART_EXPR
, type
,
9134 TREE_VALUE (arglist
)));
9137 CASE_FLT_FN (BUILT_IN_CABS
):
9138 return fold_builtin_cabs (arglist
, type
, fndecl
);
9140 CASE_FLT_FN (BUILT_IN_SQRT
):
9141 return fold_builtin_sqrt (arglist
, type
);
9143 CASE_FLT_FN (BUILT_IN_CBRT
):
9144 return fold_builtin_cbrt (arglist
, type
);
9146 CASE_FLT_FN (BUILT_IN_ASIN
):
9147 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9148 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_asin
,
9149 &dconstm1
, &dconst1
, true);
9152 CASE_FLT_FN (BUILT_IN_ACOS
):
9153 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9154 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_acos
,
9155 &dconstm1
, &dconst1
, true);
9158 CASE_FLT_FN (BUILT_IN_ATAN
):
9159 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9160 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_atan
,
9164 CASE_FLT_FN (BUILT_IN_ASINH
):
9165 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9166 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_asinh
,
9170 CASE_FLT_FN (BUILT_IN_ACOSH
):
9171 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9172 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_acosh
,
9173 &dconst1
, NULL
, true);
9176 CASE_FLT_FN (BUILT_IN_ATANH
):
9177 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9178 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_atanh
,
9179 &dconstm1
, &dconst1
, false);
9182 CASE_FLT_FN (BUILT_IN_SIN
):
9183 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9184 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_sin
,
9188 CASE_FLT_FN (BUILT_IN_COS
):
9189 return fold_builtin_cos (arglist
, type
, fndecl
);
9191 CASE_FLT_FN (BUILT_IN_TAN
):
9192 return fold_builtin_tan (arglist
, type
);
9194 CASE_FLT_FN (BUILT_IN_SINCOS
):
9195 if (validate_arglist (arglist
, REAL_TYPE
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9196 return do_mpfr_sincos (TREE_VALUE (arglist
), TREE_VALUE (TREE_CHAIN (arglist
)),
9197 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
))));
9200 CASE_FLT_FN (BUILT_IN_SINH
):
9201 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9202 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_sinh
,
9206 CASE_FLT_FN (BUILT_IN_COSH
):
9207 return fold_builtin_cosh (arglist
, type
, fndecl
);
9209 CASE_FLT_FN (BUILT_IN_TANH
):
9210 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9211 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_tanh
,
9215 CASE_FLT_FN (BUILT_IN_ERF
):
9216 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9217 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_erf
,
9221 CASE_FLT_FN (BUILT_IN_ERFC
):
9222 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9223 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_erfc
,
9227 CASE_FLT_FN (BUILT_IN_TGAMMA
):
9228 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9229 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_gamma
,
9233 CASE_FLT_FN (BUILT_IN_EXP
):
9234 return fold_builtin_exponent (fndecl
, arglist
, mpfr_exp
);
9236 CASE_FLT_FN (BUILT_IN_EXP2
):
9237 return fold_builtin_exponent (fndecl
, arglist
, mpfr_exp2
);
9239 CASE_FLT_FN (BUILT_IN_EXP10
):
9240 CASE_FLT_FN (BUILT_IN_POW10
):
9241 return fold_builtin_exponent (fndecl
, arglist
, mpfr_exp10
);
9243 CASE_FLT_FN (BUILT_IN_EXPM1
):
9244 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9245 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_expm1
,
9249 CASE_FLT_FN (BUILT_IN_LOG
):
9250 return fold_builtin_logarithm (fndecl
, arglist
, mpfr_log
);
9252 CASE_FLT_FN (BUILT_IN_LOG2
):
9253 return fold_builtin_logarithm (fndecl
, arglist
, mpfr_log2
);
9255 CASE_FLT_FN (BUILT_IN_LOG10
):
9256 return fold_builtin_logarithm (fndecl
, arglist
, mpfr_log10
);
9258 CASE_FLT_FN (BUILT_IN_LOG1P
):
9259 if (validate_arglist (arglist
, REAL_TYPE
, VOID_TYPE
))
9260 return do_mpfr_arg1 (TREE_VALUE (arglist
), type
, mpfr_log1p
,
9261 &dconstm1
, NULL
, false);
9264 CASE_FLT_FN (BUILT_IN_ATAN2
):
9265 if (validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
9266 return do_mpfr_arg2 (TREE_VALUE (arglist
),
9267 TREE_VALUE (TREE_CHAIN (arglist
)),
9271 CASE_FLT_FN (BUILT_IN_FMA
):
9272 if (validate_arglist (arglist
, REAL_TYPE
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
9273 return do_mpfr_arg3 (TREE_VALUE (arglist
),
9274 TREE_VALUE (TREE_CHAIN (arglist
)),
9275 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
))),
9279 CASE_FLT_FN (BUILT_IN_FMIN
):
9280 return fold_builtin_fmin_fmax (arglist
, type
, /*max=*/false);
9282 CASE_FLT_FN (BUILT_IN_FMAX
):
9283 return fold_builtin_fmin_fmax (arglist
, type
, /*max=*/true);
9285 CASE_FLT_FN (BUILT_IN_HYPOT
):
9286 return fold_builtin_hypot (fndecl
, arglist
, type
);
9288 CASE_FLT_FN (BUILT_IN_POW
):
9289 return fold_builtin_pow (fndecl
, arglist
, type
);
9291 CASE_FLT_FN (BUILT_IN_POWI
):
9292 return fold_builtin_powi (fndecl
, arglist
, type
);
9294 CASE_FLT_FN (BUILT_IN_INF
):
9295 case BUILT_IN_INFD32
:
9296 case BUILT_IN_INFD64
:
9297 case BUILT_IN_INFD128
:
9298 return fold_builtin_inf (type
, true);
9300 CASE_FLT_FN (BUILT_IN_HUGE_VAL
):
9301 return fold_builtin_inf (type
, false);
9303 CASE_FLT_FN (BUILT_IN_NAN
):
9304 case BUILT_IN_NAND32
:
9305 case BUILT_IN_NAND64
:
9306 case BUILT_IN_NAND128
:
9307 return fold_builtin_nan (arglist
, type
, true);
9309 CASE_FLT_FN (BUILT_IN_NANS
):
9310 return fold_builtin_nan (arglist
, type
, false);
9312 CASE_FLT_FN (BUILT_IN_FLOOR
):
9313 return fold_builtin_floor (fndecl
, arglist
);
9315 CASE_FLT_FN (BUILT_IN_CEIL
):
9316 return fold_builtin_ceil (fndecl
, arglist
);
9318 CASE_FLT_FN (BUILT_IN_TRUNC
):
9319 return fold_builtin_trunc (fndecl
, arglist
);
9321 CASE_FLT_FN (BUILT_IN_ROUND
):
9322 return fold_builtin_round (fndecl
, arglist
);
9324 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
9325 CASE_FLT_FN (BUILT_IN_RINT
):
9326 return fold_trunc_transparent_mathfn (fndecl
, arglist
);
9328 CASE_FLT_FN (BUILT_IN_LCEIL
):
9329 CASE_FLT_FN (BUILT_IN_LLCEIL
):
9330 CASE_FLT_FN (BUILT_IN_LFLOOR
):
9331 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
9332 CASE_FLT_FN (BUILT_IN_LROUND
):
9333 CASE_FLT_FN (BUILT_IN_LLROUND
):
9334 return fold_builtin_int_roundingfn (fndecl
, arglist
);
9336 CASE_FLT_FN (BUILT_IN_LRINT
):
9337 CASE_FLT_FN (BUILT_IN_LLRINT
):
9338 return fold_fixed_mathfn (fndecl
, arglist
);
9340 case BUILT_IN_BSWAP32
:
9341 case BUILT_IN_BSWAP64
:
9342 return fold_builtin_bswap (fndecl
, arglist
);
9344 CASE_INT_FN (BUILT_IN_FFS
):
9345 CASE_INT_FN (BUILT_IN_CLZ
):
9346 CASE_INT_FN (BUILT_IN_CTZ
):
9347 CASE_INT_FN (BUILT_IN_POPCOUNT
):
9348 CASE_INT_FN (BUILT_IN_PARITY
):
9349 return fold_builtin_bitop (fndecl
, arglist
);
9351 case BUILT_IN_MEMSET
:
9352 return fold_builtin_memset (arglist
, type
, ignore
);
9354 case BUILT_IN_MEMCPY
:
9355 return fold_builtin_memory_op (arglist
, type
, ignore
, /*endp=*/0);
9357 case BUILT_IN_MEMPCPY
:
9358 return fold_builtin_memory_op (arglist
, type
, ignore
, /*endp=*/1);
9360 case BUILT_IN_MEMMOVE
:
9361 return fold_builtin_memory_op (arglist
, type
, ignore
, /*endp=*/3);
9363 case BUILT_IN_BZERO
:
9364 return fold_builtin_bzero (arglist
, ignore
);
9366 case BUILT_IN_BCOPY
:
9367 return fold_builtin_bcopy (arglist
, ignore
);
9369 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
9370 return fold_builtin_signbit (fndecl
, arglist
);
9372 case BUILT_IN_ISASCII
:
9373 return fold_builtin_isascii (arglist
);
9375 case BUILT_IN_TOASCII
:
9376 return fold_builtin_toascii (arglist
);
9378 case BUILT_IN_ISDIGIT
:
9379 return fold_builtin_isdigit (arglist
);
9381 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
9382 return fold_builtin_copysign (fndecl
, arglist
, type
);
9384 CASE_FLT_FN (BUILT_IN_FINITE
):
9385 case BUILT_IN_FINITED32
:
9386 case BUILT_IN_FINITED64
:
9387 case BUILT_IN_FINITED128
:
9388 return fold_builtin_classify (fndecl
, arglist
, BUILT_IN_FINITE
);
9390 CASE_FLT_FN (BUILT_IN_ISINF
):
9391 case BUILT_IN_ISINFD32
:
9392 case BUILT_IN_ISINFD64
:
9393 case BUILT_IN_ISINFD128
:
9394 return fold_builtin_classify (fndecl
, arglist
, BUILT_IN_ISINF
);
9396 CASE_FLT_FN (BUILT_IN_ISNAN
):
9397 case BUILT_IN_ISNAND32
:
9398 case BUILT_IN_ISNAND64
:
9399 case BUILT_IN_ISNAND128
:
9400 return fold_builtin_classify (fndecl
, arglist
, BUILT_IN_ISNAN
);
9402 case BUILT_IN_ISGREATER
:
9403 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNLE_EXPR
, LE_EXPR
);
9404 case BUILT_IN_ISGREATEREQUAL
:
9405 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNLT_EXPR
, LT_EXPR
);
9406 case BUILT_IN_ISLESS
:
9407 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNGE_EXPR
, GE_EXPR
);
9408 case BUILT_IN_ISLESSEQUAL
:
9409 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNGT_EXPR
, GT_EXPR
);
9410 case BUILT_IN_ISLESSGREATER
:
9411 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNEQ_EXPR
, EQ_EXPR
);
9412 case BUILT_IN_ISUNORDERED
:
9413 return fold_builtin_unordered_cmp (fndecl
, arglist
, UNORDERED_EXPR
,
9416 /* We do the folding for va_start in the expander. */
9417 case BUILT_IN_VA_START
:
9420 case BUILT_IN_OBJECT_SIZE
:
9421 return fold_builtin_object_size (arglist
);
9422 case BUILT_IN_MEMCPY_CHK
:
9423 case BUILT_IN_MEMPCPY_CHK
:
9424 case BUILT_IN_MEMMOVE_CHK
:
9425 case BUILT_IN_MEMSET_CHK
:
9426 return fold_builtin_memory_chk (fndecl
, arglist
, NULL_TREE
, ignore
,
9427 DECL_FUNCTION_CODE (fndecl
));
9428 case BUILT_IN_STRCPY_CHK
:
9429 case BUILT_IN_STPCPY_CHK
:
9430 return fold_builtin_stxcpy_chk (fndecl
, arglist
, NULL_TREE
, ignore
,
9431 DECL_FUNCTION_CODE (fndecl
));
9432 case BUILT_IN_STRNCPY_CHK
:
9433 return fold_builtin_strncpy_chk (arglist
, NULL_TREE
);
9434 case BUILT_IN_STRCAT_CHK
:
9435 return fold_builtin_strcat_chk (fndecl
, arglist
);
9436 case BUILT_IN_STRNCAT_CHK
:
9437 return fold_builtin_strncat_chk (fndecl
, arglist
);
9438 case BUILT_IN_SPRINTF_CHK
:
9439 case BUILT_IN_VSPRINTF_CHK
:
9440 return fold_builtin_sprintf_chk (arglist
, DECL_FUNCTION_CODE (fndecl
));
9441 case BUILT_IN_SNPRINTF_CHK
:
9442 case BUILT_IN_VSNPRINTF_CHK
:
9443 return fold_builtin_snprintf_chk (arglist
, NULL_TREE
,
9444 DECL_FUNCTION_CODE (fndecl
));
9446 case BUILT_IN_PRINTF
:
9447 case BUILT_IN_PRINTF_UNLOCKED
:
9448 case BUILT_IN_VPRINTF
:
9449 case BUILT_IN_PRINTF_CHK
:
9450 case BUILT_IN_VPRINTF_CHK
:
9451 return fold_builtin_printf (fndecl
, arglist
, ignore
,
9452 DECL_FUNCTION_CODE (fndecl
));
9454 case BUILT_IN_FPRINTF
:
9455 case BUILT_IN_FPRINTF_UNLOCKED
:
9456 case BUILT_IN_VFPRINTF
:
9457 case BUILT_IN_FPRINTF_CHK
:
9458 case BUILT_IN_VFPRINTF_CHK
:
9459 return fold_builtin_fprintf (fndecl
, arglist
, ignore
,
9460 DECL_FUNCTION_CODE (fndecl
));
9469 /* A wrapper function for builtin folding that prevents warnings for
9470 "statement without effect" and the like, caused by removing the
9471 call node earlier than the warning is generated. */
9474 fold_builtin (tree fndecl
, tree arglist
, bool ignore
)
9476 tree exp
= fold_builtin_1 (fndecl
, arglist
, ignore
);
9479 exp
= build1 (NOP_EXPR
, GENERIC_TREE_TYPE (exp
), exp
);
9480 TREE_NO_WARNING (exp
) = 1;
9486 /* Conveniently construct a function call expression. */
9489 build_function_call_expr (tree fn
, tree arglist
)
9493 call_expr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
9494 return fold_build3 (CALL_EXPR
, TREE_TYPE (TREE_TYPE (fn
)),
9495 call_expr
, arglist
, NULL_TREE
);
9498 /* This function validates the types of a function call argument list
9499 represented as a tree chain of parameters against a specified list
9500 of tree_codes. If the last specifier is a 0, that represents an
9501 ellipses, otherwise the last specifier must be a VOID_TYPE. */
9504 validate_arglist (tree arglist
, ...)
9506 enum tree_code code
;
9510 va_start (ap
, arglist
);
9514 code
= va_arg (ap
, enum tree_code
);
9518 /* This signifies an ellipses, any further arguments are all ok. */
9522 /* This signifies an endlink, if no arguments remain, return
9523 true, otherwise return false. */
9527 /* If no parameters remain or the parameter's code does not
9528 match the specified code, return false. Otherwise continue
9529 checking any remaining arguments. */
9532 if (code
== POINTER_TYPE
)
9534 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
))))
9537 else if (code
!= TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))))
9541 arglist
= TREE_CHAIN (arglist
);
9545 /* We need gotos here since we can only have one VA_CLOSE in a
9553 /* Default target-specific builtin expander that does nothing. */
9556 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
9557 rtx target ATTRIBUTE_UNUSED
,
9558 rtx subtarget ATTRIBUTE_UNUSED
,
9559 enum machine_mode mode ATTRIBUTE_UNUSED
,
9560 int ignore ATTRIBUTE_UNUSED
)
9565 /* Returns true is EXP represents data that would potentially reside
9566 in a readonly section. */
9569 readonly_data_expr (tree exp
)
9573 if (TREE_CODE (exp
) != ADDR_EXPR
)
9576 exp
= get_base_address (TREE_OPERAND (exp
, 0));
9580 /* Make sure we call decl_readonly_section only for trees it
9581 can handle (since it returns true for everything it doesn't
9583 if (TREE_CODE (exp
) == STRING_CST
9584 || TREE_CODE (exp
) == CONSTRUCTOR
9585 || (TREE_CODE (exp
) == VAR_DECL
&& TREE_STATIC (exp
)))
9586 return decl_readonly_section (exp
, 0);
9591 /* Simplify a call to the strstr builtin.
9593 Return 0 if no simplification was possible, otherwise return the
9594 simplified form of the call as a tree.
9596 The simplified form may be a constant or other expression which
9597 computes the same value, but in a more efficient manner (including
9598 calls to other builtin functions).
9600 The call may contain arguments which need to be evaluated, but
9601 which are not useful to determine the result of the call. In
9602 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9603 COMPOUND_EXPR will be an argument which must be evaluated.
9604 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9605 COMPOUND_EXPR in the chain will contain the tree for the simplified
9606 form of the builtin function call. */
9609 fold_builtin_strstr (tree arglist
, tree type
)
9611 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9615 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9617 const char *p1
, *p2
;
9626 const char *r
= strstr (p1
, p2
);
9630 return build_int_cst (TREE_TYPE (s1
), 0);
9632 /* Return an offset into the constant string argument. */
9633 tem
= fold_build2 (PLUS_EXPR
, TREE_TYPE (s1
),
9634 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
));
9635 return fold_convert (type
, tem
);
9638 /* The argument is const char *, and the result is char *, so we need
9639 a type conversion here to avoid a warning. */
9641 return fold_convert (type
, s1
);
9646 fn
= implicit_built_in_decls
[BUILT_IN_STRCHR
];
9650 /* New argument list transforming strstr(s1, s2) to
9651 strchr(s1, s2[0]). */
9652 arglist
= build_tree_list (NULL_TREE
,
9653 build_int_cst (NULL_TREE
, p2
[0]));
9654 arglist
= tree_cons (NULL_TREE
, s1
, arglist
);
9655 return build_function_call_expr (fn
, arglist
);
9659 /* Simplify a call to the strchr builtin.
9661 Return 0 if no simplification was possible, otherwise return the
9662 simplified form of the call as a tree.
9664 The simplified form may be a constant or other expression which
9665 computes the same value, but in a more efficient manner (including
9666 calls to other builtin functions).
9668 The call may contain arguments which need to be evaluated, but
9669 which are not useful to determine the result of the call. In
9670 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9671 COMPOUND_EXPR will be an argument which must be evaluated.
9672 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9673 COMPOUND_EXPR in the chain will contain the tree for the simplified
9674 form of the builtin function call. */
9677 fold_builtin_strchr (tree arglist
, tree type
)
9679 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
9683 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9686 if (TREE_CODE (s2
) != INTEGER_CST
)
9696 if (target_char_cast (s2
, &c
))
9702 return build_int_cst (TREE_TYPE (s1
), 0);
9704 /* Return an offset into the constant string argument. */
9705 tem
= fold_build2 (PLUS_EXPR
, TREE_TYPE (s1
),
9706 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
));
9707 return fold_convert (type
, tem
);
9713 /* Simplify a call to the strrchr builtin.
9715 Return 0 if no simplification was possible, otherwise return the
9716 simplified form of the call as a tree.
9718 The simplified form may be a constant or other expression which
9719 computes the same value, but in a more efficient manner (including
9720 calls to other builtin functions).
9722 The call may contain arguments which need to be evaluated, but
9723 which are not useful to determine the result of the call. In
9724 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9725 COMPOUND_EXPR will be an argument which must be evaluated.
9726 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9727 COMPOUND_EXPR in the chain will contain the tree for the simplified
9728 form of the builtin function call. */
9731 fold_builtin_strrchr (tree arglist
, tree type
)
9733 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
9737 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9741 if (TREE_CODE (s2
) != INTEGER_CST
)
9751 if (target_char_cast (s2
, &c
))
9754 r
= strrchr (p1
, c
);
9757 return build_int_cst (TREE_TYPE (s1
), 0);
9759 /* Return an offset into the constant string argument. */
9760 tem
= fold_build2 (PLUS_EXPR
, TREE_TYPE (s1
),
9761 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
));
9762 return fold_convert (type
, tem
);
9765 if (! integer_zerop (s2
))
9768 fn
= implicit_built_in_decls
[BUILT_IN_STRCHR
];
9772 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9773 return build_function_call_expr (fn
, arglist
);
9777 /* Simplify a call to the strpbrk builtin.
9779 Return 0 if no simplification was possible, otherwise return the
9780 simplified form of the call as a tree.
9782 The simplified form may be a constant or other expression which
9783 computes the same value, but in a more efficient manner (including
9784 calls to other builtin functions).
9786 The call may contain arguments which need to be evaluated, but
9787 which are not useful to determine the result of the call. In
9788 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9789 COMPOUND_EXPR will be an argument which must be evaluated.
9790 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9791 COMPOUND_EXPR in the chain will contain the tree for the simplified
9792 form of the builtin function call. */
9795 fold_builtin_strpbrk (tree arglist
, tree type
)
9797 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9801 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9803 const char *p1
, *p2
;
9812 const char *r
= strpbrk (p1
, p2
);
9816 return build_int_cst (TREE_TYPE (s1
), 0);
9818 /* Return an offset into the constant string argument. */
9819 tem
= fold_build2 (PLUS_EXPR
, TREE_TYPE (s1
),
9820 s1
, build_int_cst (TREE_TYPE (s1
), r
- p1
));
9821 return fold_convert (type
, tem
);
9825 /* strpbrk(x, "") == NULL.
9826 Evaluate and ignore s1 in case it had side-effects. */
9827 return omit_one_operand (TREE_TYPE (s1
), integer_zero_node
, s1
);
9830 return 0; /* Really call strpbrk. */
9832 fn
= implicit_built_in_decls
[BUILT_IN_STRCHR
];
9836 /* New argument list transforming strpbrk(s1, s2) to
9837 strchr(s1, s2[0]). */
9838 arglist
= build_tree_list (NULL_TREE
,
9839 build_int_cst (NULL_TREE
, p2
[0]));
9840 arglist
= tree_cons (NULL_TREE
, s1
, arglist
);
9841 return build_function_call_expr (fn
, arglist
);
9845 /* Simplify a call to the strcat builtin.
9847 Return 0 if no simplification was possible, otherwise return the
9848 simplified form of the call as a tree.
9850 The simplified form may be a constant or other expression which
9851 computes the same value, but in a more efficient manner (including
9852 calls to other builtin functions).
9854 The call may contain arguments which need to be evaluated, but
9855 which are not useful to determine the result of the call. In
9856 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9857 COMPOUND_EXPR will be an argument which must be evaluated.
9858 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9859 COMPOUND_EXPR in the chain will contain the tree for the simplified
9860 form of the builtin function call. */
9863 fold_builtin_strcat (tree arglist
)
9865 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9869 tree dst
= TREE_VALUE (arglist
),
9870 src
= TREE_VALUE (TREE_CHAIN (arglist
));
9871 const char *p
= c_getstr (src
);
9873 /* If the string length is zero, return the dst parameter. */
9874 if (p
&& *p
== '\0')
9881 /* Simplify a call to the strncat builtin.
9883 Return 0 if no simplification was possible, otherwise return the
9884 simplified form of the call as a tree.
9886 The simplified form may be a constant or other expression which
9887 computes the same value, but in a more efficient manner (including
9888 calls to other builtin functions).
9890 The call may contain arguments which need to be evaluated, but
9891 which are not useful to determine the result of the call. In
9892 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9893 COMPOUND_EXPR will be an argument which must be evaluated.
9894 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9895 COMPOUND_EXPR in the chain will contain the tree for the simplified
9896 form of the builtin function call. */
9899 fold_builtin_strncat (tree arglist
)
9901 if (!validate_arglist (arglist
,
9902 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
9906 tree dst
= TREE_VALUE (arglist
);
9907 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
9908 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
9909 const char *p
= c_getstr (src
);
9911 /* If the requested length is zero, or the src parameter string
9912 length is zero, return the dst parameter. */
9913 if (integer_zerop (len
) || (p
&& *p
== '\0'))
9914 return omit_two_operands (TREE_TYPE (dst
), dst
, src
, len
);
9916 /* If the requested len is greater than or equal to the string
9917 length, call strcat. */
9918 if (TREE_CODE (len
) == INTEGER_CST
&& p
9919 && compare_tree_int (len
, strlen (p
)) >= 0)
9922 = tree_cons (NULL_TREE
, dst
, build_tree_list (NULL_TREE
, src
));
9923 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCAT
];
9925 /* If the replacement _DECL isn't initialized, don't do the
9930 return build_function_call_expr (fn
, newarglist
);
9936 /* Simplify a call to the strspn builtin.
9938 Return 0 if no simplification was possible, otherwise return the
9939 simplified form of the call as a tree.
9941 The simplified form may be a constant or other expression which
9942 computes the same value, but in a more efficient manner (including
9943 calls to other builtin functions).
9945 The call may contain arguments which need to be evaluated, but
9946 which are not useful to determine the result of the call. In
9947 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9948 COMPOUND_EXPR will be an argument which must be evaluated.
9949 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9950 COMPOUND_EXPR in the chain will contain the tree for the simplified
9951 form of the builtin function call. */
9954 fold_builtin_strspn (tree arglist
)
9956 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
9960 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
9961 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
9963 /* If both arguments are constants, evaluate at compile-time. */
9966 const size_t r
= strspn (p1
, p2
);
9967 return size_int (r
);
9970 /* If either argument is "", return 0. */
9971 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
9972 /* Evaluate and ignore both arguments in case either one has
9974 return omit_two_operands (integer_type_node
, integer_zero_node
,
9980 /* Simplify a call to the strcspn builtin.
9982 Return 0 if no simplification was possible, otherwise return the
9983 simplified form of the call as a tree.
9985 The simplified form may be a constant or other expression which
9986 computes the same value, but in a more efficient manner (including
9987 calls to other builtin functions).
9989 The call may contain arguments which need to be evaluated, but
9990 which are not useful to determine the result of the call. In
9991 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9992 COMPOUND_EXPR will be an argument which must be evaluated.
9993 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9994 COMPOUND_EXPR in the chain will contain the tree for the simplified
9995 form of the builtin function call. */
9998 fold_builtin_strcspn (tree arglist
)
10000 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
10004 tree s1
= TREE_VALUE (arglist
), s2
= TREE_VALUE (TREE_CHAIN (arglist
));
10005 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
10007 /* If both arguments are constants, evaluate at compile-time. */
10010 const size_t r
= strcspn (p1
, p2
);
10011 return size_int (r
);
10014 /* If the first argument is "", return 0. */
10015 if (p1
&& *p1
== '\0')
10017 /* Evaluate and ignore argument s2 in case it has
10019 return omit_one_operand (integer_type_node
,
10020 integer_zero_node
, s2
);
10023 /* If the second argument is "", return __builtin_strlen(s1). */
10024 if (p2
&& *p2
== '\0')
10026 tree newarglist
= build_tree_list (NULL_TREE
, s1
),
10027 fn
= implicit_built_in_decls
[BUILT_IN_STRLEN
];
10029 /* If the replacement _DECL isn't initialized, don't do the
10034 return build_function_call_expr (fn
, newarglist
);
10040 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
10041 by the builtin will be ignored. UNLOCKED is true is true if this
10042 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
10043 the known length of the string. Return NULL_TREE if no simplification
10047 fold_builtin_fputs (tree arglist
, bool ignore
, bool unlocked
, tree len
)
10050 /* If we're using an unlocked function, assume the other unlocked
10051 functions exist explicitly. */
10052 tree
const fn_fputc
= unlocked
? built_in_decls
[BUILT_IN_FPUTC_UNLOCKED
]
10053 : implicit_built_in_decls
[BUILT_IN_FPUTC
];
10054 tree
const fn_fwrite
= unlocked
? built_in_decls
[BUILT_IN_FWRITE_UNLOCKED
]
10055 : implicit_built_in_decls
[BUILT_IN_FWRITE
];
10057 /* If the return value is used, don't do the transformation. */
10061 /* Verify the arguments in the original call. */
10062 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
10066 len
= c_strlen (TREE_VALUE (arglist
), 0);
10068 /* Get the length of the string passed to fputs. If the length
10069 can't be determined, punt. */
10071 || TREE_CODE (len
) != INTEGER_CST
)
10074 switch (compare_tree_int (len
, 1))
10076 case -1: /* length is 0, delete the call entirely . */
10077 return omit_one_operand (integer_type_node
, integer_zero_node
,
10078 TREE_VALUE (TREE_CHAIN (arglist
)));
10080 case 0: /* length is 1, call fputc. */
10082 const char *p
= c_getstr (TREE_VALUE (arglist
));
10086 /* New argument list transforming fputs(string, stream) to
10087 fputc(string[0], stream). */
10088 arglist
= build_tree_list (NULL_TREE
,
10089 TREE_VALUE (TREE_CHAIN (arglist
)));
10090 arglist
= tree_cons (NULL_TREE
,
10091 build_int_cst (NULL_TREE
, p
[0]),
10098 case 1: /* length is greater than 1, call fwrite. */
10102 /* If optimizing for size keep fputs. */
10105 string_arg
= TREE_VALUE (arglist
);
10106 /* New argument list transforming fputs(string, stream) to
10107 fwrite(string, 1, len, stream). */
10108 arglist
= build_tree_list (NULL_TREE
,
10109 TREE_VALUE (TREE_CHAIN (arglist
)));
10110 arglist
= tree_cons (NULL_TREE
, len
, arglist
);
10111 arglist
= tree_cons (NULL_TREE
, size_one_node
, arglist
);
10112 arglist
= tree_cons (NULL_TREE
, string_arg
, arglist
);
10117 gcc_unreachable ();
10120 /* If the replacement _DECL isn't initialized, don't do the
10125 /* These optimizations are only performed when the result is ignored,
10126 hence there's no need to cast the result to integer_type_node. */
10127 return build_function_call_expr (fn
, arglist
);
10130 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
10131 produced. False otherwise. This is done so that we don't output the error
10132 or warning twice or three times. */
10134 fold_builtin_next_arg (tree arglist
)
10136 tree fntype
= TREE_TYPE (current_function_decl
);
10138 if (TYPE_ARG_TYPES (fntype
) == 0
10139 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype
)))
10140 == void_type_node
))
10142 error ("%<va_start%> used in function with fixed args");
10147 /* Evidently an out of date version of <stdarg.h>; can't validate
10148 va_start's second argument, but can still work as intended. */
10149 warning (0, "%<__builtin_next_arg%> called without an argument");
10152 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10153 when we checked the arguments and if needed issued a warning. */
10154 else if (!TREE_CHAIN (arglist
)
10155 || !integer_zerop (TREE_VALUE (arglist
))
10156 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist
)))
10157 || TREE_CHAIN (TREE_CHAIN (arglist
)))
10159 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
10160 tree arg
= TREE_VALUE (arglist
);
10162 if (TREE_CHAIN (arglist
))
10164 error ("%<va_start%> used with too many arguments");
10168 /* Strip off all nops for the sake of the comparison. This
10169 is not quite the same as STRIP_NOPS. It does more.
10170 We must also strip off INDIRECT_EXPR for C++ reference
10172 while (TREE_CODE (arg
) == NOP_EXPR
10173 || TREE_CODE (arg
) == CONVERT_EXPR
10174 || TREE_CODE (arg
) == NON_LVALUE_EXPR
10175 || TREE_CODE (arg
) == INDIRECT_REF
)
10176 arg
= TREE_OPERAND (arg
, 0);
10177 if (arg
!= last_parm
)
10179 /* FIXME: Sometimes with the tree optimizers we can get the
10180 not the last argument even though the user used the last
10181 argument. We just warn and set the arg to be the last
10182 argument so that we will get wrong-code because of
10184 warning (0, "second parameter of %<va_start%> not last named argument");
10186 /* We want to verify the second parameter just once before the tree
10187 optimizers are run and then avoid keeping it in the tree,
10188 as otherwise we could warn even for correct code like:
10189 void foo (int i, ...)
10190 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10191 TREE_VALUE (arglist
) = integer_zero_node
;
10192 TREE_CHAIN (arglist
) = build_tree_list (NULL
, integer_zero_node
);
10198 /* Simplify a call to the sprintf builtin.
10200 Return 0 if no simplification was possible, otherwise return the
10201 simplified form of the call as a tree. If IGNORED is true, it means that
10202 the caller does not use the returned value of the function. */
10205 fold_builtin_sprintf (tree arglist
, int ignored
)
10207 tree call
, retval
, dest
, fmt
;
10208 const char *fmt_str
= NULL
;
10210 /* Verify the required arguments in the original call. We deal with two
10211 types of sprintf() calls: 'sprintf (str, fmt)' and
10212 'sprintf (dest, "%s", orig)'. */
10213 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
)
10214 && !validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, POINTER_TYPE
,
10218 /* Get the destination string and the format specifier. */
10219 dest
= TREE_VALUE (arglist
);
10220 fmt
= TREE_VALUE (TREE_CHAIN (arglist
));
10222 /* Check whether the format is a literal string constant. */
10223 fmt_str
= c_getstr (fmt
);
10224 if (fmt_str
== NULL
)
10228 retval
= NULL_TREE
;
10230 if (!init_target_chars())
10233 /* If the format doesn't contain % args or %%, use strcpy. */
10234 if (strchr (fmt_str
, target_percent
) == NULL
)
10236 tree fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
10241 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
10242 'format' is known to contain no % formats. */
10243 arglist
= build_tree_list (NULL_TREE
, fmt
);
10244 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10245 call
= build_function_call_expr (fn
, arglist
);
10247 retval
= build_int_cst (NULL_TREE
, strlen (fmt_str
));
10250 /* If the format is "%s", use strcpy if the result isn't used. */
10251 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
10254 fn
= implicit_built_in_decls
[BUILT_IN_STRCPY
];
10259 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
10260 orig
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10261 arglist
= build_tree_list (NULL_TREE
, orig
);
10262 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10265 retval
= c_strlen (orig
, 1);
10266 if (!retval
|| TREE_CODE (retval
) != INTEGER_CST
)
10269 call
= build_function_call_expr (fn
, arglist
);
10272 if (call
&& retval
)
10274 retval
= fold_convert
10275 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls
[BUILT_IN_SPRINTF
])),
10277 return build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), call
, retval
);
10283 /* Expand a call to __builtin_object_size. */
10286 expand_builtin_object_size (tree exp
)
10289 int object_size_type
;
10290 tree fndecl
= get_callee_fndecl (exp
);
10291 tree arglist
= TREE_OPERAND (exp
, 1);
10292 location_t locus
= EXPR_LOCATION (exp
);
10294 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10296 error ("%Hfirst argument of %D must be a pointer, second integer constant",
10298 expand_builtin_trap ();
10302 ost
= TREE_VALUE (TREE_CHAIN (arglist
));
10305 if (TREE_CODE (ost
) != INTEGER_CST
10306 || tree_int_cst_sgn (ost
) < 0
10307 || compare_tree_int (ost
, 3) > 0)
10309 error ("%Hlast argument of %D is not integer constant between 0 and 3",
10311 expand_builtin_trap ();
10315 object_size_type
= tree_low_cst (ost
, 0);
10317 return object_size_type
< 2 ? constm1_rtx
: const0_rtx
;
10320 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10321 FCODE is the BUILT_IN_* to use.
10322 Return 0 if we failed; the caller should emit a normal call,
10323 otherwise try to get the result in TARGET, if convenient (and in
10324 mode MODE if that's convenient). */
10327 expand_builtin_memory_chk (tree exp
, rtx target
, enum machine_mode mode
,
10328 enum built_in_function fcode
)
10330 tree arglist
= TREE_OPERAND (exp
, 1);
10331 tree dest
, src
, len
, size
;
10333 if (!validate_arglist (arglist
,
10335 fcode
== BUILT_IN_MEMSET_CHK
10336 ? INTEGER_TYPE
: POINTER_TYPE
,
10337 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10340 dest
= TREE_VALUE (arglist
);
10341 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10342 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10343 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist
))));
10345 if (! host_integerp (size
, 1))
10348 if (host_integerp (len
, 1) || integer_all_onesp (size
))
10352 if (! integer_all_onesp (size
) && tree_int_cst_lt (size
, len
))
10354 location_t locus
= EXPR_LOCATION (exp
);
10355 warning (0, "%Hcall to %D will always overflow destination buffer",
10356 &locus
, get_callee_fndecl (exp
));
10360 arglist
= build_tree_list (NULL_TREE
, len
);
10361 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10362 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10365 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10366 mem{cpy,pcpy,move,set} is available. */
10369 case BUILT_IN_MEMCPY_CHK
:
10370 fn
= built_in_decls
[BUILT_IN_MEMCPY
];
10372 case BUILT_IN_MEMPCPY_CHK
:
10373 fn
= built_in_decls
[BUILT_IN_MEMPCPY
];
10375 case BUILT_IN_MEMMOVE_CHK
:
10376 fn
= built_in_decls
[BUILT_IN_MEMMOVE
];
10378 case BUILT_IN_MEMSET_CHK
:
10379 fn
= built_in_decls
[BUILT_IN_MEMSET
];
10388 fn
= build_function_call_expr (fn
, arglist
);
10389 if (TREE_CODE (fn
) == CALL_EXPR
)
10390 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
10391 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
10393 else if (fcode
== BUILT_IN_MEMSET_CHK
)
10397 unsigned int dest_align
10398 = get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
10400 /* If DEST is not a pointer type, call the normal function. */
10401 if (dest_align
== 0)
10404 /* If SRC and DEST are the same (and not volatile), do nothing. */
10405 if (operand_equal_p (src
, dest
, 0))
10409 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
10411 /* Evaluate and ignore LEN in case it has side-effects. */
10412 expand_expr (len
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
10413 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
10416 len
= fold_convert (TREE_TYPE (dest
), len
);
10417 expr
= fold_build2 (PLUS_EXPR
, TREE_TYPE (dest
), dest
, len
);
10418 return expand_expr (expr
, target
, mode
, EXPAND_NORMAL
);
10421 /* __memmove_chk special case. */
10422 if (fcode
== BUILT_IN_MEMMOVE_CHK
)
10424 unsigned int src_align
10425 = get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
10427 if (src_align
== 0)
10430 /* If src is categorized for a readonly section we can use
10431 normal __memcpy_chk. */
10432 if (readonly_data_expr (src
))
10434 tree fn
= built_in_decls
[BUILT_IN_MEMCPY_CHK
];
10437 fn
= build_function_call_expr (fn
, arglist
);
10438 if (TREE_CODE (fn
) == CALL_EXPR
)
10439 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
10440 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
10447 /* Emit warning if a buffer overflow is detected at compile time. */
10450 maybe_emit_chk_warning (tree exp
, enum built_in_function fcode
)
10452 int arg_mask
, is_strlen
= 0;
10453 tree arglist
= TREE_OPERAND (exp
, 1), a
;
10459 case BUILT_IN_STRCPY_CHK
:
10460 case BUILT_IN_STPCPY_CHK
:
10461 /* For __strcat_chk the warning will be emitted only if overflowing
10462 by at least strlen (dest) + 1 bytes. */
10463 case BUILT_IN_STRCAT_CHK
:
10467 case BUILT_IN_STRNCAT_CHK
:
10468 /* For __strncat_chk the warning will be emitted only if overflowing
10469 by at least strlen (dest) + 1 bytes. */
10472 case BUILT_IN_STRNCPY_CHK
:
10475 case BUILT_IN_SNPRINTF_CHK
:
10476 case BUILT_IN_VSNPRINTF_CHK
:
10480 gcc_unreachable ();
10485 for (a
= arglist
; a
&& arg_mask
; a
= TREE_CHAIN (a
), arg_mask
>>= 1)
10497 len
= TREE_VALUE (len
);
10498 size
= TREE_VALUE (size
);
10500 if (! host_integerp (size
, 1) || integer_all_onesp (size
))
10505 len
= c_strlen (len
, 1);
10506 if (! len
|| ! host_integerp (len
, 1) || tree_int_cst_lt (len
, size
))
10509 else if (fcode
== BUILT_IN_STRNCAT_CHK
)
10511 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
10512 if (! src
|| ! host_integerp (len
, 1) || tree_int_cst_lt (len
, size
))
10514 src
= c_strlen (src
, 1);
10515 if (! src
|| ! host_integerp (src
, 1))
10517 locus
= EXPR_LOCATION (exp
);
10518 warning (0, "%Hcall to %D might overflow destination buffer",
10519 &locus
, get_callee_fndecl (exp
));
10522 else if (tree_int_cst_lt (src
, size
))
10525 else if (! host_integerp (len
, 1) || ! tree_int_cst_lt (size
, len
))
10528 locus
= EXPR_LOCATION (exp
);
10529 warning (0, "%Hcall to %D will always overflow destination buffer",
10530 &locus
, get_callee_fndecl (exp
));
10533 /* Emit warning if a buffer overflow is detected at compile time
10534 in __sprintf_chk/__vsprintf_chk calls. */
10537 maybe_emit_sprintf_chk_warning (tree exp
, enum built_in_function fcode
)
10539 tree arglist
= TREE_OPERAND (exp
, 1);
10540 tree dest
, size
, len
, fmt
, flag
;
10541 const char *fmt_str
;
10543 /* Verify the required arguments in the original call. */
10546 dest
= TREE_VALUE (arglist
);
10547 arglist
= TREE_CHAIN (arglist
);
10550 flag
= TREE_VALUE (arglist
);
10551 arglist
= TREE_CHAIN (arglist
);
10554 size
= TREE_VALUE (arglist
);
10555 arglist
= TREE_CHAIN (arglist
);
10558 fmt
= TREE_VALUE (arglist
);
10559 arglist
= TREE_CHAIN (arglist
);
10561 if (! host_integerp (size
, 1) || integer_all_onesp (size
))
10564 /* Check whether the format is a literal string constant. */
10565 fmt_str
= c_getstr (fmt
);
10566 if (fmt_str
== NULL
)
10569 if (!init_target_chars())
10572 /* If the format doesn't contain % args or %%, we know its size. */
10573 if (strchr (fmt_str
, target_percent
) == 0)
10574 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
10575 /* If the format is "%s" and first ... argument is a string literal,
10577 else if (fcode
== BUILT_IN_SPRINTF_CHK
&& strcmp (fmt_str
, target_percent_s
) == 0)
10583 arg
= TREE_VALUE (arglist
);
10584 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
10587 len
= c_strlen (arg
, 1);
10588 if (!len
|| ! host_integerp (len
, 1))
10594 if (! tree_int_cst_lt (len
, size
))
10596 location_t locus
= EXPR_LOCATION (exp
);
10597 warning (0, "%Hcall to %D will always overflow destination buffer",
10598 &locus
, get_callee_fndecl (exp
));
10602 /* Fold a call to __builtin_object_size, if possible. */
10605 fold_builtin_object_size (tree arglist
)
10607 tree ptr
, ost
, ret
= 0;
10608 int object_size_type
;
10610 if (!validate_arglist (arglist
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10613 ptr
= TREE_VALUE (arglist
);
10614 ost
= TREE_VALUE (TREE_CHAIN (arglist
));
10617 if (TREE_CODE (ost
) != INTEGER_CST
10618 || tree_int_cst_sgn (ost
) < 0
10619 || compare_tree_int (ost
, 3) > 0)
10622 object_size_type
= tree_low_cst (ost
, 0);
10624 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10625 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10626 and (size_t) 0 for types 2 and 3. */
10627 if (TREE_SIDE_EFFECTS (ptr
))
10628 return fold_convert (size_type_node
,
10629 object_size_type
< 2
10630 ? integer_minus_one_node
: integer_zero_node
);
10632 if (TREE_CODE (ptr
) == ADDR_EXPR
)
10633 ret
= build_int_cstu (size_type_node
,
10634 compute_builtin_object_size (ptr
, object_size_type
));
10636 else if (TREE_CODE (ptr
) == SSA_NAME
)
10638 unsigned HOST_WIDE_INT bytes
;
10640 /* If object size is not known yet, delay folding until
10641 later. Maybe subsequent passes will help determining
10643 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
10644 if (bytes
!= (unsigned HOST_WIDE_INT
) (object_size_type
< 2
10646 ret
= build_int_cstu (size_type_node
, bytes
);
10651 ret
= force_fit_type (ret
, -1, false, false);
10652 if (TREE_CONSTANT_OVERFLOW (ret
))
10659 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10660 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10661 code of the builtin. If MAXLEN is not NULL, it is maximum length
10662 passed as third argument. */
10665 fold_builtin_memory_chk (tree fndecl
, tree arglist
, tree maxlen
, bool ignore
,
10666 enum built_in_function fcode
)
10668 tree dest
, src
, len
, size
, fn
;
10670 if (!validate_arglist (arglist
,
10672 fcode
== BUILT_IN_MEMSET_CHK
10673 ? INTEGER_TYPE
: POINTER_TYPE
,
10674 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10677 dest
= TREE_VALUE (arglist
);
10678 /* Actually val for __memset_chk, but it doesn't matter. */
10679 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10680 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10681 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist
))));
10683 /* If SRC and DEST are the same (and not volatile), return DEST
10684 (resp. DEST+LEN for __mempcpy_chk). */
10685 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
10687 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
10688 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl
)), dest
, len
);
10691 tree temp
= fold_convert (TREE_TYPE (dest
), len
);
10692 temp
= fold_build2 (PLUS_EXPR
, TREE_TYPE (dest
), dest
, temp
);
10693 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), temp
);
10697 if (! host_integerp (size
, 1))
10700 if (! integer_all_onesp (size
))
10702 if (! host_integerp (len
, 1))
10704 /* If LEN is not constant, try MAXLEN too.
10705 For MAXLEN only allow optimizing into non-_ocs function
10706 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10707 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
10709 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
10711 /* (void) __mempcpy_chk () can be optimized into
10712 (void) __memcpy_chk (). */
10713 fn
= built_in_decls
[BUILT_IN_MEMCPY_CHK
];
10717 return build_function_call_expr (fn
, arglist
);
10725 if (tree_int_cst_lt (size
, maxlen
))
10729 arglist
= build_tree_list (NULL_TREE
, len
);
10730 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10731 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10734 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10735 mem{cpy,pcpy,move,set} is available. */
10738 case BUILT_IN_MEMCPY_CHK
:
10739 fn
= built_in_decls
[BUILT_IN_MEMCPY
];
10741 case BUILT_IN_MEMPCPY_CHK
:
10742 fn
= built_in_decls
[BUILT_IN_MEMPCPY
];
10744 case BUILT_IN_MEMMOVE_CHK
:
10745 fn
= built_in_decls
[BUILT_IN_MEMMOVE
];
10747 case BUILT_IN_MEMSET_CHK
:
10748 fn
= built_in_decls
[BUILT_IN_MEMSET
];
10757 return build_function_call_expr (fn
, arglist
);
10760 /* Fold a call to the __st[rp]cpy_chk builtin.
10761 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10762 code of the builtin. If MAXLEN is not NULL, it is maximum length of
10763 strings passed as second argument. */
10766 fold_builtin_stxcpy_chk (tree fndecl
, tree arglist
, tree maxlen
, bool ignore
,
10767 enum built_in_function fcode
)
10769 tree dest
, src
, size
, len
, fn
;
10771 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
,
10775 dest
= TREE_VALUE (arglist
);
10776 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10777 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10779 /* If SRC and DEST are the same (and not volatile), return DEST. */
10780 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
10781 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), dest
);
10783 if (! host_integerp (size
, 1))
10786 if (! integer_all_onesp (size
))
10788 len
= c_strlen (src
, 1);
10789 if (! len
|| ! host_integerp (len
, 1))
10791 /* If LEN is not constant, try MAXLEN too.
10792 For MAXLEN only allow optimizing into non-_ocs function
10793 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10794 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
10796 if (fcode
== BUILT_IN_STPCPY_CHK
)
10801 /* If return value of __stpcpy_chk is ignored,
10802 optimize into __strcpy_chk. */
10803 fn
= built_in_decls
[BUILT_IN_STRCPY_CHK
];
10807 return build_function_call_expr (fn
, arglist
);
10810 if (! len
|| TREE_SIDE_EFFECTS (len
))
10813 /* If c_strlen returned something, but not a constant,
10814 transform __strcpy_chk into __memcpy_chk. */
10815 fn
= built_in_decls
[BUILT_IN_MEMCPY_CHK
];
10819 len
= size_binop (PLUS_EXPR
, len
, ssize_int (1));
10820 arglist
= build_tree_list (NULL_TREE
, size
);
10821 arglist
= tree_cons (NULL_TREE
, len
, arglist
);
10822 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10823 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10824 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)),
10825 build_function_call_expr (fn
, arglist
));
10831 if (! tree_int_cst_lt (maxlen
, size
))
10835 arglist
= build_tree_list (NULL_TREE
, src
);
10836 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10838 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
10839 fn
= built_in_decls
[fcode
== BUILT_IN_STPCPY_CHK
10840 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
];
10844 return build_function_call_expr (fn
, arglist
);
10847 /* Fold a call to the __strncpy_chk builtin.
10848 If MAXLEN is not NULL, it is maximum length passed as third argument. */
10851 fold_builtin_strncpy_chk (tree arglist
, tree maxlen
)
10853 tree dest
, src
, size
, len
, fn
;
10855 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
,
10856 INTEGER_TYPE
, VOID_TYPE
))
10859 dest
= TREE_VALUE (arglist
);
10860 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10861 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10862 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist
))));
10864 if (! host_integerp (size
, 1))
10867 if (! integer_all_onesp (size
))
10869 if (! host_integerp (len
, 1))
10871 /* If LEN is not constant, try MAXLEN too.
10872 For MAXLEN only allow optimizing into non-_ocs function
10873 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10874 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
10880 if (tree_int_cst_lt (size
, maxlen
))
10884 arglist
= build_tree_list (NULL_TREE
, len
);
10885 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10886 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10888 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
10889 fn
= built_in_decls
[BUILT_IN_STRNCPY
];
10893 return build_function_call_expr (fn
, arglist
);
10896 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */
10899 fold_builtin_strcat_chk (tree fndecl
, tree arglist
)
10901 tree dest
, src
, size
, fn
;
10904 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
,
10908 dest
= TREE_VALUE (arglist
);
10909 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10910 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10912 p
= c_getstr (src
);
10913 /* If the SRC parameter is "", return DEST. */
10914 if (p
&& *p
== '\0')
10915 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
10917 if (! host_integerp (size
, 1) || ! integer_all_onesp (size
))
10920 arglist
= build_tree_list (NULL_TREE
, src
);
10921 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10923 /* If __builtin_strcat_chk is used, assume strcat is available. */
10924 fn
= built_in_decls
[BUILT_IN_STRCAT
];
10928 return build_function_call_expr (fn
, arglist
);
10931 /* Fold a call to the __strncat_chk builtin EXP. */
10934 fold_builtin_strncat_chk (tree fndecl
, tree arglist
)
10936 tree dest
, src
, size
, len
, fn
;
10939 if (!validate_arglist (arglist
, POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
,
10940 INTEGER_TYPE
, VOID_TYPE
))
10943 dest
= TREE_VALUE (arglist
);
10944 src
= TREE_VALUE (TREE_CHAIN (arglist
));
10945 len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
10946 size
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist
))));
10948 p
= c_getstr (src
);
10949 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
10950 if (p
&& *p
== '\0')
10951 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl
)), dest
, len
);
10952 else if (integer_zerop (len
))
10953 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
10955 if (! host_integerp (size
, 1))
10958 if (! integer_all_onesp (size
))
10960 tree src_len
= c_strlen (src
, 1);
10962 && host_integerp (src_len
, 1)
10963 && host_integerp (len
, 1)
10964 && ! tree_int_cst_lt (len
, src_len
))
10966 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
10967 fn
= built_in_decls
[BUILT_IN_STRCAT_CHK
];
10971 arglist
= build_tree_list (NULL_TREE
, size
);
10972 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10973 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10974 return build_function_call_expr (fn
, arglist
);
10979 arglist
= build_tree_list (NULL_TREE
, len
);
10980 arglist
= tree_cons (NULL_TREE
, src
, arglist
);
10981 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
10983 /* If __builtin_strncat_chk is used, assume strncat is available. */
10984 fn
= built_in_decls
[BUILT_IN_STRNCAT
];
10988 return build_function_call_expr (fn
, arglist
);
10991 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if
10992 a normal call should be emitted rather than expanding the function
10993 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
10996 fold_builtin_sprintf_chk (tree arglist
, enum built_in_function fcode
)
10998 tree dest
, size
, len
, fn
, fmt
, flag
;
10999 const char *fmt_str
;
11001 /* Verify the required arguments in the original call. */
11004 dest
= TREE_VALUE (arglist
);
11005 if (! POINTER_TYPE_P (TREE_TYPE (dest
)))
11007 arglist
= TREE_CHAIN (arglist
);
11010 flag
= TREE_VALUE (arglist
);
11011 if (TREE_CODE (TREE_TYPE (flag
)) != INTEGER_TYPE
)
11013 arglist
= TREE_CHAIN (arglist
);
11016 size
= TREE_VALUE (arglist
);
11017 if (TREE_CODE (TREE_TYPE (size
)) != INTEGER_TYPE
)
11019 arglist
= TREE_CHAIN (arglist
);
11022 fmt
= TREE_VALUE (arglist
);
11023 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
11025 arglist
= TREE_CHAIN (arglist
);
11027 if (! host_integerp (size
, 1))
11032 if (!init_target_chars())
11035 /* Check whether the format is a literal string constant. */
11036 fmt_str
= c_getstr (fmt
);
11037 if (fmt_str
!= NULL
)
11039 /* If the format doesn't contain % args or %%, we know the size. */
11040 if (strchr (fmt_str
, target_percent
) == 0)
11042 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| arglist
== NULL_TREE
)
11043 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
11045 /* If the format is "%s" and first ... argument is a string literal,
11046 we know the size too. */
11047 else if (fcode
== BUILT_IN_SPRINTF_CHK
&& strcmp (fmt_str
, target_percent_s
) == 0)
11051 if (arglist
&& !TREE_CHAIN (arglist
))
11053 arg
= TREE_VALUE (arglist
);
11054 if (POINTER_TYPE_P (TREE_TYPE (arg
)))
11056 len
= c_strlen (arg
, 1);
11057 if (! len
|| ! host_integerp (len
, 1))
11064 if (! integer_all_onesp (size
))
11066 if (! len
|| ! tree_int_cst_lt (len
, size
))
11070 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
11071 or if format doesn't contain % chars or is "%s". */
11072 if (! integer_zerop (flag
))
11074 if (fmt_str
== NULL
)
11076 if (strchr (fmt_str
, target_percent
) != NULL
&& strcmp (fmt_str
, target_percent_s
))
11080 arglist
= tree_cons (NULL_TREE
, fmt
, arglist
);
11081 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
11083 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
11084 fn
= built_in_decls
[fcode
== BUILT_IN_VSPRINTF_CHK
11085 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
];
11089 return build_function_call_expr (fn
, arglist
);
11092 /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if
11093 a normal call should be emitted rather than expanding the function
11094 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
11095 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
11096 passed as second argument. */
11099 fold_builtin_snprintf_chk (tree arglist
, tree maxlen
,
11100 enum built_in_function fcode
)
11102 tree dest
, size
, len
, fn
, fmt
, flag
;
11103 const char *fmt_str
;
11105 /* Verify the required arguments in the original call. */
11108 dest
= TREE_VALUE (arglist
);
11109 if (! POINTER_TYPE_P (TREE_TYPE (dest
)))
11111 arglist
= TREE_CHAIN (arglist
);
11114 len
= TREE_VALUE (arglist
);
11115 if (TREE_CODE (TREE_TYPE (len
)) != INTEGER_TYPE
)
11117 arglist
= TREE_CHAIN (arglist
);
11120 flag
= TREE_VALUE (arglist
);
11121 if (TREE_CODE (TREE_TYPE (len
)) != INTEGER_TYPE
)
11123 arglist
= TREE_CHAIN (arglist
);
11126 size
= TREE_VALUE (arglist
);
11127 if (TREE_CODE (TREE_TYPE (size
)) != INTEGER_TYPE
)
11129 arglist
= TREE_CHAIN (arglist
);
11132 fmt
= TREE_VALUE (arglist
);
11133 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
11135 arglist
= TREE_CHAIN (arglist
);
11137 if (! host_integerp (size
, 1))
11140 if (! integer_all_onesp (size
))
11142 if (! host_integerp (len
, 1))
11144 /* If LEN is not constant, try MAXLEN too.
11145 For MAXLEN only allow optimizing into non-_ocs function
11146 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11147 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
11153 if (tree_int_cst_lt (size
, maxlen
))
11157 if (!init_target_chars())
11160 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
11161 or if format doesn't contain % chars or is "%s". */
11162 if (! integer_zerop (flag
))
11164 fmt_str
= c_getstr (fmt
);
11165 if (fmt_str
== NULL
)
11167 if (strchr (fmt_str
, target_percent
) != NULL
&& strcmp (fmt_str
, target_percent_s
))
11171 arglist
= tree_cons (NULL_TREE
, fmt
, arglist
);
11172 arglist
= tree_cons (NULL_TREE
, len
, arglist
);
11173 arglist
= tree_cons (NULL_TREE
, dest
, arglist
);
11175 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
11177 fn
= built_in_decls
[fcode
== BUILT_IN_VSNPRINTF_CHK
11178 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
];
11182 return build_function_call_expr (fn
, arglist
);
11185 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11187 Return 0 if no simplification was possible, otherwise return the
11188 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11189 code of the function to be simplified. */
11192 fold_builtin_printf (tree fndecl
, tree arglist
, bool ignore
,
11193 enum built_in_function fcode
)
11195 tree fmt
, fn
= NULL_TREE
, fn_putchar
, fn_puts
, arg
, call
;
11196 const char *fmt_str
= NULL
;
11198 /* If the return value is used, don't do the transformation. */
11202 /* Verify the required arguments in the original call. */
11203 if (fcode
== BUILT_IN_PRINTF_CHK
|| fcode
== BUILT_IN_VPRINTF_CHK
)
11209 flag
= TREE_VALUE (arglist
);
11210 if (TREE_CODE (TREE_TYPE (flag
)) != INTEGER_TYPE
11211 || TREE_SIDE_EFFECTS (flag
))
11213 arglist
= TREE_CHAIN (arglist
);
11218 fmt
= TREE_VALUE (arglist
);
11219 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
11221 arglist
= TREE_CHAIN (arglist
);
11223 /* Check whether the format is a literal string constant. */
11224 fmt_str
= c_getstr (fmt
);
11225 if (fmt_str
== NULL
)
11228 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
11230 /* If we're using an unlocked function, assume the other
11231 unlocked functions exist explicitly. */
11232 fn_putchar
= built_in_decls
[BUILT_IN_PUTCHAR_UNLOCKED
];
11233 fn_puts
= built_in_decls
[BUILT_IN_PUTS_UNLOCKED
];
11237 fn_putchar
= implicit_built_in_decls
[BUILT_IN_PUTCHAR
];
11238 fn_puts
= implicit_built_in_decls
[BUILT_IN_PUTS
];
11241 if (!init_target_chars())
11244 if (strcmp (fmt_str
, target_percent_s
) == 0 || strchr (fmt_str
, target_percent
) == NULL
)
11248 if (strcmp (fmt_str
, target_percent_s
) == 0)
11250 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
11254 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
11255 || TREE_CHAIN (arglist
))
11258 str
= c_getstr (TREE_VALUE (arglist
));
11264 /* The format specifier doesn't contain any '%' characters. */
11265 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
11271 /* If the string was "", printf does nothing. */
11272 if (str
[0] == '\0')
11273 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
11275 /* If the string has length of 1, call putchar. */
11276 if (str
[1] == '\0')
11278 /* Given printf("c"), (where c is any one character,)
11279 convert "c"[0] to an int and pass that to the replacement
11281 arg
= build_int_cst (NULL_TREE
, str
[0]);
11282 arglist
= build_tree_list (NULL_TREE
, arg
);
11287 /* If the string was "string\n", call puts("string"). */
11288 size_t len
= strlen (str
);
11289 if ((unsigned char)str
[len
- 1] == target_newline
)
11291 /* Create a NUL-terminated string that's one char shorter
11292 than the original, stripping off the trailing '\n'. */
11293 char *newstr
= alloca (len
);
11294 memcpy (newstr
, str
, len
- 1);
11295 newstr
[len
- 1] = 0;
11297 arg
= build_string_literal (len
, newstr
);
11298 arglist
= build_tree_list (NULL_TREE
, arg
);
11302 /* We'd like to arrange to call fputs(string,stdout) here,
11303 but we need stdout and don't have a way to get it yet. */
11308 /* The other optimizations can be done only on the non-va_list variants. */
11309 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
11312 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11313 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
11316 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
11317 || TREE_CHAIN (arglist
))
11322 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11323 else if (strcmp (fmt_str
, target_percent_c
) == 0)
11326 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
11327 || TREE_CHAIN (arglist
))
11335 call
= build_function_call_expr (fn
, arglist
);
11336 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), call
);
11339 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11341 Return 0 if no simplification was possible, otherwise return the
11342 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11343 code of the function to be simplified. */
11346 fold_builtin_fprintf (tree fndecl
, tree arglist
, bool ignore
,
11347 enum built_in_function fcode
)
11349 tree fp
, fmt
, fn
= NULL_TREE
, fn_fputc
, fn_fputs
, arg
, call
;
11350 const char *fmt_str
= NULL
;
11352 /* If the return value is used, don't do the transformation. */
11356 /* Verify the required arguments in the original call. */
11359 fp
= TREE_VALUE (arglist
);
11360 if (! POINTER_TYPE_P (TREE_TYPE (fp
)))
11362 arglist
= TREE_CHAIN (arglist
);
11364 if (fcode
== BUILT_IN_FPRINTF_CHK
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
11370 flag
= TREE_VALUE (arglist
);
11371 if (TREE_CODE (TREE_TYPE (flag
)) != INTEGER_TYPE
11372 || TREE_SIDE_EFFECTS (flag
))
11374 arglist
= TREE_CHAIN (arglist
);
11379 fmt
= TREE_VALUE (arglist
);
11380 if (! POINTER_TYPE_P (TREE_TYPE (fmt
)))
11382 arglist
= TREE_CHAIN (arglist
);
11384 /* Check whether the format is a literal string constant. */
11385 fmt_str
= c_getstr (fmt
);
11386 if (fmt_str
== NULL
)
11389 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
11391 /* If we're using an unlocked function, assume the other
11392 unlocked functions exist explicitly. */
11393 fn_fputc
= built_in_decls
[BUILT_IN_FPUTC_UNLOCKED
];
11394 fn_fputs
= built_in_decls
[BUILT_IN_FPUTS_UNLOCKED
];
11398 fn_fputc
= implicit_built_in_decls
[BUILT_IN_FPUTC
];
11399 fn_fputs
= implicit_built_in_decls
[BUILT_IN_FPUTS
];
11402 if (!init_target_chars())
11405 /* If the format doesn't contain % args or %%, use strcpy. */
11406 if (strchr (fmt_str
, target_percent
) == NULL
)
11408 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
11412 /* If the format specifier was "", fprintf does nothing. */
11413 if (fmt_str
[0] == '\0')
11415 /* If FP has side-effects, just wait until gimplification is
11417 if (TREE_SIDE_EFFECTS (fp
))
11420 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
11423 /* When "string" doesn't contain %, replace all cases of
11424 fprintf (fp, string) with fputs (string, fp). The fputs
11425 builtin will take care of special cases like length == 1. */
11426 arglist
= build_tree_list (NULL_TREE
, fp
);
11427 arglist
= tree_cons (NULL_TREE
, fmt
, arglist
);
11431 /* The other optimizations can be done only on the non-va_list variants. */
11432 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
11435 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11436 else if (strcmp (fmt_str
, target_percent_s
) == 0)
11439 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
11440 || TREE_CHAIN (arglist
))
11442 arg
= TREE_VALUE (arglist
);
11443 arglist
= build_tree_list (NULL_TREE
, fp
);
11444 arglist
= tree_cons (NULL_TREE
, arg
, arglist
);
11448 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11449 else if (strcmp (fmt_str
, target_percent_c
) == 0)
11452 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
11453 || TREE_CHAIN (arglist
))
11455 arg
= TREE_VALUE (arglist
);
11456 arglist
= build_tree_list (NULL_TREE
, fp
);
11457 arglist
= tree_cons (NULL_TREE
, arg
, arglist
);
11464 call
= build_function_call_expr (fn
, arglist
);
11465 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl
)), call
);
11468 /* Initialize format string characters in the target charset. */
11471 init_target_chars (void)
11476 target_newline
= lang_hooks
.to_target_charset ('\n');
11477 target_percent
= lang_hooks
.to_target_charset ('%');
11478 target_c
= lang_hooks
.to_target_charset ('c');
11479 target_s
= lang_hooks
.to_target_charset ('s');
11480 if (target_newline
== 0 || target_percent
== 0 || target_c
== 0
11484 target_percent_c
[0] = target_percent
;
11485 target_percent_c
[1] = target_c
;
11486 target_percent_c
[2] = '\0';
11488 target_percent_s
[0] = target_percent
;
11489 target_percent_s
[1] = target_s
;
11490 target_percent_s
[2] = '\0';
11492 target_percent_s_newline
[0] = target_percent
;
11493 target_percent_s_newline
[1] = target_s
;
11494 target_percent_s_newline
[2] = target_newline
;
11495 target_percent_s_newline
[3] = '\0';
11502 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11503 and no overflow/underflow occurred. INEXACT is true if M was not
11504 exactly calculated. TYPE is the tree type for the result. This
11505 function assumes that you cleared the MPFR flags and then
11506 calculated M to see if anything subsequently set a flag prior to
11507 entering this function. Return NULL_TREE if any checks fail. */
11510 do_mpfr_ckconv(mpfr_srcptr m
, tree type
, int inexact
)
11512 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11513 overflow/underflow occurred. If -frounding-math, proceed iff the
11514 result of calling FUNC was exact. */
11515 if (mpfr_number_p (m
) && !mpfr_overflow_p() && !mpfr_underflow_p()
11516 && (!flag_rounding_math
|| !inexact
))
11518 REAL_VALUE_TYPE rr
;
11520 real_from_mpfr (&rr
, m
);
11521 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11522 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11523 but the mpft_t is not, then we underflowed in the
11525 if (!real_isnan (&rr
) && !real_isinf (&rr
)
11526 && (rr
.cl
== rvc_zero
) == (mpfr_zero_p (m
) != 0))
11528 REAL_VALUE_TYPE rmode
;
11530 real_convert (&rmode
, TYPE_MODE (type
), &rr
);
11531 /* Proceed iff the specified mode can hold the value. */
11532 if (real_identical (&rmode
, &rr
))
11533 return build_real (type
, rmode
);
11539 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11540 FUNC on it and return the resulting value as a tree with type TYPE.
11541 If MIN and/or MAX are not NULL, then the supplied ARG must be
11542 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11543 acceptable values, otherwise they are not. The mpfr precision is
11544 set to the precision of TYPE. We assume that function FUNC returns
11545 zero if the result could be calculated exactly within the requested
11549 do_mpfr_arg1 (tree arg
, tree type
, int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
11550 const REAL_VALUE_TYPE
*min
, const REAL_VALUE_TYPE
*max
,
11553 tree result
= NULL_TREE
;
11557 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
11559 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
11561 if (!real_isnan (ra
) && !real_isinf (ra
)
11562 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
))
11563 && (!max
|| real_compare (inclusive
? LE_EXPR
: LT_EXPR
, ra
, max
)))
11565 const int prec
= REAL_MODE_FORMAT (TYPE_MODE (type
))->p
;
11569 mpfr_init2 (m
, prec
);
11570 mpfr_from_real (m
, ra
);
11571 mpfr_clear_flags();
11572 inexact
= func (m
, m
, GMP_RNDN
);
11573 result
= do_mpfr_ckconv (m
, type
, inexact
);
11581 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11582 FUNC on it and return the resulting value as a tree with type TYPE.
11583 The mpfr precision is set to the precision of TYPE. We assume that
11584 function FUNC returns zero if the result could be calculated
11585 exactly within the requested precision. */
11588 do_mpfr_arg2 (tree arg1
, tree arg2
, tree type
,
11589 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
11591 tree result
= NULL_TREE
;
11596 if (TREE_CODE (arg1
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg1
)
11597 && TREE_CODE (arg2
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg2
))
11599 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
11600 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
11602 if (!real_isnan (ra1
) && !real_isinf (ra1
)
11603 && !real_isnan (ra2
) && !real_isinf (ra2
))
11605 const int prec
= REAL_MODE_FORMAT (TYPE_MODE (type
))->p
;
11609 mpfr_inits2 (prec
, m1
, m2
, NULL
);
11610 mpfr_from_real (m1
, ra1
);
11611 mpfr_from_real (m2
, ra2
);
11612 mpfr_clear_flags();
11613 inexact
= func (m1
, m1
, m2
, GMP_RNDN
);
11614 result
= do_mpfr_ckconv (m1
, type
, inexact
);
11615 mpfr_clears (m1
, m2
, NULL
);
11622 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
11623 FUNC on it and return the resulting value as a tree with type TYPE.
11624 The mpfr precision is set to the precision of TYPE. We assume that
11625 function FUNC returns zero if the result could be calculated
11626 exactly within the requested precision. */
11629 do_mpfr_arg3 (tree arg1
, tree arg2
, tree arg3
, tree type
,
11630 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
11632 tree result
= NULL_TREE
;
11638 if (TREE_CODE (arg1
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg1
)
11639 && TREE_CODE (arg2
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg2
)
11640 && TREE_CODE (arg3
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg3
))
11642 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
11643 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
11644 const REAL_VALUE_TYPE
*const ra3
= &TREE_REAL_CST (arg3
);
11646 if (!real_isnan (ra1
) && !real_isinf (ra1
)
11647 && !real_isnan (ra2
) && !real_isinf (ra2
)
11648 && !real_isnan (ra3
) && !real_isinf (ra3
))
11650 const int prec
= REAL_MODE_FORMAT (TYPE_MODE (type
))->p
;
11654 mpfr_inits2 (prec
, m1
, m2
, m3
, NULL
);
11655 mpfr_from_real (m1
, ra1
);
11656 mpfr_from_real (m2
, ra2
);
11657 mpfr_from_real (m3
, ra3
);
11658 mpfr_clear_flags();
11659 inexact
= func (m1
, m1
, m2
, m3
, GMP_RNDN
);
11660 result
= do_mpfr_ckconv (m1
, type
, inexact
);
11661 mpfr_clears (m1
, m2
, m3
, NULL
);
11668 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11669 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11670 The type is taken from the type of ARG and is used for setting the
11671 precision of the calculation and results. */
11674 do_mpfr_sincos (tree arg
, tree arg_sinp
, tree arg_cosp
)
11676 tree result
= NULL_TREE
;
11680 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg
))
11682 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
11684 if (!real_isnan (ra
) && !real_isinf (ra
))
11686 tree
const type
= TREE_TYPE (arg
);
11687 const int prec
= REAL_MODE_FORMAT (TYPE_MODE (type
))->p
;
11688 tree result_s
, result_c
;
11692 mpfr_inits2 (prec
, m
, ms
, mc
, NULL
);
11693 mpfr_from_real (m
, ra
);
11694 mpfr_clear_flags();
11695 inexact
= mpfr_sin_cos (ms
, mc
, m
, GMP_RNDN
);
11696 result_s
= do_mpfr_ckconv (ms
, type
, inexact
);
11697 result_c
= do_mpfr_ckconv (mc
, type
, inexact
);
11698 mpfr_clears (m
, ms
, mc
, NULL
);
11699 if (result_s
&& result_c
)
11701 /* Dereference the sin/cos pointer arguments. */
11702 arg_sinp
= build_fold_indirect_ref (arg_sinp
);
11703 arg_cosp
= build_fold_indirect_ref (arg_cosp
);
11704 /* Proceed if valid pointer type were passed in. */
11705 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp
)) == TYPE_MAIN_VARIANT (type
)
11706 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp
)) == TYPE_MAIN_VARIANT (type
))
11708 /* Set the values. */
11709 result_s
= fold_build2 (GIMPLE_MODIFY_STMT
, type
, arg_sinp
,
11711 TREE_SIDE_EFFECTS (result_s
) = 1;
11712 result_c
= fold_build2 (GIMPLE_MODIFY_STMT
, type
, arg_cosp
,
11714 TREE_SIDE_EFFECTS (result_c
) = 1;
11715 /* Combine the assignments into a compound expr. */
11716 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
11717 result_s
, result_c
));