OSDN Git Service

* ifcvt.c (noce_emit_cmove): If both of the values are SUBREGs, try
[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, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "realmpfr.h"
30 #include "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 #include "tree-flow.h"
51 #include "value-prof.h"
52 #include "diagnostic-core.h"
53 #include "builtins.h"
54
55 #ifndef SLOW_UNALIGNED_ACCESS
56 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
57 #endif
58
59 #ifndef PAD_VARARGS_DOWN
60 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
61 #endif
62 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
63
64 struct target_builtins default_target_builtins;
65 #if SWITCHABLE_TARGET
66 struct target_builtins *this_target_builtins = &default_target_builtins;
67 #endif
68
69 /* Define the names of the builtin function types and codes.  */
70 const char *const built_in_class_names[4]
71   = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
72
73 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
74 const char * built_in_names[(int) END_BUILTINS] =
75 {
76 #include "builtins.def"
77 };
78 #undef DEF_BUILTIN
79
80 /* Setup an array of _DECL trees, make sure each element is
81    initialized to NULL_TREE.  */
82 tree built_in_decls[(int) END_BUILTINS];
83 /* Declarations used when constructing the builtin implicitly in the compiler.
84    It may be NULL_TREE when this is invalid (for instance runtime is not
85    required to implement the function call in all cases).  */
86 tree implicit_built_in_decls[(int) END_BUILTINS];
87
88 static const char *c_getstr (tree);
89 static rtx c_readstr (const char *, enum machine_mode);
90 static int target_char_cast (tree, char *);
91 static rtx get_memory_rtx (tree, tree);
92 static int apply_args_size (void);
93 static int apply_result_size (void);
94 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
95 static rtx result_vector (int, rtx);
96 #endif
97 static void expand_builtin_update_setjmp_buf (rtx);
98 static void expand_builtin_prefetch (tree);
99 static rtx expand_builtin_apply_args (void);
100 static rtx expand_builtin_apply_args_1 (void);
101 static rtx expand_builtin_apply (rtx, rtx, rtx);
102 static void expand_builtin_return (rtx);
103 static enum type_class type_to_class (tree);
104 static rtx expand_builtin_classify_type (tree);
105 static void expand_errno_check (tree, rtx);
106 static rtx expand_builtin_mathfn (tree, rtx, rtx);
107 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
108 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
109 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
110 static rtx expand_builtin_interclass_mathfn (tree, rtx);
111 static rtx expand_builtin_sincos (tree);
112 static rtx expand_builtin_cexpi (tree, rtx);
113 static rtx expand_builtin_int_roundingfn (tree, rtx);
114 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
115 static rtx expand_builtin_next_arg (void);
116 static rtx expand_builtin_va_start (tree);
117 static rtx expand_builtin_va_end (tree);
118 static rtx expand_builtin_va_copy (tree);
119 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
120 static rtx expand_builtin_strcmp (tree, rtx);
121 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
122 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx expand_builtin_memcpy (tree, rtx);
124 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
126                                         enum machine_mode, int);
127 static rtx expand_builtin_strcpy (tree, rtx);
128 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
129 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
130 static rtx expand_builtin_strncpy (tree, rtx);
131 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
132 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
133 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
134 static rtx expand_builtin_bzero (tree);
135 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
136 static rtx expand_builtin_alloca (tree, bool);
137 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
138 static rtx expand_builtin_frame_address (tree, tree);
139 static tree stabilize_va_list_loc (location_t, tree, int);
140 static rtx expand_builtin_expect (tree, rtx);
141 static tree fold_builtin_constant_p (tree);
142 static tree fold_builtin_expect (location_t, tree, tree);
143 static tree fold_builtin_classify_type (tree);
144 static tree fold_builtin_strlen (location_t, tree, tree);
145 static tree fold_builtin_inf (location_t, tree, int);
146 static tree fold_builtin_nan (tree, tree, int);
147 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
148 static bool validate_arg (const_tree, enum tree_code code);
149 static bool integer_valued_real_p (tree);
150 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
151 static bool readonly_data_expr (tree);
152 static rtx expand_builtin_fabs (tree, rtx, rtx);
153 static rtx expand_builtin_signbit (tree, rtx);
154 static tree fold_builtin_sqrt (location_t, tree, tree);
155 static tree fold_builtin_cbrt (location_t, tree, tree);
156 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
157 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
158 static tree fold_builtin_cos (location_t, tree, tree, tree);
159 static tree fold_builtin_cosh (location_t, tree, tree, tree);
160 static tree fold_builtin_tan (tree, tree);
161 static tree fold_builtin_trunc (location_t, tree, tree);
162 static tree fold_builtin_floor (location_t, tree, tree);
163 static tree fold_builtin_ceil (location_t, tree, tree);
164 static tree fold_builtin_round (location_t, tree, tree);
165 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
166 static tree fold_builtin_bitop (tree, tree);
167 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
168 static tree fold_builtin_strchr (location_t, tree, tree, tree);
169 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
170 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
171 static tree fold_builtin_strcmp (location_t, tree, tree);
172 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
173 static tree fold_builtin_signbit (location_t, tree, tree);
174 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
175 static tree fold_builtin_isascii (location_t, tree);
176 static tree fold_builtin_toascii (location_t, tree);
177 static tree fold_builtin_isdigit (location_t, tree);
178 static tree fold_builtin_fabs (location_t, tree, tree);
179 static tree fold_builtin_abs (location_t, tree, tree);
180 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
181                                         enum tree_code);
182 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
183 static tree fold_builtin_0 (location_t, tree, bool);
184 static tree fold_builtin_1 (location_t, tree, tree, bool);
185 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
186 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
187 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
188 static tree fold_builtin_varargs (location_t, tree, tree, bool);
189
190 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
191 static tree fold_builtin_strstr (location_t, tree, tree, tree);
192 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
193 static tree fold_builtin_strcat (location_t, tree, tree);
194 static tree fold_builtin_strncat (location_t, tree, tree, tree);
195 static tree fold_builtin_strspn (location_t, tree, tree);
196 static tree fold_builtin_strcspn (location_t, tree, tree);
197 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
198
199 static rtx expand_builtin_object_size (tree);
200 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
201                                       enum built_in_function);
202 static void maybe_emit_chk_warning (tree, enum built_in_function);
203 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
204 static void maybe_emit_free_warning (tree);
205 static tree fold_builtin_object_size (tree, tree);
206 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
207 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
208 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
209 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
210 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
211                                   enum built_in_function);
212 static bool init_target_chars (void);
213
214 static unsigned HOST_WIDE_INT target_newline;
215 static unsigned HOST_WIDE_INT target_percent;
216 static unsigned HOST_WIDE_INT target_c;
217 static unsigned HOST_WIDE_INT target_s;
218 static char target_percent_c[3];
219 static char target_percent_s[3];
220 static char target_percent_s_newline[4];
221 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
222                           const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
223 static tree do_mpfr_arg2 (tree, tree, tree,
224                           int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
225 static tree do_mpfr_arg3 (tree, tree, tree, tree,
226                           int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
227 static tree do_mpfr_sincos (tree, tree, tree);
228 static tree do_mpfr_bessel_n (tree, tree, tree,
229                               int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
230                               const REAL_VALUE_TYPE *, bool);
231 static tree do_mpfr_remquo (tree, tree, tree);
232 static tree do_mpfr_lgamma_r (tree, tree, tree);
233
234 /* Return true if NAME starts with __builtin_ or __sync_.  */
235
236 bool
237 is_builtin_name (const char *name)
238 {
239   if (strncmp (name, "__builtin_", 10) == 0)
240     return true;
241   if (strncmp (name, "__sync_", 7) == 0)
242     return true;
243   return false;
244 }
245
246
247 /* Return true if DECL is a function symbol representing a built-in.  */
248
249 bool
250 is_builtin_fn (tree decl)
251 {
252   return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
253 }
254
255
256 /* Return true if NODE should be considered for inline expansion regardless
257    of the optimization level.  This means whenever a function is invoked with
258    its "internal" name, which normally contains the prefix "__builtin".  */
259
260 static bool
261 called_as_built_in (tree node)
262 {
263   /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
264      we want the name used to call the function, not the name it
265      will have. */
266   const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
267   return is_builtin_name (name);
268 }
269
270 /* Return the alignment in bits of EXP, an object.
271    Don't return more than MAX_ALIGN no matter what.  */
272
273 unsigned int
274 get_object_alignment (tree exp, unsigned int max_align)
275 {
276   HOST_WIDE_INT bitsize, bitpos;
277   tree offset;
278   enum machine_mode mode;
279   int unsignedp, volatilep;
280   unsigned int align, inner;
281
282   /* Get the innermost object and the constant (bitpos) and possibly
283      variable (offset) offset of the access.  */
284   exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
285                              &mode, &unsignedp, &volatilep, true);
286
287   /* Extract alignment information from the innermost object and
288      possibly adjust bitpos and offset.  */
289   if (TREE_CODE (exp) == CONST_DECL)
290     exp = DECL_INITIAL (exp);
291   if (DECL_P (exp)
292       && TREE_CODE (exp) != LABEL_DECL)
293     align = DECL_ALIGN (exp);
294   else if (CONSTANT_CLASS_P (exp))
295     {
296       align = TYPE_ALIGN (TREE_TYPE (exp));
297 #ifdef CONSTANT_ALIGNMENT
298       align = (unsigned)CONSTANT_ALIGNMENT (exp, align);
299 #endif
300     }
301   else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
302     align = TYPE_ALIGN (TREE_TYPE (exp));
303   else if (TREE_CODE (exp) == INDIRECT_REF)
304     align = TYPE_ALIGN (TREE_TYPE (exp));
305   else if (TREE_CODE (exp) == MEM_REF)
306     {
307       tree addr = TREE_OPERAND (exp, 0);
308       struct ptr_info_def *pi;
309       if (TREE_CODE (addr) == BIT_AND_EXPR
310           && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
311         {
312           align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
313                     & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
314           align *= BITS_PER_UNIT;
315           addr = TREE_OPERAND (addr, 0);
316         }
317       else
318         align = BITS_PER_UNIT;
319       if (TREE_CODE (addr) == SSA_NAME
320           && (pi = SSA_NAME_PTR_INFO (addr)))
321         {
322           bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
323           align = MAX (pi->align * BITS_PER_UNIT, align);
324         }
325       else if (TREE_CODE (addr) == ADDR_EXPR)
326         align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0),
327                                                   max_align));
328       bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
329     }
330   else if (TREE_CODE (exp) == TARGET_MEM_REF)
331     {
332       struct ptr_info_def *pi;
333       tree addr = TMR_BASE (exp);
334       if (TREE_CODE (addr) == BIT_AND_EXPR
335           && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
336         {
337           align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
338                    & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
339           align *= BITS_PER_UNIT;
340           addr = TREE_OPERAND (addr, 0);
341         }
342       else
343         align = BITS_PER_UNIT;
344       if (TREE_CODE (addr) == SSA_NAME
345           && (pi = SSA_NAME_PTR_INFO (addr)))
346         {
347           bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
348           align = MAX (pi->align * BITS_PER_UNIT, align);
349         }
350       else if (TREE_CODE (addr) == ADDR_EXPR)
351         align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0),
352                                                   max_align));
353       if (TMR_OFFSET (exp))
354         bitpos += TREE_INT_CST_LOW (TMR_OFFSET (exp)) * BITS_PER_UNIT;
355       if (TMR_INDEX (exp) && TMR_STEP (exp))
356         {
357           unsigned HOST_WIDE_INT step = TREE_INT_CST_LOW (TMR_STEP (exp));
358           align = MIN (align, (step & -step) * BITS_PER_UNIT);
359         }
360       else if (TMR_INDEX (exp))
361         align = BITS_PER_UNIT;
362       if (TMR_INDEX2 (exp))
363         align = BITS_PER_UNIT;
364     }
365   else
366     align = BITS_PER_UNIT;
367
368   /* If there is a non-constant offset part extract the maximum
369      alignment that can prevail.  */
370   inner = max_align;
371   while (offset)
372     {
373       tree next_offset;
374
375       if (TREE_CODE (offset) == PLUS_EXPR)
376         {
377           next_offset = TREE_OPERAND (offset, 0);
378           offset = TREE_OPERAND (offset, 1);
379         }
380       else
381         next_offset = NULL;
382       if (host_integerp (offset, 1))
383         {
384           /* Any overflow in calculating offset_bits won't change
385              the alignment.  */
386           unsigned offset_bits
387             = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
388
389           if (offset_bits)
390             inner = MIN (inner, (offset_bits & -offset_bits));
391         }
392       else if (TREE_CODE (offset) == MULT_EXPR
393                && host_integerp (TREE_OPERAND (offset, 1), 1))
394         {
395           /* Any overflow in calculating offset_factor won't change
396              the alignment.  */
397           unsigned offset_factor
398             = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
399                * BITS_PER_UNIT);
400
401           if (offset_factor)
402             inner = MIN (inner, (offset_factor & -offset_factor));
403         }
404       else
405         {
406           inner = MIN (inner, BITS_PER_UNIT);
407           break;
408         }
409       offset = next_offset;
410     }
411
412   /* Alignment is innermost object alignment adjusted by the constant
413      and non-constant offset parts.  */
414   align = MIN (align, inner);
415   bitpos = bitpos & (align - 1);
416
417   /* align and bitpos now specify known low bits of the pointer.
418      ptr & (align - 1) == bitpos.  */
419
420   if (bitpos != 0)
421     align = (bitpos & -bitpos);
422
423   return MIN (align, max_align);
424 }
425
426 /* Returns true iff we can trust that alignment information has been
427    calculated properly.  */
428
429 bool
430 can_trust_pointer_alignment (void)
431 {
432   /* We rely on TER to compute accurate alignment information.  */
433   return (optimize && flag_tree_ter);
434 }
435
436 /* Return the alignment in bits of EXP, a pointer valued expression.
437    But don't return more than MAX_ALIGN no matter what.
438    The alignment returned is, by default, the alignment of the thing that
439    EXP points to.  If it is not a POINTER_TYPE, 0 is returned.
440
441    Otherwise, look at the expression to see if we can do better, i.e., if the
442    expression is actually pointing at an object whose alignment is tighter.  */
443
444 unsigned int
445 get_pointer_alignment (tree exp, unsigned int max_align)
446 {
447   STRIP_NOPS (exp);
448
449   if (TREE_CODE (exp) == ADDR_EXPR)
450     return get_object_alignment (TREE_OPERAND (exp, 0), max_align);
451   else if (TREE_CODE (exp) == SSA_NAME
452            && POINTER_TYPE_P (TREE_TYPE (exp)))
453     {
454       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
455       unsigned align;
456       if (!pi)
457         return BITS_PER_UNIT;
458       if (pi->misalign != 0)
459         align = (pi->misalign & -pi->misalign);
460       else
461         align = pi->align;
462       return MIN (max_align, align * BITS_PER_UNIT);
463     }
464
465   return POINTER_TYPE_P (TREE_TYPE (exp)) ? BITS_PER_UNIT : 0;
466 }
467
468 /* Compute the length of a C string.  TREE_STRING_LENGTH is not the right
469    way, because it could contain a zero byte in the middle.
470    TREE_STRING_LENGTH is the size of the character array, not the string.
471
472    ONLY_VALUE should be nonzero if the result is not going to be emitted
473    into the instruction stream and zero if it is going to be expanded.
474    E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
475    is returned, otherwise NULL, since
476    len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
477    evaluate the side-effects.
478
479    The value returned is of type `ssizetype'.
480
481    Unfortunately, string_constant can't access the values of const char
482    arrays with initializers, so neither can we do so here.  */
483
484 tree
485 c_strlen (tree src, int only_value)
486 {
487   tree offset_node;
488   HOST_WIDE_INT offset;
489   int max;
490   const char *ptr;
491   location_t loc;
492
493   STRIP_NOPS (src);
494   if (TREE_CODE (src) == COND_EXPR
495       && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
496     {
497       tree len1, len2;
498
499       len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
500       len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
501       if (tree_int_cst_equal (len1, len2))
502         return len1;
503     }
504
505   if (TREE_CODE (src) == COMPOUND_EXPR
506       && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
507     return c_strlen (TREE_OPERAND (src, 1), only_value);
508
509   if (EXPR_HAS_LOCATION (src))
510     loc = EXPR_LOCATION (src);
511   else
512     loc = input_location;
513
514   src = string_constant (src, &offset_node);
515   if (src == 0)
516     return NULL_TREE;
517
518   max = TREE_STRING_LENGTH (src) - 1;
519   ptr = TREE_STRING_POINTER (src);
520
521   if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
522     {
523       /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
524          compute the offset to the following null if we don't know where to
525          start searching for it.  */
526       int i;
527
528       for (i = 0; i < max; i++)
529         if (ptr[i] == 0)
530           return NULL_TREE;
531
532       /* We don't know the starting offset, but we do know that the string
533          has no internal zero bytes.  We can assume that the offset falls
534          within the bounds of the string; otherwise, the programmer deserves
535          what he gets.  Subtract the offset from the length of the string,
536          and return that.  This would perhaps not be valid if we were dealing
537          with named arrays in addition to literal string constants.  */
538
539       return size_diffop_loc (loc, size_int (max), offset_node);
540     }
541
542   /* We have a known offset into the string.  Start searching there for
543      a null character if we can represent it as a single HOST_WIDE_INT.  */
544   if (offset_node == 0)
545     offset = 0;
546   else if (! host_integerp (offset_node, 0))
547     offset = -1;
548   else
549     offset = tree_low_cst (offset_node, 0);
550
551   /* If the offset is known to be out of bounds, warn, and call strlen at
552      runtime.  */
553   if (offset < 0 || offset > max)
554     {
555      /* Suppress multiple warnings for propagated constant strings.  */
556       if (! TREE_NO_WARNING (src))
557         {
558           warning_at (loc, 0, "offset outside bounds of constant string");
559           TREE_NO_WARNING (src) = 1;
560         }
561       return NULL_TREE;
562     }
563
564   /* Use strlen to search for the first zero byte.  Since any strings
565      constructed with build_string will have nulls appended, we win even
566      if we get handed something like (char[4])"abcd".
567
568      Since OFFSET is our starting index into the string, no further
569      calculation is needed.  */
570   return ssize_int (strlen (ptr + offset));
571 }
572
573 /* Return a char pointer for a C string if it is a string constant
574    or sum of string constant and integer constant.  */
575
576 static const char *
577 c_getstr (tree src)
578 {
579   tree offset_node;
580
581   src = string_constant (src, &offset_node);
582   if (src == 0)
583     return 0;
584
585   if (offset_node == 0)
586     return TREE_STRING_POINTER (src);
587   else if (!host_integerp (offset_node, 1)
588            || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
589     return 0;
590
591   return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
592 }
593
594 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
595    GET_MODE_BITSIZE (MODE) bits from string constant STR.  */
596
597 static rtx
598 c_readstr (const char *str, enum machine_mode mode)
599 {
600   HOST_WIDE_INT c[2];
601   HOST_WIDE_INT ch;
602   unsigned int i, j;
603
604   gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
605
606   c[0] = 0;
607   c[1] = 0;
608   ch = 1;
609   for (i = 0; i < GET_MODE_SIZE (mode); i++)
610     {
611       j = i;
612       if (WORDS_BIG_ENDIAN)
613         j = GET_MODE_SIZE (mode) - i - 1;
614       if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
615           && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
616         j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
617       j *= BITS_PER_UNIT;
618       gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
619
620       if (ch)
621         ch = (unsigned char) str[i];
622       c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
623     }
624   return immed_double_const (c[0], c[1], mode);
625 }
626
627 /* Cast a target constant CST to target CHAR and if that value fits into
628    host char type, return zero and put that value into variable pointed to by
629    P.  */
630
631 static int
632 target_char_cast (tree cst, char *p)
633 {
634   unsigned HOST_WIDE_INT val, hostval;
635
636   if (!host_integerp (cst, 1)
637       || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
638     return 1;
639
640   val = tree_low_cst (cst, 1);
641   if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
642     val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
643
644   hostval = val;
645   if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
646     hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
647
648   if (val != hostval)
649     return 1;
650
651   *p = hostval;
652   return 0;
653 }
654
655 /* Similar to save_expr, but assumes that arbitrary code is not executed
656    in between the multiple evaluations.  In particular, we assume that a
657    non-addressable local variable will not be modified.  */
658
659 static tree
660 builtin_save_expr (tree exp)
661 {
662   if (TREE_ADDRESSABLE (exp) == 0
663       && (TREE_CODE (exp) == PARM_DECL
664           || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
665     return exp;
666
667   return save_expr (exp);
668 }
669
670 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
671    times to get the address of either a higher stack frame, or a return
672    address located within it (depending on FNDECL_CODE).  */
673
674 static rtx
675 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
676 {
677   int i;
678
679 #ifdef INITIAL_FRAME_ADDRESS_RTX
680   rtx tem = INITIAL_FRAME_ADDRESS_RTX;
681 #else
682   rtx tem;
683
684   /* For a zero count with __builtin_return_address, we don't care what
685      frame address we return, because target-specific definitions will
686      override us.  Therefore frame pointer elimination is OK, and using
687      the soft frame pointer is OK.
688
689      For a nonzero count, or a zero count with __builtin_frame_address,
690      we require a stable offset from the current frame pointer to the
691      previous one, so we must use the hard frame pointer, and
692      we must disable frame pointer elimination.  */
693   if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
694     tem = frame_pointer_rtx;
695   else
696     {
697       tem = hard_frame_pointer_rtx;
698
699       /* Tell reload not to eliminate the frame pointer.  */
700       crtl->accesses_prior_frames = 1;
701     }
702 #endif
703
704   /* Some machines need special handling before we can access
705      arbitrary frames.  For example, on the SPARC, we must first flush
706      all register windows to the stack.  */
707 #ifdef SETUP_FRAME_ADDRESSES
708   if (count > 0)
709     SETUP_FRAME_ADDRESSES ();
710 #endif
711
712   /* On the SPARC, the return address is not in the frame, it is in a
713      register.  There is no way to access it off of the current frame
714      pointer, but it can be accessed off the previous frame pointer by
715      reading the value from the register window save area.  */
716 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
717   if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
718     count--;
719 #endif
720
721   /* Scan back COUNT frames to the specified frame.  */
722   for (i = 0; i < count; i++)
723     {
724       /* Assume the dynamic chain pointer is in the word that the
725          frame address points to, unless otherwise specified.  */
726 #ifdef DYNAMIC_CHAIN_ADDRESS
727       tem = DYNAMIC_CHAIN_ADDRESS (tem);
728 #endif
729       tem = memory_address (Pmode, tem);
730       tem = gen_frame_mem (Pmode, tem);
731       tem = copy_to_reg (tem);
732     }
733
734   /* For __builtin_frame_address, return what we've got.  But, on
735      the SPARC for example, we may have to add a bias.  */
736   if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
737 #ifdef FRAME_ADDR_RTX
738     return FRAME_ADDR_RTX (tem);
739 #else
740     return tem;
741 #endif
742
743   /* For __builtin_return_address, get the return address from that frame.  */
744 #ifdef RETURN_ADDR_RTX
745   tem = RETURN_ADDR_RTX (count, tem);
746 #else
747   tem = memory_address (Pmode,
748                         plus_constant (tem, GET_MODE_SIZE (Pmode)));
749   tem = gen_frame_mem (Pmode, tem);
750 #endif
751   return tem;
752 }
753
754 /* Alias set used for setjmp buffer.  */
755 static alias_set_type setjmp_alias_set = -1;
756
757 /* Construct the leading half of a __builtin_setjmp call.  Control will
758    return to RECEIVER_LABEL.  This is also called directly by the SJLJ
759    exception handling code.  */
760
761 void
762 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
763 {
764   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
765   rtx stack_save;
766   rtx mem;
767
768   if (setjmp_alias_set == -1)
769     setjmp_alias_set = new_alias_set ();
770
771   buf_addr = convert_memory_address (Pmode, buf_addr);
772
773   buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
774
775   /* We store the frame pointer and the address of receiver_label in
776      the buffer and use the rest of it for the stack save area, which
777      is machine-dependent.  */
778
779   mem = gen_rtx_MEM (Pmode, buf_addr);
780   set_mem_alias_set (mem, setjmp_alias_set);
781   emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
782
783   mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
784   set_mem_alias_set (mem, setjmp_alias_set);
785
786   emit_move_insn (validize_mem (mem),
787                   force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
788
789   stack_save = gen_rtx_MEM (sa_mode,
790                             plus_constant (buf_addr,
791                                            2 * GET_MODE_SIZE (Pmode)));
792   set_mem_alias_set (stack_save, setjmp_alias_set);
793   emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
794
795   /* If there is further processing to do, do it.  */
796 #ifdef HAVE_builtin_setjmp_setup
797   if (HAVE_builtin_setjmp_setup)
798     emit_insn (gen_builtin_setjmp_setup (buf_addr));
799 #endif
800
801   /* Tell optimize_save_area_alloca that extra work is going to
802      need to go on during alloca.  */
803   cfun->calls_setjmp = 1;
804
805   /* We have a nonlocal label.   */
806   cfun->has_nonlocal_label = 1;
807 }
808
809 /* Construct the trailing part of a __builtin_setjmp call.  This is
810    also called directly by the SJLJ exception handling code.  */
811
812 void
813 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
814 {
815   rtx chain;
816
817   /* Clobber the FP when we get here, so we have to make sure it's
818      marked as used by this function.  */
819   emit_use (hard_frame_pointer_rtx);
820
821   /* Mark the static chain as clobbered here so life information
822      doesn't get messed up for it.  */
823   chain = targetm.calls.static_chain (current_function_decl, true);
824   if (chain && REG_P (chain))
825     emit_clobber (chain);
826
827   /* Now put in the code to restore the frame pointer, and argument
828      pointer, if needed.  */
829 #ifdef HAVE_nonlocal_goto
830   if (! HAVE_nonlocal_goto)
831 #endif
832     {
833       emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
834       /* This might change the hard frame pointer in ways that aren't
835          apparent to early optimization passes, so force a clobber.  */
836       emit_clobber (hard_frame_pointer_rtx);
837     }
838
839 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
840   if (fixed_regs[ARG_POINTER_REGNUM])
841     {
842 #ifdef ELIMINABLE_REGS
843       size_t i;
844       static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
845
846       for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
847         if (elim_regs[i].from == ARG_POINTER_REGNUM
848             && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
849           break;
850
851       if (i == ARRAY_SIZE (elim_regs))
852 #endif
853         {
854           /* Now restore our arg pointer from the address at which it
855              was saved in our stack frame.  */
856           emit_move_insn (crtl->args.internal_arg_pointer,
857                           copy_to_reg (get_arg_pointer_save_area ()));
858         }
859     }
860 #endif
861
862 #ifdef HAVE_builtin_setjmp_receiver
863   if (HAVE_builtin_setjmp_receiver)
864     emit_insn (gen_builtin_setjmp_receiver (receiver_label));
865   else
866 #endif
867 #ifdef HAVE_nonlocal_goto_receiver
868     if (HAVE_nonlocal_goto_receiver)
869       emit_insn (gen_nonlocal_goto_receiver ());
870     else
871 #endif
872       { /* Nothing */ }
873
874   /* We must not allow the code we just generated to be reordered by
875      scheduling.  Specifically, the update of the frame pointer must
876      happen immediately, not later.  */
877   emit_insn (gen_blockage ());
878 }
879
880 /* __builtin_longjmp is passed a pointer to an array of five words (not
881    all will be used on all machines).  It operates similarly to the C
882    library function of the same name, but is more efficient.  Much of
883    the code below is copied from the handling of non-local gotos.  */
884
885 static void
886 expand_builtin_longjmp (rtx buf_addr, rtx value)
887 {
888   rtx fp, lab, stack, insn, last;
889   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
890
891   /* DRAP is needed for stack realign if longjmp is expanded to current
892      function  */
893   if (SUPPORTS_STACK_ALIGNMENT)
894     crtl->need_drap = true;
895
896   if (setjmp_alias_set == -1)
897     setjmp_alias_set = new_alias_set ();
898
899   buf_addr = convert_memory_address (Pmode, buf_addr);
900
901   buf_addr = force_reg (Pmode, buf_addr);
902
903   /* We require that the user must pass a second argument of 1, because
904      that is what builtin_setjmp will return.  */
905   gcc_assert (value == const1_rtx);
906
907   last = get_last_insn ();
908 #ifdef HAVE_builtin_longjmp
909   if (HAVE_builtin_longjmp)
910     emit_insn (gen_builtin_longjmp (buf_addr));
911   else
912 #endif
913     {
914       fp = gen_rtx_MEM (Pmode, buf_addr);
915       lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
916                                                GET_MODE_SIZE (Pmode)));
917
918       stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
919                                                    2 * GET_MODE_SIZE (Pmode)));
920       set_mem_alias_set (fp, setjmp_alias_set);
921       set_mem_alias_set (lab, setjmp_alias_set);
922       set_mem_alias_set (stack, setjmp_alias_set);
923
924       /* Pick up FP, label, and SP from the block and jump.  This code is
925          from expand_goto in stmt.c; see there for detailed comments.  */
926 #ifdef HAVE_nonlocal_goto
927       if (HAVE_nonlocal_goto)
928         /* We have to pass a value to the nonlocal_goto pattern that will
929            get copied into the static_chain pointer, but it does not matter
930            what that value is, because builtin_setjmp does not use it.  */
931         emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
932       else
933 #endif
934         {
935           lab = copy_to_reg (lab);
936
937           emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
938           emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
939
940           emit_move_insn (hard_frame_pointer_rtx, fp);
941           emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
942
943           emit_use (hard_frame_pointer_rtx);
944           emit_use (stack_pointer_rtx);
945           emit_indirect_jump (lab);
946         }
947     }
948
949   /* Search backwards and mark the jump insn as a non-local goto.
950      Note that this precludes the use of __builtin_longjmp to a
951      __builtin_setjmp target in the same function.  However, we've
952      already cautioned the user that these functions are for
953      internal exception handling use only.  */
954   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
955     {
956       gcc_assert (insn != last);
957
958       if (JUMP_P (insn))
959         {
960           add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
961           break;
962         }
963       else if (CALL_P (insn))
964         break;
965     }
966 }
967
968 /* Expand a call to __builtin_nonlocal_goto.  We're passed the target label
969    and the address of the save area.  */
970
971 static rtx
972 expand_builtin_nonlocal_goto (tree exp)
973 {
974   tree t_label, t_save_area;
975   rtx r_label, r_save_area, r_fp, r_sp, insn;
976
977   if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
978     return NULL_RTX;
979
980   t_label = CALL_EXPR_ARG (exp, 0);
981   t_save_area = CALL_EXPR_ARG (exp, 1);
982
983   r_label = expand_normal (t_label);
984   r_label = convert_memory_address (Pmode, r_label);
985   r_save_area = expand_normal (t_save_area);
986   r_save_area = convert_memory_address (Pmode, r_save_area);
987   /* Copy the address of the save location to a register just in case it was based
988     on the frame pointer.   */
989   r_save_area = copy_to_reg (r_save_area);
990   r_fp = gen_rtx_MEM (Pmode, r_save_area);
991   r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
992                       plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
993
994   crtl->has_nonlocal_goto = 1;
995
996 #ifdef HAVE_nonlocal_goto
997   /* ??? We no longer need to pass the static chain value, afaik.  */
998   if (HAVE_nonlocal_goto)
999     emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1000   else
1001 #endif
1002     {
1003       r_label = copy_to_reg (r_label);
1004
1005       emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1006       emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1007
1008       /* Restore frame pointer for containing function.
1009          This sets the actual hard register used for the frame pointer
1010          to the location of the function's incoming static chain info.
1011          The non-local goto handler will then adjust it to contain the
1012          proper value and reload the argument pointer, if needed.  */
1013       emit_move_insn (hard_frame_pointer_rtx, r_fp);
1014       emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
1015
1016       /* USE of hard_frame_pointer_rtx added for consistency;
1017          not clear if really needed.  */
1018       emit_use (hard_frame_pointer_rtx);
1019       emit_use (stack_pointer_rtx);
1020
1021       /* If the architecture is using a GP register, we must
1022          conservatively assume that the target function makes use of it.
1023          The prologue of functions with nonlocal gotos must therefore
1024          initialize the GP register to the appropriate value, and we
1025          must then make sure that this value is live at the point
1026          of the jump.  (Note that this doesn't necessarily apply
1027          to targets with a nonlocal_goto pattern; they are free
1028          to implement it in their own way.  Note also that this is
1029          a no-op if the GP register is a global invariant.)  */
1030       if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1031           && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1032         emit_use (pic_offset_table_rtx);
1033
1034       emit_indirect_jump (r_label);
1035     }
1036
1037   /* Search backwards to the jump insn and mark it as a
1038      non-local goto.  */
1039   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1040     {
1041       if (JUMP_P (insn))
1042         {
1043           add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1044           break;
1045         }
1046       else if (CALL_P (insn))
1047         break;
1048     }
1049
1050   return const0_rtx;
1051 }
1052
1053 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1054    (not all will be used on all machines) that was passed to __builtin_setjmp.
1055    It updates the stack pointer in that block to correspond to the current
1056    stack pointer.  */
1057
1058 static void
1059 expand_builtin_update_setjmp_buf (rtx buf_addr)
1060 {
1061   enum machine_mode sa_mode = Pmode;
1062   rtx stack_save;
1063
1064
1065 #ifdef HAVE_save_stack_nonlocal
1066   if (HAVE_save_stack_nonlocal)
1067     sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
1068 #endif
1069 #ifdef STACK_SAVEAREA_MODE
1070   sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1071 #endif
1072
1073   stack_save
1074     = gen_rtx_MEM (sa_mode,
1075                    memory_address
1076                    (sa_mode,
1077                     plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1078
1079 #ifdef HAVE_setjmp
1080   if (HAVE_setjmp)
1081     emit_insn (gen_setjmp ());
1082 #endif
1083
1084   emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
1085 }
1086
1087 /* Expand a call to __builtin_prefetch.  For a target that does not support
1088    data prefetch, evaluate the memory address argument in case it has side
1089    effects.  */
1090
1091 static void
1092 expand_builtin_prefetch (tree exp)
1093 {
1094   tree arg0, arg1, arg2;
1095   int nargs;
1096   rtx op0, op1, op2;
1097
1098   if (!validate_arglist (exp, POINTER_TYPE, 0))
1099     return;
1100
1101   arg0 = CALL_EXPR_ARG (exp, 0);
1102
1103   /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1104      zero (read) and argument 2 (locality) defaults to 3 (high degree of
1105      locality).  */
1106   nargs = call_expr_nargs (exp);
1107   if (nargs > 1)
1108     arg1 = CALL_EXPR_ARG (exp, 1);
1109   else
1110     arg1 = integer_zero_node;
1111   if (nargs > 2)
1112     arg2 = CALL_EXPR_ARG (exp, 2);
1113   else
1114     arg2 = integer_three_node;
1115
1116   /* Argument 0 is an address.  */
1117   op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1118
1119   /* Argument 1 (read/write flag) must be a compile-time constant int.  */
1120   if (TREE_CODE (arg1) != INTEGER_CST)
1121     {
1122       error ("second argument to %<__builtin_prefetch%> must be a constant");
1123       arg1 = integer_zero_node;
1124     }
1125   op1 = expand_normal (arg1);
1126   /* Argument 1 must be either zero or one.  */
1127   if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1128     {
1129       warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1130                " using zero");
1131       op1 = const0_rtx;
1132     }
1133
1134   /* Argument 2 (locality) must be a compile-time constant int.  */
1135   if (TREE_CODE (arg2) != INTEGER_CST)
1136     {
1137       error ("third argument to %<__builtin_prefetch%> must be a constant");
1138       arg2 = integer_zero_node;
1139     }
1140   op2 = expand_normal (arg2);
1141   /* Argument 2 must be 0, 1, 2, or 3.  */
1142   if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1143     {
1144       warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1145       op2 = const0_rtx;
1146     }
1147
1148 #ifdef HAVE_prefetch
1149   if (HAVE_prefetch)
1150     {
1151       if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1152              (op0,
1153               insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1154           || (GET_MODE (op0) != Pmode))
1155         {
1156           op0 = convert_memory_address (Pmode, op0);
1157           op0 = force_reg (Pmode, op0);
1158         }
1159       emit_insn (gen_prefetch (op0, op1, op2));
1160     }
1161 #endif
1162
1163   /* Don't do anything with direct references to volatile memory, but
1164      generate code to handle other side effects.  */
1165   if (!MEM_P (op0) && side_effects_p (op0))
1166     emit_insn (op0);
1167 }
1168
1169 /* Get a MEM rtx for expression EXP which is the address of an operand
1170    to be used in a string instruction (cmpstrsi, movmemsi, ..).  LEN is
1171    the maximum length of the block of memory that might be accessed or
1172    NULL if unknown.  */
1173
1174 static rtx
1175 get_memory_rtx (tree exp, tree len)
1176 {
1177   tree orig_exp = exp;
1178   rtx addr, mem;
1179   HOST_WIDE_INT off;
1180
1181   /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1182      from its expression, for expr->a.b only <variable>.a.b is recorded.  */
1183   if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1184     exp = TREE_OPERAND (exp, 0);
1185
1186   addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1187   mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1188
1189   /* Get an expression we can use to find the attributes to assign to MEM.
1190      If it is an ADDR_EXPR, use the operand.  Otherwise, dereference it if
1191      we can.  First remove any nops.  */
1192   while (CONVERT_EXPR_P (exp)
1193          && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1194     exp = TREE_OPERAND (exp, 0);
1195
1196   off = 0;
1197   if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1198       && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1199       && host_integerp (TREE_OPERAND (exp, 1), 0)
1200       && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1201     exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1202   else if (TREE_CODE (exp) == ADDR_EXPR)
1203     exp = TREE_OPERAND (exp, 0);
1204   else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1205     exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1206   else
1207     exp = NULL;
1208
1209   /* Honor attributes derived from exp, except for the alias set
1210      (as builtin stringops may alias with anything) and the size
1211      (as stringops may access multiple array elements).  */
1212   if (exp)
1213     {
1214       set_mem_attributes (mem, exp, 0);
1215
1216       if (off)
1217         mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1218
1219       /* Allow the string and memory builtins to overflow from one
1220          field into another, see http://gcc.gnu.org/PR23561.
1221          Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1222          memory accessed by the string or memory builtin will fit
1223          within the field.  */
1224       if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1225         {
1226           tree mem_expr = MEM_EXPR (mem);
1227           HOST_WIDE_INT offset = -1, length = -1;
1228           tree inner = exp;
1229
1230           while (TREE_CODE (inner) == ARRAY_REF
1231                  || CONVERT_EXPR_P (inner)
1232                  || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1233                  || TREE_CODE (inner) == SAVE_EXPR)
1234             inner = TREE_OPERAND (inner, 0);
1235
1236           gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1237
1238           if (MEM_OFFSET (mem)
1239               && CONST_INT_P (MEM_OFFSET (mem)))
1240             offset = INTVAL (MEM_OFFSET (mem));
1241
1242           if (offset >= 0 && len && host_integerp (len, 0))
1243             length = tree_low_cst (len, 0);
1244
1245           while (TREE_CODE (inner) == COMPONENT_REF)
1246             {
1247               tree field = TREE_OPERAND (inner, 1);
1248               gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1249               gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1250
1251               /* Bitfields are generally not byte-addressable.  */
1252               gcc_assert (!DECL_BIT_FIELD (field)
1253                           || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1254                                % BITS_PER_UNIT) == 0
1255                               && host_integerp (DECL_SIZE (field), 0)
1256                               && (TREE_INT_CST_LOW (DECL_SIZE (field))
1257                                   % BITS_PER_UNIT) == 0));
1258
1259               /* If we can prove that the memory starting at XEXP (mem, 0) and
1260                  ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1261                  can keep the COMPONENT_REF in MEM_EXPR.  But be careful with
1262                  fields without DECL_SIZE_UNIT like flexible array members.  */
1263               if (length >= 0
1264                   && DECL_SIZE_UNIT (field)
1265                   && host_integerp (DECL_SIZE_UNIT (field), 0))
1266                 {
1267                   HOST_WIDE_INT size
1268                     = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1269                   if (offset <= size
1270                       && length <= size
1271                       && offset + length <= size)
1272                     break;
1273                 }
1274
1275               if (offset >= 0
1276                   && host_integerp (DECL_FIELD_OFFSET (field), 0))
1277                 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1278                           + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1279                             / BITS_PER_UNIT;
1280               else
1281                 {
1282                   offset = -1;
1283                   length = -1;
1284                 }
1285
1286               mem_expr = TREE_OPERAND (mem_expr, 0);
1287               inner = TREE_OPERAND (inner, 0);
1288             }
1289
1290           if (mem_expr == NULL)
1291             offset = -1;
1292           if (mem_expr != MEM_EXPR (mem))
1293             {
1294               set_mem_expr (mem, mem_expr);
1295               set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1296             }
1297         }
1298       set_mem_alias_set (mem, 0);
1299       set_mem_size (mem, NULL_RTX);
1300     }
1301
1302   return mem;
1303 }
1304 \f
1305 /* Built-in functions to perform an untyped call and return.  */
1306
1307 #define apply_args_mode \
1308   (this_target_builtins->x_apply_args_mode)
1309 #define apply_result_mode \
1310   (this_target_builtins->x_apply_result_mode)
1311
1312 /* Return the size required for the block returned by __builtin_apply_args,
1313    and initialize apply_args_mode.  */
1314
1315 static int
1316 apply_args_size (void)
1317 {
1318   static int size = -1;
1319   int align;
1320   unsigned int regno;
1321   enum machine_mode mode;
1322
1323   /* The values computed by this function never change.  */
1324   if (size < 0)
1325     {
1326       /* The first value is the incoming arg-pointer.  */
1327       size = GET_MODE_SIZE (Pmode);
1328
1329       /* The second value is the structure value address unless this is
1330          passed as an "invisible" first argument.  */
1331       if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1332         size += GET_MODE_SIZE (Pmode);
1333
1334       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1335         if (FUNCTION_ARG_REGNO_P (regno))
1336           {
1337             mode = reg_raw_mode[regno];
1338
1339             gcc_assert (mode != VOIDmode);
1340
1341             align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1342             if (size % align != 0)
1343               size = CEIL (size, align) * align;
1344             size += GET_MODE_SIZE (mode);
1345             apply_args_mode[regno] = mode;
1346           }
1347         else
1348           {
1349             apply_args_mode[regno] = VOIDmode;
1350           }
1351     }
1352   return size;
1353 }
1354
1355 /* Return the size required for the block returned by __builtin_apply,
1356    and initialize apply_result_mode.  */
1357
1358 static int
1359 apply_result_size (void)
1360 {
1361   static int size = -1;
1362   int align, regno;
1363   enum machine_mode mode;
1364
1365   /* The values computed by this function never change.  */
1366   if (size < 0)
1367     {
1368       size = 0;
1369
1370       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1371         if (targetm.calls.function_value_regno_p (regno))
1372           {
1373             mode = reg_raw_mode[regno];
1374
1375             gcc_assert (mode != VOIDmode);
1376
1377             align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1378             if (size % align != 0)
1379               size = CEIL (size, align) * align;
1380             size += GET_MODE_SIZE (mode);
1381             apply_result_mode[regno] = mode;
1382           }
1383         else
1384           apply_result_mode[regno] = VOIDmode;
1385
1386       /* Allow targets that use untyped_call and untyped_return to override
1387          the size so that machine-specific information can be stored here.  */
1388 #ifdef APPLY_RESULT_SIZE
1389       size = APPLY_RESULT_SIZE;
1390 #endif
1391     }
1392   return size;
1393 }
1394
1395 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1396 /* Create a vector describing the result block RESULT.  If SAVEP is true,
1397    the result block is used to save the values; otherwise it is used to
1398    restore the values.  */
1399
1400 static rtx
1401 result_vector (int savep, rtx result)
1402 {
1403   int regno, size, align, nelts;
1404   enum machine_mode mode;
1405   rtx reg, mem;
1406   rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1407
1408   size = nelts = 0;
1409   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1410     if ((mode = apply_result_mode[regno]) != VOIDmode)
1411       {
1412         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1413         if (size % align != 0)
1414           size = CEIL (size, align) * align;
1415         reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1416         mem = adjust_address (result, mode, size);
1417         savevec[nelts++] = (savep
1418                             ? gen_rtx_SET (VOIDmode, mem, reg)
1419                             : gen_rtx_SET (VOIDmode, reg, mem));
1420         size += GET_MODE_SIZE (mode);
1421       }
1422   return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1423 }
1424 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1425
1426 /* Save the state required to perform an untyped call with the same
1427    arguments as were passed to the current function.  */
1428
1429 static rtx
1430 expand_builtin_apply_args_1 (void)
1431 {
1432   rtx registers, tem;
1433   int size, align, regno;
1434   enum machine_mode mode;
1435   rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1436
1437   /* Create a block where the arg-pointer, structure value address,
1438      and argument registers can be saved.  */
1439   registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1440
1441   /* Walk past the arg-pointer and structure value address.  */
1442   size = GET_MODE_SIZE (Pmode);
1443   if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1444     size += GET_MODE_SIZE (Pmode);
1445
1446   /* Save each register used in calling a function to the block.  */
1447   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1448     if ((mode = apply_args_mode[regno]) != VOIDmode)
1449       {
1450         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1451         if (size % align != 0)
1452           size = CEIL (size, align) * align;
1453
1454         tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1455
1456         emit_move_insn (adjust_address (registers, mode, size), tem);
1457         size += GET_MODE_SIZE (mode);
1458       }
1459
1460   /* Save the arg pointer to the block.  */
1461   tem = copy_to_reg (crtl->args.internal_arg_pointer);
1462 #ifdef STACK_GROWS_DOWNWARD
1463   /* We need the pointer as the caller actually passed them to us, not
1464      as we might have pretended they were passed.  Make sure it's a valid
1465      operand, as emit_move_insn isn't expected to handle a PLUS.  */
1466   tem
1467     = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1468                      NULL_RTX);
1469 #endif
1470   emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1471
1472   size = GET_MODE_SIZE (Pmode);
1473
1474   /* Save the structure value address unless this is passed as an
1475      "invisible" first argument.  */
1476   if (struct_incoming_value)
1477     {
1478       emit_move_insn (adjust_address (registers, Pmode, size),
1479                       copy_to_reg (struct_incoming_value));
1480       size += GET_MODE_SIZE (Pmode);
1481     }
1482
1483   /* Return the address of the block.  */
1484   return copy_addr_to_reg (XEXP (registers, 0));
1485 }
1486
1487 /* __builtin_apply_args returns block of memory allocated on
1488    the stack into which is stored the arg pointer, structure
1489    value address, static chain, and all the registers that might
1490    possibly be used in performing a function call.  The code is
1491    moved to the start of the function so the incoming values are
1492    saved.  */
1493
1494 static rtx
1495 expand_builtin_apply_args (void)
1496 {
1497   /* Don't do __builtin_apply_args more than once in a function.
1498      Save the result of the first call and reuse it.  */
1499   if (apply_args_value != 0)
1500     return apply_args_value;
1501   {
1502     /* When this function is called, it means that registers must be
1503        saved on entry to this function.  So we migrate the
1504        call to the first insn of this function.  */
1505     rtx temp;
1506     rtx seq;
1507
1508     start_sequence ();
1509     temp = expand_builtin_apply_args_1 ();
1510     seq = get_insns ();
1511     end_sequence ();
1512
1513     apply_args_value = temp;
1514
1515     /* Put the insns after the NOTE that starts the function.
1516        If this is inside a start_sequence, make the outer-level insn
1517        chain current, so the code is placed at the start of the
1518        function.  If internal_arg_pointer is a non-virtual pseudo,
1519        it needs to be placed after the function that initializes
1520        that pseudo.  */
1521     push_topmost_sequence ();
1522     if (REG_P (crtl->args.internal_arg_pointer)
1523         && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1524       emit_insn_before (seq, parm_birth_insn);
1525     else
1526       emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1527     pop_topmost_sequence ();
1528     return temp;
1529   }
1530 }
1531
1532 /* Perform an untyped call and save the state required to perform an
1533    untyped return of whatever value was returned by the given function.  */
1534
1535 static rtx
1536 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1537 {
1538   int size, align, regno;
1539   enum machine_mode mode;
1540   rtx incoming_args, result, reg, dest, src, call_insn;
1541   rtx old_stack_level = 0;
1542   rtx call_fusage = 0;
1543   rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1544
1545   arguments = convert_memory_address (Pmode, arguments);
1546
1547   /* Create a block where the return registers can be saved.  */
1548   result = assign_stack_local (BLKmode, apply_result_size (), -1);
1549
1550   /* Fetch the arg pointer from the ARGUMENTS block.  */
1551   incoming_args = gen_reg_rtx (Pmode);
1552   emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1553 #ifndef STACK_GROWS_DOWNWARD
1554   incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1555                                        incoming_args, 0, OPTAB_LIB_WIDEN);
1556 #endif
1557
1558   /* Push a new argument block and copy the arguments.  Do not allow
1559      the (potential) memcpy call below to interfere with our stack
1560      manipulations.  */
1561   do_pending_stack_adjust ();
1562   NO_DEFER_POP;
1563
1564   /* Save the stack with nonlocal if available.  */
1565 #ifdef HAVE_save_stack_nonlocal
1566   if (HAVE_save_stack_nonlocal)
1567     emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1568   else
1569 #endif
1570     emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1571
1572   /* Allocate a block of memory onto the stack and copy the memory
1573      arguments to the outgoing arguments address.  We can pass TRUE
1574      as the 4th argument because we just saved the stack pointer
1575      and will restore it right after the call.  */
1576   allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1577
1578   /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1579      may have already set current_function_calls_alloca to true.
1580      current_function_calls_alloca won't be set if argsize is zero,
1581      so we have to guarantee need_drap is true here.  */
1582   if (SUPPORTS_STACK_ALIGNMENT)
1583     crtl->need_drap = true;
1584
1585   dest = virtual_outgoing_args_rtx;
1586 #ifndef STACK_GROWS_DOWNWARD
1587   if (CONST_INT_P (argsize))
1588     dest = plus_constant (dest, -INTVAL (argsize));
1589   else
1590     dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1591 #endif
1592   dest = gen_rtx_MEM (BLKmode, dest);
1593   set_mem_align (dest, PARM_BOUNDARY);
1594   src = gen_rtx_MEM (BLKmode, incoming_args);
1595   set_mem_align (src, PARM_BOUNDARY);
1596   emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1597
1598   /* Refer to the argument block.  */
1599   apply_args_size ();
1600   arguments = gen_rtx_MEM (BLKmode, arguments);
1601   set_mem_align (arguments, PARM_BOUNDARY);
1602
1603   /* Walk past the arg-pointer and structure value address.  */
1604   size = GET_MODE_SIZE (Pmode);
1605   if (struct_value)
1606     size += GET_MODE_SIZE (Pmode);
1607
1608   /* Restore each of the registers previously saved.  Make USE insns
1609      for each of these registers for use in making the call.  */
1610   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1611     if ((mode = apply_args_mode[regno]) != VOIDmode)
1612       {
1613         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1614         if (size % align != 0)
1615           size = CEIL (size, align) * align;
1616         reg = gen_rtx_REG (mode, regno);
1617         emit_move_insn (reg, adjust_address (arguments, mode, size));
1618         use_reg (&call_fusage, reg);
1619         size += GET_MODE_SIZE (mode);
1620       }
1621
1622   /* Restore the structure value address unless this is passed as an
1623      "invisible" first argument.  */
1624   size = GET_MODE_SIZE (Pmode);
1625   if (struct_value)
1626     {
1627       rtx value = gen_reg_rtx (Pmode);
1628       emit_move_insn (value, adjust_address (arguments, Pmode, size));
1629       emit_move_insn (struct_value, value);
1630       if (REG_P (struct_value))
1631         use_reg (&call_fusage, struct_value);
1632       size += GET_MODE_SIZE (Pmode);
1633     }
1634
1635   /* All arguments and registers used for the call are set up by now!  */
1636   function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1637
1638   /* Ensure address is valid.  SYMBOL_REF is already valid, so no need,
1639      and we don't want to load it into a register as an optimization,
1640      because prepare_call_address already did it if it should be done.  */
1641   if (GET_CODE (function) != SYMBOL_REF)
1642     function = memory_address (FUNCTION_MODE, function);
1643
1644   /* Generate the actual call instruction and save the return value.  */
1645 #ifdef HAVE_untyped_call
1646   if (HAVE_untyped_call)
1647     emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1648                                       result, result_vector (1, result)));
1649   else
1650 #endif
1651 #ifdef HAVE_call_value
1652   if (HAVE_call_value)
1653     {
1654       rtx valreg = 0;
1655
1656       /* Locate the unique return register.  It is not possible to
1657          express a call that sets more than one return register using
1658          call_value; use untyped_call for that.  In fact, untyped_call
1659          only needs to save the return registers in the given block.  */
1660       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1661         if ((mode = apply_result_mode[regno]) != VOIDmode)
1662           {
1663             gcc_assert (!valreg); /* HAVE_untyped_call required.  */
1664
1665             valreg = gen_rtx_REG (mode, regno);
1666           }
1667
1668       emit_call_insn (GEN_CALL_VALUE (valreg,
1669                                       gen_rtx_MEM (FUNCTION_MODE, function),
1670                                       const0_rtx, NULL_RTX, const0_rtx));
1671
1672       emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1673     }
1674   else
1675 #endif
1676     gcc_unreachable ();
1677
1678   /* Find the CALL insn we just emitted, and attach the register usage
1679      information.  */
1680   call_insn = last_call_insn ();
1681   add_function_usage_to (call_insn, call_fusage);
1682
1683   /* Restore the stack.  */
1684 #ifdef HAVE_save_stack_nonlocal
1685   if (HAVE_save_stack_nonlocal)
1686     emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1687   else
1688 #endif
1689     emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1690
1691   OK_DEFER_POP;
1692
1693   /* Return the address of the result block.  */
1694   result = copy_addr_to_reg (XEXP (result, 0));
1695   return convert_memory_address (ptr_mode, result);
1696 }
1697
1698 /* Perform an untyped return.  */
1699
1700 static void
1701 expand_builtin_return (rtx result)
1702 {
1703   int size, align, regno;
1704   enum machine_mode mode;
1705   rtx reg;
1706   rtx call_fusage = 0;
1707
1708   result = convert_memory_address (Pmode, result);
1709
1710   apply_result_size ();
1711   result = gen_rtx_MEM (BLKmode, result);
1712
1713 #ifdef HAVE_untyped_return
1714   if (HAVE_untyped_return)
1715     {
1716       emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1717       emit_barrier ();
1718       return;
1719     }
1720 #endif
1721
1722   /* Restore the return value and note that each value is used.  */
1723   size = 0;
1724   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1725     if ((mode = apply_result_mode[regno]) != VOIDmode)
1726       {
1727         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1728         if (size % align != 0)
1729           size = CEIL (size, align) * align;
1730         reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1731         emit_move_insn (reg, adjust_address (result, mode, size));
1732
1733         push_to_sequence (call_fusage);
1734         emit_use (reg);
1735         call_fusage = get_insns ();
1736         end_sequence ();
1737         size += GET_MODE_SIZE (mode);
1738       }
1739
1740   /* Put the USE insns before the return.  */
1741   emit_insn (call_fusage);
1742
1743   /* Return whatever values was restored by jumping directly to the end
1744      of the function.  */
1745   expand_naked_return ();
1746 }
1747
1748 /* Used by expand_builtin_classify_type and fold_builtin_classify_type.  */
1749
1750 static enum type_class
1751 type_to_class (tree type)
1752 {
1753   switch (TREE_CODE (type))
1754     {
1755     case VOID_TYPE:        return void_type_class;
1756     case INTEGER_TYPE:     return integer_type_class;
1757     case ENUMERAL_TYPE:    return enumeral_type_class;
1758     case BOOLEAN_TYPE:     return boolean_type_class;
1759     case POINTER_TYPE:     return pointer_type_class;
1760     case REFERENCE_TYPE:   return reference_type_class;
1761     case OFFSET_TYPE:      return offset_type_class;
1762     case REAL_TYPE:        return real_type_class;
1763     case COMPLEX_TYPE:     return complex_type_class;
1764     case FUNCTION_TYPE:    return function_type_class;
1765     case METHOD_TYPE:      return method_type_class;
1766     case RECORD_TYPE:      return record_type_class;
1767     case UNION_TYPE:
1768     case QUAL_UNION_TYPE:  return union_type_class;
1769     case ARRAY_TYPE:       return (TYPE_STRING_FLAG (type)
1770                                    ? string_type_class : array_type_class);
1771     case LANG_TYPE:        return lang_type_class;
1772     default:               return no_type_class;
1773     }
1774 }
1775
1776 /* Expand a call EXP to __builtin_classify_type.  */
1777
1778 static rtx
1779 expand_builtin_classify_type (tree exp)
1780 {
1781   if (call_expr_nargs (exp))
1782     return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1783   return GEN_INT (no_type_class);
1784 }
1785
1786 /* This helper macro, meant to be used in mathfn_built_in below,
1787    determines which among a set of three builtin math functions is
1788    appropriate for a given type mode.  The `F' and `L' cases are
1789    automatically generated from the `double' case.  */
1790 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1791   case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1792   fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1793   fcodel = BUILT_IN_MATHFN##L ; break;
1794 /* Similar to above, but appends _R after any F/L suffix.  */
1795 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1796   case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1797   fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1798   fcodel = BUILT_IN_MATHFN##L_R ; break;
1799
1800 /* Return mathematic function equivalent to FN but operating directly
1801    on TYPE, if available.  If IMPLICIT is true find the function in
1802    implicit_built_in_decls[], otherwise use built_in_decls[].  If we
1803    can't do the conversion, return zero.  */
1804
1805 static tree
1806 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit)
1807 {
1808   tree const *const fn_arr
1809     = implicit ? implicit_built_in_decls : built_in_decls;
1810   enum built_in_function fcode, fcodef, fcodel;
1811
1812   switch (fn)
1813     {
1814       CASE_MATHFN (BUILT_IN_ACOS)
1815       CASE_MATHFN (BUILT_IN_ACOSH)
1816       CASE_MATHFN (BUILT_IN_ASIN)
1817       CASE_MATHFN (BUILT_IN_ASINH)
1818       CASE_MATHFN (BUILT_IN_ATAN)
1819       CASE_MATHFN (BUILT_IN_ATAN2)
1820       CASE_MATHFN (BUILT_IN_ATANH)
1821       CASE_MATHFN (BUILT_IN_CBRT)
1822       CASE_MATHFN (BUILT_IN_CEIL)
1823       CASE_MATHFN (BUILT_IN_CEXPI)
1824       CASE_MATHFN (BUILT_IN_COPYSIGN)
1825       CASE_MATHFN (BUILT_IN_COS)
1826       CASE_MATHFN (BUILT_IN_COSH)
1827       CASE_MATHFN (BUILT_IN_DREM)
1828       CASE_MATHFN (BUILT_IN_ERF)
1829       CASE_MATHFN (BUILT_IN_ERFC)
1830       CASE_MATHFN (BUILT_IN_EXP)
1831       CASE_MATHFN (BUILT_IN_EXP10)
1832       CASE_MATHFN (BUILT_IN_EXP2)
1833       CASE_MATHFN (BUILT_IN_EXPM1)
1834       CASE_MATHFN (BUILT_IN_FABS)
1835       CASE_MATHFN (BUILT_IN_FDIM)
1836       CASE_MATHFN (BUILT_IN_FLOOR)
1837       CASE_MATHFN (BUILT_IN_FMA)
1838       CASE_MATHFN (BUILT_IN_FMAX)
1839       CASE_MATHFN (BUILT_IN_FMIN)
1840       CASE_MATHFN (BUILT_IN_FMOD)
1841       CASE_MATHFN (BUILT_IN_FREXP)
1842       CASE_MATHFN (BUILT_IN_GAMMA)
1843       CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1844       CASE_MATHFN (BUILT_IN_HUGE_VAL)
1845       CASE_MATHFN (BUILT_IN_HYPOT)
1846       CASE_MATHFN (BUILT_IN_ILOGB)
1847       CASE_MATHFN (BUILT_IN_INF)
1848       CASE_MATHFN (BUILT_IN_ISINF)
1849       CASE_MATHFN (BUILT_IN_J0)
1850       CASE_MATHFN (BUILT_IN_J1)
1851       CASE_MATHFN (BUILT_IN_JN)
1852       CASE_MATHFN (BUILT_IN_LCEIL)
1853       CASE_MATHFN (BUILT_IN_LDEXP)
1854       CASE_MATHFN (BUILT_IN_LFLOOR)
1855       CASE_MATHFN (BUILT_IN_LGAMMA)
1856       CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1857       CASE_MATHFN (BUILT_IN_LLCEIL)
1858       CASE_MATHFN (BUILT_IN_LLFLOOR)
1859       CASE_MATHFN (BUILT_IN_LLRINT)
1860       CASE_MATHFN (BUILT_IN_LLROUND)
1861       CASE_MATHFN (BUILT_IN_LOG)
1862       CASE_MATHFN (BUILT_IN_LOG10)
1863       CASE_MATHFN (BUILT_IN_LOG1P)
1864       CASE_MATHFN (BUILT_IN_LOG2)
1865       CASE_MATHFN (BUILT_IN_LOGB)
1866       CASE_MATHFN (BUILT_IN_LRINT)
1867       CASE_MATHFN (BUILT_IN_LROUND)
1868       CASE_MATHFN (BUILT_IN_MODF)
1869       CASE_MATHFN (BUILT_IN_NAN)
1870       CASE_MATHFN (BUILT_IN_NANS)
1871       CASE_MATHFN (BUILT_IN_NEARBYINT)
1872       CASE_MATHFN (BUILT_IN_NEXTAFTER)
1873       CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1874       CASE_MATHFN (BUILT_IN_POW)
1875       CASE_MATHFN (BUILT_IN_POWI)
1876       CASE_MATHFN (BUILT_IN_POW10)
1877       CASE_MATHFN (BUILT_IN_REMAINDER)
1878       CASE_MATHFN (BUILT_IN_REMQUO)
1879       CASE_MATHFN (BUILT_IN_RINT)
1880       CASE_MATHFN (BUILT_IN_ROUND)
1881       CASE_MATHFN (BUILT_IN_SCALB)
1882       CASE_MATHFN (BUILT_IN_SCALBLN)
1883       CASE_MATHFN (BUILT_IN_SCALBN)
1884       CASE_MATHFN (BUILT_IN_SIGNBIT)
1885       CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1886       CASE_MATHFN (BUILT_IN_SIN)
1887       CASE_MATHFN (BUILT_IN_SINCOS)
1888       CASE_MATHFN (BUILT_IN_SINH)
1889       CASE_MATHFN (BUILT_IN_SQRT)
1890       CASE_MATHFN (BUILT_IN_TAN)
1891       CASE_MATHFN (BUILT_IN_TANH)
1892       CASE_MATHFN (BUILT_IN_TGAMMA)
1893       CASE_MATHFN (BUILT_IN_TRUNC)
1894       CASE_MATHFN (BUILT_IN_Y0)
1895       CASE_MATHFN (BUILT_IN_Y1)
1896       CASE_MATHFN (BUILT_IN_YN)
1897
1898       default:
1899         return NULL_TREE;
1900       }
1901
1902   if (TYPE_MAIN_VARIANT (type) == double_type_node)
1903     return fn_arr[fcode];
1904   else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1905     return fn_arr[fcodef];
1906   else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1907     return fn_arr[fcodel];
1908   else
1909     return NULL_TREE;
1910 }
1911
1912 /* Like mathfn_built_in_1(), but always use the implicit array.  */
1913
1914 tree
1915 mathfn_built_in (tree type, enum built_in_function fn)
1916 {
1917   return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1918 }
1919
1920 /* If errno must be maintained, expand the RTL to check if the result,
1921    TARGET, of a built-in function call, EXP, is NaN, and if so set
1922    errno to EDOM.  */
1923
1924 static void
1925 expand_errno_check (tree exp, rtx target)
1926 {
1927   rtx lab = gen_label_rtx ();
1928
1929   /* Test the result; if it is NaN, set errno=EDOM because
1930      the argument was not in the domain.  */
1931   do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1932                            NULL_RTX, NULL_RTX, lab,
1933                            /* The jump is very likely.  */
1934                            REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1935
1936 #ifdef TARGET_EDOM
1937   /* If this built-in doesn't throw an exception, set errno directly.  */
1938   if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1939     {
1940 #ifdef GEN_ERRNO_RTX
1941       rtx errno_rtx = GEN_ERRNO_RTX;
1942 #else
1943       rtx errno_rtx
1944           = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1945 #endif
1946       emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1947       emit_label (lab);
1948       return;
1949     }
1950 #endif
1951
1952   /* Make sure the library call isn't expanded as a tail call.  */
1953   CALL_EXPR_TAILCALL (exp) = 0;
1954
1955   /* We can't set errno=EDOM directly; let the library call do it.
1956      Pop the arguments right away in case the call gets deleted.  */
1957   NO_DEFER_POP;
1958   expand_call (exp, target, 0);
1959   OK_DEFER_POP;
1960   emit_label (lab);
1961 }
1962
1963 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1964    Return NULL_RTX if a normal call should be emitted rather than expanding
1965    the function in-line.  EXP is the expression that is a call to the builtin
1966    function; if convenient, the result should be placed in TARGET.
1967    SUBTARGET may be used as the target for computing one of EXP's operands.  */
1968
1969 static rtx
1970 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1971 {
1972   optab builtin_optab;
1973   rtx op0, insns;
1974   tree fndecl = get_callee_fndecl (exp);
1975   enum machine_mode mode;
1976   bool errno_set = false;
1977   tree arg;
1978
1979   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1980     return NULL_RTX;
1981
1982   arg = CALL_EXPR_ARG (exp, 0);
1983
1984   switch (DECL_FUNCTION_CODE (fndecl))
1985     {
1986     CASE_FLT_FN (BUILT_IN_SQRT):
1987       errno_set = ! tree_expr_nonnegative_p (arg);
1988       builtin_optab = sqrt_optab;
1989       break;
1990     CASE_FLT_FN (BUILT_IN_EXP):
1991       errno_set = true; builtin_optab = exp_optab; break;
1992     CASE_FLT_FN (BUILT_IN_EXP10):
1993     CASE_FLT_FN (BUILT_IN_POW10):
1994       errno_set = true; builtin_optab = exp10_optab; break;
1995     CASE_FLT_FN (BUILT_IN_EXP2):
1996       errno_set = true; builtin_optab = exp2_optab; break;
1997     CASE_FLT_FN (BUILT_IN_EXPM1):
1998       errno_set = true; builtin_optab = expm1_optab; break;
1999     CASE_FLT_FN (BUILT_IN_LOGB):
2000       errno_set = true; builtin_optab = logb_optab; break;
2001     CASE_FLT_FN (BUILT_IN_LOG):
2002       errno_set = true; builtin_optab = log_optab; break;
2003     CASE_FLT_FN (BUILT_IN_LOG10):
2004       errno_set = true; builtin_optab = log10_optab; break;
2005     CASE_FLT_FN (BUILT_IN_LOG2):
2006       errno_set = true; builtin_optab = log2_optab; break;
2007     CASE_FLT_FN (BUILT_IN_LOG1P):
2008       errno_set = true; builtin_optab = log1p_optab; break;
2009     CASE_FLT_FN (BUILT_IN_ASIN):
2010       builtin_optab = asin_optab; break;
2011     CASE_FLT_FN (BUILT_IN_ACOS):
2012       builtin_optab = acos_optab; break;
2013     CASE_FLT_FN (BUILT_IN_TAN):
2014       builtin_optab = tan_optab; break;
2015     CASE_FLT_FN (BUILT_IN_ATAN):
2016       builtin_optab = atan_optab; break;
2017     CASE_FLT_FN (BUILT_IN_FLOOR):
2018       builtin_optab = floor_optab; break;
2019     CASE_FLT_FN (BUILT_IN_CEIL):
2020       builtin_optab = ceil_optab; break;
2021     CASE_FLT_FN (BUILT_IN_TRUNC):
2022       builtin_optab = btrunc_optab; break;
2023     CASE_FLT_FN (BUILT_IN_ROUND):
2024       builtin_optab = round_optab; break;
2025     CASE_FLT_FN (BUILT_IN_NEARBYINT):
2026       builtin_optab = nearbyint_optab;
2027       if (flag_trapping_math)
2028         break;
2029       /* Else fallthrough and expand as rint.  */
2030     CASE_FLT_FN (BUILT_IN_RINT):
2031       builtin_optab = rint_optab; break;
2032     CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2033       builtin_optab = significand_optab; break;
2034     default:
2035       gcc_unreachable ();
2036     }
2037
2038   /* Make a suitable register to place result in.  */
2039   mode = TYPE_MODE (TREE_TYPE (exp));
2040
2041   if (! flag_errno_math || ! HONOR_NANS (mode))
2042     errno_set = false;
2043
2044   /* Before working hard, check whether the instruction is available.  */
2045   if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2046     {
2047       target = gen_reg_rtx (mode);
2048
2049       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2050          need to expand the argument again.  This way, we will not perform
2051          side-effects more the once.  */
2052       CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2053
2054       op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2055
2056       start_sequence ();
2057
2058       /* Compute into TARGET.
2059          Set TARGET to wherever the result comes back.  */
2060       target = expand_unop (mode, builtin_optab, op0, target, 0);
2061
2062       if (target != 0)
2063         {
2064           if (errno_set)
2065             expand_errno_check (exp, target);
2066
2067           /* Output the entire sequence.  */
2068           insns = get_insns ();
2069           end_sequence ();
2070           emit_insn (insns);
2071           return target;
2072         }
2073
2074       /* If we were unable to expand via the builtin, stop the sequence
2075          (without outputting the insns) and call to the library function
2076          with the stabilized argument list.  */
2077       end_sequence ();
2078     }
2079
2080   return expand_call (exp, target, target == const0_rtx);
2081 }
2082
2083 /* Expand a call to the builtin binary math functions (pow and atan2).
2084    Return NULL_RTX if a normal call should be emitted rather than expanding the
2085    function in-line.  EXP is the expression that is a call to the builtin
2086    function; if convenient, the result should be placed in TARGET.
2087    SUBTARGET may be used as the target for computing one of EXP's
2088    operands.  */
2089
2090 static rtx
2091 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2092 {
2093   optab builtin_optab;
2094   rtx op0, op1, insns;
2095   int op1_type = REAL_TYPE;
2096   tree fndecl = get_callee_fndecl (exp);
2097   tree arg0, arg1;
2098   enum machine_mode mode;
2099   bool errno_set = true;
2100
2101   switch (DECL_FUNCTION_CODE (fndecl))
2102     {
2103     CASE_FLT_FN (BUILT_IN_SCALBN):
2104     CASE_FLT_FN (BUILT_IN_SCALBLN):
2105     CASE_FLT_FN (BUILT_IN_LDEXP):
2106       op1_type = INTEGER_TYPE;
2107     default:
2108       break;
2109     }
2110
2111   if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2112     return NULL_RTX;
2113
2114   arg0 = CALL_EXPR_ARG (exp, 0);
2115   arg1 = CALL_EXPR_ARG (exp, 1);
2116
2117   switch (DECL_FUNCTION_CODE (fndecl))
2118     {
2119     CASE_FLT_FN (BUILT_IN_POW):
2120       builtin_optab = pow_optab; break;
2121     CASE_FLT_FN (BUILT_IN_ATAN2):
2122       builtin_optab = atan2_optab; break;
2123     CASE_FLT_FN (BUILT_IN_SCALB):
2124       if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2125         return 0;
2126       builtin_optab = scalb_optab; break;
2127     CASE_FLT_FN (BUILT_IN_SCALBN):
2128     CASE_FLT_FN (BUILT_IN_SCALBLN):
2129       if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2130         return 0;
2131     /* Fall through... */
2132     CASE_FLT_FN (BUILT_IN_LDEXP):
2133       builtin_optab = ldexp_optab; break;
2134     CASE_FLT_FN (BUILT_IN_FMOD):
2135       builtin_optab = fmod_optab; break;
2136     CASE_FLT_FN (BUILT_IN_REMAINDER):
2137     CASE_FLT_FN (BUILT_IN_DREM):
2138       builtin_optab = remainder_optab; break;
2139     default:
2140       gcc_unreachable ();
2141     }
2142
2143   /* Make a suitable register to place result in.  */
2144   mode = TYPE_MODE (TREE_TYPE (exp));
2145
2146   /* Before working hard, check whether the instruction is available.  */
2147   if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2148     return NULL_RTX;
2149
2150   target = gen_reg_rtx (mode);
2151
2152   if (! flag_errno_math || ! HONOR_NANS (mode))
2153     errno_set = false;
2154
2155   /* Always stabilize the argument list.  */
2156   CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2157   CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2158
2159   op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2160   op1 = expand_normal (arg1);
2161
2162   start_sequence ();
2163
2164   /* Compute into TARGET.
2165      Set TARGET to wherever the result comes back.  */
2166   target = expand_binop (mode, builtin_optab, op0, op1,
2167                          target, 0, OPTAB_DIRECT);
2168
2169   /* If we were unable to expand via the builtin, stop the sequence
2170      (without outputting the insns) and call to the library function
2171      with the stabilized argument list.  */
2172   if (target == 0)
2173     {
2174       end_sequence ();
2175       return expand_call (exp, target, target == const0_rtx);
2176     }
2177
2178   if (errno_set)
2179     expand_errno_check (exp, target);
2180
2181   /* Output the entire sequence.  */
2182   insns = get_insns ();
2183   end_sequence ();
2184   emit_insn (insns);
2185
2186   return target;
2187 }
2188
2189 /* Expand a call to the builtin trinary math functions (fma).
2190    Return NULL_RTX if a normal call should be emitted rather than expanding the
2191    function in-line.  EXP is the expression that is a call to the builtin
2192    function; if convenient, the result should be placed in TARGET.
2193    SUBTARGET may be used as the target for computing one of EXP's
2194    operands.  */
2195
2196 static rtx
2197 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2198 {
2199   optab builtin_optab;
2200   rtx op0, op1, op2, insns;
2201   tree fndecl = get_callee_fndecl (exp);
2202   tree arg0, arg1, arg2;
2203   enum machine_mode mode;
2204
2205   if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2206     return NULL_RTX;
2207
2208   arg0 = CALL_EXPR_ARG (exp, 0);
2209   arg1 = CALL_EXPR_ARG (exp, 1);
2210   arg2 = CALL_EXPR_ARG (exp, 2);
2211
2212   switch (DECL_FUNCTION_CODE (fndecl))
2213     {
2214     CASE_FLT_FN (BUILT_IN_FMA):
2215       builtin_optab = fma_optab; break;
2216     default:
2217       gcc_unreachable ();
2218     }
2219
2220   /* Make a suitable register to place result in.  */
2221   mode = TYPE_MODE (TREE_TYPE (exp));
2222
2223   /* Before working hard, check whether the instruction is available.  */
2224   if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2225     return NULL_RTX;
2226
2227   target = gen_reg_rtx (mode);
2228
2229   /* Always stabilize the argument list.  */
2230   CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2231   CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2232   CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2233
2234   op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2235   op1 = expand_normal (arg1);
2236   op2 = expand_normal (arg2);
2237
2238   start_sequence ();
2239
2240   /* Compute into TARGET.
2241      Set TARGET to wherever the result comes back.  */
2242   target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2243                               target, 0);
2244
2245   /* If we were unable to expand via the builtin, stop the sequence
2246      (without outputting the insns) and call to the library function
2247      with the stabilized argument list.  */
2248   if (target == 0)
2249     {
2250       end_sequence ();
2251       return expand_call (exp, target, target == const0_rtx);
2252     }
2253
2254   /* Output the entire sequence.  */
2255   insns = get_insns ();
2256   end_sequence ();
2257   emit_insn (insns);
2258
2259   return target;
2260 }
2261
2262 /* Expand a call to the builtin sin and cos math functions.
2263    Return NULL_RTX if a normal call should be emitted rather than expanding the
2264    function in-line.  EXP is the expression that is a call to the builtin
2265    function; if convenient, the result should be placed in TARGET.
2266    SUBTARGET may be used as the target for computing one of EXP's
2267    operands.  */
2268
2269 static rtx
2270 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2271 {
2272   optab builtin_optab;
2273   rtx op0, insns;
2274   tree fndecl = get_callee_fndecl (exp);
2275   enum machine_mode mode;
2276   tree arg;
2277
2278   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2279     return NULL_RTX;
2280
2281   arg = CALL_EXPR_ARG (exp, 0);
2282
2283   switch (DECL_FUNCTION_CODE (fndecl))
2284     {
2285     CASE_FLT_FN (BUILT_IN_SIN):
2286     CASE_FLT_FN (BUILT_IN_COS):
2287       builtin_optab = sincos_optab; break;
2288     default:
2289       gcc_unreachable ();
2290     }
2291
2292   /* Make a suitable register to place result in.  */
2293   mode = TYPE_MODE (TREE_TYPE (exp));
2294
2295   /* Check if sincos insn is available, otherwise fallback
2296      to sin or cos insn.  */
2297   if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2298     switch (DECL_FUNCTION_CODE (fndecl))
2299       {
2300       CASE_FLT_FN (BUILT_IN_SIN):
2301         builtin_optab = sin_optab; break;
2302       CASE_FLT_FN (BUILT_IN_COS):
2303         builtin_optab = cos_optab; break;
2304       default:
2305         gcc_unreachable ();
2306       }
2307
2308   /* Before working hard, check whether the instruction is available.  */
2309   if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2310     {
2311       target = gen_reg_rtx (mode);
2312
2313       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2314          need to expand the argument again.  This way, we will not perform
2315          side-effects more the once.  */
2316       CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2317
2318       op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2319
2320       start_sequence ();
2321
2322       /* Compute into TARGET.
2323          Set TARGET to wherever the result comes back.  */
2324       if (builtin_optab == sincos_optab)
2325         {
2326           int result;
2327
2328           switch (DECL_FUNCTION_CODE (fndecl))
2329             {
2330             CASE_FLT_FN (BUILT_IN_SIN):
2331               result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2332               break;
2333             CASE_FLT_FN (BUILT_IN_COS):
2334               result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2335               break;
2336             default:
2337               gcc_unreachable ();
2338             }
2339           gcc_assert (result);
2340         }
2341       else
2342         {
2343           target = expand_unop (mode, builtin_optab, op0, target, 0);
2344         }
2345
2346       if (target != 0)
2347         {
2348           /* Output the entire sequence.  */
2349           insns = get_insns ();
2350           end_sequence ();
2351           emit_insn (insns);
2352           return target;
2353         }
2354
2355       /* If we were unable to expand via the builtin, stop the sequence
2356          (without outputting the insns) and call to the library function
2357          with the stabilized argument list.  */
2358       end_sequence ();
2359     }
2360
2361   target = expand_call (exp, target, target == const0_rtx);
2362
2363   return target;
2364 }
2365
2366 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2367    return an RTL instruction code that implements the functionality.
2368    If that isn't possible or available return CODE_FOR_nothing.  */
2369
2370 static enum insn_code
2371 interclass_mathfn_icode (tree arg, tree fndecl)
2372 {
2373   bool errno_set = false;
2374   optab builtin_optab = 0;
2375   enum machine_mode mode;
2376
2377   switch (DECL_FUNCTION_CODE (fndecl))
2378     {
2379     CASE_FLT_FN (BUILT_IN_ILOGB):
2380       errno_set = true; builtin_optab = ilogb_optab; break;
2381     CASE_FLT_FN (BUILT_IN_ISINF):
2382       builtin_optab = isinf_optab; break;
2383     case BUILT_IN_ISNORMAL:
2384     case BUILT_IN_ISFINITE:
2385     CASE_FLT_FN (BUILT_IN_FINITE):
2386     case BUILT_IN_FINITED32:
2387     case BUILT_IN_FINITED64:
2388     case BUILT_IN_FINITED128:
2389     case BUILT_IN_ISINFD32:
2390     case BUILT_IN_ISINFD64:
2391     case BUILT_IN_ISINFD128:
2392       /* These builtins have no optabs (yet).  */
2393       break;
2394     default:
2395       gcc_unreachable ();
2396     }
2397
2398   /* There's no easy way to detect the case we need to set EDOM.  */
2399   if (flag_errno_math && errno_set)
2400     return CODE_FOR_nothing;
2401
2402   /* Optab mode depends on the mode of the input argument.  */
2403   mode = TYPE_MODE (TREE_TYPE (arg));
2404
2405   if (builtin_optab)
2406     return optab_handler (builtin_optab, mode);
2407   return CODE_FOR_nothing;
2408 }
2409
2410 /* Expand a call to one of the builtin math functions that operate on
2411    floating point argument and output an integer result (ilogb, isinf,
2412    isnan, etc).
2413    Return 0 if a normal call should be emitted rather than expanding the
2414    function in-line.  EXP is the expression that is a call to the builtin
2415    function; if convenient, the result should be placed in TARGET.  */
2416
2417 static rtx
2418 expand_builtin_interclass_mathfn (tree exp, rtx target)
2419 {
2420   enum insn_code icode = CODE_FOR_nothing;
2421   rtx op0;
2422   tree fndecl = get_callee_fndecl (exp);
2423   enum machine_mode mode;
2424   tree arg;
2425
2426   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2427     return NULL_RTX;
2428
2429   arg = CALL_EXPR_ARG (exp, 0);
2430   icode = interclass_mathfn_icode (arg, fndecl);
2431   mode = TYPE_MODE (TREE_TYPE (arg));
2432
2433   if (icode != CODE_FOR_nothing)
2434     {
2435       rtx last = get_last_insn ();
2436       tree orig_arg = arg;
2437       /* Make a suitable register to place result in.  */
2438       if (!target
2439           || GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp))
2440           || !insn_data[icode].operand[0].predicate (target, GET_MODE (target)))
2441          target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2442
2443       gcc_assert (insn_data[icode].operand[0].predicate
2444                   (target, GET_MODE (target)));
2445
2446       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2447          need to expand the argument again.  This way, we will not perform
2448          side-effects more the once.  */
2449       CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2450
2451       op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2452
2453       if (mode != GET_MODE (op0))
2454         op0 = convert_to_mode (mode, op0, 0);
2455
2456       /* Compute into TARGET.
2457          Set TARGET to wherever the result comes back.  */
2458       if (maybe_emit_unop_insn (icode, target, op0, UNKNOWN))
2459         return target;
2460       delete_insns_since (last);
2461       CALL_EXPR_ARG (exp, 0) = orig_arg;
2462     }
2463
2464   return NULL_RTX;
2465 }
2466
2467 /* Expand a call to the builtin sincos math function.
2468    Return NULL_RTX if a normal call should be emitted rather than expanding the
2469    function in-line.  EXP is the expression that is a call to the builtin
2470    function.  */
2471
2472 static rtx
2473 expand_builtin_sincos (tree exp)
2474 {
2475   rtx op0, op1, op2, target1, target2;
2476   enum machine_mode mode;
2477   tree arg, sinp, cosp;
2478   int result;
2479   location_t loc = EXPR_LOCATION (exp);
2480
2481   if (!validate_arglist (exp, REAL_TYPE,
2482                          POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2483     return NULL_RTX;
2484
2485   arg = CALL_EXPR_ARG (exp, 0);
2486   sinp = CALL_EXPR_ARG (exp, 1);
2487   cosp = CALL_EXPR_ARG (exp, 2);
2488
2489   /* Make a suitable register to place result in.  */
2490   mode = TYPE_MODE (TREE_TYPE (arg));
2491
2492   /* Check if sincos insn is available, otherwise emit the call.  */
2493   if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2494     return NULL_RTX;
2495
2496   target1 = gen_reg_rtx (mode);
2497   target2 = gen_reg_rtx (mode);
2498
2499   op0 = expand_normal (arg);
2500   op1 = expand_normal (build_fold_indirect_ref_loc (loc, sinp));
2501   op2 = expand_normal (build_fold_indirect_ref_loc (loc, cosp));
2502
2503   /* Compute into target1 and target2.
2504      Set TARGET to wherever the result comes back.  */
2505   result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2506   gcc_assert (result);
2507
2508   /* Move target1 and target2 to the memory locations indicated
2509      by op1 and op2.  */
2510   emit_move_insn (op1, target1);
2511   emit_move_insn (op2, target2);
2512
2513   return const0_rtx;
2514 }
2515
2516 /* Expand a call to the internal cexpi builtin to the sincos math function.
2517    EXP is the expression that is a call to the builtin function; if convenient,
2518    the result should be placed in TARGET.  */
2519
2520 static rtx
2521 expand_builtin_cexpi (tree exp, rtx target)
2522 {
2523   tree fndecl = get_callee_fndecl (exp);
2524   tree arg, type;
2525   enum machine_mode mode;
2526   rtx op0, op1, op2;
2527   location_t loc = EXPR_LOCATION (exp);
2528
2529   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2530     return NULL_RTX;
2531
2532   arg = CALL_EXPR_ARG (exp, 0);
2533   type = TREE_TYPE (arg);
2534   mode = TYPE_MODE (TREE_TYPE (arg));
2535
2536   /* Try expanding via a sincos optab, fall back to emitting a libcall
2537      to sincos or cexp.  We are sure we have sincos or cexp because cexpi
2538      is only generated from sincos, cexp or if we have either of them.  */
2539   if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2540     {
2541       op1 = gen_reg_rtx (mode);
2542       op2 = gen_reg_rtx (mode);
2543
2544       op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2545
2546       /* Compute into op1 and op2.  */
2547       expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2548     }
2549   else if (TARGET_HAS_SINCOS)
2550     {
2551       tree call, fn = NULL_TREE;
2552       tree top1, top2;
2553       rtx op1a, op2a;
2554
2555       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2556         fn = built_in_decls[BUILT_IN_SINCOSF];
2557       else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2558         fn = built_in_decls[BUILT_IN_SINCOS];
2559       else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2560         fn = built_in_decls[BUILT_IN_SINCOSL];
2561       else
2562         gcc_unreachable ();
2563
2564       op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2565       op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2566       op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2567       op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2568       top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2569       top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2570
2571       /* Make sure not to fold the sincos call again.  */
2572       call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2573       expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2574                                       call, 3, arg, top1, top2));
2575     }
2576   else
2577     {
2578       tree call, fn = NULL_TREE, narg;
2579       tree ctype = build_complex_type (type);
2580
2581       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2582         fn = built_in_decls[BUILT_IN_CEXPF];
2583       else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2584         fn = built_in_decls[BUILT_IN_CEXP];
2585       else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2586         fn = built_in_decls[BUILT_IN_CEXPL];
2587       else
2588         gcc_unreachable ();
2589
2590       /* If we don't have a decl for cexp create one.  This is the
2591          friendliest fallback if the user calls __builtin_cexpi
2592          without full target C99 function support.  */
2593       if (fn == NULL_TREE)
2594         {
2595           tree fntype;
2596           const char *name = NULL;
2597
2598           if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2599             name = "cexpf";
2600           else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2601             name = "cexp";
2602           else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2603             name = "cexpl";
2604
2605           fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2606           fn = build_fn_decl (name, fntype);
2607         }
2608
2609       narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2610                           build_real (type, dconst0), arg);
2611
2612       /* Make sure not to fold the cexp call again.  */
2613       call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2614       return expand_expr (build_call_nary (ctype, call, 1, narg),
2615                           target, VOIDmode, EXPAND_NORMAL);
2616     }
2617
2618   /* Now build the proper return type.  */
2619   return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2620                               make_tree (TREE_TYPE (arg), op2),
2621                               make_tree (TREE_TYPE (arg), op1)),
2622                       target, VOIDmode, EXPAND_NORMAL);
2623 }
2624
2625 /* Conveniently construct a function call expression.  FNDECL names the
2626    function to be called, N is the number of arguments, and the "..."
2627    parameters are the argument expressions.  Unlike build_call_exr
2628    this doesn't fold the call, hence it will always return a CALL_EXPR.  */
2629
2630 static tree
2631 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2632 {
2633   va_list ap;
2634   tree fntype = TREE_TYPE (fndecl);
2635   tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2636
2637   va_start (ap, n);
2638   fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2639   va_end (ap);
2640   SET_EXPR_LOCATION (fn, loc);
2641   return fn;
2642 }
2643
2644 /* Expand a call to one of the builtin rounding functions gcc defines
2645    as an extension (lfloor and lceil).  As these are gcc extensions we
2646    do not need to worry about setting errno to EDOM.
2647    If expanding via optab fails, lower expression to (int)(floor(x)).
2648    EXP is the expression that is a call to the builtin function;
2649    if convenient, the result should be placed in TARGET.  */
2650
2651 static rtx
2652 expand_builtin_int_roundingfn (tree exp, rtx target)
2653 {
2654   convert_optab builtin_optab;
2655   rtx op0, insns, tmp;
2656   tree fndecl = get_callee_fndecl (exp);
2657   enum built_in_function fallback_fn;
2658   tree fallback_fndecl;
2659   enum machine_mode mode;
2660   tree arg;
2661
2662   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2663     gcc_unreachable ();
2664
2665   arg = CALL_EXPR_ARG (exp, 0);
2666
2667   switch (DECL_FUNCTION_CODE (fndecl))
2668     {
2669     CASE_FLT_FN (BUILT_IN_LCEIL):
2670     CASE_FLT_FN (BUILT_IN_LLCEIL):
2671       builtin_optab = lceil_optab;
2672       fallback_fn = BUILT_IN_CEIL;
2673       break;
2674
2675     CASE_FLT_FN (BUILT_IN_LFLOOR):
2676     CASE_FLT_FN (BUILT_IN_LLFLOOR):
2677       builtin_optab = lfloor_optab;
2678       fallback_fn = BUILT_IN_FLOOR;
2679       break;
2680
2681     default:
2682       gcc_unreachable ();
2683     }
2684
2685   /* Make a suitable register to place result in.  */
2686   mode = TYPE_MODE (TREE_TYPE (exp));
2687
2688   target = gen_reg_rtx (mode);
2689
2690   /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2691      need to expand the argument again.  This way, we will not perform
2692      side-effects more the once.  */
2693   CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2694
2695   op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2696
2697   start_sequence ();
2698
2699   /* Compute into TARGET.  */
2700   if (expand_sfix_optab (target, op0, builtin_optab))
2701     {
2702       /* Output the entire sequence.  */
2703       insns = get_insns ();
2704       end_sequence ();
2705       emit_insn (insns);
2706       return target;
2707     }
2708
2709   /* If we were unable to expand via the builtin, stop the sequence
2710      (without outputting the insns).  */
2711   end_sequence ();
2712
2713   /* Fall back to floating point rounding optab.  */
2714   fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2715
2716   /* For non-C99 targets we may end up without a fallback fndecl here
2717      if the user called __builtin_lfloor directly.  In this case emit
2718      a call to the floor/ceil variants nevertheless.  This should result
2719      in the best user experience for not full C99 targets.  */
2720   if (fallback_fndecl == NULL_TREE)
2721     {
2722       tree fntype;
2723       const char *name = NULL;
2724
2725       switch (DECL_FUNCTION_CODE (fndecl))
2726         {
2727         case BUILT_IN_LCEIL:
2728         case BUILT_IN_LLCEIL:
2729           name = "ceil";
2730           break;
2731         case BUILT_IN_LCEILF:
2732         case BUILT_IN_LLCEILF:
2733           name = "ceilf";
2734           break;
2735         case BUILT_IN_LCEILL:
2736         case BUILT_IN_LLCEILL:
2737           name = "ceill";
2738           break;
2739         case BUILT_IN_LFLOOR:
2740         case BUILT_IN_LLFLOOR:
2741           name = "floor";
2742           break;
2743         case BUILT_IN_LFLOORF:
2744         case BUILT_IN_LLFLOORF:
2745           name = "floorf";
2746           break;
2747         case BUILT_IN_LFLOORL:
2748         case BUILT_IN_LLFLOORL:
2749           name = "floorl";
2750           break;
2751         default:
2752           gcc_unreachable ();
2753         }
2754
2755       fntype = build_function_type_list (TREE_TYPE (arg),
2756                                          TREE_TYPE (arg), NULL_TREE);
2757       fallback_fndecl = build_fn_decl (name, fntype);
2758     }
2759
2760   exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2761
2762   tmp = expand_normal (exp);
2763
2764   /* Truncate the result of floating point optab to integer
2765      via expand_fix ().  */
2766   target = gen_reg_rtx (mode);
2767   expand_fix (target, tmp, 0);
2768
2769   return target;
2770 }
2771
2772 /* Expand a call to one of the builtin math functions doing integer
2773    conversion (lrint).
2774    Return 0 if a normal call should be emitted rather than expanding the
2775    function in-line.  EXP is the expression that is a call to the builtin
2776    function; if convenient, the result should be placed in TARGET.  */
2777
2778 static rtx
2779 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2780 {
2781   convert_optab builtin_optab;
2782   rtx op0, insns;
2783   tree fndecl = get_callee_fndecl (exp);
2784   tree arg;
2785   enum machine_mode mode;
2786
2787   /* There's no easy way to detect the case we need to set EDOM.  */
2788   if (flag_errno_math)
2789     return NULL_RTX;
2790
2791   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2792      gcc_unreachable ();
2793
2794   arg = CALL_EXPR_ARG (exp, 0);
2795
2796   switch (DECL_FUNCTION_CODE (fndecl))
2797     {
2798     CASE_FLT_FN (BUILT_IN_LRINT):
2799     CASE_FLT_FN (BUILT_IN_LLRINT):
2800       builtin_optab = lrint_optab; break;
2801     CASE_FLT_FN (BUILT_IN_LROUND):
2802     CASE_FLT_FN (BUILT_IN_LLROUND):
2803       builtin_optab = lround_optab; break;
2804     default:
2805       gcc_unreachable ();
2806     }
2807
2808   /* Make a suitable register to place result in.  */
2809   mode = TYPE_MODE (TREE_TYPE (exp));
2810
2811   target = gen_reg_rtx (mode);
2812
2813   /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2814      need to expand the argument again.  This way, we will not perform
2815      side-effects more the once.  */
2816   CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2817
2818   op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2819
2820   start_sequence ();
2821
2822   if (expand_sfix_optab (target, op0, builtin_optab))
2823     {
2824       /* Output the entire sequence.  */
2825       insns = get_insns ();
2826       end_sequence ();
2827       emit_insn (insns);
2828       return target;
2829     }
2830
2831   /* If we were unable to expand via the builtin, stop the sequence
2832      (without outputting the insns) and call to the library function
2833      with the stabilized argument list.  */
2834   end_sequence ();
2835
2836   target = expand_call (exp, target, target == const0_rtx);
2837
2838   return target;
2839 }
2840
2841 /* To evaluate powi(x,n), the floating point value x raised to the
2842    constant integer exponent n, we use a hybrid algorithm that
2843    combines the "window method" with look-up tables.  For an
2844    introduction to exponentiation algorithms and "addition chains",
2845    see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2846    "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2847    3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2848    Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998.  */
2849
2850 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2851    multiplications to inline before calling the system library's pow
2852    function.  powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2853    so this default never requires calling pow, powf or powl.  */
2854
2855 #ifndef POWI_MAX_MULTS
2856 #define POWI_MAX_MULTS  (2*HOST_BITS_PER_WIDE_INT-2)
2857 #endif
2858
2859 /* The size of the "optimal power tree" lookup table.  All
2860    exponents less than this value are simply looked up in the
2861    powi_table below.  This threshold is also used to size the
2862    cache of pseudo registers that hold intermediate results.  */
2863 #define POWI_TABLE_SIZE 256
2864
2865 /* The size, in bits of the window, used in the "window method"
2866    exponentiation algorithm.  This is equivalent to a radix of
2867    (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method".  */
2868 #define POWI_WINDOW_SIZE 3
2869
2870 /* The following table is an efficient representation of an
2871    "optimal power tree".  For each value, i, the corresponding
2872    value, j, in the table states than an optimal evaluation
2873    sequence for calculating pow(x,i) can be found by evaluating
2874    pow(x,j)*pow(x,i-j).  An optimal power tree for the first
2875    100 integers is given in Knuth's "Seminumerical algorithms".  */
2876
2877 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2878   {
2879       0,   1,   1,   2,   2,   3,   3,   4,  /*   0 -   7 */
2880       4,   6,   5,   6,   6,  10,   7,   9,  /*   8 -  15 */
2881       8,  16,   9,  16,  10,  12,  11,  13,  /*  16 -  23 */
2882      12,  17,  13,  18,  14,  24,  15,  26,  /*  24 -  31 */
2883      16,  17,  17,  19,  18,  33,  19,  26,  /*  32 -  39 */
2884      20,  25,  21,  40,  22,  27,  23,  44,  /*  40 -  47 */
2885      24,  32,  25,  34,  26,  29,  27,  44,  /*  48 -  55 */
2886      28,  31,  29,  34,  30,  60,  31,  36,  /*  56 -  63 */
2887      32,  64,  33,  34,  34,  46,  35,  37,  /*  64 -  71 */
2888      36,  65,  37,  50,  38,  48,  39,  69,  /*  72 -  79 */
2889      40,  49,  41,  43,  42,  51,  43,  58,  /*  80 -  87 */
2890      44,  64,  45,  47,  46,  59,  47,  76,  /*  88 -  95 */
2891      48,  65,  49,  66,  50,  67,  51,  66,  /*  96 - 103 */
2892      52,  70,  53,  74,  54, 104,  55,  74,  /* 104 - 111 */
2893      56,  64,  57,  69,  58,  78,  59,  68,  /* 112 - 119 */
2894      60,  61,  61,  80,  62,  75,  63,  68,  /* 120 - 127 */
2895      64,  65,  65, 128,  66, 129,  67,  90,  /* 128 - 135 */
2896      68,  73,  69, 131,  70,  94,  71,  88,  /* 136 - 143 */
2897      72, 128,  73,  98,  74, 132,  75, 121,  /* 144 - 151 */
2898      76, 102,  77, 124,  78, 132,  79, 106,  /* 152 - 159 */
2899      80,  97,  81, 160,  82,  99,  83, 134,  /* 160 - 167 */
2900      84,  86,  85,  95,  86, 160,  87, 100,  /* 168 - 175 */
2901      88, 113,  89,  98,  90, 107,  91, 122,  /* 176 - 183 */
2902      92, 111,  93, 102,  94, 126,  95, 150,  /* 184 - 191 */
2903      96, 128,  97, 130,  98, 133,  99, 195,  /* 192 - 199 */
2904     100, 128, 101, 123, 102, 164, 103, 138,  /* 200 - 207 */
2905     104, 145, 105, 146, 106, 109, 107, 149,  /* 208 - 215 */
2906     108, 200, 109, 146, 110, 170, 111, 157,  /* 216 - 223 */
2907     112, 128, 113, 130, 114, 182, 115, 132,  /* 224 - 231 */
2908     116, 200, 117, 132, 118, 158, 119, 206,  /* 232 - 239 */
2909     120, 240, 121, 162, 122, 147, 123, 152,  /* 240 - 247 */
2910     124, 166, 125, 214, 126, 138, 127, 153,  /* 248 - 255 */
2911   };
2912
2913
2914 /* Return the number of multiplications required to calculate
2915    powi(x,n) where n is less than POWI_TABLE_SIZE.  This is a
2916    subroutine of powi_cost.  CACHE is an array indicating
2917    which exponents have already been calculated.  */
2918
2919 static int
2920 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2921 {
2922   /* If we've already calculated this exponent, then this evaluation
2923      doesn't require any additional multiplications.  */
2924   if (cache[n])
2925     return 0;
2926
2927   cache[n] = true;
2928   return powi_lookup_cost (n - powi_table[n], cache)
2929          + powi_lookup_cost (powi_table[n], cache) + 1;
2930 }
2931
2932 /* Return the number of multiplications required to calculate
2933    powi(x,n) for an arbitrary x, given the exponent N.  This
2934    function needs to be kept in sync with expand_powi below.  */
2935
2936 static int
2937 powi_cost (HOST_WIDE_INT n)
2938 {
2939   bool cache[POWI_TABLE_SIZE];
2940   unsigned HOST_WIDE_INT digit;
2941   unsigned HOST_WIDE_INT val;
2942   int result;
2943
2944   if (n == 0)
2945     return 0;
2946
2947   /* Ignore the reciprocal when calculating the cost.  */
2948   val = (n < 0) ? -n : n;
2949
2950   /* Initialize the exponent cache.  */
2951   memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2952   cache[1] = true;
2953
2954   result = 0;
2955
2956   while (val >= POWI_TABLE_SIZE)
2957     {
2958       if (val & 1)
2959         {
2960           digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2961           result += powi_lookup_cost (digit, cache)
2962                     + POWI_WINDOW_SIZE + 1;
2963           val >>= POWI_WINDOW_SIZE;
2964         }
2965       else
2966         {
2967           val >>= 1;
2968           result++;
2969         }
2970     }
2971
2972   return result + powi_lookup_cost (val, cache);
2973 }
2974
2975 /* Recursive subroutine of expand_powi.  This function takes the array,
2976    CACHE, of already calculated exponents and an exponent N and returns
2977    an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE.  */
2978
2979 static rtx
2980 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2981 {
2982   unsigned HOST_WIDE_INT digit;
2983   rtx target, result;
2984   rtx op0, op1;
2985
2986   if (n < POWI_TABLE_SIZE)
2987     {
2988       if (cache[n])
2989         return cache[n];
2990
2991       target = gen_reg_rtx (mode);
2992       cache[n] = target;
2993
2994       op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2995       op1 = expand_powi_1 (mode, powi_table[n], cache);
2996     }
2997   else if (n & 1)
2998     {
2999       target = gen_reg_rtx (mode);
3000       digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
3001       op0 = expand_powi_1 (mode, n - digit, cache);
3002       op1 = expand_powi_1 (mode, digit, cache);
3003     }
3004   else
3005     {
3006       target = gen_reg_rtx (mode);
3007       op0 = expand_powi_1 (mode, n >> 1, cache);
3008       op1 = op0;
3009     }
3010
3011   result = expand_mult (mode, op0, op1, target, 0);
3012   if (result != target)
3013     emit_move_insn (target, result);
3014   return target;
3015 }
3016
3017 /* Expand the RTL to evaluate powi(x,n) in mode MODE.  X is the
3018    floating point operand in mode MODE, and N is the exponent.  This
3019    function needs to be kept in sync with powi_cost above.  */
3020
3021 static rtx
3022 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
3023 {
3024   rtx cache[POWI_TABLE_SIZE];
3025   rtx result;
3026
3027   if (n == 0)
3028     return CONST1_RTX (mode);
3029
3030   memset (cache, 0, sizeof (cache));
3031   cache[1] = x;
3032
3033   result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
3034
3035   /* If the original exponent was negative, reciprocate the result.  */
3036   if (n < 0)
3037     result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3038                            result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3039
3040   return result;
3041 }
3042
3043 /* Fold a builtin function call to pow, powf, or powl into a series of sqrts or
3044    cbrts.  Return NULL_RTX if no simplification can be made or expand the tree
3045    if we can simplify it.  */
3046 static rtx
3047 expand_builtin_pow_root (location_t loc, tree arg0, tree arg1, tree type,
3048                          rtx subtarget)
3049 {
3050   if (TREE_CODE (arg1) == REAL_CST
3051       && !TREE_OVERFLOW (arg1)
3052       && flag_unsafe_math_optimizations)
3053     {
3054       enum machine_mode mode = TYPE_MODE (type);
3055       tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
3056       tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
3057       REAL_VALUE_TYPE c = TREE_REAL_CST (arg1);
3058       tree op = NULL_TREE;
3059
3060       if (sqrtfn)
3061         {
3062           /* Optimize pow (x, 0.5) into sqrt.  */
3063           if (REAL_VALUES_EQUAL (c, dconsthalf))
3064             op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3065
3066           else
3067             {
3068               REAL_VALUE_TYPE dconst1_4 = dconst1;
3069               REAL_VALUE_TYPE dconst3_4;
3070               SET_REAL_EXP (&dconst1_4, REAL_EXP (&dconst1_4) - 2);
3071
3072               real_from_integer (&dconst3_4, VOIDmode, 3, 0, 0);
3073               SET_REAL_EXP (&dconst3_4, REAL_EXP (&dconst3_4) - 2);
3074
3075               /* Optimize pow (x, 0.25) into sqrt (sqrt (x)).  Assume on most
3076                  machines that a builtin sqrt instruction is smaller than a
3077                  call to pow with 0.25, so do this optimization even if
3078                  -Os.  */
3079               if (REAL_VALUES_EQUAL (c, dconst1_4))
3080                 {
3081                   op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3082                   op = build_call_nofold_loc (loc, sqrtfn, 1, op);
3083                 }
3084
3085               /* Optimize pow (x, 0.75) = sqrt (x) * sqrt (sqrt (x)) unless we
3086                  are optimizing for space.  */
3087               else if (optimize_insn_for_speed_p ()
3088                        && !TREE_SIDE_EFFECTS (arg0)
3089                        && REAL_VALUES_EQUAL (c, dconst3_4))
3090                 {
3091                   tree sqrt1 = build_call_expr_loc (loc, sqrtfn, 1, arg0);
3092                   tree sqrt2 = builtin_save_expr (sqrt1);
3093                   tree sqrt3 = build_call_expr_loc (loc, sqrtfn, 1, sqrt1);
3094                   op = fold_build2_loc (loc, MULT_EXPR, type, sqrt2, sqrt3);
3095                 }
3096             }
3097         }
3098
3099       /* Check whether we can do cbrt insstead of pow (x, 1./3.) and
3100          cbrt/sqrts instead of pow (x, 1./6.).  */
3101       if (cbrtfn && ! op
3102           && (tree_expr_nonnegative_p (arg0) || !HONOR_NANS (mode)))
3103         {
3104           /* First try 1/3.  */
3105           REAL_VALUE_TYPE dconst1_3
3106             = real_value_truncate (mode, dconst_third ());
3107
3108           if (REAL_VALUES_EQUAL (c, dconst1_3))
3109             op = build_call_nofold_loc (loc, cbrtfn, 1, arg0);
3110
3111               /* Now try 1/6.  */
3112           else if (optimize_insn_for_speed_p ())
3113             {
3114               REAL_VALUE_TYPE dconst1_6 = dconst1_3;
3115               SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1);
3116
3117               if (REAL_VALUES_EQUAL (c, dconst1_6))
3118                 {
3119                   op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
3120                   op = build_call_nofold_loc (loc, cbrtfn, 1, op);
3121                 }
3122             }
3123         }
3124
3125       if (op)
3126         return expand_expr (op, subtarget, mode, EXPAND_NORMAL);
3127     }
3128
3129   return NULL_RTX;
3130 }
3131
3132 /* Expand a call to the pow built-in mathematical function.  Return NULL_RTX if
3133    a normal call should be emitted rather than expanding the function
3134    in-line.  EXP is the expression that is a call to the builtin
3135    function; if convenient, the result should be placed in TARGET.  */
3136
3137 static rtx
3138 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
3139 {
3140   tree arg0, arg1;
3141   tree fn, narg0;
3142   tree type = TREE_TYPE (exp);
3143   REAL_VALUE_TYPE cint, c, c2;
3144   HOST_WIDE_INT n;
3145   rtx op, op2;
3146   enum machine_mode mode = TYPE_MODE (type);
3147
3148   if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
3149     return NULL_RTX;
3150
3151   arg0 = CALL_EXPR_ARG (exp, 0);
3152   arg1 = CALL_EXPR_ARG (exp, 1);
3153
3154   if (TREE_CODE (arg1) != REAL_CST
3155       || TREE_OVERFLOW (arg1))
3156     return expand_builtin_mathfn_2 (exp, target, subtarget);
3157
3158   /* Handle constant exponents.  */
3159
3160   /* For integer valued exponents we can expand to an optimal multiplication
3161      sequence using expand_powi.  */
3162   c = TREE_REAL_CST (arg1);
3163   n = real_to_integer (&c);
3164   real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3165   if (real_identical (&c, &cint)
3166       && ((n >= -1 && n <= 2)
3167           || (flag_unsafe_math_optimizations
3168               && optimize_insn_for_speed_p ()
3169               && powi_cost (n) <= POWI_MAX_MULTS)))
3170     {
3171       op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3172       if (n != 1)
3173         {
3174           op = force_reg (mode, op);
3175           op = expand_powi (op, mode, n);
3176         }
3177       return op;
3178     }
3179
3180   narg0 = builtin_save_expr (arg0);
3181
3182   /* If the exponent is not integer valued, check if it is half of an integer.
3183      In this case we can expand to sqrt (x) * x**(n/2).  */
3184   fn = mathfn_built_in (type, BUILT_IN_SQRT);
3185   if (fn != NULL_TREE)
3186     {
3187       real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
3188       n = real_to_integer (&c2);
3189       real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3190       if (real_identical (&c2, &cint)
3191           && ((flag_unsafe_math_optimizations
3192                && optimize_insn_for_speed_p ()
3193                && powi_cost (n/2) <= POWI_MAX_MULTS)
3194               /* Even the c == 0.5 case cannot be done unconditionally
3195                  when we need to preserve signed zeros, as
3196                  pow (-0, 0.5) is +0, while sqrt(-0) is -0.  */
3197               || (!HONOR_SIGNED_ZEROS (mode) && n == 1)
3198               /* For c == 1.5 we can assume that x * sqrt (x) is always
3199                  smaller than pow (x, 1.5) if sqrt will not be expanded
3200                  as a call.  */
3201               || (n == 3
3202                   && optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)))
3203         {
3204           tree call_expr = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 1,
3205                                                   narg0);
3206           /* Use expand_expr in case the newly built call expression
3207              was folded to a non-call.  */
3208           op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL);
3209           if (n != 1)
3210             {
3211               op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3212               op2 = force_reg (mode, op2);
3213               op2 = expand_powi (op2, mode, abs (n / 2));
3214               op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3215                                         0, OPTAB_LIB_WIDEN);
3216               /* If the original exponent was negative, reciprocate the
3217                  result.  */
3218               if (n < 0)
3219                 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3220                                    op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3221             }
3222           return op;
3223         }
3224     }
3225
3226   /* Check whether we can do a series of sqrt or cbrt's instead of the pow
3227      call.  */
3228   op = expand_builtin_pow_root (EXPR_LOCATION (exp), arg0, arg1, type,
3229                                 subtarget);
3230   if (op)
3231     return op;
3232
3233   /* Try if the exponent is a third of an integer.  In this case
3234      we can expand to x**(n/3) * cbrt(x)**(n%3).  As cbrt (x) is
3235      different from pow (x, 1./3.) due to rounding and behavior
3236      with negative x we need to constrain this transformation to
3237      unsafe math and positive x or finite math.  */
3238   fn = mathfn_built_in (type, BUILT_IN_CBRT);
3239   if (fn != NULL_TREE
3240       && flag_unsafe_math_optimizations
3241       && (tree_expr_nonnegative_p (arg0)
3242           || !HONOR_NANS (mode)))
3243     {
3244       REAL_VALUE_TYPE dconst3;
3245       real_from_integer (&dconst3, VOIDmode, 3, 0, 0);
3246       real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
3247       real_round (&c2, mode, &c2);
3248       n = real_to_integer (&c2);
3249       real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
3250       real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
3251       real_convert (&c2, mode, &c2);
3252       if (real_identical (&c2, &c)
3253           && ((optimize_insn_for_speed_p ()
3254                && powi_cost (n/3) <= POWI_MAX_MULTS)
3255               || n == 1))
3256         {
3257           tree call_expr = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 1,
3258                                                   narg0);
3259           op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
3260           if (abs (n) % 3 == 2)
3261             op = expand_simple_binop (mode, MULT, op, op, op,
3262                                       0, OPTAB_LIB_WIDEN);
3263           if (n != 1)
3264             {
3265               op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3266               op2 = force_reg (mode, op2);
3267               op2 = expand_powi (op2, mode, abs (n / 3));
3268               op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3269                                         0, OPTAB_LIB_WIDEN);
3270               /* If the original exponent was negative, reciprocate the
3271                  result.  */
3272               if (n < 0)
3273                 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3274                                    op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3275             }
3276           return op;
3277         }
3278     }
3279
3280   /* Fall back to optab expansion.  */
3281   return expand_builtin_mathfn_2 (exp, target, subtarget);
3282 }
3283
3284 /* Expand a call to the powi built-in mathematical function.  Return NULL_RTX if
3285    a normal call should be emitted rather than expanding the function
3286    in-line.  EXP is the expression that is a call to the builtin
3287    function; if convenient, the result should be placed in TARGET.  */
3288
3289 static rtx
3290 expand_builtin_powi (tree exp, rtx target)
3291 {
3292   tree arg0, arg1;
3293   rtx op0, op1;
3294   enum machine_mode mode;
3295   enum machine_mode mode2;
3296
3297   if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3298     return NULL_RTX;
3299
3300   arg0 = CALL_EXPR_ARG (exp, 0);
3301   arg1 = CALL_EXPR_ARG (exp, 1);
3302   mode = TYPE_MODE (TREE_TYPE (exp));
3303
3304   /* Handle constant power.  */
3305
3306   if (TREE_CODE (arg1) == INTEGER_CST
3307       && !TREE_OVERFLOW (arg1))
3308     {
3309       HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
3310
3311       /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
3312          Otherwise, check the number of multiplications required.  */
3313       if ((TREE_INT_CST_HIGH (arg1) == 0
3314            || TREE_INT_CST_HIGH (arg1) == -1)
3315           && ((n >= -1 && n <= 2)
3316               || (optimize_insn_for_speed_p ()
3317                   && powi_cost (n) <= POWI_MAX_MULTS)))
3318         {
3319           op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3320           op0 = force_reg (mode, op0);
3321           return expand_powi (op0, mode, n);
3322         }
3323     }
3324
3325   /* Emit a libcall to libgcc.  */
3326
3327   /* Mode of the 2nd argument must match that of an int.  */
3328   mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
3329
3330   if (target == NULL_RTX)
3331     target = gen_reg_rtx (mode);
3332
3333   op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3334   if (GET_MODE (op0) != mode)
3335     op0 = convert_to_mode (mode, op0, 0);
3336   op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3337   if (GET_MODE (op1) != mode2)
3338     op1 = convert_to_mode (mode2, op1, 0);
3339
3340   target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3341                                     target, LCT_CONST, mode, 2,
3342                                     op0, mode, op1, mode2);
3343
3344   return target;
3345 }
3346
3347 /* Expand expression EXP which is a call to the strlen builtin.  Return
3348    NULL_RTX if we failed the caller should emit a normal call, otherwise
3349    try to get the result in TARGET, if convenient.  */
3350
3351 static rtx
3352 expand_builtin_strlen (tree exp, rtx target,
3353                        enum machine_mode target_mode)
3354 {
3355   if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3356     return NULL_RTX;
3357   else
3358     {
3359       rtx pat;
3360       tree len;
3361       tree src = CALL_EXPR_ARG (exp, 0);
3362       rtx result, src_reg, char_rtx, before_strlen;
3363       enum machine_mode insn_mode = target_mode, char_mode;
3364       enum insn_code icode = CODE_FOR_nothing;
3365       unsigned int align;
3366
3367       /* If the length can be computed at compile-time, return it.  */
3368       len = c_strlen (src, 0);
3369       if (len)
3370         return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3371
3372       /* If the length can be computed at compile-time and is constant
3373          integer, but there are side-effects in src, evaluate
3374          src for side-effects, then return len.
3375          E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3376          can be optimized into: i++; x = 3;  */
3377       len = c_strlen (src, 1);
3378       if (len && TREE_CODE (len) == INTEGER_CST)
3379         {
3380           expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3381           return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3382         }
3383
3384       align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3385
3386       /* If SRC is not a pointer type, don't do this operation inline.  */
3387       if (align == 0)
3388         return NULL_RTX;
3389
3390       /* Bail out if we can't compute strlen in the right mode.  */
3391       while (insn_mode != VOIDmode)
3392         {
3393           icode = optab_handler (strlen_optab, insn_mode);
3394           if (icode != CODE_FOR_nothing)
3395             break;
3396
3397           insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3398         }
3399       if (insn_mode == VOIDmode)
3400         return NULL_RTX;
3401
3402       /* Make a place to write the result of the instruction.  */
3403       result = target;
3404       if (! (result != 0
3405              && REG_P (result)
3406              && GET_MODE (result) == insn_mode
3407              && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3408         result = gen_reg_rtx (insn_mode);
3409
3410       /* Make a place to hold the source address.  We will not expand
3411          the actual source until we are sure that the expansion will
3412          not fail -- there are trees that cannot be expanded twice.  */
3413       src_reg = gen_reg_rtx (Pmode);
3414
3415       /* Mark the beginning of the strlen sequence so we can emit the
3416          source operand later.  */
3417       before_strlen = get_last_insn ();
3418
3419       char_rtx = const0_rtx;
3420       char_mode = insn_data[(int) icode].operand[2].mode;
3421       if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
3422                                                             char_mode))
3423         char_rtx = copy_to_mode_reg (char_mode, char_rtx);
3424
3425       pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
3426                              char_rtx, GEN_INT (align));
3427       if (! pat)
3428         return NULL_RTX;
3429       emit_insn (pat);
3430
3431       /* Now that we are assured of success, expand the source.  */
3432       start_sequence ();
3433       pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3434       if (pat != src_reg)
3435         emit_move_insn (src_reg, pat);
3436       pat = get_insns ();
3437       end_sequence ();
3438
3439       if (before_strlen)
3440         emit_insn_after (pat, before_strlen);
3441       else
3442         emit_insn_before (pat, get_insns ());
3443
3444       /* Return the value in the proper mode for this function.  */
3445       if (GET_MODE (result) == target_mode)
3446         target = result;
3447       else if (target != 0)
3448         convert_move (target, result, 0);
3449       else
3450         target = convert_to_mode (target_mode, result, 0);
3451
3452       return target;
3453     }
3454 }
3455
3456 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
3457    bytes from constant string DATA + OFFSET and return it as target
3458    constant.  */
3459
3460 static rtx
3461 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3462                          enum machine_mode mode)
3463 {
3464   const char *str = (const char *) data;
3465
3466   gcc_assert (offset >= 0
3467               && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3468                   <= strlen (str) + 1));
3469
3470   return c_readstr (str + offset, mode);
3471 }
3472
3473 /* Expand a call EXP to the memcpy builtin.
3474    Return NULL_RTX if we failed, the caller should emit a normal call,
3475    otherwise try to get the result in TARGET, if convenient (and in
3476    mode MODE if that's convenient).  */
3477
3478 static rtx
3479 expand_builtin_memcpy (tree exp, rtx target)
3480 {
3481   if (!validate_arglist (exp,
3482                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3483     return NULL_RTX;
3484   else
3485     {
3486       tree dest = CALL_EXPR_ARG (exp, 0);
3487       tree src = CALL_EXPR_ARG (exp, 1);
3488       tree len = CALL_EXPR_ARG (exp, 2);
3489       const char *src_str;
3490       unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3491       unsigned int dest_align
3492         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3493       rtx dest_mem, src_mem, dest_addr, len_rtx;
3494       HOST_WIDE_INT expected_size = -1;
3495       unsigned int expected_align = 0;
3496
3497       /* If DEST is not a pointer type, call the normal function.  */
3498       if (dest_align == 0)
3499         return NULL_RTX;
3500
3501       /* If either SRC is not a pointer type, don't do this
3502          operation in-line.  */
3503       if (src_align == 0)
3504         return NULL_RTX;
3505
3506       if (currently_expanding_gimple_stmt)
3507         stringop_block_profile (currently_expanding_gimple_stmt,
3508                                 &expected_align, &expected_size);
3509
3510       if (expected_align < dest_align)
3511         expected_align = dest_align;
3512       dest_mem = get_memory_rtx (dest, len);
3513       set_mem_align (dest_mem, dest_align);
3514       len_rtx = expand_normal (len);
3515       src_str = c_getstr (src);
3516
3517       /* If SRC is a string constant and block move would be done
3518          by pieces, we can avoid loading the string from memory
3519          and only stored the computed constants.  */
3520       if (src_str
3521           && CONST_INT_P (len_rtx)
3522           && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3523           && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3524                                   CONST_CAST (char *, src_str),
3525                                   dest_align, false))
3526         {
3527           dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3528                                       builtin_memcpy_read_str,
3529                                       CONST_CAST (char *, src_str),
3530                                       dest_align, false, 0);
3531           dest_mem = force_operand (XEXP (dest_mem, 0), target);
3532           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3533           return dest_mem;
3534         }
3535
3536       src_mem = get_memory_rtx (src, len);
3537       set_mem_align (src_mem, src_align);
3538
3539       /* Copy word part most expediently.  */
3540       dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3541                                          CALL_EXPR_TAILCALL (exp)
3542                                          ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3543                                          expected_align, expected_size);
3544
3545       if (dest_addr == 0)
3546         {
3547           dest_addr = force_operand (XEXP (dest_mem, 0), target);
3548           dest_addr = convert_memory_address (ptr_mode, dest_addr);
3549         }
3550       return dest_addr;
3551     }
3552 }
3553
3554 /* Expand a call EXP to the mempcpy builtin.
3555    Return NULL_RTX if we failed; the caller should emit a normal call,
3556    otherwise try to get the result in TARGET, if convenient (and in
3557    mode MODE if that's convenient).  If ENDP is 0 return the
3558    destination pointer, if ENDP is 1 return the end pointer ala
3559    mempcpy, and if ENDP is 2 return the end pointer minus one ala
3560    stpcpy.  */
3561
3562 static rtx
3563 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3564 {
3565   if (!validate_arglist (exp,
3566                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3567     return NULL_RTX;
3568   else
3569     {
3570       tree dest = CALL_EXPR_ARG (exp, 0);
3571       tree src = CALL_EXPR_ARG (exp, 1);
3572       tree len = CALL_EXPR_ARG (exp, 2);
3573       return expand_builtin_mempcpy_args (dest, src, len,
3574                                           target, mode, /*endp=*/ 1);
3575     }
3576 }
3577
3578 /* Helper function to do the actual work for expand_builtin_mempcpy.  The
3579    arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3580    so that this can also be called without constructing an actual CALL_EXPR.
3581    The other arguments and return value are the same as for
3582    expand_builtin_mempcpy.  */
3583
3584 static rtx
3585 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3586                              rtx target, enum machine_mode mode, int endp)
3587 {
3588     /* If return value is ignored, transform mempcpy into memcpy.  */
3589   if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
3590     {
3591       tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3592       tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3593                                            dest, src, len);
3594       return expand_expr (result, target, mode, EXPAND_NORMAL);
3595     }
3596   else
3597     {
3598       const char *src_str;
3599       unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3600       unsigned int dest_align
3601         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3602       rtx dest_mem, src_mem, len_rtx;
3603
3604       /* If either SRC or DEST is not a pointer type, don't do this
3605          operation in-line.  */
3606       if (dest_align == 0 || src_align == 0)
3607         return NULL_RTX;
3608
3609       /* If LEN is not constant, call the normal function.  */
3610       if (! host_integerp (len, 1))
3611         return NULL_RTX;
3612
3613       len_rtx = expand_normal (len);
3614       src_str = c_getstr (src);
3615
3616       /* If SRC is a string constant and block move would be done
3617          by pieces, we can avoid loading the string from memory
3618          and only stored the computed constants.  */
3619       if (src_str
3620           && CONST_INT_P (len_rtx)
3621           && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3622           && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3623                                   CONST_CAST (char *, src_str),
3624                                   dest_align, false))
3625         {
3626           dest_mem = get_memory_rtx (dest, len);
3627           set_mem_align (dest_mem, dest_align);
3628           dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3629                                       builtin_memcpy_read_str,
3630                                       CONST_CAST (char *, src_str),
3631                                       dest_align, false, endp);
3632           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3633           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3634           return dest_mem;
3635         }
3636
3637       if (CONST_INT_P (len_rtx)
3638           && can_move_by_pieces (INTVAL (len_rtx),
3639                                  MIN (dest_align, src_align)))
3640         {
3641           dest_mem = get_memory_rtx (dest, len);
3642           set_mem_align (dest_mem, dest_align);
3643           src_mem = get_memory_rtx (src, len);
3644           set_mem_align (src_mem, src_align);
3645           dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3646                                      MIN (dest_align, src_align), endp);
3647           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3648           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3649           return dest_mem;
3650         }
3651
3652       return NULL_RTX;
3653     }
3654 }
3655
3656 #ifndef HAVE_movstr
3657 # define HAVE_movstr 0
3658 # define CODE_FOR_movstr CODE_FOR_nothing
3659 #endif
3660
3661 /* Expand into a movstr instruction, if one is available.  Return NULL_RTX if
3662    we failed, the caller should emit a normal call, otherwise try to
3663    get the result in TARGET, if convenient.  If ENDP is 0 return the
3664    destination pointer, if ENDP is 1 return the end pointer ala
3665    mempcpy, and if ENDP is 2 return the end pointer minus one ala
3666    stpcpy.  */
3667
3668 static rtx
3669 expand_movstr (tree dest, tree src, rtx target, int endp)
3670 {
3671   rtx end;
3672   rtx dest_mem;
3673   rtx src_mem;
3674   rtx insn;
3675   const struct insn_data_d * data;
3676
3677   if (!HAVE_movstr)
3678     return NULL_RTX;
3679
3680   dest_mem = get_memory_rtx (dest, NULL);
3681   src_mem = get_memory_rtx (src, NULL);
3682   data = insn_data + CODE_FOR_movstr;
3683   if (!endp)
3684     {
3685       target = force_reg (Pmode, XEXP (dest_mem, 0));
3686       dest_mem = replace_equiv_address (dest_mem, target);
3687       end = gen_reg_rtx (Pmode);
3688     }
3689   else
3690     {
3691       if (target == 0
3692           || target == const0_rtx
3693           || ! (*data->operand[0].predicate) (target, Pmode))
3694         {
3695           end = gen_reg_rtx (Pmode);
3696           if (target != const0_rtx)
3697             target = end;
3698         }
3699       else
3700         end = target;
3701     }
3702
3703   if (data->operand[0].mode != VOIDmode)
3704     end = gen_lowpart (data->operand[0].mode, end);
3705
3706   insn = data->genfun (end, dest_mem, src_mem);
3707
3708   gcc_assert (insn);
3709
3710   emit_insn (insn);
3711
3712   /* movstr is supposed to set end to the address of the NUL
3713      terminator.  If the caller requested a mempcpy-like return value,
3714      adjust it.  */
3715   if (endp == 1 && target != const0_rtx)
3716     {
3717       rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3718       emit_move_insn (target, force_operand (tem, NULL_RTX));
3719     }
3720
3721   return target;
3722 }
3723
3724 /* Expand expression EXP, which is a call to the strcpy builtin.  Return
3725    NULL_RTX if we failed the caller should emit a normal call, otherwise
3726    try to get the result in TARGET, if convenient (and in mode MODE if that's
3727    convenient).  */
3728
3729 static rtx
3730 expand_builtin_strcpy (tree exp, rtx target)
3731 {
3732   if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3733    {
3734      tree dest = CALL_EXPR_ARG (exp, 0);
3735      tree src = CALL_EXPR_ARG (exp, 1);
3736      return expand_builtin_strcpy_args (dest, src, target);
3737    }
3738    return NULL_RTX;
3739 }
3740
3741 /* Helper function to do the actual work for expand_builtin_strcpy.  The
3742    arguments to the builtin_strcpy call DEST and SRC are broken out
3743    so that this can also be called without constructing an actual CALL_EXPR.
3744    The other arguments and return value are the same as for
3745    expand_builtin_strcpy.  */
3746
3747 static rtx
3748 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3749 {
3750   return expand_movstr (dest, src, target, /*endp=*/0);
3751 }
3752
3753 /* Expand a call EXP to the stpcpy builtin.
3754    Return NULL_RTX if we failed the caller should emit a normal call,
3755    otherwise try to get the result in TARGET, if convenient (and in
3756    mode MODE if that's convenient).  */
3757
3758 static rtx
3759 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3760 {
3761   tree dst, src;
3762   location_t loc = EXPR_LOCATION (exp);
3763
3764   if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3765     return NULL_RTX;
3766
3767   dst = CALL_EXPR_ARG (exp, 0);
3768   src = CALL_EXPR_ARG (exp, 1);
3769
3770   /* If return value is ignored, transform stpcpy into strcpy.  */
3771   if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
3772     {
3773       tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3774       tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3775       return expand_expr (result, target, mode, EXPAND_NORMAL);
3776     }
3777   else
3778     {
3779       tree len, lenp1;
3780       rtx ret;
3781
3782       /* Ensure we get an actual string whose length can be evaluated at
3783          compile-time, not an expression containing a string.  This is
3784          because the latter will potentially produce pessimized code
3785          when used to produce the return value.  */
3786       if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3787         return expand_movstr (dst, src, target, /*endp=*/2);
3788
3789       lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3790       ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3791                                          target, mode, /*endp=*/2);
3792
3793       if (ret)
3794         return ret;
3795
3796       if (TREE_CODE (len) == INTEGER_CST)
3797         {
3798           rtx len_rtx = expand_normal (len);
3799
3800           if (CONST_INT_P (len_rtx))
3801             {
3802               ret = expand_builtin_strcpy_args (dst, src, target);
3803
3804               if (ret)
3805                 {
3806                   if (! target)
3807                     {
3808                       if (mode != VOIDmode)
3809                         target = gen_reg_rtx (mode);
3810                       else
3811                         target = gen_reg_rtx (GET_MODE (ret));
3812                     }
3813                   if (GET_MODE (target) != GET_MODE (ret))
3814                     ret = gen_lowpart (GET_MODE (target), ret);
3815
3816                   ret = plus_constant (ret, INTVAL (len_rtx));
3817                   ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3818                   gcc_assert (ret);
3819
3820                   return target;
3821                 }
3822             }
3823         }
3824
3825       return expand_movstr (dst, src, target, /*endp=*/2);
3826     }
3827 }
3828
3829 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
3830    bytes from constant string DATA + OFFSET and return it as target
3831    constant.  */
3832
3833 rtx
3834 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3835                           enum machine_mode mode)
3836 {
3837   const char *str = (const char *) data;
3838
3839   if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3840     return const0_rtx;
3841
3842   return c_readstr (str + offset, mode);
3843 }
3844
3845 /* Expand expression EXP, which is a call to the strncpy builtin.  Return
3846    NULL_RTX if we failed the caller should emit a normal call.  */
3847
3848 static rtx
3849 expand_builtin_strncpy (tree exp, rtx target)
3850 {
3851   location_t loc = EXPR_LOCATION (exp);
3852
3853   if (validate_arglist (exp,
3854                         POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3855     {
3856       tree dest = CALL_EXPR_ARG (exp, 0);
3857       tree src = CALL_EXPR_ARG (exp, 1);
3858       tree len = CALL_EXPR_ARG (exp, 2);
3859       tree slen = c_strlen (src, 1);
3860
3861       /* We must be passed a constant len and src parameter.  */
3862       if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3863         return NULL_RTX;
3864
3865       slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3866
3867       /* We're required to pad with trailing zeros if the requested
3868          len is greater than strlen(s2)+1.  In that case try to
3869          use store_by_pieces, if it fails, punt.  */
3870       if (tree_int_cst_lt (slen, len))
3871         {
3872           unsigned int dest_align
3873             = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3874           const char *p = c_getstr (src);
3875           rtx dest_mem;
3876
3877           if (!p || dest_align == 0 || !host_integerp (len, 1)
3878               || !can_store_by_pieces (tree_low_cst (len, 1),
3879                                        builtin_strncpy_read_str,
3880                                        CONST_CAST (char *, p),
3881                                        dest_align, false))
3882             return NULL_RTX;
3883
3884           dest_mem = get_memory_rtx (dest, len);
3885           store_by_pieces (dest_mem, tree_low_cst (len, 1),
3886                            builtin_strncpy_read_str,
3887                            CONST_CAST (char *, p), dest_align, false, 0);
3888           dest_mem = force_operand (XEXP (dest_mem, 0), target);
3889           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3890           return dest_mem;
3891         }
3892     }
3893   return NULL_RTX;
3894 }
3895
3896 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
3897    bytes from constant string DATA + OFFSET and return it as target
3898    constant.  */
3899
3900 rtx
3901 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3902                          enum machine_mode mode)
3903 {
3904   const char *c = (const char *) data;
3905   char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3906
3907   memset (p, *c, GET_MODE_SIZE (mode));
3908
3909   return c_readstr (p, mode);
3910 }
3911
3912 /* Callback routine for store_by_pieces.  Return the RTL of a register
3913    containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3914    char value given in the RTL register data.  For example, if mode is
3915    4 bytes wide, return the RTL for 0x01010101*data.  */
3916
3917 static rtx
3918 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3919                         enum machine_mode mode)
3920 {
3921   rtx target, coeff;
3922   size_t size;
3923   char *p;
3924
3925   size = GET_MODE_SIZE (mode);
3926   if (size == 1)
3927     return (rtx) data;
3928
3929   p = XALLOCAVEC (char, size);
3930   memset (p, 1, size);
3931   coeff = c_readstr (p, mode);
3932
3933   target = convert_to_mode (mode, (rtx) data, 1);
3934   target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3935   return force_reg (mode, target);
3936 }
3937
3938 /* Expand expression EXP, which is a call to the memset builtin.  Return
3939    NULL_RTX if we failed the caller should emit a normal call, otherwise
3940    try to get the result in TARGET, if convenient (and in mode MODE if that's
3941    convenient).  */
3942
3943 static rtx
3944 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3945 {
3946   if (!validate_arglist (exp,
3947                          POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3948     return NULL_RTX;
3949   else
3950     {
3951       tree dest = CALL_EXPR_ARG (exp, 0);
3952       tree val = CALL_EXPR_ARG (exp, 1);
3953       tree len = CALL_EXPR_ARG (exp, 2);
3954       return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3955     }
3956 }
3957
3958 /* Helper function to do the actual work for expand_builtin_memset.  The
3959    arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3960    so that this can also be called without constructing an actual CALL_EXPR.
3961    The other arguments and return value are the same as for
3962    expand_builtin_memset.  */
3963
3964 static rtx
3965 expand_builtin_memset_args (tree dest, tree val, tree len,
3966                             rtx target, enum machine_mode mode, tree orig_exp)
3967 {
3968   tree fndecl, fn;
3969   enum built_in_function fcode;
3970   char c;
3971   unsigned int dest_align;
3972   rtx dest_mem, dest_addr, len_rtx;
3973   HOST_WIDE_INT expected_size = -1;
3974   unsigned int expected_align = 0;
3975
3976   dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3977
3978   /* If DEST is not a pointer type, don't do this operation in-line.  */
3979   if (dest_align == 0)
3980     return NULL_RTX;
3981
3982   if (currently_expanding_gimple_stmt)
3983     stringop_block_profile (currently_expanding_gimple_stmt,
3984                             &expected_align, &expected_size);
3985
3986   if (expected_align < dest_align)
3987     expected_align = dest_align;
3988
3989   /* If the LEN parameter is zero, return DEST.  */
3990   if (integer_zerop (len))
3991     {
3992       /* Evaluate and ignore VAL in case it has side-effects.  */
3993       expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3994       return expand_expr (dest, target, mode, EXPAND_NORMAL);
3995     }
3996
3997   /* Stabilize the arguments in case we fail.  */
3998   dest = builtin_save_expr (dest);
3999   val = builtin_save_expr (val);
4000   len = builtin_save_expr (len);
4001
4002   len_rtx = expand_normal (len);
4003   dest_mem = get_memory_rtx (dest, len);
4004
4005   if (TREE_CODE (val) != INTEGER_CST)
4006     {
4007       rtx val_rtx;
4008
4009       val_rtx = expand_normal (val);
4010       val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
4011                                  val_rtx, 0);
4012
4013       /* Assume that we can memset by pieces if we can store
4014        * the coefficients by pieces (in the required modes).
4015        * We can't pass builtin_memset_gen_str as that emits RTL.  */
4016       c = 1;
4017       if (host_integerp (len, 1)
4018           && can_store_by_pieces (tree_low_cst (len, 1),
4019                                   builtin_memset_read_str, &c, dest_align,
4020                                   true))
4021         {
4022           val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
4023                                val_rtx);
4024           store_by_pieces (dest_mem, tree_low_cst (len, 1),
4025                            builtin_memset_gen_str, val_rtx, dest_align,
4026                            true, 0);
4027         }
4028       else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4029                                         dest_align, expected_align,
4030                                         expected_size))
4031         goto do_libcall;
4032
4033       dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4034       dest_mem = convert_memory_address (ptr_mode, dest_mem);
4035       return dest_mem;
4036     }
4037
4038   if (target_char_cast (val, &c))
4039     goto do_libcall;
4040
4041   if (c)
4042     {
4043       if (host_integerp (len, 1)
4044           && can_store_by_pieces (tree_low_cst (len, 1),
4045                                   builtin_memset_read_str, &c, dest_align,
4046                                   true))
4047         store_by_pieces (dest_mem, tree_low_cst (len, 1),
4048                          builtin_memset_read_str, &c, dest_align, true, 0);
4049       else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
4050                                         dest_align, expected_align,
4051                                         expected_size))
4052         goto do_libcall;
4053
4054       dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4055       dest_mem = convert_memory_address (ptr_mode, dest_mem);
4056       return dest_mem;
4057     }
4058
4059   set_mem_align (dest_mem, dest_align);
4060   dest_addr = clear_storage_hints (dest_mem, len_rtx,
4061                                    CALL_EXPR_TAILCALL (orig_exp)
4062                                    ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4063                                    expected_align, expected_size);
4064
4065   if (dest_addr == 0)
4066     {
4067       dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4068       dest_addr = convert_memory_address (ptr_mode, dest_addr);
4069     }
4070
4071   return dest_addr;
4072
4073  do_libcall:
4074   fndecl = get_callee_fndecl (orig_exp);
4075   fcode = DECL_FUNCTION_CODE (fndecl);
4076   if (fcode == BUILT_IN_MEMSET)
4077     fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4078                                 dest, val, len);
4079   else if (fcode == BUILT_IN_BZERO)
4080     fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4081                                 dest, len);
4082   else
4083     gcc_unreachable ();
4084   gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4085   CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4086   return expand_call (fn, target, target == const0_rtx);
4087 }
4088
4089 /* Expand expression EXP, which is a call to the bzero builtin.  Return
4090    NULL_RTX if we failed the caller should emit a normal call.  */
4091
4092 static rtx
4093 expand_builtin_bzero (tree exp)
4094 {
4095   tree dest, size;
4096   location_t loc = EXPR_LOCATION (exp);
4097
4098   if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4099     return NULL_RTX;
4100
4101   dest = CALL_EXPR_ARG (exp, 0);
4102   size = CALL_EXPR_ARG (exp, 1);
4103
4104   /* New argument list transforming bzero(ptr x, int y) to
4105      memset(ptr x, int 0, size_t y).   This is done this way
4106      so that if it isn't expanded inline, we fallback to
4107      calling bzero instead of memset.  */
4108
4109   return expand_builtin_memset_args (dest, integer_zero_node,
4110                                      fold_convert_loc (loc, sizetype, size),
4111                                      const0_rtx, VOIDmode, exp);
4112 }
4113
4114 /* Expand expression EXP, which is a call to the memcmp built-in function.
4115    Return NULL_RTX if we failed and the
4116    caller should emit a normal call, otherwise try to get the result in
4117    TARGET, if convenient (and in mode MODE, if that's convenient).  */
4118
4119 static rtx
4120 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4121                        ATTRIBUTE_UNUSED enum machine_mode mode)
4122 {
4123   location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4124
4125   if (!validate_arglist (exp,
4126                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4127     return NULL_RTX;
4128
4129 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
4130   {
4131     rtx arg1_rtx, arg2_rtx, arg3_rtx;
4132     rtx result;
4133     rtx insn;
4134     tree arg1 = CALL_EXPR_ARG (exp, 0);
4135     tree arg2 = CALL_EXPR_ARG (exp, 1);
4136     tree len = CALL_EXPR_ARG (exp, 2);
4137
4138     unsigned int arg1_align
4139       = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4140     unsigned int arg2_align
4141       = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4142     enum machine_mode insn_mode;
4143
4144 #ifdef HAVE_cmpmemsi
4145     if (HAVE_cmpmemsi)
4146       insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
4147     else
4148 #endif
4149 #ifdef HAVE_cmpstrnsi
4150     if (HAVE_cmpstrnsi)
4151       insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4152     else
4153 #endif
4154       return NULL_RTX;
4155
4156     /* If we don't have POINTER_TYPE, call the function.  */
4157     if (arg1_align == 0 || arg2_align == 0)
4158       return NULL_RTX;
4159
4160     /* Make a place to write the result of the instruction.  */
4161     result = target;
4162     if (! (result != 0
4163            && REG_P (result) && GET_MODE (result) == insn_mode
4164            && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4165       result = gen_reg_rtx (insn_mode);
4166
4167     arg1_rtx = get_memory_rtx (arg1, len);
4168     arg2_rtx = get_memory_rtx (arg2, len);
4169     arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4170
4171     /* Set MEM_SIZE as appropriate.  */
4172     if (CONST_INT_P (arg3_rtx))
4173       {
4174         set_mem_size (arg1_rtx, arg3_rtx);
4175         set_mem_size (arg2_rtx, arg3_rtx);
4176       }
4177
4178 #ifdef HAVE_cmpmemsi
4179     if (HAVE_cmpmemsi)
4180       insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4181                            GEN_INT (MIN (arg1_align, arg2_align)));
4182     else
4183 #endif
4184 #ifdef HAVE_cmpstrnsi
4185     if (HAVE_cmpstrnsi)
4186       insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4187                             GEN_INT (MIN (arg1_align, arg2_align)));
4188     else
4189 #endif
4190       gcc_unreachable ();
4191
4192     if (insn)
4193       emit_insn (insn);
4194     else
4195       emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4196                                TYPE_MODE (integer_type_node), 3,
4197                                XEXP (arg1_rtx, 0), Pmode,
4198                                XEXP (arg2_rtx, 0), Pmode,
4199                                convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4200                                                 TYPE_UNSIGNED (sizetype)),
4201                                TYPE_MODE (sizetype));
4202
4203     /* Return the value in the proper mode for this function.  */
4204     mode = TYPE_MODE (TREE_TYPE (exp));
4205     if (GET_MODE (result) == mode)
4206       return result;
4207     else if (target != 0)
4208       {
4209         convert_move (target, result, 0);
4210         return target;
4211       }
4212     else
4213       return convert_to_mode (mode, result, 0);
4214   }
4215 #endif
4216
4217   return NULL_RTX;
4218 }
4219
4220 /* Expand expression EXP, which is a call to the strcmp builtin.  Return NULL_RTX
4221    if we failed the caller should emit a normal call, otherwise try to get
4222    the result in TARGET, if convenient.  */
4223
4224 static rtx
4225 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4226 {
4227   if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4228     return NULL_RTX;
4229
4230 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4231   if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
4232       || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
4233     {
4234       rtx arg1_rtx, arg2_rtx;
4235       rtx result, insn = NULL_RTX;
4236       tree fndecl, fn;
4237       tree arg1 = CALL_EXPR_ARG (exp, 0);
4238       tree arg2 = CALL_EXPR_ARG (exp, 1);
4239
4240       unsigned int arg1_align
4241         = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4242       unsigned int arg2_align
4243         = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4244
4245       /* If we don't have POINTER_TYPE, call the function.  */
4246       if (arg1_align == 0 || arg2_align == 0)
4247         return NULL_RTX;
4248
4249       /* Stabilize the arguments in case gen_cmpstr(n)si fail.  */
4250       arg1 = builtin_save_expr (arg1);
4251       arg2 = builtin_save_expr (arg2);
4252
4253       arg1_rtx = get_memory_rtx (arg1, NULL);
4254       arg2_rtx = get_memory_rtx (arg2, NULL);
4255
4256 #ifdef HAVE_cmpstrsi
4257       /* Try to call cmpstrsi.  */
4258       if (HAVE_cmpstrsi)
4259         {
4260           enum machine_mode insn_mode
4261             = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4262
4263           /* Make a place to write the result of the instruction.  */
4264           result = target;
4265           if (! (result != 0
4266                  && REG_P (result) && GET_MODE (result) == insn_mode
4267                  && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4268             result = gen_reg_rtx (insn_mode);
4269
4270           insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4271                                GEN_INT (MIN (arg1_align, arg2_align)));
4272         }
4273 #endif
4274 #ifdef HAVE_cmpstrnsi
4275       /* Try to determine at least one length and call cmpstrnsi.  */
4276       if (!insn && HAVE_cmpstrnsi)
4277         {
4278           tree len;
4279           rtx arg3_rtx;
4280
4281           enum machine_mode insn_mode
4282             = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4283           tree len1 = c_strlen (arg1, 1);
4284           tree len2 = c_strlen (arg2, 1);
4285
4286           if (len1)
4287             len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4288           if (len2)
4289             len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4290
4291           /* If we don't have a constant length for the first, use the length
4292              of the second, if we know it.  We don't require a constant for
4293              this case; some cost analysis could be done if both are available
4294              but neither is constant.  For now, assume they're equally cheap,
4295              unless one has side effects.  If both strings have constant lengths,
4296              use the smaller.  */
4297
4298           if (!len1)
4299             len = len2;
4300           else if (!len2)
4301             len = len1;
4302           else if (TREE_SIDE_EFFECTS (len1))
4303             len = len2;
4304           else if (TREE_SIDE_EFFECTS (len2))
4305             len = len1;
4306           else if (TREE_CODE (len1) != INTEGER_CST)
4307             len = len2;
4308           else if (TREE_CODE (len2) != INTEGER_CST)
4309             len = len1;
4310           else if (tree_int_cst_lt (len1, len2))
4311             len = len1;
4312           else
4313             len = len2;
4314
4315           /* If both arguments have side effects, we cannot optimize.  */
4316           if (!len || TREE_SIDE_EFFECTS (len))
4317             goto do_libcall;
4318
4319           arg3_rtx = expand_normal (len);
4320
4321           /* Make a place to write the result of the instruction.  */
4322           result = target;
4323           if (! (result != 0
4324                  && REG_P (result) && GET_MODE (result) == insn_mode
4325                  && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4326             result = gen_reg_rtx (insn_mode);
4327
4328           insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4329                                 GEN_INT (MIN (arg1_align, arg2_align)));
4330         }
4331 #endif
4332
4333       if (insn)
4334         {
4335           enum machine_mode mode;
4336           emit_insn (insn);
4337
4338           /* Return the value in the proper mode for this function.  */
4339           mode = TYPE_MODE (TREE_TYPE (exp));
4340           if (GET_MODE (result) == mode)
4341             return result;
4342           if (target == 0)
4343             return convert_to_mode (mode, result, 0);
4344           convert_move (target, result, 0);
4345           return target;
4346         }
4347
4348       /* Expand the library call ourselves using a stabilized argument
4349          list to avoid re-evaluating the function's arguments twice.  */
4350 #ifdef HAVE_cmpstrnsi
4351     do_libcall:
4352 #endif
4353       fndecl = get_callee_fndecl (exp);
4354       fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4355       gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4356       CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4357       return expand_call (fn, target, target == const0_rtx);
4358     }
4359 #endif
4360   return NULL_RTX;
4361 }
4362
4363 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4364    NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4365    the result in TARGET, if convenient.  */
4366
4367 static rtx
4368 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4369                         ATTRIBUTE_UNUSED enum machine_mode mode)
4370 {
4371   location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4372
4373   if (!validate_arglist (exp,
4374                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4375     return NULL_RTX;
4376
4377   /* If c_strlen can determine an expression for one of the string
4378      lengths, and it doesn't have side effects, then emit cmpstrnsi
4379      using length MIN(strlen(string)+1, arg3).  */
4380 #ifdef HAVE_cmpstrnsi
4381   if (HAVE_cmpstrnsi)
4382   {
4383     tree len, len1, len2;
4384     rtx arg1_rtx, arg2_rtx, arg3_rtx;
4385     rtx result, insn;
4386     tree fndecl, fn;
4387     tree arg1 = CALL_EXPR_ARG (exp, 0);
4388     tree arg2 = CALL_EXPR_ARG (exp, 1);
4389     tree arg3 = CALL_EXPR_ARG (exp, 2);
4390
4391     unsigned int arg1_align
4392       = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4393     unsigned int arg2_align
4394       = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4395     enum machine_mode insn_mode
4396       = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4397
4398     len1 = c_strlen (arg1, 1);
4399     len2 = c_strlen (arg2, 1);
4400
4401     if (len1)
4402       len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4403     if (len2)
4404       len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4405
4406     /* If we don't have a constant length for the first, use the length
4407        of the second, if we know it.  We don't require a constant for
4408        this case; some cost analysis could be done if both are available
4409        but neither is constant.  For now, assume they're equally cheap,
4410        unless one has side effects.  If both strings have constant lengths,
4411        use the smaller.  */
4412
4413     if (!len1)
4414       len = len2;
4415     else if (!len2)
4416       len = len1;
4417     else if (TREE_SIDE_EFFECTS (len1))
4418       len = len2;
4419     else if (TREE_SIDE_EFFECTS (len2))
4420       len = len1;
4421     else if (TREE_CODE (len1) != INTEGER_CST)
4422       len = len2;
4423     else if (TREE_CODE (len2) != INTEGER_CST)
4424       len = len1;
4425     else if (tree_int_cst_lt (len1, len2))
4426       len = len1;
4427     else
4428       len = len2;
4429
4430     /* If both arguments have side effects, we cannot optimize.  */
4431     if (!len || TREE_SIDE_EFFECTS (len))
4432       return NULL_RTX;
4433
4434     /* The actual new length parameter is MIN(len,arg3).  */
4435     len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4436                        fold_convert_loc (loc, TREE_TYPE (len), arg3));
4437
4438     /* If we don't have POINTER_TYPE, call the function.  */
4439     if (arg1_align == 0 || arg2_align == 0)
4440       return NULL_RTX;
4441
4442     /* Make a place to write the result of the instruction.  */
4443     result = target;
4444     if (! (result != 0
4445            && REG_P (result) && GET_MODE (result) == insn_mode
4446            && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4447       result = gen_reg_rtx (insn_mode);
4448
4449     /* Stabilize the arguments in case gen_cmpstrnsi fails.  */
4450     arg1 = builtin_save_expr (arg1);
4451     arg2 = builtin_save_expr (arg2);
4452     len = builtin_save_expr (len);
4453
4454     arg1_rtx = get_memory_rtx (arg1, len);
4455     arg2_rtx = get_memory_rtx (arg2, len);
4456     arg3_rtx = expand_normal (len);
4457     insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4458                           GEN_INT (MIN (arg1_align, arg2_align)));
4459     if (insn)
4460       {
4461         emit_insn (insn);
4462
4463         /* Return the value in the proper mode for this function.  */
4464         mode = TYPE_MODE (TREE_TYPE (exp));
4465         if (GET_MODE (result) == mode)
4466           return result;
4467         if (target == 0)
4468           return convert_to_mode (mode, result, 0);
4469         convert_move (target, result, 0);
4470         return target;
4471       }
4472
4473     /* Expand the library call ourselves using a stabilized argument
4474        list to avoid re-evaluating the function's arguments twice.  */
4475     fndecl = get_callee_fndecl (exp);
4476     fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4477                                 arg1, arg2, len);
4478     gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4479     CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4480     return expand_call (fn, target, target == const0_rtx);
4481   }
4482 #endif
4483   return NULL_RTX;
4484 }
4485
4486 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4487    if that's convenient.  */
4488
4489 rtx
4490 expand_builtin_saveregs (void)
4491 {
4492   rtx val, seq;
4493
4494   /* Don't do __builtin_saveregs more than once in a function.
4495      Save the result of the first call and reuse it.  */
4496   if (saveregs_value != 0)
4497     return saveregs_value;
4498
4499   /* When this function is called, it means that registers must be
4500      saved on entry to this function.  So we migrate the call to the
4501      first insn of this function.  */
4502
4503   start_sequence ();
4504
4505   /* Do whatever the machine needs done in this case.  */
4506   val = targetm.calls.expand_builtin_saveregs ();
4507
4508   seq = get_insns ();
4509   end_sequence ();
4510
4511   saveregs_value = val;
4512
4513   /* Put the insns after the NOTE that starts the function.  If this
4514      is inside a start_sequence, make the outer-level insn chain current, so
4515      the code is placed at the start of the function.  */
4516   push_topmost_sequence ();
4517   emit_insn_after (seq, entry_of_function ());
4518   pop_topmost_sequence ();
4519
4520   return val;
4521 }
4522
4523 /* Expand a call to __builtin_next_arg.  */
4524
4525 static rtx
4526 expand_builtin_next_arg (void)
4527 {
4528   /* Checking arguments is already done in fold_builtin_next_arg
4529      that must be called before this function.  */
4530   return expand_binop (ptr_mode, add_optab,
4531                        crtl->args.internal_arg_pointer,
4532                        crtl->args.arg_offset_rtx,
4533                        NULL_RTX, 0, OPTAB_LIB_WIDEN);
4534 }
4535
4536 /* Make it easier for the backends by protecting the valist argument
4537    from multiple evaluations.  */
4538
4539 static tree
4540 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4541 {
4542   tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4543
4544   /* The current way of determining the type of valist is completely
4545      bogus.  We should have the information on the va builtin instead.  */
4546   if (!vatype)
4547     vatype = targetm.fn_abi_va_list (cfun->decl);
4548
4549   if (TREE_CODE (vatype) == ARRAY_TYPE)
4550     {
4551       if (TREE_SIDE_EFFECTS (valist))
4552         valist = save_expr (valist);
4553
4554       /* For this case, the backends will be expecting a pointer to
4555          vatype, but it's possible we've actually been given an array
4556          (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4557          So fix it.  */
4558       if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4559         {
4560           tree p1 = build_pointer_type (TREE_TYPE (vatype));
4561           valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4562         }
4563     }
4564   else
4565     {
4566       tree pt = build_pointer_type (vatype);
4567
4568       if (! needs_lvalue)
4569         {
4570           if (! TREE_SIDE_EFFECTS (valist))
4571             return valist;
4572
4573           valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4574           TREE_SIDE_EFFECTS (valist) = 1;
4575         }
4576
4577       if (TREE_SIDE_EFFECTS (valist))
4578         valist = save_expr (valist);
4579       valist = fold_build2_loc (loc, MEM_REF,
4580                                 vatype, valist, build_int_cst (pt, 0));
4581     }
4582
4583   return valist;
4584 }
4585
4586 /* The "standard" definition of va_list is void*.  */
4587
4588 tree
4589 std_build_builtin_va_list (void)
4590 {
4591   return ptr_type_node;
4592 }
4593
4594 /* The "standard" abi va_list is va_list_type_node.  */
4595
4596 tree
4597 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4598 {
4599   return va_list_type_node;
4600 }
4601
4602 /* The "standard" type of va_list is va_list_type_node.  */
4603
4604 tree
4605 std_canonical_va_list_type (tree type)
4606 {
4607   tree wtype, htype;
4608
4609   if (INDIRECT_REF_P (type))
4610     type = TREE_TYPE (type);
4611   else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4612     type = TREE_TYPE (type);
4613   wtype = va_list_type_node;
4614   htype = type;
4615   /* Treat structure va_list types.  */
4616   if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4617     htype = TREE_TYPE (htype);
4618   else if (TREE_CODE (wtype) == ARRAY_TYPE)
4619     {
4620       /* If va_list is an array type, the argument may have decayed
4621          to a pointer type, e.g. by being passed to another function.
4622          In that case, unwrap both types so that we can compare the
4623          underlying records.  */
4624       if (TREE_CODE (htype) == ARRAY_TYPE
4625           || POINTER_TYPE_P (htype))
4626         {
4627           wtype = TREE_TYPE (wtype);
4628           htype = TREE_TYPE (htype);
4629         }
4630     }
4631   if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4632     return va_list_type_node;
4633
4634   return NULL_TREE;
4635 }
4636
4637 /* The "standard" implementation of va_start: just assign `nextarg' to
4638    the variable.  */
4639
4640 void
4641 std_expand_builtin_va_start (tree valist, rtx nextarg)
4642 {
4643   rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4644   convert_move (va_r, nextarg, 0);
4645 }
4646
4647 /* Expand EXP, a call to __builtin_va_start.  */
4648
4649 static rtx
4650 expand_builtin_va_start (tree exp)
4651 {
4652   rtx nextarg;
4653   tree valist;
4654   location_t loc = EXPR_LOCATION (exp);
4655
4656   if (call_expr_nargs (exp) < 2)
4657     {
4658       error_at (loc, "too few arguments to function %<va_start%>");
4659       return const0_rtx;
4660     }
4661
4662   if (fold_builtin_next_arg (exp, true))
4663     return const0_rtx;
4664
4665   nextarg = expand_builtin_next_arg ();
4666   valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4667
4668   if (targetm.expand_builtin_va_start)
4669     targetm.expand_builtin_va_start (valist, nextarg);
4670   else
4671     std_expand_builtin_va_start (valist, nextarg);
4672
4673   return const0_rtx;
4674 }
4675
4676 /* The "standard" implementation of va_arg: read the value from the
4677    current (padded) address and increment by the (padded) size.  */
4678
4679 tree
4680 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4681                           gimple_seq *post_p)
4682 {
4683   tree addr, t, type_size, rounded_size, valist_tmp;
4684   unsigned HOST_WIDE_INT align, boundary;
4685   bool indirect;
4686
4687 #ifdef ARGS_GROW_DOWNWARD
4688   /* All of the alignment and movement below is for args-grow-up machines.
4689      As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4690      implement their own specialized gimplify_va_arg_expr routines.  */
4691   gcc_unreachable ();
4692 #endif
4693
4694   indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4695   if (indirect)
4696     type = build_pointer_type (type);
4697
4698   align = PARM_BOUNDARY / BITS_PER_UNIT;
4699   boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
4700
4701   /* When we align parameter on stack for caller, if the parameter
4702      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4703      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
4704      here with caller.  */
4705   if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4706     boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4707
4708   boundary /= BITS_PER_UNIT;
4709
4710   /* Hoist the valist value into a temporary for the moment.  */
4711   valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4712
4713   /* va_list pointer is aligned to PARM_BOUNDARY.  If argument actually
4714      requires greater alignment, we must perform dynamic alignment.  */
4715   if (boundary > align
4716       && !integer_zerop (TYPE_SIZE (type)))
4717     {
4718       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4719                   fold_build2 (POINTER_PLUS_EXPR,
4720                                TREE_TYPE (valist),
4721                                valist_tmp, size_int (boundary - 1)));
4722       gimplify_and_add (t, pre_p);
4723
4724       t = fold_convert (sizetype, valist_tmp);
4725       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4726                   fold_convert (TREE_TYPE (valist),
4727                                 fold_build2 (BIT_AND_EXPR, sizetype, t,
4728                                              size_int (-boundary))));
4729       gimplify_and_add (t, pre_p);
4730     }
4731   else
4732     boundary = align;
4733
4734   /* If the actual alignment is less than the alignment of the type,
4735      adjust the type accordingly so that we don't assume strict alignment
4736      when dereferencing the pointer.  */
4737   boundary *= BITS_PER_UNIT;
4738   if (boundary < TYPE_ALIGN (type))
4739     {
4740       type = build_variant_type_copy (type);
4741       TYPE_ALIGN (type) = boundary;
4742     }
4743
4744   /* Compute the rounded size of the type.  */
4745   type_size = size_in_bytes (type);
4746   rounded_size = round_up (type_size, align);
4747
4748   /* Reduce rounded_size so it's sharable with the postqueue.  */
4749   gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4750
4751   /* Get AP.  */
4752   addr = valist_tmp;
4753   if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4754     {
4755       /* Small args are padded downward.  */
4756       t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4757                        rounded_size, size_int (align));
4758       t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4759                        size_binop (MINUS_EXPR, rounded_size, type_size));
4760       addr = fold_build2 (POINTER_PLUS_EXPR,
4761                           TREE_TYPE (addr), addr, t);
4762     }
4763
4764   /* Compute new value for AP.  */
4765   t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size);
4766   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4767   gimplify_and_add (t, pre_p);
4768
4769   addr = fold_convert (build_pointer_type (type), addr);
4770
4771   if (indirect)
4772     addr = build_va_arg_indirect_ref (addr);
4773
4774   return build_va_arg_indirect_ref (addr);
4775 }
4776
4777 /* Build an indirect-ref expression over the given TREE, which represents a
4778    piece of a va_arg() expansion.  */
4779 tree
4780 build_va_arg_indirect_ref (tree addr)
4781 {
4782   addr = build_fold_indirect_ref_loc (EXPR_LOCATION (addr), addr);
4783
4784   if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF.  */
4785     mf_mark (addr);
4786
4787   return addr;
4788 }
4789
4790 /* Return a dummy expression of type TYPE in order to keep going after an
4791    error.  */
4792
4793 static tree
4794 dummy_object (tree type)
4795 {
4796   tree t = build_int_cst (build_pointer_type (type), 0);
4797   return build2 (MEM_REF, type, t, t);
4798 }
4799
4800 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4801    builtin function, but a very special sort of operator.  */
4802
4803 enum gimplify_status
4804 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4805 {
4806   tree promoted_type, have_va_type;
4807   tree valist = TREE_OPERAND (*expr_p, 0);
4808   tree type = TREE_TYPE (*expr_p);
4809   tree t;
4810   location_t loc = EXPR_LOCATION (*expr_p);
4811
4812   /* Verify that valist is of the proper type.  */
4813   have_va_type = TREE_TYPE (valist);
4814   if (have_va_type == error_mark_node)
4815     return GS_ERROR;
4816   have_va_type = targetm.canonical_va_list_type (have_va_type);
4817
4818   if (have_va_type == NULL_TREE)
4819     {
4820       error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4821       return GS_ERROR;
4822     }
4823
4824   /* Generate a diagnostic for requesting data of a type that cannot
4825      be passed through `...' due to type promotion at the call site.  */
4826   if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4827            != type)
4828     {
4829       static bool gave_help;
4830       bool warned;
4831
4832       /* Unfortunately, this is merely undefined, rather than a constraint
4833          violation, so we cannot make this an error.  If this call is never
4834          executed, the program is still strictly conforming.  */
4835       warned = warning_at (loc, 0,
4836                            "%qT is promoted to %qT when passed through %<...%>",
4837                            type, promoted_type);
4838       if (!gave_help && warned)
4839         {
4840           gave_help = true;
4841           inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4842                   promoted_type, type);
4843         }
4844
4845       /* We can, however, treat "undefined" any way we please.
4846          Call abort to encourage the user to fix the program.  */
4847       if (warned)
4848         inform (loc, "if this code is reached, the program will abort");
4849       /* Before the abort, allow the evaluation of the va_list
4850          expression to exit or longjmp.  */
4851       gimplify_and_add (valist, pre_p);
4852       t = build_call_expr_loc (loc,
4853                                implicit_built_in_decls[BUILT_IN_TRAP], 0);
4854       gimplify_and_add (t, pre_p);
4855
4856       /* This is dead code, but go ahead and finish so that the
4857          mode of the result comes out right.  */
4858       *expr_p = dummy_object (type);
4859       return GS_ALL_DONE;
4860     }
4861   else
4862     {
4863       /* Make it easier for the backends by protecting the valist argument
4864          from multiple evaluations.  */
4865       if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4866         {
4867           /* For this case, the backends will be expecting a pointer to
4868              TREE_TYPE (abi), but it's possible we've
4869              actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4870              So fix it.  */
4871           if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4872             {
4873               tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4874               valist = fold_convert_loc (loc, p1,
4875                                          build_fold_addr_expr_loc (loc, valist));
4876             }
4877
4878           gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4879         }
4880       else
4881         gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4882
4883       if (!targetm.gimplify_va_arg_expr)
4884         /* FIXME: Once most targets are converted we should merely
4885            assert this is non-null.  */
4886         return GS_ALL_DONE;
4887
4888       *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4889       return GS_OK;
4890     }
4891 }
4892
4893 /* Expand EXP, a call to __builtin_va_end.  */
4894
4895 static rtx
4896 expand_builtin_va_end (tree exp)
4897 {
4898   tree valist = CALL_EXPR_ARG (exp, 0);
4899
4900   /* Evaluate for side effects, if needed.  I hate macros that don't
4901      do that.  */
4902   if (TREE_SIDE_EFFECTS (valist))
4903     expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4904
4905   return const0_rtx;
4906 }
4907
4908 /* Expand EXP, a call to __builtin_va_copy.  We do this as a
4909    builtin rather than just as an assignment in stdarg.h because of the
4910    nastiness of array-type va_list types.  */
4911
4912 static rtx
4913 expand_builtin_va_copy (tree exp)
4914 {
4915   tree dst, src, t;
4916   location_t loc = EXPR_LOCATION (exp);
4917
4918   dst = CALL_EXPR_ARG (exp, 0);
4919   src = CALL_EXPR_ARG (exp, 1);
4920
4921   dst = stabilize_va_list_loc (loc, dst, 1);
4922   src = stabilize_va_list_loc (loc, src, 0);
4923
4924   gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4925
4926   if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4927     {
4928       t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4929       TREE_SIDE_EFFECTS (t) = 1;
4930       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4931     }
4932   else
4933     {
4934       rtx dstb, srcb, size;
4935
4936       /* Evaluate to pointers.  */
4937       dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4938       srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4939       size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4940                   NULL_RTX, VOIDmode, EXPAND_NORMAL);
4941
4942       dstb = convert_memory_address (Pmode, dstb);
4943       srcb = convert_memory_address (Pmode, srcb);
4944
4945       /* "Dereference" to BLKmode memories.  */
4946       dstb = gen_rtx_MEM (BLKmode, dstb);
4947       set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4948       set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4949       srcb = gen_rtx_MEM (BLKmode, srcb);
4950       set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4951       set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4952
4953       /* Copy.  */
4954       emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4955     }
4956
4957   return const0_rtx;
4958 }
4959
4960 /* Expand a call to one of the builtin functions __builtin_frame_address or
4961    __builtin_return_address.  */
4962
4963 static rtx
4964 expand_builtin_frame_address (tree fndecl, tree exp)
4965 {
4966   /* The argument must be a nonnegative integer constant.
4967      It counts the number of frames to scan up the stack.
4968      The value is the return address saved in that frame.  */
4969   if (call_expr_nargs (exp) == 0)
4970     /* Warning about missing arg was already issued.  */
4971     return const0_rtx;
4972   else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4973     {
4974       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4975         error ("invalid argument to %<__builtin_frame_address%>");
4976       else
4977         error ("invalid argument to %<__builtin_return_address%>");
4978       return const0_rtx;
4979     }
4980   else
4981     {
4982       rtx tem
4983         = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4984                                       tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4985
4986       /* Some ports cannot access arbitrary stack frames.  */
4987       if (tem == NULL)
4988         {
4989           if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4990             warning (0, "unsupported argument to %<__builtin_frame_address%>");
4991           else
4992             warning (0, "unsupported argument to %<__builtin_return_address%>");
4993           return const0_rtx;
4994         }
4995
4996       /* For __builtin_frame_address, return what we've got.  */
4997       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4998         return tem;
4999
5000       if (!REG_P (tem)
5001           && ! CONSTANT_P (tem))
5002         tem = copy_to_mode_reg (Pmode, tem);
5003       return tem;
5004     }
5005 }
5006
5007 /* Expand EXP, a call to the alloca builtin.  Return NULL_RTX if we
5008    failed and the caller should emit a normal call.  CANNOT_ACCUMULATE
5009    is the same as for allocate_dynamic_stack_space.  */
5010
5011 static rtx
5012 expand_builtin_alloca (tree exp, bool cannot_accumulate)
5013 {
5014   rtx op0;
5015   rtx result;
5016
5017   /* Emit normal call if marked not-inlineable.  */
5018   if (CALL_CANNOT_INLINE_P (exp))
5019     return NULL_RTX;
5020
5021   if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5022     return NULL_RTX;
5023
5024   /* Compute the argument.  */
5025   op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5026
5027   /* Allocate the desired space.  */
5028   result = allocate_dynamic_stack_space (op0, 0, BIGGEST_ALIGNMENT,
5029                                          cannot_accumulate);
5030   result = convert_memory_address (ptr_mode, result);
5031
5032   return result;
5033 }
5034
5035 /* Expand a call to a bswap builtin with argument ARG0.  MODE
5036    is the mode to expand with.  */
5037
5038 static rtx
5039 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
5040 {
5041   enum machine_mode mode;
5042   tree arg;
5043   rtx op0;
5044
5045   if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5046     return NULL_RTX;
5047
5048   arg = CALL_EXPR_ARG (exp, 0);
5049   mode = TYPE_MODE (TREE_TYPE (arg));
5050   op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5051
5052   target = expand_unop (mode, bswap_optab, op0, target, 1);
5053
5054   gcc_assert (target);
5055
5056   return convert_to_mode (mode, target, 0);
5057 }
5058
5059 /* Expand a call to a unary builtin in EXP.
5060    Return NULL_RTX if a normal call should be emitted rather than expanding the
5061    function in-line.  If convenient, the result should be placed in TARGET.
5062    SUBTARGET may be used as the target for computing one of EXP's operands.  */
5063
5064 static rtx
5065 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
5066                      rtx subtarget, optab op_optab)
5067 {
5068   rtx op0;
5069
5070   if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5071     return NULL_RTX;
5072
5073   /* Compute the argument.  */
5074   op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5075                      (subtarget
5076                       && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5077                           == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5078                      VOIDmode, EXPAND_NORMAL);
5079   /* Compute op, into TARGET if possible.
5080      Set TARGET to wherever the result comes back.  */
5081   target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5082                         op_optab, op0, target, 1);
5083   gcc_assert (target);
5084
5085   return convert_to_mode (target_mode, target, 0);
5086 }
5087
5088 /* Expand a call to __builtin_expect.  We just return our argument
5089    as the builtin_expect semantic should've been already executed by
5090    tree branch prediction pass. */
5091
5092 static rtx
5093 expand_builtin_expect (tree exp, rtx target)
5094 {
5095   tree arg;
5096
5097   if (call_expr_nargs (exp) < 2)
5098     return const0_rtx;
5099   arg = CALL_EXPR_ARG (exp, 0);
5100
5101   target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5102   /* When guessing was done, the hints should be already stripped away.  */
5103   gcc_assert (!flag_guess_branch_prob
5104               || optimize == 0 || seen_error ());
5105   return target;
5106 }
5107
5108 void
5109 expand_builtin_trap (void)
5110 {
5111 #ifdef HAVE_trap
5112   if (HAVE_trap)
5113     emit_insn (gen_trap ());
5114   else
5115 #endif
5116     emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
5117   emit_barrier ();
5118 }
5119
5120 /* Expand a call to __builtin_unreachable.  We do nothing except emit
5121    a barrier saying that control flow will not pass here.
5122
5123    It is the responsibility of the program being compiled to ensure
5124    that control flow does never reach __builtin_unreachable.  */
5125 static void
5126 expand_builtin_unreachable (void)
5127 {
5128   emit_barrier ();
5129 }
5130
5131 /* Expand EXP, a call to fabs, fabsf or fabsl.
5132    Return NULL_RTX if a normal call should be emitted rather than expanding
5133    the function inline.  If convenient, the result should be placed
5134    in TARGET.  SUBTARGET may be used as the target for computing
5135    the operand.  */
5136
5137 static rtx
5138 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5139 {
5140   enum machine_mode mode;
5141   tree arg;
5142   rtx op0;
5143
5144   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5145     return NULL_RTX;
5146
5147   arg = CALL_EXPR_ARG (exp, 0);
5148   CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5149   mode = TYPE_MODE (TREE_TYPE (arg));
5150   op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5151   return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5152 }
5153
5154 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5155    Return NULL is a normal call should be emitted rather than expanding the
5156    function inline.  If convenient, the result should be placed in TARGET.
5157    SUBTARGET may be used as the target for computing the operand.  */
5158
5159 static rtx
5160 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5161 {
5162   rtx op0, op1;
5163   tree arg;
5164
5165   if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5166     return NULL_RTX;
5167
5168   arg = CALL_EXPR_ARG (exp, 0);
5169   op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5170
5171   arg = CALL_EXPR_ARG (exp, 1);
5172   op1 = expand_normal (arg);
5173
5174   return expand_copysign (op0, op1, target);
5175 }
5176
5177 /* Create a new constant string literal and return a char* pointer to it.
5178    The STRING_CST value is the LEN characters at STR.  */
5179 tree
5180 build_string_literal (int len, const char *str)
5181 {
5182   tree t, elem, index, type;
5183
5184   t = build_string (len, str);
5185   elem = build_type_variant (char_type_node, 1, 0);
5186   index = build_index_type (size_int (len - 1));
5187   type = build_array_type (elem, index);
5188   TREE_TYPE (t) = type;
5189   TREE_CONSTANT (t) = 1;
5190   TREE_READONLY (t) = 1;
5191   TREE_STATIC (t) = 1;
5192
5193   type = build_pointer_type (elem);
5194   t = build1 (ADDR_EXPR, type,
5195               build4 (ARRAY_REF, elem,
5196                       t, integer_zero_node, NULL_TREE, NULL_TREE));
5197   return t;
5198 }
5199
5200 /* Expand a call to either the entry or exit function profiler.  */
5201
5202 static rtx
5203 expand_builtin_profile_func (bool exitp)
5204 {
5205   rtx this_rtx, which;
5206
5207   this_rtx = DECL_RTL (current_function_decl);
5208   gcc_assert (MEM_P (this_rtx));
5209   this_rtx = XEXP (this_rtx, 0);
5210
5211   if (exitp)
5212     which = profile_function_exit_libfunc;
5213   else
5214     which = profile_function_entry_libfunc;
5215
5216   emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this_rtx, Pmode,
5217                      expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5218                                                  0),
5219                      Pmode);
5220
5221   return const0_rtx;
5222 }
5223
5224 /* Expand a call to __builtin___clear_cache.  */
5225
5226 static rtx
5227 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
5228 {
5229 #ifndef HAVE_clear_cache
5230 #ifdef CLEAR_INSN_CACHE
5231   /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5232      does something.  Just do the default expansion to a call to
5233      __clear_cache().  */
5234   return NULL_RTX;
5235 #else
5236   /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5237      does nothing.  There is no need to call it.  Do nothing.  */
5238   return const0_rtx;
5239 #endif /* CLEAR_INSN_CACHE */
5240 #else
5241   /* We have a "clear_cache" insn, and it will handle everything.  */
5242   tree begin, end;
5243   rtx begin_rtx, end_rtx;
5244   enum insn_code icode;
5245
5246   /* We must not expand to a library call.  If we did, any
5247      fallback library function in libgcc that might contain a call to
5248      __builtin___clear_cache() would recurse infinitely.  */
5249   if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5250     {
5251       error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5252       return const0_rtx;
5253     }
5254
5255   if (HAVE_clear_cache)
5256     {
5257       icode = CODE_FOR_clear_cache;
5258
5259       begin = CALL_EXPR_ARG (exp, 0);
5260       begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5261       begin_rtx = convert_memory_address (Pmode, begin_rtx);
5262       if (!insn_data[icode].operand[0].predicate (begin_rtx, Pmode))
5263         begin_rtx = copy_to_mode_reg (Pmode, begin_rtx);
5264
5265       end = CALL_EXPR_ARG (exp, 1);
5266       end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5267       end_rtx = convert_memory_address (Pmode, end_rtx);
5268       if (!insn_data[icode].operand[1].predicate (end_rtx, Pmode))
5269         end_rtx = copy_to_mode_reg (Pmode, end_rtx);
5270
5271       emit_insn (gen_clear_cache (begin_rtx, end_rtx));
5272     }
5273   return const0_rtx;
5274 #endif /* HAVE_clear_cache */
5275 }
5276
5277 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT.  */
5278
5279 static rtx
5280 round_trampoline_addr (rtx tramp)
5281 {
5282   rtx temp, addend, mask;
5283
5284   /* If we don't need too much alignment, we'll have been guaranteed
5285      proper alignment by get_trampoline_type.  */
5286   if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5287     return tramp;
5288
5289   /* Round address up to desired boundary.  */
5290   temp = gen_reg_rtx (Pmode);
5291   addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5292   mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5293
5294   temp  = expand_simple_binop (Pmode, PLUS, tramp, addend,
5295                                temp, 0, OPTAB_LIB_WIDEN);
5296   tramp = expand_simple_binop (Pmode, AND, temp, mask,
5297                                temp, 0, OPTAB_LIB_WIDEN);
5298
5299   return tramp;
5300 }
5301
5302 static rtx
5303 expand_builtin_init_trampoline (tree exp)
5304 {
5305   tree t_tramp, t_func, t_chain;
5306   rtx m_tramp, r_tramp, r_chain, tmp;
5307
5308   if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5309                          POINTER_TYPE, VOID_TYPE))
5310     return NULL_RTX;
5311
5312   t_tramp = CALL_EXPR_ARG (exp, 0);
5313   t_func = CALL_EXPR_ARG (exp, 1);
5314   t_chain = CALL_EXPR_ARG (exp, 2);
5315
5316   r_tramp = expand_normal (t_tramp);
5317   m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5318   MEM_NOTRAP_P (m_tramp) = 1;
5319
5320   /* The TRAMP argument should be the address of a field within the
5321      local function's FRAME decl.  Let's see if we can fill in the
5322      to fill in the MEM_ATTRs for this memory.  */
5323   if (TREE_CODE (t_tramp) == ADDR_EXPR)
5324     set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
5325                                      true, 0);
5326
5327   tmp = round_trampoline_addr (r_tramp);
5328   if (tmp != r_tramp)
5329     {
5330       m_tramp = change_address (m_tramp, BLKmode, tmp);
5331       set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5332       set_mem_size (m_tramp, GEN_INT (TRAMPOLINE_SIZE));
5333     }
5334
5335   /* The FUNC argument should be the address of the nested function.
5336      Extract the actual function decl to pass to the hook.  */
5337   gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5338   t_func = TREE_OPERAND (t_func, 0);
5339   gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5340
5341   r_chain = expand_normal (t_chain);
5342
5343   /* Generate insns to initialize the trampoline.  */
5344   targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5345
5346   trampolines_created = 1;
5347
5348   warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5349               "trampoline generated for nested function %qD", t_func);
5350
5351   return const0_rtx;
5352 }
5353
5354 static rtx
5355 expand_builtin_adjust_trampoline (tree exp)
5356 {
5357   rtx tramp;
5358
5359   if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5360     return NULL_RTX;
5361
5362   tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5363   tramp = round_trampoline_addr (tramp);
5364   if (targetm.calls.trampoline_adjust_address)
5365     tramp = targetm.calls.trampoline_adjust_address (tramp);
5366
5367   return tramp;
5368 }
5369
5370 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5371    function.  The function first checks whether the back end provides
5372    an insn to implement signbit for the respective mode.  If not, it
5373    checks whether the floating point format of the value is such that
5374    the sign bit can be extracted.  If that is not the case, the
5375    function returns NULL_RTX to indicate that a normal call should be
5376    emitted rather than expanding the function in-line.  EXP is the
5377    expression that is a call to the builtin function; if convenient,
5378    the result should be placed in TARGET.  */
5379 static rtx
5380 expand_builtin_signbit (tree exp, rtx target)
5381 {
5382   const struct real_format *fmt;
5383   enum machine_mode fmode, imode, rmode;
5384   tree arg;
5385   int word, bitpos;
5386   enum insn_code icode;
5387   rtx temp;
5388   location_t loc = EXPR_LOCATION (exp);
5389
5390   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5391     return NULL_RTX;
5392
5393   arg = CALL_EXPR_ARG (exp, 0);
5394   fmode = TYPE_MODE (TREE_TYPE (arg));
5395   rmode = TYPE_MODE (TREE_TYPE (exp));
5396   fmt = REAL_MODE_FORMAT (fmode);
5397
5398   arg = builtin_save_expr (arg);
5399
5400   /* Expand the argument yielding a RTX expression. */
5401   temp = expand_normal (arg);
5402
5403   /* Check if the back end provides an insn that handles signbit for the
5404      argument's mode. */
5405   icode = optab_handler (signbit_optab, fmode);
5406   if (icode != CODE_FOR_nothing)
5407     {
5408       rtx last = get_last_insn ();
5409       target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5410       if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5411         return target;
5412       delete_insns_since (last);
5413     }
5414
5415   /* For floating point formats without a sign bit, implement signbit
5416      as "ARG < 0.0".  */
5417   bitpos = fmt->signbit_ro;
5418   if (bitpos < 0)
5419   {
5420     /* But we can't do this if the format supports signed zero.  */
5421     if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5422       return NULL_RTX;
5423
5424     arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5425                        build_real (TREE_TYPE (arg), dconst0));
5426     return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5427   }
5428
5429   if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5430     {
5431       imode = int_mode_for_mode (fmode);
5432       if (imode == BLKmode)
5433         return NULL_RTX;
5434       temp = gen_lowpart (imode, temp);
5435     }
5436   else
5437     {
5438       imode = word_mode;
5439       /* Handle targets with different FP word orders.  */
5440       if (FLOAT_WORDS_BIG_ENDIAN)
5441         word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5442       else
5443         word = bitpos / BITS_PER_WORD;
5444       temp = operand_subword_force (temp, word, fmode);
5445       bitpos = bitpos % BITS_PER_WORD;
5446     }
5447
5448   /* Force the intermediate word_mode (or narrower) result into a
5449      register.  This avoids attempting to create paradoxical SUBREGs
5450      of floating point modes below.  */
5451   temp = force_reg (imode, temp);
5452
5453   /* If the bitpos is within the "result mode" lowpart, the operation
5454      can be implement with a single bitwise AND.  Otherwise, we need
5455      a right shift and an AND.  */
5456
5457   if (bitpos < GET_MODE_BITSIZE (rmode))
5458     {
5459       double_int mask = double_int_setbit (double_int_zero, bitpos);
5460
5461       if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5462         temp = gen_lowpart (rmode, temp);
5463       temp = expand_binop (rmode, and_optab, temp,
5464                            immed_double_int_const (mask, rmode),
5465                            NULL_RTX, 1, OPTAB_LIB_WIDEN);
5466     }
5467   else
5468     {
5469       /* Perform a logical right shift to place the signbit in the least
5470          significant bit, then truncate the result to the desired mode
5471          and mask just this bit.  */
5472       temp = expand_shift (RSHIFT_EXPR, imode, temp,
5473                            build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5474       temp = gen_lowpart (rmode, temp);
5475       temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5476                            NULL_RTX, 1, OPTAB_LIB_WIDEN);
5477     }
5478
5479   return temp;
5480 }
5481
5482 /* Expand fork or exec calls.  TARGET is the desired target of the
5483    call.  EXP is the call. FN is the
5484    identificator of the actual function.  IGNORE is nonzero if the
5485    value is to be ignored.  */
5486
5487 static rtx
5488 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5489 {
5490   tree id, decl;
5491   tree call;
5492
5493   /* If we are not profiling, just call the function.  */
5494   if (!profile_arc_flag)
5495     return NULL_RTX;
5496
5497   /* Otherwise call the wrapper.  This should be equivalent for the rest of
5498      compiler, so the code does not diverge, and the wrapper may run the
5499      code necessary for keeping the profiling sane.  */
5500
5501   switch (DECL_FUNCTION_CODE (fn))
5502     {
5503     case BUILT_IN_FORK:
5504       id = get_identifier ("__gcov_fork");
5505       break;
5506
5507     case BUILT_IN_EXECL:
5508       id = get_identifier ("__gcov_execl");
5509       break;
5510
5511     case BUILT_IN_EXECV:
5512       id = get_identifier ("__gcov_execv");
5513       break;
5514
5515     case BUILT_IN_EXECLP:
5516       id = get_identifier ("__gcov_execlp");
5517       break;
5518
5519     case BUILT_IN_EXECLE:
5520       id = get_identifier ("__gcov_execle");
5521       break;
5522
5523     case BUILT_IN_EXECVP:
5524       id = get_identifier ("__gcov_execvp");
5525       break;
5526
5527     case BUILT_IN_EXECVE:
5528       id = get_identifier ("__gcov_execve");
5529       break;
5530
5531     default:
5532       gcc_unreachable ();
5533     }
5534
5535   decl = build_decl (DECL_SOURCE_LOCATION (fn),
5536                      FUNCTION_DECL, id, TREE_TYPE (fn));
5537   DECL_EXTERNAL (decl) = 1;
5538   TREE_PUBLIC (decl) = 1;
5539   DECL_ARTIFICIAL (decl) = 1;
5540   TREE_NOTHROW (decl) = 1;
5541   DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5542   DECL_VISIBILITY_SPECIFIED (decl) = 1;
5543   call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5544   return expand_call (call, target, ignore);
5545  }
5546
5547
5548 \f
5549 /* Reconstitute a mode for a __sync intrinsic operation.  Since the type of
5550    the pointer in these functions is void*, the tree optimizers may remove
5551    casts.  The mode computed in expand_builtin isn't reliable either, due
5552    to __sync_bool_compare_and_swap.
5553
5554    FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5555    group of builtins.  This gives us log2 of the mode size.  */
5556
5557 static inline enum machine_mode
5558 get_builtin_sync_mode (int fcode_diff)
5559 {
5560   /* The size is not negotiable, so ask not to get BLKmode in return
5561      if the target indicates that a smaller size would be better.  */
5562   return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5563 }
5564
5565 /* Expand the memory expression LOC and return the appropriate memory operand
5566    for the builtin_sync operations.  */
5567
5568 static rtx
5569 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5570 {
5571   rtx addr, mem;
5572
5573   addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5574   addr = convert_memory_address (Pmode, addr);
5575
5576   /* Note that we explicitly do not want any alias information for this
5577      memory, so that we kill all other live memories.  Otherwise we don't
5578      satisfy the full barrier semantics of the intrinsic.  */
5579   mem = validize_mem (gen_rtx_MEM (mode, addr));
5580
5581   /* The alignment needs to be at least according to that of the mode.  */
5582   set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5583                            get_pointer_alignment (loc, BIGGEST_ALIGNMENT)));
5584   set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5585   MEM_VOLATILE_P (mem) = 1;
5586
5587   return mem;
5588 }
5589
5590 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5591    EXP is the CALL_EXPR.  CODE is the rtx code
5592    that corresponds to the arithmetic or logical operation from the name;
5593    an exception here is that NOT actually means NAND.  TARGET is an optional
5594    place for us to store the results; AFTER is true if this is the
5595    fetch_and_xxx form.  IGNORE is true if we don't actually care about
5596    the result of the operation at all.  */
5597
5598 static rtx
5599 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5600                                enum rtx_code code, bool after,
5601                                rtx target, bool ignore)
5602 {
5603   rtx val, mem;
5604   enum machine_mode old_mode;
5605   location_t loc = EXPR_LOCATION (exp);
5606
5607   if (code == NOT && warn_sync_nand)
5608     {
5609       tree fndecl = get_callee_fndecl (exp);
5610       enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5611
5612       static bool warned_f_a_n, warned_n_a_f;
5613
5614       switch (fcode)
5615         {
5616         case BUILT_IN_FETCH_AND_NAND_1:
5617         case BUILT_IN_FETCH_AND_NAND_2:
5618         case BUILT_IN_FETCH_AND_NAND_4:
5619         case BUILT_IN_FETCH_AND_NAND_8:
5620         case BUILT_IN_FETCH_AND_NAND_16:
5621
5622           if (warned_f_a_n)
5623             break;
5624
5625           fndecl = implicit_built_in_decls[BUILT_IN_FETCH_AND_NAND_N];
5626           inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5627           warned_f_a_n = true;
5628           break;
5629
5630         case BUILT_IN_NAND_AND_FETCH_1:
5631         case BUILT_IN_NAND_AND_FETCH_2:
5632         case BUILT_IN_NAND_AND_FETCH_4:
5633         case BUILT_IN_NAND_AND_FETCH_8:
5634         case BUILT_IN_NAND_AND_FETCH_16:
5635
5636           if (warned_n_a_f)
5637             break;
5638
5639           fndecl = implicit_built_in_decls[BUILT_IN_NAND_AND_FETCH_N];
5640           inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5641           warned_n_a_f = true;
5642           break;
5643
5644         default:
5645           gcc_unreachable ();
5646         }
5647     }
5648
5649   /* Expand the operands.  */
5650   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5651
5652   val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5653   /* If VAL is promoted to a wider mode, convert it back to MODE.  Take care
5654      of CONST_INTs, where we know the old_mode only from the call argument.  */
5655   old_mode = GET_MODE (val);
5656   if (old_mode == VOIDmode)
5657     old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5658   val = convert_modes (mode, old_mode, val, 1);
5659
5660   if (ignore)
5661     return expand_sync_operation (mem, val, code);
5662   else
5663     return expand_sync_fetch_operation (mem, val, code, after, target);
5664 }
5665
5666 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5667    intrinsics. EXP is the CALL_EXPR.  IS_BOOL is
5668    true if this is the boolean form.  TARGET is a place for us to store the
5669    results; this is NOT optional if IS_BOOL is true.  */
5670
5671 static rtx
5672 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5673                                  bool is_bool, rtx target)
5674 {
5675   rtx old_val, new_val, mem;
5676   enum machine_mode old_mode;
5677
5678   /* Expand the operands.  */
5679   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5680
5681
5682   old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
5683                          mode, EXPAND_NORMAL);
5684   /* If VAL is promoted to a wider mode, convert it back to MODE.  Take care
5685      of CONST_INTs, where we know the old_mode only from the call argument.  */
5686   old_mode = GET_MODE (old_val);
5687   if (old_mode == VOIDmode)
5688     old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5689   old_val = convert_modes (mode, old_mode, old_val, 1);
5690
5691   new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
5692                          mode, EXPAND_NORMAL);
5693   /* If VAL is promoted to a wider mode, convert it back to MODE.  Take care
5694      of CONST_INTs, where we know the old_mode only from the call argument.  */
5695   old_mode = GET_MODE (new_val);
5696   if (old_mode == VOIDmode)
5697     old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
5698   new_val = convert_modes (mode, old_mode, new_val, 1);
5699
5700   if (is_bool)
5701     return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5702   else
5703     return expand_val_compare_and_swap (mem, old_val, new_val, target);
5704 }
5705
5706 /* Expand the __sync_lock_test_and_set intrinsic.  Note that the most
5707    general form is actually an atomic exchange, and some targets only
5708    support a reduced form with the second argument being a constant 1.
5709    EXP is the CALL_EXPR; TARGET is an optional place for us to store
5710    the results.  */
5711
5712 static rtx
5713 expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp,
5714                                   rtx target)
5715 {
5716   rtx val, mem;
5717   enum machine_mode old_mode;
5718
5719   /* Expand the operands.  */
5720   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5721   val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5722   /* If VAL is promoted to a wider mode, convert it back to MODE.  Take care
5723      of CONST_INTs, where we know the old_mode only from the call argument.  */
5724   old_mode = GET_MODE (val);
5725   if (old_mode == VOIDmode)
5726     old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5727   val = convert_modes (mode, old_mode, val, 1);
5728
5729   return expand_sync_lock_test_and_set (mem, val, target);
5730 }
5731
5732 /* Expand the __sync_synchronize intrinsic.  */
5733
5734 static void
5735 expand_builtin_synchronize (void)
5736 {
5737   gimple x;
5738   VEC (tree, gc) *v_clobbers;
5739
5740 #ifdef HAVE_memory_barrier
5741   if (HAVE_memory_barrier)
5742     {
5743       emit_insn (gen_memory_barrier ());
5744       return;
5745     }
5746 #endif
5747
5748   if (synchronize_libfunc != NULL_RTX)
5749     {
5750       emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
5751       return;
5752     }
5753
5754   /* If no explicit memory barrier instruction is available, create an
5755      empty asm stmt with a memory clobber.  */
5756   v_clobbers = VEC_alloc (tree, gc, 1);
5757   VEC_quick_push (tree, v_clobbers,
5758                   tree_cons (NULL, build_string (6, "memory"), NULL));
5759   x = gimple_build_asm_vec ("", NULL, NULL, v_clobbers, NULL);
5760   gimple_asm_set_volatile (x, true);
5761   expand_asm_stmt (x);
5762 }
5763
5764 /* Expand the __sync_lock_release intrinsic.  EXP is the CALL_EXPR.  */
5765
5766 static void
5767 expand_builtin_lock_release (enum machine_mode mode, tree exp)
5768 {
5769   enum insn_code icode;
5770   rtx mem, insn;
5771   rtx val = const0_rtx;
5772
5773   /* Expand the operands.  */
5774   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5775
5776   /* If there is an explicit operation in the md file, use it.  */
5777   icode = direct_optab_handler (sync_lock_release_optab, mode);
5778   if (icode != CODE_FOR_nothing)
5779     {
5780       if (!insn_data[icode].operand[1].predicate (val, mode))
5781         val = force_reg (mode, val);
5782
5783       insn = GEN_FCN (icode) (mem, val);
5784       if (insn)
5785         {
5786           emit_insn (insn);
5787           return;
5788         }
5789     }
5790
5791   /* Otherwise we can implement this operation by emitting a barrier
5792      followed by a store of zero.  */
5793   expand_builtin_synchronize ();
5794   emit_move_insn (mem, val);
5795 }
5796 \f
5797 /* Expand an expression EXP that calls a built-in function,
5798    with result going to TARGET if that's convenient
5799    (and in mode MODE if that's convenient).
5800    SUBTARGET may be used as the target for computing one of EXP's operands.
5801    IGNORE is nonzero if the value is to be ignored.  */
5802
5803 rtx
5804 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5805                 int ignore)
5806 {
5807   tree fndecl = get_callee_fndecl (exp);
5808   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5809   enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5810   int flags;
5811
5812   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5813     return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5814
5815   /* When not optimizing, generate calls to library functions for a certain
5816      set of builtins.  */
5817   if (!optimize
5818       && !called_as_built_in (fndecl)
5819       && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5820       && fcode != BUILT_IN_ALLOCA
5821       && fcode != BUILT_IN_FREE)
5822     return expand_call (exp, target, ignore);
5823
5824   /* The built-in function expanders test for target == const0_rtx
5825      to determine whether the function's result will be ignored.  */
5826   if (ignore)
5827     target = const0_rtx;
5828
5829   /* If the result of a pure or const built-in function is ignored, and
5830      none of its arguments are volatile, we can avoid expanding the
5831      built-in call and just evaluate the arguments for side-effects.  */
5832   if (target == const0_rtx
5833       && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5834       && !(flags & ECF_LOOPING_CONST_OR_PURE))
5835     {
5836       bool volatilep = false;
5837       tree arg;
5838       call_expr_arg_iterator iter;
5839
5840       FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5841         if (TREE_THIS_VOLATILE (arg))
5842           {
5843             volatilep = true;
5844             break;
5845           }
5846
5847       if (! volatilep)
5848         {
5849           FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5850             expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5851           return const0_rtx;
5852         }
5853     }
5854
5855   switch (fcode)
5856     {
5857     CASE_FLT_FN (BUILT_IN_FABS):
5858       target = expand_builtin_fabs (exp, target, subtarget);
5859       if (target)
5860         return target;
5861       break;
5862
5863     CASE_FLT_FN (BUILT_IN_COPYSIGN):
5864       target = expand_builtin_copysign (exp, target, subtarget);
5865       if (target)
5866         return target;
5867       break;
5868
5869       /* Just do a normal library call if we were unable to fold
5870          the values.  */
5871     CASE_FLT_FN (BUILT_IN_CABS):
5872       break;
5873
5874     CASE_FLT_FN (BUILT_IN_EXP):
5875     CASE_FLT_FN (BUILT_IN_EXP10):
5876     CASE_FLT_FN (BUILT_IN_POW10):
5877     CASE_FLT_FN (BUILT_IN_EXP2):
5878     CASE_FLT_FN (BUILT_IN_EXPM1):
5879     CASE_FLT_FN (BUILT_IN_LOGB):
5880     CASE_FLT_FN (BUILT_IN_LOG):
5881     CASE_FLT_FN (BUILT_IN_LOG10):
5882     CASE_FLT_FN (BUILT_IN_LOG2):
5883     CASE_FLT_FN (BUILT_IN_LOG1P):
5884     CASE_FLT_FN (BUILT_IN_TAN):
5885     CASE_FLT_FN (BUILT_IN_ASIN):
5886     CASE_FLT_FN (BUILT_IN_ACOS):
5887     CASE_FLT_FN (BUILT_IN_ATAN):
5888     CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5889       /* Treat these like sqrt only if unsafe math optimizations are allowed,
5890          because of possible accuracy problems.  */
5891       if (! flag_unsafe_math_optimizations)
5892         break;
5893     CASE_FLT_FN (BUILT_IN_SQRT):
5894     CASE_FLT_FN (BUILT_IN_FLOOR):
5895     CASE_FLT_FN (BUILT_IN_CEIL):
5896     CASE_FLT_FN (BUILT_IN_TRUNC):
5897     CASE_FLT_FN (BUILT_IN_ROUND):
5898     CASE_FLT_FN (BUILT_IN_NEARBYINT):
5899     CASE_FLT_FN (BUILT_IN_RINT):
5900       target = expand_builtin_mathfn (exp, target, subtarget);
5901       if (target)
5902         return target;
5903       break;
5904
5905     CASE_FLT_FN (BUILT_IN_FMA):
5906       target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5907       if (target)
5908         return target;
5909       break;
5910
5911     CASE_FLT_FN (BUILT_IN_ILOGB):
5912       if (! flag_unsafe_math_optimizations)
5913         break;
5914     CASE_FLT_FN (BUILT_IN_ISINF):
5915     CASE_FLT_FN (BUILT_IN_FINITE):
5916     case BUILT_IN_ISFINITE:
5917     case BUILT_IN_ISNORMAL:
5918       target = expand_builtin_interclass_mathfn (exp, target);
5919       if (target)
5920         return target;
5921       break;
5922
5923     CASE_FLT_FN (BUILT_IN_LCEIL):
5924     CASE_FLT_FN (BUILT_IN_LLCEIL):
5925     CASE_FLT_FN (BUILT_IN_LFLOOR):
5926     CASE_FLT_FN (BUILT_IN_LLFLOOR):
5927       target = expand_builtin_int_roundingfn (exp, target);
5928       if (target)
5929         return target;
5930       break;
5931
5932     CASE_FLT_FN (BUILT_IN_LRINT):
5933     CASE_FLT_FN (BUILT_IN_LLRINT):
5934     CASE_FLT_FN (BUILT_IN_LROUND):
5935     CASE_FLT_FN (BUILT_IN_LLROUND):
5936       target = expand_builtin_int_roundingfn_2 (exp, target);
5937       if (target)
5938         return target;
5939       break;
5940
5941     CASE_FLT_FN (BUILT_IN_POW):
5942       target = expand_builtin_pow (exp, target, subtarget);
5943       if (target)
5944         return target;
5945       break;
5946
5947     CASE_FLT_FN (BUILT_IN_POWI):
5948       target = expand_builtin_powi (exp, target);
5949       if (target)
5950         return target;
5951       break;
5952
5953     CASE_FLT_FN (BUILT_IN_ATAN2):
5954     CASE_FLT_FN (BUILT_IN_LDEXP):
5955     CASE_FLT_FN (BUILT_IN_SCALB):
5956     CASE_FLT_FN (BUILT_IN_SCALBN):
5957     CASE_FLT_FN (BUILT_IN_SCALBLN):
5958       if (! flag_unsafe_math_optimizations)
5959         break;
5960
5961     CASE_FLT_FN (BUILT_IN_FMOD):
5962     CASE_FLT_FN (BUILT_IN_REMAINDER):
5963     CASE_FLT_FN (BUILT_IN_DREM):
5964       target = expand_builtin_mathfn_2 (exp, target, subtarget);
5965       if (target)
5966         return target;
5967       break;
5968
5969     CASE_FLT_FN (BUILT_IN_CEXPI):
5970       target = expand_builtin_cexpi (exp, target);
5971       gcc_assert (target);
5972       return target;
5973
5974     CASE_FLT_FN (BUILT_IN_SIN):
5975     CASE_FLT_FN (BUILT_IN_COS):
5976       if (! flag_unsafe_math_optimizations)
5977         break;
5978       target = expand_builtin_mathfn_3 (exp, target, subtarget);
5979       if (target)
5980         return target;
5981       break;
5982
5983     CASE_FLT_FN (BUILT_IN_SINCOS):
5984       if (! flag_unsafe_math_optimizations)
5985         break;
5986       target = expand_builtin_sincos (exp);
5987       if (target)
5988         return target;
5989       break;
5990
5991     case BUILT_IN_APPLY_ARGS:
5992       return expand_builtin_apply_args ();
5993
5994       /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5995          FUNCTION with a copy of the parameters described by
5996          ARGUMENTS, and ARGSIZE.  It returns a block of memory
5997          allocated on the stack into which is stored all the registers
5998          that might possibly be used for returning the result of a
5999          function.  ARGUMENTS is the value returned by
6000          __builtin_apply_args.  ARGSIZE is the number of bytes of
6001          arguments that must be copied.  ??? How should this value be
6002          computed?  We'll also need a safe worst case value for varargs
6003          functions.  */
6004     case BUILT_IN_APPLY:
6005       if (!validate_arglist (exp, POINTER_TYPE,
6006                              POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6007           && !validate_arglist (exp, REFERENCE_TYPE,
6008                                 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6009         return const0_rtx;
6010       else
6011         {
6012           rtx ops[3];
6013
6014           ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6015           ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6016           ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6017
6018           return expand_builtin_apply (ops[0], ops[1], ops[2]);
6019         }
6020
6021       /* __builtin_return (RESULT) causes the function to return the
6022          value described by RESULT.  RESULT is address of the block of
6023          memory returned by __builtin_apply.  */
6024     case BUILT_IN_RETURN:
6025       if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6026         expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6027       return const0_rtx;
6028
6029     case BUILT_IN_SAVEREGS:
6030       return expand_builtin_saveregs ();
6031
6032     case BUILT_IN_VA_ARG_PACK:
6033       /* All valid uses of __builtin_va_arg_pack () are removed during
6034          inlining.  */
6035       error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6036       return const0_rtx;
6037
6038     case BUILT_IN_VA_ARG_PACK_LEN:
6039       /* All valid uses of __builtin_va_arg_pack_len () are removed during
6040          inlining.  */
6041       error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6042       return const0_rtx;
6043
6044       /* Return the address of the first anonymous stack arg.  */
6045     case BUILT_IN_NEXT_ARG:
6046       if (fold_builtin_next_arg (exp, false))
6047         return const0_rtx;
6048       return expand_builtin_next_arg ();
6049
6050     case BUILT_IN_CLEAR_CACHE:
6051       target = expand_builtin___clear_cache (exp);
6052       if (target)
6053         return target;
6054       break;
6055
6056     case BUILT_IN_CLASSIFY_TYPE:
6057       return expand_builtin_classify_type (exp);
6058
6059     case BUILT_IN_CONSTANT_P:
6060       return const0_rtx;
6061
6062     case BUILT_IN_FRAME_ADDRESS:
6063     case BUILT_IN_RETURN_ADDRESS:
6064       return expand_builtin_frame_address (fndecl, exp);
6065
6066     /* Returns the address of the area where the structure is returned.
6067        0 otherwise.  */
6068     case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6069       if (call_expr_nargs (exp) != 0
6070           || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6071           || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6072         return const0_rtx;
6073       else
6074         return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6075
6076     case BUILT_IN_ALLOCA:
6077       /* If the allocation stems from the declaration of a variable-sized
6078          object, it cannot accumulate.  */
6079       target = expand_builtin_alloca (exp, ALLOCA_FOR_VAR_P (exp));
6080       if (target)
6081         return target;
6082       break;
6083
6084     case BUILT_IN_STACK_SAVE:
6085       return expand_stack_save ();
6086
6087     case BUILT_IN_STACK_RESTORE:
6088       expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6089       return const0_rtx;
6090
6091     case BUILT_IN_BSWAP32:
6092     case BUILT_IN_BSWAP64:
6093       target = expand_builtin_bswap (exp, target, subtarget);
6094
6095       if (target)
6096         return target;
6097       break;
6098
6099     CASE_INT_FN (BUILT_IN_FFS):
6100     case BUILT_IN_FFSIMAX:
6101       target = expand_builtin_unop (target_mode, exp, target,
6102                                     subtarget, ffs_optab);
6103       if (target)
6104         return target;
6105       break;
6106
6107     CASE_INT_FN (BUILT_IN_CLZ):
6108     case BUILT_IN_CLZIMAX:
6109       target = expand_builtin_unop (target_mode, exp, target,
6110                                     subtarget, clz_optab);
6111       if (target)
6112         return target;
6113       break;
6114
6115     CASE_INT_FN (BUILT_IN_CTZ):
6116     case BUILT_IN_CTZIMAX:
6117       target = expand_builtin_unop (target_mode, exp, target,
6118                                     subtarget, ctz_optab);
6119       if (target)
6120         return target;
6121       break;
6122
6123     CASE_INT_FN (BUILT_IN_POPCOUNT):
6124     case BUILT_IN_POPCOUNTIMAX:
6125       target = expand_builtin_unop (target_mode, exp, target,
6126                                     subtarget, popcount_optab);
6127       if (target)
6128         return target;
6129       break;
6130
6131     CASE_INT_FN (BUILT_IN_PARITY):
6132     case BUILT_IN_PARITYIMAX:
6133       target = expand_builtin_unop (target_mode, exp, target,
6134                                     subtarget, parity_optab);
6135       if (target)
6136         return target;
6137       break;
6138
6139     case BUILT_IN_STRLEN:
6140       target = expand_builtin_strlen (exp, target, target_mode);
6141       if (target)
6142         return target;
6143       break;
6144
6145     case BUILT_IN_STRCPY:
6146       target = expand_builtin_strcpy (exp, target);
6147       if (target)
6148         return target;
6149       break;
6150
6151     case BUILT_IN_STRNCPY:
6152       target = expand_builtin_strncpy (exp, target);
6153       if (target)
6154         return target;
6155       break;
6156
6157     case BUILT_IN_STPCPY:
6158       target = expand_builtin_stpcpy (exp, target, mode);
6159       if (target)
6160         return target;
6161       break;
6162
6163     case BUILT_IN_MEMCPY:
6164       target = expand_builtin_memcpy (exp, target);
6165       if (target)
6166         return target;
6167       break;
6168
6169     case BUILT_IN_MEMPCPY:
6170       target = expand_builtin_mempcpy (exp, target, mode);
6171       if (target)
6172         return target;
6173       break;
6174
6175     case BUILT_IN_MEMSET:
6176       target = expand_builtin_memset (exp, target, mode);
6177       if (target)
6178         return target;
6179       break;
6180
6181     case BUILT_IN_BZERO:
6182       target = expand_builtin_bzero (exp);
6183       if (target)
6184         return target;
6185       break;
6186
6187     case BUILT_IN_STRCMP:
6188       target = expand_builtin_strcmp (exp, target);
6189       if (target)
6190         return target;
6191       break;
6192
6193     case BUILT_IN_STRNCMP:
6194       target = expand_builtin_strncmp (exp, target, mode);
6195       if (target)
6196         return target;
6197       break;
6198
6199     case BUILT_IN_BCMP:
6200     case BUILT_IN_MEMCMP:
6201       target = expand_builtin_memcmp (exp, target, mode);
6202       if (target)
6203         return target;
6204       break;
6205
6206     case BUILT_IN_SETJMP:
6207       /* This should have been lowered to the builtins below.  */
6208       gcc_unreachable ();
6209
6210     case BUILT_IN_SETJMP_SETUP:
6211       /* __builtin_setjmp_setup is passed a pointer to an array of five words
6212           and the receiver label.  */
6213       if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6214         {
6215           rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6216                                       VOIDmode, EXPAND_NORMAL);
6217           tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6218           rtx label_r = label_rtx (label);
6219
6220           /* This is copied from the handling of non-local gotos.  */
6221           expand_builtin_setjmp_setup (buf_addr, label_r);
6222           nonlocal_goto_handler_labels
6223             = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6224                                  nonlocal_goto_handler_labels);
6225           /* ??? Do not let expand_label treat us as such since we would
6226              not want to be both on the list of non-local labels and on
6227              the list of forced labels.  */
6228           FORCED_LABEL (label) = 0;
6229           return const0_rtx;
6230         }
6231       break;
6232
6233     case BUILT_IN_SETJMP_DISPATCHER:
6234        /* __builtin_setjmp_dispatcher is passed the dispatcher label.  */
6235       if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6236         {
6237           tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6238           rtx label_r = label_rtx (label);
6239
6240           /* Remove the dispatcher label from the list of non-local labels
6241              since the receiver labels have been added to it above.  */
6242           remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6243           return const0_rtx;
6244         }
6245       break;
6246
6247     case BUILT_IN_SETJMP_RECEIVER:
6248        /* __builtin_setjmp_receiver is passed the receiver label.  */
6249       if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6250         {
6251           tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6252           rtx label_r = label_rtx (label);
6253
6254           expand_builtin_setjmp_receiver (label_r);
6255           return const0_rtx;
6256         }
6257       break;
6258
6259       /* __builtin_longjmp is passed a pointer to an array of five words.
6260          It's similar to the C library longjmp function but works with
6261          __builtin_setjmp above.  */
6262     case BUILT_IN_LONGJMP:
6263       if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6264         {
6265           rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6266                                       VOIDmode, EXPAND_NORMAL);
6267           rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6268
6269           if (value != const1_rtx)
6270             {
6271               error ("%<__builtin_longjmp%> second argument must be 1");
6272               return const0_rtx;
6273             }
6274
6275           expand_builtin_longjmp (buf_addr, value);
6276           return const0_rtx;
6277         }
6278       break;
6279
6280     case BUILT_IN_NONLOCAL_GOTO:
6281       target = expand_builtin_nonlocal_goto (exp);
6282       if (target)
6283         return target;
6284       break;
6285
6286       /* This updates the setjmp buffer that is its argument with the value
6287          of the current stack pointer.  */
6288     case BUILT_IN_UPDATE_SETJMP_BUF:
6289       if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6290         {
6291           rtx buf_addr
6292             = expand_normal (CALL_EXPR_ARG (exp, 0));
6293
6294           expand_builtin_update_setjmp_buf (buf_addr);
6295           return const0_rtx;
6296         }
6297       break;
6298
6299     case BUILT_IN_TRAP:
6300       expand_builtin_trap ();
6301       return const0_rtx;
6302
6303     case BUILT_IN_UNREACHABLE:
6304       expand_builtin_unreachable ();
6305       return const0_rtx;
6306
6307     CASE_FLT_FN (BUILT_IN_SIGNBIT):
6308     case BUILT_IN_SIGNBITD32:
6309     case BUILT_IN_SIGNBITD64:
6310     case BUILT_IN_SIGNBITD128:
6311       target = expand_builtin_signbit (exp, target);
6312       if (target)
6313         return target;
6314       break;
6315
6316       /* Various hooks for the DWARF 2 __throw routine.  */
6317     case BUILT_IN_UNWIND_INIT:
6318       expand_builtin_unwind_init ();
6319       return const0_rtx;
6320     case BUILT_IN_DWARF_CFA:
6321       return virtual_cfa_rtx;
6322 #ifdef DWARF2_UNWIND_INFO
6323     case BUILT_IN_DWARF_SP_COLUMN:
6324       return expand_builtin_dwarf_sp_column ();
6325     case BUILT_IN_INIT_DWARF_REG_SIZES:
6326       expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6327       return const0_rtx;
6328 #endif
6329     case BUILT_IN_FROB_RETURN_ADDR:
6330       return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6331     case BUILT_IN_EXTRACT_RETURN_ADDR:
6332       return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6333     case BUILT_IN_EH_RETURN:
6334       expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6335                                 CALL_EXPR_ARG (exp, 1));
6336       return const0_rtx;
6337 #ifdef EH_RETURN_DATA_REGNO
6338     case BUILT_IN_EH_RETURN_DATA_REGNO:
6339       return expand_builtin_eh_return_data_regno (exp);
6340 #endif
6341     case BUILT_IN_EXTEND_POINTER:
6342       return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6343     case BUILT_IN_EH_POINTER:
6344       return expand_builtin_eh_pointer (exp);
6345     case BUILT_IN_EH_FILTER:
6346       return expand_builtin_eh_filter (exp);
6347     case BUILT_IN_EH_COPY_VALUES:
6348       return expand_builtin_eh_copy_values (exp);
6349
6350     case BUILT_IN_VA_START:
6351       return expand_builtin_va_start (exp);
6352     case BUILT_IN_VA_END:
6353       return expand_builtin_va_end (exp);
6354     case BUILT_IN_VA_COPY:
6355       return expand_builtin_va_copy (exp);
6356     case BUILT_IN_EXPECT:
6357       return expand_builtin_expect (exp, target);
6358     case BUILT_IN_PREFETCH:
6359       expand_builtin_prefetch (exp);
6360       return const0_rtx;
6361
6362     case BUILT_IN_PROFILE_FUNC_ENTER:
6363       return expand_builtin_profile_func (false);
6364     case BUILT_IN_PROFILE_FUNC_EXIT:
6365       return expand_builtin_profile_func (true);
6366
6367     case BUILT_IN_INIT_TRAMPOLINE:
6368       return expand_builtin_init_trampoline (exp);
6369     case BUILT_IN_ADJUST_TRAMPOLINE:
6370       return expand_builtin_adjust_trampoline (exp);
6371
6372     case BUILT_IN_FORK:
6373     case BUILT_IN_EXECL:
6374     case BUILT_IN_EXECV:
6375     case BUILT_IN_EXECLP:
6376     case BUILT_IN_EXECLE:
6377     case BUILT_IN_EXECVP:
6378     case BUILT_IN_EXECVE:
6379       target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6380       if (target)
6381         return target;
6382       break;
6383
6384     case BUILT_IN_FETCH_AND_ADD_1:
6385     case BUILT_IN_FETCH_AND_ADD_2:
6386     case BUILT_IN_FETCH_AND_ADD_4:
6387     case BUILT_IN_FETCH_AND_ADD_8:
6388     case BUILT_IN_FETCH_AND_ADD_16:
6389       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6390       target = expand_builtin_sync_operation (mode, exp, PLUS,
6391                                               false, target, ignore);
6392       if (target)
6393         return target;
6394       break;
6395
6396     case BUILT_IN_FETCH_AND_SUB_1:
6397     case BUILT_IN_FETCH_AND_SUB_2:
6398     case BUILT_IN_FETCH_AND_SUB_4:
6399     case BUILT_IN_FETCH_AND_SUB_8:
6400     case BUILT_IN_FETCH_AND_SUB_16:
6401       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6402       target = expand_builtin_sync_operation (mode, exp, MINUS,
6403                                               false, target, ignore);
6404       if (target)
6405         return target;
6406       break;
6407
6408     case BUILT_IN_FETCH_AND_OR_1:
6409     case BUILT_IN_FETCH_AND_OR_2:
6410     case BUILT_IN_FETCH_AND_OR_4:
6411     case BUILT_IN_FETCH_AND_OR_8:
6412     case BUILT_IN_FETCH_AND_OR_16:
6413       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6414       target = expand_builtin_sync_operation (mode, exp, IOR,
6415                                               false, target, ignore);
6416       if (target)
6417         return target;
6418       break;
6419
6420     case BUILT_IN_FETCH_AND_AND_1:
6421     case BUILT_IN_FETCH_AND_AND_2:
6422     case BUILT_IN_FETCH_AND_AND_4:
6423     case BUILT_IN_FETCH_AND_AND_8:
6424     case BUILT_IN_FETCH_AND_AND_16:
6425       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6426       target = expand_builtin_sync_operation (mode, exp, AND,
6427                                               false, target, ignore);
6428       if (target)
6429         return target;
6430       break;
6431
6432     case BUILT_IN_FETCH_AND_XOR_1:
6433     case BUILT_IN_FETCH_AND_XOR_2:
6434     case BUILT_IN_FETCH_AND_XOR_4:
6435     case BUILT_IN_FETCH_AND_XOR_8:
6436     case BUILT_IN_FETCH_AND_XOR_16:
6437       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6438       target = expand_builtin_sync_operation (mode, exp, XOR,
6439                                               false, target, ignore);
6440       if (target)
6441         return target;
6442       break;
6443
6444     case BUILT_IN_FETCH_AND_NAND_1:
6445     case BUILT_IN_FETCH_AND_NAND_2:
6446     case BUILT_IN_FETCH_AND_NAND_4:
6447     case BUILT_IN_FETCH_AND_NAND_8:
6448     case BUILT_IN_FETCH_AND_NAND_16:
6449       mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6450       target = expand_builtin_sync_operation (mode, exp, NOT,
6451                                               false, target, ignore);
6452       if (target)
6453         return target;
6454       break;
6455
6456     case BUILT_IN_ADD_AND_FETCH_1:
6457     case BUILT_IN_ADD_AND_FETCH_2:
6458     case BUILT_IN_ADD_AND_FETCH_4:
6459     case BUILT_IN_ADD_AND_FETCH_8:
6460     case BUILT_IN_ADD_AND_FETCH_16:
6461       mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6462       target = expand_builtin_sync_operation (mode, exp, PLUS,
6463                                               true, target, ignore);
6464       if (target)
6465         return target;
6466       break;
6467
6468     case BUILT_IN_SUB_AND_FETCH_1:
6469     case BUILT_IN_SUB_AND_FETCH_2:
6470     case BUILT_IN_SUB_AND_FETCH_4:
6471     case BUILT_IN_SUB_AND_FETCH_8:
6472     case BUILT_IN_SUB_AND_FETCH_16:
6473       mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6474       target = expand_builtin_sync_operation (mode, exp, MINUS,
6475                                               true, target, ignore);
6476       if (target)
6477         return target;
6478       break;
6479
6480     case BUILT_IN_OR_AND_FETCH_1:
6481     case BUILT_IN_OR_AND_FETCH_2:
6482     case BUILT_IN_OR_AND_FETCH_4:
6483     case BUILT_IN_OR_AND_FETCH_8:
6484     case BUILT_IN_OR_AND_FETCH_16:
6485       mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6486       target = expand_builtin_sync_operation (mode, exp, IOR,
6487                                               true, target, ignore);
6488       if (target)
6489         return target;
6490       break;
6491
6492     case BUILT_IN_AND_AND_FETCH_1:
6493     case BUILT_IN_AND_AND_FETCH_2:
6494     case BUILT_IN_AND_AND_FETCH_4:
6495     case BUILT_IN_AND_AND_FETCH_8:
6496     case BUILT_IN_AND_AND_FETCH_16:
6497       mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6498       target = expand_builtin_sync_operation (mode, exp, AND,
6499                                               true, target, ignore);
6500       if (target)
6501         return target;
6502       break;
6503
6504     case BUILT_IN_XOR_AND_FETCH_1:
6505     case BUILT_IN_XOR_AND_FETCH_2:
6506     case BUILT_IN_XOR_AND_FETCH_4:
6507     case BUILT_IN_XOR_AND_FETCH_8:
6508     case BUILT_IN_XOR_AND_FETCH_16:
6509       mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6510       target = expand_builtin_sync_operation (mode, exp, XOR,
6511                                               true, target, ignore);
6512       if (target)
6513         return target;
6514       break;
6515
6516     case BUILT_IN_NAND_AND_FETCH_1:
6517     case BUILT_IN_NAND_AND_FETCH_2:
6518     case BUILT_IN_NAND_AND_FETCH_4:
6519     case BUILT_IN_NAND_AND_FETCH_8:
6520     case BUILT_IN_NAND_AND_FETCH_16:
6521       mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6522       target = expand_builtin_sync_operation (mode, exp, NOT,
6523                                               true, target, ignore);
6524       if (target)
6525         return target;
6526       break;
6527
6528     case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6529     case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6530     case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6531     case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6532     case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6533       if (mode == VOIDmode)
6534         mode = TYPE_MODE (boolean_type_node);
6535       if (!target || !register_operand (target, mode))
6536         target = gen_reg_rtx (mode);
6537
6538       mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6539       target = expand_builtin_compare_and_swap (mode, exp, true, target);
6540       if (target)
6541         return target;
6542       break;
6543
6544     case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6545     case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6546     case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6547     case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6548     case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6549       mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6550       target = expand_builtin_compare_and_swap (mode, exp, false, target);
6551       if (target)
6552         return target;
6553       break;
6554
6555     case BUILT_IN_LOCK_TEST_AND_SET_1:
6556     case BUILT_IN_LOCK_TEST_AND_SET_2:
6557     case BUILT_IN_LOCK_TEST_AND_SET_4:
6558     case BUILT_IN_LOCK_TEST_AND_SET_8:
6559     case BUILT_IN_LOCK_TEST_AND_SET_16:
6560       mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6561       target = expand_builtin_lock_test_and_set (mode, exp, target);
6562       if (target)
6563         return target;
6564       break;
6565
6566     case BUILT_IN_LOCK_RELEASE_1:
6567     case BUILT_IN_LOCK_RELEASE_2:
6568     case BUILT_IN_LOCK_RELEASE_4:
6569     case BUILT_IN_LOCK_RELEASE_8:
6570     case BUILT_IN_LOCK_RELEASE_16:
6571       mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6572       expand_builtin_lock_release (mode, exp);
6573       return const0_rtx;
6574
6575     case BUILT_IN_SYNCHRONIZE:
6576       expand_builtin_synchronize ();
6577       return const0_rtx;
6578
6579     case BUILT_IN_OBJECT_SIZE:
6580       return expand_builtin_object_size (exp);
6581
6582     case BUILT_IN_MEMCPY_CHK:
6583     case BUILT_IN_MEMPCPY_CHK:
6584     case BUILT_IN_MEMMOVE_CHK:
6585     case BUILT_IN_MEMSET_CHK:
6586       target = expand_builtin_memory_chk (exp, target, mode, fcode);
6587       if (target)
6588         return target;
6589       break;
6590
6591     case BUILT_IN_STRCPY_CHK:
6592     case BUILT_IN_STPCPY_CHK:
6593     case BUILT_IN_STRNCPY_CHK:
6594     case BUILT_IN_STRCAT_CHK:
6595     case BUILT_IN_STRNCAT_CHK:
6596     case BUILT_IN_SNPRINTF_CHK:
6597     case BUILT_IN_VSNPRINTF_CHK:
6598       maybe_emit_chk_warning (exp, fcode);
6599       break;
6600
6601     case BUILT_IN_SPRINTF_CHK:
6602     case BUILT_IN_VSPRINTF_CHK:
6603       maybe_emit_sprintf_chk_warning (exp, fcode);
6604       break;
6605
6606     case BUILT_IN_FREE:
6607       maybe_emit_free_warning (exp);
6608       break;
6609
6610     default:    /* just do library call, if unknown builtin */
6611       break;
6612     }
6613
6614   /* The switch statement above can drop through to cause the function
6615      to be called normally.  */
6616   return expand_call (exp, target, ignore);
6617 }
6618
6619 /* Determine whether a tree node represents a call to a built-in
6620    function.  If the tree T is a call to a built-in function with
6621    the right number of arguments of the appropriate types, return
6622    the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6623    Otherwise the return value is END_BUILTINS.  */
6624
6625 enum built_in_function
6626 builtin_mathfn_code (const_tree t)
6627 {
6628   const_tree fndecl, arg, parmlist;
6629   const_tree argtype, parmtype;
6630   const_call_expr_arg_iterator iter;
6631
6632   if (TREE_CODE (t) != CALL_EXPR
6633       || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6634     return END_BUILTINS;
6635
6636   fndecl = get_callee_fndecl (t);
6637   if (fndecl == NULL_TREE
6638       || TREE_CODE (fndecl) != FUNCTION_DECL
6639       || ! DECL_BUILT_IN (fndecl)
6640       || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6641     return END_BUILTINS;
6642
6643   parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6644   init_const_call_expr_arg_iterator (t, &iter);
6645   for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6646     {
6647       /* If a function doesn't take a variable number of arguments,
6648          the last element in the list will have type `void'.  */
6649       parmtype = TREE_VALUE (parmlist);
6650       if (VOID_TYPE_P (parmtype))
6651         {
6652           if (more_const_call_expr_args_p (&iter))
6653             return END_BUILTINS;
6654           return DECL_FUNCTION_CODE (fndecl);
6655         }
6656
6657       if (! more_const_call_expr_args_p (&iter))
6658         return END_BUILTINS;
6659
6660       arg = next_const_call_expr_arg (&iter);
6661       argtype = TREE_TYPE (arg);
6662
6663       if (SCALAR_FLOAT_TYPE_P (parmtype))
6664         {
6665           if (! SCALAR_FLOAT_TYPE_P (argtype))
6666             return END_BUILTINS;
6667         }
6668       else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6669         {
6670           if (! COMPLEX_FLOAT_TYPE_P (argtype))
6671             return END_BUILTINS;
6672         }
6673       else if (POINTER_TYPE_P (parmtype))
6674         {
6675           if (! POINTER_TYPE_P (argtype))
6676             return END_BUILTINS;
6677         }
6678       else if (INTEGRAL_TYPE_P (parmtype))
6679         {
6680           if (! INTEGRAL_TYPE_P (argtype))
6681             return END_BUILTINS;
6682         }
6683       else
6684         return END_BUILTINS;
6685     }
6686
6687   /* Variable-length argument list.  */
6688   return DECL_FUNCTION_CODE (fndecl);
6689 }
6690
6691 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6692    evaluate to a constant.  */
6693
6694 static tree
6695 fold_builtin_constant_p (tree arg)
6696 {
6697   /* We return 1 for a numeric type that's known to be a constant
6698      value at compile-time or for an aggregate type that's a
6699      literal constant.  */
6700   STRIP_NOPS (arg);
6701
6702   /* If we know this is a constant, emit the constant of one.  */
6703   if (CONSTANT_CLASS_P (arg)
6704       || (TREE_CODE (arg) == CONSTRUCTOR
6705           && TREE_CONSTANT (arg)))
6706     return integer_one_node;
6707   if (TREE_CODE (arg) == ADDR_EXPR)
6708     {
6709        tree op = TREE_OPERAND (arg, 0);
6710        if (TREE_CODE (op) == STRING_CST
6711            || (TREE_CODE (op) == ARRAY_REF
6712                && integer_zerop (TREE_OPERAND (op, 1))
6713                && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6714          return integer_one_node;
6715     }
6716
6717   /* If this expression has side effects, show we don't know it to be a
6718      constant.  Likewise if it's a pointer or aggregate type since in
6719      those case we only want literals, since those are only optimized
6720      when generating RTL, not later.
6721      And finally, if we are compiling an initializer, not code, we
6722      need to return a definite result now; there's not going to be any
6723      more optimization done.  */
6724   if (TREE_SIDE_EFFECTS (arg)
6725       || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6726       || POINTER_TYPE_P (TREE_TYPE (arg))
6727       || cfun == 0
6728       || folding_initializer)
6729     return integer_zero_node;
6730
6731   return NULL_TREE;
6732 }
6733
6734 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6735    return it as a truthvalue.  */
6736
6737 static tree
6738 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6739 {
6740   tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6741
6742   fn = built_in_decls[BUILT_IN_EXPECT];
6743   arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6744   ret_type = TREE_TYPE (TREE_TYPE (fn));
6745   pred_type = TREE_VALUE (arg_types);
6746   expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6747
6748   pred = fold_convert_loc (loc, pred_type, pred);
6749   expected = fold_convert_loc (loc, expected_type, expected);
6750   call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6751
6752   return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6753                  build_int_cst (ret_type, 0));
6754 }
6755
6756 /* Fold a call to builtin_expect with arguments ARG0 and ARG1.  Return
6757    NULL_TREE if no simplification is possible.  */
6758
6759 static tree
6760 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6761 {
6762   tree inner, fndecl;
6763   enum tree_code code;
6764
6765   /* If this is a builtin_expect within a builtin_expect keep the
6766      inner one.  See through a comparison against a constant.  It
6767      might have been added to create a thruthvalue.  */
6768   inner = arg0;
6769   if (COMPARISON_CLASS_P (inner)
6770       && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6771     inner = TREE_OPERAND (inner, 0);
6772
6773   if (TREE_CODE (inner) == CALL_EXPR
6774       && (fndecl = get_callee_fndecl (inner))
6775       && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6776       && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6777     return arg0;
6778
6779   /* Distribute the expected value over short-circuiting operators.
6780      See through the cast from truthvalue_type_node to long.  */
6781   inner = arg0;
6782   while (TREE_CODE (inner) == NOP_EXPR
6783          && INTEGRAL_TYPE_P (TREE_TYPE (inner))
6784          && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0))))
6785     inner = TREE_OPERAND (inner, 0);
6786
6787   code = TREE_CODE (inner);
6788   if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6789     {
6790       tree op0 = TREE_OPERAND (inner, 0);
6791       tree op1 = TREE_OPERAND (inner, 1);
6792
6793       op0 = build_builtin_expect_predicate (loc, op0, arg1);
6794       op1 = build_builtin_expect_predicate (loc, op1, arg1);
6795       inner = build2 (code, TREE_TYPE (inner), op0, op1);
6796
6797       return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
6798     }
6799
6800   /* If the argument isn't invariant then there's nothing else we can do.  */
6801   if (!TREE_CONSTANT (arg0))
6802     return NULL_TREE;
6803
6804   /* If we expect that a comparison against the argument will fold to
6805      a constant return the constant.  In practice, this means a true
6806      constant or the address of a non-weak symbol.  */
6807   inner = arg0;
6808   STRIP_NOPS (inner);
6809   if (TREE_CODE (inner) == ADDR_EXPR)
6810     {
6811       do
6812         {
6813           inner = TREE_OPERAND (inner, 0);
6814         }
6815       while (TREE_CODE (inner) == COMPONENT_REF
6816              || TREE_CODE (inner) == ARRAY_REF);
6817       if ((TREE_CODE (inner) == VAR_DECL
6818            || TREE_CODE (inner) == FUNCTION_DECL)
6819           && DECL_WEAK (inner))
6820         return NULL_TREE;
6821     }
6822
6823   /* Otherwise, ARG0 already has the proper type for the return value.  */
6824   return arg0;
6825 }
6826
6827 /* Fold a call to __builtin_classify_type with argument ARG.  */
6828
6829 static tree
6830 fold_builtin_classify_type (tree arg)
6831 {
6832   if (arg == 0)
6833     return build_int_cst (NULL_TREE, no_type_class);
6834
6835   return build_int_cst (NULL_TREE, type_to_class (TREE_TYPE (arg)));
6836 }
6837
6838 /* Fold a call to __builtin_strlen with argument ARG.  */
6839
6840 static tree
6841 fold_builtin_strlen (location_t loc, tree type, tree arg)
6842 {
6843   if (!validate_arg (arg, POINTER_TYPE))
6844     return NULL_TREE;
6845   else
6846     {
6847       tree len = c_strlen (arg, 0);
6848
6849       if (len)
6850         return fold_convert_loc (loc, type, len);
6851
6852       return NULL_TREE;
6853     }
6854 }
6855
6856 /* Fold a call to __builtin_inf or __builtin_huge_val.  */
6857
6858 static tree
6859 fold_builtin_inf (location_t loc, tree type, int warn)
6860 {
6861   REAL_VALUE_TYPE real;
6862
6863   /* __builtin_inff is intended to be usable to define INFINITY on all
6864      targets.  If an infinity is not available, INFINITY expands "to a
6865      positive constant of type float that overflows at translation
6866      time", footnote "In this case, using INFINITY will violate the
6867      constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6868      Thus we pedwarn to ensure this constraint violation is
6869      diagnosed.  */
6870   if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6871     pedwarn (loc, 0, "target format does not support infinity");
6872
6873   real_inf (&real);
6874   return build_real (type, real);
6875 }
6876
6877 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG.  */
6878
6879 static tree
6880 fold_builtin_nan (tree arg, tree type, int quiet)
6881 {
6882   REAL_VALUE_TYPE real;
6883   const char *str;
6884
6885   if (!validate_arg (arg, POINTER_TYPE))
6886     return NULL_TREE;
6887   str = c_getstr (arg);
6888   if (!str)
6889     return NULL_TREE;
6890
6891   if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6892     return NULL_TREE;
6893
6894   return build_real (type, real);
6895 }
6896
6897 /* Return true if the floating point expression T has an integer value.
6898    We also allow +Inf, -Inf and NaN to be considered integer values.  */
6899
6900 static bool
6901 integer_valued_real_p (tree t)
6902 {
6903   switch (TREE_CODE (t))
6904     {
6905     case FLOAT_EXPR:
6906       return true;
6907
6908     case ABS_EXPR:
6909     case SAVE_EXPR:
6910       return integer_valued_real_p (TREE_OPERAND (t, 0));
6911
6912     case COMPOUND_EXPR:
6913     case MODIFY_EXPR:
6914     case BIND_EXPR:
6915       return integer_valued_real_p (TREE_OPERAND (t, 1));
6916
6917     case PLUS_EXPR:
6918     case MINUS_EXPR:
6919     case MULT_EXPR:
6920     case MIN_EXPR:
6921     case MAX_EXPR:
6922       return integer_valued_real_p (TREE_OPERAND (t, 0))
6923              && integer_valued_real_p (TREE_OPERAND (t, 1));
6924
6925     case COND_EXPR:
6926       return integer_valued_real_p (TREE_OPERAND (t, 1))
6927              && integer_valued_real_p (TREE_OPERAND (t, 2));
6928
6929     case REAL_CST:
6930       return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
6931
6932     case NOP_EXPR:
6933       {
6934         tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6935         if (TREE_CODE (type) == INTEGER_TYPE)
6936           return true;
6937         if (TREE_CODE (type) == REAL_TYPE)
6938           return integer_valued_real_p (TREE_OPERAND (t, 0));
6939         break;
6940       }
6941
6942     case CALL_EXPR:
6943       switch (builtin_mathfn_code (t))
6944         {
6945         CASE_FLT_FN (BUILT_IN_CEIL):
6946         CASE_FLT_FN (BUILT_IN_FLOOR):
6947         CASE_FLT_FN (BUILT_IN_NEARBYINT):
6948         CASE_FLT_FN (BUILT_IN_RINT):
6949         CASE_FLT_FN (BUILT_IN_ROUND):
6950         CASE_FLT_FN (BUILT_IN_TRUNC):
6951           return true;
6952
6953         CASE_FLT_FN (BUILT_IN_FMIN):
6954         CASE_FLT_FN (BUILT_IN_FMAX):
6955           return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
6956             && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
6957
6958         default:
6959           break;
6960         }
6961       break;
6962
6963     default:
6964       break;
6965     }
6966   return false;
6967 }
6968
6969 /* FNDECL is assumed to be a builtin where truncation can be propagated
6970    across (for instance floor((double)f) == (double)floorf (f).
6971    Do the transformation for a call with argument ARG.  */
6972
6973 static tree
6974 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
6975 {
6976   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6977
6978   if (!validate_arg (arg, REAL_TYPE))
6979     return NULL_TREE;
6980
6981   /* Integer rounding functions are idempotent.  */
6982   if (fcode == builtin_mathfn_code (arg))
6983     return arg;
6984
6985   /* If argument is already integer valued, and we don't need to worry
6986      about setting errno, there's no need to perform rounding.  */
6987   if (! flag_errno_math && integer_valued_real_p (arg))
6988     return arg;
6989
6990   if (optimize)
6991     {
6992       tree arg0 = strip_float_extensions (arg);
6993       tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6994       tree newtype = TREE_TYPE (arg0);
6995       tree decl;
6996
6997       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6998           && (decl = mathfn_built_in (newtype, fcode)))
6999         return fold_convert_loc (loc, ftype,
7000                                  build_call_expr_loc (loc, decl, 1,
7001                                                   fold_convert_loc (loc,
7002                                                                     newtype,
7003                                                                     arg0)));
7004     }
7005   return NULL_TREE;
7006 }
7007
7008 /* FNDECL is assumed to be builtin which can narrow the FP type of
7009    the argument, for instance lround((double)f) -> lroundf (f).
7010    Do the transformation for a call with argument ARG.  */
7011
7012 static tree
7013 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7014 {
7015   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7016
7017   if (!validate_arg (arg, REAL_TYPE))
7018     return NULL_TREE;
7019
7020   /* If argument is already integer valued, and we don't need to worry
7021      about setting errno, there's no need to perform rounding.  */
7022   if (! flag_errno_math && integer_valued_real_p (arg))
7023     return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7024                         TREE_TYPE (TREE_TYPE (fndecl)), arg);
7025
7026   if (optimize)
7027     {
7028       tree ftype = TREE_TYPE (arg);
7029       tree arg0 = strip_float_extensions (arg);
7030       tree newtype = TREE_TYPE (arg0);
7031       tree decl;
7032
7033       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7034           && (decl = mathfn_built_in (newtype, fcode)))
7035         return build_call_expr_loc (loc, decl, 1,
7036                                 fold_convert_loc (loc, newtype, arg0));
7037     }
7038
7039   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7040      sizeof (long long) == sizeof (long).  */
7041   if (TYPE_PRECISION (long_long_integer_type_node)
7042       == TYPE_PRECISION (long_integer_type_node))
7043     {
7044       tree newfn = NULL_TREE;
7045       switch (fcode)
7046         {
7047         CASE_FLT_FN (BUILT_IN_LLCEIL):
7048           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7049           break;
7050
7051         CASE_FLT_FN (BUILT_IN_LLFLOOR):
7052           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7053           break;
7054
7055         CASE_FLT_FN (BUILT_IN_LLROUND):
7056           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7057           break;
7058
7059         CASE_FLT_FN (BUILT_IN_LLRINT):
7060           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7061           break;
7062
7063         default:
7064           break;
7065         }
7066
7067       if (newfn)
7068         {
7069           tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7070           return fold_convert_loc (loc,
7071                                    TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7072         }
7073     }
7074
7075   return NULL_TREE;
7076 }
7077
7078 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG.  TYPE is the
7079    return type.  Return NULL_TREE if no simplification can be made.  */
7080
7081 static tree
7082 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7083 {
7084   tree res;
7085
7086   if (!validate_arg (arg, COMPLEX_TYPE)
7087       || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7088     return NULL_TREE;
7089
7090   /* Calculate the result when the argument is a constant.  */
7091   if (TREE_CODE (arg) == COMPLEX_CST
7092       && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7093                               type, mpfr_hypot)))
7094     return res;
7095
7096   if (TREE_CODE (arg) == COMPLEX_EXPR)
7097     {
7098       tree real = TREE_OPERAND (arg, 0);
7099       tree imag = TREE_OPERAND (arg, 1);
7100
7101       /* If either part is zero, cabs is fabs of the other.  */
7102       if (real_zerop (real))
7103         return fold_build1_loc (loc, ABS_EXPR, type, imag);
7104       if (real_zerop (imag))
7105         return fold_build1_loc (loc, ABS_EXPR, type, real);
7106
7107       /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7108       if (flag_unsafe_math_optimizations
7109           && operand_equal_p (real, imag, OEP_PURE_SAME))
7110         {
7111           const REAL_VALUE_TYPE sqrt2_trunc
7112             = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7113           STRIP_NOPS (real);
7114           return fold_build2_loc (loc, MULT_EXPR, type,
7115                               fold_build1_loc (loc, ABS_EXPR, type, real),
7116                               build_real (type, sqrt2_trunc));
7117         }
7118     }
7119
7120   /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z).  */
7121   if (TREE_CODE (arg) == NEGATE_EXPR
7122       || TREE_CODE (arg) == CONJ_EXPR)
7123     return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7124
7125   /* Don't do this when optimizing for size.  */
7126   if (flag_unsafe_math_optimizations
7127       && optimize && optimize_function_for_speed_p (cfun))
7128     {
7129       tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7130
7131       if (sqrtfn != NULL_TREE)
7132         {
7133           tree rpart, ipart, result;
7134
7135           arg = builtin_save_expr (arg);
7136
7137           rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7138           ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7139
7140           rpart = builtin_save_expr (rpart);
7141           ipart = builtin_save_expr (ipart);
7142
7143           result = fold_build2_loc (loc, PLUS_EXPR, type,
7144                                 fold_build2_loc (loc, MULT_EXPR, type,
7145                                              rpart, rpart),
7146                                 fold_build2_loc (loc, MULT_EXPR, type,
7147                                              ipart, ipart));
7148
7149           return build_call_expr_loc (loc, sqrtfn, 1, result);
7150         }
7151     }
7152
7153   return NULL_TREE;
7154 }
7155
7156 /* Build a complex (inf +- 0i) for the result of cproj.  TYPE is the
7157    complex tree type of the result.  If NEG is true, the imaginary
7158    zero is negative.  */
7159
7160 static tree
7161 build_complex_cproj (tree type, bool neg)
7162 {
7163   REAL_VALUE_TYPE rinf, rzero = dconst0;
7164   
7165   real_inf (&rinf);
7166   rzero.sign = neg;
7167   return build_complex (type, build_real (TREE_TYPE (type), rinf),
7168                         build_real (TREE_TYPE (type), rzero));
7169 }
7170
7171 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG.  TYPE is the
7172    return type.  Return NULL_TREE if no simplification can be made.  */
7173
7174 static tree
7175 fold_builtin_cproj (location_t loc, tree arg, tree type)
7176 {
7177   if (!validate_arg (arg, COMPLEX_TYPE)
7178       || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7179     return NULL_TREE;
7180
7181   /* If there are no infinities, return arg.  */
7182   if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7183     return non_lvalue_loc (loc, arg);
7184
7185   /* Calculate the result when the argument is a constant.  */
7186   if (TREE_CODE (arg) == COMPLEX_CST)
7187     {
7188       const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7189       const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7190       
7191       if (real_isinf (real) || real_isinf (imag))
7192         return build_complex_cproj (type, imag->sign);
7193       else
7194         return arg;
7195     }
7196   else if (TREE_CODE (arg) == COMPLEX_EXPR)
7197     {
7198       tree real = TREE_OPERAND (arg, 0);
7199       tree imag = TREE_OPERAND (arg, 1);
7200
7201       STRIP_NOPS (real);
7202       STRIP_NOPS (imag);
7203       
7204       /* If the real part is inf and the imag part is known to be
7205          nonnegative, return (inf + 0i).  Remember side-effects are
7206          possible in the imag part.  */
7207       if (TREE_CODE (real) == REAL_CST
7208           && real_isinf (TREE_REAL_CST_PTR (real))
7209           && tree_expr_nonnegative_p (imag))
7210         return omit_one_operand_loc (loc, type,
7211                                      build_complex_cproj (type, false),
7212                                      arg);
7213       
7214       /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7215          Remember side-effects are possible in the real part.  */
7216       if (TREE_CODE (imag) == REAL_CST
7217           && real_isinf (TREE_REAL_CST_PTR (imag)))
7218         return
7219           omit_one_operand_loc (loc, type,
7220                                 build_complex_cproj (type, TREE_REAL_CST_PTR
7221                                                      (imag)->sign), arg);
7222     }
7223
7224   return NULL_TREE;
7225 }
7226
7227 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7228    Return NULL_TREE if no simplification can be made.  */
7229
7230 static tree
7231 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7232 {
7233
7234   enum built_in_function fcode;
7235   tree res;
7236
7237   if (!validate_arg (arg, REAL_TYPE))
7238     return NULL_TREE;
7239
7240   /* Calculate the result when the argument is a constant.  */
7241   if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7242     return res;
7243
7244   /* Optimize sqrt(expN(x)) = expN(x*0.5).  */
7245   fcode = builtin_mathfn_code (arg);
7246   if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7247     {
7248       tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7249       arg = fold_build2_loc (loc, MULT_EXPR, type,
7250                          CALL_EXPR_ARG (arg, 0),
7251                          build_real (type, dconsthalf));
7252       return build_call_expr_loc (loc, expfn, 1, arg);
7253     }
7254
7255   /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)).  */
7256   if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7257     {
7258       tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7259
7260       if (powfn)
7261         {
7262           tree arg0 = CALL_EXPR_ARG (arg, 0);
7263           tree tree_root;
7264           /* The inner root was either sqrt or cbrt.  */
7265           /* This was a conditional expression but it triggered a bug
7266              in Sun C 5.5.  */
7267           REAL_VALUE_TYPE dconstroot;
7268           if (BUILTIN_SQRT_P (fcode))
7269             dconstroot = dconsthalf;
7270           else
7271             dconstroot = dconst_third ();
7272
7273           /* Adjust for the outer root.  */
7274           SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7275           dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7276           tree_root = build_real (type, dconstroot);
7277           return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7278         }
7279     }
7280
7281   /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5).  */
7282   if (flag_unsafe_math_optimizations
7283       && (fcode == BUILT_IN_POW
7284           || fcode == BUILT_IN_POWF
7285           || fcode == BUILT_IN_POWL))
7286     {
7287       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7288       tree arg0 = CALL_EXPR_ARG (arg, 0);
7289       tree arg1 = CALL_EXPR_ARG (arg, 1);
7290       tree narg1;
7291       if (!tree_expr_nonnegative_p (arg0))
7292         arg0 = build1 (ABS_EXPR, type, arg0);
7293       narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7294                            build_real (type, dconsthalf));
7295       return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7296     }
7297
7298   return NULL_TREE;
7299 }
7300
7301 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7302    Return NULL_TREE if no simplification can be made.  */
7303
7304 static tree
7305 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7306 {
7307   const enum built_in_function fcode = builtin_mathfn_code (arg);
7308   tree res;
7309
7310   if (!validate_arg (arg, REAL_TYPE))
7311     return NULL_TREE;
7312
7313   /* Calculate the result when the argument is a constant.  */
7314   if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7315     return res;
7316
7317   if (flag_unsafe_math_optimizations)
7318     {
7319       /* Optimize cbrt(expN(x)) -> expN(x/3).  */
7320       if (BUILTIN_EXPONENT_P (fcode))
7321         {
7322           tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7323           const REAL_VALUE_TYPE third_trunc =
7324             real_value_truncate (TYPE_MODE (type), dconst_third ());
7325           arg = fold_build2_loc (loc, MULT_EXPR, type,
7326                              CALL_EXPR_ARG (arg, 0),
7327                              build_real (type, third_trunc));
7328           return build_call_expr_loc (loc, expfn, 1, arg);
7329         }
7330
7331       /* Optimize cbrt(sqrt(x)) -> pow(x,1/6).  */
7332       if (BUILTIN_SQRT_P (fcode))
7333         {
7334           tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7335
7336           if (powfn)
7337             {
7338               tree arg0 = CALL_EXPR_ARG (arg, 0);
7339               tree tree_root;
7340               REAL_VALUE_TYPE dconstroot = dconst_third ();
7341
7342               SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7343               dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7344               tree_root = build_real (type, dconstroot);
7345               return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7346             }
7347         }
7348
7349       /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative.  */
7350       if (BUILTIN_CBRT_P (fcode))
7351         {
7352           tree arg0 = CALL_EXPR_ARG (arg, 0);
7353           if (tree_expr_nonnegative_p (arg0))
7354             {
7355               tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7356
7357               if (powfn)
7358                 {
7359                   tree tree_root;
7360                   REAL_VALUE_TYPE dconstroot;
7361
7362                   real_arithmetic (&dconstroot, MULT_EXPR,
7363                                    dconst_third_ptr (), dconst_third_ptr ());
7364                   dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7365                   tree_root = build_real (type, dconstroot);
7366                   return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7367                 }
7368             }
7369         }
7370
7371       /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative.  */
7372       if (fcode == BUILT_IN_POW
7373           || fcode == BUILT_IN_POWF
7374           || fcode == BUILT_IN_POWL)
7375         {
7376           tree arg00 = CALL_EXPR_ARG (arg, 0);
7377           tree arg01 = CALL_EXPR_ARG (arg, 1);
7378           if (tree_expr_nonnegative_p (arg00))
7379             {
7380               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7381               const REAL_VALUE_TYPE dconstroot
7382                 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7383               tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7384                                          build_real (type, dconstroot));
7385               return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7386             }
7387         }
7388     }
7389   return NULL_TREE;
7390 }
7391
7392 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7393    TYPE is the type of the return value.  Return NULL_TREE if no
7394    simplification can be made.  */
7395
7396 static tree
7397 fold_builtin_cos (location_t loc,
7398                   tree arg, tree type, tree fndecl)
7399 {
7400   tree res, narg;
7401
7402   if (!validate_arg (arg, REAL_TYPE))
7403     return NULL_TREE;
7404
7405   /* Calculate the result when the argument is a constant.  */
7406   if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7407     return res;
7408
7409   /* Optimize cos(-x) into cos (x).  */
7410   if ((narg = fold_strip_sign_ops (arg)))
7411     return build_call_expr_loc (loc, fndecl, 1, narg);
7412
7413   return NULL_TREE;
7414 }
7415
7416 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7417    Return NULL_TREE if no simplification can be made.  */
7418
7419 static tree
7420 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7421 {
7422   if (validate_arg (arg, REAL_TYPE))
7423     {
7424       tree res, narg;
7425
7426       /* Calculate the result when the argument is a constant.  */
7427       if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7428         return res;
7429
7430       /* Optimize cosh(-x) into cosh (x).  */
7431       if ((narg = fold_strip_sign_ops (arg)))
7432         return build_call_expr_loc (loc, fndecl, 1, narg);
7433     }
7434
7435   return NULL_TREE;
7436 }
7437
7438 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7439    argument ARG.  TYPE is the type of the return value.  Return
7440    NULL_TREE if no simplification can be made.  */
7441
7442 static tree
7443 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7444                    bool hyper)
7445 {
7446   if (validate_arg (arg, COMPLEX_TYPE)
7447       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7448     {
7449       tree tmp;
7450
7451       /* Calculate the result when the argument is a constant.  */
7452       if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7453         return tmp;
7454
7455       /* Optimize fn(-x) into fn(x).  */
7456       if ((tmp = fold_strip_sign_ops (arg)))
7457         return build_call_expr_loc (loc, fndecl, 1, tmp);
7458     }
7459
7460   return NULL_TREE;
7461 }
7462
7463 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7464    Return NULL_TREE if no simplification can be made.  */
7465
7466 static tree
7467 fold_builtin_tan (tree arg, tree type)
7468 {
7469   enum built_in_function fcode;
7470   tree res;
7471
7472   if (!validate_arg (arg, REAL_TYPE))
7473     return NULL_TREE;
7474
7475   /* Calculate the result when the argument is a constant.  */
7476   if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7477     return res;
7478
7479   /* Optimize tan(atan(x)) = x.  */
7480   fcode = builtin_mathfn_code (arg);
7481   if (flag_unsafe_math_optimizations
7482       && (fcode == BUILT_IN_ATAN
7483           || fcode == BUILT_IN_ATANF
7484           || fcode == BUILT_IN_ATANL))
7485     return CALL_EXPR_ARG (arg, 0);
7486
7487   return NULL_TREE;
7488 }
7489
7490 /* Fold function call to builtin sincos, sincosf, or sincosl.  Return
7491    NULL_TREE if no simplification can be made.  */
7492
7493 static tree
7494 fold_builtin_sincos (location_t loc,
7495                      tree arg0, tree arg1, tree arg2)
7496 {
7497   tree type;
7498   tree res, fn, call;
7499
7500   if (!validate_arg (arg0, REAL_TYPE)
7501       || !validate_arg (arg1, POINTER_TYPE)
7502       || !validate_arg (arg2, POINTER_TYPE))
7503     return NULL_TREE;
7504
7505   type = TREE_TYPE (arg0);
7506
7507   /* Calculate the result when the argument is a constant.  */
7508   if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7509     return res;
7510
7511   /* Canonicalize sincos to cexpi.  */
7512   if (!TARGET_C99_FUNCTIONS)
7513     return NULL_TREE;
7514   fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7515   if (!fn)
7516     return NULL_TREE;
7517
7518   call = build_call_expr_loc (loc, fn, 1, arg0);
7519   call = builtin_save_expr (call);
7520
7521   return build2 (COMPOUND_EXPR, void_type_node,
7522                  build2 (MODIFY_EXPR, void_type_node,
7523                          build_fold_indirect_ref_loc (loc, arg1),
7524                          build1 (IMAGPART_EXPR, type, call)),
7525                  build2 (MODIFY_EXPR, void_type_node,
7526                          build_fold_indirect_ref_loc (loc, arg2),
7527                          build1 (REALPART_EXPR, type, call)));
7528 }
7529
7530 /* Fold function call to builtin cexp, cexpf, or cexpl.  Return
7531    NULL_TREE if no simplification can be made.  */
7532
7533 static tree
7534 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7535 {
7536   tree rtype;
7537   tree realp, imagp, ifn;
7538   tree res;
7539
7540   if (!validate_arg (arg0, COMPLEX_TYPE)
7541       || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7542     return NULL_TREE;
7543
7544   /* Calculate the result when the argument is a constant.  */
7545   if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7546     return res;
7547
7548   rtype = TREE_TYPE (TREE_TYPE (arg0));
7549
7550   /* In case we can figure out the real part of arg0 and it is constant zero
7551      fold to cexpi.  */
7552   if (!TARGET_C99_FUNCTIONS)
7553     return NULL_TREE;
7554   ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7555   if (!ifn)
7556     return NULL_TREE;
7557
7558   if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7559       && real_zerop (realp))
7560     {
7561       tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7562       return build_call_expr_loc (loc, ifn, 1, narg);
7563     }
7564
7565   /* In case we can easily decompose real and imaginary parts split cexp
7566      to exp (r) * cexpi (i).  */
7567   if (flag_unsafe_math_optimizations
7568       && realp)
7569     {
7570       tree rfn, rcall, icall;
7571
7572       rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7573       if (!rfn)
7574         return NULL_TREE;
7575
7576       imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7577       if (!imagp)
7578         return NULL_TREE;
7579
7580       icall = build_call_expr_loc (loc, ifn, 1, imagp);
7581       icall = builtin_save_expr (icall);
7582       rcall = build_call_expr_loc (loc, rfn, 1, realp);
7583       rcall = builtin_save_expr (rcall);
7584       return fold_build2_loc (loc, COMPLEX_EXPR, type,
7585                           fold_build2_loc (loc, MULT_EXPR, rtype,
7586                                        rcall,
7587                                        fold_build1_loc (loc, REALPART_EXPR,
7588                                                     rtype, icall)),
7589                           fold_build2_loc (loc, MULT_EXPR, rtype,
7590                                        rcall,
7591                                        fold_build1_loc (loc, IMAGPART_EXPR,
7592                                                     rtype, icall)));
7593     }
7594
7595   return NULL_TREE;
7596 }
7597
7598 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7599    Return NULL_TREE if no simplification can be made.  */
7600
7601 static tree
7602 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7603 {
7604   if (!validate_arg (arg, REAL_TYPE))
7605     return NULL_TREE;
7606
7607   /* Optimize trunc of constant value.  */
7608   if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7609     {
7610       REAL_VALUE_TYPE r, x;
7611       tree type = TREE_TYPE (TREE_TYPE (fndecl));
7612
7613       x = TREE_REAL_CST (arg);
7614       real_trunc (&r, TYPE_MODE (type), &x);
7615       return build_real (type, r);
7616     }
7617
7618   return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7619 }
7620
7621 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7622    Return NULL_TREE if no simplification can be made.  */
7623
7624 static tree
7625 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7626 {
7627   if (!validate_arg (arg, REAL_TYPE))
7628     return NULL_TREE;
7629
7630   /* Optimize floor of constant value.  */
7631   if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7632     {
7633       REAL_VALUE_TYPE x;
7634
7635       x = TREE_REAL_CST (arg);
7636       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7637         {
7638           tree type = TREE_TYPE (TREE_TYPE (fndecl));
7639           REAL_VALUE_TYPE r;
7640
7641           real_floor (&r, TYPE_MODE (type), &x);
7642           return build_real (type, r);
7643         }
7644     }
7645
7646   /* Fold floor (x) where x is nonnegative to trunc (x).  */
7647   if (tree_expr_nonnegative_p (arg))
7648     {
7649       tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7650       if (truncfn)
7651         return build_call_expr_loc (loc, truncfn, 1, arg);
7652     }
7653
7654   return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7655 }
7656
7657 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7658    Return NULL_TREE if no simplification can be made.  */
7659
7660 static tree
7661 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7662 {
7663   if (!validate_arg (arg, REAL_TYPE))
7664     return NULL_TREE;
7665
7666   /* Optimize ceil of constant value.  */
7667   if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7668     {
7669       REAL_VALUE_TYPE x;
7670
7671       x = TREE_REAL_CST (arg);
7672       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7673         {
7674           tree type = TREE_TYPE (TREE_TYPE (fndecl));
7675           REAL_VALUE_TYPE r;
7676
7677           real_ceil (&r, TYPE_MODE (type), &x);
7678           return build_real (type, r);
7679         }
7680     }
7681
7682   return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7683 }
7684
7685 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7686    Return NULL_TREE if no simplification can be made.  */
7687
7688 static tree
7689 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7690 {
7691   if (!validate_arg (arg, REAL_TYPE))
7692     return NULL_TREE;
7693
7694   /* Optimize round of constant value.  */
7695   if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7696     {
7697       REAL_VALUE_TYPE x;
7698
7699       x = TREE_REAL_CST (arg);
7700       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7701         {
7702           tree type = TREE_TYPE (TREE_TYPE (fndecl));
7703           REAL_VALUE_TYPE r;
7704
7705           real_round (&r, TYPE_MODE (type), &x);
7706           return build_real (type, r);
7707         }
7708     }
7709
7710   return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7711 }
7712
7713 /* Fold function call to builtin lround, lroundf or lroundl (or the
7714    corresponding long long versions) and other rounding functions.  ARG
7715    is the argument to the call.  Return NULL_TREE if no simplification
7716    can be made.  */
7717
7718 static tree
7719 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7720 {
7721   if (!validate_arg (arg, REAL_TYPE))
7722     return NULL_TREE;
7723
7724   /* Optimize lround of constant value.  */
7725   if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7726     {
7727       const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7728
7729       if (real_isfinite (&x))
7730         {
7731           tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7732           tree ftype = TREE_TYPE (arg);
7733           double_int val;
7734           REAL_VALUE_TYPE r;
7735
7736           switch (DECL_FUNCTION_CODE (fndecl))
7737             {
7738             CASE_FLT_FN (BUILT_IN_LFLOOR):
7739             CASE_FLT_FN (BUILT_IN_LLFLOOR):
7740               real_floor (&r, TYPE_MODE (ftype), &x);
7741               break;
7742
7743             CASE_FLT_FN (BUILT_IN_LCEIL):
7744             CASE_FLT_FN (BUILT_IN_LLCEIL):
7745               real_ceil (&r, TYPE_MODE (ftype), &x);
7746               break;
7747
7748             CASE_FLT_FN (BUILT_IN_LROUND):
7749             CASE_FLT_FN (BUILT_IN_LLROUND):
7750               real_round (&r, TYPE_MODE (ftype), &x);
7751               break;
7752
7753             default:
7754               gcc_unreachable ();
7755             }
7756
7757           real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
7758           if (double_int_fits_to_tree_p (itype, val))
7759             return double_int_to_tree (itype, val);
7760         }
7761     }
7762
7763   switch (DECL_FUNCTION_CODE (fndecl))
7764     {
7765     CASE_FLT_FN (BUILT_IN_LFLOOR):
7766     CASE_FLT_FN (BUILT_IN_LLFLOOR):
7767       /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x).  */
7768       if (tree_expr_nonnegative_p (arg))
7769         return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7770                             TREE_TYPE (TREE_TYPE (fndecl)), arg);
7771       break;
7772     default:;
7773     }
7774
7775   return fold_fixed_mathfn (loc, fndecl, arg);
7776 }
7777
7778 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7779    and their long and long long variants (i.e. ffsl and ffsll).  ARG is
7780    the argument to the call.  Return NULL_TREE if no simplification can
7781    be made.  */
7782
7783 static tree
7784 fold_builtin_bitop (tree fndecl, tree arg)
7785 {
7786   if (!validate_arg (arg, INTEGER_TYPE))
7787     return NULL_TREE;
7788
7789   /* Optimize for constant argument.  */
7790   if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7791     {
7792       HOST_WIDE_INT hi, width, result;
7793       unsigned HOST_WIDE_INT lo;
7794       tree type;
7795
7796       type = TREE_TYPE (arg);
7797       width = TYPE_PRECISION (type);
7798       lo = TREE_INT_CST_LOW (arg);
7799
7800       /* Clear all the bits that are beyond the type's precision.  */
7801       if (width > HOST_BITS_PER_WIDE_INT)
7802         {
7803           hi = TREE_INT_CST_HIGH (arg);
7804           if (width < 2 * HOST_BITS_PER_WIDE_INT)
7805             hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7806         }
7807       else
7808         {
7809           hi = 0;
7810           if (width < HOST_BITS_PER_WIDE_INT)
7811             lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7812         }
7813
7814       switch (DECL_FUNCTION_CODE (fndecl))
7815         {
7816         CASE_INT_FN (BUILT_IN_FFS):
7817           if (lo != 0)
7818             result = ffs_hwi (lo);
7819           else if (hi != 0)
7820             result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
7821           else
7822             result = 0;
7823           break;
7824
7825         CASE_INT_FN (BUILT_IN_CLZ):
7826           if (hi != 0)
7827             result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7828           else if (lo != 0)
7829             result = width - floor_log2 (lo) - 1;
7830           else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7831             result = width;
7832           break;
7833
7834         CASE_INT_FN (BUILT_IN_CTZ):
7835           if (lo != 0)
7836             result = ctz_hwi (lo);
7837           else if (hi != 0)
7838             result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
7839           else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7840             result = width;
7841           break;
7842
7843         CASE_INT_FN (BUILT_IN_POPCOUNT):
7844           result = 0;
7845           while (lo)
7846             result++, lo &= lo - 1;
7847           while (hi)
7848             result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
7849           break;
7850
7851         CASE_INT_FN (BUILT_IN_PARITY):
7852           result = 0;
7853           while (lo)
7854             result++, lo &= lo - 1;
7855           while (hi)
7856             result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
7857           result &= 1;
7858           break;
7859
7860         default:
7861           gcc_unreachable ();
7862         }
7863
7864       return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7865     }
7866
7867   return NULL_TREE;
7868 }
7869
7870 /* Fold function call to builtin_bswap and the long and long long
7871    variants.  Return NULL_TREE if no simplification can be made.  */
7872 static tree
7873 fold_builtin_bswap (tree fndecl, tree arg)
7874 {
7875   if (! validate_arg (arg, INTEGER_TYPE))
7876     return NULL_TREE;
7877
7878   /* Optimize constant value.  */
7879   if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7880     {
7881       HOST_WIDE_INT hi, width, r_hi = 0;
7882       unsigned HOST_WIDE_INT lo, r_lo = 0;
7883       tree type;
7884
7885       type = TREE_TYPE (arg);
7886       width = TYPE_PRECISION (type);
7887       lo = TREE_INT_CST_LOW (arg);
7888       hi = TREE_INT_CST_HIGH (arg);
7889
7890       switch (DECL_FUNCTION_CODE (fndecl))
7891         {
7892           case BUILT_IN_BSWAP32:
7893           case BUILT_IN_BSWAP64:
7894             {
7895               int s;
7896
7897               for (s = 0; s < width; s += 8)
7898                 {
7899                   int d = width - s - 8;
7900                   unsigned HOST_WIDE_INT byte;
7901
7902                   if (s < HOST_BITS_PER_WIDE_INT)
7903                     byte = (lo >> s) & 0xff;
7904                   else
7905                     byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7906
7907                   if (d < HOST_BITS_PER_WIDE_INT)
7908                     r_lo |= byte << d;
7909                   else
7910                     r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7911                 }
7912             }
7913
7914             break;
7915
7916         default:
7917           gcc_unreachable ();
7918         }
7919
7920       if (width < HOST_BITS_PER_WIDE_INT)
7921         return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7922       else
7923         return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7924     }
7925
7926   return NULL_TREE;
7927 }
7928
7929 /* A subroutine of fold_builtin to fold the various logarithmic
7930    functions.  Return NULL_TREE if no simplification can me made.
7931    FUNC is the corresponding MPFR logarithm function.  */
7932
7933 static tree
7934 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
7935                         int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7936 {
7937   if (validate_arg (arg, REAL_TYPE))
7938     {
7939       tree type = TREE_TYPE (TREE_TYPE (fndecl));
7940       tree res;
7941       const enum built_in_function fcode = builtin_mathfn_code (arg);
7942
7943       /* Calculate the result when the argument is a constant.  */
7944       if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7945         return res;
7946
7947       /* Special case, optimize logN(expN(x)) = x.  */
7948       if (flag_unsafe_math_optimizations
7949           && ((func == mpfr_log
7950                && (fcode == BUILT_IN_EXP
7951                    || fcode == BUILT_IN_EXPF
7952                    || fcode == BUILT_IN_EXPL))
7953               || (func == mpfr_log2
7954                   && (fcode == BUILT_IN_EXP2
7955                       || fcode == BUILT_IN_EXP2F
7956                       || fcode == BUILT_IN_EXP2L))
7957               || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7958         return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
7959
7960       /* Optimize logN(func()) for various exponential functions.  We
7961          want to determine the value "x" and the power "exponent" in
7962          order to transform logN(x**exponent) into exponent*logN(x).  */
7963       if (flag_unsafe_math_optimizations)
7964         {
7965           tree exponent = 0, x = 0;
7966
7967           switch (fcode)
7968           {
7969           CASE_FLT_FN (BUILT_IN_EXP):
7970             /* Prepare to do logN(exp(exponent) -> exponent*logN(e).  */
7971             x = build_real (type, real_value_truncate (TYPE_MODE (type),
7972                                                        dconst_e ()));
7973             exponent = CALL_EXPR_ARG (arg, 0);
7974             break;
7975           CASE_FLT_FN (BUILT_IN_EXP2):
7976             /* Prepare to do logN(exp2(exponent) -> exponent*logN(2).  */
7977             x = build_real (type, dconst2);
7978             exponent = CALL_EXPR_ARG (arg, 0);
7979             break;
7980           CASE_FLT_FN (BUILT_IN_EXP10):
7981           CASE_FLT_FN (BUILT_IN_POW10):
7982             /* Prepare to do logN(exp10(exponent) -> exponent*logN(10).  */
7983             {
7984               REAL_VALUE_TYPE dconst10;
7985               real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
7986               x = build_real (type, dconst10);
7987             }
7988             exponent = CALL_EXPR_ARG (arg, 0);
7989             break;
7990           CASE_FLT_FN (BUILT_IN_SQRT):
7991             /* Prepare to do logN(sqrt(x) -> 0.5*logN(x).  */
7992             x = CALL_EXPR_ARG (arg, 0);
7993             exponent = build_real (type, dconsthalf);
7994             break;
7995           CASE_FLT_FN (BUILT_IN_CBRT):
7996             /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x).  */
7997             x = CALL_EXPR_ARG (arg, 0);
7998             exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7999                                                               dconst_third ()));
8000             break;
8001           CASE_FLT_FN (BUILT_IN_POW):
8002             /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x).  */
8003             x = CALL_EXPR_ARG (arg, 0);
8004             exponent = CALL_EXPR_ARG (arg, 1);
8005             break;
8006           default:
8007             break;
8008           }
8009
8010           /* Now perform the optimization.  */
8011           if (x && exponent)
8012             {
8013               tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8014               return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8015             }
8016         }
8017     }
8018
8019   return NULL_TREE;
8020 }
8021
8022 /* Fold a builtin function call to hypot, hypotf, or hypotl.  Return
8023    NULL_TREE if no simplification can be made.  */
8024
8025 static tree
8026 fold_builtin_hypot (location_t loc, tree fndecl,
8027                     tree arg0, tree arg1, tree type)
8028 {
8029   tree res, narg0, narg1;
8030
8031   if (!validate_arg (arg0, REAL_TYPE)
8032       || !validate_arg (arg1, REAL_TYPE))
8033     return NULL_TREE;
8034
8035   /* Calculate the result when the argument is a constant.  */
8036   if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8037     return res;
8038
8039   /* If either argument to hypot has a negate or abs, strip that off.
8040      E.g. hypot(-x,fabs(y)) -> hypot(x,y).  */
8041   narg0 = fold_strip_sign_ops (arg0);
8042   narg1 = fold_strip_sign_ops (arg1);
8043   if (narg0 || narg1)
8044     {
8045       return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8046                               narg1 ? narg1 : arg1);
8047     }
8048
8049   /* If either argument is zero, hypot is fabs of the other.  */
8050   if (real_zerop (arg0))
8051     return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8052   else if (real_zerop (arg1))
8053     return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8054
8055   /* hypot(x,x) -> fabs(x)*sqrt(2).  */
8056   if (flag_unsafe_math_optimizations
8057       && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8058     {
8059       const REAL_VALUE_TYPE sqrt2_trunc
8060         = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8061       return fold_build2_loc (loc, MULT_EXPR, type,
8062                           fold_build1_loc (loc, ABS_EXPR, type, arg0),
8063                           build_real (type, sqrt2_trunc));
8064     }
8065
8066   return NULL_TREE;
8067 }
8068
8069
8070 /* Fold a builtin function call to pow, powf, or powl.  Return
8071    NULL_TREE if no simplification can be made.  */
8072 static tree
8073 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8074 {
8075   tree res;
8076
8077   if (!validate_arg (arg0, REAL_TYPE)
8078        || !validate_arg (arg1, REAL_TYPE))
8079     return NULL_TREE;
8080
8081   /* Calculate the result when the argument is a constant.  */
8082   if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8083     return res;
8084
8085   /* Optimize pow(1.0,y) = 1.0.  */
8086   if (real_onep (arg0))
8087     return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8088
8089   if (TREE_CODE (arg1) == REAL_CST
8090       && !TREE_OVERFLOW (arg1))
8091     {
8092       REAL_VALUE_TYPE cint;
8093       REAL_VALUE_TYPE c;
8094       HOST_WIDE_INT n;
8095
8096       c = TREE_REAL_CST (arg1);
8097
8098       /* Optimize pow(x,0.0) = 1.0.  */
8099       if (REAL_VALUES_EQUAL (c, dconst0))
8100         return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8101                                  arg0);
8102
8103       /* Optimize pow(x,1.0) = x.  */
8104       if (REAL_VALUES_EQUAL (c, dconst1))
8105         return arg0;
8106
8107       /* Optimize pow(x,-1.0) = 1.0/x.  */
8108       if (REAL_VALUES_EQUAL (c, dconstm1))
8109         return fold_build2_loc (loc, RDIV_EXPR, type,
8110                             build_real (type, dconst1), arg0);
8111
8112       /* Optimize pow(x,0.5) = sqrt(x).  */
8113       if (flag_unsafe_math_optimizations
8114           && REAL_VALUES_EQUAL (c, dconsthalf))
8115         {
8116           tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8117
8118           if (sqrtfn != NULL_TREE)
8119             return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8120         }
8121
8122       /* Optimize pow(x,1.0/3.0) = cbrt(x).  */
8123       if (flag_unsafe_math_optimizations)
8124         {
8125           const REAL_VALUE_TYPE dconstroot
8126             = real_value_truncate (TYPE_MODE (type), dconst_third ());
8127
8128           if (REAL_VALUES_EQUAL (c, dconstroot))
8129             {
8130               tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8131               if (cbrtfn != NULL_TREE)
8132                 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8133             }
8134         }
8135
8136       /* Check for an integer exponent.  */
8137       n = real_to_integer (&c);
8138       real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8139       if (real_identical (&c, &cint))
8140         {
8141           /* Attempt to evaluate pow at compile-time, unless this should
8142              raise an exception.  */
8143           if (TREE_CODE (arg0) == REAL_CST
8144               && !TREE_OVERFLOW (arg0)
8145               && (n > 0
8146                   || (!flag_trapping_math && !flag_errno_math)
8147                   || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8148             {
8149               REAL_VALUE_TYPE x;
8150               bool inexact;
8151
8152               x = TREE_REAL_CST (arg0);
8153               inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8154               if (flag_unsafe_math_optimizations || !inexact)
8155                 return build_real (type, x);
8156             }
8157
8158           /* Strip sign ops from even integer powers.  */
8159           if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8160             {
8161               tree narg0 = fold_strip_sign_ops (arg0);
8162               if (narg0)
8163                 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8164             }
8165         }
8166     }
8167
8168   if (flag_unsafe_math_optimizations)
8169     {
8170       const enum built_in_function fcode = builtin_mathfn_code (arg0);
8171
8172       /* Optimize pow(expN(x),y) = expN(x*y).  */
8173       if (BUILTIN_EXPONENT_P (fcode))
8174         {
8175           tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8176           tree arg = CALL_EXPR_ARG (arg0, 0);
8177           arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8178           return build_call_expr_loc (loc, expfn, 1, arg);
8179         }
8180
8181       /* Optimize pow(sqrt(x),y) = pow(x,y*0.5).  */
8182       if (BUILTIN_SQRT_P (fcode))
8183         {
8184           tree narg0 = CALL_EXPR_ARG (arg0, 0);
8185           tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8186                                     build_real (type, dconsthalf));
8187           return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8188         }
8189
8190       /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative.  */
8191       if (BUILTIN_CBRT_P (fcode))
8192         {
8193           tree arg = CALL_EXPR_ARG (arg0, 0);
8194           if (tree_expr_nonnegative_p (arg))
8195             {
8196               const REAL_VALUE_TYPE dconstroot
8197                 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8198               tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8199                                         build_real (type, dconstroot));
8200               return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8201             }
8202         }
8203
8204       /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative.  */
8205       if (fcode == BUILT_IN_POW
8206           || fcode == BUILT_IN_POWF
8207           || fcode == BUILT_IN_POWL)
8208         {
8209           tree arg00 = CALL_EXPR_ARG (arg0, 0);
8210           if (tree_expr_nonnegative_p (arg00))
8211             {
8212               tree arg01 = CALL_EXPR_ARG (arg0, 1);
8213               tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8214               return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8215             }
8216         }
8217     }
8218
8219   return NULL_TREE;
8220 }
8221
8222 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8223    Return NULL_TREE if no simplification can be made.  */
8224 static tree
8225 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8226                    tree arg0, tree arg1, tree type)
8227 {
8228   if (!validate_arg (arg0, REAL_TYPE)
8229       || !validate_arg (arg1, INTEGER_TYPE))
8230     return NULL_TREE;
8231
8232   /* Optimize pow(1.0,y) = 1.0.  */
8233   if (real_onep (arg0))
8234     return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8235
8236   if (host_integerp (arg1, 0))
8237     {
8238       HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8239
8240       /* Evaluate powi at compile-time.  */
8241       if (TREE_CODE (arg0) == REAL_CST
8242           && !TREE_OVERFLOW (arg0))
8243         {
8244           REAL_VALUE_TYPE x;
8245           x = TREE_REAL_CST (arg0);
8246           real_powi (&x, TYPE_MODE (type), &x, c);
8247           return build_real (type, x);
8248         }
8249
8250       /* Optimize pow(x,0) = 1.0.  */
8251       if (c == 0)
8252         return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8253                                  arg0);
8254
8255       /* Optimize pow(x,1) = x.  */
8256       if (c == 1)
8257         return arg0;
8258
8259       /* Optimize pow(x,-1) = 1.0/x.  */
8260       if (c == -1)
8261         return fold_build2_loc (loc, RDIV_EXPR, type,
8262                            build_real (type, dconst1), arg0);
8263     }
8264
8265   return NULL_TREE;
8266 }
8267
8268 /* A subroutine of fold_builtin to fold the various exponent
8269    functions.  Return NULL_TREE if no simplification can be made.
8270    FUNC is the corresponding MPFR exponent function.  */
8271
8272 static tree
8273 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8274                        int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8275 {
8276   if (validate_arg (arg, REAL_TYPE))
8277     {
8278       tree type = TREE_TYPE (TREE_TYPE (fndecl));
8279       tree res;
8280
8281       /* Calculate the result when the argument is a constant.  */
8282       if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8283         return res;
8284
8285       /* Optimize expN(logN(x)) = x.  */
8286       if (flag_unsafe_math_optimizations)
8287         {
8288           const enum built_in_function fcode = builtin_mathfn_code (arg);
8289
8290           if ((func == mpfr_exp
8291                && (fcode == BUILT_IN_LOG
8292                    || fcode == BUILT_IN_LOGF
8293                    || fcode == BUILT_IN_LOGL))
8294               || (func == mpfr_exp2
8295                   && (fcode == BUILT_IN_LOG2
8296                       || fcode == BUILT_IN_LOG2F
8297                       || fcode == BUILT_IN_LOG2L))
8298               || (func == mpfr_exp10
8299                   && (fcode == BUILT_IN_LOG10
8300                       || fcode == BUILT_IN_LOG10F
8301                       || fcode == BUILT_IN_LOG10L)))
8302             return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8303         }
8304     }
8305
8306   return NULL_TREE;
8307 }
8308
8309 /* Return true if VAR is a VAR_DECL or a component thereof.  */
8310
8311 static bool
8312 var_decl_component_p (tree var)
8313 {
8314   tree inner = var;
8315   while (handled_component_p (inner))
8316     inner = TREE_OPERAND (inner, 0);
8317   return SSA_VAR_P (inner);
8318 }
8319
8320 /* Fold function call to builtin memset.  Return
8321    NULL_TREE if no simplification can be made.  */
8322
8323 static tree
8324 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8325                      tree type, bool ignore)
8326 {
8327   tree var, ret, etype;
8328   unsigned HOST_WIDE_INT length, cval;
8329
8330   if (! validate_arg (dest, POINTER_TYPE)
8331       || ! validate_arg (c, INTEGER_TYPE)
8332       || ! validate_arg (len, INTEGER_TYPE))
8333     return NULL_TREE;
8334
8335   if (! host_integerp (len, 1))
8336     return NULL_TREE;
8337
8338   /* If the LEN parameter is zero, return DEST.  */
8339   if (integer_zerop (len))
8340     return omit_one_operand_loc (loc, type, dest, c);
8341
8342   if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8343     return NULL_TREE;
8344
8345   var = dest;
8346   STRIP_NOPS (var);
8347   if (TREE_CODE (var) != ADDR_EXPR)
8348     return NULL_TREE;
8349
8350   var = TREE_OPERAND (var, 0);
8351   if (TREE_THIS_VOLATILE (var))
8352     return NULL_TREE;
8353
8354   etype = TREE_TYPE (var);
8355   if (TREE_CODE (etype) == ARRAY_TYPE)
8356     etype = TREE_TYPE (etype);
8357
8358   if (!INTEGRAL_TYPE_P (etype)
8359       && !POINTER_TYPE_P (etype))
8360     return NULL_TREE;
8361
8362   if (! var_decl_component_p (var))
8363     return NULL_TREE;
8364
8365   length = tree_low_cst (len, 1);
8366   if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8367       || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8368          < length)
8369     return NULL_TREE;
8370
8371   if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8372     return NULL_TREE;
8373
8374   if (integer_zerop (c))
8375     cval = 0;
8376   else
8377     {
8378       if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8379         return NULL_TREE;
8380
8381       cval = tree_low_cst (c, 1);
8382       cval &= 0xff;
8383       cval |= cval << 8;
8384       cval |= cval << 16;
8385       cval |= (cval << 31) << 1;
8386     }
8387
8388   ret = build_int_cst_type (etype, cval);
8389   var = build_fold_indirect_ref_loc (loc,
8390                                  fold_convert_loc (loc,
8391                                                    build_pointer_type (etype),
8392                                                    dest));
8393   ret = build2 (MODIFY_EXPR, etype, var, ret);
8394   if (ignore)
8395     return ret;
8396
8397   return omit_one_operand_loc (loc, type, dest, ret);
8398 }
8399
8400 /* Fold function call to builtin memset.  Return
8401    NULL_TREE if no simplification can be made.  */
8402
8403 static tree
8404 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8405 {
8406   if (! validate_arg (dest, POINTER_TYPE)
8407       || ! validate_arg (size, INTEGER_TYPE))
8408     return NULL_TREE;
8409
8410   if (!ignore)
8411     return NULL_TREE;
8412
8413   /* New argument list transforming bzero(ptr x, int y) to
8414      memset(ptr x, int 0, size_t y).   This is done this way
8415      so that if it isn't expanded inline, we fallback to
8416      calling bzero instead of memset.  */
8417
8418   return fold_builtin_memset (loc, dest, integer_zero_node,
8419                               fold_convert_loc (loc, sizetype, size),
8420                               void_type_node, ignore);
8421 }
8422
8423 /* Fold function call to builtin mem{{,p}cpy,move}.  Return
8424    NULL_TREE if no simplification can be made.
8425    If ENDP is 0, return DEST (like memcpy).
8426    If ENDP is 1, return DEST+LEN (like mempcpy).
8427    If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8428    If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8429    (memmove).   */
8430
8431 static tree
8432 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8433                         tree len, tree type, bool ignore, int endp)
8434 {
8435   tree destvar, srcvar, expr;
8436
8437   if (! validate_arg (dest, POINTER_TYPE)
8438       || ! validate_arg (src, POINTER_TYPE)
8439       || ! validate_arg (len, INTEGER_TYPE))
8440     return NULL_TREE;
8441
8442   /* If the LEN parameter is zero, return DEST.  */
8443   if (integer_zerop (len))
8444     return omit_one_operand_loc (loc, type, dest, src);
8445
8446   /* If SRC and DEST are the same (and not volatile), return
8447      DEST{,+LEN,+LEN-1}.  */
8448   if (operand_equal_p (src, dest, 0))
8449     expr = len;
8450   else
8451     {
8452       tree srctype, desttype;
8453       unsigned int src_align, dest_align;
8454       tree off0;
8455
8456       if (endp == 3)
8457         {
8458           src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8459           dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8460
8461           /* Both DEST and SRC must be pointer types.
8462              ??? This is what old code did.  Is the testing for pointer types
8463              really mandatory?
8464
8465              If either SRC is readonly or length is 1, we can use memcpy.  */
8466           if (!dest_align || !src_align)
8467             return NULL_TREE;
8468           if (readonly_data_expr (src)
8469               || (host_integerp (len, 1)
8470                   && (MIN (src_align, dest_align) / BITS_PER_UNIT
8471                       >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8472             {
8473               tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8474               if (!fn)
8475                 return NULL_TREE;
8476               return build_call_expr_loc (loc, fn, 3, dest, src, len);
8477             }
8478
8479           /* If *src and *dest can't overlap, optimize into memcpy as well.  */
8480           if (TREE_CODE (src) == ADDR_EXPR
8481               && TREE_CODE (dest) == ADDR_EXPR)
8482             {
8483               tree src_base, dest_base, fn;
8484               HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8485               HOST_WIDE_INT size = -1;
8486               HOST_WIDE_INT maxsize = -1;
8487
8488               srcvar = TREE_OPERAND (src, 0);
8489               src_base = get_ref_base_and_extent (srcvar, &src_offset,
8490                                                   &size, &maxsize);
8491               destvar = TREE_OPERAND (dest, 0);
8492               dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8493                                                    &size, &maxsize);
8494               if (host_integerp (len, 1))
8495                 maxsize = tree_low_cst (len, 1);
8496               else
8497                 maxsize = -1;
8498               src_offset /= BITS_PER_UNIT;
8499               dest_offset /= BITS_PER_UNIT;
8500               if (SSA_VAR_P (src_base)
8501                   && SSA_VAR_P (dest_base))
8502                 {
8503                   if (operand_equal_p (src_base, dest_base, 0)
8504                       && ranges_overlap_p (src_offset, maxsize,
8505                                            dest_offset, maxsize))
8506                     return NULL_TREE;
8507                 }
8508               else if (TREE_CODE (src_base) == MEM_REF
8509                        && TREE_CODE (dest_base) == MEM_REF)
8510                 {
8511                   double_int off;
8512                   if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8513                                          TREE_OPERAND (dest_base, 0), 0))
8514                     return NULL_TREE;
8515                   off = double_int_add (mem_ref_offset (src_base),
8516                                         shwi_to_double_int (src_offset));
8517                   if (!double_int_fits_in_shwi_p (off))
8518                     return NULL_TREE;
8519                   src_offset = off.low;
8520                   off = double_int_add (mem_ref_offset (dest_base),
8521                                         shwi_to_double_int (dest_offset));
8522                   if (!double_int_fits_in_shwi_p (off))
8523                     return NULL_TREE;
8524                   dest_offset = off.low;
8525                   if (ranges_overlap_p (src_offset, maxsize,
8526                                         dest_offset, maxsize))
8527                     return NULL_TREE;
8528                 }
8529               else
8530                 return NULL_TREE;
8531
8532               fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8533               if (!fn)
8534                 return NULL_TREE;
8535               return build_call_expr_loc (loc, fn, 3, dest, src, len);
8536             }
8537
8538           /* If the destination and source do not alias optimize into
8539              memcpy as well.  */
8540           if ((is_gimple_min_invariant (dest)
8541                || TREE_CODE (dest) == SSA_NAME)
8542               && (is_gimple_min_invariant (src)
8543                   || TREE_CODE (src) == SSA_NAME))
8544             {
8545               ao_ref destr, srcr;
8546               ao_ref_init_from_ptr_and_size (&destr, dest, len);
8547               ao_ref_init_from_ptr_and_size (&srcr, src, len);
8548               if (!refs_may_alias_p_1 (&destr, &srcr, false))
8549                 {
8550                   tree fn;
8551                   fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8552                   if (!fn)
8553                     return NULL_TREE;
8554                   return build_call_expr_loc (loc, fn, 3, dest, src, len);
8555                 }
8556             }
8557
8558           return NULL_TREE;
8559         }
8560
8561       if (!host_integerp (len, 0))
8562         return NULL_TREE;
8563       /* FIXME:
8564          This logic lose for arguments like (type *)malloc (sizeof (type)),
8565          since we strip the casts of up to VOID return value from malloc.
8566          Perhaps we ought to inherit type from non-VOID argument here?  */
8567       STRIP_NOPS (src);
8568       STRIP_NOPS (dest);
8569       /* As we fold (void *)(p + CST) to (void *)p + CST undo this here.  */
8570       if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8571         {
8572           tree tem = TREE_OPERAND (src, 0);
8573           STRIP_NOPS (tem);
8574           if (tem != TREE_OPERAND (src, 0))
8575             src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8576         }
8577       if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8578         {
8579           tree tem = TREE_OPERAND (dest, 0);
8580           STRIP_NOPS (tem);
8581           if (tem != TREE_OPERAND (dest, 0))
8582             dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8583         }
8584       srctype = TREE_TYPE (TREE_TYPE (src));
8585       if (srctype
8586           && TREE_CODE (srctype) == ARRAY_TYPE
8587           && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8588         {
8589           srctype = TREE_TYPE (srctype);
8590           STRIP_NOPS (src);
8591           src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8592         }
8593       desttype = TREE_TYPE (TREE_TYPE (dest));
8594       if (desttype
8595           && TREE_CODE (desttype) == ARRAY_TYPE
8596           && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8597         {
8598           desttype = TREE_TYPE (desttype);
8599           STRIP_NOPS (dest);
8600           dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8601         }
8602       if (!srctype || !desttype
8603           || TREE_ADDRESSABLE (srctype)
8604           || TREE_ADDRESSABLE (desttype)
8605           || !TYPE_SIZE_UNIT (srctype)
8606           || !TYPE_SIZE_UNIT (desttype)
8607           || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8608           || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST)
8609         return NULL_TREE;
8610
8611       src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8612       dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8613       if (dest_align < TYPE_ALIGN (desttype)
8614           || src_align < TYPE_ALIGN (srctype))
8615         return NULL_TREE;
8616
8617       if (!ignore)
8618         dest = builtin_save_expr (dest);
8619
8620       /* Build accesses at offset zero with a ref-all character type.  */
8621       off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8622                                                          ptr_mode, true), 0);
8623
8624       destvar = dest;
8625       STRIP_NOPS (destvar);
8626       if (TREE_CODE (destvar) == ADDR_EXPR
8627           && var_decl_component_p (TREE_OPERAND (destvar, 0))
8628           && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8629         destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8630       else
8631         destvar = NULL_TREE;
8632
8633       srcvar = src;
8634       STRIP_NOPS (srcvar);
8635       if (TREE_CODE (srcvar) == ADDR_EXPR
8636           && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8637           && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8638         {
8639           if (!destvar
8640               || src_align >= TYPE_ALIGN (desttype))
8641             srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8642                                   srcvar, off0);
8643           else if (!STRICT_ALIGNMENT)
8644             {
8645               srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8646                                             src_align);
8647               srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8648             }
8649           else
8650             srcvar = NULL_TREE;
8651         }
8652       else
8653         srcvar = NULL_TREE;
8654
8655       if (srcvar == NULL_TREE && destvar == NULL_TREE)
8656         return NULL_TREE;
8657
8658       if (srcvar == NULL_TREE)
8659         {
8660           STRIP_NOPS (src);
8661           if (src_align >= TYPE_ALIGN (desttype))
8662             srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8663           else
8664             {
8665               if (STRICT_ALIGNMENT)
8666                 return NULL_TREE;
8667               srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8668                                             src_align);
8669               srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8670             }
8671         }
8672       else if (destvar == NULL_TREE)
8673         {
8674           STRIP_NOPS (dest);
8675           if (dest_align >= TYPE_ALIGN (srctype))
8676             destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8677           else
8678             {
8679               if (STRICT_ALIGNMENT)
8680                 return NULL_TREE;
8681               desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8682                                              dest_align);
8683               destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8684             }
8685         }
8686
8687       expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8688     }
8689
8690   if (ignore)
8691     return expr;
8692
8693   if (endp == 0 || endp == 3)
8694     return omit_one_operand_loc (loc, type, dest, expr);
8695
8696   if (expr == len)
8697     expr = NULL_TREE;
8698
8699   if (endp == 2)
8700     len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8701                        ssize_int (1));
8702
8703   len = fold_convert_loc (loc, sizetype, len);
8704   dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8705   dest = fold_convert_loc (loc, type, dest);
8706   if (expr)
8707     dest = omit_one_operand_loc (loc, type, dest, expr);
8708   return dest;
8709 }
8710
8711 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8712    If LEN is not NULL, it represents the length of the string to be
8713    copied.  Return NULL_TREE if no simplification can be made.  */
8714
8715 tree
8716 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8717 {
8718   tree fn;
8719
8720   if (!validate_arg (dest, POINTER_TYPE)
8721       || !validate_arg (src, POINTER_TYPE))
8722     return NULL_TREE;
8723
8724   /* If SRC and DEST are the same (and not volatile), return DEST.  */
8725   if (operand_equal_p (src, dest, 0))
8726     return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8727
8728   if (optimize_function_for_size_p (cfun))
8729     return NULL_TREE;
8730
8731   fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8732   if (!fn)
8733     return NULL_TREE;
8734
8735   if (!len)
8736     {
8737       len = c_strlen (src, 1);
8738       if (! len || TREE_SIDE_EFFECTS (len))
8739         return NULL_TREE;
8740     }
8741
8742   len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8743   return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8744                            build_call_expr_loc (loc, fn, 3, dest, src, len));
8745 }
8746
8747 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8748    Return NULL_TREE if no simplification can be made.  */
8749
8750 static tree
8751 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8752 {
8753   tree fn, len, lenp1, call, type;
8754
8755   if (!validate_arg (dest, POINTER_TYPE)
8756       || !validate_arg (src, POINTER_TYPE))
8757     return NULL_TREE;
8758
8759   len = c_strlen (src, 1);
8760   if (!len
8761       || TREE_CODE (len) != INTEGER_CST)
8762     return NULL_TREE;
8763
8764   if (optimize_function_for_size_p (cfun)
8765       /* If length is zero it's small enough.  */
8766       && !integer_zerop (len))
8767     return NULL_TREE;
8768
8769   fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8770   if (!fn)
8771     return NULL_TREE;
8772
8773   lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
8774   /* We use dest twice in building our expression.  Save it from
8775      multiple expansions.  */
8776   dest = builtin_save_expr (dest);
8777   call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8778
8779   type = TREE_TYPE (TREE_TYPE (fndecl));
8780   len = fold_convert_loc (loc, sizetype, len);
8781   dest = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8782   dest = fold_convert_loc (loc, type, dest);
8783   dest = omit_one_operand_loc (loc, type, dest, call);
8784   return dest;
8785 }
8786
8787 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8788    If SLEN is not NULL, it represents the length of the source string.
8789    Return NULL_TREE if no simplification can be made.  */
8790
8791 tree
8792 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
8793                       tree src, tree len, tree slen)
8794 {
8795   tree fn;
8796
8797   if (!validate_arg (dest, POINTER_TYPE)
8798       || !validate_arg (src, POINTER_TYPE)
8799       || !validate_arg (len, INTEGER_TYPE))
8800     return NULL_TREE;
8801
8802   /* If the LEN parameter is zero, return DEST.  */
8803   if (integer_zerop (len))
8804     return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8805
8806   /* We can't compare slen with len as constants below if len is not a
8807      constant.  */
8808   if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8809     return NULL_TREE;
8810
8811   if (!slen)
8812     slen = c_strlen (src, 1);
8813
8814   /* Now, we must be passed a constant src ptr parameter.  */
8815   if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8816     return NULL_TREE;
8817
8818   slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
8819
8820   /* We do not support simplification of this case, though we do
8821      support it when expanding trees into RTL.  */
8822   /* FIXME: generate a call to __builtin_memset.  */
8823   if (tree_int_cst_lt (slen, len))
8824     return NULL_TREE;
8825
8826   /* OK transform into builtin memcpy.  */
8827   fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8828   if (!fn)
8829     return NULL_TREE;
8830   return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8831                            build_call_expr_loc (loc, fn, 3, dest, src, len));
8832 }
8833
8834 /* Fold function call to builtin memchr.  ARG1, ARG2 and LEN are the
8835    arguments to the call, and TYPE is its return type.
8836    Return NULL_TREE if no simplification can be made.  */
8837
8838 static tree
8839 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8840 {
8841   if (!validate_arg (arg1, POINTER_TYPE)
8842       || !validate_arg (arg2, INTEGER_TYPE)
8843       || !validate_arg (len, INTEGER_TYPE))
8844     return NULL_TREE;
8845   else
8846     {
8847       const char *p1;
8848
8849       if (TREE_CODE (arg2) != INTEGER_CST
8850           || !host_integerp (len, 1))
8851         return NULL_TREE;
8852
8853       p1 = c_getstr (arg1);
8854       if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8855         {
8856           char c;
8857           const char *r;
8858           tree tem;
8859
8860           if (target_char_cast (arg2, &c))
8861             return NULL_TREE;
8862
8863           r = (char *) memchr (p1, c, tree_low_cst (len, 1));
8864
8865           if (r == NULL)
8866             return build_int_cst (TREE_TYPE (arg1), 0);
8867
8868           tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
8869                              size_int (r - p1));
8870           return fold_convert_loc (loc, type, tem);
8871         }
8872       return NULL_TREE;
8873     }
8874 }
8875
8876 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8877    Return NULL_TREE if no simplification can be made.  */
8878
8879 static tree
8880 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8881 {
8882   const char *p1, *p2;
8883
8884   if (!validate_arg (arg1, POINTER_TYPE)
8885       || !validate_arg (arg2, POINTER_TYPE)
8886       || !validate_arg (len, INTEGER_TYPE))
8887     return NULL_TREE;
8888
8889   /* If the LEN parameter is zero, return zero.  */
8890   if (integer_zerop (len))
8891     return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8892                               arg1, arg2);
8893
8894   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
8895   if (operand_equal_p (arg1, arg2, 0))
8896     return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8897
8898   p1 = c_getstr (arg1);
8899   p2 = c_getstr (arg2);
8900
8901   /* If all arguments are constant, and the value of len is not greater
8902      than the lengths of arg1 and arg2, evaluate at compile-time.  */
8903   if (host_integerp (len, 1) && p1 && p2
8904       && compare_tree_int (len, strlen (p1) + 1) <= 0
8905       && compare_tree_int (len, strlen (p2) + 1) <= 0)
8906     {
8907       const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8908
8909       if (r > 0)
8910         return integer_one_node;
8911       else if (r < 0)
8912         return integer_minus_one_node;
8913       else
8914         return integer_zero_node;
8915     }
8916
8917   /* If len parameter is one, return an expression corresponding to
8918      (*(const unsigned char*)arg1 - (const unsigned char*)arg2).  */
8919   if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8920     {
8921       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8922       tree cst_uchar_ptr_node
8923         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8924
8925       tree ind1
8926         = fold_convert_loc (loc, integer_type_node,
8927                             build1 (INDIRECT_REF, cst_uchar_node,
8928                                     fold_convert_loc (loc,
8929                                                       cst_uchar_ptr_node,
8930                                                       arg1)));
8931       tree ind2
8932         = fold_convert_loc (loc, integer_type_node,
8933                             build1 (INDIRECT_REF, cst_uchar_node,
8934                                     fold_convert_loc (loc,
8935                                                       cst_uchar_ptr_node,
8936                                                       arg2)));
8937       return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8938     }
8939
8940   return NULL_TREE;
8941 }
8942
8943 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8944    Return NULL_TREE if no simplification can be made.  */
8945
8946 static tree
8947 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8948 {
8949   const char *p1, *p2;
8950
8951   if (!validate_arg (arg1, POINTER_TYPE)
8952       || !validate_arg (arg2, POINTER_TYPE))
8953     return NULL_TREE;
8954
8955   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
8956   if (operand_equal_p (arg1, arg2, 0))
8957     return integer_zero_node;
8958
8959   p1 = c_getstr (arg1);
8960   p2 = c_getstr (arg2);
8961
8962   if (p1 && p2)
8963     {
8964       const int i = strcmp (p1, p2);
8965       if (i < 0)
8966         return integer_minus_one_node;
8967       else if (i > 0)
8968         return integer_one_node;
8969       else
8970         return integer_zero_node;
8971     }
8972
8973   /* If the second arg is "", return *(const unsigned char*)arg1.  */
8974   if (p2 && *p2 == '\0')
8975     {
8976       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8977       tree cst_uchar_ptr_node
8978         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8979
8980       return fold_convert_loc (loc, integer_type_node,
8981                                build1 (INDIRECT_REF, cst_uchar_node,
8982                                        fold_convert_loc (loc,
8983                                                          cst_uchar_ptr_node,
8984                                                          arg1)));
8985     }
8986
8987   /* If the first arg is "", return -*(const unsigned char*)arg2.  */
8988   if (p1 && *p1 == '\0')
8989     {
8990       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8991       tree cst_uchar_ptr_node
8992         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8993
8994       tree temp
8995         = fold_convert_loc (loc, integer_type_node,
8996                             build1 (INDIRECT_REF, cst_uchar_node,
8997                                     fold_convert_loc (loc,
8998                                                       cst_uchar_ptr_node,
8999                                                       arg2)));
9000       return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9001     }
9002
9003   return NULL_TREE;
9004 }
9005
9006 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9007    Return NULL_TREE if no simplification can be made.  */
9008
9009 static tree
9010 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9011 {
9012   const char *p1, *p2;
9013
9014   if (!validate_arg (arg1, POINTER_TYPE)
9015       || !validate_arg (arg2, POINTER_TYPE)
9016       || !validate_arg (len, INTEGER_TYPE))
9017     return NULL_TREE;
9018
9019   /* If the LEN parameter is zero, return zero.  */
9020   if (integer_zerop (len))
9021     return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9022                               arg1, arg2);
9023
9024   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
9025   if (operand_equal_p (arg1, arg2, 0))
9026     return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9027
9028   p1 = c_getstr (arg1);
9029   p2 = c_getstr (arg2);
9030
9031   if (host_integerp (len, 1) && p1 && p2)
9032     {
9033       const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9034       if (i > 0)
9035         return integer_one_node;
9036       else if (i < 0)
9037         return integer_minus_one_node;
9038       else
9039         return integer_zero_node;
9040     }
9041
9042   /* If the second arg is "", and the length is greater than zero,
9043      return *(const unsigned char*)arg1.  */
9044   if (p2 && *p2 == '\0'
9045       && TREE_CODE (len) == INTEGER_CST
9046       && tree_int_cst_sgn (len) == 1)
9047     {
9048       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9049       tree cst_uchar_ptr_node
9050         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9051
9052       return fold_convert_loc (loc, integer_type_node,
9053                                build1 (INDIRECT_REF, cst_uchar_node,
9054                                        fold_convert_loc (loc,
9055                                                          cst_uchar_ptr_node,
9056                                                          arg1)));
9057     }
9058
9059   /* If the first arg is "", and the length is greater than zero,
9060      return -*(const unsigned char*)arg2.  */
9061   if (p1 && *p1 == '\0'
9062       && TREE_CODE (len) == INTEGER_CST
9063       && tree_int_cst_sgn (len) == 1)
9064     {
9065       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9066       tree cst_uchar_ptr_node
9067         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9068
9069       tree temp = fold_convert_loc (loc, integer_type_node,
9070                                     build1 (INDIRECT_REF, cst_uchar_node,
9071                                             fold_convert_loc (loc,
9072                                                               cst_uchar_ptr_node,
9073                                                               arg2)));
9074       return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9075     }
9076
9077   /* If len parameter is one, return an expression corresponding to
9078      (*(const unsigned char*)arg1 - (const unsigned char*)arg2).  */
9079   if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9080     {
9081       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9082       tree cst_uchar_ptr_node
9083         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9084
9085       tree ind1 = fold_convert_loc (loc, integer_type_node,
9086                                     build1 (INDIRECT_REF, cst_uchar_node,
9087                                             fold_convert_loc (loc,
9088                                                               cst_uchar_ptr_node,
9089                                                               arg1)));
9090       tree ind2 = fold_convert_loc (loc, integer_type_node,
9091                                     build1 (INDIRECT_REF, cst_uchar_node,
9092                                             fold_convert_loc (loc,
9093                                                               cst_uchar_ptr_node,
9094                                                               arg2)));
9095       return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9096     }
9097
9098   return NULL_TREE;
9099 }
9100
9101 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9102    ARG.  Return NULL_TREE if no simplification can be made.  */
9103
9104 static tree
9105 fold_builtin_signbit (location_t loc, tree arg, tree type)
9106 {
9107   tree temp;
9108
9109   if (!validate_arg (arg, REAL_TYPE))
9110     return NULL_TREE;
9111
9112   /* If ARG is a compile-time constant, determine the result.  */
9113   if (TREE_CODE (arg) == REAL_CST
9114       && !TREE_OVERFLOW (arg))
9115     {
9116       REAL_VALUE_TYPE c;
9117
9118       c = TREE_REAL_CST (arg);
9119       temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
9120       return fold_convert_loc (loc, type, temp);
9121     }
9122
9123   /* If ARG is non-negative, the result is always zero.  */
9124   if (tree_expr_nonnegative_p (arg))
9125     return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9126
9127   /* If ARG's format doesn't have signed zeros, return "arg < 0.0".  */
9128   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9129     return fold_build2_loc (loc, LT_EXPR, type, arg,
9130                         build_real (TREE_TYPE (arg), dconst0));
9131
9132   return NULL_TREE;
9133 }
9134
9135 /* Fold function call to builtin copysign, copysignf or copysignl with
9136    arguments ARG1 and ARG2.  Return NULL_TREE if no simplification can
9137    be made.  */
9138
9139 static tree
9140 fold_builtin_copysign (location_t loc, tree fndecl,
9141                        tree arg1, tree arg2, tree type)
9142 {
9143   tree tem;
9144
9145   if (!validate_arg (arg1, REAL_TYPE)
9146       || !validate_arg (arg2, REAL_TYPE))
9147     return NULL_TREE;
9148
9149   /* copysign(X,X) is X.  */
9150   if (operand_equal_p (arg1, arg2, 0))
9151     return fold_convert_loc (loc, type, arg1);
9152
9153   /* If ARG1 and ARG2 are compile-time constants, determine the result.  */
9154   if (TREE_CODE (arg1) == REAL_CST
9155       && TREE_CODE (arg2) == REAL_CST
9156       && !TREE_OVERFLOW (arg1)
9157       && !TREE_OVERFLOW (arg2))
9158     {
9159       REAL_VALUE_TYPE c1, c2;
9160
9161       c1 = TREE_REAL_CST (arg1);
9162       c2 = TREE_REAL_CST (arg2);
9163       /* c1.sign := c2.sign.  */
9164       real_copysign (&c1, &c2);
9165       return build_real (type, c1);
9166     }
9167
9168   /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9169      Remember to evaluate Y for side-effects.  */
9170   if (tree_expr_nonnegative_p (arg2))
9171     return omit_one_operand_loc (loc, type,
9172                              fold_build1_loc (loc, ABS_EXPR, type, arg1),
9173                              arg2);
9174
9175   /* Strip sign changing operations for the first argument.  */
9176   tem = fold_strip_sign_ops (arg1);
9177   if (tem)
9178     return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9179
9180   return NULL_TREE;
9181 }
9182
9183 /* Fold a call to builtin isascii with argument ARG.  */
9184
9185 static tree
9186 fold_builtin_isascii (location_t loc, tree arg)
9187 {
9188   if (!validate_arg (arg, INTEGER_TYPE))
9189     return NULL_TREE;
9190   else
9191     {
9192       /* Transform isascii(c) -> ((c & ~0x7f) == 0).  */
9193       arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9194                          build_int_cst (NULL_TREE,
9195                                         ~ (unsigned HOST_WIDE_INT) 0x7f));
9196       return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9197                           arg, integer_zero_node);
9198     }
9199 }
9200
9201 /* Fold a call to builtin toascii with argument ARG.  */
9202
9203 static tree
9204 fold_builtin_toascii (location_t loc, tree arg)
9205 {
9206   if (!validate_arg (arg, INTEGER_TYPE))
9207     return NULL_TREE;
9208
9209   /* Transform toascii(c) -> (c & 0x7f).  */
9210   return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9211                       build_int_cst (NULL_TREE, 0x7f));
9212 }
9213
9214 /* Fold a call to builtin isdigit with argument ARG.  */
9215
9216 static tree
9217 fold_builtin_isdigit (location_t loc, tree arg)
9218 {
9219   if (!validate_arg (arg, INTEGER_TYPE))
9220     return NULL_TREE;
9221   else
9222     {
9223       /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9.  */
9224       /* According to the C standard, isdigit is unaffected by locale.
9225          However, it definitely is affected by the target character set.  */
9226       unsigned HOST_WIDE_INT target_digit0
9227         = lang_hooks.to_target_charset ('0');
9228
9229       if (target_digit0 == 0)
9230         return NULL_TREE;
9231
9232       arg = fold_convert_loc (loc, unsigned_type_node, arg);
9233       arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9234                          build_int_cst (unsigned_type_node, target_digit0));
9235       return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9236                           build_int_cst (unsigned_type_node, 9));
9237     }
9238 }
9239
9240 /* Fold a call to fabs, fabsf or fabsl with argument ARG.  */
9241
9242 static tree
9243 fold_builtin_fabs (location_t loc, tree arg, tree type)
9244 {
9245   if (!validate_arg (arg, REAL_TYPE))
9246     return NULL_TREE;
9247
9248   arg = fold_convert_loc (loc, type, arg);
9249   if (TREE_CODE (arg) == REAL_CST)
9250     return fold_abs_const (arg, type);
9251   return fold_build1_loc (loc, ABS_EXPR, type, arg);
9252 }
9253
9254 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG.  */
9255
9256 static tree
9257 fold_builtin_abs (location_t loc, tree arg, tree type)
9258 {
9259   if (!validate_arg (arg, INTEGER_TYPE))
9260     return NULL_TREE;
9261
9262   arg = fold_convert_loc (loc, type, arg);
9263   if (TREE_CODE (arg) == INTEGER_CST)
9264     return fold_abs_const (arg, type);
9265   return fold_build1_loc (loc, ABS_EXPR, type, arg);
9266 }
9267
9268 /* Fold a call to builtin fmin or fmax.  */
9269
9270 static tree
9271 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9272                         tree type, bool max)
9273 {
9274   if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9275     {
9276       /* Calculate the result when the argument is a constant.  */
9277       tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9278
9279       if (res)
9280         return res;
9281
9282       /* If either argument is NaN, return the other one.  Avoid the
9283          transformation if we get (and honor) a signalling NaN.  Using
9284          omit_one_operand() ensures we create a non-lvalue.  */
9285       if (TREE_CODE (arg0) == REAL_CST
9286           && real_isnan (&TREE_REAL_CST (arg0))
9287           && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9288               || ! TREE_REAL_CST (arg0).signalling))
9289         return omit_one_operand_loc (loc, type, arg1, arg0);
9290       if (TREE_CODE (arg1) == REAL_CST
9291           && real_isnan (&TREE_REAL_CST (arg1))
9292           && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9293               || ! TREE_REAL_CST (arg1).signalling))
9294         return omit_one_operand_loc (loc, type, arg0, arg1);
9295
9296       /* Transform fmin/fmax(x,x) -> x.  */
9297       if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9298         return omit_one_operand_loc (loc, type, arg0, arg1);
9299
9300       /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
9301          functions to return the numeric arg if the other one is NaN.
9302          These tree codes don't honor that, so only transform if
9303          -ffinite-math-only is set.  C99 doesn't require -0.0 to be
9304          handled, so we don't have to worry about it either.  */
9305       if (flag_finite_math_only)
9306         return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9307                             fold_convert_loc (loc, type, arg0),
9308                             fold_convert_loc (loc, type, arg1));
9309     }
9310   return NULL_TREE;
9311 }
9312
9313 /* Fold a call to builtin carg(a+bi) -> atan2(b,a).  */
9314
9315 static tree
9316 fold_builtin_carg (location_t loc, tree arg, tree type)
9317 {
9318   if (validate_arg (arg, COMPLEX_TYPE)
9319       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9320     {
9321       tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9322
9323       if (atan2_fn)
9324         {
9325           tree new_arg = builtin_save_expr (arg);
9326           tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9327           tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9328           return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9329         }
9330     }
9331
9332   return NULL_TREE;
9333 }
9334
9335 /* Fold a call to builtin logb/ilogb.  */
9336
9337 static tree
9338 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9339 {
9340   if (! validate_arg (arg, REAL_TYPE))
9341     return NULL_TREE;
9342
9343   STRIP_NOPS (arg);
9344
9345   if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9346     {
9347       const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9348
9349       switch (value->cl)
9350       {
9351       case rvc_nan:
9352       case rvc_inf:
9353         /* If arg is Inf or NaN and we're logb, return it.  */
9354         if (TREE_CODE (rettype) == REAL_TYPE)
9355           return fold_convert_loc (loc, rettype, arg);
9356         /* Fall through... */
9357       case rvc_zero:
9358         /* Zero may set errno and/or raise an exception for logb, also
9359            for ilogb we don't know FP_ILOGB0.  */
9360         return NULL_TREE;
9361       case rvc_normal:
9362         /* For normal numbers, proceed iff radix == 2.  In GCC,
9363            normalized significands are in the range [0.5, 1.0).  We
9364            want the exponent as if they were [1.0, 2.0) so get the
9365            exponent and subtract 1.  */
9366         if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9367           return fold_convert_loc (loc, rettype,
9368                                    build_int_cst (NULL_TREE,
9369                                                   REAL_EXP (value)-1));
9370         break;
9371       }
9372     }
9373
9374   return NULL_TREE;
9375 }
9376
9377 /* Fold a call to builtin significand, if radix == 2.  */
9378
9379 static tree
9380 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9381 {
9382   if (! validate_arg (arg, REAL_TYPE))
9383     return NULL_TREE;
9384
9385   STRIP_NOPS (arg);
9386
9387   if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9388     {
9389       const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9390
9391       switch (value->cl)
9392       {
9393       case rvc_zero:
9394       case rvc_nan:
9395       case rvc_inf:
9396         /* If arg is +-0, +-Inf or +-NaN, then return it.  */
9397         return fold_convert_loc (loc, rettype, arg);
9398       case rvc_normal:
9399         /* For normal numbers, proceed iff radix == 2.  */
9400         if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9401           {
9402             REAL_VALUE_TYPE result = *value;
9403             /* In GCC, normalized significands are in the range [0.5,
9404                1.0).  We want them to be [1.0, 2.0) so set the
9405                exponent to 1.  */
9406             SET_REAL_EXP (&result, 1);
9407             return build_real (rettype, result);
9408           }
9409         break;
9410       }
9411     }
9412
9413   return NULL_TREE;
9414 }
9415
9416 /* Fold a call to builtin frexp, we can assume the base is 2.  */
9417
9418 static tree
9419 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9420 {
9421   if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9422     return NULL_TREE;
9423
9424   STRIP_NOPS (arg0);
9425
9426   if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9427     return NULL_TREE;
9428
9429   arg1 = build_fold_indirect_ref_loc (loc, arg1);
9430
9431   /* Proceed if a valid pointer type was passed in.  */
9432   if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9433     {
9434       const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9435       tree frac, exp;
9436
9437       switch (value->cl)
9438       {
9439       case rvc_zero:
9440         /* For +-0, return (*exp = 0, +-0).  */
9441         exp = integer_zero_node;
9442         frac = arg0;
9443         break;
9444       case rvc_nan:
9445       case rvc_inf:
9446         /* For +-NaN or +-Inf, *exp is unspecified, return arg0.  */
9447         return omit_one_operand_loc (loc, rettype, arg0, arg1);
9448       case rvc_normal:
9449         {
9450           /* Since the frexp function always expects base 2, and in
9451              GCC normalized significands are already in the range
9452              [0.5, 1.0), we have exactly what frexp wants.  */
9453           REAL_VALUE_TYPE frac_rvt = *value;
9454           SET_REAL_EXP (&frac_rvt, 0);
9455           frac = build_real (rettype, frac_rvt);
9456           exp = build_int_cst (NULL_TREE, REAL_EXP (value));
9457         }
9458         break;
9459       default:
9460         gcc_unreachable ();
9461       }
9462
9463       /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9464       arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9465       TREE_SIDE_EFFECTS (arg1) = 1;
9466       return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9467     }
9468
9469   return NULL_TREE;
9470 }
9471
9472 /* Fold a call to builtin ldexp or scalbn/scalbln.  If LDEXP is true
9473    then we can assume the base is two.  If it's false, then we have to
9474    check the mode of the TYPE parameter in certain cases.  */
9475
9476 static tree
9477 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9478                             tree type, bool ldexp)
9479 {
9480   if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9481     {
9482       STRIP_NOPS (arg0);
9483       STRIP_NOPS (arg1);
9484
9485       /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0.  */
9486       if (real_zerop (arg0) || integer_zerop (arg1)
9487           || (TREE_CODE (arg0) == REAL_CST
9488               && !real_isfinite (&TREE_REAL_CST (arg0))))
9489         return omit_one_operand_loc (loc, type, arg0, arg1);
9490
9491       /* If both arguments are constant, then try to evaluate it.  */
9492       if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9493           && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9494           && host_integerp (arg1, 0))
9495         {
9496           /* Bound the maximum adjustment to twice the range of the
9497              mode's valid exponents.  Use abs to ensure the range is
9498              positive as a sanity check.  */
9499           const long max_exp_adj = 2 *
9500             labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9501                  - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9502
9503           /* Get the user-requested adjustment.  */
9504           const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9505
9506           /* The requested adjustment must be inside this range.  This
9507              is a preliminary cap to avoid things like overflow, we
9508              may still fail to compute the result for other reasons.  */
9509           if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9510             {
9511               REAL_VALUE_TYPE initial_result;
9512
9513               real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9514
9515               /* Ensure we didn't overflow.  */
9516               if (! real_isinf (&initial_result))
9517                 {
9518                   const REAL_VALUE_TYPE trunc_result
9519                     = real_value_truncate (TYPE_MODE (type), initial_result);
9520
9521                   /* Only proceed if the target mode can hold the
9522                      resulting value.  */
9523                   if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9524                     return build_real (type, trunc_result);
9525                 }
9526             }
9527         }
9528     }
9529
9530   return NULL_TREE;
9531 }
9532
9533 /* Fold a call to builtin modf.  */
9534
9535 static tree
9536 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9537 {
9538   if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9539     return NULL_TREE;
9540
9541   STRIP_NOPS (arg0);
9542
9543   if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9544     return NULL_TREE;
9545
9546   arg1 = build_fold_indirect_ref_loc (loc, arg1);
9547
9548   /* Proceed if a valid pointer type was passed in.  */
9549   if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9550     {
9551       const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9552       REAL_VALUE_TYPE trunc, frac;
9553
9554       switch (value->cl)
9555       {
9556       case rvc_nan:
9557       case rvc_zero:
9558         /* For +-NaN or +-0, return (*arg1 = arg0, arg0).  */
9559         trunc = frac = *value;
9560         break;
9561       case rvc_inf:
9562         /* For +-Inf, return (*arg1 = arg0, +-0).  */
9563         frac = dconst0;
9564         frac.sign = value->sign;
9565         trunc = *value;
9566         break;
9567       case rvc_normal:
9568         /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)).  */
9569         real_trunc (&trunc, VOIDmode, value);
9570         real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9571         /* If the original number was negative and already
9572            integral, then the fractional part is -0.0.  */
9573         if (value->sign && frac.cl == rvc_zero)
9574           frac.sign = value->sign;
9575         break;
9576       }
9577
9578       /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9579       arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9580                           build_real (rettype, trunc));
9581       TREE_SIDE_EFFECTS (arg1) = 1;
9582       return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9583                           build_real (rettype, frac));
9584     }
9585
9586   return NULL_TREE;
9587 }
9588
9589 /* Given a location LOC, an interclass builtin function decl FNDECL
9590    and its single argument ARG, return an folded expression computing
9591    the same, or NULL_TREE if we either couldn't or didn't want to fold
9592    (the latter happen if there's an RTL instruction available).  */
9593
9594 static tree
9595 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9596 {
9597   enum machine_mode mode;
9598
9599   if (!validate_arg (arg, REAL_TYPE))
9600     return NULL_TREE;
9601
9602   if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9603     return NULL_TREE;
9604
9605   mode = TYPE_MODE (TREE_TYPE (arg));
9606
9607   /* If there is no optab, try generic code.  */
9608   switch (DECL_FUNCTION_CODE (fndecl))
9609     {
9610       tree result;
9611
9612     CASE_FLT_FN (BUILT_IN_ISINF):
9613       {
9614         /* isinf(x) -> isgreater(fabs(x),DBL_MAX).  */
9615         tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
9616         tree const type = TREE_TYPE (arg);
9617         REAL_VALUE_TYPE r;
9618         char buf[128];
9619
9620         get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9621         real_from_string (&r, buf);
9622         result = build_call_expr (isgr_fn, 2,
9623                                   fold_build1_loc (loc, ABS_EXPR, type, arg),
9624                                   build_real (type, r));
9625         return result;
9626       }
9627     CASE_FLT_FN (BUILT_IN_FINITE):
9628     case BUILT_IN_ISFINITE:
9629       {
9630         /* isfinite(x) -> islessequal(fabs(x),DBL_MAX).  */
9631         tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9632         tree const type = TREE_TYPE (arg);
9633         REAL_VALUE_TYPE r;
9634         char buf[128];
9635
9636         get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9637         real_from_string (&r, buf);
9638         result = build_call_expr (isle_fn, 2,
9639                                   fold_build1_loc (loc, ABS_EXPR, type, arg),
9640                                   build_real (type, r));
9641         /*result = fold_build2_loc (loc, UNGT_EXPR,
9642                                   TREE_TYPE (TREE_TYPE (fndecl)),
9643                                   fold_build1_loc (loc, ABS_EXPR, type, arg),
9644                                   build_real (type, r));
9645         result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9646                                   TREE_TYPE (TREE_TYPE (fndecl)),
9647                                   result);*/
9648         return result;
9649       }
9650     case BUILT_IN_ISNORMAL:
9651       {
9652         /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9653            islessequal(fabs(x),DBL_MAX).  */
9654         tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9655         tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
9656         tree const type = TREE_TYPE (arg);
9657         REAL_VALUE_TYPE rmax, rmin;
9658         char buf[128];
9659
9660         get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9661         real_from_string (&rmax, buf);
9662         sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9663         real_from_string (&rmin, buf);
9664         arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9665         result = build_call_expr (isle_fn, 2, arg,
9666                                   build_real (type, rmax));
9667         result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9668                               build_call_expr (isge_fn, 2, arg,
9669                                                build_real (type, rmin)));
9670         return result;
9671       }
9672     default:
9673       break;
9674     }
9675
9676   return NULL_TREE;
9677 }
9678
9679 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9680    ARG is the argument for the call.  */
9681
9682 static tree
9683 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9684 {
9685   tree type = TREE_TYPE (TREE_TYPE (fndecl));
9686   REAL_VALUE_TYPE r;
9687
9688   if (!validate_arg (arg, REAL_TYPE))
9689     return NULL_TREE;
9690
9691   switch (builtin_index)
9692     {
9693     case BUILT_IN_ISINF:
9694       if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9695         return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9696
9697       if (TREE_CODE (arg) == REAL_CST)
9698         {
9699           r = TREE_REAL_CST (arg);
9700           if (real_isinf (&r))
9701             return real_compare (GT_EXPR, &r, &dconst0)
9702                    ? integer_one_node : integer_minus_one_node;
9703           else
9704             return integer_zero_node;
9705         }
9706
9707       return NULL_TREE;
9708
9709     case BUILT_IN_ISINF_SIGN:
9710       {
9711         /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9712         /* In a boolean context, GCC will fold the inner COND_EXPR to
9713            1.  So e.g. "if (isinf_sign(x))" would be folded to just
9714            "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9715         tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9716         tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
9717         tree tmp = NULL_TREE;
9718
9719         arg = builtin_save_expr (arg);
9720
9721         if (signbit_fn && isinf_fn)
9722           {
9723             tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9724             tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9725
9726             signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9727                                         signbit_call, integer_zero_node);
9728             isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9729                                       isinf_call, integer_zero_node);
9730
9731             tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9732                                integer_minus_one_node, integer_one_node);
9733             tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9734                                isinf_call, tmp,
9735                                integer_zero_node);
9736           }
9737
9738         return tmp;
9739       }
9740
9741     case BUILT_IN_ISFINITE:
9742       if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9743           && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9744         return omit_one_operand_loc (loc, type, integer_one_node, arg);
9745
9746       if (TREE_CODE (arg) == REAL_CST)
9747         {
9748           r = TREE_REAL_CST (arg);
9749           return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9750         }
9751
9752       return NULL_TREE;
9753
9754     case BUILT_IN_ISNAN:
9755       if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9756         return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9757
9758       if (TREE_CODE (arg) == REAL_CST)
9759         {
9760           r = TREE_REAL_CST (arg);
9761           return real_isnan (&r) ? integer_one_node : integer_zero_node;
9762         }
9763
9764       arg = builtin_save_expr (arg);
9765       return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9766
9767     default:
9768       gcc_unreachable ();
9769     }
9770 }
9771
9772 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9773    This builtin will generate code to return the appropriate floating
9774    point classification depending on the value of the floating point
9775    number passed in.  The possible return values must be supplied as
9776    int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9777    FP_NORMAL, FP_SUBNORMAL and FP_ZERO.  The ellipses is for exactly
9778    one floating point argument which is "type generic".  */
9779
9780 static tree
9781 fold_builtin_fpclassify (location_t loc, tree exp)
9782 {
9783   tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9784     arg, type, res, tmp;
9785   enum machine_mode mode;
9786   REAL_VALUE_TYPE r;
9787   char buf[128];
9788
9789   /* Verify the required arguments in the original call.  */
9790   if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9791                          INTEGER_TYPE, INTEGER_TYPE,
9792                          INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9793     return NULL_TREE;
9794
9795   fp_nan = CALL_EXPR_ARG (exp, 0);
9796   fp_infinite = CALL_EXPR_ARG (exp, 1);
9797   fp_normal = CALL_EXPR_ARG (exp, 2);
9798   fp_subnormal = CALL_EXPR_ARG (exp, 3);
9799   fp_zero = CALL_EXPR_ARG (exp, 4);
9800   arg = CALL_EXPR_ARG (exp, 5);
9801   type = TREE_TYPE (arg);
9802   mode = TYPE_MODE (type);
9803   arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9804
9805   /* fpclassify(x) ->
9806        isnan(x) ? FP_NAN :
9807          (fabs(x) == Inf ? FP_INFINITE :
9808            (fabs(x) >= DBL_MIN ? FP_NORMAL :
9809              (x == 0 ? FP_ZERO : FP_SUBNORMAL))).  */
9810
9811   tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9812                      build_real (type, dconst0));
9813   res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9814                      tmp, fp_zero, fp_subnormal);
9815
9816   sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9817   real_from_string (&r, buf);
9818   tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9819                      arg, build_real (type, r));
9820   res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9821
9822   if (HONOR_INFINITIES (mode))
9823     {
9824       real_inf (&r);
9825       tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9826                          build_real (type, r));
9827       res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9828                          fp_infinite, res);
9829     }
9830
9831   if (HONOR_NANS (mode))
9832     {
9833       tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9834       res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9835     }
9836
9837   return res;
9838 }
9839
9840 /* Fold a call to an unordered comparison function such as
9841    __builtin_isgreater().  FNDECL is the FUNCTION_DECL for the function
9842    being called and ARG0 and ARG1 are the arguments for the call.
9843    UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9844    the opposite of the desired result.  UNORDERED_CODE is used
9845    for modes that can hold NaNs and ORDERED_CODE is used for
9846    the rest.  */
9847
9848 static tree
9849 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9850                             enum tree_code unordered_code,
9851                             enum tree_code ordered_code)
9852 {
9853   tree type = TREE_TYPE (TREE_TYPE (fndecl));
9854   enum tree_code code;
9855   tree type0, type1;
9856   enum tree_code code0, code1;
9857   tree cmp_type = NULL_TREE;
9858
9859   type0 = TREE_TYPE (arg0);
9860   type1 = TREE_TYPE (arg1);
9861
9862   code0 = TREE_CODE (type0);
9863   code1 = TREE_CODE (type1);
9864
9865   if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9866     /* Choose the wider of two real types.  */
9867     cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9868       ? type0 : type1;
9869   else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9870     cmp_type = type0;
9871   else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9872     cmp_type = type1;
9873
9874   arg0 = fold_convert_loc (loc, cmp_type, arg0);
9875   arg1 = fold_convert_loc (loc, cmp_type, arg1);
9876
9877   if (unordered_code == UNORDERED_EXPR)
9878     {
9879       if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9880         return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9881       return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9882     }
9883
9884   code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9885                                                    : ordered_code;
9886   return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9887                       fold_build2_loc (loc, code, type, arg0, arg1));
9888 }
9889
9890 /* Fold a call to built-in function FNDECL with 0 arguments.
9891    IGNORE is true if the result of the function call is ignored.  This
9892    function returns NULL_TREE if no simplification was possible.  */
9893
9894 static tree
9895 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9896 {
9897   tree type = TREE_TYPE (TREE_TYPE (fndecl));
9898   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9899   switch (fcode)
9900     {
9901     CASE_FLT_FN (BUILT_IN_INF):
9902     case BUILT_IN_INFD32:
9903     case BUILT_IN_INFD64:
9904     case BUILT_IN_INFD128:
9905       return fold_builtin_inf (loc, type, true);
9906
9907     CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9908       return fold_builtin_inf (loc, type, false);
9909
9910     case BUILT_IN_CLASSIFY_TYPE:
9911       return fold_builtin_classify_type (NULL_TREE);
9912
9913     default:
9914       break;
9915     }
9916   return NULL_TREE;
9917 }
9918
9919 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9920    IGNORE is true if the result of the function call is ignored.  This
9921    function returns NULL_TREE if no simplification was possible.  */
9922
9923 static tree
9924 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9925 {
9926   tree type = TREE_TYPE (TREE_TYPE (fndecl));
9927   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9928   switch (fcode)
9929     {
9930     case BUILT_IN_CONSTANT_P:
9931       {
9932         tree val = fold_builtin_constant_p (arg0);
9933
9934         /* Gimplification will pull the CALL_EXPR for the builtin out of
9935            an if condition.  When not optimizing, we'll not CSE it back.
9936            To avoid link error types of regressions, return false now.  */
9937         if (!val && !optimize)
9938           val = integer_zero_node;
9939
9940         return val;
9941       }
9942
9943     case BUILT_IN_CLASSIFY_TYPE:
9944       return fold_builtin_classify_type (arg0);
9945
9946     case BUILT_IN_STRLEN:
9947       return fold_builtin_strlen (loc, type, arg0);
9948
9949     CASE_FLT_FN (BUILT_IN_FABS):
9950       return fold_builtin_fabs (loc, arg0, type);
9951
9952     case BUILT_IN_ABS:
9953     case BUILT_IN_LABS:
9954     case BUILT_IN_LLABS:
9955     case BUILT_IN_IMAXABS:
9956       return fold_builtin_abs (loc, arg0, type);
9957
9958     CASE_FLT_FN (BUILT_IN_CONJ):
9959       if (validate_arg (arg0, COMPLEX_TYPE)
9960         && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9961         return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9962     break;
9963
9964     CASE_FLT_FN (BUILT_IN_CREAL):
9965       if (validate_arg (arg0, COMPLEX_TYPE)
9966         && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9967         return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9968     break;
9969
9970     CASE_FLT_FN (BUILT_IN_CIMAG):
9971       if (validate_arg (arg0, COMPLEX_TYPE)
9972           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9973         return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9974     break;
9975
9976     CASE_FLT_FN (BUILT_IN_CCOS):
9977       return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
9978
9979     CASE_FLT_FN (BUILT_IN_CCOSH):
9980       return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
9981
9982     CASE_FLT_FN (BUILT_IN_CPROJ):
9983       return fold_builtin_cproj(loc, arg0, type);
9984
9985     CASE_FLT_FN (BUILT_IN_CSIN):
9986       if (validate_arg (arg0, COMPLEX_TYPE)
9987           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9988         return do_mpc_arg1 (arg0, type, mpc_sin);
9989     break;
9990
9991     CASE_FLT_FN (BUILT_IN_CSINH):
9992       if (validate_arg (arg0, COMPLEX_TYPE)
9993           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9994         return do_mpc_arg1 (arg0, type, mpc_sinh);
9995     break;
9996
9997     CASE_FLT_FN (BUILT_IN_CTAN):
9998       if (validate_arg (arg0, COMPLEX_TYPE)
9999           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10000         return do_mpc_arg1 (arg0, type, mpc_tan);
10001     break;
10002
10003     CASE_FLT_FN (BUILT_IN_CTANH):
10004       if (validate_arg (arg0, COMPLEX_TYPE)
10005           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10006         return do_mpc_arg1 (arg0, type, mpc_tanh);
10007     break;
10008
10009     CASE_FLT_FN (BUILT_IN_CLOG):
10010       if (validate_arg (arg0, COMPLEX_TYPE)
10011           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10012         return do_mpc_arg1 (arg0, type, mpc_log);
10013     break;
10014
10015     CASE_FLT_FN (BUILT_IN_CSQRT):
10016       if (validate_arg (arg0, COMPLEX_TYPE)
10017           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10018         return do_mpc_arg1 (arg0, type, mpc_sqrt);
10019     break;
10020
10021     CASE_FLT_FN (BUILT_IN_CASIN):
10022       if (validate_arg (arg0, COMPLEX_TYPE)
10023           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10024         return do_mpc_arg1 (arg0, type, mpc_asin);
10025     break;
10026
10027     CASE_FLT_FN (BUILT_IN_CACOS):
10028       if (validate_arg (arg0, COMPLEX_TYPE)
10029           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10030         return do_mpc_arg1 (arg0, type, mpc_acos);
10031     break;
10032
10033     CASE_FLT_FN (BUILT_IN_CATAN):
10034       if (validate_arg (arg0, COMPLEX_TYPE)
10035           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10036         return do_mpc_arg1 (arg0, type, mpc_atan);
10037     break;
10038
10039     CASE_FLT_FN (BUILT_IN_CASINH):
10040       if (validate_arg (arg0, COMPLEX_TYPE)
10041           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10042         return do_mpc_arg1 (arg0, type, mpc_asinh);
10043     break;
10044
10045     CASE_FLT_FN (BUILT_IN_CACOSH):
10046       if (validate_arg (arg0, COMPLEX_TYPE)
10047           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10048         return do_mpc_arg1 (arg0, type, mpc_acosh);
10049     break;
10050
10051     CASE_FLT_FN (BUILT_IN_CATANH):
10052       if (validate_arg (arg0, COMPLEX_TYPE)
10053           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10054         return do_mpc_arg1 (arg0, type, mpc_atanh);
10055     break;
10056
10057     CASE_FLT_FN (BUILT_IN_CABS):
10058       return fold_builtin_cabs (loc, arg0, type, fndecl);
10059
10060     CASE_FLT_FN (BUILT_IN_CARG):
10061       return fold_builtin_carg (loc, arg0, type);
10062
10063     CASE_FLT_FN (BUILT_IN_SQRT):
10064       return fold_builtin_sqrt (loc, arg0, type);
10065
10066     CASE_FLT_FN (BUILT_IN_CBRT):
10067       return fold_builtin_cbrt (loc, arg0, type);
10068
10069     CASE_FLT_FN (BUILT_IN_ASIN):
10070       if (validate_arg (arg0, REAL_TYPE))
10071         return do_mpfr_arg1 (arg0, type, mpfr_asin,
10072                              &dconstm1, &dconst1, true);
10073     break;
10074
10075     CASE_FLT_FN (BUILT_IN_ACOS):
10076       if (validate_arg (arg0, REAL_TYPE))
10077         return do_mpfr_arg1 (arg0, type, mpfr_acos,
10078                              &dconstm1, &dconst1, true);
10079     break;
10080
10081     CASE_FLT_FN (BUILT_IN_ATAN):
10082       if (validate_arg (arg0, REAL_TYPE))
10083         return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10084     break;
10085
10086     CASE_FLT_FN (BUILT_IN_ASINH):
10087       if (validate_arg (arg0, REAL_TYPE))
10088         return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10089     break;
10090
10091     CASE_FLT_FN (BUILT_IN_ACOSH):
10092       if (validate_arg (arg0, REAL_TYPE))
10093         return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10094                              &dconst1, NULL, true);
10095     break;
10096
10097     CASE_FLT_FN (BUILT_IN_ATANH):
10098       if (validate_arg (arg0, REAL_TYPE))
10099         return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10100                              &dconstm1, &dconst1, false);
10101     break;
10102
10103     CASE_FLT_FN (BUILT_IN_SIN):
10104       if (validate_arg (arg0, REAL_TYPE))
10105         return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10106     break;
10107
10108     CASE_FLT_FN (BUILT_IN_COS):
10109       return fold_builtin_cos (loc, arg0, type, fndecl);
10110
10111     CASE_FLT_FN (BUILT_IN_TAN):
10112       return fold_builtin_tan (arg0, type);
10113
10114     CASE_FLT_FN (BUILT_IN_CEXP):
10115       return fold_builtin_cexp (loc, arg0, type);
10116
10117     CASE_FLT_FN (BUILT_IN_CEXPI):
10118       if (validate_arg (arg0, REAL_TYPE))
10119         return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10120     break;
10121
10122     CASE_FLT_FN (BUILT_IN_SINH):
10123       if (validate_arg (arg0, REAL_TYPE))
10124         return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10125     break;
10126
10127     CASE_FLT_FN (BUILT_IN_COSH):
10128       return fold_builtin_cosh (loc, arg0, type, fndecl);
10129
10130     CASE_FLT_FN (BUILT_IN_TANH):
10131       if (validate_arg (arg0, REAL_TYPE))
10132         return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10133     break;
10134
10135     CASE_FLT_FN (BUILT_IN_ERF):
10136       if (validate_arg (arg0, REAL_TYPE))
10137         return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10138     break;
10139
10140     CASE_FLT_FN (BUILT_IN_ERFC):
10141       if (validate_arg (arg0, REAL_TYPE))
10142         return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10143     break;
10144
10145     CASE_FLT_FN (BUILT_IN_TGAMMA):
10146       if (validate_arg (arg0, REAL_TYPE))
10147         return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10148     break;
10149
10150     CASE_FLT_FN (BUILT_IN_EXP):
10151       return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10152
10153     CASE_FLT_FN (BUILT_IN_EXP2):
10154       return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10155
10156     CASE_FLT_FN (BUILT_IN_EXP10):
10157     CASE_FLT_FN (BUILT_IN_POW10):
10158       return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10159
10160     CASE_FLT_FN (BUILT_IN_EXPM1):
10161       if (validate_arg (arg0, REAL_TYPE))
10162         return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10163     break;
10164
10165     CASE_FLT_FN (BUILT_IN_LOG):
10166     return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10167
10168     CASE_FLT_FN (BUILT_IN_LOG2):
10169       return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10170
10171     CASE_FLT_FN (BUILT_IN_LOG10):
10172       return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10173
10174     CASE_FLT_FN (BUILT_IN_LOG1P):
10175       if (validate_arg (arg0, REAL_TYPE))
10176         return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10177                              &dconstm1, NULL, false);
10178     break;
10179
10180     CASE_FLT_FN (BUILT_IN_J0):
10181       if (validate_arg (arg0, REAL_TYPE))
10182         return do_mpfr_arg1 (arg0, type, mpfr_j0,
10183                              NULL, NULL, 0);
10184     break;
10185
10186     CASE_FLT_FN (BUILT_IN_J1):
10187       if (validate_arg (arg0, REAL_TYPE))
10188         return do_mpfr_arg1 (arg0, type, mpfr_j1,
10189                              NULL, NULL, 0);
10190     break;
10191
10192     CASE_FLT_FN (BUILT_IN_Y0):
10193       if (validate_arg (arg0, REAL_TYPE))
10194         return do_mpfr_arg1 (arg0, type, mpfr_y0,
10195                              &dconst0, NULL, false);
10196     break;
10197
10198     CASE_FLT_FN (BUILT_IN_Y1):
10199       if (validate_arg (arg0, REAL_TYPE))
10200         return do_mpfr_arg1 (arg0, type, mpfr_y1,
10201                              &dconst0, NULL, false);
10202     break;
10203
10204     CASE_FLT_FN (BUILT_IN_NAN):
10205     case BUILT_IN_NAND32:
10206     case BUILT_IN_NAND64:
10207     case BUILT_IN_NAND128:
10208       return fold_builtin_nan (arg0, type, true);
10209
10210     CASE_FLT_FN (BUILT_IN_NANS):
10211       return fold_builtin_nan (arg0, type, false);
10212
10213     CASE_FLT_FN (BUILT_IN_FLOOR):
10214       return fold_builtin_floor (loc, fndecl, arg0);
10215
10216     CASE_FLT_FN (BUILT_IN_CEIL):
10217       return fold_builtin_ceil (loc, fndecl, arg0);
10218
10219     CASE_FLT_FN (BUILT_IN_TRUNC):
10220       return fold_builtin_trunc (loc, fndecl, arg0);
10221
10222     CASE_FLT_FN (BUILT_IN_ROUND):
10223       return fold_builtin_round (loc, fndecl, arg0);
10224
10225     CASE_FLT_FN (BUILT_IN_NEARBYINT):
10226     CASE_FLT_FN (BUILT_IN_RINT):
10227       return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10228
10229     CASE_FLT_FN (BUILT_IN_LCEIL):
10230     CASE_FLT_FN (BUILT_IN_LLCEIL):
10231     CASE_FLT_FN (BUILT_IN_LFLOOR):
10232     CASE_FLT_FN (BUILT_IN_LLFLOOR):
10233     CASE_FLT_FN (BUILT_IN_LROUND):
10234     CASE_FLT_FN (BUILT_IN_LLROUND):
10235       return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10236
10237     CASE_FLT_FN (BUILT_IN_LRINT):
10238     CASE_FLT_FN (BUILT_IN_LLRINT):
10239       return fold_fixed_mathfn (loc, fndecl, arg0);
10240
10241     case BUILT_IN_BSWAP32:
10242     case BUILT_IN_BSWAP64:
10243       return fold_builtin_bswap (fndecl, arg0);
10244
10245     CASE_INT_FN (BUILT_IN_FFS):
10246     CASE_INT_FN (BUILT_IN_CLZ):
10247     CASE_INT_FN (BUILT_IN_CTZ):
10248     CASE_INT_FN (BUILT_IN_POPCOUNT):
10249     CASE_INT_FN (BUILT_IN_PARITY):
10250       return fold_builtin_bitop (fndecl, arg0);
10251
10252     CASE_FLT_FN (BUILT_IN_SIGNBIT):
10253       return fold_builtin_signbit (loc, arg0, type);
10254
10255     CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10256       return fold_builtin_significand (loc, arg0, type);
10257
10258     CASE_FLT_FN (BUILT_IN_ILOGB):
10259     CASE_FLT_FN (BUILT_IN_LOGB):
10260       return fold_builtin_logb (loc, arg0, type);
10261
10262     case BUILT_IN_ISASCII:
10263       return fold_builtin_isascii (loc, arg0);
10264
10265     case BUILT_IN_TOASCII:
10266       return fold_builtin_toascii (loc, arg0);
10267
10268     case BUILT_IN_ISDIGIT:
10269       return fold_builtin_isdigit (loc, arg0);
10270
10271     CASE_FLT_FN (BUILT_IN_FINITE):
10272     case BUILT_IN_FINITED32:
10273     case BUILT_IN_FINITED64:
10274     case BUILT_IN_FINITED128:
10275     case BUILT_IN_ISFINITE:
10276       {
10277         tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10278         if (ret)
10279           return ret;
10280         return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10281       }
10282
10283     CASE_FLT_FN (BUILT_IN_ISINF):
10284     case BUILT_IN_ISINFD32:
10285     case BUILT_IN_ISINFD64:
10286     case BUILT_IN_ISINFD128:
10287       {
10288         tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10289         if (ret)
10290           return ret;
10291         return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10292       }
10293
10294     case BUILT_IN_ISNORMAL:
10295       return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10296
10297     case BUILT_IN_ISINF_SIGN:
10298       return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10299
10300     CASE_FLT_FN (BUILT_IN_ISNAN):
10301     case BUILT_IN_ISNAND32:
10302     case BUILT_IN_ISNAND64:
10303     case BUILT_IN_ISNAND128:
10304       return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10305
10306     case BUILT_IN_PRINTF:
10307     case BUILT_IN_PRINTF_UNLOCKED:
10308     case BUILT_IN_VPRINTF:
10309       return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10310
10311     case BUILT_IN_FREE:
10312       if (integer_zerop (arg0))
10313         return build_empty_stmt (loc);
10314       break;
10315
10316     default:
10317       break;
10318     }
10319
10320   return NULL_TREE;
10321
10322 }
10323
10324 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10325    IGNORE is true if the result of the function call is ignored.  This
10326    function returns NULL_TREE if no simplification was possible.  */
10327
10328 static tree
10329 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10330 {
10331   tree type = TREE_TYPE (TREE_TYPE (fndecl));
10332   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10333
10334   switch (fcode)
10335     {
10336     CASE_FLT_FN (BUILT_IN_JN):
10337       if (validate_arg (arg0, INTEGER_TYPE)
10338           && validate_arg (arg1, REAL_TYPE))
10339         return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10340     break;
10341
10342     CASE_FLT_FN (BUILT_IN_YN):
10343       if (validate_arg (arg0, INTEGER_TYPE)
10344           && validate_arg (arg1, REAL_TYPE))
10345         return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10346                                  &dconst0, false);
10347     break;
10348
10349     CASE_FLT_FN (BUILT_IN_DREM):
10350     CASE_FLT_FN (BUILT_IN_REMAINDER):
10351       if (validate_arg (arg0, REAL_TYPE)
10352           && validate_arg(arg1, REAL_TYPE))
10353         return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10354     break;
10355
10356     CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10357     CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10358       if (validate_arg (arg0, REAL_TYPE)
10359           && validate_arg(arg1, POINTER_TYPE))
10360         return do_mpfr_lgamma_r (arg0, arg1, type);
10361     break;
10362
10363     CASE_FLT_FN (BUILT_IN_ATAN2):
10364       if (validate_arg (arg0, REAL_TYPE)
10365           && validate_arg(arg1, REAL_TYPE))
10366         return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10367     break;
10368
10369     CASE_FLT_FN (BUILT_IN_FDIM):
10370       if (validate_arg (arg0, REAL_TYPE)
10371           && validate_arg(arg1, REAL_TYPE))
10372         return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10373     break;
10374
10375     CASE_FLT_FN (BUILT_IN_HYPOT):
10376       return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10377
10378     CASE_FLT_FN (BUILT_IN_CPOW):
10379       if (validate_arg (arg0, COMPLEX_TYPE)
10380           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10381           && validate_arg (arg1, COMPLEX_TYPE)
10382           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10383         return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10384     break;
10385
10386     CASE_FLT_FN (BUILT_IN_LDEXP):
10387       return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10388     CASE_FLT_FN (BUILT_IN_SCALBN):
10389     CASE_FLT_FN (BUILT_IN_SCALBLN):
10390       return fold_builtin_load_exponent (loc, arg0, arg1,
10391                                          type, /*ldexp=*/false);
10392
10393     CASE_FLT_FN (BUILT_IN_FREXP):
10394       return fold_builtin_frexp (loc, arg0, arg1, type);
10395
10396     CASE_FLT_FN (BUILT_IN_MODF):
10397       return fold_builtin_modf (loc, arg0, arg1, type);
10398
10399     case BUILT_IN_BZERO:
10400       return fold_builtin_bzero (loc, arg0, arg1, ignore);
10401
10402     case BUILT_IN_FPUTS:
10403       return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10404
10405     case BUILT_IN_FPUTS_UNLOCKED:
10406       return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10407
10408     case BUILT_IN_STRSTR:
10409       return fold_builtin_strstr (loc, arg0, arg1, type);
10410
10411     case BUILT_IN_STRCAT:
10412       return fold_builtin_strcat (loc, arg0, arg1);
10413
10414     case BUILT_IN_STRSPN:
10415       return fold_builtin_strspn (loc, arg0, arg1);
10416
10417     case BUILT_IN_STRCSPN:
10418       return fold_builtin_strcspn (loc, arg0, arg1);
10419
10420     case BUILT_IN_STRCHR:
10421     case BUILT_IN_INDEX:
10422       return fold_builtin_strchr (loc, arg0, arg1, type);
10423
10424     case BUILT_IN_STRRCHR:
10425     case BUILT_IN_RINDEX:
10426       return fold_builtin_strrchr (loc, arg0, arg1, type);
10427
10428     case BUILT_IN_STRCPY:
10429       return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10430
10431     case BUILT_IN_STPCPY:
10432       if (ignore)
10433         {
10434           tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10435           if (!fn)
10436             break;
10437
10438           return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10439         }
10440       else
10441         return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10442       break;
10443
10444     case BUILT_IN_STRCMP:
10445       return fold_builtin_strcmp (loc, arg0, arg1);
10446
10447     case BUILT_IN_STRPBRK:
10448       return fold_builtin_strpbrk (loc, arg0, arg1, type);
10449
10450     case BUILT_IN_EXPECT:
10451       return fold_builtin_expect (loc, arg0, arg1);
10452
10453     CASE_FLT_FN (BUILT_IN_POW):
10454       return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10455
10456     CASE_FLT_FN (BUILT_IN_POWI):
10457       return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10458
10459     CASE_FLT_FN (BUILT_IN_COPYSIGN):
10460       return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10461
10462     CASE_FLT_FN (BUILT_IN_FMIN):
10463       return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10464
10465     CASE_FLT_FN (BUILT_IN_FMAX):
10466       return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10467
10468     case BUILT_IN_ISGREATER:
10469       return fold_builtin_unordered_cmp (loc, fndecl,
10470                                          arg0, arg1, UNLE_EXPR, LE_EXPR);
10471     case BUILT_IN_ISGREATEREQUAL:
10472       return fold_builtin_unordered_cmp (loc, fndecl,
10473                                          arg0, arg1, UNLT_EXPR, LT_EXPR);
10474     case BUILT_IN_ISLESS:
10475       return fold_builtin_unordered_cmp (loc, fndecl,
10476                                          arg0, arg1, UNGE_EXPR, GE_EXPR);
10477     case BUILT_IN_ISLESSEQUAL:
10478       return fold_builtin_unordered_cmp (loc, fndecl,
10479                                          arg0, arg1, UNGT_EXPR, GT_EXPR);
10480     case BUILT_IN_ISLESSGREATER:
10481       return fold_builtin_unordered_cmp (loc, fndecl,
10482                                          arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10483     case BUILT_IN_ISUNORDERED:
10484       return fold_builtin_unordered_cmp (loc, fndecl,
10485                                          arg0, arg1, UNORDERED_EXPR,
10486                                          NOP_EXPR);
10487
10488       /* We do the folding for va_start in the expander.  */
10489     case BUILT_IN_VA_START:
10490       break;
10491
10492     case BUILT_IN_SPRINTF:
10493       return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10494
10495     case BUILT_IN_OBJECT_SIZE:
10496       return fold_builtin_object_size (arg0, arg1);
10497
10498     case BUILT_IN_PRINTF:
10499     case BUILT_IN_PRINTF_UNLOCKED:
10500     case BUILT_IN_VPRINTF:
10501       return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10502
10503     case BUILT_IN_PRINTF_CHK:
10504     case BUILT_IN_VPRINTF_CHK:
10505       if (!validate_arg (arg0, INTEGER_TYPE)
10506           || TREE_SIDE_EFFECTS (arg0))
10507         return NULL_TREE;
10508       else
10509         return fold_builtin_printf (loc, fndecl,
10510                                     arg1, NULL_TREE, ignore, fcode);
10511     break;
10512
10513     case BUILT_IN_FPRINTF:
10514     case BUILT_IN_FPRINTF_UNLOCKED:
10515     case BUILT_IN_VFPRINTF:
10516       return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10517                                    ignore, fcode);
10518
10519     default:
10520       break;
10521     }
10522   return NULL_TREE;
10523 }
10524
10525 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10526    and ARG2.  IGNORE is true if the result of the function call is ignored.
10527    This function returns NULL_TREE if no simplification was possible.  */
10528
10529 static tree
10530 fold_builtin_3 (location_t loc, tree fndecl,
10531                 tree arg0, tree arg1, tree arg2, bool ignore)
10532 {
10533   tree type = TREE_TYPE (TREE_TYPE (fndecl));
10534   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10535   switch (fcode)
10536     {
10537
10538     CASE_FLT_FN (BUILT_IN_SINCOS):
10539       return fold_builtin_sincos (loc, arg0, arg1, arg2);
10540
10541     CASE_FLT_FN (BUILT_IN_FMA):
10542       if (validate_arg (arg0, REAL_TYPE)
10543           && validate_arg(arg1, REAL_TYPE)
10544           && validate_arg(arg2, REAL_TYPE))
10545         return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
10546     break;
10547
10548     CASE_FLT_FN (BUILT_IN_REMQUO):
10549       if (validate_arg (arg0, REAL_TYPE)
10550           && validate_arg(arg1, REAL_TYPE)
10551           && validate_arg(arg2, POINTER_TYPE))
10552         return do_mpfr_remquo (arg0, arg1, arg2);
10553     break;
10554
10555     case BUILT_IN_MEMSET:
10556       return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10557
10558     case BUILT_IN_BCOPY:
10559       return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10560                                      void_type_node, true, /*endp=*/3);
10561
10562     case BUILT_IN_MEMCPY:
10563       return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10564                                      type, ignore, /*endp=*/0);
10565
10566     case BUILT_IN_MEMPCPY:
10567       return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10568                                      type, ignore, /*endp=*/1);
10569
10570     case BUILT_IN_MEMMOVE:
10571       return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10572                                      type, ignore, /*endp=*/3);
10573
10574     case BUILT_IN_STRNCAT:
10575       return fold_builtin_strncat (loc, arg0, arg1, arg2);
10576
10577     case BUILT_IN_STRNCPY:
10578       return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10579
10580     case BUILT_IN_STRNCMP:
10581       return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10582
10583     case BUILT_IN_MEMCHR:
10584       return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10585
10586     case BUILT_IN_BCMP:
10587     case BUILT_IN_MEMCMP:
10588       return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10589
10590     case BUILT_IN_SPRINTF:
10591       return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10592
10593     case BUILT_IN_STRCPY_CHK:
10594     case BUILT_IN_STPCPY_CHK:
10595       return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10596                                       ignore, fcode);
10597
10598     case BUILT_IN_STRCAT_CHK:
10599       return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10600
10601     case BUILT_IN_PRINTF_CHK:
10602     case BUILT_IN_VPRINTF_CHK:
10603       if (!validate_arg (arg0, INTEGER_TYPE)
10604           || TREE_SIDE_EFFECTS (arg0))
10605         return NULL_TREE;
10606       else
10607         return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10608     break;
10609
10610     case BUILT_IN_FPRINTF:
10611     case BUILT_IN_FPRINTF_UNLOCKED:
10612     case BUILT_IN_VFPRINTF:
10613       return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10614                                    ignore, fcode);
10615
10616     case BUILT_IN_FPRINTF_CHK:
10617     case BUILT_IN_VFPRINTF_CHK:
10618       if (!validate_arg (arg1, INTEGER_TYPE)
10619           || TREE_SIDE_EFFECTS (arg1))
10620         return NULL_TREE;
10621       else
10622         return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10623                                      ignore, fcode);
10624
10625     default:
10626       break;
10627     }
10628   return NULL_TREE;
10629 }
10630
10631 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10632    ARG2, and ARG3.  IGNORE is true if the result of the function call is
10633    ignored.  This function returns NULL_TREE if no simplification was
10634    possible.  */
10635
10636 static tree
10637 fold_builtin_4 (location_t loc, tree fndecl,
10638                 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10639 {
10640   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10641
10642   switch (fcode)
10643     {
10644     case BUILT_IN_MEMCPY_CHK:
10645     case BUILT_IN_MEMPCPY_CHK:
10646     case BUILT_IN_MEMMOVE_CHK:
10647     case BUILT_IN_MEMSET_CHK:
10648       return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10649                                       NULL_TREE, ignore,
10650                                       DECL_FUNCTION_CODE (fndecl));
10651
10652     case BUILT_IN_STRNCPY_CHK:
10653       return fold_builtin_strncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE);
10654
10655     case BUILT_IN_STRNCAT_CHK:
10656       return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10657
10658     case BUILT_IN_FPRINTF_CHK:
10659     case BUILT_IN_VFPRINTF_CHK:
10660       if (!validate_arg (arg1, INTEGER_TYPE)
10661           || TREE_SIDE_EFFECTS (arg1))
10662         return NULL_TREE;
10663       else
10664         return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10665                                      ignore, fcode);
10666     break;
10667
10668     default:
10669       break;
10670     }
10671   return NULL_TREE;
10672 }
10673
10674 /* Fold a call to built-in function FNDECL.  ARGS is an array of NARGS
10675     arguments, where NARGS <= 4.  IGNORE is true if the result of the
10676     function call is ignored.  This function returns NULL_TREE if no
10677     simplification was possible.  Note that this only folds builtins with
10678     fixed argument patterns.  Foldings that do varargs-to-varargs
10679     transformations, or that match calls with more than 4 arguments,
10680     need to be handled with fold_builtin_varargs instead.  */
10681
10682 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10683
10684 static tree
10685 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10686 {
10687   tree ret = NULL_TREE;
10688
10689   switch (nargs)
10690     {
10691     case 0:
10692       ret = fold_builtin_0 (loc, fndecl, ignore);
10693       break;
10694     case 1:
10695       ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10696       break;
10697     case 2:
10698       ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10699       break;
10700     case 3:
10701       ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10702       break;
10703     case 4:
10704       ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10705                             ignore);
10706       break;
10707     default:
10708       break;
10709     }
10710   if (ret)
10711     {
10712       ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10713       SET_EXPR_LOCATION (ret, loc);
10714       TREE_NO_WARNING (ret) = 1;
10715       return ret;
10716     }
10717   return NULL_TREE;
10718 }
10719
10720 /* Builtins with folding operations that operate on "..." arguments
10721    need special handling; we need to store the arguments in a convenient
10722    data structure before attempting any folding.  Fortunately there are
10723    only a few builtins that fall into this category.  FNDECL is the
10724    function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10725    result of the function call is ignored.  */
10726
10727 static tree
10728 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
10729                       bool ignore ATTRIBUTE_UNUSED)
10730 {
10731   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10732   tree ret = NULL_TREE;
10733
10734   switch (fcode)
10735     {
10736     case BUILT_IN_SPRINTF_CHK:
10737     case BUILT_IN_VSPRINTF_CHK:
10738       ret = fold_builtin_sprintf_chk (loc, exp, fcode);
10739       break;
10740
10741     case BUILT_IN_SNPRINTF_CHK:
10742     case BUILT_IN_VSNPRINTF_CHK:
10743       ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
10744       break;
10745
10746     case BUILT_IN_FPCLASSIFY:
10747       ret = fold_builtin_fpclassify (loc, exp);
10748       break;
10749
10750     default:
10751       break;
10752     }
10753   if (ret)
10754     {
10755       ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10756       SET_EXPR_LOCATION (ret, loc);
10757       TREE_NO_WARNING (ret) = 1;
10758       return ret;
10759     }
10760   return NULL_TREE;
10761 }
10762
10763 /* Return true if FNDECL shouldn't be folded right now.
10764    If a built-in function has an inline attribute always_inline
10765    wrapper, defer folding it after always_inline functions have
10766    been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10767    might not be performed.  */
10768
10769 static bool
10770 avoid_folding_inline_builtin (tree fndecl)
10771 {
10772   return (DECL_DECLARED_INLINE_P (fndecl)
10773           && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10774           && cfun
10775           && !cfun->always_inline_functions_inlined
10776           && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10777 }
10778
10779 /* A wrapper function for builtin folding that prevents warnings for
10780    "statement without effect" and the like, caused by removing the
10781    call node earlier than the warning is generated.  */
10782
10783 tree
10784 fold_call_expr (location_t loc, tree exp, bool ignore)
10785 {
10786   tree ret = NULL_TREE;
10787   tree fndecl = get_callee_fndecl (exp);
10788   if (fndecl
10789       && TREE_CODE (fndecl) == FUNCTION_DECL
10790       && DECL_BUILT_IN (fndecl)
10791       /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10792          yet.  Defer folding until we see all the arguments
10793          (after inlining).  */
10794       && !CALL_EXPR_VA_ARG_PACK (exp))
10795     {
10796       int nargs = call_expr_nargs (exp);
10797
10798       /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10799          instead last argument is __builtin_va_arg_pack ().  Defer folding
10800          even in that case, until arguments are finalized.  */
10801       if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10802         {
10803           tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10804           if (fndecl2
10805               && TREE_CODE (fndecl2) == FUNCTION_DECL
10806               && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10807               && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10808             return NULL_TREE;
10809         }
10810
10811       if (avoid_folding_inline_builtin (fndecl))
10812         return NULL_TREE;
10813
10814       if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10815         return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10816                                      CALL_EXPR_ARGP (exp), ignore);
10817       else
10818         {
10819           if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10820             {
10821               tree *args = CALL_EXPR_ARGP (exp);
10822               ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10823             }
10824           if (!ret)
10825             ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10826           if (ret)
10827             return ret;
10828         }
10829     }
10830   return NULL_TREE;
10831 }
10832
10833 /* Conveniently construct a function call expression.  FNDECL names the
10834    function to be called and N arguments are passed in the array
10835    ARGARRAY.  */
10836
10837 tree
10838 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10839 {
10840   tree fntype = TREE_TYPE (fndecl);
10841   tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10842  
10843   return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
10844 }
10845
10846 /* Conveniently construct a function call expression.  FNDECL names the
10847    function to be called and the arguments are passed in the vector
10848    VEC.  */
10849
10850 tree
10851 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
10852 {
10853   return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
10854                                     VEC_address (tree, vec));
10855 }
10856
10857
10858 /* Conveniently construct a function call expression.  FNDECL names the
10859    function to be called, N is the number of arguments, and the "..."
10860    parameters are the argument expressions.  */
10861
10862 tree
10863 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10864 {
10865   va_list ap;
10866   tree *argarray = XALLOCAVEC (tree, n);
10867   int i;
10868
10869   va_start (ap, n);
10870   for (i = 0; i < n; i++)
10871     argarray[i] = va_arg (ap, tree);
10872   va_end (ap);
10873   return build_call_expr_loc_array (loc, fndecl, n, argarray);
10874 }
10875
10876 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...).  Duplicated because
10877    varargs macros aren't supported by all bootstrap compilers.  */
10878
10879 tree
10880 build_call_expr (tree fndecl, int n, ...)
10881 {
10882   va_list ap;
10883   tree *argarray = XALLOCAVEC (tree, n);
10884   int i;
10885
10886   va_start (ap, n);
10887   for (i = 0; i < n; i++)
10888     argarray[i] = va_arg (ap, tree);
10889   va_end (ap);
10890   return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10891 }
10892
10893 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10894    N arguments are passed in the array ARGARRAY.  */
10895
10896 tree
10897 fold_builtin_call_array (location_t loc, tree type,
10898                          tree fn,
10899                          int n,
10900                          tree *argarray)
10901 {
10902   tree ret = NULL_TREE;
10903    tree exp;
10904
10905   if (TREE_CODE (fn) == ADDR_EXPR)
10906   {
10907     tree fndecl = TREE_OPERAND (fn, 0);
10908     if (TREE_CODE (fndecl) == FUNCTION_DECL
10909         && DECL_BUILT_IN (fndecl))
10910       {
10911         /* If last argument is __builtin_va_arg_pack (), arguments to this
10912            function are not finalized yet.  Defer folding until they are.  */
10913         if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10914           {
10915             tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10916             if (fndecl2
10917                 && TREE_CODE (fndecl2) == FUNCTION_DECL
10918                 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10919                 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10920               return build_call_array_loc (loc, type, fn, n, argarray);
10921           }
10922         if (avoid_folding_inline_builtin (fndecl))
10923           return build_call_array_loc (loc, type, fn, n, argarray);
10924         if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10925           {
10926             ret = targetm.fold_builtin (fndecl, n, argarray, false);
10927             if (ret)
10928               return ret;
10929
10930             return build_call_array_loc (loc, type, fn, n, argarray);
10931           }
10932         else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10933           {
10934             /* First try the transformations that don't require consing up
10935                an exp.  */
10936             ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10937             if (ret)
10938               return ret;
10939           }
10940
10941         /* If we got this far, we need to build an exp.  */
10942         exp = build_call_array_loc (loc, type, fn, n, argarray);
10943         ret = fold_builtin_varargs (loc, fndecl, exp, false);
10944         return ret ? ret : exp;
10945       }
10946   }
10947
10948   return build_call_array_loc (loc, type, fn, n, argarray);
10949 }
10950
10951 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10952    list ARGS along with N new arguments in NEWARGS.  SKIP is the number
10953    of arguments in ARGS to be omitted.  OLDNARGS is the number of
10954    elements in ARGS.  */
10955
10956 static tree
10957 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10958                           int skip, tree fndecl, int n, va_list newargs)
10959 {
10960   int nargs = oldnargs - skip + n;
10961   tree *buffer;
10962
10963   if (n > 0)
10964     {
10965       int i, j;
10966
10967       buffer = XALLOCAVEC (tree, nargs);
10968       for (i = 0; i < n; i++)
10969         buffer[i] = va_arg (newargs, tree);
10970       for (j = skip; j < oldnargs; j++, i++)
10971         buffer[i] = args[j];
10972     }
10973   else
10974     buffer = args + skip;
10975
10976   return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10977 }
10978
10979 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10980    list ARGS along with N new arguments specified as the "..."
10981    parameters.  SKIP is the number of arguments in ARGS to be omitted.
10982    OLDNARGS is the number of elements in ARGS.  */
10983
10984 static tree
10985 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
10986                          int skip, tree fndecl, int n, ...)
10987 {
10988   va_list ap;
10989   tree t;
10990
10991   va_start (ap, n);
10992   t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
10993   va_end (ap);
10994
10995   return t;
10996 }
10997
10998 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10999    along with N new arguments specified as the "..." parameters.  SKIP
11000    is the number of arguments in EXP to be omitted.  This function is used
11001    to do varargs-to-varargs transformations.  */
11002
11003 static tree
11004 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11005 {
11006   va_list ap;
11007   tree t;
11008
11009   va_start (ap, n);
11010   t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11011                                 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11012   va_end (ap);
11013
11014   return t;
11015 }
11016
11017 /* Validate a single argument ARG against a tree code CODE representing
11018    a type.  */
11019
11020 static bool
11021 validate_arg (const_tree arg, enum tree_code code)
11022 {
11023   if (!arg)
11024     return false;
11025   else if (code == POINTER_TYPE)
11026     return POINTER_TYPE_P (TREE_TYPE (arg));
11027   else if (code == INTEGER_TYPE)
11028     return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11029   return code == TREE_CODE (TREE_TYPE (arg));
11030 }
11031
11032 /* This function validates the types of a function call argument list
11033    against a specified list of tree_codes.  If the last specifier is a 0,
11034    that represents an ellipses, otherwise the last specifier must be a
11035    VOID_TYPE.
11036
11037    This is the GIMPLE version of validate_arglist.  Eventually we want to
11038    completely convert builtins.c to work from GIMPLEs and the tree based
11039    validate_arglist will then be removed.  */
11040
11041 bool
11042 validate_gimple_arglist (const_gimple call, ...)
11043 {
11044   enum tree_code code;
11045   bool res = 0;
11046   va_list ap;
11047   const_tree arg;
11048   size_t i;
11049
11050   va_start (ap, call);
11051   i = 0;
11052
11053   do
11054     {
11055       code = (enum tree_code) va_arg (ap, int);
11056       switch (code)
11057         {
11058         case 0:
11059           /* This signifies an ellipses, any further arguments are all ok.  */
11060           res = true;
11061           goto end;
11062         case VOID_TYPE:
11063           /* This signifies an endlink, if no arguments remain, return
11064              true, otherwise return false.  */
11065           res = (i == gimple_call_num_args (call));
11066           goto end;
11067         default:
11068           /* If no parameters remain or the parameter's code does not
11069              match the specified code, return false.  Otherwise continue
11070              checking any remaining arguments.  */
11071           arg = gimple_call_arg (call, i++);
11072           if (!validate_arg (arg, code))
11073             goto end;
11074           break;
11075         }
11076     }
11077   while (1);
11078
11079   /* We need gotos here since we can only have one VA_CLOSE in a
11080      function.  */
11081  end: ;
11082   va_end (ap);
11083
11084   return res;
11085 }
11086
11087 /* This function validates the types of a function call argument list
11088    against a specified list of tree_codes.  If the last specifier is a 0,
11089    that represents an ellipses, otherwise the last specifier must be a
11090    VOID_TYPE.  */
11091
11092 bool
11093 validate_arglist (const_tree callexpr, ...)
11094 {
11095   enum tree_code code;
11096   bool res = 0;
11097   va_list ap;
11098   const_call_expr_arg_iterator iter;
11099   const_tree arg;
11100
11101   va_start (ap, callexpr);
11102   init_const_call_expr_arg_iterator (callexpr, &iter);
11103
11104   do
11105     {
11106       code = (enum tree_code) va_arg (ap, int);
11107       switch (code)
11108         {
11109         case 0:
11110           /* This signifies an ellipses, any further arguments are all ok.  */
11111           res = true;
11112           goto end;
11113         case VOID_TYPE:
11114           /* This signifies an endlink, if no arguments remain, return
11115              true, otherwise return false.  */
11116           res = !more_const_call_expr_args_p (&iter);
11117           goto end;
11118         default:
11119           /* If no parameters remain or the parameter's code does not
11120              match the specified code, return false.  Otherwise continue
11121              checking any remaining arguments.  */
11122           arg = next_const_call_expr_arg (&iter);
11123           if (!validate_arg (arg, code))
11124             goto end;
11125           break;
11126         }
11127     }
11128   while (1);
11129
11130   /* We need gotos here since we can only have one VA_CLOSE in a
11131      function.  */
11132  end: ;
11133   va_end (ap);
11134
11135   return res;
11136 }
11137
11138 /* Default target-specific builtin expander that does nothing.  */
11139
11140 rtx
11141 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11142                         rtx target ATTRIBUTE_UNUSED,
11143                         rtx subtarget ATTRIBUTE_UNUSED,
11144                         enum machine_mode mode ATTRIBUTE_UNUSED,
11145                         int ignore ATTRIBUTE_UNUSED)
11146 {
11147   return NULL_RTX;
11148 }
11149
11150 /* Returns true is EXP represents data that would potentially reside
11151    in a readonly section.  */
11152
11153 static bool
11154 readonly_data_expr (tree exp)
11155 {
11156   STRIP_NOPS (exp);
11157
11158   if (TREE_CODE (exp) != ADDR_EXPR)
11159     return false;
11160
11161   exp = get_base_address (TREE_OPERAND (exp, 0));
11162   if (!exp)
11163     return false;
11164
11165   /* Make sure we call decl_readonly_section only for trees it
11166      can handle (since it returns true for everything it doesn't
11167      understand).  */
11168   if (TREE_CODE (exp) == STRING_CST
11169       || TREE_CODE (exp) == CONSTRUCTOR
11170       || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11171     return decl_readonly_section (exp, 0);
11172   else
11173     return false;
11174 }
11175
11176 /* Simplify a call to the strstr builtin.  S1 and S2 are the arguments
11177    to the call, and TYPE is its return type.
11178
11179    Return NULL_TREE if no simplification was possible, otherwise return the
11180    simplified form of the call as a tree.
11181
11182    The simplified form may be a constant or other expression which
11183    computes the same value, but in a more efficient manner (including
11184    calls to other builtin functions).
11185
11186    The call may contain arguments which need to be evaluated, but
11187    which are not useful to determine the result of the call.  In
11188    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11189    COMPOUND_EXPR will be an argument which must be evaluated.
11190    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11191    COMPOUND_EXPR in the chain will contain the tree for the simplified
11192    form of the builtin function call.  */
11193
11194 static tree
11195 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11196 {
11197   if (!validate_arg (s1, POINTER_TYPE)
11198       || !validate_arg (s2, POINTER_TYPE))
11199     return NULL_TREE;
11200   else
11201     {
11202       tree fn;
11203       const char *p1, *p2;
11204
11205       p2 = c_getstr (s2);
11206       if (p2 == NULL)
11207         return NULL_TREE;
11208
11209       p1 = c_getstr (s1);
11210       if (p1 != NULL)
11211         {
11212           const char *r = strstr (p1, p2);
11213           tree tem;
11214
11215           if (r == NULL)
11216             return build_int_cst (TREE_TYPE (s1), 0);
11217
11218           /* Return an offset into the constant string argument.  */
11219           tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11220                              s1, size_int (r - p1));
11221           return fold_convert_loc (loc, type, tem);
11222         }
11223
11224       /* The argument is const char *, and the result is char *, so we need
11225          a type conversion here to avoid a warning.  */
11226       if (p2[0] == '\0')
11227         return fold_convert_loc (loc, type, s1);
11228
11229       if (p2[1] != '\0')
11230         return NULL_TREE;
11231
11232       fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11233       if (!fn)
11234         return NULL_TREE;
11235
11236       /* New argument list transforming strstr(s1, s2) to
11237          strchr(s1, s2[0]).  */
11238       return build_call_expr_loc (loc, fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11239     }
11240 }
11241
11242 /* Simplify a call to the strchr builtin.  S1 and S2 are the arguments to
11243    the call, and TYPE is its return type.
11244
11245    Return NULL_TREE if no simplification was possible, otherwise return the
11246    simplified form of the call as a tree.
11247
11248    The simplified form may be a constant or other expression which
11249    computes the same value, but in a more efficient manner (including
11250    calls to other builtin functions).
11251
11252    The call may contain arguments which need to be evaluated, but
11253    which are not useful to determine the result of the call.  In
11254    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11255    COMPOUND_EXPR will be an argument which must be evaluated.
11256    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11257    COMPOUND_EXPR in the chain will contain the tree for the simplified
11258    form of the builtin function call.  */
11259
11260 static tree
11261 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11262 {
11263   if (!validate_arg (s1, POINTER_TYPE)
11264       || !validate_arg (s2, INTEGER_TYPE))
11265     return NULL_TREE;
11266   else
11267     {
11268       const char *p1;
11269
11270       if (TREE_CODE (s2) != INTEGER_CST)
11271         return NULL_TREE;
11272
11273       p1 = c_getstr (s1);
11274       if (p1 != NULL)
11275         {
11276           char c;
11277           const char *r;
11278           tree tem;
11279
11280           if (target_char_cast (s2, &c))
11281             return NULL_TREE;
11282
11283           r = strchr (p1, c);
11284
11285           if (r == NULL)
11286             return build_int_cst (TREE_TYPE (s1), 0);
11287
11288           /* Return an offset into the constant string argument.  */
11289           tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11290                              s1, size_int (r - p1));
11291           return fold_convert_loc (loc, type, tem);
11292         }
11293       return NULL_TREE;
11294     }
11295 }
11296
11297 /* Simplify a call to the strrchr builtin.  S1 and S2 are the arguments to
11298    the call, and TYPE is its return type.
11299
11300    Return NULL_TREE if no simplification was possible, otherwise return the
11301    simplified form of the call as a tree.
11302
11303    The simplified form may be a constant or other expression which
11304    computes the same value, but in a more efficient manner (including
11305    calls to other builtin functions).
11306
11307    The call may contain arguments which need to be evaluated, but
11308    which are not useful to determine the result of the call.  In
11309    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11310    COMPOUND_EXPR will be an argument which must be evaluated.
11311    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11312    COMPOUND_EXPR in the chain will contain the tree for the simplified
11313    form of the builtin function call.  */
11314
11315 static tree
11316 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11317 {
11318   if (!validate_arg (s1, POINTER_TYPE)
11319       || !validate_arg (s2, INTEGER_TYPE))
11320     return NULL_TREE;
11321   else
11322     {
11323       tree fn;
11324       const char *p1;
11325
11326       if (TREE_CODE (s2) != INTEGER_CST)
11327         return NULL_TREE;
11328
11329       p1 = c_getstr (s1);
11330       if (p1 != NULL)
11331         {
11332           char c;
11333           const char *r;
11334           tree tem;
11335
11336           if (target_char_cast (s2, &c))
11337             return NULL_TREE;
11338
11339           r = strrchr (p1, c);
11340
11341           if (r == NULL)
11342             return build_int_cst (TREE_TYPE (s1), 0);
11343
11344           /* Return an offset into the constant string argument.  */
11345           tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11346                              s1, size_int (r - p1));
11347           return fold_convert_loc (loc, type, tem);
11348         }
11349
11350       if (! integer_zerop (s2))
11351         return NULL_TREE;
11352
11353       fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11354       if (!fn)
11355         return NULL_TREE;
11356
11357       /* Transform strrchr(s1, '\0') to strchr(s1, '\0').  */
11358       return build_call_expr_loc (loc, fn, 2, s1, s2);
11359     }
11360 }
11361
11362 /* Simplify a call to the strpbrk builtin.  S1 and S2 are the arguments
11363    to the call, and TYPE is its return type.
11364
11365    Return NULL_TREE if no simplification was possible, otherwise return the
11366    simplified form of the call as a tree.
11367
11368    The simplified form may be a constant or other expression which
11369    computes the same value, but in a more efficient manner (including
11370    calls to other builtin functions).
11371
11372    The call may contain arguments which need to be evaluated, but
11373    which are not useful to determine the result of the call.  In
11374    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11375    COMPOUND_EXPR will be an argument which must be evaluated.
11376    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11377    COMPOUND_EXPR in the chain will contain the tree for the simplified
11378    form of the builtin function call.  */
11379
11380 static tree
11381 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11382 {
11383   if (!validate_arg (s1, POINTER_TYPE)
11384       || !validate_arg (s2, POINTER_TYPE))
11385     return NULL_TREE;
11386   else
11387     {
11388       tree fn;
11389       const char *p1, *p2;
11390
11391       p2 = c_getstr (s2);
11392       if (p2 == NULL)
11393         return NULL_TREE;
11394
11395       p1 = c_getstr (s1);
11396       if (p1 != NULL)
11397         {
11398           const char *r = strpbrk (p1, p2);
11399           tree tem;
11400
11401           if (r == NULL)
11402             return build_int_cst (TREE_TYPE (s1), 0);
11403
11404           /* Return an offset into the constant string argument.  */
11405           tem = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (s1),
11406                              s1, size_int (r - p1));
11407           return fold_convert_loc (loc, type, tem);
11408         }
11409
11410       if (p2[0] == '\0')
11411         /* strpbrk(x, "") == NULL.
11412            Evaluate and ignore s1 in case it had side-effects.  */
11413         return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11414
11415       if (p2[1] != '\0')
11416         return NULL_TREE;  /* Really call strpbrk.  */
11417
11418       fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11419       if (!fn)
11420         return NULL_TREE;
11421
11422       /* New argument list transforming strpbrk(s1, s2) to
11423          strchr(s1, s2[0]).  */
11424       return build_call_expr_loc (loc, fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11425     }
11426 }
11427
11428 /* Simplify a call to the strcat builtin.  DST and SRC are the arguments
11429    to the call.
11430
11431    Return NULL_TREE if no simplification was possible, otherwise return the
11432    simplified form of the call as a tree.
11433
11434    The simplified form may be a constant or other expression which
11435    computes the same value, but in a more efficient manner (including
11436    calls to other builtin functions).
11437
11438    The call may contain arguments which need to be evaluated, but
11439    which are not useful to determine the result of the call.  In
11440    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11441    COMPOUND_EXPR will be an argument which must be evaluated.
11442    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11443    COMPOUND_EXPR in the chain will contain the tree for the simplified
11444    form of the builtin function call.  */
11445
11446 static tree
11447 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11448 {
11449   if (!validate_arg (dst, POINTER_TYPE)
11450       || !validate_arg (src, POINTER_TYPE))
11451     return NULL_TREE;
11452   else
11453     {
11454       const char *p = c_getstr (src);
11455
11456       /* If the string length is zero, return the dst parameter.  */
11457       if (p && *p == '\0')
11458         return dst;
11459
11460       if (optimize_insn_for_speed_p ())
11461         {
11462           /* See if we can store by pieces into (dst + strlen(dst)).  */
11463           tree newdst, call;
11464           tree strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11465           tree strcpy_fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11466
11467           if (!strlen_fn || !strcpy_fn)
11468             return NULL_TREE;
11469
11470           /* If we don't have a movstr we don't want to emit an strcpy
11471              call.  We have to do that if the length of the source string
11472              isn't computable (in that case we can use memcpy probably
11473              later expanding to a sequence of mov instructions).  If we
11474              have movstr instructions we can emit strcpy calls.  */
11475           if (!HAVE_movstr)
11476             {
11477               tree len = c_strlen (src, 1);
11478               if (! len || TREE_SIDE_EFFECTS (len))
11479                 return NULL_TREE;
11480             }
11481
11482           /* Stabilize the argument list.  */
11483           dst = builtin_save_expr (dst);
11484
11485           /* Create strlen (dst).  */
11486           newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11487           /* Create (dst p+ strlen (dst)).  */
11488
11489           newdst = fold_build2_loc (loc, POINTER_PLUS_EXPR,
11490                                 TREE_TYPE (dst), dst, newdst);
11491           newdst = builtin_save_expr (newdst);
11492
11493           call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11494           return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11495         }
11496       return NULL_TREE;
11497     }
11498 }
11499
11500 /* Simplify a call to the strncat builtin.  DST, SRC, and LEN are the
11501    arguments to the call.
11502
11503    Return NULL_TREE if no simplification was possible, otherwise return the
11504    simplified form of the call as a tree.
11505
11506    The simplified form may be a constant or other expression which
11507    computes the same value, but in a more efficient manner (including
11508    calls to other builtin functions).
11509
11510    The call may contain arguments which need to be evaluated, but
11511    which are not useful to determine the result of the call.  In
11512    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11513    COMPOUND_EXPR will be an argument which must be evaluated.
11514    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11515    COMPOUND_EXPR in the chain will contain the tree for the simplified
11516    form of the builtin function call.  */
11517
11518 static tree
11519 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11520 {
11521   if (!validate_arg (dst, POINTER_TYPE)
11522       || !validate_arg (src, POINTER_TYPE)
11523       || !validate_arg (len, INTEGER_TYPE))
11524     return NULL_TREE;
11525   else
11526     {
11527       const char *p = c_getstr (src);
11528
11529       /* If the requested length is zero, or the src parameter string
11530          length is zero, return the dst parameter.  */
11531       if (integer_zerop (len) || (p && *p == '\0'))
11532         return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11533
11534       /* If the requested len is greater than or equal to the string
11535          length, call strcat.  */
11536       if (TREE_CODE (len) == INTEGER_CST && p
11537           && compare_tree_int (len, strlen (p)) >= 0)
11538         {
11539           tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11540
11541           /* If the replacement _DECL isn't initialized, don't do the
11542              transformation.  */
11543           if (!fn)
11544             return NULL_TREE;
11545
11546           return build_call_expr_loc (loc, fn, 2, dst, src);
11547         }
11548       return NULL_TREE;
11549     }
11550 }
11551
11552 /* Simplify a call to the strspn builtin.  S1 and S2 are the arguments
11553    to the call.
11554
11555    Return NULL_TREE if no simplification was possible, otherwise return the
11556    simplified form of the call as a tree.
11557
11558    The simplified form may be a constant or other expression which
11559    computes the same value, but in a more efficient manner (including
11560    calls to other builtin functions).
11561
11562    The call may contain arguments which need to be evaluated, but
11563    which are not useful to determine the result of the call.  In
11564    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11565    COMPOUND_EXPR will be an argument which must be evaluated.
11566    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11567    COMPOUND_EXPR in the chain will contain the tree for the simplified
11568    form of the builtin function call.  */
11569
11570 static tree
11571 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11572 {
11573   if (!validate_arg (s1, POINTER_TYPE)
11574       || !validate_arg (s2, POINTER_TYPE))
11575     return NULL_TREE;
11576   else
11577     {
11578       const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11579
11580       /* If both arguments are constants, evaluate at compile-time.  */
11581       if (p1 && p2)
11582         {
11583           const size_t r = strspn (p1, p2);
11584           return size_int (r);
11585         }
11586
11587       /* If either argument is "", return NULL_TREE.  */
11588       if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11589         /* Evaluate and ignore both arguments in case either one has
11590            side-effects.  */
11591         return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11592                                   s1, s2);
11593       return NULL_TREE;
11594     }
11595 }
11596
11597 /* Simplify a call to the strcspn builtin.  S1 and S2 are the arguments
11598    to the call.
11599
11600    Return NULL_TREE if no simplification was possible, otherwise return the
11601    simplified form of the call as a tree.
11602
11603    The simplified form may be a constant or other expression which
11604    computes the same value, but in a more efficient manner (including
11605    calls to other builtin functions).
11606
11607    The call may contain arguments which need to be evaluated, but
11608    which are not useful to determine the result of the call.  In
11609    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11610    COMPOUND_EXPR will be an argument which must be evaluated.
11611    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11612    COMPOUND_EXPR in the chain will contain the tree for the simplified
11613    form of the builtin function call.  */
11614
11615 static tree
11616 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11617 {
11618   if (!validate_arg (s1, POINTER_TYPE)
11619       || !validate_arg (s2, POINTER_TYPE))
11620     return NULL_TREE;
11621   else
11622     {
11623       const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11624
11625       /* If both arguments are constants, evaluate at compile-time.  */
11626       if (p1 && p2)
11627         {
11628           const size_t r = strcspn (p1, p2);
11629           return size_int (r);
11630         }
11631
11632       /* If the first argument is "", return NULL_TREE.  */
11633       if (p1 && *p1 == '\0')
11634         {
11635           /* Evaluate and ignore argument s2 in case it has
11636              side-effects.  */
11637           return omit_one_operand_loc (loc, size_type_node,
11638                                    size_zero_node, s2);
11639         }
11640
11641       /* If the second argument is "", return __builtin_strlen(s1).  */
11642       if (p2 && *p2 == '\0')
11643         {
11644           tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11645
11646           /* If the replacement _DECL isn't initialized, don't do the
11647              transformation.  */
11648           if (!fn)
11649             return NULL_TREE;
11650
11651           return build_call_expr_loc (loc, fn, 1, s1);
11652         }
11653       return NULL_TREE;
11654     }
11655 }
11656
11657 /* Fold a call to the fputs builtin.  ARG0 and ARG1 are the arguments
11658    to the call.  IGNORE is true if the value returned
11659    by the builtin will be ignored.  UNLOCKED is true is true if this
11660    actually a call to fputs_unlocked.  If LEN in non-NULL, it represents
11661    the known length of the string.  Return NULL_TREE if no simplification
11662    was possible.  */
11663
11664 tree
11665 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11666                     bool ignore, bool unlocked, tree len)
11667 {
11668   /* If we're using an unlocked function, assume the other unlocked
11669      functions exist explicitly.  */
11670   tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11671     : implicit_built_in_decls[BUILT_IN_FPUTC];
11672   tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11673     : implicit_built_in_decls[BUILT_IN_FWRITE];
11674
11675   /* If the return value is used, don't do the transformation.  */
11676   if (!ignore)
11677     return NULL_TREE;
11678
11679   /* Verify the arguments in the original call.  */
11680   if (!validate_arg (arg0, POINTER_TYPE)
11681       || !validate_arg (arg1, POINTER_TYPE))
11682     return NULL_TREE;
11683
11684   if (! len)
11685     len = c_strlen (arg0, 0);
11686
11687   /* Get the length of the string passed to fputs.  If the length
11688      can't be determined, punt.  */
11689   if (!len
11690       || TREE_CODE (len) != INTEGER_CST)
11691     return NULL_TREE;
11692
11693   switch (compare_tree_int (len, 1))
11694     {
11695     case -1: /* length is 0, delete the call entirely .  */
11696       return omit_one_operand_loc (loc, integer_type_node,
11697                                integer_zero_node, arg1);;
11698
11699     case 0: /* length is 1, call fputc.  */
11700       {
11701         const char *p = c_getstr (arg0);
11702
11703         if (p != NULL)
11704           {
11705             if (fn_fputc)
11706               return build_call_expr_loc (loc, fn_fputc, 2,
11707                                       build_int_cst (NULL_TREE, p[0]), arg1);
11708             else
11709               return NULL_TREE;
11710           }
11711       }
11712       /* FALLTHROUGH */
11713     case 1: /* length is greater than 1, call fwrite.  */
11714       {
11715         /* If optimizing for size keep fputs.  */
11716         if (optimize_function_for_size_p (cfun))
11717           return NULL_TREE;
11718         /* New argument list transforming fputs(string, stream) to
11719            fwrite(string, 1, len, stream).  */
11720         if (fn_fwrite)
11721           return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11722                                   size_one_node, len, arg1);
11723         else
11724           return NULL_TREE;
11725       }
11726     default:
11727       gcc_unreachable ();
11728     }
11729   return NULL_TREE;
11730 }
11731
11732 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11733    produced.  False otherwise.  This is done so that we don't output the error
11734    or warning twice or three times.  */
11735
11736 bool
11737 fold_builtin_next_arg (tree exp, bool va_start_p)
11738 {
11739   tree fntype = TREE_TYPE (current_function_decl);
11740   int nargs = call_expr_nargs (exp);
11741   tree arg;
11742
11743   if (!stdarg_p (fntype))
11744     {
11745       error ("%<va_start%> used in function with fixed args");
11746       return true;
11747     }
11748
11749   if (va_start_p)
11750     {
11751       if (va_start_p && (nargs != 2))
11752         {
11753           error ("wrong number of arguments to function %<va_start%>");
11754           return true;
11755         }
11756       arg = CALL_EXPR_ARG (exp, 1);
11757     }
11758   /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11759      when we checked the arguments and if needed issued a warning.  */
11760   else
11761     {
11762       if (nargs == 0)
11763         {
11764           /* Evidently an out of date version of <stdarg.h>; can't validate
11765              va_start's second argument, but can still work as intended.  */
11766           warning (0, "%<__builtin_next_arg%> called without an argument");
11767           return true;
11768         }
11769       else if (nargs > 1)
11770         {
11771           error ("wrong number of arguments to function %<__builtin_next_arg%>");
11772           return true;
11773         }
11774       arg = CALL_EXPR_ARG (exp, 0);
11775     }
11776
11777   if (TREE_CODE (arg) == SSA_NAME)
11778     arg = SSA_NAME_VAR (arg);
11779
11780   /* We destructively modify the call to be __builtin_va_start (ap, 0)
11781      or __builtin_next_arg (0) the first time we see it, after checking
11782      the arguments and if needed issuing a warning.  */
11783   if (!integer_zerop (arg))
11784     {
11785       tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11786
11787       /* Strip off all nops for the sake of the comparison.  This
11788          is not quite the same as STRIP_NOPS.  It does more.
11789          We must also strip off INDIRECT_EXPR for C++ reference
11790          parameters.  */
11791       while (CONVERT_EXPR_P (arg)
11792              || TREE_CODE (arg) == INDIRECT_REF)
11793         arg = TREE_OPERAND (arg, 0);
11794       if (arg != last_parm)
11795         {
11796           /* FIXME: Sometimes with the tree optimizers we can get the
11797              not the last argument even though the user used the last
11798              argument.  We just warn and set the arg to be the last
11799              argument so that we will get wrong-code because of
11800              it.  */
11801           warning (0, "second parameter of %<va_start%> not last named argument");
11802         }
11803
11804       /* Undefined by C99 7.15.1.4p4 (va_start):
11805          "If the parameter parmN is declared with the register storage
11806          class, with a function or array type, or with a type that is
11807          not compatible with the type that results after application of
11808          the default argument promotions, the behavior is undefined."
11809       */
11810       else if (DECL_REGISTER (arg))
11811         warning (0, "undefined behaviour when second parameter of "
11812                  "%<va_start%> is declared with %<register%> storage");
11813
11814       /* We want to verify the second parameter just once before the tree
11815          optimizers are run and then avoid keeping it in the tree,
11816          as otherwise we could warn even for correct code like:
11817          void foo (int i, ...)
11818          { va_list ap; i++; va_start (ap, i); va_end (ap); }  */
11819       if (va_start_p)
11820         CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11821       else
11822         CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11823     }
11824   return false;
11825 }
11826
11827
11828 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11829    ORIG may be null if this is a 2-argument call.  We don't attempt to
11830    simplify calls with more than 3 arguments.
11831
11832    Return NULL_TREE if no simplification was possible, otherwise return the
11833    simplified form of the call as a tree.  If IGNORED is true, it means that
11834    the caller does not use the returned value of the function.  */
11835
11836 static tree
11837 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
11838                       tree orig, int ignored)
11839 {
11840   tree call, retval;
11841   const char *fmt_str = NULL;
11842
11843   /* Verify the required arguments in the original call.  We deal with two
11844      types of sprintf() calls: 'sprintf (str, fmt)' and
11845      'sprintf (dest, "%s", orig)'.  */
11846   if (!validate_arg (dest, POINTER_TYPE)
11847       || !validate_arg (fmt, POINTER_TYPE))
11848     return NULL_TREE;
11849   if (orig && !validate_arg (orig, POINTER_TYPE))
11850     return NULL_TREE;
11851
11852   /* Check whether the format is a literal string constant.  */
11853   fmt_str = c_getstr (fmt);
11854   if (fmt_str == NULL)
11855     return NULL_TREE;
11856
11857   call = NULL_TREE;
11858   retval = NULL_TREE;
11859
11860   if (!init_target_chars ())
11861     return NULL_TREE;
11862
11863   /* If the format doesn't contain % args or %%, use strcpy.  */
11864   if (strchr (fmt_str, target_percent) == NULL)
11865     {
11866       tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11867
11868       if (!fn)
11869         return NULL_TREE;
11870
11871       /* Don't optimize sprintf (buf, "abc", ptr++).  */
11872       if (orig)
11873         return NULL_TREE;
11874
11875       /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
11876          'format' is known to contain no % formats.  */
11877       call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11878       if (!ignored)
11879         retval = build_int_cst (NULL_TREE, strlen (fmt_str));
11880     }
11881
11882   /* If the format is "%s", use strcpy if the result isn't used.  */
11883   else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11884     {
11885       tree fn;
11886       fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11887
11888       if (!fn)
11889         return NULL_TREE;
11890
11891       /* Don't crash on sprintf (str1, "%s").  */
11892       if (!orig)
11893         return NULL_TREE;
11894
11895       /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2).  */
11896       if (!ignored)
11897         {
11898           retval = c_strlen (orig, 1);
11899           if (!retval || TREE_CODE (retval) != INTEGER_CST)
11900             return NULL_TREE;
11901         }
11902       call = build_call_expr_loc (loc, fn, 2, dest, orig);
11903     }
11904
11905   if (call && retval)
11906     {
11907       retval = fold_convert_loc
11908         (loc, TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
11909          retval);
11910       return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11911     }
11912   else
11913     return call;
11914 }
11915
11916 /* Expand a call EXP to __builtin_object_size.  */
11917
11918 rtx
11919 expand_builtin_object_size (tree exp)
11920 {
11921   tree ost;
11922   int object_size_type;
11923   tree fndecl = get_callee_fndecl (exp);
11924
11925   if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11926     {
11927       error ("%Kfirst argument of %D must be a pointer, second integer constant",
11928              exp, fndecl);
11929       expand_builtin_trap ();
11930       return const0_rtx;
11931     }
11932
11933   ost = CALL_EXPR_ARG (exp, 1);
11934   STRIP_NOPS (ost);
11935
11936   if (TREE_CODE (ost) != INTEGER_CST
11937       || tree_int_cst_sgn (ost) < 0
11938       || compare_tree_int (ost, 3) > 0)
11939     {
11940       error ("%Klast argument of %D is not integer constant between 0 and 3",
11941              exp, fndecl);
11942       expand_builtin_trap ();
11943       return const0_rtx;
11944     }
11945
11946   object_size_type = tree_low_cst (ost, 0);
11947
11948   return object_size_type < 2 ? constm1_rtx : const0_rtx;
11949 }
11950
11951 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11952    FCODE is the BUILT_IN_* to use.
11953    Return NULL_RTX if we failed; the caller should emit a normal call,
11954    otherwise try to get the result in TARGET, if convenient (and in
11955    mode MODE if that's convenient).  */
11956
11957 static rtx
11958 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
11959                            enum built_in_function fcode)
11960 {
11961   tree dest, src, len, size;
11962
11963   if (!validate_arglist (exp,
11964                          POINTER_TYPE,
11965                          fcode == BUILT_IN_MEMSET_CHK
11966                          ? INTEGER_TYPE : POINTER_TYPE,
11967                          INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11968     return NULL_RTX;
11969
11970   dest = CALL_EXPR_ARG (exp, 0);
11971   src = CALL_EXPR_ARG (exp, 1);
11972   len = CALL_EXPR_ARG (exp, 2);
11973   size = CALL_EXPR_ARG (exp, 3);
11974
11975   if (! host_integerp (size, 1))
11976     return NULL_RTX;
11977
11978   if (host_integerp (len, 1) || integer_all_onesp (size))
11979     {
11980       tree fn;
11981
11982       if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11983         {
11984           warning_at (tree_nonartificial_location (exp),
11985                       0, "%Kcall to %D will always overflow destination buffer",
11986                       exp, get_callee_fndecl (exp));
11987           return NULL_RTX;
11988         }
11989
11990       fn = NULL_TREE;
11991       /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11992          mem{cpy,pcpy,move,set} is available.  */
11993       switch (fcode)
11994         {
11995         case BUILT_IN_MEMCPY_CHK:
11996           fn = built_in_decls[BUILT_IN_MEMCPY];
11997           break;
11998         case BUILT_IN_MEMPCPY_CHK:
11999           fn = built_in_decls[BUILT_IN_MEMPCPY];
12000           break;
12001         case BUILT_IN_MEMMOVE_CHK:
12002           fn = built_in_decls[BUILT_IN_MEMMOVE];
12003           break;
12004         case BUILT_IN_MEMSET_CHK:
12005           fn = built_in_decls[BUILT_IN_MEMSET];
12006           break;
12007         default:
12008           break;
12009         }
12010
12011       if (! fn)
12012         return NULL_RTX;
12013
12014       fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12015       gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12016       CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12017       return expand_expr (fn, target, mode, EXPAND_NORMAL);
12018     }
12019   else if (fcode == BUILT_IN_MEMSET_CHK)
12020     return NULL_RTX;
12021   else
12022     {
12023       unsigned int dest_align
12024         = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
12025
12026       /* If DEST is not a pointer type, call the normal function.  */
12027       if (dest_align == 0)
12028         return NULL_RTX;
12029
12030       /* If SRC and DEST are the same (and not volatile), do nothing.  */
12031       if (operand_equal_p (src, dest, 0))
12032         {
12033           tree expr;
12034
12035           if (fcode != BUILT_IN_MEMPCPY_CHK)
12036             {
12037               /* Evaluate and ignore LEN in case it has side-effects.  */
12038               expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12039               return expand_expr (dest, target, mode, EXPAND_NORMAL);
12040             }
12041
12042           expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
12043           return expand_expr (expr, target, mode, EXPAND_NORMAL);
12044         }
12045
12046       /* __memmove_chk special case.  */
12047       if (fcode == BUILT_IN_MEMMOVE_CHK)
12048         {
12049           unsigned int src_align
12050             = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
12051
12052           if (src_align == 0)
12053             return NULL_RTX;
12054
12055           /* If src is categorized for a readonly section we can use
12056              normal __memcpy_chk.  */
12057           if (readonly_data_expr (src))
12058             {
12059               tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12060               if (!fn)
12061                 return NULL_RTX;
12062               fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12063                                           dest, src, len, size);
12064               gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12065               CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12066               return expand_expr (fn, target, mode, EXPAND_NORMAL);
12067             }
12068         }
12069       return NULL_RTX;
12070     }
12071 }
12072
12073 /* Emit warning if a buffer overflow is detected at compile time.  */
12074
12075 static void
12076 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12077 {
12078   int is_strlen = 0;
12079   tree len, size;
12080   location_t loc = tree_nonartificial_location (exp);
12081
12082   switch (fcode)
12083     {
12084     case BUILT_IN_STRCPY_CHK:
12085     case BUILT_IN_STPCPY_CHK:
12086     /* For __strcat_chk the warning will be emitted only if overflowing
12087        by at least strlen (dest) + 1 bytes.  */
12088     case BUILT_IN_STRCAT_CHK:
12089       len = CALL_EXPR_ARG (exp, 1);
12090       size = CALL_EXPR_ARG (exp, 2);
12091       is_strlen = 1;
12092       break;
12093     case BUILT_IN_STRNCAT_CHK:
12094     case BUILT_IN_STRNCPY_CHK:
12095       len = CALL_EXPR_ARG (exp, 2);
12096       size = CALL_EXPR_ARG (exp, 3);
12097       break;
12098     case BUILT_IN_SNPRINTF_CHK:
12099     case BUILT_IN_VSNPRINTF_CHK:
12100       len = CALL_EXPR_ARG (exp, 1);
12101       size = CALL_EXPR_ARG (exp, 3);
12102       break;
12103     default:
12104       gcc_unreachable ();
12105     }
12106
12107   if (!len || !size)
12108     return;
12109
12110   if (! host_integerp (size, 1) || integer_all_onesp (size))
12111     return;
12112
12113   if (is_strlen)
12114     {
12115       len = c_strlen (len, 1);
12116       if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12117         return;
12118     }
12119   else if (fcode == BUILT_IN_STRNCAT_CHK)
12120     {
12121       tree src = CALL_EXPR_ARG (exp, 1);
12122       if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12123         return;
12124       src = c_strlen (src, 1);
12125       if (! src || ! host_integerp (src, 1))
12126         {
12127           warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12128                       exp, get_callee_fndecl (exp));
12129           return;
12130         }
12131       else if (tree_int_cst_lt (src, size))
12132         return;
12133     }
12134   else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12135     return;
12136
12137   warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12138               exp, get_callee_fndecl (exp));
12139 }
12140
12141 /* Emit warning if a buffer overflow is detected at compile time
12142    in __sprintf_chk/__vsprintf_chk calls.  */
12143
12144 static void
12145 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12146 {
12147   tree size, len, fmt;
12148   const char *fmt_str;
12149   int nargs = call_expr_nargs (exp);
12150
12151   /* Verify the required arguments in the original call.  */
12152
12153   if (nargs < 4)
12154     return;
12155   size = CALL_EXPR_ARG (exp, 2);
12156   fmt = CALL_EXPR_ARG (exp, 3);
12157
12158   if (! host_integerp (size, 1) || integer_all_onesp (size))
12159     return;
12160
12161   /* Check whether the format is a literal string constant.  */
12162   fmt_str = c_getstr (fmt);
12163   if (fmt_str == NULL)
12164     return;
12165
12166   if (!init_target_chars ())
12167     return;
12168
12169   /* If the format doesn't contain % args or %%, we know its size.  */
12170   if (strchr (fmt_str, target_percent) == 0)
12171     len = build_int_cstu (size_type_node, strlen (fmt_str));
12172   /* If the format is "%s" and first ... argument is a string literal,
12173      we know it too.  */
12174   else if (fcode == BUILT_IN_SPRINTF_CHK
12175            && strcmp (fmt_str, target_percent_s) == 0)
12176     {
12177       tree arg;
12178
12179       if (nargs < 5)
12180         return;
12181       arg = CALL_EXPR_ARG (exp, 4);
12182       if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12183         return;
12184
12185       len = c_strlen (arg, 1);
12186       if (!len || ! host_integerp (len, 1))
12187         return;
12188     }
12189   else
12190     return;
12191
12192   if (! tree_int_cst_lt (len, size))
12193     warning_at (tree_nonartificial_location (exp),
12194                 0, "%Kcall to %D will always overflow destination buffer",
12195                 exp, get_callee_fndecl (exp));
12196 }
12197
12198 /* Emit warning if a free is called with address of a variable.  */
12199
12200 static void
12201 maybe_emit_free_warning (tree exp)
12202 {
12203   tree arg = CALL_EXPR_ARG (exp, 0);
12204
12205   STRIP_NOPS (arg);
12206   if (TREE_CODE (arg) != ADDR_EXPR)
12207     return;
12208
12209   arg = get_base_address (TREE_OPERAND (arg, 0));
12210   if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12211     return;
12212
12213   if (SSA_VAR_P (arg))
12214     warning_at (tree_nonartificial_location (exp),
12215                 0, "%Kattempt to free a non-heap object %qD", exp, arg);
12216   else
12217     warning_at (tree_nonartificial_location (exp),
12218                 0, "%Kattempt to free a non-heap object", exp);
12219 }
12220
12221 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12222    if possible.  */
12223
12224 tree
12225 fold_builtin_object_size (tree ptr, tree ost)
12226 {
12227   unsigned HOST_WIDE_INT bytes;
12228   int object_size_type;
12229
12230   if (!validate_arg (ptr, POINTER_TYPE)
12231       || !validate_arg (ost, INTEGER_TYPE))
12232     return NULL_TREE;
12233
12234   STRIP_NOPS (ost);
12235
12236   if (TREE_CODE (ost) != INTEGER_CST
12237       || tree_int_cst_sgn (ost) < 0
12238       || compare_tree_int (ost, 3) > 0)
12239     return NULL_TREE;
12240
12241   object_size_type = tree_low_cst (ost, 0);
12242
12243   /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12244      if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12245      and (size_t) 0 for types 2 and 3.  */
12246   if (TREE_SIDE_EFFECTS (ptr))
12247     return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12248
12249   if (TREE_CODE (ptr) == ADDR_EXPR)
12250     {
12251       bytes = compute_builtin_object_size (ptr, object_size_type);
12252       if (double_int_fits_to_tree_p (size_type_node,
12253                                      uhwi_to_double_int (bytes)))
12254         return build_int_cstu (size_type_node, bytes);
12255     }
12256   else if (TREE_CODE (ptr) == SSA_NAME)
12257     {
12258       /* If object size is not known yet, delay folding until
12259        later.  Maybe subsequent passes will help determining
12260        it.  */
12261       bytes = compute_builtin_object_size (ptr, object_size_type);
12262       if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12263           && double_int_fits_to_tree_p (size_type_node,
12264                                         uhwi_to_double_int (bytes)))
12265         return build_int_cstu (size_type_node, bytes);
12266     }
12267
12268   return NULL_TREE;
12269 }
12270
12271 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12272    DEST, SRC, LEN, and SIZE are the arguments to the call.
12273    IGNORE is true, if return value can be ignored.  FCODE is the BUILT_IN_*
12274    code of the builtin.  If MAXLEN is not NULL, it is maximum length
12275    passed as third argument.  */
12276
12277 tree
12278 fold_builtin_memory_chk (location_t loc, tree fndecl,
12279                          tree dest, tree src, tree len, tree size,
12280                          tree maxlen, bool ignore,
12281                          enum built_in_function fcode)
12282 {
12283   tree fn;
12284
12285   if (!validate_arg (dest, POINTER_TYPE)
12286       || !validate_arg (src,
12287                         (fcode == BUILT_IN_MEMSET_CHK
12288                          ? INTEGER_TYPE : POINTER_TYPE))
12289       || !validate_arg (len, INTEGER_TYPE)
12290       || !validate_arg (size, INTEGER_TYPE))
12291     return NULL_TREE;
12292
12293   /* If SRC and DEST are the same (and not volatile), return DEST
12294      (resp. DEST+LEN for __mempcpy_chk).  */
12295   if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12296     {
12297       if (fcode != BUILT_IN_MEMPCPY_CHK)
12298         return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12299                                  dest, len);
12300       else
12301         {
12302           tree temp = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dest),
12303                                    dest, len);
12304           return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12305         }
12306     }
12307
12308   if (! host_integerp (size, 1))
12309     return NULL_TREE;
12310
12311   if (! integer_all_onesp (size))
12312     {
12313       if (! host_integerp (len, 1))
12314         {
12315           /* If LEN is not constant, try MAXLEN too.
12316              For MAXLEN only allow optimizing into non-_ocs function
12317              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
12318           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12319             {
12320               if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12321                 {
12322                   /* (void) __mempcpy_chk () can be optimized into
12323                      (void) __memcpy_chk ().  */
12324                   fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12325                   if (!fn)
12326                     return NULL_TREE;
12327
12328                   return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12329                 }
12330               return NULL_TREE;
12331             }
12332         }
12333       else
12334         maxlen = len;
12335
12336       if (tree_int_cst_lt (size, maxlen))
12337         return NULL_TREE;
12338     }
12339
12340   fn = NULL_TREE;
12341   /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12342      mem{cpy,pcpy,move,set} is available.  */
12343   switch (fcode)
12344     {
12345     case BUILT_IN_MEMCPY_CHK:
12346       fn = built_in_decls[BUILT_IN_MEMCPY];
12347       break;
12348     case BUILT_IN_MEMPCPY_CHK:
12349       fn = built_in_decls[BUILT_IN_MEMPCPY];
12350       break;
12351     case BUILT_IN_MEMMOVE_CHK:
12352       fn = built_in_decls[BUILT_IN_MEMMOVE];
12353       break;
12354     case BUILT_IN_MEMSET_CHK:
12355       fn = built_in_decls[BUILT_IN_MEMSET];
12356       break;
12357     default:
12358       break;
12359     }
12360
12361   if (!fn)
12362     return NULL_TREE;
12363
12364   return build_call_expr_loc (loc, fn, 3, dest, src, len);
12365 }
12366
12367 /* Fold a call to the __st[rp]cpy_chk builtin.
12368    DEST, SRC, and SIZE are the arguments to the call.
12369    IGNORE is true if return value can be ignored.  FCODE is the BUILT_IN_*
12370    code of the builtin.  If MAXLEN is not NULL, it is maximum length of
12371    strings passed as second argument.  */
12372
12373 tree
12374 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12375                          tree src, tree size,
12376                          tree maxlen, bool ignore,
12377                          enum built_in_function fcode)
12378 {
12379   tree len, fn;
12380
12381   if (!validate_arg (dest, POINTER_TYPE)
12382       || !validate_arg (src, POINTER_TYPE)
12383       || !validate_arg (size, INTEGER_TYPE))
12384     return NULL_TREE;
12385
12386   /* If SRC and DEST are the same (and not volatile), return DEST.  */
12387   if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12388     return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12389
12390   if (! host_integerp (size, 1))
12391     return NULL_TREE;
12392
12393   if (! integer_all_onesp (size))
12394     {
12395       len = c_strlen (src, 1);
12396       if (! len || ! host_integerp (len, 1))
12397         {
12398           /* If LEN is not constant, try MAXLEN too.
12399              For MAXLEN only allow optimizing into non-_ocs function
12400              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
12401           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12402             {
12403               if (fcode == BUILT_IN_STPCPY_CHK)
12404                 {
12405                   if (! ignore)
12406                     return NULL_TREE;
12407
12408                   /* If return value of __stpcpy_chk is ignored,
12409                      optimize into __strcpy_chk.  */
12410                   fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12411                   if (!fn)
12412                     return NULL_TREE;
12413
12414                   return build_call_expr_loc (loc, fn, 3, dest, src, size);
12415                 }
12416
12417               if (! len || TREE_SIDE_EFFECTS (len))
12418                 return NULL_TREE;
12419
12420               /* If c_strlen returned something, but not a constant,
12421                  transform __strcpy_chk into __memcpy_chk.  */
12422               fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12423               if (!fn)
12424                 return NULL_TREE;
12425
12426               len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
12427               return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12428                                        build_call_expr_loc (loc, fn, 4,
12429                                                         dest, src, len, size));
12430             }
12431         }
12432       else
12433         maxlen = len;
12434
12435       if (! tree_int_cst_lt (maxlen, size))
12436         return NULL_TREE;
12437     }
12438
12439   /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available.  */
12440   fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12441                       ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12442   if (!fn)
12443     return NULL_TREE;
12444
12445   return build_call_expr_loc (loc, fn, 2, dest, src);
12446 }
12447
12448 /* Fold a call to the __strncpy_chk builtin.  DEST, SRC, LEN, and SIZE
12449    are the arguments to the call.  If MAXLEN is not NULL, it is maximum
12450    length passed as third argument.  */
12451
12452 tree
12453 fold_builtin_strncpy_chk (location_t loc, tree dest, tree src,
12454                           tree len, tree size, tree maxlen)
12455 {
12456   tree fn;
12457
12458   if (!validate_arg (dest, POINTER_TYPE)
12459       || !validate_arg (src, POINTER_TYPE)
12460       || !validate_arg (len, INTEGER_TYPE)
12461       || !validate_arg (size, INTEGER_TYPE))
12462     return NULL_TREE;
12463
12464   if (! host_integerp (size, 1))
12465     return NULL_TREE;
12466
12467   if (! integer_all_onesp (size))
12468     {
12469       if (! host_integerp (len, 1))
12470         {
12471           /* If LEN is not constant, try MAXLEN too.
12472              For MAXLEN only allow optimizing into non-_ocs function
12473              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
12474           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12475             return NULL_TREE;
12476         }
12477       else
12478         maxlen = len;
12479
12480       if (tree_int_cst_lt (size, maxlen))
12481         return NULL_TREE;
12482     }
12483
12484   /* If __builtin_strncpy_chk is used, assume strncpy is available.  */
12485   fn = built_in_decls[BUILT_IN_STRNCPY];
12486   if (!fn)
12487     return NULL_TREE;
12488
12489   return build_call_expr_loc (loc, fn, 3, dest, src, len);
12490 }
12491
12492 /* Fold a call to the __strcat_chk builtin FNDECL.  DEST, SRC, and SIZE
12493    are the arguments to the call.  */
12494
12495 static tree
12496 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12497                          tree src, tree size)
12498 {
12499   tree fn;
12500   const char *p;
12501
12502   if (!validate_arg (dest, POINTER_TYPE)
12503       || !validate_arg (src, POINTER_TYPE)
12504       || !validate_arg (size, INTEGER_TYPE))
12505     return NULL_TREE;
12506
12507   p = c_getstr (src);
12508   /* If the SRC parameter is "", return DEST.  */
12509   if (p && *p == '\0')
12510     return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12511
12512   if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12513     return NULL_TREE;
12514
12515   /* If __builtin_strcat_chk is used, assume strcat is available.  */
12516   fn = built_in_decls[BUILT_IN_STRCAT];
12517   if (!fn)
12518     return NULL_TREE;
12519
12520   return build_call_expr_loc (loc, fn, 2, dest, src);
12521 }
12522
12523 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12524    LEN, and SIZE.  */
12525
12526 static tree
12527 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12528                           tree dest, tree src, tree len, tree size)
12529 {
12530   tree fn;
12531   const char *p;
12532
12533   if (!validate_arg (dest, POINTER_TYPE)
12534       || !validate_arg (src, POINTER_TYPE)
12535       || !validate_arg (size, INTEGER_TYPE)
12536       || !validate_arg (size, INTEGER_TYPE))
12537     return NULL_TREE;
12538
12539   p = c_getstr (src);
12540   /* If the SRC parameter is "" or if LEN is 0, return DEST.  */
12541   if (p && *p == '\0')
12542     return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12543   else if (integer_zerop (len))
12544     return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12545
12546   if (! host_integerp (size, 1))
12547     return NULL_TREE;
12548
12549   if (! integer_all_onesp (size))
12550     {
12551       tree src_len = c_strlen (src, 1);
12552       if (src_len
12553           && host_integerp (src_len, 1)
12554           && host_integerp (len, 1)
12555           && ! tree_int_cst_lt (len, src_len))
12556         {
12557           /* If LEN >= strlen (SRC), optimize into __strcat_chk.  */
12558           fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12559           if (!fn)
12560             return NULL_TREE;
12561
12562           return build_call_expr_loc (loc, fn, 3, dest, src, size);
12563         }
12564       return NULL_TREE;
12565     }
12566
12567   /* If __builtin_strncat_chk is used, assume strncat is available.  */
12568   fn = built_in_decls[BUILT_IN_STRNCAT];
12569   if (!fn)
12570     return NULL_TREE;
12571
12572   return build_call_expr_loc (loc, fn, 3, dest, src, len);
12573 }
12574
12575 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12576    Return NULL_TREE if a normal call should be emitted rather than
12577    expanding the function inline.  FCODE is either BUILT_IN_SPRINTF_CHK
12578    or BUILT_IN_VSPRINTF_CHK.  */
12579
12580 static tree
12581 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
12582                             enum built_in_function fcode)
12583 {
12584   tree dest, size, len, fn, fmt, flag;
12585   const char *fmt_str;
12586
12587   /* Verify the required arguments in the original call.  */
12588   if (nargs < 4)
12589     return NULL_TREE;
12590   dest = args[0];
12591   if (!validate_arg (dest, POINTER_TYPE))
12592     return NULL_TREE;
12593   flag = args[1];
12594   if (!validate_arg (flag, INTEGER_TYPE))
12595     return NULL_TREE;
12596   size = args[2];
12597   if (!validate_arg (size, INTEGER_TYPE))
12598     return NULL_TREE;
12599   fmt = args[3];
12600   if (!validate_arg (fmt, POINTER_TYPE))
12601     return NULL_TREE;
12602
12603   if (! host_integerp (size, 1))
12604     return NULL_TREE;
12605
12606   len = NULL_TREE;
12607
12608   if (!init_target_chars ())
12609     return NULL_TREE;
12610
12611   /* Check whether the format is a literal string constant.  */
12612   fmt_str = c_getstr (fmt);
12613   if (fmt_str != NULL)
12614     {
12615       /* If the format doesn't contain % args or %%, we know the size.  */
12616       if (strchr (fmt_str, target_percent) == 0)
12617         {
12618           if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12619             len = build_int_cstu (size_type_node, strlen (fmt_str));
12620         }
12621       /* If the format is "%s" and first ... argument is a string literal,
12622          we know the size too.  */
12623       else if (fcode == BUILT_IN_SPRINTF_CHK
12624                && strcmp (fmt_str, target_percent_s) == 0)
12625         {
12626           tree arg;
12627
12628           if (nargs == 5)
12629             {
12630               arg = args[4];
12631               if (validate_arg (arg, POINTER_TYPE))
12632                 {
12633                   len = c_strlen (arg, 1);
12634                   if (! len || ! host_integerp (len, 1))
12635                     len = NULL_TREE;
12636                 }
12637             }
12638         }
12639     }
12640
12641   if (! integer_all_onesp (size))
12642     {
12643       if (! len || ! tree_int_cst_lt (len, size))
12644         return NULL_TREE;
12645     }
12646
12647   /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12648      or if format doesn't contain % chars or is "%s".  */
12649   if (! integer_zerop (flag))
12650     {
12651       if (fmt_str == NULL)
12652         return NULL_TREE;
12653       if (strchr (fmt_str, target_percent) != NULL
12654           && strcmp (fmt_str, target_percent_s))
12655         return NULL_TREE;
12656     }
12657
12658   /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available.  */
12659   fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12660                       ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12661   if (!fn)
12662     return NULL_TREE;
12663
12664   return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
12665 }
12666
12667 /* Fold a call EXP to __{,v}sprintf_chk.  Return NULL_TREE if
12668    a normal call should be emitted rather than expanding the function
12669    inline.  FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK.  */
12670
12671 static tree
12672 fold_builtin_sprintf_chk (location_t loc, tree exp,
12673                           enum built_in_function fcode)
12674 {
12675   return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
12676                                      CALL_EXPR_ARGP (exp), fcode);
12677 }
12678
12679 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS.  Return
12680    NULL_TREE if a normal call should be emitted rather than expanding
12681    the function inline.  FCODE is either BUILT_IN_SNPRINTF_CHK or
12682    BUILT_IN_VSNPRINTF_CHK.  If MAXLEN is not NULL, it is maximum length
12683    passed as second argument.  */
12684
12685 static tree
12686 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
12687                              tree maxlen, enum built_in_function fcode)
12688 {
12689   tree dest, size, len, fn, fmt, flag;
12690   const char *fmt_str;
12691
12692   /* Verify the required arguments in the original call.  */
12693   if (nargs < 5)
12694     return NULL_TREE;
12695   dest = args[0];
12696   if (!validate_arg (dest, POINTER_TYPE))
12697     return NULL_TREE;
12698   len = args[1];
12699   if (!validate_arg (len, INTEGER_TYPE))
12700     return NULL_TREE;
12701   flag = args[2];
12702   if (!validate_arg (flag, INTEGER_TYPE))
12703     return NULL_TREE;
12704   size = args[3];
12705   if (!validate_arg (size, INTEGER_TYPE))
12706     return NULL_TREE;
12707   fmt = args[4];
12708   if (!validate_arg (fmt, POINTER_TYPE))
12709     return NULL_TREE;
12710
12711   if (! host_integerp (size, 1))
12712     return NULL_TREE;
12713
12714   if (! integer_all_onesp (size))
12715     {
12716       if (! host_integerp (len, 1))
12717         {
12718           /* If LEN is not constant, try MAXLEN too.
12719              For MAXLEN only allow optimizing into non-_ocs function
12720              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
12721           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12722             return NULL_TREE;
12723         }
12724       else
12725         maxlen = len;
12726
12727       if (tree_int_cst_lt (size, maxlen))
12728         return NULL_TREE;
12729     }
12730
12731   if (!init_target_chars ())
12732     return NULL_TREE;
12733
12734   /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12735      or if format doesn't contain % chars or is "%s".  */
12736   if (! integer_zerop (flag))
12737     {
12738       fmt_str = c_getstr (fmt);
12739       if (fmt_str == NULL)
12740         return NULL_TREE;
12741       if (strchr (fmt_str, target_percent) != NULL
12742           && strcmp (fmt_str, target_percent_s))
12743         return NULL_TREE;
12744     }
12745
12746   /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12747      available.  */
12748   fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12749                       ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12750   if (!fn)
12751     return NULL_TREE;
12752
12753   return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
12754 }
12755
12756 /* Fold a call EXP to {,v}snprintf.  Return NULL_TREE if
12757    a normal call should be emitted rather than expanding the function
12758    inline.  FCODE is either BUILT_IN_SNPRINTF_CHK or
12759    BUILT_IN_VSNPRINTF_CHK.  If MAXLEN is not NULL, it is maximum length
12760    passed as second argument.  */
12761
12762 tree
12763 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
12764                            enum built_in_function fcode)
12765 {
12766   return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
12767                                       CALL_EXPR_ARGP (exp), maxlen, fcode);
12768 }
12769
12770 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12771    FMT and ARG are the arguments to the call; we don't fold cases with
12772    more than 2 arguments, and ARG may be null if this is a 1-argument case.
12773
12774    Return NULL_TREE if no simplification was possible, otherwise return the
12775    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
12776    code of the function to be simplified.  */
12777
12778 static tree
12779 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
12780                      tree arg, bool ignore,
12781                      enum built_in_function fcode)
12782 {
12783   tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12784   const char *fmt_str = NULL;
12785
12786   /* If the return value is used, don't do the transformation.  */
12787   if (! ignore)
12788     return NULL_TREE;
12789
12790   /* Verify the required arguments in the original call.  */
12791   if (!validate_arg (fmt, POINTER_TYPE))
12792     return NULL_TREE;
12793
12794   /* Check whether the format is a literal string constant.  */
12795   fmt_str = c_getstr (fmt);
12796   if (fmt_str == NULL)
12797     return NULL_TREE;
12798
12799   if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12800     {
12801       /* If we're using an unlocked function, assume the other
12802          unlocked functions exist explicitly.  */
12803       fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12804       fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12805     }
12806   else
12807     {
12808       fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12809       fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12810     }
12811
12812   if (!init_target_chars ())
12813     return NULL_TREE;
12814
12815   if (strcmp (fmt_str, target_percent_s) == 0
12816       || strchr (fmt_str, target_percent) == NULL)
12817     {
12818       const char *str;
12819
12820       if (strcmp (fmt_str, target_percent_s) == 0)
12821         {
12822           if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12823             return NULL_TREE;
12824
12825           if (!arg || !validate_arg (arg, POINTER_TYPE))
12826             return NULL_TREE;
12827
12828           str = c_getstr (arg);
12829           if (str == NULL)
12830             return NULL_TREE;
12831         }
12832       else
12833         {
12834           /* The format specifier doesn't contain any '%' characters.  */
12835           if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12836               && arg)
12837             return NULL_TREE;
12838           str = fmt_str;
12839         }
12840
12841       /* If the string was "", printf does nothing.  */
12842       if (str[0] == '\0')
12843         return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12844
12845       /* If the string has length of 1, call putchar.  */
12846       if (str[1] == '\0')
12847         {
12848           /* Given printf("c"), (where c is any one character,)
12849              convert "c"[0] to an int and pass that to the replacement
12850              function.  */
12851           newarg = build_int_cst (NULL_TREE, str[0]);
12852           if (fn_putchar)
12853             call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
12854         }
12855       else
12856         {
12857           /* If the string was "string\n", call puts("string").  */
12858           size_t len = strlen (str);
12859           if ((unsigned char)str[len - 1] == target_newline)
12860             {
12861               /* Create a NUL-terminated string that's one char shorter
12862                  than the original, stripping off the trailing '\n'.  */
12863               char *newstr = XALLOCAVEC (char, len);
12864               memcpy (newstr, str, len - 1);
12865               newstr[len - 1] = 0;
12866
12867               newarg = build_string_literal (len, newstr);
12868               if (fn_puts)
12869                 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
12870             }
12871           else
12872             /* We'd like to arrange to call fputs(string,stdout) here,
12873                but we need stdout and don't have a way to get it yet.  */
12874             return NULL_TREE;
12875         }
12876     }
12877
12878   /* The other optimizations can be done only on the non-va_list variants.  */
12879   else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12880     return NULL_TREE;
12881
12882   /* If the format specifier was "%s\n", call __builtin_puts(arg).  */
12883   else if (strcmp (fmt_str, target_percent_s_newline) == 0)
12884     {
12885       if (!arg || !validate_arg (arg, POINTER_TYPE))
12886         return NULL_TREE;
12887       if (fn_puts)
12888         call = build_call_expr_loc (loc, fn_puts, 1, arg);
12889     }
12890
12891   /* If the format specifier was "%c", call __builtin_putchar(arg).  */
12892   else if (strcmp (fmt_str, target_percent_c) == 0)
12893     {
12894       if (!arg || !validate_arg (arg, INTEGER_TYPE))
12895         return NULL_TREE;
12896       if (fn_putchar)
12897         call = build_call_expr_loc (loc, fn_putchar, 1, arg);
12898     }
12899
12900   if (!call)
12901     return NULL_TREE;
12902
12903   return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
12904 }
12905
12906 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
12907    FP, FMT, and ARG are the arguments to the call.  We don't fold calls with
12908    more than 3 arguments, and ARG may be null in the 2-argument case.
12909
12910    Return NULL_TREE if no simplification was possible, otherwise return the
12911    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
12912    code of the function to be simplified.  */
12913
12914 static tree
12915 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
12916                       tree fmt, tree arg, bool ignore,
12917                       enum built_in_function fcode)
12918 {
12919   tree fn_fputc, fn_fputs, call = NULL_TREE;
12920   const char *fmt_str = NULL;
12921
12922   /* If the return value is used, don't do the transformation.  */
12923   if (! ignore)
12924     return NULL_TREE;
12925
12926   /* Verify the required arguments in the original call.  */
12927   if (!validate_arg (fp, POINTER_TYPE))
12928     return NULL_TREE;
12929   if (!validate_arg (fmt, POINTER_TYPE))
12930     return NULL_TREE;
12931
12932   /* Check whether the format is a literal string constant.  */
12933   fmt_str = c_getstr (fmt);
12934   if (fmt_str == NULL)
12935     return NULL_TREE;
12936
12937   if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
12938     {
12939       /* If we're using an unlocked function, assume the other
12940          unlocked functions exist explicitly.  */
12941       fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
12942       fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
12943     }
12944   else
12945     {
12946       fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
12947       fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
12948     }
12949
12950   if (!init_target_chars ())
12951     return NULL_TREE;
12952
12953   /* If the format doesn't contain % args or %%, use strcpy.  */
12954   if (strchr (fmt_str, target_percent) == NULL)
12955     {
12956       if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
12957           && arg)
12958         return NULL_TREE;
12959
12960       /* If the format specifier was "", fprintf does nothing.  */
12961       if (fmt_str[0] == '\0')
12962         {
12963           /* If FP has side-effects, just wait until gimplification is
12964              done.  */
12965           if (TREE_SIDE_EFFECTS (fp))
12966             return NULL_TREE;
12967
12968           return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12969         }
12970
12971       /* When "string" doesn't contain %, replace all cases of
12972          fprintf (fp, string) with fputs (string, fp).  The fputs
12973          builtin will take care of special cases like length == 1.  */
12974       if (fn_fputs)
12975         call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
12976     }
12977
12978   /* The other optimizations can be done only on the non-va_list variants.  */
12979   else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
12980     return NULL_TREE;
12981
12982   /* If the format specifier was "%s", call __builtin_fputs (arg, fp).  */
12983   else if (strcmp (fmt_str, target_percent_s) == 0)
12984     {
12985       if (!arg || !validate_arg (arg, POINTER_TYPE))
12986         return NULL_TREE;
12987       if (fn_fputs)
12988         call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
12989     }
12990
12991   /* If the format specifier was "%c", call __builtin_fputc (arg, fp).  */
12992   else if (strcmp (fmt_str, target_percent_c) == 0)
12993     {
12994       if (!arg || !validate_arg (arg, INTEGER_TYPE))
12995         return NULL_TREE;
12996       if (fn_fputc)
12997         call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
12998     }
12999
13000   if (!call)
13001     return NULL_TREE;
13002   return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13003 }
13004
13005 /* Initialize format string characters in the target charset.  */
13006
13007 static bool
13008 init_target_chars (void)
13009 {
13010   static bool init;
13011   if (!init)
13012     {
13013       target_newline = lang_hooks.to_target_charset ('\n');
13014       target_percent = lang_hooks.to_target_charset ('%');
13015       target_c = lang_hooks.to_target_charset ('c');
13016       target_s = lang_hooks.to_target_charset ('s');
13017       if (target_newline == 0 || target_percent == 0 || target_c == 0
13018           || target_s == 0)
13019         return false;
13020
13021       target_percent_c[0] = target_percent;
13022       target_percent_c[1] = target_c;
13023       target_percent_c[2] = '\0';
13024
13025       target_percent_s[0] = target_percent;
13026       target_percent_s[1] = target_s;
13027       target_percent_s[2] = '\0';
13028
13029       target_percent_s_newline[0] = target_percent;
13030       target_percent_s_newline[1] = target_s;
13031       target_percent_s_newline[2] = target_newline;
13032       target_percent_s_newline[3] = '\0';
13033
13034       init = true;
13035     }
13036   return true;
13037 }
13038
13039 /* Helper function for do_mpfr_arg*().  Ensure M is a normal number
13040    and no overflow/underflow occurred.  INEXACT is true if M was not
13041    exactly calculated.  TYPE is the tree type for the result.  This
13042    function assumes that you cleared the MPFR flags and then
13043    calculated M to see if anything subsequently set a flag prior to
13044    entering this function.  Return NULL_TREE if any checks fail.  */
13045
13046 static tree
13047 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13048 {
13049   /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13050      overflow/underflow occurred.  If -frounding-math, proceed iff the
13051      result of calling FUNC was exact.  */
13052   if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13053       && (!flag_rounding_math || !inexact))
13054     {
13055       REAL_VALUE_TYPE rr;
13056
13057       real_from_mpfr (&rr, m, type, GMP_RNDN);
13058       /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13059          check for overflow/underflow.  If the REAL_VALUE_TYPE is zero
13060          but the mpft_t is not, then we underflowed in the
13061          conversion.  */
13062       if (real_isfinite (&rr)
13063           && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13064         {
13065           REAL_VALUE_TYPE rmode;
13066
13067           real_convert (&rmode, TYPE_MODE (type), &rr);
13068           /* Proceed iff the specified mode can hold the value.  */
13069           if (real_identical (&rmode, &rr))
13070             return build_real (type, rmode);
13071         }
13072     }
13073   return NULL_TREE;
13074 }
13075
13076 /* Helper function for do_mpc_arg*().  Ensure M is a normal complex
13077    number and no overflow/underflow occurred.  INEXACT is true if M
13078    was not exactly calculated.  TYPE is the tree type for the result.
13079    This function assumes that you cleared the MPFR flags and then
13080    calculated M to see if anything subsequently set a flag prior to
13081    entering this function.  Return NULL_TREE if any checks fail, if
13082    FORCE_CONVERT is true, then bypass the checks.  */
13083
13084 static tree
13085 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13086 {
13087   /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13088      overflow/underflow occurred.  If -frounding-math, proceed iff the
13089      result of calling FUNC was exact.  */
13090   if (force_convert
13091       || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13092           && !mpfr_overflow_p () && !mpfr_underflow_p ()
13093           && (!flag_rounding_math || !inexact)))
13094     {
13095       REAL_VALUE_TYPE re, im;
13096
13097       real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13098       real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13099       /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13100          check for overflow/underflow.  If the REAL_VALUE_TYPE is zero
13101          but the mpft_t is not, then we underflowed in the
13102          conversion.  */
13103       if (force_convert
13104           || (real_isfinite (&re) && real_isfinite (&im)
13105               && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13106               && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13107         {
13108           REAL_VALUE_TYPE re_mode, im_mode;
13109
13110           real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13111           real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13112           /* Proceed iff the specified mode can hold the value.  */
13113           if (force_convert
13114               || (real_identical (&re_mode, &re)
13115                   && real_identical (&im_mode, &im)))
13116             return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13117                                   build_real (TREE_TYPE (type), im_mode));
13118         }
13119     }
13120   return NULL_TREE;
13121 }
13122
13123 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13124    FUNC on it and return the resulting value as a tree with type TYPE.
13125    If MIN and/or MAX are not NULL, then the supplied ARG must be
13126    within those bounds.  If INCLUSIVE is true, then MIN/MAX are
13127    acceptable values, otherwise they are not.  The mpfr precision is
13128    set to the precision of TYPE.  We assume that function FUNC returns
13129    zero if the result could be calculated exactly within the requested
13130    precision.  */
13131
13132 static tree
13133 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13134               const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13135               bool inclusive)
13136 {
13137   tree result = NULL_TREE;
13138
13139   STRIP_NOPS (arg);
13140
13141   /* To proceed, MPFR must exactly represent the target floating point
13142      format, which only happens when the target base equals two.  */
13143   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13144       && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13145     {
13146       const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13147
13148       if (real_isfinite (ra)
13149           && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13150           && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13151         {
13152           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13153           const int prec = fmt->p;
13154           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13155           int inexact;
13156           mpfr_t m;
13157
13158           mpfr_init2 (m, prec);
13159           mpfr_from_real (m, ra, GMP_RNDN);
13160           mpfr_clear_flags ();
13161           inexact = func (m, m, rnd);
13162           result = do_mpfr_ckconv (m, type, inexact);
13163           mpfr_clear (m);
13164         }
13165     }
13166
13167   return result;
13168 }
13169
13170 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13171    FUNC on it and return the resulting value as a tree with type TYPE.
13172    The mpfr precision is set to the precision of TYPE.  We assume that
13173    function FUNC returns zero if the result could be calculated
13174    exactly within the requested precision.  */
13175
13176 static tree
13177 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13178               int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13179 {
13180   tree result = NULL_TREE;
13181
13182   STRIP_NOPS (arg1);
13183   STRIP_NOPS (arg2);
13184
13185   /* To proceed, MPFR must exactly represent the target floating point
13186      format, which only happens when the target base equals two.  */
13187   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13188       && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13189       && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13190     {
13191       const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13192       const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13193
13194       if (real_isfinite (ra1) && real_isfinite (ra2))
13195         {
13196           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13197           const int prec = fmt->p;
13198           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13199           int inexact;
13200           mpfr_t m1, m2;
13201
13202           mpfr_inits2 (prec, m1, m2, NULL);
13203           mpfr_from_real (m1, ra1, GMP_RNDN);
13204           mpfr_from_real (m2, ra2, GMP_RNDN);
13205           mpfr_clear_flags ();
13206           inexact = func (m1, m1, m2, rnd);
13207           result = do_mpfr_ckconv (m1, type, inexact);
13208           mpfr_clears (m1, m2, NULL);
13209         }
13210     }
13211
13212   return result;
13213 }
13214
13215 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13216    FUNC on it and return the resulting value as a tree with type TYPE.
13217    The mpfr precision is set to the precision of TYPE.  We assume that
13218    function FUNC returns zero if the result could be calculated
13219    exactly within the requested precision.  */
13220
13221 static tree
13222 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13223               int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13224 {
13225   tree result = NULL_TREE;
13226
13227   STRIP_NOPS (arg1);
13228   STRIP_NOPS (arg2);
13229   STRIP_NOPS (arg3);
13230
13231   /* To proceed, MPFR must exactly represent the target floating point
13232      format, which only happens when the target base equals two.  */
13233   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13234       && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13235       && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13236       && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13237     {
13238       const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13239       const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13240       const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13241
13242       if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13243         {
13244           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13245           const int prec = fmt->p;
13246           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13247           int inexact;
13248           mpfr_t m1, m2, m3;
13249
13250           mpfr_inits2 (prec, m1, m2, m3, NULL);
13251           mpfr_from_real (m1, ra1, GMP_RNDN);
13252           mpfr_from_real (m2, ra2, GMP_RNDN);
13253           mpfr_from_real (m3, ra3, GMP_RNDN);
13254           mpfr_clear_flags ();
13255           inexact = func (m1, m1, m2, m3, rnd);
13256           result = do_mpfr_ckconv (m1, type, inexact);
13257           mpfr_clears (m1, m2, m3, NULL);
13258         }
13259     }
13260
13261   return result;
13262 }
13263
13264 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13265    the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13266    If ARG_SINP and ARG_COSP are NULL then the result is returned
13267    as a complex value.
13268    The type is taken from the type of ARG and is used for setting the
13269    precision of the calculation and results.  */
13270
13271 static tree
13272 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13273 {
13274   tree const type = TREE_TYPE (arg);
13275   tree result = NULL_TREE;
13276
13277   STRIP_NOPS (arg);
13278
13279   /* To proceed, MPFR must exactly represent the target floating point
13280      format, which only happens when the target base equals two.  */
13281   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13282       && TREE_CODE (arg) == REAL_CST
13283       && !TREE_OVERFLOW (arg))
13284     {
13285       const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13286
13287       if (real_isfinite (ra))
13288         {
13289           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13290           const int prec = fmt->p;
13291           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13292           tree result_s, result_c;
13293           int inexact;
13294           mpfr_t m, ms, mc;
13295
13296           mpfr_inits2 (prec, m, ms, mc, NULL);
13297           mpfr_from_real (m, ra, GMP_RNDN);
13298           mpfr_clear_flags ();
13299           inexact = mpfr_sin_cos (ms, mc, m, rnd);
13300           result_s = do_mpfr_ckconv (ms, type, inexact);
13301           result_c = do_mpfr_ckconv (mc, type, inexact);
13302           mpfr_clears (m, ms, mc, NULL);
13303           if (result_s && result_c)
13304             {
13305               /* If we are to return in a complex value do so.  */
13306               if (!arg_sinp && !arg_cosp)
13307                 return build_complex (build_complex_type (type),
13308                                       result_c, result_s);
13309
13310               /* Dereference the sin/cos pointer arguments.  */
13311               arg_sinp = build_fold_indirect_ref (arg_sinp);
13312               arg_cosp = build_fold_indirect_ref (arg_cosp);
13313               /* Proceed if valid pointer type were passed in.  */
13314               if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13315                   && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13316                 {
13317                   /* Set the values. */
13318                   result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13319                                           result_s);
13320                   TREE_SIDE_EFFECTS (result_s) = 1;
13321                   result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13322                                           result_c);
13323                   TREE_SIDE_EFFECTS (result_c) = 1;
13324                   /* Combine the assignments into a compound expr.  */
13325                   result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13326                                                     result_s, result_c));
13327                 }
13328             }
13329         }
13330     }
13331   return result;
13332 }
13333
13334 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13335    two-argument mpfr order N Bessel function FUNC on them and return
13336    the resulting value as a tree with type TYPE.  The mpfr precision
13337    is set to the precision of TYPE.  We assume that function FUNC
13338    returns zero if the result could be calculated exactly within the
13339    requested precision.  */
13340 static tree
13341 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13342                   int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13343                   const REAL_VALUE_TYPE *min, bool inclusive)
13344 {
13345   tree result = NULL_TREE;
13346
13347   STRIP_NOPS (arg1);
13348   STRIP_NOPS (arg2);
13349
13350   /* To proceed, MPFR must exactly represent the target floating point
13351      format, which only happens when the target base equals two.  */
13352   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13353       && host_integerp (arg1, 0)
13354       && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13355     {
13356       const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13357       const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13358
13359       if (n == (long)n
13360           && real_isfinite (ra)
13361           && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13362         {
13363           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13364           const int prec = fmt->p;
13365           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13366           int inexact;
13367           mpfr_t m;
13368
13369           mpfr_init2 (m, prec);
13370           mpfr_from_real (m, ra, GMP_RNDN);
13371           mpfr_clear_flags ();
13372           inexact = func (m, n, m, rnd);
13373           result = do_mpfr_ckconv (m, type, inexact);
13374           mpfr_clear (m);
13375         }
13376     }
13377
13378   return result;
13379 }
13380
13381 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13382    the pointer *(ARG_QUO) and return the result.  The type is taken
13383    from the type of ARG0 and is used for setting the precision of the
13384    calculation and results.  */
13385
13386 static tree
13387 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13388 {
13389   tree const type = TREE_TYPE (arg0);
13390   tree result = NULL_TREE;
13391
13392   STRIP_NOPS (arg0);
13393   STRIP_NOPS (arg1);
13394
13395   /* To proceed, MPFR must exactly represent the target floating point
13396      format, which only happens when the target base equals two.  */
13397   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13398       && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13399       && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13400     {
13401       const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13402       const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13403
13404       if (real_isfinite (ra0) && real_isfinite (ra1))
13405         {
13406           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13407           const int prec = fmt->p;
13408           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13409           tree result_rem;
13410           long integer_quo;
13411           mpfr_t m0, m1;
13412
13413           mpfr_inits2 (prec, m0, m1, NULL);
13414           mpfr_from_real (m0, ra0, GMP_RNDN);
13415           mpfr_from_real (m1, ra1, GMP_RNDN);
13416           mpfr_clear_flags ();
13417           mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13418           /* Remquo is independent of the rounding mode, so pass
13419              inexact=0 to do_mpfr_ckconv().  */
13420           result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13421           mpfr_clears (m0, m1, NULL);
13422           if (result_rem)
13423             {
13424               /* MPFR calculates quo in the host's long so it may
13425                  return more bits in quo than the target int can hold
13426                  if sizeof(host long) > sizeof(target int).  This can
13427                  happen even for native compilers in LP64 mode.  In
13428                  these cases, modulo the quo value with the largest
13429                  number that the target int can hold while leaving one
13430                  bit for the sign.  */
13431               if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13432                 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13433
13434               /* Dereference the quo pointer argument.  */
13435               arg_quo = build_fold_indirect_ref (arg_quo);
13436               /* Proceed iff a valid pointer type was passed in.  */
13437               if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13438                 {
13439                   /* Set the value. */
13440                   tree result_quo = fold_build2 (MODIFY_EXPR,
13441                                                  TREE_TYPE (arg_quo), arg_quo,
13442                                                  build_int_cst (NULL, integer_quo));
13443                   TREE_SIDE_EFFECTS (result_quo) = 1;
13444                   /* Combine the quo assignment with the rem.  */
13445                   result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13446                                                     result_quo, result_rem));
13447                 }
13448             }
13449         }
13450     }
13451   return result;
13452 }
13453
13454 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13455    resulting value as a tree with type TYPE.  The mpfr precision is
13456    set to the precision of TYPE.  We assume that this mpfr function
13457    returns zero if the result could be calculated exactly within the
13458    requested precision.  In addition, the integer pointer represented
13459    by ARG_SG will be dereferenced and set to the appropriate signgam
13460    (-1,1) value.  */
13461
13462 static tree
13463 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13464 {
13465   tree result = NULL_TREE;
13466
13467   STRIP_NOPS (arg);
13468
13469   /* To proceed, MPFR must exactly represent the target floating point
13470      format, which only happens when the target base equals two.  Also
13471      verify ARG is a constant and that ARG_SG is an int pointer.  */
13472   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13473       && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13474       && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13475       && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13476     {
13477       const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13478
13479       /* In addition to NaN and Inf, the argument cannot be zero or a
13480          negative integer.  */
13481       if (real_isfinite (ra)
13482           && ra->cl != rvc_zero
13483           && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13484         {
13485           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13486           const int prec = fmt->p;
13487           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13488           int inexact, sg;
13489           mpfr_t m;
13490           tree result_lg;
13491
13492           mpfr_init2 (m, prec);
13493           mpfr_from_real (m, ra, GMP_RNDN);
13494           mpfr_clear_flags ();
13495           inexact = mpfr_lgamma (m, &sg, m, rnd);
13496           result_lg = do_mpfr_ckconv (m, type, inexact);
13497           mpfr_clear (m);
13498           if (result_lg)
13499             {
13500               tree result_sg;
13501
13502               /* Dereference the arg_sg pointer argument.  */
13503               arg_sg = build_fold_indirect_ref (arg_sg);
13504               /* Assign the signgam value into *arg_sg. */
13505               result_sg = fold_build2 (MODIFY_EXPR,
13506                                        TREE_TYPE (arg_sg), arg_sg,
13507                                        build_int_cst (NULL, sg));
13508               TREE_SIDE_EFFECTS (result_sg) = 1;
13509               /* Combine the signgam assignment with the lgamma result.  */
13510               result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13511                                                 result_sg, result_lg));
13512             }
13513         }
13514     }
13515
13516   return result;
13517 }
13518
13519 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13520    function FUNC on it and return the resulting value as a tree with
13521    type TYPE.  The mpfr precision is set to the precision of TYPE.  We
13522    assume that function FUNC returns zero if the result could be
13523    calculated exactly within the requested precision.  */
13524
13525 static tree
13526 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13527 {
13528   tree result = NULL_TREE;
13529
13530   STRIP_NOPS (arg);
13531
13532   /* To proceed, MPFR must exactly represent the target floating point
13533      format, which only happens when the target base equals two.  */
13534   if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13535       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13536       && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13537     {
13538       const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13539       const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13540
13541       if (real_isfinite (re) && real_isfinite (im))
13542         {
13543           const struct real_format *const fmt =
13544             REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13545           const int prec = fmt->p;
13546           const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13547           const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13548           int inexact;
13549           mpc_t m;
13550
13551           mpc_init2 (m, prec);
13552           mpfr_from_real (mpc_realref(m), re, rnd);
13553           mpfr_from_real (mpc_imagref(m), im, rnd);
13554           mpfr_clear_flags ();
13555           inexact = func (m, m, crnd);
13556           result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13557           mpc_clear (m);
13558         }
13559     }
13560
13561   return result;
13562 }
13563
13564 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13565    mpc function FUNC on it and return the resulting value as a tree
13566    with type TYPE.  The mpfr precision is set to the precision of
13567    TYPE.  We assume that function FUNC returns zero if the result
13568    could be calculated exactly within the requested precision.  If
13569    DO_NONFINITE is true, then fold expressions containing Inf or NaN
13570    in the arguments and/or results.  */
13571
13572 tree
13573 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13574              int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13575 {
13576   tree result = NULL_TREE;
13577
13578   STRIP_NOPS (arg0);
13579   STRIP_NOPS (arg1);
13580
13581   /* To proceed, MPFR must exactly represent the target floating point
13582      format, which only happens when the target base equals two.  */
13583   if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13584       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13585       && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13586       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13587       && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13588     {
13589       const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13590       const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13591       const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13592       const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13593
13594       if (do_nonfinite
13595           || (real_isfinite (re0) && real_isfinite (im0)
13596               && real_isfinite (re1) && real_isfinite (im1)))
13597         {
13598           const struct real_format *const fmt =
13599             REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13600           const int prec = fmt->p;
13601           const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13602           const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13603           int inexact;
13604           mpc_t m0, m1;
13605
13606           mpc_init2 (m0, prec);
13607           mpc_init2 (m1, prec);
13608           mpfr_from_real (mpc_realref(m0), re0, rnd);
13609           mpfr_from_real (mpc_imagref(m0), im0, rnd);
13610           mpfr_from_real (mpc_realref(m1), re1, rnd);
13611           mpfr_from_real (mpc_imagref(m1), im1, rnd);
13612           mpfr_clear_flags ();
13613           inexact = func (m0, m0, m1, crnd);
13614           result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
13615           mpc_clear (m0);
13616           mpc_clear (m1);
13617         }
13618     }
13619
13620   return result;
13621 }
13622
13623 /* Fold a call STMT to __{,v}sprintf_chk.  Return NULL_TREE if
13624    a normal call should be emitted rather than expanding the function
13625    inline.  FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK.  */
13626
13627 static tree
13628 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13629 {
13630   int nargs = gimple_call_num_args (stmt);
13631
13632   return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
13633                                      (nargs > 0
13634                                       ? gimple_call_arg_ptr (stmt, 0)
13635                                       : &error_mark_node), fcode);
13636 }
13637
13638 /* Fold a call STMT to {,v}snprintf.  Return NULL_TREE if
13639    a normal call should be emitted rather than expanding the function
13640    inline.  FCODE is either BUILT_IN_SNPRINTF_CHK or
13641    BUILT_IN_VSNPRINTF_CHK.  If MAXLEN is not NULL, it is maximum length
13642    passed as second argument.  */
13643
13644 tree
13645 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13646                                   enum built_in_function fcode)
13647 {
13648   int nargs = gimple_call_num_args (stmt);
13649
13650   return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
13651                                       (nargs > 0
13652                                        ? gimple_call_arg_ptr (stmt, 0)
13653                                        : &error_mark_node), maxlen, fcode);
13654 }
13655
13656 /* Builtins with folding operations that operate on "..." arguments
13657    need special handling; we need to store the arguments in a convenient
13658    data structure before attempting any folding.  Fortunately there are
13659    only a few builtins that fall into this category.  FNDECL is the
13660    function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13661    result of the function call is ignored.  */
13662
13663 static tree
13664 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
13665                              bool ignore ATTRIBUTE_UNUSED)
13666 {
13667   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13668   tree ret = NULL_TREE;
13669
13670   switch (fcode)
13671     {
13672     case BUILT_IN_SPRINTF_CHK:
13673     case BUILT_IN_VSPRINTF_CHK:
13674       ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
13675       break;
13676
13677     case BUILT_IN_SNPRINTF_CHK:
13678     case BUILT_IN_VSNPRINTF_CHK:
13679       ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
13680
13681     default:
13682       break;
13683     }
13684   if (ret)
13685     {
13686       ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13687       TREE_NO_WARNING (ret) = 1;
13688       return ret;
13689     }
13690   return NULL_TREE;
13691 }
13692
13693 /* A wrapper function for builtin folding that prevents warnings for
13694    "statement without effect" and the like, caused by removing the
13695    call node earlier than the warning is generated.  */
13696
13697 tree
13698 fold_call_stmt (gimple stmt, bool ignore)
13699 {
13700   tree ret = NULL_TREE;
13701   tree fndecl = gimple_call_fndecl (stmt);
13702   location_t loc = gimple_location (stmt);
13703   if (fndecl
13704       && TREE_CODE (fndecl) == FUNCTION_DECL
13705       && DECL_BUILT_IN (fndecl)
13706       && !gimple_call_va_arg_pack_p (stmt))
13707     {
13708       int nargs = gimple_call_num_args (stmt);
13709       tree *args = (nargs > 0
13710                     ? gimple_call_arg_ptr (stmt, 0)
13711                     : &error_mark_node);
13712
13713       if (avoid_folding_inline_builtin (fndecl))
13714         return NULL_TREE;
13715       if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
13716         {
13717           return targetm.fold_builtin (fndecl, nargs, args, ignore);
13718         }
13719       else
13720         {
13721           if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
13722             ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
13723           if (!ret)
13724             ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
13725           if (ret)
13726             {
13727               /* Propagate location information from original call to
13728                  expansion of builtin.  Otherwise things like
13729                  maybe_emit_chk_warning, that operate on the expansion
13730                  of a builtin, will use the wrong location information.  */
13731               if (gimple_has_location (stmt))
13732                 {
13733                   tree realret = ret;
13734                   if (TREE_CODE (ret) == NOP_EXPR)
13735                     realret = TREE_OPERAND (ret, 0);
13736                   if (CAN_HAVE_LOCATION_P (realret)
13737                       && !EXPR_HAS_LOCATION (realret))
13738                     SET_EXPR_LOCATION (realret, loc);
13739                   return realret;
13740                 }
13741               return ret;
13742             }
13743         }
13744     }
13745   return NULL_TREE;
13746 }
13747
13748 /* Look up the function in built_in_decls that corresponds to DECL
13749    and set ASMSPEC as its user assembler name.  DECL must be a
13750    function decl that declares a builtin.  */
13751
13752 void
13753 set_builtin_user_assembler_name (tree decl, const char *asmspec)
13754 {
13755   tree builtin;
13756   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
13757               && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
13758               && asmspec != 0);
13759
13760   builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
13761   set_user_assembler_name (builtin, asmspec);
13762   switch (DECL_FUNCTION_CODE (decl))
13763     {
13764     case BUILT_IN_MEMCPY:
13765       init_block_move_fn (asmspec);
13766       memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
13767       break;
13768     case BUILT_IN_MEMSET:
13769       init_block_clear_fn (asmspec);
13770       memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
13771       break;
13772     case BUILT_IN_MEMMOVE:
13773       memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
13774       break;
13775     case BUILT_IN_MEMCMP:
13776       memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
13777       break;
13778     case BUILT_IN_ABORT:
13779       abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
13780       break;
13781     case BUILT_IN_FFS:
13782       if (INT_TYPE_SIZE < BITS_PER_WORD)
13783         {
13784           set_user_assembler_libfunc ("ffs", asmspec);
13785           set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
13786                                                        MODE_INT, 0), "ffs");
13787         }
13788       break;
13789     default:
13790       break;
13791     }
13792 }
13793
13794 /* Return true if DECL is a builtin that expands to a constant or similarly
13795    simple code.  */
13796 bool
13797 is_simple_builtin (tree decl)
13798 {
13799   if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13800     switch (DECL_FUNCTION_CODE (decl))
13801       {
13802         /* Builtins that expand to constants.  */
13803       case BUILT_IN_CONSTANT_P:
13804       case BUILT_IN_EXPECT:
13805       case BUILT_IN_OBJECT_SIZE:
13806       case BUILT_IN_UNREACHABLE:
13807         /* Simple register moves or loads from stack.  */
13808       case BUILT_IN_RETURN_ADDRESS:
13809       case BUILT_IN_EXTRACT_RETURN_ADDR:
13810       case BUILT_IN_FROB_RETURN_ADDR:
13811       case BUILT_IN_RETURN:
13812       case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
13813       case BUILT_IN_FRAME_ADDRESS:
13814       case BUILT_IN_VA_END:
13815       case BUILT_IN_STACK_SAVE:
13816       case BUILT_IN_STACK_RESTORE:
13817         /* Exception state returns or moves registers around.  */
13818       case BUILT_IN_EH_FILTER:
13819       case BUILT_IN_EH_POINTER:
13820       case BUILT_IN_EH_COPY_VALUES:
13821         return true;
13822
13823       default:
13824         return false;
13825       }
13826
13827   return false;
13828 }
13829
13830 /* Return true if DECL is a builtin that is not expensive, i.e., they are
13831    most probably expanded inline into reasonably simple code.  This is a
13832    superset of is_simple_builtin.  */
13833 bool
13834 is_inexpensive_builtin (tree decl)
13835 {
13836   if (!decl)
13837     return false;
13838   else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
13839     return true;
13840   else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13841     switch (DECL_FUNCTION_CODE (decl))
13842       {
13843       case BUILT_IN_ABS:
13844       case BUILT_IN_ALLOCA:
13845       case BUILT_IN_BSWAP32:
13846       case BUILT_IN_BSWAP64:
13847       case BUILT_IN_CLZ:
13848       case BUILT_IN_CLZIMAX:
13849       case BUILT_IN_CLZL:
13850       case BUILT_IN_CLZLL:
13851       case BUILT_IN_CTZ:
13852       case BUILT_IN_CTZIMAX:
13853       case BUILT_IN_CTZL:
13854       case BUILT_IN_CTZLL:
13855       case BUILT_IN_FFS:
13856       case BUILT_IN_FFSIMAX:
13857       case BUILT_IN_FFSL:
13858       case BUILT_IN_FFSLL:
13859       case BUILT_IN_IMAXABS:
13860       case BUILT_IN_FINITE:
13861       case BUILT_IN_FINITEF:
13862       case BUILT_IN_FINITEL:
13863       case BUILT_IN_FINITED32:
13864       case BUILT_IN_FINITED64:
13865       case BUILT_IN_FINITED128:
13866       case BUILT_IN_FPCLASSIFY:
13867       case BUILT_IN_ISFINITE:
13868       case BUILT_IN_ISINF_SIGN:
13869       case BUILT_IN_ISINF:
13870       case BUILT_IN_ISINFF:
13871       case BUILT_IN_ISINFL:
13872       case BUILT_IN_ISINFD32:
13873       case BUILT_IN_ISINFD64:
13874       case BUILT_IN_ISINFD128:
13875       case BUILT_IN_ISNAN:
13876       case BUILT_IN_ISNANF:
13877       case BUILT_IN_ISNANL:
13878       case BUILT_IN_ISNAND32:
13879       case BUILT_IN_ISNAND64:
13880       case BUILT_IN_ISNAND128:
13881       case BUILT_IN_ISNORMAL:
13882       case BUILT_IN_ISGREATER:
13883       case BUILT_IN_ISGREATEREQUAL:
13884       case BUILT_IN_ISLESS:
13885       case BUILT_IN_ISLESSEQUAL:
13886       case BUILT_IN_ISLESSGREATER:
13887       case BUILT_IN_ISUNORDERED:
13888       case BUILT_IN_VA_ARG_PACK:
13889       case BUILT_IN_VA_ARG_PACK_LEN:
13890       case BUILT_IN_VA_COPY:
13891       case BUILT_IN_TRAP:
13892       case BUILT_IN_SAVEREGS:
13893       case BUILT_IN_POPCOUNTL:
13894       case BUILT_IN_POPCOUNTLL:
13895       case BUILT_IN_POPCOUNTIMAX:
13896       case BUILT_IN_POPCOUNT:
13897       case BUILT_IN_PARITYL:
13898       case BUILT_IN_PARITYLL:
13899       case BUILT_IN_PARITYIMAX:
13900       case BUILT_IN_PARITY:
13901       case BUILT_IN_LABS:
13902       case BUILT_IN_LLABS:
13903       case BUILT_IN_PREFETCH:
13904         return true;
13905
13906       default:
13907         return is_simple_builtin (decl);
13908       }
13909
13910   return false;
13911 }