OSDN Git Service

* optabs.h (enum optab_index): Rename OTI_drem to OTI_remainder.
[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, 2006 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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 #ifndef PAD_VARARGS_DOWN
52 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
53 #endif
54
55 /* Define the names of the builtin function types and codes.  */
56 const char *const built_in_class_names[4]
57   = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
58
59 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
60 const char * built_in_names[(int) END_BUILTINS] =
61 {
62 #include "builtins.def"
63 };
64 #undef DEF_BUILTIN
65
66 /* Setup an array of _DECL trees, make sure each element is
67    initialized to NULL_TREE.  */
68 tree built_in_decls[(int) END_BUILTINS];
69 /* Declarations used when constructing the builtin implicitly in the compiler.
70    It may be NULL_TREE when this is invalid (for instance runtime is not
71    required to implement the function call in all cases).  */
72 tree implicit_built_in_decls[(int) END_BUILTINS];
73
74 static int get_pointer_alignment (tree, unsigned int);
75 static const char *c_getstr (tree);
76 static rtx c_readstr (const char *, enum machine_mode);
77 static int target_char_cast (tree, char *);
78 static rtx get_memory_rtx (tree, tree);
79 static int apply_args_size (void);
80 static int apply_result_size (void);
81 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
82 static rtx result_vector (int, rtx);
83 #endif
84 static void expand_builtin_update_setjmp_buf (rtx);
85 static void expand_builtin_prefetch (tree);
86 static rtx expand_builtin_apply_args (void);
87 static rtx expand_builtin_apply_args_1 (void);
88 static rtx expand_builtin_apply (rtx, rtx, rtx);
89 static void expand_builtin_return (rtx);
90 static enum type_class type_to_class (tree);
91 static rtx expand_builtin_classify_type (tree);
92 static void expand_errno_check (tree, rtx);
93 static rtx expand_builtin_mathfn (tree, rtx, rtx);
94 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
95 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
96 static rtx expand_builtin_sincos (tree);
97 static rtx expand_builtin_int_roundingfn (tree, rtx, rtx);
98 static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx);
99 static rtx expand_builtin_args_info (tree);
100 static rtx expand_builtin_next_arg (void);
101 static rtx expand_builtin_va_start (tree);
102 static rtx expand_builtin_va_end (tree);
103 static rtx expand_builtin_va_copy (tree);
104 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
105 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
106 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
107 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
108 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
109 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
110 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
114 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
115 static rtx expand_builtin_bcopy (tree);
116 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
118 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
119 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
120 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
122 static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree);
123 static rtx expand_builtin_bzero (tree);
124 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_alloca (tree, rtx);
130 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
131 static rtx expand_builtin_frame_address (tree, tree);
132 static rtx expand_builtin_fputs (tree, rtx, bool);
133 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
134 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
135 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
136 static tree stabilize_va_list (tree, int);
137 static rtx expand_builtin_expect (tree, rtx);
138 static tree fold_builtin_constant_p (tree);
139 static tree fold_builtin_classify_type (tree);
140 static tree fold_builtin_strlen (tree);
141 static tree fold_builtin_inf (tree, int);
142 static tree fold_builtin_nan (tree, tree, int);
143 static int validate_arglist (tree, ...);
144 static bool integer_valued_real_p (tree);
145 static tree fold_trunc_transparent_mathfn (tree, tree);
146 static bool readonly_data_expr (tree);
147 static rtx expand_builtin_fabs (tree, rtx, rtx);
148 static rtx expand_builtin_signbit (tree, rtx);
149 static tree fold_builtin_sqrt (tree, tree);
150 static tree fold_builtin_cbrt (tree, tree);
151 static tree fold_builtin_pow (tree, tree, tree);
152 static tree fold_builtin_powi (tree, tree, tree);
153 static tree fold_builtin_cos (tree, tree, tree);
154 static tree fold_builtin_tan (tree, tree);
155 static tree fold_builtin_trunc (tree, tree);
156 static tree fold_builtin_floor (tree, tree);
157 static tree fold_builtin_ceil (tree, tree);
158 static tree fold_builtin_round (tree, tree);
159 static tree fold_builtin_int_roundingfn (tree, tree);
160 static tree fold_builtin_bitop (tree, tree);
161 static tree fold_builtin_memory_op (tree, tree, bool, int);
162 static tree fold_builtin_strchr (tree, tree);
163 static tree fold_builtin_memcmp (tree);
164 static tree fold_builtin_strcmp (tree);
165 static tree fold_builtin_strncmp (tree);
166 static tree fold_builtin_signbit (tree, tree);
167 static tree fold_builtin_copysign (tree, tree, tree);
168 static tree fold_builtin_isascii (tree);
169 static tree fold_builtin_toascii (tree);
170 static tree fold_builtin_isdigit (tree);
171 static tree fold_builtin_fabs (tree, tree);
172 static tree fold_builtin_abs (tree, tree);
173 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
174                                         enum tree_code);
175 static tree fold_builtin_1 (tree, tree, bool);
176
177 static tree fold_builtin_strpbrk (tree, tree);
178 static tree fold_builtin_strstr (tree, tree);
179 static tree fold_builtin_strrchr (tree, tree);
180 static tree fold_builtin_strcat (tree);
181 static tree fold_builtin_strncat (tree);
182 static tree fold_builtin_strspn (tree);
183 static tree fold_builtin_strcspn (tree);
184 static tree fold_builtin_sprintf (tree, int);
185
186 static rtx expand_builtin_object_size (tree);
187 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
188                                       enum built_in_function);
189 static void maybe_emit_chk_warning (tree, enum built_in_function);
190 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
191 static tree fold_builtin_object_size (tree);
192 static tree fold_builtin_strcat_chk (tree, tree);
193 static tree fold_builtin_strncat_chk (tree, tree);
194 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
195 static tree fold_builtin_printf (tree, tree, bool, enum built_in_function);
196 static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function);
197 static bool init_target_chars (void);
198
199 static unsigned HOST_WIDE_INT target_newline;
200 static unsigned HOST_WIDE_INT target_percent;
201 static unsigned HOST_WIDE_INT target_c;
202 static unsigned HOST_WIDE_INT target_s;
203 static char target_percent_c[3];
204 static char target_percent_s[3];
205 static char target_percent_s_newline[4];
206 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
207                           const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
208
209 /* Return true if NODE should be considered for inline expansion regardless
210    of the optimization level.  This means whenever a function is invoked with
211    its "internal" name, which normally contains the prefix "__builtin".  */
212
213 static bool called_as_built_in (tree node)
214 {
215   const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
216   if (strncmp (name, "__builtin_", 10) == 0)
217     return true;
218   if (strncmp (name, "__sync_", 7) == 0)
219     return true;
220   return false;
221 }
222
223 /* Return the alignment in bits of EXP, a pointer valued expression.
224    But don't return more than MAX_ALIGN no matter what.
225    The alignment returned is, by default, the alignment of the thing that
226    EXP points to.  If it is not a POINTER_TYPE, 0 is returned.
227
228    Otherwise, look at the expression to see if we can do better, i.e., if the
229    expression is actually pointing at an object whose alignment is tighter.  */
230
231 static int
232 get_pointer_alignment (tree exp, unsigned int max_align)
233 {
234   unsigned int align, inner;
235
236   /* We rely on TER to compute accurate alignment information.  */
237   if (!(optimize && flag_tree_ter))
238     return 0;
239
240   if (!POINTER_TYPE_P (TREE_TYPE (exp)))
241     return 0;
242
243   align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
244   align = MIN (align, max_align);
245
246   while (1)
247     {
248       switch (TREE_CODE (exp))
249         {
250         case NOP_EXPR:
251         case CONVERT_EXPR:
252         case NON_LVALUE_EXPR:
253           exp = TREE_OPERAND (exp, 0);
254           if (! POINTER_TYPE_P (TREE_TYPE (exp)))
255             return align;
256
257           inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
258           align = MIN (inner, max_align);
259           break;
260
261         case PLUS_EXPR:
262           /* If sum of pointer + int, restrict our maximum alignment to that
263              imposed by the integer.  If not, we can't do any better than
264              ALIGN.  */
265           if (! host_integerp (TREE_OPERAND (exp, 1), 1))
266             return align;
267
268           while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
269                   & (max_align / BITS_PER_UNIT - 1))
270                  != 0)
271             max_align >>= 1;
272
273           exp = TREE_OPERAND (exp, 0);
274           break;
275
276         case ADDR_EXPR:
277           /* See what we are pointing at and look at its alignment.  */
278           exp = TREE_OPERAND (exp, 0);
279           inner = max_align;
280           if (handled_component_p (exp))
281             {
282               HOST_WIDE_INT bitsize, bitpos;
283               tree offset;
284               enum machine_mode mode; 
285               int unsignedp, volatilep;
286
287               exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
288                                          &mode, &unsignedp, &volatilep, true);
289               if (bitpos)
290                 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
291               if (offset && TREE_CODE (offset) == PLUS_EXPR
292                   && host_integerp (TREE_OPERAND (offset, 1), 1))
293                 {
294                   /* Any overflow in calculating offset_bits won't change
295                      the alignment.  */
296                   unsigned offset_bits
297                     = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
298                        * BITS_PER_UNIT);
299
300                   if (offset_bits)
301                     inner = MIN (inner, (offset_bits & -offset_bits));
302                   offset = TREE_OPERAND (offset, 0);
303                 }
304               if (offset && TREE_CODE (offset) == MULT_EXPR
305                   && host_integerp (TREE_OPERAND (offset, 1), 1))
306                 {
307                   /* Any overflow in calculating offset_factor won't change
308                      the alignment.  */
309                   unsigned offset_factor
310                     = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
311                        * BITS_PER_UNIT);
312
313                   if (offset_factor)
314                     inner = MIN (inner, (offset_factor & -offset_factor));
315                 }
316               else if (offset)
317                 inner = MIN (inner, BITS_PER_UNIT);
318             }
319           if (TREE_CODE (exp) == FUNCTION_DECL)
320             align = FUNCTION_BOUNDARY;
321           else if (DECL_P (exp))
322             align = MIN (inner, DECL_ALIGN (exp));
323 #ifdef CONSTANT_ALIGNMENT
324           else if (CONSTANT_CLASS_P (exp))
325             align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
326 #endif
327           else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
328                    || TREE_CODE (exp) == INDIRECT_REF)
329             align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
330           else
331             align = MIN (align, inner);
332           return MIN (align, max_align);
333
334         default:
335           return align;
336         }
337     }
338 }
339
340 /* Compute the length of a C string.  TREE_STRING_LENGTH is not the right
341    way, because it could contain a zero byte in the middle.
342    TREE_STRING_LENGTH is the size of the character array, not the string.
343
344    ONLY_VALUE should be nonzero if the result is not going to be emitted
345    into the instruction stream and zero if it is going to be expanded.
346    E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
347    is returned, otherwise NULL, since
348    len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
349    evaluate the side-effects.
350
351    The value returned is of type `ssizetype'.
352
353    Unfortunately, string_constant can't access the values of const char
354    arrays with initializers, so neither can we do so here.  */
355
356 tree
357 c_strlen (tree src, int only_value)
358 {
359   tree offset_node;
360   HOST_WIDE_INT offset;
361   int max;
362   const char *ptr;
363
364   STRIP_NOPS (src);
365   if (TREE_CODE (src) == COND_EXPR
366       && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
367     {
368       tree len1, len2;
369
370       len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
371       len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
372       if (tree_int_cst_equal (len1, len2))
373         return len1;
374     }
375
376   if (TREE_CODE (src) == COMPOUND_EXPR
377       && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
378     return c_strlen (TREE_OPERAND (src, 1), only_value);
379
380   src = string_constant (src, &offset_node);
381   if (src == 0)
382     return 0;
383
384   max = TREE_STRING_LENGTH (src) - 1;
385   ptr = TREE_STRING_POINTER (src);
386
387   if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
388     {
389       /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
390          compute the offset to the following null if we don't know where to
391          start searching for it.  */
392       int i;
393
394       for (i = 0; i < max; i++)
395         if (ptr[i] == 0)
396           return 0;
397
398       /* We don't know the starting offset, but we do know that the string
399          has no internal zero bytes.  We can assume that the offset falls
400          within the bounds of the string; otherwise, the programmer deserves
401          what he gets.  Subtract the offset from the length of the string,
402          and return that.  This would perhaps not be valid if we were dealing
403          with named arrays in addition to literal string constants.  */
404
405       return size_diffop (size_int (max), offset_node);
406     }
407
408   /* We have a known offset into the string.  Start searching there for
409      a null character if we can represent it as a single HOST_WIDE_INT.  */
410   if (offset_node == 0)
411     offset = 0;
412   else if (! host_integerp (offset_node, 0))
413     offset = -1;
414   else
415     offset = tree_low_cst (offset_node, 0);
416
417   /* If the offset is known to be out of bounds, warn, and call strlen at
418      runtime.  */
419   if (offset < 0 || offset > max)
420     {
421       warning (0, "offset outside bounds of constant string");
422       return 0;
423     }
424
425   /* Use strlen to search for the first zero byte.  Since any strings
426      constructed with build_string will have nulls appended, we win even
427      if we get handed something like (char[4])"abcd".
428
429      Since OFFSET is our starting index into the string, no further
430      calculation is needed.  */
431   return ssize_int (strlen (ptr + offset));
432 }
433
434 /* Return a char pointer for a C string if it is a string constant
435    or sum of string constant and integer constant.  */
436
437 static const char *
438 c_getstr (tree src)
439 {
440   tree offset_node;
441
442   src = string_constant (src, &offset_node);
443   if (src == 0)
444     return 0;
445
446   if (offset_node == 0)
447     return TREE_STRING_POINTER (src);
448   else if (!host_integerp (offset_node, 1)
449            || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
450     return 0;
451
452   return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
453 }
454
455 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
456    GET_MODE_BITSIZE (MODE) bits from string constant STR.  */
457
458 static rtx
459 c_readstr (const char *str, enum machine_mode mode)
460 {
461   HOST_WIDE_INT c[2];
462   HOST_WIDE_INT ch;
463   unsigned int i, j;
464
465   gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
466
467   c[0] = 0;
468   c[1] = 0;
469   ch = 1;
470   for (i = 0; i < GET_MODE_SIZE (mode); i++)
471     {
472       j = i;
473       if (WORDS_BIG_ENDIAN)
474         j = GET_MODE_SIZE (mode) - i - 1;
475       if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
476           && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
477         j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
478       j *= BITS_PER_UNIT;
479       gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
480
481       if (ch)
482         ch = (unsigned char) str[i];
483       c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
484     }
485   return immed_double_const (c[0], c[1], mode);
486 }
487
488 /* Cast a target constant CST to target CHAR and if that value fits into
489    host char type, return zero and put that value into variable pointed to by
490    P.  */
491
492 static int
493 target_char_cast (tree cst, char *p)
494 {
495   unsigned HOST_WIDE_INT val, hostval;
496
497   if (!host_integerp (cst, 1)
498       || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
499     return 1;
500
501   val = tree_low_cst (cst, 1);
502   if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
503     val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
504
505   hostval = val;
506   if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
507     hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
508
509   if (val != hostval)
510     return 1;
511
512   *p = hostval;
513   return 0;
514 }
515
516 /* Similar to save_expr, but assumes that arbitrary code is not executed
517    in between the multiple evaluations.  In particular, we assume that a
518    non-addressable local variable will not be modified.  */
519
520 static tree
521 builtin_save_expr (tree exp)
522 {
523   if (TREE_ADDRESSABLE (exp) == 0
524       && (TREE_CODE (exp) == PARM_DECL
525           || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
526     return exp;
527
528   return save_expr (exp);
529 }
530
531 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
532    times to get the address of either a higher stack frame, or a return
533    address located within it (depending on FNDECL_CODE).  */
534
535 static rtx
536 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
537 {
538   int i;
539
540 #ifdef INITIAL_FRAME_ADDRESS_RTX
541   rtx tem = INITIAL_FRAME_ADDRESS_RTX;
542 #else
543   rtx tem;
544
545   /* For a zero count with __builtin_return_address, we don't care what
546      frame address we return, because target-specific definitions will
547      override us.  Therefore frame pointer elimination is OK, and using
548      the soft frame pointer is OK.
549
550      For a non-zero count, or a zero count with __builtin_frame_address,
551      we require a stable offset from the current frame pointer to the
552      previous one, so we must use the hard frame pointer, and
553      we must disable frame pointer elimination.  */
554   if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
555     tem = frame_pointer_rtx;
556   else
557     {
558       tem = hard_frame_pointer_rtx;
559
560       /* Tell reload not to eliminate the frame pointer.  */
561       current_function_accesses_prior_frames = 1;
562     }
563 #endif
564
565   /* Some machines need special handling before we can access
566      arbitrary frames.  For example, on the SPARC, we must first flush
567      all register windows to the stack.  */
568 #ifdef SETUP_FRAME_ADDRESSES
569   if (count > 0)
570     SETUP_FRAME_ADDRESSES ();
571 #endif
572
573   /* On the SPARC, the return address is not in the frame, it is in a
574      register.  There is no way to access it off of the current frame
575      pointer, but it can be accessed off the previous frame pointer by
576      reading the value from the register window save area.  */
577 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
578   if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
579     count--;
580 #endif
581
582   /* Scan back COUNT frames to the specified frame.  */
583   for (i = 0; i < count; i++)
584     {
585       /* Assume the dynamic chain pointer is in the word that the
586          frame address points to, unless otherwise specified.  */
587 #ifdef DYNAMIC_CHAIN_ADDRESS
588       tem = DYNAMIC_CHAIN_ADDRESS (tem);
589 #endif
590       tem = memory_address (Pmode, tem);
591       tem = gen_frame_mem (Pmode, tem);
592       tem = copy_to_reg (tem);
593     }
594
595   /* For __builtin_frame_address, return what we've got.  But, on
596      the SPARC for example, we may have to add a bias.  */
597   if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
598 #ifdef FRAME_ADDR_RTX
599     return FRAME_ADDR_RTX (tem);
600 #else
601     return tem;
602 #endif
603
604   /* For __builtin_return_address, get the return address from that frame.  */
605 #ifdef RETURN_ADDR_RTX
606   tem = RETURN_ADDR_RTX (count, tem);
607 #else
608   tem = memory_address (Pmode,
609                         plus_constant (tem, GET_MODE_SIZE (Pmode)));
610   tem = gen_frame_mem (Pmode, tem);
611 #endif
612   return tem;
613 }
614
615 /* Alias set used for setjmp buffer.  */
616 static HOST_WIDE_INT setjmp_alias_set = -1;
617
618 /* Construct the leading half of a __builtin_setjmp call.  Control will
619    return to RECEIVER_LABEL.  This is also called directly by the SJLJ
620    exception handling code.  */
621
622 void
623 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
624 {
625   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
626   rtx stack_save;
627   rtx mem;
628
629   if (setjmp_alias_set == -1)
630     setjmp_alias_set = new_alias_set ();
631
632   buf_addr = convert_memory_address (Pmode, buf_addr);
633
634   buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
635
636   /* We store the frame pointer and the address of receiver_label in
637      the buffer and use the rest of it for the stack save area, which
638      is machine-dependent.  */
639
640   mem = gen_rtx_MEM (Pmode, buf_addr);
641   set_mem_alias_set (mem, setjmp_alias_set);
642   emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
643
644   mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
645   set_mem_alias_set (mem, setjmp_alias_set);
646
647   emit_move_insn (validize_mem (mem),
648                   force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
649
650   stack_save = gen_rtx_MEM (sa_mode,
651                             plus_constant (buf_addr,
652                                            2 * GET_MODE_SIZE (Pmode)));
653   set_mem_alias_set (stack_save, setjmp_alias_set);
654   emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
655
656   /* If there is further processing to do, do it.  */
657 #ifdef HAVE_builtin_setjmp_setup
658   if (HAVE_builtin_setjmp_setup)
659     emit_insn (gen_builtin_setjmp_setup (buf_addr));
660 #endif
661
662   /* Tell optimize_save_area_alloca that extra work is going to
663      need to go on during alloca.  */
664   current_function_calls_setjmp = 1;
665
666   /* Set this so all the registers get saved in our frame; we need to be
667      able to copy the saved values for any registers from frames we unwind.  */
668   current_function_has_nonlocal_label = 1;
669 }
670
671 /* Construct the trailing part of a __builtin_setjmp call.  This is
672    also called directly by the SJLJ exception handling code.  */
673
674 void
675 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
676 {
677   /* Clobber the FP when we get here, so we have to make sure it's
678      marked as used by this function.  */
679   emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
680
681   /* Mark the static chain as clobbered here so life information
682      doesn't get messed up for it.  */
683   emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
684
685   /* Now put in the code to restore the frame pointer, and argument
686      pointer, if needed.  */
687 #ifdef HAVE_nonlocal_goto
688   if (! HAVE_nonlocal_goto)
689 #endif
690     {
691       emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
692       /* This might change the hard frame pointer in ways that aren't
693          apparent to early optimization passes, so force a clobber.  */
694       emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx));
695     }
696
697 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
698   if (fixed_regs[ARG_POINTER_REGNUM])
699     {
700 #ifdef ELIMINABLE_REGS
701       size_t i;
702       static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
703
704       for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
705         if (elim_regs[i].from == ARG_POINTER_REGNUM
706             && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
707           break;
708
709       if (i == ARRAY_SIZE (elim_regs))
710 #endif
711         {
712           /* Now restore our arg pointer from the address at which it
713              was saved in our stack frame.  */
714           emit_move_insn (virtual_incoming_args_rtx,
715                           copy_to_reg (get_arg_pointer_save_area (cfun)));
716         }
717     }
718 #endif
719
720 #ifdef HAVE_builtin_setjmp_receiver
721   if (HAVE_builtin_setjmp_receiver)
722     emit_insn (gen_builtin_setjmp_receiver (receiver_label));
723   else
724 #endif
725 #ifdef HAVE_nonlocal_goto_receiver
726     if (HAVE_nonlocal_goto_receiver)
727       emit_insn (gen_nonlocal_goto_receiver ());
728     else
729 #endif
730       { /* Nothing */ }
731
732   /* @@@ This is a kludge.  Not all machine descriptions define a blockage
733      insn, but we must not allow the code we just generated to be reordered
734      by scheduling.  Specifically, the update of the frame pointer must
735      happen immediately, not later.  So emit an ASM_INPUT to act as blockage
736      insn.  */
737   emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
738 }
739
740 /* __builtin_longjmp is passed a pointer to an array of five words (not
741    all will be used on all machines).  It operates similarly to the C
742    library function of the same name, but is more efficient.  Much of
743    the code below is copied from the handling of non-local gotos.  */
744
745 static void
746 expand_builtin_longjmp (rtx buf_addr, rtx value)
747 {
748   rtx fp, lab, stack, insn, last;
749   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
750
751   if (setjmp_alias_set == -1)
752     setjmp_alias_set = new_alias_set ();
753
754   buf_addr = convert_memory_address (Pmode, buf_addr);
755
756   buf_addr = force_reg (Pmode, buf_addr);
757
758   /* We used to store value in static_chain_rtx, but that fails if pointers
759      are smaller than integers.  We instead require that the user must pass
760      a second argument of 1, because that is what builtin_setjmp will
761      return.  This also makes EH slightly more efficient, since we are no
762      longer copying around a value that we don't care about.  */
763   gcc_assert (value == const1_rtx);
764
765   last = get_last_insn ();
766 #ifdef HAVE_builtin_longjmp
767   if (HAVE_builtin_longjmp)
768     emit_insn (gen_builtin_longjmp (buf_addr));
769   else
770 #endif
771     {
772       fp = gen_rtx_MEM (Pmode, buf_addr);
773       lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
774                                                GET_MODE_SIZE (Pmode)));
775
776       stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
777                                                    2 * GET_MODE_SIZE (Pmode)));
778       set_mem_alias_set (fp, setjmp_alias_set);
779       set_mem_alias_set (lab, setjmp_alias_set);
780       set_mem_alias_set (stack, setjmp_alias_set);
781
782       /* Pick up FP, label, and SP from the block and jump.  This code is
783          from expand_goto in stmt.c; see there for detailed comments.  */
784 #ifdef HAVE_nonlocal_goto
785       if (HAVE_nonlocal_goto)
786         /* We have to pass a value to the nonlocal_goto pattern that will
787            get copied into the static_chain pointer, but it does not matter
788            what that value is, because builtin_setjmp does not use it.  */
789         emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
790       else
791 #endif
792         {
793           lab = copy_to_reg (lab);
794
795           emit_insn (gen_rtx_CLOBBER (VOIDmode,
796                                       gen_rtx_MEM (BLKmode,
797                                                    gen_rtx_SCRATCH (VOIDmode))));
798           emit_insn (gen_rtx_CLOBBER (VOIDmode,
799                                       gen_rtx_MEM (BLKmode,
800                                                    hard_frame_pointer_rtx)));
801
802           emit_move_insn (hard_frame_pointer_rtx, fp);
803           emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
804
805           emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
806           emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
807           emit_indirect_jump (lab);
808         }
809     }
810
811   /* Search backwards and mark the jump insn as a non-local goto.
812      Note that this precludes the use of __builtin_longjmp to a
813      __builtin_setjmp target in the same function.  However, we've
814      already cautioned the user that these functions are for
815      internal exception handling use only.  */
816   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
817     {
818       gcc_assert (insn != last);
819
820       if (JUMP_P (insn))
821         {
822           REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
823                                               REG_NOTES (insn));
824           break;
825         }
826       else if (CALL_P (insn))
827         break;
828     }
829 }
830
831 /* Expand a call to __builtin_nonlocal_goto.  We're passed the target label
832    and the address of the save area.  */
833
834 static rtx
835 expand_builtin_nonlocal_goto (tree arglist)
836 {
837   tree t_label, t_save_area;
838   rtx r_label, r_save_area, r_fp, r_sp, insn;
839
840   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
841     return NULL_RTX;
842
843   t_label = TREE_VALUE (arglist);
844   arglist = TREE_CHAIN (arglist);
845   t_save_area = TREE_VALUE (arglist);
846
847   r_label = expand_normal (t_label);
848   r_label = convert_memory_address (Pmode, r_label);
849   r_save_area = expand_normal (t_save_area);
850   r_save_area = convert_memory_address (Pmode, r_save_area);
851   r_fp = gen_rtx_MEM (Pmode, r_save_area);
852   r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
853                       plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
854
855   current_function_has_nonlocal_goto = 1;
856
857 #ifdef HAVE_nonlocal_goto
858   /* ??? We no longer need to pass the static chain value, afaik.  */
859   if (HAVE_nonlocal_goto)
860     emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
861   else
862 #endif
863     {
864       r_label = copy_to_reg (r_label);
865
866       emit_insn (gen_rtx_CLOBBER (VOIDmode,
867                                   gen_rtx_MEM (BLKmode,
868                                                gen_rtx_SCRATCH (VOIDmode))));
869
870       emit_insn (gen_rtx_CLOBBER (VOIDmode,
871                                   gen_rtx_MEM (BLKmode,
872                                                hard_frame_pointer_rtx)));
873
874       /* Restore frame pointer for containing function.
875          This sets the actual hard register used for the frame pointer
876          to the location of the function's incoming static chain info.
877          The non-local goto handler will then adjust it to contain the
878          proper value and reload the argument pointer, if needed.  */
879       emit_move_insn (hard_frame_pointer_rtx, r_fp);
880       emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
881
882       /* USE of hard_frame_pointer_rtx added for consistency;
883          not clear if really needed.  */
884       emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
885       emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
886       emit_indirect_jump (r_label);
887     }
888
889   /* Search backwards to the jump insn and mark it as a
890      non-local goto.  */
891   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
892     {
893       if (JUMP_P (insn))
894         {
895           REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
896                                               const0_rtx, REG_NOTES (insn));
897           break;
898         }
899       else if (CALL_P (insn))
900         break;
901     }
902
903   return const0_rtx;
904 }
905
906 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
907    (not all will be used on all machines) that was passed to __builtin_setjmp.
908    It updates the stack pointer in that block to correspond to the current
909    stack pointer.  */
910
911 static void
912 expand_builtin_update_setjmp_buf (rtx buf_addr)
913 {
914   enum machine_mode sa_mode = Pmode;
915   rtx stack_save;
916
917
918 #ifdef HAVE_save_stack_nonlocal
919   if (HAVE_save_stack_nonlocal)
920     sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
921 #endif
922 #ifdef STACK_SAVEAREA_MODE
923   sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
924 #endif
925
926   stack_save
927     = gen_rtx_MEM (sa_mode,
928                    memory_address
929                    (sa_mode,
930                     plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
931
932 #ifdef HAVE_setjmp
933   if (HAVE_setjmp)
934     emit_insn (gen_setjmp ());
935 #endif
936
937   emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
938 }
939
940 /* Expand a call to __builtin_prefetch.  For a target that does not support
941    data prefetch, evaluate the memory address argument in case it has side
942    effects.  */
943
944 static void
945 expand_builtin_prefetch (tree arglist)
946 {
947   tree arg0, arg1, arg2;
948   rtx op0, op1, op2;
949
950   if (!validate_arglist (arglist, POINTER_TYPE, 0))
951     return;
952
953   arg0 = TREE_VALUE (arglist);
954   /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
955      zero (read) and argument 2 (locality) defaults to 3 (high degree of
956      locality).  */
957   if (TREE_CHAIN (arglist))
958     {
959       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
960       if (TREE_CHAIN (TREE_CHAIN (arglist)))
961         arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
962       else
963         arg2 = build_int_cst (NULL_TREE, 3);
964     }
965   else
966     {
967       arg1 = integer_zero_node;
968       arg2 = build_int_cst (NULL_TREE, 3);
969     }
970
971   /* Argument 0 is an address.  */
972   op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
973
974   /* Argument 1 (read/write flag) must be a compile-time constant int.  */
975   if (TREE_CODE (arg1) != INTEGER_CST)
976     {
977       error ("second argument to %<__builtin_prefetch%> must be a constant");
978       arg1 = integer_zero_node;
979     }
980   op1 = expand_normal (arg1);
981   /* Argument 1 must be either zero or one.  */
982   if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
983     {
984       warning (0, "invalid second argument to %<__builtin_prefetch%>;"
985                " using zero");
986       op1 = const0_rtx;
987     }
988
989   /* Argument 2 (locality) must be a compile-time constant int.  */
990   if (TREE_CODE (arg2) != INTEGER_CST)
991     {
992       error ("third argument to %<__builtin_prefetch%> must be a constant");
993       arg2 = integer_zero_node;
994     }
995   op2 = expand_normal (arg2);
996   /* Argument 2 must be 0, 1, 2, or 3.  */
997   if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
998     {
999       warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1000       op2 = const0_rtx;
1001     }
1002
1003 #ifdef HAVE_prefetch
1004   if (HAVE_prefetch)
1005     {
1006       if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1007              (op0,
1008               insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1009           || (GET_MODE (op0) != Pmode))
1010         {
1011           op0 = convert_memory_address (Pmode, op0);
1012           op0 = force_reg (Pmode, op0);
1013         }
1014       emit_insn (gen_prefetch (op0, op1, op2));
1015     }
1016 #endif
1017
1018   /* Don't do anything with direct references to volatile memory, but
1019      generate code to handle other side effects.  */
1020   if (!MEM_P (op0) && side_effects_p (op0))
1021     emit_insn (op0);
1022 }
1023
1024 /* Get a MEM rtx for expression EXP which is the address of an operand
1025    to be used in a string instruction (cmpstrsi, movmemsi, ..).  LEN is
1026    the maximum length of the block of memory that might be accessed or
1027    NULL if unknown.  */
1028
1029 static rtx
1030 get_memory_rtx (tree exp, tree len)
1031 {
1032   rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1033   rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1034
1035   /* Get an expression we can use to find the attributes to assign to MEM.
1036      If it is an ADDR_EXPR, use the operand.  Otherwise, dereference it if
1037      we can.  First remove any nops.  */
1038   while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1039           || TREE_CODE (exp) == NON_LVALUE_EXPR)
1040          && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1041     exp = TREE_OPERAND (exp, 0);
1042
1043   if (TREE_CODE (exp) == ADDR_EXPR)
1044     exp = TREE_OPERAND (exp, 0);
1045   else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1046     exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1047   else
1048     exp = NULL;
1049
1050   /* Honor attributes derived from exp, except for the alias set
1051      (as builtin stringops may alias with anything) and the size
1052      (as stringops may access multiple array elements).  */
1053   if (exp)
1054     {
1055       set_mem_attributes (mem, exp, 0);
1056
1057       /* Allow the string and memory builtins to overflow from one
1058          field into another, see http://gcc.gnu.org/PR23561.
1059          Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1060          memory accessed by the string or memory builtin will fit
1061          within the field.  */
1062       if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1063         {
1064           tree mem_expr = MEM_EXPR (mem);
1065           HOST_WIDE_INT offset = -1, length = -1;
1066           tree inner = exp;
1067
1068           while (TREE_CODE (inner) == ARRAY_REF
1069                  || TREE_CODE (inner) == NOP_EXPR
1070                  || TREE_CODE (inner) == CONVERT_EXPR
1071                  || TREE_CODE (inner) == NON_LVALUE_EXPR
1072                  || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1073                  || TREE_CODE (inner) == SAVE_EXPR)
1074             inner = TREE_OPERAND (inner, 0);
1075
1076           gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1077
1078           if (MEM_OFFSET (mem)
1079               && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1080             offset = INTVAL (MEM_OFFSET (mem));
1081
1082           if (offset >= 0 && len && host_integerp (len, 0))
1083             length = tree_low_cst (len, 0);
1084
1085           while (TREE_CODE (inner) == COMPONENT_REF)
1086             {
1087               tree field = TREE_OPERAND (inner, 1);
1088               gcc_assert (! DECL_BIT_FIELD (field));
1089               gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1090               gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1091
1092               if (length >= 0
1093                   && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1094                   && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1095                 {
1096                   HOST_WIDE_INT size
1097                     = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1098                   /* If we can prove the memory starting at XEXP (mem, 0)
1099                      and ending at XEXP (mem, 0) + LENGTH will fit into
1100                      this field, we can keep that COMPONENT_REF in MEM_EXPR.  */
1101                   if (offset <= size
1102                       && length <= size
1103                       && offset + length <= size)
1104                     break;
1105                 }
1106
1107               if (offset >= 0
1108                   && host_integerp (DECL_FIELD_OFFSET (field), 0))
1109                 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1110                           + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1111                             / BITS_PER_UNIT;
1112               else
1113                 {
1114                   offset = -1;
1115                   length = -1;
1116                 }
1117
1118               mem_expr = TREE_OPERAND (mem_expr, 0);
1119               inner = TREE_OPERAND (inner, 0);
1120             }
1121
1122           if (mem_expr == NULL)
1123             offset = -1;
1124           if (mem_expr != MEM_EXPR (mem))
1125             {
1126               set_mem_expr (mem, mem_expr);
1127               set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1128             }
1129         }
1130       set_mem_alias_set (mem, 0);
1131       set_mem_size (mem, NULL_RTX);
1132     }
1133
1134   return mem;
1135 }
1136 \f
1137 /* Built-in functions to perform an untyped call and return.  */
1138
1139 /* For each register that may be used for calling a function, this
1140    gives a mode used to copy the register's value.  VOIDmode indicates
1141    the register is not used for calling a function.  If the machine
1142    has register windows, this gives only the outbound registers.
1143    INCOMING_REGNO gives the corresponding inbound register.  */
1144 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1145
1146 /* For each register that may be used for returning values, this gives
1147    a mode used to copy the register's value.  VOIDmode indicates the
1148    register is not used for returning values.  If the machine has
1149    register windows, this gives only the outbound registers.
1150    INCOMING_REGNO gives the corresponding inbound register.  */
1151 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1152
1153 /* For each register that may be used for calling a function, this
1154    gives the offset of that register into the block returned by
1155    __builtin_apply_args.  0 indicates that the register is not
1156    used for calling a function.  */
1157 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1158
1159 /* Return the size required for the block returned by __builtin_apply_args,
1160    and initialize apply_args_mode.  */
1161
1162 static int
1163 apply_args_size (void)
1164 {
1165   static int size = -1;
1166   int align;
1167   unsigned int regno;
1168   enum machine_mode mode;
1169
1170   /* The values computed by this function never change.  */
1171   if (size < 0)
1172     {
1173       /* The first value is the incoming arg-pointer.  */
1174       size = GET_MODE_SIZE (Pmode);
1175
1176       /* The second value is the structure value address unless this is
1177          passed as an "invisible" first argument.  */
1178       if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1179         size += GET_MODE_SIZE (Pmode);
1180
1181       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1182         if (FUNCTION_ARG_REGNO_P (regno))
1183           {
1184             mode = reg_raw_mode[regno];
1185
1186             gcc_assert (mode != VOIDmode);
1187
1188             align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1189             if (size % align != 0)
1190               size = CEIL (size, align) * align;
1191             apply_args_reg_offset[regno] = size;
1192             size += GET_MODE_SIZE (mode);
1193             apply_args_mode[regno] = mode;
1194           }
1195         else
1196           {
1197             apply_args_mode[regno] = VOIDmode;
1198             apply_args_reg_offset[regno] = 0;
1199           }
1200     }
1201   return size;
1202 }
1203
1204 /* Return the size required for the block returned by __builtin_apply,
1205    and initialize apply_result_mode.  */
1206
1207 static int
1208 apply_result_size (void)
1209 {
1210   static int size = -1;
1211   int align, regno;
1212   enum machine_mode mode;
1213
1214   /* The values computed by this function never change.  */
1215   if (size < 0)
1216     {
1217       size = 0;
1218
1219       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1220         if (FUNCTION_VALUE_REGNO_P (regno))
1221           {
1222             mode = reg_raw_mode[regno];
1223
1224             gcc_assert (mode != VOIDmode);
1225
1226             align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1227             if (size % align != 0)
1228               size = CEIL (size, align) * align;
1229             size += GET_MODE_SIZE (mode);
1230             apply_result_mode[regno] = mode;
1231           }
1232         else
1233           apply_result_mode[regno] = VOIDmode;
1234
1235       /* Allow targets that use untyped_call and untyped_return to override
1236          the size so that machine-specific information can be stored here.  */
1237 #ifdef APPLY_RESULT_SIZE
1238       size = APPLY_RESULT_SIZE;
1239 #endif
1240     }
1241   return size;
1242 }
1243
1244 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1245 /* Create a vector describing the result block RESULT.  If SAVEP is true,
1246    the result block is used to save the values; otherwise it is used to
1247    restore the values.  */
1248
1249 static rtx
1250 result_vector (int savep, rtx result)
1251 {
1252   int regno, size, align, nelts;
1253   enum machine_mode mode;
1254   rtx reg, mem;
1255   rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1256
1257   size = nelts = 0;
1258   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1259     if ((mode = apply_result_mode[regno]) != VOIDmode)
1260       {
1261         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1262         if (size % align != 0)
1263           size = CEIL (size, align) * align;
1264         reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1265         mem = adjust_address (result, mode, size);
1266         savevec[nelts++] = (savep
1267                             ? gen_rtx_SET (VOIDmode, mem, reg)
1268                             : gen_rtx_SET (VOIDmode, reg, mem));
1269         size += GET_MODE_SIZE (mode);
1270       }
1271   return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1272 }
1273 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1274
1275 /* Save the state required to perform an untyped call with the same
1276    arguments as were passed to the current function.  */
1277
1278 static rtx
1279 expand_builtin_apply_args_1 (void)
1280 {
1281   rtx registers, tem;
1282   int size, align, regno;
1283   enum machine_mode mode;
1284   rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1285
1286   /* Create a block where the arg-pointer, structure value address,
1287      and argument registers can be saved.  */
1288   registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1289
1290   /* Walk past the arg-pointer and structure value address.  */
1291   size = GET_MODE_SIZE (Pmode);
1292   if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1293     size += GET_MODE_SIZE (Pmode);
1294
1295   /* Save each register used in calling a function to the block.  */
1296   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1297     if ((mode = apply_args_mode[regno]) != VOIDmode)
1298       {
1299         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1300         if (size % align != 0)
1301           size = CEIL (size, align) * align;
1302
1303         tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1304
1305         emit_move_insn (adjust_address (registers, mode, size), tem);
1306         size += GET_MODE_SIZE (mode);
1307       }
1308
1309   /* Save the arg pointer to the block.  */
1310   tem = copy_to_reg (virtual_incoming_args_rtx);
1311 #ifdef STACK_GROWS_DOWNWARD
1312   /* We need the pointer as the caller actually passed them to us, not
1313      as we might have pretended they were passed.  Make sure it's a valid
1314      operand, as emit_move_insn isn't expected to handle a PLUS.  */
1315   tem
1316     = force_operand (plus_constant (tem, current_function_pretend_args_size),
1317                      NULL_RTX);
1318 #endif
1319   emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1320
1321   size = GET_MODE_SIZE (Pmode);
1322
1323   /* Save the structure value address unless this is passed as an
1324      "invisible" first argument.  */
1325   if (struct_incoming_value)
1326     {
1327       emit_move_insn (adjust_address (registers, Pmode, size),
1328                       copy_to_reg (struct_incoming_value));
1329       size += GET_MODE_SIZE (Pmode);
1330     }
1331
1332   /* Return the address of the block.  */
1333   return copy_addr_to_reg (XEXP (registers, 0));
1334 }
1335
1336 /* __builtin_apply_args returns block of memory allocated on
1337    the stack into which is stored the arg pointer, structure
1338    value address, static chain, and all the registers that might
1339    possibly be used in performing a function call.  The code is
1340    moved to the start of the function so the incoming values are
1341    saved.  */
1342
1343 static rtx
1344 expand_builtin_apply_args (void)
1345 {
1346   /* Don't do __builtin_apply_args more than once in a function.
1347      Save the result of the first call and reuse it.  */
1348   if (apply_args_value != 0)
1349     return apply_args_value;
1350   {
1351     /* When this function is called, it means that registers must be
1352        saved on entry to this function.  So we migrate the
1353        call to the first insn of this function.  */
1354     rtx temp;
1355     rtx seq;
1356
1357     start_sequence ();
1358     temp = expand_builtin_apply_args_1 ();
1359     seq = get_insns ();
1360     end_sequence ();
1361
1362     apply_args_value = temp;
1363
1364     /* Put the insns after the NOTE that starts the function.
1365        If this is inside a start_sequence, make the outer-level insn
1366        chain current, so the code is placed at the start of the
1367        function.  */
1368     push_topmost_sequence ();
1369     emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1370     pop_topmost_sequence ();
1371     return temp;
1372   }
1373 }
1374
1375 /* Perform an untyped call and save the state required to perform an
1376    untyped return of whatever value was returned by the given function.  */
1377
1378 static rtx
1379 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1380 {
1381   int size, align, regno;
1382   enum machine_mode mode;
1383   rtx incoming_args, result, reg, dest, src, call_insn;
1384   rtx old_stack_level = 0;
1385   rtx call_fusage = 0;
1386   rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1387
1388   arguments = convert_memory_address (Pmode, arguments);
1389
1390   /* Create a block where the return registers can be saved.  */
1391   result = assign_stack_local (BLKmode, apply_result_size (), -1);
1392
1393   /* Fetch the arg pointer from the ARGUMENTS block.  */
1394   incoming_args = gen_reg_rtx (Pmode);
1395   emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1396 #ifndef STACK_GROWS_DOWNWARD
1397   incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1398                                        incoming_args, 0, OPTAB_LIB_WIDEN);
1399 #endif
1400
1401   /* Push a new argument block and copy the arguments.  Do not allow
1402      the (potential) memcpy call below to interfere with our stack
1403      manipulations.  */
1404   do_pending_stack_adjust ();
1405   NO_DEFER_POP;
1406
1407   /* Save the stack with nonlocal if available.  */
1408 #ifdef HAVE_save_stack_nonlocal
1409   if (HAVE_save_stack_nonlocal)
1410     emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1411   else
1412 #endif
1413     emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1414
1415   /* Allocate a block of memory onto the stack and copy the memory
1416      arguments to the outgoing arguments address.  */
1417   allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1418   dest = virtual_outgoing_args_rtx;
1419 #ifndef STACK_GROWS_DOWNWARD
1420   if (GET_CODE (argsize) == CONST_INT)
1421     dest = plus_constant (dest, -INTVAL (argsize));
1422   else
1423     dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1424 #endif
1425   dest = gen_rtx_MEM (BLKmode, dest);
1426   set_mem_align (dest, PARM_BOUNDARY);
1427   src = gen_rtx_MEM (BLKmode, incoming_args);
1428   set_mem_align (src, PARM_BOUNDARY);
1429   emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1430
1431   /* Refer to the argument block.  */
1432   apply_args_size ();
1433   arguments = gen_rtx_MEM (BLKmode, arguments);
1434   set_mem_align (arguments, PARM_BOUNDARY);
1435
1436   /* Walk past the arg-pointer and structure value address.  */
1437   size = GET_MODE_SIZE (Pmode);
1438   if (struct_value)
1439     size += GET_MODE_SIZE (Pmode);
1440
1441   /* Restore each of the registers previously saved.  Make USE insns
1442      for each of these registers for use in making the call.  */
1443   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1444     if ((mode = apply_args_mode[regno]) != VOIDmode)
1445       {
1446         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1447         if (size % align != 0)
1448           size = CEIL (size, align) * align;
1449         reg = gen_rtx_REG (mode, regno);
1450         emit_move_insn (reg, adjust_address (arguments, mode, size));
1451         use_reg (&call_fusage, reg);
1452         size += GET_MODE_SIZE (mode);
1453       }
1454
1455   /* Restore the structure value address unless this is passed as an
1456      "invisible" first argument.  */
1457   size = GET_MODE_SIZE (Pmode);
1458   if (struct_value)
1459     {
1460       rtx value = gen_reg_rtx (Pmode);
1461       emit_move_insn (value, adjust_address (arguments, Pmode, size));
1462       emit_move_insn (struct_value, value);
1463       if (REG_P (struct_value))
1464         use_reg (&call_fusage, struct_value);
1465       size += GET_MODE_SIZE (Pmode);
1466     }
1467
1468   /* All arguments and registers used for the call are set up by now!  */
1469   function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1470
1471   /* Ensure address is valid.  SYMBOL_REF is already valid, so no need,
1472      and we don't want to load it into a register as an optimization,
1473      because prepare_call_address already did it if it should be done.  */
1474   if (GET_CODE (function) != SYMBOL_REF)
1475     function = memory_address (FUNCTION_MODE, function);
1476
1477   /* Generate the actual call instruction and save the return value.  */
1478 #ifdef HAVE_untyped_call
1479   if (HAVE_untyped_call)
1480     emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1481                                       result, result_vector (1, result)));
1482   else
1483 #endif
1484 #ifdef HAVE_call_value
1485   if (HAVE_call_value)
1486     {
1487       rtx valreg = 0;
1488
1489       /* Locate the unique return register.  It is not possible to
1490          express a call that sets more than one return register using
1491          call_value; use untyped_call for that.  In fact, untyped_call
1492          only needs to save the return registers in the given block.  */
1493       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1494         if ((mode = apply_result_mode[regno]) != VOIDmode)
1495           {
1496             gcc_assert (!valreg); /* HAVE_untyped_call required.  */
1497
1498             valreg = gen_rtx_REG (mode, regno);
1499           }
1500
1501       emit_call_insn (GEN_CALL_VALUE (valreg,
1502                                       gen_rtx_MEM (FUNCTION_MODE, function),
1503                                       const0_rtx, NULL_RTX, const0_rtx));
1504
1505       emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1506     }
1507   else
1508 #endif
1509     gcc_unreachable ();
1510
1511   /* Find the CALL insn we just emitted, and attach the register usage
1512      information.  */
1513   call_insn = last_call_insn ();
1514   add_function_usage_to (call_insn, call_fusage);
1515
1516   /* Restore the stack.  */
1517 #ifdef HAVE_save_stack_nonlocal
1518   if (HAVE_save_stack_nonlocal)
1519     emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1520   else
1521 #endif
1522     emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1523
1524   OK_DEFER_POP;
1525
1526   /* Return the address of the result block.  */
1527   result = copy_addr_to_reg (XEXP (result, 0));
1528   return convert_memory_address (ptr_mode, result);
1529 }
1530
1531 /* Perform an untyped return.  */
1532
1533 static void
1534 expand_builtin_return (rtx result)
1535 {
1536   int size, align, regno;
1537   enum machine_mode mode;
1538   rtx reg;
1539   rtx call_fusage = 0;
1540
1541   result = convert_memory_address (Pmode, result);
1542
1543   apply_result_size ();
1544   result = gen_rtx_MEM (BLKmode, result);
1545
1546 #ifdef HAVE_untyped_return
1547   if (HAVE_untyped_return)
1548     {
1549       emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1550       emit_barrier ();
1551       return;
1552     }
1553 #endif
1554
1555   /* Restore the return value and note that each value is used.  */
1556   size = 0;
1557   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1558     if ((mode = apply_result_mode[regno]) != VOIDmode)
1559       {
1560         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1561         if (size % align != 0)
1562           size = CEIL (size, align) * align;
1563         reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1564         emit_move_insn (reg, adjust_address (result, mode, size));
1565
1566         push_to_sequence (call_fusage);
1567         emit_insn (gen_rtx_USE (VOIDmode, reg));
1568         call_fusage = get_insns ();
1569         end_sequence ();
1570         size += GET_MODE_SIZE (mode);
1571       }
1572
1573   /* Put the USE insns before the return.  */
1574   emit_insn (call_fusage);
1575
1576   /* Return whatever values was restored by jumping directly to the end
1577      of the function.  */
1578   expand_naked_return ();
1579 }
1580
1581 /* Used by expand_builtin_classify_type and fold_builtin_classify_type.  */
1582
1583 static enum type_class
1584 type_to_class (tree type)
1585 {
1586   switch (TREE_CODE (type))
1587     {
1588     case VOID_TYPE:        return void_type_class;
1589     case INTEGER_TYPE:     return integer_type_class;
1590     case ENUMERAL_TYPE:    return enumeral_type_class;
1591     case BOOLEAN_TYPE:     return boolean_type_class;
1592     case POINTER_TYPE:     return pointer_type_class;
1593     case REFERENCE_TYPE:   return reference_type_class;
1594     case OFFSET_TYPE:      return offset_type_class;
1595     case REAL_TYPE:        return real_type_class;
1596     case COMPLEX_TYPE:     return complex_type_class;
1597     case FUNCTION_TYPE:    return function_type_class;
1598     case METHOD_TYPE:      return method_type_class;
1599     case RECORD_TYPE:      return record_type_class;
1600     case UNION_TYPE:
1601     case QUAL_UNION_TYPE:  return union_type_class;
1602     case ARRAY_TYPE:       return (TYPE_STRING_FLAG (type)
1603                                    ? string_type_class : array_type_class);
1604     case LANG_TYPE:        return lang_type_class;
1605     default:               return no_type_class;
1606     }
1607 }
1608
1609 /* Expand a call to __builtin_classify_type with arguments found in
1610    ARGLIST.  */
1611
1612 static rtx
1613 expand_builtin_classify_type (tree arglist)
1614 {
1615   if (arglist != 0)
1616     return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1617   return GEN_INT (no_type_class);
1618 }
1619
1620 /* This helper macro, meant to be used in mathfn_built_in below,
1621    determines which among a set of three builtin math functions is
1622    appropriate for a given type mode.  The `F' and `L' cases are
1623    automatically generated from the `double' case.  */
1624 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1625   case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1626   fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1627   fcodel = BUILT_IN_MATHFN##L ; break;
1628
1629 /* Return mathematic function equivalent to FN but operating directly
1630    on TYPE, if available.  If we can't do the conversion, return zero.  */
1631 tree
1632 mathfn_built_in (tree type, enum built_in_function fn)
1633 {
1634   enum built_in_function fcode, fcodef, fcodel;
1635
1636   switch (fn)
1637     {
1638       CASE_MATHFN (BUILT_IN_ACOS)
1639       CASE_MATHFN (BUILT_IN_ACOSH)
1640       CASE_MATHFN (BUILT_IN_ASIN)
1641       CASE_MATHFN (BUILT_IN_ASINH)
1642       CASE_MATHFN (BUILT_IN_ATAN)
1643       CASE_MATHFN (BUILT_IN_ATAN2)
1644       CASE_MATHFN (BUILT_IN_ATANH)
1645       CASE_MATHFN (BUILT_IN_CBRT)
1646       CASE_MATHFN (BUILT_IN_CEIL)
1647       CASE_MATHFN (BUILT_IN_COPYSIGN)
1648       CASE_MATHFN (BUILT_IN_COS)
1649       CASE_MATHFN (BUILT_IN_COSH)
1650       CASE_MATHFN (BUILT_IN_DREM)
1651       CASE_MATHFN (BUILT_IN_ERF)
1652       CASE_MATHFN (BUILT_IN_ERFC)
1653       CASE_MATHFN (BUILT_IN_EXP)
1654       CASE_MATHFN (BUILT_IN_EXP10)
1655       CASE_MATHFN (BUILT_IN_EXP2)
1656       CASE_MATHFN (BUILT_IN_EXPM1)
1657       CASE_MATHFN (BUILT_IN_FABS)
1658       CASE_MATHFN (BUILT_IN_FDIM)
1659       CASE_MATHFN (BUILT_IN_FLOOR)
1660       CASE_MATHFN (BUILT_IN_FMA)
1661       CASE_MATHFN (BUILT_IN_FMAX)
1662       CASE_MATHFN (BUILT_IN_FMIN)
1663       CASE_MATHFN (BUILT_IN_FMOD)
1664       CASE_MATHFN (BUILT_IN_FREXP)
1665       CASE_MATHFN (BUILT_IN_GAMMA)
1666       CASE_MATHFN (BUILT_IN_HUGE_VAL)
1667       CASE_MATHFN (BUILT_IN_HYPOT)
1668       CASE_MATHFN (BUILT_IN_ILOGB)
1669       CASE_MATHFN (BUILT_IN_INF)
1670       CASE_MATHFN (BUILT_IN_J0)
1671       CASE_MATHFN (BUILT_IN_J1)
1672       CASE_MATHFN (BUILT_IN_JN)
1673       CASE_MATHFN (BUILT_IN_LCEIL)
1674       CASE_MATHFN (BUILT_IN_LDEXP)
1675       CASE_MATHFN (BUILT_IN_LFLOOR)
1676       CASE_MATHFN (BUILT_IN_LGAMMA)
1677       CASE_MATHFN (BUILT_IN_LLCEIL)
1678       CASE_MATHFN (BUILT_IN_LLFLOOR)
1679       CASE_MATHFN (BUILT_IN_LLRINT)
1680       CASE_MATHFN (BUILT_IN_LLROUND)
1681       CASE_MATHFN (BUILT_IN_LOG)
1682       CASE_MATHFN (BUILT_IN_LOG10)
1683       CASE_MATHFN (BUILT_IN_LOG1P)
1684       CASE_MATHFN (BUILT_IN_LOG2)
1685       CASE_MATHFN (BUILT_IN_LOGB)
1686       CASE_MATHFN (BUILT_IN_LRINT)
1687       CASE_MATHFN (BUILT_IN_LROUND)
1688       CASE_MATHFN (BUILT_IN_MODF)
1689       CASE_MATHFN (BUILT_IN_NAN)
1690       CASE_MATHFN (BUILT_IN_NANS)
1691       CASE_MATHFN (BUILT_IN_NEARBYINT)
1692       CASE_MATHFN (BUILT_IN_NEXTAFTER)
1693       CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1694       CASE_MATHFN (BUILT_IN_POW)
1695       CASE_MATHFN (BUILT_IN_POWI)
1696       CASE_MATHFN (BUILT_IN_POW10)
1697       CASE_MATHFN (BUILT_IN_REMAINDER)
1698       CASE_MATHFN (BUILT_IN_REMQUO)
1699       CASE_MATHFN (BUILT_IN_RINT)
1700       CASE_MATHFN (BUILT_IN_ROUND)
1701       CASE_MATHFN (BUILT_IN_SCALB)
1702       CASE_MATHFN (BUILT_IN_SCALBLN)
1703       CASE_MATHFN (BUILT_IN_SCALBN)
1704       CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1705       CASE_MATHFN (BUILT_IN_SIN)
1706       CASE_MATHFN (BUILT_IN_SINCOS)
1707       CASE_MATHFN (BUILT_IN_SINH)
1708       CASE_MATHFN (BUILT_IN_SQRT)
1709       CASE_MATHFN (BUILT_IN_TAN)
1710       CASE_MATHFN (BUILT_IN_TANH)
1711       CASE_MATHFN (BUILT_IN_TGAMMA)
1712       CASE_MATHFN (BUILT_IN_TRUNC)
1713       CASE_MATHFN (BUILT_IN_Y0)
1714       CASE_MATHFN (BUILT_IN_Y1)
1715       CASE_MATHFN (BUILT_IN_YN)
1716
1717       default:
1718         return 0;
1719       }
1720
1721   if (TYPE_MAIN_VARIANT (type) == double_type_node)
1722     return implicit_built_in_decls[fcode];
1723   else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1724     return implicit_built_in_decls[fcodef];
1725   else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1726     return implicit_built_in_decls[fcodel];
1727   else
1728     return 0;
1729 }
1730
1731 /* If errno must be maintained, expand the RTL to check if the result,
1732    TARGET, of a built-in function call, EXP, is NaN, and if so set
1733    errno to EDOM.  */
1734
1735 static void
1736 expand_errno_check (tree exp, rtx target)
1737 {
1738   rtx lab = gen_label_rtx ();
1739
1740   /* Test the result; if it is NaN, set errno=EDOM because
1741      the argument was not in the domain.  */
1742   emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1743                            0, lab);
1744
1745 #ifdef TARGET_EDOM
1746   /* If this built-in doesn't throw an exception, set errno directly.  */
1747   if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1748     {
1749 #ifdef GEN_ERRNO_RTX
1750       rtx errno_rtx = GEN_ERRNO_RTX;
1751 #else
1752       rtx errno_rtx
1753           = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1754 #endif
1755       emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1756       emit_label (lab);
1757       return;
1758     }
1759 #endif
1760
1761   /* We can't set errno=EDOM directly; let the library call do it.
1762      Pop the arguments right away in case the call gets deleted.  */
1763   NO_DEFER_POP;
1764   expand_call (exp, target, 0);
1765   OK_DEFER_POP;
1766   emit_label (lab);
1767 }
1768
1769
1770 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1771    Return 0 if a normal call should be emitted rather than expanding the
1772    function in-line.  EXP is the expression that is a call to the builtin
1773    function; if convenient, the result should be placed in TARGET.
1774    SUBTARGET may be used as the target for computing one of EXP's operands.  */
1775
1776 static rtx
1777 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1778 {
1779   optab builtin_optab;
1780   rtx op0, insns, before_call;
1781   tree fndecl = get_callee_fndecl (exp);
1782   tree arglist = TREE_OPERAND (exp, 1);
1783   enum machine_mode mode;
1784   bool errno_set = false;
1785   tree arg, narg;
1786
1787   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1788     return 0;
1789
1790   arg = TREE_VALUE (arglist);
1791
1792   switch (DECL_FUNCTION_CODE (fndecl))
1793     {
1794     CASE_FLT_FN (BUILT_IN_SQRT):
1795       errno_set = ! tree_expr_nonnegative_p (arg);
1796       builtin_optab = sqrt_optab;
1797       break;
1798     CASE_FLT_FN (BUILT_IN_EXP):
1799       errno_set = true; builtin_optab = exp_optab; break;
1800     CASE_FLT_FN (BUILT_IN_EXP10):
1801     CASE_FLT_FN (BUILT_IN_POW10):
1802       errno_set = true; builtin_optab = exp10_optab; break;
1803     CASE_FLT_FN (BUILT_IN_EXP2):
1804       errno_set = true; builtin_optab = exp2_optab; break;
1805     CASE_FLT_FN (BUILT_IN_EXPM1):
1806       errno_set = true; builtin_optab = expm1_optab; break;
1807     CASE_FLT_FN (BUILT_IN_LOGB):
1808       errno_set = true; builtin_optab = logb_optab; break;
1809     CASE_FLT_FN (BUILT_IN_ILOGB):
1810       errno_set = true; builtin_optab = ilogb_optab; break;
1811     CASE_FLT_FN (BUILT_IN_LOG):
1812       errno_set = true; builtin_optab = log_optab; break;
1813     CASE_FLT_FN (BUILT_IN_LOG10):
1814       errno_set = true; builtin_optab = log10_optab; break;
1815     CASE_FLT_FN (BUILT_IN_LOG2):
1816       errno_set = true; builtin_optab = log2_optab; break;
1817     CASE_FLT_FN (BUILT_IN_LOG1P):
1818       errno_set = true; builtin_optab = log1p_optab; break;
1819     CASE_FLT_FN (BUILT_IN_ASIN):
1820       builtin_optab = asin_optab; break;
1821     CASE_FLT_FN (BUILT_IN_ACOS):
1822       builtin_optab = acos_optab; break;
1823     CASE_FLT_FN (BUILT_IN_TAN):
1824       builtin_optab = tan_optab; break;
1825     CASE_FLT_FN (BUILT_IN_ATAN):
1826       builtin_optab = atan_optab; break;
1827     CASE_FLT_FN (BUILT_IN_FLOOR):
1828       builtin_optab = floor_optab; break;
1829     CASE_FLT_FN (BUILT_IN_CEIL):
1830       builtin_optab = ceil_optab; break;
1831     CASE_FLT_FN (BUILT_IN_TRUNC):
1832       builtin_optab = btrunc_optab; break;
1833     CASE_FLT_FN (BUILT_IN_ROUND):
1834       builtin_optab = round_optab; break;
1835     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1836       builtin_optab = nearbyint_optab; break;
1837     CASE_FLT_FN (BUILT_IN_RINT):
1838       builtin_optab = rint_optab; break;
1839     default:
1840       gcc_unreachable ();
1841     }
1842
1843   /* Make a suitable register to place result in.  */
1844   mode = TYPE_MODE (TREE_TYPE (exp));
1845
1846   if (! flag_errno_math || ! HONOR_NANS (mode))
1847     errno_set = false;
1848
1849   /* Before working hard, check whether the instruction is available.  */
1850   if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1851     {
1852       target = gen_reg_rtx (mode);
1853
1854       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1855          need to expand the argument again.  This way, we will not perform
1856          side-effects more the once.  */
1857       narg = builtin_save_expr (arg);
1858       if (narg != arg)
1859         {
1860           arg = narg;
1861           arglist = build_tree_list (NULL_TREE, arg);
1862           exp = build_function_call_expr (fndecl, arglist);
1863         }
1864
1865       op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1866
1867       start_sequence ();
1868
1869       /* Compute into TARGET.
1870          Set TARGET to wherever the result comes back.  */
1871       target = expand_unop (mode, builtin_optab, op0, target, 0);
1872
1873       if (target != 0)
1874         {
1875           if (errno_set)
1876             expand_errno_check (exp, target);
1877
1878           /* Output the entire sequence.  */
1879           insns = get_insns ();
1880           end_sequence ();
1881           emit_insn (insns);
1882           return target;
1883         }
1884
1885       /* If we were unable to expand via the builtin, stop the sequence
1886          (without outputting the insns) and call to the library function
1887          with the stabilized argument list.  */
1888       end_sequence ();
1889     }
1890
1891   before_call = get_last_insn ();
1892
1893   target = expand_call (exp, target, target == const0_rtx);
1894
1895   /* If this is a sqrt operation and we don't care about errno, try to
1896      attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1897      This allows the semantics of the libcall to be visible to the RTL
1898      optimizers.  */
1899   if (builtin_optab == sqrt_optab && !errno_set)
1900     {
1901       /* Search backwards through the insns emitted by expand_call looking
1902          for the instruction with the REG_RETVAL note.  */
1903       rtx last = get_last_insn ();
1904       while (last != before_call)
1905         {
1906           if (find_reg_note (last, REG_RETVAL, NULL))
1907             {
1908               rtx note = find_reg_note (last, REG_EQUAL, NULL);
1909               /* Check that the REQ_EQUAL note is an EXPR_LIST with
1910                  two elements, i.e. symbol_ref(sqrt) and the operand.  */
1911               if (note
1912                   && GET_CODE (note) == EXPR_LIST
1913                   && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1914                   && XEXP (XEXP (note, 0), 1) != NULL_RTX
1915                   && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1916                 {
1917                   rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1918                   /* Check operand is a register with expected mode.  */
1919                   if (operand
1920                       && REG_P (operand)
1921                       && GET_MODE (operand) == mode)
1922                     {
1923                       /* Replace the REG_EQUAL note with a SQRT rtx.  */
1924                       rtx equiv = gen_rtx_SQRT (mode, operand);
1925                       set_unique_reg_note (last, REG_EQUAL, equiv);
1926                     }
1927                 }
1928               break;
1929             }
1930           last = PREV_INSN (last);
1931         }
1932     }
1933
1934   return target;
1935 }
1936
1937 /* Expand a call to the builtin binary math functions (pow and atan2).
1938    Return 0 if a normal call should be emitted rather than expanding the
1939    function in-line.  EXP is the expression that is a call to the builtin
1940    function; if convenient, the result should be placed in TARGET.
1941    SUBTARGET may be used as the target for computing one of EXP's
1942    operands.  */
1943
1944 static rtx
1945 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1946 {
1947   optab builtin_optab;
1948   rtx op0, op1, insns;
1949   int op1_type = REAL_TYPE;
1950   tree fndecl = get_callee_fndecl (exp);
1951   tree arglist = TREE_OPERAND (exp, 1);
1952   tree arg0, arg1, temp, narg;
1953   enum machine_mode mode;
1954   bool errno_set = true;
1955   bool stable = true;
1956
1957   if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1958       || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1959       || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1960     op1_type = INTEGER_TYPE;
1961
1962   if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1963     return 0;
1964
1965   arg0 = TREE_VALUE (arglist);
1966   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1967
1968   switch (DECL_FUNCTION_CODE (fndecl))
1969     {
1970     CASE_FLT_FN (BUILT_IN_POW):
1971       builtin_optab = pow_optab; break;
1972     CASE_FLT_FN (BUILT_IN_ATAN2):
1973       builtin_optab = atan2_optab; break;
1974     CASE_FLT_FN (BUILT_IN_LDEXP):
1975       builtin_optab = ldexp_optab; break;
1976     CASE_FLT_FN (BUILT_IN_FMOD):
1977       builtin_optab = fmod_optab; break;
1978     CASE_FLT_FN (BUILT_IN_REMAINDER):
1979     CASE_FLT_FN (BUILT_IN_DREM):
1980       builtin_optab = remainder_optab; break;
1981     default:
1982       gcc_unreachable ();
1983     }
1984
1985   /* Make a suitable register to place result in.  */
1986   mode = TYPE_MODE (TREE_TYPE (exp));
1987
1988   /* Before working hard, check whether the instruction is available.  */
1989   if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1990     return 0;
1991
1992   target = gen_reg_rtx (mode);
1993
1994   if (! flag_errno_math || ! HONOR_NANS (mode))
1995     errno_set = false;
1996
1997   /* Always stabilize the argument list.  */
1998   narg = builtin_save_expr (arg1);
1999   if (narg != arg1)
2000     {
2001       arg1 = narg;
2002       temp = build_tree_list (NULL_TREE, narg);
2003       stable = false;
2004     }
2005   else
2006     temp = TREE_CHAIN (arglist);
2007
2008   narg = builtin_save_expr (arg0);
2009   if (narg != arg0)
2010     {
2011       arg0 = narg;
2012       arglist = tree_cons (NULL_TREE, narg, temp);
2013       stable = false;
2014     }
2015   else if (! stable)
2016     arglist = tree_cons (NULL_TREE, arg0, temp);
2017
2018   if (! stable)
2019     exp = build_function_call_expr (fndecl, arglist);
2020
2021   op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2022   op1 = expand_normal (arg1);
2023
2024   start_sequence ();
2025
2026   /* Compute into TARGET.
2027      Set TARGET to wherever the result comes back.  */
2028   target = expand_binop (mode, builtin_optab, op0, op1,
2029                          target, 0, OPTAB_DIRECT);
2030
2031   /* If we were unable to expand via the builtin, stop the sequence
2032      (without outputting the insns) and call to the library function
2033      with the stabilized argument list.  */
2034   if (target == 0)
2035     {
2036       end_sequence ();
2037       return expand_call (exp, target, target == const0_rtx);
2038     }
2039
2040   if (errno_set)
2041     expand_errno_check (exp, target);
2042
2043   /* Output the entire sequence.  */
2044   insns = get_insns ();
2045   end_sequence ();
2046   emit_insn (insns);
2047
2048   return target;
2049 }
2050
2051 /* Expand a call to the builtin sin and cos math functions.
2052    Return 0 if a normal call should be emitted rather than expanding the
2053    function in-line.  EXP is the expression that is a call to the builtin
2054    function; if convenient, the result should be placed in TARGET.
2055    SUBTARGET may be used as the target for computing one of EXP's
2056    operands.  */
2057
2058 static rtx
2059 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2060 {
2061   optab builtin_optab;
2062   rtx op0, insns;
2063   tree fndecl = get_callee_fndecl (exp);
2064   tree arglist = TREE_OPERAND (exp, 1);
2065   enum machine_mode mode;
2066   tree arg, narg;
2067
2068   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2069     return 0;
2070
2071   arg = TREE_VALUE (arglist);
2072
2073   switch (DECL_FUNCTION_CODE (fndecl))
2074     {
2075     CASE_FLT_FN (BUILT_IN_SIN):
2076     CASE_FLT_FN (BUILT_IN_COS):
2077       builtin_optab = sincos_optab; break;
2078     default:
2079       gcc_unreachable ();
2080     }
2081
2082   /* Make a suitable register to place result in.  */
2083   mode = TYPE_MODE (TREE_TYPE (exp));
2084
2085   /* Check if sincos insn is available, otherwise fallback
2086      to sin or cos insn.  */
2087   if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2088     switch (DECL_FUNCTION_CODE (fndecl))
2089       {
2090       CASE_FLT_FN (BUILT_IN_SIN):
2091         builtin_optab = sin_optab; break;
2092       CASE_FLT_FN (BUILT_IN_COS):
2093         builtin_optab = cos_optab; break;
2094       default:
2095         gcc_unreachable ();
2096       }
2097   }
2098
2099   /* Before working hard, check whether the instruction is available.  */
2100   if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2101     {
2102       target = gen_reg_rtx (mode);
2103
2104       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2105          need to expand the argument again.  This way, we will not perform
2106          side-effects more the once.  */
2107       narg = save_expr (arg);
2108       if (narg != arg)
2109         {
2110           arg = narg;
2111           arglist = build_tree_list (NULL_TREE, arg);
2112           exp = build_function_call_expr (fndecl, arglist);
2113         }
2114
2115       op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2116
2117       start_sequence ();
2118
2119       /* Compute into TARGET.
2120          Set TARGET to wherever the result comes back.  */
2121       if (builtin_optab == sincos_optab)
2122         {
2123           int result;
2124
2125           switch (DECL_FUNCTION_CODE (fndecl))
2126             {
2127             CASE_FLT_FN (BUILT_IN_SIN):
2128               result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2129               break;
2130             CASE_FLT_FN (BUILT_IN_COS):
2131               result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2132               break;
2133             default:
2134               gcc_unreachable ();
2135             }
2136           gcc_assert (result);
2137         }
2138       else
2139         {
2140           target = expand_unop (mode, builtin_optab, op0, target, 0);
2141         }
2142
2143       if (target != 0)
2144         {
2145           /* Output the entire sequence.  */
2146           insns = get_insns ();
2147           end_sequence ();
2148           emit_insn (insns);
2149           return target;
2150         }
2151
2152       /* If we were unable to expand via the builtin, stop the sequence
2153          (without outputting the insns) and call to the library function
2154          with the stabilized argument list.  */
2155       end_sequence ();
2156     }
2157
2158   target = expand_call (exp, target, target == const0_rtx);
2159
2160   return target;
2161 }
2162
2163 /* Expand a call to the builtin sincos math function.
2164    Return 0 if a normal call should be emitted rather than expanding the
2165    function in-line.  EXP is the expression that is a call to the builtin
2166    function.  */
2167
2168 static rtx
2169 expand_builtin_sincos (tree exp)
2170 {
2171   rtx op0, op1, op2, target1, target2;
2172   tree arglist = TREE_OPERAND (exp, 1);
2173   enum machine_mode mode;
2174   tree arg, sinp, cosp;
2175   int result;
2176
2177   if (!validate_arglist (arglist, REAL_TYPE,
2178                          POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2179     return 0;
2180
2181   arg = TREE_VALUE (arglist);
2182   sinp = TREE_VALUE (TREE_CHAIN (arglist));
2183   cosp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2184
2185   /* Make a suitable register to place result in.  */
2186   mode = TYPE_MODE (TREE_TYPE (arg));
2187
2188   /* Check if sincos insn is available, otherwise emit the call.  */
2189   if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2190     return NULL_RTX;
2191
2192   target1 = gen_reg_rtx (mode);
2193   target2 = gen_reg_rtx (mode);
2194
2195   op0 = expand_normal (arg);
2196   op1 = expand_normal (build_fold_indirect_ref (sinp));
2197   op2 = expand_normal (build_fold_indirect_ref (cosp));
2198
2199   /* Compute into target1 and target2.
2200      Set TARGET to wherever the result comes back.  */
2201   result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2202   gcc_assert (result);
2203
2204   /* Move target1 and target2 to the memory locations indicated
2205      by op1 and op2.  */
2206   emit_move_insn (op1, target1);
2207   emit_move_insn (op2, target2);
2208
2209   return const0_rtx;
2210 }
2211
2212 /* Expand a call to one of the builtin rounding functions gcc defines
2213    as an extension (lfloor and lceil).  As these are gcc extensions we
2214    do not need to worry about setting errno to EDOM.
2215    If expanding via optab fails, lower expression to (int)(floor(x)).
2216    EXP is the expression that is a call to the builtin function;
2217    if convenient, the result should be placed in TARGET.  SUBTARGET may
2218    be used as the target for computing one of EXP's operands.  */
2219
2220 static rtx
2221 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2222 {
2223   optab builtin_optab;
2224   rtx op0, insns, tmp;
2225   tree fndecl = get_callee_fndecl (exp);
2226   tree arglist = TREE_OPERAND (exp, 1);
2227   enum built_in_function fallback_fn;
2228   tree fallback_fndecl;
2229   enum machine_mode mode;
2230   tree arg, narg;
2231
2232   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2233     gcc_unreachable ();
2234
2235   arg = TREE_VALUE (arglist);
2236
2237   switch (DECL_FUNCTION_CODE (fndecl))
2238     {
2239     CASE_FLT_FN (BUILT_IN_LCEIL):
2240     CASE_FLT_FN (BUILT_IN_LLCEIL):
2241       builtin_optab = lceil_optab;
2242       fallback_fn = BUILT_IN_CEIL;
2243       break;
2244
2245     CASE_FLT_FN (BUILT_IN_LFLOOR):
2246     CASE_FLT_FN (BUILT_IN_LLFLOOR):
2247       builtin_optab = lfloor_optab;
2248       fallback_fn = BUILT_IN_FLOOR;
2249       break;
2250
2251     default:
2252       gcc_unreachable ();
2253     }
2254
2255   /* Make a suitable register to place result in.  */
2256   mode = TYPE_MODE (TREE_TYPE (exp));
2257
2258   /* Before working hard, check whether the instruction is available.  */
2259   if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2260     {
2261       target = gen_reg_rtx (mode);
2262
2263       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2264          need to expand the argument again.  This way, we will not perform
2265          side-effects more the once.  */
2266       narg = builtin_save_expr (arg);
2267       if (narg != arg)
2268         {
2269           arg = narg;
2270           arglist = build_tree_list (NULL_TREE, arg);
2271           exp = build_function_call_expr (fndecl, arglist);
2272         }
2273
2274       op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2275
2276       start_sequence ();
2277
2278       /* Compute into TARGET.
2279          Set TARGET to wherever the result comes back.  */
2280       target = expand_unop (mode, builtin_optab, op0, target, 0);
2281
2282       if (target != 0)
2283         {
2284           /* Output the entire sequence.  */
2285           insns = get_insns ();
2286           end_sequence ();
2287           emit_insn (insns);
2288           return target;
2289         }
2290
2291       /* If we were unable to expand via the builtin, stop the sequence
2292          (without outputting the insns).  */
2293       end_sequence ();
2294     }
2295
2296   /* Fall back to floating point rounding optab.  */
2297   fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2298   /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2299      ??? Perhaps convert (int)floorf(x) into (int)floor((double)x).  */
2300   gcc_assert (fallback_fndecl != NULL_TREE);
2301   exp = build_function_call_expr (fallback_fndecl, arglist);
2302
2303   tmp = expand_normal (exp);
2304
2305   /* Truncate the result of floating point optab to integer
2306      via expand_fix ().  */
2307   target = gen_reg_rtx (mode);
2308   expand_fix (target, tmp, 0);
2309
2310   return target;
2311 }
2312
2313 /* Expand a call to one of the builtin math functions doing integer
2314    conversion (lrint).
2315    Return 0 if a normal call should be emitted rather than expanding the
2316    function in-line.  EXP is the expression that is a call to the builtin
2317    function; if convenient, the result should be placed in TARGET.
2318    SUBTARGET may be used as the target for computing one of EXP's operands.  */
2319
2320 static rtx
2321 expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget)
2322 {
2323   optab builtin_optab;
2324   rtx op0, insns;
2325   tree fndecl = get_callee_fndecl (exp);
2326   tree arglist = TREE_OPERAND (exp, 1);
2327   enum machine_mode mode;
2328   tree arg, narg;
2329
2330   /* There's no easy way to detect the case we need to set EDOM.  */
2331   if (flag_errno_math)
2332     return NULL_RTX;
2333
2334   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2335     return NULL_RTX;
2336
2337   arg = TREE_VALUE (arglist);
2338
2339   switch (DECL_FUNCTION_CODE (fndecl))
2340     {
2341     CASE_FLT_FN (BUILT_IN_LRINT):
2342     CASE_FLT_FN (BUILT_IN_LLRINT):
2343       builtin_optab = lrint_optab; break;
2344     default:
2345       gcc_unreachable ();
2346     }
2347
2348   /* Make a suitable register to place result in.  */
2349   mode = TYPE_MODE (TREE_TYPE (exp));
2350
2351   /* Before working hard, check whether the instruction is available.  */
2352   if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2353     {
2354       target = gen_reg_rtx (mode);
2355
2356       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2357          need to expand the argument again.  This way, we will not perform
2358          side-effects more the once.  */
2359       narg = builtin_save_expr (arg);
2360       if (narg != arg)
2361         {
2362           arg = narg;
2363           arglist = build_tree_list (NULL_TREE, arg);
2364           exp = build_function_call_expr (fndecl, arglist);
2365         }
2366
2367       op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2368
2369       start_sequence ();
2370
2371       /* Compute into TARGET.
2372          Set TARGET to wherever the result comes back.  */
2373       target = expand_unop (mode, builtin_optab, op0, target, 0);
2374
2375       if (target != 0)
2376         {
2377           /* Output the entire sequence.  */
2378           insns = get_insns ();
2379           end_sequence ();
2380           emit_insn (insns);
2381           return target;
2382         }
2383
2384       /* If we were unable to expand via the builtin, stop the sequence
2385          (without outputting the insns) and call to the library function
2386          with the stabilized argument list.  */
2387       end_sequence ();
2388     }
2389
2390   target = expand_call (exp, target, target == const0_rtx);
2391
2392   return target;
2393 }
2394
2395 /* To evaluate powi(x,n), the floating point value x raised to the
2396    constant integer exponent n, we use a hybrid algorithm that
2397    combines the "window method" with look-up tables.  For an
2398    introduction to exponentiation algorithms and "addition chains",
2399    see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2400    "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2401    3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2402    Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998.  */
2403
2404 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2405    multiplications to inline before calling the system library's pow
2406    function.  powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2407    so this default never requires calling pow, powf or powl.  */
2408
2409 #ifndef POWI_MAX_MULTS
2410 #define POWI_MAX_MULTS  (2*HOST_BITS_PER_WIDE_INT-2)
2411 #endif
2412
2413 /* The size of the "optimal power tree" lookup table.  All
2414    exponents less than this value are simply looked up in the
2415    powi_table below.  This threshold is also used to size the
2416    cache of pseudo registers that hold intermediate results.  */
2417 #define POWI_TABLE_SIZE 256
2418
2419 /* The size, in bits of the window, used in the "window method"
2420    exponentiation algorithm.  This is equivalent to a radix of
2421    (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method".  */
2422 #define POWI_WINDOW_SIZE 3
2423
2424 /* The following table is an efficient representation of an
2425    "optimal power tree".  For each value, i, the corresponding
2426    value, j, in the table states than an optimal evaluation
2427    sequence for calculating pow(x,i) can be found by evaluating
2428    pow(x,j)*pow(x,i-j).  An optimal power tree for the first
2429    100 integers is given in Knuth's "Seminumerical algorithms".  */
2430
2431 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2432   {
2433       0,   1,   1,   2,   2,   3,   3,   4,  /*   0 -   7 */
2434       4,   6,   5,   6,   6,  10,   7,   9,  /*   8 -  15 */
2435       8,  16,   9,  16,  10,  12,  11,  13,  /*  16 -  23 */
2436      12,  17,  13,  18,  14,  24,  15,  26,  /*  24 -  31 */
2437      16,  17,  17,  19,  18,  33,  19,  26,  /*  32 -  39 */
2438      20,  25,  21,  40,  22,  27,  23,  44,  /*  40 -  47 */
2439      24,  32,  25,  34,  26,  29,  27,  44,  /*  48 -  55 */
2440      28,  31,  29,  34,  30,  60,  31,  36,  /*  56 -  63 */
2441      32,  64,  33,  34,  34,  46,  35,  37,  /*  64 -  71 */
2442      36,  65,  37,  50,  38,  48,  39,  69,  /*  72 -  79 */
2443      40,  49,  41,  43,  42,  51,  43,  58,  /*  80 -  87 */
2444      44,  64,  45,  47,  46,  59,  47,  76,  /*  88 -  95 */
2445      48,  65,  49,  66,  50,  67,  51,  66,  /*  96 - 103 */
2446      52,  70,  53,  74,  54, 104,  55,  74,  /* 104 - 111 */
2447      56,  64,  57,  69,  58,  78,  59,  68,  /* 112 - 119 */
2448      60,  61,  61,  80,  62,  75,  63,  68,  /* 120 - 127 */
2449      64,  65,  65, 128,  66, 129,  67,  90,  /* 128 - 135 */
2450      68,  73,  69, 131,  70,  94,  71,  88,  /* 136 - 143 */
2451      72, 128,  73,  98,  74, 132,  75, 121,  /* 144 - 151 */
2452      76, 102,  77, 124,  78, 132,  79, 106,  /* 152 - 159 */
2453      80,  97,  81, 160,  82,  99,  83, 134,  /* 160 - 167 */
2454      84,  86,  85,  95,  86, 160,  87, 100,  /* 168 - 175 */
2455      88, 113,  89,  98,  90, 107,  91, 122,  /* 176 - 183 */
2456      92, 111,  93, 102,  94, 126,  95, 150,  /* 184 - 191 */
2457      96, 128,  97, 130,  98, 133,  99, 195,  /* 192 - 199 */
2458     100, 128, 101, 123, 102, 164, 103, 138,  /* 200 - 207 */
2459     104, 145, 105, 146, 106, 109, 107, 149,  /* 208 - 215 */
2460     108, 200, 109, 146, 110, 170, 111, 157,  /* 216 - 223 */
2461     112, 128, 113, 130, 114, 182, 115, 132,  /* 224 - 231 */
2462     116, 200, 117, 132, 118, 158, 119, 206,  /* 232 - 239 */
2463     120, 240, 121, 162, 122, 147, 123, 152,  /* 240 - 247 */
2464     124, 166, 125, 214, 126, 138, 127, 153,  /* 248 - 255 */
2465   };
2466
2467
2468 /* Return the number of multiplications required to calculate
2469    powi(x,n) where n is less than POWI_TABLE_SIZE.  This is a
2470    subroutine of powi_cost.  CACHE is an array indicating
2471    which exponents have already been calculated.  */
2472
2473 static int
2474 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2475 {
2476   /* If we've already calculated this exponent, then this evaluation
2477      doesn't require any additional multiplications.  */
2478   if (cache[n])
2479     return 0;
2480
2481   cache[n] = true;
2482   return powi_lookup_cost (n - powi_table[n], cache)
2483          + powi_lookup_cost (powi_table[n], cache) + 1;
2484 }
2485
2486 /* Return the number of multiplications required to calculate
2487    powi(x,n) for an arbitrary x, given the exponent N.  This
2488    function needs to be kept in sync with expand_powi below.  */
2489
2490 static int
2491 powi_cost (HOST_WIDE_INT n)
2492 {
2493   bool cache[POWI_TABLE_SIZE];
2494   unsigned HOST_WIDE_INT digit;
2495   unsigned HOST_WIDE_INT val;
2496   int result;
2497
2498   if (n == 0)
2499     return 0;
2500
2501   /* Ignore the reciprocal when calculating the cost.  */
2502   val = (n < 0) ? -n : n;
2503
2504   /* Initialize the exponent cache.  */
2505   memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2506   cache[1] = true;
2507
2508   result = 0;
2509
2510   while (val >= POWI_TABLE_SIZE)
2511     {
2512       if (val & 1)
2513         {
2514           digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2515           result += powi_lookup_cost (digit, cache)
2516                     + POWI_WINDOW_SIZE + 1;
2517           val >>= POWI_WINDOW_SIZE;
2518         }
2519       else
2520         {
2521           val >>= 1;
2522           result++;
2523         }
2524     }
2525
2526   return result + powi_lookup_cost (val, cache);
2527 }
2528
2529 /* Recursive subroutine of expand_powi.  This function takes the array,
2530    CACHE, of already calculated exponents and an exponent N and returns
2531    an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE.  */
2532
2533 static rtx
2534 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2535 {
2536   unsigned HOST_WIDE_INT digit;
2537   rtx target, result;
2538   rtx op0, op1;
2539
2540   if (n < POWI_TABLE_SIZE)
2541     {
2542       if (cache[n])
2543         return cache[n];
2544
2545       target = gen_reg_rtx (mode);
2546       cache[n] = target;
2547
2548       op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2549       op1 = expand_powi_1 (mode, powi_table[n], cache);
2550     }
2551   else if (n & 1)
2552     {
2553       target = gen_reg_rtx (mode);
2554       digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2555       op0 = expand_powi_1 (mode, n - digit, cache);
2556       op1 = expand_powi_1 (mode, digit, cache);
2557     }
2558   else
2559     {
2560       target = gen_reg_rtx (mode);
2561       op0 = expand_powi_1 (mode, n >> 1, cache);
2562       op1 = op0;
2563     }
2564
2565   result = expand_mult (mode, op0, op1, target, 0);
2566   if (result != target)
2567     emit_move_insn (target, result);
2568   return target;
2569 }
2570
2571 /* Expand the RTL to evaluate powi(x,n) in mode MODE.  X is the
2572    floating point operand in mode MODE, and N is the exponent.  This
2573    function needs to be kept in sync with powi_cost above.  */
2574
2575 static rtx
2576 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2577 {
2578   unsigned HOST_WIDE_INT val;
2579   rtx cache[POWI_TABLE_SIZE];
2580   rtx result;
2581
2582   if (n == 0)
2583     return CONST1_RTX (mode);
2584
2585   val = (n < 0) ? -n : n;
2586
2587   memset (cache, 0, sizeof (cache));
2588   cache[1] = x;
2589
2590   result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2591
2592   /* If the original exponent was negative, reciprocate the result.  */
2593   if (n < 0)
2594     result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2595                            result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2596
2597   return result;
2598 }
2599
2600 /* Expand a call to the pow built-in mathematical function.  Return 0 if
2601    a normal call should be emitted rather than expanding the function
2602    in-line.  EXP is the expression that is a call to the builtin
2603    function; if convenient, the result should be placed in TARGET.  */
2604
2605 static rtx
2606 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2607 {
2608   tree arglist = TREE_OPERAND (exp, 1);
2609   tree arg0, arg1;
2610
2611   if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2612     return 0;
2613
2614   arg0 = TREE_VALUE (arglist);
2615   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2616
2617   if (TREE_CODE (arg1) == REAL_CST
2618       && ! TREE_CONSTANT_OVERFLOW (arg1))
2619     {
2620       REAL_VALUE_TYPE cint;
2621       REAL_VALUE_TYPE c;
2622       HOST_WIDE_INT n;
2623
2624       c = TREE_REAL_CST (arg1);
2625       n = real_to_integer (&c);
2626       real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2627       if (real_identical (&c, &cint))
2628         {
2629           /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2630              Otherwise, check the number of multiplications required.
2631              Note that pow never sets errno for an integer exponent.  */
2632           if ((n >= -1 && n <= 2)
2633               || (flag_unsafe_math_optimizations
2634                   && ! optimize_size
2635                   && powi_cost (n) <= POWI_MAX_MULTS))
2636             {
2637               enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2638               rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2639               op = force_reg (mode, op);
2640               return expand_powi (op, mode, n);
2641             }
2642         }
2643     }
2644
2645   if (! flag_unsafe_math_optimizations)
2646     return NULL_RTX;
2647   return expand_builtin_mathfn_2 (exp, target, subtarget);
2648 }
2649
2650 /* Expand a call to the powi built-in mathematical function.  Return 0 if
2651    a normal call should be emitted rather than expanding the function
2652    in-line.  EXP is the expression that is a call to the builtin
2653    function; if convenient, the result should be placed in TARGET.  */
2654
2655 static rtx
2656 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2657 {
2658   tree arglist = TREE_OPERAND (exp, 1);
2659   tree arg0, arg1;
2660   rtx op0, op1;
2661   enum machine_mode mode;
2662   enum machine_mode mode2;
2663
2664   if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2665     return 0;
2666
2667   arg0 = TREE_VALUE (arglist);
2668   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2669   mode = TYPE_MODE (TREE_TYPE (exp));
2670
2671   /* Handle constant power.  */
2672
2673   if (TREE_CODE (arg1) == INTEGER_CST
2674       && ! TREE_CONSTANT_OVERFLOW (arg1))
2675     {
2676       HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2677
2678       /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2679          Otherwise, check the number of multiplications required.  */
2680       if ((TREE_INT_CST_HIGH (arg1) == 0
2681            || TREE_INT_CST_HIGH (arg1) == -1)
2682           && ((n >= -1 && n <= 2)
2683               || (! optimize_size
2684                   && powi_cost (n) <= POWI_MAX_MULTS)))
2685         {
2686           op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2687           op0 = force_reg (mode, op0);
2688           return expand_powi (op0, mode, n);
2689         }
2690     }
2691
2692   /* Emit a libcall to libgcc.  */
2693
2694   /* Mode of the 2nd argument must match that of an int. */
2695   mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2696
2697   if (target == NULL_RTX)
2698     target = gen_reg_rtx (mode);
2699
2700   op0 = expand_expr (arg0, subtarget, mode, 0);
2701   if (GET_MODE (op0) != mode)
2702     op0 = convert_to_mode (mode, op0, 0);
2703   op1 = expand_expr (arg1, 0, mode2, 0);
2704   if (GET_MODE (op1) != mode2)
2705     op1 = convert_to_mode (mode2, op1, 0);
2706
2707   target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2708                                     target, LCT_CONST_MAKE_BLOCK, mode, 2,
2709                                     op0, mode, op1, mode2);
2710
2711   return target;
2712 }
2713
2714 /* Expand expression EXP which is a call to the strlen builtin.  Return 0
2715    if we failed the caller should emit a normal call, otherwise
2716    try to get the result in TARGET, if convenient.  */
2717
2718 static rtx
2719 expand_builtin_strlen (tree arglist, rtx target,
2720                        enum machine_mode target_mode)
2721 {
2722   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2723     return 0;
2724   else
2725     {
2726       rtx pat;
2727       tree len, src = TREE_VALUE (arglist);
2728       rtx result, src_reg, char_rtx, before_strlen;
2729       enum machine_mode insn_mode = target_mode, char_mode;
2730       enum insn_code icode = CODE_FOR_nothing;
2731       int align;
2732
2733       /* If the length can be computed at compile-time, return it.  */
2734       len = c_strlen (src, 0);
2735       if (len)
2736         return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2737
2738       /* If the length can be computed at compile-time and is constant
2739          integer, but there are side-effects in src, evaluate
2740          src for side-effects, then return len.
2741          E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2742          can be optimized into: i++; x = 3;  */
2743       len = c_strlen (src, 1);
2744       if (len && TREE_CODE (len) == INTEGER_CST)
2745         {
2746           expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2747           return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2748         }
2749
2750       align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2751
2752       /* If SRC is not a pointer type, don't do this operation inline.  */
2753       if (align == 0)
2754         return 0;
2755
2756       /* Bail out if we can't compute strlen in the right mode.  */
2757       while (insn_mode != VOIDmode)
2758         {
2759           icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2760           if (icode != CODE_FOR_nothing)
2761             break;
2762
2763           insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2764         }
2765       if (insn_mode == VOIDmode)
2766         return 0;
2767
2768       /* Make a place to write the result of the instruction.  */
2769       result = target;
2770       if (! (result != 0
2771              && REG_P (result)
2772              && GET_MODE (result) == insn_mode
2773              && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2774         result = gen_reg_rtx (insn_mode);
2775
2776       /* Make a place to hold the source address.  We will not expand
2777          the actual source until we are sure that the expansion will
2778          not fail -- there are trees that cannot be expanded twice.  */
2779       src_reg = gen_reg_rtx (Pmode);
2780
2781       /* Mark the beginning of the strlen sequence so we can emit the
2782          source operand later.  */
2783       before_strlen = get_last_insn ();
2784
2785       char_rtx = const0_rtx;
2786       char_mode = insn_data[(int) icode].operand[2].mode;
2787       if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2788                                                             char_mode))
2789         char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2790
2791       pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2792                              char_rtx, GEN_INT (align));
2793       if (! pat)
2794         return 0;
2795       emit_insn (pat);
2796
2797       /* Now that we are assured of success, expand the source.  */
2798       start_sequence ();
2799       pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2800       if (pat != src_reg)
2801         emit_move_insn (src_reg, pat);
2802       pat = get_insns ();
2803       end_sequence ();
2804
2805       if (before_strlen)
2806         emit_insn_after (pat, before_strlen);
2807       else
2808         emit_insn_before (pat, get_insns ());
2809
2810       /* Return the value in the proper mode for this function.  */
2811       if (GET_MODE (result) == target_mode)
2812         target = result;
2813       else if (target != 0)
2814         convert_move (target, result, 0);
2815       else
2816         target = convert_to_mode (target_mode, result, 0);
2817
2818       return target;
2819     }
2820 }
2821
2822 /* Expand a call to the strstr builtin.  Return 0 if we failed the
2823    caller should emit a normal call, otherwise try to get the result
2824    in TARGET, if convenient (and in mode MODE if that's convenient).  */
2825
2826 static rtx
2827 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2828 {
2829   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2830     {
2831       tree result = fold_builtin_strstr (arglist, type);
2832       if (result)
2833         return expand_expr (result, target, mode, EXPAND_NORMAL);
2834     }
2835   return 0;
2836 }
2837
2838 /* Expand a call to the strchr builtin.  Return 0 if we failed the
2839    caller should emit a normal call, otherwise try to get the result
2840    in TARGET, if convenient (and in mode MODE if that's convenient).  */
2841
2842 static rtx
2843 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2844 {
2845   if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2846     {
2847       tree result = fold_builtin_strchr (arglist, type);
2848       if (result)
2849         return expand_expr (result, target, mode, EXPAND_NORMAL);
2850
2851       /* FIXME: Should use strchrM optab so that ports can optimize this.  */
2852     }
2853   return 0;
2854 }
2855
2856 /* Expand a call to the strrchr builtin.  Return 0 if we failed the
2857    caller should emit a normal call, otherwise try to get the result
2858    in TARGET, if convenient (and in mode MODE if that's convenient).  */
2859
2860 static rtx
2861 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2862 {
2863   if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2864     {
2865       tree result = fold_builtin_strrchr (arglist, type);
2866       if (result)
2867         return expand_expr (result, target, mode, EXPAND_NORMAL);
2868     }
2869   return 0;
2870 }
2871
2872 /* Expand a call to the strpbrk builtin.  Return 0 if we failed the
2873    caller should emit a normal call, otherwise try to get the result
2874    in TARGET, if convenient (and in mode MODE if that's convenient).  */
2875
2876 static rtx
2877 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2878 {
2879   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2880     {
2881       tree result = fold_builtin_strpbrk (arglist, type);
2882       if (result)
2883         return expand_expr (result, target, mode, EXPAND_NORMAL);
2884     }
2885   return 0;
2886 }
2887
2888 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
2889    bytes from constant string DATA + OFFSET and return it as target
2890    constant.  */
2891
2892 static rtx
2893 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2894                          enum machine_mode mode)
2895 {
2896   const char *str = (const char *) data;
2897
2898   gcc_assert (offset >= 0
2899               && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2900                   <= strlen (str) + 1));
2901
2902   return c_readstr (str + offset, mode);
2903 }
2904
2905 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2906    Return 0 if we failed, the caller should emit a normal call,
2907    otherwise try to get the result in TARGET, if convenient (and in
2908    mode MODE if that's convenient).  */
2909 static rtx
2910 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2911 {
2912   tree fndecl = get_callee_fndecl (exp);
2913   tree arglist = TREE_OPERAND (exp, 1);
2914   if (!validate_arglist (arglist,
2915                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2916     return 0;
2917   else
2918     {
2919       tree dest = TREE_VALUE (arglist);
2920       tree src = TREE_VALUE (TREE_CHAIN (arglist));
2921       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2922       const char *src_str;
2923       unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2924       unsigned int dest_align
2925         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2926       rtx dest_mem, src_mem, dest_addr, len_rtx;
2927       tree result = fold_builtin_memory_op (arglist, TREE_TYPE (TREE_TYPE (fndecl)),
2928                                             false, /*endp=*/0);
2929
2930       if (result)
2931         {
2932           while (TREE_CODE (result) == COMPOUND_EXPR)
2933             {
2934               expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
2935                            EXPAND_NORMAL);
2936               result = TREE_OPERAND (result, 1);
2937             }
2938           return expand_expr (result, target, mode, EXPAND_NORMAL);
2939         }
2940
2941       /* If DEST is not a pointer type, call the normal function.  */
2942       if (dest_align == 0)
2943         return 0;
2944
2945       /* If either SRC is not a pointer type, don't do this
2946          operation in-line.  */
2947       if (src_align == 0)
2948         return 0;
2949
2950       dest_mem = get_memory_rtx (dest, len);
2951       set_mem_align (dest_mem, dest_align);
2952       len_rtx = expand_normal (len);
2953       src_str = c_getstr (src);
2954
2955       /* If SRC is a string constant and block move would be done
2956          by pieces, we can avoid loading the string from memory
2957          and only stored the computed constants.  */
2958       if (src_str
2959           && GET_CODE (len_rtx) == CONST_INT
2960           && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2961           && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2962                                   (void *) src_str, dest_align))
2963         {
2964           dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2965                                       builtin_memcpy_read_str,
2966                                       (void *) src_str, dest_align, 0);
2967           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2968           dest_mem = convert_memory_address (ptr_mode, dest_mem);
2969           return dest_mem;
2970         }
2971
2972       src_mem = get_memory_rtx (src, len);
2973       set_mem_align (src_mem, src_align);
2974
2975       /* Copy word part most expediently.  */
2976       dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2977                                    CALL_EXPR_TAILCALL (exp)
2978                                    ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
2979
2980       if (dest_addr == 0)
2981         {
2982           dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2983           dest_addr = convert_memory_address (ptr_mode, dest_addr);
2984         }
2985       return dest_addr;
2986     }
2987 }
2988
2989 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2990    Return 0 if we failed; the caller should emit a normal call,
2991    otherwise try to get the result in TARGET, if convenient (and in
2992    mode MODE if that's convenient).  If ENDP is 0 return the
2993    destination pointer, if ENDP is 1 return the end pointer ala
2994    mempcpy, and if ENDP is 2 return the end pointer minus one ala
2995    stpcpy.  */
2996
2997 static rtx
2998 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2999                         int endp)
3000 {
3001   if (!validate_arglist (arglist,
3002                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3003     return 0;
3004   /* If return value is ignored, transform mempcpy into memcpy.  */
3005   else if (target == const0_rtx)
3006     {
3007       tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3008
3009       if (!fn)
3010         return 0;
3011
3012       return expand_expr (build_function_call_expr (fn, arglist),
3013                           target, mode, EXPAND_NORMAL);
3014     }
3015   else
3016     {
3017       tree dest = TREE_VALUE (arglist);
3018       tree src = TREE_VALUE (TREE_CHAIN (arglist));
3019       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3020       const char *src_str;
3021       unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3022       unsigned int dest_align
3023         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3024       rtx dest_mem, src_mem, len_rtx;
3025       tree result = fold_builtin_memory_op (arglist, type, false, endp);
3026
3027       if (result)
3028         {
3029           while (TREE_CODE (result) == COMPOUND_EXPR)
3030             {
3031               expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3032                            EXPAND_NORMAL);
3033               result = TREE_OPERAND (result, 1);
3034             }
3035           return expand_expr (result, target, mode, EXPAND_NORMAL);
3036         }
3037
3038       /* If either SRC or DEST is not a pointer type, don't do this
3039          operation in-line.  */
3040       if (dest_align == 0 || src_align == 0)
3041         return 0;
3042
3043       /* If LEN is not constant, call the normal function.  */
3044       if (! host_integerp (len, 1))
3045         return 0;
3046
3047       len_rtx = expand_normal (len);
3048       src_str = c_getstr (src);
3049
3050       /* If SRC is a string constant and block move would be done
3051          by pieces, we can avoid loading the string from memory
3052          and only stored the computed constants.  */
3053       if (src_str
3054           && GET_CODE (len_rtx) == CONST_INT
3055           && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3056           && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3057                                   (void *) src_str, dest_align))
3058         {
3059           dest_mem = get_memory_rtx (dest, len);
3060           set_mem_align (dest_mem, dest_align);
3061           dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3062                                       builtin_memcpy_read_str,
3063                                       (void *) src_str, dest_align, endp);
3064           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3065           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3066           return dest_mem;
3067         }
3068
3069       if (GET_CODE (len_rtx) == CONST_INT
3070           && can_move_by_pieces (INTVAL (len_rtx),
3071                                  MIN (dest_align, src_align)))
3072         {
3073           dest_mem = get_memory_rtx (dest, len);
3074           set_mem_align (dest_mem, dest_align);
3075           src_mem = get_memory_rtx (src, len);
3076           set_mem_align (src_mem, src_align);
3077           dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3078                                      MIN (dest_align, src_align), endp);
3079           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3080           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3081           return dest_mem;
3082         }
3083
3084       return 0;
3085     }
3086 }
3087
3088 /* Expand expression EXP, which is a call to the memmove builtin.  Return 0
3089    if we failed; the caller should emit a normal call.  */
3090
3091 static rtx
3092 expand_builtin_memmove (tree arglist, tree type, rtx target,
3093                         enum machine_mode mode)
3094 {
3095   if (!validate_arglist (arglist,
3096                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3097     return 0;
3098   else
3099     {
3100       tree result = fold_builtin_memory_op (arglist, type, false, /*endp=*/3);
3101
3102       if (result)
3103         {
3104           while (TREE_CODE (result) == COMPOUND_EXPR)
3105             {
3106               expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3107                            EXPAND_NORMAL);
3108               result = TREE_OPERAND (result, 1);
3109             }
3110           return expand_expr (result, target, mode, EXPAND_NORMAL);
3111         }
3112
3113       /* Otherwise, call the normal function.  */
3114       return 0;
3115    }
3116 }
3117
3118 /* Expand expression EXP, which is a call to the bcopy builtin.  Return 0
3119    if we failed the caller should emit a normal call.  */
3120
3121 static rtx
3122 expand_builtin_bcopy (tree exp)
3123 {
3124   tree arglist = TREE_OPERAND (exp, 1);
3125   tree type = TREE_TYPE (exp);
3126   tree src, dest, size, newarglist;
3127
3128   if (!validate_arglist (arglist,
3129                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3130     return NULL_RTX;
3131
3132   src = TREE_VALUE (arglist);
3133   dest = TREE_VALUE (TREE_CHAIN (arglist));
3134   size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3135
3136   /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3137      memmove(ptr y, ptr x, size_t z).   This is done this way
3138      so that if it isn't expanded inline, we fallback to
3139      calling bcopy instead of memmove.  */
3140
3141   newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3142   newarglist = tree_cons (NULL_TREE, src, newarglist);
3143   newarglist = tree_cons (NULL_TREE, dest, newarglist);
3144
3145   return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
3146 }
3147
3148 #ifndef HAVE_movstr
3149 # define HAVE_movstr 0
3150 # define CODE_FOR_movstr CODE_FOR_nothing
3151 #endif
3152
3153 /* Expand into a movstr instruction, if one is available.  Return 0 if
3154    we failed, the caller should emit a normal call, otherwise try to
3155    get the result in TARGET, if convenient.  If ENDP is 0 return the
3156    destination pointer, if ENDP is 1 return the end pointer ala
3157    mempcpy, and if ENDP is 2 return the end pointer minus one ala
3158    stpcpy.  */
3159
3160 static rtx
3161 expand_movstr (tree dest, tree src, rtx target, int endp)
3162 {
3163   rtx end;
3164   rtx dest_mem;
3165   rtx src_mem;
3166   rtx insn;
3167   const struct insn_data * data;
3168
3169   if (!HAVE_movstr)
3170     return 0;
3171
3172   dest_mem = get_memory_rtx (dest, NULL);
3173   src_mem = get_memory_rtx (src, NULL);
3174   if (!endp)
3175     {
3176       target = force_reg (Pmode, XEXP (dest_mem, 0));
3177       dest_mem = replace_equiv_address (dest_mem, target);
3178       end = gen_reg_rtx (Pmode);
3179     }
3180   else
3181     {
3182       if (target == 0 || target == const0_rtx)
3183         {
3184           end = gen_reg_rtx (Pmode);
3185           if (target == 0)
3186             target = end;
3187         }
3188       else
3189         end = target;
3190     }
3191
3192   data = insn_data + CODE_FOR_movstr;
3193
3194   if (data->operand[0].mode != VOIDmode)
3195     end = gen_lowpart (data->operand[0].mode, end);
3196
3197   insn = data->genfun (end, dest_mem, src_mem);
3198
3199   gcc_assert (insn);
3200
3201   emit_insn (insn);
3202
3203   /* movstr is supposed to set end to the address of the NUL
3204      terminator.  If the caller requested a mempcpy-like return value,
3205      adjust it.  */
3206   if (endp == 1 && target != const0_rtx)
3207     {
3208       rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3209       emit_move_insn (target, force_operand (tem, NULL_RTX));
3210     }
3211
3212   return target;
3213 }
3214
3215 /* Expand expression EXP, which is a call to the strcpy builtin.  Return 0
3216    if we failed the caller should emit a normal call, otherwise try to get
3217    the result in TARGET, if convenient (and in mode MODE if that's
3218    convenient).  */
3219
3220 static rtx
3221 expand_builtin_strcpy (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3222 {
3223   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3224     {
3225       tree result = fold_builtin_strcpy (fndecl, arglist, 0);
3226       if (result)
3227         {
3228           while (TREE_CODE (result) == COMPOUND_EXPR)
3229             {
3230               expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3231                            EXPAND_NORMAL);
3232               result = TREE_OPERAND (result, 1);
3233             }
3234           return expand_expr (result, target, mode, EXPAND_NORMAL);
3235         }
3236
3237       return expand_movstr (TREE_VALUE (arglist),
3238                             TREE_VALUE (TREE_CHAIN (arglist)),
3239                             target, /*endp=*/0);
3240     }
3241   return 0;
3242 }
3243
3244 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3245    Return 0 if we failed the caller should emit a normal call,
3246    otherwise try to get the result in TARGET, if convenient (and in
3247    mode MODE if that's convenient).  */
3248
3249 static rtx
3250 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3251 {
3252   tree arglist = TREE_OPERAND (exp, 1);
3253   /* If return value is ignored, transform stpcpy into strcpy.  */
3254   if (target == const0_rtx)
3255     {
3256       tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3257       if (!fn)
3258         return 0;
3259
3260       return expand_expr (build_function_call_expr (fn, arglist),
3261                           target, mode, EXPAND_NORMAL);
3262     }
3263
3264   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3265     return 0;
3266   else
3267     {
3268       tree dst, src, len, lenp1;
3269       tree narglist;
3270       rtx ret;
3271
3272       /* Ensure we get an actual string whose length can be evaluated at
3273          compile-time, not an expression containing a string.  This is
3274          because the latter will potentially produce pessimized code
3275          when used to produce the return value.  */
3276       src = TREE_VALUE (TREE_CHAIN (arglist));
3277       if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3278         return expand_movstr (TREE_VALUE (arglist),
3279                               TREE_VALUE (TREE_CHAIN (arglist)),
3280                               target, /*endp=*/2);
3281
3282       dst = TREE_VALUE (arglist);
3283       lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3284       narglist = build_tree_list (NULL_TREE, lenp1);
3285       narglist = tree_cons (NULL_TREE, src, narglist);
3286       narglist = tree_cons (NULL_TREE, dst, narglist);
3287       ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3288                                     target, mode, /*endp=*/2);
3289
3290       if (ret)
3291         return ret;
3292
3293       if (TREE_CODE (len) == INTEGER_CST)
3294         {
3295           rtx len_rtx = expand_normal (len);
3296
3297           if (GET_CODE (len_rtx) == CONST_INT)
3298             {
3299               ret = expand_builtin_strcpy (get_callee_fndecl (exp),
3300                                            arglist, target, mode);
3301
3302               if (ret)
3303                 {
3304                   if (! target)
3305                     {
3306                       if (mode != VOIDmode)
3307                         target = gen_reg_rtx (mode);
3308                       else
3309                         target = gen_reg_rtx (GET_MODE (ret));
3310                     }
3311                   if (GET_MODE (target) != GET_MODE (ret))
3312                     ret = gen_lowpart (GET_MODE (target), ret);
3313
3314                   ret = plus_constant (ret, INTVAL (len_rtx));
3315                   ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3316                   gcc_assert (ret);
3317
3318                   return target;
3319                 }
3320             }
3321         }
3322
3323       return expand_movstr (TREE_VALUE (arglist),
3324                             TREE_VALUE (TREE_CHAIN (arglist)),
3325                             target, /*endp=*/2);
3326     }
3327 }
3328
3329 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
3330    bytes from constant string DATA + OFFSET and return it as target
3331    constant.  */
3332
3333 static rtx
3334 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3335                           enum machine_mode mode)
3336 {
3337   const char *str = (const char *) data;
3338
3339   if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3340     return const0_rtx;
3341
3342   return c_readstr (str + offset, mode);
3343 }
3344
3345 /* Expand expression EXP, which is a call to the strncpy builtin.  Return 0
3346    if we failed the caller should emit a normal call.  */
3347
3348 static rtx
3349 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3350 {
3351   tree fndecl = get_callee_fndecl (exp);
3352   tree arglist = TREE_OPERAND (exp, 1);
3353   if (validate_arglist (arglist,
3354                         POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3355     {
3356       tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3357       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3358       tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3359
3360       if (result)
3361         {
3362           while (TREE_CODE (result) == COMPOUND_EXPR)
3363             {
3364               expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3365                            EXPAND_NORMAL);
3366               result = TREE_OPERAND (result, 1);
3367             }
3368           return expand_expr (result, target, mode, EXPAND_NORMAL);
3369         }
3370
3371       /* We must be passed a constant len and src parameter.  */
3372       if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3373         return 0;
3374
3375       slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3376
3377       /* We're required to pad with trailing zeros if the requested
3378          len is greater than strlen(s2)+1.  In that case try to
3379          use store_by_pieces, if it fails, punt.  */
3380       if (tree_int_cst_lt (slen, len))
3381         {
3382           tree dest = TREE_VALUE (arglist);
3383           unsigned int dest_align
3384             = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3385           const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3386           rtx dest_mem;
3387
3388           if (!p || dest_align == 0 || !host_integerp (len, 1)
3389               || !can_store_by_pieces (tree_low_cst (len, 1),
3390                                        builtin_strncpy_read_str,
3391                                        (void *) p, dest_align))
3392             return 0;
3393
3394           dest_mem = get_memory_rtx (dest, len);
3395           store_by_pieces (dest_mem, tree_low_cst (len, 1),
3396                            builtin_strncpy_read_str,
3397                            (void *) p, dest_align, 0);
3398           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3399           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3400           return dest_mem;
3401         }
3402     }
3403   return 0;
3404 }
3405
3406 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
3407    bytes from constant string DATA + OFFSET and return it as target
3408    constant.  */
3409
3410 static rtx
3411 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3412                          enum machine_mode mode)
3413 {
3414   const char *c = (const char *) data;
3415   char *p = alloca (GET_MODE_SIZE (mode));
3416
3417   memset (p, *c, GET_MODE_SIZE (mode));
3418
3419   return c_readstr (p, mode);
3420 }
3421
3422 /* Callback routine for store_by_pieces.  Return the RTL of a register
3423    containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3424    char value given in the RTL register data.  For example, if mode is
3425    4 bytes wide, return the RTL for 0x01010101*data.  */
3426
3427 static rtx
3428 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3429                         enum machine_mode mode)
3430 {
3431   rtx target, coeff;
3432   size_t size;
3433   char *p;
3434
3435   size = GET_MODE_SIZE (mode);
3436   if (size == 1)
3437     return (rtx) data;
3438
3439   p = alloca (size);
3440   memset (p, 1, size);
3441   coeff = c_readstr (p, mode);
3442
3443   target = convert_to_mode (mode, (rtx) data, 1);
3444   target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3445   return force_reg (mode, target);
3446 }
3447
3448 /* Expand expression EXP, which is a call to the memset builtin.  Return 0
3449    if we failed the caller should emit a normal call, otherwise try to get
3450    the result in TARGET, if convenient (and in mode MODE if that's
3451    convenient).  */
3452
3453 static rtx
3454 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode,
3455                        tree orig_exp)
3456 {
3457   if (!validate_arglist (arglist,
3458                          POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3459     return 0;
3460   else
3461     {
3462       tree dest = TREE_VALUE (arglist);
3463       tree val = TREE_VALUE (TREE_CHAIN (arglist));
3464       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3465       tree fndecl, fn;
3466       enum built_in_function fcode;
3467       char c;
3468       unsigned int dest_align;
3469       rtx dest_mem, dest_addr, len_rtx;
3470
3471       dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3472
3473       /* If DEST is not a pointer type, don't do this
3474          operation in-line.  */
3475       if (dest_align == 0)
3476         return 0;
3477
3478       /* If the LEN parameter is zero, return DEST.  */
3479       if (integer_zerop (len))
3480         {
3481           /* Evaluate and ignore VAL in case it has side-effects.  */
3482           expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3483           return expand_expr (dest, target, mode, EXPAND_NORMAL);
3484         }
3485
3486       /* Stabilize the arguments in case we fail.  */
3487       dest = builtin_save_expr (dest);
3488       val = builtin_save_expr (val);
3489       len = builtin_save_expr (len);
3490
3491       len_rtx = expand_normal (len);
3492       dest_mem = get_memory_rtx (dest, len);
3493
3494       if (TREE_CODE (val) != INTEGER_CST)
3495         {
3496           rtx val_rtx;
3497
3498           val_rtx = expand_normal (val);
3499           val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3500                                      val_rtx, 0);
3501
3502           /* Assume that we can memset by pieces if we can store the
3503            * the coefficients by pieces (in the required modes).
3504            * We can't pass builtin_memset_gen_str as that emits RTL.  */
3505           c = 1;
3506           if (host_integerp (len, 1)
3507               && !(optimize_size && tree_low_cst (len, 1) > 1)
3508               && can_store_by_pieces (tree_low_cst (len, 1),
3509                                       builtin_memset_read_str, &c, dest_align))
3510             {
3511               val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3512                                    val_rtx);
3513               store_by_pieces (dest_mem, tree_low_cst (len, 1),
3514                                builtin_memset_gen_str, val_rtx, dest_align, 0);
3515             }
3516           else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3517                                             dest_align))
3518             goto do_libcall;
3519
3520           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3521           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3522           return dest_mem;
3523         }
3524
3525       if (target_char_cast (val, &c))
3526         goto do_libcall;
3527
3528       if (c)
3529         {
3530           if (host_integerp (len, 1)
3531               && !(optimize_size && tree_low_cst (len, 1) > 1)
3532               && can_store_by_pieces (tree_low_cst (len, 1),
3533                                       builtin_memset_read_str, &c, dest_align))
3534             store_by_pieces (dest_mem, tree_low_cst (len, 1),
3535                              builtin_memset_read_str, &c, dest_align, 0);
3536           else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3537                                             dest_align))
3538             goto do_libcall;
3539
3540           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3541           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3542           return dest_mem;
3543         }
3544
3545       set_mem_align (dest_mem, dest_align);
3546       dest_addr = clear_storage (dest_mem, len_rtx,
3547                                  CALL_EXPR_TAILCALL (orig_exp)
3548                                  ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3549
3550       if (dest_addr == 0)
3551         {
3552           dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3553           dest_addr = convert_memory_address (ptr_mode, dest_addr);
3554         }
3555
3556       return dest_addr;
3557
3558     do_libcall:
3559       fndecl = get_callee_fndecl (orig_exp);
3560       fcode = DECL_FUNCTION_CODE (fndecl);
3561       gcc_assert (fcode == BUILT_IN_MEMSET || fcode == BUILT_IN_BZERO);
3562       arglist = build_tree_list (NULL_TREE, len);
3563       if (fcode == BUILT_IN_MEMSET)
3564         arglist = tree_cons (NULL_TREE, val, arglist);
3565       arglist = tree_cons (NULL_TREE, dest, arglist);
3566       fn = build_function_call_expr (fndecl, arglist);
3567       if (TREE_CODE (fn) == CALL_EXPR)
3568         CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3569       return expand_call (fn, target, target == const0_rtx);
3570     }
3571 }
3572
3573 /* Expand expression EXP, which is a call to the bzero builtin.  Return 0
3574    if we failed the caller should emit a normal call.  */
3575
3576 static rtx
3577 expand_builtin_bzero (tree exp)
3578 {
3579   tree arglist = TREE_OPERAND (exp, 1);
3580   tree dest, size, newarglist;
3581
3582   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3583     return NULL_RTX;
3584
3585   dest = TREE_VALUE (arglist);
3586   size = TREE_VALUE (TREE_CHAIN (arglist));
3587
3588   /* New argument list transforming bzero(ptr x, int y) to
3589      memset(ptr x, int 0, size_t y).   This is done this way
3590      so that if it isn't expanded inline, we fallback to
3591      calling bzero instead of memset.  */
3592
3593   newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3594   newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3595   newarglist = tree_cons (NULL_TREE, dest, newarglist);
3596
3597   return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp);
3598 }
3599
3600 /* Expand expression EXP, which is a call to the memcmp built-in function.
3601    ARGLIST is the argument list for this call.  Return 0 if we failed and the
3602    caller should emit a normal call, otherwise try to get the result in
3603    TARGET, if convenient (and in mode MODE, if that's convenient).  */
3604
3605 static rtx
3606 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3607                        enum machine_mode mode)
3608 {
3609   if (!validate_arglist (arglist,
3610                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3611     return 0;
3612   else
3613     {
3614       tree result = fold_builtin_memcmp (arglist);
3615       if (result)
3616         return expand_expr (result, target, mode, EXPAND_NORMAL);
3617     }
3618
3619 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3620   {
3621     tree arg1 = TREE_VALUE (arglist);
3622     tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3623     tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3624     rtx arg1_rtx, arg2_rtx, arg3_rtx;
3625     rtx result;
3626     rtx insn;
3627
3628     int arg1_align
3629       = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3630     int arg2_align
3631       = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3632     enum machine_mode insn_mode;
3633
3634 #ifdef HAVE_cmpmemsi
3635     if (HAVE_cmpmemsi)
3636       insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3637     else
3638 #endif
3639 #ifdef HAVE_cmpstrnsi
3640     if (HAVE_cmpstrnsi)
3641       insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3642     else
3643 #endif
3644       return 0;
3645
3646     /* If we don't have POINTER_TYPE, call the function.  */
3647     if (arg1_align == 0 || arg2_align == 0)
3648       return 0;
3649
3650     /* Make a place to write the result of the instruction.  */
3651     result = target;
3652     if (! (result != 0
3653            && REG_P (result) && GET_MODE (result) == insn_mode
3654            && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3655       result = gen_reg_rtx (insn_mode);
3656
3657     arg1_rtx = get_memory_rtx (arg1, len);
3658     arg2_rtx = get_memory_rtx (arg2, len);
3659     arg3_rtx = expand_normal (len);
3660
3661     /* Set MEM_SIZE as appropriate.  */
3662     if (GET_CODE (arg3_rtx) == CONST_INT)
3663       {
3664         set_mem_size (arg1_rtx, arg3_rtx);
3665         set_mem_size (arg2_rtx, arg3_rtx);
3666       }
3667
3668 #ifdef HAVE_cmpmemsi
3669     if (HAVE_cmpmemsi)
3670       insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3671                            GEN_INT (MIN (arg1_align, arg2_align)));
3672     else
3673 #endif
3674 #ifdef HAVE_cmpstrnsi
3675     if (HAVE_cmpstrnsi)
3676       insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3677                             GEN_INT (MIN (arg1_align, arg2_align)));
3678     else
3679 #endif
3680       gcc_unreachable ();
3681
3682     if (insn)
3683       emit_insn (insn);
3684     else
3685       emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3686                                TYPE_MODE (integer_type_node), 3,
3687                                XEXP (arg1_rtx, 0), Pmode,
3688                                XEXP (arg2_rtx, 0), Pmode,
3689                                convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3690                                                 TYPE_UNSIGNED (sizetype)),
3691                                TYPE_MODE (sizetype));
3692
3693     /* Return the value in the proper mode for this function.  */
3694     mode = TYPE_MODE (TREE_TYPE (exp));
3695     if (GET_MODE (result) == mode)
3696       return result;
3697     else if (target != 0)
3698       {
3699         convert_move (target, result, 0);
3700         return target;
3701       }
3702     else
3703       return convert_to_mode (mode, result, 0);
3704   }
3705 #endif
3706
3707   return 0;
3708 }
3709
3710 /* Expand expression EXP, which is a call to the strcmp builtin.  Return 0
3711    if we failed the caller should emit a normal call, otherwise try to get
3712    the result in TARGET, if convenient.  */
3713
3714 static rtx
3715 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3716 {
3717   tree arglist = TREE_OPERAND (exp, 1);
3718
3719   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3720     return 0;
3721   else
3722     {
3723       tree result = fold_builtin_strcmp (arglist);
3724       if (result)
3725         return expand_expr (result, target, mode, EXPAND_NORMAL);
3726     }
3727
3728 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3729   if (cmpstr_optab[SImode] != CODE_FOR_nothing
3730       || cmpstrn_optab[SImode] != CODE_FOR_nothing)
3731     {
3732       rtx arg1_rtx, arg2_rtx;
3733       rtx result, insn = NULL_RTX;
3734       tree fndecl, fn;
3735
3736       tree arg1 = TREE_VALUE (arglist);
3737       tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3738       int arg1_align
3739         = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3740       int arg2_align
3741         = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3742
3743       /* If we don't have POINTER_TYPE, call the function.  */
3744       if (arg1_align == 0 || arg2_align == 0)
3745         return 0;
3746
3747       /* Stabilize the arguments in case gen_cmpstr(n)si fail.  */
3748       arg1 = builtin_save_expr (arg1);
3749       arg2 = builtin_save_expr (arg2);
3750
3751       arg1_rtx = get_memory_rtx (arg1, NULL);
3752       arg2_rtx = get_memory_rtx (arg2, NULL);
3753
3754 #ifdef HAVE_cmpstrsi
3755       /* Try to call cmpstrsi.  */
3756       if (HAVE_cmpstrsi)
3757         {
3758           enum machine_mode insn_mode
3759             = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3760
3761           /* Make a place to write the result of the instruction.  */
3762           result = target;
3763           if (! (result != 0
3764                  && REG_P (result) && GET_MODE (result) == insn_mode
3765                  && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3766             result = gen_reg_rtx (insn_mode);
3767
3768           insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3769                                GEN_INT (MIN (arg1_align, arg2_align)));
3770         }
3771 #endif
3772 #ifdef HAVE_cmpstrnsi
3773       /* Try to determine at least one length and call cmpstrnsi.  */
3774       if (!insn && HAVE_cmpstrnsi)
3775         {
3776           tree len;
3777           rtx arg3_rtx;
3778
3779           enum machine_mode insn_mode
3780             = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3781           tree len1 = c_strlen (arg1, 1);
3782           tree len2 = c_strlen (arg2, 1);
3783
3784           if (len1)
3785             len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3786           if (len2)
3787             len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3788
3789           /* If we don't have a constant length for the first, use the length
3790              of the second, if we know it.  We don't require a constant for
3791              this case; some cost analysis could be done if both are available
3792              but neither is constant.  For now, assume they're equally cheap,
3793              unless one has side effects.  If both strings have constant lengths,
3794              use the smaller.  */
3795
3796           if (!len1)
3797             len = len2;
3798           else if (!len2)
3799             len = len1;
3800           else if (TREE_SIDE_EFFECTS (len1))
3801             len = len2;
3802           else if (TREE_SIDE_EFFECTS (len2))
3803             len = len1;
3804           else if (TREE_CODE (len1) != INTEGER_CST)
3805             len = len2;
3806           else if (TREE_CODE (len2) != INTEGER_CST)
3807             len = len1;
3808           else if (tree_int_cst_lt (len1, len2))
3809             len = len1;
3810           else
3811             len = len2;
3812
3813           /* If both arguments have side effects, we cannot optimize.  */
3814           if (!len || TREE_SIDE_EFFECTS (len))
3815             goto do_libcall;
3816
3817           arg3_rtx = expand_normal (len);
3818
3819           /* Make a place to write the result of the instruction.  */
3820           result = target;
3821           if (! (result != 0
3822                  && REG_P (result) && GET_MODE (result) == insn_mode
3823                  && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3824             result = gen_reg_rtx (insn_mode);
3825
3826           insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3827                                 GEN_INT (MIN (arg1_align, arg2_align)));
3828         }
3829 #endif
3830
3831       if (insn)
3832         {
3833           emit_insn (insn);
3834
3835           /* Return the value in the proper mode for this function.  */
3836           mode = TYPE_MODE (TREE_TYPE (exp));
3837           if (GET_MODE (result) == mode)
3838             return result;
3839           if (target == 0)
3840             return convert_to_mode (mode, result, 0);
3841           convert_move (target, result, 0);
3842           return target;
3843         }
3844
3845       /* Expand the library call ourselves using a stabilized argument
3846          list to avoid re-evaluating the function's arguments twice.  */
3847 #ifdef HAVE_cmpstrnsi
3848     do_libcall:
3849 #endif
3850       arglist = build_tree_list (NULL_TREE, arg2);
3851       arglist = tree_cons (NULL_TREE, arg1, arglist);
3852       fndecl = get_callee_fndecl (exp);
3853       fn = build_function_call_expr (fndecl, arglist);
3854       if (TREE_CODE (fn) == CALL_EXPR)
3855         CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3856       return expand_call (fn, target, target == const0_rtx);
3857     }
3858 #endif
3859   return 0;
3860 }
3861
3862 /* Expand expression EXP, which is a call to the strncmp builtin.  Return 0
3863    if we failed the caller should emit a normal call, otherwise try to get
3864    the result in TARGET, if convenient.  */
3865
3866 static rtx
3867 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3868 {
3869   tree arglist = TREE_OPERAND (exp, 1);
3870
3871   if (!validate_arglist (arglist,
3872                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3873     return 0;
3874   else
3875     {
3876       tree result = fold_builtin_strncmp (arglist);
3877       if (result)
3878         return expand_expr (result, target, mode, EXPAND_NORMAL);
3879     }
3880
3881   /* If c_strlen can determine an expression for one of the string
3882      lengths, and it doesn't have side effects, then emit cmpstrnsi
3883      using length MIN(strlen(string)+1, arg3).  */
3884 #ifdef HAVE_cmpstrnsi
3885   if (HAVE_cmpstrnsi)
3886   {
3887     tree arg1 = TREE_VALUE (arglist);
3888     tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3889     tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3890     tree len, len1, len2;
3891     rtx arg1_rtx, arg2_rtx, arg3_rtx;
3892     rtx result, insn;
3893     tree fndecl, fn;
3894
3895     int arg1_align
3896       = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3897     int arg2_align
3898       = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3899     enum machine_mode insn_mode
3900       = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3901
3902     len1 = c_strlen (arg1, 1);
3903     len2 = c_strlen (arg2, 1);
3904
3905     if (len1)
3906       len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3907     if (len2)
3908       len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3909
3910     /* If we don't have a constant length for the first, use the length
3911        of the second, if we know it.  We don't require a constant for
3912        this case; some cost analysis could be done if both are available
3913        but neither is constant.  For now, assume they're equally cheap,
3914        unless one has side effects.  If both strings have constant lengths,
3915        use the smaller.  */
3916
3917     if (!len1)
3918       len = len2;
3919     else if (!len2)
3920       len = len1;
3921     else if (TREE_SIDE_EFFECTS (len1))
3922       len = len2;
3923     else if (TREE_SIDE_EFFECTS (len2))
3924       len = len1;
3925     else if (TREE_CODE (len1) != INTEGER_CST)
3926       len = len2;
3927     else if (TREE_CODE (len2) != INTEGER_CST)
3928       len = len1;
3929     else if (tree_int_cst_lt (len1, len2))
3930       len = len1;
3931     else
3932       len = len2;
3933
3934     /* If both arguments have side effects, we cannot optimize.  */
3935     if (!len || TREE_SIDE_EFFECTS (len))
3936       return 0;
3937
3938     /* The actual new length parameter is MIN(len,arg3).  */
3939     len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
3940                        fold_convert (TREE_TYPE (len), arg3));
3941
3942     /* If we don't have POINTER_TYPE, call the function.  */
3943     if (arg1_align == 0 || arg2_align == 0)
3944       return 0;
3945
3946     /* Make a place to write the result of the instruction.  */
3947     result = target;
3948     if (! (result != 0
3949            && REG_P (result) && GET_MODE (result) == insn_mode
3950            && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3951       result = gen_reg_rtx (insn_mode);
3952
3953     /* Stabilize the arguments in case gen_cmpstrnsi fails.  */
3954     arg1 = builtin_save_expr (arg1);
3955     arg2 = builtin_save_expr (arg2);
3956     len = builtin_save_expr (len);
3957
3958     arg1_rtx = get_memory_rtx (arg1, len);
3959     arg2_rtx = get_memory_rtx (arg2, len);
3960     arg3_rtx = expand_normal (len);
3961     insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3962                           GEN_INT (MIN (arg1_align, arg2_align)));
3963     if (insn)
3964       {
3965         emit_insn (insn);
3966
3967         /* Return the value in the proper mode for this function.  */
3968         mode = TYPE_MODE (TREE_TYPE (exp));
3969         if (GET_MODE (result) == mode)
3970           return result;
3971         if (target == 0)
3972           return convert_to_mode (mode, result, 0);
3973         convert_move (target, result, 0);
3974         return target;
3975       }
3976
3977     /* Expand the library call ourselves using a stabilized argument
3978        list to avoid re-evaluating the function's arguments twice.  */
3979     arglist = build_tree_list (NULL_TREE, len);
3980     arglist = tree_cons (NULL_TREE, arg2, arglist);
3981     arglist = tree_cons (NULL_TREE, arg1, arglist);
3982     fndecl = get_callee_fndecl (exp);
3983     fn = build_function_call_expr (fndecl, arglist);
3984     if (TREE_CODE (fn) == CALL_EXPR)
3985       CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3986     return expand_call (fn, target, target == const0_rtx);
3987   }
3988 #endif
3989   return 0;
3990 }
3991
3992 /* Expand expression EXP, which is a call to the strcat builtin.
3993    Return 0 if we failed the caller should emit a normal call,
3994    otherwise try to get the result in TARGET, if convenient.  */
3995
3996 static rtx
3997 expand_builtin_strcat (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3998 {
3999   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4000     return 0;
4001   else
4002     {
4003       tree dst = TREE_VALUE (arglist),
4004       src = TREE_VALUE (TREE_CHAIN (arglist));
4005       const char *p = c_getstr (src);
4006
4007       /* If the string length is zero, return the dst parameter.  */
4008       if (p && *p == '\0')
4009         return expand_expr (dst, target, mode, EXPAND_NORMAL);
4010
4011       if (!optimize_size)
4012         {
4013           /* See if we can store by pieces into (dst + strlen(dst)).  */
4014           tree newsrc, newdst,
4015             strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4016           rtx insns;
4017
4018           /* Stabilize the argument list.  */
4019           newsrc = builtin_save_expr (src);
4020           if (newsrc != src)
4021             arglist = build_tree_list (NULL_TREE, newsrc);
4022           else
4023             arglist = TREE_CHAIN (arglist); /* Reusing arglist if safe.  */
4024
4025           dst = builtin_save_expr (dst);
4026
4027           start_sequence ();
4028
4029           /* Create strlen (dst).  */
4030           newdst =
4031             build_function_call_expr (strlen_fn,
4032                                       build_tree_list (NULL_TREE, dst));
4033           /* Create (dst + (cast) strlen (dst)).  */
4034           newdst = fold_convert (TREE_TYPE (dst), newdst);
4035           newdst = fold_build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4036
4037           newdst = builtin_save_expr (newdst);
4038           arglist = tree_cons (NULL_TREE, newdst, arglist);
4039
4040           if (!expand_builtin_strcpy (fndecl, arglist, target, mode))
4041             {
4042               end_sequence (); /* Stop sequence.  */
4043               return 0;
4044             }
4045
4046           /* Output the entire sequence.  */
4047           insns = get_insns ();
4048           end_sequence ();
4049           emit_insn (insns);
4050
4051           return expand_expr (dst, target, mode, EXPAND_NORMAL);
4052         }
4053
4054       return 0;
4055     }
4056 }
4057
4058 /* Expand expression EXP, which is a call to the strncat builtin.
4059    Return 0 if we failed the caller should emit a normal call,
4060    otherwise try to get the result in TARGET, if convenient.  */
4061
4062 static rtx
4063 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
4064 {
4065   if (validate_arglist (arglist,
4066                         POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4067     {
4068       tree result = fold_builtin_strncat (arglist);
4069       if (result)
4070         return expand_expr (result, target, mode, EXPAND_NORMAL);
4071     }
4072   return 0;
4073 }
4074
4075 /* Expand expression EXP, which is a call to the strspn builtin.
4076    Return 0 if we failed the caller should emit a normal call,
4077    otherwise try to get the result in TARGET, if convenient.  */
4078
4079 static rtx
4080 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
4081 {
4082   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4083     {
4084       tree result = fold_builtin_strspn (arglist);
4085       if (result)
4086         return expand_expr (result, target, mode, EXPAND_NORMAL);
4087     }
4088   return 0;
4089 }
4090
4091 /* Expand expression EXP, which is a call to the strcspn builtin.
4092    Return 0 if we failed the caller should emit a normal call,
4093    otherwise try to get the result in TARGET, if convenient.  */
4094
4095 static rtx
4096 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
4097 {
4098   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4099     {
4100       tree result = fold_builtin_strcspn (arglist);
4101       if (result)
4102         return expand_expr (result, target, mode, EXPAND_NORMAL);
4103     }
4104   return 0;
4105 }
4106
4107 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4108    if that's convenient.  */
4109
4110 rtx
4111 expand_builtin_saveregs (void)
4112 {
4113   rtx val, seq;
4114
4115   /* Don't do __builtin_saveregs more than once in a function.
4116      Save the result of the first call and reuse it.  */
4117   if (saveregs_value != 0)
4118     return saveregs_value;
4119
4120   /* When this function is called, it means that registers must be
4121      saved on entry to this function.  So we migrate the call to the
4122      first insn of this function.  */
4123
4124   start_sequence ();
4125
4126   /* Do whatever the machine needs done in this case.  */
4127   val = targetm.calls.expand_builtin_saveregs ();
4128
4129   seq = get_insns ();
4130   end_sequence ();
4131
4132   saveregs_value = val;
4133
4134   /* Put the insns after the NOTE that starts the function.  If this
4135      is inside a start_sequence, make the outer-level insn chain current, so
4136      the code is placed at the start of the function.  */
4137   push_topmost_sequence ();
4138   emit_insn_after (seq, entry_of_function ());
4139   pop_topmost_sequence ();
4140
4141   return val;
4142 }
4143
4144 /* __builtin_args_info (N) returns word N of the arg space info
4145    for the current function.  The number and meanings of words
4146    is controlled by the definition of CUMULATIVE_ARGS.  */
4147
4148 static rtx
4149 expand_builtin_args_info (tree arglist)
4150 {
4151   int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4152   int *word_ptr = (int *) &current_function_args_info;
4153
4154   gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4155
4156   if (arglist != 0)
4157     {
4158       if (!host_integerp (TREE_VALUE (arglist), 0))
4159         error ("argument of %<__builtin_args_info%> must be constant");
4160       else
4161         {
4162           HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
4163
4164           if (wordnum < 0 || wordnum >= nwords)
4165             error ("argument of %<__builtin_args_info%> out of range");
4166           else
4167             return GEN_INT (word_ptr[wordnum]);
4168         }
4169     }
4170   else
4171     error ("missing argument in %<__builtin_args_info%>");
4172
4173   return const0_rtx;
4174 }
4175
4176 /* Expand a call to __builtin_next_arg.  */
4177
4178 static rtx
4179 expand_builtin_next_arg (void)
4180 {
4181   /* Checking arguments is already done in fold_builtin_next_arg
4182      that must be called before this function.  */
4183   return expand_binop (Pmode, add_optab,
4184                        current_function_internal_arg_pointer,
4185                        current_function_arg_offset_rtx,
4186                        NULL_RTX, 0, OPTAB_LIB_WIDEN);
4187 }
4188
4189 /* Make it easier for the backends by protecting the valist argument
4190    from multiple evaluations.  */
4191
4192 static tree
4193 stabilize_va_list (tree valist, int needs_lvalue)
4194 {
4195   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4196     {
4197       if (TREE_SIDE_EFFECTS (valist))
4198         valist = save_expr (valist);
4199
4200       /* For this case, the backends will be expecting a pointer to
4201          TREE_TYPE (va_list_type_node), but it's possible we've
4202          actually been given an array (an actual va_list_type_node).
4203          So fix it.  */
4204       if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4205         {
4206           tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4207           valist = build_fold_addr_expr_with_type (valist, p1);
4208         }
4209     }
4210   else
4211     {
4212       tree pt;
4213
4214       if (! needs_lvalue)
4215         {
4216           if (! TREE_SIDE_EFFECTS (valist))
4217             return valist;
4218
4219           pt = build_pointer_type (va_list_type_node);
4220           valist = fold_build1 (ADDR_EXPR, pt, valist);
4221           TREE_SIDE_EFFECTS (valist) = 1;
4222         }
4223
4224       if (TREE_SIDE_EFFECTS (valist))
4225         valist = save_expr (valist);
4226       valist = build_fold_indirect_ref (valist);
4227     }
4228
4229   return valist;
4230 }
4231
4232 /* The "standard" definition of va_list is void*.  */
4233
4234 tree
4235 std_build_builtin_va_list (void)
4236 {
4237   return ptr_type_node;
4238 }
4239
4240 /* The "standard" implementation of va_start: just assign `nextarg' to
4241    the variable.  */
4242
4243 void
4244 std_expand_builtin_va_start (tree valist, rtx nextarg)
4245 {
4246   tree t;
4247
4248   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4249               make_tree (ptr_type_node, nextarg));
4250   TREE_SIDE_EFFECTS (t) = 1;
4251
4252   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4253 }
4254
4255 /* Expand ARGLIST, from a call to __builtin_va_start.  */
4256
4257 static rtx
4258 expand_builtin_va_start (tree arglist)
4259 {
4260   rtx nextarg;
4261   tree chain, valist;
4262
4263   chain = TREE_CHAIN (arglist);
4264
4265   if (!chain)
4266     {
4267       error ("too few arguments to function %<va_start%>");
4268       return const0_rtx;
4269     }
4270
4271   if (fold_builtin_next_arg (chain))
4272     return const0_rtx;
4273
4274   nextarg = expand_builtin_next_arg ();
4275   valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4276
4277 #ifdef EXPAND_BUILTIN_VA_START
4278   EXPAND_BUILTIN_VA_START (valist, nextarg);
4279 #else
4280   std_expand_builtin_va_start (valist, nextarg);
4281 #endif
4282
4283   return const0_rtx;
4284 }
4285
4286 /* The "standard" implementation of va_arg: read the value from the
4287    current (padded) address and increment by the (padded) size.  */
4288
4289 tree
4290 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4291 {
4292   tree addr, t, type_size, rounded_size, valist_tmp;
4293   unsigned HOST_WIDE_INT align, boundary;
4294   bool indirect;
4295
4296 #ifdef ARGS_GROW_DOWNWARD
4297   /* All of the alignment and movement below is for args-grow-up machines.
4298      As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4299      implement their own specialized gimplify_va_arg_expr routines.  */
4300   gcc_unreachable ();
4301 #endif
4302
4303   indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4304   if (indirect)
4305     type = build_pointer_type (type);
4306
4307   align = PARM_BOUNDARY / BITS_PER_UNIT;
4308   boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4309
4310   /* Hoist the valist value into a temporary for the moment.  */
4311   valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4312
4313   /* va_list pointer is aligned to PARM_BOUNDARY.  If argument actually
4314      requires greater alignment, we must perform dynamic alignment.  */
4315   if (boundary > align
4316       && !integer_zerop (TYPE_SIZE (type)))
4317     {
4318       t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4319       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4320                   build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4321       gimplify_and_add (t, pre_p);
4322
4323       t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4324       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4325                   build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4326       gimplify_and_add (t, pre_p);
4327     }
4328   else
4329     boundary = align;
4330
4331   /* If the actual alignment is less than the alignment of the type,
4332      adjust the type accordingly so that we don't assume strict alignment
4333      when deferencing the pointer.  */
4334   boundary *= BITS_PER_UNIT;
4335   if (boundary < TYPE_ALIGN (type))
4336     {
4337       type = build_variant_type_copy (type);
4338       TYPE_ALIGN (type) = boundary;
4339     }
4340
4341   /* Compute the rounded size of the type.  */
4342   type_size = size_in_bytes (type);
4343   rounded_size = round_up (type_size, align);
4344
4345   /* Reduce rounded_size so it's sharable with the postqueue.  */
4346   gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4347
4348   /* Get AP.  */
4349   addr = valist_tmp;
4350   if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4351     {
4352       /* Small args are padded downward.  */
4353       t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4354       t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4355                        size_binop (MINUS_EXPR, rounded_size, type_size));
4356       t = fold_convert (TREE_TYPE (addr), t);
4357       addr = fold_build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t);
4358     }
4359
4360   /* Compute new value for AP.  */
4361   t = fold_convert (TREE_TYPE (valist), rounded_size);
4362   t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4363   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4364   gimplify_and_add (t, pre_p);
4365
4366   addr = fold_convert (build_pointer_type (type), addr);
4367
4368   if (indirect)
4369     addr = build_va_arg_indirect_ref (addr);
4370
4371   return build_va_arg_indirect_ref (addr);
4372 }
4373
4374 /* Build an indirect-ref expression over the given TREE, which represents a
4375    piece of a va_arg() expansion.  */
4376 tree
4377 build_va_arg_indirect_ref (tree addr)
4378 {
4379   addr = build_fold_indirect_ref (addr);
4380
4381   if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF.  */
4382     mf_mark (addr);
4383
4384   return addr;
4385 }
4386
4387 /* Return a dummy expression of type TYPE in order to keep going after an
4388    error.  */
4389
4390 static tree
4391 dummy_object (tree type)
4392 {
4393   tree t = build_int_cst (build_pointer_type (type), 0);
4394   return build1 (INDIRECT_REF, type, t);
4395 }
4396
4397 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4398    builtin function, but a very special sort of operator.  */
4399
4400 enum gimplify_status
4401 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4402 {
4403   tree promoted_type, want_va_type, have_va_type;
4404   tree valist = TREE_OPERAND (*expr_p, 0);
4405   tree type = TREE_TYPE (*expr_p);
4406   tree t;
4407
4408   /* Verify that valist is of the proper type.  */
4409   want_va_type = va_list_type_node;
4410   have_va_type = TREE_TYPE (valist);
4411
4412   if (have_va_type == error_mark_node)
4413     return GS_ERROR;
4414
4415   if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4416     {
4417       /* If va_list is an array type, the argument may have decayed
4418          to a pointer type, e.g. by being passed to another function.
4419          In that case, unwrap both types so that we can compare the
4420          underlying records.  */
4421       if (TREE_CODE (have_va_type) == ARRAY_TYPE
4422           || POINTER_TYPE_P (have_va_type))
4423         {
4424           want_va_type = TREE_TYPE (want_va_type);
4425           have_va_type = TREE_TYPE (have_va_type);
4426         }
4427     }
4428
4429   if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4430     {
4431       error ("first argument to %<va_arg%> not of type %<va_list%>");
4432       return GS_ERROR;
4433     }
4434
4435   /* Generate a diagnostic for requesting data of a type that cannot
4436      be passed through `...' due to type promotion at the call site.  */
4437   else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4438            != type)
4439     {
4440       static bool gave_help;
4441
4442       /* Unfortunately, this is merely undefined, rather than a constraint
4443          violation, so we cannot make this an error.  If this call is never
4444          executed, the program is still strictly conforming.  */
4445       warning (0, "%qT is promoted to %qT when passed through %<...%>",
4446                type, promoted_type);
4447       if (! gave_help)
4448         {
4449           gave_help = true;
4450           warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4451                    promoted_type, type);
4452         }
4453
4454       /* We can, however, treat "undefined" any way we please.
4455          Call abort to encourage the user to fix the program.  */
4456       inform ("if this code is reached, the program will abort");
4457       t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4458                                     NULL);
4459       append_to_statement_list (t, pre_p);
4460
4461       /* This is dead code, but go ahead and finish so that the
4462          mode of the result comes out right.  */
4463       *expr_p = dummy_object (type);
4464       return GS_ALL_DONE;
4465     }
4466   else
4467     {
4468       /* Make it easier for the backends by protecting the valist argument
4469          from multiple evaluations.  */
4470       if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4471         {
4472           /* For this case, the backends will be expecting a pointer to
4473              TREE_TYPE (va_list_type_node), but it's possible we've
4474              actually been given an array (an actual va_list_type_node).
4475              So fix it.  */
4476           if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4477             {
4478               tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4479               valist = build_fold_addr_expr_with_type (valist, p1);
4480             }
4481           gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4482         }
4483       else
4484         gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4485
4486       if (!targetm.gimplify_va_arg_expr)
4487         /* FIXME:Once most targets are converted we should merely
4488            assert this is non-null.  */
4489         return GS_ALL_DONE;
4490
4491       *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4492       return GS_OK;
4493     }
4494 }
4495
4496 /* Expand ARGLIST, from a call to __builtin_va_end.  */
4497
4498 static rtx
4499 expand_builtin_va_end (tree arglist)
4500 {
4501   tree valist = TREE_VALUE (arglist);
4502
4503   /* Evaluate for side effects, if needed.  I hate macros that don't
4504      do that.  */
4505   if (TREE_SIDE_EFFECTS (valist))
4506     expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4507
4508   return const0_rtx;
4509 }
4510
4511 /* Expand ARGLIST, from a call to __builtin_va_copy.  We do this as a
4512    builtin rather than just as an assignment in stdarg.h because of the
4513    nastiness of array-type va_list types.  */
4514
4515 static rtx
4516 expand_builtin_va_copy (tree arglist)
4517 {
4518   tree dst, src, t;
4519
4520   dst = TREE_VALUE (arglist);
4521   src = TREE_VALUE (TREE_CHAIN (arglist));
4522
4523   dst = stabilize_va_list (dst, 1);
4524   src = stabilize_va_list (src, 0);
4525
4526   if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4527     {
4528       t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4529       TREE_SIDE_EFFECTS (t) = 1;
4530       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4531     }
4532   else
4533     {
4534       rtx dstb, srcb, size;
4535
4536       /* Evaluate to pointers.  */
4537       dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4538       srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4539       size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4540                           VOIDmode, EXPAND_NORMAL);
4541
4542       dstb = convert_memory_address (Pmode, dstb);
4543       srcb = convert_memory_address (Pmode, srcb);
4544
4545       /* "Dereference" to BLKmode memories.  */
4546       dstb = gen_rtx_MEM (BLKmode, dstb);
4547       set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4548       set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4549       srcb = gen_rtx_MEM (BLKmode, srcb);
4550       set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4551       set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4552
4553       /* Copy.  */
4554       emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4555     }
4556
4557   return const0_rtx;
4558 }
4559
4560 /* Expand a call to one of the builtin functions __builtin_frame_address or
4561    __builtin_return_address.  */
4562
4563 static rtx
4564 expand_builtin_frame_address (tree fndecl, tree arglist)
4565 {
4566   /* The argument must be a nonnegative integer constant.
4567      It counts the number of frames to scan up the stack.
4568      The value is the return address saved in that frame.  */
4569   if (arglist == 0)
4570     /* Warning about missing arg was already issued.  */
4571     return const0_rtx;
4572   else if (! host_integerp (TREE_VALUE (arglist), 1))
4573     {
4574       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4575         error ("invalid argument to %<__builtin_frame_address%>");
4576       else
4577         error ("invalid argument to %<__builtin_return_address%>");
4578       return const0_rtx;
4579     }
4580   else
4581     {
4582       rtx tem
4583         = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4584                                       tree_low_cst (TREE_VALUE (arglist), 1));
4585
4586       /* Some ports cannot access arbitrary stack frames.  */
4587       if (tem == NULL)
4588         {
4589           if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4590             warning (0, "unsupported argument to %<__builtin_frame_address%>");
4591           else
4592             warning (0, "unsupported argument to %<__builtin_return_address%>");
4593           return const0_rtx;
4594         }
4595
4596       /* For __builtin_frame_address, return what we've got.  */
4597       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4598         return tem;
4599
4600       if (!REG_P (tem)
4601           && ! CONSTANT_P (tem))
4602         tem = copy_to_mode_reg (Pmode, tem);
4603       return tem;
4604     }
4605 }
4606
4607 /* Expand a call to the alloca builtin, with arguments ARGLIST.  Return 0 if
4608    we failed and the caller should emit a normal call, otherwise try to get
4609    the result in TARGET, if convenient.  */
4610
4611 static rtx
4612 expand_builtin_alloca (tree arglist, rtx target)
4613 {
4614   rtx op0;
4615   rtx result;
4616
4617   /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4618      should always expand to function calls.  These can be intercepted
4619      in libmudflap.  */
4620   if (flag_mudflap)
4621     return 0;
4622
4623   if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4624     return 0;
4625
4626   /* Compute the argument.  */
4627   op0 = expand_normal (TREE_VALUE (arglist));
4628
4629   /* Allocate the desired space.  */
4630   result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4631   result = convert_memory_address (ptr_mode, result);
4632
4633   return result;
4634 }
4635
4636 /* Expand a call to a unary builtin.  The arguments are in ARGLIST.
4637    Return 0 if a normal call should be emitted rather than expanding the
4638    function in-line.  If convenient, the result should be placed in TARGET.
4639    SUBTARGET may be used as the target for computing one of EXP's operands.  */
4640
4641 static rtx
4642 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4643                      rtx subtarget, optab op_optab)
4644 {
4645   rtx op0;
4646   if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4647     return 0;
4648
4649   /* Compute the argument.  */
4650   op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4651   /* Compute op, into TARGET if possible.
4652      Set TARGET to wherever the result comes back.  */
4653   target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4654                         op_optab, op0, target, 1);
4655   gcc_assert (target);
4656
4657   return convert_to_mode (target_mode, target, 0);
4658 }
4659
4660 /* If the string passed to fputs is a constant and is one character
4661    long, we attempt to transform this call into __builtin_fputc().  */
4662
4663 static rtx
4664 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4665 {
4666   /* Verify the arguments in the original call.  */
4667   if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4668     {
4669       tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4670                                         unlocked, NULL_TREE);
4671       if (result)
4672         return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4673     }
4674   return 0;
4675 }
4676
4677 /* Expand a call to __builtin_expect.  We return our argument and emit a
4678    NOTE_INSN_EXPECTED_VALUE note.  This is the expansion of __builtin_expect in
4679    a non-jump context.  */
4680
4681 static rtx
4682 expand_builtin_expect (tree arglist, rtx target)
4683 {
4684   tree exp, c;
4685   rtx note, rtx_c;
4686
4687   if (arglist == NULL_TREE
4688       || TREE_CHAIN (arglist) == NULL_TREE)
4689     return const0_rtx;
4690   exp = TREE_VALUE (arglist);
4691   c = TREE_VALUE (TREE_CHAIN (arglist));
4692
4693   if (TREE_CODE (c) != INTEGER_CST)
4694     {
4695       error ("second argument to %<__builtin_expect%> must be a constant");
4696       c = integer_zero_node;
4697     }
4698
4699   target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4700
4701   /* Don't bother with expected value notes for integral constants.  */
4702   if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4703     {
4704       /* We do need to force this into a register so that we can be
4705          moderately sure to be able to correctly interpret the branch
4706          condition later.  */
4707       target = force_reg (GET_MODE (target), target);
4708
4709       rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4710
4711       note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4712       NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4713     }
4714
4715   return target;
4716 }
4717
4718 /* Like expand_builtin_expect, except do this in a jump context.  This is
4719    called from do_jump if the conditional is a __builtin_expect.  Return either
4720    a list of insns to emit the jump or NULL if we cannot optimize
4721    __builtin_expect.  We need to optimize this at jump time so that machines
4722    like the PowerPC don't turn the test into a SCC operation, and then jump
4723    based on the test being 0/1.  */
4724
4725 rtx
4726 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4727 {
4728   tree arglist = TREE_OPERAND (exp, 1);
4729   tree arg0 = TREE_VALUE (arglist);
4730   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4731   rtx ret = NULL_RTX;
4732
4733   /* Only handle __builtin_expect (test, 0) and
4734      __builtin_expect (test, 1).  */
4735   if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4736       && (integer_zerop (arg1) || integer_onep (arg1)))
4737     {
4738       rtx insn, drop_through_label, temp;
4739
4740       /* Expand the jump insns.  */
4741       start_sequence ();
4742       do_jump (arg0, if_false_label, if_true_label);
4743       ret = get_insns ();
4744
4745       drop_through_label = get_last_insn ();
4746       if (drop_through_label && NOTE_P (drop_through_label))
4747         drop_through_label = prev_nonnote_insn (drop_through_label);
4748       if (drop_through_label && !LABEL_P (drop_through_label))
4749         drop_through_label = NULL_RTX;
4750       end_sequence ();
4751
4752       if (! if_true_label)
4753         if_true_label = drop_through_label;
4754       if (! if_false_label)
4755         if_false_label = drop_through_label;
4756
4757       /* Go through and add the expect's to each of the conditional jumps.  */
4758       insn = ret;
4759       while (insn != NULL_RTX)
4760         {
4761           rtx next = NEXT_INSN (insn);
4762
4763           if (JUMP_P (insn) && any_condjump_p (insn))
4764             {
4765               rtx ifelse = SET_SRC (pc_set (insn));
4766               rtx then_dest = XEXP (ifelse, 1);
4767               rtx else_dest = XEXP (ifelse, 2);
4768               int taken = -1;
4769
4770               /* First check if we recognize any of the labels.  */
4771               if (GET_CODE (then_dest) == LABEL_REF
4772                   && XEXP (then_dest, 0) == if_true_label)
4773                 taken = 1;
4774               else if (GET_CODE (then_dest) == LABEL_REF
4775                        && XEXP (then_dest, 0) == if_false_label)
4776                 taken = 0;
4777               else if (GET_CODE (else_dest) == LABEL_REF
4778                        && XEXP (else_dest, 0) == if_false_label)
4779                 taken = 1;
4780               else if (GET_CODE (else_dest) == LABEL_REF
4781                        && XEXP (else_dest, 0) == if_true_label)
4782                 taken = 0;
4783               /* Otherwise check where we drop through.  */
4784               else if (else_dest == pc_rtx)
4785                 {
4786                   if (next && NOTE_P (next))
4787                     next = next_nonnote_insn (next);
4788
4789                   if (next && JUMP_P (next)
4790                       && any_uncondjump_p (next))
4791                     temp = XEXP (SET_SRC (pc_set (next)), 0);
4792                   else
4793                     temp = next;
4794
4795                   /* TEMP is either a CODE_LABEL, NULL_RTX or something
4796                      else that can't possibly match either target label.  */
4797                   if (temp == if_false_label)
4798                     taken = 1;
4799                   else if (temp == if_true_label)
4800                     taken = 0;
4801                 }
4802               else if (then_dest == pc_rtx)
4803                 {
4804                   if (next && NOTE_P (next))
4805                     next = next_nonnote_insn (next);
4806
4807                   if (next && JUMP_P (next)
4808                       && any_uncondjump_p (next))
4809                     temp = XEXP (SET_SRC (pc_set (next)), 0);
4810                   else
4811                     temp = next;
4812
4813                   if (temp == if_false_label)
4814                     taken = 0;
4815                   else if (temp == if_true_label)
4816                     taken = 1;
4817                 }
4818
4819               if (taken != -1)
4820                 {
4821                   /* If the test is expected to fail, reverse the
4822                      probabilities.  */
4823                   if (integer_zerop (arg1))
4824                     taken = 1 - taken;
4825                   predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4826                 }
4827             }
4828
4829           insn = next;
4830         }
4831     }
4832
4833   return ret;
4834 }
4835
4836 void
4837 expand_builtin_trap (void)
4838 {
4839 #ifdef HAVE_trap
4840   if (HAVE_trap)
4841     emit_insn (gen_trap ());
4842   else
4843 #endif
4844     emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4845   emit_barrier ();
4846 }
4847
4848 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4849    Return 0 if a normal call should be emitted rather than expanding
4850    the function inline.  If convenient, the result should be placed
4851    in TARGET.  SUBTARGET may be used as the target for computing
4852    the operand.  */
4853
4854 static rtx
4855 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4856 {
4857   enum machine_mode mode;
4858   tree arg;
4859   rtx op0;
4860
4861   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4862     return 0;
4863
4864   arg = TREE_VALUE (arglist);
4865   mode = TYPE_MODE (TREE_TYPE (arg));
4866   op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4867   return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4868 }
4869
4870 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4871    Return NULL is a normal call should be emitted rather than expanding the
4872    function inline.  If convenient, the result should be placed in TARGET.
4873    SUBTARGET may be used as the target for computing the operand.  */
4874
4875 static rtx
4876 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4877 {
4878   rtx op0, op1;
4879   tree arg;
4880
4881   if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4882     return 0;
4883
4884   arg = TREE_VALUE (arglist);
4885   op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4886
4887   arg = TREE_VALUE (TREE_CHAIN (arglist));
4888   op1 = expand_normal (arg);
4889
4890   return expand_copysign (op0, op1, target);
4891 }
4892
4893 /* Create a new constant string literal and return a char* pointer to it.
4894    The STRING_CST value is the LEN characters at STR.  */
4895 tree
4896 build_string_literal (int len, const char *str)
4897 {
4898   tree t, elem, index, type;
4899
4900   t = build_string (len, str);
4901   elem = build_type_variant (char_type_node, 1, 0);
4902   index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4903   type = build_array_type (elem, index);
4904   TREE_TYPE (t) = type;
4905   TREE_CONSTANT (t) = 1;
4906   TREE_INVARIANT (t) = 1;
4907   TREE_READONLY (t) = 1;
4908   TREE_STATIC (t) = 1;
4909
4910   type = build_pointer_type (type);
4911   t = build1 (ADDR_EXPR, type, t);
4912
4913   type = build_pointer_type (elem);
4914   t = build1 (NOP_EXPR, type, t);
4915   return t;
4916 }
4917
4918 /* Expand EXP, a call to printf or printf_unlocked.
4919    Return 0 if a normal call should be emitted rather than transforming
4920    the function inline.  If convenient, the result should be placed in
4921    TARGET with mode MODE.  UNLOCKED indicates this is a printf_unlocked
4922    call.  */
4923 static rtx
4924 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
4925                        bool unlocked)
4926 {
4927   tree arglist = TREE_OPERAND (exp, 1);
4928   /* If we're using an unlocked function, assume the other unlocked
4929      functions exist explicitly.  */
4930   tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4931     : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4932   tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4933     : implicit_built_in_decls[BUILT_IN_PUTS];
4934   const char *fmt_str;
4935   tree fn, fmt, arg;
4936
4937   /* If the return value is used, don't do the transformation.  */
4938   if (target != const0_rtx)
4939     return 0;
4940
4941   /* Verify the required arguments in the original call.  */
4942   if (! arglist)
4943     return 0;
4944   fmt = TREE_VALUE (arglist);
4945   if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4946     return 0;
4947   arglist = TREE_CHAIN (arglist);
4948
4949   /* Check whether the format is a literal string constant.  */
4950   fmt_str = c_getstr (fmt);
4951   if (fmt_str == NULL)
4952     return 0;
4953
4954   if (!init_target_chars())
4955     return 0;
4956
4957   /* If the format specifier was "%s\n", call __builtin_puts(arg).  */
4958   if (strcmp (fmt_str, target_percent_s_newline) == 0)
4959     {
4960       if (! arglist
4961           || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4962           || TREE_CHAIN (arglist))
4963         return 0;
4964       fn = fn_puts;
4965     }
4966   /* If the format specifier was "%c", call __builtin_putchar(arg).  */
4967   else if (strcmp (fmt_str, target_percent_c) == 0)
4968     {
4969       if (! arglist
4970           || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4971           || TREE_CHAIN (arglist))
4972         return 0;
4973       fn = fn_putchar;
4974     }
4975   else
4976     {
4977       /* We can't handle anything else with % args or %% ... yet.  */
4978       if (strchr (fmt_str, target_percent))
4979         return 0;
4980
4981       if (arglist)
4982         return 0;
4983
4984       /* If the format specifier was "", printf does nothing.  */
4985       if (fmt_str[0] == '\0')
4986         return const0_rtx;
4987       /* If the format specifier has length of 1, call putchar.  */
4988       if (fmt_str[1] == '\0')
4989         {
4990           /* Given printf("c"), (where c is any one character,)
4991              convert "c"[0] to an int and pass that to the replacement
4992              function.  */
4993           arg = build_int_cst (NULL_TREE, fmt_str[0]);
4994           arglist = build_tree_list (NULL_TREE, arg);
4995           fn = fn_putchar;
4996         }
4997       else
4998         {
4999           /* If the format specifier was "string\n", call puts("string").  */
5000           size_t len = strlen (fmt_str);
5001           if ((unsigned char)fmt_str[len - 1] == target_newline)
5002             {
5003               /* Create a NUL-terminated string that's one char shorter
5004                  than the original, stripping off the trailing '\n'.  */
5005               char *newstr = alloca (len);
5006               memcpy (newstr, fmt_str, len - 1);
5007               newstr[len - 1] = 0;
5008
5009               arg = build_string_literal (len, newstr);
5010               arglist = build_tree_list (NULL_TREE, arg);
5011               fn = fn_puts;
5012             }
5013           else
5014             /* We'd like to arrange to call fputs(string,stdout) here,
5015                but we need stdout and don't have a way to get it yet.  */
5016             return 0;
5017         }
5018     }
5019
5020   if (!fn)
5021     return 0;
5022   fn = build_function_call_expr (fn, arglist);
5023   if (TREE_CODE (fn) == CALL_EXPR)
5024     CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5025   return expand_expr (fn, target, mode, EXPAND_NORMAL);
5026 }
5027
5028 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5029    Return 0 if a normal call should be emitted rather than transforming
5030    the function inline.  If convenient, the result should be placed in
5031    TARGET with mode MODE.  UNLOCKED indicates this is a fprintf_unlocked
5032    call.  */
5033 static rtx
5034 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5035                         bool unlocked)
5036 {
5037   tree arglist = TREE_OPERAND (exp, 1);
5038   /* If we're using an unlocked function, assume the other unlocked
5039      functions exist explicitly.  */
5040   tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5041     : implicit_built_in_decls[BUILT_IN_FPUTC];
5042   tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5043     : implicit_built_in_decls[BUILT_IN_FPUTS];
5044   const char *fmt_str;
5045   tree fn, fmt, fp, arg;
5046
5047   /* If the return value is used, don't do the transformation.  */
5048   if (target != const0_rtx)
5049     return 0;
5050
5051   /* Verify the required arguments in the original call.  */
5052   if (! arglist)
5053     return 0;
5054   fp = TREE_VALUE (arglist);
5055   if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5056     return 0;
5057   arglist = TREE_CHAIN (arglist);
5058   if (! arglist)
5059     return 0;
5060   fmt = TREE_VALUE (arglist);
5061   if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5062     return 0;
5063   arglist = TREE_CHAIN (arglist);
5064
5065   /* Check whether the format is a literal string constant.  */
5066   fmt_str = c_getstr (fmt);
5067   if (fmt_str == NULL)
5068     return 0;
5069
5070   if (!init_target_chars())
5071     return 0;
5072
5073   /* If the format specifier was "%s", call __builtin_fputs(arg,fp).  */
5074   if (strcmp (fmt_str, target_percent_s) == 0)
5075     {
5076       if (! arglist
5077           || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5078           || TREE_CHAIN (arglist))
5079         return 0;
5080       arg = TREE_VALUE (arglist);
5081       arglist = build_tree_list (NULL_TREE, fp);
5082       arglist = tree_cons (NULL_TREE, arg, arglist);
5083       fn = fn_fputs;
5084     }
5085   /* If the format specifier was "%c", call __builtin_fputc(arg,fp).  */
5086   else if (strcmp (fmt_str, target_percent_c) == 0)
5087     {
5088       if (! arglist
5089           || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5090           || TREE_CHAIN (arglist))
5091         return 0;
5092       arg = TREE_VALUE (arglist);
5093       arglist = build_tree_list (NULL_TREE, fp);
5094       arglist = tree_cons (NULL_TREE, arg, arglist);
5095       fn = fn_fputc;
5096     }
5097   else
5098     {
5099       /* We can't handle anything else with % args or %% ... yet.  */
5100       if (strchr (fmt_str, target_percent))
5101         return 0;
5102
5103       if (arglist)
5104         return 0;
5105
5106       /* If the format specifier was "", fprintf does nothing.  */
5107       if (fmt_str[0] == '\0')
5108         {
5109           /* Evaluate and ignore FILE* argument for side-effects.  */
5110           expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5111           return const0_rtx;
5112         }
5113
5114       /* When "string" doesn't contain %, replace all cases of
5115          fprintf(stream,string) with fputs(string,stream).  The fputs
5116          builtin will take care of special cases like length == 1.  */
5117       arglist = build_tree_list (NULL_TREE, fp);
5118       arglist = tree_cons (NULL_TREE, fmt, arglist);
5119       fn = fn_fputs;
5120     }
5121
5122   if (!fn)
5123     return 0;
5124   fn = build_function_call_expr (fn, arglist);
5125   if (TREE_CODE (fn) == CALL_EXPR)
5126     CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5127   return expand_expr (fn, target, mode, EXPAND_NORMAL);
5128 }
5129
5130 /* Expand a call to sprintf with argument list ARGLIST.  Return 0 if
5131    a normal call should be emitted rather than expanding the function
5132    inline.  If convenient, the result should be placed in TARGET with
5133    mode MODE.  */
5134
5135 static rtx
5136 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5137 {
5138   tree orig_arglist, dest, fmt;
5139   const char *fmt_str;
5140
5141   orig_arglist = arglist;
5142
5143   /* Verify the required arguments in the original call.  */
5144   if (! arglist)
5145     return 0;
5146   dest = TREE_VALUE (arglist);
5147   if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5148     return 0;
5149   arglist = TREE_CHAIN (arglist);
5150   if (! arglist)
5151     return 0;
5152   fmt = TREE_VALUE (arglist);
5153   if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5154     return 0;
5155   arglist = TREE_CHAIN (arglist);
5156
5157   /* Check whether the format is a literal string constant.  */
5158   fmt_str = c_getstr (fmt);
5159   if (fmt_str == NULL)
5160     return 0;
5161
5162   if (!init_target_chars())
5163     return 0;
5164
5165   /* If the format doesn't contain % args or %%, use strcpy.  */
5166   if (strchr (fmt_str, target_percent) == 0)
5167     {
5168       tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5169       tree exp;
5170
5171       if (arglist || ! fn)
5172         return 0;
5173       expand_expr (build_function_call_expr (fn, orig_arglist),
5174                    const0_rtx, VOIDmode, EXPAND_NORMAL);
5175       if (target == const0_rtx)
5176         return const0_rtx;
5177       exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5178       return expand_expr (exp, target, mode, EXPAND_NORMAL);
5179     }
5180   /* If the format is "%s", use strcpy if the result isn't used.  */
5181   else if (strcmp (fmt_str, target_percent_s) == 0)
5182     {
5183       tree fn, arg, len;
5184       fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5185
5186       if (! fn)
5187         return 0;
5188
5189       if (! arglist || TREE_CHAIN (arglist))
5190         return 0;
5191       arg = TREE_VALUE (arglist);
5192       if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5193         return 0;
5194
5195       if (target != const0_rtx)
5196         {
5197           len = c_strlen (arg, 1);
5198           if (! len || TREE_CODE (len) != INTEGER_CST)
5199             return 0;
5200         }
5201       else
5202         len = NULL_TREE;
5203
5204       arglist = build_tree_list (NULL_TREE, arg);
5205       arglist = tree_cons (NULL_TREE, dest, arglist);
5206       expand_expr (build_function_call_expr (fn, arglist),
5207                    const0_rtx, VOIDmode, EXPAND_NORMAL);
5208
5209       if (target == const0_rtx)
5210         return const0_rtx;
5211       return expand_expr (len, target, mode, EXPAND_NORMAL);
5212     }
5213
5214   return 0;
5215 }
5216
5217 /* Expand a call to either the entry or exit function profiler.  */
5218
5219 static rtx
5220 expand_builtin_profile_func (bool exitp)
5221 {
5222   rtx this, which;
5223
5224   this = DECL_RTL (current_function_decl);
5225   gcc_assert (MEM_P (this));
5226   this = XEXP (this, 0);
5227
5228   if (exitp)
5229     which = profile_function_exit_libfunc;
5230   else
5231     which = profile_function_entry_libfunc;
5232
5233   emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5234                      expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5235                                                  0),
5236                      Pmode);
5237
5238   return const0_rtx;
5239 }
5240
5241 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT.  */
5242
5243 static rtx
5244 round_trampoline_addr (rtx tramp)
5245 {
5246   rtx temp, addend, mask;
5247
5248   /* If we don't need too much alignment, we'll have been guaranteed
5249      proper alignment by get_trampoline_type.  */
5250   if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5251     return tramp;
5252
5253   /* Round address up to desired boundary.  */
5254   temp = gen_reg_rtx (Pmode);
5255   addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5256   mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5257
5258   temp  = expand_simple_binop (Pmode, PLUS, tramp, addend,
5259                                temp, 0, OPTAB_LIB_WIDEN);
5260   tramp = expand_simple_binop (Pmode, AND, temp, mask,
5261                                temp, 0, OPTAB_LIB_WIDEN);
5262
5263   return tramp;
5264 }
5265
5266 static rtx
5267 expand_builtin_init_trampoline (tree arglist)
5268 {
5269   tree t_tramp, t_func, t_chain;
5270   rtx r_tramp, r_func, r_chain;
5271 #ifdef TRAMPOLINE_TEMPLATE
5272   rtx blktramp;
5273 #endif
5274
5275   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5276                          POINTER_TYPE, VOID_TYPE))
5277     return NULL_RTX;
5278
5279   t_tramp = TREE_VALUE (arglist);
5280   arglist = TREE_CHAIN (arglist);
5281   t_func = TREE_VALUE (arglist);
5282   arglist = TREE_CHAIN (arglist);
5283   t_chain = TREE_VALUE (arglist);
5284
5285   r_tramp = expand_normal (t_tramp);
5286   r_func = expand_normal (t_func);
5287   r_chain = expand_normal (t_chain);
5288
5289   /* Generate insns to initialize the trampoline.  */
5290   r_tramp = round_trampoline_addr (r_tramp);
5291 #ifdef TRAMPOLINE_TEMPLATE
5292   blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5293   set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5294   emit_block_move (blktramp, assemble_trampoline_template (),
5295                    GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5296 #endif
5297   trampolines_created = 1;
5298   INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5299
5300   return const0_rtx;
5301 }
5302
5303 static rtx
5304 expand_builtin_adjust_trampoline (tree arglist)
5305 {
5306   rtx tramp;
5307
5308   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5309     return NULL_RTX;
5310
5311   tramp = expand_normal (TREE_VALUE (arglist));
5312   tramp = round_trampoline_addr (tramp);
5313 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5314   TRAMPOLINE_ADJUST_ADDRESS (tramp);
5315 #endif
5316
5317   return tramp;
5318 }
5319
5320 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5321    Return NULL_RTX if a normal call should be emitted rather than expanding
5322    the function in-line.  EXP is the expression that is a call to the builtin
5323    function; if convenient, the result should be placed in TARGET.  */
5324
5325 static rtx
5326 expand_builtin_signbit (tree exp, rtx target)
5327 {
5328   const struct real_format *fmt;
5329   enum machine_mode fmode, imode, rmode;
5330   HOST_WIDE_INT hi, lo;
5331   tree arg, arglist;
5332   int word, bitpos;
5333   rtx temp;
5334
5335   arglist = TREE_OPERAND (exp, 1);
5336   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5337     return 0;
5338
5339   arg = TREE_VALUE (arglist);
5340   fmode = TYPE_MODE (TREE_TYPE (arg));
5341   rmode = TYPE_MODE (TREE_TYPE (exp));
5342   fmt = REAL_MODE_FORMAT (fmode);
5343
5344   /* For floating point formats without a sign bit, implement signbit
5345      as "ARG < 0.0".  */
5346   bitpos = fmt->signbit_ro;
5347   if (bitpos < 0)
5348   {
5349     /* But we can't do this if the format supports signed zero.  */
5350     if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5351       return 0;
5352
5353     arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5354                        build_real (TREE_TYPE (arg), dconst0));
5355     return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5356   }
5357
5358   temp = expand_normal (arg);
5359   if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5360     {
5361       imode = int_mode_for_mode (fmode);
5362       if (imode == BLKmode)
5363         return 0;
5364       temp = gen_lowpart (imode, temp);
5365     }
5366   else
5367     {
5368       imode = word_mode;
5369       /* Handle targets with different FP word orders.  */
5370       if (FLOAT_WORDS_BIG_ENDIAN)
5371         word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5372       else
5373         word = bitpos / BITS_PER_WORD;
5374       temp = operand_subword_force (temp, word, fmode);
5375       bitpos = bitpos % BITS_PER_WORD;
5376     }
5377
5378   /* Force the intermediate word_mode (or narrower) result into a
5379      register.  This avoids attempting to create paradoxical SUBREGs
5380      of floating point modes below.  */
5381   temp = force_reg (imode, temp);
5382
5383   /* If the bitpos is within the "result mode" lowpart, the operation
5384      can be implement with a single bitwise AND.  Otherwise, we need
5385      a right shift and an AND.  */
5386
5387   if (bitpos < GET_MODE_BITSIZE (rmode))
5388     {
5389       if (bitpos < HOST_BITS_PER_WIDE_INT)
5390         {
5391           hi = 0;
5392           lo = (HOST_WIDE_INT) 1 << bitpos;
5393         }
5394       else
5395         {
5396           hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5397           lo = 0;
5398         }
5399
5400       if (imode != rmode)
5401         temp = gen_lowpart (rmode, temp);
5402       temp = expand_binop (rmode, and_optab, temp,
5403                            immed_double_const (lo, hi, rmode),
5404                            NULL_RTX, 1, OPTAB_LIB_WIDEN);
5405     }
5406   else
5407     {
5408       /* Perform a logical right shift to place the signbit in the least
5409          significant bit, then truncate the result to the desired mode
5410          and mask just this bit.  */
5411       temp = expand_shift (RSHIFT_EXPR, imode, temp,
5412                            build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5413       temp = gen_lowpart (rmode, temp);
5414       temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5415                            NULL_RTX, 1, OPTAB_LIB_WIDEN);
5416     }
5417
5418   return temp;
5419 }
5420
5421 /* Expand fork or exec calls.  TARGET is the desired target of the
5422    call.  ARGLIST is the list of arguments of the call.  FN is the
5423    identificator of the actual function.  IGNORE is nonzero if the
5424    value is to be ignored.  */
5425
5426 static rtx
5427 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5428 {
5429   tree id, decl;
5430   tree call;
5431
5432   /* If we are not profiling, just call the function.  */
5433   if (!profile_arc_flag)
5434     return NULL_RTX;
5435
5436   /* Otherwise call the wrapper.  This should be equivalent for the rest of
5437      compiler, so the code does not diverge, and the wrapper may run the
5438      code necessary for keeping the profiling sane.  */
5439
5440   switch (DECL_FUNCTION_CODE (fn))
5441     {
5442     case BUILT_IN_FORK:
5443       id = get_identifier ("__gcov_fork");
5444       break;
5445
5446     case BUILT_IN_EXECL:
5447       id = get_identifier ("__gcov_execl");
5448       break;
5449
5450     case BUILT_IN_EXECV:
5451       id = get_identifier ("__gcov_execv");
5452       break;
5453
5454     case BUILT_IN_EXECLP:
5455       id = get_identifier ("__gcov_execlp");
5456       break;
5457
5458     case BUILT_IN_EXECLE:
5459       id = get_identifier ("__gcov_execle");
5460       break;
5461
5462     case BUILT_IN_EXECVP:
5463       id = get_identifier ("__gcov_execvp");
5464       break;
5465
5466     case BUILT_IN_EXECVE:
5467       id = get_identifier ("__gcov_execve");
5468       break;
5469
5470     default:
5471       gcc_unreachable ();
5472     }
5473
5474   decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5475   DECL_EXTERNAL (decl) = 1;
5476   TREE_PUBLIC (decl) = 1;
5477   DECL_ARTIFICIAL (decl) = 1;
5478   TREE_NOTHROW (decl) = 1;
5479   DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5480   DECL_VISIBILITY_SPECIFIED (decl) = 1;
5481   call = build_function_call_expr (decl, arglist);
5482
5483   return expand_call (call, target, ignore);
5484 }
5485
5486 \f
5487 /* Reconstitute a mode for a __sync intrinsic operation.  Since the type of
5488    the pointer in these functions is void*, the tree optimizers may remove
5489    casts.  The mode computed in expand_builtin isn't reliable either, due
5490    to __sync_bool_compare_and_swap.
5491
5492    FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5493    group of builtins.  This gives us log2 of the mode size.  */
5494
5495 static inline enum machine_mode
5496 get_builtin_sync_mode (int fcode_diff)
5497 {
5498   /* The size is not negotiable, so ask not to get BLKmode in return
5499      if the target indicates that a smaller size would be better.  */
5500   return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5501 }
5502
5503 /* Expand the memory expression LOC and return the appropriate memory operand
5504    for the builtin_sync operations.  */
5505
5506 static rtx
5507 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5508 {
5509   rtx addr, mem;
5510
5511   addr = expand_expr (loc, NULL, Pmode, EXPAND_SUM);
5512
5513   /* Note that we explicitly do not want any alias information for this
5514      memory, so that we kill all other live memories.  Otherwise we don't
5515      satisfy the full barrier semantics of the intrinsic.  */
5516   mem = validize_mem (gen_rtx_MEM (mode, addr));
5517
5518   set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5519   set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5520   MEM_VOLATILE_P (mem) = 1;
5521
5522   return mem;
5523 }
5524
5525 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5526    ARGLIST is the operands list to the function.  CODE is the rtx code
5527    that corresponds to the arithmetic or logical operation from the name;
5528    an exception here is that NOT actually means NAND.  TARGET is an optional
5529    place for us to store the results; AFTER is true if this is the
5530    fetch_and_xxx form.  IGNORE is true if we don't actually care about
5531    the result of the operation at all.  */
5532
5533 static rtx
5534 expand_builtin_sync_operation (enum machine_mode mode, tree arglist,
5535                                enum rtx_code code, bool after,
5536                                rtx target, bool ignore)
5537 {
5538   rtx val, mem;
5539
5540   /* Expand the operands.  */
5541   mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5542
5543   arglist = TREE_CHAIN (arglist);
5544   val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5545   /* If VAL is promoted to a wider mode, convert it back to MODE.  */
5546   val = convert_to_mode (mode, val, 1);
5547
5548   if (ignore)
5549     return expand_sync_operation (mem, val, code);
5550   else
5551     return expand_sync_fetch_operation (mem, val, code, after, target);
5552 }
5553
5554 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5555    intrinsics.  ARGLIST is the operands list to the function.  IS_BOOL is
5556    true if this is the boolean form.  TARGET is a place for us to store the
5557    results; this is NOT optional if IS_BOOL is true.  */
5558
5559 static rtx
5560 expand_builtin_compare_and_swap (enum machine_mode mode, tree arglist,
5561                                  bool is_bool, rtx target)
5562 {
5563   rtx old_val, new_val, mem;
5564
5565   /* Expand the operands.  */
5566   mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5567
5568   arglist = TREE_CHAIN (arglist);
5569   old_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5570   /* If OLD_VAL is promoted to a wider mode, convert it back to MODE.  */
5571   old_val = convert_to_mode (mode, old_val, 1);
5572
5573   arglist = TREE_CHAIN (arglist);
5574   new_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5575   /* If NEW_VAL is promoted to a wider mode, convert it back to MODE.  */
5576   new_val = convert_to_mode (mode, new_val, 1);
5577
5578   if (is_bool)
5579     return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5580   else
5581     return expand_val_compare_and_swap (mem, old_val, new_val, target);
5582 }
5583
5584 /* Expand the __sync_lock_test_and_set intrinsic.  Note that the most
5585    general form is actually an atomic exchange, and some targets only
5586    support a reduced form with the second argument being a constant 1.
5587    ARGLIST is the operands list to the function; TARGET is an optional
5588    place for us to store the results.  */
5589
5590 static rtx
5591 expand_builtin_lock_test_and_set (enum machine_mode mode, tree arglist,
5592                                   rtx target)
5593 {
5594   rtx val, mem;
5595
5596   /* Expand the operands.  */
5597   mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5598
5599   arglist = TREE_CHAIN (arglist);
5600   val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5601   /* If VAL is promoted to a wider mode, convert it back to MODE.  */
5602   val = convert_to_mode (mode, val, 1);
5603
5604   return expand_sync_lock_test_and_set (mem, val, target);
5605 }
5606
5607 /* Expand the __sync_synchronize intrinsic.  */
5608
5609 static void
5610 expand_builtin_synchronize (void)
5611 {
5612   tree x;
5613
5614 #ifdef HAVE_memory_barrier
5615   if (HAVE_memory_barrier)
5616     {
5617       emit_insn (gen_memory_barrier ());
5618       return;
5619     }
5620 #endif
5621
5622   /* If no explicit memory barrier instruction is available, create an
5623      empty asm stmt with a memory clobber.  */
5624   x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
5625               tree_cons (NULL, build_string (6, "memory"), NULL));
5626   ASM_VOLATILE_P (x) = 1;
5627   expand_asm_expr (x);
5628 }
5629
5630 /* Expand the __sync_lock_release intrinsic.  ARGLIST is the operands list
5631    to the function.  */
5632
5633 static void
5634 expand_builtin_lock_release (enum machine_mode mode, tree arglist)
5635 {
5636   enum insn_code icode;
5637   rtx mem, insn;
5638   rtx val = const0_rtx;
5639
5640   /* Expand the operands.  */
5641   mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5642
5643   /* If there is an explicit operation in the md file, use it.  */
5644   icode = sync_lock_release[mode];
5645   if (icode != CODE_FOR_nothing)
5646     {
5647       if (!insn_data[icode].operand[1].predicate (val, mode))
5648         val = force_reg (mode, val);
5649
5650       insn = GEN_FCN (icode) (mem, val);
5651       if (insn)
5652         {
5653           emit_insn (insn);
5654           return;
5655         }
5656     }
5657
5658   /* Otherwise we can implement this operation by emitting a barrier
5659      followed by a store of zero.  */
5660   expand_builtin_synchronize ();
5661   emit_move_insn (mem, val);
5662 }
5663 \f
5664 /* Expand an expression EXP that calls a built-in function,
5665    with result going to TARGET if that's convenient
5666    (and in mode MODE if that's convenient).
5667    SUBTARGET may be used as the target for computing one of EXP's operands.
5668    IGNORE is nonzero if the value is to be ignored.  */
5669
5670 rtx
5671 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5672                 int ignore)
5673 {
5674   tree fndecl = get_callee_fndecl (exp);
5675   tree arglist = TREE_OPERAND (exp, 1);
5676   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5677   enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5678
5679   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5680     return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5681
5682   /* When not optimizing, generate calls to library functions for a certain
5683      set of builtins.  */
5684   if (!optimize
5685       && !called_as_built_in (fndecl)
5686       && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5687       && fcode != BUILT_IN_ALLOCA)
5688     return expand_call (exp, target, ignore);
5689
5690   /* The built-in function expanders test for target == const0_rtx
5691      to determine whether the function's result will be ignored.  */
5692   if (ignore)
5693     target = const0_rtx;
5694
5695   /* If the result of a pure or const built-in function is ignored, and
5696      none of its arguments are volatile, we can avoid expanding the
5697      built-in call and just evaluate the arguments for side-effects.  */
5698   if (target == const0_rtx
5699       && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5700     {
5701       bool volatilep = false;
5702       tree arg;
5703
5704       for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5705         if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5706           {
5707             volatilep = true;
5708             break;
5709           }
5710
5711       if (! volatilep)
5712         {
5713           for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5714             expand_expr (TREE_VALUE (arg), const0_rtx,
5715                          VOIDmode, EXPAND_NORMAL);
5716           return const0_rtx;
5717         }
5718     }
5719
5720   switch (fcode)
5721     {
5722     CASE_FLT_FN (BUILT_IN_FABS):
5723       target = expand_builtin_fabs (arglist, target, subtarget);
5724       if (target)
5725         return target;
5726       break;
5727
5728     CASE_FLT_FN (BUILT_IN_COPYSIGN):
5729       target = expand_builtin_copysign (arglist, target, subtarget);
5730       if (target)
5731         return target;
5732       break;
5733
5734       /* Just do a normal library call if we were unable to fold
5735          the values.  */
5736     CASE_FLT_FN (BUILT_IN_CABS):
5737       break;
5738
5739     CASE_FLT_FN (BUILT_IN_EXP):
5740     CASE_FLT_FN (BUILT_IN_EXP10):
5741     CASE_FLT_FN (BUILT_IN_POW10):
5742     CASE_FLT_FN (BUILT_IN_EXP2):
5743     CASE_FLT_FN (BUILT_IN_EXPM1):
5744     CASE_FLT_FN (BUILT_IN_LOGB):
5745     CASE_FLT_FN (BUILT_IN_ILOGB):
5746     CASE_FLT_FN (BUILT_IN_LOG):
5747     CASE_FLT_FN (BUILT_IN_LOG10):
5748     CASE_FLT_FN (BUILT_IN_LOG2):
5749     CASE_FLT_FN (BUILT_IN_LOG1P):
5750     CASE_FLT_FN (BUILT_IN_TAN):
5751     CASE_FLT_FN (BUILT_IN_ASIN):
5752     CASE_FLT_FN (BUILT_IN_ACOS):
5753     CASE_FLT_FN (BUILT_IN_ATAN):
5754       /* Treat these like sqrt only if unsafe math optimizations are allowed,
5755          because of possible accuracy problems.  */
5756       if (! flag_unsafe_math_optimizations)
5757         break;
5758     CASE_FLT_FN (BUILT_IN_SQRT):
5759     CASE_FLT_FN (BUILT_IN_FLOOR):
5760     CASE_FLT_FN (BUILT_IN_CEIL):
5761     CASE_FLT_FN (BUILT_IN_TRUNC):
5762     CASE_FLT_FN (BUILT_IN_ROUND):
5763     CASE_FLT_FN (BUILT_IN_NEARBYINT):
5764     CASE_FLT_FN (BUILT_IN_RINT):
5765       target = expand_builtin_mathfn (exp, target, subtarget);
5766       if (target)
5767         return target;
5768       break;
5769
5770     CASE_FLT_FN (BUILT_IN_LCEIL):
5771     CASE_FLT_FN (BUILT_IN_LLCEIL):
5772     CASE_FLT_FN (BUILT_IN_LFLOOR):
5773     CASE_FLT_FN (BUILT_IN_LLFLOOR):
5774       target = expand_builtin_int_roundingfn (exp, target, subtarget);
5775       if (target)
5776         return target;
5777       break;
5778
5779     CASE_FLT_FN (BUILT_IN_LRINT):
5780     CASE_FLT_FN (BUILT_IN_LLRINT):
5781       target = expand_builtin_int_roundingfn_2 (exp, target, subtarget);
5782       if (target)
5783         return target;
5784       break;
5785
5786     CASE_FLT_FN (BUILT_IN_POW):
5787       target = expand_builtin_pow (exp, target, subtarget);
5788       if (target)
5789         return target;
5790       break;
5791
5792     CASE_FLT_FN (BUILT_IN_POWI):
5793       target = expand_builtin_powi (exp, target, subtarget);
5794       if (target)
5795         return target;
5796       break;
5797
5798     CASE_FLT_FN (BUILT_IN_ATAN2):
5799     CASE_FLT_FN (BUILT_IN_LDEXP):
5800       if (! flag_unsafe_math_optimizations)
5801         break;
5802
5803     CASE_FLT_FN (BUILT_IN_FMOD):
5804     CASE_FLT_FN (BUILT_IN_REMAINDER):
5805     CASE_FLT_FN (BUILT_IN_DREM):
5806       target = expand_builtin_mathfn_2 (exp, target, subtarget);
5807       if (target)
5808         return target;
5809       break;
5810
5811     CASE_FLT_FN (BUILT_IN_SIN):
5812     CASE_FLT_FN (BUILT_IN_COS):
5813       if (! flag_unsafe_math_optimizations)
5814         break;
5815       target = expand_builtin_mathfn_3 (exp, target, subtarget);
5816       if (target)
5817         return target;
5818       break;
5819
5820     CASE_FLT_FN (BUILT_IN_SINCOS):
5821       if (! flag_unsafe_math_optimizations)
5822         break;
5823       target = expand_builtin_sincos (exp);
5824       if (target)
5825         return target;
5826       break;
5827
5828     case BUILT_IN_APPLY_ARGS:
5829       return expand_builtin_apply_args ();
5830
5831       /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5832          FUNCTION with a copy of the parameters described by
5833          ARGUMENTS, and ARGSIZE.  It returns a block of memory
5834          allocated on the stack into which is stored all the registers
5835          that might possibly be used for returning the result of a
5836          function.  ARGUMENTS is the value returned by
5837          __builtin_apply_args.  ARGSIZE is the number of bytes of
5838          arguments that must be copied.  ??? How should this value be
5839          computed?  We'll also need a safe worst case value for varargs
5840          functions.  */
5841     case BUILT_IN_APPLY:
5842       if (!validate_arglist (arglist, POINTER_TYPE,
5843                              POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5844           && !validate_arglist (arglist, REFERENCE_TYPE,
5845                                 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5846         return const0_rtx;
5847       else
5848         {
5849           int i;
5850           tree t;
5851           rtx ops[3];
5852
5853           for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5854             ops[i] = expand_normal (TREE_VALUE (t));
5855
5856           return expand_builtin_apply (ops[0], ops[1], ops[2]);
5857         }
5858
5859       /* __builtin_return (RESULT) causes the function to return the
5860          value described by RESULT.  RESULT is address of the block of
5861          memory returned by __builtin_apply.  */
5862     case BUILT_IN_RETURN:
5863       if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5864         expand_builtin_return (expand_normal (TREE_VALUE (arglist)));
5865       return const0_rtx;
5866
5867     case BUILT_IN_SAVEREGS:
5868       return expand_builtin_saveregs ();
5869
5870     case BUILT_IN_ARGS_INFO:
5871       return expand_builtin_args_info (arglist);
5872
5873       /* Return the address of the first anonymous stack arg.  */
5874     case BUILT_IN_NEXT_ARG:
5875       if (fold_builtin_next_arg (arglist))
5876         return const0_rtx;
5877       return expand_builtin_next_arg ();
5878
5879     case BUILT_IN_CLASSIFY_TYPE:
5880       return expand_builtin_classify_type (arglist);
5881
5882     case BUILT_IN_CONSTANT_P:
5883       return const0_rtx;
5884
5885     case BUILT_IN_FRAME_ADDRESS:
5886     case BUILT_IN_RETURN_ADDRESS:
5887       return expand_builtin_frame_address (fndecl, arglist);
5888
5889     /* Returns the address of the area where the structure is returned.
5890        0 otherwise.  */
5891     case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5892       if (arglist != 0
5893           || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5894           || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5895         return const0_rtx;
5896       else
5897         return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5898
5899     case BUILT_IN_ALLOCA:
5900       target = expand_builtin_alloca (arglist, target);
5901       if (target)
5902         return target;
5903       break;
5904
5905     case BUILT_IN_STACK_SAVE:
5906       return expand_stack_save ();
5907
5908     case BUILT_IN_STACK_RESTORE:
5909       expand_stack_restore (TREE_VALUE (arglist));
5910       return const0_rtx;
5911
5912     CASE_INT_FN (BUILT_IN_FFS):
5913     case BUILT_IN_FFSIMAX:
5914       target = expand_builtin_unop (target_mode, arglist, target,
5915                                     subtarget, ffs_optab);
5916       if (target)
5917         return target;
5918       break;
5919
5920     CASE_INT_FN (BUILT_IN_CLZ):
5921     case BUILT_IN_CLZIMAX:
5922       target = expand_builtin_unop (target_mode, arglist, target,
5923                                     subtarget, clz_optab);
5924       if (target)
5925         return target;
5926       break;
5927
5928     CASE_INT_FN (BUILT_IN_CTZ):
5929     case BUILT_IN_CTZIMAX:
5930       target = expand_builtin_unop (target_mode, arglist, target,
5931                                     subtarget, ctz_optab);
5932       if (target)
5933         return target;
5934       break;
5935
5936     CASE_INT_FN (BUILT_IN_POPCOUNT):
5937     case BUILT_IN_POPCOUNTIMAX:
5938       target = expand_builtin_unop (target_mode, arglist, target,
5939                                     subtarget, popcount_optab);
5940       if (target)
5941         return target;
5942       break;
5943
5944     CASE_INT_FN (BUILT_IN_PARITY):
5945     case BUILT_IN_PARITYIMAX:
5946       target = expand_builtin_unop (target_mode, arglist, target,
5947                                     subtarget, parity_optab);
5948       if (target)
5949         return target;
5950       break;
5951
5952     case BUILT_IN_STRLEN:
5953       target = expand_builtin_strlen (arglist, target, target_mode);
5954       if (target)
5955         return target;
5956       break;
5957
5958     case BUILT_IN_STRCPY:
5959       target = expand_builtin_strcpy (fndecl, arglist, target, mode);
5960       if (target)
5961         return target;
5962       break;
5963
5964     case BUILT_IN_STRNCPY:
5965       target = expand_builtin_strncpy (exp, target, mode);
5966       if (target)
5967         return target;
5968       break;
5969
5970     case BUILT_IN_STPCPY:
5971       target = expand_builtin_stpcpy (exp, target, mode);
5972       if (target)
5973         return target;
5974       break;
5975
5976     case BUILT_IN_STRCAT:
5977       target = expand_builtin_strcat (fndecl, arglist, target, mode);
5978       if (target)
5979         return target;
5980       break;
5981
5982     case BUILT_IN_STRNCAT:
5983       target = expand_builtin_strncat (arglist, target, mode);
5984       if (target)
5985         return target;
5986       break;
5987
5988     case BUILT_IN_STRSPN:
5989       target = expand_builtin_strspn (arglist, target, mode);
5990       if (target)
5991         return target;
5992       break;
5993
5994     case BUILT_IN_STRCSPN:
5995       target = expand_builtin_strcspn (arglist, target, mode);
5996       if (target)
5997         return target;
5998       break;
5999
6000     case BUILT_IN_STRSTR:
6001       target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
6002       if (target)
6003         return target;
6004       break;
6005
6006     case BUILT_IN_STRPBRK:
6007       target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
6008       if (target)
6009         return target;
6010       break;
6011
6012     case BUILT_IN_INDEX:
6013     case BUILT_IN_STRCHR:
6014       target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
6015       if (target)
6016         return target;
6017       break;
6018
6019     case BUILT_IN_RINDEX:
6020     case BUILT_IN_STRRCHR:
6021       target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
6022       if (target)
6023         return target;
6024       break;
6025
6026     case BUILT_IN_MEMCPY:
6027       target = expand_builtin_memcpy (exp, target, mode);
6028       if (target)
6029         return target;
6030       break;
6031
6032     case BUILT_IN_MEMPCPY:
6033       target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
6034       if (target)
6035         return target;
6036       break;
6037
6038     case BUILT_IN_MEMMOVE:
6039       target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target,
6040                                        mode);
6041       if (target)
6042         return target;
6043       break;
6044
6045     case BUILT_IN_BCOPY:
6046       target = expand_builtin_bcopy (exp);
6047       if (target)
6048         return target;
6049       break;
6050
6051     case BUILT_IN_MEMSET:
6052       target = expand_builtin_memset (arglist, target, mode, exp);
6053       if (target)
6054         return target;
6055       break;
6056
6057     case BUILT_IN_BZERO:
6058       target = expand_builtin_bzero (exp);
6059       if (target)
6060         return target;
6061       break;
6062
6063     case BUILT_IN_STRCMP:
6064       target = expand_builtin_strcmp (exp, target, mode);
6065       if (target)
6066         return target;
6067       break;
6068
6069     case BUILT_IN_STRNCMP:
6070       target = expand_builtin_strncmp (exp, target, mode);
6071       if (target)
6072         return target;
6073       break;
6074
6075     case BUILT_IN_BCMP:
6076     case BUILT_IN_MEMCMP:
6077       target = expand_builtin_memcmp (exp, arglist, target, mode);
6078       if (target)
6079         return target;
6080       break;
6081
6082     case BUILT_IN_SETJMP:
6083       /* This should have been lowered to the builtins below.  */
6084       gcc_unreachable ();
6085
6086     case BUILT_IN_SETJMP_SETUP:
6087       /* __builtin_setjmp_setup is passed a pointer to an array of five words
6088           and the receiver label.  */
6089       if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6090         {
6091           rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6092                                       VOIDmode, EXPAND_NORMAL);
6093           tree label = TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist)), 0);
6094           rtx label_r = label_rtx (label);
6095
6096           /* This is copied from the handling of non-local gotos.  */
6097           expand_builtin_setjmp_setup (buf_addr, label_r);
6098           nonlocal_goto_handler_labels
6099             = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6100                                  nonlocal_goto_handler_labels);
6101           /* ??? Do not let expand_label treat us as such since we would
6102              not want to be both on the list of non-local labels and on
6103              the list of forced labels.  */
6104           FORCED_LABEL (label) = 0;
6105           return const0_rtx;
6106         }
6107       break;
6108
6109     case BUILT_IN_SETJMP_DISPATCHER:
6110        /* __builtin_setjmp_dispatcher is passed the dispatcher label.  */
6111       if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6112         {
6113           tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6114           rtx label_r = label_rtx (label);
6115
6116           /* Remove the dispatcher label from the list of non-local labels
6117              since the receiver labels have been added to it above.  */
6118           remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6119           return const0_rtx;
6120         }
6121       break;
6122
6123     case BUILT_IN_SETJMP_RECEIVER:
6124        /* __builtin_setjmp_receiver is passed the receiver label.  */
6125       if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6126         {
6127           tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6128           rtx label_r = label_rtx (label);
6129
6130           expand_builtin_setjmp_receiver (label_r);
6131           return const0_rtx;
6132         }
6133       break;
6134
6135       /* __builtin_longjmp is passed a pointer to an array of five words.
6136          It's similar to the C library longjmp function but works with
6137          __builtin_setjmp above.  */
6138     case BUILT_IN_LONGJMP:
6139       if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6140         {
6141           rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6142                                       VOIDmode, EXPAND_NORMAL);
6143           rtx value = expand_normal (TREE_VALUE (TREE_CHAIN (arglist)));
6144
6145           if (value != const1_rtx)
6146             {
6147               error ("%<__builtin_longjmp%> second argument must be 1");
6148               return const0_rtx;
6149             }
6150
6151           expand_builtin_longjmp (buf_addr, value);
6152           return const0_rtx;
6153         }
6154       break;
6155
6156     case BUILT_IN_NONLOCAL_GOTO:
6157       target = expand_builtin_nonlocal_goto (arglist);
6158       if (target)
6159         return target;
6160       break;
6161
6162       /* This updates the setjmp buffer that is its argument with the value
6163          of the current stack pointer.  */
6164     case BUILT_IN_UPDATE_SETJMP_BUF:
6165       if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6166         {
6167           rtx buf_addr
6168             = expand_normal (TREE_VALUE (arglist));
6169
6170           expand_builtin_update_setjmp_buf (buf_addr);
6171           return const0_rtx;
6172         }
6173       break;
6174
6175     case BUILT_IN_TRAP:
6176       expand_builtin_trap ();
6177       return const0_rtx;
6178
6179     case BUILT_IN_PRINTF:
6180       target = expand_builtin_printf (exp, target, mode, false);
6181       if (target)
6182         return target;
6183       break;
6184
6185     case BUILT_IN_PRINTF_UNLOCKED:
6186       target = expand_builtin_printf (exp, target, mode, true);
6187       if (target)
6188         return target;
6189       break;
6190
6191     case BUILT_IN_FPUTS:
6192       target = expand_builtin_fputs (arglist, target, false);
6193       if (target)
6194         return target;
6195       break;
6196     case BUILT_IN_FPUTS_UNLOCKED:
6197       target = expand_builtin_fputs (arglist, target, true);
6198       if (target)
6199         return target;
6200       break;
6201
6202     case BUILT_IN_FPRINTF:
6203       target = expand_builtin_fprintf (exp, target, mode, false);
6204       if (target)
6205         return target;
6206       break;
6207
6208     case BUILT_IN_FPRINTF_UNLOCKED:
6209       target = expand_builtin_fprintf (exp, target, mode, true);
6210       if (target)
6211         return target;
6212       break;
6213
6214     case BUILT_IN_SPRINTF:
6215       target = expand_builtin_sprintf (arglist, target, mode);
6216       if (target)
6217         return target;
6218       break;
6219
6220     CASE_FLT_FN (BUILT_IN_SIGNBIT):
6221       target = expand_builtin_signbit (exp, target);
6222       if (target)
6223         return target;
6224       break;
6225
6226       /* Various hooks for the DWARF 2 __throw routine.  */
6227     case BUILT_IN_UNWIND_INIT:
6228       expand_builtin_unwind_init ();
6229       return const0_rtx;
6230     case BUILT_IN_DWARF_CFA:
6231       return virtual_cfa_rtx;
6232 #ifdef DWARF2_UNWIND_INFO
6233     case BUILT_IN_DWARF_SP_COLUMN:
6234       return expand_builtin_dwarf_sp_column ();
6235     case BUILT_IN_INIT_DWARF_REG_SIZES:
6236       expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
6237       return const0_rtx;
6238 #endif
6239     case BUILT_IN_FROB_RETURN_ADDR:
6240       return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
6241     case BUILT_IN_EXTRACT_RETURN_ADDR:
6242       return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
6243     case BUILT_IN_EH_RETURN:
6244       expand_builtin_eh_return (TREE_VALUE (arglist),
6245                                 TREE_VALUE (TREE_CHAIN (arglist)));
6246       return const0_rtx;
6247 #ifdef EH_RETURN_DATA_REGNO
6248     case BUILT_IN_EH_RETURN_DATA_REGNO:
6249       return expand_builtin_eh_return_data_regno (arglist);
6250 #endif
6251     case BUILT_IN_EXTEND_POINTER:
6252       return expand_builtin_extend_pointer (TREE_VALUE (arglist));
6253
6254     case BUILT_IN_VA_START:
6255     case BUILT_IN_STDARG_START:
6256       return expand_builtin_va_start (arglist);
6257     case BUILT_IN_VA_END:
6258       return expand_builtin_va_end (arglist);
6259     case BUILT_IN_VA_COPY:
6260       return expand_builtin_va_copy (arglist);
6261     case BUILT_IN_EXPECT:
6262       return expand_builtin_expect (arglist, target);
6263     case BUILT_IN_PREFETCH:
6264       expand_builtin_prefetch (arglist);
6265       return const0_rtx;
6266
6267     case BUILT_IN_PROFILE_FUNC_ENTER:
6268       return expand_builtin_profile_func (false);
6269     case BUILT_IN_PROFILE_FUNC_EXIT:
6270       return expand_builtin_profile_func (true);
6271
6272     case BUILT_IN_INIT_TRAMPOLINE:
6273       return expand_builtin_init_trampoline (arglist);
6274     case BUILT_IN_ADJUST_TRAMPOLINE:
6275       return expand_builtin_adjust_trampoline (arglist);
6276
6277     case BUILT_IN_FORK:
6278     case BUILT_IN_EXECL:
6279     case BUILT_IN_EXECV:
6280     case BUILT_IN_EXECLP:
6281     case BUILT_IN_EXECLE:
6282     case BUILT_IN_EXECVP:
6283     case BUILT_IN_EXECVE:
6284       target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6285       if (target)
6286         return target;
6287       break;
6288
6289     case BUILT_IN_FETCH_AND_ADD_1:
6290     case BUILT_IN_FETCH_AND_ADD_2:
6291     case BUILT_IN_FETCH_AND_ADD_4:
6292     case BUILT_IN_FETCH_AND_ADD_8:
6293     case BUILT_IN_FETCH_AND_ADD_16:
6294       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6295       target = expand_builtin_sync_operation (mode, arglist, PLUS,
6296                                               false, target, ignore);
6297       if (target)
6298         return target;
6299       break;
6300
6301     case BUILT_IN_FETCH_AND_SUB_1:
6302     case BUILT_IN_FETCH_AND_SUB_2:
6303     case BUILT_IN_FETCH_AND_SUB_4:
6304     case BUILT_IN_FETCH_AND_SUB_8:
6305     case BUILT_IN_FETCH_AND_SUB_16:
6306       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6307       target = expand_builtin_sync_operation (mode, arglist, MINUS,
6308                                               false, target, ignore);
6309       if (target)
6310         return target;
6311       break;
6312
6313     case BUILT_IN_FETCH_AND_OR_1:
6314     case BUILT_IN_FETCH_AND_OR_2:
6315     case BUILT_IN_FETCH_AND_OR_4:
6316     case BUILT_IN_FETCH_AND_OR_8:
6317     case BUILT_IN_FETCH_AND_OR_16:
6318       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6319       target = expand_builtin_sync_operation (mode, arglist, IOR,
6320                                               false, target, ignore);
6321       if (target)
6322         return target;
6323       break;
6324
6325     case BUILT_IN_FETCH_AND_AND_1:
6326     case BUILT_IN_FETCH_AND_AND_2:
6327     case BUILT_IN_FETCH_AND_AND_4:
6328     case BUILT_IN_FETCH_AND_AND_8:
6329     case BUILT_IN_FETCH_AND_AND_16:
6330       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6331       target = expand_builtin_sync_operation (mode, arglist, AND,
6332                                               false, target, ignore);
6333       if (target)
6334         return target;
6335       break;
6336
6337     case BUILT_IN_FETCH_AND_XOR_1:
6338     case BUILT_IN_FETCH_AND_XOR_2:
6339     case BUILT_IN_FETCH_AND_XOR_4:
6340     case BUILT_IN_FETCH_AND_XOR_8:
6341     case BUILT_IN_FETCH_AND_XOR_16:
6342       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6343       target = expand_builtin_sync_operation (mode, arglist, XOR,
6344                                               false, target, ignore);
6345       if (target)
6346         return target;
6347       break;
6348
6349     case BUILT_IN_FETCH_AND_NAND_1:
6350     case BUILT_IN_FETCH_AND_NAND_2:
6351     case BUILT_IN_FETCH_AND_NAND_4:
6352     case BUILT_IN_FETCH_AND_NAND_8:
6353     case BUILT_IN_FETCH_AND_NAND_16:
6354       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6355       target = expand_builtin_sync_operation (mode, arglist, NOT,
6356                                               false, target, ignore);
6357       if (target)
6358         return target;
6359       break;
6360
6361     case BUILT_IN_ADD_AND_FETCH_1:
6362     case BUILT_IN_ADD_AND_FETCH_2:
6363     case BUILT_IN_ADD_AND_FETCH_4:
6364     case BUILT_IN_ADD_AND_FETCH_8:
6365     case BUILT_IN_ADD_AND_FETCH_16:
6366       mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6367       target = expand_builtin_sync_operation (mode, arglist, PLUS,
6368                                               true, target, ignore);
6369       if (target)
6370         return target;
6371       break;
6372
6373     case BUILT_IN_SUB_AND_FETCH_1:
6374     case BUILT_IN_SUB_AND_FETCH_2:
6375     case BUILT_IN_SUB_AND_FETCH_4:
6376     case BUILT_IN_SUB_AND_FETCH_8:
6377     case BUILT_IN_SUB_AND_FETCH_16:
6378       mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6379       target = expand_builtin_sync_operation (mode, arglist, MINUS,
6380                                               true, target, ignore);
6381       if (target)
6382         return target;
6383       break;
6384
6385     case BUILT_IN_OR_AND_FETCH_1:
6386     case BUILT_IN_OR_AND_FETCH_2:
6387     case BUILT_IN_OR_AND_FETCH_4:
6388     case BUILT_IN_OR_AND_FETCH_8:
6389     case BUILT_IN_OR_AND_FETCH_16:
6390       mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6391       target = expand_builtin_sync_operation (mode, arglist, IOR,
6392                                               true, target, ignore);
6393       if (target)
6394         return target;
6395       break;
6396
6397     case BUILT_IN_AND_AND_FETCH_1:
6398     case BUILT_IN_AND_AND_FETCH_2:
6399     case BUILT_IN_AND_AND_FETCH_4:
6400     case BUILT_IN_AND_AND_FETCH_8:
6401     case BUILT_IN_AND_AND_FETCH_16:
6402       mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6403       target = expand_builtin_sync_operation (mode, arglist, AND,
6404                                               true, target, ignore);
6405       if (target)
6406         return target;
6407       break;
6408
6409     case BUILT_IN_XOR_AND_FETCH_1:
6410     case BUILT_IN_XOR_AND_FETCH_2:
6411     case BUILT_IN_XOR_AND_FETCH_4:
6412     case BUILT_IN_XOR_AND_FETCH_8:
6413     case BUILT_IN_XOR_AND_FETCH_16:
6414       mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6415       target = expand_builtin_sync_operation (mode, arglist, XOR,
6416                                               true, target, ignore);
6417       if (target)
6418         return target;
6419       break;
6420
6421     case BUILT_IN_NAND_AND_FETCH_1:
6422     case BUILT_IN_NAND_AND_FETCH_2:
6423     case BUILT_IN_NAND_AND_FETCH_4:
6424     case BUILT_IN_NAND_AND_FETCH_8:
6425     case BUILT_IN_NAND_AND_FETCH_16:
6426       mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6427       target = expand_builtin_sync_operation (mode, arglist, NOT,
6428                                               true, target, ignore);
6429       if (target)
6430         return target;
6431       break;
6432
6433     case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6434     case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6435     case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6436     case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6437     case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6438       if (mode == VOIDmode)
6439         mode = TYPE_MODE (boolean_type_node);
6440       if (!target || !register_operand (target, mode))
6441         target = gen_reg_rtx (mode);
6442
6443       mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6444       target = expand_builtin_compare_and_swap (mode, arglist, true, target);
6445       if (target)
6446         return target;
6447       break;
6448
6449     case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6450     case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6451     case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6452     case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6453     case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6454       mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6455       target = expand_builtin_compare_and_swap (mode, arglist, false, target);
6456       if (target)
6457         return target;
6458       break;
6459
6460     case BUILT_IN_LOCK_TEST_AND_SET_1:
6461     case BUILT_IN_LOCK_TEST_AND_SET_2:
6462     case BUILT_IN_LOCK_TEST_AND_SET_4:
6463     case BUILT_IN_LOCK_TEST_AND_SET_8:
6464     case BUILT_IN_LOCK_TEST_AND_SET_16:
6465       mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6466       target = expand_builtin_lock_test_and_set (mode, arglist, target);
6467       if (target)
6468         return target;
6469       break;
6470
6471     case BUILT_IN_LOCK_RELEASE_1:
6472     case BUILT_IN_LOCK_RELEASE_2:
6473     case BUILT_IN_LOCK_RELEASE_4:
6474     case BUILT_IN_LOCK_RELEASE_8:
6475     case BUILT_IN_LOCK_RELEASE_16:
6476       mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6477       expand_builtin_lock_release (mode, arglist);
6478       return const0_rtx;
6479
6480     case BUILT_IN_SYNCHRONIZE:
6481       expand_builtin_synchronize ();
6482       return const0_rtx;
6483
6484     case BUILT_IN_OBJECT_SIZE:
6485       return expand_builtin_object_size (exp);
6486
6487     case BUILT_IN_MEMCPY_CHK:
6488     case BUILT_IN_MEMPCPY_CHK:
6489     case BUILT_IN_MEMMOVE_CHK:
6490     case BUILT_IN_MEMSET_CHK:
6491       target = expand_builtin_memory_chk (exp, target, mode, fcode);
6492       if (target)
6493         return target;
6494       break;
6495
6496     case BUILT_IN_STRCPY_CHK:
6497     case BUILT_IN_STPCPY_CHK:
6498     case BUILT_IN_STRNCPY_CHK:
6499     case BUILT_IN_STRCAT_CHK:
6500     case BUILT_IN_STRNCAT_CHK:
6501     case BUILT_IN_SNPRINTF_CHK:
6502     case BUILT_IN_VSNPRINTF_CHK:
6503       maybe_emit_chk_warning (exp, fcode);
6504       break;
6505
6506     case BUILT_IN_SPRINTF_CHK:
6507     case BUILT_IN_VSPRINTF_CHK:
6508       maybe_emit_sprintf_chk_warning (exp, fcode);
6509       break;
6510
6511     default:    /* just do library call, if unknown builtin */
6512       break;
6513     }
6514
6515   /* The switch statement above can drop through to cause the function
6516      to be called normally.  */
6517   return expand_call (exp, target, ignore);
6518 }
6519
6520 /* Determine whether a tree node represents a call to a built-in
6521    function.  If the tree T is a call to a built-in function with
6522    the right number of arguments of the appropriate types, return
6523    the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6524    Otherwise the return value is END_BUILTINS.  */
6525
6526 enum built_in_function
6527 builtin_mathfn_code (tree t)
6528 {
6529   tree fndecl, arglist, parmlist;
6530   tree argtype, parmtype;
6531
6532   if (TREE_CODE (t) != CALL_EXPR
6533       || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6534     return END_BUILTINS;
6535
6536   fndecl = get_callee_fndecl (t);
6537   if (fndecl == NULL_TREE
6538       || TREE_CODE (fndecl) != FUNCTION_DECL
6539       || ! DECL_BUILT_IN (fndecl)
6540       || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6541     return END_BUILTINS;
6542
6543   arglist = TREE_OPERAND (t, 1);
6544   parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6545   for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6546     {
6547       /* If a function doesn't take a variable number of arguments,
6548          the last element in the list will have type `void'.  */
6549       parmtype = TREE_VALUE (parmlist);
6550       if (VOID_TYPE_P (parmtype))
6551         {
6552           if (arglist)
6553             return END_BUILTINS;
6554           return DECL_FUNCTION_CODE (fndecl);
6555         }
6556
6557       if (! arglist)
6558         return END_BUILTINS;
6559
6560       argtype = TREE_TYPE (TREE_VALUE (arglist));
6561
6562       if (SCALAR_FLOAT_TYPE_P (parmtype))
6563         {
6564           if (! SCALAR_FLOAT_TYPE_P (argtype))
6565             return END_BUILTINS;
6566         }
6567       else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6568         {
6569           if (! COMPLEX_FLOAT_TYPE_P (argtype))
6570             return END_BUILTINS;
6571         }
6572       else if (POINTER_TYPE_P (parmtype))
6573         {
6574           if (! POINTER_TYPE_P (argtype))
6575             return END_BUILTINS;
6576         }
6577       else if (INTEGRAL_TYPE_P (parmtype))
6578         {
6579           if (! INTEGRAL_TYPE_P (argtype))
6580             return END_BUILTINS;
6581         }
6582       else
6583         return END_BUILTINS;
6584
6585       arglist = TREE_CHAIN (arglist);
6586     }
6587
6588   /* Variable-length argument list.  */
6589   return DECL_FUNCTION_CODE (fndecl);
6590 }
6591
6592 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6593    constant.  ARGLIST is the argument list of the call.  */
6594
6595 static tree
6596 fold_builtin_constant_p (tree arglist)
6597 {
6598   if (arglist == 0)
6599     return 0;
6600
6601   arglist = TREE_VALUE (arglist);
6602
6603   /* We return 1 for a numeric type that's known to be a constant
6604      value at compile-time or for an aggregate type that's a
6605      literal constant.  */
6606   STRIP_NOPS (arglist);
6607
6608   /* If we know this is a constant, emit the constant of one.  */
6609   if (CONSTANT_CLASS_P (arglist)
6610       || (TREE_CODE (arglist) == CONSTRUCTOR
6611           && TREE_CONSTANT (arglist)))
6612     return integer_one_node;
6613   if (TREE_CODE (arglist) == ADDR_EXPR)
6614     {
6615        tree op = TREE_OPERAND (arglist, 0);
6616        if (TREE_CODE (op) == STRING_CST
6617            || (TREE_CODE (op) == ARRAY_REF
6618                && integer_zerop (TREE_OPERAND (op, 1))
6619                && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6620          return integer_one_node;
6621     }
6622
6623   /* If this expression has side effects, show we don't know it to be a
6624      constant.  Likewise if it's a pointer or aggregate type since in
6625      those case we only want literals, since those are only optimized
6626      when generating RTL, not later.
6627      And finally, if we are compiling an initializer, not code, we
6628      need to return a definite result now; there's not going to be any
6629      more optimization done.  */
6630   if (TREE_SIDE_EFFECTS (arglist)
6631       || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
6632       || POINTER_TYPE_P (TREE_TYPE (arglist))
6633       || cfun == 0
6634       || folding_initializer)
6635     return integer_zero_node;
6636
6637   return 0;
6638 }
6639
6640 /* Fold a call to __builtin_expect, if we expect that a comparison against
6641    the argument will fold to a constant.  In practice, this means a true
6642    constant or the address of a non-weak symbol.  ARGLIST is the argument
6643    list of the call.  */
6644
6645 static tree
6646 fold_builtin_expect (tree arglist)
6647 {
6648   tree arg, inner;
6649
6650   if (arglist == 0)
6651     return 0;
6652
6653   arg = TREE_VALUE (arglist);
6654
6655   /* If the argument isn't invariant, then there's nothing we can do.  */
6656   if (!TREE_INVARIANT (arg))
6657     return 0;
6658
6659   /* If we're looking at an address of a weak decl, then do not fold.  */
6660   inner = arg;
6661   STRIP_NOPS (inner);
6662   if (TREE_CODE (inner) == ADDR_EXPR)
6663     {
6664       do
6665         {
6666           inner = TREE_OPERAND (inner, 0);
6667         }
6668       while (TREE_CODE (inner) == COMPONENT_REF
6669              || TREE_CODE (inner) == ARRAY_REF);
6670       if (DECL_P (inner) && DECL_WEAK (inner))
6671         return 0;
6672     }
6673
6674   /* Otherwise, ARG already has the proper type for the return value.  */
6675   return arg;
6676 }
6677
6678 /* Fold a call to __builtin_classify_type.  */
6679
6680 static tree
6681 fold_builtin_classify_type (tree arglist)
6682 {
6683   if (arglist == 0)
6684     return build_int_cst (NULL_TREE, no_type_class);
6685
6686   return build_int_cst (NULL_TREE,
6687                         type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6688 }
6689
6690 /* Fold a call to __builtin_strlen.  */
6691
6692 static tree
6693 fold_builtin_strlen (tree arglist)
6694 {
6695   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6696     return NULL_TREE;
6697   else
6698     {
6699       tree len = c_strlen (TREE_VALUE (arglist), 0);
6700
6701       if (len)
6702         {
6703           /* Convert from the internal "sizetype" type to "size_t".  */
6704           if (size_type_node)
6705             len = fold_convert (size_type_node, len);
6706           return len;
6707         }
6708
6709       return NULL_TREE;
6710     }
6711 }
6712
6713 /* Fold a call to __builtin_inf or __builtin_huge_val.  */
6714
6715 static tree
6716 fold_builtin_inf (tree type, int warn)
6717 {
6718   REAL_VALUE_TYPE real;
6719
6720   /* __builtin_inff is intended to be usable to define INFINITY on all
6721      targets.  If an infinity is not available, INFINITY expands "to a
6722      positive constant of type float that overflows at translation
6723      time", footnote "In this case, using INFINITY will violate the
6724      constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6725      Thus we pedwarn to ensure this constraint violation is
6726      diagnosed.  */
6727   if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6728     pedwarn ("target format does not support infinity");
6729
6730   real_inf (&real);
6731   return build_real (type, real);
6732 }
6733
6734 /* Fold a call to __builtin_nan or __builtin_nans.  */
6735
6736 static tree
6737 fold_builtin_nan (tree arglist, tree type, int quiet)
6738 {
6739   REAL_VALUE_TYPE real;
6740   const char *str;
6741
6742   if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6743     return 0;
6744   str = c_getstr (TREE_VALUE (arglist));
6745   if (!str)
6746     return 0;
6747
6748   if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6749     return 0;
6750
6751   return build_real (type, real);
6752 }
6753
6754 /* Return true if the floating point expression T has an integer value.
6755    We also allow +Inf, -Inf and NaN to be considered integer values.  */
6756
6757 static bool
6758 integer_valued_real_p (tree t)
6759 {
6760   switch (TREE_CODE (t))
6761     {
6762     case FLOAT_EXPR:
6763       return true;
6764
6765     case ABS_EXPR:
6766     case SAVE_EXPR:
6767     case NON_LVALUE_EXPR:
6768       return integer_valued_real_p (TREE_OPERAND (t, 0));
6769
6770     case COMPOUND_EXPR:
6771     case MODIFY_EXPR:
6772     case BIND_EXPR:
6773       return integer_valued_real_p (TREE_OPERAND (t, 1));
6774
6775     case PLUS_EXPR:
6776     case MINUS_EXPR:
6777     case MULT_EXPR:
6778     case MIN_EXPR:
6779     case MAX_EXPR:
6780       return integer_valued_real_p (TREE_OPERAND (t, 0))
6781              && integer_valued_real_p (TREE_OPERAND (t, 1));
6782
6783     case COND_EXPR:
6784       return integer_valued_real_p (TREE_OPERAND (t, 1))
6785              && integer_valued_real_p (TREE_OPERAND (t, 2));
6786
6787     case REAL_CST:
6788       if (! TREE_CONSTANT_OVERFLOW (t))
6789       {
6790         REAL_VALUE_TYPE c, cint;
6791
6792         c = TREE_REAL_CST (t);
6793         real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6794         return real_identical (&c, &cint);
6795       }
6796       break;
6797
6798     case NOP_EXPR:
6799       {
6800         tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6801         if (TREE_CODE (type) == INTEGER_TYPE)
6802           return true;
6803         if (TREE_CODE (type) == REAL_TYPE)
6804           return integer_valued_real_p (TREE_OPERAND (t, 0));
6805         break;
6806       }
6807
6808     case CALL_EXPR:
6809       switch (builtin_mathfn_code (t))
6810         {
6811         CASE_FLT_FN (BUILT_IN_CEIL):
6812         CASE_FLT_FN (BUILT_IN_FLOOR):
6813         CASE_FLT_FN (BUILT_IN_NEARBYINT):
6814         CASE_FLT_FN (BUILT_IN_RINT):
6815         CASE_FLT_FN (BUILT_IN_ROUND):
6816         CASE_FLT_FN (BUILT_IN_TRUNC):
6817           return true;
6818
6819         default:
6820           break;
6821         }
6822       break;
6823
6824     default:
6825       break;
6826     }
6827   return false;
6828 }
6829
6830 /* EXP is assumed to be builtin call where truncation can be propagated
6831    across (for instance floor((double)f) == (double)floorf (f).
6832    Do the transformation.  */
6833
6834 static tree
6835 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6836 {
6837   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6838   tree arg;
6839
6840   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6841     return 0;
6842
6843   arg = TREE_VALUE (arglist);
6844   /* Integer rounding functions are idempotent.  */
6845   if (fcode == builtin_mathfn_code (arg))
6846     return arg;
6847
6848   /* If argument is already integer valued, and we don't need to worry
6849      about setting errno, there's no need to perform rounding.  */
6850   if (! flag_errno_math && integer_valued_real_p (arg))
6851     return arg;
6852
6853   if (optimize)
6854     {
6855       tree arg0 = strip_float_extensions (arg);
6856       tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6857       tree newtype = TREE_TYPE (arg0);
6858       tree decl;
6859
6860       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6861           && (decl = mathfn_built_in (newtype, fcode)))
6862         {
6863           arglist =
6864             build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6865           return fold_convert (ftype,
6866                                build_function_call_expr (decl, arglist));
6867         }
6868     }
6869   return 0;
6870 }
6871
6872 /* EXP is assumed to be builtin call which can narrow the FP type of
6873    the argument, for instance lround((double)f) -> lroundf (f).  */
6874
6875 static tree
6876 fold_fixed_mathfn (tree fndecl, tree arglist)
6877 {
6878   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6879   tree arg;
6880
6881   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6882     return 0;
6883
6884   arg = TREE_VALUE (arglist);
6885
6886   /* If argument is already integer valued, and we don't need to worry
6887      about setting errno, there's no need to perform rounding.  */
6888   if (! flag_errno_math && integer_valued_real_p (arg))
6889     return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
6890
6891   if (optimize)
6892     {
6893       tree ftype = TREE_TYPE (arg);
6894       tree arg0 = strip_float_extensions (arg);
6895       tree newtype = TREE_TYPE (arg0);
6896       tree decl;
6897
6898       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6899           && (decl = mathfn_built_in (newtype, fcode)))
6900         {
6901           arglist =
6902             build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6903           return build_function_call_expr (decl, arglist);
6904         }
6905     }
6906
6907   /* Canonicalize llround (x) to lround (x) on LP64 targets where
6908      sizeof (long long) == sizeof (long).  */
6909   if (TYPE_PRECISION (long_long_integer_type_node)
6910       == TYPE_PRECISION (long_integer_type_node))
6911     {
6912       tree newfn = NULL_TREE;
6913       switch (fcode)
6914         {
6915         CASE_FLT_FN (BUILT_IN_LLCEIL):
6916           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6917           break;
6918
6919         CASE_FLT_FN (BUILT_IN_LLFLOOR):
6920           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6921           break;
6922
6923         CASE_FLT_FN (BUILT_IN_LLROUND):
6924           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6925           break;
6926
6927         CASE_FLT_FN (BUILT_IN_LLRINT):
6928           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6929           break;
6930
6931         default:
6932           break;
6933         }
6934
6935       if (newfn)
6936         {
6937           tree newcall = build_function_call_expr (newfn, arglist);
6938           return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
6939         }
6940     }
6941
6942   return 0;
6943 }
6944
6945 /* Fold function call to builtin cabs, cabsf or cabsl.  ARGLIST
6946    is the argument list, TYPE is the return type and FNDECL is the
6947    original function DECL.  Return NULL_TREE if no if no simplification
6948    can be made.  */
6949
6950 static tree
6951 fold_builtin_cabs (tree arglist, tree type, tree fndecl)
6952 {
6953   tree arg;
6954
6955   if (!arglist || TREE_CHAIN (arglist))
6956     return NULL_TREE;
6957
6958   arg = TREE_VALUE (arglist);
6959   if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6960       || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6961     return NULL_TREE;
6962
6963   /* Evaluate cabs of a constant at compile-time.  */
6964   if (flag_unsafe_math_optimizations
6965       && TREE_CODE (arg) == COMPLEX_CST
6966       && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6967       && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6968       && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6969       && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6970     {
6971       REAL_VALUE_TYPE r, i;
6972
6973       r = TREE_REAL_CST (TREE_REALPART (arg));
6974       i = TREE_REAL_CST (TREE_IMAGPART (arg));
6975
6976       real_arithmetic (&r, MULT_EXPR, &r, &r);
6977       real_arithmetic (&i, MULT_EXPR, &i, &i);
6978       real_arithmetic (&r, PLUS_EXPR, &r, &i);
6979       if (real_sqrt (&r, TYPE_MODE (type), &r)
6980           || ! flag_trapping_math)
6981         return build_real (type, r);
6982     }
6983
6984   /* If either part is zero, cabs is fabs of the other.  */
6985   if (TREE_CODE (arg) == COMPLEX_EXPR
6986       && real_zerop (TREE_OPERAND (arg, 0)))
6987     return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1));
6988   if (TREE_CODE (arg) == COMPLEX_EXPR
6989       && real_zerop (TREE_OPERAND (arg, 1)))
6990     return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0));
6991
6992   /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z).  */
6993   if (TREE_CODE (arg) == NEGATE_EXPR
6994       || TREE_CODE (arg) == CONJ_EXPR)
6995     {
6996       tree arglist = build_tree_list (NULL_TREE, TREE_OPERAND (arg, 0));
6997       return build_function_call_expr (fndecl, arglist);
6998     }
6999
7000   /* Don't do this when optimizing for size.  */
7001   if (flag_unsafe_math_optimizations
7002       && optimize && !optimize_size)
7003     {
7004       tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7005
7006       if (sqrtfn != NULL_TREE)
7007         {
7008           tree rpart, ipart, result, arglist;
7009
7010           arg = builtin_save_expr (arg);
7011
7012           rpart = fold_build1 (REALPART_EXPR, type, arg);
7013           ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7014
7015           rpart = builtin_save_expr (rpart);
7016           ipart = builtin_save_expr (ipart);
7017
7018           result = fold_build2 (PLUS_EXPR, type,
7019                                 fold_build2 (MULT_EXPR, type,
7020                                              rpart, rpart),
7021                                 fold_build2 (MULT_EXPR, type,
7022                                              ipart, ipart));
7023
7024           arglist = build_tree_list (NULL_TREE, result);
7025           return build_function_call_expr (sqrtfn, arglist);
7026         }
7027     }
7028
7029   return NULL_TREE;
7030 }
7031
7032 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl.  Return
7033    NULL_TREE if no simplification can be made.  */
7034
7035 static tree
7036 fold_builtin_sqrt (tree arglist, tree type)
7037 {
7038
7039   enum built_in_function fcode;
7040   tree arg = TREE_VALUE (arglist);
7041
7042   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7043     return NULL_TREE;
7044
7045   /* Optimize sqrt of constant value.  */
7046   if (TREE_CODE (arg) == REAL_CST
7047       && ! TREE_CONSTANT_OVERFLOW (arg))
7048     {
7049       REAL_VALUE_TYPE r, x;
7050
7051       x = TREE_REAL_CST (arg);
7052       if (real_sqrt (&r, TYPE_MODE (type), &x)
7053           || (!flag_trapping_math && !flag_errno_math))
7054         return build_real (type, r);
7055     }
7056
7057   /* Optimize sqrt(expN(x)) = expN(x*0.5).  */
7058   fcode = builtin_mathfn_code (arg);
7059   if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7060     {
7061       tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7062       arg = fold_build2 (MULT_EXPR, type,
7063                          TREE_VALUE (TREE_OPERAND (arg, 1)),
7064                          build_real (type, dconsthalf));
7065       arglist = build_tree_list (NULL_TREE, arg);
7066       return build_function_call_expr (expfn, arglist);
7067     }
7068
7069   /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)).  */
7070   if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7071     {
7072       tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7073
7074       if (powfn)
7075         {
7076           tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7077           tree tree_root;
7078           /* The inner root was either sqrt or cbrt.  */
7079           REAL_VALUE_TYPE dconstroot =
7080             BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7081
7082           /* Adjust for the outer root.  */
7083           SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7084           dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7085           tree_root = build_real (type, dconstroot);
7086           arglist = tree_cons (NULL_TREE, arg0,
7087                                build_tree_list (NULL_TREE, tree_root));
7088           return build_function_call_expr (powfn, arglist);
7089         }
7090     }
7091
7092   /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5).  */
7093   if (flag_unsafe_math_optimizations
7094       && (fcode == BUILT_IN_POW
7095           || fcode == BUILT_IN_POWF
7096           || fcode == BUILT_IN_POWL))
7097     {
7098       tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7099       tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7100       tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7101       tree narg1;
7102       if (!tree_expr_nonnegative_p (arg0))
7103         arg0 = build1 (ABS_EXPR, type, arg0);
7104       narg1 = fold_build2 (MULT_EXPR, type, arg1,
7105                            build_real (type, dconsthalf));
7106       arglist = tree_cons (NULL_TREE, arg0,
7107                            build_tree_list (NULL_TREE, narg1));
7108       return build_function_call_expr (powfn, arglist);
7109     }
7110
7111   return NULL_TREE;
7112 }
7113
7114 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl.  Return
7115    NULL_TREE if no simplification can be made.  */
7116 static tree
7117 fold_builtin_cbrt (tree arglist, tree type)
7118 {
7119   tree arg = TREE_VALUE (arglist);
7120   const enum built_in_function fcode = builtin_mathfn_code (arg);
7121
7122   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7123     return NULL_TREE;
7124
7125   /* Optimize cbrt of constant value.  */
7126   if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
7127     return arg;
7128
7129   if (flag_unsafe_math_optimizations)
7130     {
7131       /* Optimize cbrt(expN(x)) -> expN(x/3).  */
7132       if (BUILTIN_EXPONENT_P (fcode))
7133         {
7134           tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7135           const REAL_VALUE_TYPE third_trunc =
7136             real_value_truncate (TYPE_MODE (type), dconstthird);
7137           arg = fold_build2 (MULT_EXPR, type,
7138                              TREE_VALUE (TREE_OPERAND (arg, 1)),
7139                              build_real (type, third_trunc));
7140           arglist = build_tree_list (NULL_TREE, arg);
7141           return build_function_call_expr (expfn, arglist);
7142         }
7143
7144       /* Optimize cbrt(sqrt(x)) -> pow(x,1/6).  */
7145       if (BUILTIN_SQRT_P (fcode))
7146         {
7147           tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7148
7149           if (powfn)
7150             {
7151               tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7152               tree tree_root;
7153               REAL_VALUE_TYPE dconstroot = dconstthird;
7154
7155               SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7156               dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7157               tree_root = build_real (type, dconstroot);
7158               arglist = tree_cons (NULL_TREE, arg0,
7159                                    build_tree_list (NULL_TREE, tree_root));
7160               return build_function_call_expr (powfn, arglist);
7161             }
7162         }
7163
7164       /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative.  */
7165       if (BUILTIN_CBRT_P (fcode))
7166         {
7167           tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7168           if (tree_expr_nonnegative_p (arg0))
7169             {
7170               tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7171
7172               if (powfn)
7173                 {
7174                   tree tree_root;
7175                   REAL_VALUE_TYPE dconstroot;
7176
7177                   real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
7178                   dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7179                   tree_root = build_real (type, dconstroot);
7180                   arglist = tree_cons (NULL_TREE, arg0,
7181                                        build_tree_list (NULL_TREE, tree_root));
7182                   return build_function_call_expr (powfn, arglist);
7183                 }
7184             }
7185         }
7186
7187       /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative.  */
7188       if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7189           || fcode == BUILT_IN_POWL)
7190         {
7191           tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
7192           tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7193           if (tree_expr_nonnegative_p (arg00))
7194             {
7195               tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7196               const REAL_VALUE_TYPE dconstroot
7197                 = real_value_truncate (TYPE_MODE (type), dconstthird);
7198               tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7199                                          build_real (type, dconstroot));
7200               arglist = tree_cons (NULL_TREE, arg00,
7201                                    build_tree_list (NULL_TREE, narg01));
7202               return build_function_call_expr (powfn, arglist);
7203             }
7204         }
7205     }
7206   return NULL_TREE;
7207 }
7208
7209 /* Fold function call to builtin cos, cosf, or cosl.  Return
7210    NULL_TREE if no simplification can be made.  */
7211 static tree
7212 fold_builtin_cos (tree arglist, tree type, tree fndecl)
7213 {
7214   tree arg = TREE_VALUE (arglist), res;
7215
7216   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7217     return NULL_TREE;
7218
7219   /* Calculate the result when the argument is a constant.  */
7220   if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7221     return res;
7222   
7223   /* Optimize cos(-x) into cos (x).  */
7224   if (TREE_CODE (arg) == NEGATE_EXPR)
7225     {
7226       tree args = build_tree_list (NULL_TREE,
7227                                    TREE_OPERAND (arg, 0));
7228       return build_function_call_expr (fndecl, args);
7229     }
7230
7231   return NULL_TREE;
7232 }
7233
7234 /* Fold function call to builtin tan, tanf, or tanl.  Return
7235    NULL_TREE if no simplification can be made.  */
7236 static tree
7237 fold_builtin_tan (tree arglist, tree type)
7238 {
7239   enum built_in_function fcode;
7240   tree arg = TREE_VALUE (arglist), res;
7241
7242   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7243     return NULL_TREE;
7244
7245   /* Calculate the result when the argument is a constant.  */
7246   if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7247     return res;
7248   
7249   /* Optimize tan(atan(x)) = x.  */
7250   fcode = builtin_mathfn_code (arg);
7251   if (flag_unsafe_math_optimizations
7252       && (fcode == BUILT_IN_ATAN
7253           || fcode == BUILT_IN_ATANF
7254           || fcode == BUILT_IN_ATANL))
7255     return TREE_VALUE (TREE_OPERAND (arg, 1));
7256
7257   return NULL_TREE;
7258 }
7259
7260 /* Fold function call to builtin trunc, truncf or truncl.  Return
7261    NULL_TREE if no simplification can be made.  */
7262
7263 static tree
7264 fold_builtin_trunc (tree fndecl, tree arglist)
7265 {
7266   tree arg;
7267
7268   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7269     return 0;
7270
7271   /* Optimize trunc of constant value.  */
7272   arg = TREE_VALUE (arglist);
7273   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7274     {
7275       REAL_VALUE_TYPE r, x;
7276       tree type = TREE_TYPE (TREE_TYPE (fndecl));
7277
7278       x = TREE_REAL_CST (arg);
7279       real_trunc (&r, TYPE_MODE (type), &x);
7280       return build_real (type, r);
7281     }
7282
7283   return fold_trunc_transparent_mathfn (fndecl, arglist);
7284 }
7285
7286 /* Fold function call to builtin floor, floorf or floorl.  Return
7287    NULL_TREE if no simplification can be made.  */
7288
7289 static tree
7290 fold_builtin_floor (tree fndecl, tree arglist)
7291 {
7292   tree arg;
7293
7294   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7295     return 0;
7296
7297   /* Optimize floor of constant value.  */
7298   arg = TREE_VALUE (arglist);
7299   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7300     {
7301       REAL_VALUE_TYPE x;
7302
7303       x = TREE_REAL_CST (arg);
7304       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7305         {
7306           tree type = TREE_TYPE (TREE_TYPE (fndecl));
7307           REAL_VALUE_TYPE r;
7308
7309           real_floor (&r, TYPE_MODE (type), &x);
7310           return build_real (type, r);
7311         }
7312     }
7313
7314   /* Fold floor (x) where x is nonnegative to trunc (x).  */
7315   if (tree_expr_nonnegative_p (arg))
7316     return build_function_call_expr (mathfn_built_in (TREE_TYPE (arg),
7317                                                       BUILT_IN_TRUNC),
7318                                      arglist);
7319
7320   return fold_trunc_transparent_mathfn (fndecl, arglist);
7321 }
7322
7323 /* Fold function call to builtin ceil, ceilf or ceill.  Return
7324    NULL_TREE if no simplification can be made.  */
7325
7326 static tree
7327 fold_builtin_ceil (tree fndecl, tree arglist)
7328 {
7329   tree arg;
7330
7331   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7332     return 0;
7333
7334   /* Optimize ceil of constant value.  */
7335   arg = TREE_VALUE (arglist);
7336   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7337     {
7338       REAL_VALUE_TYPE x;
7339
7340       x = TREE_REAL_CST (arg);
7341       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7342         {
7343           tree type = TREE_TYPE (TREE_TYPE (fndecl));
7344           REAL_VALUE_TYPE r;
7345
7346           real_ceil (&r, TYPE_MODE (type), &x);
7347           return build_real (type, r);
7348         }
7349     }
7350
7351   return fold_trunc_transparent_mathfn (fndecl, arglist);
7352 }
7353
7354 /* Fold function call to builtin round, roundf or roundl.  Return
7355    NULL_TREE if no simplification can be made.  */
7356
7357 static tree
7358 fold_builtin_round (tree fndecl, tree arglist)
7359 {
7360   tree arg;
7361
7362   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7363     return 0;
7364
7365   /* Optimize round of constant value.  */
7366   arg = TREE_VALUE (arglist);
7367   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7368     {
7369       REAL_VALUE_TYPE x;
7370
7371       x = TREE_REAL_CST (arg);
7372       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7373         {
7374           tree type = TREE_TYPE (TREE_TYPE (fndecl));
7375           REAL_VALUE_TYPE r;
7376
7377           real_round (&r, TYPE_MODE (type), &x);
7378           return build_real (type, r);
7379         }
7380     }
7381
7382   return fold_trunc_transparent_mathfn (fndecl, arglist);
7383 }
7384
7385 /* Fold function call to builtin lround, lroundf or lroundl (or the
7386    corresponding long long versions) and other rounding functions.
7387    Return NULL_TREE if no simplification can be made.  */
7388
7389 static tree
7390 fold_builtin_int_roundingfn (tree fndecl, tree arglist)
7391 {
7392   tree arg;
7393
7394   if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7395     return 0;
7396
7397   /* Optimize lround of constant value.  */
7398   arg = TREE_VALUE (arglist);
7399   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7400     {
7401       const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7402
7403       if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
7404         {
7405           tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7406           tree ftype = TREE_TYPE (arg), result;
7407           HOST_WIDE_INT hi, lo;
7408           REAL_VALUE_TYPE r;
7409
7410           switch (DECL_FUNCTION_CODE (fndecl))
7411             {
7412             CASE_FLT_FN (BUILT_IN_LFLOOR):
7413             CASE_FLT_FN (BUILT_IN_LLFLOOR):
7414               real_floor (&r, TYPE_MODE (ftype), &x);
7415               break;
7416
7417             CASE_FLT_FN (BUILT_IN_LCEIL):
7418             CASE_FLT_FN (BUILT_IN_LLCEIL):
7419               real_ceil (&r, TYPE_MODE (ftype), &x);
7420               break;
7421
7422             CASE_FLT_FN (BUILT_IN_LROUND):
7423             CASE_FLT_FN (BUILT_IN_LLROUND):
7424               real_round (&r, TYPE_MODE (ftype), &x);
7425               break;
7426
7427             default:
7428               gcc_unreachable ();
7429             }
7430
7431           REAL_VALUE_TO_INT (&lo, &hi, r);
7432           result = build_int_cst_wide (NULL_TREE, lo, hi);
7433           if (int_fits_type_p (result, itype))
7434             return fold_convert (itype, result);
7435         }
7436     }
7437
7438   switch (DECL_FUNCTION_CODE (fndecl))
7439     {
7440     CASE_FLT_FN (BUILT_IN_LFLOOR):
7441     CASE_FLT_FN (BUILT_IN_LLFLOOR):
7442       /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x).  */
7443       if (tree_expr_nonnegative_p (arg))
7444         return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
7445                             arg);
7446       break;
7447     default:;
7448     }
7449
7450   return fold_fixed_mathfn (fndecl, arglist);
7451 }
7452
7453 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7454    and their long and long long variants (i.e. ffsl and ffsll).
7455    Return NULL_TREE if no simplification can be made.  */
7456
7457 static tree
7458 fold_builtin_bitop (tree fndecl, tree arglist)
7459 {
7460   tree arg;
7461
7462   if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7463     return NULL_TREE;
7464
7465   /* Optimize for constant argument.  */
7466   arg = TREE_VALUE (arglist);
7467   if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7468     {
7469       HOST_WIDE_INT hi, width, result;
7470       unsigned HOST_WIDE_INT lo;
7471       tree type;
7472
7473       type = TREE_TYPE (arg);
7474       width = TYPE_PRECISION (type);
7475       lo = TREE_INT_CST_LOW (arg);
7476
7477       /* Clear all the bits that are beyond the type's precision.  */
7478       if (width > HOST_BITS_PER_WIDE_INT)
7479         {
7480           hi = TREE_INT_CST_HIGH (arg);
7481           if (width < 2 * HOST_BITS_PER_WIDE_INT)
7482             hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7483         }
7484       else
7485         {
7486           hi = 0;
7487           if (width < HOST_BITS_PER_WIDE_INT)
7488             lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7489         }
7490
7491       switch (DECL_FUNCTION_CODE (fndecl))
7492         {
7493         CASE_INT_FN (BUILT_IN_FFS):
7494           if (lo != 0)
7495             result = exact_log2 (lo & -lo) + 1;
7496           else if (hi != 0)
7497             result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7498           else
7499             result = 0;
7500           break;
7501
7502         CASE_INT_FN (BUILT_IN_CLZ):
7503           if (hi != 0)
7504             result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7505           else if (lo != 0)
7506             result = width - floor_log2 (lo) - 1;
7507           else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7508             result = width;
7509           break;
7510
7511         CASE_INT_FN (BUILT_IN_CTZ):
7512           if (lo != 0)
7513             result = exact_log2 (lo & -lo);
7514           else if (hi != 0)
7515             result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7516           else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7517             result = width;
7518           break;
7519
7520         CASE_INT_FN (BUILT_IN_POPCOUNT):
7521           result = 0;
7522           while (lo)
7523             result++, lo &= lo - 1;
7524           while (hi)
7525             result++, hi &= hi - 1;
7526           break;
7527
7528         CASE_INT_FN (BUILT_IN_PARITY):
7529           result = 0;
7530           while (lo)
7531             result++, lo &= lo - 1;
7532           while (hi)
7533             result++, hi &= hi - 1;
7534           result &= 1;
7535           break;
7536
7537         default:
7538           gcc_unreachable ();
7539         }
7540
7541       return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7542     }
7543
7544   return NULL_TREE;
7545 }
7546
7547 /* Return true if EXPR is the real constant contained in VALUE.  */
7548
7549 static bool
7550 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
7551 {
7552   STRIP_NOPS (expr);
7553
7554   return ((TREE_CODE (expr) == REAL_CST
7555            && ! TREE_CONSTANT_OVERFLOW (expr)
7556            && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
7557           || (TREE_CODE (expr) == COMPLEX_CST
7558               && real_dconstp (TREE_REALPART (expr), value)
7559               && real_zerop (TREE_IMAGPART (expr))));
7560 }
7561
7562 /* A subroutine of fold_builtin to fold the various logarithmic
7563    functions.  EXP is the CALL_EXPR of a call to a builtin logN
7564    function.  VALUE is the base of the logN function.  */
7565
7566 static tree
7567 fold_builtin_logarithm (tree fndecl, tree arglist,
7568                         const REAL_VALUE_TYPE *value)
7569 {
7570   if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7571     {
7572       tree type = TREE_TYPE (TREE_TYPE (fndecl));
7573       tree arg = TREE_VALUE (arglist);
7574       const enum built_in_function fcode = builtin_mathfn_code (arg);
7575
7576       /* Optimize logN(1.0) = 0.0.  */
7577       if (real_onep (arg))
7578         return build_real (type, dconst0);
7579
7580       /* Optimize logN(N) = 1.0.  If N can't be truncated to MODE
7581          exactly, then only do this if flag_unsafe_math_optimizations.  */
7582       if (exact_real_truncate (TYPE_MODE (type), value)
7583           || flag_unsafe_math_optimizations)
7584         {
7585           const REAL_VALUE_TYPE value_truncate =
7586             real_value_truncate (TYPE_MODE (type), *value);
7587           if (real_dconstp (arg, &value_truncate))
7588             return build_real (type, dconst1);
7589         }
7590
7591       /* Special case, optimize logN(expN(x)) = x.  */
7592       if (flag_unsafe_math_optimizations
7593           && ((value == &dconste
7594                && (fcode == BUILT_IN_EXP
7595                    || fcode == BUILT_IN_EXPF
7596                    || fcode == BUILT_IN_EXPL))
7597               || (value == &dconst2
7598                   && (fcode == BUILT_IN_EXP2
7599                       || fcode == BUILT_IN_EXP2F
7600                       || fcode == BUILT_IN_EXP2L))
7601               || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
7602         return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7603
7604       /* Optimize logN(func()) for various exponential functions.  We
7605          want to determine the value "x" and the power "exponent" in
7606          order to transform logN(x**exponent) into exponent*logN(x).  */
7607       if (flag_unsafe_math_optimizations)
7608         {
7609           tree exponent = 0, x = 0;
7610
7611           switch (fcode)
7612           {
7613           CASE_FLT_FN (BUILT_IN_EXP):
7614             /* Prepare to do logN(exp(exponent) -> exponent*logN(e).  */
7615             x = build_real (type,
7616                             real_value_truncate (TYPE_MODE (type), dconste));
7617             exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7618             break;
7619           CASE_FLT_FN (BUILT_IN_EXP2):
7620             /* Prepare to do logN(exp2(exponent) -> exponent*logN(2).  */
7621             x = build_real (type, dconst2);
7622             exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7623             break;
7624           CASE_FLT_FN (BUILT_IN_EXP10):
7625           CASE_FLT_FN (BUILT_IN_POW10):
7626             /* Prepare to do logN(exp10(exponent) -> exponent*logN(10).  */
7627             x = build_real (type, dconst10);
7628             exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7629             break;
7630           CASE_FLT_FN (BUILT_IN_SQRT):
7631             /* Prepare to do logN(sqrt(x) -> 0.5*logN(x).  */
7632             x = TREE_VALUE (TREE_OPERAND (arg, 1));
7633             exponent = build_real (type, dconsthalf);
7634             break;
7635           CASE_FLT_FN (BUILT_IN_CBRT):
7636             /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x).  */
7637             x = TREE_VALUE (TREE_OPERAND (arg, 1));
7638             exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7639                                                               dconstthird));
7640             break;
7641           CASE_FLT_FN (BUILT_IN_POW):
7642             /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x).  */
7643             x = TREE_VALUE (TREE_OPERAND (arg, 1));
7644             exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7645             break;
7646           default:
7647             break;
7648           }
7649
7650           /* Now perform the optimization.  */
7651           if (x && exponent)
7652             {
7653               tree logfn;
7654               arglist = build_tree_list (NULL_TREE, x);
7655               logfn = build_function_call_expr (fndecl, arglist);
7656               return fold_build2 (MULT_EXPR, type, exponent, logfn);
7657             }
7658         }
7659     }
7660
7661   return 0;
7662 }
7663
7664 /* Fold a builtin function call to pow, powf, or powl.  Return
7665    NULL_TREE if no simplification can be made.  */
7666 static tree
7667 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7668 {
7669   tree arg0 = TREE_VALUE (arglist);
7670   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7671
7672   if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7673     return NULL_TREE;
7674
7675   /* Optimize pow(1.0,y) = 1.0.  */
7676   if (real_onep (arg0))
7677     return omit_one_operand (type, build_real (type, dconst1), arg1);
7678
7679   if (TREE_CODE (arg1) == REAL_CST
7680       && ! TREE_CONSTANT_OVERFLOW (arg1))
7681     {
7682       REAL_VALUE_TYPE cint;
7683       REAL_VALUE_TYPE c;
7684       HOST_WIDE_INT n;
7685
7686       c = TREE_REAL_CST (arg1);
7687
7688       /* Optimize pow(x,0.0) = 1.0.  */
7689       if (REAL_VALUES_EQUAL (c, dconst0))
7690         return omit_one_operand (type, build_real (type, dconst1),
7691                                  arg0);
7692
7693       /* Optimize pow(x,1.0) = x.  */
7694       if (REAL_VALUES_EQUAL (c, dconst1))
7695         return arg0;
7696
7697       /* Optimize pow(x,-1.0) = 1.0/x.  */
7698       if (REAL_VALUES_EQUAL (c, dconstm1))
7699         return fold_build2 (RDIV_EXPR, type,
7700                             build_real (type, dconst1), arg0);
7701
7702       /* Optimize pow(x,0.5) = sqrt(x).  */
7703       if (flag_unsafe_math_optimizations
7704           && REAL_VALUES_EQUAL (c, dconsthalf))
7705         {
7706           tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7707
7708           if (sqrtfn != NULL_TREE)
7709             {
7710               tree arglist = build_tree_list (NULL_TREE, arg0);
7711               return build_function_call_expr (sqrtfn, arglist);
7712             }
7713         }
7714
7715       /* Optimize pow(x,1.0/3.0) = cbrt(x).  */
7716       if (flag_unsafe_math_optimizations)
7717         {
7718           const REAL_VALUE_TYPE dconstroot
7719             = real_value_truncate (TYPE_MODE (type), dconstthird);
7720
7721           if (REAL_VALUES_EQUAL (c, dconstroot))
7722             {
7723               tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
7724               if (cbrtfn != NULL_TREE)
7725                 {
7726                   tree arglist = build_tree_list (NULL_TREE, arg0);
7727                   return build_function_call_expr (cbrtfn, arglist);
7728                 }
7729             }
7730         }
7731
7732       /* Check for an integer exponent.  */
7733       n = real_to_integer (&c);
7734       real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
7735       if (real_identical (&c, &cint))
7736         {
7737           /* Attempt to evaluate pow at compile-time.  */
7738           if (TREE_CODE (arg0) == REAL_CST
7739               && ! TREE_CONSTANT_OVERFLOW (arg0))
7740             {
7741               REAL_VALUE_TYPE x;
7742               bool inexact;
7743
7744               x = TREE_REAL_CST (arg0);
7745               inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7746               if (flag_unsafe_math_optimizations || !inexact)
7747                 return build_real (type, x);
7748             }
7749
7750           /* Strip sign ops from even integer powers.  */
7751           if ((n & 1) == 0 && flag_unsafe_math_optimizations)
7752             {
7753               tree narg0 = fold_strip_sign_ops (arg0);
7754               if (narg0)
7755                 {
7756                   arglist = build_tree_list (NULL_TREE, arg1);
7757                   arglist = tree_cons (NULL_TREE, narg0, arglist);
7758                   return build_function_call_expr (fndecl, arglist);
7759                 }
7760             }
7761         }
7762     }
7763
7764   if (flag_unsafe_math_optimizations)
7765     {
7766       const enum built_in_function fcode = builtin_mathfn_code (arg0);
7767
7768       /* Optimize pow(expN(x),y) = expN(x*y).  */
7769       if (BUILTIN_EXPONENT_P (fcode))
7770         {
7771           tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7772           tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7773           arg = fold_build2 (MULT_EXPR, type, arg, arg1);
7774           arglist = build_tree_list (NULL_TREE, arg);
7775           return build_function_call_expr (expfn, arglist);
7776         }
7777
7778       /* Optimize pow(sqrt(x),y) = pow(x,y*0.5).  */
7779       if (BUILTIN_SQRT_P (fcode))
7780         {
7781           tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7782           tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7783                                     build_real (type, dconsthalf));
7784
7785           arglist = tree_cons (NULL_TREE, narg0,
7786                                build_tree_list (NULL_TREE, narg1));
7787           return build_function_call_expr (fndecl, arglist);
7788         }
7789
7790       /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative.  */
7791       if (BUILTIN_CBRT_P (fcode))
7792         {
7793           tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7794           if (tree_expr_nonnegative_p (arg))
7795             {
7796               const REAL_VALUE_TYPE dconstroot
7797                 = real_value_truncate (TYPE_MODE (type), dconstthird);
7798               tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7799                                         build_real (type, dconstroot));
7800               arglist = tree_cons (NULL_TREE, arg,
7801                                    build_tree_list (NULL_TREE, narg1));
7802               return build_function_call_expr (fndecl, arglist);
7803             }
7804         }
7805
7806       /* Optimize pow(pow(x,y),z) = pow(x,y*z).  */
7807       if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7808            || fcode == BUILT_IN_POWL)
7809         {
7810           tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7811           tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7812           tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
7813           arglist = tree_cons (NULL_TREE, arg00,
7814                                build_tree_list (NULL_TREE, narg1));
7815           return build_function_call_expr (fndecl, arglist);
7816         }
7817     }
7818
7819   return NULL_TREE;
7820 }
7821
7822 /* Fold a builtin function call to powi, powif, or powil.  Return
7823    NULL_TREE if no simplification can be made.  */
7824 static tree
7825 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
7826 {
7827   tree arg0 = TREE_VALUE (arglist);
7828   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7829
7830   if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
7831     return NULL_TREE;
7832
7833   /* Optimize pow(1.0,y) = 1.0.  */
7834   if (real_onep (arg0))
7835     return omit_one_operand (type, build_real (type, dconst1), arg1);
7836
7837   if (host_integerp (arg1, 0))
7838     {
7839       HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7840
7841       /* Evaluate powi at compile-time.  */
7842       if (TREE_CODE (arg0) == REAL_CST
7843           && ! TREE_CONSTANT_OVERFLOW (arg0))
7844         {
7845           REAL_VALUE_TYPE x;
7846           x = TREE_REAL_CST (arg0);
7847           real_powi (&x, TYPE_MODE (type), &x, c);
7848           return build_real (type, x);
7849         }
7850
7851       /* Optimize pow(x,0) = 1.0.  */
7852       if (c == 0)
7853         return omit_one_operand (type, build_real (type, dconst1),
7854                                  arg0);
7855
7856       /* Optimize pow(x,1) = x.  */
7857       if (c == 1)
7858         return arg0;
7859
7860       /* Optimize pow(x,-1) = 1.0/x.  */
7861       if (c == -1)
7862         return fold_build2 (RDIV_EXPR, type,
7863                            build_real (type, dconst1), arg0);
7864     }
7865
7866   return NULL_TREE;
7867 }
7868
7869 /* A subroutine of fold_builtin to fold the various exponent
7870    functions.  Return NULL_TREE if no simplification can me made.
7871    FUNC is the corresponding MPFR exponent function.  */
7872
7873 static tree
7874 fold_builtin_exponent (tree fndecl, tree arglist,
7875                        int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7876 {
7877   if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7878     {
7879       tree type = TREE_TYPE (TREE_TYPE (fndecl));
7880       tree arg = TREE_VALUE (arglist), res;
7881       
7882       /* Calculate the result when the argument is a constant.  */
7883       if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
7884         return res;
7885
7886       /* Optimize expN(logN(x)) = x.  */
7887       if (flag_unsafe_math_optimizations)
7888         {
7889           const enum built_in_function fcode = builtin_mathfn_code (arg);
7890
7891           if ((func == mpfr_exp
7892                && (fcode == BUILT_IN_LOG
7893                    || fcode == BUILT_IN_LOGF
7894                    || fcode == BUILT_IN_LOGL))
7895               || (func == mpfr_exp2
7896                   && (fcode == BUILT_IN_LOG2
7897                       || fcode == BUILT_IN_LOG2F
7898                       || fcode == BUILT_IN_LOG2L))
7899               || (func == mpfr_exp10
7900                   && (fcode == BUILT_IN_LOG10
7901                       || fcode == BUILT_IN_LOG10F
7902                       || fcode == BUILT_IN_LOG10L)))
7903             return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7904         }
7905     }
7906
7907   return 0;
7908 }
7909
7910 /* Return true if VAR is a VAR_DECL or a component thereof.  */
7911
7912 static bool
7913 var_decl_component_p (tree var)
7914 {
7915   tree inner = var;
7916   while (handled_component_p (inner))
7917     inner = TREE_OPERAND (inner, 0);
7918   return SSA_VAR_P (inner);
7919 }
7920
7921 /* Fold function call to builtin memset.  Return
7922    NULL_TREE if no simplification can be made.  */
7923
7924 static tree
7925 fold_builtin_memset (tree arglist, tree type, bool ignore)
7926 {
7927   tree dest, c, len, var, ret;
7928   unsigned HOST_WIDE_INT length, cval;
7929
7930   if (!validate_arglist (arglist,
7931                          POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
7932     return 0;
7933
7934   dest = TREE_VALUE (arglist);
7935   c = TREE_VALUE (TREE_CHAIN (arglist));
7936   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7937
7938   if (! host_integerp (len, 1))
7939     return 0;
7940
7941   /* If the LEN parameter is zero, return DEST.  */
7942   if (integer_zerop (len))
7943     return omit_one_operand (type, dest, c);
7944
7945   if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
7946     return 0;
7947
7948   var = dest;
7949   STRIP_NOPS (var);
7950   if (TREE_CODE (var) != ADDR_EXPR)
7951     return 0;
7952
7953   var = TREE_OPERAND (var, 0);
7954   if (TREE_THIS_VOLATILE (var))
7955     return 0;
7956
7957   if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
7958       && !POINTER_TYPE_P (TREE_TYPE (var)))
7959     return 0;
7960
7961   if (! var_decl_component_p (var))
7962     return 0;
7963
7964   length = tree_low_cst (len, 1);
7965   if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
7966       || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
7967          < (int) length)
7968     return 0;
7969
7970   if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
7971     return 0;
7972
7973   if (integer_zerop (c))
7974     cval = 0;
7975   else
7976     {
7977       if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
7978         return 0;
7979
7980       cval = tree_low_cst (c, 1);
7981       cval &= 0xff;
7982       cval |= cval << 8;
7983       cval |= cval << 16;
7984       cval |= (cval << 31) << 1;
7985     }
7986
7987   ret = build_int_cst_type (TREE_TYPE (var), cval);
7988   ret = build2 (MODIFY_EXPR, TREE_TYPE (var), var, ret);
7989   if (ignore)
7990     return ret;
7991
7992   return omit_one_operand (type, dest, ret);
7993 }
7994
7995 /* Fold function call to builtin memset.  Return
7996    NULL_TREE if no simplification can be made.  */
7997
7998 static tree
7999 fold_builtin_bzero (tree arglist, bool ignore)
8000 {
8001   tree dest, size, newarglist;
8002
8003   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8004     return 0;
8005
8006   if (!ignore)
8007     return 0;
8008
8009   dest = TREE_VALUE (arglist);
8010   size = TREE_VALUE (TREE_CHAIN (arglist));
8011
8012   /* New argument list transforming bzero(ptr x, int y) to
8013      memset(ptr x, int 0, size_t y).   This is done this way
8014      so that if it isn't expanded inline, we fallback to
8015      calling bzero instead of memset.  */
8016
8017   newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8018   newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
8019   newarglist = tree_cons (NULL_TREE, dest, newarglist);
8020   return fold_builtin_memset (newarglist, void_type_node, ignore);
8021 }
8022
8023 /* Fold function call to builtin mem{{,p}cpy,move}.  Return
8024    NULL_TREE if no simplification can be made.
8025    If ENDP is 0, return DEST (like memcpy).
8026    If ENDP is 1, return DEST+LEN (like mempcpy).
8027    If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8028    If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8029    (memmove).   */
8030
8031 static tree
8032 fold_builtin_memory_op (tree arglist, tree type, bool ignore, int endp)
8033 {
8034   tree dest, src, len, destvar, srcvar, expr;
8035   unsigned HOST_WIDE_INT length;
8036
8037   if (! validate_arglist (arglist,
8038                           POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8039     return 0;
8040
8041   dest = TREE_VALUE (arglist);
8042   src = TREE_VALUE (TREE_CHAIN (arglist));
8043   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8044
8045   /* If the LEN parameter is zero, return DEST.  */
8046   if (integer_zerop (len))
8047     return omit_one_operand (type, dest, src);
8048
8049   /* If SRC and DEST are the same (and not volatile), return
8050      DEST{,+LEN,+LEN-1}.  */
8051   if (operand_equal_p (src, dest, 0))
8052     expr = len;
8053   else
8054     {
8055       if (endp == 3)
8056         {
8057           unsigned int src_align
8058              = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8059           unsigned int dest_align
8060              = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8061           /* Both DEST and SRC must be pointer types. 
8062              ??? This is what old code did.  Is the testing for pointer types
8063              really mandatory?
8064
8065              If either SRC is readonly or length is 1, we can use memcpy.  */
8066           if (dest_align && src_align
8067               && (readonly_data_expr (src)
8068                   || integer_onep (len)))
8069             {
8070               tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8071               if (!fn)
8072                 return 0;
8073               return build_function_call_expr (fn, arglist);
8074             }
8075         }
8076       if (! host_integerp (len, 1))
8077         return 0;
8078
8079       if (TREE_SIDE_EFFECTS (dest) || TREE_SIDE_EFFECTS (src))
8080         return 0;
8081
8082       destvar = dest;
8083       STRIP_NOPS (destvar);
8084       if (TREE_CODE (destvar) != ADDR_EXPR)
8085         return 0;
8086
8087       destvar = TREE_OPERAND (destvar, 0);
8088       if (TREE_THIS_VOLATILE (destvar))
8089         return 0;
8090
8091       if (!INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8092           && !POINTER_TYPE_P (TREE_TYPE (destvar))
8093           && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (destvar)))
8094         return 0;
8095
8096       if (! var_decl_component_p (destvar))
8097         return 0;
8098
8099       srcvar = src;
8100       STRIP_NOPS (srcvar);
8101       if (TREE_CODE (srcvar) != ADDR_EXPR)
8102         return 0;
8103
8104       srcvar = TREE_OPERAND (srcvar, 0);
8105       if (TREE_THIS_VOLATILE (srcvar))
8106         return 0;
8107
8108       if (!INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8109           && !POINTER_TYPE_P (TREE_TYPE (srcvar))
8110           && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (srcvar)))
8111         return 0;
8112
8113       if (! var_decl_component_p (srcvar))
8114         return 0;
8115
8116       length = tree_low_cst (len, 1);
8117       if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (destvar))) != length
8118           || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8119              < (int) length
8120           || GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (srcvar))) != length
8121           || get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8122              < (int) length)
8123         return 0;
8124
8125       if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8126            || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8127           && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8128               || POINTER_TYPE_P (TREE_TYPE (destvar))))
8129         expr = fold_convert (TREE_TYPE (destvar), srcvar);
8130       else
8131         expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
8132       expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr);
8133     }
8134
8135   if (ignore)
8136     return expr;
8137
8138   if (endp == 0 || endp == 3)
8139     return omit_one_operand (type, dest, expr);
8140
8141   if (expr == len)
8142     expr = 0;
8143
8144   if (endp == 2)
8145     len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
8146                        ssize_int (1));
8147
8148   len = fold_convert (TREE_TYPE (dest), len);
8149   dest = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
8150   dest = fold_convert (type, dest);
8151   if (expr)
8152     dest = omit_one_operand (type, dest, expr);
8153   return dest;
8154 }
8155
8156 /* Fold function call to builtin bcopy.  Return NULL_TREE if no
8157    simplification can be made.  */
8158
8159 static tree
8160 fold_builtin_bcopy (tree arglist, bool ignore)
8161 {
8162   tree src, dest, size, newarglist;
8163
8164   if (!validate_arglist (arglist,
8165                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8166     return 0;
8167
8168   if (! ignore)
8169     return 0;
8170
8171   src = TREE_VALUE (arglist);
8172   dest = TREE_VALUE (TREE_CHAIN (arglist));
8173   size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8174
8175   /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8176      memmove(ptr y, ptr x, size_t z).   This is done this way
8177      so that if it isn't expanded inline, we fallback to
8178      calling bcopy instead of memmove.  */
8179
8180   newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8181   newarglist = tree_cons (NULL_TREE, src, newarglist);
8182   newarglist = tree_cons (NULL_TREE, dest, newarglist);
8183
8184   return fold_builtin_memory_op (newarglist, void_type_node, true, /*endp=*/3);
8185 }
8186
8187 /* Fold function call to builtin strcpy.  If LEN is not NULL, it represents
8188    the length of the string to be copied.  Return NULL_TREE if no
8189    simplification can be made.  */
8190
8191 tree
8192 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
8193 {
8194   tree dest, src, fn;
8195
8196   if (!validate_arglist (arglist,
8197                          POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8198     return 0;
8199
8200   dest = TREE_VALUE (arglist);
8201   src = TREE_VALUE (TREE_CHAIN (arglist));
8202
8203   /* If SRC and DEST are the same (and not volatile), return DEST.  */
8204   if (operand_equal_p (src, dest, 0))
8205     return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
8206
8207   if (optimize_size)
8208     return 0;
8209
8210   fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8211   if (!fn)
8212     return 0;
8213
8214   if (!len)
8215     {
8216       len = c_strlen (src, 1);
8217       if (! len || TREE_SIDE_EFFECTS (len))
8218         return 0;
8219     }
8220
8221   len = size_binop (PLUS_EXPR, len, ssize_int (1));
8222   arglist = build_tree_list (NULL_TREE, len);
8223   arglist = tree_cons (NULL_TREE, src, arglist);
8224   arglist = tree_cons (NULL_TREE, dest, arglist);
8225   return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8226                        build_function_call_expr (fn, arglist));
8227 }
8228
8229 /* Fold function call to builtin strncpy.  If SLEN is not NULL, it represents
8230    the length of the source string.  Return NULL_TREE if no simplification
8231    can be made.  */
8232
8233 tree
8234 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
8235 {
8236   tree dest, src, len, fn;
8237
8238   if (!validate_arglist (arglist,
8239                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8240     return 0;
8241
8242   dest = TREE_VALUE (arglist);
8243   src = TREE_VALUE (TREE_CHAIN (arglist));
8244   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8245
8246   /* If the LEN parameter is zero, return DEST.  */
8247   if (integer_zerop (len))
8248     return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8249
8250   /* We can't compare slen with len as constants below if len is not a
8251      constant.  */
8252   if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8253     return 0;
8254
8255   if (!slen)
8256     slen = c_strlen (src, 1);
8257
8258   /* Now, we must be passed a constant src ptr parameter.  */
8259   if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8260     return 0;
8261
8262   slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8263
8264   /* We do not support simplification of this case, though we do
8265      support it when expanding trees into RTL.  */
8266   /* FIXME: generate a call to __builtin_memset.  */
8267   if (tree_int_cst_lt (slen, len))
8268     return 0;
8269
8270   /* OK transform into builtin memcpy.  */
8271   fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8272   if (!fn)
8273     return 0;
8274   return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8275                        build_function_call_expr (fn, arglist));
8276 }
8277
8278 /* Fold function call to builtin memcmp.  Return
8279    NULL_TREE if no simplification can be made.  */
8280
8281 static tree
8282 fold_builtin_memcmp (tree arglist)
8283 {
8284   tree arg1, arg2, len;
8285   const char *p1, *p2;
8286
8287   if (!validate_arglist (arglist,
8288                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8289     return 0;
8290
8291   arg1 = TREE_VALUE (arglist);
8292   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8293   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8294
8295   /* If the LEN parameter is zero, return zero.  */
8296   if (integer_zerop (len))
8297     return omit_two_operands (integer_type_node, integer_zero_node,
8298                               arg1, arg2);
8299
8300   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
8301   if (operand_equal_p (arg1, arg2, 0))
8302     return omit_one_operand (integer_type_node, integer_zero_node, len);
8303
8304   p1 = c_getstr (arg1);
8305   p2 = c_getstr (arg2);
8306
8307   /* If all arguments are constant, and the value of len is not greater
8308      than the lengths of arg1 and arg2, evaluate at compile-time.  */
8309   if (host_integerp (len, 1) && p1 && p2
8310       && compare_tree_int (len, strlen (p1) + 1) <= 0
8311       && compare_tree_int (len, strlen (p2) + 1) <= 0)
8312     {
8313       const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8314
8315       if (r > 0)
8316         return integer_one_node;
8317       else if (r < 0)
8318         return integer_minus_one_node;
8319       else
8320         return integer_zero_node;
8321     }
8322
8323   /* If len parameter is one, return an expression corresponding to
8324      (*(const unsigned char*)arg1 - (const unsigned char*)arg2).  */
8325   if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8326     {
8327       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8328       tree cst_uchar_ptr_node
8329         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8330
8331       tree ind1 = fold_convert (integer_type_node,
8332                                 build1 (INDIRECT_REF, cst_uchar_node,
8333                                         fold_convert (cst_uchar_ptr_node,
8334                                                       arg1)));
8335       tree ind2 = fold_convert (integer_type_node,
8336                                 build1 (INDIRECT_REF, cst_uchar_node,
8337                                         fold_convert (cst_uchar_ptr_node,
8338                                                       arg2)));
8339       return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8340     }
8341
8342   return 0;
8343 }
8344
8345 /* Fold function call to builtin strcmp.  Return
8346    NULL_TREE if no simplification can be made.  */
8347
8348 static tree
8349 fold_builtin_strcmp (tree arglist)
8350 {
8351   tree arg1, arg2;
8352   const char *p1, *p2;
8353
8354   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8355     return 0;
8356
8357   arg1 = TREE_VALUE (arglist);
8358   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8359
8360   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
8361   if (operand_equal_p (arg1, arg2, 0))
8362     return integer_zero_node;
8363
8364   p1 = c_getstr (arg1);
8365   p2 = c_getstr (arg2);
8366
8367   if (p1 && p2)
8368     {
8369       const int i = strcmp (p1, p2);
8370       if (i < 0)
8371         return integer_minus_one_node;
8372       else if (i > 0)
8373         return integer_one_node;
8374       else
8375         return integer_zero_node;
8376     }
8377
8378   /* If the second arg is "", return *(const unsigned char*)arg1.  */
8379   if (p2 && *p2 == '\0')
8380     {
8381       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8382       tree cst_uchar_ptr_node
8383         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8384
8385       return fold_convert (integer_type_node,
8386                            build1 (INDIRECT_REF, cst_uchar_node,
8387                                    fold_convert (cst_uchar_ptr_node,
8388                                                  arg1)));
8389     }
8390
8391   /* If the first arg is "", return -*(const unsigned char*)arg2.  */
8392   if (p1 && *p1 == '\0')
8393     {
8394       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8395       tree cst_uchar_ptr_node
8396         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8397
8398       tree temp = fold_convert (integer_type_node,
8399                                 build1 (INDIRECT_REF, cst_uchar_node,
8400                                         fold_convert (cst_uchar_ptr_node,
8401                                                       arg2)));
8402       return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8403     }
8404
8405   return 0;
8406 }
8407
8408 /* Fold function call to builtin strncmp.  Return
8409    NULL_TREE if no simplification can be made.  */
8410
8411 static tree
8412 fold_builtin_strncmp (tree arglist)
8413 {
8414   tree arg1, arg2, len;
8415   const char *p1, *p2;
8416
8417   if (!validate_arglist (arglist,
8418                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8419     return 0;
8420
8421   arg1 = TREE_VALUE (arglist);
8422   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8423   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8424
8425   /* If the LEN parameter is zero, return zero.  */
8426   if (integer_zerop (len))
8427     return omit_two_operands (integer_type_node, integer_zero_node,
8428                               arg1, arg2);
8429
8430   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
8431   if (operand_equal_p (arg1, arg2, 0))
8432     return omit_one_operand (integer_type_node, integer_zero_node, len);
8433
8434   p1 = c_getstr (arg1);
8435   p2 = c_getstr (arg2);
8436
8437   if (host_integerp (len, 1) && p1 && p2)
8438     {
8439       const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8440       if (i > 0)
8441         return integer_one_node;
8442       else if (i < 0)
8443         return integer_minus_one_node;
8444       else
8445         return integer_zero_node;
8446     }
8447
8448   /* If the second arg is "", and the length is greater than zero,
8449      return *(const unsigned char*)arg1.  */
8450   if (p2 && *p2 == '\0'
8451       && TREE_CODE (len) == INTEGER_CST
8452       && tree_int_cst_sgn (len) == 1)
8453     {
8454       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8455       tree cst_uchar_ptr_node
8456         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8457
8458       return fold_convert (integer_type_node,
8459                            build1 (INDIRECT_REF, cst_uchar_node,
8460                                    fold_convert (cst_uchar_ptr_node,
8461                                                  arg1)));
8462     }
8463
8464   /* If the first arg is "", and the length is greater than zero,
8465      return -*(const unsigned char*)arg2.  */
8466   if (p1 && *p1 == '\0'
8467       && TREE_CODE (len) == INTEGER_CST
8468       && tree_int_cst_sgn (len) == 1)
8469     {
8470       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8471       tree cst_uchar_ptr_node
8472         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8473
8474       tree temp = fold_convert (integer_type_node,
8475                                 build1 (INDIRECT_REF, cst_uchar_node,
8476                                         fold_convert (cst_uchar_ptr_node,
8477                                                       arg2)));
8478       return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8479     }
8480
8481   /* If len parameter is one, return an expression corresponding to
8482      (*(const unsigned char*)arg1 - (const unsigned char*)arg2).  */
8483   if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8484     {
8485       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8486       tree cst_uchar_ptr_node
8487         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8488
8489       tree ind1 = fold_convert (integer_type_node,
8490                                 build1 (INDIRECT_REF, cst_uchar_node,
8491                                         fold_convert (cst_uchar_ptr_node,
8492                                                       arg1)));
8493       tree ind2 = fold_convert (integer_type_node,
8494                                 build1 (INDIRECT_REF, cst_uchar_node,
8495                                         fold_convert (cst_uchar_ptr_node,
8496                                                       arg2)));
8497       return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8498     }
8499
8500   return 0;
8501 }
8502
8503 /* Fold function call to builtin signbit, signbitf or signbitl.  Return
8504    NULL_TREE if no simplification can be made.  */
8505
8506 static tree
8507 fold_builtin_signbit (tree fndecl, tree arglist)
8508 {
8509   tree type = TREE_TYPE (TREE_TYPE (fndecl));
8510   tree arg, temp;
8511
8512   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8513     return NULL_TREE;
8514
8515   arg = TREE_VALUE (arglist);
8516
8517   /* If ARG is a compile-time constant, determine the result.  */
8518   if (TREE_CODE (arg) == REAL_CST
8519       && !TREE_CONSTANT_OVERFLOW (arg))
8520     {
8521       REAL_VALUE_TYPE c;
8522
8523       c = TREE_REAL_CST (arg);
8524       temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
8525       return fold_convert (type, temp);
8526     }
8527
8528   /* If ARG is non-negative, the result is always zero.  */
8529   if (tree_expr_nonnegative_p (arg))
8530     return omit_one_operand (type, integer_zero_node, arg);
8531
8532   /* If ARG's format doesn't have signed zeros, return "arg < 0.0".  */
8533   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8534     return fold_build2 (LT_EXPR, type, arg,
8535                         build_real (TREE_TYPE (arg), dconst0));
8536
8537   return NULL_TREE;
8538 }
8539
8540 /* Fold function call to builtin copysign, copysignf or copysignl.
8541    Return NULL_TREE if no simplification can be made.  */
8542
8543 static tree
8544 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
8545 {
8546   tree arg1, arg2, tem;
8547
8548   if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8549     return NULL_TREE;
8550
8551   arg1 = TREE_VALUE (arglist);
8552   arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8553
8554   /* copysign(X,X) is X.  */
8555   if (operand_equal_p (arg1, arg2, 0))
8556     return fold_convert (type, arg1);
8557
8558   /* If ARG1 and ARG2 are compile-time constants, determine the result.  */
8559   if (TREE_CODE (arg1) == REAL_CST
8560       && TREE_CODE (arg2) == REAL_CST
8561       && !TREE_CONSTANT_OVERFLOW (arg1)
8562       && !TREE_CONSTANT_OVERFLOW (arg2))
8563     {
8564       REAL_VALUE_TYPE c1, c2;
8565
8566       c1 = TREE_REAL_CST (arg1);
8567       c2 = TREE_REAL_CST (arg2);
8568       /* c1.sign := c2.sign.  */
8569       real_copysign (&c1, &c2);
8570       return build_real (type, c1);
8571     }
8572
8573   /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8574      Remember to evaluate Y for side-effects.  */
8575   if (tree_expr_nonnegative_p (arg2))
8576     return omit_one_operand (type,
8577                              fold_build1 (ABS_EXPR, type, arg1),
8578                              arg2);
8579
8580   /* Strip sign changing operations for the first argument.  */
8581   tem = fold_strip_sign_ops (arg1);
8582   if (tem)
8583     {
8584       arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
8585       return build_function_call_expr (fndecl, arglist);
8586     }
8587
8588   return NULL_TREE;
8589 }
8590
8591 /* Fold a call to builtin isascii.  */
8592
8593 static tree
8594 fold_builtin_isascii (tree arglist)
8595 {
8596   if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8597     return 0;
8598   else
8599     {
8600       /* Transform isascii(c) -> ((c & ~0x7f) == 0).  */
8601       tree arg = TREE_VALUE (arglist);
8602
8603       arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
8604                     build_int_cst (NULL_TREE,
8605                                    ~ (unsigned HOST_WIDE_INT) 0x7f));
8606       arg = fold_build2 (EQ_EXPR, integer_type_node,
8607                          arg, integer_zero_node);
8608
8609       if (in_gimple_form && !TREE_CONSTANT (arg))
8610         return NULL_TREE;
8611       else
8612         return arg;
8613     }
8614 }
8615
8616 /* Fold a call to builtin toascii.  */
8617
8618 static tree
8619 fold_builtin_toascii (tree arglist)
8620 {
8621   if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8622     return 0;
8623   else
8624     {
8625       /* Transform toascii(c) -> (c & 0x7f).  */
8626       tree arg = TREE_VALUE (arglist);
8627
8628       return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8629                           build_int_cst (NULL_TREE, 0x7f));
8630     }
8631 }
8632
8633 /* Fold a call to builtin isdigit.  */
8634
8635 static tree
8636 fold_builtin_isdigit (tree arglist)
8637 {
8638   if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8639     return 0;
8640   else
8641     {
8642       /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9.  */
8643       /* According to the C standard, isdigit is unaffected by locale.
8644          However, it definitely is affected by the target character set.  */
8645       tree arg;
8646       unsigned HOST_WIDE_INT target_digit0
8647         = lang_hooks.to_target_charset ('0');
8648
8649       if (target_digit0 == 0)
8650         return NULL_TREE;
8651
8652       arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
8653       arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
8654                     build_int_cst (unsigned_type_node, target_digit0));
8655       arg = fold_build2 (LE_EXPR, integer_type_node, arg,
8656                          build_int_cst (unsigned_type_node, 9));
8657       if (in_gimple_form && !TREE_CONSTANT (arg))
8658         return NULL_TREE;
8659       else
8660         return arg;
8661     }
8662 }
8663
8664 /* Fold a call to fabs, fabsf or fabsl.  */
8665
8666 static tree
8667 fold_builtin_fabs (tree arglist, tree type)
8668 {
8669   tree arg;
8670
8671   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8672     return 0;
8673
8674   arg = TREE_VALUE (arglist);
8675   arg = fold_convert (type, arg);
8676   if (TREE_CODE (arg) == REAL_CST)
8677     return fold_abs_const (arg, type);
8678   return fold_build1 (ABS_EXPR, type, arg);
8679 }
8680
8681 /* Fold a call to abs, labs, llabs or imaxabs.  */
8682
8683 static tree
8684 fold_builtin_abs (tree arglist, tree type)
8685 {
8686   tree arg;
8687
8688   if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8689     return 0;
8690
8691   arg = TREE_VALUE (arglist);
8692   arg = fold_convert (type, arg);
8693   if (TREE_CODE (arg) == INTEGER_CST)
8694     return fold_abs_const (arg, type);
8695   return fold_build1 (ABS_EXPR, type, arg);
8696 }
8697
8698 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8699    EXP is the CALL_EXPR for the call.  */
8700
8701 static tree
8702 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
8703 {
8704   tree type = TREE_TYPE (TREE_TYPE (fndecl));
8705   tree arg;
8706   REAL_VALUE_TYPE r;
8707
8708   if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8709     {
8710       /* Check that we have exactly one argument.  */
8711       if (arglist == 0)
8712         {
8713           error ("too few arguments to function %qs",
8714                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8715           return error_mark_node;
8716         }
8717       else if (TREE_CHAIN (arglist) != 0)
8718         {
8719           error ("too many arguments to function %qs",
8720                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8721           return error_mark_node;
8722         }
8723       else
8724         {
8725           error ("non-floating-point argument to function %qs",
8726                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8727           return error_mark_node;
8728         }
8729     }
8730
8731   arg = TREE_VALUE (arglist);
8732   switch (builtin_index)
8733     {
8734     case BUILT_IN_ISINF:
8735       if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8736         return omit_one_operand (type, integer_zero_node, arg);
8737
8738       if (TREE_CODE (arg) == REAL_CST)
8739         {
8740           r = TREE_REAL_CST (arg);
8741           if (real_isinf (&r))
8742             return real_compare (GT_EXPR, &r, &dconst0)
8743                    ? integer_one_node : integer_minus_one_node;
8744           else
8745             return integer_zero_node;
8746         }
8747
8748       return NULL_TREE;
8749
8750     case BUILT_IN_FINITE:
8751       if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
8752           && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8753         return omit_one_operand (type, integer_one_node, arg);
8754
8755       if (TREE_CODE (arg) == REAL_CST)
8756         {
8757           r = TREE_REAL_CST (arg);
8758           return real_isinf (&r) || real_isnan (&r)
8759                  ? integer_zero_node : integer_one_node;
8760         }
8761
8762       return NULL_TREE;
8763
8764     case BUILT_IN_ISNAN:
8765       if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
8766         return omit_one_operand (type, integer_zero_node, arg);
8767
8768       if (TREE_CODE (arg) == REAL_CST)
8769         {
8770           r = TREE_REAL_CST (arg);
8771           return real_isnan (&r) ? integer_one_node : integer_zero_node;
8772         }
8773
8774       arg = builtin_save_expr (arg);
8775       return fold_build2 (UNORDERED_EXPR, type, arg, arg);
8776
8777     default:
8778       gcc_unreachable ();
8779     }
8780 }
8781
8782 /* Fold a call to an unordered comparison function such as
8783    __builtin_isgreater().  FNDECL is the FUNCTION_DECL for the function
8784    being called and ARGLIST is the argument list for the call.
8785    UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8786    the opposite of the desired result.  UNORDERED_CODE is used
8787    for modes that can hold NaNs and ORDERED_CODE is used for
8788    the rest.  */
8789
8790 static tree
8791 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
8792                             enum tree_code unordered_code,
8793                             enum tree_code ordered_code)
8794 {
8795   tree type = TREE_TYPE (TREE_TYPE (fndecl));
8796   enum tree_code code;
8797   tree arg0, arg1;
8798   tree type0, type1;
8799   enum tree_code code0, code1;
8800   tree cmp_type = NULL_TREE;
8801
8802   if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8803     {
8804       /* Check that we have exactly two arguments.  */
8805       if (arglist == 0 || TREE_CHAIN (arglist) == 0)
8806         {
8807           error ("too few arguments to function %qs",
8808                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8809           return error_mark_node;
8810         }
8811       else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
8812         {
8813           error ("too many arguments to function %qs",
8814                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8815           return error_mark_node;
8816         }
8817     }
8818
8819   arg0 = TREE_VALUE (arglist);
8820   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8821
8822   type0 = TREE_TYPE (arg0);
8823   type1 = TREE_TYPE (arg1);
8824
8825   code0 = TREE_CODE (type0);
8826   code1 = TREE_CODE (type1);
8827
8828   if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8829     /* Choose the wider of two real types.  */
8830     cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8831       ? type0 : type1;
8832   else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8833     cmp_type = type0;
8834   else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8835     cmp_type = type1;
8836   else
8837     {
8838       error ("non-floating-point argument to function %qs",
8839                  IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8840       return error_mark_node;
8841     }
8842
8843   arg0 = fold_convert (cmp_type, arg0);
8844   arg1 = fold_convert (cmp_type, arg1);
8845
8846   if (unordered_code == UNORDERED_EXPR)
8847     {
8848       if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8849         return omit_two_operands (type, integer_zero_node, arg0, arg1);
8850       return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
8851     }
8852
8853   code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
8854                                                    : ordered_code;
8855   return fold_build1 (TRUTH_NOT_EXPR, type,
8856                       fold_build2 (code, type, arg0, arg1));
8857 }
8858
8859 /* Used by constant folding to simplify calls to builtin functions.  EXP is
8860    the CALL_EXPR of a call to a builtin function.  IGNORE is true if the
8861    result of the function call is ignored.  This function returns NULL_TREE
8862    if no simplification was possible.  */
8863
8864 static tree
8865 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
8866 {
8867   tree type = TREE_TYPE (TREE_TYPE (fndecl));
8868   enum built_in_function fcode;
8869
8870   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
8871     return targetm.fold_builtin (fndecl, arglist, ignore);
8872
8873   fcode = DECL_FUNCTION_CODE (fndecl);
8874   switch (fcode)
8875     {
8876     case BUILT_IN_FPUTS:
8877       return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
8878
8879     case BUILT_IN_FPUTS_UNLOCKED:
8880       return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
8881
8882     case BUILT_IN_STRSTR:
8883       return fold_builtin_strstr (arglist, type);
8884
8885     case BUILT_IN_STRCAT:
8886       return fold_builtin_strcat (arglist);
8887
8888     case BUILT_IN_STRNCAT:
8889       return fold_builtin_strncat (arglist);
8890
8891     case BUILT_IN_STRSPN:
8892       return fold_builtin_strspn (arglist);
8893
8894     case BUILT_IN_STRCSPN:
8895       return fold_builtin_strcspn (arglist);
8896
8897     case BUILT_IN_STRCHR:
8898     case BUILT_IN_INDEX:
8899       return fold_builtin_strchr (arglist, type);
8900
8901     case BUILT_IN_STRRCHR:
8902     case BUILT_IN_RINDEX:
8903       return fold_builtin_strrchr (arglist, type);
8904
8905     case BUILT_IN_STRCPY:
8906       return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
8907
8908     case BUILT_IN_STRNCPY:
8909       return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
8910
8911     case BUILT_IN_STRCMP:
8912       return fold_builtin_strcmp (arglist);
8913
8914     case BUILT_IN_STRNCMP:
8915       return fold_builtin_strncmp (arglist);
8916
8917     case BUILT_IN_STRPBRK:
8918       return fold_builtin_strpbrk (arglist, type);
8919
8920     case BUILT_IN_BCMP:
8921     case BUILT_IN_MEMCMP:
8922       return fold_builtin_memcmp (arglist);
8923
8924     case BUILT_IN_SPRINTF:
8925       return fold_builtin_sprintf (arglist, ignore);
8926
8927     case BUILT_IN_CONSTANT_P:
8928       {
8929         tree val;
8930
8931         val = fold_builtin_constant_p (arglist);
8932         /* Gimplification will pull the CALL_EXPR for the builtin out of
8933            an if condition.  When not optimizing, we'll not CSE it back.
8934            To avoid link error types of regressions, return false now.  */
8935         if (!val && !optimize)
8936           val = integer_zero_node;
8937
8938         return val;
8939       }
8940
8941     case BUILT_IN_EXPECT:
8942       return fold_builtin_expect (arglist);
8943
8944     case BUILT_IN_CLASSIFY_TYPE:
8945       return fold_builtin_classify_type (arglist);
8946
8947     case BUILT_IN_STRLEN:
8948       return fold_builtin_strlen (arglist);
8949
8950     CASE_FLT_FN (BUILT_IN_FABS):
8951       return fold_builtin_fabs (arglist, type);
8952
8953     case BUILT_IN_ABS:
8954     case BUILT_IN_LABS:
8955     case BUILT_IN_LLABS:
8956     case BUILT_IN_IMAXABS:
8957       return fold_builtin_abs (arglist, type);
8958
8959     CASE_FLT_FN (BUILT_IN_CONJ):
8960       if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8961         return fold_build1 (CONJ_EXPR, type, TREE_VALUE (arglist));
8962       break;
8963
8964     CASE_FLT_FN (BUILT_IN_CREAL):
8965       if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8966         return non_lvalue (fold_build1 (REALPART_EXPR, type,
8967                                         TREE_VALUE (arglist)));
8968       break;
8969
8970     CASE_FLT_FN (BUILT_IN_CIMAG):
8971       if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8972         return non_lvalue (fold_build1 (IMAGPART_EXPR, type,
8973                                         TREE_VALUE (arglist)));
8974       break;
8975
8976     CASE_FLT_FN (BUILT_IN_CABS):
8977       return fold_builtin_cabs (arglist, type, fndecl);
8978
8979     CASE_FLT_FN (BUILT_IN_SQRT):
8980       return fold_builtin_sqrt (arglist, type);
8981
8982     CASE_FLT_FN (BUILT_IN_CBRT):
8983       return fold_builtin_cbrt (arglist, type);
8984
8985     CASE_FLT_FN (BUILT_IN_ASIN):
8986       if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8987         return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asin,
8988                              &dconstm1, &dconst1, true);
8989     break;
8990
8991     CASE_FLT_FN (BUILT_IN_ACOS):
8992       if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8993         return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acos,
8994                              &dconstm1, &dconst1, true);
8995     break;
8996
8997     CASE_FLT_FN (BUILT_IN_ATAN):
8998       if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8999         return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atan,
9000                              NULL, NULL, 0);
9001     break;
9002
9003     CASE_FLT_FN (BUILT_IN_ASINH):
9004       if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9005         return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asinh,
9006                              NULL, NULL, 0);
9007     break;
9008
9009     CASE_FLT_FN (BUILT_IN_ACOSH):
9010       if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9011         return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acosh,
9012                              &dconst1, NULL, true);
9013     break;
9014
9015     CASE_FLT_FN (BUILT_IN_ATANH):
9016       if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9017         return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atanh,
9018                              &dconstm1, &dconst1, false);
9019     break;
9020
9021     CASE_FLT_FN (BUILT_IN_SIN):
9022       if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9023         return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sin,
9024                              NULL, NULL, 0);
9025     break;
9026
9027     CASE_FLT_FN (BUILT_IN_COS):
9028       return fold_builtin_cos (arglist, type, fndecl);
9029
9030     CASE_FLT_FN (BUILT_IN_TAN):
9031       return fold_builtin_tan (arglist, type);
9032
9033     CASE_FLT_FN (BUILT_IN_SINH):
9034       if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9035         return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sinh,
9036                              NULL, NULL, 0);
9037     break;
9038
9039     CASE_FLT_FN (BUILT_IN_COSH):
9040       if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9041         return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_cosh,
9042                              NULL, NULL, 0);
9043     break;
9044
9045     CASE_FLT_FN (BUILT_IN_TANH):
9046       if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9047         return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_tanh,
9048                              NULL, NULL, 0);
9049     break;
9050
9051     CASE_FLT_FN (BUILT_IN_EXP):
9052       return fold_builtin_exponent (fndecl, arglist, mpfr_exp);
9053
9054     CASE_FLT_FN (BUILT_IN_EXP2):
9055       return fold_builtin_exponent (fndecl, arglist, mpfr_exp2);
9056
9057     CASE_FLT_FN (BUILT_IN_EXP10):
9058     CASE_FLT_FN (BUILT_IN_POW10):
9059       return fold_builtin_exponent (fndecl, arglist, mpfr_exp10);
9060
9061     CASE_FLT_FN (BUILT_IN_LOG):
9062       return fold_builtin_logarithm (fndecl, arglist, &dconste);
9063
9064     CASE_FLT_FN (BUILT_IN_LOG2):
9065       return fold_builtin_logarithm (fndecl, arglist, &dconst2);
9066
9067     CASE_FLT_FN (BUILT_IN_LOG10):
9068       return fold_builtin_logarithm (fndecl, arglist, &dconst10);
9069
9070     CASE_FLT_FN (BUILT_IN_POW):
9071       return fold_builtin_pow (fndecl, arglist, type);
9072
9073     CASE_FLT_FN (BUILT_IN_POWI):
9074       return fold_builtin_powi (fndecl, arglist, type);
9075
9076     CASE_FLT_FN (BUILT_IN_INF):
9077     case BUILT_IN_INFD32:
9078     case BUILT_IN_INFD64:
9079     case BUILT_IN_INFD128:
9080       return fold_builtin_inf (type, true);
9081
9082     CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9083       return fold_builtin_inf (type, false);
9084
9085     CASE_FLT_FN (BUILT_IN_NAN):
9086     case BUILT_IN_NAND32:
9087     case BUILT_IN_NAND64:
9088     case BUILT_IN_NAND128:
9089       return fold_builtin_nan (arglist, type, true);
9090
9091     CASE_FLT_FN (BUILT_IN_NANS):
9092       return fold_builtin_nan (arglist, type, false);
9093
9094     CASE_FLT_FN (BUILT_IN_FLOOR):
9095       return fold_builtin_floor (fndecl, arglist);
9096
9097     CASE_FLT_FN (BUILT_IN_CEIL):
9098       return fold_builtin_ceil (fndecl, arglist);
9099
9100     CASE_FLT_FN (BUILT_IN_TRUNC):
9101       return fold_builtin_trunc (fndecl, arglist);
9102
9103     CASE_FLT_FN (BUILT_IN_ROUND):
9104       return fold_builtin_round (fndecl, arglist);
9105
9106     CASE_FLT_FN (BUILT_IN_NEARBYINT):
9107     CASE_FLT_FN (BUILT_IN_RINT):
9108       return fold_trunc_transparent_mathfn (fndecl, arglist);
9109
9110     CASE_FLT_FN (BUILT_IN_LCEIL):
9111     CASE_FLT_FN (BUILT_IN_LLCEIL):
9112     CASE_FLT_FN (BUILT_IN_LFLOOR):
9113     CASE_FLT_FN (BUILT_IN_LLFLOOR):
9114     CASE_FLT_FN (BUILT_IN_LROUND):
9115     CASE_FLT_FN (BUILT_IN_LLROUND):
9116       return fold_builtin_int_roundingfn (fndecl, arglist);
9117
9118     CASE_FLT_FN (BUILT_IN_LRINT):
9119     CASE_FLT_FN (BUILT_IN_LLRINT):
9120       return fold_fixed_mathfn (fndecl, arglist);
9121
9122     CASE_INT_FN (BUILT_IN_FFS):
9123     CASE_INT_FN (BUILT_IN_CLZ):
9124     CASE_INT_FN (BUILT_IN_CTZ):
9125     CASE_INT_FN (BUILT_IN_POPCOUNT):
9126     CASE_INT_FN (BUILT_IN_PARITY):
9127       return fold_builtin_bitop (fndecl, arglist);
9128
9129     case BUILT_IN_MEMSET:
9130       return fold_builtin_memset (arglist, type, ignore);
9131
9132     case BUILT_IN_MEMCPY:
9133       return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/0);
9134
9135     case BUILT_IN_MEMPCPY:
9136       return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/1);
9137
9138     case BUILT_IN_MEMMOVE:
9139       return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/3);
9140
9141     case BUILT_IN_BZERO:
9142       return fold_builtin_bzero (arglist, ignore);
9143
9144     case BUILT_IN_BCOPY:
9145       return fold_builtin_bcopy (arglist, ignore);
9146
9147     CASE_FLT_FN (BUILT_IN_SIGNBIT):
9148       return fold_builtin_signbit (fndecl, arglist);
9149
9150     case BUILT_IN_ISASCII:
9151       return fold_builtin_isascii (arglist);
9152
9153     case BUILT_IN_TOASCII:
9154       return fold_builtin_toascii (arglist);
9155
9156     case BUILT_IN_ISDIGIT:
9157       return fold_builtin_isdigit (arglist);
9158
9159     CASE_FLT_FN (BUILT_IN_COPYSIGN):
9160       return fold_builtin_copysign (fndecl, arglist, type);
9161
9162     CASE_FLT_FN (BUILT_IN_FINITE):
9163     case BUILT_IN_FINITED32:
9164     case BUILT_IN_FINITED64:
9165     case BUILT_IN_FINITED128:
9166       return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
9167
9168     CASE_FLT_FN (BUILT_IN_ISINF):
9169     case BUILT_IN_ISINFD32:
9170     case BUILT_IN_ISINFD64:
9171     case BUILT_IN_ISINFD128:
9172       return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
9173
9174     CASE_FLT_FN (BUILT_IN_ISNAN):
9175     case BUILT_IN_ISNAND32:
9176     case BUILT_IN_ISNAND64:
9177     case BUILT_IN_ISNAND128:
9178       return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
9179
9180     case BUILT_IN_ISGREATER:
9181       return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
9182     case BUILT_IN_ISGREATEREQUAL:
9183       return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
9184     case BUILT_IN_ISLESS:
9185       return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
9186     case BUILT_IN_ISLESSEQUAL:
9187       return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
9188     case BUILT_IN_ISLESSGREATER:
9189       return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
9190     case BUILT_IN_ISUNORDERED:
9191       return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
9192                                          NOP_EXPR);
9193
9194       /* We do the folding for va_start in the expander.  */
9195     case BUILT_IN_VA_START:
9196       break;
9197
9198     case BUILT_IN_OBJECT_SIZE:
9199       return fold_builtin_object_size (arglist);
9200     case BUILT_IN_MEMCPY_CHK:
9201     case BUILT_IN_MEMPCPY_CHK:
9202     case BUILT_IN_MEMMOVE_CHK:
9203     case BUILT_IN_MEMSET_CHK:
9204       return fold_builtin_memory_chk (fndecl, arglist, NULL_TREE, ignore,
9205                                       DECL_FUNCTION_CODE (fndecl));
9206     case BUILT_IN_STRCPY_CHK:
9207     case BUILT_IN_STPCPY_CHK:
9208       return fold_builtin_stxcpy_chk (fndecl, arglist, NULL_TREE, ignore,
9209                                       DECL_FUNCTION_CODE (fndecl));
9210     case BUILT_IN_STRNCPY_CHK:
9211       return fold_builtin_strncpy_chk (arglist, NULL_TREE);
9212     case BUILT_IN_STRCAT_CHK:
9213       return fold_builtin_strcat_chk (fndecl, arglist);
9214     case BUILT_IN_STRNCAT_CHK:
9215       return fold_builtin_strncat_chk (fndecl, arglist);
9216     case BUILT_IN_SPRINTF_CHK:
9217     case BUILT_IN_VSPRINTF_CHK:
9218       return fold_builtin_sprintf_chk (arglist, DECL_FUNCTION_CODE (fndecl));
9219     case BUILT_IN_SNPRINTF_CHK:
9220     case BUILT_IN_VSNPRINTF_CHK:
9221       return fold_builtin_snprintf_chk (arglist, NULL_TREE,
9222                                         DECL_FUNCTION_CODE (fndecl));
9223
9224     case BUILT_IN_PRINTF:
9225     case BUILT_IN_PRINTF_UNLOCKED:
9226     case BUILT_IN_VPRINTF:
9227     case BUILT_IN_PRINTF_CHK:
9228     case BUILT_IN_VPRINTF_CHK:
9229       return fold_builtin_printf (fndecl, arglist, ignore,
9230                                   DECL_FUNCTION_CODE (fndecl));
9231
9232     case BUILT_IN_FPRINTF:
9233     case BUILT_IN_FPRINTF_UNLOCKED:
9234     case BUILT_IN_VFPRINTF:
9235     case BUILT_IN_FPRINTF_CHK:
9236     case BUILT_IN_VFPRINTF_CHK:
9237       return fold_builtin_fprintf (fndecl, arglist, ignore,
9238                                    DECL_FUNCTION_CODE (fndecl));
9239
9240     default:
9241       break;
9242     }
9243
9244   return 0;
9245 }
9246
9247 /* A wrapper function for builtin folding that prevents warnings for
9248    "statement without effect" and the like, caused by removing the
9249    call node earlier than the warning is generated.  */
9250
9251 tree
9252 fold_builtin (tree fndecl, tree arglist, bool ignore)
9253 {
9254   tree exp = fold_builtin_1 (fndecl, arglist, ignore);
9255   if (exp)
9256     {
9257       exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
9258       TREE_NO_WARNING (exp) = 1;
9259     }
9260
9261   return exp;
9262 }
9263
9264 /* Conveniently construct a function call expression.  */
9265
9266 tree
9267 build_function_call_expr (tree fn, tree arglist)
9268 {
9269   tree call_expr;
9270
9271   call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
9272   return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
9273                       call_expr, arglist, NULL_TREE);
9274 }
9275
9276 /* This function validates the types of a function call argument list
9277    represented as a tree chain of parameters against a specified list
9278    of tree_codes.  If the last specifier is a 0, that represents an
9279    ellipses, otherwise the last specifier must be a VOID_TYPE.  */
9280
9281 static int
9282 validate_arglist (tree arglist, ...)
9283 {
9284   enum tree_code code;
9285   int res = 0;
9286   va_list ap;
9287
9288   va_start (ap, arglist);
9289
9290   do
9291     {
9292       code = va_arg (ap, enum tree_code);
9293       switch (code)
9294         {
9295         case 0:
9296           /* This signifies an ellipses, any further arguments are all ok.  */
9297           res = 1;
9298           goto end;
9299         case VOID_TYPE:
9300           /* This signifies an endlink, if no arguments remain, return
9301              true, otherwise return false.  */
9302           res = arglist == 0;
9303           goto end;
9304         default:
9305           /* If no parameters remain or the parameter's code does not
9306              match the specified code, return false.  Otherwise continue
9307              checking any remaining arguments.  */
9308           if (arglist == 0)
9309             goto end;
9310           if (code == POINTER_TYPE)
9311             {
9312               if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
9313                 goto end;
9314             }
9315           else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
9316             goto end;
9317           break;
9318         }
9319       arglist = TREE_CHAIN (arglist);
9320     }
9321   while (1);
9322
9323   /* We need gotos here since we can only have one VA_CLOSE in a
9324      function.  */
9325  end: ;
9326   va_end (ap);
9327
9328   return res;
9329 }
9330
9331 /* Default target-specific builtin expander that does nothing.  */
9332
9333 rtx
9334 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9335                         rtx target ATTRIBUTE_UNUSED,
9336                         rtx subtarget ATTRIBUTE_UNUSED,
9337                         enum machine_mode mode ATTRIBUTE_UNUSED,
9338                         int ignore ATTRIBUTE_UNUSED)
9339 {
9340   return NULL_RTX;
9341 }
9342
9343 /* Returns true is EXP represents data that would potentially reside
9344    in a readonly section.  */
9345
9346 static bool
9347 readonly_data_expr (tree exp)
9348 {
9349   STRIP_NOPS (exp);
9350
9351   if (TREE_CODE (exp) != ADDR_EXPR)
9352     return false;
9353
9354   exp = get_base_address (TREE_OPERAND (exp, 0));
9355   if (!exp)
9356     return false;
9357
9358   /* Make sure we call decl_readonly_section only for trees it
9359      can handle (since it returns true for everything it doesn't
9360      understand).  */
9361   if (TREE_CODE (exp) == STRING_CST
9362       || TREE_CODE (exp) == CONSTRUCTOR
9363       || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
9364     return decl_readonly_section (exp, 0);
9365   else
9366     return false;
9367 }
9368
9369 /* Simplify a call to the strstr builtin.
9370
9371    Return 0 if no simplification was possible, otherwise return the
9372    simplified form of the call as a tree.
9373
9374    The simplified form may be a constant or other expression which
9375    computes the same value, but in a more efficient manner (including
9376    calls to other builtin functions).
9377
9378    The call may contain arguments which need to be evaluated, but
9379    which are not useful to determine the result of the call.  In
9380    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
9381    COMPOUND_EXPR will be an argument which must be evaluated.
9382    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
9383    COMPOUND_EXPR in the chain will contain the tree for the simplified
9384    form of the builtin function call.  */
9385
9386 static tree
9387 fold_builtin_strstr (tree arglist, tree type)
9388 {
9389   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9390     return 0;
9391   else
9392     {
9393       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9394       tree fn;
9395       const char *p1, *p2;
9396
9397       p2 = c_getstr (s2);
9398       if (p2 == NULL)
9399         return 0;
9400
9401       p1 = c_getstr (s1);
9402       if (p1 != NULL)
9403         {
9404           const char *r = strstr (p1, p2);
9405           tree tem;
9406
9407           if (r == NULL)
9408             return build_int_cst (TREE_TYPE (s1), 0);
9409
9410           /* Return an offset into the constant string argument.  */
9411           tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9412                              s1, build_int_cst (TREE_TYPE (s1), r - p1));
9413           return fold_convert (type, tem);
9414         }
9415
9416       /* The argument is const char *, and the result is char *, so we need
9417          a type conversion here to avoid a warning.  */
9418       if (p2[0] == '\0')
9419         return fold_convert (type, s1);
9420
9421       if (p2[1] != '\0')
9422         return 0;
9423
9424       fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9425       if (!fn)
9426         return 0;
9427
9428       /* New argument list transforming strstr(s1, s2) to
9429          strchr(s1, s2[0]).  */
9430       arglist = build_tree_list (NULL_TREE,
9431                                  build_int_cst (NULL_TREE, p2[0]));
9432       arglist = tree_cons (NULL_TREE, s1, arglist);
9433       return build_function_call_expr (fn, arglist);
9434     }
9435 }
9436
9437 /* Simplify a call to the strchr builtin.
9438
9439    Return 0 if no simplification was possible, otherwise return the
9440    simplified form of the call as a tree.
9441
9442    The simplified form may be a constant or other expression which
9443    computes the same value, but in a more efficient manner (including
9444    calls to other builtin functions).
9445
9446    The call may contain arguments which need to be evaluated, but
9447    which are not useful to determine the result of the call.  In
9448    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
9449    COMPOUND_EXPR will be an argument which must be evaluated.
9450    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
9451    COMPOUND_EXPR in the chain will contain the tree for the simplified
9452    form of the builtin function call.  */
9453
9454 static tree
9455 fold_builtin_strchr (tree arglist, tree type)
9456 {
9457   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9458     return 0;
9459   else
9460     {
9461       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9462       const char *p1;
9463
9464       if (TREE_CODE (s2) != INTEGER_CST)
9465         return 0;
9466
9467       p1 = c_getstr (s1);
9468       if (p1 != NULL)
9469         {
9470           char c;
9471           const char *r;
9472           tree tem;
9473
9474           if (target_char_cast (s2, &c))
9475             return 0;
9476
9477           r = strchr (p1, c);
9478
9479           if (r == NULL)
9480             return build_int_cst (TREE_TYPE (s1), 0);
9481
9482           /* Return an offset into the constant string argument.  */
9483           tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9484                              s1, build_int_cst (TREE_TYPE (s1), r - p1));
9485           return fold_convert (type, tem);
9486         }
9487       return 0;
9488     }
9489 }
9490
9491 /* Simplify a call to the strrchr builtin.
9492
9493    Return 0 if no simplification was possible, otherwise return the
9494    simplified form of the call as a tree.
9495
9496    The simplified form may be a constant or other expression which
9497    computes the same value, but in a more efficient manner (including
9498    calls to other builtin functions).
9499
9500    The call may contain arguments which need to be evaluated, but
9501    which are not useful to determine the result of the call.  In
9502    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
9503    COMPOUND_EXPR will be an argument which must be evaluated.
9504    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
9505    COMPOUND_EXPR in the chain will contain the tree for the simplified
9506    form of the builtin function call.  */
9507
9508 static tree
9509 fold_builtin_strrchr (tree arglist, tree type)
9510 {
9511   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9512     return 0;
9513   else
9514     {
9515       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9516       tree fn;
9517       const char *p1;
9518
9519       if (TREE_CODE (s2) != INTEGER_CST)
9520         return 0;
9521
9522       p1 = c_getstr (s1);
9523       if (p1 != NULL)
9524         {
9525           char c;
9526           const char *r;
9527           tree tem;
9528
9529           if (target_char_cast (s2, &c))
9530             return 0;
9531
9532           r = strrchr (p1, c);
9533
9534           if (r == NULL)
9535             return build_int_cst (TREE_TYPE (s1), 0);
9536
9537           /* Return an offset into the constant string argument.  */
9538           tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9539                              s1, build_int_cst (TREE_TYPE (s1), r - p1));
9540           return fold_convert (type, tem);
9541         }
9542
9543       if (! integer_zerop (s2))
9544         return 0;
9545
9546       fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9547       if (!fn)
9548         return 0;
9549
9550       /* Transform strrchr(s1, '\0') to strchr(s1, '\0').  */
9551       return build_function_call_expr (fn, arglist);
9552     }
9553 }
9554
9555 /* Simplify a call to the strpbrk builtin.
9556
9557    Return 0 if no simplification was possible, otherwise return the
9558    simplified form of the call as a tree.
9559
9560    The simplified form may be a constant or other expression which
9561    computes the same value, but in a more efficient manner (including
9562    calls to other builtin functions).
9563
9564    The call may contain arguments which need to be evaluated, but
9565    which are not useful to determine the result of the call.  In
9566    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
9567    COMPOUND_EXPR will be an argument which must be evaluated.
9568    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
9569    COMPOUND_EXPR in the chain will contain the tree for the simplified
9570    form of the builtin function call.  */
9571
9572 static tree
9573 fold_builtin_strpbrk (tree arglist, tree type)
9574 {
9575   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9576     return 0;
9577   else
9578     {
9579       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9580       tree fn;
9581       const char *p1, *p2;
9582
9583       p2 = c_getstr (s2);
9584       if (p2 == NULL)
9585         return 0;
9586
9587       p1 = c_getstr (s1);
9588       if (p1 != NULL)
9589         {
9590           const char *r = strpbrk (p1, p2);
9591           tree tem;
9592
9593           if (r == NULL)
9594             return build_int_cst (TREE_TYPE (s1), 0);
9595
9596           /* Return an offset into the constant string argument.  */
9597           tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9598                              s1, build_int_cst (TREE_TYPE (s1), r - p1));
9599           return fold_convert (type, tem);
9600         }
9601
9602       if (p2[0] == '\0')
9603         /* strpbrk(x, "") == NULL.
9604            Evaluate and ignore s1 in case it had side-effects.  */
9605         return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
9606
9607       if (p2[1] != '\0')
9608         return 0;  /* Really call strpbrk.  */
9609
9610       fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9611       if (!fn)
9612         return 0;
9613
9614       /* New argument list transforming strpbrk(s1, s2) to
9615          strchr(s1, s2[0]).  */
9616       arglist = build_tree_list (NULL_TREE,
9617                                  build_int_cst (NULL_TREE, p2[0]));
9618       arglist = tree_cons (NULL_TREE, s1, arglist);
9619       return build_function_call_expr (fn, arglist);
9620     }
9621 }
9622
9623 /* Simplify a call to the strcat builtin.
9624
9625    Return 0 if no simplification was possible, otherwise return the
9626    simplified form of the call as a tree.
9627
9628    The simplified form may be a constant or other expression which
9629    computes the same value, but in a more efficient manner (including
9630    calls to other builtin functions).
9631
9632    The call may contain arguments which need to be evaluated, but
9633    which are not useful to determine the result of the call.  In
9634    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
9635    COMPOUND_EXPR will be an argument which must be evaluated.
9636    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
9637    COMPOUND_EXPR in the chain will contain the tree for the simplified
9638    form of the builtin function call.  */
9639
9640 static tree
9641 fold_builtin_strcat (tree arglist)
9642 {
9643   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9644     return 0;
9645   else
9646     {
9647       tree dst = TREE_VALUE (arglist),
9648         src = TREE_VALUE (TREE_CHAIN (arglist));
9649       const char *p = c_getstr (src);
9650
9651       /* If the string length is zero, return the dst parameter.  */
9652       if (p && *p == '\0')
9653         return dst;
9654
9655       return 0;
9656     }
9657 }
9658
9659 /* Simplify a call to the strncat builtin.
9660
9661    Return 0 if no simplification was possible, otherwise return the
9662    simplified form of the call as a tree.
9663
9664    The simplified form may be a constant or other expression which
9665    computes the same value, but in a more efficient manner (including
9666    calls to other builtin functions).
9667
9668    The call may contain arguments which need to be evaluated, but
9669    which are not useful to determine the result of the call.  In
9670    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
9671    COMPOUND_EXPR will be an argument which must be evaluated.
9672    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
9673    COMPOUND_EXPR in the chain will contain the tree for the simplified
9674    form of the builtin function call.  */
9675
9676 static tree
9677 fold_builtin_strncat (tree arglist)
9678 {
9679   if (!validate_arglist (arglist,
9680                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9681     return 0;
9682   else
9683     {
9684       tree dst = TREE_VALUE (arglist);
9685       tree src = TREE_VALUE (TREE_CHAIN (arglist));
9686       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9687       const char *p = c_getstr (src);
9688
9689       /* If the requested length is zero, or the src parameter string
9690          length is zero, return the dst parameter.  */
9691       if (integer_zerop (len) || (p && *p == '\0'))
9692         return omit_two_operands (TREE_TYPE (dst), dst, src, len);
9693
9694       /* If the requested len is greater than or equal to the string
9695          length, call strcat.  */
9696       if (TREE_CODE (len) == INTEGER_CST && p
9697           && compare_tree_int (len, strlen (p)) >= 0)
9698         {
9699           tree newarglist
9700             = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
9701           tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
9702
9703           /* If the replacement _DECL isn't initialized, don't do the
9704              transformation.  */
9705           if (!fn)
9706             return 0;
9707
9708           return build_function_call_expr (fn, newarglist);
9709         }
9710       return 0;
9711     }
9712 }
9713
9714 /* Simplify a call to the strspn builtin.
9715
9716    Return 0 if no simplification was possible, otherwise return the
9717    simplified form of the call as a tree.
9718
9719    The simplified form may be a constant or other expression which
9720    computes the same value, but in a more efficient manner (including
9721    calls to other builtin functions).
9722
9723    The call may contain arguments which need to be evaluated, but
9724    which are not useful to determine the result of the call.  In
9725    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
9726    COMPOUND_EXPR will be an argument which must be evaluated.
9727    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
9728    COMPOUND_EXPR in the chain will contain the tree for the simplified
9729    form of the builtin function call.  */
9730
9731 static tree
9732 fold_builtin_strspn (tree arglist)
9733 {
9734   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9735     return 0;
9736   else
9737     {
9738       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9739       const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9740
9741       /* If both arguments are constants, evaluate at compile-time.  */
9742       if (p1 && p2)
9743         {
9744           const size_t r = strspn (p1, p2);
9745           return size_int (r);
9746         }
9747
9748       /* If either argument is "", return 0.  */
9749       if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9750         /* Evaluate and ignore both arguments in case either one has
9751            side-effects.  */
9752         return omit_two_operands (integer_type_node, integer_zero_node,
9753                                   s1, s2);
9754       return 0;
9755     }
9756 }
9757
9758 /* Simplify a call to the strcspn builtin.
9759
9760    Return 0 if no simplification was possible, otherwise return the
9761    simplified form of the call as a tree.
9762
9763    The simplified form may be a constant or other expression which
9764    computes the same value, but in a more efficient manner (including
9765    calls to other builtin functions).
9766
9767    The call may contain arguments which need to be evaluated, but
9768    which are not useful to determine the result of the call.  In
9769    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
9770    COMPOUND_EXPR will be an argument which must be evaluated.
9771    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
9772    COMPOUND_EXPR in the chain will contain the tree for the simplified
9773    form of the builtin function call.  */
9774
9775 static tree
9776 fold_builtin_strcspn (tree arglist)
9777 {
9778   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9779     return 0;
9780   else
9781     {
9782       tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9783       const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9784
9785       /* If both arguments are constants, evaluate at compile-time.  */
9786       if (p1 && p2)
9787         {
9788           const size_t r = strcspn (p1, p2);
9789           return size_int (r);
9790         }
9791
9792       /* If the first argument is "", return 0.  */
9793       if (p1 && *p1 == '\0')
9794         {
9795           /* Evaluate and ignore argument s2 in case it has
9796              side-effects.  */
9797           return omit_one_operand (integer_type_node,
9798                                    integer_zero_node, s2);
9799         }
9800
9801       /* If the second argument is "", return __builtin_strlen(s1).  */
9802       if (p2 && *p2 == '\0')
9803         {
9804           tree newarglist = build_tree_list (NULL_TREE, s1),
9805             fn = implicit_built_in_decls[BUILT_IN_STRLEN];
9806
9807           /* If the replacement _DECL isn't initialized, don't do the
9808              transformation.  */
9809           if (!fn)
9810             return 0;
9811
9812           return build_function_call_expr (fn, newarglist);
9813         }
9814       return 0;
9815     }
9816 }
9817
9818 /* Fold a call to the fputs builtin.  IGNORE is true if the value returned
9819    by the builtin will be ignored.  UNLOCKED is true is true if this
9820    actually a call to fputs_unlocked.  If LEN in non-NULL, it represents
9821    the known length of the string.  Return NULL_TREE if no simplification
9822    was possible.  */
9823
9824 tree
9825 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
9826 {
9827   tree fn;
9828   /* If we're using an unlocked function, assume the other unlocked
9829      functions exist explicitly.  */
9830   tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
9831     : implicit_built_in_decls[BUILT_IN_FPUTC];
9832   tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
9833     : implicit_built_in_decls[BUILT_IN_FWRITE];
9834
9835   /* If the return value is used, don't do the transformation.  */
9836   if (!ignore)
9837     return 0;
9838
9839   /* Verify the arguments in the original call.  */
9840   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9841     return 0;
9842
9843   if (! len)
9844     len = c_strlen (TREE_VALUE (arglist), 0);
9845
9846   /* Get the length of the string passed to fputs.  If the length
9847      can't be determined, punt.  */
9848   if (!len
9849       || TREE_CODE (len) != INTEGER_CST)
9850     return 0;
9851
9852   switch (compare_tree_int (len, 1))
9853     {
9854     case -1: /* length is 0, delete the call entirely .  */
9855       return omit_one_operand (integer_type_node, integer_zero_node,
9856                                TREE_VALUE (TREE_CHAIN (arglist)));
9857
9858     case 0: /* length is 1, call fputc.  */
9859       {
9860         const char *p = c_getstr (TREE_VALUE (arglist));
9861
9862         if (p != NULL)
9863           {
9864             /* New argument list transforming fputs(string, stream) to
9865                fputc(string[0], stream).  */
9866             arglist = build_tree_list (NULL_TREE,
9867                                        TREE_VALUE (TREE_CHAIN (arglist)));
9868             arglist = tree_cons (NULL_TREE,
9869                                  build_int_cst (NULL_TREE, p[0]),
9870                                  arglist);
9871             fn = fn_fputc;
9872             break;
9873           }
9874       }
9875       /* FALLTHROUGH */
9876     case 1: /* length is greater than 1, call fwrite.  */
9877       {
9878         tree string_arg;
9879
9880         /* If optimizing for size keep fputs.  */
9881         if (optimize_size)
9882           return 0;
9883         string_arg = TREE_VALUE (arglist);
9884         /* New argument list transforming fputs(string, stream) to
9885            fwrite(string, 1, len, stream).  */
9886         arglist = build_tree_list (NULL_TREE,
9887                                    TREE_VALUE (TREE_CHAIN (arglist)));
9888         arglist = tree_cons (NULL_TREE, len, arglist);
9889         arglist = tree_cons (NULL_TREE, size_one_node, arglist);
9890         arglist = tree_cons (NULL_TREE, string_arg, arglist);
9891         fn = fn_fwrite;
9892         break;
9893       }
9894     default:
9895       gcc_unreachable ();
9896     }
9897
9898   /* If the replacement _DECL isn't initialized, don't do the
9899      transformation.  */
9900   if (!fn)
9901     return 0;
9902
9903   /* These optimizations are only performed when the result is ignored,
9904      hence there's no need to cast the result to integer_type_node.  */
9905   return build_function_call_expr (fn, arglist);
9906 }
9907
9908 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
9909    produced.  False otherwise.  This is done so that we don't output the error
9910    or warning twice or three times.  */
9911 bool
9912 fold_builtin_next_arg (tree arglist)
9913 {
9914   tree fntype = TREE_TYPE (current_function_decl);
9915
9916   if (TYPE_ARG_TYPES (fntype) == 0
9917       || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
9918           == void_type_node))
9919     {
9920       error ("%<va_start%> used in function with fixed args");
9921       return true;
9922     }
9923   else if (!arglist)
9924     {
9925       /* Evidently an out of date version of <stdarg.h>; can't validate
9926          va_start's second argument, but can still work as intended.  */
9927       warning (0, "%<__builtin_next_arg%> called without an argument");
9928       return true;
9929     }
9930   /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9931      when we checked the arguments and if needed issued a warning.  */
9932   else if (!TREE_CHAIN (arglist)
9933            || !integer_zerop (TREE_VALUE (arglist))
9934            || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
9935            || TREE_CHAIN (TREE_CHAIN (arglist)))
9936     {
9937       tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9938       tree arg = TREE_VALUE (arglist);
9939
9940       if (TREE_CHAIN (arglist))
9941         {
9942           error ("%<va_start%> used with too many arguments");
9943           return true;
9944         }
9945
9946       /* Strip off all nops for the sake of the comparison.  This
9947          is not quite the same as STRIP_NOPS.  It does more.
9948          We must also strip off INDIRECT_EXPR for C++ reference
9949          parameters.  */
9950       while (TREE_CODE (arg) == NOP_EXPR
9951              || TREE_CODE (arg) == CONVERT_EXPR
9952              || TREE_CODE (arg) == NON_LVALUE_EXPR
9953              || TREE_CODE (arg) == INDIRECT_REF)
9954         arg = TREE_OPERAND (arg, 0);
9955       if (arg != last_parm)
9956         {
9957           /* FIXME: Sometimes with the tree optimizers we can get the
9958              not the last argument even though the user used the last
9959              argument.  We just warn and set the arg to be the last
9960              argument so that we will get wrong-code because of
9961              it.  */
9962           warning (0, "second parameter of %<va_start%> not last named argument");
9963         }
9964       /* We want to verify the second parameter just once before the tree
9965          optimizers are run and then avoid keeping it in the tree,
9966          as otherwise we could warn even for correct code like:
9967          void foo (int i, ...)
9968          { va_list ap; i++; va_start (ap, i); va_end (ap); }  */
9969       TREE_VALUE (arglist) = integer_zero_node;
9970       TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
9971     }
9972   return false;
9973 }
9974
9975
9976 /* Simplify a call to the sprintf builtin.
9977
9978    Return 0 if no simplification was possible, otherwise return the
9979    simplified form of the call as a tree.  If IGNORED is true, it means that
9980    the caller does not use the returned value of the function.  */
9981
9982 static tree
9983 fold_builtin_sprintf (tree arglist, int ignored)
9984 {
9985   tree call, retval, dest, fmt;
9986   const char *fmt_str = NULL;
9987
9988   /* Verify the required arguments in the original call.  We deal with two
9989      types of sprintf() calls: 'sprintf (str, fmt)' and
9990      'sprintf (dest, "%s", orig)'.  */
9991   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
9992       && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
9993                             VOID_TYPE))
9994     return NULL_TREE;
9995
9996   /* Get the destination string and the format specifier.  */
9997   dest = TREE_VALUE (arglist);
9998   fmt = TREE_VALUE (TREE_CHAIN (arglist));
9999
10000   /* Check whether the format is a literal string constant.  */
10001   fmt_str = c_getstr (fmt);
10002   if (fmt_str == NULL)
10003     return NULL_TREE;
10004
10005   call = NULL_TREE;
10006   retval = NULL_TREE;
10007
10008   if (!init_target_chars())
10009     return 0;
10010
10011   /* If the format doesn't contain % args or %%, use strcpy.  */
10012   if (strchr (fmt_str, target_percent) == NULL)
10013     {
10014       tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10015
10016       if (!fn)
10017         return NULL_TREE;
10018
10019       /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
10020          'format' is known to contain no % formats.  */
10021       arglist = build_tree_list (NULL_TREE, fmt);
10022       arglist = tree_cons (NULL_TREE, dest, arglist);
10023       call = build_function_call_expr (fn, arglist);
10024       if (!ignored)
10025         retval = build_int_cst (NULL_TREE, strlen (fmt_str));
10026     }
10027
10028   /* If the format is "%s", use strcpy if the result isn't used.  */
10029   else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
10030     {
10031       tree fn, orig;
10032       fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10033
10034       if (!fn)
10035         return NULL_TREE;
10036
10037       /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2).  */
10038       orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10039       arglist = build_tree_list (NULL_TREE, orig);
10040       arglist = tree_cons (NULL_TREE, dest, arglist);
10041       if (!ignored)
10042         {
10043           retval = c_strlen (orig, 1);
10044           if (!retval || TREE_CODE (retval) != INTEGER_CST)
10045             return NULL_TREE;
10046         }
10047       call = build_function_call_expr (fn, arglist);
10048     }
10049
10050   if (call && retval)
10051     {
10052       retval = fold_convert
10053         (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
10054          retval);
10055       return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
10056     }
10057   else
10058     return call;
10059 }
10060
10061 /* Expand a call to __builtin_object_size.  */
10062
10063 rtx
10064 expand_builtin_object_size (tree exp)
10065 {
10066   tree ost;
10067   int object_size_type;
10068   tree fndecl = get_callee_fndecl (exp);
10069   tree arglist = TREE_OPERAND (exp, 1);
10070   location_t locus = EXPR_LOCATION (exp);
10071
10072   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10073     {
10074       error ("%Hfirst argument of %D must be a pointer, second integer constant",
10075              &locus, fndecl);
10076       expand_builtin_trap ();
10077       return const0_rtx;
10078     }
10079
10080   ost = TREE_VALUE (TREE_CHAIN (arglist));
10081   STRIP_NOPS (ost);
10082
10083   if (TREE_CODE (ost) != INTEGER_CST
10084       || tree_int_cst_sgn (ost) < 0
10085       || compare_tree_int (ost, 3) > 0)
10086     {
10087       error ("%Hlast argument of %D is not integer constant between 0 and 3",
10088              &locus, fndecl);
10089       expand_builtin_trap ();
10090       return const0_rtx;
10091     }
10092
10093   object_size_type = tree_low_cst (ost, 0);
10094
10095   return object_size_type < 2 ? constm1_rtx : const0_rtx;
10096 }
10097
10098 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10099    FCODE is the BUILT_IN_* to use.
10100    Return 0 if we failed; the caller should emit a normal call,
10101    otherwise try to get the result in TARGET, if convenient (and in
10102    mode MODE if that's convenient).  */
10103
10104 static rtx
10105 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
10106                            enum built_in_function fcode)
10107 {
10108   tree arglist = TREE_OPERAND (exp, 1);
10109   tree dest, src, len, size;
10110
10111   if (!validate_arglist (arglist,
10112                          POINTER_TYPE,
10113                          fcode == BUILT_IN_MEMSET_CHK
10114                          ? INTEGER_TYPE : POINTER_TYPE,
10115                          INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10116     return 0;
10117
10118   dest = TREE_VALUE (arglist);
10119   src = TREE_VALUE (TREE_CHAIN (arglist));
10120   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10121   size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10122
10123   if (! host_integerp (size, 1))
10124     return 0;
10125
10126   if (host_integerp (len, 1) || integer_all_onesp (size))
10127     {
10128       tree fn;
10129
10130       if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
10131         {
10132           location_t locus = EXPR_LOCATION (exp);
10133           warning (0, "%Hcall to %D will always overflow destination buffer",
10134                    &locus, get_callee_fndecl (exp));
10135           return 0;
10136         }
10137
10138       arglist = build_tree_list (NULL_TREE, len);
10139       arglist = tree_cons (NULL_TREE, src, arglist);
10140       arglist = tree_cons (NULL_TREE, dest, arglist);
10141
10142       fn = NULL_TREE;
10143       /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10144          mem{cpy,pcpy,move,set} is available.  */
10145       switch (fcode)
10146         {
10147         case BUILT_IN_MEMCPY_CHK:
10148           fn = built_in_decls[BUILT_IN_MEMCPY];
10149           break;
10150         case BUILT_IN_MEMPCPY_CHK:
10151           fn = built_in_decls[BUILT_IN_MEMPCPY];
10152           break;
10153         case BUILT_IN_MEMMOVE_CHK:
10154           fn = built_in_decls[BUILT_IN_MEMMOVE];
10155           break;
10156         case BUILT_IN_MEMSET_CHK:
10157           fn = built_in_decls[BUILT_IN_MEMSET];
10158           break;
10159         default:
10160           break;
10161         }
10162
10163       if (! fn)
10164         return 0;
10165
10166       fn = build_function_call_expr (fn, arglist);
10167       if (TREE_CODE (fn) == CALL_EXPR)
10168         CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10169       return expand_expr (fn, target, mode, EXPAND_NORMAL);
10170     }
10171   else if (fcode == BUILT_IN_MEMSET_CHK)
10172     return 0;
10173   else
10174     {
10175       unsigned int dest_align
10176         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
10177
10178       /* If DEST is not a pointer type, call the normal function.  */
10179       if (dest_align == 0)
10180         return 0;
10181
10182       /* If SRC and DEST are the same (and not volatile), do nothing.  */
10183       if (operand_equal_p (src, dest, 0))
10184         {
10185           tree expr;
10186
10187           if (fcode != BUILT_IN_MEMPCPY_CHK)
10188             {
10189               /* Evaluate and ignore LEN in case it has side-effects.  */
10190               expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10191               return expand_expr (dest, target, mode, EXPAND_NORMAL);
10192             }
10193
10194           len = fold_convert (TREE_TYPE (dest), len);
10195           expr = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
10196           return expand_expr (expr, target, mode, EXPAND_NORMAL);
10197         }
10198
10199       /* __memmove_chk special case.  */
10200       if (fcode == BUILT_IN_MEMMOVE_CHK)
10201         {
10202           unsigned int src_align
10203             = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
10204
10205           if (src_align == 0)
10206             return 0;
10207
10208           /* If src is categorized for a readonly section we can use
10209              normal __memcpy_chk.  */
10210           if (readonly_data_expr (src))
10211             {
10212               tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10213               if (!fn)
10214                 return 0;
10215               fn = build_function_call_expr (fn, arglist);
10216               if (TREE_CODE (fn) == CALL_EXPR)
10217                 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10218               return expand_expr (fn, target, mode, EXPAND_NORMAL);
10219             }
10220         }
10221       return 0;
10222     }
10223 }
10224
10225 /* Emit warning if a buffer overflow is detected at compile time.  */
10226
10227 static void
10228 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10229 {
10230   int arg_mask, is_strlen = 0;
10231   tree arglist = TREE_OPERAND (exp, 1), a;
10232   tree len, size;
10233   location_t locus;
10234
10235   switch (fcode)
10236     {
10237     case BUILT_IN_STRCPY_CHK:
10238     case BUILT_IN_STPCPY_CHK:
10239     /* For __strcat_chk the warning will be emitted only if overflowing
10240        by at least strlen (dest) + 1 bytes.  */
10241     case BUILT_IN_STRCAT_CHK:
10242       arg_mask = 6;
10243       is_strlen = 1;
10244       break;
10245     case BUILT_IN_STRNCAT_CHK:
10246     /* For __strncat_chk the warning will be emitted only if overflowing
10247        by at least strlen (dest) + 1 bytes.  */
10248       arg_mask = 12;
10249       break;
10250     case BUILT_IN_STRNCPY_CHK:
10251       arg_mask = 12;
10252       break;
10253     case BUILT_IN_SNPRINTF_CHK:
10254     case BUILT_IN_VSNPRINTF_CHK:
10255       arg_mask = 10;
10256       break;
10257     default:
10258       gcc_unreachable ();
10259     }
10260
10261   len = NULL_TREE;
10262   size = NULL_TREE;
10263   for (a = arglist; a && arg_mask; a = TREE_CHAIN (a), arg_mask >>= 1)
10264     if (arg_mask & 1)
10265       {
10266         if (len)
10267           size = a;
10268         else
10269           len = a;
10270       }
10271
10272   if (!len || !size)
10273     return;
10274
10275   len = TREE_VALUE (len);
10276   size = TREE_VALUE (size);
10277
10278   if (! host_integerp (size, 1) || integer_all_onesp (size))
10279     return;
10280
10281   if (is_strlen)
10282     {
10283       len = c_strlen (len, 1);
10284       if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10285         return;
10286     }
10287   else if (fcode == BUILT_IN_STRNCAT_CHK)
10288     {
10289       tree src = TREE_VALUE (TREE_CHAIN (arglist));
10290       if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10291         return;
10292       src = c_strlen (src, 1);
10293       if (! src || ! host_integerp (src, 1))
10294         {
10295           locus = EXPR_LOCATION (exp);
10296           warning (0, "%Hcall to %D might overflow destination buffer",
10297                    &locus, get_callee_fndecl (exp));
10298           return;
10299         }
10300       else if (tree_int_cst_lt (src, size))
10301         return;
10302     }
10303   else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
10304     return;
10305
10306   locus = EXPR_LOCATION (exp);
10307   warning (0, "%Hcall to %D will always overflow destination buffer",
10308            &locus, get_callee_fndecl (exp));
10309 }
10310
10311 /* Emit warning if a buffer overflow is detected at compile time
10312    in __sprintf_chk/__vsprintf_chk calls.  */
10313
10314 static void
10315 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10316 {
10317   tree arglist = TREE_OPERAND (exp, 1);
10318   tree dest, size, len, fmt, flag;
10319   const char *fmt_str;
10320
10321   /* Verify the required arguments in the original call.  */
10322   if (! arglist)
10323     return;
10324   dest = TREE_VALUE (arglist);
10325   arglist = TREE_CHAIN (arglist);
10326   if (! arglist)
10327     return;
10328   flag = TREE_VALUE (arglist);
10329   arglist = TREE_CHAIN (arglist);
10330   if (! arglist)
10331     return;
10332   size = TREE_VALUE (arglist);
10333   arglist = TREE_CHAIN (arglist);
10334   if (! arglist)
10335     return;
10336   fmt = TREE_VALUE (arglist);
10337   arglist = TREE_CHAIN (arglist);
10338
10339   if (! host_integerp (size, 1) || integer_all_onesp (size))
10340     return;
10341
10342   /* Check whether the format is a literal string constant.  */
10343   fmt_str = c_getstr (fmt);
10344   if (fmt_str == NULL)
10345     return;
10346
10347   if (!init_target_chars())
10348     return;
10349
10350   /* If the format doesn't contain % args or %%, we know its size.  */
10351   if (strchr (fmt_str, target_percent) == 0)
10352     len = build_int_cstu (size_type_node, strlen (fmt_str));
10353   /* If the format is "%s" and first ... argument is a string literal,
10354      we know it too.  */
10355   else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10356     {
10357       tree arg;
10358
10359       if (! arglist)
10360         return;
10361       arg = TREE_VALUE (arglist);
10362       if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10363         return;
10364
10365       len = c_strlen (arg, 1);
10366       if (!len || ! host_integerp (len, 1))
10367         return;
10368     }
10369   else
10370     return;
10371
10372   if (! tree_int_cst_lt (len, size))
10373     {
10374       location_t locus = EXPR_LOCATION (exp);
10375       warning (0, "%Hcall to %D will always overflow destination buffer",
10376                &locus, get_callee_fndecl (exp));
10377     }
10378 }
10379
10380 /* Fold a call to __builtin_object_size, if possible.  */
10381
10382 tree
10383 fold_builtin_object_size (tree arglist)
10384 {
10385   tree ptr, ost, ret = 0;
10386   int object_size_type;
10387
10388   if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10389     return 0;
10390
10391   ptr = TREE_VALUE (arglist);
10392   ost = TREE_VALUE (TREE_CHAIN (arglist));
10393   STRIP_NOPS (ost);
10394
10395   if (TREE_CODE (ost) != INTEGER_CST
10396       || tree_int_cst_sgn (ost) < 0
10397       || compare_tree_int (ost, 3) > 0)
10398     return 0;
10399
10400   object_size_type = tree_low_cst (ost, 0);
10401
10402   /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10403      if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10404      and (size_t) 0 for types 2 and 3.  */
10405   if (TREE_SIDE_EFFECTS (ptr))
10406     return fold_convert (size_type_node,
10407                          object_size_type < 2
10408                          ? integer_minus_one_node : integer_zero_node);
10409
10410   if (TREE_CODE (ptr) == ADDR_EXPR)
10411     ret = build_int_cstu (size_type_node,
10412                         compute_builtin_object_size (ptr, object_size_type));
10413
10414   else if (TREE_CODE (ptr) == SSA_NAME)
10415     {
10416       unsigned HOST_WIDE_INT bytes;
10417
10418       /* If object size is not known yet, delay folding until
10419        later.  Maybe subsequent passes will help determining
10420        it.  */
10421       bytes = compute_builtin_object_size (ptr, object_size_type);
10422       if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
10423                                              ? -1 : 0))
10424         ret = build_int_cstu (size_type_node, bytes);
10425     }
10426
10427   if (ret)
10428     {
10429       ret = force_fit_type (ret, -1, false, false);
10430       if (TREE_CONSTANT_OVERFLOW (ret))
10431         ret = 0;
10432     }
10433
10434   return ret;
10435 }
10436
10437 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10438    IGNORE is true, if return value can be ignored.  FCODE is the BUILT_IN_*
10439    code of the builtin.  If MAXLEN is not NULL, it is maximum length
10440    passed as third argument.  */
10441
10442 tree
10443 fold_builtin_memory_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10444                          enum built_in_function fcode)
10445 {
10446   tree dest, src, len, size, fn;
10447
10448   if (!validate_arglist (arglist,
10449                          POINTER_TYPE,
10450                          fcode == BUILT_IN_MEMSET_CHK
10451                          ? INTEGER_TYPE : POINTER_TYPE,
10452                          INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10453     return 0;
10454
10455   dest = TREE_VALUE (arglist);
10456   /* Actually val for __memset_chk, but it doesn't matter.  */
10457   src = TREE_VALUE (TREE_CHAIN (arglist));
10458   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10459   size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10460
10461   /* If SRC and DEST are the same (and not volatile), return DEST
10462      (resp. DEST+LEN for __mempcpy_chk).  */
10463   if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
10464     {
10465       if (fcode != BUILT_IN_MEMPCPY_CHK)
10466         return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10467       else
10468         {
10469           tree temp = fold_convert (TREE_TYPE (dest), len);
10470           temp = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp);
10471           return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
10472         }
10473     }
10474
10475   if (! host_integerp (size, 1))
10476     return 0;
10477
10478   if (! integer_all_onesp (size))
10479     {
10480       if (! host_integerp (len, 1))
10481         {
10482           /* If LEN is not constant, try MAXLEN too.
10483              For MAXLEN only allow optimizing into non-_ocs function
10484              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
10485           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10486             {
10487               if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
10488                 {
10489                   /* (void) __mempcpy_chk () can be optimized into
10490                      (void) __memcpy_chk ().  */
10491                   fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10492                   if (!fn)
10493                     return 0;
10494
10495                   return build_function_call_expr (fn, arglist);
10496                 }
10497               return 0;
10498             }
10499         }
10500       else
10501         maxlen = len;
10502
10503       if (tree_int_cst_lt (size, maxlen))
10504         return 0;
10505     }
10506
10507   arglist = build_tree_list (NULL_TREE, len);
10508   arglist = tree_cons (NULL_TREE, src, arglist);
10509   arglist = tree_cons (NULL_TREE, dest, arglist);
10510
10511   fn = NULL_TREE;
10512   /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10513      mem{cpy,pcpy,move,set} is available.  */
10514   switch (fcode)
10515     {
10516     case BUILT_IN_MEMCPY_CHK:
10517       fn = built_in_decls[BUILT_IN_MEMCPY];
10518       break;
10519     case BUILT_IN_MEMPCPY_CHK:
10520       fn = built_in_decls[BUILT_IN_MEMPCPY];
10521       break;
10522     case BUILT_IN_MEMMOVE_CHK:
10523       fn = built_in_decls[BUILT_IN_MEMMOVE];
10524       break;
10525     case BUILT_IN_MEMSET_CHK:
10526       fn = built_in_decls[BUILT_IN_MEMSET];
10527       break;
10528     default:
10529       break;
10530     }
10531
10532   if (!fn)
10533     return 0;
10534
10535   return build_function_call_expr (fn, arglist);
10536 }
10537
10538 /* Fold a call to the __st[rp]cpy_chk builtin.
10539    IGNORE is true, if return value can be ignored.  FCODE is the BUILT_IN_*
10540    code of the builtin.  If MAXLEN is not NULL, it is maximum length of
10541    strings passed as second argument.  */
10542
10543 tree
10544 fold_builtin_stxcpy_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10545                          enum built_in_function fcode)
10546 {
10547   tree dest, src, size, len, fn;
10548
10549   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10550                          VOID_TYPE))
10551     return 0;
10552
10553   dest = TREE_VALUE (arglist);
10554   src = TREE_VALUE (TREE_CHAIN (arglist));
10555   size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10556
10557   /* If SRC and DEST are the same (and not volatile), return DEST.  */
10558   if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
10559     return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
10560
10561   if (! host_integerp (size, 1))
10562     return 0;
10563
10564   if (! integer_all_onesp (size))
10565     {
10566       len = c_strlen (src, 1);
10567       if (! len || ! host_integerp (len, 1))
10568         {
10569           /* If LEN is not constant, try MAXLEN too.
10570              For MAXLEN only allow optimizing into non-_ocs function
10571              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
10572           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10573             {
10574               if (fcode == BUILT_IN_STPCPY_CHK)
10575                 {
10576                   if (! ignore)
10577                     return 0;
10578
10579                   /* If return value of __stpcpy_chk is ignored,
10580                      optimize into __strcpy_chk.  */
10581                   fn = built_in_decls[BUILT_IN_STRCPY_CHK];
10582                   if (!fn)
10583                     return 0;
10584
10585                   return build_function_call_expr (fn, arglist);
10586                 }
10587
10588               if (! len || TREE_SIDE_EFFECTS (len))
10589                 return 0;
10590
10591               /* If c_strlen returned something, but not a constant,
10592                  transform __strcpy_chk into __memcpy_chk.  */
10593               fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10594               if (!fn)
10595                 return 0;
10596
10597               len = size_binop (PLUS_EXPR, len, ssize_int (1));
10598               arglist = build_tree_list (NULL_TREE, size);
10599               arglist = tree_cons (NULL_TREE, len, arglist);
10600               arglist = tree_cons (NULL_TREE, src, arglist);
10601               arglist = tree_cons (NULL_TREE, dest, arglist);
10602               return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
10603                                    build_function_call_expr (fn, arglist));
10604             }
10605         }
10606       else
10607         maxlen = len;
10608
10609       if (! tree_int_cst_lt (maxlen, size))
10610         return 0;
10611     }
10612
10613   arglist = build_tree_list (NULL_TREE, src);
10614   arglist = tree_cons (NULL_TREE, dest, arglist);
10615
10616   /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available.  */
10617   fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
10618                       ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
10619   if (!fn)
10620     return 0;
10621
10622   return build_function_call_expr (fn, arglist);
10623 }
10624
10625 /* Fold a call to the __strncpy_chk builtin.
10626    If MAXLEN is not NULL, it is maximum length passed as third argument.  */
10627
10628 tree
10629 fold_builtin_strncpy_chk (tree arglist, tree maxlen)
10630 {
10631   tree dest, src, size, len, fn;
10632
10633   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10634                          INTEGER_TYPE, VOID_TYPE))
10635     return 0;
10636
10637   dest = TREE_VALUE (arglist);
10638   src = TREE_VALUE (TREE_CHAIN (arglist));
10639   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10640   size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10641
10642   if (! host_integerp (size, 1))
10643     return 0;
10644
10645   if (! integer_all_onesp (size))
10646     {
10647       if (! host_integerp (len, 1))
10648         {
10649           /* If LEN is not constant, try MAXLEN too.
10650              For MAXLEN only allow optimizing into non-_ocs function
10651              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
10652           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10653             return 0;
10654         }
10655       else
10656         maxlen = len;
10657
10658       if (tree_int_cst_lt (size, maxlen))
10659         return 0;
10660     }
10661
10662   arglist = build_tree_list (NULL_TREE, len);
10663   arglist = tree_cons (NULL_TREE, src, arglist);
10664   arglist = tree_cons (NULL_TREE, dest, arglist);
10665
10666   /* If __builtin_strncpy_chk is used, assume strncpy is available.  */
10667   fn = built_in_decls[BUILT_IN_STRNCPY];
10668   if (!fn)
10669     return 0;
10670
10671   return build_function_call_expr (fn, arglist);
10672 }
10673
10674 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST.  */
10675
10676 static tree
10677 fold_builtin_strcat_chk (tree fndecl, tree arglist)
10678 {
10679   tree dest, src, size, fn;
10680   const char *p;
10681
10682   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10683                          VOID_TYPE))
10684     return 0;
10685
10686   dest = TREE_VALUE (arglist);
10687   src = TREE_VALUE (TREE_CHAIN (arglist));
10688   size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10689
10690   p = c_getstr (src);
10691   /* If the SRC parameter is "", return DEST.  */
10692   if (p && *p == '\0')
10693     return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10694
10695   if (! host_integerp (size, 1) || ! integer_all_onesp (size))
10696     return 0;
10697
10698   arglist = build_tree_list (NULL_TREE, src);
10699   arglist = tree_cons (NULL_TREE, dest, arglist);
10700
10701   /* If __builtin_strcat_chk is used, assume strcat is available.  */
10702   fn = built_in_decls[BUILT_IN_STRCAT];
10703   if (!fn)
10704     return 0;
10705
10706   return build_function_call_expr (fn, arglist);
10707 }
10708
10709 /* Fold a call to the __strncat_chk builtin EXP.  */
10710
10711 static tree
10712 fold_builtin_strncat_chk (tree fndecl, tree arglist)
10713 {
10714   tree dest, src, size, len, fn;
10715   const char *p;
10716
10717   if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10718                          INTEGER_TYPE, VOID_TYPE))
10719     return 0;
10720
10721   dest = TREE_VALUE (arglist);
10722   src = TREE_VALUE (TREE_CHAIN (arglist));
10723   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10724   size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10725
10726   p = c_getstr (src);
10727   /* If the SRC parameter is "" or if LEN is 0, return DEST.  */
10728   if (p && *p == '\0')
10729     return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10730   else if (integer_zerop (len))
10731     return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10732
10733   if (! host_integerp (size, 1))
10734     return 0;
10735
10736   if (! integer_all_onesp (size))
10737     {
10738       tree src_len = c_strlen (src, 1);
10739       if (src_len
10740           && host_integerp (src_len, 1)
10741           && host_integerp (len, 1)
10742           && ! tree_int_cst_lt (len, src_len))
10743         {
10744           /* If LEN >= strlen (SRC), optimize into __strcat_chk.  */
10745           fn = built_in_decls[BUILT_IN_STRCAT_CHK];
10746           if (!fn)
10747             return 0;
10748
10749           arglist = build_tree_list (NULL_TREE, size);
10750           arglist = tree_cons (NULL_TREE, src, arglist);
10751           arglist = tree_cons (NULL_TREE, dest, arglist);
10752           return build_function_call_expr (fn, arglist);
10753         }
10754       return 0;
10755     }
10756
10757   arglist = build_tree_list (NULL_TREE, len);
10758   arglist = tree_cons (NULL_TREE, src, arglist);
10759   arglist = tree_cons (NULL_TREE, dest, arglist);
10760
10761   /* If __builtin_strncat_chk is used, assume strncat is available.  */
10762   fn = built_in_decls[BUILT_IN_STRNCAT];
10763   if (!fn)
10764     return 0;
10765
10766   return build_function_call_expr (fn, arglist);
10767 }
10768
10769 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST.  Return 0 if
10770    a normal call should be emitted rather than expanding the function
10771    inline.  FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK.  */
10772
10773 static tree
10774 fold_builtin_sprintf_chk (tree arglist, enum built_in_function fcode)
10775 {
10776   tree dest, size, len, fn, fmt, flag;
10777   const char *fmt_str;
10778
10779   /* Verify the required arguments in the original call.  */
10780   if (! arglist)
10781     return 0;
10782   dest = TREE_VALUE (arglist);
10783   if (! POINTER_TYPE_P (TREE_TYPE (dest)))
10784     return 0;
10785   arglist = TREE_CHAIN (arglist);
10786   if (! arglist)
10787     return 0;
10788   flag = TREE_VALUE (arglist);
10789   if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE)
10790     return 0;
10791   arglist = TREE_CHAIN (arglist);
10792   if (! arglist)
10793     return 0;
10794   size = TREE_VALUE (arglist);
10795   if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
10796     return 0;
10797   arglist = TREE_CHAIN (arglist);
10798   if (! arglist)
10799     return 0;
10800   fmt = TREE_VALUE (arglist);
10801   if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
10802     return 0;
10803   arglist = TREE_CHAIN (arglist);
10804
10805   if (! host_integerp (size, 1))
10806     return 0;
10807
10808   len = NULL_TREE;
10809
10810   if (!init_target_chars())
10811     return 0;
10812
10813   /* Check whether the format is a literal string constant.  */
10814   fmt_str = c_getstr (fmt);
10815   if (fmt_str != NULL)
10816     {
10817       /* If the format doesn't contain % args or %%, we know the size.  */
10818       if (strchr (fmt_str, target_percent) == 0)
10819         {
10820           if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE)
10821             len = build_int_cstu (size_type_node, strlen (fmt_str));
10822         }
10823       /* If the format is "%s" and first ... argument is a string literal,
10824          we know the size too.  */
10825       else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10826         {
10827           tree arg;
10828
10829           if (arglist && !TREE_CHAIN (arglist))
10830             {
10831               arg = TREE_VALUE (arglist);
10832               if (POINTER_TYPE_P (TREE_TYPE (arg)))
10833                 {
10834                   len = c_strlen (arg, 1);
10835                   if (! len || ! host_integerp (len, 1))
10836                     len = NULL_TREE;
10837                 }
10838             }
10839         }
10840     }
10841
10842   if (! integer_all_onesp (size))
10843     {
10844       if (! len || ! tree_int_cst_lt (len, size))
10845         return 0;
10846     }
10847
10848   /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
10849      or if format doesn't contain % chars or is "%s".  */
10850   if (! integer_zerop (flag))
10851     {
10852       if (fmt_str == NULL)
10853         return 0;
10854       if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
10855         return 0;
10856     }
10857
10858   arglist = tree_cons (NULL_TREE, fmt, arglist);
10859   arglist = tree_cons (NULL_TREE, dest, arglist);
10860
10861   /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available.  */
10862   fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
10863                       ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
10864   if (!fn)
10865     return 0;
10866
10867   return build_function_call_expr (fn, arglist);
10868 }
10869
10870 /* Fold a call to {,v}snprintf with argument list ARGLIST.  Return 0 if
10871    a normal call should be emitted rather than expanding the function
10872    inline.  FCODE is either BUILT_IN_SNPRINTF_CHK or
10873    BUILT_IN_VSNPRINTF_CHK.  If MAXLEN is not NULL, it is maximum length
10874    passed as second argument.  */
10875
10876 tree
10877 fold_builtin_snprintf_chk (tree arglist, tree maxlen,
10878                            enum built_in_function fcode)
10879 {
10880   tree dest, size, len, fn, fmt, flag;
10881   const char *fmt_str;
10882
10883   /* Verify the required arguments in the original call.  */
10884   if (! arglist)
10885     return 0;
10886   dest = TREE_VALUE (arglist);
10887   if (! POINTER_TYPE_P (TREE_TYPE (dest)))
10888     return 0;
10889   arglist = TREE_CHAIN (arglist);
10890   if (! arglist)
10891     return 0;
10892   len = TREE_VALUE (arglist);
10893   if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
10894     return 0;
10895   arglist = TREE_CHAIN (arglist);
10896   if (! arglist)
10897     return 0;
10898   flag = TREE_VALUE (arglist);
10899   if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
10900     return 0;
10901   arglist = TREE_CHAIN (arglist);
10902   if (! arglist)
10903     return 0;
10904   size = TREE_VALUE (arglist);
10905   if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
10906     return 0;
10907   arglist = TREE_CHAIN (arglist);
10908   if (! arglist)
10909     return 0;
10910   fmt = TREE_VALUE (arglist);
10911   if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
10912     return 0;
10913   arglist = TREE_CHAIN (arglist);
10914
10915   if (! host_integerp (size, 1))
10916     return 0;
10917
10918   if (! integer_all_onesp (size))
10919     {
10920       if (! host_integerp (len, 1))
10921         {
10922           /* If LEN is not constant, try MAXLEN too.
10923              For MAXLEN only allow optimizing into non-_ocs function
10924              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
10925           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10926             return 0;
10927         }
10928       else
10929         maxlen = len;
10930
10931       if (tree_int_cst_lt (size, maxlen))
10932         return 0;
10933     }
10934
10935   if (!init_target_chars())
10936     return 0;
10937
10938   /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
10939      or if format doesn't contain % chars or is "%s".  */
10940   if (! integer_zerop (flag))
10941     {
10942       fmt_str = c_getstr (fmt);
10943       if (fmt_str == NULL)
10944         return 0;
10945       if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
10946         return 0;
10947     }
10948
10949   arglist = tree_cons (NULL_TREE, fmt, arglist);
10950   arglist = tree_cons (NULL_TREE, len, arglist);
10951   arglist = tree_cons (NULL_TREE, dest, arglist);
10952
10953   /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
10954      available.  */
10955   fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
10956                       ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
10957   if (!fn)
10958     return 0;
10959
10960   return build_function_call_expr (fn, arglist);
10961 }
10962
10963 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
10964
10965    Return 0 if no simplification was possible, otherwise return the
10966    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
10967    code of the function to be simplified.  */
10968
10969 static tree
10970 fold_builtin_printf (tree fndecl, tree arglist, bool ignore,
10971                      enum built_in_function fcode)
10972 {
10973   tree fmt, fn = NULL_TREE, fn_putchar, fn_puts, arg, call;
10974   const char *fmt_str = NULL;
10975
10976   /* If the return value is used, don't do the transformation.  */
10977   if (! ignore)
10978     return 0;
10979
10980   /* Verify the required arguments in the original call.  */
10981   if (fcode == BUILT_IN_PRINTF_CHK || fcode == BUILT_IN_VPRINTF_CHK)
10982     {
10983       tree flag;
10984
10985       if (! arglist)
10986         return 0;
10987       flag = TREE_VALUE (arglist);
10988       if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
10989           || TREE_SIDE_EFFECTS (flag))
10990         return 0;
10991       arglist = TREE_CHAIN (arglist);
10992     }
10993
10994   if (! arglist)
10995     return 0;
10996   fmt = TREE_VALUE (arglist);
10997   if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
10998     return 0;
10999   arglist = TREE_CHAIN (arglist);
11000
11001   /* Check whether the format is a literal string constant.  */
11002   fmt_str = c_getstr (fmt);
11003   if (fmt_str == NULL)
11004     return NULL_TREE;
11005
11006   if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11007     {
11008       /* If we're using an unlocked function, assume the other
11009          unlocked functions exist explicitly.  */
11010       fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
11011       fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
11012     }
11013   else
11014     {
11015       fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
11016       fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
11017     }
11018
11019   if (!init_target_chars())
11020     return 0;
11021
11022   if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL)
11023     {
11024       const char *str;
11025
11026       if (strcmp (fmt_str, target_percent_s) == 0)
11027         {
11028           if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11029             return 0;
11030
11031           if (! arglist
11032               || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11033               || TREE_CHAIN (arglist))
11034             return 0;
11035
11036           str = c_getstr (TREE_VALUE (arglist));
11037           if (str == NULL)
11038             return 0;
11039         }
11040       else
11041         {
11042           /* The format specifier doesn't contain any '%' characters.  */
11043           if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11044               && arglist)
11045             return 0;
11046           str = fmt_str;
11047         }
11048
11049       /* If the string was "", printf does nothing.  */
11050       if (str[0] == '\0')
11051         return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11052
11053       /* If the string has length of 1, call putchar.  */
11054       if (str[1] == '\0')
11055         {
11056           /* Given printf("c"), (where c is any one character,)
11057              convert "c"[0] to an int and pass that to the replacement
11058              function.  */
11059           arg = build_int_cst (NULL_TREE, str[0]);
11060           arglist = build_tree_list (NULL_TREE, arg);
11061           fn = fn_putchar;
11062         }
11063       else
11064         {
11065           /* If the string was "string\n", call puts("string").  */
11066           size_t len = strlen (str);
11067           if ((unsigned char)str[len - 1] == target_newline)
11068             {
11069               /* Create a NUL-terminated string that's one char shorter
11070                  than the original, stripping off the trailing '\n'.  */
11071               char *newstr = alloca (len);
11072               memcpy (newstr, str, len - 1);
11073               newstr[len - 1] = 0;
11074
11075               arg = build_string_literal (len, newstr);
11076               arglist = build_tree_list (NULL_TREE, arg);
11077               fn = fn_puts;
11078             }
11079           else
11080             /* We'd like to arrange to call fputs(string,stdout) here,
11081                but we need stdout and don't have a way to get it yet.  */
11082             return 0;
11083         }
11084     }
11085
11086   /* The other optimizations can be done only on the non-va_list variants.  */
11087   else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11088     return 0;
11089
11090   /* If the format specifier was "%s\n", call __builtin_puts(arg).  */
11091   else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11092     {
11093       if (! arglist
11094           || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11095           || TREE_CHAIN (arglist))
11096         return 0;
11097       fn = fn_puts;
11098     }
11099
11100   /* If the format specifier was "%c", call __builtin_putchar(arg).  */
11101   else if (strcmp (fmt_str, target_percent_c) == 0)
11102     {
11103       if (! arglist
11104           || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11105           || TREE_CHAIN (arglist))
11106         return 0;
11107       fn = fn_putchar;
11108     }
11109
11110   if (!fn)
11111     return 0;
11112
11113   call = build_function_call_expr (fn, arglist);
11114   return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11115 }
11116
11117 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11118
11119    Return 0 if no simplification was possible, otherwise return the
11120    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
11121    code of the function to be simplified.  */
11122
11123 static tree
11124 fold_builtin_fprintf (tree fndecl, tree arglist, bool ignore,
11125                       enum built_in_function fcode)
11126 {
11127   tree fp, fmt, fn = NULL_TREE, fn_fputc, fn_fputs, arg, call;
11128   const char *fmt_str = NULL;
11129
11130   /* If the return value is used, don't do the transformation.  */
11131   if (! ignore)
11132     return 0;
11133
11134   /* Verify the required arguments in the original call.  */
11135   if (! arglist)
11136     return 0;
11137   fp = TREE_VALUE (arglist);
11138   if (! POINTER_TYPE_P (TREE_TYPE (fp)))
11139     return 0;
11140   arglist = TREE_CHAIN (arglist);
11141
11142   if (fcode == BUILT_IN_FPRINTF_CHK || fcode == BUILT_IN_VFPRINTF_CHK)
11143     {
11144       tree flag;
11145
11146       if (! arglist)
11147         return 0;
11148       flag = TREE_VALUE (arglist);
11149       if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11150           || TREE_SIDE_EFFECTS (flag))
11151         return 0;
11152       arglist = TREE_CHAIN (arglist);
11153     }
11154
11155   if (! arglist)
11156     return 0;
11157   fmt = TREE_VALUE (arglist);
11158   if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11159     return 0;
11160   arglist = TREE_CHAIN (arglist);
11161
11162   /* Check whether the format is a literal string constant.  */
11163   fmt_str = c_getstr (fmt);
11164   if (fmt_str == NULL)
11165     return NULL_TREE;
11166
11167   if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11168     {
11169       /* If we're using an unlocked function, assume the other
11170          unlocked functions exist explicitly.  */
11171       fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
11172       fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
11173     }
11174   else
11175     {
11176       fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
11177       fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
11178     }
11179
11180   if (!init_target_chars())
11181     return 0;
11182
11183   /* If the format doesn't contain % args or %%, use strcpy.  */
11184   if (strchr (fmt_str, target_percent) == NULL)
11185     {
11186       if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11187           && arglist)
11188         return 0;
11189
11190       /* If the format specifier was "", fprintf does nothing.  */
11191       if (fmt_str[0] == '\0')
11192         {
11193           /* If FP has side-effects, just wait until gimplification is
11194              done.  */
11195           if (TREE_SIDE_EFFECTS (fp))
11196             return 0;
11197
11198           return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11199         }
11200
11201       /* When "string" doesn't contain %, replace all cases of
11202          fprintf (fp, string) with fputs (string, fp).  The fputs
11203          builtin will take care of special cases like length == 1.  */
11204       arglist = build_tree_list (NULL_TREE, fp);
11205       arglist = tree_cons (NULL_TREE, fmt, arglist);
11206       fn = fn_fputs;
11207     }
11208
11209   /* The other optimizations can be done only on the non-va_list variants.  */
11210   else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11211     return 0;
11212
11213   /* If the format specifier was "%s", call __builtin_fputs (arg, fp).  */
11214   else if (strcmp (fmt_str, target_percent_s) == 0)
11215     {
11216       if (! arglist
11217           || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11218           || TREE_CHAIN (arglist))
11219         return 0;
11220       arg = TREE_VALUE (arglist);
11221       arglist = build_tree_list (NULL_TREE, fp);
11222       arglist = tree_cons (NULL_TREE, arg, arglist);
11223       fn = fn_fputs;
11224     }
11225
11226   /* If the format specifier was "%c", call __builtin_fputc (arg, fp).  */
11227   else if (strcmp (fmt_str, target_percent_c) == 0)
11228     {
11229       if (! arglist
11230           || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11231           || TREE_CHAIN (arglist))
11232         return 0;
11233       arg = TREE_VALUE (arglist);
11234       arglist = build_tree_list (NULL_TREE, fp);
11235       arglist = tree_cons (NULL_TREE, arg, arglist);
11236       fn = fn_fputc;
11237     }
11238
11239   if (!fn)
11240     return 0;
11241
11242   call = build_function_call_expr (fn, arglist);
11243   return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11244 }
11245
11246 /* Initialize format string characters in the target charset.  */
11247
11248 static bool
11249 init_target_chars (void)
11250 {
11251   static bool init;
11252   if (!init)
11253     {
11254       target_newline = lang_hooks.to_target_charset ('\n');
11255       target_percent = lang_hooks.to_target_charset ('%');
11256       target_c = lang_hooks.to_target_charset ('c');
11257       target_s = lang_hooks.to_target_charset ('s');
11258       if (target_newline == 0 || target_percent == 0 || target_c == 0
11259           || target_s == 0)
11260         return false;
11261
11262       target_percent_c[0] = target_percent;
11263       target_percent_c[1] = target_c;
11264       target_percent_c[2] = '\0';
11265
11266       target_percent_s[0] = target_percent;
11267       target_percent_s[1] = target_s;
11268       target_percent_s[2] = '\0';
11269
11270       target_percent_s_newline[0] = target_percent;
11271       target_percent_s_newline[1] = target_s;
11272       target_percent_s_newline[2] = target_newline;
11273       target_percent_s_newline[3] = '\0';
11274
11275       init = true;
11276     }
11277   return true;
11278 }
11279
11280 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11281    FUNC on it and return the resulting value as a tree with type TYPE.
11282    If MIN and/or MAX are not NULL, then the supplied ARG must be
11283    within those bounds.  If INCLUSIVE is true, then MIN/MAX are
11284    acceptable values, otherwise they are not.  The mpfr precision is
11285    set to the precision of TYPE.  We assume that function FUNC returns
11286    zero if the result could be calculated exactly within the requested
11287    precision.  */
11288
11289 static tree
11290 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11291               const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11292               bool inclusive)
11293 {
11294   tree result = NULL_TREE;
11295   
11296   STRIP_NOPS (arg);
11297
11298   if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
11299     {
11300       REAL_VALUE_TYPE r = TREE_REAL_CST (arg);
11301
11302       if (!real_isnan (&r) && !real_isinf (&r)
11303           && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , &r, min))
11304           && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , &r, max)))
11305         {
11306           const enum machine_mode mode = TYPE_MODE (type);
11307           const int prec = REAL_MODE_FORMAT (mode)->p;
11308           int inexact;
11309           mpfr_t m;
11310
11311           mpfr_init2 (m, prec);
11312           mpfr_from_real (m, &r);
11313           mpfr_clear_flags();
11314           inexact = func (m, m, GMP_RNDN);
11315
11316           /* Proceed iff we get a normal number, i.e. not NaN or Inf
11317              and no overflow/underflow occurred.  If -frounding-math,
11318              proceed iff the result of calling FUNC was exact.  */
11319           if (mpfr_number_p (m) && !mpfr_overflow_p() && !mpfr_underflow_p()
11320               && (!flag_rounding_math || !inexact))
11321             {
11322               real_from_mpfr (&r, m);
11323               /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR
11324                  value, check for overflow/underflow.  If the
11325                  REAL_VALUE_TYPE is zero but the mpft_t is not, then
11326                  we underflowed in the conversion.  */
11327               if (!real_isnan (&r) && !real_isinf (&r)
11328                   && (r.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11329                 {
11330                   REAL_VALUE_TYPE rmode;
11331                   real_convert (&rmode, mode, &r);
11332                   /* Proceed iff the specified mode can hold the value.  */
11333                   if (real_identical (&rmode, &r))
11334                     result = build_real (type, rmode);
11335                 }
11336             }
11337           mpfr_clear (m);
11338         }
11339     }
11340   
11341   return result;
11342 }