OSDN Git Service

* tree-complex.c (expand_complex_libcall): New.
[pf3gnuchains/gcc-fork.git] / gcc / builtins.c
1 /* Expand builtin functions.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005 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 "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "real.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "tree-gimple.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "typeclass.h"
43 #include "toplev.h"
44 #include "predict.h"
45 #include "tm_p.h"
46 #include "target.h"
47 #include "langhooks.h"
48 #include "basic-block.h"
49 #include "tree-mudflap.h"
50
51 #define CALLED_AS_BUILT_IN(NODE) \
52    (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
53
54 #ifndef PAD_VARARGS_DOWN
55 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
56 #endif
57
58 /* Define the names of the builtin function types and codes.  */
59 const char *const built_in_class_names[4]
60   = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
61
62 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
63 const char * built_in_names[(int) END_BUILTINS] =
64 {
65 #include "builtins.def"
66 };
67 #undef DEF_BUILTIN
68
69 /* Setup an array of _DECL trees, make sure each element is
70    initialized to NULL_TREE.  */
71 tree built_in_decls[(int) END_BUILTINS];
72 /* Declarations used when constructing the builtin implicitly in the compiler.
73    It may be NULL_TREE when this is invalid (for instance runtime is not
74    required to implement the function call in all cases).  */
75 tree implicit_built_in_decls[(int) END_BUILTINS];
76
77 static int get_pointer_alignment (tree, unsigned int);
78 static const char *c_getstr (tree);
79 static rtx c_readstr (const char *, enum machine_mode);
80 static int target_char_cast (tree, char *);
81 static rtx get_memory_rtx (tree);
82 static tree build_string_literal (int, const char *);
83 static int apply_args_size (void);
84 static int apply_result_size (void);
85 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
86 static rtx result_vector (int, rtx);
87 #endif
88 static rtx expand_builtin_setjmp (tree, rtx);
89 static void expand_builtin_update_setjmp_buf (rtx);
90 static void expand_builtin_prefetch (tree);
91 static rtx expand_builtin_apply_args (void);
92 static rtx expand_builtin_apply_args_1 (void);
93 static rtx expand_builtin_apply (rtx, rtx, rtx);
94 static void expand_builtin_return (rtx);
95 static enum type_class type_to_class (tree);
96 static rtx expand_builtin_classify_type (tree);
97 static void expand_errno_check (tree, rtx);
98 static rtx expand_builtin_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
101 static rtx expand_builtin_args_info (tree);
102 static rtx expand_builtin_next_arg (void);
103 static rtx expand_builtin_va_start (tree);
104 static rtx expand_builtin_va_end (tree);
105 static rtx expand_builtin_va_copy (tree);
106 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
107 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
108 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
109 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
110 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
114 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
116 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_bcopy (tree, tree);
118 static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode);
119 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
120 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
122 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_bzero (tree);
126 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strstr (tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strchr (tree, rtx, enum machine_mode);
130 static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode);
131 static rtx expand_builtin_alloca (tree, rtx);
132 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
133 static rtx expand_builtin_frame_address (tree, tree);
134 static rtx expand_builtin_fputs (tree, rtx, bool);
135 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
136 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
137 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
138 static tree stabilize_va_list (tree, int);
139 static rtx expand_builtin_expect (tree, rtx);
140 static tree fold_builtin_constant_p (tree);
141 static tree fold_builtin_classify_type (tree);
142 static tree fold_builtin_strlen (tree);
143 static tree fold_builtin_inf (tree, int);
144 static tree fold_builtin_nan (tree, tree, int);
145 static int validate_arglist (tree, ...);
146 static bool integer_valued_real_p (tree);
147 static tree fold_trunc_transparent_mathfn (tree);
148 static bool readonly_data_expr (tree);
149 static rtx expand_builtin_fabs (tree, rtx, rtx);
150 static rtx expand_builtin_signbit (tree, rtx);
151 static tree fold_builtin_cabs (tree, tree);
152 static tree fold_builtin_sqrt (tree, tree);
153 static tree fold_builtin_cbrt (tree, tree);
154 static tree fold_builtin_pow (tree, tree, tree);
155 static tree fold_builtin_powi (tree, tree, tree);
156 static tree fold_builtin_sin (tree);
157 static tree fold_builtin_cos (tree, tree, tree);
158 static tree fold_builtin_tan (tree);
159 static tree fold_builtin_atan (tree, tree);
160 static tree fold_builtin_trunc (tree);
161 static tree fold_builtin_floor (tree);
162 static tree fold_builtin_ceil (tree);
163 static tree fold_builtin_round (tree);
164 static tree fold_builtin_bitop (tree);
165 static tree fold_builtin_memcpy (tree);
166 static tree fold_builtin_mempcpy (tree, tree, int);
167 static tree fold_builtin_memmove (tree, tree);
168 static tree fold_builtin_strchr (tree);
169 static tree fold_builtin_memcmp (tree);
170 static tree fold_builtin_strcmp (tree);
171 static tree fold_builtin_strncmp (tree);
172 static tree fold_builtin_signbit (tree);
173 static tree fold_builtin_copysign (tree, tree, tree);
174 static tree fold_builtin_isascii (tree);
175 static tree fold_builtin_toascii (tree);
176 static tree fold_builtin_isdigit (tree);
177 static tree fold_builtin_fabs (tree, tree);
178 static tree fold_builtin_abs (tree, tree);
179 static tree fold_builtin_unordered_cmp (tree, enum tree_code, enum tree_code);
180 static tree fold_builtin_1 (tree, bool);
181
182 static tree fold_builtin_strpbrk (tree);
183 static tree fold_builtin_strstr (tree);
184 static tree fold_builtin_strrchr (tree);
185 static tree fold_builtin_strcat (tree);
186 static tree fold_builtin_strncat (tree);
187 static tree fold_builtin_strspn (tree);
188 static tree fold_builtin_strcspn (tree);
189 static tree fold_builtin_sprintf (tree, int);
190
191
192 /* Return the alignment in bits of EXP, a pointer valued expression.
193    But don't return more than MAX_ALIGN no matter what.
194    The alignment returned is, by default, the alignment of the thing that
195    EXP points to.  If it is not a POINTER_TYPE, 0 is returned.
196
197    Otherwise, look at the expression to see if we can do better, i.e., if the
198    expression is actually pointing at an object whose alignment is tighter.  */
199
200 static int
201 get_pointer_alignment (tree exp, unsigned int max_align)
202 {
203   unsigned int align, inner;
204
205   if (! POINTER_TYPE_P (TREE_TYPE (exp)))
206     return 0;
207
208   align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
209   align = MIN (align, max_align);
210
211   while (1)
212     {
213       switch (TREE_CODE (exp))
214         {
215         case NOP_EXPR:
216         case CONVERT_EXPR:
217         case NON_LVALUE_EXPR:
218           exp = TREE_OPERAND (exp, 0);
219           if (! POINTER_TYPE_P (TREE_TYPE (exp)))
220             return align;
221
222           inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
223           align = MIN (inner, max_align);
224           break;
225
226         case PLUS_EXPR:
227           /* If sum of pointer + int, restrict our maximum alignment to that
228              imposed by the integer.  If not, we can't do any better than
229              ALIGN.  */
230           if (! host_integerp (TREE_OPERAND (exp, 1), 1))
231             return align;
232
233           while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
234                   & (max_align / BITS_PER_UNIT - 1))
235                  != 0)
236             max_align >>= 1;
237
238           exp = TREE_OPERAND (exp, 0);
239           break;
240
241         case ADDR_EXPR:
242           /* See what we are pointing at and look at its alignment.  */
243           exp = TREE_OPERAND (exp, 0);
244           if (TREE_CODE (exp) == FUNCTION_DECL)
245             align = FUNCTION_BOUNDARY;
246           else if (DECL_P (exp))
247             align = DECL_ALIGN (exp);
248 #ifdef CONSTANT_ALIGNMENT
249           else if (CONSTANT_CLASS_P (exp))
250             align = CONSTANT_ALIGNMENT (exp, align);
251 #endif
252           return MIN (align, max_align);
253
254         default:
255           return align;
256         }
257     }
258 }
259
260 /* Compute the length of a C string.  TREE_STRING_LENGTH is not the right
261    way, because it could contain a zero byte in the middle.
262    TREE_STRING_LENGTH is the size of the character array, not the string.
263
264    ONLY_VALUE should be nonzero if the result is not going to be emitted
265    into the instruction stream and zero if it is going to be expanded.
266    E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
267    is returned, otherwise NULL, since
268    len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
269    evaluate the side-effects.
270
271    The value returned is of type `ssizetype'.
272
273    Unfortunately, string_constant can't access the values of const char
274    arrays with initializers, so neither can we do so here.  */
275
276 tree
277 c_strlen (tree src, int only_value)
278 {
279   tree offset_node;
280   HOST_WIDE_INT offset;
281   int max;
282   const char *ptr;
283
284   STRIP_NOPS (src);
285   if (TREE_CODE (src) == COND_EXPR
286       && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
287     {
288       tree len1, len2;
289
290       len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
291       len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
292       if (tree_int_cst_equal (len1, len2))
293         return len1;
294     }
295
296   if (TREE_CODE (src) == COMPOUND_EXPR
297       && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
298     return c_strlen (TREE_OPERAND (src, 1), only_value);
299
300   src = string_constant (src, &offset_node);
301   if (src == 0)
302     return 0;
303
304   max = TREE_STRING_LENGTH (src) - 1;
305   ptr = TREE_STRING_POINTER (src);
306
307   if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
308     {
309       /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
310          compute the offset to the following null if we don't know where to
311          start searching for it.  */
312       int i;
313
314       for (i = 0; i < max; i++)
315         if (ptr[i] == 0)
316           return 0;
317
318       /* We don't know the starting offset, but we do know that the string
319          has no internal zero bytes.  We can assume that the offset falls
320          within the bounds of the string; otherwise, the programmer deserves
321          what he gets.  Subtract the offset from the length of the string,
322          and return that.  This would perhaps not be valid if we were dealing
323          with named arrays in addition to literal string constants.  */
324
325       return size_diffop (size_int (max), offset_node);
326     }
327
328   /* We have a known offset into the string.  Start searching there for
329      a null character if we can represent it as a single HOST_WIDE_INT.  */
330   if (offset_node == 0)
331     offset = 0;
332   else if (! host_integerp (offset_node, 0))
333     offset = -1;
334   else
335     offset = tree_low_cst (offset_node, 0);
336
337   /* If the offset is known to be out of bounds, warn, and call strlen at
338      runtime.  */
339   if (offset < 0 || offset > max)
340     {
341       warning ("offset outside bounds of constant string");
342       return 0;
343     }
344
345   /* Use strlen to search for the first zero byte.  Since any strings
346      constructed with build_string will have nulls appended, we win even
347      if we get handed something like (char[4])"abcd".
348
349      Since OFFSET is our starting index into the string, no further
350      calculation is needed.  */
351   return ssize_int (strlen (ptr + offset));
352 }
353
354 /* Return a char pointer for a C string if it is a string constant
355    or sum of string constant and integer constant.  */
356
357 static const char *
358 c_getstr (tree src)
359 {
360   tree offset_node;
361
362   src = string_constant (src, &offset_node);
363   if (src == 0)
364     return 0;
365
366   if (offset_node == 0)
367     return TREE_STRING_POINTER (src);
368   else if (!host_integerp (offset_node, 1)
369            || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
370     return 0;
371
372   return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
373 }
374
375 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
376    GET_MODE_BITSIZE (MODE) bits from string constant STR.  */
377
378 static rtx
379 c_readstr (const char *str, enum machine_mode mode)
380 {
381   HOST_WIDE_INT c[2];
382   HOST_WIDE_INT ch;
383   unsigned int i, j;
384
385   gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
386
387   c[0] = 0;
388   c[1] = 0;
389   ch = 1;
390   for (i = 0; i < GET_MODE_SIZE (mode); i++)
391     {
392       j = i;
393       if (WORDS_BIG_ENDIAN)
394         j = GET_MODE_SIZE (mode) - i - 1;
395       if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
396           && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
397         j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
398       j *= BITS_PER_UNIT;
399       gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
400
401       if (ch)
402         ch = (unsigned char) str[i];
403       c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
404     }
405   return immed_double_const (c[0], c[1], mode);
406 }
407
408 /* Cast a target constant CST to target CHAR and if that value fits into
409    host char type, return zero and put that value into variable pointed by
410    P.  */
411
412 static int
413 target_char_cast (tree cst, char *p)
414 {
415   unsigned HOST_WIDE_INT val, hostval;
416
417   if (!host_integerp (cst, 1)
418       || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
419     return 1;
420
421   val = tree_low_cst (cst, 1);
422   if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
423     val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
424
425   hostval = val;
426   if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
427     hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
428
429   if (val != hostval)
430     return 1;
431
432   *p = hostval;
433   return 0;
434 }
435
436 /* Similar to save_expr, but assumes that arbitrary code is not executed
437    in between the multiple evaluations.  In particular, we assume that a
438    non-addressable local variable will not be modified.  */
439
440 static tree
441 builtin_save_expr (tree exp)
442 {
443   if (TREE_ADDRESSABLE (exp) == 0
444       && (TREE_CODE (exp) == PARM_DECL
445           || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
446     return exp;
447
448   return save_expr (exp);
449 }
450
451 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
452    times to get the address of either a higher stack frame, or a return
453    address located within it (depending on FNDECL_CODE).  */
454
455 static rtx
456 expand_builtin_return_addr (enum built_in_function fndecl_code, int count,
457                             rtx tem)
458 {
459   int i;
460
461   /* Some machines need special handling before we can access
462      arbitrary frames.  For example, on the sparc, we must first flush
463      all register windows to the stack.  */
464 #ifdef SETUP_FRAME_ADDRESSES
465   if (count > 0)
466     SETUP_FRAME_ADDRESSES ();
467 #endif
468
469   /* On the sparc, the return address is not in the frame, it is in a
470      register.  There is no way to access it off of the current frame
471      pointer, but it can be accessed off the previous frame pointer by
472      reading the value from the register window save area.  */
473 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
474   if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
475     count--;
476 #endif
477
478   /* Scan back COUNT frames to the specified frame.  */
479   for (i = 0; i < count; i++)
480     {
481       /* Assume the dynamic chain pointer is in the word that the
482          frame address points to, unless otherwise specified.  */
483 #ifdef DYNAMIC_CHAIN_ADDRESS
484       tem = DYNAMIC_CHAIN_ADDRESS (tem);
485 #endif
486       tem = memory_address (Pmode, tem);
487       tem = gen_rtx_MEM (Pmode, tem);
488       set_mem_alias_set (tem, get_frame_alias_set ());
489       tem = copy_to_reg (tem);
490     }
491
492   /* For __builtin_frame_address, return what we've got.  */
493   if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
494     return tem;
495
496   /* For __builtin_return_address, Get the return address from that
497      frame.  */
498 #ifdef RETURN_ADDR_RTX
499   tem = RETURN_ADDR_RTX (count, tem);
500 #else
501   tem = memory_address (Pmode,
502                         plus_constant (tem, GET_MODE_SIZE (Pmode)));
503   tem = gen_rtx_MEM (Pmode, tem);
504   set_mem_alias_set (tem, get_frame_alias_set ());
505 #endif
506   return tem;
507 }
508
509 /* Alias set used for setjmp buffer.  */
510 static HOST_WIDE_INT setjmp_alias_set = -1;
511
512 /* Construct the leading half of a __builtin_setjmp call.  Control will
513    return to RECEIVER_LABEL.  This is used directly by sjlj exception
514    handling code.  */
515
516 void
517 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
518 {
519   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
520   rtx stack_save;
521   rtx mem;
522
523   if (setjmp_alias_set == -1)
524     setjmp_alias_set = new_alias_set ();
525
526   buf_addr = convert_memory_address (Pmode, buf_addr);
527
528   buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
529
530   /* We store the frame pointer and the address of receiver_label in
531      the buffer and use the rest of it for the stack save area, which
532      is machine-dependent.  */
533
534   mem = gen_rtx_MEM (Pmode, buf_addr);
535   set_mem_alias_set (mem, setjmp_alias_set);
536   emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
537
538   mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
539   set_mem_alias_set (mem, setjmp_alias_set);
540
541   emit_move_insn (validize_mem (mem),
542                   force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
543
544   stack_save = gen_rtx_MEM (sa_mode,
545                             plus_constant (buf_addr,
546                                            2 * GET_MODE_SIZE (Pmode)));
547   set_mem_alias_set (stack_save, setjmp_alias_set);
548   emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
549
550   /* If there is further processing to do, do it.  */
551 #ifdef HAVE_builtin_setjmp_setup
552   if (HAVE_builtin_setjmp_setup)
553     emit_insn (gen_builtin_setjmp_setup (buf_addr));
554 #endif
555
556   /* Tell optimize_save_area_alloca that extra work is going to
557      need to go on during alloca.  */
558   current_function_calls_setjmp = 1;
559
560   /* Set this so all the registers get saved in our frame; we need to be
561      able to copy the saved values for any registers from frames we unwind.  */
562   current_function_has_nonlocal_label = 1;
563 }
564
565 /* Construct the trailing part of a __builtin_setjmp call.
566    This is used directly by sjlj exception handling code.  */
567
568 void
569 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
570 {
571   /* Clobber the FP when we get here, so we have to make sure it's
572      marked as used by this function.  */
573   emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
574
575   /* Mark the static chain as clobbered here so life information
576      doesn't get messed up for it.  */
577   emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
578
579   /* Now put in the code to restore the frame pointer, and argument
580      pointer, if needed.  */
581 #ifdef HAVE_nonlocal_goto
582   if (! HAVE_nonlocal_goto)
583 #endif
584     emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
585
586 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
587   if (fixed_regs[ARG_POINTER_REGNUM])
588     {
589 #ifdef ELIMINABLE_REGS
590       size_t i;
591       static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
592
593       for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
594         if (elim_regs[i].from == ARG_POINTER_REGNUM
595             && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
596           break;
597
598       if (i == ARRAY_SIZE (elim_regs))
599 #endif
600         {
601           /* Now restore our arg pointer from the address at which it
602              was saved in our stack frame.  */
603           emit_move_insn (virtual_incoming_args_rtx,
604                           copy_to_reg (get_arg_pointer_save_area (cfun)));
605         }
606     }
607 #endif
608
609 #ifdef HAVE_builtin_setjmp_receiver
610   if (HAVE_builtin_setjmp_receiver)
611     emit_insn (gen_builtin_setjmp_receiver (receiver_label));
612   else
613 #endif
614 #ifdef HAVE_nonlocal_goto_receiver
615     if (HAVE_nonlocal_goto_receiver)
616       emit_insn (gen_nonlocal_goto_receiver ());
617     else
618 #endif
619       { /* Nothing */ }
620
621   /* @@@ This is a kludge.  Not all machine descriptions define a blockage
622      insn, but we must not allow the code we just generated to be reordered
623      by scheduling.  Specifically, the update of the frame pointer must
624      happen immediately, not later.  So emit an ASM_INPUT to act as blockage
625      insn.  */
626   emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
627 }
628
629 /* __builtin_setjmp is passed a pointer to an array of five words (not
630    all will be used on all machines).  It operates similarly to the C
631    library function of the same name, but is more efficient.  Much of
632    the code below (and for longjmp) is copied from the handling of
633    non-local gotos.
634
635    NOTE: This is intended for use by GNAT and the exception handling
636    scheme in the compiler and will only work in the method used by
637    them.  */
638
639 static rtx
640 expand_builtin_setjmp (tree arglist, rtx target)
641 {
642   rtx buf_addr, next_lab, cont_lab;
643
644   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
645     return NULL_RTX;
646
647   if (target == 0 || !REG_P (target)
648       || REGNO (target) < FIRST_PSEUDO_REGISTER)
649     target = gen_reg_rtx (TYPE_MODE (integer_type_node));
650
651   buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
652
653   next_lab = gen_label_rtx ();
654   cont_lab = gen_label_rtx ();
655
656   expand_builtin_setjmp_setup (buf_addr, next_lab);
657
658   /* Set TARGET to zero and branch to the continue label.  Use emit_jump to
659      ensure that pending stack adjustments are flushed.  */
660   emit_move_insn (target, const0_rtx);
661   emit_jump (cont_lab);
662
663   emit_label (next_lab);
664
665   expand_builtin_setjmp_receiver (next_lab);
666
667   /* Set TARGET to one.  */
668   emit_move_insn (target, const1_rtx);
669   emit_label (cont_lab);
670
671   /* Tell flow about the strange goings on.  Putting `next_lab' on
672      `nonlocal_goto_handler_labels' to indicates that function
673      calls may traverse the arc back to this label.  */
674
675   current_function_has_nonlocal_label = 1;
676   nonlocal_goto_handler_labels
677     = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
678
679   return target;
680 }
681
682 /* __builtin_longjmp is passed a pointer to an array of five words (not
683    all will be used on all machines).  It operates similarly to the C
684    library function of the same name, but is more efficient.  Much of
685    the code below is copied from the handling of non-local gotos.
686
687    NOTE: This is intended for use by GNAT and the exception handling
688    scheme in the compiler and will only work in the method used by
689    them.  */
690
691 static void
692 expand_builtin_longjmp (rtx buf_addr, rtx value)
693 {
694   rtx fp, lab, stack, insn, last;
695   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
696
697   if (setjmp_alias_set == -1)
698     setjmp_alias_set = new_alias_set ();
699
700   buf_addr = convert_memory_address (Pmode, buf_addr);
701
702   buf_addr = force_reg (Pmode, buf_addr);
703
704   /* We used to store value in static_chain_rtx, but that fails if pointers
705      are smaller than integers.  We instead require that the user must pass
706      a second argument of 1, because that is what builtin_setjmp will
707      return.  This also makes EH slightly more efficient, since we are no
708      longer copying around a value that we don't care about.  */
709   gcc_assert (value == const1_rtx);
710
711   last = get_last_insn ();
712 #ifdef HAVE_builtin_longjmp
713   if (HAVE_builtin_longjmp)
714     emit_insn (gen_builtin_longjmp (buf_addr));
715   else
716 #endif
717     {
718       fp = gen_rtx_MEM (Pmode, buf_addr);
719       lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
720                                                GET_MODE_SIZE (Pmode)));
721
722       stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
723                                                    2 * GET_MODE_SIZE (Pmode)));
724       set_mem_alias_set (fp, setjmp_alias_set);
725       set_mem_alias_set (lab, setjmp_alias_set);
726       set_mem_alias_set (stack, setjmp_alias_set);
727
728       /* Pick up FP, label, and SP from the block and jump.  This code is
729          from expand_goto in stmt.c; see there for detailed comments.  */
730 #if HAVE_nonlocal_goto
731       if (HAVE_nonlocal_goto)
732         /* We have to pass a value to the nonlocal_goto pattern that will
733            get copied into the static_chain pointer, but it does not matter
734            what that value is, because builtin_setjmp does not use it.  */
735         emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
736       else
737 #endif
738         {
739           lab = copy_to_reg (lab);
740
741           emit_insn (gen_rtx_CLOBBER (VOIDmode,
742                                       gen_rtx_MEM (BLKmode,
743                                                    gen_rtx_SCRATCH (VOIDmode))));
744           emit_insn (gen_rtx_CLOBBER (VOIDmode,
745                                       gen_rtx_MEM (BLKmode,
746                                                    hard_frame_pointer_rtx)));
747
748           emit_move_insn (hard_frame_pointer_rtx, fp);
749           emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
750
751           emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
752           emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
753           emit_indirect_jump (lab);
754         }
755     }
756
757   /* Search backwards and mark the jump insn as a non-local goto.
758      Note that this precludes the use of __builtin_longjmp to a
759      __builtin_setjmp target in the same function.  However, we've
760      already cautioned the user that these functions are for
761      internal exception handling use only.  */
762   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
763     {
764       gcc_assert (insn != last);
765
766       if (JUMP_P (insn))
767         {
768           REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
769                                               REG_NOTES (insn));
770           break;
771         }
772       else if (CALL_P (insn))
773         break;
774     }
775 }
776
777 /* Expand a call to __builtin_nonlocal_goto.  We're passed the target label
778    and the address of the save area.  */
779
780 static rtx
781 expand_builtin_nonlocal_goto (tree arglist)
782 {
783   tree t_label, t_save_area;
784   rtx r_label, r_save_area, r_fp, r_sp, insn;
785
786   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
787     return NULL_RTX;
788
789   t_label = TREE_VALUE (arglist);
790   arglist = TREE_CHAIN (arglist);
791   t_save_area = TREE_VALUE (arglist);
792
793   r_label = expand_expr (t_label, NULL_RTX, VOIDmode, 0);
794   r_label = convert_memory_address (Pmode, r_label);
795   r_save_area = expand_expr (t_save_area, NULL_RTX, VOIDmode, 0);
796   r_save_area = convert_memory_address (Pmode, r_save_area);
797   r_fp = gen_rtx_MEM (Pmode, r_save_area);
798   r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
799                       plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
800
801   current_function_has_nonlocal_goto = 1;
802
803 #if HAVE_nonlocal_goto
804   /* ??? We no longer need to pass the static chain value, afaik.  */
805   if (HAVE_nonlocal_goto)
806     emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
807   else
808 #endif
809     {
810       r_label = copy_to_reg (r_label);
811
812       emit_insn (gen_rtx_CLOBBER (VOIDmode,
813                                   gen_rtx_MEM (BLKmode,
814                                                gen_rtx_SCRATCH (VOIDmode))));
815
816       emit_insn (gen_rtx_CLOBBER (VOIDmode,
817                                   gen_rtx_MEM (BLKmode,
818                                                hard_frame_pointer_rtx)));
819
820       /* Restore frame pointer for containing function.
821          This sets the actual hard register used for the frame pointer
822          to the location of the function's incoming static chain info.
823          The non-local goto handler will then adjust it to contain the
824          proper value and reload the argument pointer, if needed.  */
825       emit_move_insn (hard_frame_pointer_rtx, r_fp);
826       emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
827
828       /* USE of hard_frame_pointer_rtx added for consistency;
829          not clear if really needed.  */
830       emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
831       emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
832       emit_indirect_jump (r_label);
833     }
834
835   /* Search backwards to the jump insn and mark it as a
836      non-local goto.  */
837   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
838     {
839       if (JUMP_P (insn))
840         {
841           REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
842                                               const0_rtx, REG_NOTES (insn));
843           break;
844         }
845       else if (CALL_P (insn))
846         break;
847     }
848
849   return const0_rtx;
850 }
851
852 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
853    (not all will be used on all machines) that was passed to __builtin_setjmp.
854    It updates the stack pointer in that block to correspond to the current
855    stack pointer.  */
856
857 static void
858 expand_builtin_update_setjmp_buf (rtx buf_addr)
859 {
860   enum machine_mode sa_mode = Pmode;
861   rtx stack_save;
862
863
864 #ifdef HAVE_save_stack_nonlocal
865   if (HAVE_save_stack_nonlocal)
866     sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
867 #endif
868 #ifdef STACK_SAVEAREA_MODE
869   sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
870 #endif
871
872   stack_save
873     = gen_rtx_MEM (sa_mode,
874                    memory_address
875                    (sa_mode,
876                     plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
877
878 #ifdef HAVE_setjmp
879   if (HAVE_setjmp)
880     emit_insn (gen_setjmp ());
881 #endif
882
883   emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
884 }
885
886 /* Expand a call to __builtin_prefetch.  For a target that does not support
887    data prefetch, evaluate the memory address argument in case it has side
888    effects.  */
889
890 static void
891 expand_builtin_prefetch (tree arglist)
892 {
893   tree arg0, arg1, arg2;
894   rtx op0, op1, op2;
895
896   if (!validate_arglist (arglist, POINTER_TYPE, 0))
897     return;
898
899   arg0 = TREE_VALUE (arglist);
900   /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
901      zero (read) and argument 2 (locality) defaults to 3 (high degree of
902      locality).  */
903   if (TREE_CHAIN (arglist))
904     {
905       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
906       if (TREE_CHAIN (TREE_CHAIN (arglist)))
907         arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
908       else
909         arg2 = build_int_cst (NULL_TREE, 3);
910     }
911   else
912     {
913       arg1 = integer_zero_node;
914       arg2 = build_int_cst (NULL_TREE, 3);
915     }
916
917   /* Argument 0 is an address.  */
918   op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
919
920   /* Argument 1 (read/write flag) must be a compile-time constant int.  */
921   if (TREE_CODE (arg1) != INTEGER_CST)
922     {
923       error ("second argument to %<__builtin_prefetch%> must be a constant");
924       arg1 = integer_zero_node;
925     }
926   op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
927   /* Argument 1 must be either zero or one.  */
928   if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
929     {
930       warning ("invalid second argument to %<__builtin_prefetch%>;"
931                " using zero");
932       op1 = const0_rtx;
933     }
934
935   /* Argument 2 (locality) must be a compile-time constant int.  */
936   if (TREE_CODE (arg2) != INTEGER_CST)
937     {
938       error ("third argument to %<__builtin_prefetch%> must be a constant");
939       arg2 = integer_zero_node;
940     }
941   op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
942   /* Argument 2 must be 0, 1, 2, or 3.  */
943   if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
944     {
945       warning ("invalid third argument to %<__builtin_prefetch%>; using zero");
946       op2 = const0_rtx;
947     }
948
949 #ifdef HAVE_prefetch
950   if (HAVE_prefetch)
951     {
952       if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
953              (op0,
954               insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
955           || (GET_MODE (op0) != Pmode))
956         {
957           op0 = convert_memory_address (Pmode, op0);
958           op0 = force_reg (Pmode, op0);
959         }
960       emit_insn (gen_prefetch (op0, op1, op2));
961     }
962 #endif
963
964   /* Don't do anything with direct references to volatile memory, but
965      generate code to handle other side effects.  */
966   if (!MEM_P (op0) && side_effects_p (op0))
967     emit_insn (op0);
968 }
969
970 /* Get a MEM rtx for expression EXP which is the address of an operand
971    to be used to be used in a string instruction (cmpstrsi, movmemsi, ..).  */
972
973 static rtx
974 get_memory_rtx (tree exp)
975 {
976   rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM);
977   rtx mem;
978
979   addr = convert_memory_address (Pmode, addr);
980
981   mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
982
983   /* Get an expression we can use to find the attributes to assign to MEM.
984      If it is an ADDR_EXPR, use the operand.  Otherwise, dereference it if
985      we can.  First remove any nops.  */
986   while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
987           || TREE_CODE (exp) == NON_LVALUE_EXPR)
988          && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
989     exp = TREE_OPERAND (exp, 0);
990
991   if (TREE_CODE (exp) == ADDR_EXPR)
992     exp = TREE_OPERAND (exp, 0);
993   else if (POINTER_TYPE_P (TREE_TYPE (exp)))
994     exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
995   else
996     exp = NULL;
997
998   /* Honor attributes derived from exp, except for the alias set
999      (as builtin stringops may alias with anything) and the size
1000      (as stringops may access multiple array elements).  */
1001   if (exp)
1002     {
1003       set_mem_attributes (mem, exp, 0);
1004       set_mem_alias_set (mem, 0);
1005       set_mem_size (mem, NULL_RTX);
1006     }
1007
1008   return mem;
1009 }
1010 \f
1011 /* Built-in functions to perform an untyped call and return.  */
1012
1013 /* For each register that may be used for calling a function, this
1014    gives a mode used to copy the register's value.  VOIDmode indicates
1015    the register is not used for calling a function.  If the machine
1016    has register windows, this gives only the outbound registers.
1017    INCOMING_REGNO gives the corresponding inbound register.  */
1018 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1019
1020 /* For each register that may be used for returning values, this gives
1021    a mode used to copy the register's value.  VOIDmode indicates the
1022    register is not used for returning values.  If the machine has
1023    register windows, this gives only the outbound registers.
1024    INCOMING_REGNO gives the corresponding inbound register.  */
1025 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1026
1027 /* For each register that may be used for calling a function, this
1028    gives the offset of that register into the block returned by
1029    __builtin_apply_args.  0 indicates that the register is not
1030    used for calling a function.  */
1031 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1032
1033 /* Return the size required for the block returned by __builtin_apply_args,
1034    and initialize apply_args_mode.  */
1035
1036 static int
1037 apply_args_size (void)
1038 {
1039   static int size = -1;
1040   int align;
1041   unsigned int regno;
1042   enum machine_mode mode;
1043
1044   /* The values computed by this function never change.  */
1045   if (size < 0)
1046     {
1047       /* The first value is the incoming arg-pointer.  */
1048       size = GET_MODE_SIZE (Pmode);
1049
1050       /* The second value is the structure value address unless this is
1051          passed as an "invisible" first argument.  */
1052       if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1053         size += GET_MODE_SIZE (Pmode);
1054
1055       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1056         if (FUNCTION_ARG_REGNO_P (regno))
1057           {
1058             mode = reg_raw_mode[regno];
1059
1060             gcc_assert (mode != VOIDmode);
1061
1062             align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1063             if (size % align != 0)
1064               size = CEIL (size, align) * align;
1065             apply_args_reg_offset[regno] = size;
1066             size += GET_MODE_SIZE (mode);
1067             apply_args_mode[regno] = mode;
1068           }
1069         else
1070           {
1071             apply_args_mode[regno] = VOIDmode;
1072             apply_args_reg_offset[regno] = 0;
1073           }
1074     }
1075   return size;
1076 }
1077
1078 /* Return the size required for the block returned by __builtin_apply,
1079    and initialize apply_result_mode.  */
1080
1081 static int
1082 apply_result_size (void)
1083 {
1084   static int size = -1;
1085   int align, regno;
1086   enum machine_mode mode;
1087
1088   /* The values computed by this function never change.  */
1089   if (size < 0)
1090     {
1091       size = 0;
1092
1093       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1094         if (FUNCTION_VALUE_REGNO_P (regno))
1095           {
1096             mode = reg_raw_mode[regno];
1097
1098             gcc_assert (mode != VOIDmode);
1099
1100             align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1101             if (size % align != 0)
1102               size = CEIL (size, align) * align;
1103             size += GET_MODE_SIZE (mode);
1104             apply_result_mode[regno] = mode;
1105           }
1106         else
1107           apply_result_mode[regno] = VOIDmode;
1108
1109       /* Allow targets that use untyped_call and untyped_return to override
1110          the size so that machine-specific information can be stored here.  */
1111 #ifdef APPLY_RESULT_SIZE
1112       size = APPLY_RESULT_SIZE;
1113 #endif
1114     }
1115   return size;
1116 }
1117
1118 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1119 /* Create a vector describing the result block RESULT.  If SAVEP is true,
1120    the result block is used to save the values; otherwise it is used to
1121    restore the values.  */
1122
1123 static rtx
1124 result_vector (int savep, rtx result)
1125 {
1126   int regno, size, align, nelts;
1127   enum machine_mode mode;
1128   rtx reg, mem;
1129   rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1130
1131   size = nelts = 0;
1132   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1133     if ((mode = apply_result_mode[regno]) != VOIDmode)
1134       {
1135         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1136         if (size % align != 0)
1137           size = CEIL (size, align) * align;
1138         reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1139         mem = adjust_address (result, mode, size);
1140         savevec[nelts++] = (savep
1141                             ? gen_rtx_SET (VOIDmode, mem, reg)
1142                             : gen_rtx_SET (VOIDmode, reg, mem));
1143         size += GET_MODE_SIZE (mode);
1144       }
1145   return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1146 }
1147 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1148
1149 /* Save the state required to perform an untyped call with the same
1150    arguments as were passed to the current function.  */
1151
1152 static rtx
1153 expand_builtin_apply_args_1 (void)
1154 {
1155   rtx registers, tem;
1156   int size, align, regno;
1157   enum machine_mode mode;
1158   rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1159
1160   /* Create a block where the arg-pointer, structure value address,
1161      and argument registers can be saved.  */
1162   registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1163
1164   /* Walk past the arg-pointer and structure value address.  */
1165   size = GET_MODE_SIZE (Pmode);
1166   if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1167     size += GET_MODE_SIZE (Pmode);
1168
1169   /* Save each register used in calling a function to the block.  */
1170   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1171     if ((mode = apply_args_mode[regno]) != VOIDmode)
1172       {
1173         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1174         if (size % align != 0)
1175           size = CEIL (size, align) * align;
1176
1177         tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1178
1179         emit_move_insn (adjust_address (registers, mode, size), tem);
1180         size += GET_MODE_SIZE (mode);
1181       }
1182
1183   /* Save the arg pointer to the block.  */
1184   tem = copy_to_reg (virtual_incoming_args_rtx);
1185 #ifdef STACK_GROWS_DOWNWARD
1186   /* We need the pointer as the caller actually passed them to us, not
1187      as we might have pretended they were passed.  Make sure it's a valid
1188      operand, as emit_move_insn isn't expected to handle a PLUS.  */
1189   tem
1190     = force_operand (plus_constant (tem, current_function_pretend_args_size),
1191                      NULL_RTX);
1192 #endif
1193   emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1194
1195   size = GET_MODE_SIZE (Pmode);
1196
1197   /* Save the structure value address unless this is passed as an
1198      "invisible" first argument.  */
1199   if (struct_incoming_value)
1200     {
1201       emit_move_insn (adjust_address (registers, Pmode, size),
1202                       copy_to_reg (struct_incoming_value));
1203       size += GET_MODE_SIZE (Pmode);
1204     }
1205
1206   /* Return the address of the block.  */
1207   return copy_addr_to_reg (XEXP (registers, 0));
1208 }
1209
1210 /* __builtin_apply_args returns block of memory allocated on
1211    the stack into which is stored the arg pointer, structure
1212    value address, static chain, and all the registers that might
1213    possibly be used in performing a function call.  The code is
1214    moved to the start of the function so the incoming values are
1215    saved.  */
1216
1217 static rtx
1218 expand_builtin_apply_args (void)
1219 {
1220   /* Don't do __builtin_apply_args more than once in a function.
1221      Save the result of the first call and reuse it.  */
1222   if (apply_args_value != 0)
1223     return apply_args_value;
1224   {
1225     /* When this function is called, it means that registers must be
1226        saved on entry to this function.  So we migrate the
1227        call to the first insn of this function.  */
1228     rtx temp;
1229     rtx seq;
1230
1231     start_sequence ();
1232     temp = expand_builtin_apply_args_1 ();
1233     seq = get_insns ();
1234     end_sequence ();
1235
1236     apply_args_value = temp;
1237
1238     /* Put the insns after the NOTE that starts the function.
1239        If this is inside a start_sequence, make the outer-level insn
1240        chain current, so the code is placed at the start of the
1241        function.  */
1242     push_topmost_sequence ();
1243     emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1244     pop_topmost_sequence ();
1245     return temp;
1246   }
1247 }
1248
1249 /* Perform an untyped call and save the state required to perform an
1250    untyped return of whatever value was returned by the given function.  */
1251
1252 static rtx
1253 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1254 {
1255   int size, align, regno;
1256   enum machine_mode mode;
1257   rtx incoming_args, result, reg, dest, src, call_insn;
1258   rtx old_stack_level = 0;
1259   rtx call_fusage = 0;
1260   rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1261
1262   arguments = convert_memory_address (Pmode, arguments);
1263
1264   /* Create a block where the return registers can be saved.  */
1265   result = assign_stack_local (BLKmode, apply_result_size (), -1);
1266
1267   /* Fetch the arg pointer from the ARGUMENTS block.  */
1268   incoming_args = gen_reg_rtx (Pmode);
1269   emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1270 #ifndef STACK_GROWS_DOWNWARD
1271   incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1272                                        incoming_args, 0, OPTAB_LIB_WIDEN);
1273 #endif
1274
1275   /* Push a new argument block and copy the arguments.  Do not allow
1276      the (potential) memcpy call below to interfere with our stack
1277      manipulations.  */
1278   do_pending_stack_adjust ();
1279   NO_DEFER_POP;
1280
1281   /* Save the stack with nonlocal if available.  */
1282 #ifdef HAVE_save_stack_nonlocal
1283   if (HAVE_save_stack_nonlocal)
1284     emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1285   else
1286 #endif
1287     emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1288
1289   /* Allocate a block of memory onto the stack and copy the memory
1290      arguments to the outgoing arguments address.  */
1291   allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1292   dest = virtual_outgoing_args_rtx;
1293 #ifndef STACK_GROWS_DOWNWARD
1294   if (GET_CODE (argsize) == CONST_INT)
1295     dest = plus_constant (dest, -INTVAL (argsize));
1296   else
1297     dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1298 #endif
1299   dest = gen_rtx_MEM (BLKmode, dest);
1300   set_mem_align (dest, PARM_BOUNDARY);
1301   src = gen_rtx_MEM (BLKmode, incoming_args);
1302   set_mem_align (src, PARM_BOUNDARY);
1303   emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1304
1305   /* Refer to the argument block.  */
1306   apply_args_size ();
1307   arguments = gen_rtx_MEM (BLKmode, arguments);
1308   set_mem_align (arguments, PARM_BOUNDARY);
1309
1310   /* Walk past the arg-pointer and structure value address.  */
1311   size = GET_MODE_SIZE (Pmode);
1312   if (struct_value)
1313     size += GET_MODE_SIZE (Pmode);
1314
1315   /* Restore each of the registers previously saved.  Make USE insns
1316      for each of these registers for use in making the call.  */
1317   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1318     if ((mode = apply_args_mode[regno]) != VOIDmode)
1319       {
1320         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1321         if (size % align != 0)
1322           size = CEIL (size, align) * align;
1323         reg = gen_rtx_REG (mode, regno);
1324         emit_move_insn (reg, adjust_address (arguments, mode, size));
1325         use_reg (&call_fusage, reg);
1326         size += GET_MODE_SIZE (mode);
1327       }
1328
1329   /* Restore the structure value address unless this is passed as an
1330      "invisible" first argument.  */
1331   size = GET_MODE_SIZE (Pmode);
1332   if (struct_value)
1333     {
1334       rtx value = gen_reg_rtx (Pmode);
1335       emit_move_insn (value, adjust_address (arguments, Pmode, size));
1336       emit_move_insn (struct_value, value);
1337       if (REG_P (struct_value))
1338         use_reg (&call_fusage, struct_value);
1339       size += GET_MODE_SIZE (Pmode);
1340     }
1341
1342   /* All arguments and registers used for the call are set up by now!  */
1343   function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1344
1345   /* Ensure address is valid.  SYMBOL_REF is already valid, so no need,
1346      and we don't want to load it into a register as an optimization,
1347      because prepare_call_address already did it if it should be done.  */
1348   if (GET_CODE (function) != SYMBOL_REF)
1349     function = memory_address (FUNCTION_MODE, function);
1350
1351   /* Generate the actual call instruction and save the return value.  */
1352 #ifdef HAVE_untyped_call
1353   if (HAVE_untyped_call)
1354     emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1355                                       result, result_vector (1, result)));
1356   else
1357 #endif
1358 #ifdef HAVE_call_value
1359   if (HAVE_call_value)
1360     {
1361       rtx valreg = 0;
1362
1363       /* Locate the unique return register.  It is not possible to
1364          express a call that sets more than one return register using
1365          call_value; use untyped_call for that.  In fact, untyped_call
1366          only needs to save the return registers in the given block.  */
1367       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1368         if ((mode = apply_result_mode[regno]) != VOIDmode)
1369           {
1370             gcc_assert (!valreg); /* HAVE_untyped_call required.  */
1371
1372             valreg = gen_rtx_REG (mode, regno);
1373           }
1374
1375       emit_call_insn (GEN_CALL_VALUE (valreg,
1376                                       gen_rtx_MEM (FUNCTION_MODE, function),
1377                                       const0_rtx, NULL_RTX, const0_rtx));
1378
1379       emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1380     }
1381   else
1382 #endif
1383     gcc_unreachable ();
1384
1385   /* Find the CALL insn we just emitted, and attach the register usage
1386      information.  */
1387   call_insn = last_call_insn ();
1388   add_function_usage_to (call_insn, call_fusage);
1389
1390   /* Restore the stack.  */
1391 #ifdef HAVE_save_stack_nonlocal
1392   if (HAVE_save_stack_nonlocal)
1393     emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1394   else
1395 #endif
1396     emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1397
1398   OK_DEFER_POP;
1399
1400   /* Return the address of the result block.  */
1401   result = copy_addr_to_reg (XEXP (result, 0));
1402   return convert_memory_address (ptr_mode, result);
1403 }
1404
1405 /* Perform an untyped return.  */
1406
1407 static void
1408 expand_builtin_return (rtx result)
1409 {
1410   int size, align, regno;
1411   enum machine_mode mode;
1412   rtx reg;
1413   rtx call_fusage = 0;
1414
1415   result = convert_memory_address (Pmode, result);
1416
1417   apply_result_size ();
1418   result = gen_rtx_MEM (BLKmode, result);
1419
1420 #ifdef HAVE_untyped_return
1421   if (HAVE_untyped_return)
1422     {
1423       emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1424       emit_barrier ();
1425       return;
1426     }
1427 #endif
1428
1429   /* Restore the return value and note that each value is used.  */
1430   size = 0;
1431   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1432     if ((mode = apply_result_mode[regno]) != VOIDmode)
1433       {
1434         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1435         if (size % align != 0)
1436           size = CEIL (size, align) * align;
1437         reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1438         emit_move_insn (reg, adjust_address (result, mode, size));
1439
1440         push_to_sequence (call_fusage);
1441         emit_insn (gen_rtx_USE (VOIDmode, reg));
1442         call_fusage = get_insns ();
1443         end_sequence ();
1444         size += GET_MODE_SIZE (mode);
1445       }
1446
1447   /* Put the USE insns before the return.  */
1448   emit_insn (call_fusage);
1449
1450   /* Return whatever values was restored by jumping directly to the end
1451      of the function.  */
1452   expand_naked_return ();
1453 }
1454
1455 /* Used by expand_builtin_classify_type and fold_builtin_classify_type.  */
1456
1457 static enum type_class
1458 type_to_class (tree type)
1459 {
1460   switch (TREE_CODE (type))
1461     {
1462     case VOID_TYPE:        return void_type_class;
1463     case INTEGER_TYPE:     return integer_type_class;
1464     case CHAR_TYPE:        return char_type_class;
1465     case ENUMERAL_TYPE:    return enumeral_type_class;
1466     case BOOLEAN_TYPE:     return boolean_type_class;
1467     case POINTER_TYPE:     return pointer_type_class;
1468     case REFERENCE_TYPE:   return reference_type_class;
1469     case OFFSET_TYPE:      return offset_type_class;
1470     case REAL_TYPE:        return real_type_class;
1471     case COMPLEX_TYPE:     return complex_type_class;
1472     case FUNCTION_TYPE:    return function_type_class;
1473     case METHOD_TYPE:      return method_type_class;
1474     case RECORD_TYPE:      return record_type_class;
1475     case UNION_TYPE:
1476     case QUAL_UNION_TYPE:  return union_type_class;
1477     case ARRAY_TYPE:       return (TYPE_STRING_FLAG (type)
1478                                    ? string_type_class : array_type_class);
1479     case FILE_TYPE:        return file_type_class;
1480     case LANG_TYPE:        return lang_type_class;
1481     default:               return no_type_class;
1482     }
1483 }
1484
1485 /* Expand a call to __builtin_classify_type with arguments found in
1486    ARGLIST.  */
1487
1488 static rtx
1489 expand_builtin_classify_type (tree arglist)
1490 {
1491   if (arglist != 0)
1492     return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1493   return GEN_INT (no_type_class);
1494 }
1495
1496 /* This helper macro, meant to be used in mathfn_built_in below,
1497    determines which among a set of three builtin math functions is
1498    appropriate for a given type mode.  The `F' and `L' cases are
1499    automatically generated from the `double' case.  */
1500 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1501   case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1502   fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1503   fcodel = BUILT_IN_MATHFN##L ; break;
1504
1505 /* Return mathematic function equivalent to FN but operating directly
1506    on TYPE, if available.  If we can't do the conversion, return zero.  */
1507 tree
1508 mathfn_built_in (tree type, enum built_in_function fn)
1509 {
1510   enum built_in_function fcode, fcodef, fcodel;
1511
1512   switch (fn)
1513     {
1514       CASE_MATHFN (BUILT_IN_ACOS)
1515       CASE_MATHFN (BUILT_IN_ACOSH)
1516       CASE_MATHFN (BUILT_IN_ASIN)
1517       CASE_MATHFN (BUILT_IN_ASINH)
1518       CASE_MATHFN (BUILT_IN_ATAN)
1519       CASE_MATHFN (BUILT_IN_ATAN2)
1520       CASE_MATHFN (BUILT_IN_ATANH)
1521       CASE_MATHFN (BUILT_IN_CBRT)
1522       CASE_MATHFN (BUILT_IN_CEIL)
1523       CASE_MATHFN (BUILT_IN_COPYSIGN)
1524       CASE_MATHFN (BUILT_IN_COS)
1525       CASE_MATHFN (BUILT_IN_COSH)
1526       CASE_MATHFN (BUILT_IN_DREM)
1527       CASE_MATHFN (BUILT_IN_ERF)
1528       CASE_MATHFN (BUILT_IN_ERFC)
1529       CASE_MATHFN (BUILT_IN_EXP)
1530       CASE_MATHFN (BUILT_IN_EXP10)
1531       CASE_MATHFN (BUILT_IN_EXP2)
1532       CASE_MATHFN (BUILT_IN_EXPM1)
1533       CASE_MATHFN (BUILT_IN_FABS)
1534       CASE_MATHFN (BUILT_IN_FDIM)
1535       CASE_MATHFN (BUILT_IN_FLOOR)
1536       CASE_MATHFN (BUILT_IN_FMA)
1537       CASE_MATHFN (BUILT_IN_FMAX)
1538       CASE_MATHFN (BUILT_IN_FMIN)
1539       CASE_MATHFN (BUILT_IN_FMOD)
1540       CASE_MATHFN (BUILT_IN_FREXP)
1541       CASE_MATHFN (BUILT_IN_GAMMA)
1542       CASE_MATHFN (BUILT_IN_HUGE_VAL)
1543       CASE_MATHFN (BUILT_IN_HYPOT)
1544       CASE_MATHFN (BUILT_IN_ILOGB)
1545       CASE_MATHFN (BUILT_IN_INF)
1546       CASE_MATHFN (BUILT_IN_J0)
1547       CASE_MATHFN (BUILT_IN_J1)
1548       CASE_MATHFN (BUILT_IN_JN)
1549       CASE_MATHFN (BUILT_IN_LDEXP)
1550       CASE_MATHFN (BUILT_IN_LGAMMA)
1551       CASE_MATHFN (BUILT_IN_LLRINT)
1552       CASE_MATHFN (BUILT_IN_LLROUND)
1553       CASE_MATHFN (BUILT_IN_LOG)
1554       CASE_MATHFN (BUILT_IN_LOG10)
1555       CASE_MATHFN (BUILT_IN_LOG1P)
1556       CASE_MATHFN (BUILT_IN_LOG2)
1557       CASE_MATHFN (BUILT_IN_LOGB)
1558       CASE_MATHFN (BUILT_IN_LRINT)
1559       CASE_MATHFN (BUILT_IN_LROUND)
1560       CASE_MATHFN (BUILT_IN_MODF)
1561       CASE_MATHFN (BUILT_IN_NAN)
1562       CASE_MATHFN (BUILT_IN_NANS)
1563       CASE_MATHFN (BUILT_IN_NEARBYINT)
1564       CASE_MATHFN (BUILT_IN_NEXTAFTER)
1565       CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1566       CASE_MATHFN (BUILT_IN_POW)
1567       CASE_MATHFN (BUILT_IN_POWI)
1568       CASE_MATHFN (BUILT_IN_POW10)
1569       CASE_MATHFN (BUILT_IN_REMAINDER)
1570       CASE_MATHFN (BUILT_IN_REMQUO)
1571       CASE_MATHFN (BUILT_IN_RINT)
1572       CASE_MATHFN (BUILT_IN_ROUND)
1573       CASE_MATHFN (BUILT_IN_SCALB)
1574       CASE_MATHFN (BUILT_IN_SCALBLN)
1575       CASE_MATHFN (BUILT_IN_SCALBN)
1576       CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1577       CASE_MATHFN (BUILT_IN_SIN)
1578       CASE_MATHFN (BUILT_IN_SINCOS)
1579       CASE_MATHFN (BUILT_IN_SINH)
1580       CASE_MATHFN (BUILT_IN_SQRT)
1581       CASE_MATHFN (BUILT_IN_TAN)
1582       CASE_MATHFN (BUILT_IN_TANH)
1583       CASE_MATHFN (BUILT_IN_TGAMMA)
1584       CASE_MATHFN (BUILT_IN_TRUNC)
1585       CASE_MATHFN (BUILT_IN_Y0)
1586       CASE_MATHFN (BUILT_IN_Y1)
1587       CASE_MATHFN (BUILT_IN_YN)
1588
1589       default:
1590         return 0;
1591       }
1592
1593   if (TYPE_MAIN_VARIANT (type) == double_type_node)
1594     return implicit_built_in_decls[fcode];
1595   else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1596     return implicit_built_in_decls[fcodef];
1597   else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1598     return implicit_built_in_decls[fcodel];
1599   else
1600     return 0;
1601 }
1602
1603 /* If errno must be maintained, expand the RTL to check if the result,
1604    TARGET, of a built-in function call, EXP, is NaN, and if so set
1605    errno to EDOM.  */
1606
1607 static void
1608 expand_errno_check (tree exp, rtx target)
1609 {
1610   rtx lab = gen_label_rtx ();
1611
1612   /* Test the result; if it is NaN, set errno=EDOM because
1613      the argument was not in the domain.  */
1614   emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1615                            0, lab);
1616
1617 #ifdef TARGET_EDOM
1618   /* If this built-in doesn't throw an exception, set errno directly.  */
1619   if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1620     {
1621 #ifdef GEN_ERRNO_RTX
1622       rtx errno_rtx = GEN_ERRNO_RTX;
1623 #else
1624       rtx errno_rtx
1625           = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1626 #endif
1627       emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1628       emit_label (lab);
1629       return;
1630     }
1631 #endif
1632
1633   /* We can't set errno=EDOM directly; let the library call do it.
1634      Pop the arguments right away in case the call gets deleted.  */
1635   NO_DEFER_POP;
1636   expand_call (exp, target, 0);
1637   OK_DEFER_POP;
1638   emit_label (lab);
1639 }
1640
1641
1642 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1643    Return 0 if a normal call should be emitted rather than expanding the
1644    function in-line.  EXP is the expression that is a call to the builtin
1645    function; if convenient, the result should be placed in TARGET.
1646    SUBTARGET may be used as the target for computing one of EXP's operands.  */
1647
1648 static rtx
1649 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1650 {
1651   optab builtin_optab;
1652   rtx op0, insns, before_call;
1653   tree fndecl = get_callee_fndecl (exp);
1654   tree arglist = TREE_OPERAND (exp, 1);
1655   enum machine_mode mode;
1656   bool errno_set = false;
1657   tree arg, narg;
1658
1659   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1660     return 0;
1661
1662   arg = TREE_VALUE (arglist);
1663
1664   switch (DECL_FUNCTION_CODE (fndecl))
1665     {
1666     case BUILT_IN_SQRT:
1667     case BUILT_IN_SQRTF:
1668     case BUILT_IN_SQRTL:
1669       errno_set = ! tree_expr_nonnegative_p (arg);
1670       builtin_optab = sqrt_optab;
1671       break;
1672     case BUILT_IN_EXP:
1673     case BUILT_IN_EXPF:
1674     case BUILT_IN_EXPL:
1675       errno_set = true; builtin_optab = exp_optab; break;
1676     case BUILT_IN_EXP10:
1677     case BUILT_IN_EXP10F:
1678     case BUILT_IN_EXP10L:
1679     case BUILT_IN_POW10:
1680     case BUILT_IN_POW10F:
1681     case BUILT_IN_POW10L:
1682       errno_set = true; builtin_optab = exp10_optab; break;
1683     case BUILT_IN_EXP2:
1684     case BUILT_IN_EXP2F:
1685     case BUILT_IN_EXP2L:
1686       errno_set = true; builtin_optab = exp2_optab; break;
1687     case BUILT_IN_EXPM1:
1688     case BUILT_IN_EXPM1F:
1689     case BUILT_IN_EXPM1L:
1690       errno_set = true; builtin_optab = expm1_optab; break;
1691     case BUILT_IN_LOGB:
1692     case BUILT_IN_LOGBF:
1693     case BUILT_IN_LOGBL:
1694       errno_set = true; builtin_optab = logb_optab; break;
1695     case BUILT_IN_ILOGB:
1696     case BUILT_IN_ILOGBF:
1697     case BUILT_IN_ILOGBL:
1698       errno_set = true; builtin_optab = ilogb_optab; break;
1699     case BUILT_IN_LOG:
1700     case BUILT_IN_LOGF:
1701     case BUILT_IN_LOGL:
1702       errno_set = true; builtin_optab = log_optab; break;
1703     case BUILT_IN_LOG10:
1704     case BUILT_IN_LOG10F:
1705     case BUILT_IN_LOG10L:
1706       errno_set = true; builtin_optab = log10_optab; break;
1707     case BUILT_IN_LOG2:
1708     case BUILT_IN_LOG2F:
1709     case BUILT_IN_LOG2L:
1710       errno_set = true; builtin_optab = log2_optab; break;
1711     case BUILT_IN_LOG1P:
1712     case BUILT_IN_LOG1PF:
1713     case BUILT_IN_LOG1PL:
1714       errno_set = true; builtin_optab = log1p_optab; break;
1715     case BUILT_IN_ASIN:
1716     case BUILT_IN_ASINF:
1717     case BUILT_IN_ASINL:
1718       builtin_optab = asin_optab; break;
1719     case BUILT_IN_ACOS:
1720     case BUILT_IN_ACOSF:
1721     case BUILT_IN_ACOSL:
1722       builtin_optab = acos_optab; break;
1723     case BUILT_IN_TAN:
1724     case BUILT_IN_TANF:
1725     case BUILT_IN_TANL:
1726       builtin_optab = tan_optab; break;
1727     case BUILT_IN_ATAN:
1728     case BUILT_IN_ATANF:
1729     case BUILT_IN_ATANL:
1730       builtin_optab = atan_optab; break;
1731     case BUILT_IN_FLOOR:
1732     case BUILT_IN_FLOORF:
1733     case BUILT_IN_FLOORL:
1734       builtin_optab = floor_optab; break;
1735     case BUILT_IN_CEIL:
1736     case BUILT_IN_CEILF:
1737     case BUILT_IN_CEILL:
1738       builtin_optab = ceil_optab; break;
1739     case BUILT_IN_TRUNC:
1740     case BUILT_IN_TRUNCF:
1741     case BUILT_IN_TRUNCL:
1742       builtin_optab = btrunc_optab; break;
1743     case BUILT_IN_ROUND:
1744     case BUILT_IN_ROUNDF:
1745     case BUILT_IN_ROUNDL:
1746       builtin_optab = round_optab; break;
1747     case BUILT_IN_NEARBYINT:
1748     case BUILT_IN_NEARBYINTF:
1749     case BUILT_IN_NEARBYINTL:
1750       builtin_optab = nearbyint_optab; break;
1751     case BUILT_IN_RINT:
1752     case BUILT_IN_RINTF:
1753     case BUILT_IN_RINTL:
1754       builtin_optab = rint_optab; break;
1755     default:
1756       gcc_unreachable ();
1757     }
1758
1759   /* Make a suitable register to place result in.  */
1760   mode = TYPE_MODE (TREE_TYPE (exp));
1761
1762   if (! flag_errno_math || ! HONOR_NANS (mode))
1763     errno_set = false;
1764
1765   /* Before working hard, check whether the instruction is available.  */
1766   if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1767     {
1768       target = gen_reg_rtx (mode);
1769
1770       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1771          need to expand the argument again.  This way, we will not perform
1772          side-effects more the once.  */
1773       narg = builtin_save_expr (arg);
1774       if (narg != arg)
1775         {
1776           arg = narg;
1777           arglist = build_tree_list (NULL_TREE, arg);
1778           exp = build_function_call_expr (fndecl, arglist);
1779         }
1780
1781       op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1782
1783       start_sequence ();
1784
1785       /* Compute into TARGET.
1786          Set TARGET to wherever the result comes back.  */
1787       target = expand_unop (mode, builtin_optab, op0, target, 0);
1788
1789       if (target != 0)
1790         {
1791           if (errno_set)
1792             expand_errno_check (exp, target);
1793
1794           /* Output the entire sequence.  */
1795           insns = get_insns ();
1796           end_sequence ();
1797           emit_insn (insns);
1798           return target;
1799         }
1800
1801       /* If we were unable to expand via the builtin, stop the sequence
1802          (without outputting the insns) and call to the library function
1803          with the stabilized argument list.  */
1804       end_sequence ();
1805     }
1806
1807   before_call = get_last_insn ();
1808
1809   target = expand_call (exp, target, target == const0_rtx);
1810
1811   /* If this is a sqrt operation and we don't care about errno, try to
1812      attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1813      This allows the semantics of the libcall to be visible to the RTL
1814      optimizers.  */
1815   if (builtin_optab == sqrt_optab && !errno_set)
1816     {
1817       /* Search backwards through the insns emitted by expand_call looking
1818          for the instruction with the REG_RETVAL note.  */
1819       rtx last = get_last_insn ();
1820       while (last != before_call)
1821         {
1822           if (find_reg_note (last, REG_RETVAL, NULL))
1823             {
1824               rtx note = find_reg_note (last, REG_EQUAL, NULL);
1825               /* Check that the REQ_EQUAL note is an EXPR_LIST with
1826                  two elements, i.e. symbol_ref(sqrt) and the operand.  */
1827               if (note
1828                   && GET_CODE (note) == EXPR_LIST
1829                   && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1830                   && XEXP (XEXP (note, 0), 1) != NULL_RTX
1831                   && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1832                 {
1833                   rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1834                   /* Check operand is a register with expected mode.  */
1835                   if (operand
1836                       && REG_P (operand)
1837                       && GET_MODE (operand) == mode)
1838                     {
1839                       /* Replace the REG_EQUAL note with a SQRT rtx.  */
1840                       rtx equiv = gen_rtx_SQRT (mode, operand);
1841                       set_unique_reg_note (last, REG_EQUAL, equiv);
1842                     }
1843                 }
1844               break;
1845             }
1846           last = PREV_INSN (last);
1847         }
1848     }
1849
1850   return target;
1851 }
1852
1853 /* Expand a call to the builtin binary math functions (pow and atan2).
1854    Return 0 if a normal call should be emitted rather than expanding the
1855    function in-line.  EXP is the expression that is a call to the builtin
1856    function; if convenient, the result should be placed in TARGET.
1857    SUBTARGET may be used as the target for computing one of EXP's
1858    operands.  */
1859
1860 static rtx
1861 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1862 {
1863   optab builtin_optab;
1864   rtx op0, op1, insns;
1865   tree fndecl = get_callee_fndecl (exp);
1866   tree arglist = TREE_OPERAND (exp, 1);
1867   tree arg0, arg1, temp, narg;
1868   enum machine_mode mode;
1869   bool errno_set = true;
1870   bool stable = true;
1871
1872   if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
1873     return 0;
1874
1875   arg0 = TREE_VALUE (arglist);
1876   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1877
1878   switch (DECL_FUNCTION_CODE (fndecl))
1879     {
1880     case BUILT_IN_POW:
1881     case BUILT_IN_POWF:
1882     case BUILT_IN_POWL:
1883       builtin_optab = pow_optab; break;
1884     case BUILT_IN_ATAN2:
1885     case BUILT_IN_ATAN2F:
1886     case BUILT_IN_ATAN2L:
1887       builtin_optab = atan2_optab; break;
1888     case BUILT_IN_FMOD:
1889     case BUILT_IN_FMODF:
1890     case BUILT_IN_FMODL:
1891       builtin_optab = fmod_optab; break;
1892     case BUILT_IN_DREM:
1893     case BUILT_IN_DREMF:
1894     case BUILT_IN_DREML:
1895       builtin_optab = drem_optab; break;
1896     default:
1897       gcc_unreachable ();
1898     }
1899
1900   /* Make a suitable register to place result in.  */
1901   mode = TYPE_MODE (TREE_TYPE (exp));
1902
1903   /* Before working hard, check whether the instruction is available.  */
1904   if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1905     return 0;
1906
1907   target = gen_reg_rtx (mode);
1908
1909   if (! flag_errno_math || ! HONOR_NANS (mode))
1910     errno_set = false;
1911
1912   /* Always stabilize the argument list.  */
1913   narg = builtin_save_expr (arg1);
1914   if (narg != arg1)
1915     {
1916       arg1 = narg;
1917       temp = build_tree_list (NULL_TREE, narg);
1918       stable = false;
1919     }
1920   else
1921     temp = TREE_CHAIN (arglist);
1922
1923   narg = builtin_save_expr (arg0);
1924   if (narg != arg0)
1925     {
1926       arg0 = narg;
1927       arglist = tree_cons (NULL_TREE, narg, temp);
1928       stable = false;
1929     }
1930   else if (! stable)
1931     arglist = tree_cons (NULL_TREE, arg0, temp);
1932
1933   if (! stable)
1934     exp = build_function_call_expr (fndecl, arglist);
1935
1936   op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1937   op1 = expand_expr (arg1, 0, VOIDmode, 0);
1938
1939   start_sequence ();
1940
1941   /* Compute into TARGET.
1942      Set TARGET to wherever the result comes back.  */
1943   target = expand_binop (mode, builtin_optab, op0, op1,
1944                          target, 0, OPTAB_DIRECT);
1945
1946   /* If we were unable to expand via the builtin, stop the sequence
1947      (without outputting the insns) and call to the library function
1948      with the stabilized argument list.  */
1949   if (target == 0)
1950     {
1951       end_sequence ();
1952       return expand_call (exp, target, target == const0_rtx);
1953     }
1954
1955   if (errno_set)
1956     expand_errno_check (exp, target);
1957
1958   /* Output the entire sequence.  */
1959   insns = get_insns ();
1960   end_sequence ();
1961   emit_insn (insns);
1962
1963   return target;
1964 }
1965
1966 /* Expand a call to the builtin sin and cos math functions.
1967    Return 0 if a normal call should be emitted rather than expanding the
1968    function in-line.  EXP is the expression that is a call to the builtin
1969    function; if convenient, the result should be placed in TARGET.
1970    SUBTARGET may be used as the target for computing one of EXP's
1971    operands.  */
1972
1973 static rtx
1974 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
1975 {
1976   optab builtin_optab;
1977   rtx op0, insns, before_call;
1978   tree fndecl = get_callee_fndecl (exp);
1979   tree arglist = TREE_OPERAND (exp, 1);
1980   enum machine_mode mode;
1981   bool errno_set = false;
1982   tree arg, narg;
1983
1984   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1985     return 0;
1986
1987   arg = TREE_VALUE (arglist);
1988
1989   switch (DECL_FUNCTION_CODE (fndecl))
1990     {
1991     case BUILT_IN_SIN:
1992     case BUILT_IN_SINF:
1993     case BUILT_IN_SINL:
1994     case BUILT_IN_COS:
1995     case BUILT_IN_COSF:
1996     case BUILT_IN_COSL:
1997       builtin_optab = sincos_optab; break;
1998     default:
1999       gcc_unreachable ();
2000     }
2001
2002   /* Make a suitable register to place result in.  */
2003   mode = TYPE_MODE (TREE_TYPE (exp));
2004
2005   if (! flag_errno_math || ! HONOR_NANS (mode))
2006     errno_set = false;
2007
2008   /* Check if sincos insn is available, otherwise fallback
2009      to sin or cos insn.  */
2010   if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2011     switch (DECL_FUNCTION_CODE (fndecl))
2012       {
2013       case BUILT_IN_SIN:
2014       case BUILT_IN_SINF:
2015       case BUILT_IN_SINL:
2016         builtin_optab = sin_optab; break;
2017       case BUILT_IN_COS:
2018       case BUILT_IN_COSF:
2019       case BUILT_IN_COSL:
2020         builtin_optab = cos_optab; break;
2021       default:
2022         gcc_unreachable ();
2023       }
2024   }
2025
2026   /* Before working hard, check whether the instruction is available.  */
2027   if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2028     {
2029       target = gen_reg_rtx (mode);
2030
2031       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2032          need to expand the argument again.  This way, we will not perform
2033          side-effects more the once.  */
2034       narg = save_expr (arg);
2035       if (narg != arg)
2036         {
2037           arg = narg;
2038           arglist = build_tree_list (NULL_TREE, arg);
2039           exp = build_function_call_expr (fndecl, arglist);
2040         }
2041
2042       op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2043
2044       start_sequence ();
2045
2046       /* Compute into TARGET.
2047          Set TARGET to wherever the result comes back.  */
2048       if (builtin_optab == sincos_optab)
2049         {
2050           int result;
2051
2052           switch (DECL_FUNCTION_CODE (fndecl))
2053             {
2054             case BUILT_IN_SIN:
2055             case BUILT_IN_SINF:
2056             case BUILT_IN_SINL:
2057               result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2058               break;
2059             case BUILT_IN_COS:
2060             case BUILT_IN_COSF:
2061             case BUILT_IN_COSL:
2062               result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2063               break;
2064             default:
2065               gcc_unreachable ();
2066             }
2067           gcc_assert (result);
2068         }
2069       else
2070         {
2071           target = expand_unop (mode, builtin_optab, op0, target, 0);
2072         }
2073
2074       if (target != 0)
2075         {
2076           if (errno_set)
2077             expand_errno_check (exp, target);
2078
2079           /* Output the entire sequence.  */
2080           insns = get_insns ();
2081           end_sequence ();
2082           emit_insn (insns);
2083           return target;
2084         }
2085
2086       /* If we were unable to expand via the builtin, stop the sequence
2087          (without outputting the insns) and call to the library function
2088          with the stabilized argument list.  */
2089       end_sequence ();
2090     }
2091
2092   before_call = get_last_insn ();
2093
2094   target = expand_call (exp, target, target == const0_rtx);
2095
2096   return target;
2097 }
2098
2099 /* To evaluate powi(x,n), the floating point value x raised to the
2100    constant integer exponent n, we use a hybrid algorithm that
2101    combines the "window method" with look-up tables.  For an
2102    introduction to exponentiation algorithms and "addition chains",
2103    see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2104    "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2105    3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2106    Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998.  */
2107
2108 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2109    multiplications to inline before calling the system library's pow
2110    function.  powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2111    so this default never requires calling pow, powf or powl.  */
2112
2113 #ifndef POWI_MAX_MULTS
2114 #define POWI_MAX_MULTS  (2*HOST_BITS_PER_WIDE_INT-2)
2115 #endif
2116
2117 /* The size of the "optimal power tree" lookup table.  All
2118    exponents less than this value are simply looked up in the
2119    powi_table below.  This threshold is also used to size the
2120    cache of pseudo registers that hold intermediate results.  */
2121 #define POWI_TABLE_SIZE 256
2122
2123 /* The size, in bits of the window, used in the "window method"
2124    exponentiation algorithm.  This is equivalent to a radix of
2125    (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method".  */
2126 #define POWI_WINDOW_SIZE 3
2127
2128 /* The following table is an efficient representation of an
2129    "optimal power tree".  For each value, i, the corresponding
2130    value, j, in the table states than an optimal evaluation
2131    sequence for calculating pow(x,i) can be found by evaluating
2132    pow(x,j)*pow(x,i-j).  An optimal power tree for the first
2133    100 integers is given in Knuth's "Seminumerical algorithms".  */
2134
2135 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2136   {
2137       0,   1,   1,   2,   2,   3,   3,   4,  /*   0 -   7 */
2138       4,   6,   5,   6,   6,  10,   7,   9,  /*   8 -  15 */
2139       8,  16,   9,  16,  10,  12,  11,  13,  /*  16 -  23 */
2140      12,  17,  13,  18,  14,  24,  15,  26,  /*  24 -  31 */
2141      16,  17,  17,  19,  18,  33,  19,  26,  /*  32 -  39 */
2142      20,  25,  21,  40,  22,  27,  23,  44,  /*  40 -  47 */
2143      24,  32,  25,  34,  26,  29,  27,  44,  /*  48 -  55 */
2144      28,  31,  29,  34,  30,  60,  31,  36,  /*  56 -  63 */
2145      32,  64,  33,  34,  34,  46,  35,  37,  /*  64 -  71 */
2146      36,  65,  37,  50,  38,  48,  39,  69,  /*  72 -  79 */
2147      40,  49,  41,  43,  42,  51,  43,  58,  /*  80 -  87 */
2148      44,  64,  45,  47,  46,  59,  47,  76,  /*  88 -  95 */
2149      48,  65,  49,  66,  50,  67,  51,  66,  /*  96 - 103 */
2150      52,  70,  53,  74,  54, 104,  55,  74,  /* 104 - 111 */
2151      56,  64,  57,  69,  58,  78,  59,  68,  /* 112 - 119 */
2152      60,  61,  61,  80,  62,  75,  63,  68,  /* 120 - 127 */
2153      64,  65,  65, 128,  66, 129,  67,  90,  /* 128 - 135 */
2154      68,  73,  69, 131,  70,  94,  71,  88,  /* 136 - 143 */
2155      72, 128,  73,  98,  74, 132,  75, 121,  /* 144 - 151 */
2156      76, 102,  77, 124,  78, 132,  79, 106,  /* 152 - 159 */
2157      80,  97,  81, 160,  82,  99,  83, 134,  /* 160 - 167 */
2158      84,  86,  85,  95,  86, 160,  87, 100,  /* 168 - 175 */
2159      88, 113,  89,  98,  90, 107,  91, 122,  /* 176 - 183 */
2160      92, 111,  93, 102,  94, 126,  95, 150,  /* 184 - 191 */
2161      96, 128,  97, 130,  98, 133,  99, 195,  /* 192 - 199 */
2162     100, 128, 101, 123, 102, 164, 103, 138,  /* 200 - 207 */
2163     104, 145, 105, 146, 106, 109, 107, 149,  /* 208 - 215 */
2164     108, 200, 109, 146, 110, 170, 111, 157,  /* 216 - 223 */
2165     112, 128, 113, 130, 114, 182, 115, 132,  /* 224 - 231 */
2166     116, 200, 117, 132, 118, 158, 119, 206,  /* 232 - 239 */
2167     120, 240, 121, 162, 122, 147, 123, 152,  /* 240 - 247 */
2168     124, 166, 125, 214, 126, 138, 127, 153,  /* 248 - 255 */
2169   };
2170
2171
2172 /* Return the number of multiplications required to calculate
2173    powi(x,n) where n is less than POWI_TABLE_SIZE.  This is a
2174    subroutine of powi_cost.  CACHE is an array indicating
2175    which exponents have already been calculated.  */
2176
2177 static int
2178 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2179 {
2180   /* If we've already calculated this exponent, then this evaluation
2181      doesn't require any additional multiplications.  */
2182   if (cache[n])
2183     return 0;
2184
2185   cache[n] = true;
2186   return powi_lookup_cost (n - powi_table[n], cache)
2187          + powi_lookup_cost (powi_table[n], cache) + 1;
2188 }
2189
2190 /* Return the number of multiplications required to calculate
2191    powi(x,n) for an arbitrary x, given the exponent N.  This
2192    function needs to be kept in sync with expand_powi below.  */
2193
2194 static int
2195 powi_cost (HOST_WIDE_INT n)
2196 {
2197   bool cache[POWI_TABLE_SIZE];
2198   unsigned HOST_WIDE_INT digit;
2199   unsigned HOST_WIDE_INT val;
2200   int result;
2201
2202   if (n == 0)
2203     return 0;
2204
2205   /* Ignore the reciprocal when calculating the cost.  */
2206   val = (n < 0) ? -n : n;
2207
2208   /* Initialize the exponent cache.  */
2209   memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2210   cache[1] = true;
2211
2212   result = 0;
2213
2214   while (val >= POWI_TABLE_SIZE)
2215     {
2216       if (val & 1)
2217         {
2218           digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2219           result += powi_lookup_cost (digit, cache)
2220                     + POWI_WINDOW_SIZE + 1;
2221           val >>= POWI_WINDOW_SIZE;
2222         }
2223       else
2224         {
2225           val >>= 1;
2226           result++;
2227         }
2228     }
2229
2230   return result + powi_lookup_cost (val, cache);
2231 }
2232
2233 /* Recursive subroutine of expand_powi.  This function takes the array,
2234    CACHE, of already calculated exponents and an exponent N and returns
2235    an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE.  */
2236
2237 static rtx
2238 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2239 {
2240   unsigned HOST_WIDE_INT digit;
2241   rtx target, result;
2242   rtx op0, op1;
2243
2244   if (n < POWI_TABLE_SIZE)
2245     {
2246       if (cache[n])
2247         return cache[n];
2248
2249       target = gen_reg_rtx (mode);
2250       cache[n] = target;
2251
2252       op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2253       op1 = expand_powi_1 (mode, powi_table[n], cache);
2254     }
2255   else if (n & 1)
2256     {
2257       target = gen_reg_rtx (mode);
2258       digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2259       op0 = expand_powi_1 (mode, n - digit, cache);
2260       op1 = expand_powi_1 (mode, digit, cache);
2261     }
2262   else
2263     {
2264       target = gen_reg_rtx (mode);
2265       op0 = expand_powi_1 (mode, n >> 1, cache);
2266       op1 = op0;
2267     }
2268
2269   result = expand_mult (mode, op0, op1, target, 0);
2270   if (result != target)
2271     emit_move_insn (target, result);
2272   return target;
2273 }
2274
2275 /* Expand the RTL to evaluate powi(x,n) in mode MODE.  X is the
2276    floating point operand in mode MODE, and N is the exponent.  This
2277    function needs to be kept in sync with powi_cost above.  */
2278
2279 static rtx
2280 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2281 {
2282   unsigned HOST_WIDE_INT val;
2283   rtx cache[POWI_TABLE_SIZE];
2284   rtx result;
2285
2286   if (n == 0)
2287     return CONST1_RTX (mode);
2288
2289   val = (n < 0) ? -n : n;
2290
2291   memset (cache, 0, sizeof (cache));
2292   cache[1] = x;
2293
2294   result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2295
2296   /* If the original exponent was negative, reciprocate the result.  */
2297   if (n < 0)
2298     result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2299                            result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2300
2301   return result;
2302 }
2303
2304 /* Expand a call to the pow built-in mathematical function.  Return 0 if
2305    a normal call should be emitted rather than expanding the function
2306    in-line.  EXP is the expression that is a call to the builtin
2307    function; if convenient, the result should be placed in TARGET.  */
2308
2309 static rtx
2310 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2311 {
2312   tree arglist = TREE_OPERAND (exp, 1);
2313   tree arg0, arg1;
2314
2315   if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2316     return 0;
2317
2318   arg0 = TREE_VALUE (arglist);
2319   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2320
2321   if (TREE_CODE (arg1) == REAL_CST
2322       && ! TREE_CONSTANT_OVERFLOW (arg1))
2323     {
2324       REAL_VALUE_TYPE cint;
2325       REAL_VALUE_TYPE c;
2326       HOST_WIDE_INT n;
2327
2328       c = TREE_REAL_CST (arg1);
2329       n = real_to_integer (&c);
2330       real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2331       if (real_identical (&c, &cint))
2332         {
2333           /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2334              Otherwise, check the number of multiplications required.
2335              Note that pow never sets errno for an integer exponent.  */
2336           if ((n >= -1 && n <= 2)
2337               || (flag_unsafe_math_optimizations
2338                   && ! optimize_size
2339                   && powi_cost (n) <= POWI_MAX_MULTS))
2340             {
2341               enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2342               rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2343               op = force_reg (mode, op);
2344               return expand_powi (op, mode, n);
2345             }
2346         }
2347     }
2348
2349   if (! flag_unsafe_math_optimizations)
2350     return NULL_RTX;
2351   return expand_builtin_mathfn_2 (exp, target, subtarget);
2352 }
2353
2354 /* Expand a call to the powi built-in mathematical function.  Return 0 if
2355    a normal call should be emitted rather than expanding the function
2356    in-line.  EXP is the expression that is a call to the builtin
2357    function; if convenient, the result should be placed in TARGET.  */
2358
2359 static rtx
2360 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2361 {
2362   tree arglist = TREE_OPERAND (exp, 1);
2363   tree arg0, arg1;
2364   rtx op0, op1;
2365   enum machine_mode mode;
2366
2367   if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2368     return 0;
2369
2370   arg0 = TREE_VALUE (arglist);
2371   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2372   mode = TYPE_MODE (TREE_TYPE (exp));
2373
2374   /* Handle constant power.  */
2375
2376   if (TREE_CODE (arg1) == INTEGER_CST
2377       && ! TREE_CONSTANT_OVERFLOW (arg1))
2378     {
2379       HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2380
2381       /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2382          Otherwise, check the number of multiplications required.  */
2383       if ((TREE_INT_CST_HIGH (arg1) == 0
2384            || TREE_INT_CST_HIGH (arg1) == -1)
2385           && ((n >= -1 && n <= 2)
2386               || (! optimize_size
2387                   && powi_cost (n) <= POWI_MAX_MULTS)))
2388         {
2389           op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2390           op0 = force_reg (mode, op0);
2391           return expand_powi (op0, mode, n);
2392         }
2393     }
2394
2395   /* Emit a libcall to libgcc.  */
2396
2397   if (target == NULL_RTX)
2398     target = gen_reg_rtx (mode);
2399
2400   op0 = expand_expr (arg0, subtarget, mode, 0);
2401   if (GET_MODE (op0) != mode)
2402     op0 = convert_to_mode (mode, op0, 0);
2403   op1 = expand_expr (arg1, 0, word_mode, 0);
2404   if (GET_MODE (op1) != word_mode)
2405     op1 = convert_to_mode (word_mode, op1, 0);
2406
2407   target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2408                                     target, LCT_CONST_MAKE_BLOCK, mode, 2,
2409                                     op0, mode, op1, word_mode);
2410
2411   return target;
2412 }
2413
2414 /* Expand expression EXP which is a call to the strlen builtin.  Return 0
2415    if we failed the caller should emit a normal call, otherwise
2416    try to get the result in TARGET, if convenient.  */
2417
2418 static rtx
2419 expand_builtin_strlen (tree arglist, rtx target,
2420                        enum machine_mode target_mode)
2421 {
2422   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2423     return 0;
2424   else
2425     {
2426       rtx pat;
2427       tree len, src = TREE_VALUE (arglist);
2428       rtx result, src_reg, char_rtx, before_strlen;
2429       enum machine_mode insn_mode = target_mode, char_mode;
2430       enum insn_code icode = CODE_FOR_nothing;
2431       int align;
2432
2433       /* If the length can be computed at compile-time, return it.  */
2434       len = c_strlen (src, 0);
2435       if (len)
2436         return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2437
2438       /* If the length can be computed at compile-time and is constant
2439          integer, but there are side-effects in src, evaluate
2440          src for side-effects, then return len.
2441          E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2442          can be optimized into: i++; x = 3;  */
2443       len = c_strlen (src, 1);
2444       if (len && TREE_CODE (len) == INTEGER_CST)
2445         {
2446           expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2447           return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2448         }
2449
2450       align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2451
2452       /* If SRC is not a pointer type, don't do this operation inline.  */
2453       if (align == 0)
2454         return 0;
2455
2456       /* Bail out if we can't compute strlen in the right mode.  */
2457       while (insn_mode != VOIDmode)
2458         {
2459           icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2460           if (icode != CODE_FOR_nothing)
2461             break;
2462
2463           insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2464         }
2465       if (insn_mode == VOIDmode)
2466         return 0;
2467
2468       /* Make a place to write the result of the instruction.  */
2469       result = target;
2470       if (! (result != 0
2471              && REG_P (result)
2472              && GET_MODE (result) == insn_mode
2473              && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2474         result = gen_reg_rtx (insn_mode);
2475
2476       /* Make a place to hold the source address.  We will not expand
2477          the actual source until we are sure that the expansion will
2478          not fail -- there are trees that cannot be expanded twice.  */
2479       src_reg = gen_reg_rtx (Pmode);
2480
2481       /* Mark the beginning of the strlen sequence so we can emit the
2482          source operand later.  */
2483       before_strlen = get_last_insn ();
2484
2485       char_rtx = const0_rtx;
2486       char_mode = insn_data[(int) icode].operand[2].mode;
2487       if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2488                                                             char_mode))
2489         char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2490
2491       pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2492                              char_rtx, GEN_INT (align));
2493       if (! pat)
2494         return 0;
2495       emit_insn (pat);
2496
2497       /* Now that we are assured of success, expand the source.  */
2498       start_sequence ();
2499       pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2500       if (pat != src_reg)
2501         emit_move_insn (src_reg, pat);
2502       pat = get_insns ();
2503       end_sequence ();
2504
2505       if (before_strlen)
2506         emit_insn_after (pat, before_strlen);
2507       else
2508         emit_insn_before (pat, get_insns ());
2509
2510       /* Return the value in the proper mode for this function.  */
2511       if (GET_MODE (result) == target_mode)
2512         target = result;
2513       else if (target != 0)
2514         convert_move (target, result, 0);
2515       else
2516         target = convert_to_mode (target_mode, result, 0);
2517
2518       return target;
2519     }
2520 }
2521
2522 /* Expand a call to the strstr builtin.  Return 0 if we failed the
2523    caller should emit a normal call, otherwise try to get the result
2524    in TARGET, if convenient (and in mode MODE if that's convenient).  */
2525
2526 static rtx
2527 expand_builtin_strstr (tree arglist, rtx target, enum machine_mode mode)
2528 {
2529   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2530     {
2531       tree result = fold_builtin_strstr (arglist);
2532       if (result)
2533         return expand_expr (result, target, mode, EXPAND_NORMAL);
2534     }
2535   return 0;
2536 }
2537
2538 /* Expand a call to the strchr builtin.  Return 0 if we failed the
2539    caller should emit a normal call, otherwise try to get the result
2540    in TARGET, if convenient (and in mode MODE if that's convenient).  */
2541
2542 static rtx
2543 expand_builtin_strchr (tree arglist, rtx target, enum machine_mode mode)
2544 {
2545   if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2546     {
2547       tree result = fold_builtin_strchr (arglist);
2548       if (result)
2549         return expand_expr (result, target, mode, EXPAND_NORMAL);
2550
2551       /* FIXME: Should use strchrM optab so that ports can optimize this.  */
2552     }
2553   return 0;
2554 }
2555
2556 /* Expand a call to the strrchr builtin.  Return 0 if we failed the
2557    caller should emit a normal call, otherwise try to get the result
2558    in TARGET, if convenient (and in mode MODE if that's convenient).  */
2559
2560 static rtx
2561 expand_builtin_strrchr (tree arglist, rtx target, enum machine_mode mode)
2562 {
2563   if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2564     {
2565       tree result = fold_builtin_strrchr (arglist);
2566       if (result)
2567         return expand_expr (result, target, mode, EXPAND_NORMAL);
2568     }
2569   return 0;
2570 }
2571
2572 /* Expand a call to the strpbrk builtin.  Return 0 if we failed the
2573    caller should emit a normal call, otherwise try to get the result
2574    in TARGET, if convenient (and in mode MODE if that's convenient).  */
2575
2576 static rtx
2577 expand_builtin_strpbrk (tree arglist, rtx target, enum machine_mode mode)
2578 {
2579   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2580     {
2581       tree result = fold_builtin_strpbrk (arglist);
2582       if (result)
2583         return expand_expr (result, target, mode, EXPAND_NORMAL);
2584     }
2585   return 0;
2586 }
2587
2588 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
2589    bytes from constant string DATA + OFFSET and return it as target
2590    constant.  */
2591
2592 static rtx
2593 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2594                          enum machine_mode mode)
2595 {
2596   const char *str = (const char *) data;
2597
2598   gcc_assert (offset >= 0
2599               && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2600                   <= strlen (str) + 1));
2601
2602   return c_readstr (str + offset, mode);
2603 }
2604
2605 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2606    Return 0 if we failed, the caller should emit a normal call,
2607    otherwise try to get the result in TARGET, if convenient (and in
2608    mode MODE if that's convenient).  */
2609 static rtx
2610 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2611 {
2612   tree arglist = TREE_OPERAND (exp, 1);
2613   if (!validate_arglist (arglist,
2614                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2615     return 0;
2616   else
2617     {
2618       tree dest = TREE_VALUE (arglist);
2619       tree src = TREE_VALUE (TREE_CHAIN (arglist));
2620       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2621       const char *src_str;
2622       unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2623       unsigned int dest_align
2624         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2625       rtx dest_mem, src_mem, dest_addr, len_rtx;
2626       tree result = fold_builtin_memcpy (exp);
2627
2628       if (result)
2629         return expand_expr (result, target, mode, EXPAND_NORMAL);
2630
2631       /* If DEST is not a pointer type, call the normal function.  */
2632       if (dest_align == 0)
2633         return 0;
2634
2635       /* If either SRC is not a pointer type, don't do this
2636          operation in-line.  */
2637       if (src_align == 0)
2638         return 0;
2639
2640       dest_mem = get_memory_rtx (dest);
2641       set_mem_align (dest_mem, dest_align);
2642       len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2643       src_str = c_getstr (src);
2644
2645       /* If SRC is a string constant and block move would be done
2646          by pieces, we can avoid loading the string from memory
2647          and only stored the computed constants.  */
2648       if (src_str
2649           && GET_CODE (len_rtx) == CONST_INT
2650           && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2651           && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2652                                   (void *) src_str, dest_align))
2653         {
2654           dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2655                                       builtin_memcpy_read_str,
2656                                       (void *) src_str, dest_align, 0);
2657           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2658           dest_mem = convert_memory_address (ptr_mode, dest_mem);
2659           return dest_mem;
2660         }
2661
2662       src_mem = get_memory_rtx (src);
2663       set_mem_align (src_mem, src_align);
2664
2665       /* Copy word part most expediently.  */
2666       dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2667                                    BLOCK_OP_NORMAL);
2668
2669       if (dest_addr == 0)
2670         {
2671           dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2672           dest_addr = convert_memory_address (ptr_mode, dest_addr);
2673         }
2674       return dest_addr;
2675     }
2676 }
2677
2678 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2679    Return 0 if we failed the caller should emit a normal call,
2680    otherwise try to get the result in TARGET, if convenient (and in
2681    mode MODE if that's convenient).  If ENDP is 0 return the
2682    destination pointer, if ENDP is 1 return the end pointer ala
2683    mempcpy, and if ENDP is 2 return the end pointer minus one ala
2684    stpcpy.  */
2685
2686 static rtx
2687 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2688                         int endp)
2689 {
2690   if (!validate_arglist (arglist,
2691                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2692     return 0;
2693   /* If return value is ignored, transform mempcpy into memcpy.  */
2694   else if (target == const0_rtx)
2695     {
2696       tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2697
2698       if (!fn)
2699         return 0;
2700
2701       return expand_expr (build_function_call_expr (fn, arglist),
2702                           target, mode, EXPAND_NORMAL);
2703     }
2704   else
2705     {
2706       tree dest = TREE_VALUE (arglist);
2707       tree src = TREE_VALUE (TREE_CHAIN (arglist));
2708       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2709       const char *src_str;
2710       unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2711       unsigned int dest_align
2712         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2713       rtx dest_mem, src_mem, len_rtx;
2714       tree result = fold_builtin_mempcpy (arglist, type, endp);
2715
2716       if (result)
2717         return expand_expr (result, target, mode, EXPAND_NORMAL);
2718       
2719       /* If either SRC or DEST is not a pointer type, don't do this
2720          operation in-line.  */
2721       if (dest_align == 0 || src_align == 0)
2722         return 0;
2723
2724       /* If LEN is not constant, call the normal function.  */
2725       if (! host_integerp (len, 1))
2726         return 0;
2727
2728       len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2729       src_str = c_getstr (src);
2730
2731       /* If SRC is a string constant and block move would be done
2732          by pieces, we can avoid loading the string from memory
2733          and only stored the computed constants.  */
2734       if (src_str
2735           && GET_CODE (len_rtx) == CONST_INT
2736           && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2737           && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2738                                   (void *) src_str, dest_align))
2739         {
2740           dest_mem = get_memory_rtx (dest);
2741           set_mem_align (dest_mem, dest_align);
2742           dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2743                                       builtin_memcpy_read_str,
2744                                       (void *) src_str, dest_align, endp);
2745           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2746           dest_mem = convert_memory_address (ptr_mode, dest_mem);
2747           return dest_mem;
2748         }
2749
2750       if (GET_CODE (len_rtx) == CONST_INT
2751           && can_move_by_pieces (INTVAL (len_rtx),
2752                                  MIN (dest_align, src_align)))
2753         {
2754           dest_mem = get_memory_rtx (dest);
2755           set_mem_align (dest_mem, dest_align);
2756           src_mem = get_memory_rtx (src);
2757           set_mem_align (src_mem, src_align);
2758           dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2759                                      MIN (dest_align, src_align), endp);
2760           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2761           dest_mem = convert_memory_address (ptr_mode, dest_mem);
2762           return dest_mem;
2763         }
2764
2765       return 0;
2766     }
2767 }
2768
2769 /* Expand expression EXP, which is a call to the memmove builtin.  Return 0
2770    if we failed the caller should emit a normal call.  */
2771
2772 static rtx
2773 expand_builtin_memmove (tree arglist, tree type, rtx target,
2774                         enum machine_mode mode)
2775 {
2776   if (!validate_arglist (arglist,
2777                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2778     return 0;
2779   else
2780     {
2781       tree dest = TREE_VALUE (arglist);
2782       tree src = TREE_VALUE (TREE_CHAIN (arglist));
2783       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2784
2785       unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2786       unsigned int dest_align
2787         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2788       tree result = fold_builtin_memmove (arglist, type);
2789
2790       if (result)
2791         return expand_expr (result, target, mode, EXPAND_NORMAL);
2792
2793       /* If DEST is not a pointer type, call the normal function.  */
2794       if (dest_align == 0)
2795         return 0;
2796
2797       /* If either SRC is not a pointer type, don't do this
2798          operation in-line.  */
2799       if (src_align == 0)
2800         return 0;
2801
2802       /* If src is categorized for a readonly section we can use
2803          normal memcpy.  */
2804       if (readonly_data_expr (src))
2805         {
2806           tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2807           if (!fn)
2808             return 0;
2809           return expand_expr (build_function_call_expr (fn, arglist),
2810                               target, mode, EXPAND_NORMAL);
2811         }
2812
2813       /* If length is 1 and we can expand memcpy call inline,
2814          it is ok to use memcpy as well.  */
2815       if (integer_onep (len))
2816         {
2817           rtx ret = expand_builtin_mempcpy (arglist, type, target, mode,
2818                                             /*endp=*/0);
2819           if (ret)
2820             return ret;
2821         }
2822
2823       /* Otherwise, call the normal function.  */
2824       return 0;
2825    }
2826 }
2827
2828 /* Expand expression EXP, which is a call to the bcopy builtin.  Return 0
2829    if we failed the caller should emit a normal call.  */
2830
2831 static rtx
2832 expand_builtin_bcopy (tree arglist, tree type)
2833 {
2834   tree src, dest, size, newarglist;
2835
2836   if (!validate_arglist (arglist,
2837                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2838     return NULL_RTX;
2839
2840   src = TREE_VALUE (arglist);
2841   dest = TREE_VALUE (TREE_CHAIN (arglist));
2842   size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2843
2844   /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2845      memmove(ptr y, ptr x, size_t z).   This is done this way
2846      so that if it isn't expanded inline, we fallback to
2847      calling bcopy instead of memmove.  */
2848
2849   newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
2850   newarglist = tree_cons (NULL_TREE, src, newarglist);
2851   newarglist = tree_cons (NULL_TREE, dest, newarglist);
2852
2853   return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
2854 }
2855
2856 #ifndef HAVE_movstr
2857 # define HAVE_movstr 0
2858 # define CODE_FOR_movstr CODE_FOR_nothing
2859 #endif
2860
2861 /* Expand into a movstr instruction, if one is available.  Return 0 if
2862    we failed, the caller should emit a normal call, otherwise try to
2863    get the result in TARGET, if convenient.  If ENDP is 0 return the
2864    destination pointer, if ENDP is 1 return the end pointer ala
2865    mempcpy, and if ENDP is 2 return the end pointer minus one ala
2866    stpcpy.  */
2867
2868 static rtx
2869 expand_movstr (tree dest, tree src, rtx target, int endp)
2870 {
2871   rtx end;
2872   rtx dest_mem;
2873   rtx src_mem;
2874   rtx insn;
2875   const struct insn_data * data;
2876
2877   if (!HAVE_movstr)
2878     return 0;
2879
2880   dest_mem = get_memory_rtx (dest);
2881   src_mem = get_memory_rtx (src);
2882   if (!endp)
2883     {
2884       target = force_reg (Pmode, XEXP (dest_mem, 0));
2885       dest_mem = replace_equiv_address (dest_mem, target);
2886       end = gen_reg_rtx (Pmode);
2887     }
2888   else
2889     {
2890       if (target == 0 || target == const0_rtx)
2891         {
2892           end = gen_reg_rtx (Pmode);
2893           if (target == 0)
2894             target = end;
2895         }
2896       else
2897         end = target;
2898     }
2899
2900   data = insn_data + CODE_FOR_movstr;
2901
2902   if (data->operand[0].mode != VOIDmode)
2903     end = gen_lowpart (data->operand[0].mode, end);
2904
2905   insn = data->genfun (end, dest_mem, src_mem);
2906
2907   gcc_assert (insn);
2908
2909   emit_insn (insn);
2910
2911   /* movstr is supposed to set end to the address of the NUL
2912      terminator.  If the caller requested a mempcpy-like return value,
2913      adjust it.  */
2914   if (endp == 1 && target != const0_rtx)
2915     {
2916       rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
2917       emit_move_insn (target, force_operand (tem, NULL_RTX));
2918     }
2919
2920   return target;
2921 }
2922
2923 /* Expand expression EXP, which is a call to the strcpy builtin.  Return 0
2924    if we failed the caller should emit a normal call, otherwise try to get
2925    the result in TARGET, if convenient (and in mode MODE if that's
2926    convenient).  */
2927
2928 static rtx
2929 expand_builtin_strcpy (tree exp, rtx target, enum machine_mode mode)
2930 {
2931   tree arglist = TREE_OPERAND (exp, 1);
2932   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2933     {
2934       tree result = fold_builtin_strcpy (exp, 0);
2935       if (result)
2936         return expand_expr (result, target, mode, EXPAND_NORMAL);
2937
2938       return expand_movstr (TREE_VALUE (arglist),
2939                             TREE_VALUE (TREE_CHAIN (arglist)),
2940                             target, /*endp=*/0);
2941     }
2942   return 0;
2943 }
2944
2945 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
2946    Return 0 if we failed the caller should emit a normal call,
2947    otherwise try to get the result in TARGET, if convenient (and in
2948    mode MODE if that's convenient).  */
2949
2950 static rtx
2951 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
2952 {
2953   tree arglist = TREE_OPERAND (exp, 1);
2954   /* If return value is ignored, transform stpcpy into strcpy.  */
2955   if (target == const0_rtx)
2956     {
2957       tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
2958       if (!fn)
2959         return 0;
2960
2961       return expand_expr (build_function_call_expr (fn, arglist),
2962                           target, mode, EXPAND_NORMAL);
2963     }
2964
2965   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2966     return 0;
2967   else
2968     {
2969       tree dst, src, len, lenp1;
2970       tree narglist;
2971       rtx ret;
2972
2973       /* Ensure we get an actual string whose length can be evaluated at
2974          compile-time, not an expression containing a string.  This is
2975          because the latter will potentially produce pessimized code
2976          when used to produce the return value.  */
2977       src = TREE_VALUE (TREE_CHAIN (arglist));
2978       if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
2979         return expand_movstr (TREE_VALUE (arglist),
2980                               TREE_VALUE (TREE_CHAIN (arglist)),
2981                               target, /*endp=*/2);
2982
2983       dst = TREE_VALUE (arglist);
2984       lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
2985       narglist = build_tree_list (NULL_TREE, lenp1);
2986       narglist = tree_cons (NULL_TREE, src, narglist);
2987       narglist = tree_cons (NULL_TREE, dst, narglist);
2988       ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
2989                                     target, mode, /*endp=*/2);
2990
2991       if (ret)
2992         return ret;
2993
2994       if (TREE_CODE (len) == INTEGER_CST)
2995         {
2996           rtx len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2997
2998           if (GET_CODE (len_rtx) == CONST_INT)
2999             {
3000               ret = expand_builtin_strcpy (exp, target, mode);
3001
3002               if (ret)
3003                 {
3004                   if (! target)
3005                     {
3006                       if (mode != VOIDmode)
3007                         target = gen_reg_rtx (mode);
3008                       else
3009                         target = gen_reg_rtx (GET_MODE (ret));
3010                     }
3011                   if (GET_MODE (target) != GET_MODE (ret))
3012                     ret = gen_lowpart (GET_MODE (target), ret);
3013
3014                   ret = plus_constant (ret, INTVAL (len_rtx));
3015                   ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3016                   gcc_assert (ret);
3017
3018                   return target;
3019                 }
3020             }
3021         }
3022
3023       return expand_movstr (TREE_VALUE (arglist),
3024                             TREE_VALUE (TREE_CHAIN (arglist)),
3025                             target, /*endp=*/2);
3026     }
3027 }
3028
3029 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
3030    bytes from constant string DATA + OFFSET and return it as target
3031    constant.  */
3032
3033 static rtx
3034 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3035                           enum machine_mode mode)
3036 {
3037   const char *str = (const char *) data;
3038
3039   if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3040     return const0_rtx;
3041
3042   return c_readstr (str + offset, mode);
3043 }
3044
3045 /* Expand expression EXP, which is a call to the strncpy builtin.  Return 0
3046    if we failed the caller should emit a normal call.  */
3047
3048 static rtx
3049 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3050 {
3051   tree arglist = TREE_OPERAND (exp, 1);
3052   if (validate_arglist (arglist,
3053                         POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3054     {
3055       tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3056       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3057       tree result = fold_builtin_strncpy (exp, slen);
3058       
3059       if (result)
3060         return expand_expr (result, target, mode, EXPAND_NORMAL);
3061
3062       /* We must be passed a constant len and src parameter.  */
3063       if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3064         return 0;
3065
3066       slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3067
3068       /* We're required to pad with trailing zeros if the requested
3069          len is greater than strlen(s2)+1.  In that case try to
3070          use store_by_pieces, if it fails, punt.  */
3071       if (tree_int_cst_lt (slen, len))
3072         {
3073           tree dest = TREE_VALUE (arglist);
3074           unsigned int dest_align
3075             = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3076           const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3077           rtx dest_mem;
3078
3079           if (!p || dest_align == 0 || !host_integerp (len, 1)
3080               || !can_store_by_pieces (tree_low_cst (len, 1),
3081                                        builtin_strncpy_read_str,
3082                                        (void *) p, dest_align))
3083             return 0;
3084
3085           dest_mem = get_memory_rtx (dest);
3086           store_by_pieces (dest_mem, tree_low_cst (len, 1),
3087                            builtin_strncpy_read_str,
3088                            (void *) p, dest_align, 0);
3089           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3090           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3091           return dest_mem;
3092         }
3093     }
3094   return 0;
3095 }
3096
3097 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
3098    bytes from constant string DATA + OFFSET and return it as target
3099    constant.  */
3100
3101 static rtx
3102 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3103                          enum machine_mode mode)
3104 {
3105   const char *c = (const char *) data;
3106   char *p = alloca (GET_MODE_SIZE (mode));
3107
3108   memset (p, *c, GET_MODE_SIZE (mode));
3109
3110   return c_readstr (p, mode);
3111 }
3112
3113 /* Callback routine for store_by_pieces.  Return the RTL of a register
3114    containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3115    char value given in the RTL register data.  For example, if mode is
3116    4 bytes wide, return the RTL for 0x01010101*data.  */
3117
3118 static rtx
3119 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3120                         enum machine_mode mode)
3121 {
3122   rtx target, coeff;
3123   size_t size;
3124   char *p;
3125
3126   size = GET_MODE_SIZE (mode);
3127   if (size == 1)
3128     return (rtx) data;
3129
3130   p = alloca (size);
3131   memset (p, 1, size);
3132   coeff = c_readstr (p, mode);
3133
3134   target = convert_to_mode (mode, (rtx) data, 1);
3135   target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3136   return force_reg (mode, target);
3137 }
3138
3139 /* Expand expression EXP, which is a call to the memset builtin.  Return 0
3140    if we failed the caller should emit a normal call, otherwise try to get
3141    the result in TARGET, if convenient (and in mode MODE if that's
3142    convenient).  */
3143
3144 static rtx
3145 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
3146 {
3147   if (!validate_arglist (arglist,
3148                          POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3149     return 0;
3150   else
3151     {
3152       tree dest = TREE_VALUE (arglist);
3153       tree val = TREE_VALUE (TREE_CHAIN (arglist));
3154       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3155       char c;
3156
3157       unsigned int dest_align
3158         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3159       rtx dest_mem, dest_addr, len_rtx;
3160
3161       /* If DEST is not a pointer type, don't do this
3162          operation in-line.  */
3163       if (dest_align == 0)
3164         return 0;
3165
3166       /* If the LEN parameter is zero, return DEST.  */
3167       if (integer_zerop (len))
3168         {
3169           /* Evaluate and ignore VAL in case it has side-effects.  */
3170           expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3171           return expand_expr (dest, target, mode, EXPAND_NORMAL);
3172         }
3173
3174       if (TREE_CODE (val) != INTEGER_CST)
3175         {
3176           rtx val_rtx;
3177
3178           if (!host_integerp (len, 1))
3179             return 0;
3180
3181           if (optimize_size && tree_low_cst (len, 1) > 1)
3182             return 0;
3183
3184           /* Assume that we can memset by pieces if we can store the
3185            * the coefficients by pieces (in the required modes).
3186            * We can't pass builtin_memset_gen_str as that emits RTL.  */
3187           c = 1;
3188           if (!can_store_by_pieces (tree_low_cst (len, 1),
3189                                     builtin_memset_read_str,
3190                                     &c, dest_align))
3191             return 0;
3192
3193           val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3194           val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3195           val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3196                                val_rtx);
3197           dest_mem = get_memory_rtx (dest);
3198           store_by_pieces (dest_mem, tree_low_cst (len, 1),
3199                            builtin_memset_gen_str,
3200                            val_rtx, dest_align, 0);
3201           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3202           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3203           return dest_mem;
3204         }
3205
3206       if (target_char_cast (val, &c))
3207         return 0;
3208
3209       if (c)
3210         {
3211           if (!host_integerp (len, 1))
3212             return 0;
3213           if (!can_store_by_pieces (tree_low_cst (len, 1),
3214                                     builtin_memset_read_str, &c,
3215                                     dest_align))
3216             return 0;
3217
3218           dest_mem = get_memory_rtx (dest);
3219           store_by_pieces (dest_mem, tree_low_cst (len, 1),
3220                            builtin_memset_read_str,
3221                            &c, dest_align, 0);
3222           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3223           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3224           return dest_mem;
3225         }
3226
3227       len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3228
3229       dest_mem = get_memory_rtx (dest);
3230       set_mem_align (dest_mem, dest_align);
3231       dest_addr = clear_storage (dest_mem, len_rtx);
3232
3233       if (dest_addr == 0)
3234         {
3235           dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3236           dest_addr = convert_memory_address (ptr_mode, dest_addr);
3237         }
3238
3239       return dest_addr;
3240     }
3241 }
3242
3243 /* Expand expression EXP, which is a call to the bzero builtin.  Return 0
3244    if we failed the caller should emit a normal call.  */
3245
3246 static rtx
3247 expand_builtin_bzero (tree arglist)
3248 {
3249   tree dest, size, newarglist;
3250
3251   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3252     return NULL_RTX;
3253
3254   dest = TREE_VALUE (arglist);
3255   size = TREE_VALUE (TREE_CHAIN (arglist));
3256
3257   /* New argument list transforming bzero(ptr x, int y) to
3258      memset(ptr x, int 0, size_t y).   This is done this way
3259      so that if it isn't expanded inline, we fallback to
3260      calling bzero instead of memset.  */
3261
3262   newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3263   newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3264   newarglist = tree_cons (NULL_TREE, dest, newarglist);
3265
3266   return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
3267 }
3268
3269 /* Expand expression EXP, which is a call to the memcmp built-in function.
3270    ARGLIST is the argument list for this call.  Return 0 if we failed and the
3271    caller should emit a normal call, otherwise try to get the result in
3272    TARGET, if convenient (and in mode MODE, if that's convenient).  */
3273
3274 static rtx
3275 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3276                        enum machine_mode mode)
3277 {
3278   if (!validate_arglist (arglist,
3279                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3280     return 0;
3281   else
3282     {
3283       tree result = fold_builtin_memcmp (arglist);
3284       if (result)
3285         return expand_expr (result, target, mode, EXPAND_NORMAL);
3286     }
3287
3288 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3289   {
3290     tree arg1 = TREE_VALUE (arglist);
3291     tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3292     tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3293     rtx arg1_rtx, arg2_rtx, arg3_rtx;
3294     rtx result;
3295     rtx insn;
3296
3297     int arg1_align
3298       = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3299     int arg2_align
3300       = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3301     enum machine_mode insn_mode;
3302
3303 #ifdef HAVE_cmpmemsi
3304     if (HAVE_cmpmemsi)
3305       insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3306     else
3307 #endif
3308 #ifdef HAVE_cmpstrsi
3309     if (HAVE_cmpstrsi)
3310       insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3311     else
3312 #endif
3313       return 0;
3314
3315     /* If we don't have POINTER_TYPE, call the function.  */
3316     if (arg1_align == 0 || arg2_align == 0)
3317       return 0;
3318
3319     /* Make a place to write the result of the instruction.  */
3320     result = target;
3321     if (! (result != 0
3322            && REG_P (result) && GET_MODE (result) == insn_mode
3323            && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3324       result = gen_reg_rtx (insn_mode);
3325
3326     arg1_rtx = get_memory_rtx (arg1);
3327     arg2_rtx = get_memory_rtx (arg2);
3328     arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3329
3330     /* Set MEM_SIZE as appropriate.  */
3331     if (GET_CODE (arg3_rtx) == CONST_INT)
3332       {
3333         set_mem_size (arg1_rtx, arg3_rtx);
3334         set_mem_size (arg2_rtx, arg3_rtx);
3335       }
3336
3337 #ifdef HAVE_cmpmemsi
3338     if (HAVE_cmpmemsi)
3339       insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3340                            GEN_INT (MIN (arg1_align, arg2_align)));
3341     else
3342 #endif
3343 #ifdef HAVE_cmpstrsi
3344     if (HAVE_cmpstrsi)
3345       insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3346                            GEN_INT (MIN (arg1_align, arg2_align)));
3347     else
3348 #endif
3349       gcc_unreachable ();
3350
3351     if (insn)
3352       emit_insn (insn);
3353     else
3354       emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3355                                TYPE_MODE (integer_type_node), 3,
3356                                XEXP (arg1_rtx, 0), Pmode,
3357                                XEXP (arg2_rtx, 0), Pmode,
3358                                convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3359                                                 TYPE_UNSIGNED (sizetype)),
3360                                TYPE_MODE (sizetype));
3361
3362     /* Return the value in the proper mode for this function.  */
3363     mode = TYPE_MODE (TREE_TYPE (exp));
3364     if (GET_MODE (result) == mode)
3365       return result;
3366     else if (target != 0)
3367       {
3368         convert_move (target, result, 0);
3369         return target;
3370       }
3371     else
3372       return convert_to_mode (mode, result, 0);
3373   }
3374 #endif
3375
3376   return 0;
3377 }
3378
3379 /* Expand expression EXP, which is a call to the strcmp builtin.  Return 0
3380    if we failed the caller should emit a normal call, otherwise try to get
3381    the result in TARGET, if convenient.  */
3382
3383 static rtx
3384 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3385 {
3386   tree arglist = TREE_OPERAND (exp, 1);
3387
3388   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3389     return 0;
3390   else
3391     {
3392       tree result = fold_builtin_strcmp (arglist);
3393       if (result)
3394         return expand_expr (result, target, mode, EXPAND_NORMAL);
3395     }
3396
3397 #ifdef HAVE_cmpstrsi
3398   if (HAVE_cmpstrsi)
3399   {
3400     tree arg1 = TREE_VALUE (arglist);
3401     tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3402     tree len, len1, len2;
3403     rtx arg1_rtx, arg2_rtx, arg3_rtx;
3404     rtx result, insn;
3405     tree fndecl;
3406
3407     int arg1_align
3408       = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3409     int arg2_align
3410       = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3411     enum machine_mode insn_mode
3412       = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3413
3414     len1 = c_strlen (arg1, 1);
3415     len2 = c_strlen (arg2, 1);
3416
3417     if (len1)
3418       len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3419     if (len2)
3420       len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3421
3422     /* If we don't have a constant length for the first, use the length
3423        of the second, if we know it.  We don't require a constant for
3424        this case; some cost analysis could be done if both are available
3425        but neither is constant.  For now, assume they're equally cheap,
3426        unless one has side effects.  If both strings have constant lengths,
3427        use the smaller.  */
3428
3429     if (!len1)
3430       len = len2;
3431     else if (!len2)
3432       len = len1;
3433     else if (TREE_SIDE_EFFECTS (len1))
3434       len = len2;
3435     else if (TREE_SIDE_EFFECTS (len2))
3436       len = len1;
3437     else if (TREE_CODE (len1) != INTEGER_CST)
3438       len = len2;
3439     else if (TREE_CODE (len2) != INTEGER_CST)
3440       len = len1;
3441     else if (tree_int_cst_lt (len1, len2))
3442       len = len1;
3443     else
3444       len = len2;
3445
3446     /* If both arguments have side effects, we cannot optimize.  */
3447     if (!len || TREE_SIDE_EFFECTS (len))
3448       return 0;
3449
3450     /* If we don't have POINTER_TYPE, call the function.  */
3451     if (arg1_align == 0 || arg2_align == 0)
3452       return 0;
3453
3454     /* Make a place to write the result of the instruction.  */
3455     result = target;
3456     if (! (result != 0
3457            && REG_P (result) && GET_MODE (result) == insn_mode
3458            && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3459       result = gen_reg_rtx (insn_mode);
3460
3461     /* Stabilize the arguments in case gen_cmpstrsi fails.  */
3462     arg1 = builtin_save_expr (arg1);
3463     arg2 = builtin_save_expr (arg2);
3464
3465     arg1_rtx = get_memory_rtx (arg1);
3466     arg2_rtx = get_memory_rtx (arg2);
3467     arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3468     insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3469                          GEN_INT (MIN (arg1_align, arg2_align)));
3470     if (insn)
3471       {
3472         emit_insn (insn);
3473
3474         /* Return the value in the proper mode for this function.  */
3475         mode = TYPE_MODE (TREE_TYPE (exp));
3476         if (GET_MODE (result) == mode)
3477           return result;
3478         if (target == 0)
3479           return convert_to_mode (mode, result, 0);
3480         convert_move (target, result, 0);
3481         return target;
3482       }
3483
3484     /* Expand the library call ourselves using a stabilized argument
3485        list to avoid re-evaluating the function's arguments twice.  */
3486     arglist = build_tree_list (NULL_TREE, arg2);
3487     arglist = tree_cons (NULL_TREE, arg1, arglist);
3488     fndecl = get_callee_fndecl (exp);
3489     exp = build_function_call_expr (fndecl, arglist);
3490     return expand_call (exp, target, target == const0_rtx);
3491   }
3492 #endif
3493   return 0;
3494 }
3495
3496 /* Expand expression EXP, which is a call to the strncmp builtin.  Return 0
3497    if we failed the caller should emit a normal call, otherwise try to get
3498    the result in TARGET, if convenient.  */
3499
3500 static rtx
3501 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3502 {
3503   tree arglist = TREE_OPERAND (exp, 1);
3504
3505   if (!validate_arglist (arglist,
3506                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3507     return 0;
3508   else
3509     {
3510       tree result = fold_builtin_strncmp (arglist);
3511       if (result)
3512         return expand_expr (result, target, mode, EXPAND_NORMAL);
3513     }
3514
3515   /* If c_strlen can determine an expression for one of the string
3516      lengths, and it doesn't have side effects, then emit cmpstrsi
3517      using length MIN(strlen(string)+1, arg3).  */
3518 #ifdef HAVE_cmpstrsi
3519   if (HAVE_cmpstrsi)
3520   {
3521     tree arg1 = TREE_VALUE (arglist);
3522     tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3523     tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3524     tree len, len1, len2;
3525     rtx arg1_rtx, arg2_rtx, arg3_rtx;
3526     rtx result, insn;
3527     tree fndecl;
3528
3529     int arg1_align
3530       = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3531     int arg2_align
3532       = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3533     enum machine_mode insn_mode
3534       = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3535
3536     len1 = c_strlen (arg1, 1);
3537     len2 = c_strlen (arg2, 1);
3538
3539     if (len1)
3540       len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3541     if (len2)
3542       len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3543
3544     /* If we don't have a constant length for the first, use the length
3545        of the second, if we know it.  We don't require a constant for
3546        this case; some cost analysis could be done if both are available
3547        but neither is constant.  For now, assume they're equally cheap,
3548        unless one has side effects.  If both strings have constant lengths,
3549        use the smaller.  */
3550
3551     if (!len1)
3552       len = len2;
3553     else if (!len2)
3554       len = len1;
3555     else if (TREE_SIDE_EFFECTS (len1))
3556       len = len2;
3557     else if (TREE_SIDE_EFFECTS (len2))
3558       len = len1;
3559     else if (TREE_CODE (len1) != INTEGER_CST)
3560       len = len2;
3561     else if (TREE_CODE (len2) != INTEGER_CST)
3562       len = len1;
3563     else if (tree_int_cst_lt (len1, len2))
3564       len = len1;
3565     else
3566       len = len2;
3567
3568     /* If both arguments have side effects, we cannot optimize.  */
3569     if (!len || TREE_SIDE_EFFECTS (len))
3570       return 0;
3571
3572     /* The actual new length parameter is MIN(len,arg3).  */
3573     len = fold (build2 (MIN_EXPR, TREE_TYPE (len), len,
3574                         fold_convert (TREE_TYPE (len), arg3)));
3575
3576     /* If we don't have POINTER_TYPE, call the function.  */
3577     if (arg1_align == 0 || arg2_align == 0)
3578       return 0;
3579
3580     /* Make a place to write the result of the instruction.  */
3581     result = target;
3582     if (! (result != 0
3583            && REG_P (result) && GET_MODE (result) == insn_mode
3584            && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3585       result = gen_reg_rtx (insn_mode);
3586
3587     /* Stabilize the arguments in case gen_cmpstrsi fails.  */
3588     arg1 = builtin_save_expr (arg1);
3589     arg2 = builtin_save_expr (arg2);
3590     len = builtin_save_expr (len);
3591
3592     arg1_rtx = get_memory_rtx (arg1);
3593     arg2_rtx = get_memory_rtx (arg2);
3594     arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3595     insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3596                          GEN_INT (MIN (arg1_align, arg2_align)));
3597     if (insn)
3598       {
3599         emit_insn (insn);
3600
3601         /* Return the value in the proper mode for this function.  */
3602         mode = TYPE_MODE (TREE_TYPE (exp));
3603         if (GET_MODE (result) == mode)
3604           return result;
3605         if (target == 0)
3606           return convert_to_mode (mode, result, 0);
3607         convert_move (target, result, 0);
3608         return target;
3609       }
3610
3611     /* Expand the library call ourselves using a stabilized argument
3612        list to avoid re-evaluating the function's arguments twice.  */
3613     arglist = build_tree_list (NULL_TREE, len);
3614     arglist = tree_cons (NULL_TREE, arg2, arglist);
3615     arglist = tree_cons (NULL_TREE, arg1, arglist);
3616     fndecl = get_callee_fndecl (exp);
3617     exp = build_function_call_expr (fndecl, arglist);
3618     return expand_call (exp, target, target == const0_rtx);
3619   }
3620 #endif
3621   return 0;
3622 }
3623
3624 /* Expand expression EXP, which is a call to the strcat builtin.
3625    Return 0 if we failed the caller should emit a normal call,
3626    otherwise try to get the result in TARGET, if convenient.  */
3627
3628 static rtx
3629 expand_builtin_strcat (tree arglist, tree type, rtx target, enum machine_mode mode)
3630 {
3631   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3632     return 0;
3633   else
3634     {
3635       tree dst = TREE_VALUE (arglist),
3636         src = TREE_VALUE (TREE_CHAIN (arglist));
3637       const char *p = c_getstr (src);
3638
3639       if (p)
3640         {
3641           /* If the string length is zero, return the dst parameter.  */
3642           if (*p == '\0')
3643             return expand_expr (dst, target, mode, EXPAND_NORMAL);
3644           else if (!optimize_size)
3645             {
3646               /* Otherwise if !optimize_size, see if we can store by
3647                  pieces into (dst + strlen(dst)).  */
3648               tree newdst, arglist,
3649                 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3650
3651               /* This is the length argument.  */
3652               arglist = build_tree_list (NULL_TREE,
3653                                          fold (size_binop (PLUS_EXPR,
3654                                                            c_strlen (src, 0),
3655                                                            ssize_int (1))));
3656               /* Prepend src argument.  */
3657               arglist = tree_cons (NULL_TREE, src, arglist);
3658
3659               /* We're going to use dst more than once.  */
3660               dst = builtin_save_expr (dst);
3661
3662               /* Create strlen (dst).  */
3663               newdst =
3664                 fold (build_function_call_expr (strlen_fn,
3665                                                 build_tree_list (NULL_TREE,
3666                                                                  dst)));
3667               /* Create (dst + strlen (dst)).  */
3668               newdst = fold (build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
3669
3670               /* Prepend the new dst argument.  */
3671               arglist = tree_cons (NULL_TREE, newdst, arglist);
3672
3673               /* We don't want to get turned into a memcpy if the
3674                  target is const0_rtx, i.e. when the return value
3675                  isn't used.  That would produce pessimized code so
3676                  pass in a target of zero, it should never actually be
3677                  used.  If this was successful return the original
3678                  dst, not the result of mempcpy.  */
3679               if (expand_builtin_mempcpy (arglist, type, /*target=*/0, mode, /*endp=*/0))
3680                 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3681               else
3682                 return 0;
3683             }
3684         }
3685
3686       return 0;
3687     }
3688 }
3689
3690 /* Expand expression EXP, which is a call to the strncat builtin.
3691    Return 0 if we failed the caller should emit a normal call,
3692    otherwise try to get the result in TARGET, if convenient.  */
3693
3694 static rtx
3695 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3696 {
3697   if (validate_arglist (arglist,
3698                         POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3699     {
3700       tree result = fold_builtin_strncat (arglist);
3701       if (result)
3702         return expand_expr (result, target, mode, EXPAND_NORMAL);
3703     }
3704   return 0;
3705 }
3706
3707 /* Expand expression EXP, which is a call to the strspn builtin.
3708    Return 0 if we failed the caller should emit a normal call,
3709    otherwise try to get the result in TARGET, if convenient.  */
3710
3711 static rtx
3712 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3713 {
3714   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3715     {
3716       tree result = fold_builtin_strspn (arglist);
3717       if (result)
3718         return expand_expr (result, target, mode, EXPAND_NORMAL);
3719     }
3720   return 0;
3721 }
3722
3723 /* Expand expression EXP, which is a call to the strcspn builtin.
3724    Return 0 if we failed the caller should emit a normal call,
3725    otherwise try to get the result in TARGET, if convenient.  */
3726
3727 static rtx
3728 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3729 {
3730   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3731     {
3732       tree result = fold_builtin_strcspn (arglist);
3733       if (result)
3734         return expand_expr (result, target, mode, EXPAND_NORMAL);
3735     }
3736   return 0;
3737 }
3738
3739 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3740    if that's convenient.  */
3741
3742 rtx
3743 expand_builtin_saveregs (void)
3744 {
3745   rtx val, seq;
3746
3747   /* Don't do __builtin_saveregs more than once in a function.
3748      Save the result of the first call and reuse it.  */
3749   if (saveregs_value != 0)
3750     return saveregs_value;
3751
3752   /* When this function is called, it means that registers must be
3753      saved on entry to this function.  So we migrate the call to the
3754      first insn of this function.  */
3755
3756   start_sequence ();
3757
3758   /* Do whatever the machine needs done in this case.  */
3759   val = targetm.calls.expand_builtin_saveregs ();
3760
3761   seq = get_insns ();
3762   end_sequence ();
3763
3764   saveregs_value = val;
3765
3766   /* Put the insns after the NOTE that starts the function.  If this
3767      is inside a start_sequence, make the outer-level insn chain current, so
3768      the code is placed at the start of the function.  */
3769   push_topmost_sequence ();
3770   emit_insn_after (seq, entry_of_function ());
3771   pop_topmost_sequence ();
3772
3773   return val;
3774 }
3775
3776 /* __builtin_args_info (N) returns word N of the arg space info
3777    for the current function.  The number and meanings of words
3778    is controlled by the definition of CUMULATIVE_ARGS.  */
3779
3780 static rtx
3781 expand_builtin_args_info (tree arglist)
3782 {
3783   int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
3784   int *word_ptr = (int *) &current_function_args_info;
3785
3786   gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
3787
3788   if (arglist != 0)
3789     {
3790       if (!host_integerp (TREE_VALUE (arglist), 0))
3791         error ("argument of %<__builtin_args_info%> must be constant");
3792       else
3793         {
3794           HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
3795
3796           if (wordnum < 0 || wordnum >= nwords)
3797             error ("argument of %<__builtin_args_info%> out of range");
3798           else
3799             return GEN_INT (word_ptr[wordnum]);
3800         }
3801     }
3802   else
3803     error ("missing argument in %<__builtin_args_info%>");
3804
3805   return const0_rtx;
3806 }
3807
3808 /* Expand a call to __builtin_next_arg.  */
3809
3810 static rtx
3811 expand_builtin_next_arg (void)
3812 {
3813   /* Checking arguments is already done in fold_builtin_next_arg
3814      that must be called before this function.  */
3815   return expand_binop (Pmode, add_optab,
3816                        current_function_internal_arg_pointer,
3817                        current_function_arg_offset_rtx,
3818                        NULL_RTX, 0, OPTAB_LIB_WIDEN);
3819 }
3820
3821 /* Make it easier for the backends by protecting the valist argument
3822    from multiple evaluations.  */
3823
3824 static tree
3825 stabilize_va_list (tree valist, int needs_lvalue)
3826 {
3827   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3828     {
3829       if (TREE_SIDE_EFFECTS (valist))
3830         valist = save_expr (valist);
3831
3832       /* For this case, the backends will be expecting a pointer to
3833          TREE_TYPE (va_list_type_node), but it's possible we've
3834          actually been given an array (an actual va_list_type_node).
3835          So fix it.  */
3836       if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
3837         {
3838           tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3839           valist = build_fold_addr_expr_with_type (valist, p1);
3840         }
3841     }
3842   else
3843     {
3844       tree pt;
3845
3846       if (! needs_lvalue)
3847         {
3848           if (! TREE_SIDE_EFFECTS (valist))
3849             return valist;
3850
3851           pt = build_pointer_type (va_list_type_node);
3852           valist = fold (build1 (ADDR_EXPR, pt, valist));
3853           TREE_SIDE_EFFECTS (valist) = 1;
3854         }
3855
3856       if (TREE_SIDE_EFFECTS (valist))
3857         valist = save_expr (valist);
3858       valist = build_fold_indirect_ref (valist);
3859     }
3860
3861   return valist;
3862 }
3863
3864 /* The "standard" definition of va_list is void*.  */
3865
3866 tree
3867 std_build_builtin_va_list (void)
3868 {
3869   return ptr_type_node;
3870 }
3871
3872 /* The "standard" implementation of va_start: just assign `nextarg' to
3873    the variable.  */
3874
3875 void
3876 std_expand_builtin_va_start (tree valist, rtx nextarg)
3877 {
3878   tree t;
3879
3880   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
3881               make_tree (ptr_type_node, nextarg));
3882   TREE_SIDE_EFFECTS (t) = 1;
3883
3884   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3885 }
3886
3887 /* Expand ARGLIST, from a call to __builtin_va_start.  */
3888
3889 static rtx
3890 expand_builtin_va_start (tree arglist)
3891 {
3892   rtx nextarg;
3893   tree chain, valist;
3894
3895   chain = TREE_CHAIN (arglist);
3896
3897   if (!chain)
3898     {
3899       error ("too few arguments to function %<va_start%>");
3900       return const0_rtx;
3901     }
3902
3903   if (fold_builtin_next_arg (chain))
3904     return const0_rtx;
3905
3906   nextarg = expand_builtin_next_arg ();
3907   valist = stabilize_va_list (TREE_VALUE (arglist), 1);
3908
3909 #ifdef EXPAND_BUILTIN_VA_START
3910   EXPAND_BUILTIN_VA_START (valist, nextarg);
3911 #else
3912   std_expand_builtin_va_start (valist, nextarg);
3913 #endif
3914
3915   return const0_rtx;
3916 }
3917
3918 /* The "standard" implementation of va_arg: read the value from the
3919    current (padded) address and increment by the (padded) size.  */
3920
3921 tree
3922 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
3923 {
3924   tree addr, t, type_size, rounded_size, valist_tmp;
3925   unsigned HOST_WIDE_INT align, boundary;
3926   bool indirect;
3927
3928 #ifdef ARGS_GROW_DOWNWARD
3929   /* All of the alignment and movement below is for args-grow-up machines.
3930      As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
3931      implement their own specialized gimplify_va_arg_expr routines.  */
3932   gcc_unreachable ();
3933 #endif
3934
3935   indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
3936   if (indirect)
3937     type = build_pointer_type (type);
3938
3939   align = PARM_BOUNDARY / BITS_PER_UNIT;
3940   boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
3941
3942   /* Hoist the valist value into a temporary for the moment.  */
3943   valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
3944
3945   /* va_list pointer is aligned to PARM_BOUNDARY.  If argument actually
3946      requires greater alignment, we must perform dynamic alignment.  */
3947   if (boundary > align)
3948     {
3949       t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
3950       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
3951                   build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
3952       gimplify_and_add (t, pre_p);
3953
3954       t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
3955       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
3956                   build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
3957       gimplify_and_add (t, pre_p);
3958     }
3959   else
3960     boundary = align;
3961
3962   /* If the actual alignment is less than the alignment of the type,
3963      adjust the type accordingly so that we don't assume strict alignment
3964      when deferencing the pointer.  */
3965   boundary *= BITS_PER_UNIT;
3966   if (boundary < TYPE_ALIGN (type))
3967     {
3968       type = build_variant_type_copy (type);
3969       TYPE_ALIGN (type) = boundary;
3970     }
3971
3972   /* Compute the rounded size of the type.  */
3973   type_size = size_in_bytes (type);
3974   rounded_size = round_up (type_size, align);
3975
3976   /* Reduce rounded_size so it's sharable with the postqueue.  */
3977   gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
3978
3979   /* Get AP.  */
3980   addr = valist_tmp;
3981   if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
3982     {
3983       /* Small args are padded downward.  */
3984       t = fold (build2 (GT_EXPR, sizetype, rounded_size, size_int (align)));
3985       t = fold (build3 (COND_EXPR, sizetype, t, size_zero_node,
3986                         size_binop (MINUS_EXPR, rounded_size, type_size)));
3987       t = fold_convert (TREE_TYPE (addr), t);
3988       addr = fold (build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t));
3989     }
3990
3991   /* Compute new value for AP.  */
3992   t = fold_convert (TREE_TYPE (valist), rounded_size);
3993   t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
3994   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
3995   gimplify_and_add (t, pre_p);
3996
3997   addr = fold_convert (build_pointer_type (type), addr);
3998
3999   if (indirect)
4000     addr = build_va_arg_indirect_ref (addr);
4001
4002   return build_va_arg_indirect_ref (addr);
4003 }
4004
4005 /* Build an indirect-ref expression over the given TREE, which represents a
4006    piece of a va_arg() expansion.  */
4007 tree
4008 build_va_arg_indirect_ref (tree addr)
4009 {
4010   addr = build_fold_indirect_ref (addr);
4011
4012   if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF.  */
4013     mf_mark (addr);
4014
4015   return addr;
4016 }
4017
4018 /* Return a dummy expression of type TYPE in order to keep going after an
4019    error.  */
4020
4021 static tree
4022 dummy_object (tree type)
4023 {
4024   tree t = convert (build_pointer_type (type), null_pointer_node);
4025   return build1 (INDIRECT_REF, type, t);
4026 }
4027
4028 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4029    builtin function, but a very special sort of operator.  */
4030
4031 enum gimplify_status
4032 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4033 {
4034   tree promoted_type, want_va_type, have_va_type;
4035   tree valist = TREE_OPERAND (*expr_p, 0);
4036   tree type = TREE_TYPE (*expr_p);
4037   tree t;
4038
4039   /* Verify that valist is of the proper type.  */
4040   want_va_type = va_list_type_node;
4041   have_va_type = TREE_TYPE (valist);
4042
4043   if (have_va_type == error_mark_node)
4044     return GS_ERROR;
4045
4046   if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4047     {
4048       /* If va_list is an array type, the argument may have decayed
4049          to a pointer type, e.g. by being passed to another function.
4050          In that case, unwrap both types so that we can compare the
4051          underlying records.  */
4052       if (TREE_CODE (have_va_type) == ARRAY_TYPE
4053           || POINTER_TYPE_P (have_va_type))
4054         {
4055           want_va_type = TREE_TYPE (want_va_type);
4056           have_va_type = TREE_TYPE (have_va_type);
4057         }
4058     }
4059
4060   if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4061     {
4062       error ("first argument to %<va_arg%> not of type %<va_list%>");
4063       return GS_ERROR;
4064     }
4065
4066   /* Generate a diagnostic for requesting data of a type that cannot
4067      be passed through `...' due to type promotion at the call site.  */
4068   else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4069            != type)
4070     {
4071       static bool gave_help;
4072
4073       /* Unfortunately, this is merely undefined, rather than a constraint
4074          violation, so we cannot make this an error.  If this call is never
4075          executed, the program is still strictly conforming.  */
4076       warning ("%qT is promoted to %qT when passed through %<...%>",
4077                type, promoted_type);
4078       if (! gave_help)
4079         {
4080           gave_help = true;
4081           warning ("(so you should pass %qT not %qT to %<va_arg%>)",
4082                    promoted_type, type);
4083         }
4084
4085       /* We can, however, treat "undefined" any way we please.
4086          Call abort to encourage the user to fix the program.  */
4087       inform ("if this code is reached, the program will abort");
4088       t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4089                                     NULL);
4090       append_to_statement_list (t, pre_p);
4091
4092       /* This is dead code, but go ahead and finish so that the
4093          mode of the result comes out right.  */
4094       *expr_p = dummy_object (type);
4095       return GS_ALL_DONE;
4096     }
4097   else
4098     {
4099       /* Make it easier for the backends by protecting the valist argument
4100          from multiple evaluations.  */
4101       if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4102         {
4103           /* For this case, the backends will be expecting a pointer to
4104              TREE_TYPE (va_list_type_node), but it's possible we've
4105              actually been given an array (an actual va_list_type_node).
4106              So fix it.  */
4107           if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4108             {
4109               tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4110               valist = build_fold_addr_expr_with_type (valist, p1);
4111             }
4112           gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4113         }
4114       else
4115         gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4116
4117       if (!targetm.gimplify_va_arg_expr)
4118         /* Once most targets are converted this should abort.  */
4119         return GS_ALL_DONE;
4120
4121       *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4122       return GS_OK;
4123     }
4124 }
4125
4126 /* Expand ARGLIST, from a call to __builtin_va_end.  */
4127
4128 static rtx
4129 expand_builtin_va_end (tree arglist)
4130 {
4131   tree valist = TREE_VALUE (arglist);
4132
4133   /* Evaluate for side effects, if needed.  I hate macros that don't
4134      do that.  */
4135   if (TREE_SIDE_EFFECTS (valist))
4136     expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4137
4138   return const0_rtx;
4139 }
4140
4141 /* Expand ARGLIST, from a call to __builtin_va_copy.  We do this as a
4142    builtin rather than just as an assignment in stdarg.h because of the
4143    nastiness of array-type va_list types.  */
4144
4145 static rtx
4146 expand_builtin_va_copy (tree arglist)
4147 {
4148   tree dst, src, t;
4149
4150   dst = TREE_VALUE (arglist);
4151   src = TREE_VALUE (TREE_CHAIN (arglist));
4152
4153   dst = stabilize_va_list (dst, 1);
4154   src = stabilize_va_list (src, 0);
4155
4156   if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4157     {
4158       t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4159       TREE_SIDE_EFFECTS (t) = 1;
4160       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4161     }
4162   else
4163     {
4164       rtx dstb, srcb, size;
4165
4166       /* Evaluate to pointers.  */
4167       dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4168       srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4169       size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4170                           VOIDmode, EXPAND_NORMAL);
4171
4172       dstb = convert_memory_address (Pmode, dstb);
4173       srcb = convert_memory_address (Pmode, srcb);
4174
4175       /* "Dereference" to BLKmode memories.  */
4176       dstb = gen_rtx_MEM (BLKmode, dstb);
4177       set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4178       set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4179       srcb = gen_rtx_MEM (BLKmode, srcb);
4180       set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4181       set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4182
4183       /* Copy.  */
4184       emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4185     }
4186
4187   return const0_rtx;
4188 }
4189
4190 /* Expand a call to one of the builtin functions __builtin_frame_address or
4191    __builtin_return_address.  */
4192
4193 static rtx
4194 expand_builtin_frame_address (tree fndecl, tree arglist)
4195 {
4196   /* The argument must be a nonnegative integer constant.
4197      It counts the number of frames to scan up the stack.
4198      The value is the return address saved in that frame.  */
4199   if (arglist == 0)
4200     /* Warning about missing arg was already issued.  */
4201     return const0_rtx;
4202   else if (! host_integerp (TREE_VALUE (arglist), 1))
4203     {
4204       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4205         error ("invalid argument to %<__builtin_frame_address%>");
4206       else
4207         error ("invalid argument to %<__builtin_return_address%>");
4208       return const0_rtx;
4209     }
4210   else
4211     {
4212       rtx tem
4213         = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4214                                       tree_low_cst (TREE_VALUE (arglist), 1),
4215                                       hard_frame_pointer_rtx);
4216
4217       /* Some ports cannot access arbitrary stack frames.  */
4218       if (tem == NULL)
4219         {
4220           if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4221             warning ("unsupported argument to %<__builtin_frame_address%>");
4222           else
4223             warning ("unsupported argument to %<__builtin_return_address%>");
4224           return const0_rtx;
4225         }
4226
4227       /* For __builtin_frame_address, return what we've got.  */
4228       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4229         return tem;
4230
4231       if (!REG_P (tem)
4232           && ! CONSTANT_P (tem))
4233         tem = copy_to_mode_reg (Pmode, tem);
4234       return tem;
4235     }
4236 }
4237
4238 /* Expand a call to the alloca builtin, with arguments ARGLIST.  Return 0 if
4239    we failed and the caller should emit a normal call, otherwise try to get
4240    the result in TARGET, if convenient.  */
4241
4242 static rtx
4243 expand_builtin_alloca (tree arglist, rtx target)
4244 {
4245   rtx op0;
4246   rtx result;
4247
4248   /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4249      should always expand to function calls.  These can be intercepted
4250      in libmudflap.  */
4251   if (flag_mudflap)
4252     return 0;
4253
4254   if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4255     return 0;
4256
4257   /* Compute the argument.  */
4258   op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4259
4260   /* Allocate the desired space.  */
4261   result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4262   result = convert_memory_address (ptr_mode, result);
4263
4264   return result;
4265 }
4266
4267 /* Expand a call to a unary builtin.  The arguments are in ARGLIST.
4268    Return 0 if a normal call should be emitted rather than expanding the
4269    function in-line.  If convenient, the result should be placed in TARGET.
4270    SUBTARGET may be used as the target for computing one of EXP's operands.  */
4271
4272 static rtx
4273 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4274                      rtx subtarget, optab op_optab)
4275 {
4276   rtx op0;
4277   if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4278     return 0;
4279
4280   /* Compute the argument.  */
4281   op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4282   /* Compute op, into TARGET if possible.
4283      Set TARGET to wherever the result comes back.  */
4284   target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4285                         op_optab, op0, target, 1);
4286   gcc_assert (target);
4287
4288   return convert_to_mode (target_mode, target, 0);
4289 }
4290
4291 /* If the string passed to fputs is a constant and is one character
4292    long, we attempt to transform this call into __builtin_fputc().  */
4293
4294 static rtx
4295 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4296 {
4297   /* Verify the arguments in the original call.  */
4298   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4299     {
4300       tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4301                                         unlocked, NULL_TREE);
4302       if (result)
4303         return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4304     }
4305   return 0;
4306 }
4307
4308 /* Expand a call to __builtin_expect.  We return our argument and emit a
4309    NOTE_INSN_EXPECTED_VALUE note.  This is the expansion of __builtin_expect in
4310    a non-jump context.  */
4311
4312 static rtx
4313 expand_builtin_expect (tree arglist, rtx target)
4314 {
4315   tree exp, c;
4316   rtx note, rtx_c;
4317
4318   if (arglist == NULL_TREE
4319       || TREE_CHAIN (arglist) == NULL_TREE)
4320     return const0_rtx;
4321   exp = TREE_VALUE (arglist);
4322   c = TREE_VALUE (TREE_CHAIN (arglist));
4323
4324   if (TREE_CODE (c) != INTEGER_CST)
4325     {
4326       error ("second argument to %<__builtin_expect%> must be a constant");
4327       c = integer_zero_node;
4328     }
4329
4330   target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4331
4332   /* Don't bother with expected value notes for integral constants.  */
4333   if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4334     {
4335       /* We do need to force this into a register so that we can be
4336          moderately sure to be able to correctly interpret the branch
4337          condition later.  */
4338       target = force_reg (GET_MODE (target), target);
4339
4340       rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4341
4342       note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4343       NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4344     }
4345
4346   return target;
4347 }
4348
4349 /* Like expand_builtin_expect, except do this in a jump context.  This is
4350    called from do_jump if the conditional is a __builtin_expect.  Return either
4351    a list of insns to emit the jump or NULL if we cannot optimize
4352    __builtin_expect.  We need to optimize this at jump time so that machines
4353    like the PowerPC don't turn the test into a SCC operation, and then jump
4354    based on the test being 0/1.  */
4355
4356 rtx
4357 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4358 {
4359   tree arglist = TREE_OPERAND (exp, 1);
4360   tree arg0 = TREE_VALUE (arglist);
4361   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4362   rtx ret = NULL_RTX;
4363
4364   /* Only handle __builtin_expect (test, 0) and
4365      __builtin_expect (test, 1).  */
4366   if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4367       && (integer_zerop (arg1) || integer_onep (arg1)))
4368     {
4369       rtx insn, drop_through_label, temp;
4370
4371       /* Expand the jump insns.  */
4372       start_sequence ();
4373       do_jump (arg0, if_false_label, if_true_label);
4374       ret = get_insns ();
4375
4376       drop_through_label = get_last_insn ();
4377       if (drop_through_label && NOTE_P (drop_through_label))
4378         drop_through_label = prev_nonnote_insn (drop_through_label);
4379       if (drop_through_label && !LABEL_P (drop_through_label))
4380         drop_through_label = NULL_RTX;
4381       end_sequence ();
4382
4383       if (! if_true_label)
4384         if_true_label = drop_through_label;
4385       if (! if_false_label)
4386         if_false_label = drop_through_label;
4387
4388       /* Go through and add the expect's to each of the conditional jumps.  */
4389       insn = ret;
4390       while (insn != NULL_RTX)
4391         {
4392           rtx next = NEXT_INSN (insn);
4393
4394           if (JUMP_P (insn) && any_condjump_p (insn))
4395             {
4396               rtx ifelse = SET_SRC (pc_set (insn));
4397               rtx then_dest = XEXP (ifelse, 1);
4398               rtx else_dest = XEXP (ifelse, 2);
4399               int taken = -1;
4400
4401               /* First check if we recognize any of the labels.  */
4402               if (GET_CODE (then_dest) == LABEL_REF
4403                   && XEXP (then_dest, 0) == if_true_label)
4404                 taken = 1;
4405               else if (GET_CODE (then_dest) == LABEL_REF
4406                        && XEXP (then_dest, 0) == if_false_label)
4407                 taken = 0;
4408               else if (GET_CODE (else_dest) == LABEL_REF
4409                        && XEXP (else_dest, 0) == if_false_label)
4410                 taken = 1;
4411               else if (GET_CODE (else_dest) == LABEL_REF
4412                        && XEXP (else_dest, 0) == if_true_label)
4413                 taken = 0;
4414               /* Otherwise check where we drop through.  */
4415               else if (else_dest == pc_rtx)
4416                 {
4417                   if (next && NOTE_P (next))
4418                     next = next_nonnote_insn (next);
4419
4420                   if (next && JUMP_P (next)
4421                       && any_uncondjump_p (next))
4422                     temp = XEXP (SET_SRC (pc_set (next)), 0);
4423                   else
4424                     temp = next;
4425
4426                   /* TEMP is either a CODE_LABEL, NULL_RTX or something
4427                      else that can't possibly match either target label.  */
4428                   if (temp == if_false_label)
4429                     taken = 1;
4430                   else if (temp == if_true_label)
4431                     taken = 0;
4432                 }
4433               else if (then_dest == pc_rtx)
4434                 {
4435                   if (next && NOTE_P (next))
4436                     next = next_nonnote_insn (next);
4437
4438                   if (next && JUMP_P (next)
4439                       && any_uncondjump_p (next))
4440                     temp = XEXP (SET_SRC (pc_set (next)), 0);
4441                   else
4442                     temp = next;
4443
4444                   if (temp == if_false_label)
4445                     taken = 0;
4446                   else if (temp == if_true_label)
4447                     taken = 1;
4448                 }
4449
4450               if (taken != -1)
4451                 {
4452                   /* If the test is expected to fail, reverse the
4453                      probabilities.  */
4454                   if (integer_zerop (arg1))
4455                     taken = 1 - taken;
4456                   predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4457                 }
4458             }
4459
4460           insn = next;
4461         }
4462     }
4463
4464   return ret;
4465 }
4466
4467 static void
4468 expand_builtin_trap (void)
4469 {
4470 #ifdef HAVE_trap
4471   if (HAVE_trap)
4472     emit_insn (gen_trap ());
4473   else
4474 #endif
4475     emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4476   emit_barrier ();
4477 }
4478
4479 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4480    Return 0 if a normal call should be emitted rather than expanding
4481    the function inline.  If convenient, the result should be placed
4482    in TARGET.  SUBTARGET may be used as the target for computing
4483    the operand.  */
4484
4485 static rtx
4486 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4487 {
4488   enum machine_mode mode;
4489   tree arg;
4490   rtx op0;
4491
4492   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4493     return 0;
4494
4495   arg = TREE_VALUE (arglist);
4496   mode = TYPE_MODE (TREE_TYPE (arg));
4497   op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4498   return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4499 }
4500
4501 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4502    Return NULL is a normal call should be emitted rather than expanding the
4503    function inline.  If convenient, the result should be placed in TARGET.
4504    SUBTARGET may be used as the target for computing the operand.  */
4505
4506 static rtx
4507 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4508 {
4509   rtx op0, op1;
4510   tree arg;
4511
4512   if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4513     return 0;
4514
4515   arg = TREE_VALUE (arglist);
4516   op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4517
4518   arg = TREE_VALUE (TREE_CHAIN (arglist));
4519   op1 = expand_expr (arg, NULL, VOIDmode, 0);
4520
4521   return expand_copysign (op0, op1, target);
4522 }
4523
4524 /* Create a new constant string literal and return a char* pointer to it.
4525    The STRING_CST value is the LEN characters at STR.  */
4526 static tree
4527 build_string_literal (int len, const char *str)
4528 {
4529   tree t, elem, index, type;
4530
4531   t = build_string (len, str);
4532   elem = build_type_variant (char_type_node, 1, 0);
4533   index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4534   type = build_array_type (elem, index);
4535   TREE_TYPE (t) = type;
4536   TREE_CONSTANT (t) = 1;
4537   TREE_INVARIANT (t) = 1;
4538   TREE_READONLY (t) = 1;
4539   TREE_STATIC (t) = 1;
4540
4541   type = build_pointer_type (type);
4542   t = build1 (ADDR_EXPR, type, t);
4543
4544   type = build_pointer_type (elem);
4545   t = build1 (NOP_EXPR, type, t);
4546   return t;
4547 }
4548
4549 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4550    Return 0 if a normal call should be emitted rather than transforming
4551    the function inline.  If convenient, the result should be placed in
4552    TARGET with mode MODE.  UNLOCKED indicates this is a printf_unlocked
4553    call.  */
4554 static rtx
4555 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4556                        bool unlocked)
4557 {
4558   tree fn_putchar = unlocked
4559                     ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4560                     : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4561   tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4562                           : implicit_built_in_decls[BUILT_IN_PUTS];
4563   const char *fmt_str;
4564   tree fn, fmt, arg;
4565
4566   /* If the return value is used, don't do the transformation.  */
4567   if (target != const0_rtx)
4568     return 0;
4569
4570   /* Verify the required arguments in the original call.  */
4571   if (! arglist)
4572     return 0;
4573   fmt = TREE_VALUE (arglist);
4574   if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4575     return 0;
4576   arglist = TREE_CHAIN (arglist);
4577
4578   /* Check whether the format is a literal string constant.  */
4579   fmt_str = c_getstr (fmt);
4580   if (fmt_str == NULL)
4581     return 0;
4582
4583   /* If the format specifier was "%s\n", call __builtin_puts(arg).  */
4584   if (strcmp (fmt_str, "%s\n") == 0)
4585     {
4586       if (! arglist
4587           || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4588           || TREE_CHAIN (arglist))
4589         return 0;
4590       fn = fn_puts;
4591     }
4592   /* If the format specifier was "%c", call __builtin_putchar(arg).  */
4593   else if (strcmp (fmt_str, "%c") == 0)
4594     {
4595       if (! arglist
4596           || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4597           || TREE_CHAIN (arglist))
4598         return 0;
4599       fn = fn_putchar;
4600     }
4601   else
4602     {
4603       /* We can't handle anything else with % args or %% ... yet.  */
4604       if (strchr (fmt_str, '%'))
4605         return 0;
4606
4607       if (arglist)
4608         return 0;
4609
4610       /* If the format specifier was "", printf does nothing.  */
4611       if (fmt_str[0] == '\0')
4612         return const0_rtx;
4613       /* If the format specifier has length of 1, call putchar.  */
4614       if (fmt_str[1] == '\0')
4615         {
4616           /* Given printf("c"), (where c is any one character,)
4617              convert "c"[0] to an int and pass that to the replacement
4618              function.  */
4619           arg = build_int_cst (NULL_TREE, fmt_str[0]);
4620           arglist = build_tree_list (NULL_TREE, arg);
4621           fn = fn_putchar;
4622         }
4623       else
4624         {
4625           /* If the format specifier was "string\n", call puts("string").  */
4626           size_t len = strlen (fmt_str);
4627           if (fmt_str[len - 1] == '\n')
4628             {
4629               /* Create a NUL-terminated string that's one char shorter
4630                  than the original, stripping off the trailing '\n'.  */
4631               char *newstr = alloca (len);
4632               memcpy (newstr, fmt_str, len - 1);
4633               newstr[len - 1] = 0;
4634
4635               arg = build_string_literal (len, newstr);
4636               arglist = build_tree_list (NULL_TREE, arg);
4637               fn = fn_puts;
4638             }
4639           else
4640             /* We'd like to arrange to call fputs(string,stdout) here,
4641                but we need stdout and don't have a way to get it yet.  */
4642             return 0;
4643         }
4644     }
4645
4646   if (!fn)
4647     return 0;
4648   return expand_expr (build_function_call_expr (fn, arglist),
4649                       target, mode, EXPAND_NORMAL);
4650 }
4651
4652 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4653    Return 0 if a normal call should be emitted rather than transforming
4654    the function inline.  If convenient, the result should be placed in
4655    TARGET with mode MODE.  UNLOCKED indicates this is a fprintf_unlocked
4656    call.  */
4657 static rtx
4658 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
4659                         bool unlocked)
4660 {
4661   tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4662                            : implicit_built_in_decls[BUILT_IN_FPUTC];
4663   tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4664                            : implicit_built_in_decls[BUILT_IN_FPUTS];
4665   const char *fmt_str;
4666   tree fn, fmt, fp, arg;
4667
4668   /* If the return value is used, don't do the transformation.  */
4669   if (target != const0_rtx)
4670     return 0;
4671
4672   /* Verify the required arguments in the original call.  */
4673   if (! arglist)
4674     return 0;
4675   fp = TREE_VALUE (arglist);
4676   if (! POINTER_TYPE_P (TREE_TYPE (fp)))
4677     return 0;
4678   arglist = TREE_CHAIN (arglist);
4679   if (! arglist)
4680     return 0;
4681   fmt = TREE_VALUE (arglist);
4682   if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4683     return 0;
4684   arglist = TREE_CHAIN (arglist);
4685
4686   /* Check whether the format is a literal string constant.  */
4687   fmt_str = c_getstr (fmt);
4688   if (fmt_str == NULL)
4689     return 0;
4690
4691   /* If the format specifier was "%s", call __builtin_fputs(arg,fp).  */
4692   if (strcmp (fmt_str, "%s") == 0)
4693     {
4694       if (! arglist
4695           || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4696           || TREE_CHAIN (arglist))
4697         return 0;
4698       arg = TREE_VALUE (arglist);
4699       arglist = build_tree_list (NULL_TREE, fp);
4700       arglist = tree_cons (NULL_TREE, arg, arglist);
4701       fn = fn_fputs;
4702     }
4703   /* If the format specifier was "%c", call __builtin_fputc(arg,fp).  */
4704   else if (strcmp (fmt_str, "%c") == 0)
4705     {
4706       if (! arglist
4707           || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4708           || TREE_CHAIN (arglist))
4709         return 0;
4710       arg = TREE_VALUE (arglist);
4711       arglist = build_tree_list (NULL_TREE, fp);
4712       arglist = tree_cons (NULL_TREE, arg, arglist);
4713       fn = fn_fputc;
4714     }
4715   else
4716     {
4717       /* We can't handle anything else with % args or %% ... yet.  */
4718       if (strchr (fmt_str, '%'))
4719         return 0;
4720
4721       if (arglist)
4722         return 0;
4723
4724       /* If the format specifier was "", fprintf does nothing.  */
4725       if (fmt_str[0] == '\0')
4726         {
4727           /* Evaluate and ignore FILE* argument for side-effects.  */
4728           expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4729           return const0_rtx;
4730         }
4731
4732       /* When "string" doesn't contain %, replace all cases of
4733          fprintf(stream,string) with fputs(string,stream).  The fputs
4734          builtin will take care of special cases like length == 1.  */
4735       arglist = build_tree_list (NULL_TREE, fp);
4736       arglist = tree_cons (NULL_TREE, fmt, arglist);
4737       fn = fn_fputs;
4738     }
4739
4740   if (!fn)
4741     return 0;
4742   return expand_expr (build_function_call_expr (fn, arglist),
4743                       target, mode, EXPAND_NORMAL);
4744 }
4745
4746 /* Expand a call to sprintf with argument list ARGLIST.  Return 0 if
4747    a normal call should be emitted rather than expanding the function
4748    inline.  If convenient, the result should be placed in TARGET with
4749    mode MODE.  */
4750
4751 static rtx
4752 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
4753 {
4754   tree orig_arglist, dest, fmt;
4755   const char *fmt_str;
4756
4757   orig_arglist = arglist;
4758
4759   /* Verify the required arguments in the original call.  */
4760   if (! arglist)
4761     return 0;
4762   dest = TREE_VALUE (arglist);
4763   if (! POINTER_TYPE_P (TREE_TYPE (dest)))
4764     return 0;
4765   arglist = TREE_CHAIN (arglist);
4766   if (! arglist)
4767     return 0;
4768   fmt = TREE_VALUE (arglist);
4769   if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4770     return 0;
4771   arglist = TREE_CHAIN (arglist);
4772
4773   /* Check whether the format is a literal string constant.  */
4774   fmt_str = c_getstr (fmt);
4775   if (fmt_str == NULL)
4776     return 0;
4777
4778   /* If the format doesn't contain % args or %%, use strcpy.  */
4779   if (strchr (fmt_str, '%') == 0)
4780     {
4781       tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4782       tree exp;
4783
4784       if (arglist || ! fn)
4785         return 0;
4786       expand_expr (build_function_call_expr (fn, orig_arglist),
4787                    const0_rtx, VOIDmode, EXPAND_NORMAL);
4788       if (target == const0_rtx)
4789         return const0_rtx;
4790       exp = build_int_cst (NULL_TREE, strlen (fmt_str));
4791       return expand_expr (exp, target, mode, EXPAND_NORMAL);
4792     }
4793   /* If the format is "%s", use strcpy if the result isn't used.  */
4794   else if (strcmp (fmt_str, "%s") == 0)
4795     {
4796       tree fn, arg, len;
4797       fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4798
4799       if (! fn)
4800         return 0;
4801
4802       if (! arglist || TREE_CHAIN (arglist))
4803         return 0;
4804       arg = TREE_VALUE (arglist);
4805       if (! POINTER_TYPE_P (TREE_TYPE (arg)))
4806         return 0;
4807
4808       if (target != const0_rtx)
4809         {
4810           len = c_strlen (arg, 1);
4811           if (! len || TREE_CODE (len) != INTEGER_CST)
4812             return 0;
4813         }
4814       else
4815         len = NULL_TREE;
4816
4817       arglist = build_tree_list (NULL_TREE, arg);
4818       arglist = tree_cons (NULL_TREE, dest, arglist);
4819       expand_expr (build_function_call_expr (fn, arglist),
4820                    const0_rtx, VOIDmode, EXPAND_NORMAL);
4821
4822       if (target == const0_rtx)
4823         return const0_rtx;
4824       return expand_expr (len, target, mode, EXPAND_NORMAL);
4825     }
4826
4827   return 0;
4828 }
4829
4830 /* Expand a call to either the entry or exit function profiler.  */
4831
4832 static rtx
4833 expand_builtin_profile_func (bool exitp)
4834 {
4835   rtx this, which;
4836
4837   this = DECL_RTL (current_function_decl);
4838   gcc_assert (MEM_P (this));
4839   this = XEXP (this, 0);
4840
4841   if (exitp)
4842     which = profile_function_exit_libfunc;
4843   else
4844     which = profile_function_entry_libfunc;
4845
4846   emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
4847                      expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
4848                                                  0, hard_frame_pointer_rtx),
4849                      Pmode);
4850
4851   return const0_rtx;
4852 }
4853
4854 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT.  */
4855
4856 static rtx
4857 round_trampoline_addr (rtx tramp)
4858 {
4859   rtx temp, addend, mask;
4860
4861   /* If we don't need too much alignment, we'll have been guaranteed
4862      proper alignment by get_trampoline_type.  */
4863   if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4864     return tramp;
4865
4866   /* Round address up to desired boundary.  */
4867   temp = gen_reg_rtx (Pmode);
4868   addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4869   mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4870
4871   temp  = expand_simple_binop (Pmode, PLUS, tramp, addend,
4872                                temp, 0, OPTAB_LIB_WIDEN);
4873   tramp = expand_simple_binop (Pmode, AND, temp, mask,
4874                                temp, 0, OPTAB_LIB_WIDEN);
4875
4876   return tramp;
4877 }
4878
4879 static rtx
4880 expand_builtin_init_trampoline (tree arglist)
4881 {
4882   tree t_tramp, t_func, t_chain;
4883   rtx r_tramp, r_func, r_chain;
4884 #ifdef TRAMPOLINE_TEMPLATE
4885   rtx blktramp;
4886 #endif
4887
4888   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
4889                          POINTER_TYPE, VOID_TYPE))
4890     return NULL_RTX;
4891
4892   t_tramp = TREE_VALUE (arglist);
4893   arglist = TREE_CHAIN (arglist);
4894   t_func = TREE_VALUE (arglist);
4895   arglist = TREE_CHAIN (arglist);
4896   t_chain = TREE_VALUE (arglist);
4897
4898   r_tramp = expand_expr (t_tramp, NULL_RTX, VOIDmode, 0);
4899   r_func = expand_expr (t_func, NULL_RTX, VOIDmode, 0);
4900   r_chain = expand_expr (t_chain, NULL_RTX, VOIDmode, 0);
4901
4902   /* Generate insns to initialize the trampoline.  */
4903   r_tramp = round_trampoline_addr (r_tramp);
4904 #ifdef TRAMPOLINE_TEMPLATE
4905   blktramp = gen_rtx_MEM (BLKmode, r_tramp);
4906   set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
4907   emit_block_move (blktramp, assemble_trampoline_template (),
4908                    GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
4909 #endif
4910   trampolines_created = 1;
4911   INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
4912
4913   return const0_rtx;
4914 }
4915
4916 static rtx
4917 expand_builtin_adjust_trampoline (tree arglist)
4918 {
4919   rtx tramp;
4920
4921   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
4922     return NULL_RTX;
4923
4924   tramp = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4925   tramp = round_trampoline_addr (tramp);
4926 #ifdef TRAMPOLINE_ADJUST_ADDRESS
4927   TRAMPOLINE_ADJUST_ADDRESS (tramp);
4928 #endif
4929
4930   return tramp;
4931 }
4932
4933 /* Expand a call to the built-in signbit, signbitf or signbitl function.
4934    Return NULL_RTX if a normal call should be emitted rather than expanding
4935    the function in-line.  EXP is the expression that is a call to the builtin
4936    function; if convenient, the result should be placed in TARGET.  */
4937
4938 static rtx
4939 expand_builtin_signbit (tree exp, rtx target)
4940 {
4941   const struct real_format *fmt;
4942   enum machine_mode fmode, imode, rmode;
4943   HOST_WIDE_INT hi, lo;
4944   tree arg, arglist;
4945   int bitpos;
4946   rtx temp;
4947
4948   arglist = TREE_OPERAND (exp, 1);
4949   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4950     return 0;
4951
4952   arg = TREE_VALUE (arglist);
4953   fmode = TYPE_MODE (TREE_TYPE (arg));
4954   rmode = TYPE_MODE (TREE_TYPE (exp));
4955   fmt = REAL_MODE_FORMAT (fmode);
4956
4957   /* For floating point formats without a sign bit, implement signbit
4958      as "ARG < 0.0".  */
4959   if (fmt->signbit < 0)
4960   {
4961     /* But we can't do this if the format supports signed zero.  */
4962     if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4963       return 0;
4964
4965     arg = fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
4966                         build_real (TREE_TYPE (arg), dconst0)));
4967     return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4968   }
4969
4970   imode = int_mode_for_mode (fmode);
4971   if (imode == BLKmode)
4972     return 0;
4973
4974   bitpos = fmt->signbit;
4975   /* Handle targets with different FP word orders.  */
4976   if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4977     {
4978       int nwords = GET_MODE_BITSIZE (fmode) / BITS_PER_WORD;
4979       int word = nwords - (bitpos / BITS_PER_WORD) - 1;
4980       bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
4981     }
4982
4983   /* If the sign bit is not in the lowpart and the floating point format
4984      is wider than an integer, check that is twice the size of an integer
4985      so that we can use gen_highpart below.  */
4986   if (bitpos >= GET_MODE_BITSIZE (rmode)
4987       && GET_MODE_BITSIZE (imode) != 2 * GET_MODE_BITSIZE (rmode))
4988     return 0;
4989
4990   temp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
4991   temp = gen_lowpart (imode, temp);
4992
4993   if (GET_MODE_BITSIZE (imode) > GET_MODE_BITSIZE (rmode))
4994     {
4995       if (BYTES_BIG_ENDIAN)
4996         bitpos = GET_MODE_BITSIZE (imode) - 1 - bitpos;
4997       temp = copy_to_mode_reg (imode, temp);
4998       temp = extract_bit_field (temp, 1, bitpos, 1,
4999                                 NULL_RTX, rmode, rmode);
5000     }
5001   else
5002     {
5003       if (GET_MODE_BITSIZE (imode) < GET_MODE_BITSIZE (rmode))
5004         temp = gen_lowpart (rmode, temp);
5005       if (bitpos < HOST_BITS_PER_WIDE_INT)
5006         {
5007           hi = 0;
5008           lo = (HOST_WIDE_INT) 1 << bitpos;
5009         }
5010       else
5011         {
5012           hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5013           lo = 0;
5014         }
5015
5016       temp = force_reg (rmode, temp);
5017       temp = expand_binop (rmode, and_optab, temp,
5018                            immed_double_const (lo, hi, rmode),
5019                            target, 1, OPTAB_LIB_WIDEN);
5020     }
5021   return temp;
5022 }
5023
5024 /* Expand fork or exec calls.  TARGET is the desired target of the
5025    call.  ARGLIST is the list of arguments of the call.  FN is the
5026    identificator of the actual function.  IGNORE is nonzero if the
5027    value is to be ignored.  */
5028
5029 static rtx
5030 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5031 {
5032   tree id, decl;
5033   tree call;
5034
5035   /* If we are not profiling, just call the function.  */
5036   if (!profile_arc_flag)
5037     return NULL_RTX;
5038
5039   /* Otherwise call the wrapper.  This should be equivalent for the rest of
5040      compiler, so the code does not diverge, and the wrapper may run the
5041      code necessary for keeping the profiling sane.  */
5042
5043   switch (DECL_FUNCTION_CODE (fn))
5044     {
5045     case BUILT_IN_FORK:
5046       id = get_identifier ("__gcov_fork");
5047       break;
5048
5049     case BUILT_IN_EXECL:
5050       id = get_identifier ("__gcov_execl");
5051       break;
5052
5053     case BUILT_IN_EXECV:
5054       id = get_identifier ("__gcov_execv");
5055       break;
5056
5057     case BUILT_IN_EXECLP:
5058       id = get_identifier ("__gcov_execlp");
5059       break;
5060
5061     case BUILT_IN_EXECLE:
5062       id = get_identifier ("__gcov_execle");
5063       break;
5064
5065     case BUILT_IN_EXECVP:
5066       id = get_identifier ("__gcov_execvp");
5067       break;
5068
5069     case BUILT_IN_EXECVE:
5070       id = get_identifier ("__gcov_execve");
5071       break;
5072
5073     default:
5074       gcc_unreachable ();
5075     }
5076
5077   decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5078   DECL_EXTERNAL (decl) = 1;
5079   TREE_PUBLIC (decl) = 1;
5080   DECL_ARTIFICIAL (decl) = 1;
5081   TREE_NOTHROW (decl) = 1;
5082   call = build_function_call_expr (decl, arglist);
5083
5084   return expand_call (call, target, ignore);
5085 }
5086 \f
5087 /* Expand an expression EXP that calls a built-in function,
5088    with result going to TARGET if that's convenient
5089    (and in mode MODE if that's convenient).
5090    SUBTARGET may be used as the target for computing one of EXP's operands.
5091    IGNORE is nonzero if the value is to be ignored.  */
5092
5093 rtx
5094 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5095                 int ignore)
5096 {
5097   tree fndecl = get_callee_fndecl (exp);
5098   tree arglist = TREE_OPERAND (exp, 1);
5099   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5100   enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5101
5102   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5103     return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5104
5105   /* When not optimizing, generate calls to library functions for a certain
5106      set of builtins.  */
5107   if (!optimize
5108       && !CALLED_AS_BUILT_IN (fndecl)
5109       && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5110       && fcode != BUILT_IN_ALLOCA)
5111     return expand_call (exp, target, ignore);
5112
5113   /* The built-in function expanders test for target == const0_rtx
5114      to determine whether the function's result will be ignored.  */
5115   if (ignore)
5116     target = const0_rtx;
5117
5118   /* If the result of a pure or const built-in function is ignored, and
5119      none of its arguments are volatile, we can avoid expanding the
5120      built-in call and just evaluate the arguments for side-effects.  */
5121   if (target == const0_rtx
5122       && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5123     {
5124       bool volatilep = false;
5125       tree arg;
5126
5127       for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5128         if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5129           {
5130             volatilep = true;
5131             break;
5132           }
5133
5134       if (! volatilep)
5135         {
5136           for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5137             expand_expr (TREE_VALUE (arg), const0_rtx,
5138                          VOIDmode, EXPAND_NORMAL);
5139           return const0_rtx;
5140         }
5141     }
5142
5143   switch (fcode)
5144     {
5145     case BUILT_IN_FABS:
5146     case BUILT_IN_FABSF:
5147     case BUILT_IN_FABSL:
5148       target = expand_builtin_fabs (arglist, target, subtarget);
5149       if (target)
5150         return target;
5151       break;
5152
5153     case BUILT_IN_COPYSIGN:
5154     case BUILT_IN_COPYSIGNF:
5155     case BUILT_IN_COPYSIGNL:
5156       target = expand_builtin_copysign (arglist, target, subtarget);
5157       if (target)
5158         return target;
5159       break;
5160
5161       /* Just do a normal library call if we were unable to fold
5162          the values.  */
5163     case BUILT_IN_CABS:
5164     case BUILT_IN_CABSF:
5165     case BUILT_IN_CABSL:
5166       break;
5167
5168     case BUILT_IN_EXP:
5169     case BUILT_IN_EXPF:
5170     case BUILT_IN_EXPL:
5171     case BUILT_IN_EXP10:
5172     case BUILT_IN_EXP10F:
5173     case BUILT_IN_EXP10L:
5174     case BUILT_IN_POW10:
5175     case BUILT_IN_POW10F:
5176     case BUILT_IN_POW10L:
5177     case BUILT_IN_EXP2:
5178     case BUILT_IN_EXP2F:
5179     case BUILT_IN_EXP2L:
5180     case BUILT_IN_EXPM1:
5181     case BUILT_IN_EXPM1F:
5182     case BUILT_IN_EXPM1L:
5183     case BUILT_IN_LOGB:
5184     case BUILT_IN_LOGBF:
5185     case BUILT_IN_LOGBL:
5186     case BUILT_IN_ILOGB:
5187     case BUILT_IN_ILOGBF:
5188     case BUILT_IN_ILOGBL:
5189     case BUILT_IN_LOG:
5190     case BUILT_IN_LOGF:
5191     case BUILT_IN_LOGL:
5192     case BUILT_IN_LOG10:
5193     case BUILT_IN_LOG10F:
5194     case BUILT_IN_LOG10L:
5195     case BUILT_IN_LOG2:
5196     case BUILT_IN_LOG2F:
5197     case BUILT_IN_LOG2L:
5198     case BUILT_IN_LOG1P:
5199     case BUILT_IN_LOG1PF:
5200     case BUILT_IN_LOG1PL:
5201     case BUILT_IN_TAN:
5202     case BUILT_IN_TANF:
5203     case BUILT_IN_TANL:
5204     case BUILT_IN_ASIN:
5205     case BUILT_IN_ASINF:
5206     case BUILT_IN_ASINL:
5207     case BUILT_IN_ACOS:
5208     case BUILT_IN_ACOSF:
5209     case BUILT_IN_ACOSL:
5210     case BUILT_IN_ATAN:
5211     case BUILT_IN_ATANF:
5212     case BUILT_IN_ATANL:
5213       /* Treat these like sqrt only if unsafe math optimizations are allowed,
5214          because of possible accuracy problems.  */
5215       if (! flag_unsafe_math_optimizations)
5216         break;
5217     case BUILT_IN_SQRT:
5218     case BUILT_IN_SQRTF:
5219     case BUILT_IN_SQRTL:
5220     case BUILT_IN_FLOOR:
5221     case BUILT_IN_FLOORF:
5222     case BUILT_IN_FLOORL:
5223     case BUILT_IN_CEIL:
5224     case BUILT_IN_CEILF:
5225     case BUILT_IN_CEILL:
5226     case BUILT_IN_TRUNC:
5227     case BUILT_IN_TRUNCF:
5228     case BUILT_IN_TRUNCL:
5229     case BUILT_IN_ROUND:
5230     case BUILT_IN_ROUNDF:
5231     case BUILT_IN_ROUNDL:
5232     case BUILT_IN_NEARBYINT:
5233     case BUILT_IN_NEARBYINTF:
5234     case BUILT_IN_NEARBYINTL:
5235     case BUILT_IN_RINT:
5236     case BUILT_IN_RINTF:
5237     case BUILT_IN_RINTL:
5238       target = expand_builtin_mathfn (exp, target, subtarget);
5239       if (target)
5240         return target;
5241       break;
5242
5243     case BUILT_IN_POW:
5244     case BUILT_IN_POWF:
5245     case BUILT_IN_POWL:
5246       target = expand_builtin_pow (exp, target, subtarget);
5247       if (target)
5248         return target;
5249       break;
5250
5251     case BUILT_IN_POWI:
5252     case BUILT_IN_POWIF:
5253     case BUILT_IN_POWIL:
5254       target = expand_builtin_powi (exp, target, subtarget);
5255       if (target)
5256         return target;
5257       break;
5258
5259     case BUILT_IN_ATAN2:
5260     case BUILT_IN_ATAN2F:
5261     case BUILT_IN_ATAN2L:
5262     case BUILT_IN_FMOD:
5263     case BUILT_IN_FMODF:
5264     case BUILT_IN_FMODL:
5265     case BUILT_IN_DREM:
5266     case BUILT_IN_DREMF:
5267     case BUILT_IN_DREML:
5268       if (! flag_unsafe_math_optimizations)
5269         break;
5270       target = expand_builtin_mathfn_2 (exp, target, subtarget);
5271       if (target)
5272         return target;
5273       break;
5274
5275     case BUILT_IN_SIN:
5276     case BUILT_IN_SINF:
5277     case BUILT_IN_SINL:
5278     case BUILT_IN_COS:
5279     case BUILT_IN_COSF:
5280     case BUILT_IN_COSL:
5281       if (! flag_unsafe_math_optimizations)
5282         break;
5283       target = expand_builtin_mathfn_3 (exp, target, subtarget);
5284       if (target)
5285         return target;
5286       break;
5287
5288     case BUILT_IN_APPLY_ARGS:
5289       return expand_builtin_apply_args ();
5290
5291       /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5292          FUNCTION with a copy of the parameters described by
5293          ARGUMENTS, and ARGSIZE.  It returns a block of memory
5294          allocated on the stack into which is stored all the registers
5295          that might possibly be used for returning the result of a
5296          function.  ARGUMENTS is the value returned by
5297          __builtin_apply_args.  ARGSIZE is the number of bytes of
5298          arguments that must be copied.  ??? How should this value be
5299          computed?  We'll also need a safe worst case value for varargs
5300          functions.  */
5301     case BUILT_IN_APPLY:
5302       if (!validate_arglist (arglist, POINTER_TYPE,
5303                              POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5304           && !validate_arglist (arglist, REFERENCE_TYPE,
5305                                 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5306         return const0_rtx;
5307       else
5308         {
5309           int i;
5310           tree t;
5311           rtx ops[3];
5312
5313           for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5314             ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5315
5316           return expand_builtin_apply (ops[0], ops[1], ops[2]);
5317         }
5318
5319       /* __builtin_return (RESULT) causes the function to return the
5320          value described by RESULT.  RESULT is address of the block of
5321          memory returned by __builtin_apply.  */
5322     case BUILT_IN_RETURN:
5323       if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5324         expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5325                                             NULL_RTX, VOIDmode, 0));
5326       return const0_rtx;
5327
5328     case BUILT_IN_SAVEREGS:
5329       return expand_builtin_saveregs ();
5330
5331     case BUILT_IN_ARGS_INFO:
5332       return expand_builtin_args_info (arglist);
5333
5334       /* Return the address of the first anonymous stack arg.  */
5335     case BUILT_IN_NEXT_ARG:
5336       if (fold_builtin_next_arg (arglist))
5337         return const0_rtx;
5338       return expand_builtin_next_arg ();
5339
5340     case BUILT_IN_CLASSIFY_TYPE:
5341       return expand_builtin_classify_type (arglist);
5342
5343     case BUILT_IN_CONSTANT_P:
5344       return const0_rtx;
5345
5346     case BUILT_IN_FRAME_ADDRESS:
5347     case BUILT_IN_RETURN_ADDRESS:
5348       return expand_builtin_frame_address (fndecl, arglist);
5349
5350     /* Returns the address of the area where the structure is returned.
5351        0 otherwise.  */
5352     case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5353       if (arglist != 0
5354           || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5355           || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5356         return const0_rtx;
5357       else
5358         return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5359
5360     case BUILT_IN_ALLOCA:
5361       target = expand_builtin_alloca (arglist, target);
5362       if (target)
5363         return target;
5364       break;
5365
5366     case BUILT_IN_STACK_SAVE:
5367       return expand_stack_save ();
5368
5369     case BUILT_IN_STACK_RESTORE:
5370       expand_stack_restore (TREE_VALUE (arglist));
5371       return const0_rtx;
5372
5373     case BUILT_IN_FFS:
5374     case BUILT_IN_FFSL:
5375     case BUILT_IN_FFSLL:
5376     case BUILT_IN_FFSIMAX:
5377       target = expand_builtin_unop (target_mode, arglist, target,
5378                                     subtarget, ffs_optab);
5379       if (target)
5380         return target;
5381       break;
5382
5383     case BUILT_IN_CLZ:
5384     case BUILT_IN_CLZL:
5385     case BUILT_IN_CLZLL:
5386     case BUILT_IN_CLZIMAX:
5387       target = expand_builtin_unop (target_mode, arglist, target,
5388                                     subtarget, clz_optab);
5389       if (target)
5390         return target;
5391       break;
5392
5393     case BUILT_IN_CTZ:
5394     case BUILT_IN_CTZL:
5395     case BUILT_IN_CTZLL:
5396     case BUILT_IN_CTZIMAX:
5397       target = expand_builtin_unop (target_mode, arglist, target,
5398                                     subtarget, ctz_optab);
5399       if (target)
5400         return target;
5401       break;
5402
5403     case BUILT_IN_POPCOUNT:
5404     case BUILT_IN_POPCOUNTL:
5405     case BUILT_IN_POPCOUNTLL:
5406     case BUILT_IN_POPCOUNTIMAX:
5407       target = expand_builtin_unop (target_mode, arglist, target,
5408                                     subtarget, popcount_optab);
5409       if (target)
5410         return target;
5411       break;
5412
5413     case BUILT_IN_PARITY:
5414     case BUILT_IN_PARITYL:
5415     case BUILT_IN_PARITYLL:
5416     case BUILT_IN_PARITYIMAX:
5417       target = expand_builtin_unop (target_mode, arglist, target,
5418                                     subtarget, parity_optab);
5419       if (target)
5420         return target;
5421       break;
5422
5423     case BUILT_IN_STRLEN:
5424       target = expand_builtin_strlen (arglist, target, target_mode);
5425       if (target)
5426         return target;
5427       break;
5428
5429     case BUILT_IN_STRCPY:
5430       target = expand_builtin_strcpy (exp, target, mode);
5431       if (target)
5432         return target;
5433       break;
5434
5435     case BUILT_IN_STRNCPY:
5436       target = expand_builtin_strncpy (exp, target, mode);
5437       if (target)
5438         return target;
5439       break;
5440
5441     case BUILT_IN_STPCPY:
5442       target = expand_builtin_stpcpy (exp, target, mode);
5443       if (target)
5444         return target;
5445       break;
5446
5447     case BUILT_IN_STRCAT:
5448       target = expand_builtin_strcat (arglist, TREE_TYPE (exp), target, mode);
5449       if (target)
5450         return target;
5451       break;
5452
5453     case BUILT_IN_STRNCAT:
5454       target = expand_builtin_strncat (arglist, target, mode);
5455       if (target)
5456         return target;
5457       break;
5458
5459     case BUILT_IN_STRSPN:
5460       target = expand_builtin_strspn (arglist, target, mode);
5461       if (target)
5462         return target;
5463       break;
5464
5465     case BUILT_IN_STRCSPN:
5466       target = expand_builtin_strcspn (arglist, target, mode);
5467       if (target)
5468         return target;
5469       break;
5470
5471     case BUILT_IN_STRSTR:
5472       target = expand_builtin_strstr (arglist, target, mode);
5473       if (target)
5474         return target;
5475       break;
5476
5477     case BUILT_IN_STRPBRK:
5478       target = expand_builtin_strpbrk (arglist, target, mode);
5479       if (target)
5480         return target;
5481       break;
5482
5483     case BUILT_IN_INDEX:
5484     case BUILT_IN_STRCHR:
5485       target = expand_builtin_strchr (arglist, target, mode);
5486       if (target)
5487         return target;
5488       break;
5489
5490     case BUILT_IN_RINDEX:
5491     case BUILT_IN_STRRCHR:
5492       target = expand_builtin_strrchr (arglist, target, mode);
5493       if (target)
5494         return target;
5495       break;
5496
5497     case BUILT_IN_MEMCPY:
5498       target = expand_builtin_memcpy (exp, target, mode);
5499       if (target)
5500         return target;
5501       break;
5502
5503     case BUILT_IN_MEMPCPY:
5504       target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
5505       if (target)
5506         return target;
5507       break;
5508
5509     case BUILT_IN_MEMMOVE:
5510       target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target, mode);
5511       if (target)
5512         return target;
5513       break;
5514
5515     case BUILT_IN_BCOPY:
5516       target = expand_builtin_bcopy (arglist, TREE_TYPE (exp));
5517       if (target)
5518         return target;
5519       break;
5520
5521     case BUILT_IN_MEMSET:
5522       target = expand_builtin_memset (arglist, target, mode);
5523       if (target)
5524         return target;
5525       break;
5526
5527     case BUILT_IN_BZERO:
5528       target = expand_builtin_bzero (arglist);
5529       if (target)
5530         return target;
5531       break;
5532
5533     case BUILT_IN_STRCMP:
5534       target = expand_builtin_strcmp (exp, target, mode);
5535       if (target)
5536         return target;
5537       break;
5538
5539     case BUILT_IN_STRNCMP:
5540       target = expand_builtin_strncmp (exp, target, mode);
5541       if (target)
5542         return target;
5543       break;
5544
5545     case BUILT_IN_BCMP:
5546     case BUILT_IN_MEMCMP:
5547       target = expand_builtin_memcmp (exp, arglist, target, mode);
5548       if (target)
5549         return target;
5550       break;
5551
5552     case BUILT_IN_SETJMP:
5553       target = expand_builtin_setjmp (arglist, target);
5554       if (target)
5555         return target;
5556       break;
5557
5558       /* __builtin_longjmp is passed a pointer to an array of five words.
5559          It's similar to the C library longjmp function but works with
5560          __builtin_setjmp above.  */
5561     case BUILT_IN_LONGJMP:
5562       if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5563         break;
5564       else
5565         {
5566           rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5567                                       VOIDmode, 0);
5568           rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5569                                    NULL_RTX, VOIDmode, 0);
5570
5571           if (value != const1_rtx)
5572             {
5573               error ("%<__builtin_longjmp%> second argument must be 1");
5574               return const0_rtx;
5575             }
5576
5577           expand_builtin_longjmp (buf_addr, value);
5578           return const0_rtx;
5579         }
5580
5581     case BUILT_IN_NONLOCAL_GOTO:
5582       target = expand_builtin_nonlocal_goto (arglist);
5583       if (target)
5584         return target;
5585       break;
5586
5587       /* This updates the setjmp buffer that is its argument with the value
5588          of the current stack pointer.  */
5589     case BUILT_IN_UPDATE_SETJMP_BUF:
5590       if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5591         {
5592           rtx buf_addr
5593             = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5594
5595           expand_builtin_update_setjmp_buf (buf_addr);
5596           return const0_rtx;
5597         }
5598       break;
5599
5600     case BUILT_IN_TRAP:
5601       expand_builtin_trap ();
5602       return const0_rtx;
5603
5604     case BUILT_IN_PRINTF:
5605       target = expand_builtin_printf (arglist, target, mode, false);
5606       if (target)
5607         return target;
5608       break;
5609
5610     case BUILT_IN_PRINTF_UNLOCKED:
5611       target = expand_builtin_printf (arglist, target, mode, true);
5612       if (target)
5613         return target;
5614       break;
5615
5616     case BUILT_IN_FPUTS:
5617       target = expand_builtin_fputs (arglist, target, false);
5618       if (target)
5619         return target;
5620       break;
5621     case BUILT_IN_FPUTS_UNLOCKED:
5622       target = expand_builtin_fputs (arglist, target, true);
5623       if (target)
5624         return target;
5625       break;
5626
5627     case BUILT_IN_FPRINTF:
5628       target = expand_builtin_fprintf (arglist, target, mode, false);
5629       if (target)
5630         return target;
5631       break;
5632
5633     case BUILT_IN_FPRINTF_UNLOCKED:
5634       target = expand_builtin_fprintf (arglist, target, mode, true);
5635       if (target)
5636         return target;
5637       break;
5638
5639     case BUILT_IN_SPRINTF:
5640       target = expand_builtin_sprintf (arglist, target, mode);
5641       if (target)
5642         return target;
5643       break;
5644
5645     case BUILT_IN_SIGNBIT:
5646     case BUILT_IN_SIGNBITF:
5647     case BUILT_IN_SIGNBITL:
5648       target = expand_builtin_signbit (exp, target);
5649       if (target)
5650         return target;
5651       break;
5652
5653       /* Various hooks for the DWARF 2 __throw routine.  */
5654     case BUILT_IN_UNWIND_INIT:
5655       expand_builtin_unwind_init ();
5656       return const0_rtx;
5657     case BUILT_IN_DWARF_CFA:
5658       return virtual_cfa_rtx;
5659 #ifdef DWARF2_UNWIND_INFO
5660     case BUILT_IN_DWARF_SP_COLUMN:
5661       return expand_builtin_dwarf_sp_column ();
5662     case BUILT_IN_INIT_DWARF_REG_SIZES:
5663       expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5664       return const0_rtx;
5665 #endif
5666     case BUILT_IN_FROB_RETURN_ADDR:
5667       return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5668     case BUILT_IN_EXTRACT_RETURN_ADDR:
5669       return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5670     case BUILT_IN_EH_RETURN:
5671       expand_builtin_eh_return (TREE_VALUE (arglist),
5672                                 TREE_VALUE (TREE_CHAIN (arglist)));
5673       return const0_rtx;
5674 #ifdef EH_RETURN_DATA_REGNO
5675     case BUILT_IN_EH_RETURN_DATA_REGNO:
5676       return expand_builtin_eh_return_data_regno (arglist);
5677 #endif
5678     case BUILT_IN_EXTEND_POINTER:
5679       return expand_builtin_extend_pointer (TREE_VALUE (arglist));
5680
5681     case BUILT_IN_VA_START:
5682     case BUILT_IN_STDARG_START:
5683       return expand_builtin_va_start (arglist);
5684     case BUILT_IN_VA_END:
5685       return expand_builtin_va_end (arglist);
5686     case BUILT_IN_VA_COPY:
5687       return expand_builtin_va_copy (arglist);
5688     case BUILT_IN_EXPECT:
5689       return expand_builtin_expect (arglist, target);
5690     case BUILT_IN_PREFETCH:
5691       expand_builtin_prefetch (arglist);
5692       return const0_rtx;
5693
5694     case BUILT_IN_PROFILE_FUNC_ENTER:
5695       return expand_builtin_profile_func (false);
5696     case BUILT_IN_PROFILE_FUNC_EXIT:
5697       return expand_builtin_profile_func (true);
5698
5699     case BUILT_IN_INIT_TRAMPOLINE:
5700       return expand_builtin_init_trampoline (arglist);
5701     case BUILT_IN_ADJUST_TRAMPOLINE:
5702       return expand_builtin_adjust_trampoline (arglist);
5703
5704     case BUILT_IN_FORK:
5705     case BUILT_IN_EXECL:
5706     case BUILT_IN_EXECV:
5707     case BUILT_IN_EXECLP:
5708     case BUILT_IN_EXECLE:
5709     case BUILT_IN_EXECVP:
5710     case BUILT_IN_EXECVE:
5711       target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
5712       if (target)
5713         return target;
5714       break;
5715
5716     default:    /* just do library call, if unknown builtin */
5717       break;
5718     }
5719
5720   /* The switch statement above can drop through to cause the function
5721      to be called normally.  */
5722   return expand_call (exp, target, ignore);
5723 }
5724
5725 /* Determine whether a tree node represents a call to a built-in
5726    function.  If the tree T is a call to a built-in function with
5727    the right number of arguments of the appropriate types, return
5728    the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
5729    Otherwise the return value is END_BUILTINS.  */
5730
5731 enum built_in_function
5732 builtin_mathfn_code (tree t)
5733 {
5734   tree fndecl, arglist, parmlist;
5735   tree argtype, parmtype;
5736
5737   if (TREE_CODE (t) != CALL_EXPR
5738       || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
5739     return END_BUILTINS;
5740
5741   fndecl = get_callee_fndecl (t);
5742   if (fndecl == NULL_TREE
5743       || TREE_CODE (fndecl) != FUNCTION_DECL
5744       || ! DECL_BUILT_IN (fndecl)
5745       || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5746     return END_BUILTINS;
5747
5748   arglist = TREE_OPERAND (t, 1);
5749   parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5750   for (; parmlist; parmlist = TREE_CHAIN (parmlist))
5751     {
5752       /* If a function doesn't take a variable number of arguments,
5753          the last element in the list will have type `void'.  */
5754       parmtype = TREE_VALUE (parmlist);
5755       if (VOID_TYPE_P (parmtype))
5756         {
5757           if (arglist)
5758             return END_BUILTINS;
5759           return DECL_FUNCTION_CODE (fndecl);
5760         }
5761
5762       if (! arglist)
5763         return END_BUILTINS;
5764
5765       argtype = TREE_TYPE (TREE_VALUE (arglist));
5766
5767       if (SCALAR_FLOAT_TYPE_P (parmtype))
5768         {
5769           if (! SCALAR_FLOAT_TYPE_P (argtype))
5770             return END_BUILTINS;
5771         }
5772       else if (COMPLEX_FLOAT_TYPE_P (parmtype))
5773         {
5774           if (! COMPLEX_FLOAT_TYPE_P (argtype))
5775             return END_BUILTINS;
5776         }
5777       else if (POINTER_TYPE_P (parmtype))
5778         {
5779           if (! POINTER_TYPE_P (argtype))
5780             return END_BUILTINS;
5781         }
5782       else if (INTEGRAL_TYPE_P (parmtype))
5783         {
5784           if (! INTEGRAL_TYPE_P (argtype))
5785             return END_BUILTINS;
5786         }
5787       else
5788         return END_BUILTINS;
5789
5790       arglist = TREE_CHAIN (arglist);
5791     }
5792
5793   /* Variable-length argument list.  */
5794   return DECL_FUNCTION_CODE (fndecl);
5795 }
5796
5797 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5798    constant.  ARGLIST is the argument list of the call.  */
5799
5800 static tree
5801 fold_builtin_constant_p (tree arglist)
5802 {
5803   if (arglist == 0)
5804     return 0;
5805
5806   arglist = TREE_VALUE (arglist);
5807
5808   /* We return 1 for a numeric type that's known to be a constant
5809      value at compile-time or for an aggregate type that's a
5810      literal constant.  */
5811   STRIP_NOPS (arglist);
5812
5813   /* If we know this is a constant, emit the constant of one.  */
5814   if (CONSTANT_CLASS_P (arglist)
5815       || (TREE_CODE (arglist) == CONSTRUCTOR
5816           && TREE_CONSTANT (arglist))
5817       || (TREE_CODE (arglist) == ADDR_EXPR
5818           && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
5819     return integer_one_node;
5820
5821   /* If this expression has side effects, show we don't know it to be a
5822      constant.  Likewise if it's a pointer or aggregate type since in
5823      those case we only want literals, since those are only optimized
5824      when generating RTL, not later.
5825      And finally, if we are compiling an initializer, not code, we
5826      need to return a definite result now; there's not going to be any
5827      more optimization done.  */
5828   if (TREE_SIDE_EFFECTS (arglist)
5829       || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
5830       || POINTER_TYPE_P (TREE_TYPE (arglist))
5831       || cfun == 0)
5832     return integer_zero_node;
5833
5834   return 0;
5835 }
5836
5837 /* Fold a call to __builtin_expect, if we expect that a comparison against
5838    the argument will fold to a constant.  In practice, this means a true
5839    constant or the address of a non-weak symbol.  ARGLIST is the argument
5840    list of the call.  */
5841
5842 static tree
5843 fold_builtin_expect (tree arglist)
5844 {
5845   tree arg, inner;
5846
5847   if (arglist == 0)
5848     return 0;
5849
5850   arg = TREE_VALUE (arglist);
5851
5852   /* If the argument isn't invariant, then there's nothing we can do.  */
5853   if (!TREE_INVARIANT (arg))
5854     return 0;
5855
5856   /* If we're looking at an address of a weak decl, then do not fold.  */
5857   inner = arg;
5858   STRIP_NOPS (inner);
5859   if (TREE_CODE (inner) == ADDR_EXPR)
5860     {
5861       do
5862         {
5863           inner = TREE_OPERAND (inner, 0);
5864         }
5865       while (TREE_CODE (inner) == COMPONENT_REF
5866              || TREE_CODE (inner) == ARRAY_REF);
5867       if (DECL_P (inner) && DECL_WEAK (inner))
5868         return 0;
5869     }
5870
5871   /* Otherwise, ARG already has the proper type for the return value.  */
5872   return arg;
5873 }
5874
5875 /* Fold a call to __builtin_classify_type.  */
5876
5877 static tree
5878 fold_builtin_classify_type (tree arglist)
5879 {
5880   if (arglist == 0)
5881     return build_int_cst (NULL_TREE, no_type_class);
5882
5883   return build_int_cst (NULL_TREE,
5884                         type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
5885 }
5886
5887 /* Fold a call to __builtin_strlen.  */
5888
5889 static tree
5890 fold_builtin_strlen (tree arglist)
5891 {
5892   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5893     return NULL_TREE;
5894   else
5895     {
5896       tree len = c_strlen (TREE_VALUE (arglist), 0);
5897
5898       if (len)
5899         {
5900           /* Convert from the internal "sizetype" type to "size_t".  */
5901           if (size_type_node)
5902             len = fold_convert (size_type_node, len);
5903           return len;
5904         }
5905
5906       return NULL_TREE;
5907     }
5908 }
5909
5910 /* Fold a call to __builtin_inf or __builtin_huge_val.  */
5911
5912 static tree
5913 fold_builtin_inf (tree type, int warn)
5914 {
5915   REAL_VALUE_TYPE real;
5916
5917   /* __builtin_inff is intended to be usable to define INFINITY on all
5918      targets.  If an infinity is not available, INFINITY expands "to a
5919      positive constant of type float that overflows at translation
5920      time", footnote "In this case, using INFINITY will violate the
5921      constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
5922      Thus we pedwarn to ensure this constraint violation is
5923      diagnosed.  */
5924   if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
5925     pedwarn ("target format does not support infinity");
5926
5927   real_inf (&real);
5928   return build_real (type, real);
5929 }
5930
5931 /* Fold a call to __builtin_nan or __builtin_nans.  */
5932
5933 static tree
5934 fold_builtin_nan (tree arglist, tree type, int quiet)
5935 {
5936   REAL_VALUE_TYPE real;
5937   const char *str;
5938
5939   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5940     return 0;
5941   str = c_getstr (TREE_VALUE (arglist));
5942   if (!str)
5943     return 0;
5944
5945   if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
5946     return 0;
5947
5948   return build_real (type, real);
5949 }
5950
5951 /* Return true if the floating point expression T has an integer value.
5952    We also allow +Inf, -Inf and NaN to be considered integer values.  */
5953
5954 static bool
5955 integer_valued_real_p (tree t)
5956 {
5957   switch (TREE_CODE (t))
5958     {
5959     case FLOAT_EXPR:
5960       return true;
5961
5962     case ABS_EXPR:
5963     case SAVE_EXPR:
5964     case NON_LVALUE_EXPR:
5965       return integer_valued_real_p (TREE_OPERAND (t, 0));
5966
5967     case COMPOUND_EXPR:
5968     case MODIFY_EXPR:
5969     case BIND_EXPR:
5970       return integer_valued_real_p (TREE_OPERAND (t, 1));
5971
5972     case PLUS_EXPR:
5973     case MINUS_EXPR:
5974     case MULT_EXPR:
5975     case MIN_EXPR:
5976     case MAX_EXPR:
5977       return integer_valued_real_p (TREE_OPERAND (t, 0))
5978              && integer_valued_real_p (TREE_OPERAND (t, 1));
5979
5980     case COND_EXPR:
5981       return integer_valued_real_p (TREE_OPERAND (t, 1))
5982              && integer_valued_real_p (TREE_OPERAND (t, 2));
5983
5984     case REAL_CST:
5985       if (! TREE_CONSTANT_OVERFLOW (t))
5986       {
5987         REAL_VALUE_TYPE c, cint;
5988
5989         c = TREE_REAL_CST (t);
5990         real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
5991         return real_identical (&c, &cint);
5992       }
5993
5994     case NOP_EXPR:
5995       {
5996         tree type = TREE_TYPE (TREE_OPERAND (t, 0));
5997         if (TREE_CODE (type) == INTEGER_TYPE)
5998           return true;
5999         if (TREE_CODE (type) == REAL_TYPE)
6000           return integer_valued_real_p (TREE_OPERAND (t, 0));
6001         break;
6002       }
6003
6004     case CALL_EXPR:
6005       switch (builtin_mathfn_code (t))
6006         {
6007         case BUILT_IN_CEIL:
6008         case BUILT_IN_CEILF:
6009         case BUILT_IN_CEILL:
6010         case BUILT_IN_FLOOR:
6011         case BUILT_IN_FLOORF:
6012         case BUILT_IN_FLOORL:
6013         case BUILT_IN_NEARBYINT:
6014         case BUILT_IN_NEARBYINTF:
6015         case BUILT_IN_NEARBYINTL:
6016         case BUILT_IN_RINT:
6017         case BUILT_IN_RINTF:
6018         case BUILT_IN_RINTL:
6019         case BUILT_IN_ROUND:
6020         case BUILT_IN_ROUNDF:
6021         case BUILT_IN_ROUNDL:
6022         case BUILT_IN_TRUNC:
6023         case BUILT_IN_TRUNCF:
6024         case BUILT_IN_TRUNCL:
6025           return true;
6026
6027         default:
6028           break;
6029         }
6030       break;
6031
6032     default:
6033       break;
6034     }
6035   return false;
6036 }
6037
6038 /* EXP is assumed to be builtin call where truncation can be propagated
6039    across (for instance floor((double)f) == (double)floorf (f).
6040    Do the transformation.  */
6041
6042 static tree
6043 fold_trunc_transparent_mathfn (tree exp)
6044 {
6045   tree fndecl = get_callee_fndecl (exp);
6046   tree arglist = TREE_OPERAND (exp, 1);
6047   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6048   tree arg;
6049
6050   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6051     return 0;
6052
6053   arg = TREE_VALUE (arglist);
6054   /* Integer rounding functions are idempotent.  */
6055   if (fcode == builtin_mathfn_code (arg))
6056     return arg;
6057
6058   /* If argument is already integer valued, and we don't need to worry
6059      about setting errno, there's no need to perform rounding.  */
6060   if (! flag_errno_math && integer_valued_real_p (arg))
6061     return arg;
6062
6063   if (optimize)
6064     {
6065       tree arg0 = strip_float_extensions (arg);
6066       tree ftype = TREE_TYPE (exp);
6067       tree newtype = TREE_TYPE (arg0);
6068       tree decl;
6069
6070       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6071           && (decl = mathfn_built_in (newtype, fcode)))
6072         {
6073           arglist =
6074             build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6075           return fold_convert (ftype,
6076                                build_function_call_expr (decl, arglist));
6077         }
6078     }
6079   return 0;
6080 }
6081
6082 /* EXP is assumed to be builtin call which can narrow the FP type of
6083    the argument, for instance lround((double)f) -> lroundf (f).  */
6084
6085 static tree
6086 fold_fixed_mathfn (tree exp)
6087 {
6088   tree fndecl = get_callee_fndecl (exp);
6089   tree arglist = TREE_OPERAND (exp, 1);
6090   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6091   tree arg;
6092
6093   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6094     return 0;
6095
6096   arg = TREE_VALUE (arglist);
6097
6098   /* If argument is already integer valued, and we don't need to worry
6099      about setting errno, there's no need to perform rounding.  */
6100   if (! flag_errno_math && integer_valued_real_p (arg))
6101     return fold (build1 (FIX_TRUNC_EXPR, TREE_TYPE (exp), arg));
6102
6103   if (optimize)
6104     {
6105       tree ftype = TREE_TYPE (arg);
6106       tree arg0 = strip_float_extensions (arg);
6107       tree newtype = TREE_TYPE (arg0);
6108       tree decl;
6109
6110       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6111           && (decl = mathfn_built_in (newtype, fcode)))
6112         {
6113           arglist =
6114             build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6115           return build_function_call_expr (decl, arglist);
6116         }
6117     }
6118   return 0;
6119 }
6120
6121 /* Fold function call to builtin cabs, cabsf or cabsl.  ARGLIST
6122    is the argument list and TYPE is the return type.  Return
6123    NULL_TREE if no if no simplification can be made.  */
6124
6125 static tree
6126 fold_builtin_cabs (tree arglist, tree type)
6127 {
6128   tree arg;
6129
6130   if (!arglist || TREE_CHAIN (arglist))
6131     return NULL_TREE;
6132
6133   arg = TREE_VALUE (arglist);
6134   if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6135       || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6136     return NULL_TREE;
6137
6138   /* Evaluate cabs of a constant at compile-time.  */
6139   if (flag_unsafe_math_optimizations
6140       && TREE_CODE (arg) == COMPLEX_CST
6141       && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6142       && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6143       && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6144       && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6145     {
6146       REAL_VALUE_TYPE r, i;
6147
6148       r = TREE_REAL_CST (TREE_REALPART (arg));
6149       i = TREE_REAL_CST (TREE_IMAGPART (arg));
6150
6151       real_arithmetic (&r, MULT_EXPR, &r, &r);
6152       real_arithmetic (&i, MULT_EXPR, &i, &i);
6153       real_arithmetic (&r, PLUS_EXPR, &r, &i);
6154       if (real_sqrt (&r, TYPE_MODE (type), &r)
6155           || ! flag_trapping_math)
6156         return build_real (type, r);
6157     }
6158
6159   /* If either part is zero, cabs is fabs of the other.  */
6160   if (TREE_CODE (arg) == COMPLEX_EXPR
6161       && real_zerop (TREE_OPERAND (arg, 0)))
6162     return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
6163   if (TREE_CODE (arg) == COMPLEX_EXPR
6164       && real_zerop (TREE_OPERAND (arg, 1)))
6165     return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
6166
6167   /* Don't do this when optimizing for size.  */
6168   if (flag_unsafe_math_optimizations
6169       && optimize && !optimize_size)
6170     {
6171       tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6172
6173       if (sqrtfn != NULL_TREE)
6174         {
6175           tree rpart, ipart, result, arglist;
6176
6177           arg = builtin_save_expr (arg);
6178
6179           rpart = fold (build1 (REALPART_EXPR, type, arg));
6180           ipart = fold (build1 (IMAGPART_EXPR, type, arg));
6181
6182           rpart = builtin_save_expr (rpart);
6183           ipart = builtin_save_expr (ipart);
6184
6185           result = fold (build2 (PLUS_EXPR, type,
6186                                  fold (build2 (MULT_EXPR, type,
6187                                                rpart, rpart)),
6188                                  fold (build2 (MULT_EXPR, type,
6189                                                ipart, ipart))));
6190
6191           arglist = build_tree_list (NULL_TREE, result);
6192           return build_function_call_expr (sqrtfn, arglist);
6193         }
6194     }
6195
6196   return NULL_TREE;
6197 }
6198
6199 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl.  Return
6200    NULL_TREE if no simplification can be made.  */
6201
6202 static tree
6203 fold_builtin_sqrt (tree arglist, tree type)
6204 {
6205
6206   enum built_in_function fcode;
6207   tree arg = TREE_VALUE (arglist);
6208
6209   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6210     return NULL_TREE;
6211
6212   /* Optimize sqrt of constant value.  */
6213   if (TREE_CODE (arg) == REAL_CST
6214       && ! TREE_CONSTANT_OVERFLOW (arg))
6215     {
6216       REAL_VALUE_TYPE r, x;
6217
6218       x = TREE_REAL_CST (arg);
6219       if (real_sqrt (&r, TYPE_MODE (type), &x)
6220           || (!flag_trapping_math && !flag_errno_math))
6221         return build_real (type, r);
6222     }
6223
6224   /* Optimize sqrt(expN(x)) = expN(x*0.5).  */
6225   fcode = builtin_mathfn_code (arg);
6226   if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6227     {
6228       tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6229       arg = fold (build2 (MULT_EXPR, type,
6230                           TREE_VALUE (TREE_OPERAND (arg, 1)),
6231                           build_real (type, dconsthalf)));
6232       arglist = build_tree_list (NULL_TREE, arg);
6233       return build_function_call_expr (expfn, arglist);
6234     }
6235
6236   /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)).  */
6237   if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
6238     {
6239       tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6240
6241       if (powfn)
6242         {
6243           tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6244           tree tree_root;
6245           /* The inner root was either sqrt or cbrt.  */
6246           REAL_VALUE_TYPE dconstroot =
6247             BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
6248
6249           /* Adjust for the outer root.  */
6250           SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6251           dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6252           tree_root = build_real (type, dconstroot);
6253           arglist = tree_cons (NULL_TREE, arg0,
6254                                build_tree_list (NULL_TREE, tree_root));
6255           return build_function_call_expr (powfn, arglist);
6256         }
6257     }
6258
6259   /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5).  */
6260   if (flag_unsafe_math_optimizations
6261       && (fcode == BUILT_IN_POW
6262           || fcode == BUILT_IN_POWF
6263           || fcode == BUILT_IN_POWL))
6264     {
6265       tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6266       tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6267       tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6268       tree narg1;
6269       if (!tree_expr_nonnegative_p (arg0))
6270         arg0 = build1 (ABS_EXPR, type, arg0);
6271       narg1 = fold (build2 (MULT_EXPR, type, arg1,
6272                             build_real (type, dconsthalf)));
6273       arglist = tree_cons (NULL_TREE, arg0,
6274                            build_tree_list (NULL_TREE, narg1));
6275       return build_function_call_expr (powfn, arglist);
6276     }
6277
6278   return NULL_TREE;
6279 }
6280
6281 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl.  Return
6282    NULL_TREE if no simplification can be made.  */
6283 static tree
6284 fold_builtin_cbrt (tree arglist, tree type)
6285 {
6286   tree arg = TREE_VALUE (arglist);
6287   const enum built_in_function fcode = builtin_mathfn_code (arg);
6288
6289   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6290     return NULL_TREE;
6291
6292   /* Optimize cbrt of constant value.  */
6293   if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
6294     return arg;
6295
6296   /* Optimize cbrt(expN(x)) -> expN(x/3).  */
6297   if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6298     {
6299       tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6300       const REAL_VALUE_TYPE third_trunc =
6301         real_value_truncate (TYPE_MODE (type), dconstthird);
6302       arg = fold (build2 (MULT_EXPR, type,
6303                           TREE_VALUE (TREE_OPERAND (arg, 1)),
6304                           build_real (type, third_trunc)));
6305       arglist = build_tree_list (NULL_TREE, arg);
6306       return build_function_call_expr (expfn, arglist);
6307     }
6308
6309   /* Optimize cbrt(sqrt(x)) -> pow(x,1/6).  */
6310   /* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
6311      x is negative pow will error but cbrt won't.  */
6312   if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
6313     {
6314       tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6315
6316       if (powfn)
6317         {
6318           tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6319           tree tree_root;
6320           REAL_VALUE_TYPE dconstroot = dconstthird;
6321
6322           SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6323           dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6324           tree_root = build_real (type, dconstroot);
6325           arglist = tree_cons (NULL_TREE, arg0,
6326                                build_tree_list (NULL_TREE, tree_root));
6327           return build_function_call_expr (powfn, arglist);
6328         }
6329
6330     }
6331   return NULL_TREE;
6332 }
6333
6334 /* Fold function call to builtin sin, sinf, or sinl.  Return
6335    NULL_TREE if no simplification can be made.  */
6336 static tree
6337 fold_builtin_sin (tree arglist)
6338 {
6339   tree arg = TREE_VALUE (arglist);
6340
6341   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6342     return NULL_TREE;
6343
6344   /* Optimize sin (0.0) = 0.0.  */
6345   if (real_zerop (arg))
6346     return arg;
6347
6348   return NULL_TREE;
6349 }
6350
6351 /* Fold function call to builtin cos, cosf, or cosl.  Return
6352    NULL_TREE if no simplification can be made.  */
6353 static tree
6354 fold_builtin_cos (tree arglist, tree type, tree fndecl)
6355 {
6356   tree arg = TREE_VALUE (arglist);
6357
6358   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6359     return NULL_TREE;
6360
6361   /* Optimize cos (0.0) = 1.0.  */
6362   if (real_zerop (arg))
6363     return build_real (type, dconst1);
6364
6365   /* Optimize cos(-x) into cos (x).  */
6366   if (TREE_CODE (arg) == NEGATE_EXPR)
6367     {
6368       tree args = build_tree_list (NULL_TREE,
6369                                    TREE_OPERAND (arg, 0));
6370       return build_function_call_expr (fndecl, args);
6371     }
6372
6373   return NULL_TREE;
6374 }
6375
6376 /* Fold function call to builtin tan, tanf, or tanl.  Return
6377    NULL_TREE if no simplification can be made.  */
6378 static tree
6379 fold_builtin_tan (tree arglist)
6380 {
6381   enum built_in_function fcode;
6382   tree arg = TREE_VALUE (arglist);
6383
6384   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6385     return NULL_TREE;
6386
6387   /* Optimize tan(0.0) = 0.0.  */
6388   if (real_zerop (arg))
6389     return arg;
6390
6391   /* Optimize tan(atan(x)) = x.  */
6392   fcode = builtin_mathfn_code (arg);
6393   if (flag_unsafe_math_optimizations
6394       && (fcode == BUILT_IN_ATAN
6395           || fcode == BUILT_IN_ATANF
6396           || fcode == BUILT_IN_ATANL))
6397     return TREE_VALUE (TREE_OPERAND (arg, 1));
6398
6399   return NULL_TREE;
6400 }
6401
6402 /* Fold function call to builtin atan, atanf, or atanl.  Return
6403    NULL_TREE if no simplification can be made.  */
6404
6405 static tree
6406 fold_builtin_atan (tree arglist, tree type)
6407 {
6408
6409   tree arg = TREE_VALUE (arglist);
6410
6411   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6412     return NULL_TREE;
6413
6414   /* Optimize atan(0.0) = 0.0.  */
6415   if (real_zerop (arg))
6416     return arg;
6417
6418   /* Optimize atan(1.0) = pi/4.  */
6419   if (real_onep (arg))
6420     {
6421       REAL_VALUE_TYPE cst;
6422
6423       real_convert (&cst, TYPE_MODE (type), &dconstpi);
6424       SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
6425       return build_real (type, cst);
6426     }
6427
6428   return NULL_TREE;
6429 }
6430
6431 /* Fold function call to builtin trunc, truncf or truncl.  Return
6432    NULL_TREE if no simplification can be made.  */
6433
6434 static tree
6435 fold_builtin_trunc (tree exp)
6436 {
6437   tree arglist = TREE_OPERAND (exp, 1);
6438   tree arg;
6439
6440   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6441     return 0;
6442
6443   /* Optimize trunc of constant value.  */
6444   arg = TREE_VALUE (arglist);
6445   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6446     {
6447       REAL_VALUE_TYPE r, x;
6448       tree type = TREE_TYPE (exp);
6449
6450       x = TREE_REAL_CST (arg);
6451       real_trunc (&r, TYPE_MODE (type), &x);
6452       return build_real (type, r);
6453     }
6454
6455   return fold_trunc_transparent_mathfn (exp);
6456 }
6457
6458 /* Fold function call to builtin floor, floorf or floorl.  Return
6459    NULL_TREE if no simplification can be made.  */
6460
6461 static tree
6462 fold_builtin_floor (tree exp)
6463 {
6464   tree arglist = TREE_OPERAND (exp, 1);
6465   tree arg;
6466
6467   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6468     return 0;
6469
6470   /* Optimize floor of constant value.  */
6471   arg = TREE_VALUE (arglist);
6472   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6473     {
6474       REAL_VALUE_TYPE x;
6475
6476       x = TREE_REAL_CST (arg);
6477       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6478         {
6479           tree type = TREE_TYPE (exp);
6480           REAL_VALUE_TYPE r;
6481
6482           real_floor (&r, TYPE_MODE (type), &x);
6483           return build_real (type, r);
6484         }
6485     }
6486
6487   return fold_trunc_transparent_mathfn (exp);
6488 }
6489
6490 /* Fold function call to builtin ceil, ceilf or ceill.  Return
6491    NULL_TREE if no simplification can be made.  */
6492
6493 static tree
6494 fold_builtin_ceil (tree exp)
6495 {
6496   tree arglist = TREE_OPERAND (exp, 1);
6497   tree arg;
6498
6499   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6500     return 0;
6501
6502   /* Optimize ceil of constant value.  */
6503   arg = TREE_VALUE (arglist);
6504   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6505     {
6506       REAL_VALUE_TYPE x;
6507
6508       x = TREE_REAL_CST (arg);
6509       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6510         {
6511           tree type = TREE_TYPE (exp);
6512           REAL_VALUE_TYPE r;
6513
6514           real_ceil (&r, TYPE_MODE (type), &x);
6515           return build_real (type, r);
6516         }
6517     }
6518
6519   return fold_trunc_transparent_mathfn (exp);
6520 }
6521
6522 /* Fold function call to builtin round, roundf or roundl.  Return
6523    NULL_TREE if no simplification can be made.  */
6524
6525 static tree
6526 fold_builtin_round (tree exp)
6527 {
6528   tree arglist = TREE_OPERAND (exp, 1);
6529   tree arg;
6530
6531   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6532     return 0;
6533
6534   /* Optimize round of constant value.  */
6535   arg = TREE_VALUE (arglist);
6536   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6537     {
6538       REAL_VALUE_TYPE x;
6539
6540       x = TREE_REAL_CST (arg);
6541       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6542         {
6543           tree type = TREE_TYPE (exp);
6544           REAL_VALUE_TYPE r;
6545
6546           real_round (&r, TYPE_MODE (type), &x);
6547           return build_real (type, r);
6548         }
6549     }
6550
6551   return fold_trunc_transparent_mathfn (exp);
6552 }
6553
6554 /* Fold function call to builtin lround, lroundf or lroundl (or the
6555    corresponding long long versions).  Return NULL_TREE if no
6556    simplification can be made.  */
6557
6558 static tree
6559 fold_builtin_lround (tree exp)
6560 {
6561   tree arglist = TREE_OPERAND (exp, 1);
6562   tree arg;
6563
6564   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6565     return 0;
6566
6567   /* Optimize lround of constant value.  */
6568   arg = TREE_VALUE (arglist);
6569   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6570     {
6571       const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
6572
6573       if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
6574         {
6575           tree itype = TREE_TYPE (exp), ftype = TREE_TYPE (arg), result;
6576           HOST_WIDE_INT hi, lo;
6577           REAL_VALUE_TYPE r;
6578
6579           real_round (&r, TYPE_MODE (ftype), &x);
6580           REAL_VALUE_TO_INT (&lo, &hi, r);
6581           result = build_int_cst_wide (NULL_TREE, lo, hi);
6582           if (int_fits_type_p (result, itype))
6583             return fold_convert (itype, result);
6584         }
6585     }
6586
6587   return fold_fixed_mathfn (exp);
6588 }
6589
6590 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
6591    and their long and long long variants (i.e. ffsl and ffsll).
6592    Return NULL_TREE if no simplification can be made.  */
6593
6594 static tree
6595 fold_builtin_bitop (tree exp)
6596 {
6597   tree fndecl = get_callee_fndecl (exp);
6598   tree arglist = TREE_OPERAND (exp, 1);
6599   tree arg;
6600
6601   if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6602     return NULL_TREE;
6603
6604   /* Optimize for constant argument.  */
6605   arg = TREE_VALUE (arglist);
6606   if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6607     {
6608       HOST_WIDE_INT hi, width, result;
6609       unsigned HOST_WIDE_INT lo;
6610       tree type;
6611
6612       type = TREE_TYPE (arg);
6613       width = TYPE_PRECISION (type);
6614       lo = TREE_INT_CST_LOW (arg);
6615
6616       /* Clear all the bits that are beyond the type's precision.  */
6617       if (width > HOST_BITS_PER_WIDE_INT)
6618         {
6619           hi = TREE_INT_CST_HIGH (arg);
6620           if (width < 2 * HOST_BITS_PER_WIDE_INT)
6621             hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
6622         }
6623       else
6624         {
6625           hi = 0;
6626           if (width < HOST_BITS_PER_WIDE_INT)
6627             lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
6628         }
6629
6630       switch (DECL_FUNCTION_CODE (fndecl))
6631         {
6632         case BUILT_IN_FFS:
6633         case BUILT_IN_FFSL:
6634         case BUILT_IN_FFSLL:
6635           if (lo != 0)
6636             result = exact_log2 (lo & -lo) + 1;
6637           else if (hi != 0)
6638             result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
6639           else
6640             result = 0;
6641           break;
6642
6643         case BUILT_IN_CLZ:
6644         case BUILT_IN_CLZL:
6645         case BUILT_IN_CLZLL:
6646           if (hi != 0)
6647             result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
6648           else if (lo != 0)
6649             result = width - floor_log2 (lo) - 1;
6650           else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6651             result = width;
6652           break;
6653
6654         case BUILT_IN_CTZ:
6655         case BUILT_IN_CTZL:
6656         case BUILT_IN_CTZLL:
6657           if (lo != 0)
6658             result = exact_log2 (lo & -lo);
6659           else if (hi != 0)
6660             result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
6661           else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6662             result = width;
6663           break;
6664
6665         case BUILT_IN_POPCOUNT:
6666         case BUILT_IN_POPCOUNTL:
6667         case BUILT_IN_POPCOUNTLL:
6668           result = 0;
6669           while (lo)
6670             result++, lo &= lo - 1;
6671           while (hi)
6672             result++, hi &= hi - 1;
6673           break;
6674
6675         case BUILT_IN_PARITY:
6676         case BUILT_IN_PARITYL:
6677         case BUILT_IN_PARITYLL:
6678           result = 0;
6679           while (lo)
6680             result++, lo &= lo - 1;
6681           while (hi)
6682             result++, hi &= hi - 1;
6683           result &= 1;
6684           break;
6685
6686         default:
6687           gcc_unreachable ();
6688         }
6689
6690       return build_int_cst (TREE_TYPE (exp), result);
6691     }
6692
6693   return NULL_TREE;
6694 }
6695
6696 /* Return true if EXPR is the real constant contained in VALUE.  */
6697
6698 static bool
6699 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
6700 {
6701   STRIP_NOPS (expr);
6702
6703   return ((TREE_CODE (expr) == REAL_CST
6704            && ! TREE_CONSTANT_OVERFLOW (expr)
6705            && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
6706           || (TREE_CODE (expr) == COMPLEX_CST
6707               && real_dconstp (TREE_REALPART (expr), value)
6708               && real_zerop (TREE_IMAGPART (expr))));
6709 }
6710
6711 /* A subroutine of fold_builtin to fold the various logarithmic
6712    functions.  EXP is the CALL_EXPR of a call to a builtin logN
6713    function.  VALUE is the base of the logN function.  */
6714
6715 static tree
6716 fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
6717 {
6718   tree arglist = TREE_OPERAND (exp, 1);
6719
6720   if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6721     {
6722       tree fndecl = get_callee_fndecl (exp);
6723       tree type = TREE_TYPE (TREE_TYPE (fndecl));
6724       tree arg = TREE_VALUE (arglist);
6725       const enum built_in_function fcode = builtin_mathfn_code (arg);
6726
6727       /* Optimize logN(1.0) = 0.0.  */
6728       if (real_onep (arg))
6729         return build_real (type, dconst0);
6730
6731       /* Optimize logN(N) = 1.0.  If N can't be truncated to MODE
6732          exactly, then only do this if flag_unsafe_math_optimizations.  */
6733       if (exact_real_truncate (TYPE_MODE (type), value)
6734           || flag_unsafe_math_optimizations)
6735         {
6736           const REAL_VALUE_TYPE value_truncate =
6737             real_value_truncate (TYPE_MODE (type), *value);
6738           if (real_dconstp (arg, &value_truncate))
6739             return build_real (type, dconst1);
6740         }
6741
6742       /* Special case, optimize logN(expN(x)) = x.  */
6743       if (flag_unsafe_math_optimizations
6744           && ((value == &dconste
6745                && (fcode == BUILT_IN_EXP
6746                    || fcode == BUILT_IN_EXPF
6747                    || fcode == BUILT_IN_EXPL))
6748               || (value == &dconst2
6749                   && (fcode == BUILT_IN_EXP2
6750                       || fcode == BUILT_IN_EXP2F
6751                       || fcode == BUILT_IN_EXP2L))
6752               || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
6753         return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6754
6755       /* Optimize logN(func()) for various exponential functions.  We
6756          want to determine the value "x" and the power "exponent" in
6757          order to transform logN(x**exponent) into exponent*logN(x).  */
6758       if (flag_unsafe_math_optimizations)
6759         {
6760           tree exponent = 0, x = 0;
6761
6762           switch (fcode)
6763           {
6764           case BUILT_IN_EXP:
6765           case BUILT_IN_EXPF:
6766           case BUILT_IN_EXPL:
6767             /* Prepare to do logN(exp(exponent) -> exponent*logN(e).  */
6768             x = build_real (type,
6769                             real_value_truncate (TYPE_MODE (type), dconste));
6770             exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6771             break;
6772           case BUILT_IN_EXP2:
6773           case BUILT_IN_EXP2F:
6774           case BUILT_IN_EXP2L:
6775             /* Prepare to do logN(exp2(exponent) -> exponent*logN(2).  */
6776             x = build_real (type, dconst2);
6777             exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6778             break;
6779           case BUILT_IN_EXP10:
6780           case BUILT_IN_EXP10F:
6781           case BUILT_IN_EXP10L:
6782           case BUILT_IN_POW10:
6783           case BUILT_IN_POW10F:
6784           case BUILT_IN_POW10L:
6785             /* Prepare to do logN(exp10(exponent) -> exponent*logN(10).  */
6786             x = build_real (type, dconst10);
6787             exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6788             break;
6789           case BUILT_IN_SQRT:
6790           case BUILT_IN_SQRTF:
6791           case BUILT_IN_SQRTL:
6792             /* Prepare to do logN(sqrt(x) -> 0.5*logN(x).  */
6793             x = TREE_VALUE (TREE_OPERAND (arg, 1));
6794             exponent = build_real (type, dconsthalf);
6795             break;
6796           case BUILT_IN_CBRT:
6797           case BUILT_IN_CBRTF:
6798           case BUILT_IN_CBRTL:
6799             /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x).  */
6800             x = TREE_VALUE (TREE_OPERAND (arg, 1));
6801             exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
6802                                                               dconstthird));
6803             break;
6804           case BUILT_IN_POW:
6805           case BUILT_IN_POWF:
6806           case BUILT_IN_POWL:
6807             /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x).  */
6808             x = TREE_VALUE (TREE_OPERAND (arg, 1));
6809             exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6810             break;
6811           default:
6812             break;
6813           }
6814
6815           /* Now perform the optimization.  */
6816           if (x && exponent)
6817             {
6818               tree logfn;
6819               arglist = build_tree_list (NULL_TREE, x);
6820               logfn = build_function_call_expr (fndecl, arglist);
6821               return fold (build2 (MULT_EXPR, type, exponent, logfn));
6822             }
6823         }
6824     }
6825
6826   return 0;
6827 }
6828
6829 /* Fold a builtin function call to pow, powf, or powl.  Return
6830    NULL_TREE if no simplification can be made.  */
6831 static tree
6832 fold_builtin_pow (tree fndecl, tree arglist, tree type)
6833 {
6834   enum built_in_function fcode;
6835   tree arg0 = TREE_VALUE (arglist);
6836   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6837
6838   if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6839     return NULL_TREE;
6840
6841   /* Optimize pow(1.0,y) = 1.0.  */
6842   if (real_onep (arg0))
6843     return omit_one_operand (type, build_real (type, dconst1), arg1);
6844
6845   if (TREE_CODE (arg1) == REAL_CST
6846       && ! TREE_CONSTANT_OVERFLOW (arg1))
6847     {
6848       REAL_VALUE_TYPE cint;
6849       REAL_VALUE_TYPE c;
6850       HOST_WIDE_INT n;
6851
6852       c = TREE_REAL_CST (arg1);
6853
6854       /* Optimize pow(x,0.0) = 1.0.  */
6855       if (REAL_VALUES_EQUAL (c, dconst0))
6856         return omit_one_operand (type, build_real (type, dconst1),
6857                                  arg0);
6858
6859       /* Optimize pow(x,1.0) = x.  */
6860       if (REAL_VALUES_EQUAL (c, dconst1))
6861         return arg0;
6862
6863       /* Optimize pow(x,-1.0) = 1.0/x.  */
6864       if (REAL_VALUES_EQUAL (c, dconstm1))
6865         return fold (build2 (RDIV_EXPR, type,
6866                              build_real (type, dconst1), arg0));
6867
6868       /* Optimize pow(x,0.5) = sqrt(x).  */
6869       if (flag_unsafe_math_optimizations
6870           && REAL_VALUES_EQUAL (c, dconsthalf))
6871         {
6872           tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6873
6874           if (sqrtfn != NULL_TREE)
6875             {
6876               tree arglist = build_tree_list (NULL_TREE, arg0);
6877               return build_function_call_expr (sqrtfn, arglist);
6878             }
6879         }
6880
6881       /* Check for an integer exponent.  */
6882       n = real_to_integer (&c);
6883       real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
6884       if (real_identical (&c, &cint))
6885         {
6886           /* Attempt to evaluate pow at compile-time.  */
6887           if (TREE_CODE (arg0) == REAL_CST
6888               && ! TREE_CONSTANT_OVERFLOW (arg0))
6889             {
6890               REAL_VALUE_TYPE x;
6891               bool inexact;
6892
6893               x = TREE_REAL_CST (arg0);
6894               inexact = real_powi (&x, TYPE_MODE (type), &x, n);
6895               if (flag_unsafe_math_optimizations || !inexact)
6896                 return build_real (type, x);
6897             }
6898
6899           /* Strip sign ops from even integer powers.  */
6900           if ((n & 1) == 0 && flag_unsafe_math_optimizations)
6901             {
6902               tree narg0 = fold_strip_sign_ops (arg0);
6903               if (narg0)
6904                 {
6905                   arglist = build_tree_list (NULL_TREE, arg1);
6906                   arglist = tree_cons (NULL_TREE, narg0, arglist);
6907                   return build_function_call_expr (fndecl, arglist);
6908                 }
6909             }
6910         }
6911     }
6912
6913   /* Optimize pow(expN(x),y) = expN(x*y).  */
6914   fcode = builtin_mathfn_code (arg0);
6915   if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6916     {
6917       tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6918       tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
6919       arg = fold (build2 (MULT_EXPR, type, arg, arg1));
6920       arglist = build_tree_list (NULL_TREE, arg);
6921       return build_function_call_expr (expfn, arglist);
6922     }
6923
6924   /* Optimize pow(sqrt(x),y) = pow(x,y*0.5).  */
6925   if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
6926     {
6927       tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6928       tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
6929                                  build_real (type, dconsthalf)));
6930
6931       arglist = tree_cons (NULL_TREE, narg0,
6932                            build_tree_list (NULL_TREE, narg1));
6933       return build_function_call_expr (fndecl, arglist);
6934     }
6935
6936   /* Optimize pow(pow(x,y),z) = pow(x,y*z).  */
6937   if (flag_unsafe_math_optimizations
6938       && (fcode == BUILT_IN_POW
6939           || fcode == BUILT_IN_POWF
6940           || fcode == BUILT_IN_POWL))
6941     {
6942       tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6943       tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
6944       tree narg1 = fold (build2 (MULT_EXPR, type, arg01, arg1));
6945       arglist = tree_cons (NULL_TREE, arg00,
6946                            build_tree_list (NULL_TREE, narg1));
6947       return build_function_call_expr (fndecl, arglist);
6948     }
6949   return NULL_TREE;
6950 }
6951
6952 /* Fold a builtin function call to powi, powif, or powil.  Return
6953    NULL_TREE if no simplification can be made.  */
6954 static tree
6955 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
6956 {
6957   tree arg0 = TREE_VALUE (arglist);
6958   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6959
6960   if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
6961     return NULL_TREE;
6962
6963   /* Optimize pow(1.0,y) = 1.0.  */
6964   if (real_onep (arg0))
6965     return omit_one_operand (type, build_real (type, dconst1), arg1);
6966
6967   if (host_integerp (arg1, 0))
6968     {
6969       HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
6970
6971       /* Evaluate powi at compile-time.  */
6972       if (TREE_CODE (arg0) == REAL_CST
6973           && ! TREE_CONSTANT_OVERFLOW (arg0))
6974         {
6975           REAL_VALUE_TYPE x;
6976           x = TREE_REAL_CST (arg0);
6977           real_powi (&x, TYPE_MODE (type), &x, c);
6978           return build_real (type, x);
6979         }
6980
6981       /* Optimize pow(x,0) = 1.0.  */
6982       if (c == 0)
6983         return omit_one_operand (type, build_real (type, dconst1),
6984                                  arg0);
6985
6986       /* Optimize pow(x,1) = x.  */
6987       if (c == 1)
6988         return arg0;
6989
6990       /* Optimize pow(x,-1) = 1.0/x.  */
6991       if (c == -1)
6992         return fold (build2 (RDIV_EXPR, type,
6993                              build_real (type, dconst1), arg0));
6994     }
6995
6996   return NULL_TREE;
6997 }
6998
6999 /* A subroutine of fold_builtin to fold the various exponent
7000    functions.  EXP is the CALL_EXPR of a call to a builtin function.
7001    VALUE is the value which will be raised to a power.  */
7002
7003 static tree
7004 fold_builtin_exponent (tree exp, const REAL_VALUE_TYPE *value)
7005 {
7006   tree arglist = TREE_OPERAND (exp, 1);
7007
7008   if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7009     {
7010       tree fndecl = get_callee_fndecl (exp);
7011       tree type = TREE_TYPE (TREE_TYPE (fndecl));
7012       tree arg = TREE_VALUE (arglist);
7013
7014       /* Optimize exp*(0.0) = 1.0.  */
7015       if (real_zerop (arg))
7016         return build_real (type, dconst1);
7017
7018       /* Optimize expN(1.0) = N.  */
7019       if (real_onep (arg))
7020         {
7021           REAL_VALUE_TYPE cst;
7022
7023           real_convert (&cst, TYPE_MODE (type), value);
7024           return build_real (type, cst);
7025         }
7026
7027       /* Attempt to evaluate expN(integer) at compile-time.  */
7028       if (flag_unsafe_math_optimizations
7029           && TREE_CODE (arg) == REAL_CST
7030           && ! TREE_CONSTANT_OVERFLOW (arg))
7031         {
7032           REAL_VALUE_TYPE cint;
7033           REAL_VALUE_TYPE c;
7034           HOST_WIDE_INT n;
7035
7036           c = TREE_REAL_CST (arg);
7037           n = real_to_integer (&c);
7038           real_from_integer (&cint, VOIDmode, n,
7039                              n < 0 ? -1 : 0, 0);
7040           if (real_identical (&c, &cint))
7041             {
7042               REAL_VALUE_TYPE x;
7043
7044               real_powi (&x, TYPE_MODE (type), value, n);
7045               return build_real (type, x);
7046             }
7047         }
7048
7049       /* Optimize expN(logN(x)) = x.  */
7050       if (flag_unsafe_math_optimizations)
7051         {
7052           const enum built_in_function fcode = builtin_mathfn_code (arg);
7053
7054           if ((value == &dconste
7055                && (fcode == BUILT_IN_LOG
7056                    || fcode == BUILT_IN_LOGF
7057                    || fcode == BUILT_IN_LOGL))
7058               || (value == &dconst2
7059                   && (fcode == BUILT_IN_LOG2
7060                       || fcode == BUILT_IN_LOG2F
7061                       || fcode == BUILT_IN_LOG2L))
7062               || (value == &dconst10
7063                   && (fcode == BUILT_IN_LOG10
7064                       || fcode == BUILT_IN_LOG10F
7065                       || fcode == BUILT_IN_LOG10L)))
7066             return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7067         }
7068     }
7069
7070   return 0;
7071 }
7072
7073 /* Fold function call to builtin memcpy.  Return
7074    NULL_TREE if no simplification can be made.  */
7075
7076 static tree
7077 fold_builtin_memcpy (tree exp)
7078 {
7079   tree arglist = TREE_OPERAND (exp, 1);
7080   tree dest, src, len;
7081
7082   if (!validate_arglist (arglist,
7083                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7084     return 0;
7085
7086   dest = TREE_VALUE (arglist);
7087   src = TREE_VALUE (TREE_CHAIN (arglist));
7088   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7089
7090   /* If the LEN parameter is zero, return DEST.  */
7091   if (integer_zerop (len))
7092     return omit_one_operand (TREE_TYPE (exp), dest, src);
7093
7094   /* If SRC and DEST are the same (and not volatile), return DEST.  */
7095   if (operand_equal_p (src, dest, 0))
7096     return omit_one_operand (TREE_TYPE (exp), dest, len);
7097
7098   return 0;
7099 }
7100
7101 /* Fold function call to builtin mempcpy.  Return
7102    NULL_TREE if no simplification can be made.  */
7103
7104 static tree
7105 fold_builtin_mempcpy (tree arglist, tree type, int endp)
7106 {
7107   if (validate_arglist (arglist,
7108                         POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7109     {
7110       tree dest = TREE_VALUE (arglist);
7111       tree src = TREE_VALUE (TREE_CHAIN (arglist));
7112       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7113
7114       /* If the LEN parameter is zero, return DEST.  */
7115       if (integer_zerop (len))
7116         return omit_one_operand (type, dest, src);
7117
7118       /* If SRC and DEST are the same (and not volatile), return DEST+LEN.  */
7119       if (operand_equal_p (src, dest, 0))
7120         {
7121           if (endp == 0)
7122             return omit_one_operand (type, dest, len);
7123
7124           if (endp == 2)
7125             len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
7126                                 ssize_int (1)));
7127       
7128           len = fold_convert (TREE_TYPE (dest), len);
7129           len = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
7130           return fold_convert (type, len);
7131         }
7132     }
7133   return 0;
7134 }
7135
7136 /* Fold function call to builtin memmove.  Return
7137    NULL_TREE if no simplification can be made.  */
7138
7139 static tree
7140 fold_builtin_memmove (tree arglist, tree type)
7141 {
7142   tree dest, src, len;
7143
7144   if (!validate_arglist (arglist,
7145                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7146     return 0;
7147
7148   dest = TREE_VALUE (arglist);
7149   src = TREE_VALUE (TREE_CHAIN (arglist));
7150   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7151
7152   /* If the LEN parameter is zero, return DEST.  */
7153   if (integer_zerop (len))
7154     return omit_one_operand (type, dest, src);
7155
7156   /* If SRC and DEST are the same (and not volatile), return DEST.  */
7157   if (operand_equal_p (src, dest, 0))
7158     return omit_one_operand (type, dest, len);
7159
7160   return 0;
7161 }
7162
7163 /* Fold function call to builtin strcpy.  If LEN is not NULL, it represents
7164    the length of the string to be copied.  Return NULL_TREE if no
7165    simplification can be made.  */
7166
7167 tree
7168 fold_builtin_strcpy (tree exp, tree len)
7169 {
7170   tree arglist = TREE_OPERAND (exp, 1);
7171   tree dest, src, fn;
7172
7173   if (!validate_arglist (arglist,
7174                          POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7175     return 0;
7176
7177   dest = TREE_VALUE (arglist);
7178   src = TREE_VALUE (TREE_CHAIN (arglist));
7179
7180   /* If SRC and DEST are the same (and not volatile), return DEST.  */
7181   if (operand_equal_p (src, dest, 0))
7182     return fold_convert (TREE_TYPE (exp), dest);
7183
7184   if (optimize_size)
7185     return 0;
7186
7187   fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7188   if (!fn)
7189     return 0;
7190
7191   if (!len)
7192     {
7193       len = c_strlen (src, 1);
7194       if (! len || TREE_SIDE_EFFECTS (len))
7195         return 0;
7196     }
7197
7198   len = size_binop (PLUS_EXPR, len, ssize_int (1));
7199   arglist = build_tree_list (NULL_TREE, len);
7200   arglist = tree_cons (NULL_TREE, src, arglist);
7201   arglist = tree_cons (NULL_TREE, dest, arglist);
7202   return fold_convert (TREE_TYPE (exp),
7203                        build_function_call_expr (fn, arglist));
7204 }
7205
7206 /* Fold function call to builtin strncpy.  If SLEN is not NULL, it represents
7207    the length of the source string.  Return NULL_TREE if no simplification
7208    can be made.  */
7209
7210 tree
7211 fold_builtin_strncpy (tree exp, tree slen)
7212 {
7213   tree arglist = TREE_OPERAND (exp, 1);
7214   tree dest, src, len, fn;
7215
7216   if (!validate_arglist (arglist,
7217                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7218     return 0;
7219
7220   dest = TREE_VALUE (arglist);
7221   src = TREE_VALUE (TREE_CHAIN (arglist));
7222   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7223
7224   /* If the LEN parameter is zero, return DEST.  */
7225   if (integer_zerop (len))
7226     return omit_one_operand (TREE_TYPE (exp), dest, src);
7227
7228   /* We can't compare slen with len as constants below if len is not a
7229      constant.  */
7230   if (len == 0 || TREE_CODE (len) != INTEGER_CST)
7231     return 0;
7232
7233   if (!slen)
7234     slen = c_strlen (src, 1);
7235
7236   /* Now, we must be passed a constant src ptr parameter.  */
7237   if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
7238     return 0;
7239
7240   slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
7241
7242   /* We do not support simplification of this case, though we do
7243      support it when expanding trees into RTL.  */
7244   /* FIXME: generate a call to __builtin_memset.  */
7245   if (tree_int_cst_lt (slen, len))
7246     return 0;
7247
7248   /* OK transform into builtin memcpy.  */
7249   fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7250   if (!fn)
7251     return 0;
7252   return fold_convert (TREE_TYPE (exp),
7253                        build_function_call_expr (fn, arglist));
7254 }
7255
7256 /* Fold function call to builtin memcmp.  Return
7257    NULL_TREE if no simplification can be made.  */
7258
7259 static tree
7260 fold_builtin_memcmp (tree arglist)
7261 {
7262   tree arg1, arg2, len;
7263   const char *p1, *p2;
7264
7265   if (!validate_arglist (arglist,
7266                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7267     return 0;
7268
7269   arg1 = TREE_VALUE (arglist);
7270   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7271   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7272
7273   /* If the LEN parameter is zero, return zero.  */
7274   if (integer_zerop (len))
7275     return omit_two_operands (integer_type_node, integer_zero_node,
7276                               arg1, arg2);
7277
7278   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
7279   if (operand_equal_p (arg1, arg2, 0))
7280     return omit_one_operand (integer_type_node, integer_zero_node, len);
7281
7282   p1 = c_getstr (arg1);
7283   p2 = c_getstr (arg2);
7284
7285   /* If all arguments are constant, and the value of len is not greater
7286      than the lengths of arg1 and arg2, evaluate at compile-time.  */
7287   if (host_integerp (len, 1) && p1 && p2
7288       && compare_tree_int (len, strlen (p1) + 1) <= 0
7289       && compare_tree_int (len, strlen (p2) + 1) <= 0)
7290     {
7291       const int r = memcmp (p1, p2, tree_low_cst (len, 1));
7292
7293       if (r > 0)
7294         return integer_one_node;
7295       else if (r < 0)
7296         return integer_minus_one_node;
7297       else
7298         return integer_zero_node;
7299     }
7300
7301   /* If len parameter is one, return an expression corresponding to
7302      (*(const unsigned char*)arg1 - (const unsigned char*)arg2).  */
7303   if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7304     {
7305       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7306       tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7307       tree ind1 = fold_convert (integer_type_node,
7308                                 build1 (INDIRECT_REF, cst_uchar_node,
7309                                         fold_convert (cst_uchar_ptr_node,
7310                                                       arg1)));
7311       tree ind2 = fold_convert (integer_type_node,
7312                                 build1 (INDIRECT_REF, cst_uchar_node,
7313                                         fold_convert (cst_uchar_ptr_node,
7314                                                       arg2)));
7315       return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7316     }
7317
7318   return 0;
7319 }
7320
7321 /* Fold function call to builtin strcmp.  Return
7322    NULL_TREE if no simplification can be made.  */
7323
7324 static tree
7325 fold_builtin_strcmp (tree arglist)
7326 {
7327   tree arg1, arg2;
7328   const char *p1, *p2;
7329
7330   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7331     return 0;
7332
7333   arg1 = TREE_VALUE (arglist);
7334   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7335
7336   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
7337   if (operand_equal_p (arg1, arg2, 0))
7338     return integer_zero_node;
7339
7340   p1 = c_getstr (arg1);
7341   p2 = c_getstr (arg2);
7342
7343   if (p1 && p2)
7344     {
7345       const int i = strcmp (p1, p2);
7346       if (i < 0)
7347         return integer_minus_one_node;
7348       else if (i > 0)
7349         return integer_one_node;
7350       else
7351         return integer_zero_node;
7352     }
7353
7354   /* If the second arg is "", return *(const unsigned char*)arg1.  */
7355   if (p2 && *p2 == '\0')
7356     {
7357       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7358       tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7359       return fold_convert (integer_type_node,
7360                            build1 (INDIRECT_REF, cst_uchar_node,
7361                                    fold_convert (cst_uchar_ptr_node,
7362                                                  arg1)));
7363     }
7364
7365   /* If the first arg is "", return -*(const unsigned char*)arg2.  */
7366   if (p1 && *p1 == '\0')
7367     {
7368       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7369       tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7370       tree temp = fold_convert (integer_type_node,
7371                                 build1 (INDIRECT_REF, cst_uchar_node,
7372                                         fold_convert (cst_uchar_ptr_node,
7373                                                       arg2)));
7374       return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7375     }
7376
7377   return 0;
7378 }
7379
7380 /* Fold function call to builtin strncmp.  Return
7381    NULL_TREE if no simplification can be made.  */
7382
7383 static tree
7384 fold_builtin_strncmp (tree arglist)
7385 {
7386   tree arg1, arg2, len;
7387   const char *p1, *p2;
7388
7389   if (!validate_arglist (arglist,
7390                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7391     return 0;
7392
7393   arg1 = TREE_VALUE (arglist);
7394   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7395   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7396
7397   /* If the LEN parameter is zero, return zero.  */
7398   if (integer_zerop (len))
7399     return omit_two_operands (integer_type_node, integer_zero_node,
7400                               arg1, arg2);
7401
7402   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
7403   if (operand_equal_p (arg1, arg2, 0))
7404     return omit_one_operand (integer_type_node, integer_zero_node, len);
7405
7406   p1 = c_getstr (arg1);
7407   p2 = c_getstr (arg2);
7408
7409   if (host_integerp (len, 1) && p1 && p2)
7410     {
7411       const int i = strncmp (p1, p2, tree_low_cst (len, 1));
7412       if (i > 0)
7413         return integer_one_node;
7414       else if (i < 0)
7415         return integer_minus_one_node;
7416       else
7417         return integer_zero_node;
7418     }
7419
7420   /* If the second arg is "", and the length is greater than zero,
7421      return *(const unsigned char*)arg1.  */
7422   if (p2 && *p2 == '\0'
7423       && TREE_CODE (len) == INTEGER_CST
7424       && tree_int_cst_sgn (len) == 1)
7425     {
7426       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7427       tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7428       return fold_convert (integer_type_node,
7429                            build1 (INDIRECT_REF, cst_uchar_node,
7430                                    fold_convert (cst_uchar_ptr_node,
7431                                                  arg1)));
7432     }
7433
7434   /* If the first arg is "", and the length is greater than zero,
7435      return -*(const unsigned char*)arg2.  */
7436   if (p1 && *p1 == '\0'
7437       && TREE_CODE (len) == INTEGER_CST
7438       && tree_int_cst_sgn (len) == 1)
7439     {
7440       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7441       tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7442       tree temp = fold_convert (integer_type_node,
7443                                 build1 (INDIRECT_REF, cst_uchar_node,
7444                                         fold_convert (cst_uchar_ptr_node,
7445                                                       arg2)));
7446       return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7447     }
7448
7449   /* If len parameter is one, return an expression corresponding to
7450      (*(const unsigned char*)arg1 - (const unsigned char*)arg2).  */
7451   if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7452     {
7453       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7454       tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7455       tree ind1 = fold_convert (integer_type_node,
7456                                 build1 (INDIRECT_REF, cst_uchar_node,
7457                                         fold_convert (cst_uchar_ptr_node,
7458                                                       arg1)));
7459       tree ind2 = fold_convert (integer_type_node,
7460                                 build1 (INDIRECT_REF, cst_uchar_node,
7461                                         fold_convert (cst_uchar_ptr_node,
7462                                                       arg2)));
7463       return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7464     }
7465
7466   return 0;
7467 }
7468
7469 /* Fold function call to builtin signbit, signbitf or signbitl.  Return
7470    NULL_TREE if no simplification can be made.  */
7471
7472 static tree
7473 fold_builtin_signbit (tree exp)
7474 {
7475   tree arglist = TREE_OPERAND (exp, 1);
7476   tree arg, temp;
7477
7478   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7479     return NULL_TREE;
7480
7481   arg = TREE_VALUE (arglist);
7482
7483   /* If ARG is a compile-time constant, determine the result.  */
7484   if (TREE_CODE (arg) == REAL_CST
7485       && !TREE_CONSTANT_OVERFLOW (arg))
7486     {
7487       REAL_VALUE_TYPE c;
7488
7489       c = TREE_REAL_CST (arg);
7490       temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
7491       return fold_convert (TREE_TYPE (exp), temp);
7492     }
7493
7494   /* If ARG is non-negative, the result is always zero.  */
7495   if (tree_expr_nonnegative_p (arg))
7496     return omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg);
7497
7498   /* If ARG's format doesn't have signed zeros, return "arg < 0.0".  */
7499   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
7500     return fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
7501                          build_real (TREE_TYPE (arg), dconst0)));
7502
7503   return NULL_TREE;
7504 }
7505
7506 /* Fold function call to builtin copysign, copysignf or copysignl.
7507    Return NULL_TREE if no simplification can be made.  */
7508
7509 static tree
7510 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
7511 {
7512   tree arg1, arg2, tem;
7513
7514   if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7515     return NULL_TREE;
7516
7517   arg1 = TREE_VALUE (arglist);
7518   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7519
7520   /* copysign(X,X) is X.  */
7521   if (operand_equal_p (arg1, arg2, 0))
7522     return fold_convert (type, arg1);
7523
7524   /* If ARG1 and ARG2 are compile-time constants, determine the result.  */
7525   if (TREE_CODE (arg1) == REAL_CST
7526       && TREE_CODE (arg2) == REAL_CST
7527       && !TREE_CONSTANT_OVERFLOW (arg1)
7528       && !TREE_CONSTANT_OVERFLOW (arg2))
7529     {
7530       REAL_VALUE_TYPE c1, c2;
7531
7532       c1 = TREE_REAL_CST (arg1);
7533       c2 = TREE_REAL_CST (arg2);
7534       real_copysign (&c1, &c2);
7535       return build_real (type, c1);
7536       c1.sign = c2.sign;
7537     }
7538
7539   /* copysign(X, Y) is fabs(X) when Y is always non-negative.
7540      Remember to evaluate Y for side-effects.  */
7541   if (tree_expr_nonnegative_p (arg2))
7542     return omit_one_operand (type,
7543                              fold (build1 (ABS_EXPR, type, arg1)),
7544                              arg2);
7545
7546   /* Strip sign changing operations for the first argument.  */
7547   tem = fold_strip_sign_ops (arg1);
7548   if (tem)
7549     {
7550       arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
7551       return build_function_call_expr (fndecl, arglist);
7552     }
7553
7554   return NULL_TREE;
7555 }
7556
7557 /* Fold a call to builtin isascii.  */
7558
7559 static tree
7560 fold_builtin_isascii (tree arglist)
7561 {
7562   if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7563     return 0;
7564   else
7565     {
7566       /* Transform isascii(c) -> ((c & ~0x7f) == 0).  */
7567       tree arg = TREE_VALUE (arglist);
7568
7569       arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
7570                     build_int_cst (NULL_TREE,
7571                                    ~ (unsigned HOST_WIDE_INT) 0x7f));
7572       arg = fold (build2 (EQ_EXPR, integer_type_node,
7573                           arg, integer_zero_node));
7574
7575       if (in_gimple_form && !TREE_CONSTANT (arg))
7576         return NULL_TREE;
7577       else
7578         return arg;
7579     }
7580 }
7581
7582 /* Fold a call to builtin toascii.  */
7583
7584 static tree
7585 fold_builtin_toascii (tree arglist)
7586 {
7587   if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7588     return 0;
7589   else
7590     {
7591       /* Transform toascii(c) -> (c & 0x7f).  */
7592       tree arg = TREE_VALUE (arglist);
7593
7594       return fold (build2 (BIT_AND_EXPR, integer_type_node, arg,
7595                            build_int_cst (NULL_TREE, 0x7f)));
7596     }
7597 }
7598
7599 /* Fold a call to builtin isdigit.  */
7600
7601 static tree
7602 fold_builtin_isdigit (tree arglist)
7603 {
7604   if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7605     return 0;
7606   else
7607     {
7608       /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9.  */
7609       /* According to the C standard, isdigit is unaffected by locale.  */
7610       tree arg = TREE_VALUE (arglist);
7611       arg = fold_convert (unsigned_type_node, arg);
7612       arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
7613                     build_int_cst (unsigned_type_node, TARGET_DIGIT0));
7614       arg = build2 (LE_EXPR, integer_type_node, arg,
7615                     build_int_cst (unsigned_type_node, 9));
7616       arg = fold (arg);
7617       if (in_gimple_form && !TREE_CONSTANT (arg))
7618         return NULL_TREE;
7619       else
7620         return arg;
7621     }
7622 }
7623
7624 /* Fold a call to fabs, fabsf or fabsl.  */
7625
7626 static tree
7627 fold_builtin_fabs (tree arglist, tree type)
7628 {
7629   tree arg;
7630
7631   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7632     return 0;
7633
7634   arg = TREE_VALUE (arglist);
7635   arg = fold_convert (type, arg);
7636   if (TREE_CODE (arg) == REAL_CST)
7637     return fold_abs_const (arg, type);
7638   return fold (build1 (ABS_EXPR, type, arg));
7639 }
7640
7641 /* Fold a call to abs, labs, llabs or imaxabs.  */
7642
7643 static tree
7644 fold_builtin_abs (tree arglist, tree type)
7645 {
7646   tree arg;
7647
7648   if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7649     return 0;
7650
7651   arg = TREE_VALUE (arglist);
7652   arg = fold_convert (type, arg);
7653   if (TREE_CODE (arg) == INTEGER_CST)
7654     return fold_abs_const (arg, type);
7655   return fold (build1 (ABS_EXPR, type, arg));
7656 }
7657
7658 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
7659    EXP is the CALL_EXPR for the call.  */
7660
7661 static tree
7662 fold_builtin_classify (tree exp, int builtin_index)
7663 {
7664   tree fndecl = get_callee_fndecl (exp);
7665   tree arglist = TREE_OPERAND (exp, 1);
7666   tree type = TREE_TYPE (TREE_TYPE (fndecl));
7667   tree arg;
7668   REAL_VALUE_TYPE r;
7669
7670   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7671     {
7672       /* Check that we have exactly one argument.  */
7673       if (arglist == 0)
7674         {
7675           error ("too few arguments to function %qs",
7676                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7677           return error_mark_node;
7678         }
7679       else if (TREE_CHAIN (arglist) != 0)
7680         {
7681           error ("too many arguments to function %qs",
7682                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7683           return error_mark_node;
7684         }
7685       else
7686         {
7687           error ("non-floating-point argument to function %qs",
7688                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7689           return error_mark_node;
7690         }
7691     }
7692
7693   arg = TREE_VALUE (arglist);
7694   switch (builtin_index)
7695     {
7696     case BUILT_IN_ISINF:
7697       if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7698         return omit_one_operand (type, integer_zero_node, arg);
7699
7700       if (TREE_CODE (arg) == REAL_CST)
7701         {
7702           r = TREE_REAL_CST (arg);
7703           if (real_isinf (&r))
7704             return real_compare (GT_EXPR, &r, &dconst0)
7705                    ? integer_one_node : integer_minus_one_node;
7706           else
7707             return integer_zero_node;
7708         }
7709
7710       return NULL_TREE;
7711
7712     case BUILT_IN_FINITE:
7713       if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg)))
7714           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7715         return omit_one_operand (type, integer_zero_node, arg);
7716
7717       if (TREE_CODE (arg) == REAL_CST)
7718         {
7719           r = TREE_REAL_CST (arg);
7720           return real_isinf (&r) || real_isnan (&r)
7721                  ? integer_zero_node : integer_one_node;
7722         }
7723
7724       return NULL_TREE;
7725
7726     case BUILT_IN_ISNAN:
7727       if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg))))
7728         return omit_one_operand (type, integer_zero_node, arg);
7729
7730       if (TREE_CODE (arg) == REAL_CST)
7731         {
7732           r = TREE_REAL_CST (arg);
7733           return real_isnan (&r) ? integer_one_node : integer_zero_node;
7734         }
7735
7736       arg = builtin_save_expr (arg);
7737       return fold (build2 (UNORDERED_EXPR, type, arg, arg));
7738
7739     default:
7740       gcc_unreachable ();
7741     }
7742 }
7743
7744 /* Fold a call to an unordered comparison function such as
7745    __builtin_isgreater().  EXP is the CALL_EXPR for the call.
7746    UNORDERED_CODE and ORDERED_CODE are comparison codes that give
7747    the opposite of the desired result.  UNORDERED_CODE is used
7748    for modes that can hold NaNs and ORDERED_CODE is used for
7749    the rest.  */
7750
7751 static tree
7752 fold_builtin_unordered_cmp (tree exp,
7753                             enum tree_code unordered_code,
7754                             enum tree_code ordered_code)
7755 {
7756   tree fndecl = get_callee_fndecl (exp);
7757   tree arglist = TREE_OPERAND (exp, 1);
7758   tree type = TREE_TYPE (TREE_TYPE (fndecl));
7759   enum tree_code code;
7760   tree arg0, arg1;
7761   tree type0, type1;
7762   enum tree_code code0, code1;
7763   tree cmp_type = NULL_TREE;
7764
7765   if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7766     {
7767       /* Check that we have exactly two arguments.  */
7768       if (arglist == 0 || TREE_CHAIN (arglist) == 0)
7769         {
7770           error ("too few arguments to function %qs",
7771                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7772           return error_mark_node;
7773         }
7774       else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
7775         {
7776           error ("too many arguments to function %qs",
7777                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7778           return error_mark_node;
7779         }
7780     }
7781
7782   arg0 = TREE_VALUE (arglist);
7783   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7784   
7785   type0 = TREE_TYPE (arg0);
7786   type1 = TREE_TYPE (arg1);
7787   
7788   code0 = TREE_CODE (type0);
7789   code1 = TREE_CODE (type1);
7790   
7791   if (code0 == REAL_TYPE && code1 == REAL_TYPE)
7792     /* Choose the wider of two real types.  */
7793     cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
7794       ? type0 : type1;
7795   else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
7796     cmp_type = type0;
7797   else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
7798     cmp_type = type1;
7799   else
7800     {
7801       error ("non-floating-point argument to function %qs",
7802                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7803       return error_mark_node;
7804     }
7805   
7806   arg0 = fold_convert (cmp_type, arg0);
7807   arg1 = fold_convert (cmp_type, arg1);
7808
7809   if (unordered_code == UNORDERED_EXPR)
7810     {
7811       if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7812         return omit_two_operands (type, integer_zero_node, arg0, arg1);
7813       return fold (build2 (UNORDERED_EXPR, type, arg0, arg1));
7814     }
7815
7816   code = MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
7817                                                       : ordered_code;
7818   return fold (build1 (TRUTH_NOT_EXPR, type,
7819                        fold (build2 (code, type, arg0, arg1))));
7820 }
7821
7822 /* Used by constant folding to simplify calls to builtin functions.  EXP is
7823    the CALL_EXPR of a call to a builtin function.  IGNORE is true if the
7824    result of the function call is ignored.  This function returns NULL_TREE
7825    if no simplification was possible.  */
7826
7827 static tree
7828 fold_builtin_1 (tree exp, bool ignore)
7829 {
7830   tree fndecl = get_callee_fndecl (exp);
7831   tree arglist = TREE_OPERAND (exp, 1);
7832   tree type = TREE_TYPE (TREE_TYPE (fndecl));
7833
7834   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7835     return targetm.fold_builtin (exp, ignore);
7836
7837   switch (DECL_FUNCTION_CODE (fndecl))
7838     {
7839     case BUILT_IN_FPUTS:
7840       return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
7841
7842     case BUILT_IN_FPUTS_UNLOCKED:
7843       return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
7844
7845     case BUILT_IN_STRSTR:
7846       return fold_builtin_strstr (arglist);
7847
7848     case BUILT_IN_STRCAT:
7849       return fold_builtin_strcat (arglist);
7850
7851     case BUILT_IN_STRNCAT:
7852       return fold_builtin_strncat (arglist);
7853
7854     case BUILT_IN_STRSPN:
7855       return fold_builtin_strspn (arglist);
7856
7857     case BUILT_IN_STRCSPN:
7858       return fold_builtin_strcspn (arglist);
7859
7860     case BUILT_IN_STRCHR:
7861     case BUILT_IN_INDEX:
7862       return fold_builtin_strchr (arglist);
7863
7864     case BUILT_IN_STRRCHR:
7865     case BUILT_IN_RINDEX:
7866       return fold_builtin_strrchr (arglist);
7867
7868     case BUILT_IN_STRCPY:
7869       return fold_builtin_strcpy (exp, NULL_TREE);
7870
7871     case BUILT_IN_STRNCPY:
7872       return fold_builtin_strncpy (exp, NULL_TREE);
7873
7874     case BUILT_IN_STRCMP:
7875       return fold_builtin_strcmp (arglist);
7876
7877     case BUILT_IN_STRNCMP:
7878       return fold_builtin_strncmp (arglist);
7879
7880     case BUILT_IN_STRPBRK:
7881       return fold_builtin_strpbrk (arglist);
7882
7883     case BUILT_IN_BCMP:
7884     case BUILT_IN_MEMCMP:
7885       return fold_builtin_memcmp (arglist);
7886
7887     case BUILT_IN_SPRINTF:
7888       return fold_builtin_sprintf (arglist, ignore);
7889
7890     case BUILT_IN_CONSTANT_P:
7891       {
7892         tree val;
7893
7894         val = fold_builtin_constant_p (arglist);
7895         /* Gimplification will pull the CALL_EXPR for the builtin out of
7896            an if condition.  When not optimizing, we'll not CSE it back.
7897            To avoid link error types of regressions, return false now.  */
7898         if (!val && !optimize)
7899           val = integer_zero_node;
7900
7901         return val;
7902       }
7903
7904     case BUILT_IN_EXPECT:
7905       return fold_builtin_expect (arglist);
7906
7907     case BUILT_IN_CLASSIFY_TYPE:
7908       return fold_builtin_classify_type (arglist);
7909
7910     case BUILT_IN_STRLEN:
7911       return fold_builtin_strlen (arglist);
7912
7913     case BUILT_IN_FABS:
7914     case BUILT_IN_FABSF:
7915     case BUILT_IN_FABSL:
7916       return fold_builtin_fabs (arglist, type);
7917
7918     case BUILT_IN_ABS:
7919     case BUILT_IN_LABS:
7920     case BUILT_IN_LLABS:
7921     case BUILT_IN_IMAXABS:
7922       return fold_builtin_abs (arglist, type);
7923
7924     case BUILT_IN_CONJ:
7925     case BUILT_IN_CONJF:
7926     case BUILT_IN_CONJL:
7927       if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7928         return fold (build1 (CONJ_EXPR, type, TREE_VALUE (arglist)));
7929       break;
7930
7931     case BUILT_IN_CREAL:
7932     case BUILT_IN_CREALF:
7933     case BUILT_IN_CREALL:
7934       if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7935         return non_lvalue (fold (build1 (REALPART_EXPR, type,
7936                                          TREE_VALUE (arglist))));
7937       break;
7938
7939     case BUILT_IN_CIMAG:
7940     case BUILT_IN_CIMAGF:
7941     case BUILT_IN_CIMAGL:
7942       if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7943         return non_lvalue (fold (build1 (IMAGPART_EXPR, type,
7944                                          TREE_VALUE (arglist))));
7945       break;
7946
7947     case BUILT_IN_CABS:
7948     case BUILT_IN_CABSF:
7949     case BUILT_IN_CABSL:
7950       return fold_builtin_cabs (arglist, type);
7951
7952     case BUILT_IN_SQRT:
7953     case BUILT_IN_SQRTF:
7954     case BUILT_IN_SQRTL:
7955       return fold_builtin_sqrt (arglist, type);
7956
7957     case BUILT_IN_CBRT:
7958     case BUILT_IN_CBRTF:
7959     case BUILT_IN_CBRTL:
7960       return fold_builtin_cbrt (arglist, type);
7961
7962     case BUILT_IN_SIN:
7963     case BUILT_IN_SINF:
7964     case BUILT_IN_SINL:
7965       return fold_builtin_sin (arglist);
7966
7967     case BUILT_IN_COS:
7968     case BUILT_IN_COSF:
7969     case BUILT_IN_COSL:
7970       return fold_builtin_cos (arglist, type, fndecl);
7971
7972     case BUILT_IN_EXP:
7973     case BUILT_IN_EXPF:
7974     case BUILT_IN_EXPL:
7975       return fold_builtin_exponent (exp, &dconste);
7976
7977     case BUILT_IN_EXP2:
7978     case BUILT_IN_EXP2F:
7979     case BUILT_IN_EXP2L:
7980       return fold_builtin_exponent (exp, &dconst2);
7981
7982     case BUILT_IN_EXP10:
7983     case BUILT_IN_EXP10F:
7984     case BUILT_IN_EXP10L:
7985     case BUILT_IN_POW10:
7986     case BUILT_IN_POW10F:
7987     case BUILT_IN_POW10L:
7988       return fold_builtin_exponent (exp, &dconst10);
7989
7990     case BUILT_IN_LOG:
7991     case BUILT_IN_LOGF:
7992     case BUILT_IN_LOGL:
7993       return fold_builtin_logarithm (exp, &dconste);
7994
7995     case BUILT_IN_LOG2:
7996     case BUILT_IN_LOG2F:
7997     case BUILT_IN_LOG2L:
7998       return fold_builtin_logarithm (exp, &dconst2);
7999
8000     case BUILT_IN_LOG10:
8001     case BUILT_IN_LOG10F:
8002     case BUILT_IN_LOG10L:
8003       return fold_builtin_logarithm (exp, &dconst10);
8004
8005     case BUILT_IN_TAN:
8006     case BUILT_IN_TANF:
8007     case BUILT_IN_TANL:
8008       return fold_builtin_tan (arglist);
8009
8010     case BUILT_IN_ATAN:
8011     case BUILT_IN_ATANF:
8012     case BUILT_IN_ATANL:
8013       return fold_builtin_atan (arglist, type);
8014
8015     case BUILT_IN_POW:
8016     case BUILT_IN_POWF:
8017     case BUILT_IN_POWL:
8018       return fold_builtin_pow (fndecl, arglist, type);
8019
8020     case BUILT_IN_POWI:
8021     case BUILT_IN_POWIF:
8022     case BUILT_IN_POWIL:
8023       return fold_builtin_powi (fndecl, arglist, type);
8024
8025     case BUILT_IN_INF:
8026     case BUILT_IN_INFF:
8027     case BUILT_IN_INFL:
8028       return fold_builtin_inf (type, true);
8029
8030     case BUILT_IN_HUGE_VAL:
8031     case BUILT_IN_HUGE_VALF:
8032     case BUILT_IN_HUGE_VALL:
8033       return fold_builtin_inf (type, false);
8034
8035     case BUILT_IN_NAN:
8036     case BUILT_IN_NANF:
8037     case BUILT_IN_NANL:
8038       return fold_builtin_nan (arglist, type, true);
8039
8040     case BUILT_IN_NANS:
8041     case BUILT_IN_NANSF:
8042     case BUILT_IN_NANSL:
8043       return fold_builtin_nan (arglist, type, false);
8044
8045     case BUILT_IN_FLOOR:
8046     case BUILT_IN_FLOORF:
8047     case BUILT_IN_FLOORL:
8048       return fold_builtin_floor (exp);
8049
8050     case BUILT_IN_CEIL:
8051     case BUILT_IN_CEILF:
8052     case BUILT_IN_CEILL:
8053       return fold_builtin_ceil (exp);
8054
8055     case BUILT_IN_TRUNC:
8056     case BUILT_IN_TRUNCF:
8057     case BUILT_IN_TRUNCL:
8058       return fold_builtin_trunc (exp);
8059
8060     case BUILT_IN_ROUND:
8061     case BUILT_IN_ROUNDF:
8062     case BUILT_IN_ROUNDL:
8063       return fold_builtin_round (exp);
8064
8065     case BUILT_IN_NEARBYINT:
8066     case BUILT_IN_NEARBYINTF:
8067     case BUILT_IN_NEARBYINTL:
8068     case BUILT_IN_RINT:
8069     case BUILT_IN_RINTF:
8070     case BUILT_IN_RINTL:
8071       return fold_trunc_transparent_mathfn (exp);
8072
8073     case BUILT_IN_LROUND:
8074     case BUILT_IN_LROUNDF:
8075     case BUILT_IN_LROUNDL:
8076     case BUILT_IN_LLROUND:
8077     case BUILT_IN_LLROUNDF:
8078     case BUILT_IN_LLROUNDL:
8079       return fold_builtin_lround (exp);
8080
8081     case BUILT_IN_LRINT:
8082     case BUILT_IN_LRINTF:
8083     case BUILT_IN_LRINTL:
8084     case BUILT_IN_LLRINT:
8085     case BUILT_IN_LLRINTF:
8086     case BUILT_IN_LLRINTL:
8087       return fold_fixed_mathfn (exp);
8088
8089     case BUILT_IN_FFS:
8090     case BUILT_IN_FFSL:
8091     case BUILT_IN_FFSLL:
8092     case BUILT_IN_CLZ:
8093     case BUILT_IN_CLZL:
8094     case BUILT_IN_CLZLL:
8095     case BUILT_IN_CTZ:
8096     case BUILT_IN_CTZL:
8097     case BUILT_IN_CTZLL:
8098     case BUILT_IN_POPCOUNT:
8099     case BUILT_IN_POPCOUNTL:
8100     case BUILT_IN_POPCOUNTLL:
8101     case BUILT_IN_PARITY:
8102     case BUILT_IN_PARITYL:
8103     case BUILT_IN_PARITYLL:
8104       return fold_builtin_bitop (exp);
8105
8106     case BUILT_IN_MEMCPY:
8107       return fold_builtin_memcpy (exp);
8108
8109     case BUILT_IN_MEMPCPY:
8110       return fold_builtin_mempcpy (arglist, type, /*endp=*/1);
8111
8112     case BUILT_IN_MEMMOVE:
8113       return fold_builtin_memmove (arglist, type);
8114
8115     case BUILT_IN_SIGNBIT:
8116     case BUILT_IN_SIGNBITF:
8117     case BUILT_IN_SIGNBITL:
8118       return fold_builtin_signbit (exp);
8119
8120     case BUILT_IN_ISASCII:
8121       return fold_builtin_isascii (arglist);
8122
8123     case BUILT_IN_TOASCII:
8124       return fold_builtin_toascii (arglist);
8125
8126     case BUILT_IN_ISDIGIT:
8127       return fold_builtin_isdigit (arglist);
8128
8129     case BUILT_IN_COPYSIGN:
8130     case BUILT_IN_COPYSIGNF:
8131     case BUILT_IN_COPYSIGNL:
8132       return fold_builtin_copysign (fndecl, arglist, type);
8133
8134     case BUILT_IN_FINITE:
8135     case BUILT_IN_FINITEF:
8136     case BUILT_IN_FINITEL:
8137       return fold_builtin_classify (exp, BUILT_IN_FINITE);
8138
8139     case BUILT_IN_ISINF:
8140     case BUILT_IN_ISINFF:
8141     case BUILT_IN_ISINFL:
8142       return fold_builtin_classify (exp, BUILT_IN_ISINF);
8143
8144     case BUILT_IN_ISNAN:
8145     case BUILT_IN_ISNANF:
8146     case BUILT_IN_ISNANL:
8147       return fold_builtin_classify (exp, BUILT_IN_ISNAN);
8148
8149     case BUILT_IN_ISGREATER:
8150       return fold_builtin_unordered_cmp (exp, UNLE_EXPR, LE_EXPR);
8151     case BUILT_IN_ISGREATEREQUAL:
8152       return fold_builtin_unordered_cmp (exp, UNLT_EXPR, LT_EXPR);
8153     case BUILT_IN_ISLESS:
8154       return fold_builtin_unordered_cmp (exp, UNGE_EXPR, GE_EXPR);
8155     case BUILT_IN_ISLESSEQUAL:
8156       return fold_builtin_unordered_cmp (exp, UNGT_EXPR, GT_EXPR);
8157     case BUILT_IN_ISLESSGREATER:
8158       return fold_builtin_unordered_cmp (exp, UNEQ_EXPR, EQ_EXPR);
8159     case BUILT_IN_ISUNORDERED:
8160       return fold_builtin_unordered_cmp (exp, UNORDERED_EXPR, NOP_EXPR);
8161
8162       /* We do the folding for va_start in the expander.  */
8163     case BUILT_IN_VA_START:
8164       break;
8165
8166     default:
8167       break;
8168     }
8169
8170   return 0;
8171 }
8172
8173 /* A wrapper function for builtin folding that prevents warnings for
8174    "statement without effect" and the like, caused by removing the
8175    call node earlier than the warning is generated.  */
8176
8177 tree
8178 fold_builtin (tree exp, bool ignore)
8179 {
8180   exp = fold_builtin_1 (exp, ignore);
8181   if (exp)
8182     {
8183       /* ??? Don't clobber shared nodes such as integer_zero_node.  */
8184       if (CONSTANT_CLASS_P (exp))
8185         exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
8186       TREE_NO_WARNING (exp) = 1;
8187     }
8188
8189   return exp;
8190 }
8191
8192 /* Conveniently construct a function call expression.  */
8193
8194 tree
8195 build_function_call_expr (tree fn, tree arglist)
8196 {
8197   tree call_expr;
8198
8199   call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
8200   call_expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
8201                       call_expr, arglist, NULL_TREE);
8202   return fold (call_expr);
8203 }
8204
8205 /* This function validates the types of a function call argument list
8206    represented as a tree chain of parameters against a specified list
8207    of tree_codes.  If the last specifier is a 0, that represents an
8208    ellipses, otherwise the last specifier must be a VOID_TYPE.  */
8209
8210 static int
8211 validate_arglist (tree arglist, ...)
8212 {
8213   enum tree_code code;
8214   int res = 0;
8215   va_list ap;
8216
8217   va_start (ap, arglist);
8218
8219   do
8220     {
8221       code = va_arg (ap, enum tree_code);
8222       switch (code)
8223         {
8224         case 0:
8225           /* This signifies an ellipses, any further arguments are all ok.  */
8226           res = 1;
8227           goto end;
8228         case VOID_TYPE:
8229           /* This signifies an endlink, if no arguments remain, return
8230              true, otherwise return false.  */
8231           res = arglist == 0;
8232           goto end;
8233         default:
8234           /* If no parameters remain or the parameter's code does not
8235              match the specified code, return false.  Otherwise continue
8236              checking any remaining arguments.  */
8237           if (arglist == 0)
8238             goto end;
8239           if (code == POINTER_TYPE)
8240             {
8241               if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
8242                 goto end;
8243             }
8244           else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
8245             goto end;
8246           break;
8247         }
8248       arglist = TREE_CHAIN (arglist);
8249     }
8250   while (1);
8251
8252   /* We need gotos here since we can only have one VA_CLOSE in a
8253      function.  */
8254  end: ;
8255   va_end (ap);
8256
8257   return res;
8258 }
8259
8260 /* Default target-specific builtin expander that does nothing.  */
8261
8262 rtx
8263 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
8264                         rtx target ATTRIBUTE_UNUSED,
8265                         rtx subtarget ATTRIBUTE_UNUSED,
8266                         enum machine_mode mode ATTRIBUTE_UNUSED,
8267                         int ignore ATTRIBUTE_UNUSED)
8268 {
8269   return NULL_RTX;
8270 }
8271
8272 /* Returns true is EXP represents data that would potentially reside
8273    in a readonly section.  */
8274
8275 static bool
8276 readonly_data_expr (tree exp)
8277 {
8278   STRIP_NOPS (exp);
8279
8280   if (TREE_CODE (exp) != ADDR_EXPR)
8281     return false;
8282
8283   exp = get_base_address (TREE_OPERAND (exp, 0));
8284   if (!exp)
8285     return false;
8286
8287   /* Make sure we call decl_readonly_section only for trees it
8288      can handle (since it returns true for everything it doesn't
8289      understand).  */
8290   if (TREE_CODE (exp) == STRING_CST
8291       || TREE_CODE (exp) == CONSTRUCTOR
8292       || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
8293     return decl_readonly_section (exp, 0);
8294   else
8295     return false;
8296 }
8297
8298 /* Simplify a call to the strstr builtin.
8299
8300    Return 0 if no simplification was possible, otherwise return the
8301    simplified form of the call as a tree.
8302
8303    The simplified form may be a constant or other expression which
8304    computes the same value, but in a more efficient manner (including
8305    calls to other builtin functions).
8306
8307    The call may contain arguments which need to be evaluated, but
8308    which are not useful to determine the result of the call.  In
8309    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
8310    COMPOUND_EXPR will be an argument which must be evaluated.
8311    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
8312    COMPOUND_EXPR in the chain will contain the tree for the simplified
8313    form of the builtin function call.  */
8314
8315 static tree
8316 fold_builtin_strstr (tree arglist)
8317 {
8318   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8319     return 0;
8320   else
8321     {
8322       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8323       tree fn;
8324       const char *p1, *p2;
8325
8326       p2 = c_getstr (s2);
8327       if (p2 == NULL)
8328         return 0;
8329
8330       p1 = c_getstr (s1);
8331       if (p1 != NULL)
8332         {
8333           const char *r = strstr (p1, p2);
8334
8335           if (r == NULL)
8336             return build_int_cst (TREE_TYPE (s1), 0);
8337
8338           /* Return an offset into the constant string argument.  */
8339           return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8340                                s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8341         }
8342
8343       if (p2[0] == '\0')
8344         return s1;
8345
8346       if (p2[1] != '\0')
8347         return 0;
8348
8349       fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8350       if (!fn)
8351         return 0;
8352
8353       /* New argument list transforming strstr(s1, s2) to
8354          strchr(s1, s2[0]).  */
8355       arglist = build_tree_list (NULL_TREE,
8356                                  build_int_cst (NULL_TREE, p2[0]));
8357       arglist = tree_cons (NULL_TREE, s1, arglist);
8358       return build_function_call_expr (fn, arglist);
8359     }
8360 }
8361
8362 /* Simplify a call to the strchr builtin.
8363
8364    Return 0 if no simplification was possible, otherwise return the
8365    simplified form of the call as a tree.
8366
8367    The simplified form may be a constant or other expression which
8368    computes the same value, but in a more efficient manner (including
8369    calls to other builtin functions).
8370
8371    The call may contain arguments which need to be evaluated, but
8372    which are not useful to determine the result of the call.  In
8373    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
8374    COMPOUND_EXPR will be an argument which must be evaluated.
8375    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
8376    COMPOUND_EXPR in the chain will contain the tree for the simplified
8377    form of the builtin function call.  */
8378
8379 static tree
8380 fold_builtin_strchr (tree arglist)
8381 {
8382   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8383     return 0;
8384   else
8385     {
8386       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8387       const char *p1;
8388
8389       if (TREE_CODE (s2) != INTEGER_CST)
8390         return 0;
8391
8392       p1 = c_getstr (s1);
8393       if (p1 != NULL)
8394         {
8395           char c;
8396           const char *r;
8397
8398           if (target_char_cast (s2, &c))
8399             return 0;
8400
8401           r = strchr (p1, c);
8402
8403           if (r == NULL)
8404             return build_int_cst (TREE_TYPE (s1), 0);
8405
8406           /* Return an offset into the constant string argument.  */
8407           return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8408                                s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8409         }
8410       return 0;
8411     }
8412 }
8413
8414 /* Simplify a call to the strrchr builtin.
8415
8416    Return 0 if no simplification was possible, otherwise return the
8417    simplified form of the call as a tree.
8418
8419    The simplified form may be a constant or other expression which
8420    computes the same value, but in a more efficient manner (including
8421    calls to other builtin functions).
8422
8423    The call may contain arguments which need to be evaluated, but
8424    which are not useful to determine the result of the call.  In
8425    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
8426    COMPOUND_EXPR will be an argument which must be evaluated.
8427    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
8428    COMPOUND_EXPR in the chain will contain the tree for the simplified
8429    form of the builtin function call.  */
8430
8431 static tree
8432 fold_builtin_strrchr (tree arglist)
8433 {
8434   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8435     return 0;
8436   else
8437     {
8438       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8439       tree fn;
8440       const char *p1;
8441
8442       if (TREE_CODE (s2) != INTEGER_CST)
8443         return 0;
8444
8445       p1 = c_getstr (s1);
8446       if (p1 != NULL)
8447         {
8448           char c;
8449           const char *r;
8450
8451           if (target_char_cast (s2, &c))
8452             return 0;
8453
8454           r = strrchr (p1, c);
8455
8456           if (r == NULL)
8457             return build_int_cst (TREE_TYPE (s1), 0);
8458
8459           /* Return an offset into the constant string argument.  */
8460           return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8461                                s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8462         }
8463
8464       if (! integer_zerop (s2))
8465         return 0;
8466
8467       fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8468       if (!fn)
8469         return 0;
8470
8471       /* Transform strrchr(s1, '\0') to strchr(s1, '\0').  */
8472       return build_function_call_expr (fn, arglist);
8473     }
8474 }
8475
8476 /* Simplify a call to the strpbrk builtin.
8477
8478    Return 0 if no simplification was possible, otherwise return the
8479    simplified form of the call as a tree.
8480
8481    The simplified form may be a constant or other expression which
8482    computes the same value, but in a more efficient manner (including
8483    calls to other builtin functions).
8484
8485    The call may contain arguments which need to be evaluated, but
8486    which are not useful to determine the result of the call.  In
8487    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
8488    COMPOUND_EXPR will be an argument which must be evaluated.
8489    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
8490    COMPOUND_EXPR in the chain will contain the tree for the simplified
8491    form of the builtin function call.  */
8492
8493 static tree
8494 fold_builtin_strpbrk (tree arglist)
8495 {
8496   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8497     return 0;
8498   else
8499     {
8500       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8501       tree fn;
8502       const char *p1, *p2;
8503
8504       p2 = c_getstr (s2);
8505       if (p2 == NULL)
8506         return 0;
8507
8508       p1 = c_getstr (s1);
8509       if (p1 != NULL)
8510         {
8511           const char *r = strpbrk (p1, p2);
8512
8513           if (r == NULL)
8514             return build_int_cst (TREE_TYPE (s1), 0);
8515
8516           /* Return an offset into the constant string argument.  */
8517           return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8518                                s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8519         }
8520
8521       if (p2[0] == '\0')
8522         /* strpbrk(x, "") == NULL.
8523            Evaluate and ignore s1 in case it had side-effects.  */
8524         return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
8525
8526       if (p2[1] != '\0')
8527         return 0;  /* Really call strpbrk.  */
8528
8529       fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8530       if (!fn)
8531         return 0;
8532
8533       /* New argument list transforming strpbrk(s1, s2) to
8534          strchr(s1, s2[0]).  */
8535       arglist = build_tree_list (NULL_TREE,
8536                                  build_int_cst (NULL_TREE, p2[0]));
8537       arglist = tree_cons (NULL_TREE, s1, arglist);
8538       return build_function_call_expr (fn, arglist);
8539     }
8540 }
8541
8542 /* Simplify a call to the strcat builtin.
8543
8544    Return 0 if no simplification was possible, otherwise return the
8545    simplified form of the call as a tree.
8546
8547    The simplified form may be a constant or other expression which
8548    computes the same value, but in a more efficient manner (including
8549    calls to other builtin functions).
8550
8551    The call may contain arguments which need to be evaluated, but
8552    which are not useful to determine the result of the call.  In
8553    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
8554    COMPOUND_EXPR will be an argument which must be evaluated.
8555    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
8556    COMPOUND_EXPR in the chain will contain the tree for the simplified
8557    form of the builtin function call.  */
8558
8559 static tree
8560 fold_builtin_strcat (tree arglist)
8561 {
8562   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8563     return 0;
8564   else
8565     {
8566       tree dst = TREE_VALUE (arglist),
8567         src = TREE_VALUE (TREE_CHAIN (arglist));
8568       const char *p = c_getstr (src);
8569
8570       /* If the string length is zero, return the dst parameter.  */
8571       if (p && *p == '\0')
8572         return dst;
8573
8574       return 0;
8575     }
8576 }
8577
8578 /* Simplify a call to the strncat builtin.
8579
8580    Return 0 if no simplification was possible, otherwise return the
8581    simplified form of the call as a tree.
8582
8583    The simplified form may be a constant or other expression which
8584    computes the same value, but in a more efficient manner (including
8585    calls to other builtin functions).
8586
8587    The call may contain arguments which need to be evaluated, but
8588    which are not useful to determine the result of the call.  In
8589    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
8590    COMPOUND_EXPR will be an argument which must be evaluated.
8591    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
8592    COMPOUND_EXPR in the chain will contain the tree for the simplified
8593    form of the builtin function call.  */
8594
8595 static tree
8596 fold_builtin_strncat (tree arglist)
8597 {
8598   if (!validate_arglist (arglist,
8599                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8600     return 0;
8601   else
8602     {
8603       tree dst = TREE_VALUE (arglist);
8604       tree src = TREE_VALUE (TREE_CHAIN (arglist));
8605       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8606       const char *p = c_getstr (src);
8607
8608       /* If the requested length is zero, or the src parameter string
8609           length is zero, return the dst parameter.  */
8610       if (integer_zerop (len) || (p && *p == '\0'))
8611         return omit_two_operands (TREE_TYPE (dst), dst, src, len);
8612
8613       /* If the requested len is greater than or equal to the string
8614          length, call strcat.  */
8615       if (TREE_CODE (len) == INTEGER_CST && p
8616           && compare_tree_int (len, strlen (p)) >= 0)
8617         {
8618           tree newarglist
8619             = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
8620           tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
8621
8622           /* If the replacement _DECL isn't initialized, don't do the
8623              transformation.  */
8624           if (!fn)
8625             return 0;
8626
8627           return build_function_call_expr (fn, newarglist);
8628         }
8629       return 0;
8630     }
8631 }
8632
8633 /* Simplify a call to the strspn builtin.
8634
8635    Return 0 if no simplification was possible, otherwise return the
8636    simplified form of the call as a tree.
8637
8638    The simplified form may be a constant or other expression which
8639    computes the same value, but in a more efficient manner (including
8640    calls to other builtin functions).
8641
8642    The call may contain arguments which need to be evaluated, but
8643    which are not useful to determine the result of the call.  In
8644    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
8645    COMPOUND_EXPR will be an argument which must be evaluated.
8646    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
8647    COMPOUND_EXPR in the chain will contain the tree for the simplified
8648    form of the builtin function call.  */
8649
8650 static tree
8651 fold_builtin_strspn (tree arglist)
8652 {
8653   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8654     return 0;
8655   else
8656     {
8657       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8658       const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8659
8660       /* If both arguments are constants, evaluate at compile-time.  */
8661       if (p1 && p2)
8662         {
8663           const size_t r = strspn (p1, p2);
8664           return size_int (r);
8665         }
8666
8667       /* If either argument is "", return 0.  */
8668       if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
8669         /* Evaluate and ignore both arguments in case either one has
8670            side-effects.  */
8671         return omit_two_operands (integer_type_node, integer_zero_node,
8672                                   s1, s2);
8673       return 0;
8674     }
8675 }
8676
8677 /* Simplify a call to the strcspn builtin.
8678
8679    Return 0 if no simplification was possible, otherwise return the
8680    simplified form of the call as a tree.
8681
8682    The simplified form may be a constant or other expression which
8683    computes the same value, but in a more efficient manner (including
8684    calls to other builtin functions).
8685
8686    The call may contain arguments which need to be evaluated, but
8687    which are not useful to determine the result of the call.  In
8688    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
8689    COMPOUND_EXPR will be an argument which must be evaluated.
8690    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
8691    COMPOUND_EXPR in the chain will contain the tree for the simplified
8692    form of the builtin function call.  */
8693
8694 static tree
8695 fold_builtin_strcspn (tree arglist)
8696 {
8697   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8698     return 0;
8699   else
8700     {
8701       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8702       const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8703
8704       /* If both arguments are constants, evaluate at compile-time.  */
8705       if (p1 && p2)
8706         {
8707           const size_t r = strcspn (p1, p2);
8708           return size_int (r);
8709         }
8710
8711       /* If the first argument is "", return 0.  */
8712       if (p1 && *p1 == '\0')
8713         {
8714           /* Evaluate and ignore argument s2 in case it has
8715              side-effects.  */
8716           return omit_one_operand (integer_type_node,
8717                                    integer_zero_node, s2);
8718         }
8719
8720       /* If the second argument is "", return __builtin_strlen(s1).  */
8721       if (p2 && *p2 == '\0')
8722         {
8723           tree newarglist = build_tree_list (NULL_TREE, s1),
8724             fn = implicit_built_in_decls[BUILT_IN_STRLEN];
8725
8726           /* If the replacement _DECL isn't initialized, don't do the
8727              transformation.  */
8728           if (!fn)
8729             return 0;
8730
8731           return build_function_call_expr (fn, newarglist);
8732         }
8733       return 0;
8734     }
8735 }
8736
8737 /* Fold a call to the fputs builtin.  IGNORE is true if the value returned
8738    by the builtin will be ignored.  UNLOCKED is true is true if this
8739    actually a call to fputs_unlocked.  If LEN in non-NULL, it represents
8740    the known length of the string.  Return NULL_TREE if no simplification
8741    was possible.  */
8742
8743 tree
8744 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
8745 {
8746   tree fn;
8747   tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
8748     : implicit_built_in_decls[BUILT_IN_FPUTC];
8749   tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
8750     : implicit_built_in_decls[BUILT_IN_FWRITE];
8751
8752   /* If the return value is used, or the replacement _DECL isn't
8753      initialized, don't do the transformation.  */
8754   if (!ignore || !fn_fputc || !fn_fwrite)
8755     return 0;
8756
8757   /* Verify the arguments in the original call.  */
8758   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8759     return 0;
8760
8761   if (! len)
8762     len = c_strlen (TREE_VALUE (arglist), 0);
8763
8764   /* Get the length of the string passed to fputs.  If the length
8765      can't be determined, punt.  */
8766   if (!len
8767       || TREE_CODE (len) != INTEGER_CST)
8768     return 0;
8769
8770   switch (compare_tree_int (len, 1))
8771     {
8772     case -1: /* length is 0, delete the call entirely .  */
8773       return omit_one_operand (integer_type_node, integer_zero_node,
8774                                TREE_VALUE (TREE_CHAIN (arglist)));
8775
8776     case 0: /* length is 1, call fputc.  */
8777       {
8778         const char *p = c_getstr (TREE_VALUE (arglist));
8779
8780         if (p != NULL)
8781           {
8782             /* New argument list transforming fputs(string, stream) to
8783                fputc(string[0], stream).  */
8784             arglist = build_tree_list (NULL_TREE,
8785                                        TREE_VALUE (TREE_CHAIN (arglist)));
8786             arglist = tree_cons (NULL_TREE,
8787                                  build_int_cst (NULL_TREE, p[0]),
8788                                  arglist);
8789             fn = fn_fputc;
8790             break;
8791           }
8792       }
8793       /* FALLTHROUGH */
8794     case 1: /* length is greater than 1, call fwrite.  */
8795       {
8796         tree string_arg;
8797
8798         /* If optimizing for size keep fputs.  */
8799         if (optimize_size)
8800           return 0;
8801         string_arg = TREE_VALUE (arglist);
8802         /* New argument list transforming fputs(string, stream) to
8803            fwrite(string, 1, len, stream).  */
8804         arglist = build_tree_list (NULL_TREE,
8805                                    TREE_VALUE (TREE_CHAIN (arglist)));
8806         arglist = tree_cons (NULL_TREE, len, arglist);
8807         arglist = tree_cons (NULL_TREE, size_one_node, arglist);
8808         arglist = tree_cons (NULL_TREE, string_arg, arglist);
8809         fn = fn_fwrite;
8810         break;
8811       }
8812     default:
8813       gcc_unreachable ();
8814     }
8815
8816   /* These optimizations are only performed when the result is ignored,
8817      hence there's no need to cast the result to integer_type_node.  */
8818   return build_function_call_expr (fn, arglist);
8819 }
8820
8821 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
8822    produced.  False otherwise.  This is done so that we don't output the error
8823    or warning twice or three times.  */
8824 bool
8825 fold_builtin_next_arg (tree arglist)
8826 {
8827   tree fntype = TREE_TYPE (current_function_decl);
8828
8829   if (TYPE_ARG_TYPES (fntype) == 0
8830       || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
8831           == void_type_node))
8832     {
8833       error ("%<va_start%> used in function with fixed args");
8834       return true;
8835     }
8836   else if (!arglist)
8837     {
8838       /* Evidently an out of date version of <stdarg.h>; can't validate
8839          va_start's second argument, but can still work as intended.  */
8840       warning ("%<__builtin_next_arg%> called without an argument");
8841       return true;
8842     }
8843   /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
8844      when we checked the arguments and if needed issued a warning.  */
8845   else if (!TREE_CHAIN (arglist)
8846            || !integer_zerop (TREE_VALUE (arglist))
8847            || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
8848            || TREE_CHAIN (TREE_CHAIN (arglist)))
8849     {
8850       tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
8851       tree arg = TREE_VALUE (arglist);
8852
8853       if (TREE_CHAIN (arglist))
8854         {
8855           error ("%<va_start%> used with too many arguments");
8856           return true;
8857         }
8858
8859       /* Strip off all nops for the sake of the comparison.  This
8860          is not quite the same as STRIP_NOPS.  It does more.
8861          We must also strip off INDIRECT_EXPR for C++ reference
8862          parameters.  */
8863       while (TREE_CODE (arg) == NOP_EXPR
8864              || TREE_CODE (arg) == CONVERT_EXPR
8865              || TREE_CODE (arg) == NON_LVALUE_EXPR
8866              || TREE_CODE (arg) == INDIRECT_REF)
8867         arg = TREE_OPERAND (arg, 0);
8868       if (arg != last_parm)
8869         {
8870           /* FIXME: Sometimes with the tree optimizers we can get the
8871              not the last argument even though the user used the last
8872              argument.  We just warn and set the arg to be the last
8873              argument so that we will get wrong-code because of
8874              it.  */
8875           warning ("second parameter of %<va_start%> not last named argument");
8876         }
8877       /* We want to verify the second parameter just once before the tree
8878          optimizers are run and then avoid keeping it in the tree,
8879          as otherwise we could warn even for correct code like:
8880          void foo (int i, ...)
8881          { va_list ap; i++; va_start (ap, i); va_end (ap); }  */
8882       TREE_VALUE (arglist) = integer_zero_node;
8883       TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
8884     }
8885   return false;
8886 }
8887
8888
8889 /* Simplify a call to the sprintf builtin.
8890
8891    Return 0 if no simplification was possible, otherwise return the
8892    simplified form of the call as a tree.  If IGNORED is true, it means that
8893    the caller does not use the returned value of the function.  */
8894
8895 static tree
8896 fold_builtin_sprintf (tree arglist, int ignored)
8897 {
8898   tree call, retval, dest, fmt;
8899   const char *fmt_str = NULL;
8900
8901   /* Verify the required arguments in the original call.  We deal with two
8902      types of sprintf() calls: 'sprintf (str, fmt)' and
8903      'sprintf (dest, "%s", orig)'.  */
8904   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
8905       && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
8906                             VOID_TYPE))
8907     return NULL_TREE;
8908
8909   /* Get the destination string and the format specifier.  */
8910   dest = TREE_VALUE (arglist);
8911   fmt = TREE_VALUE (TREE_CHAIN (arglist));
8912
8913   /* Check whether the format is a literal string constant.  */
8914   fmt_str = c_getstr (fmt);
8915   if (fmt_str == NULL)
8916     return NULL_TREE;
8917
8918   call = NULL_TREE;
8919   retval = NULL_TREE;
8920
8921   /* If the format doesn't contain % args or %%, use strcpy.  */
8922   if (strchr (fmt_str, '%') == NULL)
8923     {
8924       tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
8925
8926       if (!fn)
8927         return NULL_TREE;
8928
8929       /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
8930          'format' is known to contain no % formats.  */
8931       arglist = build_tree_list (NULL_TREE, fmt);
8932       arglist = tree_cons (NULL_TREE, dest, arglist);
8933       call = build_function_call_expr (fn, arglist);
8934       if (!ignored)
8935         retval = build_int_cst (NULL_TREE, strlen (fmt_str));
8936     }
8937
8938   /* If the format is "%s", use strcpy if the result isn't used.  */
8939   else if (fmt_str && strcmp (fmt_str, "%s") == 0)
8940     {
8941       tree fn, orig;
8942       fn = implicit_built_in_decls[BUILT_IN_STRCPY];
8943
8944       if (!fn)
8945         return NULL_TREE;
8946
8947       /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2).  */
8948       orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8949       arglist = build_tree_list (NULL_TREE, orig);
8950       arglist = tree_cons (NULL_TREE, dest, arglist);
8951       if (!ignored)
8952         {
8953           retval = c_strlen (orig, 1);
8954           if (!retval || TREE_CODE (retval) != INTEGER_CST)
8955             return NULL_TREE;
8956         }
8957       call = build_function_call_expr (fn, arglist);
8958     }
8959
8960   if (call && retval)
8961     {
8962       retval = convert
8963         (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
8964          retval);
8965       return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
8966     }
8967   else
8968     return call;
8969 }