OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / builtins.c
1 /* Expand builtin functions.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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 "flags.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "except.h"
32 #include "function.h"
33 #include "insn-config.h"
34 #include "expr.h"
35 #include "optabs.h"
36 #include "libfuncs.h"
37 #include "recog.h"
38 #include "output.h"
39 #include "typeclass.h"
40 #include "toplev.h"
41 #include "predict.h"
42 #include "tm_p.h"
43 #include "target.h"
44 #include "langhooks.h"
45
46 #define CALLED_AS_BUILT_IN(NODE) \
47    (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
48
49 /* Register mappings for target machines without register windows.  */
50 #ifndef INCOMING_REGNO
51 #define INCOMING_REGNO(OUT) (OUT)
52 #endif
53 #ifndef OUTGOING_REGNO
54 #define OUTGOING_REGNO(IN) (IN)
55 #endif
56
57 #ifndef PAD_VARARGS_DOWN
58 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
59 #endif
60
61 /* Define the names of the builtin function types and codes.  */
62 const char *const built_in_class_names[4]
63   = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
64
65 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT) STRINGX(X),
66 const char *const built_in_names[(int) END_BUILTINS] =
67 {
68 #include "builtins.def"
69 };
70 #undef DEF_BUILTIN
71
72 /* Setup an array of _DECL trees, make sure each element is
73    initialized to NULL_TREE.  */
74 tree built_in_decls[(int) END_BUILTINS];
75
76 static int get_pointer_alignment        PARAMS ((tree, unsigned int));
77 static tree c_strlen                    PARAMS ((tree));
78 static const char *c_getstr             PARAMS ((tree));
79 static rtx c_readstr                    PARAMS ((const char *,
80                                                  enum machine_mode));
81 static int target_char_cast             PARAMS ((tree, char *));
82 static rtx get_memory_rtx               PARAMS ((tree));
83 static int apply_args_size              PARAMS ((void));
84 static int apply_result_size            PARAMS ((void));
85 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
86 static rtx result_vector                PARAMS ((int, rtx));
87 #endif
88 static rtx expand_builtin_setjmp        PARAMS ((tree, rtx));
89 static void expand_builtin_prefetch     PARAMS ((tree));
90 static rtx expand_builtin_apply_args    PARAMS ((void));
91 static rtx expand_builtin_apply_args_1  PARAMS ((void));
92 static rtx expand_builtin_apply         PARAMS ((rtx, rtx, rtx));
93 static void expand_builtin_return       PARAMS ((rtx));
94 static enum type_class type_to_class    PARAMS ((tree));
95 static rtx expand_builtin_classify_type PARAMS ((tree));
96 static rtx expand_builtin_mathfn        PARAMS ((tree, rtx, rtx));
97 static rtx expand_builtin_constant_p    PARAMS ((tree));
98 static rtx expand_builtin_args_info     PARAMS ((tree));
99 static rtx expand_builtin_next_arg      PARAMS ((tree));
100 static rtx expand_builtin_va_start      PARAMS ((tree));
101 static rtx expand_builtin_va_end        PARAMS ((tree));
102 static rtx expand_builtin_va_copy       PARAMS ((tree));
103 static rtx expand_builtin_memcmp        PARAMS ((tree, tree, rtx,
104                                                  enum machine_mode));
105 static rtx expand_builtin_strcmp        PARAMS ((tree, rtx,
106                                                  enum machine_mode));
107 static rtx expand_builtin_strncmp       PARAMS ((tree, rtx,
108                                                  enum machine_mode));
109 static rtx builtin_memcpy_read_str      PARAMS ((PTR, HOST_WIDE_INT,
110                                                  enum machine_mode));
111 static rtx expand_builtin_strcat        PARAMS ((tree, rtx,
112                                                  enum machine_mode));
113 static rtx expand_builtin_strncat       PARAMS ((tree, rtx,
114                                                  enum machine_mode));
115 static rtx expand_builtin_strspn        PARAMS ((tree, rtx,
116                                                  enum machine_mode));
117 static rtx expand_builtin_strcspn       PARAMS ((tree, rtx,
118                                                  enum machine_mode));
119 static rtx expand_builtin_memcpy        PARAMS ((tree, rtx,
120                                                  enum machine_mode));
121 static rtx expand_builtin_strcpy        PARAMS ((tree, rtx,
122                                                  enum machine_mode));
123 static rtx builtin_strncpy_read_str     PARAMS ((PTR, HOST_WIDE_INT,
124                                                  enum machine_mode));
125 static rtx expand_builtin_strncpy       PARAMS ((tree, rtx,
126                                                  enum machine_mode));
127 static rtx builtin_memset_read_str      PARAMS ((PTR, HOST_WIDE_INT,
128                                                  enum machine_mode));
129 static rtx builtin_memset_gen_str       PARAMS ((PTR, HOST_WIDE_INT,
130                                                  enum machine_mode));
131 static rtx expand_builtin_memset        PARAMS ((tree, rtx,
132                                                  enum machine_mode));
133 static rtx expand_builtin_bzero         PARAMS ((tree));
134 static rtx expand_builtin_strlen        PARAMS ((tree, rtx));
135 static rtx expand_builtin_strstr        PARAMS ((tree, rtx,
136                                                  enum machine_mode));
137 static rtx expand_builtin_strpbrk       PARAMS ((tree, rtx,
138                                                  enum machine_mode));
139 static rtx expand_builtin_strchr        PARAMS ((tree, rtx,
140                                                  enum machine_mode));
141 static rtx expand_builtin_strrchr       PARAMS ((tree, rtx,
142                                                  enum machine_mode));
143 static rtx expand_builtin_alloca        PARAMS ((tree, rtx));
144 static rtx expand_builtin_ffs           PARAMS ((tree, rtx, rtx));
145 static rtx expand_builtin_frame_address PARAMS ((tree));
146 static rtx expand_builtin_fputs         PARAMS ((tree, int, int));
147 static tree stabilize_va_list           PARAMS ((tree, int));
148 static rtx expand_builtin_expect        PARAMS ((tree, rtx));
149 static tree fold_builtin_constant_p     PARAMS ((tree));
150 static tree fold_builtin_classify_type  PARAMS ((tree));
151 static tree fold_builtin_inf            PARAMS ((tree, int));
152 static tree build_function_call_expr    PARAMS ((tree, tree));
153 static int validate_arglist             PARAMS ((tree, ...));
154
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.
159
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.  */
162
163 static int
164 get_pointer_alignment (exp, max_align)
165      tree exp;
166      unsigned int max_align;
167 {
168   unsigned int align, inner;
169
170   if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
171     return 0;
172
173   align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
174   align = MIN (align, max_align);
175
176   while (1)
177     {
178       switch (TREE_CODE (exp))
179         {
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;
186
187           inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
188           align = MIN (inner, max_align);
189           break;
190
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;
197
198           while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
199                   & (max_align / BITS_PER_UNIT - 1))
200                  != 0)
201             max_align >>= 1;
202
203           exp = TREE_OPERAND (exp, 0);
204           break;
205
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);
218
219         default:
220           return align;
221         }
222     }
223 }
224
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.
228
229    The value returned is of type `ssizetype'.
230
231    Unfortunately, string_constant can't access the values of const char
232    arrays with initializers, so neither can we do so here.  */
233
234 static tree
235 c_strlen (src)
236      tree src;
237 {
238   tree offset_node;
239   HOST_WIDE_INT offset;
240   int max;
241   const char *ptr;
242
243   src = string_constant (src, &offset_node);
244   if (src == 0)
245     return 0;
246
247   max = TREE_STRING_LENGTH (src) - 1;
248   ptr = TREE_STRING_POINTER (src);
249
250   if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
251     {
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;
256
257       for (i = 0; i < max; i++)
258         if (ptr[i] == 0)
259           return 0;
260
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.  */
267
268       return size_diffop (size_int (max), offset_node);
269     }
270
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);
279
280   /* If the offset is known to be out of bounds, warn, and call strlen at
281      runtime.  */
282   if (offset < 0 || offset > max)
283     {
284       warning ("offset outside bounds of constant string");
285       return 0;
286     }
287
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".
291
292      Since OFFSET is our starting index into the string, no further
293      calculation is needed.  */
294   return ssize_int (strlen (ptr + offset));
295 }
296
297 /* Return a char pointer for a C string if it is a string constant
298    or sum of string constant and integer constant.  */
299
300 static const char *
301 c_getstr (src)
302      tree src;
303 {
304   tree offset_node;
305
306   src = string_constant (src, &offset_node);
307   if (src == 0)
308     return 0;
309
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;
315
316   return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
317 }
318
319 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
320    GET_MODE_BITSIZE (MODE) bits from string constant STR.  */
321
322 static rtx
323 c_readstr (str, mode)
324      const char *str;
325      enum machine_mode mode;
326 {
327   HOST_WIDE_INT c[2];
328   HOST_WIDE_INT ch;
329   unsigned int i, j;
330
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++)
337     {
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);
350     }
351   return immed_double_const (c[0], c[1], mode);
352 }
353
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.  */
357
358 static int
359 target_char_cast (cst, p)
360      tree cst;
361      char *p;
362 {
363   unsigned HOST_WIDE_INT val, hostval;
364
365   if (!host_integerp (cst, 1)
366       || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
367     return 1;
368
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;
372
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;
376
377   if (val != hostval)
378     return 1;
379
380   *p = hostval;
381   return 0;
382 }
383
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).  */
387
388 rtx
389 expand_builtin_return_addr (fndecl_code, count, tem)
390      enum built_in_function fndecl_code;
391      int count;
392      rtx tem;
393 {
394   int i;
395
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
403
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
412
413   /* Scan back COUNT frames to the specified frame.  */
414   for (i = 0; i < count; i++)
415     {
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);
425     }
426
427   /* For __builtin_frame_address, return what we've got.  */
428   if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
429     return tem;
430
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;
442 }
443
444 /* Alias set used for setjmp buffer.  */
445 static HOST_WIDE_INT setjmp_alias_set = -1;
446
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.  */
450
451 void
452 expand_builtin_setjmp_setup (buf_addr, receiver_label)
453      rtx buf_addr;
454      rtx receiver_label;
455 {
456   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
457   rtx stack_save;
458   rtx mem;
459
460   if (setjmp_alias_set == -1)
461     setjmp_alias_set = new_alias_set ();
462
463 #ifdef POINTERS_EXTEND_UNSIGNED
464   if (GET_MODE (buf_addr) != Pmode)
465     buf_addr = convert_memory_address (Pmode, buf_addr);
466 #endif
467
468   buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
469
470   emit_queue ();
471
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.  */
475
476 #ifndef BUILTIN_SETJMP_FRAME_VALUE
477 #define BUILTIN_SETJMP_FRAME_VALUE virtual_stack_vars_rtx
478 #endif
479
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);
483
484   mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
485   set_mem_alias_set (mem, setjmp_alias_set);
486
487   emit_move_insn (validize_mem (mem),
488                   force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
489
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);
495
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
501
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;
505
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;
509 }
510
511 /* Construct the trailing part of a __builtin_setjmp call.
512    This is used directly by sjlj exception handling code.  */
513
514 void
515 expand_builtin_setjmp_receiver (receiver_label)
516      rtx receiver_label ATTRIBUTE_UNUSED;
517 {
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));
521
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));
525
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);
533
534 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
535   if (fixed_regs[ARG_POINTER_REGNUM])
536     {
537 #ifdef ELIMINABLE_REGS
538       size_t i;
539       static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
540
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;
545
546       if (i == ARRAY_SIZE (elim_regs))
547 #endif
548         {
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)));
553         }
554     }
555 #endif
556
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 */ }
568
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, ""));
575 }
576
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.
582
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.  */
586
587 static rtx
588 expand_builtin_setjmp (arglist, target)
589      tree arglist;
590      rtx target;
591 {
592   rtx buf_addr, next_lab, cont_lab;
593
594   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
595     return NULL_RTX;
596
597   if (target == 0 || GET_CODE (target) != REG
598       || REGNO (target) < FIRST_PSEUDO_REGISTER)
599     target = gen_reg_rtx (TYPE_MODE (integer_type_node));
600
601   buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
602
603   next_lab = gen_label_rtx ();
604   cont_lab = gen_label_rtx ();
605
606   expand_builtin_setjmp_setup (buf_addr, next_lab);
607
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);
613
614   expand_builtin_setjmp_receiver (next_lab);
615
616   /* Set TARGET to one.  */
617   emit_move_insn (target, const1_rtx);
618   emit_label (cont_lab);
619
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.  */
623
624   current_function_has_nonlocal_label = 1;
625   nonlocal_goto_handler_labels
626     = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
627
628   return target;
629 }
630
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.
635
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.  */
639
640 void
641 expand_builtin_longjmp (buf_addr, value)
642      rtx buf_addr, value;
643 {
644   rtx fp, lab, stack, insn, last;
645   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
646
647   if (setjmp_alias_set == -1)
648     setjmp_alias_set = new_alias_set ();
649
650 #ifdef POINTERS_EXTEND_UNSIGNED
651   if (GET_MODE (buf_addr) != Pmode)
652     buf_addr = convert_memory_address (Pmode, buf_addr);
653 #endif
654
655   buf_addr = force_reg (Pmode, buf_addr);
656
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 ();
664
665   current_function_calls_longjmp = 1;
666
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
673     {
674       fp = gen_rtx_MEM (Pmode, buf_addr);
675       lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
676                                                GET_MODE_SIZE (Pmode)));
677
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);
683
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
694         {
695           lab = copy_to_reg (lab);
696
697           emit_move_insn (hard_frame_pointer_rtx, fp);
698           emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
699
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);
703         }
704     }
705
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))
712     {
713       if (insn == last)
714         abort ();
715       if (GET_CODE (insn) == JUMP_INSN)
716         {
717           REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
718                                               REG_NOTES (insn));
719           break;
720         }
721       else if (GET_CODE (insn) == CALL_INSN)
722         break;
723     }
724 }
725
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.  */
729
730 static void
731 expand_builtin_prefetch (arglist)
732      tree arglist;
733 {
734   tree arg0, arg1, arg2;
735   rtx op0, op1, op2;
736
737   if (!validate_arglist (arglist, POINTER_TYPE, 0))
738     return;
739
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))
745     {
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);
751     }
752   else
753     {
754       arg1 = integer_zero_node;
755       arg2 = build_int_2 (3, 0);
756     }
757
758   /* Argument 0 is an address.  */
759   op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
760
761   /* Argument 1 (read/write flag) must be a compile-time constant int.  */
762   if (TREE_CODE (arg1) != INTEGER_CST)
763     {
764       error ("second arg to `__builtin_prefetch' must be a constant");
765       arg1 = integer_zero_node;
766     }
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)
770     {
771       warning ("invalid second arg to __builtin_prefetch; using zero");
772       op1 = const0_rtx;
773     }
774
775   /* Argument 2 (locality) must be a compile-time constant int.  */
776   if (TREE_CODE (arg2) != INTEGER_CST)
777     {
778       error ("third arg to `__builtin_prefetch' must be a constant");
779       arg2 = integer_zero_node;
780     }
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)
784     {
785       warning ("invalid third arg to __builtin_prefetch; using zero");
786       op2 = const0_rtx;
787     }
788
789 #ifdef HAVE_prefetch
790   if (HAVE_prefetch)
791     {
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))
796         {
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);
802         }
803       emit_insn (gen_prefetch (op0, op1, op2));
804     }
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);
812 }
813
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, ..).  */
816
817 static rtx
818 get_memory_rtx (exp)
819      tree exp;
820 {
821   rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM);
822   rtx mem;
823
824 #ifdef POINTERS_EXTEND_UNSIGNED
825   if (GET_MODE (addr) != Pmode)
826     addr = convert_memory_address (Pmode, addr);
827 #endif
828
829   mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
830
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);
838
839   if (TREE_CODE (exp) == ADDR_EXPR)
840     {
841       exp = TREE_OPERAND (exp, 0);
842       set_mem_attributes (mem, exp, 0);
843     }
844   else if (POINTER_TYPE_P (TREE_TYPE (exp)))
845     {
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);
849     }
850
851   return mem;
852 }
853 \f
854 /* Built-in functions to perform an untyped call and return.  */
855
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];
862
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];
869
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];
875
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.  */
879
880 int
881 apply_args_register_offset (regno)
882      int regno;
883 {
884   apply_args_size ();
885
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];
892 }
893
894 /* Return the size required for the block returned by __builtin_apply_args,
895    and initialize apply_args_mode.  */
896
897 static int
898 apply_args_size ()
899 {
900   static int size = -1;
901   int align;
902   unsigned int regno;
903   enum machine_mode mode;
904
905   /* The values computed by this function never change.  */
906   if (size < 0)
907     {
908       /* The first value is the incoming arg-pointer.  */
909       size = GET_MODE_SIZE (Pmode);
910
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);
915
916       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
917         if (FUNCTION_ARG_REGNO_P (regno))
918           {
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;
922
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;
929
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;
937
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;
945
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;
953
954             mode = best_mode;
955             if (mode == VOIDmode)
956               abort ();
957
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;
964           }
965         else
966           {
967             apply_args_mode[regno] = VOIDmode;
968             apply_args_reg_offset[regno] = 0;
969           }
970     }
971   return size;
972 }
973
974 /* Return the size required for the block returned by __builtin_apply,
975    and initialize apply_result_mode.  */
976
977 static int
978 apply_result_size ()
979 {
980   static int size = -1;
981   int align, regno;
982   enum machine_mode mode;
983
984   /* The values computed by this function never change.  */
985   if (size < 0)
986     {
987       size = 0;
988
989       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
990         if (FUNCTION_VALUE_REGNO_P (regno))
991           {
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;
995
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;
1001
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;
1009
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;
1017
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;
1025
1026             mode = best_mode;
1027             if (mode == VOIDmode)
1028               abort ();
1029
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;
1035           }
1036         else
1037           apply_result_mode[regno] = VOIDmode;
1038
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
1044     }
1045   return size;
1046 }
1047
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.  */
1052
1053 static rtx
1054 result_vector (savep, result)
1055      int savep;
1056      rtx result;
1057 {
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));
1062
1063   size = nelts = 0;
1064   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1065     if ((mode = apply_result_mode[regno]) != VOIDmode)
1066       {
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);
1076       }
1077   return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1078 }
1079 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1080
1081 /* Save the state required to perform an untyped call with the same
1082    arguments as were passed to the current function.  */
1083
1084 static rtx
1085 expand_builtin_apply_args_1 ()
1086 {
1087   rtx registers;
1088   int size, align, regno;
1089   enum machine_mode mode;
1090
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);
1094
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);
1099
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)
1103       {
1104         rtx tem;
1105
1106         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1107         if (size % align != 0)
1108           size = CEIL (size, align) * align;
1109
1110         tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1111
1112         emit_move_insn (adjust_address (registers, mode, size), tem);
1113         size += GET_MODE_SIZE (mode);
1114       }
1115
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);
1120
1121   /* Save the structure value address unless this is passed as an
1122      "invisible" first argument.  */
1123   if (struct_value_incoming_rtx)
1124     {
1125       emit_move_insn (adjust_address (registers, Pmode, size),
1126                       copy_to_reg (struct_value_incoming_rtx));
1127       size += GET_MODE_SIZE (Pmode);
1128     }
1129
1130   /* Return the address of the block.  */
1131   return copy_addr_to_reg (XEXP (registers, 0));
1132 }
1133
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.  */
1140
1141 static rtx
1142 expand_builtin_apply_args ()
1143 {
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;
1148   {
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;
1154
1155     start_sequence ();
1156     temp = expand_builtin_apply_args_1 ();
1157     seq = get_insns ();
1158     end_sequence ();
1159
1160     apply_args_value = temp;
1161
1162     /* Put the insns after the NOTE that starts the function.
1163        If this is inside a start_sequence, make the outer-level insn
1164        chain current, so the code is placed at the start of the
1165        function.  */
1166     push_topmost_sequence ();
1167     emit_insn_before (seq, NEXT_INSN (get_insns ()));
1168     pop_topmost_sequence ();
1169     return temp;
1170   }
1171 }
1172
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.  */
1175
1176 static rtx
1177 expand_builtin_apply (function, arguments, argsize)
1178      rtx function, arguments, argsize;
1179 {
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;
1185
1186 #ifdef POINTERS_EXTEND_UNSIGNED
1187   if (GET_MODE (arguments) != Pmode)
1188     arguments = convert_memory_address (Pmode, arguments);
1189 #endif
1190
1191   /* Create a block where the return registers can be saved.  */
1192   result = assign_stack_local (BLKmode, apply_result_size (), -1);
1193
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
1201
1202   /* Perform postincrements before actually calling the function.  */
1203   emit_queue ();
1204
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;
1210
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);
1218
1219   /* Push a block of memory onto the stack to store the memory arguments.
1220      Save the address in a register, and copy the memory arguments.  ??? I
1221      haven't figured out how the calling convention macros effect this,
1222      but it's likely that the source and/or destination addresses in
1223      the block copy will need updating in machine specific ways.  */
1224   dest = allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1225   dest = gen_rtx_MEM (BLKmode, dest);
1226   set_mem_align (dest, PARM_BOUNDARY);
1227   src = gen_rtx_MEM (BLKmode, incoming_args);
1228   set_mem_align (src, PARM_BOUNDARY);
1229   emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1230
1231   /* Refer to the argument block.  */
1232   apply_args_size ();
1233   arguments = gen_rtx_MEM (BLKmode, arguments);
1234   set_mem_align (arguments, PARM_BOUNDARY);
1235
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);
1240
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)
1245       {
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);
1253       }
1254
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)
1259     {
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);
1266     }
1267
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);
1270
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);
1276
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)
1286     {
1287       rtx valreg = 0;
1288
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)
1295           {
1296             if (valreg)
1297               abort (); /* HAVE_untyped_call required.  */
1298             valreg = gen_rtx_REG (mode, regno);
1299           }
1300
1301       emit_call_insn (GEN_CALL_VALUE (valreg,
1302                                       gen_rtx_MEM (FUNCTION_MODE, function),
1303                                       const0_rtx, NULL_RTX, const0_rtx));
1304
1305       emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1306     }
1307   else
1308 #endif
1309     abort ();
1310
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))
1315     ;
1316
1317   if (! call_insn)
1318     abort ();
1319
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))
1323     {
1324       rtx link;
1325
1326       for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
1327            link = XEXP (link, 1))
1328         ;
1329
1330       XEXP (link, 1) = call_fusage;
1331     }
1332   else
1333     CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
1334
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);
1342
1343   OK_DEFER_POP;
1344
1345   /* Return the address of the result block.  */
1346   return copy_addr_to_reg (XEXP (result, 0));
1347 }
1348
1349 /* Perform an untyped return.  */
1350
1351 static void
1352 expand_builtin_return (result)
1353      rtx result;
1354 {
1355   int size, align, regno;
1356   enum machine_mode mode;
1357   rtx reg;
1358   rtx call_fusage = 0;
1359
1360 #ifdef POINTERS_EXTEND_UNSIGNED
1361   if (GET_MODE (result) != Pmode)
1362     result = convert_memory_address (Pmode, result);
1363 #endif
1364
1365   apply_result_size ();
1366   result = gen_rtx_MEM (BLKmode, result);
1367
1368 #ifdef HAVE_untyped_return
1369   if (HAVE_untyped_return)
1370     {
1371       emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1372       emit_barrier ();
1373       return;
1374     }
1375 #endif
1376
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)
1381       {
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));
1387
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);
1393       }
1394
1395   /* Put the USE insns before the return.  */
1396   emit_insn (call_fusage);
1397
1398   /* Return whatever values was restored by jumping directly to the end
1399      of the function.  */
1400   expand_null_return ();
1401 }
1402
1403 /* Used by expand_builtin_classify_type and fold_builtin_classify_type.  */
1404
1405 static enum type_class
1406 type_to_class (type)
1407      tree type;
1408 {
1409   switch (TREE_CODE (type))
1410     {
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;
1432     }
1433 }
1434
1435 /* Expand a call to __builtin_classify_type with arguments found in
1436    ARGLIST.  */
1437
1438 static rtx
1439 expand_builtin_classify_type (arglist)
1440      tree arglist;
1441 {
1442   if (arglist != 0)
1443     return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1444   return GEN_INT (no_type_class);
1445 }
1446
1447 /* Expand expression EXP, which is a call to __builtin_constant_p.  */
1448
1449 static rtx
1450 expand_builtin_constant_p (exp)
1451      tree exp;
1452 {
1453   tree arglist = TREE_OPERAND (exp, 1);
1454   enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1455   rtx tmp;
1456
1457   if (arglist == 0)
1458     return const0_rtx;
1459   arglist = TREE_VALUE (arglist);
1460
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.  */
1464
1465   tmp = expand_expr (arglist, NULL_RTX, VOIDmode, 0);
1466   tmp = gen_rtx_CONSTANT_P_RTX (value_mode, tmp);
1467   return tmp;
1468 }
1469
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.  */
1475
1476 static rtx
1477 expand_builtin_mathfn (exp, target, subtarget)
1478      tree exp;
1479      rtx target, subtarget;
1480 {
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;
1486
1487   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1488     return 0;
1489
1490   /* Stabilize and compute the argument.  */
1491   if (TREE_CODE (TREE_VALUE (arglist)) != VAR_DECL
1492       && TREE_CODE (TREE_VALUE (arglist)) != PARM_DECL)
1493     {
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);
1505     }
1506   op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
1507
1508   /* Make a suitable register to place result in.  */
1509   target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
1510
1511   emit_queue ();
1512   start_sequence ();
1513
1514   switch (DECL_FUNCTION_CODE (fndecl))
1515     {
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     case BUILT_IN_EXP:
1529     case BUILT_IN_EXPF:
1530     case BUILT_IN_EXPL:
1531       builtin_optab = exp_optab; break;
1532     case BUILT_IN_LOG:
1533     case BUILT_IN_LOGF:
1534     case BUILT_IN_LOGL:
1535       builtin_optab = log_optab; break;
1536     default:
1537       abort ();
1538     }
1539
1540   /* Compute into TARGET.
1541      Set TARGET to wherever the result comes back.  */
1542   argmode = TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist)));
1543   target = expand_unop (argmode, builtin_optab, op0, target, 0);
1544
1545   /* If we were unable to expand via the builtin, stop the
1546      sequence (without outputting the insns) and return 0, causing
1547      a call to the library function.  */
1548   if (target == 0)
1549     {
1550       end_sequence ();
1551       return 0;
1552     }
1553
1554   /* If errno must be maintained, we must set it to EDOM for NaN results.  */
1555
1556   if (flag_errno_math && HONOR_NANS (argmode))
1557     {
1558       rtx lab1;
1559
1560       lab1 = gen_label_rtx ();
1561
1562       /* Test the result; if it is NaN, set errno=EDOM because
1563          the argument was not in the domain.  */
1564       emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1565                                0, lab1);
1566
1567 #ifdef TARGET_EDOM
1568       {
1569 #ifdef GEN_ERRNO_RTX
1570         rtx errno_rtx = GEN_ERRNO_RTX;
1571 #else
1572         rtx errno_rtx
1573           = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1574 #endif
1575
1576         emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1577       }
1578 #else
1579       /* We can't set errno=EDOM directly; let the library call do it.
1580          Pop the arguments right away in case the call gets deleted.  */
1581       NO_DEFER_POP;
1582       expand_call (exp, target, 0);
1583       OK_DEFER_POP;
1584 #endif
1585
1586       emit_label (lab1);
1587     }
1588
1589   /* Output the entire sequence.  */
1590   insns = get_insns ();
1591   end_sequence ();
1592   emit_insn (insns);
1593
1594   return target;
1595 }
1596
1597 /* Expand expression EXP which is a call to the strlen builtin.  Return 0
1598    if we failed the caller should emit a normal call, otherwise
1599    try to get the result in TARGET, if convenient.  */
1600
1601 static rtx
1602 expand_builtin_strlen (exp, target)
1603      tree exp;
1604      rtx target;
1605 {
1606   tree arglist = TREE_OPERAND (exp, 1);
1607   enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1608
1609   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
1610     return 0;
1611   else
1612     {
1613       rtx pat;
1614       tree src = TREE_VALUE (arglist);
1615
1616       int align
1617         = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1618
1619       rtx result, src_reg, char_rtx, before_strlen;
1620       enum machine_mode insn_mode = value_mode, char_mode;
1621       enum insn_code icode = CODE_FOR_nothing;
1622
1623       /* If SRC is not a pointer type, don't do this operation inline.  */
1624       if (align == 0)
1625         return 0;
1626
1627       /* Bail out if we can't compute strlen in the right mode.  */
1628       while (insn_mode != VOIDmode)
1629         {
1630           icode = strlen_optab->handlers[(int) insn_mode].insn_code;
1631           if (icode != CODE_FOR_nothing)
1632             break;
1633
1634           insn_mode = GET_MODE_WIDER_MODE (insn_mode);
1635         }
1636       if (insn_mode == VOIDmode)
1637         return 0;
1638
1639       /* Make a place to write the result of the instruction.  */
1640       result = target;
1641       if (! (result != 0
1642              && GET_CODE (result) == REG
1643              && GET_MODE (result) == insn_mode
1644              && REGNO (result) >= FIRST_PSEUDO_REGISTER))
1645         result = gen_reg_rtx (insn_mode);
1646
1647       /* Make a place to hold the source address.  We will not expand
1648          the actual source until we are sure that the expansion will
1649          not fail -- there are trees that cannot be expanded twice.  */
1650       src_reg = gen_reg_rtx (Pmode);
1651
1652       /* Mark the beginning of the strlen sequence so we can emit the
1653          source operand later.  */
1654       before_strlen = get_last_insn();
1655
1656       char_rtx = const0_rtx;
1657       char_mode = insn_data[(int) icode].operand[2].mode;
1658       if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
1659                                                             char_mode))
1660         char_rtx = copy_to_mode_reg (char_mode, char_rtx);
1661
1662       pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
1663                              char_rtx, GEN_INT (align));
1664       if (! pat)
1665         return 0;
1666       emit_insn (pat);
1667
1668       /* Now that we are assured of success, expand the source.  */
1669       start_sequence ();
1670       pat = memory_address (BLKmode,
1671                             expand_expr (src, src_reg, ptr_mode, EXPAND_SUM));
1672       if (pat != src_reg)
1673         emit_move_insn (src_reg, pat);
1674       pat = get_insns ();
1675       end_sequence ();
1676
1677       if (before_strlen)
1678         emit_insn_after (pat, before_strlen);
1679       else
1680         emit_insn_before (pat, get_insns ());
1681
1682       /* Return the value in the proper mode for this function.  */
1683       if (GET_MODE (result) == value_mode)
1684         target = result;
1685       else if (target != 0)
1686         convert_move (target, result, 0);
1687       else
1688         target = convert_to_mode (value_mode, result, 0);
1689
1690       return target;
1691     }
1692 }
1693
1694 /* Expand a call to the strstr builtin.  Return 0 if we failed the
1695    caller should emit a normal call, otherwise try to get the result
1696    in TARGET, if convenient (and in mode MODE if that's convenient).  */
1697
1698 static rtx
1699 expand_builtin_strstr (arglist, target, mode)
1700      tree arglist;
1701      rtx target;
1702      enum machine_mode mode;
1703 {
1704   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1705     return 0;
1706   else
1707     {
1708       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1709       tree fn;
1710       const char *p1, *p2;
1711
1712       p2 = c_getstr (s2);
1713       if (p2 == NULL)
1714         return 0;
1715
1716       p1 = c_getstr (s1);
1717       if (p1 != NULL)
1718         {
1719           const char *r = strstr (p1, p2);
1720
1721           if (r == NULL)
1722             return const0_rtx;
1723
1724           /* Return an offset into the constant string argument.  */
1725           return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1726                                            s1, ssize_int (r - p1))),
1727                               target, mode, EXPAND_NORMAL);
1728         }
1729
1730       if (p2[0] == '\0')
1731         return expand_expr (s1, target, mode, EXPAND_NORMAL);
1732
1733       if (p2[1] != '\0')
1734         return 0;
1735
1736       fn = built_in_decls[BUILT_IN_STRCHR];
1737       if (!fn)
1738         return 0;
1739
1740       /* New argument list transforming strstr(s1, s2) to
1741          strchr(s1, s2[0]).  */
1742       arglist =
1743         build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
1744       arglist = tree_cons (NULL_TREE, s1, arglist);
1745       return expand_expr (build_function_call_expr (fn, arglist),
1746                           target, mode, EXPAND_NORMAL);
1747     }
1748 }
1749
1750 /* Expand a call to the strchr builtin.  Return 0 if we failed the
1751    caller should emit a normal call, otherwise try to get the result
1752    in TARGET, if convenient (and in mode MODE if that's convenient).  */
1753
1754 static rtx
1755 expand_builtin_strchr (arglist, target, mode)
1756      tree arglist;
1757      rtx target;
1758      enum machine_mode mode;
1759 {
1760   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
1761     return 0;
1762   else
1763     {
1764       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1765       const char *p1;
1766
1767       if (TREE_CODE (s2) != INTEGER_CST)
1768         return 0;
1769
1770       p1 = c_getstr (s1);
1771       if (p1 != NULL)
1772         {
1773           char c;
1774           const char *r;
1775
1776           if (target_char_cast (s2, &c))
1777             return 0;
1778
1779           r = strchr (p1, c);
1780
1781           if (r == NULL)
1782             return const0_rtx;
1783
1784           /* Return an offset into the constant string argument.  */
1785           return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1786                                            s1, ssize_int (r - p1))),
1787                               target, mode, EXPAND_NORMAL);
1788         }
1789
1790       /* FIXME: Should use here strchrM optab so that ports can optimize
1791          this.  */
1792       return 0;
1793     }
1794 }
1795
1796 /* Expand a call to the strrchr builtin.  Return 0 if we failed the
1797    caller should emit a normal call, otherwise try to get the result
1798    in TARGET, if convenient (and in mode MODE if that's convenient).  */
1799
1800 static rtx
1801 expand_builtin_strrchr (arglist, target, mode)
1802      tree arglist;
1803      rtx target;
1804      enum machine_mode mode;
1805 {
1806   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
1807     return 0;
1808   else
1809     {
1810       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1811       tree fn;
1812       const char *p1;
1813
1814       if (TREE_CODE (s2) != INTEGER_CST)
1815         return 0;
1816
1817       p1 = c_getstr (s1);
1818       if (p1 != NULL)
1819         {
1820           char c;
1821           const char *r;
1822
1823           if (target_char_cast (s2, &c))
1824             return 0;
1825
1826           r = strrchr (p1, c);
1827
1828           if (r == NULL)
1829             return const0_rtx;
1830
1831           /* Return an offset into the constant string argument.  */
1832           return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1833                                            s1, ssize_int (r - p1))),
1834                               target, mode, EXPAND_NORMAL);
1835         }
1836
1837       if (! integer_zerop (s2))
1838         return 0;
1839
1840       fn = built_in_decls[BUILT_IN_STRCHR];
1841       if (!fn)
1842         return 0;
1843
1844       /* Transform strrchr(s1, '\0') to strchr(s1, '\0').  */
1845       return expand_expr (build_function_call_expr (fn, arglist),
1846                           target, mode, EXPAND_NORMAL);
1847     }
1848 }
1849
1850 /* Expand a call to the strpbrk builtin.  Return 0 if we failed the
1851    caller should emit a normal call, otherwise try to get the result
1852    in TARGET, if convenient (and in mode MODE if that's convenient).  */
1853
1854 static rtx
1855 expand_builtin_strpbrk (arglist, target, mode)
1856      tree arglist;
1857      rtx target;
1858      enum machine_mode mode;
1859 {
1860   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1861     return 0;
1862   else
1863     {
1864       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
1865       tree fn;
1866       const char *p1, *p2;
1867
1868       p2 = c_getstr (s2);
1869       if (p2 == NULL)
1870         return 0;
1871
1872       p1 = c_getstr (s1);
1873       if (p1 != NULL)
1874         {
1875           const char *r = strpbrk (p1, p2);
1876
1877           if (r == NULL)
1878             return const0_rtx;
1879
1880           /* Return an offset into the constant string argument.  */
1881           return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1),
1882                                            s1, ssize_int (r - p1))),
1883                               target, mode, EXPAND_NORMAL);
1884         }
1885
1886       if (p2[0] == '\0')
1887         {
1888           /* strpbrk(x, "") == NULL.
1889              Evaluate and ignore the arguments in case they had
1890              side-effects.  */
1891           expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
1892           return const0_rtx;
1893         }
1894
1895       if (p2[1] != '\0')
1896         return 0;  /* Really call strpbrk.  */
1897
1898       fn = built_in_decls[BUILT_IN_STRCHR];
1899       if (!fn)
1900         return 0;
1901
1902       /* New argument list transforming strpbrk(s1, s2) to
1903          strchr(s1, s2[0]).  */
1904       arglist =
1905         build_tree_list (NULL_TREE, build_int_2 (p2[0], 0));
1906       arglist = tree_cons (NULL_TREE, s1, arglist);
1907       return expand_expr (build_function_call_expr (fn, arglist),
1908                           target, mode, EXPAND_NORMAL);
1909     }
1910 }
1911
1912 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
1913    bytes from constant string DATA + OFFSET and return it as target
1914    constant.  */
1915
1916 static rtx
1917 builtin_memcpy_read_str (data, offset, mode)
1918      PTR data;
1919      HOST_WIDE_INT offset;
1920      enum machine_mode mode;
1921 {
1922   const char *str = (const char *) data;
1923
1924   if (offset < 0
1925       || ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
1926           > strlen (str) + 1))
1927     abort ();  /* Attempt to read past the end of constant string.  */
1928
1929   return c_readstr (str + offset, mode);
1930 }
1931
1932 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
1933    Return 0 if we failed, the caller should emit a normal call, otherwise
1934    try to get the result in TARGET, if convenient (and in mode MODE if
1935    that's convenient).  */
1936
1937 static rtx
1938 expand_builtin_memcpy (arglist, target, mode)
1939      tree arglist;
1940      rtx target;
1941      enum machine_mode mode;
1942 {
1943   if (!validate_arglist (arglist,
1944                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
1945     return 0;
1946   else
1947     {
1948       tree dest = TREE_VALUE (arglist);
1949       tree src = TREE_VALUE (TREE_CHAIN (arglist));
1950       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1951       const char *src_str;
1952
1953       unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
1954       unsigned int dest_align
1955         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
1956       rtx dest_mem, src_mem, dest_addr, len_rtx;
1957
1958       /* If DEST is not a pointer type, call the normal function.  */
1959       if (dest_align == 0)
1960         return 0;
1961
1962       /* If the LEN parameter is zero, return DEST.  */
1963       if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
1964         {
1965           /* Evaluate and ignore SRC in case it has side-effects.  */
1966           expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
1967           return expand_expr (dest, target, mode, EXPAND_NORMAL);
1968         }
1969
1970       /* If either SRC is not a pointer type, don't do this
1971          operation in-line.  */
1972       if (src_align == 0)
1973         return 0;
1974
1975       dest_mem = get_memory_rtx (dest);
1976       set_mem_align (dest_mem, dest_align);
1977       len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
1978       src_str = c_getstr (src);
1979
1980       /* If SRC is a string constant and block move would be done
1981          by pieces, we can avoid loading the string from memory
1982          and only stored the computed constants.  */
1983       if (src_str
1984           && GET_CODE (len_rtx) == CONST_INT
1985           && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
1986           && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
1987                                   (PTR) src_str, dest_align))
1988         {
1989           store_by_pieces (dest_mem, INTVAL (len_rtx),
1990                            builtin_memcpy_read_str,
1991                            (PTR) src_str, dest_align);
1992           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
1993 #ifdef POINTERS_EXTEND_UNSIGNED
1994           if (GET_MODE (dest_mem) != ptr_mode)
1995             dest_mem = convert_memory_address (ptr_mode, dest_mem);
1996 #endif
1997           return dest_mem;
1998         }
1999
2000       src_mem = get_memory_rtx (src);
2001       set_mem_align (src_mem, src_align);
2002
2003       /* Copy word part most expediently.  */
2004       dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2005                                    BLOCK_OP_NORMAL);
2006
2007       if (dest_addr == 0)
2008         {
2009           dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2010 #ifdef POINTERS_EXTEND_UNSIGNED
2011           if (GET_MODE (dest_addr) != ptr_mode)
2012             dest_addr = convert_memory_address (ptr_mode, dest_addr);
2013 #endif
2014         }
2015
2016       return dest_addr;
2017     }
2018 }
2019
2020 /* Expand expression EXP, which is a call to the strcpy builtin.  Return 0
2021    if we failed the caller should emit a normal call, otherwise try to get
2022    the result in TARGET, if convenient (and in mode MODE if that's
2023    convenient).  */
2024
2025 static rtx
2026 expand_builtin_strcpy (exp, target, mode)
2027      tree exp;
2028      rtx target;
2029      enum machine_mode mode;
2030 {
2031   tree arglist = TREE_OPERAND (exp, 1);
2032   tree fn, len;
2033
2034   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2035     return 0;
2036
2037   fn = built_in_decls[BUILT_IN_MEMCPY];
2038   if (!fn)
2039     return 0;
2040
2041   len = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)));
2042   if (len == 0)
2043     return 0;
2044
2045   len = size_binop (PLUS_EXPR, len, ssize_int (1));
2046   chainon (arglist, build_tree_list (NULL_TREE, len));
2047   return expand_expr (build_function_call_expr (fn, arglist),
2048                       target, mode, EXPAND_NORMAL);
2049 }
2050
2051 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
2052    bytes from constant string DATA + OFFSET and return it as target
2053    constant.  */
2054
2055 static rtx
2056 builtin_strncpy_read_str (data, offset, mode)
2057      PTR data;
2058      HOST_WIDE_INT offset;
2059      enum machine_mode mode;
2060 {
2061   const char *str = (const char *) data;
2062
2063   if ((unsigned HOST_WIDE_INT) offset > strlen (str))
2064     return const0_rtx;
2065
2066   return c_readstr (str + offset, mode);
2067 }
2068
2069 /* Expand expression EXP, which is a call to the strncpy builtin.  Return 0
2070    if we failed the caller should emit a normal call.  */
2071
2072 static rtx
2073 expand_builtin_strncpy (arglist, target, mode)
2074      tree arglist;
2075      rtx target;
2076      enum machine_mode mode;
2077 {
2078   if (!validate_arglist (arglist,
2079                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2080     return 0;
2081   else
2082     {
2083       tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)));
2084       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2085       tree fn;
2086
2087       /* We must be passed a constant len parameter.  */
2088       if (TREE_CODE (len) != INTEGER_CST)
2089         return 0;
2090
2091       /* If the len parameter is zero, return the dst parameter.  */
2092       if (integer_zerop (len))
2093         {
2094           /* Evaluate and ignore the src argument in case it has
2095              side-effects.  */
2096           expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
2097                        VOIDmode, EXPAND_NORMAL);
2098           /* Return the dst parameter.  */
2099           return expand_expr (TREE_VALUE (arglist), target, mode,
2100                               EXPAND_NORMAL);
2101         }
2102
2103       /* Now, we must be passed a constant src ptr parameter.  */
2104       if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
2105         return 0;
2106
2107       slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
2108
2109       /* We're required to pad with trailing zeros if the requested
2110          len is greater than strlen(s2)+1.  In that case try to
2111          use store_by_pieces, if it fails, punt.  */
2112       if (tree_int_cst_lt (slen, len))
2113         {
2114           tree dest = TREE_VALUE (arglist);
2115           unsigned int dest_align
2116             = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2117           const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
2118           rtx dest_mem;
2119
2120           if (!p || dest_align == 0 || !host_integerp (len, 1)
2121               || !can_store_by_pieces (tree_low_cst (len, 1),
2122                                        builtin_strncpy_read_str,
2123                                        (PTR) p, dest_align))
2124             return 0;
2125
2126           dest_mem = get_memory_rtx (dest);
2127           store_by_pieces (dest_mem, tree_low_cst (len, 1),
2128                            builtin_strncpy_read_str,
2129                            (PTR) p, dest_align);
2130           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2131 #ifdef POINTERS_EXTEND_UNSIGNED
2132           if (GET_MODE (dest_mem) != ptr_mode)
2133             dest_mem = convert_memory_address (ptr_mode, dest_mem);
2134 #endif
2135           return dest_mem;
2136         }
2137
2138       /* OK transform into builtin memcpy.  */
2139       fn = built_in_decls[BUILT_IN_MEMCPY];
2140       if (!fn)
2141         return 0;
2142       return expand_expr (build_function_call_expr (fn, arglist),
2143                           target, mode, EXPAND_NORMAL);
2144     }
2145 }
2146
2147 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
2148    bytes from constant string DATA + OFFSET and return it as target
2149    constant.  */
2150
2151 static rtx
2152 builtin_memset_read_str (data, offset, mode)
2153      PTR data;
2154      HOST_WIDE_INT offset ATTRIBUTE_UNUSED;
2155      enum machine_mode mode;
2156 {
2157   const char *c = (const char *) data;
2158   char *p = alloca (GET_MODE_SIZE (mode));
2159
2160   memset (p, *c, GET_MODE_SIZE (mode));
2161
2162   return c_readstr (p, mode);
2163 }
2164
2165 /* Callback routine for store_by_pieces.  Return the RTL of a register
2166    containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
2167    char value given in the RTL register data.  For example, if mode is
2168    4 bytes wide, return the RTL for 0x01010101*data.  */
2169
2170 static rtx
2171 builtin_memset_gen_str (data, offset, mode)
2172      PTR data;
2173      HOST_WIDE_INT offset ATTRIBUTE_UNUSED;
2174      enum machine_mode mode;
2175 {
2176   rtx target, coeff;
2177   size_t size;
2178   char *p;
2179
2180   size = GET_MODE_SIZE (mode);
2181   if (size==1)
2182     return (rtx)data;
2183
2184   p = alloca (size);
2185   memset (p, 1, size);
2186   coeff = c_readstr (p, mode);
2187
2188   target = convert_to_mode (mode, (rtx)data, 1);
2189   target = expand_mult (mode, target, coeff, NULL_RTX, 1);
2190   return force_reg (mode, target);
2191 }
2192
2193 /* Expand expression EXP, which is a call to the memset builtin.  Return 0
2194    if we failed the caller should emit a normal call, otherwise try to get
2195    the result in TARGET, if convenient (and in mode MODE if that's
2196    convenient).  */
2197
2198 static rtx
2199 expand_builtin_memset (exp, target, mode)
2200      tree exp;
2201      rtx target;
2202      enum machine_mode mode;
2203 {
2204   tree arglist = TREE_OPERAND (exp, 1);
2205
2206   if (!validate_arglist (arglist,
2207                          POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
2208     return 0;
2209   else
2210     {
2211       tree dest = TREE_VALUE (arglist);
2212       tree val = TREE_VALUE (TREE_CHAIN (arglist));
2213       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2214       char c;
2215
2216       unsigned int dest_align
2217         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2218       rtx dest_mem, dest_addr, len_rtx;
2219
2220       /* If DEST is not a pointer type, don't do this
2221          operation in-line.  */
2222       if (dest_align == 0)
2223         return 0;
2224
2225       /* If the LEN parameter is zero, return DEST.  */
2226       if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
2227         {
2228           /* Evaluate and ignore VAL in case it has side-effects.  */
2229           expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
2230           return expand_expr (dest, target, mode, EXPAND_NORMAL);
2231         }
2232
2233       if (TREE_CODE (val) != INTEGER_CST)
2234         {
2235           rtx val_rtx;
2236
2237           if (!host_integerp (len, 1))
2238             return 0;
2239
2240           if (optimize_size && tree_low_cst (len, 1) > 1)
2241             return 0;
2242
2243           /* Assume that we can memset by pieces if we can store the
2244            * the coefficients by pieces (in the required modes).
2245            * We can't pass builtin_memset_gen_str as that emits RTL.  */
2246           c = 1;
2247           if (!can_store_by_pieces (tree_low_cst (len, 1),
2248                                     builtin_memset_read_str,
2249                                     (PTR) &c, dest_align))
2250             return 0;
2251
2252           val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
2253           val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
2254           val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
2255                                val_rtx);
2256           dest_mem = get_memory_rtx (dest);
2257           store_by_pieces (dest_mem, tree_low_cst (len, 1),
2258                            builtin_memset_gen_str,
2259                            (PTR)val_rtx, dest_align);
2260           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2261 #ifdef POINTERS_EXTEND_UNSIGNED
2262           if (GET_MODE (dest_mem) != ptr_mode)
2263             dest_mem = convert_memory_address (ptr_mode, dest_mem);
2264 #endif
2265           return dest_mem;
2266         }
2267
2268       if (target_char_cast (val, &c))
2269         return 0;
2270
2271       if (c)
2272         {
2273           if (!host_integerp (len, 1))
2274             return 0;
2275           if (!can_store_by_pieces (tree_low_cst (len, 1),
2276                                     builtin_memset_read_str, (PTR) &c,
2277                                     dest_align))
2278             return 0;
2279
2280           dest_mem = get_memory_rtx (dest);
2281           store_by_pieces (dest_mem, tree_low_cst (len, 1),
2282                            builtin_memset_read_str,
2283                            (PTR) &c, dest_align);
2284           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2285 #ifdef POINTERS_EXTEND_UNSIGNED
2286           if (GET_MODE (dest_mem) != ptr_mode)
2287             dest_mem = convert_memory_address (ptr_mode, dest_mem);
2288 #endif
2289           return dest_mem;
2290         }
2291
2292       len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2293
2294       dest_mem = get_memory_rtx (dest);
2295       set_mem_align (dest_mem, dest_align);
2296       dest_addr = clear_storage (dest_mem, len_rtx);
2297
2298       if (dest_addr == 0)
2299         {
2300           dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2301 #ifdef POINTERS_EXTEND_UNSIGNED
2302           if (GET_MODE (dest_addr) != ptr_mode)
2303             dest_addr = convert_memory_address (ptr_mode, dest_addr);
2304 #endif
2305         }
2306
2307       return dest_addr;
2308     }
2309 }
2310
2311 /* Expand expression EXP, which is a call to the bzero builtin.  Return 0
2312    if we failed the caller should emit a normal call.  */
2313
2314 static rtx
2315 expand_builtin_bzero (exp)
2316      tree exp;
2317 {
2318   tree arglist = TREE_OPERAND (exp, 1);
2319   tree dest, size, newarglist;
2320   rtx result;
2321
2322   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2323     return NULL_RTX;
2324
2325   dest = TREE_VALUE (arglist);
2326   size = TREE_VALUE (TREE_CHAIN (arglist));
2327
2328   /* New argument list transforming bzero(ptr x, int y) to
2329      memset(ptr x, int 0, size_t y).   This is done this way
2330      so that if it isn't expanded inline, we fallback to
2331      calling bzero instead of memset.  */
2332
2333   newarglist = build_tree_list (NULL_TREE, convert (sizetype, size));
2334   newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
2335   newarglist = tree_cons (NULL_TREE, dest, newarglist);
2336
2337   TREE_OPERAND (exp, 1) = newarglist;
2338   result = expand_builtin_memset (exp, const0_rtx, VOIDmode);
2339
2340   /* Always restore the original arguments.  */
2341   TREE_OPERAND (exp, 1) = arglist;
2342
2343   return result;
2344 }
2345
2346 /* Expand expression EXP, which is a call to the memcmp or the strcmp builtin.
2347    ARGLIST is the argument list for this call.  Return 0 if we failed and the
2348    caller should emit a normal call, otherwise try to get the result in
2349    TARGET, if convenient (and in mode MODE, if that's convenient).  */
2350
2351 static rtx
2352 expand_builtin_memcmp (exp, arglist, target, mode)
2353      tree exp ATTRIBUTE_UNUSED;
2354      tree arglist;
2355      rtx target;
2356      enum machine_mode mode;
2357 {
2358   tree arg1, arg2, len;
2359   const char *p1, *p2;
2360
2361   if (!validate_arglist (arglist,
2362                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2363     return 0;
2364
2365   arg1 = TREE_VALUE (arglist);
2366   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
2367   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2368
2369   /* If the len parameter is zero, return zero.  */
2370   if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0)
2371     {
2372       /* Evaluate and ignore arg1 and arg2 in case they have
2373          side-effects.  */
2374       expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2375       expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2376       return const0_rtx;
2377     }
2378
2379   p1 = c_getstr (arg1);
2380   p2 = c_getstr (arg2);
2381
2382   /* If all arguments are constant, and the value of len is not greater
2383      than the lengths of arg1 and arg2, evaluate at compile-time.  */
2384   if (host_integerp (len, 1) && p1 && p2
2385       && compare_tree_int (len, strlen (p1) + 1) <= 0
2386       && compare_tree_int (len, strlen (p2) + 1) <= 0)
2387     {
2388       const int r = memcmp (p1, p2, tree_low_cst (len, 1));
2389
2390       return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
2391     }
2392
2393   /* If len parameter is one, return an expression corresponding to
2394      (*(const unsigned char*)arg1 - (const unsigned char*)arg2).  */
2395   if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
2396     {
2397       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2398       tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
2399       tree ind1 =
2400       fold (build1 (CONVERT_EXPR, integer_type_node,
2401                     build1 (INDIRECT_REF, cst_uchar_node,
2402                             build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
2403       tree ind2 =
2404       fold (build1 (CONVERT_EXPR, integer_type_node,
2405                     build1 (INDIRECT_REF, cst_uchar_node,
2406                             build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
2407       tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
2408       return expand_expr (result, target, mode, EXPAND_NORMAL);
2409     }
2410
2411 #ifdef HAVE_cmpstrsi
2412   {
2413     rtx arg1_rtx, arg2_rtx, arg3_rtx;
2414     rtx result;
2415     rtx insn;
2416
2417     int arg1_align
2418       = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2419     int arg2_align
2420       = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2421     enum machine_mode insn_mode
2422       = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
2423
2424     /* If we don't have POINTER_TYPE, call the function.  */
2425     if (arg1_align == 0 || arg2_align == 0)
2426       return 0;
2427
2428     /* Make a place to write the result of the instruction.  */
2429     result = target;
2430     if (! (result != 0
2431            && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
2432            && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2433       result = gen_reg_rtx (insn_mode);
2434
2435     arg1_rtx = get_memory_rtx (arg1);
2436     arg2_rtx = get_memory_rtx (arg2);
2437     arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2438     if (!HAVE_cmpstrsi)
2439       insn = NULL_RTX;
2440     else
2441       insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
2442                            GEN_INT (MIN (arg1_align, arg2_align)));
2443
2444     if (insn)
2445       emit_insn (insn);
2446     else
2447       emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
2448                                TYPE_MODE (integer_type_node), 3,
2449                                XEXP (arg1_rtx, 0), Pmode,
2450                                XEXP (arg2_rtx, 0), Pmode,
2451                                convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
2452                                                 TREE_UNSIGNED (sizetype)),
2453                                TYPE_MODE (sizetype));
2454
2455     /* Return the value in the proper mode for this function.  */
2456     mode = TYPE_MODE (TREE_TYPE (exp));
2457     if (GET_MODE (result) == mode)
2458       return result;
2459     else if (target != 0)
2460       {
2461         convert_move (target, result, 0);
2462         return target;
2463       }
2464     else
2465       return convert_to_mode (mode, result, 0);
2466   }
2467 #endif
2468
2469   return 0;
2470 }
2471
2472 /* Expand expression EXP, which is a call to the strcmp builtin.  Return 0
2473    if we failed the caller should emit a normal call, otherwise try to get
2474    the result in TARGET, if convenient.  */
2475
2476 static rtx
2477 expand_builtin_strcmp (exp, target, mode)
2478      tree exp;
2479      rtx target;
2480      enum machine_mode mode;
2481 {
2482   tree arglist = TREE_OPERAND (exp, 1);
2483   tree arg1, arg2, len, len2, fn;
2484   const char *p1, *p2;
2485
2486   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2487     return 0;
2488
2489   arg1 = TREE_VALUE (arglist);
2490   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
2491
2492   p1 = c_getstr (arg1);
2493   p2 = c_getstr (arg2);
2494
2495   if (p1 && p2)
2496     {
2497       const int i = strcmp (p1, p2);
2498       return (i < 0 ? constm1_rtx : (i > 0 ? const1_rtx : const0_rtx));
2499     }
2500
2501   /* If either arg is "", return an expression corresponding to
2502      (*(const unsigned char*)arg1 - (const unsigned char*)arg2).  */
2503   if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
2504     {
2505       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2506       tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
2507       tree ind1 =
2508         fold (build1 (CONVERT_EXPR, integer_type_node,
2509                       build1 (INDIRECT_REF, cst_uchar_node,
2510                               build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
2511       tree ind2 =
2512         fold (build1 (CONVERT_EXPR, integer_type_node,
2513                       build1 (INDIRECT_REF, cst_uchar_node,
2514                               build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
2515       tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
2516       return expand_expr (result, target, mode, EXPAND_NORMAL);
2517     }
2518
2519   len = c_strlen (arg1);
2520   len2 = c_strlen (arg2);
2521
2522   if (len)
2523     len = size_binop (PLUS_EXPR, ssize_int (1), len);
2524
2525   if (len2)
2526     len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
2527
2528   /* If we don't have a constant length for the first, use the length
2529      of the second, if we know it.  We don't require a constant for
2530      this case; some cost analysis could be done if both are available
2531      but neither is constant.  For now, assume they're equally cheap
2532      unless one has side effects.
2533
2534      If both strings have constant lengths, use the smaller.  This
2535      could arise if optimization results in strcpy being called with
2536      two fixed strings, or if the code was machine-generated.  We should
2537      add some code to the `memcmp' handler below to deal with such
2538      situations, someday.  */
2539
2540   if (!len || TREE_CODE (len) != INTEGER_CST)
2541     {
2542       if (len2 && !TREE_SIDE_EFFECTS (len2))
2543         len = len2;
2544       else if (len == 0)
2545         return 0;
2546     }
2547   else if (len2 && TREE_CODE (len2) == INTEGER_CST
2548            && tree_int_cst_lt (len2, len))
2549     len = len2;
2550
2551   /* If both arguments have side effects, we cannot optimize.  */
2552   if (TREE_SIDE_EFFECTS (len))
2553     return 0;
2554
2555   fn = built_in_decls[BUILT_IN_MEMCMP];
2556   if (!fn)
2557     return 0;
2558
2559   chainon (arglist, build_tree_list (NULL_TREE, len));
2560   return expand_expr (build_function_call_expr (fn, arglist),
2561                       target, mode, EXPAND_NORMAL);
2562 }
2563
2564 /* Expand expression EXP, which is a call to the strncmp builtin.  Return 0
2565    if we failed the caller should emit a normal call, otherwise try to get
2566    the result in TARGET, if convenient.  */
2567
2568 static rtx
2569 expand_builtin_strncmp (exp, target, mode)
2570      tree exp;
2571      rtx target;
2572      enum machine_mode mode;
2573 {
2574   tree arglist = TREE_OPERAND (exp, 1);
2575   tree fn, newarglist, len = 0;
2576   tree arg1, arg2, arg3;
2577   const char *p1, *p2;
2578
2579   if (!validate_arglist (arglist,
2580                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2581     return 0;
2582
2583   arg1 = TREE_VALUE (arglist);
2584   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
2585   arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2586
2587   /* If the len parameter is zero, return zero.  */
2588   if (host_integerp (arg3, 1) && tree_low_cst (arg3, 1) == 0)
2589     {
2590       /* Evaluate and ignore arg1 and arg2 in case they have
2591          side-effects.  */
2592       expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2593       expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2594       return const0_rtx;
2595     }
2596
2597   p1 = c_getstr (arg1);
2598   p2 = c_getstr (arg2);
2599
2600   /* If all arguments are constant, evaluate at compile-time.  */
2601   if (host_integerp (arg3, 1) && p1 && p2)
2602     {
2603       const int r = strncmp (p1, p2, tree_low_cst (arg3, 1));
2604       return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
2605     }
2606
2607   /* If len == 1 or (either string parameter is "" and (len >= 1)),
2608       return (*(const u_char*)arg1 - *(const u_char*)arg2).  */
2609   if (host_integerp (arg3, 1)
2610       && (tree_low_cst (arg3, 1) == 1
2611           || (tree_low_cst (arg3, 1) > 1
2612               && ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')))))
2613     {
2614       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2615       tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
2616       tree ind1 =
2617         fold (build1 (CONVERT_EXPR, integer_type_node,
2618                       build1 (INDIRECT_REF, cst_uchar_node,
2619                               build1 (NOP_EXPR, cst_uchar_ptr_node, arg1))));
2620       tree ind2 =
2621         fold (build1 (CONVERT_EXPR, integer_type_node,
2622                       build1 (INDIRECT_REF, cst_uchar_node,
2623                               build1 (NOP_EXPR, cst_uchar_ptr_node, arg2))));
2624       tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2));
2625       return expand_expr (result, target, mode, EXPAND_NORMAL);
2626     }
2627
2628   /* If c_strlen can determine an expression for one of the string
2629      lengths, and it doesn't have side effects, then call
2630      expand_builtin_memcmp() using length MIN(strlen(string)+1, arg3).  */
2631
2632   /* Perhaps one of the strings is really constant, if so prefer
2633      that constant length over the other string's length.  */
2634   if (p1)
2635     len = c_strlen (arg1);
2636   else if (p2)
2637     len = c_strlen (arg2);
2638
2639   /* If we still don't have a len, try either string arg as long
2640      as they don't have side effects.  */
2641   if (!len && !TREE_SIDE_EFFECTS (arg1))
2642     len = c_strlen (arg1);
2643   if (!len && !TREE_SIDE_EFFECTS (arg2))
2644     len = c_strlen (arg2);
2645   /* If we still don't have a length, punt.  */
2646   if (!len)
2647     return 0;
2648
2649   fn = built_in_decls[BUILT_IN_MEMCMP];
2650   if (!fn)
2651     return 0;
2652
2653   /* Add one to the string length.  */
2654   len = fold (size_binop (PLUS_EXPR, len, ssize_int (1)));
2655
2656   /* The actual new length parameter is MIN(len,arg3).  */
2657   len = fold (build (MIN_EXPR, TREE_TYPE (len), len, arg3));
2658
2659   newarglist = build_tree_list (NULL_TREE, len);
2660   newarglist = tree_cons (NULL_TREE, arg2, newarglist);
2661   newarglist = tree_cons (NULL_TREE, arg1, newarglist);
2662   return expand_expr (build_function_call_expr (fn, newarglist),
2663                       target, mode, EXPAND_NORMAL);
2664 }
2665
2666 /* Expand expression EXP, which is a call to the strcat builtin.
2667    Return 0 if we failed the caller should emit a normal call,
2668    otherwise try to get the result in TARGET, if convenient.  */
2669
2670 static rtx
2671 expand_builtin_strcat (arglist, target, mode)
2672      tree arglist;
2673      rtx target;
2674      enum machine_mode mode;
2675 {
2676   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2677     return 0;
2678   else
2679     {
2680       tree dst = TREE_VALUE (arglist),
2681         src = TREE_VALUE (TREE_CHAIN (arglist));
2682       const char *p = c_getstr (src);
2683
2684       /* If the string length is zero, return the dst parameter.  */
2685       if (p && *p == '\0')
2686         return expand_expr (dst, target, mode, EXPAND_NORMAL);
2687
2688       return 0;
2689     }
2690 }
2691
2692 /* Expand expression EXP, which is a call to the strncat builtin.
2693    Return 0 if we failed the caller should emit a normal call,
2694    otherwise try to get the result in TARGET, if convenient.  */
2695
2696 static rtx
2697 expand_builtin_strncat (arglist, target, mode)
2698      tree arglist;
2699      rtx target;
2700      enum machine_mode mode;
2701 {
2702   if (!validate_arglist (arglist,
2703                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2704     return 0;
2705   else
2706     {
2707       tree dst = TREE_VALUE (arglist),
2708         src = TREE_VALUE (TREE_CHAIN (arglist)),
2709         len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2710       const char *p = c_getstr (src);
2711
2712       /* If the requested length is zero, or the src parameter string
2713           length is zero, return the dst parameter.  */
2714       if (integer_zerop (len) || (p && *p == '\0'))
2715         {
2716           /* Evaluate and ignore the src and len parameters in case
2717              they have side-effects.  */
2718           expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2719           expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2720           return expand_expr (dst, target, mode, EXPAND_NORMAL);
2721         }
2722
2723       /* If the requested len is greater than or equal to the string
2724          length, call strcat.  */
2725       if (TREE_CODE (len) == INTEGER_CST && p
2726           && compare_tree_int (len, strlen (p)) >= 0)
2727         {
2728           tree newarglist
2729             = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
2730           tree fn = built_in_decls[BUILT_IN_STRCAT];
2731
2732           /* If the replacement _DECL isn't initialized, don't do the
2733              transformation.  */
2734           if (!fn)
2735             return 0;
2736
2737           return expand_expr (build_function_call_expr (fn, newarglist),
2738                               target, mode, EXPAND_NORMAL);
2739         }
2740       return 0;
2741     }
2742 }
2743
2744 /* Expand expression EXP, which is a call to the strspn builtin.
2745    Return 0 if we failed the caller should emit a normal call,
2746    otherwise try to get the result in TARGET, if convenient.  */
2747
2748 static rtx
2749 expand_builtin_strspn (arglist, target, mode)
2750      tree arglist;
2751      rtx target;
2752      enum machine_mode mode;
2753 {
2754   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2755     return 0;
2756   else
2757     {
2758       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2759       const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
2760
2761       /* If both arguments are constants, evaluate at compile-time.  */
2762       if (p1 && p2)
2763         {
2764           const size_t r = strspn (p1, p2);
2765           return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
2766         }
2767
2768       /* If either argument is "", return 0.  */
2769       if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
2770         {
2771           /* Evaluate and ignore both arguments in case either one has
2772              side-effects.  */
2773           expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2774           expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2775           return const0_rtx;
2776         }
2777       return 0;
2778     }
2779 }
2780
2781 /* Expand expression EXP, which is a call to the strcspn builtin.
2782    Return 0 if we failed the caller should emit a normal call,
2783    otherwise try to get the result in TARGET, if convenient.  */
2784
2785 static rtx
2786 expand_builtin_strcspn (arglist, target, mode)
2787      tree arglist;
2788      rtx target;
2789      enum machine_mode mode;
2790 {
2791   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2792     return 0;
2793   else
2794     {
2795       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
2796       const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
2797
2798       /* If both arguments are constants, evaluate at compile-time.  */
2799       if (p1 && p2)
2800         {
2801           const size_t r = strcspn (p1, p2);
2802           return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
2803         }
2804
2805       /* If the first argument is "", return 0.  */
2806       if (p1 && *p1 == '\0')
2807         {
2808           /* Evaluate and ignore argument s2 in case it has
2809              side-effects.  */
2810           expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
2811           return const0_rtx;
2812         }
2813
2814       /* If the second argument is "", return __builtin_strlen(s1).  */
2815       if (p2 && *p2 == '\0')
2816         {
2817           tree newarglist = build_tree_list (NULL_TREE, s1),
2818             fn = built_in_decls[BUILT_IN_STRLEN];
2819
2820           /* If the replacement _DECL isn't initialized, don't do the
2821              transformation.  */
2822           if (!fn)
2823             return 0;
2824
2825           return expand_expr (build_function_call_expr (fn, newarglist),
2826                               target, mode, EXPAND_NORMAL);
2827         }
2828       return 0;
2829     }
2830 }
2831
2832 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
2833    if that's convenient.  */
2834
2835 rtx
2836 expand_builtin_saveregs ()
2837 {
2838   rtx val, seq;
2839
2840   /* Don't do __builtin_saveregs more than once in a function.
2841      Save the result of the first call and reuse it.  */
2842   if (saveregs_value != 0)
2843     return saveregs_value;
2844
2845   /* When this function is called, it means that registers must be
2846      saved on entry to this function.  So we migrate the call to the
2847      first insn of this function.  */
2848
2849   start_sequence ();
2850
2851 #ifdef EXPAND_BUILTIN_SAVEREGS
2852   /* Do whatever the machine needs done in this case.  */
2853   val = EXPAND_BUILTIN_SAVEREGS ();
2854 #else
2855   /* ??? We used to try and build up a call to the out of line function,
2856      guessing about what registers needed saving etc.  This became much
2857      harder with __builtin_va_start, since we don't have a tree for a
2858      call to __builtin_saveregs to fall back on.  There was exactly one
2859      port (i860) that used this code, and I'm unconvinced it could actually
2860      handle the general case.  So we no longer try to handle anything
2861      weird and make the backend absorb the evil.  */
2862
2863   error ("__builtin_saveregs not supported by this target");
2864   val = const0_rtx;
2865 #endif
2866
2867   seq = get_insns ();
2868   end_sequence ();
2869
2870   saveregs_value = val;
2871
2872   /* Put the insns after the NOTE that starts the function.  If this
2873      is inside a start_sequence, make the outer-level insn chain current, so
2874      the code is placed at the start of the function.  */
2875   push_topmost_sequence ();
2876   emit_insn_after (seq, get_insns ());
2877   pop_topmost_sequence ();
2878
2879   return val;
2880 }
2881
2882 /* __builtin_args_info (N) returns word N of the arg space info
2883    for the current function.  The number and meanings of words
2884    is controlled by the definition of CUMULATIVE_ARGS.  */
2885
2886 static rtx
2887 expand_builtin_args_info (exp)
2888      tree exp;
2889 {
2890   tree arglist = TREE_OPERAND (exp, 1);
2891   int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
2892   int *word_ptr = (int *) &current_function_args_info;
2893 #if 0
2894   /* These are used by the code below that is if 0'ed away */
2895   int i;
2896   tree type, elts, result;
2897 #endif
2898
2899   if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0)
2900     abort ();
2901
2902   if (arglist != 0)
2903     {
2904       if (!host_integerp (TREE_VALUE (arglist), 0))
2905         error ("argument of `__builtin_args_info' must be constant");
2906       else
2907         {
2908           HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
2909
2910           if (wordnum < 0 || wordnum >= nwords)
2911             error ("argument of `__builtin_args_info' out of range");
2912           else
2913             return GEN_INT (word_ptr[wordnum]);
2914         }
2915     }
2916   else
2917     error ("missing argument in `__builtin_args_info'");
2918
2919   return const0_rtx;
2920
2921 #if 0
2922   for (i = 0; i < nwords; i++)
2923     elts = tree_cons (NULL_TREE, build_int_2 (word_ptr[i], 0));
2924
2925   type = build_array_type (integer_type_node,
2926                            build_index_type (build_int_2 (nwords, 0)));
2927   result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (elts));
2928   TREE_CONSTANT (result) = 1;
2929   TREE_STATIC (result) = 1;
2930   result = build1 (INDIRECT_REF, build_pointer_type (type), result);
2931   TREE_CONSTANT (result) = 1;
2932   return expand_expr (result, NULL_RTX, VOIDmode, 0);
2933 #endif
2934 }
2935
2936 /* Expand ARGLIST, from a call to __builtin_next_arg.  */
2937
2938 static rtx
2939 expand_builtin_next_arg (arglist)
2940      tree arglist;
2941 {
2942   tree fntype = TREE_TYPE (current_function_decl);
2943
2944   if (TYPE_ARG_TYPES (fntype) == 0
2945       || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
2946           == void_type_node))
2947     {
2948       error ("`va_start' used in function with fixed args");
2949       return const0_rtx;
2950     }
2951
2952   if (arglist)
2953     {
2954       tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
2955       tree arg = TREE_VALUE (arglist);
2956
2957       /* Strip off all nops for the sake of the comparison.  This
2958          is not quite the same as STRIP_NOPS.  It does more.
2959          We must also strip off INDIRECT_EXPR for C++ reference
2960          parameters.  */
2961       while (TREE_CODE (arg) == NOP_EXPR
2962              || TREE_CODE (arg) == CONVERT_EXPR
2963              || TREE_CODE (arg) == NON_LVALUE_EXPR
2964              || TREE_CODE (arg) == INDIRECT_REF)
2965         arg = TREE_OPERAND (arg, 0);
2966       if (arg != last_parm)
2967         warning ("second parameter of `va_start' not last named argument");
2968     }
2969   else
2970     /* Evidently an out of date version of <stdarg.h>; can't validate
2971        va_start's second argument, but can still work as intended.  */
2972     warning ("`__builtin_next_arg' called without an argument");
2973
2974   return expand_binop (Pmode, add_optab,
2975                        current_function_internal_arg_pointer,
2976                        current_function_arg_offset_rtx,
2977                        NULL_RTX, 0, OPTAB_LIB_WIDEN);
2978 }
2979
2980 /* Make it easier for the backends by protecting the valist argument
2981    from multiple evaluations.  */
2982
2983 static tree
2984 stabilize_va_list (valist, needs_lvalue)
2985      tree valist;
2986      int needs_lvalue;
2987 {
2988   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2989     {
2990       if (TREE_SIDE_EFFECTS (valist))
2991         valist = save_expr (valist);
2992
2993       /* For this case, the backends will be expecting a pointer to
2994          TREE_TYPE (va_list_type_node), but it's possible we've
2995          actually been given an array (an actual va_list_type_node).
2996          So fix it.  */
2997       if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
2998         {
2999           tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3000           tree p2 = build_pointer_type (va_list_type_node);
3001
3002           valist = build1 (ADDR_EXPR, p2, valist);
3003           valist = fold (build1 (NOP_EXPR, p1, valist));
3004         }
3005     }
3006   else
3007     {
3008       tree pt;
3009
3010       if (! needs_lvalue)
3011         {
3012           if (! TREE_SIDE_EFFECTS (valist))
3013             return valist;
3014
3015           pt = build_pointer_type (va_list_type_node);
3016           valist = fold (build1 (ADDR_EXPR, pt, valist));
3017           TREE_SIDE_EFFECTS (valist) = 1;
3018         }
3019
3020       if (TREE_SIDE_EFFECTS (valist))
3021         valist = save_expr (valist);
3022       valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)),
3023                              valist));
3024     }
3025
3026   return valist;
3027 }
3028
3029 /* The "standard" implementation of va_start: just assign `nextarg' to
3030    the variable.  */
3031
3032 void
3033 std_expand_builtin_va_start (valist, nextarg)
3034      tree valist;
3035      rtx nextarg;
3036 {
3037   tree t;
3038
3039   t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
3040              make_tree (ptr_type_node, nextarg));
3041   TREE_SIDE_EFFECTS (t) = 1;
3042
3043   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3044 }
3045
3046 /* Expand ARGLIST, from a call to __builtin_va_start.  */
3047
3048 static rtx
3049 expand_builtin_va_start (arglist)
3050      tree arglist;
3051 {
3052   rtx nextarg;
3053   tree chain, valist;
3054
3055   chain = TREE_CHAIN (arglist);
3056
3057   if (TREE_CHAIN (chain))
3058     error ("too many arguments to function `va_start'");
3059
3060   nextarg = expand_builtin_next_arg (chain);
3061   valist = stabilize_va_list (TREE_VALUE (arglist), 1);
3062
3063 #ifdef EXPAND_BUILTIN_VA_START
3064   EXPAND_BUILTIN_VA_START (valist, nextarg);
3065 #else
3066   std_expand_builtin_va_start (valist, nextarg);
3067 #endif
3068
3069   return const0_rtx;
3070 }
3071
3072 /* The "standard" implementation of va_arg: read the value from the
3073    current (padded) address and increment by the (padded) size.  */
3074
3075 rtx
3076 std_expand_builtin_va_arg (valist, type)
3077      tree valist, type;
3078 {
3079   tree addr_tree, t, type_size = NULL;
3080   tree align, alignm1;
3081   tree rounded_size;
3082   rtx addr;
3083
3084   /* Compute the rounded size of the type.  */
3085   align = size_int (PARM_BOUNDARY / BITS_PER_UNIT);
3086   alignm1 = size_int (PARM_BOUNDARY / BITS_PER_UNIT - 1);
3087   if (type == error_mark_node
3088       || (type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type))) == NULL
3089       || TREE_OVERFLOW (type_size))
3090     rounded_size = size_zero_node;
3091   else
3092     rounded_size = fold (build (MULT_EXPR, sizetype,
3093                                 fold (build (TRUNC_DIV_EXPR, sizetype,
3094                                              fold (build (PLUS_EXPR, sizetype,
3095                                                           type_size, alignm1)),
3096                                              align)),
3097                                 align));
3098
3099   /* Get AP.  */
3100   addr_tree = valist;
3101   if (PAD_VARARGS_DOWN && ! integer_zerop (rounded_size))
3102     {
3103       /* Small args are padded downward.  */
3104       addr_tree = fold (build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
3105                                fold (build (COND_EXPR, sizetype,
3106                                             fold (build (GT_EXPR, sizetype,
3107                                                          rounded_size,
3108                                                          align)),
3109                                             size_zero_node,
3110                                             fold (build (MINUS_EXPR, sizetype,
3111                                                          rounded_size,
3112                                                          type_size))))));
3113     }
3114
3115   addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
3116   addr = copy_to_reg (addr);
3117
3118   /* Compute new value for AP.  */
3119   if (! integer_zerop (rounded_size))
3120     {
3121       t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
3122                  build (PLUS_EXPR, TREE_TYPE (valist), valist,
3123                         rounded_size));
3124       TREE_SIDE_EFFECTS (t) = 1;
3125       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3126     }
3127
3128   return addr;
3129 }
3130
3131 /* Expand __builtin_va_arg, which is not really a builtin function, but
3132    a very special sort of operator.  */
3133
3134 rtx
3135 expand_builtin_va_arg (valist, type)
3136      tree valist, type;
3137 {
3138   rtx addr, result;
3139   tree promoted_type, want_va_type, have_va_type;
3140
3141   /* Verify that valist is of the proper type.  */
3142
3143   want_va_type = va_list_type_node;
3144   have_va_type = TREE_TYPE (valist);
3145   if (TREE_CODE (want_va_type) == ARRAY_TYPE)
3146     {
3147       /* If va_list is an array type, the argument may have decayed
3148          to a pointer type, e.g. by being passed to another function.
3149          In that case, unwrap both types so that we can compare the
3150          underlying records.  */
3151       if (TREE_CODE (have_va_type) == ARRAY_TYPE
3152           || TREE_CODE (have_va_type) == POINTER_TYPE)
3153         {
3154           want_va_type = TREE_TYPE (want_va_type);
3155           have_va_type = TREE_TYPE (have_va_type);
3156         }
3157     }
3158   if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
3159     {
3160       error ("first argument to `va_arg' not of type `va_list'");
3161       addr = const0_rtx;
3162     }
3163
3164   /* Generate a diagnostic for requesting data of a type that cannot
3165      be passed through `...' due to type promotion at the call site.  */
3166   else if ((promoted_type = (*lang_hooks.types.type_promotes_to) (type))
3167            != type)
3168     {
3169       const char *name = "<anonymous type>", *pname = 0;
3170       static bool gave_help;
3171
3172       if (TYPE_NAME (type))
3173         {
3174           if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3175             name = IDENTIFIER_POINTER (TYPE_NAME (type));
3176           else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
3177                    && DECL_NAME (TYPE_NAME (type)))
3178             name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3179         }
3180       if (TYPE_NAME (promoted_type))
3181         {
3182           if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE)
3183             pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type));
3184           else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL
3185                    && DECL_NAME (TYPE_NAME (promoted_type)))
3186             pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type)));
3187         }
3188
3189       /* Unfortunately, this is merely undefined, rather than a constraint
3190          violation, so we cannot make this an error.  If this call is never
3191          executed, the program is still strictly conforming.  */
3192       warning ("`%s' is promoted to `%s' when passed through `...'",
3193                name, pname);
3194       if (! gave_help)
3195         {
3196           gave_help = true;
3197           warning ("(so you should pass `%s' not `%s' to `va_arg')",
3198                    pname, name);
3199         }
3200
3201       /* We can, however, treat "undefined" any way we please.
3202          Call abort to encourage the user to fix the program.  */
3203       expand_builtin_trap ();
3204
3205       /* This is dead code, but go ahead and finish so that the
3206          mode of the result comes out right.  */
3207       addr = const0_rtx;
3208     }
3209   else
3210     {
3211       /* Make it easier for the backends by protecting the valist argument
3212          from multiple evaluations.  */
3213       valist = stabilize_va_list (valist, 0);
3214
3215 #ifdef EXPAND_BUILTIN_VA_ARG
3216       addr = EXPAND_BUILTIN_VA_ARG (valist, type);
3217 #else
3218       addr = std_expand_builtin_va_arg (valist, type);
3219 #endif
3220     }
3221
3222 #ifdef POINTERS_EXTEND_UNSIGNED
3223   if (GET_MODE (addr) != Pmode)
3224     addr = convert_memory_address (Pmode, addr);
3225 #endif
3226
3227   result = gen_rtx_MEM (TYPE_MODE (type), addr);
3228   set_mem_alias_set (result, get_varargs_alias_set ());
3229
3230   return result;
3231 }
3232
3233 /* Expand ARGLIST, from a call to __builtin_va_end.  */
3234
3235 static rtx
3236 expand_builtin_va_end (arglist)
3237      tree arglist;
3238 {
3239   tree valist = TREE_VALUE (arglist);
3240
3241 #ifdef EXPAND_BUILTIN_VA_END
3242   valist = stabilize_va_list (valist, 0);
3243   EXPAND_BUILTIN_VA_END(arglist);
3244 #else
3245   /* Evaluate for side effects, if needed.  I hate macros that don't
3246      do that.  */
3247   if (TREE_SIDE_EFFECTS (valist))
3248     expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
3249 #endif
3250
3251   return const0_rtx;
3252 }
3253
3254 /* Expand ARGLIST, from a call to __builtin_va_copy.  We do this as a
3255    builtin rather than just as an assignment in stdarg.h because of the
3256    nastiness of array-type va_list types.  */
3257
3258 static rtx
3259 expand_builtin_va_copy (arglist)
3260      tree arglist;
3261 {
3262   tree dst, src, t;
3263
3264   dst = TREE_VALUE (arglist);
3265   src = TREE_VALUE (TREE_CHAIN (arglist));
3266
3267   dst = stabilize_va_list (dst, 1);
3268   src = stabilize_va_list (src, 0);
3269
3270   if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
3271     {
3272       t = build (MODIFY_EXPR, va_list_type_node, dst, src);
3273       TREE_SIDE_EFFECTS (t) = 1;
3274       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3275     }
3276   else
3277     {
3278       rtx dstb, srcb, size;
3279
3280       /* Evaluate to pointers.  */
3281       dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
3282       srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
3283       size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
3284                           VOIDmode, EXPAND_NORMAL);
3285
3286 #ifdef POINTERS_EXTEND_UNSIGNED
3287       if (GET_MODE (dstb) != Pmode)
3288         dstb = convert_memory_address (Pmode, dstb);
3289
3290       if (GET_MODE (srcb) != Pmode)
3291         srcb = convert_memory_address (Pmode, srcb);
3292 #endif
3293
3294       /* "Dereference" to BLKmode memories.  */
3295       dstb = gen_rtx_MEM (BLKmode, dstb);
3296       set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
3297       set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
3298       srcb = gen_rtx_MEM (BLKmode, srcb);
3299       set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
3300       set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
3301
3302       /* Copy.  */
3303       emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
3304     }
3305
3306   return const0_rtx;
3307 }
3308
3309 /* Expand a call to one of the builtin functions __builtin_frame_address or
3310    __builtin_return_address.  */
3311
3312 static rtx
3313 expand_builtin_frame_address (exp)
3314      tree exp;
3315 {
3316   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3317   tree arglist = TREE_OPERAND (exp, 1);
3318
3319   /* The argument must be a nonnegative integer constant.
3320      It counts the number of frames to scan up the stack.
3321      The value is the return address saved in that frame.  */
3322   if (arglist == 0)
3323     /* Warning about missing arg was already issued.  */
3324     return const0_rtx;
3325   else if (! host_integerp (TREE_VALUE (arglist), 1))
3326     {
3327       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
3328         error ("invalid arg to `__builtin_frame_address'");
3329       else
3330         error ("invalid arg to `__builtin_return_address'");
3331       return const0_rtx;
3332     }
3333   else
3334     {
3335       rtx tem
3336         = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
3337                                       tree_low_cst (TREE_VALUE (arglist), 1),
3338                                       hard_frame_pointer_rtx);
3339
3340       /* Some ports cannot access arbitrary stack frames.  */
3341       if (tem == NULL)
3342         {
3343           if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
3344             warning ("unsupported arg to `__builtin_frame_address'");
3345           else
3346             warning ("unsupported arg to `__builtin_return_address'");
3347           return const0_rtx;
3348         }
3349
3350       /* For __builtin_frame_address, return what we've got.  */
3351       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
3352         return tem;
3353
3354       if (GET_CODE (tem) != REG
3355           && ! CONSTANT_P (tem))
3356         tem = copy_to_mode_reg (Pmode, tem);
3357       return tem;
3358     }
3359 }
3360
3361 /* Expand a call to the alloca builtin, with arguments ARGLIST.  Return 0 if
3362    we failed and the caller should emit a normal call, otherwise try to get
3363    the result in TARGET, if convenient.  */
3364
3365 static rtx
3366 expand_builtin_alloca (arglist, target)
3367      tree arglist;
3368      rtx target;
3369 {
3370   rtx op0;
3371   rtx result;
3372
3373   if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
3374     return 0;
3375
3376   /* Compute the argument.  */
3377   op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
3378
3379   /* Allocate the desired space.  */
3380   result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
3381
3382 #ifdef POINTERS_EXTEND_UNSIGNED
3383   if (GET_MODE (result) != ptr_mode)
3384     result = convert_memory_address (ptr_mode, result);
3385 #endif
3386
3387   return result;
3388 }
3389
3390 /* Expand a call to the ffs builtin.  The arguments are in ARGLIST.
3391    Return 0 if a normal call should be emitted rather than expanding the
3392    function in-line.  If convenient, the result should be placed in TARGET.
3393    SUBTARGET may be used as the target for computing one of EXP's operands.  */
3394
3395 static rtx
3396 expand_builtin_ffs (arglist, target, subtarget)
3397      tree arglist;
3398      rtx target, subtarget;
3399 {
3400   rtx op0;
3401   if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
3402     return 0;
3403
3404   /* Compute the argument.  */
3405   op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
3406   /* Compute ffs, into TARGET if possible.
3407      Set TARGET to wherever the result comes back.  */
3408   target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
3409                         ffs_optab, op0, target, 1);
3410   if (target == 0)
3411     abort ();
3412   return target;
3413 }
3414
3415 /* If the string passed to fputs is a constant and is one character
3416    long, we attempt to transform this call into __builtin_fputc().  */
3417
3418 static rtx
3419 expand_builtin_fputs (arglist, ignore, unlocked)
3420      tree arglist;
3421      int ignore;
3422      int unlocked;
3423 {
3424   tree len, fn;
3425   tree fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
3426     : built_in_decls[BUILT_IN_FPUTC];
3427   tree fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
3428     : built_in_decls[BUILT_IN_FWRITE];
3429
3430   /* If the return value is used, or the replacement _DECL isn't
3431      initialized, don't do the transformation.  */
3432   if (!ignore || !fn_fputc || !fn_fwrite)
3433     return 0;
3434
3435   /* Verify the arguments in the original call.  */
3436   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3437     return 0;
3438
3439   /* Get the length of the string passed to fputs.  If the length
3440      can't be determined, punt.  */
3441   if (!(len = c_strlen (TREE_VALUE (arglist)))
3442       || TREE_CODE (len) != INTEGER_CST)
3443     return 0;
3444
3445   switch (compare_tree_int (len, 1))
3446     {
3447     case -1: /* length is 0, delete the call entirely .  */
3448       {
3449         /* Evaluate and ignore the argument in case it has
3450            side-effects.  */
3451         expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
3452                      VOIDmode, EXPAND_NORMAL);
3453         return const0_rtx;
3454       }
3455     case 0: /* length is 1, call fputc.  */
3456       {
3457         const char *p = c_getstr (TREE_VALUE (arglist));
3458
3459         if (p != NULL)
3460           {
3461             /* New argument list transforming fputs(string, stream) to
3462                fputc(string[0], stream).  */
3463             arglist =
3464               build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
3465             arglist =
3466               tree_cons (NULL_TREE, build_int_2 (p[0], 0), arglist);
3467             fn = fn_fputc;
3468             break;
3469           }
3470       }
3471       /* FALLTHROUGH */
3472     case 1: /* length is greater than 1, call fwrite.  */
3473       {
3474         tree string_arg = TREE_VALUE (arglist);
3475
3476         /* New argument list transforming fputs(string, stream) to
3477            fwrite(string, 1, len, stream).  */
3478         arglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
3479         arglist = tree_cons (NULL_TREE, len, arglist);
3480         arglist = tree_cons (NULL_TREE, size_one_node, arglist);
3481         arglist = tree_cons (NULL_TREE, string_arg, arglist);
3482         fn = fn_fwrite;
3483         break;
3484       }
3485     default:
3486       abort ();
3487     }
3488
3489   return expand_expr (build_function_call_expr (fn, arglist),
3490                       (ignore ? const0_rtx : NULL_RTX),
3491                       VOIDmode, EXPAND_NORMAL);
3492 }
3493
3494 /* Expand a call to __builtin_expect.  We return our argument and emit a
3495    NOTE_INSN_EXPECTED_VALUE note.  This is the expansion of __builtin_expect in
3496    a non-jump context.  */
3497
3498 static rtx
3499 expand_builtin_expect (arglist, target)
3500      tree arglist;
3501      rtx target;
3502 {
3503   tree exp, c;
3504   rtx note, rtx_c;
3505
3506   if (arglist == NULL_TREE
3507       || TREE_CHAIN (arglist) == NULL_TREE)
3508     return const0_rtx;
3509   exp = TREE_VALUE (arglist);
3510   c = TREE_VALUE (TREE_CHAIN (arglist));
3511
3512   if (TREE_CODE (c) != INTEGER_CST)
3513     {
3514       error ("second arg to `__builtin_expect' must be a constant");
3515       c = integer_zero_node;
3516     }
3517
3518   target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
3519
3520   /* Don't bother with expected value notes for integral constants.  */
3521   if (GET_CODE (target) != CONST_INT)
3522     {
3523       /* We do need to force this into a register so that we can be
3524          moderately sure to be able to correctly interpret the branch
3525          condition later.  */
3526       target = force_reg (GET_MODE (target), target);
3527
3528       rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
3529
3530       note = emit_note (NULL, NOTE_INSN_EXPECTED_VALUE);
3531       NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
3532     }
3533
3534   return target;
3535 }
3536
3537 /* Like expand_builtin_expect, except do this in a jump context.  This is
3538    called from do_jump if the conditional is a __builtin_expect.  Return either
3539    a list of insns to emit the jump or NULL if we cannot optimize
3540    __builtin_expect.  We need to optimize this at jump time so that machines
3541    like the PowerPC don't turn the test into a SCC operation, and then jump
3542    based on the test being 0/1.  */
3543
3544 rtx
3545 expand_builtin_expect_jump (exp, if_false_label, if_true_label)
3546      tree exp;
3547      rtx if_false_label;
3548      rtx if_true_label;
3549 {
3550   tree arglist = TREE_OPERAND (exp, 1);
3551   tree arg0 = TREE_VALUE (arglist);
3552   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3553   rtx ret = NULL_RTX;
3554
3555   /* Only handle __builtin_expect (test, 0) and
3556      __builtin_expect (test, 1).  */
3557   if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
3558       && (integer_zerop (arg1) || integer_onep (arg1)))
3559     {
3560       int num_jumps = 0;
3561       rtx insn;
3562
3563       /* If we fail to locate an appropriate conditional jump, we'll
3564          fall back to normal evaluation.  Ensure that the expression
3565          can be re-evaluated.  */
3566       switch (unsafe_for_reeval (arg0))
3567         {
3568         case 0: /* Safe.  */
3569           break;
3570
3571         case 1: /* Mildly unsafe.  */
3572           arg0 = unsave_expr (arg0);
3573           break;
3574
3575         case 2: /* Wildly unsafe.  */
3576           return NULL_RTX;
3577         }
3578
3579       /* Expand the jump insns.  */
3580       start_sequence ();
3581       do_jump (arg0, if_false_label, if_true_label);
3582       ret = get_insns ();
3583       end_sequence ();
3584
3585       /* Now that the __builtin_expect has been validated, go through and add
3586          the expect's to each of the conditional jumps.  If we run into an
3587          error, just give up and generate the 'safe' code of doing a SCC
3588          operation and then doing a branch on that.  */
3589       insn = ret;
3590       while (insn != NULL_RTX)
3591         {
3592           rtx next = NEXT_INSN (insn);
3593           rtx pattern;
3594
3595           if (GET_CODE (insn) == JUMP_INSN && any_condjump_p (insn)
3596               && (pattern = pc_set (insn)) != NULL_RTX)
3597             {
3598               rtx ifelse = SET_SRC (pattern);
3599               rtx label;
3600               int taken;
3601
3602               if (GET_CODE (ifelse) != IF_THEN_ELSE)
3603                 goto do_next_insn;
3604
3605               if (GET_CODE (XEXP (ifelse, 1)) == LABEL_REF)
3606                 {
3607                   taken = 1;
3608                   label = XEXP (XEXP (ifelse, 1), 0);
3609                 }
3610               /* An inverted jump reverses the probabilities.  */
3611               else if (GET_CODE (XEXP (ifelse, 2)) == LABEL_REF)
3612                 {
3613                   taken = 0;
3614                   label = XEXP (XEXP (ifelse, 2), 0);
3615                 }
3616               /* We shouldn't have to worry about conditional returns during
3617                  the expansion stage, but handle it gracefully anyway.  */
3618               else if (GET_CODE (XEXP (ifelse, 1)) == RETURN)
3619                 {
3620                   taken = 1;
3621                   label = NULL_RTX;
3622                 }
3623               /* An inverted return reverses the probabilities.  */
3624               else if (GET_CODE (XEXP (ifelse, 2)) == RETURN)
3625                 {
3626                   taken = 0;
3627                   label = NULL_RTX;
3628                 }
3629               else
3630                 goto do_next_insn;
3631
3632               /* If the test is expected to fail, reverse the
3633                  probabilities.  */
3634               if (integer_zerop (arg1))
3635                 taken = 1 - taken;
3636
3637               /* If we are jumping to the false label, reverse the
3638                  probabilities.  */
3639               if (label == NULL_RTX)
3640                 ;               /* conditional return */
3641               else if (label == if_false_label)
3642                 taken = 1 - taken;
3643               else if (label != if_true_label)
3644                 goto do_next_insn;
3645
3646               num_jumps++;
3647               predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
3648             }
3649
3650         do_next_insn:
3651           insn = next;
3652         }
3653
3654       /* If no jumps were modified, fail and do __builtin_expect the normal
3655          way.  */
3656       if (num_jumps == 0)
3657         ret = NULL_RTX;
3658     }
3659
3660   return ret;
3661 }
3662
3663 void
3664 expand_builtin_trap ()
3665 {
3666 #ifdef HAVE_trap
3667   if (HAVE_trap)
3668     emit_insn (gen_trap ());
3669   else
3670 #endif
3671     emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
3672   emit_barrier ();
3673 }
3674 \f
3675 /* Expand an expression EXP that calls a built-in function,
3676    with result going to TARGET if that's convenient
3677    (and in mode MODE if that's convenient).
3678    SUBTARGET may be used as the target for computing one of EXP's operands.
3679    IGNORE is nonzero if the value is to be ignored.  */
3680
3681 rtx
3682 expand_builtin (exp, target, subtarget, mode, ignore)
3683      tree exp;
3684      rtx target;
3685      rtx subtarget;
3686      enum machine_mode mode;
3687      int ignore;
3688 {
3689   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3690   tree arglist = TREE_OPERAND (exp, 1);
3691   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
3692
3693   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
3694     return (*targetm.expand_builtin) (exp, target, subtarget, mode, ignore);
3695
3696   /* When not optimizing, generate calls to library functions for a certain
3697      set of builtins.  */
3698   if (!optimize && !CALLED_AS_BUILT_IN (fndecl))
3699     switch (fcode)
3700       {
3701       case BUILT_IN_SQRT:
3702       case BUILT_IN_SQRTF:
3703       case BUILT_IN_SQRTL:
3704       case BUILT_IN_SIN:
3705       case BUILT_IN_SINF:
3706       case BUILT_IN_SINL:
3707       case BUILT_IN_COS:
3708       case BUILT_IN_COSF:
3709       case BUILT_IN_COSL:
3710       case BUILT_IN_EXP:
3711       case BUILT_IN_EXPF:
3712       case BUILT_IN_EXPL:
3713       case BUILT_IN_MEMSET:
3714       case BUILT_IN_MEMCPY:
3715       case BUILT_IN_MEMCMP:
3716       case BUILT_IN_BCMP:
3717       case BUILT_IN_BZERO:
3718       case BUILT_IN_INDEX:
3719       case BUILT_IN_RINDEX:
3720       case BUILT_IN_STRCHR:
3721       case BUILT_IN_STRRCHR:
3722       case BUILT_IN_STRLEN:
3723       case BUILT_IN_STRCPY:
3724       case BUILT_IN_STRNCPY:
3725       case BUILT_IN_STRNCMP:
3726       case BUILT_IN_STRSTR:
3727       case BUILT_IN_STRPBRK:
3728       case BUILT_IN_STRCAT:
3729       case BUILT_IN_STRNCAT:
3730       case BUILT_IN_STRSPN:
3731       case BUILT_IN_STRCSPN:
3732       case BUILT_IN_STRCMP:
3733       case BUILT_IN_FFS:
3734       case BUILT_IN_PUTCHAR:
3735       case BUILT_IN_PUTS:
3736       case BUILT_IN_PRINTF:
3737       case BUILT_IN_FPUTC:
3738       case BUILT_IN_FPUTS:
3739       case BUILT_IN_FWRITE:
3740       case BUILT_IN_PUTCHAR_UNLOCKED:
3741       case BUILT_IN_PUTS_UNLOCKED:
3742       case BUILT_IN_PRINTF_UNLOCKED:
3743       case BUILT_IN_FPUTC_UNLOCKED:
3744       case BUILT_IN_FPUTS_UNLOCKED:
3745       case BUILT_IN_FWRITE_UNLOCKED:
3746         return expand_call (exp, target, ignore);
3747
3748       default:
3749         break;
3750       }
3751
3752   switch (fcode)
3753     {
3754     case BUILT_IN_ABS:
3755     case BUILT_IN_LABS:
3756     case BUILT_IN_LLABS:
3757     case BUILT_IN_IMAXABS:
3758     case BUILT_IN_FABS:
3759     case BUILT_IN_FABSF:
3760     case BUILT_IN_FABSL:
3761       /* build_function_call changes these into ABS_EXPR.  */
3762       abort ();
3763
3764     case BUILT_IN_CONJ:
3765     case BUILT_IN_CONJF:
3766     case BUILT_IN_CONJL:
3767     case BUILT_IN_CREAL:
3768     case BUILT_IN_CREALF:
3769     case BUILT_IN_CREALL:
3770     case BUILT_IN_CIMAG:
3771     case BUILT_IN_CIMAGF:
3772     case BUILT_IN_CIMAGL:
3773       /* expand_tree_builtin changes these into CONJ_EXPR, REALPART_EXPR
3774          and IMAGPART_EXPR.  */
3775       abort ();
3776
3777     case BUILT_IN_SIN:
3778     case BUILT_IN_SINF:
3779     case BUILT_IN_SINL:
3780     case BUILT_IN_COS:
3781     case BUILT_IN_COSF:
3782     case BUILT_IN_COSL:
3783     case BUILT_IN_EXP:
3784     case BUILT_IN_EXPF:
3785     case BUILT_IN_EXPL:
3786     case BUILT_IN_LOG:
3787     case BUILT_IN_LOGF:
3788     case BUILT_IN_LOGL:
3789       /* Treat these like sqrt only if unsafe math optimizations are allowed,
3790          because of possible accuracy problems.  */
3791       if (! flag_unsafe_math_optimizations)
3792         break;
3793     case BUILT_IN_SQRT:
3794     case BUILT_IN_SQRTF:
3795     case BUILT_IN_SQRTL:
3796       target = expand_builtin_mathfn (exp, target, subtarget);
3797       if (target)
3798         return target;
3799       break;
3800
3801     case BUILT_IN_APPLY_ARGS:
3802       return expand_builtin_apply_args ();
3803
3804       /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
3805          FUNCTION with a copy of the parameters described by
3806          ARGUMENTS, and ARGSIZE.  It returns a block of memory
3807          allocated on the stack into which is stored all the registers
3808          that might possibly be used for returning the result of a
3809          function.  ARGUMENTS is the value returned by
3810          __builtin_apply_args.  ARGSIZE is the number of bytes of
3811          arguments that must be copied.  ??? How should this value be
3812          computed?  We'll also need a safe worst case value for varargs
3813          functions.  */
3814     case BUILT_IN_APPLY:
3815       if (!validate_arglist (arglist, POINTER_TYPE,
3816                              POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
3817           && !validate_arglist (arglist, REFERENCE_TYPE,
3818                                 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3819         return const0_rtx;
3820       else
3821         {
3822           int i;
3823           tree t;
3824           rtx ops[3];
3825
3826           for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
3827             ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
3828
3829           return expand_builtin_apply (ops[0], ops[1], ops[2]);
3830         }
3831
3832       /* __builtin_return (RESULT) causes the function to return the
3833          value described by RESULT.  RESULT is address of the block of
3834          memory returned by __builtin_apply.  */
3835     case BUILT_IN_RETURN:
3836       if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
3837         expand_builtin_return (expand_expr (TREE_VALUE (arglist),
3838                                             NULL_RTX, VOIDmode, 0));
3839       return const0_rtx;
3840
3841     case BUILT_IN_SAVEREGS:
3842       return expand_builtin_saveregs ();
3843
3844     case BUILT_IN_ARGS_INFO:
3845       return expand_builtin_args_info (exp);
3846
3847       /* Return the address of the first anonymous stack arg.  */
3848     case BUILT_IN_NEXT_ARG:
3849       return expand_builtin_next_arg (arglist);
3850
3851     case BUILT_IN_CLASSIFY_TYPE:
3852       return expand_builtin_classify_type (arglist);
3853
3854     case BUILT_IN_CONSTANT_P:
3855       return expand_builtin_constant_p (exp);
3856
3857     case BUILT_IN_FRAME_ADDRESS:
3858     case BUILT_IN_RETURN_ADDRESS:
3859       return expand_builtin_frame_address (exp);
3860
3861     /* Returns the address of the area where the structure is returned.
3862        0 otherwise.  */
3863     case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
3864       if (arglist != 0
3865           || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
3866           || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM)
3867         return const0_rtx;
3868       else
3869         return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
3870
3871     case BUILT_IN_ALLOCA:
3872       target = expand_builtin_alloca (arglist, target);
3873       if (target)
3874         return target;
3875       break;
3876
3877     case BUILT_IN_FFS:
3878       target = expand_builtin_ffs (arglist, target, subtarget);
3879       if (target)
3880         return target;
3881       break;
3882
3883     case BUILT_IN_STRLEN:
3884       target = expand_builtin_strlen (exp, target);
3885       if (target)
3886         return target;
3887       break;
3888
3889     case BUILT_IN_STRCPY:
3890       target = expand_builtin_strcpy (exp, target, mode);
3891       if (target)
3892         return target;
3893       break;
3894
3895     case BUILT_IN_STRNCPY:
3896       target = expand_builtin_strncpy (arglist, target, mode);
3897       if (target)
3898         return target;
3899       break;
3900
3901     case BUILT_IN_STRCAT:
3902       target = expand_builtin_strcat (arglist, target, mode);
3903       if (target)
3904         return target;
3905       break;
3906
3907     case BUILT_IN_STRNCAT:
3908       target = expand_builtin_strncat (arglist, target, mode);
3909       if (target)
3910         return target;
3911       break;
3912
3913     case BUILT_IN_STRSPN:
3914       target = expand_builtin_strspn (arglist, target, mode);
3915       if (target)
3916         return target;
3917       break;
3918
3919     case BUILT_IN_STRCSPN:
3920       target = expand_builtin_strcspn (arglist, target, mode);
3921       if (target)
3922         return target;
3923       break;
3924
3925     case BUILT_IN_STRSTR:
3926       target = expand_builtin_strstr (arglist, target, mode);
3927       if (target)
3928         return target;
3929       break;
3930
3931     case BUILT_IN_STRPBRK:
3932       target = expand_builtin_strpbrk (arglist, target, mode);
3933       if (target)
3934         return target;
3935       break;
3936
3937     case BUILT_IN_INDEX:
3938     case BUILT_IN_STRCHR:
3939       target = expand_builtin_strchr (arglist, target, mode);
3940       if (target)
3941         return target;
3942       break;
3943
3944     case BUILT_IN_RINDEX:
3945     case BUILT_IN_STRRCHR:
3946       target = expand_builtin_strrchr (arglist, target, mode);
3947       if (target)
3948         return target;
3949       break;
3950
3951     case BUILT_IN_MEMCPY:
3952       target = expand_builtin_memcpy (arglist, target, mode);
3953       if (target)
3954         return target;
3955       break;
3956
3957     case BUILT_IN_MEMSET:
3958       target = expand_builtin_memset (exp, target, mode);
3959       if (target)
3960         return target;
3961       break;
3962
3963     case BUILT_IN_BZERO:
3964       target = expand_builtin_bzero (exp);
3965       if (target)
3966         return target;
3967       break;
3968
3969     case BUILT_IN_STRCMP:
3970       target = expand_builtin_strcmp (exp, target, mode);
3971       if (target)
3972         return target;
3973       break;
3974
3975     case BUILT_IN_STRNCMP:
3976       target = expand_builtin_strncmp (exp, target, mode);
3977       if (target)
3978         return target;
3979       break;
3980
3981     case BUILT_IN_BCMP:
3982     case BUILT_IN_MEMCMP:
3983       target = expand_builtin_memcmp (exp, arglist, target, mode);
3984       if (target)
3985         return target;
3986       break;
3987
3988     case BUILT_IN_SETJMP:
3989       target = expand_builtin_setjmp (arglist, target);
3990       if (target)
3991         return target;
3992       break;
3993
3994       /* __builtin_longjmp is passed a pointer to an array of five words.
3995          It's similar to the C library longjmp function but works with
3996          __builtin_setjmp above.  */
3997     case BUILT_IN_LONGJMP:
3998       if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3999         break;
4000       else
4001         {
4002           rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
4003                                       VOIDmode, 0);
4004           rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
4005                                    NULL_RTX, VOIDmode, 0);
4006
4007           if (value != const1_rtx)
4008             {
4009               error ("__builtin_longjmp second argument must be 1");
4010               return const0_rtx;
4011             }
4012
4013           expand_builtin_longjmp (buf_addr, value);
4014           return const0_rtx;
4015         }
4016
4017     case BUILT_IN_TRAP:
4018       expand_builtin_trap ();
4019       return const0_rtx;
4020
4021     case BUILT_IN_FPUTS:
4022       target = expand_builtin_fputs (arglist, ignore,/*unlocked=*/ 0);
4023       if (target)
4024         return target;
4025       break;
4026     case BUILT_IN_FPUTS_UNLOCKED:
4027       target = expand_builtin_fputs (arglist, ignore,/*unlocked=*/ 1);
4028       if (target)
4029         return target;
4030       break;
4031
4032       /* Various hooks for the DWARF 2 __throw routine.  */
4033     case BUILT_IN_UNWIND_INIT:
4034       expand_builtin_unwind_init ();
4035       return const0_rtx;
4036     case BUILT_IN_DWARF_CFA:
4037       return virtual_cfa_rtx;
4038 #ifdef DWARF2_UNWIND_INFO
4039     case BUILT_IN_DWARF_FP_REGNUM:
4040       return expand_builtin_dwarf_fp_regnum ();
4041     case BUILT_IN_INIT_DWARF_REG_SIZES:
4042       expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
4043       return const0_rtx;
4044 #endif
4045     case BUILT_IN_FROB_RETURN_ADDR:
4046       return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
4047     case BUILT_IN_EXTRACT_RETURN_ADDR:
4048       return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
4049     case BUILT_IN_EH_RETURN:
4050       expand_builtin_eh_return (TREE_VALUE (arglist),
4051                                 TREE_VALUE (TREE_CHAIN (arglist)));
4052       return const0_rtx;
4053 #ifdef EH_RETURN_DATA_REGNO
4054     case BUILT_IN_EH_RETURN_DATA_REGNO:
4055       return expand_builtin_eh_return_data_regno (arglist);
4056 #endif
4057     case BUILT_IN_VA_START:
4058     case BUILT_IN_STDARG_START:
4059       return expand_builtin_va_start (arglist);
4060     case BUILT_IN_VA_END:
4061       return expand_builtin_va_end (arglist);
4062     case BUILT_IN_VA_COPY:
4063       return expand_builtin_va_copy (arglist);
4064     case BUILT_IN_EXPECT:
4065       return expand_builtin_expect (arglist, target);
4066     case BUILT_IN_PREFETCH:
4067       expand_builtin_prefetch (arglist);
4068       return const0_rtx;
4069
4070
4071     default:    /* just do library call, if unknown builtin */
4072       if (!DECL_ASSEMBLER_NAME_SET_P (fndecl))
4073         error ("built-in function `%s' not currently supported",
4074                IDENTIFIER_POINTER (DECL_NAME (fndecl)));
4075     }
4076
4077   /* The switch statement above can drop through to cause the function
4078      to be called normally.  */
4079   return expand_call (exp, target, ignore);
4080 }
4081
4082 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
4083    constant.  ARGLIST is the argument list of the call.  */
4084
4085 static tree
4086 fold_builtin_constant_p (arglist)
4087      tree arglist;
4088 {
4089   if (arglist == 0)
4090     return 0;
4091
4092   arglist = TREE_VALUE (arglist);
4093
4094   /* We return 1 for a numeric type that's known to be a constant
4095      value at compile-time or for an aggregate type that's a
4096      literal constant.  */
4097   STRIP_NOPS (arglist);
4098
4099   /* If we know this is a constant, emit the constant of one.  */
4100   if (TREE_CODE_CLASS (TREE_CODE (arglist)) == 'c'
4101       || (TREE_CODE (arglist) == CONSTRUCTOR
4102           && TREE_CONSTANT (arglist))
4103       || (TREE_CODE (arglist) == ADDR_EXPR
4104           && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
4105     return integer_one_node;
4106
4107   /* If we aren't going to be running CSE or this expression
4108      has side effects, show we don't know it to be a constant.
4109      Likewise if it's a pointer or aggregate type since in those
4110      case we only want literals, since those are only optimized
4111      when generating RTL, not later.
4112      And finally, if we are compiling an initializer, not code, we
4113      need to return a definite result now; there's not going to be any
4114      more optimization done.  */
4115   if (TREE_SIDE_EFFECTS (arglist) || cse_not_expected
4116       || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
4117       || POINTER_TYPE_P (TREE_TYPE (arglist))
4118       || cfun == 0)
4119     return integer_zero_node;
4120
4121   return 0;
4122 }
4123
4124 /* Fold a call to __builtin_classify_type.  */
4125
4126 static tree
4127 fold_builtin_classify_type (arglist)
4128      tree arglist;
4129 {
4130   if (arglist == 0)
4131     return build_int_2 (no_type_class, 0);
4132
4133   return build_int_2 (type_to_class (TREE_TYPE (TREE_VALUE (arglist))), 0);
4134 }
4135
4136 /* Fold a call to __builtin_inf or __builtin_huge_val.  */
4137
4138 static tree
4139 fold_builtin_inf (type, warn)
4140      tree type;
4141      int warn;
4142 {
4143   REAL_VALUE_TYPE real;
4144
4145   if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
4146     warning ("target format does not support infinity");
4147
4148   real_inf (&real);
4149   return build_real (type, real);
4150 }
4151
4152 /* Used by constant folding to eliminate some builtin calls early.  EXP is
4153    the CALL_EXPR of a call to a builtin function.  */
4154
4155 tree
4156 fold_builtin (exp)
4157      tree exp;
4158 {
4159   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4160   tree arglist = TREE_OPERAND (exp, 1);
4161   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4162
4163   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4164     return 0;
4165
4166   switch (fcode)
4167     {
4168     case BUILT_IN_CONSTANT_P:
4169       return fold_builtin_constant_p (arglist);
4170
4171     case BUILT_IN_CLASSIFY_TYPE:
4172       return fold_builtin_classify_type (arglist);
4173
4174     case BUILT_IN_STRLEN:
4175       if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
4176         {
4177           tree len = c_strlen (TREE_VALUE (arglist));
4178           if (len != 0)
4179             return len;
4180         }
4181       break;
4182
4183     case BUILT_IN_INF:
4184     case BUILT_IN_INFF:
4185     case BUILT_IN_INFL:
4186       return fold_builtin_inf (TREE_TYPE (TREE_TYPE (fndecl)), true);
4187
4188     case BUILT_IN_HUGE_VAL:
4189     case BUILT_IN_HUGE_VALF:
4190     case BUILT_IN_HUGE_VALL:
4191       return fold_builtin_inf (TREE_TYPE (TREE_TYPE (fndecl)), false);
4192
4193     default:
4194       break;
4195     }
4196
4197   return 0;
4198 }
4199
4200 static tree
4201 build_function_call_expr (fn, arglist)
4202      tree fn, arglist;
4203 {
4204   tree call_expr;
4205
4206   call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
4207   call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
4208                      call_expr, arglist);
4209   TREE_SIDE_EFFECTS (call_expr) = 1;
4210   return fold (call_expr);
4211 }
4212
4213 /* This function validates the types of a function call argument list
4214    represented as a tree chain of parameters against a specified list
4215    of tree_codes.  If the last specifier is a 0, that represents an
4216    ellipses, otherwise the last specifier must be a VOID_TYPE.  */
4217
4218 static int
4219 validate_arglist VPARAMS ((tree arglist, ...))
4220 {
4221   enum tree_code code;
4222   int res = 0;
4223
4224   VA_OPEN (ap, arglist);
4225   VA_FIXEDARG (ap, tree, arglist);
4226
4227   do {
4228     code = va_arg (ap, enum tree_code);
4229     switch (code)
4230     {
4231     case 0:
4232       /* This signifies an ellipses, any further arguments are all ok.  */
4233       res = 1;
4234       goto end;
4235     case VOID_TYPE:
4236       /* This signifies an endlink, if no arguments remain, return
4237          true, otherwise return false.  */
4238       res = arglist == 0;
4239       goto end;
4240     default:
4241       /* If no parameters remain or the parameter's code does not
4242          match the specified code, return false.  Otherwise continue
4243          checking any remaining arguments.  */
4244       if (arglist == 0 || code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
4245         goto end;
4246       break;
4247     }
4248     arglist = TREE_CHAIN (arglist);
4249   } while (1);
4250
4251   /* We need gotos here since we can only have one VA_CLOSE in a
4252      function.  */
4253  end: ;
4254   VA_CLOSE (ap);
4255
4256   return res;
4257 }
4258
4259 /* Default version of target-specific builtin setup that does nothing.  */
4260
4261 void
4262 default_init_builtins ()
4263 {
4264 }
4265
4266 /* Default target-specific builtin expander that does nothing.  */
4267
4268 rtx
4269 default_expand_builtin (exp, target, subtarget, mode, ignore)
4270      tree exp ATTRIBUTE_UNUSED;
4271      rtx target ATTRIBUTE_UNUSED;
4272      rtx subtarget ATTRIBUTE_UNUSED;
4273      enum machine_mode mode ATTRIBUTE_UNUSED;
4274      int ignore ATTRIBUTE_UNUSED;
4275 {
4276   return NULL_RTX;
4277 }