1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004 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"
49 #define CALLED_AS_BUILT_IN(NODE) \
50 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
52 #ifndef PAD_VARARGS_DOWN
53 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
56 /* Define the names of the builtin function types and codes. */
57 const char *const built_in_class_names[4]
58 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
60 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM) #X,
61 const char *const built_in_names[(int) END_BUILTINS] =
63 #include "builtins.def"
67 /* Setup an array of _DECL trees, make sure each element is
68 initialized to NULL_TREE. */
69 tree built_in_decls[(int) END_BUILTINS];
70 /* Declarations used when constructing the builtin implicitly in the compiler.
71 It may be NULL_TREE when this is invalid (for instance runtime is not
72 required to implement the function call in all cases). */
73 tree implicit_built_in_decls[(int) END_BUILTINS];
75 static int get_pointer_alignment (tree, unsigned int);
76 static const char *c_getstr (tree);
77 static rtx c_readstr (const char *, enum machine_mode);
78 static int target_char_cast (tree, char *);
79 static rtx get_memory_rtx (tree);
80 static tree build_string_literal (int, const char *);
81 static int apply_args_size (void);
82 static int apply_result_size (void);
83 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
84 static rtx result_vector (int, rtx);
86 static rtx expand_builtin_setjmp (tree, rtx);
87 static void expand_builtin_update_setjmp_buf (rtx);
88 static void expand_builtin_prefetch (tree);
89 static rtx expand_builtin_apply_args (void);
90 static rtx expand_builtin_apply_args_1 (void);
91 static rtx expand_builtin_apply (rtx, rtx, rtx);
92 static void expand_builtin_return (rtx);
93 static enum type_class type_to_class (tree);
94 static rtx expand_builtin_classify_type (tree);
95 static void expand_errno_check (tree, rtx);
96 static rtx expand_builtin_mathfn (tree, rtx, rtx);
97 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
98 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
99 static rtx expand_builtin_args_info (tree);
100 static rtx expand_builtin_next_arg (tree);
101 static rtx expand_builtin_va_start (tree);
102 static rtx expand_builtin_va_end (tree);
103 static rtx expand_builtin_va_copy (tree);
104 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
105 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
106 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
107 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
108 static rtx expand_builtin_strcat (tree, rtx, enum machine_mode);
109 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
110 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode, int);
114 static rtx expand_builtin_memmove (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_bcopy (tree);
116 static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode);
117 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
118 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
119 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
120 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
122 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
123 static rtx expand_builtin_bzero (tree);
124 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_strstr (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strchr (tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_alloca (tree, rtx);
130 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
131 static rtx expand_builtin_frame_address (tree, tree);
132 static rtx expand_builtin_fputs (tree, rtx, bool);
133 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
134 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
135 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
136 static tree stabilize_va_list (tree, int);
137 static rtx expand_builtin_expect (tree, rtx);
138 static tree fold_builtin_constant_p (tree);
139 static tree fold_builtin_classify_type (tree);
140 static tree fold_builtin_inf (tree, int);
141 static tree fold_builtin_nan (tree, tree, int);
142 static int validate_arglist (tree, ...);
143 static bool integer_valued_real_p (tree);
144 static tree fold_trunc_transparent_mathfn (tree);
145 static bool readonly_data_expr (tree);
146 static rtx expand_builtin_fabs (tree, rtx, rtx);
147 static rtx expand_builtin_cabs (tree, rtx);
148 static rtx expand_builtin_signbit (tree, rtx);
149 static tree fold_builtin_cabs (tree, tree);
150 static tree fold_builtin_trunc (tree);
151 static tree fold_builtin_floor (tree);
152 static tree fold_builtin_ceil (tree);
153 static tree fold_builtin_round (tree);
154 static tree fold_builtin_bitop (tree);
155 static tree fold_builtin_memcpy (tree);
156 static tree fold_builtin_mempcpy (tree);
157 static tree fold_builtin_memmove (tree);
158 static tree fold_builtin_strcpy (tree);
159 static tree fold_builtin_strncpy (tree);
160 static tree fold_builtin_memcmp (tree);
161 static tree fold_builtin_strcmp (tree);
162 static tree fold_builtin_strncmp (tree);
163 static tree fold_builtin_signbit (tree);
165 static tree simplify_builtin_memcmp (tree);
166 static tree simplify_builtin_strcmp (tree);
167 static tree simplify_builtin_strncmp (tree);
168 static tree simplify_builtin_strpbrk (tree);
169 static tree simplify_builtin_strstr (tree);
170 static tree simplify_builtin_strchr (tree);
171 static tree simplify_builtin_strrchr (tree);
172 static tree simplify_builtin_strcat (tree);
173 static tree simplify_builtin_strncat (tree);
174 static tree simplify_builtin_strspn (tree);
175 static tree simplify_builtin_strcspn (tree);
176 static void simplify_builtin_next_arg (tree);
177 static void simplify_builtin_va_start (tree);
178 static tree simplify_builtin_sprintf (tree, int);
181 /* Return the alignment in bits of EXP, a pointer valued expression.
182 But don't return more than MAX_ALIGN no matter what.
183 The alignment returned is, by default, the alignment of the thing that
184 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
186 Otherwise, look at the expression to see if we can do better, i.e., if the
187 expression is actually pointing at an object whose alignment is tighter. */
190 get_pointer_alignment (tree exp, unsigned int max_align)
192 unsigned int align, inner;
194 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
197 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
198 align = MIN (align, max_align);
202 switch (TREE_CODE (exp))
206 case NON_LVALUE_EXPR:
207 exp = TREE_OPERAND (exp, 0);
208 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
211 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
212 align = MIN (inner, max_align);
216 /* If sum of pointer + int, restrict our maximum alignment to that
217 imposed by the integer. If not, we can't do any better than
219 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
222 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
223 & (max_align / BITS_PER_UNIT - 1))
227 exp = TREE_OPERAND (exp, 0);
231 /* See what we are pointing at and look at its alignment. */
232 exp = TREE_OPERAND (exp, 0);
233 if (TREE_CODE (exp) == FUNCTION_DECL)
234 align = FUNCTION_BOUNDARY;
235 else if (DECL_P (exp))
236 align = DECL_ALIGN (exp);
237 #ifdef CONSTANT_ALIGNMENT
238 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c')
239 align = CONSTANT_ALIGNMENT (exp, align);
241 return MIN (align, max_align);
249 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
250 way, because it could contain a zero byte in the middle.
251 TREE_STRING_LENGTH is the size of the character array, not the string.
253 ONLY_VALUE should be nonzero if the result is not going to be emitted
254 into the instruction stream and zero if it is going to be expanded.
255 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
256 is returned, otherwise NULL, since
257 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
258 evaluate the side-effects.
260 The value returned is of type `ssizetype'.
262 Unfortunately, string_constant can't access the values of const char
263 arrays with initializers, so neither can we do so here. */
266 c_strlen (tree src, int only_value)
269 HOST_WIDE_INT offset;
274 if (TREE_CODE (src) == COND_EXPR
275 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
279 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
280 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
281 if (tree_int_cst_equal (len1, len2))
285 if (TREE_CODE (src) == COMPOUND_EXPR
286 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
287 return c_strlen (TREE_OPERAND (src, 1), only_value);
289 src = string_constant (src, &offset_node);
293 max = TREE_STRING_LENGTH (src) - 1;
294 ptr = TREE_STRING_POINTER (src);
296 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
298 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
299 compute the offset to the following null if we don't know where to
300 start searching for it. */
303 for (i = 0; i < max; i++)
307 /* We don't know the starting offset, but we do know that the string
308 has no internal zero bytes. We can assume that the offset falls
309 within the bounds of the string; otherwise, the programmer deserves
310 what he gets. Subtract the offset from the length of the string,
311 and return that. This would perhaps not be valid if we were dealing
312 with named arrays in addition to literal string constants. */
314 return size_diffop (size_int (max), offset_node);
317 /* We have a known offset into the string. Start searching there for
318 a null character if we can represent it as a single HOST_WIDE_INT. */
319 if (offset_node == 0)
321 else if (! host_integerp (offset_node, 0))
324 offset = tree_low_cst (offset_node, 0);
326 /* If the offset is known to be out of bounds, warn, and call strlen at
328 if (offset < 0 || offset > max)
330 warning ("offset outside bounds of constant string");
334 /* Use strlen to search for the first zero byte. Since any strings
335 constructed with build_string will have nulls appended, we win even
336 if we get handed something like (char[4])"abcd".
338 Since OFFSET is our starting index into the string, no further
339 calculation is needed. */
340 return ssize_int (strlen (ptr + offset));
343 /* Return a char pointer for a C string if it is a string constant
344 or sum of string constant and integer constant. */
351 src = string_constant (src, &offset_node);
355 if (offset_node == 0)
356 return TREE_STRING_POINTER (src);
357 else if (!host_integerp (offset_node, 1)
358 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
361 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
364 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
365 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
368 c_readstr (const char *str, enum machine_mode mode)
374 if (GET_MODE_CLASS (mode) != MODE_INT)
379 for (i = 0; i < GET_MODE_SIZE (mode); i++)
382 if (WORDS_BIG_ENDIAN)
383 j = GET_MODE_SIZE (mode) - i - 1;
384 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
385 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
386 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
388 if (j > 2 * HOST_BITS_PER_WIDE_INT)
391 ch = (unsigned char) str[i];
392 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
394 return immed_double_const (c[0], c[1], mode);
397 /* Cast a target constant CST to target CHAR and if that value fits into
398 host char type, return zero and put that value into variable pointed by
402 target_char_cast (tree cst, char *p)
404 unsigned HOST_WIDE_INT val, hostval;
406 if (!host_integerp (cst, 1)
407 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
410 val = tree_low_cst (cst, 1);
411 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
412 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
415 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
416 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
425 /* Similar to save_expr, but assumes that arbitrary code is not executed
426 in between the multiple evaluations. In particular, we assume that a
427 non-addressable local variable will not be modified. */
430 builtin_save_expr (tree exp)
432 if (TREE_ADDRESSABLE (exp) == 0
433 && (TREE_CODE (exp) == PARM_DECL
434 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
437 return save_expr (exp);
440 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
441 times to get the address of either a higher stack frame, or a return
442 address located within it (depending on FNDECL_CODE). */
445 expand_builtin_return_addr (enum built_in_function fndecl_code, int count,
450 /* Some machines need special handling before we can access
451 arbitrary frames. For example, on the sparc, we must first flush
452 all register windows to the stack. */
453 #ifdef SETUP_FRAME_ADDRESSES
455 SETUP_FRAME_ADDRESSES ();
458 /* On the sparc, the return address is not in the frame, it is in a
459 register. There is no way to access it off of the current frame
460 pointer, but it can be accessed off the previous frame pointer by
461 reading the value from the register window save area. */
462 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
463 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
467 /* Scan back COUNT frames to the specified frame. */
468 for (i = 0; i < count; i++)
470 /* Assume the dynamic chain pointer is in the word that the
471 frame address points to, unless otherwise specified. */
472 #ifdef DYNAMIC_CHAIN_ADDRESS
473 tem = DYNAMIC_CHAIN_ADDRESS (tem);
475 tem = memory_address (Pmode, tem);
476 tem = gen_rtx_MEM (Pmode, tem);
477 set_mem_alias_set (tem, get_frame_alias_set ());
478 tem = copy_to_reg (tem);
481 /* For __builtin_frame_address, return what we've got. */
482 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
485 /* For __builtin_return_address, Get the return address from that
487 #ifdef RETURN_ADDR_RTX
488 tem = RETURN_ADDR_RTX (count, tem);
490 tem = memory_address (Pmode,
491 plus_constant (tem, GET_MODE_SIZE (Pmode)));
492 tem = gen_rtx_MEM (Pmode, tem);
493 set_mem_alias_set (tem, get_frame_alias_set ());
498 /* Alias set used for setjmp buffer. */
499 static HOST_WIDE_INT setjmp_alias_set = -1;
501 /* Construct the leading half of a __builtin_setjmp call. Control will
502 return to RECEIVER_LABEL. This is used directly by sjlj exception
506 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
508 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
512 if (setjmp_alias_set == -1)
513 setjmp_alias_set = new_alias_set ();
515 buf_addr = convert_memory_address (Pmode, buf_addr);
517 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
521 /* We store the frame pointer and the address of receiver_label in
522 the buffer and use the rest of it for the stack save area, which
523 is machine-dependent. */
525 mem = gen_rtx_MEM (Pmode, buf_addr);
526 set_mem_alias_set (mem, setjmp_alias_set);
527 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
529 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
530 set_mem_alias_set (mem, setjmp_alias_set);
532 emit_move_insn (validize_mem (mem),
533 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
535 stack_save = gen_rtx_MEM (sa_mode,
536 plus_constant (buf_addr,
537 2 * GET_MODE_SIZE (Pmode)));
538 set_mem_alias_set (stack_save, setjmp_alias_set);
539 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
541 /* If there is further processing to do, do it. */
542 #ifdef HAVE_builtin_setjmp_setup
543 if (HAVE_builtin_setjmp_setup)
544 emit_insn (gen_builtin_setjmp_setup (buf_addr));
547 /* Tell optimize_save_area_alloca that extra work is going to
548 need to go on during alloca. */
549 current_function_calls_setjmp = 1;
551 /* Set this so all the registers get saved in our frame; we need to be
552 able to copy the saved values for any registers from frames we unwind. */
553 current_function_has_nonlocal_label = 1;
556 /* Construct the trailing part of a __builtin_setjmp call.
557 This is used directly by sjlj exception handling code. */
560 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
562 /* Clobber the FP when we get here, so we have to make sure it's
563 marked as used by this function. */
564 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
566 /* Mark the static chain as clobbered here so life information
567 doesn't get messed up for it. */
568 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
570 /* Now put in the code to restore the frame pointer, and argument
571 pointer, if needed. The code below is from expand_end_bindings
572 in stmt.c; see detailed documentation there. */
573 #ifdef HAVE_nonlocal_goto
574 if (! HAVE_nonlocal_goto)
576 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
578 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
579 if (fixed_regs[ARG_POINTER_REGNUM])
581 #ifdef ELIMINABLE_REGS
583 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
585 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
586 if (elim_regs[i].from == ARG_POINTER_REGNUM
587 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
590 if (i == ARRAY_SIZE (elim_regs))
593 /* Now restore our arg pointer from the address at which it
594 was saved in our stack frame. */
595 emit_move_insn (virtual_incoming_args_rtx,
596 copy_to_reg (get_arg_pointer_save_area (cfun)));
601 #ifdef HAVE_builtin_setjmp_receiver
602 if (HAVE_builtin_setjmp_receiver)
603 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
606 #ifdef HAVE_nonlocal_goto_receiver
607 if (HAVE_nonlocal_goto_receiver)
608 emit_insn (gen_nonlocal_goto_receiver ());
613 /* @@@ This is a kludge. Not all machine descriptions define a blockage
614 insn, but we must not allow the code we just generated to be reordered
615 by scheduling. Specifically, the update of the frame pointer must
616 happen immediately, not later. So emit an ASM_INPUT to act as blockage
618 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
621 /* __builtin_setjmp is passed a pointer to an array of five words (not
622 all will be used on all machines). It operates similarly to the C
623 library function of the same name, but is more efficient. Much of
624 the code below (and for longjmp) is copied from the handling of
627 NOTE: This is intended for use by GNAT and the exception handling
628 scheme in the compiler and will only work in the method used by
632 expand_builtin_setjmp (tree arglist, rtx target)
634 rtx buf_addr, next_lab, cont_lab;
636 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
639 if (target == 0 || GET_CODE (target) != REG
640 || REGNO (target) < FIRST_PSEUDO_REGISTER)
641 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
643 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
645 next_lab = gen_label_rtx ();
646 cont_lab = gen_label_rtx ();
648 expand_builtin_setjmp_setup (buf_addr, next_lab);
650 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
651 ensure that pending stack adjustments are flushed. */
652 emit_move_insn (target, const0_rtx);
653 emit_jump (cont_lab);
655 emit_label (next_lab);
657 expand_builtin_setjmp_receiver (next_lab);
659 /* Set TARGET to one. */
660 emit_move_insn (target, const1_rtx);
661 emit_label (cont_lab);
663 /* Tell flow about the strange goings on. Putting `next_lab' on
664 `nonlocal_goto_handler_labels' to indicates that function
665 calls may traverse the arc back to this label. */
667 current_function_has_nonlocal_label = 1;
668 nonlocal_goto_handler_labels
669 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
674 /* __builtin_longjmp is passed a pointer to an array of five words (not
675 all will be used on all machines). It operates similarly to the C
676 library function of the same name, but is more efficient. Much of
677 the code below is copied from the handling of non-local gotos.
679 NOTE: This is intended for use by GNAT and the exception handling
680 scheme in the compiler and will only work in the method used by
684 expand_builtin_longjmp (rtx buf_addr, rtx value)
686 rtx fp, lab, stack, insn, last;
687 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
689 if (setjmp_alias_set == -1)
690 setjmp_alias_set = new_alias_set ();
692 buf_addr = convert_memory_address (Pmode, buf_addr);
694 buf_addr = force_reg (Pmode, buf_addr);
696 /* We used to store value in static_chain_rtx, but that fails if pointers
697 are smaller than integers. We instead require that the user must pass
698 a second argument of 1, because that is what builtin_setjmp will
699 return. This also makes EH slightly more efficient, since we are no
700 longer copying around a value that we don't care about. */
701 if (value != const1_rtx)
704 current_function_calls_longjmp = 1;
706 last = get_last_insn ();
707 #ifdef HAVE_builtin_longjmp
708 if (HAVE_builtin_longjmp)
709 emit_insn (gen_builtin_longjmp (buf_addr));
713 fp = gen_rtx_MEM (Pmode, buf_addr);
714 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
715 GET_MODE_SIZE (Pmode)));
717 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
718 2 * GET_MODE_SIZE (Pmode)));
719 set_mem_alias_set (fp, setjmp_alias_set);
720 set_mem_alias_set (lab, setjmp_alias_set);
721 set_mem_alias_set (stack, setjmp_alias_set);
723 /* Pick up FP, label, and SP from the block and jump. This code is
724 from expand_goto in stmt.c; see there for detailed comments. */
725 #if HAVE_nonlocal_goto
726 if (HAVE_nonlocal_goto)
727 /* We have to pass a value to the nonlocal_goto pattern that will
728 get copied into the static_chain pointer, but it does not matter
729 what that value is, because builtin_setjmp does not use it. */
730 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
734 lab = copy_to_reg (lab);
736 emit_insn (gen_rtx_CLOBBER (VOIDmode,
737 gen_rtx_MEM (BLKmode,
738 gen_rtx_SCRATCH (VOIDmode))));
739 emit_insn (gen_rtx_CLOBBER (VOIDmode,
740 gen_rtx_MEM (BLKmode,
741 hard_frame_pointer_rtx)));
743 emit_move_insn (hard_frame_pointer_rtx, fp);
744 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
746 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
747 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
748 emit_indirect_jump (lab);
752 /* Search backwards and mark the jump insn as a non-local goto.
753 Note that this precludes the use of __builtin_longjmp to a
754 __builtin_setjmp target in the same function. However, we've
755 already cautioned the user that these functions are for
756 internal exception handling use only. */
757 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
761 if (GET_CODE (insn) == JUMP_INSN)
763 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
767 else if (GET_CODE (insn) == CALL_INSN)
772 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
773 and the address of the save area. */
776 expand_builtin_nonlocal_goto (tree arglist)
778 tree t_label, t_save_area;
779 rtx r_label, r_save_area, r_fp, r_sp, insn;
781 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
784 t_label = TREE_VALUE (arglist);
785 arglist = TREE_CHAIN (arglist);
786 t_save_area = TREE_VALUE (arglist);
788 r_label = expand_expr (t_label, NULL_RTX, VOIDmode, 0);
789 r_save_area = expand_expr (t_save_area, NULL_RTX, VOIDmode, 0);
790 r_fp = gen_rtx_MEM (Pmode, r_save_area);
791 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
792 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
794 current_function_has_nonlocal_goto = 1;
796 #if HAVE_nonlocal_goto
797 /* ??? We no longer need to pass the static chain value, afaik. */
798 if (HAVE_nonlocal_goto)
799 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
803 r_label = copy_to_reg (r_label);
805 emit_insn (gen_rtx_CLOBBER (VOIDmode,
806 gen_rtx_MEM (BLKmode,
807 gen_rtx_SCRATCH (VOIDmode))));
809 emit_insn (gen_rtx_CLOBBER (VOIDmode,
810 gen_rtx_MEM (BLKmode,
811 hard_frame_pointer_rtx)));
813 /* Restore frame pointer for containing function.
814 This sets the actual hard register used for the frame pointer
815 to the location of the function's incoming static chain info.
816 The non-local goto handler will then adjust it to contain the
817 proper value and reload the argument pointer, if needed. */
818 emit_move_insn (hard_frame_pointer_rtx, r_fp);
819 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
821 /* USE of hard_frame_pointer_rtx added for consistency;
822 not clear if really needed. */
823 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
824 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
825 emit_indirect_jump (r_label);
828 /* Search backwards to the jump insn and mark it as a
830 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
832 if (GET_CODE (insn) == JUMP_INSN)
834 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
835 const0_rtx, REG_NOTES (insn));
838 else if (GET_CODE (insn) == CALL_INSN)
845 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
846 (not all will be used on all machines) that was passed to __builtin_setjmp.
847 It updates the stack pointer in that block to correspond to the current
851 expand_builtin_update_setjmp_buf (rtx buf_addr)
853 enum machine_mode sa_mode = Pmode;
857 #ifdef HAVE_save_stack_nonlocal
858 if (HAVE_save_stack_nonlocal)
859 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
861 #ifdef STACK_SAVEAREA_MODE
862 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
866 = gen_rtx_MEM (sa_mode,
869 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
873 emit_insn (gen_setjmp ());
876 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
879 /* Expand a call to __builtin_prefetch. For a target that does not support
880 data prefetch, evaluate the memory address argument in case it has side
884 expand_builtin_prefetch (tree arglist)
886 tree arg0, arg1, arg2;
889 if (!validate_arglist (arglist, POINTER_TYPE, 0))
892 arg0 = TREE_VALUE (arglist);
893 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
894 zero (read) and argument 2 (locality) defaults to 3 (high degree of
896 if (TREE_CHAIN (arglist))
898 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
899 if (TREE_CHAIN (TREE_CHAIN (arglist)))
900 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
902 arg2 = build_int_2 (3, 0);
906 arg1 = integer_zero_node;
907 arg2 = build_int_2 (3, 0);
910 /* Argument 0 is an address. */
911 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
913 /* Argument 1 (read/write flag) must be a compile-time constant int. */
914 if (TREE_CODE (arg1) != INTEGER_CST)
916 error ("second arg to `__builtin_prefetch' must be a constant");
917 arg1 = integer_zero_node;
919 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
920 /* Argument 1 must be either zero or one. */
921 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
923 warning ("invalid second arg to __builtin_prefetch; using zero");
927 /* Argument 2 (locality) must be a compile-time constant int. */
928 if (TREE_CODE (arg2) != INTEGER_CST)
930 error ("third arg to `__builtin_prefetch' must be a constant");
931 arg2 = integer_zero_node;
933 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
934 /* Argument 2 must be 0, 1, 2, or 3. */
935 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
937 warning ("invalid third arg to __builtin_prefetch; using zero");
944 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
946 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
947 || (GET_MODE (op0) != Pmode))
949 op0 = convert_memory_address (Pmode, op0);
950 op0 = force_reg (Pmode, op0);
952 emit_insn (gen_prefetch (op0, op1, op2));
956 op0 = protect_from_queue (op0, 0);
957 /* Don't do anything with direct references to volatile memory, but
958 generate code to handle other side effects. */
959 if (GET_CODE (op0) != MEM && side_effects_p (op0))
963 /* Get a MEM rtx for expression EXP which is the address of an operand
964 to be used to be used in a string instruction (cmpstrsi, movstrsi, ..). */
967 get_memory_rtx (tree exp)
969 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM);
972 addr = convert_memory_address (Pmode, addr);
974 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
976 /* Get an expression we can use to find the attributes to assign to MEM.
977 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
978 we can. First remove any nops. */
979 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
980 || TREE_CODE (exp) == NON_LVALUE_EXPR)
981 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
982 exp = TREE_OPERAND (exp, 0);
984 if (TREE_CODE (exp) == ADDR_EXPR)
986 exp = TREE_OPERAND (exp, 0);
987 set_mem_attributes (mem, exp, 0);
989 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
991 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
992 /* memcpy, memset and other builtin stringops can alias with anything. */
993 set_mem_alias_set (mem, 0);
999 /* Built-in functions to perform an untyped call and return. */
1001 /* For each register that may be used for calling a function, this
1002 gives a mode used to copy the register's value. VOIDmode indicates
1003 the register is not used for calling a function. If the machine
1004 has register windows, this gives only the outbound registers.
1005 INCOMING_REGNO gives the corresponding inbound register. */
1006 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1008 /* For each register that may be used for returning values, this gives
1009 a mode used to copy the register's value. VOIDmode indicates the
1010 register is not used for returning values. If the machine has
1011 register windows, this gives only the outbound registers.
1012 INCOMING_REGNO gives the corresponding inbound register. */
1013 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1015 /* For each register that may be used for calling a function, this
1016 gives the offset of that register into the block returned by
1017 __builtin_apply_args. 0 indicates that the register is not
1018 used for calling a function. */
1019 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1021 /* Return the size required for the block returned by __builtin_apply_args,
1022 and initialize apply_args_mode. */
1025 apply_args_size (void)
1027 static int size = -1;
1030 enum machine_mode mode;
1032 /* The values computed by this function never change. */
1035 /* The first value is the incoming arg-pointer. */
1036 size = GET_MODE_SIZE (Pmode);
1038 /* The second value is the structure value address unless this is
1039 passed as an "invisible" first argument. */
1040 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1041 size += GET_MODE_SIZE (Pmode);
1043 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1044 if (FUNCTION_ARG_REGNO_P (regno))
1046 mode = reg_raw_mode[regno];
1048 if (mode == VOIDmode)
1051 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1052 if (size % align != 0)
1053 size = CEIL (size, align) * align;
1054 apply_args_reg_offset[regno] = size;
1055 size += GET_MODE_SIZE (mode);
1056 apply_args_mode[regno] = mode;
1060 apply_args_mode[regno] = VOIDmode;
1061 apply_args_reg_offset[regno] = 0;
1067 /* Return the size required for the block returned by __builtin_apply,
1068 and initialize apply_result_mode. */
1071 apply_result_size (void)
1073 static int size = -1;
1075 enum machine_mode mode;
1077 /* The values computed by this function never change. */
1082 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1083 if (FUNCTION_VALUE_REGNO_P (regno))
1085 mode = reg_raw_mode[regno];
1087 if (mode == VOIDmode)
1090 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1091 if (size % align != 0)
1092 size = CEIL (size, align) * align;
1093 size += GET_MODE_SIZE (mode);
1094 apply_result_mode[regno] = mode;
1097 apply_result_mode[regno] = VOIDmode;
1099 /* Allow targets that use untyped_call and untyped_return to override
1100 the size so that machine-specific information can be stored here. */
1101 #ifdef APPLY_RESULT_SIZE
1102 size = APPLY_RESULT_SIZE;
1108 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1109 /* Create a vector describing the result block RESULT. If SAVEP is true,
1110 the result block is used to save the values; otherwise it is used to
1111 restore the values. */
1114 result_vector (int savep, rtx result)
1116 int regno, size, align, nelts;
1117 enum machine_mode mode;
1119 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1122 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1123 if ((mode = apply_result_mode[regno]) != VOIDmode)
1125 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1126 if (size % align != 0)
1127 size = CEIL (size, align) * align;
1128 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1129 mem = adjust_address (result, mode, size);
1130 savevec[nelts++] = (savep
1131 ? gen_rtx_SET (VOIDmode, mem, reg)
1132 : gen_rtx_SET (VOIDmode, reg, mem));
1133 size += GET_MODE_SIZE (mode);
1135 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1137 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1139 /* Save the state required to perform an untyped call with the same
1140 arguments as were passed to the current function. */
1143 expand_builtin_apply_args_1 (void)
1146 int size, align, regno;
1147 enum machine_mode mode;
1148 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1150 /* Create a block where the arg-pointer, structure value address,
1151 and argument registers can be saved. */
1152 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1154 /* Walk past the arg-pointer and structure value address. */
1155 size = GET_MODE_SIZE (Pmode);
1156 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1157 size += GET_MODE_SIZE (Pmode);
1159 /* Save each register used in calling a function to the block. */
1160 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1161 if ((mode = apply_args_mode[regno]) != VOIDmode)
1163 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1164 if (size % align != 0)
1165 size = CEIL (size, align) * align;
1167 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1169 emit_move_insn (adjust_address (registers, mode, size), tem);
1170 size += GET_MODE_SIZE (mode);
1173 /* Save the arg pointer to the block. */
1174 tem = copy_to_reg (virtual_incoming_args_rtx);
1175 #ifdef STACK_GROWS_DOWNWARD
1176 /* We need the pointer as the caller actually passed them to us, not
1177 as we might have pretended they were passed. Make sure it's a valid
1178 operand, as emit_move_insn isn't expected to handle a PLUS. */
1180 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1183 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1185 size = GET_MODE_SIZE (Pmode);
1187 /* Save the structure value address unless this is passed as an
1188 "invisible" first argument. */
1189 if (struct_incoming_value)
1191 emit_move_insn (adjust_address (registers, Pmode, size),
1192 copy_to_reg (struct_incoming_value));
1193 size += GET_MODE_SIZE (Pmode);
1196 /* Return the address of the block. */
1197 return copy_addr_to_reg (XEXP (registers, 0));
1200 /* __builtin_apply_args returns block of memory allocated on
1201 the stack into which is stored the arg pointer, structure
1202 value address, static chain, and all the registers that might
1203 possibly be used in performing a function call. The code is
1204 moved to the start of the function so the incoming values are
1208 expand_builtin_apply_args (void)
1210 /* Don't do __builtin_apply_args more than once in a function.
1211 Save the result of the first call and reuse it. */
1212 if (apply_args_value != 0)
1213 return apply_args_value;
1215 /* When this function is called, it means that registers must be
1216 saved on entry to this function. So we migrate the
1217 call to the first insn of this function. */
1222 temp = expand_builtin_apply_args_1 ();
1226 apply_args_value = temp;
1228 /* Put the insns after the NOTE that starts the function.
1229 If this is inside a start_sequence, make the outer-level insn
1230 chain current, so the code is placed at the start of the
1232 push_topmost_sequence ();
1233 emit_insn_before (seq, NEXT_INSN (get_insns ()));
1234 pop_topmost_sequence ();
1239 /* Perform an untyped call and save the state required to perform an
1240 untyped return of whatever value was returned by the given function. */
1243 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1245 int size, align, regno;
1246 enum machine_mode mode;
1247 rtx incoming_args, result, reg, dest, src, call_insn;
1248 rtx old_stack_level = 0;
1249 rtx call_fusage = 0;
1250 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1252 arguments = convert_memory_address (Pmode, arguments);
1254 /* Create a block where the return registers can be saved. */
1255 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1257 /* Fetch the arg pointer from the ARGUMENTS block. */
1258 incoming_args = gen_reg_rtx (Pmode);
1259 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1260 #ifndef STACK_GROWS_DOWNWARD
1261 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1262 incoming_args, 0, OPTAB_LIB_WIDEN);
1265 /* Perform postincrements before actually calling the function. */
1268 /* Push a new argument block and copy the arguments. Do not allow
1269 the (potential) memcpy call below to interfere with our stack
1271 do_pending_stack_adjust ();
1274 /* Save the stack with nonlocal if available. */
1275 #ifdef HAVE_save_stack_nonlocal
1276 if (HAVE_save_stack_nonlocal)
1277 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1280 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1282 /* Allocate a block of memory onto the stack and copy the memory
1283 arguments to the outgoing arguments address. */
1284 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1285 dest = virtual_outgoing_args_rtx;
1286 #ifndef STACK_GROWS_DOWNWARD
1287 if (GET_CODE (argsize) == CONST_INT)
1288 dest = plus_constant (dest, -INTVAL (argsize));
1290 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1292 dest = gen_rtx_MEM (BLKmode, dest);
1293 set_mem_align (dest, PARM_BOUNDARY);
1294 src = gen_rtx_MEM (BLKmode, incoming_args);
1295 set_mem_align (src, PARM_BOUNDARY);
1296 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1298 /* Refer to the argument block. */
1300 arguments = gen_rtx_MEM (BLKmode, arguments);
1301 set_mem_align (arguments, PARM_BOUNDARY);
1303 /* Walk past the arg-pointer and structure value address. */
1304 size = GET_MODE_SIZE (Pmode);
1306 size += GET_MODE_SIZE (Pmode);
1308 /* Restore each of the registers previously saved. Make USE insns
1309 for each of these registers for use in making the call. */
1310 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1311 if ((mode = apply_args_mode[regno]) != VOIDmode)
1313 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1314 if (size % align != 0)
1315 size = CEIL (size, align) * align;
1316 reg = gen_rtx_REG (mode, regno);
1317 emit_move_insn (reg, adjust_address (arguments, mode, size));
1318 use_reg (&call_fusage, reg);
1319 size += GET_MODE_SIZE (mode);
1322 /* Restore the structure value address unless this is passed as an
1323 "invisible" first argument. */
1324 size = GET_MODE_SIZE (Pmode);
1327 rtx value = gen_reg_rtx (Pmode);
1328 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1329 emit_move_insn (struct_value, value);
1330 if (GET_CODE (struct_value) == REG)
1331 use_reg (&call_fusage, struct_value);
1332 size += GET_MODE_SIZE (Pmode);
1335 /* All arguments and registers used for the call are set up by now! */
1336 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1338 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1339 and we don't want to load it into a register as an optimization,
1340 because prepare_call_address already did it if it should be done. */
1341 if (GET_CODE (function) != SYMBOL_REF)
1342 function = memory_address (FUNCTION_MODE, function);
1344 /* Generate the actual call instruction and save the return value. */
1345 #ifdef HAVE_untyped_call
1346 if (HAVE_untyped_call)
1347 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1348 result, result_vector (1, result)));
1351 #ifdef HAVE_call_value
1352 if (HAVE_call_value)
1356 /* Locate the unique return register. It is not possible to
1357 express a call that sets more than one return register using
1358 call_value; use untyped_call for that. In fact, untyped_call
1359 only needs to save the return registers in the given block. */
1360 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1361 if ((mode = apply_result_mode[regno]) != VOIDmode)
1364 abort (); /* HAVE_untyped_call required. */
1365 valreg = gen_rtx_REG (mode, regno);
1368 emit_call_insn (GEN_CALL_VALUE (valreg,
1369 gen_rtx_MEM (FUNCTION_MODE, function),
1370 const0_rtx, NULL_RTX, const0_rtx));
1372 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1378 /* Find the CALL insn we just emitted, and attach the register usage
1380 call_insn = last_call_insn ();
1381 add_function_usage_to (call_insn, call_fusage);
1383 /* Restore the stack. */
1384 #ifdef HAVE_save_stack_nonlocal
1385 if (HAVE_save_stack_nonlocal)
1386 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1389 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1393 /* Return the address of the result block. */
1394 result = copy_addr_to_reg (XEXP (result, 0));
1395 return convert_memory_address (ptr_mode, result);
1398 /* Perform an untyped return. */
1401 expand_builtin_return (rtx result)
1403 int size, align, regno;
1404 enum machine_mode mode;
1406 rtx call_fusage = 0;
1408 result = convert_memory_address (Pmode, result);
1410 apply_result_size ();
1411 result = gen_rtx_MEM (BLKmode, result);
1413 #ifdef HAVE_untyped_return
1414 if (HAVE_untyped_return)
1416 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1422 /* Restore the return value and note that each value is used. */
1424 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1425 if ((mode = apply_result_mode[regno]) != VOIDmode)
1427 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1428 if (size % align != 0)
1429 size = CEIL (size, align) * align;
1430 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1431 emit_move_insn (reg, adjust_address (result, mode, size));
1433 push_to_sequence (call_fusage);
1434 emit_insn (gen_rtx_USE (VOIDmode, reg));
1435 call_fusage = get_insns ();
1437 size += GET_MODE_SIZE (mode);
1440 /* Put the USE insns before the return. */
1441 emit_insn (call_fusage);
1443 /* Return whatever values was restored by jumping directly to the end
1445 expand_naked_return ();
1448 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1450 static enum type_class
1451 type_to_class (tree type)
1453 switch (TREE_CODE (type))
1455 case VOID_TYPE: return void_type_class;
1456 case INTEGER_TYPE: return integer_type_class;
1457 case CHAR_TYPE: return char_type_class;
1458 case ENUMERAL_TYPE: return enumeral_type_class;
1459 case BOOLEAN_TYPE: return boolean_type_class;
1460 case POINTER_TYPE: return pointer_type_class;
1461 case REFERENCE_TYPE: return reference_type_class;
1462 case OFFSET_TYPE: return offset_type_class;
1463 case REAL_TYPE: return real_type_class;
1464 case COMPLEX_TYPE: return complex_type_class;
1465 case FUNCTION_TYPE: return function_type_class;
1466 case METHOD_TYPE: return method_type_class;
1467 case RECORD_TYPE: return record_type_class;
1469 case QUAL_UNION_TYPE: return union_type_class;
1470 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1471 ? string_type_class : array_type_class);
1472 case SET_TYPE: return set_type_class;
1473 case FILE_TYPE: return file_type_class;
1474 case LANG_TYPE: return lang_type_class;
1475 default: return no_type_class;
1479 /* Expand a call to __builtin_classify_type with arguments found in
1483 expand_builtin_classify_type (tree arglist)
1486 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1487 return GEN_INT (no_type_class);
1490 /* This helper macro, meant to be used in mathfn_built_in below,
1491 determines which among a set of three builtin math functions is
1492 appropriate for a given type mode. The `F' and `L' cases are
1493 automatically generated from the `double' case. */
1494 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1495 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1496 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1497 fcodel = BUILT_IN_MATHFN##L ; break;
1499 /* Return mathematic function equivalent to FN but operating directly
1500 on TYPE, if available. If we can't do the conversion, return zero. */
1502 mathfn_built_in (tree type, enum built_in_function fn)
1504 enum built_in_function fcode, fcodef, fcodel;
1508 CASE_MATHFN (BUILT_IN_ACOS)
1509 CASE_MATHFN (BUILT_IN_ACOSH)
1510 CASE_MATHFN (BUILT_IN_ASIN)
1511 CASE_MATHFN (BUILT_IN_ASINH)
1512 CASE_MATHFN (BUILT_IN_ATAN)
1513 CASE_MATHFN (BUILT_IN_ATAN2)
1514 CASE_MATHFN (BUILT_IN_ATANH)
1515 CASE_MATHFN (BUILT_IN_CBRT)
1516 CASE_MATHFN (BUILT_IN_CEIL)
1517 CASE_MATHFN (BUILT_IN_COPYSIGN)
1518 CASE_MATHFN (BUILT_IN_COS)
1519 CASE_MATHFN (BUILT_IN_COSH)
1520 CASE_MATHFN (BUILT_IN_DREM)
1521 CASE_MATHFN (BUILT_IN_ERF)
1522 CASE_MATHFN (BUILT_IN_ERFC)
1523 CASE_MATHFN (BUILT_IN_EXP)
1524 CASE_MATHFN (BUILT_IN_EXP10)
1525 CASE_MATHFN (BUILT_IN_EXP2)
1526 CASE_MATHFN (BUILT_IN_EXPM1)
1527 CASE_MATHFN (BUILT_IN_FABS)
1528 CASE_MATHFN (BUILT_IN_FDIM)
1529 CASE_MATHFN (BUILT_IN_FLOOR)
1530 CASE_MATHFN (BUILT_IN_FMA)
1531 CASE_MATHFN (BUILT_IN_FMAX)
1532 CASE_MATHFN (BUILT_IN_FMIN)
1533 CASE_MATHFN (BUILT_IN_FMOD)
1534 CASE_MATHFN (BUILT_IN_FREXP)
1535 CASE_MATHFN (BUILT_IN_GAMMA)
1536 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1537 CASE_MATHFN (BUILT_IN_HYPOT)
1538 CASE_MATHFN (BUILT_IN_ILOGB)
1539 CASE_MATHFN (BUILT_IN_INF)
1540 CASE_MATHFN (BUILT_IN_J0)
1541 CASE_MATHFN (BUILT_IN_J1)
1542 CASE_MATHFN (BUILT_IN_JN)
1543 CASE_MATHFN (BUILT_IN_LDEXP)
1544 CASE_MATHFN (BUILT_IN_LGAMMA)
1545 CASE_MATHFN (BUILT_IN_LLRINT)
1546 CASE_MATHFN (BUILT_IN_LLROUND)
1547 CASE_MATHFN (BUILT_IN_LOG)
1548 CASE_MATHFN (BUILT_IN_LOG10)
1549 CASE_MATHFN (BUILT_IN_LOG1P)
1550 CASE_MATHFN (BUILT_IN_LOG2)
1551 CASE_MATHFN (BUILT_IN_LOGB)
1552 CASE_MATHFN (BUILT_IN_LRINT)
1553 CASE_MATHFN (BUILT_IN_LROUND)
1554 CASE_MATHFN (BUILT_IN_MODF)
1555 CASE_MATHFN (BUILT_IN_NAN)
1556 CASE_MATHFN (BUILT_IN_NANS)
1557 CASE_MATHFN (BUILT_IN_NEARBYINT)
1558 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1559 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1560 CASE_MATHFN (BUILT_IN_POW)
1561 CASE_MATHFN (BUILT_IN_POW10)
1562 CASE_MATHFN (BUILT_IN_REMAINDER)
1563 CASE_MATHFN (BUILT_IN_REMQUO)
1564 CASE_MATHFN (BUILT_IN_RINT)
1565 CASE_MATHFN (BUILT_IN_ROUND)
1566 CASE_MATHFN (BUILT_IN_SCALB)
1567 CASE_MATHFN (BUILT_IN_SCALBLN)
1568 CASE_MATHFN (BUILT_IN_SCALBN)
1569 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1570 CASE_MATHFN (BUILT_IN_SIN)
1571 CASE_MATHFN (BUILT_IN_SINCOS)
1572 CASE_MATHFN (BUILT_IN_SINH)
1573 CASE_MATHFN (BUILT_IN_SQRT)
1574 CASE_MATHFN (BUILT_IN_TAN)
1575 CASE_MATHFN (BUILT_IN_TANH)
1576 CASE_MATHFN (BUILT_IN_TGAMMA)
1577 CASE_MATHFN (BUILT_IN_TRUNC)
1578 CASE_MATHFN (BUILT_IN_Y0)
1579 CASE_MATHFN (BUILT_IN_Y1)
1580 CASE_MATHFN (BUILT_IN_YN)
1586 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1587 return implicit_built_in_decls[fcode];
1588 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1589 return implicit_built_in_decls[fcodef];
1590 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1591 return implicit_built_in_decls[fcodel];
1596 /* If errno must be maintained, expand the RTL to check if the result,
1597 TARGET, of a built-in function call, EXP, is NaN, and if so set
1601 expand_errno_check (tree exp, rtx target)
1603 rtx lab = gen_label_rtx ();
1605 /* Test the result; if it is NaN, set errno=EDOM because
1606 the argument was not in the domain. */
1607 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1611 /* If this built-in doesn't throw an exception, set errno directly. */
1612 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1614 #ifdef GEN_ERRNO_RTX
1615 rtx errno_rtx = GEN_ERRNO_RTX;
1618 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1620 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1626 /* We can't set errno=EDOM directly; let the library call do it.
1627 Pop the arguments right away in case the call gets deleted. */
1629 expand_call (exp, target, 0);
1635 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1636 Return 0 if a normal call should be emitted rather than expanding the
1637 function in-line. EXP is the expression that is a call to the builtin
1638 function; if convenient, the result should be placed in TARGET.
1639 SUBTARGET may be used as the target for computing one of EXP's operands. */
1642 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1644 optab builtin_optab;
1645 rtx op0, insns, before_call;
1646 tree fndecl = get_callee_fndecl (exp);
1647 tree arglist = TREE_OPERAND (exp, 1);
1648 enum machine_mode mode;
1649 bool errno_set = false;
1652 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1655 arg = TREE_VALUE (arglist);
1657 switch (DECL_FUNCTION_CODE (fndecl))
1660 case BUILT_IN_SQRTF:
1661 case BUILT_IN_SQRTL:
1662 errno_set = ! tree_expr_nonnegative_p (arg);
1663 builtin_optab = sqrt_optab;
1668 errno_set = true; builtin_optab = exp_optab; break;
1669 case BUILT_IN_EXP10:
1670 case BUILT_IN_EXP10F:
1671 case BUILT_IN_EXP10L:
1672 case BUILT_IN_POW10:
1673 case BUILT_IN_POW10F:
1674 case BUILT_IN_POW10L:
1675 errno_set = true; builtin_optab = exp10_optab; break;
1677 case BUILT_IN_EXP2F:
1678 case BUILT_IN_EXP2L:
1679 errno_set = true; builtin_optab = exp2_optab; break;
1680 case BUILT_IN_EXPM1:
1681 case BUILT_IN_EXPM1F:
1682 case BUILT_IN_EXPM1L:
1683 errno_set = true; builtin_optab = expm1_optab; break;
1685 case BUILT_IN_LOGBF:
1686 case BUILT_IN_LOGBL:
1687 errno_set = true; builtin_optab = logb_optab; break;
1688 case BUILT_IN_ILOGB:
1689 case BUILT_IN_ILOGBF:
1690 case BUILT_IN_ILOGBL:
1691 errno_set = true; builtin_optab = ilogb_optab; break;
1695 errno_set = true; builtin_optab = log_optab; break;
1696 case BUILT_IN_LOG10:
1697 case BUILT_IN_LOG10F:
1698 case BUILT_IN_LOG10L:
1699 errno_set = true; builtin_optab = log10_optab; break;
1701 case BUILT_IN_LOG2F:
1702 case BUILT_IN_LOG2L:
1703 errno_set = true; builtin_optab = log2_optab; break;
1704 case BUILT_IN_LOG1P:
1705 case BUILT_IN_LOG1PF:
1706 case BUILT_IN_LOG1PL:
1707 errno_set = true; builtin_optab = log1p_optab; break;
1709 case BUILT_IN_ASINF:
1710 case BUILT_IN_ASINL:
1711 builtin_optab = asin_optab; break;
1713 case BUILT_IN_ACOSF:
1714 case BUILT_IN_ACOSL:
1715 builtin_optab = acos_optab; break;
1719 builtin_optab = tan_optab; break;
1721 case BUILT_IN_ATANF:
1722 case BUILT_IN_ATANL:
1723 builtin_optab = atan_optab; break;
1724 case BUILT_IN_FLOOR:
1725 case BUILT_IN_FLOORF:
1726 case BUILT_IN_FLOORL:
1727 builtin_optab = floor_optab; break;
1729 case BUILT_IN_CEILF:
1730 case BUILT_IN_CEILL:
1731 builtin_optab = ceil_optab; break;
1732 case BUILT_IN_TRUNC:
1733 case BUILT_IN_TRUNCF:
1734 case BUILT_IN_TRUNCL:
1735 builtin_optab = btrunc_optab; break;
1736 case BUILT_IN_ROUND:
1737 case BUILT_IN_ROUNDF:
1738 case BUILT_IN_ROUNDL:
1739 builtin_optab = round_optab; break;
1740 case BUILT_IN_NEARBYINT:
1741 case BUILT_IN_NEARBYINTF:
1742 case BUILT_IN_NEARBYINTL:
1743 builtin_optab = nearbyint_optab; break;
1748 /* Make a suitable register to place result in. */
1749 mode = TYPE_MODE (TREE_TYPE (exp));
1751 if (! flag_errno_math || ! HONOR_NANS (mode))
1754 /* Before working hard, check whether the instruction is available. */
1755 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1757 target = gen_reg_rtx (mode);
1759 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1760 need to expand the argument again. This way, we will not perform
1761 side-effects more the once. */
1762 narg = builtin_save_expr (arg);
1765 arglist = build_tree_list (NULL_TREE, arg);
1766 exp = build_function_call_expr (fndecl, arglist);
1769 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1774 /* Compute into TARGET.
1775 Set TARGET to wherever the result comes back. */
1776 target = expand_unop (mode, builtin_optab, op0, target, 0);
1781 expand_errno_check (exp, target);
1783 /* Output the entire sequence. */
1784 insns = get_insns ();
1790 /* If we were unable to expand via the builtin, stop the sequence
1791 (without outputting the insns) and call to the library function
1792 with the stabilized argument list. */
1796 before_call = get_last_insn ();
1798 target = expand_call (exp, target, target == const0_rtx);
1800 /* If this is a sqrt operation and we don't care about errno, try to
1801 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1802 This allows the semantics of the libcall to be visible to the RTL
1804 if (builtin_optab == sqrt_optab && !errno_set)
1806 /* Search backwards through the insns emitted by expand_call looking
1807 for the instruction with the REG_RETVAL note. */
1808 rtx last = get_last_insn ();
1809 while (last != before_call)
1811 if (find_reg_note (last, REG_RETVAL, NULL))
1813 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1814 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1815 two elements, i.e. symbol_ref(sqrt) and the operand. */
1817 && GET_CODE (note) == EXPR_LIST
1818 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1819 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1820 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1822 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1823 /* Check operand is a register with expected mode. */
1825 && GET_CODE (operand) == REG
1826 && GET_MODE (operand) == mode)
1828 /* Replace the REG_EQUAL note with a SQRT rtx. */
1829 rtx equiv = gen_rtx_SQRT (mode, operand);
1830 set_unique_reg_note (last, REG_EQUAL, equiv);
1835 last = PREV_INSN (last);
1842 /* Expand a call to the builtin binary math functions (pow and atan2).
1843 Return 0 if a normal call should be emitted rather than expanding the
1844 function in-line. EXP is the expression that is a call to the builtin
1845 function; if convenient, the result should be placed in TARGET.
1846 SUBTARGET may be used as the target for computing one of EXP's
1850 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1852 optab builtin_optab;
1853 rtx op0, op1, insns;
1854 tree fndecl = get_callee_fndecl (exp);
1855 tree arglist = TREE_OPERAND (exp, 1);
1856 tree arg0, arg1, temp, narg;
1857 enum machine_mode mode;
1858 bool errno_set = true;
1861 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
1864 arg0 = TREE_VALUE (arglist);
1865 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1867 switch (DECL_FUNCTION_CODE (fndecl))
1872 builtin_optab = pow_optab; break;
1873 case BUILT_IN_ATAN2:
1874 case BUILT_IN_ATAN2F:
1875 case BUILT_IN_ATAN2L:
1876 builtin_optab = atan2_optab; break;
1878 case BUILT_IN_FMODF:
1879 case BUILT_IN_FMODL:
1880 builtin_optab = fmod_optab; break;
1882 case BUILT_IN_DREMF:
1883 case BUILT_IN_DREML:
1884 builtin_optab = drem_optab; break;
1889 /* Make a suitable register to place result in. */
1890 mode = TYPE_MODE (TREE_TYPE (exp));
1892 /* Before working hard, check whether the instruction is available. */
1893 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1896 target = gen_reg_rtx (mode);
1898 if (! flag_errno_math || ! HONOR_NANS (mode))
1901 /* Always stabilize the argument list. */
1902 narg = builtin_save_expr (arg1);
1905 temp = build_tree_list (NULL_TREE, narg);
1909 temp = TREE_CHAIN (arglist);
1911 narg = builtin_save_expr (arg0);
1914 arglist = tree_cons (NULL_TREE, narg, temp);
1918 arglist = tree_cons (NULL_TREE, arg0, temp);
1921 exp = build_function_call_expr (fndecl, arglist);
1923 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1924 op1 = expand_expr (arg1, 0, VOIDmode, 0);
1929 /* Compute into TARGET.
1930 Set TARGET to wherever the result comes back. */
1931 target = expand_binop (mode, builtin_optab, op0, op1,
1932 target, 0, OPTAB_DIRECT);
1934 /* If we were unable to expand via the builtin, stop the sequence
1935 (without outputting the insns) and call to the library function
1936 with the stabilized argument list. */
1940 return expand_call (exp, target, target == const0_rtx);
1944 expand_errno_check (exp, target);
1946 /* Output the entire sequence. */
1947 insns = get_insns ();
1954 /* Expand a call to the builtin sin and cos math functions.
1955 Return 0 if a normal call should be emitted rather than expanding the
1956 function in-line. EXP is the expression that is a call to the builtin
1957 function; if convenient, the result should be placed in TARGET.
1958 SUBTARGET may be used as the target for computing one of EXP's
1962 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
1964 optab builtin_optab;
1965 rtx op0, insns, before_call;
1966 tree fndecl = get_callee_fndecl (exp);
1967 tree arglist = TREE_OPERAND (exp, 1);
1968 enum machine_mode mode;
1969 bool errno_set = false;
1972 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1975 arg = TREE_VALUE (arglist);
1977 switch (DECL_FUNCTION_CODE (fndecl))
1985 builtin_optab = sincos_optab; break;
1990 /* Make a suitable register to place result in. */
1991 mode = TYPE_MODE (TREE_TYPE (exp));
1993 if (! flag_errno_math || ! HONOR_NANS (mode))
1996 /* Check if sincos insn is available, otherwise fallback
1997 to sin or cos insn. */
1998 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
1999 switch (DECL_FUNCTION_CODE (fndecl))
2004 builtin_optab = sin_optab; break;
2008 builtin_optab = cos_optab; break;
2014 /* Before working hard, check whether the instruction is available. */
2015 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2017 target = gen_reg_rtx (mode);
2019 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2020 need to expand the argument again. This way, we will not perform
2021 side-effects more the once. */
2022 narg = save_expr (arg);
2025 arglist = build_tree_list (NULL_TREE, arg);
2026 exp = build_function_call_expr (fndecl, arglist);
2029 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2034 /* Compute into TARGET.
2035 Set TARGET to wherever the result comes back. */
2036 if (builtin_optab == sincos_optab)
2038 switch (DECL_FUNCTION_CODE (fndecl))
2043 if (!expand_twoval_unop (builtin_optab, op0, 0, target, 0))
2049 if (!expand_twoval_unop (builtin_optab, op0, target, 0, 0))
2058 target = expand_unop (mode, builtin_optab, op0, target, 0);
2064 expand_errno_check (exp, target);
2066 /* Output the entire sequence. */
2067 insns = get_insns ();
2073 /* If we were unable to expand via the builtin, stop the sequence
2074 (without outputting the insns) and call to the library function
2075 with the stabilized argument list. */
2079 before_call = get_last_insn ();
2081 target = expand_call (exp, target, target == const0_rtx);
2086 /* To evaluate powi(x,n), the floating point value x raised to the
2087 constant integer exponent n, we use a hybrid algorithm that
2088 combines the "window method" with look-up tables. For an
2089 introduction to exponentiation algorithms and "addition chains",
2090 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2091 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2092 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2093 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2095 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2096 multiplications to inline before calling the system library's pow
2097 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2098 so this default never requires calling pow, powf or powl. */
2100 #ifndef POWI_MAX_MULTS
2101 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2104 /* The size of the "optimal power tree" lookup table. All
2105 exponents less than this value are simply looked up in the
2106 powi_table below. This threshold is also used to size the
2107 cache of pseudo registers that hold intermediate results. */
2108 #define POWI_TABLE_SIZE 256
2110 /* The size, in bits of the window, used in the "window method"
2111 exponentiation algorithm. This is equivalent to a radix of
2112 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2113 #define POWI_WINDOW_SIZE 3
2115 /* The following table is an efficient representation of an
2116 "optimal power tree". For each value, i, the corresponding
2117 value, j, in the table states than an optimal evaluation
2118 sequence for calculating pow(x,i) can be found by evaluating
2119 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2120 100 integers is given in Knuth's "Seminumerical algorithms". */
2122 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2124 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2125 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2126 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2127 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2128 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2129 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2130 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2131 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2132 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2133 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2134 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2135 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2136 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2137 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2138 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2139 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2140 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2141 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2142 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2143 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2144 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2145 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2146 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2147 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2148 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2149 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2150 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2151 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2152 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2153 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2154 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2155 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2159 /* Return the number of multiplications required to calculate
2160 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2161 subroutine of powi_cost. CACHE is an array indicating
2162 which exponents have already been calculated. */
2165 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2167 /* If we've already calculated this exponent, then this evaluation
2168 doesn't require any additional multiplications. */
2173 return powi_lookup_cost (n - powi_table[n], cache)
2174 + powi_lookup_cost (powi_table[n], cache) + 1;
2177 /* Return the number of multiplications required to calculate
2178 powi(x,n) for an arbitrary x, given the exponent N. This
2179 function needs to be kept in sync with expand_powi below. */
2182 powi_cost (HOST_WIDE_INT n)
2184 bool cache[POWI_TABLE_SIZE];
2185 unsigned HOST_WIDE_INT digit;
2186 unsigned HOST_WIDE_INT val;
2192 /* Ignore the reciprocal when calculating the cost. */
2193 val = (n < 0) ? -n : n;
2195 /* Initialize the exponent cache. */
2196 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2201 while (val >= POWI_TABLE_SIZE)
2205 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2206 result += powi_lookup_cost (digit, cache)
2207 + POWI_WINDOW_SIZE + 1;
2208 val >>= POWI_WINDOW_SIZE;
2217 return result + powi_lookup_cost (val, cache);
2220 /* Recursive subroutine of expand_powi. This function takes the array,
2221 CACHE, of already calculated exponents and an exponent N and returns
2222 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2225 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2227 unsigned HOST_WIDE_INT digit;
2231 if (n < POWI_TABLE_SIZE)
2236 target = gen_reg_rtx (mode);
2239 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2240 op1 = expand_powi_1 (mode, powi_table[n], cache);
2244 target = gen_reg_rtx (mode);
2245 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2246 op0 = expand_powi_1 (mode, n - digit, cache);
2247 op1 = expand_powi_1 (mode, digit, cache);
2251 target = gen_reg_rtx (mode);
2252 op0 = expand_powi_1 (mode, n >> 1, cache);
2256 result = expand_mult (mode, op0, op1, target, 0);
2257 if (result != target)
2258 emit_move_insn (target, result);
2262 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2263 floating point operand in mode MODE, and N is the exponent. This
2264 function needs to be kept in sync with powi_cost above. */
2267 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2269 unsigned HOST_WIDE_INT val;
2270 rtx cache[POWI_TABLE_SIZE];
2274 return CONST1_RTX (mode);
2276 val = (n < 0) ? -n : n;
2278 memset (cache, 0, sizeof (cache));
2281 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2283 /* If the original exponent was negative, reciprocate the result. */
2285 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2286 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2291 /* Expand a call to the pow built-in mathematical function. Return 0 if
2292 a normal call should be emitted rather than expanding the function
2293 in-line. EXP is the expression that is a call to the builtin
2294 function; if convenient, the result should be placed in TARGET. */
2297 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2299 tree arglist = TREE_OPERAND (exp, 1);
2302 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2305 arg0 = TREE_VALUE (arglist);
2306 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2308 if (TREE_CODE (arg1) == REAL_CST
2309 && ! TREE_CONSTANT_OVERFLOW (arg1))
2311 REAL_VALUE_TYPE cint;
2315 c = TREE_REAL_CST (arg1);
2316 n = real_to_integer (&c);
2317 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2318 if (real_identical (&c, &cint))
2320 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2321 Otherwise, check the number of multiplications required.
2322 Note that pow never sets errno for an integer exponent. */
2323 if ((n >= -1 && n <= 2)
2324 || (flag_unsafe_math_optimizations
2326 && powi_cost (n) <= POWI_MAX_MULTS))
2328 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2329 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2330 op = force_reg (mode, op);
2331 return expand_powi (op, mode, n);
2336 if (! flag_unsafe_math_optimizations)
2338 return expand_builtin_mathfn_2 (exp, target, subtarget);
2341 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2342 if we failed the caller should emit a normal call, otherwise
2343 try to get the result in TARGET, if convenient. */
2346 expand_builtin_strlen (tree arglist, rtx target,
2347 enum machine_mode target_mode)
2349 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2354 tree len, src = TREE_VALUE (arglist);
2355 rtx result, src_reg, char_rtx, before_strlen;
2356 enum machine_mode insn_mode = target_mode, char_mode;
2357 enum insn_code icode = CODE_FOR_nothing;
2360 /* If the length can be computed at compile-time, return it. */
2361 len = c_strlen (src, 0);
2363 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2365 /* If the length can be computed at compile-time and is constant
2366 integer, but there are side-effects in src, evaluate
2367 src for side-effects, then return len.
2368 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2369 can be optimized into: i++; x = 3; */
2370 len = c_strlen (src, 1);
2371 if (len && TREE_CODE (len) == INTEGER_CST)
2373 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2374 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2377 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2379 /* If SRC is not a pointer type, don't do this operation inline. */
2383 /* Bail out if we can't compute strlen in the right mode. */
2384 while (insn_mode != VOIDmode)
2386 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2387 if (icode != CODE_FOR_nothing)
2390 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2392 if (insn_mode == VOIDmode)
2395 /* Make a place to write the result of the instruction. */
2398 && GET_CODE (result) == REG
2399 && GET_MODE (result) == insn_mode
2400 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2401 result = gen_reg_rtx (insn_mode);
2403 /* Make a place to hold the source address. We will not expand
2404 the actual source until we are sure that the expansion will
2405 not fail -- there are trees that cannot be expanded twice. */
2406 src_reg = gen_reg_rtx (Pmode);
2408 /* Mark the beginning of the strlen sequence so we can emit the
2409 source operand later. */
2410 before_strlen = get_last_insn ();
2412 char_rtx = const0_rtx;
2413 char_mode = insn_data[(int) icode].operand[2].mode;
2414 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2416 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2418 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2419 char_rtx, GEN_INT (align));
2424 /* Now that we are assured of success, expand the source. */
2426 pat = memory_address (BLKmode,
2427 expand_expr (src, src_reg, ptr_mode, EXPAND_SUM));
2429 emit_move_insn (src_reg, pat);
2434 emit_insn_after (pat, before_strlen);
2436 emit_insn_before (pat, get_insns ());
2438 /* Return the value in the proper mode for this function. */
2439 if (GET_MODE (result) == target_mode)
2441 else if (target != 0)
2442 convert_move (target, result, 0);
2444 target = convert_to_mode (target_mode, result, 0);
2450 /* Expand a call to the strstr builtin. Return 0 if we failed the
2451 caller should emit a normal call, otherwise try to get the result
2452 in TARGET, if convenient (and in mode MODE if that's convenient). */
2455 expand_builtin_strstr (tree arglist, rtx target, enum machine_mode mode)
2457 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2461 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2463 const char *p1, *p2;
2472 const char *r = strstr (p1, p2);
2477 /* Return an offset into the constant string argument. */
2478 tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (s1), s1,
2479 fold_convert (TREE_TYPE (s1),
2480 ssize_int (r - p1))));
2481 return expand_expr (tmp, target, mode, EXPAND_NORMAL);
2485 return expand_expr (s1, target, mode, EXPAND_NORMAL);
2490 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2494 /* New argument list transforming strstr(s1, s2) to
2495 strchr(s1, s2[0]). */
2497 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
2498 arglist = tree_cons (NULL_TREE, s1, arglist);
2499 return expand_expr (build_function_call_expr (fn, arglist),
2500 target, mode, EXPAND_NORMAL);
2504 /* Expand a call to the strchr builtin. Return 0 if we failed the
2505 caller should emit a normal call, otherwise try to get the result
2506 in TARGET, if convenient (and in mode MODE if that's convenient). */
2509 expand_builtin_strchr (tree arglist, rtx target, enum machine_mode mode)
2511 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2515 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2518 if (TREE_CODE (s2) != INTEGER_CST)
2528 if (target_char_cast (s2, &c))
2536 /* Return an offset into the constant string argument. */
2537 tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (s1), s1,
2538 fold_convert (TREE_TYPE (s1),
2539 ssize_int (r - p1))));
2540 return expand_expr (tmp, target, mode, EXPAND_NORMAL);
2543 /* FIXME: Should use here strchrM optab so that ports can optimize
2549 /* Expand a call to the strrchr 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_strrchr (tree arglist, rtx target, enum machine_mode mode)
2556 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2560 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2564 if (TREE_CODE (s2) != INTEGER_CST)
2573 if (target_char_cast (s2, &c))
2576 r = strrchr (p1, c);
2581 /* Return an offset into the constant string argument. */
2582 tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (s1), s1,
2583 fold_convert (TREE_TYPE (s1),
2584 ssize_int (r - p1))));
2585 return expand_expr (tmp, target, mode, EXPAND_NORMAL);
2588 if (! integer_zerop (s2))
2591 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2595 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
2596 return expand_expr (build_function_call_expr (fn, arglist),
2597 target, mode, EXPAND_NORMAL);
2601 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2602 caller should emit a normal call, otherwise try to get the result
2603 in TARGET, if convenient (and in mode MODE if that's convenient). */
2606 expand_builtin_strpbrk (tree arglist, rtx target, enum machine_mode mode)
2608 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2612 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2614 const char *p1, *p2;
2623 const char *r = strpbrk (p1, p2);
2628 /* Return an offset into the constant string argument. */
2629 tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (s1), s1,
2630 fold_convert (TREE_TYPE (s1),
2631 ssize_int (r - p1))));
2632 return expand_expr (tmp, target, mode, EXPAND_NORMAL);
2637 /* strpbrk(x, "") == NULL.
2638 Evaluate and ignore the arguments in case they had
2640 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2645 return 0; /* Really call strpbrk. */
2647 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2651 /* New argument list transforming strpbrk(s1, s2) to
2652 strchr(s1, s2[0]). */
2654 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
2655 arglist = tree_cons (NULL_TREE, s1, arglist);
2656 return expand_expr (build_function_call_expr (fn, arglist),
2657 target, mode, EXPAND_NORMAL);
2661 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2662 bytes from constant string DATA + OFFSET and return it as target
2666 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2667 enum machine_mode mode)
2669 const char *str = (const char *) data;
2672 || ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2673 > strlen (str) + 1))
2674 abort (); /* Attempt to read past the end of constant string. */
2676 return c_readstr (str + offset, mode);
2679 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2680 Return 0 if we failed, the caller should emit a normal call,
2681 otherwise try to get the result in TARGET, if convenient (and in
2682 mode MODE if that's convenient). */
2684 expand_builtin_memcpy (tree arglist, rtx target, enum machine_mode mode)
2686 if (!validate_arglist (arglist,
2687 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2691 tree dest = TREE_VALUE (arglist);
2692 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2693 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2694 const char *src_str;
2695 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2696 unsigned int dest_align
2697 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2698 rtx dest_mem, src_mem, dest_addr, len_rtx;
2700 /* If DEST is not a pointer type, call the normal function. */
2701 if (dest_align == 0)
2704 /* If the LEN parameter is zero, return DEST. */
2705 if (integer_zerop (len))
2707 /* Evaluate and ignore SRC in case it has side-effects. */
2708 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2709 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2712 /* If SRC and DEST are the same (and not volatile), return DEST. */
2713 if (operand_equal_p (src, dest, 0))
2715 /* Evaluate and ignore LEN in case it has side-effects. */
2716 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2717 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2720 /* If either SRC is not a pointer type, don't do this
2721 operation in-line. */
2725 dest_mem = get_memory_rtx (dest);
2726 set_mem_align (dest_mem, dest_align);
2727 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2728 src_str = c_getstr (src);
2730 /* If SRC is a string constant and block move would be done
2731 by pieces, we can avoid loading the string from memory
2732 and only stored the computed constants. */
2734 && GET_CODE (len_rtx) == CONST_INT
2735 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2736 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2737 (void *) src_str, dest_align))
2739 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2740 builtin_memcpy_read_str,
2741 (void *) src_str, dest_align, 0);
2742 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2743 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2747 src_mem = get_memory_rtx (src);
2748 set_mem_align (src_mem, src_align);
2750 /* Copy word part most expediently. */
2751 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2756 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2757 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2763 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2764 Return 0 if we failed the caller should emit a normal call,
2765 otherwise try to get the result in TARGET, if convenient (and in
2766 mode MODE if that's convenient). If ENDP is 0 return the
2767 destination pointer, if ENDP is 1 return the end pointer ala
2768 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2772 expand_builtin_mempcpy (tree arglist, rtx target, enum machine_mode mode,
2775 if (!validate_arglist (arglist,
2776 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2778 /* If return value is ignored, transform mempcpy into memcpy. */
2779 else if (target == const0_rtx)
2781 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2786 return expand_expr (build_function_call_expr (fn, arglist),
2787 target, mode, EXPAND_NORMAL);
2791 tree dest = TREE_VALUE (arglist);
2792 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2793 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2794 const char *src_str;
2795 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2796 unsigned int dest_align
2797 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2798 rtx dest_mem, src_mem, len_rtx;
2800 /* If DEST is not a pointer type, call the normal function. */
2801 if (dest_align == 0)
2804 /* If SRC and DEST are the same (and not volatile), do nothing. */
2805 if (operand_equal_p (src, dest, 0))
2811 /* Evaluate and ignore LEN in case it has side-effects. */
2812 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2813 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2817 len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
2819 len = fold_convert (TREE_TYPE (dest), len);
2820 expr = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
2821 return expand_expr (expr, target, mode, EXPAND_NORMAL);
2824 /* If LEN is not constant, call the normal function. */
2825 if (! host_integerp (len, 1))
2828 /* If the LEN parameter is zero, return DEST. */
2829 if (tree_low_cst (len, 1) == 0)
2831 /* Evaluate and ignore SRC in case it has side-effects. */
2832 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2833 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2836 /* If either SRC is not a pointer type, don't do this
2837 operation in-line. */
2841 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2842 src_str = c_getstr (src);
2844 /* If SRC is a string constant and block move would be done
2845 by pieces, we can avoid loading the string from memory
2846 and only stored the computed constants. */
2848 && GET_CODE (len_rtx) == CONST_INT
2849 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2850 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2851 (void *) src_str, dest_align))
2853 dest_mem = get_memory_rtx (dest);
2854 set_mem_align (dest_mem, dest_align);
2855 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2856 builtin_memcpy_read_str,
2857 (void *) src_str, dest_align, endp);
2858 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2859 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2863 if (GET_CODE (len_rtx) == CONST_INT
2864 && can_move_by_pieces (INTVAL (len_rtx),
2865 MIN (dest_align, src_align)))
2867 dest_mem = get_memory_rtx (dest);
2868 set_mem_align (dest_mem, dest_align);
2869 src_mem = get_memory_rtx (src);
2870 set_mem_align (src_mem, src_align);
2871 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2872 MIN (dest_align, src_align), endp);
2873 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2874 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2882 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2883 if we failed the caller should emit a normal call. */
2886 expand_builtin_memmove (tree arglist, rtx target, enum machine_mode mode)
2888 if (!validate_arglist (arglist,
2889 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2893 tree dest = TREE_VALUE (arglist);
2894 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2895 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2897 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2898 unsigned int dest_align
2899 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2901 /* If DEST is not a pointer type, call the normal function. */
2902 if (dest_align == 0)
2905 /* If the LEN parameter is zero, return DEST. */
2906 if (integer_zerop (len))
2908 /* Evaluate and ignore SRC in case it has side-effects. */
2909 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2910 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2913 /* If SRC and DEST are the same (and not volatile), return DEST. */
2914 if (operand_equal_p (src, dest, 0))
2916 /* Evaluate and ignore LEN in case it has side-effects. */
2917 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2918 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2921 /* If either SRC is not a pointer type, don't do this
2922 operation in-line. */
2926 /* If src is categorized for a readonly section we can use
2928 if (readonly_data_expr (src))
2930 tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2933 return expand_expr (build_function_call_expr (fn, arglist),
2934 target, mode, EXPAND_NORMAL);
2937 /* Otherwise, call the normal function. */
2942 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2943 if we failed the caller should emit a normal call. */
2946 expand_builtin_bcopy (tree arglist)
2948 tree src, dest, size, newarglist;
2950 if (!validate_arglist (arglist,
2951 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2954 src = TREE_VALUE (arglist);
2955 dest = TREE_VALUE (TREE_CHAIN (arglist));
2956 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2958 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2959 memmove(ptr y, ptr x, size_t z). This is done this way
2960 so that if it isn't expanded inline, we fallback to
2961 calling bcopy instead of memmove. */
2963 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
2964 newarglist = tree_cons (NULL_TREE, src, newarglist);
2965 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2967 return expand_builtin_memmove (newarglist, const0_rtx, VOIDmode);
2970 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2971 if we failed the caller should emit a normal call, otherwise try to get
2972 the result in TARGET, if convenient (and in mode MODE if that's
2976 expand_builtin_strcpy (tree arglist, rtx target, enum machine_mode mode)
2978 tree fn, len, src, dst;
2980 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2983 src = TREE_VALUE (TREE_CHAIN (arglist));
2984 dst = TREE_VALUE (arglist);
2986 /* If SRC and DST are equal (and not volatile), return DST. */
2987 if (operand_equal_p (src, dst, 0))
2988 return expand_expr (dst, target, mode, EXPAND_NORMAL);
2990 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2994 len = c_strlen (src, 1);
2995 if (len == 0 || TREE_SIDE_EFFECTS (len))
2998 len = size_binop (PLUS_EXPR, len, ssize_int (1));
2999 arglist = build_tree_list (NULL_TREE, len);
3000 arglist = tree_cons (NULL_TREE, src, arglist);
3001 arglist = tree_cons (NULL_TREE, dst, arglist);
3002 return expand_expr (build_function_call_expr (fn, arglist),
3003 target, mode, EXPAND_NORMAL);
3006 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3007 Return 0 if we failed the caller should emit a normal call,
3008 otherwise try to get the result in TARGET, if convenient (and in
3009 mode MODE if that's convenient). */
3012 expand_builtin_stpcpy (tree arglist, rtx target, enum machine_mode mode)
3014 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3020 /* If return value is ignored, transform stpcpy into strcpy. */
3021 if (target == const0_rtx)
3023 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3027 return expand_expr (build_function_call_expr (fn, arglist),
3028 target, mode, EXPAND_NORMAL);
3031 /* Ensure we get an actual string whose length can be evaluated at
3032 compile-time, not an expression containing a string. This is
3033 because the latter will potentially produce pessimized code
3034 when used to produce the return value. */
3035 src = TREE_VALUE (TREE_CHAIN (arglist));
3036 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3039 dst = TREE_VALUE (arglist);
3040 len = fold (size_binop (PLUS_EXPR, len, ssize_int (1)));
3041 arglist = build_tree_list (NULL_TREE, len);
3042 arglist = tree_cons (NULL_TREE, src, arglist);
3043 arglist = tree_cons (NULL_TREE, dst, arglist);
3044 return expand_builtin_mempcpy (arglist, target, mode, /*endp=*/2);
3048 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3049 bytes from constant string DATA + OFFSET and return it as target
3053 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3054 enum machine_mode mode)
3056 const char *str = (const char *) data;
3058 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3061 return c_readstr (str + offset, mode);
3064 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3065 if we failed the caller should emit a normal call. */
3068 expand_builtin_strncpy (tree arglist, rtx target, enum machine_mode mode)
3070 if (!validate_arglist (arglist,
3071 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3075 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3076 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3079 /* We must be passed a constant len parameter. */
3080 if (TREE_CODE (len) != INTEGER_CST)
3083 /* If the len parameter is zero, return the dst parameter. */
3084 if (integer_zerop (len))
3086 /* Evaluate and ignore the src argument in case it has
3088 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
3089 VOIDmode, EXPAND_NORMAL);
3090 /* Return the dst parameter. */
3091 return expand_expr (TREE_VALUE (arglist), target, mode,
3095 /* Now, we must be passed a constant src ptr parameter. */
3096 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
3099 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3101 /* We're required to pad with trailing zeros if the requested
3102 len is greater than strlen(s2)+1. In that case try to
3103 use store_by_pieces, if it fails, punt. */
3104 if (tree_int_cst_lt (slen, len))
3106 tree dest = TREE_VALUE (arglist);
3107 unsigned int dest_align
3108 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3109 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3112 if (!p || dest_align == 0 || !host_integerp (len, 1)
3113 || !can_store_by_pieces (tree_low_cst (len, 1),
3114 builtin_strncpy_read_str,
3115 (void *) p, dest_align))
3118 dest_mem = get_memory_rtx (dest);
3119 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3120 builtin_strncpy_read_str,
3121 (void *) p, dest_align, 0);
3122 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3123 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3127 /* OK transform into builtin memcpy. */
3128 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3131 return expand_expr (build_function_call_expr (fn, arglist),
3132 target, mode, EXPAND_NORMAL);
3136 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3137 bytes from constant string DATA + OFFSET and return it as target
3141 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3142 enum machine_mode mode)
3144 const char *c = (const char *) data;
3145 char *p = alloca (GET_MODE_SIZE (mode));
3147 memset (p, *c, GET_MODE_SIZE (mode));
3149 return c_readstr (p, mode);
3152 /* Callback routine for store_by_pieces. Return the RTL of a register
3153 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3154 char value given in the RTL register data. For example, if mode is
3155 4 bytes wide, return the RTL for 0x01010101*data. */
3158 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3159 enum machine_mode mode)
3165 size = GET_MODE_SIZE (mode);
3170 memset (p, 1, size);
3171 coeff = c_readstr (p, mode);
3173 target = convert_to_mode (mode, (rtx) data, 1);
3174 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3175 return force_reg (mode, target);
3178 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3179 if we failed the caller should emit a normal call, otherwise try to get
3180 the result in TARGET, if convenient (and in mode MODE if that's
3184 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
3186 if (!validate_arglist (arglist,
3187 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3191 tree dest = TREE_VALUE (arglist);
3192 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3193 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3196 unsigned int dest_align
3197 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3198 rtx dest_mem, dest_addr, len_rtx;
3200 /* If DEST is not a pointer type, don't do this
3201 operation in-line. */
3202 if (dest_align == 0)
3205 /* If the LEN parameter is zero, return DEST. */
3206 if (integer_zerop (len))
3208 /* Evaluate and ignore VAL in case it has side-effects. */
3209 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3210 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3213 if (TREE_CODE (val) != INTEGER_CST)
3217 if (!host_integerp (len, 1))
3220 if (optimize_size && tree_low_cst (len, 1) > 1)
3223 /* Assume that we can memset by pieces if we can store the
3224 * the coefficients by pieces (in the required modes).
3225 * We can't pass builtin_memset_gen_str as that emits RTL. */
3227 if (!can_store_by_pieces (tree_low_cst (len, 1),
3228 builtin_memset_read_str,
3232 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3233 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3234 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3236 dest_mem = get_memory_rtx (dest);
3237 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3238 builtin_memset_gen_str,
3239 val_rtx, dest_align, 0);
3240 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3241 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3245 if (target_char_cast (val, &c))
3250 if (!host_integerp (len, 1))
3252 if (!can_store_by_pieces (tree_low_cst (len, 1),
3253 builtin_memset_read_str, &c,
3257 dest_mem = get_memory_rtx (dest);
3258 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3259 builtin_memset_read_str,
3261 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3262 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3266 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3268 dest_mem = get_memory_rtx (dest);
3269 set_mem_align (dest_mem, dest_align);
3270 dest_addr = clear_storage (dest_mem, len_rtx);
3274 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3275 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3282 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3283 if we failed the caller should emit a normal call. */
3286 expand_builtin_bzero (tree arglist)
3288 tree dest, size, newarglist;
3290 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3293 dest = TREE_VALUE (arglist);
3294 size = TREE_VALUE (TREE_CHAIN (arglist));
3296 /* New argument list transforming bzero(ptr x, int y) to
3297 memset(ptr x, int 0, size_t y). This is done this way
3298 so that if it isn't expanded inline, we fallback to
3299 calling bzero instead of memset. */
3301 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3302 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3303 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3305 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
3308 /* Expand expression EXP, which is a call to the memcmp built-in function.
3309 ARGLIST is the argument list for this call. Return 0 if we failed and the
3310 caller should emit a normal call, otherwise try to get the result in
3311 TARGET, if convenient (and in mode MODE, if that's convenient). */
3314 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3315 enum machine_mode mode)
3317 tree arg1, arg2, len;
3318 const char *p1, *p2;
3320 if (!validate_arglist (arglist,
3321 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3324 arg1 = TREE_VALUE (arglist);
3325 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3326 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3328 /* If the len parameter is zero, return zero. */
3329 if (integer_zerop (len))
3331 /* Evaluate and ignore arg1 and arg2 in case they have
3333 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3334 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3338 /* If both arguments are equal (and not volatile), return zero. */
3339 if (operand_equal_p (arg1, arg2, 0))
3341 /* Evaluate and ignore len in case it has side-effects. */
3342 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
3346 p1 = c_getstr (arg1);
3347 p2 = c_getstr (arg2);
3349 /* If all arguments are constant, and the value of len is not greater
3350 than the lengths of arg1 and arg2, evaluate at compile-time. */
3351 if (host_integerp (len, 1) && p1 && p2
3352 && compare_tree_int (len, strlen (p1) + 1) <= 0
3353 && compare_tree_int (len, strlen (p2) + 1) <= 0)
3355 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
3357 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
3360 /* If len parameter is one, return an expression corresponding to
3361 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
3362 if (integer_onep (len))
3364 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3365 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3367 fold (build1 (CONVERT_EXPR, integer_type_node,
3368 build1 (INDIRECT_REF, cst_uchar_node,
3369 fold_convert (cst_uchar_ptr_node, arg1))));
3371 fold (build1 (CONVERT_EXPR, integer_type_node,
3372 build1 (INDIRECT_REF, cst_uchar_node,
3373 fold_convert (cst_uchar_ptr_node, arg2))));
3374 tree result = fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
3375 return expand_expr (result, target, mode, EXPAND_NORMAL);
3378 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3380 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3385 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3387 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3388 enum machine_mode insn_mode;
3390 #ifdef HAVE_cmpmemsi
3392 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3395 #ifdef HAVE_cmpstrsi
3397 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3402 /* If we don't have POINTER_TYPE, call the function. */
3403 if (arg1_align == 0 || arg2_align == 0)
3406 /* Make a place to write the result of the instruction. */
3409 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3410 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3411 result = gen_reg_rtx (insn_mode);
3413 arg1_rtx = get_memory_rtx (arg1);
3414 arg2_rtx = get_memory_rtx (arg2);
3415 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3416 #ifdef HAVE_cmpmemsi
3418 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3419 GEN_INT (MIN (arg1_align, arg2_align)));
3422 #ifdef HAVE_cmpstrsi
3424 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3425 GEN_INT (MIN (arg1_align, arg2_align)));
3433 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3434 TYPE_MODE (integer_type_node), 3,
3435 XEXP (arg1_rtx, 0), Pmode,
3436 XEXP (arg2_rtx, 0), Pmode,
3437 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3438 TYPE_UNSIGNED (sizetype)),
3439 TYPE_MODE (sizetype));
3441 /* Return the value in the proper mode for this function. */
3442 mode = TYPE_MODE (TREE_TYPE (exp));
3443 if (GET_MODE (result) == mode)
3445 else if (target != 0)
3447 convert_move (target, result, 0);
3451 return convert_to_mode (mode, result, 0);
3458 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3459 if we failed the caller should emit a normal call, otherwise try to get
3460 the result in TARGET, if convenient. */
3463 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3465 tree arglist = TREE_OPERAND (exp, 1);
3467 const char *p1, *p2;
3469 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3472 arg1 = TREE_VALUE (arglist);
3473 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3475 /* If both arguments are equal (and not volatile), return zero. */
3476 if (operand_equal_p (arg1, arg2, 0))
3479 p1 = c_getstr (arg1);
3480 p2 = c_getstr (arg2);
3484 const int i = strcmp (p1, p2);
3485 return (i < 0 ? constm1_rtx : (i > 0 ? const1_rtx : const0_rtx));
3488 /* If either arg is "", return an expression corresponding to
3489 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
3490 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
3492 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3493 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3495 fold (build1 (CONVERT_EXPR, integer_type_node,
3496 build1 (INDIRECT_REF, cst_uchar_node,
3497 fold_convert (cst_uchar_ptr_node, arg1))));
3499 fold (build1 (CONVERT_EXPR, integer_type_node,
3500 build1 (INDIRECT_REF, cst_uchar_node,
3501 fold_convert (cst_uchar_ptr_node, arg2))));
3502 tree result = fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
3503 return expand_expr (result, target, mode, EXPAND_NORMAL);
3506 #ifdef HAVE_cmpstrsi
3509 tree len, len1, len2;
3510 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3515 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3517 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3518 enum machine_mode insn_mode
3519 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3521 len1 = c_strlen (arg1, 1);
3522 len2 = c_strlen (arg2, 1);
3525 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3527 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3529 /* If we don't have a constant length for the first, use the length
3530 of the second, if we know it. We don't require a constant for
3531 this case; some cost analysis could be done if both are available
3532 but neither is constant. For now, assume they're equally cheap,
3533 unless one has side effects. If both strings have constant lengths,
3540 else if (TREE_SIDE_EFFECTS (len1))
3542 else if (TREE_SIDE_EFFECTS (len2))
3544 else if (TREE_CODE (len1) != INTEGER_CST)
3546 else if (TREE_CODE (len2) != INTEGER_CST)
3548 else if (tree_int_cst_lt (len1, len2))
3553 /* If both arguments have side effects, we cannot optimize. */
3554 if (!len || TREE_SIDE_EFFECTS (len))
3557 /* If we don't have POINTER_TYPE, call the function. */
3558 if (arg1_align == 0 || arg2_align == 0)
3561 /* Make a place to write the result of the instruction. */
3564 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3565 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3566 result = gen_reg_rtx (insn_mode);
3568 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3569 arg1 = builtin_save_expr (arg1);
3570 arg2 = builtin_save_expr (arg2);
3572 arg1_rtx = get_memory_rtx (arg1);
3573 arg2_rtx = get_memory_rtx (arg2);
3574 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3575 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3576 GEN_INT (MIN (arg1_align, arg2_align)));
3581 /* Return the value in the proper mode for this function. */
3582 mode = TYPE_MODE (TREE_TYPE (exp));
3583 if (GET_MODE (result) == mode)
3586 return convert_to_mode (mode, result, 0);
3587 convert_move (target, result, 0);
3591 /* Expand the library call ourselves using a stabilized argument
3592 list to avoid re-evaluating the function's arguments twice. */
3593 arglist = build_tree_list (NULL_TREE, arg2);
3594 arglist = tree_cons (NULL_TREE, arg1, arglist);
3595 fndecl = get_callee_fndecl (exp);
3596 exp = build_function_call_expr (fndecl, arglist);
3597 return expand_call (exp, target, target == const0_rtx);
3603 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3604 if we failed the caller should emit a normal call, otherwise try to get
3605 the result in TARGET, if convenient. */
3608 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3610 tree arglist = TREE_OPERAND (exp, 1);
3611 tree arg1, arg2, arg3;
3612 const char *p1, *p2;
3614 if (!validate_arglist (arglist,
3615 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3618 arg1 = TREE_VALUE (arglist);
3619 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3620 arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3622 /* If the len parameter is zero, return zero. */
3623 if (integer_zerop (arg3))
3625 /* Evaluate and ignore arg1 and arg2 in case they have
3627 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3628 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3632 /* If arg1 and arg2 are equal (and not volatile), return zero. */
3633 if (operand_equal_p (arg1, arg2, 0))
3635 /* Evaluate and ignore arg3 in case it has side-effects. */
3636 expand_expr (arg3, const0_rtx, VOIDmode, EXPAND_NORMAL);
3640 p1 = c_getstr (arg1);
3641 p2 = c_getstr (arg2);
3643 /* If all arguments are constant, evaluate at compile-time. */
3644 if (host_integerp (arg3, 1) && p1 && p2)
3646 const int r = strncmp (p1, p2, tree_low_cst (arg3, 1));
3647 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
3650 /* If len == 1 or (either string parameter is "" and (len >= 1)),
3651 return (*(const u_char*)arg1 - *(const u_char*)arg2). */
3652 if (host_integerp (arg3, 1)
3653 && (tree_low_cst (arg3, 1) == 1
3654 || (tree_low_cst (arg3, 1) > 1
3655 && ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')))))
3657 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3658 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3660 fold (build1 (CONVERT_EXPR, integer_type_node,
3661 build1 (INDIRECT_REF, cst_uchar_node,
3662 fold_convert (cst_uchar_ptr_node, arg1))));
3664 fold (build1 (CONVERT_EXPR, integer_type_node,
3665 build1 (INDIRECT_REF, cst_uchar_node,
3666 fold_convert (cst_uchar_ptr_node, arg2))));
3667 tree result = fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
3668 return expand_expr (result, target, mode, EXPAND_NORMAL);
3671 /* If c_strlen can determine an expression for one of the string
3672 lengths, and it doesn't have side effects, then emit cmpstrsi
3673 using length MIN(strlen(string)+1, arg3). */
3674 #ifdef HAVE_cmpstrsi
3677 tree len, len1, len2;
3678 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3683 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3685 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3686 enum machine_mode insn_mode
3687 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3689 len1 = c_strlen (arg1, 1);
3690 len2 = c_strlen (arg2, 1);
3693 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3695 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3697 /* If we don't have a constant length for the first, use the length
3698 of the second, if we know it. We don't require a constant for
3699 this case; some cost analysis could be done if both are available
3700 but neither is constant. For now, assume they're equally cheap,
3701 unless one has side effects. If both strings have constant lengths,
3708 else if (TREE_SIDE_EFFECTS (len1))
3710 else if (TREE_SIDE_EFFECTS (len2))
3712 else if (TREE_CODE (len1) != INTEGER_CST)
3714 else if (TREE_CODE (len2) != INTEGER_CST)
3716 else if (tree_int_cst_lt (len1, len2))
3721 /* If both arguments have side effects, we cannot optimize. */
3722 if (!len || TREE_SIDE_EFFECTS (len))
3725 /* The actual new length parameter is MIN(len,arg3). */
3726 len = fold (build2 (MIN_EXPR, TREE_TYPE (len), len, arg3));
3728 /* If we don't have POINTER_TYPE, call the function. */
3729 if (arg1_align == 0 || arg2_align == 0)
3732 /* Make a place to write the result of the instruction. */
3735 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3736 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3737 result = gen_reg_rtx (insn_mode);
3739 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3740 arg1 = builtin_save_expr (arg1);
3741 arg2 = builtin_save_expr (arg2);
3742 len = builtin_save_expr (len);
3744 arg1_rtx = get_memory_rtx (arg1);
3745 arg2_rtx = get_memory_rtx (arg2);
3746 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3747 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3748 GEN_INT (MIN (arg1_align, arg2_align)));
3753 /* Return the value in the proper mode for this function. */
3754 mode = TYPE_MODE (TREE_TYPE (exp));
3755 if (GET_MODE (result) == mode)
3758 return convert_to_mode (mode, result, 0);
3759 convert_move (target, result, 0);
3763 /* Expand the library call ourselves using a stabilized argument
3764 list to avoid re-evaluating the function's arguments twice. */
3765 arglist = build_tree_list (NULL_TREE, len);
3766 arglist = tree_cons (NULL_TREE, arg2, arglist);
3767 arglist = tree_cons (NULL_TREE, arg1, arglist);
3768 fndecl = get_callee_fndecl (exp);
3769 exp = build_function_call_expr (fndecl, arglist);
3770 return expand_call (exp, target, target == const0_rtx);
3776 /* Expand expression EXP, which is a call to the strcat builtin.
3777 Return 0 if we failed the caller should emit a normal call,
3778 otherwise try to get the result in TARGET, if convenient. */
3781 expand_builtin_strcat (tree arglist, rtx target, enum machine_mode mode)
3783 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3787 tree dst = TREE_VALUE (arglist),
3788 src = TREE_VALUE (TREE_CHAIN (arglist));
3789 const char *p = c_getstr (src);
3793 /* If the string length is zero, return the dst parameter. */
3795 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3796 else if (!optimize_size)
3798 /* Otherwise if !optimize_size, see if we can store by
3799 pieces into (dst + strlen(dst)). */
3800 tree newdst, arglist,
3801 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3803 /* This is the length argument. */
3804 arglist = build_tree_list (NULL_TREE,
3805 fold (size_binop (PLUS_EXPR,
3808 /* Prepend src argument. */
3809 arglist = tree_cons (NULL_TREE, src, arglist);
3811 /* We're going to use dst more than once. */
3812 dst = builtin_save_expr (dst);
3814 /* Create strlen (dst). */
3816 fold (build_function_call_expr (strlen_fn,
3817 build_tree_list (NULL_TREE,
3819 /* Create (dst + strlen (dst)). */
3820 newdst = fold (build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
3822 /* Prepend the new dst argument. */
3823 arglist = tree_cons (NULL_TREE, newdst, arglist);
3825 /* We don't want to get turned into a memcpy if the
3826 target is const0_rtx, i.e. when the return value
3827 isn't used. That would produce pessimized code so
3828 pass in a target of zero, it should never actually be
3829 used. If this was successful return the original
3830 dst, not the result of mempcpy. */
3831 if (expand_builtin_mempcpy (arglist, /*target=*/0, mode, /*endp=*/0))
3832 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3842 /* Expand expression EXP, which is a call to the strncat builtin.
3843 Return 0 if we failed the caller should emit a normal call,
3844 otherwise try to get the result in TARGET, if convenient. */
3847 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3849 if (!validate_arglist (arglist,
3850 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3854 tree dst = TREE_VALUE (arglist),
3855 src = TREE_VALUE (TREE_CHAIN (arglist)),
3856 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3857 const char *p = c_getstr (src);
3859 /* If the requested length is zero, or the src parameter string
3860 length is zero, return the dst parameter. */
3861 if (integer_zerop (len) || (p && *p == '\0'))
3863 /* Evaluate and ignore the src and len parameters in case
3864 they have side-effects. */
3865 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3866 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
3867 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3870 /* If the requested len is greater than or equal to the string
3871 length, call strcat. */
3872 if (TREE_CODE (len) == INTEGER_CST && p
3873 && compare_tree_int (len, strlen (p)) >= 0)
3876 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
3877 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
3879 /* If the replacement _DECL isn't initialized, don't do the
3884 return expand_expr (build_function_call_expr (fn, newarglist),
3885 target, mode, EXPAND_NORMAL);
3891 /* Expand expression EXP, which is a call to the strspn builtin.
3892 Return 0 if we failed the caller should emit a normal call,
3893 otherwise try to get the result in TARGET, if convenient. */
3896 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3898 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3902 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
3903 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
3905 /* If both arguments are constants, evaluate at compile-time. */
3908 const size_t r = strspn (p1, p2);
3909 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
3912 /* If either argument is "", return 0. */
3913 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
3915 /* Evaluate and ignore both arguments in case either one has
3917 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3918 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3925 /* Expand expression EXP, which is a call to the strcspn builtin.
3926 Return 0 if we failed the caller should emit a normal call,
3927 otherwise try to get the result in TARGET, if convenient. */
3930 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3932 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3936 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
3937 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
3939 /* If both arguments are constants, evaluate at compile-time. */
3942 const size_t r = strcspn (p1, p2);
3943 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
3946 /* If the first argument is "", return 0. */
3947 if (p1 && *p1 == '\0')
3949 /* Evaluate and ignore argument s2 in case it has
3951 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3955 /* If the second argument is "", return __builtin_strlen(s1). */
3956 if (p2 && *p2 == '\0')
3958 tree newarglist = build_tree_list (NULL_TREE, s1),
3959 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3961 /* If the replacement _DECL isn't initialized, don't do the
3966 return expand_expr (build_function_call_expr (fn, newarglist),
3967 target, mode, EXPAND_NORMAL);
3973 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3974 if that's convenient. */
3977 expand_builtin_saveregs (void)
3981 /* Don't do __builtin_saveregs more than once in a function.
3982 Save the result of the first call and reuse it. */
3983 if (saveregs_value != 0)
3984 return saveregs_value;
3986 /* When this function is called, it means that registers must be
3987 saved on entry to this function. So we migrate the call to the
3988 first insn of this function. */
3992 /* Do whatever the machine needs done in this case. */
3993 val = targetm.calls.expand_builtin_saveregs ();
3998 saveregs_value = val;
4000 /* Put the insns after the NOTE that starts the function. If this
4001 is inside a start_sequence, make the outer-level insn chain current, so
4002 the code is placed at the start of the function. */
4003 push_topmost_sequence ();
4004 emit_insn_after (seq, get_insns ());
4005 pop_topmost_sequence ();
4010 /* __builtin_args_info (N) returns word N of the arg space info
4011 for the current function. The number and meanings of words
4012 is controlled by the definition of CUMULATIVE_ARGS. */
4015 expand_builtin_args_info (tree arglist)
4017 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4018 int *word_ptr = (int *) ¤t_function_args_info;
4020 if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0)
4025 if (!host_integerp (TREE_VALUE (arglist), 0))
4026 error ("argument of `__builtin_args_info' must be constant");
4029 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
4031 if (wordnum < 0 || wordnum >= nwords)
4032 error ("argument of `__builtin_args_info' out of range");
4034 return GEN_INT (word_ptr[wordnum]);
4038 error ("missing argument in `__builtin_args_info'");
4043 /* Expand ARGLIST, from a call to __builtin_next_arg. */
4046 expand_builtin_next_arg (tree arglist)
4048 tree fntype = TREE_TYPE (current_function_decl);
4050 if (TYPE_ARG_TYPES (fntype) == 0
4051 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4054 error ("`va_start' used in function with fixed args");
4060 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
4061 tree arg = TREE_VALUE (arglist);
4063 /* Strip off all nops for the sake of the comparison. This
4064 is not quite the same as STRIP_NOPS. It does more.
4065 We must also strip off INDIRECT_EXPR for C++ reference
4067 while (TREE_CODE (arg) == NOP_EXPR
4068 || TREE_CODE (arg) == CONVERT_EXPR
4069 || TREE_CODE (arg) == NON_LVALUE_EXPR
4070 || TREE_CODE (arg) == INDIRECT_REF)
4071 arg = TREE_OPERAND (arg, 0);
4072 if (arg != last_parm)
4073 warning ("second parameter of `va_start' not last named argument");
4076 /* Evidently an out of date version of <stdarg.h>; can't validate
4077 va_start's second argument, but can still work as intended. */
4078 warning ("`__builtin_next_arg' called without an argument");
4080 return expand_binop (Pmode, add_optab,
4081 current_function_internal_arg_pointer,
4082 current_function_arg_offset_rtx,
4083 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4086 /* Make it easier for the backends by protecting the valist argument
4087 from multiple evaluations. */
4090 stabilize_va_list (tree valist, int needs_lvalue)
4092 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4094 if (TREE_SIDE_EFFECTS (valist))
4095 valist = save_expr (valist);
4097 /* For this case, the backends will be expecting a pointer to
4098 TREE_TYPE (va_list_type_node), but it's possible we've
4099 actually been given an array (an actual va_list_type_node).
4101 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4103 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4104 tree p2 = build_pointer_type (va_list_type_node);
4106 valist = build1 (ADDR_EXPR, p2, valist);
4107 valist = fold_convert (p1, valist);
4116 if (! TREE_SIDE_EFFECTS (valist))
4119 pt = build_pointer_type (va_list_type_node);
4120 valist = fold (build1 (ADDR_EXPR, pt, valist));
4121 TREE_SIDE_EFFECTS (valist) = 1;
4124 if (TREE_SIDE_EFFECTS (valist))
4125 valist = save_expr (valist);
4126 valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)),
4133 /* The "standard" definition of va_list is void*. */
4136 std_build_builtin_va_list (void)
4138 return ptr_type_node;
4141 /* The "standard" implementation of va_start: just assign `nextarg' to
4145 std_expand_builtin_va_start (tree valist, rtx nextarg)
4149 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4150 make_tree (ptr_type_node, nextarg));
4151 TREE_SIDE_EFFECTS (t) = 1;
4153 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4156 /* Expand ARGLIST, from a call to __builtin_va_start. */
4159 expand_builtin_va_start (tree arglist)
4164 chain = TREE_CHAIN (arglist);
4166 if (TREE_CHAIN (chain))
4167 error ("too many arguments to function `va_start'");
4169 nextarg = expand_builtin_next_arg (chain);
4170 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4172 #ifdef EXPAND_BUILTIN_VA_START
4173 EXPAND_BUILTIN_VA_START (valist, nextarg);
4175 std_expand_builtin_va_start (valist, nextarg);
4181 /* The "standard" implementation of va_arg: read the value from the
4182 current (padded) address and increment by the (padded) size. */
4185 std_expand_builtin_va_arg (tree valist, tree type)
4187 tree addr_tree, t, type_size = NULL;
4188 tree align, alignm1;
4191 HOST_WIDE_INT boundary;
4193 /* Compute the rounded size of the type. */
4194 align = size_int (PARM_BOUNDARY / BITS_PER_UNIT);
4195 alignm1 = size_int (PARM_BOUNDARY / BITS_PER_UNIT - 1);
4196 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
4198 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4199 requires greater alignment, we must perform dynamic alignment. */
4201 if (boundary > PARM_BOUNDARY)
4203 if (!PAD_VARARGS_DOWN)
4205 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4206 build2 (PLUS_EXPR, TREE_TYPE (valist), valist,
4207 build_int_2 (boundary / BITS_PER_UNIT - 1, 0)));
4208 TREE_SIDE_EFFECTS (t) = 1;
4209 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4211 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4212 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist,
4213 build_int_2 (~(boundary / BITS_PER_UNIT - 1), -1)));
4214 TREE_SIDE_EFFECTS (t) = 1;
4215 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4217 if (type == error_mark_node
4218 || (type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type))) == NULL
4219 || TREE_OVERFLOW (type_size))
4220 rounded_size = size_zero_node;
4223 rounded_size = fold (build2 (PLUS_EXPR, sizetype, type_size, alignm1));
4224 rounded_size = fold (build2 (TRUNC_DIV_EXPR, sizetype,
4225 rounded_size, align));
4226 rounded_size = fold (build2 (MULT_EXPR, sizetype,
4227 rounded_size, align));
4232 if (PAD_VARARGS_DOWN && ! integer_zerop (rounded_size))
4234 /* Small args are padded downward. */
4235 addr_tree = fold (build2 (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
4236 fold (build3 (COND_EXPR, sizetype,
4237 fold (build2 (GT_EXPR, sizetype,
4241 fold (build2 (MINUS_EXPR,
4247 addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
4248 addr = copy_to_reg (addr);
4250 /* Compute new value for AP. */
4251 if (! integer_zerop (rounded_size))
4253 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4254 build2 (PLUS_EXPR, TREE_TYPE (valist), valist,
4256 TREE_SIDE_EFFECTS (t) = 1;
4257 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4263 /* Expand __builtin_va_arg, which is not really a builtin function, but
4264 a very special sort of operator. */
4267 expand_builtin_va_arg (tree valist, tree type)
4270 tree promoted_type, want_va_type, have_va_type;
4272 /* Verify that valist is of the proper type. */
4274 want_va_type = va_list_type_node;
4275 have_va_type = TREE_TYPE (valist);
4276 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4278 /* If va_list is an array type, the argument may have decayed
4279 to a pointer type, e.g. by being passed to another function.
4280 In that case, unwrap both types so that we can compare the
4281 underlying records. */
4282 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4283 || TREE_CODE (have_va_type) == POINTER_TYPE)
4285 want_va_type = TREE_TYPE (want_va_type);
4286 have_va_type = TREE_TYPE (have_va_type);
4289 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4291 error ("first argument to `va_arg' not of type `va_list'");
4295 /* Generate a diagnostic for requesting data of a type that cannot
4296 be passed through `...' due to type promotion at the call site. */
4297 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4300 const char *name = "<anonymous type>", *pname = 0;
4301 static bool gave_help;
4303 if (TYPE_NAME (type))
4305 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4306 name = IDENTIFIER_POINTER (TYPE_NAME (type));
4307 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4308 && DECL_NAME (TYPE_NAME (type)))
4309 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
4311 if (TYPE_NAME (promoted_type))
4313 if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE)
4314 pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type));
4315 else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL
4316 && DECL_NAME (TYPE_NAME (promoted_type)))
4317 pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type)));
4320 /* Unfortunately, this is merely undefined, rather than a constraint
4321 violation, so we cannot make this an error. If this call is never
4322 executed, the program is still strictly conforming. */
4323 warning ("`%s' is promoted to `%s' when passed through `...'",
4328 warning ("(so you should pass `%s' not `%s' to `va_arg')",
4332 /* We can, however, treat "undefined" any way we please.
4333 Call abort to encourage the user to fix the program. */
4334 inform ("if this code is reached, the program will abort");
4335 expand_builtin_trap ();
4337 /* This is dead code, but go ahead and finish so that the
4338 mode of the result comes out right. */
4343 /* Make it easier for the backends by protecting the valist argument
4344 from multiple evaluations. */
4345 valist = stabilize_va_list (valist, 0);
4347 #ifdef EXPAND_BUILTIN_VA_ARG
4348 addr = EXPAND_BUILTIN_VA_ARG (valist, type);
4350 addr = std_expand_builtin_va_arg (valist, type);
4354 addr = convert_memory_address (Pmode, addr);
4356 result = gen_rtx_MEM (TYPE_MODE (type), addr);
4357 set_mem_alias_set (result, get_varargs_alias_set ());
4362 /* Expand ARGLIST, from a call to __builtin_va_end. */
4365 expand_builtin_va_end (tree arglist)
4367 tree valist = TREE_VALUE (arglist);
4369 /* Evaluate for side effects, if needed. I hate macros that don't
4371 if (TREE_SIDE_EFFECTS (valist))
4372 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4377 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4378 builtin rather than just as an assignment in stdarg.h because of the
4379 nastiness of array-type va_list types. */
4382 expand_builtin_va_copy (tree arglist)
4386 dst = TREE_VALUE (arglist);
4387 src = TREE_VALUE (TREE_CHAIN (arglist));
4389 dst = stabilize_va_list (dst, 1);
4390 src = stabilize_va_list (src, 0);
4392 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4394 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4395 TREE_SIDE_EFFECTS (t) = 1;
4396 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4400 rtx dstb, srcb, size;
4402 /* Evaluate to pointers. */
4403 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4404 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4405 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4406 VOIDmode, EXPAND_NORMAL);
4408 dstb = convert_memory_address (Pmode, dstb);
4409 srcb = convert_memory_address (Pmode, srcb);
4411 /* "Dereference" to BLKmode memories. */
4412 dstb = gen_rtx_MEM (BLKmode, dstb);
4413 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4414 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4415 srcb = gen_rtx_MEM (BLKmode, srcb);
4416 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4417 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4420 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4426 /* Expand a call to one of the builtin functions __builtin_frame_address or
4427 __builtin_return_address. */
4430 expand_builtin_frame_address (tree fndecl, tree arglist)
4432 /* The argument must be a nonnegative integer constant.
4433 It counts the number of frames to scan up the stack.
4434 The value is the return address saved in that frame. */
4436 /* Warning about missing arg was already issued. */
4438 else if (! host_integerp (TREE_VALUE (arglist), 1))
4440 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4441 error ("invalid arg to `__builtin_frame_address'");
4443 error ("invalid arg to `__builtin_return_address'");
4449 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4450 tree_low_cst (TREE_VALUE (arglist), 1),
4451 hard_frame_pointer_rtx);
4453 /* Some ports cannot access arbitrary stack frames. */
4456 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4457 warning ("unsupported arg to `__builtin_frame_address'");
4459 warning ("unsupported arg to `__builtin_return_address'");
4463 /* For __builtin_frame_address, return what we've got. */
4464 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4467 if (GET_CODE (tem) != REG
4468 && ! CONSTANT_P (tem))
4469 tem = copy_to_mode_reg (Pmode, tem);
4474 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4475 we failed and the caller should emit a normal call, otherwise try to get
4476 the result in TARGET, if convenient. */
4479 expand_builtin_alloca (tree arglist, rtx target)
4484 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4485 should always expand to function calls. These can be intercepted
4490 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4493 /* Compute the argument. */
4494 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4496 /* Allocate the desired space. */
4497 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4498 result = convert_memory_address (ptr_mode, result);
4503 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4504 Return 0 if a normal call should be emitted rather than expanding the
4505 function in-line. If convenient, the result should be placed in TARGET.
4506 SUBTARGET may be used as the target for computing one of EXP's operands. */
4509 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4510 rtx subtarget, optab op_optab)
4513 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4516 /* Compute the argument. */
4517 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4518 /* Compute op, into TARGET if possible.
4519 Set TARGET to wherever the result comes back. */
4520 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4521 op_optab, op0, target, 1);
4525 return convert_to_mode (target_mode, target, 0);
4528 /* If the string passed to fputs is a constant and is one character
4529 long, we attempt to transform this call into __builtin_fputc(). */
4532 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4535 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4536 : implicit_built_in_decls[BUILT_IN_FPUTC];
4537 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
4538 : implicit_built_in_decls[BUILT_IN_FWRITE];
4540 /* If the return value is used, or the replacement _DECL isn't
4541 initialized, don't do the transformation. */
4542 if (target != const0_rtx || !fn_fputc || !fn_fwrite)
4545 /* Verify the arguments in the original call. */
4546 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4549 /* Get the length of the string passed to fputs. If the length
4550 can't be determined, punt. */
4551 if (!(len = c_strlen (TREE_VALUE (arglist), 1))
4552 || TREE_CODE (len) != INTEGER_CST)
4555 switch (compare_tree_int (len, 1))
4557 case -1: /* length is 0, delete the call entirely . */
4559 /* Evaluate and ignore the argument in case it has
4561 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
4562 VOIDmode, EXPAND_NORMAL);
4565 case 0: /* length is 1, call fputc. */
4567 const char *p = c_getstr (TREE_VALUE (arglist));
4571 /* New argument list transforming fputs(string, stream) to
4572 fputc(string[0], stream). */
4574 build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
4576 tree_cons (NULL_TREE, build_int_2 (p[0], 0), arglist);
4582 case 1: /* length is greater than 1, call fwrite. */
4586 /* If optimizing for size keep fputs. */
4589 string_arg = TREE_VALUE (arglist);
4590 /* New argument list transforming fputs(string, stream) to
4591 fwrite(string, 1, len, stream). */
4592 arglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
4593 arglist = tree_cons (NULL_TREE, len, arglist);
4594 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
4595 arglist = tree_cons (NULL_TREE, string_arg, arglist);
4603 return expand_expr (build_function_call_expr (fn, arglist),
4604 const0_rtx, VOIDmode, EXPAND_NORMAL);
4607 /* Expand a call to __builtin_expect. We return our argument and emit a
4608 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4609 a non-jump context. */
4612 expand_builtin_expect (tree arglist, rtx target)
4617 if (arglist == NULL_TREE
4618 || TREE_CHAIN (arglist) == NULL_TREE)
4620 exp = TREE_VALUE (arglist);
4621 c = TREE_VALUE (TREE_CHAIN (arglist));
4623 if (TREE_CODE (c) != INTEGER_CST)
4625 error ("second arg to `__builtin_expect' must be a constant");
4626 c = integer_zero_node;
4629 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4631 /* Don't bother with expected value notes for integral constants. */
4632 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4634 /* We do need to force this into a register so that we can be
4635 moderately sure to be able to correctly interpret the branch
4637 target = force_reg (GET_MODE (target), target);
4639 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4641 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4642 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4648 /* Like expand_builtin_expect, except do this in a jump context. This is
4649 called from do_jump if the conditional is a __builtin_expect. Return either
4650 a list of insns to emit the jump or NULL if we cannot optimize
4651 __builtin_expect. We need to optimize this at jump time so that machines
4652 like the PowerPC don't turn the test into a SCC operation, and then jump
4653 based on the test being 0/1. */
4656 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4658 tree arglist = TREE_OPERAND (exp, 1);
4659 tree arg0 = TREE_VALUE (arglist);
4660 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4663 /* Only handle __builtin_expect (test, 0) and
4664 __builtin_expect (test, 1). */
4665 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4666 && (integer_zerop (arg1) || integer_onep (arg1)))
4668 rtx insn, drop_through_label, temp;
4670 /* Expand the jump insns. */
4672 do_jump (arg0, if_false_label, if_true_label);
4675 drop_through_label = get_last_insn ();
4676 if (drop_through_label && GET_CODE (drop_through_label) == NOTE)
4677 drop_through_label = prev_nonnote_insn (drop_through_label);
4678 if (drop_through_label && GET_CODE (drop_through_label) != CODE_LABEL)
4679 drop_through_label = NULL_RTX;
4682 if (! if_true_label)
4683 if_true_label = drop_through_label;
4684 if (! if_false_label)
4685 if_false_label = drop_through_label;
4687 /* Go through and add the expect's to each of the conditional jumps. */
4689 while (insn != NULL_RTX)
4691 rtx next = NEXT_INSN (insn);
4693 if (GET_CODE (insn) == JUMP_INSN && any_condjump_p (insn))
4695 rtx ifelse = SET_SRC (pc_set (insn));
4696 rtx then_dest = XEXP (ifelse, 1);
4697 rtx else_dest = XEXP (ifelse, 2);
4700 /* First check if we recognize any of the labels. */
4701 if (GET_CODE (then_dest) == LABEL_REF
4702 && XEXP (then_dest, 0) == if_true_label)
4704 else if (GET_CODE (then_dest) == LABEL_REF
4705 && XEXP (then_dest, 0) == if_false_label)
4707 else if (GET_CODE (else_dest) == LABEL_REF
4708 && XEXP (else_dest, 0) == if_false_label)
4710 else if (GET_CODE (else_dest) == LABEL_REF
4711 && XEXP (else_dest, 0) == if_true_label)
4713 /* Otherwise check where we drop through. */
4714 else if (else_dest == pc_rtx)
4716 if (next && GET_CODE (next) == NOTE)
4717 next = next_nonnote_insn (next);
4719 if (next && GET_CODE (next) == JUMP_INSN
4720 && any_uncondjump_p (next))
4721 temp = XEXP (SET_SRC (pc_set (next)), 0);
4725 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4726 else that can't possibly match either target label. */
4727 if (temp == if_false_label)
4729 else if (temp == if_true_label)
4732 else if (then_dest == pc_rtx)
4734 if (next && GET_CODE (next) == NOTE)
4735 next = next_nonnote_insn (next);
4737 if (next && GET_CODE (next) == JUMP_INSN
4738 && any_uncondjump_p (next))
4739 temp = XEXP (SET_SRC (pc_set (next)), 0);
4743 if (temp == if_false_label)
4745 else if (temp == if_true_label)
4751 /* If the test is expected to fail, reverse the
4753 if (integer_zerop (arg1))
4755 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4767 expand_builtin_trap (void)
4771 emit_insn (gen_trap ());
4774 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4778 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4779 Return 0 if a normal call should be emitted rather than expanding
4780 the function inline. If convenient, the result should be placed
4781 in TARGET. SUBTARGET may be used as the target for computing
4785 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4787 enum machine_mode mode;
4791 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4794 arg = TREE_VALUE (arglist);
4795 mode = TYPE_MODE (TREE_TYPE (arg));
4796 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4797 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4800 /* Expand a call to cabs, cabsf or cabsl with arguments ARGLIST.
4801 Return 0 if a normal call should be emitted rather than expanding
4802 the function inline. If convenient, the result should be placed
4806 expand_builtin_cabs (tree arglist, rtx target)
4808 enum machine_mode mode;
4812 if (arglist == 0 || TREE_CHAIN (arglist))
4814 arg = TREE_VALUE (arglist);
4815 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
4816 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
4819 mode = TYPE_MODE (TREE_TYPE (arg));
4820 op0 = expand_expr (arg, NULL_RTX, VOIDmode, 0);
4821 return expand_complex_abs (mode, op0, target, 0);
4824 /* Create a new constant string literal and return a char* pointer to it.
4825 The STRING_CST value is the LEN characters at STR. */
4827 build_string_literal (int len, const char *str)
4829 tree t, elem, index, type;
4831 t = build_string (len, str);
4832 elem = build_type_variant (char_type_node, 1, 0);
4833 index = build_index_type (build_int_2 (len - 1, 0));
4834 type = build_array_type (elem, index);
4835 TREE_TYPE (t) = type;
4836 TREE_CONSTANT (t) = 1;
4837 TREE_INVARIANT (t) = 1;
4838 TREE_READONLY (t) = 1;
4839 TREE_STATIC (t) = 1;
4841 type = build_pointer_type (type);
4842 t = build1 (ADDR_EXPR, type, t);
4844 type = build_pointer_type (elem);
4845 t = build1 (NOP_EXPR, type, t);
4849 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4850 Return 0 if a normal call should be emitted rather than transforming
4851 the function inline. If convenient, the result should be placed in
4852 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4855 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4858 tree fn_putchar = unlocked
4859 ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4860 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4861 tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4862 : implicit_built_in_decls[BUILT_IN_PUTS];
4863 const char *fmt_str;
4866 /* If the return value is used, don't do the transformation. */
4867 if (target != const0_rtx)
4870 /* Verify the required arguments in the original call. */
4873 fmt = TREE_VALUE (arglist);
4874 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4876 arglist = TREE_CHAIN (arglist);
4878 /* Check whether the format is a literal string constant. */
4879 fmt_str = c_getstr (fmt);
4880 if (fmt_str == NULL)
4883 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4884 if (strcmp (fmt_str, "%s\n") == 0)
4887 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
4888 || TREE_CHAIN (arglist))
4892 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4893 else if (strcmp (fmt_str, "%c") == 0)
4896 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4897 || TREE_CHAIN (arglist))
4903 /* We can't handle anything else with % args or %% ... yet. */
4904 if (strchr (fmt_str, '%'))
4910 /* If the format specifier was "", printf does nothing. */
4911 if (fmt_str[0] == '\0')
4913 /* If the format specifier has length of 1, call putchar. */
4914 if (fmt_str[1] == '\0')
4916 /* Given printf("c"), (where c is any one character,)
4917 convert "c"[0] to an int and pass that to the replacement
4919 arg = build_int_2 (fmt_str[0], 0);
4920 arglist = build_tree_list (NULL_TREE, arg);
4925 /* If the format specifier was "string\n", call puts("string"). */
4926 size_t len = strlen (fmt_str);
4927 if (fmt_str[len - 1] == '\n')
4929 /* Create a NUL-terminated string that's one char shorter
4930 than the original, stripping off the trailing '\n'. */
4931 char *newstr = alloca (len);
4932 memcpy (newstr, fmt_str, len - 1);
4933 newstr[len - 1] = 0;
4935 arg = build_string_literal (len, newstr);
4936 arglist = build_tree_list (NULL_TREE, arg);
4940 /* We'd like to arrange to call fputs(string,stdout) here,
4941 but we need stdout and don't have a way to get it yet. */
4948 return expand_expr (build_function_call_expr (fn, arglist),
4949 target, mode, EXPAND_NORMAL);
4952 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4953 Return 0 if a normal call should be emitted rather than transforming
4954 the function inline. If convenient, the result should be placed in
4955 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4958 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
4961 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4962 : implicit_built_in_decls[BUILT_IN_FPUTC];
4963 tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4964 : implicit_built_in_decls[BUILT_IN_FPUTS];
4965 const char *fmt_str;
4966 tree fn, fmt, fp, arg;
4968 /* If the return value is used, don't do the transformation. */
4969 if (target != const0_rtx)
4972 /* Verify the required arguments in the original call. */
4975 fp = TREE_VALUE (arglist);
4976 if (TREE_CODE (TREE_TYPE (fp)) != POINTER_TYPE)
4978 arglist = TREE_CHAIN (arglist);
4981 fmt = TREE_VALUE (arglist);
4982 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4984 arglist = TREE_CHAIN (arglist);
4986 /* Check whether the format is a literal string constant. */
4987 fmt_str = c_getstr (fmt);
4988 if (fmt_str == NULL)
4991 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4992 if (strcmp (fmt_str, "%s") == 0)
4995 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
4996 || TREE_CHAIN (arglist))
4998 arg = TREE_VALUE (arglist);
4999 arglist = build_tree_list (NULL_TREE, fp);
5000 arglist = tree_cons (NULL_TREE, arg, arglist);
5003 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5004 else if (strcmp (fmt_str, "%c") == 0)
5007 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5008 || TREE_CHAIN (arglist))
5010 arg = TREE_VALUE (arglist);
5011 arglist = build_tree_list (NULL_TREE, fp);
5012 arglist = tree_cons (NULL_TREE, arg, arglist);
5017 /* We can't handle anything else with % args or %% ... yet. */
5018 if (strchr (fmt_str, '%'))
5024 /* If the format specifier was "", fprintf does nothing. */
5025 if (fmt_str[0] == '\0')
5027 /* Evaluate and ignore FILE* argument for side-effects. */
5028 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5032 /* When "string" doesn't contain %, replace all cases of
5033 fprintf(stream,string) with fputs(string,stream). The fputs
5034 builtin will take care of special cases like length == 1. */
5035 arglist = build_tree_list (NULL_TREE, fp);
5036 arglist = tree_cons (NULL_TREE, fmt, arglist);
5042 return expand_expr (build_function_call_expr (fn, arglist),
5043 target, mode, EXPAND_NORMAL);
5046 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5047 a normal call should be emitted rather than expanding the function
5048 inline. If convenient, the result should be placed in TARGET with
5052 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5054 tree orig_arglist, dest, fmt;
5055 const char *fmt_str;
5057 orig_arglist = arglist;
5059 /* Verify the required arguments in the original call. */
5062 dest = TREE_VALUE (arglist);
5063 if (TREE_CODE (TREE_TYPE (dest)) != POINTER_TYPE)
5065 arglist = TREE_CHAIN (arglist);
5068 fmt = TREE_VALUE (arglist);
5069 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
5071 arglist = TREE_CHAIN (arglist);
5073 /* Check whether the format is a literal string constant. */
5074 fmt_str = c_getstr (fmt);
5075 if (fmt_str == NULL)
5078 /* If the format doesn't contain % args or %%, use strcpy. */
5079 if (strchr (fmt_str, '%') == 0)
5081 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5084 if (arglist || ! fn)
5086 expand_expr (build_function_call_expr (fn, orig_arglist),
5087 const0_rtx, VOIDmode, EXPAND_NORMAL);
5088 if (target == const0_rtx)
5090 exp = build_int_2 (strlen (fmt_str), 0);
5091 exp = fold_convert (integer_type_node, exp);
5092 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5094 /* If the format is "%s", use strcpy if the result isn't used. */
5095 else if (strcmp (fmt_str, "%s") == 0)
5098 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5103 if (! arglist || TREE_CHAIN (arglist))
5105 arg = TREE_VALUE (arglist);
5106 if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
5109 if (target != const0_rtx)
5111 len = c_strlen (arg, 1);
5112 if (! len || TREE_CODE (len) != INTEGER_CST)
5118 arglist = build_tree_list (NULL_TREE, arg);
5119 arglist = tree_cons (NULL_TREE, dest, arglist);
5120 expand_expr (build_function_call_expr (fn, arglist),
5121 const0_rtx, VOIDmode, EXPAND_NORMAL);
5123 if (target == const0_rtx)
5125 return expand_expr (len, target, mode, EXPAND_NORMAL);
5131 /* Expand a call to either the entry or exit function profiler. */
5134 expand_builtin_profile_func (bool exitp)
5138 this = DECL_RTL (current_function_decl);
5139 if (GET_CODE (this) == MEM)
5140 this = XEXP (this, 0);
5145 which = profile_function_exit_libfunc;
5147 which = profile_function_entry_libfunc;
5149 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5150 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5151 0, hard_frame_pointer_rtx),
5157 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5160 round_trampoline_addr (rtx tramp)
5162 rtx temp, addend, mask;
5164 /* If we don't need too much alignment, we'll have been guaranteed
5165 proper alignment by get_trampoline_type. */
5166 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5169 /* Round address up to desired boundary. */
5170 temp = gen_reg_rtx (Pmode);
5171 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5172 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5174 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5175 temp, 0, OPTAB_LIB_WIDEN);
5176 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5177 temp, 0, OPTAB_LIB_WIDEN);
5183 expand_builtin_init_trampoline (tree arglist)
5185 tree t_tramp, t_func, t_chain;
5186 rtx r_tramp, r_func, r_chain;
5187 #ifdef TRAMPOLINE_TEMPLATE
5191 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5192 POINTER_TYPE, VOID_TYPE))
5195 t_tramp = TREE_VALUE (arglist);
5196 arglist = TREE_CHAIN (arglist);
5197 t_func = TREE_VALUE (arglist);
5198 arglist = TREE_CHAIN (arglist);
5199 t_chain = TREE_VALUE (arglist);
5201 r_tramp = expand_expr (t_tramp, NULL_RTX, VOIDmode, 0);
5202 r_func = expand_expr (t_func, NULL_RTX, VOIDmode, 0);
5203 r_chain = expand_expr (t_chain, NULL_RTX, VOIDmode, 0);
5205 /* Generate insns to initialize the trampoline. */
5206 r_tramp = round_trampoline_addr (r_tramp);
5207 #ifdef TRAMPOLINE_TEMPLATE
5208 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5209 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5210 emit_block_move (blktramp, assemble_trampoline_template (),
5211 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5213 trampolines_created = 1;
5214 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5220 expand_builtin_adjust_trampoline (tree arglist)
5224 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5227 tramp = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5228 tramp = round_trampoline_addr (tramp);
5229 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5230 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5236 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5237 Return NULL_RTX if a normal call should be emitted rather than expanding
5238 the function in-line. EXP is the expression that is a call to the builtin
5239 function; if convenient, the result should be placed in TARGET. */
5242 expand_builtin_signbit (tree exp, rtx target)
5244 const struct real_format *fmt;
5245 enum machine_mode fmode, imode, rmode;
5246 HOST_WIDE_INT hi, lo;
5251 arglist = TREE_OPERAND (exp, 1);
5252 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5255 arg = TREE_VALUE (arglist);
5256 fmode = TYPE_MODE (TREE_TYPE (arg));
5257 rmode = TYPE_MODE (TREE_TYPE (exp));
5258 fmt = REAL_MODE_FORMAT (fmode);
5260 /* For floating point formats without a sign bit, implement signbit
5262 if (fmt->signbit < 0)
5264 /* But we can't do this if the format supports signed zero. */
5265 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5268 arg = fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
5269 build_real (TREE_TYPE (arg), dconst0)));
5270 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5273 imode = int_mode_for_mode (fmode);
5274 if (imode == BLKmode)
5277 bitpos = fmt->signbit;
5278 /* Handle targets with different FP word orders. */
5279 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
5281 int nwords = GET_MODE_BITSIZE (fmode) / BITS_PER_WORD;
5282 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
5283 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
5286 /* If the sign bit is not in the lowpart and the floating point format
5287 is wider than an integer, check that is twice the size of an integer
5288 so that we can use gen_highpart below. */
5289 if (bitpos >= GET_MODE_BITSIZE (rmode)
5290 && GET_MODE_BITSIZE (imode) != 2 * GET_MODE_BITSIZE (rmode))
5293 temp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
5294 temp = gen_lowpart (imode, temp);
5296 if (GET_MODE_BITSIZE (imode) > GET_MODE_BITSIZE (rmode))
5298 if (BYTES_BIG_ENDIAN)
5299 bitpos = GET_MODE_BITSIZE (imode) - 1 - bitpos;
5300 temp = copy_to_mode_reg (imode, temp);
5301 temp = extract_bit_field (temp, 1, bitpos, 1,
5302 NULL_RTX, rmode, rmode,
5303 GET_MODE_SIZE (imode));
5307 if (GET_MODE_BITSIZE (imode) < GET_MODE_BITSIZE (rmode))
5308 temp = gen_lowpart (rmode, temp);
5309 if (bitpos < HOST_BITS_PER_WIDE_INT)
5312 lo = (HOST_WIDE_INT) 1 << bitpos;
5316 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5320 temp = force_reg (rmode, temp);
5321 temp = expand_binop (rmode, and_optab, temp,
5322 immed_double_const (lo, hi, rmode),
5323 target, 1, OPTAB_LIB_WIDEN);
5328 /* Expand fork or exec calls. TARGET is the desired target of the
5329 call. ARGLIST is the list of arguments of the call. FN is the
5330 identificator of the actual function. IGNORE is nonzero if the
5331 value is to be ignored. */
5334 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5339 /* If we are not profiling, just call the function. */
5340 if (!profile_arc_flag)
5343 /* Otherwise call the wrapper. This should be equivalent for the rest of
5344 compiler, so the code does not diverge, and the wrapper may run the
5345 code necessary for keeping the profiling sane. */
5347 switch (DECL_FUNCTION_CODE (fn))
5350 id = get_identifier ("__gcov_fork");
5353 case BUILT_IN_EXECL:
5354 id = get_identifier ("__gcov_execl");
5357 case BUILT_IN_EXECV:
5358 id = get_identifier ("__gcov_execv");
5361 case BUILT_IN_EXECLP:
5362 id = get_identifier ("__gcov_execlp");
5365 case BUILT_IN_EXECLE:
5366 id = get_identifier ("__gcov_execle");
5369 case BUILT_IN_EXECVP:
5370 id = get_identifier ("__gcov_execvp");
5373 case BUILT_IN_EXECVE:
5374 id = get_identifier ("__gcov_execve");
5381 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5382 DECL_EXTERNAL (decl) = 1;
5383 TREE_PUBLIC (decl) = 1;
5384 DECL_ARTIFICIAL (decl) = 1;
5385 TREE_NOTHROW (decl) = 1;
5386 call = build_function_call_expr (decl, arglist);
5388 return expand_call (call, target, ignore);
5391 /* Expand an expression EXP that calls a built-in function,
5392 with result going to TARGET if that's convenient
5393 (and in mode MODE if that's convenient).
5394 SUBTARGET may be used as the target for computing one of EXP's operands.
5395 IGNORE is nonzero if the value is to be ignored. */
5398 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5401 tree fndecl = get_callee_fndecl (exp);
5402 tree arglist = TREE_OPERAND (exp, 1);
5403 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5404 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5406 /* Perform postincrements before expanding builtin functions. */
5409 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5410 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5412 /* When not optimizing, generate calls to library functions for a certain
5415 && !CALLED_AS_BUILT_IN (fndecl)
5416 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5417 && fcode != BUILT_IN_ALLOCA)
5418 return expand_call (exp, target, ignore);
5420 /* The built-in function expanders test for target == const0_rtx
5421 to determine whether the function's result will be ignored. */
5423 target = const0_rtx;
5425 /* If the result of a pure or const built-in function is ignored, and
5426 none of its arguments are volatile, we can avoid expanding the
5427 built-in call and just evaluate the arguments for side-effects. */
5428 if (target == const0_rtx
5429 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5431 bool volatilep = false;
5434 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5435 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5443 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5444 expand_expr (TREE_VALUE (arg), const0_rtx,
5445 VOIDmode, EXPAND_NORMAL);
5454 case BUILT_IN_LLABS:
5455 case BUILT_IN_IMAXABS:
5456 /* build_function_call changes these into ABS_EXPR. */
5460 case BUILT_IN_FABSF:
5461 case BUILT_IN_FABSL:
5462 target = expand_builtin_fabs (arglist, target, subtarget);
5468 case BUILT_IN_CABSF:
5469 case BUILT_IN_CABSL:
5470 if (flag_unsafe_math_optimizations)
5472 target = expand_builtin_cabs (arglist, target);
5479 case BUILT_IN_CONJF:
5480 case BUILT_IN_CONJL:
5481 case BUILT_IN_CREAL:
5482 case BUILT_IN_CREALF:
5483 case BUILT_IN_CREALL:
5484 case BUILT_IN_CIMAG:
5485 case BUILT_IN_CIMAGF:
5486 case BUILT_IN_CIMAGL:
5487 /* expand_tree_builtin changes these into CONJ_EXPR, REALPART_EXPR
5488 and IMAGPART_EXPR. */
5494 case BUILT_IN_EXP10:
5495 case BUILT_IN_EXP10F:
5496 case BUILT_IN_EXP10L:
5497 case BUILT_IN_POW10:
5498 case BUILT_IN_POW10F:
5499 case BUILT_IN_POW10L:
5501 case BUILT_IN_EXP2F:
5502 case BUILT_IN_EXP2L:
5503 case BUILT_IN_EXPM1:
5504 case BUILT_IN_EXPM1F:
5505 case BUILT_IN_EXPM1L:
5507 case BUILT_IN_LOGBF:
5508 case BUILT_IN_LOGBL:
5509 case BUILT_IN_ILOGB:
5510 case BUILT_IN_ILOGBF:
5511 case BUILT_IN_ILOGBL:
5515 case BUILT_IN_LOG10:
5516 case BUILT_IN_LOG10F:
5517 case BUILT_IN_LOG10L:
5519 case BUILT_IN_LOG2F:
5520 case BUILT_IN_LOG2L:
5521 case BUILT_IN_LOG1P:
5522 case BUILT_IN_LOG1PF:
5523 case BUILT_IN_LOG1PL:
5528 case BUILT_IN_ASINF:
5529 case BUILT_IN_ASINL:
5531 case BUILT_IN_ACOSF:
5532 case BUILT_IN_ACOSL:
5534 case BUILT_IN_ATANF:
5535 case BUILT_IN_ATANL:
5536 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5537 because of possible accuracy problems. */
5538 if (! flag_unsafe_math_optimizations)
5541 case BUILT_IN_SQRTF:
5542 case BUILT_IN_SQRTL:
5543 case BUILT_IN_FLOOR:
5544 case BUILT_IN_FLOORF:
5545 case BUILT_IN_FLOORL:
5547 case BUILT_IN_CEILF:
5548 case BUILT_IN_CEILL:
5549 case BUILT_IN_TRUNC:
5550 case BUILT_IN_TRUNCF:
5551 case BUILT_IN_TRUNCL:
5552 case BUILT_IN_ROUND:
5553 case BUILT_IN_ROUNDF:
5554 case BUILT_IN_ROUNDL:
5555 case BUILT_IN_NEARBYINT:
5556 case BUILT_IN_NEARBYINTF:
5557 case BUILT_IN_NEARBYINTL:
5558 target = expand_builtin_mathfn (exp, target, subtarget);
5566 target = expand_builtin_pow (exp, target, subtarget);
5571 case BUILT_IN_ATAN2:
5572 case BUILT_IN_ATAN2F:
5573 case BUILT_IN_ATAN2L:
5575 case BUILT_IN_FMODF:
5576 case BUILT_IN_FMODL:
5578 case BUILT_IN_DREMF:
5579 case BUILT_IN_DREML:
5580 if (! flag_unsafe_math_optimizations)
5582 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5593 if (! flag_unsafe_math_optimizations)
5595 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5600 case BUILT_IN_APPLY_ARGS:
5601 return expand_builtin_apply_args ();
5603 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5604 FUNCTION with a copy of the parameters described by
5605 ARGUMENTS, and ARGSIZE. It returns a block of memory
5606 allocated on the stack into which is stored all the registers
5607 that might possibly be used for returning the result of a
5608 function. ARGUMENTS is the value returned by
5609 __builtin_apply_args. ARGSIZE is the number of bytes of
5610 arguments that must be copied. ??? How should this value be
5611 computed? We'll also need a safe worst case value for varargs
5613 case BUILT_IN_APPLY:
5614 if (!validate_arglist (arglist, POINTER_TYPE,
5615 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5616 && !validate_arglist (arglist, REFERENCE_TYPE,
5617 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5625 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5626 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5628 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5631 /* __builtin_return (RESULT) causes the function to return the
5632 value described by RESULT. RESULT is address of the block of
5633 memory returned by __builtin_apply. */
5634 case BUILT_IN_RETURN:
5635 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5636 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5637 NULL_RTX, VOIDmode, 0));
5640 case BUILT_IN_SAVEREGS:
5641 return expand_builtin_saveregs ();
5643 case BUILT_IN_ARGS_INFO:
5644 return expand_builtin_args_info (arglist);
5646 /* Return the address of the first anonymous stack arg. */
5647 case BUILT_IN_NEXT_ARG:
5648 simplify_builtin_next_arg (arglist);
5649 return expand_builtin_next_arg (arglist);
5651 case BUILT_IN_CLASSIFY_TYPE:
5652 return expand_builtin_classify_type (arglist);
5654 case BUILT_IN_CONSTANT_P:
5657 case BUILT_IN_FRAME_ADDRESS:
5658 case BUILT_IN_RETURN_ADDRESS:
5659 return expand_builtin_frame_address (fndecl, arglist);
5661 /* Returns the address of the area where the structure is returned.
5663 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5665 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5666 || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM)
5669 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5671 case BUILT_IN_ALLOCA:
5672 target = expand_builtin_alloca (arglist, target);
5677 case BUILT_IN_STACK_ALLOC:
5678 expand_stack_alloc (TREE_VALUE (arglist),
5679 TREE_VALUE (TREE_CHAIN (arglist)));
5682 case BUILT_IN_STACK_SAVE:
5683 return expand_stack_save ();
5685 case BUILT_IN_STACK_RESTORE:
5686 expand_stack_restore (TREE_VALUE (arglist));
5691 case BUILT_IN_FFSLL:
5692 target = expand_builtin_unop (target_mode, arglist, target,
5693 subtarget, ffs_optab);
5700 case BUILT_IN_CLZLL:
5701 target = expand_builtin_unop (target_mode, arglist, target,
5702 subtarget, clz_optab);
5709 case BUILT_IN_CTZLL:
5710 target = expand_builtin_unop (target_mode, arglist, target,
5711 subtarget, ctz_optab);
5716 case BUILT_IN_POPCOUNT:
5717 case BUILT_IN_POPCOUNTL:
5718 case BUILT_IN_POPCOUNTLL:
5719 target = expand_builtin_unop (target_mode, arglist, target,
5720 subtarget, popcount_optab);
5725 case BUILT_IN_PARITY:
5726 case BUILT_IN_PARITYL:
5727 case BUILT_IN_PARITYLL:
5728 target = expand_builtin_unop (target_mode, arglist, target,
5729 subtarget, parity_optab);
5734 case BUILT_IN_STRLEN:
5735 target = expand_builtin_strlen (arglist, target, target_mode);
5740 case BUILT_IN_STRCPY:
5741 target = expand_builtin_strcpy (arglist, target, mode);
5746 case BUILT_IN_STRNCPY:
5747 target = expand_builtin_strncpy (arglist, target, mode);
5752 case BUILT_IN_STPCPY:
5753 target = expand_builtin_stpcpy (arglist, target, mode);
5758 case BUILT_IN_STRCAT:
5759 target = expand_builtin_strcat (arglist, target, mode);
5764 case BUILT_IN_STRNCAT:
5765 target = expand_builtin_strncat (arglist, target, mode);
5770 case BUILT_IN_STRSPN:
5771 target = expand_builtin_strspn (arglist, target, mode);
5776 case BUILT_IN_STRCSPN:
5777 target = expand_builtin_strcspn (arglist, target, mode);
5782 case BUILT_IN_STRSTR:
5783 target = expand_builtin_strstr (arglist, target, mode);
5788 case BUILT_IN_STRPBRK:
5789 target = expand_builtin_strpbrk (arglist, target, mode);
5794 case BUILT_IN_INDEX:
5795 case BUILT_IN_STRCHR:
5796 target = expand_builtin_strchr (arglist, target, mode);
5801 case BUILT_IN_RINDEX:
5802 case BUILT_IN_STRRCHR:
5803 target = expand_builtin_strrchr (arglist, target, mode);
5808 case BUILT_IN_MEMCPY:
5809 target = expand_builtin_memcpy (arglist, target, mode);
5814 case BUILT_IN_MEMPCPY:
5815 target = expand_builtin_mempcpy (arglist, target, mode, /*endp=*/ 1);
5820 case BUILT_IN_MEMMOVE:
5821 target = expand_builtin_memmove (arglist, target, mode);
5826 case BUILT_IN_BCOPY:
5827 target = expand_builtin_bcopy (arglist);
5832 case BUILT_IN_MEMSET:
5833 target = expand_builtin_memset (arglist, target, mode);
5838 case BUILT_IN_BZERO:
5839 target = expand_builtin_bzero (arglist);
5844 case BUILT_IN_STRCMP:
5845 target = expand_builtin_strcmp (exp, target, mode);
5850 case BUILT_IN_STRNCMP:
5851 target = expand_builtin_strncmp (exp, target, mode);
5857 case BUILT_IN_MEMCMP:
5858 target = expand_builtin_memcmp (exp, arglist, target, mode);
5863 case BUILT_IN_SETJMP:
5864 target = expand_builtin_setjmp (arglist, target);
5869 /* __builtin_longjmp is passed a pointer to an array of five words.
5870 It's similar to the C library longjmp function but works with
5871 __builtin_setjmp above. */
5872 case BUILT_IN_LONGJMP:
5873 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5877 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5879 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5880 NULL_RTX, VOIDmode, 0);
5882 if (value != const1_rtx)
5884 error ("__builtin_longjmp second argument must be 1");
5888 expand_builtin_longjmp (buf_addr, value);
5892 case BUILT_IN_NONLOCAL_GOTO:
5893 target = expand_builtin_nonlocal_goto (arglist);
5898 /* This updates the setjmp buffer that is its argument with the value
5899 of the current stack pointer. */
5900 case BUILT_IN_UPDATE_SETJMP_BUF:
5901 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5904 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5906 expand_builtin_update_setjmp_buf (buf_addr);
5912 expand_builtin_trap ();
5915 case BUILT_IN_PRINTF:
5916 target = expand_builtin_printf (arglist, target, mode, false);
5921 case BUILT_IN_PRINTF_UNLOCKED:
5922 target = expand_builtin_printf (arglist, target, mode, true);
5927 case BUILT_IN_FPUTS:
5928 target = expand_builtin_fputs (arglist, target, false);
5932 case BUILT_IN_FPUTS_UNLOCKED:
5933 target = expand_builtin_fputs (arglist, target, true);
5938 case BUILT_IN_FPRINTF:
5939 target = expand_builtin_fprintf (arglist, target, mode, false);
5944 case BUILT_IN_FPRINTF_UNLOCKED:
5945 target = expand_builtin_fprintf (arglist, target, mode, true);
5950 case BUILT_IN_SPRINTF:
5951 target = expand_builtin_sprintf (arglist, target, mode);
5956 case BUILT_IN_SIGNBIT:
5957 case BUILT_IN_SIGNBITF:
5958 case BUILT_IN_SIGNBITL:
5959 target = expand_builtin_signbit (exp, target);
5964 /* Various hooks for the DWARF 2 __throw routine. */
5965 case BUILT_IN_UNWIND_INIT:
5966 expand_builtin_unwind_init ();
5968 case BUILT_IN_DWARF_CFA:
5969 return virtual_cfa_rtx;
5970 #ifdef DWARF2_UNWIND_INFO
5971 case BUILT_IN_DWARF_SP_COLUMN:
5972 return expand_builtin_dwarf_sp_column ();
5973 case BUILT_IN_INIT_DWARF_REG_SIZES:
5974 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5977 case BUILT_IN_FROB_RETURN_ADDR:
5978 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5979 case BUILT_IN_EXTRACT_RETURN_ADDR:
5980 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5981 case BUILT_IN_EH_RETURN:
5982 expand_builtin_eh_return (TREE_VALUE (arglist),
5983 TREE_VALUE (TREE_CHAIN (arglist)));
5985 #ifdef EH_RETURN_DATA_REGNO
5986 case BUILT_IN_EH_RETURN_DATA_REGNO:
5987 return expand_builtin_eh_return_data_regno (arglist);
5989 case BUILT_IN_EXTEND_POINTER:
5990 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
5992 case BUILT_IN_VA_START:
5993 case BUILT_IN_STDARG_START:
5994 return expand_builtin_va_start (arglist);
5995 case BUILT_IN_VA_END:
5996 return expand_builtin_va_end (arglist);
5997 case BUILT_IN_VA_COPY:
5998 return expand_builtin_va_copy (arglist);
5999 case BUILT_IN_EXPECT:
6000 return expand_builtin_expect (arglist, target);
6001 case BUILT_IN_PREFETCH:
6002 expand_builtin_prefetch (arglist);
6005 case BUILT_IN_PROFILE_FUNC_ENTER:
6006 return expand_builtin_profile_func (false);
6007 case BUILT_IN_PROFILE_FUNC_EXIT:
6008 return expand_builtin_profile_func (true);
6010 case BUILT_IN_INIT_TRAMPOLINE:
6011 return expand_builtin_init_trampoline (arglist);
6012 case BUILT_IN_ADJUST_TRAMPOLINE:
6013 return expand_builtin_adjust_trampoline (arglist);
6016 case BUILT_IN_EXECL:
6017 case BUILT_IN_EXECV:
6018 case BUILT_IN_EXECLP:
6019 case BUILT_IN_EXECLE:
6020 case BUILT_IN_EXECVP:
6021 case BUILT_IN_EXECVE:
6022 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6027 default: /* just do library call, if unknown builtin */
6028 if (!DECL_ASSEMBLER_NAME_SET_P (fndecl))
6029 error ("built-in function `%s' not currently supported",
6030 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
6033 /* The switch statement above can drop through to cause the function
6034 to be called normally. */
6035 return expand_call (exp, target, ignore);
6038 /* Determine whether a tree node represents a call to a built-in
6039 function. If the tree T is a call to a built-in function with
6040 the right number of arguments of the appropriate types, return
6041 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6042 Otherwise the return value is END_BUILTINS. */
6044 enum built_in_function
6045 builtin_mathfn_code (tree t)
6047 tree fndecl, arglist, parmlist;
6048 tree argtype, parmtype;
6050 if (TREE_CODE (t) != CALL_EXPR
6051 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6052 return END_BUILTINS;
6054 fndecl = get_callee_fndecl (t);
6055 if (fndecl == NULL_TREE
6056 || TREE_CODE (fndecl) != FUNCTION_DECL
6057 || ! DECL_BUILT_IN (fndecl)
6058 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6059 return END_BUILTINS;
6061 arglist = TREE_OPERAND (t, 1);
6062 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6063 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6065 /* If a function doesn't take a variable number of arguments,
6066 the last element in the list will have type `void'. */
6067 parmtype = TREE_VALUE (parmlist);
6068 if (VOID_TYPE_P (parmtype))
6071 return END_BUILTINS;
6072 return DECL_FUNCTION_CODE (fndecl);
6076 return END_BUILTINS;
6078 argtype = TREE_TYPE (TREE_VALUE (arglist));
6080 if (SCALAR_FLOAT_TYPE_P (parmtype))
6082 if (! SCALAR_FLOAT_TYPE_P (argtype))
6083 return END_BUILTINS;
6085 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6087 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6088 return END_BUILTINS;
6090 else if (POINTER_TYPE_P (parmtype))
6092 if (! POINTER_TYPE_P (argtype))
6093 return END_BUILTINS;
6095 else if (INTEGRAL_TYPE_P (parmtype))
6097 if (! INTEGRAL_TYPE_P (argtype))
6098 return END_BUILTINS;
6101 return END_BUILTINS;
6103 arglist = TREE_CHAIN (arglist);
6106 /* Variable-length argument list. */
6107 return DECL_FUNCTION_CODE (fndecl);
6110 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6111 constant. ARGLIST is the argument list of the call. */
6114 fold_builtin_constant_p (tree arglist)
6119 arglist = TREE_VALUE (arglist);
6121 /* We return 1 for a numeric type that's known to be a constant
6122 value at compile-time or for an aggregate type that's a
6123 literal constant. */
6124 STRIP_NOPS (arglist);
6126 /* If we know this is a constant, emit the constant of one. */
6127 if (TREE_CODE_CLASS (TREE_CODE (arglist)) == 'c'
6128 || (TREE_CODE (arglist) == CONSTRUCTOR
6129 && TREE_CONSTANT (arglist))
6130 || (TREE_CODE (arglist) == ADDR_EXPR
6131 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
6132 return integer_one_node;
6134 /* If this expression has side effects, show we don't know it to be a
6135 constant. Likewise if it's a pointer or aggregate type since in
6136 those case we only want literals, since those are only optimized
6137 when generating RTL, not later.
6138 And finally, if we are compiling an initializer, not code, we
6139 need to return a definite result now; there's not going to be any
6140 more optimization done. */
6141 if (TREE_SIDE_EFFECTS (arglist)
6142 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
6143 || POINTER_TYPE_P (TREE_TYPE (arglist))
6145 return integer_zero_node;
6150 /* Fold a call to __builtin_expect, if we expect that a comparison against
6151 the argument will fold to a constant. In practice, this means a true
6152 constant or the address of a non-weak symbol. ARGLIST is the argument
6153 list of the call. */
6156 fold_builtin_expect (tree arglist)
6163 arg = TREE_VALUE (arglist);
6165 /* If the argument isn't invariant, then there's nothing we can do. */
6166 if (!TREE_INVARIANT (arg))
6169 /* If we're looking at an address of a weak decl, then do not fold. */
6172 if (TREE_CODE (inner) == ADDR_EXPR)
6176 inner = TREE_OPERAND (inner, 0);
6178 while (TREE_CODE (inner) == COMPONENT_REF
6179 || TREE_CODE (inner) == ARRAY_REF);
6180 if (DECL_P (inner) && DECL_WEAK (inner))
6184 /* Otherwise, ARG already has the proper type for the return value. */
6188 /* Fold a call to __builtin_classify_type. */
6191 fold_builtin_classify_type (tree arglist)
6194 return build_int_2 (no_type_class, 0);
6196 return build_int_2 (type_to_class (TREE_TYPE (TREE_VALUE (arglist))), 0);
6199 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6202 fold_builtin_inf (tree type, int warn)
6204 REAL_VALUE_TYPE real;
6206 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6207 warning ("target format does not support infinity");
6210 return build_real (type, real);
6213 /* Fold a call to __builtin_nan or __builtin_nans. */
6216 fold_builtin_nan (tree arglist, tree type, int quiet)
6218 REAL_VALUE_TYPE real;
6221 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6223 str = c_getstr (TREE_VALUE (arglist));
6227 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6230 return build_real (type, real);
6233 /* Return true if the floating point expression T has an integer value.
6234 We also allow +Inf, -Inf and NaN to be considered integer values. */
6237 integer_valued_real_p (tree t)
6239 switch (TREE_CODE (t))
6246 case NON_LVALUE_EXPR:
6247 return integer_valued_real_p (TREE_OPERAND (t, 0));
6252 return integer_valued_real_p (TREE_OPERAND (t, 1));
6259 return integer_valued_real_p (TREE_OPERAND (t, 0))
6260 && integer_valued_real_p (TREE_OPERAND (t, 1));
6263 return integer_valued_real_p (TREE_OPERAND (t, 1))
6264 && integer_valued_real_p (TREE_OPERAND (t, 2));
6267 if (! TREE_CONSTANT_OVERFLOW (t))
6269 REAL_VALUE_TYPE c, cint;
6271 c = TREE_REAL_CST (t);
6272 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6273 return real_identical (&c, &cint);
6278 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6279 if (TREE_CODE (type) == INTEGER_TYPE)
6281 if (TREE_CODE (type) == REAL_TYPE)
6282 return integer_valued_real_p (TREE_OPERAND (t, 0));
6287 switch (builtin_mathfn_code (t))
6290 case BUILT_IN_CEILF:
6291 case BUILT_IN_CEILL:
6292 case BUILT_IN_FLOOR:
6293 case BUILT_IN_FLOORF:
6294 case BUILT_IN_FLOORL:
6295 case BUILT_IN_NEARBYINT:
6296 case BUILT_IN_NEARBYINTF:
6297 case BUILT_IN_NEARBYINTL:
6299 case BUILT_IN_RINTF:
6300 case BUILT_IN_RINTL:
6301 case BUILT_IN_ROUND:
6302 case BUILT_IN_ROUNDF:
6303 case BUILT_IN_ROUNDL:
6304 case BUILT_IN_TRUNC:
6305 case BUILT_IN_TRUNCF:
6306 case BUILT_IN_TRUNCL:
6320 /* EXP is assumed to be builtin call where truncation can be propagated
6321 across (for instance floor((double)f) == (double)floorf (f).
6322 Do the transformation. */
6325 fold_trunc_transparent_mathfn (tree exp)
6327 tree fndecl = get_callee_fndecl (exp);
6328 tree arglist = TREE_OPERAND (exp, 1);
6329 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6332 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6335 arg = TREE_VALUE (arglist);
6336 /* Integer rounding functions are idempotent. */
6337 if (fcode == builtin_mathfn_code (arg))
6340 /* If argument is already integer valued, and we don't need to worry
6341 about setting errno, there's no need to perform rounding. */
6342 if (! flag_errno_math && integer_valued_real_p (arg))
6347 tree arg0 = strip_float_extensions (arg);
6348 tree ftype = TREE_TYPE (exp);
6349 tree newtype = TREE_TYPE (arg0);
6352 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6353 && (decl = mathfn_built_in (newtype, fcode)))
6356 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6357 return fold_convert (ftype,
6358 build_function_call_expr (decl, arglist));
6364 /* EXP is assumed to be builtin call which can narrow the FP type of
6365 the argument, for instance lround((double)f) -> lroundf (f). */
6368 fold_fixed_mathfn (tree exp)
6370 tree fndecl = get_callee_fndecl (exp);
6371 tree arglist = TREE_OPERAND (exp, 1);
6372 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6375 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6378 arg = TREE_VALUE (arglist);
6380 /* If argument is already integer valued, and we don't need to worry
6381 about setting errno, there's no need to perform rounding. */
6382 if (! flag_errno_math && integer_valued_real_p (arg))
6383 return fold (build1 (FIX_TRUNC_EXPR, TREE_TYPE (exp), arg));
6387 tree ftype = TREE_TYPE (arg);
6388 tree arg0 = strip_float_extensions (arg);
6389 tree newtype = TREE_TYPE (arg0);
6392 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6393 && (decl = mathfn_built_in (newtype, fcode)))
6396 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6397 return build_function_call_expr (decl, arglist);
6403 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6404 is the argument list and TYPE is the return type. Return
6405 NULL_TREE if no if no simplification can be made. */
6408 fold_builtin_cabs (tree arglist, tree type)
6412 if (!arglist || TREE_CHAIN (arglist))
6415 arg = TREE_VALUE (arglist);
6416 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6417 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6420 /* Evaluate cabs of a constant at compile-time. */
6421 if (flag_unsafe_math_optimizations
6422 && TREE_CODE (arg) == COMPLEX_CST
6423 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6424 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6425 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6426 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6428 REAL_VALUE_TYPE r, i;
6430 r = TREE_REAL_CST (TREE_REALPART (arg));
6431 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6433 real_arithmetic (&r, MULT_EXPR, &r, &r);
6434 real_arithmetic (&i, MULT_EXPR, &i, &i);
6435 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6436 if (real_sqrt (&r, TYPE_MODE (type), &r)
6437 || ! flag_trapping_math)
6438 return build_real (type, r);
6441 /* If either part is zero, cabs is fabs of the other. */
6442 if (TREE_CODE (arg) == COMPLEX_EXPR
6443 && real_zerop (TREE_OPERAND (arg, 0)))
6444 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
6445 if (TREE_CODE (arg) == COMPLEX_EXPR
6446 && real_zerop (TREE_OPERAND (arg, 1)))
6447 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
6449 if (flag_unsafe_math_optimizations)
6451 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6453 if (sqrtfn != NULL_TREE)
6455 tree rpart, ipart, result, arglist;
6457 arg = builtin_save_expr (arg);
6459 rpart = fold (build1 (REALPART_EXPR, type, arg));
6460 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
6462 rpart = builtin_save_expr (rpart);
6463 ipart = builtin_save_expr (ipart);
6465 result = fold (build2 (PLUS_EXPR, type,
6466 fold (build2 (MULT_EXPR, type,
6468 fold (build2 (MULT_EXPR, type,
6471 arglist = build_tree_list (NULL_TREE, result);
6472 return build_function_call_expr (sqrtfn, arglist);
6479 /* Fold function call to builtin trunc, truncf or truncl. Return
6480 NULL_TREE if no simplification can be made. */
6483 fold_builtin_trunc (tree exp)
6485 tree arglist = TREE_OPERAND (exp, 1);
6488 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6491 /* Optimize trunc of constant value. */
6492 arg = TREE_VALUE (arglist);
6493 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6495 REAL_VALUE_TYPE r, x;
6496 tree type = TREE_TYPE (exp);
6498 x = TREE_REAL_CST (arg);
6499 real_trunc (&r, TYPE_MODE (type), &x);
6500 return build_real (type, r);
6503 return fold_trunc_transparent_mathfn (exp);
6506 /* Fold function call to builtin floor, floorf or floorl. Return
6507 NULL_TREE if no simplification can be made. */
6510 fold_builtin_floor (tree exp)
6512 tree arglist = TREE_OPERAND (exp, 1);
6515 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6518 /* Optimize floor of constant value. */
6519 arg = TREE_VALUE (arglist);
6520 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6524 x = TREE_REAL_CST (arg);
6525 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6527 tree type = TREE_TYPE (exp);
6530 real_floor (&r, TYPE_MODE (type), &x);
6531 return build_real (type, r);
6535 return fold_trunc_transparent_mathfn (exp);
6538 /* Fold function call to builtin ceil, ceilf or ceill. Return
6539 NULL_TREE if no simplification can be made. */
6542 fold_builtin_ceil (tree exp)
6544 tree arglist = TREE_OPERAND (exp, 1);
6547 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6550 /* Optimize ceil of constant value. */
6551 arg = TREE_VALUE (arglist);
6552 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6556 x = TREE_REAL_CST (arg);
6557 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6559 tree type = TREE_TYPE (exp);
6562 real_ceil (&r, TYPE_MODE (type), &x);
6563 return build_real (type, r);
6567 return fold_trunc_transparent_mathfn (exp);
6570 /* Fold function call to builtin round, roundf or roundl. Return
6571 NULL_TREE if no simplification can be made. */
6574 fold_builtin_round (tree exp)
6576 tree arglist = TREE_OPERAND (exp, 1);
6579 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6582 /* Optimize round of constant value. */
6583 arg = TREE_VALUE (arglist);
6584 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6588 x = TREE_REAL_CST (arg);
6589 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6591 tree type = TREE_TYPE (exp);
6594 real_round (&r, TYPE_MODE (type), &x);
6595 return build_real (type, r);
6599 return fold_trunc_transparent_mathfn (exp);
6602 /* Fold function call to builtin lround, lroundf or lroundl (or the
6603 corresponding long long versions). Return NULL_TREE if no
6604 simplification can be made. */
6607 fold_builtin_lround (tree exp)
6609 tree arglist = TREE_OPERAND (exp, 1);
6612 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6615 /* Optimize lround of constant value. */
6616 arg = TREE_VALUE (arglist);
6617 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6619 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
6621 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
6623 tree itype = TREE_TYPE (exp), ftype = TREE_TYPE (arg), result;
6624 HOST_WIDE_INT hi, lo;
6627 real_round (&r, TYPE_MODE (ftype), &x);
6628 REAL_VALUE_TO_INT (&lo, &hi, r);
6629 result = build_int_2 (lo, hi);
6630 if (int_fits_type_p (result, itype))
6631 return fold_convert (itype, result);
6635 return fold_fixed_mathfn (exp);
6638 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
6639 and their long and long long variants (i.e. ffsl and ffsll).
6640 Return NULL_TREE if no simplification can be made. */
6643 fold_builtin_bitop (tree exp)
6645 tree fndecl = get_callee_fndecl (exp);
6646 tree arglist = TREE_OPERAND (exp, 1);
6649 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6652 /* Optimize for constant argument. */
6653 arg = TREE_VALUE (arglist);
6654 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6656 HOST_WIDE_INT hi, width, result;
6657 unsigned HOST_WIDE_INT lo;
6660 type = TREE_TYPE (arg);
6661 width = TYPE_PRECISION (type);
6662 lo = TREE_INT_CST_LOW (arg);
6664 /* Clear all the bits that are beyond the type's precision. */
6665 if (width > HOST_BITS_PER_WIDE_INT)
6667 hi = TREE_INT_CST_HIGH (arg);
6668 if (width < 2 * HOST_BITS_PER_WIDE_INT)
6669 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
6674 if (width < HOST_BITS_PER_WIDE_INT)
6675 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
6678 switch (DECL_FUNCTION_CODE (fndecl))
6682 case BUILT_IN_FFSLL:
6684 result = exact_log2 (lo & -lo) + 1;
6686 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
6693 case BUILT_IN_CLZLL:
6695 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
6697 result = width - floor_log2 (lo) - 1;
6698 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6704 case BUILT_IN_CTZLL:
6706 result = exact_log2 (lo & -lo);
6708 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
6709 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6713 case BUILT_IN_POPCOUNT:
6714 case BUILT_IN_POPCOUNTL:
6715 case BUILT_IN_POPCOUNTLL:
6718 result++, lo &= lo - 1;
6720 result++, hi &= hi - 1;
6723 case BUILT_IN_PARITY:
6724 case BUILT_IN_PARITYL:
6725 case BUILT_IN_PARITYLL:
6728 result++, lo &= lo - 1;
6730 result++, hi &= hi - 1;
6738 t = build_int_2 (result, 0);
6739 TREE_TYPE (t) = TREE_TYPE (exp);
6746 /* Return true if EXPR is the real constant contained in VALUE. */
6749 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
6753 return ((TREE_CODE (expr) == REAL_CST
6754 && ! TREE_CONSTANT_OVERFLOW (expr)
6755 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
6756 || (TREE_CODE (expr) == COMPLEX_CST
6757 && real_dconstp (TREE_REALPART (expr), value)
6758 && real_zerop (TREE_IMAGPART (expr))));
6761 /* A subroutine of fold_builtin to fold the various logarithmic
6762 functions. EXP is the CALL_EXPR of a call to a builtin logN
6763 function. VALUE is the base of the logN function. */
6766 fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
6768 tree arglist = TREE_OPERAND (exp, 1);
6770 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6772 tree fndecl = get_callee_fndecl (exp);
6773 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6774 tree arg = TREE_VALUE (arglist);
6775 const enum built_in_function fcode = builtin_mathfn_code (arg);
6777 /* Optimize logN(1.0) = 0.0. */
6778 if (real_onep (arg))
6779 return build_real (type, dconst0);
6781 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
6782 exactly, then only do this if flag_unsafe_math_optimizations. */
6783 if (exact_real_truncate (TYPE_MODE (type), value)
6784 || flag_unsafe_math_optimizations)
6786 const REAL_VALUE_TYPE value_truncate =
6787 real_value_truncate (TYPE_MODE (type), *value);
6788 if (real_dconstp (arg, &value_truncate))
6789 return build_real (type, dconst1);
6792 /* Special case, optimize logN(expN(x)) = x. */
6793 if (flag_unsafe_math_optimizations
6794 && ((value == &dconste
6795 && (fcode == BUILT_IN_EXP
6796 || fcode == BUILT_IN_EXPF
6797 || fcode == BUILT_IN_EXPL))
6798 || (value == &dconst2
6799 && (fcode == BUILT_IN_EXP2
6800 || fcode == BUILT_IN_EXP2F
6801 || fcode == BUILT_IN_EXP2L))
6802 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
6803 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6805 /* Optimize logN(func()) for various exponential functions. We
6806 want to determine the value "x" and the power "exponent" in
6807 order to transform logN(x**exponent) into exponent*logN(x). */
6808 if (flag_unsafe_math_optimizations)
6810 tree exponent = 0, x = 0;
6817 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
6818 x = build_real (type,
6819 real_value_truncate (TYPE_MODE (type), dconste));
6820 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6823 case BUILT_IN_EXP2F:
6824 case BUILT_IN_EXP2L:
6825 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
6826 x = build_real (type, dconst2);
6827 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6829 case BUILT_IN_EXP10:
6830 case BUILT_IN_EXP10F:
6831 case BUILT_IN_EXP10L:
6832 case BUILT_IN_POW10:
6833 case BUILT_IN_POW10F:
6834 case BUILT_IN_POW10L:
6835 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
6836 x = build_real (type, dconst10);
6837 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6840 case BUILT_IN_SQRTF:
6841 case BUILT_IN_SQRTL:
6842 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
6843 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6844 exponent = build_real (type, dconsthalf);
6847 case BUILT_IN_CBRTF:
6848 case BUILT_IN_CBRTL:
6849 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
6850 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6851 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
6857 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
6858 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6859 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6865 /* Now perform the optimization. */
6869 arglist = build_tree_list (NULL_TREE, x);
6870 logfn = build_function_call_expr (fndecl, arglist);
6871 return fold (build2 (MULT_EXPR, type, exponent, logfn));
6879 /* A subroutine of fold_builtin to fold the various exponent
6880 functions. EXP is the CALL_EXPR of a call to a builtin function.
6881 VALUE is the value which will be raised to a power. */
6884 fold_builtin_exponent (tree exp, const REAL_VALUE_TYPE *value)
6886 tree arglist = TREE_OPERAND (exp, 1);
6888 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6890 tree fndecl = get_callee_fndecl (exp);
6891 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6892 tree arg = TREE_VALUE (arglist);
6894 /* Optimize exp*(0.0) = 1.0. */
6895 if (real_zerop (arg))
6896 return build_real (type, dconst1);
6898 /* Optimize expN(1.0) = N. */
6899 if (real_onep (arg))
6901 REAL_VALUE_TYPE cst;
6903 real_convert (&cst, TYPE_MODE (type), value);
6904 return build_real (type, cst);
6907 /* Attempt to evaluate expN(integer) at compile-time. */
6908 if (flag_unsafe_math_optimizations
6909 && TREE_CODE (arg) == REAL_CST
6910 && ! TREE_CONSTANT_OVERFLOW (arg))
6912 REAL_VALUE_TYPE cint;
6916 c = TREE_REAL_CST (arg);
6917 n = real_to_integer (&c);
6918 real_from_integer (&cint, VOIDmode, n,
6920 if (real_identical (&c, &cint))
6924 real_powi (&x, TYPE_MODE (type), value, n);
6925 return build_real (type, x);
6929 /* Optimize expN(logN(x)) = x. */
6930 if (flag_unsafe_math_optimizations)
6932 const enum built_in_function fcode = builtin_mathfn_code (arg);
6934 if ((value == &dconste
6935 && (fcode == BUILT_IN_LOG
6936 || fcode == BUILT_IN_LOGF
6937 || fcode == BUILT_IN_LOGL))
6938 || (value == &dconst2
6939 && (fcode == BUILT_IN_LOG2
6940 || fcode == BUILT_IN_LOG2F
6941 || fcode == BUILT_IN_LOG2L))
6942 || (value == &dconst10
6943 && (fcode == BUILT_IN_LOG10
6944 || fcode == BUILT_IN_LOG10F
6945 || fcode == BUILT_IN_LOG10L)))
6946 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6953 /* Fold function call to builtin memcpy. Return
6954 NULL_TREE if no simplification can be made. */
6957 fold_builtin_memcpy (tree exp)
6959 tree arglist = TREE_OPERAND (exp, 1);
6960 tree dest, src, len;
6962 if (!validate_arglist (arglist,
6963 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6966 dest = TREE_VALUE (arglist);
6967 src = TREE_VALUE (TREE_CHAIN (arglist));
6968 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6970 /* If the LEN parameter is zero, return DEST. */
6971 if (integer_zerop (len))
6972 return omit_one_operand (TREE_TYPE (exp), dest, src);
6974 /* If SRC and DEST are the same (and not volatile), return DEST. */
6975 if (operand_equal_p (src, dest, 0))
6976 return omit_one_operand (TREE_TYPE (exp), dest, len);
6981 /* Fold function call to builtin mempcpy. Return
6982 NULL_TREE if no simplification can be made. */
6985 fold_builtin_mempcpy (tree exp)
6987 tree arglist = TREE_OPERAND (exp, 1);
6988 tree dest, src, len;
6990 if (!validate_arglist (arglist,
6991 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6994 dest = TREE_VALUE (arglist);
6995 src = TREE_VALUE (TREE_CHAIN (arglist));
6996 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6998 /* If the LEN parameter is zero, return DEST. */
6999 if (integer_zerop (len))
7000 return omit_one_operand (TREE_TYPE (exp), dest, src);
7002 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
7003 if (operand_equal_p (src, dest, 0))
7005 tree temp = fold_convert (TREE_TYPE (dest), len);
7006 temp = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp));
7007 return fold_convert (TREE_TYPE (exp), temp);
7013 /* Fold function call to builtin memmove. Return
7014 NULL_TREE if no simplification can be made. */
7017 fold_builtin_memmove (tree exp)
7019 tree arglist = TREE_OPERAND (exp, 1);
7020 tree dest, src, len;
7022 if (!validate_arglist (arglist,
7023 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7026 dest = TREE_VALUE (arglist);
7027 src = TREE_VALUE (TREE_CHAIN (arglist));
7028 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7030 /* If the LEN parameter is zero, return DEST. */
7031 if (integer_zerop (len))
7032 return omit_one_operand (TREE_TYPE (exp), dest, src);
7034 /* If SRC and DEST are the same (and not volatile), return DEST. */
7035 if (operand_equal_p (src, dest, 0))
7036 return omit_one_operand (TREE_TYPE (exp), dest, len);
7041 /* Fold function call to builtin strcpy. Return
7042 NULL_TREE if no simplification can be made. */
7045 fold_builtin_strcpy (tree exp)
7047 tree arglist = TREE_OPERAND (exp, 1);
7050 if (!validate_arglist (arglist,
7051 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7054 dest = TREE_VALUE (arglist);
7055 src = TREE_VALUE (TREE_CHAIN (arglist));
7057 /* If SRC and DEST are the same (and not volatile), return DEST. */
7058 if (operand_equal_p (src, dest, 0))
7059 return fold_convert (TREE_TYPE (exp), dest);
7064 /* Fold function call to builtin strncpy. Return
7065 NULL_TREE if no simplification can be made. */
7068 fold_builtin_strncpy (tree exp)
7070 tree arglist = TREE_OPERAND (exp, 1);
7071 tree dest, src, len;
7073 if (!validate_arglist (arglist,
7074 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7077 dest = TREE_VALUE (arglist);
7078 src = TREE_VALUE (TREE_CHAIN (arglist));
7079 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7081 /* If the LEN parameter is zero, return DEST. */
7082 if (integer_zerop (len))
7083 return omit_one_operand (TREE_TYPE (exp), dest, src);
7088 /* Fold function call to builtin memcmp. Return
7089 NULL_TREE if no simplification can be made. */
7092 fold_builtin_memcmp (tree exp)
7094 tree arglist = TREE_OPERAND (exp, 1);
7095 tree arg1, arg2, len;
7097 if (!validate_arglist (arglist,
7098 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7101 arg1 = TREE_VALUE (arglist);
7102 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7103 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7105 /* If the LEN parameter is zero, return zero. */
7106 if (integer_zerop (len))
7108 tree temp = omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg2);
7109 return omit_one_operand (TREE_TYPE (exp), temp, arg1);
7112 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7113 if (operand_equal_p (arg1, arg2, 0))
7114 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, len);
7119 /* Fold function call to builtin strcmp. Return
7120 NULL_TREE if no simplification can be made. */
7123 fold_builtin_strcmp (tree exp)
7125 tree arglist = TREE_OPERAND (exp, 1);
7127 const char *p1, *p2;
7129 if (!validate_arglist (arglist,
7130 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7133 arg1 = TREE_VALUE (arglist);
7134 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7136 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7137 if (operand_equal_p (arg1, arg2, 0))
7138 return fold_convert (TREE_TYPE (exp), integer_zero_node);
7140 p1 = c_getstr (arg1);
7141 p2 = c_getstr (arg2);
7146 const int i = strcmp (p1, p2);
7148 temp = integer_minus_one_node;
7150 temp = integer_one_node;
7152 temp = integer_zero_node;
7153 return fold_convert (TREE_TYPE (exp), temp);
7159 /* Fold function call to builtin strncmp. Return
7160 NULL_TREE if no simplification can be made. */
7163 fold_builtin_strncmp (tree exp)
7165 tree arglist = TREE_OPERAND (exp, 1);
7166 tree arg1, arg2, len;
7167 const char *p1, *p2;
7169 if (!validate_arglist (arglist,
7170 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7173 arg1 = TREE_VALUE (arglist);
7174 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7175 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7177 /* If the LEN parameter is zero, return zero. */
7178 if (integer_zerop (len))
7180 tree temp = omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg2);
7181 return omit_one_operand (TREE_TYPE (exp), temp, arg1);
7184 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7185 if (operand_equal_p (arg1, arg2, 0))
7186 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, len);
7188 p1 = c_getstr (arg1);
7189 p2 = c_getstr (arg2);
7191 if (host_integerp (len, 1) && p1 && p2)
7194 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
7196 temp = integer_minus_one_node;
7198 temp = integer_one_node;
7200 temp = integer_zero_node;
7201 return fold_convert (TREE_TYPE (exp), temp);
7207 /* Fold function call to builtin signbit, signbitf or signbitl. Return
7208 NULL_TREE if no simplification can be made. */
7211 fold_builtin_signbit (tree exp)
7213 tree arglist = TREE_OPERAND (exp, 1);
7216 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7219 arg = TREE_VALUE (arglist);
7221 /* If ARG is a compile-time constant, determine the result. */
7222 if (TREE_CODE (arg) == REAL_CST
7223 && !TREE_CONSTANT_OVERFLOW (arg))
7227 c = TREE_REAL_CST (arg);
7228 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
7229 return fold_convert (TREE_TYPE (exp), temp);
7232 /* If ARG is non-negative, the result is always zero. */
7233 if (tree_expr_nonnegative_p (arg))
7234 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg);
7236 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
7237 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
7238 return fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
7239 build_real (TREE_TYPE (arg), dconst0)));
7244 /* Fold a call to builtin isascii. */
7247 fold_builtin_isascii (tree arglist)
7249 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7253 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7254 tree arg = TREE_VALUE (arglist);
7256 arg = fold (build2 (EQ_EXPR, integer_type_node,
7257 build2 (BIT_AND_EXPR, integer_type_node, arg,
7258 build_int_2 (~ (unsigned HOST_WIDE_INT) 0x7f,
7259 ~ (HOST_WIDE_INT) 0)),
7260 integer_zero_node));
7262 if (in_gimple_form && !TREE_CONSTANT (arg))
7269 /* Fold a call to builtin toascii. */
7272 fold_builtin_toascii (tree arglist)
7274 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7278 /* Transform toascii(c) -> (c & 0x7f). */
7279 tree arg = TREE_VALUE (arglist);
7281 return fold (build2 (BIT_AND_EXPR, integer_type_node, arg,
7282 build_int_2 (0x7f, 0)));
7286 /* Fold a call to builtin isdigit. */
7289 fold_builtin_isdigit (tree arglist)
7291 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7295 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7296 /* According to the C standard, isdigit is unaffected by locale. */
7297 tree arg = TREE_VALUE (arglist);
7298 arg = fold_convert (unsigned_type_node, arg);
7299 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
7300 fold_convert (unsigned_type_node,
7301 build_int_2 (TARGET_DIGIT0, 0)));
7302 arg = build2 (LE_EXPR, integer_type_node, arg,
7303 fold_convert (unsigned_type_node, build_int_2 (9, 0)));
7305 if (in_gimple_form && !TREE_CONSTANT (arg))
7312 /* Used by constant folding to eliminate some builtin calls early. EXP is
7313 the CALL_EXPR of a call to a builtin function. */
7316 fold_builtin_1 (tree exp)
7318 tree fndecl = get_callee_fndecl (exp);
7319 tree arglist = TREE_OPERAND (exp, 1);
7320 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7322 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7325 switch (DECL_FUNCTION_CODE (fndecl))
7327 case BUILT_IN_CONSTANT_P:
7328 return fold_builtin_constant_p (arglist);
7330 case BUILT_IN_EXPECT:
7331 return fold_builtin_expect (arglist);
7333 case BUILT_IN_CLASSIFY_TYPE:
7334 return fold_builtin_classify_type (arglist);
7336 case BUILT_IN_STRLEN:
7337 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
7339 tree len = c_strlen (TREE_VALUE (arglist), 0);
7342 /* Convert from the internal "sizetype" type to "size_t". */
7344 len = fold_convert (size_type_node, len);
7351 case BUILT_IN_FABSF:
7352 case BUILT_IN_FABSL:
7353 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7354 return fold (build1 (ABS_EXPR, type, TREE_VALUE (arglist)));
7358 case BUILT_IN_CABSF:
7359 case BUILT_IN_CABSL:
7360 return fold_builtin_cabs (arglist, type);
7363 case BUILT_IN_SQRTF:
7364 case BUILT_IN_SQRTL:
7365 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7367 enum built_in_function fcode;
7368 tree arg = TREE_VALUE (arglist);
7370 /* Optimize sqrt of constant value. */
7371 if (TREE_CODE (arg) == REAL_CST
7372 && ! TREE_CONSTANT_OVERFLOW (arg))
7374 REAL_VALUE_TYPE r, x;
7376 x = TREE_REAL_CST (arg);
7377 if (real_sqrt (&r, TYPE_MODE (type), &x)
7378 || (!flag_trapping_math && !flag_errno_math))
7379 return build_real (type, r);
7382 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7383 fcode = builtin_mathfn_code (arg);
7384 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7386 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7387 arg = fold (build2 (MULT_EXPR, type,
7388 TREE_VALUE (TREE_OPERAND (arg, 1)),
7389 build_real (type, dconsthalf)));
7390 arglist = build_tree_list (NULL_TREE, arg);
7391 return build_function_call_expr (expfn, arglist);
7394 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7395 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7397 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7401 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7403 /* The inner root was either sqrt or cbrt. */
7404 REAL_VALUE_TYPE dconstroot =
7405 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7407 /* Adjust for the outer root. */
7408 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7409 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7410 tree_root = build_real (type, dconstroot);
7411 arglist = tree_cons (NULL_TREE, arg0,
7412 build_tree_list (NULL_TREE, tree_root));
7413 return build_function_call_expr (powfn, arglist);
7417 /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5). */
7418 if (flag_unsafe_math_optimizations
7419 && (fcode == BUILT_IN_POW
7420 || fcode == BUILT_IN_POWF
7421 || fcode == BUILT_IN_POWL))
7423 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7424 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7425 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7426 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
7427 build_real (type, dconsthalf)));
7428 arglist = tree_cons (NULL_TREE, arg0,
7429 build_tree_list (NULL_TREE, narg1));
7430 return build_function_call_expr (powfn, arglist);
7436 case BUILT_IN_CBRTF:
7437 case BUILT_IN_CBRTL:
7438 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7440 tree arg = TREE_VALUE (arglist);
7441 const enum built_in_function fcode = builtin_mathfn_code (arg);
7443 /* Optimize cbrt of constant value. */
7444 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
7447 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7448 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7450 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7451 const REAL_VALUE_TYPE third_trunc =
7452 real_value_truncate (TYPE_MODE (type), dconstthird);
7453 arg = fold (build2 (MULT_EXPR, type,
7454 TREE_VALUE (TREE_OPERAND (arg, 1)),
7455 build_real (type, third_trunc)));
7456 arglist = build_tree_list (NULL_TREE, arg);
7457 return build_function_call_expr (expfn, arglist);
7460 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7461 /* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
7462 x is negative pow will error but cbrt won't. */
7463 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
7465 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7469 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7471 REAL_VALUE_TYPE dconstroot = dconstthird;
7473 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7474 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7475 tree_root = build_real (type, dconstroot);
7476 arglist = tree_cons (NULL_TREE, arg0,
7477 build_tree_list (NULL_TREE, tree_root));
7478 return build_function_call_expr (powfn, arglist);
7488 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7490 tree arg = TREE_VALUE (arglist);
7492 /* Optimize sin(0.0) = 0.0. */
7493 if (real_zerop (arg))
7501 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7503 tree arg = TREE_VALUE (arglist);
7505 /* Optimize cos(0.0) = 1.0. */
7506 if (real_zerop (arg))
7507 return build_real (type, dconst1);
7509 /* Optimize cos(-x) into cos(x). */
7510 if (TREE_CODE (arg) == NEGATE_EXPR)
7512 tree arglist = build_tree_list (NULL_TREE,
7513 TREE_OPERAND (arg, 0));
7514 return build_function_call_expr (fndecl, arglist);
7522 return fold_builtin_exponent (exp, &dconste);
7524 case BUILT_IN_EXP2F:
7525 case BUILT_IN_EXP2L:
7526 return fold_builtin_exponent (exp, &dconst2);
7527 case BUILT_IN_EXP10:
7528 case BUILT_IN_EXP10F:
7529 case BUILT_IN_EXP10L:
7530 case BUILT_IN_POW10:
7531 case BUILT_IN_POW10F:
7532 case BUILT_IN_POW10L:
7533 return fold_builtin_exponent (exp, &dconst10);
7537 return fold_builtin_logarithm (exp, &dconste);
7540 case BUILT_IN_LOG2F:
7541 case BUILT_IN_LOG2L:
7542 return fold_builtin_logarithm (exp, &dconst2);
7544 case BUILT_IN_LOG10:
7545 case BUILT_IN_LOG10F:
7546 case BUILT_IN_LOG10L:
7547 return fold_builtin_logarithm (exp, &dconst10);
7553 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7555 enum built_in_function fcode;
7556 tree arg = TREE_VALUE (arglist);
7558 /* Optimize tan(0.0) = 0.0. */
7559 if (real_zerop (arg))
7562 /* Optimize tan(atan(x)) = x. */
7563 fcode = builtin_mathfn_code (arg);
7564 if (flag_unsafe_math_optimizations
7565 && (fcode == BUILT_IN_ATAN
7566 || fcode == BUILT_IN_ATANF
7567 || fcode == BUILT_IN_ATANL))
7568 return TREE_VALUE (TREE_OPERAND (arg, 1));
7573 case BUILT_IN_ATANF:
7574 case BUILT_IN_ATANL:
7575 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7577 tree arg = TREE_VALUE (arglist);
7579 /* Optimize atan(0.0) = 0.0. */
7580 if (real_zerop (arg))
7583 /* Optimize atan(1.0) = pi/4. */
7584 if (real_onep (arg))
7586 REAL_VALUE_TYPE cst;
7588 real_convert (&cst, TYPE_MODE (type), &dconstpi);
7589 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
7590 return build_real (type, cst);
7598 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7600 enum built_in_function fcode;
7601 tree arg0 = TREE_VALUE (arglist);
7602 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7604 /* Optimize pow(1.0,y) = 1.0. */
7605 if (real_onep (arg0))
7606 return omit_one_operand (type, build_real (type, dconst1), arg1);
7608 if (TREE_CODE (arg1) == REAL_CST
7609 && ! TREE_CONSTANT_OVERFLOW (arg1))
7612 c = TREE_REAL_CST (arg1);
7614 /* Optimize pow(x,0.0) = 1.0. */
7615 if (REAL_VALUES_EQUAL (c, dconst0))
7616 return omit_one_operand (type, build_real (type, dconst1),
7619 /* Optimize pow(x,1.0) = x. */
7620 if (REAL_VALUES_EQUAL (c, dconst1))
7623 /* Optimize pow(x,-1.0) = 1.0/x. */
7624 if (REAL_VALUES_EQUAL (c, dconstm1))
7625 return fold (build2 (RDIV_EXPR, type,
7626 build_real (type, dconst1), arg0));
7628 /* Optimize pow(x,0.5) = sqrt(x). */
7629 if (flag_unsafe_math_optimizations
7630 && REAL_VALUES_EQUAL (c, dconsthalf))
7632 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7634 if (sqrtfn != NULL_TREE)
7636 tree arglist = build_tree_list (NULL_TREE, arg0);
7637 return build_function_call_expr (sqrtfn, arglist);
7641 /* Attempt to evaluate pow at compile-time. */
7642 if (TREE_CODE (arg0) == REAL_CST
7643 && ! TREE_CONSTANT_OVERFLOW (arg0))
7645 REAL_VALUE_TYPE cint;
7648 n = real_to_integer (&c);
7649 real_from_integer (&cint, VOIDmode, n,
7651 if (real_identical (&c, &cint))
7656 x = TREE_REAL_CST (arg0);
7657 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7658 if (flag_unsafe_math_optimizations || !inexact)
7659 return build_real (type, x);
7664 /* Optimize pow(expN(x),y) = expN(x*y). */
7665 fcode = builtin_mathfn_code (arg0);
7666 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7668 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7669 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7670 arg = fold (build2 (MULT_EXPR, type, arg, arg1));
7671 arglist = build_tree_list (NULL_TREE, arg);
7672 return build_function_call_expr (expfn, arglist);
7675 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7676 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
7678 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7679 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
7680 build_real (type, dconsthalf)));
7682 arglist = tree_cons (NULL_TREE, narg0,
7683 build_tree_list (NULL_TREE, narg1));
7684 return build_function_call_expr (fndecl, arglist);
7687 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7688 if (flag_unsafe_math_optimizations
7689 && (fcode == BUILT_IN_POW
7690 || fcode == BUILT_IN_POWF
7691 || fcode == BUILT_IN_POWL))
7693 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7694 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7695 tree narg1 = fold (build2 (MULT_EXPR, type, arg01, arg1));
7696 arglist = tree_cons (NULL_TREE, arg00,
7697 build_tree_list (NULL_TREE, narg1));
7698 return build_function_call_expr (fndecl, arglist);
7706 return fold_builtin_inf (type, true);
7708 case BUILT_IN_HUGE_VAL:
7709 case BUILT_IN_HUGE_VALF:
7710 case BUILT_IN_HUGE_VALL:
7711 return fold_builtin_inf (type, false);
7716 return fold_builtin_nan (arglist, type, true);
7719 case BUILT_IN_NANSF:
7720 case BUILT_IN_NANSL:
7721 return fold_builtin_nan (arglist, type, false);
7723 case BUILT_IN_FLOOR:
7724 case BUILT_IN_FLOORF:
7725 case BUILT_IN_FLOORL:
7726 return fold_builtin_floor (exp);
7729 case BUILT_IN_CEILF:
7730 case BUILT_IN_CEILL:
7731 return fold_builtin_ceil (exp);
7733 case BUILT_IN_TRUNC:
7734 case BUILT_IN_TRUNCF:
7735 case BUILT_IN_TRUNCL:
7736 return fold_builtin_trunc (exp);
7738 case BUILT_IN_ROUND:
7739 case BUILT_IN_ROUNDF:
7740 case BUILT_IN_ROUNDL:
7741 return fold_builtin_round (exp);
7743 case BUILT_IN_NEARBYINT:
7744 case BUILT_IN_NEARBYINTF:
7745 case BUILT_IN_NEARBYINTL:
7747 case BUILT_IN_RINTF:
7748 case BUILT_IN_RINTL:
7749 return fold_trunc_transparent_mathfn (exp);
7751 case BUILT_IN_LROUND:
7752 case BUILT_IN_LROUNDF:
7753 case BUILT_IN_LROUNDL:
7754 case BUILT_IN_LLROUND:
7755 case BUILT_IN_LLROUNDF:
7756 case BUILT_IN_LLROUNDL:
7757 return fold_builtin_lround (exp);
7759 case BUILT_IN_LRINT:
7760 case BUILT_IN_LRINTF:
7761 case BUILT_IN_LRINTL:
7762 case BUILT_IN_LLRINT:
7763 case BUILT_IN_LLRINTF:
7764 case BUILT_IN_LLRINTL:
7765 return fold_fixed_mathfn (exp);
7769 case BUILT_IN_FFSLL:
7772 case BUILT_IN_CLZLL:
7775 case BUILT_IN_CTZLL:
7776 case BUILT_IN_POPCOUNT:
7777 case BUILT_IN_POPCOUNTL:
7778 case BUILT_IN_POPCOUNTLL:
7779 case BUILT_IN_PARITY:
7780 case BUILT_IN_PARITYL:
7781 case BUILT_IN_PARITYLL:
7782 return fold_builtin_bitop (exp);
7784 case BUILT_IN_MEMCPY:
7785 return fold_builtin_memcpy (exp);
7787 case BUILT_IN_MEMPCPY:
7788 return fold_builtin_mempcpy (exp);
7790 case BUILT_IN_MEMMOVE:
7791 return fold_builtin_memmove (exp);
7793 case BUILT_IN_STRCPY:
7794 return fold_builtin_strcpy (exp);
7796 case BUILT_IN_STRNCPY:
7797 return fold_builtin_strncpy (exp);
7799 case BUILT_IN_MEMCMP:
7800 return fold_builtin_memcmp (exp);
7802 case BUILT_IN_STRCMP:
7803 return fold_builtin_strcmp (exp);
7805 case BUILT_IN_STRNCMP:
7806 return fold_builtin_strncmp (exp);
7808 case BUILT_IN_SIGNBIT:
7809 case BUILT_IN_SIGNBITF:
7810 case BUILT_IN_SIGNBITL:
7811 return fold_builtin_signbit (exp);
7813 case BUILT_IN_ISASCII:
7814 return fold_builtin_isascii (arglist);
7816 case BUILT_IN_TOASCII:
7817 return fold_builtin_toascii (arglist);
7819 case BUILT_IN_ISDIGIT:
7820 return fold_builtin_isdigit (arglist);
7829 /* A wrapper function for builtin folding that prevents warnings for
7830 "statement without effect" and the like, caused by removing the
7831 call node earlier than the warning is generated. */
7834 fold_builtin (tree exp)
7836 exp = fold_builtin_1 (exp);
7839 /* ??? Don't clobber shared nodes such as integer_zero_node. */
7840 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c')
7841 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
7842 TREE_NO_WARNING (exp) = 1;
7847 /* Conveniently construct a function call expression. */
7850 build_function_call_expr (tree fn, tree arglist)
7854 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
7855 call_expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
7856 call_expr, arglist, NULL_TREE);
7857 return fold (call_expr);
7860 /* This function validates the types of a function call argument list
7861 represented as a tree chain of parameters against a specified list
7862 of tree_codes. If the last specifier is a 0, that represents an
7863 ellipses, otherwise the last specifier must be a VOID_TYPE. */
7866 validate_arglist (tree arglist, ...)
7868 enum tree_code code;
7872 va_start (ap, arglist);
7876 code = va_arg (ap, enum tree_code);
7880 /* This signifies an ellipses, any further arguments are all ok. */
7884 /* This signifies an endlink, if no arguments remain, return
7885 true, otherwise return false. */
7889 /* If no parameters remain or the parameter's code does not
7890 match the specified code, return false. Otherwise continue
7891 checking any remaining arguments. */
7893 || code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
7897 arglist = TREE_CHAIN (arglist);
7901 /* We need gotos here since we can only have one VA_CLOSE in a
7909 /* Default target-specific builtin expander that does nothing. */
7912 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
7913 rtx target ATTRIBUTE_UNUSED,
7914 rtx subtarget ATTRIBUTE_UNUSED,
7915 enum machine_mode mode ATTRIBUTE_UNUSED,
7916 int ignore ATTRIBUTE_UNUSED)
7921 /* Returns true is EXP represents data that would potentially reside
7922 in a readonly section. */
7925 readonly_data_expr (tree exp)
7929 if (TREE_CODE (exp) != ADDR_EXPR)
7932 exp = get_base_address (TREE_OPERAND (exp, 0));
7936 /* Make sure we call decl_readonly_section only for trees it
7937 can handle (since it returns true for everything it doesn't
7939 if (TREE_CODE (exp) == STRING_CST
7940 || TREE_CODE (exp) == CONSTRUCTOR
7941 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
7942 return decl_readonly_section (exp, 0);
7947 /* Front-end to the simplify_builtin_XXX routines.
7949 EXP is a call to a builtin function. If possible try to simplify
7950 that into a constant, expression or call to a more efficient
7953 If IGNORE is nonzero, then the result of this builtin function
7956 If simplification is possible, return the simplified tree, otherwise
7957 return NULL_TREE. */
7960 simplify_builtin (tree exp, int ignore)
7962 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7963 tree arglist = TREE_OPERAND (exp, 1);
7964 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7969 case BUILT_IN_FPUTS:
7970 val = simplify_builtin_fputs (arglist, ignore, 0, NULL_TREE);
7972 case BUILT_IN_FPUTS_UNLOCKED:
7973 val = simplify_builtin_fputs (arglist, ignore, 1, NULL_TREE);
7975 case BUILT_IN_STRSTR:
7976 val = simplify_builtin_strstr (arglist);
7978 case BUILT_IN_STRCAT:
7979 val = simplify_builtin_strcat (arglist);
7981 case BUILT_IN_STRNCAT:
7982 val = simplify_builtin_strncat (arglist);
7984 case BUILT_IN_STRSPN:
7985 val = simplify_builtin_strspn (arglist);
7987 case BUILT_IN_STRCSPN:
7988 val = simplify_builtin_strcspn (arglist);
7990 case BUILT_IN_STRCHR:
7991 case BUILT_IN_INDEX:
7992 val = simplify_builtin_strchr (arglist);
7994 case BUILT_IN_STRRCHR:
7995 case BUILT_IN_RINDEX:
7996 val = simplify_builtin_strrchr (arglist);
7998 case BUILT_IN_STRCPY:
7999 val = simplify_builtin_strcpy (arglist, NULL_TREE);
8001 case BUILT_IN_STRNCPY:
8002 val = simplify_builtin_strncpy (arglist, NULL_TREE);
8004 case BUILT_IN_STRCMP:
8005 val = simplify_builtin_strcmp (arglist);
8007 case BUILT_IN_STRNCMP:
8008 val = simplify_builtin_strncmp (arglist);
8010 case BUILT_IN_STRPBRK:
8011 val = simplify_builtin_strpbrk (arglist);
8014 case BUILT_IN_MEMCMP:
8015 val = simplify_builtin_memcmp (arglist);
8017 case BUILT_IN_VA_START:
8018 simplify_builtin_va_start (arglist);
8021 case BUILT_IN_SPRINTF:
8022 val = simplify_builtin_sprintf (arglist, ignore);
8024 case BUILT_IN_CONSTANT_P:
8025 val = fold_builtin_constant_p (arglist);
8026 /* Gimplification will pull the CALL_EXPR for the builtin out of
8027 an if condition. When not optimizing, we'll not CSE it back.
8028 To avoid link error types of regressions, return false now. */
8029 if (!val && !optimize)
8030 val = integer_zero_node;
8038 val = fold_convert (TREE_TYPE (exp), val);
8042 /* Simplify a call to the strstr builtin.
8044 Return 0 if no simplification was possible, otherwise return the
8045 simplified form of the call as a tree.
8047 The simplified form may be a constant or other expression which
8048 computes the same value, but in a more efficient manner (including
8049 calls to other builtin functions).
8051 The call may contain arguments which need to be evaluated, but
8052 which are not useful to determine the result of the call. In
8053 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8054 COMPOUND_EXPR will be an argument which must be evaluated.
8055 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8056 COMPOUND_EXPR in the chain will contain the tree for the simplified
8057 form of the builtin function call. */
8060 simplify_builtin_strstr (tree arglist)
8062 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8066 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8068 const char *p1, *p2;
8077 const char *r = strstr (p1, p2);
8080 return fold_convert (TREE_TYPE (s1), integer_zero_node);
8082 /* Return an offset into the constant string argument. */
8083 return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8084 s1, fold_convert (TREE_TYPE (s1),
8085 ssize_int (r - p1))));
8094 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8098 /* New argument list transforming strstr(s1, s2) to
8099 strchr(s1, s2[0]). */
8100 arglist = build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
8101 arglist = tree_cons (NULL_TREE, s1, arglist);
8102 return build_function_call_expr (fn, arglist);
8106 /* Simplify a call to the strstr builtin.
8108 Return 0 if no simplification was possible, otherwise return the
8109 simplified form of the call as a tree.
8111 The simplified form may be a constant or other expression which
8112 computes the same value, but in a more efficient manner (including
8113 calls to other builtin functions).
8115 The call may contain arguments which need to be evaluated, but
8116 which are not useful to determine the result of the call. In
8117 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8118 COMPOUND_EXPR will be an argument which must be evaluated.
8119 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8120 COMPOUND_EXPR in the chain will contain the tree for the simplified
8121 form of the builtin function call. */
8124 simplify_builtin_strchr (tree arglist)
8126 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8130 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8133 if (TREE_CODE (s2) != INTEGER_CST)
8142 if (target_char_cast (s2, &c))
8148 return fold_convert (TREE_TYPE (s1), integer_zero_node);
8150 /* Return an offset into the constant string argument. */
8151 return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8152 s1, fold_convert (TREE_TYPE (s1),
8153 ssize_int (r - p1))));
8156 /* FIXME: Should use here strchrM optab so that ports can optimize
8162 /* Simplify a call to the strrchr builtin.
8164 Return 0 if no simplification was possible, otherwise return the
8165 simplified form of the call as a tree.
8167 The simplified form may be a constant or other expression which
8168 computes the same value, but in a more efficient manner (including
8169 calls to other builtin functions).
8171 The call may contain arguments which need to be evaluated, but
8172 which are not useful to determine the result of the call. In
8173 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8174 COMPOUND_EXPR will be an argument which must be evaluated.
8175 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8176 COMPOUND_EXPR in the chain will contain the tree for the simplified
8177 form of the builtin function call. */
8180 simplify_builtin_strrchr (tree arglist)
8182 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8186 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8190 if (TREE_CODE (s2) != INTEGER_CST)
8199 if (target_char_cast (s2, &c))
8202 r = strrchr (p1, c);
8205 return fold_convert (TREE_TYPE (s1), integer_zero_node);
8207 /* Return an offset into the constant string argument. */
8208 return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8209 s1, fold_convert (TREE_TYPE (s1),
8210 ssize_int (r - p1))));
8213 if (! integer_zerop (s2))
8216 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8220 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
8221 return build_function_call_expr (fn, arglist);
8225 /* Simplify a call to the strpbrk builtin.
8227 Return 0 if no simplification was possible, otherwise return the
8228 simplified form of the call as a tree.
8230 The simplified form may be a constant or other expression which
8231 computes the same value, but in a more efficient manner (including
8232 calls to other builtin functions).
8234 The call may contain arguments which need to be evaluated, but
8235 which are not useful to determine the result of the call. In
8236 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8237 COMPOUND_EXPR will be an argument which must be evaluated.
8238 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8239 COMPOUND_EXPR in the chain will contain the tree for the simplified
8240 form of the builtin function call. */
8243 simplify_builtin_strpbrk (tree arglist)
8245 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8249 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8251 const char *p1, *p2;
8260 const char *r = strpbrk (p1, p2);
8263 return fold_convert (TREE_TYPE (s1), integer_zero_node);
8265 /* Return an offset into the constant string argument. */
8266 return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8267 s1, fold_convert (TREE_TYPE (s1),
8268 ssize_int (r - p1))));
8272 /* strpbrk(x, "") == NULL.
8273 Evaluate and ignore s1 in case it had side-effects. */
8274 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
8277 return 0; /* Really call strpbrk. */
8279 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8283 /* New argument list transforming strpbrk(s1, s2) to
8284 strchr(s1, s2[0]). */
8286 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
8287 arglist = tree_cons (NULL_TREE, s1, arglist);
8288 return build_function_call_expr (fn, arglist);
8292 /* Simplify a call to the strcpy builtin.
8294 Return 0 if no simplification was possible, otherwise return the
8295 simplified form of the call as a tree.
8297 The simplified form may be a constant or other expression which
8298 computes the same value, but in a more efficient manner (including
8299 calls to other builtin functions).
8301 The call may contain arguments which need to be evaluated, but
8302 which are not useful to determine the result of the call. In
8303 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8304 COMPOUND_EXPR will be an argument which must be evaluated.
8305 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8306 COMPOUND_EXPR in the chain will contain the tree for the simplified
8307 form of the builtin function call. */
8310 simplify_builtin_strcpy (tree arglist, tree len)
8314 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8317 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8321 src = TREE_VALUE (TREE_CHAIN (arglist));
8322 dst = TREE_VALUE (arglist);
8326 len = c_strlen (src, 1);
8327 if (!len || TREE_SIDE_EFFECTS (len))
8331 len = size_binop (PLUS_EXPR, len, ssize_int (1));
8332 arglist = build_tree_list (NULL_TREE, len);
8333 arglist = tree_cons (NULL_TREE, src, arglist);
8334 arglist = tree_cons (NULL_TREE, dst, arglist);
8335 return build_function_call_expr (fn, arglist);
8338 /* Simplify a call to the strncpy builtin.
8340 Return 0 if no simplification was possible, otherwise return the
8341 simplified form of the call as a tree.
8343 The simplified form may be a constant or other expression which
8344 computes the same value, but in a more efficient manner (including
8345 calls to other builtin functions).
8347 The call may contain arguments which need to be evaluated, but
8348 which are not useful to determine the result of the call. In
8349 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8350 COMPOUND_EXPR will be an argument which must be evaluated.
8351 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8352 COMPOUND_EXPR in the chain will contain the tree for the simplified
8353 form of the builtin function call. */
8356 simplify_builtin_strncpy (tree arglist, tree slen)
8358 if (!validate_arglist (arglist,
8359 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8363 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8366 /* We must be passed a constant len parameter. */
8367 if (TREE_CODE (len) != INTEGER_CST)
8370 /* If the len parameter is zero, return the dst parameter. */
8371 if (integer_zerop (len))
8372 /* Evaluate and ignore the src argument in case it has
8373 side-effects and return the dst parameter. */
8374 return omit_one_operand (TREE_TYPE (TREE_VALUE (arglist)),
8375 TREE_VALUE (arglist),
8376 TREE_VALUE (TREE_CHAIN (arglist)));
8379 slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 0);
8381 /* Now, we must be passed a constant src ptr parameter. */
8382 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8385 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8387 /* We do not support simplification of this case, though we do
8388 support it when expanding trees into RTL. */
8389 /* FIXME: generate a call to __builtin_memset. */
8390 if (tree_int_cst_lt (slen, len))
8393 /* OK transform into builtin memcpy. */
8394 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8397 return build_function_call_expr (fn, arglist);
8401 /* Simplify a call to the memcmp builtin.
8403 Return 0 if no simplification was possible, otherwise return the
8404 simplified form of the call as a tree.
8406 The simplified form may be a constant or other expression which
8407 computes the same value, but in a more efficient manner (including
8408 calls to other builtin functions).
8410 The call may contain arguments which need to be evaluated, but
8411 which are not useful to determine the result of the call. In
8412 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8413 COMPOUND_EXPR will be an argument which must be evaluated.
8414 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8415 COMPOUND_EXPR in the chain will contain the tree for the simplified
8416 form of the builtin function call. */
8419 simplify_builtin_memcmp (tree arglist)
8421 tree arg1, arg2, len;
8422 const char *p1, *p2;
8424 if (!validate_arglist (arglist,
8425 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8428 arg1 = TREE_VALUE (arglist);
8429 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8430 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8432 /* If the len parameter is zero, return zero. */
8433 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
8435 /* Evaluate and ignore arg1 and arg2 in case they have
8437 return build2 (COMPOUND_EXPR, integer_type_node, arg1,
8438 build2 (COMPOUND_EXPR, integer_type_node,
8439 arg2, integer_zero_node));
8442 p1 = c_getstr (arg1);
8443 p2 = c_getstr (arg2);
8445 /* If all arguments are constant, and the value of len is not greater
8446 than the lengths of arg1 and arg2, evaluate at compile-time. */
8447 if (host_integerp (len, 1) && p1 && p2
8448 && compare_tree_int (len, strlen (p1) + 1) <= 0
8449 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8451 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8454 ? integer_minus_one_node
8455 : (r > 0 ? integer_one_node : integer_zero_node));
8458 /* If len parameter is one, return an expression corresponding to
8459 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8460 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8462 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8463 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
8465 fold (build1 (CONVERT_EXPR, integer_type_node,
8466 build1 (INDIRECT_REF, cst_uchar_node,
8467 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
8469 fold (build1 (CONVERT_EXPR, integer_type_node,
8470 build1 (INDIRECT_REF, cst_uchar_node,
8471 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
8472 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
8478 /* Simplify a call to the strcmp builtin.
8480 Return 0 if no simplification was possible, otherwise return the
8481 simplified form of the call as a tree.
8483 The simplified form may be a constant or other expression which
8484 computes the same value, but in a more efficient manner (including
8485 calls to other builtin functions).
8487 The call may contain arguments which need to be evaluated, but
8488 which are not useful to determine the result of the call. In
8489 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8490 COMPOUND_EXPR will be an argument which must be evaluated.
8491 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8492 COMPOUND_EXPR in the chain will contain the tree for the simplified
8493 form of the builtin function call. */
8496 simplify_builtin_strcmp (tree arglist)
8499 const char *p1, *p2;
8501 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8504 arg1 = TREE_VALUE (arglist);
8505 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8507 /* If both arguments are equal (and not volatile), return zero. */
8508 if (operand_equal_p (arg1, arg2, 0))
8509 return integer_zero_node;
8511 p1 = c_getstr (arg1);
8512 p2 = c_getstr (arg2);
8516 const int i = strcmp (p1, p2);
8518 ? integer_minus_one_node
8519 : (i > 0 ? integer_one_node : integer_zero_node));
8522 /* If either arg is "", return an expression corresponding to
8523 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8524 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
8526 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8527 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
8529 fold (build1 (CONVERT_EXPR, integer_type_node,
8530 build1 (INDIRECT_REF, cst_uchar_node,
8531 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
8533 fold (build1 (CONVERT_EXPR, integer_type_node,
8534 build1 (INDIRECT_REF, cst_uchar_node,
8535 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
8536 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
8542 /* Simplify a call to the strncmp builtin.
8544 Return 0 if no simplification was possible, otherwise return the
8545 simplified form of the call as a tree.
8547 The simplified form may be a constant or other expression which
8548 computes the same value, but in a more efficient manner (including
8549 calls to other builtin functions).
8551 The call may contain arguments which need to be evaluated, but
8552 which are not useful to determine the result of the call. In
8553 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8554 COMPOUND_EXPR will be an argument which must be evaluated.
8555 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8556 COMPOUND_EXPR in the chain will contain the tree for the simplified
8557 form of the builtin function call. */
8560 simplify_builtin_strncmp (tree arglist)
8562 tree arg1, arg2, arg3;
8563 const char *p1, *p2;
8565 if (!validate_arglist (arglist,
8566 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8569 arg1 = TREE_VALUE (arglist);
8570 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8571 arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8573 /* If the len parameter is zero, return zero. */
8574 if (integer_zerop (arg3))
8576 /* Evaluate and ignore arg1 and arg2 in case they have
8578 return build2 (COMPOUND_EXPR, integer_type_node, arg1,
8579 build2 (COMPOUND_EXPR, integer_type_node,
8580 arg2, integer_zero_node));
8583 /* If arg1 and arg2 are equal (and not volatile), return zero. */
8584 if (operand_equal_p (arg1, arg2, 0))
8585 /* Evaluate and ignore arg3 in case it has side-effects. */
8586 return omit_one_operand (integer_type_node, integer_zero_node, arg3);
8588 p1 = c_getstr (arg1);
8589 p2 = c_getstr (arg2);
8591 /* If all arguments are constant, evaluate at compile-time. */
8592 if (host_integerp (arg3, 1) && p1 && p2)
8594 const int r = strncmp (p1, p2, tree_low_cst (arg3, 1));
8596 ? integer_minus_one_node
8597 : (r > 0 ? integer_one_node : integer_zero_node));
8600 /* If len == 1 or (either string parameter is "" and (len >= 1)),
8601 return (*(const u_char*)arg1 - *(const u_char*)arg2). */
8602 if (host_integerp (arg3, 1)
8603 && (tree_low_cst (arg3, 1) == 1
8604 || (tree_low_cst (arg3, 1) > 1
8605 && ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')))))
8607 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8608 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
8610 fold (build1 (CONVERT_EXPR, integer_type_node,
8611 build1 (INDIRECT_REF, cst_uchar_node,
8612 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
8614 fold (build1 (CONVERT_EXPR, integer_type_node,
8615 build1 (INDIRECT_REF, cst_uchar_node,
8616 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
8617 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
8623 /* Simplify a call to the strcat builtin.
8625 Return 0 if no simplification was possible, otherwise return the
8626 simplified form of the call as a tree.
8628 The simplified form may be a constant or other expression which
8629 computes the same value, but in a more efficient manner (including
8630 calls to other builtin functions).
8632 The call may contain arguments which need to be evaluated, but
8633 which are not useful to determine the result of the call. In
8634 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8635 COMPOUND_EXPR will be an argument which must be evaluated.
8636 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8637 COMPOUND_EXPR in the chain will contain the tree for the simplified
8638 form of the builtin function call. */
8641 simplify_builtin_strcat (tree arglist)
8643 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8647 tree dst = TREE_VALUE (arglist),
8648 src = TREE_VALUE (TREE_CHAIN (arglist));
8649 const char *p = c_getstr (src);
8651 /* If the string length is zero, return the dst parameter. */
8652 if (p && *p == '\0')
8659 /* Simplify a call to the strncat builtin.
8661 Return 0 if no simplification was possible, otherwise return the
8662 simplified form of the call as a tree.
8664 The simplified form may be a constant or other expression which
8665 computes the same value, but in a more efficient manner (including
8666 calls to other builtin functions).
8668 The call may contain arguments which need to be evaluated, but
8669 which are not useful to determine the result of the call. In
8670 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8671 COMPOUND_EXPR will be an argument which must be evaluated.
8672 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8673 COMPOUND_EXPR in the chain will contain the tree for the simplified
8674 form of the builtin function call. */
8677 simplify_builtin_strncat (tree arglist)
8679 if (!validate_arglist (arglist,
8680 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8684 tree dst = TREE_VALUE (arglist);
8685 tree src = TREE_VALUE (TREE_CHAIN (arglist));
8686 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8687 const char *p = c_getstr (src);
8689 /* If the requested length is zero, or the src parameter string
8690 length is zero, return the dst parameter. */
8691 if (integer_zerop (len) || (p && *p == '\0'))
8692 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), src,
8693 build2 (COMPOUND_EXPR, integer_type_node, len, dst));
8695 /* If the requested len is greater than or equal to the string
8696 length, call strcat. */
8697 if (TREE_CODE (len) == INTEGER_CST && p
8698 && compare_tree_int (len, strlen (p)) >= 0)
8701 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
8702 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
8704 /* If the replacement _DECL isn't initialized, don't do the
8709 return build_function_call_expr (fn, newarglist);
8715 /* Simplify a call to the strspn builtin.
8717 Return 0 if no simplification was possible, otherwise return the
8718 simplified form of the call as a tree.
8720 The simplified form may be a constant or other expression which
8721 computes the same value, but in a more efficient manner (including
8722 calls to other builtin functions).
8724 The call may contain arguments which need to be evaluated, but
8725 which are not useful to determine the result of the call. In
8726 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8727 COMPOUND_EXPR will be an argument which must be evaluated.
8728 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8729 COMPOUND_EXPR in the chain will contain the tree for the simplified
8730 form of the builtin function call. */
8733 simplify_builtin_strspn (tree arglist)
8735 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8739 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8740 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8742 /* If both arguments are constants, evaluate at compile-time. */
8745 const size_t r = strspn (p1, p2);
8746 return size_int (r);
8749 /* If either argument is "", return 0. */
8750 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
8752 /* Evaluate and ignore both arguments in case either one has
8754 return build2 (COMPOUND_EXPR, integer_type_node, s1,
8755 build2 (COMPOUND_EXPR, integer_type_node,
8756 s2, integer_zero_node));
8762 /* Simplify a call to the strcspn builtin.
8764 Return 0 if no simplification was possible, otherwise return the
8765 simplified form of the call as a tree.
8767 The simplified form may be a constant or other expression which
8768 computes the same value, but in a more efficient manner (including
8769 calls to other builtin functions).
8771 The call may contain arguments which need to be evaluated, but
8772 which are not useful to determine the result of the call. In
8773 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8774 COMPOUND_EXPR will be an argument which must be evaluated.
8775 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8776 COMPOUND_EXPR in the chain will contain the tree for the simplified
8777 form of the builtin function call. */
8780 simplify_builtin_strcspn (tree arglist)
8782 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8786 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8787 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8789 /* If both arguments are constants, evaluate at compile-time. */
8792 const size_t r = strcspn (p1, p2);
8793 return size_int (r);
8796 /* If the first argument is "", return 0. */
8797 if (p1 && *p1 == '\0')
8799 /* Evaluate and ignore argument s2 in case it has
8801 return omit_one_operand (integer_type_node,
8802 integer_zero_node, s2);
8805 /* If the second argument is "", return __builtin_strlen(s1). */
8806 if (p2 && *p2 == '\0')
8808 tree newarglist = build_tree_list (NULL_TREE, s1),
8809 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
8811 /* If the replacement _DECL isn't initialized, don't do the
8816 return build_function_call_expr (fn, newarglist);
8822 /* Simplify a call to the fputs builtin.
8824 Return 0 if no simplification was possible, otherwise return the
8825 simplified form of the call as a tree.
8827 The simplified form may be a constant or other expression which
8828 computes the same value, but in a more efficient manner (including
8829 calls to other builtin functions).
8831 The call may contain arguments which need to be evaluated, but
8832 which are not useful to determine the result of the call. In
8833 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8834 COMPOUND_EXPR will be an argument which must be evaluated.
8835 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8836 COMPOUND_EXPR in the chain will contain the tree for the simplified
8837 form of the builtin function call.
8839 If KNOWN_LEN is non-NULL, it represents the known length of the string.
8840 This is determined by SSA-CCP in cases where the string itself is not
8841 known to be constant but its length is always the same constant. */
8844 simplify_builtin_fputs (tree arglist, int ignore, int unlocked, tree known_len)
8847 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
8848 : implicit_built_in_decls[BUILT_IN_FPUTC];
8849 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
8850 : implicit_built_in_decls[BUILT_IN_FWRITE];
8852 /* If the return value is used, or the replacement _DECL isn't
8853 initialized, don't do the transformation. */
8854 if (!ignore || !fn_fputc || !fn_fwrite)
8857 /* Verify the arguments in the original call. */
8858 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8861 len = (known_len) ? known_len : c_strlen (TREE_VALUE (arglist), 0);
8863 /* Get the length of the string passed to fputs. If the length
8864 can't be determined, punt. */
8866 || TREE_CODE (len) != INTEGER_CST)
8869 switch (compare_tree_int (len, 1))
8871 case -1: /* length is 0, delete the call entirely . */
8872 return omit_one_operand (integer_type_node, integer_zero_node,
8873 TREE_VALUE (TREE_CHAIN (arglist)));
8875 case 0: /* length is 1, call fputc. */
8877 const char *p = c_getstr (TREE_VALUE (arglist));
8881 /* New argument list transforming fputs(string, stream) to
8882 fputc(string[0], stream). */
8884 build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
8886 tree_cons (NULL_TREE, build_int_2 (p[0], 0), arglist);
8892 case 1: /* length is greater than 1, call fwrite. */
8896 /* If optimizing for size keep fputs. */
8899 string_arg = TREE_VALUE (arglist);
8900 /* New argument list transforming fputs(string, stream) to
8901 fwrite(string, 1, len, stream). */
8902 arglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
8903 arglist = tree_cons (NULL_TREE, len, arglist);
8904 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
8905 arglist = tree_cons (NULL_TREE, string_arg, arglist);
8913 return build_function_call_expr (fn, arglist);
8917 simplify_builtin_va_start (tree arglist)
8919 tree chain = TREE_CHAIN (arglist);
8921 if (TREE_CHAIN (chain))
8922 error ("too many arguments to function `va_start'");
8924 simplify_builtin_next_arg (chain);
8928 simplify_builtin_next_arg (tree arglist)
8930 tree fntype = TREE_TYPE (current_function_decl);
8932 if (TYPE_ARG_TYPES (fntype) == 0
8933 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
8935 error ("`va_start' used in function with fixed args");
8938 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
8939 tree arg = TREE_VALUE (arglist);
8941 /* Strip off all nops for the sake of the comparison. This
8942 is not quite the same as STRIP_NOPS. It does more.
8943 We must also strip off INDIRECT_EXPR for C++ reference
8945 while (TREE_CODE (arg) == NOP_EXPR
8946 || TREE_CODE (arg) == CONVERT_EXPR
8947 || TREE_CODE (arg) == NON_LVALUE_EXPR
8948 || TREE_CODE (arg) == INDIRECT_REF)
8949 arg = TREE_OPERAND (arg, 0);
8950 if (arg != last_parm)
8951 warning ("second parameter of `va_start' not last named argument");
8952 TREE_VALUE (arglist) = arg;
8955 /* Evidently an out of date version of <stdarg.h>; can't validate
8956 va_start's second argument, but can still work as intended. */
8957 warning ("`__builtin_next_arg' called without an argument");
8961 /* Simplify a call to the sprintf builtin.
8963 Return 0 if no simplification was possible, otherwise return the
8964 simplified form of the call as a tree. If IGNORED is true, it means that
8965 the caller does not use the returned value of the function. */
8968 simplify_builtin_sprintf (tree arglist, int ignored)
8970 tree call, retval, dest, fmt;
8971 const char *fmt_str = NULL;
8973 /* Verify the required arguments in the original call. We deal with two
8974 types of sprintf() calls: 'sprintf (str, fmt)' and
8975 'sprintf (dest, "%s", orig)'. */
8976 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
8977 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
8981 /* Get the destination string and the format specifier. */
8982 dest = TREE_VALUE (arglist);
8983 fmt = TREE_VALUE (TREE_CHAIN (arglist));
8985 /* Check whether the format is a literal string constant. */
8986 fmt_str = c_getstr (fmt);
8987 if (fmt_str == NULL)
8993 /* If the format doesn't contain % args or %%, use strcpy. */
8994 if (strchr (fmt_str, '%') == NULL)
8996 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9001 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
9002 'format' is known to contain no % formats. */
9003 arglist = build_tree_list (NULL_TREE, fmt);
9004 arglist = tree_cons (NULL_TREE, dest, arglist);
9005 call = build_function_call_expr (fn, arglist);
9007 retval = build_int_2 (strlen (fmt_str), 0);
9010 /* If the format is "%s", use strcpy if the result isn't used. */
9011 else if (fmt_str && strcmp (fmt_str, "%s") == 0)
9014 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9019 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
9020 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9021 arglist = build_tree_list (NULL_TREE, orig);
9022 arglist = tree_cons (NULL_TREE, dest, arglist);
9025 retval = c_strlen (orig, 1);
9026 if (!retval || TREE_CODE (retval) != INTEGER_CST)
9029 call = build_function_call_expr (fn, arglist);
9035 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
9037 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);