* config/arm/netbsd.h (TARGET_OS_CPP_BUILTINS): Use
[official-gcc.git] / gcc / builtins.c
bloba686c8dce9dfb74d785cf99b153a3ff609ebbd27
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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "machmode.h"
25 #include "real.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "obstack.h"
29 #include "flags.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "except.h"
33 #include "function.h"
34 #include "insn-config.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "libfuncs.h"
38 #include "recog.h"
39 #include "output.h"
40 #include "typeclass.h"
41 #include "toplev.h"
42 #include "predict.h"
43 #include "tm_p.h"
44 #include "target.h"
45 #include "langhooks.h"
47 #define CALLED_AS_BUILT_IN(NODE) \
48 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
50 /* Register mappings for target machines without register windows. */
51 #ifndef INCOMING_REGNO
52 #define INCOMING_REGNO(OUT) (OUT)
53 #endif
54 #ifndef OUTGOING_REGNO
55 #define OUTGOING_REGNO(IN) (IN)
56 #endif
58 #ifndef PAD_VARARGS_DOWN
59 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
60 #endif
62 /* Define the names of the builtin function types and codes. */
63 const char *const built_in_class_names[4]
64 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
66 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA) STRINGX(X),
67 const char *const built_in_names[(int) END_BUILTINS] =
69 #include "builtins.def"
71 #undef DEF_BUILTIN
73 /* Setup an array of _DECL trees, make sure each element is
74 initialized to NULL_TREE. */
75 tree built_in_decls[(int) END_BUILTINS];
77 static int get_pointer_alignment PARAMS ((tree, unsigned int));
78 static tree c_strlen PARAMS ((tree));
79 static const char *c_getstr PARAMS ((tree));
80 static rtx c_readstr PARAMS ((const char *,
81 enum machine_mode));
82 static int target_char_cast PARAMS ((tree, char *));
83 static rtx get_memory_rtx PARAMS ((tree));
84 static int apply_args_size PARAMS ((void));
85 static int apply_result_size PARAMS ((void));
86 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
87 static rtx result_vector PARAMS ((int, rtx));
88 #endif
89 static rtx expand_builtin_setjmp PARAMS ((tree, rtx));
90 static void expand_builtin_prefetch PARAMS ((tree));
91 static rtx expand_builtin_apply_args PARAMS ((void));
92 static rtx expand_builtin_apply_args_1 PARAMS ((void));
93 static rtx expand_builtin_apply PARAMS ((rtx, rtx, rtx));
94 static void expand_builtin_return PARAMS ((rtx));
95 static enum type_class type_to_class PARAMS ((tree));
96 static rtx expand_builtin_classify_type PARAMS ((tree));
97 static rtx expand_builtin_mathfn PARAMS ((tree, rtx, rtx));
98 static rtx expand_builtin_constant_p PARAMS ((tree));
99 static rtx expand_builtin_args_info PARAMS ((tree));
100 static rtx expand_builtin_next_arg PARAMS ((tree));
101 static rtx expand_builtin_va_start PARAMS ((int, tree));
102 static rtx expand_builtin_va_end PARAMS ((tree));
103 static rtx expand_builtin_va_copy PARAMS ((tree));
104 static rtx expand_builtin_memcmp PARAMS ((tree, tree, rtx,
105 enum machine_mode));
106 static rtx expand_builtin_strcmp PARAMS ((tree, rtx,
107 enum machine_mode));
108 static rtx expand_builtin_strncmp PARAMS ((tree, rtx,
109 enum machine_mode));
110 static rtx builtin_memcpy_read_str PARAMS ((PTR, HOST_WIDE_INT,
111 enum machine_mode));
112 static rtx expand_builtin_strcat PARAMS ((tree, rtx,
113 enum machine_mode));
114 static rtx expand_builtin_strncat PARAMS ((tree, rtx,
115 enum machine_mode));
116 static rtx expand_builtin_strspn PARAMS ((tree, rtx,
117 enum machine_mode));
118 static rtx expand_builtin_strcspn PARAMS ((tree, rtx,
119 enum machine_mode));
120 static rtx expand_builtin_memcpy PARAMS ((tree, rtx,
121 enum machine_mode));
122 static rtx expand_builtin_strcpy PARAMS ((tree, rtx,
123 enum machine_mode));
124 static rtx builtin_strncpy_read_str PARAMS ((PTR, HOST_WIDE_INT,
125 enum machine_mode));
126 static rtx expand_builtin_strncpy PARAMS ((tree, rtx,
127 enum machine_mode));
128 static rtx builtin_memset_read_str PARAMS ((PTR, HOST_WIDE_INT,
129 enum machine_mode));
130 static rtx builtin_memset_gen_str PARAMS ((PTR, HOST_WIDE_INT,
131 enum machine_mode));
132 static rtx expand_builtin_memset PARAMS ((tree, rtx,
133 enum machine_mode));
134 static rtx expand_builtin_bzero PARAMS ((tree));
135 static rtx expand_builtin_strlen PARAMS ((tree, rtx));
136 static rtx expand_builtin_strstr PARAMS ((tree, rtx,
137 enum machine_mode));
138 static rtx expand_builtin_strpbrk PARAMS ((tree, rtx,
139 enum machine_mode));
140 static rtx expand_builtin_strchr PARAMS ((tree, rtx,
141 enum machine_mode));
142 static rtx expand_builtin_strrchr PARAMS ((tree, rtx,
143 enum machine_mode));
144 static rtx expand_builtin_alloca PARAMS ((tree, rtx));
145 static rtx expand_builtin_ffs PARAMS ((tree, rtx, rtx));
146 static rtx expand_builtin_frame_address PARAMS ((tree));
147 static rtx expand_builtin_fputs PARAMS ((tree, int, int));
148 static tree stabilize_va_list PARAMS ((tree, int));
149 static rtx expand_builtin_expect PARAMS ((tree, rtx));
150 static tree fold_builtin_constant_p PARAMS ((tree));
151 static tree fold_builtin_classify_type PARAMS ((tree));
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. */
163 static int
164 get_pointer_alignment (exp, max_align)
165 tree exp;
166 unsigned int max_align;
168 unsigned int align, inner;
170 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
171 return 0;
173 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
174 align = MIN (align, max_align);
176 while (1)
178 switch (TREE_CODE (exp))
180 case NOP_EXPR:
181 case CONVERT_EXPR:
182 case NON_LVALUE_EXPR:
183 exp = TREE_OPERAND (exp, 0);
184 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
185 return align;
187 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
188 align = MIN (inner, max_align);
189 break;
191 case PLUS_EXPR:
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
194 ALIGN. */
195 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
196 return align;
198 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
199 & (max_align / BITS_PER_UNIT - 1))
200 != 0)
201 max_align >>= 1;
203 exp = TREE_OPERAND (exp, 0);
204 break;
206 case ADDR_EXPR:
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);
216 #endif
217 return MIN (align, max_align);
219 default:
220 return 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. */
234 static tree
235 c_strlen (src)
236 tree src;
238 tree offset_node;
239 HOST_WIDE_INT offset;
240 int max;
241 const char *ptr;
243 src = string_constant (src, &offset_node);
244 if (src == 0)
245 return 0;
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. */
255 int i;
257 for (i = 0; i < max; i++)
258 if (ptr[i] == 0)
259 return 0;
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)
274 offset = 0;
275 else if (! host_integerp (offset_node, 0))
276 offset = -1;
277 else
278 offset = tree_low_cst (offset_node, 0);
280 /* If the offset is known to be out of bounds, warn, and call strlen at
281 runtime. */
282 if (offset < 0 || offset > max)
284 warning ("offset outside bounds of constant string");
285 return 0;
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. */
300 static const char *
301 c_getstr (src)
302 tree src;
304 tree offset_node;
306 src = string_constant (src, &offset_node);
307 if (src == 0)
308 return 0;
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)
314 return 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. */
322 static rtx
323 c_readstr (str, mode)
324 const char *str;
325 enum machine_mode mode;
327 HOST_WIDE_INT c[2];
328 HOST_WIDE_INT ch;
329 unsigned int i, j;
331 if (GET_MODE_CLASS (mode) != MODE_INT)
332 abort ();
333 c[0] = 0;
334 c[1] = 0;
335 ch = 1;
336 for (i = 0; i < GET_MODE_SIZE (mode); i++)
338 j = 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;
344 j *= BITS_PER_UNIT;
345 if (j > 2 * HOST_BITS_PER_WIDE_INT)
346 abort ();
347 if (ch)
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
356 P. */
358 static int
359 target_char_cast (cst, p)
360 tree cst;
361 char *p;
363 unsigned HOST_WIDE_INT val, hostval;
365 if (!host_integerp (cst, 1)
366 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
367 return 1;
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;
373 hostval = val;
374 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
375 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
377 if (val != hostval)
378 return 1;
380 *p = hostval;
381 return 0;
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;
391 int count;
392 rtx tem;
394 int i;
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
400 if (count > 0)
401 SETUP_FRAME_ADDRESSES ();
402 #endif
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)
410 count--;
411 #endif
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);
420 #endif
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)
429 return tem;
431 /* For __builtin_return_address, Get the return address from that
432 frame. */
433 #ifdef RETURN_ADDR_RTX
434 tem = RETURN_ADDR_RTX (count, tem);
435 #else
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 ());
440 #endif
441 return tem;
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
449 handling code. */
451 void
452 expand_builtin_setjmp_setup (buf_addr, receiver_label)
453 rtx buf_addr;
454 rtx receiver_label;
456 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
457 rtx stack_save;
458 rtx mem;
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);
466 #endif
468 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
470 emit_queue ();
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
478 #endif
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));
500 #endif
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. */
514 void
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)
531 #endif
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
538 size_t i;
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)
544 break;
546 if (i == ARRAY_SIZE (elim_regs))
547 #endif
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)));
555 #endif
557 #ifdef HAVE_builtin_setjmp_receiver
558 if (HAVE_builtin_setjmp_receiver)
559 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
560 else
561 #endif
562 #ifdef HAVE_nonlocal_goto_receiver
563 if (HAVE_nonlocal_goto_receiver)
564 emit_insn (gen_nonlocal_goto_receiver ());
565 else
566 #endif
567 { /* Nothing */ }
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
573 insn. */
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
581 non-local gotos.
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
585 them. */
587 static rtx
588 expand_builtin_setjmp (arglist, target)
589 tree arglist;
590 rtx target;
592 rtx buf_addr, next_lab, cont_lab;
594 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
595 return NULL_RTX;
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));
611 emit_barrier ();
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);
628 return target;
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
638 them. */
640 void
641 expand_builtin_longjmp (buf_addr, value)
642 rtx 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);
653 #endif
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)
663 abort ();
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));
671 else
672 #endif
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));
692 else
693 #endif
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))
713 if (insn == last)
714 abort ();
715 if (GET_CODE (insn) == JUMP_INSN)
717 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
718 REG_NOTES (insn));
719 break;
721 else if (GET_CODE (insn) == CALL_INSN)
722 break;
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
728 effects. */
730 static void
731 expand_builtin_prefetch (arglist)
732 tree arglist;
734 tree arg0, arg1, arg2;
735 rtx op0, op1, op2;
737 if (!validate_arglist (arglist, POINTER_TYPE, 0))
738 return;
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
743 locality). */
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)));
749 else
750 arg2 = build_int_2 (3, 0);
752 else
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");
772 op1 = const0_rtx;
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");
786 op2 = const0_rtx;
789 #ifdef HAVE_prefetch
790 if (HAVE_prefetch)
792 if ((! (*insn_data[(int)CODE_FOR_prefetch].operand[0].predicate)
793 (op0,
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);
800 #endif
801 op0 = force_reg (Pmode, op0);
803 emit_insn (gen_prefetch (op0, op1, op2));
805 else
806 #endif
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))
811 emit_insn (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, ..). */
817 static rtx
818 get_memory_rtx (exp)
819 tree exp;
821 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM);
822 rtx mem;
824 #ifdef POINTERS_EXTEND_UNSIGNED
825 if (GET_MODE (addr) != Pmode)
826 addr = convert_memory_address (Pmode, addr);
827 #endif
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);
851 return mem;
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)
882 int regno;
884 apply_args_size ();
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);
890 #endif
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. */
897 static int
898 apply_args_size ()
900 static int size = -1;
901 int align;
902 unsigned int regno;
903 enum machine_mode mode;
905 /* The values computed by this function never change. */
906 if (size < 0)
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);
924 mode != VOIDmode;
925 mode = GET_MODE_WIDER_MODE (mode))
926 if (HARD_REGNO_MODE_OK (regno, mode)
927 && HARD_REGNO_NREGS (regno, mode) == 1)
928 best_mode = mode;
930 if (best_mode == VOIDmode)
931 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
932 mode != VOIDmode;
933 mode = GET_MODE_WIDER_MODE (mode))
934 if (HARD_REGNO_MODE_OK (regno, mode)
935 && have_insn_for (SET, mode))
936 best_mode = mode;
938 if (best_mode == VOIDmode)
939 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
940 mode != VOIDmode;
941 mode = GET_MODE_WIDER_MODE (mode))
942 if (HARD_REGNO_MODE_OK (regno, mode)
943 && have_insn_for (SET, mode))
944 best_mode = mode;
946 if (best_mode == VOIDmode)
947 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
948 mode != VOIDmode;
949 mode = GET_MODE_WIDER_MODE (mode))
950 if (HARD_REGNO_MODE_OK (regno, mode)
951 && have_insn_for (SET, mode))
952 best_mode = mode;
954 mode = best_mode;
955 if (mode == VOIDmode)
956 abort ();
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;
965 else
967 apply_args_mode[regno] = VOIDmode;
968 apply_args_reg_offset[regno] = 0;
971 return size;
974 /* Return the size required for the block returned by __builtin_apply,
975 and initialize apply_result_mode. */
977 static int
978 apply_result_size ()
980 static int size = -1;
981 int align, regno;
982 enum machine_mode mode;
984 /* The values computed by this function never change. */
985 if (size < 0)
987 size = 0;
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);
997 mode != TImode;
998 mode = GET_MODE_WIDER_MODE (mode))
999 if (HARD_REGNO_MODE_OK (regno, mode))
1000 best_mode = mode;
1002 if (best_mode == VOIDmode)
1003 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
1004 mode != VOIDmode;
1005 mode = GET_MODE_WIDER_MODE (mode))
1006 if (HARD_REGNO_MODE_OK (regno, mode)
1007 && have_insn_for (SET, mode))
1008 best_mode = mode;
1010 if (best_mode == VOIDmode)
1011 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
1012 mode != VOIDmode;
1013 mode = GET_MODE_WIDER_MODE (mode))
1014 if (HARD_REGNO_MODE_OK (regno, mode)
1015 && have_insn_for (SET, mode))
1016 best_mode = mode;
1018 if (best_mode == VOIDmode)
1019 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
1020 mode != VOIDmode;
1021 mode = GET_MODE_WIDER_MODE (mode))
1022 if (HARD_REGNO_MODE_OK (regno, mode)
1023 && have_insn_for (SET, mode))
1024 best_mode = mode;
1026 mode = best_mode;
1027 if (mode == VOIDmode)
1028 abort ();
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;
1036 else
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;
1043 #endif
1045 return 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. */
1053 static rtx
1054 result_vector (savep, result)
1055 int savep;
1056 rtx result;
1058 int regno, size, align, nelts;
1059 enum machine_mode mode;
1060 rtx reg, mem;
1061 rtx *savevec = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1063 size = nelts = 0;
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. */
1084 static rtx
1085 expand_builtin_apply_args_1 ()
1087 rtx registers;
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)
1104 rtx tem;
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
1139 saved. */
1141 static rtx
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. */
1152 rtx temp;
1153 rtx seq;
1155 start_sequence ();
1156 temp = expand_builtin_apply_args_1 ();
1157 seq = get_insns ();
1158 end_sequence ();
1160 apply_args_value = temp;
1162 /* Put the sequence after the NOTE that starts the function.
1163 If this is inside a SEQUENCE, make the outer-level insn
1164 chain current, so the code is placed at the start of the
1165 function. */
1166 push_topmost_sequence ();
1167 emit_insns_before (seq, NEXT_INSN (get_insns ()));
1168 pop_topmost_sequence ();
1169 return temp;
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. */
1176 static rtx
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);
1189 #endif
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);
1200 #endif
1202 /* Perform postincrements before actually calling the function. */
1203 emit_queue ();
1205 /* Push a new argument block and copy the arguments. Do not allow
1206 the (potential) memcpy call below to interfere with our stack
1207 manipulations. */
1208 do_pending_stack_adjust ();
1209 NO_DEFER_POP;
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);
1215 else
1216 #endif
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);
1231 /* Refer to the argument block. */
1232 apply_args_size ();
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)));
1282 else
1283 #endif
1284 #ifdef HAVE_call_value
1285 if (HAVE_call_value)
1287 rtx valreg = 0;
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)
1296 if (valreg)
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);
1307 else
1308 #endif
1309 abort ();
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))
1317 if (! call_insn)
1318 abort ();
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))
1324 rtx link;
1326 for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
1327 link = XEXP (link, 1))
1330 XEXP (link, 1) = call_fusage;
1332 else
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);
1339 else
1340 #endif
1341 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1343 OK_DEFER_POP;
1345 /* Return the address of the result block. */
1346 return copy_addr_to_reg (XEXP (result, 0));
1349 /* Perform an untyped return. */
1351 static void
1352 expand_builtin_return (result)
1353 rtx result;
1355 int size, align, regno;
1356 enum machine_mode mode;
1357 rtx reg;
1358 rtx call_fusage = 0;
1360 #ifdef POINTERS_EXTEND_UNSIGNED
1361 if (GET_MODE (result) != Pmode)
1362 result = convert_memory_address (Pmode, result);
1363 #endif
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)));
1372 emit_barrier ();
1373 return;
1375 #endif
1377 /* Restore the return value and note that each value is used. */
1378 size = 0;
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 ();
1391 end_sequence ();
1392 size += GET_MODE_SIZE (mode);
1395 /* Put the USE insns before the return. */
1396 emit_insns (call_fusage);
1398 /* Return whatever values was restored by jumping directly to the end
1399 of the function. */
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)
1407 tree 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;
1424 case UNION_TYPE:
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
1436 ARGLIST. */
1438 static rtx
1439 expand_builtin_classify_type (arglist)
1440 tree arglist;
1442 if (arglist != 0)
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. */
1449 static rtx
1450 expand_builtin_constant_p (exp)
1451 tree exp;
1453 tree arglist = TREE_OPERAND (exp, 1);
1454 enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1455 rtx tmp;
1457 if (arglist == 0)
1458 return const0_rtx;
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);
1467 return 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. */
1476 static rtx
1477 expand_builtin_mathfn (exp, target, subtarget)
1478 tree exp;
1479 rtx target, subtarget;
1481 optab builtin_optab;
1482 rtx op0, insns;
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))
1488 return 0;
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)));
1511 emit_queue ();
1512 start_sequence ();
1514 switch (DECL_FUNCTION_CODE (fndecl))
1516 case BUILT_IN_SIN:
1517 case BUILT_IN_SINF:
1518 case BUILT_IN_SINL:
1519 builtin_optab = sin_optab; break;
1520 case BUILT_IN_COS:
1521 case BUILT_IN_COSF:
1522 case BUILT_IN_COSL:
1523 builtin_optab = cos_optab; break;
1524 case BUILT_IN_SQRT:
1525 case BUILT_IN_SQRTF:
1526 case BUILT_IN_SQRTL:
1527 builtin_optab = sqrt_optab; break;
1528 default:
1529 abort ();
1532 /* Compute into TARGET.
1533 Set TARGET to wherever the result comes back. */
1534 argmode = TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist)));
1535 target = expand_unop (argmode, builtin_optab, op0, target, 0);
1537 /* If we were unable to expand via the builtin, stop the
1538 sequence (without outputting the insns) and return 0, causing
1539 a call to the library function. */
1540 if (target == 0)
1542 end_sequence ();
1543 return 0;
1546 /* If errno must be maintained, we must set it to EDOM for NaN results. */
1548 if (flag_errno_math && HONOR_NANS (argmode))
1550 rtx lab1;
1552 lab1 = gen_label_rtx ();
1554 /* Test the result; if it is NaN, set errno=EDOM because
1555 the argument was not in the domain. */
1556 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1557 0, lab1);
1559 #ifdef TARGET_EDOM
1561 #ifdef GEN_ERRNO_RTX
1562 rtx errno_rtx = GEN_ERRNO_RTX;
1563 #else
1564 rtx errno_rtx
1565 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1566 #endif
1568 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1570 #else
1571 /* We can't set errno=EDOM directly; let the library call do it.
1572 Pop the arguments right away in case the call gets deleted. */
1573 NO_DEFER_POP;
1574 expand_call (exp, target, 0);
1575 OK_DEFER_POP;
1576 #endif
1578 emit_label (lab1);
1581 /* Output the entire sequence. */
1582 insns = get_insns ();
1583 end_sequence ();
1584 emit_insns (insns);
1586 return target;
1589 /* Expand expression EXP which is a call to the strlen builtin. Return 0
1590 if we failed the caller should emit a normal call, otherwise
1591 try to get the result in TARGET, if convenient. */
1593 static rtx
1594 expand_builtin_strlen (exp, target)
1595 tree exp;
1596 rtx target;
1598 tree arglist = TREE_OPERAND (exp, 1);
1599 enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1601 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
1602 return 0;
1603 else
1605 rtx pat;
1606 tree src = TREE_VALUE (arglist);
1608 int align
1609 = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1611 rtx result, src_reg, char_rtx, before_strlen;
1612 enum machine_mode insn_mode = value_mode, char_mode;
1613 enum insn_code icode = CODE_FOR_nothing;
1615 /* If SRC is not a pointer type, don't do this operation inline. */
1616 if (align == 0)
1617 return 0;
1619 /* Bail out if we can't compute strlen in the right mode. */
1620 while (insn_mode != VOIDmode)
1622 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
1623 if (icode != CODE_FOR_nothing)
1624 break;
1626 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
1628 if (insn_mode == VOIDmode)
1629 return 0;
1631 /* Make a place to write the result of the instruction. */
1632 result = target;
1633 if (! (result != 0
1634 && GET_CODE (result) == REG
1635 && GET_MODE (result) == insn_mode
1636 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
1637 result = gen_reg_rtx (insn_mode);
1639 /* Make a place to hold the source address. We will not expand
1640 the actual source until we are sure that the expansion will
1641 not fail -- there are trees that cannot be expanded twice. */
1642 src_reg = gen_reg_rtx (Pmode);
1644 /* Mark the beginning of the strlen sequence so we can emit the
1645 source operand later. */
1646 before_strlen = get_last_insn();
1648 char_rtx = const0_rtx;
1649 char_mode = insn_data[(int) icode].operand[2].mode;
1650 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
1651 char_mode))
1652 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
1654 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
1655 char_rtx, GEN_INT (align));
1656 if (! pat)
1657 return 0;
1658 emit_insn (pat);
1660 /* Now that we are assured of success, expand the source. */
1661 start_sequence ();
1662 pat = memory_address (BLKmode,
1663 expand_expr (src, src_reg, ptr_mode, EXPAND_SUM));
1664 if (pat != src_reg)
1665 emit_move_insn (src_reg, pat);
1666 pat = gen_sequence ();
1667 end_sequence ();
1669 if (before_strlen)
1670 emit_insn_after (pat, before_strlen);
1671 else
1672 emit_insn_before (pat, get_insns ());
1674 /* Return the value in the proper mode for this function. */
1675 if (GET_MODE (result) == value_mode)
1676 target = result;
1677 else if (target != 0)
1678 convert_move (target, result, 0);
1679 else
1680 target = convert_to_mode (value_mode, result, 0);
1682 return target;
1686 /* Expand a call to the strstr builtin. Return 0 if we failed the
1687 caller should emit a normal call, otherwise try to get the result
1688 in TARGET, if convenient (and in mode MODE if that's convenient). */
1690 static rtx
1691 expand_builtin_strstr (arglist, target, mode)
1692 tree arglist;
1693 rtx target;
1694 enum machine_mode mode;
1696 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1697 return 0;
1698 else
1700 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1701 tree fn;
1702 const char *p1, *p2;
1704 p2 = c_getstr (s2);
1705 if (p2 == NULL)
1706 return 0;
1708 p1 = c_getstr (s1);
1709 if (p1 != NULL)
1711 const char *r = strstr (p1, p2);
1713 if (r == NULL)
1714 return const0_rtx;
1716 /* Return an offset into the constant string argument. */
1717 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1718 s1, ssize_int (r - p1))),
1719 target, mode, EXPAND_NORMAL);
1722 if (p2[0] == '\0')
1723 return expand_expr (s1, target, mode, EXPAND_NORMAL);
1725 if (p2[1] != '\0')
1726 return 0;
1728 fn = built_in_decls[BUILT_IN_STRCHR];
1729 if (!fn)
1730 return 0;
1732 /* New argument list transforming strstr(s1, s2) to
1733 strchr(s1, s2[0]). */
1734 arglist =
1735 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
1736 arglist = tree_cons (NULL_TREE, s1, arglist);
1737 return expand_expr (build_function_call_expr (fn, arglist),
1738 target, mode, EXPAND_NORMAL);
1742 /* Expand a call to the strchr builtin. Return 0 if we failed the
1743 caller should emit a normal call, otherwise try to get the result
1744 in TARGET, if convenient (and in mode MODE if that's convenient). */
1746 static rtx
1747 expand_builtin_strchr (arglist, target, mode)
1748 tree arglist;
1749 rtx target;
1750 enum machine_mode mode;
1752 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
1753 return 0;
1754 else
1756 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1757 const char *p1;
1759 if (TREE_CODE (s2) != INTEGER_CST)
1760 return 0;
1762 p1 = c_getstr (s1);
1763 if (p1 != NULL)
1765 char c;
1766 const char *r;
1768 if (target_char_cast (s2, &c))
1769 return 0;
1771 r = strchr (p1, c);
1773 if (r == NULL)
1774 return const0_rtx;
1776 /* Return an offset into the constant string argument. */
1777 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1778 s1, ssize_int (r - p1))),
1779 target, mode, EXPAND_NORMAL);
1782 /* FIXME: Should use here strchrM optab so that ports can optimize
1783 this. */
1784 return 0;
1788 /* Expand a call to the strrchr builtin. Return 0 if we failed the
1789 caller should emit a normal call, otherwise try to get the result
1790 in TARGET, if convenient (and in mode MODE if that's convenient). */
1792 static rtx
1793 expand_builtin_strrchr (arglist, target, mode)
1794 tree arglist;
1795 rtx target;
1796 enum machine_mode mode;
1798 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
1799 return 0;
1800 else
1802 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1803 tree fn;
1804 const char *p1;
1806 if (TREE_CODE (s2) != INTEGER_CST)
1807 return 0;
1809 p1 = c_getstr (s1);
1810 if (p1 != NULL)
1812 char c;
1813 const char *r;
1815 if (target_char_cast (s2, &c))
1816 return 0;
1818 r = strrchr (p1, c);
1820 if (r == NULL)
1821 return const0_rtx;
1823 /* Return an offset into the constant string argument. */
1824 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1825 s1, ssize_int (r - p1))),
1826 target, mode, EXPAND_NORMAL);
1829 if (! integer_zerop (s2))
1830 return 0;
1832 fn = built_in_decls[BUILT_IN_STRCHR];
1833 if (!fn)
1834 return 0;
1836 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
1837 return expand_expr (build_function_call_expr (fn, arglist),
1838 target, mode, EXPAND_NORMAL);
1842 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
1843 caller should emit a normal call, otherwise try to get the result
1844 in TARGET, if convenient (and in mode MODE if that's convenient). */
1846 static rtx
1847 expand_builtin_strpbrk (arglist, target, mode)
1848 tree arglist;
1849 rtx target;
1850 enum machine_mode mode;
1852 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1853 return 0;
1854 else
1856 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1857 tree fn;
1858 const char *p1, *p2;
1860 p2 = c_getstr (s2);
1861 if (p2 == NULL)
1862 return 0;
1864 p1 = c_getstr (s1);
1865 if (p1 != NULL)
1867 const char *r = strpbrk (p1, p2);
1869 if (r == NULL)
1870 return const0_rtx;
1872 /* Return an offset into the constant string argument. */
1873 return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1874 s1, ssize_int (r - p1))),
1875 target, mode, EXPAND_NORMAL);
1878 if (p2[0] == '\0')
1880 /* strpbrk(x, "") == NULL.
1881 Evaluate and ignore the arguments in case they had
1882 side-effects. */
1883 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
1884 return const0_rtx;
1887 if (p2[1] != '\0')
1888 return 0; /* Really call strpbrk. */
1890 fn = built_in_decls[BUILT_IN_STRCHR];
1891 if (!fn)
1892 return 0;
1894 /* New argument list transforming strpbrk(s1, s2) to
1895 strchr(s1, s2[0]). */
1896 arglist =
1897 build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
1898 arglist = tree_cons (NULL_TREE, s1, arglist);
1899 return expand_expr (build_function_call_expr (fn, arglist),
1900 target, mode, EXPAND_NORMAL);
1904 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
1905 bytes from constant string DATA + OFFSET and return it as target
1906 constant. */
1908 static rtx
1909 builtin_memcpy_read_str (data, offset, mode)
1910 PTR data;
1911 HOST_WIDE_INT offset;
1912 enum machine_mode mode;
1914 const char *str = (const char *) data;
1916 if (offset < 0
1917 || ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
1918 > strlen (str) + 1))
1919 abort (); /* Attempt to read past the end of constant string. */
1921 return c_readstr (str + offset, mode);
1924 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
1925 Return 0 if we failed, the caller should emit a normal call, otherwise
1926 try to get the result in TARGET, if convenient (and in mode MODE if
1927 that's convenient). */
1929 static rtx
1930 expand_builtin_memcpy (arglist, target, mode)
1931 tree arglist;
1932 rtx target;
1933 enum machine_mode mode;
1935 if (!validate_arglist (arglist,
1936 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
1937 return 0;
1938 else
1940 tree dest = TREE_VALUE (arglist);
1941 tree src = TREE_VALUE (TREE_CHAIN (arglist));
1942 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1943 const char *src_str;
1945 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
1946 unsigned int dest_align
1947 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
1948 rtx dest_mem, src_mem, dest_addr, len_rtx;
1950 /* If DEST is not a pointer type, call the normal function. */
1951 if (dest_align == 0)
1952 return 0;
1954 /* If the LEN parameter is zero, return DEST. */
1955 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
1957 /* Evaluate and ignore SRC in case it has side-effects. */
1958 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
1959 return expand_expr (dest, target, mode, EXPAND_NORMAL);
1962 /* If either SRC is not a pointer type, don't do this
1963 operation in-line. */
1964 if (src_align == 0)
1965 return 0;
1967 dest_mem = get_memory_rtx (dest);
1968 set_mem_align (dest_mem, dest_align);
1969 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
1970 src_str = c_getstr (src);
1972 /* If SRC is a string constant and block move would be done
1973 by pieces, we can avoid loading the string from memory
1974 and only stored the computed constants. */
1975 if (src_str
1976 && GET_CODE (len_rtx) == CONST_INT
1977 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
1978 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
1979 (PTR) src_str, dest_align))
1981 store_by_pieces (dest_mem, INTVAL (len_rtx),
1982 builtin_memcpy_read_str,
1983 (PTR) src_str, dest_align);
1984 return force_operand (XEXP (dest_mem, 0), NULL_RTX);
1987 src_mem = get_memory_rtx (src);
1988 set_mem_align (src_mem, src_align);
1990 /* Copy word part most expediently. */
1991 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx);
1993 if (dest_addr == 0)
1994 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
1996 return dest_addr;
2000 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2001 if we failed the caller should emit a normal call, otherwise try to get
2002 the result in TARGET, if convenient (and in mode MODE if that's
2003 convenient). */
2005 static rtx
2006 expand_builtin_strcpy (exp, target, mode)
2007 tree exp;
2008 rtx target;
2009 enum machine_mode mode;
2011 tree arglist = TREE_OPERAND (exp, 1);
2012 tree fn, len;
2014 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2015 return 0;
2017 fn = built_in_decls[BUILT_IN_MEMCPY];
2018 if (!fn)
2019 return 0;
2021 len = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)));
2022 if (len == 0)
2023 return 0;
2025 len = size_binop (PLUS_EXPR, len, ssize_int (1));
2026 chainon (arglist, build_tree_list (NULL_TREE, len));
2027 return expand_expr (build_function_call_expr (fn, arglist),
2028 target, mode, EXPAND_NORMAL);
2031 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2032 bytes from constant string DATA + OFFSET and return it as target
2033 constant. */
2035 static rtx
2036 builtin_strncpy_read_str (data, offset, mode)
2037 PTR data;
2038 HOST_WIDE_INT offset;
2039 enum machine_mode mode;
2041 const char *str = (const char *) data;
2043 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
2044 return const0_rtx;
2046 return c_readstr (str + offset, mode);
2049 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
2050 if we failed the caller should emit a normal call. */
2052 static rtx
2053 expand_builtin_strncpy (arglist, target, mode)
2054 tree arglist;
2055 rtx target;
2056 enum machine_mode mode;
2058 if (!validate_arglist (arglist,
2059 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2060 return 0;
2061 else
2063 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)));
2064 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2065 tree fn;
2067 /* We must be passed a constant len parameter. */
2068 if (TREE_CODE (len) != INTEGER_CST)
2069 return 0;
2071 /* If the len parameter is zero, return the dst parameter. */
2072 if (integer_zerop (len))
2074 /* Evaluate and ignore the src argument in case it has
2075 side-effects. */
2076 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
2077 VOIDmode, EXPAND_NORMAL);
2078 /* Return the dst parameter. */
2079 return expand_expr (TREE_VALUE (arglist), target, mode,
2080 EXPAND_NORMAL);
2083 /* Now, we must be passed a constant src ptr parameter. */
2084 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
2085 return 0;
2087 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
2089 /* We're required to pad with trailing zeros if the requested
2090 len is greater than strlen(s2)+1. In that case try to
2091 use store_by_pieces, if it fails, punt. */
2092 if (tree_int_cst_lt (slen, len))
2094 tree dest = TREE_VALUE (arglist);
2095 unsigned int dest_align
2096 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2097 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
2098 rtx dest_mem;
2100 if (!p || dest_align == 0 || !host_integerp (len, 1)
2101 || !can_store_by_pieces (tree_low_cst (len, 1),
2102 builtin_strncpy_read_str,
2103 (PTR) p, dest_align))
2104 return 0;
2106 dest_mem = get_memory_rtx (dest);
2107 store_by_pieces (dest_mem, tree_low_cst (len, 1),
2108 builtin_strncpy_read_str,
2109 (PTR) p, dest_align);
2110 return force_operand (XEXP (dest_mem, 0), NULL_RTX);
2113 /* OK transform into builtin memcpy. */
2114 fn = built_in_decls[BUILT_IN_MEMCPY];
2115 if (!fn)
2116 return 0;
2117 return expand_expr (build_function_call_expr (fn, arglist),
2118 target, mode, EXPAND_NORMAL);
2122 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2123 bytes from constant string DATA + OFFSET and return it as target
2124 constant. */
2126 static rtx
2127 builtin_memset_read_str (data, offset, mode)
2128 PTR data;
2129 HOST_WIDE_INT offset ATTRIBUTE_UNUSED;
2130 enum machine_mode mode;
2132 const char *c = (const char *) data;
2133 char *p = alloca (GET_MODE_SIZE (mode));
2135 memset (p, *c, GET_MODE_SIZE (mode));
2137 return c_readstr (p, mode);
2140 /* Callback routine for store_by_pieces. Return the RTL of a register
2141 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
2142 char value given in the RTL register data. For example, if mode is
2143 4 bytes wide, return the RTL for 0x01010101*data. */
2145 static rtx
2146 builtin_memset_gen_str (data, offset, mode)
2147 PTR data;
2148 HOST_WIDE_INT offset ATTRIBUTE_UNUSED;
2149 enum machine_mode mode;
2151 rtx target, coeff;
2152 size_t size;
2153 char *p;
2155 size = GET_MODE_SIZE (mode);
2156 if (size==1)
2157 return (rtx)data;
2159 p = alloca (size);
2160 memset (p, 1, size);
2161 coeff = c_readstr (p, mode);
2163 target = convert_to_mode (mode, (rtx)data, 1);
2164 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
2165 return force_reg (mode, target);
2168 /* Expand expression EXP, which is a call to the memset builtin. Return 0
2169 if we failed the caller should emit a normal call, otherwise try to get
2170 the result in TARGET, if convenient (and in mode MODE if that's
2171 convenient). */
2173 static rtx
2174 expand_builtin_memset (exp, target, mode)
2175 tree exp;
2176 rtx target;
2177 enum machine_mode mode;
2179 tree arglist = TREE_OPERAND (exp, 1);
2181 if (!validate_arglist (arglist,
2182 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
2183 return 0;
2184 else
2186 tree dest = TREE_VALUE (arglist);
2187 tree val = TREE_VALUE (TREE_CHAIN (arglist));
2188 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2189 char c;
2191 unsigned int dest_align
2192 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2193 rtx dest_mem, dest_addr, len_rtx;
2195 /* If DEST is not a pointer type, don't do this
2196 operation in-line. */
2197 if (dest_align == 0)
2198 return 0;
2200 /* If the LEN parameter is zero, return DEST. */
2201 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
2203 /* Evaluate and ignore VAL in case it has side-effects. */
2204 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
2205 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2208 if (TREE_CODE (val) != INTEGER_CST)
2210 rtx val_rtx;
2212 if (!host_integerp (len, 1))
2213 return 0;
2215 if (optimize_size && tree_low_cst (len, 1) > 1)
2216 return 0;
2218 /* Assume that we can memset by pieces if we can store the
2219 * the coefficients by pieces (in the required modes).
2220 * We can't pass builtin_memset_gen_str as that emits RTL. */
2221 c = 1;
2222 if (!can_store_by_pieces (tree_low_cst (len, 1),
2223 builtin_memset_read_str,
2224 (PTR) &c, dest_align))
2225 return 0;
2227 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
2228 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
2229 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
2230 val_rtx);
2231 dest_mem = get_memory_rtx (dest);
2232 store_by_pieces (dest_mem, tree_low_cst (len, 1),
2233 builtin_memset_gen_str,
2234 (PTR)val_rtx, dest_align);
2235 return force_operand (XEXP (dest_mem, 0), NULL_RTX);
2238 if (target_char_cast (val, &c))
2239 return 0;
2241 if (c)
2243 if (!host_integerp (len, 1))
2244 return 0;
2245 if (!can_store_by_pieces (tree_low_cst (len, 1),
2246 builtin_memset_read_str, (PTR) &c,
2247 dest_align))
2248 return 0;
2250 dest_mem = get_memory_rtx (dest);
2251 store_by_pieces (dest_mem, tree_low_cst (len, 1),
2252 builtin_memset_read_str,
2253 (PTR) &c, dest_align);
2254 return force_operand (XEXP (dest_mem, 0), NULL_RTX);
2257 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2259 dest_mem = get_memory_rtx (dest);
2260 set_mem_align (dest_mem, dest_align);
2261 dest_addr = clear_storage (dest_mem, len_rtx);
2263 if (dest_addr == 0)
2264 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2266 return dest_addr;
2270 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
2271 if we failed the caller should emit a normal call. */
2273 static rtx
2274 expand_builtin_bzero (exp)
2275 tree exp;
2277 tree arglist = TREE_OPERAND (exp, 1);
2278 tree dest, size, newarglist;
2279 rtx result;
2281 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2282 return NULL_RTX;
2284 dest = TREE_VALUE (arglist);
2285 size = TREE_VALUE (TREE_CHAIN (arglist));
2287 /* New argument list transforming bzero(ptr x, int y) to
2288 memset(ptr x, int 0, size_t y). This is done this way
2289 so that if it isn't expanded inline, we fallback to
2290 calling bzero instead of memset. */
2292 newarglist = build_tree_list (NULL_TREE, convert (sizetype, size));
2293 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
2294 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2296 TREE_OPERAND (exp, 1) = newarglist;
2297 result = expand_builtin_memset (exp, const0_rtx, VOIDmode);
2299 /* Always restore the original arguments. */
2300 TREE_OPERAND (exp, 1) = arglist;
2302 return result;
2305 /* Expand expression EXP, which is a call to the memcmp or the strcmp builtin.
2306 ARGLIST is the argument list for this call. Return 0 if we failed and the
2307 caller should emit a normal call, otherwise try to get the result in
2308 TARGET, if convenient (and in mode MODE, if that's convenient). */
2310 static rtx
2311 expand_builtin_memcmp (exp, arglist, target, mode)
2312 tree exp ATTRIBUTE_UNUSED;
2313 tree arglist;
2314 rtx target;
2315 enum machine_mode mode;
2317 tree arg1, arg2, len;
2318 const char *p1, *p2;
2320 if (!validate_arglist (arglist,
2321 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2322 return 0;
2324 arg1 = TREE_VALUE (arglist);
2325 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
2326 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2328 /* If the len parameter is zero, return zero. */
2329 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
2331 /* Evaluate and ignore arg1 and arg2 in case they have
2332 side-effects. */
2333 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2334 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2335 return const0_rtx;
2338 p1 = c_getstr (arg1);
2339 p2 = c_getstr (arg2);
2341 /* If all arguments are constant, and the value of len is not greater
2342 than the lengths of arg1 and arg2, evaluate at compile-time. */
2343 if (host_integerp (len, 1) && p1 && p2
2344 && compare_tree_int (len, strlen (p1) + 1) <= 0
2345 && compare_tree_int (len, strlen (p2) + 1) <= 0)
2347 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
2349 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
2352 /* If len parameter is one, return an expression corresponding to
2353 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
2354 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
2356 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2357 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
2358 tree ind1 =
2359 fold (build1 (CONVERT_EXPR, integer_type_node,
2360 build1 (INDIRECT_REF, cst_uchar_node,
2361 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
2362 tree ind2 =
2363 fold (build1 (CONVERT_EXPR, integer_type_node,
2364 build1 (INDIRECT_REF, cst_uchar_node,
2365 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
2366 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
2367 return expand_expr (result, target, mode, EXPAND_NORMAL);
2370 #ifdef HAVE_cmpstrsi
2372 rtx arg1_rtx, arg2_rtx, arg3_rtx;
2373 rtx result;
2374 rtx insn;
2376 int arg1_align
2377 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2378 int arg2_align
2379 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2380 enum machine_mode insn_mode
2381 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
2383 /* If we don't have POINTER_TYPE, call the function. */
2384 if (arg1_align == 0 || arg2_align == 0)
2385 return 0;
2387 /* Make a place to write the result of the instruction. */
2388 result = target;
2389 if (! (result != 0
2390 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
2391 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2392 result = gen_reg_rtx (insn_mode);
2394 arg1_rtx = get_memory_rtx (arg1);
2395 arg2_rtx = get_memory_rtx (arg2);
2396 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2397 if (!HAVE_cmpstrsi)
2398 insn = NULL_RTX;
2399 else
2400 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
2401 GEN_INT (MIN (arg1_align, arg2_align)));
2403 if (insn)
2404 emit_insn (insn);
2405 else
2406 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
2407 TYPE_MODE (integer_type_node), 3,
2408 XEXP (arg1_rtx, 0), Pmode,
2409 XEXP (arg2_rtx, 0), Pmode,
2410 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
2411 TREE_UNSIGNED (sizetype)),
2412 TYPE_MODE (sizetype));
2414 /* Return the value in the proper mode for this function. */
2415 mode = TYPE_MODE (TREE_TYPE (exp));
2416 if (GET_MODE (result) == mode)
2417 return result;
2418 else if (target != 0)
2420 convert_move (target, result, 0);
2421 return target;
2423 else
2424 return convert_to_mode (mode, result, 0);
2426 #endif
2428 return 0;
2431 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
2432 if we failed the caller should emit a normal call, otherwise try to get
2433 the result in TARGET, if convenient. */
2435 static rtx
2436 expand_builtin_strcmp (exp, target, mode)
2437 tree exp;
2438 rtx target;
2439 enum machine_mode mode;
2441 tree arglist = TREE_OPERAND (exp, 1);
2442 tree arg1, arg2, len, len2, fn;
2443 const char *p1, *p2;
2445 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2446 return 0;
2448 arg1 = TREE_VALUE (arglist);
2449 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
2451 p1 = c_getstr (arg1);
2452 p2 = c_getstr (arg2);
2454 if (p1 && p2)
2456 const int i = strcmp (p1, p2);
2457 return (i < 0 ? constm1_rtx : (i > 0 ? const1_rtx : const0_rtx));
2460 /* If either arg is "", return an expression corresponding to
2461 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
2462 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
2464 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2465 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
2466 tree ind1 =
2467 fold (build1 (CONVERT_EXPR, integer_type_node,
2468 build1 (INDIRECT_REF, cst_uchar_node,
2469 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
2470 tree ind2 =
2471 fold (build1 (CONVERT_EXPR, integer_type_node,
2472 build1 (INDIRECT_REF, cst_uchar_node,
2473 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
2474 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
2475 return expand_expr (result, target, mode, EXPAND_NORMAL);
2478 len = c_strlen (arg1);
2479 len2 = c_strlen (arg2);
2481 if (len)
2482 len = size_binop (PLUS_EXPR, ssize_int (1), len);
2484 if (len2)
2485 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
2487 /* If we don't have a constant length for the first, use the length
2488 of the second, if we know it. We don't require a constant for
2489 this case; some cost analysis could be done if both are available
2490 but neither is constant. For now, assume they're equally cheap
2491 unless one has side effects.
2493 If both strings have constant lengths, use the smaller. This
2494 could arise if optimization results in strcpy being called with
2495 two fixed strings, or if the code was machine-generated. We should
2496 add some code to the `memcmp' handler below to deal with such
2497 situations, someday. */
2499 if (!len || TREE_CODE (len) != INTEGER_CST)
2501 if (len2 && !TREE_SIDE_EFFECTS (len2))
2502 len = len2;
2503 else if (len == 0)
2504 return 0;
2506 else if (len2 && TREE_CODE (len2) == INTEGER_CST
2507 && tree_int_cst_lt (len2, len))
2508 len = len2;
2510 /* If both arguments have side effects, we cannot optimize. */
2511 if (TREE_SIDE_EFFECTS (len))
2512 return 0;
2514 fn = built_in_decls[BUILT_IN_MEMCMP];
2515 if (!fn)
2516 return 0;
2518 chainon (arglist, build_tree_list (NULL_TREE, len));
2519 return expand_expr (build_function_call_expr (fn, arglist),
2520 target, mode, EXPAND_NORMAL);
2523 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
2524 if we failed the caller should emit a normal call, otherwise try to get
2525 the result in TARGET, if convenient. */
2527 static rtx
2528 expand_builtin_strncmp (exp, target, mode)
2529 tree exp;
2530 rtx target;
2531 enum machine_mode mode;
2533 tree arglist = TREE_OPERAND (exp, 1);
2534 tree fn, newarglist, len = 0;
2535 tree arg1, arg2, arg3;
2536 const char *p1, *p2;
2538 if (!validate_arglist (arglist,
2539 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2540 return 0;
2542 arg1 = TREE_VALUE (arglist);
2543 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
2544 arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2546 /* If the len parameter is zero, return zero. */
2547 if (host_integerp (arg3, 1) && tree_low_cst (arg3, 1) == 0)
2549 /* Evaluate and ignore arg1 and arg2 in case they have
2550 side-effects. */
2551 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2552 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2553 return const0_rtx;
2556 p1 = c_getstr (arg1);
2557 p2 = c_getstr (arg2);
2559 /* If all arguments are constant, evaluate at compile-time. */
2560 if (host_integerp (arg3, 1) && p1 && p2)
2562 const int r = strncmp (p1, p2, tree_low_cst (arg3, 1));
2563 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
2566 /* If len == 1 or (either string parameter is "" and (len >= 1)),
2567 return (*(const u_char*)arg1 - *(const u_char*)arg2). */
2568 if (host_integerp (arg3, 1)
2569 && (tree_low_cst (arg3, 1) == 1
2570 || (tree_low_cst (arg3, 1) > 1
2571 && ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')))))
2573 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2574 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
2575 tree ind1 =
2576 fold (build1 (CONVERT_EXPR, integer_type_node,
2577 build1 (INDIRECT_REF, cst_uchar_node,
2578 build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
2579 tree ind2 =
2580 fold (build1 (CONVERT_EXPR, integer_type_node,
2581 build1 (INDIRECT_REF, cst_uchar_node,
2582 build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
2583 tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
2584 return expand_expr (result, target, mode, EXPAND_NORMAL);
2587 /* If c_strlen can determine an expression for one of the string
2588 lengths, and it doesn't have side effects, then call
2589 expand_builtin_memcmp() using length MIN(strlen(string)+1, arg3). */
2591 /* Perhaps one of the strings is really constant, if so prefer
2592 that constant length over the other string's length. */
2593 if (p1)
2594 len = c_strlen (arg1);
2595 else if (p2)
2596 len = c_strlen (arg2);
2598 /* If we still don't have a len, try either string arg as long
2599 as they don't have side effects. */
2600 if (!len && !TREE_SIDE_EFFECTS (arg1))
2601 len = c_strlen (arg1);
2602 if (!len && !TREE_SIDE_EFFECTS (arg2))
2603 len = c_strlen (arg2);
2604 /* If we still don't have a length, punt. */
2605 if (!len)
2606 return 0;
2608 fn = built_in_decls[BUILT_IN_MEMCMP];
2609 if (!fn)
2610 return 0;
2612 /* Add one to the string length. */
2613 len = fold (size_binop (PLUS_EXPR, len, ssize_int (1)));
2615 /* The actual new length parameter is MIN(len,arg3). */
2616 len = fold (build (MIN_EXPR, TREE_TYPE (len), len, arg3));
2618 newarglist = build_tree_list (NULL_TREE, len);
2619 newarglist = tree_cons (NULL_TREE, arg2, newarglist);
2620 newarglist = tree_cons (NULL_TREE, arg1, newarglist);
2621 return expand_expr (build_function_call_expr (fn, newarglist),
2622 target, mode, EXPAND_NORMAL);
2625 /* Expand expression EXP, which is a call to the strcat builtin.
2626 Return 0 if we failed the caller should emit a normal call,
2627 otherwise try to get the result in TARGET, if convenient. */
2629 static rtx
2630 expand_builtin_strcat (arglist, target, mode)
2631 tree arglist;
2632 rtx target;
2633 enum machine_mode mode;
2635 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2636 return 0;
2637 else
2639 tree dst = TREE_VALUE (arglist),
2640 src = TREE_VALUE (TREE_CHAIN (arglist));
2641 const char *p = c_getstr (src);
2643 /* If the string length is zero, return the dst parameter. */
2644 if (p && *p == '\0')
2645 return expand_expr (dst, target, mode, EXPAND_NORMAL);
2647 return 0;
2651 /* Expand expression EXP, which is a call to the strncat builtin.
2652 Return 0 if we failed the caller should emit a normal call,
2653 otherwise try to get the result in TARGET, if convenient. */
2655 static rtx
2656 expand_builtin_strncat (arglist, target, mode)
2657 tree arglist;
2658 rtx target;
2659 enum machine_mode mode;
2661 if (!validate_arglist (arglist,
2662 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2663 return 0;
2664 else
2666 tree dst = TREE_VALUE (arglist),
2667 src = TREE_VALUE (TREE_CHAIN (arglist)),
2668 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2669 const char *p = c_getstr (src);
2671 /* If the requested length is zero, or the src parameter string
2672 length is zero, return the dst parameter. */
2673 if (integer_zerop (len) || (p && *p == '\0'))
2675 /* Evaluate and ignore the src and len parameters in case
2676 they have side-effects. */
2677 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2678 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2679 return expand_expr (dst, target, mode, EXPAND_NORMAL);
2682 /* If the requested len is greater than or equal to the string
2683 length, call strcat. */
2684 if (TREE_CODE (len) == INTEGER_CST && p
2685 && compare_tree_int (len, strlen (p)) >= 0)
2687 tree newarglist
2688 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
2689 tree fn = built_in_decls[BUILT_IN_STRCAT];
2691 /* If the replacement _DECL isn't initialized, don't do the
2692 transformation. */
2693 if (!fn)
2694 return 0;
2696 return expand_expr (build_function_call_expr (fn, newarglist),
2697 target, mode, EXPAND_NORMAL);
2699 return 0;
2703 /* Expand expression EXP, which is a call to the strspn builtin.
2704 Return 0 if we failed the caller should emit a normal call,
2705 otherwise try to get the result in TARGET, if convenient. */
2707 static rtx
2708 expand_builtin_strspn (arglist, target, mode)
2709 tree arglist;
2710 rtx target;
2711 enum machine_mode mode;
2713 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2714 return 0;
2715 else
2717 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2718 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
2720 /* If both arguments are constants, evaluate at compile-time. */
2721 if (p1 && p2)
2723 const size_t r = strspn (p1, p2);
2724 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
2727 /* If either argument is "", return 0. */
2728 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
2730 /* Evaluate and ignore both arguments in case either one has
2731 side-effects. */
2732 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2733 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2734 return const0_rtx;
2736 return 0;
2740 /* Expand expression EXP, which is a call to the strcspn builtin.
2741 Return 0 if we failed the caller should emit a normal call,
2742 otherwise try to get the result in TARGET, if convenient. */
2744 static rtx
2745 expand_builtin_strcspn (arglist, target, mode)
2746 tree arglist;
2747 rtx target;
2748 enum machine_mode mode;
2750 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2751 return 0;
2752 else
2754 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2755 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
2757 /* If both arguments are constants, evaluate at compile-time. */
2758 if (p1 && p2)
2760 const size_t r = strcspn (p1, p2);
2761 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
2764 /* If the first argument is "", return 0. */
2765 if (p1 && *p1 == '\0')
2767 /* Evaluate and ignore argument s2 in case it has
2768 side-effects. */
2769 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2770 return const0_rtx;
2773 /* If the second argument is "", return __builtin_strlen(s1). */
2774 if (p2 && *p2 == '\0')
2776 tree newarglist = build_tree_list (NULL_TREE, s1),
2777 fn = built_in_decls[BUILT_IN_STRLEN];
2779 /* If the replacement _DECL isn't initialized, don't do the
2780 transformation. */
2781 if (!fn)
2782 return 0;
2784 return expand_expr (build_function_call_expr (fn, newarglist),
2785 target, mode, EXPAND_NORMAL);
2787 return 0;
2791 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
2792 if that's convenient. */
2795 expand_builtin_saveregs ()
2797 rtx val, seq;
2799 /* Don't do __builtin_saveregs more than once in a function.
2800 Save the result of the first call and reuse it. */
2801 if (saveregs_value != 0)
2802 return saveregs_value;
2804 /* When this function is called, it means that registers must be
2805 saved on entry to this function. So we migrate the call to the
2806 first insn of this function. */
2808 start_sequence ();
2810 #ifdef EXPAND_BUILTIN_SAVEREGS
2811 /* Do whatever the machine needs done in this case. */
2812 val = EXPAND_BUILTIN_SAVEREGS ();
2813 #else
2814 /* ??? We used to try and build up a call to the out of line function,
2815 guessing about what registers needed saving etc. This became much
2816 harder with __builtin_va_start, since we don't have a tree for a
2817 call to __builtin_saveregs to fall back on. There was exactly one
2818 port (i860) that used this code, and I'm unconvinced it could actually
2819 handle the general case. So we no longer try to handle anything
2820 weird and make the backend absorb the evil. */
2822 error ("__builtin_saveregs not supported by this target");
2823 val = const0_rtx;
2824 #endif
2826 seq = get_insns ();
2827 end_sequence ();
2829 saveregs_value = val;
2831 /* Put the sequence after the NOTE that starts the function. If this
2832 is inside a SEQUENCE, make the outer-level insn chain current, so
2833 the code is placed at the start of the function. */
2834 push_topmost_sequence ();
2835 emit_insns_after (seq, get_insns ());
2836 pop_topmost_sequence ();
2838 return val;
2841 /* __builtin_args_info (N) returns word N of the arg space info
2842 for the current function. The number and meanings of words
2843 is controlled by the definition of CUMULATIVE_ARGS. */
2845 static rtx
2846 expand_builtin_args_info (exp)
2847 tree exp;
2849 tree arglist = TREE_OPERAND (exp, 1);
2850 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
2851 int *word_ptr = (int *) &current_function_args_info;
2852 #if 0
2853 /* These are used by the code below that is if 0'ed away */
2854 int i;
2855 tree type, elts, result;
2856 #endif
2858 if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0)
2859 abort ();
2861 if (arglist != 0)
2863 if (!host_integerp (TREE_VALUE (arglist), 0))
2864 error ("argument of `__builtin_args_info' must be constant");
2865 else
2867 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
2869 if (wordnum < 0 || wordnum >= nwords)
2870 error ("argument of `__builtin_args_info' out of range");
2871 else
2872 return GEN_INT (word_ptr[wordnum]);
2875 else
2876 error ("missing argument in `__builtin_args_info'");
2878 return const0_rtx;
2880 #if 0
2881 for (i = 0; i < nwords; i++)
2882 elts = tree_cons (NULL_TREE, build_int_2 (word_ptr[i], 0));
2884 type = build_array_type (integer_type_node,
2885 build_index_type (build_int_2 (nwords, 0)));
2886 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (elts));
2887 TREE_CONSTANT (result) = 1;
2888 TREE_STATIC (result) = 1;
2889 result = build1 (INDIRECT_REF, build_pointer_type (type), result);
2890 TREE_CONSTANT (result) = 1;
2891 return expand_expr (result, NULL_RTX, VOIDmode, 0);
2892 #endif
2895 /* Expand ARGLIST, from a call to __builtin_next_arg. */
2897 static rtx
2898 expand_builtin_next_arg (arglist)
2899 tree arglist;
2901 tree fntype = TREE_TYPE (current_function_decl);
2903 if ((TYPE_ARG_TYPES (fntype) == 0
2904 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
2905 == void_type_node))
2906 && ! current_function_varargs)
2908 error ("`va_start' used in function with fixed args");
2909 return const0_rtx;
2912 if (arglist)
2914 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
2915 tree arg = TREE_VALUE (arglist);
2917 /* Strip off all nops for the sake of the comparison. This
2918 is not quite the same as STRIP_NOPS. It does more.
2919 We must also strip off INDIRECT_EXPR for C++ reference
2920 parameters. */
2921 while (TREE_CODE (arg) == NOP_EXPR
2922 || TREE_CODE (arg) == CONVERT_EXPR
2923 || TREE_CODE (arg) == NON_LVALUE_EXPR
2924 || TREE_CODE (arg) == INDIRECT_REF)
2925 arg = TREE_OPERAND (arg, 0);
2926 if (arg != last_parm)
2927 warning ("second parameter of `va_start' not last named argument");
2929 else if (! current_function_varargs)
2930 /* Evidently an out of date version of <stdarg.h>; can't validate
2931 va_start's second argument, but can still work as intended. */
2932 warning ("`__builtin_next_arg' called without an argument");
2934 return expand_binop (Pmode, add_optab,
2935 current_function_internal_arg_pointer,
2936 current_function_arg_offset_rtx,
2937 NULL_RTX, 0, OPTAB_LIB_WIDEN);
2940 /* Make it easier for the backends by protecting the valist argument
2941 from multiple evaluations. */
2943 static tree
2944 stabilize_va_list (valist, needs_lvalue)
2945 tree valist;
2946 int needs_lvalue;
2948 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2950 if (TREE_SIDE_EFFECTS (valist))
2951 valist = save_expr (valist);
2953 /* For this case, the backends will be expecting a pointer to
2954 TREE_TYPE (va_list_type_node), but it's possible we've
2955 actually been given an array (an actual va_list_type_node).
2956 So fix it. */
2957 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
2959 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
2960 tree p2 = build_pointer_type (va_list_type_node);
2962 valist = build1 (ADDR_EXPR, p2, valist);
2963 valist = fold (build1 (NOP_EXPR, p1, valist));
2966 else
2968 tree pt;
2970 if (! needs_lvalue)
2972 if (! TREE_SIDE_EFFECTS (valist))
2973 return valist;
2975 pt = build_pointer_type (va_list_type_node);
2976 valist = fold (build1 (ADDR_EXPR, pt, valist));
2977 TREE_SIDE_EFFECTS (valist) = 1;
2980 if (TREE_SIDE_EFFECTS (valist))
2981 valist = save_expr (valist);
2982 valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)),
2983 valist));
2986 return valist;
2989 /* The "standard" implementation of va_start: just assign `nextarg' to
2990 the variable. */
2992 void
2993 std_expand_builtin_va_start (stdarg_p, valist, nextarg)
2994 int stdarg_p;
2995 tree valist;
2996 rtx nextarg;
2998 tree t;
3000 if (! stdarg_p)
3002 /* The dummy named parameter is declared as a 'word' sized
3003 object, but if a 'word' is smaller than an 'int', it would
3004 have been promoted to int when it was added to the arglist. */
3005 int align = PARM_BOUNDARY / BITS_PER_UNIT;
3006 int size = MAX (UNITS_PER_WORD,
3007 GET_MODE_SIZE (TYPE_MODE (integer_type_node)));
3008 int offset = ((size + align - 1) / align) * align;
3009 nextarg = plus_constant (nextarg, -offset);
3012 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
3013 make_tree (ptr_type_node, nextarg));
3014 TREE_SIDE_EFFECTS (t) = 1;
3016 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3019 /* Expand ARGLIST, which from a call to __builtin_stdarg_va_start or
3020 __builtin_varargs_va_start, depending on STDARG_P. */
3022 static rtx
3023 expand_builtin_va_start (stdarg_p, arglist)
3024 int stdarg_p;
3025 tree arglist;
3027 rtx nextarg;
3028 tree chain = arglist, valist;
3030 if (stdarg_p)
3031 nextarg = expand_builtin_next_arg (chain = TREE_CHAIN (arglist));
3032 else
3033 nextarg = expand_builtin_next_arg (NULL_TREE);
3035 if (TREE_CHAIN (chain))
3036 error ("too many arguments to function `va_start'");
3038 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
3040 #ifdef EXPAND_BUILTIN_VA_START
3041 EXPAND_BUILTIN_VA_START (stdarg_p, valist, nextarg);
3042 #else
3043 std_expand_builtin_va_start (stdarg_p, valist, nextarg);
3044 #endif
3046 return const0_rtx;
3049 /* The "standard" implementation of va_arg: read the value from the
3050 current (padded) address and increment by the (padded) size. */
3053 std_expand_builtin_va_arg (valist, type)
3054 tree valist, type;
3056 tree addr_tree, t, type_size = NULL;
3057 tree align, alignm1;
3058 tree rounded_size;
3059 rtx addr;
3061 /* Compute the rounded size of the type. */
3062 align = size_int (PARM_BOUNDARY / BITS_PER_UNIT);
3063 alignm1 = size_int (PARM_BOUNDARY / BITS_PER_UNIT - 1);
3064 if (type == error_mark_node
3065 || (type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type))) == NULL
3066 || TREE_OVERFLOW (type_size))
3067 rounded_size = size_zero_node;
3068 else
3069 rounded_size = fold (build (MULT_EXPR, sizetype,
3070 fold (build (TRUNC_DIV_EXPR, sizetype,
3071 fold (build (PLUS_EXPR, sizetype,
3072 type_size, alignm1)),
3073 align)),
3074 align));
3076 /* Get AP. */
3077 addr_tree = valist;
3078 if (PAD_VARARGS_DOWN && ! integer_zerop (rounded_size))
3080 /* Small args are padded downward. */
3081 addr_tree = fold (build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
3082 fold (build (COND_EXPR, sizetype,
3083 fold (build (GT_EXPR, sizetype,
3084 rounded_size,
3085 align)),
3086 size_zero_node,
3087 fold (build (MINUS_EXPR, sizetype,
3088 rounded_size,
3089 type_size))))));
3092 addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
3093 addr = copy_to_reg (addr);
3095 /* Compute new value for AP. */
3096 if (! integer_zerop (rounded_size))
3098 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
3099 build (PLUS_EXPR, TREE_TYPE (valist), valist,
3100 rounded_size));
3101 TREE_SIDE_EFFECTS (t) = 1;
3102 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3105 return addr;
3108 /* Expand __builtin_va_arg, which is not really a builtin function, but
3109 a very special sort of operator. */
3112 expand_builtin_va_arg (valist, type)
3113 tree valist, type;
3115 rtx addr, result;
3116 tree promoted_type, want_va_type, have_va_type;
3118 /* Verify that valist is of the proper type. */
3120 want_va_type = va_list_type_node;
3121 have_va_type = TREE_TYPE (valist);
3122 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
3124 /* If va_list is an array type, the argument may have decayed
3125 to a pointer type, e.g. by being passed to another function.
3126 In that case, unwrap both types so that we can compare the
3127 underlying records. */
3128 if (TREE_CODE (have_va_type) == ARRAY_TYPE
3129 || TREE_CODE (have_va_type) == POINTER_TYPE)
3131 want_va_type = TREE_TYPE (want_va_type);
3132 have_va_type = TREE_TYPE (have_va_type);
3135 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
3137 error ("first argument to `va_arg' not of type `va_list'");
3138 addr = const0_rtx;
3141 /* Generate a diagnostic for requesting data of a type that cannot
3142 be passed through `...' due to type promotion at the call site. */
3143 else if ((promoted_type = (*lang_hooks.types.type_promotes_to) (type))
3144 != type)
3146 const char *name = "<anonymous type>", *pname = 0;
3147 static bool gave_help;
3149 if (TYPE_NAME (type))
3151 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3152 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3153 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
3154 && DECL_NAME (TYPE_NAME (type)))
3155 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3157 if (TYPE_NAME (promoted_type))
3159 if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE)
3160 pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type));
3161 else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL
3162 && DECL_NAME (TYPE_NAME (promoted_type)))
3163 pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type)));
3166 /* Unfortunately, this is merely undefined, rather than a constraint
3167 violation, so we cannot make this an error. If this call is never
3168 executed, the program is still strictly conforming. */
3169 warning ("`%s' is promoted to `%s' when passed through `...'",
3170 name, pname);
3171 if (! gave_help)
3173 gave_help = true;
3174 warning ("(so you should pass `%s' not `%s' to `va_arg')",
3175 pname, name);
3178 /* We can, however, treat "undefined" any way we please.
3179 Call abort to encourage the user to fix the program. */
3180 expand_builtin_trap ();
3182 /* This is dead code, but go ahead and finish so that the
3183 mode of the result comes out right. */
3184 addr = const0_rtx;
3186 else
3188 /* Make it easier for the backends by protecting the valist argument
3189 from multiple evaluations. */
3190 valist = stabilize_va_list (valist, 0);
3192 #ifdef EXPAND_BUILTIN_VA_ARG
3193 addr = EXPAND_BUILTIN_VA_ARG (valist, type);
3194 #else
3195 addr = std_expand_builtin_va_arg (valist, type);
3196 #endif
3199 #ifdef POINTERS_EXTEND_UNSIGNED
3200 if (GET_MODE (addr) != Pmode)
3201 addr = convert_memory_address (Pmode, addr);
3202 #endif
3204 result = gen_rtx_MEM (TYPE_MODE (type), addr);
3205 set_mem_alias_set (result, get_varargs_alias_set ());
3207 return result;
3210 /* Expand ARGLIST, from a call to __builtin_va_end. */
3212 static rtx
3213 expand_builtin_va_end (arglist)
3214 tree arglist;
3216 tree valist = TREE_VALUE (arglist);
3218 #ifdef EXPAND_BUILTIN_VA_END
3219 valist = stabilize_va_list (valist, 0);
3220 EXPAND_BUILTIN_VA_END(arglist);
3221 #else
3222 /* Evaluate for side effects, if needed. I hate macros that don't
3223 do that. */
3224 if (TREE_SIDE_EFFECTS (valist))
3225 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
3226 #endif
3228 return const0_rtx;
3231 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
3232 builtin rather than just as an assignment in stdarg.h because of the
3233 nastiness of array-type va_list types. */
3235 static rtx
3236 expand_builtin_va_copy (arglist)
3237 tree arglist;
3239 tree dst, src, t;
3241 dst = TREE_VALUE (arglist);
3242 src = TREE_VALUE (TREE_CHAIN (arglist));
3244 dst = stabilize_va_list (dst, 1);
3245 src = stabilize_va_list (src, 0);
3247 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
3249 t = build (MODIFY_EXPR, va_list_type_node, dst, src);
3250 TREE_SIDE_EFFECTS (t) = 1;
3251 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3253 else
3255 rtx dstb, srcb, size;
3257 /* Evaluate to pointers. */
3258 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
3259 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
3260 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
3261 VOIDmode, EXPAND_NORMAL);
3263 #ifdef POINTERS_EXTEND_UNSIGNED
3264 if (GET_MODE (dstb) != Pmode)
3265 dstb = convert_memory_address (Pmode, dstb);
3267 if (GET_MODE (srcb) != Pmode)
3268 srcb = convert_memory_address (Pmode, srcb);
3269 #endif
3271 /* "Dereference" to BLKmode memories. */
3272 dstb = gen_rtx_MEM (BLKmode, dstb);
3273 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
3274 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
3275 srcb = gen_rtx_MEM (BLKmode, srcb);
3276 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
3277 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
3279 /* Copy. */
3280 emit_block_move (dstb, srcb, size);
3283 return const0_rtx;
3286 /* Expand a call to one of the builtin functions __builtin_frame_address or
3287 __builtin_return_address. */
3289 static rtx
3290 expand_builtin_frame_address (exp)
3291 tree exp;
3293 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3294 tree arglist = TREE_OPERAND (exp, 1);
3296 /* The argument must be a nonnegative integer constant.
3297 It counts the number of frames to scan up the stack.
3298 The value is the return address saved in that frame. */
3299 if (arglist == 0)
3300 /* Warning about missing arg was already issued. */
3301 return const0_rtx;
3302 else if (! host_integerp (TREE_VALUE (arglist), 1))
3304 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
3305 error ("invalid arg to `__builtin_frame_address'");
3306 else
3307 error ("invalid arg to `__builtin_return_address'");
3308 return const0_rtx;
3310 else
3312 rtx tem
3313 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
3314 tree_low_cst (TREE_VALUE (arglist), 1),
3315 hard_frame_pointer_rtx);
3317 /* Some ports cannot access arbitrary stack frames. */
3318 if (tem == NULL)
3320 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
3321 warning ("unsupported arg to `__builtin_frame_address'");
3322 else
3323 warning ("unsupported arg to `__builtin_return_address'");
3324 return const0_rtx;
3327 /* For __builtin_frame_address, return what we've got. */
3328 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
3329 return tem;
3331 if (GET_CODE (tem) != REG
3332 && ! CONSTANT_P (tem))
3333 tem = copy_to_mode_reg (Pmode, tem);
3334 return tem;
3338 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
3339 we failed and the caller should emit a normal call, otherwise try to get
3340 the result in TARGET, if convenient. */
3342 static rtx
3343 expand_builtin_alloca (arglist, target)
3344 tree arglist;
3345 rtx target;
3347 rtx op0;
3348 rtx result;
3350 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
3351 return 0;
3353 /* Compute the argument. */
3354 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
3356 /* Allocate the desired space. */
3357 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
3359 #ifdef POINTERS_EXTEND_UNSIGNED
3360 if (GET_MODE (result) != ptr_mode)
3361 result = convert_memory_address (ptr_mode, result);
3362 #endif
3364 return result;
3367 /* Expand a call to the ffs builtin. The arguments are in ARGLIST.
3368 Return 0 if a normal call should be emitted rather than expanding the
3369 function in-line. If convenient, the result should be placed in TARGET.
3370 SUBTARGET may be used as the target for computing one of EXP's operands. */
3372 static rtx
3373 expand_builtin_ffs (arglist, target, subtarget)
3374 tree arglist;
3375 rtx target, subtarget;
3377 rtx op0;
3378 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
3379 return 0;
3381 /* Compute the argument. */
3382 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
3383 /* Compute ffs, into TARGET if possible.
3384 Set TARGET to wherever the result comes back. */
3385 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
3386 ffs_optab, op0, target, 1);
3387 if (target == 0)
3388 abort ();
3389 return target;
3392 /* If the string passed to fputs is a constant and is one character
3393 long, we attempt to transform this call into __builtin_fputc(). */
3395 static rtx
3396 expand_builtin_fputs (arglist, ignore, unlocked)
3397 tree arglist;
3398 int ignore;
3399 int unlocked;
3401 tree len, fn;
3402 tree fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
3403 : built_in_decls[BUILT_IN_FPUTC];
3404 tree fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
3405 : built_in_decls[BUILT_IN_FWRITE];
3407 /* If the return value is used, or the replacement _DECL isn't
3408 initialized, don't do the transformation. */
3409 if (!ignore || !fn_fputc || !fn_fwrite)
3410 return 0;
3412 /* Verify the arguments in the original call. */
3413 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3414 return 0;
3416 /* Get the length of the string passed to fputs. If the length
3417 can't be determined, punt. */
3418 if (!(len = c_strlen (TREE_VALUE (arglist)))
3419 || TREE_CODE (len) != INTEGER_CST)
3420 return 0;
3422 switch (compare_tree_int (len, 1))
3424 case -1: /* length is 0, delete the call entirely . */
3426 /* Evaluate and ignore the argument in case it has
3427 side-effects. */
3428 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
3429 VOIDmode, EXPAND_NORMAL);
3430 return const0_rtx;
3432 case 0: /* length is 1, call fputc. */
3434 const char *p = c_getstr (TREE_VALUE (arglist));
3436 if (p != NULL)
3438 /* New argument list transforming fputs(string, stream) to
3439 fputc(string[0], stream). */
3440 arglist =
3441 build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
3442 arglist =
3443 tree_cons (NULL_TREE, build_int_2 (p[0], 0), arglist);
3444 fn = fn_fputc;
3445 break;
3448 /* FALLTHROUGH */
3449 case 1: /* length is greater than 1, call fwrite. */
3451 tree string_arg = TREE_VALUE (arglist);
3453 /* New argument list transforming fputs(string, stream) to
3454 fwrite(string, 1, len, stream). */
3455 arglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
3456 arglist = tree_cons (NULL_TREE, len, arglist);
3457 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
3458 arglist = tree_cons (NULL_TREE, string_arg, arglist);
3459 fn = fn_fwrite;
3460 break;
3462 default:
3463 abort ();
3466 return expand_expr (build_function_call_expr (fn, arglist),
3467 (ignore ? const0_rtx : NULL_RTX),
3468 VOIDmode, EXPAND_NORMAL);
3471 /* Expand a call to __builtin_expect. We return our argument and emit a
3472 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
3473 a non-jump context. */
3475 static rtx
3476 expand_builtin_expect (arglist, target)
3477 tree arglist;
3478 rtx target;
3480 tree exp, c;
3481 rtx note, rtx_c;
3483 if (arglist == NULL_TREE
3484 || TREE_CHAIN (arglist) == NULL_TREE)
3485 return const0_rtx;
3486 exp = TREE_VALUE (arglist);
3487 c = TREE_VALUE (TREE_CHAIN (arglist));
3489 if (TREE_CODE (c) != INTEGER_CST)
3491 error ("second arg to `__builtin_expect' must be a constant");
3492 c = integer_zero_node;
3495 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
3497 /* Don't bother with expected value notes for integral constants. */
3498 if (GET_CODE (target) != CONST_INT)
3500 /* We do need to force this into a register so that we can be
3501 moderately sure to be able to correctly interpret the branch
3502 condition later. */
3503 target = force_reg (GET_MODE (target), target);
3505 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
3507 note = emit_note (NULL, NOTE_INSN_EXPECTED_VALUE);
3508 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
3511 return target;
3514 /* Like expand_builtin_expect, except do this in a jump context. This is
3515 called from do_jump if the conditional is a __builtin_expect. Return either
3516 a SEQUENCE of insns to emit the jump or NULL if we cannot optimize
3517 __builtin_expect. We need to optimize this at jump time so that machines
3518 like the PowerPC don't turn the test into a SCC operation, and then jump
3519 based on the test being 0/1. */
3522 expand_builtin_expect_jump (exp, if_false_label, if_true_label)
3523 tree exp;
3524 rtx if_false_label;
3525 rtx if_true_label;
3527 tree arglist = TREE_OPERAND (exp, 1);
3528 tree arg0 = TREE_VALUE (arglist);
3529 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3530 rtx ret = NULL_RTX;
3532 /* Only handle __builtin_expect (test, 0) and
3533 __builtin_expect (test, 1). */
3534 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
3535 && (integer_zerop (arg1) || integer_onep (arg1)))
3537 int j;
3538 int num_jumps = 0;
3540 /* If we fail to locate an appropriate conditional jump, we'll
3541 fall back to normal evaluation. Ensure that the expression
3542 can be re-evaluated. */
3543 switch (unsafe_for_reeval (arg0))
3545 case 0: /* Safe. */
3546 break;
3548 case 1: /* Mildly unsafe. */
3549 arg0 = unsave_expr (arg0);
3550 break;
3552 case 2: /* Wildly unsafe. */
3553 return NULL_RTX;
3556 /* Expand the jump insns. */
3557 start_sequence ();
3558 do_jump (arg0, if_false_label, if_true_label);
3559 ret = gen_sequence ();
3560 end_sequence ();
3562 /* Now that the __builtin_expect has been validated, go through and add
3563 the expect's to each of the conditional jumps. If we run into an
3564 error, just give up and generate the 'safe' code of doing a SCC
3565 operation and then doing a branch on that. */
3566 for (j = 0; j < XVECLEN (ret, 0); j++)
3568 rtx insn = XVECEXP (ret, 0, j);
3569 rtx pattern;
3571 if (GET_CODE (insn) == JUMP_INSN && any_condjump_p (insn)
3572 && (pattern = pc_set (insn)) != NULL_RTX)
3574 rtx ifelse = SET_SRC (pattern);
3575 rtx label;
3576 int taken;
3578 if (GET_CODE (ifelse) != IF_THEN_ELSE)
3579 continue;
3581 if (GET_CODE (XEXP (ifelse, 1)) == LABEL_REF)
3583 taken = 1;
3584 label = XEXP (XEXP (ifelse, 1), 0);
3586 /* An inverted jump reverses the probabilities. */
3587 else if (GET_CODE (XEXP (ifelse, 2)) == LABEL_REF)
3589 taken = 0;
3590 label = XEXP (XEXP (ifelse, 2), 0);
3592 /* We shouldn't have to worry about conditional returns during
3593 the expansion stage, but handle it gracefully anyway. */
3594 else if (GET_CODE (XEXP (ifelse, 1)) == RETURN)
3596 taken = 1;
3597 label = NULL_RTX;
3599 /* An inverted return reverses the probabilities. */
3600 else if (GET_CODE (XEXP (ifelse, 2)) == RETURN)
3602 taken = 0;
3603 label = NULL_RTX;
3605 else
3606 continue;
3608 /* If the test is expected to fail, reverse the
3609 probabilities. */
3610 if (integer_zerop (arg1))
3611 taken = 1 - taken;
3613 /* If we are jumping to the false label, reverse the
3614 probabilities. */
3615 if (label == NULL_RTX)
3616 ; /* conditional return */
3617 else if (label == if_false_label)
3618 taken = 1 - taken;
3619 else if (label != if_true_label)
3620 continue;
3622 num_jumps++;
3623 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
3627 /* If no jumps were modified, fail and do __builtin_expect the normal
3628 way. */
3629 if (num_jumps == 0)
3630 ret = NULL_RTX;
3633 return ret;
3636 void
3637 expand_builtin_trap ()
3639 #ifdef HAVE_trap
3640 if (HAVE_trap)
3641 emit_insn (gen_trap ());
3642 else
3643 #endif
3644 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
3645 emit_barrier ();
3648 /* Expand an expression EXP that calls a built-in function,
3649 with result going to TARGET if that's convenient
3650 (and in mode MODE if that's convenient).
3651 SUBTARGET may be used as the target for computing one of EXP's operands.
3652 IGNORE is nonzero if the value is to be ignored. */
3655 expand_builtin (exp, target, subtarget, mode, ignore)
3656 tree exp;
3657 rtx target;
3658 rtx subtarget;
3659 enum machine_mode mode;
3660 int ignore;
3662 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3663 tree arglist = TREE_OPERAND (exp, 1);
3664 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
3666 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
3667 return (*targetm.expand_builtin) (exp, target, subtarget, mode, ignore);
3669 /* When not optimizing, generate calls to library functions for a certain
3670 set of builtins. */
3671 if (!optimize && !CALLED_AS_BUILT_IN (fndecl))
3672 switch (fcode)
3674 case BUILT_IN_SIN:
3675 case BUILT_IN_COS:
3676 case BUILT_IN_SQRT:
3677 case BUILT_IN_SQRTF:
3678 case BUILT_IN_SQRTL:
3679 case BUILT_IN_MEMSET:
3680 case BUILT_IN_MEMCPY:
3681 case BUILT_IN_MEMCMP:
3682 case BUILT_IN_BCMP:
3683 case BUILT_IN_BZERO:
3684 case BUILT_IN_INDEX:
3685 case BUILT_IN_RINDEX:
3686 case BUILT_IN_STRCHR:
3687 case BUILT_IN_STRRCHR:
3688 case BUILT_IN_STRLEN:
3689 case BUILT_IN_STRCPY:
3690 case BUILT_IN_STRNCPY:
3691 case BUILT_IN_STRNCMP:
3692 case BUILT_IN_STRSTR:
3693 case BUILT_IN_STRPBRK:
3694 case BUILT_IN_STRCAT:
3695 case BUILT_IN_STRNCAT:
3696 case BUILT_IN_STRSPN:
3697 case BUILT_IN_STRCSPN:
3698 case BUILT_IN_STRCMP:
3699 case BUILT_IN_FFS:
3700 case BUILT_IN_PUTCHAR:
3701 case BUILT_IN_PUTS:
3702 case BUILT_IN_PRINTF:
3703 case BUILT_IN_FPUTC:
3704 case BUILT_IN_FPUTS:
3705 case BUILT_IN_FWRITE:
3706 case BUILT_IN_PUTCHAR_UNLOCKED:
3707 case BUILT_IN_PUTS_UNLOCKED:
3708 case BUILT_IN_PRINTF_UNLOCKED:
3709 case BUILT_IN_FPUTC_UNLOCKED:
3710 case BUILT_IN_FPUTS_UNLOCKED:
3711 case BUILT_IN_FWRITE_UNLOCKED:
3712 return expand_call (exp, target, ignore);
3714 default:
3715 break;
3718 switch (fcode)
3720 case BUILT_IN_ABS:
3721 case BUILT_IN_LABS:
3722 case BUILT_IN_LLABS:
3723 case BUILT_IN_IMAXABS:
3724 case BUILT_IN_FABS:
3725 case BUILT_IN_FABSF:
3726 case BUILT_IN_FABSL:
3727 /* build_function_call changes these into ABS_EXPR. */
3728 abort ();
3730 case BUILT_IN_CONJ:
3731 case BUILT_IN_CONJF:
3732 case BUILT_IN_CONJL:
3733 case BUILT_IN_CREAL:
3734 case BUILT_IN_CREALF:
3735 case BUILT_IN_CREALL:
3736 case BUILT_IN_CIMAG:
3737 case BUILT_IN_CIMAGF:
3738 case BUILT_IN_CIMAGL:
3739 /* expand_tree_builtin changes these into CONJ_EXPR, REALPART_EXPR
3740 and IMAGPART_EXPR. */
3741 abort ();
3743 case BUILT_IN_SIN:
3744 case BUILT_IN_SINF:
3745 case BUILT_IN_SINL:
3746 case BUILT_IN_COS:
3747 case BUILT_IN_COSF:
3748 case BUILT_IN_COSL:
3749 /* Treat these like sqrt only if unsafe math optimizations are allowed,
3750 because of possible accuracy problems. */
3751 if (! flag_unsafe_math_optimizations)
3752 break;
3753 case BUILT_IN_SQRT:
3754 case BUILT_IN_SQRTF:
3755 case BUILT_IN_SQRTL:
3756 target = expand_builtin_mathfn (exp, target, subtarget);
3757 if (target)
3758 return target;
3759 break;
3761 case BUILT_IN_FMOD:
3762 break;
3764 case BUILT_IN_APPLY_ARGS:
3765 return expand_builtin_apply_args ();
3767 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
3768 FUNCTION with a copy of the parameters described by
3769 ARGUMENTS, and ARGSIZE. It returns a block of memory
3770 allocated on the stack into which is stored all the registers
3771 that might possibly be used for returning the result of a
3772 function. ARGUMENTS is the value returned by
3773 __builtin_apply_args. ARGSIZE is the number of bytes of
3774 arguments that must be copied. ??? How should this value be
3775 computed? We'll also need a safe worst case value for varargs
3776 functions. */
3777 case BUILT_IN_APPLY:
3778 if (!validate_arglist (arglist, POINTER_TYPE,
3779 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
3780 && !validate_arglist (arglist, REFERENCE_TYPE,
3781 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3782 return const0_rtx;
3783 else
3785 int i;
3786 tree t;
3787 rtx ops[3];
3789 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
3790 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
3792 return expand_builtin_apply (ops[0], ops[1], ops[2]);
3795 /* __builtin_return (RESULT) causes the function to return the
3796 value described by RESULT. RESULT is address of the block of
3797 memory returned by __builtin_apply. */
3798 case BUILT_IN_RETURN:
3799 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
3800 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
3801 NULL_RTX, VOIDmode, 0));
3802 return const0_rtx;
3804 case BUILT_IN_SAVEREGS:
3805 return expand_builtin_saveregs ();
3807 case BUILT_IN_ARGS_INFO:
3808 return expand_builtin_args_info (exp);
3810 /* Return the address of the first anonymous stack arg. */
3811 case BUILT_IN_NEXT_ARG:
3812 return expand_builtin_next_arg (arglist);
3814 case BUILT_IN_CLASSIFY_TYPE:
3815 return expand_builtin_classify_type (arglist);
3817 case BUILT_IN_CONSTANT_P:
3818 return expand_builtin_constant_p (exp);
3820 case BUILT_IN_FRAME_ADDRESS:
3821 case BUILT_IN_RETURN_ADDRESS:
3822 return expand_builtin_frame_address (exp);
3824 /* Returns the address of the area where the structure is returned.
3825 0 otherwise. */
3826 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
3827 if (arglist != 0
3828 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
3829 || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM)
3830 return const0_rtx;
3831 else
3832 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
3834 case BUILT_IN_ALLOCA:
3835 target = expand_builtin_alloca (arglist, target);
3836 if (target)
3837 return target;
3838 break;
3840 case BUILT_IN_FFS:
3841 target = expand_builtin_ffs (arglist, target, subtarget);
3842 if (target)
3843 return target;
3844 break;
3846 case BUILT_IN_STRLEN:
3847 target = expand_builtin_strlen (exp, target);
3848 if (target)
3849 return target;
3850 break;
3852 case BUILT_IN_STRCPY:
3853 target = expand_builtin_strcpy (exp, target, mode);
3854 if (target)
3855 return target;
3856 break;
3858 case BUILT_IN_STRNCPY:
3859 target = expand_builtin_strncpy (arglist, target, mode);
3860 if (target)
3861 return target;
3862 break;
3864 case BUILT_IN_STRCAT:
3865 target = expand_builtin_strcat (arglist, target, mode);
3866 if (target)
3867 return target;
3868 break;
3870 case BUILT_IN_STRNCAT:
3871 target = expand_builtin_strncat (arglist, target, mode);
3872 if (target)
3873 return target;
3874 break;
3876 case BUILT_IN_STRSPN:
3877 target = expand_builtin_strspn (arglist, target, mode);
3878 if (target)
3879 return target;
3880 break;
3882 case BUILT_IN_STRCSPN:
3883 target = expand_builtin_strcspn (arglist, target, mode);
3884 if (target)
3885 return target;
3886 break;
3888 case BUILT_IN_STRSTR:
3889 target = expand_builtin_strstr (arglist, target, mode);
3890 if (target)
3891 return target;
3892 break;
3894 case BUILT_IN_STRPBRK:
3895 target = expand_builtin_strpbrk (arglist, target, mode);
3896 if (target)
3897 return target;
3898 break;
3900 case BUILT_IN_INDEX:
3901 case BUILT_IN_STRCHR:
3902 target = expand_builtin_strchr (arglist, target, mode);
3903 if (target)
3904 return target;
3905 break;
3907 case BUILT_IN_RINDEX:
3908 case BUILT_IN_STRRCHR:
3909 target = expand_builtin_strrchr (arglist, target, mode);
3910 if (target)
3911 return target;
3912 break;
3914 case BUILT_IN_MEMCPY:
3915 target = expand_builtin_memcpy (arglist, target, mode);
3916 if (target)
3917 return target;
3918 break;
3920 case BUILT_IN_MEMSET:
3921 target = expand_builtin_memset (exp, target, mode);
3922 if (target)
3923 return target;
3924 break;
3926 case BUILT_IN_BZERO:
3927 target = expand_builtin_bzero (exp);
3928 if (target)
3929 return target;
3930 break;
3932 case BUILT_IN_STRCMP:
3933 target = expand_builtin_strcmp (exp, target, mode);
3934 if (target)
3935 return target;
3936 break;
3938 case BUILT_IN_STRNCMP:
3939 target = expand_builtin_strncmp (exp, target, mode);
3940 if (target)
3941 return target;
3942 break;
3944 case BUILT_IN_BCMP:
3945 case BUILT_IN_MEMCMP:
3946 target = expand_builtin_memcmp (exp, arglist, target, mode);
3947 if (target)
3948 return target;
3949 break;
3951 case BUILT_IN_SETJMP:
3952 target = expand_builtin_setjmp (arglist, target);
3953 if (target)
3954 return target;
3955 break;
3957 /* __builtin_longjmp is passed a pointer to an array of five words.
3958 It's similar to the C library longjmp function but works with
3959 __builtin_setjmp above. */
3960 case BUILT_IN_LONGJMP:
3961 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3962 break;
3963 else
3965 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
3966 VOIDmode, 0);
3967 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
3968 NULL_RTX, VOIDmode, 0);
3970 if (value != const1_rtx)
3972 error ("__builtin_longjmp second argument must be 1");
3973 return const0_rtx;
3976 expand_builtin_longjmp (buf_addr, value);
3977 return const0_rtx;
3980 case BUILT_IN_TRAP:
3981 expand_builtin_trap ();
3982 return const0_rtx;
3984 case BUILT_IN_PUTCHAR:
3985 case BUILT_IN_PUTS:
3986 case BUILT_IN_FPUTC:
3987 case BUILT_IN_FWRITE:
3988 case BUILT_IN_PUTCHAR_UNLOCKED:
3989 case BUILT_IN_PUTS_UNLOCKED:
3990 case BUILT_IN_FPUTC_UNLOCKED:
3991 case BUILT_IN_FWRITE_UNLOCKED:
3992 break;
3993 case BUILT_IN_FPUTS:
3994 target = expand_builtin_fputs (arglist, ignore,/*unlocked=*/ 0);
3995 if (target)
3996 return target;
3997 break;
3998 case BUILT_IN_FPUTS_UNLOCKED:
3999 target = expand_builtin_fputs (arglist, ignore,/*unlocked=*/ 1);
4000 if (target)
4001 return target;
4002 break;
4004 /* Various hooks for the DWARF 2 __throw routine. */
4005 case BUILT_IN_UNWIND_INIT:
4006 expand_builtin_unwind_init ();
4007 return const0_rtx;
4008 case BUILT_IN_DWARF_CFA:
4009 return virtual_cfa_rtx;
4010 #ifdef DWARF2_UNWIND_INFO
4011 case BUILT_IN_DWARF_FP_REGNUM:
4012 return expand_builtin_dwarf_fp_regnum ();
4013 case BUILT_IN_INIT_DWARF_REG_SIZES:
4014 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
4015 return const0_rtx;
4016 #endif
4017 case BUILT_IN_FROB_RETURN_ADDR:
4018 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
4019 case BUILT_IN_EXTRACT_RETURN_ADDR:
4020 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
4021 case BUILT_IN_EH_RETURN:
4022 expand_builtin_eh_return (TREE_VALUE (arglist),
4023 TREE_VALUE (TREE_CHAIN (arglist)));
4024 return const0_rtx;
4025 #ifdef EH_RETURN_DATA_REGNO
4026 case BUILT_IN_EH_RETURN_DATA_REGNO:
4027 return expand_builtin_eh_return_data_regno (arglist);
4028 #endif
4029 case BUILT_IN_VARARGS_START:
4030 return expand_builtin_va_start (0, arglist);
4031 case BUILT_IN_STDARG_START:
4032 return expand_builtin_va_start (1, arglist);
4033 case BUILT_IN_VA_END:
4034 return expand_builtin_va_end (arglist);
4035 case BUILT_IN_VA_COPY:
4036 return expand_builtin_va_copy (arglist);
4037 case BUILT_IN_EXPECT:
4038 return expand_builtin_expect (arglist, target);
4039 case BUILT_IN_PREFETCH:
4040 expand_builtin_prefetch (arglist);
4041 return const0_rtx;
4044 default: /* just do library call, if unknown builtin */
4045 error ("built-in function `%s' not currently supported",
4046 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
4049 /* The switch statement above can drop through to cause the function
4050 to be called normally. */
4051 return expand_call (exp, target, ignore);
4054 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
4055 constant. ARGLIST is the argument list of the call. */
4057 static tree
4058 fold_builtin_constant_p (arglist)
4059 tree arglist;
4061 if (arglist == 0)
4062 return 0;
4064 arglist = TREE_VALUE (arglist);
4066 /* We return 1 for a numeric type that's known to be a constant
4067 value at compile-time or for an aggregate type that's a
4068 literal constant. */
4069 STRIP_NOPS (arglist);
4071 /* If we know this is a constant, emit the constant of one. */
4072 if (TREE_CODE_CLASS (TREE_CODE (arglist)) == 'c'
4073 || (TREE_CODE (arglist) == CONSTRUCTOR
4074 && TREE_CONSTANT (arglist))
4075 || (TREE_CODE (arglist) == ADDR_EXPR
4076 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
4077 return integer_one_node;
4079 /* If we aren't going to be running CSE or this expression
4080 has side effects, show we don't know it to be a constant.
4081 Likewise if it's a pointer or aggregate type since in those
4082 case we only want literals, since those are only optimized
4083 when generating RTL, not later.
4084 And finally, if we are compiling an initializer, not code, we
4085 need to return a definite result now; there's not going to be any
4086 more optimization done. */
4087 if (TREE_SIDE_EFFECTS (arglist) || cse_not_expected
4088 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
4089 || POINTER_TYPE_P (TREE_TYPE (arglist))
4090 || cfun == 0)
4091 return integer_zero_node;
4093 return 0;
4096 /* Fold a call to __builtin_classify_type. */
4098 static tree
4099 fold_builtin_classify_type (arglist)
4100 tree arglist;
4102 if (arglist == 0)
4103 return build_int_2 (no_type_class, 0);
4105 return build_int_2 (type_to_class (TREE_TYPE (TREE_VALUE (arglist))), 0);
4108 /* Used by constant folding to eliminate some builtin calls early. EXP is
4109 the CALL_EXPR of a call to a builtin function. */
4111 tree
4112 fold_builtin (exp)
4113 tree exp;
4115 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4116 tree arglist = TREE_OPERAND (exp, 1);
4117 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4119 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4120 return 0;
4122 switch (fcode)
4124 case BUILT_IN_CONSTANT_P:
4125 return fold_builtin_constant_p (arglist);
4127 case BUILT_IN_CLASSIFY_TYPE:
4128 return fold_builtin_classify_type (arglist);
4130 case BUILT_IN_STRLEN:
4131 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
4133 tree len = c_strlen (TREE_VALUE (arglist));
4134 if (len != 0)
4135 return len;
4137 break;
4139 default:
4140 break;
4143 return 0;
4146 static tree
4147 build_function_call_expr (fn, arglist)
4148 tree fn, arglist;
4150 tree call_expr;
4152 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
4153 call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
4154 call_expr, arglist);
4155 TREE_SIDE_EFFECTS (call_expr) = 1;
4156 return fold (call_expr);
4159 /* This function validates the types of a function call argument list
4160 represented as a tree chain of parameters against a specified list
4161 of tree_codes. If the last specifier is a 0, that represents an
4162 ellipses, otherwise the last specifier must be a VOID_TYPE. */
4164 static int
4165 validate_arglist VPARAMS ((tree arglist, ...))
4167 enum tree_code code;
4168 int res = 0;
4170 VA_OPEN (ap, arglist);
4171 VA_FIXEDARG (ap, tree, arglist);
4173 do {
4174 code = va_arg (ap, enum tree_code);
4175 switch (code)
4177 case 0:
4178 /* This signifies an ellipses, any further arguments are all ok. */
4179 res = 1;
4180 goto end;
4181 case VOID_TYPE:
4182 /* This signifies an endlink, if no arguments remain, return
4183 true, otherwise return false. */
4184 res = arglist == 0;
4185 goto end;
4186 default:
4187 /* If no parameters remain or the parameter's code does not
4188 match the specified code, return false. Otherwise continue
4189 checking any remaining arguments. */
4190 if (arglist == 0 || code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
4191 goto end;
4192 break;
4194 arglist = TREE_CHAIN (arglist);
4195 } while (1);
4197 /* We need gotos here since we can only have one VA_CLOSE in a
4198 function. */
4199 end: ;
4200 VA_CLOSE (ap);
4202 return res;
4205 /* Default version of target-specific builtin setup that does nothing. */
4207 void
4208 default_init_builtins ()
4212 /* Default target-specific builtin expander that does nothing. */
4215 default_expand_builtin (exp, target, subtarget, mode, ignore)
4216 tree exp ATTRIBUTE_UNUSED;
4217 rtx target ATTRIBUTE_UNUSED;
4218 rtx subtarget ATTRIBUTE_UNUSED;
4219 enum machine_mode mode ATTRIBUTE_UNUSED;
4220 int ignore ATTRIBUTE_UNUSED;
4222 return NULL_RTX;