LCOV - code coverage report
Current view: top level - gcc - internal-fn.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 74.3 % 3169 2356
Test Date: 2026-12-05 16:10:02 Functions: 61.4 % 184 113
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Internal functions.
       2              :    Copyright (C) 2011-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify it under
       7              : the terms of the GNU General Public License as published by the Free
       8              : Software Foundation; either version 3, or (at your option) any later
       9              : version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      12              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14              : for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : #include "config.h"
      21              : #define INCLUDE_MEMORY
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "backend.h"
      25              : #include "target.h"
      26              : #include "rtl.h"
      27              : #include "tree.h"
      28              : #include "tree-eh.h"
      29              : #include "gimple.h"
      30              : #include "predict.h"
      31              : #include "stringpool.h"
      32              : #include "tree-vrp.h"
      33              : #include "tree-ssanames.h"
      34              : #include "expmed.h"
      35              : #include "memmodel.h"
      36              : #include "optabs.h"
      37              : #include "emit-rtl.h"
      38              : #include "diagnostic-core.h"
      39              : #include "fold-const.h"
      40              : #include "internal-fn.h"
      41              : #include "stor-layout.h"
      42              : #include "dojump.h"
      43              : #include "expr.h"
      44              : #include "attribs.h"
      45              : #include "asan.h"
      46              : #include "ubsan.h"
      47              : #include "recog.h"
      48              : #include "builtins.h"
      49              : #include "optabs-tree.h"
      50              : #include "gimple-ssa.h"
      51              : #include "tree-phinodes.h"
      52              : #include "ssa-iterators.h"
      53              : #include "explow.h"
      54              : #include "rtl-iter.h"
      55              : #include "gimple-range.h"
      56              : #include "fold-const-call.h"
      57              : #include "tree-ssa-live.h"
      58              : #include "tree-outof-ssa.h"
      59              : #include "gcc-urlifier.h"
      60              : 
      61              : /* For lang_hooks.types.type_for_mode.  */
      62              : #include "langhooks.h"
      63              : 
      64              : /* The names of each internal function, indexed by function number.  */
      65              : const char *const internal_fn_name_array[] = {
      66              : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) #CODE,
      67              : #include "internal-fn.def"
      68              :   "<invalid-fn>"
      69              : };
      70              : 
      71              : /* The ECF_* flags of each internal function, indexed by function number.  */
      72              : const int internal_fn_flags_array[] = {
      73              : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) FLAGS,
      74              : #include "internal-fn.def"
      75              :   0
      76              : };
      77              : 
      78              : /* Return the internal function called NAME, or IFN_LAST if there's
      79              :    no such function.  */
      80              : 
      81              : internal_fn
      82          154 : lookup_internal_fn (const char *name)
      83              : {
      84          154 :   typedef hash_map<nofree_string_hash, internal_fn> name_to_fn_map_type;
      85          154 :   static name_to_fn_map_type *name_to_fn_map;
      86              : 
      87          154 :   if (!name_to_fn_map)
      88              :     {
      89           21 :       name_to_fn_map = new name_to_fn_map_type (IFN_LAST);
      90         6321 :       for (unsigned int i = 0; i < IFN_LAST; ++i)
      91        12600 :         name_to_fn_map->put (internal_fn_name (internal_fn (i)),
      92         6300 :                              internal_fn (i));
      93              :     }
      94          154 :   internal_fn *entry = name_to_fn_map->get (name);
      95          154 :   return entry ? *entry : IFN_LAST;
      96              : }
      97              : 
      98              : /* Geven an internal_fn IFN that is a widening function, return its
      99              :    corresponding LO and HI internal_fns.  */
     100              : 
     101              : extern void
     102        71861 : lookup_hilo_internal_fn (internal_fn ifn, internal_fn *lo, internal_fn *hi)
     103              : {
     104        71861 :   gcc_assert (widening_fn_p (ifn));
     105              : 
     106        71861 :   switch (ifn)
     107              :     {
     108            0 :     default:
     109            0 :       gcc_unreachable ();
     110              : #define DEF_INTERNAL_FN(NAME, FLAGS, TYPE)
     111              : #define DEF_INTERNAL_WIDENING_OPTAB_FN(NAME, F, S, SO, UO, T)   \
     112              :     case IFN_##NAME:                                            \
     113              :       *lo = internal_fn (IFN_##NAME##_LO);                      \
     114              :       *hi = internal_fn (IFN_##NAME##_HI);                      \
     115              :       break;
     116              : #include "internal-fn.def"
     117              :     }
     118        71861 : }
     119              : 
     120              : /* Given an internal_fn IFN that is a widening function, return its
     121              :    corresponding _EVEN and _ODD internal_fns in *EVEN and *ODD.  */
     122              : 
     123              : extern void
     124        71861 : lookup_evenodd_internal_fn (internal_fn ifn, internal_fn *even,
     125              :                             internal_fn *odd)
     126              : {
     127        71861 :   gcc_assert (widening_fn_p (ifn));
     128              : 
     129        71861 :   switch (ifn)
     130              :     {
     131            0 :     default:
     132            0 :       gcc_unreachable ();
     133              : #define DEF_INTERNAL_FN(NAME, FLAGS, TYPE)
     134              : #define DEF_INTERNAL_WIDENING_OPTAB_FN(NAME, F, S, SO, UO, T)   \
     135              :     case IFN_##NAME:                                            \
     136              :       *even = internal_fn (IFN_##NAME##_EVEN);                  \
     137              :       *odd = internal_fn (IFN_##NAME##_ODD);                    \
     138              :       break;
     139              : #include "internal-fn.def"
     140              :     }
     141        71861 : }
     142              : 
     143              : 
     144              : /* Fnspec of each internal function, indexed by function number.  */
     145              : const_tree internal_fn_fnspec_array[IFN_LAST + 1];
     146              : 
     147              : void
     148       302660 : init_internal_fns ()
     149              : {
     150              : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
     151              :   if (FNSPEC) internal_fn_fnspec_array[IFN_##CODE] = \
     152              :     build_string ((int) sizeof (FNSPEC) - 1, FNSPEC ? FNSPEC : "");
     153              : #include "internal-fn.def"
     154       302660 :   internal_fn_fnspec_array[IFN_LAST] = 0;
     155       302660 : }
     156              : 
     157              : /* Create static initializers for the information returned by
     158              :    direct_internal_fn.  */
     159              : #define not_direct { -2, -2, false, 0, 0 }
     160              : #define mask_load_direct { -1, 2, false, 4, 4 }
     161              : #define load_lanes_direct { -1, -1, false, 1, 1 }
     162              : #define mask_load_lanes_direct { -1, -1, false, 4, 4 }
     163              : #define mask_len_load_lanes_direct { -1, -1, false, 6, 6 }
     164              : #define gather_load_direct { 3, 1, false, 5, 5 }
     165              : #define mask_gather_load_direct { 3, 1, false, 7, 7 }
     166              : #define mask_len_gather_load_direct { 3, 1, false, 9, 9 }
     167              : #define strided_load_direct { -1, -1, false, 7, 7 }
     168              : #define len_load_direct { -1, -1, false, 5, 5 }
     169              : #define mask_len_load_direct { -1, 4, false, 6, 6 }
     170              : #define mask_store_direct { 3, 2, false, 4, 4 }
     171              : #define store_lanes_direct { 0, 0, false, 1, 1 }
     172              : #define mask_store_lanes_direct { 0, 0, false, 4, 4 }
     173              : #define mask_len_store_lanes_direct { 0, 0, false, 6, 6 }
     174              : #define vec_cond_mask_direct { 1, 0, false, 3, 3 }
     175              : #define vec_cond_mask_len_direct { 1, 1, false, 5, 5 }
     176              : #define vec_cond_direct { 2, 0, false, 5, 5 }
     177              : #define scatter_store_direct { 3, 1, false, 5, 5 }
     178              : #define mask_scatter_store_direct { 3, 1, false, 6, 6 }
     179              : #define mask_len_scatter_store_direct { 3, 1, false, 8, 8 }
     180              : #define strided_store_direct { 1, 1, false, 6, 6 }
     181              : #define len_store_direct { 3, 3, false, 5, 5 }
     182              : #define mask_len_store_direct { 4, 5, false, 6, 6 }
     183              : #define vec_set_direct { 3, 3, false, 3, 3 }
     184              : #define vec_extract_direct { 0, -1, false, 2, 2 }
     185              : #define unary_direct { 0, 0, true, 1, 1 }
     186              : #define unary_opt_direct { 0, 0, true, 1, 2 }
     187              : #define unary_convert_direct { -1, 0, true, 1, 1 }
     188              : #define binary_direct { 0, 0, true, 2, 2 }
     189              : #define ternary_direct { 0, 0, true, 3, 3 }
     190              : #define cond_unary_direct { 1, 1, true, 3, 3 }
     191              : #define cond_binary_direct { 1, 1, true, 4, 4 }
     192              : #define cond_ternary_direct { 1, 1, true, 5, 5 }
     193              : #define cond_len_unary_direct { 1, 1, true, 5, 5 }
     194              : #define cond_len_binary_direct { 1, 1, true, 6, 6 }
     195              : #define cond_len_ternary_direct { 1, 1, true, 7, 7 }
     196              : #define while_direct { 0, 2, false, 3, 3 }
     197              : #define fold_extract_direct { 2, 2, false, 3, 3 }
     198              : #define fold_len_extract_direct { 2, 2, false, 5, 5 }
     199              : #define fold_left_direct { 1, 1, false, 2, 2 }
     200              : #define mask_fold_left_direct { 1, 1, false, 3, 3 }
     201              : #define mask_len_fold_left_direct { 1, 1, false, 5, 5 }
     202              : #define check_ptrs_direct { 0, 0, false, 4, 4 }
     203              : #define crc_direct { 1, -1, true, 3, 3 }
     204              : #define reduc_sbool_direct { 0, 0, true, 1, 1 }
     205              : #define select_vl_direct { 2, 0, false, 3, 3 }
     206              : 
     207              : const direct_internal_fn_info direct_internal_fn_array[IFN_LAST + 1] = {
     208              : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) not_direct,
     209              : #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) TYPE##_direct,
     210              : #define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
     211              :                                      UNSIGNED_OPTAB, TYPE) TYPE##_direct,
     212              : #include "internal-fn.def"
     213              :   not_direct
     214              : };
     215              : 
     216              : /* Like create_output_operand, but for callers that will use
     217              :    assign_call_lhs afterwards.  */
     218              : 
     219              : static void
     220        90443 : create_call_lhs_operand (expand_operand *op, rtx lhs_rtx, machine_mode mode)
     221              : {
     222              :   /* Do not assign directly to a promoted subreg, since there is no
     223              :      guarantee that the instruction will leave the upper bits of the
     224              :      register in the state required by SUBREG_PROMOTED_SIGN.  */
     225        90443 :   rtx dest = lhs_rtx;
     226        90443 :   if (dest && GET_CODE (dest) == SUBREG && SUBREG_PROMOTED_VAR_P (dest))
     227        90443 :     dest = NULL_RTX;
     228        90443 :   create_output_operand (op, dest, mode);
     229        90443 : }
     230              : 
     231              : /* Move the result of an expanded instruction into the lhs of a gimple call.
     232              :    LHS is the lhs of the call, LHS_RTX is its expanded form, and OP is the
     233              :    result of the expanded instruction.  OP should have been set up by
     234              :    create_call_lhs_operand.  */
     235              : 
     236              : static void
     237        90443 : assign_call_lhs (tree lhs, rtx lhs_rtx, expand_operand *op)
     238              : {
     239        90443 :   if (rtx_equal_p (lhs_rtx, op->value))
     240              :     return;
     241              : 
     242              :   /* If the return value has an integral type, convert the instruction
     243              :      result to that type.  This is useful for things that return an
     244              :      int regardless of the size of the input.  If the instruction result
     245              :      is smaller than required, assume that it is signed.
     246              : 
     247              :      If the return value has a nonintegral type, its mode must match
     248              :      the instruction result.  */
     249         3652 :   if (GET_CODE (lhs_rtx) == SUBREG && SUBREG_PROMOTED_VAR_P (lhs_rtx))
     250              :     {
     251              :       /* If this is a scalar in a register that is stored in a wider
     252              :          mode than the declared mode, compute the result into its
     253              :          declared mode and then convert to the wider mode.  */
     254            0 :       gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (lhs)));
     255            0 :       rtx tmp = convert_to_mode (GET_MODE (lhs_rtx), op->value, 0);
     256            0 :       convert_move (SUBREG_REG (lhs_rtx), tmp,
     257            0 :                     SUBREG_PROMOTED_SIGN (lhs_rtx));
     258              :     }
     259         3652 :   else if (GET_MODE (lhs_rtx) == GET_MODE (op->value))
     260           49 :     emit_move_insn (lhs_rtx, op->value);
     261              :   else
     262              :     {
     263         3603 :       gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (lhs)));
     264         3603 :       convert_move (lhs_rtx, op->value, 0);
     265              :     }
     266              : }
     267              : 
     268              : /* Expand STMT using instruction ICODE.  The instruction has NOUTPUTS
     269              :    output operands and NINPUTS input operands, where NOUTPUTS is either
     270              :    0 or 1.  The output operand (if any) comes first, followed by the
     271              :    NINPUTS input operands.  */
     272              : 
     273              : static void
     274        79308 : expand_fn_using_insn (gcall *stmt, insn_code icode, unsigned int noutputs,
     275              :                       unsigned int ninputs)
     276              : {
     277        79308 :   gcc_assert (icode != CODE_FOR_nothing);
     278              : 
     279        79308 :   expand_operand *ops = XALLOCAVEC (expand_operand, noutputs + ninputs);
     280        79308 :   unsigned int opno = 0;
     281        79308 :   rtx lhs_rtx = NULL_RTX;
     282        79308 :   tree lhs = gimple_call_lhs (stmt);
     283              : 
     284        79308 :   if (noutputs)
     285              :     {
     286        79308 :       gcc_assert (noutputs == 1);
     287        79308 :       if (lhs)
     288        79308 :         lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
     289        79308 :       create_call_lhs_operand (&ops[opno], lhs_rtx,
     290        79308 :                                insn_data[icode].operand[opno].mode);
     291        79308 :       opno += 1;
     292              :     }
     293              :   else
     294            0 :     gcc_assert (!lhs);
     295              : 
     296       225351 :   for (unsigned int i = 0; i < ninputs; ++i)
     297              :     {
     298       146043 :       tree rhs = gimple_call_arg (stmt, i);
     299       146043 :       tree rhs_type = TREE_TYPE (rhs);
     300       146043 :       rtx rhs_rtx = expand_normal (rhs);
     301       146043 :       if (INTEGRAL_TYPE_P (rhs_type))
     302         8735 :         create_convert_operand_from (&ops[opno], rhs_rtx,
     303         8735 :                                      TYPE_MODE (rhs_type),
     304         8735 :                                      TYPE_UNSIGNED (rhs_type));
     305       137308 :       else if (TREE_CODE (rhs) == SSA_NAME
     306       112005 :                && SSA_NAME_IS_DEFAULT_DEF (rhs)
     307       141909 :                && VAR_P (SSA_NAME_VAR (rhs)))
     308          249 :         create_undefined_input_operand (&ops[opno], TYPE_MODE (rhs_type));
     309        56327 :       else if (VECTOR_BOOLEAN_TYPE_P (rhs_type)
     310         1579 :                && SCALAR_INT_MODE_P (TYPE_MODE (rhs_type))
     311       138638 :                && maybe_ne (GET_MODE_PRECISION (TYPE_MODE (rhs_type)),
     312         1579 :                             TYPE_VECTOR_SUBPARTS (rhs_type).to_constant ()))
     313              :         {
     314              :           /* Ensure that the vector bitmasks do not have excess bits.  */
     315          427 :           int nunits = TYPE_VECTOR_SUBPARTS (rhs_type).to_constant ();
     316          427 :           rtx tmp = expand_binop (TYPE_MODE (rhs_type), and_optab, rhs_rtx,
     317          427 :                                   GEN_INT ((HOST_WIDE_INT_1U << nunits) - 1),
     318              :                                   NULL_RTX, true, OPTAB_WIDEN);
     319          427 :           create_input_operand (&ops[opno], tmp, TYPE_MODE (rhs_type));
     320              :         }
     321              :       else
     322       136632 :         create_input_operand (&ops[opno], rhs_rtx, TYPE_MODE (rhs_type));
     323       146043 :       opno += 1;
     324              :     }
     325              : 
     326        79308 :   gcc_assert (opno == noutputs + ninputs);
     327        79308 :   expand_insn (icode, opno, ops);
     328        79308 :   if (lhs_rtx)
     329        79308 :     assign_call_lhs (lhs, lhs_rtx, &ops[0]);
     330        79308 : }
     331              : 
     332              : /* ARRAY_TYPE is an array of vector modes.  Return the associated insn
     333              :    for load-lanes-style optab OPTAB, or CODE_FOR_nothing if none.  */
     334              : 
     335              : static enum insn_code
     336            0 : get_multi_vector_move (tree array_type, convert_optab optab)
     337              : {
     338            0 :   machine_mode imode;
     339            0 :   machine_mode vmode;
     340              : 
     341            0 :   gcc_assert (TREE_CODE (array_type) == ARRAY_TYPE);
     342            0 :   imode = TYPE_MODE (array_type);
     343            0 :   vmode = TYPE_MODE (TREE_TYPE (array_type));
     344              : 
     345            0 :   return convert_optab_handler (optab, imode, vmode);
     346              : }
     347              : 
     348              : /* Add mask, else, and len arguments according to the STMT.  */
     349              : 
     350              : static unsigned int
     351         1503 : add_mask_else_and_len_args (expand_operand *ops, unsigned int opno, gcall *stmt)
     352              : {
     353         1503 :   internal_fn ifn = gimple_call_internal_fn (stmt);
     354         1503 :   int len_index = internal_fn_len_index (ifn);
     355              :   /* BIAS is always consecutive next of LEN.  */
     356         1503 :   int bias_index = len_index + 1;
     357         1503 :   int mask_index = internal_fn_mask_index (ifn);
     358              : 
     359              :   /* The order of arguments is always {mask, else, len, bias}.  */
     360         1503 :   if (mask_index >= 0)
     361              :     {
     362         1503 :       tree mask = gimple_call_arg (stmt, mask_index);
     363         1503 :       rtx mask_rtx = expand_normal (mask);
     364              : 
     365         1503 :       tree mask_type = TREE_TYPE (mask);
     366         1503 :       if (VECTOR_BOOLEAN_TYPE_P (mask_type)
     367         1502 :           && SCALAR_INT_MODE_P (TYPE_MODE (mask_type))
     368         2563 :           && maybe_ne (GET_MODE_PRECISION (TYPE_MODE (mask_type)),
     369         2316 :                        TYPE_VECTOR_SUBPARTS (mask_type).to_constant ()))
     370              :         {
     371              :           /* Ensure that the vector bitmasks do not have excess bits.  */
     372          247 :           int nunits = TYPE_VECTOR_SUBPARTS (mask_type).to_constant ();
     373          247 :           mask_rtx = expand_binop (TYPE_MODE (mask_type), and_optab, mask_rtx,
     374          247 :                                    GEN_INT ((HOST_WIDE_INT_1U << nunits) - 1),
     375              :                                    NULL_RTX, true, OPTAB_WIDEN);
     376              :         }
     377              : 
     378         1503 :       create_input_operand (&ops[opno++], mask_rtx,
     379         1503 :                             TYPE_MODE (TREE_TYPE (mask)));
     380              :     }
     381              : 
     382         1503 :   int els_index = internal_fn_else_index (ifn);
     383         1503 :   if (els_index >= 0)
     384              :     {
     385          726 :       tree els = gimple_call_arg (stmt, els_index);
     386          726 :       tree els_type = TREE_TYPE (els);
     387          726 :       if (TREE_CODE (els) == SSA_NAME
     388            0 :           && SSA_NAME_IS_DEFAULT_DEF (els)
     389          726 :           && VAR_P (SSA_NAME_VAR (els)))
     390            0 :         create_undefined_input_operand (&ops[opno++], TYPE_MODE (els_type));
     391              :       else
     392              :         {
     393          726 :           rtx els_rtx = expand_normal (els);
     394          726 :           create_input_operand (&ops[opno++], els_rtx, TYPE_MODE (els_type));
     395              :         }
     396              :     }
     397         1503 :   if (len_index >= 0)
     398              :     {
     399            0 :       tree len = gimple_call_arg (stmt, len_index);
     400            0 :       rtx len_rtx = expand_normal (len);
     401            0 :       create_convert_operand_from (&ops[opno++], len_rtx,
     402            0 :                                    TYPE_MODE (TREE_TYPE (len)),
     403            0 :                                    TYPE_UNSIGNED (TREE_TYPE (len)));
     404            0 :       tree biast = gimple_call_arg (stmt, bias_index);
     405            0 :       rtx bias = expand_normal (biast);
     406            0 :       create_input_operand (&ops[opno++], bias, QImode);
     407              :     }
     408         1503 :   return opno;
     409              : }
     410              : 
     411              : /* Expand LOAD_LANES call STMT using optab OPTAB.  */
     412              : 
     413              : static void
     414            0 : expand_load_lanes_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
     415              : {
     416            0 :   class expand_operand ops[2];
     417            0 :   tree type, lhs, rhs;
     418            0 :   rtx target, mem;
     419              : 
     420            0 :   lhs = gimple_call_lhs (stmt);
     421            0 :   rhs = gimple_call_arg (stmt, 0);
     422            0 :   type = TREE_TYPE (lhs);
     423              : 
     424            0 :   target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
     425            0 :   mem = expand_normal (rhs);
     426              : 
     427            0 :   gcc_assert (MEM_P (mem));
     428            0 :   PUT_MODE (mem, TYPE_MODE (type));
     429              : 
     430            0 :   create_call_lhs_operand (&ops[0], target, TYPE_MODE (type));
     431            0 :   create_fixed_operand (&ops[1], mem);
     432            0 :   expand_insn (get_multi_vector_move (type, optab), 2, ops);
     433            0 :   assign_call_lhs (lhs, target, &ops[0]);
     434            0 : }
     435              : 
     436              : /* Expand STORE_LANES call STMT using optab OPTAB.  */
     437              : 
     438              : static void
     439            0 : expand_store_lanes_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
     440              : {
     441            0 :   class expand_operand ops[2];
     442            0 :   tree type, lhs, rhs;
     443            0 :   rtx target, reg;
     444              : 
     445            0 :   lhs = gimple_call_lhs (stmt);
     446            0 :   rhs = gimple_call_arg (stmt, 0);
     447            0 :   type = TREE_TYPE (rhs);
     448              : 
     449            0 :   target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
     450            0 :   reg = expand_normal (rhs);
     451              : 
     452            0 :   gcc_assert (MEM_P (target));
     453            0 :   PUT_MODE (target, TYPE_MODE (type));
     454              : 
     455            0 :   create_fixed_operand (&ops[0], target);
     456            0 :   create_input_operand (&ops[1], reg, TYPE_MODE (type));
     457            0 :   expand_insn (get_multi_vector_move (type, optab), 2, ops);
     458            0 : }
     459              : 
     460              : static void
     461            0 : expand_ANNOTATE (internal_fn, gcall *)
     462              : {
     463            0 :   gcc_unreachable ();
     464              : }
     465              : 
     466              : /* This should get expanded in omp_device_lower pass.  */
     467              : 
     468              : static void
     469            0 : expand_GOMP_USE_SIMT (internal_fn, gcall *)
     470              : {
     471            0 :   gcc_unreachable ();
     472              : }
     473              : 
     474              : /* This should get expanded in omp_device_lower pass.  */
     475              : 
     476              : static void
     477            0 : expand_GOMP_SIMT_ENTER (internal_fn, gcall *)
     478              : {
     479            0 :   gcc_unreachable ();
     480              : }
     481              : 
     482              : /* Allocate per-lane storage and begin non-uniform execution region.  */
     483              : 
     484              : static void
     485            0 : expand_GOMP_SIMT_ENTER_ALLOC (internal_fn, gcall *stmt)
     486              : {
     487            0 :   rtx target;
     488            0 :   tree lhs = gimple_call_lhs (stmt);
     489            0 :   if (lhs)
     490            0 :     target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
     491              :   else
     492            0 :     target = gen_reg_rtx (Pmode);
     493            0 :   rtx size = expand_normal (gimple_call_arg (stmt, 0));
     494            0 :   rtx align = expand_normal (gimple_call_arg (stmt, 1));
     495            0 :   class expand_operand ops[3];
     496            0 :   create_call_lhs_operand (&ops[0], target, Pmode);
     497            0 :   create_input_operand (&ops[1], size, Pmode);
     498            0 :   create_input_operand (&ops[2], align, Pmode);
     499            0 :   gcc_assert (targetm.have_omp_simt_enter ());
     500            0 :   expand_insn (targetm.code_for_omp_simt_enter, 3, ops);
     501            0 :   assign_call_lhs (lhs, target, &ops[0]);
     502            0 : }
     503              : 
     504              : /* Deallocate per-lane storage and leave non-uniform execution region.  */
     505              : 
     506              : static void
     507            0 : expand_GOMP_SIMT_EXIT (internal_fn, gcall *stmt)
     508              : {
     509            0 :   gcc_checking_assert (!gimple_call_lhs (stmt));
     510            0 :   rtx arg = expand_normal (gimple_call_arg (stmt, 0));
     511            0 :   class expand_operand ops[1];
     512            0 :   create_input_operand (&ops[0], arg, Pmode);
     513            0 :   gcc_assert (targetm.have_omp_simt_exit ());
     514            0 :   expand_insn (targetm.code_for_omp_simt_exit, 1, ops);
     515            0 : }
     516              : 
     517              : /* Lane index on SIMT targets: thread index in the warp on NVPTX.  On targets
     518              :    without SIMT execution this should be expanded in omp_device_lower pass.  */
     519              : 
     520              : static void
     521            0 : expand_GOMP_SIMT_LANE (internal_fn, gcall *stmt)
     522              : {
     523            0 :   tree lhs = gimple_call_lhs (stmt);
     524            0 :   if (!lhs)
     525              :     return;
     526              : 
     527            0 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
     528            0 :   gcc_assert (targetm.have_omp_simt_lane ());
     529            0 :   emit_insn (targetm.gen_omp_simt_lane (target));
     530              : }
     531              : 
     532              : /* This should get expanded in omp_device_lower pass.  */
     533              : 
     534              : static void
     535            0 : expand_GOMP_SIMT_VF (internal_fn, gcall *)
     536              : {
     537            0 :   gcc_unreachable ();
     538              : }
     539              : 
     540              : /* This should get expanded in omp_device_lower pass.  */
     541              : 
     542              : static void
     543            0 : expand_GOMP_MAX_VF (internal_fn, gcall *)
     544              : {
     545            0 :   gcc_unreachable ();
     546              : }
     547              : 
     548              : /* This should get expanded in omp_device_lower pass.  */
     549              : 
     550              : static void
     551            0 : expand_GOMP_TARGET_REV (internal_fn, gcall *)
     552              : {
     553            0 :   gcc_unreachable ();
     554              : }
     555              : 
     556              : /* Lane index of the first SIMT lane that supplies a non-zero argument.
     557              :    This is a SIMT counterpart to GOMP_SIMD_LAST_LANE, used to represent the
     558              :    lane that executed the last iteration for handling OpenMP lastprivate.  */
     559              : 
     560              : static void
     561            0 : expand_GOMP_SIMT_LAST_LANE (internal_fn, gcall *stmt)
     562              : {
     563            0 :   tree lhs = gimple_call_lhs (stmt);
     564            0 :   if (!lhs)
     565            0 :     return;
     566              : 
     567            0 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
     568            0 :   rtx cond = expand_normal (gimple_call_arg (stmt, 0));
     569            0 :   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
     570            0 :   class expand_operand ops[2];
     571            0 :   create_call_lhs_operand (&ops[0], target, mode);
     572            0 :   create_input_operand (&ops[1], cond, mode);
     573            0 :   gcc_assert (targetm.have_omp_simt_last_lane ());
     574            0 :   expand_insn (targetm.code_for_omp_simt_last_lane, 2, ops);
     575            0 :   assign_call_lhs (lhs, target, &ops[0]);
     576              : }
     577              : 
     578              : /* Non-transparent predicate used in SIMT lowering of OpenMP "ordered".  */
     579              : 
     580              : static void
     581            0 : expand_GOMP_SIMT_ORDERED_PRED (internal_fn, gcall *stmt)
     582              : {
     583            0 :   tree lhs = gimple_call_lhs (stmt);
     584            0 :   if (!lhs)
     585            0 :     return;
     586              : 
     587            0 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
     588            0 :   rtx ctr = expand_normal (gimple_call_arg (stmt, 0));
     589            0 :   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
     590            0 :   class expand_operand ops[2];
     591            0 :   create_call_lhs_operand (&ops[0], target, mode);
     592            0 :   create_input_operand (&ops[1], ctr, mode);
     593            0 :   gcc_assert (targetm.have_omp_simt_ordered ());
     594            0 :   expand_insn (targetm.code_for_omp_simt_ordered, 2, ops);
     595            0 :   assign_call_lhs (lhs, target, &ops[0]);
     596              : }
     597              : 
     598              : /* "Or" boolean reduction across SIMT lanes: return non-zero in all lanes if
     599              :    any lane supplies a non-zero argument.  */
     600              : 
     601              : static void
     602            0 : expand_GOMP_SIMT_VOTE_ANY (internal_fn, gcall *stmt)
     603              : {
     604            0 :   tree lhs = gimple_call_lhs (stmt);
     605            0 :   if (!lhs)
     606            0 :     return;
     607              : 
     608            0 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
     609            0 :   rtx cond = expand_normal (gimple_call_arg (stmt, 0));
     610            0 :   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
     611            0 :   class expand_operand ops[2];
     612            0 :   create_call_lhs_operand (&ops[0], target, mode);
     613            0 :   create_input_operand (&ops[1], cond, mode);
     614            0 :   gcc_assert (targetm.have_omp_simt_vote_any ());
     615            0 :   expand_insn (targetm.code_for_omp_simt_vote_any, 2, ops);
     616            0 :   assign_call_lhs (lhs, target, &ops[0]);
     617              : }
     618              : 
     619              : /* Exchange between SIMT lanes with a "butterfly" pattern: source lane index
     620              :    is destination lane index XOR given offset.  */
     621              : 
     622              : static void
     623            0 : expand_GOMP_SIMT_XCHG_BFLY (internal_fn, gcall *stmt)
     624              : {
     625            0 :   tree lhs = gimple_call_lhs (stmt);
     626            0 :   if (!lhs)
     627            0 :     return;
     628              : 
     629            0 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
     630            0 :   rtx src = expand_normal (gimple_call_arg (stmt, 0));
     631            0 :   rtx idx = expand_normal (gimple_call_arg (stmt, 1));
     632            0 :   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
     633            0 :   class expand_operand ops[3];
     634            0 :   create_call_lhs_operand (&ops[0], target, mode);
     635            0 :   create_input_operand (&ops[1], src, mode);
     636            0 :   create_input_operand (&ops[2], idx, SImode);
     637            0 :   gcc_assert (targetm.have_omp_simt_xchg_bfly ());
     638            0 :   expand_insn (targetm.code_for_omp_simt_xchg_bfly, 3, ops);
     639            0 :   assign_call_lhs (lhs, target, &ops[0]);
     640              : }
     641              : 
     642              : /* Exchange between SIMT lanes according to given source lane index.  */
     643              : 
     644              : static void
     645            0 : expand_GOMP_SIMT_XCHG_IDX (internal_fn, gcall *stmt)
     646              : {
     647            0 :   tree lhs = gimple_call_lhs (stmt);
     648            0 :   if (!lhs)
     649            0 :     return;
     650              : 
     651            0 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
     652            0 :   rtx src = expand_normal (gimple_call_arg (stmt, 0));
     653            0 :   rtx idx = expand_normal (gimple_call_arg (stmt, 1));
     654            0 :   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
     655            0 :   class expand_operand ops[3];
     656            0 :   create_call_lhs_operand (&ops[0], target, mode);
     657            0 :   create_input_operand (&ops[1], src, mode);
     658            0 :   create_input_operand (&ops[2], idx, SImode);
     659            0 :   gcc_assert (targetm.have_omp_simt_xchg_idx ());
     660            0 :   expand_insn (targetm.code_for_omp_simt_xchg_idx, 3, ops);
     661            0 :   assign_call_lhs (lhs, target, &ops[0]);
     662              : }
     663              : 
     664              : /* This should get expanded in adjust_simduid_builtins.  */
     665              : 
     666              : static void
     667            0 : expand_GOMP_SIMD_LANE (internal_fn, gcall *)
     668              : {
     669            0 :   gcc_unreachable ();
     670              : }
     671              : 
     672              : /* This should get expanded in adjust_simduid_builtins.  */
     673              : 
     674              : static void
     675            0 : expand_GOMP_SIMD_VF (internal_fn, gcall *)
     676              : {
     677            0 :   gcc_unreachable ();
     678              : }
     679              : 
     680              : /* This should get expanded in adjust_simduid_builtins.  */
     681              : 
     682              : static void
     683            0 : expand_GOMP_SIMD_LAST_LANE (internal_fn, gcall *)
     684              : {
     685            0 :   gcc_unreachable ();
     686              : }
     687              : 
     688              : /* This should get expanded in adjust_simduid_builtins.  */
     689              : 
     690              : static void
     691            0 : expand_GOMP_SIMD_ORDERED_START (internal_fn, gcall *)
     692              : {
     693            0 :   gcc_unreachable ();
     694              : }
     695              : 
     696              : /* This should get expanded in adjust_simduid_builtins.  */
     697              : 
     698              : static void
     699            0 : expand_GOMP_SIMD_ORDERED_END (internal_fn, gcall *)
     700              : {
     701            0 :   gcc_unreachable ();
     702              : }
     703              : 
     704              : /* This should get expanded in gimplify_omp_dispatch.  */
     705              : 
     706              : static void
     707            0 : expand_GOMP_DISPATCH (internal_fn, gcall *)
     708              : {
     709            0 :   gcc_unreachable ();
     710              : }
     711              : 
     712              : /* This should get expanded in the sanopt pass.  */
     713              : 
     714              : static void
     715            0 : expand_UBSAN_NULL (internal_fn, gcall *)
     716              : {
     717            0 :   gcc_unreachable ();
     718              : }
     719              : 
     720              : /* This should get expanded in the sanopt pass.  */
     721              : 
     722              : static void
     723            0 : expand_UBSAN_BOUNDS (internal_fn, gcall *)
     724              : {
     725            0 :   gcc_unreachable ();
     726              : }
     727              : 
     728              : /* This should get expanded in the sanopt pass.  */
     729              : 
     730              : static void
     731            0 : expand_UBSAN_VPTR (internal_fn, gcall *)
     732              : {
     733            0 :   gcc_unreachable ();
     734              : }
     735              : 
     736              : /* This should get expanded in the sanopt pass.  */
     737              : 
     738              : static void
     739            0 : expand_UBSAN_PTR (internal_fn, gcall *)
     740              : {
     741            0 :   gcc_unreachable ();
     742              : }
     743              : 
     744              : /* This should get expanded in the sanopt pass.  */
     745              : 
     746              : static void
     747            0 : expand_UBSAN_OBJECT_SIZE (internal_fn, gcall *)
     748              : {
     749            0 :   gcc_unreachable ();
     750              : }
     751              : 
     752              : /* This should get expanded in the sanopt pass.  */
     753              : 
     754              : static void
     755            0 : expand_HWASAN_CHECK (internal_fn, gcall *)
     756              : {
     757            0 :   gcc_unreachable ();
     758              : }
     759              : 
     760              : /* For hwasan stack tagging:
     761              :    Tag memory which is dynamically allocated.  */
     762              : static void
     763            0 : expand_HWASAN_ALLOCA_POISON (internal_fn, gcall *gc)
     764              : {
     765            0 :   gcc_assert (ptr_mode == Pmode);
     766            0 :   tree g_target = gimple_call_lhs (gc);
     767            0 :   tree g_ptr = gimple_call_arg (gc, 0);
     768            0 :   tree g_size = gimple_call_arg (gc, 1);
     769              : 
     770              :   /* There is no target; this happens, usually, when we have an alloca of zero
     771              :      size.  */
     772            0 :   if (!g_target)
     773              :     return;
     774            0 :   rtx target = expand_normal (g_target);
     775            0 :   rtx ptr = expand_normal (g_ptr);
     776            0 :   rtx size = expand_normal (g_size);
     777              : 
     778              :   /* No size, nothing to do.  */
     779            0 :   if (size == const0_rtx)
     780              :     return;
     781              : 
     782              :   /* Get new tag for the alloca'd memory.
     783              :      Doing a regular add_tag () like so:
     784              :         rtx tag = targetm.memtag.add_tag (hwasan_frame_base (), 0,
     785              :                                           hwasan_current_frame_tag ());
     786              :      gets a new tag, which can be used for tagging memory.  But for alloca, we
     787              :      need both tagged memory and a tagged pointer to pass to consumers.  Invoke
     788              :      insert_random_tag () instead to add a random tag to ptr to get a tagged
     789              :      pointer that will work for both purposes.  */
     790            0 :   rtx tagged_ptr
     791            0 :     = force_reg (Pmode, targetm.memtag.insert_random_tag (ptr, NULL_RTX));
     792            0 :   rtx tag = targetm.memtag.extract_tag (tagged_ptr, NULL_RTX);
     793              : 
     794            0 :   if (memtag_sanitize_p ())
     795              :     {
     796              :       /* Need to put the tagged ptr into the `target` RTX for consumers
     797              :          of alloca'd memory.  */
     798            0 :       if (tagged_ptr != target)
     799            0 :         emit_move_insn (target, tagged_ptr);
     800              :       /* Tag the memory.  */
     801            0 :       emit_insn (targetm.gen_tag_memory (ptr, tag, size));
     802            0 :       hwasan_increment_frame_tag ();
     803              :     }
     804              :   else
     805            0 :     gcc_unreachable ();
     806              : }
     807              : 
     808              : /* For hwasan stack tagging:
     809              :    Clear tags on the dynamically allocated space.
     810              :    For use after an object dynamically allocated on the stack goes out of
     811              :    scope.  */
     812              : static void
     813            0 : expand_HWASAN_ALLOCA_UNPOISON (internal_fn, gcall *gc)
     814              : {
     815            0 :   gcc_assert (Pmode == ptr_mode);
     816            0 :   tree restored_position = gimple_call_arg (gc, 0);
     817            0 :   rtx restored_rtx = expand_expr (restored_position, NULL_RTX, VOIDmode,
     818              :                                   EXPAND_NORMAL);
     819            0 :   rtx off = expand_simple_binop (Pmode, MINUS, restored_rtx,
     820              :                                  stack_pointer_rtx, NULL_RTX, 0,
     821              :                                  OPTAB_WIDEN);
     822              : 
     823            0 :   if (memtag_sanitize_p ())
     824            0 :     emit_insn (targetm.gen_tag_memory (virtual_stack_dynamic_rtx,
     825              :                                        HWASAN_STACK_BACKGROUND,
     826              :                                        off));
     827              :   else
     828              :     {
     829            0 :       rtx func = init_one_libfunc ("__hwasan_tag_memory");
     830            0 :       emit_library_call_value (func, NULL_RTX, LCT_NORMAL, VOIDmode,
     831            0 :                                virtual_stack_dynamic_rtx, Pmode,
     832              :                                HWASAN_STACK_BACKGROUND, QImode,
     833            0 :                                off, Pmode);
     834              :     }
     835            0 : }
     836              : 
     837              : /* For hwasan stack tagging:
     838              :    Return a tag to be used for a dynamic allocation.  */
     839              : static void
     840            0 : expand_HWASAN_CHOOSE_TAG (internal_fn, gcall *gc)
     841              : {
     842            0 :   tree tag = gimple_call_lhs (gc);
     843            0 :   rtx target = expand_expr (tag, NULL_RTX, VOIDmode, EXPAND_NORMAL);
     844            0 :   machine_mode mode = GET_MODE (target);
     845            0 :   gcc_assert (mode == QImode);
     846              : 
     847            0 :   rtx base_tag = targetm.memtag.extract_tag (hwasan_frame_base (), NULL_RTX);
     848            0 :   gcc_assert (base_tag);
     849            0 :   rtx tag_offset = gen_int_mode (hwasan_current_frame_tag (), QImode);
     850            0 :   rtx chosen_tag = expand_simple_binop (QImode, PLUS, base_tag, tag_offset,
     851              :                                         target, /* unsignedp = */1,
     852              :                                         OPTAB_WIDEN);
     853            0 :   chosen_tag = hwasan_truncate_to_tag_size (chosen_tag, target);
     854              : 
     855              :   /* Really need to put the tag into the `target` RTX.  */
     856            0 :   if (chosen_tag != target)
     857              :     {
     858            0 :       rtx temp = chosen_tag;
     859            0 :       gcc_assert (GET_MODE (chosen_tag) == mode);
     860            0 :       emit_move_insn (target, temp);
     861              :     }
     862              : 
     863            0 :   hwasan_increment_frame_tag ();
     864            0 : }
     865              : 
     866              : /* For hwasan stack tagging:
     867              :    Tag a region of space in the shadow stack according to the base pointer of
     868              :    an object on the stack.  N.b. the length provided in the internal call is
     869              :    required to be aligned to HWASAN_TAG_GRANULE_SIZE.  */
     870              : static void
     871            0 : expand_HWASAN_MARK (internal_fn, gcall *gc)
     872              : {
     873            0 :   gcc_assert (ptr_mode == Pmode);
     874            0 :   HOST_WIDE_INT flag = tree_to_shwi (gimple_call_arg (gc, 0));
     875            0 :   bool is_poison = ((asan_mark_flags)flag) == ASAN_MARK_POISON;
     876              : 
     877            0 :   tree base = gimple_call_arg (gc, 1);
     878            0 :   gcc_checking_assert (TREE_CODE (base) == ADDR_EXPR);
     879            0 :   rtx base_rtx = expand_normal (base);
     880              : 
     881            0 :   rtx tag = is_poison ? HWASAN_STACK_BACKGROUND
     882            0 :     : targetm.memtag.extract_tag (base_rtx, NULL_RTX);
     883            0 :   rtx address = targetm.memtag.untagged_pointer (base_rtx, NULL_RTX);
     884              : 
     885            0 :   tree len = gimple_call_arg (gc, 2);
     886            0 :   rtx r_len = expand_normal (len);
     887              : 
     888            0 :   if (memtag_sanitize_p ())
     889            0 :     emit_insn (targetm.gen_tag_memory (address, tag, r_len));
     890              :   else
     891              :     {
     892            0 :       rtx func = init_one_libfunc ("__hwasan_tag_memory");
     893            0 :       emit_library_call (func, LCT_NORMAL, VOIDmode, address, Pmode,
     894            0 :                          tag, QImode, r_len, Pmode);
     895              :     }
     896            0 : }
     897              : 
     898              : /* For hwasan stack tagging:
     899              :    Store a tag into a pointer.  */
     900              : static void
     901            0 : expand_HWASAN_SET_TAG (internal_fn, gcall *gc)
     902              : {
     903            0 :   gcc_assert (ptr_mode == Pmode);
     904            0 :   tree g_target = gimple_call_lhs (gc);
     905            0 :   tree g_ptr = gimple_call_arg (gc, 0);
     906            0 :   tree g_tag = gimple_call_arg (gc, 1);
     907              : 
     908            0 :   rtx ptr = expand_normal (g_ptr);
     909            0 :   rtx tag = expand_expr (g_tag, NULL_RTX, QImode, EXPAND_NORMAL);
     910            0 :   rtx target = expand_normal (g_target);
     911              : 
     912            0 :   rtx untagged = targetm.memtag.untagged_pointer (ptr, target);
     913            0 :   rtx tagged_value = targetm.memtag.set_tag (untagged, tag, target);
     914            0 :   if (tagged_value != target)
     915            0 :     emit_move_insn (target, tagged_value);
     916            0 : }
     917              : 
     918              : /* This should get expanded in the sanopt pass.  */
     919              : 
     920              : static void
     921            0 : expand_ASAN_CHECK (internal_fn, gcall *)
     922              : {
     923            0 :   gcc_unreachable ();
     924              : }
     925              : 
     926              : /* This should get expanded in the sanopt pass.  */
     927              : 
     928              : static void
     929            0 : expand_ASAN_MARK (internal_fn, gcall *)
     930              : {
     931            0 :   gcc_unreachable ();
     932              : }
     933              : 
     934              : /* This should get expanded in the sanopt pass.  */
     935              : 
     936              : static void
     937            0 : expand_ASAN_POISON (internal_fn, gcall *)
     938              : {
     939            0 :   gcc_unreachable ();
     940              : }
     941              : 
     942              : /* This should get expanded in the sanopt pass.  */
     943              : 
     944              : static void
     945            0 : expand_ASAN_POISON_USE (internal_fn, gcall *)
     946              : {
     947            0 :   gcc_unreachable ();
     948              : }
     949              : 
     950              : /* This should get expanded in the tsan pass.  */
     951              : 
     952              : static void
     953            0 : expand_TSAN_FUNC_EXIT (internal_fn, gcall *)
     954              : {
     955            0 :   gcc_unreachable ();
     956              : }
     957              : 
     958              : /* This should get expanded in the lower pass.  */
     959              : 
     960              : static void
     961           25 : expand_FALLTHROUGH (internal_fn, gcall *call)
     962              : {
     963           25 :   auto_urlify_attributes sentinel;
     964           25 :   error_at (gimple_location (call),
     965              :             "invalid use of attribute %<fallthrough%>");
     966           25 : }
     967              : 
     968              : /* Return minimum precision needed to represent all values
     969              :    of ARG in SIGNed integral type.  */
     970              : 
     971              : static int
     972       157856 : get_min_precision (tree arg, signop sign)
     973              : {
     974       157856 :   int prec = TYPE_PRECISION (TREE_TYPE (arg));
     975       157856 :   int cnt = 0;
     976       157856 :   signop orig_sign = sign;
     977       157856 :   if (TREE_CODE (arg) == INTEGER_CST)
     978              :     {
     979        40791 :       int p;
     980        40791 :       if (TYPE_SIGN (TREE_TYPE (arg)) != sign)
     981              :         {
     982        14623 :           widest_int w = wi::to_widest (arg);
     983        14623 :           w = wi::ext (w, prec, sign);
     984        14623 :           p = wi::min_precision (w, sign);
     985        14623 :         }
     986              :       else
     987        26168 :         p = wi::min_precision (wi::to_wide (arg), sign);
     988        40791 :       return MIN (p, prec);
     989              :     }
     990       117020 :   while (CONVERT_EXPR_P (arg)
     991         7371 :          && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
     992       131762 :          && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
     993              :     {
     994         7371 :       arg = TREE_OPERAND (arg, 0);
     995         7371 :       if (TYPE_PRECISION (TREE_TYPE (arg)) < prec)
     996              :         {
     997         1109 :           if (TYPE_UNSIGNED (TREE_TYPE (arg)))
     998              :             sign = UNSIGNED;
     999          805 :           else if (sign == UNSIGNED
    1000          805 :                    && (get_range_pos_neg (arg,
    1001              :                                           currently_expanding_gimple_stmt)
    1002              :                        != 1))
    1003           45 :             return prec + (orig_sign != sign);
    1004         1064 :           prec = TYPE_PRECISION (TREE_TYPE (arg));
    1005              :         }
    1006         7326 :       if (++cnt > 30)
    1007            0 :         return prec + (orig_sign != sign);
    1008              :     }
    1009       117020 :   if (CONVERT_EXPR_P (arg)
    1010            0 :       && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
    1011       117020 :       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) > prec)
    1012              :     {
    1013              :       /* We have e.g. (unsigned short) y_2 where int y_2 = (int) x_1(D);
    1014              :          If y_2's min precision is smaller than prec, return that.  */
    1015            0 :       int oprec = get_min_precision (TREE_OPERAND (arg, 0), sign);
    1016            0 :       if (oprec < prec)
    1017            0 :         return oprec + (orig_sign != sign);
    1018              :     }
    1019       117020 :   if (TREE_CODE (arg) != SSA_NAME)
    1020            0 :     return prec + (orig_sign != sign);
    1021       117020 :   int_range_max r;
    1022       117020 :   gimple *cg = currently_expanding_gimple_stmt;
    1023       239631 :   while (!get_range_query (cfun)->range_of_expr (r, arg, cg)
    1024       122611 :          || r.varying_p ()
    1025       254607 :          || r.undefined_p ())
    1026              :     {
    1027       113227 :       auto *g = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (arg));
    1028        53812 :       if (g && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
    1029              :         {
    1030         6701 :           tree t = gimple_assign_rhs1 (g);
    1031        13402 :           if (INTEGRAL_TYPE_P (TREE_TYPE (t))
    1032        12405 :               && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
    1033              :             {
    1034         5598 :               arg = t;
    1035         5598 :               if (TYPE_PRECISION (TREE_TYPE (arg)) < prec)
    1036              :                 {
    1037         5209 :                   if (TYPE_UNSIGNED (TREE_TYPE (arg)))
    1038              :                     sign = UNSIGNED;
    1039         2885 :                   else if (sign == UNSIGNED
    1040         2885 :                            && get_range_pos_neg (arg, g) != 1)
    1041            7 :                     return prec + (orig_sign != sign);
    1042         5202 :                   prec = TYPE_PRECISION (TREE_TYPE (arg));
    1043              :                 }
    1044         5591 :               if (++cnt > 30)
    1045            0 :                 return prec + (orig_sign != sign);
    1046         5591 :               continue;
    1047              :             }
    1048              :         }
    1049       107629 :       return prec + (orig_sign != sign);
    1050              :     }
    1051         9384 :   if (sign == TYPE_SIGN (TREE_TYPE (arg)))
    1052              :     {
    1053         8802 :       int p1 = wi::min_precision (r.lower_bound (), sign);
    1054         8802 :       int p2 = wi::min_precision (r.upper_bound (), sign);
    1055         8802 :       p1 = MAX (p1, p2);
    1056         8802 :       prec = MIN (prec, p1);
    1057              :     }
    1058         1140 :   else if (sign == UNSIGNED && !wi::neg_p (r.lower_bound (), SIGNED))
    1059              :     {
    1060          554 :       int p = wi::min_precision (r.upper_bound (), UNSIGNED);
    1061          582 :       prec = MIN (prec, p);
    1062              :     }
    1063         9384 :   return prec + (orig_sign != sign);
    1064       117020 : }
    1065              : 
    1066              : /* Helper for expand_*_overflow.  Set the __imag__ part to true
    1067              :    (1 except for signed:1 type, in which case store -1).  */
    1068              : 
    1069              : static void
    1070        82361 : expand_arith_set_overflow (tree lhs, rtx target)
    1071              : {
    1072        82361 :   if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (lhs))) == 1
    1073        82361 :       && !TYPE_UNSIGNED (TREE_TYPE (TREE_TYPE (lhs))))
    1074          202 :     write_complex_part (target, constm1_rtx, true, false);
    1075              :   else
    1076        82159 :     write_complex_part (target, const1_rtx, true, false);
    1077        82361 : }
    1078              : 
    1079              : /* Helper for expand_*_overflow.  Store RES into the __real__ part
    1080              :    of TARGET.  If RES has larger MODE than __real__ part of TARGET,
    1081              :    set the __imag__ part to 1 if RES doesn't fit into it.  Similarly
    1082              :    if LHS has smaller precision than its mode, unless OVERFLOW_CHECKED
    1083              :    is true in which case the caller has already performed the
    1084              :    overflow check and recorded the result.  */
    1085              : 
    1086              : static void
    1087        72277 : expand_arith_overflow_result_store (tree lhs, rtx target,
    1088              :                                     scalar_int_mode mode, rtx res,
    1089              :                                     bool overflow_checked = false)
    1090              : {
    1091        72277 :   scalar_int_mode tgtmode
    1092       144554 :     = as_a <scalar_int_mode> (GET_MODE_INNER (GET_MODE (target)));
    1093        72277 :   rtx lres = res;
    1094        72277 :   if (tgtmode != mode)
    1095              :     {
    1096        18892 :       rtx_code_label *done_label = gen_label_rtx ();
    1097        18892 :       int uns = TYPE_UNSIGNED (TREE_TYPE (TREE_TYPE (lhs)));
    1098        18892 :       lres = convert_modes (tgtmode, mode, res, uns);
    1099        18892 :       gcc_assert (GET_MODE_PRECISION (tgtmode) < GET_MODE_PRECISION (mode));
    1100        18892 :       do_compare_rtx_and_jump (res, convert_modes (mode, tgtmode, lres, uns),
    1101              :                                EQ, true, mode, NULL_RTX, NULL, done_label,
    1102              :                                profile_probability::very_likely ());
    1103        18892 :       expand_arith_set_overflow (lhs, target);
    1104        18892 :       emit_label (done_label);
    1105              :     }
    1106        72277 :   int prec = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (lhs)));
    1107        72277 :   int tgtprec = GET_MODE_PRECISION (tgtmode);
    1108        72277 :   if (prec < tgtprec && !overflow_checked)
    1109              :     {
    1110         3569 :       rtx_code_label *done_label = gen_label_rtx ();
    1111         3569 :       int uns = TYPE_UNSIGNED (TREE_TYPE (TREE_TYPE (lhs)));
    1112         3569 :       res = lres;
    1113         3569 :       if (uns)
    1114              :         {
    1115         2024 :           rtx mask
    1116         2024 :             = immed_wide_int_const (wi::shifted_mask (0, prec, false, tgtprec),
    1117              :                                     tgtmode);
    1118         2024 :           lres = expand_simple_binop (tgtmode, AND, res, mask, NULL_RTX,
    1119              :                                       true, OPTAB_LIB_WIDEN);
    1120              :         }
    1121              :       else
    1122              :         {
    1123         1545 :           lres = expand_shift (LSHIFT_EXPR, tgtmode, res, tgtprec - prec,
    1124              :                                NULL_RTX, 1);
    1125         1545 :           lres = expand_shift (RSHIFT_EXPR, tgtmode, lres, tgtprec - prec,
    1126              :                                NULL_RTX, 0);
    1127              :         }
    1128         3569 :       do_compare_rtx_and_jump (res, lres,
    1129              :                                EQ, true, tgtmode, NULL_RTX, NULL, done_label,
    1130              :                                profile_probability::very_likely ());
    1131         3569 :       expand_arith_set_overflow (lhs, target);
    1132         3569 :       emit_label (done_label);
    1133              :     }
    1134        68708 :   else if (prec < tgtprec)
    1135              :     {
    1136              :       /* overflow_checked: the caller already detected and recorded overflow
    1137              :          via the hipart comparison.  */
    1138            0 :       int uns = TYPE_UNSIGNED (TREE_TYPE (TREE_TYPE (lhs)));
    1139            0 :       res = lres;
    1140            0 :       if (uns)
    1141              :         {
    1142            0 :           rtx mask
    1143            0 :             = immed_wide_int_const (wi::shifted_mask (0, prec, false, tgtprec),
    1144              :                                     tgtmode);
    1145            0 :           lres = expand_simple_binop (tgtmode, AND, res, mask, NULL_RTX,
    1146              :                                       true, OPTAB_LIB_WIDEN);
    1147              :         }
    1148              :       else
    1149              :         {
    1150            0 :           lres = expand_shift (LSHIFT_EXPR, tgtmode, res, tgtprec - prec,
    1151              :                                NULL_RTX, 1);
    1152            0 :           lres = expand_shift (RSHIFT_EXPR, tgtmode, lres, tgtprec - prec,
    1153              :                                NULL_RTX, 0);
    1154              :         }
    1155              :     }
    1156        72277 :   write_complex_part (target, lres, false, false);
    1157        72277 : }
    1158              : 
    1159              : /* Helper for expand_*_overflow.  Store RES into TARGET.  */
    1160              : 
    1161              : static void
    1162         4759 : expand_ubsan_result_store (tree lhs, rtx target, scalar_int_mode mode,
    1163              :                            rtx res, rtx_code_label *do_error)
    1164              : {
    1165         9464 :   if (BITINT_TYPE_P (TREE_TYPE (lhs))
    1166         4759 :       && TYPE_PRECISION (TREE_TYPE (lhs)) < GET_MODE_PRECISION (mode))
    1167              :     {
    1168           54 :       int uns = TYPE_UNSIGNED (TREE_TYPE (lhs));
    1169           54 :       int prec = TYPE_PRECISION (TREE_TYPE (lhs));
    1170           54 :       int tgtprec = GET_MODE_PRECISION (mode);
    1171           54 :       rtx resc = gen_reg_rtx (mode), lres;
    1172           54 :       emit_move_insn (resc, res);
    1173           54 :       if (uns)
    1174              :         {
    1175            0 :           rtx mask
    1176            0 :             = immed_wide_int_const (wi::shifted_mask (0, prec, false, tgtprec),
    1177              :                                     mode);
    1178            0 :           lres = expand_simple_binop (mode, AND, res, mask, NULL_RTX,
    1179              :                                       true, OPTAB_LIB_WIDEN);
    1180              :         }
    1181              :       else
    1182              :         {
    1183           54 :           lres = expand_shift (LSHIFT_EXPR, mode, res, tgtprec - prec,
    1184              :                                NULL_RTX, 1);
    1185           54 :           lres = expand_shift (RSHIFT_EXPR, mode, lres, tgtprec - prec,
    1186              :                                NULL_RTX, 0);
    1187              :         }
    1188           54 :       if (lres != res)
    1189           54 :         emit_move_insn (res, lres);
    1190           54 :       do_compare_rtx_and_jump (res, resc,
    1191              :                                NE, true, mode, NULL_RTX, NULL, do_error,
    1192              :                                profile_probability::very_unlikely ());
    1193              :     }
    1194         4759 :   if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
    1195              :     /* If this is a scalar in a register that is stored in a wider mode
    1196              :        than the declared mode, compute the result into its declared mode
    1197              :        and then convert to the wider mode.  Our value is the computed
    1198              :        expression.  */
    1199            0 :     convert_move (SUBREG_REG (target), res, SUBREG_PROMOTED_SIGN (target));
    1200              :   else
    1201         4759 :     emit_move_insn (target, res);
    1202         4759 : }
    1203              : 
    1204              : /* Add sub/add overflow checking to the statement STMT.
    1205              :    CODE says whether the operation is +, or -.  */
    1206              : 
    1207              : void
    1208        39360 : expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
    1209              :                         tree arg0, tree arg1, bool unsr_p, bool uns0_p,
    1210              :                         bool uns1_p, bool is_ubsan, tree *datap)
    1211              : {
    1212        39360 :   rtx res, target = NULL_RTX;
    1213        39360 :   tree fn;
    1214        39360 :   rtx_code_label *done_label = gen_label_rtx ();
    1215        39360 :   rtx_code_label *do_error = gen_label_rtx ();
    1216        39360 :   do_pending_stack_adjust ();
    1217        39360 :   rtx op0 = expand_normal (arg0);
    1218        39360 :   rtx op1 = expand_normal (arg1);
    1219        39360 :   scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
    1220        39360 :   int prec = GET_MODE_PRECISION (mode);
    1221        39360 :   rtx sgn = immed_wide_int_const (wi::min_value (prec, SIGNED), mode);
    1222        39360 :   bool do_xor = false;
    1223              : 
    1224        39360 :   if (is_ubsan)
    1225         3461 :     gcc_assert (!unsr_p && !uns0_p && !uns1_p);
    1226              : 
    1227        39360 :   if (lhs)
    1228              :     {
    1229        38832 :       target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    1230        38832 :       if (!is_ubsan)
    1231        35899 :         write_complex_part (target, const0_rtx, true, false);
    1232              :     }
    1233              : 
    1234              :   /* We assume both operands and result have the same precision
    1235              :      here (GET_MODE_BITSIZE (mode)), S stands for signed type
    1236              :      with that precision, U for unsigned type with that precision,
    1237              :      sgn for unsigned most significant bit in that precision.
    1238              :      s1 is signed first operand, u1 is unsigned first operand,
    1239              :      s2 is signed second operand, u2 is unsigned second operand,
    1240              :      sr is signed result, ur is unsigned result and the following
    1241              :      rules say how to compute result (which is always result of
    1242              :      the operands as if both were unsigned, cast to the right
    1243              :      signedness) and how to compute whether operation overflowed.
    1244              : 
    1245              :      s1 + s2 -> sr
    1246              :         res = (S) ((U) s1 + (U) s2)
    1247              :         ovf = s2 < 0 ? res > s1 : res < s1 (or jump on overflow)
    1248              :      s1 - s2 -> sr
    1249              :         res = (S) ((U) s1 - (U) s2)
    1250              :         ovf = s2 < 0 ? res < s1 : res > s2 (or jump on overflow)
    1251              :      u1 + u2 -> ur
    1252              :         res = u1 + u2
    1253              :         ovf = res < u1 (or jump on carry, but RTL opts will handle it)
    1254              :      u1 - u2 -> ur
    1255              :         res = u1 - u2
    1256              :         ovf = res > u1 (or jump on carry, but RTL opts will handle it)
    1257              :      s1 + u2 -> sr
    1258              :         res = (S) ((U) s1 + u2)
    1259              :         ovf = ((U) res ^ sgn) < u2
    1260              :      s1 + u2 -> ur
    1261              :         t1 = (S) (u2 ^ sgn)
    1262              :         t2 = s1 + t1
    1263              :         res = (U) t2 ^ sgn
    1264              :         ovf = t1 < 0 ? t2 > s1 : t2 < s1 (or jump on overflow)
    1265              :      s1 - u2 -> sr
    1266              :         res = (S) ((U) s1 - u2)
    1267              :         ovf = u2 > ((U) s1 ^ sgn)
    1268              :      s1 - u2 -> ur
    1269              :         res = (U) s1 - u2
    1270              :         ovf = s1 < 0 || u2 > (U) s1
    1271              :      u1 - s2 -> sr
    1272              :         res = u1 - (U) s2
    1273              :         ovf = u1 >= ((U) s2 ^ sgn)
    1274              :      u1 - s2 -> ur
    1275              :         t1 = u1 ^ sgn
    1276              :         t2 = t1 - (U) s2
    1277              :         res = t2 ^ sgn
    1278              :         ovf = s2 < 0 ? (S) t2 < (S) t1 : (S) t2 > (S) t1 (or jump on overflow)
    1279              :      s1 + s2 -> ur
    1280              :         res = (U) s1 + (U) s2
    1281              :         ovf = s2 < 0 ? (s1 | (S) res) < 0) : (s1 & (S) res) < 0)
    1282              :      u1 + u2 -> sr
    1283              :         res = (S) (u1 + u2)
    1284              :         ovf = (U) res < u2 || res < 0
    1285              :      u1 - u2 -> sr
    1286              :         res = (S) (u1 - u2)
    1287              :         ovf = u1 >= u2 ? res < 0 : res >= 0
    1288              :      s1 - s2 -> ur
    1289              :         res = (U) s1 - (U) s2
    1290              :         ovf = s2 >= 0 ? ((s1 | (S) res) < 0) : ((s1 & (S) res) < 0)  */
    1291              : 
    1292        39360 :   if (code == PLUS_EXPR && uns0_p && !uns1_p)
    1293              :     {
    1294              :       /* PLUS_EXPR is commutative, if operand signedness differs,
    1295              :          canonicalize to the first operand being signed and second
    1296              :          unsigned to simplify following code.  */
    1297              :       std::swap (op0, op1);
    1298              :       std::swap (arg0, arg1);
    1299              :       uns0_p = false;
    1300              :       uns1_p = true;
    1301              :     }
    1302              : 
    1303              :   /* u1 +- u2 -> ur  */
    1304        38577 :   if (uns0_p && uns1_p && unsr_p)
    1305              :     {
    1306        20605 :       insn_code icode = optab_handler (code == PLUS_EXPR ? uaddv4_optab
    1307              :                                        : usubv4_optab, mode);
    1308        15497 :       if (icode != CODE_FOR_nothing)
    1309              :         {
    1310        14668 :           class expand_operand ops[4];
    1311        14668 :           rtx_insn *last = get_last_insn ();
    1312              : 
    1313        14668 :           res = gen_reg_rtx (mode);
    1314        14668 :           create_output_operand (&ops[0], res, mode);
    1315        14668 :           create_input_operand (&ops[1], op0, mode);
    1316        14668 :           create_input_operand (&ops[2], op1, mode);
    1317        14668 :           create_fixed_operand (&ops[3], do_error);
    1318        14668 :           if (maybe_expand_insn (icode, 4, ops))
    1319              :             {
    1320        14668 :               last = get_last_insn ();
    1321        14668 :               if (profile_status_for_fn (cfun) != PROFILE_ABSENT
    1322        12330 :                   && JUMP_P (last)
    1323        12330 :                   && any_condjump_p (last)
    1324        26998 :                   && !find_reg_note (last, REG_BR_PROB, 0))
    1325        12330 :                 add_reg_br_prob_note (last,
    1326              :                                       profile_probability::very_unlikely ());
    1327        14668 :               emit_jump (done_label);
    1328        14668 :               goto do_error_label;
    1329              :             }
    1330              : 
    1331            0 :           delete_insns_since (last);
    1332              :         }
    1333              : 
    1334              :       /* Compute the operation.  On RTL level, the addition is always
    1335              :          unsigned.  */
    1336         1658 :       res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
    1337              :                           op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
    1338          829 :       rtx tem = op0;
    1339              :       /* For PLUS_EXPR, the operation is commutative, so we can pick
    1340              :          operand to compare against.  For prec <= BITS_PER_WORD, I think
    1341              :          preferring REG operand is better over CONST_INT, because
    1342              :          the CONST_INT might enlarge the instruction or CSE would need
    1343              :          to figure out we'd already loaded it into a register before.
    1344              :          For prec > BITS_PER_WORD, I think CONST_INT might be more beneficial,
    1345              :          as then the multi-word comparison can be perhaps simplified.  */
    1346          829 :       if (code == PLUS_EXPR
    1347            0 :           && (prec <= BITS_PER_WORD
    1348            0 :               ? (CONST_SCALAR_INT_P (op0) && REG_P (op1))
    1349            0 :               : CONST_SCALAR_INT_P (op1)))
    1350          829 :         tem = op1;
    1351         1658 :       do_compare_rtx_and_jump (res, tem, code == PLUS_EXPR ? GEU : LEU,
    1352              :                                true, mode, NULL_RTX, NULL, done_label,
    1353              :                                profile_probability::very_likely ());
    1354          829 :       goto do_error_label;
    1355              :     }
    1356              : 
    1357              :   /* s1 +- u2 -> sr  */
    1358        23863 :   if (!uns0_p && uns1_p && !unsr_p)
    1359              :     {
    1360              :       /* Compute the operation.  On RTL level, the addition is always
    1361              :          unsigned.  */
    1362         2514 :       res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
    1363              :                           op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
    1364         2514 :       rtx tem = expand_binop (mode, add_optab,
    1365              :                               code == PLUS_EXPR ? res : op0, sgn,
    1366              :                               NULL_RTX, false, OPTAB_LIB_WIDEN);
    1367         1582 :       do_compare_rtx_and_jump (tem, op1, GEU, true, mode, NULL_RTX, NULL,
    1368              :                                done_label, profile_probability::very_likely ());
    1369         1582 :       goto do_error_label;
    1370              :     }
    1371              : 
    1372              :   /* s1 + u2 -> ur  */
    1373        22281 :   if (code == PLUS_EXPR && !uns0_p && uns1_p && unsr_p)
    1374              :     {
    1375         1691 :       op1 = expand_binop (mode, add_optab, op1, sgn, NULL_RTX, false,
    1376              :                           OPTAB_LIB_WIDEN);
    1377              :       /* As we've changed op1, we have to avoid using the value range
    1378              :          for the original argument.  */
    1379         1691 :       arg1 = error_mark_node;
    1380         1691 :       do_xor = true;
    1381         1691 :       goto do_signed;
    1382              :     }
    1383              : 
    1384              :   /* u1 - s2 -> ur  */
    1385        20590 :   if (code == MINUS_EXPR && uns0_p && !uns1_p && unsr_p)
    1386              :     {
    1387          846 :       op0 = expand_binop (mode, add_optab, op0, sgn, NULL_RTX, false,
    1388              :                           OPTAB_LIB_WIDEN);
    1389              :       /* As we've changed op0, we have to avoid using the value range
    1390              :          for the original argument.  */
    1391          846 :       arg0 = error_mark_node;
    1392          846 :       do_xor = true;
    1393          846 :       goto do_signed;
    1394              :     }
    1395              : 
    1396              :   /* s1 - u2 -> ur  */
    1397        19744 :   if (code == MINUS_EXPR && !uns0_p && uns1_p && unsr_p)
    1398              :     {
    1399              :       /* Compute the operation.  On RTL level, the addition is always
    1400              :          unsigned.  */
    1401         1374 :       res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false,
    1402              :                           OPTAB_LIB_WIDEN);
    1403         1374 :       int pos_neg = get_range_pos_neg (arg0, currently_expanding_gimple_stmt);
    1404         1374 :       if (pos_neg == 2)
    1405              :         /* If ARG0 is known to be always negative, this is always overflow.  */
    1406           39 :         emit_jump (do_error);
    1407         1335 :       else if (pos_neg == 3)
    1408              :         /* If ARG0 is not known to be always positive, check at runtime.  */
    1409         1335 :         do_compare_rtx_and_jump (op0, const0_rtx, LT, false, mode, NULL_RTX,
    1410              :                                  NULL, do_error, profile_probability::very_unlikely ());
    1411         1374 :       do_compare_rtx_and_jump (op1, op0, LEU, true, mode, NULL_RTX, NULL,
    1412              :                                done_label, profile_probability::very_likely ());
    1413         1374 :       goto do_error_label;
    1414              :     }
    1415              : 
    1416              :   /* u1 - s2 -> sr  */
    1417        18370 :   if (code == MINUS_EXPR && uns0_p && !uns1_p && !unsr_p)
    1418              :     {
    1419              :       /* Compute the operation.  On RTL level, the addition is always
    1420              :          unsigned.  */
    1421          481 :       res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false,
    1422              :                           OPTAB_LIB_WIDEN);
    1423          481 :       rtx tem = expand_binop (mode, add_optab, op1, sgn, NULL_RTX, false,
    1424              :                               OPTAB_LIB_WIDEN);
    1425          481 :       do_compare_rtx_and_jump (op0, tem, LTU, true, mode, NULL_RTX, NULL,
    1426              :                                done_label, profile_probability::very_likely ());
    1427          481 :       goto do_error_label;
    1428              :     }
    1429              : 
    1430              :   /* u1 + u2 -> sr  */
    1431        17889 :   if (code == PLUS_EXPR && uns0_p && uns1_p && !unsr_p)
    1432              :     {
    1433              :       /* Compute the operation.  On RTL level, the addition is always
    1434              :          unsigned.  */
    1435         1192 :       res = expand_binop (mode, add_optab, op0, op1, NULL_RTX, false,
    1436              :                           OPTAB_LIB_WIDEN);
    1437         1192 :       do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, NULL_RTX,
    1438              :                                NULL, do_error, profile_probability::very_unlikely ());
    1439         1192 :       rtx tem = op1;
    1440              :       /* The operation is commutative, so we can pick operand to compare
    1441              :          against.  For prec <= BITS_PER_WORD, I think preferring REG operand
    1442              :          is better over CONST_INT, because the CONST_INT might enlarge the
    1443              :          instruction or CSE would need to figure out we'd already loaded it
    1444              :          into a register before.  For prec > BITS_PER_WORD, I think CONST_INT
    1445              :          might be more beneficial, as then the multi-word comparison can be
    1446              :          perhaps simplified.  */
    1447         1192 :       if (prec <= BITS_PER_WORD
    1448         1192 :           ? (CONST_SCALAR_INT_P (op1) && REG_P (op0))
    1449          408 :           : CONST_SCALAR_INT_P (op0))
    1450         1192 :         tem = op0;
    1451         1192 :       do_compare_rtx_and_jump (res, tem, GEU, true, mode, NULL_RTX, NULL,
    1452              :                                done_label, profile_probability::very_likely ());
    1453         1192 :       goto do_error_label;
    1454              :     }
    1455              : 
    1456              :   /* s1 +- s2 -> ur  */
    1457        16697 :   if (!uns0_p && !uns1_p && unsr_p)
    1458              :     {
    1459              :       /* Compute the operation.  On RTL level, the addition is always
    1460              :          unsigned.  */
    1461         3825 :       res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
    1462              :                           op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
    1463         2589 :       int pos_neg = get_range_pos_neg (arg1, currently_expanding_gimple_stmt);
    1464         2589 :       if (code == PLUS_EXPR)
    1465              :         {
    1466         1353 :           int pos_neg0 = get_range_pos_neg (arg0,
    1467              :                                             currently_expanding_gimple_stmt);
    1468         1353 :           if (pos_neg0 != 3 && pos_neg == 3)
    1469              :             {
    1470              :               std::swap (op0, op1);
    1471              :               pos_neg = pos_neg0;
    1472              :             }
    1473              :         }
    1474         2589 :       rtx tem;
    1475         2589 :       if (pos_neg != 3)
    1476              :         {
    1477         1585 :           tem = expand_binop (mode, ((pos_neg == 1) ^ (code == MINUS_EXPR))
    1478              :                                     ? and_optab : ior_optab,
    1479              :                               op0, res, NULL_RTX, false, OPTAB_LIB_WIDEN);
    1480          935 :           do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL,
    1481              :                                    NULL, done_label, profile_probability::very_likely ());
    1482              :         }
    1483              :       else
    1484              :         {
    1485         1654 :           rtx_code_label *do_ior_label = gen_label_rtx ();
    1486         2357 :           do_compare_rtx_and_jump (op1, const0_rtx,
    1487              :                                    code == MINUS_EXPR ? GE : LT, false, mode,
    1488              :                                    NULL_RTX, NULL, do_ior_label,
    1489              :                                    profile_probability::even ());
    1490         1654 :           tem = expand_binop (mode, and_optab, op0, res, NULL_RTX, false,
    1491              :                               OPTAB_LIB_WIDEN);
    1492         1654 :           do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
    1493              :                                    NULL, done_label, profile_probability::very_likely ());
    1494         1654 :           emit_jump (do_error);
    1495         1654 :           emit_label (do_ior_label);
    1496         1654 :           tem = expand_binop (mode, ior_optab, op0, res, NULL_RTX, false,
    1497              :                               OPTAB_LIB_WIDEN);
    1498         1654 :           do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
    1499              :                                    NULL, done_label, profile_probability::very_likely ());
    1500              :         }
    1501         2589 :       goto do_error_label;
    1502              :     }
    1503              : 
    1504              :   /* u1 - u2 -> sr  */
    1505        14108 :   if (code == MINUS_EXPR && uns0_p && uns1_p && !unsr_p)
    1506              :     {
    1507              :       /* Compute the operation.  On RTL level, the addition is always
    1508              :          unsigned.  */
    1509         1729 :       res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false,
    1510              :                           OPTAB_LIB_WIDEN);
    1511         1729 :       rtx_code_label *op0_geu_op1 = gen_label_rtx ();
    1512         1729 :       do_compare_rtx_and_jump (op0, op1, GEU, true, mode, NULL_RTX, NULL,
    1513              :                                op0_geu_op1, profile_probability::even ());
    1514         1729 :       do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, NULL_RTX,
    1515              :                                NULL, done_label, profile_probability::very_likely ());
    1516         1729 :       emit_jump (do_error);
    1517         1729 :       emit_label (op0_geu_op1);
    1518         1729 :       do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, NULL_RTX,
    1519              :                                NULL, done_label, profile_probability::very_likely ());
    1520         1729 :       goto do_error_label;
    1521              :     }
    1522              : 
    1523        12379 :   gcc_assert (!uns0_p && !uns1_p && !unsr_p);
    1524              : 
    1525              :   /* s1 +- s2 -> sr  */
    1526        12379 :  do_signed:
    1527        14916 :   {
    1528        21636 :     insn_code icode = optab_handler (code == PLUS_EXPR ? addv4_optab
    1529              :                                      : subv4_optab, mode);
    1530        14916 :     if (icode != CODE_FOR_nothing)
    1531              :       {
    1532        14916 :         class expand_operand ops[4];
    1533        14916 :         rtx_insn *last = get_last_insn ();
    1534              : 
    1535        14916 :         res = gen_reg_rtx (mode);
    1536        14916 :         create_output_operand (&ops[0], res, mode);
    1537        14916 :         create_input_operand (&ops[1], op0, mode);
    1538        14916 :         create_input_operand (&ops[2], op1, mode);
    1539        14916 :         create_fixed_operand (&ops[3], do_error);
    1540        14916 :         if (maybe_expand_insn (icode, 4, ops))
    1541              :           {
    1542        14916 :             last = get_last_insn ();
    1543        14916 :             if (profile_status_for_fn (cfun) != PROFILE_ABSENT
    1544         9818 :                 && JUMP_P (last)
    1545         9818 :                 && any_condjump_p (last)
    1546        24734 :                 && !find_reg_note (last, REG_BR_PROB, 0))
    1547         9818 :               add_reg_br_prob_note (last,
    1548              :                                     profile_probability::very_unlikely ());
    1549        14916 :             emit_jump (done_label);
    1550        14916 :             goto do_error_label;
    1551              :           }
    1552              : 
    1553            0 :         delete_insns_since (last);
    1554              :       }
    1555              : 
    1556              :     /* Compute the operation.  On RTL level, the addition is always
    1557              :        unsigned.  */
    1558            0 :     res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
    1559              :                         op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
    1560              : 
    1561              :     /* If we can prove that one of the arguments (for MINUS_EXPR only
    1562              :        the second operand, as subtraction is not commutative) is always
    1563              :        non-negative or always negative, we can do just one comparison
    1564              :        and conditional jump.  */
    1565            0 :     int pos_neg = get_range_pos_neg (arg1, currently_expanding_gimple_stmt);
    1566            0 :     if (code == PLUS_EXPR)
    1567              :       {
    1568            0 :         int pos_neg0 = get_range_pos_neg (arg0,
    1569              :                                           currently_expanding_gimple_stmt);
    1570            0 :         if (pos_neg0 != 3 && pos_neg == 3)
    1571              :           {
    1572              :             std::swap (op0, op1);
    1573              :             pos_neg = pos_neg0;
    1574              :           }
    1575              :       }
    1576              : 
    1577              :     /* Addition overflows if and only if the two operands have the same sign,
    1578              :        and the result has the opposite sign.  Subtraction overflows if and
    1579              :        only if the two operands have opposite sign, and the subtrahend has
    1580              :        the same sign as the result.  Here 0 is counted as positive.  */
    1581            0 :     if (pos_neg == 3)
    1582              :       {
    1583              :         /* Compute op0 ^ op1 (operands have opposite sign).  */
    1584            0 :         rtx op_xor = expand_binop (mode, xor_optab, op0, op1, NULL_RTX, false,
    1585              :                                    OPTAB_LIB_WIDEN);
    1586              : 
    1587              :         /* Compute res ^ op1 (result and 2nd operand have opposite sign).  */
    1588            0 :         rtx res_xor = expand_binop (mode, xor_optab, res, op1, NULL_RTX, false,
    1589              :                                     OPTAB_LIB_WIDEN);
    1590              : 
    1591            0 :         rtx tem;
    1592            0 :         if (code == PLUS_EXPR)
    1593              :           {
    1594              :             /* Compute (res ^ op1) & ~(op0 ^ op1).  */
    1595            0 :             tem = expand_unop (mode, one_cmpl_optab, op_xor, NULL_RTX, false);
    1596            0 :             tem = expand_binop (mode, and_optab, res_xor, tem, NULL_RTX, false,
    1597              :                                 OPTAB_LIB_WIDEN);
    1598              :           }
    1599              :         else
    1600              :           {
    1601              :             /* Compute (op0 ^ op1) & ~(res ^ op1).  */
    1602            0 :             tem = expand_unop (mode, one_cmpl_optab, res_xor, NULL_RTX, false);
    1603            0 :             tem = expand_binop (mode, and_optab, op_xor, tem, NULL_RTX, false,
    1604              :                                 OPTAB_LIB_WIDEN);
    1605              :           }
    1606              : 
    1607              :         /* No overflow if the result has bit sign cleared.  */
    1608            0 :         do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
    1609              :                                  NULL, done_label, profile_probability::very_likely ());
    1610              :       }
    1611              : 
    1612              :     /* Compare the result of the operation with the first operand.
    1613              :        No overflow for addition if second operand is positive and result
    1614              :        is larger or second operand is negative and result is smaller.
    1615              :        Likewise for subtraction with sign of second operand flipped.  */
    1616              :     else
    1617            0 :       do_compare_rtx_and_jump (res, op0,
    1618            0 :                                (pos_neg == 1) ^ (code == MINUS_EXPR) ? GE : LE,
    1619              :                                false, mode, NULL_RTX, NULL, done_label,
    1620              :                                profile_probability::very_likely ());
    1621              :   }
    1622              : 
    1623        39360 :  do_error_label:
    1624        39360 :   emit_label (do_error);
    1625        39360 :   if (is_ubsan)
    1626              :     {
    1627              :       /* Expand the ubsan builtin call.  */
    1628         3461 :       push_temp_slots ();
    1629         3461 :       fn = ubsan_build_overflow_builtin (code, loc, TREE_TYPE (arg0),
    1630              :                                          arg0, arg1, datap);
    1631         3461 :       expand_normal (fn);
    1632         3461 :       pop_temp_slots ();
    1633         3461 :       do_pending_stack_adjust ();
    1634              :     }
    1635        35899 :   else if (lhs)
    1636        35899 :     expand_arith_set_overflow (lhs, target);
    1637              : 
    1638              :   /* We're done.  */
    1639        39360 :   emit_label (done_label);
    1640              : 
    1641        39360 :   if (lhs)
    1642              :     {
    1643        38832 :       if (is_ubsan)
    1644         2933 :         expand_ubsan_result_store (lhs, target, mode, res, do_error);
    1645              :       else
    1646              :         {
    1647        35899 :           if (do_xor)
    1648         2537 :             res = expand_binop (mode, add_optab, res, sgn, NULL_RTX, false,
    1649              :                                 OPTAB_LIB_WIDEN);
    1650              : 
    1651        35899 :           expand_arith_overflow_result_store (lhs, target, mode, res, false);
    1652              :         }
    1653              :     }
    1654        39360 : }
    1655              : 
    1656              : /* Add negate overflow checking to the statement STMT.  */
    1657              : 
    1658              : static void
    1659         1064 : expand_neg_overflow (location_t loc, tree lhs, tree arg1, bool is_ubsan,
    1660              :                      tree *datap)
    1661              : {
    1662         1064 :   rtx res, op1;
    1663         1064 :   tree fn;
    1664         1064 :   rtx_code_label *done_label, *do_error;
    1665         1064 :   rtx target = NULL_RTX;
    1666              : 
    1667         1064 :   done_label = gen_label_rtx ();
    1668         1064 :   do_error = gen_label_rtx ();
    1669              : 
    1670         1064 :   do_pending_stack_adjust ();
    1671         1064 :   op1 = expand_normal (arg1);
    1672              : 
    1673         1064 :   scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg1));
    1674         1064 :   if (lhs)
    1675              :     {
    1676          860 :       target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    1677          860 :       if (!is_ubsan)
    1678          402 :         write_complex_part (target, const0_rtx, true, false);
    1679              :     }
    1680              : 
    1681         1064 :   enum insn_code icode = optab_handler (negv3_optab, mode);
    1682         1064 :   if (icode != CODE_FOR_nothing)
    1683              :     {
    1684          986 :       class expand_operand ops[3];
    1685          986 :       rtx_insn *last = get_last_insn ();
    1686              : 
    1687          986 :       res = gen_reg_rtx (mode);
    1688          986 :       create_output_operand (&ops[0], res, mode);
    1689          986 :       create_input_operand (&ops[1], op1, mode);
    1690          986 :       create_fixed_operand (&ops[2], do_error);
    1691          986 :       if (maybe_expand_insn (icode, 3, ops))
    1692              :         {
    1693          986 :           last = get_last_insn ();
    1694          986 :           if (profile_status_for_fn (cfun) != PROFILE_ABSENT
    1695          693 :               && JUMP_P (last)
    1696          693 :               && any_condjump_p (last)
    1697         1679 :               && !find_reg_note (last, REG_BR_PROB, 0))
    1698          693 :             add_reg_br_prob_note (last,
    1699              :                                   profile_probability::very_unlikely ());
    1700          986 :           emit_jump (done_label);
    1701              :         }
    1702              :       else
    1703              :         {
    1704            0 :           delete_insns_since (last);
    1705            0 :           icode = CODE_FOR_nothing;
    1706              :         }
    1707              :     }
    1708              : 
    1709          986 :   if (icode == CODE_FOR_nothing)
    1710              :     {
    1711              :       /* Compute the operation.  On RTL level, the addition is always
    1712              :          unsigned.  */
    1713           78 :       res = expand_unop (mode, neg_optab, op1, NULL_RTX, false);
    1714              : 
    1715              :       /* Compare the operand with the most negative value.  */
    1716           78 :       rtx minv = expand_normal (TYPE_MIN_VALUE (TREE_TYPE (arg1)));
    1717           78 :       do_compare_rtx_and_jump (op1, minv, NE, true, mode, NULL_RTX, NULL,
    1718              :                                done_label, profile_probability::very_likely ());
    1719              :     }
    1720              : 
    1721         1064 :   emit_label (do_error);
    1722         1064 :   if (is_ubsan)
    1723              :     {
    1724              :       /* Expand the ubsan builtin call.  */
    1725          662 :       push_temp_slots ();
    1726          662 :       fn = ubsan_build_overflow_builtin (NEGATE_EXPR, loc, TREE_TYPE (arg1),
    1727              :                                          arg1, NULL_TREE, datap);
    1728          662 :       expand_normal (fn);
    1729          662 :       pop_temp_slots ();
    1730          662 :       do_pending_stack_adjust ();
    1731              :     }
    1732          402 :   else if (lhs)
    1733          402 :     expand_arith_set_overflow (lhs, target);
    1734              : 
    1735              :   /* We're done.  */
    1736         1064 :   emit_label (done_label);
    1737              : 
    1738         1064 :   if (lhs)
    1739              :     {
    1740          860 :       if (is_ubsan)
    1741          458 :         expand_ubsan_result_store (lhs, target, mode, res, do_error);
    1742              :       else
    1743          402 :         expand_arith_overflow_result_store (lhs, target, mode, res, false);
    1744              :     }
    1745         1064 : }
    1746              : 
    1747              : /* Return true if UNS WIDEN_MULT_EXPR with result mode WMODE and operand
    1748              :    mode MODE can be expanded without using a libcall.  */
    1749              : 
    1750              : static bool
    1751            0 : can_widen_mult_without_libcall (scalar_int_mode wmode, scalar_int_mode mode,
    1752              :                                 rtx op0, rtx op1, bool uns)
    1753              : {
    1754            0 :   if (find_widening_optab_handler (umul_widen_optab, wmode, mode)
    1755              :       != CODE_FOR_nothing)
    1756              :     return true;
    1757              : 
    1758            0 :   if (find_widening_optab_handler (smul_widen_optab, wmode, mode)
    1759              :       != CODE_FOR_nothing)
    1760              :     return true;
    1761              : 
    1762            0 :   rtx_insn *last = get_last_insn ();
    1763            0 :   if (CONSTANT_P (op0))
    1764            0 :     op0 = convert_modes (wmode, mode, op0, uns);
    1765              :   else
    1766            0 :     op0 = gen_raw_REG (wmode, LAST_VIRTUAL_REGISTER + 1);
    1767            0 :   if (CONSTANT_P (op1))
    1768            0 :     op1 = convert_modes (wmode, mode, op1, uns);
    1769              :   else
    1770            0 :     op1 = gen_raw_REG (wmode, LAST_VIRTUAL_REGISTER + 2);
    1771            0 :   rtx ret = expand_mult (wmode, op0, op1, NULL_RTX, uns, true);
    1772            0 :   delete_insns_since (last);
    1773            0 :   return ret != NULL_RTX;
    1774              : }
    1775              : 
    1776              : /* Add mul overflow checking to the statement STMT.  */
    1777              : 
    1778              : static void
    1779        18963 : expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
    1780              :                      bool unsr_p, bool uns0_p, bool uns1_p, bool is_ubsan,
    1781              :                      tree *datap)
    1782              : {
    1783        18963 :   rtx res, op0, op1;
    1784        18963 :   tree fn, type, orig_arg0 = arg0, orig_arg1 = arg1;
    1785        18963 :   rtx_code_label *done_label, *do_error;
    1786        18963 :   rtx target = NULL_RTX;
    1787        18963 :   signop sign;
    1788        18963 :   enum insn_code icode;
    1789        18963 :   int save_flag_trapv = flag_trapv;
    1790              : 
    1791              :   /* We don't want any __mulv?i3 etc. calls from the expansion of
    1792              :      these internal functions, so disable -ftrapv temporarily.  */
    1793        18963 :   flag_trapv = 0;
    1794        18963 :   done_label = gen_label_rtx ();
    1795        18963 :   do_error = gen_label_rtx ();
    1796              : 
    1797        18963 :   do_pending_stack_adjust ();
    1798              : 
    1799        18963 :   scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
    1800              :   /* If the operand types don't have mode precision, extend them
    1801              :      to mode precision.  */
    1802        18963 :   if (TYPE_PRECISION (TREE_TYPE (arg0)) < GET_MODE_PRECISION (mode))
    1803              :     {
    1804          627 :       tree type = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
    1805              :                                                   uns0_p);
    1806          627 :       arg0 = fold_convert_loc (loc, type, arg0);
    1807              :     }
    1808        18963 :   if (TYPE_PRECISION (TREE_TYPE (arg1)) < GET_MODE_PRECISION (mode))
    1809              :     {
    1810          627 :       tree type = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
    1811              :                                                   uns1_p);
    1812          627 :       arg1 = fold_convert_loc (loc, type, arg1);
    1813              :     }
    1814              : 
    1815        18963 :   op0 = expand_normal (arg0);
    1816        18963 :   op1 = expand_normal (arg1);
    1817              : 
    1818        18963 :   bool uns = unsr_p;
    1819        18963 :   bool overflow_checked = false;
    1820        18963 :   if (lhs)
    1821              :     {
    1822        18819 :       target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    1823        18819 :       if (!is_ubsan)
    1824        17451 :         write_complex_part (target, const0_rtx, true, false);
    1825              :     }
    1826              : 
    1827        17595 :   if (is_ubsan)
    1828         1512 :     gcc_assert (!unsr_p && !uns0_p && !uns1_p);
    1829              : 
    1830              :   /* We assume both operands and result have the same precision
    1831              :      here (GET_MODE_BITSIZE (mode)), S stands for signed type
    1832              :      with that precision, U for unsigned type with that precision,
    1833              :      sgn for unsigned most significant bit in that precision.
    1834              :      s1 is signed first operand, u1 is unsigned first operand,
    1835              :      s2 is signed second operand, u2 is unsigned second operand,
    1836              :      sr is signed result, ur is unsigned result and the following
    1837              :      rules say how to compute result (which is always result of
    1838              :      the operands as if both were unsigned, cast to the right
    1839              :      signedness) and how to compute whether operation overflowed.
    1840              :      main_ovf (false) stands for jump on signed multiplication
    1841              :      overflow or the main algorithm with uns == false.
    1842              :      main_ovf (true) stands for jump on unsigned multiplication
    1843              :      overflow or the main algorithm with uns == true.
    1844              : 
    1845              :      s1 * s2 -> sr
    1846              :         res = (S) ((U) s1 * (U) s2)
    1847              :         ovf = main_ovf (false)
    1848              :      u1 * u2 -> ur
    1849              :         res = u1 * u2
    1850              :         ovf = main_ovf (true)
    1851              :      s1 * u2 -> ur
    1852              :         res = (U) s1 * u2
    1853              :         ovf = (s1 < 0 && u2) || main_ovf (true)
    1854              :      u1 * u2 -> sr
    1855              :         res = (S) (u1 * u2)
    1856              :         ovf = res < 0 || main_ovf (true)
    1857              :      s1 * u2 -> sr
    1858              :         res = (S) ((U) s1 * u2)
    1859              :         ovf = (S) u2 >= 0 ? main_ovf (false)
    1860              :                           : (s1 != 0 && (s1 != -1 || u2 != (U) res))
    1861              :      s1 * s2 -> ur
    1862              :         t1 = (s1 & s2) < 0 ? (-(U) s1) : ((U) s1)
    1863              :         t2 = (s1 & s2) < 0 ? (-(U) s2) : ((U) s2)
    1864              :         res = t1 * t2
    1865              :         ovf = (s1 ^ s2) < 0 ? (s1 && s2) : main_ovf (true)  */
    1866              : 
    1867        18963 :   if (uns0_p && !uns1_p)
    1868              :     {
    1869              :       /* Multiplication is commutative, if operand signedness differs,
    1870              :          canonicalize to the first operand being signed and second
    1871              :          unsigned to simplify following code.  */
    1872         1558 :       std::swap (op0, op1);
    1873         1558 :       std::swap (arg0, arg1);
    1874         1558 :       uns0_p = false;
    1875         1558 :       uns1_p = true;
    1876              :     }
    1877              : 
    1878        18963 :   int pos_neg0 = get_range_pos_neg (arg0, currently_expanding_gimple_stmt);
    1879        18963 :   int pos_neg1 = get_range_pos_neg (arg1, currently_expanding_gimple_stmt);
    1880              : 
    1881              :   /* s1 * u2 -> ur  */
    1882        18963 :   if (!uns0_p && uns1_p && unsr_p)
    1883              :     {
    1884         2254 :       switch (pos_neg0)
    1885              :         {
    1886            0 :         case 1:
    1887              :           /* If s1 is non-negative, just perform normal u1 * u2 -> ur.  */
    1888         1821 :           goto do_main;
    1889          328 :         case 2:
    1890              :           /* If s1 is negative, avoid the main code, just multiply and
    1891              :              signal overflow if op1 is not 0.  */
    1892          328 :           struct separate_ops ops;
    1893          328 :           ops.code = MULT_EXPR;
    1894          328 :           ops.type = TREE_TYPE (arg1);
    1895          328 :           ops.op0 = make_tree (ops.type, op0);
    1896          328 :           ops.op1 = make_tree (ops.type, op1);
    1897          328 :           ops.op2 = NULL_TREE;
    1898          328 :           ops.location = loc;
    1899          328 :           res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    1900          328 :           do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
    1901              :                                    NULL, done_label, profile_probability::very_likely ());
    1902          433 :           goto do_error_label;
    1903         1926 :         case 3:
    1904         3852 :           if (get_min_precision (arg1, UNSIGNED)
    1905         1926 :               + get_min_precision (arg0, SIGNED) <= GET_MODE_PRECISION (mode))
    1906              :             {
    1907              :               /* If the first operand is sign extended from narrower type, the
    1908              :                  second operand is zero extended from narrower type and
    1909              :                  the sum of the two precisions is smaller or equal to the
    1910              :                  result precision: if the first argument is at runtime
    1911              :                  non-negative, maximum result will be 0x7e81 or 0x7f..fe80..01
    1912              :                  and there will be no overflow, if the first argument is
    1913              :                  negative and the second argument zero, the result will be
    1914              :                  0 and there will be no overflow, if the first argument is
    1915              :                  negative and the second argument positive, the result when
    1916              :                  treated as signed will be negative (minimum -0x7f80 or
    1917              :                  -0x7f..f80..0) there will be always overflow.  So, do
    1918              :                  res = (U) (s1 * u2)
    1919              :                  ovf = (S) res < 0  */
    1920          105 :               struct separate_ops ops;
    1921          105 :               ops.code = MULT_EXPR;
    1922          105 :               ops.type
    1923          105 :                 = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
    1924              :                                                   1);
    1925          105 :               ops.op0 = make_tree (ops.type, op0);
    1926          105 :               ops.op1 = make_tree (ops.type, op1);
    1927          105 :               ops.op2 = NULL_TREE;
    1928          105 :               ops.location = loc;
    1929          105 :               res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    1930          105 :               do_compare_rtx_and_jump (res, const0_rtx, GE, false,
    1931              :                                        mode, NULL_RTX, NULL, done_label,
    1932              :                                        profile_probability::very_likely ());
    1933          105 :               goto do_error_label;
    1934              :             }
    1935         1821 :           rtx_code_label *do_main_label;
    1936         1821 :           do_main_label = gen_label_rtx ();
    1937         1821 :           do_compare_rtx_and_jump (op0, const0_rtx, GE, false, mode, NULL_RTX,
    1938              :                                    NULL, do_main_label, profile_probability::very_likely ());
    1939         1821 :           do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
    1940              :                                    NULL, do_main_label, profile_probability::very_likely ());
    1941         1821 :           expand_arith_set_overflow (lhs, target);
    1942         1821 :           emit_label (do_main_label);
    1943         1821 :           goto do_main;
    1944            0 :         default:
    1945            0 :           gcc_unreachable ();
    1946              :         }
    1947              :     }
    1948              : 
    1949              :   /* u1 * u2 -> sr  */
    1950        16709 :   if (uns0_p && uns1_p && !unsr_p)
    1951              :     {
    1952         1369 :       if ((pos_neg0 | pos_neg1) == 1)
    1953              :         {
    1954              :           /* If both arguments are zero extended from narrower types,
    1955              :              the MSB will be clear on both and so we can pretend it is
    1956              :              a normal s1 * s2 -> sr multiplication.  */
    1957              :           uns0_p = false;
    1958              :           uns1_p = false;
    1959              :         }
    1960              :       else
    1961          957 :         uns = true;
    1962              :       /* Rest of handling of this case after res is computed.  */
    1963         1369 :       goto do_main;
    1964              :     }
    1965              : 
    1966              :   /* s1 * u2 -> sr  */
    1967        15340 :   if (!uns0_p && uns1_p && !unsr_p)
    1968              :     {
    1969         1788 :       switch (pos_neg1)
    1970              :         {
    1971           66 :         case 1:
    1972         1339 :           goto do_main;
    1973          449 :         case 2:
    1974              :           /* If (S) u2 is negative (i.e. u2 is larger than maximum of S,
    1975              :              avoid the main code, just multiply and signal overflow
    1976              :              unless 0 * u2 or -1 * ((U) Smin).  */
    1977          449 :           struct separate_ops ops;
    1978          449 :           ops.code = MULT_EXPR;
    1979          449 :           ops.type = TREE_TYPE (arg1);
    1980          449 :           ops.op0 = make_tree (ops.type, op0);
    1981          449 :           ops.op1 = make_tree (ops.type, op1);
    1982          449 :           ops.op2 = NULL_TREE;
    1983          449 :           ops.location = loc;
    1984          449 :           res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    1985          449 :           do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
    1986              :                                    NULL, done_label, profile_probability::very_likely ());
    1987          449 :           do_compare_rtx_and_jump (op0, constm1_rtx, NE, true, mode, NULL_RTX,
    1988              :                                    NULL, do_error, profile_probability::very_unlikely ());
    1989          449 :           int prec;
    1990          449 :           prec = GET_MODE_PRECISION (mode);
    1991          449 :           rtx sgn;
    1992          449 :           sgn = immed_wide_int_const (wi::min_value (prec, SIGNED), mode);
    1993          449 :           do_compare_rtx_and_jump (op1, sgn, EQ, true, mode, NULL_RTX,
    1994              :                                    NULL, done_label, profile_probability::very_likely ());
    1995          449 :           goto do_error_label;
    1996         1273 :         case 3:
    1997              :           /* Rest of handling of this case after res is computed.  */
    1998         1273 :           goto do_main;
    1999            0 :         default:
    2000            0 :           gcc_unreachable ();
    2001              :         }
    2002              :     }
    2003              : 
    2004              :   /* s1 * s2 -> ur  */
    2005        13552 :   if (!uns0_p && !uns1_p && unsr_p)
    2006              :     {
    2007         2329 :       rtx tem;
    2008         2329 :       switch (pos_neg0 | pos_neg1)
    2009              :         {
    2010            0 :         case 1: /* Both operands known to be non-negative.  */
    2011            0 :           goto do_main;
    2012           12 :         case 2: /* Both operands known to be negative.  */
    2013           12 :           op0 = expand_unop (mode, neg_optab, op0, NULL_RTX, false);
    2014           12 :           op1 = expand_unop (mode, neg_optab, op1, NULL_RTX, false);
    2015              :           /* Avoid looking at arg0/arg1 ranges, as we've changed
    2016              :              the arguments.  */
    2017           12 :           arg0 = error_mark_node;
    2018           12 :           arg1 = error_mark_node;
    2019           12 :           goto do_main;
    2020         2317 :         case 3:
    2021         2317 :           if ((pos_neg0 ^ pos_neg1) == 3)
    2022              :             {
    2023              :               /* If one operand is known to be negative and the other
    2024              :                  non-negative, this overflows always, unless the non-negative
    2025              :                  one is 0.  Just do normal multiply and set overflow
    2026              :                  unless one of the operands is 0.  */
    2027            0 :               struct separate_ops ops;
    2028            0 :               ops.code = MULT_EXPR;
    2029            0 :               ops.type
    2030            0 :                 = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
    2031              :                                                   1);
    2032            0 :               ops.op0 = make_tree (ops.type, op0);
    2033            0 :               ops.op1 = make_tree (ops.type, op1);
    2034            0 :               ops.op2 = NULL_TREE;
    2035            0 :               ops.location = loc;
    2036            0 :               res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    2037            0 :               do_compare_rtx_and_jump (pos_neg0 == 1 ? op0 : op1, const0_rtx, EQ,
    2038              :                                        true, mode, NULL_RTX, NULL, done_label,
    2039              :                                        profile_probability::very_likely ());
    2040            0 :               goto do_error_label;
    2041              :             }
    2042         4634 :           if (get_min_precision (arg0, SIGNED)
    2043         2317 :               + get_min_precision (arg1, SIGNED) <= GET_MODE_PRECISION (mode))
    2044              :             {
    2045              :               /* If both operands are sign extended from narrower types and
    2046              :                  the sum of the two precisions is smaller or equal to the
    2047              :                  result precision: if both arguments are at runtime
    2048              :                  non-negative, maximum result will be 0x3f01 or 0x3f..f0..01
    2049              :                  and there will be no overflow, if both arguments are negative,
    2050              :                  maximum result will be 0x40..00 and there will be no overflow
    2051              :                  either, if one argument is positive and the other argument
    2052              :                  negative, the result when treated as signed will be negative
    2053              :                  and there will be always overflow, and if one argument is
    2054              :                  zero and the other negative the result will be zero and no
    2055              :                  overflow.  So, do
    2056              :                  res = (U) (s1 * s2)
    2057              :                  ovf = (S) res < 0  */
    2058          220 :               struct separate_ops ops;
    2059          220 :               ops.code = MULT_EXPR;
    2060          220 :               ops.type
    2061          220 :                 = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
    2062              :                                                   1);
    2063          220 :               ops.op0 = make_tree (ops.type, op0);
    2064          220 :               ops.op1 = make_tree (ops.type, op1);
    2065          220 :               ops.op2 = NULL_TREE;
    2066          220 :               ops.location = loc;
    2067          220 :               res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    2068          220 :               do_compare_rtx_and_jump (res, const0_rtx, GE, false,
    2069              :                                        mode, NULL_RTX, NULL, done_label,
    2070              :                                        profile_probability::very_likely ());
    2071          220 :               goto do_error_label;
    2072              :             }
    2073              :           /* The general case, do all the needed comparisons at runtime.  */
    2074         2097 :           rtx_code_label *do_main_label, *after_negate_label;
    2075         2097 :           rtx rop0, rop1;
    2076         2097 :           rop0 = gen_reg_rtx (mode);
    2077         2097 :           rop1 = gen_reg_rtx (mode);
    2078         2097 :           emit_move_insn (rop0, op0);
    2079         2097 :           emit_move_insn (rop1, op1);
    2080         2097 :           op0 = rop0;
    2081         2097 :           op1 = rop1;
    2082         2097 :           do_main_label = gen_label_rtx ();
    2083         2097 :           after_negate_label = gen_label_rtx ();
    2084         2097 :           tem = expand_binop (mode, and_optab, op0, op1, NULL_RTX, false,
    2085              :                               OPTAB_LIB_WIDEN);
    2086         2097 :           do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
    2087              :                                    NULL, after_negate_label, profile_probability::very_likely ());
    2088              :           /* Both arguments negative here, negate them and continue with
    2089              :              normal unsigned overflow checking multiplication.  */
    2090         2097 :           emit_move_insn (op0, expand_unop (mode, neg_optab, op0,
    2091              :                                             NULL_RTX, false));
    2092         2097 :           emit_move_insn (op1, expand_unop (mode, neg_optab, op1,
    2093              :                                             NULL_RTX, false));
    2094              :           /* Avoid looking at arg0/arg1 ranges, as we might have changed
    2095              :              the arguments.  */
    2096         2097 :           arg0 = error_mark_node;
    2097         2097 :           arg1 = error_mark_node;
    2098         2097 :           emit_jump (do_main_label);
    2099         2097 :           emit_label (after_negate_label);
    2100         2097 :           tem = expand_binop (mode, xor_optab, op0, op1, NULL_RTX, false,
    2101              :                               OPTAB_LIB_WIDEN);
    2102         2097 :           do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
    2103              :                                    NULL, do_main_label,
    2104              :                                    profile_probability::very_likely ());
    2105              :           /* One argument is negative here, the other positive.  This
    2106              :              overflows always, unless one of the arguments is 0.  But
    2107              :              if e.g. s2 is 0, (U) s1 * 0 doesn't overflow, whatever s1
    2108              :              is, thus we can keep do_main code oring in overflow as is.  */
    2109         2097 :           if (pos_neg0 != 2)
    2110         2097 :             do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
    2111              :                                      NULL, do_main_label,
    2112              :                                      profile_probability::very_unlikely ());
    2113         2097 :           if (pos_neg1 != 2)
    2114         1080 :             do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
    2115              :                                      NULL, do_main_label,
    2116              :                                      profile_probability::very_unlikely ());
    2117         2097 :           expand_arith_set_overflow (lhs, target);
    2118         2097 :           emit_label (do_main_label);
    2119         2097 :           goto do_main;
    2120            0 :         default:
    2121            0 :           gcc_unreachable ();
    2122              :         }
    2123              :     }
    2124              : 
    2125        11223 :  do_main:
    2126        17861 :   type = build_nonstandard_integer_type (GET_MODE_PRECISION (mode), uns);
    2127        17861 :   sign = uns ? UNSIGNED : SIGNED;
    2128        17861 :   icode = optab_handler (uns ? umulv4_optab : mulv4_optab, mode);
    2129        17861 :   if (uns
    2130        10498 :       && (integer_pow2p (arg0) || integer_pow2p (arg1))
    2131        18735 :       && (optimize_insn_for_speed_p () || icode == CODE_FOR_nothing))
    2132              :     {
    2133              :       /* Optimize unsigned multiplication by power of 2 constant
    2134              :          using 2 shifts, one for result, one to extract the shifted
    2135              :          out bits to see if they are all zero.
    2136              :          Don't do this if optimizing for size and we have umulv4_optab,
    2137              :          in that case assume multiplication will be shorter.
    2138              :          This is heuristics based on the single target that provides
    2139              :          umulv4 right now (i?86/x86_64), if further targets add it, this
    2140              :          might need to be revisited.
    2141              :          Cases where both operands are constant should be folded already
    2142              :          during GIMPLE, and cases where one operand is constant but not
    2143              :          power of 2 are questionable, either the WIDEN_MULT_EXPR case
    2144              :          below can be done without multiplication, just by shifts and adds,
    2145              :          or we'd need to divide the result (and hope it actually doesn't
    2146              :          really divide nor multiply) and compare the result of the division
    2147              :          with the original operand.  */
    2148          872 :       rtx opn0 = op0;
    2149          872 :       rtx opn1 = op1;
    2150          872 :       tree argn0 = arg0;
    2151          872 :       tree argn1 = arg1;
    2152          872 :       if (integer_pow2p (arg0))
    2153              :         {
    2154            0 :           std::swap (opn0, opn1);
    2155            0 :           std::swap (argn0, argn1);
    2156              :         }
    2157          872 :       int cnt = tree_log2 (argn1);
    2158          872 :       if (cnt >= 0 && cnt < GET_MODE_PRECISION (mode))
    2159              :         {
    2160          872 :           rtx upper = const0_rtx;
    2161          872 :           res = expand_shift (LSHIFT_EXPR, mode, opn0, cnt, NULL_RTX, uns);
    2162          872 :           if (cnt != 0)
    2163          700 :             upper = expand_shift (RSHIFT_EXPR, mode, opn0,
    2164          700 :                                   GET_MODE_PRECISION (mode) - cnt,
    2165              :                                   NULL_RTX, uns);
    2166          872 :           do_compare_rtx_and_jump (upper, const0_rtx, EQ, true, mode,
    2167              :                                    NULL_RTX, NULL, done_label,
    2168              :                                    profile_probability::very_likely ());
    2169          872 :           goto do_error_label;
    2170              :         }
    2171              :     }
    2172        16989 :   if (icode != CODE_FOR_nothing)
    2173              :     {
    2174        13890 :       class expand_operand ops[4];
    2175        13890 :       rtx_insn *last = get_last_insn ();
    2176              : 
    2177        13890 :       res = gen_reg_rtx (mode);
    2178        13890 :       create_output_operand (&ops[0], res, mode);
    2179        13890 :       create_input_operand (&ops[1], op0, mode);
    2180        13890 :       create_input_operand (&ops[2], op1, mode);
    2181        13890 :       create_fixed_operand (&ops[3], do_error);
    2182        13890 :       if (maybe_expand_insn (icode, 4, ops))
    2183              :         {
    2184        13890 :           last = get_last_insn ();
    2185        13890 :           if (profile_status_for_fn (cfun) != PROFILE_ABSENT
    2186         9203 :               && JUMP_P (last)
    2187         9203 :               && any_condjump_p (last)
    2188        23093 :               && !find_reg_note (last, REG_BR_PROB, 0))
    2189         9203 :             add_reg_br_prob_note (last,
    2190              :                                   profile_probability::very_unlikely ());
    2191        13890 :           emit_jump (done_label);
    2192              :         }
    2193              :       else
    2194              :         {
    2195            0 :           delete_insns_since (last);
    2196            0 :           icode = CODE_FOR_nothing;
    2197              :         }
    2198              :     }
    2199              : 
    2200            0 :   if (icode == CODE_FOR_nothing)
    2201              :     {
    2202         3099 :       struct separate_ops ops;
    2203         3099 :       int prec = GET_MODE_PRECISION (mode);
    2204         3099 :       scalar_int_mode hmode, wmode;
    2205         3099 :       ops.op0 = make_tree (type, op0);
    2206         3099 :       ops.op1 = make_tree (type, op1);
    2207         3099 :       ops.op2 = NULL_TREE;
    2208         3099 :       ops.location = loc;
    2209              : 
    2210              :       /* Optimize unsigned overflow check where we don't use the
    2211              :          multiplication result, just whether overflow happened.
    2212              :          If we can do MULT_HIGHPART_EXPR, that followed by
    2213              :          comparison of the result against zero is cheapest.
    2214              :          We'll still compute res, but it should be DCEd later.  */
    2215         3099 :       use_operand_p use;
    2216         3099 :       gimple *use_stmt;
    2217         3099 :       if (!is_ubsan
    2218         3099 :           && lhs
    2219         3079 :           && uns
    2220         1973 :           && !(uns0_p && uns1_p && !unsr_p)
    2221         1692 :           && can_mult_highpart_p (mode, uns) == 1
    2222            0 :           && single_imm_use (lhs, &use, &use_stmt)
    2223            0 :           && is_gimple_assign (use_stmt)
    2224         3099 :           && gimple_assign_rhs_code (use_stmt) == IMAGPART_EXPR)
    2225            0 :         goto highpart;
    2226              : 
    2227         3099 :       if (GET_MODE_2XWIDER_MODE (mode).exists (&wmode)
    2228         3099 :           && targetm.scalar_mode_supported_p (wmode)
    2229            0 :           && can_widen_mult_without_libcall (wmode, mode, op0, op1, uns))
    2230              :         {
    2231            0 :         twoxwider:
    2232            0 :           int ovf_prec = prec;
    2233            0 :           if (!is_ubsan && lhs && unsr_p)
    2234            0 :             ovf_prec = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (lhs)));
    2235            0 :           ops.code = WIDEN_MULT_EXPR;
    2236            0 :           ops.type
    2237            0 :             = build_nonstandard_integer_type (GET_MODE_PRECISION (wmode), uns);
    2238              : 
    2239            0 :           res = expand_expr_real_2 (&ops, NULL_RTX, wmode, EXPAND_NORMAL);
    2240            0 :           rtx hipart = expand_shift (RSHIFT_EXPR, wmode, res, ovf_prec,
    2241              :                                      NULL_RTX, uns);
    2242            0 :           if (uns)
    2243              :             {
    2244              :               /* For the unsigned multiplication, there was overflow if
    2245              :                  HIPART is non-zero. Convert RES before the branch so that
    2246              :                  both the no-overflow fall-through and the overflow path see
    2247              :                  RES in MODE.  */
    2248            0 :               res = convert_modes (mode, wmode, res, uns);
    2249            0 :               if (ovf_prec < prec)
    2250              :                 {
    2251            0 :                   overflow_checked = true;
    2252            0 :                   do_compare_rtx_and_jump (hipart, const0_rtx, EQ, true, wmode,
    2253              :                                            NULL_RTX, NULL, done_label,
    2254              :                                            profile_probability::very_likely ());
    2255              :                 }
    2256              :               else
    2257              :                 {
    2258            0 :                   hipart = convert_modes (mode, wmode, hipart, uns);
    2259            0 :                   do_compare_rtx_and_jump (hipart, const0_rtx, EQ, true, mode,
    2260              :                                            NULL_RTX, NULL, done_label,
    2261              :                                            profile_probability::very_likely ());
    2262              :                 }
    2263              :             }
    2264              :           else
    2265              :             {
    2266            0 :               hipart = convert_modes (mode, wmode, hipart, uns);
    2267            0 :               res = convert_modes (mode, wmode, res, uns);
    2268              :               /* RES is used more than once, place it in a pseudo.  */
    2269            0 :               res = force_reg (mode, res);
    2270              : 
    2271            0 :               rtx signbit = expand_shift (RSHIFT_EXPR, mode, res, prec - 1,
    2272              :                                           NULL_RTX, 0);
    2273              :               /* RES is low half of the double width result, HIPART
    2274              :                  the high half.  There was overflow if
    2275              :                  HIPART is different from RES < 0 ? -1 : 0.  */
    2276            0 :               do_compare_rtx_and_jump (signbit, hipart, EQ, true, mode,
    2277              :                                        NULL_RTX, NULL, done_label,
    2278              :                                        profile_probability::very_likely ());
    2279              :             }
    2280              :         }
    2281         3099 :       else if (can_mult_highpart_p (mode, uns) == 1)
    2282              :         {
    2283            0 :         highpart:
    2284            0 :           ops.code = MULT_HIGHPART_EXPR;
    2285            0 :           ops.type = type;
    2286              : 
    2287            0 :           rtx hipart = expand_expr_real_2 (&ops, NULL_RTX, mode,
    2288              :                                            EXPAND_NORMAL);
    2289            0 :           ops.code = MULT_EXPR;
    2290            0 :           res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    2291            0 :           if (uns)
    2292              :             /* For the unsigned multiplication, there was overflow if
    2293              :                HIPART is non-zero.  */
    2294            0 :             do_compare_rtx_and_jump (hipart, const0_rtx, EQ, true, mode,
    2295              :                                      NULL_RTX, NULL, done_label,
    2296              :                                      profile_probability::very_likely ());
    2297              :           else
    2298              :             {
    2299            0 :               rtx signbit = expand_shift (RSHIFT_EXPR, mode, res, prec - 1,
    2300              :                                           NULL_RTX, 0);
    2301              :               /* RES is low half of the double width result, HIPART
    2302              :                  the high half.  There was overflow if
    2303              :                  HIPART is different from RES < 0 ? -1 : 0.  */
    2304            0 :               do_compare_rtx_and_jump (signbit, hipart, EQ, true, mode,
    2305              :                                        NULL_RTX, NULL, done_label,
    2306              :                                        profile_probability::very_likely ());
    2307              :             }
    2308              : 
    2309              :         }
    2310         3099 :       else if (int_mode_for_size (prec / 2, 1).exists (&hmode)
    2311         3099 :                && 2 * GET_MODE_PRECISION (hmode) == prec)
    2312              :         {
    2313         3099 :           rtx_code_label *large_op0 = gen_label_rtx ();
    2314         3099 :           rtx_code_label *small_op0_large_op1 = gen_label_rtx ();
    2315         3099 :           rtx_code_label *one_small_one_large = gen_label_rtx ();
    2316         3099 :           rtx_code_label *both_ops_large = gen_label_rtx ();
    2317         3099 :           rtx_code_label *after_hipart_neg = uns ? NULL : gen_label_rtx ();
    2318         1126 :           rtx_code_label *after_lopart_neg = uns ? NULL : gen_label_rtx ();
    2319         3099 :           rtx_code_label *do_overflow = gen_label_rtx ();
    2320         3099 :           rtx_code_label *hipart_different = uns ? NULL : gen_label_rtx ();
    2321              : 
    2322         3099 :           unsigned int hprec = GET_MODE_PRECISION (hmode);
    2323         3099 :           rtx hipart0 = expand_shift (RSHIFT_EXPR, mode, op0, hprec,
    2324              :                                       NULL_RTX, uns);
    2325         3099 :           hipart0 = convert_modes (hmode, mode, hipart0, uns);
    2326         3099 :           rtx lopart0 = convert_modes (hmode, mode, op0, uns);
    2327         3099 :           rtx signbit0 = const0_rtx;
    2328         3099 :           if (!uns)
    2329         1126 :             signbit0 = expand_shift (RSHIFT_EXPR, hmode, lopart0, hprec - 1,
    2330              :                                      NULL_RTX, 0);
    2331         3099 :           rtx hipart1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec,
    2332              :                                       NULL_RTX, uns);
    2333         3099 :           hipart1 = convert_modes (hmode, mode, hipart1, uns);
    2334         3099 :           rtx lopart1 = convert_modes (hmode, mode, op1, uns);
    2335         3099 :           rtx signbit1 = const0_rtx;
    2336         3099 :           if (!uns)
    2337         1126 :             signbit1 = expand_shift (RSHIFT_EXPR, hmode, lopart1, hprec - 1,
    2338              :                                      NULL_RTX, 0);
    2339              : 
    2340         3099 :           res = gen_reg_rtx (mode);
    2341              : 
    2342              :           /* True if op0 resp. op1 are known to be in the range of
    2343              :              halfstype.  */
    2344         3099 :           bool op0_small_p = false;
    2345         3099 :           bool op1_small_p = false;
    2346              :           /* True if op0 resp. op1 are known to have all zeros or all ones
    2347              :              in the upper half of bits, but are not known to be
    2348              :              op{0,1}_small_p.  */
    2349         3099 :           bool op0_medium_p = false;
    2350         3099 :           bool op1_medium_p = false;
    2351              :           /* -1 if op{0,1} is known to be negative, 0 if it is known to be
    2352              :              nonnegative, 1 if unknown.  */
    2353         3099 :           int op0_sign = 1;
    2354         3099 :           int op1_sign = 1;
    2355              : 
    2356         3099 :           if (pos_neg0 == 1)
    2357              :             op0_sign = 0;
    2358         2950 :           else if (pos_neg0 == 2)
    2359          188 :             op0_sign = -1;
    2360         3099 :           if (pos_neg1 == 1)
    2361              :             op1_sign = 0;
    2362         2002 :           else if (pos_neg1 == 2)
    2363          428 :             op1_sign = -1;
    2364              : 
    2365         3099 :           unsigned int mprec0 = prec;
    2366         3099 :           if (arg0 != error_mark_node)
    2367         2350 :             mprec0 = get_min_precision (arg0, sign);
    2368         3099 :           if (mprec0 <= hprec)
    2369              :             op0_small_p = true;
    2370         2923 :           else if (!uns && mprec0 <= hprec + 1)
    2371         3099 :             op0_medium_p = true;
    2372         3099 :           unsigned int mprec1 = prec;
    2373         3099 :           if (arg1 != error_mark_node)
    2374         2350 :             mprec1 = get_min_precision (arg1, sign);
    2375         3099 :           if (mprec1 <= hprec)
    2376              :             op1_small_p = true;
    2377         2582 :           else if (!uns && mprec1 <= hprec + 1)
    2378         3099 :             op1_medium_p = true;
    2379              : 
    2380         3099 :           int smaller_sign = 1;
    2381         3099 :           int larger_sign = 1;
    2382         3099 :           if (op0_small_p)
    2383              :             {
    2384              :               smaller_sign = op0_sign;
    2385              :               larger_sign = op1_sign;
    2386              :             }
    2387         2923 :           else if (op1_small_p)
    2388              :             {
    2389              :               smaller_sign = op1_sign;
    2390              :               larger_sign = op0_sign;
    2391              :             }
    2392         2406 :           else if (op0_sign == op1_sign)
    2393              :             {
    2394         1494 :               smaller_sign = op0_sign;
    2395         1494 :               larger_sign = op0_sign;
    2396              :             }
    2397              : 
    2398         2923 :           if (!op0_small_p)
    2399         2923 :             do_compare_rtx_and_jump (signbit0, hipart0, NE, true, hmode,
    2400              :                                      NULL_RTX, NULL, large_op0,
    2401              :                                      profile_probability::unlikely ());
    2402              : 
    2403         3099 :           if (!op1_small_p)
    2404         2582 :             do_compare_rtx_and_jump (signbit1, hipart1, NE, true, hmode,
    2405              :                                      NULL_RTX, NULL, small_op0_large_op1,
    2406              :                                      profile_probability::unlikely ());
    2407              : 
    2408              :           /* If both op0 and op1 are sign (!uns) or zero (uns) extended from
    2409              :              hmode to mode, the multiplication will never overflow.  We can
    2410              :              do just one hmode x hmode => mode widening multiplication.  */
    2411         3099 :           tree halfstype = build_nonstandard_integer_type (hprec, uns);
    2412         3099 :           ops.op0 = make_tree (halfstype, lopart0);
    2413         3099 :           ops.op1 = make_tree (halfstype, lopart1);
    2414         3099 :           ops.code = WIDEN_MULT_EXPR;
    2415         3099 :           ops.type = type;
    2416         3099 :           rtx thisres
    2417         3099 :             = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    2418         3099 :           emit_move_insn (res, thisres);
    2419         3099 :           emit_jump (done_label);
    2420              : 
    2421         3099 :           emit_label (small_op0_large_op1);
    2422              : 
    2423              :           /* If op0 is sign (!uns) or zero (uns) extended from hmode to mode,
    2424              :              but op1 is not, just swap the arguments and handle it as op1
    2425              :              sign/zero extended, op0 not.  */
    2426         3099 :           rtx larger = gen_reg_rtx (mode);
    2427         3099 :           rtx hipart = gen_reg_rtx (hmode);
    2428         3099 :           rtx lopart = gen_reg_rtx (hmode);
    2429         3099 :           emit_move_insn (larger, op1);
    2430         3099 :           emit_move_insn (hipart, hipart1);
    2431         3099 :           emit_move_insn (lopart, lopart0);
    2432         3099 :           emit_jump (one_small_one_large);
    2433              : 
    2434         3099 :           emit_label (large_op0);
    2435              : 
    2436         3099 :           if (!op1_small_p)
    2437         2582 :             do_compare_rtx_and_jump (signbit1, hipart1, NE, true, hmode,
    2438              :                                      NULL_RTX, NULL, both_ops_large,
    2439              :                                      profile_probability::unlikely ());
    2440              : 
    2441              :           /* If op1 is sign (!uns) or zero (uns) extended from hmode to mode,
    2442              :              but op0 is not, prepare larger, hipart and lopart pseudos and
    2443              :              handle it together with small_op0_large_op1.  */
    2444         3099 :           emit_move_insn (larger, op0);
    2445         3099 :           emit_move_insn (hipart, hipart0);
    2446         3099 :           emit_move_insn (lopart, lopart1);
    2447              : 
    2448         3099 :           emit_label (one_small_one_large);
    2449              : 
    2450              :           /* lopart is the low part of the operand that is sign extended
    2451              :              to mode, larger is the other operand, hipart is the
    2452              :              high part of larger and lopart0 and lopart1 are the low parts
    2453              :              of both operands.
    2454              :              We perform lopart0 * lopart1 and lopart * hipart widening
    2455              :              multiplications.  */
    2456         3099 :           tree halfutype = build_nonstandard_integer_type (hprec, 1);
    2457         3099 :           ops.op0 = make_tree (halfutype, lopart0);
    2458         3099 :           ops.op1 = make_tree (halfutype, lopart1);
    2459         3099 :           rtx lo0xlo1
    2460         3099 :             = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    2461              : 
    2462         3099 :           ops.op0 = make_tree (halfutype, lopart);
    2463         3099 :           ops.op1 = make_tree (halfutype, hipart);
    2464         3099 :           rtx loxhi = gen_reg_rtx (mode);
    2465         3099 :           rtx tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    2466         3099 :           emit_move_insn (loxhi, tem);
    2467              : 
    2468         3099 :           if (!uns)
    2469              :             {
    2470              :               /* if (hipart < 0) loxhi -= lopart << (bitsize / 2);  */
    2471         1126 :               if (larger_sign == 0)
    2472           72 :                 emit_jump (after_hipart_neg);
    2473         1054 :               else if (larger_sign != -1)
    2474         1054 :                 do_compare_rtx_and_jump (hipart, const0_rtx, GE, false, hmode,
    2475              :                                          NULL_RTX, NULL, after_hipart_neg,
    2476              :                                          profile_probability::even ());
    2477              : 
    2478         1126 :               tem = convert_modes (mode, hmode, lopart, 1);
    2479         1126 :               tem = expand_shift (LSHIFT_EXPR, mode, tem, hprec, NULL_RTX, 1);
    2480         1126 :               tem = expand_simple_binop (mode, MINUS, loxhi, tem, NULL_RTX,
    2481              :                                          1, OPTAB_WIDEN);
    2482         1126 :               emit_move_insn (loxhi, tem);
    2483              : 
    2484         1126 :               emit_label (after_hipart_neg);
    2485              : 
    2486              :               /* if (lopart < 0) loxhi -= larger;  */
    2487         1126 :               if (smaller_sign == 0)
    2488          251 :                 emit_jump (after_lopart_neg);
    2489          875 :               else if (smaller_sign != -1)
    2490          705 :                 do_compare_rtx_and_jump (lopart, const0_rtx, GE, false, hmode,
    2491              :                                          NULL_RTX, NULL, after_lopart_neg,
    2492              :                                          profile_probability::even ());
    2493              : 
    2494         1126 :               tem = expand_simple_binop (mode, MINUS, loxhi, larger, NULL_RTX,
    2495              :                                          1, OPTAB_WIDEN);
    2496         1126 :               emit_move_insn (loxhi, tem);
    2497              : 
    2498         1126 :               emit_label (after_lopart_neg);
    2499              :             }
    2500              : 
    2501              :           /* loxhi += (uns) lo0xlo1 >> (bitsize / 2);  */
    2502         3099 :           tem = expand_shift (RSHIFT_EXPR, mode, lo0xlo1, hprec, NULL_RTX, 1);
    2503         3099 :           tem = expand_simple_binop (mode, PLUS, loxhi, tem, NULL_RTX,
    2504              :                                      1, OPTAB_WIDEN);
    2505         3099 :           emit_move_insn (loxhi, tem);
    2506              : 
    2507              :           /* if (loxhi >> (bitsize / 2)
    2508              :                  == (hmode) loxhi >> (bitsize / 2 - 1))  (if !uns)
    2509              :              if (loxhi >> (bitsize / 2) == 0               (if uns).  */
    2510         3099 :           rtx hipartloxhi = expand_shift (RSHIFT_EXPR, mode, loxhi, hprec,
    2511              :                                           NULL_RTX, 0);
    2512         3099 :           hipartloxhi = convert_modes (hmode, mode, hipartloxhi, 0);
    2513         3099 :           rtx signbitloxhi = const0_rtx;
    2514         3099 :           if (!uns)
    2515         1126 :             signbitloxhi = expand_shift (RSHIFT_EXPR, hmode,
    2516              :                                          convert_modes (hmode, mode,
    2517              :                                                         loxhi, 0),
    2518         1126 :                                          hprec - 1, NULL_RTX, 0);
    2519              : 
    2520         3099 :           do_compare_rtx_and_jump (signbitloxhi, hipartloxhi, NE, true, hmode,
    2521              :                                    NULL_RTX, NULL, do_overflow,
    2522              :                                    profile_probability::very_unlikely ());
    2523              : 
    2524              :           /* res = (loxhi << (bitsize / 2)) | (hmode) lo0xlo1;  */
    2525         3099 :           rtx loxhishifted = expand_shift (LSHIFT_EXPR, mode, loxhi, hprec,
    2526              :                                            NULL_RTX, 1);
    2527         3099 :           tem = convert_modes (mode, hmode,
    2528              :                                convert_modes (hmode, mode, lo0xlo1, 1), 1);
    2529              : 
    2530         3099 :           tem = expand_simple_binop (mode, IOR, loxhishifted, tem, res,
    2531              :                                      1, OPTAB_WIDEN);
    2532         3099 :           if (tem != res)
    2533            0 :             emit_move_insn (res, tem);
    2534         3099 :           emit_jump (done_label);
    2535              : 
    2536         3099 :           emit_label (both_ops_large);
    2537              : 
    2538              :           /* If both operands are large (not sign (!uns) or zero (uns)
    2539              :              extended from hmode), then perform the full multiplication
    2540              :              which will be the result of the operation.
    2541              :              The only cases which don't overflow are for signed multiplication
    2542              :              some cases where both hipart0 and highpart1 are 0 or -1.
    2543              :              For unsigned multiplication when high parts are both non-zero
    2544              :              this overflows always.  */
    2545         3099 :           ops.code = MULT_EXPR;
    2546         3099 :           ops.op0 = make_tree (type, op0);
    2547         3099 :           ops.op1 = make_tree (type, op1);
    2548         3099 :           tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    2549         3099 :           emit_move_insn (res, tem);
    2550              : 
    2551         3099 :           if (!uns)
    2552              :             {
    2553         1126 :               if (!op0_medium_p)
    2554              :                 {
    2555         1126 :                   tem = expand_simple_binop (hmode, PLUS, hipart0, const1_rtx,
    2556              :                                              NULL_RTX, 1, OPTAB_WIDEN);
    2557         1126 :                   do_compare_rtx_and_jump (tem, const1_rtx, GTU, true, hmode,
    2558              :                                            NULL_RTX, NULL, do_error,
    2559              :                                            profile_probability::very_unlikely ());
    2560              :                 }
    2561              : 
    2562         1126 :               if (!op1_medium_p)
    2563              :                 {
    2564         1126 :                   tem = expand_simple_binop (hmode, PLUS, hipart1, const1_rtx,
    2565              :                                              NULL_RTX, 1, OPTAB_WIDEN);
    2566         1126 :                   do_compare_rtx_and_jump (tem, const1_rtx, GTU, true, hmode,
    2567              :                                            NULL_RTX, NULL, do_error,
    2568              :                                            profile_probability::very_unlikely ());
    2569              :                 }
    2570              : 
    2571              :               /* At this point hipart{0,1} are both in [-1, 0].  If they are
    2572              :                  the same, overflow happened if res is non-positive, if they
    2573              :                  are different, overflow happened if res is positive.  */
    2574         1126 :               if (op0_sign != 1 && op1_sign != 1 && op0_sign != op1_sign)
    2575           12 :                 emit_jump (hipart_different);
    2576         1114 :               else if (op0_sign == 1 || op1_sign == 1)
    2577         1058 :                 do_compare_rtx_and_jump (hipart0, hipart1, NE, true, hmode,
    2578              :                                          NULL_RTX, NULL, hipart_different,
    2579              :                                          profile_probability::even ());
    2580              : 
    2581         1126 :               do_compare_rtx_and_jump (res, const0_rtx, LE, false, mode,
    2582              :                                        NULL_RTX, NULL, do_error,
    2583              :                                        profile_probability::very_unlikely ());
    2584         1126 :               emit_jump (done_label);
    2585              : 
    2586         1126 :               emit_label (hipart_different);
    2587              : 
    2588         1126 :               do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode,
    2589              :                                        NULL_RTX, NULL, do_error,
    2590              :                                        profile_probability::very_unlikely ());
    2591         1126 :               emit_jump (done_label);
    2592              :             }
    2593              : 
    2594         3099 :           emit_label (do_overflow);
    2595              : 
    2596              :           /* Overflow, do full multiplication and fallthru into do_error.  */
    2597         3099 :           ops.op0 = make_tree (type, op0);
    2598         3099 :           ops.op1 = make_tree (type, op1);
    2599         3099 :           tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    2600         3099 :           emit_move_insn (res, tem);
    2601              :         }
    2602            0 :       else if (GET_MODE_2XWIDER_MODE (mode).exists (&wmode)
    2603            0 :                && targetm.scalar_mode_supported_p (wmode))
    2604              :         /* Even emitting a libcall is better than not detecting overflow
    2605              :            at all.  */
    2606            0 :         goto twoxwider;
    2607              :       else
    2608              :         {
    2609            0 :           gcc_assert (!is_ubsan);
    2610            0 :           ops.code = MULT_EXPR;
    2611            0 :           ops.type = type;
    2612            0 :           res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    2613            0 :           emit_jump (done_label);
    2614              :         }
    2615              :     }
    2616              : 
    2617        13890 :  do_error_label:
    2618        18963 :   emit_label (do_error);
    2619        18963 :   if (is_ubsan)
    2620              :     {
    2621              :       /* Expand the ubsan builtin call.  */
    2622         1512 :       push_temp_slots ();
    2623         1512 :       fn = ubsan_build_overflow_builtin (MULT_EXPR, loc, TREE_TYPE (orig_arg0),
    2624              :                                          orig_arg0, orig_arg1, datap);
    2625         1512 :       expand_normal (fn);
    2626         1512 :       pop_temp_slots ();
    2627         1512 :       do_pending_stack_adjust ();
    2628              :     }
    2629        17451 :   else if (lhs)
    2630        17451 :     expand_arith_set_overflow (lhs, target);
    2631              : 
    2632              :   /* We're done.  */
    2633        18963 :   emit_label (done_label);
    2634              : 
    2635              :   /* u1 * u2 -> sr  */
    2636        18963 :   if (uns0_p && uns1_p && !unsr_p)
    2637              :     {
    2638          957 :       rtx_code_label *all_done_label = gen_label_rtx ();
    2639          957 :       do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, NULL_RTX,
    2640              :                                NULL, all_done_label, profile_probability::very_likely ());
    2641          957 :       expand_arith_set_overflow (lhs, target);
    2642          957 :       emit_label (all_done_label);
    2643              :     }
    2644              : 
    2645              :   /* s1 * u2 -> sr  */
    2646        18963 :   if (!uns0_p && uns1_p && !unsr_p && pos_neg1 == 3)
    2647              :     {
    2648         1273 :       rtx_code_label *all_done_label = gen_label_rtx ();
    2649         1273 :       rtx_code_label *set_noovf = gen_label_rtx ();
    2650         1273 :       do_compare_rtx_and_jump (op1, const0_rtx, GE, false, mode, NULL_RTX,
    2651              :                                NULL, all_done_label, profile_probability::very_likely ());
    2652         1273 :       expand_arith_set_overflow (lhs, target);
    2653         1273 :       do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
    2654              :                                NULL, set_noovf, profile_probability::very_likely ());
    2655         1273 :       do_compare_rtx_and_jump (op0, constm1_rtx, NE, true, mode, NULL_RTX,
    2656              :                                NULL, all_done_label, profile_probability::very_unlikely ());
    2657         1273 :       do_compare_rtx_and_jump (op1, res, NE, true, mode, NULL_RTX, NULL,
    2658              :                                all_done_label, profile_probability::very_unlikely ());
    2659         1273 :       emit_label (set_noovf);
    2660         1273 :       write_complex_part (target, const0_rtx, true, false);
    2661         1273 :       emit_label (all_done_label);
    2662              :     }
    2663              : 
    2664        18963 :   if (lhs)
    2665              :     {
    2666        18819 :       if (is_ubsan)
    2667         1368 :         expand_ubsan_result_store (lhs, target, mode, res, do_error);
    2668              :       else
    2669        17451 :         expand_arith_overflow_result_store (lhs, target, mode, res,
    2670              :                                             overflow_checked);
    2671              :     }
    2672        18963 :   flag_trapv = save_flag_trapv;
    2673        18963 : }
    2674              : 
    2675              : /* Expand UBSAN_CHECK_* internal function if it has vector operands.  */
    2676              : 
    2677              : static void
    2678          567 : expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
    2679              :                               tree arg0, tree arg1)
    2680              : {
    2681          567 :   poly_uint64 cnt = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
    2682          567 :   rtx_code_label *loop_lab = NULL;
    2683          567 :   rtx cntvar = NULL_RTX;
    2684          567 :   tree cntv = NULL_TREE;
    2685          567 :   tree eltype = TREE_TYPE (TREE_TYPE (arg0));
    2686          567 :   tree sz = TYPE_SIZE (eltype);
    2687          567 :   tree data = NULL_TREE;
    2688          567 :   tree resv = NULL_TREE;
    2689          567 :   rtx lhsr = NULL_RTX;
    2690          567 :   rtx resvr = NULL_RTX;
    2691          567 :   unsigned HOST_WIDE_INT const_cnt = 0;
    2692          567 :   bool use_loop_p = (!cnt.is_constant (&const_cnt) || const_cnt > 4);
    2693          567 :   int save_flag_trapv = flag_trapv;
    2694              : 
    2695              :   /* We don't want any __mulv?i3 etc. calls from the expansion of
    2696              :      these internal functions, so disable -ftrapv temporarily.  */
    2697          567 :   flag_trapv = 0;
    2698          567 :   if (lhs)
    2699              :     {
    2700          567 :       optab op;
    2701          567 :       lhsr = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    2702          141 :       if (!VECTOR_MODE_P (GET_MODE (lhsr))
    2703          426 :           || (op = optab_for_tree_code (code, TREE_TYPE (arg0),
    2704              :                                         optab_default)) == unknown_optab
    2705          993 :           || (optab_handler (op, TYPE_MODE (TREE_TYPE (arg0)))
    2706              :               == CODE_FOR_nothing))
    2707              :         {
    2708          141 :           if (MEM_P (lhsr))
    2709          141 :             resv = make_tree (TREE_TYPE (lhs), lhsr);
    2710              :           else
    2711              :             {
    2712            0 :               resvr = assign_temp (TREE_TYPE (lhs), 1, 1);
    2713            0 :               resv = make_tree (TREE_TYPE (lhs), resvr);
    2714              :             }
    2715              :         }
    2716              :     }
    2717          567 :   if (use_loop_p)
    2718              :     {
    2719          417 :       do_pending_stack_adjust ();
    2720          417 :       loop_lab = gen_label_rtx ();
    2721          417 :       cntvar = gen_reg_rtx (TYPE_MODE (sizetype));
    2722          417 :       cntv = make_tree (sizetype, cntvar);
    2723          417 :       emit_move_insn (cntvar, const0_rtx);
    2724          417 :       emit_label (loop_lab);
    2725              :     }
    2726          567 :   if (TREE_CODE (arg0) != VECTOR_CST)
    2727              :     {
    2728          461 :       rtx arg0r = expand_normal (arg0);
    2729          461 :       arg0 = make_tree (TREE_TYPE (arg0), arg0r);
    2730              :     }
    2731          567 :   if (TREE_CODE (arg1) != VECTOR_CST)
    2732              :     {
    2733          555 :       rtx arg1r = expand_normal (arg1);
    2734          555 :       arg1 = make_tree (TREE_TYPE (arg1), arg1r);
    2735              :     }
    2736         2334 :   for (unsigned int i = 0; i < (use_loop_p ? 1 : const_cnt); i++)
    2737              :     {
    2738         1017 :       tree op0, op1, res = NULL_TREE;
    2739         1017 :       if (use_loop_p)
    2740              :         {
    2741          417 :           tree atype = build_array_type_nelts (eltype, cnt);
    2742          417 :           op0 = uniform_vector_p (arg0);
    2743          417 :           if (op0 == NULL_TREE)
    2744              :             {
    2745          351 :               op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, arg0);
    2746          351 :               op0 = build4_loc (loc, ARRAY_REF, eltype, op0, cntv,
    2747              :                                 NULL_TREE, NULL_TREE);
    2748              :             }
    2749          417 :           op1 = uniform_vector_p (arg1);
    2750          417 :           if (op1 == NULL_TREE)
    2751              :             {
    2752          411 :               op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, arg1);
    2753          411 :               op1 = build4_loc (loc, ARRAY_REF, eltype, op1, cntv,
    2754              :                                 NULL_TREE, NULL_TREE);
    2755              :             }
    2756          417 :           if (resv)
    2757              :             {
    2758          141 :               res = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, resv);
    2759          141 :               res = build4_loc (loc, ARRAY_REF, eltype, res, cntv,
    2760              :                                 NULL_TREE, NULL_TREE);
    2761              :             }
    2762              :         }
    2763              :       else
    2764              :         {
    2765          600 :           tree bitpos = bitsize_int (tree_to_uhwi (sz) * i);
    2766          600 :           op0 = fold_build3_loc (loc, BIT_FIELD_REF, eltype, arg0, sz, bitpos);
    2767          600 :           op1 = fold_build3_loc (loc, BIT_FIELD_REF, eltype, arg1, sz, bitpos);
    2768          600 :           if (resv)
    2769            0 :             res = fold_build3_loc (loc, BIT_FIELD_REF, eltype, resv, sz,
    2770              :                                    bitpos);
    2771              :         }
    2772         1017 :       switch (code)
    2773              :         {
    2774          317 :         case PLUS_EXPR:
    2775          317 :           expand_addsub_overflow (loc, PLUS_EXPR, res, op0, op1,
    2776              :                                   false, false, false, true, &data);
    2777          317 :           break;
    2778          534 :         case MINUS_EXPR:
    2779          534 :           if (use_loop_p ? integer_zerop (arg0) : integer_zerop (op0))
    2780          226 :             expand_neg_overflow (loc, res, op1, true, &data);
    2781              :           else
    2782          308 :             expand_addsub_overflow (loc, MINUS_EXPR, res, op0, op1,
    2783              :                                     false, false, false, true, &data);
    2784              :           break;
    2785          166 :         case MULT_EXPR:
    2786          166 :           expand_mul_overflow (loc, res, op0, op1, false, false, false,
    2787              :                                true, &data);
    2788          166 :           break;
    2789            0 :         default:
    2790            0 :           gcc_unreachable ();
    2791              :         }
    2792              :     }
    2793          567 :   if (use_loop_p)
    2794              :     {
    2795          417 :       struct separate_ops ops;
    2796          417 :       ops.code = PLUS_EXPR;
    2797          417 :       ops.type = TREE_TYPE (cntv);
    2798          417 :       ops.op0 = cntv;
    2799          417 :       ops.op1 = build_int_cst (TREE_TYPE (cntv), 1);
    2800          417 :       ops.op2 = NULL_TREE;
    2801          417 :       ops.location = loc;
    2802          417 :       rtx ret = expand_expr_real_2 (&ops, cntvar, TYPE_MODE (sizetype),
    2803              :                                     EXPAND_NORMAL);
    2804          417 :       if (ret != cntvar)
    2805            0 :         emit_move_insn (cntvar, ret);
    2806          417 :       rtx cntrtx = gen_int_mode (cnt, TYPE_MODE (sizetype));
    2807          834 :       do_compare_rtx_and_jump (cntvar, cntrtx, NE, false,
    2808          417 :                                TYPE_MODE (sizetype), NULL_RTX, NULL, loop_lab,
    2809              :                                profile_probability::very_likely ());
    2810              :     }
    2811          567 :   if (lhs && resv == NULL_TREE)
    2812              :     {
    2813          426 :       struct separate_ops ops;
    2814          426 :       ops.code = code;
    2815          426 :       ops.type = TREE_TYPE (arg0);
    2816          426 :       ops.op0 = arg0;
    2817          426 :       ops.op1 = arg1;
    2818          426 :       ops.op2 = NULL_TREE;
    2819          426 :       ops.location = loc;
    2820          426 :       rtx ret = expand_expr_real_2 (&ops, lhsr, TYPE_MODE (TREE_TYPE (arg0)),
    2821              :                                     EXPAND_NORMAL);
    2822          426 :       if (ret != lhsr)
    2823            5 :         emit_move_insn (lhsr, ret);
    2824              :     }
    2825          141 :   else if (resvr)
    2826            0 :     emit_move_insn (lhsr, resvr);
    2827          567 :   flag_trapv = save_flag_trapv;
    2828          567 : }
    2829              : 
    2830              : /* Expand UBSAN_CHECK_ADD call STMT.  */
    2831              : 
    2832              : static void
    2833         1816 : expand_UBSAN_CHECK_ADD (internal_fn, gcall *stmt)
    2834              : {
    2835         1816 :   location_t loc = gimple_location (stmt);
    2836         1816 :   tree lhs = gimple_call_lhs (stmt);
    2837         1816 :   tree arg0 = gimple_call_arg (stmt, 0);
    2838         1816 :   tree arg1 = gimple_call_arg (stmt, 1);
    2839         1816 :   if (VECTOR_TYPE_P (TREE_TYPE (arg0)))
    2840          185 :     expand_vector_ubsan_overflow (loc, PLUS_EXPR, lhs, arg0, arg1);
    2841              :   else
    2842         1631 :     expand_addsub_overflow (loc, PLUS_EXPR, lhs, arg0, arg1,
    2843              :                             false, false, false, true, NULL);
    2844         1816 : }
    2845              : 
    2846              : /* Expand UBSAN_CHECK_SUB call STMT.  */
    2847              : 
    2848              : static void
    2849         1923 : expand_UBSAN_CHECK_SUB (internal_fn, gcall *stmt)
    2850              : {
    2851         1923 :   location_t loc = gimple_location (stmt);
    2852         1923 :   tree lhs = gimple_call_lhs (stmt);
    2853         1923 :   tree arg0 = gimple_call_arg (stmt, 0);
    2854         1923 :   tree arg1 = gimple_call_arg (stmt, 1);
    2855         1923 :   if (VECTOR_TYPE_P (TREE_TYPE (arg0)))
    2856          282 :     expand_vector_ubsan_overflow (loc, MINUS_EXPR, lhs, arg0, arg1);
    2857         1641 :   else if (integer_zerop (arg0))
    2858          436 :     expand_neg_overflow (loc, lhs, arg1, true, NULL);
    2859              :   else
    2860         1205 :     expand_addsub_overflow (loc, MINUS_EXPR, lhs, arg0, arg1,
    2861              :                             false, false, false, true, NULL);
    2862         1923 : }
    2863              : 
    2864              : /* Expand UBSAN_CHECK_MUL call STMT.  */
    2865              : 
    2866              : static void
    2867         1446 : expand_UBSAN_CHECK_MUL (internal_fn, gcall *stmt)
    2868              : {
    2869         1446 :   location_t loc = gimple_location (stmt);
    2870         1446 :   tree lhs = gimple_call_lhs (stmt);
    2871         1446 :   tree arg0 = gimple_call_arg (stmt, 0);
    2872         1446 :   tree arg1 = gimple_call_arg (stmt, 1);
    2873         1446 :   if (VECTOR_TYPE_P (TREE_TYPE (arg0)))
    2874          100 :     expand_vector_ubsan_overflow (loc, MULT_EXPR, lhs, arg0, arg1);
    2875              :   else
    2876         1346 :     expand_mul_overflow (loc, lhs, arg0, arg1, false, false, false, true,
    2877              :                          NULL);
    2878         1446 : }
    2879              : 
    2880              : /* Helper function for {ADD,SUB,MUL}_OVERFLOW call stmt expansion.  */
    2881              : 
    2882              : static void
    2883        72067 : expand_arith_overflow (enum tree_code code, gimple *stmt)
    2884              : {
    2885        72067 :   tree lhs = gimple_call_lhs (stmt);
    2886        72067 :   if (lhs == NULL_TREE)
    2887              :     return;
    2888        72067 :   tree arg0 = gimple_call_arg (stmt, 0);
    2889        72067 :   tree arg1 = gimple_call_arg (stmt, 1);
    2890        72067 :   tree type = TREE_TYPE (TREE_TYPE (lhs));
    2891        72067 :   int uns0_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
    2892        72067 :   int uns1_p = TYPE_UNSIGNED (TREE_TYPE (arg1));
    2893        72067 :   int unsr_p = TYPE_UNSIGNED (type);
    2894        72067 :   int prec0 = TYPE_PRECISION (TREE_TYPE (arg0));
    2895        72067 :   int prec1 = TYPE_PRECISION (TREE_TYPE (arg1));
    2896        72067 :   int precres = TYPE_PRECISION (type);
    2897        72067 :   location_t loc = gimple_location (stmt);
    2898        72067 :   if (!uns0_p && get_range_pos_neg (arg0, stmt) == 1)
    2899              :     uns0_p = true;
    2900        72067 :   if (!uns1_p && get_range_pos_neg (arg1, stmt) == 1)
    2901              :     uns1_p = true;
    2902       106409 :   int pr = get_min_precision (arg0, uns0_p ? UNSIGNED : SIGNED);
    2903        72067 :   prec0 = MIN (prec0, pr);
    2904        97243 :   pr = get_min_precision (arg1, uns1_p ? UNSIGNED : SIGNED);
    2905        72067 :   prec1 = MIN (prec1, pr);
    2906        72067 :   int save_flag_trapv = flag_trapv;
    2907              : 
    2908              :   /* We don't want any __mulv?i3 etc. calls from the expansion of
    2909              :      these internal functions, so disable -ftrapv temporarily.  */
    2910        72067 :   flag_trapv = 0;
    2911              :   /* If uns0_p && uns1_p, precop is minimum needed precision
    2912              :      of unsigned type to hold the exact result, otherwise
    2913              :      precop is minimum needed precision of signed type to
    2914              :      hold the exact result.  */
    2915        72067 :   int precop;
    2916        72067 :   if (code == MULT_EXPR)
    2917        23458 :     precop = prec0 + prec1 + (uns0_p != uns1_p);
    2918              :   else
    2919              :     {
    2920        48609 :       if (uns0_p == uns1_p)
    2921        31441 :         precop = MAX (prec0, prec1) + 1;
    2922        17168 :       else if (uns0_p)
    2923         5815 :         precop = MAX (prec0 + 1, prec1) + 1;
    2924              :       else
    2925        11353 :         precop = MAX (prec0, prec1 + 1) + 1;
    2926              :     }
    2927        72067 :   int orig_precres = precres;
    2928              : 
    2929        91507 :   do
    2930              :     {
    2931        91507 :       if ((uns0_p && uns1_p)
    2932        91507 :           ? ((precop + !unsr_p) <= precres
    2933              :              /* u1 - u2 -> ur can overflow, no matter what precision
    2934              :                 the result has.  */
    2935        33750 :              && (code != MINUS_EXPR || !unsr_p))
    2936        57757 :           : (!unsr_p && precop <= precres))
    2937              :         {
    2938              :           /* The infinity precision result will always fit into result.  */
    2939        18525 :           rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    2940        18525 :           write_complex_part (target, const0_rtx, true, false);
    2941        18525 :           scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
    2942        18525 :           struct separate_ops ops;
    2943        18525 :           ops.code = code;
    2944        18525 :           ops.type = type;
    2945        18525 :           ops.op0 = fold_convert_loc (loc, type, arg0);
    2946        18525 :           ops.op1 = fold_convert_loc (loc, type, arg1);
    2947        18525 :           ops.op2 = NULL_TREE;
    2948        18525 :           ops.location = loc;
    2949        18525 :           rtx tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    2950        18525 :           expand_arith_overflow_result_store (lhs, target, mode, tem);
    2951        18525 :           flag_trapv = save_flag_trapv;
    2952        18525 :           return;
    2953              :         }
    2954              : 
    2955              :       /* For operations with low precision, if target doesn't have them, start
    2956              :          with precres widening right away, otherwise do it only if the most
    2957              :          simple cases can't be used.  */
    2958        72982 :       const int min_precision = targetm.min_arithmetic_precision ();
    2959        72982 :       if (orig_precres == precres && precres < min_precision)
    2960              :         ;
    2961        71715 :       else if ((uns0_p && uns1_p && unsr_p && prec0 <= precres
    2962        20901 :                 && prec1 <= precres)
    2963        50817 :           || ((!uns0_p || !uns1_p) && !unsr_p
    2964        20780 :               && prec0 + uns0_p <= precres
    2965        17578 :               && prec1 + uns1_p <= precres))
    2966              :         {
    2967        34318 :           arg0 = fold_convert_loc (loc, type, arg0);
    2968        34318 :           arg1 = fold_convert_loc (loc, type, arg1);
    2969        34318 :           switch (code)
    2970              :             {
    2971         9871 :             case MINUS_EXPR:
    2972         9871 :               if (integer_zerop (arg0) && !unsr_p)
    2973              :                 {
    2974          402 :                   expand_neg_overflow (loc, lhs, arg1, false, NULL);
    2975          402 :                   flag_trapv = save_flag_trapv;
    2976          402 :                   return;
    2977              :                 }
    2978              :               /* FALLTHRU */
    2979        24205 :             case PLUS_EXPR:
    2980        24205 :               expand_addsub_overflow (loc, code, lhs, arg0, arg1, unsr_p,
    2981              :                                       unsr_p, unsr_p, false, NULL);
    2982        24205 :               flag_trapv = save_flag_trapv;
    2983        24205 :               return;
    2984         9711 :             case MULT_EXPR:
    2985         9711 :               expand_mul_overflow (loc, lhs, arg0, arg1, unsr_p,
    2986              :                                    unsr_p, unsr_p, false, NULL);
    2987         9711 :               flag_trapv = save_flag_trapv;
    2988         9711 :               return;
    2989            0 :             default:
    2990            0 :               gcc_unreachable ();
    2991              :             }
    2992              :         }
    2993              : 
    2994              :       /* For sub-word operations, retry with a wider type first.  */
    2995        38665 :       if (orig_precres == precres && precop <= BITS_PER_WORD)
    2996              :         {
    2997        19508 :           int p = MAX (min_precision, precop);
    2998        19508 :           scalar_int_mode m = smallest_int_mode_for_size (p).require ();
    2999        19508 :           tree optype = build_nonstandard_integer_type (GET_MODE_PRECISION (m),
    3000              :                                                         uns0_p && uns1_p
    3001        19508 :                                                         && unsr_p);
    3002        19508 :           p = TYPE_PRECISION (optype);
    3003        19508 :           if (p > precres)
    3004              :             {
    3005        18805 :               precres = p;
    3006        18805 :               unsr_p = TYPE_UNSIGNED (optype);
    3007        18805 :               type = optype;
    3008        18805 :               continue;
    3009              :             }
    3010              :         }
    3011              : 
    3012        19859 :       if (prec0 <= precres && prec1 <= precres)
    3013              :         {
    3014        19224 :           tree types[2];
    3015        19224 :           if (unsr_p)
    3016              :             {
    3017        11083 :               types[0] = build_nonstandard_integer_type (precres, 0);
    3018        11083 :               types[1] = type;
    3019              :             }
    3020              :           else
    3021              :             {
    3022         8141 :               types[0] = type;
    3023         8141 :               types[1] = build_nonstandard_integer_type (precres, 1);
    3024              :             }
    3025        19224 :           arg0 = fold_convert_loc (loc, types[uns0_p], arg0);
    3026        19224 :           arg1 = fold_convert_loc (loc, types[uns1_p], arg1);
    3027        19224 :           if (code != MULT_EXPR)
    3028        11484 :             expand_addsub_overflow (loc, code, lhs, arg0, arg1, unsr_p,
    3029              :                                     uns0_p, uns1_p, false, NULL);
    3030              :           else
    3031         7740 :             expand_mul_overflow (loc, lhs, arg0, arg1, unsr_p,
    3032              :                                  uns0_p, uns1_p, false, NULL);
    3033        19224 :           flag_trapv = save_flag_trapv;
    3034        19224 :           return;
    3035              :         }
    3036              : 
    3037              :       /* Retry with a wider type.  */
    3038          635 :       if (orig_precres == precres)
    3039              :         {
    3040          635 :           int p = MAX (prec0, prec1);
    3041          635 :           scalar_int_mode m = smallest_int_mode_for_size (p).require ();
    3042          635 :           tree optype = build_nonstandard_integer_type (GET_MODE_PRECISION (m),
    3043              :                                                         uns0_p && uns1_p
    3044          635 :                                                         && unsr_p);
    3045          635 :           p = TYPE_PRECISION (optype);
    3046          635 :           if (p > precres)
    3047              :             {
    3048          635 :               precres = p;
    3049          635 :               unsr_p = TYPE_UNSIGNED (optype);
    3050          635 :               type = optype;
    3051          635 :               continue;
    3052              :             }
    3053              :         }
    3054              : 
    3055            0 :       gcc_unreachable ();
    3056              :     }
    3057              :   while (1);
    3058              : }
    3059              : 
    3060              : /* Expand ADD_OVERFLOW STMT.  */
    3061              : 
    3062              : static void
    3063        25107 : expand_ADD_OVERFLOW (internal_fn, gcall *stmt)
    3064              : {
    3065        25107 :   expand_arith_overflow (PLUS_EXPR, stmt);
    3066        25107 : }
    3067              : 
    3068              : /* Expand SUB_OVERFLOW STMT.  */
    3069              : 
    3070              : static void
    3071        23502 : expand_SUB_OVERFLOW (internal_fn, gcall *stmt)
    3072              : {
    3073        23502 :   expand_arith_overflow (MINUS_EXPR, stmt);
    3074        23502 : }
    3075              : 
    3076              : /* Expand MUL_OVERFLOW STMT.  */
    3077              : 
    3078              : static void
    3079        23458 : expand_MUL_OVERFLOW (internal_fn, gcall *stmt)
    3080              : {
    3081        23458 :   expand_arith_overflow (MULT_EXPR, stmt);
    3082        23458 : }
    3083              : 
    3084              : /* Expand UADDC STMT.  */
    3085              : 
    3086              : static void
    3087        17294 : expand_UADDC (internal_fn ifn, gcall *stmt)
    3088              : {
    3089        17294 :   tree lhs = gimple_call_lhs (stmt);
    3090        17294 :   tree arg1 = gimple_call_arg (stmt, 0);
    3091        17294 :   tree arg2 = gimple_call_arg (stmt, 1);
    3092        17294 :   tree arg3 = gimple_call_arg (stmt, 2);
    3093        17294 :   tree type = TREE_TYPE (arg1);
    3094        17294 :   machine_mode mode = TYPE_MODE (type);
    3095        23806 :   insn_code icode = optab_handler (ifn == IFN_UADDC
    3096              :                                    ? uaddc5_optab : usubc5_optab, mode);
    3097        17294 :   rtx op1 = expand_normal (arg1);
    3098        17294 :   rtx op2 = expand_normal (arg2);
    3099        17294 :   rtx op3 = expand_normal (arg3);
    3100        17294 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3101        17294 :   rtx re = gen_reg_rtx (mode);
    3102        17294 :   rtx im = gen_reg_rtx (mode);
    3103        17294 :   class expand_operand ops[5];
    3104        17294 :   create_output_operand (&ops[0], re, mode);
    3105        17294 :   create_output_operand (&ops[1], im, mode);
    3106        17294 :   create_input_operand (&ops[2], op1, mode);
    3107        17294 :   create_input_operand (&ops[3], op2, mode);
    3108        17294 :   create_input_operand (&ops[4], op3, mode);
    3109        17294 :   expand_insn (icode, 5, ops);
    3110        17294 :   write_complex_part (target, re, false, false);
    3111        17294 :   write_complex_part (target, im, true, false);
    3112        17294 : }
    3113              : 
    3114              : /* Expand USUBC STMT.  */
    3115              : 
    3116              : static void
    3117         6512 : expand_USUBC (internal_fn ifn, gcall *stmt)
    3118              : {
    3119         6512 :   expand_UADDC (ifn, stmt);
    3120         6512 : }
    3121              : 
    3122              : /* This should get folded in tree-vectorizer.cc.  */
    3123              : 
    3124              : static void
    3125            0 : expand_LOOP_VECTORIZED (internal_fn, gcall *)
    3126              : {
    3127            0 :   gcc_unreachable ();
    3128              : }
    3129              : 
    3130              : /* This should get folded in tree-vectorizer.cc.  */
    3131              : 
    3132              : static void
    3133            0 : expand_LOOP_DIST_ALIAS (internal_fn, gcall *)
    3134              : {
    3135            0 :   gcc_unreachable ();
    3136              : }
    3137              : 
    3138              : /* Return a memory reference of type TYPE for argument INDEX of STMT.
    3139              :    Use argument INDEX + 1 to derive the second (TBAA) operand.  */
    3140              : 
    3141              : static tree
    3142         1503 : expand_call_mem_ref (tree type, gcall *stmt, int index)
    3143              : {
    3144         1503 :   tree addr = gimple_call_arg (stmt, index);
    3145         1503 :   tree alias_ptr_type = TREE_TYPE (gimple_call_arg (stmt, index + 1));
    3146         1503 :   unsigned int align = tree_to_shwi (gimple_call_arg (stmt, index + 1));
    3147         1503 :   if (TYPE_ALIGN (type) != align)
    3148         1235 :     type = build_aligned_type (type, align);
    3149              : 
    3150         1503 :   tree tmp = addr;
    3151         1503 :   if (TREE_CODE (tmp) == SSA_NAME)
    3152              :     {
    3153         1365 :       gimple *def = get_gimple_for_ssa_name (tmp);
    3154         1365 :       if (def && gimple_assign_single_p (def))
    3155          631 :         tmp = gimple_assign_rhs1 (def);
    3156              :     }
    3157              : 
    3158         1503 :   if (TREE_CODE (tmp) == ADDR_EXPR)
    3159              :     {
    3160          769 :       tree mem = TREE_OPERAND (tmp, 0);
    3161          769 :       if (TREE_CODE (mem) == TARGET_MEM_REF
    3162          769 :           && types_compatible_p (TREE_TYPE (mem), type))
    3163              :         {
    3164          631 :           tree offset = TMR_OFFSET (mem);
    3165          631 :           if (type != TREE_TYPE (mem)
    3166          132 :               || alias_ptr_type != TREE_TYPE (offset)
    3167          763 :               || !integer_zerop (offset))
    3168              :             {
    3169          526 :               mem = copy_node (mem);
    3170         1052 :               TMR_OFFSET (mem) = wide_int_to_tree (alias_ptr_type,
    3171          526 :                                                    wi::to_poly_wide (offset));
    3172          526 :               TREE_TYPE (mem) = type;
    3173              :             }
    3174              :           return mem;
    3175              :         }
    3176              :     }
    3177              : 
    3178          872 :   return fold_build2 (MEM_REF, type, addr, build_int_cst (alias_ptr_type, 0));
    3179              : }
    3180              : 
    3181              : /* Expand MASK_LOAD{,_LANES}, MASK_LEN_LOAD or LEN_LOAD call STMT using optab
    3182              :  * OPTAB.  */
    3183              : 
    3184              : static void
    3185          726 : expand_partial_load_optab_fn (internal_fn ifn, gcall *stmt, convert_optab optab)
    3186              : {
    3187          726 :   int i = 0;
    3188          726 :   class expand_operand ops[6];
    3189          726 :   tree type, lhs, rhs, maskt;
    3190          726 :   rtx mem, target;
    3191          726 :   insn_code icode;
    3192              : 
    3193          726 :   maskt = gimple_call_arg (stmt, internal_fn_mask_index (ifn));
    3194          726 :   lhs = gimple_call_lhs (stmt);
    3195          726 :   if (lhs == NULL_TREE)
    3196            0 :     return;
    3197          726 :   type = TREE_TYPE (lhs);
    3198          726 :   rhs = expand_call_mem_ref (type, stmt, 0);
    3199              : 
    3200          726 :   if (optab == vec_mask_load_lanes_optab
    3201          726 :       || optab == vec_mask_len_load_lanes_optab)
    3202            0 :     icode = get_multi_vector_move (type, optab);
    3203          726 :   else if (optab == len_load_optab)
    3204            0 :     icode = direct_optab_handler (optab, TYPE_MODE (type));
    3205              :   else
    3206          726 :     icode = convert_optab_handler (optab, TYPE_MODE (type),
    3207          726 :                                    TYPE_MODE (TREE_TYPE (maskt)));
    3208              : 
    3209          726 :   mem = expand_expr (rhs, NULL_RTX, VOIDmode, EXPAND_MEMORY);
    3210          726 :   gcc_assert (MEM_P (mem));
    3211          726 :   MEM_NOTRAP_P (mem) = !tree_could_trap_p (rhs);
    3212              :   /* The built MEM_REF does not accurately reflect that the load
    3213              :      is only partial.  Clear it.  */
    3214          726 :   set_mem_expr (mem, NULL_TREE);
    3215          726 :   clear_mem_offset (mem);
    3216          726 :   target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3217          726 :   create_call_lhs_operand (&ops[i++], target, TYPE_MODE (type));
    3218          726 :   create_fixed_operand (&ops[i++], mem);
    3219          726 :   i = add_mask_else_and_len_args (ops, i, stmt);
    3220          726 :   expand_insn (icode, i, ops);
    3221              : 
    3222          726 :   assign_call_lhs (lhs, target, &ops[0]);
    3223              : }
    3224              : 
    3225              : #define expand_mask_load_optab_fn expand_partial_load_optab_fn
    3226              : #define expand_mask_load_lanes_optab_fn expand_mask_load_optab_fn
    3227              : #define expand_mask_len_load_lanes_optab_fn expand_mask_load_optab_fn
    3228              : #define expand_len_load_optab_fn expand_partial_load_optab_fn
    3229              : #define expand_mask_len_load_optab_fn expand_partial_load_optab_fn
    3230              : 
    3231              : /* Expand MASK_STORE{,_LANES}, MASK_LEN_STORE or LEN_STORE call STMT using optab
    3232              :  * OPTAB.  */
    3233              : 
    3234              : static void
    3235          777 : expand_partial_store_optab_fn (internal_fn ifn, gcall *stmt, convert_optab optab)
    3236              : {
    3237          777 :   int i = 0;
    3238          777 :   class expand_operand ops[5];
    3239          777 :   tree type, lhs, rhs, maskt;
    3240          777 :   rtx mem, reg;
    3241          777 :   insn_code icode;
    3242              : 
    3243          777 :   maskt = gimple_call_arg (stmt, internal_fn_mask_index (ifn));
    3244          777 :   rhs = gimple_call_arg (stmt, internal_fn_stored_value_index (ifn));
    3245          777 :   type = TREE_TYPE (rhs);
    3246          777 :   lhs = expand_call_mem_ref (type, stmt, 0);
    3247              : 
    3248          777 :   if (optab == vec_mask_store_lanes_optab
    3249          777 :       || optab == vec_mask_len_store_lanes_optab)
    3250            0 :     icode = get_multi_vector_move (type, optab);
    3251          777 :   else if (optab == len_store_optab)
    3252            0 :     icode = direct_optab_handler (optab, TYPE_MODE (type));
    3253              :   else
    3254          777 :     icode = convert_optab_handler (optab, TYPE_MODE (type),
    3255          777 :                                    TYPE_MODE (TREE_TYPE (maskt)));
    3256              : 
    3257          777 :   mem = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3258          777 :   gcc_assert (MEM_P (mem));
    3259              :   /* The built MEM_REF does not accurately reflect that the store
    3260              :      is only partial.  Clear it.  */
    3261          777 :   set_mem_expr (mem, NULL_TREE);
    3262          777 :   clear_mem_offset (mem);
    3263          777 :   reg = expand_normal (rhs);
    3264          777 :   create_fixed_operand (&ops[i++], mem);
    3265          777 :   create_input_operand (&ops[i++], reg, TYPE_MODE (type));
    3266          777 :   i = add_mask_else_and_len_args (ops, i, stmt);
    3267          777 :   expand_insn (icode, i, ops);
    3268          777 : }
    3269              : 
    3270              : #define expand_mask_store_optab_fn expand_partial_store_optab_fn
    3271              : #define expand_mask_store_lanes_optab_fn expand_mask_store_optab_fn
    3272              : #define expand_mask_len_store_lanes_optab_fn expand_mask_store_optab_fn
    3273              : #define expand_len_store_optab_fn expand_partial_store_optab_fn
    3274              : #define expand_mask_len_store_optab_fn expand_partial_store_optab_fn
    3275              : 
    3276              : /* Expand VCOND_MASK optab internal function.
    3277              :    The expansion of STMT happens based on OPTAB table associated.  */
    3278              : 
    3279              : static void
    3280         9626 : expand_vec_cond_mask_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
    3281              : {
    3282         9626 :   class expand_operand ops[4];
    3283              : 
    3284         9626 :   tree lhs = gimple_call_lhs (stmt);
    3285         9626 :   tree op0 = gimple_call_arg (stmt, 0);
    3286         9626 :   tree op1 = gimple_call_arg (stmt, 1);
    3287         9626 :   tree op2 = gimple_call_arg (stmt, 2);
    3288         9626 :   tree vec_cond_type = TREE_TYPE (lhs);
    3289              : 
    3290         9626 :   machine_mode mode = TYPE_MODE (vec_cond_type);
    3291         9626 :   machine_mode mask_mode = TYPE_MODE (TREE_TYPE (op0));
    3292         9626 :   enum insn_code icode = convert_optab_handler (optab, mode, mask_mode);
    3293         9626 :   rtx mask, rtx_op1, rtx_op2;
    3294              : 
    3295         9626 :   gcc_assert (icode != CODE_FOR_nothing);
    3296              : 
    3297              :   /* Find the comparison generating the mask OP0.  */
    3298         9626 :   tree cmp_op0 = NULL_TREE;
    3299         9626 :   tree cmp_op1 = NULL_TREE;
    3300         9626 :   enum tree_code cmp_code = TREE_CODE (op0);
    3301         9626 :   if (TREE_CODE_CLASS (cmp_code) == tcc_comparison)
    3302              :     {
    3303            0 :       cmp_op0 = TREE_OPERAND (op0, 0);
    3304            0 :       cmp_op1 = TREE_OPERAND (op0, 1);
    3305              :     }
    3306         9626 :   else if (cmp_code == SSA_NAME)
    3307              :     {
    3308         8290 :       if (auto *def_stmt
    3309         8290 :           = safe_dyn_cast<gassign *> (get_gimple_for_ssa_name (op0)))
    3310              :         {
    3311         6618 :           cmp_code = gimple_assign_rhs_code (def_stmt);
    3312         6618 :           if (TREE_CODE_CLASS (cmp_code) == tcc_comparison)
    3313              :             {
    3314         4877 :               cmp_op0 = gimple_assign_rhs1 (def_stmt);
    3315         4877 :               cmp_op1 = gimple_assign_rhs2 (def_stmt);
    3316              :             }
    3317              :         }
    3318              :     }
    3319              : 
    3320              :   /* Decide whether to invert comparison based on rtx_cost.  */
    3321         6549 :   if (cmp_op0)
    3322              :     {
    3323         4877 :       enum tree_code rev_code;
    3324         4877 :       tree op_type = TREE_TYPE (cmp_op0);
    3325         4877 :       int unsignedp = TYPE_UNSIGNED (op_type);
    3326         4877 :       rev_code = invert_tree_comparison (cmp_code, HONOR_NANS (op_type));
    3327              : 
    3328         4877 :       if (rev_code != ERROR_MARK)
    3329              :         {
    3330         3541 :           tree cmp_type = TREE_TYPE (op0);
    3331         3541 :           machine_mode cmp_mode = TYPE_MODE (cmp_type);
    3332         3541 :           machine_mode op_mode = TYPE_MODE (op_type);
    3333         3541 :           bool speed_p = optimize_insn_for_speed_p ();
    3334         3541 :           rtx reg = gen_raw_REG (op_mode, LAST_VIRTUAL_REGISTER + 1);
    3335         3541 :           enum rtx_code cmp_rtx_code = convert_tree_comp_to_rtx (cmp_code,
    3336              :                                                                  unsignedp);
    3337         3541 :           rtx veccmp = gen_rtx_fmt_ee (cmp_rtx_code, cmp_mode, reg, reg);
    3338         3541 :           int old_cost = rtx_cost (veccmp, cmp_mode, SET, 0, speed_p);
    3339         3541 :           enum rtx_code rev_rtx_code = convert_tree_comp_to_rtx (rev_code,
    3340              :                                                                  unsignedp);
    3341         3541 :           PUT_CODE (veccmp, rev_rtx_code);
    3342         3541 :           int new_cost = rtx_cost (veccmp, cmp_mode, SET, 0, speed_p);
    3343         3541 :           if (new_cost < old_cost)
    3344              :             {
    3345          401 :               op0 = fold_build2_loc (EXPR_LOCATION (op0), rev_code,
    3346              :                                      cmp_type, cmp_op0, cmp_op1);
    3347          401 :               std::swap (op1, op2);
    3348              :             }
    3349              : 
    3350         3541 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3351              :             {
    3352            0 :               fprintf (dump_file,
    3353              :                        ";; %sswapping operands of .VCOND_MASK\n",
    3354              :                        new_cost >= old_cost ? "not " : "");
    3355            0 :               fprintf (dump_file,
    3356              :                        ";; cost of original %s: %d\n",
    3357            0 :                        GET_RTX_NAME (cmp_rtx_code), old_cost);
    3358            0 :               fprintf (dump_file,
    3359              :                        ";; cost of replacement %s: %d\n",
    3360            0 :                        GET_RTX_NAME (rev_rtx_code), new_cost);
    3361              :             }
    3362              :         }
    3363              :     }
    3364              : 
    3365         9626 :   mask = expand_normal (op0);
    3366         9626 :   rtx_op1 = expand_normal (op1);
    3367         9626 :   rtx_op2 = expand_normal (op2);
    3368              : 
    3369         9626 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3370         9626 :   create_call_lhs_operand (&ops[0], target, mode);
    3371         9626 :   create_input_operand (&ops[1], rtx_op1, mode);
    3372         9626 :   create_input_operand (&ops[2], rtx_op2, mode);
    3373         9626 :   create_input_operand (&ops[3], mask, mask_mode);
    3374         9626 :   expand_insn (icode, 4, ops);
    3375         9626 :   assign_call_lhs (lhs, target, &ops[0]);
    3376         9626 : }
    3377              : 
    3378              : /* Expand VEC_SET internal functions.  */
    3379              : 
    3380              : static void
    3381          123 : expand_vec_set_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
    3382              : {
    3383          123 :   tree lhs = gimple_call_lhs (stmt);
    3384          123 :   tree op0 = gimple_call_arg (stmt, 0);
    3385          123 :   tree op1 = gimple_call_arg (stmt, 1);
    3386          123 :   tree op2 = gimple_call_arg (stmt, 2);
    3387          123 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3388          123 :   rtx src = expand_normal (op0);
    3389              : 
    3390          123 :   machine_mode outermode = TYPE_MODE (TREE_TYPE (op0));
    3391          123 :   scalar_mode innermode = GET_MODE_INNER (outermode);
    3392              : 
    3393          123 :   rtx value = expand_normal (op1);
    3394          123 :   rtx pos = expand_normal (op2);
    3395              : 
    3396          123 :   class expand_operand ops[3];
    3397          123 :   enum insn_code icode = optab_handler (optab, outermode);
    3398              : 
    3399          123 :   if (icode != CODE_FOR_nothing)
    3400              :     {
    3401          123 :       rtx temp = gen_reg_rtx (outermode);
    3402          123 :       emit_move_insn (temp, src);
    3403              : 
    3404          123 :       create_fixed_operand (&ops[0], temp);
    3405          123 :       create_input_operand (&ops[1], value, innermode);
    3406          123 :       create_convert_operand_from (&ops[2], pos, TYPE_MODE (TREE_TYPE (op2)),
    3407              :                                    true);
    3408          123 :       if (maybe_expand_insn (icode, 3, ops))
    3409              :         {
    3410          123 :           emit_move_insn (target, temp);
    3411          123 :           return;
    3412              :         }
    3413              :     }
    3414            0 :   gcc_unreachable ();
    3415              : }
    3416              : 
    3417              : static void
    3418         1922 : expand_ABNORMAL_DISPATCHER (internal_fn, gcall *)
    3419              : {
    3420         1922 : }
    3421              : 
    3422              : static void
    3423            0 : expand_BUILTIN_EXPECT (internal_fn, gcall *stmt)
    3424              : {
    3425              :   /* When guessing was done, the hints should be already stripped away.  */
    3426            0 :   gcc_assert (!flag_guess_branch_prob || optimize == 0 || seen_error ());
    3427              : 
    3428            0 :   rtx target;
    3429            0 :   tree lhs = gimple_call_lhs (stmt);
    3430            0 :   if (lhs)
    3431            0 :     target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3432              :   else
    3433            0 :     target = const0_rtx;
    3434            0 :   rtx val = expand_expr (gimple_call_arg (stmt, 0), target, VOIDmode, EXPAND_NORMAL);
    3435            0 :   if (lhs && val != target)
    3436            0 :     emit_move_insn (target, val);
    3437            0 : }
    3438              : 
    3439              : /* IFN_VA_ARG is supposed to be expanded at pass_stdarg.  So this dummy function
    3440              :    should never be called.  */
    3441              : 
    3442              : static void
    3443            0 : expand_VA_ARG (internal_fn, gcall *)
    3444              : {
    3445            0 :   gcc_unreachable ();
    3446              : }
    3447              : 
    3448              : /* IFN_VEC_CONVERT is supposed to be expanded at pass_lower_vector.  So this
    3449              :    dummy function should never be called.  */
    3450              : 
    3451              : static void
    3452            0 : expand_VEC_CONVERT (internal_fn, gcall *)
    3453              : {
    3454            0 :   gcc_unreachable ();
    3455              : }
    3456              : 
    3457              : /* Expand IFN_RAWMEMCHR internal function.  */
    3458              : 
    3459              : void
    3460            0 : expand_RAWMEMCHR (internal_fn, gcall *stmt)
    3461              : {
    3462            0 :   expand_operand ops[3];
    3463              : 
    3464            0 :   tree lhs = gimple_call_lhs (stmt);
    3465            0 :   if (!lhs)
    3466            0 :     return;
    3467            0 :   machine_mode lhs_mode = TYPE_MODE (TREE_TYPE (lhs));
    3468            0 :   rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3469            0 :   create_call_lhs_operand (&ops[0], lhs_rtx, lhs_mode);
    3470              : 
    3471            0 :   tree mem = gimple_call_arg (stmt, 0);
    3472            0 :   rtx mem_rtx = get_memory_rtx (mem, NULL, false);
    3473            0 :   create_fixed_operand (&ops[1], mem_rtx);
    3474              : 
    3475            0 :   tree pattern = gimple_call_arg (stmt, 1);
    3476            0 :   machine_mode mode = TYPE_MODE (TREE_TYPE (pattern));
    3477            0 :   rtx pattern_rtx = expand_normal (pattern);
    3478            0 :   create_input_operand (&ops[2], pattern_rtx, mode);
    3479              : 
    3480            0 :   insn_code icode = direct_optab_handler (rawmemchr_optab, mode);
    3481              : 
    3482            0 :   expand_insn (icode, 3, ops);
    3483            0 :   assign_call_lhs (lhs, lhs_rtx, &ops[0]);
    3484              : }
    3485              : 
    3486              : /* Expand the IFN_UNIQUE function according to its first argument.  */
    3487              : 
    3488              : static void
    3489            0 : expand_UNIQUE (internal_fn, gcall *stmt)
    3490              : {
    3491            0 :   rtx pattern = NULL_RTX;
    3492            0 :   enum ifn_unique_kind kind
    3493            0 :     = (enum ifn_unique_kind) TREE_INT_CST_LOW (gimple_call_arg (stmt, 0));
    3494              : 
    3495            0 :   switch (kind)
    3496              :     {
    3497            0 :     default:
    3498            0 :       gcc_unreachable ();
    3499              : 
    3500            0 :     case IFN_UNIQUE_UNSPEC:
    3501            0 :       if (targetm.have_unique ())
    3502            0 :         pattern = targetm.gen_unique ();
    3503              :       break;
    3504              : 
    3505            0 :     case IFN_UNIQUE_OACC_FORK:
    3506            0 :     case IFN_UNIQUE_OACC_JOIN:
    3507            0 :       if (targetm.have_oacc_fork () && targetm.have_oacc_join ())
    3508              :         {
    3509            0 :           tree lhs = gimple_call_lhs (stmt);
    3510            0 :           rtx target = const0_rtx;
    3511              : 
    3512            0 :           if (lhs)
    3513            0 :             target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3514              : 
    3515            0 :           rtx data_dep = expand_normal (gimple_call_arg (stmt, 1));
    3516            0 :           rtx axis = expand_normal (gimple_call_arg (stmt, 2));
    3517              : 
    3518            0 :           if (kind == IFN_UNIQUE_OACC_FORK)
    3519            0 :             pattern = targetm.gen_oacc_fork (target, data_dep, axis);
    3520              :           else
    3521            0 :             pattern = targetm.gen_oacc_join (target, data_dep, axis);
    3522              :         }
    3523              :       else
    3524            0 :         gcc_unreachable ();
    3525              :       break;
    3526              :     }
    3527              : 
    3528            0 :   if (pattern)
    3529            0 :     emit_insn (pattern);
    3530            0 : }
    3531              : 
    3532              : /* Expand the IFN_DEFERRED_INIT function:
    3533              :    LHS = DEFERRED_INIT (SIZE of the DECL, INIT_TYPE, NAME of the DECL);
    3534              : 
    3535              :    Initialize the LHS with zero/pattern according to its second argument
    3536              :    INIT_TYPE:
    3537              :    if INIT_TYPE is AUTO_INIT_ZERO, use zeroes to initialize;
    3538              :    if INIT_TYPE is AUTO_INIT_PATTERN, use 0xFE byte-repeatable pattern
    3539              :      to initialize;
    3540              :    The LHS variable is initialized including paddings.
    3541              :    The reasons to choose 0xFE for pattern initialization are:
    3542              :      1. It is a non-canonical virtual address on x86_64, and at the
    3543              :         high end of the i386 kernel address space.
    3544              :      2. It is a very large float value (-1.694739530317379e+38).
    3545              :      3. It is also an unusual number for integers.  */
    3546              : #define INIT_PATTERN_VALUE  0xFE
    3547              : static void
    3548        40926 : expand_DEFERRED_INIT (internal_fn, gcall *stmt)
    3549              : {
    3550        40926 :   tree lhs = gimple_call_lhs (stmt);
    3551        40926 :   tree var_size = gimple_call_arg (stmt, 0);
    3552        40926 :   enum auto_init_type init_type
    3553        40926 :     = (enum auto_init_type) TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
    3554        40926 :   bool reg_lhs = true;
    3555              : 
    3556        40926 :   tree var_type = TREE_TYPE (lhs);
    3557        40926 :   gcc_assert (init_type > AUTO_INIT_UNINITIALIZED);
    3558              : 
    3559        40926 :   if (TREE_CODE (lhs) == SSA_NAME)
    3560              :     reg_lhs = true;
    3561              :   else
    3562              :     {
    3563              :       tree lhs_base = lhs;
    3564        27692 :       while (handled_component_p (lhs_base))
    3565            4 :         lhs_base = TREE_OPERAND (lhs_base, 0);
    3566        27688 :       reg_lhs = (mem_ref_refers_to_non_mem_p (lhs_base)
    3567        27688 :                  || non_mem_decl_p (lhs_base));
    3568              :       /* If this expands to a register and the underlying decl is wrapped in
    3569              :          a MEM_REF that just serves as an access type change expose the decl
    3570              :          if it is of correct size.  This avoids a situation as in PR103271
    3571              :          if the target does not support a direct move to the registers mode.  */
    3572              :       if (reg_lhs
    3573         1832 :           && TREE_CODE (lhs_base) == MEM_REF
    3574            4 :           && TREE_CODE (TREE_OPERAND (lhs_base, 0)) == ADDR_EXPR
    3575            4 :           && DECL_P (TREE_OPERAND (TREE_OPERAND (lhs_base, 0), 0))
    3576            4 :           && integer_zerop (TREE_OPERAND (lhs_base, 1))
    3577            4 :           && tree_fits_uhwi_p (var_size)
    3578            4 :           && tree_int_cst_equal
    3579            4 :                (var_size,
    3580            4 :                 DECL_SIZE_UNIT (TREE_OPERAND (TREE_OPERAND (lhs_base, 0), 0))))
    3581              :         {
    3582            4 :           lhs = TREE_OPERAND (TREE_OPERAND (lhs_base, 0), 0);
    3583            4 :           var_type = TREE_TYPE (lhs);
    3584              :         }
    3585              :     }
    3586              : 
    3587        27688 :   if (!reg_lhs)
    3588              :     {
    3589              :       /* If the variable is not in register, expand to a memset
    3590              :          to initialize it.  */
    3591        25856 :       mark_addressable (lhs);
    3592        25856 :       tree var_addr = build_fold_addr_expr (lhs);
    3593              : 
    3594        25856 :       tree value = ((init_type & ~AUTO_INIT_CXX26) == AUTO_INIT_PATTERN)
    3595        25856 :                     ? build_int_cst (integer_type_node,
    3596              :                                      INIT_PATTERN_VALUE)
    3597        25779 :                     : integer_zero_node;
    3598        51712 :       tree m_call = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMSET),
    3599              :                                      3, var_addr, value, var_size);
    3600              :       /* Expand this memset call.  */
    3601        25856 :       expand_builtin_memset (m_call, NULL_RTX, TYPE_MODE (var_type));
    3602              :     }
    3603              :   else
    3604              :     {
    3605              :       /* If this variable is in a register use expand_assignment.
    3606              :          For boolean scalars force zero-init.  */
    3607        15070 :       tree init;
    3608        15070 :       scalar_int_mode var_mode;
    3609        15070 :       if (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE
    3610        14697 :           && tree_fits_uhwi_p (var_size)
    3611        14697 :           && ((init_type & ~AUTO_INIT_CXX26) == AUTO_INIT_PATTERN
    3612        14611 :               || !is_gimple_reg_type (var_type))
    3613         1918 :           && int_mode_for_size (tree_to_uhwi (var_size) * BITS_PER_UNIT,
    3614        15075 :                                 0).exists (&var_mode)
    3615        16988 :           && have_insn_for (SET, var_mode))
    3616              :         {
    3617         1913 :           unsigned HOST_WIDE_INT total_bytes = tree_to_uhwi (var_size);
    3618         1913 :           unsigned char *buf = XALLOCAVEC (unsigned char, total_bytes);
    3619         1913 :           memset (buf, ((init_type & ~AUTO_INIT_CXX26) == AUTO_INIT_PATTERN
    3620              :                         ? INIT_PATTERN_VALUE : 0), total_bytes);
    3621         1913 :           tree itype
    3622         1913 :             = build_nonstandard_integer_type (total_bytes * BITS_PER_UNIT, 1);
    3623         1913 :           wide_int w = wi::from_buffer (buf, total_bytes);
    3624         1913 :           init = wide_int_to_tree (itype, w);
    3625              :           /* Pun the LHS to make sure its type has constant size
    3626              :              unless it is an SSA name where that's already known.  */
    3627         1913 :           if (TREE_CODE (lhs) != SSA_NAME)
    3628         1832 :             lhs = build1 (VIEW_CONVERT_EXPR, itype, lhs);
    3629              :           else
    3630           81 :             init = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), init);
    3631         1913 :         }
    3632              :       else
    3633              :         /* Use zero-init also for variable-length sizes.  */
    3634        13157 :         init = build_zero_cst (var_type);
    3635              : 
    3636        15070 :       expand_assignment (lhs, init, false);
    3637              :     }
    3638        40926 : }
    3639              : 
    3640              : /* Expand the IFN_ACCESS_WITH_SIZE function:
    3641              :    ACCESS_WITH_SIZE (REF_TO_OBJ, REF_TO_SIZE,
    3642              :                      TYPE_OF_SIZE + ACCESS_MODE, TYPE_SIZE_UNIT for element)
    3643              :    which returns the REF_TO_OBJ same as the 1st argument;
    3644              : 
    3645              :    1st argument REF_TO_OBJ: The reference to the object;
    3646              :    2nd argument REF_TO_SIZE: The reference to the size of the object,
    3647              :    3rd argument TYPE_OF_SIZE + ACCESS_MODE: An integer constant with a pointer
    3648              :      TYPE.
    3649              :      The pointee TYPE of the pointer TYPE is the TYPE of the object referenced
    3650              :         by REF_TO_SIZE.
    3651              :      The integer constant value represents the ACCESS_MODE:
    3652              :         0: none
    3653              :         1: read_only
    3654              :         2: write_only
    3655              :         3: read_write
    3656              : 
    3657              :    4th argument: The TYPE_SIZE_UNIT of the element TYPE of the array.
    3658              : 
    3659              :    Both the return type and the type of the first argument of this
    3660              :    function have been converted from the incomplete array type to
    3661              :    the corresponding pointer type.
    3662              : 
    3663              :    For each call to a .ACCESS_WITH_SIZE, replace it with its 1st argument.  */
    3664              : 
    3665              : static void
    3666          526 : expand_ACCESS_WITH_SIZE (internal_fn, gcall *stmt)
    3667              : {
    3668          526 :   tree lhs = gimple_call_lhs (stmt);
    3669          526 :   tree ref_to_obj = gimple_call_arg (stmt, 0);
    3670          526 :   if (lhs)
    3671          526 :     expand_assignment (lhs, ref_to_obj, false);
    3672          526 : }
    3673              : 
    3674              : /* The size of an OpenACC compute dimension.  */
    3675              : 
    3676              : static void
    3677         5695 : expand_GOACC_DIM_SIZE (internal_fn, gcall *stmt)
    3678              : {
    3679         5695 :   tree lhs = gimple_call_lhs (stmt);
    3680              : 
    3681         5695 :   if (!lhs)
    3682              :     return;
    3683              : 
    3684         5695 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3685         5695 :   if (targetm.have_oacc_dim_size ())
    3686              :     {
    3687            0 :       rtx dim = expand_expr (gimple_call_arg (stmt, 0), NULL_RTX,
    3688              :                              VOIDmode, EXPAND_NORMAL);
    3689            0 :       emit_insn (targetm.gen_oacc_dim_size (target, dim));
    3690              :     }
    3691              :   else
    3692         5695 :     emit_move_insn (target, GEN_INT (1));
    3693              : }
    3694              : 
    3695              : /* The position of an OpenACC execution engine along one compute axis.  */
    3696              : 
    3697              : static void
    3698         4567 : expand_GOACC_DIM_POS (internal_fn, gcall *stmt)
    3699              : {
    3700         4567 :   tree lhs = gimple_call_lhs (stmt);
    3701              : 
    3702         4567 :   if (!lhs)
    3703              :     return;
    3704              : 
    3705         4567 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3706         4567 :   if (targetm.have_oacc_dim_pos ())
    3707              :     {
    3708            0 :       rtx dim = expand_expr (gimple_call_arg (stmt, 0), NULL_RTX,
    3709              :                              VOIDmode, EXPAND_NORMAL);
    3710            0 :       emit_insn (targetm.gen_oacc_dim_pos (target, dim));
    3711              :     }
    3712              :   else
    3713         4567 :     emit_move_insn (target, const0_rtx);
    3714              : }
    3715              : 
    3716              : /* This is expanded by oacc_device_lower pass.  */
    3717              : 
    3718              : static void
    3719            0 : expand_GOACC_LOOP (internal_fn, gcall *)
    3720              : {
    3721            0 :   gcc_unreachable ();
    3722              : }
    3723              : 
    3724              : /* This is expanded by oacc_device_lower pass.  */
    3725              : 
    3726              : static void
    3727            0 : expand_GOACC_REDUCTION (internal_fn, gcall *)
    3728              : {
    3729            0 :   gcc_unreachable ();
    3730              : }
    3731              : 
    3732              : /* This is expanded by oacc_device_lower pass.  */
    3733              : 
    3734              : static void
    3735            0 : expand_GOACC_TILE (internal_fn, gcall *)
    3736              : {
    3737            0 :   gcc_unreachable ();
    3738              : }
    3739              : 
    3740              : /* Set errno to EDOM.  */
    3741              : 
    3742              : static void
    3743            0 : expand_SET_EDOM (internal_fn, gcall *)
    3744              : {
    3745              : #ifdef TARGET_EDOM
    3746              : #ifdef GEN_ERRNO_RTX
    3747              :   rtx errno_rtx = GEN_ERRNO_RTX;
    3748              : #else
    3749              :   rtx errno_rtx = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
    3750              : #endif
    3751              :   emit_move_insn (errno_rtx,
    3752              :                   gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
    3753              : #else
    3754            0 :   gcc_unreachable ();
    3755              : #endif
    3756              : }
    3757              : 
    3758              : /* Expand atomic bit test and set.  */
    3759              : 
    3760              : static void
    3761          152 : expand_ATOMIC_BIT_TEST_AND_SET (internal_fn, gcall *call)
    3762              : {
    3763          152 :   expand_ifn_atomic_bit_test_and (call);
    3764          152 : }
    3765              : 
    3766              : /* Expand atomic bit test and complement.  */
    3767              : 
    3768              : static void
    3769          146 : expand_ATOMIC_BIT_TEST_AND_COMPLEMENT (internal_fn, gcall *call)
    3770              : {
    3771          146 :   expand_ifn_atomic_bit_test_and (call);
    3772          146 : }
    3773              : 
    3774              : /* Expand atomic bit test and reset.  */
    3775              : 
    3776              : static void
    3777          135 : expand_ATOMIC_BIT_TEST_AND_RESET (internal_fn, gcall *call)
    3778              : {
    3779          135 :   expand_ifn_atomic_bit_test_and (call);
    3780          135 : }
    3781              : 
    3782              : /* Expand atomic bit test and set.  */
    3783              : 
    3784              : static void
    3785        14279 : expand_ATOMIC_COMPARE_EXCHANGE (internal_fn, gcall *call)
    3786              : {
    3787        14279 :   expand_ifn_atomic_compare_exchange (call);
    3788        14279 : }
    3789              : 
    3790              : /* Expand atomic add fetch and cmp with 0.  */
    3791              : 
    3792              : static void
    3793          938 : expand_ATOMIC_ADD_FETCH_CMP_0 (internal_fn, gcall *call)
    3794              : {
    3795          938 :   expand_ifn_atomic_op_fetch_cmp_0 (call);
    3796          938 : }
    3797              : 
    3798              : /* Expand atomic sub fetch and cmp with 0.  */
    3799              : 
    3800              : static void
    3801          745 : expand_ATOMIC_SUB_FETCH_CMP_0 (internal_fn, gcall *call)
    3802              : {
    3803          745 :   expand_ifn_atomic_op_fetch_cmp_0 (call);
    3804          745 : }
    3805              : 
    3806              : /* Expand atomic and fetch and cmp with 0.  */
    3807              : 
    3808              : static void
    3809          176 : expand_ATOMIC_AND_FETCH_CMP_0 (internal_fn, gcall *call)
    3810              : {
    3811          176 :   expand_ifn_atomic_op_fetch_cmp_0 (call);
    3812          176 : }
    3813              : 
    3814              : /* Expand atomic or fetch and cmp with 0.  */
    3815              : 
    3816              : static void
    3817          112 : expand_ATOMIC_OR_FETCH_CMP_0 (internal_fn, gcall *call)
    3818              : {
    3819          112 :   expand_ifn_atomic_op_fetch_cmp_0 (call);
    3820          112 : }
    3821              : 
    3822              : /* Expand atomic xor fetch and cmp with 0.  */
    3823              : 
    3824              : static void
    3825          176 : expand_ATOMIC_XOR_FETCH_CMP_0 (internal_fn, gcall *call)
    3826              : {
    3827          176 :   expand_ifn_atomic_op_fetch_cmp_0 (call);
    3828          176 : }
    3829              : 
    3830              : /* Expand LAUNDER to assignment, lhs = arg0.  */
    3831              : 
    3832              : static void
    3833           29 : expand_LAUNDER (internal_fn, gcall *call)
    3834              : {
    3835           29 :   tree lhs = gimple_call_lhs (call);
    3836              : 
    3837           29 :   if (!lhs)
    3838              :     return;
    3839              : 
    3840           28 :   expand_assignment (lhs, gimple_call_arg (call, 0), false);
    3841              : }
    3842              : 
    3843              : /* Expand {MASK_,}SCATTER_STORE{S,U} call CALL using optab OPTAB.  */
    3844              : 
    3845              : static void
    3846            0 : expand_scatter_store_optab_fn (internal_fn, gcall *stmt, direct_optab optab)
    3847              : {
    3848            0 :   internal_fn ifn = gimple_call_internal_fn (stmt);
    3849            0 :   int rhs_index = internal_fn_stored_value_index (ifn);
    3850            0 :   tree base = gimple_call_arg (stmt, 0);
    3851            0 :   tree offset = gimple_call_arg (stmt, internal_fn_offset_index (ifn));
    3852            0 :   tree scale = gimple_call_arg (stmt, internal_fn_scale_index (ifn));
    3853            0 :   tree rhs = gimple_call_arg (stmt, rhs_index);
    3854              : 
    3855            0 :   rtx base_rtx = expand_normal (base);
    3856            0 :   rtx offset_rtx = expand_normal (offset);
    3857            0 :   HOST_WIDE_INT scale_int = tree_to_shwi (scale);
    3858            0 :   rtx rhs_rtx = expand_normal (rhs);
    3859              : 
    3860            0 :   class expand_operand ops[8];
    3861            0 :   int i = 0;
    3862            0 :   create_address_operand (&ops[i++], base_rtx);
    3863            0 :   create_input_operand (&ops[i++], offset_rtx, TYPE_MODE (TREE_TYPE (offset)));
    3864            0 :   create_integer_operand (&ops[i++], TYPE_UNSIGNED (TREE_TYPE (offset)));
    3865            0 :   create_integer_operand (&ops[i++], scale_int);
    3866            0 :   create_input_operand (&ops[i++], rhs_rtx, TYPE_MODE (TREE_TYPE (rhs)));
    3867            0 :   i = add_mask_else_and_len_args (ops, i, stmt);
    3868              : 
    3869            0 :   insn_code icode = convert_optab_handler (optab, TYPE_MODE (TREE_TYPE (rhs)),
    3870            0 :                                            TYPE_MODE (TREE_TYPE (offset)));
    3871            0 :   expand_insn (icode, i, ops);
    3872            0 : }
    3873              : 
    3874              : #define expand_mask_scatter_store_optab_fn expand_scatter_store_optab_fn
    3875              : #define expand_mask_len_scatter_store_optab_fn expand_scatter_store_optab_fn
    3876              : 
    3877              : /* Expand {MASK_,}GATHER_LOAD call CALL using optab OPTAB.  */
    3878              : 
    3879              : static void
    3880            0 : expand_gather_load_optab_fn (internal_fn ifn, gcall *stmt, direct_optab optab)
    3881              : {
    3882            0 :   tree lhs = gimple_call_lhs (stmt);
    3883            0 :   tree base = gimple_call_arg (stmt, 0);
    3884            0 :   tree offset = gimple_call_arg (stmt, internal_fn_offset_index (ifn));
    3885            0 :   tree scale = gimple_call_arg (stmt, internal_fn_scale_index (ifn));
    3886              : 
    3887            0 :   rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3888            0 :   rtx base_rtx = expand_normal (base);
    3889            0 :   rtx offset_rtx = expand_normal (offset);
    3890            0 :   HOST_WIDE_INT scale_int = tree_to_shwi (scale);
    3891              : 
    3892            0 :   int i = 0;
    3893            0 :   class expand_operand ops[9];
    3894            0 :   create_call_lhs_operand (&ops[i++], lhs_rtx, TYPE_MODE (TREE_TYPE (lhs)));
    3895            0 :   create_address_operand (&ops[i++], base_rtx);
    3896            0 :   create_input_operand (&ops[i++], offset_rtx, TYPE_MODE (TREE_TYPE (offset)));
    3897            0 :   create_integer_operand (&ops[i++], TYPE_UNSIGNED (TREE_TYPE (offset)));
    3898            0 :   create_integer_operand (&ops[i++], scale_int);
    3899            0 :   i = add_mask_else_and_len_args (ops, i, stmt);
    3900            0 :   insn_code icode = convert_optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)),
    3901            0 :                                            TYPE_MODE (TREE_TYPE (offset)));
    3902            0 :   expand_insn (icode, i, ops);
    3903            0 :   assign_call_lhs (lhs, lhs_rtx, &ops[0]);
    3904            0 : }
    3905              : 
    3906              : #define expand_mask_gather_load_optab_fn expand_gather_load_optab_fn
    3907              : #define expand_mask_len_gather_load_optab_fn expand_gather_load_optab_fn
    3908              : 
    3909              : /* Expand MASK_LEN_STRIDED_LOAD call CALL by optab OPTAB.  */
    3910              : 
    3911              : static void
    3912            0 : expand_strided_load_optab_fn (ATTRIBUTE_UNUSED internal_fn, gcall *stmt,
    3913              :                               direct_optab optab)
    3914              : {
    3915            0 :   tree lhs = gimple_call_lhs (stmt);
    3916            0 :   tree base = gimple_call_arg (stmt, 0);
    3917            0 :   tree stride = gimple_call_arg (stmt, 1);
    3918              : 
    3919            0 :   rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    3920            0 :   rtx base_rtx = expand_normal (base);
    3921            0 :   rtx stride_rtx = expand_normal (stride);
    3922              : 
    3923            0 :   unsigned i = 0;
    3924            0 :   class expand_operand ops[7];
    3925            0 :   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
    3926              : 
    3927            0 :   create_output_operand (&ops[i++], lhs_rtx, mode);
    3928            0 :   create_address_operand (&ops[i++], base_rtx);
    3929            0 :   create_address_operand (&ops[i++], stride_rtx);
    3930              : 
    3931            0 :   i = add_mask_else_and_len_args (ops, i, stmt);
    3932            0 :   expand_insn (direct_optab_handler (optab, mode), i, ops);
    3933              : 
    3934            0 :   if (!rtx_equal_p (lhs_rtx, ops[0].value))
    3935            0 :     emit_move_insn (lhs_rtx, ops[0].value);
    3936            0 : }
    3937              : 
    3938              : /* Expand MASK_LEN_STRIDED_STORE call CALL by optab OPTAB.  */
    3939              : 
    3940              : static void
    3941            0 : expand_strided_store_optab_fn (ATTRIBUTE_UNUSED internal_fn, gcall *stmt,
    3942              :                                direct_optab optab)
    3943              : {
    3944            0 :   internal_fn fn = gimple_call_internal_fn (stmt);
    3945            0 :   int rhs_index = internal_fn_stored_value_index (fn);
    3946              : 
    3947            0 :   tree base = gimple_call_arg (stmt, 0);
    3948            0 :   tree stride = gimple_call_arg (stmt, 1);
    3949            0 :   tree rhs = gimple_call_arg (stmt, rhs_index);
    3950              : 
    3951            0 :   rtx base_rtx = expand_normal (base);
    3952            0 :   rtx stride_rtx = expand_normal (stride);
    3953            0 :   rtx rhs_rtx = expand_normal (rhs);
    3954              : 
    3955            0 :   unsigned i = 0;
    3956            0 :   class expand_operand ops[6];
    3957            0 :   machine_mode mode = TYPE_MODE (TREE_TYPE (rhs));
    3958              : 
    3959            0 :   create_address_operand (&ops[i++], base_rtx);
    3960            0 :   create_address_operand (&ops[i++], stride_rtx);
    3961            0 :   create_input_operand (&ops[i++], rhs_rtx, mode);
    3962              : 
    3963            0 :   i = add_mask_else_and_len_args (ops, i, stmt);
    3964            0 :   expand_insn (direct_optab_handler (optab, mode), i, ops);
    3965            0 : }
    3966              : 
    3967              : /* Helper for expand_DIVMOD.  Return true if the sequence starting with
    3968              :    INSN contains any call insns or insns with {,U}{DIV,MOD} rtxes.  */
    3969              : 
    3970              : static bool
    3971          838 : contains_call_div_mod (rtx_insn *insn)
    3972              : {
    3973          838 :   subrtx_iterator::array_type array;
    3974         4821 :   for (; insn; insn = NEXT_INSN (insn))
    3975         4821 :     if (CALL_P (insn))
    3976              :       return true;
    3977         3983 :     else if (INSN_P (insn))
    3978        17804 :       FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
    3979        13821 :         switch (GET_CODE (*iter))
    3980              :           {
    3981            0 :           case CALL:
    3982            0 :           case DIV:
    3983            0 :           case UDIV:
    3984            0 :           case MOD:
    3985            0 :           case UMOD:
    3986            0 :             return true;
    3987        13821 :           default:
    3988        13821 :             break;
    3989              :           }
    3990              :   return false;
    3991          838 :  }
    3992              : 
    3993              : /* Expand DIVMOD() using:
    3994              :  a) optab handler for udivmod/sdivmod if it is available.
    3995              :  b) If optab_handler doesn't exist, generate call to
    3996              :     target-specific divmod libfunc.  */
    3997              : 
    3998              : static void
    3999        12558 : expand_DIVMOD (internal_fn, gcall *call_stmt)
    4000              : {
    4001        12558 :   tree lhs = gimple_call_lhs (call_stmt);
    4002        12558 :   tree arg0 = gimple_call_arg (call_stmt, 0);
    4003        12558 :   tree arg1 = gimple_call_arg (call_stmt, 1);
    4004              : 
    4005        12558 :   gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
    4006        12558 :   tree type = TREE_TYPE (TREE_TYPE (lhs));
    4007        12558 :   machine_mode mode = TYPE_MODE (type);
    4008        12558 :   bool unsignedp = TYPE_UNSIGNED (type);
    4009        12558 :   optab tab = (unsignedp) ? udivmod_optab : sdivmod_optab;
    4010              : 
    4011        12558 :   rtx op0 = expand_normal (arg0);
    4012        12558 :   rtx op1 = expand_normal (arg1);
    4013        12558 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    4014              : 
    4015        12558 :   rtx quotient = NULL_RTX, remainder = NULL_RTX;
    4016        12558 :   rtx_insn *insns = NULL;
    4017              : 
    4018        12558 :   if (TREE_CODE (arg1) == INTEGER_CST)
    4019              :     {
    4020              :       /* For DIVMOD by integral constants, there could be efficient code
    4021              :          expanded inline e.g. using shifts and plus/minus.  Try to expand
    4022              :          the division and modulo and if it emits any library calls or any
    4023              :          {,U}{DIV,MOD} rtxes throw it away and use a divmod optab or
    4024              :          divmod libcall.  */
    4025         1178 :       scalar_int_mode int_mode;
    4026         1178 :       if (remainder == NULL_RTX
    4027         1178 :           && optimize
    4028         1178 :           && CONST_INT_P (op1)
    4029         1174 :           && !pow2p_hwi (INTVAL (op1))
    4030         2348 :           && is_int_mode (TYPE_MODE (type), &int_mode)
    4031         1335 :           && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
    4032         1174 :           && optab_handler (and_optab, word_mode) != CODE_FOR_nothing
    4033         1174 :           && optab_handler (add_optab, word_mode) != CODE_FOR_nothing
    4034         1174 :           && optimize_insn_for_speed_p ())
    4035              :         {
    4036         1174 :           rtx_insn *last = get_last_insn ();
    4037         1174 :           remainder = NULL_RTX;
    4038         3522 :           quotient = expand_doubleword_divmod (int_mode, op0, op1, &remainder,
    4039         1174 :                                                TYPE_UNSIGNED (type));
    4040         1174 :           if (quotient != NULL_RTX)
    4041              :             {
    4042          340 :               if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
    4043              :                 {
    4044          340 :                   rtx_insn *move = emit_move_insn (quotient, quotient);
    4045          680 :                   set_dst_reg_note (move, REG_EQUAL,
    4046          340 :                                     gen_rtx_fmt_ee (TYPE_UNSIGNED (type)
    4047              :                                                     ? UDIV : DIV, int_mode,
    4048              :                                                     copy_rtx (op0), op1),
    4049              :                                     quotient);
    4050          340 :                   move = emit_move_insn (remainder, remainder);
    4051          680 :                   set_dst_reg_note (move, REG_EQUAL,
    4052          340 :                                     gen_rtx_fmt_ee (TYPE_UNSIGNED (type)
    4053              :                                                     ? UMOD : MOD, int_mode,
    4054              :                                                     copy_rtx (op0), op1),
    4055              :                                     quotient);
    4056              :                 }
    4057              :             }
    4058              :           else
    4059          834 :             delete_insns_since (last);
    4060              :         }
    4061              : 
    4062         1178 :       if (remainder == NULL_RTX)
    4063              :         {
    4064          838 :           struct separate_ops ops;
    4065          838 :           ops.code = TRUNC_DIV_EXPR;
    4066          838 :           ops.type = type;
    4067          838 :           ops.op0 = make_tree (ops.type, op0);
    4068          838 :           ops.op1 = arg1;
    4069          838 :           ops.op2 = NULL_TREE;
    4070          838 :           ops.location = gimple_location (call_stmt);
    4071          838 :           start_sequence ();
    4072          838 :           quotient = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
    4073          838 :           if (contains_call_div_mod (get_insns ()))
    4074          838 :             quotient = NULL_RTX;
    4075              :           else
    4076              :             {
    4077            0 :               ops.code = TRUNC_MOD_EXPR;
    4078            0 :               remainder = expand_expr_real_2 (&ops, NULL_RTX, mode,
    4079              :                                               EXPAND_NORMAL);
    4080            0 :               if (contains_call_div_mod (get_insns ()))
    4081            0 :                 remainder = NULL_RTX;
    4082              :             }
    4083          838 :           if (remainder)
    4084            0 :             insns = get_insns ();
    4085          838 :           end_sequence ();
    4086              :         }
    4087              :     }
    4088              : 
    4089        12558 :   if (remainder)
    4090          340 :     emit_insn (insns);
    4091              : 
    4092              :   /* Check if optab_handler exists for divmod_optab for given mode.  */
    4093        12218 :   else if (optab_handler (tab, mode) != CODE_FOR_nothing)
    4094              :     {
    4095        11326 :       quotient = gen_reg_rtx (mode);
    4096        11326 :       remainder = gen_reg_rtx (mode);
    4097        11326 :       expand_twoval_binop (tab, op0, op1, quotient, remainder, unsignedp);
    4098              :     }
    4099              : 
    4100              :   /* Generate call to divmod libfunc if it exists.  */
    4101          892 :   else if (rtx libfunc = optab_libfunc (tab, mode))
    4102          892 :     targetm.expand_divmod_libfunc (libfunc, mode, op0, op1,
    4103              :                                    &quotient, &remainder);
    4104              : 
    4105              :   else
    4106            0 :     gcc_unreachable ();
    4107              : 
    4108              :   /* Wrap the return value (quotient, remainder) within COMPLEX_EXPR.  */
    4109        25116 :   expand_expr (build2 (COMPLEX_EXPR, TREE_TYPE (lhs),
    4110        12558 :                        make_tree (TREE_TYPE (arg0), quotient),
    4111        12558 :                        make_tree (TREE_TYPE (arg1), remainder)),
    4112              :                target, VOIDmode, EXPAND_NORMAL);
    4113        12558 : }
    4114              : 
    4115              : /* Expand a NOP.  */
    4116              : 
    4117              : static void
    4118            1 : expand_NOP (internal_fn, gcall *)
    4119              : {
    4120              :   /* Nothing.  But it shouldn't really prevail.  */
    4121            1 : }
    4122              : 
    4123              : /* Coroutines, all should have been processed at this stage.  */
    4124              : 
    4125              : static void
    4126            0 : expand_CO_FRAME (internal_fn, gcall *)
    4127              : {
    4128            0 :   gcc_unreachable ();
    4129              : }
    4130              : 
    4131              : static void
    4132            0 : expand_CO_YIELD (internal_fn, gcall *)
    4133              : {
    4134            0 :   gcc_unreachable ();
    4135              : }
    4136              : 
    4137              : static void
    4138            0 : expand_CO_SUSPN (internal_fn, gcall *)
    4139              : {
    4140            0 :   gcc_unreachable ();
    4141              : }
    4142              : 
    4143              : static void
    4144            0 : expand_CO_ACTOR (internal_fn, gcall *)
    4145              : {
    4146            0 :   gcc_unreachable ();
    4147              : }
    4148              : 
    4149              : /* Expand a call to FN using the operands in STMT.  FN has a single
    4150              :    output operand and NARGS input operands.  */
    4151              : 
    4152              : static void
    4153        78825 : expand_direct_optab_fn (internal_fn fn, gcall *stmt, direct_optab optab,
    4154              :                         unsigned int nargs)
    4155              : {
    4156        78825 :   gcc_checking_assert (direct_internal_fn_nargs_ok_p
    4157              :                        (fn, gimple_call_num_args (stmt)));
    4158        78825 :   tree_pair types = direct_internal_fn_types (fn, stmt);
    4159        78825 :   insn_code icode = direct_optab_handler (optab, TYPE_MODE (types.first));
    4160        78825 :   expand_fn_using_insn (stmt, icode, 1, nargs);
    4161        78825 : }
    4162              : 
    4163              : /* Expand WHILE_ULT call STMT using optab OPTAB.  */
    4164              : 
    4165              : static void
    4166            0 : expand_while_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
    4167              : {
    4168            0 :   expand_operand ops[4];
    4169            0 :   tree rhs_type[2];
    4170              : 
    4171            0 :   tree lhs = gimple_call_lhs (stmt);
    4172            0 :   tree lhs_type = TREE_TYPE (lhs);
    4173            0 :   rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    4174            0 :   create_call_lhs_operand (&ops[0], lhs_rtx, TYPE_MODE (lhs_type));
    4175              : 
    4176            0 :   for (unsigned int i = 0; i < 2; ++i)
    4177              :     {
    4178            0 :       tree rhs = gimple_call_arg (stmt, i);
    4179            0 :       rhs_type[i] = TREE_TYPE (rhs);
    4180            0 :       rtx rhs_rtx = expand_normal (rhs);
    4181            0 :       create_input_operand (&ops[i + 1], rhs_rtx, TYPE_MODE (rhs_type[i]));
    4182              :     }
    4183              : 
    4184            0 :   int opcnt;
    4185            0 :   if (!VECTOR_MODE_P (TYPE_MODE (lhs_type)))
    4186              :     {
    4187              :       /* When the mask is an integer mode the exact vector length may not
    4188              :          be clear to the backend, so we pass it in operand[3].
    4189              :          Use the vector in arg2 for the most reliable intended size.  */
    4190            0 :       tree type = TREE_TYPE (gimple_call_arg (stmt, 2));
    4191            0 :       create_integer_operand (&ops[3], TYPE_VECTOR_SUBPARTS (type));
    4192            0 :       opcnt = 4;
    4193              :     }
    4194              :   else
    4195              :     /* The mask has a vector type so the length operand is unnecessary.  */
    4196              :     opcnt = 3;
    4197              : 
    4198            0 :   insn_code icode = convert_optab_handler (optab, TYPE_MODE (rhs_type[0]),
    4199            0 :                                            TYPE_MODE (lhs_type));
    4200              : 
    4201            0 :   expand_insn (icode, opcnt, ops);
    4202            0 :   assign_call_lhs (lhs, lhs_rtx, &ops[0]);
    4203            0 : }
    4204              : 
    4205              : /* Expand a call to a convert-like optab using the operands in STMT.
    4206              :    FN has a single output operand and NARGS input operands.  */
    4207              : 
    4208              : static void
    4209          483 : expand_convert_optab_fn (internal_fn fn, gcall *stmt, convert_optab optab,
    4210              :                          unsigned int nargs)
    4211              : {
    4212          483 :   gcc_checking_assert (direct_internal_fn_nargs_ok_p
    4213              :                        (fn, gimple_call_num_args (stmt)));
    4214          483 :   tree_pair types = direct_internal_fn_types (fn, stmt);
    4215          483 :   insn_code icode = convert_optab_handler (optab, TYPE_MODE (types.first),
    4216          483 :                                           TYPE_MODE (types.second));
    4217          483 :   expand_fn_using_insn (stmt, icode, 1, nargs);
    4218          483 : }
    4219              : 
    4220              : /* Expand CRC call STMT.  */
    4221              : 
    4222              : static void
    4223          355 : expand_crc_optab_fn (internal_fn fn, gcall *stmt, convert_optab optab)
    4224              : {
    4225          355 :   tree lhs = gimple_call_lhs (stmt);
    4226          355 :   tree rhs1 = gimple_call_arg (stmt, 0); // crc
    4227          355 :   tree rhs2 = gimple_call_arg (stmt, 1); // data
    4228          355 :   tree rhs3 = gimple_call_arg (stmt, 2); // polynomial
    4229              : 
    4230          355 :   tree result_type = TREE_TYPE (lhs);
    4231          355 :   tree data_type = TREE_TYPE (rhs2);
    4232              : 
    4233          355 :   gcc_assert (TYPE_MODE (result_type) >= TYPE_MODE (data_type));
    4234              : 
    4235          355 :   rtx dest = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    4236          355 :   rtx crc = expand_normal (rhs1);
    4237          355 :   rtx data = expand_normal (rhs2);
    4238          355 :   rtx polynomial;
    4239          355 :   if (TREE_CODE (rhs3) != INTEGER_CST)
    4240              :     {
    4241            0 :       error ("third argument to %<crc%> builtins must be a constant");
    4242            0 :       polynomial = const0_rtx;
    4243              :     }
    4244              :   else
    4245          355 :     polynomial = convert_to_mode (TYPE_MODE (result_type), expand_normal (rhs3), 0);
    4246              : 
    4247              :   /* Use target specific expansion if it exists.
    4248              :      Otherwise, generate table-based CRC.  */
    4249          355 :   if (direct_internal_fn_supported_p (fn, tree_pair (data_type, result_type),
    4250              :                                       OPTIMIZE_FOR_SPEED))
    4251              :     {
    4252           43 :       class expand_operand ops[4];
    4253              : 
    4254           43 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4255              :         {
    4256            0 :           fprintf (dump_file,
    4257              :                    ";; using optab for crc_%u_polynomial_"
    4258              :                    HOST_WIDE_INT_PRINT_HEX "\n",
    4259            0 :                    GET_MODE_BITSIZE (GET_MODE (dest)).to_constant (),
    4260            0 :                    TREE_INT_CST_LOW (rhs3));
    4261              :         }
    4262              : 
    4263           43 :       create_call_lhs_operand (&ops[0], dest, TYPE_MODE (result_type));
    4264           43 :       create_input_operand (&ops[1], crc, TYPE_MODE (result_type));
    4265           43 :       create_input_operand (&ops[2], data, TYPE_MODE (data_type));
    4266           43 :       create_input_operand (&ops[3], polynomial, TYPE_MODE (result_type));
    4267           43 :       insn_code icode = convert_optab_handler (optab, TYPE_MODE (data_type),
    4268           43 :                                                TYPE_MODE (result_type));
    4269           43 :       expand_insn (icode, 4, ops);
    4270           43 :       assign_call_lhs (lhs, dest, &ops[0]);
    4271              :     }
    4272              :   else
    4273              :     {
    4274              :       /* We're bypassing all the operand conversions that are done in the
    4275              :          case when we get an icode, operands and pass that off to expand_insn.
    4276              : 
    4277              :          That path has special case handling for promoted return values which
    4278              :          we must emulate here (is the same kind of special treatment ever
    4279              :          needed for input arguments here?).
    4280              : 
    4281              :          In particular we do not want to store directly into a promoted
    4282              :          SUBREG destination, instead store into a suitably sized pseudo.  */
    4283          312 :       rtx orig_dest = dest;
    4284          312 :       if (SUBREG_P (dest) && SUBREG_PROMOTED_VAR_P (dest))
    4285            0 :         dest = gen_reg_rtx (GET_MODE (dest));
    4286              : 
    4287              :       /* If it's IFN_CRC generate bit-forward CRC.  */
    4288          312 :       if (fn == IFN_CRC)
    4289          302 :         expand_crc_table_based (dest, crc, data, polynomial,
    4290          151 :                                 TYPE_PRECISION (data_type));
    4291              :       else
    4292              :         /* If it's IFN_CRC_REV generate bit-reversed CRC.  */
    4293          322 :         expand_reversed_crc_table_based (dest, crc, data, polynomial,
    4294          161 :                                          TYPE_PRECISION (data_type));
    4295              : 
    4296              :       /* Now get the return value where it needs to be, taking care to
    4297              :          ensure it's promoted appropriately if the ABI demands it.
    4298              : 
    4299              :          Re-use assign_call_lhs to handle the details.  */
    4300          312 :       class expand_operand ops[4];
    4301          312 :       create_call_lhs_operand (&ops[0], dest, TYPE_MODE (result_type));
    4302          312 :       ops[0].value = dest;
    4303          312 :       assign_call_lhs (lhs, orig_dest, &ops[0]);
    4304              :     }
    4305          355 : }
    4306              : 
    4307              : /* Expand .REDUC_SBOOL_{AND,IOR,XOR}.  */
    4308              : 
    4309              : static void
    4310           49 : expand_reduc_sbool_optab_fn (internal_fn fn, gcall *stmt, direct_optab optab)
    4311              : {
    4312           49 :   tree_pair types = direct_internal_fn_types (fn, stmt);
    4313           49 :   insn_code icode = direct_optab_handler (optab, TYPE_MODE (types.first));
    4314              : 
    4315              :   /* Below copied from expand_fn_using_insn.  */
    4316              : 
    4317           49 :   gcc_assert (icode != CODE_FOR_nothing);
    4318              : 
    4319           49 :   expand_operand *ops = XALLOCAVEC (expand_operand, 3);
    4320           49 :   rtx lhs_rtx = NULL_RTX;
    4321           49 :   tree lhs = gimple_call_lhs (stmt);
    4322           49 :   if (lhs)
    4323           49 :     lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    4324           49 :   create_call_lhs_operand (&ops[0], lhs_rtx,
    4325           49 :                            insn_data[icode].operand[0].mode);
    4326              : 
    4327           49 :   tree rhs = gimple_call_arg (stmt, 0);
    4328           49 :   tree rhs_type = TREE_TYPE (rhs);
    4329           49 :   rtx rhs_rtx = expand_normal (rhs);
    4330           49 :   gcc_assert (VECTOR_BOOLEAN_TYPE_P (rhs_type));
    4331           49 :   create_input_operand (&ops[1], rhs_rtx, TYPE_MODE (rhs_type));
    4332           49 :   if (SCALAR_INT_MODE_P (TYPE_MODE (rhs_type)))
    4333              :     {
    4334           24 :       rtx nunits = GEN_INT (TYPE_VECTOR_SUBPARTS (rhs_type).to_constant ());
    4335           24 :       gcc_assert (insn_operand_matches (icode, 2, nunits));
    4336           24 :       create_input_operand (&ops[2], nunits, SImode);
    4337              :     }
    4338           73 :   expand_insn (icode, SCALAR_INT_MODE_P (TYPE_MODE (rhs_type)) ? 3 : 2, ops);
    4339           49 :   if (lhs_rtx)
    4340           49 :     assign_call_lhs (lhs, lhs_rtx, &ops[0]);
    4341           49 : }
    4342              : 
    4343              : /* Expanders for optabs that can use expand_direct_optab_fn.  */
    4344              : 
    4345              : #define expand_unary_optab_fn(FN, STMT, OPTAB) \
    4346              :   expand_direct_optab_fn (FN, STMT, OPTAB, 1)
    4347              : 
    4348              : #define expand_unary_opt_optab_fn expand_unary_optab_fn
    4349              : 
    4350              : #define expand_binary_optab_fn(FN, STMT, OPTAB) \
    4351              :   expand_direct_optab_fn (FN, STMT, OPTAB, 2)
    4352              : 
    4353              : #define expand_ternary_optab_fn(FN, STMT, OPTAB) \
    4354              :   expand_direct_optab_fn (FN, STMT, OPTAB, 3)
    4355              : 
    4356              : #define expand_cond_unary_optab_fn(FN, STMT, OPTAB) \
    4357              :   expand_direct_optab_fn (FN, STMT, OPTAB, 3)
    4358              : 
    4359              : #define expand_cond_binary_optab_fn(FN, STMT, OPTAB) \
    4360              :   expand_direct_optab_fn (FN, STMT, OPTAB, 4)
    4361              : 
    4362              : #define expand_cond_ternary_optab_fn(FN, STMT, OPTAB) \
    4363              :   expand_direct_optab_fn (FN, STMT, OPTAB, 5)
    4364              : 
    4365              : #define expand_cond_len_unary_optab_fn(FN, STMT, OPTAB) \
    4366              :   expand_direct_optab_fn (FN, STMT, OPTAB, 5)
    4367              : 
    4368              : #define expand_cond_len_binary_optab_fn(FN, STMT, OPTAB) \
    4369              :   expand_direct_optab_fn (FN, STMT, OPTAB, 6)
    4370              : 
    4371              : #define expand_cond_len_ternary_optab_fn(FN, STMT, OPTAB) \
    4372              :   expand_direct_optab_fn (FN, STMT, OPTAB, 7)
    4373              : 
    4374              : #define expand_fold_extract_optab_fn(FN, STMT, OPTAB) \
    4375              :   expand_direct_optab_fn (FN, STMT, OPTAB, 3)
    4376              : 
    4377              : #define expand_fold_len_extract_optab_fn(FN, STMT, OPTAB) \
    4378              :   expand_direct_optab_fn (FN, STMT, OPTAB, 5)
    4379              : 
    4380              : #define expand_fold_left_optab_fn(FN, STMT, OPTAB) \
    4381              :   expand_direct_optab_fn (FN, STMT, OPTAB, 2)
    4382              : 
    4383              : #define expand_mask_fold_left_optab_fn(FN, STMT, OPTAB) \
    4384              :   expand_direct_optab_fn (FN, STMT, OPTAB, 3)
    4385              : 
    4386              : #define expand_mask_len_fold_left_optab_fn(FN, STMT, OPTAB) \
    4387              :   expand_direct_optab_fn (FN, STMT, OPTAB, 5)
    4388              : 
    4389              : #define expand_check_ptrs_optab_fn(FN, STMT, OPTAB) \
    4390              :   expand_direct_optab_fn (FN, STMT, OPTAB, 4)
    4391              : 
    4392              : #define expand_select_vl_optab_fn(FN, STMT, OPTAB) \
    4393              :   expand_convert_optab_fn (FN, STMT, OPTAB, 3)
    4394              : 
    4395              : /* Expanders for optabs that can use expand_convert_optab_fn.  */
    4396              : 
    4397              : #define expand_unary_convert_optab_fn(FN, STMT, OPTAB) \
    4398              :   expand_convert_optab_fn (FN, STMT, OPTAB, 1)
    4399              : 
    4400              : #define expand_vec_extract_optab_fn(FN, STMT, OPTAB) \
    4401              :   expand_convert_optab_fn (FN, STMT, OPTAB, 2)
    4402              : 
    4403              : /* RETURN_TYPE and ARGS are a return type and argument list that are
    4404              :    in principle compatible with FN (which satisfies direct_internal_fn_p).
    4405              :    Return the types that should be used to determine whether the
    4406              :    target supports FN.  */
    4407              : 
    4408              : tree_pair
    4409         7405 : direct_internal_fn_types (internal_fn fn, tree return_type, tree *args)
    4410              : {
    4411         7405 :   const direct_internal_fn_info &info = direct_internal_fn (fn);
    4412         7405 :   tree type0 = (info.type0 < 0 ? return_type : TREE_TYPE (args[info.type0]));
    4413         7405 :   tree type1 = (info.type1 < 0 ? return_type : TREE_TYPE (args[info.type1]));
    4414         7405 :   return tree_pair (type0, type1);
    4415              : }
    4416              : 
    4417              : /* CALL is a call whose return type and arguments are in principle
    4418              :    compatible with FN (which satisfies direct_internal_fn_p).  Return the
    4419              :    types that should be used to determine whether the target supports FN.  */
    4420              : 
    4421              : tree_pair
    4422       142569 : direct_internal_fn_types (internal_fn fn, gcall *call)
    4423              : {
    4424       142569 :   const direct_internal_fn_info &info = direct_internal_fn (fn);
    4425       142569 :   tree op0 = (info.type0 < 0
    4426       142569 :               ? gimple_call_lhs (call)
    4427       141052 :               : gimple_call_arg (call, info.type0));
    4428       142569 :   tree op1 = (info.type1 < 0
    4429       142569 :               ? gimple_call_lhs (call)
    4430       142516 :               : gimple_call_arg (call, info.type1));
    4431       142569 :   return tree_pair (TREE_TYPE (op0), TREE_TYPE (op1));
    4432              : }
    4433              : 
    4434              : /* Return true if OPTAB is supported for TYPES (whose modes should be
    4435              :    the same) when the optimization type is OPT_TYPE.  Used for simple
    4436              :    direct optabs.  */
    4437              : 
    4438              : static bool
    4439      4172224 : direct_optab_supported_p (direct_optab optab, tree_pair types,
    4440              :                           optimization_type opt_type)
    4441              : {
    4442      4172224 :   machine_mode mode = TYPE_MODE (types.first);
    4443      4172224 :   gcc_checking_assert (mode == TYPE_MODE (types.second));
    4444      4172224 :   return direct_optab_handler (optab, mode, opt_type) != CODE_FOR_nothing;
    4445              : }
    4446              : 
    4447              : /* Return true if OPTAB is supported for TYPES, where the first type
    4448              :    is the destination and the second type is the source.  Used for
    4449              :    convert optabs.  */
    4450              : 
    4451              : static bool
    4452       116135 : convert_optab_supported_p (convert_optab optab, tree_pair types,
    4453              :                            optimization_type opt_type)
    4454              : {
    4455       116135 :   return (convert_optab_handler (optab, TYPE_MODE (types.first),
    4456       116135 :                                  TYPE_MODE (types.second), opt_type)
    4457       116135 :           != CODE_FOR_nothing);
    4458              : }
    4459              : 
    4460              : /* Return true if load/store lanes optab OPTAB is supported for
    4461              :    array type TYPES.first when the optimization type is OPT_TYPE.  */
    4462              : 
    4463              : static bool
    4464            0 : multi_vector_optab_supported_p (convert_optab optab, tree_pair types,
    4465              :                                 optimization_type opt_type)
    4466              : {
    4467            0 :   gcc_assert (TREE_CODE (types.first) == ARRAY_TYPE);
    4468            0 :   machine_mode imode = TYPE_MODE (types.first);
    4469            0 :   machine_mode vmode = TYPE_MODE (TREE_TYPE (types.first));
    4470            0 :   return (convert_optab_handler (optab, imode, vmode, opt_type)
    4471            0 :           != CODE_FOR_nothing);
    4472              : }
    4473              : 
    4474              : #define direct_unary_optab_supported_p direct_optab_supported_p
    4475              : #define direct_unary_opt_optab_supported_p direct_optab_supported_p
    4476              : #define direct_unary_convert_optab_supported_p convert_optab_supported_p
    4477              : #define direct_binary_optab_supported_p direct_optab_supported_p
    4478              : #define direct_ternary_optab_supported_p direct_optab_supported_p
    4479              : #define direct_cond_unary_optab_supported_p direct_optab_supported_p
    4480              : #define direct_cond_binary_optab_supported_p direct_optab_supported_p
    4481              : #define direct_cond_ternary_optab_supported_p direct_optab_supported_p
    4482              : #define direct_cond_len_unary_optab_supported_p direct_optab_supported_p
    4483              : #define direct_cond_len_binary_optab_supported_p direct_optab_supported_p
    4484              : #define direct_cond_len_ternary_optab_supported_p direct_optab_supported_p
    4485              : #define direct_crc_optab_supported_p convert_optab_supported_p
    4486              : #define direct_mask_load_optab_supported_p convert_optab_supported_p
    4487              : #define direct_load_lanes_optab_supported_p multi_vector_optab_supported_p
    4488              : #define direct_mask_load_lanes_optab_supported_p multi_vector_optab_supported_p
    4489              : #define direct_mask_len_load_lanes_optab_supported_p \
    4490              :   multi_vector_optab_supported_p
    4491              : #define direct_gather_load_optab_supported_p convert_optab_supported_p
    4492              : #define direct_mask_gather_load_optab_supported_p convert_optab_supported_p
    4493              : #define direct_mask_len_gather_load_optab_supported_p convert_optab_supported_p
    4494              : #define direct_strided_load_optab_supported_p direct_optab_supported_p
    4495              : #define direct_len_load_optab_supported_p direct_optab_supported_p
    4496              : #define direct_mask_len_load_optab_supported_p convert_optab_supported_p
    4497              : #define direct_mask_store_optab_supported_p convert_optab_supported_p
    4498              : #define direct_store_lanes_optab_supported_p multi_vector_optab_supported_p
    4499              : #define direct_mask_store_lanes_optab_supported_p multi_vector_optab_supported_p
    4500              : #define direct_mask_len_store_lanes_optab_supported_p \
    4501              :   multi_vector_optab_supported_p
    4502              : #define direct_vec_cond_mask_optab_supported_p convert_optab_supported_p
    4503              : #define direct_vec_cond_optab_supported_p convert_optab_supported_p
    4504              : #define direct_scatter_store_optab_supported_p convert_optab_supported_p
    4505              : #define direct_mask_scatter_store_optab_supported_p convert_optab_supported_p
    4506              : #define direct_mask_len_scatter_store_optab_supported_p \
    4507              :   convert_optab_supported_p
    4508              : #define direct_strided_store_optab_supported_p direct_optab_supported_p
    4509              : #define direct_len_store_optab_supported_p direct_optab_supported_p
    4510              : #define direct_mask_len_store_optab_supported_p convert_optab_supported_p
    4511              : #define direct_while_optab_supported_p convert_optab_supported_p
    4512              : #define direct_fold_extract_optab_supported_p direct_optab_supported_p
    4513              : #define direct_fold_len_extract_optab_supported_p direct_optab_supported_p
    4514              : #define direct_fold_left_optab_supported_p direct_optab_supported_p
    4515              : #define direct_mask_fold_left_optab_supported_p direct_optab_supported_p
    4516              : #define direct_mask_len_fold_left_optab_supported_p direct_optab_supported_p
    4517              : #define direct_check_ptrs_optab_supported_p direct_optab_supported_p
    4518              : #define direct_vec_set_optab_supported_p direct_optab_supported_p
    4519              : #define direct_vec_extract_optab_supported_p convert_optab_supported_p
    4520              : #define direct_reduc_sbool_optab_supported_p direct_optab_supported_p
    4521              : #define direct_select_vl_optab_supported_p convert_optab_supported_p
    4522              : 
    4523              : /* Return the optab used by internal function FN.  */
    4524              : 
    4525              : optab
    4526       288840 : direct_internal_fn_optab (internal_fn fn, tree_pair types)
    4527              : {
    4528       288840 :   switch (fn)
    4529              :     {
    4530              : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
    4531              :     case IFN_##CODE: break;
    4532              : #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) \
    4533              :     case IFN_##CODE: return OPTAB##_optab;
    4534              : #define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
    4535              :                                      UNSIGNED_OPTAB, TYPE)              \
    4536              :     case IFN_##CODE: return (TYPE_UNSIGNED (types.SELECTOR)             \
    4537              :                              ? UNSIGNED_OPTAB ## _optab                 \
    4538              :                              : SIGNED_OPTAB ## _optab);
    4539              : #include "internal-fn.def"
    4540              : 
    4541              :     case IFN_LAST:
    4542              :       break;
    4543              :     }
    4544            0 :   gcc_unreachable ();
    4545              : }
    4546              : 
    4547              : /* Return the optab used by internal function FN.  */
    4548              : 
    4549              : static optab
    4550       762447 : direct_internal_fn_optab (internal_fn fn)
    4551              : {
    4552       762447 :   switch (fn)
    4553              :     {
    4554              : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
    4555              :     case IFN_##CODE: break;
    4556              : #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) \
    4557              :     case IFN_##CODE: return OPTAB##_optab;
    4558              : #include "internal-fn.def"
    4559              : 
    4560              :     case IFN_LAST:
    4561              :       break;
    4562              :     }
    4563            0 :   gcc_unreachable ();
    4564              : }
    4565              : 
    4566              : /* Return true if TYPE's mode has the same format as TYPE, and if there is
    4567              :    a 1:1 correspondence between the values that the mode can store and the
    4568              :    values that the type can store.  */
    4569              : 
    4570              : static bool
    4571      8729761 : type_strictly_matches_mode_p (const_tree type)
    4572              : {
    4573              :   /* The masked vector operations have both vector data operands and vector
    4574              :      boolean operands.  The vector data operands are expected to have a vector
    4575              :      mode,  but the vector boolean operands can be an integer mode rather than
    4576              :      a vector mode,  depending on how TARGET_VECTORIZE_GET_MASK_MODE is
    4577              :      defined.  PR116103.  */
    4578      1024404 :   if (VECTOR_BOOLEAN_TYPE_P (type)
    4579       265586 :       && SCALAR_INT_MODE_P (TYPE_MODE (type))
    4580      8823007 :       && TYPE_PRECISION (TREE_TYPE (type)) == 1)
    4581              :     return true;
    4582              : 
    4583      8636516 :   if (VECTOR_TYPE_P (type))
    4584       931159 :     return VECTOR_MODE_P (TYPE_MODE (type));
    4585              : 
    4586      7705357 :   if (INTEGRAL_TYPE_P (type))
    4587      7293705 :     return type_has_mode_precision_p (type);
    4588              : 
    4589       411652 :   if (SCALAR_FLOAT_TYPE_P (type) || COMPLEX_FLOAT_TYPE_P (type))
    4590              :     return true;
    4591              : 
    4592              :   return false;
    4593              : }
    4594              : 
    4595              : /* Returns true if both types of TYPE_PAIR strictly match their modes,
    4596              :    else returns false.  */
    4597              : 
    4598              : static bool
    4599      4441402 : type_pair_strictly_matches_mode_p (tree_pair type_pair)
    4600              : {
    4601      4441402 :   return type_strictly_matches_mode_p (type_pair.first)
    4602      4441402 :     && type_strictly_matches_mode_p (type_pair.second);
    4603              : }
    4604              : 
    4605              : /* Return true if FN is supported for the types in TYPES when the
    4606              :    optimization type is OPT_TYPE.  The types are those associated with
    4607              :    the "type0" and "type1" fields of FN's direct_internal_fn_info
    4608              :    structure.  */
    4609              : 
    4610              : bool
    4611      4441402 : direct_internal_fn_supported_p (internal_fn fn, tree_pair types,
    4612              :                                 optimization_type opt_type)
    4613              : {
    4614      4441402 :   if (!type_pair_strictly_matches_mode_p (types))
    4615              :     return false;
    4616              : 
    4617      4288359 :   switch (fn)
    4618              :     {
    4619              : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
    4620              :     case IFN_##CODE: break;
    4621              : #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) \
    4622              :     case IFN_##CODE: \
    4623              :       return direct_##TYPE##_optab_supported_p (OPTAB##_optab, types, \
    4624              :                                                 opt_type);
    4625              : #define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
    4626              :                                      UNSIGNED_OPTAB, TYPE)              \
    4627              :     case IFN_##CODE:                                                    \
    4628              :       {                                                                 \
    4629              :         optab which_optab = (TYPE_UNSIGNED (types.SELECTOR)             \
    4630              :                              ? UNSIGNED_OPTAB ## _optab                 \
    4631              :                              : SIGNED_OPTAB ## _optab);                 \
    4632              :         return direct_##TYPE##_optab_supported_p (which_optab, types,   \
    4633              :                                                   opt_type);            \
    4634              :       }
    4635              : #include "internal-fn.def"
    4636              : 
    4637              :     case IFN_LAST:
    4638              :       break;
    4639              :     }
    4640            0 :   gcc_unreachable ();
    4641              : }
    4642              : 
    4643              : /* Return true if FN is supported for type TYPE when the optimization
    4644              :    type is OPT_TYPE.  The caller knows that the "type0" and "type1"
    4645              :    fields of FN's direct_internal_fn_info structure are the same.  */
    4646              : 
    4647              : bool
    4648      4242077 : direct_internal_fn_supported_p (internal_fn fn, tree type,
    4649              :                                 optimization_type opt_type)
    4650              : {
    4651      4242077 :   const direct_internal_fn_info &info = direct_internal_fn (fn);
    4652      4242077 :   gcc_checking_assert (info.type0 == info.type1);
    4653      4242077 :   return direct_internal_fn_supported_p (fn, tree_pair (type, type), opt_type);
    4654              : }
    4655              : 
    4656              : /* Return true if the STMT is supported when the optimization type is OPT_TYPE,
    4657              :    given that STMT is a call to a direct internal function.  */
    4658              : 
    4659              : bool
    4660          416 : direct_internal_fn_supported_p (gcall *stmt, optimization_type opt_type)
    4661              : {
    4662          416 :   internal_fn fn = gimple_call_internal_fn (stmt);
    4663          416 :   tree_pair types = direct_internal_fn_types (fn, stmt);
    4664          416 :   return direct_internal_fn_supported_p (fn, types, opt_type);
    4665              : }
    4666              : 
    4667              : /* Return true if FN is a binary operation and if FN is commutative.  */
    4668              : 
    4669              : bool
    4670     34618290 : commutative_binary_fn_p (internal_fn fn)
    4671              : {
    4672     34618290 :   switch (fn)
    4673              :     {
    4674              :     case IFN_AVG_FLOOR:
    4675              :     case IFN_AVG_CEIL:
    4676              :     case IFN_MULH:
    4677              :     case IFN_MULHS:
    4678              :     case IFN_MULHRS:
    4679              :     case IFN_FMIN:
    4680              :     case IFN_FMAX:
    4681              :     case IFN_COMPLEX_MUL:
    4682              :     case IFN_UBSAN_CHECK_ADD:
    4683              :     case IFN_UBSAN_CHECK_MUL:
    4684              :     case IFN_ADD_OVERFLOW:
    4685              :     case IFN_MUL_OVERFLOW:
    4686              :     case IFN_CLMUL:
    4687              :     case IFN_CLMUL_WIDEN:
    4688              :     case IFN_SAT_ADD:
    4689              :     case IFN_SAT_MUL:
    4690              :     case IFN_VEC_WIDEN_PLUS:
    4691              :     case IFN_VEC_WIDEN_PLUS_LO:
    4692              :     case IFN_VEC_WIDEN_PLUS_HI:
    4693              :     case IFN_VEC_WIDEN_PLUS_EVEN:
    4694              :     case IFN_VEC_WIDEN_PLUS_ODD:
    4695              :       return true;
    4696              : 
    4697     33699585 :     default:
    4698     33699585 :       return false;
    4699              :     }
    4700              : }
    4701              : 
    4702              : /* Return true if FN is a ternary operation and if its first two arguments
    4703              :    are commutative.  */
    4704              : 
    4705              : bool
    4706     28211567 : commutative_ternary_fn_p (internal_fn fn)
    4707              : {
    4708     28211567 :   switch (fn)
    4709              :     {
    4710              :     case IFN_FMA:
    4711              :     case IFN_FMS:
    4712              :     case IFN_FNMA:
    4713              :     case IFN_FNMS:
    4714              :     case IFN_UADDC:
    4715              :       return true;
    4716              : 
    4717     28064276 :     default:
    4718     28064276 :       return false;
    4719              :     }
    4720              : }
    4721              : 
    4722              : /* Return true if FN is an associative binary operation.  */
    4723              : 
    4724              : bool
    4725           52 : associative_binary_fn_p (internal_fn fn)
    4726              : {
    4727           52 :   switch (fn)
    4728              :     {
    4729              :     case IFN_FMIN:
    4730              :     case IFN_FMAX:
    4731              :       return true;
    4732              : 
    4733            0 :     default:
    4734            0 :       return false;
    4735              :     }
    4736              : }
    4737              : 
    4738              : /* If FN is commutative in two consecutive arguments, return the
    4739              :    index of the first, otherwise return -1.  */
    4740              : 
    4741              : int
    4742     28549464 : first_commutative_argument (internal_fn fn)
    4743              : {
    4744     28549464 :   switch (fn)
    4745              :     {
    4746              :     case IFN_COND_ADD:
    4747              :     case IFN_COND_MUL:
    4748              :     case IFN_COND_MIN:
    4749              :     case IFN_COND_MAX:
    4750              :     case IFN_COND_FMIN:
    4751              :     case IFN_COND_FMAX:
    4752              :     case IFN_COND_AND:
    4753              :     case IFN_COND_IOR:
    4754              :     case IFN_COND_XOR:
    4755              :     case IFN_COND_FMA:
    4756              :     case IFN_COND_FMS:
    4757              :     case IFN_COND_FNMA:
    4758              :     case IFN_COND_FNMS:
    4759              :     case IFN_COND_LEN_ADD:
    4760              :     case IFN_COND_LEN_MUL:
    4761              :     case IFN_COND_LEN_MIN:
    4762              :     case IFN_COND_LEN_MAX:
    4763              :     case IFN_COND_LEN_FMIN:
    4764              :     case IFN_COND_LEN_FMAX:
    4765              :     case IFN_COND_LEN_AND:
    4766              :     case IFN_COND_LEN_IOR:
    4767              :     case IFN_COND_LEN_XOR:
    4768              :     case IFN_COND_LEN_FMA:
    4769              :     case IFN_COND_LEN_FMS:
    4770              :     case IFN_COND_LEN_FNMA:
    4771              :     case IFN_COND_LEN_FNMS:
    4772              :       return 1;
    4773              : 
    4774     28524938 :     default:
    4775     28524938 :       if (commutative_binary_fn_p (fn)
    4776     28524938 :           || commutative_ternary_fn_p (fn))
    4777       465898 :         return 0;
    4778              :       return -1;
    4779              :     }
    4780              : }
    4781              : 
    4782              : /* Return true if this CODE describes an internal_fn that returns a vector with
    4783              :    elements twice as wide as the element size of the input vectors.  */
    4784              : 
    4785              : bool
    4786      2143933 : widening_fn_p (code_helper code)
    4787              : {
    4788      2143933 :   if (!code.is_fn_code ())
    4789              :     return false;
    4790              : 
    4791       223483 :   if (!internal_fn_p ((combined_fn) code))
    4792              :     return false;
    4793              : 
    4794       223483 :   internal_fn fn = as_internal_fn ((combined_fn) code);
    4795       223483 :   switch (fn)
    4796              :     {
    4797              :     #define DEF_INTERNAL_WIDENING_OPTAB_FN(NAME, F, S, SO, UO, T) \
    4798              :     case IFN_##NAME:                                              \
    4799              :     case IFN_##NAME##_HI:                                         \
    4800              :     case IFN_##NAME##_LO:                                         \
    4801              :     case IFN_##NAME##_EVEN:                                       \
    4802              :     case IFN_##NAME##_ODD:                                        \
    4803              :       return true;
    4804              :     #include "internal-fn.def"
    4805              : 
    4806              :     default:
    4807              :       return false;
    4808              :     }
    4809              : }
    4810              : 
    4811              : /* Return true if this CODE describes an internal_fn that returns a vector with
    4812              :    elements twice as wide as the element size of the input vectors and operates
    4813              :    on even/odd parts of the input.  */
    4814              : 
    4815              : bool
    4816        21416 : widening_evenodd_fn_p (code_helper code)
    4817              : {
    4818        21416 :   if (!code.is_fn_code ())
    4819              :     return false;
    4820              : 
    4821            0 :   if (!internal_fn_p ((combined_fn) code))
    4822              :     return false;
    4823              : 
    4824            0 :   internal_fn fn = as_internal_fn ((combined_fn) code);
    4825            0 :   switch (fn)
    4826              :     {
    4827              :     #define DEF_INTERNAL_WIDENING_OPTAB_FN(NAME, F, S, SO, UO, T) \
    4828              :     case IFN_##NAME##_EVEN:                                       \
    4829              :     case IFN_##NAME##_ODD:                                        \
    4830              :       return true;
    4831              :     #include "internal-fn.def"
    4832              : 
    4833              :     default:
    4834              :       return false;
    4835              :     }
    4836              : }
    4837              : 
    4838              : /* Check the NARGS arguments ARGS of a call to internal function FN.
    4839              :    Return true if they are valid.  Otherwise report an error at LOC
    4840              :    and return false.  */
    4841              : 
    4842              : bool
    4843     42779055 : check_internal_fn_args (location_t loc, internal_fn fn, unsigned int nargs,
    4844              :                         const tree *args)
    4845              : {
    4846     42779055 :   const char *name = internal_fn_name (fn);
    4847     42779055 :   if (direct_internal_fn_p (fn))
    4848              :     {
    4849      1649811 :       const direct_internal_fn_info &info = direct_internal_fn (fn);
    4850      1649811 :       if (nargs < info.nargs_min)
    4851              :         {
    4852            2 :           error_at (loc, "%<.%s%> call with too few arguments", name);
    4853            2 :           return false;
    4854              :         }
    4855      1649809 :       if (nargs > info.nargs_max)
    4856              :         {
    4857            2 :           error_at (loc, "%<.%s%> call with too many arguments", name);
    4858            2 :           return false;
    4859              :         }
    4860              :     }
    4861              : 
    4862     42779051 :   switch (fn)
    4863              :     {
    4864        48798 :     case IFN_CLZ:
    4865        48798 :     case IFN_CTZ:
    4866        48798 :       {
    4867        48798 :         tree type = TREE_TYPE (args[0]);
    4868         3586 :         if (!(INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
    4869        52384 :             || !TYPE_UNSIGNED (type))
    4870              :           {
    4871            3 :             error_at (loc, "%<.%s%> call with an argument that is not "
    4872              :                       "unsigned integer", name);
    4873            3 :             return false;
    4874              :           }
    4875        48795 :         if (nargs == 2 && TREE_CODE (args[1]) != INTEGER_CST)
    4876              :           {
    4877            1 :             error_at (loc, "%<.%s%> call with a value at zero that is not "
    4878              :                       "constant", name);
    4879            1 :             return false;
    4880              :           }
    4881              :         break;
    4882              :       }
    4883              : 
    4884              :     default:
    4885              :       break;
    4886              :     }
    4887              :   return true;
    4888              : }
    4889              : 
    4890              : /* Return true if IFN_SET_EDOM is supported.  */
    4891              : 
    4892              : bool
    4893          140 : set_edom_supported_p (void)
    4894              : {
    4895              : #ifdef TARGET_EDOM
    4896              :   return true;
    4897              : #else
    4898          140 :   return false;
    4899              : #endif
    4900              : }
    4901              : 
    4902              : #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) \
    4903              :   static void                                           \
    4904              :   expand_##CODE (internal_fn fn, gcall *stmt)           \
    4905              :   {                                                     \
    4906              :     expand_##TYPE##_optab_fn (fn, stmt, OPTAB##_optab); \
    4907              :   }
    4908              : #define DEF_INTERNAL_INT_EXT_FN(CODE, FLAGS, OPTAB, TYPE)
    4909              : #define DEF_INTERNAL_INTSZ_EXT_FN(CODE, FLAGS, OPTAB, TYPE)
    4910              : #define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
    4911              :                                      UNSIGNED_OPTAB, TYPE)              \
    4912              :   static void                                                           \
    4913              :   expand_##CODE (internal_fn fn, gcall *stmt)                           \
    4914              :   {                                                                     \
    4915              :     tree_pair types = direct_internal_fn_types (fn, stmt);              \
    4916              :     optab which_optab = direct_internal_fn_optab (fn, types);           \
    4917              :     expand_##TYPE##_optab_fn (fn, stmt, which_optab);                   \
    4918              :   }
    4919              : #include "internal-fn.def"
    4920              : 
    4921              : /* Routines to expand each internal function, indexed by function number.
    4922              :    Each routine has the prototype:
    4923              : 
    4924              :        expand_<NAME> (gcall *stmt)
    4925              : 
    4926              :    where STMT is the statement that performs the call. */
    4927              : static void (*const internal_fn_expanders[]) (internal_fn, gcall *) = {
    4928              : 
    4929              : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) expand_##CODE,
    4930              : #include "internal-fn.def"
    4931              :   0
    4932              : };
    4933              : 
    4934              : /* Invoke T(CODE, SUFFIX) for each conditional function IFN_COND_##SUFFIX
    4935              :    that maps to a tree code CODE.  There is also an IFN_COND_LEN_##SUFFIX
    4936              :    for each such IFN_COND_##SUFFIX.  */
    4937              : #define FOR_EACH_CODE_MAPPING(T) \
    4938              :   T (PLUS_EXPR, ADD) \
    4939              :   T (MINUS_EXPR, SUB) \
    4940              :   T (MULT_EXPR, MUL) \
    4941              :   T (TRUNC_DIV_EXPR, DIV) \
    4942              :   T (TRUNC_MOD_EXPR, MOD) \
    4943              :   T (RDIV_EXPR, RDIV) \
    4944              :   T (MIN_EXPR, MIN) \
    4945              :   T (MAX_EXPR, MAX) \
    4946              :   T (BIT_AND_EXPR, AND) \
    4947              :   T (BIT_IOR_EXPR, IOR) \
    4948              :   T (BIT_XOR_EXPR, XOR) \
    4949              :   T (LSHIFT_EXPR, SHL) \
    4950              :   T (RSHIFT_EXPR, SHR) \
    4951              :   T (NEGATE_EXPR, NEG)
    4952              : 
    4953              : /* Return a function that only performs CODE when a certain condition is met
    4954              :    and that uses a given fallback value otherwise.  For example, if CODE is
    4955              :    a binary operation associated with conditional function FN:
    4956              : 
    4957              :      LHS = FN (COND, A, B, ELSE)
    4958              : 
    4959              :    is equivalent to the C expression:
    4960              : 
    4961              :      LHS = COND ? A CODE B : ELSE;
    4962              : 
    4963              :    operating elementwise if the operands are vectors.
    4964              : 
    4965              :    Return IFN_LAST if no such function exists.  */
    4966              : 
    4967              : internal_fn
    4968       574996 : get_conditional_internal_fn (tree_code code)
    4969              : {
    4970       574996 :   switch (code)
    4971              :     {
    4972              : #define CASE(CODE, IFN) case CODE: return IFN_COND_##IFN;
    4973              :       FOR_EACH_CODE_MAPPING(CASE)
    4974              : #undef CASE
    4975              :     default:
    4976              :       return IFN_LAST;
    4977              :     }
    4978              : }
    4979              : 
    4980              : /* If IFN implements the conditional form of a tree code, return that
    4981              :    tree code, otherwise return ERROR_MARK.  */
    4982              : 
    4983              : tree_code
    4984      3572743 : conditional_internal_fn_code (internal_fn ifn)
    4985              : {
    4986      3572743 :   switch (ifn)
    4987              :     {
    4988              : #define CASE(CODE, IFN)                                                        \
    4989              :   case IFN_COND_##IFN:                                                         \
    4990              :   case IFN_COND_LEN_##IFN:                                                     \
    4991              :     return CODE;
    4992              :       FOR_EACH_CODE_MAPPING (CASE)
    4993              : #undef CASE
    4994              :       default:
    4995              :         return ERROR_MARK;
    4996              :     }
    4997              : }
    4998              : 
    4999              : /* Like get_conditional_internal_fn, but return a function that
    5000              :    additionally restricts the operation to the leading elements
    5001              :    of a vector.  The number of elements to process is given by a length
    5002              :    and bias pair, as for IFN_LOAD_LEN.  The values of the remaining
    5003              :    elements are taken from the fallback ("else") argument.
    5004              : 
    5005              :    For example, if CODE is a binary operation associated with FN:
    5006              : 
    5007              :      LHS = FN (COND, A, B, ELSE, LEN, BIAS)
    5008              : 
    5009              :    is equivalent to the C code:
    5010              : 
    5011              :      for (int i = 0; i < NUNITS; i++)
    5012              :       {
    5013              :         if (i < LEN + BIAS && COND[i])
    5014              :           LHS[i] = A[i] CODE B[i];
    5015              :         else
    5016              :           LHS[i] = ELSE[i];
    5017              :       }
    5018              : */
    5019              : 
    5020              : internal_fn
    5021       550173 : get_conditional_len_internal_fn (tree_code code)
    5022              : {
    5023       550173 :   switch (code)
    5024              :     {
    5025              : #define CASE(CODE, IFN) case CODE: return IFN_COND_LEN_##IFN;
    5026              :       FOR_EACH_CODE_MAPPING(CASE)
    5027              : #undef CASE
    5028              :     default:
    5029              :       return IFN_LAST;
    5030              :     }
    5031              : }
    5032              : 
    5033              : /* Invoke T(IFN) for each internal function IFN that also has an
    5034              :    IFN_COND_* form.  */
    5035              : #define FOR_EACH_COND_FN_PAIR(T) \
    5036              :   T (FMAX) \
    5037              :   T (FMIN) \
    5038              :   T (FMA) \
    5039              :   T (FMS) \
    5040              :   T (FNMA) \
    5041              :   T (FNMS) \
    5042              :   T (SQRT) \
    5043              :   T (ROUND) \
    5044              :   T (FLOOR) \
    5045              :   T (RINT) \
    5046              :   T (CEIL)
    5047              : 
    5048              : /* Return a function that only performs internal function FN when a
    5049              :    certain condition is met and that uses a given fallback value otherwise.
    5050              :    In other words, the returned function FN' is such that:
    5051              : 
    5052              :      LHS = FN' (COND, A1, ... An, ELSE)
    5053              : 
    5054              :    is equivalent to the C expression:
    5055              : 
    5056              :      LHS = COND ? FN (A1, ..., An) : ELSE;
    5057              : 
    5058              :    operating elementwise if the operands are vectors.
    5059              : 
    5060              :    Return IFN_LAST if no such function exists.  */
    5061              : 
    5062              : internal_fn
    5063        11741 : get_conditional_internal_fn (internal_fn fn)
    5064              : {
    5065        11741 :   switch (fn)
    5066              :     {
    5067              : #define CASE(NAME) case IFN_##NAME: return IFN_COND_##NAME;
    5068         2250 :       FOR_EACH_COND_FN_PAIR(CASE)
    5069              : #undef CASE
    5070         9490 :     default:
    5071         9490 :       return IFN_LAST;
    5072              :     }
    5073              : }
    5074              : 
    5075              : /* If there exists an internal function like IFN that operates on vectors,
    5076              :    but with additional length and bias parameters, return the internal_fn
    5077              :    for that function, otherwise return IFN_LAST.  */
    5078              : internal_fn
    5079        13878 : get_len_internal_fn (internal_fn fn)
    5080              : {
    5081        13878 :   switch (fn)
    5082              :     {
    5083              : #define DEF_INTERNAL_COND_FN(NAME, ...)                                        \
    5084              :   case IFN_COND_##NAME:                                                        \
    5085              :     return IFN_COND_LEN_##NAME;
    5086              : #define DEF_INTERNAL_SIGNED_COND_FN(NAME, ...)                                 \
    5087              :   case IFN_COND_##NAME:                                                        \
    5088              :     return IFN_COND_LEN_##NAME;
    5089              : #include "internal-fn.def"
    5090         9440 :     default:
    5091         9440 :       break;
    5092              :     }
    5093              : 
    5094         9440 :   switch (fn)
    5095              :     {
    5096              :     case IFN_MASK_LOAD:
    5097              :       return IFN_MASK_LEN_LOAD;
    5098              :     case IFN_MASK_LOAD_LANES:
    5099              :       return IFN_MASK_LEN_LOAD_LANES;
    5100              :     case IFN_MASK_GATHER_LOAD:
    5101              :       return IFN_MASK_LEN_GATHER_LOAD;
    5102              :     default:
    5103              :       return IFN_LAST;
    5104              :     }
    5105              : }
    5106              : 
    5107              : /* If IFN implements the conditional form of an unconditional internal
    5108              :    function, return that unconditional function, otherwise return IFN_LAST.  */
    5109              : 
    5110              : internal_fn
    5111      3516839 : get_unconditional_internal_fn (internal_fn ifn)
    5112              : {
    5113      3516839 :   switch (ifn)
    5114              :     {
    5115              : #define CASE(NAME)                                                             \
    5116              :     case IFN_COND_##NAME:                                                      \
    5117              :     case IFN_COND_LEN_##NAME:                                                  \
    5118              :       return IFN_##NAME;
    5119              : FOR_EACH_COND_FN_PAIR (CASE)
    5120              : #undef CASE
    5121              :     default:
    5122              :       return IFN_LAST;
    5123              :     }
    5124              : }
    5125              : 
    5126              : /* Return true if STMT can be interpreted as a conditional tree code
    5127              :    operation of the form:
    5128              : 
    5129              :      LHS = COND ? OP (RHS1, ...) : ELSE;
    5130              : 
    5131              :    operating elementwise if the operands are vectors.  This includes
    5132              :    the case of an all-true COND, so that the operation always happens.
    5133              : 
    5134              :    There is an alternative approach to interpret the STMT when the operands
    5135              :    are vectors which is the operation predicated by both conditional mask
    5136              :    and loop control length, the equivalent C code:
    5137              : 
    5138              :      for (int i = 0; i < NUNTIS; i++)
    5139              :       {
    5140              :         if (i < LEN + BIAS && COND[i])
    5141              :           LHS[i] = A[i] CODE B[i];
    5142              :         else
    5143              :           LHS[i] = ELSE[i];
    5144              :       }
    5145              : 
    5146              :    When returning true, set:
    5147              : 
    5148              :    - *COND_OUT to the condition COND, or to NULL_TREE if the condition
    5149              :      is known to be all-true
    5150              :    - *CODE_OUT to the tree code
    5151              :    - OPS[I] to operand I of *CODE_OUT
    5152              :    - *ELSE_OUT to the fallback value ELSE, or to NULL_TREE if the
    5153              :      condition is known to be all true.
    5154              :    - *LEN to the len argument if it COND_LEN_* operations or to NULL_TREE.
    5155              :    - *BIAS to the bias argument if it COND_LEN_* operations or to NULL_TREE.  */
    5156              : 
    5157              : bool
    5158        40675 : can_interpret_as_conditional_op_p (gimple *stmt, tree *cond_out,
    5159              :                                    tree_code *code_out,
    5160              :                                    tree (&ops)[3], tree *else_out,
    5161              :                                    tree *len, tree *bias)
    5162              : {
    5163        40675 :   *len = NULL_TREE;
    5164        40675 :   *bias = NULL_TREE;
    5165        40675 :   if (gassign *assign = dyn_cast <gassign *> (stmt))
    5166              :     {
    5167        37987 :       *cond_out = NULL_TREE;
    5168        37987 :       *code_out = gimple_assign_rhs_code (assign);
    5169        37987 :       ops[0] = gimple_assign_rhs1 (assign);
    5170        37987 :       ops[1] = gimple_assign_rhs2 (assign);
    5171        37987 :       ops[2] = gimple_assign_rhs3 (assign);
    5172        37987 :       *else_out = NULL_TREE;
    5173        37987 :       return true;
    5174              :     }
    5175         2688 :   if (gcall *call = dyn_cast <gcall *> (stmt))
    5176         2110 :     if (gimple_call_internal_p (call))
    5177              :       {
    5178         2053 :         internal_fn ifn = gimple_call_internal_fn (call);
    5179         2053 :         tree_code code = conditional_internal_fn_code (ifn);
    5180         2053 :         int len_index = internal_fn_len_index (ifn);
    5181         2053 :         int cond_nargs = len_index >= 0 ? 4 : 2;
    5182         2053 :         if (code != ERROR_MARK)
    5183              :           {
    5184          328 :             *cond_out = gimple_call_arg (call, 0);
    5185          328 :             *code_out = code;
    5186          328 :             unsigned int nops = gimple_call_num_args (call) - cond_nargs;
    5187         1312 :             for (unsigned int i = 0; i < 3; ++i)
    5188          984 :               ops[i] = i < nops ? gimple_call_arg (call, i + 1) : NULL_TREE;
    5189          328 :             *else_out = gimple_call_arg (call, nops + 1);
    5190          328 :             if (len_index < 0)
    5191              :               {
    5192          328 :                 if (integer_truep (*cond_out))
    5193              :                   {
    5194            0 :                     *cond_out = NULL_TREE;
    5195            0 :                     *else_out = NULL_TREE;
    5196              :                   }
    5197              :               }
    5198              :             else
    5199              :               {
    5200            0 :                 *len = gimple_call_arg (call, len_index);
    5201            0 :                 *bias = gimple_call_arg (call, len_index + 1);
    5202              :               }
    5203              :             return true;
    5204              :           }
    5205              :       }
    5206              :   return false;
    5207              : }
    5208              : 
    5209              : /* Return true if IFN is some form of load from memory.  */
    5210              : 
    5211              : bool
    5212       779737 : internal_load_fn_p (internal_fn fn)
    5213              : {
    5214       779737 :   switch (fn)
    5215              :     {
    5216              :     case IFN_MASK_LOAD:
    5217              :     case IFN_LOAD_LANES:
    5218              :     case IFN_MASK_LOAD_LANES:
    5219              :     case IFN_MASK_LEN_LOAD_LANES:
    5220              :     case IFN_GATHER_LOAD:
    5221              :     case IFN_MASK_GATHER_LOAD:
    5222              :     case IFN_MASK_LEN_GATHER_LOAD:
    5223              :     case IFN_LEN_LOAD:
    5224              :     case IFN_MASK_LEN_LOAD:
    5225              :       return true;
    5226              : 
    5227       266443 :     default:
    5228       266443 :       return false;
    5229              :     }
    5230              : }
    5231              : 
    5232              : /* Return true if IFN is some form of store to memory.  */
    5233              : 
    5234              : bool
    5235       568694 : internal_store_fn_p (internal_fn fn)
    5236              : {
    5237       568694 :   switch (fn)
    5238              :     {
    5239              :     case IFN_MASK_STORE:
    5240              :     case IFN_STORE_LANES:
    5241              :     case IFN_MASK_STORE_LANES:
    5242              :     case IFN_MASK_LEN_STORE_LANES:
    5243              :     case IFN_SCATTER_STORE:
    5244              :     case IFN_MASK_SCATTER_STORE:
    5245              :     case IFN_MASK_LEN_SCATTER_STORE:
    5246              :     case IFN_LEN_STORE:
    5247              :     case IFN_MASK_LEN_STORE:
    5248              :       return true;
    5249              : 
    5250       555155 :     default:
    5251       555155 :       return false;
    5252              :     }
    5253              : }
    5254              : 
    5255              : /* Return true if IFN is some form of gather load or scatter store.  */
    5256              : 
    5257              : bool
    5258        52591 : internal_gather_scatter_fn_p (internal_fn fn)
    5259              : {
    5260        52591 :   switch (fn)
    5261              :     {
    5262              :     case IFN_GATHER_LOAD:
    5263              :     case IFN_MASK_GATHER_LOAD:
    5264              :     case IFN_MASK_LEN_GATHER_LOAD:
    5265              :     case IFN_SCATTER_STORE:
    5266              :     case IFN_MASK_SCATTER_STORE:
    5267              :     case IFN_MASK_LEN_SCATTER_STORE:
    5268              :       return true;
    5269              : 
    5270        52591 :     default:
    5271        52591 :       return false;
    5272              :     }
    5273              : }
    5274              : 
    5275              : /* If FN takes a vector len argument, return the index of that argument,
    5276              :    otherwise return -1.  */
    5277              : 
    5278              : int
    5279      3393069 : internal_fn_len_index (internal_fn fn)
    5280              : {
    5281      3393069 :   switch (fn)
    5282              :     {
    5283              :     case IFN_LEN_LOAD:
    5284              :       return 3;
    5285              :     case IFN_LEN_STORE:
    5286              :       return 2;
    5287              : 
    5288              :     case IFN_MASK_LEN_SCATTER_STORE:
    5289              :       return 6;
    5290              : 
    5291              :     case IFN_MASK_LEN_STRIDED_LOAD:
    5292              :       return 5;
    5293              : 
    5294              :     case IFN_MASK_LEN_GATHER_LOAD:
    5295              :       return 7;
    5296              : 
    5297              :     case IFN_COND_LEN_FMA:
    5298              :     case IFN_COND_LEN_FMS:
    5299              :     case IFN_COND_LEN_FNMA:
    5300              :     case IFN_COND_LEN_FNMS:
    5301              :       return 5;
    5302              : 
    5303              :     case IFN_COND_LEN_ADD:
    5304              :     case IFN_COND_LEN_SUB:
    5305              :     case IFN_COND_LEN_MUL:
    5306              :     case IFN_COND_LEN_DIV:
    5307              :     case IFN_COND_LEN_MOD:
    5308              :     case IFN_COND_LEN_RDIV:
    5309              :     case IFN_COND_LEN_MIN:
    5310              :     case IFN_COND_LEN_MAX:
    5311              :     case IFN_COND_LEN_FMIN:
    5312              :     case IFN_COND_LEN_FMAX:
    5313              :     case IFN_COND_LEN_AND:
    5314              :     case IFN_COND_LEN_IOR:
    5315              :     case IFN_COND_LEN_XOR:
    5316              :     case IFN_COND_LEN_SHL:
    5317              :     case IFN_COND_LEN_SHR:
    5318              :     case IFN_MASK_LEN_STRIDED_STORE:
    5319              :       return 4;
    5320              : 
    5321              :     case IFN_COND_LEN_NEG:
    5322              :     case IFN_MASK_LEN_STORE:
    5323              :     case IFN_MASK_LEN_STORE_LANES:
    5324              :     case IFN_VCOND_MASK_LEN:
    5325              :       return 3;
    5326              : 
    5327              :     case IFN_MASK_LEN_LOAD:
    5328              :     case IFN_MASK_LEN_LOAD_LANES:
    5329              :       return 4;
    5330              : 
    5331              :     default:
    5332              :       return -1;
    5333              :     }
    5334              : }
    5335              : 
    5336              : /* If FN is an IFN_COND_* or IFN_COND_LEN_* function, return the index of the
    5337              :    argument that is used when the condition is false.  Return -1 otherwise.  */
    5338              : 
    5339              : int
    5340        87217 : internal_fn_else_index (internal_fn fn)
    5341              : {
    5342        87217 :   switch (fn)
    5343              :     {
    5344              :     case IFN_COND_NEG:
    5345              :     case IFN_COND_NOT:
    5346              :     case IFN_COND_SQRT:
    5347              :     case IFN_COND_CEIL:
    5348              :     case IFN_COND_FLOOR:
    5349              :     case IFN_COND_ROUND:
    5350              :     case IFN_COND_RINT:
    5351              :     case IFN_COND_LEN_NEG:
    5352              :     case IFN_COND_LEN_NOT:
    5353              :     case IFN_COND_LEN_SQRT:
    5354              :     case IFN_COND_LEN_CEIL:
    5355              :     case IFN_COND_LEN_FLOOR:
    5356              :     case IFN_COND_LEN_ROUND:
    5357              :     case IFN_COND_LEN_RINT:
    5358              :       return 2;
    5359              : 
    5360              :     case IFN_LEN_LOAD:
    5361              :       return 2;
    5362              : 
    5363              :     case IFN_COND_ADD:
    5364              :     case IFN_COND_SUB:
    5365              :     case IFN_COND_MUL:
    5366              :     case IFN_COND_DIV:
    5367              :     case IFN_COND_MOD:
    5368              :     case IFN_COND_MIN:
    5369              :     case IFN_COND_MAX:
    5370              :     case IFN_COND_FMIN:
    5371              :     case IFN_COND_FMAX:
    5372              :     case IFN_COND_AND:
    5373              :     case IFN_COND_IOR:
    5374              :     case IFN_COND_XOR:
    5375              :     case IFN_COND_SHL:
    5376              :     case IFN_COND_SHR:
    5377              :     case IFN_COND_LEN_ADD:
    5378              :     case IFN_COND_LEN_SUB:
    5379              :     case IFN_COND_LEN_MUL:
    5380              :     case IFN_COND_LEN_DIV:
    5381              :     case IFN_COND_LEN_MOD:
    5382              :     case IFN_COND_LEN_MIN:
    5383              :     case IFN_COND_LEN_MAX:
    5384              :     case IFN_COND_LEN_FMIN:
    5385              :     case IFN_COND_LEN_FMAX:
    5386              :     case IFN_COND_LEN_AND:
    5387              :     case IFN_COND_LEN_IOR:
    5388              :     case IFN_COND_LEN_XOR:
    5389              :     case IFN_COND_LEN_SHL:
    5390              :     case IFN_COND_LEN_SHR:
    5391              :       return 3;
    5392              : 
    5393              :     case IFN_MASK_LOAD:
    5394              :     case IFN_MASK_LEN_LOAD:
    5395              :     case IFN_MASK_LOAD_LANES:
    5396              :     case IFN_MASK_LEN_LOAD_LANES:
    5397              :       return 3;
    5398              : 
    5399              :     case IFN_COND_FMA:
    5400              :     case IFN_COND_FMS:
    5401              :     case IFN_COND_FNMA:
    5402              :     case IFN_COND_FNMS:
    5403              :     case IFN_COND_LEN_FMA:
    5404              :     case IFN_COND_LEN_FMS:
    5405              :     case IFN_COND_LEN_FNMA:
    5406              :     case IFN_COND_LEN_FNMS:
    5407              :     case IFN_MASK_LEN_STRIDED_LOAD:
    5408              :       return 4;
    5409              : 
    5410              :     case IFN_MASK_GATHER_LOAD:
    5411              :     case IFN_MASK_LEN_GATHER_LOAD:
    5412              :       return 6;
    5413              : 
    5414              :     default:
    5415              :       return -1;
    5416              :     }
    5417              : 
    5418              :   return -1;
    5419              : }
    5420              : 
    5421              : /* If FN takes a vector mask argument, return the index of that argument,
    5422              :    otherwise return -1.  */
    5423              : 
    5424              : int
    5425       225490 : internal_fn_mask_index (internal_fn fn)
    5426              : {
    5427       225490 :   switch (fn)
    5428              :     {
    5429              :     case IFN_MASK_LOAD:
    5430              :     case IFN_MASK_LOAD_LANES:
    5431              :     case IFN_MASK_LEN_LOAD_LANES:
    5432              :     case IFN_MASK_STORE:
    5433              :     case IFN_MASK_STORE_LANES:
    5434              :     case IFN_MASK_LEN_STORE_LANES:
    5435              :     case IFN_MASK_LEN_LOAD:
    5436              :     case IFN_MASK_LEN_STORE:
    5437              :       return 2;
    5438              : 
    5439            0 :     case IFN_MASK_LEN_STRIDED_LOAD:
    5440            0 :     case IFN_MASK_LEN_STRIDED_STORE:
    5441            0 :       return 3;
    5442              : 
    5443            0 :     case IFN_MASK_GATHER_LOAD:
    5444            0 :     case IFN_MASK_SCATTER_STORE:
    5445            0 :     case IFN_MASK_LEN_GATHER_LOAD:
    5446            0 :     case IFN_MASK_LEN_SCATTER_STORE:
    5447            0 :       return 5;
    5448              : 
    5449              :     case IFN_VCOND_MASK:
    5450              :     case IFN_VCOND_MASK_LEN:
    5451              :       return 0;
    5452              : 
    5453       177570 :     default:
    5454       177570 :       return (conditional_internal_fn_code (fn) != ERROR_MARK
    5455       177570 :               || get_unconditional_internal_fn (fn) != IFN_LAST ? 0 : -1);
    5456              :     }
    5457              : }
    5458              : 
    5459              : /* If FN takes a value that should be stored to memory, return the index
    5460              :    of that argument, otherwise return -1.  */
    5461              : 
    5462              : int
    5463        31983 : internal_fn_stored_value_index (internal_fn fn)
    5464              : {
    5465        31983 :   switch (fn)
    5466              :     {
    5467              :     case IFN_MASK_LEN_STRIDED_STORE:
    5468              :       return 2;
    5469              : 
    5470              :     case IFN_MASK_STORE:
    5471              :     case IFN_MASK_STORE_LANES:
    5472              :       return 3;
    5473              :     case IFN_SCATTER_STORE:
    5474              :     case IFN_MASK_SCATTER_STORE:
    5475              :     case IFN_MASK_LEN_SCATTER_STORE:
    5476              :       return 4;
    5477              : 
    5478              :     case IFN_LEN_STORE:
    5479              :       return 4;
    5480              : 
    5481              :     case IFN_MASK_LEN_STORE:
    5482              :     case IFN_MASK_LEN_STORE_LANES:
    5483              :       return 5;
    5484              : 
    5485              :     default:
    5486              :       return -1;
    5487              :     }
    5488              : }
    5489              : 
    5490              : /* If FN has an alias pointer return its index, otherwise return -1.  */
    5491              : 
    5492              : int
    5493            0 : internal_fn_alias_ptr_index (internal_fn fn)
    5494              : {
    5495            0 :   switch (fn)
    5496              :     {
    5497              :     case IFN_MASK_LOAD:
    5498              :     case IFN_MASK_LEN_LOAD:
    5499              :     case IFN_GATHER_LOAD:
    5500              :     case IFN_MASK_GATHER_LOAD:
    5501              :     case IFN_MASK_LEN_GATHER_LOAD:
    5502              :     case IFN_SCATTER_STORE:
    5503              :     case IFN_MASK_SCATTER_STORE:
    5504              :     case IFN_MASK_LEN_SCATTER_STORE:
    5505              :       return 1;
    5506              : 
    5507            0 :     default:
    5508            0 :       return -1;
    5509              :     }
    5510              : }
    5511              : 
    5512              : /* If FN is a gather/scatter return the index of its offset argument,
    5513              :    otherwise return -1.  */
    5514              : 
    5515              : int
    5516            0 : internal_fn_offset_index (internal_fn fn)
    5517              : {
    5518            0 :   if (!internal_gather_scatter_fn_p (fn))
    5519              :     return -1;
    5520              : 
    5521            0 :   switch (fn)
    5522              :     {
    5523              :     case IFN_GATHER_LOAD:
    5524              :     case IFN_MASK_GATHER_LOAD:
    5525              :     case IFN_MASK_LEN_GATHER_LOAD:
    5526              :     case IFN_SCATTER_STORE:
    5527              :     case IFN_MASK_SCATTER_STORE:
    5528              :     case IFN_MASK_LEN_SCATTER_STORE:
    5529              :       return 2;
    5530              : 
    5531              :     default:
    5532              :       return -1;
    5533              :     }
    5534              : }
    5535              : 
    5536              : /* If FN is a gather/scatter return the index of its scale argument,
    5537              :    otherwise return -1.  */
    5538              : 
    5539              : int
    5540            0 : internal_fn_scale_index (internal_fn fn)
    5541              : {
    5542            0 :   if (!internal_gather_scatter_fn_p (fn))
    5543              :     return -1;
    5544              : 
    5545            0 :   switch (fn)
    5546              :     {
    5547              :     case IFN_GATHER_LOAD:
    5548              :     case IFN_MASK_GATHER_LOAD:
    5549              :     case IFN_MASK_LEN_GATHER_LOAD:
    5550              :     case IFN_SCATTER_STORE:
    5551              :     case IFN_MASK_SCATTER_STORE:
    5552              :     case IFN_MASK_LEN_SCATTER_STORE:
    5553              :       return 3;
    5554              : 
    5555              :     default:
    5556              :       return -1;
    5557              :     }
    5558              : }
    5559              : 
    5560              : /* Store all supported else values for the optab referred to by ICODE
    5561              :    in ELSE_VALS.  The index of the else operand must be specified in
    5562              :    ELSE_INDEX.  */
    5563              : 
    5564              : void
    5565        64165 : get_supported_else_vals (enum insn_code icode, unsigned else_index,
    5566              :                          vec<int> &else_vals)
    5567              : {
    5568        64165 :   const struct insn_data_d *data = &insn_data[icode];
    5569        64165 :   if ((int) else_index >= data->n_operands || (int) else_index == -1)
    5570              :     return;
    5571              : 
    5572        64165 :   machine_mode else_mode = data->operand[else_index].mode;
    5573              : 
    5574        64165 :   else_vals.truncate (0);
    5575              : 
    5576              :   /* For now we only support else values of 0, -1, and "undefined".  */
    5577        64165 :   if (insn_operand_matches (icode, else_index, CONST0_RTX (else_mode)))
    5578        64165 :     else_vals.safe_push (MASK_LOAD_ELSE_ZERO);
    5579              : 
    5580        64165 :   if (insn_operand_matches (icode, else_index, gen_rtx_SCRATCH (else_mode)))
    5581            0 :     else_vals.safe_push (MASK_LOAD_ELSE_UNDEFINED);
    5582              : 
    5583        64165 :   if (GET_MODE_CLASS (else_mode) == MODE_VECTOR_INT
    5584        64165 :       && insn_operand_matches (icode, else_index, CONSTM1_RTX (else_mode)))
    5585            0 :     else_vals.safe_push (MASK_LOAD_ELSE_M1);
    5586              : }
    5587              : 
    5588              : /* Return true if the else value ELSE_VAL (one of MASK_LOAD_ELSE_ZERO,
    5589              :    MASK_LOAD_ELSE_M1, and MASK_LOAD_ELSE_UNDEFINED) is valid fo the optab
    5590              :    referred to by ICODE.  The index of the else operand must be specified
    5591              :    in ELSE_INDEX.  */
    5592              : 
    5593              : bool
    5594            0 : supported_else_val_p (enum insn_code icode, unsigned else_index, int else_val)
    5595              : {
    5596            0 :   if (else_val != MASK_LOAD_ELSE_ZERO && else_val != MASK_LOAD_ELSE_M1
    5597            0 :       && else_val != MASK_LOAD_ELSE_UNDEFINED)
    5598            0 :     gcc_unreachable ();
    5599              : 
    5600            0 :   auto_vec<int> else_vals;
    5601            0 :   get_supported_else_vals (icode, else_index, else_vals);
    5602            0 :   return else_vals.contains (else_val);
    5603            0 : }
    5604              : 
    5605              : /* Return true if the target supports gather load or scatter store function
    5606              :    IFN.  For loads, VECTOR_TYPE is the vector type of the load result,
    5607              :    while for stores it is the vector type of the stored data argument.
    5608              :    MEMORY_ELEMENT_TYPE is the type of the memory elements being loaded
    5609              :    or stored.  OFFSET_VECTOR_TYPE is the vector type that holds the
    5610              :    offset from the shared base address of each loaded or stored element.
    5611              :    SCALE is the amount by which these offsets should be multiplied
    5612              :    *after* they have been extended to address width.
    5613              :    If the target supports the gather load the supported else values
    5614              :    will be added to the vector ELSVAL points to if it is nonzero.  */
    5615              : 
    5616              : bool
    5617      2708654 : internal_gather_scatter_fn_supported_p (internal_fn ifn, tree vector_type,
    5618              :                                         tree memory_element_type,
    5619              :                                         tree offset_vector_type, int scale,
    5620              :                                         vec<int> *elsvals)
    5621              : {
    5622      2708654 :   if (!tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (vector_type)),
    5623      2708654 :                            TYPE_SIZE (memory_element_type)))
    5624              :     return false;
    5625      2708654 :   if (maybe_ne (TYPE_VECTOR_SUBPARTS (vector_type),
    5626      5417308 :                 TYPE_VECTOR_SUBPARTS (offset_vector_type)))
    5627              :     return false;
    5628       755182 :   optab optab = direct_internal_fn_optab (ifn);
    5629       755182 :   insn_code icode = convert_optab_handler (optab, TYPE_MODE (vector_type),
    5630       755182 :                                            TYPE_MODE (offset_vector_type));
    5631       755182 :   int output_ops = internal_load_fn_p (ifn) ? 1 : 0;
    5632       755182 :   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (offset_vector_type));
    5633       755182 :   bool ok = icode != CODE_FOR_nothing
    5634            0 :     && insn_operand_matches (icode, 2 + output_ops, GEN_INT (unsigned_p))
    5635       755182 :     && insn_operand_matches (icode, 3 + output_ops, GEN_INT (scale));
    5636              : 
    5637       755182 :   if (ok && elsvals)
    5638            0 :     get_supported_else_vals
    5639            0 :       (icode, internal_fn_else_index (IFN_MASK_GATHER_LOAD), *elsvals);
    5640              : 
    5641              :   return ok;
    5642              : }
    5643              : 
    5644              : /* Return true if the target supports a strided load/store function IFN
    5645              :    with VECTOR_TYPE.  If supported and ELSVALS is nonzero the supported else
    5646              :    values will be added to the vector ELSVALS points to.  */
    5647              : 
    5648              : bool
    5649         1863 : internal_strided_fn_supported_p (internal_fn ifn, tree vector_type,
    5650              :                                  vec<int> *elsvals)
    5651              : {
    5652         1863 :   machine_mode mode = TYPE_MODE (vector_type);
    5653         1863 :   optab optab = direct_internal_fn_optab (ifn);
    5654         1863 :   insn_code icode = direct_optab_handler (optab, mode);
    5655              : 
    5656         1863 :   bool ok = icode != CODE_FOR_nothing;
    5657              : 
    5658         1863 :   if (ok && elsvals)
    5659            0 :     get_supported_else_vals (icode, internal_fn_else_index (ifn), *elsvals);
    5660              : 
    5661         1863 :   return ok;
    5662              : }
    5663              : 
    5664              : /* Return true if the target supports IFN_CHECK_{RAW,WAR}_PTRS function IFN
    5665              :    for pointers of type TYPE when the accesses have LENGTH bytes and their
    5666              :    common byte alignment is ALIGN.  */
    5667              : 
    5668              : bool
    5669         5402 : internal_check_ptrs_fn_supported_p (internal_fn ifn, tree type,
    5670              :                                     poly_uint64 length, unsigned int align)
    5671              : {
    5672         5402 :   machine_mode mode = TYPE_MODE (type);
    5673         5402 :   optab optab = direct_internal_fn_optab (ifn);
    5674         5402 :   insn_code icode = direct_optab_handler (optab, mode);
    5675         5402 :   if (icode == CODE_FOR_nothing)
    5676              :     return false;
    5677            0 :   rtx length_rtx = immed_wide_int_const (length, mode);
    5678            0 :   return (insn_operand_matches (icode, 3, length_rtx)
    5679            0 :           && insn_operand_matches (icode, 4, GEN_INT (align)));
    5680              : }
    5681              : 
    5682              : /* Return the supported bias for IFN which is either IFN_{LEN_,MASK_LEN_,}LOAD
    5683              :    or IFN_{LEN_,MASK_LEN_,}STORE.  For now we only support the biases of 0 and
    5684              :    -1 (in case 0 is not an allowable length for {len_,mask_len_}load or
    5685              :    {len_,mask_len_}store). If none of the biases match what the backend
    5686              :    provides, return VECT_PARTIAL_BIAS_UNSUPPORTED.  */
    5687              : 
    5688              : signed char
    5689            0 : internal_len_load_store_bias (internal_fn ifn, machine_mode mode)
    5690              : {
    5691            0 :   optab optab = direct_internal_fn_optab (ifn);
    5692            0 :   insn_code icode = direct_optab_handler (optab, mode);
    5693            0 :   int bias_idx = internal_fn_len_index (ifn) + 1;
    5694              : 
    5695            0 :   if (icode == CODE_FOR_nothing)
    5696              :     {
    5697            0 :       machine_mode mask_mode;
    5698            0 :       if (!targetm.vectorize.get_mask_mode (mode).exists (&mask_mode))
    5699            0 :         return VECT_PARTIAL_BIAS_UNSUPPORTED;
    5700            0 :       if (ifn == IFN_LEN_LOAD)
    5701              :         {
    5702              :           /* Try MASK_LEN_LOAD.  */
    5703            0 :           optab = direct_internal_fn_optab (IFN_MASK_LEN_LOAD);
    5704            0 :           bias_idx = internal_fn_len_index (IFN_MASK_LEN_LOAD) + 1;
    5705              :         }
    5706              :       else
    5707              :         {
    5708              :           /* Try MASK_LEN_STORE.  */
    5709            0 :           optab = direct_internal_fn_optab (IFN_MASK_LEN_STORE);
    5710            0 :           bias_idx = internal_fn_len_index (IFN_MASK_LEN_STORE) + 1;
    5711              :         }
    5712            0 :       icode = convert_optab_handler (optab, mode, mask_mode);
    5713              :     }
    5714              : 
    5715            0 :   if (icode != CODE_FOR_nothing)
    5716              :     {
    5717              :       /* For now we only support biases of 0 or -1.  Try both of them.  */
    5718            0 :       if (insn_operand_matches (icode, bias_idx, GEN_INT (0)))
    5719              :         return 0;
    5720            0 :       if (insn_operand_matches (icode, bias_idx, GEN_INT (-1)))
    5721            0 :         return -1;
    5722              :     }
    5723              : 
    5724              :   return VECT_PARTIAL_BIAS_UNSUPPORTED;
    5725              : }
    5726              : 
    5727              : /* Expand STMT as though it were a call to internal function FN.  */
    5728              : 
    5729              : void
    5730       271782 : expand_internal_call (internal_fn fn, gcall *stmt)
    5731              : {
    5732       271782 :   internal_fn_expanders[fn] (fn, stmt);
    5733       271782 : }
    5734              : 
    5735              : /* Expand STMT, which is a call to internal function FN.  */
    5736              : 
    5737              : void
    5738       236761 : expand_internal_call (gcall *stmt)
    5739              : {
    5740       236761 :   expand_internal_call (gimple_call_internal_fn (stmt), stmt);
    5741       236761 : }
    5742              : 
    5743              : /* If TYPE is a vector type, return true if IFN is a direct internal
    5744              :    function that is supported for that type.  If TYPE is a scalar type,
    5745              :    return true if IFN is a direct internal function that is supported for
    5746              :    the target's preferred vector version of TYPE.  */
    5747              : 
    5748              : bool
    5749        18690 : vectorized_internal_fn_supported_p (internal_fn ifn, tree type)
    5750              : {
    5751        18690 :   if (VECTOR_MODE_P (TYPE_MODE (type)))
    5752        12276 :     return direct_internal_fn_supported_p (ifn, type, OPTIMIZE_FOR_SPEED);
    5753              : 
    5754         6414 :   scalar_mode smode;
    5755         6414 :   if (VECTOR_TYPE_P (type)
    5756         6414 :       || !is_a <scalar_mode> (TYPE_MODE (type), &smode))
    5757              :     return false;
    5758              : 
    5759         6403 :   machine_mode vmode = targetm.vectorize.preferred_simd_mode (smode);
    5760         6403 :   if (VECTOR_MODE_P (vmode))
    5761              :     {
    5762         6364 :       tree vectype = build_vector_type_for_mode (type, vmode);
    5763         6364 :       if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
    5764              :         return true;
    5765              :     }
    5766              : 
    5767         4328 :   auto_vector_modes vector_modes;
    5768         4328 :   targetm.vectorize.autovectorize_vector_modes (&vector_modes, true);
    5769        26987 :   for (machine_mode base_mode : vector_modes)
    5770        14003 :     if (related_vector_mode (base_mode, smode).exists (&vmode))
    5771              :       {
    5772        12268 :         tree vectype = build_vector_type_for_mode (type, vmode);
    5773        12268 :         if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
    5774              :           return true;
    5775              :       }
    5776              : 
    5777              :   return false;
    5778         4328 : }
    5779              : 
    5780              : void
    5781            0 : expand_SHUFFLEVECTOR (internal_fn, gcall *)
    5782              : {
    5783            0 :   gcc_unreachable ();
    5784              : }
    5785              : 
    5786              : void
    5787            0 : expand_PHI (internal_fn, gcall *)
    5788              : {
    5789            0 :   gcc_unreachable ();
    5790              : }
    5791              : 
    5792              : void
    5793          379 : expand_SPACESHIP (internal_fn, gcall *stmt)
    5794              : {
    5795          379 :   tree lhs = gimple_call_lhs (stmt);
    5796          379 :   tree rhs1 = gimple_call_arg (stmt, 0);
    5797          379 :   tree rhs2 = gimple_call_arg (stmt, 1);
    5798          379 :   tree rhs3 = gimple_call_arg (stmt, 2);
    5799          379 :   tree type = TREE_TYPE (rhs1);
    5800              : 
    5801          379 :   do_pending_stack_adjust ();
    5802              : 
    5803          379 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    5804          379 :   rtx op1 = expand_normal (rhs1);
    5805          379 :   rtx op2 = expand_normal (rhs2);
    5806          379 :   rtx op3 = expand_normal (rhs3);
    5807              : 
    5808          379 :   class expand_operand ops[4];
    5809          379 :   create_call_lhs_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (lhs)));
    5810          379 :   create_input_operand (&ops[1], op1, TYPE_MODE (type));
    5811          379 :   create_input_operand (&ops[2], op2, TYPE_MODE (type));
    5812          379 :   create_input_operand (&ops[3], op3, TYPE_MODE (TREE_TYPE (rhs3)));
    5813          379 :   insn_code icode = optab_handler (spaceship_optab, TYPE_MODE (type));
    5814          379 :   expand_insn (icode, 4, ops);
    5815          379 :   assign_call_lhs (lhs, target, &ops[0]);
    5816          379 : }
    5817              : 
    5818              : void
    5819            0 : expand_ASSUME (internal_fn, gcall *)
    5820              : {
    5821            0 : }
    5822              : 
    5823              : void
    5824            0 : expand_MASK_CALL (internal_fn, gcall *)
    5825              : {
    5826              :   /* This IFN should only exist between ifcvt and vect passes.  */
    5827            0 :   gcc_unreachable ();
    5828              : }
    5829              : 
    5830              : void
    5831            0 : expand_VARYING (internal_fn, gcall *)
    5832              : {
    5833              :   /* This IFN should reach expand.  */
    5834            0 :   gcc_unreachable ();
    5835              : }
    5836              : 
    5837              : void
    5838         1763 : expand_MULBITINT (internal_fn, gcall *stmt)
    5839              : {
    5840         1763 :   rtx_mode_t args[6];
    5841        12341 :   for (int i = 0; i < 6; i++)
    5842        10578 :     args[i] = rtx_mode_t (expand_normal (gimple_call_arg (stmt, i)),
    5843        21156 :                           (i & 1) ? SImode : ptr_mode);
    5844         1763 :   rtx fun = init_one_libfunc ("__mulbitint3");
    5845         1763 :   emit_library_call_value_1 (0, fun, NULL_RTX, LCT_NORMAL, VOIDmode, 6, args);
    5846         1763 : }
    5847              : 
    5848              : void
    5849          163 : expand_DIVMODBITINT (internal_fn, gcall *stmt)
    5850              : {
    5851          163 :   rtx_mode_t args[8];
    5852         1467 :   for (int i = 0; i < 8; i++)
    5853         1304 :     args[i] = rtx_mode_t (expand_normal (gimple_call_arg (stmt, i)),
    5854         2608 :                           (i & 1) ? SImode : ptr_mode);
    5855          163 :   rtx fun = init_one_libfunc ("__divmodbitint4");
    5856          163 :   emit_library_call_value_1 (0, fun, NULL_RTX, LCT_NORMAL, VOIDmode, 8, args);
    5857          163 : }
    5858              : 
    5859              : void
    5860          179 : expand_FLOATTOBITINT (internal_fn, gcall *stmt)
    5861              : {
    5862          179 :   machine_mode mode = TYPE_MODE (TREE_TYPE (gimple_call_arg (stmt, 2)));
    5863          179 :   rtx arg0 = expand_normal (gimple_call_arg (stmt, 0));
    5864          179 :   rtx arg1 = expand_normal (gimple_call_arg (stmt, 1));
    5865          179 :   rtx arg2 = expand_normal (gimple_call_arg (stmt, 2));
    5866          179 :   rtx fun = optab_libfunc (bitintfromfp_optab, mode);
    5867          179 :   emit_library_call (fun, LCT_NORMAL, VOIDmode, arg0, ptr_mode, arg1,
    5868              :                      SImode, arg2, mode);
    5869          179 : }
    5870              : 
    5871              : void
    5872          148 : expand_BITINTTOFLOAT (internal_fn, gcall *stmt)
    5873              : {
    5874          148 :   tree lhs = gimple_call_lhs (stmt);
    5875          148 :   if (!lhs)
    5876              :     return;
    5877          148 :   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
    5878          148 :   rtx arg0 = expand_normal (gimple_call_arg (stmt, 0));
    5879          148 :   rtx arg1 = expand_normal (gimple_call_arg (stmt, 1));
    5880          148 :   rtx fun = optab_libfunc (bitinttofp_optab, mode);
    5881          148 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    5882          148 :   rtx val = emit_library_call_value (fun, target, LCT_PURE, mode,
    5883              :                                      arg0, ptr_mode, arg1, SImode);
    5884          148 :   if (val != target)
    5885            0 :     emit_move_insn (target, val);
    5886              : }
    5887              : 
    5888              : static bool
    5889         8159 : expand_bitquery (internal_fn fn, gcall *stmt)
    5890              : {
    5891         8159 :   tree lhs = gimple_call_lhs (stmt);
    5892         8159 :   if (lhs == NULL_TREE)
    5893              :     return false;
    5894         8153 :   tree arg = gimple_call_arg (stmt, 0);
    5895         8153 :   if (TREE_CODE (arg) == INTEGER_CST)
    5896              :     {
    5897            1 :       tree ret;
    5898              :       /* A second argument of IFN_CLZ or IFN_CTZ is the result for zero.  */
    5899            1 :       if ((fn == IFN_CLZ || fn == IFN_CTZ) && gimple_call_num_args (stmt) == 2)
    5900            1 :         ret = fold_const_call (as_combined_fn (fn), TREE_TYPE (lhs), arg,
    5901              :                                gimple_call_arg (stmt, 1));
    5902              :       else
    5903            0 :         ret = fold_const_call (as_combined_fn (fn), TREE_TYPE (arg), arg);
    5904            1 :       gcc_checking_assert (ret && TREE_CODE (ret) == INTEGER_CST);
    5905            1 :       expand_assignment (lhs, ret, false);
    5906            1 :       return false;
    5907              :     }
    5908              :   return true;
    5909              : }
    5910              : 
    5911              : void
    5912            1 : expand_CLRSB (internal_fn fn, gcall *stmt)
    5913              : {
    5914            1 :   if (expand_bitquery (fn, stmt))
    5915            0 :     expand_unary_optab_fn (fn, stmt, clrsb_optab);
    5916            1 : }
    5917              : 
    5918              : /* Expand STMT, a call to the IFN_CLZ or IFN_CTZ internal function FN,
    5919              :    using OPTAB.  The optional second argument of STMT is the result for a
    5920              :    zero first argument.  If it is not the value that the instruction gives
    5921              :    for zero, test the first argument for zero and use the second argument
    5922              :    as the result.  If the target does not define the instruction for zero,
    5923              :    execute it only for a nonzero first argument.  */
    5924              : 
    5925              : static void
    5926         6347 : expand_clz_ctz (internal_fn fn, gcall *stmt, direct_optab optab)
    5927              : {
    5928         6347 :   if (!expand_bitquery (fn, stmt))
    5929         6133 :     return;
    5930         6344 :   tree arg = gimple_call_arg (stmt, 0);
    5931         6344 :   machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
    5932         6344 :   int val = 0, defined = 0;
    5933         6344 :   if (gimple_call_num_args (stmt) == 2)
    5934          622 :     defined = (fn == IFN_CLZ
    5935          528 :                ? CLZ_DEFINED_VALUE_AT_ZERO (mode, val)
    5936           94 :                : CTZ_DEFINED_VALUE_AT_ZERO (mode, val));
    5937         6344 :   if (gimple_call_num_args (stmt) == 1
    5938         6344 :       || (defined == 2
    5939         6227 :           && wi::to_widest (gimple_call_arg (stmt, 1)) == val
    5940           97 :           && direct_optab_handler (optab, mode) != CODE_FOR_nothing))
    5941              :     {
    5942         6130 :       expand_unary_optab_fn (fn, stmt, optab);
    5943         6130 :       return;
    5944              :     }
    5945              :   /* The vectorizer creates vector calls only with the value of the
    5946              :      target.  */
    5947          214 :   gcc_checking_assert (!VECTOR_TYPE_P (TREE_TYPE (arg)));
    5948              : 
    5949          214 :   tree lhs = gimple_call_lhs (stmt);
    5950          214 :   rtx op0 = force_reg (mode, expand_normal (arg));
    5951          214 :   rtx zero_val = expand_normal (fold_convert (TREE_TYPE (lhs),
    5952              :                                               gimple_call_arg (stmt, 1)));
    5953          214 :   rtx res = gen_reg_rtx (TYPE_MODE (TREE_TYPE (lhs)));
    5954          214 :   rtx_code_label *done = gen_label_rtx ();
    5955          214 :   if (defined)
    5956              :     {
    5957            0 :       convert_move (res, expand_unop (mode, optab, op0, NULL_RTX, 1), 1);
    5958            0 :       emit_cmp_and_jump_insns (op0, const0_rtx, NE, NULL_RTX, mode, true,
    5959              :                                done, profile_probability::likely ());
    5960            0 :       emit_move_insn (res, zero_val);
    5961              :     }
    5962              :   else
    5963              :     {
    5964          214 :       emit_move_insn (res, zero_val);
    5965          214 :       emit_cmp_and_jump_insns (op0, const0_rtx, EQ, NULL_RTX, mode, true,
    5966              :                                done, profile_probability::unlikely ());
    5967          214 :       convert_move (res, expand_unop (mode, optab, op0, NULL_RTX, 1), 1);
    5968              :     }
    5969          214 :   emit_label (done);
    5970          214 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    5971          214 :   if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
    5972            0 :     convert_move (SUBREG_REG (target), res, SUBREG_PROMOTED_SIGN (target));
    5973              :   else
    5974          214 :     emit_move_insn (target, res);
    5975              : }
    5976              : 
    5977              : void
    5978         4959 : expand_CLZ (internal_fn fn, gcall *stmt)
    5979              : {
    5980         4959 :   expand_clz_ctz (fn, stmt, clz_optab);
    5981         4959 : }
    5982              : 
    5983              : void
    5984         1388 : expand_CTZ (internal_fn fn, gcall *stmt)
    5985              : {
    5986         1388 :   expand_clz_ctz (fn, stmt, ctz_optab);
    5987         1388 : }
    5988              : 
    5989              : void
    5990          133 : expand_FFS (internal_fn fn, gcall *stmt)
    5991              : {
    5992          133 :   if (expand_bitquery (fn, stmt))
    5993          132 :     expand_unary_optab_fn (fn, stmt, ffs_optab);
    5994          133 : }
    5995              : 
    5996              : void
    5997          200 : expand_PARITY (internal_fn fn, gcall *stmt)
    5998              : {
    5999          200 :   if (expand_bitquery (fn, stmt))
    6000          199 :     expand_unary_optab_fn (fn, stmt, parity_optab);
    6001          200 : }
    6002              : 
    6003              : void
    6004          234 : expand_POPCOUNT (internal_fn fn, gcall *stmt)
    6005              : {
    6006          234 :   if (!expand_bitquery (fn, stmt))
    6007              :     return;
    6008          233 :   if (gimple_call_num_args (stmt) == 1)
    6009              :     {
    6010          225 :       expand_unary_optab_fn (fn, stmt, popcount_optab);
    6011          225 :       return;
    6012              :     }
    6013              :   /* If .POPCOUNT call has 2 arguments, match_single_bit_test marked it
    6014              :      because the result is only used in an equality comparison against 1.
    6015              :      Use rtx costs in that case to determine if .POPCOUNT (arg) == 1
    6016              :      or (arg ^ (arg - 1)) > arg - 1 is cheaper.
    6017              :      If .POPCOUNT second argument is 0, we additionally know that arg
    6018              :      is non-zero, so use arg & (arg - 1) == 0 instead.
    6019              :      If .POPCOUNT second argument is -1, the comparison was either `<= 1`
    6020              :      or `> 1`.  */
    6021            8 :   bool speed_p = optimize_insn_for_speed_p ();
    6022            8 :   tree lhs = gimple_call_lhs (stmt);
    6023            8 :   tree arg = gimple_call_arg (stmt, 0);
    6024            8 :   bool nonzero_arg = integer_zerop (gimple_call_arg (stmt, 1));
    6025            8 :   bool was_le = integer_minus_onep (gimple_call_arg (stmt, 1));
    6026            8 :   if (was_le)
    6027            0 :     nonzero_arg = true;
    6028            8 :   tree type = TREE_TYPE (arg);
    6029            8 :   machine_mode mode = TYPE_MODE (type);
    6030            8 :   machine_mode lhsmode = TYPE_MODE (TREE_TYPE (lhs));
    6031            8 :   do_pending_stack_adjust ();
    6032            8 :   start_sequence ();
    6033            8 :   expand_unary_optab_fn (fn, stmt, popcount_optab);
    6034            8 :   rtx_insn *popcount_insns = end_sequence ();
    6035            8 :   start_sequence ();
    6036            8 :   rtx plhs = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    6037            8 :   rtx pcmp = emit_store_flag (NULL_RTX, EQ, plhs, const1_rtx, lhsmode, 0, 0);
    6038            8 :   if (pcmp == NULL_RTX)
    6039              :     {
    6040            0 :     fail:
    6041            0 :       end_sequence ();
    6042            0 :       emit_insn (popcount_insns);
    6043            0 :       return;
    6044              :     }
    6045            8 :   rtx_insn *popcount_cmp_insns = end_sequence ();
    6046            8 :   start_sequence ();
    6047            8 :   rtx op0 = expand_normal (arg);
    6048            8 :   rtx argm1 = expand_simple_binop (mode, PLUS, op0, constm1_rtx, NULL_RTX,
    6049              :                                    1, OPTAB_WIDEN);
    6050            8 :   if (argm1 == NULL_RTX)
    6051            0 :     goto fail;
    6052           12 :   rtx argxorargm1 = expand_simple_binop (mode, nonzero_arg ? AND : XOR, op0,
    6053              :                                          argm1, NULL_RTX, 1, OPTAB_WIDEN);
    6054            8 :   if (argxorargm1 == NULL_RTX)
    6055            0 :     goto fail;
    6056            8 :   rtx cmp;
    6057            8 :   if (nonzero_arg)
    6058            4 :     cmp = emit_store_flag (NULL_RTX, EQ, argxorargm1, const0_rtx, mode, 1, 1);
    6059              :   else
    6060            4 :     cmp = emit_store_flag (NULL_RTX, GTU, argxorargm1, argm1, mode, 1, 1);
    6061            8 :   if (cmp == NULL_RTX)
    6062            0 :     goto fail;
    6063            8 :   rtx_insn *cmp_insns = end_sequence ();
    6064            8 :   unsigned popcount_cost = (seq_cost (popcount_insns, speed_p)
    6065            8 :                             + seq_cost (popcount_cmp_insns, speed_p));
    6066            8 :   unsigned cmp_cost = seq_cost (cmp_insns, speed_p);
    6067              : 
    6068            8 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6069            0 :     fprintf(dump_file, "popcount == 1: popcount cost: %u; cmp cost: %u\n",
    6070              :             popcount_cost, cmp_cost);
    6071              : 
    6072            8 :   if (popcount_cost <= cmp_cost)
    6073            8 :     emit_insn (popcount_insns);
    6074              :   else
    6075              :     {
    6076            0 :       start_sequence ();
    6077            0 :       emit_insn (cmp_insns);
    6078            0 :       plhs = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    6079            0 :       if (GET_MODE (cmp) != GET_MODE (plhs))
    6080            0 :         cmp = convert_to_mode (GET_MODE (plhs), cmp, 1);
    6081              :       /* For `<= 1`, we need to produce `2 - cmp` or `cmp ? 1 : 2` as that
    6082              :          then gets compared against 1 and we need the false case to be 2.  */
    6083            0 :       if (was_le)
    6084              :         {
    6085            0 :           cmp = expand_simple_binop (GET_MODE (cmp), MINUS, const2_rtx,
    6086              :                                      cmp, NULL_RTX, 1, OPTAB_WIDEN);
    6087            0 :           if (!cmp)
    6088            0 :             goto fail;
    6089              :         }
    6090            0 :       emit_move_insn (plhs, cmp);
    6091            0 :       rtx_insn *all_insns = end_sequence ();
    6092            0 :       emit_insn (all_insns);
    6093              :     }
    6094              : }
    6095              : 
    6096              : void
    6097         1152 : expand_BSWAP (internal_fn fn, gcall *stmt)
    6098              : {
    6099         1152 :   if (expand_bitquery (fn, stmt))
    6100         1152 :     expand_unary_optab_fn (fn, stmt, bswap_optab);
    6101         1152 : }
    6102              : 
    6103              : void
    6104           92 : expand_BITREVERSE (internal_fn fn, gcall *stmt)
    6105              : {
    6106           92 :   if (expand_bitquery (fn, stmt))
    6107           92 :     expand_unary_optab_fn (fn, stmt, bitreverse_optab);
    6108           92 : }
    6109              : 
    6110              : /* Expand CLMUL (OP0, OP1) where operands are in OP_MODE and have
    6111              :    minimum unsigned precision PREC0 and PREC1.  If OP_EQ, op0 and op1
    6112              :    have the same value.  Compute result in MODE.  */
    6113              : 
    6114              : static rtx
    6115          250 : expand_clmul (machine_mode mode, machine_mode op_mode, rtx op0, rtx op1,
    6116              :               unsigned int prec0, unsigned int prec1, bool op_eq)
    6117              : {
    6118          250 :   unsigned int op_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (op_mode));
    6119          250 :   unsigned int prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode));
    6120          250 :   machine_mode cmode;
    6121          250 :   optab_methods olw = OPTAB_LIB_WIDEN;
    6122          250 :   if (MIN (prec0, prec1) == 1)
    6123              :     {
    6124              :       /* If one operand has [0, 1] range, we can do just
    6125              :          multiplication and if both are [0, 1], this is equivalent to AND.  */
    6126            0 :       rtx res = expand_binop (op_mode, prec0 == prec1 ? and_optab : smul_optab,
    6127              :                               op0, op1, NULL_RTX, 1, olw);
    6128            0 :       return convert_modes (mode, op_mode, res, 1);
    6129              :     }
    6130         1875 :   FOR_EACH_MODE_IN_CLASS (cmode, MODE_INT)
    6131              :     {
    6132         1655 :       unsigned int this_prec
    6133         1655 :         = GET_MODE_PRECISION (as_a <scalar_int_mode> (cmode));
    6134         1655 :       if (prec0 > this_prec || prec1 > this_prec)
    6135          529 :         continue;
    6136         1126 :       if (op_eq
    6137          269 :           && (prec0 + prec1 - 1 <= this_prec || prec <= this_prec)
    6138         1360 :           && optab_handler (bitexpand_optab, cmode) != CODE_FOR_nothing)
    6139              :         {
    6140              :           /* Optimize clmul (x, x) using bitexpand (x, 0x55555555U)
    6141              :              if supported in hw.  */
    6142            7 :           rtx_insn *start = get_last_insn ();
    6143            7 :           rtx op0c = convert_modes (cmode, op_mode, op0, 1);
    6144            7 :           rtx op1c
    6145            7 :             = immed_wide_int_const (wi::bit_repeat (wide_int::from (1, 2,
    6146              :                                                                     UNSIGNED),
    6147              :                                                     this_prec), cmode);
    6148            7 :           if (rtx res = expand_binop (cmode, bitexpand_optab, op0c, op1c,
    6149              :                                       NULL_RTX, 1, OPTAB_DIRECT))
    6150            7 :             return convert_modes (mode, cmode, res, 1);
    6151            0 :           delete_insns_since (start);
    6152              :         }
    6153         1119 :       if (optab_handler (clmul_optab, cmode) != CODE_FOR_nothing
    6154         1119 :           && (prec0 + prec1 - 1 <= this_prec || prec <= this_prec))
    6155              :         {
    6156           16 :           rtx_insn *start = get_last_insn ();
    6157           16 :           rtx op0c = convert_modes (cmode, op_mode, op0, 1);
    6158           16 :           rtx op1c = convert_modes (cmode, op_mode, op1, 1);
    6159           16 :           if (rtx res = expand_binop (cmode, clmul_optab, op0c, op1c,
    6160              :                                       NULL_RTX, 1, OPTAB_DIRECT))
    6161           16 :             return convert_modes (mode, cmode, res, 1);
    6162            0 :           delete_insns_since (start);
    6163              :         }
    6164         1103 :       if (optab_handler (clmul_widen_optab, cmode) != CODE_FOR_nothing)
    6165              :         {
    6166            7 :           rtx_insn *start = get_last_insn ();
    6167            7 :           rtx op0c = convert_modes (cmode, op_mode, op0, 1);
    6168            7 :           rtx op1c = convert_modes (cmode, op_mode, op1, 1);
    6169            7 :           rtx reslo = gen_reg_rtx (cmode);
    6170            7 :           rtx reshi = gen_reg_rtx (cmode);
    6171            7 :           class expand_operand ops[4];
    6172            7 :           create_output_operand (&ops[0], reslo, cmode);
    6173            7 :           create_output_operand (&ops[1], reshi, cmode);
    6174            7 :           create_input_operand (&ops[2], op0c, cmode);
    6175            7 :           create_input_operand (&ops[3], op1c, cmode);
    6176            7 :           if (maybe_expand_insn (optab_handler (clmul_widen_optab, cmode),
    6177              :                                  4, ops))
    6178              :             {
    6179            7 :               if (prec0 + prec1 - 1 <= this_prec || prec <= this_prec)
    6180            0 :                 return convert_modes (mode, cmode, reslo, 1);
    6181            7 :               machine_mode wmode;
    6182            7 :               if (!GET_MODE_2XWIDER_MODE (cmode).exists (&wmode))
    6183              :                 wmode = mode;
    6184            7 :               reslo = convert_modes (wmode, cmode, reslo, 1);
    6185            7 :               reshi = convert_modes (wmode, cmode, reshi, 1);
    6186            7 :               reshi = expand_shift (LSHIFT_EXPR, wmode, reshi, this_prec,
    6187              :                                     NULL_RTX, 1);
    6188            7 :               rtx res = expand_binop (wmode, ior_optab, reslo, reshi,
    6189              :                                       NULL_RTX, 1, olw);
    6190            7 :               return convert_modes (mode, wmode, res, 1);
    6191              :             }
    6192            0 :           delete_insns_since (start);
    6193              :         }
    6194              :     }
    6195          220 :   if (CONST_SCALAR_INT_P (op1))
    6196              :     {
    6197           53 :       wide_int wop1 = rtx_mode_t (op1, op_mode);
    6198           53 :       unsigned lz = wi::clz (wop1);
    6199           53 :       unsigned tz = wi::ctz (wop1);
    6200           53 :       unsigned cnt = wi::popcount (wop1);
    6201           53 :       if (lz + cnt + tz == op_prec
    6202           69 :           && (pow2p_hwi (cnt) || (lz == 0 && prec == op_prec)))
    6203              :         {
    6204              :           /* Optimize the cases where one argument is a constant
    6205              :              equal to ((((T) 1) << M) - 1) << N for some M and N
    6206              :              constants.
    6207              :              In this case the clmul (x, ((((T) 1) << M) - 1) << N)
    6208              :              result can be computed as
    6209              :              T r = x;
    6210              :              for (unsigned i = 1; i < M; i = i + i)
    6211              :                r ^= r << i;
    6212              :              return r << N;
    6213              :              If M is not a power of two, it shouldn't be a widening
    6214              :              shift and most significant bit of the constant should be
    6215              :              set.  */
    6216           33 :           rtx x = convert_modes (mode, op_mode, op0, 1);
    6217           33 :           rtx res = gen_reg_rtx (mode), tem;
    6218           33 :           emit_move_insn (res, x);
    6219           33 :           if (cnt > 2)
    6220              :             {
    6221           20 :               rtx i = gen_reg_rtx (TYPE_MODE (integer_type_node));
    6222           20 :               emit_move_insn (i, const1_rtx);
    6223           20 :               rtx_code_label *loop_label = gen_label_rtx ();
    6224           20 :               emit_label (loop_label);
    6225           20 :               tem = expand_variable_shift (LSHIFT_EXPR, mode, res,
    6226              :                                            make_tree (integer_type_node, i),
    6227              :                                            NULL_RTX, 1);
    6228           20 :               tem = expand_binop (mode, xor_optab, res, tem, res, 1, olw);
    6229           20 :               if (tem != res)
    6230            0 :                 emit_move_insn (res, tem);
    6231           20 :               tem = expand_binop (GET_MODE (i), add_optab, i, i, i, 1, olw);
    6232           20 :               if (tem != res)
    6233           20 :                 emit_move_insn (i, tem);
    6234           40 :               do_compare_rtx_and_jump (i, gen_int_mode (cnt, GET_MODE (i)),
    6235           20 :                                        LTU, true, GET_MODE (i), NULL_RTX, NULL,
    6236              :                                        loop_label,
    6237              :                                        profile_probability::very_likely ());
    6238              :             }
    6239           13 :           else if (cnt == 2)
    6240              :             {
    6241              :               /* For M 2 we don't need a loop, unroll the loop manually.  */
    6242            4 :               tem = expand_shift (LSHIFT_EXPR, mode, res, 1, NULL_RTX, 1);
    6243            4 :               tem = expand_binop (mode, xor_optab, res, tem, res, 1, olw);
    6244            4 :               if (tem != res)
    6245            0 :                 emit_move_insn (res, tem);
    6246              :             }
    6247           33 :           if (tz)
    6248              :             {
    6249           25 :               tem = expand_shift (LSHIFT_EXPR, mode, res, tz, res, 1);
    6250           25 :               if (tem != res)
    6251            0 :                 emit_move_insn (res, tem);
    6252              :             }
    6253           33 :           return res;
    6254              :         }
    6255           53 :     }
    6256              :   /* Fallback using Kronecker substitution.
    6257              :      Depending on the number of active bits in the operand with smaller
    6258              :      number of those and the result precision we choose smallest spacing
    6259              :      S in [2, 13], such that MIN (prec0, prec1) <= S * ((1 << S) - 1),
    6260              :      or prec <= S * (1 << S) or prec0 + prec1 - 1 <= S * (1 << S).
    6261              :      The algorithm then uses S * S multiplications, 3 * S bitwise ANDs,
    6262              :      S * (S - 1) XORs and S - 1 IORs.
    6263              :      For e.g. S = 4 this is:
    6264              :      uint64_t x0 = x & 0x1111111111111111ULL, y0 = y & 0x1111111111111111ULL;
    6265              :      uint64_t x1 = x & 0x2222222222222222ULL, y1 = y & 0x2222222222222222ULL;
    6266              :      uint64_t x2 = x & 0x4444444444444444ULL, y2 = y & 0x4444444444444444ULL;
    6267              :      uint64_t x3 = x & 0x8888888888888888ULL, y3 = y & 0x8888888888888888ULL;
    6268              :      uint64_t z0 = (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1);
    6269              :      uint64_t z1 = (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2);
    6270              :      uint64_t z2 = (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3);
    6271              :      uint64_t z3 = (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0);
    6272              :      z0 &= 0x1111111111111111ULL;
    6273              :      z1 &= 0x2222222222222222ULL;
    6274              :      z2 &= 0x4444444444444444ULL;
    6275              :      z3 &= 0x8888888888888888ULL;
    6276              :      return (z0 | z1) | (z2 | z3);  */
    6277          187 :   unsigned int s;
    6278          535 :   for (s = 2; s <= 13; ++s)
    6279          535 :     if (MIN (prec0, prec1) <= s * ((1 << s) - 1)
    6280          376 :         || MIN (prec, prec0 + prec1 - 1) <= s * (1 << s))
    6281              :       break;
    6282              :   /* S = 13 should handle up to _BitInt maximum 65535 (106483 bits).  */
    6283          187 :   gcc_assert (s <= 13);
    6284          187 :   rtx mask[13], x[13], y[13], t[13], r;
    6285          187 :   bool do_widen_mult = false;
    6286          187 :   if (mode != op_mode
    6287          187 :       && (convert_optab_handler (umul_widen_optab, mode, op_mode)
    6288              :           != CODE_FOR_nothing))
    6289              :     do_widen_mult = true;
    6290          909 :   for (unsigned int i = 0; i < s; ++i)
    6291              :     {
    6292          722 :       mask[i]
    6293          722 :         = immed_wide_int_const (wi::bit_repeat (wide_int::from (1 << i, s,
    6294              :                                                                 UNSIGNED),
    6295              :                                                 op_prec), op_mode);
    6296          722 :       if (CONST_INT_P (op0) && CONST_INT_P (mask[i]))
    6297            0 :         x[i] = gen_int_mode (INTVAL (op0) & INTVAL (mask[i]), op_mode);
    6298              :       else
    6299          722 :         x[i] = expand_binop (op_mode, and_optab, op0, mask[i], NULL_RTX, 1,
    6300              :                              olw);
    6301          722 :       if (mode != op_mode && !do_widen_mult)
    6302           67 :         x[i] = convert_modes (mode, op_mode, x[i], 1);
    6303          722 :       if (CONST_INT_P (op1) && CONST_INT_P (mask[i]))
    6304           88 :         y[i] = gen_int_mode (INTVAL (op1) & INTVAL (mask[i]), op_mode);
    6305              :       else
    6306          634 :         y[i] = expand_binop (op_mode, and_optab, op1, mask[i], NULL_RTX, 1,
    6307              :                              olw);
    6308          722 :       if (mode != op_mode && !do_widen_mult)
    6309           67 :         y[i] = convert_modes (mode, op_mode, y[i], 1);
    6310              :     }
    6311          909 :   for (unsigned int i = 0; i < s; ++i)
    6312              :     {
    6313         3728 :       for (unsigned int j = 0; j < s; ++j)
    6314              :         {
    6315         3006 :           unsigned int k = (i + s - j) % s;
    6316         3006 :           if (do_widen_mult)
    6317          753 :             t[j] = expand_widening_mult (mode, x[j], y[k], NULL_RTX, 1,
    6318              :                                          umul_widen_optab);
    6319              :           else
    6320         2253 :             t[j] = expand_binop (mode, smul_optab, x[j], y[k], NULL_RTX, 1,
    6321              :                                  olw);
    6322         3006 :           if ((j & 1) == 1)
    6323              :             {
    6324         1275 :               t[j - 1] = expand_binop (mode, xor_optab, t[j - 1], t[j],
    6325              :                                        NULL_RTX, 1, olw);
    6326         1275 :               t[j] = NULL_RTX;
    6327              :             }
    6328         1275 :           if ((j & 3) == 3)
    6329              :             {
    6330          553 :               t[j - 3] = expand_binop (mode, xor_optab, t[j - 3], t[j - 1],
    6331              :                                        NULL_RTX, 1, olw);
    6332          553 :               t[j - 1] = NULL_RTX;
    6333              :             }
    6334              :         }
    6335         3006 :       for (unsigned int j = 1; j < s; ++j)
    6336         2284 :         if (t[j])
    6337          456 :           t[0] = expand_binop (mode, xor_optab, t[0], t[j], NULL_RTX, 1, olw);
    6338          722 :       if (mode != op_mode)
    6339          236 :         mask[i]
    6340          236 :           = immed_wide_int_const (wi::bit_repeat (wide_int::from (1 << i, s,
    6341              :                                                                   UNSIGNED),
    6342              :                                                   prec), mode);
    6343          722 :       t[0] = expand_binop (mode, and_optab, t[0], mask[i], NULL_RTX, 1, olw);
    6344          722 :       if (i == 0)
    6345              :         r = t[0];
    6346              :       else
    6347          535 :         r = expand_binop (mode, ior_optab, r, t[0], NULL_RTX, 1, olw);
    6348              :     }
    6349              :   return r;
    6350              : }
    6351              : 
    6352              : void
    6353          124 : expand_CLMUL (internal_fn, gcall *stmt)
    6354              : {
    6355          124 :   tree lhs = gimple_call_lhs (stmt);
    6356          124 :   if (lhs == NULL_TREE)
    6357          124 :     return;
    6358          123 :   tree arg0 = gimple_call_arg (stmt, 0);
    6359          123 :   tree arg1 = gimple_call_arg (stmt, 1);
    6360          123 :   scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
    6361          123 :   unsigned int prec0 = get_min_precision (arg0, UNSIGNED);
    6362          123 :   unsigned int prec1 = get_min_precision (arg1, UNSIGNED);
    6363          123 :   rtx op0 = expand_normal (arg0);
    6364          123 :   rtx op1 = expand_normal (arg1);
    6365          123 :   rtx res = expand_clmul (mode, mode, op0, op1, prec0, prec1,
    6366              :                           operand_equal_p (arg0, arg1, 0));
    6367          123 :   if (TYPE_PRECISION (TREE_TYPE (arg0)) < GET_MODE_PRECISION (mode))
    6368              :     {
    6369            5 :       rtx mask
    6370            5 :         = immed_wide_int_const (wi::mask (TYPE_PRECISION (TREE_TYPE (arg0)),
    6371              :                                           false,
    6372            5 :                                           GET_MODE_PRECISION (mode)), mode);
    6373            5 :       res = expand_and (mode, res, mask, NULL_RTX);
    6374              :     }
    6375          123 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    6376          123 :   if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
    6377            0 :     convert_move (SUBREG_REG (target), res, SUBREG_PROMOTED_SIGN (target));
    6378              :   else
    6379          123 :     emit_move_insn (target, res);
    6380              : }
    6381              : 
    6382              : void
    6383          103 : expand_CLMUL_WIDEN (internal_fn, gcall *stmt)
    6384              : {
    6385          103 :   tree lhs = gimple_call_lhs (stmt);
    6386          103 :   if (lhs == NULL_TREE)
    6387              :     return;
    6388          103 :   tree arg0 = gimple_call_arg (stmt, 0);
    6389          103 :   tree arg1 = gimple_call_arg (stmt, 1);
    6390          103 :   scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
    6391          103 :   unsigned int prec0 = get_min_precision (arg0, UNSIGNED);
    6392          103 :   unsigned int prec1 = get_min_precision (arg1, UNSIGNED);
    6393          103 :   unsigned int prec = GET_MODE_PRECISION (mode);
    6394          103 :   unsigned int type_prec = TYPE_PRECISION (TREE_TYPE (arg0));
    6395          103 :   rtx op0 = expand_normal (arg0);
    6396          103 :   rtx op1 = expand_normal (arg1);
    6397          103 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    6398          103 :   rtx bitint_mask = NULL_RTX;
    6399          103 :   bool op_eq = operand_equal_p (arg0, arg1, 0);
    6400          103 :   if (type_prec != prec)
    6401           16 :     bitint_mask
    6402           16 :       = immed_wide_int_const (wi::mask (type_prec, false, prec), mode);
    6403          103 :   if (prec0 + prec1 - 1 <= prec)
    6404              :     {
    6405              :       /* The whole product fits in MODE.  For a type narrower than MODE
    6406              :          the bits above its precision are the high half.  */
    6407            9 :       rtx reslo = expand_clmul (mode, mode, op0, op1, prec0, prec1, op_eq);
    6408            9 :       rtx reshi = const0_rtx;
    6409            9 :       if (prec0 + prec1 - 1 > type_prec)
    6410            9 :         reshi = expand_shift (RSHIFT_EXPR, mode, reslo, type_prec, NULL_RTX, 1);
    6411            9 :       if (bitint_mask)
    6412            9 :         reslo = expand_and (mode, reslo, bitint_mask, NULL_RTX);
    6413            9 :       write_complex_part (target, reslo, false, true);
    6414            9 :       write_complex_part (target, reshi, true, false);
    6415            9 :       return;
    6416              :     }
    6417           94 :   if (optab_handler (clmul_widen_optab, mode) != CODE_FOR_nothing
    6418           94 :       && type_prec == prec)
    6419              :     {
    6420            2 :       rtx_insn *start = get_last_insn ();
    6421            2 :       rtx reslo = gen_reg_rtx (mode);
    6422            2 :       rtx reshi = gen_reg_rtx (mode);
    6423            2 :       class expand_operand ops[4];
    6424            2 :       create_output_operand (&ops[0], reslo, mode);
    6425            2 :       create_output_operand (&ops[1], reshi, mode);
    6426            2 :       create_input_operand (&ops[2], op0, mode);
    6427            2 :       create_input_operand (&ops[3], op1, mode);
    6428            2 :       if (maybe_expand_insn (optab_handler (clmul_widen_optab, mode),
    6429              :                              4, ops))
    6430              :         {
    6431            2 :           write_complex_part (target, reslo, false, true);
    6432            2 :           write_complex_part (target, reshi, true, false);
    6433            2 :           return;
    6434              :         }
    6435            0 :       delete_insns_since (start);
    6436              :     }
    6437           92 :   scalar_int_mode wmode;
    6438           92 :   if (GET_MODE_2XWIDER_MODE (mode).exists (&wmode)
    6439           92 :       && targetm.scalar_mode_supported_p (wmode))
    6440              :     {
    6441           79 :       rtx res = expand_clmul (wmode, mode, op0, op1, prec0, prec1, op_eq);
    6442           79 :       rtx reslo = convert_modes (mode, wmode, res, 1);
    6443           79 :       if (bitint_mask)
    6444            7 :         reslo = expand_and (mode, reslo, bitint_mask, NULL_RTX);
    6445           79 :       write_complex_part (target, reslo, false, true);
    6446           79 :       rtx reshi = expand_shift (RSHIFT_EXPR, wmode, res, type_prec,
    6447              :                                 NULL_RTX, 1);
    6448           79 :       reshi = convert_modes (mode, wmode, reshi, 1);
    6449           79 :       write_complex_part (target, reshi, true, false);
    6450           79 :       return;
    6451              :     }
    6452              :   /* If we have native bitreverse in mode, fallback using:
    6453              :      uint64_t l = clmul (x, y);
    6454              :      uint64_t h = clmul (__builtin_bitreverseg (x),
    6455              :                          __builtin_bitreverseg (y));
    6456              :      return { l, __builtin_bitreverseg (h) >> 1 };  */
    6457           13 :   if (optab_handler (bitreverse_optab, mode) != CODE_FOR_nothing
    6458           13 :       || (prec == 2 * UNITS_PER_WORD
    6459            0 :           && optab_handler (bitreverse_optab, word_mode) != CODE_FOR_nothing))
    6460              :     {
    6461            0 :       rtx reslo = expand_clmul (mode, mode, op0, op1, prec0, prec1, op_eq);
    6462            0 :       if (bitint_mask)
    6463            0 :         reslo = expand_and (mode, reslo, bitint_mask, NULL_RTX);
    6464            0 :       write_complex_part (target, reslo, false, true);
    6465            0 :       rtx revx = expand_unop (mode, bitreverse_optab, op0, NULL_RTX, 1);
    6466            0 :       rtx revy = expand_unop (mode, bitreverse_optab, op1, NULL_RTX, 1);
    6467            0 :       rtx reshi = expand_clmul (mode, mode, revx, revy, prec, prec, op_eq);
    6468            0 :       reshi = expand_unop (mode, bitreverse_optab, reshi, NULL_RTX, 1);
    6469            0 :       if (bitint_mask)
    6470            0 :         reshi = expand_and (mode, reshi, bitint_mask, NULL_RTX);
    6471            0 :       reshi = expand_shift (RSHIFT_EXPR, mode, reshi, 1, NULL_RTX, 1);
    6472            0 :       write_complex_part (target, reshi, true, false);
    6473            0 :       return;
    6474              :     }
    6475              :   /* Fallback to modified Karatsuba multiplication step
    6476              :      using 3x expand_clmul in mode.
    6477              :      For 64x64 -> 128bit CLMUL this would be:
    6478              :      uint64_t x0 = (uint32_t) x, x1 = (x >> 32);
    6479              :      uint64_t y0 = (uint32_t) y, y1 = (y >> 32);
    6480              :      uint64_t l = clmul (x0, y0);
    6481              :      uint64_t m = clmul (x0 ^ x1, y0 ^ y1);
    6482              :      uint64_t h = clmul (x1, y1);
    6483              :      m ^= l ^ h;
    6484              :      return { l ^ (m << 32), h ^ (m >> 32) };  */
    6485           13 :   unsigned int hprec = prec / 2;
    6486           13 :   rtx mask = immed_wide_int_const (wi::mask (hprec, false, prec), mode);
    6487           13 :   optab_methods olw = OPTAB_LIB_WIDEN;
    6488           13 :   rtx x0 = expand_binop (mode, and_optab, op0, mask, NULL_RTX, 1, olw);
    6489           13 :   rtx x1 = expand_shift (RSHIFT_EXPR, mode, op0, hprec, NULL_RTX, 1);
    6490           13 :   rtx y0 = expand_binop (mode, and_optab, op1, mask, NULL_RTX, 1, olw);
    6491           13 :   rtx y1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec, NULL_RTX, 1);
    6492           13 :   rtx l = expand_clmul (mode, mode, x0, y0, hprec, hprec, op_eq);
    6493           13 :   rtx t0 = expand_binop (mode, xor_optab, x0, x1, NULL_RTX, 1, olw);
    6494           13 :   rtx t1 = expand_binop (mode, xor_optab, y0, y1, NULL_RTX, 1, olw);
    6495           13 :   rtx m = expand_clmul (mode, mode, t0, t1, hprec, hprec, op_eq);
    6496           13 :   rtx h;
    6497           13 :   if (prec0 <= hprec || prec1 <= hprec)
    6498            0 :     h = const0_rtx;
    6499              :   else
    6500           13 :     h = expand_clmul (mode, mode, x1, y1, MIN (hprec, prec0 - hprec),
    6501           13 :                       MIN (hprec, prec1 - hprec), op_eq);
    6502           13 :   t0 = expand_binop (mode, xor_optab, l, h, NULL_RTX, 1, olw);
    6503           13 :   m = expand_binop (mode, xor_optab, m, t0, NULL_RTX, 1, olw);
    6504           13 :   t0 = expand_shift (LSHIFT_EXPR, mode, m, hprec, NULL_RTX, 1);
    6505           13 :   t1 = expand_shift (RSHIFT_EXPR, mode, m, hprec, NULL_RTX, 1);
    6506           13 :   rtx reslo = expand_binop (mode, xor_optab, l, t0, NULL_RTX, 1, olw);
    6507           13 :   if (bitint_mask)
    6508              :     {
    6509            0 :       rtx reslom = expand_and (mode, reslo, bitint_mask, NULL_RTX);
    6510            0 :       write_complex_part (target, reslom, false, true);
    6511            0 :       rtx reshi = expand_binop (mode, xor_optab, h, t1, NULL_RTX, 1, olw);
    6512            0 :       reshi = expand_shift (LSHIFT_EXPR, mode, reshi,
    6513            0 :                             prec - type_prec, NULL_RTX, 1);
    6514            0 :       reslo = expand_shift (RSHIFT_EXPR, mode, reslo, type_prec, NULL_RTX, 1);
    6515            0 :       reshi = expand_binop (mode, ior_optab, reshi, reslo, NULL_RTX, 1, olw);
    6516            0 :       reshi = expand_and (mode, reshi, bitint_mask, NULL_RTX);
    6517            0 :       write_complex_part (target, reshi, true, false);
    6518              :     }
    6519              :   else
    6520              :     {
    6521           13 :       write_complex_part (target, reslo, false, true);
    6522           13 :       rtx reshi = expand_binop (mode, xor_optab, h, t1, NULL_RTX, 1, olw);
    6523           13 :       write_complex_part (target, reshi, true, false);
    6524              :     }
    6525              : }
    6526              : 
    6527              : void
    6528           42 : expand_BITCOMPRESS (internal_fn, gcall *stmt)
    6529              : {
    6530           42 :   tree lhs = gimple_call_lhs (stmt);
    6531           42 :   if (lhs == NULL_TREE)
    6532              :     return;
    6533           42 :   tree arg0 = gimple_call_arg (stmt, 0);
    6534           42 :   tree arg1 = gimple_call_arg (stmt, 1);
    6535           42 :   scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
    6536           42 :   unsigned int prec1 = get_min_precision (arg1, UNSIGNED);
    6537           42 :   rtx op0 = expand_normal (arg0);
    6538           42 :   rtx op1 = expand_normal (arg1);
    6539           42 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    6540           42 :   machine_mode cmode;
    6541           42 :   scalar_int_mode wmode = mode;
    6542           42 :   machine_mode clmul_mode = VOIDmode;
    6543           42 :   machine_mode half_mode = VOIDmode;
    6544           42 :   optab_methods olw = OPTAB_LIB_WIDEN;
    6545           42 :   rtx subtarget = target, tem = NULL_RTX;
    6546           42 :   if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
    6547           42 :     subtarget = NULL_RTX;
    6548           42 :   if (prec1 <= 1)
    6549              :     {
    6550              :       /* If mask operand has [0, 1] range, we can do just AND.  */
    6551            0 :       tem = expand_binop (mode, and_optab, op0, op1, subtarget, 1, olw);
    6552           42 :      do_return:
    6553           42 :       if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
    6554            0 :         convert_move (SUBREG_REG (target), tem, SUBREG_PROMOTED_SIGN (target));
    6555           42 :       else if (tem != target)
    6556           41 :         emit_move_insn (target, tem);
    6557              :       return;
    6558              :     }
    6559          290 :   FOR_EACH_MODE_IN_CLASS (cmode, MODE_INT)
    6560              :     {
    6561          258 :       unsigned int this_prec
    6562          258 :         = GET_MODE_PRECISION (as_a <scalar_int_mode> (cmode));
    6563          258 :       if (prec1 > this_prec)
    6564              :         {
    6565           91 :           if (this_prec * 2 >= prec1
    6566           35 :               && optab_handler (bitcompress_optab, cmode) != CODE_FOR_nothing
    6567           97 :               && optab_handler (popcount_optab, cmode) != CODE_FOR_nothing)
    6568              :             half_mode = cmode;
    6569           91 :           continue;
    6570              :         }
    6571          167 :       if (this_prec < GET_MODE_PRECISION (wmode))
    6572            0 :         wmode = as_a <scalar_int_mode> (cmode);
    6573          167 :       if (optab_handler (bitcompress_optab, cmode) != CODE_FOR_nothing)
    6574              :         {
    6575           10 :           rtx_insn *start = get_last_insn ();
    6576           10 :           rtx op0c = convert_modes (cmode, mode, op0, 1);
    6577           10 :           rtx op1c = convert_modes (cmode, mode, op1, 1);
    6578           10 :           if (rtx res = expand_binop (cmode, bitcompress_optab, op0c, op1c,
    6579              :                                       NULL_RTX, 1, OPTAB_DIRECT))
    6580              :             {
    6581           10 :               tem = convert_modes (mode, cmode, res, 1);
    6582           10 :               goto do_return;
    6583              :             }
    6584            0 :           delete_insns_since (start);
    6585              :         }
    6586          157 :       if (this_prec < GET_MODE_PRECISION (wmode))
    6587            0 :         wmode = as_a <scalar_int_mode> (cmode);
    6588          157 :       if (optab_handler (clmul_optab, cmode) != CODE_FOR_nothing
    6589          157 :           && (clmul_mode == VOIDmode
    6590            0 :               || (this_prec
    6591          157 :                   < GET_MODE_PRECISION (as_a <scalar_int_mode> (clmul_mode)))))
    6592              :         clmul_mode = cmode;
    6593              :     }
    6594              :   /* If there is hw support for bitcompress for at least half as wide mode,
    6595              :      use one step of a recursive algorithm.  H being the half type.
    6596              :      T
    6597              :      bitcompressg (T x, T m)
    6598              :      {
    6599              :        int hprec = __builtin_popcountg ((H) ~(H) 0);
    6600              :        H x0 = x, x1 = x >> hprec;
    6601              :        H m0 = m, m1 = m >> hprec;
    6602              :        return bitcompressg (x0, m0)
    6603              :               | (((T) bitcompressg (x1, m1)) << __builtin_popcountg (m0));
    6604              :      }  */
    6605           32 :   if (half_mode)
    6606              :     {
    6607            1 :       rtx_insn *start = get_last_insn ();
    6608            1 :       unsigned hprec = GET_MODE_PRECISION (as_a <scalar_int_mode> (half_mode));
    6609            1 :       rtx x0 = convert_modes (half_mode, mode, op0, 1);
    6610            1 :       rtx m0 = convert_modes (half_mode, mode, op1, 1);
    6611            1 :       rtx x1 = expand_shift (RSHIFT_EXPR, mode, op0, hprec, NULL_RTX, 1);
    6612            1 :       rtx m1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec, NULL_RTX, 1);
    6613            1 :       x1 = convert_modes (half_mode, mode, x1, 1);
    6614            1 :       m1 = convert_modes (half_mode, mode, m1, 1);
    6615            1 :       if (rtx res1 = expand_binop (half_mode, bitcompress_optab, x0, m0,
    6616              :                                    NULL_RTX, 1, OPTAB_DIRECT))
    6617            1 :         if (rtx res2 = expand_binop (half_mode, bitcompress_optab, x1, m1,
    6618              :                                      NULL_RTX, 1, OPTAB_DIRECT))
    6619            1 :           if (rtx res3 = expand_unop (half_mode, popcount_optab, m0,
    6620              :                                       NULL_RTX, 1))
    6621              :             {
    6622            1 :               res1 = convert_modes (mode, half_mode, res1, 1);
    6623            1 :               res2 = convert_modes (mode, half_mode, res2, 1);
    6624            1 :               res3 = convert_modes (TYPE_MODE (integer_type_node),
    6625            1 :                                     GET_MODE (res3), res3, 1);
    6626            1 :               res2 = expand_variable_shift (LSHIFT_EXPR, mode, res2,
    6627              :                                             make_tree (integer_type_node,
    6628              :                                                        res3), NULL_RTX, 1);
    6629            1 :               tem = expand_binop (mode, ior_optab, res1, res2, subtarget, 1,
    6630              :                                   olw);
    6631            1 :               goto do_return;
    6632              :             }
    6633            0 :       delete_insns_since (start);
    6634              :     }
    6635              :   /* Fallback based on "Compress, or Generalized Extract" in Hacker's Delight.
    6636              :      T
    6637              :      bitcompressg (T x, T m)
    6638              :      {
    6639              :        int prec = __builtin_popcountg ((T) ~(T) 0);
    6640              :        x &= m;
    6641              :        T mk = ~m << 1;
    6642              :        for (int i = 1; i < prec; i <<= 1)
    6643              :          {
    6644              :            T mp = mk;
    6645              :            for (int j = 1; j < prec; j <<= 1)
    6646              :              mp ^= mp << j;
    6647              :            // If there is __builtin_clmulg hw support for T,
    6648              :            // the above 3 lines can be done using
    6649              :            // T mp = __builtin_clmulg (mk, (T) -1);
    6650              :            // instead.
    6651              :            T mv = mp & m;
    6652              :            m = (m ^ mv) | (mv >> i);
    6653              :            T t = x & mv;
    6654              :            x = (x ^ t) | (t >> i);
    6655              :            mk = mk & ~mp;
    6656              :          }
    6657              :        return x;
    6658              :      }  */
    6659           31 :   op0 = convert_modes (wmode, mode, op0, 1);
    6660           31 :   op1 = convert_modes (wmode, mode, op1, 1);
    6661           31 :   rtx x = gen_reg_rtx (wmode);
    6662           31 :   tem = expand_binop (wmode, and_optab, op0, op1, x, 1, olw);
    6663           31 :   if (tem != x)
    6664            0 :     emit_move_insn (x, tem);
    6665           31 :   rtx m = gen_reg_rtx (wmode);
    6666           31 :   emit_move_insn (m, op1);
    6667           31 :   tem = expand_unop (wmode, one_cmpl_optab, op1, NULL_RTX, 1);
    6668           31 :   rtx mk = gen_reg_rtx (wmode);
    6669           31 :   tem = expand_shift (LSHIFT_EXPR, wmode, tem, 1, mk, 1);
    6670           31 :   if (tem != mk)
    6671            0 :     emit_move_insn (mk, tem);
    6672           31 :   rtx i = gen_reg_rtx (TYPE_MODE (integer_type_node));
    6673           31 :   emit_move_insn (i, const1_rtx);
    6674           31 :   rtx_code_label *loop1_label = gen_label_rtx ();
    6675           31 :   emit_label (loop1_label);
    6676           31 :   rtx mp = gen_reg_rtx (wmode);
    6677           31 :   rtx mpval = NULL_RTX;
    6678           31 :   if (clmul_mode != VOIDmode)
    6679              :     {
    6680            5 :       rtx_insn *start = get_last_insn ();
    6681            5 :       tem = convert_modes (clmul_mode, wmode, mk, 1);
    6682            5 :       rtx m1 = convert_modes (clmul_mode, wmode, constm1_rtx, 1);
    6683            5 :       if (rtx res = expand_binop (clmul_mode, clmul_optab, tem, m1,
    6684              :                                   NULL_RTX, 1, OPTAB_DIRECT))
    6685            5 :         mpval = convert_modes (wmode, clmul_mode, res, 1);
    6686              :       else
    6687            0 :         delete_insns_since (start);
    6688              :     }
    6689            5 :   if (mpval == NULL_RTX)
    6690              :     {
    6691           26 :       emit_move_insn (mp, mk);
    6692           26 :       rtx j = gen_reg_rtx (TYPE_MODE (integer_type_node));
    6693           26 :       emit_move_insn (j, const1_rtx);
    6694           26 :       rtx_code_label *loop2_label = gen_label_rtx ();
    6695           26 :       emit_label (loop2_label);
    6696           26 :       tem = expand_variable_shift (LSHIFT_EXPR, wmode, mp,
    6697              :                                    make_tree (integer_type_node, j),
    6698              :                                    NULL_RTX, 1);
    6699           26 :       tem = expand_binop (wmode, xor_optab, mp, tem, mp, 1, olw);
    6700           26 :       if (tem != mp)
    6701            0 :         emit_move_insn (mp, tem);
    6702           26 :       tem = expand_binop (GET_MODE (j), add_optab, j, j, j, 1, olw);
    6703           26 :       if (tem != j)
    6704            0 :         emit_move_insn (j, tem);
    6705           52 :       do_compare_rtx_and_jump (j, gen_int_mode (prec1, GET_MODE (j)),
    6706           26 :                                LTU, true, GET_MODE (j), NULL_RTX, NULL,
    6707              :                                loop2_label,
    6708              :                                profile_probability::very_likely ());
    6709              :     }
    6710              :   else
    6711            5 :     emit_move_insn (mp, mpval);
    6712           31 :   rtx mv = expand_binop (wmode, and_optab, mp, m, NULL_RTX, 1, olw);
    6713           31 :   tem = expand_binop (wmode, xor_optab, m, mv, NULL_RTX, 1, olw);
    6714           31 :   tree it = make_tree (integer_type_node, i);
    6715           31 :   rtx tem2 = expand_variable_shift (RSHIFT_EXPR, wmode, mv, it, NULL_RTX, 1);
    6716           31 :   tem = expand_binop (wmode, ior_optab, tem, tem2, m, 1, olw);
    6717           31 :   if (tem != m)
    6718            0 :     emit_move_insn (m, tem);
    6719           31 :   rtx t = expand_binop (wmode, and_optab, x, mv, NULL_RTX, 1, olw);
    6720           31 :   tem = expand_binop (wmode, xor_optab, x, t, NULL_RTX, 1, olw);
    6721           31 :   tem2 = expand_variable_shift (RSHIFT_EXPR, wmode, t, it, NULL_RTX, 1);
    6722           31 :   tem = expand_binop (wmode, ior_optab, tem, tem2, x, 1, olw);
    6723           31 :   if (tem != x)
    6724            0 :     emit_move_insn (x, tem);
    6725           31 :   tem = expand_unop (wmode, one_cmpl_optab, mp, NULL_RTX, 1);
    6726           31 :   tem = expand_binop (wmode, and_optab, mk, tem, mk, 1, olw);
    6727           31 :   if (tem != mk)
    6728            0 :     emit_move_insn (mk, tem);
    6729           31 :   tem = expand_binop (GET_MODE (i), add_optab, i, i, i, 1, olw);
    6730           31 :   if (tem != i)
    6731            0 :     emit_move_insn (i, tem);
    6732           62 :   do_compare_rtx_and_jump (i, gen_int_mode (prec1, GET_MODE (i)),
    6733           31 :                            LTU, true, GET_MODE (i), NULL_RTX, NULL,
    6734              :                            loop1_label, profile_probability::very_likely ());
    6735           31 :   tem = convert_modes (mode, wmode, x, 1);
    6736           31 :   goto do_return;
    6737              : }
    6738              : 
    6739              : void
    6740           42 : expand_BITEXPAND (internal_fn, gcall *stmt)
    6741              : {
    6742           42 :   tree lhs = gimple_call_lhs (stmt);
    6743           42 :   if (lhs == NULL_TREE)
    6744              :     return;
    6745           42 :   tree arg0 = gimple_call_arg (stmt, 0);
    6746           42 :   tree arg1 = gimple_call_arg (stmt, 1);
    6747           42 :   scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
    6748           42 :   unsigned int prec1 = get_min_precision (arg1, UNSIGNED);
    6749           42 :   rtx op0 = expand_normal (arg0);
    6750           42 :   rtx op1 = expand_normal (arg1);
    6751           42 :   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
    6752           42 :   machine_mode cmode;
    6753           42 :   scalar_int_mode wmode = mode;
    6754           42 :   machine_mode clmul_mode = VOIDmode;
    6755           42 :   machine_mode half_mode = VOIDmode;
    6756           42 :   optab_methods olw = OPTAB_LIB_WIDEN;
    6757           42 :   rtx subtarget = target, tem = NULL_RTX;
    6758           42 :   if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
    6759           42 :     subtarget = NULL_RTX;
    6760           42 :   if (prec1 <= 1)
    6761              :     {
    6762              :       /* If mask operand has [0, 1] range, we can do just AND.  */
    6763            0 :       tem = expand_binop (mode, and_optab, op0, op1, subtarget, 1, olw);
    6764           42 :      do_return:
    6765           42 :       if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
    6766            0 :         convert_move (SUBREG_REG (target), tem, SUBREG_PROMOTED_SIGN (target));
    6767           42 :       else if (tem != target)
    6768           41 :         emit_move_insn (target, tem);
    6769              :       return;
    6770              :     }
    6771          290 :   FOR_EACH_MODE_IN_CLASS (cmode, MODE_INT)
    6772              :     {
    6773          258 :       unsigned int this_prec
    6774          258 :         = GET_MODE_PRECISION (as_a <scalar_int_mode> (cmode));
    6775          258 :       if (prec1 > this_prec)
    6776              :         {
    6777           91 :           if (this_prec * 2 >= prec1
    6778           35 :               && optab_handler (bitexpand_optab, cmode) != CODE_FOR_nothing
    6779           97 :               && optab_handler (popcount_optab, cmode) != CODE_FOR_nothing)
    6780              :             half_mode = cmode;
    6781           91 :           continue;
    6782              :         }
    6783          167 :       if (this_prec < GET_MODE_PRECISION (wmode))
    6784            0 :         wmode = as_a <scalar_int_mode> (cmode);
    6785          167 :       if (optab_handler (bitexpand_optab, cmode) != CODE_FOR_nothing)
    6786              :         {
    6787           10 :           rtx_insn *start = get_last_insn ();
    6788           10 :           rtx op0c = convert_modes (cmode, mode, op0, 1);
    6789           10 :           rtx op1c = convert_modes (cmode, mode, op1, 1);
    6790           10 :           if (rtx res = expand_binop (cmode, bitexpand_optab, op0c, op1c,
    6791              :                                       NULL_RTX, 1, OPTAB_DIRECT))
    6792              :             {
    6793           10 :               tem = convert_modes (mode, cmode, res, 1);
    6794           10 :               goto do_return;
    6795              :             }
    6796            0 :           delete_insns_since (start);
    6797              :         }
    6798          157 :       if (this_prec < GET_MODE_PRECISION (wmode))
    6799            0 :         wmode = as_a <scalar_int_mode> (cmode);
    6800          157 :       if (optab_handler (clmul_optab, cmode) != CODE_FOR_nothing
    6801          157 :           && (clmul_mode == VOIDmode
    6802            0 :               || (this_prec
    6803          157 :                   < GET_MODE_PRECISION (as_a <scalar_int_mode> (clmul_mode)))))
    6804              :         clmul_mode = cmode;
    6805              :     }
    6806              :   /* If there is hw support for bitexpand for at least half as wide mode,
    6807              :      use one step of a recursive algorithm.  H being the half type.
    6808              :      T
    6809              :      bitexpandg (T x, T m)
    6810              :      {
    6811              :        int hprec = __builtin_popcountg ((H) ~(H) 0);
    6812              :        H m0 = m, m1 = m >> hprec;
    6813              :        int k = __builtin_popcountg (m0);
    6814              :        H x0 = x, x1 = x >> k;
    6815              :        return bitexpandg (x0, m0)
    6816              :               | (((T) bitexpandg (x1, m1)) << hprec);
    6817              :      }  */
    6818           32 :   if (half_mode)
    6819              :     {
    6820            1 :       rtx_insn *start = get_last_insn ();
    6821            1 :       unsigned hprec = GET_MODE_PRECISION (as_a <scalar_int_mode> (half_mode));
    6822            1 :       rtx x0 = convert_modes (half_mode, mode, op0, 1);
    6823            1 :       rtx m0 = convert_modes (half_mode, mode, op1, 1);
    6824            1 :       if (rtx res3 = expand_unop (half_mode, popcount_optab, m0, NULL_RTX, 1))
    6825              :         {
    6826            1 :           res3 = convert_modes (TYPE_MODE (integer_type_node),
    6827            1 :                                 GET_MODE (res3), res3, 1);
    6828            1 :           rtx x1 = expand_variable_shift (RSHIFT_EXPR, mode, op0,
    6829              :                                           make_tree (integer_type_node,
    6830              :                                                      res3), NULL_RTX, 1);
    6831            1 :           rtx m1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec, NULL_RTX, 1);
    6832            1 :           x1 = convert_modes (half_mode, mode, x1, 1);
    6833            1 :           m1 = convert_modes (half_mode, mode, m1, 1);
    6834            1 :           if (rtx res1 = expand_binop (half_mode, bitexpand_optab, x0, m0,
    6835              :                                        NULL_RTX, 1, OPTAB_DIRECT))
    6836            1 :             if (rtx res2 = expand_binop (half_mode, bitexpand_optab, x1, m1,
    6837              :                                          NULL_RTX, 1, OPTAB_DIRECT))
    6838              :               {
    6839            1 :                 res1 = convert_modes (mode, half_mode, res1, 1);
    6840            1 :                 res2 = convert_modes (mode, half_mode, res2, 1);
    6841            1 :                 res2 = expand_shift (LSHIFT_EXPR, mode, res2, hprec,
    6842              :                                      NULL_RTX, 1);
    6843            1 :                 tem = expand_binop (mode, ior_optab, res1, res2, subtarget, 1,
    6844              :                                     olw);
    6845            1 :                 goto do_return;
    6846              :               }
    6847              :         }
    6848            0 :       delete_insns_since (start);
    6849              :     }
    6850              :   /* Fallback based on "Expand, or Generalized Insert" in Hacker's Delight.
    6851              :      T
    6852              :      bitexpandg (T x, T m)
    6853              :      {
    6854              :        int prec = __builtin_popcountg ((T) ~(T) 0);
    6855              :        int log2prec = ceil_log2 (prec);
    6856              :        T m0 = m;
    6857              :        T mk = ~m << 1;
    6858              :        T array[log2prec];
    6859              :        for (int i = 0; i < log2prec; i++)
    6860              :          {
    6861              :            T mp = mk;
    6862              :            for (int j = 1; j < prec; j <<= 1)
    6863              :              mp ^= mp << j;
    6864              :            T mv = mp & m;
    6865              :            array[i] = mv;
    6866              :            m = (m ^ mv) | (mv >> (1 << i));
    6867              :            mk = mk & ~mp;
    6868              :          }
    6869              :        for (int i = log2prec - 1; i >= 0; i--)
    6870              :          {
    6871              :            T mv = array[i];
    6872              :            T t = x << (((T) 1) << i);
    6873              :            x = (x & ~mv) | (t & mv);
    6874              :          }
    6875              :        return x & m0;
    6876              :      }  */
    6877           31 :   op0 = convert_modes (wmode, mode, op0, 1);
    6878           31 :   op1 = convert_modes (wmode, mode, op1, 1);
    6879           31 :   rtx x = gen_reg_rtx (wmode);
    6880           31 :   emit_move_insn (x, op0);
    6881           31 :   rtx m = gen_reg_rtx (wmode);
    6882           31 :   emit_move_insn (m, op1);
    6883           31 :   rtx m0 = op1;
    6884           31 :   tem = expand_unop (wmode, one_cmpl_optab, op1, NULL_RTX, 1);
    6885           31 :   rtx mk = gen_reg_rtx (wmode);
    6886           31 :   tem = expand_shift (LSHIFT_EXPR, wmode, tem, 1, mk, 1);
    6887           31 :   if (tem != mk)
    6888            0 :     emit_move_insn (mk, tem);
    6889           31 :   unsigned log2_prec1 = ceil_log2 (prec1);
    6890           31 :   tree wtype = lang_hooks.types.type_for_mode (wmode, 1);
    6891           31 :   tree atype = build_array_type_nelts (wtype, log2_prec1);
    6892           31 :   rtx array = assign_stack_temp_for_type (TYPE_MODE (atype),
    6893           31 :                                           int_size_in_bytes (atype), atype);
    6894           31 :   rtx array_addr = force_reg (GET_MODE (XEXP (array, 0)), XEXP (array, 0));
    6895           31 :   rtx i = gen_reg_rtx (TYPE_MODE (integer_type_node));
    6896           31 :   emit_move_insn (i, const0_rtx);
    6897           31 :   rtx i2 = gen_reg_rtx (TYPE_MODE (integer_type_node));
    6898           31 :   emit_move_insn (i2, const1_rtx);
    6899           31 :   rtx_code_label *loop1_label = gen_label_rtx ();
    6900           31 :   emit_label (loop1_label);
    6901           31 :   rtx mp = gen_reg_rtx (wmode);
    6902           31 :   rtx mpval = NULL_RTX;
    6903           31 :   if (clmul_mode != VOIDmode)
    6904              :     {
    6905            5 :       rtx_insn *start = get_last_insn ();
    6906            5 :       tem = convert_modes (clmul_mode, wmode, mk, 1);
    6907            5 :       rtx m1 = convert_modes (clmul_mode, wmode, constm1_rtx, 1);
    6908            5 :       if (rtx res = expand_binop (clmul_mode, clmul_optab, tem, m1,
    6909              :                                   NULL_RTX, 1, OPTAB_DIRECT))
    6910            5 :         mpval = convert_modes (wmode, clmul_mode, res, 1);
    6911              :       else
    6912            0 :         delete_insns_since (start);
    6913              :     }
    6914            5 :   if (mpval == NULL_RTX)
    6915              :     {
    6916           26 :       emit_move_insn (mp, mk);
    6917           26 :       rtx j = gen_reg_rtx (TYPE_MODE (integer_type_node));
    6918           26 :       emit_move_insn (j, const1_rtx);
    6919           26 :       rtx_code_label *loop2_label = gen_label_rtx ();
    6920           26 :       emit_label (loop2_label);
    6921           26 :       tem = expand_variable_shift (LSHIFT_EXPR, wmode, mp,
    6922              :                                    make_tree (integer_type_node, j),
    6923              :                                    NULL_RTX, 1);
    6924           26 :       tem = expand_binop (wmode, xor_optab, mp, tem, mp, 1, olw);
    6925           26 :       if (tem != mp)
    6926            0 :         emit_move_insn (mp, tem);
    6927           26 :       tem = expand_binop (GET_MODE (j), add_optab, j, j, j, 1, olw);
    6928           26 :       if (tem != j)
    6929            0 :         emit_move_insn (j, tem);
    6930           52 :       do_compare_rtx_and_jump (j, gen_int_mode (prec1, GET_MODE (j)),
    6931           26 :                                LTU, true, GET_MODE (j), NULL_RTX, NULL,
    6932              :                                loop2_label,
    6933              :                                profile_probability::very_likely ());
    6934              :     }
    6935              :   else
    6936            5 :     emit_move_insn (mp, mpval);
    6937           31 :   rtx mv = expand_binop (wmode, and_optab, mp, m, NULL_RTX, 1, olw);
    6938           31 :   tem = convert_modes (GET_MODE (array_addr), GET_MODE (i), i, 1);
    6939           62 :   if (GET_MODE_SIZE (wmode) != 1)
    6940           52 :     tem = expand_mult (GET_MODE (array_addr), tem,
    6941           26 :                        GEN_INT (GET_MODE_SIZE (wmode)),
    6942              :                        NULL_RTX, 1, false);
    6943           31 :   tem = expand_binop (GET_MODE (array_addr), add_optab, array_addr, tem,
    6944              :                       NULL_RTX, 1, olw);
    6945           31 :   tem = change_address (array, wmode, tem);
    6946           31 :   emit_move_insn (tem, mv);
    6947           31 :   tem = expand_binop (wmode, xor_optab, m, mv, NULL_RTX, 1, olw);
    6948           31 :   tree i2t = make_tree (integer_type_node, i2);
    6949           31 :   rtx tem2 = expand_variable_shift (RSHIFT_EXPR, wmode, mv, i2t, NULL_RTX, 1);
    6950           31 :   tem = expand_binop (wmode, ior_optab, tem, tem2, m, 1, olw);
    6951           31 :   if (tem != m)
    6952            0 :     emit_move_insn (m, tem);
    6953           31 :   tem = expand_unop (wmode, one_cmpl_optab, mp, NULL_RTX, 1);
    6954           31 :   tem = expand_binop (wmode, and_optab, mk, tem, mk, 1, olw);
    6955           31 :   if (tem != mk)
    6956            0 :     emit_move_insn (mk, tem);
    6957           31 :   tem = expand_binop (GET_MODE (i), add_optab, i, const1_rtx, i, 1, olw);
    6958           31 :   if (tem != i)
    6959            0 :     emit_move_insn (i, tem);
    6960           31 :   tem = expand_binop (GET_MODE (i2), add_optab, i2, i2, i2, 1, olw);
    6961           31 :   if (tem != i2)
    6962            0 :     emit_move_insn (i2, tem);
    6963           62 :   do_compare_rtx_and_jump (i, gen_int_mode (log2_prec1, GET_MODE (i)),
    6964           31 :                            LTU, true, GET_MODE (i), NULL_RTX, NULL,
    6965              :                            loop1_label, profile_probability::very_likely ());
    6966           31 :   tem = expand_binop (GET_MODE (i), add_optab, i, constm1_rtx, i, 1, olw);
    6967           31 :   if (tem != i)
    6968            0 :     emit_move_insn (i, tem);
    6969           31 :   tem = expand_shift (RSHIFT_EXPR, GET_MODE (i2), i2, 1, i2, 1);
    6970           31 :   if (tem != i2)
    6971            0 :     emit_move_insn (i2, tem);
    6972           31 :   rtx_code_label *loop3_label = gen_label_rtx ();
    6973           31 :   emit_label (loop3_label);
    6974           31 :   tem = convert_modes (GET_MODE (array_addr), GET_MODE (i), i, 1);
    6975           62 :   if (GET_MODE_SIZE (wmode) != 1)
    6976           52 :     tem = expand_mult (GET_MODE (array_addr), tem,
    6977           26 :                        GEN_INT (GET_MODE_SIZE (wmode)),
    6978              :                        NULL_RTX, 1, false);
    6979           31 :   tem = expand_binop (GET_MODE (array_addr), add_optab, array_addr, tem,
    6980              :                       NULL_RTX, 1, olw);
    6981           31 :   tem = change_address (array, wmode, tem);
    6982           31 :   mv = force_reg (wmode, tem);
    6983           31 :   rtx t = expand_variable_shift (LSHIFT_EXPR, wmode, x, i2t, NULL_RTX, 1);
    6984           31 :   tem = expand_unop (wmode, one_cmpl_optab, mv, NULL_RTX, 1);
    6985           31 :   tem = expand_binop (wmode, and_optab, x, tem, NULL_RTX, 1, olw);
    6986           31 :   tem2 = expand_binop (wmode, and_optab, t, mv, NULL_RTX, 1, olw);
    6987           31 :   tem = expand_binop (wmode, ior_optab, tem, tem2, x, 1, olw);
    6988           31 :   if (tem != x)
    6989            0 :     emit_move_insn (x, tem);
    6990           31 :   tem = expand_binop (GET_MODE (i), add_optab, i, constm1_rtx, i, 1, olw);
    6991           31 :   if (tem != i)
    6992            0 :     emit_move_insn (i, tem);
    6993           31 :   tem = expand_shift (RSHIFT_EXPR, GET_MODE (i2), i2, 1, i2, 1);
    6994           31 :   if (tem != i2)
    6995            0 :     emit_move_insn (i2, tem);
    6996           31 :   do_compare_rtx_and_jump (i, const0_rtx, GE, false, GET_MODE (i), NULL_RTX,
    6997              :                            NULL, loop3_label,
    6998              :                            profile_probability::very_likely ());
    6999           31 :   tem = expand_binop (wmode, and_optab, x, m0, NULL_RTX, 1, olw);
    7000           31 :   tem = convert_modes (mode, wmode, tem, 1);
    7001           31 :   goto do_return;
    7002              : }
        

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.