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"
32 #include "hard-reg-set.h"
35 #include "insn-config.h"
41 #include "typeclass.h"
46 #include "langhooks.h"
48 #define CALLED_AS_BUILT_IN(NODE) \
49 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
51 #ifndef PAD_VARARGS_DOWN
52 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
55 /* Define the names of the builtin function types and codes. */
56 const char *const built_in_class_names[4]
57 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
59 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM) #X,
60 const char *const built_in_names[(int) END_BUILTINS] =
62 #include "builtins.def"
66 /* Setup an array of _DECL trees, make sure each element is
67 initialized to NULL_TREE. */
68 tree built_in_decls[(int) END_BUILTINS];
69 /* Declarations used when constructing the builtin implicitly in the compiler.
70 It may be NULL_TREE when this is invalid (for instance runtime is not
71 required to implement the function call in all cases. */
72 tree implicit_built_in_decls[(int) END_BUILTINS];
74 static int get_pointer_alignment (tree, unsigned int);
75 static tree c_strlen (tree, 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_prefetch (tree);
88 static rtx expand_builtin_apply_args (void);
89 static rtx expand_builtin_apply_args_1 (void);
90 static rtx expand_builtin_apply (rtx, rtx, rtx);
91 static void expand_builtin_return (rtx);
92 static enum type_class type_to_class (tree);
93 static rtx expand_builtin_classify_type (tree);
94 static void expand_errno_check (tree, rtx);
95 static rtx expand_builtin_mathfn (tree, rtx, rtx);
96 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
97 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
98 static rtx expand_builtin_constant_p (tree, enum machine_mode);
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, 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 /* Return the alignment in bits of EXP, a pointer valued expression.
166 But don't return more than MAX_ALIGN no matter what.
167 The alignment returned is, by default, the alignment of the thing that
168 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
170 Otherwise, look at the expression to see if we can do better, i.e., if the
171 expression is actually pointing at an object whose alignment is tighter. */
174 get_pointer_alignment (tree exp, unsigned int max_align)
176 unsigned int align, inner;
178 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
181 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
182 align = MIN (align, max_align);
186 switch (TREE_CODE (exp))
190 case NON_LVALUE_EXPR:
191 exp = TREE_OPERAND (exp, 0);
192 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
195 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
196 align = MIN (inner, max_align);
200 /* If sum of pointer + int, restrict our maximum alignment to that
201 imposed by the integer. If not, we can't do any better than
203 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
206 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
207 & (max_align / BITS_PER_UNIT - 1))
211 exp = TREE_OPERAND (exp, 0);
215 /* See what we are pointing at and look at its alignment. */
216 exp = TREE_OPERAND (exp, 0);
217 if (TREE_CODE (exp) == FUNCTION_DECL)
218 align = FUNCTION_BOUNDARY;
219 else if (DECL_P (exp))
220 align = DECL_ALIGN (exp);
221 #ifdef CONSTANT_ALIGNMENT
222 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c')
223 align = CONSTANT_ALIGNMENT (exp, align);
225 return MIN (align, max_align);
233 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
234 way, because it could contain a zero byte in the middle.
235 TREE_STRING_LENGTH is the size of the character array, not the string.
237 ONLY_VALUE should be nonzero if the result is not going to be emitted
238 into the instruction stream and zero if it is going to be expanded.
239 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
240 is returned, otherwise NULL, since
241 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
242 evaluate the side-effects.
244 The value returned is of type `ssizetype'.
246 Unfortunately, string_constant can't access the values of const char
247 arrays with initializers, so neither can we do so here. */
250 c_strlen (tree src, int only_value)
253 HOST_WIDE_INT offset;
258 if (TREE_CODE (src) == COND_EXPR
259 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
263 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
264 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
265 if (tree_int_cst_equal (len1, len2))
269 if (TREE_CODE (src) == COMPOUND_EXPR
270 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
271 return c_strlen (TREE_OPERAND (src, 1), only_value);
273 src = string_constant (src, &offset_node);
277 max = TREE_STRING_LENGTH (src) - 1;
278 ptr = TREE_STRING_POINTER (src);
280 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
282 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
283 compute the offset to the following null if we don't know where to
284 start searching for it. */
287 for (i = 0; i < max; i++)
291 /* We don't know the starting offset, but we do know that the string
292 has no internal zero bytes. We can assume that the offset falls
293 within the bounds of the string; otherwise, the programmer deserves
294 what he gets. Subtract the offset from the length of the string,
295 and return that. This would perhaps not be valid if we were dealing
296 with named arrays in addition to literal string constants. */
298 return size_diffop (size_int (max), offset_node);
301 /* We have a known offset into the string. Start searching there for
302 a null character if we can represent it as a single HOST_WIDE_INT. */
303 if (offset_node == 0)
305 else if (! host_integerp (offset_node, 0))
308 offset = tree_low_cst (offset_node, 0);
310 /* If the offset is known to be out of bounds, warn, and call strlen at
312 if (offset < 0 || offset > max)
314 warning ("offset outside bounds of constant string");
318 /* Use strlen to search for the first zero byte. Since any strings
319 constructed with build_string will have nulls appended, we win even
320 if we get handed something like (char[4])"abcd".
322 Since OFFSET is our starting index into the string, no further
323 calculation is needed. */
324 return ssize_int (strlen (ptr + offset));
327 /* Return a char pointer for a C string if it is a string constant
328 or sum of string constant and integer constant. */
335 src = string_constant (src, &offset_node);
339 if (offset_node == 0)
340 return TREE_STRING_POINTER (src);
341 else if (!host_integerp (offset_node, 1)
342 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
345 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
348 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
349 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
352 c_readstr (const char *str, enum machine_mode mode)
358 if (GET_MODE_CLASS (mode) != MODE_INT)
363 for (i = 0; i < GET_MODE_SIZE (mode); i++)
366 if (WORDS_BIG_ENDIAN)
367 j = GET_MODE_SIZE (mode) - i - 1;
368 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
369 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
370 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
372 if (j > 2 * HOST_BITS_PER_WIDE_INT)
375 ch = (unsigned char) str[i];
376 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
378 return immed_double_const (c[0], c[1], mode);
381 /* Cast a target constant CST to target CHAR and if that value fits into
382 host char type, return zero and put that value into variable pointed by
386 target_char_cast (tree cst, char *p)
388 unsigned HOST_WIDE_INT val, hostval;
390 if (!host_integerp (cst, 1)
391 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
394 val = tree_low_cst (cst, 1);
395 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
396 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
399 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
400 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
409 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
410 times to get the address of either a higher stack frame, or a return
411 address located within it (depending on FNDECL_CODE). */
414 expand_builtin_return_addr (enum built_in_function fndecl_code, int count,
419 /* Some machines need special handling before we can access
420 arbitrary frames. For example, on the sparc, we must first flush
421 all register windows to the stack. */
422 #ifdef SETUP_FRAME_ADDRESSES
424 SETUP_FRAME_ADDRESSES ();
427 /* On the sparc, the return address is not in the frame, it is in a
428 register. There is no way to access it off of the current frame
429 pointer, but it can be accessed off the previous frame pointer by
430 reading the value from the register window save area. */
431 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
432 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
436 /* Scan back COUNT frames to the specified frame. */
437 for (i = 0; i < count; i++)
439 /* Assume the dynamic chain pointer is in the word that the
440 frame address points to, unless otherwise specified. */
441 #ifdef DYNAMIC_CHAIN_ADDRESS
442 tem = DYNAMIC_CHAIN_ADDRESS (tem);
444 tem = memory_address (Pmode, tem);
445 tem = gen_rtx_MEM (Pmode, tem);
446 set_mem_alias_set (tem, get_frame_alias_set ());
447 tem = copy_to_reg (tem);
450 /* For __builtin_frame_address, return what we've got. */
451 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
454 /* For __builtin_return_address, Get the return address from that
456 #ifdef RETURN_ADDR_RTX
457 tem = RETURN_ADDR_RTX (count, tem);
459 tem = memory_address (Pmode,
460 plus_constant (tem, GET_MODE_SIZE (Pmode)));
461 tem = gen_rtx_MEM (Pmode, tem);
462 set_mem_alias_set (tem, get_frame_alias_set ());
467 /* Alias set used for setjmp buffer. */
468 static HOST_WIDE_INT setjmp_alias_set = -1;
470 /* Construct the leading half of a __builtin_setjmp call. Control will
471 return to RECEIVER_LABEL. This is used directly by sjlj exception
475 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
477 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
481 if (setjmp_alias_set == -1)
482 setjmp_alias_set = new_alias_set ();
484 buf_addr = convert_memory_address (Pmode, buf_addr);
486 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
490 /* We store the frame pointer and the address of receiver_label in
491 the buffer and use the rest of it for the stack save area, which
492 is machine-dependent. */
494 mem = gen_rtx_MEM (Pmode, buf_addr);
495 set_mem_alias_set (mem, setjmp_alias_set);
496 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
498 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
499 set_mem_alias_set (mem, setjmp_alias_set);
501 emit_move_insn (validize_mem (mem),
502 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
504 stack_save = gen_rtx_MEM (sa_mode,
505 plus_constant (buf_addr,
506 2 * GET_MODE_SIZE (Pmode)));
507 set_mem_alias_set (stack_save, setjmp_alias_set);
508 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
510 /* If there is further processing to do, do it. */
511 #ifdef HAVE_builtin_setjmp_setup
512 if (HAVE_builtin_setjmp_setup)
513 emit_insn (gen_builtin_setjmp_setup (buf_addr));
516 /* Tell optimize_save_area_alloca that extra work is going to
517 need to go on during alloca. */
518 current_function_calls_setjmp = 1;
520 /* Set this so all the registers get saved in our frame; we need to be
521 able to copy the saved values for any registers from frames we unwind. */
522 current_function_has_nonlocal_label = 1;
525 /* Construct the trailing part of a __builtin_setjmp call.
526 This is used directly by sjlj exception handling code. */
529 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
531 /* Clobber the FP when we get here, so we have to make sure it's
532 marked as used by this function. */
533 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
535 /* Mark the static chain as clobbered here so life information
536 doesn't get messed up for it. */
537 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
539 /* Now put in the code to restore the frame pointer, and argument
540 pointer, if needed. The code below is from expand_end_bindings
541 in stmt.c; see detailed documentation there. */
542 #ifdef HAVE_nonlocal_goto
543 if (! HAVE_nonlocal_goto)
545 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
547 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
548 if (fixed_regs[ARG_POINTER_REGNUM])
550 #ifdef ELIMINABLE_REGS
552 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
554 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
555 if (elim_regs[i].from == ARG_POINTER_REGNUM
556 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
559 if (i == ARRAY_SIZE (elim_regs))
562 /* Now restore our arg pointer from the address at which it
563 was saved in our stack frame. */
564 emit_move_insn (virtual_incoming_args_rtx,
565 copy_to_reg (get_arg_pointer_save_area (cfun)));
570 #ifdef HAVE_builtin_setjmp_receiver
571 if (HAVE_builtin_setjmp_receiver)
572 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
575 #ifdef HAVE_nonlocal_goto_receiver
576 if (HAVE_nonlocal_goto_receiver)
577 emit_insn (gen_nonlocal_goto_receiver ());
582 /* @@@ This is a kludge. Not all machine descriptions define a blockage
583 insn, but we must not allow the code we just generated to be reordered
584 by scheduling. Specifically, the update of the frame pointer must
585 happen immediately, not later. So emit an ASM_INPUT to act as blockage
587 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
590 /* __builtin_setjmp is passed a pointer to an array of five words (not
591 all will be used on all machines). It operates similarly to the C
592 library function of the same name, but is more efficient. Much of
593 the code below (and for longjmp) is copied from the handling of
596 NOTE: This is intended for use by GNAT and the exception handling
597 scheme in the compiler and will only work in the method used by
601 expand_builtin_setjmp (tree arglist, rtx target)
603 rtx buf_addr, next_lab, cont_lab;
605 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
608 if (target == 0 || GET_CODE (target) != REG
609 || REGNO (target) < FIRST_PSEUDO_REGISTER)
610 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
612 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
614 next_lab = gen_label_rtx ();
615 cont_lab = gen_label_rtx ();
617 expand_builtin_setjmp_setup (buf_addr, next_lab);
619 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
620 ensure that pending stack adjustments are flushed. */
621 emit_move_insn (target, const0_rtx);
622 emit_jump (cont_lab);
624 emit_label (next_lab);
626 expand_builtin_setjmp_receiver (next_lab);
628 /* Set TARGET to one. */
629 emit_move_insn (target, const1_rtx);
630 emit_label (cont_lab);
632 /* Tell flow about the strange goings on. Putting `next_lab' on
633 `nonlocal_goto_handler_labels' to indicates that function
634 calls may traverse the arc back to this label. */
636 current_function_has_nonlocal_label = 1;
637 nonlocal_goto_handler_labels
638 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
643 /* __builtin_longjmp is passed a pointer to an array of five words (not
644 all will be used on all machines). It operates similarly to the C
645 library function of the same name, but is more efficient. Much of
646 the code below is copied from the handling of non-local gotos.
648 NOTE: This is intended for use by GNAT and the exception handling
649 scheme in the compiler and will only work in the method used by
653 expand_builtin_longjmp (rtx buf_addr, rtx value)
655 rtx fp, lab, stack, insn, last;
656 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
658 if (setjmp_alias_set == -1)
659 setjmp_alias_set = new_alias_set ();
661 buf_addr = convert_memory_address (Pmode, buf_addr);
663 buf_addr = force_reg (Pmode, buf_addr);
665 /* We used to store value in static_chain_rtx, but that fails if pointers
666 are smaller than integers. We instead require that the user must pass
667 a second argument of 1, because that is what builtin_setjmp will
668 return. This also makes EH slightly more efficient, since we are no
669 longer copying around a value that we don't care about. */
670 if (value != const1_rtx)
673 current_function_calls_longjmp = 1;
675 last = get_last_insn ();
676 #ifdef HAVE_builtin_longjmp
677 if (HAVE_builtin_longjmp)
678 emit_insn (gen_builtin_longjmp (buf_addr));
682 fp = gen_rtx_MEM (Pmode, buf_addr);
683 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
684 GET_MODE_SIZE (Pmode)));
686 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
687 2 * GET_MODE_SIZE (Pmode)));
688 set_mem_alias_set (fp, setjmp_alias_set);
689 set_mem_alias_set (lab, setjmp_alias_set);
690 set_mem_alias_set (stack, setjmp_alias_set);
692 /* Pick up FP, label, and SP from the block and jump. This code is
693 from expand_goto in stmt.c; see there for detailed comments. */
694 #if HAVE_nonlocal_goto
695 if (HAVE_nonlocal_goto)
696 /* We have to pass a value to the nonlocal_goto pattern that will
697 get copied into the static_chain pointer, but it does not matter
698 what that value is, because builtin_setjmp does not use it. */
699 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
703 lab = copy_to_reg (lab);
705 emit_insn (gen_rtx_CLOBBER (VOIDmode,
706 gen_rtx_MEM (BLKmode,
707 gen_rtx_SCRATCH (VOIDmode))));
708 emit_insn (gen_rtx_CLOBBER (VOIDmode,
709 gen_rtx_MEM (BLKmode,
710 hard_frame_pointer_rtx)));
712 emit_move_insn (hard_frame_pointer_rtx, fp);
713 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
715 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
716 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
717 emit_indirect_jump (lab);
721 /* Search backwards and mark the jump insn as a non-local goto.
722 Note that this precludes the use of __builtin_longjmp to a
723 __builtin_setjmp target in the same function. However, we've
724 already cautioned the user that these functions are for
725 internal exception handling use only. */
726 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
730 if (GET_CODE (insn) == JUMP_INSN)
732 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
736 else if (GET_CODE (insn) == CALL_INSN)
741 /* Expand a call to __builtin_prefetch. For a target that does not support
742 data prefetch, evaluate the memory address argument in case it has side
746 expand_builtin_prefetch (tree arglist)
748 tree arg0, arg1, arg2;
751 if (!validate_arglist (arglist, POINTER_TYPE, 0))
754 arg0 = TREE_VALUE (arglist);
755 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
756 zero (read) and argument 2 (locality) defaults to 3 (high degree of
758 if (TREE_CHAIN (arglist))
760 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
761 if (TREE_CHAIN (TREE_CHAIN (arglist)))
762 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
764 arg2 = build_int_2 (3, 0);
768 arg1 = integer_zero_node;
769 arg2 = build_int_2 (3, 0);
772 /* Argument 0 is an address. */
773 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
775 /* Argument 1 (read/write flag) must be a compile-time constant int. */
776 if (TREE_CODE (arg1) != INTEGER_CST)
778 error ("second arg to `__builtin_prefetch' must be a constant");
779 arg1 = integer_zero_node;
781 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
782 /* Argument 1 must be either zero or one. */
783 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
785 warning ("invalid second arg to __builtin_prefetch; using zero");
789 /* Argument 2 (locality) must be a compile-time constant int. */
790 if (TREE_CODE (arg2) != INTEGER_CST)
792 error ("third arg to `__builtin_prefetch' must be a constant");
793 arg2 = integer_zero_node;
795 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
796 /* Argument 2 must be 0, 1, 2, or 3. */
797 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
799 warning ("invalid third arg to __builtin_prefetch; using zero");
806 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
808 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
809 || (GET_MODE (op0) != Pmode))
811 op0 = convert_memory_address (Pmode, op0);
812 op0 = force_reg (Pmode, op0);
814 emit_insn (gen_prefetch (op0, op1, op2));
818 op0 = protect_from_queue (op0, 0);
819 /* Don't do anything with direct references to volatile memory, but
820 generate code to handle other side effects. */
821 if (GET_CODE (op0) != MEM && side_effects_p (op0))
825 /* Get a MEM rtx for expression EXP which is the address of an operand
826 to be used to be used in a string instruction (cmpstrsi, movstrsi, ..). */
829 get_memory_rtx (tree exp)
831 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM);
834 addr = convert_memory_address (Pmode, addr);
836 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
838 /* Get an expression we can use to find the attributes to assign to MEM.
839 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
840 we can. First remove any nops. */
841 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
842 || TREE_CODE (exp) == NON_LVALUE_EXPR)
843 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
844 exp = TREE_OPERAND (exp, 0);
846 if (TREE_CODE (exp) == ADDR_EXPR)
848 exp = TREE_OPERAND (exp, 0);
849 set_mem_attributes (mem, exp, 0);
851 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
853 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
854 /* memcpy, memset and other builtin stringops can alias with anything. */
855 set_mem_alias_set (mem, 0);
861 /* Built-in functions to perform an untyped call and return. */
863 /* For each register that may be used for calling a function, this
864 gives a mode used to copy the register's value. VOIDmode indicates
865 the register is not used for calling a function. If the machine
866 has register windows, this gives only the outbound registers.
867 INCOMING_REGNO gives the corresponding inbound register. */
868 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
870 /* For each register that may be used for returning values, this gives
871 a mode used to copy the register's value. VOIDmode indicates the
872 register is not used for returning values. If the machine has
873 register windows, this gives only the outbound registers.
874 INCOMING_REGNO gives the corresponding inbound register. */
875 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
877 /* For each register that may be used for calling a function, this
878 gives the offset of that register into the block returned by
879 __builtin_apply_args. 0 indicates that the register is not
880 used for calling a function. */
881 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
883 /* Return the size required for the block returned by __builtin_apply_args,
884 and initialize apply_args_mode. */
887 apply_args_size (void)
889 static int size = -1;
892 enum machine_mode mode;
894 /* The values computed by this function never change. */
897 /* The first value is the incoming arg-pointer. */
898 size = GET_MODE_SIZE (Pmode);
900 /* The second value is the structure value address unless this is
901 passed as an "invisible" first argument. */
902 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
903 size += GET_MODE_SIZE (Pmode);
905 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
906 if (FUNCTION_ARG_REGNO_P (regno))
908 mode = reg_raw_mode[regno];
910 if (mode == VOIDmode)
913 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
914 if (size % align != 0)
915 size = CEIL (size, align) * align;
916 apply_args_reg_offset[regno] = size;
917 size += GET_MODE_SIZE (mode);
918 apply_args_mode[regno] = mode;
922 apply_args_mode[regno] = VOIDmode;
923 apply_args_reg_offset[regno] = 0;
929 /* Return the size required for the block returned by __builtin_apply,
930 and initialize apply_result_mode. */
933 apply_result_size (void)
935 static int size = -1;
937 enum machine_mode mode;
939 /* The values computed by this function never change. */
944 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
945 if (FUNCTION_VALUE_REGNO_P (regno))
947 mode = reg_raw_mode[regno];
949 if (mode == VOIDmode)
952 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
953 if (size % align != 0)
954 size = CEIL (size, align) * align;
955 size += GET_MODE_SIZE (mode);
956 apply_result_mode[regno] = mode;
959 apply_result_mode[regno] = VOIDmode;
961 /* Allow targets that use untyped_call and untyped_return to override
962 the size so that machine-specific information can be stored here. */
963 #ifdef APPLY_RESULT_SIZE
964 size = APPLY_RESULT_SIZE;
970 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
971 /* Create a vector describing the result block RESULT. If SAVEP is true,
972 the result block is used to save the values; otherwise it is used to
973 restore the values. */
976 result_vector (int savep, rtx result)
978 int regno, size, align, nelts;
979 enum machine_mode mode;
981 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
984 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
985 if ((mode = apply_result_mode[regno]) != VOIDmode)
987 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
988 if (size % align != 0)
989 size = CEIL (size, align) * align;
990 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
991 mem = adjust_address (result, mode, size);
992 savevec[nelts++] = (savep
993 ? gen_rtx_SET (VOIDmode, mem, reg)
994 : gen_rtx_SET (VOIDmode, reg, mem));
995 size += GET_MODE_SIZE (mode);
997 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
999 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1001 /* Save the state required to perform an untyped call with the same
1002 arguments as were passed to the current function. */
1005 expand_builtin_apply_args_1 (void)
1008 int size, align, regno;
1009 enum machine_mode mode;
1010 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1012 /* Create a block where the arg-pointer, structure value address,
1013 and argument registers can be saved. */
1014 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1016 /* Walk past the arg-pointer and structure value address. */
1017 size = GET_MODE_SIZE (Pmode);
1018 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1019 size += GET_MODE_SIZE (Pmode);
1021 /* Save each register used in calling a function to the block. */
1022 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1023 if ((mode = apply_args_mode[regno]) != VOIDmode)
1025 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1026 if (size % align != 0)
1027 size = CEIL (size, align) * align;
1029 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1031 emit_move_insn (adjust_address (registers, mode, size), tem);
1032 size += GET_MODE_SIZE (mode);
1035 /* Save the arg pointer to the block. */
1036 tem = copy_to_reg (virtual_incoming_args_rtx);
1037 #ifdef STACK_GROWS_DOWNWARD
1038 /* We need the pointer as the caller actually passed them to us, not
1039 as we might have pretended they were passed. Make sure it's a valid
1040 operand, as emit_move_insn isn't expected to handle a PLUS. */
1042 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1045 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1047 size = GET_MODE_SIZE (Pmode);
1049 /* Save the structure value address unless this is passed as an
1050 "invisible" first argument. */
1051 if (struct_incoming_value)
1053 emit_move_insn (adjust_address (registers, Pmode, size),
1054 copy_to_reg (struct_incoming_value));
1055 size += GET_MODE_SIZE (Pmode);
1058 /* Return the address of the block. */
1059 return copy_addr_to_reg (XEXP (registers, 0));
1062 /* __builtin_apply_args returns block of memory allocated on
1063 the stack into which is stored the arg pointer, structure
1064 value address, static chain, and all the registers that might
1065 possibly be used in performing a function call. The code is
1066 moved to the start of the function so the incoming values are
1070 expand_builtin_apply_args (void)
1072 /* Don't do __builtin_apply_args more than once in a function.
1073 Save the result of the first call and reuse it. */
1074 if (apply_args_value != 0)
1075 return apply_args_value;
1077 /* When this function is called, it means that registers must be
1078 saved on entry to this function. So we migrate the
1079 call to the first insn of this function. */
1084 temp = expand_builtin_apply_args_1 ();
1088 apply_args_value = temp;
1090 /* Put the insns after the NOTE that starts the function.
1091 If this is inside a start_sequence, make the outer-level insn
1092 chain current, so the code is placed at the start of the
1094 push_topmost_sequence ();
1095 emit_insn_before (seq, NEXT_INSN (get_insns ()));
1096 pop_topmost_sequence ();
1101 /* Perform an untyped call and save the state required to perform an
1102 untyped return of whatever value was returned by the given function. */
1105 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1107 int size, align, regno;
1108 enum machine_mode mode;
1109 rtx incoming_args, result, reg, dest, src, call_insn;
1110 rtx old_stack_level = 0;
1111 rtx call_fusage = 0;
1112 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1114 arguments = convert_memory_address (Pmode, arguments);
1116 /* Create a block where the return registers can be saved. */
1117 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1119 /* Fetch the arg pointer from the ARGUMENTS block. */
1120 incoming_args = gen_reg_rtx (Pmode);
1121 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1122 #ifndef STACK_GROWS_DOWNWARD
1123 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1124 incoming_args, 0, OPTAB_LIB_WIDEN);
1127 /* Perform postincrements before actually calling the function. */
1130 /* Push a new argument block and copy the arguments. Do not allow
1131 the (potential) memcpy call below to interfere with our stack
1133 do_pending_stack_adjust ();
1136 /* Save the stack with nonlocal if available. */
1137 #ifdef HAVE_save_stack_nonlocal
1138 if (HAVE_save_stack_nonlocal)
1139 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1142 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1144 /* Allocate a block of memory onto the stack and copy the memory
1145 arguments to the outgoing arguments address. */
1146 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1147 dest = virtual_outgoing_args_rtx;
1148 #ifndef STACK_GROWS_DOWNWARD
1149 if (GET_CODE (argsize) == CONST_INT)
1150 dest = plus_constant (dest, -INTVAL (argsize));
1152 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1154 dest = gen_rtx_MEM (BLKmode, dest);
1155 set_mem_align (dest, PARM_BOUNDARY);
1156 src = gen_rtx_MEM (BLKmode, incoming_args);
1157 set_mem_align (src, PARM_BOUNDARY);
1158 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1160 /* Refer to the argument block. */
1162 arguments = gen_rtx_MEM (BLKmode, arguments);
1163 set_mem_align (arguments, PARM_BOUNDARY);
1165 /* Walk past the arg-pointer and structure value address. */
1166 size = GET_MODE_SIZE (Pmode);
1168 size += GET_MODE_SIZE (Pmode);
1170 /* Restore each of the registers previously saved. Make USE insns
1171 for each of these registers for use in making the call. */
1172 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1173 if ((mode = apply_args_mode[regno]) != VOIDmode)
1175 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1176 if (size % align != 0)
1177 size = CEIL (size, align) * align;
1178 reg = gen_rtx_REG (mode, regno);
1179 emit_move_insn (reg, adjust_address (arguments, mode, size));
1180 use_reg (&call_fusage, reg);
1181 size += GET_MODE_SIZE (mode);
1184 /* Restore the structure value address unless this is passed as an
1185 "invisible" first argument. */
1186 size = GET_MODE_SIZE (Pmode);
1189 rtx value = gen_reg_rtx (Pmode);
1190 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1191 emit_move_insn (struct_value, value);
1192 if (GET_CODE (struct_value) == REG)
1193 use_reg (&call_fusage, struct_value);
1194 size += GET_MODE_SIZE (Pmode);
1197 /* All arguments and registers used for the call are set up by now! */
1198 function = prepare_call_address (function, NULL_TREE, &call_fusage, 0, 0);
1200 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1201 and we don't want to load it into a register as an optimization,
1202 because prepare_call_address already did it if it should be done. */
1203 if (GET_CODE (function) != SYMBOL_REF)
1204 function = memory_address (FUNCTION_MODE, function);
1206 /* Generate the actual call instruction and save the return value. */
1207 #ifdef HAVE_untyped_call
1208 if (HAVE_untyped_call)
1209 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1210 result, result_vector (1, result)));
1213 #ifdef HAVE_call_value
1214 if (HAVE_call_value)
1218 /* Locate the unique return register. It is not possible to
1219 express a call that sets more than one return register using
1220 call_value; use untyped_call for that. In fact, untyped_call
1221 only needs to save the return registers in the given block. */
1222 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1223 if ((mode = apply_result_mode[regno]) != VOIDmode)
1226 abort (); /* HAVE_untyped_call required. */
1227 valreg = gen_rtx_REG (mode, regno);
1230 emit_call_insn (GEN_CALL_VALUE (valreg,
1231 gen_rtx_MEM (FUNCTION_MODE, function),
1232 const0_rtx, NULL_RTX, const0_rtx));
1234 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1240 /* Find the CALL insn we just emitted, and attach the register usage
1242 call_insn = last_call_insn ();
1243 add_function_usage_to (call_insn, call_fusage);
1245 /* Restore the stack. */
1246 #ifdef HAVE_save_stack_nonlocal
1247 if (HAVE_save_stack_nonlocal)
1248 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1251 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1255 /* Return the address of the result block. */
1256 result = copy_addr_to_reg (XEXP (result, 0));
1257 return convert_memory_address (ptr_mode, result);
1260 /* Perform an untyped return. */
1263 expand_builtin_return (rtx result)
1265 int size, align, regno;
1266 enum machine_mode mode;
1268 rtx call_fusage = 0;
1270 result = convert_memory_address (Pmode, result);
1272 apply_result_size ();
1273 result = gen_rtx_MEM (BLKmode, result);
1275 #ifdef HAVE_untyped_return
1276 if (HAVE_untyped_return)
1278 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1284 /* Restore the return value and note that each value is used. */
1286 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1287 if ((mode = apply_result_mode[regno]) != VOIDmode)
1289 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1290 if (size % align != 0)
1291 size = CEIL (size, align) * align;
1292 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1293 emit_move_insn (reg, adjust_address (result, mode, size));
1295 push_to_sequence (call_fusage);
1296 emit_insn (gen_rtx_USE (VOIDmode, reg));
1297 call_fusage = get_insns ();
1299 size += GET_MODE_SIZE (mode);
1302 /* Put the USE insns before the return. */
1303 emit_insn (call_fusage);
1305 /* Return whatever values was restored by jumping directly to the end
1307 expand_naked_return ();
1310 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1312 static enum type_class
1313 type_to_class (tree type)
1315 switch (TREE_CODE (type))
1317 case VOID_TYPE: return void_type_class;
1318 case INTEGER_TYPE: return integer_type_class;
1319 case CHAR_TYPE: return char_type_class;
1320 case ENUMERAL_TYPE: return enumeral_type_class;
1321 case BOOLEAN_TYPE: return boolean_type_class;
1322 case POINTER_TYPE: return pointer_type_class;
1323 case REFERENCE_TYPE: return reference_type_class;
1324 case OFFSET_TYPE: return offset_type_class;
1325 case REAL_TYPE: return real_type_class;
1326 case COMPLEX_TYPE: return complex_type_class;
1327 case FUNCTION_TYPE: return function_type_class;
1328 case METHOD_TYPE: return method_type_class;
1329 case RECORD_TYPE: return record_type_class;
1331 case QUAL_UNION_TYPE: return union_type_class;
1332 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1333 ? string_type_class : array_type_class);
1334 case SET_TYPE: return set_type_class;
1335 case FILE_TYPE: return file_type_class;
1336 case LANG_TYPE: return lang_type_class;
1337 default: return no_type_class;
1341 /* Expand a call to __builtin_classify_type with arguments found in
1345 expand_builtin_classify_type (tree arglist)
1348 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1349 return GEN_INT (no_type_class);
1352 /* Expand expression EXP, which is a call to __builtin_constant_p. */
1355 expand_builtin_constant_p (tree arglist, enum machine_mode target_mode)
1361 arglist = TREE_VALUE (arglist);
1363 /* We have taken care of the easy cases during constant folding. This
1364 case is not obvious, so emit (constant_p_rtx (ARGLIST)) and let CSE
1365 get a chance to see if it can deduce whether ARGLIST is constant.
1366 If CSE isn't going to run, of course, don't bother waiting. */
1368 if (cse_not_expected)
1371 current_function_calls_constant_p = 1;
1373 tmp = expand_expr (arglist, NULL_RTX, VOIDmode, 0);
1374 tmp = gen_rtx_CONSTANT_P_RTX (target_mode, tmp);
1378 /* This helper macro, meant to be used in mathfn_built_in below,
1379 determines which among a set of three builtin math functions is
1380 appropriate for a given type mode. The `F' and `L' cases are
1381 automatically generated from the `double' case. */
1382 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1383 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1384 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1385 fcodel = BUILT_IN_MATHFN##L ; break;
1387 /* Return mathematic function equivalent to FN but operating directly
1388 on TYPE, if available. If we can't do the conversion, return zero. */
1390 mathfn_built_in (tree type, enum built_in_function fn)
1392 enum built_in_function fcode, fcodef, fcodel;
1396 CASE_MATHFN (BUILT_IN_ACOS)
1397 CASE_MATHFN (BUILT_IN_ACOSH)
1398 CASE_MATHFN (BUILT_IN_ASIN)
1399 CASE_MATHFN (BUILT_IN_ASINH)
1400 CASE_MATHFN (BUILT_IN_ATAN)
1401 CASE_MATHFN (BUILT_IN_ATAN2)
1402 CASE_MATHFN (BUILT_IN_ATANH)
1403 CASE_MATHFN (BUILT_IN_CBRT)
1404 CASE_MATHFN (BUILT_IN_CEIL)
1405 CASE_MATHFN (BUILT_IN_COPYSIGN)
1406 CASE_MATHFN (BUILT_IN_COS)
1407 CASE_MATHFN (BUILT_IN_COSH)
1408 CASE_MATHFN (BUILT_IN_DREM)
1409 CASE_MATHFN (BUILT_IN_ERF)
1410 CASE_MATHFN (BUILT_IN_ERFC)
1411 CASE_MATHFN (BUILT_IN_EXP)
1412 CASE_MATHFN (BUILT_IN_EXP10)
1413 CASE_MATHFN (BUILT_IN_EXP2)
1414 CASE_MATHFN (BUILT_IN_EXPM1)
1415 CASE_MATHFN (BUILT_IN_FABS)
1416 CASE_MATHFN (BUILT_IN_FDIM)
1417 CASE_MATHFN (BUILT_IN_FLOOR)
1418 CASE_MATHFN (BUILT_IN_FMA)
1419 CASE_MATHFN (BUILT_IN_FMAX)
1420 CASE_MATHFN (BUILT_IN_FMIN)
1421 CASE_MATHFN (BUILT_IN_FMOD)
1422 CASE_MATHFN (BUILT_IN_FREXP)
1423 CASE_MATHFN (BUILT_IN_GAMMA)
1424 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1425 CASE_MATHFN (BUILT_IN_HYPOT)
1426 CASE_MATHFN (BUILT_IN_ILOGB)
1427 CASE_MATHFN (BUILT_IN_INF)
1428 CASE_MATHFN (BUILT_IN_J0)
1429 CASE_MATHFN (BUILT_IN_J1)
1430 CASE_MATHFN (BUILT_IN_JN)
1431 CASE_MATHFN (BUILT_IN_LDEXP)
1432 CASE_MATHFN (BUILT_IN_LGAMMA)
1433 CASE_MATHFN (BUILT_IN_LLRINT)
1434 CASE_MATHFN (BUILT_IN_LLROUND)
1435 CASE_MATHFN (BUILT_IN_LOG)
1436 CASE_MATHFN (BUILT_IN_LOG10)
1437 CASE_MATHFN (BUILT_IN_LOG1P)
1438 CASE_MATHFN (BUILT_IN_LOG2)
1439 CASE_MATHFN (BUILT_IN_LOGB)
1440 CASE_MATHFN (BUILT_IN_LRINT)
1441 CASE_MATHFN (BUILT_IN_LROUND)
1442 CASE_MATHFN (BUILT_IN_MODF)
1443 CASE_MATHFN (BUILT_IN_NAN)
1444 CASE_MATHFN (BUILT_IN_NANS)
1445 CASE_MATHFN (BUILT_IN_NEARBYINT)
1446 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1447 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1448 CASE_MATHFN (BUILT_IN_POW)
1449 CASE_MATHFN (BUILT_IN_POW10)
1450 CASE_MATHFN (BUILT_IN_REMAINDER)
1451 CASE_MATHFN (BUILT_IN_REMQUO)
1452 CASE_MATHFN (BUILT_IN_RINT)
1453 CASE_MATHFN (BUILT_IN_ROUND)
1454 CASE_MATHFN (BUILT_IN_SCALB)
1455 CASE_MATHFN (BUILT_IN_SCALBLN)
1456 CASE_MATHFN (BUILT_IN_SCALBN)
1457 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1458 CASE_MATHFN (BUILT_IN_SIN)
1459 CASE_MATHFN (BUILT_IN_SINCOS)
1460 CASE_MATHFN (BUILT_IN_SINH)
1461 CASE_MATHFN (BUILT_IN_SQRT)
1462 CASE_MATHFN (BUILT_IN_TAN)
1463 CASE_MATHFN (BUILT_IN_TANH)
1464 CASE_MATHFN (BUILT_IN_TGAMMA)
1465 CASE_MATHFN (BUILT_IN_TRUNC)
1466 CASE_MATHFN (BUILT_IN_Y0)
1467 CASE_MATHFN (BUILT_IN_Y1)
1468 CASE_MATHFN (BUILT_IN_YN)
1474 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1475 return implicit_built_in_decls[fcode];
1476 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1477 return implicit_built_in_decls[fcodef];
1478 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1479 return implicit_built_in_decls[fcodel];
1484 /* If errno must be maintained, expand the RTL to check if the result,
1485 TARGET, of a built-in function call, EXP, is NaN, and if so set
1489 expand_errno_check (tree exp, rtx target)
1491 rtx lab = gen_label_rtx ();
1493 /* Test the result; if it is NaN, set errno=EDOM because
1494 the argument was not in the domain. */
1495 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1499 /* If this built-in doesn't throw an exception, set errno directly. */
1500 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1502 #ifdef GEN_ERRNO_RTX
1503 rtx errno_rtx = GEN_ERRNO_RTX;
1506 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1508 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1514 /* We can't set errno=EDOM directly; let the library call do it.
1515 Pop the arguments right away in case the call gets deleted. */
1517 expand_call (exp, target, 0);
1523 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1524 Return 0 if a normal call should be emitted rather than expanding the
1525 function in-line. EXP is the expression that is a call to the builtin
1526 function; if convenient, the result should be placed in TARGET.
1527 SUBTARGET may be used as the target for computing one of EXP's operands. */
1530 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1532 optab builtin_optab;
1533 rtx op0, insns, before_call;
1534 tree fndecl = get_callee_fndecl (exp);
1535 tree arglist = TREE_OPERAND (exp, 1);
1536 enum machine_mode mode;
1537 bool errno_set = false;
1540 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1543 arg = TREE_VALUE (arglist);
1545 switch (DECL_FUNCTION_CODE (fndecl))
1548 case BUILT_IN_SQRTF:
1549 case BUILT_IN_SQRTL:
1550 errno_set = ! tree_expr_nonnegative_p (arg);
1551 builtin_optab = sqrt_optab;
1556 errno_set = true; builtin_optab = exp_optab; break;
1557 case BUILT_IN_EXP10:
1558 case BUILT_IN_EXP10F:
1559 case BUILT_IN_EXP10L:
1560 case BUILT_IN_POW10:
1561 case BUILT_IN_POW10F:
1562 case BUILT_IN_POW10L:
1563 errno_set = true; builtin_optab = exp10_optab; break;
1565 case BUILT_IN_EXP2F:
1566 case BUILT_IN_EXP2L:
1567 errno_set = true; builtin_optab = exp2_optab; break;
1569 case BUILT_IN_LOGBF:
1570 case BUILT_IN_LOGBL:
1571 errno_set = true; builtin_optab = logb_optab; break;
1572 case BUILT_IN_ILOGB:
1573 case BUILT_IN_ILOGBF:
1574 case BUILT_IN_ILOGBL:
1575 errno_set = true; builtin_optab = ilogb_optab; break;
1579 errno_set = true; builtin_optab = log_optab; break;
1580 case BUILT_IN_LOG10:
1581 case BUILT_IN_LOG10F:
1582 case BUILT_IN_LOG10L:
1583 errno_set = true; builtin_optab = log10_optab; break;
1585 case BUILT_IN_LOG2F:
1586 case BUILT_IN_LOG2L:
1587 errno_set = true; builtin_optab = log2_optab; break;
1591 builtin_optab = tan_optab; break;
1593 case BUILT_IN_ATANF:
1594 case BUILT_IN_ATANL:
1595 builtin_optab = atan_optab; break;
1596 case BUILT_IN_FLOOR:
1597 case BUILT_IN_FLOORF:
1598 case BUILT_IN_FLOORL:
1599 builtin_optab = floor_optab; break;
1601 case BUILT_IN_CEILF:
1602 case BUILT_IN_CEILL:
1603 builtin_optab = ceil_optab; break;
1604 case BUILT_IN_TRUNC:
1605 case BUILT_IN_TRUNCF:
1606 case BUILT_IN_TRUNCL:
1607 builtin_optab = btrunc_optab; break;
1608 case BUILT_IN_ROUND:
1609 case BUILT_IN_ROUNDF:
1610 case BUILT_IN_ROUNDL:
1611 builtin_optab = round_optab; break;
1612 case BUILT_IN_NEARBYINT:
1613 case BUILT_IN_NEARBYINTF:
1614 case BUILT_IN_NEARBYINTL:
1615 builtin_optab = nearbyint_optab; break;
1620 /* Make a suitable register to place result in. */
1621 mode = TYPE_MODE (TREE_TYPE (exp));
1623 if (! flag_errno_math || ! HONOR_NANS (mode))
1626 /* Before working hard, check whether the instruction is available. */
1627 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1629 target = gen_reg_rtx (mode);
1631 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1632 need to expand the argument again. This way, we will not perform
1633 side-effects more the once. */
1634 narg = save_expr (arg);
1637 arglist = build_tree_list (NULL_TREE, arg);
1638 exp = build_function_call_expr (fndecl, arglist);
1641 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1646 /* Compute into TARGET.
1647 Set TARGET to wherever the result comes back. */
1648 target = expand_unop (mode, builtin_optab, op0, target, 0);
1653 expand_errno_check (exp, target);
1655 /* Output the entire sequence. */
1656 insns = get_insns ();
1662 /* If we were unable to expand via the builtin, stop the sequence
1663 (without outputting the insns) and call to the library function
1664 with the stabilized argument list. */
1668 before_call = get_last_insn ();
1670 target = expand_call (exp, target, target == const0_rtx);
1672 /* If this is a sqrt operation and we don't care about errno, try to
1673 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1674 This allows the semantics of the libcall to be visible to the RTL
1676 if (builtin_optab == sqrt_optab && !errno_set)
1678 /* Search backwards through the insns emitted by expand_call looking
1679 for the instruction with the REG_RETVAL note. */
1680 rtx last = get_last_insn ();
1681 while (last != before_call)
1683 if (find_reg_note (last, REG_RETVAL, NULL))
1685 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1686 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1687 two elements, i.e. symbol_ref(sqrt) and the operand. */
1689 && GET_CODE (note) == EXPR_LIST
1690 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1691 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1692 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1694 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1695 /* Check operand is a register with expected mode. */
1697 && GET_CODE (operand) == REG
1698 && GET_MODE (operand) == mode)
1700 /* Replace the REG_EQUAL note with a SQRT rtx. */
1701 rtx equiv = gen_rtx_SQRT (mode, operand);
1702 set_unique_reg_note (last, REG_EQUAL, equiv);
1707 last = PREV_INSN (last);
1714 /* Expand a call to the builtin binary math functions (pow and atan2).
1715 Return 0 if a normal call should be emitted rather than expanding the
1716 function in-line. EXP is the expression that is a call to the builtin
1717 function; if convenient, the result should be placed in TARGET.
1718 SUBTARGET may be used as the target for computing one of EXP's
1722 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1724 optab builtin_optab;
1725 rtx op0, op1, insns;
1726 tree fndecl = get_callee_fndecl (exp);
1727 tree arglist = TREE_OPERAND (exp, 1);
1728 tree arg0, arg1, temp, narg;
1729 enum machine_mode mode;
1730 bool errno_set = true;
1733 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
1736 arg0 = TREE_VALUE (arglist);
1737 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1739 switch (DECL_FUNCTION_CODE (fndecl))
1744 builtin_optab = pow_optab; break;
1745 case BUILT_IN_ATAN2:
1746 case BUILT_IN_ATAN2F:
1747 case BUILT_IN_ATAN2L:
1748 builtin_optab = atan2_optab; break;
1753 /* Make a suitable register to place result in. */
1754 mode = TYPE_MODE (TREE_TYPE (exp));
1756 /* Before working hard, check whether the instruction is available. */
1757 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1760 target = gen_reg_rtx (mode);
1762 if (! flag_errno_math || ! HONOR_NANS (mode))
1765 /* Alway stabilize the argument list. */
1766 narg = save_expr (arg1);
1769 temp = build_tree_list (NULL_TREE, narg);
1773 temp = TREE_CHAIN (arglist);
1775 narg = save_expr (arg0);
1778 arglist = tree_cons (NULL_TREE, narg, temp);
1782 arglist = tree_cons (NULL_TREE, arg0, temp);
1785 exp = build_function_call_expr (fndecl, arglist);
1787 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1788 op1 = expand_expr (arg1, 0, VOIDmode, 0);
1793 /* Compute into TARGET.
1794 Set TARGET to wherever the result comes back. */
1795 target = expand_binop (mode, builtin_optab, op0, op1,
1796 target, 0, OPTAB_DIRECT);
1798 /* If we were unable to expand via the builtin, stop the sequence
1799 (without outputting the insns) and call to the library function
1800 with the stabilized argument list. */
1804 return expand_call (exp, target, target == const0_rtx);
1808 expand_errno_check (exp, target);
1810 /* Output the entire sequence. */
1811 insns = get_insns ();
1818 /* Expand a call to the builtin sin and cos math functions.
1819 Return 0 if a normal call should be emitted rather than expanding the
1820 function in-line. EXP is the expression that is a call to the builtin
1821 function; if convenient, the result should be placed in TARGET.
1822 SUBTARGET may be used as the target for computing one of EXP's
1826 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
1828 optab builtin_optab;
1829 rtx op0, insns, before_call;
1830 tree fndecl = get_callee_fndecl (exp);
1831 tree arglist = TREE_OPERAND (exp, 1);
1832 enum machine_mode mode;
1833 bool errno_set = false;
1836 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1839 arg = TREE_VALUE (arglist);
1841 switch (DECL_FUNCTION_CODE (fndecl))
1849 builtin_optab = sincos_optab; break;
1854 /* Make a suitable register to place result in. */
1855 mode = TYPE_MODE (TREE_TYPE (exp));
1857 if (! flag_errno_math || ! HONOR_NANS (mode))
1860 /* Check if sincos insn is available, otherwise fallback
1861 to sin or cos insn. */
1862 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
1863 switch (DECL_FUNCTION_CODE (fndecl))
1868 builtin_optab = sin_optab; break;
1872 builtin_optab = cos_optab; break;
1878 /* Before working hard, check whether the instruction is available. */
1879 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1881 target = gen_reg_rtx (mode);
1883 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1884 need to expand the argument again. This way, we will not perform
1885 side-effects more the once. */
1886 narg = save_expr (arg);
1889 arglist = build_tree_list (NULL_TREE, arg);
1890 exp = build_function_call_expr (fndecl, arglist);
1893 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1898 /* Compute into TARGET.
1899 Set TARGET to wherever the result comes back. */
1900 if (builtin_optab == sincos_optab)
1902 switch (DECL_FUNCTION_CODE (fndecl))
1907 if (!expand_twoval_unop (builtin_optab, op0, 0, target, 0))
1913 if (!expand_twoval_unop (builtin_optab, op0, target, 0, 0))
1922 target = expand_unop (mode, builtin_optab, op0, target, 0);
1928 expand_errno_check (exp, target);
1930 /* Output the entire sequence. */
1931 insns = get_insns ();
1937 /* If we were unable to expand via the builtin, stop the sequence
1938 (without outputting the insns) and call to the library function
1939 with the stabilized argument list. */
1943 before_call = get_last_insn ();
1945 target = expand_call (exp, target, target == const0_rtx);
1950 /* To evaluate powi(x,n), the floating point value x raised to the
1951 constant integer exponent n, we use a hybrid algorithm that
1952 combines the "window method" with look-up tables. For an
1953 introduction to exponentiation algorithms and "addition chains",
1954 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
1955 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
1956 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
1957 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
1959 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
1960 multiplications to inline before calling the system library's pow
1961 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
1962 so this default never requires calling pow, powf or powl. */
1964 #ifndef POWI_MAX_MULTS
1965 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
1968 /* The size of the "optimal power tree" lookup table. All
1969 exponents less than this value are simply looked up in the
1970 powi_table below. This threshold is also used to size the
1971 cache of pseudo registers that hold intermediate results. */
1972 #define POWI_TABLE_SIZE 256
1974 /* The size, in bits of the window, used in the "window method"
1975 exponentiation algorithm. This is equivalent to a radix of
1976 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
1977 #define POWI_WINDOW_SIZE 3
1979 /* The following table is an efficient representation of an
1980 "optimal power tree". For each value, i, the corresponding
1981 value, j, in the table states than an optimal evaluation
1982 sequence for calculating pow(x,i) can be found by evaluating
1983 pow(x,j)*pow(x,i-j). An optimal power tree for the first
1984 100 integers is given in Knuth's "Seminumerical algorithms". */
1986 static const unsigned char powi_table[POWI_TABLE_SIZE] =
1988 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
1989 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
1990 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
1991 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
1992 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
1993 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
1994 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
1995 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
1996 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
1997 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
1998 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
1999 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2000 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2001 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2002 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2003 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2004 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2005 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2006 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2007 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2008 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2009 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2010 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2011 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2012 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2013 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2014 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2015 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2016 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2017 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2018 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2019 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2023 /* Return the number of multiplications required to calculate
2024 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2025 subroutine of powi_cost. CACHE is an array indicating
2026 which exponents have already been calculated. */
2029 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2031 /* If we've already calculated this exponent, then this evaluation
2032 doesn't require any additional multiplications. */
2037 return powi_lookup_cost (n - powi_table[n], cache)
2038 + powi_lookup_cost (powi_table[n], cache) + 1;
2041 /* Return the number of multiplications required to calculate
2042 powi(x,n) for an arbitrary x, given the exponent N. This
2043 function needs to be kept in sync with expand_powi below. */
2046 powi_cost (HOST_WIDE_INT n)
2048 bool cache[POWI_TABLE_SIZE];
2049 unsigned HOST_WIDE_INT digit;
2050 unsigned HOST_WIDE_INT val;
2056 /* Ignore the reciprocal when calculating the cost. */
2057 val = (n < 0) ? -n : n;
2059 /* Initialize the exponent cache. */
2060 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2065 while (val >= POWI_TABLE_SIZE)
2069 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2070 result += powi_lookup_cost (digit, cache)
2071 + POWI_WINDOW_SIZE + 1;
2072 val >>= POWI_WINDOW_SIZE;
2081 return result + powi_lookup_cost (val, cache);
2084 /* Recursive subroutine of expand_powi. This function takes the array,
2085 CACHE, of already calculated exponents and an exponent N and returns
2086 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2089 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2091 unsigned HOST_WIDE_INT digit;
2095 if (n < POWI_TABLE_SIZE)
2100 target = gen_reg_rtx (mode);
2103 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2104 op1 = expand_powi_1 (mode, powi_table[n], cache);
2108 target = gen_reg_rtx (mode);
2109 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2110 op0 = expand_powi_1 (mode, n - digit, cache);
2111 op1 = expand_powi_1 (mode, digit, cache);
2115 target = gen_reg_rtx (mode);
2116 op0 = expand_powi_1 (mode, n >> 1, cache);
2120 result = expand_mult (mode, op0, op1, target, 0);
2121 if (result != target)
2122 emit_move_insn (target, result);
2126 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2127 floating point operand in mode MODE, and N is the exponent. This
2128 function needs to be kept in sync with powi_cost above. */
2131 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2133 unsigned HOST_WIDE_INT val;
2134 rtx cache[POWI_TABLE_SIZE];
2138 return CONST1_RTX (mode);
2140 val = (n < 0) ? -n : n;
2142 memset (cache, 0, sizeof (cache));
2145 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2147 /* If the original exponent was negative, reciprocate the result. */
2149 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2150 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2155 /* Expand a call to the pow built-in mathematical function. Return 0 if
2156 a normal call should be emitted rather than expanding the function
2157 in-line. EXP is the expression that is a call to the builtin
2158 function; if convenient, the result should be placed in TARGET. */
2161 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2163 tree arglist = TREE_OPERAND (exp, 1);
2166 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2169 arg0 = TREE_VALUE (arglist);
2170 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2172 if (TREE_CODE (arg1) == REAL_CST
2173 && ! TREE_CONSTANT_OVERFLOW (arg1))
2175 REAL_VALUE_TYPE cint;
2179 c = TREE_REAL_CST (arg1);
2180 n = real_to_integer (&c);
2181 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2182 if (real_identical (&c, &cint))
2184 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2185 Otherwise, check the number of multiplications required.
2186 Note that pow never sets errno for an integer exponent. */
2187 if ((n >= -1 && n <= 2)
2188 || (flag_unsafe_math_optimizations
2190 && powi_cost (n) <= POWI_MAX_MULTS))
2192 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2193 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2194 op = force_reg (mode, op);
2195 return expand_powi (op, mode, n);
2200 if (! flag_unsafe_math_optimizations)
2202 return expand_builtin_mathfn_2 (exp, target, subtarget);
2205 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2206 if we failed the caller should emit a normal call, otherwise
2207 try to get the result in TARGET, if convenient. */
2210 expand_builtin_strlen (tree arglist, rtx target,
2211 enum machine_mode target_mode)
2213 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2218 tree len, src = TREE_VALUE (arglist);
2219 rtx result, src_reg, char_rtx, before_strlen;
2220 enum machine_mode insn_mode = target_mode, char_mode;
2221 enum insn_code icode = CODE_FOR_nothing;
2224 /* If the length can be computed at compile-time, return it. */
2225 len = c_strlen (src, 0);
2227 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2229 /* If the length can be computed at compile-time and is constant
2230 integer, but there are side-effects in src, evaluate
2231 src for side-effects, then return len.
2232 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2233 can be optimized into: i++; x = 3; */
2234 len = c_strlen (src, 1);
2235 if (len && TREE_CODE (len) == INTEGER_CST)
2237 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2238 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2241 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2243 /* If SRC is not a pointer type, don't do this operation inline. */
2247 /* Bail out if we can't compute strlen in the right mode. */
2248 while (insn_mode != VOIDmode)
2250 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2251 if (icode != CODE_FOR_nothing)
2254 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2256 if (insn_mode == VOIDmode)
2259 /* Make a place to write the result of the instruction. */
2262 && GET_CODE (result) == REG
2263 && GET_MODE (result) == insn_mode
2264 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2265 result = gen_reg_rtx (insn_mode);
2267 /* Make a place to hold the source address. We will not expand
2268 the actual source until we are sure that the expansion will
2269 not fail -- there are trees that cannot be expanded twice. */
2270 src_reg = gen_reg_rtx (Pmode);
2272 /* Mark the beginning of the strlen sequence so we can emit the
2273 source operand later. */
2274 before_strlen = get_last_insn ();
2276 char_rtx = const0_rtx;
2277 char_mode = insn_data[(int) icode].operand[2].mode;
2278 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2280 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2282 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2283 char_rtx, GEN_INT (align));
2288 /* Now that we are assured of success, expand the source. */
2290 pat = memory_address (BLKmode,
2291 expand_expr (src, src_reg, ptr_mode, EXPAND_SUM));
2293 emit_move_insn (src_reg, pat);
2298 emit_insn_after (pat, before_strlen);
2300 emit_insn_before (pat, get_insns ());
2302 /* Return the value in the proper mode for this function. */
2303 if (GET_MODE (result) == target_mode)
2305 else if (target != 0)
2306 convert_move (target, result, 0);
2308 target = convert_to_mode (target_mode, result, 0);
2314 /* Expand a call to the strstr builtin. Return 0 if we failed the
2315 caller should emit a normal call, otherwise try to get the result
2316 in TARGET, if convenient (and in mode MODE if that's convenient). */
2319 expand_builtin_strstr (tree arglist, rtx target, enum machine_mode mode)
2321 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2325 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2327 const char *p1, *p2;
2336 const char *r = strstr (p1, p2);
2341 /* Return an offset into the constant string argument. */
2342 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2343 s1, convert (TREE_TYPE (s1),
2344 ssize_int (r - p1)))),
2345 target, mode, EXPAND_NORMAL);
2349 return expand_expr (s1, target, mode, EXPAND_NORMAL);
2354 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2358 /* New argument list transforming strstr(s1, s2) to
2359 strchr(s1, s2[0]). */
2361 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
2362 arglist = tree_cons (NULL_TREE, s1, arglist);
2363 return expand_expr (build_function_call_expr (fn, arglist),
2364 target, mode, EXPAND_NORMAL);
2368 /* Expand a call to the strchr builtin. Return 0 if we failed the
2369 caller should emit a normal call, otherwise try to get the result
2370 in TARGET, if convenient (and in mode MODE if that's convenient). */
2373 expand_builtin_strchr (tree arglist, rtx target, enum machine_mode mode)
2375 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2379 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2382 if (TREE_CODE (s2) != INTEGER_CST)
2391 if (target_char_cast (s2, &c))
2399 /* Return an offset into the constant string argument. */
2400 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2401 s1, convert (TREE_TYPE (s1),
2402 ssize_int (r - p1)))),
2403 target, mode, EXPAND_NORMAL);
2406 /* FIXME: Should use here strchrM optab so that ports can optimize
2412 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2413 caller should emit a normal call, otherwise try to get the result
2414 in TARGET, if convenient (and in mode MODE if that's convenient). */
2417 expand_builtin_strrchr (tree arglist, rtx target, enum machine_mode mode)
2419 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2423 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2427 if (TREE_CODE (s2) != INTEGER_CST)
2436 if (target_char_cast (s2, &c))
2439 r = strrchr (p1, c);
2444 /* Return an offset into the constant string argument. */
2445 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2446 s1, convert (TREE_TYPE (s1),
2447 ssize_int (r - p1)))),
2448 target, mode, EXPAND_NORMAL);
2451 if (! integer_zerop (s2))
2454 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2458 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
2459 return expand_expr (build_function_call_expr (fn, arglist),
2460 target, mode, EXPAND_NORMAL);
2464 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2465 caller should emit a normal call, otherwise try to get the result
2466 in TARGET, if convenient (and in mode MODE if that's convenient). */
2469 expand_builtin_strpbrk (tree arglist, rtx target, enum machine_mode mode)
2471 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2475 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2477 const char *p1, *p2;
2486 const char *r = strpbrk (p1, p2);
2491 /* Return an offset into the constant string argument. */
2492 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
2493 s1, convert (TREE_TYPE (s1),
2494 ssize_int (r - p1)))),
2495 target, mode, EXPAND_NORMAL);
2500 /* strpbrk(x, "") == NULL.
2501 Evaluate and ignore the arguments in case they had
2503 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2508 return 0; /* Really call strpbrk. */
2510 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
2514 /* New argument list transforming strpbrk(s1, s2) to
2515 strchr(s1, s2[0]). */
2517 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
2518 arglist = tree_cons (NULL_TREE, s1, arglist);
2519 return expand_expr (build_function_call_expr (fn, arglist),
2520 target, mode, EXPAND_NORMAL);
2524 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2525 bytes from constant string DATA + OFFSET and return it as target
2529 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2530 enum machine_mode mode)
2532 const char *str = (const char *) data;
2535 || ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2536 > strlen (str) + 1))
2537 abort (); /* Attempt to read past the end of constant string. */
2539 return c_readstr (str + offset, mode);
2542 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2543 Return 0 if we failed, the caller should emit a normal call,
2544 otherwise try to get the result in TARGET, if convenient (and in
2545 mode MODE if that's convenient). */
2547 expand_builtin_memcpy (tree arglist, rtx target, enum machine_mode mode)
2549 if (!validate_arglist (arglist,
2550 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2554 tree dest = TREE_VALUE (arglist);
2555 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2556 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2557 const char *src_str;
2558 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2559 unsigned int dest_align
2560 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2561 rtx dest_mem, src_mem, dest_addr, len_rtx;
2563 /* If DEST is not a pointer type, call the normal function. */
2564 if (dest_align == 0)
2567 /* If the LEN parameter is zero, return DEST. */
2568 if (integer_zerop (len))
2570 /* Evaluate and ignore SRC in case it has side-effects. */
2571 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2572 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2575 /* If SRC and DEST are the same (and not volatile), return DEST. */
2576 if (operand_equal_p (src, dest, 0))
2578 /* Evaluate and ignore LEN in case it has side-effects. */
2579 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2580 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2583 /* If either SRC is not a pointer type, don't do this
2584 operation in-line. */
2588 dest_mem = get_memory_rtx (dest);
2589 set_mem_align (dest_mem, dest_align);
2590 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2591 src_str = c_getstr (src);
2593 /* If SRC is a string constant and block move would be done
2594 by pieces, we can avoid loading the string from memory
2595 and only stored the computed constants. */
2597 && GET_CODE (len_rtx) == CONST_INT
2598 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2599 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2600 (void *) src_str, dest_align))
2602 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2603 builtin_memcpy_read_str,
2604 (void *) src_str, dest_align, 0);
2605 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2606 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2610 src_mem = get_memory_rtx (src);
2611 set_mem_align (src_mem, src_align);
2613 /* Copy word part most expediently. */
2614 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2619 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2620 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2626 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2627 Return 0 if we failed the caller should emit a normal call,
2628 otherwise try to get the result in TARGET, if convenient (and in
2629 mode MODE if that's convenient). If ENDP is 0 return the
2630 destination pointer, if ENDP is 1 return the end pointer ala
2631 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2635 expand_builtin_mempcpy (tree arglist, rtx target, enum machine_mode mode,
2638 if (!validate_arglist (arglist,
2639 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2641 /* If return value is ignored, transform mempcpy into memcpy. */
2642 else if (target == const0_rtx)
2644 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2649 return expand_expr (build_function_call_expr (fn, arglist),
2650 target, mode, EXPAND_NORMAL);
2654 tree dest = TREE_VALUE (arglist);
2655 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2656 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2657 const char *src_str;
2658 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2659 unsigned int dest_align
2660 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2661 rtx dest_mem, src_mem, len_rtx;
2663 /* If DEST is not a pointer type, call the normal function. */
2664 if (dest_align == 0)
2667 /* If SRC and DEST are the same (and not volatile), do nothing. */
2668 if (operand_equal_p (src, dest, 0))
2674 /* Evaluate and ignore LEN in case it has side-effects. */
2675 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2676 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2680 len = fold (build (MINUS_EXPR, TREE_TYPE (len), dest,
2682 len = convert (TREE_TYPE (dest), len);
2683 expr = fold (build (PLUS_EXPR, TREE_TYPE (dest), dest, len));
2684 return expand_expr (expr, target, mode, EXPAND_NORMAL);
2687 /* If LEN is not constant, call the normal function. */
2688 if (! host_integerp (len, 1))
2691 /* If the LEN parameter is zero, return DEST. */
2692 if (tree_low_cst (len, 1) == 0)
2694 /* Evaluate and ignore SRC in case it has side-effects. */
2695 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2696 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2699 /* If either SRC is not a pointer type, don't do this
2700 operation in-line. */
2704 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2705 src_str = c_getstr (src);
2707 /* If SRC is a string constant and block move would be done
2708 by pieces, we can avoid loading the string from memory
2709 and only stored the computed constants. */
2711 && GET_CODE (len_rtx) == CONST_INT
2712 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2713 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2714 (void *) src_str, dest_align))
2716 dest_mem = get_memory_rtx (dest);
2717 set_mem_align (dest_mem, dest_align);
2718 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2719 builtin_memcpy_read_str,
2720 (void *) src_str, dest_align, endp);
2721 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2722 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2726 if (GET_CODE (len_rtx) == CONST_INT
2727 && can_move_by_pieces (INTVAL (len_rtx),
2728 MIN (dest_align, src_align)))
2730 dest_mem = get_memory_rtx (dest);
2731 set_mem_align (dest_mem, dest_align);
2732 src_mem = get_memory_rtx (src);
2733 set_mem_align (src_mem, src_align);
2734 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2735 MIN (dest_align, src_align), endp);
2736 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2737 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2745 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2746 if we failed the caller should emit a normal call. */
2749 expand_builtin_memmove (tree arglist, rtx target, enum machine_mode mode)
2751 if (!validate_arglist (arglist,
2752 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2756 tree dest = TREE_VALUE (arglist);
2757 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2758 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2760 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2761 unsigned int dest_align
2762 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2764 /* If DEST is not a pointer type, call the normal function. */
2765 if (dest_align == 0)
2768 /* If the LEN parameter is zero, return DEST. */
2769 if (integer_zerop (len))
2771 /* Evaluate and ignore SRC in case it has side-effects. */
2772 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2773 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2776 /* If SRC and DEST are the same (and not volatile), return DEST. */
2777 if (operand_equal_p (src, dest, 0))
2779 /* Evaluate and ignore LEN in case it has side-effects. */
2780 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2781 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2784 /* If either SRC is not a pointer type, don't do this
2785 operation in-line. */
2789 /* If src is categorized for a readonly section we can use
2791 if (readonly_data_expr (src))
2793 tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2796 return expand_expr (build_function_call_expr (fn, arglist),
2797 target, mode, EXPAND_NORMAL);
2800 /* Otherwise, call the normal function. */
2805 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2806 if we failed the caller should emit a normal call. */
2809 expand_builtin_bcopy (tree arglist)
2811 tree src, dest, size, newarglist;
2813 if (!validate_arglist (arglist,
2814 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2817 src = TREE_VALUE (arglist);
2818 dest = TREE_VALUE (TREE_CHAIN (arglist));
2819 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2821 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2822 memmove(ptr y, ptr x, size_t z). This is done this way
2823 so that if it isn't expanded inline, we fallback to
2824 calling bcopy instead of memmove. */
2826 newarglist = build_tree_list (NULL_TREE, convert (sizetype, size));
2827 newarglist = tree_cons (NULL_TREE, src, newarglist);
2828 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2830 return expand_builtin_memmove (newarglist, const0_rtx, VOIDmode);
2833 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2834 if we failed the caller should emit a normal call, otherwise try to get
2835 the result in TARGET, if convenient (and in mode MODE if that's
2839 expand_builtin_strcpy (tree arglist, rtx target, enum machine_mode mode)
2841 tree fn, len, src, dst;
2843 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2846 src = TREE_VALUE (TREE_CHAIN (arglist));
2847 dst = TREE_VALUE (arglist);
2849 /* If SRC and DST are equal (and not volatile), return DST. */
2850 if (operand_equal_p (src, dst, 0))
2851 return expand_expr (dst, target, mode, EXPAND_NORMAL);
2853 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2857 len = c_strlen (src, 1);
2858 if (len == 0 || TREE_SIDE_EFFECTS (len))
2861 len = size_binop (PLUS_EXPR, len, ssize_int (1));
2862 arglist = build_tree_list (NULL_TREE, len);
2863 arglist = tree_cons (NULL_TREE, src, arglist);
2864 arglist = tree_cons (NULL_TREE, dst, arglist);
2865 return expand_expr (build_function_call_expr (fn, arglist),
2866 target, mode, EXPAND_NORMAL);
2869 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
2870 Return 0 if we failed the caller should emit a normal call,
2871 otherwise try to get the result in TARGET, if convenient (and in
2872 mode MODE if that's convenient). */
2875 expand_builtin_stpcpy (tree arglist, rtx target, enum machine_mode mode)
2877 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2883 /* If return value is ignored, transform stpcpy into strcpy. */
2884 if (target == const0_rtx)
2886 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
2890 return expand_expr (build_function_call_expr (fn, arglist),
2891 target, mode, EXPAND_NORMAL);
2894 /* Ensure we get an actual string whose length can be evaluated at
2895 compile-time, not an expression containing a string. This is
2896 because the latter will potentially produce pessimized code
2897 when used to produce the return value. */
2898 src = TREE_VALUE (TREE_CHAIN (arglist));
2899 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
2902 dst = TREE_VALUE (arglist);
2903 len = fold (size_binop (PLUS_EXPR, len, ssize_int (1)));
2904 arglist = build_tree_list (NULL_TREE, len);
2905 arglist = tree_cons (NULL_TREE, src, arglist);
2906 arglist = tree_cons (NULL_TREE, dst, arglist);
2907 return expand_builtin_mempcpy (arglist, target, mode, /*endp=*/2);
2911 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2912 bytes from constant string DATA + OFFSET and return it as target
2916 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
2917 enum machine_mode mode)
2919 const char *str = (const char *) data;
2921 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
2924 return c_readstr (str + offset, mode);
2927 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
2928 if we failed the caller should emit a normal call. */
2931 expand_builtin_strncpy (tree arglist, rtx target, enum machine_mode mode)
2933 if (!validate_arglist (arglist,
2934 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2938 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
2939 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2942 /* We must be passed a constant len parameter. */
2943 if (TREE_CODE (len) != INTEGER_CST)
2946 /* If the len parameter is zero, return the dst parameter. */
2947 if (integer_zerop (len))
2949 /* Evaluate and ignore the src argument in case it has
2951 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
2952 VOIDmode, EXPAND_NORMAL);
2953 /* Return the dst parameter. */
2954 return expand_expr (TREE_VALUE (arglist), target, mode,
2958 /* Now, we must be passed a constant src ptr parameter. */
2959 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
2962 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
2964 /* We're required to pad with trailing zeros if the requested
2965 len is greater than strlen(s2)+1. In that case try to
2966 use store_by_pieces, if it fails, punt. */
2967 if (tree_int_cst_lt (slen, len))
2969 tree dest = TREE_VALUE (arglist);
2970 unsigned int dest_align
2971 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2972 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
2975 if (!p || dest_align == 0 || !host_integerp (len, 1)
2976 || !can_store_by_pieces (tree_low_cst (len, 1),
2977 builtin_strncpy_read_str,
2978 (void *) p, dest_align))
2981 dest_mem = get_memory_rtx (dest);
2982 store_by_pieces (dest_mem, tree_low_cst (len, 1),
2983 builtin_strncpy_read_str,
2984 (void *) p, dest_align, 0);
2985 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2986 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2990 /* OK transform into builtin memcpy. */
2991 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2994 return expand_expr (build_function_call_expr (fn, arglist),
2995 target, mode, EXPAND_NORMAL);
2999 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3000 bytes from constant string DATA + OFFSET and return it as target
3004 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3005 enum machine_mode mode)
3007 const char *c = (const char *) data;
3008 char *p = alloca (GET_MODE_SIZE (mode));
3010 memset (p, *c, GET_MODE_SIZE (mode));
3012 return c_readstr (p, mode);
3015 /* Callback routine for store_by_pieces. Return the RTL of a register
3016 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3017 char value given in the RTL register data. For example, if mode is
3018 4 bytes wide, return the RTL for 0x01010101*data. */
3021 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3022 enum machine_mode mode)
3028 size = GET_MODE_SIZE (mode);
3033 memset (p, 1, size);
3034 coeff = c_readstr (p, mode);
3036 target = convert_to_mode (mode, (rtx) data, 1);
3037 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3038 return force_reg (mode, target);
3041 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3042 if we failed the caller should emit a normal call, otherwise try to get
3043 the result in TARGET, if convenient (and in mode MODE if that's
3047 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
3049 if (!validate_arglist (arglist,
3050 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3054 tree dest = TREE_VALUE (arglist);
3055 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3056 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3059 unsigned int dest_align
3060 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3061 rtx dest_mem, dest_addr, len_rtx;
3063 /* If DEST is not a pointer type, don't do this
3064 operation in-line. */
3065 if (dest_align == 0)
3068 /* If the LEN parameter is zero, return DEST. */
3069 if (integer_zerop (len))
3071 /* Evaluate and ignore VAL in case it has side-effects. */
3072 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3073 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3076 if (TREE_CODE (val) != INTEGER_CST)
3080 if (!host_integerp (len, 1))
3083 if (optimize_size && tree_low_cst (len, 1) > 1)
3086 /* Assume that we can memset by pieces if we can store the
3087 * the coefficients by pieces (in the required modes).
3088 * We can't pass builtin_memset_gen_str as that emits RTL. */
3090 if (!can_store_by_pieces (tree_low_cst (len, 1),
3091 builtin_memset_read_str,
3095 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3096 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3097 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3099 dest_mem = get_memory_rtx (dest);
3100 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3101 builtin_memset_gen_str,
3102 val_rtx, dest_align, 0);
3103 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3104 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3108 if (target_char_cast (val, &c))
3113 if (!host_integerp (len, 1))
3115 if (!can_store_by_pieces (tree_low_cst (len, 1),
3116 builtin_memset_read_str, &c,
3120 dest_mem = get_memory_rtx (dest);
3121 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3122 builtin_memset_read_str,
3124 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3125 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3129 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3131 dest_mem = get_memory_rtx (dest);
3132 set_mem_align (dest_mem, dest_align);
3133 dest_addr = clear_storage (dest_mem, len_rtx);
3137 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3138 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3145 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3146 if we failed the caller should emit a normal call. */
3149 expand_builtin_bzero (tree arglist)
3151 tree dest, size, newarglist;
3153 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3156 dest = TREE_VALUE (arglist);
3157 size = TREE_VALUE (TREE_CHAIN (arglist));
3159 /* New argument list transforming bzero(ptr x, int y) to
3160 memset(ptr x, int 0, size_t y). This is done this way
3161 so that if it isn't expanded inline, we fallback to
3162 calling bzero instead of memset. */
3164 newarglist = build_tree_list (NULL_TREE, convert (sizetype, size));
3165 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3166 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3168 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
3171 /* Expand expression EXP, which is a call to the memcmp built-in function.
3172 ARGLIST is the argument list for this call. Return 0 if we failed and the
3173 caller should emit a normal call, otherwise try to get the result in
3174 TARGET, if convenient (and in mode MODE, if that's convenient). */
3177 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3178 enum machine_mode mode)
3180 tree arg1, arg2, len;
3181 const char *p1, *p2;
3183 if (!validate_arglist (arglist,
3184 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3187 arg1 = TREE_VALUE (arglist);
3188 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3189 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3191 /* If the len parameter is zero, return zero. */
3192 if (integer_zerop (len))
3194 /* Evaluate and ignore arg1 and arg2 in case they have
3196 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3197 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3201 /* If both arguments are equal (and not volatile), return zero. */
3202 if (operand_equal_p (arg1, arg2, 0))
3204 /* Evaluate and ignore len in case it has side-effects. */
3205 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
3209 p1 = c_getstr (arg1);
3210 p2 = c_getstr (arg2);
3212 /* If all arguments are constant, and the value of len is not greater
3213 than the lengths of arg1 and arg2, evaluate at compile-time. */
3214 if (host_integerp (len, 1) && p1 && p2
3215 && compare_tree_int (len, strlen (p1) + 1) <= 0
3216 && compare_tree_int (len, strlen (p2) + 1) <= 0)
3218 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
3220 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
3223 /* If len parameter is one, return an expression corresponding to
3224 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
3225 if (integer_onep (len))
3227 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3228 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3230 fold (build1 (CONVERT_EXPR, integer_type_node,
3231 build1 (INDIRECT_REF, cst_uchar_node,
3232 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
3234 fold (build1 (CONVERT_EXPR, integer_type_node,
3235 build1 (INDIRECT_REF, cst_uchar_node,
3236 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
3237 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
3238 return expand_expr (result, target, mode, EXPAND_NORMAL);
3241 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3243 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3248 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3250 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3251 enum machine_mode insn_mode;
3253 #ifdef HAVE_cmpmemsi
3255 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3258 #ifdef HAVE_cmpstrsi
3260 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3265 /* If we don't have POINTER_TYPE, call the function. */
3266 if (arg1_align == 0 || arg2_align == 0)
3269 /* Make a place to write the result of the instruction. */
3272 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3273 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3274 result = gen_reg_rtx (insn_mode);
3276 arg1_rtx = get_memory_rtx (arg1);
3277 arg2_rtx = get_memory_rtx (arg2);
3278 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3279 #ifdef HAVE_cmpmemsi
3281 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3282 GEN_INT (MIN (arg1_align, arg2_align)));
3285 #ifdef HAVE_cmpstrsi
3287 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3288 GEN_INT (MIN (arg1_align, arg2_align)));
3296 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3297 TYPE_MODE (integer_type_node), 3,
3298 XEXP (arg1_rtx, 0), Pmode,
3299 XEXP (arg2_rtx, 0), Pmode,
3300 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3301 TYPE_UNSIGNED (sizetype)),
3302 TYPE_MODE (sizetype));
3304 /* Return the value in the proper mode for this function. */
3305 mode = TYPE_MODE (TREE_TYPE (exp));
3306 if (GET_MODE (result) == mode)
3308 else if (target != 0)
3310 convert_move (target, result, 0);
3314 return convert_to_mode (mode, result, 0);
3321 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3322 if we failed the caller should emit a normal call, otherwise try to get
3323 the result in TARGET, if convenient. */
3326 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3328 tree arglist = TREE_OPERAND (exp, 1);
3330 const char *p1, *p2;
3332 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3335 arg1 = TREE_VALUE (arglist);
3336 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3338 /* If both arguments are equal (and not volatile), return zero. */
3339 if (operand_equal_p (arg1, arg2, 0))
3342 p1 = c_getstr (arg1);
3343 p2 = c_getstr (arg2);
3347 const int i = strcmp (p1, p2);
3348 return (i < 0 ? constm1_rtx : (i > 0 ? const1_rtx : const0_rtx));
3351 /* If either arg is "", return an expression corresponding to
3352 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
3353 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
3355 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3356 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3358 fold (build1 (CONVERT_EXPR, integer_type_node,
3359 build1 (INDIRECT_REF, cst_uchar_node,
3360 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
3362 fold (build1 (CONVERT_EXPR, integer_type_node,
3363 build1 (INDIRECT_REF, cst_uchar_node,
3364 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
3365 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
3366 return expand_expr (result, target, mode, EXPAND_NORMAL);
3369 #ifdef HAVE_cmpstrsi
3372 tree len, len1, len2;
3373 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3378 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3380 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3381 enum machine_mode insn_mode
3382 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3384 len1 = c_strlen (arg1, 1);
3385 len2 = c_strlen (arg2, 1);
3388 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3390 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3392 /* If we don't have a constant length for the first, use the length
3393 of the second, if we know it. We don't require a constant for
3394 this case; some cost analysis could be done if both are available
3395 but neither is constant. For now, assume they're equally cheap,
3396 unless one has side effects. If both strings have constant lengths,
3403 else if (TREE_SIDE_EFFECTS (len1))
3405 else if (TREE_SIDE_EFFECTS (len2))
3407 else if (TREE_CODE (len1) != INTEGER_CST)
3409 else if (TREE_CODE (len2) != INTEGER_CST)
3411 else if (tree_int_cst_lt (len1, len2))
3416 /* If both arguments have side effects, we cannot optimize. */
3417 if (!len || TREE_SIDE_EFFECTS (len))
3420 /* If we don't have POINTER_TYPE, call the function. */
3421 if (arg1_align == 0 || arg2_align == 0)
3424 /* Make a place to write the result of the instruction. */
3427 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3428 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3429 result = gen_reg_rtx (insn_mode);
3431 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3432 arg1 = save_expr (arg1);
3433 arg2 = save_expr (arg2);
3435 arg1_rtx = get_memory_rtx (arg1);
3436 arg2_rtx = get_memory_rtx (arg2);
3437 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3438 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3439 GEN_INT (MIN (arg1_align, arg2_align)));
3444 /* Return the value in the proper mode for this function. */
3445 mode = TYPE_MODE (TREE_TYPE (exp));
3446 if (GET_MODE (result) == mode)
3449 return convert_to_mode (mode, result, 0);
3450 convert_move (target, result, 0);
3454 /* Expand the library call ourselves using a stabilized argument
3455 list to avoid re-evaluating the function's arguments twice. */
3456 arglist = build_tree_list (NULL_TREE, arg2);
3457 arglist = tree_cons (NULL_TREE, arg1, arglist);
3458 fndecl = get_callee_fndecl (exp);
3459 exp = build_function_call_expr (fndecl, arglist);
3460 return expand_call (exp, target, target == const0_rtx);
3466 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3467 if we failed the caller should emit a normal call, otherwise try to get
3468 the result in TARGET, if convenient. */
3471 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3473 tree arglist = TREE_OPERAND (exp, 1);
3474 tree arg1, arg2, arg3;
3475 const char *p1, *p2;
3477 if (!validate_arglist (arglist,
3478 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3481 arg1 = TREE_VALUE (arglist);
3482 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3483 arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3485 /* If the len parameter is zero, return zero. */
3486 if (integer_zerop (arg3))
3488 /* Evaluate and ignore arg1 and arg2 in case they have
3490 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3491 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3495 /* If arg1 and arg2 are equal (and not volatile), return zero. */
3496 if (operand_equal_p (arg1, arg2, 0))
3498 /* Evaluate and ignore arg3 in case it has side-effects. */
3499 expand_expr (arg3, const0_rtx, VOIDmode, EXPAND_NORMAL);
3503 p1 = c_getstr (arg1);
3504 p2 = c_getstr (arg2);
3506 /* If all arguments are constant, evaluate at compile-time. */
3507 if (host_integerp (arg3, 1) && p1 && p2)
3509 const int r = strncmp (p1, p2, tree_low_cst (arg3, 1));
3510 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
3513 /* If len == 1 or (either string parameter is "" and (len >= 1)),
3514 return (*(const u_char*)arg1 - *(const u_char*)arg2). */
3515 if (host_integerp (arg3, 1)
3516 && (tree_low_cst (arg3, 1) == 1
3517 || (tree_low_cst (arg3, 1) > 1
3518 && ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')))))
3520 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3521 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3523 fold (build1 (CONVERT_EXPR, integer_type_node,
3524 build1 (INDIRECT_REF, cst_uchar_node,
3525 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
3527 fold (build1 (CONVERT_EXPR, integer_type_node,
3528 build1 (INDIRECT_REF, cst_uchar_node,
3529 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
3530 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
3531 return expand_expr (result, target, mode, EXPAND_NORMAL);
3534 /* If c_strlen can determine an expression for one of the string
3535 lengths, and it doesn't have side effects, then emit cmpstrsi
3536 using length MIN(strlen(string)+1, arg3). */
3537 #ifdef HAVE_cmpstrsi
3540 tree len, len1, len2;
3541 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3546 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3548 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3549 enum machine_mode insn_mode
3550 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3552 len1 = c_strlen (arg1, 1);
3553 len2 = c_strlen (arg2, 1);
3556 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3558 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3560 /* If we don't have a constant length for the first, use the length
3561 of the second, if we know it. We don't require a constant for
3562 this case; some cost analysis could be done if both are available
3563 but neither is constant. For now, assume they're equally cheap,
3564 unless one has side effects. If both strings have constant lengths,
3571 else if (TREE_SIDE_EFFECTS (len1))
3573 else if (TREE_SIDE_EFFECTS (len2))
3575 else if (TREE_CODE (len1) != INTEGER_CST)
3577 else if (TREE_CODE (len2) != INTEGER_CST)
3579 else if (tree_int_cst_lt (len1, len2))
3584 /* If both arguments have side effects, we cannot optimize. */
3585 if (!len || TREE_SIDE_EFFECTS (len))
3588 /* The actual new length parameter is MIN(len,arg3). */
3589 len = fold (build (MIN_EXPR, TREE_TYPE (len), len, arg3));
3591 /* If we don't have POINTER_TYPE, call the function. */
3592 if (arg1_align == 0 || arg2_align == 0)
3595 /* Make a place to write the result of the instruction. */
3598 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
3599 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3600 result = gen_reg_rtx (insn_mode);
3602 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3603 arg1 = save_expr (arg1);
3604 arg2 = save_expr (arg2);
3605 len = save_expr (len);
3607 arg1_rtx = get_memory_rtx (arg1);
3608 arg2_rtx = get_memory_rtx (arg2);
3609 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3610 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3611 GEN_INT (MIN (arg1_align, arg2_align)));
3616 /* Return the value in the proper mode for this function. */
3617 mode = TYPE_MODE (TREE_TYPE (exp));
3618 if (GET_MODE (result) == mode)
3621 return convert_to_mode (mode, result, 0);
3622 convert_move (target, result, 0);
3626 /* Expand the library call ourselves using a stabilized argument
3627 list to avoid re-evaluating the function's arguments twice. */
3628 arglist = build_tree_list (NULL_TREE, len);
3629 arglist = tree_cons (NULL_TREE, arg2, arglist);
3630 arglist = tree_cons (NULL_TREE, arg1, arglist);
3631 fndecl = get_callee_fndecl (exp);
3632 exp = build_function_call_expr (fndecl, arglist);
3633 return expand_call (exp, target, target == const0_rtx);
3639 /* Expand expression EXP, which is a call to the strcat builtin.
3640 Return 0 if we failed the caller should emit a normal call,
3641 otherwise try to get the result in TARGET, if convenient. */
3644 expand_builtin_strcat (tree arglist, rtx target, enum machine_mode mode)
3646 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3650 tree dst = TREE_VALUE (arglist),
3651 src = TREE_VALUE (TREE_CHAIN (arglist));
3652 const char *p = c_getstr (src);
3656 /* If the string length is zero, return the dst parameter. */
3658 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3659 else if (!optimize_size)
3661 /* Otherwise if !optimize_size, see if we can store by
3662 pieces into (dst + strlen(dst)). */
3663 tree newdst, arglist,
3664 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3666 /* This is the length argument. */
3667 arglist = build_tree_list (NULL_TREE,
3668 fold (size_binop (PLUS_EXPR,
3671 /* Prepend src argument. */
3672 arglist = tree_cons (NULL_TREE, src, arglist);
3674 /* We're going to use dst more than once. */
3675 dst = save_expr (dst);
3677 /* Create strlen (dst). */
3679 fold (build_function_call_expr (strlen_fn,
3680 build_tree_list (NULL_TREE,
3682 /* Create (dst + strlen (dst)). */
3683 newdst = fold (build (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
3685 /* Prepend the new dst argument. */
3686 arglist = tree_cons (NULL_TREE, newdst, arglist);
3688 /* We don't want to get turned into a memcpy if the
3689 target is const0_rtx, i.e. when the return value
3690 isn't used. That would produce pessimized code so
3691 pass in a target of zero, it should never actually be
3692 used. If this was successful return the original
3693 dst, not the result of mempcpy. */
3694 if (expand_builtin_mempcpy (arglist, /*target=*/0, mode, /*endp=*/0))
3695 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3705 /* Expand expression EXP, which is a call to the strncat builtin.
3706 Return 0 if we failed the caller should emit a normal call,
3707 otherwise try to get the result in TARGET, if convenient. */
3710 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3712 if (!validate_arglist (arglist,
3713 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3717 tree dst = TREE_VALUE (arglist),
3718 src = TREE_VALUE (TREE_CHAIN (arglist)),
3719 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3720 const char *p = c_getstr (src);
3722 /* If the requested length is zero, or the src parameter string
3723 length is zero, return the dst parameter. */
3724 if (integer_zerop (len) || (p && *p == '\0'))
3726 /* Evaluate and ignore the src and len parameters in case
3727 they have side-effects. */
3728 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3729 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
3730 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3733 /* If the requested len is greater than or equal to the string
3734 length, call strcat. */
3735 if (TREE_CODE (len) == INTEGER_CST && p
3736 && compare_tree_int (len, strlen (p)) >= 0)
3739 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
3740 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
3742 /* If the replacement _DECL isn't initialized, don't do the
3747 return expand_expr (build_function_call_expr (fn, newarglist),
3748 target, mode, EXPAND_NORMAL);
3754 /* Expand expression EXP, which is a call to the strspn builtin.
3755 Return 0 if we failed the caller should emit a normal call,
3756 otherwise try to get the result in TARGET, if convenient. */
3759 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3761 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3765 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
3766 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
3768 /* If both arguments are constants, evaluate at compile-time. */
3771 const size_t r = strspn (p1, p2);
3772 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
3775 /* If either argument is "", return 0. */
3776 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
3778 /* Evaluate and ignore both arguments in case either one has
3780 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3781 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3788 /* Expand expression EXP, which is a call to the strcspn builtin.
3789 Return 0 if we failed the caller should emit a normal call,
3790 otherwise try to get the result in TARGET, if convenient. */
3793 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3795 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3799 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
3800 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
3802 /* If both arguments are constants, evaluate at compile-time. */
3805 const size_t r = strcspn (p1, p2);
3806 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
3809 /* If the first argument is "", return 0. */
3810 if (p1 && *p1 == '\0')
3812 /* Evaluate and ignore argument s2 in case it has
3814 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3818 /* If the second argument is "", return __builtin_strlen(s1). */
3819 if (p2 && *p2 == '\0')
3821 tree newarglist = build_tree_list (NULL_TREE, s1),
3822 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3824 /* If the replacement _DECL isn't initialized, don't do the
3829 return expand_expr (build_function_call_expr (fn, newarglist),
3830 target, mode, EXPAND_NORMAL);
3836 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3837 if that's convenient. */
3840 expand_builtin_saveregs (void)
3844 /* Don't do __builtin_saveregs more than once in a function.
3845 Save the result of the first call and reuse it. */
3846 if (saveregs_value != 0)
3847 return saveregs_value;
3849 /* When this function is called, it means that registers must be
3850 saved on entry to this function. So we migrate the call to the
3851 first insn of this function. */
3855 /* Do whatever the machine needs done in this case. */
3856 val = targetm.calls.expand_builtin_saveregs ();
3861 saveregs_value = val;
3863 /* Put the insns after the NOTE that starts the function. If this
3864 is inside a start_sequence, make the outer-level insn chain current, so
3865 the code is placed at the start of the function. */
3866 push_topmost_sequence ();
3867 emit_insn_after (seq, get_insns ());
3868 pop_topmost_sequence ();
3873 /* __builtin_args_info (N) returns word N of the arg space info
3874 for the current function. The number and meanings of words
3875 is controlled by the definition of CUMULATIVE_ARGS. */
3878 expand_builtin_args_info (tree arglist)
3880 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
3881 int *word_ptr = (int *) ¤t_function_args_info;
3883 if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0)
3888 if (!host_integerp (TREE_VALUE (arglist), 0))
3889 error ("argument of `__builtin_args_info' must be constant");
3892 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
3894 if (wordnum < 0 || wordnum >= nwords)
3895 error ("argument of `__builtin_args_info' out of range");
3897 return GEN_INT (word_ptr[wordnum]);
3901 error ("missing argument in `__builtin_args_info'");
3906 /* Expand ARGLIST, from a call to __builtin_next_arg. */
3909 expand_builtin_next_arg (tree arglist)
3911 tree fntype = TREE_TYPE (current_function_decl);
3913 if (TYPE_ARG_TYPES (fntype) == 0
3914 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3917 error ("`va_start' used in function with fixed args");
3923 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
3924 tree arg = TREE_VALUE (arglist);
3926 /* Strip off all nops for the sake of the comparison. This
3927 is not quite the same as STRIP_NOPS. It does more.
3928 We must also strip off INDIRECT_EXPR for C++ reference
3930 while (TREE_CODE (arg) == NOP_EXPR
3931 || TREE_CODE (arg) == CONVERT_EXPR
3932 || TREE_CODE (arg) == NON_LVALUE_EXPR
3933 || TREE_CODE (arg) == INDIRECT_REF)
3934 arg = TREE_OPERAND (arg, 0);
3935 if (arg != last_parm)
3936 warning ("second parameter of `va_start' not last named argument");
3939 /* Evidently an out of date version of <stdarg.h>; can't validate
3940 va_start's second argument, but can still work as intended. */
3941 warning ("`__builtin_next_arg' called without an argument");
3943 return expand_binop (Pmode, add_optab,
3944 current_function_internal_arg_pointer,
3945 current_function_arg_offset_rtx,
3946 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3949 /* Make it easier for the backends by protecting the valist argument
3950 from multiple evaluations. */
3953 stabilize_va_list (tree valist, int needs_lvalue)
3955 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3957 if (TREE_SIDE_EFFECTS (valist))
3958 valist = save_expr (valist);
3960 /* For this case, the backends will be expecting a pointer to
3961 TREE_TYPE (va_list_type_node), but it's possible we've
3962 actually been given an array (an actual va_list_type_node).
3964 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
3966 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3967 tree p2 = build_pointer_type (va_list_type_node);
3969 valist = build1 (ADDR_EXPR, p2, valist);
3970 valist = fold (build1 (NOP_EXPR, p1, valist));
3979 if (! TREE_SIDE_EFFECTS (valist))
3982 pt = build_pointer_type (va_list_type_node);
3983 valist = fold (build1 (ADDR_EXPR, pt, valist));
3984 TREE_SIDE_EFFECTS (valist) = 1;
3987 if (TREE_SIDE_EFFECTS (valist))
3988 valist = save_expr (valist);
3989 valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)),
3996 /* The "standard" definition of va_list is void*. */
3999 std_build_builtin_va_list (void)
4001 return ptr_type_node;
4004 /* The "standard" implementation of va_start: just assign `nextarg' to
4008 std_expand_builtin_va_start (tree valist, rtx nextarg)
4012 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
4013 make_tree (ptr_type_node, nextarg));
4014 TREE_SIDE_EFFECTS (t) = 1;
4016 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4019 /* Expand ARGLIST, from a call to __builtin_va_start. */
4022 expand_builtin_va_start (tree arglist)
4027 chain = TREE_CHAIN (arglist);
4029 if (TREE_CHAIN (chain))
4030 error ("too many arguments to function `va_start'");
4032 nextarg = expand_builtin_next_arg (chain);
4033 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4035 #ifdef EXPAND_BUILTIN_VA_START
4036 EXPAND_BUILTIN_VA_START (valist, nextarg);
4038 std_expand_builtin_va_start (valist, nextarg);
4044 /* The "standard" implementation of va_arg: read the value from the
4045 current (padded) address and increment by the (padded) size. */
4048 std_expand_builtin_va_arg (tree valist, tree type)
4050 tree addr_tree, t, type_size = NULL;
4051 tree align, alignm1;
4054 HOST_WIDE_INT boundary;
4056 /* Compute the rounded size of the type. */
4057 align = size_int (PARM_BOUNDARY / BITS_PER_UNIT);
4058 alignm1 = size_int (PARM_BOUNDARY / BITS_PER_UNIT - 1);
4059 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
4061 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4062 requires greater alignment, we must perform dynamic alignment. */
4064 if (boundary > PARM_BOUNDARY)
4066 if (!PAD_VARARGS_DOWN)
4068 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
4069 build (PLUS_EXPR, TREE_TYPE (valist), valist,
4070 build_int_2 (boundary / BITS_PER_UNIT - 1, 0)));
4071 TREE_SIDE_EFFECTS (t) = 1;
4072 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4074 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
4075 build (BIT_AND_EXPR, TREE_TYPE (valist), valist,
4076 build_int_2 (~(boundary / BITS_PER_UNIT - 1), -1)));
4077 TREE_SIDE_EFFECTS (t) = 1;
4078 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4080 if (type == error_mark_node
4081 || (type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type))) == NULL
4082 || TREE_OVERFLOW (type_size))
4083 rounded_size = size_zero_node;
4085 rounded_size = fold (build (MULT_EXPR, sizetype,
4086 fold (build (TRUNC_DIV_EXPR, sizetype,
4087 fold (build (PLUS_EXPR, sizetype,
4088 type_size, alignm1)),
4094 if (PAD_VARARGS_DOWN && ! integer_zerop (rounded_size))
4096 /* Small args are padded downward. */
4097 addr_tree = fold (build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
4098 fold (build (COND_EXPR, sizetype,
4099 fold (build (GT_EXPR, sizetype,
4103 fold (build (MINUS_EXPR, sizetype,
4108 addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
4109 addr = copy_to_reg (addr);
4111 /* Compute new value for AP. */
4112 if (! integer_zerop (rounded_size))
4114 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
4115 build (PLUS_EXPR, TREE_TYPE (valist), valist,
4117 TREE_SIDE_EFFECTS (t) = 1;
4118 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4124 /* Expand __builtin_va_arg, which is not really a builtin function, but
4125 a very special sort of operator. */
4128 expand_builtin_va_arg (tree valist, tree type)
4131 tree promoted_type, want_va_type, have_va_type;
4133 /* Verify that valist is of the proper type. */
4135 want_va_type = va_list_type_node;
4136 have_va_type = TREE_TYPE (valist);
4137 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4139 /* If va_list is an array type, the argument may have decayed
4140 to a pointer type, e.g. by being passed to another function.
4141 In that case, unwrap both types so that we can compare the
4142 underlying records. */
4143 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4144 || TREE_CODE (have_va_type) == POINTER_TYPE)
4146 want_va_type = TREE_TYPE (want_va_type);
4147 have_va_type = TREE_TYPE (have_va_type);
4150 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4152 error ("first argument to `va_arg' not of type `va_list'");
4156 /* Generate a diagnostic for requesting data of a type that cannot
4157 be passed through `...' due to type promotion at the call site. */
4158 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4161 const char *name = "<anonymous type>", *pname = 0;
4162 static bool gave_help;
4164 if (TYPE_NAME (type))
4166 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4167 name = IDENTIFIER_POINTER (TYPE_NAME (type));
4168 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4169 && DECL_NAME (TYPE_NAME (type)))
4170 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
4172 if (TYPE_NAME (promoted_type))
4174 if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE)
4175 pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type));
4176 else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL
4177 && DECL_NAME (TYPE_NAME (promoted_type)))
4178 pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type)));
4181 /* Unfortunately, this is merely undefined, rather than a constraint
4182 violation, so we cannot make this an error. If this call is never
4183 executed, the program is still strictly conforming. */
4184 warning ("`%s' is promoted to `%s' when passed through `...'",
4189 warning ("(so you should pass `%s' not `%s' to `va_arg')",
4193 /* We can, however, treat "undefined" any way we please.
4194 Call abort to encourage the user to fix the program. */
4195 inform ("if this code is reached, the program will abort");
4196 expand_builtin_trap ();
4198 /* This is dead code, but go ahead and finish so that the
4199 mode of the result comes out right. */
4204 /* Make it easier for the backends by protecting the valist argument
4205 from multiple evaluations. */
4206 valist = stabilize_va_list (valist, 0);
4208 #ifdef EXPAND_BUILTIN_VA_ARG
4209 addr = EXPAND_BUILTIN_VA_ARG (valist, type);
4211 addr = std_expand_builtin_va_arg (valist, type);
4215 addr = convert_memory_address (Pmode, addr);
4217 result = gen_rtx_MEM (TYPE_MODE (type), addr);
4218 set_mem_alias_set (result, get_varargs_alias_set ());
4223 /* Expand ARGLIST, from a call to __builtin_va_end. */
4226 expand_builtin_va_end (tree arglist)
4228 tree valist = TREE_VALUE (arglist);
4230 /* Evaluate for side effects, if needed. I hate macros that don't
4232 if (TREE_SIDE_EFFECTS (valist))
4233 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4238 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4239 builtin rather than just as an assignment in stdarg.h because of the
4240 nastiness of array-type va_list types. */
4243 expand_builtin_va_copy (tree arglist)
4247 dst = TREE_VALUE (arglist);
4248 src = TREE_VALUE (TREE_CHAIN (arglist));
4250 dst = stabilize_va_list (dst, 1);
4251 src = stabilize_va_list (src, 0);
4253 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4255 t = build (MODIFY_EXPR, va_list_type_node, dst, src);
4256 TREE_SIDE_EFFECTS (t) = 1;
4257 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4261 rtx dstb, srcb, size;
4263 /* Evaluate to pointers. */
4264 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4265 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4266 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4267 VOIDmode, EXPAND_NORMAL);
4269 dstb = convert_memory_address (Pmode, dstb);
4270 srcb = convert_memory_address (Pmode, srcb);
4272 /* "Dereference" to BLKmode memories. */
4273 dstb = gen_rtx_MEM (BLKmode, dstb);
4274 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4275 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4276 srcb = gen_rtx_MEM (BLKmode, srcb);
4277 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4278 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4281 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4287 /* Expand a call to one of the builtin functions __builtin_frame_address or
4288 __builtin_return_address. */
4291 expand_builtin_frame_address (tree fndecl, tree arglist)
4293 /* The argument must be a nonnegative integer constant.
4294 It counts the number of frames to scan up the stack.
4295 The value is the return address saved in that frame. */
4297 /* Warning about missing arg was already issued. */
4299 else if (! host_integerp (TREE_VALUE (arglist), 1))
4301 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4302 error ("invalid arg to `__builtin_frame_address'");
4304 error ("invalid arg to `__builtin_return_address'");
4310 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4311 tree_low_cst (TREE_VALUE (arglist), 1),
4312 hard_frame_pointer_rtx);
4314 /* Some ports cannot access arbitrary stack frames. */
4317 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4318 warning ("unsupported arg to `__builtin_frame_address'");
4320 warning ("unsupported arg to `__builtin_return_address'");
4324 /* For __builtin_frame_address, return what we've got. */
4325 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4328 if (GET_CODE (tem) != REG
4329 && ! CONSTANT_P (tem))
4330 tem = copy_to_mode_reg (Pmode, tem);
4335 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4336 we failed and the caller should emit a normal call, otherwise try to get
4337 the result in TARGET, if convenient. */
4340 expand_builtin_alloca (tree arglist, rtx target)
4345 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4348 /* Compute the argument. */
4349 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4351 /* Allocate the desired space. */
4352 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4353 result = convert_memory_address (ptr_mode, result);
4358 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4359 Return 0 if a normal call should be emitted rather than expanding the
4360 function in-line. If convenient, the result should be placed in TARGET.
4361 SUBTARGET may be used as the target for computing one of EXP's operands. */
4364 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4365 rtx subtarget, optab op_optab)
4368 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4371 /* Compute the argument. */
4372 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4373 /* Compute op, into TARGET if possible.
4374 Set TARGET to wherever the result comes back. */
4375 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4376 op_optab, op0, target, 1);
4380 return convert_to_mode (target_mode, target, 0);
4383 /* If the string passed to fputs is a constant and is one character
4384 long, we attempt to transform this call into __builtin_fputc(). */
4387 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4390 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4391 : implicit_built_in_decls[BUILT_IN_FPUTC];
4392 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
4393 : implicit_built_in_decls[BUILT_IN_FWRITE];
4395 /* If the return value is used, or the replacement _DECL isn't
4396 initialized, don't do the transformation. */
4397 if (target != const0_rtx || !fn_fputc || !fn_fwrite)
4400 /* Verify the arguments in the original call. */
4401 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4404 /* Get the length of the string passed to fputs. If the length
4405 can't be determined, punt. */
4406 if (!(len = c_strlen (TREE_VALUE (arglist), 1))
4407 || TREE_CODE (len) != INTEGER_CST)
4410 switch (compare_tree_int (len, 1))
4412 case -1: /* length is 0, delete the call entirely . */
4414 /* Evaluate and ignore the argument in case it has
4416 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
4417 VOIDmode, EXPAND_NORMAL);
4420 case 0: /* length is 1, call fputc. */
4422 const char *p = c_getstr (TREE_VALUE (arglist));
4426 /* New argument list transforming fputs(string, stream) to
4427 fputc(string[0], stream). */
4429 build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
4431 tree_cons (NULL_TREE, build_int_2 (p[0], 0), arglist);
4437 case 1: /* length is greater than 1, call fwrite. */
4441 /* If optimizing for size keep fputs. */
4444 string_arg = TREE_VALUE (arglist);
4445 /* New argument list transforming fputs(string, stream) to
4446 fwrite(string, 1, len, stream). */
4447 arglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
4448 arglist = tree_cons (NULL_TREE, len, arglist);
4449 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
4450 arglist = tree_cons (NULL_TREE, string_arg, arglist);
4458 return expand_expr (build_function_call_expr (fn, arglist),
4459 const0_rtx, VOIDmode, EXPAND_NORMAL);
4462 /* Expand a call to __builtin_expect. We return our argument and emit a
4463 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4464 a non-jump context. */
4467 expand_builtin_expect (tree arglist, rtx target)
4472 if (arglist == NULL_TREE
4473 || TREE_CHAIN (arglist) == NULL_TREE)
4475 exp = TREE_VALUE (arglist);
4476 c = TREE_VALUE (TREE_CHAIN (arglist));
4478 if (TREE_CODE (c) != INTEGER_CST)
4480 error ("second arg to `__builtin_expect' must be a constant");
4481 c = integer_zero_node;
4484 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4486 /* Don't bother with expected value notes for integral constants. */
4487 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4489 /* We do need to force this into a register so that we can be
4490 moderately sure to be able to correctly interpret the branch
4492 target = force_reg (GET_MODE (target), target);
4494 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4496 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4497 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4503 /* Like expand_builtin_expect, except do this in a jump context. This is
4504 called from do_jump if the conditional is a __builtin_expect. Return either
4505 a list of insns to emit the jump or NULL if we cannot optimize
4506 __builtin_expect. We need to optimize this at jump time so that machines
4507 like the PowerPC don't turn the test into a SCC operation, and then jump
4508 based on the test being 0/1. */
4511 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4513 tree arglist = TREE_OPERAND (exp, 1);
4514 tree arg0 = TREE_VALUE (arglist);
4515 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4518 /* Only handle __builtin_expect (test, 0) and
4519 __builtin_expect (test, 1). */
4520 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4521 && (integer_zerop (arg1) || integer_onep (arg1)))
4523 rtx insn, drop_through_label, temp;
4525 /* Expand the jump insns. */
4527 do_jump (arg0, if_false_label, if_true_label);
4530 drop_through_label = get_last_insn ();
4531 if (drop_through_label && GET_CODE (drop_through_label) == NOTE)
4532 drop_through_label = prev_nonnote_insn (drop_through_label);
4533 if (drop_through_label && GET_CODE (drop_through_label) != CODE_LABEL)
4534 drop_through_label = NULL_RTX;
4537 if (! if_true_label)
4538 if_true_label = drop_through_label;
4539 if (! if_false_label)
4540 if_false_label = drop_through_label;
4542 /* Go through and add the expect's to each of the conditional jumps. */
4544 while (insn != NULL_RTX)
4546 rtx next = NEXT_INSN (insn);
4548 if (GET_CODE (insn) == JUMP_INSN && any_condjump_p (insn))
4550 rtx ifelse = SET_SRC (pc_set (insn));
4551 rtx then_dest = XEXP (ifelse, 1);
4552 rtx else_dest = XEXP (ifelse, 2);
4555 /* First check if we recognize any of the labels. */
4556 if (GET_CODE (then_dest) == LABEL_REF
4557 && XEXP (then_dest, 0) == if_true_label)
4559 else if (GET_CODE (then_dest) == LABEL_REF
4560 && XEXP (then_dest, 0) == if_false_label)
4562 else if (GET_CODE (else_dest) == LABEL_REF
4563 && XEXP (else_dest, 0) == if_false_label)
4565 else if (GET_CODE (else_dest) == LABEL_REF
4566 && XEXP (else_dest, 0) == if_true_label)
4568 /* Otherwise check where we drop through. */
4569 else if (else_dest == pc_rtx)
4571 if (next && GET_CODE (next) == NOTE)
4572 next = next_nonnote_insn (next);
4574 if (next && GET_CODE (next) == JUMP_INSN
4575 && any_uncondjump_p (next))
4576 temp = XEXP (SET_SRC (pc_set (next)), 0);
4580 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4581 else that can't possibly match either target label. */
4582 if (temp == if_false_label)
4584 else if (temp == if_true_label)
4587 else if (then_dest == pc_rtx)
4589 if (next && GET_CODE (next) == NOTE)
4590 next = next_nonnote_insn (next);
4592 if (next && GET_CODE (next) == JUMP_INSN
4593 && any_uncondjump_p (next))
4594 temp = XEXP (SET_SRC (pc_set (next)), 0);
4598 if (temp == if_false_label)
4600 else if (temp == if_true_label)
4606 /* If the test is expected to fail, reverse the
4608 if (integer_zerop (arg1))
4610 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4622 expand_builtin_trap (void)
4626 emit_insn (gen_trap ());
4629 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4633 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4634 Return 0 if a normal call should be emitted rather than expanding
4635 the function inline. If convenient, the result should be placed
4636 in TARGET. SUBTARGET may be used as the target for computing
4640 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4642 enum machine_mode mode;
4646 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4649 arg = TREE_VALUE (arglist);
4650 mode = TYPE_MODE (TREE_TYPE (arg));
4651 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4652 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4655 /* Expand a call to cabs, cabsf or cabsl with arguments ARGLIST.
4656 Return 0 if a normal call should be emitted rather than expanding
4657 the function inline. If convenient, the result should be placed
4661 expand_builtin_cabs (tree arglist, rtx target)
4663 enum machine_mode mode;
4667 if (arglist == 0 || TREE_CHAIN (arglist))
4669 arg = TREE_VALUE (arglist);
4670 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
4671 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
4674 mode = TYPE_MODE (TREE_TYPE (arg));
4675 op0 = expand_expr (arg, NULL_RTX, VOIDmode, 0);
4676 return expand_complex_abs (mode, op0, target, 0);
4679 /* Create a new constant string literal and return a char* pointer to it.
4680 The STRING_CST value is the LEN characters at STR. */
4682 build_string_literal (int len, const char *str)
4684 tree t, elem, index, type;
4686 t = build_string (len, str);
4687 elem = build_type_variant (char_type_node, 1, 0);
4688 index = build_index_type (build_int_2 (len - 1, 0));
4689 type = build_array_type (elem, index);
4690 TREE_TYPE (t) = type;
4691 TREE_CONSTANT (t) = 1;
4692 TREE_READONLY (t) = 1;
4693 TREE_STATIC (t) = 1;
4695 type = build_pointer_type (type);
4696 t = build1 (ADDR_EXPR, type, t);
4698 type = build_pointer_type (elem);
4699 t = build1 (NOP_EXPR, type, t);
4703 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4704 Return 0 if a normal call should be emitted rather than transforming
4705 the function inline. If convenient, the result should be placed in
4706 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4709 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4712 tree fn_putchar = unlocked
4713 ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4714 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4715 tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4716 : implicit_built_in_decls[BUILT_IN_PUTS];
4717 const char *fmt_str;
4720 /* If the return value is used, don't do the transformation. */
4721 if (target != const0_rtx)
4724 /* Verify the required arguments in the original call. */
4727 fmt = TREE_VALUE (arglist);
4728 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4730 arglist = TREE_CHAIN (arglist);
4732 /* Check whether the format is a literal string constant. */
4733 fmt_str = c_getstr (fmt);
4734 if (fmt_str == NULL)
4737 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4738 if (strcmp (fmt_str, "%s\n") == 0)
4741 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
4742 || TREE_CHAIN (arglist))
4746 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4747 else if (strcmp (fmt_str, "%c") == 0)
4750 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4751 || TREE_CHAIN (arglist))
4757 /* We can't handle anything else with % args or %% ... yet. */
4758 if (strchr (fmt_str, '%'))
4764 /* If the format specifier was "", printf does nothing. */
4765 if (fmt_str[0] == '\0')
4767 /* If the format specifier has length of 1, call putchar. */
4768 if (fmt_str[1] == '\0')
4770 /* Given printf("c"), (where c is any one character,)
4771 convert "c"[0] to an int and pass that to the replacement
4773 arg = build_int_2 (fmt_str[0], 0);
4774 arglist = build_tree_list (NULL_TREE, arg);
4779 /* If the format specifier was "string\n", call puts("string"). */
4780 size_t len = strlen (fmt_str);
4781 if (fmt_str[len - 1] == '\n')
4783 /* Create a NUL-terminated string that's one char shorter
4784 than the original, stripping off the trailing '\n'. */
4785 char *newstr = alloca (len);
4786 memcpy (newstr, fmt_str, len - 1);
4787 newstr[len - 1] = 0;
4789 arg = build_string_literal (len, newstr);
4790 arglist = build_tree_list (NULL_TREE, arg);
4794 /* We'd like to arrange to call fputs(string,stdout) here,
4795 but we need stdout and don't have a way to get it yet. */
4802 return expand_expr (build_function_call_expr (fn, arglist),
4803 target, mode, EXPAND_NORMAL);
4806 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4807 Return 0 if a normal call should be emitted rather than transforming
4808 the function inline. If convenient, the result should be placed in
4809 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4812 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
4815 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4816 : implicit_built_in_decls[BUILT_IN_FPUTC];
4817 tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4818 : implicit_built_in_decls[BUILT_IN_FPUTS];
4819 const char *fmt_str;
4820 tree fn, fmt, fp, arg;
4822 /* If the return value is used, don't do the transformation. */
4823 if (target != const0_rtx)
4826 /* Verify the required arguments in the original call. */
4829 fp = TREE_VALUE (arglist);
4830 if (TREE_CODE (TREE_TYPE (fp)) != POINTER_TYPE)
4832 arglist = TREE_CHAIN (arglist);
4835 fmt = TREE_VALUE (arglist);
4836 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4838 arglist = TREE_CHAIN (arglist);
4840 /* Check whether the format is a literal string constant. */
4841 fmt_str = c_getstr (fmt);
4842 if (fmt_str == NULL)
4845 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4846 if (strcmp (fmt_str, "%s") == 0)
4849 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
4850 || TREE_CHAIN (arglist))
4852 arg = TREE_VALUE (arglist);
4853 arglist = build_tree_list (NULL_TREE, fp);
4854 arglist = tree_cons (NULL_TREE, arg, arglist);
4857 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
4858 else if (strcmp (fmt_str, "%c") == 0)
4861 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4862 || TREE_CHAIN (arglist))
4864 arg = TREE_VALUE (arglist);
4865 arglist = build_tree_list (NULL_TREE, fp);
4866 arglist = tree_cons (NULL_TREE, arg, arglist);
4871 /* We can't handle anything else with % args or %% ... yet. */
4872 if (strchr (fmt_str, '%'))
4878 /* If the format specifier was "", fprintf does nothing. */
4879 if (fmt_str[0] == '\0')
4881 /* Evaluate and ignore FILE* argument for side-effects. */
4882 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4886 /* When "string" doesn't contain %, replace all cases of
4887 fprintf(stream,string) with fputs(string,stream). The fputs
4888 builtin will take care of special cases like length == 1. */
4889 arglist = build_tree_list (NULL_TREE, fp);
4890 arglist = tree_cons (NULL_TREE, fmt, arglist);
4896 return expand_expr (build_function_call_expr (fn, arglist),
4897 target, mode, EXPAND_NORMAL);
4900 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
4901 a normal call should be emitted rather than expanding the function
4902 inline. If convenient, the result should be placed in TARGET with
4906 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
4908 tree orig_arglist, dest, fmt;
4909 const char *fmt_str;
4911 orig_arglist = arglist;
4913 /* Verify the required arguments in the original call. */
4916 dest = TREE_VALUE (arglist);
4917 if (TREE_CODE (TREE_TYPE (dest)) != POINTER_TYPE)
4919 arglist = TREE_CHAIN (arglist);
4922 fmt = TREE_VALUE (arglist);
4923 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4925 arglist = TREE_CHAIN (arglist);
4927 /* Check whether the format is a literal string constant. */
4928 fmt_str = c_getstr (fmt);
4929 if (fmt_str == NULL)
4932 /* If the format doesn't contain % args or %%, use strcpy. */
4933 if (strchr (fmt_str, '%') == 0)
4935 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4938 if (arglist || ! fn)
4940 expand_expr (build_function_call_expr (fn, orig_arglist),
4941 const0_rtx, VOIDmode, EXPAND_NORMAL);
4942 if (target == const0_rtx)
4944 exp = build_int_2 (strlen (fmt_str), 0);
4945 exp = fold (build1 (NOP_EXPR, integer_type_node, exp));
4946 return expand_expr (exp, target, mode, EXPAND_NORMAL);
4948 /* If the format is "%s", use strcpy if the result isn't used. */
4949 else if (strcmp (fmt_str, "%s") == 0)
4952 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4957 if (! arglist || TREE_CHAIN (arglist))
4959 arg = TREE_VALUE (arglist);
4960 if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
4963 if (target != const0_rtx)
4965 len = c_strlen (arg, 1);
4966 if (! len || TREE_CODE (len) != INTEGER_CST)
4972 arglist = build_tree_list (NULL_TREE, arg);
4973 arglist = tree_cons (NULL_TREE, dest, arglist);
4974 expand_expr (build_function_call_expr (fn, arglist),
4975 const0_rtx, VOIDmode, EXPAND_NORMAL);
4977 if (target == const0_rtx)
4979 return expand_expr (len, target, mode, EXPAND_NORMAL);
4985 /* Expand a call to the built-in signbit, signbitf or signbitl function.
4986 Return NULL_RTX if a normal call should be emitted rather than expanding
4987 the function in-line. EXP is the expression that is a call to the builtin
4988 function; if convenient, the result should be placed in TARGET. */
4991 expand_builtin_signbit (tree exp, rtx target)
4993 const struct real_format *fmt;
4994 enum machine_mode fmode, imode, rmode;
4995 HOST_WIDE_INT hi, lo;
5000 arglist = TREE_OPERAND (exp, 1);
5001 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5004 arg = TREE_VALUE (arglist);
5005 fmode = TYPE_MODE (TREE_TYPE (arg));
5006 rmode = TYPE_MODE (TREE_TYPE (exp));
5007 fmt = REAL_MODE_FORMAT (fmode);
5009 /* For floating point formats without a sign bit, implement signbit
5011 if (fmt->signbit < 0)
5013 /* But we can't do this if the format supports signed zero. */
5014 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5017 arg = fold (build (LT_EXPR, TREE_TYPE (exp), arg,
5018 build_real (TREE_TYPE (arg), dconst0)));
5019 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5022 imode = int_mode_for_mode (fmode);
5023 if (imode == BLKmode)
5026 bitpos = fmt->signbit;
5027 /* Handle targets with different FP word orders. */
5028 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
5030 int nwords = GET_MODE_BITSIZE (fmode) / BITS_PER_WORD;
5031 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
5032 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
5035 /* If the sign bit is not in the lowpart and the floating point format
5036 is wider than an integer, check that is twice the size of an integer
5037 so that we can use gen_highpart below. */
5038 if (bitpos >= GET_MODE_BITSIZE (rmode)
5039 && GET_MODE_BITSIZE (imode) != 2 * GET_MODE_BITSIZE (rmode))
5042 temp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
5043 temp = gen_lowpart (imode, temp);
5045 if (GET_MODE_BITSIZE (imode) > GET_MODE_BITSIZE (rmode))
5047 if (BYTES_BIG_ENDIAN)
5048 bitpos = GET_MODE_BITSIZE (imode) - 1 - bitpos;
5049 temp = copy_to_mode_reg (imode, temp);
5050 temp = extract_bit_field (temp, 1, bitpos, 1,
5051 NULL_RTX, rmode, rmode,
5052 GET_MODE_SIZE (imode));
5056 if (GET_MODE_BITSIZE (imode) < GET_MODE_BITSIZE (rmode))
5057 temp = gen_lowpart (rmode, temp);
5058 if (bitpos < HOST_BITS_PER_WIDE_INT)
5061 lo = (HOST_WIDE_INT) 1 << bitpos;
5065 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5069 temp = force_reg (rmode, temp);
5070 temp = expand_binop (rmode, and_optab, temp,
5071 immed_double_const (lo, hi, rmode),
5072 target, 1, OPTAB_LIB_WIDEN);
5077 /* Expand an expression EXP that calls a built-in function,
5078 with result going to TARGET if that's convenient
5079 (and in mode MODE if that's convenient).
5080 SUBTARGET may be used as the target for computing one of EXP's operands.
5081 IGNORE is nonzero if the value is to be ignored. */
5084 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5087 tree fndecl = get_callee_fndecl (exp);
5088 tree arglist = TREE_OPERAND (exp, 1);
5089 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5090 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5092 /* Perform postincrements before expanding builtin functions. */
5095 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5096 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5098 /* When not optimizing, generate calls to library functions for a certain
5101 && !CALLED_AS_BUILT_IN (fndecl)
5102 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5103 && fcode != BUILT_IN_ALLOCA)
5104 return expand_call (exp, target, ignore);
5106 /* The built-in function expanders test for target == const0_rtx
5107 to determine whether the function's result will be ignored. */
5109 target = const0_rtx;
5111 /* If the result of a pure or const built-in function is ignored, and
5112 none of its arguments are volatile, we can avoid expanding the
5113 built-in call and just evaluate the arguments for side-effects. */
5114 if (target == const0_rtx
5115 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5117 bool volatilep = false;
5120 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5121 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5129 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5130 expand_expr (TREE_VALUE (arg), const0_rtx,
5131 VOIDmode, EXPAND_NORMAL);
5140 case BUILT_IN_LLABS:
5141 case BUILT_IN_IMAXABS:
5142 /* build_function_call changes these into ABS_EXPR. */
5146 case BUILT_IN_FABSF:
5147 case BUILT_IN_FABSL:
5148 target = expand_builtin_fabs (arglist, target, subtarget);
5154 case BUILT_IN_CABSF:
5155 case BUILT_IN_CABSL:
5156 if (flag_unsafe_math_optimizations)
5158 target = expand_builtin_cabs (arglist, target);
5165 case BUILT_IN_CONJF:
5166 case BUILT_IN_CONJL:
5167 case BUILT_IN_CREAL:
5168 case BUILT_IN_CREALF:
5169 case BUILT_IN_CREALL:
5170 case BUILT_IN_CIMAG:
5171 case BUILT_IN_CIMAGF:
5172 case BUILT_IN_CIMAGL:
5173 /* expand_tree_builtin changes these into CONJ_EXPR, REALPART_EXPR
5174 and IMAGPART_EXPR. */
5180 case BUILT_IN_EXP10:
5181 case BUILT_IN_EXP10F:
5182 case BUILT_IN_EXP10L:
5183 case BUILT_IN_POW10:
5184 case BUILT_IN_POW10F:
5185 case BUILT_IN_POW10L:
5187 case BUILT_IN_EXP2F:
5188 case BUILT_IN_EXP2L:
5190 case BUILT_IN_LOGBF:
5191 case BUILT_IN_LOGBL:
5192 case BUILT_IN_ILOGB:
5193 case BUILT_IN_ILOGBF:
5194 case BUILT_IN_ILOGBL:
5198 case BUILT_IN_LOG10:
5199 case BUILT_IN_LOG10F:
5200 case BUILT_IN_LOG10L:
5202 case BUILT_IN_LOG2F:
5203 case BUILT_IN_LOG2L:
5208 case BUILT_IN_ATANF:
5209 case BUILT_IN_ATANL:
5210 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5211 because of possible accuracy problems. */
5212 if (! flag_unsafe_math_optimizations)
5215 case BUILT_IN_SQRTF:
5216 case BUILT_IN_SQRTL:
5217 case BUILT_IN_FLOOR:
5218 case BUILT_IN_FLOORF:
5219 case BUILT_IN_FLOORL:
5221 case BUILT_IN_CEILF:
5222 case BUILT_IN_CEILL:
5223 case BUILT_IN_TRUNC:
5224 case BUILT_IN_TRUNCF:
5225 case BUILT_IN_TRUNCL:
5226 case BUILT_IN_ROUND:
5227 case BUILT_IN_ROUNDF:
5228 case BUILT_IN_ROUNDL:
5229 case BUILT_IN_NEARBYINT:
5230 case BUILT_IN_NEARBYINTF:
5231 case BUILT_IN_NEARBYINTL:
5232 target = expand_builtin_mathfn (exp, target, subtarget);
5240 target = expand_builtin_pow (exp, target, subtarget);
5245 case BUILT_IN_ATAN2:
5246 case BUILT_IN_ATAN2F:
5247 case BUILT_IN_ATAN2L:
5248 if (! flag_unsafe_math_optimizations)
5250 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5261 if (! flag_unsafe_math_optimizations)
5263 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5268 case BUILT_IN_APPLY_ARGS:
5269 return expand_builtin_apply_args ();
5271 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5272 FUNCTION with a copy of the parameters described by
5273 ARGUMENTS, and ARGSIZE. It returns a block of memory
5274 allocated on the stack into which is stored all the registers
5275 that might possibly be used for returning the result of a
5276 function. ARGUMENTS is the value returned by
5277 __builtin_apply_args. ARGSIZE is the number of bytes of
5278 arguments that must be copied. ??? How should this value be
5279 computed? We'll also need a safe worst case value for varargs
5281 case BUILT_IN_APPLY:
5282 if (!validate_arglist (arglist, POINTER_TYPE,
5283 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5284 && !validate_arglist (arglist, REFERENCE_TYPE,
5285 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5293 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5294 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5296 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5299 /* __builtin_return (RESULT) causes the function to return the
5300 value described by RESULT. RESULT is address of the block of
5301 memory returned by __builtin_apply. */
5302 case BUILT_IN_RETURN:
5303 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5304 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5305 NULL_RTX, VOIDmode, 0));
5308 case BUILT_IN_SAVEREGS:
5309 return expand_builtin_saveregs ();
5311 case BUILT_IN_ARGS_INFO:
5312 return expand_builtin_args_info (arglist);
5314 /* Return the address of the first anonymous stack arg. */
5315 case BUILT_IN_NEXT_ARG:
5316 return expand_builtin_next_arg (arglist);
5318 case BUILT_IN_CLASSIFY_TYPE:
5319 return expand_builtin_classify_type (arglist);
5321 case BUILT_IN_CONSTANT_P:
5322 return expand_builtin_constant_p (arglist, target_mode);
5324 case BUILT_IN_FRAME_ADDRESS:
5325 case BUILT_IN_RETURN_ADDRESS:
5326 return expand_builtin_frame_address (fndecl, arglist);
5328 /* Returns the address of the area where the structure is returned.
5330 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5332 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5333 || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM)
5336 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5338 case BUILT_IN_ALLOCA:
5339 target = expand_builtin_alloca (arglist, target);
5346 case BUILT_IN_FFSLL:
5347 target = expand_builtin_unop (target_mode, arglist, target,
5348 subtarget, ffs_optab);
5355 case BUILT_IN_CLZLL:
5356 target = expand_builtin_unop (target_mode, arglist, target,
5357 subtarget, clz_optab);
5364 case BUILT_IN_CTZLL:
5365 target = expand_builtin_unop (target_mode, arglist, target,
5366 subtarget, ctz_optab);
5371 case BUILT_IN_POPCOUNT:
5372 case BUILT_IN_POPCOUNTL:
5373 case BUILT_IN_POPCOUNTLL:
5374 target = expand_builtin_unop (target_mode, arglist, target,
5375 subtarget, popcount_optab);
5380 case BUILT_IN_PARITY:
5381 case BUILT_IN_PARITYL:
5382 case BUILT_IN_PARITYLL:
5383 target = expand_builtin_unop (target_mode, arglist, target,
5384 subtarget, parity_optab);
5389 case BUILT_IN_STRLEN:
5390 target = expand_builtin_strlen (arglist, target, target_mode);
5395 case BUILT_IN_STRCPY:
5396 target = expand_builtin_strcpy (arglist, target, mode);
5401 case BUILT_IN_STRNCPY:
5402 target = expand_builtin_strncpy (arglist, target, mode);
5407 case BUILT_IN_STPCPY:
5408 target = expand_builtin_stpcpy (arglist, target, mode);
5413 case BUILT_IN_STRCAT:
5414 target = expand_builtin_strcat (arglist, target, mode);
5419 case BUILT_IN_STRNCAT:
5420 target = expand_builtin_strncat (arglist, target, mode);
5425 case BUILT_IN_STRSPN:
5426 target = expand_builtin_strspn (arglist, target, mode);
5431 case BUILT_IN_STRCSPN:
5432 target = expand_builtin_strcspn (arglist, target, mode);
5437 case BUILT_IN_STRSTR:
5438 target = expand_builtin_strstr (arglist, target, mode);
5443 case BUILT_IN_STRPBRK:
5444 target = expand_builtin_strpbrk (arglist, target, mode);
5449 case BUILT_IN_INDEX:
5450 case BUILT_IN_STRCHR:
5451 target = expand_builtin_strchr (arglist, target, mode);
5456 case BUILT_IN_RINDEX:
5457 case BUILT_IN_STRRCHR:
5458 target = expand_builtin_strrchr (arglist, target, mode);
5463 case BUILT_IN_MEMCPY:
5464 target = expand_builtin_memcpy (arglist, target, mode);
5469 case BUILT_IN_MEMPCPY:
5470 target = expand_builtin_mempcpy (arglist, target, mode, /*endp=*/ 1);
5475 case BUILT_IN_MEMMOVE:
5476 target = expand_builtin_memmove (arglist, target, mode);
5481 case BUILT_IN_BCOPY:
5482 target = expand_builtin_bcopy (arglist);
5487 case BUILT_IN_MEMSET:
5488 target = expand_builtin_memset (arglist, target, mode);
5493 case BUILT_IN_BZERO:
5494 target = expand_builtin_bzero (arglist);
5499 case BUILT_IN_STRCMP:
5500 target = expand_builtin_strcmp (exp, target, mode);
5505 case BUILT_IN_STRNCMP:
5506 target = expand_builtin_strncmp (exp, target, mode);
5512 case BUILT_IN_MEMCMP:
5513 target = expand_builtin_memcmp (exp, arglist, target, mode);
5518 case BUILT_IN_SETJMP:
5519 target = expand_builtin_setjmp (arglist, target);
5524 /* __builtin_longjmp is passed a pointer to an array of five words.
5525 It's similar to the C library longjmp function but works with
5526 __builtin_setjmp above. */
5527 case BUILT_IN_LONGJMP:
5528 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5532 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5534 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5535 NULL_RTX, VOIDmode, 0);
5537 if (value != const1_rtx)
5539 error ("__builtin_longjmp second argument must be 1");
5543 expand_builtin_longjmp (buf_addr, value);
5548 expand_builtin_trap ();
5551 case BUILT_IN_PRINTF:
5552 target = expand_builtin_printf (arglist, target, mode, false);
5557 case BUILT_IN_PRINTF_UNLOCKED:
5558 target = expand_builtin_printf (arglist, target, mode, true);
5563 case BUILT_IN_FPUTS:
5564 target = expand_builtin_fputs (arglist, target, false);
5569 case BUILT_IN_FPUTS_UNLOCKED:
5570 target = expand_builtin_fputs (arglist, target, true);
5575 case BUILT_IN_FPRINTF:
5576 target = expand_builtin_fprintf (arglist, target, mode, false);
5581 case BUILT_IN_FPRINTF_UNLOCKED:
5582 target = expand_builtin_fprintf (arglist, target, mode, true);
5587 case BUILT_IN_SPRINTF:
5588 target = expand_builtin_sprintf (arglist, target, mode);
5593 case BUILT_IN_SIGNBIT:
5594 case BUILT_IN_SIGNBITF:
5595 case BUILT_IN_SIGNBITL:
5596 target = expand_builtin_signbit (exp, target);
5601 /* Various hooks for the DWARF 2 __throw routine. */
5602 case BUILT_IN_UNWIND_INIT:
5603 expand_builtin_unwind_init ();
5605 case BUILT_IN_DWARF_CFA:
5606 return virtual_cfa_rtx;
5607 #ifdef DWARF2_UNWIND_INFO
5608 case BUILT_IN_DWARF_SP_COLUMN:
5609 return expand_builtin_dwarf_sp_column ();
5610 case BUILT_IN_INIT_DWARF_REG_SIZES:
5611 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5614 case BUILT_IN_FROB_RETURN_ADDR:
5615 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5616 case BUILT_IN_EXTRACT_RETURN_ADDR:
5617 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5618 case BUILT_IN_EH_RETURN:
5619 expand_builtin_eh_return (TREE_VALUE (arglist),
5620 TREE_VALUE (TREE_CHAIN (arglist)));
5622 #ifdef EH_RETURN_DATA_REGNO
5623 case BUILT_IN_EH_RETURN_DATA_REGNO:
5624 return expand_builtin_eh_return_data_regno (arglist);
5626 case BUILT_IN_EXTEND_POINTER:
5627 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
5629 case BUILT_IN_VA_START:
5630 case BUILT_IN_STDARG_START:
5631 return expand_builtin_va_start (arglist);
5632 case BUILT_IN_VA_END:
5633 return expand_builtin_va_end (arglist);
5634 case BUILT_IN_VA_COPY:
5635 return expand_builtin_va_copy (arglist);
5636 case BUILT_IN_EXPECT:
5637 return expand_builtin_expect (arglist, target);
5638 case BUILT_IN_PREFETCH:
5639 expand_builtin_prefetch (arglist);
5643 default: /* just do library call, if unknown builtin */
5644 if (!DECL_ASSEMBLER_NAME_SET_P (fndecl))
5645 error ("built-in function `%s' not currently supported",
5646 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
5649 /* The switch statement above can drop through to cause the function
5650 to be called normally. */
5651 return expand_call (exp, target, ignore);
5654 /* Determine whether a tree node represents a call to a built-in
5655 function. If the tree T is a call to a built-in function with
5656 the right number of arguments of the appropriate types, return
5657 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
5658 Otherwise the return value is END_BUILTINS. */
5660 enum built_in_function
5661 builtin_mathfn_code (tree t)
5663 tree fndecl, arglist, parmlist;
5664 tree argtype, parmtype;
5666 if (TREE_CODE (t) != CALL_EXPR
5667 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
5668 return END_BUILTINS;
5670 fndecl = get_callee_fndecl (t);
5671 if (fndecl == NULL_TREE
5672 || TREE_CODE (fndecl) != FUNCTION_DECL
5673 || ! DECL_BUILT_IN (fndecl)
5674 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5675 return END_BUILTINS;
5677 arglist = TREE_OPERAND (t, 1);
5678 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5679 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
5681 /* If a function doesn't take a variable number of arguments,
5682 the last element in the list will have type `void'. */
5683 parmtype = TREE_VALUE (parmlist);
5684 if (VOID_TYPE_P (parmtype))
5687 return END_BUILTINS;
5688 return DECL_FUNCTION_CODE (fndecl);
5692 return END_BUILTINS;
5694 argtype = TREE_TYPE (TREE_VALUE (arglist));
5696 if (SCALAR_FLOAT_TYPE_P (parmtype))
5698 if (! SCALAR_FLOAT_TYPE_P (argtype))
5699 return END_BUILTINS;
5701 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
5703 if (! COMPLEX_FLOAT_TYPE_P (argtype))
5704 return END_BUILTINS;
5706 else if (POINTER_TYPE_P (parmtype))
5708 if (! POINTER_TYPE_P (argtype))
5709 return END_BUILTINS;
5711 else if (INTEGRAL_TYPE_P (parmtype))
5713 if (! INTEGRAL_TYPE_P (argtype))
5714 return END_BUILTINS;
5717 return END_BUILTINS;
5719 arglist = TREE_CHAIN (arglist);
5722 /* Variable-length argument list. */
5723 return DECL_FUNCTION_CODE (fndecl);
5726 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5727 constant. ARGLIST is the argument list of the call. */
5730 fold_builtin_constant_p (tree arglist)
5735 arglist = TREE_VALUE (arglist);
5737 /* We return 1 for a numeric type that's known to be a constant
5738 value at compile-time or for an aggregate type that's a
5739 literal constant. */
5740 STRIP_NOPS (arglist);
5742 /* If we know this is a constant, emit the constant of one. */
5743 if (TREE_CODE_CLASS (TREE_CODE (arglist)) == 'c'
5744 || (TREE_CODE (arglist) == CONSTRUCTOR
5745 && TREE_CONSTANT (arglist))
5746 || (TREE_CODE (arglist) == ADDR_EXPR
5747 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
5748 return integer_one_node;
5750 /* If this expression has side effects, show we don't know it to be a
5751 constant. Likewise if it's a pointer or aggregate type since in
5752 those case we only want literals, since those are only optimized
5753 when generating RTL, not later.
5754 And finally, if we are compiling an initializer, not code, we
5755 need to return a definite result now; there's not going to be any
5756 more optimization done. */
5757 if (TREE_SIDE_EFFECTS (arglist)
5758 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
5759 || POINTER_TYPE_P (TREE_TYPE (arglist))
5761 return integer_zero_node;
5766 /* Fold a call to __builtin_classify_type. */
5769 fold_builtin_classify_type (tree arglist)
5772 return build_int_2 (no_type_class, 0);
5774 return build_int_2 (type_to_class (TREE_TYPE (TREE_VALUE (arglist))), 0);
5777 /* Fold a call to __builtin_inf or __builtin_huge_val. */
5780 fold_builtin_inf (tree type, int warn)
5782 REAL_VALUE_TYPE real;
5784 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
5785 warning ("target format does not support infinity");
5788 return build_real (type, real);
5791 /* Fold a call to __builtin_nan or __builtin_nans. */
5794 fold_builtin_nan (tree arglist, tree type, int quiet)
5796 REAL_VALUE_TYPE real;
5799 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5801 str = c_getstr (TREE_VALUE (arglist));
5805 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
5808 return build_real (type, real);
5811 /* Return true if the floating point expression T has an integer value.
5812 We also allow +Inf, -Inf and NaN to be considered integer values. */
5815 integer_valued_real_p (tree t)
5817 switch (TREE_CODE (t))
5824 case NON_LVALUE_EXPR:
5825 return integer_valued_real_p (TREE_OPERAND (t, 0));
5830 return integer_valued_real_p (TREE_OPERAND (t, 1));
5837 return integer_valued_real_p (TREE_OPERAND (t, 0))
5838 && integer_valued_real_p (TREE_OPERAND (t, 1));
5841 return integer_valued_real_p (TREE_OPERAND (t, 1))
5842 && integer_valued_real_p (TREE_OPERAND (t, 2));
5845 if (! TREE_CONSTANT_OVERFLOW (t))
5847 REAL_VALUE_TYPE c, cint;
5849 c = TREE_REAL_CST (t);
5850 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
5851 return real_identical (&c, &cint);
5856 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
5857 if (TREE_CODE (type) == INTEGER_TYPE)
5859 if (TREE_CODE (type) == REAL_TYPE)
5860 return integer_valued_real_p (TREE_OPERAND (t, 0));
5865 switch (builtin_mathfn_code (t))
5868 case BUILT_IN_CEILF:
5869 case BUILT_IN_CEILL:
5870 case BUILT_IN_FLOOR:
5871 case BUILT_IN_FLOORF:
5872 case BUILT_IN_FLOORL:
5873 case BUILT_IN_NEARBYINT:
5874 case BUILT_IN_NEARBYINTF:
5875 case BUILT_IN_NEARBYINTL:
5877 case BUILT_IN_RINTF:
5878 case BUILT_IN_RINTL:
5879 case BUILT_IN_ROUND:
5880 case BUILT_IN_ROUNDF:
5881 case BUILT_IN_ROUNDL:
5882 case BUILT_IN_TRUNC:
5883 case BUILT_IN_TRUNCF:
5884 case BUILT_IN_TRUNCL:
5898 /* EXP is assumed to be builtin call where truncation can be propagated
5899 across (for instance floor((double)f) == (double)floorf (f).
5900 Do the transformation. */
5903 fold_trunc_transparent_mathfn (tree exp)
5905 tree fndecl = get_callee_fndecl (exp);
5906 tree arglist = TREE_OPERAND (exp, 1);
5907 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5910 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5913 arg = TREE_VALUE (arglist);
5914 /* Integer rounding functions are idempotent. */
5915 if (fcode == builtin_mathfn_code (arg))
5918 /* If argument is already integer valued, and we don't need to worry
5919 about setting errno, there's no need to perform rounding. */
5920 if (! flag_errno_math && integer_valued_real_p (arg))
5925 tree arg0 = strip_float_extensions (arg);
5926 tree ftype = TREE_TYPE (exp);
5927 tree newtype = TREE_TYPE (arg0);
5930 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
5931 && (decl = mathfn_built_in (newtype, fcode)))
5934 build_tree_list (NULL_TREE, fold (convert (newtype, arg0)));
5935 return convert (ftype,
5936 build_function_call_expr (decl, arglist));
5942 /* Fold function call to builtin cabs, cabsf or cabsl. FNDECL is the
5943 function's DECL, ARGLIST is the argument list and TYPE is the return
5944 type. Return NULL_TREE if no simplification can be made. */
5947 fold_builtin_cabs (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
5951 if (!arglist || TREE_CHAIN (arglist))
5954 arg = TREE_VALUE (arglist);
5955 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
5956 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
5959 /* Evaluate cabs of a constant at compile-time. */
5960 if (flag_unsafe_math_optimizations
5961 && TREE_CODE (arg) == COMPLEX_CST
5962 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
5963 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
5964 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
5965 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
5967 REAL_VALUE_TYPE r, i;
5969 r = TREE_REAL_CST (TREE_REALPART (arg));
5970 i = TREE_REAL_CST (TREE_IMAGPART (arg));
5972 real_arithmetic (&r, MULT_EXPR, &r, &r);
5973 real_arithmetic (&i, MULT_EXPR, &i, &i);
5974 real_arithmetic (&r, PLUS_EXPR, &r, &i);
5975 if (real_sqrt (&r, TYPE_MODE (type), &r)
5976 || ! flag_trapping_math)
5977 return build_real (type, r);
5980 /* If either part is zero, cabs is fabs of the other. */
5981 if (TREE_CODE (arg) == COMPLEX_EXPR
5982 && real_zerop (TREE_OPERAND (arg, 0)))
5983 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
5984 if (TREE_CODE (arg) == COMPLEX_EXPR
5985 && real_zerop (TREE_OPERAND (arg, 1)))
5986 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
5988 if (flag_unsafe_math_optimizations)
5990 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
5992 if (sqrtfn != NULL_TREE)
5994 tree rpart, ipart, result, arglist;
5996 arg = save_expr (arg);
5998 rpart = fold (build1 (REALPART_EXPR, type, arg));
5999 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
6001 rpart = save_expr (rpart);
6002 ipart = save_expr (ipart);
6004 result = fold (build (PLUS_EXPR, type,
6005 fold (build (MULT_EXPR, type,
6007 fold (build (MULT_EXPR, type,
6010 arglist = build_tree_list (NULL_TREE, result);
6011 return build_function_call_expr (sqrtfn, arglist);
6018 /* Fold function call to builtin trunc, truncf or truncl. Return
6019 NULL_TREE if no simplification can be made. */
6022 fold_builtin_trunc (tree exp)
6024 tree arglist = TREE_OPERAND (exp, 1);
6027 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6030 /* Optimize trunc of constant value. */
6031 arg = TREE_VALUE (arglist);
6032 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6034 REAL_VALUE_TYPE r, x;
6035 tree type = TREE_TYPE (exp);
6037 x = TREE_REAL_CST (arg);
6038 real_trunc (&r, TYPE_MODE (type), &x);
6039 return build_real (type, r);
6042 return fold_trunc_transparent_mathfn (exp);
6045 /* Fold function call to builtin floor, floorf or floorl. Return
6046 NULL_TREE if no simplification can be made. */
6049 fold_builtin_floor (tree exp)
6051 tree arglist = TREE_OPERAND (exp, 1);
6054 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6057 /* Optimize floor of constant value. */
6058 arg = TREE_VALUE (arglist);
6059 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6063 x = TREE_REAL_CST (arg);
6064 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6066 tree type = TREE_TYPE (exp);
6069 real_floor (&r, TYPE_MODE (type), &x);
6070 return build_real (type, r);
6074 return fold_trunc_transparent_mathfn (exp);
6077 /* Fold function call to builtin ceil, ceilf or ceill. Return
6078 NULL_TREE if no simplification can be made. */
6081 fold_builtin_ceil (tree exp)
6083 tree arglist = TREE_OPERAND (exp, 1);
6086 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6089 /* Optimize ceil of constant value. */
6090 arg = TREE_VALUE (arglist);
6091 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6095 x = TREE_REAL_CST (arg);
6096 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6098 tree type = TREE_TYPE (exp);
6101 real_ceil (&r, TYPE_MODE (type), &x);
6102 return build_real (type, r);
6106 return fold_trunc_transparent_mathfn (exp);
6109 /* Fold function call to builtin round, roundf or roundl. Return
6110 NULL_TREE if no simplification can be made. */
6113 fold_builtin_round (tree exp)
6115 tree arglist = TREE_OPERAND (exp, 1);
6118 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6121 /* Optimize ceil of constant value. */
6122 arg = TREE_VALUE (arglist);
6123 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6127 x = TREE_REAL_CST (arg);
6128 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6130 tree type = TREE_TYPE (exp);
6133 real_round (&r, TYPE_MODE (type), &x);
6134 return build_real (type, r);
6138 return fold_trunc_transparent_mathfn (exp);
6141 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
6142 and their long and long long variants (i.e. ffsl and ffsll).
6143 Return NULL_TREE if no simplification can be made. */
6146 fold_builtin_bitop (tree exp)
6148 tree fndecl = get_callee_fndecl (exp);
6149 tree arglist = TREE_OPERAND (exp, 1);
6152 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6155 /* Optimize for constant argument. */
6156 arg = TREE_VALUE (arglist);
6157 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6159 HOST_WIDE_INT hi, width, result;
6160 unsigned HOST_WIDE_INT lo;
6163 type = TREE_TYPE (arg);
6164 width = TYPE_PRECISION (type);
6165 lo = TREE_INT_CST_LOW (arg);
6167 /* Clear all the bits that are beyond the type's precision. */
6168 if (width > HOST_BITS_PER_WIDE_INT)
6170 hi = TREE_INT_CST_HIGH (arg);
6171 if (width < 2 * HOST_BITS_PER_WIDE_INT)
6172 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
6177 if (width < HOST_BITS_PER_WIDE_INT)
6178 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
6181 switch (DECL_FUNCTION_CODE (fndecl))
6185 case BUILT_IN_FFSLL:
6187 result = exact_log2 (lo & -lo) + 1;
6189 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
6196 case BUILT_IN_CLZLL:
6198 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
6200 result = width - floor_log2 (lo) - 1;
6201 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6207 case BUILT_IN_CTZLL:
6209 result = exact_log2 (lo & -lo);
6211 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
6212 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6216 case BUILT_IN_POPCOUNT:
6217 case BUILT_IN_POPCOUNTL:
6218 case BUILT_IN_POPCOUNTLL:
6221 result++, lo &= lo - 1;
6223 result++, hi &= hi - 1;
6226 case BUILT_IN_PARITY:
6227 case BUILT_IN_PARITYL:
6228 case BUILT_IN_PARITYLL:
6231 result++, lo &= lo - 1;
6233 result++, hi &= hi - 1;
6241 t = build_int_2 (result, 0);
6242 TREE_TYPE (t) = TREE_TYPE (exp);
6249 /* Return true if EXPR is the real constant contained in VALUE. */
6252 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
6256 return ((TREE_CODE (expr) == REAL_CST
6257 && ! TREE_CONSTANT_OVERFLOW (expr)
6258 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
6259 || (TREE_CODE (expr) == COMPLEX_CST
6260 && real_dconstp (TREE_REALPART (expr), value)
6261 && real_zerop (TREE_IMAGPART (expr))));
6264 /* A subroutine of fold_builtin to fold the various logarithmic
6265 functions. EXP is the CALL_EXPR of a call to a builtin logN
6266 function. VALUE is the base of the logN function. */
6269 fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
6271 tree arglist = TREE_OPERAND (exp, 1);
6273 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6275 tree fndecl = get_callee_fndecl (exp);
6276 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6277 tree arg = TREE_VALUE (arglist);
6278 const enum built_in_function fcode = builtin_mathfn_code (arg);
6280 /* Optimize logN(1.0) = 0.0. */
6281 if (real_onep (arg))
6282 return build_real (type, dconst0);
6284 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
6285 exactly, then only do this if flag_unsafe_math_optimizations. */
6286 if (exact_real_truncate (TYPE_MODE (type), value)
6287 || flag_unsafe_math_optimizations)
6289 const REAL_VALUE_TYPE value_truncate =
6290 real_value_truncate (TYPE_MODE (type), *value);
6291 if (real_dconstp (arg, &value_truncate))
6292 return build_real (type, dconst1);
6295 /* Special case, optimize logN(expN(x)) = x. */
6296 if (flag_unsafe_math_optimizations
6297 && ((value == &dconste
6298 && (fcode == BUILT_IN_EXP
6299 || fcode == BUILT_IN_EXPF
6300 || fcode == BUILT_IN_EXPL))
6301 || (value == &dconst2
6302 && (fcode == BUILT_IN_EXP2
6303 || fcode == BUILT_IN_EXP2F
6304 || fcode == BUILT_IN_EXP2L))
6305 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
6306 return convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6308 /* Optimize logN(func()) for various exponential functions. We
6309 want to determine the value "x" and the power "exponent" in
6310 order to transform logN(x**exponent) into exponent*logN(x). */
6311 if (flag_unsafe_math_optimizations)
6313 tree exponent = 0, x = 0;
6320 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
6321 x = build_real (type,
6322 real_value_truncate (TYPE_MODE (type), dconste));
6323 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6326 case BUILT_IN_EXP2F:
6327 case BUILT_IN_EXP2L:
6328 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
6329 x = build_real (type, dconst2);
6330 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6332 case BUILT_IN_EXP10:
6333 case BUILT_IN_EXP10F:
6334 case BUILT_IN_EXP10L:
6335 case BUILT_IN_POW10:
6336 case BUILT_IN_POW10F:
6337 case BUILT_IN_POW10L:
6338 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
6339 x = build_real (type, dconst10);
6340 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6343 case BUILT_IN_SQRTF:
6344 case BUILT_IN_SQRTL:
6345 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
6346 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6347 exponent = build_real (type, dconsthalf);
6350 case BUILT_IN_CBRTF:
6351 case BUILT_IN_CBRTL:
6352 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
6353 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6354 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
6360 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
6361 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6362 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6368 /* Now perform the optimization. */
6372 arglist = build_tree_list (NULL_TREE, x);
6373 logfn = build_function_call_expr (fndecl, arglist);
6374 return fold (build (MULT_EXPR, type, exponent, logfn));
6382 /* A subroutine of fold_builtin to fold the various exponent
6383 functions. EXP is the CALL_EXPR of a call to a builtin function.
6384 VALUE is the value which will be raised to a power. */
6387 fold_builtin_exponent (tree exp, const REAL_VALUE_TYPE *value)
6389 tree arglist = TREE_OPERAND (exp, 1);
6391 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6393 tree fndecl = get_callee_fndecl (exp);
6394 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6395 tree arg = TREE_VALUE (arglist);
6397 /* Optimize exp*(0.0) = 1.0. */
6398 if (real_zerop (arg))
6399 return build_real (type, dconst1);
6401 /* Optimize expN(1.0) = N. */
6402 if (real_onep (arg))
6404 REAL_VALUE_TYPE cst;
6406 real_convert (&cst, TYPE_MODE (type), value);
6407 return build_real (type, cst);
6410 /* Attempt to evaluate expN(integer) at compile-time. */
6411 if (flag_unsafe_math_optimizations
6412 && TREE_CODE (arg) == REAL_CST
6413 && ! TREE_CONSTANT_OVERFLOW (arg))
6415 REAL_VALUE_TYPE cint;
6419 c = TREE_REAL_CST (arg);
6420 n = real_to_integer (&c);
6421 real_from_integer (&cint, VOIDmode, n,
6423 if (real_identical (&c, &cint))
6427 real_powi (&x, TYPE_MODE (type), value, n);
6428 return build_real (type, x);
6432 /* Optimize expN(logN(x)) = x. */
6433 if (flag_unsafe_math_optimizations)
6435 const enum built_in_function fcode = builtin_mathfn_code (arg);
6437 if ((value == &dconste
6438 && (fcode == BUILT_IN_LOG
6439 || fcode == BUILT_IN_LOGF
6440 || fcode == BUILT_IN_LOGL))
6441 || (value == &dconst2
6442 && (fcode == BUILT_IN_LOG2
6443 || fcode == BUILT_IN_LOG2F
6444 || fcode == BUILT_IN_LOG2L))
6445 || (value == &dconst10
6446 && (fcode == BUILT_IN_LOG10
6447 || fcode == BUILT_IN_LOG10F
6448 || fcode == BUILT_IN_LOG10L)))
6449 return convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6456 /* Fold function call to builtin memcpy. Return
6457 NULL_TREE if no simplification can be made. */
6460 fold_builtin_memcpy (tree exp)
6462 tree arglist = TREE_OPERAND (exp, 1);
6463 tree dest, src, len;
6465 if (!validate_arglist (arglist,
6466 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6469 dest = TREE_VALUE (arglist);
6470 src = TREE_VALUE (TREE_CHAIN (arglist));
6471 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6473 /* If the LEN parameter is zero, return DEST. */
6474 if (integer_zerop (len))
6475 return omit_one_operand (TREE_TYPE (exp), dest, src);
6477 /* If SRC and DEST are the same (and not volatile), return DEST. */
6478 if (operand_equal_p (src, dest, 0))
6479 return omit_one_operand (TREE_TYPE (exp), dest, len);
6484 /* Fold function call to builtin mempcpy. Return
6485 NULL_TREE if no simplification can be made. */
6488 fold_builtin_mempcpy (tree exp)
6490 tree arglist = TREE_OPERAND (exp, 1);
6491 tree dest, src, len;
6493 if (!validate_arglist (arglist,
6494 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6497 dest = TREE_VALUE (arglist);
6498 src = TREE_VALUE (TREE_CHAIN (arglist));
6499 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6501 /* If the LEN parameter is zero, return DEST. */
6502 if (integer_zerop (len))
6503 return omit_one_operand (TREE_TYPE (exp), dest, src);
6505 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
6506 if (operand_equal_p (src, dest, 0))
6508 tree temp = convert (TREE_TYPE (dest), len);
6509 temp = fold (build (PLUS_EXPR, TREE_TYPE (dest), dest, len));
6510 return convert (TREE_TYPE (exp), temp);
6516 /* Fold function call to builtin memmove. Return
6517 NULL_TREE if no simplification can be made. */
6520 fold_builtin_memmove (tree exp)
6522 tree arglist = TREE_OPERAND (exp, 1);
6523 tree dest, src, len;
6525 if (!validate_arglist (arglist,
6526 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6529 dest = TREE_VALUE (arglist);
6530 src = TREE_VALUE (TREE_CHAIN (arglist));
6531 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6533 /* If the LEN parameter is zero, return DEST. */
6534 if (integer_zerop (len))
6535 return omit_one_operand (TREE_TYPE (exp), dest, src);
6537 /* If SRC and DEST are the same (and not volatile), return DEST. */
6538 if (operand_equal_p (src, dest, 0))
6539 return omit_one_operand (TREE_TYPE (exp), dest, len);
6544 /* Fold function call to builtin strcpy. Return
6545 NULL_TREE if no simplification can be made. */
6548 fold_builtin_strcpy (tree exp)
6550 tree arglist = TREE_OPERAND (exp, 1);
6553 if (!validate_arglist (arglist,
6554 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6557 dest = TREE_VALUE (arglist);
6558 src = TREE_VALUE (TREE_CHAIN (arglist));
6560 /* If SRC and DEST are the same (and not volatile), return DEST. */
6561 if (operand_equal_p (src, dest, 0))
6562 return convert (TREE_TYPE (exp), dest);
6567 /* Fold function call to builtin strncpy. Return
6568 NULL_TREE if no simplification can be made. */
6571 fold_builtin_strncpy (tree exp)
6573 tree arglist = TREE_OPERAND (exp, 1);
6574 tree dest, src, len;
6576 if (!validate_arglist (arglist,
6577 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6580 dest = TREE_VALUE (arglist);
6581 src = TREE_VALUE (TREE_CHAIN (arglist));
6582 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6584 /* If the LEN parameter is zero, return DEST. */
6585 if (integer_zerop (len))
6586 return omit_one_operand (TREE_TYPE (exp), dest, src);
6591 /* Fold function call to builtin memcmp. Return
6592 NULL_TREE if no simplification can be made. */
6595 fold_builtin_memcmp (tree exp)
6597 tree arglist = TREE_OPERAND (exp, 1);
6598 tree arg1, arg2, len;
6600 if (!validate_arglist (arglist,
6601 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6604 arg1 = TREE_VALUE (arglist);
6605 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
6606 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6608 /* If the LEN parameter is zero, return zero. */
6609 if (integer_zerop (len))
6611 tree temp = omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg2);
6612 return omit_one_operand (TREE_TYPE (exp), temp, arg1);
6615 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
6616 if (operand_equal_p (arg1, arg2, 0))
6617 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, len);
6622 /* Fold function call to builtin strcmp. Return
6623 NULL_TREE if no simplification can be made. */
6626 fold_builtin_strcmp (tree exp)
6628 tree arglist = TREE_OPERAND (exp, 1);
6630 const char *p1, *p2;
6632 if (!validate_arglist (arglist,
6633 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6636 arg1 = TREE_VALUE (arglist);
6637 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
6639 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
6640 if (operand_equal_p (arg1, arg2, 0))
6641 return convert (TREE_TYPE (exp), integer_zero_node);
6643 p1 = c_getstr (arg1);
6644 p2 = c_getstr (arg2);
6649 const int i = strcmp (p1, p2);
6651 temp = integer_minus_one_node;
6653 temp = integer_one_node;
6655 temp = integer_zero_node;
6656 return convert (TREE_TYPE (exp), temp);
6662 /* Fold function call to builtin strncmp. Return
6663 NULL_TREE if no simplification can be made. */
6666 fold_builtin_strncmp (tree exp)
6668 tree arglist = TREE_OPERAND (exp, 1);
6669 tree arg1, arg2, len;
6670 const char *p1, *p2;
6672 if (!validate_arglist (arglist,
6673 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6676 arg1 = TREE_VALUE (arglist);
6677 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
6678 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6680 /* If the LEN parameter is zero, return zero. */
6681 if (integer_zerop (len))
6683 tree temp = omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg2);
6684 return omit_one_operand (TREE_TYPE (exp), temp, arg1);
6687 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
6688 if (operand_equal_p (arg1, arg2, 0))
6689 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, len);
6691 p1 = c_getstr (arg1);
6692 p2 = c_getstr (arg2);
6694 if (host_integerp (len, 1) && p1 && p2)
6697 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
6699 temp = integer_minus_one_node;
6701 temp = integer_one_node;
6703 temp = integer_zero_node;
6704 return convert (TREE_TYPE (exp), temp);
6710 /* Fold function call to builtin signbit, signbitf or signbitl. Return
6711 NULL_TREE if no simplification can be made. */
6714 fold_builtin_signbit (tree exp)
6716 tree arglist = TREE_OPERAND (exp, 1);
6719 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6722 arg = TREE_VALUE (arglist);
6724 /* If ARG is a compile-time constant, determine the result. */
6725 if (TREE_CODE (arg) == REAL_CST
6726 && !TREE_CONSTANT_OVERFLOW (arg))
6730 c = TREE_REAL_CST (arg);
6731 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
6732 return convert (TREE_TYPE (exp), temp);
6735 /* If ARG is non-negative, the result is always zero. */
6736 if (tree_expr_nonnegative_p (arg))
6737 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg);
6739 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
6740 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
6741 return fold (build (LT_EXPR, TREE_TYPE (exp), arg,
6742 build_real (TREE_TYPE (arg), dconst0)));
6747 /* Fold a call to builtin isascii. */
6750 fold_builtin_isascii (tree arglist)
6752 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6756 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
6757 tree arg = TREE_VALUE (arglist);
6759 return fold (build (EQ_EXPR, integer_type_node,
6760 build (BIT_AND_EXPR, integer_type_node, arg,
6761 build_int_2 (~ (unsigned HOST_WIDE_INT) 0x7f,
6762 ~ (HOST_WIDE_INT) 0)),
6763 integer_zero_node));
6767 /* Fold a call to builtin toascii. */
6770 fold_builtin_toascii (tree arglist)
6772 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6776 /* Transform toascii(c) -> (c & 0x7f). */
6777 tree arg = TREE_VALUE (arglist);
6779 return fold (build (BIT_AND_EXPR, integer_type_node, arg,
6780 build_int_2 (0x7f, 0)));
6784 /* Fold a call to builtin isdigit. */
6787 fold_builtin_isdigit (tree arglist)
6789 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6793 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
6794 /* According to the C standard, isdigit is unaffected by locale. */
6795 tree arg = TREE_VALUE (arglist);
6796 arg = build1 (NOP_EXPR, unsigned_type_node, arg);
6797 arg = build (MINUS_EXPR, unsigned_type_node, arg,
6798 fold (build1 (NOP_EXPR, unsigned_type_node,
6799 build_int_2 (TARGET_DIGIT0, 0))));
6800 arg = build (LE_EXPR, integer_type_node, arg,
6801 fold (build1 (NOP_EXPR, unsigned_type_node,
6802 build_int_2 (9, 0))));
6807 /* Used by constant folding to eliminate some builtin calls early. EXP is
6808 the CALL_EXPR of a call to a builtin function. */
6811 fold_builtin (tree exp)
6813 tree fndecl = get_callee_fndecl (exp);
6814 tree arglist = TREE_OPERAND (exp, 1);
6815 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6817 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6820 switch (DECL_FUNCTION_CODE (fndecl))
6822 case BUILT_IN_CONSTANT_P:
6823 return fold_builtin_constant_p (arglist);
6825 case BUILT_IN_CLASSIFY_TYPE:
6826 return fold_builtin_classify_type (arglist);
6828 case BUILT_IN_STRLEN:
6829 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6831 tree len = c_strlen (TREE_VALUE (arglist), 0);
6834 /* Convert from the internal "sizetype" type to "size_t". */
6836 len = convert (size_type_node, len);
6843 case BUILT_IN_FABSF:
6844 case BUILT_IN_FABSL:
6845 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6846 return fold (build1 (ABS_EXPR, type, TREE_VALUE (arglist)));
6850 case BUILT_IN_CABSF:
6851 case BUILT_IN_CABSL:
6852 return fold_builtin_cabs (fndecl, arglist, type);
6855 case BUILT_IN_SQRTF:
6856 case BUILT_IN_SQRTL:
6857 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6859 enum built_in_function fcode;
6860 tree arg = TREE_VALUE (arglist);
6862 /* Optimize sqrt of constant value. */
6863 if (TREE_CODE (arg) == REAL_CST
6864 && ! TREE_CONSTANT_OVERFLOW (arg))
6866 REAL_VALUE_TYPE r, x;
6868 x = TREE_REAL_CST (arg);
6869 if (real_sqrt (&r, TYPE_MODE (type), &x)
6870 || (!flag_trapping_math && !flag_errno_math))
6871 return build_real (type, r);
6874 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
6875 fcode = builtin_mathfn_code (arg);
6876 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6878 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6879 arg = fold (build (MULT_EXPR, type,
6880 TREE_VALUE (TREE_OPERAND (arg, 1)),
6881 build_real (type, dconsthalf)));
6882 arglist = build_tree_list (NULL_TREE, arg);
6883 return build_function_call_expr (expfn, arglist);
6886 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
6887 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
6889 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6893 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6895 /* The inner root was either sqrt or cbrt. */
6896 REAL_VALUE_TYPE dconstroot =
6897 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
6899 /* Adjust for the outer root. */
6900 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6901 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6902 tree_root = build_real (type, dconstroot);
6903 arglist = tree_cons (NULL_TREE, arg0,
6904 build_tree_list (NULL_TREE, tree_root));
6905 return build_function_call_expr (powfn, arglist);
6909 /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5). */
6910 if (flag_unsafe_math_optimizations
6911 && (fcode == BUILT_IN_POW
6912 || fcode == BUILT_IN_POWF
6913 || fcode == BUILT_IN_POWL))
6915 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6916 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6917 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6918 tree narg1 = fold (build (MULT_EXPR, type, arg1,
6919 build_real (type, dconsthalf)));
6920 arglist = tree_cons (NULL_TREE, arg0,
6921 build_tree_list (NULL_TREE, narg1));
6922 return build_function_call_expr (powfn, arglist);
6928 case BUILT_IN_CBRTF:
6929 case BUILT_IN_CBRTL:
6930 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6932 tree arg = TREE_VALUE (arglist);
6933 const enum built_in_function fcode = builtin_mathfn_code (arg);
6935 /* Optimize cbrt of constant value. */
6936 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
6939 /* Optimize cbrt(expN(x)) -> expN(x/3). */
6940 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6942 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6943 const REAL_VALUE_TYPE third_trunc =
6944 real_value_truncate (TYPE_MODE (type), dconstthird);
6945 arg = fold (build (MULT_EXPR, type,
6946 TREE_VALUE (TREE_OPERAND (arg, 1)),
6947 build_real (type, third_trunc)));
6948 arglist = build_tree_list (NULL_TREE, arg);
6949 return build_function_call_expr (expfn, arglist);
6952 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
6953 /* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
6954 x is negative pow will error but cbrt won't. */
6955 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
6957 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6961 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6963 REAL_VALUE_TYPE dconstroot = dconstthird;
6965 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6966 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6967 tree_root = build_real (type, dconstroot);
6968 arglist = tree_cons (NULL_TREE, arg0,
6969 build_tree_list (NULL_TREE, tree_root));
6970 return build_function_call_expr (powfn, arglist);
6980 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6982 tree arg = TREE_VALUE (arglist);
6984 /* Optimize sin(0.0) = 0.0. */
6985 if (real_zerop (arg))
6993 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6995 tree arg = TREE_VALUE (arglist);
6997 /* Optimize cos(0.0) = 1.0. */
6998 if (real_zerop (arg))
6999 return build_real (type, dconst1);
7001 /* Optimize cos(-x) into cos(x). */
7002 if (TREE_CODE (arg) == NEGATE_EXPR)
7004 tree arglist = build_tree_list (NULL_TREE,
7005 TREE_OPERAND (arg, 0));
7006 return build_function_call_expr (fndecl, arglist);
7014 return fold_builtin_exponent (exp, &dconste);
7016 case BUILT_IN_EXP2F:
7017 case BUILT_IN_EXP2L:
7018 return fold_builtin_exponent (exp, &dconst2);
7019 case BUILT_IN_EXP10:
7020 case BUILT_IN_EXP10F:
7021 case BUILT_IN_EXP10L:
7022 case BUILT_IN_POW10:
7023 case BUILT_IN_POW10F:
7024 case BUILT_IN_POW10L:
7025 return fold_builtin_exponent (exp, &dconst10);
7029 return fold_builtin_logarithm (exp, &dconste);
7032 case BUILT_IN_LOG2F:
7033 case BUILT_IN_LOG2L:
7034 return fold_builtin_logarithm (exp, &dconst2);
7036 case BUILT_IN_LOG10:
7037 case BUILT_IN_LOG10F:
7038 case BUILT_IN_LOG10L:
7039 return fold_builtin_logarithm (exp, &dconst10);
7045 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7047 enum built_in_function fcode;
7048 tree arg = TREE_VALUE (arglist);
7050 /* Optimize tan(0.0) = 0.0. */
7051 if (real_zerop (arg))
7054 /* Optimize tan(atan(x)) = x. */
7055 fcode = builtin_mathfn_code (arg);
7056 if (flag_unsafe_math_optimizations
7057 && (fcode == BUILT_IN_ATAN
7058 || fcode == BUILT_IN_ATANF
7059 || fcode == BUILT_IN_ATANL))
7060 return TREE_VALUE (TREE_OPERAND (arg, 1));
7065 case BUILT_IN_ATANF:
7066 case BUILT_IN_ATANL:
7067 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7069 tree arg = TREE_VALUE (arglist);
7071 /* Optimize atan(0.0) = 0.0. */
7072 if (real_zerop (arg))
7075 /* Optimize atan(1.0) = pi/4. */
7076 if (real_onep (arg))
7078 REAL_VALUE_TYPE cst;
7080 real_convert (&cst, TYPE_MODE (type), &dconstpi);
7081 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
7082 return build_real (type, cst);
7090 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7092 enum built_in_function fcode;
7093 tree arg0 = TREE_VALUE (arglist);
7094 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7096 /* Optimize pow(1.0,y) = 1.0. */
7097 if (real_onep (arg0))
7098 return omit_one_operand (type, build_real (type, dconst1), arg1);
7100 if (TREE_CODE (arg1) == REAL_CST
7101 && ! TREE_CONSTANT_OVERFLOW (arg1))
7104 c = TREE_REAL_CST (arg1);
7106 /* Optimize pow(x,0.0) = 1.0. */
7107 if (REAL_VALUES_EQUAL (c, dconst0))
7108 return omit_one_operand (type, build_real (type, dconst1),
7111 /* Optimize pow(x,1.0) = x. */
7112 if (REAL_VALUES_EQUAL (c, dconst1))
7115 /* Optimize pow(x,-1.0) = 1.0/x. */
7116 if (REAL_VALUES_EQUAL (c, dconstm1))
7117 return fold (build (RDIV_EXPR, type,
7118 build_real (type, dconst1),
7121 /* Optimize pow(x,0.5) = sqrt(x). */
7122 if (flag_unsafe_math_optimizations
7123 && REAL_VALUES_EQUAL (c, dconsthalf))
7125 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7127 if (sqrtfn != NULL_TREE)
7129 tree arglist = build_tree_list (NULL_TREE, arg0);
7130 return build_function_call_expr (sqrtfn, arglist);
7134 /* Attempt to evaluate pow at compile-time. */
7135 if (TREE_CODE (arg0) == REAL_CST
7136 && ! TREE_CONSTANT_OVERFLOW (arg0))
7138 REAL_VALUE_TYPE cint;
7141 n = real_to_integer (&c);
7142 real_from_integer (&cint, VOIDmode, n,
7144 if (real_identical (&c, &cint))
7149 x = TREE_REAL_CST (arg0);
7150 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7151 if (flag_unsafe_math_optimizations || !inexact)
7152 return build_real (type, x);
7157 /* Optimize pow(expN(x),y) = expN(x*y). */
7158 fcode = builtin_mathfn_code (arg0);
7159 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7161 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7162 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7163 arg = fold (build (MULT_EXPR, type, arg, arg1));
7164 arglist = build_tree_list (NULL_TREE, arg);
7165 return build_function_call_expr (expfn, arglist);
7168 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7169 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
7171 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7172 tree narg1 = fold (build (MULT_EXPR, type, arg1,
7173 build_real (type, dconsthalf)));
7175 arglist = tree_cons (NULL_TREE, narg0,
7176 build_tree_list (NULL_TREE, narg1));
7177 return build_function_call_expr (fndecl, arglist);
7180 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7181 if (flag_unsafe_math_optimizations
7182 && (fcode == BUILT_IN_POW
7183 || fcode == BUILT_IN_POWF
7184 || fcode == BUILT_IN_POWL))
7186 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7187 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7188 tree narg1 = fold (build (MULT_EXPR, type, arg01, arg1));
7189 arglist = tree_cons (NULL_TREE, arg00,
7190 build_tree_list (NULL_TREE, narg1));
7191 return build_function_call_expr (fndecl, arglist);
7199 return fold_builtin_inf (type, true);
7201 case BUILT_IN_HUGE_VAL:
7202 case BUILT_IN_HUGE_VALF:
7203 case BUILT_IN_HUGE_VALL:
7204 return fold_builtin_inf (type, false);
7209 return fold_builtin_nan (arglist, type, true);
7212 case BUILT_IN_NANSF:
7213 case BUILT_IN_NANSL:
7214 return fold_builtin_nan (arglist, type, false);
7216 case BUILT_IN_FLOOR:
7217 case BUILT_IN_FLOORF:
7218 case BUILT_IN_FLOORL:
7219 return fold_builtin_floor (exp);
7222 case BUILT_IN_CEILF:
7223 case BUILT_IN_CEILL:
7224 return fold_builtin_ceil (exp);
7226 case BUILT_IN_TRUNC:
7227 case BUILT_IN_TRUNCF:
7228 case BUILT_IN_TRUNCL:
7229 return fold_builtin_trunc (exp);
7231 case BUILT_IN_ROUND:
7232 case BUILT_IN_ROUNDF:
7233 case BUILT_IN_ROUNDL:
7234 return fold_builtin_round (exp);
7236 case BUILT_IN_NEARBYINT:
7237 case BUILT_IN_NEARBYINTF:
7238 case BUILT_IN_NEARBYINTL:
7240 case BUILT_IN_RINTF:
7241 case BUILT_IN_RINTL:
7242 return fold_trunc_transparent_mathfn (exp);
7246 case BUILT_IN_FFSLL:
7249 case BUILT_IN_CLZLL:
7252 case BUILT_IN_CTZLL:
7253 case BUILT_IN_POPCOUNT:
7254 case BUILT_IN_POPCOUNTL:
7255 case BUILT_IN_POPCOUNTLL:
7256 case BUILT_IN_PARITY:
7257 case BUILT_IN_PARITYL:
7258 case BUILT_IN_PARITYLL:
7259 return fold_builtin_bitop (exp);
7261 case BUILT_IN_MEMCPY:
7262 return fold_builtin_memcpy (exp);
7264 case BUILT_IN_MEMPCPY:
7265 return fold_builtin_mempcpy (exp);
7267 case BUILT_IN_MEMMOVE:
7268 return fold_builtin_memmove (exp);
7270 case BUILT_IN_STRCPY:
7271 return fold_builtin_strcpy (exp);
7273 case BUILT_IN_STRNCPY:
7274 return fold_builtin_strncpy (exp);
7276 case BUILT_IN_MEMCMP:
7277 return fold_builtin_memcmp (exp);
7279 case BUILT_IN_STRCMP:
7280 return fold_builtin_strcmp (exp);
7282 case BUILT_IN_STRNCMP:
7283 return fold_builtin_strncmp (exp);
7285 case BUILT_IN_SIGNBIT:
7286 case BUILT_IN_SIGNBITF:
7287 case BUILT_IN_SIGNBITL:
7288 return fold_builtin_signbit (exp);
7290 case BUILT_IN_ISASCII:
7291 return fold_builtin_isascii (arglist);
7293 case BUILT_IN_TOASCII:
7294 return fold_builtin_toascii (arglist);
7296 case BUILT_IN_ISDIGIT:
7297 return fold_builtin_isdigit (arglist);
7306 /* Conveniently construct a function call expression. */
7309 build_function_call_expr (tree fn, tree arglist)
7313 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
7314 call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
7315 call_expr, arglist);
7316 return fold (call_expr);
7319 /* This function validates the types of a function call argument list
7320 represented as a tree chain of parameters against a specified list
7321 of tree_codes. If the last specifier is a 0, that represents an
7322 ellipses, otherwise the last specifier must be a VOID_TYPE. */
7325 validate_arglist (tree arglist, ...)
7327 enum tree_code code;
7331 va_start (ap, arglist);
7335 code = va_arg (ap, enum tree_code);
7339 /* This signifies an ellipses, any further arguments are all ok. */
7343 /* This signifies an endlink, if no arguments remain, return
7344 true, otherwise return false. */
7348 /* If no parameters remain or the parameter's code does not
7349 match the specified code, return false. Otherwise continue
7350 checking any remaining arguments. */
7352 || code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
7356 arglist = TREE_CHAIN (arglist);
7360 /* We need gotos here since we can only have one VA_CLOSE in a
7368 /* Default target-specific builtin expander that does nothing. */
7371 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
7372 rtx target ATTRIBUTE_UNUSED,
7373 rtx subtarget ATTRIBUTE_UNUSED,
7374 enum machine_mode mode ATTRIBUTE_UNUSED,
7375 int ignore ATTRIBUTE_UNUSED)
7380 /* Instantiate all remaining CONSTANT_P_RTX nodes. */
7383 purge_builtin_constant_p (void)
7385 rtx insn, set, arg, new, note;
7387 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7389 && (set = single_set (insn)) != NULL_RTX
7390 && (GET_CODE (arg = SET_SRC (set)) == CONSTANT_P_RTX
7391 || (GET_CODE (arg) == SUBREG
7392 && (GET_CODE (arg = SUBREG_REG (arg))
7393 == CONSTANT_P_RTX))))
7395 arg = XEXP (arg, 0);
7396 new = CONSTANT_P (arg) ? const1_rtx : const0_rtx;
7397 validate_change (insn, &SET_SRC (set), new, 0);
7399 /* Remove the REG_EQUAL note from the insn. */
7400 if ((note = find_reg_note (insn, REG_EQUAL, NULL_RTX)) != 0)
7401 remove_note (insn, note);
7405 /* Returns true is EXP represents data that would potentially reside
7406 in a readonly section. */
7409 readonly_data_expr (tree exp)
7413 if (TREE_CODE (exp) == ADDR_EXPR)
7414 return decl_readonly_section (TREE_OPERAND (exp, 0), 0);