OSDN Git Service

2012-04-15 Fabien ChĂȘne <fabien@gcc.gnu.org>
[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, 2011,
4    2012 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 "predict.h"
44 #include "tm_p.h"
45 #include "target.h"
46 #include "langhooks.h"
47 #include "basic-block.h"
48 #include "tree-mudflap.h"
49 #include "tree-flow.h"
50 #include "value-prof.h"
51 #include "diagnostic-core.h"
52 #include "builtins.h"
53
54
55 #ifndef PAD_VARARGS_DOWN
56 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
57 #endif
58 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
59
60 struct target_builtins default_target_builtins;
61 #if SWITCHABLE_TARGET
62 struct target_builtins *this_target_builtins = &default_target_builtins;
63 #endif
64
65 /* Define the names of the builtin function types and codes.  */
66 const char *const built_in_class_names[4]
67   = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
68
69 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
70 const char * built_in_names[(int) END_BUILTINS] =
71 {
72 #include "builtins.def"
73 };
74 #undef DEF_BUILTIN
75
76 /* Setup an array of _DECL trees, make sure each element is
77    initialized to NULL_TREE.  */
78 builtin_info_type builtin_info;
79
80 static const char *c_getstr (tree);
81 static rtx c_readstr (const char *, enum machine_mode);
82 static int target_char_cast (tree, char *);
83 static rtx get_memory_rtx (tree, tree);
84 static int apply_args_size (void);
85 static int apply_result_size (void);
86 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
87 static rtx result_vector (int, rtx);
88 #endif
89 static void expand_builtin_update_setjmp_buf (rtx);
90 static void expand_builtin_prefetch (tree);
91 static rtx expand_builtin_apply_args (void);
92 static rtx expand_builtin_apply_args_1 (void);
93 static rtx expand_builtin_apply (rtx, rtx, rtx);
94 static void expand_builtin_return (rtx);
95 static enum type_class type_to_class (tree);
96 static rtx expand_builtin_classify_type (tree);
97 static void expand_errno_check (tree, rtx);
98 static rtx expand_builtin_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
101 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
102 static rtx expand_builtin_interclass_mathfn (tree, rtx);
103 static rtx expand_builtin_sincos (tree);
104 static rtx expand_builtin_cexpi (tree, rtx);
105 static rtx expand_builtin_int_roundingfn (tree, rtx);
106 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
107 static rtx expand_builtin_next_arg (void);
108 static rtx expand_builtin_va_start (tree);
109 static rtx expand_builtin_va_end (tree);
110 static rtx expand_builtin_va_copy (tree);
111 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strcmp (tree, rtx);
113 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
114 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
115 static rtx expand_builtin_memcpy (tree, rtx);
116 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
117 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
118                                         enum machine_mode, int);
119 static rtx expand_builtin_strcpy (tree, rtx);
120 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
121 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
122 static rtx expand_builtin_strncpy (tree, rtx);
123 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
126 static rtx expand_builtin_bzero (tree);
127 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
128 static rtx expand_builtin_alloca (tree, bool);
129 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
130 static rtx expand_builtin_frame_address (tree, tree);
131 static tree stabilize_va_list_loc (location_t, tree, int);
132 static rtx expand_builtin_expect (tree, rtx);
133 static tree fold_builtin_constant_p (tree);
134 static tree fold_builtin_expect (location_t, tree, tree);
135 static tree fold_builtin_classify_type (tree);
136 static tree fold_builtin_strlen (location_t, tree, tree);
137 static tree fold_builtin_inf (location_t, tree, int);
138 static tree fold_builtin_nan (tree, tree, int);
139 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
140 static bool validate_arg (const_tree, enum tree_code code);
141 static bool integer_valued_real_p (tree);
142 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
143 static bool readonly_data_expr (tree);
144 static rtx expand_builtin_fabs (tree, rtx, rtx);
145 static rtx expand_builtin_signbit (tree, rtx);
146 static tree fold_builtin_sqrt (location_t, tree, tree);
147 static tree fold_builtin_cbrt (location_t, tree, tree);
148 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
149 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
150 static tree fold_builtin_cos (location_t, tree, tree, tree);
151 static tree fold_builtin_cosh (location_t, tree, tree, tree);
152 static tree fold_builtin_tan (tree, tree);
153 static tree fold_builtin_trunc (location_t, tree, tree);
154 static tree fold_builtin_floor (location_t, tree, tree);
155 static tree fold_builtin_ceil (location_t, tree, tree);
156 static tree fold_builtin_round (location_t, tree, tree);
157 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
158 static tree fold_builtin_bitop (tree, tree);
159 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
160 static tree fold_builtin_strchr (location_t, tree, tree, tree);
161 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
162 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
163 static tree fold_builtin_strcmp (location_t, tree, tree);
164 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
165 static tree fold_builtin_signbit (location_t, tree, tree);
166 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
167 static tree fold_builtin_isascii (location_t, tree);
168 static tree fold_builtin_toascii (location_t, tree);
169 static tree fold_builtin_isdigit (location_t, tree);
170 static tree fold_builtin_fabs (location_t, tree, tree);
171 static tree fold_builtin_abs (location_t, tree, tree);
172 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
173                                         enum tree_code);
174 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
175 static tree fold_builtin_0 (location_t, tree, bool);
176 static tree fold_builtin_1 (location_t, tree, tree, bool);
177 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
178 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
179 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
180 static tree fold_builtin_varargs (location_t, tree, tree, bool);
181
182 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
183 static tree fold_builtin_strstr (location_t, tree, tree, tree);
184 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
185 static tree fold_builtin_strcat (location_t, tree, tree);
186 static tree fold_builtin_strncat (location_t, tree, tree, tree);
187 static tree fold_builtin_strspn (location_t, tree, tree);
188 static tree fold_builtin_strcspn (location_t, tree, tree);
189 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
190 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
191
192 static rtx expand_builtin_object_size (tree);
193 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
194                                       enum built_in_function);
195 static void maybe_emit_chk_warning (tree, enum built_in_function);
196 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
197 static void maybe_emit_free_warning (tree);
198 static tree fold_builtin_object_size (tree, tree);
199 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
200 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
201 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
202 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
203 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
204                                   enum built_in_function);
205 static bool init_target_chars (void);
206
207 static unsigned HOST_WIDE_INT target_newline;
208 static unsigned HOST_WIDE_INT target_percent;
209 static unsigned HOST_WIDE_INT target_c;
210 static unsigned HOST_WIDE_INT target_s;
211 static char target_percent_c[3];
212 static char target_percent_s[3];
213 static char target_percent_s_newline[4];
214 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
215                           const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
216 static tree do_mpfr_arg2 (tree, tree, tree,
217                           int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
218 static tree do_mpfr_arg3 (tree, tree, tree, tree,
219                           int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
220 static tree do_mpfr_sincos (tree, tree, tree);
221 static tree do_mpfr_bessel_n (tree, tree, tree,
222                               int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
223                               const REAL_VALUE_TYPE *, bool);
224 static tree do_mpfr_remquo (tree, tree, tree);
225 static tree do_mpfr_lgamma_r (tree, tree, tree);
226 static void expand_builtin_sync_synchronize (void);
227
228 /* Return true if NAME starts with __builtin_ or __sync_.  */
229
230 static bool
231 is_builtin_name (const char *name)
232 {
233   if (strncmp (name, "__builtin_", 10) == 0)
234     return true;
235   if (strncmp (name, "__sync_", 7) == 0)
236     return true;
237   if (strncmp (name, "__atomic_", 9) == 0)
238     return true;
239   return false;
240 }
241
242
243 /* Return true if DECL is a function symbol representing a built-in.  */
244
245 bool
246 is_builtin_fn (tree decl)
247 {
248   return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
249 }
250
251
252 /* Return true if NODE should be considered for inline expansion regardless
253    of the optimization level.  This means whenever a function is invoked with
254    its "internal" name, which normally contains the prefix "__builtin".  */
255
256 static bool
257 called_as_built_in (tree node)
258 {
259   /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
260      we want the name used to call the function, not the name it
261      will have. */
262   const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
263   return is_builtin_name (name);
264 }
265
266 /* Compute values M and N such that M divides (address of EXP - N) and
267    such that N < M.  Store N in *BITPOSP and return M.
268
269    Note that the address (and thus the alignment) computed here is based
270    on the address to which a symbol resolves, whereas DECL_ALIGN is based
271    on the address at which an object is actually located.  These two
272    addresses are not always the same.  For example, on ARM targets,
273    the address &foo of a Thumb function foo() has the lowest bit set,
274    whereas foo() itself starts on an even address.  */
275
276 unsigned int
277 get_object_alignment_1 (tree exp, unsigned HOST_WIDE_INT *bitposp)
278 {
279   HOST_WIDE_INT bitsize, bitpos;
280   tree offset;
281   enum machine_mode mode;
282   int unsignedp, volatilep;
283   unsigned int align, inner;
284
285   /* Get the innermost object and the constant (bitpos) and possibly
286      variable (offset) offset of the access.  */
287   exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
288                              &mode, &unsignedp, &volatilep, true);
289
290   /* Extract alignment information from the innermost object and
291      possibly adjust bitpos and offset.  */
292   if (TREE_CODE (exp) == CONST_DECL)
293     exp = DECL_INITIAL (exp);
294   if (DECL_P (exp)
295       && TREE_CODE (exp) != LABEL_DECL)
296     {
297       if (TREE_CODE (exp) == FUNCTION_DECL)
298         {
299           /* Function addresses can encode extra information besides their
300              alignment.  However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
301              allows the low bit to be used as a virtual bit, we know
302              that the address itself must be 2-byte aligned.  */
303           if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
304             align = 2 * BITS_PER_UNIT;
305           else
306             align = BITS_PER_UNIT;
307         }
308       else
309         align = DECL_ALIGN (exp);
310     }
311   else if (CONSTANT_CLASS_P (exp))
312     {
313       align = TYPE_ALIGN (TREE_TYPE (exp));
314 #ifdef CONSTANT_ALIGNMENT
315       align = (unsigned)CONSTANT_ALIGNMENT (exp, align);
316 #endif
317     }
318   else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
319     align = TYPE_ALIGN (TREE_TYPE (exp));
320   else if (TREE_CODE (exp) == INDIRECT_REF)
321     align = TYPE_ALIGN (TREE_TYPE (exp));
322   else if (TREE_CODE (exp) == MEM_REF)
323     {
324       tree addr = TREE_OPERAND (exp, 0);
325       struct ptr_info_def *pi;
326       if (TREE_CODE (addr) == BIT_AND_EXPR
327           && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
328         {
329           align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
330                     & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
331           align *= BITS_PER_UNIT;
332           addr = TREE_OPERAND (addr, 0);
333         }
334       else
335         align = BITS_PER_UNIT;
336       if (TREE_CODE (addr) == SSA_NAME
337           && (pi = SSA_NAME_PTR_INFO (addr)))
338         {
339           bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
340           align = MAX (pi->align * BITS_PER_UNIT, align);
341         }
342       else if (TREE_CODE (addr) == ADDR_EXPR)
343         align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0)));
344       bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
345     }
346   else if (TREE_CODE (exp) == TARGET_MEM_REF)
347     {
348       struct ptr_info_def *pi;
349       tree addr = TMR_BASE (exp);
350       if (TREE_CODE (addr) == BIT_AND_EXPR
351           && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
352         {
353           align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
354                    & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
355           align *= BITS_PER_UNIT;
356           addr = TREE_OPERAND (addr, 0);
357         }
358       else
359         align = BITS_PER_UNIT;
360       if (TREE_CODE (addr) == SSA_NAME
361           && (pi = SSA_NAME_PTR_INFO (addr)))
362         {
363           bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
364           align = MAX (pi->align * BITS_PER_UNIT, align);
365         }
366       else if (TREE_CODE (addr) == ADDR_EXPR)
367         align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0)));
368       if (TMR_OFFSET (exp))
369         bitpos += TREE_INT_CST_LOW (TMR_OFFSET (exp)) * BITS_PER_UNIT;
370       if (TMR_INDEX (exp) && TMR_STEP (exp))
371         {
372           unsigned HOST_WIDE_INT step = TREE_INT_CST_LOW (TMR_STEP (exp));
373           align = MIN (align, (step & -step) * BITS_PER_UNIT);
374         }
375       else if (TMR_INDEX (exp))
376         align = BITS_PER_UNIT;
377       if (TMR_INDEX2 (exp))
378         align = BITS_PER_UNIT;
379     }
380   else
381     align = BITS_PER_UNIT;
382
383   /* If there is a non-constant offset part extract the maximum
384      alignment that can prevail.  */
385   inner = ~0U;
386   while (offset)
387     {
388       tree next_offset;
389
390       if (TREE_CODE (offset) == PLUS_EXPR)
391         {
392           next_offset = TREE_OPERAND (offset, 0);
393           offset = TREE_OPERAND (offset, 1);
394         }
395       else
396         next_offset = NULL;
397       if (host_integerp (offset, 1))
398         {
399           /* Any overflow in calculating offset_bits won't change
400              the alignment.  */
401           unsigned offset_bits
402             = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
403
404           if (offset_bits)
405             inner = MIN (inner, (offset_bits & -offset_bits));
406         }
407       else if (TREE_CODE (offset) == MULT_EXPR
408                && host_integerp (TREE_OPERAND (offset, 1), 1))
409         {
410           /* Any overflow in calculating offset_factor won't change
411              the alignment.  */
412           unsigned offset_factor
413             = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
414                * BITS_PER_UNIT);
415
416           if (offset_factor)
417             inner = MIN (inner, (offset_factor & -offset_factor));
418         }
419       else
420         {
421           inner = MIN (inner, BITS_PER_UNIT);
422           break;
423         }
424       offset = next_offset;
425     }
426
427   /* Alignment is innermost object alignment adjusted by the constant
428      and non-constant offset parts.  */
429   align = MIN (align, inner);
430   bitpos = bitpos & (align - 1);
431
432   *bitposp = bitpos;
433   return align;
434 }
435
436 /* Return the alignment in bits of EXP, an object.  */
437
438 unsigned int
439 get_object_alignment (tree exp)
440 {
441   unsigned HOST_WIDE_INT bitpos = 0;
442   unsigned int align;
443
444   align = get_object_alignment_1 (exp, &bitpos);
445
446   /* align and bitpos now specify known low bits of the pointer.
447      ptr & (align - 1) == bitpos.  */
448
449   if (bitpos != 0)
450     align = (bitpos & -bitpos);
451
452   return align;
453 }
454
455 /* Return the alignment of object EXP, also considering its type when we do
456    not know of explicit misalignment.  Only handle MEM_REF and TARGET_MEM_REF.
457
458    ??? Note that, in the general case, the type of an expression is not kept
459    consistent with misalignment information by the front-end, for example when
460    taking the address of a member of a packed structure.  However, in most of
461    the cases, expressions have the alignment of their type so we optimistically
462    fall back to this alignment when we cannot compute a misalignment.  */
463
464 unsigned int
465 get_object_or_type_alignment (tree exp)
466 {
467   unsigned HOST_WIDE_INT misalign;
468   unsigned int align = get_object_alignment_1 (exp, &misalign);
469
470   gcc_assert (TREE_CODE (exp) == MEM_REF || TREE_CODE (exp) == TARGET_MEM_REF);
471
472   if (misalign != 0)
473     align = (misalign & -misalign);
474   else
475     align = MAX (TYPE_ALIGN (TREE_TYPE (exp)), align);
476
477   return align;
478 }
479
480 /* For a pointer valued expression EXP compute values M and N such that
481    M divides (EXP - N) and such that N < M.  Store N in *BITPOSP and return M.
482
483    If EXP is not a pointer, 0 is returned.  */
484
485 unsigned int
486 get_pointer_alignment_1 (tree exp, unsigned HOST_WIDE_INT *bitposp)
487 {
488   STRIP_NOPS (exp);
489
490   if (TREE_CODE (exp) == ADDR_EXPR)
491     return get_object_alignment_1 (TREE_OPERAND (exp, 0), bitposp);
492   else if (TREE_CODE (exp) == SSA_NAME
493            && POINTER_TYPE_P (TREE_TYPE (exp)))
494     {
495       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
496       if (!pi)
497         {
498           *bitposp = 0;
499           return BITS_PER_UNIT;
500         }
501       *bitposp = pi->misalign * BITS_PER_UNIT;
502       return pi->align * BITS_PER_UNIT;
503     }
504
505   *bitposp = 0;
506   return POINTER_TYPE_P (TREE_TYPE (exp)) ? BITS_PER_UNIT : 0;
507 }
508
509 /* Return the alignment in bits of EXP, a pointer valued expression.
510    The alignment returned is, by default, the alignment of the thing that
511    EXP points to.  If it is not a POINTER_TYPE, 0 is returned.
512
513    Otherwise, look at the expression to see if we can do better, i.e., if the
514    expression is actually pointing at an object whose alignment is tighter.  */
515
516 unsigned int
517 get_pointer_alignment (tree exp)
518 {
519   unsigned HOST_WIDE_INT bitpos = 0;
520   unsigned int align;
521   
522   align = get_pointer_alignment_1 (exp, &bitpos);
523
524   /* align and bitpos now specify known low bits of the pointer.
525      ptr & (align - 1) == bitpos.  */
526
527   if (bitpos != 0)
528     align = (bitpos & -bitpos);
529
530   return align;
531 }
532
533 /* Compute the length of a C string.  TREE_STRING_LENGTH is not the right
534    way, because it could contain a zero byte in the middle.
535    TREE_STRING_LENGTH is the size of the character array, not the string.
536
537    ONLY_VALUE should be nonzero if the result is not going to be emitted
538    into the instruction stream and zero if it is going to be expanded.
539    E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
540    is returned, otherwise NULL, since
541    len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
542    evaluate the side-effects.
543
544    The value returned is of type `ssizetype'.
545
546    Unfortunately, string_constant can't access the values of const char
547    arrays with initializers, so neither can we do so here.  */
548
549 tree
550 c_strlen (tree src, int only_value)
551 {
552   tree offset_node;
553   HOST_WIDE_INT offset;
554   int max;
555   const char *ptr;
556   location_t loc;
557
558   STRIP_NOPS (src);
559   if (TREE_CODE (src) == COND_EXPR
560       && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
561     {
562       tree len1, len2;
563
564       len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
565       len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
566       if (tree_int_cst_equal (len1, len2))
567         return len1;
568     }
569
570   if (TREE_CODE (src) == COMPOUND_EXPR
571       && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
572     return c_strlen (TREE_OPERAND (src, 1), only_value);
573
574   loc = EXPR_LOC_OR_HERE (src);
575
576   src = string_constant (src, &offset_node);
577   if (src == 0)
578     return NULL_TREE;
579
580   max = TREE_STRING_LENGTH (src) - 1;
581   ptr = TREE_STRING_POINTER (src);
582
583   if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
584     {
585       /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
586          compute the offset to the following null if we don't know where to
587          start searching for it.  */
588       int i;
589
590       for (i = 0; i < max; i++)
591         if (ptr[i] == 0)
592           return NULL_TREE;
593
594       /* We don't know the starting offset, but we do know that the string
595          has no internal zero bytes.  We can assume that the offset falls
596          within the bounds of the string; otherwise, the programmer deserves
597          what he gets.  Subtract the offset from the length of the string,
598          and return that.  This would perhaps not be valid if we were dealing
599          with named arrays in addition to literal string constants.  */
600
601       return size_diffop_loc (loc, size_int (max), offset_node);
602     }
603
604   /* We have a known offset into the string.  Start searching there for
605      a null character if we can represent it as a single HOST_WIDE_INT.  */
606   if (offset_node == 0)
607     offset = 0;
608   else if (! host_integerp (offset_node, 0))
609     offset = -1;
610   else
611     offset = tree_low_cst (offset_node, 0);
612
613   /* If the offset is known to be out of bounds, warn, and call strlen at
614      runtime.  */
615   if (offset < 0 || offset > max)
616     {
617      /* Suppress multiple warnings for propagated constant strings.  */
618       if (! TREE_NO_WARNING (src))
619         {
620           warning_at (loc, 0, "offset outside bounds of constant string");
621           TREE_NO_WARNING (src) = 1;
622         }
623       return NULL_TREE;
624     }
625
626   /* Use strlen to search for the first zero byte.  Since any strings
627      constructed with build_string will have nulls appended, we win even
628      if we get handed something like (char[4])"abcd".
629
630      Since OFFSET is our starting index into the string, no further
631      calculation is needed.  */
632   return ssize_int (strlen (ptr + offset));
633 }
634
635 /* Return a char pointer for a C string if it is a string constant
636    or sum of string constant and integer constant.  */
637
638 static const char *
639 c_getstr (tree src)
640 {
641   tree offset_node;
642
643   src = string_constant (src, &offset_node);
644   if (src == 0)
645     return 0;
646
647   if (offset_node == 0)
648     return TREE_STRING_POINTER (src);
649   else if (!host_integerp (offset_node, 1)
650            || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
651     return 0;
652
653   return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
654 }
655
656 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
657    GET_MODE_BITSIZE (MODE) bits from string constant STR.  */
658
659 static rtx
660 c_readstr (const char *str, enum machine_mode mode)
661 {
662   HOST_WIDE_INT c[2];
663   HOST_WIDE_INT ch;
664   unsigned int i, j;
665
666   gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
667
668   c[0] = 0;
669   c[1] = 0;
670   ch = 1;
671   for (i = 0; i < GET_MODE_SIZE (mode); i++)
672     {
673       j = i;
674       if (WORDS_BIG_ENDIAN)
675         j = GET_MODE_SIZE (mode) - i - 1;
676       if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
677           && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
678         j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
679       j *= BITS_PER_UNIT;
680       gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
681
682       if (ch)
683         ch = (unsigned char) str[i];
684       c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
685     }
686   return immed_double_const (c[0], c[1], mode);
687 }
688
689 /* Cast a target constant CST to target CHAR and if that value fits into
690    host char type, return zero and put that value into variable pointed to by
691    P.  */
692
693 static int
694 target_char_cast (tree cst, char *p)
695 {
696   unsigned HOST_WIDE_INT val, hostval;
697
698   if (TREE_CODE (cst) != INTEGER_CST
699       || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
700     return 1;
701
702   val = TREE_INT_CST_LOW (cst);
703   if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
704     val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
705
706   hostval = val;
707   if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
708     hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
709
710   if (val != hostval)
711     return 1;
712
713   *p = hostval;
714   return 0;
715 }
716
717 /* Similar to save_expr, but assumes that arbitrary code is not executed
718    in between the multiple evaluations.  In particular, we assume that a
719    non-addressable local variable will not be modified.  */
720
721 static tree
722 builtin_save_expr (tree exp)
723 {
724   if (TREE_CODE (exp) == SSA_NAME
725       || (TREE_ADDRESSABLE (exp) == 0
726           && (TREE_CODE (exp) == PARM_DECL
727               || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
728     return exp;
729
730   return save_expr (exp);
731 }
732
733 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
734    times to get the address of either a higher stack frame, or a return
735    address located within it (depending on FNDECL_CODE).  */
736
737 static rtx
738 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
739 {
740   int i;
741
742 #ifdef INITIAL_FRAME_ADDRESS_RTX
743   rtx tem = INITIAL_FRAME_ADDRESS_RTX;
744 #else
745   rtx tem;
746
747   /* For a zero count with __builtin_return_address, we don't care what
748      frame address we return, because target-specific definitions will
749      override us.  Therefore frame pointer elimination is OK, and using
750      the soft frame pointer is OK.
751
752      For a nonzero count, or a zero count with __builtin_frame_address,
753      we require a stable offset from the current frame pointer to the
754      previous one, so we must use the hard frame pointer, and
755      we must disable frame pointer elimination.  */
756   if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
757     tem = frame_pointer_rtx;
758   else
759     {
760       tem = hard_frame_pointer_rtx;
761
762       /* Tell reload not to eliminate the frame pointer.  */
763       crtl->accesses_prior_frames = 1;
764     }
765 #endif
766
767   /* Some machines need special handling before we can access
768      arbitrary frames.  For example, on the SPARC, we must first flush
769      all register windows to the stack.  */
770 #ifdef SETUP_FRAME_ADDRESSES
771   if (count > 0)
772     SETUP_FRAME_ADDRESSES ();
773 #endif
774
775   /* On the SPARC, the return address is not in the frame, it is in a
776      register.  There is no way to access it off of the current frame
777      pointer, but it can be accessed off the previous frame pointer by
778      reading the value from the register window save area.  */
779 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
780   if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
781     count--;
782 #endif
783
784   /* Scan back COUNT frames to the specified frame.  */
785   for (i = 0; i < count; i++)
786     {
787       /* Assume the dynamic chain pointer is in the word that the
788          frame address points to, unless otherwise specified.  */
789 #ifdef DYNAMIC_CHAIN_ADDRESS
790       tem = DYNAMIC_CHAIN_ADDRESS (tem);
791 #endif
792       tem = memory_address (Pmode, tem);
793       tem = gen_frame_mem (Pmode, tem);
794       tem = copy_to_reg (tem);
795     }
796
797   /* For __builtin_frame_address, return what we've got.  But, on
798      the SPARC for example, we may have to add a bias.  */
799   if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
800 #ifdef FRAME_ADDR_RTX
801     return FRAME_ADDR_RTX (tem);
802 #else
803     return tem;
804 #endif
805
806   /* For __builtin_return_address, get the return address from that frame.  */
807 #ifdef RETURN_ADDR_RTX
808   tem = RETURN_ADDR_RTX (count, tem);
809 #else
810   tem = memory_address (Pmode,
811                         plus_constant (tem, GET_MODE_SIZE (Pmode)));
812   tem = gen_frame_mem (Pmode, tem);
813 #endif
814   return tem;
815 }
816
817 /* Alias set used for setjmp buffer.  */
818 static alias_set_type setjmp_alias_set = -1;
819
820 /* Construct the leading half of a __builtin_setjmp call.  Control will
821    return to RECEIVER_LABEL.  This is also called directly by the SJLJ
822    exception handling code.  */
823
824 void
825 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
826 {
827   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
828   rtx stack_save;
829   rtx mem;
830
831   if (setjmp_alias_set == -1)
832     setjmp_alias_set = new_alias_set ();
833
834   buf_addr = convert_memory_address (Pmode, buf_addr);
835
836   buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
837
838   /* We store the frame pointer and the address of receiver_label in
839      the buffer and use the rest of it for the stack save area, which
840      is machine-dependent.  */
841
842   mem = gen_rtx_MEM (Pmode, buf_addr);
843   set_mem_alias_set (mem, setjmp_alias_set);
844   emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
845
846   mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
847   set_mem_alias_set (mem, setjmp_alias_set);
848
849   emit_move_insn (validize_mem (mem),
850                   force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
851
852   stack_save = gen_rtx_MEM (sa_mode,
853                             plus_constant (buf_addr,
854                                            2 * GET_MODE_SIZE (Pmode)));
855   set_mem_alias_set (stack_save, setjmp_alias_set);
856   emit_stack_save (SAVE_NONLOCAL, &stack_save);
857
858   /* If there is further processing to do, do it.  */
859 #ifdef HAVE_builtin_setjmp_setup
860   if (HAVE_builtin_setjmp_setup)
861     emit_insn (gen_builtin_setjmp_setup (buf_addr));
862 #endif
863
864   /* We have a nonlocal label.   */
865   cfun->has_nonlocal_label = 1;
866 }
867
868 /* Construct the trailing part of a __builtin_setjmp call.  This is
869    also called directly by the SJLJ exception handling code.  */
870
871 void
872 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
873 {
874   rtx chain;
875
876   /* Clobber the FP when we get here, so we have to make sure it's
877      marked as used by this function.  */
878   emit_use (hard_frame_pointer_rtx);
879
880   /* Mark the static chain as clobbered here so life information
881      doesn't get messed up for it.  */
882   chain = targetm.calls.static_chain (current_function_decl, true);
883   if (chain && REG_P (chain))
884     emit_clobber (chain);
885
886   /* Now put in the code to restore the frame pointer, and argument
887      pointer, if needed.  */
888 #ifdef HAVE_nonlocal_goto
889   if (! HAVE_nonlocal_goto)
890 #endif
891     {
892       emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
893       /* This might change the hard frame pointer in ways that aren't
894          apparent to early optimization passes, so force a clobber.  */
895       emit_clobber (hard_frame_pointer_rtx);
896     }
897
898 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
899   if (fixed_regs[ARG_POINTER_REGNUM])
900     {
901 #ifdef ELIMINABLE_REGS
902       size_t i;
903       static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
904
905       for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
906         if (elim_regs[i].from == ARG_POINTER_REGNUM
907             && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
908           break;
909
910       if (i == ARRAY_SIZE (elim_regs))
911 #endif
912         {
913           /* Now restore our arg pointer from the address at which it
914              was saved in our stack frame.  */
915           emit_move_insn (crtl->args.internal_arg_pointer,
916                           copy_to_reg (get_arg_pointer_save_area ()));
917         }
918     }
919 #endif
920
921 #ifdef HAVE_builtin_setjmp_receiver
922   if (HAVE_builtin_setjmp_receiver)
923     emit_insn (gen_builtin_setjmp_receiver (receiver_label));
924   else
925 #endif
926 #ifdef HAVE_nonlocal_goto_receiver
927     if (HAVE_nonlocal_goto_receiver)
928       emit_insn (gen_nonlocal_goto_receiver ());
929     else
930 #endif
931       { /* Nothing */ }
932
933   /* We must not allow the code we just generated to be reordered by
934      scheduling.  Specifically, the update of the frame pointer must
935      happen immediately, not later.  */
936   emit_insn (gen_blockage ());
937 }
938
939 /* __builtin_longjmp is passed a pointer to an array of five words (not
940    all will be used on all machines).  It operates similarly to the C
941    library function of the same name, but is more efficient.  Much of
942    the code below is copied from the handling of non-local gotos.  */
943
944 static void
945 expand_builtin_longjmp (rtx buf_addr, rtx value)
946 {
947   rtx fp, lab, stack, insn, last;
948   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
949
950   /* DRAP is needed for stack realign if longjmp is expanded to current
951      function  */
952   if (SUPPORTS_STACK_ALIGNMENT)
953     crtl->need_drap = true;
954
955   if (setjmp_alias_set == -1)
956     setjmp_alias_set = new_alias_set ();
957
958   buf_addr = convert_memory_address (Pmode, buf_addr);
959
960   buf_addr = force_reg (Pmode, buf_addr);
961
962   /* We require that the user must pass a second argument of 1, because
963      that is what builtin_setjmp will return.  */
964   gcc_assert (value == const1_rtx);
965
966   last = get_last_insn ();
967 #ifdef HAVE_builtin_longjmp
968   if (HAVE_builtin_longjmp)
969     emit_insn (gen_builtin_longjmp (buf_addr));
970   else
971 #endif
972     {
973       fp = gen_rtx_MEM (Pmode, buf_addr);
974       lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
975                                                GET_MODE_SIZE (Pmode)));
976
977       stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
978                                                    2 * GET_MODE_SIZE (Pmode)));
979       set_mem_alias_set (fp, setjmp_alias_set);
980       set_mem_alias_set (lab, setjmp_alias_set);
981       set_mem_alias_set (stack, setjmp_alias_set);
982
983       /* Pick up FP, label, and SP from the block and jump.  This code is
984          from expand_goto in stmt.c; see there for detailed comments.  */
985 #ifdef HAVE_nonlocal_goto
986       if (HAVE_nonlocal_goto)
987         /* We have to pass a value to the nonlocal_goto pattern that will
988            get copied into the static_chain pointer, but it does not matter
989            what that value is, because builtin_setjmp does not use it.  */
990         emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
991       else
992 #endif
993         {
994           lab = copy_to_reg (lab);
995
996           emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
997           emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
998
999           emit_move_insn (hard_frame_pointer_rtx, fp);
1000           emit_stack_restore (SAVE_NONLOCAL, stack);
1001
1002           emit_use (hard_frame_pointer_rtx);
1003           emit_use (stack_pointer_rtx);
1004           emit_indirect_jump (lab);
1005         }
1006     }
1007
1008   /* Search backwards and mark the jump insn as a non-local goto.
1009      Note that this precludes the use of __builtin_longjmp to a
1010      __builtin_setjmp target in the same function.  However, we've
1011      already cautioned the user that these functions are for
1012      internal exception handling use only.  */
1013   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1014     {
1015       gcc_assert (insn != last);
1016
1017       if (JUMP_P (insn))
1018         {
1019           add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1020           break;
1021         }
1022       else if (CALL_P (insn))
1023         break;
1024     }
1025 }
1026
1027 /* Expand a call to __builtin_nonlocal_goto.  We're passed the target label
1028    and the address of the save area.  */
1029
1030 static rtx
1031 expand_builtin_nonlocal_goto (tree exp)
1032 {
1033   tree t_label, t_save_area;
1034   rtx r_label, r_save_area, r_fp, r_sp, insn;
1035
1036   if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1037     return NULL_RTX;
1038
1039   t_label = CALL_EXPR_ARG (exp, 0);
1040   t_save_area = CALL_EXPR_ARG (exp, 1);
1041
1042   r_label = expand_normal (t_label);
1043   r_label = convert_memory_address (Pmode, r_label);
1044   r_save_area = expand_normal (t_save_area);
1045   r_save_area = convert_memory_address (Pmode, r_save_area);
1046   /* Copy the address of the save location to a register just in case it was
1047      based on the frame pointer.   */
1048   r_save_area = copy_to_reg (r_save_area);
1049   r_fp = gen_rtx_MEM (Pmode, r_save_area);
1050   r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1051                       plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
1052
1053   crtl->has_nonlocal_goto = 1;
1054
1055 #ifdef HAVE_nonlocal_goto
1056   /* ??? We no longer need to pass the static chain value, afaik.  */
1057   if (HAVE_nonlocal_goto)
1058     emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1059   else
1060 #endif
1061     {
1062       r_label = copy_to_reg (r_label);
1063
1064       emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1065       emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1066
1067       /* Restore frame pointer for containing function.  */
1068       emit_move_insn (hard_frame_pointer_rtx, r_fp);
1069       emit_stack_restore (SAVE_NONLOCAL, r_sp);
1070
1071       /* USE of hard_frame_pointer_rtx added for consistency;
1072          not clear if really needed.  */
1073       emit_use (hard_frame_pointer_rtx);
1074       emit_use (stack_pointer_rtx);
1075
1076       /* If the architecture is using a GP register, we must
1077          conservatively assume that the target function makes use of it.
1078          The prologue of functions with nonlocal gotos must therefore
1079          initialize the GP register to the appropriate value, and we
1080          must then make sure that this value is live at the point
1081          of the jump.  (Note that this doesn't necessarily apply
1082          to targets with a nonlocal_goto pattern; they are free
1083          to implement it in their own way.  Note also that this is
1084          a no-op if the GP register is a global invariant.)  */
1085       if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1086           && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1087         emit_use (pic_offset_table_rtx);
1088
1089       emit_indirect_jump (r_label);
1090     }
1091
1092   /* Search backwards to the jump insn and mark it as a
1093      non-local goto.  */
1094   for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1095     {
1096       if (JUMP_P (insn))
1097         {
1098           add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1099           break;
1100         }
1101       else if (CALL_P (insn))
1102         break;
1103     }
1104
1105   return const0_rtx;
1106 }
1107
1108 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1109    (not all will be used on all machines) that was passed to __builtin_setjmp.
1110    It updates the stack pointer in that block to correspond to the current
1111    stack pointer.  */
1112
1113 static void
1114 expand_builtin_update_setjmp_buf (rtx buf_addr)
1115 {
1116   enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1117   rtx stack_save
1118     = gen_rtx_MEM (sa_mode,
1119                    memory_address
1120                    (sa_mode,
1121                     plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1122
1123   emit_stack_save (SAVE_NONLOCAL, &stack_save);
1124 }
1125
1126 /* Expand a call to __builtin_prefetch.  For a target that does not support
1127    data prefetch, evaluate the memory address argument in case it has side
1128    effects.  */
1129
1130 static void
1131 expand_builtin_prefetch (tree exp)
1132 {
1133   tree arg0, arg1, arg2;
1134   int nargs;
1135   rtx op0, op1, op2;
1136
1137   if (!validate_arglist (exp, POINTER_TYPE, 0))
1138     return;
1139
1140   arg0 = CALL_EXPR_ARG (exp, 0);
1141
1142   /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1143      zero (read) and argument 2 (locality) defaults to 3 (high degree of
1144      locality).  */
1145   nargs = call_expr_nargs (exp);
1146   if (nargs > 1)
1147     arg1 = CALL_EXPR_ARG (exp, 1);
1148   else
1149     arg1 = integer_zero_node;
1150   if (nargs > 2)
1151     arg2 = CALL_EXPR_ARG (exp, 2);
1152   else
1153     arg2 = integer_three_node;
1154
1155   /* Argument 0 is an address.  */
1156   op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1157
1158   /* Argument 1 (read/write flag) must be a compile-time constant int.  */
1159   if (TREE_CODE (arg1) != INTEGER_CST)
1160     {
1161       error ("second argument to %<__builtin_prefetch%> must be a constant");
1162       arg1 = integer_zero_node;
1163     }
1164   op1 = expand_normal (arg1);
1165   /* Argument 1 must be either zero or one.  */
1166   if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1167     {
1168       warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1169                " using zero");
1170       op1 = const0_rtx;
1171     }
1172
1173   /* Argument 2 (locality) must be a compile-time constant int.  */
1174   if (TREE_CODE (arg2) != INTEGER_CST)
1175     {
1176       error ("third argument to %<__builtin_prefetch%> must be a constant");
1177       arg2 = integer_zero_node;
1178     }
1179   op2 = expand_normal (arg2);
1180   /* Argument 2 must be 0, 1, 2, or 3.  */
1181   if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1182     {
1183       warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1184       op2 = const0_rtx;
1185     }
1186
1187 #ifdef HAVE_prefetch
1188   if (HAVE_prefetch)
1189     {
1190       struct expand_operand ops[3];
1191
1192       create_address_operand (&ops[0], op0);
1193       create_integer_operand (&ops[1], INTVAL (op1));
1194       create_integer_operand (&ops[2], INTVAL (op2));
1195       if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1196         return;
1197     }
1198 #endif
1199
1200   /* Don't do anything with direct references to volatile memory, but
1201      generate code to handle other side effects.  */
1202   if (!MEM_P (op0) && side_effects_p (op0))
1203     emit_insn (op0);
1204 }
1205
1206 /* Get a MEM rtx for expression EXP which is the address of an operand
1207    to be used in a string instruction (cmpstrsi, movmemsi, ..).  LEN is
1208    the maximum length of the block of memory that might be accessed or
1209    NULL if unknown.  */
1210
1211 static rtx
1212 get_memory_rtx (tree exp, tree len)
1213 {
1214   tree orig_exp = exp;
1215   rtx addr, mem;
1216   HOST_WIDE_INT off;
1217
1218   /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1219      from its expression, for expr->a.b only <variable>.a.b is recorded.  */
1220   if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1221     exp = TREE_OPERAND (exp, 0);
1222
1223   addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1224   mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1225
1226   /* Get an expression we can use to find the attributes to assign to MEM.
1227      If it is an ADDR_EXPR, use the operand.  Otherwise, dereference it if
1228      we can.  First remove any nops.  */
1229   while (CONVERT_EXPR_P (exp)
1230          && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1231     exp = TREE_OPERAND (exp, 0);
1232
1233   off = 0;
1234   if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1235       && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1236       && host_integerp (TREE_OPERAND (exp, 1), 0)
1237       && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1238     exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1239   else if (TREE_CODE (exp) == ADDR_EXPR)
1240     exp = TREE_OPERAND (exp, 0);
1241   else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1242     exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1243   else
1244     exp = NULL;
1245
1246   /* Honor attributes derived from exp, except for the alias set
1247      (as builtin stringops may alias with anything) and the size
1248      (as stringops may access multiple array elements).  */
1249   if (exp)
1250     {
1251       set_mem_attributes (mem, exp, 0);
1252
1253       if (off)
1254         mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1255
1256       /* Allow the string and memory builtins to overflow from one
1257          field into another, see http://gcc.gnu.org/PR23561.
1258          Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1259          memory accessed by the string or memory builtin will fit
1260          within the field.  */
1261       if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1262         {
1263           tree mem_expr = MEM_EXPR (mem);
1264           HOST_WIDE_INT offset = -1, length = -1;
1265           tree inner = exp;
1266
1267           while (TREE_CODE (inner) == ARRAY_REF
1268                  || CONVERT_EXPR_P (inner)
1269                  || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1270                  || TREE_CODE (inner) == SAVE_EXPR)
1271             inner = TREE_OPERAND (inner, 0);
1272
1273           gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1274
1275           if (MEM_OFFSET_KNOWN_P (mem))
1276             offset = MEM_OFFSET (mem);
1277
1278           if (offset >= 0 && len && host_integerp (len, 0))
1279             length = tree_low_cst (len, 0);
1280
1281           while (TREE_CODE (inner) == COMPONENT_REF)
1282             {
1283               tree field = TREE_OPERAND (inner, 1);
1284               gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1285               gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1286
1287               /* Bitfields are generally not byte-addressable.  */
1288               gcc_assert (!DECL_BIT_FIELD (field)
1289                           || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1290                                % BITS_PER_UNIT) == 0
1291                               && host_integerp (DECL_SIZE (field), 0)
1292                               && (TREE_INT_CST_LOW (DECL_SIZE (field))
1293                                   % BITS_PER_UNIT) == 0));
1294
1295               /* If we can prove that the memory starting at XEXP (mem, 0) and
1296                  ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1297                  can keep the COMPONENT_REF in MEM_EXPR.  But be careful with
1298                  fields without DECL_SIZE_UNIT like flexible array members.  */
1299               if (length >= 0
1300                   && DECL_SIZE_UNIT (field)
1301                   && host_integerp (DECL_SIZE_UNIT (field), 0))
1302                 {
1303                   HOST_WIDE_INT size
1304                     = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1305                   if (offset <= size
1306                       && length <= size
1307                       && offset + length <= size)
1308                     break;
1309                 }
1310
1311               if (offset >= 0
1312                   && host_integerp (DECL_FIELD_OFFSET (field), 0))
1313                 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1314                           + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1315                             / BITS_PER_UNIT;
1316               else
1317                 {
1318                   offset = -1;
1319                   length = -1;
1320                 }
1321
1322               mem_expr = TREE_OPERAND (mem_expr, 0);
1323               inner = TREE_OPERAND (inner, 0);
1324             }
1325
1326           if (mem_expr == NULL)
1327             offset = -1;
1328           if (mem_expr != MEM_EXPR (mem))
1329             {
1330               set_mem_expr (mem, mem_expr);
1331               if (offset >= 0)
1332                 set_mem_offset (mem, offset);
1333               else
1334                 clear_mem_offset (mem);
1335             }
1336         }
1337       set_mem_alias_set (mem, 0);
1338       clear_mem_size (mem);
1339     }
1340
1341   return mem;
1342 }
1343 \f
1344 /* Built-in functions to perform an untyped call and return.  */
1345
1346 #define apply_args_mode \
1347   (this_target_builtins->x_apply_args_mode)
1348 #define apply_result_mode \
1349   (this_target_builtins->x_apply_result_mode)
1350
1351 /* Return the size required for the block returned by __builtin_apply_args,
1352    and initialize apply_args_mode.  */
1353
1354 static int
1355 apply_args_size (void)
1356 {
1357   static int size = -1;
1358   int align;
1359   unsigned int regno;
1360   enum machine_mode mode;
1361
1362   /* The values computed by this function never change.  */
1363   if (size < 0)
1364     {
1365       /* The first value is the incoming arg-pointer.  */
1366       size = GET_MODE_SIZE (Pmode);
1367
1368       /* The second value is the structure value address unless this is
1369          passed as an "invisible" first argument.  */
1370       if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1371         size += GET_MODE_SIZE (Pmode);
1372
1373       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1374         if (FUNCTION_ARG_REGNO_P (regno))
1375           {
1376             mode = targetm.calls.get_raw_arg_mode (regno);
1377
1378             gcc_assert (mode != VOIDmode);
1379
1380             align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1381             if (size % align != 0)
1382               size = CEIL (size, align) * align;
1383             size += GET_MODE_SIZE (mode);
1384             apply_args_mode[regno] = mode;
1385           }
1386         else
1387           {
1388             apply_args_mode[regno] = VOIDmode;
1389           }
1390     }
1391   return size;
1392 }
1393
1394 /* Return the size required for the block returned by __builtin_apply,
1395    and initialize apply_result_mode.  */
1396
1397 static int
1398 apply_result_size (void)
1399 {
1400   static int size = -1;
1401   int align, regno;
1402   enum machine_mode mode;
1403
1404   /* The values computed by this function never change.  */
1405   if (size < 0)
1406     {
1407       size = 0;
1408
1409       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1410         if (targetm.calls.function_value_regno_p (regno))
1411           {
1412             mode = targetm.calls.get_raw_result_mode (regno);
1413
1414             gcc_assert (mode != VOIDmode);
1415
1416             align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1417             if (size % align != 0)
1418               size = CEIL (size, align) * align;
1419             size += GET_MODE_SIZE (mode);
1420             apply_result_mode[regno] = mode;
1421           }
1422         else
1423           apply_result_mode[regno] = VOIDmode;
1424
1425       /* Allow targets that use untyped_call and untyped_return to override
1426          the size so that machine-specific information can be stored here.  */
1427 #ifdef APPLY_RESULT_SIZE
1428       size = APPLY_RESULT_SIZE;
1429 #endif
1430     }
1431   return size;
1432 }
1433
1434 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1435 /* Create a vector describing the result block RESULT.  If SAVEP is true,
1436    the result block is used to save the values; otherwise it is used to
1437    restore the values.  */
1438
1439 static rtx
1440 result_vector (int savep, rtx result)
1441 {
1442   int regno, size, align, nelts;
1443   enum machine_mode mode;
1444   rtx reg, mem;
1445   rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1446
1447   size = nelts = 0;
1448   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1449     if ((mode = apply_result_mode[regno]) != VOIDmode)
1450       {
1451         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1452         if (size % align != 0)
1453           size = CEIL (size, align) * align;
1454         reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1455         mem = adjust_address (result, mode, size);
1456         savevec[nelts++] = (savep
1457                             ? gen_rtx_SET (VOIDmode, mem, reg)
1458                             : gen_rtx_SET (VOIDmode, reg, mem));
1459         size += GET_MODE_SIZE (mode);
1460       }
1461   return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1462 }
1463 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1464
1465 /* Save the state required to perform an untyped call with the same
1466    arguments as were passed to the current function.  */
1467
1468 static rtx
1469 expand_builtin_apply_args_1 (void)
1470 {
1471   rtx registers, tem;
1472   int size, align, regno;
1473   enum machine_mode mode;
1474   rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1475
1476   /* Create a block where the arg-pointer, structure value address,
1477      and argument registers can be saved.  */
1478   registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1479
1480   /* Walk past the arg-pointer and structure value address.  */
1481   size = GET_MODE_SIZE (Pmode);
1482   if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1483     size += GET_MODE_SIZE (Pmode);
1484
1485   /* Save each register used in calling a function to the block.  */
1486   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1487     if ((mode = apply_args_mode[regno]) != VOIDmode)
1488       {
1489         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1490         if (size % align != 0)
1491           size = CEIL (size, align) * align;
1492
1493         tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1494
1495         emit_move_insn (adjust_address (registers, mode, size), tem);
1496         size += GET_MODE_SIZE (mode);
1497       }
1498
1499   /* Save the arg pointer to the block.  */
1500   tem = copy_to_reg (crtl->args.internal_arg_pointer);
1501 #ifdef STACK_GROWS_DOWNWARD
1502   /* We need the pointer as the caller actually passed them to us, not
1503      as we might have pretended they were passed.  Make sure it's a valid
1504      operand, as emit_move_insn isn't expected to handle a PLUS.  */
1505   tem
1506     = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1507                      NULL_RTX);
1508 #endif
1509   emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1510
1511   size = GET_MODE_SIZE (Pmode);
1512
1513   /* Save the structure value address unless this is passed as an
1514      "invisible" first argument.  */
1515   if (struct_incoming_value)
1516     {
1517       emit_move_insn (adjust_address (registers, Pmode, size),
1518                       copy_to_reg (struct_incoming_value));
1519       size += GET_MODE_SIZE (Pmode);
1520     }
1521
1522   /* Return the address of the block.  */
1523   return copy_addr_to_reg (XEXP (registers, 0));
1524 }
1525
1526 /* __builtin_apply_args returns block of memory allocated on
1527    the stack into which is stored the arg pointer, structure
1528    value address, static chain, and all the registers that might
1529    possibly be used in performing a function call.  The code is
1530    moved to the start of the function so the incoming values are
1531    saved.  */
1532
1533 static rtx
1534 expand_builtin_apply_args (void)
1535 {
1536   /* Don't do __builtin_apply_args more than once in a function.
1537      Save the result of the first call and reuse it.  */
1538   if (apply_args_value != 0)
1539     return apply_args_value;
1540   {
1541     /* When this function is called, it means that registers must be
1542        saved on entry to this function.  So we migrate the
1543        call to the first insn of this function.  */
1544     rtx temp;
1545     rtx seq;
1546
1547     start_sequence ();
1548     temp = expand_builtin_apply_args_1 ();
1549     seq = get_insns ();
1550     end_sequence ();
1551
1552     apply_args_value = temp;
1553
1554     /* Put the insns after the NOTE that starts the function.
1555        If this is inside a start_sequence, make the outer-level insn
1556        chain current, so the code is placed at the start of the
1557        function.  If internal_arg_pointer is a non-virtual pseudo,
1558        it needs to be placed after the function that initializes
1559        that pseudo.  */
1560     push_topmost_sequence ();
1561     if (REG_P (crtl->args.internal_arg_pointer)
1562         && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1563       emit_insn_before (seq, parm_birth_insn);
1564     else
1565       emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1566     pop_topmost_sequence ();
1567     return temp;
1568   }
1569 }
1570
1571 /* Perform an untyped call and save the state required to perform an
1572    untyped return of whatever value was returned by the given function.  */
1573
1574 static rtx
1575 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1576 {
1577   int size, align, regno;
1578   enum machine_mode mode;
1579   rtx incoming_args, result, reg, dest, src, call_insn;
1580   rtx old_stack_level = 0;
1581   rtx call_fusage = 0;
1582   rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1583
1584   arguments = convert_memory_address (Pmode, arguments);
1585
1586   /* Create a block where the return registers can be saved.  */
1587   result = assign_stack_local (BLKmode, apply_result_size (), -1);
1588
1589   /* Fetch the arg pointer from the ARGUMENTS block.  */
1590   incoming_args = gen_reg_rtx (Pmode);
1591   emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1592 #ifndef STACK_GROWS_DOWNWARD
1593   incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1594                                        incoming_args, 0, OPTAB_LIB_WIDEN);
1595 #endif
1596
1597   /* Push a new argument block and copy the arguments.  Do not allow
1598      the (potential) memcpy call below to interfere with our stack
1599      manipulations.  */
1600   do_pending_stack_adjust ();
1601   NO_DEFER_POP;
1602
1603   /* Save the stack with nonlocal if available.  */
1604 #ifdef HAVE_save_stack_nonlocal
1605   if (HAVE_save_stack_nonlocal)
1606     emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1607   else
1608 #endif
1609     emit_stack_save (SAVE_BLOCK, &old_stack_level);
1610
1611   /* Allocate a block of memory onto the stack and copy the memory
1612      arguments to the outgoing arguments address.  We can pass TRUE
1613      as the 4th argument because we just saved the stack pointer
1614      and will restore it right after the call.  */
1615   allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1616
1617   /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1618      may have already set current_function_calls_alloca to true.
1619      current_function_calls_alloca won't be set if argsize is zero,
1620      so we have to guarantee need_drap is true here.  */
1621   if (SUPPORTS_STACK_ALIGNMENT)
1622     crtl->need_drap = true;
1623
1624   dest = virtual_outgoing_args_rtx;
1625 #ifndef STACK_GROWS_DOWNWARD
1626   if (CONST_INT_P (argsize))
1627     dest = plus_constant (dest, -INTVAL (argsize));
1628   else
1629     dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1630 #endif
1631   dest = gen_rtx_MEM (BLKmode, dest);
1632   set_mem_align (dest, PARM_BOUNDARY);
1633   src = gen_rtx_MEM (BLKmode, incoming_args);
1634   set_mem_align (src, PARM_BOUNDARY);
1635   emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1636
1637   /* Refer to the argument block.  */
1638   apply_args_size ();
1639   arguments = gen_rtx_MEM (BLKmode, arguments);
1640   set_mem_align (arguments, PARM_BOUNDARY);
1641
1642   /* Walk past the arg-pointer and structure value address.  */
1643   size = GET_MODE_SIZE (Pmode);
1644   if (struct_value)
1645     size += GET_MODE_SIZE (Pmode);
1646
1647   /* Restore each of the registers previously saved.  Make USE insns
1648      for each of these registers for use in making the call.  */
1649   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1650     if ((mode = apply_args_mode[regno]) != VOIDmode)
1651       {
1652         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1653         if (size % align != 0)
1654           size = CEIL (size, align) * align;
1655         reg = gen_rtx_REG (mode, regno);
1656         emit_move_insn (reg, adjust_address (arguments, mode, size));
1657         use_reg (&call_fusage, reg);
1658         size += GET_MODE_SIZE (mode);
1659       }
1660
1661   /* Restore the structure value address unless this is passed as an
1662      "invisible" first argument.  */
1663   size = GET_MODE_SIZE (Pmode);
1664   if (struct_value)
1665     {
1666       rtx value = gen_reg_rtx (Pmode);
1667       emit_move_insn (value, adjust_address (arguments, Pmode, size));
1668       emit_move_insn (struct_value, value);
1669       if (REG_P (struct_value))
1670         use_reg (&call_fusage, struct_value);
1671       size += GET_MODE_SIZE (Pmode);
1672     }
1673
1674   /* All arguments and registers used for the call are set up by now!  */
1675   function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1676
1677   /* Ensure address is valid.  SYMBOL_REF is already valid, so no need,
1678      and we don't want to load it into a register as an optimization,
1679      because prepare_call_address already did it if it should be done.  */
1680   if (GET_CODE (function) != SYMBOL_REF)
1681     function = memory_address (FUNCTION_MODE, function);
1682
1683   /* Generate the actual call instruction and save the return value.  */
1684 #ifdef HAVE_untyped_call
1685   if (HAVE_untyped_call)
1686     emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1687                                       result, result_vector (1, result)));
1688   else
1689 #endif
1690 #ifdef HAVE_call_value
1691   if (HAVE_call_value)
1692     {
1693       rtx valreg = 0;
1694
1695       /* Locate the unique return register.  It is not possible to
1696          express a call that sets more than one return register using
1697          call_value; use untyped_call for that.  In fact, untyped_call
1698          only needs to save the return registers in the given block.  */
1699       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1700         if ((mode = apply_result_mode[regno]) != VOIDmode)
1701           {
1702             gcc_assert (!valreg); /* HAVE_untyped_call required.  */
1703
1704             valreg = gen_rtx_REG (mode, regno);
1705           }
1706
1707       emit_call_insn (GEN_CALL_VALUE (valreg,
1708                                       gen_rtx_MEM (FUNCTION_MODE, function),
1709                                       const0_rtx, NULL_RTX, const0_rtx));
1710
1711       emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1712     }
1713   else
1714 #endif
1715     gcc_unreachable ();
1716
1717   /* Find the CALL insn we just emitted, and attach the register usage
1718      information.  */
1719   call_insn = last_call_insn ();
1720   add_function_usage_to (call_insn, call_fusage);
1721
1722   /* Restore the stack.  */
1723 #ifdef HAVE_save_stack_nonlocal
1724   if (HAVE_save_stack_nonlocal)
1725     emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1726   else
1727 #endif
1728     emit_stack_restore (SAVE_BLOCK, old_stack_level);
1729   fixup_args_size_notes (call_insn, get_last_insn(), 0);
1730
1731   OK_DEFER_POP;
1732
1733   /* Return the address of the result block.  */
1734   result = copy_addr_to_reg (XEXP (result, 0));
1735   return convert_memory_address (ptr_mode, result);
1736 }
1737
1738 /* Perform an untyped return.  */
1739
1740 static void
1741 expand_builtin_return (rtx result)
1742 {
1743   int size, align, regno;
1744   enum machine_mode mode;
1745   rtx reg;
1746   rtx call_fusage = 0;
1747
1748   result = convert_memory_address (Pmode, result);
1749
1750   apply_result_size ();
1751   result = gen_rtx_MEM (BLKmode, result);
1752
1753 #ifdef HAVE_untyped_return
1754   if (HAVE_untyped_return)
1755     {
1756       emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1757       emit_barrier ();
1758       return;
1759     }
1760 #endif
1761
1762   /* Restore the return value and note that each value is used.  */
1763   size = 0;
1764   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1765     if ((mode = apply_result_mode[regno]) != VOIDmode)
1766       {
1767         align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1768         if (size % align != 0)
1769           size = CEIL (size, align) * align;
1770         reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1771         emit_move_insn (reg, adjust_address (result, mode, size));
1772
1773         push_to_sequence (call_fusage);
1774         emit_use (reg);
1775         call_fusage = get_insns ();
1776         end_sequence ();
1777         size += GET_MODE_SIZE (mode);
1778       }
1779
1780   /* Put the USE insns before the return.  */
1781   emit_insn (call_fusage);
1782
1783   /* Return whatever values was restored by jumping directly to the end
1784      of the function.  */
1785   expand_naked_return ();
1786 }
1787
1788 /* Used by expand_builtin_classify_type and fold_builtin_classify_type.  */
1789
1790 static enum type_class
1791 type_to_class (tree type)
1792 {
1793   switch (TREE_CODE (type))
1794     {
1795     case VOID_TYPE:        return void_type_class;
1796     case INTEGER_TYPE:     return integer_type_class;
1797     case ENUMERAL_TYPE:    return enumeral_type_class;
1798     case BOOLEAN_TYPE:     return boolean_type_class;
1799     case POINTER_TYPE:     return pointer_type_class;
1800     case REFERENCE_TYPE:   return reference_type_class;
1801     case OFFSET_TYPE:      return offset_type_class;
1802     case REAL_TYPE:        return real_type_class;
1803     case COMPLEX_TYPE:     return complex_type_class;
1804     case FUNCTION_TYPE:    return function_type_class;
1805     case METHOD_TYPE:      return method_type_class;
1806     case RECORD_TYPE:      return record_type_class;
1807     case UNION_TYPE:
1808     case QUAL_UNION_TYPE:  return union_type_class;
1809     case ARRAY_TYPE:       return (TYPE_STRING_FLAG (type)
1810                                    ? string_type_class : array_type_class);
1811     case LANG_TYPE:        return lang_type_class;
1812     default:               return no_type_class;
1813     }
1814 }
1815
1816 /* Expand a call EXP to __builtin_classify_type.  */
1817
1818 static rtx
1819 expand_builtin_classify_type (tree exp)
1820 {
1821   if (call_expr_nargs (exp))
1822     return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1823   return GEN_INT (no_type_class);
1824 }
1825
1826 /* This helper macro, meant to be used in mathfn_built_in below,
1827    determines which among a set of three builtin math functions is
1828    appropriate for a given type mode.  The `F' and `L' cases are
1829    automatically generated from the `double' case.  */
1830 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1831   case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1832   fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1833   fcodel = BUILT_IN_MATHFN##L ; break;
1834 /* Similar to above, but appends _R after any F/L suffix.  */
1835 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1836   case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1837   fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1838   fcodel = BUILT_IN_MATHFN##L_R ; break;
1839
1840 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1841    if available.  If IMPLICIT is true use the implicit builtin declaration,
1842    otherwise use the explicit declaration.  If we can't do the conversion,
1843    return zero.  */
1844
1845 static tree
1846 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1847 {
1848   enum built_in_function fcode, fcodef, fcodel, fcode2;
1849
1850   switch (fn)
1851     {
1852       CASE_MATHFN (BUILT_IN_ACOS)
1853       CASE_MATHFN (BUILT_IN_ACOSH)
1854       CASE_MATHFN (BUILT_IN_ASIN)
1855       CASE_MATHFN (BUILT_IN_ASINH)
1856       CASE_MATHFN (BUILT_IN_ATAN)
1857       CASE_MATHFN (BUILT_IN_ATAN2)
1858       CASE_MATHFN (BUILT_IN_ATANH)
1859       CASE_MATHFN (BUILT_IN_CBRT)
1860       CASE_MATHFN (BUILT_IN_CEIL)
1861       CASE_MATHFN (BUILT_IN_CEXPI)
1862       CASE_MATHFN (BUILT_IN_COPYSIGN)
1863       CASE_MATHFN (BUILT_IN_COS)
1864       CASE_MATHFN (BUILT_IN_COSH)
1865       CASE_MATHFN (BUILT_IN_DREM)
1866       CASE_MATHFN (BUILT_IN_ERF)
1867       CASE_MATHFN (BUILT_IN_ERFC)
1868       CASE_MATHFN (BUILT_IN_EXP)
1869       CASE_MATHFN (BUILT_IN_EXP10)
1870       CASE_MATHFN (BUILT_IN_EXP2)
1871       CASE_MATHFN (BUILT_IN_EXPM1)
1872       CASE_MATHFN (BUILT_IN_FABS)
1873       CASE_MATHFN (BUILT_IN_FDIM)
1874       CASE_MATHFN (BUILT_IN_FLOOR)
1875       CASE_MATHFN (BUILT_IN_FMA)
1876       CASE_MATHFN (BUILT_IN_FMAX)
1877       CASE_MATHFN (BUILT_IN_FMIN)
1878       CASE_MATHFN (BUILT_IN_FMOD)
1879       CASE_MATHFN (BUILT_IN_FREXP)
1880       CASE_MATHFN (BUILT_IN_GAMMA)
1881       CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1882       CASE_MATHFN (BUILT_IN_HUGE_VAL)
1883       CASE_MATHFN (BUILT_IN_HYPOT)
1884       CASE_MATHFN (BUILT_IN_ILOGB)
1885       CASE_MATHFN (BUILT_IN_ICEIL)
1886       CASE_MATHFN (BUILT_IN_IFLOOR)
1887       CASE_MATHFN (BUILT_IN_INF)
1888       CASE_MATHFN (BUILT_IN_IRINT)
1889       CASE_MATHFN (BUILT_IN_IROUND)
1890       CASE_MATHFN (BUILT_IN_ISINF)
1891       CASE_MATHFN (BUILT_IN_J0)
1892       CASE_MATHFN (BUILT_IN_J1)
1893       CASE_MATHFN (BUILT_IN_JN)
1894       CASE_MATHFN (BUILT_IN_LCEIL)
1895       CASE_MATHFN (BUILT_IN_LDEXP)
1896       CASE_MATHFN (BUILT_IN_LFLOOR)
1897       CASE_MATHFN (BUILT_IN_LGAMMA)
1898       CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1899       CASE_MATHFN (BUILT_IN_LLCEIL)
1900       CASE_MATHFN (BUILT_IN_LLFLOOR)
1901       CASE_MATHFN (BUILT_IN_LLRINT)
1902       CASE_MATHFN (BUILT_IN_LLROUND)
1903       CASE_MATHFN (BUILT_IN_LOG)
1904       CASE_MATHFN (BUILT_IN_LOG10)
1905       CASE_MATHFN (BUILT_IN_LOG1P)
1906       CASE_MATHFN (BUILT_IN_LOG2)
1907       CASE_MATHFN (BUILT_IN_LOGB)
1908       CASE_MATHFN (BUILT_IN_LRINT)
1909       CASE_MATHFN (BUILT_IN_LROUND)
1910       CASE_MATHFN (BUILT_IN_MODF)
1911       CASE_MATHFN (BUILT_IN_NAN)
1912       CASE_MATHFN (BUILT_IN_NANS)
1913       CASE_MATHFN (BUILT_IN_NEARBYINT)
1914       CASE_MATHFN (BUILT_IN_NEXTAFTER)
1915       CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1916       CASE_MATHFN (BUILT_IN_POW)
1917       CASE_MATHFN (BUILT_IN_POWI)
1918       CASE_MATHFN (BUILT_IN_POW10)
1919       CASE_MATHFN (BUILT_IN_REMAINDER)
1920       CASE_MATHFN (BUILT_IN_REMQUO)
1921       CASE_MATHFN (BUILT_IN_RINT)
1922       CASE_MATHFN (BUILT_IN_ROUND)
1923       CASE_MATHFN (BUILT_IN_SCALB)
1924       CASE_MATHFN (BUILT_IN_SCALBLN)
1925       CASE_MATHFN (BUILT_IN_SCALBN)
1926       CASE_MATHFN (BUILT_IN_SIGNBIT)
1927       CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1928       CASE_MATHFN (BUILT_IN_SIN)
1929       CASE_MATHFN (BUILT_IN_SINCOS)
1930       CASE_MATHFN (BUILT_IN_SINH)
1931       CASE_MATHFN (BUILT_IN_SQRT)
1932       CASE_MATHFN (BUILT_IN_TAN)
1933       CASE_MATHFN (BUILT_IN_TANH)
1934       CASE_MATHFN (BUILT_IN_TGAMMA)
1935       CASE_MATHFN (BUILT_IN_TRUNC)
1936       CASE_MATHFN (BUILT_IN_Y0)
1937       CASE_MATHFN (BUILT_IN_Y1)
1938       CASE_MATHFN (BUILT_IN_YN)
1939
1940       default:
1941         return NULL_TREE;
1942       }
1943
1944   if (TYPE_MAIN_VARIANT (type) == double_type_node)
1945     fcode2 = fcode;
1946   else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1947     fcode2 = fcodef;
1948   else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1949     fcode2 = fcodel;
1950   else
1951     return NULL_TREE;
1952
1953   if (implicit_p && !builtin_decl_implicit_p (fcode2))
1954     return NULL_TREE;
1955
1956   return builtin_decl_explicit (fcode2);
1957 }
1958
1959 /* Like mathfn_built_in_1(), but always use the implicit array.  */
1960
1961 tree
1962 mathfn_built_in (tree type, enum built_in_function fn)
1963 {
1964   return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1965 }
1966
1967 /* If errno must be maintained, expand the RTL to check if the result,
1968    TARGET, of a built-in function call, EXP, is NaN, and if so set
1969    errno to EDOM.  */
1970
1971 static void
1972 expand_errno_check (tree exp, rtx target)
1973 {
1974   rtx lab = gen_label_rtx ();
1975
1976   /* Test the result; if it is NaN, set errno=EDOM because
1977      the argument was not in the domain.  */
1978   do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1979                            NULL_RTX, NULL_RTX, lab,
1980                            /* The jump is very likely.  */
1981                            REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1982
1983 #ifdef TARGET_EDOM
1984   /* If this built-in doesn't throw an exception, set errno directly.  */
1985   if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1986     {
1987 #ifdef GEN_ERRNO_RTX
1988       rtx errno_rtx = GEN_ERRNO_RTX;
1989 #else
1990       rtx errno_rtx
1991           = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1992 #endif
1993       emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1994       emit_label (lab);
1995       return;
1996     }
1997 #endif
1998
1999   /* Make sure the library call isn't expanded as a tail call.  */
2000   CALL_EXPR_TAILCALL (exp) = 0;
2001
2002   /* We can't set errno=EDOM directly; let the library call do it.
2003      Pop the arguments right away in case the call gets deleted.  */
2004   NO_DEFER_POP;
2005   expand_call (exp, target, 0);
2006   OK_DEFER_POP;
2007   emit_label (lab);
2008 }
2009
2010 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2011    Return NULL_RTX if a normal call should be emitted rather than expanding
2012    the function in-line.  EXP is the expression that is a call to the builtin
2013    function; if convenient, the result should be placed in TARGET.
2014    SUBTARGET may be used as the target for computing one of EXP's operands.  */
2015
2016 static rtx
2017 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2018 {
2019   optab builtin_optab;
2020   rtx op0, insns;
2021   tree fndecl = get_callee_fndecl (exp);
2022   enum machine_mode mode;
2023   bool errno_set = false;
2024   tree arg;
2025
2026   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2027     return NULL_RTX;
2028
2029   arg = CALL_EXPR_ARG (exp, 0);
2030
2031   switch (DECL_FUNCTION_CODE (fndecl))
2032     {
2033     CASE_FLT_FN (BUILT_IN_SQRT):
2034       errno_set = ! tree_expr_nonnegative_p (arg);
2035       builtin_optab = sqrt_optab;
2036       break;
2037     CASE_FLT_FN (BUILT_IN_EXP):
2038       errno_set = true; builtin_optab = exp_optab; break;
2039     CASE_FLT_FN (BUILT_IN_EXP10):
2040     CASE_FLT_FN (BUILT_IN_POW10):
2041       errno_set = true; builtin_optab = exp10_optab; break;
2042     CASE_FLT_FN (BUILT_IN_EXP2):
2043       errno_set = true; builtin_optab = exp2_optab; break;
2044     CASE_FLT_FN (BUILT_IN_EXPM1):
2045       errno_set = true; builtin_optab = expm1_optab; break;
2046     CASE_FLT_FN (BUILT_IN_LOGB):
2047       errno_set = true; builtin_optab = logb_optab; break;
2048     CASE_FLT_FN (BUILT_IN_LOG):
2049       errno_set = true; builtin_optab = log_optab; break;
2050     CASE_FLT_FN (BUILT_IN_LOG10):
2051       errno_set = true; builtin_optab = log10_optab; break;
2052     CASE_FLT_FN (BUILT_IN_LOG2):
2053       errno_set = true; builtin_optab = log2_optab; break;
2054     CASE_FLT_FN (BUILT_IN_LOG1P):
2055       errno_set = true; builtin_optab = log1p_optab; break;
2056     CASE_FLT_FN (BUILT_IN_ASIN):
2057       builtin_optab = asin_optab; break;
2058     CASE_FLT_FN (BUILT_IN_ACOS):
2059       builtin_optab = acos_optab; break;
2060     CASE_FLT_FN (BUILT_IN_TAN):
2061       builtin_optab = tan_optab; break;
2062     CASE_FLT_FN (BUILT_IN_ATAN):
2063       builtin_optab = atan_optab; break;
2064     CASE_FLT_FN (BUILT_IN_FLOOR):
2065       builtin_optab = floor_optab; break;
2066     CASE_FLT_FN (BUILT_IN_CEIL):
2067       builtin_optab = ceil_optab; break;
2068     CASE_FLT_FN (BUILT_IN_TRUNC):
2069       builtin_optab = btrunc_optab; break;
2070     CASE_FLT_FN (BUILT_IN_ROUND):
2071       builtin_optab = round_optab; break;
2072     CASE_FLT_FN (BUILT_IN_NEARBYINT):
2073       builtin_optab = nearbyint_optab;
2074       if (flag_trapping_math)
2075         break;
2076       /* Else fallthrough and expand as rint.  */
2077     CASE_FLT_FN (BUILT_IN_RINT):
2078       builtin_optab = rint_optab; break;
2079     CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2080       builtin_optab = significand_optab; break;
2081     default:
2082       gcc_unreachable ();
2083     }
2084
2085   /* Make a suitable register to place result in.  */
2086   mode = TYPE_MODE (TREE_TYPE (exp));
2087
2088   if (! flag_errno_math || ! HONOR_NANS (mode))
2089     errno_set = false;
2090
2091   /* Before working hard, check whether the instruction is available.  */
2092   if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2093       && (!errno_set || !optimize_insn_for_size_p ()))
2094     {
2095       target = gen_reg_rtx (mode);
2096
2097       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2098          need to expand the argument again.  This way, we will not perform
2099          side-effects more the once.  */
2100       CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2101
2102       op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2103
2104       start_sequence ();
2105
2106       /* Compute into TARGET.
2107          Set TARGET to wherever the result comes back.  */
2108       target = expand_unop (mode, builtin_optab, op0, target, 0);
2109
2110       if (target != 0)
2111         {
2112           if (errno_set)
2113             expand_errno_check (exp, target);
2114
2115           /* Output the entire sequence.  */
2116           insns = get_insns ();
2117           end_sequence ();
2118           emit_insn (insns);
2119           return target;
2120         }
2121
2122       /* If we were unable to expand via the builtin, stop the sequence
2123          (without outputting the insns) and call to the library function
2124          with the stabilized argument list.  */
2125       end_sequence ();
2126     }
2127
2128   return expand_call (exp, target, target == const0_rtx);
2129 }
2130
2131 /* Expand a call to the builtin binary math functions (pow and atan2).
2132    Return NULL_RTX if a normal call should be emitted rather than expanding the
2133    function in-line.  EXP is the expression that is a call to the builtin
2134    function; if convenient, the result should be placed in TARGET.
2135    SUBTARGET may be used as the target for computing one of EXP's
2136    operands.  */
2137
2138 static rtx
2139 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2140 {
2141   optab builtin_optab;
2142   rtx op0, op1, insns;
2143   int op1_type = REAL_TYPE;
2144   tree fndecl = get_callee_fndecl (exp);
2145   tree arg0, arg1;
2146   enum machine_mode mode;
2147   bool errno_set = true;
2148
2149   switch (DECL_FUNCTION_CODE (fndecl))
2150     {
2151     CASE_FLT_FN (BUILT_IN_SCALBN):
2152     CASE_FLT_FN (BUILT_IN_SCALBLN):
2153     CASE_FLT_FN (BUILT_IN_LDEXP):
2154       op1_type = INTEGER_TYPE;
2155     default:
2156       break;
2157     }
2158
2159   if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2160     return NULL_RTX;
2161
2162   arg0 = CALL_EXPR_ARG (exp, 0);
2163   arg1 = CALL_EXPR_ARG (exp, 1);
2164
2165   switch (DECL_FUNCTION_CODE (fndecl))
2166     {
2167     CASE_FLT_FN (BUILT_IN_POW):
2168       builtin_optab = pow_optab; break;
2169     CASE_FLT_FN (BUILT_IN_ATAN2):
2170       builtin_optab = atan2_optab; break;
2171     CASE_FLT_FN (BUILT_IN_SCALB):
2172       if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2173         return 0;
2174       builtin_optab = scalb_optab; break;
2175     CASE_FLT_FN (BUILT_IN_SCALBN):
2176     CASE_FLT_FN (BUILT_IN_SCALBLN):
2177       if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2178         return 0;
2179     /* Fall through... */
2180     CASE_FLT_FN (BUILT_IN_LDEXP):
2181       builtin_optab = ldexp_optab; break;
2182     CASE_FLT_FN (BUILT_IN_FMOD):
2183       builtin_optab = fmod_optab; break;
2184     CASE_FLT_FN (BUILT_IN_REMAINDER):
2185     CASE_FLT_FN (BUILT_IN_DREM):
2186       builtin_optab = remainder_optab; break;
2187     default:
2188       gcc_unreachable ();
2189     }
2190
2191   /* Make a suitable register to place result in.  */
2192   mode = TYPE_MODE (TREE_TYPE (exp));
2193
2194   /* Before working hard, check whether the instruction is available.  */
2195   if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2196     return NULL_RTX;
2197
2198   target = gen_reg_rtx (mode);
2199
2200   if (! flag_errno_math || ! HONOR_NANS (mode))
2201     errno_set = false;
2202
2203   if (errno_set && optimize_insn_for_size_p ())
2204     return 0;
2205
2206   /* Always stabilize the argument list.  */
2207   CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2208   CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2209
2210   op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2211   op1 = expand_normal (arg1);
2212
2213   start_sequence ();
2214
2215   /* Compute into TARGET.
2216      Set TARGET to wherever the result comes back.  */
2217   target = expand_binop (mode, builtin_optab, op0, op1,
2218                          target, 0, OPTAB_DIRECT);
2219
2220   /* If we were unable to expand via the builtin, stop the sequence
2221      (without outputting the insns) and call to the library function
2222      with the stabilized argument list.  */
2223   if (target == 0)
2224     {
2225       end_sequence ();
2226       return expand_call (exp, target, target == const0_rtx);
2227     }
2228
2229   if (errno_set)
2230     expand_errno_check (exp, target);
2231
2232   /* Output the entire sequence.  */
2233   insns = get_insns ();
2234   end_sequence ();
2235   emit_insn (insns);
2236
2237   return target;
2238 }
2239
2240 /* Expand a call to the builtin trinary math functions (fma).
2241    Return NULL_RTX if a normal call should be emitted rather than expanding the
2242    function in-line.  EXP is the expression that is a call to the builtin
2243    function; if convenient, the result should be placed in TARGET.
2244    SUBTARGET may be used as the target for computing one of EXP's
2245    operands.  */
2246
2247 static rtx
2248 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2249 {
2250   optab builtin_optab;
2251   rtx op0, op1, op2, insns;
2252   tree fndecl = get_callee_fndecl (exp);
2253   tree arg0, arg1, arg2;
2254   enum machine_mode mode;
2255
2256   if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2257     return NULL_RTX;
2258
2259   arg0 = CALL_EXPR_ARG (exp, 0);
2260   arg1 = CALL_EXPR_ARG (exp, 1);
2261   arg2 = CALL_EXPR_ARG (exp, 2);
2262
2263   switch (DECL_FUNCTION_CODE (fndecl))
2264     {
2265     CASE_FLT_FN (BUILT_IN_FMA):
2266       builtin_optab = fma_optab; break;
2267     default:
2268       gcc_unreachable ();
2269     }
2270
2271   /* Make a suitable register to place result in.  */
2272   mode = TYPE_MODE (TREE_TYPE (exp));
2273
2274   /* Before working hard, check whether the instruction is available.  */
2275   if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2276     return NULL_RTX;
2277
2278   target = gen_reg_rtx (mode);
2279
2280   /* Always stabilize the argument list.  */
2281   CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2282   CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2283   CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2284
2285   op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2286   op1 = expand_normal (arg1);
2287   op2 = expand_normal (arg2);
2288
2289   start_sequence ();
2290
2291   /* Compute into TARGET.
2292      Set TARGET to wherever the result comes back.  */
2293   target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2294                               target, 0);
2295
2296   /* If we were unable to expand via the builtin, stop the sequence
2297      (without outputting the insns) and call to the library function
2298      with the stabilized argument list.  */
2299   if (target == 0)
2300     {
2301       end_sequence ();
2302       return expand_call (exp, target, target == const0_rtx);
2303     }
2304
2305   /* Output the entire sequence.  */
2306   insns = get_insns ();
2307   end_sequence ();
2308   emit_insn (insns);
2309
2310   return target;
2311 }
2312
2313 /* Expand a call to the builtin sin and cos math functions.
2314    Return NULL_RTX if a normal call should be emitted rather than expanding the
2315    function in-line.  EXP is the expression that is a call to the builtin
2316    function; if convenient, the result should be placed in TARGET.
2317    SUBTARGET may be used as the target for computing one of EXP's
2318    operands.  */
2319
2320 static rtx
2321 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2322 {
2323   optab builtin_optab;
2324   rtx op0, insns;
2325   tree fndecl = get_callee_fndecl (exp);
2326   enum machine_mode mode;
2327   tree arg;
2328
2329   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2330     return NULL_RTX;
2331
2332   arg = CALL_EXPR_ARG (exp, 0);
2333
2334   switch (DECL_FUNCTION_CODE (fndecl))
2335     {
2336     CASE_FLT_FN (BUILT_IN_SIN):
2337     CASE_FLT_FN (BUILT_IN_COS):
2338       builtin_optab = sincos_optab; break;
2339     default:
2340       gcc_unreachable ();
2341     }
2342
2343   /* Make a suitable register to place result in.  */
2344   mode = TYPE_MODE (TREE_TYPE (exp));
2345
2346   /* Check if sincos insn is available, otherwise fallback
2347      to sin or cos insn.  */
2348   if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2349     switch (DECL_FUNCTION_CODE (fndecl))
2350       {
2351       CASE_FLT_FN (BUILT_IN_SIN):
2352         builtin_optab = sin_optab; break;
2353       CASE_FLT_FN (BUILT_IN_COS):
2354         builtin_optab = cos_optab; break;
2355       default:
2356         gcc_unreachable ();
2357       }
2358
2359   /* Before working hard, check whether the instruction is available.  */
2360   if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2361     {
2362       target = gen_reg_rtx (mode);
2363
2364       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2365          need to expand the argument again.  This way, we will not perform
2366          side-effects more the once.  */
2367       CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2368
2369       op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2370
2371       start_sequence ();
2372
2373       /* Compute into TARGET.
2374          Set TARGET to wherever the result comes back.  */
2375       if (builtin_optab == sincos_optab)
2376         {
2377           int result;
2378
2379           switch (DECL_FUNCTION_CODE (fndecl))
2380             {
2381             CASE_FLT_FN (BUILT_IN_SIN):
2382               result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2383               break;
2384             CASE_FLT_FN (BUILT_IN_COS):
2385               result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2386               break;
2387             default:
2388               gcc_unreachable ();
2389             }
2390           gcc_assert (result);
2391         }
2392       else
2393         {
2394           target = expand_unop (mode, builtin_optab, op0, target, 0);
2395         }
2396
2397       if (target != 0)
2398         {
2399           /* Output the entire sequence.  */
2400           insns = get_insns ();
2401           end_sequence ();
2402           emit_insn (insns);
2403           return target;
2404         }
2405
2406       /* If we were unable to expand via the builtin, stop the sequence
2407          (without outputting the insns) and call to the library function
2408          with the stabilized argument list.  */
2409       end_sequence ();
2410     }
2411
2412   target = expand_call (exp, target, target == const0_rtx);
2413
2414   return target;
2415 }
2416
2417 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2418    return an RTL instruction code that implements the functionality.
2419    If that isn't possible or available return CODE_FOR_nothing.  */
2420
2421 static enum insn_code
2422 interclass_mathfn_icode (tree arg, tree fndecl)
2423 {
2424   bool errno_set = false;
2425   optab builtin_optab = 0;
2426   enum machine_mode mode;
2427
2428   switch (DECL_FUNCTION_CODE (fndecl))
2429     {
2430     CASE_FLT_FN (BUILT_IN_ILOGB):
2431       errno_set = true; builtin_optab = ilogb_optab; break;
2432     CASE_FLT_FN (BUILT_IN_ISINF):
2433       builtin_optab = isinf_optab; break;
2434     case BUILT_IN_ISNORMAL:
2435     case BUILT_IN_ISFINITE:
2436     CASE_FLT_FN (BUILT_IN_FINITE):
2437     case BUILT_IN_FINITED32:
2438     case BUILT_IN_FINITED64:
2439     case BUILT_IN_FINITED128:
2440     case BUILT_IN_ISINFD32:
2441     case BUILT_IN_ISINFD64:
2442     case BUILT_IN_ISINFD128:
2443       /* These builtins have no optabs (yet).  */
2444       break;
2445     default:
2446       gcc_unreachable ();
2447     }
2448
2449   /* There's no easy way to detect the case we need to set EDOM.  */
2450   if (flag_errno_math && errno_set)
2451     return CODE_FOR_nothing;
2452
2453   /* Optab mode depends on the mode of the input argument.  */
2454   mode = TYPE_MODE (TREE_TYPE (arg));
2455
2456   if (builtin_optab)
2457     return optab_handler (builtin_optab, mode);
2458   return CODE_FOR_nothing;
2459 }
2460
2461 /* Expand a call to one of the builtin math functions that operate on
2462    floating point argument and output an integer result (ilogb, isinf,
2463    isnan, etc).
2464    Return 0 if a normal call should be emitted rather than expanding the
2465    function in-line.  EXP is the expression that is a call to the builtin
2466    function; if convenient, the result should be placed in TARGET.  */
2467
2468 static rtx
2469 expand_builtin_interclass_mathfn (tree exp, rtx target)
2470 {
2471   enum insn_code icode = CODE_FOR_nothing;
2472   rtx op0;
2473   tree fndecl = get_callee_fndecl (exp);
2474   enum machine_mode mode;
2475   tree arg;
2476
2477   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2478     return NULL_RTX;
2479
2480   arg = CALL_EXPR_ARG (exp, 0);
2481   icode = interclass_mathfn_icode (arg, fndecl);
2482   mode = TYPE_MODE (TREE_TYPE (arg));
2483
2484   if (icode != CODE_FOR_nothing)
2485     {
2486       struct expand_operand ops[1];
2487       rtx last = get_last_insn ();
2488       tree orig_arg = arg;
2489
2490       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2491          need to expand the argument again.  This way, we will not perform
2492          side-effects more the once.  */
2493       CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2494
2495       op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2496
2497       if (mode != GET_MODE (op0))
2498         op0 = convert_to_mode (mode, op0, 0);
2499
2500       create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2501       if (maybe_legitimize_operands (icode, 0, 1, ops)
2502           && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2503         return ops[0].value;
2504
2505       delete_insns_since (last);
2506       CALL_EXPR_ARG (exp, 0) = orig_arg;
2507     }
2508
2509   return NULL_RTX;
2510 }
2511
2512 /* Expand a call to the builtin sincos math function.
2513    Return NULL_RTX if a normal call should be emitted rather than expanding the
2514    function in-line.  EXP is the expression that is a call to the builtin
2515    function.  */
2516
2517 static rtx
2518 expand_builtin_sincos (tree exp)
2519 {
2520   rtx op0, op1, op2, target1, target2;
2521   enum machine_mode mode;
2522   tree arg, sinp, cosp;
2523   int result;
2524   location_t loc = EXPR_LOCATION (exp);
2525   tree alias_type, alias_off;
2526
2527   if (!validate_arglist (exp, REAL_TYPE,
2528                          POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2529     return NULL_RTX;
2530
2531   arg = CALL_EXPR_ARG (exp, 0);
2532   sinp = CALL_EXPR_ARG (exp, 1);
2533   cosp = CALL_EXPR_ARG (exp, 2);
2534
2535   /* Make a suitable register to place result in.  */
2536   mode = TYPE_MODE (TREE_TYPE (arg));
2537
2538   /* Check if sincos insn is available, otherwise emit the call.  */
2539   if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2540     return NULL_RTX;
2541
2542   target1 = gen_reg_rtx (mode);
2543   target2 = gen_reg_rtx (mode);
2544
2545   op0 = expand_normal (arg);
2546   alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2547   alias_off = build_int_cst (alias_type, 0);
2548   op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2549                                         sinp, alias_off));
2550   op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2551                                         cosp, alias_off));
2552
2553   /* Compute into target1 and target2.
2554      Set TARGET to wherever the result comes back.  */
2555   result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2556   gcc_assert (result);
2557
2558   /* Move target1 and target2 to the memory locations indicated
2559      by op1 and op2.  */
2560   emit_move_insn (op1, target1);
2561   emit_move_insn (op2, target2);
2562
2563   return const0_rtx;
2564 }
2565
2566 /* Expand a call to the internal cexpi builtin to the sincos math function.
2567    EXP is the expression that is a call to the builtin function; if convenient,
2568    the result should be placed in TARGET.  */
2569
2570 static rtx
2571 expand_builtin_cexpi (tree exp, rtx target)
2572 {
2573   tree fndecl = get_callee_fndecl (exp);
2574   tree arg, type;
2575   enum machine_mode mode;
2576   rtx op0, op1, op2;
2577   location_t loc = EXPR_LOCATION (exp);
2578
2579   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2580     return NULL_RTX;
2581
2582   arg = CALL_EXPR_ARG (exp, 0);
2583   type = TREE_TYPE (arg);
2584   mode = TYPE_MODE (TREE_TYPE (arg));
2585
2586   /* Try expanding via a sincos optab, fall back to emitting a libcall
2587      to sincos or cexp.  We are sure we have sincos or cexp because cexpi
2588      is only generated from sincos, cexp or if we have either of them.  */
2589   if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2590     {
2591       op1 = gen_reg_rtx (mode);
2592       op2 = gen_reg_rtx (mode);
2593
2594       op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2595
2596       /* Compute into op1 and op2.  */
2597       expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2598     }
2599   else if (TARGET_HAS_SINCOS)
2600     {
2601       tree call, fn = NULL_TREE;
2602       tree top1, top2;
2603       rtx op1a, op2a;
2604
2605       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2606         fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2607       else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2608         fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2609       else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2610         fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2611       else
2612         gcc_unreachable ();
2613
2614       op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2615       op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2616       op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2617       op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2618       top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2619       top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2620
2621       /* Make sure not to fold the sincos call again.  */
2622       call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2623       expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2624                                       call, 3, arg, top1, top2));
2625     }
2626   else
2627     {
2628       tree call, fn = NULL_TREE, narg;
2629       tree ctype = build_complex_type (type);
2630
2631       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2632         fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2633       else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2634         fn = builtin_decl_explicit (BUILT_IN_CEXP);
2635       else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2636         fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2637       else
2638         gcc_unreachable ();
2639
2640       /* If we don't have a decl for cexp create one.  This is the
2641          friendliest fallback if the user calls __builtin_cexpi
2642          without full target C99 function support.  */
2643       if (fn == NULL_TREE)
2644         {
2645           tree fntype;
2646           const char *name = NULL;
2647
2648           if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2649             name = "cexpf";
2650           else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2651             name = "cexp";
2652           else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2653             name = "cexpl";
2654
2655           fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2656           fn = build_fn_decl (name, fntype);
2657         }
2658
2659       narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2660                           build_real (type, dconst0), arg);
2661
2662       /* Make sure not to fold the cexp call again.  */
2663       call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2664       return expand_expr (build_call_nary (ctype, call, 1, narg),
2665                           target, VOIDmode, EXPAND_NORMAL);
2666     }
2667
2668   /* Now build the proper return type.  */
2669   return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2670                               make_tree (TREE_TYPE (arg), op2),
2671                               make_tree (TREE_TYPE (arg), op1)),
2672                       target, VOIDmode, EXPAND_NORMAL);
2673 }
2674
2675 /* Conveniently construct a function call expression.  FNDECL names the
2676    function to be called, N is the number of arguments, and the "..."
2677    parameters are the argument expressions.  Unlike build_call_exr
2678    this doesn't fold the call, hence it will always return a CALL_EXPR.  */
2679
2680 static tree
2681 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2682 {
2683   va_list ap;
2684   tree fntype = TREE_TYPE (fndecl);
2685   tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2686
2687   va_start (ap, n);
2688   fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2689   va_end (ap);
2690   SET_EXPR_LOCATION (fn, loc);
2691   return fn;
2692 }
2693
2694 /* Expand a call to one of the builtin rounding functions gcc defines
2695    as an extension (lfloor and lceil).  As these are gcc extensions we
2696    do not need to worry about setting errno to EDOM.
2697    If expanding via optab fails, lower expression to (int)(floor(x)).
2698    EXP is the expression that is a call to the builtin function;
2699    if convenient, the result should be placed in TARGET.  */
2700
2701 static rtx
2702 expand_builtin_int_roundingfn (tree exp, rtx target)
2703 {
2704   convert_optab builtin_optab;
2705   rtx op0, insns, tmp;
2706   tree fndecl = get_callee_fndecl (exp);
2707   enum built_in_function fallback_fn;
2708   tree fallback_fndecl;
2709   enum machine_mode mode;
2710   tree arg;
2711
2712   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2713     gcc_unreachable ();
2714
2715   arg = CALL_EXPR_ARG (exp, 0);
2716
2717   switch (DECL_FUNCTION_CODE (fndecl))
2718     {
2719     CASE_FLT_FN (BUILT_IN_ICEIL):
2720     CASE_FLT_FN (BUILT_IN_LCEIL):
2721     CASE_FLT_FN (BUILT_IN_LLCEIL):
2722       builtin_optab = lceil_optab;
2723       fallback_fn = BUILT_IN_CEIL;
2724       break;
2725
2726     CASE_FLT_FN (BUILT_IN_IFLOOR):
2727     CASE_FLT_FN (BUILT_IN_LFLOOR):
2728     CASE_FLT_FN (BUILT_IN_LLFLOOR):
2729       builtin_optab = lfloor_optab;
2730       fallback_fn = BUILT_IN_FLOOR;
2731       break;
2732
2733     default:
2734       gcc_unreachable ();
2735     }
2736
2737   /* Make a suitable register to place result in.  */
2738   mode = TYPE_MODE (TREE_TYPE (exp));
2739
2740   target = gen_reg_rtx (mode);
2741
2742   /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2743      need to expand the argument again.  This way, we will not perform
2744      side-effects more the once.  */
2745   CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2746
2747   op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2748
2749   start_sequence ();
2750
2751   /* Compute into TARGET.  */
2752   if (expand_sfix_optab (target, op0, builtin_optab))
2753     {
2754       /* Output the entire sequence.  */
2755       insns = get_insns ();
2756       end_sequence ();
2757       emit_insn (insns);
2758       return target;
2759     }
2760
2761   /* If we were unable to expand via the builtin, stop the sequence
2762      (without outputting the insns).  */
2763   end_sequence ();
2764
2765   /* Fall back to floating point rounding optab.  */
2766   fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2767
2768   /* For non-C99 targets we may end up without a fallback fndecl here
2769      if the user called __builtin_lfloor directly.  In this case emit
2770      a call to the floor/ceil variants nevertheless.  This should result
2771      in the best user experience for not full C99 targets.  */
2772   if (fallback_fndecl == NULL_TREE)
2773     {
2774       tree fntype;
2775       const char *name = NULL;
2776
2777       switch (DECL_FUNCTION_CODE (fndecl))
2778         {
2779         case BUILT_IN_ICEIL:
2780         case BUILT_IN_LCEIL:
2781         case BUILT_IN_LLCEIL:
2782           name = "ceil";
2783           break;
2784         case BUILT_IN_ICEILF:
2785         case BUILT_IN_LCEILF:
2786         case BUILT_IN_LLCEILF:
2787           name = "ceilf";
2788           break;
2789         case BUILT_IN_ICEILL:
2790         case BUILT_IN_LCEILL:
2791         case BUILT_IN_LLCEILL:
2792           name = "ceill";
2793           break;
2794         case BUILT_IN_IFLOOR:
2795         case BUILT_IN_LFLOOR:
2796         case BUILT_IN_LLFLOOR:
2797           name = "floor";
2798           break;
2799         case BUILT_IN_IFLOORF:
2800         case BUILT_IN_LFLOORF:
2801         case BUILT_IN_LLFLOORF:
2802           name = "floorf";
2803           break;
2804         case BUILT_IN_IFLOORL:
2805         case BUILT_IN_LFLOORL:
2806         case BUILT_IN_LLFLOORL:
2807           name = "floorl";
2808           break;
2809         default:
2810           gcc_unreachable ();
2811         }
2812
2813       fntype = build_function_type_list (TREE_TYPE (arg),
2814                                          TREE_TYPE (arg), NULL_TREE);
2815       fallback_fndecl = build_fn_decl (name, fntype);
2816     }
2817
2818   exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2819
2820   tmp = expand_normal (exp);
2821
2822   /* Truncate the result of floating point optab to integer
2823      via expand_fix ().  */
2824   target = gen_reg_rtx (mode);
2825   expand_fix (target, tmp, 0);
2826
2827   return target;
2828 }
2829
2830 /* Expand a call to one of the builtin math functions doing integer
2831    conversion (lrint).
2832    Return 0 if a normal call should be emitted rather than expanding the
2833    function in-line.  EXP is the expression that is a call to the builtin
2834    function; if convenient, the result should be placed in TARGET.  */
2835
2836 static rtx
2837 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2838 {
2839   convert_optab builtin_optab;
2840   rtx op0, insns;
2841   tree fndecl = get_callee_fndecl (exp);
2842   tree arg;
2843   enum machine_mode mode;
2844   enum built_in_function fallback_fn = BUILT_IN_NONE;
2845
2846   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2847      gcc_unreachable ();
2848
2849   arg = CALL_EXPR_ARG (exp, 0);
2850
2851   switch (DECL_FUNCTION_CODE (fndecl))
2852     {
2853     CASE_FLT_FN (BUILT_IN_IRINT):
2854       fallback_fn = BUILT_IN_LRINT;
2855       /* FALLTHRU */
2856     CASE_FLT_FN (BUILT_IN_LRINT):
2857     CASE_FLT_FN (BUILT_IN_LLRINT):
2858       builtin_optab = lrint_optab;
2859       break;
2860
2861     CASE_FLT_FN (BUILT_IN_IROUND):
2862       fallback_fn = BUILT_IN_LROUND;
2863       /* FALLTHRU */
2864     CASE_FLT_FN (BUILT_IN_LROUND):
2865     CASE_FLT_FN (BUILT_IN_LLROUND):
2866       builtin_optab = lround_optab;
2867       break;
2868
2869     default:
2870       gcc_unreachable ();
2871     }
2872
2873   /* There's no easy way to detect the case we need to set EDOM.  */
2874   if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2875     return NULL_RTX;
2876
2877   /* Make a suitable register to place result in.  */
2878   mode = TYPE_MODE (TREE_TYPE (exp));
2879
2880   /* There's no easy way to detect the case we need to set EDOM.  */
2881   if (!flag_errno_math)
2882     {
2883       target = gen_reg_rtx (mode);
2884
2885       /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2886          need to expand the argument again.  This way, we will not perform
2887          side-effects more the once.  */
2888       CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2889
2890       op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2891
2892       start_sequence ();
2893
2894       if (expand_sfix_optab (target, op0, builtin_optab))
2895         {
2896           /* Output the entire sequence.  */
2897           insns = get_insns ();
2898           end_sequence ();
2899           emit_insn (insns);
2900           return target;
2901         }
2902
2903       /* If we were unable to expand via the builtin, stop the sequence
2904          (without outputting the insns) and call to the library function
2905          with the stabilized argument list.  */
2906       end_sequence ();
2907     }
2908
2909   if (fallback_fn != BUILT_IN_NONE)
2910     {
2911       /* Fall back to rounding to long int.  Use implicit_p 0 - for non-C99
2912          targets, (int) round (x) should never be transformed into
2913          BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2914          a call to lround in the hope that the target provides at least some
2915          C99 functions.  This should result in the best user experience for
2916          not full C99 targets.  */
2917       tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2918                                                 fallback_fn, 0);
2919
2920       exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2921                                    fallback_fndecl, 1, arg);
2922
2923       target = expand_call (exp, NULL_RTX, target == const0_rtx);
2924       return convert_to_mode (mode, target, 0);
2925     }
2926
2927   target = expand_call (exp, target, target == const0_rtx);
2928
2929   return target;
2930 }
2931
2932 /* Expand a call to the powi built-in mathematical function.  Return NULL_RTX if
2933    a normal call should be emitted rather than expanding the function
2934    in-line.  EXP is the expression that is a call to the builtin
2935    function; if convenient, the result should be placed in TARGET.  */
2936
2937 static rtx
2938 expand_builtin_powi (tree exp, rtx target)
2939 {
2940   tree arg0, arg1;
2941   rtx op0, op1;
2942   enum machine_mode mode;
2943   enum machine_mode mode2;
2944
2945   if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2946     return NULL_RTX;
2947
2948   arg0 = CALL_EXPR_ARG (exp, 0);
2949   arg1 = CALL_EXPR_ARG (exp, 1);
2950   mode = TYPE_MODE (TREE_TYPE (exp));
2951
2952   /* Emit a libcall to libgcc.  */
2953
2954   /* Mode of the 2nd argument must match that of an int.  */
2955   mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2956
2957   if (target == NULL_RTX)
2958     target = gen_reg_rtx (mode);
2959
2960   op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2961   if (GET_MODE (op0) != mode)
2962     op0 = convert_to_mode (mode, op0, 0);
2963   op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2964   if (GET_MODE (op1) != mode2)
2965     op1 = convert_to_mode (mode2, op1, 0);
2966
2967   target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2968                                     target, LCT_CONST, mode, 2,
2969                                     op0, mode, op1, mode2);
2970
2971   return target;
2972 }
2973
2974 /* Expand expression EXP which is a call to the strlen builtin.  Return
2975    NULL_RTX if we failed the caller should emit a normal call, otherwise
2976    try to get the result in TARGET, if convenient.  */
2977
2978 static rtx
2979 expand_builtin_strlen (tree exp, rtx target,
2980                        enum machine_mode target_mode)
2981 {
2982   if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2983     return NULL_RTX;
2984   else
2985     {
2986       struct expand_operand ops[4];
2987       rtx pat;
2988       tree len;
2989       tree src = CALL_EXPR_ARG (exp, 0);
2990       rtx src_reg, before_strlen;
2991       enum machine_mode insn_mode = target_mode;
2992       enum insn_code icode = CODE_FOR_nothing;
2993       unsigned int align;
2994
2995       /* If the length can be computed at compile-time, return it.  */
2996       len = c_strlen (src, 0);
2997       if (len)
2998         return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2999
3000       /* If the length can be computed at compile-time and is constant
3001          integer, but there are side-effects in src, evaluate
3002          src for side-effects, then return len.
3003          E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3004          can be optimized into: i++; x = 3;  */
3005       len = c_strlen (src, 1);
3006       if (len && TREE_CODE (len) == INTEGER_CST)
3007         {
3008           expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3009           return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3010         }
3011
3012       align = get_pointer_alignment (src) / BITS_PER_UNIT;
3013
3014       /* If SRC is not a pointer type, don't do this operation inline.  */
3015       if (align == 0)
3016         return NULL_RTX;
3017
3018       /* Bail out if we can't compute strlen in the right mode.  */
3019       while (insn_mode != VOIDmode)
3020         {
3021           icode = optab_handler (strlen_optab, insn_mode);
3022           if (icode != CODE_FOR_nothing)
3023             break;
3024
3025           insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3026         }
3027       if (insn_mode == VOIDmode)
3028         return NULL_RTX;
3029
3030       /* Make a place to hold the source address.  We will not expand
3031          the actual source until we are sure that the expansion will
3032          not fail -- there are trees that cannot be expanded twice.  */
3033       src_reg = gen_reg_rtx (Pmode);
3034
3035       /* Mark the beginning of the strlen sequence so we can emit the
3036          source operand later.  */
3037       before_strlen = get_last_insn ();
3038
3039       create_output_operand (&ops[0], target, insn_mode);
3040       create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3041       create_integer_operand (&ops[2], 0);
3042       create_integer_operand (&ops[3], align);
3043       if (!maybe_expand_insn (icode, 4, ops))
3044         return NULL_RTX;
3045
3046       /* Now that we are assured of success, expand the source.  */
3047       start_sequence ();
3048       pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3049       if (pat != src_reg)
3050         {
3051 #ifdef POINTERS_EXTEND_UNSIGNED
3052           if (GET_MODE (pat) != Pmode)
3053             pat = convert_to_mode (Pmode, pat,
3054                                    POINTERS_EXTEND_UNSIGNED);
3055 #endif
3056           emit_move_insn (src_reg, pat);
3057         }
3058       pat = get_insns ();
3059       end_sequence ();
3060
3061       if (before_strlen)
3062         emit_insn_after (pat, before_strlen);
3063       else
3064         emit_insn_before (pat, get_insns ());
3065
3066       /* Return the value in the proper mode for this function.  */
3067       if (GET_MODE (ops[0].value) == target_mode)
3068         target = ops[0].value;
3069       else if (target != 0)
3070         convert_move (target, ops[0].value, 0);
3071       else
3072         target = convert_to_mode (target_mode, ops[0].value, 0);
3073
3074       return target;
3075     }
3076 }
3077
3078 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
3079    bytes from constant string DATA + OFFSET and return it as target
3080    constant.  */
3081
3082 static rtx
3083 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3084                          enum machine_mode mode)
3085 {
3086   const char *str = (const char *) data;
3087
3088   gcc_assert (offset >= 0
3089               && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3090                   <= strlen (str) + 1));
3091
3092   return c_readstr (str + offset, mode);
3093 }
3094
3095 /* Expand a call EXP to the memcpy builtin.
3096    Return NULL_RTX if we failed, the caller should emit a normal call,
3097    otherwise try to get the result in TARGET, if convenient (and in
3098    mode MODE if that's convenient).  */
3099
3100 static rtx
3101 expand_builtin_memcpy (tree exp, rtx target)
3102 {
3103   if (!validate_arglist (exp,
3104                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3105     return NULL_RTX;
3106   else
3107     {
3108       tree dest = CALL_EXPR_ARG (exp, 0);
3109       tree src = CALL_EXPR_ARG (exp, 1);
3110       tree len = CALL_EXPR_ARG (exp, 2);
3111       const char *src_str;
3112       unsigned int src_align = get_pointer_alignment (src);
3113       unsigned int dest_align = get_pointer_alignment (dest);
3114       rtx dest_mem, src_mem, dest_addr, len_rtx;
3115       HOST_WIDE_INT expected_size = -1;
3116       unsigned int expected_align = 0;
3117
3118       /* If DEST is not a pointer type, call the normal function.  */
3119       if (dest_align == 0)
3120         return NULL_RTX;
3121
3122       /* If either SRC is not a pointer type, don't do this
3123          operation in-line.  */
3124       if (src_align == 0)
3125         return NULL_RTX;
3126
3127       if (currently_expanding_gimple_stmt)
3128         stringop_block_profile (currently_expanding_gimple_stmt,
3129                                 &expected_align, &expected_size);
3130
3131       if (expected_align < dest_align)
3132         expected_align = dest_align;
3133       dest_mem = get_memory_rtx (dest, len);
3134       set_mem_align (dest_mem, dest_align);
3135       len_rtx = expand_normal (len);
3136       src_str = c_getstr (src);
3137
3138       /* If SRC is a string constant and block move would be done
3139          by pieces, we can avoid loading the string from memory
3140          and only stored the computed constants.  */
3141       if (src_str
3142           && CONST_INT_P (len_rtx)
3143           && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3144           && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3145                                   CONST_CAST (char *, src_str),
3146                                   dest_align, false))
3147         {
3148           dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3149                                       builtin_memcpy_read_str,
3150                                       CONST_CAST (char *, src_str),
3151                                       dest_align, false, 0);
3152           dest_mem = force_operand (XEXP (dest_mem, 0), target);
3153           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3154           return dest_mem;
3155         }
3156
3157       src_mem = get_memory_rtx (src, len);
3158       set_mem_align (src_mem, src_align);
3159
3160       /* Copy word part most expediently.  */
3161       dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3162                                          CALL_EXPR_TAILCALL (exp)
3163                                          ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3164                                          expected_align, expected_size);
3165
3166       if (dest_addr == 0)
3167         {
3168           dest_addr = force_operand (XEXP (dest_mem, 0), target);
3169           dest_addr = convert_memory_address (ptr_mode, dest_addr);
3170         }
3171       return dest_addr;
3172     }
3173 }
3174
3175 /* Expand a call EXP to the mempcpy builtin.
3176    Return NULL_RTX if we failed; the caller should emit a normal call,
3177    otherwise try to get the result in TARGET, if convenient (and in
3178    mode MODE if that's convenient).  If ENDP is 0 return the
3179    destination pointer, if ENDP is 1 return the end pointer ala
3180    mempcpy, and if ENDP is 2 return the end pointer minus one ala
3181    stpcpy.  */
3182
3183 static rtx
3184 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3185 {
3186   if (!validate_arglist (exp,
3187                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3188     return NULL_RTX;
3189   else
3190     {
3191       tree dest = CALL_EXPR_ARG (exp, 0);
3192       tree src = CALL_EXPR_ARG (exp, 1);
3193       tree len = CALL_EXPR_ARG (exp, 2);
3194       return expand_builtin_mempcpy_args (dest, src, len,
3195                                           target, mode, /*endp=*/ 1);
3196     }
3197 }
3198
3199 /* Helper function to do the actual work for expand_builtin_mempcpy.  The
3200    arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3201    so that this can also be called without constructing an actual CALL_EXPR.
3202    The other arguments and return value are the same as for
3203    expand_builtin_mempcpy.  */
3204
3205 static rtx
3206 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3207                              rtx target, enum machine_mode mode, int endp)
3208 {
3209     /* If return value is ignored, transform mempcpy into memcpy.  */
3210   if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3211     {
3212       tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3213       tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3214                                            dest, src, len);
3215       return expand_expr (result, target, mode, EXPAND_NORMAL);
3216     }
3217   else
3218     {
3219       const char *src_str;
3220       unsigned int src_align = get_pointer_alignment (src);
3221       unsigned int dest_align = get_pointer_alignment (dest);
3222       rtx dest_mem, src_mem, len_rtx;
3223
3224       /* If either SRC or DEST is not a pointer type, don't do this
3225          operation in-line.  */
3226       if (dest_align == 0 || src_align == 0)
3227         return NULL_RTX;
3228
3229       /* If LEN is not constant, call the normal function.  */
3230       if (! host_integerp (len, 1))
3231         return NULL_RTX;
3232
3233       len_rtx = expand_normal (len);
3234       src_str = c_getstr (src);
3235
3236       /* If SRC is a string constant and block move would be done
3237          by pieces, we can avoid loading the string from memory
3238          and only stored the computed constants.  */
3239       if (src_str
3240           && CONST_INT_P (len_rtx)
3241           && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3242           && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3243                                   CONST_CAST (char *, src_str),
3244                                   dest_align, false))
3245         {
3246           dest_mem = get_memory_rtx (dest, len);
3247           set_mem_align (dest_mem, dest_align);
3248           dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3249                                       builtin_memcpy_read_str,
3250                                       CONST_CAST (char *, src_str),
3251                                       dest_align, false, endp);
3252           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3253           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3254           return dest_mem;
3255         }
3256
3257       if (CONST_INT_P (len_rtx)
3258           && can_move_by_pieces (INTVAL (len_rtx),
3259                                  MIN (dest_align, src_align)))
3260         {
3261           dest_mem = get_memory_rtx (dest, len);
3262           set_mem_align (dest_mem, dest_align);
3263           src_mem = get_memory_rtx (src, len);
3264           set_mem_align (src_mem, src_align);
3265           dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3266                                      MIN (dest_align, src_align), endp);
3267           dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3268           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3269           return dest_mem;
3270         }
3271
3272       return NULL_RTX;
3273     }
3274 }
3275
3276 #ifndef HAVE_movstr
3277 # define HAVE_movstr 0
3278 # define CODE_FOR_movstr CODE_FOR_nothing
3279 #endif
3280
3281 /* Expand into a movstr instruction, if one is available.  Return NULL_RTX if
3282    we failed, the caller should emit a normal call, otherwise try to
3283    get the result in TARGET, if convenient.  If ENDP is 0 return the
3284    destination pointer, if ENDP is 1 return the end pointer ala
3285    mempcpy, and if ENDP is 2 return the end pointer minus one ala
3286    stpcpy.  */
3287
3288 static rtx
3289 expand_movstr (tree dest, tree src, rtx target, int endp)
3290 {
3291   struct expand_operand ops[3];
3292   rtx dest_mem;
3293   rtx src_mem;
3294
3295   if (!HAVE_movstr)
3296     return NULL_RTX;
3297
3298   dest_mem = get_memory_rtx (dest, NULL);
3299   src_mem = get_memory_rtx (src, NULL);
3300   if (!endp)
3301     {
3302       target = force_reg (Pmode, XEXP (dest_mem, 0));
3303       dest_mem = replace_equiv_address (dest_mem, target);
3304     }
3305
3306   create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3307   create_fixed_operand (&ops[1], dest_mem);
3308   create_fixed_operand (&ops[2], src_mem);
3309   expand_insn (CODE_FOR_movstr, 3, ops);
3310
3311   if (endp && target != const0_rtx)
3312     {
3313       target = ops[0].value;
3314       /* movstr is supposed to set end to the address of the NUL
3315          terminator.  If the caller requested a mempcpy-like return value,
3316          adjust it.  */
3317       if (endp == 1)
3318         {
3319           rtx tem = plus_constant (gen_lowpart (GET_MODE (target), target), 1);
3320           emit_move_insn (target, force_operand (tem, NULL_RTX));
3321         }
3322     }
3323   return target;
3324 }
3325
3326 /* Expand expression EXP, which is a call to the strcpy builtin.  Return
3327    NULL_RTX if we failed the caller should emit a normal call, otherwise
3328    try to get the result in TARGET, if convenient (and in mode MODE if that's
3329    convenient).  */
3330
3331 static rtx
3332 expand_builtin_strcpy (tree exp, rtx target)
3333 {
3334   if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3335    {
3336      tree dest = CALL_EXPR_ARG (exp, 0);
3337      tree src = CALL_EXPR_ARG (exp, 1);
3338      return expand_builtin_strcpy_args (dest, src, target);
3339    }
3340    return NULL_RTX;
3341 }
3342
3343 /* Helper function to do the actual work for expand_builtin_strcpy.  The
3344    arguments to the builtin_strcpy call DEST and SRC are broken out
3345    so that this can also be called without constructing an actual CALL_EXPR.
3346    The other arguments and return value are the same as for
3347    expand_builtin_strcpy.  */
3348
3349 static rtx
3350 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3351 {
3352   return expand_movstr (dest, src, target, /*endp=*/0);
3353 }
3354
3355 /* Expand a call EXP to the stpcpy builtin.
3356    Return NULL_RTX if we failed the caller should emit a normal call,
3357    otherwise try to get the result in TARGET, if convenient (and in
3358    mode MODE if that's convenient).  */
3359
3360 static rtx
3361 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3362 {
3363   tree dst, src;
3364   location_t loc = EXPR_LOCATION (exp);
3365
3366   if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3367     return NULL_RTX;
3368
3369   dst = CALL_EXPR_ARG (exp, 0);
3370   src = CALL_EXPR_ARG (exp, 1);
3371
3372   /* If return value is ignored, transform stpcpy into strcpy.  */
3373   if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3374     {
3375       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3376       tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3377       return expand_expr (result, target, mode, EXPAND_NORMAL);
3378     }
3379   else
3380     {
3381       tree len, lenp1;
3382       rtx ret;
3383
3384       /* Ensure we get an actual string whose length can be evaluated at
3385          compile-time, not an expression containing a string.  This is
3386          because the latter will potentially produce pessimized code
3387          when used to produce the return value.  */
3388       if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3389         return expand_movstr (dst, src, target, /*endp=*/2);
3390
3391       lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3392       ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3393                                          target, mode, /*endp=*/2);
3394
3395       if (ret)
3396         return ret;
3397
3398       if (TREE_CODE (len) == INTEGER_CST)
3399         {
3400           rtx len_rtx = expand_normal (len);
3401
3402           if (CONST_INT_P (len_rtx))
3403             {
3404               ret = expand_builtin_strcpy_args (dst, src, target);
3405
3406               if (ret)
3407                 {
3408                   if (! target)
3409                     {
3410                       if (mode != VOIDmode)
3411                         target = gen_reg_rtx (mode);
3412                       else
3413                         target = gen_reg_rtx (GET_MODE (ret));
3414                     }
3415                   if (GET_MODE (target) != GET_MODE (ret))
3416                     ret = gen_lowpart (GET_MODE (target), ret);
3417
3418                   ret = plus_constant (ret, INTVAL (len_rtx));
3419                   ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3420                   gcc_assert (ret);
3421
3422                   return target;
3423                 }
3424             }
3425         }
3426
3427       return expand_movstr (dst, src, target, /*endp=*/2);
3428     }
3429 }
3430
3431 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
3432    bytes from constant string DATA + OFFSET and return it as target
3433    constant.  */
3434
3435 rtx
3436 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3437                           enum machine_mode mode)
3438 {
3439   const char *str = (const char *) data;
3440
3441   if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3442     return const0_rtx;
3443
3444   return c_readstr (str + offset, mode);
3445 }
3446
3447 /* Expand expression EXP, which is a call to the strncpy builtin.  Return
3448    NULL_RTX if we failed the caller should emit a normal call.  */
3449
3450 static rtx
3451 expand_builtin_strncpy (tree exp, rtx target)
3452 {
3453   location_t loc = EXPR_LOCATION (exp);
3454
3455   if (validate_arglist (exp,
3456                         POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3457     {
3458       tree dest = CALL_EXPR_ARG (exp, 0);
3459       tree src = CALL_EXPR_ARG (exp, 1);
3460       tree len = CALL_EXPR_ARG (exp, 2);
3461       tree slen = c_strlen (src, 1);
3462
3463       /* We must be passed a constant len and src parameter.  */
3464       if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3465         return NULL_RTX;
3466
3467       slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3468
3469       /* We're required to pad with trailing zeros if the requested
3470          len is greater than strlen(s2)+1.  In that case try to
3471          use store_by_pieces, if it fails, punt.  */
3472       if (tree_int_cst_lt (slen, len))
3473         {
3474           unsigned int dest_align = get_pointer_alignment (dest);
3475           const char *p = c_getstr (src);
3476           rtx dest_mem;
3477
3478           if (!p || dest_align == 0 || !host_integerp (len, 1)
3479               || !can_store_by_pieces (tree_low_cst (len, 1),
3480                                        builtin_strncpy_read_str,
3481                                        CONST_CAST (char *, p),
3482                                        dest_align, false))
3483             return NULL_RTX;
3484
3485           dest_mem = get_memory_rtx (dest, len);
3486           store_by_pieces (dest_mem, tree_low_cst (len, 1),
3487                            builtin_strncpy_read_str,
3488                            CONST_CAST (char *, p), dest_align, false, 0);
3489           dest_mem = force_operand (XEXP (dest_mem, 0), target);
3490           dest_mem = convert_memory_address (ptr_mode, dest_mem);
3491           return dest_mem;
3492         }
3493     }
3494   return NULL_RTX;
3495 }
3496
3497 /* Callback routine for store_by_pieces.  Read GET_MODE_BITSIZE (MODE)
3498    bytes from constant string DATA + OFFSET and return it as target
3499    constant.  */
3500
3501 rtx
3502 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3503                          enum machine_mode mode)
3504 {
3505   const char *c = (const char *) data;
3506   char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3507
3508   memset (p, *c, GET_MODE_SIZE (mode));
3509
3510   return c_readstr (p, mode);
3511 }
3512
3513 /* Callback routine for store_by_pieces.  Return the RTL of a register
3514    containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3515    char value given in the RTL register data.  For example, if mode is
3516    4 bytes wide, return the RTL for 0x01010101*data.  */
3517
3518 static rtx
3519 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3520                         enum machine_mode mode)
3521 {
3522   rtx target, coeff;
3523   size_t size;
3524   char *p;
3525
3526   size = GET_MODE_SIZE (mode);
3527   if (size == 1)
3528     return (rtx) data;
3529
3530   p = XALLOCAVEC (char, size);
3531   memset (p, 1, size);
3532   coeff = c_readstr (p, mode);
3533
3534   target = convert_to_mode (mode, (rtx) data, 1);
3535   target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3536   return force_reg (mode, target);
3537 }
3538
3539 /* Expand expression EXP, which is a call to the memset builtin.  Return
3540    NULL_RTX if we failed the caller should emit a normal call, otherwise
3541    try to get the result in TARGET, if convenient (and in mode MODE if that's
3542    convenient).  */
3543
3544 static rtx
3545 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3546 {
3547   if (!validate_arglist (exp,
3548                          POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3549     return NULL_RTX;
3550   else
3551     {
3552       tree dest = CALL_EXPR_ARG (exp, 0);
3553       tree val = CALL_EXPR_ARG (exp, 1);
3554       tree len = CALL_EXPR_ARG (exp, 2);
3555       return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3556     }
3557 }
3558
3559 /* Helper function to do the actual work for expand_builtin_memset.  The
3560    arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3561    so that this can also be called without constructing an actual CALL_EXPR.
3562    The other arguments and return value are the same as for
3563    expand_builtin_memset.  */
3564
3565 static rtx
3566 expand_builtin_memset_args (tree dest, tree val, tree len,
3567                             rtx target, enum machine_mode mode, tree orig_exp)
3568 {
3569   tree fndecl, fn;
3570   enum built_in_function fcode;
3571   enum machine_mode val_mode;
3572   char c;
3573   unsigned int dest_align;
3574   rtx dest_mem, dest_addr, len_rtx;
3575   HOST_WIDE_INT expected_size = -1;
3576   unsigned int expected_align = 0;
3577
3578   dest_align = get_pointer_alignment (dest);
3579
3580   /* If DEST is not a pointer type, don't do this operation in-line.  */
3581   if (dest_align == 0)
3582     return NULL_RTX;
3583
3584   if (currently_expanding_gimple_stmt)
3585     stringop_block_profile (currently_expanding_gimple_stmt,
3586                             &expected_align, &expected_size);
3587
3588   if (expected_align < dest_align)
3589     expected_align = dest_align;
3590
3591   /* If the LEN parameter is zero, return DEST.  */
3592   if (integer_zerop (len))
3593     {
3594       /* Evaluate and ignore VAL in case it has side-effects.  */
3595       expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3596       return expand_expr (dest, target, mode, EXPAND_NORMAL);
3597     }
3598
3599   /* Stabilize the arguments in case we fail.  */
3600   dest = builtin_save_expr (dest);
3601   val = builtin_save_expr (val);
3602   len = builtin_save_expr (len);
3603
3604   len_rtx = expand_normal (len);
3605   dest_mem = get_memory_rtx (dest, len);
3606   val_mode = TYPE_MODE (unsigned_char_type_node);
3607
3608   if (TREE_CODE (val) != INTEGER_CST)
3609     {
3610       rtx val_rtx;
3611
3612       val_rtx = expand_normal (val);
3613       val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3614
3615       /* Assume that we can memset by pieces if we can store
3616        * the coefficients by pieces (in the required modes).
3617        * We can't pass builtin_memset_gen_str as that emits RTL.  */
3618       c = 1;
3619       if (host_integerp (len, 1)
3620           && can_store_by_pieces (tree_low_cst (len, 1),
3621                                   builtin_memset_read_str, &c, dest_align,
3622                                   true))
3623         {
3624           val_rtx = force_reg (val_mode, val_rtx);
3625           store_by_pieces (dest_mem, tree_low_cst (len, 1),
3626                            builtin_memset_gen_str, val_rtx, dest_align,
3627                            true, 0);
3628         }
3629       else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3630                                         dest_align, expected_align,
3631                                         expected_size))
3632         goto do_libcall;
3633
3634       dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3635       dest_mem = convert_memory_address (ptr_mode, dest_mem);
3636       return dest_mem;
3637     }
3638
3639   if (target_char_cast (val, &c))
3640     goto do_libcall;
3641
3642   if (c)
3643     {
3644       if (host_integerp (len, 1)
3645           && can_store_by_pieces (tree_low_cst (len, 1),
3646                                   builtin_memset_read_str, &c, dest_align,
3647                                   true))
3648         store_by_pieces (dest_mem, tree_low_cst (len, 1),
3649                          builtin_memset_read_str, &c, dest_align, true, 0);
3650       else if (!set_storage_via_setmem (dest_mem, len_rtx,
3651                                         gen_int_mode (c, val_mode),
3652                                         dest_align, expected_align,
3653                                         expected_size))
3654         goto do_libcall;
3655
3656       dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3657       dest_mem = convert_memory_address (ptr_mode, dest_mem);
3658       return dest_mem;
3659     }
3660
3661   set_mem_align (dest_mem, dest_align);
3662   dest_addr = clear_storage_hints (dest_mem, len_rtx,
3663                                    CALL_EXPR_TAILCALL (orig_exp)
3664                                    ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3665                                    expected_align, expected_size);
3666
3667   if (dest_addr == 0)
3668     {
3669       dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3670       dest_addr = convert_memory_address (ptr_mode, dest_addr);
3671     }
3672
3673   return dest_addr;
3674
3675  do_libcall:
3676   fndecl = get_callee_fndecl (orig_exp);
3677   fcode = DECL_FUNCTION_CODE (fndecl);
3678   if (fcode == BUILT_IN_MEMSET)
3679     fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3680                                 dest, val, len);
3681   else if (fcode == BUILT_IN_BZERO)
3682     fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3683                                 dest, len);
3684   else
3685     gcc_unreachable ();
3686   gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3687   CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3688   return expand_call (fn, target, target == const0_rtx);
3689 }
3690
3691 /* Expand expression EXP, which is a call to the bzero builtin.  Return
3692    NULL_RTX if we failed the caller should emit a normal call.  */
3693
3694 static rtx
3695 expand_builtin_bzero (tree exp)
3696 {
3697   tree dest, size;
3698   location_t loc = EXPR_LOCATION (exp);
3699
3700   if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3701     return NULL_RTX;
3702
3703   dest = CALL_EXPR_ARG (exp, 0);
3704   size = CALL_EXPR_ARG (exp, 1);
3705
3706   /* New argument list transforming bzero(ptr x, int y) to
3707      memset(ptr x, int 0, size_t y).   This is done this way
3708      so that if it isn't expanded inline, we fallback to
3709      calling bzero instead of memset.  */
3710
3711   return expand_builtin_memset_args (dest, integer_zero_node,
3712                                      fold_convert_loc (loc,
3713                                                        size_type_node, size),
3714                                      const0_rtx, VOIDmode, exp);
3715 }
3716
3717 /* Expand expression EXP, which is a call to the memcmp built-in function.
3718    Return NULL_RTX if we failed and the caller should emit a normal call,
3719    otherwise try to get the result in TARGET, if convenient (and in mode
3720    MODE, if that's convenient).  */
3721
3722 static rtx
3723 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3724                        ATTRIBUTE_UNUSED enum machine_mode mode)
3725 {
3726   location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3727
3728   if (!validate_arglist (exp,
3729                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3730     return NULL_RTX;
3731
3732   /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3733      implementing memcmp because it will stop if it encounters two
3734      zero bytes.  */
3735 #if defined HAVE_cmpmemsi
3736   {
3737     rtx arg1_rtx, arg2_rtx, arg3_rtx;
3738     rtx result;
3739     rtx insn;
3740     tree arg1 = CALL_EXPR_ARG (exp, 0);
3741     tree arg2 = CALL_EXPR_ARG (exp, 1);
3742     tree len = CALL_EXPR_ARG (exp, 2);
3743
3744     unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3745     unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3746     enum machine_mode insn_mode;
3747
3748     if (HAVE_cmpmemsi)
3749       insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3750     else
3751       return NULL_RTX;
3752
3753     /* If we don't have POINTER_TYPE, call the function.  */
3754     if (arg1_align == 0 || arg2_align == 0)
3755       return NULL_RTX;
3756
3757     /* Make a place to write the result of the instruction.  */
3758     result = target;
3759     if (! (result != 0
3760            && REG_P (result) && GET_MODE (result) == insn_mode
3761            && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3762       result = gen_reg_rtx (insn_mode);
3763
3764     arg1_rtx = get_memory_rtx (arg1, len);
3765     arg2_rtx = get_memory_rtx (arg2, len);
3766     arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3767
3768     /* Set MEM_SIZE as appropriate.  */
3769     if (CONST_INT_P (arg3_rtx))
3770       {
3771         set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3772         set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3773       }
3774
3775     if (HAVE_cmpmemsi)
3776       insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3777                            GEN_INT (MIN (arg1_align, arg2_align)));
3778     else
3779       gcc_unreachable ();
3780
3781     if (insn)
3782       emit_insn (insn);
3783     else
3784       emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3785                                TYPE_MODE (integer_type_node), 3,
3786                                XEXP (arg1_rtx, 0), Pmode,
3787                                XEXP (arg2_rtx, 0), Pmode,
3788                                convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3789                                                 TYPE_UNSIGNED (sizetype)),
3790                                TYPE_MODE (sizetype));
3791
3792     /* Return the value in the proper mode for this function.  */
3793     mode = TYPE_MODE (TREE_TYPE (exp));
3794     if (GET_MODE (result) == mode)
3795       return result;
3796     else if (target != 0)
3797       {
3798         convert_move (target, result, 0);
3799         return target;
3800       }
3801     else
3802       return convert_to_mode (mode, result, 0);
3803   }
3804 #endif /* HAVE_cmpmemsi.  */
3805
3806   return NULL_RTX;
3807 }
3808
3809 /* Expand expression EXP, which is a call to the strcmp builtin.  Return NULL_RTX
3810    if we failed the caller should emit a normal call, otherwise try to get
3811    the result in TARGET, if convenient.  */
3812
3813 static rtx
3814 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3815 {
3816   if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3817     return NULL_RTX;
3818
3819 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3820   if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3821       || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3822     {
3823       rtx arg1_rtx, arg2_rtx;
3824       rtx result, insn = NULL_RTX;
3825       tree fndecl, fn;
3826       tree arg1 = CALL_EXPR_ARG (exp, 0);
3827       tree arg2 = CALL_EXPR_ARG (exp, 1);
3828
3829       unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3830       unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3831
3832       /* If we don't have POINTER_TYPE, call the function.  */
3833       if (arg1_align == 0 || arg2_align == 0)
3834         return NULL_RTX;
3835
3836       /* Stabilize the arguments in case gen_cmpstr(n)si fail.  */
3837       arg1 = builtin_save_expr (arg1);
3838       arg2 = builtin_save_expr (arg2);
3839
3840       arg1_rtx = get_memory_rtx (arg1, NULL);
3841       arg2_rtx = get_memory_rtx (arg2, NULL);
3842
3843 #ifdef HAVE_cmpstrsi
3844       /* Try to call cmpstrsi.  */
3845       if (HAVE_cmpstrsi)
3846         {
3847           enum machine_mode insn_mode
3848             = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3849
3850           /* Make a place to write the result of the instruction.  */
3851           result = target;
3852           if (! (result != 0
3853                  && REG_P (result) && GET_MODE (result) == insn_mode
3854                  && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3855             result = gen_reg_rtx (insn_mode);
3856
3857           insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3858                                GEN_INT (MIN (arg1_align, arg2_align)));
3859         }
3860 #endif
3861 #ifdef HAVE_cmpstrnsi
3862       /* Try to determine at least one length and call cmpstrnsi.  */
3863       if (!insn && HAVE_cmpstrnsi)
3864         {
3865           tree len;
3866           rtx arg3_rtx;
3867
3868           enum machine_mode insn_mode
3869             = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3870           tree len1 = c_strlen (arg1, 1);
3871           tree len2 = c_strlen (arg2, 1);
3872
3873           if (len1)
3874             len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3875           if (len2)
3876             len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3877
3878           /* If we don't have a constant length for the first, use the length
3879              of the second, if we know it.  We don't require a constant for
3880              this case; some cost analysis could be done if both are available
3881              but neither is constant.  For now, assume they're equally cheap,
3882              unless one has side effects.  If both strings have constant lengths,
3883              use the smaller.  */
3884
3885           if (!len1)
3886             len = len2;
3887           else if (!len2)
3888             len = len1;
3889           else if (TREE_SIDE_EFFECTS (len1))
3890             len = len2;
3891           else if (TREE_SIDE_EFFECTS (len2))
3892             len = len1;
3893           else if (TREE_CODE (len1) != INTEGER_CST)
3894             len = len2;
3895           else if (TREE_CODE (len2) != INTEGER_CST)
3896             len = len1;
3897           else if (tree_int_cst_lt (len1, len2))
3898             len = len1;
3899           else
3900             len = len2;
3901
3902           /* If both arguments have side effects, we cannot optimize.  */
3903           if (!len || TREE_SIDE_EFFECTS (len))
3904             goto do_libcall;
3905
3906           arg3_rtx = expand_normal (len);
3907
3908           /* Make a place to write the result of the instruction.  */
3909           result = target;
3910           if (! (result != 0
3911                  && REG_P (result) && GET_MODE (result) == insn_mode
3912                  && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3913             result = gen_reg_rtx (insn_mode);
3914
3915           insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3916                                 GEN_INT (MIN (arg1_align, arg2_align)));
3917         }
3918 #endif
3919
3920       if (insn)
3921         {
3922           enum machine_mode mode;
3923           emit_insn (insn);
3924
3925           /* Return the value in the proper mode for this function.  */
3926           mode = TYPE_MODE (TREE_TYPE (exp));
3927           if (GET_MODE (result) == mode)
3928             return result;
3929           if (target == 0)
3930             return convert_to_mode (mode, result, 0);
3931           convert_move (target, result, 0);
3932           return target;
3933         }
3934
3935       /* Expand the library call ourselves using a stabilized argument
3936          list to avoid re-evaluating the function's arguments twice.  */
3937 #ifdef HAVE_cmpstrnsi
3938     do_libcall:
3939 #endif
3940       fndecl = get_callee_fndecl (exp);
3941       fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
3942       gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3943       CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3944       return expand_call (fn, target, target == const0_rtx);
3945     }
3946 #endif
3947   return NULL_RTX;
3948 }
3949
3950 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3951    NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3952    the result in TARGET, if convenient.  */
3953
3954 static rtx
3955 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3956                         ATTRIBUTE_UNUSED enum machine_mode mode)
3957 {
3958   location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3959
3960   if (!validate_arglist (exp,
3961                          POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3962     return NULL_RTX;
3963
3964   /* If c_strlen can determine an expression for one of the string
3965      lengths, and it doesn't have side effects, then emit cmpstrnsi
3966      using length MIN(strlen(string)+1, arg3).  */
3967 #ifdef HAVE_cmpstrnsi
3968   if (HAVE_cmpstrnsi)
3969   {
3970     tree len, len1, len2;
3971     rtx arg1_rtx, arg2_rtx, arg3_rtx;
3972     rtx result, insn;
3973     tree fndecl, fn;
3974     tree arg1 = CALL_EXPR_ARG (exp, 0);
3975     tree arg2 = CALL_EXPR_ARG (exp, 1);
3976     tree arg3 = CALL_EXPR_ARG (exp, 2);
3977
3978     unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3979     unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3980     enum machine_mode insn_mode
3981       = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3982
3983     len1 = c_strlen (arg1, 1);
3984     len2 = c_strlen (arg2, 1);
3985
3986     if (len1)
3987       len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
3988     if (len2)
3989       len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
3990
3991     /* If we don't have a constant length for the first, use the length
3992        of the second, if we know it.  We don't require a constant for
3993        this case; some cost analysis could be done if both are available
3994        but neither is constant.  For now, assume they're equally cheap,
3995        unless one has side effects.  If both strings have constant lengths,
3996        use the smaller.  */
3997
3998     if (!len1)
3999       len = len2;
4000     else if (!len2)
4001       len = len1;
4002     else if (TREE_SIDE_EFFECTS (len1))
4003       len = len2;
4004     else if (TREE_SIDE_EFFECTS (len2))
4005       len = len1;
4006     else if (TREE_CODE (len1) != INTEGER_CST)
4007       len = len2;
4008     else if (TREE_CODE (len2) != INTEGER_CST)
4009       len = len1;
4010     else if (tree_int_cst_lt (len1, len2))
4011       len = len1;
4012     else
4013       len = len2;
4014
4015     /* If both arguments have side effects, we cannot optimize.  */
4016     if (!len || TREE_SIDE_EFFECTS (len))
4017       return NULL_RTX;
4018
4019     /* The actual new length parameter is MIN(len,arg3).  */
4020     len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4021                        fold_convert_loc (loc, TREE_TYPE (len), arg3));
4022
4023     /* If we don't have POINTER_TYPE, call the function.  */
4024     if (arg1_align == 0 || arg2_align == 0)
4025       return NULL_RTX;
4026
4027     /* Make a place to write the result of the instruction.  */
4028     result = target;
4029     if (! (result != 0
4030            && REG_P (result) && GET_MODE (result) == insn_mode
4031            && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4032       result = gen_reg_rtx (insn_mode);
4033
4034     /* Stabilize the arguments in case gen_cmpstrnsi fails.  */
4035     arg1 = builtin_save_expr (arg1);
4036     arg2 = builtin_save_expr (arg2);
4037     len = builtin_save_expr (len);
4038
4039     arg1_rtx = get_memory_rtx (arg1, len);
4040     arg2_rtx = get_memory_rtx (arg2, len);
4041     arg3_rtx = expand_normal (len);
4042     insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4043                           GEN_INT (MIN (arg1_align, arg2_align)));
4044     if (insn)
4045       {
4046         emit_insn (insn);
4047
4048         /* Return the value in the proper mode for this function.  */
4049         mode = TYPE_MODE (TREE_TYPE (exp));
4050         if (GET_MODE (result) == mode)
4051           return result;
4052         if (target == 0)
4053           return convert_to_mode (mode, result, 0);
4054         convert_move (target, result, 0);
4055         return target;
4056       }
4057
4058     /* Expand the library call ourselves using a stabilized argument
4059        list to avoid re-evaluating the function's arguments twice.  */
4060     fndecl = get_callee_fndecl (exp);
4061     fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4062                                 arg1, arg2, len);
4063     gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4064     CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4065     return expand_call (fn, target, target == const0_rtx);
4066   }
4067 #endif
4068   return NULL_RTX;
4069 }
4070
4071 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4072    if that's convenient.  */
4073
4074 rtx
4075 expand_builtin_saveregs (void)
4076 {
4077   rtx val, seq;
4078
4079   /* Don't do __builtin_saveregs more than once in a function.
4080      Save the result of the first call and reuse it.  */
4081   if (saveregs_value != 0)
4082     return saveregs_value;
4083
4084   /* When this function is called, it means that registers must be
4085      saved on entry to this function.  So we migrate the call to the
4086      first insn of this function.  */
4087
4088   start_sequence ();
4089
4090   /* Do whatever the machine needs done in this case.  */
4091   val = targetm.calls.expand_builtin_saveregs ();
4092
4093   seq = get_insns ();
4094   end_sequence ();
4095
4096   saveregs_value = val;
4097
4098   /* Put the insns after the NOTE that starts the function.  If this
4099      is inside a start_sequence, make the outer-level insn chain current, so
4100      the code is placed at the start of the function.  */
4101   push_topmost_sequence ();
4102   emit_insn_after (seq, entry_of_function ());
4103   pop_topmost_sequence ();
4104
4105   return val;
4106 }
4107
4108 /* Expand a call to __builtin_next_arg.  */
4109
4110 static rtx
4111 expand_builtin_next_arg (void)
4112 {
4113   /* Checking arguments is already done in fold_builtin_next_arg
4114      that must be called before this function.  */
4115   return expand_binop (ptr_mode, add_optab,
4116                        crtl->args.internal_arg_pointer,
4117                        crtl->args.arg_offset_rtx,
4118                        NULL_RTX, 0, OPTAB_LIB_WIDEN);
4119 }
4120
4121 /* Make it easier for the backends by protecting the valist argument
4122    from multiple evaluations.  */
4123
4124 static tree
4125 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4126 {
4127   tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4128
4129   /* The current way of determining the type of valist is completely
4130      bogus.  We should have the information on the va builtin instead.  */
4131   if (!vatype)
4132     vatype = targetm.fn_abi_va_list (cfun->decl);
4133
4134   if (TREE_CODE (vatype) == ARRAY_TYPE)
4135     {
4136       if (TREE_SIDE_EFFECTS (valist))
4137         valist = save_expr (valist);
4138
4139       /* For this case, the backends will be expecting a pointer to
4140          vatype, but it's possible we've actually been given an array
4141          (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4142          So fix it.  */
4143       if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4144         {
4145           tree p1 = build_pointer_type (TREE_TYPE (vatype));
4146           valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4147         }
4148     }
4149   else
4150     {
4151       tree pt = build_pointer_type (vatype);
4152
4153       if (! needs_lvalue)
4154         {
4155           if (! TREE_SIDE_EFFECTS (valist))
4156             return valist;
4157
4158           valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4159           TREE_SIDE_EFFECTS (valist) = 1;
4160         }
4161
4162       if (TREE_SIDE_EFFECTS (valist))
4163         valist = save_expr (valist);
4164       valist = fold_build2_loc (loc, MEM_REF,
4165                                 vatype, valist, build_int_cst (pt, 0));
4166     }
4167
4168   return valist;
4169 }
4170
4171 /* The "standard" definition of va_list is void*.  */
4172
4173 tree
4174 std_build_builtin_va_list (void)
4175 {
4176   return ptr_type_node;
4177 }
4178
4179 /* The "standard" abi va_list is va_list_type_node.  */
4180
4181 tree
4182 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4183 {
4184   return va_list_type_node;
4185 }
4186
4187 /* The "standard" type of va_list is va_list_type_node.  */
4188
4189 tree
4190 std_canonical_va_list_type (tree type)
4191 {
4192   tree wtype, htype;
4193
4194   if (INDIRECT_REF_P (type))
4195     type = TREE_TYPE (type);
4196   else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4197     type = TREE_TYPE (type);
4198   wtype = va_list_type_node;
4199   htype = type;
4200   /* Treat structure va_list types.  */
4201   if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4202     htype = TREE_TYPE (htype);
4203   else if (TREE_CODE (wtype) == ARRAY_TYPE)
4204     {
4205       /* If va_list is an array type, the argument may have decayed
4206          to a pointer type, e.g. by being passed to another function.
4207          In that case, unwrap both types so that we can compare the
4208          underlying records.  */
4209       if (TREE_CODE (htype) == ARRAY_TYPE
4210           || POINTER_TYPE_P (htype))
4211         {
4212           wtype = TREE_TYPE (wtype);
4213           htype = TREE_TYPE (htype);
4214         }
4215     }
4216   if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4217     return va_list_type_node;
4218
4219   return NULL_TREE;
4220 }
4221
4222 /* The "standard" implementation of va_start: just assign `nextarg' to
4223    the variable.  */
4224
4225 void
4226 std_expand_builtin_va_start (tree valist, rtx nextarg)
4227 {
4228   rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4229   convert_move (va_r, nextarg, 0);
4230 }
4231
4232 /* Expand EXP, a call to __builtin_va_start.  */
4233
4234 static rtx
4235 expand_builtin_va_start (tree exp)
4236 {
4237   rtx nextarg;
4238   tree valist;
4239   location_t loc = EXPR_LOCATION (exp);
4240
4241   if (call_expr_nargs (exp) < 2)
4242     {
4243       error_at (loc, "too few arguments to function %<va_start%>");
4244       return const0_rtx;
4245     }
4246
4247   if (fold_builtin_next_arg (exp, true))
4248     return const0_rtx;
4249
4250   nextarg = expand_builtin_next_arg ();
4251   valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4252
4253   if (targetm.expand_builtin_va_start)
4254     targetm.expand_builtin_va_start (valist, nextarg);
4255   else
4256     std_expand_builtin_va_start (valist, nextarg);
4257
4258   return const0_rtx;
4259 }
4260
4261 /* The "standard" implementation of va_arg: read the value from the
4262    current (padded) address and increment by the (padded) size.  */
4263
4264 tree
4265 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4266                           gimple_seq *post_p)
4267 {
4268   tree addr, t, type_size, rounded_size, valist_tmp;
4269   unsigned HOST_WIDE_INT align, boundary;
4270   bool indirect;
4271
4272 #ifdef ARGS_GROW_DOWNWARD
4273   /* All of the alignment and movement below is for args-grow-up machines.
4274      As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4275      implement their own specialized gimplify_va_arg_expr routines.  */
4276   gcc_unreachable ();
4277 #endif
4278
4279   indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4280   if (indirect)
4281     type = build_pointer_type (type);
4282
4283   align = PARM_BOUNDARY / BITS_PER_UNIT;
4284   boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4285
4286   /* When we align parameter on stack for caller, if the parameter
4287      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4288      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
4289      here with caller.  */
4290   if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4291     boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4292
4293   boundary /= BITS_PER_UNIT;
4294
4295   /* Hoist the valist value into a temporary for the moment.  */
4296   valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4297
4298   /* va_list pointer is aligned to PARM_BOUNDARY.  If argument actually
4299      requires greater alignment, we must perform dynamic alignment.  */
4300   if (boundary > align
4301       && !integer_zerop (TYPE_SIZE (type)))
4302     {
4303       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4304                   fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
4305       gimplify_and_add (t, pre_p);
4306
4307       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4308                   fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
4309                                valist_tmp,
4310                                build_int_cst (TREE_TYPE (valist), -boundary)));
4311       gimplify_and_add (t, pre_p);
4312     }
4313   else
4314     boundary = align;
4315
4316   /* If the actual alignment is less than the alignment of the type,
4317      adjust the type accordingly so that we don't assume strict alignment
4318      when dereferencing the pointer.  */
4319   boundary *= BITS_PER_UNIT;
4320   if (boundary < TYPE_ALIGN (type))
4321     {
4322       type = build_variant_type_copy (type);
4323       TYPE_ALIGN (type) = boundary;
4324     }
4325
4326   /* Compute the rounded size of the type.  */
4327   type_size = size_in_bytes (type);
4328   rounded_size = round_up (type_size, align);
4329
4330   /* Reduce rounded_size so it's sharable with the postqueue.  */
4331   gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4332
4333   /* Get AP.  */
4334   addr = valist_tmp;
4335   if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4336     {
4337       /* Small args are padded downward.  */
4338       t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4339                        rounded_size, size_int (align));
4340       t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4341                        size_binop (MINUS_EXPR, rounded_size, type_size));
4342       addr = fold_build_pointer_plus (addr, t);
4343     }
4344
4345   /* Compute new value for AP.  */
4346   t = fold_build_pointer_plus (valist_tmp, rounded_size);
4347   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4348   gimplify_and_add (t, pre_p);
4349
4350   addr = fold_convert (build_pointer_type (type), addr);
4351
4352   if (indirect)
4353     addr = build_va_arg_indirect_ref (addr);
4354
4355   return build_va_arg_indirect_ref (addr);
4356 }
4357
4358 /* Build an indirect-ref expression over the given TREE, which represents a
4359    piece of a va_arg() expansion.  */
4360 tree
4361 build_va_arg_indirect_ref (tree addr)
4362 {
4363   addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4364
4365   if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF.  */
4366     mf_mark (addr);
4367
4368   return addr;
4369 }
4370
4371 /* Return a dummy expression of type TYPE in order to keep going after an
4372    error.  */
4373
4374 static tree
4375 dummy_object (tree type)
4376 {
4377   tree t = build_int_cst (build_pointer_type (type), 0);
4378   return build2 (MEM_REF, type, t, t);
4379 }
4380
4381 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4382    builtin function, but a very special sort of operator.  */
4383
4384 enum gimplify_status
4385 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4386 {
4387   tree promoted_type, have_va_type;
4388   tree valist = TREE_OPERAND (*expr_p, 0);
4389   tree type = TREE_TYPE (*expr_p);
4390   tree t;
4391   location_t loc = EXPR_LOCATION (*expr_p);
4392
4393   /* Verify that valist is of the proper type.  */
4394   have_va_type = TREE_TYPE (valist);
4395   if (have_va_type == error_mark_node)
4396     return GS_ERROR;
4397   have_va_type = targetm.canonical_va_list_type (have_va_type);
4398
4399   if (have_va_type == NULL_TREE)
4400     {
4401       error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4402       return GS_ERROR;
4403     }
4404
4405   /* Generate a diagnostic for requesting data of a type that cannot
4406      be passed through `...' due to type promotion at the call site.  */
4407   if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4408            != type)
4409     {
4410       static bool gave_help;
4411       bool warned;
4412
4413       /* Unfortunately, this is merely undefined, rather than a constraint
4414          violation, so we cannot make this an error.  If this call is never
4415          executed, the program is still strictly conforming.  */
4416       warned = warning_at (loc, 0,
4417                            "%qT is promoted to %qT when passed through %<...%>",
4418                            type, promoted_type);
4419       if (!gave_help && warned)
4420         {
4421           gave_help = true;
4422           inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4423                   promoted_type, type);
4424         }
4425
4426       /* We can, however, treat "undefined" any way we please.
4427          Call abort to encourage the user to fix the program.  */
4428       if (warned)
4429         inform (loc, "if this code is reached, the program will abort");
4430       /* Before the abort, allow the evaluation of the va_list
4431          expression to exit or longjmp.  */
4432       gimplify_and_add (valist, pre_p);
4433       t = build_call_expr_loc (loc,
4434                                builtin_decl_implicit (BUILT_IN_TRAP), 0);
4435       gimplify_and_add (t, pre_p);
4436
4437       /* This is dead code, but go ahead and finish so that the
4438          mode of the result comes out right.  */
4439       *expr_p = dummy_object (type);
4440       return GS_ALL_DONE;
4441     }
4442   else
4443     {
4444       /* Make it easier for the backends by protecting the valist argument
4445          from multiple evaluations.  */
4446       if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4447         {
4448           /* For this case, the backends will be expecting a pointer to
4449              TREE_TYPE (abi), but it's possible we've
4450              actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4451              So fix it.  */
4452           if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4453             {
4454               tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4455               valist = fold_convert_loc (loc, p1,
4456                                          build_fold_addr_expr_loc (loc, valist));
4457             }
4458
4459           gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4460         }
4461       else
4462         gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4463
4464       if (!targetm.gimplify_va_arg_expr)
4465         /* FIXME: Once most targets are converted we should merely
4466            assert this is non-null.  */
4467         return GS_ALL_DONE;
4468
4469       *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4470       return GS_OK;
4471     }
4472 }
4473
4474 /* Expand EXP, a call to __builtin_va_end.  */
4475
4476 static rtx
4477 expand_builtin_va_end (tree exp)
4478 {
4479   tree valist = CALL_EXPR_ARG (exp, 0);
4480
4481   /* Evaluate for side effects, if needed.  I hate macros that don't
4482      do that.  */
4483   if (TREE_SIDE_EFFECTS (valist))
4484     expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4485
4486   return const0_rtx;
4487 }
4488
4489 /* Expand EXP, a call to __builtin_va_copy.  We do this as a
4490    builtin rather than just as an assignment in stdarg.h because of the
4491    nastiness of array-type va_list types.  */
4492
4493 static rtx
4494 expand_builtin_va_copy (tree exp)
4495 {
4496   tree dst, src, t;
4497   location_t loc = EXPR_LOCATION (exp);
4498
4499   dst = CALL_EXPR_ARG (exp, 0);
4500   src = CALL_EXPR_ARG (exp, 1);
4501
4502   dst = stabilize_va_list_loc (loc, dst, 1);
4503   src = stabilize_va_list_loc (loc, src, 0);
4504
4505   gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4506
4507   if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4508     {
4509       t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4510       TREE_SIDE_EFFECTS (t) = 1;
4511       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4512     }
4513   else
4514     {
4515       rtx dstb, srcb, size;
4516
4517       /* Evaluate to pointers.  */
4518       dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4519       srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4520       size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4521                   NULL_RTX, VOIDmode, EXPAND_NORMAL);
4522
4523       dstb = convert_memory_address (Pmode, dstb);
4524       srcb = convert_memory_address (Pmode, srcb);
4525
4526       /* "Dereference" to BLKmode memories.  */
4527       dstb = gen_rtx_MEM (BLKmode, dstb);
4528       set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4529       set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4530       srcb = gen_rtx_MEM (BLKmode, srcb);
4531       set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4532       set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4533
4534       /* Copy.  */
4535       emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4536     }
4537
4538   return const0_rtx;
4539 }
4540
4541 /* Expand a call to one of the builtin functions __builtin_frame_address or
4542    __builtin_return_address.  */
4543
4544 static rtx
4545 expand_builtin_frame_address (tree fndecl, tree exp)
4546 {
4547   /* The argument must be a nonnegative integer constant.
4548      It counts the number of frames to scan up the stack.
4549      The value is the return address saved in that frame.  */
4550   if (call_expr_nargs (exp) == 0)
4551     /* Warning about missing arg was already issued.  */
4552     return const0_rtx;
4553   else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4554     {
4555       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4556         error ("invalid argument to %<__builtin_frame_address%>");
4557       else
4558         error ("invalid argument to %<__builtin_return_address%>");
4559       return const0_rtx;
4560     }
4561   else
4562     {
4563       rtx tem
4564         = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4565                                       tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4566
4567       /* Some ports cannot access arbitrary stack frames.  */
4568       if (tem == NULL)
4569         {
4570           if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4571             warning (0, "unsupported argument to %<__builtin_frame_address%>");
4572           else
4573             warning (0, "unsupported argument to %<__builtin_return_address%>");
4574           return const0_rtx;
4575         }
4576
4577       /* For __builtin_frame_address, return what we've got.  */
4578       if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4579         return tem;
4580
4581       if (!REG_P (tem)
4582           && ! CONSTANT_P (tem))
4583         tem = copy_to_mode_reg (Pmode, tem);
4584       return tem;
4585     }
4586 }
4587
4588 /* Expand EXP, a call to the alloca builtin.  Return NULL_RTX if we
4589    failed and the caller should emit a normal call.  CANNOT_ACCUMULATE
4590    is the same as for allocate_dynamic_stack_space.  */
4591
4592 static rtx
4593 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4594 {
4595   rtx op0;
4596   rtx result;
4597   bool valid_arglist;
4598   unsigned int align;
4599   bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4600                             == BUILT_IN_ALLOCA_WITH_ALIGN);
4601
4602   /* Emit normal call if we use mudflap.  */
4603   if (flag_mudflap)
4604     return NULL_RTX;
4605
4606   valid_arglist
4607     = (alloca_with_align
4608        ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4609        : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4610
4611   if (!valid_arglist)
4612     return NULL_RTX;
4613
4614   /* Compute the argument.  */
4615   op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4616
4617   /* Compute the alignment.  */
4618   align = (alloca_with_align
4619            ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4620            : BIGGEST_ALIGNMENT);
4621
4622   /* Allocate the desired space.  */
4623   result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4624   result = convert_memory_address (ptr_mode, result);
4625
4626   return result;
4627 }
4628
4629 /* Expand a call to a bswap builtin with argument ARG0.  MODE
4630    is the mode to expand with.  */
4631
4632 static rtx
4633 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
4634 {
4635   enum machine_mode mode;
4636   tree arg;
4637   rtx op0;
4638
4639   if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4640     return NULL_RTX;
4641
4642   arg = CALL_EXPR_ARG (exp, 0);
4643   mode = TYPE_MODE (TREE_TYPE (arg));
4644   op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4645
4646   target = expand_unop (mode, bswap_optab, op0, target, 1);
4647
4648   gcc_assert (target);
4649
4650   return convert_to_mode (mode, target, 0);
4651 }
4652
4653 /* Expand a call to a unary builtin in EXP.
4654    Return NULL_RTX if a normal call should be emitted rather than expanding the
4655    function in-line.  If convenient, the result should be placed in TARGET.
4656    SUBTARGET may be used as the target for computing one of EXP's operands.  */
4657
4658 static rtx
4659 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4660                      rtx subtarget, optab op_optab)
4661 {
4662   rtx op0;
4663
4664   if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4665     return NULL_RTX;
4666
4667   /* Compute the argument.  */
4668   op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4669                      (subtarget
4670                       && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4671                           == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4672                      VOIDmode, EXPAND_NORMAL);
4673   /* Compute op, into TARGET if possible.
4674      Set TARGET to wherever the result comes back.  */
4675   target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4676                         op_optab, op0, target, op_optab != clrsb_optab);
4677   gcc_assert (target);
4678
4679   return convert_to_mode (target_mode, target, 0);
4680 }
4681
4682 /* Expand a call to __builtin_expect.  We just return our argument
4683    as the builtin_expect semantic should've been already executed by
4684    tree branch prediction pass. */
4685
4686 static rtx
4687 expand_builtin_expect (tree exp, rtx target)
4688 {
4689   tree arg;
4690
4691   if (call_expr_nargs (exp) < 2)
4692     return const0_rtx;
4693   arg = CALL_EXPR_ARG (exp, 0);
4694
4695   target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4696   /* When guessing was done, the hints should be already stripped away.  */
4697   gcc_assert (!flag_guess_branch_prob
4698               || optimize == 0 || seen_error ());
4699   return target;
4700 }
4701
4702 /* Expand a call to __builtin_assume_aligned.  We just return our first
4703    argument as the builtin_assume_aligned semantic should've been already
4704    executed by CCP.  */
4705
4706 static rtx
4707 expand_builtin_assume_aligned (tree exp, rtx target)
4708 {
4709   if (call_expr_nargs (exp) < 2)
4710     return const0_rtx;
4711   target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4712                         EXPAND_NORMAL);
4713   gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4714               && (call_expr_nargs (exp) < 3
4715                   || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4716   return target;
4717 }
4718
4719 void
4720 expand_builtin_trap (void)
4721 {
4722 #ifdef HAVE_trap
4723   if (HAVE_trap)
4724     emit_insn (gen_trap ());
4725   else
4726 #endif
4727     emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4728   emit_barrier ();
4729 }
4730
4731 /* Expand a call to __builtin_unreachable.  We do nothing except emit
4732    a barrier saying that control flow will not pass here.
4733
4734    It is the responsibility of the program being compiled to ensure
4735    that control flow does never reach __builtin_unreachable.  */
4736 static void
4737 expand_builtin_unreachable (void)
4738 {
4739   emit_barrier ();
4740 }
4741
4742 /* Expand EXP, a call to fabs, fabsf or fabsl.
4743    Return NULL_RTX if a normal call should be emitted rather than expanding
4744    the function inline.  If convenient, the result should be placed
4745    in TARGET.  SUBTARGET may be used as the target for computing
4746    the operand.  */
4747
4748 static rtx
4749 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4750 {
4751   enum machine_mode mode;
4752   tree arg;
4753   rtx op0;
4754
4755   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4756     return NULL_RTX;
4757
4758   arg = CALL_EXPR_ARG (exp, 0);
4759   CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4760   mode = TYPE_MODE (TREE_TYPE (arg));
4761   op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4762   return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4763 }
4764
4765 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4766    Return NULL is a normal call should be emitted rather than expanding the
4767    function inline.  If convenient, the result should be placed in TARGET.
4768    SUBTARGET may be used as the target for computing the operand.  */
4769
4770 static rtx
4771 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4772 {
4773   rtx op0, op1;
4774   tree arg;
4775
4776   if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4777     return NULL_RTX;
4778
4779   arg = CALL_EXPR_ARG (exp, 0);
4780   op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4781
4782   arg = CALL_EXPR_ARG (exp, 1);
4783   op1 = expand_normal (arg);
4784
4785   return expand_copysign (op0, op1, target);
4786 }
4787
4788 /* Create a new constant string literal and return a char* pointer to it.
4789    The STRING_CST value is the LEN characters at STR.  */
4790 tree
4791 build_string_literal (int len, const char *str)
4792 {
4793   tree t, elem, index, type;
4794
4795   t = build_string (len, str);
4796   elem = build_type_variant (char_type_node, 1, 0);
4797   index = build_index_type (size_int (len - 1));
4798   type = build_array_type (elem, index);
4799   TREE_TYPE (t) = type;
4800   TREE_CONSTANT (t) = 1;
4801   TREE_READONLY (t) = 1;
4802   TREE_STATIC (t) = 1;
4803
4804   type = build_pointer_type (elem);
4805   t = build1 (ADDR_EXPR, type,
4806               build4 (ARRAY_REF, elem,
4807                       t, integer_zero_node, NULL_TREE, NULL_TREE));
4808   return t;
4809 }
4810
4811 /* Expand a call to __builtin___clear_cache.  */
4812
4813 static rtx
4814 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4815 {
4816 #ifndef HAVE_clear_cache
4817 #ifdef CLEAR_INSN_CACHE
4818   /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4819      does something.  Just do the default expansion to a call to
4820      __clear_cache().  */
4821   return NULL_RTX;
4822 #else
4823   /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4824      does nothing.  There is no need to call it.  Do nothing.  */
4825   return const0_rtx;
4826 #endif /* CLEAR_INSN_CACHE */
4827 #else
4828   /* We have a "clear_cache" insn, and it will handle everything.  */
4829   tree begin, end;
4830   rtx begin_rtx, end_rtx;
4831
4832   /* We must not expand to a library call.  If we did, any
4833      fallback library function in libgcc that might contain a call to
4834      __builtin___clear_cache() would recurse infinitely.  */
4835   if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4836     {
4837       error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4838       return const0_rtx;
4839     }
4840
4841   if (HAVE_clear_cache)
4842     {
4843       struct expand_operand ops[2];
4844
4845       begin = CALL_EXPR_ARG (exp, 0);
4846       begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4847
4848       end = CALL_EXPR_ARG (exp, 1);
4849       end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4850
4851       create_address_operand (&ops[0], begin_rtx);
4852       create_address_operand (&ops[1], end_rtx);
4853       if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4854         return const0_rtx;
4855     }
4856   return const0_rtx;
4857 #endif /* HAVE_clear_cache */
4858 }
4859
4860 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT.  */
4861
4862 static rtx
4863 round_trampoline_addr (rtx tramp)
4864 {
4865   rtx temp, addend, mask;
4866
4867   /* If we don't need too much alignment, we'll have been guaranteed
4868      proper alignment by get_trampoline_type.  */
4869   if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4870     return tramp;
4871
4872   /* Round address up to desired boundary.  */
4873   temp = gen_reg_rtx (Pmode);
4874   addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4875   mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4876
4877   temp  = expand_simple_binop (Pmode, PLUS, tramp, addend,
4878                                temp, 0, OPTAB_LIB_WIDEN);
4879   tramp = expand_simple_binop (Pmode, AND, temp, mask,
4880                                temp, 0, OPTAB_LIB_WIDEN);
4881
4882   return tramp;
4883 }
4884
4885 static rtx
4886 expand_builtin_init_trampoline (tree exp, bool onstack)
4887 {
4888   tree t_tramp, t_func, t_chain;
4889   rtx m_tramp, r_tramp, r_chain, tmp;
4890
4891   if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4892                          POINTER_TYPE, VOID_TYPE))
4893     return NULL_RTX;
4894
4895   t_tramp = CALL_EXPR_ARG (exp, 0);
4896   t_func = CALL_EXPR_ARG (exp, 1);
4897   t_chain = CALL_EXPR_ARG (exp, 2);
4898
4899   r_tramp = expand_normal (t_tramp);
4900   m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4901   MEM_NOTRAP_P (m_tramp) = 1;
4902
4903   /* If ONSTACK, the TRAMP argument should be the address of a field
4904      within the local function's FRAME decl.  Either way, let's see if
4905      we can fill in the MEM_ATTRs for this memory.  */
4906   if (TREE_CODE (t_tramp) == ADDR_EXPR)
4907     set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
4908                                      true, 0);
4909
4910   /* Creator of a heap trampoline is responsible for making sure the
4911      address is aligned to at least STACK_BOUNDARY.  Normally malloc
4912      will ensure this anyhow.  */
4913   tmp = round_trampoline_addr (r_tramp);
4914   if (tmp != r_tramp)
4915     {
4916       m_tramp = change_address (m_tramp, BLKmode, tmp);
4917       set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4918       set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4919     }
4920
4921   /* The FUNC argument should be the address of the nested function.
4922      Extract the actual function decl to pass to the hook.  */
4923   gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4924   t_func = TREE_OPERAND (t_func, 0);
4925   gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4926
4927   r_chain = expand_normal (t_chain);
4928
4929   /* Generate insns to initialize the trampoline.  */
4930   targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4931
4932   if (onstack)
4933     {
4934       trampolines_created = 1;
4935
4936       warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4937                   "trampoline generated for nested function %qD", t_func);
4938     }
4939
4940   return const0_rtx;
4941 }
4942
4943 static rtx
4944 expand_builtin_adjust_trampoline (tree exp)
4945 {
4946   rtx tramp;
4947
4948   if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4949     return NULL_RTX;
4950
4951   tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4952   tramp = round_trampoline_addr (tramp);
4953   if (targetm.calls.trampoline_adjust_address)
4954     tramp = targetm.calls.trampoline_adjust_address (tramp);
4955
4956   return tramp;
4957 }
4958
4959 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4960    function.  The function first checks whether the back end provides
4961    an insn to implement signbit for the respective mode.  If not, it
4962    checks whether the floating point format of the value is such that
4963    the sign bit can be extracted.  If that is not the case, the
4964    function returns NULL_RTX to indicate that a normal call should be
4965    emitted rather than expanding the function in-line.  EXP is the
4966    expression that is a call to the builtin function; if convenient,
4967    the result should be placed in TARGET.  */
4968 static rtx
4969 expand_builtin_signbit (tree exp, rtx target)
4970 {
4971   const struct real_format *fmt;
4972   enum machine_mode fmode, imode, rmode;
4973   tree arg;
4974   int word, bitpos;
4975   enum insn_code icode;
4976   rtx temp;
4977   location_t loc = EXPR_LOCATION (exp);
4978
4979   if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4980     return NULL_RTX;
4981
4982   arg = CALL_EXPR_ARG (exp, 0);
4983   fmode = TYPE_MODE (TREE_TYPE (arg));
4984   rmode = TYPE_MODE (TREE_TYPE (exp));
4985   fmt = REAL_MODE_FORMAT (fmode);
4986
4987   arg = builtin_save_expr (arg);
4988
4989   /* Expand the argument yielding a RTX expression. */
4990   temp = expand_normal (arg);
4991
4992   /* Check if the back end provides an insn that handles signbit for the
4993      argument's mode. */
4994   icode = optab_handler (signbit_optab, fmode);
4995   if (icode != CODE_FOR_nothing)
4996     {
4997       rtx last = get_last_insn ();
4998       target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4999       if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5000         return target;
5001       delete_insns_since (last);
5002     }
5003
5004   /* For floating point formats without a sign bit, implement signbit
5005      as "ARG < 0.0".  */
5006   bitpos = fmt->signbit_ro;
5007   if (bitpos < 0)
5008   {
5009     /* But we can't do this if the format supports signed zero.  */
5010     if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5011       return NULL_RTX;
5012
5013     arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5014                        build_real (TREE_TYPE (arg), dconst0));
5015     return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5016   }
5017
5018   if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5019     {
5020       imode = int_mode_for_mode (fmode);
5021       if (imode == BLKmode)
5022         return NULL_RTX;
5023       temp = gen_lowpart (imode, temp);
5024     }
5025   else
5026     {
5027       imode = word_mode;
5028       /* Handle targets with different FP word orders.  */
5029       if (FLOAT_WORDS_BIG_ENDIAN)
5030         word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5031       else
5032         word = bitpos / BITS_PER_WORD;
5033       temp = operand_subword_force (temp, word, fmode);
5034       bitpos = bitpos % BITS_PER_WORD;
5035     }
5036
5037   /* Force the intermediate word_mode (or narrower) result into a
5038      register.  This avoids attempting to create paradoxical SUBREGs
5039      of floating point modes below.  */
5040   temp = force_reg (imode, temp);
5041
5042   /* If the bitpos is within the "result mode" lowpart, the operation
5043      can be implement with a single bitwise AND.  Otherwise, we need
5044      a right shift and an AND.  */
5045
5046   if (bitpos < GET_MODE_BITSIZE (rmode))
5047     {
5048       double_int mask = double_int_setbit (double_int_zero, bitpos);
5049
5050       if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5051         temp = gen_lowpart (rmode, temp);
5052       temp = expand_binop (rmode, and_optab, temp,
5053                            immed_double_int_const (mask, rmode),
5054                            NULL_RTX, 1, OPTAB_LIB_WIDEN);
5055     }
5056   else
5057     {
5058       /* Perform a logical right shift to place the signbit in the least
5059          significant bit, then truncate the result to the desired mode
5060          and mask just this bit.  */
5061       temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5062       temp = gen_lowpart (rmode, temp);
5063       temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5064                            NULL_RTX, 1, OPTAB_LIB_WIDEN);
5065     }
5066
5067   return temp;
5068 }
5069
5070 /* Expand fork or exec calls.  TARGET is the desired target of the
5071    call.  EXP is the call. FN is the
5072    identificator of the actual function.  IGNORE is nonzero if the
5073    value is to be ignored.  */
5074
5075 static rtx
5076 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5077 {
5078   tree id, decl;
5079   tree call;
5080
5081   /* If we are not profiling, just call the function.  */
5082   if (!profile_arc_flag)
5083     return NULL_RTX;
5084
5085   /* Otherwise call the wrapper.  This should be equivalent for the rest of
5086      compiler, so the code does not diverge, and the wrapper may run the
5087      code necessary for keeping the profiling sane.  */
5088
5089   switch (DECL_FUNCTION_CODE (fn))
5090     {
5091     case BUILT_IN_FORK:
5092       id = get_identifier ("__gcov_fork");
5093       break;
5094
5095     case BUILT_IN_EXECL:
5096       id = get_identifier ("__gcov_execl");
5097       break;
5098
5099     case BUILT_IN_EXECV:
5100       id = get_identifier ("__gcov_execv");
5101       break;
5102
5103     case BUILT_IN_EXECLP:
5104       id = get_identifier ("__gcov_execlp");
5105       break;
5106
5107     case BUILT_IN_EXECLE:
5108       id = get_identifier ("__gcov_execle");
5109       break;
5110
5111     case BUILT_IN_EXECVP:
5112       id = get_identifier ("__gcov_execvp");
5113       break;
5114
5115     case BUILT_IN_EXECVE:
5116       id = get_identifier ("__gcov_execve");
5117       break;
5118
5119     default:
5120       gcc_unreachable ();
5121     }
5122
5123   decl = build_decl (DECL_SOURCE_LOCATION (fn),
5124                      FUNCTION_DECL, id, TREE_TYPE (fn));
5125   DECL_EXTERNAL (decl) = 1;
5126   TREE_PUBLIC (decl) = 1;
5127   DECL_ARTIFICIAL (decl) = 1;
5128   TREE_NOTHROW (decl) = 1;
5129   DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5130   DECL_VISIBILITY_SPECIFIED (decl) = 1;
5131   call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5132   return expand_call (call, target, ignore);
5133  }
5134
5135
5136 \f
5137 /* Reconstitute a mode for a __sync intrinsic operation.  Since the type of
5138    the pointer in these functions is void*, the tree optimizers may remove
5139    casts.  The mode computed in expand_builtin isn't reliable either, due
5140    to __sync_bool_compare_and_swap.
5141
5142    FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5143    group of builtins.  This gives us log2 of the mode size.  */
5144
5145 static inline enum machine_mode
5146 get_builtin_sync_mode (int fcode_diff)
5147 {
5148   /* The size is not negotiable, so ask not to get BLKmode in return
5149      if the target indicates that a smaller size would be better.  */
5150   return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5151 }
5152
5153 /* Expand the memory expression LOC and return the appropriate memory operand
5154    for the builtin_sync operations.  */
5155
5156 static rtx
5157 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5158 {
5159   rtx addr, mem;
5160
5161   addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5162   addr = convert_memory_address (Pmode, addr);
5163
5164   /* Note that we explicitly do not want any alias information for this
5165      memory, so that we kill all other live memories.  Otherwise we don't
5166      satisfy the full barrier semantics of the intrinsic.  */
5167   mem = validize_mem (gen_rtx_MEM (mode, addr));
5168
5169   /* The alignment needs to be at least according to that of the mode.  */
5170   set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5171                            get_pointer_alignment (loc)));
5172   set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5173   MEM_VOLATILE_P (mem) = 1;
5174
5175   return mem;
5176 }
5177
5178 /* Make sure an argument is in the right mode.
5179    EXP is the tree argument. 
5180    MODE is the mode it should be in.  */
5181
5182 static rtx
5183 expand_expr_force_mode (tree exp, enum machine_mode mode)
5184 {
5185   rtx val;
5186   enum machine_mode old_mode;
5187
5188   val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5189   /* If VAL is promoted to a wider mode, convert it back to MODE.  Take care
5190      of CONST_INTs, where we know the old_mode only from the call argument.  */
5191
5192   old_mode = GET_MODE (val);
5193   if (old_mode == VOIDmode)
5194     old_mode = TYPE_MODE (TREE_TYPE (exp));
5195   val = convert_modes (mode, old_mode, val, 1);
5196   return val;
5197 }
5198
5199
5200 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5201    EXP is the CALL_EXPR.  CODE is the rtx code
5202    that corresponds to the arithmetic or logical operation from the name;
5203    an exception here is that NOT actually means NAND.  TARGET is an optional
5204    place for us to store the results; AFTER is true if this is the
5205    fetch_and_xxx form.  */
5206
5207 static rtx
5208 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5209                                enum rtx_code code, bool after,
5210                                rtx target)
5211 {
5212   rtx val, mem;
5213   location_t loc = EXPR_LOCATION (exp);
5214
5215   if (code == NOT && warn_sync_nand)
5216     {
5217       tree fndecl = get_callee_fndecl (exp);
5218       enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5219
5220       static bool warned_f_a_n, warned_n_a_f;
5221
5222       switch (fcode)
5223         {
5224         case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5225         case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5226         case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5227         case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5228         case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5229           if (warned_f_a_n)
5230             break;
5231
5232           fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5233           inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5234           warned_f_a_n = true;
5235           break;
5236
5237         case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5238         case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5239         case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5240         case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5241         case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5242           if (warned_n_a_f)
5243             break;
5244
5245          fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5246           inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5247           warned_n_a_f = true;
5248           break;
5249
5250         default:
5251           gcc_unreachable ();
5252         }
5253     }
5254
5255   /* Expand the operands.  */
5256   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5257   val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5258
5259   return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5260                                  after);
5261 }
5262
5263 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5264    intrinsics. EXP is the CALL_EXPR.  IS_BOOL is
5265    true if this is the boolean form.  TARGET is a place for us to store the
5266    results; this is NOT optional if IS_BOOL is true.  */
5267
5268 static rtx
5269 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5270                                  bool is_bool, rtx target)
5271 {
5272   rtx old_val, new_val, mem;
5273   rtx *pbool, *poval;
5274
5275   /* Expand the operands.  */
5276   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5277   old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5278   new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5279
5280   pbool = poval = NULL;
5281   if (target != const0_rtx)
5282     {
5283       if (is_bool)
5284         pbool = &target;
5285       else
5286         poval = &target;
5287     }
5288   if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5289                                        false, MEMMODEL_SEQ_CST,
5290                                        MEMMODEL_SEQ_CST))
5291     return NULL_RTX;
5292
5293   return target;
5294 }
5295
5296 /* Expand the __sync_lock_test_and_set intrinsic.  Note that the most
5297    general form is actually an atomic exchange, and some targets only
5298    support a reduced form with the second argument being a constant 1.
5299    EXP is the CALL_EXPR; TARGET is an optional place for us to store
5300    the results.  */
5301
5302 static rtx
5303 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5304                                        rtx target)
5305 {
5306   rtx val, mem;
5307
5308   /* Expand the operands.  */
5309   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5310   val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5311
5312   return expand_sync_lock_test_and_set (target, mem, val);
5313 }
5314
5315 /* Expand the __sync_lock_release intrinsic.  EXP is the CALL_EXPR.  */
5316
5317 static void
5318 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5319 {
5320   rtx mem;
5321
5322   /* Expand the operands.  */
5323   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5324
5325   expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5326 }
5327
5328 /* Given an integer representing an ``enum memmodel'', verify its
5329    correctness and return the memory model enum.  */
5330
5331 static enum memmodel
5332 get_memmodel (tree exp)
5333 {
5334   rtx op;
5335
5336   /* If the parameter is not a constant, it's a run time value so we'll just
5337      convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking.  */
5338   if (TREE_CODE (exp) != INTEGER_CST)
5339     return MEMMODEL_SEQ_CST;
5340
5341   op = expand_normal (exp);
5342   if (INTVAL (op) < 0 || INTVAL (op) >= MEMMODEL_LAST)
5343     {
5344       warning (OPT_Winvalid_memory_model,
5345                "invalid memory model argument to builtin");
5346       return MEMMODEL_SEQ_CST;
5347     }
5348   return (enum memmodel) INTVAL (op);
5349 }
5350
5351 /* Expand the __atomic_exchange intrinsic:
5352         TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5353    EXP is the CALL_EXPR.
5354    TARGET is an optional place for us to store the results.  */
5355
5356 static rtx
5357 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5358 {
5359   rtx val, mem;
5360   enum memmodel model;
5361
5362   model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5363   if (model == MEMMODEL_CONSUME)
5364     {
5365       error ("invalid memory model for %<__atomic_exchange%>");
5366       return NULL_RTX;
5367     }
5368
5369   if (!flag_inline_atomics)
5370     return NULL_RTX;
5371
5372   /* Expand the operands.  */
5373   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5374   val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5375
5376   return expand_atomic_exchange (target, mem, val, model);
5377 }
5378
5379 /* Expand the __atomic_compare_exchange intrinsic:
5380         bool __atomic_compare_exchange (TYPE *object, TYPE *expect, 
5381                                         TYPE desired, BOOL weak, 
5382                                         enum memmodel success,
5383                                         enum memmodel failure)
5384    EXP is the CALL_EXPR.
5385    TARGET is an optional place for us to store the results.  */
5386
5387 static rtx
5388 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp, 
5389                                         rtx target)
5390 {
5391   rtx expect, desired, mem, oldval;
5392   enum memmodel success, failure;
5393   tree weak;
5394   bool is_weak;
5395
5396   success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5397   failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5398
5399   if (failure == MEMMODEL_RELEASE || failure == MEMMODEL_ACQ_REL)
5400     {
5401       error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5402       return NULL_RTX;
5403     }
5404
5405   if (failure > success)
5406     {
5407       error ("failure memory model cannot be stronger than success "
5408              "memory model for %<__atomic_compare_exchange%>");
5409       return NULL_RTX;
5410     }
5411   
5412   if (!flag_inline_atomics)
5413     return NULL_RTX;
5414
5415   /* Expand the operands.  */
5416   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5417
5418   expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5419   expect = convert_memory_address (Pmode, expect);
5420   desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5421
5422   weak = CALL_EXPR_ARG (exp, 3);
5423   is_weak = false;
5424   if (host_integerp (weak, 0) && tree_low_cst (weak, 0) != 0)
5425     is_weak = true;
5426
5427   oldval = copy_to_reg (gen_rtx_MEM (mode, expect));
5428
5429   if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5430                                        &oldval, mem, oldval, desired,
5431                                        is_weak, success, failure))
5432     return NULL_RTX;
5433
5434   emit_move_insn (gen_rtx_MEM (mode, expect), oldval);
5435   return target;
5436 }
5437
5438 /* Expand the __atomic_load intrinsic:
5439         TYPE __atomic_load (TYPE *object, enum memmodel)
5440    EXP is the CALL_EXPR.
5441    TARGET is an optional place for us to store the results.  */
5442
5443 static rtx
5444 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5445 {
5446   rtx mem;
5447   enum memmodel model;
5448
5449   model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5450   if (model == MEMMODEL_RELEASE
5451       || model == MEMMODEL_ACQ_REL)
5452     {
5453       error ("invalid memory model for %<__atomic_load%>");
5454       return NULL_RTX;
5455     }
5456
5457   if (!flag_inline_atomics)
5458     return NULL_RTX;
5459
5460   /* Expand the operand.  */
5461   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5462
5463   return expand_atomic_load (target, mem, model);
5464 }
5465
5466
5467 /* Expand the __atomic_store intrinsic:
5468         void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5469    EXP is the CALL_EXPR.
5470    TARGET is an optional place for us to store the results.  */
5471
5472 static rtx
5473 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5474 {
5475   rtx mem, val;
5476   enum memmodel model;
5477
5478   model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5479   if (model != MEMMODEL_RELAXED
5480       && model != MEMMODEL_SEQ_CST
5481       && model != MEMMODEL_RELEASE)
5482     {
5483       error ("invalid memory model for %<__atomic_store%>");
5484       return NULL_RTX;
5485     }
5486
5487   if (!flag_inline_atomics)
5488     return NULL_RTX;
5489
5490   /* Expand the operands.  */
5491   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5492   val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5493
5494   return expand_atomic_store (mem, val, model, false);
5495 }
5496
5497 /* Expand the __atomic_fetch_XXX intrinsic:
5498         TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5499    EXP is the CALL_EXPR.
5500    TARGET is an optional place for us to store the results.
5501    CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5502    FETCH_AFTER is true if returning the result of the operation.
5503    FETCH_AFTER is false if returning the value before the operation.
5504    IGNORE is true if the result is not used.
5505    EXT_CALL is the correct builtin for an external call if this cannot be
5506    resolved to an instruction sequence.  */
5507
5508 static rtx
5509 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5510                                 enum rtx_code code, bool fetch_after,
5511                                 bool ignore, enum built_in_function ext_call)
5512 {
5513   rtx val, mem, ret;
5514   enum memmodel model;
5515   tree fndecl;
5516   tree addr;
5517
5518   model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5519
5520   /* Expand the operands.  */
5521   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5522   val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5523
5524   /* Only try generating instructions if inlining is turned on.  */
5525   if (flag_inline_atomics)
5526     {
5527       ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5528       if (ret)
5529         return ret;
5530     }
5531
5532   /* Return if a different routine isn't needed for the library call.  */
5533   if (ext_call == BUILT_IN_NONE)
5534     return NULL_RTX;
5535
5536   /* Change the call to the specified function.  */
5537   fndecl = get_callee_fndecl (exp);
5538   addr = CALL_EXPR_FN (exp);
5539   STRIP_NOPS (addr);
5540
5541   gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5542   TREE_OPERAND (addr, 0) = builtin_decl_explicit(ext_call);
5543
5544   /* Expand the call here so we can emit trailing code.  */
5545   ret = expand_call (exp, target, ignore);
5546
5547   /* Replace the original function just in case it matters.  */
5548   TREE_OPERAND (addr, 0) = fndecl;
5549
5550   /* Then issue the arithmetic correction to return the right result.  */
5551   if (!ignore)
5552     {
5553       if (code == NOT)
5554         {
5555           ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5556                                      OPTAB_LIB_WIDEN);
5557           ret = expand_simple_unop (mode, NOT, ret, target, true);
5558         }
5559       else
5560         ret = expand_simple_binop (mode, code, ret, val, target, true,
5561                                    OPTAB_LIB_WIDEN);
5562     }
5563   return ret;
5564 }
5565
5566
5567 #ifndef HAVE_atomic_clear
5568 # define HAVE_atomic_clear 0
5569 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5570 #endif
5571
5572 /* Expand an atomic clear operation.
5573         void _atomic_clear (BOOL *obj, enum memmodel)
5574    EXP is the call expression.  */
5575
5576 static rtx
5577 expand_builtin_atomic_clear (tree exp) 
5578 {
5579   enum machine_mode mode;
5580   rtx mem, ret;
5581   enum memmodel model;
5582
5583   mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5584   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5585   model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5586
5587   if (model == MEMMODEL_ACQUIRE || model == MEMMODEL_ACQ_REL)
5588     {
5589       error ("invalid memory model for %<__atomic_store%>");
5590       return const0_rtx;
5591     }
5592
5593   if (HAVE_atomic_clear)
5594     {
5595       emit_insn (gen_atomic_clear (mem, model));
5596       return const0_rtx;
5597     }
5598
5599   /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5600      Failing that, a store is issued by __atomic_store.  The only way this can
5601      fail is if the bool type is larger than a word size.  Unlikely, but
5602      handle it anyway for completeness.  Assume a single threaded model since
5603      there is no atomic support in this case, and no barriers are required.  */
5604   ret = expand_atomic_store (mem, const0_rtx, model, true);
5605   if (!ret)
5606     emit_move_insn (mem, const0_rtx);
5607   return const0_rtx;
5608 }
5609
5610 /* Expand an atomic test_and_set operation.
5611         bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5612    EXP is the call expression.  */
5613
5614 static rtx
5615 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5616 {
5617   rtx mem;
5618   enum memmodel model;
5619   enum machine_mode mode;
5620
5621   mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5622   mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5623   model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5624
5625   return expand_atomic_test_and_set (target, mem, model);
5626 }
5627
5628
5629 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5630    this architecture.  If ARG1 is NULL, use typical alignment for size ARG0.  */
5631
5632 static tree
5633 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5634 {
5635   int size;
5636   enum machine_mode mode;
5637   unsigned int mode_align, type_align;
5638
5639   if (TREE_CODE (arg0) != INTEGER_CST)
5640     return NULL_TREE;
5641
5642   size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5643   mode = mode_for_size (size, MODE_INT, 0);
5644   mode_align = GET_MODE_ALIGNMENT (mode);
5645
5646   if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5647     type_align = mode_align;
5648   else
5649     {
5650       tree ttype = TREE_TYPE (arg1);
5651
5652       /* This function is usually invoked and folded immediately by the front
5653          end before anything else has a chance to look at it.  The pointer
5654          parameter at this point is usually cast to a void *, so check for that
5655          and look past the cast.  */
5656       if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5657           && VOID_TYPE_P (TREE_TYPE (ttype)))
5658         arg1 = TREE_OPERAND (arg1, 0);
5659
5660       ttype = TREE_TYPE (arg1);
5661       gcc_assert (POINTER_TYPE_P (ttype));
5662
5663       /* Get the underlying type of the object.  */
5664       ttype = TREE_TYPE (ttype);
5665       type_align = TYPE_ALIGN (ttype);
5666     }
5667
5668   /* If the object has smaller alignment, the the lock free routines cannot
5669      be used.  */
5670   if (type_align < mode_align)
5671     return boolean_false_node;
5672
5673   /* Check if a compare_and_swap pattern exists for the mode which represents
5674      the required size.  The pattern is not allowed to fail, so the existence
5675      of the pattern indicates support is present.  */
5676   if (can_compare_and_swap_p (mode, true))
5677     return boolean_true_node;
5678   else
5679     return boolean_false_node;
5680 }
5681
5682 /* Return true if the parameters to call EXP represent an object which will
5683    always generate lock free instructions.  The first argument represents the
5684    size of the object, and the second parameter is a pointer to the object 
5685    itself.  If NULL is passed for the object, then the result is based on 
5686    typical alignment for an object of the specified size.  Otherwise return 
5687    false.  */
5688
5689 static rtx
5690 expand_builtin_atomic_always_lock_free (tree exp)
5691 {
5692   tree size;
5693   tree arg0 = CALL_EXPR_ARG (exp, 0);
5694   tree arg1 = CALL_EXPR_ARG (exp, 1);
5695
5696   if (TREE_CODE (arg0) != INTEGER_CST)
5697     {
5698       error ("non-constant argument 1 to __atomic_always_lock_free");
5699       return const0_rtx;
5700     }
5701
5702   size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5703   if (size == boolean_true_node)
5704     return const1_rtx;
5705   return const0_rtx;
5706 }
5707
5708 /* Return a one or zero if it can be determined that object ARG1 of size ARG 
5709    is lock free on this architecture.  */
5710
5711 static tree
5712 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5713 {
5714   if (!flag_inline_atomics)
5715     return NULL_TREE;
5716   
5717   /* If it isn't always lock free, don't generate a result.  */
5718   if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5719     return boolean_true_node;
5720
5721   return NULL_TREE;
5722 }
5723
5724 /* Return true if the parameters to call EXP represent an object which will
5725    always generate lock free instructions.  The first argument represents the
5726    size of the object, and the second parameter is a pointer to the object 
5727    itself.  If NULL is passed for the object, then the result is based on 
5728    typical alignment for an object of the specified size.  Otherwise return 
5729    NULL*/
5730
5731 static rtx
5732 expand_builtin_atomic_is_lock_free (tree exp)
5733 {
5734   tree size;
5735   tree arg0 = CALL_EXPR_ARG (exp, 0);
5736   tree arg1 = CALL_EXPR_ARG (exp, 1);
5737
5738   if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5739     {
5740       error ("non-integer argument 1 to __atomic_is_lock_free");
5741       return NULL_RTX;
5742     }
5743
5744   if (!flag_inline_atomics)
5745     return NULL_RTX; 
5746
5747   /* If the value is known at compile time, return the RTX for it.  */
5748   size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5749   if (size == boolean_true_node)
5750     return const1_rtx;
5751
5752   return NULL_RTX;
5753 }
5754
5755 /* Expand the __atomic_thread_fence intrinsic:
5756         void __atomic_thread_fence (enum memmodel)
5757    EXP is the CALL_EXPR.  */
5758
5759 static void
5760 expand_builtin_atomic_thread_fence (tree exp)
5761 {
5762   enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5763   expand_mem_thread_fence (model);
5764 }
5765
5766 /* Expand the __atomic_signal_fence intrinsic:
5767         void __atomic_signal_fence (enum memmodel)
5768    EXP is the CALL_EXPR.  */
5769
5770 static void
5771 expand_builtin_atomic_signal_fence (tree exp)
5772 {
5773   enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5774   expand_mem_signal_fence (model);
5775 }
5776
5777 /* Expand the __sync_synchronize intrinsic.  */
5778
5779 static void
5780 expand_builtin_sync_synchronize (void)
5781 {
5782   expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5783 }
5784
5785 \f
5786 /* Expand an expression EXP that calls a built-in function,
5787    with result going to TARGET if that's convenient
5788    (and in mode MODE if that's convenient).
5789    SUBTARGET may be used as the target for computing one of EXP's operands.
5790    IGNORE is nonzero if the value is to be ignored.  */
5791
5792 rtx
5793 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5794                 int ignore)
5795 {
5796   tree fndecl = get_callee_fndecl (exp);
5797   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5798   enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5799   int flags;
5800
5801   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5802     return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5803
5804   /* When not optimizing, generate calls to library functions for a certain
5805      set of builtins.  */
5806   if (!optimize
5807       && !called_as_built_in (fndecl)
5808       && fcode != BUILT_IN_ALLOCA
5809       && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5810       && fcode != BUILT_IN_FREE)
5811     return expand_call (exp, target, ignore);
5812
5813   /* The built-in function expanders test for target == const0_rtx
5814      to determine whether the function's result will be ignored.  */
5815   if (ignore)
5816     target = const0_rtx;
5817
5818   /* If the result of a pure or const built-in function is ignored, and
5819      none of its arguments are volatile, we can avoid expanding the
5820      built-in call and just evaluate the arguments for side-effects.  */
5821   if (target == const0_rtx
5822       && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5823       && !(flags & ECF_LOOPING_CONST_OR_PURE))
5824     {
5825       bool volatilep = false;
5826       tree arg;
5827       call_expr_arg_iterator iter;
5828
5829       FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5830         if (TREE_THIS_VOLATILE (arg))
5831           {
5832             volatilep = true;
5833             break;
5834           }
5835
5836       if (! volatilep)
5837         {
5838           FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5839             expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5840           return const0_rtx;
5841         }
5842     }
5843
5844   switch (fcode)
5845     {
5846     CASE_FLT_FN (BUILT_IN_FABS):
5847       target = expand_builtin_fabs (exp, target, subtarget);
5848       if (target)
5849         return target;
5850       break;
5851
5852     CASE_FLT_FN (BUILT_IN_COPYSIGN):
5853       target = expand_builtin_copysign (exp, target, subtarget);
5854       if (target)
5855         return target;
5856       break;
5857
5858       /* Just do a normal library call if we were unable to fold
5859          the values.  */
5860     CASE_FLT_FN (BUILT_IN_CABS):
5861       break;
5862
5863     CASE_FLT_FN (BUILT_IN_EXP):
5864     CASE_FLT_FN (BUILT_IN_EXP10):
5865     CASE_FLT_FN (BUILT_IN_POW10):
5866     CASE_FLT_FN (BUILT_IN_EXP2):
5867     CASE_FLT_FN (BUILT_IN_EXPM1):
5868     CASE_FLT_FN (BUILT_IN_LOGB):
5869     CASE_FLT_FN (BUILT_IN_LOG):
5870     CASE_FLT_FN (BUILT_IN_LOG10):
5871     CASE_FLT_FN (BUILT_IN_LOG2):
5872     CASE_FLT_FN (BUILT_IN_LOG1P):
5873     CASE_FLT_FN (BUILT_IN_TAN):
5874     CASE_FLT_FN (BUILT_IN_ASIN):
5875     CASE_FLT_FN (BUILT_IN_ACOS):
5876     CASE_FLT_FN (BUILT_IN_ATAN):
5877     CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5878       /* Treat these like sqrt only if unsafe math optimizations are allowed,
5879          because of possible accuracy problems.  */
5880       if (! flag_unsafe_math_optimizations)
5881         break;
5882     CASE_FLT_FN (BUILT_IN_SQRT):
5883     CASE_FLT_FN (BUILT_IN_FLOOR):
5884     CASE_FLT_FN (BUILT_IN_CEIL):
5885     CASE_FLT_FN (BUILT_IN_TRUNC):
5886     CASE_FLT_FN (BUILT_IN_ROUND):
5887     CASE_FLT_FN (BUILT_IN_NEARBYINT):
5888     CASE_FLT_FN (BUILT_IN_RINT):
5889       target = expand_builtin_mathfn (exp, target, subtarget);
5890       if (target)
5891         return target;
5892       break;
5893
5894     CASE_FLT_FN (BUILT_IN_FMA):
5895       target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5896       if (target)
5897         return target;
5898       break;
5899
5900     CASE_FLT_FN (BUILT_IN_ILOGB):
5901       if (! flag_unsafe_math_optimizations)
5902         break;
5903     CASE_FLT_FN (BUILT_IN_ISINF):
5904     CASE_FLT_FN (BUILT_IN_FINITE):
5905     case BUILT_IN_ISFINITE:
5906     case BUILT_IN_ISNORMAL:
5907       target = expand_builtin_interclass_mathfn (exp, target);
5908       if (target)
5909         return target;
5910       break;
5911
5912     CASE_FLT_FN (BUILT_IN_ICEIL):
5913     CASE_FLT_FN (BUILT_IN_LCEIL):
5914     CASE_FLT_FN (BUILT_IN_LLCEIL):
5915     CASE_FLT_FN (BUILT_IN_LFLOOR):
5916     CASE_FLT_FN (BUILT_IN_IFLOOR):
5917     CASE_FLT_FN (BUILT_IN_LLFLOOR):
5918       target = expand_builtin_int_roundingfn (exp, target);
5919       if (target)
5920         return target;
5921       break;
5922
5923     CASE_FLT_FN (BUILT_IN_IRINT):
5924     CASE_FLT_FN (BUILT_IN_LRINT):
5925     CASE_FLT_FN (BUILT_IN_LLRINT):
5926     CASE_FLT_FN (BUILT_IN_IROUND):
5927     CASE_FLT_FN (BUILT_IN_LROUND):
5928     CASE_FLT_FN (BUILT_IN_LLROUND):
5929       target = expand_builtin_int_roundingfn_2 (exp, target);
5930       if (target)
5931         return target;
5932       break;
5933
5934     CASE_FLT_FN (BUILT_IN_POWI):
5935       target = expand_builtin_powi (exp, target);
5936       if (target)
5937         return target;
5938       break;
5939
5940     CASE_FLT_FN (BUILT_IN_ATAN2):
5941     CASE_FLT_FN (BUILT_IN_LDEXP):
5942     CASE_FLT_FN (BUILT_IN_SCALB):
5943     CASE_FLT_FN (BUILT_IN_SCALBN):
5944     CASE_FLT_FN (BUILT_IN_SCALBLN):
5945       if (! flag_unsafe_math_optimizations)
5946         break;
5947
5948     CASE_FLT_FN (BUILT_IN_FMOD):
5949     CASE_FLT_FN (BUILT_IN_REMAINDER):
5950     CASE_FLT_FN (BUILT_IN_DREM):
5951     CASE_FLT_FN (BUILT_IN_POW):
5952       target = expand_builtin_mathfn_2 (exp, target, subtarget);
5953       if (target)
5954         return target;
5955       break;
5956
5957     CASE_FLT_FN (BUILT_IN_CEXPI):
5958       target = expand_builtin_cexpi (exp, target);
5959       gcc_assert (target);
5960       return target;
5961
5962     CASE_FLT_FN (BUILT_IN_SIN):
5963     CASE_FLT_FN (BUILT_IN_COS):
5964       if (! flag_unsafe_math_optimizations)
5965         break;
5966       target = expand_builtin_mathfn_3 (exp, target, subtarget);
5967       if (target)
5968         return target;
5969       break;
5970
5971     CASE_FLT_FN (BUILT_IN_SINCOS):
5972       if (! flag_unsafe_math_optimizations)
5973         break;
5974       target = expand_builtin_sincos (exp);
5975       if (target)
5976         return target;
5977       break;
5978
5979     case BUILT_IN_APPLY_ARGS:
5980       return expand_builtin_apply_args ();
5981
5982       /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5983          FUNCTION with a copy of the parameters described by
5984          ARGUMENTS, and ARGSIZE.  It returns a block of memory
5985          allocated on the stack into which is stored all the registers
5986          that might possibly be used for returning the result of a
5987          function.  ARGUMENTS is the value returned by
5988          __builtin_apply_args.  ARGSIZE is the number of bytes of
5989          arguments that must be copied.  ??? How should this value be
5990          computed?  We'll also need a safe worst case value for varargs
5991          functions.  */
5992     case BUILT_IN_APPLY:
5993       if (!validate_arglist (exp, POINTER_TYPE,
5994                              POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5995           && !validate_arglist (exp, REFERENCE_TYPE,
5996                                 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5997         return const0_rtx;
5998       else
5999         {
6000           rtx ops[3];
6001
6002           ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6003           ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6004           ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6005
6006           return expand_builtin_apply (ops[0], ops[1], ops[2]);
6007         }
6008
6009       /* __builtin_return (RESULT) causes the function to return the
6010          value described by RESULT.  RESULT is address of the block of
6011          memory returned by __builtin_apply.  */
6012     case BUILT_IN_RETURN:
6013       if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6014         expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6015       return const0_rtx;
6016
6017     case BUILT_IN_SAVEREGS:
6018       return expand_builtin_saveregs ();
6019
6020     case BUILT_IN_VA_ARG_PACK:
6021       /* All valid uses of __builtin_va_arg_pack () are removed during
6022          inlining.  */
6023       error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6024       return const0_rtx;
6025
6026     case BUILT_IN_VA_ARG_PACK_LEN:
6027       /* All valid uses of __builtin_va_arg_pack_len () are removed during
6028          inlining.  */
6029       error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6030       return const0_rtx;
6031
6032       /* Return the address of the first anonymous stack arg.  */
6033     case BUILT_IN_NEXT_ARG:
6034       if (fold_builtin_next_arg (exp, false))
6035         return const0_rtx;
6036       return expand_builtin_next_arg ();
6037
6038     case BUILT_IN_CLEAR_CACHE:
6039       target = expand_builtin___clear_cache (exp);
6040       if (target)
6041         return target;
6042       break;
6043
6044     case BUILT_IN_CLASSIFY_TYPE:
6045       return expand_builtin_classify_type (exp);
6046
6047     case BUILT_IN_CONSTANT_P:
6048       return const0_rtx;
6049
6050     case BUILT_IN_FRAME_ADDRESS:
6051     case BUILT_IN_RETURN_ADDRESS:
6052       return expand_builtin_frame_address (fndecl, exp);
6053
6054     /* Returns the address of the area where the structure is returned.
6055        0 otherwise.  */
6056     case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6057       if (call_expr_nargs (exp) != 0
6058           || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6059           || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6060         return const0_rtx;
6061       else
6062         return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6063
6064     case BUILT_IN_ALLOCA:
6065     case BUILT_IN_ALLOCA_WITH_ALIGN:
6066       /* If the allocation stems from the declaration of a variable-sized
6067          object, it cannot accumulate.  */
6068       target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6069       if (target)
6070         return target;
6071       break;
6072
6073     case BUILT_IN_STACK_SAVE:
6074       return expand_stack_save ();
6075
6076     case BUILT_IN_STACK_RESTORE:
6077       expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6078       return const0_rtx;
6079
6080     case BUILT_IN_BSWAP32:
6081     case BUILT_IN_BSWAP64:
6082       target = expand_builtin_bswap (exp, target, subtarget);
6083
6084       if (target)
6085         return target;
6086       break;
6087
6088     CASE_INT_FN (BUILT_IN_FFS):
6089     case BUILT_IN_FFSIMAX:
6090       target = expand_builtin_unop (target_mode, exp, target,
6091                                     subtarget, ffs_optab);
6092       if (target)
6093         return target;
6094       break;
6095
6096     CASE_INT_FN (BUILT_IN_CLZ):
6097     case BUILT_IN_CLZIMAX:
6098       target = expand_builtin_unop (target_mode, exp, target,
6099                                     subtarget, clz_optab);
6100       if (target)
6101         return target;
6102       break;
6103
6104     CASE_INT_FN (BUILT_IN_CTZ):
6105     case BUILT_IN_CTZIMAX:
6106       target = expand_builtin_unop (target_mode, exp, target,
6107                                     subtarget, ctz_optab);
6108       if (target)
6109         return target;
6110       break;
6111
6112     CASE_INT_FN (BUILT_IN_CLRSB):
6113     case BUILT_IN_CLRSBIMAX:
6114       target = expand_builtin_unop (target_mode, exp, target,
6115                                     subtarget, clrsb_optab);
6116       if (target)
6117         return target;
6118       break;
6119
6120     CASE_INT_FN (BUILT_IN_POPCOUNT):
6121     case BUILT_IN_POPCOUNTIMAX:
6122       target = expand_builtin_unop (target_mode, exp, target,
6123                                     subtarget, popcount_optab);
6124       if (target)
6125         return target;
6126       break;
6127
6128     CASE_INT_FN (BUILT_IN_PARITY):
6129     case BUILT_IN_PARITYIMAX:
6130       target = expand_builtin_unop (target_mode, exp, target,
6131                                     subtarget, parity_optab);
6132       if (target)
6133         return target;
6134       break;
6135
6136     case BUILT_IN_STRLEN:
6137       target = expand_builtin_strlen (exp, target, target_mode);
6138       if (target)
6139         return target;
6140       break;
6141
6142     case BUILT_IN_STRCPY:
6143       target = expand_builtin_strcpy (exp, target);
6144       if (target)
6145         return target;
6146       break;
6147
6148     case BUILT_IN_STRNCPY:
6149       target = expand_builtin_strncpy (exp, target);
6150       if (target)
6151         return target;
6152       break;
6153
6154     case BUILT_IN_STPCPY:
6155       target = expand_builtin_stpcpy (exp, target, mode);
6156       if (target)
6157         return target;
6158       break;
6159
6160     case BUILT_IN_MEMCPY:
6161       target = expand_builtin_memcpy (exp, target);
6162       if (target)
6163         return target;
6164       break;
6165
6166     case BUILT_IN_MEMPCPY:
6167       target = expand_builtin_mempcpy (exp, target, mode);
6168       if (target)
6169         return target;
6170       break;
6171
6172     case BUILT_IN_MEMSET:
6173       target = expand_builtin_memset (exp, target, mode);
6174       if (target)
6175         return target;
6176       break;
6177
6178     case BUILT_IN_BZERO:
6179       target = expand_builtin_bzero (exp);
6180       if (target)
6181         return target;
6182       break;
6183
6184     case BUILT_IN_STRCMP:
6185       target = expand_builtin_strcmp (exp, target);
6186       if (target)
6187         return target;
6188       break;
6189
6190     case BUILT_IN_STRNCMP:
6191       target = expand_builtin_strncmp (exp, target, mode);
6192       if (target)
6193         return target;
6194       break;
6195
6196     case BUILT_IN_BCMP:
6197     case BUILT_IN_MEMCMP:
6198       target = expand_builtin_memcmp (exp, target, mode);
6199       if (target)
6200         return target;
6201       break;
6202
6203     case BUILT_IN_SETJMP:
6204       /* This should have been lowered to the builtins below.  */
6205       gcc_unreachable ();
6206
6207     case BUILT_IN_SETJMP_SETUP:
6208       /* __builtin_setjmp_setup is passed a pointer to an array of five words
6209           and the receiver label.  */
6210       if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6211         {
6212           rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6213                                       VOIDmode, EXPAND_NORMAL);
6214           tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6215           rtx label_r = label_rtx (label);
6216
6217           /* This is copied from the handling of non-local gotos.  */
6218           expand_builtin_setjmp_setup (buf_addr, label_r);
6219           nonlocal_goto_handler_labels
6220             = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6221                                  nonlocal_goto_handler_labels);
6222           /* ??? Do not let expand_label treat us as such since we would
6223              not want to be both on the list of non-local labels and on
6224              the list of forced labels.  */
6225           FORCED_LABEL (label) = 0;
6226           return const0_rtx;
6227         }
6228       break;
6229
6230     case BUILT_IN_SETJMP_DISPATCHER:
6231        /* __builtin_setjmp_dispatcher is passed the dispatcher label.  */
6232       if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6233         {
6234           tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6235           rtx label_r = label_rtx (label);
6236
6237           /* Remove the dispatcher label from the list of non-local labels
6238              since the receiver labels have been added to it above.  */
6239           remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6240           return const0_rtx;
6241         }
6242       break;
6243
6244     case BUILT_IN_SETJMP_RECEIVER:
6245        /* __builtin_setjmp_receiver is passed the receiver label.  */
6246       if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6247         {
6248           tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6249           rtx label_r = label_rtx (label);
6250
6251           expand_builtin_setjmp_receiver (label_r);
6252           return const0_rtx;
6253         }
6254       break;
6255
6256       /* __builtin_longjmp is passed a pointer to an array of five words.
6257          It's similar to the C library longjmp function but works with
6258          __builtin_setjmp above.  */
6259     case BUILT_IN_LONGJMP:
6260       if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6261         {
6262           rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6263                                       VOIDmode, EXPAND_NORMAL);
6264           rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6265
6266           if (value != const1_rtx)
6267             {
6268               error ("%<__builtin_longjmp%> second argument must be 1");
6269               return const0_rtx;
6270             }
6271
6272           expand_builtin_longjmp (buf_addr, value);
6273           return const0_rtx;
6274         }
6275       break;
6276
6277     case BUILT_IN_NONLOCAL_GOTO:
6278       target = expand_builtin_nonlocal_goto (exp);
6279       if (target)
6280         return target;
6281       break;
6282
6283       /* This updates the setjmp buffer that is its argument with the value
6284          of the current stack pointer.  */
6285     case BUILT_IN_UPDATE_SETJMP_BUF:
6286       if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6287         {
6288           rtx buf_addr
6289             = expand_normal (CALL_EXPR_ARG (exp, 0));
6290
6291           expand_builtin_update_setjmp_buf (buf_addr);
6292           return const0_rtx;
6293         }
6294       break;
6295
6296     case BUILT_IN_TRAP:
6297       expand_builtin_trap ();
6298       return const0_rtx;
6299
6300     case BUILT_IN_UNREACHABLE:
6301       expand_builtin_unreachable ();
6302       return const0_rtx;
6303
6304     CASE_FLT_FN (BUILT_IN_SIGNBIT):
6305     case BUILT_IN_SIGNBITD32:
6306     case BUILT_IN_SIGNBITD64:
6307     case BUILT_IN_SIGNBITD128:
6308       target = expand_builtin_signbit (exp, target);
6309       if (target)
6310         return target;
6311       break;
6312
6313       /* Various hooks for the DWARF 2 __throw routine.  */
6314     case BUILT_IN_UNWIND_INIT:
6315       expand_builtin_unwind_init ();
6316       return const0_rtx;
6317     case BUILT_IN_DWARF_CFA:
6318       return virtual_cfa_rtx;
6319 #ifdef DWARF2_UNWIND_INFO
6320     case BUILT_IN_DWARF_SP_COLUMN:
6321       return expand_builtin_dwarf_sp_column ();
6322     case BUILT_IN_INIT_DWARF_REG_SIZES:
6323       expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6324       return const0_rtx;
6325 #endif
6326     case BUILT_IN_FROB_RETURN_ADDR:
6327       return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6328     case BUILT_IN_EXTRACT_RETURN_ADDR:
6329       return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6330     case BUILT_IN_EH_RETURN:
6331       expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6332                                 CALL_EXPR_ARG (exp, 1));
6333       return const0_rtx;
6334 #ifdef EH_RETURN_DATA_REGNO
6335     case BUILT_IN_EH_RETURN_DATA_REGNO:
6336       return expand_builtin_eh_return_data_regno (exp);
6337 #endif
6338     case BUILT_IN_EXTEND_POINTER:
6339       return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6340     case BUILT_IN_EH_POINTER:
6341       return expand_builtin_eh_pointer (exp);
6342     case BUILT_IN_EH_FILTER:
6343       return expand_builtin_eh_filter (exp);
6344     case BUILT_IN_EH_COPY_VALUES:
6345       return expand_builtin_eh_copy_values (exp);
6346
6347     case BUILT_IN_VA_START:
6348       return expand_builtin_va_start (exp);
6349     case BUILT_IN_VA_END:
6350       return expand_builtin_va_end (exp);
6351     case BUILT_IN_VA_COPY:
6352       return expand_builtin_va_copy (exp);
6353     case BUILT_IN_EXPECT:
6354       return expand_builtin_expect (exp, target);
6355     case BUILT_IN_ASSUME_ALIGNED:
6356       return expand_builtin_assume_aligned (exp, target);
6357     case BUILT_IN_PREFETCH:
6358       expand_builtin_prefetch (exp);
6359       return const0_rtx;
6360
6361     case BUILT_IN_INIT_TRAMPOLINE:
6362       return expand_builtin_init_trampoline (exp, true);
6363     case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6364       return expand_builtin_init_trampoline (exp, false);
6365     case BUILT_IN_ADJUST_TRAMPOLINE:
6366       return expand_builtin_adjust_trampoline (exp);
6367
6368     case BUILT_IN_FORK:
6369     case BUILT_IN_EXECL:
6370     case BUILT_IN_EXECV:
6371     case BUILT_IN_EXECLP:
6372     case BUILT_IN_EXECLE:
6373     case BUILT_IN_EXECVP:
6374     case BUILT_IN_EXECVE:
6375       target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6376       if (target)
6377         return target;
6378       break;
6379
6380     case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6381     case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6382     case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6383     case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6384     case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6385       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6386       target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6387       if (target)
6388         return target;
6389       break;
6390
6391     case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6392     case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6393     case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6394     case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6395     case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6396       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6397       target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6398       if (target)
6399         return target;
6400       break;
6401
6402     case BUILT_IN_SYNC_FETCH_AND_OR_1:
6403     case BUILT_IN_SYNC_FETCH_AND_OR_2:
6404     case BUILT_IN_SYNC_FETCH_AND_OR_4:
6405     case BUILT_IN_SYNC_FETCH_AND_OR_8:
6406     case BUILT_IN_SYNC_FETCH_AND_OR_16:
6407       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6408       target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6409       if (target)
6410         return target;
6411       break;
6412
6413     case BUILT_IN_SYNC_FETCH_AND_AND_1:
6414     case BUILT_IN_SYNC_FETCH_AND_AND_2:
6415     case BUILT_IN_SYNC_FETCH_AND_AND_4:
6416     case BUILT_IN_SYNC_FETCH_AND_AND_8:
6417     case BUILT_IN_SYNC_FETCH_AND_AND_16:
6418       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6419       target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6420       if (target)
6421         return target;
6422       break;
6423
6424     case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6425     case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6426     case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6427     case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6428     case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6429       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6430       target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6431       if (target)
6432         return target;
6433       break;
6434
6435     case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6436     case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6437     case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6438     case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6439     case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6440       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6441       target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6442       if (target)
6443         return target;
6444       break;
6445
6446     case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6447     case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6448     case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6449     case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6450     case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6451       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6452       target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6453       if (target)
6454         return target;
6455       break;
6456
6457     case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6458     case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6459     case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6460     case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6461     case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6462       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6463       target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6464       if (target)
6465         return target;
6466       break;
6467
6468     case BUILT_IN_SYNC_OR_AND_FETCH_1:
6469     case BUILT_IN_SYNC_OR_AND_FETCH_2:
6470     case BUILT_IN_SYNC_OR_AND_FETCH_4:
6471     case BUILT_IN_SYNC_OR_AND_FETCH_8:
6472     case BUILT_IN_SYNC_OR_AND_FETCH_16:
6473       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6474       target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6475       if (target)
6476         return target;
6477       break;
6478
6479     case BUILT_IN_SYNC_AND_AND_FETCH_1:
6480     case BUILT_IN_SYNC_AND_AND_FETCH_2:
6481     case BUILT_IN_SYNC_AND_AND_FETCH_4:
6482     case BUILT_IN_SYNC_AND_AND_FETCH_8:
6483     case BUILT_IN_SYNC_AND_AND_FETCH_16:
6484       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6485       target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6486       if (target)
6487         return target;
6488       break;
6489
6490     case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6491     case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6492     case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6493     case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6494     case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6495       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6496       target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6497       if (target)
6498         return target;
6499       break;
6500
6501     case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6502     case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6503     case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6504     case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6505     case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6506       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6507       target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6508       if (target)
6509         return target;
6510       break;
6511
6512     case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6513     case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6514     case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6515     case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6516     case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6517       if (mode == VOIDmode)
6518         mode = TYPE_MODE (boolean_type_node);
6519       if (!target || !register_operand (target, mode))
6520         target = gen_reg_rtx (mode);
6521
6522       mode = get_builtin_sync_mode 
6523                                 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6524       target = expand_builtin_compare_and_swap (mode, exp, true, target);
6525       if (target)
6526         return target;
6527       break;
6528
6529     case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6530     case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6531     case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6532     case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6533     case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6534       mode = get_builtin_sync_mode 
6535                                 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6536       target = expand_builtin_compare_and_swap (mode, exp, false, target);
6537       if (target)
6538         return target;
6539       break;
6540
6541     case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6542     case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6543     case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6544     case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6545     case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6546       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6547       target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6548       if (target)
6549         return target;
6550       break;
6551
6552     case BUILT_IN_SYNC_LOCK_RELEASE_1:
6553     case BUILT_IN_SYNC_LOCK_RELEASE_2:
6554     case BUILT_IN_SYNC_LOCK_RELEASE_4:
6555     case BUILT_IN_SYNC_LOCK_RELEASE_8:
6556     case BUILT_IN_SYNC_LOCK_RELEASE_16:
6557       mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6558       expand_builtin_sync_lock_release (mode, exp);
6559       return const0_rtx;
6560
6561     case BUILT_IN_SYNC_SYNCHRONIZE:
6562       expand_builtin_sync_synchronize ();
6563       return const0_rtx;
6564
6565     case BUILT_IN_ATOMIC_EXCHANGE_1:
6566     case BUILT_IN_ATOMIC_EXCHANGE_2:
6567     case BUILT_IN_ATOMIC_EXCHANGE_4:
6568     case BUILT_IN_ATOMIC_EXCHANGE_8:
6569     case BUILT_IN_ATOMIC_EXCHANGE_16:
6570       mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6571       target = expand_builtin_atomic_exchange (mode, exp, target);
6572       if (target)
6573         return target;
6574       break;
6575
6576     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6577     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6578     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6579     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6580     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6581       {
6582         unsigned int nargs, z;
6583         VEC(tree,gc) *vec;
6584
6585         mode = 
6586             get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6587         target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6588         if (target)
6589           return target;
6590
6591         /* If this is turned into an external library call, the weak parameter
6592            must be dropped to match the expected parameter list.  */
6593         nargs = call_expr_nargs (exp);
6594         vec = VEC_alloc (tree, gc, nargs - 1);
6595         for (z = 0; z < 3; z++)
6596           VEC_quick_push (tree, vec, CALL_EXPR_ARG (exp, z));
6597         /* Skip the boolean weak parameter.  */
6598         for (z = 4; z < 6; z++)
6599           VEC_quick_push (tree, vec, CALL_EXPR_ARG (exp, z));
6600         exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6601         break;
6602       }
6603
6604     case BUILT_IN_ATOMIC_LOAD_1:
6605     case BUILT_IN_ATOMIC_LOAD_2:
6606     case BUILT_IN_ATOMIC_LOAD_4:
6607     case BUILT_IN_ATOMIC_LOAD_8:
6608     case BUILT_IN_ATOMIC_LOAD_16:
6609       mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6610       target = expand_builtin_atomic_load (mode, exp, target);
6611       if (target)
6612         return target;
6613       break;
6614
6615     case BUILT_IN_ATOMIC_STORE_1:
6616     case BUILT_IN_ATOMIC_STORE_2:
6617     case BUILT_IN_ATOMIC_STORE_4:
6618     case BUILT_IN_ATOMIC_STORE_8:
6619     case BUILT_IN_ATOMIC_STORE_16:
6620       mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6621       target = expand_builtin_atomic_store (mode, exp);
6622       if (target)
6623         return const0_rtx;
6624       break;
6625
6626     case BUILT_IN_ATOMIC_ADD_FETCH_1:
6627     case BUILT_IN_ATOMIC_ADD_FETCH_2:
6628     case BUILT_IN_ATOMIC_ADD_FETCH_4:
6629     case BUILT_IN_ATOMIC_ADD_FETCH_8:
6630     case BUILT_IN_ATOMIC_ADD_FETCH_16:
6631       {
6632         enum built_in_function lib;
6633         mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6634         lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 + 
6635                                        (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6636         target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6637                                                  ignore, lib);
6638         if (target)
6639           return target;
6640         break;
6641       }
6642     case BUILT_IN_ATOMIC_SUB_FETCH_1:
6643     case BUILT_IN_ATOMIC_SUB_FETCH_2:
6644     case BUILT_IN_ATOMIC_SUB_FETCH_4:
6645     case BUILT_IN_ATOMIC_SUB_FETCH_8:
6646     case BUILT_IN_ATOMIC_SUB_FETCH_16:
6647       {
6648         enum built_in_function lib;
6649         mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6650         lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 + 
6651                                        (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6652         target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6653                                                  ignore, lib);
6654         if (target)
6655           return target;
6656         break;
6657       }
6658     case BUILT_IN_ATOMIC_AND_FETCH_1:
6659     case BUILT_IN_ATOMIC_AND_FETCH_2:
6660     case BUILT_IN_ATOMIC_AND_FETCH_4:
6661     case BUILT_IN_ATOMIC_AND_FETCH_8:
6662     case BUILT_IN_ATOMIC_AND_FETCH_16:
6663       {
6664         enum built_in_function lib;
6665         mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6666         lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 + 
6667                                        (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6668         target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6669                                                  ignore, lib);
6670         if (target)
6671           return target;
6672         break;
6673       }
6674     case BUILT_IN_ATOMIC_NAND_FETCH_1:
6675     case BUILT_IN_ATOMIC_NAND_FETCH_2:
6676     case BUILT_IN_ATOMIC_NAND_FETCH_4:
6677     case BUILT_IN_ATOMIC_NAND_FETCH_8:
6678     case BUILT_IN_ATOMIC_NAND_FETCH_16:
6679       {
6680         enum built_in_function lib;
6681         mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6682         lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 + 
6683                                        (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6684         target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6685                                                  ignore, lib);
6686         if (target)
6687           return target;
6688         break;
6689       }
6690     case BUILT_IN_ATOMIC_XOR_FETCH_1:
6691     case BUILT_IN_ATOMIC_XOR_FETCH_2:
6692     case BUILT_IN_ATOMIC_XOR_FETCH_4:
6693     case BUILT_IN_ATOMIC_XOR_FETCH_8:
6694     case BUILT_IN_ATOMIC_XOR_FETCH_16:
6695       {
6696         enum built_in_function lib;
6697         mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6698         lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 + 
6699                                        (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6700         target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6701                                                  ignore, lib);
6702         if (target)
6703           return target;
6704         break;
6705       }
6706     case BUILT_IN_ATOMIC_OR_FETCH_1:
6707     case BUILT_IN_ATOMIC_OR_FETCH_2:
6708     case BUILT_IN_ATOMIC_OR_FETCH_4:
6709     case BUILT_IN_ATOMIC_OR_FETCH_8:
6710     case BUILT_IN_ATOMIC_OR_FETCH_16:
6711       {
6712         enum built_in_function lib;
6713         mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6714         lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 + 
6715                                        (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6716         target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6717                                                  ignore, lib);
6718         if (target)
6719           return target;
6720         break;
6721       }
6722     case BUILT_IN_ATOMIC_FETCH_ADD_1:
6723     case BUILT_IN_ATOMIC_FETCH_ADD_2:
6724     case BUILT_IN_ATOMIC_FETCH_ADD_4:
6725     case BUILT_IN_ATOMIC_FETCH_ADD_8:
6726     case BUILT_IN_ATOMIC_FETCH_ADD_16:
6727       mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6728       target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6729                                                ignore, BUILT_IN_NONE);
6730       if (target)
6731         return target;
6732       break;
6733  
6734     case BUILT_IN_ATOMIC_FETCH_SUB_1:
6735     case BUILT_IN_ATOMIC_FETCH_SUB_2:
6736     case BUILT_IN_ATOMIC_FETCH_SUB_4:
6737     case BUILT_IN_ATOMIC_FETCH_SUB_8:
6738     case BUILT_IN_ATOMIC_FETCH_SUB_16:
6739       mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6740       target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6741                                                ignore, BUILT_IN_NONE);
6742       if (target)
6743         return target;
6744       break;
6745
6746     case BUILT_IN_ATOMIC_FETCH_AND_1:
6747     case BUILT_IN_ATOMIC_FETCH_AND_2:
6748     case BUILT_IN_ATOMIC_FETCH_AND_4:
6749     case BUILT_IN_ATOMIC_FETCH_AND_8:
6750     case BUILT_IN_ATOMIC_FETCH_AND_16:
6751       mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6752       target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6753                                                ignore, BUILT_IN_NONE);
6754       if (target)
6755         return target;
6756       break;
6757   
6758     case BUILT_IN_ATOMIC_FETCH_NAND_1:
6759     case BUILT_IN_ATOMIC_FETCH_NAND_2:
6760     case BUILT_IN_ATOMIC_FETCH_NAND_4:
6761     case BUILT_IN_ATOMIC_FETCH_NAND_8:
6762     case BUILT_IN_ATOMIC_FETCH_NAND_16:
6763       mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6764       target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6765                                                ignore, BUILT_IN_NONE);
6766       if (target)
6767         return target;
6768       break;
6769  
6770     case BUILT_IN_ATOMIC_FETCH_XOR_1:
6771     case BUILT_IN_ATOMIC_FETCH_XOR_2:
6772     case BUILT_IN_ATOMIC_FETCH_XOR_4:
6773     case BUILT_IN_ATOMIC_FETCH_XOR_8:
6774     case BUILT_IN_ATOMIC_FETCH_XOR_16:
6775       mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6776       target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6777                                                ignore, BUILT_IN_NONE);
6778       if (target)
6779         return target;
6780       break;
6781  
6782     case BUILT_IN_ATOMIC_FETCH_OR_1:
6783     case BUILT_IN_ATOMIC_FETCH_OR_2:
6784     case BUILT_IN_ATOMIC_FETCH_OR_4:
6785     case BUILT_IN_ATOMIC_FETCH_OR_8:
6786     case BUILT_IN_ATOMIC_FETCH_OR_16:
6787       mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6788       target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6789                                                ignore, BUILT_IN_NONE);
6790       if (target)
6791         return target;
6792       break;
6793
6794     case BUILT_IN_ATOMIC_TEST_AND_SET:
6795       return expand_builtin_atomic_test_and_set (exp, target);
6796
6797     case BUILT_IN_ATOMIC_CLEAR:
6798       return expand_builtin_atomic_clear (exp);
6799  
6800     case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6801       return expand_builtin_atomic_always_lock_free (exp);
6802
6803     case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6804       target = expand_builtin_atomic_is_lock_free (exp);
6805       if (target)
6806         return target;
6807       break;
6808
6809     case BUILT_IN_ATOMIC_THREAD_FENCE:
6810       expand_builtin_atomic_thread_fence (exp);
6811       return const0_rtx;
6812
6813     case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6814       expand_builtin_atomic_signal_fence (exp);
6815       return const0_rtx;
6816
6817     case BUILT_IN_OBJECT_SIZE:
6818       return expand_builtin_object_size (exp);
6819
6820     case BUILT_IN_MEMCPY_CHK:
6821     case BUILT_IN_MEMPCPY_CHK:
6822     case BUILT_IN_MEMMOVE_CHK:
6823     case BUILT_IN_MEMSET_CHK:
6824       target = expand_builtin_memory_chk (exp, target, mode, fcode);
6825       if (target)
6826         return target;
6827       break;
6828
6829     case BUILT_IN_STRCPY_CHK:
6830     case BUILT_IN_STPCPY_CHK:
6831     case BUILT_IN_STRNCPY_CHK:
6832     case BUILT_IN_STPNCPY_CHK:
6833     case BUILT_IN_STRCAT_CHK:
6834     case BUILT_IN_STRNCAT_CHK:
6835     case BUILT_IN_SNPRINTF_CHK:
6836     case BUILT_IN_VSNPRINTF_CHK:
6837       maybe_emit_chk_warning (exp, fcode);
6838       break;
6839
6840     case BUILT_IN_SPRINTF_CHK:
6841     case BUILT_IN_VSPRINTF_CHK:
6842       maybe_emit_sprintf_chk_warning (exp, fcode);
6843       break;
6844
6845     case BUILT_IN_FREE:
6846       if (warn_free_nonheap_object)
6847         maybe_emit_free_warning (exp);
6848       break;
6849
6850     default:    /* just do library call, if unknown builtin */
6851       break;
6852     }
6853
6854   /* The switch statement above can drop through to cause the function
6855      to be called normally.  */
6856   return expand_call (exp, target, ignore);
6857 }
6858
6859 /* Determine whether a tree node represents a call to a built-in
6860    function.  If the tree T is a call to a built-in function with
6861    the right number of arguments of the appropriate types, return
6862    the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6863    Otherwise the return value is END_BUILTINS.  */
6864
6865 enum built_in_function
6866 builtin_mathfn_code (const_tree t)
6867 {
6868   const_tree fndecl, arg, parmlist;
6869   const_tree argtype, parmtype;
6870   const_call_expr_arg_iterator iter;
6871
6872   if (TREE_CODE (t) != CALL_EXPR
6873       || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6874     return END_BUILTINS;
6875
6876   fndecl = get_callee_fndecl (t);
6877   if (fndecl == NULL_TREE
6878       || TREE_CODE (fndecl) != FUNCTION_DECL
6879       || ! DECL_BUILT_IN (fndecl)
6880       || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6881     return END_BUILTINS;
6882
6883   parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6884   init_const_call_expr_arg_iterator (t, &iter);
6885   for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6886     {
6887       /* If a function doesn't take a variable number of arguments,
6888          the last element in the list will have type `void'.  */
6889       parmtype = TREE_VALUE (parmlist);
6890       if (VOID_TYPE_P (parmtype))
6891         {
6892           if (more_const_call_expr_args_p (&iter))
6893             return END_BUILTINS;
6894           return DECL_FUNCTION_CODE (fndecl);
6895         }
6896
6897       if (! more_const_call_expr_args_p (&iter))
6898         return END_BUILTINS;
6899
6900       arg = next_const_call_expr_arg (&iter);
6901       argtype = TREE_TYPE (arg);
6902
6903       if (SCALAR_FLOAT_TYPE_P (parmtype))
6904         {
6905           if (! SCALAR_FLOAT_TYPE_P (argtype))
6906             return END_BUILTINS;
6907         }
6908       else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6909         {
6910           if (! COMPLEX_FLOAT_TYPE_P (argtype))
6911             return END_BUILTINS;
6912         }
6913       else if (POINTER_TYPE_P (parmtype))
6914         {
6915           if (! POINTER_TYPE_P (argtype))
6916             return END_BUILTINS;
6917         }
6918       else if (INTEGRAL_TYPE_P (parmtype))
6919         {
6920           if (! INTEGRAL_TYPE_P (argtype))
6921             return END_BUILTINS;
6922         }
6923       else
6924         return END_BUILTINS;
6925     }
6926
6927   /* Variable-length argument list.  */
6928   return DECL_FUNCTION_CODE (fndecl);
6929 }
6930
6931 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6932    evaluate to a constant.  */
6933
6934 static tree
6935 fold_builtin_constant_p (tree arg)
6936 {
6937   /* We return 1 for a numeric type that's known to be a constant
6938      value at compile-time or for an aggregate type that's a
6939      literal constant.  */
6940   STRIP_NOPS (arg);
6941
6942   /* If we know this is a constant, emit the constant of one.  */
6943   if (CONSTANT_CLASS_P (arg)
6944       || (TREE_CODE (arg) == CONSTRUCTOR
6945           && TREE_CONSTANT (arg)))
6946     return integer_one_node;
6947   if (TREE_CODE (arg) == ADDR_EXPR)
6948     {
6949        tree op = TREE_OPERAND (arg, 0);
6950        if (TREE_CODE (op) == STRING_CST
6951            || (TREE_CODE (op) == ARRAY_REF
6952                && integer_zerop (TREE_OPERAND (op, 1))
6953                && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6954          return integer_one_node;
6955     }
6956
6957   /* If this expression has side effects, show we don't know it to be a
6958      constant.  Likewise if it's a pointer or aggregate type since in
6959      those case we only want literals, since those are only optimized
6960      when generating RTL, not later.
6961      And finally, if we are compiling an initializer, not code, we
6962      need to return a definite result now; there's not going to be any
6963      more optimization done.  */
6964   if (TREE_SIDE_EFFECTS (arg)
6965       || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6966       || POINTER_TYPE_P (TREE_TYPE (arg))
6967       || cfun == 0
6968       || folding_initializer)
6969     return integer_zero_node;
6970
6971   return NULL_TREE;
6972 }
6973
6974 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6975    return it as a truthvalue.  */
6976
6977 static tree
6978 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6979 {
6980   tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6981
6982   fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6983   arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6984   ret_type = TREE_TYPE (TREE_TYPE (fn));
6985   pred_type = TREE_VALUE (arg_types);
6986   expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6987
6988   pred = fold_convert_loc (loc, pred_type, pred);
6989   expected = fold_convert_loc (loc, expected_type, expected);
6990   call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6991
6992   return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6993                  build_int_cst (ret_type, 0));
6994 }
6995
6996 /* Fold a call to builtin_expect with arguments ARG0 and ARG1.  Return
6997    NULL_TREE if no simplification is possible.  */
6998
6999 static tree
7000 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
7001 {
7002   tree inner, fndecl, inner_arg0;
7003   enum tree_code code;
7004
7005   /* Distribute the expected value over short-circuiting operators.
7006      See through the cast from truthvalue_type_node to long.  */
7007   inner_arg0 = arg0;
7008   while (TREE_CODE (inner_arg0) == NOP_EXPR
7009          && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7010          && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7011     inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7012
7013   /* If this is a builtin_expect within a builtin_expect keep the
7014      inner one.  See through a comparison against a constant.  It
7015      might have been added to create a thruthvalue.  */
7016   inner = inner_arg0;
7017
7018   if (COMPARISON_CLASS_P (inner)
7019       && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7020     inner = TREE_OPERAND (inner, 0);
7021
7022   if (TREE_CODE (inner) == CALL_EXPR
7023       && (fndecl = get_callee_fndecl (inner))
7024       && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7025       && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7026     return arg0;
7027
7028   inner = inner_arg0;
7029   code = TREE_CODE (inner);
7030   if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7031     {
7032       tree op0 = TREE_OPERAND (inner, 0);
7033       tree op1 = TREE_OPERAND (inner, 1);
7034
7035       op0 = build_builtin_expect_predicate (loc, op0, arg1);
7036       op1 = build_builtin_expect_predicate (loc, op1, arg1);
7037       inner = build2 (code, TREE_TYPE (inner), op0, op1);
7038
7039       return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7040     }
7041
7042   /* If the argument isn't invariant then there's nothing else we can do.  */
7043   if (!TREE_CONSTANT (inner_arg0))
7044     return NULL_TREE;
7045
7046   /* If we expect that a comparison against the argument will fold to
7047      a constant return the constant.  In practice, this means a true
7048      constant or the address of a non-weak symbol.  */
7049   inner = inner_arg0;
7050   STRIP_NOPS (inner);
7051   if (TREE_CODE (inner) == ADDR_EXPR)
7052     {
7053       do
7054         {
7055           inner = TREE_OPERAND (inner, 0);
7056         }
7057       while (TREE_CODE (inner) == COMPONENT_REF
7058              || TREE_CODE (inner) == ARRAY_REF);
7059       if ((TREE_CODE (inner) == VAR_DECL
7060            || TREE_CODE (inner) == FUNCTION_DECL)
7061           && DECL_WEAK (inner))
7062         return NULL_TREE;
7063     }
7064
7065   /* Otherwise, ARG0 already has the proper type for the return value.  */
7066   return arg0;
7067 }
7068
7069 /* Fold a call to __builtin_classify_type with argument ARG.  */
7070
7071 static tree
7072 fold_builtin_classify_type (tree arg)
7073 {
7074   if (arg == 0)
7075     return build_int_cst (integer_type_node, no_type_class);
7076
7077   return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7078 }
7079
7080 /* Fold a call to __builtin_strlen with argument ARG.  */
7081
7082 static tree
7083 fold_builtin_strlen (location_t loc, tree type, tree arg)
7084 {
7085   if (!validate_arg (arg, POINTER_TYPE))
7086     return NULL_TREE;
7087   else
7088     {
7089       tree len = c_strlen (arg, 0);
7090
7091       if (len)
7092         return fold_convert_loc (loc, type, len);
7093
7094       return NULL_TREE;
7095     }
7096 }
7097
7098 /* Fold a call to __builtin_inf or __builtin_huge_val.  */
7099
7100 static tree
7101 fold_builtin_inf (location_t loc, tree type, int warn)
7102 {
7103   REAL_VALUE_TYPE real;
7104
7105   /* __builtin_inff is intended to be usable to define INFINITY on all
7106      targets.  If an infinity is not available, INFINITY expands "to a
7107      positive constant of type float that overflows at translation
7108      time", footnote "In this case, using INFINITY will violate the
7109      constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7110      Thus we pedwarn to ensure this constraint violation is
7111      diagnosed.  */
7112   if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7113     pedwarn (loc, 0, "target format does not support infinity");
7114
7115   real_inf (&real);
7116   return build_real (type, real);
7117 }
7118
7119 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG.  */
7120
7121 static tree
7122 fold_builtin_nan (tree arg, tree type, int quiet)
7123 {
7124   REAL_VALUE_TYPE real;
7125   const char *str;
7126
7127   if (!validate_arg (arg, POINTER_TYPE))
7128     return NULL_TREE;
7129   str = c_getstr (arg);
7130   if (!str)
7131     return NULL_TREE;
7132
7133   if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7134     return NULL_TREE;
7135
7136   return build_real (type, real);
7137 }
7138
7139 /* Return true if the floating point expression T has an integer value.
7140    We also allow +Inf, -Inf and NaN to be considered integer values.  */
7141
7142 static bool
7143 integer_valued_real_p (tree t)
7144 {
7145   switch (TREE_CODE (t))
7146     {
7147     case FLOAT_EXPR:
7148       return true;
7149
7150     case ABS_EXPR:
7151     case SAVE_EXPR:
7152       return integer_valued_real_p (TREE_OPERAND (t, 0));
7153
7154     case COMPOUND_EXPR:
7155     case MODIFY_EXPR:
7156     case BIND_EXPR:
7157       return integer_valued_real_p (TREE_OPERAND (t, 1));
7158
7159     case PLUS_EXPR:
7160     case MINUS_EXPR:
7161     case MULT_EXPR:
7162     case MIN_EXPR:
7163     case MAX_EXPR:
7164       return integer_valued_real_p (TREE_OPERAND (t, 0))
7165              && integer_valued_real_p (TREE_OPERAND (t, 1));
7166
7167     case COND_EXPR:
7168       return integer_valued_real_p (TREE_OPERAND (t, 1))
7169              && integer_valued_real_p (TREE_OPERAND (t, 2));
7170
7171     case REAL_CST:
7172       return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7173
7174     case NOP_EXPR:
7175       {
7176         tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7177         if (TREE_CODE (type) == INTEGER_TYPE)
7178           return true;
7179         if (TREE_CODE (type) == REAL_TYPE)
7180           return integer_valued_real_p (TREE_OPERAND (t, 0));
7181         break;
7182       }
7183
7184     case CALL_EXPR:
7185       switch (builtin_mathfn_code (t))
7186         {
7187         CASE_FLT_FN (BUILT_IN_CEIL):
7188         CASE_FLT_FN (BUILT_IN_FLOOR):
7189         CASE_FLT_FN (BUILT_IN_NEARBYINT):
7190         CASE_FLT_FN (BUILT_IN_RINT):
7191         CASE_FLT_FN (BUILT_IN_ROUND):
7192         CASE_FLT_FN (BUILT_IN_TRUNC):
7193           return true;
7194
7195         CASE_FLT_FN (BUILT_IN_FMIN):
7196         CASE_FLT_FN (BUILT_IN_FMAX):
7197           return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7198             && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7199
7200         default:
7201           break;
7202         }
7203       break;
7204
7205     default:
7206       break;
7207     }
7208   return false;
7209 }
7210
7211 /* FNDECL is assumed to be a builtin where truncation can be propagated
7212    across (for instance floor((double)f) == (double)floorf (f).
7213    Do the transformation for a call with argument ARG.  */
7214
7215 static tree
7216 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7217 {
7218   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7219
7220   if (!validate_arg (arg, REAL_TYPE))
7221     return NULL_TREE;
7222
7223   /* Integer rounding functions are idempotent.  */
7224   if (fcode == builtin_mathfn_code (arg))
7225     return arg;
7226
7227   /* If argument is already integer valued, and we don't need to worry
7228      about setting errno, there's no need to perform rounding.  */
7229   if (! flag_errno_math && integer_valued_real_p (arg))
7230     return arg;
7231
7232   if (optimize)
7233     {
7234       tree arg0 = strip_float_extensions (arg);
7235       tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7236       tree newtype = TREE_TYPE (arg0);
7237       tree decl;
7238
7239       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7240           && (decl = mathfn_built_in (newtype, fcode)))
7241         return fold_convert_loc (loc, ftype,
7242                                  build_call_expr_loc (loc, decl, 1,
7243                                                   fold_convert_loc (loc,
7244                                                                     newtype,
7245                                                                     arg0)));
7246     }
7247   return NULL_TREE;
7248 }
7249
7250 /* FNDECL is assumed to be builtin which can narrow the FP type of
7251    the argument, for instance lround((double)f) -> lroundf (f).
7252    Do the transformation for a call with argument ARG.  */
7253
7254 static tree
7255 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7256 {
7257   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7258
7259   if (!validate_arg (arg, REAL_TYPE))
7260     return NULL_TREE;
7261
7262   /* If argument is already integer valued, and we don't need to worry
7263      about setting errno, there's no need to perform rounding.  */
7264   if (! flag_errno_math && integer_valued_real_p (arg))
7265     return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7266                         TREE_TYPE (TREE_TYPE (fndecl)), arg);
7267
7268   if (optimize)
7269     {
7270       tree ftype = TREE_TYPE (arg);
7271       tree arg0 = strip_float_extensions (arg);
7272       tree newtype = TREE_TYPE (arg0);
7273       tree decl;
7274
7275       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7276           && (decl = mathfn_built_in (newtype, fcode)))
7277         return build_call_expr_loc (loc, decl, 1,
7278                                 fold_convert_loc (loc, newtype, arg0));
7279     }
7280
7281   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7282      sizeof (int) == sizeof (long).  */
7283   if (TYPE_PRECISION (integer_type_node)
7284       == TYPE_PRECISION (long_integer_type_node))
7285     {
7286       tree newfn = NULL_TREE;
7287       switch (fcode)
7288         {
7289         CASE_FLT_FN (BUILT_IN_ICEIL):
7290           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7291           break;
7292
7293         CASE_FLT_FN (BUILT_IN_IFLOOR):
7294           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7295           break;
7296
7297         CASE_FLT_FN (BUILT_IN_IROUND):
7298           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7299           break;
7300
7301         CASE_FLT_FN (BUILT_IN_IRINT):
7302           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7303           break;
7304
7305         default:
7306           break;
7307         }
7308
7309       if (newfn)
7310         {
7311           tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7312           return fold_convert_loc (loc,
7313                                    TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7314         }
7315     }
7316
7317   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7318      sizeof (long long) == sizeof (long).  */
7319   if (TYPE_PRECISION (long_long_integer_type_node)
7320       == TYPE_PRECISION (long_integer_type_node))
7321     {
7322       tree newfn = NULL_TREE;
7323       switch (fcode)
7324         {
7325         CASE_FLT_FN (BUILT_IN_LLCEIL):
7326           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7327           break;
7328
7329         CASE_FLT_FN (BUILT_IN_LLFLOOR):
7330           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7331           break;
7332
7333         CASE_FLT_FN (BUILT_IN_LLROUND):
7334           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7335           break;
7336
7337         CASE_FLT_FN (BUILT_IN_LLRINT):
7338           newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7339           break;
7340
7341         default:
7342           break;
7343         }
7344
7345       if (newfn)
7346         {
7347           tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7348           return fold_convert_loc (loc,
7349                                    TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7350         }
7351     }
7352
7353   return NULL_TREE;
7354 }
7355
7356 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG.  TYPE is the
7357    return type.  Return NULL_TREE if no simplification can be made.  */
7358
7359 static tree
7360 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7361 {
7362   tree res;
7363
7364   if (!validate_arg (arg, COMPLEX_TYPE)
7365       || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7366     return NULL_TREE;
7367
7368   /* Calculate the result when the argument is a constant.  */
7369   if (TREE_CODE (arg) == COMPLEX_CST
7370       && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7371                               type, mpfr_hypot)))
7372     return res;
7373
7374   if (TREE_CODE (arg) == COMPLEX_EXPR)
7375     {
7376       tree real = TREE_OPERAND (arg, 0);
7377       tree imag = TREE_OPERAND (arg, 1);
7378
7379       /* If either part is zero, cabs is fabs of the other.  */
7380       if (real_zerop (real))
7381         return fold_build1_loc (loc, ABS_EXPR, type, imag);
7382       if (real_zerop (imag))
7383         return fold_build1_loc (loc, ABS_EXPR, type, real);
7384
7385       /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7386       if (flag_unsafe_math_optimizations
7387           && operand_equal_p (real, imag, OEP_PURE_SAME))
7388         {
7389           const REAL_VALUE_TYPE sqrt2_trunc
7390             = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7391           STRIP_NOPS (real);
7392           return fold_build2_loc (loc, MULT_EXPR, type,
7393                               fold_build1_loc (loc, ABS_EXPR, type, real),
7394                               build_real (type, sqrt2_trunc));
7395         }
7396     }
7397
7398   /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z).  */
7399   if (TREE_CODE (arg) == NEGATE_EXPR
7400       || TREE_CODE (arg) == CONJ_EXPR)
7401     return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7402
7403   /* Don't do this when optimizing for size.  */
7404   if (flag_unsafe_math_optimizations
7405       && optimize && optimize_function_for_speed_p (cfun))
7406     {
7407       tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7408
7409       if (sqrtfn != NULL_TREE)
7410         {
7411           tree rpart, ipart, result;
7412
7413           arg = builtin_save_expr (arg);
7414
7415           rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7416           ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7417
7418           rpart = builtin_save_expr (rpart);
7419           ipart = builtin_save_expr (ipart);
7420
7421           result = fold_build2_loc (loc, PLUS_EXPR, type,
7422                                 fold_build2_loc (loc, MULT_EXPR, type,
7423                                              rpart, rpart),
7424                                 fold_build2_loc (loc, MULT_EXPR, type,
7425                                              ipart, ipart));
7426
7427           return build_call_expr_loc (loc, sqrtfn, 1, result);
7428         }
7429     }
7430
7431   return NULL_TREE;
7432 }
7433
7434 /* Build a complex (inf +- 0i) for the result of cproj.  TYPE is the
7435    complex tree type of the result.  If NEG is true, the imaginary
7436    zero is negative.  */
7437
7438 static tree
7439 build_complex_cproj (tree type, bool neg)
7440 {
7441   REAL_VALUE_TYPE rinf, rzero = dconst0;
7442   
7443   real_inf (&rinf);
7444   rzero.sign = neg;
7445   return build_complex (type, build_real (TREE_TYPE (type), rinf),
7446                         build_real (TREE_TYPE (type), rzero));
7447 }
7448
7449 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG.  TYPE is the
7450    return type.  Return NULL_TREE if no simplification can be made.  */
7451
7452 static tree
7453 fold_builtin_cproj (location_t loc, tree arg, tree type)
7454 {
7455   if (!validate_arg (arg, COMPLEX_TYPE)
7456       || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7457     return NULL_TREE;
7458
7459   /* If there are no infinities, return arg.  */
7460   if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7461     return non_lvalue_loc (loc, arg);
7462
7463   /* Calculate the result when the argument is a constant.  */
7464   if (TREE_CODE (arg) == COMPLEX_CST)
7465     {
7466       const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7467       const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7468       
7469       if (real_isinf (real) || real_isinf (imag))
7470         return build_complex_cproj (type, imag->sign);
7471       else
7472         return arg;
7473     }
7474   else if (TREE_CODE (arg) == COMPLEX_EXPR)
7475     {
7476       tree real = TREE_OPERAND (arg, 0);
7477       tree imag = TREE_OPERAND (arg, 1);
7478
7479       STRIP_NOPS (real);
7480       STRIP_NOPS (imag);
7481       
7482       /* If the real part is inf and the imag part is known to be
7483          nonnegative, return (inf + 0i).  Remember side-effects are
7484          possible in the imag part.  */
7485       if (TREE_CODE (real) == REAL_CST
7486           && real_isinf (TREE_REAL_CST_PTR (real))
7487           && tree_expr_nonnegative_p (imag))
7488         return omit_one_operand_loc (loc, type,
7489                                      build_complex_cproj (type, false),
7490                                      arg);
7491       
7492       /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7493          Remember side-effects are possible in the real part.  */
7494       if (TREE_CODE (imag) == REAL_CST
7495           && real_isinf (TREE_REAL_CST_PTR (imag)))
7496         return
7497           omit_one_operand_loc (loc, type,
7498                                 build_complex_cproj (type, TREE_REAL_CST_PTR
7499                                                      (imag)->sign), arg);
7500     }
7501
7502   return NULL_TREE;
7503 }
7504
7505 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7506    Return NULL_TREE if no simplification can be made.  */
7507
7508 static tree
7509 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7510 {
7511
7512   enum built_in_function fcode;
7513   tree res;
7514
7515   if (!validate_arg (arg, REAL_TYPE))
7516     return NULL_TREE;
7517
7518   /* Calculate the result when the argument is a constant.  */
7519   if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7520     return res;
7521
7522   /* Optimize sqrt(expN(x)) = expN(x*0.5).  */
7523   fcode = builtin_mathfn_code (arg);
7524   if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7525     {
7526       tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7527       arg = fold_build2_loc (loc, MULT_EXPR, type,
7528                          CALL_EXPR_ARG (arg, 0),
7529                          build_real (type, dconsthalf));
7530       return build_call_expr_loc (loc, expfn, 1, arg);
7531     }
7532
7533   /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)).  */
7534   if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7535     {
7536       tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7537
7538       if (powfn)
7539         {
7540           tree arg0 = CALL_EXPR_ARG (arg, 0);
7541           tree tree_root;
7542           /* The inner root was either sqrt or cbrt.  */
7543           /* This was a conditional expression but it triggered a bug
7544              in Sun C 5.5.  */
7545           REAL_VALUE_TYPE dconstroot;
7546           if (BUILTIN_SQRT_P (fcode))
7547             dconstroot = dconsthalf;
7548           else
7549             dconstroot = dconst_third ();
7550
7551           /* Adjust for the outer root.  */
7552           SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7553           dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7554           tree_root = build_real (type, dconstroot);
7555           return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7556         }
7557     }
7558
7559   /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5).  */
7560   if (flag_unsafe_math_optimizations
7561       && (fcode == BUILT_IN_POW
7562           || fcode == BUILT_IN_POWF
7563           || fcode == BUILT_IN_POWL))
7564     {
7565       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7566       tree arg0 = CALL_EXPR_ARG (arg, 0);
7567       tree arg1 = CALL_EXPR_ARG (arg, 1);
7568       tree narg1;
7569       if (!tree_expr_nonnegative_p (arg0))
7570         arg0 = build1 (ABS_EXPR, type, arg0);
7571       narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7572                            build_real (type, dconsthalf));
7573       return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7574     }
7575
7576   return NULL_TREE;
7577 }
7578
7579 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7580    Return NULL_TREE if no simplification can be made.  */
7581
7582 static tree
7583 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7584 {
7585   const enum built_in_function fcode = builtin_mathfn_code (arg);
7586   tree res;
7587
7588   if (!validate_arg (arg, REAL_TYPE))
7589     return NULL_TREE;
7590
7591   /* Calculate the result when the argument is a constant.  */
7592   if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7593     return res;
7594
7595   if (flag_unsafe_math_optimizations)
7596     {
7597       /* Optimize cbrt(expN(x)) -> expN(x/3).  */
7598       if (BUILTIN_EXPONENT_P (fcode))
7599         {
7600           tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7601           const REAL_VALUE_TYPE third_trunc =
7602             real_value_truncate (TYPE_MODE (type), dconst_third ());
7603           arg = fold_build2_loc (loc, MULT_EXPR, type,
7604                              CALL_EXPR_ARG (arg, 0),
7605                              build_real (type, third_trunc));
7606           return build_call_expr_loc (loc, expfn, 1, arg);
7607         }
7608
7609       /* Optimize cbrt(sqrt(x)) -> pow(x,1/6).  */
7610       if (BUILTIN_SQRT_P (fcode))
7611         {
7612           tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7613
7614           if (powfn)
7615             {
7616               tree arg0 = CALL_EXPR_ARG (arg, 0);
7617               tree tree_root;
7618               REAL_VALUE_TYPE dconstroot = dconst_third ();
7619
7620               SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7621               dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7622               tree_root = build_real (type, dconstroot);
7623               return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7624             }
7625         }
7626
7627       /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative.  */
7628       if (BUILTIN_CBRT_P (fcode))
7629         {
7630           tree arg0 = CALL_EXPR_ARG (arg, 0);
7631           if (tree_expr_nonnegative_p (arg0))
7632             {
7633               tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7634
7635               if (powfn)
7636                 {
7637                   tree tree_root;
7638                   REAL_VALUE_TYPE dconstroot;
7639
7640                   real_arithmetic (&dconstroot, MULT_EXPR,
7641                                    dconst_third_ptr (), dconst_third_ptr ());
7642                   dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7643                   tree_root = build_real (type, dconstroot);
7644                   return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7645                 }
7646             }
7647         }
7648
7649       /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative.  */
7650       if (fcode == BUILT_IN_POW
7651           || fcode == BUILT_IN_POWF
7652           || fcode == BUILT_IN_POWL)
7653         {
7654           tree arg00 = CALL_EXPR_ARG (arg, 0);
7655           tree arg01 = CALL_EXPR_ARG (arg, 1);
7656           if (tree_expr_nonnegative_p (arg00))
7657             {
7658               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7659               const REAL_VALUE_TYPE dconstroot
7660                 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7661               tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7662                                          build_real (type, dconstroot));
7663               return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7664             }
7665         }
7666     }
7667   return NULL_TREE;
7668 }
7669
7670 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7671    TYPE is the type of the return value.  Return NULL_TREE if no
7672    simplification can be made.  */
7673
7674 static tree
7675 fold_builtin_cos (location_t loc,
7676                   tree arg, tree type, tree fndecl)
7677 {
7678   tree res, narg;
7679
7680   if (!validate_arg (arg, REAL_TYPE))
7681     return NULL_TREE;
7682
7683   /* Calculate the result when the argument is a constant.  */
7684   if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7685     return res;
7686
7687   /* Optimize cos(-x) into cos (x).  */
7688   if ((narg = fold_strip_sign_ops (arg)))
7689     return build_call_expr_loc (loc, fndecl, 1, narg);
7690
7691   return NULL_TREE;
7692 }
7693
7694 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7695    Return NULL_TREE if no simplification can be made.  */
7696
7697 static tree
7698 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7699 {
7700   if (validate_arg (arg, REAL_TYPE))
7701     {
7702       tree res, narg;
7703
7704       /* Calculate the result when the argument is a constant.  */
7705       if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7706         return res;
7707
7708       /* Optimize cosh(-x) into cosh (x).  */
7709       if ((narg = fold_strip_sign_ops (arg)))
7710         return build_call_expr_loc (loc, fndecl, 1, narg);
7711     }
7712
7713   return NULL_TREE;
7714 }
7715
7716 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7717    argument ARG.  TYPE is the type of the return value.  Return
7718    NULL_TREE if no simplification can be made.  */
7719
7720 static tree
7721 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7722                    bool hyper)
7723 {
7724   if (validate_arg (arg, COMPLEX_TYPE)
7725       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7726     {
7727       tree tmp;
7728
7729       /* Calculate the result when the argument is a constant.  */
7730       if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7731         return tmp;
7732
7733       /* Optimize fn(-x) into fn(x).  */
7734       if ((tmp = fold_strip_sign_ops (arg)))
7735         return build_call_expr_loc (loc, fndecl, 1, tmp);
7736     }
7737
7738   return NULL_TREE;
7739 }
7740
7741 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7742    Return NULL_TREE if no simplification can be made.  */
7743
7744 static tree
7745 fold_builtin_tan (tree arg, tree type)
7746 {
7747   enum built_in_function fcode;
7748   tree res;
7749
7750   if (!validate_arg (arg, REAL_TYPE))
7751     return NULL_TREE;
7752
7753   /* Calculate the result when the argument is a constant.  */
7754   if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7755     return res;
7756
7757   /* Optimize tan(atan(x)) = x.  */
7758   fcode = builtin_mathfn_code (arg);
7759   if (flag_unsafe_math_optimizations
7760       && (fcode == BUILT_IN_ATAN
7761           || fcode == BUILT_IN_ATANF
7762           || fcode == BUILT_IN_ATANL))
7763     return CALL_EXPR_ARG (arg, 0);
7764
7765   return NULL_TREE;
7766 }
7767
7768 /* Fold function call to builtin sincos, sincosf, or sincosl.  Return
7769    NULL_TREE if no simplification can be made.  */
7770
7771 static tree
7772 fold_builtin_sincos (location_t loc,
7773                      tree arg0, tree arg1, tree arg2)
7774 {
7775   tree type;
7776   tree res, fn, call;
7777
7778   if (!validate_arg (arg0, REAL_TYPE)
7779       || !validate_arg (arg1, POINTER_TYPE)
7780       || !validate_arg (arg2, POINTER_TYPE))
7781     return NULL_TREE;
7782
7783   type = TREE_TYPE (arg0);
7784
7785   /* Calculate the result when the argument is a constant.  */
7786   if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7787     return res;
7788
7789   /* Canonicalize sincos to cexpi.  */
7790   if (!TARGET_C99_FUNCTIONS)
7791     return NULL_TREE;
7792   fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7793   if (!fn)
7794     return NULL_TREE;
7795
7796   call = build_call_expr_loc (loc, fn, 1, arg0);
7797   call = builtin_save_expr (call);
7798
7799   return build2 (COMPOUND_EXPR, void_type_node,
7800                  build2 (MODIFY_EXPR, void_type_node,
7801                          build_fold_indirect_ref_loc (loc, arg1),
7802                          build1 (IMAGPART_EXPR, type, call)),
7803                  build2 (MODIFY_EXPR, void_type_node,
7804                          build_fold_indirect_ref_loc (loc, arg2),
7805                          build1 (REALPART_EXPR, type, call)));
7806 }
7807
7808 /* Fold function call to builtin cexp, cexpf, or cexpl.  Return
7809    NULL_TREE if no simplification can be made.  */
7810
7811 static tree
7812 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7813 {
7814   tree rtype;
7815   tree realp, imagp, ifn;
7816   tree res;
7817
7818   if (!validate_arg (arg0, COMPLEX_TYPE)
7819       || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7820     return NULL_TREE;
7821
7822   /* Calculate the result when the argument is a constant.  */
7823   if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7824     return res;
7825
7826   rtype = TREE_TYPE (TREE_TYPE (arg0));
7827
7828   /* In case we can figure out the real part of arg0 and it is constant zero
7829      fold to cexpi.  */
7830   if (!TARGET_C99_FUNCTIONS)
7831     return NULL_TREE;
7832   ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7833   if (!ifn)
7834     return NULL_TREE;
7835
7836   if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7837       && real_zerop (realp))
7838     {
7839       tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7840       return build_call_expr_loc (loc, ifn, 1, narg);
7841     }
7842
7843   /* In case we can easily decompose real and imaginary parts split cexp
7844      to exp (r) * cexpi (i).  */
7845   if (flag_unsafe_math_optimizations
7846       && realp)
7847     {
7848       tree rfn, rcall, icall;
7849
7850       rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7851       if (!rfn)
7852         return NULL_TREE;
7853
7854       imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7855       if (!imagp)
7856         return NULL_TREE;
7857
7858       icall = build_call_expr_loc (loc, ifn, 1, imagp);
7859       icall = builtin_save_expr (icall);
7860       rcall = build_call_expr_loc (loc, rfn, 1, realp);
7861       rcall = builtin_save_expr (rcall);
7862       return fold_build2_loc (loc, COMPLEX_EXPR, type,
7863                           fold_build2_loc (loc, MULT_EXPR, rtype,
7864                                        rcall,
7865                                        fold_build1_loc (loc, REALPART_EXPR,
7866                                                     rtype, icall)),
7867                           fold_build2_loc (loc, MULT_EXPR, rtype,
7868                                        rcall,
7869                                        fold_build1_loc (loc, IMAGPART_EXPR,
7870                                                     rtype, icall)));
7871     }
7872
7873   return NULL_TREE;
7874 }
7875
7876 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7877    Return NULL_TREE if no simplification can be made.  */
7878
7879 static tree
7880 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7881 {
7882   if (!validate_arg (arg, REAL_TYPE))
7883     return NULL_TREE;
7884
7885   /* Optimize trunc of constant value.  */
7886   if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7887     {
7888       REAL_VALUE_TYPE r, x;
7889       tree type = TREE_TYPE (TREE_TYPE (fndecl));
7890
7891       x = TREE_REAL_CST (arg);
7892       real_trunc (&r, TYPE_MODE (type), &x);
7893       return build_real (type, r);
7894     }
7895
7896   return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7897 }
7898
7899 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7900    Return NULL_TREE if no simplification can be made.  */
7901
7902 static tree
7903 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7904 {
7905   if (!validate_arg (arg, REAL_TYPE))
7906     return NULL_TREE;
7907
7908   /* Optimize floor of constant value.  */
7909   if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7910     {
7911       REAL_VALUE_TYPE x;
7912
7913       x = TREE_REAL_CST (arg);
7914       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7915         {
7916           tree type = TREE_TYPE (TREE_TYPE (fndecl));
7917           REAL_VALUE_TYPE r;
7918
7919           real_floor (&r, TYPE_MODE (type), &x);
7920           return build_real (type, r);
7921         }
7922     }
7923
7924   /* Fold floor (x) where x is nonnegative to trunc (x).  */
7925   if (tree_expr_nonnegative_p (arg))
7926     {
7927       tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7928       if (truncfn)
7929         return build_call_expr_loc (loc, truncfn, 1, arg);
7930     }
7931
7932   return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7933 }
7934
7935 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7936    Return NULL_TREE if no simplification can be made.  */
7937
7938 static tree
7939 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7940 {
7941   if (!validate_arg (arg, REAL_TYPE))
7942     return NULL_TREE;
7943
7944   /* Optimize ceil of constant value.  */
7945   if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7946     {
7947       REAL_VALUE_TYPE x;
7948
7949       x = TREE_REAL_CST (arg);
7950       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7951         {
7952           tree type = TREE_TYPE (TREE_TYPE (fndecl));
7953           REAL_VALUE_TYPE r;
7954
7955           real_ceil (&r, TYPE_MODE (type), &x);
7956           return build_real (type, r);
7957         }
7958     }
7959
7960   return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7961 }
7962
7963 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7964    Return NULL_TREE if no simplification can be made.  */
7965
7966 static tree
7967 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7968 {
7969   if (!validate_arg (arg, REAL_TYPE))
7970     return NULL_TREE;
7971
7972   /* Optimize round of constant value.  */
7973   if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7974     {
7975       REAL_VALUE_TYPE x;
7976
7977       x = TREE_REAL_CST (arg);
7978       if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7979         {
7980           tree type = TREE_TYPE (TREE_TYPE (fndecl));
7981           REAL_VALUE_TYPE r;
7982
7983           real_round (&r, TYPE_MODE (type), &x);
7984           return build_real (type, r);
7985         }
7986     }
7987
7988   return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7989 }
7990
7991 /* Fold function call to builtin lround, lroundf or lroundl (or the
7992    corresponding long long versions) and other rounding functions.  ARG
7993    is the argument to the call.  Return NULL_TREE if no simplification
7994    can be made.  */
7995
7996 static tree
7997 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7998 {
7999   if (!validate_arg (arg, REAL_TYPE))
8000     return NULL_TREE;
8001
8002   /* Optimize lround of constant value.  */
8003   if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8004     {
8005       const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8006
8007       if (real_isfinite (&x))
8008         {
8009           tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8010           tree ftype = TREE_TYPE (arg);
8011           double_int val;
8012           REAL_VALUE_TYPE r;
8013
8014           switch (DECL_FUNCTION_CODE (fndecl))
8015             {
8016             CASE_FLT_FN (BUILT_IN_IFLOOR):
8017             CASE_FLT_FN (BUILT_IN_LFLOOR):
8018             CASE_FLT_FN (BUILT_IN_LLFLOOR):
8019               real_floor (&r, TYPE_MODE (ftype), &x);
8020               break;
8021
8022             CASE_FLT_FN (BUILT_IN_ICEIL):
8023             CASE_FLT_FN (BUILT_IN_LCEIL):
8024             CASE_FLT_FN (BUILT_IN_LLCEIL):
8025               real_ceil (&r, TYPE_MODE (ftype), &x);
8026               break;
8027
8028             CASE_FLT_FN (BUILT_IN_IROUND):
8029             CASE_FLT_FN (BUILT_IN_LROUND):
8030             CASE_FLT_FN (BUILT_IN_LLROUND):
8031               real_round (&r, TYPE_MODE (ftype), &x);
8032               break;
8033
8034             default:
8035               gcc_unreachable ();
8036             }
8037
8038           real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8039           if (double_int_fits_to_tree_p (itype, val))
8040             return double_int_to_tree (itype, val);
8041         }
8042     }
8043
8044   switch (DECL_FUNCTION_CODE (fndecl))
8045     {
8046     CASE_FLT_FN (BUILT_IN_LFLOOR):
8047     CASE_FLT_FN (BUILT_IN_LLFLOOR):
8048       /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x).  */
8049       if (tree_expr_nonnegative_p (arg))
8050         return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8051                             TREE_TYPE (TREE_TYPE (fndecl)), arg);
8052       break;
8053     default:;
8054     }
8055
8056   return fold_fixed_mathfn (loc, fndecl, arg);
8057 }
8058
8059 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8060    and their long and long long variants (i.e. ffsl and ffsll).  ARG is
8061    the argument to the call.  Return NULL_TREE if no simplification can
8062    be made.  */
8063
8064 static tree
8065 fold_builtin_bitop (tree fndecl, tree arg)
8066 {
8067   if (!validate_arg (arg, INTEGER_TYPE))
8068     return NULL_TREE;
8069
8070   /* Optimize for constant argument.  */
8071   if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8072     {
8073       HOST_WIDE_INT hi, width, result;
8074       unsigned HOST_WIDE_INT lo;
8075       tree type;
8076
8077       type = TREE_TYPE (arg);
8078       width = TYPE_PRECISION (type);
8079       lo = TREE_INT_CST_LOW (arg);
8080
8081       /* Clear all the bits that are beyond the type's precision.  */
8082       if (width > HOST_BITS_PER_WIDE_INT)
8083         {
8084           hi = TREE_INT_CST_HIGH (arg);
8085           if (width < 2 * HOST_BITS_PER_WIDE_INT)
8086             hi &= ~((unsigned HOST_WIDE_INT) (-1)
8087                     << (width - HOST_BITS_PER_WIDE_INT));
8088         }
8089       else
8090         {
8091           hi = 0;
8092           if (width < HOST_BITS_PER_WIDE_INT)
8093             lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8094         }
8095
8096       switch (DECL_FUNCTION_CODE (fndecl))
8097         {
8098         CASE_INT_FN (BUILT_IN_FFS):
8099           if (lo != 0)
8100             result = ffs_hwi (lo);
8101           else if (hi != 0)
8102             result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8103           else
8104             result = 0;
8105           break;
8106
8107         CASE_INT_FN (BUILT_IN_CLZ):
8108           if (hi != 0)
8109             result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8110           else if (lo != 0)
8111             result = width - floor_log2 (lo) - 1;
8112           else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8113             result = width;
8114           break;
8115
8116         CASE_INT_FN (BUILT_IN_CTZ):
8117           if (lo != 0)
8118             result = ctz_hwi (lo);
8119           else if (hi != 0)
8120             result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8121           else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8122             result = width;
8123           break;
8124
8125         CASE_INT_FN (BUILT_IN_CLRSB):
8126           if (width > HOST_BITS_PER_WIDE_INT
8127               && (hi & ((unsigned HOST_WIDE_INT) 1
8128                         << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8129             {
8130               hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
8131                            << (width - HOST_BITS_PER_WIDE_INT - 1));
8132               lo = ~lo;
8133             }
8134           else if (width <= HOST_BITS_PER_WIDE_INT
8135                    && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8136             lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
8137           if (hi != 0)
8138             result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8139           else if (lo != 0)
8140             result = width - floor_log2 (lo) - 2;
8141           else
8142             result = width - 1;
8143           break;
8144
8145         CASE_INT_FN (BUILT_IN_POPCOUNT):
8146           result = 0;
8147           while (lo)
8148             result++, lo &= lo - 1;
8149           while (hi)
8150             result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8151           break;
8152
8153         CASE_INT_FN (BUILT_IN_PARITY):
8154           result = 0;
8155           while (lo)
8156             result++, lo &= lo - 1;
8157           while (hi)
8158             result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8159           result &= 1;
8160           break;
8161
8162         default:
8163           gcc_unreachable ();
8164         }
8165
8166       return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8167     }
8168
8169   return NULL_TREE;
8170 }
8171
8172 /* Fold function call to builtin_bswap and the long and long long
8173    variants.  Return NULL_TREE if no simplification can be made.  */
8174 static tree
8175 fold_builtin_bswap (tree fndecl, tree arg)
8176 {
8177   if (! validate_arg (arg, INTEGER_TYPE))
8178     return NULL_TREE;
8179
8180   /* Optimize constant value.  */
8181   if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8182     {
8183       HOST_WIDE_INT hi, width, r_hi = 0;
8184       unsigned HOST_WIDE_INT lo, r_lo = 0;
8185       tree type;
8186
8187       type = TREE_TYPE (arg);
8188       width = TYPE_PRECISION (type);
8189       lo = TREE_INT_CST_LOW (arg);
8190       hi = TREE_INT_CST_HIGH (arg);
8191
8192       switch (DECL_FUNCTION_CODE (fndecl))
8193         {
8194           case BUILT_IN_BSWAP32:
8195           case BUILT_IN_BSWAP64:
8196             {
8197               int s;
8198
8199               for (s = 0; s < width; s += 8)
8200                 {
8201                   int d = width - s - 8;
8202                   unsigned HOST_WIDE_INT byte;
8203
8204                   if (s < HOST_BITS_PER_WIDE_INT)
8205                     byte = (lo >> s) & 0xff;
8206                   else
8207                     byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8208
8209                   if (d < HOST_BITS_PER_WIDE_INT)
8210                     r_lo |= byte << d;
8211                   else
8212                     r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8213                 }
8214             }
8215
8216             break;
8217
8218         default:
8219           gcc_unreachable ();
8220         }
8221
8222       if (width < HOST_BITS_PER_WIDE_INT)
8223         return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
8224       else
8225         return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
8226     }
8227
8228   return NULL_TREE;
8229 }
8230
8231 /* A subroutine of fold_builtin to fold the various logarithmic
8232    functions.  Return NULL_TREE if no simplification can me made.
8233    FUNC is the corresponding MPFR logarithm function.  */
8234
8235 static tree
8236 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8237                         int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8238 {
8239   if (validate_arg (arg, REAL_TYPE))
8240     {
8241       tree type = TREE_TYPE (TREE_TYPE (fndecl));
8242       tree res;
8243       const enum built_in_function fcode = builtin_mathfn_code (arg);
8244
8245       /* Calculate the result when the argument is a constant.  */
8246       if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8247         return res;
8248
8249       /* Special case, optimize logN(expN(x)) = x.  */
8250       if (flag_unsafe_math_optimizations
8251           && ((func == mpfr_log
8252                && (fcode == BUILT_IN_EXP
8253                    || fcode == BUILT_IN_EXPF
8254                    || fcode == BUILT_IN_EXPL))
8255               || (func == mpfr_log2
8256                   && (fcode == BUILT_IN_EXP2
8257                       || fcode == BUILT_IN_EXP2F
8258                       || fcode == BUILT_IN_EXP2L))
8259               || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8260         return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8261
8262       /* Optimize logN(func()) for various exponential functions.  We
8263          want to determine the value "x" and the power "exponent" in
8264          order to transform logN(x**exponent) into exponent*logN(x).  */
8265       if (flag_unsafe_math_optimizations)
8266         {
8267           tree exponent = 0, x = 0;
8268
8269           switch (fcode)
8270           {
8271           CASE_FLT_FN (BUILT_IN_EXP):
8272             /* Prepare to do logN(exp(exponent) -> exponent*logN(e).  */
8273             x = build_real (type, real_value_truncate (TYPE_MODE (type),
8274                                                        dconst_e ()));
8275             exponent = CALL_EXPR_ARG (arg, 0);
8276             break;
8277           CASE_FLT_FN (BUILT_IN_EXP2):
8278             /* Prepare to do logN(exp2(exponent) -> exponent*logN(2).  */
8279             x = build_real (type, dconst2);
8280             exponent = CALL_EXPR_ARG (arg, 0);
8281             break;
8282           CASE_FLT_FN (BUILT_IN_EXP10):
8283           CASE_FLT_FN (BUILT_IN_POW10):
8284             /* Prepare to do logN(exp10(exponent) -> exponent*logN(10).  */
8285             {
8286               REAL_VALUE_TYPE dconst10;
8287               real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8288               x = build_real (type, dconst10);
8289             }
8290             exponent = CALL_EXPR_ARG (arg, 0);
8291             break;
8292           CASE_FLT_FN (BUILT_IN_SQRT):
8293             /* Prepare to do logN(sqrt(x) -> 0.5*logN(x).  */
8294             x = CALL_EXPR_ARG (arg, 0);
8295             exponent = build_real (type, dconsthalf);
8296             break;
8297           CASE_FLT_FN (BUILT_IN_CBRT):
8298             /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x).  */
8299             x = CALL_EXPR_ARG (arg, 0);
8300             exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8301                                                               dconst_third ()));
8302             break;
8303           CASE_FLT_FN (BUILT_IN_POW):
8304             /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x).  */
8305             x = CALL_EXPR_ARG (arg, 0);
8306             exponent = CALL_EXPR_ARG (arg, 1);
8307             break;
8308           default:
8309             break;
8310           }
8311
8312           /* Now perform the optimization.  */
8313           if (x && exponent)
8314             {
8315               tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8316               return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8317             }
8318         }
8319     }
8320
8321   return NULL_TREE;
8322 }
8323
8324 /* Fold a builtin function call to hypot, hypotf, or hypotl.  Return
8325    NULL_TREE if no simplification can be made.  */
8326
8327 static tree
8328 fold_builtin_hypot (location_t loc, tree fndecl,
8329                     tree arg0, tree arg1, tree type)
8330 {
8331   tree res, narg0, narg1;
8332
8333   if (!validate_arg (arg0, REAL_TYPE)
8334       || !validate_arg (arg1, REAL_TYPE))
8335     return NULL_TREE;
8336
8337   /* Calculate the result when the argument is a constant.  */
8338   if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8339     return res;
8340
8341   /* If either argument to hypot has a negate or abs, strip that off.
8342      E.g. hypot(-x,fabs(y)) -> hypot(x,y).  */
8343   narg0 = fold_strip_sign_ops (arg0);
8344   narg1 = fold_strip_sign_ops (arg1);
8345   if (narg0 || narg1)
8346     {
8347       return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8348                               narg1 ? narg1 : arg1);
8349     }
8350
8351   /* If either argument is zero, hypot is fabs of the other.  */
8352   if (real_zerop (arg0))
8353     return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8354   else if (real_zerop (arg1))
8355     return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8356
8357   /* hypot(x,x) -> fabs(x)*sqrt(2).  */
8358   if (flag_unsafe_math_optimizations
8359       && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8360     {
8361       const REAL_VALUE_TYPE sqrt2_trunc
8362         = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8363       return fold_build2_loc (loc, MULT_EXPR, type,
8364                           fold_build1_loc (loc, ABS_EXPR, type, arg0),
8365                           build_real (type, sqrt2_trunc));
8366     }
8367
8368   return NULL_TREE;
8369 }
8370
8371
8372 /* Fold a builtin function call to pow, powf, or powl.  Return
8373    NULL_TREE if no simplification can be made.  */
8374 static tree
8375 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8376 {
8377   tree res;
8378
8379   if (!validate_arg (arg0, REAL_TYPE)
8380        || !validate_arg (arg1, REAL_TYPE))
8381     return NULL_TREE;
8382
8383   /* Calculate the result when the argument is a constant.  */
8384   if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8385     return res;
8386
8387   /* Optimize pow(1.0,y) = 1.0.  */
8388   if (real_onep (arg0))
8389     return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8390
8391   if (TREE_CODE (arg1) == REAL_CST
8392       && !TREE_OVERFLOW (arg1))
8393     {
8394       REAL_VALUE_TYPE cint;
8395       REAL_VALUE_TYPE c;
8396       HOST_WIDE_INT n;
8397
8398       c = TREE_REAL_CST (arg1);
8399
8400       /* Optimize pow(x,0.0) = 1.0.  */
8401       if (REAL_VALUES_EQUAL (c, dconst0))
8402         return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8403                                  arg0);
8404
8405       /* Optimize pow(x,1.0) = x.  */
8406       if (REAL_VALUES_EQUAL (c, dconst1))
8407         return arg0;
8408
8409       /* Optimize pow(x,-1.0) = 1.0/x.  */
8410       if (REAL_VALUES_EQUAL (c, dconstm1))
8411         return fold_build2_loc (loc, RDIV_EXPR, type,
8412                             build_real (type, dconst1), arg0);
8413
8414       /* Optimize pow(x,0.5) = sqrt(x).  */
8415       if (flag_unsafe_math_optimizations
8416           && REAL_VALUES_EQUAL (c, dconsthalf))
8417         {
8418           tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8419
8420           if (sqrtfn != NULL_TREE)
8421             return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8422         }
8423
8424       /* Optimize pow(x,1.0/3.0) = cbrt(x).  */
8425       if (flag_unsafe_math_optimizations)
8426         {
8427           const REAL_VALUE_TYPE dconstroot
8428             = real_value_truncate (TYPE_MODE (type), dconst_third ());
8429
8430           if (REAL_VALUES_EQUAL (c, dconstroot))
8431             {
8432               tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8433               if (cbrtfn != NULL_TREE)
8434                 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8435             }
8436         }
8437
8438       /* Check for an integer exponent.  */
8439       n = real_to_integer (&c);
8440       real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8441       if (real_identical (&c, &cint))
8442         {
8443           /* Attempt to evaluate pow at compile-time, unless this should
8444              raise an exception.  */
8445           if (TREE_CODE (arg0) == REAL_CST
8446               && !TREE_OVERFLOW (arg0)
8447               && (n > 0
8448                   || (!flag_trapping_math && !flag_errno_math)
8449                   || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8450             {
8451               REAL_VALUE_TYPE x;
8452               bool inexact;
8453
8454               x = TREE_REAL_CST (arg0);
8455               inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8456               if (flag_unsafe_math_optimizations || !inexact)
8457                 return build_real (type, x);
8458             }
8459
8460           /* Strip sign ops from even integer powers.  */
8461           if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8462             {
8463               tree narg0 = fold_strip_sign_ops (arg0);
8464               if (narg0)
8465                 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8466             }
8467         }
8468     }
8469
8470   if (flag_unsafe_math_optimizations)
8471     {
8472       const enum built_in_function fcode = builtin_mathfn_code (arg0);
8473
8474       /* Optimize pow(expN(x),y) = expN(x*y).  */
8475       if (BUILTIN_EXPONENT_P (fcode))
8476         {
8477           tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8478           tree arg = CALL_EXPR_ARG (arg0, 0);
8479           arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8480           return build_call_expr_loc (loc, expfn, 1, arg);
8481         }
8482
8483       /* Optimize pow(sqrt(x),y) = pow(x,y*0.5).  */
8484       if (BUILTIN_SQRT_P (fcode))
8485         {
8486           tree narg0 = CALL_EXPR_ARG (arg0, 0);
8487           tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8488                                     build_real (type, dconsthalf));
8489           return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8490         }
8491
8492       /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative.  */
8493       if (BUILTIN_CBRT_P (fcode))
8494         {
8495           tree arg = CALL_EXPR_ARG (arg0, 0);
8496           if (tree_expr_nonnegative_p (arg))
8497             {
8498               const REAL_VALUE_TYPE dconstroot
8499                 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8500               tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8501                                         build_real (type, dconstroot));
8502               return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8503             }
8504         }
8505
8506       /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative.  */
8507       if (fcode == BUILT_IN_POW
8508           || fcode == BUILT_IN_POWF
8509           || fcode == BUILT_IN_POWL)
8510         {
8511           tree arg00 = CALL_EXPR_ARG (arg0, 0);
8512           if (tree_expr_nonnegative_p (arg00))
8513             {
8514               tree arg01 = CALL_EXPR_ARG (arg0, 1);
8515               tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8516               return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8517             }
8518         }
8519     }
8520
8521   return NULL_TREE;
8522 }
8523
8524 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8525    Return NULL_TREE if no simplification can be made.  */
8526 static tree
8527 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8528                    tree arg0, tree arg1, tree type)
8529 {
8530   if (!validate_arg (arg0, REAL_TYPE)
8531       || !validate_arg (arg1, INTEGER_TYPE))
8532     return NULL_TREE;
8533
8534   /* Optimize pow(1.0,y) = 1.0.  */
8535   if (real_onep (arg0))
8536     return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8537
8538   if (host_integerp (arg1, 0))
8539     {
8540       HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8541
8542       /* Evaluate powi at compile-time.  */
8543       if (TREE_CODE (arg0) == REAL_CST
8544           && !TREE_OVERFLOW (arg0))
8545         {
8546           REAL_VALUE_TYPE x;
8547           x = TREE_REAL_CST (arg0);
8548           real_powi (&x, TYPE_MODE (type), &x, c);
8549           return build_real (type, x);
8550         }
8551
8552       /* Optimize pow(x,0) = 1.0.  */
8553       if (c == 0)
8554         return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8555                                  arg0);
8556
8557       /* Optimize pow(x,1) = x.  */
8558       if (c == 1)
8559         return arg0;
8560
8561       /* Optimize pow(x,-1) = 1.0/x.  */
8562       if (c == -1)
8563         return fold_build2_loc (loc, RDIV_EXPR, type,
8564                            build_real (type, dconst1), arg0);
8565     }
8566
8567   return NULL_TREE;
8568 }
8569
8570 /* A subroutine of fold_builtin to fold the various exponent
8571    functions.  Return NULL_TREE if no simplification can be made.
8572    FUNC is the corresponding MPFR exponent function.  */
8573
8574 static tree
8575 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8576                        int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8577 {
8578   if (validate_arg (arg, REAL_TYPE))
8579     {
8580       tree type = TREE_TYPE (TREE_TYPE (fndecl));
8581       tree res;
8582
8583       /* Calculate the result when the argument is a constant.  */
8584       if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8585         return res;
8586
8587       /* Optimize expN(logN(x)) = x.  */
8588       if (flag_unsafe_math_optimizations)
8589         {
8590           const enum built_in_function fcode = builtin_mathfn_code (arg);
8591
8592           if ((func == mpfr_exp
8593                && (fcode == BUILT_IN_LOG
8594                    || fcode == BUILT_IN_LOGF
8595                    || fcode == BUILT_IN_LOGL))
8596               || (func == mpfr_exp2
8597                   && (fcode == BUILT_IN_LOG2
8598                       || fcode == BUILT_IN_LOG2F
8599                       || fcode == BUILT_IN_LOG2L))
8600               || (func == mpfr_exp10
8601                   && (fcode == BUILT_IN_LOG10
8602                       || fcode == BUILT_IN_LOG10F
8603                       || fcode == BUILT_IN_LOG10L)))
8604             return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8605         }
8606     }
8607
8608   return NULL_TREE;
8609 }
8610
8611 /* Return true if VAR is a VAR_DECL or a component thereof.  */
8612
8613 static bool
8614 var_decl_component_p (tree var)
8615 {
8616   tree inner = var;
8617   while (handled_component_p (inner))
8618     inner = TREE_OPERAND (inner, 0);
8619   return SSA_VAR_P (inner);
8620 }
8621
8622 /* Fold function call to builtin memset.  Return
8623    NULL_TREE if no simplification can be made.  */
8624
8625 static tree
8626 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8627                      tree type, bool ignore)
8628 {
8629   tree var, ret, etype;
8630   unsigned HOST_WIDE_INT length, cval;
8631
8632   if (! validate_arg (dest, POINTER_TYPE)
8633       || ! validate_arg (c, INTEGER_TYPE)
8634       || ! validate_arg (len, INTEGER_TYPE))
8635     return NULL_TREE;
8636
8637   if (! host_integerp (len, 1))
8638     return NULL_TREE;
8639
8640   /* If the LEN parameter is zero, return DEST.  */
8641   if (integer_zerop (len))
8642     return omit_one_operand_loc (loc, type, dest, c);
8643
8644   if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8645     return NULL_TREE;
8646
8647   var = dest;
8648   STRIP_NOPS (var);
8649   if (TREE_CODE (var) != ADDR_EXPR)
8650     return NULL_TREE;
8651
8652   var = TREE_OPERAND (var, 0);
8653   if (TREE_THIS_VOLATILE (var))
8654     return NULL_TREE;
8655
8656   etype = TREE_TYPE (var);
8657   if (TREE_CODE (etype) == ARRAY_TYPE)
8658     etype = TREE_TYPE (etype);
8659
8660   if (!INTEGRAL_TYPE_P (etype)
8661       && !POINTER_TYPE_P (etype))
8662     return NULL_TREE;
8663
8664   if (! var_decl_component_p (var))
8665     return NULL_TREE;
8666
8667   length = tree_low_cst (len, 1);
8668   if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8669       || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8670     return NULL_TREE;
8671
8672   if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8673     return NULL_TREE;
8674
8675   if (integer_zerop (c))
8676     cval = 0;
8677   else
8678     {
8679       if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8680         return NULL_TREE;
8681
8682       cval = TREE_INT_CST_LOW (c);
8683       cval &= 0xff;
8684       cval |= cval << 8;
8685       cval |= cval << 16;
8686       cval |= (cval << 31) << 1;
8687     }
8688
8689   ret = build_int_cst_type (etype, cval);
8690   var = build_fold_indirect_ref_loc (loc,
8691                                  fold_convert_loc (loc,
8692                                                    build_pointer_type (etype),
8693                                                    dest));
8694   ret = build2 (MODIFY_EXPR, etype, var, ret);
8695   if (ignore)
8696     return ret;
8697
8698   return omit_one_operand_loc (loc, type, dest, ret);
8699 }
8700
8701 /* Fold function call to builtin memset.  Return
8702    NULL_TREE if no simplification can be made.  */
8703
8704 static tree
8705 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8706 {
8707   if (! validate_arg (dest, POINTER_TYPE)
8708       || ! validate_arg (size, INTEGER_TYPE))
8709     return NULL_TREE;
8710
8711   if (!ignore)
8712     return NULL_TREE;
8713
8714   /* New argument list transforming bzero(ptr x, int y) to
8715      memset(ptr x, int 0, size_t y).   This is done this way
8716      so that if it isn't expanded inline, we fallback to
8717      calling bzero instead of memset.  */
8718
8719   return fold_builtin_memset (loc, dest, integer_zero_node,
8720                               fold_convert_loc (loc, size_type_node, size),
8721                               void_type_node, ignore);
8722 }
8723
8724 /* Fold function call to builtin mem{{,p}cpy,move}.  Return
8725    NULL_TREE if no simplification can be made.
8726    If ENDP is 0, return DEST (like memcpy).
8727    If ENDP is 1, return DEST+LEN (like mempcpy).
8728    If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8729    If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8730    (memmove).   */
8731
8732 static tree
8733 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8734                         tree len, tree type, bool ignore, int endp)
8735 {
8736   tree destvar, srcvar, expr;
8737
8738   if (! validate_arg (dest, POINTER_TYPE)
8739       || ! validate_arg (src, POINTER_TYPE)
8740       || ! validate_arg (len, INTEGER_TYPE))
8741     return NULL_TREE;
8742
8743   /* If the LEN parameter is zero, return DEST.  */
8744   if (integer_zerop (len))
8745     return omit_one_operand_loc (loc, type, dest, src);
8746
8747   /* If SRC and DEST are the same (and not volatile), return
8748      DEST{,+LEN,+LEN-1}.  */
8749   if (operand_equal_p (src, dest, 0))
8750     expr = len;
8751   else
8752     {
8753       tree srctype, desttype;
8754       unsigned int src_align, dest_align;
8755       tree off0;
8756
8757       if (endp == 3)
8758         {
8759           src_align = get_pointer_alignment (src);
8760           dest_align = get_pointer_alignment (dest);
8761
8762           /* Both DEST and SRC must be pointer types.
8763              ??? This is what old code did.  Is the testing for pointer types
8764              really mandatory?
8765
8766              If either SRC is readonly or length is 1, we can use memcpy.  */
8767           if (!dest_align || !src_align)
8768             return NULL_TREE;
8769           if (readonly_data_expr (src)
8770               || (host_integerp (len, 1)
8771                   && (MIN (src_align, dest_align) / BITS_PER_UNIT
8772                       >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8773             {
8774               tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8775               if (!fn)
8776                 return NULL_TREE;
8777               return build_call_expr_loc (loc, fn, 3, dest, src, len);
8778             }
8779
8780           /* If *src and *dest can't overlap, optimize into memcpy as well.  */
8781           if (TREE_CODE (src) == ADDR_EXPR
8782               && TREE_CODE (dest) == ADDR_EXPR)
8783             {
8784               tree src_base, dest_base, fn;
8785               HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8786               HOST_WIDE_INT size = -1;
8787               HOST_WIDE_INT maxsize = -1;
8788
8789               srcvar = TREE_OPERAND (src, 0);
8790               src_base = get_ref_base_and_extent (srcvar, &src_offset,
8791                                                   &size, &maxsize);
8792               destvar = TREE_OPERAND (dest, 0);
8793               dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8794                                                    &size, &maxsize);
8795               if (host_integerp (len, 1))
8796                 maxsize = tree_low_cst (len, 1);
8797               else
8798                 maxsize = -1;
8799               src_offset /= BITS_PER_UNIT;
8800               dest_offset /= BITS_PER_UNIT;
8801               if (SSA_VAR_P (src_base)
8802                   && SSA_VAR_P (dest_base))
8803                 {
8804                   if (operand_equal_p (src_base, dest_base, 0)
8805                       && ranges_overlap_p (src_offset, maxsize,
8806                                            dest_offset, maxsize))
8807                     return NULL_TREE;
8808                 }
8809               else if (TREE_CODE (src_base) == MEM_REF
8810                        && TREE_CODE (dest_base) == MEM_REF)
8811                 {
8812                   double_int off;
8813                   if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8814                                          TREE_OPERAND (dest_base, 0), 0))
8815                     return NULL_TREE;
8816                   off = double_int_add (mem_ref_offset (src_base),
8817                                         shwi_to_double_int (src_offset));
8818                   if (!double_int_fits_in_shwi_p (off))
8819                     return NULL_TREE;
8820                   src_offset = off.low;
8821                   off = double_int_add (mem_ref_offset (dest_base),
8822                                         shwi_to_double_int (dest_offset));
8823                   if (!double_int_fits_in_shwi_p (off))
8824                     return NULL_TREE;
8825                   dest_offset = off.low;
8826                   if (ranges_overlap_p (src_offset, maxsize,
8827                                         dest_offset, maxsize))
8828                     return NULL_TREE;
8829                 }
8830               else
8831                 return NULL_TREE;
8832
8833               fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8834               if (!fn)
8835                 return NULL_TREE;
8836               return build_call_expr_loc (loc, fn, 3, dest, src, len);
8837             }
8838
8839           /* If the destination and source do not alias optimize into
8840              memcpy as well.  */
8841           if ((is_gimple_min_invariant (dest)
8842                || TREE_CODE (dest) == SSA_NAME)
8843               && (is_gimple_min_invariant (src)
8844                   || TREE_CODE (src) == SSA_NAME))
8845             {
8846               ao_ref destr, srcr;
8847               ao_ref_init_from_ptr_and_size (&destr, dest, len);
8848               ao_ref_init_from_ptr_and_size (&srcr, src, len);
8849               if (!refs_may_alias_p_1 (&destr, &srcr, false))
8850                 {
8851                   tree fn;
8852                   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8853                   if (!fn)
8854                     return NULL_TREE;
8855                   return build_call_expr_loc (loc, fn, 3, dest, src, len);
8856                 }
8857             }
8858
8859           return NULL_TREE;
8860         }
8861
8862       if (!host_integerp (len, 0))
8863         return NULL_TREE;
8864       /* FIXME:
8865          This logic lose for arguments like (type *)malloc (sizeof (type)),
8866          since we strip the casts of up to VOID return value from malloc.
8867          Perhaps we ought to inherit type from non-VOID argument here?  */
8868       STRIP_NOPS (src);
8869       STRIP_NOPS (dest);
8870       if (!POINTER_TYPE_P (TREE_TYPE (src))
8871           || !POINTER_TYPE_P (TREE_TYPE (dest)))
8872         return NULL_TREE;
8873       /* As we fold (void *)(p + CST) to (void *)p + CST undo this here.  */
8874       if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8875         {
8876           tree tem = TREE_OPERAND (src, 0);
8877           STRIP_NOPS (tem);
8878           if (tem != TREE_OPERAND (src, 0))
8879             src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8880         }
8881       if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8882         {
8883           tree tem = TREE_OPERAND (dest, 0);
8884           STRIP_NOPS (tem);
8885           if (tem != TREE_OPERAND (dest, 0))
8886             dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8887         }
8888       srctype = TREE_TYPE (TREE_TYPE (src));
8889       if (TREE_CODE (srctype) == ARRAY_TYPE
8890           && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8891         {
8892           srctype = TREE_TYPE (srctype);
8893           STRIP_NOPS (src);
8894           src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8895         }
8896       desttype = TREE_TYPE (TREE_TYPE (dest));
8897       if (TREE_CODE (desttype) == ARRAY_TYPE
8898           && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8899         {
8900           desttype = TREE_TYPE (desttype);
8901           STRIP_NOPS (dest);
8902           dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8903         }
8904       if (TREE_ADDRESSABLE (srctype)
8905           || TREE_ADDRESSABLE (desttype))
8906         return NULL_TREE;
8907
8908       src_align = get_pointer_alignment (src);
8909       dest_align = get_pointer_alignment (dest);
8910       if (dest_align < TYPE_ALIGN (desttype)
8911           || src_align < TYPE_ALIGN (srctype))
8912         return NULL_TREE;
8913
8914       if (!ignore)
8915         dest = builtin_save_expr (dest);
8916
8917       /* Build accesses at offset zero with a ref-all character type.  */
8918       off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8919                                                          ptr_mode, true), 0);
8920
8921       destvar = dest;
8922       STRIP_NOPS (destvar);
8923       if (TREE_CODE (destvar) == ADDR_EXPR
8924           && var_decl_component_p (TREE_OPERAND (destvar, 0))
8925           && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8926         destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8927       else
8928         destvar = NULL_TREE;
8929
8930       srcvar = src;
8931       STRIP_NOPS (srcvar);
8932       if (TREE_CODE (srcvar) == ADDR_EXPR
8933           && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8934           && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8935         {
8936           if (!destvar
8937               || src_align >= TYPE_ALIGN (desttype))
8938             srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8939                                   srcvar, off0);
8940           else if (!STRICT_ALIGNMENT)
8941             {
8942               srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8943                                             src_align);
8944               srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8945             }
8946           else
8947             srcvar = NULL_TREE;
8948         }
8949       else
8950         srcvar = NULL_TREE;
8951
8952       if (srcvar == NULL_TREE && destvar == NULL_TREE)
8953         return NULL_TREE;
8954
8955       if (srcvar == NULL_TREE)
8956         {
8957           STRIP_NOPS (src);
8958           if (src_align >= TYPE_ALIGN (desttype))
8959             srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8960           else
8961             {
8962               if (STRICT_ALIGNMENT)
8963                 return NULL_TREE;
8964               srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8965                                             src_align);
8966               srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8967             }
8968         }
8969       else if (destvar == NULL_TREE)
8970         {
8971           STRIP_NOPS (dest);
8972           if (dest_align >= TYPE_ALIGN (srctype))
8973             destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8974           else
8975             {
8976               if (STRICT_ALIGNMENT)
8977                 return NULL_TREE;
8978               desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8979                                              dest_align);
8980               destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8981             }
8982         }
8983
8984       expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8985     }
8986
8987   if (ignore)
8988     return expr;
8989
8990   if (endp == 0 || endp == 3)
8991     return omit_one_operand_loc (loc, type, dest, expr);
8992
8993   if (expr == len)
8994     expr = NULL_TREE;
8995
8996   if (endp == 2)
8997     len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8998                        ssize_int (1));
8999
9000   dest = fold_build_pointer_plus_loc (loc, dest, len);
9001   dest = fold_convert_loc (loc, type, dest);
9002   if (expr)
9003     dest = omit_one_operand_loc (loc, type, dest, expr);
9004   return dest;
9005 }
9006
9007 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9008    If LEN is not NULL, it represents the length of the string to be
9009    copied.  Return NULL_TREE if no simplification can be made.  */
9010
9011 tree
9012 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
9013 {
9014   tree fn;
9015
9016   if (!validate_arg (dest, POINTER_TYPE)
9017       || !validate_arg (src, POINTER_TYPE))
9018     return NULL_TREE;
9019
9020   /* If SRC and DEST are the same (and not volatile), return DEST.  */
9021   if (operand_equal_p (src, dest, 0))
9022     return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9023
9024   if (optimize_function_for_size_p (cfun))
9025     return NULL_TREE;
9026
9027   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9028   if (!fn)
9029     return NULL_TREE;
9030
9031   if (!len)
9032     {
9033       len = c_strlen (src, 1);
9034       if (! len || TREE_SIDE_EFFECTS (len))
9035         return NULL_TREE;
9036     }
9037
9038   len = fold_convert_loc (loc, size_type_node, len);
9039   len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9040   return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9041                            build_call_expr_loc (loc, fn, 3, dest, src, len));
9042 }
9043
9044 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9045    Return NULL_TREE if no simplification can be made.  */
9046
9047 static tree
9048 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9049 {
9050   tree fn, len, lenp1, call, type;
9051
9052   if (!validate_arg (dest, POINTER_TYPE)
9053       || !validate_arg (src, POINTER_TYPE))
9054     return NULL_TREE;
9055
9056   len = c_strlen (src, 1);
9057   if (!len
9058       || TREE_CODE (len) != INTEGER_CST)
9059     return NULL_TREE;
9060
9061   if (optimize_function_for_size_p (cfun)
9062       /* If length is zero it's small enough.  */
9063       && !integer_zerop (len))
9064     return NULL_TREE;
9065
9066   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9067   if (!fn)
9068     return NULL_TREE;
9069
9070   lenp1 = size_binop_loc (loc, PLUS_EXPR,
9071                           fold_convert_loc (loc, size_type_node, len),
9072                           build_int_cst (size_type_node, 1));
9073   /* We use dest twice in building our expression.  Save it from
9074      multiple expansions.  */
9075   dest = builtin_save_expr (dest);
9076   call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9077
9078   type = TREE_TYPE (TREE_TYPE (fndecl));
9079   dest = fold_build_pointer_plus_loc (loc, dest, len);
9080   dest = fold_convert_loc (loc, type, dest);
9081   dest = omit_one_operand_loc (loc, type, dest, call);
9082   return dest;
9083 }
9084
9085 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9086    If SLEN is not NULL, it represents the length of the source string.
9087    Return NULL_TREE if no simplification can be made.  */
9088
9089 tree
9090 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9091                       tree src, tree len, tree slen)
9092 {
9093   tree fn;
9094
9095   if (!validate_arg (dest, POINTER_TYPE)
9096       || !validate_arg (src, POINTER_TYPE)
9097       || !validate_arg (len, INTEGER_TYPE))
9098     return NULL_TREE;
9099
9100   /* If the LEN parameter is zero, return DEST.  */
9101   if (integer_zerop (len))
9102     return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9103
9104   /* We can't compare slen with len as constants below if len is not a
9105      constant.  */
9106   if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9107     return NULL_TREE;
9108
9109   if (!slen)
9110     slen = c_strlen (src, 1);
9111
9112   /* Now, we must be passed a constant src ptr parameter.  */
9113   if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9114     return NULL_TREE;
9115
9116   slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9117
9118   /* We do not support simplification of this case, though we do
9119      support it when expanding trees into RTL.  */
9120   /* FIXME: generate a call to __builtin_memset.  */
9121   if (tree_int_cst_lt (slen, len))
9122     return NULL_TREE;
9123
9124   /* OK transform into builtin memcpy.  */
9125   fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9126   if (!fn)
9127     return NULL_TREE;
9128
9129   len = fold_convert_loc (loc, size_type_node, len);
9130   return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9131                            build_call_expr_loc (loc, fn, 3, dest, src, len));
9132 }
9133
9134 /* Fold function call to builtin memchr.  ARG1, ARG2 and LEN are the
9135    arguments to the call, and TYPE is its return type.
9136    Return NULL_TREE if no simplification can be made.  */
9137
9138 static tree
9139 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9140 {
9141   if (!validate_arg (arg1, POINTER_TYPE)
9142       || !validate_arg (arg2, INTEGER_TYPE)
9143       || !validate_arg (len, INTEGER_TYPE))
9144     return NULL_TREE;
9145   else
9146     {
9147       const char *p1;
9148
9149       if (TREE_CODE (arg2) != INTEGER_CST
9150           || !host_integerp (len, 1))
9151         return NULL_TREE;
9152
9153       p1 = c_getstr (arg1);
9154       if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9155         {
9156           char c;
9157           const char *r;
9158           tree tem;
9159
9160           if (target_char_cast (arg2, &c))
9161             return NULL_TREE;
9162
9163           r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
9164
9165           if (r == NULL)
9166             return build_int_cst (TREE_TYPE (arg1), 0);
9167
9168           tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9169           return fold_convert_loc (loc, type, tem);
9170         }
9171       return NULL_TREE;
9172     }
9173 }
9174
9175 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9176    Return NULL_TREE if no simplification can be made.  */
9177
9178 static tree
9179 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9180 {
9181   const char *p1, *p2;
9182
9183   if (!validate_arg (arg1, POINTER_TYPE)
9184       || !validate_arg (arg2, POINTER_TYPE)
9185       || !validate_arg (len, INTEGER_TYPE))
9186     return NULL_TREE;
9187
9188   /* If the LEN parameter is zero, return zero.  */
9189   if (integer_zerop (len))
9190     return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9191                               arg1, arg2);
9192
9193   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
9194   if (operand_equal_p (arg1, arg2, 0))
9195     return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9196
9197   p1 = c_getstr (arg1);
9198   p2 = c_getstr (arg2);
9199
9200   /* If all arguments are constant, and the value of len is not greater
9201      than the lengths of arg1 and arg2, evaluate at compile-time.  */
9202   if (host_integerp (len, 1) && p1 && p2
9203       && compare_tree_int (len, strlen (p1) + 1) <= 0
9204       && compare_tree_int (len, strlen (p2) + 1) <= 0)
9205     {
9206       const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9207
9208       if (r > 0)
9209         return integer_one_node;
9210       else if (r < 0)
9211         return integer_minus_one_node;
9212       else
9213         return integer_zero_node;
9214     }
9215
9216   /* If len parameter is one, return an expression corresponding to
9217      (*(const unsigned char*)arg1 - (const unsigned char*)arg2).  */
9218   if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9219     {
9220       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9221       tree cst_uchar_ptr_node
9222         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9223
9224       tree ind1
9225         = fold_convert_loc (loc, integer_type_node,
9226                             build1 (INDIRECT_REF, cst_uchar_node,
9227                                     fold_convert_loc (loc,
9228                                                       cst_uchar_ptr_node,
9229                                                       arg1)));
9230       tree ind2
9231         = fold_convert_loc (loc, integer_type_node,
9232                             build1 (INDIRECT_REF, cst_uchar_node,
9233                                     fold_convert_loc (loc,
9234                                                       cst_uchar_ptr_node,
9235                                                       arg2)));
9236       return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9237     }
9238
9239   return NULL_TREE;
9240 }
9241
9242 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9243    Return NULL_TREE if no simplification can be made.  */
9244
9245 static tree
9246 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9247 {
9248   const char *p1, *p2;
9249
9250   if (!validate_arg (arg1, POINTER_TYPE)
9251       || !validate_arg (arg2, POINTER_TYPE))
9252     return NULL_TREE;
9253
9254   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
9255   if (operand_equal_p (arg1, arg2, 0))
9256     return integer_zero_node;
9257
9258   p1 = c_getstr (arg1);
9259   p2 = c_getstr (arg2);
9260
9261   if (p1 && p2)
9262     {
9263       const int i = strcmp (p1, p2);
9264       if (i < 0)
9265         return integer_minus_one_node;
9266       else if (i > 0)
9267         return integer_one_node;
9268       else
9269         return integer_zero_node;
9270     }
9271
9272   /* If the second arg is "", return *(const unsigned char*)arg1.  */
9273   if (p2 && *p2 == '\0')
9274     {
9275       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9276       tree cst_uchar_ptr_node
9277         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9278
9279       return fold_convert_loc (loc, integer_type_node,
9280                                build1 (INDIRECT_REF, cst_uchar_node,
9281                                        fold_convert_loc (loc,
9282                                                          cst_uchar_ptr_node,
9283                                                          arg1)));
9284     }
9285
9286   /* If the first arg is "", return -*(const unsigned char*)arg2.  */
9287   if (p1 && *p1 == '\0')
9288     {
9289       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9290       tree cst_uchar_ptr_node
9291         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9292
9293       tree temp
9294         = fold_convert_loc (loc, integer_type_node,
9295                             build1 (INDIRECT_REF, cst_uchar_node,
9296                                     fold_convert_loc (loc,
9297                                                       cst_uchar_ptr_node,
9298                                                       arg2)));
9299       return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9300     }
9301
9302   return NULL_TREE;
9303 }
9304
9305 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9306    Return NULL_TREE if no simplification can be made.  */
9307
9308 static tree
9309 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9310 {
9311   const char *p1, *p2;
9312
9313   if (!validate_arg (arg1, POINTER_TYPE)
9314       || !validate_arg (arg2, POINTER_TYPE)
9315       || !validate_arg (len, INTEGER_TYPE))
9316     return NULL_TREE;
9317
9318   /* If the LEN parameter is zero, return zero.  */
9319   if (integer_zerop (len))
9320     return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9321                               arg1, arg2);
9322
9323   /* If ARG1 and ARG2 are the same (and not volatile), return zero.  */
9324   if (operand_equal_p (arg1, arg2, 0))
9325     return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9326
9327   p1 = c_getstr (arg1);
9328   p2 = c_getstr (arg2);
9329
9330   if (host_integerp (len, 1) && p1 && p2)
9331     {
9332       const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9333       if (i > 0)
9334         return integer_one_node;
9335       else if (i < 0)
9336         return integer_minus_one_node;
9337       else
9338         return integer_zero_node;
9339     }
9340
9341   /* If the second arg is "", and the length is greater than zero,
9342      return *(const unsigned char*)arg1.  */
9343   if (p2 && *p2 == '\0'
9344       && TREE_CODE (len) == INTEGER_CST
9345       && tree_int_cst_sgn (len) == 1)
9346     {
9347       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9348       tree cst_uchar_ptr_node
9349         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9350
9351       return fold_convert_loc (loc, integer_type_node,
9352                                build1 (INDIRECT_REF, cst_uchar_node,
9353                                        fold_convert_loc (loc,
9354                                                          cst_uchar_ptr_node,
9355                                                          arg1)));
9356     }
9357
9358   /* If the first arg is "", and the length is greater than zero,
9359      return -*(const unsigned char*)arg2.  */
9360   if (p1 && *p1 == '\0'
9361       && TREE_CODE (len) == INTEGER_CST
9362       && tree_int_cst_sgn (len) == 1)
9363     {
9364       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9365       tree cst_uchar_ptr_node
9366         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9367
9368       tree temp = fold_convert_loc (loc, integer_type_node,
9369                                     build1 (INDIRECT_REF, cst_uchar_node,
9370                                             fold_convert_loc (loc,
9371                                                               cst_uchar_ptr_node,
9372                                                               arg2)));
9373       return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9374     }
9375
9376   /* If len parameter is one, return an expression corresponding to
9377      (*(const unsigned char*)arg1 - (const unsigned char*)arg2).  */
9378   if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9379     {
9380       tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9381       tree cst_uchar_ptr_node
9382         = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9383
9384       tree ind1 = fold_convert_loc (loc, integer_type_node,
9385                                     build1 (INDIRECT_REF, cst_uchar_node,
9386                                             fold_convert_loc (loc,
9387                                                               cst_uchar_ptr_node,
9388                                                               arg1)));
9389       tree ind2 = fold_convert_loc (loc, integer_type_node,
9390                                     build1 (INDIRECT_REF, cst_uchar_node,
9391                                             fold_convert_loc (loc,
9392                                                               cst_uchar_ptr_node,
9393                                                               arg2)));
9394       return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9395     }
9396
9397   return NULL_TREE;
9398 }
9399
9400 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9401    ARG.  Return NULL_TREE if no simplification can be made.  */
9402
9403 static tree
9404 fold_builtin_signbit (location_t loc, tree arg, tree type)
9405 {
9406   if (!validate_arg (arg, REAL_TYPE))
9407     return NULL_TREE;
9408
9409   /* If ARG is a compile-time constant, determine the result.  */
9410   if (TREE_CODE (arg) == REAL_CST
9411       && !TREE_OVERFLOW (arg))
9412     {
9413       REAL_VALUE_TYPE c;
9414
9415       c = TREE_REAL_CST (arg);
9416       return (REAL_VALUE_NEGATIVE (c)
9417               ? build_one_cst (type)
9418               : build_zero_cst (type));
9419     }
9420
9421   /* If ARG is non-negative, the result is always zero.  */
9422   if (tree_expr_nonnegative_p (arg))
9423     return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9424
9425   /* If ARG's format doesn't have signed zeros, return "arg < 0.0".  */
9426   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9427     return fold_convert (type,
9428                          fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9429                         build_real (TREE_TYPE (arg), dconst0)));
9430
9431   return NULL_TREE;
9432 }
9433
9434 /* Fold function call to builtin copysign, copysignf or copysignl with
9435    arguments ARG1 and ARG2.  Return NULL_TREE if no simplification can
9436    be made.  */
9437
9438 static tree
9439 fold_builtin_copysign (location_t loc, tree fndecl,
9440                        tree arg1, tree arg2, tree type)
9441 {
9442   tree tem;
9443
9444   if (!validate_arg (arg1, REAL_TYPE)
9445       || !validate_arg (arg2, REAL_TYPE))
9446     return NULL_TREE;
9447
9448   /* copysign(X,X) is X.  */
9449   if (operand_equal_p (arg1, arg2, 0))
9450     return fold_convert_loc (loc, type, arg1);
9451
9452   /* If ARG1 and ARG2 are compile-time constants, determine the result.  */
9453   if (TREE_CODE (arg1) == REAL_CST
9454       && TREE_CODE (arg2) == REAL_CST
9455       && !TREE_OVERFLOW (arg1)
9456       && !TREE_OVERFLOW (arg2))
9457     {
9458       REAL_VALUE_TYPE c1, c2;
9459
9460       c1 = TREE_REAL_CST (arg1);
9461       c2 = TREE_REAL_CST (arg2);
9462       /* c1.sign := c2.sign.  */
9463       real_copysign (&c1, &c2);
9464       return build_real (type, c1);
9465     }
9466
9467   /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9468      Remember to evaluate Y for side-effects.  */
9469   if (tree_expr_nonnegative_p (arg2))
9470     return omit_one_operand_loc (loc, type,
9471                              fold_build1_loc (loc, ABS_EXPR, type, arg1),
9472                              arg2);
9473
9474   /* Strip sign changing operations for the first argument.  */
9475   tem = fold_strip_sign_ops (arg1);
9476   if (tem)
9477     return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9478
9479   return NULL_TREE;
9480 }
9481
9482 /* Fold a call to builtin isascii with argument ARG.  */
9483
9484 static tree
9485 fold_builtin_isascii (location_t loc, tree arg)
9486 {
9487   if (!validate_arg (arg, INTEGER_TYPE))
9488     return NULL_TREE;
9489   else
9490     {
9491       /* Transform isascii(c) -> ((c & ~0x7f) == 0).  */
9492       arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9493                          build_int_cst (integer_type_node,
9494                                         ~ (unsigned HOST_WIDE_INT) 0x7f));
9495       return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9496                               arg, integer_zero_node);
9497     }
9498 }
9499
9500 /* Fold a call to builtin toascii with argument ARG.  */
9501
9502 static tree
9503 fold_builtin_toascii (location_t loc, tree arg)
9504 {
9505   if (!validate_arg (arg, INTEGER_TYPE))
9506     return NULL_TREE;
9507
9508   /* Transform toascii(c) -> (c & 0x7f).  */
9509   return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9510                           build_int_cst (integer_type_node, 0x7f));
9511 }
9512
9513 /* Fold a call to builtin isdigit with argument ARG.  */
9514
9515 static tree
9516 fold_builtin_isdigit (location_t loc, tree arg)
9517 {
9518   if (!validate_arg (arg, INTEGER_TYPE))
9519     return NULL_TREE;
9520   else
9521     {
9522       /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9.  */
9523       /* According to the C standard, isdigit is unaffected by locale.
9524          However, it definitely is affected by the target character set.  */
9525       unsigned HOST_WIDE_INT target_digit0
9526         = lang_hooks.to_target_charset ('0');
9527
9528       if (target_digit0 == 0)
9529         return NULL_TREE;
9530
9531       arg = fold_convert_loc (loc, unsigned_type_node, arg);
9532       arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9533                          build_int_cst (unsigned_type_node, target_digit0));
9534       return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9535                           build_int_cst (unsigned_type_node, 9));
9536     }
9537 }
9538
9539 /* Fold a call to fabs, fabsf or fabsl with argument ARG.  */
9540
9541 static tree
9542 fold_builtin_fabs (location_t loc, tree arg, tree type)
9543 {
9544   if (!validate_arg (arg, REAL_TYPE))
9545     return NULL_TREE;
9546
9547   arg = fold_convert_loc (loc, type, arg);
9548   if (TREE_CODE (arg) == REAL_CST)
9549     return fold_abs_const (arg, type);
9550   return fold_build1_loc (loc, ABS_EXPR, type, arg);
9551 }
9552
9553 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG.  */
9554
9555 static tree
9556 fold_builtin_abs (location_t loc, tree arg, tree type)
9557 {
9558   if (!validate_arg (arg, INTEGER_TYPE))
9559     return NULL_TREE;
9560
9561   arg = fold_convert_loc (loc, type, arg);
9562   if (TREE_CODE (arg) == INTEGER_CST)
9563     return fold_abs_const (arg, type);
9564   return fold_build1_loc (loc, ABS_EXPR, type, arg);
9565 }
9566
9567 /* Fold a fma operation with arguments ARG[012].  */
9568
9569 tree
9570 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9571           tree type, tree arg0, tree arg1, tree arg2)
9572 {
9573   if (TREE_CODE (arg0) == REAL_CST
9574       && TREE_CODE (arg1) == REAL_CST
9575       && TREE_CODE (arg2) == REAL_CST)
9576     return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9577
9578   return NULL_TREE;
9579 }
9580
9581 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012].  */
9582
9583 static tree
9584 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9585 {
9586   if (validate_arg (arg0, REAL_TYPE)
9587       && validate_arg(arg1, REAL_TYPE)
9588       && validate_arg(arg2, REAL_TYPE))
9589     {
9590       tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9591       if (tem)
9592         return tem;
9593
9594       /* ??? Only expand to FMA_EXPR if it's directly supported.  */
9595       if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9596         return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9597     }
9598   return NULL_TREE;
9599 }
9600
9601 /* Fold a call to builtin fmin or fmax.  */
9602
9603 static tree
9604 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9605                         tree type, bool max)
9606 {
9607   if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9608     {
9609       /* Calculate the result when the argument is a constant.  */
9610       tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9611
9612       if (res)
9613         return res;
9614
9615       /* If either argument is NaN, return the other one.  Avoid the
9616          transformation if we get (and honor) a signalling NaN.  Using
9617          omit_one_operand() ensures we create a non-lvalue.  */
9618       if (TREE_CODE (arg0) == REAL_CST
9619           && real_isnan (&TREE_REAL_CST (arg0))
9620           && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9621               || ! TREE_REAL_CST (arg0).signalling))
9622         return omit_one_operand_loc (loc, type, arg1, arg0);
9623       if (TREE_CODE (arg1) == REAL_CST
9624           && real_isnan (&TREE_REAL_CST (arg1))
9625           && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9626               || ! TREE_REAL_CST (arg1).signalling))
9627         return omit_one_operand_loc (loc, type, arg0, arg1);
9628
9629       /* Transform fmin/fmax(x,x) -> x.  */
9630       if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9631         return omit_one_operand_loc (loc, type, arg0, arg1);
9632
9633       /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
9634          functions to return the numeric arg if the other one is NaN.
9635          These tree codes don't honor that, so only transform if
9636          -ffinite-math-only is set.  C99 doesn't require -0.0 to be
9637          handled, so we don't have to worry about it either.  */
9638       if (flag_finite_math_only)
9639         return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9640                             fold_convert_loc (loc, type, arg0),
9641                             fold_convert_loc (loc, type, arg1));
9642     }
9643   return NULL_TREE;
9644 }
9645
9646 /* Fold a call to builtin carg(a+bi) -> atan2(b,a).  */
9647
9648 static tree
9649 fold_builtin_carg (location_t loc, tree arg, tree type)
9650 {
9651   if (validate_arg (arg, COMPLEX_TYPE)
9652       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9653     {
9654       tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9655
9656       if (atan2_fn)
9657         {
9658           tree new_arg = builtin_save_expr (arg);
9659           tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9660           tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9661           return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9662         }
9663     }
9664
9665   return NULL_TREE;
9666 }
9667
9668 /* Fold a call to builtin logb/ilogb.  */
9669
9670 static tree
9671 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9672 {
9673   if (! validate_arg (arg, REAL_TYPE))
9674     return NULL_TREE;
9675
9676   STRIP_NOPS (arg);
9677
9678   if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9679     {
9680       const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9681
9682       switch (value->cl)
9683       {
9684       case rvc_nan:
9685       case rvc_inf:
9686         /* If arg is Inf or NaN and we're logb, return it.  */
9687         if (TREE_CODE (rettype) == REAL_TYPE)
9688           return fold_convert_loc (loc, rettype, arg);
9689         /* Fall through... */
9690       case rvc_zero:
9691         /* Zero may set errno and/or raise an exception for logb, also
9692            for ilogb we don't know FP_ILOGB0.  */
9693         return NULL_TREE;
9694       case rvc_normal:
9695         /* For normal numbers, proceed iff radix == 2.  In GCC,
9696            normalized significands are in the range [0.5, 1.0).  We
9697            want the exponent as if they were [1.0, 2.0) so get the
9698            exponent and subtract 1.  */
9699         if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9700           return fold_convert_loc (loc, rettype,
9701                                    build_int_cst (integer_type_node,
9702                                                   REAL_EXP (value)-1));
9703         break;
9704       }
9705     }
9706
9707   return NULL_TREE;
9708 }
9709
9710 /* Fold a call to builtin significand, if radix == 2.  */
9711
9712 static tree
9713 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9714 {
9715   if (! validate_arg (arg, REAL_TYPE))
9716     return NULL_TREE;
9717
9718   STRIP_NOPS (arg);
9719
9720   if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9721     {
9722       const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9723
9724       switch (value->cl)
9725       {
9726       case rvc_zero:
9727       case rvc_nan:
9728       case rvc_inf:
9729         /* If arg is +-0, +-Inf or +-NaN, then return it.  */
9730         return fold_convert_loc (loc, rettype, arg);
9731       case rvc_normal:
9732         /* For normal numbers, proceed iff radix == 2.  */
9733         if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9734           {
9735             REAL_VALUE_TYPE result = *value;
9736             /* In GCC, normalized significands are in the range [0.5,
9737                1.0).  We want them to be [1.0, 2.0) so set the
9738                exponent to 1.  */
9739             SET_REAL_EXP (&result, 1);
9740             return build_real (rettype, result);
9741           }
9742         break;
9743       }
9744     }
9745
9746   return NULL_TREE;
9747 }
9748
9749 /* Fold a call to builtin frexp, we can assume the base is 2.  */
9750
9751 static tree
9752 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9753 {
9754   if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9755     return NULL_TREE;
9756
9757   STRIP_NOPS (arg0);
9758
9759   if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9760     return NULL_TREE;
9761
9762   arg1 = build_fold_indirect_ref_loc (loc, arg1);
9763
9764   /* Proceed if a valid pointer type was passed in.  */
9765   if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9766     {
9767       const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9768       tree frac, exp;
9769
9770       switch (value->cl)
9771       {
9772       case rvc_zero:
9773         /* For +-0, return (*exp = 0, +-0).  */
9774         exp = integer_zero_node;
9775         frac = arg0;
9776         break;
9777       case rvc_nan:
9778       case rvc_inf:
9779         /* For +-NaN or +-Inf, *exp is unspecified, return arg0.  */
9780         return omit_one_operand_loc (loc, rettype, arg0, arg1);
9781       case rvc_normal:
9782         {
9783           /* Since the frexp function always expects base 2, and in
9784              GCC normalized significands are already in the range
9785              [0.5, 1.0), we have exactly what frexp wants.  */
9786           REAL_VALUE_TYPE frac_rvt = *value;
9787           SET_REAL_EXP (&frac_rvt, 0);
9788           frac = build_real (rettype, frac_rvt);
9789           exp = build_int_cst (integer_type_node, REAL_EXP (value));
9790         }
9791         break;
9792       default:
9793         gcc_unreachable ();
9794       }
9795
9796       /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9797       arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9798       TREE_SIDE_EFFECTS (arg1) = 1;
9799       return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9800     }
9801
9802   return NULL_TREE;
9803 }
9804
9805 /* Fold a call to builtin ldexp or scalbn/scalbln.  If LDEXP is true
9806    then we can assume the base is two.  If it's false, then we have to
9807    check the mode of the TYPE parameter in certain cases.  */
9808
9809 static tree
9810 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9811                             tree type, bool ldexp)
9812 {
9813   if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9814     {
9815       STRIP_NOPS (arg0);
9816       STRIP_NOPS (arg1);
9817
9818       /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0.  */
9819       if (real_zerop (arg0) || integer_zerop (arg1)
9820           || (TREE_CODE (arg0) == REAL_CST
9821               && !real_isfinite (&TREE_REAL_CST (arg0))))
9822         return omit_one_operand_loc (loc, type, arg0, arg1);
9823
9824       /* If both arguments are constant, then try to evaluate it.  */
9825       if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9826           && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9827           && host_integerp (arg1, 0))
9828         {
9829           /* Bound the maximum adjustment to twice the range of the
9830              mode's valid exponents.  Use abs to ensure the range is
9831              positive as a sanity check.  */
9832           const long max_exp_adj = 2 *
9833             labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9834                  - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9835
9836           /* Get the user-requested adjustment.  */
9837           const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9838
9839           /* The requested adjustment must be inside this range.  This
9840              is a preliminary cap to avoid things like overflow, we
9841              may still fail to compute the result for other reasons.  */
9842           if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9843             {
9844               REAL_VALUE_TYPE initial_result;
9845
9846               real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9847
9848               /* Ensure we didn't overflow.  */
9849               if (! real_isinf (&initial_result))
9850                 {
9851                   const REAL_VALUE_TYPE trunc_result
9852                     = real_value_truncate (TYPE_MODE (type), initial_result);
9853
9854                   /* Only proceed if the target mode can hold the
9855                      resulting value.  */
9856                   if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9857                     return build_real (type, trunc_result);
9858                 }
9859             }
9860         }
9861     }
9862
9863   return NULL_TREE;
9864 }
9865
9866 /* Fold a call to builtin modf.  */
9867
9868 static tree
9869 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9870 {
9871   if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9872     return NULL_TREE;
9873
9874   STRIP_NOPS (arg0);
9875
9876   if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9877     return NULL_TREE;
9878
9879   arg1 = build_fold_indirect_ref_loc (loc, arg1);
9880
9881   /* Proceed if a valid pointer type was passed in.  */
9882   if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9883     {
9884       const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9885       REAL_VALUE_TYPE trunc, frac;
9886
9887       switch (value->cl)
9888       {
9889       case rvc_nan:
9890       case rvc_zero:
9891         /* For +-NaN or +-0, return (*arg1 = arg0, arg0).  */
9892         trunc = frac = *value;
9893         break;
9894       case rvc_inf:
9895         /* For +-Inf, return (*arg1 = arg0, +-0).  */
9896         frac = dconst0;
9897         frac.sign = value->sign;
9898         trunc = *value;
9899         break;
9900       case rvc_normal:
9901         /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)).  */
9902         real_trunc (&trunc, VOIDmode, value);
9903         real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9904         /* If the original number was negative and already
9905            integral, then the fractional part is -0.0.  */
9906         if (value->sign && frac.cl == rvc_zero)
9907           frac.sign = value->sign;
9908         break;
9909       }
9910
9911       /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9912       arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9913                           build_real (rettype, trunc));
9914       TREE_SIDE_EFFECTS (arg1) = 1;
9915       return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9916                           build_real (rettype, frac));
9917     }
9918
9919   return NULL_TREE;
9920 }
9921
9922 /* Given a location LOC, an interclass builtin function decl FNDECL
9923    and its single argument ARG, return an folded expression computing
9924    the same, or NULL_TREE if we either couldn't or didn't want to fold
9925    (the latter happen if there's an RTL instruction available).  */
9926
9927 static tree
9928 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9929 {
9930   enum machine_mode mode;
9931
9932   if (!validate_arg (arg, REAL_TYPE))
9933     return NULL_TREE;
9934
9935   if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9936     return NULL_TREE;
9937
9938   mode = TYPE_MODE (TREE_TYPE (arg));
9939
9940   /* If there is no optab, try generic code.  */
9941   switch (DECL_FUNCTION_CODE (fndecl))
9942     {
9943       tree result;
9944
9945     CASE_FLT_FN (BUILT_IN_ISINF):
9946       {
9947         /* isinf(x) -> isgreater(fabs(x),DBL_MAX).  */
9948         tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9949         tree const type = TREE_TYPE (arg);
9950         REAL_VALUE_TYPE r;
9951         char buf[128];
9952
9953         get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9954         real_from_string (&r, buf);
9955         result = build_call_expr (isgr_fn, 2,
9956                                   fold_build1_loc (loc, ABS_EXPR, type, arg),
9957                                   build_real (type, r));
9958         return result;
9959       }
9960     CASE_FLT_FN (BUILT_IN_FINITE):
9961     case BUILT_IN_ISFINITE:
9962       {
9963         /* isfinite(x) -> islessequal(fabs(x),DBL_MAX).  */
9964         tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9965         tree const type = TREE_TYPE (arg);
9966         REAL_VALUE_TYPE r;
9967         char buf[128];
9968
9969         get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9970         real_from_string (&r, buf);
9971         result = build_call_expr (isle_fn, 2,
9972                                   fold_build1_loc (loc, ABS_EXPR, type, arg),
9973                                   build_real (type, r));
9974         /*result = fold_build2_loc (loc, UNGT_EXPR,
9975                                   TREE_TYPE (TREE_TYPE (fndecl)),
9976                                   fold_build1_loc (loc, ABS_EXPR, type, arg),
9977                                   build_real (type, r));
9978         result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9979                                   TREE_TYPE (TREE_TYPE (fndecl)),
9980                                   result);*/
9981         return result;
9982       }
9983     case BUILT_IN_ISNORMAL:
9984       {
9985         /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9986            islessequal(fabs(x),DBL_MAX).  */
9987         tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9988         tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9989         tree const type = TREE_TYPE (arg);
9990         REAL_VALUE_TYPE rmax, rmin;
9991         char buf[128];
9992
9993         get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9994         real_from_string (&rmax, buf);
9995         sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9996         real_from_string (&rmin, buf);
9997         arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9998         result = build_call_expr (isle_fn, 2, arg,
9999                                   build_real (type, rmax));
10000         result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
10001                               build_call_expr (isge_fn, 2, arg,
10002                                                build_real (type, rmin)));
10003         return result;
10004       }
10005     default:
10006       break;
10007     }
10008
10009   return NULL_TREE;
10010 }
10011
10012 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10013    ARG is the argument for the call.  */
10014
10015 static tree
10016 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
10017 {
10018   tree type = TREE_TYPE (TREE_TYPE (fndecl));
10019   REAL_VALUE_TYPE r;
10020
10021   if (!validate_arg (arg, REAL_TYPE))
10022     return NULL_TREE;
10023
10024   switch (builtin_index)
10025     {
10026     case BUILT_IN_ISINF:
10027       if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10028         return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10029
10030       if (TREE_CODE (arg) == REAL_CST)
10031         {
10032           r = TREE_REAL_CST (arg);
10033           if (real_isinf (&r))
10034             return real_compare (GT_EXPR, &r, &dconst0)
10035                    ? integer_one_node : integer_minus_one_node;
10036           else
10037             return integer_zero_node;
10038         }
10039
10040       return NULL_TREE;
10041
10042     case BUILT_IN_ISINF_SIGN:
10043       {
10044         /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10045         /* In a boolean context, GCC will fold the inner COND_EXPR to
10046            1.  So e.g. "if (isinf_sign(x))" would be folded to just
10047            "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10048         tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10049         tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10050         tree tmp = NULL_TREE;
10051
10052         arg = builtin_save_expr (arg);
10053
10054         if (signbit_fn && isinf_fn)
10055           {
10056             tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10057             tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10058
10059             signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10060                                         signbit_call, integer_zero_node);
10061             isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10062                                       isinf_call, integer_zero_node);
10063
10064             tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10065                                integer_minus_one_node, integer_one_node);
10066             tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10067                                isinf_call, tmp,
10068                                integer_zero_node);
10069           }
10070
10071         return tmp;
10072       }
10073
10074     case BUILT_IN_ISFINITE:
10075       if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10076           && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10077         return omit_one_operand_loc (loc, type, integer_one_node, arg);
10078
10079       if (TREE_CODE (arg) == REAL_CST)
10080         {
10081           r = TREE_REAL_CST (arg);
10082           return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10083         }
10084
10085       return NULL_TREE;
10086
10087     case BUILT_IN_ISNAN:
10088       if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10089         return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10090
10091       if (TREE_CODE (arg) == REAL_CST)
10092         {
10093           r = TREE_REAL_CST (arg);
10094           return real_isnan (&r) ? integer_one_node : integer_zero_node;
10095         }
10096
10097       arg = builtin_save_expr (arg);
10098       return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10099
10100     default:
10101       gcc_unreachable ();
10102     }
10103 }
10104
10105 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10106    This builtin will generate code to return the appropriate floating
10107    point classification depending on the value of the floating point
10108    number passed in.  The possible return values must be supplied as
10109    int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10110    FP_NORMAL, FP_SUBNORMAL and FP_ZERO.  The ellipses is for exactly
10111    one floating point argument which is "type generic".  */
10112
10113 static tree
10114 fold_builtin_fpclassify (location_t loc, tree exp)
10115 {
10116   tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10117     arg, type, res, tmp;
10118   enum machine_mode mode;
10119   REAL_VALUE_TYPE r;
10120   char buf[128];
10121
10122   /* Verify the required arguments in the original call.  */
10123   if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10124                          INTEGER_TYPE, INTEGER_TYPE,
10125                          INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10126     return NULL_TREE;
10127
10128   fp_nan = CALL_EXPR_ARG (exp, 0);
10129   fp_infinite = CALL_EXPR_ARG (exp, 1);
10130   fp_normal = CALL_EXPR_ARG (exp, 2);
10131   fp_subnormal = CALL_EXPR_ARG (exp, 3);
10132   fp_zero = CALL_EXPR_ARG (exp, 4);
10133   arg = CALL_EXPR_ARG (exp, 5);
10134   type = TREE_TYPE (arg);
10135   mode = TYPE_MODE (type);
10136   arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10137
10138   /* fpclassify(x) ->
10139        isnan(x) ? FP_NAN :
10140          (fabs(x) == Inf ? FP_INFINITE :
10141            (fabs(x) >= DBL_MIN ? FP_NORMAL :
10142              (x == 0 ? FP_ZERO : FP_SUBNORMAL))).  */
10143
10144   tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10145                      build_real (type, dconst0));
10146   res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10147                      tmp, fp_zero, fp_subnormal);
10148
10149   sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10150   real_from_string (&r, buf);
10151   tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10152                      arg, build_real (type, r));
10153   res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10154
10155   if (HONOR_INFINITIES (mode))
10156     {
10157       real_inf (&r);
10158       tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10159                          build_real (type, r));
10160       res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10161                          fp_infinite, res);
10162     }
10163
10164   if (HONOR_NANS (mode))
10165     {
10166       tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10167       res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10168     }
10169
10170   return res;
10171 }
10172
10173 /* Fold a call to an unordered comparison function such as
10174    __builtin_isgreater().  FNDECL is the FUNCTION_DECL for the function
10175    being called and ARG0 and ARG1 are the arguments for the call.
10176    UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10177    the opposite of the desired result.  UNORDERED_CODE is used
10178    for modes that can hold NaNs and ORDERED_CODE is used for
10179    the rest.  */
10180
10181 static tree
10182 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10183                             enum tree_code unordered_code,
10184                             enum tree_code ordered_code)
10185 {
10186   tree type = TREE_TYPE (TREE_TYPE (fndecl));
10187   enum tree_code code;
10188   tree type0, type1;
10189   enum tree_code code0, code1;
10190   tree cmp_type = NULL_TREE;
10191
10192   type0 = TREE_TYPE (arg0);
10193   type1 = TREE_TYPE (arg1);
10194
10195   code0 = TREE_CODE (type0);
10196   code1 = TREE_CODE (type1);
10197
10198   if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10199     /* Choose the wider of two real types.  */
10200     cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10201       ? type0 : type1;
10202   else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10203     cmp_type = type0;
10204   else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10205     cmp_type = type1;
10206
10207   arg0 = fold_convert_loc (loc, cmp_type, arg0);
10208   arg1 = fold_convert_loc (loc, cmp_type, arg1);
10209
10210   if (unordered_code == UNORDERED_EXPR)
10211     {
10212       if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10213         return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10214       return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10215     }
10216
10217   code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10218                                                    : ordered_code;
10219   return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10220                       fold_build2_loc (loc, code, type, arg0, arg1));
10221 }
10222
10223 /* Fold a call to built-in function FNDECL with 0 arguments.
10224    IGNORE is true if the result of the function call is ignored.  This
10225    function returns NULL_TREE if no simplification was possible.  */
10226
10227 static tree
10228 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10229 {
10230   tree type = TREE_TYPE (TREE_TYPE (fndecl));
10231   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10232   switch (fcode)
10233     {
10234     CASE_FLT_FN (BUILT_IN_INF):
10235     case BUILT_IN_INFD32:
10236     case BUILT_IN_INFD64:
10237     case BUILT_IN_INFD128:
10238       return fold_builtin_inf (loc, type, true);
10239
10240     CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10241       return fold_builtin_inf (loc, type, false);
10242
10243     case BUILT_IN_CLASSIFY_TYPE:
10244       return fold_builtin_classify_type (NULL_TREE);
10245
10246     default:
10247       break;
10248     }
10249   return NULL_TREE;
10250 }
10251
10252 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10253    IGNORE is true if the result of the function call is ignored.  This
10254    function returns NULL_TREE if no simplification was possible.  */
10255
10256 static tree
10257 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10258 {
10259   tree type = TREE_TYPE (TREE_TYPE (fndecl));
10260   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10261   switch (fcode)
10262     {
10263     case BUILT_IN_CONSTANT_P:
10264       {
10265         tree val = fold_builtin_constant_p (arg0);
10266
10267         /* Gimplification will pull the CALL_EXPR for the builtin out of
10268            an if condition.  When not optimizing, we'll not CSE it back.
10269            To avoid link error types of regressions, return false now.  */
10270         if (!val && !optimize)
10271           val = integer_zero_node;
10272
10273         return val;
10274       }
10275
10276     case BUILT_IN_CLASSIFY_TYPE:
10277       return fold_builtin_classify_type (arg0);
10278
10279     case BUILT_IN_STRLEN:
10280       return fold_builtin_strlen (loc, type, arg0);
10281
10282     CASE_FLT_FN (BUILT_IN_FABS):
10283       return fold_builtin_fabs (loc, arg0, type);
10284
10285     case BUILT_IN_ABS:
10286     case BUILT_IN_LABS:
10287     case BUILT_IN_LLABS:
10288     case BUILT_IN_IMAXABS:
10289       return fold_builtin_abs (loc, arg0, type);
10290
10291     CASE_FLT_FN (BUILT_IN_CONJ):
10292       if (validate_arg (arg0, COMPLEX_TYPE)
10293         && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10294         return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10295     break;
10296
10297     CASE_FLT_FN (BUILT_IN_CREAL):
10298       if (validate_arg (arg0, COMPLEX_TYPE)
10299         && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10300         return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10301     break;
10302
10303     CASE_FLT_FN (BUILT_IN_CIMAG):
10304       if (validate_arg (arg0, COMPLEX_TYPE)
10305           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10306         return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10307     break;
10308
10309     CASE_FLT_FN (BUILT_IN_CCOS):
10310       return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
10311
10312     CASE_FLT_FN (BUILT_IN_CCOSH):
10313       return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
10314
10315     CASE_FLT_FN (BUILT_IN_CPROJ):
10316       return fold_builtin_cproj(loc, arg0, type);
10317
10318     CASE_FLT_FN (BUILT_IN_CSIN):
10319       if (validate_arg (arg0, COMPLEX_TYPE)
10320           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10321         return do_mpc_arg1 (arg0, type, mpc_sin);
10322     break;
10323
10324     CASE_FLT_FN (BUILT_IN_CSINH):
10325       if (validate_arg (arg0, COMPLEX_TYPE)
10326           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10327         return do_mpc_arg1 (arg0, type, mpc_sinh);
10328     break;
10329
10330     CASE_FLT_FN (BUILT_IN_CTAN):
10331       if (validate_arg (arg0, COMPLEX_TYPE)
10332           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10333         return do_mpc_arg1 (arg0, type, mpc_tan);
10334     break;
10335
10336     CASE_FLT_FN (BUILT_IN_CTANH):
10337       if (validate_arg (arg0, COMPLEX_TYPE)
10338           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10339         return do_mpc_arg1 (arg0, type, mpc_tanh);
10340     break;
10341
10342     CASE_FLT_FN (BUILT_IN_CLOG):
10343       if (validate_arg (arg0, COMPLEX_TYPE)
10344           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10345         return do_mpc_arg1 (arg0, type, mpc_log);
10346     break;
10347
10348     CASE_FLT_FN (BUILT_IN_CSQRT):
10349       if (validate_arg (arg0, COMPLEX_TYPE)
10350           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10351         return do_mpc_arg1 (arg0, type, mpc_sqrt);
10352     break;
10353
10354     CASE_FLT_FN (BUILT_IN_CASIN):
10355       if (validate_arg (arg0, COMPLEX_TYPE)
10356           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10357         return do_mpc_arg1 (arg0, type, mpc_asin);
10358     break;
10359
10360     CASE_FLT_FN (BUILT_IN_CACOS):
10361       if (validate_arg (arg0, COMPLEX_TYPE)
10362           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10363         return do_mpc_arg1 (arg0, type, mpc_acos);
10364     break;
10365
10366     CASE_FLT_FN (BUILT_IN_CATAN):
10367       if (validate_arg (arg0, COMPLEX_TYPE)
10368           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10369         return do_mpc_arg1 (arg0, type, mpc_atan);
10370     break;
10371
10372     CASE_FLT_FN (BUILT_IN_CASINH):
10373       if (validate_arg (arg0, COMPLEX_TYPE)
10374           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10375         return do_mpc_arg1 (arg0, type, mpc_asinh);
10376     break;
10377
10378     CASE_FLT_FN (BUILT_IN_CACOSH):
10379       if (validate_arg (arg0, COMPLEX_TYPE)
10380           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10381         return do_mpc_arg1 (arg0, type, mpc_acosh);
10382     break;
10383
10384     CASE_FLT_FN (BUILT_IN_CATANH):
10385       if (validate_arg (arg0, COMPLEX_TYPE)
10386           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10387         return do_mpc_arg1 (arg0, type, mpc_atanh);
10388     break;
10389
10390     CASE_FLT_FN (BUILT_IN_CABS):
10391       return fold_builtin_cabs (loc, arg0, type, fndecl);
10392
10393     CASE_FLT_FN (BUILT_IN_CARG):
10394       return fold_builtin_carg (loc, arg0, type);
10395
10396     CASE_FLT_FN (BUILT_IN_SQRT):
10397       return fold_builtin_sqrt (loc, arg0, type);
10398
10399     CASE_FLT_FN (BUILT_IN_CBRT):
10400       return fold_builtin_cbrt (loc, arg0, type);
10401
10402     CASE_FLT_FN (BUILT_IN_ASIN):
10403       if (validate_arg (arg0, REAL_TYPE))
10404         return do_mpfr_arg1 (arg0, type, mpfr_asin,
10405                              &dconstm1, &dconst1, true);
10406     break;
10407
10408     CASE_FLT_FN (BUILT_IN_ACOS):
10409       if (validate_arg (arg0, REAL_TYPE))
10410         return do_mpfr_arg1 (arg0, type, mpfr_acos,
10411                              &dconstm1, &dconst1, true);
10412     break;
10413
10414     CASE_FLT_FN (BUILT_IN_ATAN):
10415       if (validate_arg (arg0, REAL_TYPE))
10416         return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10417     break;
10418
10419     CASE_FLT_FN (BUILT_IN_ASINH):
10420       if (validate_arg (arg0, REAL_TYPE))
10421         return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10422     break;
10423
10424     CASE_FLT_FN (BUILT_IN_ACOSH):
10425       if (validate_arg (arg0, REAL_TYPE))
10426         return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10427                              &dconst1, NULL, true);
10428     break;
10429
10430     CASE_FLT_FN (BUILT_IN_ATANH):
10431       if (validate_arg (arg0, REAL_TYPE))
10432         return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10433                              &dconstm1, &dconst1, false);
10434     break;
10435
10436     CASE_FLT_FN (BUILT_IN_SIN):
10437       if (validate_arg (arg0, REAL_TYPE))
10438         return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10439     break;
10440
10441     CASE_FLT_FN (BUILT_IN_COS):
10442       return fold_builtin_cos (loc, arg0, type, fndecl);
10443
10444     CASE_FLT_FN (BUILT_IN_TAN):
10445       return fold_builtin_tan (arg0, type);
10446
10447     CASE_FLT_FN (BUILT_IN_CEXP):
10448       return fold_builtin_cexp (loc, arg0, type);
10449
10450     CASE_FLT_FN (BUILT_IN_CEXPI):
10451       if (validate_arg (arg0, REAL_TYPE))
10452         return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10453     break;
10454
10455     CASE_FLT_FN (BUILT_IN_SINH):
10456       if (validate_arg (arg0, REAL_TYPE))
10457         return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10458     break;
10459
10460     CASE_FLT_FN (BUILT_IN_COSH):
10461       return fold_builtin_cosh (loc, arg0, type, fndecl);
10462
10463     CASE_FLT_FN (BUILT_IN_TANH):
10464       if (validate_arg (arg0, REAL_TYPE))
10465         return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10466     break;
10467
10468     CASE_FLT_FN (BUILT_IN_ERF):
10469       if (validate_arg (arg0, REAL_TYPE))
10470         return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10471     break;
10472
10473     CASE_FLT_FN (BUILT_IN_ERFC):
10474       if (validate_arg (arg0, REAL_TYPE))
10475         return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10476     break;
10477
10478     CASE_FLT_FN (BUILT_IN_TGAMMA):
10479       if (validate_arg (arg0, REAL_TYPE))
10480         return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10481     break;
10482
10483     CASE_FLT_FN (BUILT_IN_EXP):
10484       return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10485
10486     CASE_FLT_FN (BUILT_IN_EXP2):
10487       return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10488
10489     CASE_FLT_FN (BUILT_IN_EXP10):
10490     CASE_FLT_FN (BUILT_IN_POW10):
10491       return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10492
10493     CASE_FLT_FN (BUILT_IN_EXPM1):
10494       if (validate_arg (arg0, REAL_TYPE))
10495         return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10496     break;
10497
10498     CASE_FLT_FN (BUILT_IN_LOG):
10499     return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10500
10501     CASE_FLT_FN (BUILT_IN_LOG2):
10502       return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10503
10504     CASE_FLT_FN (BUILT_IN_LOG10):
10505       return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10506
10507     CASE_FLT_FN (BUILT_IN_LOG1P):
10508       if (validate_arg (arg0, REAL_TYPE))
10509         return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10510                              &dconstm1, NULL, false);
10511     break;
10512
10513     CASE_FLT_FN (BUILT_IN_J0):
10514       if (validate_arg (arg0, REAL_TYPE))
10515         return do_mpfr_arg1 (arg0, type, mpfr_j0,
10516                              NULL, NULL, 0);
10517     break;
10518
10519     CASE_FLT_FN (BUILT_IN_J1):
10520       if (validate_arg (arg0, REAL_TYPE))
10521         return do_mpfr_arg1 (arg0, type, mpfr_j1,
10522                              NULL, NULL, 0);
10523     break;
10524
10525     CASE_FLT_FN (BUILT_IN_Y0):
10526       if (validate_arg (arg0, REAL_TYPE))
10527         return do_mpfr_arg1 (arg0, type, mpfr_y0,
10528                              &dconst0, NULL, false);
10529     break;
10530
10531     CASE_FLT_FN (BUILT_IN_Y1):
10532       if (validate_arg (arg0, REAL_TYPE))
10533         return do_mpfr_arg1 (arg0, type, mpfr_y1,
10534                              &dconst0, NULL, false);
10535     break;
10536
10537     CASE_FLT_FN (BUILT_IN_NAN):
10538     case BUILT_IN_NAND32:
10539     case BUILT_IN_NAND64:
10540     case BUILT_IN_NAND128:
10541       return fold_builtin_nan (arg0, type, true);
10542
10543     CASE_FLT_FN (BUILT_IN_NANS):
10544       return fold_builtin_nan (arg0, type, false);
10545
10546     CASE_FLT_FN (BUILT_IN_FLOOR):
10547       return fold_builtin_floor (loc, fndecl, arg0);
10548
10549     CASE_FLT_FN (BUILT_IN_CEIL):
10550       return fold_builtin_ceil (loc, fndecl, arg0);
10551
10552     CASE_FLT_FN (BUILT_IN_TRUNC):
10553       return fold_builtin_trunc (loc, fndecl, arg0);
10554
10555     CASE_FLT_FN (BUILT_IN_ROUND):
10556       return fold_builtin_round (loc, fndecl, arg0);
10557
10558     CASE_FLT_FN (BUILT_IN_NEARBYINT):
10559     CASE_FLT_FN (BUILT_IN_RINT):
10560       return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10561
10562     CASE_FLT_FN (BUILT_IN_ICEIL):
10563     CASE_FLT_FN (BUILT_IN_LCEIL):
10564     CASE_FLT_FN (BUILT_IN_LLCEIL):
10565     CASE_FLT_FN (BUILT_IN_LFLOOR):
10566     CASE_FLT_FN (BUILT_IN_IFLOOR):
10567     CASE_FLT_FN (BUILT_IN_LLFLOOR):
10568     CASE_FLT_FN (BUILT_IN_IROUND):
10569     CASE_FLT_FN (BUILT_IN_LROUND):
10570     CASE_FLT_FN (BUILT_IN_LLROUND):
10571       return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10572
10573     CASE_FLT_FN (BUILT_IN_IRINT):
10574     CASE_FLT_FN (BUILT_IN_LRINT):
10575     CASE_FLT_FN (BUILT_IN_LLRINT):
10576       return fold_fixed_mathfn (loc, fndecl, arg0);
10577
10578     case BUILT_IN_BSWAP32:
10579     case BUILT_IN_BSWAP64:
10580       return fold_builtin_bswap (fndecl, arg0);
10581
10582     CASE_INT_FN (BUILT_IN_FFS):
10583     CASE_INT_FN (BUILT_IN_CLZ):
10584     CASE_INT_FN (BUILT_IN_CTZ):
10585     CASE_INT_FN (BUILT_IN_CLRSB):
10586     CASE_INT_FN (BUILT_IN_POPCOUNT):
10587     CASE_INT_FN (BUILT_IN_PARITY):
10588       return fold_builtin_bitop (fndecl, arg0);
10589
10590     CASE_FLT_FN (BUILT_IN_SIGNBIT):
10591       return fold_builtin_signbit (loc, arg0, type);
10592
10593     CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10594       return fold_builtin_significand (loc, arg0, type);
10595
10596     CASE_FLT_FN (BUILT_IN_ILOGB):
10597     CASE_FLT_FN (BUILT_IN_LOGB):
10598       return fold_builtin_logb (loc, arg0, type);
10599
10600     case BUILT_IN_ISASCII:
10601       return fold_builtin_isascii (loc, arg0);
10602
10603     case BUILT_IN_TOASCII:
10604       return fold_builtin_toascii (loc, arg0);
10605
10606     case BUILT_IN_ISDIGIT:
10607       return fold_builtin_isdigit (loc, arg0);
10608
10609     CASE_FLT_FN (BUILT_IN_FINITE):
10610     case BUILT_IN_FINITED32:
10611     case BUILT_IN_FINITED64:
10612     case BUILT_IN_FINITED128:
10613     case BUILT_IN_ISFINITE:
10614       {
10615         tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10616         if (ret)
10617           return ret;
10618         return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10619       }
10620
10621     CASE_FLT_FN (BUILT_IN_ISINF):
10622     case BUILT_IN_ISINFD32:
10623     case BUILT_IN_ISINFD64:
10624     case BUILT_IN_ISINFD128:
10625       {
10626         tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10627         if (ret)
10628           return ret;
10629         return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10630       }
10631
10632     case BUILT_IN_ISNORMAL:
10633       return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10634
10635     case BUILT_IN_ISINF_SIGN:
10636       return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10637
10638     CASE_FLT_FN (BUILT_IN_ISNAN):
10639     case BUILT_IN_ISNAND32:
10640     case BUILT_IN_ISNAND64:
10641     case BUILT_IN_ISNAND128:
10642       return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10643
10644     case BUILT_IN_PRINTF:
10645     case BUILT_IN_PRINTF_UNLOCKED:
10646     case BUILT_IN_VPRINTF:
10647       return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10648
10649     case BUILT_IN_FREE:
10650       if (integer_zerop (arg0))
10651         return build_empty_stmt (loc);
10652       break;
10653
10654     default:
10655       break;
10656     }
10657
10658   return NULL_TREE;
10659
10660 }
10661
10662 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10663    IGNORE is true if the result of the function call is ignored.  This
10664    function returns NULL_TREE if no simplification was possible.  */
10665
10666 static tree
10667 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10668 {
10669   tree type = TREE_TYPE (TREE_TYPE (fndecl));
10670   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10671
10672   switch (fcode)
10673     {
10674     CASE_FLT_FN (BUILT_IN_JN):
10675       if (validate_arg (arg0, INTEGER_TYPE)
10676           && validate_arg (arg1, REAL_TYPE))
10677         return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10678     break;
10679
10680     CASE_FLT_FN (BUILT_IN_YN):
10681       if (validate_arg (arg0, INTEGER_TYPE)
10682           && validate_arg (arg1, REAL_TYPE))
10683         return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10684                                  &dconst0, false);
10685     break;
10686
10687     CASE_FLT_FN (BUILT_IN_DREM):
10688     CASE_FLT_FN (BUILT_IN_REMAINDER):
10689       if (validate_arg (arg0, REAL_TYPE)
10690           && validate_arg(arg1, REAL_TYPE))
10691         return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10692     break;
10693
10694     CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10695     CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10696       if (validate_arg (arg0, REAL_TYPE)
10697           && validate_arg(arg1, POINTER_TYPE))
10698         return do_mpfr_lgamma_r (arg0, arg1, type);
10699     break;
10700
10701     CASE_FLT_FN (BUILT_IN_ATAN2):
10702       if (validate_arg (arg0, REAL_TYPE)
10703           && validate_arg(arg1, REAL_TYPE))
10704         return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10705     break;
10706
10707     CASE_FLT_FN (BUILT_IN_FDIM):
10708       if (validate_arg (arg0, REAL_TYPE)
10709           && validate_arg(arg1, REAL_TYPE))
10710         return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10711     break;
10712
10713     CASE_FLT_FN (BUILT_IN_HYPOT):
10714       return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10715
10716     CASE_FLT_FN (BUILT_IN_CPOW):
10717       if (validate_arg (arg0, COMPLEX_TYPE)
10718           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10719           && validate_arg (arg1, COMPLEX_TYPE)
10720           && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10721         return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10722     break;
10723
10724     CASE_FLT_FN (BUILT_IN_LDEXP):
10725       return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10726     CASE_FLT_FN (BUILT_IN_SCALBN):
10727     CASE_FLT_FN (BUILT_IN_SCALBLN):
10728       return fold_builtin_load_exponent (loc, arg0, arg1,
10729                                          type, /*ldexp=*/false);
10730
10731     CASE_FLT_FN (BUILT_IN_FREXP):
10732       return fold_builtin_frexp (loc, arg0, arg1, type);
10733
10734     CASE_FLT_FN (BUILT_IN_MODF):
10735       return fold_builtin_modf (loc, arg0, arg1, type);
10736
10737     case BUILT_IN_BZERO:
10738       return fold_builtin_bzero (loc, arg0, arg1, ignore);
10739
10740     case BUILT_IN_FPUTS:
10741       return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10742
10743     case BUILT_IN_FPUTS_UNLOCKED:
10744       return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10745
10746     case BUILT_IN_STRSTR:
10747       return fold_builtin_strstr (loc, arg0, arg1, type);
10748
10749     case BUILT_IN_STRCAT:
10750       return fold_builtin_strcat (loc, arg0, arg1);
10751
10752     case BUILT_IN_STRSPN:
10753       return fold_builtin_strspn (loc, arg0, arg1);
10754
10755     case BUILT_IN_STRCSPN:
10756       return fold_builtin_strcspn (loc, arg0, arg1);
10757
10758     case BUILT_IN_STRCHR:
10759     case BUILT_IN_INDEX:
10760       return fold_builtin_strchr (loc, arg0, arg1, type);
10761
10762     case BUILT_IN_STRRCHR:
10763     case BUILT_IN_RINDEX:
10764       return fold_builtin_strrchr (loc, arg0, arg1, type);
10765
10766     case BUILT_IN_STRCPY:
10767       return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10768
10769     case BUILT_IN_STPCPY:
10770       if (ignore)
10771         {
10772           tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10773           if (!fn)
10774             break;
10775
10776           return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10777         }
10778       else
10779         return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10780       break;
10781
10782     case BUILT_IN_STRCMP:
10783       return fold_builtin_strcmp (loc, arg0, arg1);
10784
10785     case BUILT_IN_STRPBRK:
10786       return fold_builtin_strpbrk (loc, arg0, arg1, type);
10787
10788     case BUILT_IN_EXPECT:
10789       return fold_builtin_expect (loc, arg0, arg1);
10790
10791     CASE_FLT_FN (BUILT_IN_POW):
10792       return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10793
10794     CASE_FLT_FN (BUILT_IN_POWI):
10795       return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10796
10797     CASE_FLT_FN (BUILT_IN_COPYSIGN):
10798       return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10799
10800     CASE_FLT_FN (BUILT_IN_FMIN):
10801       return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10802
10803     CASE_FLT_FN (BUILT_IN_FMAX):
10804       return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10805
10806     case BUILT_IN_ISGREATER:
10807       return fold_builtin_unordered_cmp (loc, fndecl,
10808                                          arg0, arg1, UNLE_EXPR, LE_EXPR);
10809     case BUILT_IN_ISGREATEREQUAL:
10810       return fold_builtin_unordered_cmp (loc, fndecl,
10811                                          arg0, arg1, UNLT_EXPR, LT_EXPR);
10812     case BUILT_IN_ISLESS:
10813       return fold_builtin_unordered_cmp (loc, fndecl,
10814                                          arg0, arg1, UNGE_EXPR, GE_EXPR);
10815     case BUILT_IN_ISLESSEQUAL:
10816       return fold_builtin_unordered_cmp (loc, fndecl,
10817                                          arg0, arg1, UNGT_EXPR, GT_EXPR);
10818     case BUILT_IN_ISLESSGREATER:
10819       return fold_builtin_unordered_cmp (loc, fndecl,
10820                                          arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10821     case BUILT_IN_ISUNORDERED:
10822       return fold_builtin_unordered_cmp (loc, fndecl,
10823                                          arg0, arg1, UNORDERED_EXPR,
10824                                          NOP_EXPR);
10825
10826       /* We do the folding for va_start in the expander.  */
10827     case BUILT_IN_VA_START:
10828       break;
10829
10830     case BUILT_IN_SPRINTF:
10831       return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10832
10833     case BUILT_IN_OBJECT_SIZE:
10834       return fold_builtin_object_size (arg0, arg1);
10835
10836     case BUILT_IN_PRINTF:
10837     case BUILT_IN_PRINTF_UNLOCKED:
10838     case BUILT_IN_VPRINTF:
10839       return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10840
10841     case BUILT_IN_PRINTF_CHK:
10842     case BUILT_IN_VPRINTF_CHK:
10843       if (!validate_arg (arg0, INTEGER_TYPE)
10844           || TREE_SIDE_EFFECTS (arg0))
10845         return NULL_TREE;
10846       else
10847         return fold_builtin_printf (loc, fndecl,
10848                                     arg1, NULL_TREE, ignore, fcode);
10849     break;
10850
10851     case BUILT_IN_FPRINTF:
10852     case BUILT_IN_FPRINTF_UNLOCKED:
10853     case BUILT_IN_VFPRINTF:
10854       return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10855                                    ignore, fcode);
10856
10857     case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10858       return fold_builtin_atomic_always_lock_free (arg0, arg1);
10859
10860     case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10861       return fold_builtin_atomic_is_lock_free (arg0, arg1);
10862
10863     default:
10864       break;
10865     }
10866   return NULL_TREE;
10867 }
10868
10869 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10870    and ARG2.  IGNORE is true if the result of the function call is ignored.
10871    This function returns NULL_TREE if no simplification was possible.  */
10872
10873 static tree
10874 fold_builtin_3 (location_t loc, tree fndecl,
10875                 tree arg0, tree arg1, tree arg2, bool ignore)
10876 {
10877   tree type = TREE_TYPE (TREE_TYPE (fndecl));
10878   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10879   switch (fcode)
10880     {
10881
10882     CASE_FLT_FN (BUILT_IN_SINCOS):
10883       return fold_builtin_sincos (loc, arg0, arg1, arg2);
10884
10885     CASE_FLT_FN (BUILT_IN_FMA):
10886       return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10887     break;
10888
10889     CASE_FLT_FN (BUILT_IN_REMQUO):
10890       if (validate_arg (arg0, REAL_TYPE)
10891           && validate_arg(arg1, REAL_TYPE)
10892           && validate_arg(arg2, POINTER_TYPE))
10893         return do_mpfr_remquo (arg0, arg1, arg2);
10894     break;
10895
10896     case BUILT_IN_MEMSET:
10897       return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10898
10899     case BUILT_IN_BCOPY:
10900       return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10901                                      void_type_node, true, /*endp=*/3);
10902
10903     case BUILT_IN_MEMCPY:
10904       return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10905                                      type, ignore, /*endp=*/0);
10906
10907     case BUILT_IN_MEMPCPY:
10908       return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10909                                      type, ignore, /*endp=*/1);
10910
10911     case BUILT_IN_MEMMOVE:
10912       return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10913                                      type, ignore, /*endp=*/3);
10914
10915     case BUILT_IN_STRNCAT:
10916       return fold_builtin_strncat (loc, arg0, arg1, arg2);
10917
10918     case BUILT_IN_STRNCPY:
10919       return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10920
10921     case BUILT_IN_STRNCMP:
10922       return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10923
10924     case BUILT_IN_MEMCHR:
10925       return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10926
10927     case BUILT_IN_BCMP:
10928     case BUILT_IN_MEMCMP:
10929       return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10930
10931     case BUILT_IN_SPRINTF:
10932       return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10933
10934     case BUILT_IN_SNPRINTF:
10935       return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10936
10937     case BUILT_IN_STRCPY_CHK:
10938     case BUILT_IN_STPCPY_CHK:
10939       return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10940                                       ignore, fcode);
10941
10942     case BUILT_IN_STRCAT_CHK:
10943       return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10944
10945     case BUILT_IN_PRINTF_CHK:
10946     case BUILT_IN_VPRINTF_CHK:
10947       if (!validate_arg (arg0, INTEGER_TYPE)
10948           || TREE_SIDE_EFFECTS (arg0))
10949         return NULL_TREE;
10950       else
10951         return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10952     break;
10953
10954     case BUILT_IN_FPRINTF:
10955     case BUILT_IN_FPRINTF_UNLOCKED:
10956     case BUILT_IN_VFPRINTF:
10957       return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10958                                    ignore, fcode);
10959
10960     case BUILT_IN_FPRINTF_CHK:
10961     case BUILT_IN_VFPRINTF_CHK:
10962       if (!validate_arg (arg1, INTEGER_TYPE)
10963           || TREE_SIDE_EFFECTS (arg1))
10964         return NULL_TREE;
10965       else
10966         return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10967                                      ignore, fcode);
10968
10969     default:
10970       break;
10971     }
10972   return NULL_TREE;
10973 }
10974
10975 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10976    ARG2, and ARG3.  IGNORE is true if the result of the function call is
10977    ignored.  This function returns NULL_TREE if no simplification was
10978    possible.  */
10979
10980 static tree
10981 fold_builtin_4 (location_t loc, tree fndecl,
10982                 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10983 {
10984   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10985
10986   switch (fcode)
10987     {
10988     case BUILT_IN_MEMCPY_CHK:
10989     case BUILT_IN_MEMPCPY_CHK:
10990     case BUILT_IN_MEMMOVE_CHK:
10991     case BUILT_IN_MEMSET_CHK:
10992       return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10993                                       NULL_TREE, ignore,
10994                                       DECL_FUNCTION_CODE (fndecl));
10995
10996     case BUILT_IN_STRNCPY_CHK:
10997     case BUILT_IN_STPNCPY_CHK:
10998       return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
10999                                        ignore, fcode);
11000
11001     case BUILT_IN_STRNCAT_CHK:
11002       return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
11003
11004     case BUILT_IN_SNPRINTF:
11005       return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
11006
11007     case BUILT_IN_FPRINTF_CHK:
11008     case BUILT_IN_VFPRINTF_CHK:
11009       if (!validate_arg (arg1, INTEGER_TYPE)
11010           || TREE_SIDE_EFFECTS (arg1))
11011         return NULL_TREE;
11012       else
11013         return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
11014                                      ignore, fcode);
11015     break;
11016
11017     default:
11018       break;
11019     }
11020   return NULL_TREE;
11021 }
11022
11023 /* Fold a call to built-in function FNDECL.  ARGS is an array of NARGS
11024     arguments, where NARGS <= 4.  IGNORE is true if the result of the
11025     function call is ignored.  This function returns NULL_TREE if no
11026     simplification was possible.  Note that this only folds builtins with
11027     fixed argument patterns.  Foldings that do varargs-to-varargs
11028     transformations, or that match calls with more than 4 arguments,
11029     need to be handled with fold_builtin_varargs instead.  */
11030
11031 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11032
11033 static tree
11034 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11035 {
11036   tree ret = NULL_TREE;
11037
11038   switch (nargs)
11039     {
11040     case 0:
11041       ret = fold_builtin_0 (loc, fndecl, ignore);
11042       break;
11043     case 1:
11044       ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11045       break;
11046     case 2:
11047       ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11048       break;
11049     case 3:
11050       ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11051       break;
11052     case 4:
11053       ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11054                             ignore);
11055       break;
11056     default:
11057       break;
11058     }
11059   if (ret)
11060     {
11061       ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11062       SET_EXPR_LOCATION (ret, loc);
11063       TREE_NO_WARNING (ret) = 1;
11064       return ret;
11065     }
11066   return NULL_TREE;
11067 }
11068
11069 /* Builtins with folding operations that operate on "..." arguments
11070    need special handling; we need to store the arguments in a convenient
11071    data structure before attempting any folding.  Fortunately there are
11072    only a few builtins that fall into this category.  FNDECL is the
11073    function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11074    result of the function call is ignored.  */
11075
11076 static tree
11077 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11078                       bool ignore ATTRIBUTE_UNUSED)
11079 {
11080   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11081   tree ret = NULL_TREE;
11082
11083   switch (fcode)
11084     {
11085     case BUILT_IN_SPRINTF_CHK:
11086     case BUILT_IN_VSPRINTF_CHK:
11087       ret = fold_builtin_sprintf_chk (loc, exp, fcode);
11088       break;
11089
11090     case BUILT_IN_SNPRINTF_CHK:
11091     case BUILT_IN_VSNPRINTF_CHK:
11092       ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
11093       break;
11094
11095     case BUILT_IN_FPCLASSIFY:
11096       ret = fold_builtin_fpclassify (loc, exp);
11097       break;
11098
11099     default:
11100       break;
11101     }
11102   if (ret)
11103     {
11104       ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11105       SET_EXPR_LOCATION (ret, loc);
11106       TREE_NO_WARNING (ret) = 1;
11107       return ret;
11108     }
11109   return NULL_TREE;
11110 }
11111
11112 /* Return true if FNDECL shouldn't be folded right now.
11113    If a built-in function has an inline attribute always_inline
11114    wrapper, defer folding it after always_inline functions have
11115    been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11116    might not be performed.  */
11117
11118 bool
11119 avoid_folding_inline_builtin (tree fndecl)
11120 {
11121   return (DECL_DECLARED_INLINE_P (fndecl)
11122           && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11123           && cfun
11124           && !cfun->always_inline_functions_inlined
11125           && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11126 }
11127
11128 /* A wrapper function for builtin folding that prevents warnings for
11129    "statement without effect" and the like, caused by removing the
11130    call node earlier than the warning is generated.  */
11131
11132 tree
11133 fold_call_expr (location_t loc, tree exp, bool ignore)
11134 {
11135   tree ret = NULL_TREE;
11136   tree fndecl = get_callee_fndecl (exp);
11137   if (fndecl
11138       && TREE_CODE (fndecl) == FUNCTION_DECL
11139       && DECL_BUILT_IN (fndecl)
11140       /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11141          yet.  Defer folding until we see all the arguments
11142          (after inlining).  */
11143       && !CALL_EXPR_VA_ARG_PACK (exp))
11144     {
11145       int nargs = call_expr_nargs (exp);
11146
11147       /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11148          instead last argument is __builtin_va_arg_pack ().  Defer folding
11149          even in that case, until arguments are finalized.  */
11150       if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11151         {
11152           tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11153           if (fndecl2
11154               && TREE_CODE (fndecl2) == FUNCTION_DECL
11155               && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11156               && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11157             return NULL_TREE;
11158         }
11159
11160       if (avoid_folding_inline_builtin (fndecl))
11161         return NULL_TREE;
11162
11163       if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11164         return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11165                                      CALL_EXPR_ARGP (exp), ignore);
11166       else
11167         {
11168           if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11169             {
11170               tree *args = CALL_EXPR_ARGP (exp);
11171               ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11172             }
11173           if (!ret)
11174             ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11175           if (ret)
11176             return ret;
11177         }
11178     }
11179   return NULL_TREE;
11180 }
11181
11182 /* Conveniently construct a function call expression.  FNDECL names the
11183    function to be called and N arguments are passed in the array
11184    ARGARRAY.  */
11185
11186 tree
11187 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11188 {
11189   tree fntype = TREE_TYPE (fndecl);
11190   tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11191  
11192   return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11193 }
11194
11195 /* Conveniently construct a function call expression.  FNDECL names the
11196    function to be called and the arguments are passed in the vector
11197    VEC.  */
11198
11199 tree
11200 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
11201 {
11202   return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
11203                                     VEC_address (tree, vec));
11204 }
11205
11206
11207 /* Conveniently construct a function call expression.  FNDECL names the
11208    function to be called, N is the number of arguments, and the "..."
11209    parameters are the argument expressions.  */
11210
11211 tree
11212 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11213 {
11214   va_list ap;
11215   tree *argarray = XALLOCAVEC (tree, n);
11216   int i;
11217
11218   va_start (ap, n);
11219   for (i = 0; i < n; i++)
11220     argarray[i] = va_arg (ap, tree);
11221   va_end (ap);
11222   return build_call_expr_loc_array (loc, fndecl, n, argarray);
11223 }
11224
11225 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...).  Duplicated because
11226    varargs macros aren't supported by all bootstrap compilers.  */
11227
11228 tree
11229 build_call_expr (tree fndecl, int n, ...)
11230 {
11231   va_list ap;
11232   tree *argarray = XALLOCAVEC (tree, n);
11233   int i;
11234
11235   va_start (ap, n);
11236   for (i = 0; i < n; i++)
11237     argarray[i] = va_arg (ap, tree);
11238   va_end (ap);
11239   return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11240 }
11241
11242 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11243    N arguments are passed in the array ARGARRAY.  */
11244
11245 tree
11246 fold_builtin_call_array (location_t loc, tree type,
11247                          tree fn,
11248                          int n,
11249                          tree *argarray)
11250 {
11251   tree ret = NULL_TREE;
11252    tree exp;
11253
11254   if (TREE_CODE (fn) == ADDR_EXPR)
11255   {
11256     tree fndecl = TREE_OPERAND (fn, 0);
11257     if (TREE_CODE (fndecl) == FUNCTION_DECL
11258         && DECL_BUILT_IN (fndecl))
11259       {
11260         /* If last argument is __builtin_va_arg_pack (), arguments to this
11261            function are not finalized yet.  Defer folding until they are.  */
11262         if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11263           {
11264             tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11265             if (fndecl2
11266                 && TREE_CODE (fndecl2) == FUNCTION_DECL
11267                 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11268                 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11269               return build_call_array_loc (loc, type, fn, n, argarray);
11270           }
11271         if (avoid_folding_inline_builtin (fndecl))
11272           return build_call_array_loc (loc, type, fn, n, argarray);
11273         if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11274           {
11275             ret = targetm.fold_builtin (fndecl, n, argarray, false);
11276             if (ret)
11277               return ret;
11278
11279             return build_call_array_loc (loc, type, fn, n, argarray);
11280           }
11281         else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11282           {
11283             /* First try the transformations that don't require consing up
11284                an exp.  */
11285             ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11286             if (ret)
11287               return ret;
11288           }
11289
11290         /* If we got this far, we need to build an exp.  */
11291         exp = build_call_array_loc (loc, type, fn, n, argarray);
11292         ret = fold_builtin_varargs (loc, fndecl, exp, false);
11293         return ret ? ret : exp;
11294       }
11295   }
11296
11297   return build_call_array_loc (loc, type, fn, n, argarray);
11298 }
11299
11300 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11301    list ARGS along with N new arguments in NEWARGS.  SKIP is the number
11302    of arguments in ARGS to be omitted.  OLDNARGS is the number of
11303    elements in ARGS.  */
11304
11305 static tree
11306 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11307                           int skip, tree fndecl, int n, va_list newargs)
11308 {
11309   int nargs = oldnargs - skip + n;
11310   tree *buffer;
11311
11312   if (n > 0)
11313     {
11314       int i, j;
11315
11316       buffer = XALLOCAVEC (tree, nargs);
11317       for (i = 0; i < n; i++)
11318         buffer[i] = va_arg (newargs, tree);
11319       for (j = skip; j < oldnargs; j++, i++)
11320         buffer[i] = args[j];
11321     }
11322   else
11323     buffer = args + skip;
11324
11325   return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11326 }
11327
11328 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11329    list ARGS along with N new arguments specified as the "..."
11330    parameters.  SKIP is the number of arguments in ARGS to be omitted.
11331    OLDNARGS is the number of elements in ARGS.  */
11332
11333 static tree
11334 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11335                          int skip, tree fndecl, int n, ...)
11336 {
11337   va_list ap;
11338   tree t;
11339
11340   va_start (ap, n);
11341   t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11342   va_end (ap);
11343
11344   return t;
11345 }
11346
11347 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11348    along with N new arguments specified as the "..." parameters.  SKIP
11349    is the number of arguments in EXP to be omitted.  This function is used
11350    to do varargs-to-varargs transformations.  */
11351
11352 static tree
11353 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11354 {
11355   va_list ap;
11356   tree t;
11357
11358   va_start (ap, n);
11359   t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11360                                 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11361   va_end (ap);
11362
11363   return t;
11364 }
11365
11366 /* Validate a single argument ARG against a tree code CODE representing
11367    a type.  */
11368
11369 static bool
11370 validate_arg (const_tree arg, enum tree_code code)
11371 {
11372   if (!arg)
11373     return false;
11374   else if (code == POINTER_TYPE)
11375     return POINTER_TYPE_P (TREE_TYPE (arg));
11376   else if (code == INTEGER_TYPE)
11377     return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11378   return code == TREE_CODE (TREE_TYPE (arg));
11379 }
11380
11381 /* This function validates the types of a function call argument list
11382    against a specified list of tree_codes.  If the last specifier is a 0,
11383    that represents an ellipses, otherwise the last specifier must be a
11384    VOID_TYPE.
11385
11386    This is the GIMPLE version of validate_arglist.  Eventually we want to
11387    completely convert builtins.c to work from GIMPLEs and the tree based
11388    validate_arglist will then be removed.  */
11389
11390 bool
11391 validate_gimple_arglist (const_gimple call, ...)
11392 {
11393   enum tree_code code;
11394   bool res = 0;
11395   va_list ap;
11396   const_tree arg;
11397   size_t i;
11398
11399   va_start (ap, call);
11400   i = 0;
11401
11402   do
11403     {
11404       code = (enum tree_code) va_arg (ap, int);
11405       switch (code)
11406         {
11407         case 0:
11408           /* This signifies an ellipses, any further arguments are all ok.  */
11409           res = true;
11410           goto end;
11411         case VOID_TYPE:
11412           /* This signifies an endlink, if no arguments remain, return
11413              true, otherwise return false.  */
11414           res = (i == gimple_call_num_args (call));
11415           goto end;
11416         default:
11417           /* If no parameters remain or the parameter's code does not
11418              match the specified code, return false.  Otherwise continue
11419              checking any remaining arguments.  */
11420           arg = gimple_call_arg (call, i++);
11421           if (!validate_arg (arg, code))
11422             goto end;
11423           break;
11424         }
11425     }
11426   while (1);
11427
11428   /* We need gotos here since we can only have one VA_CLOSE in a
11429      function.  */
11430  end: ;
11431   va_end (ap);
11432
11433   return res;
11434 }
11435
11436 /* This function validates the types of a function call argument list
11437    against a specified list of tree_codes.  If the last specifier is a 0,
11438    that represents an ellipses, otherwise the last specifier must be a
11439    VOID_TYPE.  */
11440
11441 bool
11442 validate_arglist (const_tree callexpr, ...)
11443 {
11444   enum tree_code code;
11445   bool res = 0;
11446   va_list ap;
11447   const_call_expr_arg_iterator iter;
11448   const_tree arg;
11449
11450   va_start (ap, callexpr);
11451   init_const_call_expr_arg_iterator (callexpr, &iter);
11452
11453   do
11454     {
11455       code = (enum tree_code) va_arg (ap, int);
11456       switch (code)
11457         {
11458         case 0:
11459           /* This signifies an ellipses, any further arguments are all ok.  */
11460           res = true;
11461           goto end;
11462         case VOID_TYPE:
11463           /* This signifies an endlink, if no arguments remain, return
11464              true, otherwise return false.  */
11465           res = !more_const_call_expr_args_p (&iter);
11466           goto end;
11467         default:
11468           /* If no parameters remain or the parameter's code does not
11469              match the specified code, return false.  Otherwise continue
11470              checking any remaining arguments.  */
11471           arg = next_const_call_expr_arg (&iter);
11472           if (!validate_arg (arg, code))
11473             goto end;
11474           break;
11475         }
11476     }
11477   while (1);
11478
11479   /* We need gotos here since we can only have one VA_CLOSE in a
11480      function.  */
11481  end: ;
11482   va_end (ap);
11483
11484   return res;
11485 }
11486
11487 /* Default target-specific builtin expander that does nothing.  */
11488
11489 rtx
11490 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11491                         rtx target ATTRIBUTE_UNUSED,
11492                         rtx subtarget ATTRIBUTE_UNUSED,
11493                         enum machine_mode mode ATTRIBUTE_UNUSED,
11494                         int ignore ATTRIBUTE_UNUSED)
11495 {
11496   return NULL_RTX;
11497 }
11498
11499 /* Returns true is EXP represents data that would potentially reside
11500    in a readonly section.  */
11501
11502 static bool
11503 readonly_data_expr (tree exp)
11504 {
11505   STRIP_NOPS (exp);
11506
11507   if (TREE_CODE (exp) != ADDR_EXPR)
11508     return false;
11509
11510   exp = get_base_address (TREE_OPERAND (exp, 0));
11511   if (!exp)
11512     return false;
11513
11514   /* Make sure we call decl_readonly_section only for trees it
11515      can handle (since it returns true for everything it doesn't
11516      understand).  */
11517   if (TREE_CODE (exp) == STRING_CST
11518       || TREE_CODE (exp) == CONSTRUCTOR
11519       || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11520     return decl_readonly_section (exp, 0);
11521   else
11522     return false;
11523 }
11524
11525 /* Simplify a call to the strstr builtin.  S1 and S2 are the arguments
11526    to the call, and TYPE is its return type.
11527
11528    Return NULL_TREE if no simplification was possible, otherwise return the
11529    simplified form of the call as a tree.
11530
11531    The simplified form may be a constant or other expression which
11532    computes the same value, but in a more efficient manner (including
11533    calls to other builtin functions).
11534
11535    The call may contain arguments which need to be evaluated, but
11536    which are not useful to determine the result of the call.  In
11537    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11538    COMPOUND_EXPR will be an argument which must be evaluated.
11539    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11540    COMPOUND_EXPR in the chain will contain the tree for the simplified
11541    form of the builtin function call.  */
11542
11543 static tree
11544 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11545 {
11546   if (!validate_arg (s1, POINTER_TYPE)
11547       || !validate_arg (s2, POINTER_TYPE))
11548     return NULL_TREE;
11549   else
11550     {
11551       tree fn;
11552       const char *p1, *p2;
11553
11554       p2 = c_getstr (s2);
11555       if (p2 == NULL)
11556         return NULL_TREE;
11557
11558       p1 = c_getstr (s1);
11559       if (p1 != NULL)
11560         {
11561           const char *r = strstr (p1, p2);
11562           tree tem;
11563
11564           if (r == NULL)
11565             return build_int_cst (TREE_TYPE (s1), 0);
11566
11567           /* Return an offset into the constant string argument.  */
11568           tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11569           return fold_convert_loc (loc, type, tem);
11570         }
11571
11572       /* The argument is const char *, and the result is char *, so we need
11573          a type conversion here to avoid a warning.  */
11574       if (p2[0] == '\0')
11575         return fold_convert_loc (loc, type, s1);
11576
11577       if (p2[1] != '\0')
11578         return NULL_TREE;
11579
11580       fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11581       if (!fn)
11582         return NULL_TREE;
11583
11584       /* New argument list transforming strstr(s1, s2) to
11585          strchr(s1, s2[0]).  */
11586       return build_call_expr_loc (loc, fn, 2, s1,
11587                                   build_int_cst (integer_type_node, p2[0]));
11588     }
11589 }
11590
11591 /* Simplify a call to the strchr builtin.  S1 and S2 are the arguments to
11592    the call, and TYPE is its return type.
11593
11594    Return NULL_TREE if no simplification was possible, otherwise return the
11595    simplified form of the call as a tree.
11596
11597    The simplified form may be a constant or other expression which
11598    computes the same value, but in a more efficient manner (including
11599    calls to other builtin functions).
11600
11601    The call may contain arguments which need to be evaluated, but
11602    which are not useful to determine the result of the call.  In
11603    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11604    COMPOUND_EXPR will be an argument which must be evaluated.
11605    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11606    COMPOUND_EXPR in the chain will contain the tree for the simplified
11607    form of the builtin function call.  */
11608
11609 static tree
11610 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11611 {
11612   if (!validate_arg (s1, POINTER_TYPE)
11613       || !validate_arg (s2, INTEGER_TYPE))
11614     return NULL_TREE;
11615   else
11616     {
11617       const char *p1;
11618
11619       if (TREE_CODE (s2) != INTEGER_CST)
11620         return NULL_TREE;
11621
11622       p1 = c_getstr (s1);
11623       if (p1 != NULL)
11624         {
11625           char c;
11626           const char *r;
11627           tree tem;
11628
11629           if (target_char_cast (s2, &c))
11630             return NULL_TREE;
11631
11632           r = strchr (p1, c);
11633
11634           if (r == NULL)
11635             return build_int_cst (TREE_TYPE (s1), 0);
11636
11637           /* Return an offset into the constant string argument.  */
11638           tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11639           return fold_convert_loc (loc, type, tem);
11640         }
11641       return NULL_TREE;
11642     }
11643 }
11644
11645 /* Simplify a call to the strrchr builtin.  S1 and S2 are the arguments to
11646    the call, and TYPE is its return type.
11647
11648    Return NULL_TREE if no simplification was possible, otherwise return the
11649    simplified form of the call as a tree.
11650
11651    The simplified form may be a constant or other expression which
11652    computes the same value, but in a more efficient manner (including
11653    calls to other builtin functions).
11654
11655    The call may contain arguments which need to be evaluated, but
11656    which are not useful to determine the result of the call.  In
11657    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11658    COMPOUND_EXPR will be an argument which must be evaluated.
11659    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11660    COMPOUND_EXPR in the chain will contain the tree for the simplified
11661    form of the builtin function call.  */
11662
11663 static tree
11664 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11665 {
11666   if (!validate_arg (s1, POINTER_TYPE)
11667       || !validate_arg (s2, INTEGER_TYPE))
11668     return NULL_TREE;
11669   else
11670     {
11671       tree fn;
11672       const char *p1;
11673
11674       if (TREE_CODE (s2) != INTEGER_CST)
11675         return NULL_TREE;
11676
11677       p1 = c_getstr (s1);
11678       if (p1 != NULL)
11679         {
11680           char c;
11681           const char *r;
11682           tree tem;
11683
11684           if (target_char_cast (s2, &c))
11685             return NULL_TREE;
11686
11687           r = strrchr (p1, c);
11688
11689           if (r == NULL)
11690             return build_int_cst (TREE_TYPE (s1), 0);
11691
11692           /* Return an offset into the constant string argument.  */
11693           tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11694           return fold_convert_loc (loc, type, tem);
11695         }
11696
11697       if (! integer_zerop (s2))
11698         return NULL_TREE;
11699
11700       fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11701       if (!fn)
11702         return NULL_TREE;
11703
11704       /* Transform strrchr(s1, '\0') to strchr(s1, '\0').  */
11705       return build_call_expr_loc (loc, fn, 2, s1, s2);
11706     }
11707 }
11708
11709 /* Simplify a call to the strpbrk builtin.  S1 and S2 are the arguments
11710    to the call, and TYPE is its return type.
11711
11712    Return NULL_TREE if no simplification was possible, otherwise return the
11713    simplified form of the call as a tree.
11714
11715    The simplified form may be a constant or other expression which
11716    computes the same value, but in a more efficient manner (including
11717    calls to other builtin functions).
11718
11719    The call may contain arguments which need to be evaluated, but
11720    which are not useful to determine the result of the call.  In
11721    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11722    COMPOUND_EXPR will be an argument which must be evaluated.
11723    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11724    COMPOUND_EXPR in the chain will contain the tree for the simplified
11725    form of the builtin function call.  */
11726
11727 static tree
11728 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11729 {
11730   if (!validate_arg (s1, POINTER_TYPE)
11731       || !validate_arg (s2, POINTER_TYPE))
11732     return NULL_TREE;
11733   else
11734     {
11735       tree fn;
11736       const char *p1, *p2;
11737
11738       p2 = c_getstr (s2);
11739       if (p2 == NULL)
11740         return NULL_TREE;
11741
11742       p1 = c_getstr (s1);
11743       if (p1 != NULL)
11744         {
11745           const char *r = strpbrk (p1, p2);
11746           tree tem;
11747
11748           if (r == NULL)
11749             return build_int_cst (TREE_TYPE (s1), 0);
11750
11751           /* Return an offset into the constant string argument.  */
11752           tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11753           return fold_convert_loc (loc, type, tem);
11754         }
11755
11756       if (p2[0] == '\0')
11757         /* strpbrk(x, "") == NULL.
11758            Evaluate and ignore s1 in case it had side-effects.  */
11759         return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11760
11761       if (p2[1] != '\0')
11762         return NULL_TREE;  /* Really call strpbrk.  */
11763
11764       fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11765       if (!fn)
11766         return NULL_TREE;
11767
11768       /* New argument list transforming strpbrk(s1, s2) to
11769          strchr(s1, s2[0]).  */
11770       return build_call_expr_loc (loc, fn, 2, s1,
11771                                   build_int_cst (integer_type_node, p2[0]));
11772     }
11773 }
11774
11775 /* Simplify a call to the strcat builtin.  DST and SRC are the arguments
11776    to the call.
11777
11778    Return NULL_TREE if no simplification was possible, otherwise return the
11779    simplified form of the call as a tree.
11780
11781    The simplified form may be a constant or other expression which
11782    computes the same value, but in a more efficient manner (including
11783    calls to other builtin functions).
11784
11785    The call may contain arguments which need to be evaluated, but
11786    which are not useful to determine the result of the call.  In
11787    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11788    COMPOUND_EXPR will be an argument which must be evaluated.
11789    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11790    COMPOUND_EXPR in the chain will contain the tree for the simplified
11791    form of the builtin function call.  */
11792
11793 static tree
11794 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11795 {
11796   if (!validate_arg (dst, POINTER_TYPE)
11797       || !validate_arg (src, POINTER_TYPE))
11798     return NULL_TREE;
11799   else
11800     {
11801       const char *p = c_getstr (src);
11802
11803       /* If the string length is zero, return the dst parameter.  */
11804       if (p && *p == '\0')
11805         return dst;
11806
11807       if (optimize_insn_for_speed_p ())
11808         {
11809           /* See if we can store by pieces into (dst + strlen(dst)).  */
11810           tree newdst, call;
11811           tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11812           tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11813
11814           if (!strlen_fn || !strcpy_fn)
11815             return NULL_TREE;
11816
11817           /* If we don't have a movstr we don't want to emit an strcpy
11818              call.  We have to do that if the length of the source string
11819              isn't computable (in that case we can use memcpy probably
11820              later expanding to a sequence of mov instructions).  If we
11821              have movstr instructions we can emit strcpy calls.  */
11822           if (!HAVE_movstr)
11823             {
11824               tree len = c_strlen (src, 1);
11825               if (! len || TREE_SIDE_EFFECTS (len))
11826                 return NULL_TREE;
11827             }
11828
11829           /* Stabilize the argument list.  */
11830           dst = builtin_save_expr (dst);
11831
11832           /* Create strlen (dst).  */
11833           newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11834           /* Create (dst p+ strlen (dst)).  */
11835
11836           newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11837           newdst = builtin_save_expr (newdst);
11838
11839           call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11840           return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11841         }
11842       return NULL_TREE;
11843     }
11844 }
11845
11846 /* Simplify a call to the strncat builtin.  DST, SRC, and LEN are the
11847    arguments to the call.
11848
11849    Return NULL_TREE if no simplification was possible, otherwise return the
11850    simplified form of the call as a tree.
11851
11852    The simplified form may be a constant or other expression which
11853    computes the same value, but in a more efficient manner (including
11854    calls to other builtin functions).
11855
11856    The call may contain arguments which need to be evaluated, but
11857    which are not useful to determine the result of the call.  In
11858    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11859    COMPOUND_EXPR will be an argument which must be evaluated.
11860    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11861    COMPOUND_EXPR in the chain will contain the tree for the simplified
11862    form of the builtin function call.  */
11863
11864 static tree
11865 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11866 {
11867   if (!validate_arg (dst, POINTER_TYPE)
11868       || !validate_arg (src, POINTER_TYPE)
11869       || !validate_arg (len, INTEGER_TYPE))
11870     return NULL_TREE;
11871   else
11872     {
11873       const char *p = c_getstr (src);
11874
11875       /* If the requested length is zero, or the src parameter string
11876          length is zero, return the dst parameter.  */
11877       if (integer_zerop (len) || (p && *p == '\0'))
11878         return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11879
11880       /* If the requested len is greater than or equal to the string
11881          length, call strcat.  */
11882       if (TREE_CODE (len) == INTEGER_CST && p
11883           && compare_tree_int (len, strlen (p)) >= 0)
11884         {
11885           tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11886
11887           /* If the replacement _DECL isn't initialized, don't do the
11888              transformation.  */
11889           if (!fn)
11890             return NULL_TREE;
11891
11892           return build_call_expr_loc (loc, fn, 2, dst, src);
11893         }
11894       return NULL_TREE;
11895     }
11896 }
11897
11898 /* Simplify a call to the strspn builtin.  S1 and S2 are the arguments
11899    to the call.
11900
11901    Return NULL_TREE if no simplification was possible, otherwise return the
11902    simplified form of the call as a tree.
11903
11904    The simplified form may be a constant or other expression which
11905    computes the same value, but in a more efficient manner (including
11906    calls to other builtin functions).
11907
11908    The call may contain arguments which need to be evaluated, but
11909    which are not useful to determine the result of the call.  In
11910    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11911    COMPOUND_EXPR will be an argument which must be evaluated.
11912    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11913    COMPOUND_EXPR in the chain will contain the tree for the simplified
11914    form of the builtin function call.  */
11915
11916 static tree
11917 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11918 {
11919   if (!validate_arg (s1, POINTER_TYPE)
11920       || !validate_arg (s2, POINTER_TYPE))
11921     return NULL_TREE;
11922   else
11923     {
11924       const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11925
11926       /* If both arguments are constants, evaluate at compile-time.  */
11927       if (p1 && p2)
11928         {
11929           const size_t r = strspn (p1, p2);
11930           return size_int (r);
11931         }
11932
11933       /* If either argument is "", return NULL_TREE.  */
11934       if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11935         /* Evaluate and ignore both arguments in case either one has
11936            side-effects.  */
11937         return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11938                                   s1, s2);
11939       return NULL_TREE;
11940     }
11941 }
11942
11943 /* Simplify a call to the strcspn builtin.  S1 and S2 are the arguments
11944    to the call.
11945
11946    Return NULL_TREE if no simplification was possible, otherwise return the
11947    simplified form of the call as a tree.
11948
11949    The simplified form may be a constant or other expression which
11950    computes the same value, but in a more efficient manner (including
11951    calls to other builtin functions).
11952
11953    The call may contain arguments which need to be evaluated, but
11954    which are not useful to determine the result of the call.  In
11955    this case we return a chain of COMPOUND_EXPRs.  The LHS of each
11956    COMPOUND_EXPR will be an argument which must be evaluated.
11957    COMPOUND_EXPRs are chained through their RHS.  The RHS of the last
11958    COMPOUND_EXPR in the chain will contain the tree for the simplified
11959    form of the builtin function call.  */
11960
11961 static tree
11962 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11963 {
11964   if (!validate_arg (s1, POINTER_TYPE)
11965       || !validate_arg (s2, POINTER_TYPE))
11966     return NULL_TREE;
11967   else
11968     {
11969       const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11970
11971       /* If both arguments are constants, evaluate at compile-time.  */
11972       if (p1 && p2)
11973         {
11974           const size_t r = strcspn (p1, p2);
11975           return size_int (r);
11976         }
11977
11978       /* If the first argument is "", return NULL_TREE.  */
11979       if (p1 && *p1 == '\0')
11980         {
11981           /* Evaluate and ignore argument s2 in case it has
11982              side-effects.  */
11983           return omit_one_operand_loc (loc, size_type_node,
11984                                    size_zero_node, s2);
11985         }
11986
11987       /* If the second argument is "", return __builtin_strlen(s1).  */
11988       if (p2 && *p2 == '\0')
11989         {
11990           tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11991
11992           /* If the replacement _DECL isn't initialized, don't do the
11993              transformation.  */
11994           if (!fn)
11995             return NULL_TREE;
11996
11997           return build_call_expr_loc (loc, fn, 1, s1);
11998         }
11999       return NULL_TREE;
12000     }
12001 }
12002
12003 /* Fold a call to the fputs builtin.  ARG0 and ARG1 are the arguments
12004    to the call.  IGNORE is true if the value returned
12005    by the builtin will be ignored.  UNLOCKED is true is true if this
12006    actually a call to fputs_unlocked.  If LEN in non-NULL, it represents
12007    the known length of the string.  Return NULL_TREE if no simplification
12008    was possible.  */
12009
12010 tree
12011 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
12012                     bool ignore, bool unlocked, tree len)
12013 {
12014   /* If we're using an unlocked function, assume the other unlocked
12015      functions exist explicitly.  */
12016   tree const fn_fputc = (unlocked
12017                          ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
12018                          : builtin_decl_implicit (BUILT_IN_FPUTC));
12019   tree const fn_fwrite = (unlocked
12020                           ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
12021                           : builtin_decl_implicit (BUILT_IN_FWRITE));
12022
12023   /* If the return value is used, don't do the transformation.  */
12024   if (!ignore)
12025     return NULL_TREE;
12026
12027   /* Verify the arguments in the original call.  */
12028   if (!validate_arg (arg0, POINTER_TYPE)
12029       || !validate_arg (arg1, POINTER_TYPE))
12030     return NULL_TREE;
12031
12032   if (! len)
12033     len = c_strlen (arg0, 0);
12034
12035   /* Get the length of the string passed to fputs.  If the length
12036      can't be determined, punt.  */
12037   if (!len
12038       || TREE_CODE (len) != INTEGER_CST)
12039     return NULL_TREE;
12040
12041   switch (compare_tree_int (len, 1))
12042     {
12043     case -1: /* length is 0, delete the call entirely .  */
12044       return omit_one_operand_loc (loc, integer_type_node,
12045                                integer_zero_node, arg1);;
12046
12047     case 0: /* length is 1, call fputc.  */
12048       {
12049         const char *p = c_getstr (arg0);
12050
12051         if (p != NULL)
12052           {
12053             if (fn_fputc)
12054               return build_call_expr_loc (loc, fn_fputc, 2,
12055                                           build_int_cst
12056                                             (integer_type_node, p[0]), arg1);
12057             else
12058               return NULL_TREE;
12059           }
12060       }
12061       /* FALLTHROUGH */
12062     case 1: /* length is greater than 1, call fwrite.  */
12063       {
12064         /* If optimizing for size keep fputs.  */
12065         if (optimize_function_for_size_p (cfun))
12066           return NULL_TREE;
12067         /* New argument list transforming fputs(string, stream) to
12068            fwrite(string, 1, len, stream).  */
12069         if (fn_fwrite)
12070           return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12071                                   size_one_node, len, arg1);
12072         else
12073           return NULL_TREE;
12074       }
12075     default:
12076       gcc_unreachable ();
12077     }
12078   return NULL_TREE;
12079 }
12080
12081 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12082    produced.  False otherwise.  This is done so that we don't output the error
12083    or warning twice or three times.  */
12084
12085 bool
12086 fold_builtin_next_arg (tree exp, bool va_start_p)
12087 {
12088   tree fntype = TREE_TYPE (current_function_decl);
12089   int nargs = call_expr_nargs (exp);
12090   tree arg;
12091
12092   if (!stdarg_p (fntype))
12093     {
12094       error ("%<va_start%> used in function with fixed args");
12095       return true;
12096     }
12097
12098   if (va_start_p)
12099     {
12100       if (va_start_p && (nargs != 2))
12101         {
12102           error ("wrong number of arguments to function %<va_start%>");
12103           return true;
12104         }
12105       arg = CALL_EXPR_ARG (exp, 1);
12106     }
12107   /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12108      when we checked the arguments and if needed issued a warning.  */
12109   else
12110     {
12111       if (nargs == 0)
12112         {
12113           /* Evidently an out of date version of <stdarg.h>; can't validate
12114              va_start's second argument, but can still work as intended.  */
12115           warning (0, "%<__builtin_next_arg%> called without an argument");
12116           return true;
12117         }
12118       else if (nargs > 1)
12119         {
12120           error ("wrong number of arguments to function %<__builtin_next_arg%>");
12121           return true;
12122         }
12123       arg = CALL_EXPR_ARG (exp, 0);
12124     }
12125
12126   if (TREE_CODE (arg) == SSA_NAME)
12127     arg = SSA_NAME_VAR (arg);
12128
12129   /* We destructively modify the call to be __builtin_va_start (ap, 0)
12130      or __builtin_next_arg (0) the first time we see it, after checking
12131      the arguments and if needed issuing a warning.  */
12132   if (!integer_zerop (arg))
12133     {
12134       tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12135
12136       /* Strip off all nops for the sake of the comparison.  This
12137          is not quite the same as STRIP_NOPS.  It does more.
12138          We must also strip off INDIRECT_EXPR for C++ reference
12139          parameters.  */
12140       while (CONVERT_EXPR_P (arg)
12141              || TREE_CODE (arg) == INDIRECT_REF)
12142         arg = TREE_OPERAND (arg, 0);
12143       if (arg != last_parm)
12144         {
12145           /* FIXME: Sometimes with the tree optimizers we can get the
12146              not the last argument even though the user used the last
12147              argument.  We just warn and set the arg to be the last
12148              argument so that we will get wrong-code because of
12149              it.  */
12150           warning (0, "second parameter of %<va_start%> not last named argument");
12151         }
12152
12153       /* Undefined by C99 7.15.1.4p4 (va_start):
12154          "If the parameter parmN is declared with the register storage
12155          class, with a function or array type, or with a type that is
12156          not compatible with the type that results after application of
12157          the default argument promotions, the behavior is undefined."
12158       */
12159       else if (DECL_REGISTER (arg))
12160         warning (0, "undefined behaviour when second parameter of "
12161                  "%<va_start%> is declared with %<register%> storage");
12162
12163       /* We want to verify the second parameter just once before the tree
12164          optimizers are run and then avoid keeping it in the tree,
12165          as otherwise we could warn even for correct code like:
12166          void foo (int i, ...)
12167          { va_list ap; i++; va_start (ap, i); va_end (ap); }  */
12168       if (va_start_p)
12169         CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12170       else
12171         CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12172     }
12173   return false;
12174 }
12175
12176
12177 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12178    ORIG may be null if this is a 2-argument call.  We don't attempt to
12179    simplify calls with more than 3 arguments.
12180
12181    Return NULL_TREE if no simplification was possible, otherwise return the
12182    simplified form of the call as a tree.  If IGNORED is true, it means that
12183    the caller does not use the returned value of the function.  */
12184
12185 static tree
12186 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12187                       tree orig, int ignored)
12188 {
12189   tree call, retval;
12190   const char *fmt_str = NULL;
12191
12192   /* Verify the required arguments in the original call.  We deal with two
12193      types of sprintf() calls: 'sprintf (str, fmt)' and
12194      'sprintf (dest, "%s", orig)'.  */
12195   if (!validate_arg (dest, POINTER_TYPE)
12196       || !validate_arg (fmt, POINTER_TYPE))
12197     return NULL_TREE;
12198   if (orig && !validate_arg (orig, POINTER_TYPE))
12199     return NULL_TREE;
12200
12201   /* Check whether the format is a literal string constant.  */
12202   fmt_str = c_getstr (fmt);
12203   if (fmt_str == NULL)
12204     return NULL_TREE;
12205
12206   call = NULL_TREE;
12207   retval = NULL_TREE;
12208
12209   if (!init_target_chars ())
12210     return NULL_TREE;
12211
12212   /* If the format doesn't contain % args or %%, use strcpy.  */
12213   if (strchr (fmt_str, target_percent) == NULL)
12214     {
12215       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12216
12217       if (!fn)
12218         return NULL_TREE;
12219
12220       /* Don't optimize sprintf (buf, "abc", ptr++).  */
12221       if (orig)
12222         return NULL_TREE;
12223
12224       /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12225          'format' is known to contain no % formats.  */
12226       call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12227       if (!ignored)
12228         retval = build_int_cst (integer_type_node, strlen (fmt_str));
12229     }
12230
12231   /* If the format is "%s", use strcpy if the result isn't used.  */
12232   else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12233     {
12234       tree fn;
12235       fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12236
12237       if (!fn)
12238         return NULL_TREE;
12239
12240       /* Don't crash on sprintf (str1, "%s").  */
12241       if (!orig)
12242         return NULL_TREE;
12243
12244       /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2).  */
12245       if (!ignored)
12246         {
12247           retval = c_strlen (orig, 1);
12248           if (!retval || TREE_CODE (retval) != INTEGER_CST)
12249             return NULL_TREE;
12250         }
12251       call = build_call_expr_loc (loc, fn, 2, dest, orig);
12252     }
12253
12254   if (call && retval)
12255     {
12256       retval = fold_convert_loc
12257         (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12258          retval);
12259       return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12260     }
12261   else
12262     return call;
12263 }
12264
12265 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12266    FMT, and ORIG.  ORIG may be null if this is a 3-argument call.  We don't
12267    attempt to simplify calls with more than 4 arguments.
12268
12269    Return NULL_TREE if no simplification was possible, otherwise return the
12270    simplified form of the call as a tree.  If IGNORED is true, it means that
12271    the caller does not use the returned value of the function.  */
12272
12273 static tree
12274 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12275                        tree orig, int ignored)
12276 {
12277   tree call, retval;
12278   const char *fmt_str = NULL;
12279   unsigned HOST_WIDE_INT destlen;
12280
12281   /* Verify the required arguments in the original call.  We deal with two
12282      types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12283      'snprintf (dest, cst, "%s", orig)'.  */
12284   if (!validate_arg (dest, POINTER_TYPE)
12285       || !validate_arg (destsize, INTEGER_TYPE)
12286       || !validate_arg (fmt, POINTER_TYPE))
12287     return NULL_TREE;
12288   if (orig && !validate_arg (orig, POINTER_TYPE))
12289     return NULL_TREE;
12290
12291   if (!host_integerp (destsize, 1))
12292     return NULL_TREE;
12293
12294   /* Check whether the format is a literal string constant.  */
12295   fmt_str = c_getstr (fmt);
12296   if (fmt_str == NULL)
12297     return NULL_TREE;
12298
12299   call = NULL_TREE;
12300   retval = NULL_TREE;
12301
12302   if (!init_target_chars ())
12303     return NULL_TREE;
12304
12305   destlen = tree_low_cst (destsize, 1);
12306
12307   /* If the format doesn't contain % args or %%, use strcpy.  */
12308   if (strchr (fmt_str, target_percent) == NULL)
12309     {
12310       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12311       size_t len = strlen (fmt_str);
12312
12313       /* Don't optimize snprintf (buf, 4, "abc", ptr++).  */
12314       if (orig)
12315         return NULL_TREE;
12316
12317       /* We could expand this as
12318          memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12319          or to
12320          memcpy (str, fmt_with_nul_at_cstm1, cst);
12321          but in the former case that might increase code size
12322          and in the latter case grow .rodata section too much.
12323          So punt for now.  */
12324       if (len >= destlen)
12325         return NULL_TREE;
12326
12327       if (!fn)
12328         return NULL_TREE;
12329
12330       /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12331          'format' is known to contain no % formats and
12332          strlen (fmt) < cst.  */
12333       call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12334
12335       if (!ignored)
12336         retval = build_int_cst (integer_type_node, strlen (fmt_str));
12337     }
12338
12339   /* If the format is "%s", use strcpy if the result isn't used.  */
12340   else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12341     {
12342       tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12343       unsigned HOST_WIDE_INT origlen;
12344
12345       /* Don't crash on snprintf (str1, cst, "%s").  */
12346       if (!orig)
12347         return NULL_TREE;
12348
12349       retval = c_strlen (orig, 1);
12350       if (!retval || !host_integerp (retval, 1))  
12351         return NULL_TREE;
12352
12353       origlen = tree_low_cst (retval, 1);
12354       /* We could expand this as
12355          memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12356          or to
12357          memcpy (str1, str2_with_nul_at_cstm1, cst);
12358          but in the former case that might increase code size
12359          and in the latter case grow .rodata section too much.
12360          So punt for now.  */
12361       if (origlen >= destlen)
12362         return NULL_TREE;
12363
12364       /* Convert snprintf (str1, cst, "%s", str2) into
12365          strcpy (str1, str2) if strlen (str2) < cst.  */
12366       if (!fn)
12367         return NULL_TREE;
12368
12369       call = build_call_expr_loc (loc, fn, 2, dest, orig);
12370
12371       if (ignored)
12372         retval = NULL_TREE;
12373     }
12374
12375   if (call && retval)
12376     {
12377       tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12378       retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12379       return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12380     }
12381   else
12382     return call;
12383 }
12384
12385 /* Expand a call EXP to __builtin_object_size.  */
12386
12387 rtx
12388 expand_builtin_object_size (tree exp)
12389 {
12390   tree ost;
12391   int object_size_type;
12392   tree fndecl = get_callee_fndecl (exp);
12393
12394   if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12395     {
12396       error ("%Kfirst argument of %D must be a pointer, second integer constant",
12397              exp, fndecl);
12398       expand_builtin_trap ();
12399       return const0_rtx;
12400     }
12401
12402   ost = CALL_EXPR_ARG (exp, 1);
12403   STRIP_NOPS (ost);
12404
12405   if (TREE_CODE (ost) != INTEGER_CST
12406       || tree_int_cst_sgn (ost) < 0
12407       || compare_tree_int (ost, 3) > 0)
12408     {
12409       error ("%Klast argument of %D is not integer constant between 0 and 3",
12410              exp, fndecl);
12411       expand_builtin_trap ();
12412       return const0_rtx;
12413     }
12414
12415   object_size_type = tree_low_cst (ost, 0);
12416
12417   return object_size_type < 2 ? constm1_rtx : const0_rtx;
12418 }
12419
12420 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12421    FCODE is the BUILT_IN_* to use.
12422    Return NULL_RTX if we failed; the caller should emit a normal call,
12423    otherwise try to get the result in TARGET, if convenient (and in
12424    mode MODE if that's convenient).  */
12425
12426 static rtx
12427 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12428                            enum built_in_function fcode)
12429 {
12430   tree dest, src, len, size;
12431
12432   if (!validate_arglist (exp,
12433                          POINTER_TYPE,
12434                          fcode == BUILT_IN_MEMSET_CHK
12435                          ? INTEGER_TYPE : POINTER_TYPE,
12436                          INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12437     return NULL_RTX;
12438
12439   dest = CALL_EXPR_ARG (exp, 0);
12440   src = CALL_EXPR_ARG (exp, 1);
12441   len = CALL_EXPR_ARG (exp, 2);
12442   size = CALL_EXPR_ARG (exp, 3);
12443
12444   if (! host_integerp (size, 1))
12445     return NULL_RTX;
12446
12447   if (host_integerp (len, 1) || integer_all_onesp (size))
12448     {
12449       tree fn;
12450
12451       if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12452         {
12453           warning_at (tree_nonartificial_location (exp),
12454                       0, "%Kcall to %D will always overflow destination buffer",
12455                       exp, get_callee_fndecl (exp));
12456           return NULL_RTX;
12457         }
12458
12459       fn = NULL_TREE;
12460       /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12461          mem{cpy,pcpy,move,set} is available.  */
12462       switch (fcode)
12463         {
12464         case BUILT_IN_MEMCPY_CHK:
12465           fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12466           break;
12467         case BUILT_IN_MEMPCPY_CHK:
12468           fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12469           break;
12470         case BUILT_IN_MEMMOVE_CHK:
12471           fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12472           break;
12473         case BUILT_IN_MEMSET_CHK:
12474           fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12475           break;
12476         default:
12477           break;
12478         }
12479
12480       if (! fn)
12481         return NULL_RTX;
12482
12483       fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12484       gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12485       CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12486       return expand_expr (fn, target, mode, EXPAND_NORMAL);
12487     }
12488   else if (fcode == BUILT_IN_MEMSET_CHK)
12489     return NULL_RTX;
12490   else
12491     {
12492       unsigned int dest_align = get_pointer_alignment (dest);
12493
12494       /* If DEST is not a pointer type, call the normal function.  */
12495       if (dest_align == 0)
12496         return NULL_RTX;
12497
12498       /* If SRC and DEST are the same (and not volatile), do nothing.  */
12499       if (operand_equal_p (src, dest, 0))
12500         {
12501           tree expr;
12502
12503           if (fcode != BUILT_IN_MEMPCPY_CHK)
12504             {
12505               /* Evaluate and ignore LEN in case it has side-effects.  */
12506               expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12507               return expand_expr (dest, target, mode, EXPAND_NORMAL);
12508             }
12509
12510           expr = fold_build_pointer_plus (dest, len);
12511           return expand_expr (expr, target, mode, EXPAND_NORMAL);
12512         }
12513
12514       /* __memmove_chk special case.  */
12515       if (fcode == BUILT_IN_MEMMOVE_CHK)
12516         {
12517           unsigned int src_align = get_pointer_alignment (src);
12518
12519           if (src_align == 0)
12520             return NULL_RTX;
12521
12522           /* If src is categorized for a readonly section we can use
12523              normal __memcpy_chk.  */
12524           if (readonly_data_expr (src))
12525             {
12526               tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12527               if (!fn)
12528                 return NULL_RTX;
12529               fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12530                                           dest, src, len, size);
12531               gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12532               CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12533               return expand_expr (fn, target, mode, EXPAND_NORMAL);
12534             }
12535         }
12536       return NULL_RTX;
12537     }
12538 }
12539
12540 /* Emit warning if a buffer overflow is detected at compile time.  */
12541
12542 static void
12543 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12544 {
12545   int is_strlen = 0;
12546   tree len, size;
12547   location_t loc = tree_nonartificial_location (exp);
12548
12549   switch (fcode)
12550     {
12551     case BUILT_IN_STRCPY_CHK:
12552     case BUILT_IN_STPCPY_CHK:
12553     /* For __strcat_chk the warning will be emitted only if overflowing
12554        by at least strlen (dest) + 1 bytes.  */
12555     case BUILT_IN_STRCAT_CHK:
12556       len = CALL_EXPR_ARG (exp, 1);
12557       size = CALL_EXPR_ARG (exp, 2);
12558       is_strlen = 1;
12559       break;
12560     case BUILT_IN_STRNCAT_CHK:
12561     case BUILT_IN_STRNCPY_CHK:
12562     case BUILT_IN_STPNCPY_CHK:
12563       len = CALL_EXPR_ARG (exp, 2);
12564       size = CALL_EXPR_ARG (exp, 3);
12565       break;
12566     case BUILT_IN_SNPRINTF_CHK:
12567     case BUILT_IN_VSNPRINTF_CHK:
12568       len = CALL_EXPR_ARG (exp, 1);
12569       size = CALL_EXPR_ARG (exp, 3);
12570       break;
12571     default:
12572       gcc_unreachable ();
12573     }
12574
12575   if (!len || !size)
12576     return;
12577
12578   if (! host_integerp (size, 1) || integer_all_onesp (size))
12579     return;
12580
12581   if (is_strlen)
12582     {
12583       len = c_strlen (len, 1);
12584       if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12585         return;
12586     }
12587   else if (fcode == BUILT_IN_STRNCAT_CHK)
12588     {
12589       tree src = CALL_EXPR_ARG (exp, 1);
12590       if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12591         return;
12592       src = c_strlen (src, 1);
12593       if (! src || ! host_integerp (src, 1))
12594         {
12595           warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12596                       exp, get_callee_fndecl (exp));
12597           return;
12598         }
12599       else if (tree_int_cst_lt (src, size))
12600         return;
12601     }
12602   else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12603     return;
12604
12605   warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12606               exp, get_callee_fndecl (exp));
12607 }
12608
12609 /* Emit warning if a buffer overflow is detected at compile time
12610    in __sprintf_chk/__vsprintf_chk calls.  */
12611
12612 static void
12613 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12614 {
12615   tree size, len, fmt;
12616   const char *fmt_str;
12617   int nargs = call_expr_nargs (exp);
12618
12619   /* Verify the required arguments in the original call.  */
12620
12621   if (nargs < 4)
12622     return;
12623   size = CALL_EXPR_ARG (exp, 2);
12624   fmt = CALL_EXPR_ARG (exp, 3);
12625
12626   if (! host_integerp (size, 1) || integer_all_onesp (size))
12627     return;
12628
12629   /* Check whether the format is a literal string constant.  */
12630   fmt_str = c_getstr (fmt);
12631   if (fmt_str == NULL)
12632     return;
12633
12634   if (!init_target_chars ())
12635     return;
12636
12637   /* If the format doesn't contain % args or %%, we know its size.  */
12638   if (strchr (fmt_str, target_percent) == 0)
12639     len = build_int_cstu (size_type_node, strlen (fmt_str));
12640   /* If the format is "%s" and first ... argument is a string literal,
12641      we know it too.  */
12642   else if (fcode == BUILT_IN_SPRINTF_CHK
12643            && strcmp (fmt_str, target_percent_s) == 0)
12644     {
12645       tree arg;
12646
12647       if (nargs < 5)
12648         return;
12649       arg = CALL_EXPR_ARG (exp, 4);
12650       if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12651         return;
12652
12653       len = c_strlen (arg, 1);
12654       if (!len || ! host_integerp (len, 1))
12655         return;
12656     }
12657   else
12658     return;
12659
12660   if (! tree_int_cst_lt (len, size))
12661     warning_at (tree_nonartificial_location (exp),
12662                 0, "%Kcall to %D will always overflow destination buffer",
12663                 exp, get_callee_fndecl (exp));
12664 }
12665
12666 /* Emit warning if a free is called with address of a variable.  */
12667
12668 static void
12669 maybe_emit_free_warning (tree exp)
12670 {
12671   tree arg = CALL_EXPR_ARG (exp, 0);
12672
12673   STRIP_NOPS (arg);
12674   if (TREE_CODE (arg) != ADDR_EXPR)
12675     return;
12676
12677   arg = get_base_address (TREE_OPERAND (arg, 0));
12678   if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12679     return;
12680
12681   if (SSA_VAR_P (arg))
12682     warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12683                 "%Kattempt to free a non-heap object %qD", exp, arg);
12684   else
12685     warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12686                 "%Kattempt to free a non-heap object", exp);
12687 }
12688
12689 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12690    if possible.  */
12691
12692 tree
12693 fold_builtin_object_size (tree ptr, tree ost)
12694 {
12695   unsigned HOST_WIDE_INT bytes;
12696   int object_size_type;
12697
12698   if (!validate_arg (ptr, POINTER_TYPE)
12699       || !validate_arg (ost, INTEGER_TYPE))
12700     return NULL_TREE;
12701
12702   STRIP_NOPS (ost);
12703
12704   if (TREE_CODE (ost) != INTEGER_CST
12705       || tree_int_cst_sgn (ost) < 0
12706       || compare_tree_int (ost, 3) > 0)
12707     return NULL_TREE;
12708
12709   object_size_type = tree_low_cst (ost, 0);
12710
12711   /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12712      if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12713      and (size_t) 0 for types 2 and 3.  */
12714   if (TREE_SIDE_EFFECTS (ptr))
12715     return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12716
12717   if (TREE_CODE (ptr) == ADDR_EXPR)
12718     {
12719       bytes = compute_builtin_object_size (ptr, object_size_type);
12720       if (double_int_fits_to_tree_p (size_type_node,
12721                                      uhwi_to_double_int (bytes)))
12722         return build_int_cstu (size_type_node, bytes);
12723     }
12724   else if (TREE_CODE (ptr) == SSA_NAME)
12725     {
12726       /* If object size is not known yet, delay folding until
12727        later.  Maybe subsequent passes will help determining
12728        it.  */
12729       bytes = compute_builtin_object_size (ptr, object_size_type);
12730       if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12731           && double_int_fits_to_tree_p (size_type_node,
12732                                         uhwi_to_double_int (bytes)))
12733         return build_int_cstu (size_type_node, bytes);
12734     }
12735
12736   return NULL_TREE;
12737 }
12738
12739 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12740    DEST, SRC, LEN, and SIZE are the arguments to the call.
12741    IGNORE is true, if return value can be ignored.  FCODE is the BUILT_IN_*
12742    code of the builtin.  If MAXLEN is not NULL, it is maximum length
12743    passed as third argument.  */
12744
12745 tree
12746 fold_builtin_memory_chk (location_t loc, tree fndecl,
12747                          tree dest, tree src, tree len, tree size,
12748                          tree maxlen, bool ignore,
12749                          enum built_in_function fcode)
12750 {
12751   tree fn;
12752
12753   if (!validate_arg (dest, POINTER_TYPE)
12754       || !validate_arg (src,
12755                         (fcode == BUILT_IN_MEMSET_CHK
12756                          ? INTEGER_TYPE : POINTER_TYPE))
12757       || !validate_arg (len, INTEGER_TYPE)
12758       || !validate_arg (size, INTEGER_TYPE))
12759     return NULL_TREE;
12760
12761   /* If SRC and DEST are the same (and not volatile), return DEST
12762      (resp. DEST+LEN for __mempcpy_chk).  */
12763   if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12764     {
12765       if (fcode != BUILT_IN_MEMPCPY_CHK)
12766         return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12767                                  dest, len);
12768       else
12769         {
12770           tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12771           return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12772         }
12773     }
12774
12775   if (! host_integerp (size, 1))
12776     return NULL_TREE;
12777
12778   if (! integer_all_onesp (size))
12779     {
12780       if (! host_integerp (len, 1))
12781         {
12782           /* If LEN is not constant, try MAXLEN too.
12783              For MAXLEN only allow optimizing into non-_ocs function
12784              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
12785           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12786             {
12787               if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12788                 {
12789                   /* (void) __mempcpy_chk () can be optimized into
12790                      (void) __memcpy_chk ().  */
12791                   fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12792                   if (!fn)
12793                     return NULL_TREE;
12794
12795                   return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12796                 }
12797               return NULL_TREE;
12798             }
12799         }
12800       else
12801         maxlen = len;
12802
12803       if (tree_int_cst_lt (size, maxlen))
12804         return NULL_TREE;
12805     }
12806
12807   fn = NULL_TREE;
12808   /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12809      mem{cpy,pcpy,move,set} is available.  */
12810   switch (fcode)
12811     {
12812     case BUILT_IN_MEMCPY_CHK:
12813       fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12814       break;
12815     case BUILT_IN_MEMPCPY_CHK:
12816       fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12817       break;
12818     case BUILT_IN_MEMMOVE_CHK:
12819       fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12820       break;
12821     case BUILT_IN_MEMSET_CHK:
12822       fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12823       break;
12824     default:
12825       break;
12826     }
12827
12828   if (!fn)
12829     return NULL_TREE;
12830
12831   return build_call_expr_loc (loc, fn, 3, dest, src, len);
12832 }
12833
12834 /* Fold a call to the __st[rp]cpy_chk builtin.
12835    DEST, SRC, and SIZE are the arguments to the call.
12836    IGNORE is true if return value can be ignored.  FCODE is the BUILT_IN_*
12837    code of the builtin.  If MAXLEN is not NULL, it is maximum length of
12838    strings passed as second argument.  */
12839
12840 tree
12841 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12842                          tree src, tree size,
12843                          tree maxlen, bool ignore,
12844                          enum built_in_function fcode)
12845 {
12846   tree len, fn;
12847
12848   if (!validate_arg (dest, POINTER_TYPE)
12849       || !validate_arg (src, POINTER_TYPE)
12850       || !validate_arg (size, INTEGER_TYPE))
12851     return NULL_TREE;
12852
12853   /* If SRC and DEST are the same (and not volatile), return DEST.  */
12854   if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12855     return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12856
12857   if (! host_integerp (size, 1))
12858     return NULL_TREE;
12859
12860   if (! integer_all_onesp (size))
12861     {
12862       len = c_strlen (src, 1);
12863       if (! len || ! host_integerp (len, 1))
12864         {
12865           /* If LEN is not constant, try MAXLEN too.
12866              For MAXLEN only allow optimizing into non-_ocs function
12867              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
12868           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12869             {
12870               if (fcode == BUILT_IN_STPCPY_CHK)
12871                 {
12872                   if (! ignore)
12873                     return NULL_TREE;
12874
12875                   /* If return value of __stpcpy_chk is ignored,
12876                      optimize into __strcpy_chk.  */
12877                   fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12878                   if (!fn)
12879                     return NULL_TREE;
12880
12881                   return build_call_expr_loc (loc, fn, 3, dest, src, size);
12882                 }
12883
12884               if (! len || TREE_SIDE_EFFECTS (len))
12885                 return NULL_TREE;
12886
12887               /* If c_strlen returned something, but not a constant,
12888                  transform __strcpy_chk into __memcpy_chk.  */
12889               fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12890               if (!fn)
12891                 return NULL_TREE;
12892
12893               len = fold_convert_loc (loc, size_type_node, len);
12894               len = size_binop_loc (loc, PLUS_EXPR, len,
12895                                     build_int_cst (size_type_node, 1));
12896               return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12897                                        build_call_expr_loc (loc, fn, 4,
12898                                                         dest, src, len, size));
12899             }
12900         }
12901       else
12902         maxlen = len;
12903
12904       if (! tree_int_cst_lt (maxlen, size))
12905         return NULL_TREE;
12906     }
12907
12908   /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available.  */
12909   fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12910                               ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12911   if (!fn)
12912     return NULL_TREE;
12913
12914   return build_call_expr_loc (loc, fn, 2, dest, src);
12915 }
12916
12917 /* Fold a call to the __st{r,p}ncpy_chk builtin.  DEST, SRC, LEN, and SIZE
12918    are the arguments to the call.  If MAXLEN is not NULL, it is maximum
12919    length passed as third argument. IGNORE is true if return value can be
12920    ignored. FCODE is the BUILT_IN_* code of the builtin. */
12921
12922 tree
12923 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12924                           tree len, tree size, tree maxlen, bool ignore,
12925                           enum built_in_function fcode)
12926 {
12927   tree fn;
12928
12929   if (!validate_arg (dest, POINTER_TYPE)
12930       || !validate_arg (src, POINTER_TYPE)
12931       || !validate_arg (len, INTEGER_TYPE)
12932       || !validate_arg (size, INTEGER_TYPE))
12933     return NULL_TREE;
12934
12935   if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12936     {
12937        /* If return value of __stpncpy_chk is ignored,
12938           optimize into __strncpy_chk.  */
12939        fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12940        if (fn)
12941          return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12942     }
12943
12944   if (! host_integerp (size, 1))
12945     return NULL_TREE;
12946
12947   if (! integer_all_onesp (size))
12948     {
12949       if (! host_integerp (len, 1))
12950         {
12951           /* If LEN is not constant, try MAXLEN too.
12952              For MAXLEN only allow optimizing into non-_ocs function
12953              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
12954           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12955             return NULL_TREE;
12956         }
12957       else
12958         maxlen = len;
12959
12960       if (tree_int_cst_lt (size, maxlen))
12961         return NULL_TREE;
12962     }
12963
12964   /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available.  */
12965   fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12966                               ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12967   if (!fn)
12968     return NULL_TREE;
12969
12970   return build_call_expr_loc (loc, fn, 3, dest, src, len);
12971 }
12972
12973 /* Fold a call to the __strcat_chk builtin FNDECL.  DEST, SRC, and SIZE
12974    are the arguments to the call.  */
12975
12976 static tree
12977 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12978                          tree src, tree size)
12979 {
12980   tree fn;
12981   const char *p;
12982
12983   if (!validate_arg (dest, POINTER_TYPE)
12984       || !validate_arg (src, POINTER_TYPE)
12985       || !validate_arg (size, INTEGER_TYPE))
12986     return NULL_TREE;
12987
12988   p = c_getstr (src);
12989   /* If the SRC parameter is "", return DEST.  */
12990   if (p && *p == '\0')
12991     return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12992
12993   if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12994     return NULL_TREE;
12995
12996   /* If __builtin_strcat_chk is used, assume strcat is available.  */
12997   fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12998   if (!fn)
12999     return NULL_TREE;
13000
13001   return build_call_expr_loc (loc, fn, 2, dest, src);
13002 }
13003
13004 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
13005    LEN, and SIZE.  */
13006
13007 static tree
13008 fold_builtin_strncat_chk (location_t loc, tree fndecl,
13009                           tree dest, tree src, tree len, tree size)
13010 {
13011   tree fn;
13012   const char *p;
13013
13014   if (!validate_arg (dest, POINTER_TYPE)
13015       || !validate_arg (src, POINTER_TYPE)
13016       || !validate_arg (size, INTEGER_TYPE)
13017       || !validate_arg (size, INTEGER_TYPE))
13018     return NULL_TREE;
13019
13020   p = c_getstr (src);
13021   /* If the SRC parameter is "" or if LEN is 0, return DEST.  */
13022   if (p && *p == '\0')
13023     return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
13024   else if (integer_zerop (len))
13025     return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13026
13027   if (! host_integerp (size, 1))
13028     return NULL_TREE;
13029
13030   if (! integer_all_onesp (size))
13031     {
13032       tree src_len = c_strlen (src, 1);
13033       if (src_len
13034           && host_integerp (src_len, 1)
13035           && host_integerp (len, 1)
13036           && ! tree_int_cst_lt (len, src_len))
13037         {
13038           /* If LEN >= strlen (SRC), optimize into __strcat_chk.  */
13039           fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
13040           if (!fn)
13041             return NULL_TREE;
13042
13043           return build_call_expr_loc (loc, fn, 3, dest, src, size);
13044         }
13045       return NULL_TREE;
13046     }
13047
13048   /* If __builtin_strncat_chk is used, assume strncat is available.  */
13049   fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13050   if (!fn)
13051     return NULL_TREE;
13052
13053   return build_call_expr_loc (loc, fn, 3, dest, src, len);
13054 }
13055
13056 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13057    Return NULL_TREE if a normal call should be emitted rather than
13058    expanding the function inline.  FCODE is either BUILT_IN_SPRINTF_CHK
13059    or BUILT_IN_VSPRINTF_CHK.  */
13060
13061 static tree
13062 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13063                             enum built_in_function fcode)
13064 {
13065   tree dest, size, len, fn, fmt, flag;
13066   const char *fmt_str;
13067
13068   /* Verify the required arguments in the original call.  */
13069   if (nargs < 4)
13070     return NULL_TREE;
13071   dest = args[0];
13072   if (!validate_arg (dest, POINTER_TYPE))
13073     return NULL_TREE;
13074   flag = args[1];
13075   if (!validate_arg (flag, INTEGER_TYPE))
13076     return NULL_TREE;
13077   size = args[2];
13078   if (!validate_arg (size, INTEGER_TYPE))
13079     return NULL_TREE;
13080   fmt = args[3];
13081   if (!validate_arg (fmt, POINTER_TYPE))
13082     return NULL_TREE;
13083
13084   if (! host_integerp (size, 1))
13085     return NULL_TREE;
13086
13087   len = NULL_TREE;
13088
13089   if (!init_target_chars ())
13090     return NULL_TREE;
13091
13092   /* Check whether the format is a literal string constant.  */
13093   fmt_str = c_getstr (fmt);
13094   if (fmt_str != NULL)
13095     {
13096       /* If the format doesn't contain % args or %%, we know the size.  */
13097       if (strchr (fmt_str, target_percent) == 0)
13098         {
13099           if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13100             len = build_int_cstu (size_type_node, strlen (fmt_str));
13101         }
13102       /* If the format is "%s" and first ... argument is a string literal,
13103          we know the size too.  */
13104       else if (fcode == BUILT_IN_SPRINTF_CHK
13105                && strcmp (fmt_str, target_percent_s) == 0)
13106         {
13107           tree arg;
13108
13109           if (nargs == 5)
13110             {
13111               arg = args[4];
13112               if (validate_arg (arg, POINTER_TYPE))
13113                 {
13114                   len = c_strlen (arg, 1);
13115                   if (! len || ! host_integerp (len, 1))
13116                     len = NULL_TREE;
13117                 }
13118             }
13119         }
13120     }
13121
13122   if (! integer_all_onesp (size))
13123     {
13124       if (! len || ! tree_int_cst_lt (len, size))
13125         return NULL_TREE;
13126     }
13127
13128   /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13129      or if format doesn't contain % chars or is "%s".  */
13130   if (! integer_zerop (flag))
13131     {
13132       if (fmt_str == NULL)
13133         return NULL_TREE;
13134       if (strchr (fmt_str, target_percent) != NULL
13135           && strcmp (fmt_str, target_percent_s))
13136         return NULL_TREE;
13137     }
13138
13139   /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available.  */
13140   fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13141                               ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13142   if (!fn)
13143     return NULL_TREE;
13144
13145   return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13146 }
13147
13148 /* Fold a call EXP to __{,v}sprintf_chk.  Return NULL_TREE if
13149    a normal call should be emitted rather than expanding the function
13150    inline.  FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK.  */
13151
13152 static tree
13153 fold_builtin_sprintf_chk (location_t loc, tree exp,
13154                           enum built_in_function fcode)
13155 {
13156   return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13157                                      CALL_EXPR_ARGP (exp), fcode);
13158 }
13159
13160 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS.  Return
13161    NULL_TREE if a normal call should be emitted rather than expanding
13162    the function inline.  FCODE is either BUILT_IN_SNPRINTF_CHK or
13163    BUILT_IN_VSNPRINTF_CHK.  If MAXLEN is not NULL, it is maximum length
13164    passed as second argument.  */
13165
13166 static tree
13167 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13168                              tree maxlen, enum built_in_function fcode)
13169 {
13170   tree dest, size, len, fn, fmt, flag;
13171   const char *fmt_str;
13172
13173   /* Verify the required arguments in the original call.  */
13174   if (nargs < 5)
13175     return NULL_TREE;
13176   dest = args[0];
13177   if (!validate_arg (dest, POINTER_TYPE))
13178     return NULL_TREE;
13179   len = args[1];
13180   if (!validate_arg (len, INTEGER_TYPE))
13181     return NULL_TREE;
13182   flag = args[2];
13183   if (!validate_arg (flag, INTEGER_TYPE))
13184     return NULL_TREE;
13185   size = args[3];
13186   if (!validate_arg (size, INTEGER_TYPE))
13187     return NULL_TREE;
13188   fmt = args[4];
13189   if (!validate_arg (fmt, POINTER_TYPE))
13190     return NULL_TREE;
13191
13192   if (! host_integerp (size, 1))
13193     return NULL_TREE;
13194
13195   if (! integer_all_onesp (size))
13196     {
13197       if (! host_integerp (len, 1))
13198         {
13199           /* If LEN is not constant, try MAXLEN too.
13200              For MAXLEN only allow optimizing into non-_ocs function
13201              if SIZE is >= MAXLEN, never convert to __ocs_fail ().  */
13202           if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13203             return NULL_TREE;
13204         }
13205       else
13206         maxlen = len;
13207
13208       if (tree_int_cst_lt (size, maxlen))
13209         return NULL_TREE;
13210     }
13211
13212   if (!init_target_chars ())
13213     return NULL_TREE;
13214
13215   /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13216      or if format doesn't contain % chars or is "%s".  */
13217   if (! integer_zerop (flag))
13218     {
13219       fmt_str = c_getstr (fmt);
13220       if (fmt_str == NULL)
13221         return NULL_TREE;
13222       if (strchr (fmt_str, target_percent) != NULL
13223           && strcmp (fmt_str, target_percent_s))
13224         return NULL_TREE;
13225     }
13226
13227   /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13228      available.  */
13229   fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13230                               ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13231   if (!fn)
13232     return NULL_TREE;
13233
13234   return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13235 }
13236
13237 /* Fold a call EXP to {,v}snprintf.  Return NULL_TREE if
13238    a normal call should be emitted rather than expanding the function
13239    inline.  FCODE is either BUILT_IN_SNPRINTF_CHK or
13240    BUILT_IN_VSNPRINTF_CHK.  If MAXLEN is not NULL, it is maximum length
13241    passed as second argument.  */
13242
13243 tree
13244 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13245                            enum built_in_function fcode)
13246 {
13247   return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13248                                       CALL_EXPR_ARGP (exp), maxlen, fcode);
13249 }
13250
13251 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13252    FMT and ARG are the arguments to the call; we don't fold cases with
13253    more than 2 arguments, and ARG may be null if this is a 1-argument case.
13254
13255    Return NULL_TREE if no simplification was possible, otherwise return the
13256    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
13257    code of the function to be simplified.  */
13258
13259 static tree
13260 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13261                      tree arg, bool ignore,
13262                      enum built_in_function fcode)
13263 {
13264   tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13265   const char *fmt_str = NULL;
13266
13267   /* If the return value is used, don't do the transformation.  */
13268   if (! ignore)
13269     return NULL_TREE;
13270
13271   /* Verify the required arguments in the original call.  */
13272   if (!validate_arg (fmt, POINTER_TYPE))
13273     return NULL_TREE;
13274
13275   /* Check whether the format is a literal string constant.  */
13276   fmt_str = c_getstr (fmt);
13277   if (fmt_str == NULL)
13278     return NULL_TREE;
13279
13280   if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13281     {
13282       /* If we're using an unlocked function, assume the other
13283          unlocked functions exist explicitly.  */
13284       fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13285       fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13286     }
13287   else
13288     {
13289       fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13290       fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13291     }
13292
13293   if (!init_target_chars ())
13294     return NULL_TREE;
13295
13296   if (strcmp (fmt_str, target_percent_s) == 0
13297       || strchr (fmt_str, target_percent) == NULL)
13298     {
13299       const char *str;
13300
13301       if (strcmp (fmt_str, target_percent_s) == 0)
13302         {
13303           if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13304             return NULL_TREE;
13305
13306           if (!arg || !validate_arg (arg, POINTER_TYPE))
13307             return NULL_TREE;
13308
13309           str = c_getstr (arg);
13310           if (str == NULL)
13311             return NULL_TREE;
13312         }
13313       else
13314         {
13315           /* The format specifier doesn't contain any '%' characters.  */
13316           if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13317               && arg)
13318             return NULL_TREE;
13319           str = fmt_str;
13320         }
13321
13322       /* If the string was "", printf does nothing.  */
13323       if (str[0] == '\0')
13324         return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13325
13326       /* If the string has length of 1, call putchar.  */
13327       if (str[1] == '\0')
13328         {
13329           /* Given printf("c"), (where c is any one character,)
13330              convert "c"[0] to an int and pass that to the replacement
13331              function.  */
13332           newarg = build_int_cst (integer_type_node, str[0]);
13333           if (fn_putchar)
13334             call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13335         }
13336       else
13337         {
13338           /* If the string was "string\n", call puts("string").  */
13339           size_t len = strlen (str);
13340           if ((unsigned char)str[len - 1] == target_newline
13341               && (size_t) (int) len == len
13342               && (int) len > 0)
13343             {
13344               char *newstr;
13345               tree offset_node, string_cst;
13346
13347               /* Create a NUL-terminated string that's one char shorter
13348                  than the original, stripping off the trailing '\n'.  */
13349               newarg = build_string_literal (len, str);
13350               string_cst = string_constant (newarg, &offset_node);
13351               gcc_checking_assert (string_cst
13352                                    && (TREE_STRING_LENGTH (string_cst)
13353                                        == (int) len)
13354                                    && integer_zerop (offset_node)
13355                                    && (unsigned char)
13356                                       TREE_STRING_POINTER (string_cst)[len - 1]
13357                                       == target_newline);
13358               /* build_string_literal creates a new STRING_CST,
13359                  modify it in place to avoid double copying.  */
13360               newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13361               newstr[len - 1] = '\0';
13362               if (fn_puts)
13363                 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13364             }
13365           else
13366             /* We'd like to arrange to call fputs(string,stdout) here,
13367                but we need stdout and don't have a way to get it yet.  */
13368             return NULL_TREE;
13369         }
13370     }
13371
13372   /* The other optimizations can be done only on the non-va_list variants.  */
13373   else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13374     return NULL_TREE;
13375
13376   /* If the format specifier was "%s\n", call __builtin_puts(arg).  */
13377   else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13378     {
13379       if (!arg || !validate_arg (arg, POINTER_TYPE))
13380         return NULL_TREE;
13381       if (fn_puts)
13382         call = build_call_expr_loc (loc, fn_puts, 1, arg);
13383     }
13384
13385   /* If the format specifier was "%c", call __builtin_putchar(arg).  */
13386   else if (strcmp (fmt_str, target_percent_c) == 0)
13387     {
13388       if (!arg || !validate_arg (arg, INTEGER_TYPE))
13389         return NULL_TREE;
13390       if (fn_putchar)
13391         call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13392     }
13393
13394   if (!call)
13395     return NULL_TREE;
13396
13397   return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13398 }
13399
13400 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13401    FP, FMT, and ARG are the arguments to the call.  We don't fold calls with
13402    more than 3 arguments, and ARG may be null in the 2-argument case.
13403
13404    Return NULL_TREE if no simplification was possible, otherwise return the
13405    simplified form of the call as a tree.  FCODE is the BUILT_IN_*
13406    code of the function to be simplified.  */
13407
13408 static tree
13409 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13410                       tree fmt, tree arg, bool ignore,
13411                       enum built_in_function fcode)
13412 {
13413   tree fn_fputc, fn_fputs, call = NULL_TREE;
13414   const char *fmt_str = NULL;
13415
13416   /* If the return value is used, don't do the transformation.  */
13417   if (! ignore)
13418     return NULL_TREE;
13419
13420   /* Verify the required arguments in the original call.  */
13421   if (!validate_arg (fp, POINTER_TYPE))
13422     return NULL_TREE;
13423   if (!validate_arg (fmt, POINTER_TYPE))
13424     return NULL_TREE;
13425
13426   /* Check whether the format is a literal string constant.  */
13427   fmt_str = c_getstr (fmt);
13428   if (fmt_str == NULL)
13429     return NULL_TREE;
13430
13431   if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13432     {
13433       /* If we're using an unlocked function, assume the other
13434          unlocked functions exist explicitly.  */
13435       fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13436       fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13437     }
13438   else
13439     {
13440       fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13441       fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13442     }
13443
13444   if (!init_target_chars ())
13445     return NULL_TREE;
13446
13447   /* If the format doesn't contain % args or %%, use strcpy.  */
13448   if (strchr (fmt_str, target_percent) == NULL)
13449     {
13450       if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13451           && arg)
13452         return NULL_TREE;
13453
13454       /* If the format specifier was "", fprintf does nothing.  */
13455       if (fmt_str[0] == '\0')
13456         {
13457           /* If FP has side-effects, just wait until gimplification is
13458              done.  */
13459           if (TREE_SIDE_EFFECTS (fp))
13460             return NULL_TREE;
13461
13462           return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13463         }
13464
13465       /* When "string" doesn't contain %, replace all cases of
13466          fprintf (fp, string) with fputs (string, fp).  The fputs
13467          builtin will take care of special cases like length == 1.  */
13468       if (fn_fputs)
13469         call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13470     }
13471
13472   /* The other optimizations can be done only on the non-va_list variants.  */
13473   else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13474     return NULL_TREE;
13475
13476   /* If the format specifier was "%s", call __builtin_fputs (arg, fp).  */
13477   else if (strcmp (fmt_str, target_percent_s) == 0)
13478     {
13479       if (!arg || !validate_arg (arg, POINTER_TYPE))
13480         return NULL_TREE;
13481       if (fn_fputs)
13482         call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13483     }
13484
13485   /* If the format specifier was "%c", call __builtin_fputc (arg, fp).  */
13486   else if (strcmp (fmt_str, target_percent_c) == 0)
13487     {
13488       if (!arg || !validate_arg (arg, INTEGER_TYPE))
13489         return NULL_TREE;
13490       if (fn_fputc)
13491         call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13492     }
13493
13494   if (!call)
13495     return NULL_TREE;
13496   return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13497 }
13498
13499 /* Initialize format string characters in the target charset.  */
13500
13501 static bool
13502 init_target_chars (void)
13503 {
13504   static bool init;
13505   if (!init)
13506     {
13507       target_newline = lang_hooks.to_target_charset ('\n');
13508       target_percent = lang_hooks.to_target_charset ('%');
13509       target_c = lang_hooks.to_target_charset ('c');
13510       target_s = lang_hooks.to_target_charset ('s');
13511       if (target_newline == 0 || target_percent == 0 || target_c == 0
13512           || target_s == 0)
13513         return false;
13514
13515       target_percent_c[0] = target_percent;
13516       target_percent_c[1] = target_c;
13517       target_percent_c[2] = '\0';
13518
13519       target_percent_s[0] = target_percent;
13520       target_percent_s[1] = target_s;
13521       target_percent_s[2] = '\0';
13522
13523       target_percent_s_newline[0] = target_percent;
13524       target_percent_s_newline[1] = target_s;
13525       target_percent_s_newline[2] = target_newline;
13526       target_percent_s_newline[3] = '\0';
13527
13528       init = true;
13529     }
13530   return true;
13531 }
13532
13533 /* Helper function for do_mpfr_arg*().  Ensure M is a normal number
13534    and no overflow/underflow occurred.  INEXACT is true if M was not
13535    exactly calculated.  TYPE is the tree type for the result.  This
13536    function assumes that you cleared the MPFR flags and then
13537    calculated M to see if anything subsequently set a flag prior to
13538    entering this function.  Return NULL_TREE if any checks fail.  */
13539
13540 static tree
13541 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13542 {
13543   /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13544      overflow/underflow occurred.  If -frounding-math, proceed iff the
13545      result of calling FUNC was exact.  */
13546   if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13547       && (!flag_rounding_math || !inexact))
13548     {
13549       REAL_VALUE_TYPE rr;
13550
13551       real_from_mpfr (&rr, m, type, GMP_RNDN);
13552       /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13553          check for overflow/underflow.  If the REAL_VALUE_TYPE is zero
13554          but the mpft_t is not, then we underflowed in the
13555          conversion.  */
13556       if (real_isfinite (&rr)
13557           && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13558         {
13559           REAL_VALUE_TYPE rmode;
13560
13561           real_convert (&rmode, TYPE_MODE (type), &rr);
13562           /* Proceed iff the specified mode can hold the value.  */
13563           if (real_identical (&rmode, &rr))
13564             return build_real (type, rmode);
13565         }
13566     }
13567   return NULL_TREE;
13568 }
13569
13570 /* Helper function for do_mpc_arg*().  Ensure M is a normal complex
13571    number and no overflow/underflow occurred.  INEXACT is true if M
13572    was not exactly calculated.  TYPE is the tree type for the result.
13573    This function assumes that you cleared the MPFR flags and then
13574    calculated M to see if anything subsequently set a flag prior to
13575    entering this function.  Return NULL_TREE if any checks fail, if
13576    FORCE_CONVERT is true, then bypass the checks.  */
13577
13578 static tree
13579 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13580 {
13581   /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13582      overflow/underflow occurred.  If -frounding-math, proceed iff the
13583      result of calling FUNC was exact.  */
13584   if (force_convert
13585       || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13586           && !mpfr_overflow_p () && !mpfr_underflow_p ()
13587           && (!flag_rounding_math || !inexact)))
13588     {
13589       REAL_VALUE_TYPE re, im;
13590
13591       real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13592       real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13593       /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13594          check for overflow/underflow.  If the REAL_VALUE_TYPE is zero
13595          but the mpft_t is not, then we underflowed in the
13596          conversion.  */
13597       if (force_convert
13598           || (real_isfinite (&re) && real_isfinite (&im)
13599               && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13600               && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13601         {
13602           REAL_VALUE_TYPE re_mode, im_mode;
13603
13604           real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13605           real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13606           /* Proceed iff the specified mode can hold the value.  */
13607           if (force_convert
13608               || (real_identical (&re_mode, &re)
13609                   && real_identical (&im_mode, &im)))
13610             return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13611                                   build_real (TREE_TYPE (type), im_mode));
13612         }
13613     }
13614   return NULL_TREE;
13615 }
13616
13617 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13618    FUNC on it and return the resulting value as a tree with type TYPE.
13619    If MIN and/or MAX are not NULL, then the supplied ARG must be
13620    within those bounds.  If INCLUSIVE is true, then MIN/MAX are
13621    acceptable values, otherwise they are not.  The mpfr precision is
13622    set to the precision of TYPE.  We assume that function FUNC returns
13623    zero if the result could be calculated exactly within the requested
13624    precision.  */
13625
13626 static tree
13627 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13628               const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13629               bool inclusive)
13630 {
13631   tree result = NULL_TREE;
13632
13633   STRIP_NOPS (arg);
13634
13635   /* To proceed, MPFR must exactly represent the target floating point
13636      format, which only happens when the target base equals two.  */
13637   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13638       && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13639     {
13640       const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13641
13642       if (real_isfinite (ra)
13643           && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13644           && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13645         {
13646           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13647           const int prec = fmt->p;
13648           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13649           int inexact;
13650           mpfr_t m;
13651
13652           mpfr_init2 (m, prec);
13653           mpfr_from_real (m, ra, GMP_RNDN);
13654           mpfr_clear_flags ();
13655           inexact = func (m, m, rnd);
13656           result = do_mpfr_ckconv (m, type, inexact);
13657           mpfr_clear (m);
13658         }
13659     }
13660
13661   return result;
13662 }
13663
13664 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13665    FUNC on it and return the resulting value as a tree with type TYPE.
13666    The mpfr precision is set to the precision of TYPE.  We assume that
13667    function FUNC returns zero if the result could be calculated
13668    exactly within the requested precision.  */
13669
13670 static tree
13671 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13672               int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13673 {
13674   tree result = NULL_TREE;
13675
13676   STRIP_NOPS (arg1);
13677   STRIP_NOPS (arg2);
13678
13679   /* To proceed, MPFR must exactly represent the target floating point
13680      format, which only happens when the target base equals two.  */
13681   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13682       && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13683       && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13684     {
13685       const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13686       const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13687
13688       if (real_isfinite (ra1) && real_isfinite (ra2))
13689         {
13690           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13691           const int prec = fmt->p;
13692           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13693           int inexact;
13694           mpfr_t m1, m2;
13695
13696           mpfr_inits2 (prec, m1, m2, NULL);
13697           mpfr_from_real (m1, ra1, GMP_RNDN);
13698           mpfr_from_real (m2, ra2, GMP_RNDN);
13699           mpfr_clear_flags ();
13700           inexact = func (m1, m1, m2, rnd);
13701           result = do_mpfr_ckconv (m1, type, inexact);
13702           mpfr_clears (m1, m2, NULL);
13703         }
13704     }
13705
13706   return result;
13707 }
13708
13709 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13710    FUNC on it and return the resulting value as a tree with type TYPE.
13711    The mpfr precision is set to the precision of TYPE.  We assume that
13712    function FUNC returns zero if the result could be calculated
13713    exactly within the requested precision.  */
13714
13715 static tree
13716 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13717               int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13718 {
13719   tree result = NULL_TREE;
13720
13721   STRIP_NOPS (arg1);
13722   STRIP_NOPS (arg2);
13723   STRIP_NOPS (arg3);
13724
13725   /* To proceed, MPFR must exactly represent the target floating point
13726      format, which only happens when the target base equals two.  */
13727   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13728       && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13729       && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13730       && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13731     {
13732       const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13733       const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13734       const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13735
13736       if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13737         {
13738           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13739           const int prec = fmt->p;
13740           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13741           int inexact;
13742           mpfr_t m1, m2, m3;
13743
13744           mpfr_inits2 (prec, m1, m2, m3, NULL);
13745           mpfr_from_real (m1, ra1, GMP_RNDN);
13746           mpfr_from_real (m2, ra2, GMP_RNDN);
13747           mpfr_from_real (m3, ra3, GMP_RNDN);
13748           mpfr_clear_flags ();
13749           inexact = func (m1, m1, m2, m3, rnd);
13750           result = do_mpfr_ckconv (m1, type, inexact);
13751           mpfr_clears (m1, m2, m3, NULL);
13752         }
13753     }
13754
13755   return result;
13756 }
13757
13758 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13759    the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13760    If ARG_SINP and ARG_COSP are NULL then the result is returned
13761    as a complex value.
13762    The type is taken from the type of ARG and is used for setting the
13763    precision of the calculation and results.  */
13764
13765 static tree
13766 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13767 {
13768   tree const type = TREE_TYPE (arg);
13769   tree result = NULL_TREE;
13770
13771   STRIP_NOPS (arg);
13772
13773   /* To proceed, MPFR must exactly represent the target floating point
13774      format, which only happens when the target base equals two.  */
13775   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13776       && TREE_CODE (arg) == REAL_CST
13777       && !TREE_OVERFLOW (arg))
13778     {
13779       const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13780
13781       if (real_isfinite (ra))
13782         {
13783           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13784           const int prec = fmt->p;
13785           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13786           tree result_s, result_c;
13787           int inexact;
13788           mpfr_t m, ms, mc;
13789
13790           mpfr_inits2 (prec, m, ms, mc, NULL);
13791           mpfr_from_real (m, ra, GMP_RNDN);
13792           mpfr_clear_flags ();
13793           inexact = mpfr_sin_cos (ms, mc, m, rnd);
13794           result_s = do_mpfr_ckconv (ms, type, inexact);
13795           result_c = do_mpfr_ckconv (mc, type, inexact);
13796           mpfr_clears (m, ms, mc, NULL);
13797           if (result_s && result_c)
13798             {
13799               /* If we are to return in a complex value do so.  */
13800               if (!arg_sinp && !arg_cosp)
13801                 return build_complex (build_complex_type (type),
13802                                       result_c, result_s);
13803
13804               /* Dereference the sin/cos pointer arguments.  */
13805               arg_sinp = build_fold_indirect_ref (arg_sinp);
13806               arg_cosp = build_fold_indirect_ref (arg_cosp);
13807               /* Proceed if valid pointer type were passed in.  */
13808               if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13809                   && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13810                 {
13811                   /* Set the values. */
13812                   result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13813                                           result_s);
13814                   TREE_SIDE_EFFECTS (result_s) = 1;
13815                   result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13816                                           result_c);
13817                   TREE_SIDE_EFFECTS (result_c) = 1;
13818                   /* Combine the assignments into a compound expr.  */
13819                   result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13820                                                     result_s, result_c));
13821                 }
13822             }
13823         }
13824     }
13825   return result;
13826 }
13827
13828 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13829    two-argument mpfr order N Bessel function FUNC on them and return
13830    the resulting value as a tree with type TYPE.  The mpfr precision
13831    is set to the precision of TYPE.  We assume that function FUNC
13832    returns zero if the result could be calculated exactly within the
13833    requested precision.  */
13834 static tree
13835 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13836                   int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13837                   const REAL_VALUE_TYPE *min, bool inclusive)
13838 {
13839   tree result = NULL_TREE;
13840
13841   STRIP_NOPS (arg1);
13842   STRIP_NOPS (arg2);
13843
13844   /* To proceed, MPFR must exactly represent the target floating point
13845      format, which only happens when the target base equals two.  */
13846   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13847       && host_integerp (arg1, 0)
13848       && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13849     {
13850       const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13851       const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13852
13853       if (n == (long)n
13854           && real_isfinite (ra)
13855           && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13856         {
13857           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13858           const int prec = fmt->p;
13859           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13860           int inexact;
13861           mpfr_t m;
13862
13863           mpfr_init2 (m, prec);
13864           mpfr_from_real (m, ra, GMP_RNDN);
13865           mpfr_clear_flags ();
13866           inexact = func (m, n, m, rnd);
13867           result = do_mpfr_ckconv (m, type, inexact);
13868           mpfr_clear (m);
13869         }
13870     }
13871
13872   return result;
13873 }
13874
13875 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13876    the pointer *(ARG_QUO) and return the result.  The type is taken
13877    from the type of ARG0 and is used for setting the precision of the
13878    calculation and results.  */
13879
13880 static tree
13881 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13882 {
13883   tree const type = TREE_TYPE (arg0);
13884   tree result = NULL_TREE;
13885
13886   STRIP_NOPS (arg0);
13887   STRIP_NOPS (arg1);
13888
13889   /* To proceed, MPFR must exactly represent the target floating point
13890      format, which only happens when the target base equals two.  */
13891   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13892       && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13893       && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13894     {
13895       const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13896       const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13897
13898       if (real_isfinite (ra0) && real_isfinite (ra1))
13899         {
13900           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13901           const int prec = fmt->p;
13902           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13903           tree result_rem;
13904           long integer_quo;
13905           mpfr_t m0, m1;
13906
13907           mpfr_inits2 (prec, m0, m1, NULL);
13908           mpfr_from_real (m0, ra0, GMP_RNDN);
13909           mpfr_from_real (m1, ra1, GMP_RNDN);
13910           mpfr_clear_flags ();
13911           mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13912           /* Remquo is independent of the rounding mode, so pass
13913              inexact=0 to do_mpfr_ckconv().  */
13914           result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13915           mpfr_clears (m0, m1, NULL);
13916           if (result_rem)
13917             {
13918               /* MPFR calculates quo in the host's long so it may
13919                  return more bits in quo than the target int can hold
13920                  if sizeof(host long) > sizeof(target int).  This can
13921                  happen even for native compilers in LP64 mode.  In
13922                  these cases, modulo the quo value with the largest
13923                  number that the target int can hold while leaving one
13924                  bit for the sign.  */
13925               if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13926                 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13927
13928               /* Dereference the quo pointer argument.  */
13929               arg_quo = build_fold_indirect_ref (arg_quo);
13930               /* Proceed iff a valid pointer type was passed in.  */
13931               if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13932                 {
13933                   /* Set the value. */
13934                   tree result_quo
13935                     = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13936                                    build_int_cst (TREE_TYPE (arg_quo),
13937                                                   integer_quo));
13938                   TREE_SIDE_EFFECTS (result_quo) = 1;
13939                   /* Combine the quo assignment with the rem.  */
13940                   result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13941                                                     result_quo, result_rem));
13942                 }
13943             }
13944         }
13945     }
13946   return result;
13947 }
13948
13949 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13950    resulting value as a tree with type TYPE.  The mpfr precision is
13951    set to the precision of TYPE.  We assume that this mpfr function
13952    returns zero if the result could be calculated exactly within the
13953    requested precision.  In addition, the integer pointer represented
13954    by ARG_SG will be dereferenced and set to the appropriate signgam
13955    (-1,1) value.  */
13956
13957 static tree
13958 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13959 {
13960   tree result = NULL_TREE;
13961
13962   STRIP_NOPS (arg);
13963
13964   /* To proceed, MPFR must exactly represent the target floating point
13965      format, which only happens when the target base equals two.  Also
13966      verify ARG is a constant and that ARG_SG is an int pointer.  */
13967   if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13968       && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13969       && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13970       && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13971     {
13972       const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13973
13974       /* In addition to NaN and Inf, the argument cannot be zero or a
13975          negative integer.  */
13976       if (real_isfinite (ra)
13977           && ra->cl != rvc_zero
13978           && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13979         {
13980           const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13981           const int prec = fmt->p;
13982           const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13983           int inexact, sg;
13984           mpfr_t m;
13985           tree result_lg;
13986
13987           mpfr_init2 (m, prec);
13988           mpfr_from_real (m, ra, GMP_RNDN);
13989           mpfr_clear_flags ();
13990           inexact = mpfr_lgamma (m, &sg, m, rnd);
13991           result_lg = do_mpfr_ckconv (m, type, inexact);
13992           mpfr_clear (m);
13993           if (result_lg)
13994             {
13995               tree result_sg;
13996
13997               /* Dereference the arg_sg pointer argument.  */
13998               arg_sg = build_fold_indirect_ref (arg_sg);
13999               /* Assign the signgam value into *arg_sg. */
14000               result_sg = fold_build2 (MODIFY_EXPR,
14001                                        TREE_TYPE (arg_sg), arg_sg,
14002                                        build_int_cst (TREE_TYPE (arg_sg), sg));
14003               TREE_SIDE_EFFECTS (result_sg) = 1;
14004               /* Combine the signgam assignment with the lgamma result.  */
14005               result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
14006                                                 result_sg, result_lg));
14007             }
14008         }
14009     }
14010
14011   return result;
14012 }
14013
14014 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14015    function FUNC on it and return the resulting value as a tree with
14016    type TYPE.  The mpfr precision is set to the precision of TYPE.  We
14017    assume that function FUNC returns zero if the result could be
14018    calculated exactly within the requested precision.  */
14019
14020 static tree
14021 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
14022 {
14023   tree result = NULL_TREE;
14024
14025   STRIP_NOPS (arg);
14026
14027   /* To proceed, MPFR must exactly represent the target floating point
14028      format, which only happens when the target base equals two.  */
14029   if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14030       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14031       && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14032     {
14033       const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14034       const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14035
14036       if (real_isfinite (re) && real_isfinite (im))
14037         {
14038           const struct real_format *const fmt =
14039             REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14040           const int prec = fmt->p;
14041           const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14042           const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14043           int inexact;
14044           mpc_t m;
14045
14046           mpc_init2 (m, prec);
14047           mpfr_from_real (mpc_realref(m), re, rnd);
14048           mpfr_from_real (mpc_imagref(m), im, rnd);
14049           mpfr_clear_flags ();
14050           inexact = func (m, m, crnd);
14051           result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14052           mpc_clear (m);
14053         }
14054     }
14055
14056   return result;
14057 }
14058
14059 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14060    mpc function FUNC on it and return the resulting value as a tree
14061    with type TYPE.  The mpfr precision is set to the precision of
14062    TYPE.  We assume that function FUNC returns zero if the result
14063    could be calculated exactly within the requested precision.  If
14064    DO_NONFINITE is true, then fold expressions containing Inf or NaN
14065    in the arguments and/or results.  */
14066
14067 tree
14068 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14069              int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14070 {
14071   tree result = NULL_TREE;
14072
14073   STRIP_NOPS (arg0);
14074   STRIP_NOPS (arg1);
14075
14076   /* To proceed, MPFR must exactly represent the target floating point
14077      format, which only happens when the target base equals two.  */
14078   if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14079       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14080       && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14081       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14082       && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14083     {
14084       const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14085       const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14086       const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14087       const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14088
14089       if (do_nonfinite
14090           || (real_isfinite (re0) && real_isfinite (im0)
14091               && real_isfinite (re1) && real_isfinite (im1)))
14092         {
14093           const struct real_format *const fmt =
14094             REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14095           const int prec = fmt->p;
14096           const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14097           const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14098           int inexact;
14099           mpc_t m0, m1;
14100
14101           mpc_init2 (m0, prec);
14102           mpc_init2 (m1, prec);
14103           mpfr_from_real (mpc_realref(m0), re0, rnd);
14104           mpfr_from_real (mpc_imagref(m0), im0, rnd);
14105           mpfr_from_real (mpc_realref(m1), re1, rnd);
14106           mpfr_from_real (mpc_imagref(m1), im1, rnd);
14107           mpfr_clear_flags ();
14108           inexact = func (m0, m0, m1, crnd);
14109           result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14110           mpc_clear (m0);
14111           mpc_clear (m1);
14112         }
14113     }
14114
14115   return result;
14116 }
14117
14118 /* Fold a call STMT to __{,v}sprintf_chk.  Return NULL_TREE if
14119    a normal call should be emitted rather than expanding the function
14120    inline.  FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK.  */
14121
14122 static tree
14123 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14124 {
14125   int nargs = gimple_call_num_args (stmt);
14126
14127   return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14128                                      (nargs > 0
14129                                       ? gimple_call_arg_ptr (stmt, 0)
14130                                       : &error_mark_node), fcode);
14131 }
14132
14133 /* Fold a call STMT to {,v}snprintf.  Return NULL_TREE if
14134    a normal call should be emitted rather than expanding the function
14135    inline.  FCODE is either BUILT_IN_SNPRINTF_CHK or
14136    BUILT_IN_VSNPRINTF_CHK.  If MAXLEN is not NULL, it is maximum length
14137    passed as second argument.  */
14138
14139 tree
14140 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14141                                   enum built_in_function fcode)
14142 {
14143   int nargs = gimple_call_num_args (stmt);
14144
14145   return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14146                                       (nargs > 0
14147                                        ? gimple_call_arg_ptr (stmt, 0)
14148                                        : &error_mark_node), maxlen, fcode);
14149 }
14150
14151 /* Builtins with folding operations that operate on "..." arguments
14152    need special handling; we need to store the arguments in a convenient
14153    data structure before attempting any folding.  Fortunately there are
14154    only a few builtins that fall into this category.  FNDECL is the
14155    function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14156    result of the function call is ignored.  */
14157
14158 static tree
14159 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14160                              bool ignore ATTRIBUTE_UNUSED)
14161 {
14162   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14163   tree ret = NULL_TREE;
14164
14165   switch (fcode)
14166     {
14167     case BUILT_IN_SPRINTF_CHK:
14168     case BUILT_IN_VSPRINTF_CHK:
14169       ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14170       break;
14171
14172     case BUILT_IN_SNPRINTF_CHK:
14173     case BUILT_IN_VSNPRINTF_CHK:
14174       ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14175
14176     default:
14177       break;
14178     }
14179   if (ret)
14180     {
14181       ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14182       TREE_NO_WARNING (ret) = 1;
14183       return ret;
14184     }
14185   return NULL_TREE;
14186 }
14187
14188 /* A wrapper function for builtin folding that prevents warnings for
14189    "statement without effect" and the like, caused by removing the
14190    call node earlier than the warning is generated.  */
14191
14192 tree
14193 fold_call_stmt (gimple stmt, bool ignore)
14194 {
14195   tree ret = NULL_TREE;
14196   tree fndecl = gimple_call_fndecl (stmt);
14197   location_t loc = gimple_location (stmt);
14198   if (fndecl
14199       && TREE_CODE (fndecl) == FUNCTION_DECL
14200       && DECL_BUILT_IN (fndecl)
14201       && !gimple_call_va_arg_pack_p (stmt))
14202     {
14203       int nargs = gimple_call_num_args (stmt);
14204       tree *args = (nargs > 0
14205                     ? gimple_call_arg_ptr (stmt, 0)
14206                     : &error_mark_node);
14207
14208       if (avoid_folding_inline_builtin (fndecl))
14209         return NULL_TREE;
14210       if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14211         {
14212           return targetm.fold_builtin (fndecl, nargs, args, ignore);
14213         }
14214       else
14215         {
14216           if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14217             ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14218           if (!ret)
14219             ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14220           if (ret)
14221             {
14222               /* Propagate location information from original call to
14223                  expansion of builtin.  Otherwise things like
14224                  maybe_emit_chk_warning, that operate on the expansion
14225                  of a builtin, will use the wrong location information.  */
14226               if (gimple_has_location (stmt))
14227                 {
14228                   tree realret = ret;
14229                   if (TREE_CODE (ret) == NOP_EXPR)
14230                     realret = TREE_OPERAND (ret, 0);
14231                   if (CAN_HAVE_LOCATION_P (realret)
14232                       && !EXPR_HAS_LOCATION (realret))
14233                     SET_EXPR_LOCATION (realret, loc);
14234                   return realret;
14235                 }
14236               return ret;
14237             }
14238         }
14239     }
14240   return NULL_TREE;
14241 }
14242
14243 /* Look up the function in builtin_decl that corresponds to DECL
14244    and set ASMSPEC as its user assembler name.  DECL must be a
14245    function decl that declares a builtin.  */
14246
14247 void
14248 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14249 {
14250   tree builtin;
14251   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14252               && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14253               && asmspec != 0);
14254
14255   builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14256   set_user_assembler_name (builtin, asmspec);
14257   switch (DECL_FUNCTION_CODE (decl))
14258     {
14259     case BUILT_IN_MEMCPY:
14260       init_block_move_fn (asmspec);
14261       memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14262       break;
14263     case BUILT_IN_MEMSET:
14264       init_block_clear_fn (asmspec);
14265       memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14266       break;
14267     case BUILT_IN_MEMMOVE:
14268       memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14269       break;
14270     case BUILT_IN_MEMCMP:
14271       memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14272       break;
14273     case BUILT_IN_ABORT:
14274       abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14275       break;
14276     case BUILT_IN_FFS:
14277       if (INT_TYPE_SIZE < BITS_PER_WORD)
14278         {
14279           set_user_assembler_libfunc ("ffs", asmspec);
14280           set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14281                                                        MODE_INT, 0), "ffs");
14282         }
14283       break;
14284     default:
14285       break;
14286     }
14287 }
14288
14289 /* Return true if DECL is a builtin that expands to a constant or similarly
14290    simple code.  */
14291 bool
14292 is_simple_builtin (tree decl)
14293 {
14294   if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14295     switch (DECL_FUNCTION_CODE (decl))
14296       {
14297         /* Builtins that expand to constants.  */
14298       case BUILT_IN_CONSTANT_P:
14299       case BUILT_IN_EXPECT:
14300       case BUILT_IN_OBJECT_SIZE:
14301       case BUILT_IN_UNREACHABLE:
14302         /* Simple register moves or loads from stack.  */
14303       case BUILT_IN_ASSUME_ALIGNED:
14304       case BUILT_IN_RETURN_ADDRESS:
14305       case BUILT_IN_EXTRACT_RETURN_ADDR:
14306       case BUILT_IN_FROB_RETURN_ADDR:
14307       case BUILT_IN_RETURN:
14308       case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14309       case BUILT_IN_FRAME_ADDRESS:
14310       case BUILT_IN_VA_END:
14311       case BUILT_IN_STACK_SAVE:
14312       case BUILT_IN_STACK_RESTORE:
14313         /* Exception state returns or moves registers around.  */
14314       case BUILT_IN_EH_FILTER:
14315       case BUILT_IN_EH_POINTER:
14316       case BUILT_IN_EH_COPY_VALUES:
14317         return true;
14318
14319       default:
14320         return false;
14321       }
14322
14323   return false;
14324 }
14325
14326 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14327    most probably expanded inline into reasonably simple code.  This is a
14328    superset of is_simple_builtin.  */
14329 bool
14330 is_inexpensive_builtin (tree decl)
14331 {
14332   if (!decl)
14333     return false;
14334   else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14335     return true;
14336   else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14337     switch (DECL_FUNCTION_CODE (decl))
14338       {
14339       case BUILT_IN_ABS:
14340       case BUILT_IN_ALLOCA:
14341       case BUILT_IN_ALLOCA_WITH_ALIGN:
14342       case BUILT_IN_BSWAP32:
14343       case BUILT_IN_BSWAP64:
14344       case BUILT_IN_CLZ:
14345       case BUILT_IN_CLZIMAX:
14346       case BUILT_IN_CLZL:
14347       case BUILT_IN_CLZLL:
14348       case BUILT_IN_CTZ:
14349       case BUILT_IN_CTZIMAX:
14350       case BUILT_IN_CTZL:
14351       case BUILT_IN_CTZLL:
14352       case BUILT_IN_FFS:
14353       case BUILT_IN_FFSIMAX:
14354       case BUILT_IN_FFSL:
14355       case BUILT_IN_FFSLL:
14356       case BUILT_IN_IMAXABS:
14357       case BUILT_IN_FINITE:
14358       case BUILT_IN_FINITEF:
14359       case BUILT_IN_FINITEL:
14360       case BUILT_IN_FINITED32:
14361       case BUILT_IN_FINITED64:
14362       case BUILT_IN_FINITED128:
14363       case BUILT_IN_FPCLASSIFY:
14364       case BUILT_IN_ISFINITE:
14365       case BUILT_IN_ISINF_SIGN:
14366       case BUILT_IN_ISINF:
14367       case BUILT_IN_ISINFF:
14368       case BUILT_IN_ISINFL:
14369       case BUILT_IN_ISINFD32:
14370       case BUILT_IN_ISINFD64:
14371       case BUILT_IN_ISINFD128:
14372       case BUILT_IN_ISNAN:
14373       case BUILT_IN_ISNANF:
14374       case BUILT_IN_ISNANL:
14375       case BUILT_IN_ISNAND32:
14376       case BUILT_IN_ISNAND64:
14377       case BUILT_IN_ISNAND128:
14378       case BUILT_IN_ISNORMAL:
14379       case BUILT_IN_ISGREATER:
14380       case BUILT_IN_ISGREATEREQUAL:
14381       case BUILT_IN_ISLESS:
14382       case BUILT_IN_ISLESSEQUAL:
14383       case BUILT_IN_ISLESSGREATER:
14384       case BUILT_IN_ISUNORDERED:
14385       case BUILT_IN_VA_ARG_PACK:
14386       case BUILT_IN_VA_ARG_PACK_LEN:
14387       case BUILT_IN_VA_COPY:
14388       case BUILT_IN_TRAP:
14389       case BUILT_IN_SAVEREGS:
14390       case BUILT_IN_POPCOUNTL:
14391       case BUILT_IN_POPCOUNTLL:
14392       case BUILT_IN_POPCOUNTIMAX:
14393       case BUILT_IN_POPCOUNT:
14394       case BUILT_IN_PARITYL:
14395       case BUILT_IN_PARITYLL:
14396       case BUILT_IN_PARITYIMAX:
14397       case BUILT_IN_PARITY:
14398       case BUILT_IN_LABS:
14399       case BUILT_IN_LLABS:
14400       case BUILT_IN_PREFETCH:
14401         return true;
14402
14403       default:
14404         return is_simple_builtin (decl);
14405       }
14406
14407   return false;
14408 }