LCOV - code coverage report
Current view: top level - gcc/config/i386 - i386-features.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 88.9 % 2847 2531
Test Date: 2026-12-05 16:10:02 Functions: 98.9 % 95 94
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Copyright (C) 1988-2026 Free Software Foundation, Inc.
       2              : 
       3              : This file is part of GCC.
       4              : 
       5              : GCC is free software; you can redistribute it and/or modify
       6              : it under the terms of the GNU General Public License as published by
       7              : the Free Software Foundation; either version 3, or (at your option)
       8              : any later version.
       9              : 
      10              : GCC is distributed in the hope that it will be useful,
      11              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      12              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13              : GNU General Public License for more details.
      14              : 
      15              : You should have received a copy of the GNU General Public License
      16              : along with GCC; see the file COPYING3.  If not see
      17              : <http://www.gnu.org/licenses/>.  */
      18              : 
      19              : #define IN_TARGET_CODE 1
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "backend.h"
      25              : #include "rtl.h"
      26              : #include "tree.h"
      27              : #include "memmodel.h"
      28              : #include "gimple.h"
      29              : #include "cfghooks.h"
      30              : #include "cfgloop.h"
      31              : #include "df.h"
      32              : #include "tm_p.h"
      33              : #include "stringpool.h"
      34              : #include "expmed.h"
      35              : #include "optabs.h"
      36              : #include "regs.h"
      37              : #include "emit-rtl.h"
      38              : #include "recog.h"
      39              : #include "cgraph.h"
      40              : #include "diagnostic.h"
      41              : #include "cfgbuild.h"
      42              : #include "alias.h"
      43              : #include "fold-const.h"
      44              : #include "attribs.h"
      45              : #include "calls.h"
      46              : #include "stor-layout.h"
      47              : #include "varasm.h"
      48              : #include "output.h"
      49              : #include "insn-attr.h"
      50              : #include "flags.h"
      51              : #include "except.h"
      52              : #include "explow.h"
      53              : #include "expr.h"
      54              : #include "cfgrtl.h"
      55              : #include "common/common-target.h"
      56              : #include "langhooks.h"
      57              : #include "reload.h"
      58              : #include "gimplify.h"
      59              : #include "dwarf2.h"
      60              : #include "tm-constrs.h"
      61              : #include "cselib.h"
      62              : #include "sched-int.h"
      63              : #include "opts.h"
      64              : #include "tree-pass.h"
      65              : #include "context.h"
      66              : #include "pass_manager.h"
      67              : #include "target-globals.h"
      68              : #include "gimple-iterator.h"
      69              : #include "shrink-wrap.h"
      70              : #include "builtins.h"
      71              : #include "rtl-iter.h"
      72              : #include "tree-iterator.h"
      73              : #include "dbgcnt.h"
      74              : #include "case-cfn-macros.h"
      75              : #include "dojump.h"
      76              : #include "fold-const-call.h"
      77              : #include "tree-vrp.h"
      78              : #include "tree-ssanames.h"
      79              : #include "selftest.h"
      80              : #include "selftest-rtl.h"
      81              : #include "print-rtl.h"
      82              : #include "intl.h"
      83              : #include "ifcvt.h"
      84              : #include "symbol-summary.h"
      85              : #include "sreal.h"
      86              : #include "ipa-cp.h"
      87              : #include "ipa-prop.h"
      88              : #include "ipa-fnsummary.h"
      89              : #include "wide-int-bitmask.h"
      90              : #include "tree-vector-builder.h"
      91              : #include "debug.h"
      92              : #include "dwarf2out.h"
      93              : #include "function-abi.h"
      94              : #include "i386-builtins.h"
      95              : #include "i386-features.h"
      96              : #include "i386-expand.h"
      97              : 
      98              : const char * const xlogue_layout::STUB_BASE_NAMES[XLOGUE_STUB_COUNT] = {
      99              :   "savms64",
     100              :   "resms64",
     101              :   "resms64x",
     102              :   "savms64f",
     103              :   "resms64f",
     104              :   "resms64fx"
     105              : };
     106              : 
     107              : const unsigned xlogue_layout::REG_ORDER[xlogue_layout::MAX_REGS] = {
     108              : /* The below offset values are where each register is stored for the layout
     109              :    relative to incoming stack pointer.  The value of each m_regs[].offset will
     110              :    be relative to the incoming base pointer (rax or rsi) used by the stub.
     111              : 
     112              :     s_instances:   0            1               2               3
     113              :     Offset:                                     realigned or    aligned + 8
     114              :     Register       aligned      aligned + 8     aligned w/HFP   w/HFP   */
     115              :     XMM15_REG,  /* 0x10         0x18            0x10            0x18    */
     116              :     XMM14_REG,  /* 0x20         0x28            0x20            0x28    */
     117              :     XMM13_REG,  /* 0x30         0x38            0x30            0x38    */
     118              :     XMM12_REG,  /* 0x40         0x48            0x40            0x48    */
     119              :     XMM11_REG,  /* 0x50         0x58            0x50            0x58    */
     120              :     XMM10_REG,  /* 0x60         0x68            0x60            0x68    */
     121              :     XMM9_REG,   /* 0x70         0x78            0x70            0x78    */
     122              :     XMM8_REG,   /* 0x80         0x88            0x80            0x88    */
     123              :     XMM7_REG,   /* 0x90         0x98            0x90            0x98    */
     124              :     XMM6_REG,   /* 0xa0         0xa8            0xa0            0xa8    */
     125              :     SI_REG,     /* 0xa8         0xb0            0xa8            0xb0    */
     126              :     DI_REG,     /* 0xb0         0xb8            0xb0            0xb8    */
     127              :     BX_REG,     /* 0xb8         0xc0            0xb8            0xc0    */
     128              :     BP_REG,     /* 0xc0         0xc8            N/A             N/A     */
     129              :     R12_REG,    /* 0xc8         0xd0            0xc0            0xc8    */
     130              :     R13_REG,    /* 0xd0         0xd8            0xc8            0xd0    */
     131              :     R14_REG,    /* 0xd8         0xe0            0xd0            0xd8    */
     132              :     R15_REG,    /* 0xe0         0xe8            0xd8            0xe0    */
     133              : };
     134              : 
     135              : /* Instantiate static const values.  */
     136              : const HOST_WIDE_INT xlogue_layout::STUB_INDEX_OFFSET;
     137              : const unsigned xlogue_layout::MIN_REGS;
     138              : const unsigned xlogue_layout::MAX_REGS;
     139              : const unsigned xlogue_layout::MAX_EXTRA_REGS;
     140              : const unsigned xlogue_layout::VARIANT_COUNT;
     141              : const unsigned xlogue_layout::STUB_NAME_MAX_LEN;
     142              : 
     143              : /* Initialize xlogue_layout::s_stub_names to zero.  */
     144              : char xlogue_layout::s_stub_names[2][XLOGUE_STUB_COUNT][VARIANT_COUNT]
     145              :                                 [STUB_NAME_MAX_LEN];
     146              : 
     147              : /* Instantiates all xlogue_layout instances.  */
     148              : const xlogue_layout xlogue_layout::s_instances[XLOGUE_SET_COUNT] = {
     149              :   xlogue_layout (0, false),
     150              :   xlogue_layout (8, false),
     151              :   xlogue_layout (0, true),
     152              :   xlogue_layout (8, true)
     153              : };
     154              : 
     155              : /* Return an appropriate const instance of xlogue_layout based upon values
     156              :    in cfun->machine and crtl.  */
     157              : const class xlogue_layout &
     158        49891 : xlogue_layout::get_instance ()
     159              : {
     160        49891 :   enum xlogue_stub_sets stub_set;
     161        49891 :   bool aligned_plus_8 = cfun->machine->call_ms2sysv_pad_in;
     162              : 
     163        49891 :   if (stack_realign_fp)
     164              :     stub_set = XLOGUE_SET_HFP_ALIGNED_OR_REALIGN;
     165        40910 :   else if (frame_pointer_needed)
     166        25246 :     stub_set = aligned_plus_8
     167        31552 :               ? XLOGUE_SET_HFP_ALIGNED_PLUS_8
     168              :               : XLOGUE_SET_HFP_ALIGNED_OR_REALIGN;
     169              :   else
     170         9358 :     stub_set = aligned_plus_8 ? XLOGUE_SET_ALIGNED_PLUS_8 : XLOGUE_SET_ALIGNED;
     171              : 
     172        49891 :   return s_instances[stub_set];
     173              : }
     174              : 
     175              : /* Determine how many clobbered registers can be saved by the stub.
     176              :    Returns the count of registers the stub will save and restore.  */
     177              : unsigned
     178        35225 : xlogue_layout::count_stub_managed_regs ()
     179              : {
     180        35225 :   bool hfp = frame_pointer_needed || stack_realign_fp;
     181        35225 :   unsigned i, count;
     182        35225 :   unsigned regno;
     183              : 
     184        93950 :   for (count = i = MIN_REGS; i < MAX_REGS; ++i)
     185              :     {
     186        92750 :       regno = REG_ORDER[i];
     187        92750 :       if (regno == BP_REG && hfp)
     188        17880 :         continue;
     189        74870 :       if (!ix86_save_reg (regno, false, false))
     190              :         break;
     191        40845 :       ++count;
     192              :     }
     193        35225 :   return count;
     194              : }
     195              : 
     196              : /* Determine if register REGNO is a stub managed register given the
     197              :    total COUNT of stub managed registers.  */
     198              : bool
     199      2642240 : xlogue_layout::is_stub_managed_reg (unsigned regno, unsigned count)
     200              : {
     201      2642240 :   bool hfp = frame_pointer_needed || stack_realign_fp;
     202      2642240 :   unsigned i;
     203              : 
     204     34418160 :   for (i = 0; i < count; ++i)
     205              :     {
     206     32275013 :       gcc_assert (i < MAX_REGS);
     207     32275013 :       if (REG_ORDER[i] == BP_REG && hfp)
     208       510254 :         ++count;
     209     31764759 :       else if (REG_ORDER[i] == regno)
     210              :         return true;
     211              :     }
     212              :   return false;
     213              : }
     214              : 
     215              : /* Constructor for xlogue_layout.  */
     216      1207232 : xlogue_layout::xlogue_layout (HOST_WIDE_INT stack_align_off_in, bool hfp)
     217      1207232 :   : m_hfp (hfp) , m_nregs (hfp ? 17 : 18),
     218      1207232 :     m_stack_align_off_in (stack_align_off_in)
     219              : {
     220      1207232 :   HOST_WIDE_INT offset = stack_align_off_in;
     221      1207232 :   unsigned i, j;
     222              : 
     223     22937408 :   for (i = j = 0; i < MAX_REGS; ++i)
     224              :     {
     225     21730176 :       unsigned regno = REG_ORDER[i];
     226              : 
     227     21730176 :       if (regno == BP_REG && hfp)
     228       603616 :         continue;
     229     21126560 :       if (SSE_REGNO_P (regno))
     230              :         {
     231     12072320 :           offset += 16;
     232              :           /* Verify that SSE regs are always aligned.  */
     233     12072320 :           gcc_assert (!((stack_align_off_in + offset) & 15));
     234              :         }
     235              :       else
     236      9054240 :         offset += 8;
     237              : 
     238     21126560 :       m_regs[j].regno    = regno;
     239     21126560 :       m_regs[j++].offset = offset - STUB_INDEX_OFFSET;
     240              :     }
     241      1207232 :   gcc_assert (j == m_nregs);
     242      1207232 : }
     243              : 
     244              : const char *
     245        14666 : xlogue_layout::get_stub_name (enum xlogue_stub stub,
     246              :                               unsigned n_extra_regs)
     247              : {
     248        14666 :   const int have_avx = TARGET_AVX;
     249        14666 :   char *name = s_stub_names[!!have_avx][stub][n_extra_regs];
     250              : 
     251              :   /* Lazy init */
     252        14666 :   if (!*name)
     253              :     {
     254          362 :       int res = snprintf (name, STUB_NAME_MAX_LEN, "__%s_%s_%u",
     255              :                           (have_avx ? "avx" : "sse"),
     256          181 :                           STUB_BASE_NAMES[stub],
     257              :                           MIN_REGS + n_extra_regs);
     258          181 :       gcc_checking_assert (res < (int)STUB_NAME_MAX_LEN);
     259              :     }
     260              : 
     261        14666 :   return name;
     262              : }
     263              : 
     264              : /* Return rtx of a symbol ref for the entry point (based upon
     265              :    cfun->machine->call_ms2sysv_extra_regs) of the specified stub.  */
     266              : rtx
     267        14666 : xlogue_layout::get_stub_rtx (enum xlogue_stub stub)
     268              : {
     269        14666 :   const unsigned n_extra_regs = cfun->machine->call_ms2sysv_extra_regs;
     270        14666 :   gcc_checking_assert (n_extra_regs <= MAX_EXTRA_REGS);
     271        14666 :   gcc_assert (stub < XLOGUE_STUB_COUNT);
     272        14666 :   gcc_assert (crtl->stack_realign_finalized);
     273              : 
     274        14666 :   return gen_rtx_SYMBOL_REF (Pmode, get_stub_name (stub, n_extra_regs));
     275              : }
     276              : 
     277              : namespace {
     278              : 
     279              : class scalar_chain
     280              : {
     281              :  public:
     282              :   scalar_chain (enum machine_mode, enum machine_mode);
     283              :   virtual ~scalar_chain ();
     284              : 
     285              :   static unsigned max_id;
     286              : 
     287              :   /* Scalar mode.  */
     288              :   enum machine_mode smode;
     289              :   /* Vector mode.  */
     290              :   enum machine_mode vmode;
     291              : 
     292              :   /* ID of a chain.  */
     293              :   unsigned int chain_id;
     294              :   /* A queue of instructions to be included into a chain.  */
     295              :   bitmap queue;
     296              :   /* Instructions included into a chain.  */
     297              :   bitmap insns;
     298              :   /* All registers defined by a chain.  */
     299              :   bitmap defs;
     300              :   /* Registers used in both vector and scalar modes.  */
     301              :   bitmap defs_conv;
     302              : 
     303              :   /* Limit on chain discovery.  */
     304              :   unsigned max_visits;
     305              : 
     306              :   bitmap insns_conv;
     307              :   hash_map<rtx, rtx> defs_map;
     308              :   /* Cost of inserted conversion between ineteger and sse.  */
     309              :   int cost_sse_integer;
     310              :   sreal weighted_cost_sse_integer;
     311              :   auto_vec<rtx_insn *> control_flow_insns;
     312              : 
     313              :   bool build (bitmap candidates, unsigned insn_uid, bitmap disallowed);
     314              :   virtual bool compute_convert_gain () = 0;
     315              :   int convert ();
     316              : 
     317              :  protected:
     318              :   void add_to_queue (unsigned insn_uid);
     319              :   void emit_conversion_insns (rtx_insn *insns, rtx_insn *pos);
     320              :   rtx convert_compare (rtx op1, rtx op2, rtx_insn *insn);
     321              :   void mark_dual_mode_def (df_ref def);
     322              :   void convert_reg (rtx_insn *insn, rtx dst, rtx src);
     323              :   void convert_insn_common (rtx_insn *insn);
     324              :   void make_vector_copies (rtx_insn *, rtx);
     325              :   void convert_registers ();
     326              :   void convert_op (rtx *op, rtx_insn *insn);
     327              : 
     328              :  private:
     329              :   bool add_insn (bitmap candidates, unsigned insn_uid, bitmap disallowed);
     330              :   bool analyze_register_chain (bitmap candidates, df_ref ref,
     331              :                                bitmap disallowed);
     332              :   virtual void convert_insn (rtx_insn *insn) = 0;
     333              : };
     334              : 
     335              : class general_scalar_chain : public scalar_chain
     336              : {
     337              :  public:
     338      6142539 :   general_scalar_chain (enum machine_mode smode_, enum machine_mode vmode_)
     339     12285078 :     : scalar_chain (smode_, vmode_) {}
     340              :   bool compute_convert_gain () final override;
     341              : 
     342              :  private:
     343              :   void convert_insn (rtx_insn *insn) final override;
     344              :   int vector_const_cost (rtx exp, basic_block bb);
     345              :   rtx convert_rotate (enum rtx_code, rtx op0, rtx op1, rtx_insn *insn);
     346              : };
     347              : 
     348              : class timode_scalar_chain : public scalar_chain
     349              : {
     350              :  public:
     351      1063532 :   timode_scalar_chain () : scalar_chain (TImode, V1TImode) {}
     352              :   bool compute_convert_gain () final override;
     353              : 
     354              :  private:
     355              :   void fix_debug_reg_uses (rtx reg);
     356              :   void convert_insn (rtx_insn *insn) final override;
     357              : };
     358              : 
     359              : } // anon namespace
     360              : 
     361              : unsigned scalar_chain::max_id = 0;
     362              : 
     363              : namespace {
     364              : 
     365              : /* Initialize new chain.  */
     366              : 
     367      6674305 : scalar_chain::scalar_chain (enum machine_mode smode_, enum machine_mode vmode_)
     368              : {
     369      6674305 :   smode = smode_;
     370      6674305 :   vmode = vmode_;
     371              : 
     372      6674305 :   chain_id = ++max_id;
     373              : 
     374      6674305 :    if (dump_file)
     375          139 :     fprintf (dump_file, "Created a new instruction chain #%d\n", chain_id);
     376              : 
     377      6674305 :   bitmap_obstack_initialize (NULL);
     378      6674305 :   insns = BITMAP_ALLOC (NULL);
     379      6674305 :   defs = BITMAP_ALLOC (NULL);
     380      6674305 :   defs_conv = BITMAP_ALLOC (NULL);
     381      6674305 :   insns_conv = BITMAP_ALLOC (NULL);
     382      6674305 :   queue = NULL;
     383              : 
     384      6674305 :   cost_sse_integer = 0;
     385      6674305 :   weighted_cost_sse_integer = 0 ;
     386      6674305 :   max_visits = x86_stv_max_visits;
     387      6674305 : }
     388              : 
     389              : /* Free chain's data.  */
     390              : 
     391      6674305 : scalar_chain::~scalar_chain ()
     392              : {
     393      6674305 :   BITMAP_FREE (insns);
     394      6674305 :   BITMAP_FREE (defs);
     395      6674305 :   BITMAP_FREE (defs_conv);
     396      6674305 :   BITMAP_FREE (insns_conv);
     397      6674305 :   bitmap_obstack_release (NULL);
     398      6674305 : }
     399              : 
     400              : /* Add instruction into chains' queue.  */
     401              : 
     402              : void
     403      8483280 : scalar_chain::add_to_queue (unsigned insn_uid)
     404              : {
     405      8483280 :   if (!bitmap_set_bit (queue, insn_uid))
     406              :     return;
     407              : 
     408      6504463 :   if (dump_file)
     409          147 :     fprintf (dump_file, "  Adding insn %d into chain's #%d queue\n",
     410              :              insn_uid, chain_id);
     411              : }
     412              : 
     413              : /* For DImode conversion, mark register defined by DEF as requiring
     414              :    conversion.  */
     415              : 
     416              : void
     417      9845921 : scalar_chain::mark_dual_mode_def (df_ref def)
     418              : {
     419      9845921 :   gcc_assert (DF_REF_REG_DEF_P (def));
     420              : 
     421              :   /* Record the def/insn pair so we can later efficiently iterate over
     422              :      the defs to convert on insns not in the chain.  */
     423      9845921 :   bool reg_new = bitmap_set_bit (defs_conv, DF_REF_REGNO (def));
     424      9845921 :   basic_block bb = BLOCK_FOR_INSN (DF_REF_INSN (def));
     425      9845921 :   profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
     426      9845921 :   bool speed_p = optimize_bb_for_speed_p (bb);
     427      9845921 :   int cost = 0;
     428              : 
     429      9845921 :   if (!bitmap_bit_p (insns, DF_REF_INSN_UID (def)))
     430              :     {
     431      2988660 :       if (!bitmap_set_bit (insns_conv, DF_REF_INSN_UID (def))
     432      2988660 :           && !reg_new)
     433      1525596 :         return;
     434              : 
     435              :       /* Cost integer to sse moves.  */
     436      2627487 :       if (speed_p)
     437      2318527 :         cost = COSTS_N_INSNS (ix86_cost->integer_to_sse) / 2;
     438       308960 :       else if (TARGET_64BIT || smode == SImode)
     439              :         cost = COSTS_N_BYTES (4);
     440              :       /* vmovd (4 bytes) + vpinsrd (6 bytes).  */
     441        18748 :       else if (TARGET_SSE4_1)
     442              :         cost = COSTS_N_BYTES (10);
     443              :       /* movd (4 bytes) + movd (4 bytes) + unpckldq (4 bytes).  */
     444              :       else
     445      8320325 :         cost = COSTS_N_BYTES (12);
     446              :     }
     447              :   else
     448              :     {
     449      6857261 :       if (!reg_new)
     450              :         return;
     451              : 
     452              :       /* Cost sse to integer moves.  */
     453      5692838 :       if (speed_p)
     454      5100817 :         cost = COSTS_N_INSNS (ix86_cost->sse_to_integer) / 2;
     455       592021 :       else if (TARGET_64BIT || smode == SImode)
     456              :         cost = COSTS_N_BYTES (4);
     457              :       /* vmovd (4 bytes) + vpextrd (6 bytes).  */
     458         2849 :       else if (TARGET_SSE4_1)
     459              :         cost = COSTS_N_BYTES (10);
     460              :       /* movd (4 bytes) + psrlq (5 bytes) + movd (4 bytes).  */
     461              :       else
     462      8320325 :         cost = COSTS_N_BYTES (13);
     463              :     }
     464              : 
     465      8320325 :   if (speed_p)
     466      7419344 :     weighted_cost_sse_integer += bb->count.to_sreal_scale (entry_count) * cost;
     467              : 
     468      8320325 :   cost_sse_integer += cost;
     469              : 
     470      8320325 :   if (dump_file)
     471          236 :     fprintf (dump_file,
     472              :              "  Mark r%d def in insn %d as requiring both modes in chain #%d\n",
     473          236 :              DF_REF_REGNO (def), DF_REF_INSN_UID (def), chain_id);
     474              : }
     475              : 
     476              : /* Check REF's chain to add new insns into a queue
     477              :    and find registers requiring conversion.  Return true if OK, false
     478              :    if the analysis was aborted.  */
     479              : 
     480              : bool
     481     18615794 : scalar_chain::analyze_register_chain (bitmap candidates, df_ref ref,
     482              :                                       bitmap disallowed)
     483              : {
     484     18615794 :   df_link *chain;
     485     18615794 :   bool mark_def = false;
     486              : 
     487     18615794 :   gcc_checking_assert (bitmap_bit_p (insns, DF_REF_INSN_UID (ref)));
     488              : 
     489     63406494 :   for (chain = DF_REF_CHAIN (ref); chain; chain = chain->next)
     490              :     {
     491     44792539 :       unsigned uid = DF_REF_INSN_UID (chain->ref);
     492              : 
     493     44792539 :       if (!NONDEBUG_INSN_P (DF_REF_INSN (chain->ref)))
     494      7658628 :         continue;
     495              : 
     496     37133911 :       if (--max_visits == 0)
     497              :         return false;
     498              : 
     499     37133390 :       if (!DF_REF_REG_MEM_P (chain->ref))
     500              :         {
     501     31160831 :           if (bitmap_bit_p (insns, uid))
     502      9918122 :             continue;
     503              : 
     504     21242709 :           if (bitmap_bit_p (candidates, uid))
     505              :             {
     506      8483280 :               add_to_queue (uid);
     507      8483280 :               continue;
     508              :             }
     509              : 
     510              :           /* If we run into parts of an aborted chain discovery abort.  */
     511     12759429 :           if (bitmap_bit_p (disallowed, uid))
     512              :             return false;
     513              :         }
     514              : 
     515     18730845 :       if (DF_REF_REG_DEF_P (chain->ref))
     516              :         {
     517              :           /* An asm goto with outputs defines them on all outgoing edges,
     518              :              so a conversion copy can't be emitted after it.  */
     519      2988835 :           if (JUMP_P (DF_REF_INSN (chain->ref)))
     520              :             {
     521          175 :               if (dump_file)
     522            1 :                 fprintf (dump_file,
     523              :                          "  r%d def in jump insn %d, aborting chain\n",
     524              :                          DF_REF_REGNO (chain->ref), uid);
     525              :               return false;
     526              :             }
     527              : 
     528      2988660 :           if (dump_file)
     529          125 :             fprintf (dump_file, "  r%d def in insn %d isn't convertible\n",
     530              :                      DF_REF_REGNO (chain->ref), uid);
     531      2988660 :           mark_dual_mode_def (chain->ref);
     532              :         }
     533              :       else
     534              :         {
     535     15742010 :           if (dump_file)
     536          524 :             fprintf (dump_file, "  r%d use in insn %d isn't convertible\n",
     537              :                      DF_REF_REGNO (chain->ref), uid);
     538              :           mark_def = true;
     539              :         }
     540              :     }
     541              : 
     542     18613955 :   if (mark_def)
     543      6857261 :     mark_dual_mode_def (ref);
     544              : 
     545              :   return true;
     546              : }
     547              : 
     548              : /* Check whether X is a convertible *concatditi_? variant.  X is known
     549              :    to be any_or_plus:TI, i.e. PLUS:TI, IOR:TI or XOR:TI.  */
     550              : 
     551              : static bool
     552        33646 : timode_concatdi_p (rtx x)
     553              : {
     554        33646 :   rtx op0 = XEXP (x, 0);
     555        33646 :   rtx op1 = XEXP (x, 1);
     556              : 
     557        33646 :   if (GET_CODE (op1) == ASHIFT)
     558          964 :     std::swap (op0, op1);
     559              : 
     560        33646 :   return GET_CODE (op0) == ASHIFT
     561        22101 :          && GET_CODE (XEXP (op0, 0)) == ZERO_EXTEND
     562        22101 :          && GET_MODE (XEXP (XEXP (op0, 0), 0)) == DImode
     563        22101 :          && REG_P (XEXP (XEXP (op0, 0), 0))
     564        21944 :          && CONST_INT_P (XEXP (op0, 1))
     565        21944 :          && INTVAL (XEXP (op0, 1)) == 64
     566        21944 :          && GET_CODE (op1) == ZERO_EXTEND
     567        20980 :          && GET_MODE (XEXP (op1, 0)) == DImode
     568        54626 :          && REG_P (XEXP (op1, 0));
     569              : }
     570              : 
     571              : 
     572              : /* Add instruction into a chain.  Return true if OK, false if the search
     573              :    was aborted.  */
     574              : 
     575              : bool
     576     13176888 : scalar_chain::add_insn (bitmap candidates, unsigned int insn_uid,
     577              :                         bitmap disallowed)
     578              : {
     579     13176888 :   if (!bitmap_set_bit (insns, insn_uid))
     580              :     return true;
     581              : 
     582     13176888 :   if (dump_file)
     583          284 :     fprintf (dump_file, "  Adding insn %d to chain #%d\n", insn_uid, chain_id);
     584              : 
     585     13176888 :   rtx_insn *insn = DF_INSN_UID_GET (insn_uid)->insn;
     586     13176888 :   rtx def_set = single_set (insn);
     587     13176888 :   if (def_set && REG_P (SET_DEST (def_set))
     588     23317232 :       && !HARD_REGISTER_P (SET_DEST (def_set)))
     589     10114817 :     bitmap_set_bit (defs, REGNO (SET_DEST (def_set)));
     590              : 
     591              :   /* ???  The following is quadratic since analyze_register_chain
     592              :      iterates over all refs to look for dual-mode regs.  Instead this
     593              :      should be done separately for all regs mentioned in the chain once.  */
     594     13176888 :   df_ref ref;
     595     26922818 :   for (ref = DF_INSN_UID_DEFS (insn_uid); ref; ref = DF_REF_NEXT_LOC (ref))
     596     13746721 :     if (!HARD_REGISTER_P (DF_REF_REG (ref)))
     597     10114817 :       if (!analyze_register_chain (candidates, ref, disallowed))
     598              :         return false;
     599              : 
     600              :   /* The operand(s) of VEC_SELECT, ZERO_EXTEND and similar ops don't need
     601              :      to be converted/convertible.  */
     602     13176097 :   if (def_set)
     603     13176097 :     switch (GET_CODE (SET_SRC (def_set)))
     604              :       {
     605      3940809 :       case REG:
     606      3940809 :         if (HARD_REGISTER_P (SET_SRC (def_set)))
     607              :           return true;
     608              :         break;
     609              :       case VEC_SELECT:
     610              :         return true;
     611        10393 :       case ZERO_EXTEND:
     612        10393 :         if (GET_MODE (XEXP (SET_SRC (def_set), 0)) == DImode)
     613              :           return true;
     614              :         break;
     615      2465511 :       case PLUS:
     616      2465511 :       case IOR:
     617      2465511 :       case XOR:
     618      2465511 :         if (smode == TImode && timode_concatdi_p (SET_SRC (def_set)))
     619              :           return true;
     620              :         break;
     621              :       default:
     622              :         break;
     623              :       }
     624              : 
     625     28869625 :   for (ref = DF_INSN_UID_USES (insn_uid); ref; ref = DF_REF_NEXT_LOC (ref))
     626     15749007 :     if (DF_REF_TYPE (ref) == DF_REF_REG_USE
     627      8500980 :         && !SUBREG_P (DF_REF_REG (ref)))
     628      8500977 :       if (!analyze_register_chain (candidates, ref, disallowed))
     629              :         return false;
     630              : 
     631              :   return true;
     632              : }
     633              : 
     634              : /* Build new chain starting from insn INSN_UID recursively
     635              :    adding all dependent uses and definitions.  Return true if OK, false
     636              :    if the chain discovery was aborted.  */
     637              : 
     638              : bool
     639      6674305 : scalar_chain::build (bitmap candidates, unsigned insn_uid, bitmap disallowed)
     640              : {
     641      6674305 :   queue = BITMAP_ALLOC (NULL);
     642      6674305 :   bitmap_set_bit (queue, insn_uid);
     643              : 
     644      6674305 :   if (dump_file)
     645          139 :     fprintf (dump_file, "Building chain #%d...\n", chain_id);
     646              : 
     647     19849354 :   while (!bitmap_empty_p (queue))
     648              :     {
     649     13176888 :       insn_uid = bitmap_first_set_bit (queue);
     650     13176888 :       bitmap_clear_bit (queue, insn_uid);
     651     13176888 :       bitmap_clear_bit (candidates, insn_uid);
     652     13176888 :       if (!add_insn (candidates, insn_uid, disallowed))
     653              :         {
     654              :           /* If we aborted the search put sofar found insn on the set of
     655              :              disallowed insns so that further searches reaching them also
     656              :              abort and thus we abort the whole but yet undiscovered chain.  */
     657         1839 :           bitmap_ior_into (disallowed, insns);
     658         1839 :           if (dump_file)
     659            3 :             fprintf (dump_file, "Aborted chain #%d discovery\n", chain_id);
     660         1839 :           BITMAP_FREE (queue);
     661         1839 :           return false;
     662              :         }
     663              :     }
     664              : 
     665      6672466 :   if (dump_file)
     666              :     {
     667          136 :       fprintf (dump_file, "Collected chain #%d...\n", chain_id);
     668          136 :       fprintf (dump_file, "  insns: ");
     669          136 :       dump_bitmap (dump_file, insns);
     670          136 :       if (!bitmap_empty_p (defs_conv))
     671              :         {
     672          136 :           bitmap_iterator bi;
     673          136 :           unsigned id;
     674          136 :           const char *comma = "";
     675          136 :           fprintf (dump_file, "  defs to convert: ");
     676          362 :           EXECUTE_IF_SET_IN_BITMAP (defs_conv, 0, id, bi)
     677              :             {
     678          226 :               fprintf (dump_file, "%sr%d", comma, id);
     679          226 :               comma = ", ";
     680              :             }
     681          136 :           fprintf (dump_file, "\n");
     682              :         }
     683              :     }
     684              : 
     685      6672466 :   BITMAP_FREE (queue);
     686              : 
     687      6672466 :   return true;
     688              : }
     689              : 
     690              : /* Return a cost of building a vector constant
     691              :    instead of using a scalar one.  */
     692              : 
     693              : int
     694      2739974 : general_scalar_chain::vector_const_cost (rtx exp, basic_block bb)
     695              : {
     696      2739974 :   gcc_assert (CONST_INT_P (exp));
     697              : 
     698      2739974 :   if (standard_sse_constant_p (exp, vmode))
     699       619096 :     return ix86_cost->sse_op;
     700      2120878 :   if (optimize_bb_for_size_p (bb))
     701              :     return COSTS_N_BYTES (8);
     702              :   /* We have separate costs for SImode and DImode, use SImode costs
     703              :      for smaller modes.  */
     704      2509620 :   return COSTS_N_INSNS (ix86_cost->sse_load[smode == DImode ? 1 : 0]) / 2;
     705              : }
     706              : 
     707              : /* Return true if it's cost profitable for chain conversion.  */
     708              : 
     709              : bool
     710      6140700 : general_scalar_chain::compute_convert_gain ()
     711              : {
     712      6140700 :   bitmap_iterator bi;
     713      6140700 :   unsigned insn_uid;
     714      6140700 :   int gain = 0;
     715      6140700 :   sreal weighted_gain = 0;
     716              : 
     717      6140700 :   if (dump_file)
     718          136 :     fprintf (dump_file, "Computing gain for chain #%d...\n", chain_id);
     719              : 
     720              :   /* SSE costs distinguish between SImode and DImode loads/stores, for
     721              :      int costs factor in the number of GPRs involved.  When supporting
     722              :      smaller modes than SImode the int load/store costs need to be
     723              :      adjusted as well.  */
     724      6140700 :   unsigned sse_cost_idx = smode == DImode ? 1 : 0;
     725      6140700 :   int m = smode == DImode ? (TARGET_64BIT ? 1 : 2) : 1;
     726              : 
     727     18249463 :   EXECUTE_IF_SET_IN_BITMAP (insns, 0, insn_uid, bi)
     728              :     {
     729     12108763 :       rtx_insn *insn = DF_INSN_UID_GET (insn_uid)->insn;
     730     12108763 :       rtx def_set = single_set (insn);
     731     12108763 :       rtx src = SET_SRC (def_set);
     732     12108763 :       rtx dst = SET_DEST (def_set);
     733     12108763 :       basic_block bb = BLOCK_FOR_INSN (insn);
     734     12108763 :       int igain = 0;
     735     12108763 :       profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
     736     12108763 :       bool speed_p = optimize_bb_for_speed_p (bb);
     737     12108763 :       sreal bb_freq = bb->count.to_sreal_scale (entry_count);
     738              : 
     739     12108763 :       if (REG_P (src) && REG_P (dst))
     740              :         {
     741       970513 :           if (!speed_p)
     742              :             /* reg-reg move is 2 bytes, while SSE 3.  */
     743       209167 :             igain += COSTS_N_BYTES (2 * m - 3);
     744              :           else
     745              :             /* Move costs are normalized to reg-reg move having cost 2.  */
     746       761346 :             igain += COSTS_N_INSNS (2 * m - ix86_cost->xmm_move) / 2;
     747              :         }
     748     11138250 :       else if (REG_P (src) && MEM_P (dst))
     749              :         {
     750      2419435 :           if (!speed_p)
     751              :             /* Integer load/store is 3+ bytes and SSE 4+.  */
     752       204034 :             igain += COSTS_N_BYTES (3 * m - 4);
     753              :           else
     754      2215401 :             igain
     755      2215401 :               += COSTS_N_INSNS (m * ix86_cost->int_store[2]
     756              :                                 - ix86_cost->sse_store[sse_cost_idx]) / 2;
     757              :         }
     758      8718815 :       else if (MEM_P (src) && REG_P (dst))
     759              :         {
     760      3903646 :           if (!speed_p)
     761       367220 :             igain += COSTS_N_BYTES (3 * m - 4);
     762              :           else
     763      3536426 :             igain += COSTS_N_INSNS (m * ix86_cost->int_load[2]
     764              :                                     - ix86_cost->sse_load[sse_cost_idx]) / 2;
     765              :         }
     766              :       else
     767              :         {
     768              :           /* For operations on memory operands, include the overhead
     769              :              of explicit load and store instructions.  */
     770      4815169 :           if (MEM_P (dst))
     771              :             {
     772        73632 :               if (!speed_p)
     773              :                 /* ??? This probably should account size difference
     774              :                    of SSE and integer load rather than full SSE load.  */
     775              :                 igain -= COSTS_N_BYTES (8);
     776              :               else
     777              :                 {
     778        63456 :                   int cost = (m * (ix86_cost->int_load[2]
     779        63456 :                                    + ix86_cost->int_store[2])
     780        63456 :                              - (ix86_cost->sse_load[sse_cost_idx] +
     781        63456 :                                 ix86_cost->sse_store[sse_cost_idx]));
     782        63456 :                   igain += COSTS_N_INSNS (cost) / 2;
     783              :                 }
     784              :             }
     785              : 
     786      4815169 :           switch (GET_CODE (src))
     787              :             {
     788       507723 :             case ASHIFT:
     789       507723 :             case ASHIFTRT:
     790       507723 :             case LSHIFTRT:
     791       507723 :               if (m == 2)
     792              :                 {
     793        17929 :                   if (INTVAL (XEXP (src, 1)) >= 32)
     794        12323 :                     igain += ix86_cost->add;
     795              :                   /* Gain for extend highpart case.  */
     796         5606 :                   else if (GET_CODE (XEXP (src, 0)) == ASHIFT)
     797            0 :                     igain += ix86_cost->shift_const - ix86_cost->sse_op;
     798              :                   else
     799         5606 :                     igain += ix86_cost->shift_const;
     800              :                 }
     801              : 
     802       507723 :               igain += ix86_cost->shift_const - ix86_cost->sse_op;
     803              : 
     804       507723 :               if (CONST_INT_P (XEXP (src, 0)))
     805            0 :                 igain -= vector_const_cost (XEXP (src, 0), bb);
     806              :               break;
     807              : 
     808         3942 :             case ROTATE:
     809         3942 :             case ROTATERT:
     810         3942 :               igain += m * ix86_cost->shift_const;
     811         3942 :               if (TARGET_AVX512VL)
     812          204 :                 igain -= ix86_cost->sse_op;
     813         3738 :               else if (smode == DImode)
     814              :                 {
     815          798 :                   int bits = INTVAL (XEXP (src, 1));
     816          798 :                   if ((bits & 0x0f) == 0)
     817          193 :                     igain -= ix86_cost->sse_op;
     818          605 :                   else if ((bits & 0x07) == 0)
     819           31 :                     igain -= 2 * ix86_cost->sse_op;
     820              :                   else
     821          574 :                     igain -= 3 * ix86_cost->sse_op;
     822              :                 }
     823         2940 :               else if (INTVAL (XEXP (src, 1)) == 16)
     824          139 :                 igain -= ix86_cost->sse_op;
     825              :               else
     826         2801 :                 igain -= 2 * ix86_cost->sse_op;
     827              :               break;
     828              : 
     829      2974261 :             case AND:
     830      2974261 :             case IOR:
     831      2974261 :             case XOR:
     832      2974261 :             case PLUS:
     833      2974261 :             case MINUS:
     834      2974261 :               igain += m * ix86_cost->add - ix86_cost->sse_op;
     835              :               /* Additional gain for andnot for targets without BMI.  */
     836      2974261 :               if (GET_CODE (XEXP (src, 0)) == NOT
     837         3148 :                   && !TARGET_BMI)
     838         3139 :                 igain += m * ix86_cost->add;
     839              : 
     840      2974261 :               if (CONST_INT_P (XEXP (src, 0)))
     841            0 :                 igain -= vector_const_cost (XEXP (src, 0), bb);
     842      2974261 :               if (CONST_INT_P (XEXP (src, 1)))
     843      1747169 :                 igain -= vector_const_cost (XEXP (src, 1), bb);
     844      2974261 :               if (MEM_P (XEXP (src, 1)))
     845              :                 {
     846       105904 :                   if (!speed_p)
     847        24463 :                     igain -= COSTS_N_BYTES (m == 2 ? 3 : 5);
     848              :                   else
     849        93668 :                     igain += COSTS_N_INSNS
     850              :                                (m * ix86_cost->int_load[2]
     851              :                                  - ix86_cost->sse_load[sse_cost_idx]) / 2;
     852              :                 }
     853              :               break;
     854              : 
     855        61351 :             case NEG:
     856        61351 :             case NOT:
     857        61351 :               igain -= ix86_cost->sse_op + COSTS_N_INSNS (1);
     858              : 
     859        61351 :               if (GET_CODE (XEXP (src, 0)) != ABS)
     860              :                 {
     861        61351 :                   igain += m * ix86_cost->add;
     862        61351 :                   break;
     863              :                 }
     864              :               /* FALLTHRU */
     865              : 
     866          975 :             case ABS:
     867          975 :             case SMAX:
     868          975 :             case SMIN:
     869          975 :             case UMAX:
     870          975 :             case UMIN:
     871              :               /* We do not have any conditional move cost, estimate it as a
     872              :                  reg-reg move.  Comparisons are costed as adds.  */
     873          975 :               igain += m * (COSTS_N_INSNS (2) + ix86_cost->add);
     874              :               /* Integer SSE ops are all costed the same.  */
     875          975 :               igain -= ix86_cost->sse_op;
     876          975 :               break;
     877              : 
     878            0 :             case COMPARE:
     879            0 :               if (XEXP (src, 1) != const0_rtx)
     880              :                 {
     881              :                   /* cmp vs. pxor;pshufd;ptest.  */
     882            0 :                   igain += COSTS_N_INSNS (m - 3);
     883              :                 }
     884            0 :               else if (GET_CODE (XEXP (src, 0)) != AND)
     885              :                 {
     886              :                   /* test vs. pshufd;ptest.  */
     887            0 :                   igain += COSTS_N_INSNS (m - 2);
     888              :                 }
     889            0 :               else if (GET_CODE (XEXP (XEXP (src, 0), 0)) != NOT)
     890              :                 {
     891              :                   /* and;test vs. pshufd;ptest.  */
     892            0 :                   igain += COSTS_N_INSNS (2 * m - 2);
     893              :                 }
     894            0 :               else if (TARGET_BMI)
     895              :                 {
     896              :                   /* andn;test vs. pandn;pshufd;ptest.  */
     897            0 :                   igain += COSTS_N_INSNS (2 * m - 3);
     898              :                 }
     899              :               else
     900              :                 {
     901              :                   /* not;and;test vs. pandn;pshufd;ptest.  */
     902            0 :                   igain += COSTS_N_INSNS (3 * m - 3);
     903              :                 }
     904              :               break;
     905              : 
     906      1232156 :             case CONST_INT:
     907      1232156 :               if (REG_P (dst))
     908              :                 {
     909      1232156 :                   if (!speed_p)
     910              :                     {
     911              :                       /* xor (2 bytes) vs. xorps (3 bytes).  */
     912       239351 :                       if (src == const0_rtx)
     913       125377 :                         igain -= COSTS_N_BYTES (1);
     914              :                       /* movdi_internal vs. movv2di_internal.  */
     915              :                       /* => mov (5 bytes) vs. movaps (7 bytes).  */
     916       113974 :                       else if (x86_64_immediate_operand (src, SImode))
     917       101749 :                         igain -= COSTS_N_BYTES (2);
     918              :                       else
     919              :                         /* ??? Larger immediate constants are placed in the
     920              :                            constant pool, where the size benefit/impact of
     921              :                            STV conversion is affected by whether and how
     922              :                            often each constant pool entry is shared/reused.
     923              :                            The value below is empirically derived from the
     924              :                            CSiBE benchmark (and the optimal value may drift
     925              :                            over time).  */
     926              :                         igain += COSTS_N_BYTES (0);
     927              :                     }
     928              :                   else
     929              :                     {
     930              :                       /* DImode can be immediate for TARGET_64BIT
     931              :                          and SImode always.  */
     932       992805 :                       igain += m * COSTS_N_INSNS (1);
     933       992805 :                       igain -= vector_const_cost (src, bb);
     934              :                     }
     935              :                 }
     936            0 :               else if (MEM_P (dst))
     937              :                 {
     938            0 :                   igain += (m * ix86_cost->int_store[2]
     939            0 :                             - ix86_cost->sse_store[sse_cost_idx]);
     940            0 :                   igain -= vector_const_cost (src, bb);
     941              :                 }
     942              :               break;
     943              : 
     944        24722 :             case VEC_SELECT:
     945        24722 :               if (XVECEXP (XEXP (src, 1), 0, 0) == const0_rtx)
     946              :                 {
     947              :                   // movd (4 bytes) replaced with movdqa (4 bytes).
     948        19494 :                   if (!!speed_p)
     949        17852 :                     igain += COSTS_N_INSNS (ix86_cost->sse_to_integer
     950              :                                             - ix86_cost->xmm_move) / 2;
     951              :                 }
     952              :               else
     953              :                 {
     954              :                   // pshufd; movd replaced with pshufd.
     955         5228 :                   if (!speed_p)
     956          516 :                     igain += COSTS_N_BYTES (4);
     957              :                   else
     958         4712 :                     igain += ix86_cost->sse_to_integer;
     959              :                 }
     960              :               break;
     961              : 
     962        10039 :             case ZERO_EXTEND:
     963              :               /* mov eax (6 bytes) vs movd xmm0 (8 bytes). */
     964              :               /* mov eax; xor edx,edx (7 bytes).  */
     965        10039 :               if (speed_p)
     966         8871 :                 igain += COSTS_N_INSNS (ix86_cost->int_load[2]
     967              :                                         - ix86_cost->sse_load[0]) / 2;
     968              :               else
     969         1214 :                 igain += COSTS_N_BYTES (TARGET_64BIT ? -2 : -1);
     970              :               break;
     971              : 
     972            0 :             default:
     973            0 :               gcc_unreachable ();
     974              :             }
     975              :         }
     976              : 
     977     12107121 :       if (speed_p)
     978     10769564 :         weighted_gain += bb_freq * igain;
     979     12108763 :       gain += igain;
     980              : 
     981     12108763 :       if (igain != 0 && dump_file)
     982              :         {
     983           93 :           fprintf (dump_file, "  Instruction gain %d with bb_freq %.2f for",
     984              :                    igain, bb_freq.to_double ());
     985           93 :           dump_insn_slim (dump_file, insn);
     986              :         }
     987              :     }
     988              : 
     989      6140700 :   if (dump_file)
     990              :     {
     991          136 :       fprintf (dump_file, "  Instruction conversion gain: %d, \n",
     992              :                gain);
     993          136 :       fprintf (dump_file, "  Registers conversion cost: %d\n",
     994              :                cost_sse_integer);
     995          136 :       fprintf (dump_file, "  Weighted instruction conversion gain: %.2f, \n",
     996              :                weighted_gain.to_double ());
     997          136 :       fprintf (dump_file, "  Weighted registers conversion cost: %.2f\n",
     998              :                weighted_cost_sse_integer.to_double ());
     999              :     }
    1000              : 
    1001      6140700 :   if (weighted_gain != weighted_cost_sse_integer)
    1002      4948978 :     return weighted_gain > weighted_cost_sse_integer;
    1003              :   else
    1004      1191722 :     return gain > cost_sse_integer;;
    1005              : }
    1006              : 
    1007              : /* Insert generated conversion instruction sequence INSNS
    1008              :    after instruction AFTER.  New BB may be required in case
    1009              :    instruction has EH region attached.  */
    1010              : 
    1011              : void
    1012        33337 : scalar_chain::emit_conversion_insns (rtx_insn *insns, rtx_insn *after)
    1013              : {
    1014        33337 :   if (!control_flow_insn_p (after))
    1015              :     {
    1016        33129 :       emit_insn_after (insns, after);
    1017        33129 :       return;
    1018              :     }
    1019              : 
    1020          208 :   basic_block bb = BLOCK_FOR_INSN (after);
    1021          208 :   edge e = find_fallthru_edge (bb->succs);
    1022          208 :   gcc_assert (e);
    1023              : 
    1024          208 :   basic_block new_bb = split_edge (e);
    1025          208 :   emit_insn_after (insns, BB_HEAD (new_bb));
    1026              : }
    1027              : 
    1028              : } // anon namespace
    1029              : 
    1030              : /* Generate the canonical SET_SRC to move GPR to a VMODE vector register,
    1031              :    zeroing the upper parts.  */
    1032              : 
    1033              : static rtx
    1034       173045 : gen_gpr_to_xmm_move_src (enum machine_mode vmode, rtx gpr)
    1035              : {
    1036       346090 :   switch (GET_MODE_NUNITS (vmode))
    1037              :     {
    1038           71 :     case 1:
    1039           71 :       return gen_rtx_SUBREG (vmode, gpr, 0);
    1040       172390 :     case 2:
    1041       344780 :       return gen_rtx_VEC_CONCAT (vmode, gpr,
    1042              :                                  CONST0_RTX (GET_MODE_INNER (vmode)));
    1043          584 :     default:
    1044          584 :       return gen_rtx_VEC_MERGE (vmode, gen_rtx_VEC_DUPLICATE (vmode, gpr),
    1045              :                                 CONST0_RTX (vmode), const1_rtx);
    1046              :     }
    1047              : }
    1048              : 
    1049              : /* Make vector copies for all register REGNO definitions
    1050              :    and replace its uses in a chain.  */
    1051              : 
    1052              : void
    1053         8840 : scalar_chain::make_vector_copies (rtx_insn *insn, rtx reg)
    1054              : {
    1055         8840 :   rtx vreg = *defs_map.get (reg);
    1056              : 
    1057         8840 :   start_sequence ();
    1058         8840 :   if (!TARGET_INTER_UNIT_MOVES_TO_VEC)
    1059              :     {
    1060            0 :       rtx tmp = assign_386_stack_local (smode, SLOT_STV_TEMP);
    1061            0 :       if (smode == DImode && !TARGET_64BIT)
    1062              :         {
    1063            0 :           emit_move_insn (adjust_address (tmp, SImode, 0),
    1064              :                           gen_rtx_SUBREG (SImode, reg, 0));
    1065            0 :           emit_move_insn (adjust_address (tmp, SImode, 4),
    1066              :                           gen_rtx_SUBREG (SImode, reg, 4));
    1067              :         }
    1068              :       else
    1069            0 :         emit_move_insn (copy_rtx (tmp), reg);
    1070            0 :       emit_insn (gen_rtx_SET (gen_rtx_SUBREG (vmode, vreg, 0),
    1071              :                               gen_gpr_to_xmm_move_src (vmode, tmp)));
    1072              :     }
    1073         8840 :   else if (!TARGET_64BIT && smode == DImode)
    1074              :     {
    1075         8773 :       if (TARGET_SSE4_1)
    1076              :         {
    1077          280 :           emit_insn (gen_sse2_loadld (gen_rtx_SUBREG (V4SImode, vreg, 0),
    1078              :                                       CONST0_RTX (V4SImode),
    1079              :                                       gen_rtx_SUBREG (SImode, reg, 0)));
    1080          280 :           emit_insn (gen_sse4_1_pinsrd (gen_rtx_SUBREG (V4SImode, vreg, 0),
    1081              :                                         gen_rtx_SUBREG (V4SImode, vreg, 0),
    1082              :                                         gen_rtx_SUBREG (SImode, reg, 4),
    1083              :                                         const2_rtx));
    1084              :         }
    1085              :       else
    1086              :         {
    1087         8493 :           rtx tmp = gen_reg_rtx (DImode);
    1088         8493 :           emit_insn (gen_sse2_loadld (gen_rtx_SUBREG (V4SImode, vreg, 0),
    1089              :                                       CONST0_RTX (V4SImode),
    1090              :                                       gen_rtx_SUBREG (SImode, reg, 0)));
    1091         8493 :           emit_insn (gen_sse2_loadld (gen_rtx_SUBREG (V4SImode, tmp, 0),
    1092              :                                       CONST0_RTX (V4SImode),
    1093              :                                       gen_rtx_SUBREG (SImode, reg, 4)));
    1094         8493 :           emit_insn (gen_vec_interleave_lowv4si
    1095              :                      (gen_rtx_SUBREG (V4SImode, vreg, 0),
    1096              :                       gen_rtx_SUBREG (V4SImode, vreg, 0),
    1097              :                       gen_rtx_SUBREG (V4SImode, tmp, 0)));
    1098              :         }
    1099              :     }
    1100              :   else
    1101           67 :     emit_insn (gen_rtx_SET (gen_rtx_SUBREG (vmode, vreg, 0),
    1102              :                             gen_gpr_to_xmm_move_src (vmode, reg)));
    1103         8840 :   rtx_insn *seq = end_sequence ();
    1104         8840 :   emit_conversion_insns (seq, insn);
    1105              : 
    1106         8840 :   if (dump_file)
    1107            0 :     fprintf (dump_file,
    1108              :              "  Copied r%d to a vector register r%d for insn %d\n",
    1109            0 :              REGNO (reg), REGNO (vreg), INSN_UID (insn));
    1110         8840 : }
    1111              : 
    1112              : /* Copy the definition SRC of INSN inside the chain to DST for
    1113              :    scalar uses outside of the chain.  */
    1114              : 
    1115              : void
    1116        23757 : scalar_chain::convert_reg (rtx_insn *insn, rtx dst, rtx src)
    1117              : {
    1118        23757 :   start_sequence ();
    1119        23757 :   if (!TARGET_INTER_UNIT_MOVES_FROM_VEC)
    1120              :     {
    1121            0 :       rtx tmp = assign_386_stack_local (smode, SLOT_STV_TEMP);
    1122            0 :       emit_move_insn (tmp, src);
    1123            0 :       if (!TARGET_64BIT && smode == DImode)
    1124              :         {
    1125            0 :           emit_move_insn (gen_rtx_SUBREG (SImode, dst, 0),
    1126              :                           adjust_address (tmp, SImode, 0));
    1127            0 :           emit_move_insn (gen_rtx_SUBREG (SImode, dst, 4),
    1128              :                           adjust_address (tmp, SImode, 4));
    1129              :         }
    1130              :       else
    1131            0 :         emit_move_insn (dst, copy_rtx (tmp));
    1132              :     }
    1133        23757 :   else if (!TARGET_64BIT && smode == DImode)
    1134              :     {
    1135        22731 :       if (TARGET_SSE4_1)
    1136              :         {
    1137            0 :           rtx tmp = gen_rtx_PARALLEL (VOIDmode,
    1138              :                                       gen_rtvec (1, const0_rtx));
    1139            0 :           emit_insn
    1140            0 :               (gen_rtx_SET
    1141              :                (gen_rtx_SUBREG (SImode, dst, 0),
    1142              :                 gen_rtx_VEC_SELECT (SImode,
    1143              :                                     gen_rtx_SUBREG (V4SImode, src, 0),
    1144              :                                     tmp)));
    1145              : 
    1146            0 :           tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const1_rtx));
    1147            0 :           emit_insn
    1148            0 :               (gen_rtx_SET
    1149              :                (gen_rtx_SUBREG (SImode, dst, 4),
    1150              :                 gen_rtx_VEC_SELECT (SImode,
    1151              :                                     gen_rtx_SUBREG (V4SImode, src, 0),
    1152              :                                     tmp)));
    1153              :         }
    1154              :       else
    1155              :         {
    1156        22731 :           rtx vcopy = gen_reg_rtx (V2DImode);
    1157        22731 :           emit_move_insn (vcopy, gen_rtx_SUBREG (V2DImode, src, 0));
    1158        22731 :           emit_move_insn (gen_rtx_SUBREG (SImode, dst, 0),
    1159              :                           gen_rtx_SUBREG (SImode, vcopy, 0));
    1160        22731 :           emit_move_insn (vcopy,
    1161              :                           gen_rtx_LSHIFTRT (V2DImode,
    1162              :                                             vcopy, GEN_INT (32)));
    1163        22731 :           emit_move_insn (gen_rtx_SUBREG (SImode, dst, 4),
    1164              :                           gen_rtx_SUBREG (SImode, vcopy, 0));
    1165              :         }
    1166              :     }
    1167              :   else
    1168         1026 :     emit_move_insn (dst, src);
    1169              : 
    1170        23757 :   rtx_insn *seq = end_sequence ();
    1171        23757 :   emit_conversion_insns (seq, insn);
    1172              : 
    1173        23757 :   if (dump_file)
    1174            0 :     fprintf (dump_file,
    1175              :              "  Copied r%d to a scalar register r%d for insn %d\n",
    1176            0 :              REGNO (src), REGNO (dst), INSN_UID (insn));
    1177        23757 : }
    1178              : 
    1179              : /* Helper function to convert immediate constant X to vmode.  */
    1180              : static rtx
    1181        38084 : smode_convert_cst (rtx x, enum machine_mode vmode)
    1182              : {
    1183              :   /* Prefer all ones vector in case of -1.  */
    1184        38084 :   if (constm1_operand (x, GET_MODE (x)))
    1185          623 :     return CONSTM1_RTX (vmode);
    1186              : 
    1187        37461 :   unsigned n = GET_MODE_NUNITS (vmode);
    1188        37461 :   rtx *v = XALLOCAVEC (rtx, n);
    1189        37461 :   v[0] = x;
    1190        43490 :   for (unsigned i = 1; i < n; ++i)
    1191         6029 :     v[i] = const0_rtx;
    1192        37461 :   return gen_rtx_CONST_VECTOR (vmode, gen_rtvec_v (n, v));
    1193              : }
    1194              : 
    1195              : /* Convert operand OP in INSN.  We should handle
    1196              :    memory operands and uninitialized registers.
    1197              :    All other register uses are converted during
    1198              :    registers conversion.  */
    1199              : 
    1200              : void
    1201       240044 : scalar_chain::convert_op (rtx *op, rtx_insn *insn)
    1202              : {
    1203       240044 :   rtx tmp;
    1204              : 
    1205       240044 :   if (GET_MODE (*op) == V1TImode)
    1206              :     return;
    1207              : 
    1208       239753 :   *op = copy_rtx_if_shared (*op);
    1209              : 
    1210       239753 :   if (GET_CODE (*op) == NOT
    1211       239753 :       || GET_CODE (*op) == ASHIFT)
    1212              :     {
    1213         2892 :       convert_op (&XEXP (*op, 0), insn);
    1214         2892 :       PUT_MODE (*op, vmode);
    1215              :     }
    1216              :   else if (MEM_P (*op))
    1217              :     {
    1218       172978 :       rtx_insn *movabs = NULL;
    1219              : 
    1220              :       /* Emit MOVABS to load from a 64-bit absolute address to a GPR.  */
    1221       172978 :       if (!memory_operand (*op, GET_MODE (*op)))
    1222              :         {
    1223            0 :           tmp = gen_reg_rtx (GET_MODE (*op));
    1224            0 :           movabs = emit_insn_before (gen_rtx_SET (tmp, *op), insn);
    1225              : 
    1226            0 :           *op = tmp;
    1227              :         }
    1228              : 
    1229       172978 :       tmp = gen_rtx_SUBREG (vmode, gen_reg_rtx (GET_MODE (*op)), 0);
    1230              : 
    1231       172978 :       rtx_insn *eh_insn
    1232       172978 :         = emit_insn_before (gen_rtx_SET (copy_rtx (tmp),
    1233              :                                          gen_gpr_to_xmm_move_src (vmode, *op)),
    1234       172978 :                             insn);
    1235              : 
    1236       172978 :       if (cfun->can_throw_non_call_exceptions)
    1237              :         {
    1238              :           /* Handle REG_EH_REGION note.  */
    1239       168793 :           rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
    1240       168793 :           if (note)
    1241              :             {
    1242         3577 :               if (movabs)
    1243            0 :                 eh_insn = movabs;
    1244         3577 :               control_flow_insns.safe_push (eh_insn);
    1245         3577 :               add_reg_note (eh_insn, REG_EH_REGION, XEXP (note, 0));
    1246              :             }
    1247              :         }
    1248              : 
    1249       172978 :       *op = tmp;
    1250              : 
    1251       172978 :       if (dump_file)
    1252            0 :         fprintf (dump_file, "  Preloading operand for insn %d into r%d\n",
    1253            0 :                  INSN_UID (insn), reg_or_subregno (tmp));
    1254              :     }
    1255              :   else if (REG_P (*op))
    1256        57367 :     *op = gen_rtx_SUBREG (vmode, *op, 0);
    1257              :   else if (CONST_SCALAR_INT_P (*op))
    1258              :     {
    1259         6513 :       rtx vec_cst = smode_convert_cst (*op, vmode);
    1260              : 
    1261         6513 :       if (!standard_sse_constant_p (vec_cst, vmode))
    1262              :         {
    1263         2716 :           start_sequence ();
    1264         2716 :           vec_cst = validize_mem (force_const_mem (vmode, vec_cst));
    1265         2716 :           rtx_insn *seq = end_sequence ();
    1266         2716 :           emit_insn_before (seq, insn);
    1267              :         }
    1268              : 
    1269         6513 :       tmp = gen_rtx_SUBREG (vmode, gen_reg_rtx (smode), 0);
    1270              : 
    1271         6513 :       emit_insn_before (gen_move_insn (copy_rtx (tmp), vec_cst), insn);
    1272         6513 :       *op = tmp;
    1273              :     }
    1274              :   else
    1275              :     {
    1276            0 :       gcc_assert (SUBREG_P (*op));
    1277            3 :       if (GET_MODE (*op) != vmode)
    1278              :         {
    1279            3 :           rtx inner = SUBREG_REG (*op);
    1280            3 :           poly_uint64 byte = SUBREG_BYTE (*op);
    1281            3 :           if (targetm.modes_tieable_p (vmode, GET_MODE (inner))
    1282            3 :               && validate_subreg (vmode, GET_MODE (inner), inner, byte))
    1283            3 :             *op = gen_lowpart (vmode, *op);
    1284              :           else
    1285              :             {
    1286            0 :               tmp = gen_reg_rtx (GET_MODE (*op));
    1287            0 :               emit_insn_before (gen_rtx_SET (tmp, *op), insn);
    1288            0 :               *op = gen_rtx_SUBREG (vmode, tmp, 0);
    1289              :             }
    1290              :         }
    1291              :     }
    1292              : }
    1293              : 
    1294              : /* Convert CCZmode COMPARE to vector mode.  */
    1295              : 
    1296              : rtx
    1297            8 : scalar_chain::convert_compare (rtx op1, rtx op2, rtx_insn *insn)
    1298              : {
    1299            8 :   rtx src, tmp;
    1300              : 
    1301              :   /* Handle any REG_EQUAL notes.  */
    1302            8 :   tmp = find_reg_equal_equiv_note (insn);
    1303            8 :   if (tmp)
    1304              :     {
    1305            1 :       if (GET_CODE (XEXP (tmp, 0)) == COMPARE
    1306            1 :           && GET_MODE (XEXP (tmp, 0)) == CCZmode
    1307            1 :           && REG_P (XEXP (XEXP (tmp, 0), 0)))
    1308              :         {
    1309            1 :           rtx *op = &XEXP (XEXP (tmp, 0), 1);
    1310            1 :           if (CONST_SCALAR_INT_P (*op))
    1311              :             {
    1312            1 :               if (constm1_operand (*op, GET_MODE (*op)))
    1313            0 :                 *op = CONSTM1_RTX (vmode);
    1314              :               else
    1315              :                 {
    1316            1 :                   unsigned n = GET_MODE_NUNITS (vmode);
    1317            1 :                   rtx *v = XALLOCAVEC (rtx, n);
    1318            1 :                   v[0] = *op;
    1319            1 :                   for (unsigned i = 1; i < n; ++i)
    1320            0 :                     v[i] = const0_rtx;
    1321            1 :                   *op = gen_rtx_CONST_VECTOR (vmode, gen_rtvec_v (n, v));
    1322              :                 }
    1323              :               tmp = NULL_RTX;
    1324              :             }
    1325            0 :           else if (REG_P (*op))
    1326              :             tmp = NULL_RTX;
    1327              :         }
    1328              : 
    1329            1 :       if (tmp)
    1330            0 :         remove_note (insn, tmp);
    1331              :     }
    1332              : 
    1333              :   /* Comparison against anything other than zero, requires an XOR.  */
    1334            8 :   if (op2 != const0_rtx)
    1335              :     {
    1336            6 :       convert_op (&op1, insn);
    1337            6 :       convert_op (&op2, insn);
    1338              :       /* If both operands are MEMs, explicitly load the OP1 into TMP.  */
    1339            6 :       if (MEM_P (op1) && MEM_P (op2))
    1340              :         {
    1341            0 :           tmp = gen_reg_rtx (vmode);
    1342            0 :           emit_insn_before (gen_rtx_SET (tmp, op1), insn);
    1343            0 :           src = tmp;
    1344              :         }
    1345              :       else
    1346              :         src = op1;
    1347            6 :       src = gen_rtx_XOR (vmode, src, op2);
    1348              :     }
    1349            2 :   else if (GET_CODE (op1) == AND
    1350            0 :            && GET_CODE (XEXP (op1, 0)) == NOT)
    1351              :     {
    1352            0 :       rtx op11 = XEXP (XEXP (op1, 0), 0);
    1353            0 :       rtx op12 = XEXP (op1, 1);
    1354            0 :       convert_op (&op11, insn);
    1355            0 :       convert_op (&op12, insn);
    1356            0 :       if (!REG_P (op11))
    1357              :         {
    1358            0 :           tmp = gen_reg_rtx (vmode);
    1359            0 :           emit_insn_before (gen_rtx_SET (tmp, op11), insn);
    1360            0 :           op11 = tmp;
    1361              :         }
    1362            0 :       src = gen_rtx_AND (vmode, gen_rtx_NOT (vmode, op11), op12);
    1363            0 :     }
    1364            2 :   else if (GET_CODE (op1) == AND)
    1365              :     {
    1366            0 :       rtx op11 = XEXP (op1, 0);
    1367            0 :       rtx op12 = XEXP (op1, 1);
    1368            0 :       convert_op (&op11, insn);
    1369            0 :       convert_op (&op12, insn);
    1370            0 :       if (!REG_P (op11))
    1371              :         {
    1372            0 :           tmp = gen_reg_rtx (vmode);
    1373            0 :           emit_insn_before (gen_rtx_SET (tmp, op11), insn);
    1374            0 :           op11 = tmp;
    1375              :         }
    1376            0 :       return gen_rtx_UNSPEC (CCZmode, gen_rtvec (2, op11, op12),
    1377              :                              UNSPEC_PTEST);
    1378              :     }
    1379              :   else
    1380              :     {
    1381            2 :       convert_op (&op1, insn);
    1382            2 :       src = op1;
    1383              :     }
    1384              : 
    1385            8 :   if (!REG_P (src))
    1386              :     {
    1387            6 :       tmp = gen_reg_rtx (vmode);
    1388            6 :       emit_insn_before (gen_rtx_SET (tmp, src), insn);
    1389            6 :       src = tmp;
    1390              :     }
    1391              : 
    1392            8 :   if (vmode == V2DImode)
    1393              :     {
    1394            0 :       tmp = gen_reg_rtx (vmode);
    1395            0 :       emit_insn_before (gen_vec_interleave_lowv2di (tmp, src, src), insn);
    1396            0 :       src = tmp;
    1397              :     }
    1398            8 :   else if (vmode == V4SImode)
    1399              :     {
    1400            0 :       tmp = gen_reg_rtx (vmode);
    1401            0 :       emit_insn_before (gen_sse2_pshufd (tmp, src, const0_rtx), insn);
    1402            0 :       src = tmp;
    1403              :     }
    1404              : 
    1405            8 :   return gen_rtx_UNSPEC (CCZmode, gen_rtvec (2, src, src), UNSPEC_PTEST);
    1406              : }
    1407              : 
    1408              : /* Helper function for converting INSN to vector mode.  */
    1409              : 
    1410              : void
    1411      1376334 : scalar_chain::convert_insn_common (rtx_insn *insn)
    1412              : {
    1413              :   /* Generate copies for out-of-chain uses of defs and adjust debug uses.  */
    1414      2097883 :   for (df_ref ref = DF_INSN_DEFS (insn); ref; ref = DF_REF_NEXT_LOC (ref))
    1415       721549 :     if (bitmap_bit_p (defs_conv, DF_REF_REGNO (ref)))
    1416              :       {
    1417        25173 :         df_link *use;
    1418        49363 :         for (use = DF_REF_CHAIN (ref); use; use = use->next)
    1419        47947 :           if (NONDEBUG_INSN_P (DF_REF_INSN (use->ref))
    1420        47947 :               && (DF_REF_REG_MEM_P (use->ref)
    1421        41970 :                   || !bitmap_bit_p (insns, DF_REF_INSN_UID (use->ref))))
    1422              :             break;
    1423        25173 :         if (use)
    1424        23757 :           convert_reg (insn, DF_REF_REG (ref),
    1425        23757 :                        *defs_map.get (regno_reg_rtx [DF_REF_REGNO (ref)]));
    1426         1416 :         else if (MAY_HAVE_DEBUG_BIND_INSNS)
    1427              :           {
    1428              :             /* If we generated a scalar copy we can leave debug-insns
    1429              :                as-is, if not, we have to adjust them.  */
    1430         1282 :             auto_vec<rtx_insn *, 5> to_reset_debug_insns;
    1431         3907 :             for (use = DF_REF_CHAIN (ref); use; use = use->next)
    1432         2625 :               if (DEBUG_INSN_P (DF_REF_INSN (use->ref)))
    1433              :                 {
    1434          821 :                   rtx_insn *debug_insn = DF_REF_INSN (use->ref);
    1435              :                   /* If there's a reaching definition outside of the
    1436              :                      chain we have to reset.  */
    1437          821 :                   df_link *def;
    1438         2869 :                   for (def = DF_REF_CHAIN (use->ref); def; def = def->next)
    1439         2212 :                     if (!bitmap_bit_p (insns, DF_REF_INSN_UID (def->ref)))
    1440              :                       break;
    1441          821 :                   if (def)
    1442          164 :                     to_reset_debug_insns.safe_push (debug_insn);
    1443              :                   else
    1444              :                     {
    1445          657 :                       *DF_REF_REAL_LOC (use->ref)
    1446          657 :                         = *defs_map.get (regno_reg_rtx [DF_REF_REGNO (ref)]);
    1447          657 :                       df_insn_rescan (debug_insn);
    1448              :                     }
    1449              :                 }
    1450              :             /* Have to do the reset outside of the DF_CHAIN walk to not
    1451              :                disrupt it.  */
    1452         2728 :             while (!to_reset_debug_insns.is_empty ())
    1453              :               {
    1454          164 :                 rtx_insn *debug_insn = to_reset_debug_insns.pop ();
    1455          164 :                 INSN_VAR_LOCATION_LOC (debug_insn) = gen_rtx_UNKNOWN_VAR_LOC ();
    1456          164 :                 df_insn_rescan_debug_internal (debug_insn);
    1457              :               }
    1458         1282 :           }
    1459              :       }
    1460              : 
    1461              :   /* Replace uses in this insn with the defs we use in the chain.  */
    1462      3429338 :   for (df_ref ref = DF_INSN_USES (insn); ref; ref = DF_REF_NEXT_LOC (ref))
    1463      2053004 :     if (!DF_REF_REG_MEM_P (ref))
    1464       734121 :       if (rtx *vreg = defs_map.get (regno_reg_rtx[DF_REF_REGNO (ref)]))
    1465              :         {
    1466              :           /* Also update a corresponding REG_DEAD note.  */
    1467        36699 :           rtx note = find_reg_note (insn, REG_DEAD, DF_REF_REG (ref));
    1468        36699 :           if (note)
    1469        22928 :             XEXP (note, 0) = *vreg;
    1470        36699 :           *DF_REF_REAL_LOC (ref) = *vreg;
    1471              :         }
    1472      1376334 : }
    1473              : 
    1474              : /* Convert INSN which is an SImode or DImode rotation by a constant
    1475              :    to vector mode.  CODE is either ROTATE or ROTATERT with operands
    1476              :    OP0 and OP1.  Returns the SET_SRC of the last instruction in the
    1477              :    resulting sequence, which is emitted before INSN.  */
    1478              : 
    1479              : rtx
    1480           92 : general_scalar_chain::convert_rotate (enum rtx_code code, rtx op0, rtx op1,
    1481              :                                       rtx_insn *insn)
    1482              : {
    1483           92 :   int bits = INTVAL (op1);
    1484           92 :   rtx pat, result;
    1485              : 
    1486           92 :   convert_op (&op0, insn);
    1487           92 :   if (bits == 0)
    1488            0 :     return op0;
    1489              : 
    1490           92 :   if (smode == DImode)
    1491              :     {
    1492           92 :       if (code == ROTATE)
    1493           45 :         bits = 64 - bits;
    1494           92 :       if (bits == 32)
    1495              :         {
    1496            0 :           rtx tmp1 = gen_reg_rtx (V4SImode);
    1497            0 :           pat = gen_sse2_pshufd (tmp1, gen_lowpart (V4SImode, op0),
    1498              :                                  GEN_INT (225));
    1499            0 :           emit_insn_before (pat, insn);
    1500            0 :           result = gen_lowpart (V2DImode, tmp1);
    1501              :         }
    1502           92 :       else if (TARGET_AVX512VL)
    1503            0 :         result = simplify_gen_binary (code, V2DImode, op0, op1);
    1504           92 :       else if (bits == 16 || bits == 48)
    1505              :         {
    1506            0 :           rtx tmp1 = gen_reg_rtx (V8HImode);
    1507            0 :           pat = gen_sse2_pshuflw (tmp1, gen_lowpart (V8HImode, op0),
    1508              :                                   GEN_INT (bits == 16 ? 57 : 147));
    1509            0 :           emit_insn_before (pat, insn);
    1510            0 :           result = gen_lowpart (V2DImode, tmp1);
    1511              :         }
    1512           92 :       else if ((bits & 0x07) == 0)
    1513              :         {
    1514            0 :           rtx tmp1 = gen_reg_rtx (V4SImode);
    1515            0 :           pat = gen_sse2_pshufd (tmp1, gen_lowpart (V4SImode, op0),
    1516              :                                  GEN_INT (68));
    1517            0 :           emit_insn_before (pat, insn);
    1518            0 :           rtx tmp2 = gen_reg_rtx (V1TImode);
    1519            0 :           pat = gen_sse2_lshrv1ti3 (tmp2, gen_lowpart (V1TImode, tmp1),
    1520              :                                     GEN_INT (bits));
    1521            0 :           emit_insn_before (pat, insn);
    1522            0 :           result = gen_lowpart (V2DImode, tmp2);
    1523              :         }
    1524              :       else
    1525              :         {
    1526           92 :           rtx tmp1 = gen_reg_rtx (V4SImode);
    1527           92 :           pat = gen_sse2_pshufd (tmp1, gen_lowpart (V4SImode, op0),
    1528              :                                  GEN_INT (20));
    1529           92 :           emit_insn_before (pat, insn);
    1530           92 :           rtx tmp2 = gen_reg_rtx (V2DImode);
    1531           92 :           pat = gen_lshrv2di3 (tmp2, gen_lowpart (V2DImode, tmp1),
    1532              :                                GEN_INT (bits & 31));
    1533           92 :           emit_insn_before (pat, insn);
    1534           92 :           rtx tmp3 = gen_reg_rtx (V4SImode);
    1535          139 :           pat = gen_sse2_pshufd (tmp3, gen_lowpart (V4SImode, tmp2),
    1536              :                                  GEN_INT (bits > 32 ? 34 : 136));
    1537           92 :           emit_insn_before (pat, insn);
    1538           92 :           result = gen_lowpart (V2DImode, tmp3);
    1539              :         }
    1540              :     }
    1541            0 :   else if (bits == 16)
    1542              :     {
    1543            0 :       rtx tmp1 = gen_reg_rtx (V8HImode);
    1544            0 :       pat = gen_sse2_pshuflw (tmp1, gen_lowpart (V8HImode, op0), GEN_INT (225));
    1545            0 :       emit_insn_before (pat, insn);
    1546            0 :       result = gen_lowpart (V4SImode, tmp1);
    1547              :     }
    1548            0 :   else if (TARGET_AVX512VL)
    1549            0 :     result = simplify_gen_binary (code, V4SImode, op0, op1);
    1550              :   else
    1551              :     {
    1552            0 :       if (code == ROTATE)
    1553            0 :         bits = 32 - bits;
    1554              : 
    1555            0 :       rtx tmp1 = gen_reg_rtx (V4SImode);
    1556            0 :       emit_insn_before (gen_sse2_pshufd (tmp1, op0, GEN_INT (224)), insn);
    1557            0 :       rtx tmp2 = gen_reg_rtx (V2DImode);
    1558            0 :       pat = gen_lshrv2di3 (tmp2, gen_lowpart (V2DImode, tmp1),
    1559              :                            GEN_INT (bits));
    1560            0 :       emit_insn_before (pat, insn);
    1561            0 :       result = gen_lowpart (V4SImode, tmp2);
    1562              :     }
    1563              : 
    1564              :   return result;
    1565              : }
    1566              : 
    1567              : /* Convert INSN to vector mode.  */
    1568              : 
    1569              : void
    1570       408449 : general_scalar_chain::convert_insn (rtx_insn *insn)
    1571              : {
    1572       408449 :   rtx def_set = single_set (insn);
    1573       408449 :   rtx src = SET_SRC (def_set);
    1574       408449 :   rtx dst = SET_DEST (def_set);
    1575       408449 :   rtx subreg;
    1576              : 
    1577       408449 :   if (MEM_P (dst) && !REG_P (src))
    1578              :     {
    1579              :       /* There are no scalar integer instructions and therefore
    1580              :          temporary register usage is required.  */
    1581          740 :       rtx tmp = gen_reg_rtx (smode);
    1582          740 :       emit_conversion_insns (gen_move_insn (dst, tmp), insn);
    1583          740 :       dst = gen_rtx_SUBREG (vmode, tmp, 0);
    1584          740 :     }
    1585       407709 :   else if (REG_P (dst) && GET_MODE (dst) == smode)
    1586              :     {
    1587              :       /* Replace the definition with a SUBREG to the definition we
    1588              :          use inside the chain.  */
    1589       212470 :       rtx *vdef = defs_map.get (dst);
    1590       212470 :       if (vdef)
    1591        25173 :         dst = *vdef;
    1592       212470 :       dst = gen_rtx_SUBREG (vmode, dst, 0);
    1593              :       /* IRA doesn't like to have REG_EQUAL/EQUIV notes when the SET_DEST
    1594              :          is a non-REG_P.  So kill those off.  */
    1595       212470 :       rtx note = find_reg_equal_equiv_note (insn);
    1596       212470 :       if (note)
    1597         9782 :         remove_note (insn, note);
    1598              :     }
    1599              : 
    1600       408449 :   switch (GET_CODE (src))
    1601              :     {
    1602        26685 :     case PLUS:
    1603        26685 :     case MINUS:
    1604        26685 :     case IOR:
    1605        26685 :     case XOR:
    1606        26685 :     case AND:
    1607        26685 :     case SMAX:
    1608        26685 :     case SMIN:
    1609        26685 :     case UMAX:
    1610        26685 :     case UMIN:
    1611        26685 :       convert_op (&XEXP (src, 1), insn);
    1612              :       /* FALLTHRU */
    1613              : 
    1614        33289 :     case ABS:
    1615        33289 :     case ASHIFT:
    1616        33289 :     case ASHIFTRT:
    1617        33289 :     case LSHIFTRT:
    1618        33289 :       convert_op (&XEXP (src, 0), insn);
    1619        33289 :       PUT_MODE (src, vmode);
    1620        33289 :       break;
    1621              : 
    1622           92 :     case ROTATE:
    1623           92 :     case ROTATERT:
    1624           92 :       src = convert_rotate (GET_CODE (src), XEXP (src, 0), XEXP (src, 1),
    1625              :                             insn);
    1626           92 :       break;
    1627              : 
    1628          351 :     case NEG:
    1629          351 :       src = XEXP (src, 0);
    1630              : 
    1631          351 :       if (GET_CODE (src) == ABS)
    1632              :         {
    1633            0 :           src = XEXP (src, 0);
    1634            0 :           convert_op (&src, insn);
    1635            0 :           subreg = gen_reg_rtx (vmode);
    1636            0 :           emit_insn_before (gen_rtx_SET (subreg,
    1637              :                                          gen_rtx_ABS (vmode, src)), insn);
    1638            0 :           src = subreg;
    1639              :         }
    1640              :       else
    1641          351 :         convert_op (&src, insn);
    1642              : 
    1643          351 :       subreg = gen_reg_rtx (vmode);
    1644          351 :       emit_insn_before (gen_move_insn (subreg, CONST0_RTX (vmode)), insn);
    1645          351 :       src = gen_rtx_MINUS (vmode, subreg, src);
    1646          351 :       break;
    1647              : 
    1648          219 :     case NOT:
    1649          219 :       src = XEXP (src, 0);
    1650          219 :       convert_op (&src, insn);
    1651          219 :       subreg = gen_reg_rtx (vmode);
    1652          219 :       emit_insn_before (gen_move_insn (subreg, CONSTM1_RTX (vmode)), insn);
    1653          219 :       src = gen_rtx_XOR (vmode, src, subreg);
    1654          219 :       break;
    1655              : 
    1656       170823 :     case MEM:
    1657       170823 :       if (!REG_P (dst))
    1658       170823 :         convert_op (&src, insn);
    1659              :       break;
    1660              : 
    1661       196757 :     case REG:
    1662       196757 :       if (!MEM_P (dst))
    1663         1518 :         convert_op (&src, insn);
    1664              :       break;
    1665              : 
    1666            0 :     case SUBREG:
    1667            0 :       gcc_assert (GET_MODE (src) == vmode);
    1668              :       break;
    1669              : 
    1670            0 :     case COMPARE:
    1671            0 :       dst = gen_rtx_REG (CCZmode, FLAGS_REG);
    1672            0 :       src = convert_compare (XEXP (src, 0), XEXP (src, 1), insn);
    1673            0 :       break;
    1674              : 
    1675         3788 :     case CONST_INT:
    1676         3788 :       convert_op (&src, insn);
    1677         3788 :       break;
    1678              : 
    1679         2801 :     case VEC_SELECT:
    1680         2801 :       if (XVECEXP (XEXP (src, 1), 0, 0) == const0_rtx)
    1681         1622 :         src = XEXP (src, 0);
    1682         1179 :       else if (smode == DImode)
    1683              :         {
    1684          807 :           rtx tmp = gen_lowpart (V1TImode, XEXP (src, 0));
    1685          807 :           dst = gen_lowpart (V1TImode, dst);
    1686          807 :           src = gen_rtx_LSHIFTRT (V1TImode, tmp, GEN_INT (64));
    1687              :         }
    1688              :       else
    1689              :         {
    1690          372 :           rtx tmp = XVECEXP (XEXP (src, 1), 0, 0);
    1691          372 :           rtvec vec = gen_rtvec (4, tmp, tmp, tmp, tmp);
    1692          372 :           rtx par = gen_rtx_PARALLEL (VOIDmode, vec);
    1693          372 :           src = gen_rtx_VEC_SELECT (vmode, XEXP (src, 0), par);
    1694              :         }
    1695              :       break;
    1696              : 
    1697          329 :     case ZERO_EXTEND:
    1698              :       /* *zero_extendsidi2 becomes *vec_setv2di_0_zero_extendsi_1.  */
    1699          329 :       src = gen_rtx_VEC_CONCAT (V2DImode, src, const0_rtx);
    1700          329 :       break;
    1701              : 
    1702            0 :     default:
    1703            0 :       gcc_unreachable ();
    1704              :     }
    1705              : 
    1706       408449 :   SET_SRC (def_set) = src;
    1707       408449 :   SET_DEST (def_set) = dst;
    1708              : 
    1709              :   /* Drop possible dead definitions.  */
    1710       408449 :   PATTERN (insn) = def_set;
    1711              : 
    1712       408449 :   INSN_CODE (insn) = -1;
    1713       408449 :   int patt = recog_memoized (insn);
    1714       408449 :   if  (patt == -1)
    1715            0 :     fatal_insn_not_found (insn);
    1716       408449 :   df_insn_rescan (insn);
    1717       408449 : }
    1718              : 
    1719              : /* Helper function to compute gain for loading an immediate constant.
    1720              :    Typically, two movabsq for TImode vs. vmovdqa for V1TImode, but
    1721              :    with numerous special cases.  */
    1722              : 
    1723              : static int
    1724           34 : timode_immed_const_gain (rtx cst, basic_block bb)
    1725              : {
    1726              :   /* movabsq vs. movabsq+vmovq+vunpacklqdq.  */
    1727           34 :   if (CONST_WIDE_INT_P (cst)
    1728           23 :       && CONST_WIDE_INT_NUNITS (cst) == 2
    1729           57 :       && CONST_WIDE_INT_ELT (cst, 0) == CONST_WIDE_INT_ELT (cst, 1))
    1730            0 :     return optimize_bb_for_size_p (bb) ? -COSTS_N_BYTES (9)
    1731              :                                        : -COSTS_N_INSNS (2);
    1732              :   /* 2x movabsq ~ vmovdqa.  */
    1733              :   return 0;
    1734              : }
    1735              : 
    1736              : /* Return true it's cost profitable for for chain conversion.  */
    1737              : 
    1738              : bool
    1739       531766 : timode_scalar_chain::compute_convert_gain ()
    1740              : {
    1741              :   /* Assume that if we have to move TImode values between units,
    1742              :      then transforming this chain isn't worth it.  */
    1743       531766 :   if (cost_sse_integer)
    1744              :     return false;
    1745              : 
    1746       531766 :   bitmap_iterator bi;
    1747       531766 :   unsigned insn_uid;
    1748              : 
    1749              :   /* Split ties to prefer V1TImode when not optimizing for size.  */
    1750       531766 :   int gain = optimize_size ? 0 : 1;
    1751       531766 :   sreal weighted_gain  = 0;
    1752              : 
    1753       531766 :   if (dump_file)
    1754            0 :     fprintf (dump_file, "Computing gain for chain #%d...\n", chain_id);
    1755              : 
    1756      1589499 :   EXECUTE_IF_SET_IN_BITMAP (insns, 0, insn_uid, bi)
    1757              :     {
    1758      1057733 :       rtx_insn *insn = DF_INSN_UID_GET (insn_uid)->insn;
    1759      1057733 :       rtx def_set = single_set (insn);
    1760      1057733 :       rtx src = SET_SRC (def_set);
    1761      1057733 :       rtx dst = SET_DEST (def_set);
    1762      1057733 :       HOST_WIDE_INT op1val;
    1763      1057733 :       basic_block bb = BLOCK_FOR_INSN (insn);
    1764      1057733 :       int scost, vcost;
    1765      1057733 :       int igain = 0;
    1766      1057733 :       profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
    1767      1057733 :       bool speed_p = optimize_bb_for_speed_p (bb);
    1768      1057733 :       sreal bb_freq = bb->count.to_sreal_scale (entry_count);
    1769              : 
    1770      1057733 :       switch (GET_CODE (src))
    1771              :         {
    1772       550185 :         case REG:
    1773       550185 :           if (GENERAL_REGNO_P (REGNO (src)))
    1774              :             {
    1775        24975 :               if (TARGET_SSE4_1)
    1776              :                 /* movq + pinsrq */
    1777           27 :                 igain = speed_p ? -ix86_cost->integer_to_sse
    1778              :                                   - COSTS_N_INSNS (1)
    1779              :                                 : -COSTS_N_BYTES (11);
    1780              :               else
    1781              :                 /* movq + movq + punpcklqdq */
    1782        24948 :                 igain = speed_p ? -ix86_cost->integer_to_sse
    1783              :                                   - COSTS_N_INSNS (2)
    1784              :                                 : -COSTS_N_BYTES (14);
    1785              :             }
    1786       525210 :           else if (GENERAL_REG_P (dst))
    1787              :             {
    1788        25140 :               if (TARGET_SSE4_1)
    1789              :                 /* pextrq + movq */
    1790           27 :                 igain = speed_p ? -ix86_cost->sse_to_integer
    1791              :                                   - COSTS_N_INSNS (1)
    1792              :                                 : -COSTS_N_BYTES (11);
    1793              :               else
    1794              :                 /* movhlps + movq + movq */
    1795        25113 :                 igain = speed_p ? -ix86_cost->sse_to_integer
    1796              :                                   - COSTS_N_INSNS (2)
    1797              :                                 : -COSTS_N_BYTES (13);
    1798              :             }
    1799       500070 :           else if (!speed_p)
    1800        16226 :             igain = MEM_P (dst) ? COSTS_N_BYTES (6) : COSTS_N_BYTES (3);
    1801              :           else
    1802              :             igain = COSTS_N_INSNS (1);
    1803              :           break;
    1804              : 
    1805       457331 :         case MEM:
    1806       457331 :           igain = !speed_p ? COSTS_N_BYTES (7) : COSTS_N_INSNS (1);
    1807              :           break;
    1808              : 
    1809        12873 :         case CONST_INT:
    1810        12873 :           if (MEM_P (dst)
    1811        12873 :               && standard_sse_constant_p (src, V1TImode))
    1812        12341 :             igain = !speed_p ? COSTS_N_BYTES (11) : 1;
    1813              :           break;
    1814              : 
    1815        31948 :         case CONST_WIDE_INT:
    1816              :           /* 2 x mov vs. vmovdqa.  */
    1817        31948 :           if (MEM_P (dst))
    1818        31377 :             igain = !speed_p ? COSTS_N_BYTES (3) : COSTS_N_INSNS (1);
    1819              :           break;
    1820              : 
    1821           78 :         case NOT:
    1822           78 :           if (MEM_P (dst))
    1823        69160 :             igain = -COSTS_N_INSNS (1);
    1824              :           break;
    1825              : 
    1826           69 :         case AND:
    1827           69 :           if (!MEM_P (dst))
    1828              :             {
    1829           58 :               igain = COSTS_N_INSNS (1);
    1830           58 :               if (MEM_P (XEXP (src, 0)) || MEM_P (XEXP (src, 1)))
    1831              :                 /* One less load.  */
    1832           20 :                 igain += COSTS_N_INSNS (1);
    1833              :             }
    1834           69 :           if (CONST_SCALAR_INT_P (XEXP (src, 1)))
    1835           27 :             igain += timode_immed_const_gain (XEXP (src, 1), bb);
    1836              :           break;
    1837              : 
    1838         4521 :         case XOR:
    1839         4521 :         case IOR:
    1840         4521 :           if (timode_concatdi_p (src))
    1841              :             {
    1842              :               /* vmovq;vpinsrq (11 bytes).  */
    1843         4363 :               igain = speed_p ? -ix86_cost->integer_to_sse - COSTS_N_INSNS (1)
    1844              :                               : -COSTS_N_BYTES (11);
    1845              :               break;
    1846              :             }
    1847          158 :           if (!MEM_P (dst))
    1848              :             {
    1849          150 :               igain = COSTS_N_INSNS (1);
    1850          150 :               if (MEM_P (XEXP (src, 0)) || MEM_P (XEXP (src, 1)))
    1851              :                 /* One less load.  */
    1852           48 :                 igain += COSTS_N_INSNS (1);
    1853              :             }
    1854          158 :           if (CONST_SCALAR_INT_P (XEXP (src, 1)))
    1855            7 :             igain += timode_immed_const_gain (XEXP (src, 1), bb);
    1856              :           break;
    1857              : 
    1858            0 :         case PLUS:
    1859            0 :           if (timode_concatdi_p (src))
    1860              :             /* vmovq;vpinsrq (11 bytes).  */
    1861            0 :             igain = speed_p ? -ix86_cost->integer_to_sse - COSTS_N_INSNS (1)
    1862              :                             : -COSTS_N_BYTES (11);
    1863              :           break;
    1864              : 
    1865          231 :         case ASHIFT:
    1866          231 :         case LSHIFTRT:
    1867              :           /* See ix86_expand_v1ti_shift.  */
    1868          231 :           op1val = INTVAL (XEXP (src, 1));
    1869          231 :           if (!speed_p)
    1870              :             {
    1871           21 :               if (op1val == 64 || op1val == 65)
    1872              :                 scost = COSTS_N_BYTES (5);
    1873           13 :               else if (op1val >= 66)
    1874              :                 scost = COSTS_N_BYTES (6);
    1875           13 :               else if (op1val == 1)
    1876              :                 scost = COSTS_N_BYTES (8);
    1877              :               else
    1878              :                 scost = COSTS_N_BYTES (9);
    1879              : 
    1880           19 :               if ((op1val & 7) == 0)
    1881              :                 vcost = COSTS_N_BYTES (5);
    1882           13 :               else if (op1val > 64)
    1883              :                 vcost = COSTS_N_BYTES (10);
    1884              :               else
    1885           13 :                 vcost = TARGET_AVX ? COSTS_N_BYTES (19) : COSTS_N_BYTES (23);
    1886              :             }
    1887              :           else
    1888              :             {
    1889          210 :               scost = COSTS_N_INSNS (2);
    1890          210 :               if ((op1val & 7) == 0)
    1891              :                 vcost = COSTS_N_INSNS (1);
    1892          131 :               else if (op1val > 64)
    1893              :                 vcost = COSTS_N_INSNS (2);
    1894              :               else
    1895          131 :                 vcost = TARGET_AVX ? COSTS_N_INSNS (4) : COSTS_N_INSNS (5);
    1896              :             }
    1897          231 :           igain = scost - vcost;
    1898          231 :           break;
    1899              : 
    1900          124 :         case ASHIFTRT:
    1901              :           /* See ix86_expand_v1ti_ashiftrt.  */
    1902          124 :           op1val = INTVAL (XEXP (src, 1));
    1903          124 :           if (!speed_p)
    1904              :             {
    1905            9 :               if (op1val == 64 || op1val == 127)
    1906              :                 scost = COSTS_N_BYTES (7);
    1907            9 :               else if (op1val == 1)
    1908              :                 scost = COSTS_N_BYTES (8);
    1909            8 :               else if (op1val == 65)
    1910              :                 scost = COSTS_N_BYTES (10);
    1911            8 :               else if (op1val >= 66)
    1912              :                 scost = COSTS_N_BYTES (11);
    1913              :               else
    1914              :                 scost = COSTS_N_BYTES (9);
    1915              : 
    1916            0 :               if (op1val == 127)
    1917              :                 vcost = COSTS_N_BYTES (10);
    1918            9 :               else if (op1val == 64)
    1919              :                 vcost = COSTS_N_BYTES (14);
    1920            9 :               else if (op1val == 96)
    1921              :                 vcost = COSTS_N_BYTES (18);
    1922            9 :               else if (op1val >= 111)
    1923              :                 vcost = COSTS_N_BYTES (15);
    1924            9 :               else if (TARGET_AVX2 && op1val == 32)
    1925              :                 vcost = COSTS_N_BYTES (16);
    1926            9 :               else if (TARGET_SSE4_1 && op1val == 32)
    1927              :                 vcost = COSTS_N_BYTES (20);
    1928            9 :               else if (op1val >= 96)
    1929              :                 vcost = COSTS_N_BYTES (23);
    1930            9 :               else if ((op1val & 7) == 0)
    1931              :                 vcost = COSTS_N_BYTES (28);
    1932            9 :               else if (TARGET_AVX2 && op1val < 32)
    1933              :                 vcost = COSTS_N_BYTES (30);
    1934            9 :               else if (op1val == 1 || op1val >= 64)
    1935              :                 vcost = COSTS_N_BYTES (42);
    1936              :               else
    1937            8 :                 vcost = COSTS_N_BYTES (47);
    1938              :             }
    1939              :           else
    1940              :             {
    1941          115 :               if (op1val >= 65 && op1val <= 126)
    1942              :                 scost = COSTS_N_INSNS (3);
    1943              :               else
    1944          115 :                 scost = COSTS_N_INSNS (2);
    1945              : 
    1946          115 :               if (op1val == 127)
    1947              :                 vcost = COSTS_N_INSNS (2);
    1948          114 :               else if (op1val == 64)
    1949              :                 vcost = COSTS_N_INSNS (3);
    1950          114 :               else if (op1val == 96)
    1951              :                 vcost = COSTS_N_INSNS (3);
    1952          114 :               else if (op1val >= 111)
    1953              :                 vcost = COSTS_N_INSNS (3);
    1954          114 :               else if (TARGET_SSE4_1 && op1val == 32)
    1955              :                 vcost = COSTS_N_INSNS (3);
    1956          114 :               else if (TARGET_SSE4_1
    1957            0 :                        && (op1val == 8 || op1val == 16 || op1val == 24))
    1958              :                 vcost = COSTS_N_INSNS (3);
    1959          114 :               else if (op1val >= 96)
    1960              :                 vcost = COSTS_N_INSNS (4);
    1961          114 :               else if (TARGET_SSE4_1 && (op1val == 28 || op1val == 80))
    1962              :                 vcost = COSTS_N_INSNS (4);
    1963          114 :               else if ((op1val & 7) == 0)
    1964              :                 vcost = COSTS_N_INSNS (5);
    1965          114 :               else if (TARGET_AVX2 && op1val < 32)
    1966              :                 vcost = COSTS_N_INSNS (6);
    1967          114 :               else if (TARGET_SSE4_1 && op1val < 15)
    1968              :                 vcost = COSTS_N_INSNS (6);
    1969          114 :               else if (op1val == 1 || op1val >= 64)
    1970              :                 vcost = COSTS_N_INSNS (8);
    1971              :               else
    1972           17 :                 vcost = COSTS_N_INSNS (9);
    1973              :             }
    1974          124 :           igain = scost - vcost;
    1975          124 :           break;
    1976              : 
    1977            6 :         case ROTATE:
    1978            6 :         case ROTATERT:
    1979              :           /* See ix86_expand_v1ti_rotate.  */
    1980            6 :           op1val = INTVAL (XEXP (src, 1));
    1981            6 :           if (!speed_p)
    1982              :             {
    1983            0 :               scost = COSTS_N_BYTES (13);
    1984            0 :               if ((op1val & 31) == 0)
    1985              :                 vcost = COSTS_N_BYTES (5);
    1986            0 :               else if ((op1val & 7) == 0)
    1987            0 :                 vcost = TARGET_AVX ? COSTS_N_BYTES (13) : COSTS_N_BYTES (18);
    1988            0 :               else if (op1val > 32 && op1val < 96)
    1989              :                 vcost = COSTS_N_BYTES (24);
    1990              :               else
    1991            0 :                 vcost = COSTS_N_BYTES (19);
    1992              :             }
    1993              :           else
    1994              :             {
    1995            6 :               scost = COSTS_N_INSNS (3);
    1996            6 :               if ((op1val & 31) == 0)
    1997              :                 vcost = COSTS_N_INSNS (1);
    1998            4 :               else if ((op1val & 7) == 0)
    1999            1 :                 vcost = TARGET_AVX ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4);
    2000            3 :               else if (op1val > 32 && op1val < 96)
    2001              :                 vcost = COSTS_N_INSNS (5);
    2002              :               else
    2003            3 :                 vcost = COSTS_N_INSNS (4);
    2004              :             }
    2005            6 :           igain = scost - vcost;
    2006            6 :           break;
    2007              : 
    2008           13 :         case COMPARE:
    2009           13 :           if (XEXP (src, 1) == const0_rtx)
    2010              :             {
    2011            2 :               if (GET_CODE (XEXP (src, 0)) == AND)
    2012              :                 /* and;and;or (9 bytes) vs. ptest (5 bytes).  */
    2013              :                 igain = !speed_p ? COSTS_N_BYTES (4) : COSTS_N_INSNS (2);
    2014              :               /* or (3 bytes) vs. ptest (5 bytes).  */
    2015            2 :               else if (!speed_p)
    2016            0 :                 igain = -COSTS_N_BYTES (2);
    2017              :             }
    2018           11 :           else if (XEXP (src, 1) == const1_rtx)
    2019              :             /* and;cmp -1 (7 bytes) vs. pcmpeqd;pxor;ptest (13 bytes).  */
    2020            0 :             igain = !speed_p ? -COSTS_N_BYTES (6) : -COSTS_N_INSNS (1);
    2021              :           break;
    2022              : 
    2023          354 :         case ZERO_EXTEND:
    2024          354 :           if (GET_MODE (XEXP (src, 0)) == DImode)
    2025              :             /* xor (2 bytes) vs. vmovq (5 bytes).  */
    2026          354 :             igain = speed_p ? COSTS_N_INSNS (1) - ix86_cost->sse_to_integer
    2027              :                             : -COSTS_N_BYTES (3);
    2028              :           break;
    2029              : 
    2030              :         default:
    2031              :           break;
    2032              :         }
    2033              : 
    2034      2080305 :       gain += igain;
    2035      1057731 :       if (speed_p)
    2036      1022574 :         weighted_gain += bb_freq * igain;
    2037              : 
    2038      1057733 :       if (igain != 0 && dump_file)
    2039              :         {
    2040            0 :           fprintf (dump_file, "  Instruction gain %d with bb_freq %.2f for ",
    2041              :                    igain, bb_freq.to_double ());
    2042            0 :           dump_insn_slim (dump_file, insn);
    2043              :         }
    2044              :     }
    2045              : 
    2046       531766 :   if (dump_file)
    2047            0 :     fprintf (dump_file, "  Total gain: %d, weighted gain %.2f\n",
    2048              :              gain, weighted_gain.to_double ());
    2049              : 
    2050       531766 :   if (weighted_gain > (sreal) 0)
    2051              :     return true;
    2052              :   else
    2053        56857 :     return gain > 0;
    2054              : }
    2055              : 
    2056              : /* Fix uses of converted REG in debug insns.  */
    2057              : 
    2058              : void
    2059       446977 : timode_scalar_chain::fix_debug_reg_uses (rtx reg)
    2060              : {
    2061       446977 :   if (!flag_var_tracking)
    2062              :     return;
    2063              : 
    2064       390084 :   df_ref ref, next;
    2065       799321 :   for (ref = DF_REG_USE_CHAIN (REGNO (reg)); ref; ref = next)
    2066              :     {
    2067       409237 :       rtx_insn *insn = DF_REF_INSN (ref);
    2068              :       /* Make sure the next ref is for a different instruction,
    2069              :          so that we're not affected by the rescan.  */
    2070       409237 :       next = DF_REF_NEXT_REG (ref);
    2071       409237 :       while (next && DF_REF_INSN (next) == insn)
    2072            0 :         next = DF_REF_NEXT_REG (next);
    2073              : 
    2074       409237 :       if (DEBUG_INSN_P (insn))
    2075              :         {
    2076              :           /* It may be a debug insn with a TImode variable in
    2077              :              register.  */
    2078              :           bool changed = false;
    2079          246 :           for (; ref != next; ref = DF_REF_NEXT_REG (ref))
    2080              :             {
    2081          123 :               rtx *loc = DF_REF_LOC (ref);
    2082          123 :               if (REG_P (*loc) && GET_MODE (*loc) == V1TImode)
    2083              :                 {
    2084          114 :                   *loc = gen_rtx_SUBREG (TImode, *loc, 0);
    2085          114 :                   changed = true;
    2086              :                 }
    2087              :             }
    2088          123 :           if (changed)
    2089          114 :             df_insn_rescan (insn);
    2090              :         }
    2091              :     }
    2092              : }
    2093              : 
    2094              : /* Convert SRC, a *concatditi3 pattern, into a vec_concatv2di instruction.
    2095              :    Insert this before INSN, and return the result as a V1TImode subreg.  */
    2096              : 
    2097              : static rtx
    2098          284 : timode_convert_concatdi (rtx src, rtx_insn *insn)
    2099              : {
    2100          284 :   rtx hi, lo;
    2101          284 :   rtx tmp = gen_reg_rtx (V2DImode);
    2102          284 :   if (GET_CODE (XEXP (src, 0)) == ASHIFT)
    2103              :     {
    2104          284 :       hi = XEXP (XEXP (XEXP (src, 0), 0), 0);
    2105          284 :       lo = XEXP (XEXP (src, 1), 0);
    2106              :     }
    2107              :   else
    2108              :     {
    2109            0 :       hi = XEXP (XEXP (XEXP (src, 1), 0), 0);
    2110            0 :       lo = XEXP (XEXP (src, 0), 0);
    2111              :     }
    2112          284 :   emit_insn_before (gen_vec_concatv2di (tmp, lo, hi), insn);
    2113          284 :   return gen_rtx_SUBREG (V1TImode, tmp, 0);
    2114              : }
    2115              : 
    2116              : /* Convert INSN from TImode to V1T1mode.  */
    2117              : 
    2118              : void
    2119       967885 : timode_scalar_chain::convert_insn (rtx_insn *insn)
    2120              : {
    2121       967885 :   rtx def_set = single_set (insn);
    2122       967885 :   rtx src = SET_SRC (def_set);
    2123       967885 :   rtx dst = SET_DEST (def_set);
    2124       967885 :   rtx tmp;
    2125              : 
    2126       967885 :   switch (GET_CODE (dst))
    2127              :     {
    2128       447498 :     case REG:
    2129       447498 :       if (GET_MODE (dst) == TImode)
    2130              :         {
    2131       446897 :           if (!HARD_REGISTER_NUM_P (REGNO (dst)))
    2132              :             {
    2133       446384 :               PUT_MODE (dst, V1TImode);
    2134       446384 :               fix_debug_reg_uses (dst);
    2135              :             }
    2136          513 :           else if (!GENERAL_REGNO_P (REGNO (dst)))
    2137          359 :             dst = gen_raw_REG (V1TImode, REGNO (dst));
    2138              :         }
    2139       447498 :       if (GET_MODE (dst) == V1TImode)
    2140              :         {
    2141              :           /* It might potentially be helpful to convert REG_EQUAL notes,
    2142              :              but for now we just remove them.  */
    2143       447336 :           rtx note = find_reg_equal_equiv_note (insn);
    2144       447336 :           if (note)
    2145          483 :             remove_note (insn, note);
    2146              :         }
    2147              :       break;
    2148       520387 :     case MEM:
    2149       520387 :       PUT_MODE (dst, V1TImode);
    2150       520387 :       break;
    2151              : 
    2152            0 :     default:
    2153            0 :       gcc_unreachable ();
    2154              :     }
    2155              : 
    2156       967885 :   switch (GET_CODE (src))
    2157              :     {
    2158       477513 :     case REG:
    2159       477513 :       if (GET_MODE (src) == TImode)
    2160              :         {
    2161          821 :           if (GENERAL_REGNO_P (REGNO (src)))
    2162              :             {
    2163          223 :               rtx lo = gen_reg_rtx (DImode);
    2164          223 :               rtx hi = gen_reg_rtx (DImode);
    2165          223 :               emit_insn_before (gen_rtx_SET (lo, gen_lowpart (DImode, src)),
    2166              :                                 insn);
    2167          223 :               emit_insn_before (gen_rtx_SET (hi, gen_highpart (DImode, src)),
    2168              :                                 insn);
    2169          223 :               src = gen_reg_rtx (V2DImode);
    2170          223 :               emit_insn_before (gen_vec_concatv2di (src, lo, hi), insn);
    2171          223 :               src = gen_lowpart (V1TImode, src);
    2172              :             }
    2173          598 :           else if (!HARD_REGISTER_NUM_P (REGNO (src)))
    2174              :             {
    2175          593 :               PUT_MODE (src, V1TImode);
    2176          593 :               fix_debug_reg_uses (src);
    2177              :             }
    2178              :           else
    2179            5 :             src = gen_raw_REG (V1TImode, REGNO (src));
    2180              :         }
    2181       477513 :       if (GENERAL_REG_P (dst))
    2182              :         {
    2183          154 :           rtx tmp = gen_reg_rtx (V2DImode);
    2184          154 :           src = gen_lowpart (V2DImode, src);
    2185          154 :           emit_insn_before (gen_rtx_SET (tmp, src), insn);
    2186              :           /* Extracting hi before lo helps register allocation.  */
    2187          154 :           rtx hi = gen_reg_rtx (DImode);
    2188          154 :           rtx lo = gen_reg_rtx (DImode);
    2189          154 :           emit_insn_before (gen_vec_extractv2didi (hi, tmp, const1_rtx), insn);
    2190          154 :           emit_insn_before (gen_vec_extractv2didi (lo, tmp, const0_rtx), insn);
    2191              : 
    2192              :           /* Construct *concatditi3 pattern from lo and hi.  */
    2193          154 :           hi = gen_rtx_ZERO_EXTEND (TImode, hi);
    2194          154 :           hi = gen_rtx_ASHIFT (TImode, hi, GEN_INT (64));
    2195          154 :           lo = gen_rtx_ZERO_EXTEND (TImode, lo);
    2196          154 :           src = gen_rtx_PLUS (TImode, hi, lo);
    2197              :         }
    2198              :       break;
    2199              : 
    2200       445104 :     case MEM:
    2201       445104 :       PUT_MODE (src, V1TImode);
    2202       445104 :       break;
    2203              : 
    2204        31765 :     case CONST_WIDE_INT:
    2205        31765 :       if (NONDEBUG_INSN_P (insn))
    2206              :         {
    2207              :           /* Since there are no instructions to store 128-bit constant,
    2208              :              temporary register usage is required.  */
    2209        31765 :           bool use_move;
    2210        31765 :           start_sequence ();
    2211        31765 :           tmp = ix86_convert_const_wide_int_to_broadcast (TImode, src);
    2212        31765 :           if (tmp)
    2213              :             {
    2214          194 :               src = lowpart_subreg (V1TImode, tmp, TImode);
    2215          194 :               use_move = true;
    2216              :             }
    2217              :           else
    2218              :             {
    2219        31571 :               src = smode_convert_cst (src, V1TImode);
    2220        31571 :               src = validize_mem (force_const_mem (V1TImode, src));
    2221        31571 :               use_move = MEM_P (dst);
    2222              :             }
    2223        31765 :           rtx_insn *seq = end_sequence ();
    2224        31765 :           if (seq)
    2225          195 :             emit_insn_before (seq, insn);
    2226        31765 :           if (use_move)
    2227              :             {
    2228        31378 :               tmp = gen_reg_rtx (V1TImode);
    2229        31378 :               emit_insn_before (gen_rtx_SET (tmp, src), insn);
    2230        31378 :               src = tmp;
    2231              :             }
    2232              :         }
    2233              :       break;
    2234              : 
    2235        12873 :     case CONST_INT:
    2236        12873 :       switch (standard_sse_constant_p (src, TImode))
    2237              :         {
    2238        12644 :         case 1:
    2239        12644 :           src = CONST0_RTX (GET_MODE (dst));
    2240        12644 :           break;
    2241          229 :         case 2:
    2242          229 :           src = CONSTM1_RTX (GET_MODE (dst));
    2243          229 :           break;
    2244            0 :         default:
    2245            0 :           gcc_unreachable ();
    2246              :         }
    2247        12873 :       if (MEM_P (dst))
    2248              :         {
    2249        12341 :           tmp = gen_reg_rtx (V1TImode);
    2250        12341 :           emit_insn_before (gen_rtx_SET (tmp, src), insn);
    2251        12341 :           src = tmp;
    2252              :         }
    2253              :       break;
    2254              : 
    2255           41 :     case AND:
    2256           41 :       if (GET_CODE (XEXP (src, 0)) == NOT)
    2257              :         {
    2258            0 :           convert_op (&XEXP (XEXP (src, 0), 0), insn);
    2259            0 :           convert_op (&XEXP (src, 1), insn);
    2260            0 :           PUT_MODE (XEXP (src, 0), V1TImode);
    2261            0 :           PUT_MODE (src, V1TImode);
    2262            0 :           break;
    2263              :         }
    2264           41 :       convert_op (&XEXP (src, 0), insn);
    2265           41 :       convert_op (&XEXP (src, 1), insn);
    2266           41 :       PUT_MODE (src, V1TImode);
    2267           41 :       if (MEM_P (dst))
    2268              :         {
    2269           10 :           tmp = gen_reg_rtx (V1TImode);
    2270           10 :           emit_insn_before (gen_rtx_SET (tmp, src), insn);
    2271           10 :           src = tmp;
    2272              :         }
    2273              :       break;
    2274              : 
    2275          394 :     case XOR:
    2276          394 :     case IOR:
    2277          394 :       if (timode_concatdi_p (src))
    2278              :         {
    2279          284 :           src = timode_convert_concatdi (src, insn);
    2280          284 :           break;
    2281              :         }
    2282          110 :       convert_op (&XEXP (src, 0), insn);
    2283          110 :       convert_op (&XEXP (src, 1), insn);
    2284          110 :       PUT_MODE (src, V1TImode);
    2285          110 :       if (MEM_P (dst))
    2286              :         {
    2287            8 :           tmp = gen_reg_rtx (V1TImode);
    2288            8 :           emit_insn_before (gen_rtx_SET (tmp, src), insn);
    2289            8 :           src = tmp;
    2290              :         }
    2291              :       break;
    2292              : 
    2293            3 :     case NOT:
    2294            3 :       src = XEXP (src, 0);
    2295            3 :       convert_op (&src, insn);
    2296            3 :       tmp = gen_reg_rtx (V1TImode);
    2297            3 :       emit_insn_before (gen_move_insn (tmp, CONSTM1_RTX (V1TImode)), insn);
    2298            3 :       src = gen_rtx_XOR (V1TImode, src, tmp);
    2299            3 :       if (MEM_P (dst))
    2300              :         {
    2301            0 :           tmp = gen_reg_rtx (V1TImode);
    2302            0 :           emit_insn_before (gen_rtx_SET (tmp, src), insn);
    2303            0 :           src = tmp;
    2304              :         }
    2305              :       break;
    2306              : 
    2307            8 :     case COMPARE:
    2308            8 :       dst = gen_rtx_REG (CCZmode, FLAGS_REG);
    2309            8 :       src = convert_compare (XEXP (src, 0), XEXP (src, 1), insn);
    2310            8 :       break;
    2311              : 
    2312           68 :     case ASHIFT:
    2313           68 :     case LSHIFTRT:
    2314           68 :     case ASHIFTRT:
    2315           68 :     case ROTATERT:
    2316           68 :     case ROTATE:
    2317           68 :       convert_op (&XEXP (src, 0), insn);
    2318           68 :       PUT_MODE (src, V1TImode);
    2319           68 :       break;
    2320              : 
    2321          116 :     case ZERO_EXTEND:
    2322          116 :       if (GET_MODE (XEXP (src, 0)) == DImode)
    2323              :         {
    2324              :           /* Convert to *vec_concatv2di_0.  */
    2325          116 :           rtx tmp = gen_reg_rtx (V2DImode);
    2326          116 :           rtx pat = gen_rtx_VEC_CONCAT (V2DImode, XEXP (src, 0), const0_rtx);
    2327          116 :           emit_insn_before (gen_move_insn (tmp, pat), insn);
    2328          116 :           src = gen_rtx_SUBREG (vmode, tmp, 0);
    2329              :         }
    2330              :       else
    2331            0 :         gcc_unreachable ();
    2332          116 :       break;
    2333              : 
    2334            0 :     case PLUS:
    2335            0 :       if (timode_concatdi_p (src))
    2336            0 :         src = timode_convert_concatdi (src, insn);
    2337              :       else
    2338            0 :         gcc_unreachable ();
    2339            0 :       break;
    2340              : 
    2341            0 :     default:
    2342            0 :       gcc_unreachable ();
    2343              :     }
    2344              : 
    2345       967885 :   SET_SRC (def_set) = src;
    2346       967885 :   SET_DEST (def_set) = dst;
    2347              : 
    2348              :   /* Drop possible dead definitions.  */
    2349       967885 :   PATTERN (insn) = def_set;
    2350              : 
    2351       967885 :   INSN_CODE (insn) = -1;
    2352       967885 :   recog_memoized (insn);
    2353       967885 :   df_insn_rescan (insn);
    2354       967885 : }
    2355              : 
    2356              : /* Generate copies from defs used by the chain but not defined therein.
    2357              :    Also populates defs_map which is used later by convert_insn.  */
    2358              : 
    2359              : void
    2360       663750 : scalar_chain::convert_registers ()
    2361              : {
    2362       663750 :   bitmap_iterator bi;
    2363       663750 :   unsigned id;
    2364       691189 :   EXECUTE_IF_SET_IN_BITMAP (defs_conv, 0, id, bi)
    2365              :     {
    2366        27439 :       rtx chain_reg = gen_reg_rtx (smode);
    2367        27439 :       defs_map.put (regno_reg_rtx[id], chain_reg);
    2368              :     }
    2369       672590 :   EXECUTE_IF_SET_IN_BITMAP (insns_conv, 0, id, bi)
    2370        21247 :     for (df_ref ref = DF_INSN_UID_DEFS (id); ref; ref = DF_REF_NEXT_LOC (ref))
    2371        12407 :       if (bitmap_bit_p (defs_conv, DF_REF_REGNO (ref)))
    2372         8840 :         make_vector_copies (DF_REF_INSN (ref), DF_REF_REAL_REG (ref));
    2373       663750 : }
    2374              : 
    2375              : /* Convert whole chain creating required register
    2376              :    conversions and copies.  */
    2377              : 
    2378              : int
    2379       663750 : scalar_chain::convert ()
    2380              : {
    2381       663750 :   bitmap_iterator bi;
    2382       663750 :   unsigned id;
    2383       663750 :   int converted_insns = 0;
    2384              : 
    2385       663750 :   if (!dbg_cnt (stv_conversion))
    2386              :     return 0;
    2387              : 
    2388       663750 :   if (dump_file)
    2389            0 :     fprintf (dump_file, "Converting chain #%d...\n", chain_id);
    2390              : 
    2391       663750 :   convert_registers ();
    2392              : 
    2393      2040084 :   EXECUTE_IF_SET_IN_BITMAP (insns, 0, id, bi)
    2394              :     {
    2395      1376334 :       rtx_insn *insn = DF_INSN_UID_GET (id)->insn;
    2396      1376334 :       convert_insn_common (insn);
    2397      1376334 :       convert_insn (insn);
    2398      1376334 :       converted_insns++;
    2399              :     }
    2400              : 
    2401              :   return converted_insns;
    2402              : }
    2403              : 
    2404              : /* Return the SET expression if INSN doesn't reference hard register.
    2405              :    Return NULL if INSN uses or defines a hard register, excluding
    2406              :    pseudo register pushes, hard register uses in a memory address,
    2407              :    clobbers and flags definitions.  */
    2408              : 
    2409              : static rtx
    2410    355363506 : pseudo_reg_set (rtx_insn *insn)
    2411              : {
    2412    355363506 :   rtx set = single_set (insn);
    2413    355363506 :   if (!set)
    2414              :     return NULL;
    2415              : 
    2416              :   /* Check pseudo register push first. */
    2417    140370516 :   machine_mode mode = TARGET_64BIT ? TImode : DImode;
    2418    140370516 :   if (REG_P (SET_SRC (set))
    2419     39628937 :       && !HARD_REGISTER_P (SET_SRC (set))
    2420    171352050 :       && push_operand (SET_DEST (set), mode))
    2421              :     return set;
    2422              : 
    2423    140117063 :   df_ref ref;
    2424    227353951 :   FOR_EACH_INSN_DEF (ref, insn)
    2425    125507669 :     if (HARD_REGISTER_P (DF_REF_REAL_REG (ref))
    2426     67497791 :         && !DF_REF_FLAGS_IS_SET (ref, DF_REF_MUST_CLOBBER)
    2427    177652005 :         && DF_REF_REGNO (ref) != FLAGS_REG)
    2428              :       return NULL;
    2429              : 
    2430    194889400 :   FOR_EACH_INSN_USE (ref, insn)
    2431    119282653 :     if (!DF_REF_REG_MEM_P (ref) && HARD_REGISTER_P (DF_REF_REAL_REG (ref)))
    2432              :       return NULL;
    2433              : 
    2434              :   return set;
    2435              : }
    2436              : 
    2437              : /* Return true if the register REG is defined in a single DEF chain.
    2438              :    If it is defined in more than one DEF chains, we may not be able
    2439              :    to convert it in all chains.  */
    2440              : 
    2441              : static bool
    2442      1316671 : single_def_chain_p (rtx reg)
    2443              : {
    2444      1316671 :   df_ref ref = DF_REG_DEF_CHAIN (REGNO (reg));
    2445      1316671 :   if (!ref)
    2446              :     return false;
    2447      1316640 :   return DF_REF_NEXT_REG (ref) == nullptr;
    2448              : }
    2449              : 
    2450              : /* Check if comparison INSN may be transformed into vector comparison.
    2451              :    Currently we transform equality/inequality checks which look like:
    2452              :    (set (reg:CCZ 17 flags) (compare:CCZ (reg:TI x) (reg:TI y)))  */
    2453              : 
    2454              : static bool
    2455     13233966 : convertible_comparison_p (rtx_insn *insn, enum machine_mode mode)
    2456              : {
    2457     14650296 :   if (mode != (TARGET_64BIT ? TImode : DImode))
    2458              :     return false;
    2459              : 
    2460      4853965 :   if (!TARGET_SSE4_1)
    2461              :     return false;
    2462              : 
    2463       167673 :   rtx def_set = single_set (insn);
    2464              : 
    2465       167673 :   gcc_assert (def_set);
    2466              : 
    2467       167673 :   rtx src = SET_SRC (def_set);
    2468       167673 :   rtx dst = SET_DEST (def_set);
    2469              : 
    2470       167673 :   gcc_assert (GET_CODE (src) == COMPARE);
    2471              : 
    2472       167673 :   if (!REG_P (dst)
    2473       167673 :       || REGNO (dst) != FLAGS_REG
    2474       335346 :       || GET_MODE (dst) != CCZmode)
    2475              :     return false;
    2476              : 
    2477       117029 :   rtx op1 = XEXP (src, 0);
    2478       117029 :   rtx op2 = XEXP (src, 1);
    2479              : 
    2480              :   /* *cmp<dwi>_doubleword.  */
    2481       117029 :   if (general_operand (op1, mode)
    2482       117029 :       && general_operand (op2, mode))
    2483              :     return true;
    2484              : 
    2485              :   /* *testti_doubleword.  */
    2486       116966 :   if (op2 == const0_rtx
    2487        40422 :       && GET_CODE (op1) == AND
    2488          142 :       && REG_P (XEXP (op1, 0)))
    2489              :     {
    2490          142 :       rtx op12 = XEXP (op1, 1);
    2491          142 :       return GET_MODE (XEXP (op1, 0)) == TImode
    2492          142 :              && (CONST_SCALAR_INT_P (op12)
    2493            0 :                  || ((REG_P (op12) || MEM_P (op12))
    2494            0 :                      && GET_MODE (op12) == TImode));
    2495              :     }
    2496              : 
    2497              :   /* *test<dwi>_not_doubleword.  */
    2498       116824 :   if (op2 == const0_rtx
    2499        40280 :       && GET_CODE (op1) == AND
    2500            0 :       && GET_CODE (XEXP (op1, 0)) == NOT)
    2501              :     {
    2502            0 :       rtx op11 = XEXP (XEXP (op1, 0), 0);
    2503            0 :       rtx op12 = XEXP (op1, 1);
    2504            0 :       return (REG_P (op11) || MEM_P (op11))
    2505            0 :              && (REG_P (op12) || MEM_P (op12))
    2506            0 :              && GET_MODE (op11) == mode
    2507            0 :              && GET_MODE (op12) == mode;
    2508              :     }
    2509              : 
    2510              :   return false;
    2511              : }
    2512              : 
    2513              : /* The general version of scalar_to_vector_candidate_p.  */
    2514              : 
    2515              : static bool
    2516    248029663 : general_scalar_to_vector_candidate_p (rtx_insn *insn, enum machine_mode mode)
    2517              : {
    2518    248029663 :   rtx def_set = pseudo_reg_set (insn);
    2519              : 
    2520    248029663 :   if (!def_set)
    2521              :     return false;
    2522              : 
    2523     51221613 :   rtx src = SET_SRC (def_set);
    2524     51221613 :   rtx dst = SET_DEST (def_set);
    2525              : 
    2526     51221613 :   if (GET_CODE (src) == COMPARE)
    2527      9088166 :     return convertible_comparison_p (insn, mode);
    2528              : 
    2529              :   /* We are interested in "mode" only.  */
    2530     42133447 :   if ((GET_MODE (src) != mode
    2531     28707811 :        && !CONST_INT_P (src))
    2532     18873988 :       || GET_MODE (dst) != mode)
    2533              :     return false;
    2534              : 
    2535     15831460 :   if (!REG_P (dst) && !MEM_P (dst))
    2536              :     return false;
    2537              : 
    2538     15567966 :   switch (GET_CODE (src))
    2539              :     {
    2540       565920 :     case ASHIFT:
    2541       565920 :     case LSHIFTRT:
    2542       565920 :     case ASHIFTRT:
    2543       565920 :     case ROTATE:
    2544       565920 :     case ROTATERT:
    2545       565920 :       if (!CONST_INT_P (XEXP (src, 1))
    2546      1093223 :           || !IN_RANGE (INTVAL (XEXP (src, 1)), 0, GET_MODE_BITSIZE (mode)-1))
    2547              :         return false;
    2548              : 
    2549              :       /* Check for extend highpart case.  */
    2550       527303 :       if (mode != DImode
    2551       389030 :           || GET_CODE (src) != ASHIFTRT
    2552        89268 :           || GET_CODE (XEXP (src, 0)) != ASHIFT)
    2553              :         break;
    2554              : 
    2555      3860252 :       src = XEXP (src, 0);
    2556              :       break;
    2557              : 
    2558       106165 :     case SMAX:
    2559       106165 :     case SMIN:
    2560       106165 :     case UMAX:
    2561       106165 :     case UMIN:
    2562       106165 :       if ((mode == DImode && !TARGET_AVX512VL)
    2563        19007 :           || (mode == SImode && !TARGET_SSE4_1))
    2564              :         return false;
    2565              :       /* Fallthru.  */
    2566              : 
    2567      3365480 :     case AND:
    2568      3365480 :     case IOR:
    2569      3365480 :     case XOR:
    2570      3365480 :     case PLUS:
    2571      3365480 :     case MINUS:
    2572      3365480 :       if (!REG_P (XEXP (src, 1))
    2573              :           && !MEM_P (XEXP (src, 1))
    2574              :           && !CONST_INT_P (XEXP (src, 1)))
    2575              :         return false;
    2576              : 
    2577      3269619 :       if (GET_MODE (XEXP (src, 1)) != mode
    2578      1872540 :           && !CONST_INT_P (XEXP (src, 1)))
    2579              :         return false;
    2580              : 
    2581              :       /* Check for andnot case.  */
    2582      3269619 :       if (GET_CODE (src) != AND
    2583       186524 :           || GET_CODE (XEXP (src, 0)) != NOT)
    2584              :         break;
    2585              : 
    2586      3860252 :       src = XEXP (src, 0);
    2587              :       /* FALLTHRU */
    2588              : 
    2589              :     case NOT:
    2590              :       break;
    2591              : 
    2592        34387 :     case NEG:
    2593              :       /* Check for nabs case.  */
    2594        34387 :       if (GET_CODE (XEXP (src, 0)) != ABS)
    2595              :         break;
    2596              : 
    2597              :       src = XEXP (src, 0);
    2598              :       /* FALLTHRU */
    2599              : 
    2600         3760 :     case ABS:
    2601         3760 :       if ((mode == DImode && !TARGET_AVX512VL)
    2602         1400 :           || (mode == SImode && !TARGET_SSSE3))
    2603              :         return false;
    2604              :       break;
    2605              : 
    2606              :     case REG:
    2607              :       return true;
    2608              : 
    2609      6229927 :     case MEM:
    2610      6229927 :     case CONST_INT:
    2611      6229927 :       return REG_P (dst);
    2612              : 
    2613        41370 :     case VEC_SELECT:
    2614              :       /* Excluding MEM_P (dst) avoids interfering with vpextr[dq].  */
    2615        41370 :       return REG_P (dst)
    2616        34365 :              && REG_P (XEXP (src, 0))
    2617        44091 :              && GET_MODE (XEXP (src, 0)) == (mode == DImode ? V2DImode
    2618              :                                                             : V4SImode)
    2619        24733 :              && GET_CODE (XEXP (src, 1)) == PARALLEL
    2620        24733 :              && XVECLEN (XEXP (src, 1), 0) == 1
    2621        66103 :              && CONST_INT_P (XVECEXP (XEXP (src, 1), 0, 0));
    2622              : 
    2623       326305 :     case ZERO_EXTEND:
    2624              :       /* *zero_extendsidi2 becomes *vec_setv2di_0_zero_extendsi_1.  */
    2625       326305 :       return mode == DImode
    2626       230374 :              && REG_P (dst)
    2627       227411 :              && GET_MODE (XEXP (src, 0)) == SImode
    2628       514060 :              && MEM_P (XEXP (src, 0));
    2629              : 
    2630              :     default:
    2631              :       return false;
    2632              :     }
    2633              : 
    2634      3860252 :   if (!REG_P (XEXP (src, 0))
    2635              :       && !MEM_P (XEXP (src, 0))
    2636              :       && !CONST_INT_P (XEXP (src, 0)))
    2637              :     return false;
    2638              : 
    2639      3557334 :   if (GET_MODE (XEXP (src, 0)) != mode
    2640            0 :       && !CONST_INT_P (XEXP (src, 0)))
    2641              :     return false;
    2642              : 
    2643              :   return true;
    2644              : }
    2645              : 
    2646              : /* Check for a suitable TImode memory operand.  */
    2647              : 
    2648              : static bool
    2649         1671 : timode_mem_p (rtx x)
    2650              : {
    2651         1671 :   return MEM_P (x)
    2652         1671 :          && (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL
    2653            0 :              || !misaligned_operand (x, TImode));
    2654              : }
    2655              : 
    2656              : /* The TImode version of scalar_to_vector_candidate_p.  */
    2657              : 
    2658              : static bool
    2659    107333843 : timode_scalar_to_vector_candidate_p (rtx_insn *insn)
    2660              : {
    2661    107333843 :   rtx def_set = pseudo_reg_set (insn);
    2662              : 
    2663              :   /* We allow two exceptions to the pseudo registers only rule.
    2664              :      Setting a hard register from a pseudo, and setting a pseudo
    2665              :      from a hard register.  */
    2666    107333843 :   if (!def_set)
    2667              :     {
    2668     82695256 :       def_set = single_set (insn);
    2669     82695256 :       if (def_set)
    2670              :         {
    2671     18667104 :           rtx src = SET_SRC (def_set);
    2672     18667104 :           rtx dst = SET_DEST (def_set);
    2673     18667104 :           if (GET_MODE (dst) == TImode
    2674       225600 :               && REG_P (src) && REG_P (dst))
    2675              :             {
    2676       103210 :               if (HARD_REGISTER_P (dst)
    2677        53357 :                   && !HARD_REGISTER_P (src)
    2678       156567 :                   && single_def_chain_p (src))
    2679              :                 return true;
    2680        73616 :               if (HARD_REGISTER_P (src)
    2681        49853 :                   && !HARD_REGISTER_P (dst)
    2682       123469 :                   && single_def_chain_p (dst))
    2683              :                 return true;
    2684              :             }
    2685              :         }
    2686              :       return false;
    2687              :     }
    2688              : 
    2689     24638587 :   rtx src = SET_SRC (def_set);
    2690     24638587 :   rtx dst = SET_DEST (def_set);
    2691              : 
    2692     24638587 :   if (GET_CODE (src) == COMPARE)
    2693      4145800 :     return convertible_comparison_p (insn, TImode);
    2694              : 
    2695     20492787 :   if (GET_MODE (dst) != TImode
    2696      1260538 :       || (GET_MODE (src) != TImode
    2697        64047 :           && !CONST_SCALAR_INT_P (src)))
    2698              :     return false;
    2699              : 
    2700      1260538 :   if (!REG_P (dst) && !MEM_P (dst))
    2701              :     return false;
    2702              : 
    2703      1259085 :   if (MEM_P (dst)
    2704       558541 :       && misaligned_operand (dst, TImode)
    2705      1591554 :       && !TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
    2706              :     return false;
    2707              : 
    2708      1259080 :   if (REG_P (dst) && !single_def_chain_p (dst))
    2709              :     return false;
    2710              : 
    2711      1099057 :   switch (GET_CODE (src))
    2712              :     {
    2713       512917 :     case REG:
    2714       512917 :       return single_def_chain_p (src);
    2715              : 
    2716              :     case CONST_WIDE_INT:
    2717              :       return true;
    2718              : 
    2719        15325 :     case CONST_INT:
    2720              :       /* ??? Verify performance impact before enabling CONST_INT for
    2721              :          __int128 store.  */
    2722        15325 :       return standard_sse_constant_p (src, TImode);
    2723              : 
    2724       467809 :     case MEM:
    2725              :       /* Memory must be aligned or unaligned load is optimal.  */
    2726       467809 :       return (REG_P (dst)
    2727       467809 :               && (!misaligned_operand (src, TImode)
    2728       163930 :                   || TARGET_SSE_UNALIGNED_LOAD_OPTIMAL));
    2729              : 
    2730         4169 :     case AND:
    2731         4169 :       if (!MEM_P (dst)
    2732         4128 :           && GET_CODE (XEXP (src, 0)) == NOT
    2733            0 :           && REG_P (XEXP (XEXP (src, 0), 0))
    2734         4169 :           && (REG_P (XEXP (src, 1))
    2735            0 :               || CONST_SCALAR_INT_P (XEXP (src, 1))
    2736            0 :               || timode_mem_p (XEXP (src, 1))))
    2737              :         return true;
    2738         4169 :       return (REG_P (XEXP (src, 0))
    2739           46 :               || timode_mem_p (XEXP (src, 0)))
    2740         4215 :              && (REG_P (XEXP (src, 1))
    2741         1510 :                  || CONST_SCALAR_INT_P (XEXP (src, 1))
    2742           45 :                  || timode_mem_p (XEXP (src, 1)));
    2743              : 
    2744        16553 :     case IOR:
    2745        16553 :     case XOR:
    2746        16553 :       if (timode_concatdi_p (src))
    2747              :         return true;
    2748         4687 :       return (REG_P (XEXP (src, 0))
    2749         1496 :               || timode_mem_p (XEXP (src, 0)))
    2750         4704 :              && (REG_P (XEXP (src, 1))
    2751          314 :                  || CONST_SCALAR_INT_P (XEXP (src, 1))
    2752           68 :                  || timode_mem_p (XEXP (src, 1)));
    2753              : 
    2754          376 :     case NOT:
    2755          376 :       return REG_P (XEXP (src, 0)) || timode_mem_p (XEXP (src, 0));
    2756              : 
    2757        11908 :     case ASHIFT:
    2758        11908 :     case LSHIFTRT:
    2759        11908 :     case ASHIFTRT:
    2760        11908 :     case ROTATERT:
    2761        11908 :     case ROTATE:
    2762              :       /* Handle shifts/rotates by integer constants between 0 and 127.  */
    2763        11908 :       return REG_P (XEXP (src, 0))
    2764        11877 :              && CONST_INT_P (XEXP (src, 1))
    2765        23383 :              && (INTVAL (XEXP (src, 1)) & ~0x7f) == 0;
    2766              : 
    2767         7657 :     case PLUS:
    2768         7657 :       return timode_concatdi_p (src);
    2769              : 
    2770         4254 :     case ZERO_EXTEND:
    2771         4254 :       return REG_P (XEXP (src, 0))
    2772         4254 :              && GET_MODE (XEXP (src, 0)) == DImode;
    2773              : 
    2774              :     default:
    2775              :       return false;
    2776              :     }
    2777              : }
    2778              : 
    2779              : /* For a register REGNO, scan instructions for its defs and uses.
    2780              :    Put REGNO in REGS if a def or use isn't in CANDIDATES.  */
    2781              : 
    2782              : static void
    2783      1302137 : timode_check_non_convertible_regs (bitmap candidates, bitmap regs,
    2784              :                                    unsigned int regno)
    2785              : {
    2786              :   /* Do nothing if REGNO is already in REGS or is a hard reg.  */
    2787      1302137 :   if (bitmap_bit_p (regs, regno)
    2788      1302137 :       || HARD_REGISTER_NUM_P (regno))
    2789              :     return;
    2790              : 
    2791      1293270 :   for (df_ref def = DF_REG_DEF_CHAIN (regno);
    2792      2572440 :        def;
    2793      1279170 :        def = DF_REF_NEXT_REG (def))
    2794              :     {
    2795      1293250 :       if (!bitmap_bit_p (candidates, DF_REF_INSN_UID (def)))
    2796              :         {
    2797        14080 :           if (dump_file)
    2798            0 :             fprintf (dump_file,
    2799              :                      "r%d has non convertible def in insn %d\n",
    2800            0 :                      regno, DF_REF_INSN_UID (def));
    2801              : 
    2802        14080 :           bitmap_set_bit (regs, regno);
    2803        14080 :           break;
    2804              :         }
    2805              :     }
    2806              : 
    2807      1293270 :   for (df_ref ref = DF_REG_USE_CHAIN (regno);
    2808      2870669 :        ref;
    2809      1577399 :        ref = DF_REF_NEXT_REG (ref))
    2810              :     {
    2811              :       /* Debug instructions are skipped.  */
    2812      1651504 :       if (NONDEBUG_INSN_P (DF_REF_INSN (ref))
    2813      1651504 :           && !bitmap_bit_p (candidates, DF_REF_INSN_UID (ref)))
    2814              :         {
    2815        74105 :           if (dump_file)
    2816            0 :             fprintf (dump_file,
    2817              :                      "r%d has non convertible use in insn %d\n",
    2818            0 :                      regno, DF_REF_INSN_UID (ref));
    2819              : 
    2820        74105 :           bitmap_set_bit (regs, regno);
    2821        74105 :           break;
    2822              :         }
    2823              :     }
    2824              : }
    2825              : 
    2826              : /* For a given bitmap of insn UIDs scans all instructions and
    2827              :    remove insn from CANDIDATES in case it has both convertible
    2828              :    and not convertible definitions.
    2829              : 
    2830              :    All insns in a bitmap are conversion candidates according to
    2831              :    scalar_to_vector_candidate_p.  Currently it implies all insns
    2832              :    are single_set.  */
    2833              : 
    2834              : static void
    2835       855133 : timode_remove_non_convertible_regs (bitmap candidates)
    2836              : {
    2837       855133 :   bitmap_iterator bi;
    2838       855133 :   unsigned id;
    2839       855133 :   bitmap regs = BITMAP_ALLOC (NULL);
    2840       877299 :   bool changed;
    2841              : 
    2842       877299 :   do {
    2843       877299 :     changed = false;
    2844      2198464 :     EXECUTE_IF_SET_IN_BITMAP (candidates, 0, id, bi)
    2845              :       {
    2846      1321165 :         rtx_insn *insn = DF_INSN_UID_GET (id)->insn;
    2847      1321165 :         df_ref ref;
    2848              : 
    2849      2067573 :         FOR_EACH_INSN_DEF (ref, insn)
    2850       746408 :           if (!DF_REF_REG_MEM_P (ref)
    2851       746408 :               && GET_MODE (DF_REF_REG (ref)) == TImode)
    2852       651470 :             timode_check_non_convertible_regs (candidates, regs,
    2853              :                                                DF_REF_REGNO (ref));
    2854              : 
    2855      3254125 :         FOR_EACH_INSN_USE (ref, insn)
    2856      1932960 :           if (DF_REF_TYPE (ref) == DF_REF_REG_USE
    2857       789573 :               && GET_MODE (DF_REF_REG (ref)) == TImode
    2858       650672 :               && !SUBREG_P (DF_REF_REG (ref)))
    2859       650667 :             timode_check_non_convertible_regs (candidates, regs,
    2860              :                                                DF_REF_REGNO (ref));
    2861              :       }
    2862              : 
    2863      1076795 :     EXECUTE_IF_SET_IN_BITMAP (regs, 0, id, bi)
    2864              :       {
    2865       199496 :         for (df_ref def = DF_REG_DEF_CHAIN (id);
    2866       411637 :              def;
    2867       212141 :              def = DF_REF_NEXT_REG (def))
    2868       212141 :           if (bitmap_bit_p (candidates, DF_REF_INSN_UID (def)))
    2869              :             {
    2870        58671 :               if (dump_file)
    2871            0 :                 fprintf (dump_file, "Removing insn %d from candidates list\n",
    2872            0 :                          DF_REF_INSN_UID (def));
    2873              : 
    2874        58671 :               bitmap_clear_bit (candidates, DF_REF_INSN_UID (def));
    2875        58671 :               changed = true;
    2876              :             }
    2877              : 
    2878       199496 :         for (df_ref ref = DF_REG_USE_CHAIN (id);
    2879       626069 :              ref;
    2880       426573 :              ref = DF_REF_NEXT_REG (ref))
    2881       426573 :           if (bitmap_bit_p (candidates, DF_REF_INSN_UID (ref)))
    2882              :             {
    2883        19217 :               if (dump_file)
    2884            0 :                 fprintf (dump_file, "Removing insn %d from candidates list\n",
    2885            0 :                          DF_REF_INSN_UID (ref));
    2886              : 
    2887        19217 :               bitmap_clear_bit (candidates, DF_REF_INSN_UID (ref));
    2888        19217 :               changed = true;
    2889              :             }
    2890              :       }
    2891              :   } while (changed);
    2892              : 
    2893       855133 :   BITMAP_FREE (regs);
    2894       855133 : }
    2895              : 
    2896              : /* Main STV pass function.  Find and convert scalar
    2897              :    instructions into vector mode when profitable.  */
    2898              : 
    2899              : static unsigned int
    2900      1836681 : convert_scalars_to_vector (bool timode_p)
    2901              : {
    2902      1836681 :   basic_block bb;
    2903      1836681 :   int converted_insns = 0;
    2904      1836681 :   auto_vec<rtx_insn *> control_flow_insns;
    2905              : 
    2906      1836681 :   bitmap_obstack_initialize (NULL);
    2907      1836681 :   const machine_mode cand_mode[3] = { SImode, DImode, TImode };
    2908      1836681 :   const machine_mode cand_vmode[3] = { V4SImode, V2DImode, V1TImode };
    2909      5510043 :   bitmap_head candidates[3];  /* { SImode, DImode, TImode } */
    2910      7346724 :   for (unsigned i = 0; i < 3; ++i)
    2911      5510043 :     bitmap_initialize (&candidates[i], &bitmap_default_obstack);
    2912              : 
    2913      1836681 :   calculate_dominance_info (CDI_DOMINATORS);
    2914      1836681 :   df_set_flags (DF_DEFER_INSN_RESCAN | DF_RD_PRUNE_DEAD_DEFS);
    2915      1836681 :   df_chain_add_problem (DF_DU_CHAIN | DF_UD_CHAIN);
    2916      1836681 :   df_analyze ();
    2917              : 
    2918              :   /* Find all instructions we want to convert into vector mode.  */
    2919      1836681 :   if (dump_file)
    2920           45 :     fprintf (dump_file, "Searching for mode conversion candidates...\n");
    2921              : 
    2922     20373328 :   FOR_EACH_BB_FN (bb, cfun)
    2923              :     {
    2924     18536647 :       rtx_insn *insn;
    2925    251701314 :       FOR_BB_INSNS (bb, insn)
    2926    233164667 :         if (timode_p
    2927    233164667 :             && timode_scalar_to_vector_candidate_p (insn))
    2928              :           {
    2929      1135621 :             if (dump_file)
    2930            0 :               fprintf (dump_file, "  insn %d is marked as a TImode candidate\n",
    2931            0 :                        INSN_UID (insn));
    2932              : 
    2933      1135621 :             bitmap_set_bit (&candidates[2], INSN_UID (insn));
    2934              :           }
    2935    232029046 :         else if (!timode_p)
    2936              :           {
    2937              :             /* Check {SI,DI}mode.  */
    2938    361741332 :             for (unsigned i = 0; i <= 1; ++i)
    2939    248029663 :               if (general_scalar_to_vector_candidate_p (insn, cand_mode[i]))
    2940              :                 {
    2941     12119155 :                   if (dump_file)
    2942          568 :                     fprintf (dump_file, "  insn %d is marked as a %s candidate\n",
    2943          284 :                              INSN_UID (insn), i == 0 ? "SImode" : "DImode");
    2944              : 
    2945     12119155 :                   bitmap_set_bit (&candidates[i], INSN_UID (insn));
    2946     12119155 :                   break;
    2947              :                 }
    2948              :           }
    2949              :     }
    2950              : 
    2951      1836681 :   if (timode_p)
    2952       855133 :     timode_remove_non_convertible_regs (&candidates[2]);
    2953              : 
    2954      5809524 :   for (unsigned i = 0; i <= 2; ++i)
    2955      4631377 :     if (!bitmap_empty_p (&candidates[i]))
    2956              :       break;
    2957      3972843 :     else if (i == 2 && dump_file)
    2958           23 :       fprintf (dump_file, "There are no candidates for optimization.\n");
    2959              : 
    2960      7346724 :   for (unsigned i = 0; i <= 2; ++i)
    2961              :     {
    2962      5510043 :       auto_bitmap disallowed;
    2963      5510043 :       bitmap_tree_view (&candidates[i]);
    2964     17694391 :       while (!bitmap_empty_p (&candidates[i]))
    2965              :         {
    2966      6674305 :           unsigned uid = bitmap_first_set_bit (&candidates[i]);
    2967      6674305 :           scalar_chain *chain;
    2968              : 
    2969      6674305 :           if (cand_mode[i] == TImode)
    2970       531766 :             chain = new timode_scalar_chain;
    2971              :           else
    2972      6142539 :             chain = new general_scalar_chain (cand_mode[i], cand_vmode[i]);
    2973              : 
    2974              :           /* Find instructions chain we want to convert to vector mode.
    2975              :              Check all uses and definitions to estimate all required
    2976              :              conversions.  */
    2977      6674305 :           if (chain->build (&candidates[i], uid, disallowed))
    2978              :             {
    2979      6672466 :               if (chain->compute_convert_gain ())
    2980       663750 :                 converted_insns += chain->convert ();
    2981      6008716 :               else if (dump_file)
    2982          136 :                 fprintf (dump_file, "Chain #%d conversion is not profitable\n",
    2983              :                          chain->chain_id);
    2984              :             }
    2985              : 
    2986      6674305 :           rtx_insn* iter_insn;
    2987      6674305 :           unsigned int ii;
    2988      6681431 :           FOR_EACH_VEC_ELT (chain->control_flow_insns, ii, iter_insn)
    2989         3577 :             control_flow_insns.safe_push (iter_insn);
    2990              : 
    2991      6674305 :           delete chain;
    2992              :         }
    2993      5510043 :     }
    2994              : 
    2995      1836681 :   if (dump_file)
    2996           45 :     fprintf (dump_file, "Total insns converted: %d\n", converted_insns);
    2997              : 
    2998      7346724 :   for (unsigned i = 0; i <= 2; ++i)
    2999      5510043 :     bitmap_release (&candidates[i]);
    3000      1836681 :   bitmap_obstack_release (NULL);
    3001      1836681 :   df_process_deferred_rescans ();
    3002              : 
    3003              :   /* Conversion means we may have 128bit register spills/fills
    3004              :      which require aligned stack.  */
    3005      1836681 :   if (converted_insns)
    3006              :     {
    3007       107938 :       if (crtl->stack_alignment_needed < 128)
    3008         2499 :         crtl->stack_alignment_needed = 128;
    3009       107938 :       if (crtl->stack_alignment_estimated < 128)
    3010          228 :         crtl->stack_alignment_estimated = 128;
    3011              : 
    3012       107938 :       crtl->stack_realign_needed
    3013       107938 :         = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
    3014       107938 :       crtl->stack_realign_tried = crtl->stack_realign_needed;
    3015              : 
    3016       107938 :       crtl->stack_realign_processed = true;
    3017              : 
    3018       107938 :       if (!crtl->drap_reg)
    3019              :         {
    3020       107748 :           rtx drap_rtx = targetm.calls.get_drap_rtx ();
    3021              : 
    3022              :           /* stack_realign_drap and drap_rtx must match.  */
    3023       107748 :           gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
    3024              : 
    3025              :           /* Do nothing if NULL is returned,
    3026              :              which means DRAP is not needed.  */
    3027       107748 :           if (drap_rtx != NULL)
    3028              :             {
    3029            0 :               crtl->args.internal_arg_pointer = drap_rtx;
    3030              : 
    3031              :               /* Call fixup_tail_calls to clean up
    3032              :                  REG_EQUIV note if DRAP is needed. */
    3033            0 :               fixup_tail_calls ();
    3034              :             }
    3035              :         }
    3036              : 
    3037              :       /* Fix up DECL_RTL/DECL_INCOMING_RTL of arguments.  */
    3038       107938 :       if (TARGET_64BIT)
    3039        69199 :         for (tree parm = DECL_ARGUMENTS (current_function_decl);
    3040       189502 :              parm; parm = DECL_CHAIN (parm))
    3041              :           {
    3042       120303 :             if (TYPE_MODE (TREE_TYPE (parm)) != TImode)
    3043       104178 :               continue;
    3044        16125 :             if (DECL_RTL_SET_P (parm)
    3045        32250 :                 && GET_MODE (DECL_RTL (parm)) == V1TImode)
    3046              :               {
    3047          621 :                 rtx r = DECL_RTL (parm);
    3048          621 :                 if (REG_P (r))
    3049          621 :                   SET_DECL_RTL (parm, gen_rtx_SUBREG (TImode, r, 0));
    3050              :               }
    3051        16125 :             if (DECL_INCOMING_RTL (parm)
    3052        16125 :                 && GET_MODE (DECL_INCOMING_RTL (parm)) == V1TImode)
    3053              :               {
    3054            0 :                 rtx r = DECL_INCOMING_RTL (parm);
    3055            0 :                 if (REG_P (r))
    3056            0 :                   DECL_INCOMING_RTL (parm) = gen_rtx_SUBREG (TImode, r, 0);
    3057              :               }
    3058              :           }
    3059              : 
    3060      1837809 :       if (!control_flow_insns.is_empty ())
    3061              :         {
    3062         1128 :           free_dominance_info (CDI_DOMINATORS);
    3063              : 
    3064         1128 :           unsigned int i;
    3065         1128 :           rtx_insn* insn;
    3066         5833 :           FOR_EACH_VEC_ELT (control_flow_insns, i, insn)
    3067         3577 :             if (control_flow_insn_p (insn))
    3068              :               {
    3069              :                 /* Split the block after insn.  There will be a fallthru
    3070              :                    edge, which is OK so we keep it.  We have to create
    3071              :                    the exception edges ourselves.  */
    3072         3577 :                 bb = BLOCK_FOR_INSN (insn);
    3073         3577 :                 split_block (bb, insn);
    3074         3577 :                 rtl_make_eh_edge (NULL, bb, BB_END (bb));
    3075              :               }
    3076              :         }
    3077              :     }
    3078              : 
    3079      1836681 :   return 0;
    3080      1836681 : }
    3081              : 
    3082              : static unsigned int
    3083        76811 : rest_of_handle_insert_vzeroupper (void)
    3084              : {
    3085              :   /* vzeroupper instructions are inserted immediately after reload and
    3086              :      postreload_cse to clean up after it a little bit to account for possible
    3087              :      spills from 256bit or 512bit registers.  The pass reuses mode switching
    3088              :      infrastructure by re-running mode insertion pass, so disable entities
    3089              :      that have already been processed.  */
    3090       537677 :   for (int i = 0; i < MAX_386_ENTITIES; i++)
    3091       460866 :     ix86_optimize_mode_switching[i] = 0;
    3092              : 
    3093        76811 :   ix86_optimize_mode_switching[AVX_U128] = 1;
    3094              : 
    3095              :   /* Call optimize_mode_switching.  */
    3096        76811 :   g->get_passes ()->execute_pass_mode_switching ();
    3097              : 
    3098              :   /* LRA removes all REG_DEAD/REG_UNUSED notes and normally they
    3099              :      reappear in the IL only at the start of pass_rtl_dse2, which does
    3100              :      df_note_add_problem (); df_analyze ();
    3101              :      The vzeroupper is scheduled after postreload_cse pass and mode
    3102              :      switching computes the notes as well, the problem is that e.g.
    3103              :      pass_gcse2 doesn't maintain the notes, see PR113059 and
    3104              :      PR112760.  Remove the notes now to restore status quo ante
    3105              :      until we figure out how to maintain the notes or what else
    3106              :      to do.  */
    3107        76811 :   basic_block bb;
    3108        76811 :   rtx_insn *insn;
    3109       411996 :   FOR_EACH_BB_FN (bb, cfun)
    3110      4306714 :     FOR_BB_INSNS (bb, insn)
    3111      3971529 :       if (NONDEBUG_INSN_P (insn))
    3112              :         {
    3113      2127203 :           rtx *pnote = &REG_NOTES (insn);
    3114      3956231 :           while (*pnote != 0)
    3115              :             {
    3116      1829028 :               if (REG_NOTE_KIND (*pnote) == REG_DEAD
    3117       837054 :                   || REG_NOTE_KIND (*pnote) == REG_UNUSED)
    3118      1307286 :                 *pnote = XEXP (*pnote, 1);
    3119              :               else
    3120       521742 :                 pnote = &XEXP (*pnote, 1);
    3121              :             }
    3122              :         }
    3123              : 
    3124        76811 :   df_remove_problem (df_note);
    3125        76811 :   df_analyze ();
    3126        76811 :   return 0;
    3127              : }
    3128              : 
    3129              : namespace {
    3130              : 
    3131              : const pass_data pass_data_insert_vzeroupper =
    3132              : {
    3133              :   RTL_PASS, /* type */
    3134              :   "vzeroupper", /* name */
    3135              :   OPTGROUP_NONE, /* optinfo_flags */
    3136              :   TV_MACH_DEP, /* tv_id */
    3137              :   0, /* properties_required */
    3138              :   0, /* properties_provided */
    3139              :   0, /* properties_destroyed */
    3140              :   0, /* todo_flags_start */
    3141              :   TODO_df_finish, /* todo_flags_finish */
    3142              : };
    3143              : 
    3144              : class pass_insert_vzeroupper : public rtl_opt_pass
    3145              : {
    3146              : public:
    3147       302985 :   pass_insert_vzeroupper(gcc::context *ctxt)
    3148       605970 :     : rtl_opt_pass(pass_data_insert_vzeroupper, ctxt)
    3149              :   {}
    3150              : 
    3151              :   /* opt_pass methods: */
    3152      1531131 :   bool gate (function *) final override
    3153              :     {
    3154      1531131 :       return TARGET_AVX && TARGET_VZEROUPPER;
    3155              :     }
    3156              : 
    3157        76811 :   unsigned int execute (function *) final override
    3158              :     {
    3159        76811 :       return rest_of_handle_insert_vzeroupper ();
    3160              :     }
    3161              : 
    3162              : }; // class pass_insert_vzeroupper
    3163              : 
    3164              : const pass_data pass_data_stv =
    3165              : {
    3166              :   RTL_PASS, /* type */
    3167              :   "stv", /* name */
    3168              :   OPTGROUP_NONE, /* optinfo_flags */
    3169              :   TV_MACH_DEP, /* tv_id */
    3170              :   0, /* properties_required */
    3171              :   0, /* properties_provided */
    3172              :   0, /* properties_destroyed */
    3173              :   0, /* todo_flags_start */
    3174              :   TODO_df_finish, /* todo_flags_finish */
    3175              : };
    3176              : 
    3177              : class pass_stv : public rtl_opt_pass
    3178              : {
    3179              : public:
    3180       605970 :   pass_stv (gcc::context *ctxt)
    3181              :     : rtl_opt_pass (pass_data_stv, ctxt),
    3182      1211940 :       timode_p (false)
    3183              :   {}
    3184              : 
    3185              :   /* opt_pass methods: */
    3186      3062262 :   bool gate (function *) final override
    3187              :     {
    3188      1531131 :       return ((!timode_p || TARGET_64BIT)
    3189      4466670 :               && TARGET_STV && TARGET_SSE2 && optimize > 1);
    3190              :     }
    3191              : 
    3192      1836681 :   unsigned int execute (function *) final override
    3193              :     {
    3194      1836681 :       return convert_scalars_to_vector (timode_p);
    3195              :     }
    3196              : 
    3197       302985 :   opt_pass *clone () final override
    3198              :     {
    3199       302985 :       return new pass_stv (m_ctxt);
    3200              :     }
    3201              : 
    3202       605970 :   void set_pass_param (unsigned int n, bool param) final override
    3203              :     {
    3204       605970 :       gcc_assert (n == 0);
    3205       605970 :       timode_p = param;
    3206       605970 :     }
    3207              : 
    3208              : private:
    3209              :   bool timode_p;
    3210              : }; // class pass_stv
    3211              : 
    3212              : } // anon namespace
    3213              : 
    3214              : rtl_opt_pass *
    3215       302985 : make_pass_insert_vzeroupper (gcc::context *ctxt)
    3216              : {
    3217       302985 :   return new pass_insert_vzeroupper (ctxt);
    3218              : }
    3219              : 
    3220              : rtl_opt_pass *
    3221       302985 : make_pass_stv (gcc::context *ctxt)
    3222              : {
    3223       302985 :   return new pass_stv (ctxt);
    3224              : }
    3225              : 
    3226              : /* Inserting ENDBR and pseudo patchable-area instructions.  */
    3227              : 
    3228              : static void
    3229       198076 : rest_of_insert_endbr_and_patchable_area (bool need_endbr,
    3230              :                                          unsigned int patchable_area_size)
    3231              : {
    3232       198076 :   rtx endbr;
    3233       198076 :   rtx_insn *insn;
    3234       198076 :   rtx_insn *endbr_insn = NULL;
    3235       198076 :   basic_block bb;
    3236              : 
    3237       198076 :   if (need_endbr)
    3238              :     {
    3239              :       /* Currently emit EB if it's a tracking function, i.e. 'nocf_check'
    3240              :          is absent among function attributes.  Later an optimization will
    3241              :          be introduced to make analysis if an address of a static function
    3242              :          is taken.  A static function whose address is not taken will get
    3243              :          a nocf_check attribute.  This will allow to reduce the number of
    3244              :          EB.  */
    3245       198027 :       if (!lookup_attribute ("nocf_check",
    3246       198027 :                              TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl)))
    3247       198027 :           && (lookup_attribute ("cf_check", DECL_ATTRIBUTES (cfun->decl))
    3248       197998 :               || (!flag_manual_endbr
    3249       197996 :                   && (!cgraph_node::get (cfun->decl)->only_called_directly_p ()
    3250        28200 :                       || ix86_cmodel == CM_LARGE
    3251        28199 :                       || ix86_cmodel == CM_LARGE_PIC
    3252        28198 :                       || flag_force_indirect_call
    3253        28198 :                       || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
    3254              :                           && DECL_DLLIMPORT_P (cfun->decl))))))
    3255              :         {
    3256       169810 :           if (crtl->profile && flag_fentry)
    3257              :             {
    3258              :               /* Queue ENDBR insertion to x86_function_profiler.
    3259              :                  NB: Any patchable-area insn will be inserted after
    3260              :                  ENDBR.  */
    3261            6 :               cfun->machine->insn_queued_at_entrance = TYPE_ENDBR;
    3262              :             }
    3263              :           else
    3264              :             {
    3265       169804 :               endbr = gen_nop_endbr ();
    3266       169804 :               bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
    3267       169804 :               rtx_insn *insn = BB_HEAD (bb);
    3268       169804 :               endbr_insn = emit_insn_before (endbr, insn);
    3269              :             }
    3270              :         }
    3271              :     }
    3272              : 
    3273       198076 :   if (patchable_area_size)
    3274              :     {
    3275           55 :       if (crtl->profile && flag_fentry)
    3276              :         {
    3277              :           /* Queue patchable-area insertion to x86_function_profiler.
    3278              :              NB: If there is a queued ENDBR, x86_function_profiler
    3279              :              will also handle patchable-area.  */
    3280            2 :           if (!cfun->machine->insn_queued_at_entrance)
    3281            1 :             cfun->machine->insn_queued_at_entrance = TYPE_PATCHABLE_AREA;
    3282              :         }
    3283              :       else
    3284              :         {
    3285           53 :           rtx patchable_area
    3286           53 :             = gen_patchable_area (GEN_INT (patchable_area_size),
    3287           53 :                                   GEN_INT (crtl->patch_area_entry == 0));
    3288           53 :           if (endbr_insn)
    3289            3 :             emit_insn_after (patchable_area, endbr_insn);
    3290              :           else
    3291              :             {
    3292           50 :               bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
    3293           50 :               insn = BB_HEAD (bb);
    3294           50 :               emit_insn_before (patchable_area, insn);
    3295              :             }
    3296              :         }
    3297              :     }
    3298              : 
    3299       198076 :   if (!need_endbr)
    3300              :     return;
    3301              : 
    3302       198027 :   bb = 0;
    3303      4148048 :   FOR_EACH_BB_FN (bb, cfun)
    3304              :     {
    3305     77376685 :       for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
    3306     73426664 :            insn = NEXT_INSN (insn))
    3307              :         {
    3308     73426664 :           if (auto call_insn = dyn_cast<rtx_call_insn *> (insn))
    3309              :             {
    3310      1484787 :               need_endbr = find_reg_note (call_insn, REG_SETJMP, NULL) != NULL;
    3311      1484787 :               if (!need_endbr && !SIBLING_CALL_P (call_insn))
    3312              :                 {
    3313      1435846 :                   rtx call = get_call_rtx_from (call_insn);
    3314      1435846 :                   rtx fnaddr = XEXP (call, 0);
    3315      1435846 :                   tree fndecl = NULL_TREE;
    3316              : 
    3317              :                   /* Also generate ENDBRANCH for non-tail call which
    3318              :                      may return via indirect branch.  */
    3319      1435846 :                   if (SYMBOL_REF_P (XEXP (fnaddr, 0)))
    3320      1377322 :                     fndecl = SYMBOL_REF_DECL (XEXP (fnaddr, 0));
    3321      1377322 :                   if (fndecl == NULL_TREE)
    3322        58892 :                     fndecl = MEM_EXPR (fnaddr);
    3323        58892 :                   if (fndecl
    3324      1433388 :                       && TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE
    3325       596516 :                       && TREE_CODE (TREE_TYPE (fndecl)) != METHOD_TYPE)
    3326              :                     fndecl = NULL_TREE;
    3327      1435846 :                   if (fndecl && TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
    3328              :                     {
    3329      1390846 :                       tree fntype = TREE_TYPE (fndecl);
    3330      1390846 :                       if (lookup_attribute ("indirect_return",
    3331      1390846 :                                             TYPE_ATTRIBUTES (fntype)))
    3332              :                         need_endbr = true;
    3333              :                     }
    3334              :                 }
    3335      1484775 :               if (!need_endbr)
    3336      1484767 :                 continue;
    3337              :               /* Generate ENDBRANCH after CALL, which can return more than
    3338              :                  twice, setjmp-like functions.  */
    3339              : 
    3340           20 :               endbr = gen_nop_endbr ();
    3341           20 :               emit_insn_after_setloc (endbr, call_insn,
    3342              :                                       call_insn->location ());
    3343           20 :               continue;
    3344           20 :             }
    3345              : 
    3346     71941877 :           if (JUMP_P (insn) && flag_cet_switch)
    3347              :             {
    3348           15 :               rtx target = JUMP_LABEL (insn);
    3349           15 :               if (target == NULL_RTX || ANY_RETURN_P (target))
    3350            6 :                 continue;
    3351              : 
    3352              :               /* Check the jump is a switch table.  */
    3353            9 :               rtx_insn *label = as_a<rtx_insn *> (target);
    3354            9 :               rtx_insn *table = next_insn (label);
    3355            9 :               if (table == NULL_RTX || !JUMP_TABLE_DATA_P (table))
    3356            5 :                 continue;
    3357              : 
    3358              :               /* For the indirect jump find out all places it jumps and insert
    3359              :                  ENDBRANCH there.  It should be done under a special flag to
    3360              :                  control ENDBRANCH generation for switch stmts.  */
    3361            4 :               edge_iterator ei;
    3362            4 :               edge e;
    3363            4 :               basic_block dest_blk;
    3364              : 
    3365           42 :               FOR_EACH_EDGE (e, ei, bb->succs)
    3366              :                 {
    3367           38 :                   rtx_insn *insn;
    3368              : 
    3369           38 :                   dest_blk = e->dest;
    3370           38 :                   insn = BB_HEAD (dest_blk);
    3371           38 :                   gcc_assert (LABEL_P (insn));
    3372              : 
    3373              :                   /* Reuse existing ENDBR.  */
    3374           38 :                   rtx_insn *next = next_nonnote_nondebug_insn (insn);
    3375           38 :                   if (next && NONJUMP_INSN_P (next)
    3376           30 :                       && GET_CODE (PATTERN (next)) == UNSPEC_VOLATILE
    3377           46 :                       && XINT (PATTERN (next), 1) == UNSPECV_NOP_ENDBR)
    3378            8 :                     continue;
    3379              : 
    3380           30 :                   endbr = gen_nop_endbr ();
    3381           30 :                   emit_insn_after (endbr, insn);
    3382              :                 }
    3383            4 :               continue;
    3384            4 :             }
    3385              : 
    3386     71941862 :           if (LABEL_P (insn) && LABEL_PRESERVE_P (insn))
    3387              :             {
    3388       153292 :               endbr = gen_nop_endbr ();
    3389       153292 :               emit_insn_after (endbr, insn);
    3390       153292 :               continue;
    3391              :             }
    3392              :         }
    3393              :     }
    3394              : 
    3395              :   return;
    3396              : }
    3397              : 
    3398              : namespace {
    3399              : 
    3400              : const pass_data pass_data_insert_endbr_and_patchable_area =
    3401              : {
    3402              :   RTL_PASS, /* type.  */
    3403              :   "endbr_and_patchable_area", /* name.  */
    3404              :   OPTGROUP_NONE, /* optinfo_flags.  */
    3405              :   TV_MACH_DEP, /* tv_id.  */
    3406              :   0, /* properties_required.  */
    3407              :   0, /* properties_provided.  */
    3408              :   0, /* properties_destroyed.  */
    3409              :   0, /* todo_flags_start.  */
    3410              :   0, /* todo_flags_finish.  */
    3411              : };
    3412              : 
    3413              : class pass_insert_endbr_and_patchable_area : public rtl_opt_pass
    3414              : {
    3415              : public:
    3416       302985 :   pass_insert_endbr_and_patchable_area (gcc::context *ctxt)
    3417       605970 :     : rtl_opt_pass (pass_data_insert_endbr_and_patchable_area, ctxt)
    3418              :   {}
    3419              : 
    3420              :   /* opt_pass methods: */
    3421      1531131 :   bool gate (function *) final override
    3422              :     {
    3423      1531131 :       need_endbr = (flag_cf_protection & CF_BRANCH) != 0;
    3424      1531131 :       patchable_area_size = crtl->patch_area_size - crtl->patch_area_entry;
    3425      1531131 :       return need_endbr || patchable_area_size;
    3426              :     }
    3427              : 
    3428       198076 :   unsigned int execute (function *) final override
    3429              :     {
    3430       198076 :       timevar_push (TV_MACH_DEP);
    3431       198076 :       rest_of_insert_endbr_and_patchable_area (need_endbr,
    3432              :                                                patchable_area_size);
    3433       198076 :       timevar_pop (TV_MACH_DEP);
    3434       198076 :       return 0;
    3435              :     }
    3436              : 
    3437              : private:
    3438              :   bool need_endbr;
    3439              :   unsigned int patchable_area_size;
    3440              : }; // class pass_insert_endbr_and_patchable_area
    3441              : 
    3442              : } // anon namespace
    3443              : 
    3444              : rtl_opt_pass *
    3445       302985 : make_pass_insert_endbr_and_patchable_area (gcc::context *ctxt)
    3446              : {
    3447       302985 :   return new pass_insert_endbr_and_patchable_area (ctxt);
    3448              : }
    3449              : 
    3450              : bool
    3451      6189209 : ix86_rpad_gate ()
    3452              : {
    3453      6189209 :   return (TARGET_AVX
    3454       394204 :           && TARGET_SSE_PARTIAL_REG_DEPENDENCY
    3455       297077 :           && TARGET_SSE_MATH
    3456       296783 :           && optimize
    3457      6480793 :           && optimize_function_for_speed_p (cfun));
    3458              : }
    3459              : 
    3460              : enum x86_cse_kind
    3461              : {
    3462              :   X86_CSE_CONST0_VECTOR,
    3463              :   X86_CSE_CONSTM1_VECTOR,
    3464              :   X86_CSE_CONST_VECTOR,
    3465              :   X86_CSE_VEC_DUP,
    3466              :   X86_CSE_TLS_GD,
    3467              :   X86_CSE_TLS_LD_BASE,
    3468              :   X86_CSE_TLSDESC
    3469              : };
    3470              : 
    3471       160040 : struct redundant_pattern
    3472              : {
    3473              :   /* Bitmap of basic blocks with broadcast instructions.  */
    3474              :   auto_bitmap bbs;
    3475              :   /* Bitmap of broadcast instructions.  */
    3476              :   auto_bitmap insns;
    3477              :   /* The broadcast inner scalar.  */
    3478              :   rtx val;
    3479              :   /* The actual redundant source value for UNSPEC_TLSDESC.  */
    3480              :   rtx tlsdesc_val;
    3481              :   /* The inner scalar mode.  */
    3482              :   machine_mode mode;
    3483              :   /* The destination mode which can be changed to the integer mode of
    3484              :      the same time.  */
    3485              :   machine_mode dest_mode;
    3486              :   /* The instruction which sets the inner scalar.  Nullptr if the inner
    3487              :      scalar is applied to the whole function, instead of within the same
    3488              :      block.  */
    3489              :   rtx_insn *def_insn;
    3490              :   /* The widest broadcast source.  */
    3491              :   rtx broadcast_source;
    3492              :   /* The widest broadcast register.  */
    3493              :   rtx broadcast_reg;
    3494              :   /* The basic block of the broadcast instruction.  */
    3495              :   basic_block bb;
    3496              :   /* The number of broadcast instructions with the same inner scalar.  */
    3497              :   unsigned HOST_WIDE_INT count;
    3498              :   /* The threshold of broadcast instructions with the same inner
    3499              :      scalar.  */
    3500              :   unsigned int threshold;
    3501              :   /* The widest broadcast size in bytes.  */
    3502              :   unsigned int size;
    3503              :   /* Load kind.  */
    3504              :   x86_cse_kind kind;
    3505              : };
    3506              : 
    3507              : /* Generate a vector set, DEST = SRC, at entry of the nearest dominator
    3508              :    for basic block map BBS, which is in the fake loop that contains the
    3509              :    whole function, so that there is only a single vector set in the
    3510              :    whole function.  If not nullptr, LOAD is a pointer to the load.  */
    3511              : 
    3512              : static void
    3513        46474 : ix86_place_single_vector_set (rtx dest, rtx src, bitmap bbs,
    3514              :                               redundant_pattern *load = nullptr)
    3515              : {
    3516        46474 :   basic_block bb = nearest_common_dominator_for_set (CDI_DOMINATORS, bbs);
    3517              :   /* For X86_CSE_VEC_DUP and X86_CSE_CONST_VECTOR, don't place the vector
    3518              :      set outside of the loop to avoid extra spills.  */
    3519        46474 :   if (!load
    3520        45443 :       || (load->kind != X86_CSE_VEC_DUP
    3521        45443 :           && load->kind != X86_CSE_CONST_VECTOR))
    3522              :     {
    3523        25299 :       while (bb->loop_father->latch
    3524        25299 :              != EXIT_BLOCK_PTR_FOR_FN (cfun))
    3525         1562 :         bb = get_immediate_dominator (CDI_DOMINATORS,
    3526              :                                       bb->loop_father->header);
    3527              :     }
    3528              : 
    3529        46474 :   if (CONST_INT_P (src))
    3530        12052 :     dest = gen_rtx_SUBREG (load->dest_mode, dest, 0);
    3531        34422 :   else if (CONST_VECTOR_P (src))
    3532              :     {
    3533              :       /* The only possible CONST_VECTORs of SRC are CONST0_RTX and
    3534              :          CONSTM1_RTX.  Otherwise,
    3535              : 
    3536              :          rtx set = gen_rtx_SET (dest, src);
    3537              : 
    3538              :          won't be a valid instruction.  CONST0_RTX always works.  It
    3539              :          can comes from:
    3540              : 
    3541              :          1. remove_partial_avx_dependency with LOAD == NULL.
    3542              :          2. X86_CSE_VEC_DUP with
    3543              : 
    3544              :          (insn 48 58 16 3 (set (reg:V4HI 123)
    3545              :                 (const_vector:V4HI [
    3546              :                         (const_int 0 [0]) repeated x4
    3547              :                   ])) 2065 {*movv4hi_internal} (nil))
    3548              : 
    3549              :          3. X86_CSE_CONST0_VECTOR.
    3550              :        */
    3551        23737 :       machine_mode mode = GET_MODE (dest);
    3552        23737 :       if (!(src == CONST0_RTX (mode)
    3553         1720 :             || (src == CONSTM1_RTX (mode)
    3554         1720 :                 && load->kind == X86_CSE_CONSTM1_VECTOR)))
    3555            0 :         gcc_unreachable ();
    3556              :     }
    3557        46474 :   rtx set = gen_rtx_SET (dest, src);
    3558              : 
    3559        46474 :   rtx_insn *insn = BB_HEAD (bb);
    3560       191902 :   while (insn && !NONDEBUG_INSN_P (insn))
    3561              :     {
    3562       145432 :       if (insn == BB_END (bb))
    3563              :         {
    3564              :           insn = NULL;
    3565              :           break;
    3566              :         }
    3567       145428 :       insn = NEXT_INSN (insn);
    3568              :     }
    3569              : 
    3570        46474 :   rtx_insn *set_insn;
    3571        46474 :   if (insn == BB_HEAD (bb))
    3572              :     {
    3573            0 :       set_insn = emit_insn_before (set, insn);
    3574            0 :       if (dump_file)
    3575              :         {
    3576            0 :           fprintf (dump_file, "\nPlace:\n\n");
    3577            0 :           print_rtl_single (dump_file, set_insn);
    3578            0 :           fprintf (dump_file, "\nbefore:\n\n");
    3579            0 :           print_rtl_single (dump_file, insn);
    3580            0 :           fprintf (dump_file, "\n");
    3581              :         }
    3582              :     }
    3583              :   else
    3584              :     {
    3585        46474 :       rtx_insn *after = insn ? PREV_INSN (insn) : BB_END (bb);
    3586        46474 :       set_insn = emit_insn_after (set, after);
    3587        46474 :       if (dump_file)
    3588              :         {
    3589            0 :           fprintf (dump_file, "\nPlace:\n\n");
    3590            0 :           print_rtl_single (dump_file, set_insn);
    3591            0 :           fprintf (dump_file, "\nafter:\n\n");
    3592            0 :           print_rtl_single (dump_file, after);
    3593            0 :           fprintf (dump_file, "\n");
    3594              :         }
    3595              :     }
    3596              : 
    3597        46474 :   if (load && load->kind == X86_CSE_VEC_DUP)
    3598              :     {
    3599              :       /* Get the source from LOAD as (reg:SI 99) in
    3600              : 
    3601              :          (vec_duplicate:V4SI (reg:SI 99))
    3602              : 
    3603              :        */
    3604        10685 :       rtx inner_scalar = load->val;
    3605              :       /* Set the source in (vec_duplicate:V4SI (reg:SI 99)).  */
    3606        10685 :       rtx reg = XEXP (src, 0);
    3607        10685 :       machine_mode reg_mode = GET_MODE (reg);
    3608        10685 :       if (reg_mode != GET_MODE (inner_scalar))
    3609              :         {
    3610        10547 :           if (REG_P (inner_scalar) || MEM_P (inner_scalar))
    3611            0 :             inner_scalar = gen_rtx_SUBREG (reg_mode, inner_scalar, 0);
    3612        10547 :           else if (!SCALAR_INT_MODE_P (reg_mode))
    3613              :             {
    3614              :               /* For non-int load with integer constant, generate
    3615              : 
    3616              :                  (set (subreg:SI (reg/v:SF 105 [ f ]) 0)
    3617              :                       (const_int 1313486336 [0x4e4a3600]))
    3618              : 
    3619              :                */
    3620            1 :               gcc_assert (CONST_INT_P (inner_scalar));
    3621            1 :               unsigned int bits = GET_MODE_BITSIZE (reg_mode);
    3622            1 :               machine_mode mode = int_mode_for_size (bits, 0).require ();
    3623            1 :               reg = gen_rtx_SUBREG (mode, reg, 0);
    3624              :             }
    3625              :         }
    3626        10685 :       rtx set = gen_rtx_SET (reg, inner_scalar);
    3627        10685 :       insn = emit_insn_before (set, set_insn);
    3628        10685 :       if (dump_file)
    3629              :         {
    3630            0 :           fprintf (dump_file, "\nAdd:\n\n");
    3631            0 :           print_rtl_single (dump_file, insn);
    3632            0 :           fprintf (dump_file, "\nbefore:\n\n");
    3633            0 :           print_rtl_single (dump_file, set_insn);
    3634            0 :           fprintf (dump_file, "\n");
    3635              :         }
    3636              :     }
    3637        46474 : }
    3638              : 
    3639              : /* At entry of the nearest common dominator for basic blocks with
    3640              :    conversions/rcp/sqrt/rsqrt/round, generate a single
    3641              :         vxorps %xmmN, %xmmN, %xmmN
    3642              :    for all
    3643              :         vcvtss2sd  op, %xmmN, %xmmX
    3644              :         vcvtsd2ss  op, %xmmN, %xmmX
    3645              :         vcvtsi2ss  op, %xmmN, %xmmX
    3646              :         vcvtsi2sd  op, %xmmN, %xmmX
    3647              : 
    3648              :    NB: We want to generate only a single vxorps to cover the whole
    3649              :    function.  The LCM algorithm isn't appropriate here since it may
    3650              :    place a vxorps inside the loop.  */
    3651              : 
    3652              : static unsigned int
    3653        34464 : remove_partial_avx_dependency (void)
    3654              : {
    3655        34464 :   timevar_push (TV_MACH_DEP);
    3656              : 
    3657        34464 :   bitmap_obstack_initialize (NULL);
    3658        34464 :   bitmap convert_bbs = BITMAP_ALLOC (NULL);
    3659              : 
    3660        34464 :   basic_block bb;
    3661        34464 :   rtx_insn *insn, *set_insn;
    3662        34464 :   rtx set;
    3663        34464 :   rtx v4sf_const0 = NULL_RTX;
    3664              : 
    3665        34464 :   auto_vec<rtx_insn *> control_flow_insns;
    3666              : 
    3667              :   /* We create invalid RTL initially so defer rescans.  */
    3668        34464 :   df_set_flags (DF_DEFER_INSN_RESCAN);
    3669              : 
    3670       314896 :   FOR_EACH_BB_FN (bb, cfun)
    3671              :     {
    3672      3517145 :       FOR_BB_INSNS (bb, insn)
    3673              :         {
    3674      3236713 :           if (!NONDEBUG_INSN_P (insn))
    3675      1434714 :             continue;
    3676              : 
    3677      1801999 :           set = single_set (insn);
    3678      1801999 :           if (!set)
    3679        72122 :             continue;
    3680              : 
    3681      1729877 :           if (get_attr_avx_partial_xmm_update (insn)
    3682              :               != AVX_PARTIAL_XMM_UPDATE_TRUE)
    3683      1726624 :             continue;
    3684              : 
    3685              :           /* Convert PARTIAL_XMM_UPDATE_TRUE insns, DF -> SF, SF -> DF,
    3686              :              SI -> SF, SI -> DF, DI -> SF, DI -> DF, sqrt, rsqrt, rcp,
    3687              :              round, to vec_dup and vec_merge with subreg.  */
    3688         3253 :           rtx src = SET_SRC (set);
    3689         3253 :           rtx dest = SET_DEST (set);
    3690         3253 :           machine_mode dest_mode = GET_MODE (dest);
    3691         3253 :           bool convert_p = false;
    3692         3253 :           switch (GET_CODE (src))
    3693              :             {
    3694         3188 :             case FLOAT:
    3695         3188 :             case FLOAT_EXTEND:
    3696         3188 :             case FLOAT_TRUNCATE:
    3697         3188 :             case UNSIGNED_FLOAT:
    3698         3188 :               convert_p = true;
    3699         3188 :               break;
    3700              :             default:
    3701              :               break;
    3702              :             }
    3703              : 
    3704              :           /* Only handle conversion here.  */
    3705         3188 :           machine_mode src_mode
    3706         3188 :             = convert_p ? GET_MODE (XEXP (src, 0)) : VOIDmode;
    3707         3188 :           switch (src_mode)
    3708              :             {
    3709          153 :             case E_SFmode:
    3710          153 :             case E_DFmode:
    3711          153 :               if (TARGET_USE_VECTOR_FP_CONVERTS
    3712          147 :                   || !TARGET_SSE_PARTIAL_REG_FP_CONVERTS_DEPENDENCY)
    3713            8 :                 continue;
    3714              :               break;
    3715         3035 :             case E_SImode:
    3716         3035 :             case E_DImode:
    3717         3035 :               if (TARGET_USE_VECTOR_CONVERTS
    3718         3023 :                   || !TARGET_SSE_PARTIAL_REG_CONVERTS_DEPENDENCY)
    3719           14 :                 continue;
    3720              :               break;
    3721           65 :             case E_VOIDmode:
    3722           65 :               gcc_assert (!convert_p);
    3723              :               break;
    3724            0 :             default:
    3725            0 :               gcc_unreachable ();
    3726              :             }
    3727              : 
    3728         3231 :           if (!v4sf_const0)
    3729         1031 :             v4sf_const0 = gen_reg_rtx (V4SFmode);
    3730              : 
    3731         3231 :           rtx zero;
    3732         3231 :           machine_mode dest_vecmode;
    3733         3231 :           switch (dest_mode)
    3734              :             {
    3735           50 :             case E_HFmode:
    3736           50 :               dest_vecmode = V8HFmode;
    3737           50 :               zero = gen_rtx_SUBREG (V8HFmode, v4sf_const0, 0);
    3738           50 :               break;
    3739              :             case E_SFmode:
    3740              :               dest_vecmode = V4SFmode;
    3741              :               zero = v4sf_const0;
    3742              :               break;
    3743         1186 :             case E_DFmode:
    3744         1186 :               dest_vecmode = V2DFmode;
    3745         1186 :               zero = gen_rtx_SUBREG (V2DFmode, v4sf_const0, 0);
    3746         1186 :               break;
    3747            0 :             default:
    3748            0 :               gcc_unreachable ();
    3749              :             }
    3750              : 
    3751              :           /* Change source to vector mode.  */
    3752         3231 :           src = gen_rtx_VEC_DUPLICATE (dest_vecmode, src);
    3753         3231 :           src = gen_rtx_VEC_MERGE (dest_vecmode, src, zero, const1_rtx);
    3754              :           /* Change destination to vector mode.  */
    3755         3231 :           rtx vec = gen_reg_rtx (dest_vecmode);
    3756              :           /* Generate an XMM vector SET.  */
    3757         3231 :           set = gen_rtx_SET (vec, src);
    3758         3231 :           set_insn = emit_insn_before (set, insn);
    3759              : 
    3760         3231 :           if (cfun->can_throw_non_call_exceptions)
    3761              :             {
    3762              :               /* Handle REG_EH_REGION note.  */
    3763            0 :               rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
    3764            0 :               if (note)
    3765              :                 {
    3766            0 :                   control_flow_insns.safe_push (set_insn);
    3767            0 :                   add_reg_note (set_insn, REG_EH_REGION, XEXP (note, 0));
    3768              :                 }
    3769              :             }
    3770              : 
    3771         3231 :           src = gen_rtx_SUBREG (dest_mode, vec, 0);
    3772         3231 :           set = gen_rtx_SET (dest, src);
    3773              : 
    3774              :           /* Drop possible dead definitions.  */
    3775         3231 :           PATTERN (insn) = set;
    3776              : 
    3777         3231 :           INSN_CODE (insn) = -1;
    3778         3231 :           recog_memoized (insn);
    3779         3231 :           df_insn_rescan (insn);
    3780         3231 :           bitmap_set_bit (convert_bbs, bb->index);
    3781              :         }
    3782              :     }
    3783              : 
    3784        34464 :   if (v4sf_const0)
    3785              :     {
    3786              :       /* (Re-)discover loops so that bb->loop_father can be used in the
    3787              :          analysis below.  */
    3788         1031 :       calculate_dominance_info (CDI_DOMINATORS);
    3789         1031 :       loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
    3790              : 
    3791         1031 :       ix86_place_single_vector_set (v4sf_const0,
    3792              :                                     CONST0_RTX (V4SFmode),
    3793              :                                     convert_bbs);
    3794              : 
    3795         1031 :       loop_optimizer_finalize ();
    3796              : 
    3797        34464 :       if (!control_flow_insns.is_empty ())
    3798              :         {
    3799            0 :           free_dominance_info (CDI_DOMINATORS);
    3800              : 
    3801            0 :           unsigned int i;
    3802            0 :           FOR_EACH_VEC_ELT (control_flow_insns, i, insn)
    3803            0 :             if (control_flow_insn_p (insn))
    3804              :               {
    3805              :                 /* Split the block after insn.  There will be a fallthru
    3806              :                    edge, which is OK so we keep it.  We have to create
    3807              :                    the exception edges ourselves.  */
    3808            0 :                 bb = BLOCK_FOR_INSN (insn);
    3809            0 :                 split_block (bb, insn);
    3810            0 :                 rtl_make_eh_edge (NULL, bb, BB_END (bb));
    3811              :               }
    3812              :         }
    3813              :     }
    3814              : 
    3815        34464 :   df_process_deferred_rescans ();
    3816        34464 :   df_clear_flags (DF_DEFER_INSN_RESCAN);
    3817        34464 :   bitmap_obstack_release (NULL);
    3818        34464 :   BITMAP_FREE (convert_bbs);
    3819              : 
    3820        34464 :   timevar_pop (TV_MACH_DEP);
    3821        34464 :   return 0;
    3822        34464 : }
    3823              : 
    3824              : namespace {
    3825              : 
    3826              : const pass_data pass_data_remove_partial_avx_dependency =
    3827              : {
    3828              :   RTL_PASS, /* type */
    3829              :   "rpad", /* name */
    3830              :   OPTGROUP_NONE, /* optinfo_flags */
    3831              :   TV_MACH_DEP, /* tv_id */
    3832              :   0, /* properties_required */
    3833              :   0, /* properties_provided */
    3834              :   0, /* properties_destroyed */
    3835              :   0, /* todo_flags_start */
    3836              :   0, /* todo_flags_finish */
    3837              : };
    3838              : 
    3839              : class pass_remove_partial_avx_dependency : public rtl_opt_pass
    3840              : {
    3841              : public:
    3842       302985 :   pass_remove_partial_avx_dependency (gcc::context *ctxt)
    3843       605970 :     : rtl_opt_pass (pass_data_remove_partial_avx_dependency, ctxt)
    3844              :   {}
    3845              : 
    3846              :   /* opt_pass methods: */
    3847      1531131 :   bool gate (function *) final override
    3848              :     {
    3849      1531131 :       return ix86_rpad_gate ();
    3850              :     }
    3851              : 
    3852        34464 :   unsigned int execute (function *) final override
    3853              :     {
    3854        34464 :       return remove_partial_avx_dependency ();
    3855              :     }
    3856              : }; // class pass_rpad
    3857              : 
    3858              : } // anon namespace
    3859              : 
    3860              : rtl_opt_pass *
    3861       302985 : make_pass_remove_partial_avx_dependency (gcc::context *ctxt)
    3862              : {
    3863       302985 :   return new pass_remove_partial_avx_dependency (ctxt);
    3864              : }
    3865              : 
    3866              : /* Return a machine mode suitable for vector SIZE with SMODE inner
    3867              :    mode.  */
    3868              : 
    3869              : static machine_mode
    3870        68059 : ix86_get_vector_cse_mode (unsigned int size, machine_mode smode)
    3871              : {
    3872              :   /* Use the inner scalar mode of vector broadcast source in:
    3873              : 
    3874              :      (set (reg:V8DF 394)
    3875              :           (vec_duplicate:V8DF (reg:V2DF 190 [ alpha ])))
    3876              : 
    3877              :      to compute the vector mode for broadcast from vector source.
    3878              :    */
    3879        68059 :   if (VECTOR_MODE_P (smode))
    3880        33202 :     smode = GET_MODE_INNER (smode);
    3881        68059 :   scalar_mode s_mode = as_a <scalar_mode> (smode);
    3882       136118 :   poly_uint64 nunits = size / GET_MODE_SIZE (smode);
    3883        68059 :   machine_mode mode = mode_for_vector (s_mode, nunits).require ();
    3884        68059 :   return mode;
    3885              : }
    3886              : 
    3887              : /* Replace the source operand of instructions in VECTOR_INSNS with
    3888              :    VECTOR_CONST in VECTOR_MODE.  */
    3889              : 
    3890              : static void
    3891        67436 : replace_vector_const (machine_mode vector_mode, rtx vector_const,
    3892              :                       auto_bitmap &vector_insns,
    3893              :                       machine_mode scalar_mode)
    3894              : {
    3895        67436 :   bitmap_iterator bi;
    3896        67436 :   unsigned int id;
    3897              : 
    3898       236332 :   EXECUTE_IF_SET_IN_BITMAP (vector_insns, 0, id, bi)
    3899              :     {
    3900       168896 :       rtx_insn *insn = DF_INSN_UID_GET (id)->insn;
    3901              : 
    3902              :       /* Get the single SET instruction.  */
    3903       168896 :       rtx set = single_set (insn);
    3904       168896 :       rtx src = SET_SRC (set);
    3905       168896 :       rtx dest = SET_DEST (set);
    3906       168896 :       machine_mode mode = GET_MODE (dest);
    3907              : 
    3908       168896 :       rtx replace;
    3909              :       /* Replace the source operand with VECTOR_CONST.  */
    3910       168896 :       if (SUBREG_P (src)
    3911       168896 :           || mode == vector_mode
    3912        61708 :           || CONST_INT_P (vector_const))
    3913              :         replace = vector_const;
    3914              :       else
    3915              :         {
    3916        61701 :           unsigned int size = GET_MODE_SIZE (mode);
    3917        61701 :           if (size < ix86_regmode_natural_size (mode))
    3918              :             {
    3919              :               /* If the mode size is smaller than its natural size,
    3920              :                  first insert an extra move with a QI vector SUBREG
    3921              :                  of the same size to avoid validate_subreg failure.  */
    3922          623 :               machine_mode vmode
    3923          623 :                 = ix86_get_vector_cse_mode (size, scalar_mode);
    3924          623 :               rtx vreg;
    3925          623 :               if (mode == vmode)
    3926              :                 vreg = vector_const;
    3927              :               else
    3928              :                 {
    3929          140 :                   vreg = gen_reg_rtx (vmode);
    3930          140 :                   rtx vsubreg = gen_rtx_SUBREG (vmode, vector_const, 0);
    3931          140 :                   rtx pat = gen_rtx_SET (vreg, vsubreg);
    3932          140 :                   rtx_insn *vinsn = emit_insn_before (pat, insn);
    3933          140 :                   if (dump_file)
    3934              :                     {
    3935            0 :                       fprintf (dump_file, "\nInsert an extra move:\n\n");
    3936            0 :                       print_rtl_single (dump_file, vinsn);
    3937            0 :                       fprintf (dump_file, "\nbefore:\n\n");
    3938            0 :                       print_rtl_single (dump_file, insn);
    3939            0 :                       fprintf (dump_file, "\n");
    3940              :                     }
    3941              :                 }
    3942          623 :               replace = gen_rtx_SUBREG (mode, vreg, 0);
    3943              :             }
    3944              :           else
    3945        61078 :             replace = gen_rtx_SUBREG (mode, vector_const, 0);
    3946              :         }
    3947              : 
    3948       168896 :       if (dump_file)
    3949              :         {
    3950            0 :           fprintf (dump_file, "\nReplace:\n\n");
    3951            0 :           print_rtl_single (dump_file, insn);
    3952              :         }
    3953       168896 :       SET_SRC (set) = replace;
    3954       168896 :       if (CONST_INT_P (replace))
    3955              :         {
    3956        24838 :           dest = gen_lowpart (scalar_mode, dest);
    3957        24838 :           SET_DEST (set) = dest;
    3958              :           /* SET_SRC is now the constant itself, so any REG_EQUAL or
    3959              :              REG_EQUIV note is redundant (PR target/128690).  */
    3960        24838 :           remove_reg_equal_equiv_notes (insn, true);
    3961              :         }
    3962              :       /* Drop possible dead definitions.  */
    3963       168896 :       PATTERN (insn) = set;
    3964       168896 :       INSN_CODE (insn) = -1;
    3965       168896 :       recog_memoized (insn);
    3966              :       /* Add a REG_EQUAL note with the original CONST_VECTOR so that LRA
    3967              :          can rematerialize it instead of spilling DEST.  */
    3968       168896 :       if (REG_P (dest) && !CONST_INT_P (replace) && CONST_VECTOR_P (src))
    3969        91013 :         set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
    3970       168896 :       if (dump_file)
    3971              :         {
    3972            0 :           fprintf (dump_file, "\nwith:\n\n");
    3973            0 :           print_rtl_single (dump_file, insn);
    3974            0 :           fprintf (dump_file, "\n");
    3975              :         }
    3976       168896 :       df_insn_rescan (insn);
    3977              :     }
    3978        67436 : }
    3979              : 
    3980              : /* Return the inner scalar if OP is a broadcast, else return nullptr.  */
    3981              : 
    3982              : static rtx
    3983      2242771 : ix86_broadcast_inner (rtx op, machine_mode mode,
    3984              :                       machine_mode *scalar_mode_p,
    3985              :                       x86_cse_kind *kind_p, rtx_insn **insn_p)
    3986              : {
    3987      2242771 :   switch (standard_sse_constant_p (op, mode))
    3988              :     {
    3989       120656 :     case 1:
    3990       120656 :       *scalar_mode_p = QImode;
    3991       120656 :       *kind_p = X86_CSE_CONST0_VECTOR;
    3992       120656 :       *insn_p = nullptr;
    3993       120656 :       return const0_rtx;
    3994        13870 :     case 2:
    3995        13870 :       *scalar_mode_p = QImode;
    3996        13870 :       *kind_p = X86_CSE_CONSTM1_VECTOR;
    3997        13870 :       *insn_p = nullptr;
    3998        13870 :       return constm1_rtx;
    3999      2108245 :     default:
    4000      2108245 :       break;
    4001              :     }
    4002              : 
    4003      2108245 :   mode = GET_MODE (op);
    4004      2108245 :   int nunits = GET_MODE_NUNITS (mode);
    4005      2108245 :   if (nunits < 2)
    4006              :     return nullptr;
    4007              : 
    4008      1600164 :   bool const_vector_p = CONST_VECTOR_P (op);
    4009      1600164 :   bool duplicated = GET_CODE (op) == VEC_DUPLICATE;
    4010      1600164 :   rtx orig_op = op;
    4011      1600164 :   if (!const_vector_p)
    4012              :     {
    4013              :       /* Check CONST_VECTOR in REG_EQUAL note.  */
    4014      1600144 :       rtx equal = find_reg_equal_equiv_note (*insn_p);
    4015      1600144 :       if (equal)
    4016              :         {
    4017       395306 :           equal = XEXP (equal, 0);
    4018       395306 :           const_vector_p = CONST_VECTOR_P (equal);
    4019              :           /* Use CONST_VECTOR in REG_EQUAL note.  */
    4020       395306 :           if (const_vector_p)
    4021              :             {
    4022              :               /* Handle REG_EQUAL note in:
    4023              : 
    4024              :                  (insn 7 5 12 2 (set (subreg:V8SI (reg:V4DI 100) 0)
    4025              :                         (vec_duplicate:V8SI (reg:SI 102)))
    4026              :                     (expr_list:REG_DEAD (reg:SI 102)
    4027              :                        (expr_list:REG_EQUAL (const_vector:V4DI [
    4028              :                           (const_int -1 [0xffffffffffffffff]) repeated x4]) (nil))))
    4029              : 
    4030              :                  NB: Don't treat it as CONST_VECTOR since EQUAL isn't
    4031              :                  supported by ISAs as in gcc.target/i386/pr40957.c.  */
    4032       279323 :               if (GET_MODE (equal) != mode)
    4033              :                 const_vector_p = false;
    4034              :               else
    4035      1600164 :                 op = equal;
    4036              :             }
    4037              :         }
    4038              :     }
    4039              : 
    4040      1600164 :   machine_mode inner_mode = GET_MODE_INNER (mode);
    4041              : 
    4042      1600164 :   if (const_vector_p)
    4043              :     {
    4044       558612 :       bool int_load_p = GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
    4045       279306 :       *kind_p = X86_CSE_CONST_VECTOR;
    4046       279306 :       if (int_load_p)
    4047              :         {
    4048              :           /* This CONST_VECTOR load can be converted to constant
    4049              :              integer load.  */
    4050        37560 :           *scalar_mode_p = mode;
    4051        37560 :           *insn_p = nullptr;
    4052        37560 :           return op;
    4053              :         }
    4054              : 
    4055              :       /* This CONST_VECTOR is wider than the integer register.  */
    4056       241746 :       rtx first = XVECEXP (op, 0, 0);
    4057              : 
    4058       241746 :       if (duplicated)
    4059              :         {
    4060              :           /* Check if CONST_VECTOR in REG_EQUAL note is duplicated in
    4061              : 
    4062              :              (insn 10 7 12 2 (set (reg:V8SI 128)
    4063              :                 (vec_duplicate:V8SI (vec_select:V2SI (reg:V4SI 180)
    4064              :                         (parallel [(const_int 0 [0])
    4065              :                                    (const_int 1 [0x1])]))))
    4066              :                   (expr_list:REG_EQUAL (const_vector:V8SI [
    4067              :                     (const_int 0 [0])
    4068              :                     (const_int 34 [0x22])
    4069              :                     (const_int 0 [0])
    4070              :                     (const_int 34 [0x22])
    4071              :                     (const_int 0 [0])
    4072              :                     (const_int 34 [0x22])
    4073              :                     (const_int 0 [0])
    4074              :                     (const_int 34 [0x22])])(nil)))
    4075              : 
    4076              :            */
    4077              : 
    4078       211172 :           bool duplicated_const_vector = true;
    4079       211172 :           for (int i = 1; i < nunits; ++i)
    4080              :             {
    4081       138172 :               rtx tmp = XVECEXP (op, 0, i);
    4082       138172 :               if (!rtx_equal_p (tmp, first))
    4083              :                 {
    4084              :                   duplicated_const_vector = false;
    4085              :                   break;
    4086              :                 }
    4087              :             }
    4088              : 
    4089        73016 :           if (duplicated_const_vector)
    4090              :             {
    4091        73000 :               bool const_double_p = CONST_DOUBLE_P (first);
    4092              :               /* Force the floating point constant to memory.  */
    4093        73000 :               if (const_double_p)
    4094         1259 :                 first = validize_mem (force_const_mem (inner_mode, first));
    4095              : 
    4096        73000 :               if (const_double_p || CONST_INT_P (first))
    4097              :                 {
    4098              :                   /* Handle
    4099              : 
    4100              :                      (insn 7 6 8 2 (set (reg:V4SF 99)
    4101              :                           (vec_duplicate:V4SF (mem/u/c:SF (symbol_ref/u:DI ("*.LC2") [flags 0x2]) [0  S4 A32])))
    4102              :                         (expr_list:REG_EQUAL (const_vector:V4SF [
    4103              :                            (const_double:SF 3.4e+1 [0x0.88p+6]) repeated x4]) (nil)))
    4104              : 
    4105              :                      and
    4106              : 
    4107              :                      (insn 14 15 16 3 (set (reg:V4SI 116)
    4108              :                           (vec_duplicate:V4SI (reg:SI 117)))
    4109              :                        (expr_list:REG_EQUAL (const_vector:V4SI [
    4110              :                           (const_int 34 [0x22]) repeated x4]) (nil)))
    4111              : 
    4112              :                    */
    4113        73000 :                   *kind_p = X86_CSE_VEC_DUP;
    4114        73000 :                   *insn_p = nullptr;
    4115        73000 :                   *scalar_mode_p = inner_mode;
    4116        73000 :                   return first;
    4117              :                 }
    4118              :             }
    4119              : 
    4120              :           op = orig_op;
    4121              :         }
    4122              :       else
    4123              :         {
    4124              :           /* Only native CONST_VECTOR is allowed.  */
    4125       168730 :           if (orig_op != op)
    4126              :             return nullptr;
    4127              : 
    4128              :           /* Check if VEC_DUPLICATE can be used.  */
    4129           48 :           for (int i = 1; i < nunits; ++i)
    4130              :             {
    4131           48 :               rtx tmp = XVECEXP (op, 0, i);
    4132              :               /* Vector duplicate value.  */
    4133           48 :               if (!rtx_equal_p (tmp, first))
    4134              :                 return nullptr;
    4135              :             }
    4136              : 
    4137              :           /* Use the inner mode to handle
    4138              :              (const_vector:V2QI [(const_int 0 [0]) repeated x2])
    4139              :            */
    4140            0 :           *scalar_mode_p = inner_mode;
    4141            0 :           *insn_p = nullptr;
    4142            0 :           return first;
    4143              :         }
    4144              :     }
    4145              : 
    4146      1320874 :   if (!duplicated)
    4147              :     return nullptr;
    4148              : 
    4149        25199 :   *kind_p = X86_CSE_VEC_DUP;
    4150              : 
    4151              :   /* Only
    4152              : 
    4153              :      (vec_duplicate:V4SI (reg:SI 99))
    4154              :      (vec_duplicate:V2DF (mem/u/c:DF (symbol_ref/u:DI ("*.LC1") [flags 0x2]) [0 S8 A64]))
    4155              : 
    4156              :      are supported.  Set OP to the broadcast source by default.  */
    4157        25199 :   op = XEXP (op, 0);
    4158        25199 :   rtx reg = op;
    4159        25199 :   if (SUBREG_P (op)
    4160          553 :       && SUBREG_BYTE (op) == 0
    4161        25752 :       && !paradoxical_subreg_p (op))
    4162          553 :     reg = SUBREG_REG (op);
    4163        25199 :   if (!REG_P (reg))
    4164              :     {
    4165         2425 :       if (MEM_P (op)
    4166         2155 :           && SYMBOL_REF_P (XEXP (op, 0))
    4167         2698 :           && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
    4168              :         {
    4169              :           /* Handle constant broadcast from memory.  */
    4170           10 :           *scalar_mode_p = inner_mode;
    4171           10 :           *insn_p = nullptr;
    4172           10 :           return op;
    4173              :         }
    4174              :       return nullptr;
    4175              :     }
    4176              : 
    4177        22774 :   machine_mode orig_mode = mode;
    4178        22774 :   mode = GET_MODE (op);
    4179              : 
    4180              :   /* Only single def chain is supported.  */
    4181        22774 :   df_ref ref = DF_REG_DEF_CHAIN (REGNO (reg));
    4182        22774 :   if (!ref
    4183        22774 :       || DF_REF_IS_ARTIFICIAL (ref)
    4184        22774 :       || DF_REF_NEXT_REG (ref) != nullptr)
    4185              :     return nullptr;
    4186              : 
    4187        15444 :   rtx_insn *insn = DF_REF_INSN (ref);
    4188              : 
    4189              :   /* Skip JUMP which happens with asm goto.  */
    4190        15444 :   if (JUMP_P (insn))
    4191              :     return nullptr;
    4192              : 
    4193        15442 :   rtx set = single_set (insn);
    4194        15442 :   if (!set)
    4195              :     return nullptr;
    4196              : 
    4197        15430 :   rtx src = SET_SRC (set);
    4198              : 
    4199        15430 :   if (CONST_INT_P (src))
    4200              :     {
    4201              :       /* Handle sequences like
    4202              : 
    4203              :          (set (subreg:SI (reg/v:SF 105 [ f ]) 0)
    4204              :               (const_int 0 [0]))
    4205              :          (set (reg:V4SF 110)
    4206              :               (vec_duplicate:V4SF (reg/v:SF 105 [ f ])))
    4207              : 
    4208              :          and
    4209              : 
    4210              :          (set (reg:SI 99)
    4211              :                (const_int 34 [0x22]))
    4212              :          (set (reg:V4SI 98)
    4213              :                (vec_duplicate:V4SI (reg:SI 99)))
    4214              : 
    4215              :          Set *INSN_P to nullptr and return SET_SRC if SET_SRC is an
    4216              :          integer constant.  */
    4217          233 :       op = src;
    4218          233 :       if (SCALAR_INT_MODE_P (mode) && mode != GET_MODE (reg))
    4219            0 :         op = gen_int_mode (INTVAL (src), mode);
    4220          233 :       if (op == const0_rtx)
    4221              :         {
    4222            4 :            if (standard_sse_constant_p (CONST0_RTX (orig_mode),
    4223              :                                         orig_mode) == 1)
    4224              :              {
    4225            4 :                *scalar_mode_p = QImode;
    4226            4 :                *kind_p = X86_CSE_CONST0_VECTOR;
    4227            4 :                *insn_p = nullptr;
    4228            4 :                return const0_rtx;
    4229              :              }
    4230            0 :            op = CONST0_RTX (mode);
    4231              :         }
    4232          229 :       else if (op == constm1_rtx
    4233          229 :                && standard_sse_constant_p (CONSTM1_RTX (orig_mode),
    4234              :                                            orig_mode) == 2)
    4235              :         {
    4236            0 :           *scalar_mode_p = QImode;
    4237            0 :           *kind_p = X86_CSE_CONSTM1_VECTOR;
    4238            0 :           *insn_p = nullptr;
    4239            0 :           return constm1_rtx;
    4240              :         }
    4241              : 
    4242              :       /* Check if we can convert:
    4243              : 
    4244              :          (insn 14 465 412 3 (set (reg:SI 507 [ j_lsm.26 ])
    4245              :                 (const_int 2 [0x2])) "foo.c":10:12 discrim 2 100 {*movsi_internal} (nil))
    4246              :          ...
    4247              :          (insn 518 507 434 16 (set (reg:V2SI 493)
    4248              :                 (vec_duplicate:V2SI (reg:SI 507 [ j_lsm.26 ]))) 2395 {*vec_dupv2si} (nil))
    4249              : 
    4250              :          to constant integer load:
    4251              : 
    4252              :          (insn 566 55 56 6 (set (subreg:DI (reg:V2SI 517) 0)
    4253              :                 (const_int 8589934594 [0x200000002])) -1 (nil))
    4254              :          ...
    4255              :          (insn 518 507 434 16 (set (reg:V2SI 493)
    4256              :                 (reg:V2SI 517)) 2066 {*movv2si_internal} (nil))
    4257              : 
    4258              :        */
    4259          458 :       if (GET_MODE_SIZE (orig_mode) <= UNITS_PER_WORD)
    4260           17 :         *kind_p = X86_CSE_CONST_VECTOR;
    4261              : 
    4262          229 :       *insn_p = nullptr;
    4263              :     }
    4264              :   else
    4265              :     {
    4266              :       /* Handle sequences like
    4267              : 
    4268              :          (set (reg:QI 105 [ c ])
    4269              :               (reg:QI 5 di [ c ]))
    4270              :          (set (reg:V64QI 102 [ _1 ])
    4271              :               (vec_duplicate:V64QI (reg:QI 105 [ c ])))
    4272              : 
    4273              :          (set (reg/v:SI 116 [ argc ])
    4274              :               (mem/c:SI (reg:SI 135) [2 argc+0 S4 A32]))
    4275              :          (set (reg:V4SI 119 [ _45 ])
    4276              :               (vec_duplicate:V4SI (reg/v:SI 116 [ argc ])))
    4277              : 
    4278              :          (set (reg:SI 98 [ _1 ])
    4279              :               (sign_extend:SI (reg:QI 106 [ c ])))
    4280              :          (set (reg:V16SI 103 [ _2 ])
    4281              :                (vec_duplicate:V16SI (reg:SI 98 [ _1 ])))
    4282              : 
    4283              :          (set (reg:SI 102 [ cost ])
    4284              :               (mem/c:SI (symbol_ref:DI ("cost") [flags 0x40])))
    4285              :          (set (reg:V4HI 103 [ _16 ])
    4286              :               (vec_duplicate:V4HI (subreg:HI (reg:SI 102 [ cost ]) 0)))
    4287              : 
    4288              :          (set (subreg:SI (reg/v:HI 107 [ cr_val ]) 0)
    4289              :               (ashift:SI (reg:SI 158)
    4290              :                          (subreg:QI (reg:SI 156 [ _2 ]) 0)))
    4291              :          (set (reg:V16HI 183 [ _61 ])
    4292              :               (vec_duplicate:V16HI (reg/v:HI 107 [ cr_val ])))
    4293              : 
    4294              :          Set *INSN_P to INSN and return the broadcast source otherwise.  */
    4295        15197 :       *insn_p = insn;
    4296              :     }
    4297              : 
    4298        15426 :   *scalar_mode_p = mode;
    4299        15426 :   return op;
    4300              : }
    4301              : 
    4302              : /* Replace CALL instruction in TLS_CALL_INSNS with SET from SRC and
    4303              :    put the updated instruction in UPDATED_TLS_INSNS.  */
    4304              : 
    4305              : static void
    4306          323 : replace_tls_call (rtx src, auto_bitmap &tls_call_insns,
    4307              :                   auto_bitmap &updated_tls_insns)
    4308              : {
    4309          323 :   bitmap_iterator bi;
    4310          323 :   unsigned int id;
    4311              : 
    4312         1771 :   EXECUTE_IF_SET_IN_BITMAP (tls_call_insns, 0, id, bi)
    4313              :     {
    4314         1448 :       rtx_insn *insn = DF_INSN_UID_GET (id)->insn;
    4315              : 
    4316              :       /* If this isn't a CALL, only GNU2 TLS implicit CALL patterns are
    4317              :          allowed.  */
    4318         1448 :       if (!CALL_P (insn))
    4319              :         {
    4320           47 :           attr_tls64 tls64 = get_attr_tls64 (insn);
    4321           47 :           if (tls64 != TLS64_CALL && tls64 != TLS64_COMBINE)
    4322            0 :             gcc_unreachable ();
    4323              :         }
    4324              : 
    4325         1448 :       rtx pat = PATTERN (insn);
    4326         1448 :       gcc_assert (GET_CODE (pat) == PARALLEL);
    4327         1448 :       rtx set = XVECEXP (pat, 0, 0);
    4328         1448 :       gcc_assert (GET_CODE (set) == SET);
    4329         1448 :       rtx dest = SET_DEST (set);
    4330              : 
    4331         1448 :       set = gen_rtx_SET (dest, src);
    4332         1448 :       rtx_insn *set_insn = emit_insn_after (set, insn);
    4333         1448 :       if (recog_memoized (set_insn) < 0)
    4334            0 :         gcc_unreachable ();
    4335              : 
    4336              :       /* Put SET_INSN in UPDATED_TLS_INSNS.  */
    4337         1448 :       bitmap_set_bit (updated_tls_insns, INSN_UID (set_insn));
    4338              : 
    4339         1448 :       if (dump_file)
    4340              :         {
    4341            0 :           fprintf (dump_file, "\nReplace:\n\n");
    4342            0 :           print_rtl_single (dump_file, insn);
    4343            0 :           fprintf (dump_file, "\nwith:\n\n");
    4344            0 :           print_rtl_single (dump_file, set_insn);
    4345            0 :           fprintf (dump_file, "\n");
    4346              :         }
    4347              : 
    4348              :       /* Delete the CALL insn.  */
    4349         1448 :       delete_insn (insn);
    4350              : 
    4351         1448 :       df_insn_rescan (set_insn);
    4352              :     }
    4353          323 : }
    4354              : 
    4355              : /* Return the basic block which dominates all basic blocks which set
    4356              :    hard register REGNO used in basic block BB.  */
    4357              : 
    4358              : static basic_block
    4359            2 : ix86_get_dominator_for_reg (unsigned int regno, basic_block bb)
    4360              : {
    4361            2 :   basic_block set_bb;
    4362            2 :   auto_bitmap set_bbs;
    4363              : 
    4364              :   /* Get all BBs which set REGNO and dominate the current BB from all
    4365              :      DEFs of REGNO.  */
    4366            2 :   for (df_ref def = DF_REG_DEF_CHAIN (regno);
    4367           18 :        def;
    4368           16 :        def = DF_REF_NEXT_REG (def))
    4369           16 :     if (!DF_REF_IS_ARTIFICIAL (def)
    4370           16 :         && !DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER)
    4371            6 :         && !DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
    4372              :       {
    4373            4 :         set_bb = DF_REF_BB (def);
    4374            4 :         if (dominated_by_p (CDI_DOMINATORS, bb, set_bb))
    4375            2 :           bitmap_set_bit (set_bbs, set_bb->index);
    4376              :       }
    4377              : 
    4378            2 :   bb = nearest_common_dominator_for_set (CDI_DOMINATORS, set_bbs);
    4379            2 :   return bb;
    4380            2 : }
    4381              : 
    4382              : /* Mark FLAGS register as live in DATA, a bitmap of live caller-saved
    4383              :    registers, if DEST is FLAGS register.  */
    4384              : 
    4385              : static void
    4386          388 : ix86_check_flags_reg (rtx dest, const_rtx x, void *data)
    4387              : {
    4388          388 :   if (GET_CODE (x) == CLOBBER)
    4389              :     return;
    4390              : 
    4391          381 :   auto_bitmap *live_caller_saved_regs = (auto_bitmap *) data;
    4392          381 :   if (REG_P (dest) && REGNO (dest) == FLAGS_REG)
    4393            0 :     bitmap_set_bit (*live_caller_saved_regs, FLAGS_REG);
    4394              : }
    4395              : 
    4396              : /* Emit a TLS_SET instruction of KIND in basic block BB.   Store the
    4397              :    insertion point in *BEFORE_P for emit_insn_before or in *AFTER_P
    4398              :    for emit_insn_after.  UPDATED_GNU_TLS_INSNS contains instructions
    4399              :    which replace the GNU TLS instructions.  UPDATED_GNU2_TLS_INSNS
    4400              :    contains instructions which replace the GNU2 TLS instructions.  */
    4401              : 
    4402              : static rtx_insn *
    4403          323 : ix86_emit_tls_call (rtx tls_set, x86_cse_kind kind, basic_block bb,
    4404              :                     rtx_insn **before_p, rtx_insn **after_p,
    4405              :                     auto_bitmap &updated_gnu_tls_insns,
    4406              :                     auto_bitmap &updated_gnu2_tls_insns)
    4407              : {
    4408          325 :   rtx_insn *tls_insn;
    4409              : 
    4410          325 :   do
    4411              :     {
    4412          325 :       rtx_insn *insn = BB_HEAD (bb);
    4413         1296 :       while (insn && !NONDEBUG_INSN_P (insn))
    4414              :         {
    4415          975 :           if (insn == BB_END (bb))
    4416              :             {
    4417              :               /* This must be the beginning basic block:
    4418              : 
    4419              :                  (note 4 0 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
    4420              :                  (note 2 4 26 2 NOTE_INSN_FUNCTION_BEG)
    4421              : 
    4422              :                  or a basic block with only a label:
    4423              : 
    4424              :                  (code_label 78 11 77 3 14 (nil) [1 uses])
    4425              :                  (note 77 78 54 3 [bb 3] NOTE_INSN_BASIC_BLOCK)
    4426              : 
    4427              :                  or a basic block with only a debug marker:
    4428              : 
    4429              :                  (note 3 0 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
    4430              :                  (note 2 3 5 2 NOTE_INSN_FUNCTION_BEG)
    4431              :                  (debug_insn 5 2 16 2 (debug_marker) "x.c":6:3 -1 (nil))
    4432              : 
    4433              :                  or a basic block with only deleted instructions:
    4434              : 
    4435              :                  (code_label 348 23 349 45 3 (nil) [0 uses])
    4436              :                  (note 349 348 436 45 [bb 45] NOTE_INSN_BASIC_BLOCK)
    4437              :                  (note 436 349 362 45 NOTE_INSN_DELETED)
    4438              : 
    4439              :                */
    4440            4 :               gcc_assert (DEBUG_INSN_P (insn)
    4441              :                           || (NOTE_P (insn)
    4442              :                               && ((NOTE_KIND (insn)
    4443              :                                    == NOTE_INSN_FUNCTION_BEG)
    4444              :                                   || (NOTE_KIND (insn)
    4445              :                                       == NOTE_INSN_DELETED)
    4446              :                                   || (NOTE_KIND (insn)
    4447              :                                       == NOTE_INSN_BASIC_BLOCK))));
    4448              :               insn = NULL;
    4449              :               break;
    4450              :             }
    4451          971 :           insn = NEXT_INSN (insn);
    4452              :         }
    4453              : 
    4454              :       /* TLS_GD and TLS_LD_BASE instructions are normal functions which
    4455              :          clobber caller-saved registers.  TLSDESC instructions only
    4456              :          clobber FLAGS.  If any registers clobbered by TLS instructions
    4457              :          are live in this basic block, we must insert TLS instructions
    4458              :          after all live registers clobbered are dead.  */
    4459              : 
    4460          325 :       auto_bitmap live_caller_saved_regs;
    4461          650 :       bitmap in = df_live ? DF_LIVE_IN (bb) : DF_LR_IN (bb);
    4462              : 
    4463          325 :       if (bitmap_bit_p (in, FLAGS_REG))
    4464            4 :         bitmap_set_bit (live_caller_saved_regs, FLAGS_REG);
    4465              : 
    4466          325 :       unsigned int i;
    4467              : 
    4468              :       /* Get all live caller-saved registers for TLS_GD and TLS_LD_BASE
    4469              :          instructions.  */
    4470          325 :       if (kind != X86_CSE_TLSDESC)
    4471              :         {
    4472          303 :           const predefined_function_abi &tls_abi
    4473          303 :             = ix86_tls_get_addr_abi ();
    4474        29088 :           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
    4475        28482 :             if (tls_abi.clobbers_full_reg_p (i)
    4476        25758 :                 && !fixed_regs[i]
    4477        40953 :                 && bitmap_bit_p (in, i))
    4478          350 :               bitmap_set_bit (live_caller_saved_regs, i);
    4479          303 :           if (df_live && crtl->drap_reg)
    4480              :             {
    4481              :               /* DRAP has no reaching definition at this point, so df_live drops
    4482              :                  it above.  Its hard register can also go dead mid-function once
    4483              :                  copied elsewhere (e.g. right after the prologue), so query
    4484              :                  DF_LR_IN per-block rather than treating it as live whenever
    4485              :                  crtl->drap_reg is set.  */
    4486            1 :               i = REGNO (crtl->drap_reg);
    4487            2 :               if (bitmap_bit_p (DF_LR_IN (bb), i))
    4488            1 :                 bitmap_set_bit (live_caller_saved_regs, i);
    4489              :             }
    4490              :         }
    4491              : 
    4492          325 :       if (bitmap_empty_p (live_caller_saved_regs))
    4493              :         {
    4494           85 :           if (insn == BB_HEAD (bb))
    4495              :             {
    4496            0 :               *before_p = insn;
    4497            0 :               tls_insn = emit_insn_before (tls_set, insn);
    4498              :             }
    4499              :           else
    4500              :             {
    4501              :               /* Emit the TLS call after NOTE_INSN_FUNCTION_BEG in the
    4502              :                  beginning basic block:
    4503              : 
    4504              :                  (note 4 0 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
    4505              :                  (note 2 4 26 2 NOTE_INSN_FUNCTION_BEG)
    4506              : 
    4507              :                  or after NOTE_INSN_BASIC_BLOCK in a basic block with
    4508              :                  only a label:
    4509              : 
    4510              :                  (code_label 78 11 77 3 14 (nil) [1 uses])
    4511              :                  (note 77 78 54 3 [bb 3] NOTE_INSN_BASIC_BLOCK)
    4512              : 
    4513              :                  or after debug marker in a basic block with only a
    4514              :                  debug marker:
    4515              : 
    4516              :                  (note 3 0 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
    4517              :                  (note 2 3 5 2 NOTE_INSN_FUNCTION_BEG)
    4518              :                  (debug_insn 5 2 16 2 (debug_marker) "x.c":6:3 -1 (nil))
    4519              : 
    4520              :                */
    4521           85 :               insn = insn ? PREV_INSN (insn) : BB_END (bb);
    4522           85 :               *after_p = insn;
    4523           85 :               tls_insn = emit_insn_after (tls_set, insn);
    4524              :             }
    4525              :           return tls_insn;
    4526              :         }
    4527              : 
    4528          240 :       bool repeat = false;
    4529              : 
    4530              :       /* Search for REG_DEAD notes in this basic block.  */
    4531          675 :       FOR_BB_INSNS (bb, insn)
    4532              :         {
    4533          675 :           if (!NONDEBUG_INSN_P (insn))
    4534          290 :             continue;
    4535              : 
    4536              :           /* NB: Conditional jump is the only instruction which reads
    4537              :              flags register and changes control flow.  We can never
    4538              :              place the TLS call after unconditional jump.  */
    4539          385 :           if (JUMP_P (insn))
    4540              :             {
    4541              :               /* This must be a conditional jump.  */
    4542            2 :               rtx label = JUMP_LABEL (insn);
    4543            2 :               if (label == nullptr
    4544            2 :                   || ANY_RETURN_P (label)
    4545            2 :                   || !(LABEL_P (label) || SYMBOL_REF_P (label)))
    4546            0 :                 gcc_unreachable ();
    4547              : 
    4548              :               /* Place the call before all FLAGS_REG setting BBs since
    4549              :                  we can't place a call before nor after a conditional
    4550              :                  jump.  */
    4551            2 :               bb = ix86_get_dominator_for_reg (FLAGS_REG, bb);
    4552              : 
    4553              :               /* Start over again.  */
    4554            2 :               repeat = true;
    4555            2 :               break;
    4556              :             }
    4557              : 
    4558          383 :           if (bitmap_bit_p (updated_gnu_tls_insns, INSN_UID (insn)))
    4559              :             {
    4560              :               /* Insert the __tls_get_addr call before INSN which
    4561              :                  replaces a __tls_get_addr call.  */
    4562            1 :               *before_p = insn;
    4563            1 :               tls_insn = emit_insn_before (tls_set, insn);
    4564            1 :               return tls_insn;
    4565              :             }
    4566              : 
    4567          382 :           if (bitmap_bit_p (updated_gnu2_tls_insns, INSN_UID (insn)))
    4568              :             {
    4569              :               /* Mark FLAGS register as dead since FLAGS register
    4570              :                  would be clobbered by the GNU2 TLS instruction.  */
    4571            1 :               bitmap_clear_bit (live_caller_saved_regs, FLAGS_REG);
    4572            1 :               continue;
    4573              :             }
    4574              : 
    4575              :           /* Check if FLAGS register is live.  */
    4576          381 :           note_stores (insn, ix86_check_flags_reg,
    4577              :                        &live_caller_saved_regs);
    4578              : 
    4579          381 :           rtx link;
    4580          522 :           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
    4581          378 :             if ((REG_NOTE_KIND (link) == REG_DEAD
    4582            9 :                  || (REG_NOTE_KIND (link) == REG_UNUSED
    4583            7 :                      && REGNO (XEXP (link, 0)) == FLAGS_REG))
    4584          385 :                 && REG_P (XEXP (link, 0)))
    4585              :               {
    4586              :                 /* Mark the live caller-saved register as dead.  */
    4587          757 :                 for (i = REGNO (XEXP (link, 0));
    4588          757 :                      i < END_REGNO (XEXP (link, 0));
    4589              :                      i++)
    4590          381 :                   if (i < FIRST_PSEUDO_REGISTER)
    4591          358 :                     bitmap_clear_bit (live_caller_saved_regs, i);
    4592              : 
    4593          376 :                 if (bitmap_empty_p (live_caller_saved_regs))
    4594              :                   {
    4595          237 :                     *after_p = insn;
    4596          237 :                     tls_insn = emit_insn_after (tls_set, insn);
    4597          237 :                     return tls_insn;
    4598              :                   }
    4599              :               }
    4600              :         }
    4601              : 
    4602              :       /* NB: Start over again for conditional jump.  */
    4603            2 :       if (repeat)
    4604            2 :         continue;
    4605              : 
    4606            0 :       gcc_assert (!bitmap_empty_p (live_caller_saved_regs));
    4607              : 
    4608              :       /* If any live caller-saved registers aren't dead at the end of
    4609              :          this basic block, get the basic block which dominates all
    4610              :          basic blocks which set the remaining live registers.  */
    4611            0 :       auto_bitmap set_bbs;
    4612            0 :       bitmap_iterator bi;
    4613            0 :       unsigned int id;
    4614            0 :       EXECUTE_IF_SET_IN_BITMAP (live_caller_saved_regs, 0, id, bi)
    4615              :         {
    4616            0 :           basic_block set_bb = ix86_get_dominator_for_reg (id, bb);
    4617            0 :           bitmap_set_bit (set_bbs, set_bb->index);
    4618              :         }
    4619            0 :       bb = nearest_common_dominator_for_set (CDI_DOMINATORS, set_bbs);
    4620            2 :     }
    4621              :   while (true);
    4622              : }
    4623              : 
    4624              : /* Generate a TLS call of KIND with VAL and copy the call result to DEST,
    4625              :    at entry of the nearest dominator for basic block map BBS, which is in
    4626              :    the fake loop that contains the whole function, so that there is only
    4627              :    a single TLS CALL of KIND with VAL in the whole function.
    4628              :    UPDATED_GNU_TLS_INSNS contains instructions which replace the GNU TLS
    4629              :    instructions.  UPDATED_GNU2_TLS_INSNS contains instructions which
    4630              :    replace the GNU2 TLS instructions.  If TLSDESC_SET isn't nullptr,
    4631              :    insert it before the TLS call.  */
    4632              : 
    4633              : static void
    4634          323 : ix86_place_single_tls_call (rtx dest, rtx val, x86_cse_kind kind,
    4635              :                             auto_bitmap &bbs,
    4636              :                             auto_bitmap &updated_gnu_tls_insns,
    4637              :                             auto_bitmap &updated_gnu2_tls_insns,
    4638              :                             rtx tlsdesc_set = nullptr)
    4639              : {
    4640          323 :   basic_block bb = nearest_common_dominator_for_set (CDI_DOMINATORS, bbs);
    4641          323 :   while (bb->loop_father->latch
    4642          333 :          != EXIT_BLOCK_PTR_FOR_FN (cfun))
    4643           10 :     bb = get_immediate_dominator (CDI_DOMINATORS,
    4644              :                                   bb->loop_father->header);
    4645              : 
    4646          323 :   rtx rax = nullptr, rdi;
    4647          323 :   rtx eqv = nullptr;
    4648          323 :   rtx caddr;
    4649          323 :   rtx set;
    4650          323 :   rtx clob;
    4651          323 :   rtx symbol;
    4652          323 :   rtx tls;
    4653              : 
    4654          323 :   switch (kind)
    4655              :     {
    4656          265 :     case X86_CSE_TLS_GD:
    4657          265 :       rax = gen_rtx_REG (Pmode, AX_REG);
    4658          265 :       rdi = gen_rtx_REG (Pmode, DI_REG);
    4659          265 :       caddr = ix86_tls_get_addr ();
    4660              : 
    4661          265 :       symbol = XVECEXP (val, 0, 0);
    4662          265 :       tls = gen_tls_global_dynamic_64 (Pmode, rax, symbol, caddr, rdi);
    4663          265 :       CALL_INSN_ABI_ID (tls) = ix86_tls_get_addr_abi ().id ();
    4664              : 
    4665          265 :       if (GET_MODE (symbol) != Pmode)
    4666            0 :         symbol = gen_rtx_ZERO_EXTEND (Pmode, symbol);
    4667              :       eqv = symbol;
    4668              :       break;
    4669              : 
    4670           37 :     case X86_CSE_TLS_LD_BASE:
    4671           37 :       rax = gen_rtx_REG (Pmode, AX_REG);
    4672           37 :       rdi = gen_rtx_REG (Pmode, DI_REG);
    4673           37 :       caddr = ix86_tls_get_addr ();
    4674              : 
    4675           37 :       tls = gen_tls_local_dynamic_base_64 (Pmode, rax, caddr, rdi);
    4676           37 :       CALL_INSN_ABI_ID (tls) = ix86_tls_get_addr_abi ().id ();
    4677              : 
    4678              :       /* Attach a unique REG_EQUAL to DEST, to allow the RTL optimizers
    4679              :          to share the LD_BASE result with other LD model accesses.  */
    4680           37 :       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
    4681              :                             UNSPEC_TLS_LD_BASE);
    4682              : 
    4683           37 :       break;
    4684              : 
    4685           21 :     case X86_CSE_TLSDESC:
    4686           21 :       set = gen_rtx_SET (dest, val);
    4687           21 :       clob = gen_rtx_CLOBBER (VOIDmode,
    4688              :                               gen_rtx_REG (CCmode, FLAGS_REG));
    4689           21 :       tls = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clob));
    4690           21 :       break;
    4691              : 
    4692            0 :     default:
    4693            0 :       gcc_unreachable ();
    4694              :     }
    4695              : 
    4696              :   /* Emit the TLS CALL insn.  */
    4697          323 :   rtx_insn *before = nullptr;
    4698          323 :   rtx_insn *after = nullptr;
    4699          323 :   rtx_insn *tls_insn = ix86_emit_tls_call (tls, kind, bb, &before,
    4700              :                                            &after,
    4701              :                                            updated_gnu_tls_insns,
    4702              :                                            updated_gnu2_tls_insns);
    4703              : 
    4704          323 :   rtx_insn *tlsdesc_insn = nullptr;
    4705          323 :   if (tlsdesc_set)
    4706              :     {
    4707           16 :       rtx dest = copy_rtx (SET_DEST (tlsdesc_set));
    4708           16 :       rtx src = copy_rtx (SET_SRC (tlsdesc_set));
    4709           16 :       tlsdesc_set = gen_rtx_SET (dest, src);
    4710           16 :       tlsdesc_insn = emit_insn_before (tlsdesc_set, tls_insn);
    4711              :     }
    4712              : 
    4713          323 :   if (kind != X86_CSE_TLSDESC)
    4714              :     {
    4715          302 :       RTL_CONST_CALL_P (tls_insn) = 1;
    4716              : 
    4717              :       /* Indicate that this function can't jump to non-local gotos.  */
    4718          302 :       make_reg_eh_region_note_nothrow_nononlocal (tls_insn);
    4719              :     }
    4720              : 
    4721          323 :   if (recog_memoized (tls_insn) < 0)
    4722            0 :     gcc_unreachable ();
    4723              : 
    4724          323 :   if (dump_file)
    4725              :     {
    4726            0 :       if (after)
    4727              :         {
    4728            0 :           fprintf (dump_file, "\nPlace:\n\n");
    4729            0 :           if (tlsdesc_insn)
    4730            0 :             print_rtl_single (dump_file, tlsdesc_insn);
    4731            0 :           print_rtl_single (dump_file, tls_insn);
    4732            0 :           fprintf (dump_file, "\nafter:\n\n");
    4733            0 :           print_rtl_single (dump_file, after);
    4734            0 :           fprintf (dump_file, "\n");
    4735              :         }
    4736              :       else
    4737              :         {
    4738            0 :           fprintf (dump_file, "\nPlace:\n\n");
    4739            0 :           if (tlsdesc_insn)
    4740            0 :             print_rtl_single (dump_file, tlsdesc_insn);
    4741            0 :           print_rtl_single (dump_file, tls_insn);
    4742            0 :           fprintf (dump_file, "\nbefore:\n\n");
    4743            0 :           print_rtl_single (dump_file, before);
    4744            0 :           fprintf (dump_file, "\n");
    4745              :         }
    4746              :     }
    4747              : 
    4748          323 :   if (kind != X86_CSE_TLSDESC)
    4749              :     {
    4750              :       /* Copy RAX to DEST.  */
    4751          302 :       set = gen_rtx_SET (dest, rax);
    4752          302 :       rtx_insn *set_insn = emit_insn_after (set, tls_insn);
    4753          302 :       set_dst_reg_note (set_insn, REG_EQUAL, copy_rtx (eqv), dest);
    4754          302 :       if (dump_file)
    4755              :         {
    4756            0 :           fprintf (dump_file, "\nPlace:\n\n");
    4757            0 :           print_rtl_single (dump_file, set_insn);
    4758            0 :           fprintf (dump_file, "\nafter:\n\n");
    4759            0 :           print_rtl_single (dump_file, tls_insn);
    4760            0 :           fprintf (dump_file, "\n");
    4761              :         }
    4762              :     }
    4763          323 : }
    4764              : 
    4765              : namespace {
    4766              : 
    4767              : const pass_data pass_data_x86_cse =
    4768              : {
    4769              :   RTL_PASS, /* type */
    4770              :   "x86_cse", /* name */
    4771              :   OPTGROUP_NONE, /* optinfo_flags */
    4772              :   TV_MACH_DEP, /* tv_id */
    4773              :   0, /* properties_required */
    4774              :   0, /* properties_provided */
    4775              :   0, /* properties_destroyed */
    4776              :   0, /* todo_flags_start */
    4777              :   0, /* todo_flags_finish */
    4778              : };
    4779              : 
    4780              : class pass_x86_cse : public rtl_opt_pass
    4781              : {
    4782              : public:
    4783       302985 :   pass_x86_cse (gcc::context *ctxt)
    4784       605970 :     : rtl_opt_pass (pass_data_x86_cse, ctxt)
    4785              :   {}
    4786              : 
    4787              :   /* opt_pass methods: */
    4788      1531131 :   bool gate (function *fun) final override
    4789              :     {
    4790      1531131 :       return optimize && optimize_function_for_speed_p (fun);
    4791              :     }
    4792              : 
    4793      1003045 :   unsigned int execute (function *) final override
    4794              :     {
    4795      1003045 :       return x86_cse ();
    4796              :     }
    4797              : 
    4798              : private:
    4799              :   /* The redundant source value.  */
    4800              :   rtx val;
    4801              :   /* The actual redundant source value for UNSPEC_TLSDESC.  */
    4802              :   rtx tlsdesc_val;
    4803              :   /* The instruction which defines the redundant value.  */
    4804              :   rtx_insn *def_insn;
    4805              :   /* Mode of the destination of the candidate redundant instruction.  */
    4806              :   machine_mode mode;
    4807              :   /* Mode of the source of the candidate redundant instruction.  */
    4808              :   machine_mode scalar_mode;
    4809              :   /* The classification of the candidate redundant instruction.  */
    4810              :   x86_cse_kind kind;
    4811              : 
    4812              :   unsigned int x86_cse (void);
    4813              :   bool candidate_gnu_tls_p (rtx_insn *, attr_tls64);
    4814              :   bool candidate_gnu2_tls_p (rtx, attr_tls64);
    4815              :   bool candidate_vector_p (rtx, rtx_insn *);
    4816              :   rtx_insn *tls_set_insn_from_symbol (const_rtx, const_rtx);
    4817              : }; // class pass_x86_cse
    4818              : 
    4819              : /* Return the instruction which sets REG from TLS_SYMBOL.  */
    4820              : 
    4821              : rtx_insn *
    4822           42 : pass_x86_cse::tls_set_insn_from_symbol (const_rtx reg,
    4823              :                                         const_rtx tls_symbol)
    4824              : {
    4825           42 :   rtx_insn *set_insn = nullptr;
    4826           42 :   for (df_ref ref = DF_REG_DEF_CHAIN (REGNO (reg));
    4827          111 :        ref;
    4828           69 :        ref = DF_REF_NEXT_REG (ref))
    4829              :     {
    4830           69 :       if (DF_REF_IS_ARTIFICIAL (ref))
    4831              :         return nullptr;
    4832              : 
    4833           69 :       set_insn = DF_REF_INSN (ref);
    4834           69 :       if (get_attr_tls64 (set_insn) != TLS64_LEA)
    4835              :         return nullptr;
    4836              : 
    4837           69 :       rtx tls_set = PATTERN (set_insn);
    4838           69 :       rtx tls_src = XVECEXP (SET_SRC (tls_set), 0, 0);
    4839           69 :       if (!rtx_equal_p (tls_symbol, tls_src))
    4840              :         return nullptr;
    4841              :     }
    4842              : 
    4843              :   return set_insn;
    4844              : }
    4845              : 
    4846              : /* Return true and output def_insn, val, mode, scalar_mode and kind if
    4847              :    INSN is UNSPEC_TLS_GD or UNSPEC_TLS_LD_BASE.  */
    4848              : 
    4849              : bool
    4850         2219 : pass_x86_cse::candidate_gnu_tls_p (rtx_insn *insn, attr_tls64 tls64)
    4851              : {
    4852         2219 :   if (!TARGET_64BIT || !cfun->machine->tls_descriptor_call_multiple_p)
    4853              :     return false;
    4854              : 
    4855              :   /* Record the redundant TLS CALLs for 64-bit:
    4856              : 
    4857              :      (parallel [
    4858              :         (set (reg:DI 0 ax)
    4859              :              (call:DI (mem:QI (symbol_ref:DI ("__tls_get_addr")))
    4860              :                       (const_int 0 [0])))
    4861              :         (unspec:DI [(symbol_ref:DI ("foo") [flags 0x50])
    4862              :                     (reg/f:DI 7 sp)] UNSPEC_TLS_GD)
    4863              :         (clobber (reg:DI 5 di))])
    4864              : 
    4865              : 
    4866              :      and
    4867              : 
    4868              :      (parallel [
    4869              :         (set (reg:DI 0 ax)
    4870              :              (call:DI (mem:QI (symbol_ref:DI ("__tls_get_addr")))
    4871              :                       (const_int 0 [0])))
    4872              :         (unspec:DI [(reg/f:DI 7 sp)] UNSPEC_TLS_LD_BASE)])
    4873              : 
    4874              :    */
    4875              : 
    4876         2045 :   rtx pat = PATTERN (insn);
    4877         2045 :   rtx set = XVECEXP (pat, 0, 0);
    4878         2045 :   gcc_assert (GET_CODE (set) == SET);
    4879         2045 :   rtx dest = SET_DEST (set);
    4880         2045 :   scalar_mode = mode = GET_MODE (dest);
    4881         2045 :   val = XVECEXP (pat, 0, 1);
    4882         2045 :   gcc_assert (GET_CODE (val) == UNSPEC);
    4883              : 
    4884         2045 :   if (tls64 == TLS64_GD)
    4885              :     kind = X86_CSE_TLS_GD;
    4886              :   else
    4887          115 :     kind = X86_CSE_TLS_LD_BASE;
    4888              : 
    4889         2045 :   def_insn = nullptr;
    4890         2045 :   return true;
    4891              : }
    4892              : 
    4893              : /* Return true and output def_insn, val, mode, scalar_mode and kind if
    4894              :    SET is UNSPEC_TLSDESC.  */
    4895              : 
    4896              : bool
    4897           56 : pass_x86_cse::candidate_gnu2_tls_p (rtx set, attr_tls64 tls64)
    4898              : {
    4899           56 :   if (!TARGET_64BIT || !cfun->machine->tls_descriptor_call_multiple_p)
    4900              :     return false;
    4901              : 
    4902           54 :   rtx tls_symbol;
    4903           54 :   rtx_insn *set_insn;
    4904           54 :   rtx src = SET_SRC (set);
    4905           54 :   val = src;
    4906           54 :   tlsdesc_val = src;
    4907           54 :   kind = X86_CSE_TLSDESC;
    4908              : 
    4909           54 :   if (tls64 == TLS64_COMBINE)
    4910              :     {
    4911              :       /* Record 64-bit TLS64_COMBINE:
    4912              : 
    4913              :          (set (reg/f:DI 104)
    4914              :               (plus:DI (unspec:DI [
    4915              :                           (symbol_ref:DI ("_TLS_MODULE_BASE_") [flags 0x10])
    4916              :                           (reg:DI 114)
    4917              :                           (reg/f:DI 7 sp)] UNSPEC_TLSDESC)
    4918              :                        (const:DI (unspec:DI [
    4919              :                                     (symbol_ref:DI ("e") [flags 0x1a])
    4920              :                                   ] UNSPEC_DTPOFF))))
    4921              : 
    4922              :          (set (reg/f:DI 104)
    4923              :               (plus:DI (unspec:DI [
    4924              :                           (symbol_ref:DI ("_TLS_MODULE_BASE_") [flags 0x10])
    4925              :                           (unspec:DI [
    4926              :                              (symbol_ref:DI ("_TLS_MODULE_BASE_") [flags 0x10])
    4927              :                           ] UNSPEC_TLSDESC)
    4928              :                           (reg/f:DI 7 sp)] UNSPEC_TLSDESC)
    4929              :                        (const:DI (unspec:DI [
    4930              :                                     (symbol_ref:DI ("e") [flags 0x1a])
    4931              :                                  ] UNSPEC_DTPOFF))))
    4932              :      */
    4933              : 
    4934           12 :       scalar_mode = mode = GET_MODE (src);
    4935              : 
    4936              :       /* Since the first operand of PLUS in the source TLS_COMBINE
    4937              :          pattern is unused, use the second operand of PLUS:
    4938              : 
    4939              :          (const:DI (unspec:DI [
    4940              :                       (symbol_ref:DI ("e") [flags 0x1a])
    4941              :                    ] UNSPEC_DTPOFF))
    4942              : 
    4943              :          as VAL to check if 2 TLS_COMBINE patterns have the same
    4944              :          source.  */
    4945           12 :       val = XEXP (src, 1);
    4946           12 :       gcc_assert (GET_CODE (val) == CONST
    4947              :                   && GET_CODE (XEXP (val, 0)) == UNSPEC
    4948              :                       && XINT (XEXP (val, 0), 1) == UNSPEC_DTPOFF
    4949              :                       && SYMBOL_REF_P (XVECEXP (XEXP (val, 0), 0, 0)));
    4950           12 :       def_insn = nullptr;
    4951           12 :       return true;
    4952              :     }
    4953              : 
    4954              :   /* Record 64-bit TLS_CALL:
    4955              : 
    4956              :      (set (reg:DI 101)
    4957              :           (unspec:DI [(symbol_ref:DI ("foo") [flags 0x50])
    4958              :                       (reg:DI 112)
    4959              :                       (reg/f:DI 7 sp)] UNSPEC_TLSDESC))
    4960              : 
    4961              :    */
    4962              : 
    4963           42 :   gcc_assert (GET_CODE (src) == UNSPEC);
    4964           42 :   tls_symbol = XVECEXP (src, 0, 0);
    4965           42 :   src = XVECEXP (src, 0, 1);
    4966           42 :   scalar_mode = mode = GET_MODE (src);
    4967           42 :   gcc_assert (REG_P (src));
    4968              : 
    4969              :   /* All definitions of reg:DI 129 in
    4970              : 
    4971              :      (set (reg:DI 110)
    4972              :           (unspec:DI [(symbol_ref:DI ("foo"))
    4973              :                       (reg:DI 129)
    4974              :                       (reg/f:DI 7 sp)] UNSPEC_TLSDESC))
    4975              : 
    4976              :      should have the same source as in
    4977              : 
    4978              :      (set (reg:DI 129)
    4979              :           (unspec:DI [(symbol_ref:DI ("foo"))] UNSPEC_TLSDESC))
    4980              : 
    4981              :    */
    4982              : 
    4983           42 :   set_insn = tls_set_insn_from_symbol (src, tls_symbol);
    4984           42 :   if (!set_insn)
    4985              :     return false;
    4986              : 
    4987              :   /* Use TLS_SYMBOL as VAL to check if 2 patterns have the same source.  */
    4988           42 :   val = tls_symbol;
    4989           42 :   def_insn = set_insn;
    4990           42 :   return true;
    4991              : }
    4992              : 
    4993              : /* Return true and output def_insn, val, mode, scalar_mode and kind if
    4994              :   INSN is a vector broadcast instruction.  */
    4995              : 
    4996              : bool
    4997     51877572 : pass_x86_cse::candidate_vector_p (rtx set, rtx_insn *insn)
    4998              : {
    4999     51877572 :   rtx src = SET_SRC (set);
    5000     51877572 :   rtx dest = SET_DEST (set);
    5001     51877572 :   mode = GET_MODE (dest);
    5002              :   /* Skip non-vector instruction.  */
    5003     51877572 :   if (!VECTOR_MODE_P (mode))
    5004              :     return false;
    5005              : 
    5006              :   /* Skip non-vector load instruction.  */
    5007      3736289 :   if (!REG_P (dest) && !SUBREG_P (dest))
    5008              :     return false;
    5009              : 
    5010      2242771 :   def_insn = insn;
    5011      2242771 :   val = ix86_broadcast_inner (src, mode, &scalar_mode, &kind,
    5012              :                               &def_insn);
    5013      2242771 :   return val ? true : false;
    5014              : }
    5015              : 
    5016              : /* At entry of the nearest common dominator for basic blocks with
    5017              : 
    5018              :    1. Vector CONST0_RTX patterns.
    5019              :    2. Vector CONSTM1_RTX patterns.
    5020              :    3. Vector broadcast patterns.
    5021              :    4. UNSPEC_TLS_GD patterns.
    5022              :    5. UNSPEC_TLS_LD_BASE patterns.
    5023              :    6. UNSPEC_TLSDESC patterns.
    5024              : 
    5025              :    generate a single pattern whose destination is used to replace the
    5026              :    source in all identical patterns.
    5027              : 
    5028              :    NB: We want to generate a pattern, which is executed only once, to
    5029              :    cover the whole function.  The LCM algorithm isn't appropriate here
    5030              :    since it may place a pattern inside the loop.  */
    5031              : 
    5032              : unsigned int
    5033      1003045 : pass_x86_cse::x86_cse (void)
    5034              : {
    5035      1003045 :   timevar_push (TV_MACH_DEP);
    5036              : 
    5037      1003045 :   auto_vec<redundant_pattern *> loads;
    5038      1003045 :   redundant_pattern *load;
    5039      1003045 :   basic_block bb;
    5040      1003045 :   rtx_insn *insn;
    5041      1003045 :   unsigned int i;
    5042      1003045 :   auto_bitmap updated_gnu_tls_insns;
    5043      1003045 :   auto_bitmap updated_gnu2_tls_insns;
    5044      1003045 :   auto_bitmap call_bbs;
    5045              : 
    5046      1003045 :   df_set_flags (DF_DEFER_INSN_RESCAN);
    5047              : 
    5048      1003045 :   bool recursive_call_p = cfun->machine->recursive_function;
    5049              : 
    5050     11277866 :   FOR_EACH_BB_FN (bb, cfun)
    5051              :     {
    5052    138206640 :       FOR_BB_INSNS (bb, insn)
    5053              :         {
    5054    127931819 :           if (!NONDEBUG_INSN_P (insn))
    5055     72276700 :             continue;
    5056              : 
    5057     55655119 :           bool matched = false;
    5058              :           /* Remove redundant patterns if there are more than 2 of
    5059              :              them.  */
    5060     55655119 :           unsigned int threshold = 2;
    5061              : 
    5062     55655119 :           bool call_p = CALL_P (insn);
    5063     55655119 :           rtx set = single_set (insn);
    5064     55655119 :           if (!set && !call_p)
    5065      1120423 :             continue;
    5066              : 
    5067     54534696 :           tlsdesc_val = nullptr;
    5068              : 
    5069     54534696 :           attr_tls64 tls64 = get_attr_tls64 (insn);
    5070              : 
    5071              :           /* NB: TLS calls preserve all registers.  */
    5072     54534696 :           if (call_p && tls64 == TLS64_NONE)
    5073      4596416 :             bitmap_set_bit (call_bbs, BLOCK_FOR_INSN (insn)->index);
    5074              : 
    5075     54534696 :           switch (tls64)
    5076              :             {
    5077         2219 :             case TLS64_GD:
    5078         2219 :             case TLS64_LD_BASE:
    5079              :               /* Verify UNSPEC_TLS_GD and UNSPEC_TLS_LD_BASE.  */
    5080         2219 :               if (candidate_gnu_tls_p (insn, tls64))
    5081              :                 break;
    5082          174 :               continue;
    5083              : 
    5084           56 :             case TLS64_CALL:
    5085           56 :             case TLS64_COMBINE:
    5086              :               /* Verify UNSPEC_TLSDESC.  */
    5087           56 :               if (candidate_gnu2_tls_p (set, tls64))
    5088              :                 break;
    5089            2 :               continue;
    5090              : 
    5091           36 :             case TLS64_LEA:
    5092              :               /* Skip TLS64_LEA.  */
    5093           36 :               continue;
    5094              : 
    5095     54532385 :             case TLS64_NONE:
    5096     54532385 :               if (!set)
    5097      2654813 :                 continue;
    5098              : 
    5099              :               /* Check for vector broadcast.  */
    5100     51877572 :               if (candidate_vector_p (set, insn))
    5101              :                 break;
    5102     51617046 :               continue;
    5103              :             }
    5104              : 
    5105              :           /* Check if there is a matching redundant load.   */
    5106       671969 :           FOR_EACH_VEC_ELT (loads, i, load)
    5107       449826 :             if (load->val
    5108       449826 :                 && load->kind == kind
    5109       295311 :                 && load->mode == scalar_mode
    5110       262869 :                 && (load->bb == bb
    5111       200664 :                     || (kind != X86_CSE_VEC_DUP
    5112       200664 :                         && kind != X86_CSE_CONST_VECTOR)
    5113              :                     /* Non all 0s/1s vector load must be in the same
    5114              :                        basic block if it is in a recursive call.  */
    5115       136536 :                     || !recursive_call_p)
    5116       710484 :                 && rtx_equal_p (load->val, val))
    5117              :               {
    5118              :                 /* Record instruction.  */
    5119       102585 :                 bitmap_set_bit (load->insns, INSN_UID (insn));
    5120              : 
    5121              :                 /* Record the maximum vector size.  */
    5122       102585 :                 if (kind <= X86_CSE_VEC_DUP
    5123       204045 :                     && load->size < GET_MODE_SIZE (mode))
    5124          848 :                   load->size = GET_MODE_SIZE (mode);
    5125              : 
    5126              :                 /* Record the basic block.  */
    5127       102585 :                 bitmap_set_bit (load->bbs, bb->index);
    5128              : 
    5129              :                 /* Increment the count.  */
    5130       102585 :                 load->count++;
    5131              : 
    5132       102585 :                 matched = true;
    5133       102585 :                 break;
    5134              :               }
    5135              : 
    5136       262625 :           if (matched)
    5137       102585 :             continue;
    5138              : 
    5139              :           /* We see this instruction the first time.  Record the
    5140              :              redundant source value, its mode, the destination size,
    5141              :              instruction which defines the redundant source value,
    5142              :              instruction basic block and the instruction kind.  */
    5143       160040 :           load = new redundant_pattern;
    5144              : 
    5145              :           /* Convert CONST_VECTOR load no larger than integer register
    5146              :              to constant integer load even if there is no redundant
    5147              :              CONST_VECTOR load.  */
    5148       160040 :           if (CONST_VECTOR_P (val))
    5149        33201 :             threshold = 1;
    5150              : 
    5151       160040 :           load->val = copy_rtx (val);
    5152       160040 :           if (tlsdesc_val)
    5153           28 :             load->tlsdesc_val = copy_rtx (tlsdesc_val);
    5154              :           else
    5155              :             load->tlsdesc_val = nullptr;
    5156       160040 :           load->mode = scalar_mode;
    5157       160040 :           load->dest_mode = mode;
    5158       160040 :           load->size = GET_MODE_SIZE (mode);
    5159       160040 :           load->def_insn = def_insn;
    5160       160040 :           load->count = 1;
    5161       160040 :           load->threshold = threshold;
    5162       160040 :           load->bb = BLOCK_FOR_INSN (insn);
    5163       160040 :           load->kind = kind;
    5164              : 
    5165       160040 :           bitmap_set_bit (load->insns, INSN_UID (insn));
    5166       160040 :           bitmap_set_bit (load->bbs, bb->index);
    5167              : 
    5168       160040 :           loads.safe_push (load);
    5169              :         }
    5170              :     }
    5171              : 
    5172              :   bool replaced = false;
    5173      1261022 :   FOR_EACH_VEC_ELT (loads, i, load)
    5174       160040 :     if (load->count >= load->threshold)
    5175              :       {
    5176        67759 :         machine_mode mode;
    5177        67759 :         rtx reg, broadcast_reg;
    5178        67759 :         rtx broadcast_source = nullptr;
    5179        67759 :         replaced = true;
    5180        67759 :         switch (load->kind)
    5181              :           {
    5182          323 :           case X86_CSE_TLS_GD:
    5183          323 :           case X86_CSE_TLS_LD_BASE:
    5184          323 :           case X86_CSE_TLSDESC:
    5185          323 :             broadcast_reg = gen_reg_rtx (load->mode);
    5186          323 :             replace_tls_call (broadcast_reg, load->insns,
    5187          323 :                               (load->kind == X86_CSE_TLSDESC
    5188              :                                ? updated_gnu2_tls_insns
    5189              :                                : updated_gnu_tls_insns));
    5190          323 :             load->broadcast_reg = broadcast_reg;
    5191          323 :             break;
    5192              : 
    5193        11526 :           case X86_CSE_VEC_DUP:
    5194        11526 :             if (CONST_INT_P (load->val)
    5195        10547 :                 && (load->val == CONST0_RTX (load->mode)
    5196        10574 :                     || load->size <= UNITS_PER_WORD))
    5197              :               {
    5198              :                 /* Generate CONST_VECTOR load.  */
    5199        33204 :               case X86_CSE_CONST_VECTOR:
    5200        33204 :                 mode = ix86_get_vector_cse_mode (load->size,
    5201              :                                                  load->mode);
    5202              : 
    5203        33204 :                 if (CONST_VECTOR_P (load->val))
    5204              :                   broadcast_source = load->val;
    5205            3 :                 else if (load->val == CONST0_RTX (load->mode))
    5206            0 :                   broadcast_source = CONST0_RTX (mode);
    5207            3 :                 else if (load->val == CONSTM1_RTX (load->mode))
    5208            0 :                   broadcast_source = CONSTM1_RTX (mode);
    5209              :                 else
    5210              :                   {
    5211            3 :                     int nunits = GET_MODE_NUNITS (mode);
    5212            3 :                     rtvec v = rtvec_alloc (nunits);
    5213           12 :                     for (int j = 0; j < nunits ; j++)
    5214            6 :                       RTVEC_ELT (v, j) = load->val;
    5215            3 :                     if (CONST_INT_P (load->val)
    5216            3 :                         && GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
    5217              :                       {
    5218            1 :                         gcc_assert (load->size <= UNITS_PER_WORD);
    5219            1 :                         machine_mode imode = GET_MODE_INNER (mode);
    5220            1 :                         class scalar_mode i_mode
    5221            1 :                           = int_mode_for_mode (imode).require ();
    5222            1 :                         mode = mode_for_vector (i_mode,
    5223            1 :                                                 nunits).require ();
    5224              :                       }
    5225              : 
    5226            3 :                     broadcast_source = gen_rtx_CONST_VECTOR (mode, v);
    5227              :                   }
    5228              : 
    5229              :                 /* NB: Zero CONST_VECTOR load works for MMX and XMM
    5230              :                    registers.  */
    5231        34606 :                 if (load->size <= UNITS_PER_WORD)
    5232              :                   {
    5233              :                     /* Convert CONST_VECTOR load no larger than integer
    5234              :                        register:
    5235              : 
    5236              :                        (set (reg:V2SI 106)
    5237              :                             (const_vector:V2SI [(const_int 1 [1]) repeated x2]))
    5238              : 
    5239              :                        to constant integer load:
    5240              : 
    5241              :                        (set (subreg:DI (reg:V2SI 106 [ _20 ]) 0)
    5242              :                             (const_int 4294967297 [0x100000001]))
    5243              :                        */
    5244        33204 :                     machine_mode int_mode
    5245        33204 :                       = int_mode_for_mode (mode).require ();
    5246        33204 :                     load->dest_mode = int_mode;
    5247        33204 :                     broadcast_source = simplify_subreg (int_mode,
    5248              :                                                         broadcast_source,
    5249              :                                                         mode, 0);
    5250        33204 :                     gcc_assert (broadcast_source != nullptr);
    5251              : 
    5252        33204 :                     bool keep_const_int_load = false;
    5253        33204 :                     if (!bitmap_empty_p (call_bbs))
    5254              :                       {
    5255        29537 :                         bitmap_iterator bi;
    5256        29537 :                         unsigned int id;
    5257        39455 :                         EXECUTE_IF_SET_IN_BITMAP (load->bbs, 0, id, bi)
    5258        31070 :                           if (bitmap_bit_p (call_bbs, id))
    5259              :                             {
    5260              :                               /* NB: Constant integer load is faster
    5261              :                                  than save and restore an integer
    5262              :                                  register when crossing a function call.
    5263              :                                */
    5264              :                               keep_const_int_load = true;
    5265              :                               break;
    5266              :                             }
    5267              :                       }
    5268              : 
    5269        29537 :                     if (keep_const_int_load)
    5270              :                       {
    5271              :                         /* Keep constant integer load.  */
    5272        21152 :                         replace_vector_const (mode, broadcast_source,
    5273        21152 :                                               load->insns, int_mode);
    5274        21152 :                         load->broadcast_source = nullptr;
    5275        21152 :                         load->broadcast_reg = nullptr;
    5276              :                       }
    5277              :                     else
    5278              :                       {
    5279        12052 :                         broadcast_reg = gen_reg_rtx (mode);
    5280        12052 :                         reg = gen_reg_rtx (load->mode);
    5281        12052 :                         replace_vector_const (mode, broadcast_reg,
    5282        12052 :                                               load->insns, load->mode);
    5283        12052 :                         load->broadcast_source = broadcast_source;
    5284        12052 :                         load->broadcast_reg = broadcast_reg;
    5285              :                       }
    5286              :                     break;
    5287              :                   }
    5288              :               }
    5289              :             /* FALLTHRU */
    5290              : 
    5291        34232 :           case X86_CSE_CONST0_VECTOR:
    5292        34232 :           case X86_CSE_CONSTM1_VECTOR:
    5293        34232 :             mode = ix86_get_vector_cse_mode (load->size, load->mode);
    5294        34232 :             broadcast_reg = gen_reg_rtx (mode);
    5295        34232 :             if (load->def_insn)
    5296              :               {
    5297              :                 /* Replace redundant vector loads with a single vector
    5298              :                    load in the same basic block.  */
    5299          841 :                 reg = load->val;
    5300          841 :                 if (load->mode != GET_MODE (reg))
    5301            0 :                   reg = gen_rtx_SUBREG (load->mode, reg, 0);
    5302          841 :                 broadcast_source = gen_rtx_VEC_DUPLICATE (mode, reg);
    5303              :               }
    5304              :             else
    5305              :               /* This is a constant integer/double vector.  If the
    5306              :                  inner scalar is 0 or -1, set vector to CONST0_RTX
    5307              :                  or CONSTM1_RTX directly.  */
    5308        33391 :               switch (load->kind)
    5309              :                 {
    5310        20986 :                 case X86_CSE_CONST0_VECTOR:
    5311        20986 :                   broadcast_source = CONST0_RTX (mode);
    5312        20986 :                   break;
    5313         1720 :                 case X86_CSE_CONSTM1_VECTOR:
    5314         1720 :                   broadcast_source = CONSTM1_RTX (mode);
    5315         1720 :                   break;
    5316        10685 :                 case X86_CSE_CONST_VECTOR:
    5317        10685 :                 case X86_CSE_VEC_DUP:
    5318        10685 :                   if (!broadcast_source)
    5319              :                     {
    5320        10685 :                       reg = gen_reg_rtx (load->mode);
    5321        10685 :                       broadcast_source = gen_rtx_VEC_DUPLICATE (mode,
    5322              :                                                                 reg);
    5323              :                     }
    5324              :                   break;
    5325            0 :                 default:
    5326            0 :                   gcc_unreachable ();
    5327              :                 }
    5328        34232 :             replace_vector_const (mode, broadcast_reg, load->insns,
    5329              :                                   load->mode);
    5330        34232 :             load->broadcast_source = broadcast_source;
    5331        34232 :             load->broadcast_reg = broadcast_reg;
    5332        34232 :             break;
    5333              :           }
    5334              :       }
    5335              : 
    5336      1003045 :   if (replaced)
    5337              :     {
    5338        44009 :       auto_vec<rtx_insn *> control_flow_insns;
    5339              : 
    5340              :       /* (Re-)discover loops so that bb->loop_father can be used in the
    5341              :          analysis below.  */
    5342        44009 :       calculate_dominance_info (CDI_DOMINATORS);
    5343        44009 :       loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
    5344              : 
    5345       221070 :       FOR_EACH_VEC_ELT (loads, i, load)
    5346        89043 :         if (load->count >= load->threshold)
    5347              :           {
    5348        67759 :             rtx set;
    5349        67759 :             if (load->def_insn)
    5350          857 :               switch (load->kind)
    5351              :                 {
    5352           16 :                 case X86_CSE_TLSDESC:
    5353           16 :                   ix86_place_single_tls_call (load->broadcast_reg,
    5354              :                                               load->tlsdesc_val,
    5355              :                                               load->kind,
    5356           16 :                                               load->bbs,
    5357              :                                               updated_gnu_tls_insns,
    5358              :                                               updated_gnu2_tls_insns,
    5359           16 :                                               PATTERN (load->def_insn));
    5360           16 :                   break;
    5361          841 :                 case X86_CSE_VEC_DUP:
    5362              :                   /* Insert a broadcast after the original scalar
    5363              :                      definition.  */
    5364          841 :                   set = gen_rtx_SET (load->broadcast_reg,
    5365              :                                      load->broadcast_source);
    5366          841 :                   insn = emit_insn_after (set, load->def_insn);
    5367              : 
    5368          841 :                   if (cfun->can_throw_non_call_exceptions)
    5369              :                     {
    5370              :                       /* Handle REG_EH_REGION note in DEF_INSN.  */
    5371            4 :                       rtx note = find_reg_note (load->def_insn,
    5372              :                                                 REG_EH_REGION, nullptr);
    5373            4 :                       if (note)
    5374              :                         {
    5375            1 :                           control_flow_insns.safe_push (load->def_insn);
    5376            1 :                           add_reg_note (insn, REG_EH_REGION,
    5377              :                                         XEXP (note, 0));
    5378              :                         }
    5379              :                     }
    5380              : 
    5381          841 :                   if (dump_file)
    5382              :                     {
    5383            0 :                       fprintf (dump_file, "\nAdd:\n\n");
    5384            0 :                       print_rtl_single (dump_file, insn);
    5385            0 :                       fprintf (dump_file, "\nafter:\n\n");
    5386            0 :                       print_rtl_single (dump_file, load->def_insn);
    5387            0 :                       fprintf (dump_file, "\n");
    5388              :                     }
    5389              :                   break;
    5390            0 :                 default:
    5391            0 :                   gcc_unreachable ();
    5392              :                 }
    5393              :             else
    5394        66902 :               switch (load->kind)
    5395              :                 {
    5396          307 :                 case X86_CSE_TLS_GD:
    5397          307 :                 case X86_CSE_TLS_LD_BASE:
    5398          307 :                 case X86_CSE_TLSDESC:
    5399          307 :                   ix86_place_single_tls_call (load->broadcast_reg,
    5400              :                                               (load->kind == X86_CSE_TLSDESC
    5401              :                                                ? load->tlsdesc_val
    5402              :                                                : load->val),
    5403              :                                               load->kind,
    5404          307 :                                               load->bbs,
    5405              :                                               updated_gnu_tls_insns,
    5406              :                                               updated_gnu2_tls_insns);
    5407          307 :                   break;
    5408        43889 :                 case X86_CSE_CONST_VECTOR:
    5409        43889 :                 case X86_CSE_VEC_DUP:
    5410              :                   /* Keep redundant constant integer load.  */
    5411        43889 :                   if (!load->broadcast_reg)
    5412              :                     break;
    5413              :                   /* FALLTHRU */
    5414        45443 :                 case X86_CSE_CONST0_VECTOR:
    5415        45443 :                 case X86_CSE_CONSTM1_VECTOR:
    5416        45443 :                   ix86_place_single_vector_set (load->broadcast_reg,
    5417              :                                                 load->broadcast_source,
    5418              :                                                 load->bbs,
    5419              :                                                 load);
    5420        45443 :                   break;
    5421              :                 }
    5422              :           }
    5423              : 
    5424        44009 :       loop_optimizer_finalize ();
    5425              : 
    5426        44010 :       if (!control_flow_insns.is_empty ())
    5427              :         {
    5428            1 :           free_dominance_info (CDI_DOMINATORS);
    5429              : 
    5430            3 :           FOR_EACH_VEC_ELT (control_flow_insns, i, insn)
    5431            1 :             if (control_flow_insn_p (insn))
    5432              :               {
    5433              :                 /* Split the block after insn.  There will be a fallthru
    5434              :                    edge, which is OK so we keep it.  We have to create
    5435              :                    the exception edges ourselves.  */
    5436            1 :                 bb = BLOCK_FOR_INSN (insn);
    5437            1 :                 split_block (bb, insn);
    5438            1 :                 rtl_make_eh_edge (NULL, bb, BB_END (bb));
    5439              :               }
    5440              :         }
    5441              : 
    5442        44009 :       df_process_deferred_rescans ();
    5443        44009 :     }
    5444              : 
    5445      1261022 :   FOR_EACH_VEC_ELT (loads, i, load)
    5446       320080 :     delete load;
    5447              : 
    5448      1003045 :   df_clear_flags (DF_DEFER_INSN_RESCAN);
    5449              : 
    5450      1003045 :   timevar_pop (TV_MACH_DEP);
    5451      1003045 :   return 0;
    5452      1003045 : }
    5453              : 
    5454              : } // anon namespace
    5455              : 
    5456              : rtl_opt_pass *
    5457       302985 : make_pass_x86_cse (gcc::context *ctxt)
    5458              : {
    5459       302985 :   return new pass_x86_cse (ctxt);
    5460              : }
    5461              : 
    5462              : /* Convert legacy instructions that clobbers EFLAGS to APX_NF
    5463              :    instructions when there are no flag set between a flag
    5464              :    producer and user.  */
    5465              : 
    5466              : static unsigned int
    5467          414 : ix86_apx_nf_convert (void)
    5468              : {
    5469          414 :   timevar_push (TV_MACH_DEP);
    5470              : 
    5471          414 :   basic_block bb;
    5472          414 :   rtx_insn *insn;
    5473          414 :   hash_map <rtx_insn *, rtx> converting_map;
    5474          414 :   auto_vec <rtx_insn *> current_convert_list;
    5475              : 
    5476          414 :   bool converting_seq = false;
    5477          414 :   rtx cc = gen_rtx_REG (CCmode, FLAGS_REG);
    5478              : 
    5479          965 :   FOR_EACH_BB_FN (bb, cfun)
    5480              :     {
    5481              :       /* Reset conversion for each bb.  */
    5482          551 :       converting_seq = false;
    5483         6225 :       FOR_BB_INSNS (bb, insn)
    5484              :         {
    5485         5674 :           if (!NONDEBUG_INSN_P (insn))
    5486         4968 :             continue;
    5487              : 
    5488         4513 :           if (recog_memoized (insn) < 0)
    5489          373 :             continue;
    5490              : 
    5491              :           /* Convert candidate insns after cstore, which should
    5492              :              satisfy the two conditions:
    5493              :              1. Is not flag user or producer, only clobbers
    5494              :              FLAGS_REG.
    5495              :              2. Have corresponding nf pattern.  */
    5496              : 
    5497         4140 :           rtx pat = PATTERN (insn);
    5498              : 
    5499              :           /* Starting conversion at first cstorecc.  */
    5500         4140 :           rtx set = NULL_RTX;
    5501         4140 :           if (!converting_seq
    5502         3479 :               && (set = single_set (insn))
    5503         3389 :               && ix86_comparison_operator (SET_SRC (set), VOIDmode)
    5504          161 :               && reg_overlap_mentioned_p (cc, SET_SRC (set))
    5505         4298 :               && !reg_overlap_mentioned_p (cc, SET_DEST (set)))
    5506              :             {
    5507          158 :               converting_seq = true;
    5508          158 :               current_convert_list.truncate (0);
    5509              :             }
    5510              :           /* Terminate at the next explicit flag set.  */
    5511         3982 :           else if (reg_set_p (cc, pat)
    5512         3982 :                    && GET_CODE (set_of (cc, pat)) != CLOBBER)
    5513              :             converting_seq = false;
    5514              : 
    5515         3820 :           if (!converting_seq)
    5516         3434 :             continue;
    5517              : 
    5518          706 :           if (get_attr_has_nf (insn)
    5519          706 :               && GET_CODE (pat) == PARALLEL)
    5520              :             {
    5521              :               /* Record the insn to candidate map.  */
    5522           84 :               current_convert_list.safe_push (insn);
    5523           84 :               converting_map.put (insn, pat);
    5524              :             }
    5525              :           /* If the insn clobbers flags but has no nf_attr,
    5526              :              revoke all previous candidates.  */
    5527          622 :           else if (!get_attr_has_nf (insn)
    5528          621 :                    && reg_set_p (cc, pat)
    5529          625 :                    && GET_CODE (set_of (cc, pat)) == CLOBBER)
    5530              :             {
    5531            3 :               for (auto item : current_convert_list)
    5532            0 :                 converting_map.remove (item);
    5533            3 :               converting_seq = false;
    5534              :             }
    5535              :         }
    5536              :     }
    5537              : 
    5538          414 :   if (!converting_map.is_empty ())
    5539              :     {
    5540          108 :       for (auto iter = converting_map.begin ();
    5541          132 :            iter != converting_map.end (); ++iter)
    5542              :         {
    5543           84 :           rtx_insn *replace = (*iter).first;
    5544           84 :           rtx pat = (*iter).second;
    5545           84 :           int i, n = 0, len = XVECLEN (pat, 0);
    5546           84 :           rtx *new_elems = XALLOCAVEC (rtx, len);
    5547           84 :           rtx new_pat;
    5548          252 :           for (i = 0; i < len; i++)
    5549              :             {
    5550          168 :               rtx temp = XVECEXP (pat, 0, i);
    5551          252 :               if (! (GET_CODE (temp) == CLOBBER
    5552           84 :                      && reg_overlap_mentioned_p (cc,
    5553           84 :                                                  XEXP (temp, 0))))
    5554              :                 {
    5555           84 :                   new_elems[n] = temp;
    5556           84 :                   n++;
    5557              :                 }
    5558              :             }
    5559              : 
    5560           84 :           if (n == 1)
    5561           84 :             new_pat = new_elems[0];
    5562              :           else
    5563            0 :             new_pat =
    5564            0 :               gen_rtx_PARALLEL (VOIDmode,
    5565              :                                 gen_rtvec_v (n,
    5566              :                                              new_elems));
    5567              : 
    5568           84 :           PATTERN (replace) = new_pat;
    5569           84 :           INSN_CODE (replace) = -1;
    5570           84 :           recog_memoized (replace);
    5571           84 :           df_insn_rescan (replace);
    5572              :         }
    5573              :     }
    5574              : 
    5575          414 :   timevar_pop (TV_MACH_DEP);
    5576          414 :   return 0;
    5577          414 : }
    5578              : 
    5579              : 
    5580              : namespace {
    5581              : 
    5582              : const pass_data pass_data_apx_nf_convert =
    5583              : {
    5584              :   RTL_PASS, /* type */
    5585              :   "apx_nfcvt", /* name */
    5586              :   OPTGROUP_NONE, /* optinfo_flags */
    5587              :   TV_MACH_DEP, /* tv_id */
    5588              :   0, /* properties_required */
    5589              :   0, /* properties_provided */
    5590              :   0, /* properties_destroyed */
    5591              :   0, /* todo_flags_start */
    5592              :   0, /* todo_flags_finish */
    5593              : };
    5594              : 
    5595              : class pass_apx_nf_convert : public rtl_opt_pass
    5596              : {
    5597              : public:
    5598       302985 :   pass_apx_nf_convert (gcc::context *ctxt)
    5599       605970 :     : rtl_opt_pass (pass_data_apx_nf_convert, ctxt)
    5600              :   {}
    5601              : 
    5602              :   /* opt_pass methods: */
    5603      1531131 :   bool gate (function *) final override
    5604              :     {
    5605      1531131 :       return (TARGET_APX_NF
    5606          510 :               && optimize
    5607      1531629 :               && optimize_function_for_speed_p (cfun));
    5608              :     }
    5609              : 
    5610          414 :   unsigned int execute (function *) final override
    5611              :     {
    5612          414 :       return ix86_apx_nf_convert ();
    5613              :     }
    5614              : }; // class pass_apx_nf_convert
    5615              : 
    5616              : } // anon namespace
    5617              : 
    5618              : rtl_opt_pass *
    5619       302985 : make_pass_apx_nf_convert (gcc::context *ctxt)
    5620              : {
    5621       302985 :   return new pass_apx_nf_convert (ctxt);
    5622              : }
    5623              : 
    5624              : /* When a hot loop can be fit into one cacheline,
    5625              :    force align the loop without considering the max skip.  */
    5626              : static void ATTRIBUTE_UNUSED
    5627      1002559 : ix86_align_loops ()
    5628              : {
    5629      1002559 :   basic_block bb;
    5630              : 
    5631              :   /* Don't do this when we don't know cache line size.  */
    5632      1002559 :   if (ix86_cost->prefetch_block == 0)
    5633            9 :     return;
    5634              : 
    5635      1002550 :   loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
    5636      1002550 :   profile_count count_threshold = cfun->cfg->count_max / param_align_threshold;
    5637     11724882 :   FOR_EACH_BB_FN (bb, cfun)
    5638              :     {
    5639     10722332 :       rtx_insn *label = BB_HEAD (bb);
    5640     10722332 :       bool has_fallthru = 0;
    5641     10722332 :       edge e;
    5642     10722332 :       edge_iterator ei;
    5643              : 
    5644     10722332 :       if (!LABEL_P (label))
    5645      5461699 :         continue;
    5646              : 
    5647      5265548 :       profile_count fallthru_count = profile_count::zero ();
    5648      5265548 :       profile_count branch_count = profile_count::zero ();
    5649              : 
    5650     15340707 :       FOR_EACH_EDGE (e, ei, bb->preds)
    5651              :         {
    5652     10075159 :           if (e->flags & EDGE_FALLTHRU)
    5653      2545673 :             has_fallthru = 1, fallthru_count += e->count ();
    5654              :           else
    5655      7529486 :             branch_count += e->count ();
    5656              :         }
    5657              : 
    5658      5265548 :       if (!fallthru_count.initialized_p () || !branch_count.initialized_p ())
    5659         4915 :         continue;
    5660              : 
    5661      5260633 :       if (bb->loop_father
    5662      5260633 :           && bb->loop_father->latch != EXIT_BLOCK_PTR_FOR_FN (cfun)
    5663      6651715 :           && (has_fallthru
    5664      1391082 :               ? (!(single_succ_p (bb)
    5665       156808 :                    && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun))
    5666       959117 :                  && optimize_bb_for_speed_p (bb)
    5667       876664 :                  && branch_count + fallthru_count > count_threshold
    5668       749773 :                  && (branch_count > fallthru_count * param_align_loop_iterations))
    5669              :               /* In case there'no fallthru for the loop.
    5670              :                  Nops inserted won't be executed.  */
    5671       431965 :               : (branch_count > count_threshold
    5672       143282 :                  || (bb->count > bb->prev_bb->count * 10
    5673        12763 :                      && (bb->prev_bb->count
    5674      5273396 :                          <= ENTRY_BLOCK_PTR_FOR_FN (cfun)->count / 2)))))
    5675              :         {
    5676       570015 :           rtx_insn* insn, *end_insn;
    5677       570015 :           HOST_WIDE_INT size = 0;
    5678       570015 :           bool padding_p = true;
    5679       570015 :           basic_block tbb = bb;
    5680       570015 :           unsigned cond_branch_num = 0;
    5681       570015 :           bool detect_tight_loop_p = false;
    5682              : 
    5683       888796 :           for (unsigned int i = 0; i != bb->loop_father->num_nodes;
    5684       318781 :                i++, tbb = tbb->next_bb)
    5685              :             {
    5686              :               /* Only handle continuous cfg layout. */
    5687       888796 :               if (bb->loop_father != tbb->loop_father)
    5688              :                 {
    5689              :                   padding_p = false;
    5690              :                   break;
    5691              :                 }
    5692              : 
    5693     10844075 :               FOR_BB_INSNS (tbb, insn)
    5694              :                 {
    5695     10159504 :                   if (!NONDEBUG_INSN_P (insn))
    5696      5872901 :                     continue;
    5697      4286603 :                   size += ix86_min_insn_size (insn);
    5698              : 
    5699              :                   /* We don't know size of inline asm.
    5700              :                      Don't align loop for call.  */
    5701      4286603 :                   if (asm_noperands (PATTERN (insn)) >= 0
    5702      4286603 :                       || CALL_P (insn))
    5703              :                     {
    5704              :                       size = -1;
    5705              :                       break;
    5706              :                     }
    5707              :                 }
    5708              : 
    5709       845816 :               if (size == -1 || size > ix86_cost->prefetch_block)
    5710              :                 {
    5711              :                   padding_p = false;
    5712              :                   break;
    5713              :                 }
    5714              : 
    5715      1500064 :               FOR_EACH_EDGE (e, ei, tbb->succs)
    5716              :                 {
    5717              :                   /* It could be part of the loop.  */
    5718      1035216 :                   if (e->dest == bb)
    5719              :                     {
    5720              :                       detect_tight_loop_p = true;
    5721              :                       break;
    5722              :                     }
    5723              :                 }
    5724              : 
    5725       657513 :               if (detect_tight_loop_p)
    5726              :                 break;
    5727              : 
    5728       464848 :               end_insn = BB_END (tbb);
    5729       464848 :               if (JUMP_P (end_insn))
    5730              :                 {
    5731              :                   /* For decoded icache:
    5732              :                      1. Up to two branches are allowed per Way.
    5733              :                      2. A non-conditional branch is the last micro-op in a Way.
    5734              :                   */
    5735       377267 :                   if (onlyjump_p (end_insn)
    5736       377267 :                       && (any_uncondjump_p (end_insn)
    5737       313050 :                           || single_succ_p (tbb)))
    5738              :                     {
    5739              :                       padding_p = false;
    5740              :                       break;
    5741              :                     }
    5742       313050 :                   else if (++cond_branch_num >= 2)
    5743              :                     {
    5744              :                       padding_p = false;
    5745              :                       break;
    5746              :                     }
    5747              :                 }
    5748              : 
    5749              :             }
    5750              : 
    5751       570015 :           if (padding_p && detect_tight_loop_p)
    5752              :             {
    5753       385330 :               emit_insn_before (gen_max_skip_align (GEN_INT (ceil_log2 (size)),
    5754              :                                                     const0_rtx), label);
    5755              :               /* End of function.  */
    5756       192665 :               if (!tbb || tbb == EXIT_BLOCK_PTR_FOR_FN (cfun))
    5757              :                 break;
    5758              :               /* Skip bb which already fits into one cacheline.  */
    5759              :               bb = tbb;
    5760              :             }
    5761              :         }
    5762              :     }
    5763              : 
    5764      1002550 :   loop_optimizer_finalize ();
    5765      1002550 :   free_dominance_info (CDI_DOMINATORS);
    5766              : }
    5767              : 
    5768              : namespace {
    5769              : 
    5770              : const pass_data pass_data_align_tight_loops =
    5771              : {
    5772              :   RTL_PASS, /* type */
    5773              :   "align_tight_loops", /* name */
    5774              :   OPTGROUP_NONE, /* optinfo_flags */
    5775              :   TV_MACH_DEP, /* tv_id */
    5776              :   0, /* properties_required */
    5777              :   0, /* properties_provided */
    5778              :   0, /* properties_destroyed */
    5779              :   0, /* todo_flags_start */
    5780              :   0, /* todo_flags_finish */
    5781              : };
    5782              : 
    5783              : class pass_align_tight_loops : public rtl_opt_pass
    5784              : {
    5785              : public:
    5786       302985 :   pass_align_tight_loops (gcc::context *ctxt)
    5787       605970 :     : rtl_opt_pass (pass_data_align_tight_loops, ctxt)
    5788              :   {}
    5789              : 
    5790              :   /* opt_pass methods: */
    5791      1531131 :   bool gate (function *) final override
    5792              :     {
    5793      1531131 :       return TARGET_ALIGN_TIGHT_LOOPS
    5794      1530637 :              && optimize
    5795      2600869 :              && optimize_function_for_speed_p (cfun);
    5796              :     }
    5797              : 
    5798      1002559 :   unsigned int execute (function *) final override
    5799              :     {
    5800      1002559 :       timevar_push (TV_MACH_DEP);
    5801              : #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
    5802      1002559 :       ix86_align_loops ();
    5803              : #endif
    5804      1002559 :       timevar_pop (TV_MACH_DEP);
    5805      1002559 :       return 0;
    5806              :     }
    5807              : }; // class pass_align_tight_loops
    5808              : 
    5809              : } // anon namespace
    5810              : 
    5811              : rtl_opt_pass *
    5812       302985 : make_pass_align_tight_loops (gcc::context *ctxt)
    5813              : {
    5814       302985 :   return new pass_align_tight_loops (ctxt);
    5815              : }
    5816              : 
    5817              : /* This compares the priority of target features in function DECL1
    5818              :    and DECL2.  It returns positive value if DECL1 is higher priority,
    5819              :    negative value if DECL2 is higher priority and 0 if they are the
    5820              :    same.  */
    5821              : 
    5822              : int
    5823         5840 : ix86_compare_version_priority (tree decl1, tree decl2)
    5824              : {
    5825         5840 :   unsigned int priority1 = get_builtin_code_for_version (decl1, NULL);
    5826         5840 :   unsigned int priority2 = get_builtin_code_for_version (decl2, NULL);
    5827              : 
    5828         5840 :   return (int)priority1 - (int)priority2;
    5829              : }
    5830              : 
    5831              : /* V1 and V2 point to function versions with different priorities
    5832              :    based on the target ISA.  This function compares their priorities.  */
    5833              : 
    5834              : static int
    5835         6882 : feature_compare (const void *v1, const void *v2)
    5836              : {
    5837         6882 :   typedef struct _function_version_info
    5838              :     {
    5839              :       tree version_decl;
    5840              :       tree predicate_chain;
    5841              :       unsigned int dispatch_priority;
    5842              :     } function_version_info;
    5843              : 
    5844         6882 :   const function_version_info c1 = *(const function_version_info *)v1;
    5845         6882 :   const function_version_info c2 = *(const function_version_info *)v2;
    5846         6882 :   return (c2.dispatch_priority - c1.dispatch_priority);
    5847              : }
    5848              : 
    5849              : /* This adds a condition to the basic_block NEW_BB in function FUNCTION_DECL
    5850              :    to return a pointer to VERSION_DECL if the outcome of the expression
    5851              :    formed by PREDICATE_CHAIN is true.  This function will be called during
    5852              :    version dispatch to decide which function version to execute.  It returns
    5853              :    the basic block at the end, to which more conditions can be added.  */
    5854              : 
    5855              : static basic_block
    5856          868 : add_condition_to_bb (tree function_decl, tree version_decl,
    5857              :                      tree predicate_chain, basic_block new_bb)
    5858              : {
    5859          868 :   gimple *return_stmt;
    5860          868 :   tree convert_expr, result_var;
    5861          868 :   gimple *convert_stmt;
    5862          868 :   gimple *call_cond_stmt;
    5863          868 :   gimple *if_else_stmt;
    5864              : 
    5865          868 :   basic_block bb1, bb2, bb3;
    5866          868 :   edge e12, e23;
    5867              : 
    5868          868 :   tree cond_var, and_expr_var = NULL_TREE;
    5869          868 :   gimple_seq gseq;
    5870              : 
    5871          868 :   tree predicate_decl, predicate_arg;
    5872              : 
    5873          868 :   push_cfun (DECL_STRUCT_FUNCTION (function_decl));
    5874              : 
    5875          868 :   gcc_assert (new_bb != NULL);
    5876          868 :   gseq = bb_seq (new_bb);
    5877              : 
    5878              : 
    5879          868 :   convert_expr = build1 (CONVERT_EXPR, ptr_type_node,
    5880              :                          build_fold_addr_expr (version_decl));
    5881          868 :   result_var = create_tmp_var (ptr_type_node);
    5882          868 :   convert_stmt = gimple_build_assign (result_var, convert_expr);
    5883          868 :   return_stmt = gimple_build_return (result_var);
    5884              : 
    5885          868 :   if (predicate_chain == NULL_TREE)
    5886              :     {
    5887          214 :       gimple_seq_add_stmt (&gseq, convert_stmt);
    5888          214 :       gimple_seq_add_stmt (&gseq, return_stmt);
    5889          214 :       set_bb_seq (new_bb, gseq);
    5890          214 :       gimple_set_bb (convert_stmt, new_bb);
    5891          214 :       gimple_set_bb (return_stmt, new_bb);
    5892          214 :       pop_cfun ();
    5893          214 :       return new_bb;
    5894              :     }
    5895              : 
    5896         1347 :   while (predicate_chain != NULL)
    5897              :     {
    5898          693 :       cond_var = create_tmp_var (integer_type_node);
    5899          693 :       predicate_decl = TREE_PURPOSE (predicate_chain);
    5900          693 :       predicate_arg = TREE_VALUE (predicate_chain);
    5901          693 :       call_cond_stmt = gimple_build_call (predicate_decl, 1, predicate_arg);
    5902          693 :       gimple_call_set_lhs (call_cond_stmt, cond_var);
    5903              : 
    5904          693 :       gimple_set_block (call_cond_stmt, DECL_INITIAL (function_decl));
    5905          693 :       gimple_set_bb (call_cond_stmt, new_bb);
    5906          693 :       gimple_seq_add_stmt (&gseq, call_cond_stmt);
    5907              : 
    5908          693 :       predicate_chain = TREE_CHAIN (predicate_chain);
    5909              : 
    5910          693 :       if (and_expr_var == NULL)
    5911              :         and_expr_var = cond_var;
    5912              :       else
    5913              :         {
    5914           39 :           gimple *assign_stmt;
    5915              :           /* Use MIN_EXPR to check if any integer is zero?.
    5916              :              and_expr_var = min_expr <cond_var, and_expr_var>  */
    5917           39 :           assign_stmt = gimple_build_assign (and_expr_var,
    5918              :                           build2 (MIN_EXPR, integer_type_node,
    5919              :                                   cond_var, and_expr_var));
    5920              : 
    5921           39 :           gimple_set_block (assign_stmt, DECL_INITIAL (function_decl));
    5922           39 :           gimple_set_bb (assign_stmt, new_bb);
    5923           39 :           gimple_seq_add_stmt (&gseq, assign_stmt);
    5924              :         }
    5925              :     }
    5926              : 
    5927          654 :   if_else_stmt = gimple_build_cond (GT_EXPR, and_expr_var,
    5928              :                                     integer_zero_node,
    5929              :                                     NULL_TREE, NULL_TREE);
    5930          654 :   gimple_set_block (if_else_stmt, DECL_INITIAL (function_decl));
    5931          654 :   gimple_set_bb (if_else_stmt, new_bb);
    5932          654 :   gimple_seq_add_stmt (&gseq, if_else_stmt);
    5933              : 
    5934          654 :   gimple_seq_add_stmt (&gseq, convert_stmt);
    5935          654 :   gimple_seq_add_stmt (&gseq, return_stmt);
    5936          654 :   set_bb_seq (new_bb, gseq);
    5937              : 
    5938          654 :   bb1 = new_bb;
    5939          654 :   e12 = split_block (bb1, if_else_stmt);
    5940          654 :   bb2 = e12->dest;
    5941          654 :   e12->flags &= ~EDGE_FALLTHRU;
    5942          654 :   e12->flags |= EDGE_TRUE_VALUE;
    5943              : 
    5944          654 :   e23 = split_block (bb2, return_stmt);
    5945              : 
    5946          654 :   gimple_set_bb (convert_stmt, bb2);
    5947          654 :   gimple_set_bb (return_stmt, bb2);
    5948              : 
    5949          654 :   bb3 = e23->dest;
    5950          654 :   make_edge (bb1, bb3, EDGE_FALSE_VALUE);
    5951              : 
    5952          654 :   remove_edge (e23);
    5953          654 :   make_edge (bb2, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
    5954              : 
    5955          654 :   pop_cfun ();
    5956              : 
    5957          654 :   return bb3;
    5958              : }
    5959              : 
    5960              : /* This function generates the dispatch function for
    5961              :    multi-versioned functions.  DISPATCH_DECL is the function which will
    5962              :    contain the dispatch logic.  FNDECLS are the function choices for
    5963              :    dispatch, and is a tree chain.  EMPTY_BB is the basic block pointer
    5964              :    in DISPATCH_DECL in which the dispatch code is generated.  */
    5965              : 
    5966              : static int
    5967          214 : dispatch_function_versions (tree dispatch_decl,
    5968              :                             void *fndecls_p,
    5969              :                             basic_block *empty_bb)
    5970              : {
    5971          214 :   tree default_decl;
    5972          214 :   gimple *ifunc_cpu_init_stmt;
    5973          214 :   gimple_seq gseq;
    5974          214 :   int ix;
    5975          214 :   tree ele;
    5976          214 :   vec<tree> *fndecls;
    5977          214 :   unsigned int num_versions = 0;
    5978          214 :   unsigned int actual_versions = 0;
    5979          214 :   unsigned int i;
    5980              : 
    5981          214 :   struct _function_version_info
    5982              :     {
    5983              :       tree version_decl;
    5984              :       tree predicate_chain;
    5985              :       unsigned int dispatch_priority;
    5986              :     }*function_version_info;
    5987              : 
    5988          214 :   gcc_assert (dispatch_decl != NULL
    5989              :               && fndecls_p != NULL
    5990              :               && empty_bb != NULL);
    5991              : 
    5992              :   /*fndecls_p is actually a vector.  */
    5993          214 :   fndecls = static_cast<vec<tree> *> (fndecls_p);
    5994              : 
    5995              :   /* At least one more version other than the default.  */
    5996          214 :   num_versions = fndecls->length ();
    5997          214 :   gcc_assert (num_versions >= 2);
    5998              : 
    5999          214 :   function_version_info = (struct _function_version_info *)
    6000          214 :     XNEWVEC (struct _function_version_info, (num_versions - 1));
    6001              : 
    6002              :   /* The first version in the vector is the default decl.  */
    6003          214 :   default_decl = (*fndecls)[0];
    6004              : 
    6005          214 :   push_cfun (DECL_STRUCT_FUNCTION (dispatch_decl));
    6006              : 
    6007          214 :   gseq = bb_seq (*empty_bb);
    6008              :   /* Function version dispatch is via IFUNC.  IFUNC resolvers fire before
    6009              :      constructors, so explicitly call __builtin_cpu_init here.  */
    6010          214 :   ifunc_cpu_init_stmt
    6011          214 :     = gimple_build_call_vec (get_ix86_builtin (IX86_BUILTIN_CPU_INIT), vNULL);
    6012          214 :   gimple_seq_add_stmt (&gseq, ifunc_cpu_init_stmt);
    6013          214 :   gimple_set_bb (ifunc_cpu_init_stmt, *empty_bb);
    6014          214 :   set_bb_seq (*empty_bb, gseq);
    6015              : 
    6016          214 :   pop_cfun ();
    6017              : 
    6018              : 
    6019         1239 :   for (ix = 1; fndecls->iterate (ix, &ele); ++ix)
    6020              :     {
    6021          811 :       tree version_decl = ele;
    6022          811 :       tree predicate_chain = NULL_TREE;
    6023          811 :       unsigned int priority;
    6024              :       /* Get attribute string, parse it and find the right predicate decl.
    6025              :          The predicate function could be a lengthy combination of many
    6026              :          features, like arch-type and various isa-variants.  */
    6027          811 :       priority = get_builtin_code_for_version (version_decl,
    6028              :                                                &predicate_chain);
    6029              : 
    6030          811 :       if (predicate_chain == NULL_TREE)
    6031          157 :         continue;
    6032              : 
    6033          654 :       function_version_info [actual_versions].version_decl = version_decl;
    6034          654 :       function_version_info [actual_versions].predicate_chain
    6035          654 :          = predicate_chain;
    6036          654 :       function_version_info [actual_versions].dispatch_priority = priority;
    6037          654 :       actual_versions++;
    6038              :     }
    6039              : 
    6040              :   /* Sort the versions according to descending order of dispatch priority.  The
    6041              :      priority is based on the ISA.  This is not a perfect solution.  There
    6042              :      could still be ambiguity.  If more than one function version is suitable
    6043              :      to execute,  which one should be dispatched?  In future, allow the user
    6044              :      to specify a dispatch  priority next to the version.  */
    6045          214 :   qsort (function_version_info, actual_versions,
    6046              :          sizeof (struct _function_version_info), feature_compare);
    6047              : 
    6048         1082 :   for  (i = 0; i < actual_versions; ++i)
    6049          654 :     *empty_bb = add_condition_to_bb (dispatch_decl,
    6050              :                                      function_version_info[i].version_decl,
    6051          654 :                                      function_version_info[i].predicate_chain,
    6052              :                                      *empty_bb);
    6053              : 
    6054              :   /* dispatch default version at the end.  */
    6055          214 :   *empty_bb = add_condition_to_bb (dispatch_decl, default_decl,
    6056              :                                    NULL, *empty_bb);
    6057              : 
    6058          214 :   free (function_version_info);
    6059          214 :   return 0;
    6060              : }
    6061              : 
    6062              : /* This function changes the assembler name for functions that are
    6063              :    versions.  If DECL is a function version and has a "target"
    6064              :    attribute, it appends the attribute string to its assembler name.  */
    6065              : 
    6066              : static tree
    6067         1147 : ix86_mangle_function_version_assembler_name (tree decl, tree id)
    6068              : {
    6069         1147 :   tree version_attr;
    6070         1147 :   char *attr_str;
    6071              : 
    6072         1147 :   if (DECL_DECLARED_INLINE_P (decl)
    6073         1196 :       && lookup_attribute ("gnu_inline",
    6074           49 :                            DECL_ATTRIBUTES (decl)))
    6075            0 :     error_at (DECL_SOURCE_LOCATION (decl),
    6076              :               "function versions cannot be marked as %<gnu_inline%>,"
    6077              :               " bodies have to be generated");
    6078              : 
    6079         1147 :   if (DECL_VIRTUAL_P (decl)
    6080         2294 :       || DECL_VINDEX (decl))
    6081            0 :     sorry ("virtual function multiversioning not supported");
    6082              : 
    6083         1147 :   version_attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
    6084              : 
    6085              :   /* target attribute string cannot be NULL.  */
    6086         1147 :   gcc_assert (version_attr != NULL_TREE);
    6087              : 
    6088         1147 :   attr_str = sorted_attr_string (TREE_VALUE (version_attr));
    6089              : 
    6090              :   /* Allow assembler name to be modified if already set.  */
    6091         1147 :   if (DECL_ASSEMBLER_NAME_SET_P (decl))
    6092         1132 :     SET_DECL_RTL (decl, NULL);
    6093              : 
    6094         1147 :   tree ret = clone_identifier (id, attr_str, true);
    6095              : 
    6096         1147 :   XDELETEVEC (attr_str);
    6097              : 
    6098         1147 :   return ret;
    6099              : }
    6100              : 
    6101              : tree
    6102    510734879 : ix86_mangle_decl_assembler_name (tree decl, tree id)
    6103              : {
    6104              :   /* For function version, add the target suffix to the assembler name.  */
    6105    510734879 :   if (TREE_CODE (decl) == FUNCTION_DECL)
    6106              :     {
    6107    474547368 :       cgraph_node *node = cgraph_node::get (decl);
    6108              :       /* Mangle all versions when annotated with target_clones, but only
    6109              :          non-default versions when annotated with target attributes.  */
    6110    474547368 :       if (DECL_FUNCTION_VERSIONED (decl)
    6111    474547368 :           && (node->is_target_clone
    6112         1092 :               || !is_function_default_version (node->decl)))
    6113         1147 :         id = ix86_mangle_function_version_assembler_name (decl, id);
    6114              :       /* Mangle the dispatched symbol but only in the case of target clones.  */
    6115    474546221 :       else if (node && node->dispatcher_function && !node->is_target_clone)
    6116          117 :         id = clone_identifier (id, "ifunc");
    6117     67364961 :       else if (node && node->dispatcher_resolver_function)
    6118          214 :         id = clone_identifier (id, "resolver");
    6119              :     }
    6120              : #ifdef SUBTARGET_MANGLE_DECL_ASSEMBLER_NAME
    6121              :   id = SUBTARGET_MANGLE_DECL_ASSEMBLER_NAME (decl, id);
    6122              : #endif
    6123              : 
    6124    510734879 :   return id;
    6125              : }
    6126              : 
    6127              : /* Make a dispatcher declaration for the multi-versioned function DECL.
    6128              :    Calls to DECL function will be replaced with calls to the dispatcher
    6129              :    by the front-end.  Returns the decl of the dispatcher function.  */
    6130              : 
    6131              : tree
    6132          340 : ix86_get_function_versions_dispatcher (void *decl)
    6133              : {
    6134          340 :   tree fn = (tree) decl;
    6135          340 :   struct cgraph_node *node = NULL;
    6136          340 :   struct cgraph_node *default_node = NULL;
    6137          340 :   struct cgraph_function_version_info *node_v = NULL;
    6138              : 
    6139          340 :   tree dispatch_decl = NULL;
    6140              : 
    6141          340 :   struct cgraph_function_version_info *default_version_info = NULL;
    6142              : 
    6143          680 :   gcc_assert (fn != NULL && DECL_FUNCTION_VERSIONED (fn));
    6144              : 
    6145          340 :   node = cgraph_node::get (fn);
    6146          340 :   gcc_assert (node != NULL);
    6147              : 
    6148          340 :   node_v = node->function_version ();
    6149          340 :   gcc_assert (node_v != NULL);
    6150              : 
    6151          340 :   if (node_v->dispatcher_resolver != NULL)
    6152              :     return node_v->dispatcher_resolver;
    6153              : 
    6154              :   /* The default node is always the beginning of the chain.  */
    6155              :   default_version_info = node_v;
    6156          691 :   while (default_version_info->prev != NULL)
    6157              :     default_version_info = default_version_info->prev;
    6158          226 :   default_node = default_version_info->this_node;
    6159              : 
    6160              :   /* If there is no default node, just return NULL.  */
    6161          226 :   if (!is_function_default_version (default_node->decl))
    6162              :     return NULL;
    6163              : 
    6164              : #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
    6165          217 :   if (targetm.has_ifunc_p ())
    6166              :     {
    6167          217 :       struct cgraph_function_version_info *it_v = NULL;
    6168              : 
    6169              :       /* Right now, the dispatching is done via ifunc.  */
    6170          217 :       dispatch_decl = make_dispatcher_decl (default_node->decl);
    6171              : 
    6172              :       /* Set the dispatcher for all the versions.  */
    6173          217 :       it_v = default_version_info;
    6174         1465 :       while (it_v != NULL)
    6175              :         {
    6176         1031 :           it_v->dispatcher_resolver = dispatch_decl;
    6177         1031 :           it_v = it_v->next;
    6178              :         }
    6179              :     }
    6180              :   else
    6181              : #endif
    6182              :     {
    6183            0 :       error_at (DECL_SOURCE_LOCATION (default_node->decl),
    6184              :                 "multiversioning needs %<ifunc%> which is not supported "
    6185              :                 "on this target");
    6186              :     }
    6187              : 
    6188              :   return dispatch_decl;
    6189              : }
    6190              : 
    6191              : /* Make the resolver function decl to dispatch the versions of
    6192              :    a multi-versioned function,  DEFAULT_DECL.  IFUNC_ALIAS_DECL is
    6193              :    ifunc alias that will point to the created resolver.  Create an
    6194              :    empty basic block in the resolver and store the pointer in
    6195              :    EMPTY_BB.  Return the decl of the resolver function.  */
    6196              : 
    6197              : static tree
    6198          214 : make_resolver_func (const tree default_decl,
    6199              :                     const tree ifunc_alias_decl,
    6200              :                     basic_block *empty_bb)
    6201              : {
    6202          214 :   tree decl, type, t;
    6203              : 
    6204              :   /* The resolver function should return a (void *). */
    6205          214 :   type = build_function_type_list (ptr_type_node, NULL_TREE);
    6206              : 
    6207          214 :   cgraph_node *node = cgraph_node::get (default_decl);
    6208          214 :   gcc_assert (node && node->function_version ());
    6209              : 
    6210          214 :   decl = build_fn_decl (IDENTIFIER_POINTER (DECL_NAME (default_decl)), type);
    6211              : 
    6212              :   /* Set the assembler name to prevent cgraph_node attempting to mangle.  */
    6213          214 :   SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (default_decl));
    6214              : 
    6215          214 :   cgraph_node *resolver_node = cgraph_node::get_create (decl);
    6216          214 :   resolver_node->dispatcher_resolver_function = true;
    6217              : 
    6218          214 :   if (node->is_target_clone)
    6219          100 :     resolver_node->is_target_clone = true;
    6220              : 
    6221          214 :   tree id = ix86_mangle_decl_assembler_name
    6222          214 :     (decl, node->function_version ()->assembler_name);
    6223          214 :   symtab->change_decl_assembler_name (decl, id);
    6224              : 
    6225          214 :   DECL_NAME (decl) = DECL_NAME (default_decl);
    6226          214 :   TREE_USED (decl) = 1;
    6227          214 :   DECL_ARTIFICIAL (decl) = 1;
    6228          214 :   DECL_IGNORED_P (decl) = 1;
    6229          214 :   TREE_PUBLIC (decl) = 0;
    6230          214 :   DECL_UNINLINABLE (decl) = 1;
    6231              : 
    6232              :   /* Resolver is not external, body is generated.  */
    6233          214 :   DECL_EXTERNAL (decl) = 0;
    6234          214 :   DECL_EXTERNAL (ifunc_alias_decl) = 0;
    6235              : 
    6236          214 :   DECL_CONTEXT (decl) = NULL_TREE;
    6237          214 :   DECL_INITIAL (decl) = make_node (BLOCK);
    6238          214 :   DECL_STATIC_CONSTRUCTOR (decl) = 0;
    6239              : 
    6240          214 :   if (DECL_COMDAT_GROUP (default_decl)
    6241          214 :       || TREE_PUBLIC (default_decl))
    6242              :     {
    6243              :       /* In this case, each translation unit with a call to this
    6244              :          versioned function will put out a resolver.  Ensure it
    6245              :          is comdat to keep just one copy.  */
    6246          184 :       DECL_COMDAT (decl) = 1;
    6247          184 :       make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
    6248              :     }
    6249              :   else
    6250           30 :     TREE_PUBLIC (ifunc_alias_decl) = 0;
    6251              : 
    6252              :   /* Build result decl and add to function_decl. */
    6253          214 :   t = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, ptr_type_node);
    6254          214 :   DECL_CONTEXT (t) = decl;
    6255          214 :   DECL_ARTIFICIAL (t) = 1;
    6256          214 :   DECL_IGNORED_P (t) = 1;
    6257          214 :   DECL_RESULT (decl) = t;
    6258              : 
    6259          214 :   gimplify_function_tree (decl);
    6260          214 :   push_cfun (DECL_STRUCT_FUNCTION (decl));
    6261          214 :   *empty_bb = init_lowered_empty_function (decl, false,
    6262              :                                            profile_count::uninitialized ());
    6263              : 
    6264          214 :   cgraph_node::add_new_function (decl, true);
    6265          214 :   symtab->call_cgraph_insertion_hooks (cgraph_node::get_create (decl));
    6266              : 
    6267          214 :   pop_cfun ();
    6268              : 
    6269          214 :   gcc_assert (ifunc_alias_decl != NULL);
    6270              :   /* Mark ifunc_alias_decl as "ifunc" with resolver as resolver_name.  */
    6271          214 :   DECL_ATTRIBUTES (ifunc_alias_decl)
    6272          214 :     = make_attribute ("ifunc", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
    6273          214 :                       DECL_ATTRIBUTES (ifunc_alias_decl));
    6274              : 
    6275              :   /* Create the alias for dispatch to resolver here.  */
    6276          214 :   cgraph_node::create_same_body_alias (ifunc_alias_decl, decl);
    6277          214 :   return decl;
    6278              : }
    6279              : 
    6280              : /* Generate the dispatching code body to dispatch multi-versioned function
    6281              :    DECL.  The target hook is called to process the "target" attributes and
    6282              :    provide the code to dispatch the right function at run-time.  NODE points
    6283              :    to the dispatcher decl whose body will be created.  */
    6284              : 
    6285              : tree
    6286          214 : ix86_generate_version_dispatcher_body (void *node_p)
    6287              : {
    6288          214 :   tree resolver_decl;
    6289          214 :   basic_block empty_bb;
    6290          214 :   tree default_ver_decl;
    6291          214 :   struct cgraph_node *versn;
    6292          214 :   struct cgraph_node *node;
    6293              : 
    6294          214 :   struct cgraph_function_version_info *node_version_info = NULL;
    6295          214 :   struct cgraph_function_version_info *versn_info = NULL;
    6296              : 
    6297          214 :   node = (cgraph_node *)node_p;
    6298              : 
    6299          214 :   node_version_info = node->function_version ();
    6300          214 :   gcc_assert (node->dispatcher_function
    6301              :               && node_version_info != NULL);
    6302              : 
    6303          214 :   if (node_version_info->dispatcher_resolver)
    6304              :     return node_version_info->dispatcher_resolver;
    6305              : 
    6306              :   /* The first version in the chain corresponds to the default version.  */
    6307          214 :   default_ver_decl = node_version_info->next->this_node->decl;
    6308              : 
    6309              :   /* node is going to be an alias, so remove the finalized bit.  */
    6310          214 :   node->definition = false;
    6311              : 
    6312          214 :   resolver_decl = make_resolver_func (default_ver_decl,
    6313              :                                       node->decl, &empty_bb);
    6314              : 
    6315          214 :   node_version_info->dispatcher_resolver = resolver_decl;
    6316              : 
    6317          214 :   push_cfun (DECL_STRUCT_FUNCTION (resolver_decl));
    6318              : 
    6319          214 :   auto_vec<tree, 2> fn_ver_vec;
    6320              : 
    6321         1239 :   for (versn_info = node_version_info->next; versn_info;
    6322         1025 :        versn_info = versn_info->next)
    6323              :     {
    6324         1025 :       versn = versn_info->this_node;
    6325              :       /* Check for virtual functions here again, as by this time it should
    6326              :          have been determined if this function needs a vtable index or
    6327              :          not.  This happens for methods in derived classes that override
    6328              :          virtual methods in base classes but are not explicitly marked as
    6329              :          virtual.  */
    6330         1025 :       if (DECL_VIRTUAL_P (versn->decl))
    6331            0 :         sorry ("virtual function multiversioning not supported");
    6332              : 
    6333         1025 :       fn_ver_vec.safe_push (versn->decl);
    6334              :     }
    6335              : 
    6336          214 :   dispatch_function_versions (resolver_decl, &fn_ver_vec, &empty_bb);
    6337          214 :   cgraph_edge::rebuild_edges ();
    6338          214 :   pop_cfun ();
    6339          214 :   return resolver_decl;
    6340          214 : }
    6341              : 
    6342              : 
        

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.