1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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
30 #include "hard-reg-set.h"
33 #include "insn-config.h"
39 #include "typeclass.h"
44 #include "langhooks.h"
46 #define CALLED_AS_BUILT_IN(NODE) \
47 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
49 /* Register mappings for target machines without register windows. */
50 #ifndef INCOMING_REGNO
51 #define INCOMING_REGNO(OUT) (OUT)
53 #ifndef OUTGOING_REGNO
54 #define OUTGOING_REGNO(IN) (IN)
57 #ifndef PAD_VARARGS_DOWN
58 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
61 /* Define the names of the builtin function types and codes. */
62 const char *const built_in_class_names[4]
63 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
65 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT) STRINGX(X),
66 const char *const built_in_names[(int) END_BUILTINS] =
68 #include "builtins.def"
72 /* Setup an array of _DECL trees, make sure each element is
73 initialized to NULL_TREE. */
74 tree built_in_decls[(int) END_BUILTINS];
76 static int get_pointer_alignment PARAMS ((tree, unsigned int));
77 static tree c_strlen PARAMS ((tree));
78 static const char *c_getstr PARAMS ((tree));
79 static rtx c_readstr PARAMS ((const char *,
81 static int target_char_cast PARAMS ((tree, char *));
82 static rtx get_memory_rtx PARAMS ((tree));
83 static int apply_args_size PARAMS ((void));
84 static int apply_result_size PARAMS ((void));
85 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
86 static rtx result_vector PARAMS ((int, rtx));
88 static rtx expand_builtin_setjmp PARAMS ((tree, rtx));
89 static void expand_builtin_prefetch PARAMS ((tree));
90 static rtx expand_builtin_apply_args PARAMS ((void));
91 static rtx expand_builtin_apply_args_1 PARAMS ((void));
92 static rtx expand_builtin_apply PARAMS ((rtx, rtx, rtx));
93 static void expand_builtin_return PARAMS ((rtx));
94 static enum type_class type_to_class PARAMS ((tree));
95 static rtx expand_builtin_classify_type PARAMS ((tree));
96 static rtx expand_builtin_mathfn PARAMS ((tree, rtx, rtx));
97 static rtx expand_builtin_constant_p PARAMS ((tree));
98 static rtx expand_builtin_args_info PARAMS ((tree));
99 static rtx expand_builtin_next_arg PARAMS ((tree));
100 static rtx expand_builtin_va_start PARAMS ((tree));
101 static rtx expand_builtin_va_end PARAMS ((tree));
102 static rtx expand_builtin_va_copy PARAMS ((tree));
103 static rtx expand_builtin_memcmp PARAMS ((tree, tree, rtx,
105 static rtx expand_builtin_strcmp PARAMS ((tree, rtx,
107 static rtx expand_builtin_strncmp PARAMS ((tree, rtx,
109 static rtx builtin_memcpy_read_str PARAMS ((PTR, HOST_WIDE_INT,
111 static rtx expand_builtin_strcat PARAMS ((tree, rtx,
113 static rtx expand_builtin_strncat PARAMS ((tree, rtx,
115 static rtx expand_builtin_strspn PARAMS ((tree, rtx,
117 static rtx expand_builtin_strcspn PARAMS ((tree, rtx,
119 static rtx expand_builtin_memcpy PARAMS ((tree, rtx,
121 static rtx expand_builtin_strcpy PARAMS ((tree, rtx,
123 static rtx builtin_strncpy_read_str PARAMS ((PTR, HOST_WIDE_INT,
125 static rtx expand_builtin_strncpy PARAMS ((tree, rtx,
127 static rtx builtin_memset_read_str PARAMS ((PTR, HOST_WIDE_INT,
129 static rtx builtin_memset_gen_str PARAMS ((PTR, HOST_WIDE_INT,
131 static rtx expand_builtin_memset PARAMS ((tree, rtx,
133 static rtx expand_builtin_bzero PARAMS ((tree));
134 static rtx expand_builtin_strlen PARAMS ((tree, rtx));
135 static rtx expand_builtin_strstr PARAMS ((tree, rtx,
137 static rtx expand_builtin_strpbrk PARAMS ((tree, rtx,
139 static rtx expand_builtin_strchr PARAMS ((tree, rtx,
141 static rtx expand_builtin_strrchr PARAMS ((tree, rtx,
143 static rtx expand_builtin_alloca PARAMS ((tree, rtx));
144 static rtx expand_builtin_ffs PARAMS ((tree, rtx, rtx));
145 static rtx expand_builtin_frame_address PARAMS ((tree));
146 static rtx expand_builtin_fputs PARAMS ((tree, int, int));
147 static tree stabilize_va_list PARAMS ((tree, int));
148 static rtx expand_builtin_expect PARAMS ((tree, rtx));
149 static tree fold_builtin_constant_p PARAMS ((tree));
150 static tree fold_builtin_classify_type PARAMS ((tree));
151 static tree fold_builtin_inf PARAMS ((tree, int));
152 static tree build_function_call_expr PARAMS ((tree, tree));
153 static int validate_arglist PARAMS ((tree, ...));
155 /* Return the alignment in bits of EXP, a pointer valued expression.
156 But don't return more than MAX_ALIGN no matter what.
157 The alignment returned is, by default, the alignment of the thing that
158 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
160 Otherwise, look at the expression to see if we can do better, i.e., if the
161 expression is actually pointing at an object whose alignment is tighter. */
164 get_pointer_alignment (exp, max_align)
166 unsigned int max_align;
168 unsigned int align, inner;
170 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
173 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
174 align = MIN (align, max_align);
178 switch (TREE_CODE (exp))
182 case NON_LVALUE_EXPR:
183 exp = TREE_OPERAND (exp, 0);
184 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
187 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
188 align = MIN (inner, max_align);
192 /* If sum of pointer + int, restrict our maximum alignment to that
193 imposed by the integer. If not, we can't do any better than
195 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
198 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
199 & (max_align / BITS_PER_UNIT - 1))
203 exp = TREE_OPERAND (exp, 0);
207 /* See what we are pointing at and look at its alignment. */
208 exp = TREE_OPERAND (exp, 0);
209 if (TREE_CODE (exp) == FUNCTION_DECL)
210 align = FUNCTION_BOUNDARY;
211 else if (DECL_P (exp))
212 align = DECL_ALIGN (exp);
213 #ifdef CONSTANT_ALIGNMENT
214 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c')
215 align = CONSTANT_ALIGNMENT (exp, align);
217 return MIN (align, max_align);
225 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
226 way, because it could contain a zero byte in the middle.
227 TREE_STRING_LENGTH is the size of the character array, not the string.
229 The value returned is of type `ssizetype'.
231 Unfortunately, string_constant can't access the values of const char
232 arrays with initializers, so neither can we do so here. */
239 HOST_WIDE_INT offset;
243 src = string_constant (src, &offset_node);
247 max = TREE_STRING_LENGTH (src) - 1;
248 ptr = TREE_STRING_POINTER (src);
250 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
252 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
253 compute the offset to the following null if we don't know where to
254 start searching for it. */
257 for (i = 0; i < max; i++)
261 /* We don't know the starting offset, but we do know that the string
262 has no internal zero bytes. We can assume that the offset falls
263 within the bounds of the string; otherwise, the programmer deserves
264 what he gets. Subtract the offset from the length of the string,
265 and return that. This would perhaps not be valid if we were dealing
266 with named arrays in addition to literal string constants. */
268 return size_diffop (size_int (max), offset_node);
271 /* We have a known offset into the string. Start searching there for
272 a null character if we can represent it as a single HOST_WIDE_INT. */
273 if (offset_node == 0)
275 else if (! host_integerp (offset_node, 0))
278 offset = tree_low_cst (offset_node, 0);
280 /* If the offset is known to be out of bounds, warn, and call strlen at
282 if (offset < 0 || offset > max)
284 warning ("offset outside bounds of constant string");
288 /* Use strlen to search for the first zero byte. Since any strings
289 constructed with build_string will have nulls appended, we win even
290 if we get handed something like (char[4])"abcd".
292 Since OFFSET is our starting index into the string, no further
293 calculation is needed. */
294 return ssize_int (strlen (ptr + offset));
297 /* Return a char pointer for a C string if it is a string constant
298 or sum of string constant and integer constant. */
306 src = string_constant (src, &offset_node);
310 if (offset_node == 0)
311 return TREE_STRING_POINTER (src);
312 else if (!host_integerp (offset_node, 1)
313 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
316 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
319 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
320 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
323 c_readstr (str, mode)
325 enum machine_mode mode;
331 if (GET_MODE_CLASS (mode) != MODE_INT)
336 for (i = 0; i < GET_MODE_SIZE (mode); i++)
339 if (WORDS_BIG_ENDIAN)
340 j = GET_MODE_SIZE (mode) - i - 1;
341 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
342 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
343 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
345 if (j > 2 * HOST_BITS_PER_WIDE_INT)
348 ch = (unsigned char) str[i];
349 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
351 return immed_double_const (c[0], c[1], mode);
354 /* Cast a target constant CST to target CHAR and if that value fits into
355 host char type, return zero and put that value into variable pointed by
359 target_char_cast (cst, p)
363 unsigned HOST_WIDE_INT val, hostval;
365 if (!host_integerp (cst, 1)
366 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
369 val = tree_low_cst (cst, 1);
370 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
371 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
374 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
375 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
384 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
385 times to get the address of either a higher stack frame, or a return
386 address located within it (depending on FNDECL_CODE). */
389 expand_builtin_return_addr (fndecl_code, count, tem)
390 enum built_in_function fndecl_code;
396 /* Some machines need special handling before we can access
397 arbitrary frames. For example, on the sparc, we must first flush
398 all register windows to the stack. */
399 #ifdef SETUP_FRAME_ADDRESSES
401 SETUP_FRAME_ADDRESSES ();
404 /* On the sparc, the return address is not in the frame, it is in a
405 register. There is no way to access it off of the current frame
406 pointer, but it can be accessed off the previous frame pointer by
407 reading the value from the register window save area. */
408 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
409 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
413 /* Scan back COUNT frames to the specified frame. */
414 for (i = 0; i < count; i++)
416 /* Assume the dynamic chain pointer is in the word that the
417 frame address points to, unless otherwise specified. */
418 #ifdef DYNAMIC_CHAIN_ADDRESS
419 tem = DYNAMIC_CHAIN_ADDRESS (tem);
421 tem = memory_address (Pmode, tem);
422 tem = gen_rtx_MEM (Pmode, tem);
423 set_mem_alias_set (tem, get_frame_alias_set ());
424 tem = copy_to_reg (tem);
427 /* For __builtin_frame_address, return what we've got. */
428 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
431 /* For __builtin_return_address, Get the return address from that
433 #ifdef RETURN_ADDR_RTX
434 tem = RETURN_ADDR_RTX (count, tem);
436 tem = memory_address (Pmode,
437 plus_constant (tem, GET_MODE_SIZE (Pmode)));
438 tem = gen_rtx_MEM (Pmode, tem);
439 set_mem_alias_set (tem, get_frame_alias_set ());
444 /* Alias set used for setjmp buffer. */
445 static HOST_WIDE_INT setjmp_alias_set = -1;
447 /* Construct the leading half of a __builtin_setjmp call. Control will
448 return to RECEIVER_LABEL. This is used directly by sjlj exception
452 expand_builtin_setjmp_setup (buf_addr, receiver_label)
456 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
460 if (setjmp_alias_set == -1)
461 setjmp_alias_set = new_alias_set ();
463 #ifdef POINTERS_EXTEND_UNSIGNED
464 if (GET_MODE (buf_addr) != Pmode)
465 buf_addr = convert_memory_address (Pmode, buf_addr);
468 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
472 /* We store the frame pointer and the address of receiver_label in
473 the buffer and use the rest of it for the stack save area, which
474 is machine-dependent. */
476 #ifndef BUILTIN_SETJMP_FRAME_VALUE
477 #define BUILTIN_SETJMP_FRAME_VALUE virtual_stack_vars_rtx
480 mem = gen_rtx_MEM (Pmode, buf_addr);
481 set_mem_alias_set (mem, setjmp_alias_set);
482 emit_move_insn (mem, BUILTIN_SETJMP_FRAME_VALUE);
484 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
485 set_mem_alias_set (mem, setjmp_alias_set);
487 emit_move_insn (validize_mem (mem),
488 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
490 stack_save = gen_rtx_MEM (sa_mode,
491 plus_constant (buf_addr,
492 2 * GET_MODE_SIZE (Pmode)));
493 set_mem_alias_set (stack_save, setjmp_alias_set);
494 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
496 /* If there is further processing to do, do it. */
497 #ifdef HAVE_builtin_setjmp_setup
498 if (HAVE_builtin_setjmp_setup)
499 emit_insn (gen_builtin_setjmp_setup (buf_addr));
502 /* Tell optimize_save_area_alloca that extra work is going to
503 need to go on during alloca. */
504 current_function_calls_setjmp = 1;
506 /* Set this so all the registers get saved in our frame; we need to be
507 able to copy the saved values for any registers from frames we unwind. */
508 current_function_has_nonlocal_label = 1;
511 /* Construct the trailing part of a __builtin_setjmp call.
512 This is used directly by sjlj exception handling code. */
515 expand_builtin_setjmp_receiver (receiver_label)
516 rtx receiver_label ATTRIBUTE_UNUSED;
518 /* Clobber the FP when we get here, so we have to make sure it's
519 marked as used by this function. */
520 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
522 /* Mark the static chain as clobbered here so life information
523 doesn't get messed up for it. */
524 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
526 /* Now put in the code to restore the frame pointer, and argument
527 pointer, if needed. The code below is from expand_end_bindings
528 in stmt.c; see detailed documentation there. */
529 #ifdef HAVE_nonlocal_goto
530 if (! HAVE_nonlocal_goto)
532 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
534 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
535 if (fixed_regs[ARG_POINTER_REGNUM])
537 #ifdef ELIMINABLE_REGS
539 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
541 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
542 if (elim_regs[i].from == ARG_POINTER_REGNUM
543 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
546 if (i == ARRAY_SIZE (elim_regs))
549 /* Now restore our arg pointer from the address at which it
550 was saved in our stack frame. */
551 emit_move_insn (virtual_incoming_args_rtx,
552 copy_to_reg (get_arg_pointer_save_area (cfun)));
557 #ifdef HAVE_builtin_setjmp_receiver
558 if (HAVE_builtin_setjmp_receiver)
559 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
562 #ifdef HAVE_nonlocal_goto_receiver
563 if (HAVE_nonlocal_goto_receiver)
564 emit_insn (gen_nonlocal_goto_receiver ());
569 /* @@@ This is a kludge. Not all machine descriptions define a blockage
570 insn, but we must not allow the code we just generated to be reordered
571 by scheduling. Specifically, the update of the frame pointer must
572 happen immediately, not later. So emit an ASM_INPUT to act as blockage
574 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
577 /* __builtin_setjmp is passed a pointer to an array of five words (not
578 all will be used on all machines). It operates similarly to the C
579 library function of the same name, but is more efficient. Much of
580 the code below (and for longjmp) is copied from the handling of
583 NOTE: This is intended for use by GNAT and the exception handling
584 scheme in the compiler and will only work in the method used by
588 expand_builtin_setjmp (arglist, target)
592 rtx buf_addr, next_lab, cont_lab;
594 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
597 if (target == 0 || GET_CODE (target) != REG
598 || REGNO (target) < FIRST_PSEUDO_REGISTER)
599 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
601 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
603 next_lab = gen_label_rtx ();
604 cont_lab = gen_label_rtx ();
606 expand_builtin_setjmp_setup (buf_addr, next_lab);
608 /* Set TARGET to zero and branch to the continue label. */
609 emit_move_insn (target, const0_rtx);
610 emit_jump_insn (gen_jump (cont_lab));
612 emit_label (next_lab);
614 expand_builtin_setjmp_receiver (next_lab);
616 /* Set TARGET to one. */
617 emit_move_insn (target, const1_rtx);
618 emit_label (cont_lab);
620 /* Tell flow about the strange goings on. Putting `next_lab' on
621 `nonlocal_goto_handler_labels' to indicates that function
622 calls may traverse the arc back to this label. */
624 current_function_has_nonlocal_label = 1;
625 nonlocal_goto_handler_labels
626 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
631 /* __builtin_longjmp is passed a pointer to an array of five words (not
632 all will be used on all machines). It operates similarly to the C
633 library function of the same name, but is more efficient. Much of
634 the code below is copied from the handling of non-local gotos.
636 NOTE: This is intended for use by GNAT and the exception handling
637 scheme in the compiler and will only work in the method used by
641 expand_builtin_longjmp (buf_addr, value)
644 rtx fp, lab, stack, insn, last;
645 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
647 if (setjmp_alias_set == -1)
648 setjmp_alias_set = new_alias_set ();
650 #ifdef POINTERS_EXTEND_UNSIGNED
651 if (GET_MODE (buf_addr) != Pmode)
652 buf_addr = convert_memory_address (Pmode, buf_addr);
655 buf_addr = force_reg (Pmode, buf_addr);
657 /* We used to store value in static_chain_rtx, but that fails if pointers
658 are smaller than integers. We instead require that the user must pass
659 a second argument of 1, because that is what builtin_setjmp will
660 return. This also makes EH slightly more efficient, since we are no
661 longer copying around a value that we don't care about. */
662 if (value != const1_rtx)
665 current_function_calls_longjmp = 1;
667 last = get_last_insn ();
668 #ifdef HAVE_builtin_longjmp
669 if (HAVE_builtin_longjmp)
670 emit_insn (gen_builtin_longjmp (buf_addr));
674 fp = gen_rtx_MEM (Pmode, buf_addr);
675 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
676 GET_MODE_SIZE (Pmode)));
678 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
679 2 * GET_MODE_SIZE (Pmode)));
680 set_mem_alias_set (fp, setjmp_alias_set);
681 set_mem_alias_set (lab, setjmp_alias_set);
682 set_mem_alias_set (stack, setjmp_alias_set);
684 /* Pick up FP, label, and SP from the block and jump. This code is
685 from expand_goto in stmt.c; see there for detailed comments. */
686 #if HAVE_nonlocal_goto
687 if (HAVE_nonlocal_goto)
688 /* We have to pass a value to the nonlocal_goto pattern that will
689 get copied into the static_chain pointer, but it does not matter
690 what that value is, because builtin_setjmp does not use it. */
691 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
695 lab = copy_to_reg (lab);
697 emit_move_insn (hard_frame_pointer_rtx, fp);
698 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
700 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
701 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
702 emit_indirect_jump (lab);
706 /* Search backwards and mark the jump insn as a non-local goto.
707 Note that this precludes the use of __builtin_longjmp to a
708 __builtin_setjmp target in the same function. However, we've
709 already cautioned the user that these functions are for
710 internal exception handling use only. */
711 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
715 if (GET_CODE (insn) == JUMP_INSN)
717 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
721 else if (GET_CODE (insn) == CALL_INSN)
726 /* Expand a call to __builtin_prefetch. For a target that does not support
727 data prefetch, evaluate the memory address argument in case it has side
731 expand_builtin_prefetch (arglist)
734 tree arg0, arg1, arg2;
737 if (!validate_arglist (arglist, POINTER_TYPE, 0))
740 arg0 = TREE_VALUE (arglist);
741 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
742 zero (read) and argument 2 (locality) defaults to 3 (high degree of
744 if (TREE_CHAIN (arglist))
746 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
747 if (TREE_CHAIN (TREE_CHAIN (arglist)))
748 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
750 arg2 = build_int_2 (3, 0);
754 arg1 = integer_zero_node;
755 arg2 = build_int_2 (3, 0);
758 /* Argument 0 is an address. */
759 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
761 /* Argument 1 (read/write flag) must be a compile-time constant int. */
762 if (TREE_CODE (arg1) != INTEGER_CST)
764 error ("second arg to `__builtin_prefetch' must be a constant");
765 arg1 = integer_zero_node;
767 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
768 /* Argument 1 must be either zero or one. */
769 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
771 warning ("invalid second arg to __builtin_prefetch; using zero");
775 /* Argument 2 (locality) must be a compile-time constant int. */
776 if (TREE_CODE (arg2) != INTEGER_CST)
778 error ("third arg to `__builtin_prefetch' must be a constant");
779 arg2 = integer_zero_node;
781 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
782 /* Argument 2 must be 0, 1, 2, or 3. */
783 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
785 warning ("invalid third arg to __builtin_prefetch; using zero");
792 if ((! (*insn_data[(int)CODE_FOR_prefetch].operand[0].predicate)
794 insn_data[(int)CODE_FOR_prefetch].operand[0].mode)) ||
795 (GET_MODE(op0) != Pmode))
797 #ifdef POINTERS_EXTEND_UNSIGNED
798 if (GET_MODE(op0) != Pmode)
799 op0 = convert_memory_address (Pmode, op0);
801 op0 = force_reg (Pmode, op0);
803 emit_insn (gen_prefetch (op0, op1, op2));
807 op0 = protect_from_queue (op0, 0);
808 /* Don't do anything with direct references to volatile memory, but
809 generate code to handle other side effects. */
810 if (GET_CODE (op0) != MEM && side_effects_p (op0))
814 /* Get a MEM rtx for expression EXP which is the address of an operand
815 to be used to be used in a string instruction (cmpstrsi, movstrsi, ..). */
821 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM);
824 #ifdef POINTERS_EXTEND_UNSIGNED
825 if (GET_MODE (addr) != Pmode)
826 addr = convert_memory_address (Pmode, addr);
829 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
831 /* Get an expression we can use to find the attributes to assign to MEM.
832 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
833 we can. First remove any nops. */
834 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
835 || TREE_CODE (exp) == NON_LVALUE_EXPR)
836 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
837 exp = TREE_OPERAND (exp, 0);
839 if (TREE_CODE (exp) == ADDR_EXPR)
841 exp = TREE_OPERAND (exp, 0);
842 set_mem_attributes (mem, exp, 0);
844 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
846 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
847 /* memcpy, memset and other builtin stringops can alias with anything. */
848 set_mem_alias_set (mem, 0);
854 /* Built-in functions to perform an untyped call and return. */
856 /* For each register that may be used for calling a function, this
857 gives a mode used to copy the register's value. VOIDmode indicates
858 the register is not used for calling a function. If the machine
859 has register windows, this gives only the outbound registers.
860 INCOMING_REGNO gives the corresponding inbound register. */
861 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
863 /* For each register that may be used for returning values, this gives
864 a mode used to copy the register's value. VOIDmode indicates the
865 register is not used for returning values. If the machine has
866 register windows, this gives only the outbound registers.
867 INCOMING_REGNO gives the corresponding inbound register. */
868 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
870 /* For each register that may be used for calling a function, this
871 gives the offset of that register into the block returned by
872 __builtin_apply_args. 0 indicates that the register is not
873 used for calling a function. */
874 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
876 /* Return the offset of register REGNO into the block returned by
877 __builtin_apply_args. This is not declared static, since it is
878 needed in objc-act.c. */
881 apply_args_register_offset (regno)
886 /* Arguments are always put in outgoing registers (in the argument
887 block) if such make sense. */
888 #ifdef OUTGOING_REGNO
889 regno = OUTGOING_REGNO(regno);
891 return apply_args_reg_offset[regno];
894 /* Return the size required for the block returned by __builtin_apply_args,
895 and initialize apply_args_mode. */
900 static int size = -1;
903 enum machine_mode mode;
905 /* The values computed by this function never change. */
908 /* The first value is the incoming arg-pointer. */
909 size = GET_MODE_SIZE (Pmode);
911 /* The second value is the structure value address unless this is
912 passed as an "invisible" first argument. */
913 if (struct_value_rtx)
914 size += GET_MODE_SIZE (Pmode);
916 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
917 if (FUNCTION_ARG_REGNO_P (regno))
919 /* Search for the proper mode for copying this register's
920 value. I'm not sure this is right, but it works so far. */
921 enum machine_mode best_mode = VOIDmode;
923 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
925 mode = GET_MODE_WIDER_MODE (mode))
926 if (HARD_REGNO_MODE_OK (regno, mode)
927 && HARD_REGNO_NREGS (regno, mode) == 1)
930 if (best_mode == VOIDmode)
931 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
933 mode = GET_MODE_WIDER_MODE (mode))
934 if (HARD_REGNO_MODE_OK (regno, mode)
935 && have_insn_for (SET, mode))
938 if (best_mode == VOIDmode)
939 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
941 mode = GET_MODE_WIDER_MODE (mode))
942 if (HARD_REGNO_MODE_OK (regno, mode)
943 && have_insn_for (SET, mode))
946 if (best_mode == VOIDmode)
947 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
949 mode = GET_MODE_WIDER_MODE (mode))
950 if (HARD_REGNO_MODE_OK (regno, mode)
951 && have_insn_for (SET, mode))
955 if (mode == VOIDmode)
958 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
959 if (size % align != 0)
960 size = CEIL (size, align) * align;
961 apply_args_reg_offset[regno] = size;
962 size += GET_MODE_SIZE (mode);
963 apply_args_mode[regno] = mode;
967 apply_args_mode[regno] = VOIDmode;
968 apply_args_reg_offset[regno] = 0;
974 /* Return the size required for the block returned by __builtin_apply,
975 and initialize apply_result_mode. */
980 static int size = -1;
982 enum machine_mode mode;
984 /* The values computed by this function never change. */
989 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
990 if (FUNCTION_VALUE_REGNO_P (regno))
992 /* Search for the proper mode for copying this register's
993 value. I'm not sure this is right, but it works so far. */
994 enum machine_mode best_mode = VOIDmode;
996 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
998 mode = GET_MODE_WIDER_MODE (mode))
999 if (HARD_REGNO_MODE_OK (regno, mode))
1002 if (best_mode == VOIDmode)
1003 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
1005 mode = GET_MODE_WIDER_MODE (mode))
1006 if (HARD_REGNO_MODE_OK (regno, mode)
1007 && have_insn_for (SET, mode))
1010 if (best_mode == VOIDmode)
1011 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
1013 mode = GET_MODE_WIDER_MODE (mode))
1014 if (HARD_REGNO_MODE_OK (regno, mode)
1015 && have_insn_for (SET, mode))
1018 if (best_mode == VOIDmode)
1019 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
1021 mode = GET_MODE_WIDER_MODE (mode))
1022 if (HARD_REGNO_MODE_OK (regno, mode)
1023 && have_insn_for (SET, mode))
1027 if (mode == VOIDmode)
1030 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1031 if (size % align != 0)
1032 size = CEIL (size, align) * align;
1033 size += GET_MODE_SIZE (mode);
1034 apply_result_mode[regno] = mode;
1037 apply_result_mode[regno] = VOIDmode;
1039 /* Allow targets that use untyped_call and untyped_return to override
1040 the size so that machine-specific information can be stored here. */
1041 #ifdef APPLY_RESULT_SIZE
1042 size = APPLY_RESULT_SIZE;
1048 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1049 /* Create a vector describing the result block RESULT. If SAVEP is true,
1050 the result block is used to save the values; otherwise it is used to
1051 restore the values. */
1054 result_vector (savep, result)
1058 int regno, size, align, nelts;
1059 enum machine_mode mode;
1061 rtx *savevec = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1064 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1065 if ((mode = apply_result_mode[regno]) != VOIDmode)
1067 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1068 if (size % align != 0)
1069 size = CEIL (size, align) * align;
1070 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1071 mem = adjust_address (result, mode, size);
1072 savevec[nelts++] = (savep
1073 ? gen_rtx_SET (VOIDmode, mem, reg)
1074 : gen_rtx_SET (VOIDmode, reg, mem));
1075 size += GET_MODE_SIZE (mode);
1077 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1079 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1081 /* Save the state required to perform an untyped call with the same
1082 arguments as were passed to the current function. */
1085 expand_builtin_apply_args_1 ()
1088 int size, align, regno;
1089 enum machine_mode mode;
1091 /* Create a block where the arg-pointer, structure value address,
1092 and argument registers can be saved. */
1093 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1095 /* Walk past the arg-pointer and structure value address. */
1096 size = GET_MODE_SIZE (Pmode);
1097 if (struct_value_rtx)
1098 size += GET_MODE_SIZE (Pmode);
1100 /* Save each register used in calling a function to the block. */
1101 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1102 if ((mode = apply_args_mode[regno]) != VOIDmode)
1106 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1107 if (size % align != 0)
1108 size = CEIL (size, align) * align;
1110 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1112 emit_move_insn (adjust_address (registers, mode, size), tem);
1113 size += GET_MODE_SIZE (mode);
1116 /* Save the arg pointer to the block. */
1117 emit_move_insn (adjust_address (registers, Pmode, 0),
1118 copy_to_reg (virtual_incoming_args_rtx));
1119 size = GET_MODE_SIZE (Pmode);
1121 /* Save the structure value address unless this is passed as an
1122 "invisible" first argument. */
1123 if (struct_value_incoming_rtx)
1125 emit_move_insn (adjust_address (registers, Pmode, size),
1126 copy_to_reg (struct_value_incoming_rtx));
1127 size += GET_MODE_SIZE (Pmode);
1130 /* Return the address of the block. */
1131 return copy_addr_to_reg (XEXP (registers, 0));
1134 /* __builtin_apply_args returns block of memory allocated on
1135 the stack into which is stored the arg pointer, structure
1136 value address, static chain, and all the registers that might
1137 possibly be used in performing a function call. The code is
1138 moved to the start of the function so the incoming values are
1142 expand_builtin_apply_args ()
1144 /* Don't do __builtin_apply_args more than once in a function.
1145 Save the result of the first call and reuse it. */
1146 if (apply_args_value != 0)
1147 return apply_args_value;
1149 /* When this function is called, it means that registers must be
1150 saved on entry to this function. So we migrate the
1151 call to the first insn of this function. */
1156 temp = expand_builtin_apply_args_1 ();
1160 apply_args_value = temp;
1162 /* Put the insns after the NOTE that starts the function.
1163 If this is inside a start_sequence, make the outer-level insn
1164 chain current, so the code is placed at the start of the
1166 push_topmost_sequence ();
1167 emit_insn_before (seq, NEXT_INSN (get_insns ()));
1168 pop_topmost_sequence ();
1173 /* Perform an untyped call and save the state required to perform an
1174 untyped return of whatever value was returned by the given function. */
1177 expand_builtin_apply (function, arguments, argsize)
1178 rtx function, arguments, argsize;
1180 int size, align, regno;
1181 enum machine_mode mode;
1182 rtx incoming_args, result, reg, dest, src, call_insn;
1183 rtx old_stack_level = 0;
1184 rtx call_fusage = 0;
1186 #ifdef POINTERS_EXTEND_UNSIGNED
1187 if (GET_MODE (arguments) != Pmode)
1188 arguments = convert_memory_address (Pmode, arguments);
1191 /* Create a block where the return registers can be saved. */
1192 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1194 /* Fetch the arg pointer from the ARGUMENTS block. */
1195 incoming_args = gen_reg_rtx (Pmode);
1196 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1197 #ifndef STACK_GROWS_DOWNWARD
1198 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1199 incoming_args, 0, OPTAB_LIB_WIDEN);
1202 /* Perform postincrements before actually calling the function. */
1205 /* Push a new argument block and copy the arguments. Do not allow
1206 the (potential) memcpy call below to interfere with our stack
1208 do_pending_stack_adjust ();
1211 /* Save the stack with nonlocal if available */
1212 #ifdef HAVE_save_stack_nonlocal
1213 if (HAVE_save_stack_nonlocal)
1214 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1217 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1219 /* Push a block of memory onto the stack to store the memory arguments.
1220 Save the address in a register, and copy the memory arguments. ??? I
1221 haven't figured out how the calling convention macros effect this,
1222 but it's likely that the source and/or destination addresses in
1223 the block copy will need updating in machine specific ways. */
1224 dest = allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1225 dest = gen_rtx_MEM (BLKmode, dest);
1226 set_mem_align (dest, PARM_BOUNDARY);
1227 src = gen_rtx_MEM (BLKmode, incoming_args);
1228 set_mem_align (src, PARM_BOUNDARY);
1229 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1231 /* Refer to the argument block. */
1233 arguments = gen_rtx_MEM (BLKmode, arguments);
1234 set_mem_align (arguments, PARM_BOUNDARY);
1236 /* Walk past the arg-pointer and structure value address. */
1237 size = GET_MODE_SIZE (Pmode);
1238 if (struct_value_rtx)
1239 size += GET_MODE_SIZE (Pmode);
1241 /* Restore each of the registers previously saved. Make USE insns
1242 for each of these registers for use in making the call. */
1243 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1244 if ((mode = apply_args_mode[regno]) != VOIDmode)
1246 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1247 if (size % align != 0)
1248 size = CEIL (size, align) * align;
1249 reg = gen_rtx_REG (mode, regno);
1250 emit_move_insn (reg, adjust_address (arguments, mode, size));
1251 use_reg (&call_fusage, reg);
1252 size += GET_MODE_SIZE (mode);
1255 /* Restore the structure value address unless this is passed as an
1256 "invisible" first argument. */
1257 size = GET_MODE_SIZE (Pmode);
1258 if (struct_value_rtx)
1260 rtx value = gen_reg_rtx (Pmode);
1261 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1262 emit_move_insn (struct_value_rtx, value);
1263 if (GET_CODE (struct_value_rtx) == REG)
1264 use_reg (&call_fusage, struct_value_rtx);
1265 size += GET_MODE_SIZE (Pmode);
1268 /* All arguments and registers used for the call are set up by now! */
1269 function = prepare_call_address (function, NULL_TREE, &call_fusage, 0, 0);
1271 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1272 and we don't want to load it into a register as an optimization,
1273 because prepare_call_address already did it if it should be done. */
1274 if (GET_CODE (function) != SYMBOL_REF)
1275 function = memory_address (FUNCTION_MODE, function);
1277 /* Generate the actual call instruction and save the return value. */
1278 #ifdef HAVE_untyped_call
1279 if (HAVE_untyped_call)
1280 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1281 result, result_vector (1, result)));
1284 #ifdef HAVE_call_value
1285 if (HAVE_call_value)
1289 /* Locate the unique return register. It is not possible to
1290 express a call that sets more than one return register using
1291 call_value; use untyped_call for that. In fact, untyped_call
1292 only needs to save the return registers in the given block. */
1293 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1294 if ((mode = apply_result_mode[regno]) != VOIDmode)
1297 abort (); /* HAVE_untyped_call required. */
1298 valreg = gen_rtx_REG (mode, regno);
1301 emit_call_insn (GEN_CALL_VALUE (valreg,
1302 gen_rtx_MEM (FUNCTION_MODE, function),
1303 const0_rtx, NULL_RTX, const0_rtx));
1305 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1311 /* Find the CALL insn we just emitted. */
1312 for (call_insn = get_last_insn ();
1313 call_insn && GET_CODE (call_insn) != CALL_INSN;
1314 call_insn = PREV_INSN (call_insn))
1320 /* Put the register usage information on the CALL. If there is already
1321 some usage information, put ours at the end. */
1322 if (CALL_INSN_FUNCTION_USAGE (call_insn))
1326 for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
1327 link = XEXP (link, 1))
1330 XEXP (link, 1) = call_fusage;
1333 CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
1335 /* Restore the stack. */
1336 #ifdef HAVE_save_stack_nonlocal
1337 if (HAVE_save_stack_nonlocal)
1338 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1341 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1345 /* Return the address of the result block. */
1346 return copy_addr_to_reg (XEXP (result, 0));
1349 /* Perform an untyped return. */
1352 expand_builtin_return (result)
1355 int size, align, regno;
1356 enum machine_mode mode;
1358 rtx call_fusage = 0;
1360 #ifdef POINTERS_EXTEND_UNSIGNED
1361 if (GET_MODE (result) != Pmode)
1362 result = convert_memory_address (Pmode, result);
1365 apply_result_size ();
1366 result = gen_rtx_MEM (BLKmode, result);
1368 #ifdef HAVE_untyped_return
1369 if (HAVE_untyped_return)
1371 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1377 /* Restore the return value and note that each value is used. */
1379 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1380 if ((mode = apply_result_mode[regno]) != VOIDmode)
1382 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1383 if (size % align != 0)
1384 size = CEIL (size, align) * align;
1385 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1386 emit_move_insn (reg, adjust_address (result, mode, size));
1388 push_to_sequence (call_fusage);
1389 emit_insn (gen_rtx_USE (VOIDmode, reg));
1390 call_fusage = get_insns ();
1392 size += GET_MODE_SIZE (mode);
1395 /* Put the USE insns before the return. */
1396 emit_insn (call_fusage);
1398 /* Return whatever values was restored by jumping directly to the end
1400 expand_null_return ();
1403 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1405 static enum type_class
1406 type_to_class (type)
1409 switch (TREE_CODE (type))
1411 case VOID_TYPE: return void_type_class;
1412 case INTEGER_TYPE: return integer_type_class;
1413 case CHAR_TYPE: return char_type_class;
1414 case ENUMERAL_TYPE: return enumeral_type_class;
1415 case BOOLEAN_TYPE: return boolean_type_class;
1416 case POINTER_TYPE: return pointer_type_class;
1417 case REFERENCE_TYPE: return reference_type_class;
1418 case OFFSET_TYPE: return offset_type_class;
1419 case REAL_TYPE: return real_type_class;
1420 case COMPLEX_TYPE: return complex_type_class;
1421 case FUNCTION_TYPE: return function_type_class;
1422 case METHOD_TYPE: return method_type_class;
1423 case RECORD_TYPE: return record_type_class;
1425 case QUAL_UNION_TYPE: return union_type_class;
1426 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1427 ? string_type_class : array_type_class);
1428 case SET_TYPE: return set_type_class;
1429 case FILE_TYPE: return file_type_class;
1430 case LANG_TYPE: return lang_type_class;
1431 default: return no_type_class;
1435 /* Expand a call to __builtin_classify_type with arguments found in
1439 expand_builtin_classify_type (arglist)
1443 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1444 return GEN_INT (no_type_class);
1447 /* Expand expression EXP, which is a call to __builtin_constant_p. */
1450 expand_builtin_constant_p (exp)
1453 tree arglist = TREE_OPERAND (exp, 1);
1454 enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1459 arglist = TREE_VALUE (arglist);
1461 /* We have taken care of the easy cases during constant folding. This
1462 case is not obvious, so emit (constant_p_rtx (ARGLIST)) and let CSE get a
1463 chance to see if it can deduce whether ARGLIST is constant. */
1465 tmp = expand_expr (arglist, NULL_RTX, VOIDmode, 0);
1466 tmp = gen_rtx_CONSTANT_P_RTX (value_mode, tmp);
1470 /* Expand a call to one of the builtin math functions (sin, cos, or sqrt).
1471 Return 0 if a normal call should be emitted rather than expanding the
1472 function in-line. EXP is the expression that is a call to the builtin
1473 function; if convenient, the result should be placed in TARGET.
1474 SUBTARGET may be used as the target for computing one of EXP's operands. */
1477 expand_builtin_mathfn (exp, target, subtarget)
1479 rtx target, subtarget;
1481 optab builtin_optab;
1483 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1484 tree arglist = TREE_OPERAND (exp, 1);
1485 enum machine_mode argmode;
1487 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1490 /* Stabilize and compute the argument. */
1491 if (TREE_CODE (TREE_VALUE (arglist)) != VAR_DECL
1492 && TREE_CODE (TREE_VALUE (arglist)) != PARM_DECL)
1494 exp = copy_node (exp);
1495 TREE_OPERAND (exp, 1) = arglist;
1496 /* Wrap the computation of the argument in a SAVE_EXPR. That
1497 way, if we need to expand the argument again (as in the
1498 flag_errno_math case below where we cannot directly set
1499 errno), we will not perform side-effects more than once.
1500 Note that here we're mutating the original EXP as well as the
1501 copy; that's the right thing to do in case the original EXP
1502 is expanded later. */
1503 TREE_VALUE (arglist) = save_expr (TREE_VALUE (arglist));
1504 arglist = copy_node (arglist);
1506 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
1508 /* Make a suitable register to place result in. */
1509 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
1514 switch (DECL_FUNCTION_CODE (fndecl))
1519 builtin_optab = sin_optab; break;
1523 builtin_optab = cos_optab; break;
1525 case BUILT_IN_SQRTF:
1526 case BUILT_IN_SQRTL:
1527 builtin_optab = sqrt_optab; break;
1531 builtin_optab = exp_optab; break;
1535 builtin_optab = log_optab; break;
1540 /* Compute into TARGET.
1541 Set TARGET to wherever the result comes back. */
1542 argmode = TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist)));
1543 target = expand_unop (argmode, builtin_optab, op0, target, 0);
1545 /* If we were unable to expand via the builtin, stop the
1546 sequence (without outputting the insns) and return 0, causing
1547 a call to the library function. */
1554 /* If errno must be maintained, we must set it to EDOM for NaN results. */
1556 if (flag_errno_math && HONOR_NANS (argmode))
1560 lab1 = gen_label_rtx ();
1562 /* Test the result; if it is NaN, set errno=EDOM because
1563 the argument was not in the domain. */
1564 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1569 #ifdef GEN_ERRNO_RTX
1570 rtx errno_rtx = GEN_ERRNO_RTX;
1573 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1576 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1579 /* We can't set errno=EDOM directly; let the library call do it.
1580 Pop the arguments right away in case the call gets deleted. */
1582 expand_call (exp, target, 0);
1589 /* Output the entire sequence. */
1590 insns = get_insns ();
1597 /* Expand expression EXP which is a call to the strlen builtin. Return 0
1598 if we failed the caller should emit a normal call, otherwise
1599 try to get the result in TARGET, if convenient. */
1602 expand_builtin_strlen (exp, target)
1606 tree arglist = TREE_OPERAND (exp, 1);
1607 enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1609 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
1614 tree src = TREE_VALUE (arglist);
1617 = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1619 rtx result, src_reg, char_rtx, before_strlen;
1620 enum machine_mode insn_mode = value_mode, char_mode;
1621 enum insn_code icode = CODE_FOR_nothing;
1623 /* If SRC is not a pointer type, don't do this operation inline. */
1627 /* Bail out if we can't compute strlen in the right mode. */
1628 while (insn_mode != VOIDmode)
1630 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
1631 if (icode != CODE_FOR_nothing)
1634 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
1636 if (insn_mode == VOIDmode)
1639 /* Make a place to write the result of the instruction. */
1642 && GET_CODE (result) == REG
1643 && GET_MODE (result) == insn_mode
1644 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
1645 result = gen_reg_rtx (insn_mode);
1647 /* Make a place to hold the source address. We will not expand
1648 the actual source until we are sure that the expansion will
1649 not fail -- there are trees that cannot be expanded twice. */
1650 src_reg = gen_reg_rtx (Pmode);
1652 /* Mark the beginning of the strlen sequence so we can emit the
1653 source operand later. */
1654 before_strlen = get_last_insn();
1656 char_rtx = const0_rtx;
1657 char_mode = insn_data[(int) icode].operand[2].mode;
1658 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
1660 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
1662 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
1663 char_rtx, GEN_INT (align));
1668 /* Now that we are assured of success, expand the source. */
1670 pat = memory_address (BLKmode,
1671 expand_expr (src, src_reg, ptr_mode, EXPAND_SUM));
1673 emit_move_insn (src_reg, pat);
1678 emit_insn_after (pat, before_strlen);
1680 emit_insn_before (pat, get_insns ());
1682 /* Return the value in the proper mode for this function. */
1683 if (GET_MODE (result) == value_mode)
1685 else if (target != 0)
1686 convert_move (target, result, 0);
1688 target = convert_to_mode (value_mode, result, 0);
1694 /* Expand a call to the strstr builtin. Return 0 if we failed the
1695 caller should emit a normal call, otherwise try to get the result
1696 in TARGET, if convenient (and in mode MODE if that's convenient). */
1699 expand_builtin_strstr (arglist, target, mode)
1702 enum machine_mode mode;
1704 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1708 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1710 const char *p1, *p2;
1719 const char *r = strstr (p1, p2);
1724 /* Return an offset into the constant string argument. */
1725 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1726 s1, ssize_int (r - p1))),
1727 target, mode, EXPAND_NORMAL);
1731 return expand_expr (s1, target, mode, EXPAND_NORMAL);
1736 fn = built_in_decls[BUILT_IN_STRCHR];
1740 /* New argument list transforming strstr(s1, s2) to
1741 strchr(s1, s2[0]). */
1743 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
1744 arglist = tree_cons (NULL_TREE, s1, arglist);
1745 return expand_expr (build_function_call_expr (fn, arglist),
1746 target, mode, EXPAND_NORMAL);
1750 /* Expand a call to the strchr builtin. Return 0 if we failed the
1751 caller should emit a normal call, otherwise try to get the result
1752 in TARGET, if convenient (and in mode MODE if that's convenient). */
1755 expand_builtin_strchr (arglist, target, mode)
1758 enum machine_mode mode;
1760 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
1764 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1767 if (TREE_CODE (s2) != INTEGER_CST)
1776 if (target_char_cast (s2, &c))
1784 /* Return an offset into the constant string argument. */
1785 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1786 s1, ssize_int (r - p1))),
1787 target, mode, EXPAND_NORMAL);
1790 /* FIXME: Should use here strchrM optab so that ports can optimize
1796 /* Expand a call to the strrchr builtin. Return 0 if we failed the
1797 caller should emit a normal call, otherwise try to get the result
1798 in TARGET, if convenient (and in mode MODE if that's convenient). */
1801 expand_builtin_strrchr (arglist, target, mode)
1804 enum machine_mode mode;
1806 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
1810 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1814 if (TREE_CODE (s2) != INTEGER_CST)
1823 if (target_char_cast (s2, &c))
1826 r = strrchr (p1, c);
1831 /* Return an offset into the constant string argument. */
1832 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1833 s1, ssize_int (r - p1))),
1834 target, mode, EXPAND_NORMAL);
1837 if (! integer_zerop (s2))
1840 fn = built_in_decls[BUILT_IN_STRCHR];
1844 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
1845 return expand_expr (build_function_call_expr (fn, arglist),
1846 target, mode, EXPAND_NORMAL);
1850 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
1851 caller should emit a normal call, otherwise try to get the result
1852 in TARGET, if convenient (and in mode MODE if that's convenient). */
1855 expand_builtin_strpbrk (arglist, target, mode)
1858 enum machine_mode mode;
1860 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1864 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1866 const char *p1, *p2;
1875 const char *r = strpbrk (p1, p2);
1880 /* Return an offset into the constant string argument. */
1881 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1882 s1, ssize_int (r - p1))),
1883 target, mode, EXPAND_NORMAL);
1888 /* strpbrk(x, "") == NULL.
1889 Evaluate and ignore the arguments in case they had
1891 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
1896 return 0; /* Really call strpbrk. */
1898 fn = built_in_decls[BUILT_IN_STRCHR];
1902 /* New argument list transforming strpbrk(s1, s2) to
1903 strchr(s1, s2[0]). */
1905 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
1906 arglist = tree_cons (NULL_TREE, s1, arglist);
1907 return expand_expr (build_function_call_expr (fn, arglist),
1908 target, mode, EXPAND_NORMAL);
1912 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
1913 bytes from constant string DATA + OFFSET and return it as target
1917 builtin_memcpy_read_str (data, offset, mode)
1919 HOST_WIDE_INT offset;
1920 enum machine_mode mode;
1922 const char *str = (const char *) data;
1925 || ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
1926 > strlen (str) + 1))
1927 abort (); /* Attempt to read past the end of constant string. */
1929 return c_readstr (str + offset, mode);
1932 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
1933 Return 0 if we failed, the caller should emit a normal call, otherwise
1934 try to get the result in TARGET, if convenient (and in mode MODE if
1935 that's convenient). */
1938 expand_builtin_memcpy (arglist, target, mode)
1941 enum machine_mode mode;
1943 if (!validate_arglist (arglist,
1944 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
1948 tree dest = TREE_VALUE (arglist);
1949 tree src = TREE_VALUE (TREE_CHAIN (arglist));
1950 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1951 const char *src_str;
1953 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
1954 unsigned int dest_align
1955 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
1956 rtx dest_mem, src_mem, dest_addr, len_rtx;
1958 /* If DEST is not a pointer type, call the normal function. */
1959 if (dest_align == 0)
1962 /* If the LEN parameter is zero, return DEST. */
1963 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
1965 /* Evaluate and ignore SRC in case it has side-effects. */
1966 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
1967 return expand_expr (dest, target, mode, EXPAND_NORMAL);
1970 /* If either SRC is not a pointer type, don't do this
1971 operation in-line. */
1975 dest_mem = get_memory_rtx (dest);
1976 set_mem_align (dest_mem, dest_align);
1977 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
1978 src_str = c_getstr (src);
1980 /* If SRC is a string constant and block move would be done
1981 by pieces, we can avoid loading the string from memory
1982 and only stored the computed constants. */
1984 && GET_CODE (len_rtx) == CONST_INT
1985 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
1986 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
1987 (PTR) src_str, dest_align))
1989 store_by_pieces (dest_mem, INTVAL (len_rtx),
1990 builtin_memcpy_read_str,
1991 (PTR) src_str, dest_align);
1992 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
1993 #ifdef POINTERS_EXTEND_UNSIGNED
1994 if (GET_MODE (dest_mem) != ptr_mode)
1995 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2000 src_mem = get_memory_rtx (src);
2001 set_mem_align (src_mem, src_align);
2003 /* Copy word part most expediently. */
2004 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2009 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2010 #ifdef POINTERS_EXTEND_UNSIGNED
2011 if (GET_MODE (dest_addr) != ptr_mode)
2012 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2020 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2021 if we failed the caller should emit a normal call, otherwise try to get
2022 the result in TARGET, if convenient (and in mode MODE if that's
2026 expand_builtin_strcpy (exp, target, mode)
2029 enum machine_mode mode;
2031 tree arglist = TREE_OPERAND (exp, 1);
2034 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2037 fn = built_in_decls[BUILT_IN_MEMCPY];
2041 len = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)));
2045 len = size_binop (PLUS_EXPR, len, ssize_int (1));
2046 chainon (arglist, build_tree_list (NULL_TREE, len));
2047 return expand_expr (build_function_call_expr (fn, arglist),
2048 target, mode, EXPAND_NORMAL);
2051 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2052 bytes from constant string DATA + OFFSET and return it as target
2056 builtin_strncpy_read_str (data, offset, mode)
2058 HOST_WIDE_INT offset;
2059 enum machine_mode mode;
2061 const char *str = (const char *) data;
2063 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
2066 return c_readstr (str + offset, mode);
2069 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
2070 if we failed the caller should emit a normal call. */
2073 expand_builtin_strncpy (arglist, target, mode)
2076 enum machine_mode mode;
2078 if (!validate_arglist (arglist,
2079 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2083 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)));
2084 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2087 /* We must be passed a constant len parameter. */
2088 if (TREE_CODE (len) != INTEGER_CST)
2091 /* If the len parameter is zero, return the dst parameter. */
2092 if (integer_zerop (len))
2094 /* Evaluate and ignore the src argument in case it has
2096 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
2097 VOIDmode, EXPAND_NORMAL);
2098 /* Return the dst parameter. */
2099 return expand_expr (TREE_VALUE (arglist), target, mode,
2103 /* Now, we must be passed a constant src ptr parameter. */
2104 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
2107 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
2109 /* We're required to pad with trailing zeros if the requested
2110 len is greater than strlen(s2)+1. In that case try to
2111 use store_by_pieces, if it fails, punt. */
2112 if (tree_int_cst_lt (slen, len))
2114 tree dest = TREE_VALUE (arglist);
2115 unsigned int dest_align
2116 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2117 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
2120 if (!p || dest_align == 0 || !host_integerp (len, 1)
2121 || !can_store_by_pieces (tree_low_cst (len, 1),
2122 builtin_strncpy_read_str,
2123 (PTR) p, dest_align))
2126 dest_mem = get_memory_rtx (dest);
2127 store_by_pieces (dest_mem, tree_low_cst (len, 1),
2128 builtin_strncpy_read_str,
2129 (PTR) p, dest_align);
2130 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2131 #ifdef POINTERS_EXTEND_UNSIGNED
2132 if (GET_MODE (dest_mem) != ptr_mode)
2133 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2138 /* OK transform into builtin memcpy. */
2139 fn = built_in_decls[BUILT_IN_MEMCPY];
2142 return expand_expr (build_function_call_expr (fn, arglist),
2143 target, mode, EXPAND_NORMAL);
2147 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2148 bytes from constant string DATA + OFFSET and return it as target
2152 builtin_memset_read_str (data, offset, mode)
2154 HOST_WIDE_INT offset ATTRIBUTE_UNUSED;
2155 enum machine_mode mode;
2157 const char *c = (const char *) data;
2158 char *p = alloca (GET_MODE_SIZE (mode));
2160 memset (p, *c, GET_MODE_SIZE (mode));
2162 return c_readstr (p, mode);
2165 /* Callback routine for store_by_pieces. Return the RTL of a register
2166 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
2167 char value given in the RTL register data. For example, if mode is
2168 4 bytes wide, return the RTL for 0x01010101*data. */
2171 builtin_memset_gen_str (data, offset, mode)
2173 HOST_WIDE_INT offset ATTRIBUTE_UNUSED;
2174 enum machine_mode mode;
2180 size = GET_MODE_SIZE (mode);
2185 memset (p, 1, size);
2186 coeff = c_readstr (p, mode);
2188 target = convert_to_mode (mode, (rtx)data, 1);
2189 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
2190 return force_reg (mode, target);
2193 /* Expand expression EXP, which is a call to the memset builtin. Return 0
2194 if we failed the caller should emit a normal call, otherwise try to get
2195 the result in TARGET, if convenient (and in mode MODE if that's
2199 expand_builtin_memset (exp, target, mode)
2202 enum machine_mode mode;
2204 tree arglist = TREE_OPERAND (exp, 1);
2206 if (!validate_arglist (arglist,
2207 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
2211 tree dest = TREE_VALUE (arglist);
2212 tree val = TREE_VALUE (TREE_CHAIN (arglist));
2213 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2216 unsigned int dest_align
2217 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2218 rtx dest_mem, dest_addr, len_rtx;
2220 /* If DEST is not a pointer type, don't do this
2221 operation in-line. */
2222 if (dest_align == 0)
2225 /* If the LEN parameter is zero, return DEST. */
2226 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
2228 /* Evaluate and ignore VAL in case it has side-effects. */
2229 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
2230 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2233 if (TREE_CODE (val) != INTEGER_CST)
2237 if (!host_integerp (len, 1))
2240 if (optimize_size && tree_low_cst (len, 1) > 1)
2243 /* Assume that we can memset by pieces if we can store the
2244 * the coefficients by pieces (in the required modes).
2245 * We can't pass builtin_memset_gen_str as that emits RTL. */
2247 if (!can_store_by_pieces (tree_low_cst (len, 1),
2248 builtin_memset_read_str,
2249 (PTR) &c, dest_align))
2252 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
2253 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
2254 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
2256 dest_mem = get_memory_rtx (dest);
2257 store_by_pieces (dest_mem, tree_low_cst (len, 1),
2258 builtin_memset_gen_str,
2259 (PTR)val_rtx, dest_align);
2260 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2261 #ifdef POINTERS_EXTEND_UNSIGNED
2262 if (GET_MODE (dest_mem) != ptr_mode)
2263 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2268 if (target_char_cast (val, &c))
2273 if (!host_integerp (len, 1))
2275 if (!can_store_by_pieces (tree_low_cst (len, 1),
2276 builtin_memset_read_str, (PTR) &c,
2280 dest_mem = get_memory_rtx (dest);
2281 store_by_pieces (dest_mem, tree_low_cst (len, 1),
2282 builtin_memset_read_str,
2283 (PTR) &c, dest_align);
2284 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2285 #ifdef POINTERS_EXTEND_UNSIGNED
2286 if (GET_MODE (dest_mem) != ptr_mode)
2287 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2292 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2294 dest_mem = get_memory_rtx (dest);
2295 set_mem_align (dest_mem, dest_align);
2296 dest_addr = clear_storage (dest_mem, len_rtx);
2300 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2301 #ifdef POINTERS_EXTEND_UNSIGNED
2302 if (GET_MODE (dest_addr) != ptr_mode)
2303 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2311 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
2312 if we failed the caller should emit a normal call. */
2315 expand_builtin_bzero (exp)
2318 tree arglist = TREE_OPERAND (exp, 1);
2319 tree dest, size, newarglist;
2322 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2325 dest = TREE_VALUE (arglist);
2326 size = TREE_VALUE (TREE_CHAIN (arglist));
2328 /* New argument list transforming bzero(ptr x, int y) to
2329 memset(ptr x, int 0, size_t y). This is done this way
2330 so that if it isn't expanded inline, we fallback to
2331 calling bzero instead of memset. */
2333 newarglist = build_tree_list (NULL_TREE, convert (sizetype, size));
2334 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
2335 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2337 TREE_OPERAND (exp, 1) = newarglist;
2338 result = expand_builtin_memset (exp, const0_rtx, VOIDmode);
2340 /* Always restore the original arguments. */
2341 TREE_OPERAND (exp, 1) = arglist;
2346 /* Expand expression EXP, which is a call to the memcmp or the strcmp builtin.
2347 ARGLIST is the argument list for this call. Return 0 if we failed and the
2348 caller should emit a normal call, otherwise try to get the result in
2349 TARGET, if convenient (and in mode MODE, if that's convenient). */
2352 expand_builtin_memcmp (exp, arglist, target, mode)
2353 tree exp ATTRIBUTE_UNUSED;
2356 enum machine_mode mode;
2358 tree arg1, arg2, len;
2359 const char *p1, *p2;
2361 if (!validate_arglist (arglist,
2362 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2365 arg1 = TREE_VALUE (arglist);
2366 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
2367 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2369 /* If the len parameter is zero, return zero. */
2370 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
2372 /* Evaluate and ignore arg1 and arg2 in case they have
2374 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2375 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2379 p1 = c_getstr (arg1);
2380 p2 = c_getstr (arg2);
2382 /* If all arguments are constant, and the value of len is not greater
2383 than the lengths of arg1 and arg2, evaluate at compile-time. */
2384 if (host_integerp (len, 1) && p1 && p2
2385 && compare_tree_int (len, strlen (p1) + 1) <= 0
2386 && compare_tree_int (len, strlen (p2) + 1) <= 0)
2388 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
2390 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
2393 /* If len parameter is one, return an expression corresponding to
2394 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
2395 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
2397 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2398 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
2400 fold (build1 (CONVERT_EXPR, integer_type_node,
2401 build1 (INDIRECT_REF, cst_uchar_node,
2402 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
2404 fold (build1 (CONVERT_EXPR, integer_type_node,
2405 build1 (INDIRECT_REF, cst_uchar_node,
2406 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
2407 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
2408 return expand_expr (result, target, mode, EXPAND_NORMAL);
2411 #ifdef HAVE_cmpstrsi
2413 rtx arg1_rtx, arg2_rtx, arg3_rtx;
2418 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2420 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2421 enum machine_mode insn_mode
2422 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
2424 /* If we don't have POINTER_TYPE, call the function. */
2425 if (arg1_align == 0 || arg2_align == 0)
2428 /* Make a place to write the result of the instruction. */
2431 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
2432 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2433 result = gen_reg_rtx (insn_mode);
2435 arg1_rtx = get_memory_rtx (arg1);
2436 arg2_rtx = get_memory_rtx (arg2);
2437 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2441 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
2442 GEN_INT (MIN (arg1_align, arg2_align)));
2447 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
2448 TYPE_MODE (integer_type_node), 3,
2449 XEXP (arg1_rtx, 0), Pmode,
2450 XEXP (arg2_rtx, 0), Pmode,
2451 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
2452 TREE_UNSIGNED (sizetype)),
2453 TYPE_MODE (sizetype));
2455 /* Return the value in the proper mode for this function. */
2456 mode = TYPE_MODE (TREE_TYPE (exp));
2457 if (GET_MODE (result) == mode)
2459 else if (target != 0)
2461 convert_move (target, result, 0);
2465 return convert_to_mode (mode, result, 0);
2472 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
2473 if we failed the caller should emit a normal call, otherwise try to get
2474 the result in TARGET, if convenient. */
2477 expand_builtin_strcmp (exp, target, mode)
2480 enum machine_mode mode;
2482 tree arglist = TREE_OPERAND (exp, 1);
2483 tree arg1, arg2, len, len2, fn;
2484 const char *p1, *p2;
2486 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2489 arg1 = TREE_VALUE (arglist);
2490 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
2492 p1 = c_getstr (arg1);
2493 p2 = c_getstr (arg2);
2497 const int i = strcmp (p1, p2);
2498 return (i < 0 ? constm1_rtx : (i > 0 ? const1_rtx : const0_rtx));
2501 /* If either arg is "", return an expression corresponding to
2502 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
2503 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
2505 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2506 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
2508 fold (build1 (CONVERT_EXPR, integer_type_node,
2509 build1 (INDIRECT_REF, cst_uchar_node,
2510 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
2512 fold (build1 (CONVERT_EXPR, integer_type_node,
2513 build1 (INDIRECT_REF, cst_uchar_node,
2514 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
2515 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
2516 return expand_expr (result, target, mode, EXPAND_NORMAL);
2519 len = c_strlen (arg1);
2520 len2 = c_strlen (arg2);
2523 len = size_binop (PLUS_EXPR, ssize_int (1), len);
2526 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
2528 /* If we don't have a constant length for the first, use the length
2529 of the second, if we know it. We don't require a constant for
2530 this case; some cost analysis could be done if both are available
2531 but neither is constant. For now, assume they're equally cheap
2532 unless one has side effects.
2534 If both strings have constant lengths, use the smaller. This
2535 could arise if optimization results in strcpy being called with
2536 two fixed strings, or if the code was machine-generated. We should
2537 add some code to the `memcmp' handler below to deal with such
2538 situations, someday. */
2540 if (!len || TREE_CODE (len) != INTEGER_CST)
2542 if (len2 && !TREE_SIDE_EFFECTS (len2))
2547 else if (len2 && TREE_CODE (len2) == INTEGER_CST
2548 && tree_int_cst_lt (len2, len))
2551 /* If both arguments have side effects, we cannot optimize. */
2552 if (TREE_SIDE_EFFECTS (len))
2555 fn = built_in_decls[BUILT_IN_MEMCMP];
2559 chainon (arglist, build_tree_list (NULL_TREE, len));
2560 return expand_expr (build_function_call_expr (fn, arglist),
2561 target, mode, EXPAND_NORMAL);
2564 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
2565 if we failed the caller should emit a normal call, otherwise try to get
2566 the result in TARGET, if convenient. */
2569 expand_builtin_strncmp (exp, target, mode)
2572 enum machine_mode mode;
2574 tree arglist = TREE_OPERAND (exp, 1);
2575 tree fn, newarglist, len = 0;
2576 tree arg1, arg2, arg3;
2577 const char *p1, *p2;
2579 if (!validate_arglist (arglist,
2580 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2583 arg1 = TREE_VALUE (arglist);
2584 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
2585 arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2587 /* If the len parameter is zero, return zero. */
2588 if (host_integerp (arg3, 1) && tree_low_cst (arg3, 1) == 0)
2590 /* Evaluate and ignore arg1 and arg2 in case they have
2592 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2593 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2597 p1 = c_getstr (arg1);
2598 p2 = c_getstr (arg2);
2600 /* If all arguments are constant, evaluate at compile-time. */
2601 if (host_integerp (arg3, 1) && p1 && p2)
2603 const int r = strncmp (p1, p2, tree_low_cst (arg3, 1));
2604 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
2607 /* If len == 1 or (either string parameter is "" and (len >= 1)),
2608 return (*(const u_char*)arg1 - *(const u_char*)arg2). */
2609 if (host_integerp (arg3, 1)
2610 && (tree_low_cst (arg3, 1) == 1
2611 || (tree_low_cst (arg3, 1) > 1
2612 && ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')))))
2614 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2615 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
2617 fold (build1 (CONVERT_EXPR, integer_type_node,
2618 build1 (INDIRECT_REF, cst_uchar_node,
2619 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
2621 fold (build1 (CONVERT_EXPR, integer_type_node,
2622 build1 (INDIRECT_REF, cst_uchar_node,
2623 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
2624 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
2625 return expand_expr (result, target, mode, EXPAND_NORMAL);
2628 /* If c_strlen can determine an expression for one of the string
2629 lengths, and it doesn't have side effects, then call
2630 expand_builtin_memcmp() using length MIN(strlen(string)+1, arg3). */
2632 /* Perhaps one of the strings is really constant, if so prefer
2633 that constant length over the other string's length. */
2635 len = c_strlen (arg1);
2637 len = c_strlen (arg2);
2639 /* If we still don't have a len, try either string arg as long
2640 as they don't have side effects. */
2641 if (!len && !TREE_SIDE_EFFECTS (arg1))
2642 len = c_strlen (arg1);
2643 if (!len && !TREE_SIDE_EFFECTS (arg2))
2644 len = c_strlen (arg2);
2645 /* If we still don't have a length, punt. */
2649 fn = built_in_decls[BUILT_IN_MEMCMP];
2653 /* Add one to the string length. */
2654 len = fold (size_binop (PLUS_EXPR, len, ssize_int (1)));
2656 /* The actual new length parameter is MIN(len,arg3). */
2657 len = fold (build (MIN_EXPR, TREE_TYPE (len), len, arg3));
2659 newarglist = build_tree_list (NULL_TREE, len);
2660 newarglist = tree_cons (NULL_TREE, arg2, newarglist);
2661 newarglist = tree_cons (NULL_TREE, arg1, newarglist);
2662 return expand_expr (build_function_call_expr (fn, newarglist),
2663 target, mode, EXPAND_NORMAL);
2666 /* Expand expression EXP, which is a call to the strcat builtin.
2667 Return 0 if we failed the caller should emit a normal call,
2668 otherwise try to get the result in TARGET, if convenient. */
2671 expand_builtin_strcat (arglist, target, mode)
2674 enum machine_mode mode;
2676 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2680 tree dst = TREE_VALUE (arglist),
2681 src = TREE_VALUE (TREE_CHAIN (arglist));
2682 const char *p = c_getstr (src);
2684 /* If the string length is zero, return the dst parameter. */
2685 if (p && *p == '\0')
2686 return expand_expr (dst, target, mode, EXPAND_NORMAL);
2692 /* Expand expression EXP, which is a call to the strncat builtin.
2693 Return 0 if we failed the caller should emit a normal call,
2694 otherwise try to get the result in TARGET, if convenient. */
2697 expand_builtin_strncat (arglist, target, mode)
2700 enum machine_mode mode;
2702 if (!validate_arglist (arglist,
2703 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2707 tree dst = TREE_VALUE (arglist),
2708 src = TREE_VALUE (TREE_CHAIN (arglist)),
2709 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2710 const char *p = c_getstr (src);
2712 /* If the requested length is zero, or the src parameter string
2713 length is zero, return the dst parameter. */
2714 if (integer_zerop (len) || (p && *p == '\0'))
2716 /* Evaluate and ignore the src and len parameters in case
2717 they have side-effects. */
2718 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2719 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2720 return expand_expr (dst, target, mode, EXPAND_NORMAL);
2723 /* If the requested len is greater than or equal to the string
2724 length, call strcat. */
2725 if (TREE_CODE (len) == INTEGER_CST && p
2726 && compare_tree_int (len, strlen (p)) >= 0)
2729 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
2730 tree fn = built_in_decls[BUILT_IN_STRCAT];
2732 /* If the replacement _DECL isn't initialized, don't do the
2737 return expand_expr (build_function_call_expr (fn, newarglist),
2738 target, mode, EXPAND_NORMAL);
2744 /* Expand expression EXP, which is a call to the strspn builtin.
2745 Return 0 if we failed the caller should emit a normal call,
2746 otherwise try to get the result in TARGET, if convenient. */
2749 expand_builtin_strspn (arglist, target, mode)
2752 enum machine_mode mode;
2754 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2758 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2759 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
2761 /* If both arguments are constants, evaluate at compile-time. */
2764 const size_t r = strspn (p1, p2);
2765 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
2768 /* If either argument is "", return 0. */
2769 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
2771 /* Evaluate and ignore both arguments in case either one has
2773 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2774 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2781 /* Expand expression EXP, which is a call to the strcspn builtin.
2782 Return 0 if we failed the caller should emit a normal call,
2783 otherwise try to get the result in TARGET, if convenient. */
2786 expand_builtin_strcspn (arglist, target, mode)
2789 enum machine_mode mode;
2791 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2795 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2796 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
2798 /* If both arguments are constants, evaluate at compile-time. */
2801 const size_t r = strcspn (p1, p2);
2802 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
2805 /* If the first argument is "", return 0. */
2806 if (p1 && *p1 == '\0')
2808 /* Evaluate and ignore argument s2 in case it has
2810 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2814 /* If the second argument is "", return __builtin_strlen(s1). */
2815 if (p2 && *p2 == '\0')
2817 tree newarglist = build_tree_list (NULL_TREE, s1),
2818 fn = built_in_decls[BUILT_IN_STRLEN];
2820 /* If the replacement _DECL isn't initialized, don't do the
2825 return expand_expr (build_function_call_expr (fn, newarglist),
2826 target, mode, EXPAND_NORMAL);
2832 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
2833 if that's convenient. */
2836 expand_builtin_saveregs ()
2840 /* Don't do __builtin_saveregs more than once in a function.
2841 Save the result of the first call and reuse it. */
2842 if (saveregs_value != 0)
2843 return saveregs_value;
2845 /* When this function is called, it means that registers must be
2846 saved on entry to this function. So we migrate the call to the
2847 first insn of this function. */
2851 #ifdef EXPAND_BUILTIN_SAVEREGS
2852 /* Do whatever the machine needs done in this case. */
2853 val = EXPAND_BUILTIN_SAVEREGS ();
2855 /* ??? We used to try and build up a call to the out of line function,
2856 guessing about what registers needed saving etc. This became much
2857 harder with __builtin_va_start, since we don't have a tree for a
2858 call to __builtin_saveregs to fall back on. There was exactly one
2859 port (i860) that used this code, and I'm unconvinced it could actually
2860 handle the general case. So we no longer try to handle anything
2861 weird and make the backend absorb the evil. */
2863 error ("__builtin_saveregs not supported by this target");
2870 saveregs_value = val;
2872 /* Put the insns after the NOTE that starts the function. If this
2873 is inside a start_sequence, make the outer-level insn chain current, so
2874 the code is placed at the start of the function. */
2875 push_topmost_sequence ();
2876 emit_insn_after (seq, get_insns ());
2877 pop_topmost_sequence ();
2882 /* __builtin_args_info (N) returns word N of the arg space info
2883 for the current function. The number and meanings of words
2884 is controlled by the definition of CUMULATIVE_ARGS. */
2887 expand_builtin_args_info (exp)
2890 tree arglist = TREE_OPERAND (exp, 1);
2891 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
2892 int *word_ptr = (int *) ¤t_function_args_info;
2894 /* These are used by the code below that is if 0'ed away */
2896 tree type, elts, result;
2899 if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0)
2904 if (!host_integerp (TREE_VALUE (arglist), 0))
2905 error ("argument of `__builtin_args_info' must be constant");
2908 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
2910 if (wordnum < 0 || wordnum >= nwords)
2911 error ("argument of `__builtin_args_info' out of range");
2913 return GEN_INT (word_ptr[wordnum]);
2917 error ("missing argument in `__builtin_args_info'");
2922 for (i = 0; i < nwords; i++)
2923 elts = tree_cons (NULL_TREE, build_int_2 (word_ptr[i], 0));
2925 type = build_array_type (integer_type_node,
2926 build_index_type (build_int_2 (nwords, 0)));
2927 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (elts));
2928 TREE_CONSTANT (result) = 1;
2929 TREE_STATIC (result) = 1;
2930 result = build1 (INDIRECT_REF, build_pointer_type (type), result);
2931 TREE_CONSTANT (result) = 1;
2932 return expand_expr (result, NULL_RTX, VOIDmode, 0);
2936 /* Expand ARGLIST, from a call to __builtin_next_arg. */
2939 expand_builtin_next_arg (arglist)
2942 tree fntype = TREE_TYPE (current_function_decl);
2944 if (TYPE_ARG_TYPES (fntype) == 0
2945 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
2948 error ("`va_start' used in function with fixed args");
2954 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
2955 tree arg = TREE_VALUE (arglist);
2957 /* Strip off all nops for the sake of the comparison. This
2958 is not quite the same as STRIP_NOPS. It does more.
2959 We must also strip off INDIRECT_EXPR for C++ reference
2961 while (TREE_CODE (arg) == NOP_EXPR
2962 || TREE_CODE (arg) == CONVERT_EXPR
2963 || TREE_CODE (arg) == NON_LVALUE_EXPR
2964 || TREE_CODE (arg) == INDIRECT_REF)
2965 arg = TREE_OPERAND (arg, 0);
2966 if (arg != last_parm)
2967 warning ("second parameter of `va_start' not last named argument");
2970 /* Evidently an out of date version of <stdarg.h>; can't validate
2971 va_start's second argument, but can still work as intended. */
2972 warning ("`__builtin_next_arg' called without an argument");
2974 return expand_binop (Pmode, add_optab,
2975 current_function_internal_arg_pointer,
2976 current_function_arg_offset_rtx,
2977 NULL_RTX, 0, OPTAB_LIB_WIDEN);
2980 /* Make it easier for the backends by protecting the valist argument
2981 from multiple evaluations. */
2984 stabilize_va_list (valist, needs_lvalue)
2988 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2990 if (TREE_SIDE_EFFECTS (valist))
2991 valist = save_expr (valist);
2993 /* For this case, the backends will be expecting a pointer to
2994 TREE_TYPE (va_list_type_node), but it's possible we've
2995 actually been given an array (an actual va_list_type_node).
2997 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
2999 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3000 tree p2 = build_pointer_type (va_list_type_node);
3002 valist = build1 (ADDR_EXPR, p2, valist);
3003 valist = fold (build1 (NOP_EXPR, p1, valist));
3012 if (! TREE_SIDE_EFFECTS (valist))
3015 pt = build_pointer_type (va_list_type_node);
3016 valist = fold (build1 (ADDR_EXPR, pt, valist));
3017 TREE_SIDE_EFFECTS (valist) = 1;
3020 if (TREE_SIDE_EFFECTS (valist))
3021 valist = save_expr (valist);
3022 valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)),
3029 /* The "standard" implementation of va_start: just assign `nextarg' to
3033 std_expand_builtin_va_start (valist, nextarg)
3039 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
3040 make_tree (ptr_type_node, nextarg));
3041 TREE_SIDE_EFFECTS (t) = 1;
3043 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3046 /* Expand ARGLIST, from a call to __builtin_va_start. */
3049 expand_builtin_va_start (arglist)
3055 chain = TREE_CHAIN (arglist);
3057 if (TREE_CHAIN (chain))
3058 error ("too many arguments to function `va_start'");
3060 nextarg = expand_builtin_next_arg (chain);
3061 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
3063 #ifdef EXPAND_BUILTIN_VA_START
3064 EXPAND_BUILTIN_VA_START (valist, nextarg);
3066 std_expand_builtin_va_start (valist, nextarg);
3072 /* The "standard" implementation of va_arg: read the value from the
3073 current (padded) address and increment by the (padded) size. */
3076 std_expand_builtin_va_arg (valist, type)
3079 tree addr_tree, t, type_size = NULL;
3080 tree align, alignm1;
3084 /* Compute the rounded size of the type. */
3085 align = size_int (PARM_BOUNDARY / BITS_PER_UNIT);
3086 alignm1 = size_int (PARM_BOUNDARY / BITS_PER_UNIT - 1);
3087 if (type == error_mark_node
3088 || (type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type))) == NULL
3089 || TREE_OVERFLOW (type_size))
3090 rounded_size = size_zero_node;
3092 rounded_size = fold (build (MULT_EXPR, sizetype,
3093 fold (build (TRUNC_DIV_EXPR, sizetype,
3094 fold (build (PLUS_EXPR, sizetype,
3095 type_size, alignm1)),
3101 if (PAD_VARARGS_DOWN && ! integer_zerop (rounded_size))
3103 /* Small args are padded downward. */
3104 addr_tree = fold (build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
3105 fold (build (COND_EXPR, sizetype,
3106 fold (build (GT_EXPR, sizetype,
3110 fold (build (MINUS_EXPR, sizetype,
3115 addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
3116 addr = copy_to_reg (addr);
3118 /* Compute new value for AP. */
3119 if (! integer_zerop (rounded_size))
3121 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
3122 build (PLUS_EXPR, TREE_TYPE (valist), valist,
3124 TREE_SIDE_EFFECTS (t) = 1;
3125 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3131 /* Expand __builtin_va_arg, which is not really a builtin function, but
3132 a very special sort of operator. */
3135 expand_builtin_va_arg (valist, type)
3139 tree promoted_type, want_va_type, have_va_type;
3141 /* Verify that valist is of the proper type. */
3143 want_va_type = va_list_type_node;
3144 have_va_type = TREE_TYPE (valist);
3145 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
3147 /* If va_list is an array type, the argument may have decayed
3148 to a pointer type, e.g. by being passed to another function.
3149 In that case, unwrap both types so that we can compare the
3150 underlying records. */
3151 if (TREE_CODE (have_va_type) == ARRAY_TYPE
3152 || TREE_CODE (have_va_type) == POINTER_TYPE)
3154 want_va_type = TREE_TYPE (want_va_type);
3155 have_va_type = TREE_TYPE (have_va_type);
3158 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
3160 error ("first argument to `va_arg' not of type `va_list'");
3164 /* Generate a diagnostic for requesting data of a type that cannot
3165 be passed through `...' due to type promotion at the call site. */
3166 else if ((promoted_type = (*lang_hooks.types.type_promotes_to) (type))
3169 const char *name = "<anonymous type>", *pname = 0;
3170 static bool gave_help;
3172 if (TYPE_NAME (type))
3174 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3175 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3176 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
3177 && DECL_NAME (TYPE_NAME (type)))
3178 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3180 if (TYPE_NAME (promoted_type))
3182 if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE)
3183 pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type));
3184 else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL
3185 && DECL_NAME (TYPE_NAME (promoted_type)))
3186 pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type)));
3189 /* Unfortunately, this is merely undefined, rather than a constraint
3190 violation, so we cannot make this an error. If this call is never
3191 executed, the program is still strictly conforming. */
3192 warning ("`%s' is promoted to `%s' when passed through `...'",
3197 warning ("(so you should pass `%s' not `%s' to `va_arg')",
3201 /* We can, however, treat "undefined" any way we please.
3202 Call abort to encourage the user to fix the program. */
3203 expand_builtin_trap ();
3205 /* This is dead code, but go ahead and finish so that the
3206 mode of the result comes out right. */
3211 /* Make it easier for the backends by protecting the valist argument
3212 from multiple evaluations. */
3213 valist = stabilize_va_list (valist, 0);
3215 #ifdef EXPAND_BUILTIN_VA_ARG
3216 addr = EXPAND_BUILTIN_VA_ARG (valist, type);
3218 addr = std_expand_builtin_va_arg (valist, type);
3222 #ifdef POINTERS_EXTEND_UNSIGNED
3223 if (GET_MODE (addr) != Pmode)
3224 addr = convert_memory_address (Pmode, addr);
3227 result = gen_rtx_MEM (TYPE_MODE (type), addr);
3228 set_mem_alias_set (result, get_varargs_alias_set ());
3233 /* Expand ARGLIST, from a call to __builtin_va_end. */
3236 expand_builtin_va_end (arglist)
3239 tree valist = TREE_VALUE (arglist);
3241 #ifdef EXPAND_BUILTIN_VA_END
3242 valist = stabilize_va_list (valist, 0);
3243 EXPAND_BUILTIN_VA_END(arglist);
3245 /* Evaluate for side effects, if needed. I hate macros that don't
3247 if (TREE_SIDE_EFFECTS (valist))
3248 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
3254 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
3255 builtin rather than just as an assignment in stdarg.h because of the
3256 nastiness of array-type va_list types. */
3259 expand_builtin_va_copy (arglist)
3264 dst = TREE_VALUE (arglist);
3265 src = TREE_VALUE (TREE_CHAIN (arglist));
3267 dst = stabilize_va_list (dst, 1);
3268 src = stabilize_va_list (src, 0);
3270 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
3272 t = build (MODIFY_EXPR, va_list_type_node, dst, src);
3273 TREE_SIDE_EFFECTS (t) = 1;
3274 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3278 rtx dstb, srcb, size;
3280 /* Evaluate to pointers. */
3281 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
3282 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
3283 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
3284 VOIDmode, EXPAND_NORMAL);
3286 #ifdef POINTERS_EXTEND_UNSIGNED
3287 if (GET_MODE (dstb) != Pmode)
3288 dstb = convert_memory_address (Pmode, dstb);
3290 if (GET_MODE (srcb) != Pmode)
3291 srcb = convert_memory_address (Pmode, srcb);
3294 /* "Dereference" to BLKmode memories. */
3295 dstb = gen_rtx_MEM (BLKmode, dstb);
3296 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
3297 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
3298 srcb = gen_rtx_MEM (BLKmode, srcb);
3299 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
3300 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
3303 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
3309 /* Expand a call to one of the builtin functions __builtin_frame_address or
3310 __builtin_return_address. */
3313 expand_builtin_frame_address (exp)
3316 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3317 tree arglist = TREE_OPERAND (exp, 1);
3319 /* The argument must be a nonnegative integer constant.
3320 It counts the number of frames to scan up the stack.
3321 The value is the return address saved in that frame. */
3323 /* Warning about missing arg was already issued. */
3325 else if (! host_integerp (TREE_VALUE (arglist), 1))
3327 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
3328 error ("invalid arg to `__builtin_frame_address'");
3330 error ("invalid arg to `__builtin_return_address'");
3336 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
3337 tree_low_cst (TREE_VALUE (arglist), 1),
3338 hard_frame_pointer_rtx);
3340 /* Some ports cannot access arbitrary stack frames. */
3343 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
3344 warning ("unsupported arg to `__builtin_frame_address'");
3346 warning ("unsupported arg to `__builtin_return_address'");
3350 /* For __builtin_frame_address, return what we've got. */
3351 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
3354 if (GET_CODE (tem) != REG
3355 && ! CONSTANT_P (tem))
3356 tem = copy_to_mode_reg (Pmode, tem);
3361 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
3362 we failed and the caller should emit a normal call, otherwise try to get
3363 the result in TARGET, if convenient. */
3366 expand_builtin_alloca (arglist, target)
3373 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
3376 /* Compute the argument. */
3377 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
3379 /* Allocate the desired space. */
3380 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
3382 #ifdef POINTERS_EXTEND_UNSIGNED
3383 if (GET_MODE (result) != ptr_mode)
3384 result = convert_memory_address (ptr_mode, result);
3390 /* Expand a call to the ffs builtin. The arguments are in ARGLIST.
3391 Return 0 if a normal call should be emitted rather than expanding the
3392 function in-line. If convenient, the result should be placed in TARGET.
3393 SUBTARGET may be used as the target for computing one of EXP's operands. */
3396 expand_builtin_ffs (arglist, target, subtarget)
3398 rtx target, subtarget;
3401 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
3404 /* Compute the argument. */
3405 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
3406 /* Compute ffs, into TARGET if possible.
3407 Set TARGET to wherever the result comes back. */
3408 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
3409 ffs_optab, op0, target, 1);
3415 /* If the string passed to fputs is a constant and is one character
3416 long, we attempt to transform this call into __builtin_fputc(). */
3419 expand_builtin_fputs (arglist, ignore, unlocked)
3425 tree fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
3426 : built_in_decls[BUILT_IN_FPUTC];
3427 tree fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
3428 : built_in_decls[BUILT_IN_FWRITE];
3430 /* If the return value is used, or the replacement _DECL isn't
3431 initialized, don't do the transformation. */
3432 if (!ignore || !fn_fputc || !fn_fwrite)
3435 /* Verify the arguments in the original call. */
3436 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3439 /* Get the length of the string passed to fputs. If the length
3440 can't be determined, punt. */
3441 if (!(len = c_strlen (TREE_VALUE (arglist)))
3442 || TREE_CODE (len) != INTEGER_CST)
3445 switch (compare_tree_int (len, 1))
3447 case -1: /* length is 0, delete the call entirely . */
3449 /* Evaluate and ignore the argument in case it has
3451 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
3452 VOIDmode, EXPAND_NORMAL);
3455 case 0: /* length is 1, call fputc. */
3457 const char *p = c_getstr (TREE_VALUE (arglist));
3461 /* New argument list transforming fputs(string, stream) to
3462 fputc(string[0], stream). */
3464 build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
3466 tree_cons (NULL_TREE, build_int_2 (p[0], 0), arglist);
3472 case 1: /* length is greater than 1, call fwrite. */
3474 tree string_arg = TREE_VALUE (arglist);
3476 /* New argument list transforming fputs(string, stream) to
3477 fwrite(string, 1, len, stream). */
3478 arglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
3479 arglist = tree_cons (NULL_TREE, len, arglist);
3480 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
3481 arglist = tree_cons (NULL_TREE, string_arg, arglist);
3489 return expand_expr (build_function_call_expr (fn, arglist),
3490 (ignore ? const0_rtx : NULL_RTX),
3491 VOIDmode, EXPAND_NORMAL);
3494 /* Expand a call to __builtin_expect. We return our argument and emit a
3495 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
3496 a non-jump context. */
3499 expand_builtin_expect (arglist, target)
3506 if (arglist == NULL_TREE
3507 || TREE_CHAIN (arglist) == NULL_TREE)
3509 exp = TREE_VALUE (arglist);
3510 c = TREE_VALUE (TREE_CHAIN (arglist));
3512 if (TREE_CODE (c) != INTEGER_CST)
3514 error ("second arg to `__builtin_expect' must be a constant");
3515 c = integer_zero_node;
3518 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
3520 /* Don't bother with expected value notes for integral constants. */
3521 if (GET_CODE (target) != CONST_INT)
3523 /* We do need to force this into a register so that we can be
3524 moderately sure to be able to correctly interpret the branch
3526 target = force_reg (GET_MODE (target), target);
3528 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
3530 note = emit_note (NULL, NOTE_INSN_EXPECTED_VALUE);
3531 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
3537 /* Like expand_builtin_expect, except do this in a jump context. This is
3538 called from do_jump if the conditional is a __builtin_expect. Return either
3539 a list of insns to emit the jump or NULL if we cannot optimize
3540 __builtin_expect. We need to optimize this at jump time so that machines
3541 like the PowerPC don't turn the test into a SCC operation, and then jump
3542 based on the test being 0/1. */
3545 expand_builtin_expect_jump (exp, if_false_label, if_true_label)
3550 tree arglist = TREE_OPERAND (exp, 1);
3551 tree arg0 = TREE_VALUE (arglist);
3552 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3555 /* Only handle __builtin_expect (test, 0) and
3556 __builtin_expect (test, 1). */
3557 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
3558 && (integer_zerop (arg1) || integer_onep (arg1)))
3563 /* If we fail to locate an appropriate conditional jump, we'll
3564 fall back to normal evaluation. Ensure that the expression
3565 can be re-evaluated. */
3566 switch (unsafe_for_reeval (arg0))
3571 case 1: /* Mildly unsafe. */
3572 arg0 = unsave_expr (arg0);
3575 case 2: /* Wildly unsafe. */
3579 /* Expand the jump insns. */
3581 do_jump (arg0, if_false_label, if_true_label);
3585 /* Now that the __builtin_expect has been validated, go through and add
3586 the expect's to each of the conditional jumps. If we run into an
3587 error, just give up and generate the 'safe' code of doing a SCC
3588 operation and then doing a branch on that. */
3590 while (insn != NULL_RTX)
3592 rtx next = NEXT_INSN (insn);
3595 if (GET_CODE (insn) == JUMP_INSN && any_condjump_p (insn)
3596 && (pattern = pc_set (insn)) != NULL_RTX)
3598 rtx ifelse = SET_SRC (pattern);
3602 if (GET_CODE (ifelse) != IF_THEN_ELSE)
3605 if (GET_CODE (XEXP (ifelse, 1)) == LABEL_REF)
3608 label = XEXP (XEXP (ifelse, 1), 0);
3610 /* An inverted jump reverses the probabilities. */
3611 else if (GET_CODE (XEXP (ifelse, 2)) == LABEL_REF)
3614 label = XEXP (XEXP (ifelse, 2), 0);
3616 /* We shouldn't have to worry about conditional returns during
3617 the expansion stage, but handle it gracefully anyway. */
3618 else if (GET_CODE (XEXP (ifelse, 1)) == RETURN)
3623 /* An inverted return reverses the probabilities. */
3624 else if (GET_CODE (XEXP (ifelse, 2)) == RETURN)
3632 /* If the test is expected to fail, reverse the
3634 if (integer_zerop (arg1))
3637 /* If we are jumping to the false label, reverse the
3639 if (label == NULL_RTX)
3640 ; /* conditional return */
3641 else if (label == if_false_label)
3643 else if (label != if_true_label)
3647 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
3654 /* If no jumps were modified, fail and do __builtin_expect the normal
3664 expand_builtin_trap ()
3668 emit_insn (gen_trap ());
3671 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
3675 /* Expand an expression EXP that calls a built-in function,
3676 with result going to TARGET if that's convenient
3677 (and in mode MODE if that's convenient).
3678 SUBTARGET may be used as the target for computing one of EXP's operands.
3679 IGNORE is nonzero if the value is to be ignored. */
3682 expand_builtin (exp, target, subtarget, mode, ignore)
3686 enum machine_mode mode;
3689 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3690 tree arglist = TREE_OPERAND (exp, 1);
3691 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
3693 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
3694 return (*targetm.expand_builtin) (exp, target, subtarget, mode, ignore);
3696 /* When not optimizing, generate calls to library functions for a certain
3698 if (!optimize && !CALLED_AS_BUILT_IN (fndecl))
3702 case BUILT_IN_SQRTF:
3703 case BUILT_IN_SQRTL:
3713 case BUILT_IN_MEMSET:
3714 case BUILT_IN_MEMCPY:
3715 case BUILT_IN_MEMCMP:
3717 case BUILT_IN_BZERO:
3718 case BUILT_IN_INDEX:
3719 case BUILT_IN_RINDEX:
3720 case BUILT_IN_STRCHR:
3721 case BUILT_IN_STRRCHR:
3722 case BUILT_IN_STRLEN:
3723 case BUILT_IN_STRCPY:
3724 case BUILT_IN_STRNCPY:
3725 case BUILT_IN_STRNCMP:
3726 case BUILT_IN_STRSTR:
3727 case BUILT_IN_STRPBRK:
3728 case BUILT_IN_STRCAT:
3729 case BUILT_IN_STRNCAT:
3730 case BUILT_IN_STRSPN:
3731 case BUILT_IN_STRCSPN:
3732 case BUILT_IN_STRCMP:
3734 case BUILT_IN_PUTCHAR:
3736 case BUILT_IN_PRINTF:
3737 case BUILT_IN_FPUTC:
3738 case BUILT_IN_FPUTS:
3739 case BUILT_IN_FWRITE:
3740 case BUILT_IN_PUTCHAR_UNLOCKED:
3741 case BUILT_IN_PUTS_UNLOCKED:
3742 case BUILT_IN_PRINTF_UNLOCKED:
3743 case BUILT_IN_FPUTC_UNLOCKED:
3744 case BUILT_IN_FPUTS_UNLOCKED:
3745 case BUILT_IN_FWRITE_UNLOCKED:
3746 return expand_call (exp, target, ignore);
3756 case BUILT_IN_LLABS:
3757 case BUILT_IN_IMAXABS:
3759 case BUILT_IN_FABSF:
3760 case BUILT_IN_FABSL:
3761 /* build_function_call changes these into ABS_EXPR. */
3765 case BUILT_IN_CONJF:
3766 case BUILT_IN_CONJL:
3767 case BUILT_IN_CREAL:
3768 case BUILT_IN_CREALF:
3769 case BUILT_IN_CREALL:
3770 case BUILT_IN_CIMAG:
3771 case BUILT_IN_CIMAGF:
3772 case BUILT_IN_CIMAGL:
3773 /* expand_tree_builtin changes these into CONJ_EXPR, REALPART_EXPR
3774 and IMAGPART_EXPR. */
3789 /* Treat these like sqrt only if unsafe math optimizations are allowed,
3790 because of possible accuracy problems. */
3791 if (! flag_unsafe_math_optimizations)
3794 case BUILT_IN_SQRTF:
3795 case BUILT_IN_SQRTL:
3796 target = expand_builtin_mathfn (exp, target, subtarget);
3801 case BUILT_IN_APPLY_ARGS:
3802 return expand_builtin_apply_args ();
3804 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
3805 FUNCTION with a copy of the parameters described by
3806 ARGUMENTS, and ARGSIZE. It returns a block of memory
3807 allocated on the stack into which is stored all the registers
3808 that might possibly be used for returning the result of a
3809 function. ARGUMENTS is the value returned by
3810 __builtin_apply_args. ARGSIZE is the number of bytes of
3811 arguments that must be copied. ??? How should this value be
3812 computed? We'll also need a safe worst case value for varargs
3814 case BUILT_IN_APPLY:
3815 if (!validate_arglist (arglist, POINTER_TYPE,
3816 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
3817 && !validate_arglist (arglist, REFERENCE_TYPE,
3818 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3826 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
3827 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
3829 return expand_builtin_apply (ops[0], ops[1], ops[2]);
3832 /* __builtin_return (RESULT) causes the function to return the
3833 value described by RESULT. RESULT is address of the block of
3834 memory returned by __builtin_apply. */
3835 case BUILT_IN_RETURN:
3836 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
3837 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
3838 NULL_RTX, VOIDmode, 0));
3841 case BUILT_IN_SAVEREGS:
3842 return expand_builtin_saveregs ();
3844 case BUILT_IN_ARGS_INFO:
3845 return expand_builtin_args_info (exp);
3847 /* Return the address of the first anonymous stack arg. */
3848 case BUILT_IN_NEXT_ARG:
3849 return expand_builtin_next_arg (arglist);
3851 case BUILT_IN_CLASSIFY_TYPE:
3852 return expand_builtin_classify_type (arglist);
3854 case BUILT_IN_CONSTANT_P:
3855 return expand_builtin_constant_p (exp);
3857 case BUILT_IN_FRAME_ADDRESS:
3858 case BUILT_IN_RETURN_ADDRESS:
3859 return expand_builtin_frame_address (exp);
3861 /* Returns the address of the area where the structure is returned.
3863 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
3865 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
3866 || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM)
3869 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
3871 case BUILT_IN_ALLOCA:
3872 target = expand_builtin_alloca (arglist, target);
3878 target = expand_builtin_ffs (arglist, target, subtarget);
3883 case BUILT_IN_STRLEN:
3884 target = expand_builtin_strlen (exp, target);
3889 case BUILT_IN_STRCPY:
3890 target = expand_builtin_strcpy (exp, target, mode);
3895 case BUILT_IN_STRNCPY:
3896 target = expand_builtin_strncpy (arglist, target, mode);
3901 case BUILT_IN_STRCAT:
3902 target = expand_builtin_strcat (arglist, target, mode);
3907 case BUILT_IN_STRNCAT:
3908 target = expand_builtin_strncat (arglist, target, mode);
3913 case BUILT_IN_STRSPN:
3914 target = expand_builtin_strspn (arglist, target, mode);
3919 case BUILT_IN_STRCSPN:
3920 target = expand_builtin_strcspn (arglist, target, mode);
3925 case BUILT_IN_STRSTR:
3926 target = expand_builtin_strstr (arglist, target, mode);
3931 case BUILT_IN_STRPBRK:
3932 target = expand_builtin_strpbrk (arglist, target, mode);
3937 case BUILT_IN_INDEX:
3938 case BUILT_IN_STRCHR:
3939 target = expand_builtin_strchr (arglist, target, mode);
3944 case BUILT_IN_RINDEX:
3945 case BUILT_IN_STRRCHR:
3946 target = expand_builtin_strrchr (arglist, target, mode);
3951 case BUILT_IN_MEMCPY:
3952 target = expand_builtin_memcpy (arglist, target, mode);
3957 case BUILT_IN_MEMSET:
3958 target = expand_builtin_memset (exp, target, mode);
3963 case BUILT_IN_BZERO:
3964 target = expand_builtin_bzero (exp);
3969 case BUILT_IN_STRCMP:
3970 target = expand_builtin_strcmp (exp, target, mode);
3975 case BUILT_IN_STRNCMP:
3976 target = expand_builtin_strncmp (exp, target, mode);
3982 case BUILT_IN_MEMCMP:
3983 target = expand_builtin_memcmp (exp, arglist, target, mode);
3988 case BUILT_IN_SETJMP:
3989 target = expand_builtin_setjmp (arglist, target);
3994 /* __builtin_longjmp is passed a pointer to an array of five words.
3995 It's similar to the C library longjmp function but works with
3996 __builtin_setjmp above. */
3997 case BUILT_IN_LONGJMP:
3998 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4002 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
4004 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
4005 NULL_RTX, VOIDmode, 0);
4007 if (value != const1_rtx)
4009 error ("__builtin_longjmp second argument must be 1");
4013 expand_builtin_longjmp (buf_addr, value);
4018 expand_builtin_trap ();
4021 case BUILT_IN_FPUTS:
4022 target = expand_builtin_fputs (arglist, ignore,/*unlocked=*/ 0);
4026 case BUILT_IN_FPUTS_UNLOCKED:
4027 target = expand_builtin_fputs (arglist, ignore,/*unlocked=*/ 1);
4032 /* Various hooks for the DWARF 2 __throw routine. */
4033 case BUILT_IN_UNWIND_INIT:
4034 expand_builtin_unwind_init ();
4036 case BUILT_IN_DWARF_CFA:
4037 return virtual_cfa_rtx;
4038 #ifdef DWARF2_UNWIND_INFO
4039 case BUILT_IN_DWARF_FP_REGNUM:
4040 return expand_builtin_dwarf_fp_regnum ();
4041 case BUILT_IN_INIT_DWARF_REG_SIZES:
4042 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
4045 case BUILT_IN_FROB_RETURN_ADDR:
4046 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
4047 case BUILT_IN_EXTRACT_RETURN_ADDR:
4048 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
4049 case BUILT_IN_EH_RETURN:
4050 expand_builtin_eh_return (TREE_VALUE (arglist),
4051 TREE_VALUE (TREE_CHAIN (arglist)));
4053 #ifdef EH_RETURN_DATA_REGNO
4054 case BUILT_IN_EH_RETURN_DATA_REGNO:
4055 return expand_builtin_eh_return_data_regno (arglist);
4057 case BUILT_IN_VA_START:
4058 case BUILT_IN_STDARG_START:
4059 return expand_builtin_va_start (arglist);
4060 case BUILT_IN_VA_END:
4061 return expand_builtin_va_end (arglist);
4062 case BUILT_IN_VA_COPY:
4063 return expand_builtin_va_copy (arglist);
4064 case BUILT_IN_EXPECT:
4065 return expand_builtin_expect (arglist, target);
4066 case BUILT_IN_PREFETCH:
4067 expand_builtin_prefetch (arglist);
4071 default: /* just do library call, if unknown builtin */
4072 if (!DECL_ASSEMBLER_NAME_SET_P (fndecl))
4073 error ("built-in function `%s' not currently supported",
4074 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
4077 /* The switch statement above can drop through to cause the function
4078 to be called normally. */
4079 return expand_call (exp, target, ignore);
4082 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
4083 constant. ARGLIST is the argument list of the call. */
4086 fold_builtin_constant_p (arglist)
4092 arglist = TREE_VALUE (arglist);
4094 /* We return 1 for a numeric type that's known to be a constant
4095 value at compile-time or for an aggregate type that's a
4096 literal constant. */
4097 STRIP_NOPS (arglist);
4099 /* If we know this is a constant, emit the constant of one. */
4100 if (TREE_CODE_CLASS (TREE_CODE (arglist)) == 'c'
4101 || (TREE_CODE (arglist) == CONSTRUCTOR
4102 && TREE_CONSTANT (arglist))
4103 || (TREE_CODE (arglist) == ADDR_EXPR
4104 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
4105 return integer_one_node;
4107 /* If we aren't going to be running CSE or this expression
4108 has side effects, show we don't know it to be a constant.
4109 Likewise if it's a pointer or aggregate type since in those
4110 case we only want literals, since those are only optimized
4111 when generating RTL, not later.
4112 And finally, if we are compiling an initializer, not code, we
4113 need to return a definite result now; there's not going to be any
4114 more optimization done. */
4115 if (TREE_SIDE_EFFECTS (arglist) || cse_not_expected
4116 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
4117 || POINTER_TYPE_P (TREE_TYPE (arglist))
4119 return integer_zero_node;
4124 /* Fold a call to __builtin_classify_type. */
4127 fold_builtin_classify_type (arglist)
4131 return build_int_2 (no_type_class, 0);
4133 return build_int_2 (type_to_class (TREE_TYPE (TREE_VALUE (arglist))), 0);
4136 /* Fold a call to __builtin_inf or __builtin_huge_val. */
4139 fold_builtin_inf (type, warn)
4143 REAL_VALUE_TYPE real;
4145 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
4146 warning ("target format does not support infinity");
4149 return build_real (type, real);
4152 /* Used by constant folding to eliminate some builtin calls early. EXP is
4153 the CALL_EXPR of a call to a builtin function. */
4159 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4160 tree arglist = TREE_OPERAND (exp, 1);
4161 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4163 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4168 case BUILT_IN_CONSTANT_P:
4169 return fold_builtin_constant_p (arglist);
4171 case BUILT_IN_CLASSIFY_TYPE:
4172 return fold_builtin_classify_type (arglist);
4174 case BUILT_IN_STRLEN:
4175 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
4177 tree len = c_strlen (TREE_VALUE (arglist));
4186 return fold_builtin_inf (TREE_TYPE (TREE_TYPE (fndecl)), true);
4188 case BUILT_IN_HUGE_VAL:
4189 case BUILT_IN_HUGE_VALF:
4190 case BUILT_IN_HUGE_VALL:
4191 return fold_builtin_inf (TREE_TYPE (TREE_TYPE (fndecl)), false);
4201 build_function_call_expr (fn, arglist)
4206 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
4207 call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
4208 call_expr, arglist);
4209 TREE_SIDE_EFFECTS (call_expr) = 1;
4210 return fold (call_expr);
4213 /* This function validates the types of a function call argument list
4214 represented as a tree chain of parameters against a specified list
4215 of tree_codes. If the last specifier is a 0, that represents an
4216 ellipses, otherwise the last specifier must be a VOID_TYPE. */
4219 validate_arglist VPARAMS ((tree arglist, ...))
4221 enum tree_code code;
4224 VA_OPEN (ap, arglist);
4225 VA_FIXEDARG (ap, tree, arglist);
4228 code = va_arg (ap, enum tree_code);
4232 /* This signifies an ellipses, any further arguments are all ok. */
4236 /* This signifies an endlink, if no arguments remain, return
4237 true, otherwise return false. */
4241 /* If no parameters remain or the parameter's code does not
4242 match the specified code, return false. Otherwise continue
4243 checking any remaining arguments. */
4244 if (arglist == 0 || code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
4248 arglist = TREE_CHAIN (arglist);
4251 /* We need gotos here since we can only have one VA_CLOSE in a
4259 /* Default version of target-specific builtin setup that does nothing. */
4262 default_init_builtins ()
4266 /* Default target-specific builtin expander that does nothing. */
4269 default_expand_builtin (exp, target, subtarget, mode, ignore)
4270 tree exp ATTRIBUTE_UNUSED;
4271 rtx target ATTRIBUTE_UNUSED;
4272 rtx subtarget ATTRIBUTE_UNUSED;
4273 enum machine_mode mode ATTRIBUTE_UNUSED;
4274 int ignore ATTRIBUTE_UNUSED;