1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
30 #include "tree-gimple.h"
33 #include "hard-reg-set.h"
36 #include "insn-config.h"
42 #include "typeclass.h"
47 #include "langhooks.h"
48 #include "basic-block.h"
49 #include "tree-mudflap.h"
51 #define CALLED_AS_BUILT_IN(NODE) \
52 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
54 #ifndef PAD_VARARGS_DOWN
55 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
58 /* Define the names of the builtin function types and codes. */
59 const char *const built_in_class_names[4]
60 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
62 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
63 const char * built_in_names[(int) END_BUILTINS] =
65 #include "builtins.def"
69 /* Setup an array of _DECL trees, make sure each element is
70 initialized to NULL_TREE. */
71 tree built_in_decls[(int) END_BUILTINS];
72 /* Declarations used when constructing the builtin implicitly in the compiler.
73 It may be NULL_TREE when this is invalid (for instance runtime is not
74 required to implement the function call in all cases). */
75 tree implicit_built_in_decls[(int) END_BUILTINS];
77 static int get_pointer_alignment (tree, unsigned int);
78 static const char *c_getstr (tree);
79 static rtx c_readstr (const char *, enum machine_mode);
80 static int target_char_cast (tree, char *);
81 static rtx get_memory_rtx (tree);
82 static tree build_string_literal (int, const char *);
83 static int apply_args_size (void);
84 static int apply_result_size (void);
85 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
86 static rtx result_vector (int, rtx);
88 static rtx expand_builtin_setjmp (tree, rtx);
89 static void expand_builtin_update_setjmp_buf (rtx);
90 static void expand_builtin_prefetch (tree);
91 static rtx expand_builtin_apply_args (void);
92 static rtx expand_builtin_apply_args_1 (void);
93 static rtx expand_builtin_apply (rtx, rtx, rtx);
94 static void expand_builtin_return (rtx);
95 static enum type_class type_to_class (tree);
96 static rtx expand_builtin_classify_type (tree);
97 static void expand_errno_check (tree, rtx);
98 static rtx expand_builtin_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
101 static rtx expand_builtin_args_info (tree);
102 static rtx expand_builtin_next_arg (void);
103 static rtx expand_builtin_va_start (tree);
104 static rtx expand_builtin_va_end (tree);
105 static rtx expand_builtin_va_copy (tree);
106 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
107 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
108 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
109 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
110 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
114 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
116 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_bcopy (tree, tree);
118 static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode);
119 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
120 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
122 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_bzero (tree);
126 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
130 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
131 static rtx expand_builtin_alloca (tree, rtx);
132 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
133 static rtx expand_builtin_frame_address (tree, tree);
134 static rtx expand_builtin_fputs (tree, rtx, bool);
135 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
136 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
137 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
138 static tree stabilize_va_list (tree, int);
139 static rtx expand_builtin_expect (tree, rtx);
140 static tree fold_builtin_constant_p (tree);
141 static tree fold_builtin_classify_type (tree);
142 static tree fold_builtin_strlen (tree);
143 static tree fold_builtin_inf (tree, int);
144 static tree fold_builtin_nan (tree, tree, int);
145 static int validate_arglist (tree, ...);
146 static bool integer_valued_real_p (tree);
147 static tree fold_trunc_transparent_mathfn (tree);
148 static bool readonly_data_expr (tree);
149 static rtx expand_builtin_fabs (tree, rtx, rtx);
150 static rtx expand_builtin_signbit (tree, rtx);
151 static tree fold_builtin_cabs (tree, tree);
152 static tree fold_builtin_sqrt (tree, tree);
153 static tree fold_builtin_cbrt (tree, tree);
154 static tree fold_builtin_pow (tree, tree, tree);
155 static tree fold_builtin_powi (tree, tree, tree);
156 static tree fold_builtin_sin (tree);
157 static tree fold_builtin_cos (tree, tree, tree);
158 static tree fold_builtin_tan (tree);
159 static tree fold_builtin_atan (tree, tree);
160 static tree fold_builtin_trunc (tree);
161 static tree fold_builtin_floor (tree);
162 static tree fold_builtin_ceil (tree);
163 static tree fold_builtin_round (tree);
164 static tree fold_builtin_bitop (tree);
165 static tree fold_builtin_memcpy (tree);
166 static tree fold_builtin_mempcpy (tree, tree, int);
167 static tree fold_builtin_memmove (tree, tree);
168 static tree fold_builtin_strchr (tree, tree);
169 static tree fold_builtin_memcmp (tree);
170 static tree fold_builtin_strcmp (tree);
171 static tree fold_builtin_strncmp (tree);
172 static tree fold_builtin_signbit (tree);
173 static tree fold_builtin_copysign (tree, tree, tree);
174 static tree fold_builtin_isascii (tree);
175 static tree fold_builtin_toascii (tree);
176 static tree fold_builtin_isdigit (tree);
177 static tree fold_builtin_fabs (tree, tree);
178 static tree fold_builtin_abs (tree, tree);
179 static tree fold_builtin_unordered_cmp (tree, enum tree_code, enum tree_code);
180 static tree fold_builtin_1 (tree, bool);
182 static tree fold_builtin_strpbrk (tree, tree);
183 static tree fold_builtin_strstr (tree, tree);
184 static tree fold_builtin_strrchr (tree, tree);
185 static tree fold_builtin_strcat (tree);
186 static tree fold_builtin_strncat (tree);
187 static tree fold_builtin_strspn (tree);
188 static tree fold_builtin_strcspn (tree);
189 static tree fold_builtin_sprintf (tree, int);
192 /* Return the alignment in bits of EXP, a pointer valued expression.
193 But don't return more than MAX_ALIGN no matter what.
194 The alignment returned is, by default, the alignment of the thing that
195 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
197 Otherwise, look at the expression to see if we can do better, i.e., if the
198 expression is actually pointing at an object whose alignment is tighter. */
201 get_pointer_alignment (tree exp, unsigned int max_align)
203 unsigned int align, inner;
205 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
208 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
209 align = MIN (align, max_align);
213 switch (TREE_CODE (exp))
217 case NON_LVALUE_EXPR:
218 exp = TREE_OPERAND (exp, 0);
219 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
222 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
223 align = MIN (inner, max_align);
227 /* If sum of pointer + int, restrict our maximum alignment to that
228 imposed by the integer. If not, we can't do any better than
230 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
233 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
234 & (max_align / BITS_PER_UNIT - 1))
238 exp = TREE_OPERAND (exp, 0);
242 /* See what we are pointing at and look at its alignment. */
243 exp = TREE_OPERAND (exp, 0);
244 if (TREE_CODE (exp) == FUNCTION_DECL)
245 align = FUNCTION_BOUNDARY;
246 else if (DECL_P (exp))
247 align = DECL_ALIGN (exp);
248 #ifdef CONSTANT_ALIGNMENT
249 else if (CONSTANT_CLASS_P (exp))
250 align = CONSTANT_ALIGNMENT (exp, align);
252 return MIN (align, max_align);
260 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
261 way, because it could contain a zero byte in the middle.
262 TREE_STRING_LENGTH is the size of the character array, not the string.
264 ONLY_VALUE should be nonzero if the result is not going to be emitted
265 into the instruction stream and zero if it is going to be expanded.
266 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
267 is returned, otherwise NULL, since
268 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
269 evaluate the side-effects.
271 The value returned is of type `ssizetype'.
273 Unfortunately, string_constant can't access the values of const char
274 arrays with initializers, so neither can we do so here. */
277 c_strlen (tree src, int only_value)
280 HOST_WIDE_INT offset;
285 if (TREE_CODE (src) == COND_EXPR
286 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
290 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
291 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
292 if (tree_int_cst_equal (len1, len2))
296 if (TREE_CODE (src) == COMPOUND_EXPR
297 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
298 return c_strlen (TREE_OPERAND (src, 1), only_value);
300 src = string_constant (src, &offset_node);
304 max = TREE_STRING_LENGTH (src) - 1;
305 ptr = TREE_STRING_POINTER (src);
307 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
309 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
310 compute the offset to the following null if we don't know where to
311 start searching for it. */
314 for (i = 0; i < max; i++)
318 /* We don't know the starting offset, but we do know that the string
319 has no internal zero bytes. We can assume that the offset falls
320 within the bounds of the string; otherwise, the programmer deserves
321 what he gets. Subtract the offset from the length of the string,
322 and return that. This would perhaps not be valid if we were dealing
323 with named arrays in addition to literal string constants. */
325 return size_diffop (size_int (max), offset_node);
328 /* We have a known offset into the string. Start searching there for
329 a null character if we can represent it as a single HOST_WIDE_INT. */
330 if (offset_node == 0)
332 else if (! host_integerp (offset_node, 0))
335 offset = tree_low_cst (offset_node, 0);
337 /* If the offset is known to be out of bounds, warn, and call strlen at
339 if (offset < 0 || offset > max)
341 warning ("offset outside bounds of constant string");
345 /* Use strlen to search for the first zero byte. Since any strings
346 constructed with build_string will have nulls appended, we win even
347 if we get handed something like (char[4])"abcd".
349 Since OFFSET is our starting index into the string, no further
350 calculation is needed. */
351 return ssize_int (strlen (ptr + offset));
354 /* Return a char pointer for a C string if it is a string constant
355 or sum of string constant and integer constant. */
362 src = string_constant (src, &offset_node);
366 if (offset_node == 0)
367 return TREE_STRING_POINTER (src);
368 else if (!host_integerp (offset_node, 1)
369 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
372 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
375 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
376 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
379 c_readstr (const char *str, enum machine_mode mode)
385 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
390 for (i = 0; i < GET_MODE_SIZE (mode); i++)
393 if (WORDS_BIG_ENDIAN)
394 j = GET_MODE_SIZE (mode) - i - 1;
395 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
396 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
397 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
399 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
402 ch = (unsigned char) str[i];
403 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
405 return immed_double_const (c[0], c[1], mode);
408 /* Cast a target constant CST to target CHAR and if that value fits into
409 host char type, return zero and put that value into variable pointed by
413 target_char_cast (tree cst, char *p)
415 unsigned HOST_WIDE_INT val, hostval;
417 if (!host_integerp (cst, 1)
418 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
421 val = tree_low_cst (cst, 1);
422 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
423 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
426 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
427 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
436 /* Similar to save_expr, but assumes that arbitrary code is not executed
437 in between the multiple evaluations. In particular, we assume that a
438 non-addressable local variable will not be modified. */
441 builtin_save_expr (tree exp)
443 if (TREE_ADDRESSABLE (exp) == 0
444 && (TREE_CODE (exp) == PARM_DECL
445 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
448 return save_expr (exp);
451 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
452 times to get the address of either a higher stack frame, or a return
453 address located within it (depending on FNDECL_CODE). */
456 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
460 #ifdef INITIAL_FRAME_ADDRESS_RTX
461 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
463 rtx tem = hard_frame_pointer_rtx;
466 /* Some machines need special handling before we can access
467 arbitrary frames. For example, on the sparc, we must first flush
468 all register windows to the stack. */
469 #ifdef SETUP_FRAME_ADDRESSES
471 SETUP_FRAME_ADDRESSES ();
474 /* On the sparc, the return address is not in the frame, it is in a
475 register. There is no way to access it off of the current frame
476 pointer, but it can be accessed off the previous frame pointer by
477 reading the value from the register window save area. */
478 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
479 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
483 /* Scan back COUNT frames to the specified frame. */
484 for (i = 0; i < count; i++)
486 /* Assume the dynamic chain pointer is in the word that the
487 frame address points to, unless otherwise specified. */
488 #ifdef DYNAMIC_CHAIN_ADDRESS
489 tem = DYNAMIC_CHAIN_ADDRESS (tem);
491 tem = memory_address (Pmode, tem);
492 tem = gen_rtx_MEM (Pmode, tem);
493 set_mem_alias_set (tem, get_frame_alias_set ());
494 tem = copy_to_reg (tem);
497 /* For __builtin_frame_address, return what we've got. */
498 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
501 /* For __builtin_return_address, Get the return address from that
503 #ifdef RETURN_ADDR_RTX
504 tem = RETURN_ADDR_RTX (count, tem);
506 tem = memory_address (Pmode,
507 plus_constant (tem, GET_MODE_SIZE (Pmode)));
508 tem = gen_rtx_MEM (Pmode, tem);
509 set_mem_alias_set (tem, get_frame_alias_set ());
514 /* Alias set used for setjmp buffer. */
515 static HOST_WIDE_INT setjmp_alias_set = -1;
517 /* Construct the leading half of a __builtin_setjmp call. Control will
518 return to RECEIVER_LABEL. This is used directly by sjlj exception
522 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
524 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
528 if (setjmp_alias_set == -1)
529 setjmp_alias_set = new_alias_set ();
531 buf_addr = convert_memory_address (Pmode, buf_addr);
533 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
535 /* We store the frame pointer and the address of receiver_label in
536 the buffer and use the rest of it for the stack save area, which
537 is machine-dependent. */
539 mem = gen_rtx_MEM (Pmode, buf_addr);
540 set_mem_alias_set (mem, setjmp_alias_set);
541 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
543 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
544 set_mem_alias_set (mem, setjmp_alias_set);
546 emit_move_insn (validize_mem (mem),
547 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
549 stack_save = gen_rtx_MEM (sa_mode,
550 plus_constant (buf_addr,
551 2 * GET_MODE_SIZE (Pmode)));
552 set_mem_alias_set (stack_save, setjmp_alias_set);
553 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
555 /* If there is further processing to do, do it. */
556 #ifdef HAVE_builtin_setjmp_setup
557 if (HAVE_builtin_setjmp_setup)
558 emit_insn (gen_builtin_setjmp_setup (buf_addr));
561 /* Tell optimize_save_area_alloca that extra work is going to
562 need to go on during alloca. */
563 current_function_calls_setjmp = 1;
565 /* Set this so all the registers get saved in our frame; we need to be
566 able to copy the saved values for any registers from frames we unwind. */
567 current_function_has_nonlocal_label = 1;
570 /* Construct the trailing part of a __builtin_setjmp call.
571 This is used directly by sjlj exception handling code. */
574 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
576 /* Clobber the FP when we get here, so we have to make sure it's
577 marked as used by this function. */
578 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
580 /* Mark the static chain as clobbered here so life information
581 doesn't get messed up for it. */
582 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
584 /* Now put in the code to restore the frame pointer, and argument
585 pointer, if needed. */
586 #ifdef HAVE_nonlocal_goto
587 if (! HAVE_nonlocal_goto)
589 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
591 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
592 if (fixed_regs[ARG_POINTER_REGNUM])
594 #ifdef ELIMINABLE_REGS
596 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
598 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
599 if (elim_regs[i].from == ARG_POINTER_REGNUM
600 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
603 if (i == ARRAY_SIZE (elim_regs))
606 /* Now restore our arg pointer from the address at which it
607 was saved in our stack frame. */
608 emit_move_insn (virtual_incoming_args_rtx,
609 copy_to_reg (get_arg_pointer_save_area (cfun)));
614 #ifdef HAVE_builtin_setjmp_receiver
615 if (HAVE_builtin_setjmp_receiver)
616 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
619 #ifdef HAVE_nonlocal_goto_receiver
620 if (HAVE_nonlocal_goto_receiver)
621 emit_insn (gen_nonlocal_goto_receiver ());
626 /* @@@ This is a kludge. Not all machine descriptions define a blockage
627 insn, but we must not allow the code we just generated to be reordered
628 by scheduling. Specifically, the update of the frame pointer must
629 happen immediately, not later. So emit an ASM_INPUT to act as blockage
631 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
634 /* __builtin_setjmp is passed a pointer to an array of five words (not
635 all will be used on all machines). It operates similarly to the C
636 library function of the same name, but is more efficient. Much of
637 the code below (and for longjmp) is copied from the handling of
640 NOTE: This is intended for use by GNAT and the exception handling
641 scheme in the compiler and will only work in the method used by
645 expand_builtin_setjmp (tree arglist, rtx target)
647 rtx buf_addr, next_lab, cont_lab;
649 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
652 if (target == 0 || !REG_P (target)
653 || REGNO (target) < FIRST_PSEUDO_REGISTER)
654 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
656 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
658 next_lab = gen_label_rtx ();
659 cont_lab = gen_label_rtx ();
661 expand_builtin_setjmp_setup (buf_addr, next_lab);
663 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
664 ensure that pending stack adjustments are flushed. */
665 emit_move_insn (target, const0_rtx);
666 emit_jump (cont_lab);
668 emit_label (next_lab);
670 expand_builtin_setjmp_receiver (next_lab);
672 /* Set TARGET to one. */
673 emit_move_insn (target, const1_rtx);
674 emit_label (cont_lab);
676 /* Tell flow about the strange goings on. Putting `next_lab' on
677 `nonlocal_goto_handler_labels' to indicates that function
678 calls may traverse the arc back to this label. */
680 current_function_has_nonlocal_label = 1;
681 nonlocal_goto_handler_labels
682 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
687 /* __builtin_longjmp is passed a pointer to an array of five words (not
688 all will be used on all machines). It operates similarly to the C
689 library function of the same name, but is more efficient. Much of
690 the code below is copied from the handling of non-local gotos.
692 NOTE: This is intended for use by GNAT and the exception handling
693 scheme in the compiler and will only work in the method used by
697 expand_builtin_longjmp (rtx buf_addr, rtx value)
699 rtx fp, lab, stack, insn, last;
700 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
702 if (setjmp_alias_set == -1)
703 setjmp_alias_set = new_alias_set ();
705 buf_addr = convert_memory_address (Pmode, buf_addr);
707 buf_addr = force_reg (Pmode, buf_addr);
709 /* We used to store value in static_chain_rtx, but that fails if pointers
710 are smaller than integers. We instead require that the user must pass
711 a second argument of 1, because that is what builtin_setjmp will
712 return. This also makes EH slightly more efficient, since we are no
713 longer copying around a value that we don't care about. */
714 gcc_assert (value == const1_rtx);
716 last = get_last_insn ();
717 #ifdef HAVE_builtin_longjmp
718 if (HAVE_builtin_longjmp)
719 emit_insn (gen_builtin_longjmp (buf_addr));
723 fp = gen_rtx_MEM (Pmode, buf_addr);
724 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
725 GET_MODE_SIZE (Pmode)));
727 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
728 2 * GET_MODE_SIZE (Pmode)));
729 set_mem_alias_set (fp, setjmp_alias_set);
730 set_mem_alias_set (lab, setjmp_alias_set);
731 set_mem_alias_set (stack, setjmp_alias_set);
733 /* Pick up FP, label, and SP from the block and jump. This code is
734 from expand_goto in stmt.c; see there for detailed comments. */
735 #if HAVE_nonlocal_goto
736 if (HAVE_nonlocal_goto)
737 /* We have to pass a value to the nonlocal_goto pattern that will
738 get copied into the static_chain pointer, but it does not matter
739 what that value is, because builtin_setjmp does not use it. */
740 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
744 lab = copy_to_reg (lab);
746 emit_insn (gen_rtx_CLOBBER (VOIDmode,
747 gen_rtx_MEM (BLKmode,
748 gen_rtx_SCRATCH (VOIDmode))));
749 emit_insn (gen_rtx_CLOBBER (VOIDmode,
750 gen_rtx_MEM (BLKmode,
751 hard_frame_pointer_rtx)));
753 emit_move_insn (hard_frame_pointer_rtx, fp);
754 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
756 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
757 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
758 emit_indirect_jump (lab);
762 /* Search backwards and mark the jump insn as a non-local goto.
763 Note that this precludes the use of __builtin_longjmp to a
764 __builtin_setjmp target in the same function. However, we've
765 already cautioned the user that these functions are for
766 internal exception handling use only. */
767 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
769 gcc_assert (insn != last);
773 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
777 else if (CALL_P (insn))
782 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
783 and the address of the save area. */
786 expand_builtin_nonlocal_goto (tree arglist)
788 tree t_label, t_save_area;
789 rtx r_label, r_save_area, r_fp, r_sp, insn;
791 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
794 t_label = TREE_VALUE (arglist);
795 arglist = TREE_CHAIN (arglist);
796 t_save_area = TREE_VALUE (arglist);
798 r_label = expand_expr (t_label, NULL_RTX, VOIDmode, 0);
799 r_label = convert_memory_address (Pmode, r_label);
800 r_save_area = expand_expr (t_save_area, NULL_RTX, VOIDmode, 0);
801 r_save_area = convert_memory_address (Pmode, r_save_area);
802 r_fp = gen_rtx_MEM (Pmode, r_save_area);
803 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
804 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
806 current_function_has_nonlocal_goto = 1;
808 #if HAVE_nonlocal_goto
809 /* ??? We no longer need to pass the static chain value, afaik. */
810 if (HAVE_nonlocal_goto)
811 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
815 r_label = copy_to_reg (r_label);
817 emit_insn (gen_rtx_CLOBBER (VOIDmode,
818 gen_rtx_MEM (BLKmode,
819 gen_rtx_SCRATCH (VOIDmode))));
821 emit_insn (gen_rtx_CLOBBER (VOIDmode,
822 gen_rtx_MEM (BLKmode,
823 hard_frame_pointer_rtx)));
825 /* Restore frame pointer for containing function.
826 This sets the actual hard register used for the frame pointer
827 to the location of the function's incoming static chain info.
828 The non-local goto handler will then adjust it to contain the
829 proper value and reload the argument pointer, if needed. */
830 emit_move_insn (hard_frame_pointer_rtx, r_fp);
831 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
833 /* USE of hard_frame_pointer_rtx added for consistency;
834 not clear if really needed. */
835 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
836 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
837 emit_indirect_jump (r_label);
840 /* Search backwards to the jump insn and mark it as a
842 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
846 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
847 const0_rtx, REG_NOTES (insn));
850 else if (CALL_P (insn))
857 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
858 (not all will be used on all machines) that was passed to __builtin_setjmp.
859 It updates the stack pointer in that block to correspond to the current
863 expand_builtin_update_setjmp_buf (rtx buf_addr)
865 enum machine_mode sa_mode = Pmode;
869 #ifdef HAVE_save_stack_nonlocal
870 if (HAVE_save_stack_nonlocal)
871 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
873 #ifdef STACK_SAVEAREA_MODE
874 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
878 = gen_rtx_MEM (sa_mode,
881 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
885 emit_insn (gen_setjmp ());
888 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
891 /* Expand a call to __builtin_prefetch. For a target that does not support
892 data prefetch, evaluate the memory address argument in case it has side
896 expand_builtin_prefetch (tree arglist)
898 tree arg0, arg1, arg2;
901 if (!validate_arglist (arglist, POINTER_TYPE, 0))
904 arg0 = TREE_VALUE (arglist);
905 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
906 zero (read) and argument 2 (locality) defaults to 3 (high degree of
908 if (TREE_CHAIN (arglist))
910 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
911 if (TREE_CHAIN (TREE_CHAIN (arglist)))
912 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
914 arg2 = build_int_cst (NULL_TREE, 3);
918 arg1 = integer_zero_node;
919 arg2 = build_int_cst (NULL_TREE, 3);
922 /* Argument 0 is an address. */
923 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
925 /* Argument 1 (read/write flag) must be a compile-time constant int. */
926 if (TREE_CODE (arg1) != INTEGER_CST)
928 error ("second argument to %<__builtin_prefetch%> must be a constant");
929 arg1 = integer_zero_node;
931 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
932 /* Argument 1 must be either zero or one. */
933 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
935 warning ("invalid second argument to %<__builtin_prefetch%>;"
940 /* Argument 2 (locality) must be a compile-time constant int. */
941 if (TREE_CODE (arg2) != INTEGER_CST)
943 error ("third argument to %<__builtin_prefetch%> must be a constant");
944 arg2 = integer_zero_node;
946 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
947 /* Argument 2 must be 0, 1, 2, or 3. */
948 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
950 warning ("invalid third argument to %<__builtin_prefetch%>; using zero");
957 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
959 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
960 || (GET_MODE (op0) != Pmode))
962 op0 = convert_memory_address (Pmode, op0);
963 op0 = force_reg (Pmode, op0);
965 emit_insn (gen_prefetch (op0, op1, op2));
969 /* Don't do anything with direct references to volatile memory, but
970 generate code to handle other side effects. */
971 if (!MEM_P (op0) && side_effects_p (op0))
975 /* Get a MEM rtx for expression EXP which is the address of an operand
976 to be used to be used in a string instruction (cmpstrsi, movmemsi, ..). */
979 get_memory_rtx (tree exp)
981 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
982 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
984 /* Get an expression we can use to find the attributes to assign to MEM.
985 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
986 we can. First remove any nops. */
987 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
988 || TREE_CODE (exp) == NON_LVALUE_EXPR)
989 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
990 exp = TREE_OPERAND (exp, 0);
992 if (TREE_CODE (exp) == ADDR_EXPR)
993 exp = TREE_OPERAND (exp, 0);
994 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
995 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
999 /* Honor attributes derived from exp, except for the alias set
1000 (as builtin stringops may alias with anything) and the size
1001 (as stringops may access multiple array elements). */
1004 set_mem_attributes (mem, exp, 0);
1005 set_mem_alias_set (mem, 0);
1006 set_mem_size (mem, NULL_RTX);
1012 /* Built-in functions to perform an untyped call and return. */
1014 /* For each register that may be used for calling a function, this
1015 gives a mode used to copy the register's value. VOIDmode indicates
1016 the register is not used for calling a function. If the machine
1017 has register windows, this gives only the outbound registers.
1018 INCOMING_REGNO gives the corresponding inbound register. */
1019 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1021 /* For each register that may be used for returning values, this gives
1022 a mode used to copy the register's value. VOIDmode indicates the
1023 register is not used for returning values. If the machine has
1024 register windows, this gives only the outbound registers.
1025 INCOMING_REGNO gives the corresponding inbound register. */
1026 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1028 /* For each register that may be used for calling a function, this
1029 gives the offset of that register into the block returned by
1030 __builtin_apply_args. 0 indicates that the register is not
1031 used for calling a function. */
1032 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1034 /* Return the size required for the block returned by __builtin_apply_args,
1035 and initialize apply_args_mode. */
1038 apply_args_size (void)
1040 static int size = -1;
1043 enum machine_mode mode;
1045 /* The values computed by this function never change. */
1048 /* The first value is the incoming arg-pointer. */
1049 size = GET_MODE_SIZE (Pmode);
1051 /* The second value is the structure value address unless this is
1052 passed as an "invisible" first argument. */
1053 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1054 size += GET_MODE_SIZE (Pmode);
1056 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1057 if (FUNCTION_ARG_REGNO_P (regno))
1059 mode = reg_raw_mode[regno];
1061 gcc_assert (mode != VOIDmode);
1063 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1064 if (size % align != 0)
1065 size = CEIL (size, align) * align;
1066 apply_args_reg_offset[regno] = size;
1067 size += GET_MODE_SIZE (mode);
1068 apply_args_mode[regno] = mode;
1072 apply_args_mode[regno] = VOIDmode;
1073 apply_args_reg_offset[regno] = 0;
1079 /* Return the size required for the block returned by __builtin_apply,
1080 and initialize apply_result_mode. */
1083 apply_result_size (void)
1085 static int size = -1;
1087 enum machine_mode mode;
1089 /* The values computed by this function never change. */
1094 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1095 if (FUNCTION_VALUE_REGNO_P (regno))
1097 mode = reg_raw_mode[regno];
1099 gcc_assert (mode != VOIDmode);
1101 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1102 if (size % align != 0)
1103 size = CEIL (size, align) * align;
1104 size += GET_MODE_SIZE (mode);
1105 apply_result_mode[regno] = mode;
1108 apply_result_mode[regno] = VOIDmode;
1110 /* Allow targets that use untyped_call and untyped_return to override
1111 the size so that machine-specific information can be stored here. */
1112 #ifdef APPLY_RESULT_SIZE
1113 size = APPLY_RESULT_SIZE;
1119 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1120 /* Create a vector describing the result block RESULT. If SAVEP is true,
1121 the result block is used to save the values; otherwise it is used to
1122 restore the values. */
1125 result_vector (int savep, rtx result)
1127 int regno, size, align, nelts;
1128 enum machine_mode mode;
1130 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1133 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1134 if ((mode = apply_result_mode[regno]) != VOIDmode)
1136 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1137 if (size % align != 0)
1138 size = CEIL (size, align) * align;
1139 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1140 mem = adjust_address (result, mode, size);
1141 savevec[nelts++] = (savep
1142 ? gen_rtx_SET (VOIDmode, mem, reg)
1143 : gen_rtx_SET (VOIDmode, reg, mem));
1144 size += GET_MODE_SIZE (mode);
1146 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1148 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1150 /* Save the state required to perform an untyped call with the same
1151 arguments as were passed to the current function. */
1154 expand_builtin_apply_args_1 (void)
1157 int size, align, regno;
1158 enum machine_mode mode;
1159 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1161 /* Create a block where the arg-pointer, structure value address,
1162 and argument registers can be saved. */
1163 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1165 /* Walk past the arg-pointer and structure value address. */
1166 size = GET_MODE_SIZE (Pmode);
1167 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1168 size += GET_MODE_SIZE (Pmode);
1170 /* Save each register used in calling a function to the block. */
1171 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1172 if ((mode = apply_args_mode[regno]) != VOIDmode)
1174 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1175 if (size % align != 0)
1176 size = CEIL (size, align) * align;
1178 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1180 emit_move_insn (adjust_address (registers, mode, size), tem);
1181 size += GET_MODE_SIZE (mode);
1184 /* Save the arg pointer to the block. */
1185 tem = copy_to_reg (virtual_incoming_args_rtx);
1186 #ifdef STACK_GROWS_DOWNWARD
1187 /* We need the pointer as the caller actually passed them to us, not
1188 as we might have pretended they were passed. Make sure it's a valid
1189 operand, as emit_move_insn isn't expected to handle a PLUS. */
1191 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1194 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1196 size = GET_MODE_SIZE (Pmode);
1198 /* Save the structure value address unless this is passed as an
1199 "invisible" first argument. */
1200 if (struct_incoming_value)
1202 emit_move_insn (adjust_address (registers, Pmode, size),
1203 copy_to_reg (struct_incoming_value));
1204 size += GET_MODE_SIZE (Pmode);
1207 /* Return the address of the block. */
1208 return copy_addr_to_reg (XEXP (registers, 0));
1211 /* __builtin_apply_args returns block of memory allocated on
1212 the stack into which is stored the arg pointer, structure
1213 value address, static chain, and all the registers that might
1214 possibly be used in performing a function call. The code is
1215 moved to the start of the function so the incoming values are
1219 expand_builtin_apply_args (void)
1221 /* Don't do __builtin_apply_args more than once in a function.
1222 Save the result of the first call and reuse it. */
1223 if (apply_args_value != 0)
1224 return apply_args_value;
1226 /* When this function is called, it means that registers must be
1227 saved on entry to this function. So we migrate the
1228 call to the first insn of this function. */
1233 temp = expand_builtin_apply_args_1 ();
1237 apply_args_value = temp;
1239 /* Put the insns after the NOTE that starts the function.
1240 If this is inside a start_sequence, make the outer-level insn
1241 chain current, so the code is placed at the start of the
1243 push_topmost_sequence ();
1244 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1245 pop_topmost_sequence ();
1250 /* Perform an untyped call and save the state required to perform an
1251 untyped return of whatever value was returned by the given function. */
1254 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1256 int size, align, regno;
1257 enum machine_mode mode;
1258 rtx incoming_args, result, reg, dest, src, call_insn;
1259 rtx old_stack_level = 0;
1260 rtx call_fusage = 0;
1261 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1263 arguments = convert_memory_address (Pmode, arguments);
1265 /* Create a block where the return registers can be saved. */
1266 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1268 /* Fetch the arg pointer from the ARGUMENTS block. */
1269 incoming_args = gen_reg_rtx (Pmode);
1270 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1271 #ifndef STACK_GROWS_DOWNWARD
1272 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1273 incoming_args, 0, OPTAB_LIB_WIDEN);
1276 /* Push a new argument block and copy the arguments. Do not allow
1277 the (potential) memcpy call below to interfere with our stack
1279 do_pending_stack_adjust ();
1282 /* Save the stack with nonlocal if available. */
1283 #ifdef HAVE_save_stack_nonlocal
1284 if (HAVE_save_stack_nonlocal)
1285 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1288 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1290 /* Allocate a block of memory onto the stack and copy the memory
1291 arguments to the outgoing arguments address. */
1292 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1293 dest = virtual_outgoing_args_rtx;
1294 #ifndef STACK_GROWS_DOWNWARD
1295 if (GET_CODE (argsize) == CONST_INT)
1296 dest = plus_constant (dest, -INTVAL (argsize));
1298 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1300 dest = gen_rtx_MEM (BLKmode, dest);
1301 set_mem_align (dest, PARM_BOUNDARY);
1302 src = gen_rtx_MEM (BLKmode, incoming_args);
1303 set_mem_align (src, PARM_BOUNDARY);
1304 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1306 /* Refer to the argument block. */
1308 arguments = gen_rtx_MEM (BLKmode, arguments);
1309 set_mem_align (arguments, PARM_BOUNDARY);
1311 /* Walk past the arg-pointer and structure value address. */
1312 size = GET_MODE_SIZE (Pmode);
1314 size += GET_MODE_SIZE (Pmode);
1316 /* Restore each of the registers previously saved. Make USE insns
1317 for each of these registers for use in making the call. */
1318 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1319 if ((mode = apply_args_mode[regno]) != VOIDmode)
1321 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1322 if (size % align != 0)
1323 size = CEIL (size, align) * align;
1324 reg = gen_rtx_REG (mode, regno);
1325 emit_move_insn (reg, adjust_address (arguments, mode, size));
1326 use_reg (&call_fusage, reg);
1327 size += GET_MODE_SIZE (mode);
1330 /* Restore the structure value address unless this is passed as an
1331 "invisible" first argument. */
1332 size = GET_MODE_SIZE (Pmode);
1335 rtx value = gen_reg_rtx (Pmode);
1336 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1337 emit_move_insn (struct_value, value);
1338 if (REG_P (struct_value))
1339 use_reg (&call_fusage, struct_value);
1340 size += GET_MODE_SIZE (Pmode);
1343 /* All arguments and registers used for the call are set up by now! */
1344 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1346 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1347 and we don't want to load it into a register as an optimization,
1348 because prepare_call_address already did it if it should be done. */
1349 if (GET_CODE (function) != SYMBOL_REF)
1350 function = memory_address (FUNCTION_MODE, function);
1352 /* Generate the actual call instruction and save the return value. */
1353 #ifdef HAVE_untyped_call
1354 if (HAVE_untyped_call)
1355 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1356 result, result_vector (1, result)));
1359 #ifdef HAVE_call_value
1360 if (HAVE_call_value)
1364 /* Locate the unique return register. It is not possible to
1365 express a call that sets more than one return register using
1366 call_value; use untyped_call for that. In fact, untyped_call
1367 only needs to save the return registers in the given block. */
1368 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1369 if ((mode = apply_result_mode[regno]) != VOIDmode)
1371 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1373 valreg = gen_rtx_REG (mode, regno);
1376 emit_call_insn (GEN_CALL_VALUE (valreg,
1377 gen_rtx_MEM (FUNCTION_MODE, function),
1378 const0_rtx, NULL_RTX, const0_rtx));
1380 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1386 /* Find the CALL insn we just emitted, and attach the register usage
1388 call_insn = last_call_insn ();
1389 add_function_usage_to (call_insn, call_fusage);
1391 /* Restore the stack. */
1392 #ifdef HAVE_save_stack_nonlocal
1393 if (HAVE_save_stack_nonlocal)
1394 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1397 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1401 /* Return the address of the result block. */
1402 result = copy_addr_to_reg (XEXP (result, 0));
1403 return convert_memory_address (ptr_mode, result);
1406 /* Perform an untyped return. */
1409 expand_builtin_return (rtx result)
1411 int size, align, regno;
1412 enum machine_mode mode;
1414 rtx call_fusage = 0;
1416 result = convert_memory_address (Pmode, result);
1418 apply_result_size ();
1419 result = gen_rtx_MEM (BLKmode, result);
1421 #ifdef HAVE_untyped_return
1422 if (HAVE_untyped_return)
1424 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1430 /* Restore the return value and note that each value is used. */
1432 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1433 if ((mode = apply_result_mode[regno]) != VOIDmode)
1435 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1436 if (size % align != 0)
1437 size = CEIL (size, align) * align;
1438 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1439 emit_move_insn (reg, adjust_address (result, mode, size));
1441 push_to_sequence (call_fusage);
1442 emit_insn (gen_rtx_USE (VOIDmode, reg));
1443 call_fusage = get_insns ();
1445 size += GET_MODE_SIZE (mode);
1448 /* Put the USE insns before the return. */
1449 emit_insn (call_fusage);
1451 /* Return whatever values was restored by jumping directly to the end
1453 expand_naked_return ();
1456 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1458 static enum type_class
1459 type_to_class (tree type)
1461 switch (TREE_CODE (type))
1463 case VOID_TYPE: return void_type_class;
1464 case INTEGER_TYPE: return integer_type_class;
1465 case CHAR_TYPE: return char_type_class;
1466 case ENUMERAL_TYPE: return enumeral_type_class;
1467 case BOOLEAN_TYPE: return boolean_type_class;
1468 case POINTER_TYPE: return pointer_type_class;
1469 case REFERENCE_TYPE: return reference_type_class;
1470 case OFFSET_TYPE: return offset_type_class;
1471 case REAL_TYPE: return real_type_class;
1472 case COMPLEX_TYPE: return complex_type_class;
1473 case FUNCTION_TYPE: return function_type_class;
1474 case METHOD_TYPE: return method_type_class;
1475 case RECORD_TYPE: return record_type_class;
1477 case QUAL_UNION_TYPE: return union_type_class;
1478 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1479 ? string_type_class : array_type_class);
1480 case FILE_TYPE: return file_type_class;
1481 case LANG_TYPE: return lang_type_class;
1482 default: return no_type_class;
1486 /* Expand a call to __builtin_classify_type with arguments found in
1490 expand_builtin_classify_type (tree arglist)
1493 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1494 return GEN_INT (no_type_class);
1497 /* This helper macro, meant to be used in mathfn_built_in below,
1498 determines which among a set of three builtin math functions is
1499 appropriate for a given type mode. The `F' and `L' cases are
1500 automatically generated from the `double' case. */
1501 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1502 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1503 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1504 fcodel = BUILT_IN_MATHFN##L ; break;
1506 /* Return mathematic function equivalent to FN but operating directly
1507 on TYPE, if available. If we can't do the conversion, return zero. */
1509 mathfn_built_in (tree type, enum built_in_function fn)
1511 enum built_in_function fcode, fcodef, fcodel;
1515 CASE_MATHFN (BUILT_IN_ACOS)
1516 CASE_MATHFN (BUILT_IN_ACOSH)
1517 CASE_MATHFN (BUILT_IN_ASIN)
1518 CASE_MATHFN (BUILT_IN_ASINH)
1519 CASE_MATHFN (BUILT_IN_ATAN)
1520 CASE_MATHFN (BUILT_IN_ATAN2)
1521 CASE_MATHFN (BUILT_IN_ATANH)
1522 CASE_MATHFN (BUILT_IN_CBRT)
1523 CASE_MATHFN (BUILT_IN_CEIL)
1524 CASE_MATHFN (BUILT_IN_COPYSIGN)
1525 CASE_MATHFN (BUILT_IN_COS)
1526 CASE_MATHFN (BUILT_IN_COSH)
1527 CASE_MATHFN (BUILT_IN_DREM)
1528 CASE_MATHFN (BUILT_IN_ERF)
1529 CASE_MATHFN (BUILT_IN_ERFC)
1530 CASE_MATHFN (BUILT_IN_EXP)
1531 CASE_MATHFN (BUILT_IN_EXP10)
1532 CASE_MATHFN (BUILT_IN_EXP2)
1533 CASE_MATHFN (BUILT_IN_EXPM1)
1534 CASE_MATHFN (BUILT_IN_FABS)
1535 CASE_MATHFN (BUILT_IN_FDIM)
1536 CASE_MATHFN (BUILT_IN_FLOOR)
1537 CASE_MATHFN (BUILT_IN_FMA)
1538 CASE_MATHFN (BUILT_IN_FMAX)
1539 CASE_MATHFN (BUILT_IN_FMIN)
1540 CASE_MATHFN (BUILT_IN_FMOD)
1541 CASE_MATHFN (BUILT_IN_FREXP)
1542 CASE_MATHFN (BUILT_IN_GAMMA)
1543 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1544 CASE_MATHFN (BUILT_IN_HYPOT)
1545 CASE_MATHFN (BUILT_IN_ILOGB)
1546 CASE_MATHFN (BUILT_IN_INF)
1547 CASE_MATHFN (BUILT_IN_J0)
1548 CASE_MATHFN (BUILT_IN_J1)
1549 CASE_MATHFN (BUILT_IN_JN)
1550 CASE_MATHFN (BUILT_IN_LDEXP)
1551 CASE_MATHFN (BUILT_IN_LGAMMA)
1552 CASE_MATHFN (BUILT_IN_LLRINT)
1553 CASE_MATHFN (BUILT_IN_LLROUND)
1554 CASE_MATHFN (BUILT_IN_LOG)
1555 CASE_MATHFN (BUILT_IN_LOG10)
1556 CASE_MATHFN (BUILT_IN_LOG1P)
1557 CASE_MATHFN (BUILT_IN_LOG2)
1558 CASE_MATHFN (BUILT_IN_LOGB)
1559 CASE_MATHFN (BUILT_IN_LRINT)
1560 CASE_MATHFN (BUILT_IN_LROUND)
1561 CASE_MATHFN (BUILT_IN_MODF)
1562 CASE_MATHFN (BUILT_IN_NAN)
1563 CASE_MATHFN (BUILT_IN_NANS)
1564 CASE_MATHFN (BUILT_IN_NEARBYINT)
1565 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1566 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1567 CASE_MATHFN (BUILT_IN_POW)
1568 CASE_MATHFN (BUILT_IN_POWI)
1569 CASE_MATHFN (BUILT_IN_POW10)
1570 CASE_MATHFN (BUILT_IN_REMAINDER)
1571 CASE_MATHFN (BUILT_IN_REMQUO)
1572 CASE_MATHFN (BUILT_IN_RINT)
1573 CASE_MATHFN (BUILT_IN_ROUND)
1574 CASE_MATHFN (BUILT_IN_SCALB)
1575 CASE_MATHFN (BUILT_IN_SCALBLN)
1576 CASE_MATHFN (BUILT_IN_SCALBN)
1577 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1578 CASE_MATHFN (BUILT_IN_SIN)
1579 CASE_MATHFN (BUILT_IN_SINCOS)
1580 CASE_MATHFN (BUILT_IN_SINH)
1581 CASE_MATHFN (BUILT_IN_SQRT)
1582 CASE_MATHFN (BUILT_IN_TAN)
1583 CASE_MATHFN (BUILT_IN_TANH)
1584 CASE_MATHFN (BUILT_IN_TGAMMA)
1585 CASE_MATHFN (BUILT_IN_TRUNC)
1586 CASE_MATHFN (BUILT_IN_Y0)
1587 CASE_MATHFN (BUILT_IN_Y1)
1588 CASE_MATHFN (BUILT_IN_YN)
1594 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1595 return implicit_built_in_decls[fcode];
1596 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1597 return implicit_built_in_decls[fcodef];
1598 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1599 return implicit_built_in_decls[fcodel];
1604 /* If errno must be maintained, expand the RTL to check if the result,
1605 TARGET, of a built-in function call, EXP, is NaN, and if so set
1609 expand_errno_check (tree exp, rtx target)
1611 rtx lab = gen_label_rtx ();
1613 /* Test the result; if it is NaN, set errno=EDOM because
1614 the argument was not in the domain. */
1615 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1619 /* If this built-in doesn't throw an exception, set errno directly. */
1620 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1622 #ifdef GEN_ERRNO_RTX
1623 rtx errno_rtx = GEN_ERRNO_RTX;
1626 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1628 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1634 /* We can't set errno=EDOM directly; let the library call do it.
1635 Pop the arguments right away in case the call gets deleted. */
1637 expand_call (exp, target, 0);
1643 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1644 Return 0 if a normal call should be emitted rather than expanding the
1645 function in-line. EXP is the expression that is a call to the builtin
1646 function; if convenient, the result should be placed in TARGET.
1647 SUBTARGET may be used as the target for computing one of EXP's operands. */
1650 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1652 optab builtin_optab;
1653 rtx op0, insns, before_call;
1654 tree fndecl = get_callee_fndecl (exp);
1655 tree arglist = TREE_OPERAND (exp, 1);
1656 enum machine_mode mode;
1657 bool errno_set = false;
1660 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1663 arg = TREE_VALUE (arglist);
1665 switch (DECL_FUNCTION_CODE (fndecl))
1668 case BUILT_IN_SQRTF:
1669 case BUILT_IN_SQRTL:
1670 errno_set = ! tree_expr_nonnegative_p (arg);
1671 builtin_optab = sqrt_optab;
1676 errno_set = true; builtin_optab = exp_optab; break;
1677 case BUILT_IN_EXP10:
1678 case BUILT_IN_EXP10F:
1679 case BUILT_IN_EXP10L:
1680 case BUILT_IN_POW10:
1681 case BUILT_IN_POW10F:
1682 case BUILT_IN_POW10L:
1683 errno_set = true; builtin_optab = exp10_optab; break;
1685 case BUILT_IN_EXP2F:
1686 case BUILT_IN_EXP2L:
1687 errno_set = true; builtin_optab = exp2_optab; break;
1688 case BUILT_IN_EXPM1:
1689 case BUILT_IN_EXPM1F:
1690 case BUILT_IN_EXPM1L:
1691 errno_set = true; builtin_optab = expm1_optab; break;
1693 case BUILT_IN_LOGBF:
1694 case BUILT_IN_LOGBL:
1695 errno_set = true; builtin_optab = logb_optab; break;
1696 case BUILT_IN_ILOGB:
1697 case BUILT_IN_ILOGBF:
1698 case BUILT_IN_ILOGBL:
1699 errno_set = true; builtin_optab = ilogb_optab; break;
1703 errno_set = true; builtin_optab = log_optab; break;
1704 case BUILT_IN_LOG10:
1705 case BUILT_IN_LOG10F:
1706 case BUILT_IN_LOG10L:
1707 errno_set = true; builtin_optab = log10_optab; break;
1709 case BUILT_IN_LOG2F:
1710 case BUILT_IN_LOG2L:
1711 errno_set = true; builtin_optab = log2_optab; break;
1712 case BUILT_IN_LOG1P:
1713 case BUILT_IN_LOG1PF:
1714 case BUILT_IN_LOG1PL:
1715 errno_set = true; builtin_optab = log1p_optab; break;
1717 case BUILT_IN_ASINF:
1718 case BUILT_IN_ASINL:
1719 builtin_optab = asin_optab; break;
1721 case BUILT_IN_ACOSF:
1722 case BUILT_IN_ACOSL:
1723 builtin_optab = acos_optab; break;
1727 builtin_optab = tan_optab; break;
1729 case BUILT_IN_ATANF:
1730 case BUILT_IN_ATANL:
1731 builtin_optab = atan_optab; break;
1732 case BUILT_IN_FLOOR:
1733 case BUILT_IN_FLOORF:
1734 case BUILT_IN_FLOORL:
1735 builtin_optab = floor_optab; break;
1737 case BUILT_IN_CEILF:
1738 case BUILT_IN_CEILL:
1739 builtin_optab = ceil_optab; break;
1740 case BUILT_IN_TRUNC:
1741 case BUILT_IN_TRUNCF:
1742 case BUILT_IN_TRUNCL:
1743 builtin_optab = btrunc_optab; break;
1744 case BUILT_IN_ROUND:
1745 case BUILT_IN_ROUNDF:
1746 case BUILT_IN_ROUNDL:
1747 builtin_optab = round_optab; break;
1748 case BUILT_IN_NEARBYINT:
1749 case BUILT_IN_NEARBYINTF:
1750 case BUILT_IN_NEARBYINTL:
1751 builtin_optab = nearbyint_optab; break;
1753 case BUILT_IN_RINTF:
1754 case BUILT_IN_RINTL:
1755 builtin_optab = rint_optab; break;
1760 /* Make a suitable register to place result in. */
1761 mode = TYPE_MODE (TREE_TYPE (exp));
1763 if (! flag_errno_math || ! HONOR_NANS (mode))
1766 /* Before working hard, check whether the instruction is available. */
1767 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1769 target = gen_reg_rtx (mode);
1771 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1772 need to expand the argument again. This way, we will not perform
1773 side-effects more the once. */
1774 narg = builtin_save_expr (arg);
1778 arglist = build_tree_list (NULL_TREE, arg);
1779 exp = build_function_call_expr (fndecl, arglist);
1782 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1786 /* Compute into TARGET.
1787 Set TARGET to wherever the result comes back. */
1788 target = expand_unop (mode, builtin_optab, op0, target, 0);
1793 expand_errno_check (exp, target);
1795 /* Output the entire sequence. */
1796 insns = get_insns ();
1802 /* If we were unable to expand via the builtin, stop the sequence
1803 (without outputting the insns) and call to the library function
1804 with the stabilized argument list. */
1808 before_call = get_last_insn ();
1810 target = expand_call (exp, target, target == const0_rtx);
1812 /* If this is a sqrt operation and we don't care about errno, try to
1813 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1814 This allows the semantics of the libcall to be visible to the RTL
1816 if (builtin_optab == sqrt_optab && !errno_set)
1818 /* Search backwards through the insns emitted by expand_call looking
1819 for the instruction with the REG_RETVAL note. */
1820 rtx last = get_last_insn ();
1821 while (last != before_call)
1823 if (find_reg_note (last, REG_RETVAL, NULL))
1825 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1826 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1827 two elements, i.e. symbol_ref(sqrt) and the operand. */
1829 && GET_CODE (note) == EXPR_LIST
1830 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1831 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1832 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1834 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1835 /* Check operand is a register with expected mode. */
1838 && GET_MODE (operand) == mode)
1840 /* Replace the REG_EQUAL note with a SQRT rtx. */
1841 rtx equiv = gen_rtx_SQRT (mode, operand);
1842 set_unique_reg_note (last, REG_EQUAL, equiv);
1847 last = PREV_INSN (last);
1854 /* Expand a call to the builtin binary math functions (pow and atan2).
1855 Return 0 if a normal call should be emitted rather than expanding the
1856 function in-line. EXP is the expression that is a call to the builtin
1857 function; if convenient, the result should be placed in TARGET.
1858 SUBTARGET may be used as the target for computing one of EXP's
1862 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1864 optab builtin_optab;
1865 rtx op0, op1, insns;
1866 int op1_type = REAL_TYPE;
1867 tree fndecl = get_callee_fndecl (exp);
1868 tree arglist = TREE_OPERAND (exp, 1);
1869 tree arg0, arg1, temp, narg;
1870 enum machine_mode mode;
1871 bool errno_set = true;
1874 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1875 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1876 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1877 op1_type = INTEGER_TYPE;
1879 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1882 arg0 = TREE_VALUE (arglist);
1883 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1885 switch (DECL_FUNCTION_CODE (fndecl))
1890 builtin_optab = pow_optab; break;
1891 case BUILT_IN_ATAN2:
1892 case BUILT_IN_ATAN2F:
1893 case BUILT_IN_ATAN2L:
1894 builtin_optab = atan2_optab; break;
1895 case BUILT_IN_LDEXP:
1896 case BUILT_IN_LDEXPF:
1897 case BUILT_IN_LDEXPL:
1898 builtin_optab = ldexp_optab; break;
1900 case BUILT_IN_FMODF:
1901 case BUILT_IN_FMODL:
1902 builtin_optab = fmod_optab; break;
1904 case BUILT_IN_DREMF:
1905 case BUILT_IN_DREML:
1906 builtin_optab = drem_optab; break;
1911 /* Make a suitable register to place result in. */
1912 mode = TYPE_MODE (TREE_TYPE (exp));
1914 /* Before working hard, check whether the instruction is available. */
1915 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1918 target = gen_reg_rtx (mode);
1920 if (! flag_errno_math || ! HONOR_NANS (mode))
1923 /* Always stabilize the argument list. */
1924 narg = builtin_save_expr (arg1);
1928 temp = build_tree_list (NULL_TREE, narg);
1932 temp = TREE_CHAIN (arglist);
1934 narg = builtin_save_expr (arg0);
1938 arglist = tree_cons (NULL_TREE, narg, temp);
1942 arglist = tree_cons (NULL_TREE, arg0, temp);
1945 exp = build_function_call_expr (fndecl, arglist);
1947 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1948 op1 = expand_expr (arg1, 0, VOIDmode, 0);
1952 /* Compute into TARGET.
1953 Set TARGET to wherever the result comes back. */
1954 target = expand_binop (mode, builtin_optab, op0, op1,
1955 target, 0, OPTAB_DIRECT);
1957 /* If we were unable to expand via the builtin, stop the sequence
1958 (without outputting the insns) and call to the library function
1959 with the stabilized argument list. */
1963 return expand_call (exp, target, target == const0_rtx);
1967 expand_errno_check (exp, target);
1969 /* Output the entire sequence. */
1970 insns = get_insns ();
1977 /* Expand a call to the builtin sin and cos math functions.
1978 Return 0 if a normal call should be emitted rather than expanding the
1979 function in-line. EXP is the expression that is a call to the builtin
1980 function; if convenient, the result should be placed in TARGET.
1981 SUBTARGET may be used as the target for computing one of EXP's
1985 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
1987 optab builtin_optab;
1988 rtx op0, insns, before_call;
1989 tree fndecl = get_callee_fndecl (exp);
1990 tree arglist = TREE_OPERAND (exp, 1);
1991 enum machine_mode mode;
1992 bool errno_set = false;
1995 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1998 arg = TREE_VALUE (arglist);
2000 switch (DECL_FUNCTION_CODE (fndecl))
2008 builtin_optab = sincos_optab; break;
2013 /* Make a suitable register to place result in. */
2014 mode = TYPE_MODE (TREE_TYPE (exp));
2016 if (! flag_errno_math || ! HONOR_NANS (mode))
2019 /* Check if sincos insn is available, otherwise fallback
2020 to sin or cos insn. */
2021 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2022 switch (DECL_FUNCTION_CODE (fndecl))
2027 builtin_optab = sin_optab; break;
2031 builtin_optab = cos_optab; break;
2037 /* Before working hard, check whether the instruction is available. */
2038 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2040 target = gen_reg_rtx (mode);
2042 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2043 need to expand the argument again. This way, we will not perform
2044 side-effects more the once. */
2045 narg = save_expr (arg);
2049 arglist = build_tree_list (NULL_TREE, arg);
2050 exp = build_function_call_expr (fndecl, arglist);
2053 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2057 /* Compute into TARGET.
2058 Set TARGET to wherever the result comes back. */
2059 if (builtin_optab == sincos_optab)
2063 switch (DECL_FUNCTION_CODE (fndecl))
2068 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2073 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2078 gcc_assert (result);
2082 target = expand_unop (mode, builtin_optab, op0, target, 0);
2088 expand_errno_check (exp, target);
2090 /* Output the entire sequence. */
2091 insns = get_insns ();
2097 /* If we were unable to expand via the builtin, stop the sequence
2098 (without outputting the insns) and call to the library function
2099 with the stabilized argument list. */
2103 before_call = get_last_insn ();
2105 target = expand_call (exp, target, target == const0_rtx);
2110 /* To evaluate powi(x,n), the floating point value x raised to the
2111 constant integer exponent n, we use a hybrid algorithm that
2112 combines the "window method" with look-up tables. For an
2113 introduction to exponentiation algorithms and "addition chains",
2114 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2115 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2116 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2117 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2119 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2120 multiplications to inline before calling the system library's pow
2121 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2122 so this default never requires calling pow, powf or powl. */
2124 #ifndef POWI_MAX_MULTS
2125 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2128 /* The size of the "optimal power tree" lookup table. All
2129 exponents less than this value are simply looked up in the
2130 powi_table below. This threshold is also used to size the
2131 cache of pseudo registers that hold intermediate results. */
2132 #define POWI_TABLE_SIZE 256
2134 /* The size, in bits of the window, used in the "window method"
2135 exponentiation algorithm. This is equivalent to a radix of
2136 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2137 #define POWI_WINDOW_SIZE 3
2139 /* The following table is an efficient representation of an
2140 "optimal power tree". For each value, i, the corresponding
2141 value, j, in the table states than an optimal evaluation
2142 sequence for calculating pow(x,i) can be found by evaluating
2143 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2144 100 integers is given in Knuth's "Seminumerical algorithms". */
2146 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2148 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2149 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2150 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2151 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2152 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2153 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2154 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2155 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2156 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2157 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2158 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2159 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2160 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2161 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2162 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2163 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2164 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2165 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2166 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2167 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2168 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2169 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2170 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2171 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2172 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2173 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2174 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2175 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2176 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2177 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2178 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2179 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2183 /* Return the number of multiplications required to calculate
2184 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2185 subroutine of powi_cost. CACHE is an array indicating
2186 which exponents have already been calculated. */
2189 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2191 /* If we've already calculated this exponent, then this evaluation
2192 doesn't require any additional multiplications. */
2197 return powi_lookup_cost (n - powi_table[n], cache)
2198 + powi_lookup_cost (powi_table[n], cache) + 1;
2201 /* Return the number of multiplications required to calculate
2202 powi(x,n) for an arbitrary x, given the exponent N. This
2203 function needs to be kept in sync with expand_powi below. */
2206 powi_cost (HOST_WIDE_INT n)
2208 bool cache[POWI_TABLE_SIZE];
2209 unsigned HOST_WIDE_INT digit;
2210 unsigned HOST_WIDE_INT val;
2216 /* Ignore the reciprocal when calculating the cost. */
2217 val = (n < 0) ? -n : n;
2219 /* Initialize the exponent cache. */
2220 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2225 while (val >= POWI_TABLE_SIZE)
2229 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2230 result += powi_lookup_cost (digit, cache)
2231 + POWI_WINDOW_SIZE + 1;
2232 val >>= POWI_WINDOW_SIZE;
2241 return result + powi_lookup_cost (val, cache);
2244 /* Recursive subroutine of expand_powi. This function takes the array,
2245 CACHE, of already calculated exponents and an exponent N and returns
2246 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2249 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2251 unsigned HOST_WIDE_INT digit;
2255 if (n < POWI_TABLE_SIZE)
2260 target = gen_reg_rtx (mode);
2263 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2264 op1 = expand_powi_1 (mode, powi_table[n], cache);
2268 target = gen_reg_rtx (mode);
2269 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2270 op0 = expand_powi_1 (mode, n - digit, cache);
2271 op1 = expand_powi_1 (mode, digit, cache);
2275 target = gen_reg_rtx (mode);
2276 op0 = expand_powi_1 (mode, n >> 1, cache);
2280 result = expand_mult (mode, op0, op1, target, 0);
2281 if (result != target)
2282 emit_move_insn (target, result);
2286 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2287 floating point operand in mode MODE, and N is the exponent. This
2288 function needs to be kept in sync with powi_cost above. */
2291 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2293 unsigned HOST_WIDE_INT val;
2294 rtx cache[POWI_TABLE_SIZE];
2298 return CONST1_RTX (mode);
2300 val = (n < 0) ? -n : n;
2302 memset (cache, 0, sizeof (cache));
2305 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2307 /* If the original exponent was negative, reciprocate the result. */
2309 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2310 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2315 /* Expand a call to the pow built-in mathematical function. Return 0 if
2316 a normal call should be emitted rather than expanding the function
2317 in-line. EXP is the expression that is a call to the builtin
2318 function; if convenient, the result should be placed in TARGET. */
2321 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2323 tree arglist = TREE_OPERAND (exp, 1);
2326 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2329 arg0 = TREE_VALUE (arglist);
2330 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2332 if (TREE_CODE (arg1) == REAL_CST
2333 && ! TREE_CONSTANT_OVERFLOW (arg1))
2335 REAL_VALUE_TYPE cint;
2339 c = TREE_REAL_CST (arg1);
2340 n = real_to_integer (&c);
2341 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2342 if (real_identical (&c, &cint))
2344 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2345 Otherwise, check the number of multiplications required.
2346 Note that pow never sets errno for an integer exponent. */
2347 if ((n >= -1 && n <= 2)
2348 || (flag_unsafe_math_optimizations
2350 && powi_cost (n) <= POWI_MAX_MULTS))
2352 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2353 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2354 op = force_reg (mode, op);
2355 return expand_powi (op, mode, n);
2360 if (! flag_unsafe_math_optimizations)
2362 return expand_builtin_mathfn_2 (exp, target, subtarget);
2365 /* Expand a call to the powi built-in mathematical function. Return 0 if
2366 a normal call should be emitted rather than expanding the function
2367 in-line. EXP is the expression that is a call to the builtin
2368 function; if convenient, the result should be placed in TARGET. */
2371 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2373 tree arglist = TREE_OPERAND (exp, 1);
2376 enum machine_mode mode;
2378 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2381 arg0 = TREE_VALUE (arglist);
2382 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2383 mode = TYPE_MODE (TREE_TYPE (exp));
2385 /* Handle constant power. */
2387 if (TREE_CODE (arg1) == INTEGER_CST
2388 && ! TREE_CONSTANT_OVERFLOW (arg1))
2390 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2392 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2393 Otherwise, check the number of multiplications required. */
2394 if ((TREE_INT_CST_HIGH (arg1) == 0
2395 || TREE_INT_CST_HIGH (arg1) == -1)
2396 && ((n >= -1 && n <= 2)
2398 && powi_cost (n) <= POWI_MAX_MULTS)))
2400 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2401 op0 = force_reg (mode, op0);
2402 return expand_powi (op0, mode, n);
2406 /* Emit a libcall to libgcc. */
2408 if (target == NULL_RTX)
2409 target = gen_reg_rtx (mode);
2411 op0 = expand_expr (arg0, subtarget, mode, 0);
2412 if (GET_MODE (op0) != mode)
2413 op0 = convert_to_mode (mode, op0, 0);
2414 op1 = expand_expr (arg1, 0, word_mode, 0);
2415 if (GET_MODE (op1) != word_mode)
2416 op1 = convert_to_mode (word_mode, op1, 0);
2418 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2419 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2420 op0, mode, op1, word_mode);
2425 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2426 if we failed the caller should emit a normal call, otherwise
2427 try to get the result in TARGET, if convenient. */
2430 expand_builtin_strlen (tree arglist, rtx target,
2431 enum machine_mode target_mode)
2433 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2438 tree len, src = TREE_VALUE (arglist);
2439 rtx result, src_reg, char_rtx, before_strlen;
2440 enum machine_mode insn_mode = target_mode, char_mode;
2441 enum insn_code icode = CODE_FOR_nothing;
2444 /* If the length can be computed at compile-time, return it. */
2445 len = c_strlen (src, 0);
2447 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2449 /* If the length can be computed at compile-time and is constant
2450 integer, but there are side-effects in src, evaluate
2451 src for side-effects, then return len.
2452 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2453 can be optimized into: i++; x = 3; */
2454 len = c_strlen (src, 1);
2455 if (len && TREE_CODE (len) == INTEGER_CST)
2457 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2458 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2461 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2463 /* If SRC is not a pointer type, don't do this operation inline. */
2467 /* Bail out if we can't compute strlen in the right mode. */
2468 while (insn_mode != VOIDmode)
2470 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2471 if (icode != CODE_FOR_nothing)
2474 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2476 if (insn_mode == VOIDmode)
2479 /* Make a place to write the result of the instruction. */
2483 && GET_MODE (result) == insn_mode
2484 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2485 result = gen_reg_rtx (insn_mode);
2487 /* Make a place to hold the source address. We will not expand
2488 the actual source until we are sure that the expansion will
2489 not fail -- there are trees that cannot be expanded twice. */
2490 src_reg = gen_reg_rtx (Pmode);
2492 /* Mark the beginning of the strlen sequence so we can emit the
2493 source operand later. */
2494 before_strlen = get_last_insn ();
2496 char_rtx = const0_rtx;
2497 char_mode = insn_data[(int) icode].operand[2].mode;
2498 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2500 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2502 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2503 char_rtx, GEN_INT (align));
2508 /* Now that we are assured of success, expand the source. */
2510 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2512 emit_move_insn (src_reg, pat);
2517 emit_insn_after (pat, before_strlen);
2519 emit_insn_before (pat, get_insns ());
2521 /* Return the value in the proper mode for this function. */
2522 if (GET_MODE (result) == target_mode)
2524 else if (target != 0)
2525 convert_move (target, result, 0);
2527 target = convert_to_mode (target_mode, result, 0);
2533 /* Expand a call to the strstr builtin. Return 0 if we failed the
2534 caller should emit a normal call, otherwise try to get the result
2535 in TARGET, if convenient (and in mode MODE if that's convenient). */
2538 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2540 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2542 tree result = fold_builtin_strstr (arglist, type);
2544 return expand_expr (result, target, mode, EXPAND_NORMAL);
2549 /* Expand a call to the strchr builtin. Return 0 if we failed the
2550 caller should emit a normal call, otherwise try to get the result
2551 in TARGET, if convenient (and in mode MODE if that's convenient). */
2554 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2556 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2558 tree result = fold_builtin_strchr (arglist, type);
2560 return expand_expr (result, target, mode, EXPAND_NORMAL);
2562 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2567 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2568 caller should emit a normal call, otherwise try to get the result
2569 in TARGET, if convenient (and in mode MODE if that's convenient). */
2572 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2574 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2576 tree result = fold_builtin_strrchr (arglist, type);
2578 return expand_expr (result, target, mode, EXPAND_NORMAL);
2583 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2584 caller should emit a normal call, otherwise try to get the result
2585 in TARGET, if convenient (and in mode MODE if that's convenient). */
2588 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2590 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2592 tree result = fold_builtin_strpbrk (arglist, type);
2594 return expand_expr (result, target, mode, EXPAND_NORMAL);
2599 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2600 bytes from constant string DATA + OFFSET and return it as target
2604 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2605 enum machine_mode mode)
2607 const char *str = (const char *) data;
2609 gcc_assert (offset >= 0
2610 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2611 <= strlen (str) + 1));
2613 return c_readstr (str + offset, mode);
2616 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2617 Return 0 if we failed, the caller should emit a normal call,
2618 otherwise try to get the result in TARGET, if convenient (and in
2619 mode MODE if that's convenient). */
2621 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2623 tree arglist = TREE_OPERAND (exp, 1);
2624 if (!validate_arglist (arglist,
2625 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2629 tree dest = TREE_VALUE (arglist);
2630 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2631 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2632 const char *src_str;
2633 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2634 unsigned int dest_align
2635 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2636 rtx dest_mem, src_mem, dest_addr, len_rtx;
2637 tree result = fold_builtin_memcpy (exp);
2640 return expand_expr (result, target, mode, EXPAND_NORMAL);
2642 /* If DEST is not a pointer type, call the normal function. */
2643 if (dest_align == 0)
2646 /* If either SRC is not a pointer type, don't do this
2647 operation in-line. */
2651 dest_mem = get_memory_rtx (dest);
2652 set_mem_align (dest_mem, dest_align);
2653 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2654 src_str = c_getstr (src);
2656 /* If SRC is a string constant and block move would be done
2657 by pieces, we can avoid loading the string from memory
2658 and only stored the computed constants. */
2660 && GET_CODE (len_rtx) == CONST_INT
2661 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2662 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2663 (void *) src_str, dest_align))
2665 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2666 builtin_memcpy_read_str,
2667 (void *) src_str, dest_align, 0);
2668 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2669 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2673 src_mem = get_memory_rtx (src);
2674 set_mem_align (src_mem, src_align);
2676 /* Copy word part most expediently. */
2677 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2682 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2683 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2689 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2690 Return 0 if we failed the caller should emit a normal call,
2691 otherwise try to get the result in TARGET, if convenient (and in
2692 mode MODE if that's convenient). If ENDP is 0 return the
2693 destination pointer, if ENDP is 1 return the end pointer ala
2694 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2698 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2701 if (!validate_arglist (arglist,
2702 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2704 /* If return value is ignored, transform mempcpy into memcpy. */
2705 else if (target == const0_rtx)
2707 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2712 return expand_expr (build_function_call_expr (fn, arglist),
2713 target, mode, EXPAND_NORMAL);
2717 tree dest = TREE_VALUE (arglist);
2718 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2719 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2720 const char *src_str;
2721 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2722 unsigned int dest_align
2723 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2724 rtx dest_mem, src_mem, len_rtx;
2725 tree result = fold_builtin_mempcpy (arglist, type, endp);
2728 return expand_expr (result, target, mode, EXPAND_NORMAL);
2730 /* If either SRC or DEST is not a pointer type, don't do this
2731 operation in-line. */
2732 if (dest_align == 0 || src_align == 0)
2735 /* If LEN is not constant, call the normal function. */
2736 if (! host_integerp (len, 1))
2739 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2740 src_str = c_getstr (src);
2742 /* If SRC is a string constant and block move would be done
2743 by pieces, we can avoid loading the string from memory
2744 and only stored the computed constants. */
2746 && GET_CODE (len_rtx) == CONST_INT
2747 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2748 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2749 (void *) src_str, dest_align))
2751 dest_mem = get_memory_rtx (dest);
2752 set_mem_align (dest_mem, dest_align);
2753 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2754 builtin_memcpy_read_str,
2755 (void *) src_str, dest_align, endp);
2756 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2757 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2761 if (GET_CODE (len_rtx) == CONST_INT
2762 && can_move_by_pieces (INTVAL (len_rtx),
2763 MIN (dest_align, src_align)))
2765 dest_mem = get_memory_rtx (dest);
2766 set_mem_align (dest_mem, dest_align);
2767 src_mem = get_memory_rtx (src);
2768 set_mem_align (src_mem, src_align);
2769 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2770 MIN (dest_align, src_align), endp);
2771 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2772 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2780 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2781 if we failed the caller should emit a normal call. */
2784 expand_builtin_memmove (tree arglist, tree type, rtx target,
2785 enum machine_mode mode)
2787 if (!validate_arglist (arglist,
2788 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2792 tree dest = TREE_VALUE (arglist);
2793 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2794 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2796 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2797 unsigned int dest_align
2798 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2799 tree result = fold_builtin_memmove (arglist, type);
2802 return expand_expr (result, target, mode, EXPAND_NORMAL);
2804 /* If DEST is not a pointer type, call the normal function. */
2805 if (dest_align == 0)
2808 /* If either SRC is not a pointer type, don't do this
2809 operation in-line. */
2813 /* If src is categorized for a readonly section we can use
2815 if (readonly_data_expr (src))
2817 tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2820 return expand_expr (build_function_call_expr (fn, arglist),
2821 target, mode, EXPAND_NORMAL);
2824 /* If length is 1 and we can expand memcpy call inline,
2825 it is ok to use memcpy as well. */
2826 if (integer_onep (len))
2828 rtx ret = expand_builtin_mempcpy (arglist, type, target, mode,
2834 /* Otherwise, call the normal function. */
2839 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2840 if we failed the caller should emit a normal call. */
2843 expand_builtin_bcopy (tree arglist, tree type)
2845 tree src, dest, size, newarglist;
2847 if (!validate_arglist (arglist,
2848 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2851 src = TREE_VALUE (arglist);
2852 dest = TREE_VALUE (TREE_CHAIN (arglist));
2853 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2855 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2856 memmove(ptr y, ptr x, size_t z). This is done this way
2857 so that if it isn't expanded inline, we fallback to
2858 calling bcopy instead of memmove. */
2860 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
2861 newarglist = tree_cons (NULL_TREE, src, newarglist);
2862 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2864 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
2868 # define HAVE_movstr 0
2869 # define CODE_FOR_movstr CODE_FOR_nothing
2872 /* Expand into a movstr instruction, if one is available. Return 0 if
2873 we failed, the caller should emit a normal call, otherwise try to
2874 get the result in TARGET, if convenient. If ENDP is 0 return the
2875 destination pointer, if ENDP is 1 return the end pointer ala
2876 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2880 expand_movstr (tree dest, tree src, rtx target, int endp)
2886 const struct insn_data * data;
2891 dest_mem = get_memory_rtx (dest);
2892 src_mem = get_memory_rtx (src);
2895 target = force_reg (Pmode, XEXP (dest_mem, 0));
2896 dest_mem = replace_equiv_address (dest_mem, target);
2897 end = gen_reg_rtx (Pmode);
2901 if (target == 0 || target == const0_rtx)
2903 end = gen_reg_rtx (Pmode);
2911 data = insn_data + CODE_FOR_movstr;
2913 if (data->operand[0].mode != VOIDmode)
2914 end = gen_lowpart (data->operand[0].mode, end);
2916 insn = data->genfun (end, dest_mem, src_mem);
2922 /* movstr is supposed to set end to the address of the NUL
2923 terminator. If the caller requested a mempcpy-like return value,
2925 if (endp == 1 && target != const0_rtx)
2927 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
2928 emit_move_insn (target, force_operand (tem, NULL_RTX));
2934 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2935 if we failed the caller should emit a normal call, otherwise try to get
2936 the result in TARGET, if convenient (and in mode MODE if that's
2940 expand_builtin_strcpy (tree exp, rtx target, enum machine_mode mode)
2942 tree arglist = TREE_OPERAND (exp, 1);
2943 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2945 tree result = fold_builtin_strcpy (exp, 0);
2947 return expand_expr (result, target, mode, EXPAND_NORMAL);
2949 return expand_movstr (TREE_VALUE (arglist),
2950 TREE_VALUE (TREE_CHAIN (arglist)),
2951 target, /*endp=*/0);
2956 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
2957 Return 0 if we failed the caller should emit a normal call,
2958 otherwise try to get the result in TARGET, if convenient (and in
2959 mode MODE if that's convenient). */
2962 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
2964 tree arglist = TREE_OPERAND (exp, 1);
2965 /* If return value is ignored, transform stpcpy into strcpy. */
2966 if (target == const0_rtx)
2968 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
2972 return expand_expr (build_function_call_expr (fn, arglist),
2973 target, mode, EXPAND_NORMAL);
2976 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2980 tree dst, src, len, lenp1;
2984 /* Ensure we get an actual string whose length can be evaluated at
2985 compile-time, not an expression containing a string. This is
2986 because the latter will potentially produce pessimized code
2987 when used to produce the return value. */
2988 src = TREE_VALUE (TREE_CHAIN (arglist));
2989 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
2990 return expand_movstr (TREE_VALUE (arglist),
2991 TREE_VALUE (TREE_CHAIN (arglist)),
2992 target, /*endp=*/2);
2994 dst = TREE_VALUE (arglist);
2995 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
2996 narglist = build_tree_list (NULL_TREE, lenp1);
2997 narglist = tree_cons (NULL_TREE, src, narglist);
2998 narglist = tree_cons (NULL_TREE, dst, narglist);
2999 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3000 target, mode, /*endp=*/2);
3005 if (TREE_CODE (len) == INTEGER_CST)
3007 rtx len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3009 if (GET_CODE (len_rtx) == CONST_INT)
3011 ret = expand_builtin_strcpy (exp, target, mode);
3017 if (mode != VOIDmode)
3018 target = gen_reg_rtx (mode);
3020 target = gen_reg_rtx (GET_MODE (ret));
3022 if (GET_MODE (target) != GET_MODE (ret))
3023 ret = gen_lowpart (GET_MODE (target), ret);
3025 ret = plus_constant (ret, INTVAL (len_rtx));
3026 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3034 return expand_movstr (TREE_VALUE (arglist),
3035 TREE_VALUE (TREE_CHAIN (arglist)),
3036 target, /*endp=*/2);
3040 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3041 bytes from constant string DATA + OFFSET and return it as target
3045 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3046 enum machine_mode mode)
3048 const char *str = (const char *) data;
3050 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3053 return c_readstr (str + offset, mode);
3056 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3057 if we failed the caller should emit a normal call. */
3060 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3062 tree arglist = TREE_OPERAND (exp, 1);
3063 if (validate_arglist (arglist,
3064 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3066 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3067 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3068 tree result = fold_builtin_strncpy (exp, slen);
3071 return expand_expr (result, target, mode, EXPAND_NORMAL);
3073 /* We must be passed a constant len and src parameter. */
3074 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3077 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3079 /* We're required to pad with trailing zeros if the requested
3080 len is greater than strlen(s2)+1. In that case try to
3081 use store_by_pieces, if it fails, punt. */
3082 if (tree_int_cst_lt (slen, len))
3084 tree dest = TREE_VALUE (arglist);
3085 unsigned int dest_align
3086 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3087 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3090 if (!p || dest_align == 0 || !host_integerp (len, 1)
3091 || !can_store_by_pieces (tree_low_cst (len, 1),
3092 builtin_strncpy_read_str,
3093 (void *) p, dest_align))
3096 dest_mem = get_memory_rtx (dest);
3097 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3098 builtin_strncpy_read_str,
3099 (void *) p, dest_align, 0);
3100 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3101 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3108 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3109 bytes from constant string DATA + OFFSET and return it as target
3113 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3114 enum machine_mode mode)
3116 const char *c = (const char *) data;
3117 char *p = alloca (GET_MODE_SIZE (mode));
3119 memset (p, *c, GET_MODE_SIZE (mode));
3121 return c_readstr (p, mode);
3124 /* Callback routine for store_by_pieces. Return the RTL of a register
3125 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3126 char value given in the RTL register data. For example, if mode is
3127 4 bytes wide, return the RTL for 0x01010101*data. */
3130 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3131 enum machine_mode mode)
3137 size = GET_MODE_SIZE (mode);
3142 memset (p, 1, size);
3143 coeff = c_readstr (p, mode);
3145 target = convert_to_mode (mode, (rtx) data, 1);
3146 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3147 return force_reg (mode, target);
3150 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3151 if we failed the caller should emit a normal call, otherwise try to get
3152 the result in TARGET, if convenient (and in mode MODE if that's
3156 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
3158 if (!validate_arglist (arglist,
3159 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3163 tree dest = TREE_VALUE (arglist);
3164 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3165 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3168 unsigned int dest_align
3169 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3170 rtx dest_mem, dest_addr, len_rtx;
3172 /* If DEST is not a pointer type, don't do this
3173 operation in-line. */
3174 if (dest_align == 0)
3177 /* If the LEN parameter is zero, return DEST. */
3178 if (integer_zerop (len))
3180 /* Evaluate and ignore VAL in case it has side-effects. */
3181 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3182 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3185 if (TREE_CODE (val) != INTEGER_CST)
3189 if (!host_integerp (len, 1))
3192 if (optimize_size && tree_low_cst (len, 1) > 1)
3195 /* Assume that we can memset by pieces if we can store the
3196 * the coefficients by pieces (in the required modes).
3197 * We can't pass builtin_memset_gen_str as that emits RTL. */
3199 if (!can_store_by_pieces (tree_low_cst (len, 1),
3200 builtin_memset_read_str,
3204 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3205 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3206 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3208 dest_mem = get_memory_rtx (dest);
3209 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3210 builtin_memset_gen_str,
3211 val_rtx, dest_align, 0);
3212 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3213 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3217 if (target_char_cast (val, &c))
3222 if (!host_integerp (len, 1))
3224 if (!can_store_by_pieces (tree_low_cst (len, 1),
3225 builtin_memset_read_str, &c,
3229 dest_mem = get_memory_rtx (dest);
3230 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3231 builtin_memset_read_str,
3233 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3234 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3238 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3240 dest_mem = get_memory_rtx (dest);
3241 set_mem_align (dest_mem, dest_align);
3242 dest_addr = clear_storage (dest_mem, len_rtx);
3246 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3247 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3254 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3255 if we failed the caller should emit a normal call. */
3258 expand_builtin_bzero (tree arglist)
3260 tree dest, size, newarglist;
3262 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3265 dest = TREE_VALUE (arglist);
3266 size = TREE_VALUE (TREE_CHAIN (arglist));
3268 /* New argument list transforming bzero(ptr x, int y) to
3269 memset(ptr x, int 0, size_t y). This is done this way
3270 so that if it isn't expanded inline, we fallback to
3271 calling bzero instead of memset. */
3273 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3274 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3275 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3277 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
3280 /* Expand expression EXP, which is a call to the memcmp built-in function.
3281 ARGLIST is the argument list for this call. Return 0 if we failed and the
3282 caller should emit a normal call, otherwise try to get the result in
3283 TARGET, if convenient (and in mode MODE, if that's convenient). */
3286 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3287 enum machine_mode mode)
3289 if (!validate_arglist (arglist,
3290 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3294 tree result = fold_builtin_memcmp (arglist);
3296 return expand_expr (result, target, mode, EXPAND_NORMAL);
3299 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3301 tree arg1 = TREE_VALUE (arglist);
3302 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3303 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3304 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3309 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3311 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3312 enum machine_mode insn_mode;
3314 #ifdef HAVE_cmpmemsi
3316 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3319 #ifdef HAVE_cmpstrsi
3321 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3326 /* If we don't have POINTER_TYPE, call the function. */
3327 if (arg1_align == 0 || arg2_align == 0)
3330 /* Make a place to write the result of the instruction. */
3333 && REG_P (result) && GET_MODE (result) == insn_mode
3334 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3335 result = gen_reg_rtx (insn_mode);
3337 arg1_rtx = get_memory_rtx (arg1);
3338 arg2_rtx = get_memory_rtx (arg2);
3339 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3341 /* Set MEM_SIZE as appropriate. */
3342 if (GET_CODE (arg3_rtx) == CONST_INT)
3344 set_mem_size (arg1_rtx, arg3_rtx);
3345 set_mem_size (arg2_rtx, arg3_rtx);
3348 #ifdef HAVE_cmpmemsi
3350 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3351 GEN_INT (MIN (arg1_align, arg2_align)));
3354 #ifdef HAVE_cmpstrsi
3356 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3357 GEN_INT (MIN (arg1_align, arg2_align)));
3365 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3366 TYPE_MODE (integer_type_node), 3,
3367 XEXP (arg1_rtx, 0), Pmode,
3368 XEXP (arg2_rtx, 0), Pmode,
3369 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3370 TYPE_UNSIGNED (sizetype)),
3371 TYPE_MODE (sizetype));
3373 /* Return the value in the proper mode for this function. */
3374 mode = TYPE_MODE (TREE_TYPE (exp));
3375 if (GET_MODE (result) == mode)
3377 else if (target != 0)
3379 convert_move (target, result, 0);
3383 return convert_to_mode (mode, result, 0);
3390 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3391 if we failed the caller should emit a normal call, otherwise try to get
3392 the result in TARGET, if convenient. */
3395 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3397 tree arglist = TREE_OPERAND (exp, 1);
3399 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3403 tree result = fold_builtin_strcmp (arglist);
3405 return expand_expr (result, target, mode, EXPAND_NORMAL);
3408 #ifdef HAVE_cmpstrsi
3411 tree arg1 = TREE_VALUE (arglist);
3412 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3413 tree len, len1, len2;
3414 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3419 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3421 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3422 enum machine_mode insn_mode
3423 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3425 len1 = c_strlen (arg1, 1);
3426 len2 = c_strlen (arg2, 1);
3429 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3431 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3433 /* If we don't have a constant length for the first, use the length
3434 of the second, if we know it. We don't require a constant for
3435 this case; some cost analysis could be done if both are available
3436 but neither is constant. For now, assume they're equally cheap,
3437 unless one has side effects. If both strings have constant lengths,
3444 else if (TREE_SIDE_EFFECTS (len1))
3446 else if (TREE_SIDE_EFFECTS (len2))
3448 else if (TREE_CODE (len1) != INTEGER_CST)
3450 else if (TREE_CODE (len2) != INTEGER_CST)
3452 else if (tree_int_cst_lt (len1, len2))
3457 /* If both arguments have side effects, we cannot optimize. */
3458 if (!len || TREE_SIDE_EFFECTS (len))
3461 /* If we don't have POINTER_TYPE, call the function. */
3462 if (arg1_align == 0 || arg2_align == 0)
3465 /* Make a place to write the result of the instruction. */
3468 && REG_P (result) && GET_MODE (result) == insn_mode
3469 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3470 result = gen_reg_rtx (insn_mode);
3472 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3473 arg1 = builtin_save_expr (arg1);
3474 arg2 = builtin_save_expr (arg2);
3476 arg1_rtx = get_memory_rtx (arg1);
3477 arg2_rtx = get_memory_rtx (arg2);
3478 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3479 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3480 GEN_INT (MIN (arg1_align, arg2_align)));
3485 /* Return the value in the proper mode for this function. */
3486 mode = TYPE_MODE (TREE_TYPE (exp));
3487 if (GET_MODE (result) == mode)
3490 return convert_to_mode (mode, result, 0);
3491 convert_move (target, result, 0);
3495 /* Expand the library call ourselves using a stabilized argument
3496 list to avoid re-evaluating the function's arguments twice. */
3497 arglist = build_tree_list (NULL_TREE, arg2);
3498 arglist = tree_cons (NULL_TREE, arg1, arglist);
3499 fndecl = get_callee_fndecl (exp);
3500 exp = build_function_call_expr (fndecl, arglist);
3501 return expand_call (exp, target, target == const0_rtx);
3507 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3508 if we failed the caller should emit a normal call, otherwise try to get
3509 the result in TARGET, if convenient. */
3512 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3514 tree arglist = TREE_OPERAND (exp, 1);
3516 if (!validate_arglist (arglist,
3517 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3521 tree result = fold_builtin_strncmp (arglist);
3523 return expand_expr (result, target, mode, EXPAND_NORMAL);
3526 /* If c_strlen can determine an expression for one of the string
3527 lengths, and it doesn't have side effects, then emit cmpstrsi
3528 using length MIN(strlen(string)+1, arg3). */
3529 #ifdef HAVE_cmpstrsi
3532 tree arg1 = TREE_VALUE (arglist);
3533 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3534 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3535 tree len, len1, len2;
3536 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3541 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3543 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3544 enum machine_mode insn_mode
3545 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3547 len1 = c_strlen (arg1, 1);
3548 len2 = c_strlen (arg2, 1);
3551 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3553 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3555 /* If we don't have a constant length for the first, use the length
3556 of the second, if we know it. We don't require a constant for
3557 this case; some cost analysis could be done if both are available
3558 but neither is constant. For now, assume they're equally cheap,
3559 unless one has side effects. If both strings have constant lengths,
3566 else if (TREE_SIDE_EFFECTS (len1))
3568 else if (TREE_SIDE_EFFECTS (len2))
3570 else if (TREE_CODE (len1) != INTEGER_CST)
3572 else if (TREE_CODE (len2) != INTEGER_CST)
3574 else if (tree_int_cst_lt (len1, len2))
3579 /* If both arguments have side effects, we cannot optimize. */
3580 if (!len || TREE_SIDE_EFFECTS (len))
3583 /* The actual new length parameter is MIN(len,arg3). */
3584 len = fold (build2 (MIN_EXPR, TREE_TYPE (len), len,
3585 fold_convert (TREE_TYPE (len), arg3)));
3587 /* If we don't have POINTER_TYPE, call the function. */
3588 if (arg1_align == 0 || arg2_align == 0)
3591 /* Make a place to write the result of the instruction. */
3594 && REG_P (result) && GET_MODE (result) == insn_mode
3595 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3596 result = gen_reg_rtx (insn_mode);
3598 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3599 arg1 = builtin_save_expr (arg1);
3600 arg2 = builtin_save_expr (arg2);
3601 len = builtin_save_expr (len);
3603 arg1_rtx = get_memory_rtx (arg1);
3604 arg2_rtx = get_memory_rtx (arg2);
3605 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3606 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3607 GEN_INT (MIN (arg1_align, arg2_align)));
3612 /* Return the value in the proper mode for this function. */
3613 mode = TYPE_MODE (TREE_TYPE (exp));
3614 if (GET_MODE (result) == mode)
3617 return convert_to_mode (mode, result, 0);
3618 convert_move (target, result, 0);
3622 /* Expand the library call ourselves using a stabilized argument
3623 list to avoid re-evaluating the function's arguments twice. */
3624 arglist = build_tree_list (NULL_TREE, len);
3625 arglist = tree_cons (NULL_TREE, arg2, arglist);
3626 arglist = tree_cons (NULL_TREE, arg1, arglist);
3627 fndecl = get_callee_fndecl (exp);
3628 exp = build_function_call_expr (fndecl, arglist);
3629 return expand_call (exp, target, target == const0_rtx);
3635 /* Expand expression EXP, which is a call to the strcat builtin.
3636 Return 0 if we failed the caller should emit a normal call,
3637 otherwise try to get the result in TARGET, if convenient. */
3640 expand_builtin_strcat (tree arglist, tree type, rtx target, enum machine_mode mode)
3642 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3646 tree dst = TREE_VALUE (arglist),
3647 src = TREE_VALUE (TREE_CHAIN (arglist));
3648 const char *p = c_getstr (src);
3652 /* If the string length is zero, return the dst parameter. */
3654 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3655 else if (!optimize_size)
3657 /* Otherwise if !optimize_size, see if we can store by
3658 pieces into (dst + strlen(dst)). */
3659 tree newdst, arglist,
3660 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3662 /* This is the length argument. */
3663 arglist = build_tree_list (NULL_TREE,
3664 fold (size_binop (PLUS_EXPR,
3667 /* Prepend src argument. */
3668 arglist = tree_cons (NULL_TREE, src, arglist);
3670 /* We're going to use dst more than once. */
3671 dst = builtin_save_expr (dst);
3673 /* Create strlen (dst). */
3675 fold (build_function_call_expr (strlen_fn,
3676 build_tree_list (NULL_TREE,
3678 /* Create (dst + strlen (dst)). */
3679 newdst = fold (build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
3681 /* Prepend the new dst argument. */
3682 arglist = tree_cons (NULL_TREE, newdst, arglist);
3684 /* We don't want to get turned into a memcpy if the
3685 target is const0_rtx, i.e. when the return value
3686 isn't used. That would produce pessimized code so
3687 pass in a target of zero, it should never actually be
3688 used. If this was successful return the original
3689 dst, not the result of mempcpy. */
3690 if (expand_builtin_mempcpy (arglist, type, /*target=*/0, mode, /*endp=*/0))
3691 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3701 /* Expand expression EXP, which is a call to the strncat builtin.
3702 Return 0 if we failed the caller should emit a normal call,
3703 otherwise try to get the result in TARGET, if convenient. */
3706 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3708 if (validate_arglist (arglist,
3709 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3711 tree result = fold_builtin_strncat (arglist);
3713 return expand_expr (result, target, mode, EXPAND_NORMAL);
3718 /* Expand expression EXP, which is a call to the strspn builtin.
3719 Return 0 if we failed the caller should emit a normal call,
3720 otherwise try to get the result in TARGET, if convenient. */
3723 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3725 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3727 tree result = fold_builtin_strspn (arglist);
3729 return expand_expr (result, target, mode, EXPAND_NORMAL);
3734 /* Expand expression EXP, which is a call to the strcspn builtin.
3735 Return 0 if we failed the caller should emit a normal call,
3736 otherwise try to get the result in TARGET, if convenient. */
3739 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3741 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3743 tree result = fold_builtin_strcspn (arglist);
3745 return expand_expr (result, target, mode, EXPAND_NORMAL);
3750 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3751 if that's convenient. */
3754 expand_builtin_saveregs (void)
3758 /* Don't do __builtin_saveregs more than once in a function.
3759 Save the result of the first call and reuse it. */
3760 if (saveregs_value != 0)
3761 return saveregs_value;
3763 /* When this function is called, it means that registers must be
3764 saved on entry to this function. So we migrate the call to the
3765 first insn of this function. */
3769 /* Do whatever the machine needs done in this case. */
3770 val = targetm.calls.expand_builtin_saveregs ();
3775 saveregs_value = val;
3777 /* Put the insns after the NOTE that starts the function. If this
3778 is inside a start_sequence, make the outer-level insn chain current, so
3779 the code is placed at the start of the function. */
3780 push_topmost_sequence ();
3781 emit_insn_after (seq, entry_of_function ());
3782 pop_topmost_sequence ();
3787 /* __builtin_args_info (N) returns word N of the arg space info
3788 for the current function. The number and meanings of words
3789 is controlled by the definition of CUMULATIVE_ARGS. */
3792 expand_builtin_args_info (tree arglist)
3794 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
3795 int *word_ptr = (int *) ¤t_function_args_info;
3797 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
3801 if (!host_integerp (TREE_VALUE (arglist), 0))
3802 error ("argument of %<__builtin_args_info%> must be constant");
3805 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
3807 if (wordnum < 0 || wordnum >= nwords)
3808 error ("argument of %<__builtin_args_info%> out of range");
3810 return GEN_INT (word_ptr[wordnum]);
3814 error ("missing argument in %<__builtin_args_info%>");
3819 /* Expand a call to __builtin_next_arg. */
3822 expand_builtin_next_arg (void)
3824 /* Checking arguments is already done in fold_builtin_next_arg
3825 that must be called before this function. */
3826 return expand_binop (Pmode, add_optab,
3827 current_function_internal_arg_pointer,
3828 current_function_arg_offset_rtx,
3829 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3832 /* Make it easier for the backends by protecting the valist argument
3833 from multiple evaluations. */
3836 stabilize_va_list (tree valist, int needs_lvalue)
3838 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3840 if (TREE_SIDE_EFFECTS (valist))
3841 valist = save_expr (valist);
3843 /* For this case, the backends will be expecting a pointer to
3844 TREE_TYPE (va_list_type_node), but it's possible we've
3845 actually been given an array (an actual va_list_type_node).
3847 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
3849 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3850 valist = build_fold_addr_expr_with_type (valist, p1);
3859 if (! TREE_SIDE_EFFECTS (valist))
3862 pt = build_pointer_type (va_list_type_node);
3863 valist = fold (build1 (ADDR_EXPR, pt, valist));
3864 TREE_SIDE_EFFECTS (valist) = 1;
3867 if (TREE_SIDE_EFFECTS (valist))
3868 valist = save_expr (valist);
3869 valist = build_fold_indirect_ref (valist);
3875 /* The "standard" definition of va_list is void*. */
3878 std_build_builtin_va_list (void)
3880 return ptr_type_node;
3883 /* The "standard" implementation of va_start: just assign `nextarg' to
3887 std_expand_builtin_va_start (tree valist, rtx nextarg)
3891 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
3892 make_tree (ptr_type_node, nextarg));
3893 TREE_SIDE_EFFECTS (t) = 1;
3895 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3898 /* Expand ARGLIST, from a call to __builtin_va_start. */
3901 expand_builtin_va_start (tree arglist)
3906 chain = TREE_CHAIN (arglist);
3910 error ("too few arguments to function %<va_start%>");
3914 if (fold_builtin_next_arg (chain))
3917 nextarg = expand_builtin_next_arg ();
3918 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
3920 #ifdef EXPAND_BUILTIN_VA_START
3921 EXPAND_BUILTIN_VA_START (valist, nextarg);
3923 std_expand_builtin_va_start (valist, nextarg);
3929 /* The "standard" implementation of va_arg: read the value from the
3930 current (padded) address and increment by the (padded) size. */
3933 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
3935 tree addr, t, type_size, rounded_size, valist_tmp;
3936 unsigned HOST_WIDE_INT align, boundary;
3939 #ifdef ARGS_GROW_DOWNWARD
3940 /* All of the alignment and movement below is for args-grow-up machines.
3941 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
3942 implement their own specialized gimplify_va_arg_expr routines. */
3946 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
3948 type = build_pointer_type (type);
3950 align = PARM_BOUNDARY / BITS_PER_UNIT;
3951 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
3953 /* Hoist the valist value into a temporary for the moment. */
3954 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
3956 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
3957 requires greater alignment, we must perform dynamic alignment. */
3958 if (boundary > align)
3960 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
3961 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
3962 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
3963 gimplify_and_add (t, pre_p);
3965 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
3966 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
3967 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
3968 gimplify_and_add (t, pre_p);
3973 /* If the actual alignment is less than the alignment of the type,
3974 adjust the type accordingly so that we don't assume strict alignment
3975 when deferencing the pointer. */
3976 boundary *= BITS_PER_UNIT;
3977 if (boundary < TYPE_ALIGN (type))
3979 type = build_variant_type_copy (type);
3980 TYPE_ALIGN (type) = boundary;
3983 /* Compute the rounded size of the type. */
3984 type_size = size_in_bytes (type);
3985 rounded_size = round_up (type_size, align);
3987 /* Reduce rounded_size so it's sharable with the postqueue. */
3988 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
3992 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
3994 /* Small args are padded downward. */
3995 t = fold (build2 (GT_EXPR, sizetype, rounded_size, size_int (align)));
3996 t = fold (build3 (COND_EXPR, sizetype, t, size_zero_node,
3997 size_binop (MINUS_EXPR, rounded_size, type_size)));
3998 t = fold_convert (TREE_TYPE (addr), t);
3999 addr = fold (build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t));
4002 /* Compute new value for AP. */
4003 t = fold_convert (TREE_TYPE (valist), rounded_size);
4004 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4005 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4006 gimplify_and_add (t, pre_p);
4008 addr = fold_convert (build_pointer_type (type), addr);
4011 addr = build_va_arg_indirect_ref (addr);
4013 return build_va_arg_indirect_ref (addr);
4016 /* Build an indirect-ref expression over the given TREE, which represents a
4017 piece of a va_arg() expansion. */
4019 build_va_arg_indirect_ref (tree addr)
4021 addr = build_fold_indirect_ref (addr);
4023 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4029 /* Return a dummy expression of type TYPE in order to keep going after an
4033 dummy_object (tree type)
4035 tree t = convert (build_pointer_type (type), null_pointer_node);
4036 return build1 (INDIRECT_REF, type, t);
4039 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4040 builtin function, but a very special sort of operator. */
4042 enum gimplify_status
4043 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4045 tree promoted_type, want_va_type, have_va_type;
4046 tree valist = TREE_OPERAND (*expr_p, 0);
4047 tree type = TREE_TYPE (*expr_p);
4050 /* Verify that valist is of the proper type. */
4051 want_va_type = va_list_type_node;
4052 have_va_type = TREE_TYPE (valist);
4054 if (have_va_type == error_mark_node)
4057 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4059 /* If va_list is an array type, the argument may have decayed
4060 to a pointer type, e.g. by being passed to another function.
4061 In that case, unwrap both types so that we can compare the
4062 underlying records. */
4063 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4064 || POINTER_TYPE_P (have_va_type))
4066 want_va_type = TREE_TYPE (want_va_type);
4067 have_va_type = TREE_TYPE (have_va_type);
4071 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4073 error ("first argument to %<va_arg%> not of type %<va_list%>");
4077 /* Generate a diagnostic for requesting data of a type that cannot
4078 be passed through `...' due to type promotion at the call site. */
4079 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4082 static bool gave_help;
4084 /* Unfortunately, this is merely undefined, rather than a constraint
4085 violation, so we cannot make this an error. If this call is never
4086 executed, the program is still strictly conforming. */
4087 warning ("%qT is promoted to %qT when passed through %<...%>",
4088 type, promoted_type);
4092 warning ("(so you should pass %qT not %qT to %<va_arg%>)",
4093 promoted_type, type);
4096 /* We can, however, treat "undefined" any way we please.
4097 Call abort to encourage the user to fix the program. */
4098 inform ("if this code is reached, the program will abort");
4099 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4101 append_to_statement_list (t, pre_p);
4103 /* This is dead code, but go ahead and finish so that the
4104 mode of the result comes out right. */
4105 *expr_p = dummy_object (type);
4110 /* Make it easier for the backends by protecting the valist argument
4111 from multiple evaluations. */
4112 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4114 /* For this case, the backends will be expecting a pointer to
4115 TREE_TYPE (va_list_type_node), but it's possible we've
4116 actually been given an array (an actual va_list_type_node).
4118 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4120 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4121 valist = build_fold_addr_expr_with_type (valist, p1);
4123 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4126 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4128 if (!targetm.gimplify_va_arg_expr)
4129 /* Once most targets are converted this should abort. */
4132 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4137 /* Expand ARGLIST, from a call to __builtin_va_end. */
4140 expand_builtin_va_end (tree arglist)
4142 tree valist = TREE_VALUE (arglist);
4144 /* Evaluate for side effects, if needed. I hate macros that don't
4146 if (TREE_SIDE_EFFECTS (valist))
4147 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4152 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4153 builtin rather than just as an assignment in stdarg.h because of the
4154 nastiness of array-type va_list types. */
4157 expand_builtin_va_copy (tree arglist)
4161 dst = TREE_VALUE (arglist);
4162 src = TREE_VALUE (TREE_CHAIN (arglist));
4164 dst = stabilize_va_list (dst, 1);
4165 src = stabilize_va_list (src, 0);
4167 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4169 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4170 TREE_SIDE_EFFECTS (t) = 1;
4171 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4175 rtx dstb, srcb, size;
4177 /* Evaluate to pointers. */
4178 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4179 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4180 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4181 VOIDmode, EXPAND_NORMAL);
4183 dstb = convert_memory_address (Pmode, dstb);
4184 srcb = convert_memory_address (Pmode, srcb);
4186 /* "Dereference" to BLKmode memories. */
4187 dstb = gen_rtx_MEM (BLKmode, dstb);
4188 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4189 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4190 srcb = gen_rtx_MEM (BLKmode, srcb);
4191 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4192 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4195 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4201 /* Expand a call to one of the builtin functions __builtin_frame_address or
4202 __builtin_return_address. */
4205 expand_builtin_frame_address (tree fndecl, tree arglist)
4207 /* The argument must be a nonnegative integer constant.
4208 It counts the number of frames to scan up the stack.
4209 The value is the return address saved in that frame. */
4211 /* Warning about missing arg was already issued. */
4213 else if (! host_integerp (TREE_VALUE (arglist), 1))
4215 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4216 error ("invalid argument to %<__builtin_frame_address%>");
4218 error ("invalid argument to %<__builtin_return_address%>");
4224 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4225 tree_low_cst (TREE_VALUE (arglist), 1));
4227 /* Some ports cannot access arbitrary stack frames. */
4230 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4231 warning ("unsupported argument to %<__builtin_frame_address%>");
4233 warning ("unsupported argument to %<__builtin_return_address%>");
4237 /* For __builtin_frame_address, return what we've got. */
4238 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4242 && ! CONSTANT_P (tem))
4243 tem = copy_to_mode_reg (Pmode, tem);
4248 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4249 we failed and the caller should emit a normal call, otherwise try to get
4250 the result in TARGET, if convenient. */
4253 expand_builtin_alloca (tree arglist, rtx target)
4258 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4259 should always expand to function calls. These can be intercepted
4264 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4267 /* Compute the argument. */
4268 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4270 /* Allocate the desired space. */
4271 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4272 result = convert_memory_address (ptr_mode, result);
4277 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4278 Return 0 if a normal call should be emitted rather than expanding the
4279 function in-line. If convenient, the result should be placed in TARGET.
4280 SUBTARGET may be used as the target for computing one of EXP's operands. */
4283 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4284 rtx subtarget, optab op_optab)
4287 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4290 /* Compute the argument. */
4291 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4292 /* Compute op, into TARGET if possible.
4293 Set TARGET to wherever the result comes back. */
4294 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4295 op_optab, op0, target, 1);
4296 gcc_assert (target);
4298 return convert_to_mode (target_mode, target, 0);
4301 /* If the string passed to fputs is a constant and is one character
4302 long, we attempt to transform this call into __builtin_fputc(). */
4305 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4307 /* Verify the arguments in the original call. */
4308 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4310 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4311 unlocked, NULL_TREE);
4313 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4318 /* Expand a call to __builtin_expect. We return our argument and emit a
4319 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4320 a non-jump context. */
4323 expand_builtin_expect (tree arglist, rtx target)
4328 if (arglist == NULL_TREE
4329 || TREE_CHAIN (arglist) == NULL_TREE)
4331 exp = TREE_VALUE (arglist);
4332 c = TREE_VALUE (TREE_CHAIN (arglist));
4334 if (TREE_CODE (c) != INTEGER_CST)
4336 error ("second argument to %<__builtin_expect%> must be a constant");
4337 c = integer_zero_node;
4340 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4342 /* Don't bother with expected value notes for integral constants. */
4343 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4345 /* We do need to force this into a register so that we can be
4346 moderately sure to be able to correctly interpret the branch
4348 target = force_reg (GET_MODE (target), target);
4350 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4352 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4353 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4359 /* Like expand_builtin_expect, except do this in a jump context. This is
4360 called from do_jump if the conditional is a __builtin_expect. Return either
4361 a list of insns to emit the jump or NULL if we cannot optimize
4362 __builtin_expect. We need to optimize this at jump time so that machines
4363 like the PowerPC don't turn the test into a SCC operation, and then jump
4364 based on the test being 0/1. */
4367 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4369 tree arglist = TREE_OPERAND (exp, 1);
4370 tree arg0 = TREE_VALUE (arglist);
4371 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4374 /* Only handle __builtin_expect (test, 0) and
4375 __builtin_expect (test, 1). */
4376 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4377 && (integer_zerop (arg1) || integer_onep (arg1)))
4379 rtx insn, drop_through_label, temp;
4381 /* Expand the jump insns. */
4383 do_jump (arg0, if_false_label, if_true_label);
4386 drop_through_label = get_last_insn ();
4387 if (drop_through_label && NOTE_P (drop_through_label))
4388 drop_through_label = prev_nonnote_insn (drop_through_label);
4389 if (drop_through_label && !LABEL_P (drop_through_label))
4390 drop_through_label = NULL_RTX;
4393 if (! if_true_label)
4394 if_true_label = drop_through_label;
4395 if (! if_false_label)
4396 if_false_label = drop_through_label;
4398 /* Go through and add the expect's to each of the conditional jumps. */
4400 while (insn != NULL_RTX)
4402 rtx next = NEXT_INSN (insn);
4404 if (JUMP_P (insn) && any_condjump_p (insn))
4406 rtx ifelse = SET_SRC (pc_set (insn));
4407 rtx then_dest = XEXP (ifelse, 1);
4408 rtx else_dest = XEXP (ifelse, 2);
4411 /* First check if we recognize any of the labels. */
4412 if (GET_CODE (then_dest) == LABEL_REF
4413 && XEXP (then_dest, 0) == if_true_label)
4415 else if (GET_CODE (then_dest) == LABEL_REF
4416 && XEXP (then_dest, 0) == if_false_label)
4418 else if (GET_CODE (else_dest) == LABEL_REF
4419 && XEXP (else_dest, 0) == if_false_label)
4421 else if (GET_CODE (else_dest) == LABEL_REF
4422 && XEXP (else_dest, 0) == if_true_label)
4424 /* Otherwise check where we drop through. */
4425 else if (else_dest == pc_rtx)
4427 if (next && NOTE_P (next))
4428 next = next_nonnote_insn (next);
4430 if (next && JUMP_P (next)
4431 && any_uncondjump_p (next))
4432 temp = XEXP (SET_SRC (pc_set (next)), 0);
4436 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4437 else that can't possibly match either target label. */
4438 if (temp == if_false_label)
4440 else if (temp == if_true_label)
4443 else if (then_dest == pc_rtx)
4445 if (next && NOTE_P (next))
4446 next = next_nonnote_insn (next);
4448 if (next && JUMP_P (next)
4449 && any_uncondjump_p (next))
4450 temp = XEXP (SET_SRC (pc_set (next)), 0);
4454 if (temp == if_false_label)
4456 else if (temp == if_true_label)
4462 /* If the test is expected to fail, reverse the
4464 if (integer_zerop (arg1))
4466 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4478 expand_builtin_trap (void)
4482 emit_insn (gen_trap ());
4485 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4489 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4490 Return 0 if a normal call should be emitted rather than expanding
4491 the function inline. If convenient, the result should be placed
4492 in TARGET. SUBTARGET may be used as the target for computing
4496 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4498 enum machine_mode mode;
4502 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4505 arg = TREE_VALUE (arglist);
4506 mode = TYPE_MODE (TREE_TYPE (arg));
4507 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4508 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4511 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4512 Return NULL is a normal call should be emitted rather than expanding the
4513 function inline. If convenient, the result should be placed in TARGET.
4514 SUBTARGET may be used as the target for computing the operand. */
4517 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4522 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4525 arg = TREE_VALUE (arglist);
4526 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4528 arg = TREE_VALUE (TREE_CHAIN (arglist));
4529 op1 = expand_expr (arg, NULL, VOIDmode, 0);
4531 return expand_copysign (op0, op1, target);
4534 /* Create a new constant string literal and return a char* pointer to it.
4535 The STRING_CST value is the LEN characters at STR. */
4537 build_string_literal (int len, const char *str)
4539 tree t, elem, index, type;
4541 t = build_string (len, str);
4542 elem = build_type_variant (char_type_node, 1, 0);
4543 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4544 type = build_array_type (elem, index);
4545 TREE_TYPE (t) = type;
4546 TREE_CONSTANT (t) = 1;
4547 TREE_INVARIANT (t) = 1;
4548 TREE_READONLY (t) = 1;
4549 TREE_STATIC (t) = 1;
4551 type = build_pointer_type (type);
4552 t = build1 (ADDR_EXPR, type, t);
4554 type = build_pointer_type (elem);
4555 t = build1 (NOP_EXPR, type, t);
4559 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4560 Return 0 if a normal call should be emitted rather than transforming
4561 the function inline. If convenient, the result should be placed in
4562 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4565 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4568 tree fn_putchar = unlocked
4569 ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4570 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4571 tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4572 : implicit_built_in_decls[BUILT_IN_PUTS];
4573 const char *fmt_str;
4576 /* If the return value is used, don't do the transformation. */
4577 if (target != const0_rtx)
4580 /* Verify the required arguments in the original call. */
4583 fmt = TREE_VALUE (arglist);
4584 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4586 arglist = TREE_CHAIN (arglist);
4588 /* Check whether the format is a literal string constant. */
4589 fmt_str = c_getstr (fmt);
4590 if (fmt_str == NULL)
4593 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4594 if (strcmp (fmt_str, "%s\n") == 0)
4597 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4598 || TREE_CHAIN (arglist))
4602 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4603 else if (strcmp (fmt_str, "%c") == 0)
4606 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4607 || TREE_CHAIN (arglist))
4613 /* We can't handle anything else with % args or %% ... yet. */
4614 if (strchr (fmt_str, '%'))
4620 /* If the format specifier was "", printf does nothing. */
4621 if (fmt_str[0] == '\0')
4623 /* If the format specifier has length of 1, call putchar. */
4624 if (fmt_str[1] == '\0')
4626 /* Given printf("c"), (where c is any one character,)
4627 convert "c"[0] to an int and pass that to the replacement
4629 arg = build_int_cst (NULL_TREE, fmt_str[0]);
4630 arglist = build_tree_list (NULL_TREE, arg);
4635 /* If the format specifier was "string\n", call puts("string"). */
4636 size_t len = strlen (fmt_str);
4637 if (fmt_str[len - 1] == '\n')
4639 /* Create a NUL-terminated string that's one char shorter
4640 than the original, stripping off the trailing '\n'. */
4641 char *newstr = alloca (len);
4642 memcpy (newstr, fmt_str, len - 1);
4643 newstr[len - 1] = 0;
4645 arg = build_string_literal (len, newstr);
4646 arglist = build_tree_list (NULL_TREE, arg);
4650 /* We'd like to arrange to call fputs(string,stdout) here,
4651 but we need stdout and don't have a way to get it yet. */
4658 return expand_expr (build_function_call_expr (fn, arglist),
4659 target, mode, EXPAND_NORMAL);
4662 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4663 Return 0 if a normal call should be emitted rather than transforming
4664 the function inline. If convenient, the result should be placed in
4665 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4668 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
4671 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4672 : implicit_built_in_decls[BUILT_IN_FPUTC];
4673 tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4674 : implicit_built_in_decls[BUILT_IN_FPUTS];
4675 const char *fmt_str;
4676 tree fn, fmt, fp, arg;
4678 /* If the return value is used, don't do the transformation. */
4679 if (target != const0_rtx)
4682 /* Verify the required arguments in the original call. */
4685 fp = TREE_VALUE (arglist);
4686 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
4688 arglist = TREE_CHAIN (arglist);
4691 fmt = TREE_VALUE (arglist);
4692 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4694 arglist = TREE_CHAIN (arglist);
4696 /* Check whether the format is a literal string constant. */
4697 fmt_str = c_getstr (fmt);
4698 if (fmt_str == NULL)
4701 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4702 if (strcmp (fmt_str, "%s") == 0)
4705 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4706 || TREE_CHAIN (arglist))
4708 arg = TREE_VALUE (arglist);
4709 arglist = build_tree_list (NULL_TREE, fp);
4710 arglist = tree_cons (NULL_TREE, arg, arglist);
4713 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
4714 else if (strcmp (fmt_str, "%c") == 0)
4717 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4718 || TREE_CHAIN (arglist))
4720 arg = TREE_VALUE (arglist);
4721 arglist = build_tree_list (NULL_TREE, fp);
4722 arglist = tree_cons (NULL_TREE, arg, arglist);
4727 /* We can't handle anything else with % args or %% ... yet. */
4728 if (strchr (fmt_str, '%'))
4734 /* If the format specifier was "", fprintf does nothing. */
4735 if (fmt_str[0] == '\0')
4737 /* Evaluate and ignore FILE* argument for side-effects. */
4738 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4742 /* When "string" doesn't contain %, replace all cases of
4743 fprintf(stream,string) with fputs(string,stream). The fputs
4744 builtin will take care of special cases like length == 1. */
4745 arglist = build_tree_list (NULL_TREE, fp);
4746 arglist = tree_cons (NULL_TREE, fmt, arglist);
4752 return expand_expr (build_function_call_expr (fn, arglist),
4753 target, mode, EXPAND_NORMAL);
4756 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
4757 a normal call should be emitted rather than expanding the function
4758 inline. If convenient, the result should be placed in TARGET with
4762 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
4764 tree orig_arglist, dest, fmt;
4765 const char *fmt_str;
4767 orig_arglist = arglist;
4769 /* Verify the required arguments in the original call. */
4772 dest = TREE_VALUE (arglist);
4773 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
4775 arglist = TREE_CHAIN (arglist);
4778 fmt = TREE_VALUE (arglist);
4779 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4781 arglist = TREE_CHAIN (arglist);
4783 /* Check whether the format is a literal string constant. */
4784 fmt_str = c_getstr (fmt);
4785 if (fmt_str == NULL)
4788 /* If the format doesn't contain % args or %%, use strcpy. */
4789 if (strchr (fmt_str, '%') == 0)
4791 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4794 if (arglist || ! fn)
4796 expand_expr (build_function_call_expr (fn, orig_arglist),
4797 const0_rtx, VOIDmode, EXPAND_NORMAL);
4798 if (target == const0_rtx)
4800 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
4801 return expand_expr (exp, target, mode, EXPAND_NORMAL);
4803 /* If the format is "%s", use strcpy if the result isn't used. */
4804 else if (strcmp (fmt_str, "%s") == 0)
4807 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4812 if (! arglist || TREE_CHAIN (arglist))
4814 arg = TREE_VALUE (arglist);
4815 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
4818 if (target != const0_rtx)
4820 len = c_strlen (arg, 1);
4821 if (! len || TREE_CODE (len) != INTEGER_CST)
4827 arglist = build_tree_list (NULL_TREE, arg);
4828 arglist = tree_cons (NULL_TREE, dest, arglist);
4829 expand_expr (build_function_call_expr (fn, arglist),
4830 const0_rtx, VOIDmode, EXPAND_NORMAL);
4832 if (target == const0_rtx)
4834 return expand_expr (len, target, mode, EXPAND_NORMAL);
4840 /* Expand a call to either the entry or exit function profiler. */
4843 expand_builtin_profile_func (bool exitp)
4847 this = DECL_RTL (current_function_decl);
4848 gcc_assert (MEM_P (this));
4849 this = XEXP (this, 0);
4852 which = profile_function_exit_libfunc;
4854 which = profile_function_entry_libfunc;
4856 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
4857 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
4864 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4867 round_trampoline_addr (rtx tramp)
4869 rtx temp, addend, mask;
4871 /* If we don't need too much alignment, we'll have been guaranteed
4872 proper alignment by get_trampoline_type. */
4873 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4876 /* Round address up to desired boundary. */
4877 temp = gen_reg_rtx (Pmode);
4878 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4879 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4881 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4882 temp, 0, OPTAB_LIB_WIDEN);
4883 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4884 temp, 0, OPTAB_LIB_WIDEN);
4890 expand_builtin_init_trampoline (tree arglist)
4892 tree t_tramp, t_func, t_chain;
4893 rtx r_tramp, r_func, r_chain;
4894 #ifdef TRAMPOLINE_TEMPLATE
4898 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
4899 POINTER_TYPE, VOID_TYPE))
4902 t_tramp = TREE_VALUE (arglist);
4903 arglist = TREE_CHAIN (arglist);
4904 t_func = TREE_VALUE (arglist);
4905 arglist = TREE_CHAIN (arglist);
4906 t_chain = TREE_VALUE (arglist);
4908 r_tramp = expand_expr (t_tramp, NULL_RTX, VOIDmode, 0);
4909 r_func = expand_expr (t_func, NULL_RTX, VOIDmode, 0);
4910 r_chain = expand_expr (t_chain, NULL_RTX, VOIDmode, 0);
4912 /* Generate insns to initialize the trampoline. */
4913 r_tramp = round_trampoline_addr (r_tramp);
4914 #ifdef TRAMPOLINE_TEMPLATE
4915 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
4916 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
4917 emit_block_move (blktramp, assemble_trampoline_template (),
4918 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
4920 trampolines_created = 1;
4921 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
4927 expand_builtin_adjust_trampoline (tree arglist)
4931 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
4934 tramp = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4935 tramp = round_trampoline_addr (tramp);
4936 #ifdef TRAMPOLINE_ADJUST_ADDRESS
4937 TRAMPOLINE_ADJUST_ADDRESS (tramp);
4943 /* Expand a call to the built-in signbit, signbitf or signbitl function.
4944 Return NULL_RTX if a normal call should be emitted rather than expanding
4945 the function in-line. EXP is the expression that is a call to the builtin
4946 function; if convenient, the result should be placed in TARGET. */
4949 expand_builtin_signbit (tree exp, rtx target)
4951 const struct real_format *fmt;
4952 enum machine_mode fmode, imode, rmode;
4953 HOST_WIDE_INT hi, lo;
4958 arglist = TREE_OPERAND (exp, 1);
4959 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4962 arg = TREE_VALUE (arglist);
4963 fmode = TYPE_MODE (TREE_TYPE (arg));
4964 rmode = TYPE_MODE (TREE_TYPE (exp));
4965 fmt = REAL_MODE_FORMAT (fmode);
4967 /* For floating point formats without a sign bit, implement signbit
4969 if (fmt->signbit < 0)
4971 /* But we can't do this if the format supports signed zero. */
4972 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4975 arg = fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
4976 build_real (TREE_TYPE (arg), dconst0)));
4977 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4980 imode = int_mode_for_mode (fmode);
4981 if (imode == BLKmode)
4984 bitpos = fmt->signbit;
4985 /* Handle targets with different FP word orders. */
4986 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4988 int nwords = GET_MODE_BITSIZE (fmode) / BITS_PER_WORD;
4989 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
4990 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
4993 /* If the sign bit is not in the lowpart and the floating point format
4994 is wider than an integer, check that is twice the size of an integer
4995 so that we can use gen_highpart below. */
4996 if (bitpos >= GET_MODE_BITSIZE (rmode)
4997 && GET_MODE_BITSIZE (imode) != 2 * GET_MODE_BITSIZE (rmode))
5000 temp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
5001 temp = gen_lowpart (imode, temp);
5003 if (GET_MODE_BITSIZE (imode) > GET_MODE_BITSIZE (rmode))
5005 if (BYTES_BIG_ENDIAN)
5006 bitpos = GET_MODE_BITSIZE (imode) - 1 - bitpos;
5007 temp = copy_to_mode_reg (imode, temp);
5008 temp = extract_bit_field (temp, 1, bitpos, 1,
5009 NULL_RTX, rmode, rmode);
5013 if (GET_MODE_BITSIZE (imode) < GET_MODE_BITSIZE (rmode))
5014 temp = gen_lowpart (rmode, temp);
5015 if (bitpos < HOST_BITS_PER_WIDE_INT)
5018 lo = (HOST_WIDE_INT) 1 << bitpos;
5022 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5026 temp = force_reg (rmode, temp);
5027 temp = expand_binop (rmode, and_optab, temp,
5028 immed_double_const (lo, hi, rmode),
5029 target, 1, OPTAB_LIB_WIDEN);
5034 /* Expand fork or exec calls. TARGET is the desired target of the
5035 call. ARGLIST is the list of arguments of the call. FN is the
5036 identificator of the actual function. IGNORE is nonzero if the
5037 value is to be ignored. */
5040 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5045 /* If we are not profiling, just call the function. */
5046 if (!profile_arc_flag)
5049 /* Otherwise call the wrapper. This should be equivalent for the rest of
5050 compiler, so the code does not diverge, and the wrapper may run the
5051 code necessary for keeping the profiling sane. */
5053 switch (DECL_FUNCTION_CODE (fn))
5056 id = get_identifier ("__gcov_fork");
5059 case BUILT_IN_EXECL:
5060 id = get_identifier ("__gcov_execl");
5063 case BUILT_IN_EXECV:
5064 id = get_identifier ("__gcov_execv");
5067 case BUILT_IN_EXECLP:
5068 id = get_identifier ("__gcov_execlp");
5071 case BUILT_IN_EXECLE:
5072 id = get_identifier ("__gcov_execle");
5075 case BUILT_IN_EXECVP:
5076 id = get_identifier ("__gcov_execvp");
5079 case BUILT_IN_EXECVE:
5080 id = get_identifier ("__gcov_execve");
5087 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5088 DECL_EXTERNAL (decl) = 1;
5089 TREE_PUBLIC (decl) = 1;
5090 DECL_ARTIFICIAL (decl) = 1;
5091 TREE_NOTHROW (decl) = 1;
5092 call = build_function_call_expr (decl, arglist);
5094 return expand_call (call, target, ignore);
5097 /* Expand an expression EXP that calls a built-in function,
5098 with result going to TARGET if that's convenient
5099 (and in mode MODE if that's convenient).
5100 SUBTARGET may be used as the target for computing one of EXP's operands.
5101 IGNORE is nonzero if the value is to be ignored. */
5104 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5107 tree fndecl = get_callee_fndecl (exp);
5108 tree arglist = TREE_OPERAND (exp, 1);
5109 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5110 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5112 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5113 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5115 /* When not optimizing, generate calls to library functions for a certain
5118 && !CALLED_AS_BUILT_IN (fndecl)
5119 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5120 && fcode != BUILT_IN_ALLOCA)
5121 return expand_call (exp, target, ignore);
5123 /* The built-in function expanders test for target == const0_rtx
5124 to determine whether the function's result will be ignored. */
5126 target = const0_rtx;
5128 /* If the result of a pure or const built-in function is ignored, and
5129 none of its arguments are volatile, we can avoid expanding the
5130 built-in call and just evaluate the arguments for side-effects. */
5131 if (target == const0_rtx
5132 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5134 bool volatilep = false;
5137 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5138 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5146 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5147 expand_expr (TREE_VALUE (arg), const0_rtx,
5148 VOIDmode, EXPAND_NORMAL);
5156 case BUILT_IN_FABSF:
5157 case BUILT_IN_FABSL:
5158 target = expand_builtin_fabs (arglist, target, subtarget);
5163 case BUILT_IN_COPYSIGN:
5164 case BUILT_IN_COPYSIGNF:
5165 case BUILT_IN_COPYSIGNL:
5166 target = expand_builtin_copysign (arglist, target, subtarget);
5171 /* Just do a normal library call if we were unable to fold
5174 case BUILT_IN_CABSF:
5175 case BUILT_IN_CABSL:
5181 case BUILT_IN_EXP10:
5182 case BUILT_IN_EXP10F:
5183 case BUILT_IN_EXP10L:
5184 case BUILT_IN_POW10:
5185 case BUILT_IN_POW10F:
5186 case BUILT_IN_POW10L:
5188 case BUILT_IN_EXP2F:
5189 case BUILT_IN_EXP2L:
5190 case BUILT_IN_EXPM1:
5191 case BUILT_IN_EXPM1F:
5192 case BUILT_IN_EXPM1L:
5194 case BUILT_IN_LOGBF:
5195 case BUILT_IN_LOGBL:
5196 case BUILT_IN_ILOGB:
5197 case BUILT_IN_ILOGBF:
5198 case BUILT_IN_ILOGBL:
5202 case BUILT_IN_LOG10:
5203 case BUILT_IN_LOG10F:
5204 case BUILT_IN_LOG10L:
5206 case BUILT_IN_LOG2F:
5207 case BUILT_IN_LOG2L:
5208 case BUILT_IN_LOG1P:
5209 case BUILT_IN_LOG1PF:
5210 case BUILT_IN_LOG1PL:
5215 case BUILT_IN_ASINF:
5216 case BUILT_IN_ASINL:
5218 case BUILT_IN_ACOSF:
5219 case BUILT_IN_ACOSL:
5221 case BUILT_IN_ATANF:
5222 case BUILT_IN_ATANL:
5223 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5224 because of possible accuracy problems. */
5225 if (! flag_unsafe_math_optimizations)
5228 case BUILT_IN_SQRTF:
5229 case BUILT_IN_SQRTL:
5230 case BUILT_IN_FLOOR:
5231 case BUILT_IN_FLOORF:
5232 case BUILT_IN_FLOORL:
5234 case BUILT_IN_CEILF:
5235 case BUILT_IN_CEILL:
5236 case BUILT_IN_TRUNC:
5237 case BUILT_IN_TRUNCF:
5238 case BUILT_IN_TRUNCL:
5239 case BUILT_IN_ROUND:
5240 case BUILT_IN_ROUNDF:
5241 case BUILT_IN_ROUNDL:
5242 case BUILT_IN_NEARBYINT:
5243 case BUILT_IN_NEARBYINTF:
5244 case BUILT_IN_NEARBYINTL:
5246 case BUILT_IN_RINTF:
5247 case BUILT_IN_RINTL:
5248 target = expand_builtin_mathfn (exp, target, subtarget);
5256 target = expand_builtin_pow (exp, target, subtarget);
5262 case BUILT_IN_POWIF:
5263 case BUILT_IN_POWIL:
5264 target = expand_builtin_powi (exp, target, subtarget);
5269 case BUILT_IN_ATAN2:
5270 case BUILT_IN_ATAN2F:
5271 case BUILT_IN_ATAN2L:
5272 case BUILT_IN_LDEXP:
5273 case BUILT_IN_LDEXPF:
5274 case BUILT_IN_LDEXPL:
5276 case BUILT_IN_FMODF:
5277 case BUILT_IN_FMODL:
5279 case BUILT_IN_DREMF:
5280 case BUILT_IN_DREML:
5281 if (! flag_unsafe_math_optimizations)
5283 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5294 if (! flag_unsafe_math_optimizations)
5296 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5301 case BUILT_IN_APPLY_ARGS:
5302 return expand_builtin_apply_args ();
5304 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5305 FUNCTION with a copy of the parameters described by
5306 ARGUMENTS, and ARGSIZE. It returns a block of memory
5307 allocated on the stack into which is stored all the registers
5308 that might possibly be used for returning the result of a
5309 function. ARGUMENTS is the value returned by
5310 __builtin_apply_args. ARGSIZE is the number of bytes of
5311 arguments that must be copied. ??? How should this value be
5312 computed? We'll also need a safe worst case value for varargs
5314 case BUILT_IN_APPLY:
5315 if (!validate_arglist (arglist, POINTER_TYPE,
5316 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5317 && !validate_arglist (arglist, REFERENCE_TYPE,
5318 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5326 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5327 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5329 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5332 /* __builtin_return (RESULT) causes the function to return the
5333 value described by RESULT. RESULT is address of the block of
5334 memory returned by __builtin_apply. */
5335 case BUILT_IN_RETURN:
5336 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5337 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5338 NULL_RTX, VOIDmode, 0));
5341 case BUILT_IN_SAVEREGS:
5342 return expand_builtin_saveregs ();
5344 case BUILT_IN_ARGS_INFO:
5345 return expand_builtin_args_info (arglist);
5347 /* Return the address of the first anonymous stack arg. */
5348 case BUILT_IN_NEXT_ARG:
5349 if (fold_builtin_next_arg (arglist))
5351 return expand_builtin_next_arg ();
5353 case BUILT_IN_CLASSIFY_TYPE:
5354 return expand_builtin_classify_type (arglist);
5356 case BUILT_IN_CONSTANT_P:
5359 case BUILT_IN_FRAME_ADDRESS:
5360 case BUILT_IN_RETURN_ADDRESS:
5361 return expand_builtin_frame_address (fndecl, arglist);
5363 /* Returns the address of the area where the structure is returned.
5365 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5367 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5368 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5371 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5373 case BUILT_IN_ALLOCA:
5374 target = expand_builtin_alloca (arglist, target);
5379 case BUILT_IN_STACK_SAVE:
5380 return expand_stack_save ();
5382 case BUILT_IN_STACK_RESTORE:
5383 expand_stack_restore (TREE_VALUE (arglist));
5388 case BUILT_IN_FFSLL:
5389 case BUILT_IN_FFSIMAX:
5390 target = expand_builtin_unop (target_mode, arglist, target,
5391 subtarget, ffs_optab);
5398 case BUILT_IN_CLZLL:
5399 case BUILT_IN_CLZIMAX:
5400 target = expand_builtin_unop (target_mode, arglist, target,
5401 subtarget, clz_optab);
5408 case BUILT_IN_CTZLL:
5409 case BUILT_IN_CTZIMAX:
5410 target = expand_builtin_unop (target_mode, arglist, target,
5411 subtarget, ctz_optab);
5416 case BUILT_IN_POPCOUNT:
5417 case BUILT_IN_POPCOUNTL:
5418 case BUILT_IN_POPCOUNTLL:
5419 case BUILT_IN_POPCOUNTIMAX:
5420 target = expand_builtin_unop (target_mode, arglist, target,
5421 subtarget, popcount_optab);
5426 case BUILT_IN_PARITY:
5427 case BUILT_IN_PARITYL:
5428 case BUILT_IN_PARITYLL:
5429 case BUILT_IN_PARITYIMAX:
5430 target = expand_builtin_unop (target_mode, arglist, target,
5431 subtarget, parity_optab);
5436 case BUILT_IN_STRLEN:
5437 target = expand_builtin_strlen (arglist, target, target_mode);
5442 case BUILT_IN_STRCPY:
5443 target = expand_builtin_strcpy (exp, target, mode);
5448 case BUILT_IN_STRNCPY:
5449 target = expand_builtin_strncpy (exp, target, mode);
5454 case BUILT_IN_STPCPY:
5455 target = expand_builtin_stpcpy (exp, target, mode);
5460 case BUILT_IN_STRCAT:
5461 target = expand_builtin_strcat (arglist, TREE_TYPE (exp), target, mode);
5466 case BUILT_IN_STRNCAT:
5467 target = expand_builtin_strncat (arglist, target, mode);
5472 case BUILT_IN_STRSPN:
5473 target = expand_builtin_strspn (arglist, target, mode);
5478 case BUILT_IN_STRCSPN:
5479 target = expand_builtin_strcspn (arglist, target, mode);
5484 case BUILT_IN_STRSTR:
5485 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
5490 case BUILT_IN_STRPBRK:
5491 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
5496 case BUILT_IN_INDEX:
5497 case BUILT_IN_STRCHR:
5498 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
5503 case BUILT_IN_RINDEX:
5504 case BUILT_IN_STRRCHR:
5505 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
5510 case BUILT_IN_MEMCPY:
5511 target = expand_builtin_memcpy (exp, target, mode);
5516 case BUILT_IN_MEMPCPY:
5517 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
5522 case BUILT_IN_MEMMOVE:
5523 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target, mode);
5528 case BUILT_IN_BCOPY:
5529 target = expand_builtin_bcopy (arglist, TREE_TYPE (exp));
5534 case BUILT_IN_MEMSET:
5535 target = expand_builtin_memset (arglist, target, mode);
5540 case BUILT_IN_BZERO:
5541 target = expand_builtin_bzero (arglist);
5546 case BUILT_IN_STRCMP:
5547 target = expand_builtin_strcmp (exp, target, mode);
5552 case BUILT_IN_STRNCMP:
5553 target = expand_builtin_strncmp (exp, target, mode);
5559 case BUILT_IN_MEMCMP:
5560 target = expand_builtin_memcmp (exp, arglist, target, mode);
5565 case BUILT_IN_SETJMP:
5566 target = expand_builtin_setjmp (arglist, target);
5571 /* __builtin_longjmp is passed a pointer to an array of five words.
5572 It's similar to the C library longjmp function but works with
5573 __builtin_setjmp above. */
5574 case BUILT_IN_LONGJMP:
5575 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5579 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5581 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5582 NULL_RTX, VOIDmode, 0);
5584 if (value != const1_rtx)
5586 error ("%<__builtin_longjmp%> second argument must be 1");
5590 expand_builtin_longjmp (buf_addr, value);
5594 case BUILT_IN_NONLOCAL_GOTO:
5595 target = expand_builtin_nonlocal_goto (arglist);
5600 /* This updates the setjmp buffer that is its argument with the value
5601 of the current stack pointer. */
5602 case BUILT_IN_UPDATE_SETJMP_BUF:
5603 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5606 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5608 expand_builtin_update_setjmp_buf (buf_addr);
5614 expand_builtin_trap ();
5617 case BUILT_IN_PRINTF:
5618 target = expand_builtin_printf (arglist, target, mode, false);
5623 case BUILT_IN_PRINTF_UNLOCKED:
5624 target = expand_builtin_printf (arglist, target, mode, true);
5629 case BUILT_IN_FPUTS:
5630 target = expand_builtin_fputs (arglist, target, false);
5634 case BUILT_IN_FPUTS_UNLOCKED:
5635 target = expand_builtin_fputs (arglist, target, true);
5640 case BUILT_IN_FPRINTF:
5641 target = expand_builtin_fprintf (arglist, target, mode, false);
5646 case BUILT_IN_FPRINTF_UNLOCKED:
5647 target = expand_builtin_fprintf (arglist, target, mode, true);
5652 case BUILT_IN_SPRINTF:
5653 target = expand_builtin_sprintf (arglist, target, mode);
5658 case BUILT_IN_SIGNBIT:
5659 case BUILT_IN_SIGNBITF:
5660 case BUILT_IN_SIGNBITL:
5661 target = expand_builtin_signbit (exp, target);
5666 /* Various hooks for the DWARF 2 __throw routine. */
5667 case BUILT_IN_UNWIND_INIT:
5668 expand_builtin_unwind_init ();
5670 case BUILT_IN_DWARF_CFA:
5671 return virtual_cfa_rtx;
5672 #ifdef DWARF2_UNWIND_INFO
5673 case BUILT_IN_DWARF_SP_COLUMN:
5674 return expand_builtin_dwarf_sp_column ();
5675 case BUILT_IN_INIT_DWARF_REG_SIZES:
5676 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5679 case BUILT_IN_FROB_RETURN_ADDR:
5680 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5681 case BUILT_IN_EXTRACT_RETURN_ADDR:
5682 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5683 case BUILT_IN_EH_RETURN:
5684 expand_builtin_eh_return (TREE_VALUE (arglist),
5685 TREE_VALUE (TREE_CHAIN (arglist)));
5687 #ifdef EH_RETURN_DATA_REGNO
5688 case BUILT_IN_EH_RETURN_DATA_REGNO:
5689 return expand_builtin_eh_return_data_regno (arglist);
5691 case BUILT_IN_EXTEND_POINTER:
5692 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
5694 case BUILT_IN_VA_START:
5695 case BUILT_IN_STDARG_START:
5696 return expand_builtin_va_start (arglist);
5697 case BUILT_IN_VA_END:
5698 return expand_builtin_va_end (arglist);
5699 case BUILT_IN_VA_COPY:
5700 return expand_builtin_va_copy (arglist);
5701 case BUILT_IN_EXPECT:
5702 return expand_builtin_expect (arglist, target);
5703 case BUILT_IN_PREFETCH:
5704 expand_builtin_prefetch (arglist);
5707 case BUILT_IN_PROFILE_FUNC_ENTER:
5708 return expand_builtin_profile_func (false);
5709 case BUILT_IN_PROFILE_FUNC_EXIT:
5710 return expand_builtin_profile_func (true);
5712 case BUILT_IN_INIT_TRAMPOLINE:
5713 return expand_builtin_init_trampoline (arglist);
5714 case BUILT_IN_ADJUST_TRAMPOLINE:
5715 return expand_builtin_adjust_trampoline (arglist);
5718 case BUILT_IN_EXECL:
5719 case BUILT_IN_EXECV:
5720 case BUILT_IN_EXECLP:
5721 case BUILT_IN_EXECLE:
5722 case BUILT_IN_EXECVP:
5723 case BUILT_IN_EXECVE:
5724 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
5729 default: /* just do library call, if unknown builtin */
5733 /* The switch statement above can drop through to cause the function
5734 to be called normally. */
5735 return expand_call (exp, target, ignore);
5738 /* Determine whether a tree node represents a call to a built-in
5739 function. If the tree T is a call to a built-in function with
5740 the right number of arguments of the appropriate types, return
5741 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
5742 Otherwise the return value is END_BUILTINS. */
5744 enum built_in_function
5745 builtin_mathfn_code (tree t)
5747 tree fndecl, arglist, parmlist;
5748 tree argtype, parmtype;
5750 if (TREE_CODE (t) != CALL_EXPR
5751 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
5752 return END_BUILTINS;
5754 fndecl = get_callee_fndecl (t);
5755 if (fndecl == NULL_TREE
5756 || TREE_CODE (fndecl) != FUNCTION_DECL
5757 || ! DECL_BUILT_IN (fndecl)
5758 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5759 return END_BUILTINS;
5761 arglist = TREE_OPERAND (t, 1);
5762 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5763 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
5765 /* If a function doesn't take a variable number of arguments,
5766 the last element in the list will have type `void'. */
5767 parmtype = TREE_VALUE (parmlist);
5768 if (VOID_TYPE_P (parmtype))
5771 return END_BUILTINS;
5772 return DECL_FUNCTION_CODE (fndecl);
5776 return END_BUILTINS;
5778 argtype = TREE_TYPE (TREE_VALUE (arglist));
5780 if (SCALAR_FLOAT_TYPE_P (parmtype))
5782 if (! SCALAR_FLOAT_TYPE_P (argtype))
5783 return END_BUILTINS;
5785 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
5787 if (! COMPLEX_FLOAT_TYPE_P (argtype))
5788 return END_BUILTINS;
5790 else if (POINTER_TYPE_P (parmtype))
5792 if (! POINTER_TYPE_P (argtype))
5793 return END_BUILTINS;
5795 else if (INTEGRAL_TYPE_P (parmtype))
5797 if (! INTEGRAL_TYPE_P (argtype))
5798 return END_BUILTINS;
5801 return END_BUILTINS;
5803 arglist = TREE_CHAIN (arglist);
5806 /* Variable-length argument list. */
5807 return DECL_FUNCTION_CODE (fndecl);
5810 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5811 constant. ARGLIST is the argument list of the call. */
5814 fold_builtin_constant_p (tree arglist)
5819 arglist = TREE_VALUE (arglist);
5821 /* We return 1 for a numeric type that's known to be a constant
5822 value at compile-time or for an aggregate type that's a
5823 literal constant. */
5824 STRIP_NOPS (arglist);
5826 /* If we know this is a constant, emit the constant of one. */
5827 if (CONSTANT_CLASS_P (arglist)
5828 || (TREE_CODE (arglist) == CONSTRUCTOR
5829 && TREE_CONSTANT (arglist))
5830 || (TREE_CODE (arglist) == ADDR_EXPR
5831 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
5832 return integer_one_node;
5834 /* If this expression has side effects, show we don't know it to be a
5835 constant. Likewise if it's a pointer or aggregate type since in
5836 those case we only want literals, since those are only optimized
5837 when generating RTL, not later.
5838 And finally, if we are compiling an initializer, not code, we
5839 need to return a definite result now; there's not going to be any
5840 more optimization done. */
5841 if (TREE_SIDE_EFFECTS (arglist)
5842 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
5843 || POINTER_TYPE_P (TREE_TYPE (arglist))
5845 return integer_zero_node;
5850 /* Fold a call to __builtin_expect, if we expect that a comparison against
5851 the argument will fold to a constant. In practice, this means a true
5852 constant or the address of a non-weak symbol. ARGLIST is the argument
5853 list of the call. */
5856 fold_builtin_expect (tree arglist)
5863 arg = TREE_VALUE (arglist);
5865 /* If the argument isn't invariant, then there's nothing we can do. */
5866 if (!TREE_INVARIANT (arg))
5869 /* If we're looking at an address of a weak decl, then do not fold. */
5872 if (TREE_CODE (inner) == ADDR_EXPR)
5876 inner = TREE_OPERAND (inner, 0);
5878 while (TREE_CODE (inner) == COMPONENT_REF
5879 || TREE_CODE (inner) == ARRAY_REF);
5880 if (DECL_P (inner) && DECL_WEAK (inner))
5884 /* Otherwise, ARG already has the proper type for the return value. */
5888 /* Fold a call to __builtin_classify_type. */
5891 fold_builtin_classify_type (tree arglist)
5894 return build_int_cst (NULL_TREE, no_type_class);
5896 return build_int_cst (NULL_TREE,
5897 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
5900 /* Fold a call to __builtin_strlen. */
5903 fold_builtin_strlen (tree arglist)
5905 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5909 tree len = c_strlen (TREE_VALUE (arglist), 0);
5913 /* Convert from the internal "sizetype" type to "size_t". */
5915 len = fold_convert (size_type_node, len);
5923 /* Fold a call to __builtin_inf or __builtin_huge_val. */
5926 fold_builtin_inf (tree type, int warn)
5928 REAL_VALUE_TYPE real;
5930 /* __builtin_inff is intended to be usable to define INFINITY on all
5931 targets. If an infinity is not available, INFINITY expands "to a
5932 positive constant of type float that overflows at translation
5933 time", footnote "In this case, using INFINITY will violate the
5934 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
5935 Thus we pedwarn to ensure this constraint violation is
5937 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
5938 pedwarn ("target format does not support infinity");
5941 return build_real (type, real);
5944 /* Fold a call to __builtin_nan or __builtin_nans. */
5947 fold_builtin_nan (tree arglist, tree type, int quiet)
5949 REAL_VALUE_TYPE real;
5952 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5954 str = c_getstr (TREE_VALUE (arglist));
5958 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
5961 return build_real (type, real);
5964 /* Return true if the floating point expression T has an integer value.
5965 We also allow +Inf, -Inf and NaN to be considered integer values. */
5968 integer_valued_real_p (tree t)
5970 switch (TREE_CODE (t))
5977 case NON_LVALUE_EXPR:
5978 return integer_valued_real_p (TREE_OPERAND (t, 0));
5983 return integer_valued_real_p (TREE_OPERAND (t, 1));
5990 return integer_valued_real_p (TREE_OPERAND (t, 0))
5991 && integer_valued_real_p (TREE_OPERAND (t, 1));
5994 return integer_valued_real_p (TREE_OPERAND (t, 1))
5995 && integer_valued_real_p (TREE_OPERAND (t, 2));
5998 if (! TREE_CONSTANT_OVERFLOW (t))
6000 REAL_VALUE_TYPE c, cint;
6002 c = TREE_REAL_CST (t);
6003 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6004 return real_identical (&c, &cint);
6009 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6010 if (TREE_CODE (type) == INTEGER_TYPE)
6012 if (TREE_CODE (type) == REAL_TYPE)
6013 return integer_valued_real_p (TREE_OPERAND (t, 0));
6018 switch (builtin_mathfn_code (t))
6021 case BUILT_IN_CEILF:
6022 case BUILT_IN_CEILL:
6023 case BUILT_IN_FLOOR:
6024 case BUILT_IN_FLOORF:
6025 case BUILT_IN_FLOORL:
6026 case BUILT_IN_NEARBYINT:
6027 case BUILT_IN_NEARBYINTF:
6028 case BUILT_IN_NEARBYINTL:
6030 case BUILT_IN_RINTF:
6031 case BUILT_IN_RINTL:
6032 case BUILT_IN_ROUND:
6033 case BUILT_IN_ROUNDF:
6034 case BUILT_IN_ROUNDL:
6035 case BUILT_IN_TRUNC:
6036 case BUILT_IN_TRUNCF:
6037 case BUILT_IN_TRUNCL:
6051 /* EXP is assumed to be builtin call where truncation can be propagated
6052 across (for instance floor((double)f) == (double)floorf (f).
6053 Do the transformation. */
6056 fold_trunc_transparent_mathfn (tree exp)
6058 tree fndecl = get_callee_fndecl (exp);
6059 tree arglist = TREE_OPERAND (exp, 1);
6060 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6063 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6066 arg = TREE_VALUE (arglist);
6067 /* Integer rounding functions are idempotent. */
6068 if (fcode == builtin_mathfn_code (arg))
6071 /* If argument is already integer valued, and we don't need to worry
6072 about setting errno, there's no need to perform rounding. */
6073 if (! flag_errno_math && integer_valued_real_p (arg))
6078 tree arg0 = strip_float_extensions (arg);
6079 tree ftype = TREE_TYPE (exp);
6080 tree newtype = TREE_TYPE (arg0);
6083 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6084 && (decl = mathfn_built_in (newtype, fcode)))
6087 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6088 return fold_convert (ftype,
6089 build_function_call_expr (decl, arglist));
6095 /* EXP is assumed to be builtin call which can narrow the FP type of
6096 the argument, for instance lround((double)f) -> lroundf (f). */
6099 fold_fixed_mathfn (tree exp)
6101 tree fndecl = get_callee_fndecl (exp);
6102 tree arglist = TREE_OPERAND (exp, 1);
6103 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6106 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6109 arg = TREE_VALUE (arglist);
6111 /* If argument is already integer valued, and we don't need to worry
6112 about setting errno, there's no need to perform rounding. */
6113 if (! flag_errno_math && integer_valued_real_p (arg))
6114 return fold (build1 (FIX_TRUNC_EXPR, TREE_TYPE (exp), arg));
6118 tree ftype = TREE_TYPE (arg);
6119 tree arg0 = strip_float_extensions (arg);
6120 tree newtype = TREE_TYPE (arg0);
6123 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6124 && (decl = mathfn_built_in (newtype, fcode)))
6127 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6128 return build_function_call_expr (decl, arglist);
6134 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6135 is the argument list and TYPE is the return type. Return
6136 NULL_TREE if no if no simplification can be made. */
6139 fold_builtin_cabs (tree arglist, tree type)
6143 if (!arglist || TREE_CHAIN (arglist))
6146 arg = TREE_VALUE (arglist);
6147 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6148 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6151 /* Evaluate cabs of a constant at compile-time. */
6152 if (flag_unsafe_math_optimizations
6153 && TREE_CODE (arg) == COMPLEX_CST
6154 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6155 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6156 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6157 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6159 REAL_VALUE_TYPE r, i;
6161 r = TREE_REAL_CST (TREE_REALPART (arg));
6162 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6164 real_arithmetic (&r, MULT_EXPR, &r, &r);
6165 real_arithmetic (&i, MULT_EXPR, &i, &i);
6166 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6167 if (real_sqrt (&r, TYPE_MODE (type), &r)
6168 || ! flag_trapping_math)
6169 return build_real (type, r);
6172 /* If either part is zero, cabs is fabs of the other. */
6173 if (TREE_CODE (arg) == COMPLEX_EXPR
6174 && real_zerop (TREE_OPERAND (arg, 0)))
6175 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
6176 if (TREE_CODE (arg) == COMPLEX_EXPR
6177 && real_zerop (TREE_OPERAND (arg, 1)))
6178 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
6180 /* Don't do this when optimizing for size. */
6181 if (flag_unsafe_math_optimizations
6182 && optimize && !optimize_size)
6184 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6186 if (sqrtfn != NULL_TREE)
6188 tree rpart, ipart, result, arglist;
6190 arg = builtin_save_expr (arg);
6192 rpart = fold (build1 (REALPART_EXPR, type, arg));
6193 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
6195 rpart = builtin_save_expr (rpart);
6196 ipart = builtin_save_expr (ipart);
6198 result = fold (build2 (PLUS_EXPR, type,
6199 fold (build2 (MULT_EXPR, type,
6201 fold (build2 (MULT_EXPR, type,
6204 arglist = build_tree_list (NULL_TREE, result);
6205 return build_function_call_expr (sqrtfn, arglist);
6212 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
6213 NULL_TREE if no simplification can be made. */
6216 fold_builtin_sqrt (tree arglist, tree type)
6219 enum built_in_function fcode;
6220 tree arg = TREE_VALUE (arglist);
6222 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6225 /* Optimize sqrt of constant value. */
6226 if (TREE_CODE (arg) == REAL_CST
6227 && ! TREE_CONSTANT_OVERFLOW (arg))
6229 REAL_VALUE_TYPE r, x;
6231 x = TREE_REAL_CST (arg);
6232 if (real_sqrt (&r, TYPE_MODE (type), &x)
6233 || (!flag_trapping_math && !flag_errno_math))
6234 return build_real (type, r);
6237 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
6238 fcode = builtin_mathfn_code (arg);
6239 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6241 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6242 arg = fold (build2 (MULT_EXPR, type,
6243 TREE_VALUE (TREE_OPERAND (arg, 1)),
6244 build_real (type, dconsthalf)));
6245 arglist = build_tree_list (NULL_TREE, arg);
6246 return build_function_call_expr (expfn, arglist);
6249 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
6250 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
6252 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6256 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6258 /* The inner root was either sqrt or cbrt. */
6259 REAL_VALUE_TYPE dconstroot =
6260 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
6262 /* Adjust for the outer root. */
6263 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6264 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6265 tree_root = build_real (type, dconstroot);
6266 arglist = tree_cons (NULL_TREE, arg0,
6267 build_tree_list (NULL_TREE, tree_root));
6268 return build_function_call_expr (powfn, arglist);
6272 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
6273 if (flag_unsafe_math_optimizations
6274 && (fcode == BUILT_IN_POW
6275 || fcode == BUILT_IN_POWF
6276 || fcode == BUILT_IN_POWL))
6278 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6279 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6280 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6282 if (!tree_expr_nonnegative_p (arg0))
6283 arg0 = build1 (ABS_EXPR, type, arg0);
6284 narg1 = fold (build2 (MULT_EXPR, type, arg1,
6285 build_real (type, dconsthalf)));
6286 arglist = tree_cons (NULL_TREE, arg0,
6287 build_tree_list (NULL_TREE, narg1));
6288 return build_function_call_expr (powfn, arglist);
6294 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
6295 NULL_TREE if no simplification can be made. */
6297 fold_builtin_cbrt (tree arglist, tree type)
6299 tree arg = TREE_VALUE (arglist);
6300 const enum built_in_function fcode = builtin_mathfn_code (arg);
6302 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6305 /* Optimize cbrt of constant value. */
6306 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
6309 /* Optimize cbrt(expN(x)) -> expN(x/3). */
6310 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6312 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6313 const REAL_VALUE_TYPE third_trunc =
6314 real_value_truncate (TYPE_MODE (type), dconstthird);
6315 arg = fold (build2 (MULT_EXPR, type,
6316 TREE_VALUE (TREE_OPERAND (arg, 1)),
6317 build_real (type, third_trunc)));
6318 arglist = build_tree_list (NULL_TREE, arg);
6319 return build_function_call_expr (expfn, arglist);
6322 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
6323 /* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
6324 x is negative pow will error but cbrt won't. */
6325 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
6327 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6331 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6333 REAL_VALUE_TYPE dconstroot = dconstthird;
6335 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6336 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6337 tree_root = build_real (type, dconstroot);
6338 arglist = tree_cons (NULL_TREE, arg0,
6339 build_tree_list (NULL_TREE, tree_root));
6340 return build_function_call_expr (powfn, arglist);
6347 /* Fold function call to builtin sin, sinf, or sinl. Return
6348 NULL_TREE if no simplification can be made. */
6350 fold_builtin_sin (tree arglist)
6352 tree arg = TREE_VALUE (arglist);
6354 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6357 /* Optimize sin (0.0) = 0.0. */
6358 if (real_zerop (arg))
6364 /* Fold function call to builtin cos, cosf, or cosl. Return
6365 NULL_TREE if no simplification can be made. */
6367 fold_builtin_cos (tree arglist, tree type, tree fndecl)
6369 tree arg = TREE_VALUE (arglist);
6371 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6374 /* Optimize cos (0.0) = 1.0. */
6375 if (real_zerop (arg))
6376 return build_real (type, dconst1);
6378 /* Optimize cos(-x) into cos (x). */
6379 if (TREE_CODE (arg) == NEGATE_EXPR)
6381 tree args = build_tree_list (NULL_TREE,
6382 TREE_OPERAND (arg, 0));
6383 return build_function_call_expr (fndecl, args);
6389 /* Fold function call to builtin tan, tanf, or tanl. Return
6390 NULL_TREE if no simplification can be made. */
6392 fold_builtin_tan (tree arglist)
6394 enum built_in_function fcode;
6395 tree arg = TREE_VALUE (arglist);
6397 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6400 /* Optimize tan(0.0) = 0.0. */
6401 if (real_zerop (arg))
6404 /* Optimize tan(atan(x)) = x. */
6405 fcode = builtin_mathfn_code (arg);
6406 if (flag_unsafe_math_optimizations
6407 && (fcode == BUILT_IN_ATAN
6408 || fcode == BUILT_IN_ATANF
6409 || fcode == BUILT_IN_ATANL))
6410 return TREE_VALUE (TREE_OPERAND (arg, 1));
6415 /* Fold function call to builtin atan, atanf, or atanl. Return
6416 NULL_TREE if no simplification can be made. */
6419 fold_builtin_atan (tree arglist, tree type)
6422 tree arg = TREE_VALUE (arglist);
6424 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6427 /* Optimize atan(0.0) = 0.0. */
6428 if (real_zerop (arg))
6431 /* Optimize atan(1.0) = pi/4. */
6432 if (real_onep (arg))
6434 REAL_VALUE_TYPE cst;
6436 real_convert (&cst, TYPE_MODE (type), &dconstpi);
6437 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
6438 return build_real (type, cst);
6444 /* Fold function call to builtin trunc, truncf or truncl. Return
6445 NULL_TREE if no simplification can be made. */
6448 fold_builtin_trunc (tree exp)
6450 tree arglist = TREE_OPERAND (exp, 1);
6453 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6456 /* Optimize trunc of constant value. */
6457 arg = TREE_VALUE (arglist);
6458 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6460 REAL_VALUE_TYPE r, x;
6461 tree type = TREE_TYPE (exp);
6463 x = TREE_REAL_CST (arg);
6464 real_trunc (&r, TYPE_MODE (type), &x);
6465 return build_real (type, r);
6468 return fold_trunc_transparent_mathfn (exp);
6471 /* Fold function call to builtin floor, floorf or floorl. Return
6472 NULL_TREE if no simplification can be made. */
6475 fold_builtin_floor (tree exp)
6477 tree arglist = TREE_OPERAND (exp, 1);
6480 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6483 /* Optimize floor of constant value. */
6484 arg = TREE_VALUE (arglist);
6485 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6489 x = TREE_REAL_CST (arg);
6490 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6492 tree type = TREE_TYPE (exp);
6495 real_floor (&r, TYPE_MODE (type), &x);
6496 return build_real (type, r);
6500 return fold_trunc_transparent_mathfn (exp);
6503 /* Fold function call to builtin ceil, ceilf or ceill. Return
6504 NULL_TREE if no simplification can be made. */
6507 fold_builtin_ceil (tree exp)
6509 tree arglist = TREE_OPERAND (exp, 1);
6512 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6515 /* Optimize ceil of constant value. */
6516 arg = TREE_VALUE (arglist);
6517 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6521 x = TREE_REAL_CST (arg);
6522 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6524 tree type = TREE_TYPE (exp);
6527 real_ceil (&r, TYPE_MODE (type), &x);
6528 return build_real (type, r);
6532 return fold_trunc_transparent_mathfn (exp);
6535 /* Fold function call to builtin round, roundf or roundl. Return
6536 NULL_TREE if no simplification can be made. */
6539 fold_builtin_round (tree exp)
6541 tree arglist = TREE_OPERAND (exp, 1);
6544 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6547 /* Optimize round of constant value. */
6548 arg = TREE_VALUE (arglist);
6549 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6553 x = TREE_REAL_CST (arg);
6554 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6556 tree type = TREE_TYPE (exp);
6559 real_round (&r, TYPE_MODE (type), &x);
6560 return build_real (type, r);
6564 return fold_trunc_transparent_mathfn (exp);
6567 /* Fold function call to builtin lround, lroundf or lroundl (or the
6568 corresponding long long versions). Return NULL_TREE if no
6569 simplification can be made. */
6572 fold_builtin_lround (tree exp)
6574 tree arglist = TREE_OPERAND (exp, 1);
6577 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6580 /* Optimize lround of constant value. */
6581 arg = TREE_VALUE (arglist);
6582 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6584 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
6586 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
6588 tree itype = TREE_TYPE (exp), ftype = TREE_TYPE (arg), result;
6589 HOST_WIDE_INT hi, lo;
6592 real_round (&r, TYPE_MODE (ftype), &x);
6593 REAL_VALUE_TO_INT (&lo, &hi, r);
6594 result = build_int_cst_wide (NULL_TREE, lo, hi);
6595 if (int_fits_type_p (result, itype))
6596 return fold_convert (itype, result);
6600 return fold_fixed_mathfn (exp);
6603 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
6604 and their long and long long variants (i.e. ffsl and ffsll).
6605 Return NULL_TREE if no simplification can be made. */
6608 fold_builtin_bitop (tree exp)
6610 tree fndecl = get_callee_fndecl (exp);
6611 tree arglist = TREE_OPERAND (exp, 1);
6614 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6617 /* Optimize for constant argument. */
6618 arg = TREE_VALUE (arglist);
6619 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6621 HOST_WIDE_INT hi, width, result;
6622 unsigned HOST_WIDE_INT lo;
6625 type = TREE_TYPE (arg);
6626 width = TYPE_PRECISION (type);
6627 lo = TREE_INT_CST_LOW (arg);
6629 /* Clear all the bits that are beyond the type's precision. */
6630 if (width > HOST_BITS_PER_WIDE_INT)
6632 hi = TREE_INT_CST_HIGH (arg);
6633 if (width < 2 * HOST_BITS_PER_WIDE_INT)
6634 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
6639 if (width < HOST_BITS_PER_WIDE_INT)
6640 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
6643 switch (DECL_FUNCTION_CODE (fndecl))
6647 case BUILT_IN_FFSLL:
6649 result = exact_log2 (lo & -lo) + 1;
6651 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
6658 case BUILT_IN_CLZLL:
6660 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
6662 result = width - floor_log2 (lo) - 1;
6663 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6669 case BUILT_IN_CTZLL:
6671 result = exact_log2 (lo & -lo);
6673 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
6674 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6678 case BUILT_IN_POPCOUNT:
6679 case BUILT_IN_POPCOUNTL:
6680 case BUILT_IN_POPCOUNTLL:
6683 result++, lo &= lo - 1;
6685 result++, hi &= hi - 1;
6688 case BUILT_IN_PARITY:
6689 case BUILT_IN_PARITYL:
6690 case BUILT_IN_PARITYLL:
6693 result++, lo &= lo - 1;
6695 result++, hi &= hi - 1;
6703 return build_int_cst (TREE_TYPE (exp), result);
6709 /* Return true if EXPR is the real constant contained in VALUE. */
6712 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
6716 return ((TREE_CODE (expr) == REAL_CST
6717 && ! TREE_CONSTANT_OVERFLOW (expr)
6718 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
6719 || (TREE_CODE (expr) == COMPLEX_CST
6720 && real_dconstp (TREE_REALPART (expr), value)
6721 && real_zerop (TREE_IMAGPART (expr))));
6724 /* A subroutine of fold_builtin to fold the various logarithmic
6725 functions. EXP is the CALL_EXPR of a call to a builtin logN
6726 function. VALUE is the base of the logN function. */
6729 fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
6731 tree arglist = TREE_OPERAND (exp, 1);
6733 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6735 tree fndecl = get_callee_fndecl (exp);
6736 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6737 tree arg = TREE_VALUE (arglist);
6738 const enum built_in_function fcode = builtin_mathfn_code (arg);
6740 /* Optimize logN(1.0) = 0.0. */
6741 if (real_onep (arg))
6742 return build_real (type, dconst0);
6744 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
6745 exactly, then only do this if flag_unsafe_math_optimizations. */
6746 if (exact_real_truncate (TYPE_MODE (type), value)
6747 || flag_unsafe_math_optimizations)
6749 const REAL_VALUE_TYPE value_truncate =
6750 real_value_truncate (TYPE_MODE (type), *value);
6751 if (real_dconstp (arg, &value_truncate))
6752 return build_real (type, dconst1);
6755 /* Special case, optimize logN(expN(x)) = x. */
6756 if (flag_unsafe_math_optimizations
6757 && ((value == &dconste
6758 && (fcode == BUILT_IN_EXP
6759 || fcode == BUILT_IN_EXPF
6760 || fcode == BUILT_IN_EXPL))
6761 || (value == &dconst2
6762 && (fcode == BUILT_IN_EXP2
6763 || fcode == BUILT_IN_EXP2F
6764 || fcode == BUILT_IN_EXP2L))
6765 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
6766 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6768 /* Optimize logN(func()) for various exponential functions. We
6769 want to determine the value "x" and the power "exponent" in
6770 order to transform logN(x**exponent) into exponent*logN(x). */
6771 if (flag_unsafe_math_optimizations)
6773 tree exponent = 0, x = 0;
6780 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
6781 x = build_real (type,
6782 real_value_truncate (TYPE_MODE (type), dconste));
6783 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6786 case BUILT_IN_EXP2F:
6787 case BUILT_IN_EXP2L:
6788 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
6789 x = build_real (type, dconst2);
6790 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6792 case BUILT_IN_EXP10:
6793 case BUILT_IN_EXP10F:
6794 case BUILT_IN_EXP10L:
6795 case BUILT_IN_POW10:
6796 case BUILT_IN_POW10F:
6797 case BUILT_IN_POW10L:
6798 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
6799 x = build_real (type, dconst10);
6800 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6803 case BUILT_IN_SQRTF:
6804 case BUILT_IN_SQRTL:
6805 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
6806 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6807 exponent = build_real (type, dconsthalf);
6810 case BUILT_IN_CBRTF:
6811 case BUILT_IN_CBRTL:
6812 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
6813 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6814 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
6820 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
6821 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6822 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6828 /* Now perform the optimization. */
6832 arglist = build_tree_list (NULL_TREE, x);
6833 logfn = build_function_call_expr (fndecl, arglist);
6834 return fold (build2 (MULT_EXPR, type, exponent, logfn));
6842 /* Fold a builtin function call to pow, powf, or powl. Return
6843 NULL_TREE if no simplification can be made. */
6845 fold_builtin_pow (tree fndecl, tree arglist, tree type)
6847 enum built_in_function fcode;
6848 tree arg0 = TREE_VALUE (arglist);
6849 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6851 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6854 /* Optimize pow(1.0,y) = 1.0. */
6855 if (real_onep (arg0))
6856 return omit_one_operand (type, build_real (type, dconst1), arg1);
6858 if (TREE_CODE (arg1) == REAL_CST
6859 && ! TREE_CONSTANT_OVERFLOW (arg1))
6861 REAL_VALUE_TYPE cint;
6865 c = TREE_REAL_CST (arg1);
6867 /* Optimize pow(x,0.0) = 1.0. */
6868 if (REAL_VALUES_EQUAL (c, dconst0))
6869 return omit_one_operand (type, build_real (type, dconst1),
6872 /* Optimize pow(x,1.0) = x. */
6873 if (REAL_VALUES_EQUAL (c, dconst1))
6876 /* Optimize pow(x,-1.0) = 1.0/x. */
6877 if (REAL_VALUES_EQUAL (c, dconstm1))
6878 return fold (build2 (RDIV_EXPR, type,
6879 build_real (type, dconst1), arg0));
6881 /* Optimize pow(x,0.5) = sqrt(x). */
6882 if (flag_unsafe_math_optimizations
6883 && REAL_VALUES_EQUAL (c, dconsthalf))
6885 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6887 if (sqrtfn != NULL_TREE)
6889 tree arglist = build_tree_list (NULL_TREE, arg0);
6890 return build_function_call_expr (sqrtfn, arglist);
6894 /* Check for an integer exponent. */
6895 n = real_to_integer (&c);
6896 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
6897 if (real_identical (&c, &cint))
6899 /* Attempt to evaluate pow at compile-time. */
6900 if (TREE_CODE (arg0) == REAL_CST
6901 && ! TREE_CONSTANT_OVERFLOW (arg0))
6906 x = TREE_REAL_CST (arg0);
6907 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
6908 if (flag_unsafe_math_optimizations || !inexact)
6909 return build_real (type, x);
6912 /* Strip sign ops from even integer powers. */
6913 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
6915 tree narg0 = fold_strip_sign_ops (arg0);
6918 arglist = build_tree_list (NULL_TREE, arg1);
6919 arglist = tree_cons (NULL_TREE, narg0, arglist);
6920 return build_function_call_expr (fndecl, arglist);
6926 /* Optimize pow(expN(x),y) = expN(x*y). */
6927 fcode = builtin_mathfn_code (arg0);
6928 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6930 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6931 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
6932 arg = fold (build2 (MULT_EXPR, type, arg, arg1));
6933 arglist = build_tree_list (NULL_TREE, arg);
6934 return build_function_call_expr (expfn, arglist);
6937 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
6938 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
6940 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6941 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
6942 build_real (type, dconsthalf)));
6944 arglist = tree_cons (NULL_TREE, narg0,
6945 build_tree_list (NULL_TREE, narg1));
6946 return build_function_call_expr (fndecl, arglist);
6949 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
6950 if (flag_unsafe_math_optimizations
6951 && (fcode == BUILT_IN_POW
6952 || fcode == BUILT_IN_POWF
6953 || fcode == BUILT_IN_POWL))
6955 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6956 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
6957 tree narg1 = fold (build2 (MULT_EXPR, type, arg01, arg1));
6958 arglist = tree_cons (NULL_TREE, arg00,
6959 build_tree_list (NULL_TREE, narg1));
6960 return build_function_call_expr (fndecl, arglist);
6965 /* Fold a builtin function call to powi, powif, or powil. Return
6966 NULL_TREE if no simplification can be made. */
6968 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
6970 tree arg0 = TREE_VALUE (arglist);
6971 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6973 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
6976 /* Optimize pow(1.0,y) = 1.0. */
6977 if (real_onep (arg0))
6978 return omit_one_operand (type, build_real (type, dconst1), arg1);
6980 if (host_integerp (arg1, 0))
6982 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
6984 /* Evaluate powi at compile-time. */
6985 if (TREE_CODE (arg0) == REAL_CST
6986 && ! TREE_CONSTANT_OVERFLOW (arg0))
6989 x = TREE_REAL_CST (arg0);
6990 real_powi (&x, TYPE_MODE (type), &x, c);
6991 return build_real (type, x);
6994 /* Optimize pow(x,0) = 1.0. */
6996 return omit_one_operand (type, build_real (type, dconst1),
6999 /* Optimize pow(x,1) = x. */
7003 /* Optimize pow(x,-1) = 1.0/x. */
7005 return fold (build2 (RDIV_EXPR, type,
7006 build_real (type, dconst1), arg0));
7012 /* A subroutine of fold_builtin to fold the various exponent
7013 functions. EXP is the CALL_EXPR of a call to a builtin function.
7014 VALUE is the value which will be raised to a power. */
7017 fold_builtin_exponent (tree exp, const REAL_VALUE_TYPE *value)
7019 tree arglist = TREE_OPERAND (exp, 1);
7021 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7023 tree fndecl = get_callee_fndecl (exp);
7024 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7025 tree arg = TREE_VALUE (arglist);
7027 /* Optimize exp*(0.0) = 1.0. */
7028 if (real_zerop (arg))
7029 return build_real (type, dconst1);
7031 /* Optimize expN(1.0) = N. */
7032 if (real_onep (arg))
7034 REAL_VALUE_TYPE cst;
7036 real_convert (&cst, TYPE_MODE (type), value);
7037 return build_real (type, cst);
7040 /* Attempt to evaluate expN(integer) at compile-time. */
7041 if (flag_unsafe_math_optimizations
7042 && TREE_CODE (arg) == REAL_CST
7043 && ! TREE_CONSTANT_OVERFLOW (arg))
7045 REAL_VALUE_TYPE cint;
7049 c = TREE_REAL_CST (arg);
7050 n = real_to_integer (&c);
7051 real_from_integer (&cint, VOIDmode, n,
7053 if (real_identical (&c, &cint))
7057 real_powi (&x, TYPE_MODE (type), value, n);
7058 return build_real (type, x);
7062 /* Optimize expN(logN(x)) = x. */
7063 if (flag_unsafe_math_optimizations)
7065 const enum built_in_function fcode = builtin_mathfn_code (arg);
7067 if ((value == &dconste
7068 && (fcode == BUILT_IN_LOG
7069 || fcode == BUILT_IN_LOGF
7070 || fcode == BUILT_IN_LOGL))
7071 || (value == &dconst2
7072 && (fcode == BUILT_IN_LOG2
7073 || fcode == BUILT_IN_LOG2F
7074 || fcode == BUILT_IN_LOG2L))
7075 || (value == &dconst10
7076 && (fcode == BUILT_IN_LOG10
7077 || fcode == BUILT_IN_LOG10F
7078 || fcode == BUILT_IN_LOG10L)))
7079 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7086 /* Fold function call to builtin memcpy. Return
7087 NULL_TREE if no simplification can be made. */
7090 fold_builtin_memcpy (tree exp)
7092 tree arglist = TREE_OPERAND (exp, 1);
7093 tree dest, src, len;
7095 if (!validate_arglist (arglist,
7096 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7099 dest = TREE_VALUE (arglist);
7100 src = TREE_VALUE (TREE_CHAIN (arglist));
7101 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7103 /* If the LEN parameter is zero, return DEST. */
7104 if (integer_zerop (len))
7105 return omit_one_operand (TREE_TYPE (exp), dest, src);
7107 /* If SRC and DEST are the same (and not volatile), return DEST. */
7108 if (operand_equal_p (src, dest, 0))
7109 return omit_one_operand (TREE_TYPE (exp), dest, len);
7114 /* Fold function call to builtin mempcpy. Return
7115 NULL_TREE if no simplification can be made. */
7118 fold_builtin_mempcpy (tree arglist, tree type, int endp)
7120 if (validate_arglist (arglist,
7121 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7123 tree dest = TREE_VALUE (arglist);
7124 tree src = TREE_VALUE (TREE_CHAIN (arglist));
7125 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7127 /* If the LEN parameter is zero, return DEST. */
7128 if (integer_zerop (len))
7129 return omit_one_operand (type, dest, src);
7131 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
7132 if (operand_equal_p (src, dest, 0))
7135 return omit_one_operand (type, dest, len);
7138 len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
7141 len = fold_convert (TREE_TYPE (dest), len);
7142 len = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
7143 return fold_convert (type, len);
7149 /* Fold function call to builtin memmove. Return
7150 NULL_TREE if no simplification can be made. */
7153 fold_builtin_memmove (tree arglist, tree type)
7155 tree dest, src, len;
7157 if (!validate_arglist (arglist,
7158 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7161 dest = TREE_VALUE (arglist);
7162 src = TREE_VALUE (TREE_CHAIN (arglist));
7163 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7165 /* If the LEN parameter is zero, return DEST. */
7166 if (integer_zerop (len))
7167 return omit_one_operand (type, dest, src);
7169 /* If SRC and DEST are the same (and not volatile), return DEST. */
7170 if (operand_equal_p (src, dest, 0))
7171 return omit_one_operand (type, dest, len);
7176 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
7177 the length of the string to be copied. Return NULL_TREE if no
7178 simplification can be made. */
7181 fold_builtin_strcpy (tree exp, tree len)
7183 tree arglist = TREE_OPERAND (exp, 1);
7186 if (!validate_arglist (arglist,
7187 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7190 dest = TREE_VALUE (arglist);
7191 src = TREE_VALUE (TREE_CHAIN (arglist));
7193 /* If SRC and DEST are the same (and not volatile), return DEST. */
7194 if (operand_equal_p (src, dest, 0))
7195 return fold_convert (TREE_TYPE (exp), dest);
7200 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7206 len = c_strlen (src, 1);
7207 if (! len || TREE_SIDE_EFFECTS (len))
7211 len = size_binop (PLUS_EXPR, len, ssize_int (1));
7212 arglist = build_tree_list (NULL_TREE, len);
7213 arglist = tree_cons (NULL_TREE, src, arglist);
7214 arglist = tree_cons (NULL_TREE, dest, arglist);
7215 return fold_convert (TREE_TYPE (exp),
7216 build_function_call_expr (fn, arglist));
7219 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
7220 the length of the source string. Return NULL_TREE if no simplification
7224 fold_builtin_strncpy (tree exp, tree slen)
7226 tree arglist = TREE_OPERAND (exp, 1);
7227 tree dest, src, len, fn;
7229 if (!validate_arglist (arglist,
7230 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7233 dest = TREE_VALUE (arglist);
7234 src = TREE_VALUE (TREE_CHAIN (arglist));
7235 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7237 /* If the LEN parameter is zero, return DEST. */
7238 if (integer_zerop (len))
7239 return omit_one_operand (TREE_TYPE (exp), dest, src);
7241 /* We can't compare slen with len as constants below if len is not a
7243 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
7247 slen = c_strlen (src, 1);
7249 /* Now, we must be passed a constant src ptr parameter. */
7250 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
7253 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
7255 /* We do not support simplification of this case, though we do
7256 support it when expanding trees into RTL. */
7257 /* FIXME: generate a call to __builtin_memset. */
7258 if (tree_int_cst_lt (slen, len))
7261 /* OK transform into builtin memcpy. */
7262 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7265 return fold_convert (TREE_TYPE (exp),
7266 build_function_call_expr (fn, arglist));
7269 /* Fold function call to builtin memcmp. Return
7270 NULL_TREE if no simplification can be made. */
7273 fold_builtin_memcmp (tree arglist)
7275 tree arg1, arg2, len;
7276 const char *p1, *p2;
7278 if (!validate_arglist (arglist,
7279 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7282 arg1 = TREE_VALUE (arglist);
7283 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7284 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7286 /* If the LEN parameter is zero, return zero. */
7287 if (integer_zerop (len))
7288 return omit_two_operands (integer_type_node, integer_zero_node,
7291 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7292 if (operand_equal_p (arg1, arg2, 0))
7293 return omit_one_operand (integer_type_node, integer_zero_node, len);
7295 p1 = c_getstr (arg1);
7296 p2 = c_getstr (arg2);
7298 /* If all arguments are constant, and the value of len is not greater
7299 than the lengths of arg1 and arg2, evaluate at compile-time. */
7300 if (host_integerp (len, 1) && p1 && p2
7301 && compare_tree_int (len, strlen (p1) + 1) <= 0
7302 && compare_tree_int (len, strlen (p2) + 1) <= 0)
7304 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
7307 return integer_one_node;
7309 return integer_minus_one_node;
7311 return integer_zero_node;
7314 /* If len parameter is one, return an expression corresponding to
7315 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7316 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7318 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7319 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7320 tree ind1 = fold_convert (integer_type_node,
7321 build1 (INDIRECT_REF, cst_uchar_node,
7322 fold_convert (cst_uchar_ptr_node,
7324 tree ind2 = fold_convert (integer_type_node,
7325 build1 (INDIRECT_REF, cst_uchar_node,
7326 fold_convert (cst_uchar_ptr_node,
7328 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7334 /* Fold function call to builtin strcmp. Return
7335 NULL_TREE if no simplification can be made. */
7338 fold_builtin_strcmp (tree arglist)
7341 const char *p1, *p2;
7343 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7346 arg1 = TREE_VALUE (arglist);
7347 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7349 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7350 if (operand_equal_p (arg1, arg2, 0))
7351 return integer_zero_node;
7353 p1 = c_getstr (arg1);
7354 p2 = c_getstr (arg2);
7358 const int i = strcmp (p1, p2);
7360 return integer_minus_one_node;
7362 return integer_one_node;
7364 return integer_zero_node;
7367 /* If the second arg is "", return *(const unsigned char*)arg1. */
7368 if (p2 && *p2 == '\0')
7370 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7371 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7372 return fold_convert (integer_type_node,
7373 build1 (INDIRECT_REF, cst_uchar_node,
7374 fold_convert (cst_uchar_ptr_node,
7378 /* If the first arg is "", return -*(const unsigned char*)arg2. */
7379 if (p1 && *p1 == '\0')
7381 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7382 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7383 tree temp = fold_convert (integer_type_node,
7384 build1 (INDIRECT_REF, cst_uchar_node,
7385 fold_convert (cst_uchar_ptr_node,
7387 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7393 /* Fold function call to builtin strncmp. Return
7394 NULL_TREE if no simplification can be made. */
7397 fold_builtin_strncmp (tree arglist)
7399 tree arg1, arg2, len;
7400 const char *p1, *p2;
7402 if (!validate_arglist (arglist,
7403 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7406 arg1 = TREE_VALUE (arglist);
7407 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7408 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7410 /* If the LEN parameter is zero, return zero. */
7411 if (integer_zerop (len))
7412 return omit_two_operands (integer_type_node, integer_zero_node,
7415 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7416 if (operand_equal_p (arg1, arg2, 0))
7417 return omit_one_operand (integer_type_node, integer_zero_node, len);
7419 p1 = c_getstr (arg1);
7420 p2 = c_getstr (arg2);
7422 if (host_integerp (len, 1) && p1 && p2)
7424 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
7426 return integer_one_node;
7428 return integer_minus_one_node;
7430 return integer_zero_node;
7433 /* If the second arg is "", and the length is greater than zero,
7434 return *(const unsigned char*)arg1. */
7435 if (p2 && *p2 == '\0'
7436 && TREE_CODE (len) == INTEGER_CST
7437 && tree_int_cst_sgn (len) == 1)
7439 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7440 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7441 return fold_convert (integer_type_node,
7442 build1 (INDIRECT_REF, cst_uchar_node,
7443 fold_convert (cst_uchar_ptr_node,
7447 /* If the first arg is "", and the length is greater than zero,
7448 return -*(const unsigned char*)arg2. */
7449 if (p1 && *p1 == '\0'
7450 && TREE_CODE (len) == INTEGER_CST
7451 && tree_int_cst_sgn (len) == 1)
7453 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7454 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7455 tree temp = fold_convert (integer_type_node,
7456 build1 (INDIRECT_REF, cst_uchar_node,
7457 fold_convert (cst_uchar_ptr_node,
7459 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7462 /* If len parameter is one, return an expression corresponding to
7463 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7464 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7466 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7467 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7468 tree ind1 = fold_convert (integer_type_node,
7469 build1 (INDIRECT_REF, cst_uchar_node,
7470 fold_convert (cst_uchar_ptr_node,
7472 tree ind2 = fold_convert (integer_type_node,
7473 build1 (INDIRECT_REF, cst_uchar_node,
7474 fold_convert (cst_uchar_ptr_node,
7476 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7482 /* Fold function call to builtin signbit, signbitf or signbitl. Return
7483 NULL_TREE if no simplification can be made. */
7486 fold_builtin_signbit (tree exp)
7488 tree arglist = TREE_OPERAND (exp, 1);
7491 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7494 arg = TREE_VALUE (arglist);
7496 /* If ARG is a compile-time constant, determine the result. */
7497 if (TREE_CODE (arg) == REAL_CST
7498 && !TREE_CONSTANT_OVERFLOW (arg))
7502 c = TREE_REAL_CST (arg);
7503 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
7504 return fold_convert (TREE_TYPE (exp), temp);
7507 /* If ARG is non-negative, the result is always zero. */
7508 if (tree_expr_nonnegative_p (arg))
7509 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg);
7511 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
7512 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
7513 return fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
7514 build_real (TREE_TYPE (arg), dconst0)));
7519 /* Fold function call to builtin copysign, copysignf or copysignl.
7520 Return NULL_TREE if no simplification can be made. */
7523 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
7525 tree arg1, arg2, tem;
7527 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7530 arg1 = TREE_VALUE (arglist);
7531 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7533 /* copysign(X,X) is X. */
7534 if (operand_equal_p (arg1, arg2, 0))
7535 return fold_convert (type, arg1);
7537 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
7538 if (TREE_CODE (arg1) == REAL_CST
7539 && TREE_CODE (arg2) == REAL_CST
7540 && !TREE_CONSTANT_OVERFLOW (arg1)
7541 && !TREE_CONSTANT_OVERFLOW (arg2))
7543 REAL_VALUE_TYPE c1, c2;
7545 c1 = TREE_REAL_CST (arg1);
7546 c2 = TREE_REAL_CST (arg2);
7547 real_copysign (&c1, &c2);
7548 return build_real (type, c1);
7552 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
7553 Remember to evaluate Y for side-effects. */
7554 if (tree_expr_nonnegative_p (arg2))
7555 return omit_one_operand (type,
7556 fold (build1 (ABS_EXPR, type, arg1)),
7559 /* Strip sign changing operations for the first argument. */
7560 tem = fold_strip_sign_ops (arg1);
7563 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
7564 return build_function_call_expr (fndecl, arglist);
7570 /* Fold a call to builtin isascii. */
7573 fold_builtin_isascii (tree arglist)
7575 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7579 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7580 tree arg = TREE_VALUE (arglist);
7582 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
7583 build_int_cst (NULL_TREE,
7584 ~ (unsigned HOST_WIDE_INT) 0x7f));
7585 arg = fold (build2 (EQ_EXPR, integer_type_node,
7586 arg, integer_zero_node));
7588 if (in_gimple_form && !TREE_CONSTANT (arg))
7595 /* Fold a call to builtin toascii. */
7598 fold_builtin_toascii (tree arglist)
7600 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7604 /* Transform toascii(c) -> (c & 0x7f). */
7605 tree arg = TREE_VALUE (arglist);
7607 return fold (build2 (BIT_AND_EXPR, integer_type_node, arg,
7608 build_int_cst (NULL_TREE, 0x7f)));
7612 /* Fold a call to builtin isdigit. */
7615 fold_builtin_isdigit (tree arglist)
7617 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7621 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7622 /* According to the C standard, isdigit is unaffected by locale.
7623 However, it definitely is affected by the target character set. */
7625 unsigned HOST_WIDE_INT target_digit0
7626 = lang_hooks.to_target_charset ('0');
7628 if (target_digit0 == 0)
7631 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
7632 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
7633 build_int_cst (unsigned_type_node, target_digit0));
7634 arg = build2 (LE_EXPR, integer_type_node, arg,
7635 build_int_cst (unsigned_type_node, 9));
7637 if (in_gimple_form && !TREE_CONSTANT (arg))
7644 /* Fold a call to fabs, fabsf or fabsl. */
7647 fold_builtin_fabs (tree arglist, tree type)
7651 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7654 arg = TREE_VALUE (arglist);
7655 arg = fold_convert (type, arg);
7656 if (TREE_CODE (arg) == REAL_CST)
7657 return fold_abs_const (arg, type);
7658 return fold (build1 (ABS_EXPR, type, arg));
7661 /* Fold a call to abs, labs, llabs or imaxabs. */
7664 fold_builtin_abs (tree arglist, tree type)
7668 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7671 arg = TREE_VALUE (arglist);
7672 arg = fold_convert (type, arg);
7673 if (TREE_CODE (arg) == INTEGER_CST)
7674 return fold_abs_const (arg, type);
7675 return fold (build1 (ABS_EXPR, type, arg));
7678 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
7679 EXP is the CALL_EXPR for the call. */
7682 fold_builtin_classify (tree exp, int builtin_index)
7684 tree fndecl = get_callee_fndecl (exp);
7685 tree arglist = TREE_OPERAND (exp, 1);
7686 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7690 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7692 /* Check that we have exactly one argument. */
7695 error ("too few arguments to function %qs",
7696 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7697 return error_mark_node;
7699 else if (TREE_CHAIN (arglist) != 0)
7701 error ("too many arguments to function %qs",
7702 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7703 return error_mark_node;
7707 error ("non-floating-point argument to function %qs",
7708 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7709 return error_mark_node;
7713 arg = TREE_VALUE (arglist);
7714 switch (builtin_index)
7716 case BUILT_IN_ISINF:
7717 if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7718 return omit_one_operand (type, integer_zero_node, arg);
7720 if (TREE_CODE (arg) == REAL_CST)
7722 r = TREE_REAL_CST (arg);
7723 if (real_isinf (&r))
7724 return real_compare (GT_EXPR, &r, &dconst0)
7725 ? integer_one_node : integer_minus_one_node;
7727 return integer_zero_node;
7732 case BUILT_IN_FINITE:
7733 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg)))
7734 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7735 return omit_one_operand (type, integer_zero_node, arg);
7737 if (TREE_CODE (arg) == REAL_CST)
7739 r = TREE_REAL_CST (arg);
7740 return real_isinf (&r) || real_isnan (&r)
7741 ? integer_zero_node : integer_one_node;
7746 case BUILT_IN_ISNAN:
7747 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg))))
7748 return omit_one_operand (type, integer_zero_node, arg);
7750 if (TREE_CODE (arg) == REAL_CST)
7752 r = TREE_REAL_CST (arg);
7753 return real_isnan (&r) ? integer_one_node : integer_zero_node;
7756 arg = builtin_save_expr (arg);
7757 return fold (build2 (UNORDERED_EXPR, type, arg, arg));
7764 /* Fold a call to an unordered comparison function such as
7765 __builtin_isgreater(). EXP is the CALL_EXPR for the call.
7766 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
7767 the opposite of the desired result. UNORDERED_CODE is used
7768 for modes that can hold NaNs and ORDERED_CODE is used for
7772 fold_builtin_unordered_cmp (tree exp,
7773 enum tree_code unordered_code,
7774 enum tree_code ordered_code)
7776 tree fndecl = get_callee_fndecl (exp);
7777 tree arglist = TREE_OPERAND (exp, 1);
7778 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7779 enum tree_code code;
7782 enum tree_code code0, code1;
7783 tree cmp_type = NULL_TREE;
7785 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7787 /* Check that we have exactly two arguments. */
7788 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
7790 error ("too few arguments to function %qs",
7791 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7792 return error_mark_node;
7794 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
7796 error ("too many arguments to function %qs",
7797 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7798 return error_mark_node;
7802 arg0 = TREE_VALUE (arglist);
7803 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7805 type0 = TREE_TYPE (arg0);
7806 type1 = TREE_TYPE (arg1);
7808 code0 = TREE_CODE (type0);
7809 code1 = TREE_CODE (type1);
7811 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
7812 /* Choose the wider of two real types. */
7813 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
7815 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
7817 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
7821 error ("non-floating-point argument to function %qs",
7822 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7823 return error_mark_node;
7826 arg0 = fold_convert (cmp_type, arg0);
7827 arg1 = fold_convert (cmp_type, arg1);
7829 if (unordered_code == UNORDERED_EXPR)
7831 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7832 return omit_two_operands (type, integer_zero_node, arg0, arg1);
7833 return fold (build2 (UNORDERED_EXPR, type, arg0, arg1));
7836 code = MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
7838 return fold (build1 (TRUTH_NOT_EXPR, type,
7839 fold (build2 (code, type, arg0, arg1))));
7842 /* Fold a call to one of the external complex multiply libcalls. */
7845 fold_builtin_complex_mul (tree type, tree arglist)
7847 tree ar, ai, br, bi;
7849 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE,
7850 REAL_TYPE, VOID_TYPE))
7853 ar = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7854 ai = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7855 br = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7856 bi = TREE_VALUE (arglist);
7858 return fold_complex_mult_parts (type, ar, ai, br, bi);
7861 /* Fold a call to one of the external complex division libcalls. */
7864 fold_builtin_complex_div (tree type, tree arglist)
7866 tree ar, ai, br, bi;
7868 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE,
7869 REAL_TYPE, VOID_TYPE))
7872 ar = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7873 ai = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7874 br = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7875 bi = TREE_VALUE (arglist);
7877 return fold_complex_div_parts (type, ar, ai, br, bi, RDIV_EXPR);
7880 /* Used by constant folding to simplify calls to builtin functions. EXP is
7881 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
7882 result of the function call is ignored. This function returns NULL_TREE
7883 if no simplification was possible. */
7886 fold_builtin_1 (tree exp, bool ignore)
7888 tree fndecl = get_callee_fndecl (exp);
7889 tree arglist = TREE_OPERAND (exp, 1);
7890 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7891 enum built_in_function fcode;
7893 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7894 return targetm.fold_builtin (exp, ignore);
7896 fcode = DECL_FUNCTION_CODE (fndecl);
7899 case BUILT_IN_FPUTS:
7900 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
7902 case BUILT_IN_FPUTS_UNLOCKED:
7903 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
7905 case BUILT_IN_STRSTR:
7906 return fold_builtin_strstr (arglist, type);
7908 case BUILT_IN_STRCAT:
7909 return fold_builtin_strcat (arglist);
7911 case BUILT_IN_STRNCAT:
7912 return fold_builtin_strncat (arglist);
7914 case BUILT_IN_STRSPN:
7915 return fold_builtin_strspn (arglist);
7917 case BUILT_IN_STRCSPN:
7918 return fold_builtin_strcspn (arglist);
7920 case BUILT_IN_STRCHR:
7921 case BUILT_IN_INDEX:
7922 return fold_builtin_strchr (arglist, type);
7924 case BUILT_IN_STRRCHR:
7925 case BUILT_IN_RINDEX:
7926 return fold_builtin_strrchr (arglist, type);
7928 case BUILT_IN_STRCPY:
7929 return fold_builtin_strcpy (exp, NULL_TREE);
7931 case BUILT_IN_STRNCPY:
7932 return fold_builtin_strncpy (exp, NULL_TREE);
7934 case BUILT_IN_STRCMP:
7935 return fold_builtin_strcmp (arglist);
7937 case BUILT_IN_STRNCMP:
7938 return fold_builtin_strncmp (arglist);
7940 case BUILT_IN_STRPBRK:
7941 return fold_builtin_strpbrk (arglist, type);
7944 case BUILT_IN_MEMCMP:
7945 return fold_builtin_memcmp (arglist);
7947 case BUILT_IN_SPRINTF:
7948 return fold_builtin_sprintf (arglist, ignore);
7950 case BUILT_IN_CONSTANT_P:
7954 val = fold_builtin_constant_p (arglist);
7955 /* Gimplification will pull the CALL_EXPR for the builtin out of
7956 an if condition. When not optimizing, we'll not CSE it back.
7957 To avoid link error types of regressions, return false now. */
7958 if (!val && !optimize)
7959 val = integer_zero_node;
7964 case BUILT_IN_EXPECT:
7965 return fold_builtin_expect (arglist);
7967 case BUILT_IN_CLASSIFY_TYPE:
7968 return fold_builtin_classify_type (arglist);
7970 case BUILT_IN_STRLEN:
7971 return fold_builtin_strlen (arglist);
7974 case BUILT_IN_FABSF:
7975 case BUILT_IN_FABSL:
7976 return fold_builtin_fabs (arglist, type);
7980 case BUILT_IN_LLABS:
7981 case BUILT_IN_IMAXABS:
7982 return fold_builtin_abs (arglist, type);
7985 case BUILT_IN_CONJF:
7986 case BUILT_IN_CONJL:
7987 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7988 return fold (build1 (CONJ_EXPR, type, TREE_VALUE (arglist)));
7991 case BUILT_IN_CREAL:
7992 case BUILT_IN_CREALF:
7993 case BUILT_IN_CREALL:
7994 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7995 return non_lvalue (fold (build1 (REALPART_EXPR, type,
7996 TREE_VALUE (arglist))));
7999 case BUILT_IN_CIMAG:
8000 case BUILT_IN_CIMAGF:
8001 case BUILT_IN_CIMAGL:
8002 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8003 return non_lvalue (fold (build1 (IMAGPART_EXPR, type,
8004 TREE_VALUE (arglist))));
8008 case BUILT_IN_CABSF:
8009 case BUILT_IN_CABSL:
8010 return fold_builtin_cabs (arglist, type);
8013 case BUILT_IN_SQRTF:
8014 case BUILT_IN_SQRTL:
8015 return fold_builtin_sqrt (arglist, type);
8018 case BUILT_IN_CBRTF:
8019 case BUILT_IN_CBRTL:
8020 return fold_builtin_cbrt (arglist, type);
8025 return fold_builtin_sin (arglist);
8030 return fold_builtin_cos (arglist, type, fndecl);
8035 return fold_builtin_exponent (exp, &dconste);
8038 case BUILT_IN_EXP2F:
8039 case BUILT_IN_EXP2L:
8040 return fold_builtin_exponent (exp, &dconst2);
8042 case BUILT_IN_EXP10:
8043 case BUILT_IN_EXP10F:
8044 case BUILT_IN_EXP10L:
8045 case BUILT_IN_POW10:
8046 case BUILT_IN_POW10F:
8047 case BUILT_IN_POW10L:
8048 return fold_builtin_exponent (exp, &dconst10);
8053 return fold_builtin_logarithm (exp, &dconste);
8056 case BUILT_IN_LOG2F:
8057 case BUILT_IN_LOG2L:
8058 return fold_builtin_logarithm (exp, &dconst2);
8060 case BUILT_IN_LOG10:
8061 case BUILT_IN_LOG10F:
8062 case BUILT_IN_LOG10L:
8063 return fold_builtin_logarithm (exp, &dconst10);
8068 return fold_builtin_tan (arglist);
8071 case BUILT_IN_ATANF:
8072 case BUILT_IN_ATANL:
8073 return fold_builtin_atan (arglist, type);
8078 return fold_builtin_pow (fndecl, arglist, type);
8081 case BUILT_IN_POWIF:
8082 case BUILT_IN_POWIL:
8083 return fold_builtin_powi (fndecl, arglist, type);
8088 return fold_builtin_inf (type, true);
8090 case BUILT_IN_HUGE_VAL:
8091 case BUILT_IN_HUGE_VALF:
8092 case BUILT_IN_HUGE_VALL:
8093 return fold_builtin_inf (type, false);
8098 return fold_builtin_nan (arglist, type, true);
8101 case BUILT_IN_NANSF:
8102 case BUILT_IN_NANSL:
8103 return fold_builtin_nan (arglist, type, false);
8105 case BUILT_IN_FLOOR:
8106 case BUILT_IN_FLOORF:
8107 case BUILT_IN_FLOORL:
8108 return fold_builtin_floor (exp);
8111 case BUILT_IN_CEILF:
8112 case BUILT_IN_CEILL:
8113 return fold_builtin_ceil (exp);
8115 case BUILT_IN_TRUNC:
8116 case BUILT_IN_TRUNCF:
8117 case BUILT_IN_TRUNCL:
8118 return fold_builtin_trunc (exp);
8120 case BUILT_IN_ROUND:
8121 case BUILT_IN_ROUNDF:
8122 case BUILT_IN_ROUNDL:
8123 return fold_builtin_round (exp);
8125 case BUILT_IN_NEARBYINT:
8126 case BUILT_IN_NEARBYINTF:
8127 case BUILT_IN_NEARBYINTL:
8129 case BUILT_IN_RINTF:
8130 case BUILT_IN_RINTL:
8131 return fold_trunc_transparent_mathfn (exp);
8133 case BUILT_IN_LROUND:
8134 case BUILT_IN_LROUNDF:
8135 case BUILT_IN_LROUNDL:
8136 case BUILT_IN_LLROUND:
8137 case BUILT_IN_LLROUNDF:
8138 case BUILT_IN_LLROUNDL:
8139 return fold_builtin_lround (exp);
8141 case BUILT_IN_LRINT:
8142 case BUILT_IN_LRINTF:
8143 case BUILT_IN_LRINTL:
8144 case BUILT_IN_LLRINT:
8145 case BUILT_IN_LLRINTF:
8146 case BUILT_IN_LLRINTL:
8147 return fold_fixed_mathfn (exp);
8151 case BUILT_IN_FFSLL:
8154 case BUILT_IN_CLZLL:
8157 case BUILT_IN_CTZLL:
8158 case BUILT_IN_POPCOUNT:
8159 case BUILT_IN_POPCOUNTL:
8160 case BUILT_IN_POPCOUNTLL:
8161 case BUILT_IN_PARITY:
8162 case BUILT_IN_PARITYL:
8163 case BUILT_IN_PARITYLL:
8164 return fold_builtin_bitop (exp);
8166 case BUILT_IN_MEMCPY:
8167 return fold_builtin_memcpy (exp);
8169 case BUILT_IN_MEMPCPY:
8170 return fold_builtin_mempcpy (arglist, type, /*endp=*/1);
8172 case BUILT_IN_MEMMOVE:
8173 return fold_builtin_memmove (arglist, type);
8175 case BUILT_IN_SIGNBIT:
8176 case BUILT_IN_SIGNBITF:
8177 case BUILT_IN_SIGNBITL:
8178 return fold_builtin_signbit (exp);
8180 case BUILT_IN_ISASCII:
8181 return fold_builtin_isascii (arglist);
8183 case BUILT_IN_TOASCII:
8184 return fold_builtin_toascii (arglist);
8186 case BUILT_IN_ISDIGIT:
8187 return fold_builtin_isdigit (arglist);
8189 case BUILT_IN_COPYSIGN:
8190 case BUILT_IN_COPYSIGNF:
8191 case BUILT_IN_COPYSIGNL:
8192 return fold_builtin_copysign (fndecl, arglist, type);
8194 case BUILT_IN_FINITE:
8195 case BUILT_IN_FINITEF:
8196 case BUILT_IN_FINITEL:
8197 return fold_builtin_classify (exp, BUILT_IN_FINITE);
8199 case BUILT_IN_ISINF:
8200 case BUILT_IN_ISINFF:
8201 case BUILT_IN_ISINFL:
8202 return fold_builtin_classify (exp, BUILT_IN_ISINF);
8204 case BUILT_IN_ISNAN:
8205 case BUILT_IN_ISNANF:
8206 case BUILT_IN_ISNANL:
8207 return fold_builtin_classify (exp, BUILT_IN_ISNAN);
8209 case BUILT_IN_ISGREATER:
8210 return fold_builtin_unordered_cmp (exp, UNLE_EXPR, LE_EXPR);
8211 case BUILT_IN_ISGREATEREQUAL:
8212 return fold_builtin_unordered_cmp (exp, UNLT_EXPR, LT_EXPR);
8213 case BUILT_IN_ISLESS:
8214 return fold_builtin_unordered_cmp (exp, UNGE_EXPR, GE_EXPR);
8215 case BUILT_IN_ISLESSEQUAL:
8216 return fold_builtin_unordered_cmp (exp, UNGT_EXPR, GT_EXPR);
8217 case BUILT_IN_ISLESSGREATER:
8218 return fold_builtin_unordered_cmp (exp, UNEQ_EXPR, EQ_EXPR);
8219 case BUILT_IN_ISUNORDERED:
8220 return fold_builtin_unordered_cmp (exp, UNORDERED_EXPR, NOP_EXPR);
8222 /* We do the folding for va_start in the expander. */
8223 case BUILT_IN_VA_START:
8227 if (fcode >= BUILT_IN_COMPLEX_MUL_MIN
8228 && fcode <= BUILT_IN_COMPLEX_MUL_MAX)
8229 return fold_builtin_complex_mul (type, arglist);
8230 if (fcode >= BUILT_IN_COMPLEX_DIV_MIN
8231 && fcode <= BUILT_IN_COMPLEX_DIV_MAX)
8232 return fold_builtin_complex_div (type, arglist);
8239 /* A wrapper function for builtin folding that prevents warnings for
8240 "statement without effect" and the like, caused by removing the
8241 call node earlier than the warning is generated. */
8244 fold_builtin (tree exp, bool ignore)
8246 exp = fold_builtin_1 (exp, ignore);
8249 /* ??? Don't clobber shared nodes such as integer_zero_node. */
8250 if (CONSTANT_CLASS_P (exp))
8251 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
8252 TREE_NO_WARNING (exp) = 1;
8258 /* Conveniently construct a function call expression. */
8261 build_function_call_expr (tree fn, tree arglist)
8265 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
8266 call_expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
8267 call_expr, arglist, NULL_TREE);
8268 return fold (call_expr);
8271 /* This function validates the types of a function call argument list
8272 represented as a tree chain of parameters against a specified list
8273 of tree_codes. If the last specifier is a 0, that represents an
8274 ellipses, otherwise the last specifier must be a VOID_TYPE. */
8277 validate_arglist (tree arglist, ...)
8279 enum tree_code code;
8283 va_start (ap, arglist);
8287 code = va_arg (ap, enum tree_code);
8291 /* This signifies an ellipses, any further arguments are all ok. */
8295 /* This signifies an endlink, if no arguments remain, return
8296 true, otherwise return false. */
8300 /* If no parameters remain or the parameter's code does not
8301 match the specified code, return false. Otherwise continue
8302 checking any remaining arguments. */
8305 if (code == POINTER_TYPE)
8307 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
8310 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
8314 arglist = TREE_CHAIN (arglist);
8318 /* We need gotos here since we can only have one VA_CLOSE in a
8326 /* Default target-specific builtin expander that does nothing. */
8329 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
8330 rtx target ATTRIBUTE_UNUSED,
8331 rtx subtarget ATTRIBUTE_UNUSED,
8332 enum machine_mode mode ATTRIBUTE_UNUSED,
8333 int ignore ATTRIBUTE_UNUSED)
8338 /* Returns true is EXP represents data that would potentially reside
8339 in a readonly section. */
8342 readonly_data_expr (tree exp)
8346 if (TREE_CODE (exp) != ADDR_EXPR)
8349 exp = get_base_address (TREE_OPERAND (exp, 0));
8353 /* Make sure we call decl_readonly_section only for trees it
8354 can handle (since it returns true for everything it doesn't
8356 if (TREE_CODE (exp) == STRING_CST
8357 || TREE_CODE (exp) == CONSTRUCTOR
8358 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
8359 return decl_readonly_section (exp, 0);
8364 /* Simplify a call to the strstr builtin.
8366 Return 0 if no simplification was possible, otherwise return the
8367 simplified form of the call as a tree.
8369 The simplified form may be a constant or other expression which
8370 computes the same value, but in a more efficient manner (including
8371 calls to other builtin functions).
8373 The call may contain arguments which need to be evaluated, but
8374 which are not useful to determine the result of the call. In
8375 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8376 COMPOUND_EXPR will be an argument which must be evaluated.
8377 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8378 COMPOUND_EXPR in the chain will contain the tree for the simplified
8379 form of the builtin function call. */
8382 fold_builtin_strstr (tree arglist, tree type)
8384 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8388 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8390 const char *p1, *p2;
8399 const char *r = strstr (p1, p2);
8403 return build_int_cst (TREE_TYPE (s1), 0);
8405 /* Return an offset into the constant string argument. */
8406 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8407 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8408 return fold_convert (type, tem);
8417 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8421 /* New argument list transforming strstr(s1, s2) to
8422 strchr(s1, s2[0]). */
8423 arglist = build_tree_list (NULL_TREE,
8424 build_int_cst (NULL_TREE, p2[0]));
8425 arglist = tree_cons (NULL_TREE, s1, arglist);
8426 return build_function_call_expr (fn, arglist);
8430 /* Simplify a call to the strchr builtin.
8432 Return 0 if no simplification was possible, otherwise return the
8433 simplified form of the call as a tree.
8435 The simplified form may be a constant or other expression which
8436 computes the same value, but in a more efficient manner (including
8437 calls to other builtin functions).
8439 The call may contain arguments which need to be evaluated, but
8440 which are not useful to determine the result of the call. In
8441 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8442 COMPOUND_EXPR will be an argument which must be evaluated.
8443 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8444 COMPOUND_EXPR in the chain will contain the tree for the simplified
8445 form of the builtin function call. */
8448 fold_builtin_strchr (tree arglist, tree type)
8450 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8454 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8457 if (TREE_CODE (s2) != INTEGER_CST)
8467 if (target_char_cast (s2, &c))
8473 return build_int_cst (TREE_TYPE (s1), 0);
8475 /* Return an offset into the constant string argument. */
8476 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8477 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8478 return fold_convert (type, tem);
8484 /* Simplify a call to the strrchr builtin.
8486 Return 0 if no simplification was possible, otherwise return the
8487 simplified form of the call as a tree.
8489 The simplified form may be a constant or other expression which
8490 computes the same value, but in a more efficient manner (including
8491 calls to other builtin functions).
8493 The call may contain arguments which need to be evaluated, but
8494 which are not useful to determine the result of the call. In
8495 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8496 COMPOUND_EXPR will be an argument which must be evaluated.
8497 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8498 COMPOUND_EXPR in the chain will contain the tree for the simplified
8499 form of the builtin function call. */
8502 fold_builtin_strrchr (tree arglist, tree type)
8504 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8508 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8512 if (TREE_CODE (s2) != INTEGER_CST)
8522 if (target_char_cast (s2, &c))
8525 r = strrchr (p1, c);
8528 return build_int_cst (TREE_TYPE (s1), 0);
8530 /* Return an offset into the constant string argument. */
8531 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8532 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8533 return fold_convert (type, tem);
8536 if (! integer_zerop (s2))
8539 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8543 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
8544 return build_function_call_expr (fn, arglist);
8548 /* Simplify a call to the strpbrk builtin.
8550 Return 0 if no simplification was possible, otherwise return the
8551 simplified form of the call as a tree.
8553 The simplified form may be a constant or other expression which
8554 computes the same value, but in a more efficient manner (including
8555 calls to other builtin functions).
8557 The call may contain arguments which need to be evaluated, but
8558 which are not useful to determine the result of the call. In
8559 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8560 COMPOUND_EXPR will be an argument which must be evaluated.
8561 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8562 COMPOUND_EXPR in the chain will contain the tree for the simplified
8563 form of the builtin function call. */
8566 fold_builtin_strpbrk (tree arglist, tree type)
8568 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8572 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8574 const char *p1, *p2;
8583 const char *r = strpbrk (p1, p2);
8587 return build_int_cst (TREE_TYPE (s1), 0);
8589 /* Return an offset into the constant string argument. */
8590 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8591 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8592 return fold_convert (type, tem);
8596 /* strpbrk(x, "") == NULL.
8597 Evaluate and ignore s1 in case it had side-effects. */
8598 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
8601 return 0; /* Really call strpbrk. */
8603 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8607 /* New argument list transforming strpbrk(s1, s2) to
8608 strchr(s1, s2[0]). */
8609 arglist = build_tree_list (NULL_TREE,
8610 build_int_cst (NULL_TREE, p2[0]));
8611 arglist = tree_cons (NULL_TREE, s1, arglist);
8612 return build_function_call_expr (fn, arglist);
8616 /* Simplify a call to the strcat builtin.
8618 Return 0 if no simplification was possible, otherwise return the
8619 simplified form of the call as a tree.
8621 The simplified form may be a constant or other expression which
8622 computes the same value, but in a more efficient manner (including
8623 calls to other builtin functions).
8625 The call may contain arguments which need to be evaluated, but
8626 which are not useful to determine the result of the call. In
8627 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8628 COMPOUND_EXPR will be an argument which must be evaluated.
8629 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8630 COMPOUND_EXPR in the chain will contain the tree for the simplified
8631 form of the builtin function call. */
8634 fold_builtin_strcat (tree arglist)
8636 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8640 tree dst = TREE_VALUE (arglist),
8641 src = TREE_VALUE (TREE_CHAIN (arglist));
8642 const char *p = c_getstr (src);
8644 /* If the string length is zero, return the dst parameter. */
8645 if (p && *p == '\0')
8652 /* Simplify a call to the strncat builtin.
8654 Return 0 if no simplification was possible, otherwise return the
8655 simplified form of the call as a tree.
8657 The simplified form may be a constant or other expression which
8658 computes the same value, but in a more efficient manner (including
8659 calls to other builtin functions).
8661 The call may contain arguments which need to be evaluated, but
8662 which are not useful to determine the result of the call. In
8663 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8664 COMPOUND_EXPR will be an argument which must be evaluated.
8665 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8666 COMPOUND_EXPR in the chain will contain the tree for the simplified
8667 form of the builtin function call. */
8670 fold_builtin_strncat (tree arglist)
8672 if (!validate_arglist (arglist,
8673 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8677 tree dst = TREE_VALUE (arglist);
8678 tree src = TREE_VALUE (TREE_CHAIN (arglist));
8679 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8680 const char *p = c_getstr (src);
8682 /* If the requested length is zero, or the src parameter string
8683 length is zero, return the dst parameter. */
8684 if (integer_zerop (len) || (p && *p == '\0'))
8685 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
8687 /* If the requested len is greater than or equal to the string
8688 length, call strcat. */
8689 if (TREE_CODE (len) == INTEGER_CST && p
8690 && compare_tree_int (len, strlen (p)) >= 0)
8693 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
8694 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
8696 /* If the replacement _DECL isn't initialized, don't do the
8701 return build_function_call_expr (fn, newarglist);
8707 /* Simplify a call to the strspn builtin.
8709 Return 0 if no simplification was possible, otherwise return the
8710 simplified form of the call as a tree.
8712 The simplified form may be a constant or other expression which
8713 computes the same value, but in a more efficient manner (including
8714 calls to other builtin functions).
8716 The call may contain arguments which need to be evaluated, but
8717 which are not useful to determine the result of the call. In
8718 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8719 COMPOUND_EXPR will be an argument which must be evaluated.
8720 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8721 COMPOUND_EXPR in the chain will contain the tree for the simplified
8722 form of the builtin function call. */
8725 fold_builtin_strspn (tree arglist)
8727 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8731 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8732 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8734 /* If both arguments are constants, evaluate at compile-time. */
8737 const size_t r = strspn (p1, p2);
8738 return size_int (r);
8741 /* If either argument is "", return 0. */
8742 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
8743 /* Evaluate and ignore both arguments in case either one has
8745 return omit_two_operands (integer_type_node, integer_zero_node,
8751 /* Simplify a call to the strcspn builtin.
8753 Return 0 if no simplification was possible, otherwise return the
8754 simplified form of the call as a tree.
8756 The simplified form may be a constant or other expression which
8757 computes the same value, but in a more efficient manner (including
8758 calls to other builtin functions).
8760 The call may contain arguments which need to be evaluated, but
8761 which are not useful to determine the result of the call. In
8762 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8763 COMPOUND_EXPR will be an argument which must be evaluated.
8764 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8765 COMPOUND_EXPR in the chain will contain the tree for the simplified
8766 form of the builtin function call. */
8769 fold_builtin_strcspn (tree arglist)
8771 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8775 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8776 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8778 /* If both arguments are constants, evaluate at compile-time. */
8781 const size_t r = strcspn (p1, p2);
8782 return size_int (r);
8785 /* If the first argument is "", return 0. */
8786 if (p1 && *p1 == '\0')
8788 /* Evaluate and ignore argument s2 in case it has
8790 return omit_one_operand (integer_type_node,
8791 integer_zero_node, s2);
8794 /* If the second argument is "", return __builtin_strlen(s1). */
8795 if (p2 && *p2 == '\0')
8797 tree newarglist = build_tree_list (NULL_TREE, s1),
8798 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
8800 /* If the replacement _DECL isn't initialized, don't do the
8805 return build_function_call_expr (fn, newarglist);
8811 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
8812 by the builtin will be ignored. UNLOCKED is true is true if this
8813 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
8814 the known length of the string. Return NULL_TREE if no simplification
8818 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
8821 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
8822 : implicit_built_in_decls[BUILT_IN_FPUTC];
8823 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
8824 : implicit_built_in_decls[BUILT_IN_FWRITE];
8826 /* If the return value is used, or the replacement _DECL isn't
8827 initialized, don't do the transformation. */
8828 if (!ignore || !fn_fputc || !fn_fwrite)
8831 /* Verify the arguments in the original call. */
8832 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8836 len = c_strlen (TREE_VALUE (arglist), 0);
8838 /* Get the length of the string passed to fputs. If the length
8839 can't be determined, punt. */
8841 || TREE_CODE (len) != INTEGER_CST)
8844 switch (compare_tree_int (len, 1))
8846 case -1: /* length is 0, delete the call entirely . */
8847 return omit_one_operand (integer_type_node, integer_zero_node,
8848 TREE_VALUE (TREE_CHAIN (arglist)));
8850 case 0: /* length is 1, call fputc. */
8852 const char *p = c_getstr (TREE_VALUE (arglist));
8856 /* New argument list transforming fputs(string, stream) to
8857 fputc(string[0], stream). */
8858 arglist = build_tree_list (NULL_TREE,
8859 TREE_VALUE (TREE_CHAIN (arglist)));
8860 arglist = tree_cons (NULL_TREE,
8861 build_int_cst (NULL_TREE, p[0]),
8868 case 1: /* length is greater than 1, call fwrite. */
8872 /* If optimizing for size keep fputs. */
8875 string_arg = TREE_VALUE (arglist);
8876 /* New argument list transforming fputs(string, stream) to
8877 fwrite(string, 1, len, stream). */
8878 arglist = build_tree_list (NULL_TREE,
8879 TREE_VALUE (TREE_CHAIN (arglist)));
8880 arglist = tree_cons (NULL_TREE, len, arglist);
8881 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
8882 arglist = tree_cons (NULL_TREE, string_arg, arglist);
8890 /* These optimizations are only performed when the result is ignored,
8891 hence there's no need to cast the result to integer_type_node. */
8892 return build_function_call_expr (fn, arglist);
8895 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
8896 produced. False otherwise. This is done so that we don't output the error
8897 or warning twice or three times. */
8899 fold_builtin_next_arg (tree arglist)
8901 tree fntype = TREE_TYPE (current_function_decl);
8903 if (TYPE_ARG_TYPES (fntype) == 0
8904 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
8907 error ("%<va_start%> used in function with fixed args");
8912 /* Evidently an out of date version of <stdarg.h>; can't validate
8913 va_start's second argument, but can still work as intended. */
8914 warning ("%<__builtin_next_arg%> called without an argument");
8917 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
8918 when we checked the arguments and if needed issued a warning. */
8919 else if (!TREE_CHAIN (arglist)
8920 || !integer_zerop (TREE_VALUE (arglist))
8921 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
8922 || TREE_CHAIN (TREE_CHAIN (arglist)))
8924 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
8925 tree arg = TREE_VALUE (arglist);
8927 if (TREE_CHAIN (arglist))
8929 error ("%<va_start%> used with too many arguments");
8933 /* Strip off all nops for the sake of the comparison. This
8934 is not quite the same as STRIP_NOPS. It does more.
8935 We must also strip off INDIRECT_EXPR for C++ reference
8937 while (TREE_CODE (arg) == NOP_EXPR
8938 || TREE_CODE (arg) == CONVERT_EXPR
8939 || TREE_CODE (arg) == NON_LVALUE_EXPR
8940 || TREE_CODE (arg) == INDIRECT_REF)
8941 arg = TREE_OPERAND (arg, 0);
8942 if (arg != last_parm)
8944 /* FIXME: Sometimes with the tree optimizers we can get the
8945 not the last argument even though the user used the last
8946 argument. We just warn and set the arg to be the last
8947 argument so that we will get wrong-code because of
8949 warning ("second parameter of %<va_start%> not last named argument");
8951 /* We want to verify the second parameter just once before the tree
8952 optimizers are run and then avoid keeping it in the tree,
8953 as otherwise we could warn even for correct code like:
8954 void foo (int i, ...)
8955 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
8956 TREE_VALUE (arglist) = integer_zero_node;
8957 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
8963 /* Simplify a call to the sprintf builtin.
8965 Return 0 if no simplification was possible, otherwise return the
8966 simplified form of the call as a tree. If IGNORED is true, it means that
8967 the caller does not use the returned value of the function. */
8970 fold_builtin_sprintf (tree arglist, int ignored)
8972 tree call, retval, dest, fmt;
8973 const char *fmt_str = NULL;
8975 /* Verify the required arguments in the original call. We deal with two
8976 types of sprintf() calls: 'sprintf (str, fmt)' and
8977 'sprintf (dest, "%s", orig)'. */
8978 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
8979 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
8983 /* Get the destination string and the format specifier. */
8984 dest = TREE_VALUE (arglist);
8985 fmt = TREE_VALUE (TREE_CHAIN (arglist));
8987 /* Check whether the format is a literal string constant. */
8988 fmt_str = c_getstr (fmt);
8989 if (fmt_str == NULL)
8995 /* If the format doesn't contain % args or %%, use strcpy. */
8996 if (strchr (fmt_str, '%') == NULL)
8998 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9003 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
9004 'format' is known to contain no % formats. */
9005 arglist = build_tree_list (NULL_TREE, fmt);
9006 arglist = tree_cons (NULL_TREE, dest, arglist);
9007 call = build_function_call_expr (fn, arglist);
9009 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
9012 /* If the format is "%s", use strcpy if the result isn't used. */
9013 else if (fmt_str && strcmp (fmt_str, "%s") == 0)
9016 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9021 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
9022 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9023 arglist = build_tree_list (NULL_TREE, orig);
9024 arglist = tree_cons (NULL_TREE, dest, arglist);
9027 retval = c_strlen (orig, 1);
9028 if (!retval || TREE_CODE (retval) != INTEGER_CST)
9031 call = build_function_call_expr (fn, arglist);
9037 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
9039 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);