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