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