LCOV - code coverage report
Current view: top level - gcc - tree-vect-slp.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.9 % 5505 5059
Test Date: 2024-12-21 13:15:12 Functions: 93.7 % 174 163
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* SLP - Basic Block Vectorization
       2                 :             :    Copyright (C) 2007-2024 Free Software Foundation, Inc.
       3                 :             :    Contributed by Dorit Naishlos <dorit@il.ibm.com>
       4                 :             :    and Ira Rosen <irar@il.ibm.com>
       5                 :             : 
       6                 :             : This file is part of GCC.
       7                 :             : 
       8                 :             : GCC is free software; you can redistribute it and/or modify it under
       9                 :             : the terms of the GNU General Public License as published by the Free
      10                 :             : Software Foundation; either version 3, or (at your option) any later
      11                 :             : version.
      12                 :             : 
      13                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      14                 :             : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      15                 :             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      16                 :             : for more details.
      17                 :             : 
      18                 :             : You should have received a copy of the GNU General Public License
      19                 :             : along with GCC; see the file COPYING3.  If not see
      20                 :             : <http://www.gnu.org/licenses/>.  */
      21                 :             : 
      22                 :             : #include "config.h"
      23                 :             : #define INCLUDE_ALGORITHM
      24                 :             : #include "system.h"
      25                 :             : #include "coretypes.h"
      26                 :             : #include "backend.h"
      27                 :             : #include "target.h"
      28                 :             : #include "rtl.h"
      29                 :             : #include "tree.h"
      30                 :             : #include "gimple.h"
      31                 :             : #include "tree-pass.h"
      32                 :             : #include "ssa.h"
      33                 :             : #include "optabs-tree.h"
      34                 :             : #include "insn-config.h"
      35                 :             : #include "recog.h"            /* FIXME: for insn_data */
      36                 :             : #include "fold-const.h"
      37                 :             : #include "stor-layout.h"
      38                 :             : #include "gimple-iterator.h"
      39                 :             : #include "cfgloop.h"
      40                 :             : #include "tree-vectorizer.h"
      41                 :             : #include "langhooks.h"
      42                 :             : #include "gimple-walk.h"
      43                 :             : #include "dbgcnt.h"
      44                 :             : #include "tree-vector-builder.h"
      45                 :             : #include "vec-perm-indices.h"
      46                 :             : #include "gimple-fold.h"
      47                 :             : #include "internal-fn.h"
      48                 :             : #include "dump-context.h"
      49                 :             : #include "cfganal.h"
      50                 :             : #include "tree-eh.h"
      51                 :             : #include "tree-cfg.h"
      52                 :             : #include "alloc-pool.h"
      53                 :             : #include "sreal.h"
      54                 :             : #include "predict.h"
      55                 :             : 
      56                 :             : static bool vect_transform_slp_perm_load_1 (vec_info *, slp_tree,
      57                 :             :                                             load_permutation_t &,
      58                 :             :                                             const vec<tree> &,
      59                 :             :                                             gimple_stmt_iterator *,
      60                 :             :                                             poly_uint64, bool, bool,
      61                 :             :                                             unsigned *,
      62                 :             :                                             unsigned * = nullptr,
      63                 :             :                                             bool = false);
      64                 :             : static int vectorizable_slp_permutation_1 (vec_info *, gimple_stmt_iterator *,
      65                 :             :                                            slp_tree, lane_permutation_t &,
      66                 :             :                                            vec<slp_tree> &, bool);
      67                 :             : static bool vectorizable_slp_permutation (vec_info *, gimple_stmt_iterator *,
      68                 :             :                                           slp_tree, stmt_vector_for_cost *);
      69                 :             : static void vect_print_slp_tree (dump_flags_t, dump_location_t, slp_tree);
      70                 :             : static bool vect_slp_can_convert_to_external (const vec<stmt_vec_info> &);
      71                 :             : 
      72                 :             : static object_allocator<_slp_tree> *slp_tree_pool;
      73                 :             : static slp_tree slp_first_node;
      74                 :             : 
      75                 :             : void
      76                 :     1068264 : vect_slp_init (void)
      77                 :             : {
      78                 :     1068264 :   slp_tree_pool = new object_allocator<_slp_tree> ("SLP nodes");
      79                 :     1068264 : }
      80                 :             : 
      81                 :             : void
      82                 :     1068264 : vect_slp_fini (void)
      83                 :             : {
      84                 :     1539213 :   while (slp_first_node)
      85                 :      470949 :     delete slp_first_node;
      86                 :     2136528 :   delete slp_tree_pool;
      87                 :     1068264 :   slp_tree_pool = NULL;
      88                 :     1068264 : }
      89                 :             : 
      90                 :             : void *
      91                 :     6147437 : _slp_tree::operator new (size_t n)
      92                 :             : {
      93                 :     6147437 :   gcc_assert (n == sizeof (_slp_tree));
      94                 :     6147437 :   return slp_tree_pool->allocate_raw ();
      95                 :             : }
      96                 :             : 
      97                 :             : void
      98                 :     6147437 : _slp_tree::operator delete (void *node, size_t n)
      99                 :             : {
     100                 :     6147437 :   gcc_assert (n == sizeof (_slp_tree));
     101                 :     6147437 :   slp_tree_pool->remove_raw (node);
     102                 :     6147437 : }
     103                 :             : 
     104                 :             : 
     105                 :             : /* Initialize a SLP node.  */
     106                 :             : 
     107                 :     6147437 : _slp_tree::_slp_tree ()
     108                 :             : {
     109                 :     6147437 :   this->prev_node = NULL;
     110                 :     6147437 :   if (slp_first_node)
     111                 :     5382321 :     slp_first_node->prev_node = this;
     112                 :     6147437 :   this->next_node = slp_first_node;
     113                 :     6147437 :   slp_first_node = this;
     114                 :     6147437 :   SLP_TREE_SCALAR_STMTS (this) = vNULL;
     115                 :     6147437 :   SLP_TREE_SCALAR_OPS (this) = vNULL;
     116                 :     6147437 :   SLP_TREE_VEC_DEFS (this) = vNULL;
     117                 :     6147437 :   SLP_TREE_NUMBER_OF_VEC_STMTS (this) = 0;
     118                 :     6147437 :   SLP_TREE_CHILDREN (this) = vNULL;
     119                 :     6147437 :   SLP_TREE_LOAD_PERMUTATION (this) = vNULL;
     120                 :     6147437 :   SLP_TREE_LANE_PERMUTATION (this) = vNULL;
     121                 :     6147437 :   SLP_TREE_SIMD_CLONE_INFO (this) = vNULL;
     122                 :     6147437 :   SLP_TREE_DEF_TYPE (this) = vect_uninitialized_def;
     123                 :     6147437 :   SLP_TREE_CODE (this) = ERROR_MARK;
     124                 :     6147437 :   this->ldst_lanes = false;
     125                 :     6147437 :   SLP_TREE_VECTYPE (this) = NULL_TREE;
     126                 :     6147437 :   SLP_TREE_REPRESENTATIVE (this) = NULL;
     127                 :     6147437 :   SLP_TREE_MEMORY_ACCESS_TYPE (this) = VMAT_INVARIANT;
     128                 :     6147437 :   SLP_TREE_REF_COUNT (this) = 1;
     129                 :     6147437 :   this->failed = NULL;
     130                 :     6147437 :   this->max_nunits = 1;
     131                 :     6147437 :   this->lanes = 0;
     132                 :     6147437 : }
     133                 :             : 
     134                 :             : /* Tear down a SLP node.  */
     135                 :             : 
     136                 :     6147437 : _slp_tree::~_slp_tree ()
     137                 :             : {
     138                 :     6147437 :   if (this->prev_node)
     139                 :     3842161 :     this->prev_node->next_node = this->next_node;
     140                 :             :   else
     141                 :     2305276 :     slp_first_node = this->next_node;
     142                 :     6147437 :   if (this->next_node)
     143                 :     4554452 :     this->next_node->prev_node = this->prev_node;
     144                 :     6147437 :   SLP_TREE_CHILDREN (this).release ();
     145                 :     6147437 :   SLP_TREE_SCALAR_STMTS (this).release ();
     146                 :     6147437 :   SLP_TREE_SCALAR_OPS (this).release ();
     147                 :     6147437 :   SLP_TREE_VEC_DEFS (this).release ();
     148                 :     6147437 :   SLP_TREE_LOAD_PERMUTATION (this).release ();
     149                 :     6147437 :   SLP_TREE_LANE_PERMUTATION (this).release ();
     150                 :     6147437 :   SLP_TREE_SIMD_CLONE_INFO (this).release ();
     151                 :     6147437 :   if (this->failed)
     152                 :     1856591 :     free (failed);
     153                 :     6147437 : }
     154                 :             : 
     155                 :             : /* Push the single SSA definition in DEF to the vector of vector defs.  */
     156                 :             : 
     157                 :             : void
     158                 :      467670 : _slp_tree::push_vec_def (gimple *def)
     159                 :             : {
     160                 :      467670 :   if (gphi *phi = dyn_cast <gphi *> (def))
     161                 :       54500 :     vec_defs.quick_push (gimple_phi_result (phi));
     162                 :             :   else
     163                 :             :     {
     164                 :      413170 :       def_operand_p defop = single_ssa_def_operand (def, SSA_OP_ALL_DEFS);
     165                 :      413170 :       vec_defs.quick_push (get_def_from_ptr (defop));
     166                 :             :     }
     167                 :      467670 : }
     168                 :             : 
     169                 :             : /* Recursively free the memory allocated for the SLP tree rooted at NODE.  */
     170                 :             : 
     171                 :             : void
     172                 :    11320704 : vect_free_slp_tree (slp_tree node)
     173                 :             : {
     174                 :    11320704 :   int i;
     175                 :    11320704 :   slp_tree child;
     176                 :             : 
     177                 :    11320704 :   if (--SLP_TREE_REF_COUNT (node) != 0)
     178                 :    11320704 :     return;
     179                 :             : 
     180                 :     8587839 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
     181                 :     2911351 :     if (child)
     182                 :     2610280 :       vect_free_slp_tree (child);
     183                 :             : 
     184                 :             :   /* If the node defines any SLP only patterns then those patterns are no
     185                 :             :      longer valid and should be removed.  */
     186                 :     5676488 :   stmt_vec_info rep_stmt_info = SLP_TREE_REPRESENTATIVE (node);
     187                 :     5676488 :   if (rep_stmt_info && STMT_VINFO_SLP_VECT_ONLY_PATTERN (rep_stmt_info))
     188                 :             :     {
     189                 :         448 :       stmt_vec_info stmt_info = vect_orig_stmt (rep_stmt_info);
     190                 :         448 :       STMT_VINFO_IN_PATTERN_P (stmt_info) = false;
     191                 :         448 :       STMT_SLP_TYPE (stmt_info) = STMT_SLP_TYPE (rep_stmt_info);
     192                 :             :     }
     193                 :             : 
     194                 :     5676488 :   delete node;
     195                 :             : }
     196                 :             : 
     197                 :             : /* Return a location suitable for dumpings related to the SLP instance.  */
     198                 :             : 
     199                 :             : dump_user_location_t
     200                 :     3295584 : _slp_instance::location () const
     201                 :             : {
     202                 :     3295584 :   if (!root_stmts.is_empty ())
     203                 :      252656 :     return root_stmts[0]->stmt;
     204                 :             :   else
     205                 :     3042928 :     return SLP_TREE_SCALAR_STMTS (root)[0]->stmt;
     206                 :             : }
     207                 :             : 
     208                 :             : 
     209                 :             : /* Free the memory allocated for the SLP instance.  */
     210                 :             : 
     211                 :             : void
     212                 :     1201460 : vect_free_slp_instance (slp_instance instance)
     213                 :             : {
     214                 :     1201460 :   vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
     215                 :     1201460 :   SLP_INSTANCE_LOADS (instance).release ();
     216                 :     1201460 :   SLP_INSTANCE_ROOT_STMTS (instance).release ();
     217                 :     1201460 :   SLP_INSTANCE_REMAIN_DEFS (instance).release ();
     218                 :     1201460 :   instance->subgraph_entries.release ();
     219                 :     1201460 :   instance->cost_vec.release ();
     220                 :     1201460 :   free (instance);
     221                 :     1201460 : }
     222                 :             : 
     223                 :             : 
     224                 :             : /* Create an SLP node for SCALAR_STMTS.  */
     225                 :             : 
     226                 :             : slp_tree
     227                 :       70284 : vect_create_new_slp_node (unsigned nops, tree_code code)
     228                 :             : {
     229                 :       70284 :   slp_tree node = new _slp_tree;
     230                 :       70284 :   SLP_TREE_SCALAR_STMTS (node) = vNULL;
     231                 :       70284 :   SLP_TREE_CHILDREN (node).create (nops);
     232                 :       70284 :   SLP_TREE_DEF_TYPE (node) = vect_internal_def;
     233                 :       70284 :   SLP_TREE_CODE (node) = code;
     234                 :       70284 :   return node;
     235                 :             : }
     236                 :             : /* Create an SLP node for SCALAR_STMTS.  */
     237                 :             : 
     238                 :             : static slp_tree
     239                 :     2691189 : vect_create_new_slp_node (slp_tree node,
     240                 :             :                           vec<stmt_vec_info> scalar_stmts, unsigned nops)
     241                 :             : {
     242                 :     2691189 :   SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
     243                 :     2691189 :   SLP_TREE_CHILDREN (node).create (nops);
     244                 :     2691189 :   SLP_TREE_DEF_TYPE (node) = vect_internal_def;
     245                 :     2691189 :   SLP_TREE_REPRESENTATIVE (node) = scalar_stmts[0];
     246                 :     2691189 :   SLP_TREE_LANES (node) = scalar_stmts.length ();
     247                 :     2691189 :   return node;
     248                 :             : }
     249                 :             : 
     250                 :             : /* Create an SLP node for SCALAR_STMTS.  */
     251                 :             : 
     252                 :             : static slp_tree
     253                 :        6192 : vect_create_new_slp_node (vec<stmt_vec_info> scalar_stmts, unsigned nops)
     254                 :             : {
     255                 :        6192 :   return vect_create_new_slp_node (new _slp_tree, scalar_stmts, nops);
     256                 :             : }
     257                 :             : 
     258                 :             : /* Create an SLP node for OPS.  */
     259                 :             : 
     260                 :             : static slp_tree
     261                 :     1523598 : vect_create_new_slp_node (slp_tree node, vec<tree> ops)
     262                 :             : {
     263                 :     1523598 :   SLP_TREE_SCALAR_OPS (node) = ops;
     264                 :     1523598 :   SLP_TREE_DEF_TYPE (node) = vect_external_def;
     265                 :           0 :   SLP_TREE_LANES (node) = ops.length ();
     266                 :     1523598 :   return node;
     267                 :             : }
     268                 :             : 
     269                 :             : /* Create an SLP node for OPS.  */
     270                 :             : 
     271                 :             : static slp_tree
     272                 :     1523598 : vect_create_new_slp_node (vec<tree> ops)
     273                 :             : {
     274                 :     1523598 :   return vect_create_new_slp_node (new _slp_tree, ops);
     275                 :             : }
     276                 :             : 
     277                 :             : 
     278                 :             : /* This structure is used in creation of an SLP tree.  Each instance
     279                 :             :    corresponds to the same operand in a group of scalar stmts in an SLP
     280                 :             :    node.  */
     281                 :             : typedef struct _slp_oprnd_info
     282                 :             : {
     283                 :             :   /* Def-stmts for the operands.  */
     284                 :             :   vec<stmt_vec_info> def_stmts;
     285                 :             :   /* Operands.  */
     286                 :             :   vec<tree> ops;
     287                 :             :   /* Information about the first statement, its vector def-type, type, the
     288                 :             :      operand itself in case it's constant, and an indication if it's a pattern
     289                 :             :      stmt and gather/scatter info.  */
     290                 :             :   tree first_op_type;
     291                 :             :   enum vect_def_type first_dt;
     292                 :             :   bool any_pattern;
     293                 :             :   bool first_gs_p;
     294                 :             :   gather_scatter_info first_gs_info;
     295                 :             : } *slp_oprnd_info;
     296                 :             : 
     297                 :             : 
     298                 :             : /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
     299                 :             :    operand.  */
     300                 :             : static vec<slp_oprnd_info>
     301                 :     2765120 : vect_create_oprnd_info (int nops, int group_size)
     302                 :             : {
     303                 :     2765120 :   int i;
     304                 :     2765120 :   slp_oprnd_info oprnd_info;
     305                 :     2765120 :   vec<slp_oprnd_info> oprnds_info;
     306                 :             : 
     307                 :     2765120 :   oprnds_info.create (nops);
     308                 :     9794199 :   for (i = 0; i < nops; i++)
     309                 :             :     {
     310                 :     4263959 :       oprnd_info = XNEW (struct _slp_oprnd_info);
     311                 :     4263959 :       oprnd_info->def_stmts.create (group_size);
     312                 :     4263959 :       oprnd_info->ops.create (group_size);
     313                 :     4263959 :       oprnd_info->first_dt = vect_uninitialized_def;
     314                 :     4263959 :       oprnd_info->first_op_type = NULL_TREE;
     315                 :     4263959 :       oprnd_info->any_pattern = false;
     316                 :     4263959 :       oprnd_info->first_gs_p = false;
     317                 :     4263959 :       oprnds_info.quick_push (oprnd_info);
     318                 :             :     }
     319                 :             : 
     320                 :     2765120 :   return oprnds_info;
     321                 :             : }
     322                 :             : 
     323                 :             : 
     324                 :             : /* Free operands info.  */
     325                 :             : 
     326                 :             : static void
     327                 :     2763816 : vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
     328                 :             : {
     329                 :     2763816 :   int i;
     330                 :     2763816 :   slp_oprnd_info oprnd_info;
     331                 :             : 
     332                 :     7026471 :   FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
     333                 :             :     {
     334                 :     4262655 :       oprnd_info->def_stmts.release ();
     335                 :     4262655 :       oprnd_info->ops.release ();
     336                 :     4262655 :       XDELETE (oprnd_info);
     337                 :             :     }
     338                 :             : 
     339                 :     2763816 :   oprnds_info.release ();
     340                 :     2763816 : }
     341                 :             : 
     342                 :             : /* Return the execution frequency of NODE (so that a higher value indicates
     343                 :             :    a "more important" node when optimizing for speed).  */
     344                 :             : 
     345                 :             : static sreal
     346                 :     2713958 : vect_slp_node_weight (slp_tree node)
     347                 :             : {
     348                 :     2713958 :   stmt_vec_info stmt_info = vect_orig_stmt (SLP_TREE_REPRESENTATIVE (node));
     349                 :     2713958 :   basic_block bb = gimple_bb (stmt_info->stmt);
     350                 :     2713958 :   return bb->count.to_sreal_scale (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
     351                 :             : }
     352                 :             : 
     353                 :             : /* Return true if STMTS contains a pattern statement.  */
     354                 :             : 
     355                 :             : static bool
     356                 :       23629 : vect_contains_pattern_stmt_p (vec<stmt_vec_info> stmts)
     357                 :             : {
     358                 :       23629 :   stmt_vec_info stmt_info;
     359                 :       23629 :   unsigned int i;
     360                 :       77572 :   FOR_EACH_VEC_ELT (stmts, i, stmt_info)
     361                 :       55941 :     if (stmt_info && is_pattern_stmt_p (stmt_info))
     362                 :             :       return true;
     363                 :             :   return false;
     364                 :             : }
     365                 :             : 
     366                 :             : /* Return true when all lanes in the external or constant NODE have
     367                 :             :    the same value.  */
     368                 :             : 
     369                 :             : static bool
     370                 :      493385 : vect_slp_tree_uniform_p (slp_tree node)
     371                 :             : {
     372                 :      493385 :   gcc_assert (SLP_TREE_DEF_TYPE (node) == vect_constant_def
     373                 :             :               || SLP_TREE_DEF_TYPE (node) == vect_external_def);
     374                 :             : 
     375                 :             :   /* Pre-exsting vectors.  */
     376                 :      852281 :   if (SLP_TREE_SCALAR_OPS (node).is_empty ())
     377                 :             :     return false;
     378                 :             : 
     379                 :             :   unsigned i;
     380                 :             :   tree op, first = NULL_TREE;
     381                 :     1146217 :   FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node), i, op)
     382                 :     1011728 :     if (!first)
     383                 :             :       first = op;
     384                 :      518343 :     else if (!operand_equal_p (first, op, 0))
     385                 :             :       return false;
     386                 :             : 
     387                 :             :   return true;
     388                 :             : }
     389                 :             : 
     390                 :             : /* Find the place of the data-ref in STMT_INFO in the interleaving chain
     391                 :             :    that starts from FIRST_STMT_INFO.  Return -1 if the data-ref is not a part
     392                 :             :    of the chain.  */
     393                 :             : 
     394                 :             : int
     395                 :      565087 : vect_get_place_in_interleaving_chain (stmt_vec_info stmt_info,
     396                 :             :                                       stmt_vec_info first_stmt_info)
     397                 :             : {
     398                 :      565087 :   stmt_vec_info next_stmt_info = first_stmt_info;
     399                 :      565087 :   int result = 0;
     400                 :             : 
     401                 :      565087 :   if (first_stmt_info != DR_GROUP_FIRST_ELEMENT (stmt_info))
     402                 :             :     return -1;
     403                 :             : 
     404                 :     1541746 :   do
     405                 :             :     {
     406                 :     1541746 :       if (next_stmt_info == stmt_info)
     407                 :             :         return result;
     408                 :      976659 :       next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
     409                 :      976659 :       if (next_stmt_info)
     410                 :      976659 :         result += DR_GROUP_GAP (next_stmt_info);
     411                 :             :     }
     412                 :      976659 :   while (next_stmt_info);
     413                 :             : 
     414                 :             :   return -1;
     415                 :             : }
     416                 :             : 
     417                 :             : /* Check whether it is possible to load COUNT elements of type ELT_TYPE
     418                 :             :    using the method implemented by duplicate_and_interleave.  Return true
     419                 :             :    if so, returning the number of intermediate vectors in *NVECTORS_OUT
     420                 :             :    (if nonnull) and the type of each intermediate vector in *VECTOR_TYPE_OUT
     421                 :             :    (if nonnull).  */
     422                 :             : 
     423                 :             : bool
     424                 :           0 : can_duplicate_and_interleave_p (vec_info *vinfo, unsigned int count,
     425                 :             :                                 tree elt_type, unsigned int *nvectors_out,
     426                 :             :                                 tree *vector_type_out,
     427                 :             :                                 tree *permutes)
     428                 :             : {
     429                 :           0 :   tree base_vector_type = get_vectype_for_scalar_type (vinfo, elt_type, count);
     430                 :           0 :   if (!base_vector_type || !VECTOR_MODE_P (TYPE_MODE (base_vector_type)))
     431                 :           0 :     return false;
     432                 :             : 
     433                 :           0 :   machine_mode base_vector_mode = TYPE_MODE (base_vector_type);
     434                 :           0 :   poly_int64 elt_bytes = count * GET_MODE_UNIT_SIZE (base_vector_mode);
     435                 :           0 :   unsigned int nvectors = 1;
     436                 :           0 :   for (;;)
     437                 :             :     {
     438                 :           0 :       scalar_int_mode int_mode;
     439                 :           0 :       poly_int64 elt_bits = elt_bytes * BITS_PER_UNIT;
     440                 :           0 :       if (int_mode_for_size (elt_bits, 1).exists (&int_mode))
     441                 :             :         {
     442                 :             :           /* Get the natural vector type for this SLP group size.  */
     443                 :           0 :           tree int_type = build_nonstandard_integer_type
     444                 :           0 :             (GET_MODE_BITSIZE (int_mode), 1);
     445                 :           0 :           tree vector_type
     446                 :           0 :             = get_vectype_for_scalar_type (vinfo, int_type, count);
     447                 :           0 :           poly_int64 half_nelts;
     448                 :           0 :           if (vector_type
     449                 :           0 :               && VECTOR_MODE_P (TYPE_MODE (vector_type))
     450                 :           0 :               && known_eq (GET_MODE_SIZE (TYPE_MODE (vector_type)),
     451                 :             :                            GET_MODE_SIZE (base_vector_mode))
     452                 :           0 :               && multiple_p (GET_MODE_NUNITS (TYPE_MODE (vector_type)),
     453                 :             :                              2, &half_nelts))
     454                 :             :             {
     455                 :             :               /* Try fusing consecutive sequences of COUNT / NVECTORS elements
     456                 :             :                  together into elements of type INT_TYPE and using the result
     457                 :             :                  to build NVECTORS vectors.  */
     458                 :           0 :               poly_uint64 nelts = GET_MODE_NUNITS (TYPE_MODE (vector_type));
     459                 :           0 :               vec_perm_builder sel1 (nelts, 2, 3);
     460                 :           0 :               vec_perm_builder sel2 (nelts, 2, 3);
     461                 :             : 
     462                 :           0 :               for (unsigned int i = 0; i < 3; ++i)
     463                 :             :                 {
     464                 :           0 :                   sel1.quick_push (i);
     465                 :           0 :                   sel1.quick_push (i + nelts);
     466                 :           0 :                   sel2.quick_push (half_nelts + i);
     467                 :           0 :                   sel2.quick_push (half_nelts + i + nelts);
     468                 :             :                 }
     469                 :           0 :               vec_perm_indices indices1 (sel1, 2, nelts);
     470                 :           0 :               vec_perm_indices indices2 (sel2, 2, nelts);
     471                 :           0 :               machine_mode vmode = TYPE_MODE (vector_type);
     472                 :           0 :               if (can_vec_perm_const_p (vmode, vmode, indices1)
     473                 :           0 :                   && can_vec_perm_const_p (vmode, vmode, indices2))
     474                 :             :                 {
     475                 :           0 :                   if (nvectors_out)
     476                 :           0 :                     *nvectors_out = nvectors;
     477                 :           0 :                   if (vector_type_out)
     478                 :           0 :                     *vector_type_out = vector_type;
     479                 :           0 :                   if (permutes)
     480                 :             :                     {
     481                 :           0 :                       permutes[0] = vect_gen_perm_mask_checked (vector_type,
     482                 :             :                                                                 indices1);
     483                 :           0 :                       permutes[1] = vect_gen_perm_mask_checked (vector_type,
     484                 :             :                                                                 indices2);
     485                 :             :                     }
     486                 :           0 :                   return true;
     487                 :             :                 }
     488                 :           0 :             }
     489                 :             :         }
     490                 :           0 :       if (!multiple_p (elt_bytes, 2, &elt_bytes))
     491                 :             :         return false;
     492                 :           0 :       nvectors *= 2;
     493                 :             :       /* We need to be able to fuse COUNT / NVECTORS elements together.  */
     494                 :           0 :       if (!multiple_p (count, nvectors))
     495                 :             :         return false;
     496                 :             :     }
     497                 :             : }
     498                 :             : 
     499                 :             : /* Return true if DTA and DTB match.  */
     500                 :             : 
     501                 :             : static bool
     502                 :    16735114 : vect_def_types_match (enum vect_def_type dta, enum vect_def_type dtb)
     503                 :             : {
     504                 :    16735114 :   return (dta == dtb
     505                 :      360005 :           || ((dta == vect_external_def || dta == vect_constant_def)
     506                 :      225335 :               && (dtb == vect_external_def || dtb == vect_constant_def)));
     507                 :             : }
     508                 :             : 
     509                 :             : static const int cond_expr_maps[3][5] = {
     510                 :             :   { 4, -1, -2, 1, 2 },
     511                 :             :   { 4, -2, -1, 1, 2 },
     512                 :             :   { 4, -1, -2, 2, 1 }
     513                 :             : };
     514                 :             : static const int no_arg_map[] = { 0 };
     515                 :             : static const int arg0_map[] = { 1, 0 };
     516                 :             : static const int arg1_map[] = { 1, 1 };
     517                 :             : static const int arg2_arg3_map[] = { 2, 2, 3 };
     518                 :             : static const int arg1_arg3_map[] = { 2, 1, 3 };
     519                 :             : static const int arg1_arg4_arg5_map[] = { 3, 1, 4, 5 };
     520                 :             : static const int arg1_arg3_arg4_map[] = { 3, 1, 3, 4 };
     521                 :             : static const int arg3_arg2_map[] = { 2, 3, 2 };
     522                 :             : static const int op1_op0_map[] = { 2, 1, 0 };
     523                 :             : static const int off_map[] = { 1, -3 };
     524                 :             : static const int off_op0_map[] = { 2, -3, 0 };
     525                 :             : static const int off_arg2_arg3_map[] = { 3, -3, 2, 3 };
     526                 :             : static const int off_arg3_arg2_map[] = { 3, -3, 3, 2 };
     527                 :             : static const int mask_call_maps[6][7] = {
     528                 :             :   { 1, 1, },
     529                 :             :   { 2, 1, 2, },
     530                 :             :   { 3, 1, 2, 3, },
     531                 :             :   { 4, 1, 2, 3, 4, },
     532                 :             :   { 5, 1, 2, 3, 4, 5, },
     533                 :             :   { 6, 1, 2, 3, 4, 5, 6 },
     534                 :             : };
     535                 :             : 
     536                 :             : /* For most SLP statements, there is a one-to-one mapping between
     537                 :             :    gimple arguments and child nodes.  If that is not true for STMT,
     538                 :             :    return an array that contains:
     539                 :             : 
     540                 :             :    - the number of child nodes, followed by
     541                 :             :    - for each child node, the index of the argument associated with that node.
     542                 :             :      The special index -1 is the first operand of an embedded comparison and
     543                 :             :      the special index -2 is the second operand of an embedded comparison.
     544                 :             :      The special indes -3 is the offset of a gather as analyzed by
     545                 :             :      vect_check_gather_scatter.
     546                 :             : 
     547                 :             :    SWAP is as for vect_get_and_check_slp_defs.  */
     548                 :             : 
     549                 :             : static const int *
     550                 :    17886188 : vect_get_operand_map (const gimple *stmt, bool gather_scatter_p = false,
     551                 :             :                       unsigned char swap = 0)
     552                 :             : {
     553                 :    17886188 :   if (auto assign = dyn_cast<const gassign *> (stmt))
     554                 :             :     {
     555                 :    16702968 :       if (gimple_assign_rhs_code (assign) == COND_EXPR
     556                 :    16702968 :           && COMPARISON_CLASS_P (gimple_assign_rhs1 (assign)))
     557                 :           0 :         gcc_unreachable ();
     558                 :    16702968 :       if (TREE_CODE_CLASS (gimple_assign_rhs_code (assign)) == tcc_comparison
     559                 :    16702968 :           && swap)
     560                 :             :         return op1_op0_map;
     561                 :    16351674 :       if (gather_scatter_p)
     562                 :       21704 :         return (TREE_CODE (gimple_assign_lhs (assign)) != SSA_NAME
     563                 :       21704 :                 ? off_op0_map : off_map);
     564                 :             :     }
     565                 :    17513190 :   gcc_assert (!swap);
     566                 :    17513190 :   if (auto call = dyn_cast<const gcall *> (stmt))
     567                 :             :     {
     568                 :      140999 :       if (gimple_call_internal_p (call))
     569                 :       72374 :         switch (gimple_call_internal_fn (call))
     570                 :             :           {
     571                 :       12248 :           case IFN_MASK_LOAD:
     572                 :       20016 :             return gather_scatter_p ? off_arg2_arg3_map : arg2_arg3_map;
     573                 :             : 
     574                 :           0 :           case IFN_GATHER_LOAD:
     575                 :           0 :             return arg1_map;
     576                 :             : 
     577                 :           0 :           case IFN_MASK_GATHER_LOAD:
     578                 :           0 :           case IFN_MASK_LEN_GATHER_LOAD:
     579                 :           0 :             return arg1_arg4_arg5_map;
     580                 :             : 
     581                 :           0 :           case IFN_SCATTER_STORE:
     582                 :           0 :             return arg1_arg3_map;
     583                 :             : 
     584                 :           0 :           case IFN_MASK_SCATTER_STORE:
     585                 :           0 :           case IFN_MASK_LEN_SCATTER_STORE:
     586                 :           0 :             return arg1_arg3_arg4_map;
     587                 :             : 
     588                 :        7036 :           case IFN_MASK_STORE:
     589                 :       12552 :             return gather_scatter_p ? off_arg3_arg2_map : arg3_arg2_map;
     590                 :             : 
     591                 :         884 :           case IFN_MASK_CALL:
     592                 :         884 :             {
     593                 :         884 :               unsigned nargs = gimple_call_num_args (call);
     594                 :         884 :               if (nargs >= 2 && nargs <= 7)
     595                 :         884 :                 return mask_call_maps[nargs-2];
     596                 :             :               else
     597                 :             :                 return nullptr;
     598                 :             :             }
     599                 :             : 
     600                 :         140 :           case IFN_CLZ:
     601                 :         140 :           case IFN_CTZ:
     602                 :         140 :             return arg0_map;
     603                 :             : 
     604                 :        7292 :           case IFN_GOMP_SIMD_LANE:
     605                 :        7292 :             return no_arg_map;
     606                 :             : 
     607                 :             :           default:
     608                 :             :             break;
     609                 :             :           }
     610                 :             :     }
     611                 :             :   return nullptr;
     612                 :             : }
     613                 :             : 
     614                 :             : /* Return the SLP node child index for operand OP of STMT.  */
     615                 :             : 
     616                 :             : int
     617                 :     1303557 : vect_slp_child_index_for_operand (const gimple *stmt, int op,
     618                 :             :                                   bool gather_scatter_p)
     619                 :             : {
     620                 :     1303557 :   const int *opmap = vect_get_operand_map (stmt, gather_scatter_p);
     621                 :     1303557 :   if (!opmap)
     622                 :             :     return op;
     623                 :       20745 :   for (int i = 1; i < 1 + opmap[0]; ++i)
     624                 :       20745 :     if (opmap[i] == op)
     625                 :       11200 :       return i - 1;
     626                 :           0 :   gcc_unreachable ();
     627                 :             : }
     628                 :             : 
     629                 :             : /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
     630                 :             :    they are of a valid type and that they match the defs of the first stmt of
     631                 :             :    the SLP group (stored in OPRNDS_INFO).  This function tries to match stmts
     632                 :             :    by swapping operands of STMTS[STMT_NUM] when possible.  Non-zero SWAP
     633                 :             :    indicates swap is required for cond_expr stmts.  Specifically, SWAP
     634                 :             :    is 1 if STMT is cond and operands of comparison need to be swapped;
     635                 :             :    SWAP is 2 if STMT is cond and code of comparison needs to be inverted.
     636                 :             : 
     637                 :             :    If there was a fatal error return -1; if the error could be corrected by
     638                 :             :    swapping operands of father node of this one, return 1; if everything is
     639                 :             :    ok return 0.  */
     640                 :             : static int
     641                 :    12026210 : vect_get_and_check_slp_defs (vec_info *vinfo, unsigned char swap,
     642                 :             :                              bool *skip_args,
     643                 :             :                              vec<stmt_vec_info> stmts, unsigned stmt_num,
     644                 :             :                              vec<slp_oprnd_info> *oprnds_info)
     645                 :             : {
     646                 :    12026210 :   stmt_vec_info stmt_info = stmts[stmt_num];
     647                 :    12026210 :   tree oprnd;
     648                 :    12026210 :   unsigned int i, number_of_oprnds;
     649                 :    12026210 :   enum vect_def_type dt = vect_uninitialized_def;
     650                 :    12026210 :   slp_oprnd_info oprnd_info;
     651                 :    12026210 :   gather_scatter_info gs_info;
     652                 :    12026210 :   unsigned int gs_op = -1u;
     653                 :    12026210 :   unsigned int commutative_op = -1U;
     654                 :    12026210 :   bool first = stmt_num == 0;
     655                 :             : 
     656                 :    12026210 :   if (!stmt_info)
     657                 :             :     {
     658                 :           0 :       for (auto oi : *oprnds_info)
     659                 :             :         {
     660                 :           0 :           oi->def_stmts.quick_push (NULL);
     661                 :           0 :           oi->ops.quick_push (NULL_TREE);
     662                 :             :         }
     663                 :             :       return 0;
     664                 :             :     }
     665                 :             : 
     666                 :    12026210 :   if (!is_a<gcall *> (stmt_info->stmt)
     667                 :             :       && !is_a<gassign *> (stmt_info->stmt)
     668                 :             :       && !is_a<gphi *> (stmt_info->stmt))
     669                 :             :     return -1;
     670                 :             : 
     671                 :    12026210 :   number_of_oprnds = gimple_num_args (stmt_info->stmt);
     672                 :    12026210 :   const int *map
     673                 :    24052420 :     = vect_get_operand_map (stmt_info->stmt,
     674                 :    12026210 :                             STMT_VINFO_GATHER_SCATTER_P (stmt_info), swap);
     675                 :    12026210 :   if (map)
     676                 :      370375 :     number_of_oprnds = *map++;
     677                 :    12026210 :   if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
     678                 :             :     {
     679                 :       41985 :       if (gimple_call_internal_p (stmt))
     680                 :             :         {
     681                 :       23400 :           internal_fn ifn = gimple_call_internal_fn (stmt);
     682                 :       23400 :           commutative_op = first_commutative_argument (ifn);
     683                 :             :         }
     684                 :             :     }
     685                 :    11984225 :   else if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
     686                 :             :     {
     687                 :    14063159 :       if (commutative_tree_code (gimple_assign_rhs_code (stmt)))
     688                 :    12026210 :         commutative_op = 0;
     689                 :             :     }
     690                 :             : 
     691                 :    12026210 :   bool swapped = (swap != 0);
     692                 :    12026210 :   bool backedge = false;
     693                 :    12026210 :   enum vect_def_type *dts = XALLOCAVEC (enum vect_def_type, number_of_oprnds);
     694                 :    33169982 :   for (i = 0; i < number_of_oprnds; i++)
     695                 :             :     {
     696                 :    21146391 :       oprnd_info = (*oprnds_info)[i];
     697                 :    21146391 :       int opno = map ? map[i] : int (i);
     698                 :    21146391 :       if (opno == -3)
     699                 :             :         {
     700                 :       11373 :           gcc_assert (STMT_VINFO_GATHER_SCATTER_P (stmt_info));
     701                 :       11373 :           if (!is_a <loop_vec_info> (vinfo)
     702                 :       11373 :               || !vect_check_gather_scatter (stmt_info,
     703                 :             :                                              as_a <loop_vec_info> (vinfo),
     704                 :             :                                              first ? &oprnd_info->first_gs_info
     705                 :             :                                              : &gs_info))
     706                 :        2619 :             return -1;
     707                 :             : 
     708                 :       11373 :           if (first)
     709                 :             :             {
     710                 :       11183 :               oprnd_info->first_gs_p = true;
     711                 :       11183 :               oprnd = oprnd_info->first_gs_info.offset;
     712                 :             :             }
     713                 :             :           else
     714                 :             :             {
     715                 :         190 :               gs_op = i;
     716                 :         190 :               oprnd = gs_info.offset;
     717                 :             :             }
     718                 :             :         }
     719                 :    21135018 :       else if (opno < 0)
     720                 :           0 :         oprnd = TREE_OPERAND (gimple_arg (stmt_info->stmt, 0), -1 - opno);
     721                 :             :       else
     722                 :             :         {
     723                 :    21135018 :           oprnd = gimple_arg (stmt_info->stmt, opno);
     724                 :    21135018 :           if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
     725                 :             :             {
     726                 :     1104370 :               edge e = gimple_phi_arg_edge (stmt, opno);
     727                 :     2208740 :               backedge = (is_a <bb_vec_info> (vinfo)
     728                 :     1577667 :                           ? e->flags & EDGE_DFS_BACK
     729                 :      473297 :                           : dominated_by_p (CDI_DOMINATORS, e->src,
     730                 :      473297 :                                             gimple_bb (stmt_info->stmt)));
     731                 :             :             }
     732                 :             :         }
     733                 :    21146391 :       if (TREE_CODE (oprnd) == VIEW_CONVERT_EXPR)
     734                 :        2411 :         oprnd = TREE_OPERAND (oprnd, 0);
     735                 :             : 
     736                 :    21146391 :       stmt_vec_info def_stmt_info;
     737                 :    21146391 :       if (!vect_is_simple_use (oprnd, vinfo, &dts[i], &def_stmt_info))
     738                 :             :         {
     739                 :        1181 :           if (dump_enabled_p ())
     740                 :           4 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
     741                 :             :                              "Build SLP failed: can't analyze def for %T\n",
     742                 :             :                              oprnd);
     743                 :             : 
     744                 :        1181 :           return -1;
     745                 :             :         }
     746                 :             : 
     747                 :    21145210 :       if (skip_args[i])
     748                 :             :         {
     749                 :      397781 :           oprnd_info->def_stmts.quick_push (NULL);
     750                 :      397781 :           oprnd_info->ops.quick_push (NULL_TREE);
     751                 :      397781 :           oprnd_info->first_dt = vect_uninitialized_def;
     752                 :      397781 :           continue;
     753                 :             :         }
     754                 :             : 
     755                 :    20747429 :       oprnd_info->def_stmts.quick_push (def_stmt_info);
     756                 :    20747429 :       oprnd_info->ops.quick_push (oprnd);
     757                 :             : 
     758                 :    20747429 :       if (def_stmt_info
     759                 :    20747429 :           && is_pattern_stmt_p (def_stmt_info))
     760                 :             :         {
     761                 :      332228 :           if (STMT_VINFO_RELATED_STMT (vect_orig_stmt (def_stmt_info))
     762                 :             :               != def_stmt_info)
     763                 :      239082 :             oprnd_info->any_pattern = true;
     764                 :             :           else
     765                 :             :             /* If we promote this to external use the original stmt def.  */
     766                 :       93146 :             oprnd_info->ops.last ()
     767                 :      186292 :               = gimple_get_lhs (vect_orig_stmt (def_stmt_info)->stmt);
     768                 :             :         }
     769                 :             : 
     770                 :             :       /* If there's a extern def on a backedge make sure we can
     771                 :             :          code-generate at the region start.
     772                 :             :          ???  This is another case that could be fixed by adjusting
     773                 :             :          how we split the function but at the moment we'd have conflicting
     774                 :             :          goals there.  */
     775                 :    20747429 :       if (backedge
     776                 :      121855 :           && dts[i] == vect_external_def
     777                 :        1462 :           && is_a <bb_vec_info> (vinfo)
     778                 :        1462 :           && TREE_CODE (oprnd) == SSA_NAME
     779                 :        1438 :           && !SSA_NAME_IS_DEFAULT_DEF (oprnd)
     780                 :    20748867 :           && !dominated_by_p (CDI_DOMINATORS, vinfo->bbs[0],
     781                 :        1438 :                               gimple_bb (SSA_NAME_DEF_STMT (oprnd))))
     782                 :             :         {
     783                 :        1438 :           if (dump_enabled_p ())
     784                 :           0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
     785                 :             :                              "Build SLP failed: extern def %T only defined "
     786                 :             :                              "on backedge\n", oprnd);
     787                 :        1438 :           return -1;
     788                 :             :         }
     789                 :             : 
     790                 :    20745991 :       if (first)
     791                 :             :         {
     792                 :     3872759 :           tree type = TREE_TYPE (oprnd);
     793                 :     3872759 :           dt = dts[i];
     794                 :             : 
     795                 :             :           /* For the swapping logic below force vect_reduction_def
     796                 :             :              for the reduction op in a SLP reduction group.  */
     797                 :     3872759 :           if (!STMT_VINFO_DATA_REF (stmt_info)
     798                 :     2777299 :               && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
     799                 :        1244 :               && (int)i == STMT_VINFO_REDUC_IDX (stmt_info)
     800                 :     3873369 :               && def_stmt_info)
     801                 :         610 :             dts[i] = dt = vect_reduction_def;
     802                 :             : 
     803                 :             :           /* Check the types of the definition.  */
     804                 :     3872759 :           switch (dt)
     805                 :             :             {
     806                 :     3872759 :             case vect_external_def:
     807                 :     3872759 :             case vect_constant_def:
     808                 :     3872759 :             case vect_internal_def:
     809                 :     3872759 :             case vect_reduction_def:
     810                 :     3872759 :             case vect_double_reduction_def:
     811                 :     3872759 :             case vect_induction_def:
     812                 :     3872759 :             case vect_nested_cycle:
     813                 :     3872759 :             case vect_first_order_recurrence:
     814                 :     3872759 :               break;
     815                 :             : 
     816                 :           0 :             default:
     817                 :             :               /* FORNOW: Not supported.  */
     818                 :           0 :               if (dump_enabled_p ())
     819                 :           0 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
     820                 :             :                                  "Build SLP failed: illegal type of def %T\n",
     821                 :             :                                  oprnd);
     822                 :           0 :               return -1;
     823                 :             :             }
     824                 :             : 
     825                 :     3872759 :           oprnd_info->first_dt = dt;
     826                 :     3872759 :           oprnd_info->first_op_type = type;
     827                 :             :         }
     828                 :             :     }
     829                 :    12023591 :   if (first)
     830                 :             :     return 0;
     831                 :             : 
     832                 :             :   /* Now match the operand definition types to that of the first stmt.  */
     833                 :    25820133 :   for (i = 0; i < number_of_oprnds;)
     834                 :             :     {
     835                 :    16852553 :       if (skip_args[i])
     836                 :             :         {
     837                 :       11018 :           ++i;
     838                 :       11018 :           continue;
     839                 :             :         }
     840                 :             : 
     841                 :    16841535 :       oprnd_info = (*oprnds_info)[i];
     842                 :    16841535 :       dt = dts[i];
     843                 :    16841535 :       stmt_vec_info def_stmt_info = oprnd_info->def_stmts[stmt_num];
     844                 :    16841535 :       oprnd = oprnd_info->ops[stmt_num];
     845                 :    16841535 :       tree type = TREE_TYPE (oprnd);
     846                 :             : 
     847                 :    16841535 :       if (!types_compatible_p (oprnd_info->first_op_type, type))
     848                 :             :         {
     849                 :      109216 :           if (dump_enabled_p ())
     850                 :         170 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
     851                 :             :                              "Build SLP failed: different operand types\n");
     852                 :      109216 :           return 1;
     853                 :             :         }
     854                 :             : 
     855                 :    16732319 :       if ((gs_op == i) != oprnd_info->first_gs_p)
     856                 :             :         {
     857                 :           0 :           if (dump_enabled_p ())
     858                 :           0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
     859                 :             :                              "Build SLP failed: mixed gather and non-gather\n");
     860                 :           0 :           return 1;
     861                 :             :         }
     862                 :    16732319 :       else if (gs_op == i)
     863                 :             :         {
     864                 :         182 :           if (!operand_equal_p (oprnd_info->first_gs_info.base,
     865                 :         182 :                                 gs_info.base))
     866                 :             :             {
     867                 :          20 :               if (dump_enabled_p ())
     868                 :           8 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
     869                 :             :                                  "Build SLP failed: different gather base\n");
     870                 :          20 :               return 1;
     871                 :             :             }
     872                 :         162 :           if (oprnd_info->first_gs_info.scale != gs_info.scale)
     873                 :             :             {
     874                 :           8 :               if (dump_enabled_p ())
     875                 :           2 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
     876                 :             :                                  "Build SLP failed: different gather scale\n");
     877                 :           8 :               return 1;
     878                 :             :             }
     879                 :             :         }
     880                 :             : 
     881                 :             :       /* Not first stmt of the group, check that the def-stmt/s match
     882                 :             :          the def-stmt/s of the first stmt.  Allow different definition
     883                 :             :          types for reduction chains: the first stmt must be a
     884                 :             :          vect_reduction_def (a phi node), and the rest
     885                 :             :          end in the reduction chain.  */
     886                 :    16732291 :       if ((!vect_def_types_match (oprnd_info->first_dt, dt)
     887                 :      304553 :            && !(oprnd_info->first_dt == vect_reduction_def
     888                 :        5123 :                 && !STMT_VINFO_DATA_REF (stmt_info)
     889                 :        5123 :                 && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
     890                 :        1705 :                 && def_stmt_info
     891                 :        1699 :                 && !STMT_VINFO_DATA_REF (def_stmt_info)
     892                 :        1693 :                 && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
     893                 :             :                     == REDUC_GROUP_FIRST_ELEMENT (stmt_info))))
     894                 :    16429208 :           || (!STMT_VINFO_DATA_REF (stmt_info)
     895                 :    15130661 :               && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
     896                 :        4174 :               && ((!def_stmt_info
     897                 :        4084 :                    || STMT_VINFO_DATA_REF (def_stmt_info)
     898                 :        7311 :                    || (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
     899                 :             :                        != REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
     900                 :        4174 :                   != (oprnd_info->first_dt != vect_reduction_def))))
     901                 :             :         {
     902                 :             :           /* Try swapping operands if we got a mismatch.  For BB
     903                 :             :              vectorization only in case it will clearly improve things.  */
     904                 :      305107 :           if (i == commutative_op && !swapped
     905                 :      303398 :               && (!is_a <bb_vec_info> (vinfo)
     906                 :        2114 :                   || (!vect_def_types_match ((*oprnds_info)[i+1]->first_dt,
     907                 :        2114 :                                              dts[i+1])
     908                 :         465 :                       && (vect_def_types_match (oprnd_info->first_dt, dts[i+1])
     909                 :             :                           || vect_def_types_match
     910                 :         244 :                                ((*oprnds_info)[i+1]->first_dt, dts[i])))))
     911                 :             :             {
     912                 :        1709 :               if (dump_enabled_p ())
     913                 :         338 :                 dump_printf_loc (MSG_NOTE, vect_location,
     914                 :             :                                  "trying swapped operands\n");
     915                 :        1709 :               std::swap (dts[i], dts[i+1]);
     916                 :        1709 :               std::swap ((*oprnds_info)[i]->def_stmts[stmt_num],
     917                 :        1709 :                          (*oprnds_info)[i+1]->def_stmts[stmt_num]);
     918                 :        1709 :               std::swap ((*oprnds_info)[i]->ops[stmt_num],
     919                 :        1709 :                          (*oprnds_info)[i+1]->ops[stmt_num]);
     920                 :             :               /* After swapping some operands we lost track whether an
     921                 :             :                  operand has any pattern defs so be conservative here.  */
     922                 :        1709 :               if ((*oprnds_info)[i]->any_pattern
     923                 :        1709 :                   || (*oprnds_info)[i+1]->any_pattern)
     924                 :           8 :                 (*oprnds_info)[i]->any_pattern
     925                 :           4 :                   = (*oprnds_info)[i+1]->any_pattern = true;
     926                 :        1709 :               swapped = true;
     927                 :        1709 :               continue;
     928                 :             :             }
     929                 :             : 
     930                 :      301689 :           if (is_a <bb_vec_info> (vinfo)
     931                 :      288207 :               && !oprnd_info->any_pattern
     932                 :      589696 :               && number_of_oprnds > 1)
     933                 :             :             {
     934                 :             :               /* Now for commutative ops we should see whether we can
     935                 :             :                  make the other operand matching.  */
     936                 :      116079 :               if (dump_enabled_p ())
     937                 :         149 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
     938                 :             :                                  "treating operand as external\n");
     939                 :      116079 :               oprnd_info->first_dt = dt = vect_external_def;
     940                 :             :             }
     941                 :             :           else
     942                 :             :             {
     943                 :      185610 :               if (dump_enabled_p ())
     944                 :         479 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
     945                 :             :                                  "Build SLP failed: different types\n");
     946                 :      185610 :               return 1;
     947                 :             :             }
     948                 :             :         }
     949                 :             : 
     950                 :             :       /* Make sure to demote the overall operand to external.  */
     951                 :    16544972 :       if (dt == vect_external_def)
     952                 :      374792 :         oprnd_info->first_dt = vect_external_def;
     953                 :             :       /* For a SLP reduction chain we want to duplicate the reduction to
     954                 :             :          each of the chain members.  That gets us a sane SLP graph (still
     955                 :             :          the stmts are not 100% correct wrt the initial values).  */
     956                 :    16170180 :       else if ((dt == vect_internal_def
     957                 :    16170180 :                 || dt == vect_reduction_def)
     958                 :    15302318 :                && oprnd_info->first_dt == vect_reduction_def
     959                 :       14456 :                && !STMT_VINFO_DATA_REF (stmt_info)
     960                 :       14456 :                && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
     961                 :        1879 :                && !STMT_VINFO_DATA_REF (def_stmt_info)
     962                 :    16172059 :                && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
     963                 :             :                    == REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
     964                 :             :         {
     965                 :        1879 :           oprnd_info->def_stmts[stmt_num] = oprnd_info->def_stmts[0];
     966                 :        1879 :           oprnd_info->ops[stmt_num] = oprnd_info->ops[0];
     967                 :             :         }
     968                 :             : 
     969                 :    16544972 :       ++i;
     970                 :             :     }
     971                 :             : 
     972                 :             :   /* Swap operands.  */
     973                 :     8967580 :   if (swapped)
     974                 :             :     {
     975                 :      121827 :       if (dump_enabled_p ())
     976                 :        1011 :         dump_printf_loc (MSG_NOTE, vect_location,
     977                 :             :                          "swapped operands to match def types in %G",
     978                 :             :                          stmt_info->stmt);
     979                 :             :     }
     980                 :             : 
     981                 :             :   return 0;
     982                 :             : }
     983                 :             : 
     984                 :             : /* Return true if call statements CALL1 and CALL2 are similar enough
     985                 :             :    to be combined into the same SLP group.  */
     986                 :             : 
     987                 :             : bool
     988                 :       25840 : compatible_calls_p (gcall *call1, gcall *call2)
     989                 :             : {
     990                 :       25840 :   unsigned int nargs = gimple_call_num_args (call1);
     991                 :       25840 :   if (nargs != gimple_call_num_args (call2))
     992                 :             :     return false;
     993                 :             : 
     994                 :       22996 :   if (gimple_call_combined_fn (call1) != gimple_call_combined_fn (call2))
     995                 :             :     return false;
     996                 :             : 
     997                 :       22996 :   if (gimple_call_internal_p (call1))
     998                 :             :     {
     999                 :        6365 :       if (!types_compatible_p (TREE_TYPE (gimple_call_lhs (call1)),
    1000                 :        6365 :                                TREE_TYPE (gimple_call_lhs (call2))))
    1001                 :             :         return false;
    1002                 :       12929 :       for (unsigned int i = 0; i < nargs; ++i)
    1003                 :        6564 :         if (!types_compatible_p (TREE_TYPE (gimple_call_arg (call1, i)),
    1004                 :        6564 :                                  TREE_TYPE (gimple_call_arg (call2, i))))
    1005                 :             :           return false;
    1006                 :             :     }
    1007                 :             :   else
    1008                 :             :     {
    1009                 :       16631 :       if (!operand_equal_p (gimple_call_fn (call1),
    1010                 :       16631 :                             gimple_call_fn (call2), 0))
    1011                 :             :         return false;
    1012                 :             : 
    1013                 :       32862 :       if (gimple_call_fntype (call1) != gimple_call_fntype (call2))
    1014                 :             :         return false;
    1015                 :             :     }
    1016                 :             : 
    1017                 :             :   /* Check that any unvectorized arguments are equal.  */
    1018                 :       17319 :   if (const int *map = vect_get_operand_map (call1))
    1019                 :             :     {
    1020                 :          15 :       unsigned int nkept = *map++;
    1021                 :          15 :       unsigned int mapi = 0;
    1022                 :          57 :       for (unsigned int i = 0; i < nargs; ++i)
    1023                 :          42 :         if (mapi < nkept && map[mapi] == int (i))
    1024                 :          27 :           mapi += 1;
    1025                 :          15 :         else if (!operand_equal_p (gimple_call_arg (call1, i),
    1026                 :          15 :                                    gimple_call_arg (call2, i)))
    1027                 :             :           return false;
    1028                 :             :     }
    1029                 :             : 
    1030                 :             :   return true;
    1031                 :             : }
    1032                 :             : 
    1033                 :             : /* A subroutine of vect_build_slp_tree for checking VECTYPE, which is the
    1034                 :             :    caller's attempt to find the vector type in STMT_INFO with the narrowest
    1035                 :             :    element type.  Return true if VECTYPE is nonnull and if it is valid
    1036                 :             :    for STMT_INFO.  When returning true, update MAX_NUNITS to reflect the
    1037                 :             :    number of units in VECTYPE.  GROUP_SIZE and MAX_NUNITS are as for
    1038                 :             :    vect_build_slp_tree.  */
    1039                 :             : 
    1040                 :             : static bool
    1041                 :     4593249 : vect_record_max_nunits (vec_info *vinfo, stmt_vec_info stmt_info,
    1042                 :             :                         unsigned int group_size,
    1043                 :             :                         tree vectype, poly_uint64 *max_nunits)
    1044                 :             : {
    1045                 :     4593249 :   if (!vectype)
    1046                 :             :     {
    1047                 :           0 :       if (dump_enabled_p ())
    1048                 :           0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1049                 :             :                          "Build SLP failed: unsupported data-type in %G\n",
    1050                 :             :                          stmt_info->stmt);
    1051                 :             :       /* Fatal mismatch.  */
    1052                 :           0 :       return false;
    1053                 :             :     }
    1054                 :             : 
    1055                 :             :   /* If populating the vector type requires unrolling then fail
    1056                 :             :      before adjusting *max_nunits for basic-block vectorization.  */
    1057                 :     4593249 :   if (is_a <bb_vec_info> (vinfo)
    1058                 :     4593249 :       && !multiple_p (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
    1059                 :             :     {
    1060                 :      130563 :       if (dump_enabled_p ())
    1061                 :          34 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1062                 :             :                          "Build SLP failed: unrolling required "
    1063                 :             :                          "in basic block SLP\n");
    1064                 :             :       /* Fatal mismatch.  */
    1065                 :      130563 :       return false;
    1066                 :             :     }
    1067                 :             : 
    1068                 :             :   /* In case of multiple types we need to detect the smallest type.  */
    1069                 :     4462686 :   vect_update_max_nunits (max_nunits, vectype);
    1070                 :     4462686 :   return true;
    1071                 :             : }
    1072                 :             : 
    1073                 :             : /* Verify if the scalar stmts STMTS are isomorphic, require data
    1074                 :             :    permutation or are of unsupported types of operation.  Return
    1075                 :             :    true if they are, otherwise return false and indicate in *MATCHES
    1076                 :             :    which stmts are not isomorphic to the first one.  If MATCHES[0]
    1077                 :             :    is false then this indicates the comparison could not be
    1078                 :             :    carried out or the stmts will never be vectorized by SLP.
    1079                 :             : 
    1080                 :             :    Note COND_EXPR is possibly isomorphic to another one after swapping its
    1081                 :             :    operands.  Set SWAP[i] to 1 if stmt I is COND_EXPR and isomorphic to
    1082                 :             :    the first stmt by swapping the two operands of comparison; set SWAP[i]
    1083                 :             :    to 2 if stmt I is isormorphic to the first stmt by inverting the code
    1084                 :             :    of comparison.  Take A1 >= B1 ? X1 : Y1 as an exmple, it can be swapped
    1085                 :             :    to (B1 <= A1 ? X1 : Y1); or be inverted to (A1 < B1) ? Y1 : X1.  */
    1086                 :             : 
    1087                 :             : static bool
    1088                 :     4539102 : vect_build_slp_tree_1 (vec_info *vinfo, unsigned char *swap,
    1089                 :             :                        vec<stmt_vec_info> stmts, unsigned int group_size,
    1090                 :             :                        poly_uint64 *max_nunits, bool *matches,
    1091                 :             :                        bool *two_operators, tree *node_vectype)
    1092                 :             : {
    1093                 :     4539102 :   unsigned int i;
    1094                 :     4539102 :   stmt_vec_info first_stmt_info = stmts[0];
    1095                 :     4539102 :   code_helper first_stmt_code = ERROR_MARK;
    1096                 :     4539102 :   code_helper alt_stmt_code = ERROR_MARK;
    1097                 :     4539102 :   code_helper first_cond_code = ERROR_MARK;
    1098                 :     4539102 :   bool need_same_oprnds = false;
    1099                 :     4539102 :   tree first_lhs = NULL_TREE;
    1100                 :     4539102 :   tree first_op1 = NULL_TREE;
    1101                 :     4539102 :   stmt_vec_info first_load = NULL, prev_first_load = NULL;
    1102                 :     4539102 :   bool first_stmt_ldst_p = false;
    1103                 :     4539102 :   bool first_stmt_phi_p = false;
    1104                 :     4539102 :   int first_reduc_idx = -1;
    1105                 :     4539102 :   bool maybe_soft_fail = false;
    1106                 :     4539102 :   tree soft_fail_nunits_vectype = NULL_TREE;
    1107                 :             : 
    1108                 :     4539102 :   tree vectype, nunits_vectype;
    1109                 :     4539102 :   if (!vect_get_vector_types_for_stmt (vinfo, first_stmt_info, &vectype,
    1110                 :     4539102 :                                        &nunits_vectype, group_size))
    1111                 :             :     {
    1112                 :             :       /* Fatal mismatch.  */
    1113                 :      177053 :       matches[0] = false;
    1114                 :      177053 :       return false;
    1115                 :             :     }
    1116                 :             :   /* Record nunits required but continue analysis, producing matches[]
    1117                 :             :      as if nunits was not an issue.  This allows splitting of groups
    1118                 :             :      to happen.  */
    1119                 :     4362049 :   if (nunits_vectype
    1120                 :     4362049 :       && !vect_record_max_nunits (vinfo, first_stmt_info, group_size,
    1121                 :             :                                   nunits_vectype, max_nunits))
    1122                 :             :     {
    1123                 :      130563 :       gcc_assert (is_a <bb_vec_info> (vinfo));
    1124                 :      130563 :       maybe_soft_fail = true;
    1125                 :      130563 :       soft_fail_nunits_vectype = nunits_vectype;
    1126                 :             :     }
    1127                 :             : 
    1128                 :     4362049 :   gcc_assert (vectype);
    1129                 :     4362049 :   *node_vectype = vectype;
    1130                 :             : 
    1131                 :             :   /* For every stmt in NODE find its def stmt/s.  */
    1132                 :     4362049 :   stmt_vec_info stmt_info;
    1133                 :    20279376 :   FOR_EACH_VEC_ELT (stmts, i, stmt_info)
    1134                 :             :     {
    1135                 :    16131595 :       bool ldst_p = false;
    1136                 :    16131595 :       bool phi_p = false;
    1137                 :    16131595 :       code_helper rhs_code = ERROR_MARK;
    1138                 :             : 
    1139                 :    16131595 :       swap[i] = 0;
    1140                 :    16131595 :       matches[i] = false;
    1141                 :    16131595 :       if (!stmt_info)
    1142                 :             :         {
    1143                 :       70473 :           matches[i] = true;
    1144                 :    15987800 :           continue;
    1145                 :             :         }
    1146                 :             : 
    1147                 :    16061122 :       gimple *stmt = stmt_info->stmt;
    1148                 :    16061122 :       if (dump_enabled_p ())
    1149                 :      221316 :         dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for %G", stmt);
    1150                 :             : 
    1151                 :             :       /* Fail to vectorize statements marked as unvectorizable, throw
    1152                 :             :          or are volatile.  */
    1153                 :    16061122 :       if (!STMT_VINFO_VECTORIZABLE (stmt_info)
    1154                 :    15839407 :           || stmt_can_throw_internal (cfun, stmt)
    1155                 :    31089029 :           || gimple_has_volatile_ops (stmt))
    1156                 :             :         {
    1157                 :      228762 :           if (dump_enabled_p ())
    1158                 :         231 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1159                 :             :                              "Build SLP failed: unvectorizable statement %G",
    1160                 :             :                              stmt);
    1161                 :             :           /* ???  For BB vectorization we want to commutate operands in a way
    1162                 :             :              to shuffle all unvectorizable defs into one operand and have
    1163                 :             :              the other still vectorized.  The following doesn't reliably
    1164                 :             :              work for this though but it's the easiest we can do here.  */
    1165                 :      228762 :           if (is_a <bb_vec_info> (vinfo) && i != 0)
    1166                 :       65604 :             continue;
    1167                 :             :           /* Fatal mismatch.  */
    1168                 :      163158 :           matches[0] = false;
    1169                 :      163158 :           return false;
    1170                 :             :         }
    1171                 :             : 
    1172                 :    15832360 :       gcall *call_stmt = dyn_cast <gcall *> (stmt);
    1173                 :    15832360 :       tree lhs = gimple_get_lhs (stmt);
    1174                 :    15832360 :       if (lhs == NULL_TREE
    1175                 :    15832360 :           && (!call_stmt
    1176                 :        1955 :               || !gimple_call_internal_p (stmt)
    1177                 :        1955 :               || !internal_store_fn_p (gimple_call_internal_fn (stmt))))
    1178                 :             :         {
    1179                 :          60 :           if (dump_enabled_p ())
    1180                 :           0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1181                 :             :                              "Build SLP failed: not GIMPLE_ASSIGN nor "
    1182                 :             :                              "GIMPLE_CALL %G", stmt);
    1183                 :          60 :           if (is_a <bb_vec_info> (vinfo) && i != 0)
    1184                 :          60 :             continue;
    1185                 :             :           /* Fatal mismatch.  */
    1186                 :           0 :           matches[0] = false;
    1187                 :           0 :           return false;
    1188                 :             :         }
    1189                 :             : 
    1190                 :    15832300 :       if (call_stmt)
    1191                 :             :         {
    1192                 :      102322 :           combined_fn cfn = gimple_call_combined_fn (call_stmt);
    1193                 :      102322 :           if (cfn != CFN_LAST && cfn != CFN_MASK_CALL)
    1194                 :       51920 :             rhs_code = cfn;
    1195                 :             :           else
    1196                 :             :             rhs_code = CALL_EXPR;
    1197                 :             : 
    1198                 :      102322 :           if (cfn == CFN_MASK_LOAD
    1199                 :      102322 :               || cfn == CFN_GATHER_LOAD
    1200                 :             :               || cfn == CFN_MASK_GATHER_LOAD
    1201                 :             :               || cfn == CFN_MASK_LEN_GATHER_LOAD
    1202                 :             :               || cfn == CFN_SCATTER_STORE
    1203                 :             :               || cfn == CFN_MASK_SCATTER_STORE
    1204                 :             :               || cfn == CFN_MASK_LEN_SCATTER_STORE)
    1205                 :             :             ldst_p = true;
    1206                 :             :           else if (cfn == CFN_MASK_STORE)
    1207                 :             :             {
    1208                 :             :               ldst_p = true;
    1209                 :             :               rhs_code = CFN_MASK_STORE;
    1210                 :             :             }
    1211                 :             :           else if (cfn == CFN_GOMP_SIMD_LANE)
    1212                 :             :             ;
    1213                 :       92882 :           else if ((cfn != CFN_LAST
    1214                 :             :                     && cfn != CFN_MASK_CALL
    1215                 :       42480 :                     && internal_fn_p (cfn)
    1216                 :       30209 :                     && !vectorizable_internal_fn_p (as_internal_fn (cfn)))
    1217                 :       92817 :                    || gimple_call_tail_p (call_stmt)
    1218                 :       92817 :                    || gimple_call_noreturn_p (call_stmt)
    1219                 :      185699 :                    || gimple_call_chain (call_stmt))
    1220                 :             :             {
    1221                 :         665 :               if (dump_enabled_p ())
    1222                 :           0 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1223                 :             :                                  "Build SLP failed: unsupported call type %G",
    1224                 :             :                                  (gimple *) call_stmt);
    1225                 :         665 :               if (is_a <bb_vec_info> (vinfo) && i != 0)
    1226                 :          87 :                 continue;
    1227                 :             :               /* Fatal mismatch.  */
    1228                 :         578 :               matches[0] = false;
    1229                 :         578 :               return false;
    1230                 :             :             }
    1231                 :             :         }
    1232                 :    15729978 :       else if (gimple_code (stmt) == GIMPLE_PHI)
    1233                 :             :         {
    1234                 :             :           rhs_code = ERROR_MARK;
    1235                 :             :           phi_p = true;
    1236                 :             :         }
    1237                 :             :       else
    1238                 :             :         {
    1239                 :    14918478 :           rhs_code = gimple_assign_rhs_code (stmt);
    1240                 :    14918478 :           ldst_p = STMT_VINFO_DATA_REF (stmt_info) != nullptr;
    1241                 :             :         }
    1242                 :             : 
    1243                 :             :       /* Check the operation.  */
    1244                 :    15831635 :       if (i == 0)
    1245                 :             :         {
    1246                 :     4198313 :           first_lhs = lhs;
    1247                 :     4198313 :           first_stmt_code = rhs_code;
    1248                 :     4198313 :           first_stmt_ldst_p = ldst_p;
    1249                 :     4198313 :           first_stmt_phi_p = phi_p;
    1250                 :     4198313 :           first_reduc_idx = STMT_VINFO_REDUC_IDX (stmt_info);
    1251                 :             : 
    1252                 :             :           /* Shift arguments should be equal in all the packed stmts for a
    1253                 :             :              vector shift with scalar shift operand.  */
    1254                 :     4198313 :           if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
    1255                 :     4066954 :               || rhs_code == LROTATE_EXPR
    1256                 :     8265260 :               || rhs_code == RROTATE_EXPR)
    1257                 :             :             {
    1258                 :             :               /* First see if we have a vector/vector shift.  */
    1259                 :      131505 :               if (!directly_supported_p (rhs_code, vectype, optab_vector))
    1260                 :             :                 {
    1261                 :             :                   /* No vector/vector shift, try for a vector/scalar shift.  */
    1262                 :      118699 :                   if (!directly_supported_p (rhs_code, vectype, optab_scalar))
    1263                 :             :                     {
    1264                 :       29915 :                       if (dump_enabled_p ())
    1265                 :         457 :                         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1266                 :             :                                          "Build SLP failed: "
    1267                 :             :                                          "op not supported by target.\n");
    1268                 :       29915 :                       if (is_a <bb_vec_info> (vinfo) && i != 0)
    1269                 :             :                         continue;
    1270                 :             :                       /* Fatal mismatch.  */
    1271                 :       29915 :                       matches[0] = false;
    1272                 :       29915 :                       return false;
    1273                 :             :                     }
    1274                 :       88784 :                   need_same_oprnds = true;
    1275                 :       88784 :                   first_op1 = gimple_assign_rhs2 (stmt);
    1276                 :             :                 }
    1277                 :             :             }
    1278                 :     4066808 :           else if (rhs_code == WIDEN_LSHIFT_EXPR)
    1279                 :             :             {
    1280                 :           0 :               need_same_oprnds = true;
    1281                 :           0 :               first_op1 = gimple_assign_rhs2 (stmt);
    1282                 :             :             }
    1283                 :     4066808 :           else if (!ldst_p
    1284                 :     4066808 :                    && rhs_code == BIT_FIELD_REF)
    1285                 :             :             {
    1286                 :        5710 :               tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
    1287                 :        5710 :               if (!is_a <bb_vec_info> (vinfo)
    1288                 :        5628 :                   || TREE_CODE (vec) != SSA_NAME
    1289                 :             :                   /* When the element types are not compatible we pun the
    1290                 :             :                      source to the target vectype which requires equal size.  */
    1291                 :       11326 :                   || ((!VECTOR_TYPE_P (TREE_TYPE (vec))
    1292                 :        4753 :                        || !types_compatible_p (TREE_TYPE (vectype),
    1293                 :        4753 :                                                TREE_TYPE (TREE_TYPE (vec))))
    1294                 :        1056 :                       && !operand_equal_p (TYPE_SIZE (vectype),
    1295                 :        1056 :                                            TYPE_SIZE (TREE_TYPE (vec)))))
    1296                 :             :                 {
    1297                 :         802 :                   if (dump_enabled_p ())
    1298                 :           0 :                     dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1299                 :             :                                      "Build SLP failed: "
    1300                 :             :                                      "BIT_FIELD_REF not supported\n");
    1301                 :             :                   /* Fatal mismatch.  */
    1302                 :         802 :                   matches[0] = false;
    1303                 :         802 :                   return false;
    1304                 :             :                 }
    1305                 :             :             }
    1306                 :     4061098 :           else if (rhs_code == CFN_DIV_POW2)
    1307                 :             :             {
    1308                 :           0 :               need_same_oprnds = true;
    1309                 :           0 :               first_op1 = gimple_call_arg (call_stmt, 1);
    1310                 :             :             }
    1311                 :     4061098 :           else if (rhs_code == CFN_GOMP_SIMD_LANE)
    1312                 :             :             {
    1313                 :        3646 :               need_same_oprnds = true;
    1314                 :        3646 :               first_op1 = gimple_call_arg (call_stmt, 1);
    1315                 :             :             }
    1316                 :             :         }
    1317                 :             :       else
    1318                 :             :         {
    1319                 :    11634300 :           if (first_reduc_idx != STMT_VINFO_REDUC_IDX (stmt_info)
    1320                 :             :               /* For SLP reduction groups the index isn't necessarily
    1321                 :             :                  uniform but only that of the first stmt matters.  */
    1322                 :    11633322 :               && !(first_reduc_idx != -1
    1323                 :        1697 :                    && STMT_VINFO_REDUC_IDX (stmt_info) != -1
    1324                 :        1697 :                    && REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
    1325                 :             :             {
    1326                 :         978 :               if (dump_enabled_p ())
    1327                 :             :                 {
    1328                 :          56 :                   dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1329                 :             :                                    "Build SLP failed: different reduc_idx "
    1330                 :             :                                    "%d instead of %d in %G",
    1331                 :             :                                    STMT_VINFO_REDUC_IDX (stmt_info),
    1332                 :             :                                    first_reduc_idx, stmt);
    1333                 :             :                 }
    1334                 :             :               /* Mismatch.  */
    1335                 :         978 :               continue;
    1336                 :             :             }
    1337                 :    11632344 :           if (!ldst_p
    1338                 :     9254235 :               && first_stmt_code != rhs_code
    1339                 :    13108387 :               && alt_stmt_code == ERROR_MARK)
    1340                 :             :             alt_stmt_code = rhs_code;
    1341                 :    13093959 :           if ((!ldst_p
    1342                 :     9254235 :                && first_stmt_code != rhs_code
    1343                 :     1476043 :                && (first_stmt_code != IMAGPART_EXPR
    1344                 :         112 :                    || rhs_code != REALPART_EXPR)
    1345                 :     1476031 :                && (first_stmt_code != REALPART_EXPR
    1346                 :         238 :                    || rhs_code != IMAGPART_EXPR)
    1347                 :             :                /* Handle mismatches in plus/minus by computing both
    1348                 :             :                   and merging the results.  */
    1349                 :     1476028 :                && !((first_stmt_code == PLUS_EXPR
    1350                 :     1373101 :                      || first_stmt_code == MINUS_EXPR)
    1351                 :      125514 :                     && (alt_stmt_code == PLUS_EXPR
    1352                 :      115609 :                         || alt_stmt_code == MINUS_EXPR)
    1353                 :       19316 :                     && rhs_code == alt_stmt_code)
    1354                 :     1456890 :                && !(first_stmt_code.is_tree_code ()
    1355                 :     1351046 :                     && rhs_code.is_tree_code ()
    1356                 :     1240276 :                     && (TREE_CODE_CLASS (tree_code (first_stmt_code))
    1357                 :             :                         == tcc_comparison)
    1358                 :      131170 :                     && (swap_tree_comparison (tree_code (first_stmt_code))
    1359                 :      131170 :                         == tree_code (rhs_code))))
    1360                 :             :               || (ldst_p
    1361                 :     4756218 :                   && (STMT_VINFO_GROUPED_ACCESS (stmt_info)
    1362                 :     2378109 :                       != STMT_VINFO_GROUPED_ACCESS (first_stmt_info)))
    1363                 :             :               || (ldst_p
    1364                 :     2331696 :                   && (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
    1365                 :     2331696 :                       != STMT_VINFO_GATHER_SCATTER_P (first_stmt_info)))
    1366                 :    10170844 :               || first_stmt_ldst_p != ldst_p
    1367                 :    21803188 :               || first_stmt_phi_p != phi_p)
    1368                 :             :             {
    1369                 :     1461615 :               if (dump_enabled_p ())
    1370                 :             :                 {
    1371                 :        3236 :                   dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1372                 :             :                                    "Build SLP failed: different operation "
    1373                 :             :                                    "in stmt %G", stmt);
    1374                 :        3236 :                   dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1375                 :             :                                    "original stmt %G", first_stmt_info->stmt);
    1376                 :             :                 }
    1377                 :             :               /* Mismatch.  */
    1378                 :     1461615 :               continue;
    1379                 :             :             }
    1380                 :             : 
    1381                 :    10172552 :           if (!ldst_p
    1382                 :     7839135 :               && first_stmt_code == BIT_FIELD_REF
    1383                 :    10175878 :               && (TREE_OPERAND (gimple_assign_rhs1 (first_stmt_info->stmt), 0)
    1384                 :        5149 :                   != TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0)))
    1385                 :             :             {
    1386                 :        1823 :               if (dump_enabled_p ())
    1387                 :          36 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1388                 :             :                                  "Build SLP failed: different BIT_FIELD_REF "
    1389                 :             :                                  "arguments in %G", stmt);
    1390                 :             :               /* Mismatch.  */
    1391                 :        1823 :               continue;
    1392                 :             :             }
    1393                 :             : 
    1394                 :    10168906 :           if (call_stmt
    1395                 :       25951 :               && first_stmt_code != CFN_MASK_LOAD
    1396                 :    10194796 :               && first_stmt_code != CFN_MASK_STORE)
    1397                 :             :             {
    1398                 :       25840 :               if (!is_a <gcall *> (stmts[0]->stmt)
    1399                 :       25840 :                   || !compatible_calls_p (as_a <gcall *> (stmts[0]->stmt),
    1400                 :             :                                           call_stmt))
    1401                 :             :                 {
    1402                 :        8521 :                   if (dump_enabled_p ())
    1403                 :           0 :                     dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1404                 :             :                                      "Build SLP failed: different calls in %G",
    1405                 :             :                                      stmt);
    1406                 :             :                   /* Mismatch.  */
    1407                 :        8521 :                   continue;
    1408                 :             :                 }
    1409                 :             :             }
    1410                 :             : 
    1411                 :     9965892 :           if ((phi_p || gimple_could_trap_p (stmt_info->stmt))
    1412                 :    10802524 :               && (gimple_bb (first_stmt_info->stmt)
    1413                 :      836632 :                   != gimple_bb (stmt_info->stmt)))
    1414                 :             :             {
    1415                 :       28404 :               if (dump_enabled_p ())
    1416                 :           4 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1417                 :             :                                  "Build SLP failed: different BB for PHI "
    1418                 :             :                                  "or possibly trapping operation in %G", stmt);
    1419                 :             :               /* Mismatch.  */
    1420                 :       28404 :               continue;
    1421                 :             :             }
    1422                 :             : 
    1423                 :    10131981 :           if (need_same_oprnds)
    1424                 :             :             {
    1425                 :       51631 :               tree other_op1 = gimple_arg (stmt, 1);
    1426                 :       51631 :               if (!operand_equal_p (first_op1, other_op1, 0))
    1427                 :             :                 {
    1428                 :        6735 :                   if (dump_enabled_p ())
    1429                 :         121 :                     dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1430                 :             :                                      "Build SLP failed: different shift "
    1431                 :             :                                      "arguments in %G", stmt);
    1432                 :             :                   /* Mismatch.  */
    1433                 :        6735 :                   continue;
    1434                 :             :                 }
    1435                 :             :             }
    1436                 :             : 
    1437                 :    10125837 :           if (first_lhs
    1438                 :    10125246 :               && lhs
    1439                 :    10125246 :               && !types_compatible_p (TREE_TYPE (lhs), TREE_TYPE (first_lhs)))
    1440                 :             :             {
    1441                 :         591 :               if (dump_enabled_p ())
    1442                 :           0 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1443                 :             :                                  "Build SLP failed: different vector type "
    1444                 :             :                                  "in %G", stmt);
    1445                 :             :               /* Mismatch.  */
    1446                 :         591 :               continue;
    1447                 :             :             }
    1448                 :             :         }
    1449                 :             : 
    1450                 :             :       /* Grouped store or load.  */
    1451                 :    14292251 :       if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
    1452                 :             :         {
    1453                 :     3690141 :           gcc_assert (ldst_p);
    1454                 :     3690141 :           if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
    1455                 :             :             {
    1456                 :             :               /* Store.  */
    1457                 :     3021510 :               gcc_assert (rhs_code == CFN_MASK_STORE
    1458                 :             :                           || REFERENCE_CLASS_P (lhs)
    1459                 :             :                           || DECL_P (lhs));
    1460                 :             :             }
    1461                 :             :           else
    1462                 :             :             {
    1463                 :             :               /* Load.  */
    1464                 :      668631 :               first_load = DR_GROUP_FIRST_ELEMENT (stmt_info);
    1465                 :      668631 :               if (prev_first_load)
    1466                 :             :                 {
    1467                 :             :                   /* Check that there are no loads from different interleaving
    1468                 :             :                      chains in the same node.  */
    1469                 :      316652 :                   if (prev_first_load != first_load)
    1470                 :             :                     {
    1471                 :       24843 :                       if (dump_enabled_p ())
    1472                 :        1886 :                         dump_printf_loc (MSG_MISSED_OPTIMIZATION,
    1473                 :             :                                          vect_location,
    1474                 :             :                                          "Build SLP failed: different "
    1475                 :             :                                          "interleaving chains in one node %G",
    1476                 :             :                                          stmt);
    1477                 :             :                       /* Mismatch.  */
    1478                 :       24843 :                       continue;
    1479                 :             :                     }
    1480                 :             :                 }
    1481                 :             :               else
    1482                 :             :                 prev_first_load = first_load;
    1483                 :             :            }
    1484                 :             :         }
    1485                 :             :       /* Non-grouped store or load.  */
    1486                 :    10602110 :       else if (ldst_p)
    1487                 :             :         {
    1488                 :      425704 :           if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info))
    1489                 :      251472 :               && rhs_code != CFN_GATHER_LOAD
    1490                 :             :               && rhs_code != CFN_MASK_GATHER_LOAD
    1491                 :             :               && rhs_code != CFN_MASK_LEN_GATHER_LOAD
    1492                 :             :               && rhs_code != CFN_SCATTER_STORE
    1493                 :             :               && rhs_code != CFN_MASK_SCATTER_STORE
    1494                 :             :               && rhs_code != CFN_MASK_LEN_SCATTER_STORE
    1495                 :      251472 :               && !STMT_VINFO_GATHER_SCATTER_P (stmt_info)
    1496                 :             :               /* Not grouped loads are handled as externals for BB
    1497                 :             :                  vectorization.  For loop vectorization we can handle
    1498                 :             :                  splats the same we handle single element interleaving.  */
    1499                 :      670768 :               && (is_a <bb_vec_info> (vinfo)
    1500                 :      245064 :                   || stmt_info != first_stmt_info))
    1501                 :             :             {
    1502                 :             :               /* Not grouped load.  */
    1503                 :       15655 :               if (dump_enabled_p ())
    1504                 :         138 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1505                 :             :                                  "Build SLP failed: not grouped load %G", stmt);
    1506                 :             : 
    1507                 :       15655 :               if (i != 0)
    1508                 :       15655 :                 continue;
    1509                 :             :               /* Fatal mismatch.  */
    1510                 :           0 :               matches[0] = false;
    1511                 :           0 :               return false;
    1512                 :             :             }
    1513                 :             :         }
    1514                 :             :       /* Not memory operation.  */
    1515                 :             :       else
    1516                 :             :         {
    1517                 :    10176406 :           if (!phi_p
    1518                 :     9505407 :               && rhs_code.is_tree_code ()
    1519                 :     9461703 :               && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_binary
    1520                 :     1253440 :               && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_unary
    1521                 :      722793 :               && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_expression
    1522                 :      663959 :               && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_comparison
    1523                 :       66943 :               && rhs_code != VIEW_CONVERT_EXPR
    1524                 :             :               && rhs_code != CALL_EXPR
    1525                 :    10176406 :               && rhs_code != BIT_FIELD_REF)
    1526                 :             :             {
    1527                 :       19815 :               if (dump_enabled_p ())
    1528                 :           9 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1529                 :             :                                  "Build SLP failed: operation unsupported %G",
    1530                 :             :                                  stmt);
    1531                 :       19815 :               if (is_a <bb_vec_info> (vinfo) && i != 0)
    1532                 :           0 :                 continue;
    1533                 :             :               /* Fatal mismatch.  */
    1534                 :       19815 :               matches[0] = false;
    1535                 :       19815 :               return false;
    1536                 :             :             }
    1537                 :             : 
    1538                 :    10156591 :           if (rhs_code == COND_EXPR)
    1539                 :             :             {
    1540                 :       56516 :               tree cond_expr = gimple_assign_rhs1 (stmt);
    1541                 :       56516 :               enum tree_code cond_code = TREE_CODE (cond_expr);
    1542                 :       56516 :               enum tree_code swap_code = ERROR_MARK;
    1543                 :       56516 :               enum tree_code invert_code = ERROR_MARK;
    1544                 :             : 
    1545                 :       56516 :               if (i == 0)
    1546                 :       47641 :                 first_cond_code = TREE_CODE (cond_expr);
    1547                 :        8875 :               else if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
    1548                 :             :                 {
    1549                 :           0 :                   bool honor_nans = HONOR_NANS (TREE_OPERAND (cond_expr, 0));
    1550                 :           0 :                   swap_code = swap_tree_comparison (cond_code);
    1551                 :           0 :                   invert_code = invert_tree_comparison (cond_code, honor_nans);
    1552                 :             :                 }
    1553                 :             : 
    1554                 :       56516 :               if (first_cond_code == cond_code)
    1555                 :             :                 ;
    1556                 :             :               /* Isomorphic can be achieved by swapping.  */
    1557                 :           0 :               else if (first_cond_code == swap_code)
    1558                 :           0 :                 swap[i] = 1;
    1559                 :             :               /* Isomorphic can be achieved by inverting.  */
    1560                 :           0 :               else if (first_cond_code == invert_code)
    1561                 :           0 :                 swap[i] = 2;
    1562                 :             :               else
    1563                 :             :                 {
    1564                 :           0 :                   if (dump_enabled_p ())
    1565                 :           0 :                     dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1566                 :             :                                      "Build SLP failed: different"
    1567                 :             :                                      " operation %G", stmt);
    1568                 :             :                   /* Mismatch.  */
    1569                 :           0 :                   continue;
    1570                 :             :                 }
    1571                 :             :             }
    1572                 :             : 
    1573                 :    10156591 :           if (rhs_code.is_tree_code ()
    1574                 :     9441888 :               && TREE_CODE_CLASS ((tree_code)rhs_code) == tcc_comparison
    1575                 :    10753607 :               && (swap_tree_comparison ((tree_code)first_stmt_code)
    1576                 :      597016 :                   == (tree_code)rhs_code))
    1577                 :      399183 :             swap[i] = 1;
    1578                 :             :         }
    1579                 :             : 
    1580                 :    14231938 :       matches[i] = true;
    1581                 :             :     }
    1582                 :             : 
    1583                 :    18427980 :   for (i = 0; i < group_size; ++i)
    1584                 :    15033491 :     if (!matches[i])
    1585                 :             :       return false;
    1586                 :             : 
    1587                 :             :   /* If we allowed a two-operation SLP node verify the target can cope
    1588                 :             :      with the permute we are going to use.  */
    1589                 :     3394489 :   if (alt_stmt_code != ERROR_MARK
    1590                 :     3394489 :       && (!alt_stmt_code.is_tree_code ()
    1591                 :       53716 :           || (TREE_CODE_CLASS (tree_code (alt_stmt_code)) != tcc_reference
    1592                 :       53716 :               && TREE_CODE_CLASS (tree_code (alt_stmt_code)) != tcc_comparison)))
    1593                 :             :     {
    1594                 :       12123 :       *two_operators = true;
    1595                 :             :     }
    1596                 :             : 
    1597                 :     3394489 :   if (maybe_soft_fail)
    1598                 :             :     {
    1599                 :      130058 :       unsigned HOST_WIDE_INT const_nunits;
    1600                 :      130058 :       if (!TYPE_VECTOR_SUBPARTS
    1601                 :      130058 :             (soft_fail_nunits_vectype).is_constant (&const_nunits)
    1602                 :      130058 :           || const_nunits > group_size)
    1603                 :           0 :         matches[0] = false;
    1604                 :             :       else
    1605                 :             :         {
    1606                 :             :           /* With constant vector elements simulate a mismatch at the
    1607                 :             :              point we need to split.  */
    1608                 :      130058 :           unsigned tail = group_size & (const_nunits - 1);
    1609                 :      130058 :           memset (&matches[group_size - tail], 0, sizeof (bool) * tail);
    1610                 :             :         }
    1611                 :      130058 :       return false;
    1612                 :             :     }
    1613                 :             : 
    1614                 :             :   return true;
    1615                 :             : }
    1616                 :             : 
    1617                 :             : /* Traits for the hash_set to record failed SLP builds for a stmt set.
    1618                 :             :    Note we never remove apart from at destruction time so we do not
    1619                 :             :    need a special value for deleted that differs from empty.  */
    1620                 :             : struct bst_traits
    1621                 :             : {
    1622                 :             :   typedef vec <stmt_vec_info> value_type;
    1623                 :             :   typedef vec <stmt_vec_info> compare_type;
    1624                 :             :   static inline hashval_t hash (value_type);
    1625                 :             :   static inline bool equal (value_type existing, value_type candidate);
    1626                 :   378744795 :   static inline bool is_empty (value_type x) { return !x.exists (); }
    1627                 :    86676788 :   static inline bool is_deleted (value_type x) { return !x.exists (); }
    1628                 :             :   static const bool empty_zero_p = true;
    1629                 :           0 :   static inline void mark_empty (value_type &x) { x.release (); }
    1630                 :             :   static inline void mark_deleted (value_type &x) { x.release (); }
    1631                 :     7234441 :   static inline void remove (value_type &x) { x.release (); }
    1632                 :             : };
    1633                 :             : inline hashval_t
    1634                 :    75314664 : bst_traits::hash (value_type x)
    1635                 :             : {
    1636                 :    75314664 :   inchash::hash h;
    1637                 :   372967167 :   for (unsigned i = 0; i < x.length (); ++i)
    1638                 :   297652503 :     h.add_int (x[i] ? gimple_uid (x[i]->stmt) : -1);
    1639                 :    75314664 :   return h.end ();
    1640                 :             : }
    1641                 :             : inline bool
    1642                 :    66433938 : bst_traits::equal (value_type existing, value_type candidate)
    1643                 :             : {
    1644                 :   199301814 :   if (existing.length () != candidate.length ())
    1645                 :             :     return false;
    1646                 :    69222483 :   for (unsigned i = 0; i < existing.length (); ++i)
    1647                 :    65886230 :     if (existing[i] != candidate[i])
    1648                 :             :       return false;
    1649                 :             :   return true;
    1650                 :             : }
    1651                 :             : 
    1652                 :             : typedef hash_map <vec <stmt_vec_info>, slp_tree,
    1653                 :             :                   simple_hashmap_traits <bst_traits, slp_tree> >
    1654                 :             :   scalar_stmts_to_slp_tree_map_t;
    1655                 :             : 
    1656                 :             : /* Release BST_MAP.  */
    1657                 :             : 
    1658                 :             : static void
    1659                 :     1391188 : release_scalar_stmts_to_slp_tree_map (scalar_stmts_to_slp_tree_map_t *bst_map)
    1660                 :             : {
    1661                 :             :   /* The map keeps a reference on SLP nodes built, release that.  */
    1662                 :     8625629 :   for (scalar_stmts_to_slp_tree_map_t::iterator it = bst_map->begin ();
    1663                 :    15860070 :        it != bst_map->end (); ++it)
    1664                 :     7234441 :     if ((*it).second)
    1665                 :     7234441 :       vect_free_slp_tree ((*it).second);
    1666                 :     1391188 :   delete bst_map;
    1667                 :     1391188 : }
    1668                 :             : 
    1669                 :             : /* ???  This was std::pair<std::pair<tree_code, vect_def_type>, tree>
    1670                 :             :    but then vec::insert does memmove and that's not compatible with
    1671                 :             :    std::pair.  */
    1672                 :             : struct chain_op_t
    1673                 :             : {
    1674                 :     3328529 :   chain_op_t (tree_code code_, vect_def_type dt_, tree op_)
    1675                 :     3328529 :       : code (code_), dt (dt_), op (op_) {}
    1676                 :             :   tree_code code;
    1677                 :             :   vect_def_type dt;
    1678                 :             :   tree op;
    1679                 :             : };
    1680                 :             : 
    1681                 :             : /* Comparator for sorting associatable chains.  */
    1682                 :             : 
    1683                 :             : static int
    1684                 :     7964241 : dt_sort_cmp (const void *op1_, const void *op2_, void *)
    1685                 :             : {
    1686                 :     7964241 :   auto *op1 = (const chain_op_t *) op1_;
    1687                 :     7964241 :   auto *op2 = (const chain_op_t *) op2_;
    1688                 :     7964241 :   if (op1->dt != op2->dt)
    1689                 :      959392 :     return (int)op1->dt - (int)op2->dt;
    1690                 :     7004849 :   return (int)op1->code - (int)op2->code;
    1691                 :             : }
    1692                 :             : 
    1693                 :             : /* Linearize the associatable expression chain at START with the
    1694                 :             :    associatable operation CODE (where PLUS_EXPR also allows MINUS_EXPR),
    1695                 :             :    filling CHAIN with the result and using WORKLIST as intermediate storage.
    1696                 :             :    CODE_STMT and ALT_CODE_STMT are filled with the first stmt using CODE
    1697                 :             :    or MINUS_EXPR.  *CHAIN_STMTS if not NULL is filled with all computation
    1698                 :             :    stmts, starting with START.  */
    1699                 :             : 
    1700                 :             : static void
    1701                 :     1485744 : vect_slp_linearize_chain (vec_info *vinfo,
    1702                 :             :                           vec<std::pair<tree_code, gimple *> > &worklist,
    1703                 :             :                           vec<chain_op_t> &chain,
    1704                 :             :                           enum tree_code code, gimple *start,
    1705                 :             :                           gimple *&code_stmt, gimple *&alt_code_stmt,
    1706                 :             :                           vec<gimple *> *chain_stmts)
    1707                 :             : {
    1708                 :             :   /* For each lane linearize the addition/subtraction (or other
    1709                 :             :      uniform associatable operation) expression tree.  */
    1710                 :     1485744 :   worklist.safe_push (std::make_pair (code, start));
    1711                 :     3328529 :   while (!worklist.is_empty ())
    1712                 :             :     {
    1713                 :     1842785 :       auto entry = worklist.pop ();
    1714                 :     1842785 :       gassign *stmt = as_a <gassign *> (entry.second);
    1715                 :     1842785 :       enum tree_code in_code = entry.first;
    1716                 :     3685570 :       enum tree_code this_code = gimple_assign_rhs_code (stmt);
    1717                 :             :       /* Pick some stmts suitable for SLP_TREE_REPRESENTATIVE.  */
    1718                 :     1842785 :       if (!code_stmt
    1719                 :     1842785 :           && gimple_assign_rhs_code (stmt) == code)
    1720                 :     1255303 :         code_stmt = stmt;
    1721                 :      587482 :       else if (!alt_code_stmt
    1722                 :      587482 :                && gimple_assign_rhs_code (stmt) == MINUS_EXPR)
    1723                 :      301609 :         alt_code_stmt = stmt;
    1724                 :     1842785 :       if (chain_stmts)
    1725                 :     1836618 :         chain_stmts->safe_push (stmt);
    1726                 :     5528355 :       for (unsigned opnum = 1; opnum <= 2; ++opnum)
    1727                 :             :         {
    1728                 :     3685570 :           tree op = gimple_op (stmt, opnum);
    1729                 :     3685570 :           vect_def_type dt;
    1730                 :     3685570 :           stmt_vec_info def_stmt_info;
    1731                 :     3685570 :           bool res = vect_is_simple_use (op, vinfo, &dt, &def_stmt_info);
    1732                 :     3685570 :           gcc_assert (res);
    1733                 :     3685570 :           if (dt == vect_internal_def
    1734                 :     3685570 :               && is_pattern_stmt_p (def_stmt_info))
    1735                 :         998 :             op = gimple_get_lhs (def_stmt_info->stmt);
    1736                 :     3685570 :           gimple *use_stmt;
    1737                 :     3685570 :           use_operand_p use_p;
    1738                 :     3685570 :           if (dt == vect_internal_def
    1739                 :     3449473 :               && single_imm_use (op, &use_p, &use_stmt)
    1740                 :     2169800 :               && is_gimple_assign (def_stmt_info->stmt)
    1741                 :     5683805 :               && (gimple_assign_rhs_code (def_stmt_info->stmt) == code
    1742                 :     1665105 :                   || (code == PLUS_EXPR
    1743                 :      793517 :                       && (gimple_assign_rhs_code (def_stmt_info->stmt)
    1744                 :             :                           == MINUS_EXPR))))
    1745                 :             :             {
    1746                 :      357041 :               tree_code op_def_code = this_code;
    1747                 :      357041 :               if (op_def_code == MINUS_EXPR && opnum == 1)
    1748                 :       53134 :                 op_def_code = PLUS_EXPR;
    1749                 :      357041 :               if (in_code == MINUS_EXPR)
    1750                 :         193 :                 op_def_code = op_def_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
    1751                 :      357041 :               worklist.safe_push (std::make_pair (op_def_code,
    1752                 :      357041 :                                                   def_stmt_info->stmt));
    1753                 :             :             }
    1754                 :             :           else
    1755                 :             :             {
    1756                 :     3328529 :               tree_code op_def_code = this_code;
    1757                 :     3328529 :               if (op_def_code == MINUS_EXPR && opnum == 1)
    1758                 :      252417 :                 op_def_code = PLUS_EXPR;
    1759                 :     3328529 :               if (in_code == MINUS_EXPR)
    1760                 :        4227 :                 op_def_code = op_def_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
    1761                 :     3328529 :               chain.safe_push (chain_op_t (op_def_code, dt, op));
    1762                 :             :             }
    1763                 :             :         }
    1764                 :             :     }
    1765                 :     1485744 : }
    1766                 :             : 
    1767                 :             : static slp_tree
    1768                 :             : vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
    1769                 :             :                        vec<stmt_vec_info> stmts, unsigned int group_size,
    1770                 :             :                        poly_uint64 *max_nunits,
    1771                 :             :                        bool *matches, unsigned *limit, unsigned *tree_size,
    1772                 :             :                        scalar_stmts_to_slp_tree_map_t *bst_map);
    1773                 :             : 
    1774                 :             : static slp_tree
    1775                 :     4885685 : vect_build_slp_tree (vec_info *vinfo,
    1776                 :             :                      vec<stmt_vec_info> stmts, unsigned int group_size,
    1777                 :             :                      poly_uint64 *max_nunits,
    1778                 :             :                      bool *matches, unsigned *limit, unsigned *tree_size,
    1779                 :             :                      scalar_stmts_to_slp_tree_map_t *bst_map)
    1780                 :             : {
    1781                 :     4885685 :   if (slp_tree *leader = bst_map->get (stmts))
    1782                 :             :     {
    1783                 :      345135 :       if (dump_enabled_p ())
    1784                 :       18721 :         dump_printf_loc (MSG_NOTE, vect_location, "re-using %sSLP tree %p\n",
    1785                 :       18721 :                          !(*leader)->failed ? "" : "failed ",
    1786                 :             :                          (void *) *leader);
    1787                 :      345135 :       if (!(*leader)->failed)
    1788                 :             :         {
    1789                 :      299123 :           SLP_TREE_REF_COUNT (*leader)++;
    1790                 :      299123 :           vect_update_max_nunits (max_nunits, (*leader)->max_nunits);
    1791                 :      299123 :           stmts.release ();
    1792                 :      299123 :           return *leader;
    1793                 :             :         }
    1794                 :       46012 :       memcpy (matches, (*leader)->failed, sizeof (bool) * group_size);
    1795                 :       46012 :       return NULL;
    1796                 :             :     }
    1797                 :             : 
    1798                 :             :   /* Single-lane SLP doesn't have the chance of run-away, do not account
    1799                 :             :      it to the limit.  */
    1800                 :     4540550 :   if (stmts.length () > 1)
    1801                 :             :     {
    1802                 :     2984284 :       if (*limit == 0)
    1803                 :             :         {
    1804                 :        1347 :           if (dump_enabled_p ())
    1805                 :          48 :             dump_printf_loc (MSG_NOTE, vect_location,
    1806                 :             :                              "SLP discovery limit exceeded\n");
    1807                 :        1347 :           memset (matches, 0, sizeof (bool) * group_size);
    1808                 :        1347 :           return NULL;
    1809                 :             :         }
    1810                 :     2982937 :       --*limit;
    1811                 :             :     }
    1812                 :             : 
    1813                 :             :   /* Seed the bst_map with a stub node to be filled by vect_build_slp_tree_2
    1814                 :             :      so we can pick up backedge destinations during discovery.  */
    1815                 :     4539203 :   slp_tree res = new _slp_tree;
    1816                 :     4539203 :   SLP_TREE_DEF_TYPE (res) = vect_internal_def;
    1817                 :     4539203 :   SLP_TREE_SCALAR_STMTS (res) = stmts;
    1818                 :     4539203 :   bst_map->put (stmts.copy (), res);
    1819                 :             : 
    1820                 :     4539203 :   if (dump_enabled_p ())
    1821                 :      150439 :     dump_printf_loc (MSG_NOTE, vect_location,
    1822                 :             :                      "starting SLP discovery for node %p\n", (void *) res);
    1823                 :             : 
    1824                 :     4539203 :   poly_uint64 this_max_nunits = 1;
    1825                 :     4539203 :   slp_tree res_ = vect_build_slp_tree_2 (vinfo, res, stmts, group_size,
    1826                 :             :                                         &this_max_nunits,
    1827                 :             :                                         matches, limit, tree_size, bst_map);
    1828                 :     4539203 :   if (!res_)
    1829                 :             :     {
    1830                 :     1856591 :       if (dump_enabled_p ())
    1831                 :        8453 :         dump_printf_loc (MSG_NOTE, vect_location,
    1832                 :             :                          "SLP discovery for node %p failed\n", (void *) res);
    1833                 :             :       /* Mark the node invalid so we can detect those when still in use
    1834                 :             :          as backedge destinations.  */
    1835                 :     1856591 :       SLP_TREE_SCALAR_STMTS (res) = vNULL;
    1836                 :     1856591 :       SLP_TREE_DEF_TYPE (res) = vect_uninitialized_def;
    1837                 :     1856591 :       res->failed = XNEWVEC (bool, group_size);
    1838                 :     1856591 :       if (flag_checking)
    1839                 :             :         {
    1840                 :             :           unsigned i;
    1841                 :     3541086 :           for (i = 0; i < group_size; ++i)
    1842                 :     3541086 :             if (!matches[i])
    1843                 :             :               break;
    1844                 :     1856591 :           gcc_assert (i < group_size);
    1845                 :             :         }
    1846                 :     1856591 :       memcpy (res->failed, matches, sizeof (bool) * group_size);
    1847                 :             :     }
    1848                 :             :   else
    1849                 :             :     {
    1850                 :     2682612 :       if (dump_enabled_p ())
    1851                 :      141986 :         dump_printf_loc (MSG_NOTE, vect_location,
    1852                 :             :                          "SLP discovery for node %p succeeded\n",
    1853                 :             :                          (void *) res);
    1854                 :     2682612 :       gcc_assert (res_ == res);
    1855                 :     2682612 :       res->max_nunits = this_max_nunits;
    1856                 :     2682612 :       vect_update_max_nunits (max_nunits, this_max_nunits);
    1857                 :             :       /* Keep a reference for the bst_map use.  */
    1858                 :     2682612 :       SLP_TREE_REF_COUNT (res)++;
    1859                 :             :     }
    1860                 :             :   return res_;
    1861                 :             : }
    1862                 :             : 
    1863                 :             : /* Helper for building an associated SLP node chain.  */
    1864                 :             : 
    1865                 :             : static void
    1866                 :         165 : vect_slp_build_two_operator_nodes (slp_tree perm, tree vectype,
    1867                 :             :                                    slp_tree op0, slp_tree op1,
    1868                 :             :                                    stmt_vec_info oper1, stmt_vec_info oper2,
    1869                 :             :                                    vec<std::pair<unsigned, unsigned> > lperm)
    1870                 :             : {
    1871                 :         165 :   unsigned group_size = SLP_TREE_LANES (op1);
    1872                 :             : 
    1873                 :         165 :   slp_tree child1 = new _slp_tree;
    1874                 :         165 :   SLP_TREE_DEF_TYPE (child1) = vect_internal_def;
    1875                 :         165 :   SLP_TREE_VECTYPE (child1) = vectype;
    1876                 :         165 :   SLP_TREE_LANES (child1) = group_size;
    1877                 :         165 :   SLP_TREE_CHILDREN (child1).create (2);
    1878                 :         165 :   SLP_TREE_CHILDREN (child1).quick_push (op0);
    1879                 :         165 :   SLP_TREE_CHILDREN (child1).quick_push (op1);
    1880                 :         165 :   SLP_TREE_REPRESENTATIVE (child1) = oper1;
    1881                 :             : 
    1882                 :         165 :   slp_tree child2 = new _slp_tree;
    1883                 :         165 :   SLP_TREE_DEF_TYPE (child2) = vect_internal_def;
    1884                 :         165 :   SLP_TREE_VECTYPE (child2) = vectype;
    1885                 :         165 :   SLP_TREE_LANES (child2) = group_size;
    1886                 :         165 :   SLP_TREE_CHILDREN (child2).create (2);
    1887                 :         165 :   SLP_TREE_CHILDREN (child2).quick_push (op0);
    1888                 :         165 :   SLP_TREE_REF_COUNT (op0)++;
    1889                 :         165 :   SLP_TREE_CHILDREN (child2).quick_push (op1);
    1890                 :         165 :   SLP_TREE_REF_COUNT (op1)++;
    1891                 :         165 :   SLP_TREE_REPRESENTATIVE (child2) = oper2;
    1892                 :             : 
    1893                 :         165 :   SLP_TREE_DEF_TYPE (perm) = vect_internal_def;
    1894                 :         165 :   SLP_TREE_CODE (perm) = VEC_PERM_EXPR;
    1895                 :         165 :   SLP_TREE_VECTYPE (perm) = vectype;
    1896                 :         165 :   SLP_TREE_LANES (perm) = group_size;
    1897                 :             :   /* ???  We should set this NULL but that's not expected.  */
    1898                 :         165 :   SLP_TREE_REPRESENTATIVE (perm) = oper1;
    1899                 :         165 :   SLP_TREE_LANE_PERMUTATION (perm) = lperm;
    1900                 :         165 :   SLP_TREE_CHILDREN (perm).quick_push (child1);
    1901                 :         165 :   SLP_TREE_CHILDREN (perm).quick_push (child2);
    1902                 :         165 : }
    1903                 :             : 
    1904                 :             : /* Recursively build an SLP tree starting from NODE.
    1905                 :             :    Fail (and return a value not equal to zero) if def-stmts are not
    1906                 :             :    isomorphic, require data permutation or are of unsupported types of
    1907                 :             :    operation.  Otherwise, return 0.
    1908                 :             :    The value returned is the depth in the SLP tree where a mismatch
    1909                 :             :    was found.  */
    1910                 :             : 
    1911                 :             : static slp_tree
    1912                 :     4539203 : vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
    1913                 :             :                        vec<stmt_vec_info> stmts, unsigned int group_size,
    1914                 :             :                        poly_uint64 *max_nunits,
    1915                 :             :                        bool *matches, unsigned *limit, unsigned *tree_size,
    1916                 :             :                        scalar_stmts_to_slp_tree_map_t *bst_map)
    1917                 :             : {
    1918                 :     4539203 :   unsigned nops, i, this_tree_size = 0;
    1919                 :     4539203 :   poly_uint64 this_max_nunits = *max_nunits;
    1920                 :             : 
    1921                 :     4539203 :   matches[0] = false;
    1922                 :             : 
    1923                 :     4539203 :   stmt_vec_info stmt_info = stmts[0];
    1924                 :     4539203 :   if (!is_a<gcall *> (stmt_info->stmt)
    1925                 :             :       && !is_a<gassign *> (stmt_info->stmt)
    1926                 :             :       && !is_a<gphi *> (stmt_info->stmt))
    1927                 :             :     return NULL;
    1928                 :             : 
    1929                 :     4539102 :   nops = gimple_num_args (stmt_info->stmt);
    1930                 :     4539102 :   if (const int *map = vect_get_operand_map (stmt_info->stmt,
    1931                 :     4539102 :                                              STMT_VINFO_GATHER_SCATTER_P
    1932                 :             :                                                (stmt_info)))
    1933                 :       19008 :     nops = map[0];
    1934                 :             : 
    1935                 :             :   /* If the SLP node is a PHI (induction or reduction), terminate
    1936                 :             :      the recursion.  */
    1937                 :     4539102 :   bool *skip_args = XALLOCAVEC (bool, nops);
    1938                 :     4539102 :   memset (skip_args, 0, sizeof (bool) * nops);
    1939                 :     4539102 :   if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
    1940                 :     1641613 :     if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
    1941                 :             :       {
    1942                 :      231200 :         tree scalar_type = TREE_TYPE (PHI_RESULT (stmt));
    1943                 :      231200 :         tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
    1944                 :             :                                                     group_size);
    1945                 :      231200 :         if (!vect_record_max_nunits (vinfo, stmt_info, group_size, vectype,
    1946                 :             :                                      max_nunits))
    1947                 :             :           return NULL;
    1948                 :             : 
    1949                 :      231200 :         vect_def_type def_type = STMT_VINFO_DEF_TYPE (stmt_info);
    1950                 :      231200 :         if (def_type == vect_induction_def)
    1951                 :             :           {
    1952                 :             :             /* Induction PHIs are not cycles but walk the initial
    1953                 :             :                value.  Only for inner loops through, for outer loops
    1954                 :             :                we need to pick up the value from the actual PHIs
    1955                 :             :                to more easily support peeling and epilogue vectorization.  */
    1956                 :      160178 :             class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
    1957                 :      160178 :             if (!nested_in_vect_loop_p (loop, stmt_info))
    1958                 :      159391 :               skip_args[loop_preheader_edge (loop)->dest_idx] = true;
    1959                 :             :             else
    1960                 :             :               loop = loop->inner;
    1961                 :      160178 :             skip_args[loop_latch_edge (loop)->dest_idx] = true;
    1962                 :             :           }
    1963                 :       71022 :         else if (def_type == vect_reduction_def
    1964                 :             :                  || def_type == vect_double_reduction_def
    1965                 :             :                  || def_type == vect_nested_cycle
    1966                 :       71022 :                  || def_type == vect_first_order_recurrence)
    1967                 :             :           {
    1968                 :             :             /* Else def types have to match.  */
    1969                 :             :             stmt_vec_info other_info;
    1970                 :             :             bool all_same = true;
    1971                 :      141875 :             FOR_EACH_VEC_ELT (stmts, i, other_info)
    1972                 :             :               {
    1973                 :       72084 :                 if (STMT_VINFO_DEF_TYPE (other_info) != def_type)
    1974                 :     1505927 :                   return NULL;
    1975                 :       72084 :                 if (other_info != stmt_info)
    1976                 :         756 :                   all_same = false;
    1977                 :             :               }
    1978                 :       69791 :             class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
    1979                 :             :             /* Reduction initial values are not explicitely represented.  */
    1980                 :       69791 :             if (def_type != vect_first_order_recurrence
    1981                 :       69791 :                 && gimple_bb (stmt_info->stmt) == loop->header)
    1982                 :       66828 :               skip_args[loop_preheader_edge (loop)->dest_idx] = true;
    1983                 :             :             /* Reduction chain backedge defs are filled manually.
    1984                 :             :                ???  Need a better way to identify a SLP reduction chain PHI.
    1985                 :             :                Or a better overall way to SLP match those.  */
    1986                 :       69791 :             if (stmts.length () > 1
    1987                 :       69791 :                 && all_same && def_type == vect_reduction_def)
    1988                 :         366 :               skip_args[loop_latch_edge (loop)->dest_idx] = true;
    1989                 :             :           }
    1990                 :        1231 :         else if (def_type != vect_internal_def)
    1991                 :             :           return NULL;
    1992                 :             :       }
    1993                 :             : 
    1994                 :             : 
    1995                 :     4539102 :   bool two_operators = false;
    1996                 :     4539102 :   unsigned char *swap = XALLOCAVEC (unsigned char, group_size);
    1997                 :     4539102 :   tree vectype = NULL_TREE;
    1998                 :     4539102 :   if (!vect_build_slp_tree_1 (vinfo, swap, stmts, group_size,
    1999                 :             :                               &this_max_nunits, matches, &two_operators,
    2000                 :             :                               &vectype))
    2001                 :             :     return NULL;
    2002                 :             : 
    2003                 :             :   /* If the SLP node is a load, terminate the recursion unless masked.  */
    2004                 :     3264431 :   if (STMT_VINFO_DATA_REF (stmt_info)
    2005                 :     1581719 :       && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
    2006                 :             :     {
    2007                 :      506459 :       if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
    2008                 :             :         gcc_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
    2009                 :             :       else
    2010                 :             :         {
    2011                 :      500241 :           *max_nunits = this_max_nunits;
    2012                 :      500241 :           (*tree_size)++;
    2013                 :      500241 :           node = vect_create_new_slp_node (node, stmts, 0);
    2014                 :      500241 :           SLP_TREE_VECTYPE (node) = vectype;
    2015                 :             :           /* And compute the load permutation.  Whether it is actually
    2016                 :             :              a permutation depends on the unrolling factor which is
    2017                 :             :              decided later.  */
    2018                 :      500241 :           vec<unsigned> load_permutation;
    2019                 :      500241 :           int j;
    2020                 :      500241 :           stmt_vec_info load_info;
    2021                 :      500241 :           load_permutation.create (group_size);
    2022                 :      500241 :           stmt_vec_info first_stmt_info
    2023                 :      500241 :             = STMT_VINFO_GROUPED_ACCESS (stmt_info)
    2024                 :      500241 :               ? DR_GROUP_FIRST_ELEMENT (stmt_info) : stmt_info;
    2025                 :      500241 :           bool any_permute = false;
    2026                 :     1357754 :           FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
    2027                 :             :             {
    2028                 :      857513 :               int load_place;
    2029                 :      857513 :               if (! load_info)
    2030                 :             :                 {
    2031                 :       70473 :                   if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
    2032                 :             :                     load_place = j;
    2033                 :             :                   else
    2034                 :             :                     load_place = 0;
    2035                 :             :                 }
    2036                 :      787040 :               else if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
    2037                 :      563202 :                 load_place = vect_get_place_in_interleaving_chain
    2038                 :      563202 :                     (load_info, first_stmt_info);
    2039                 :             :               else
    2040                 :             :                 load_place = 0;
    2041                 :      633675 :               gcc_assert (load_place != -1);
    2042                 :      857513 :               any_permute |= load_place != j;
    2043                 :      857513 :               load_permutation.quick_push (load_place);
    2044                 :             :             }
    2045                 :             : 
    2046                 :      500241 :           if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
    2047                 :             :             {
    2048                 :        2413 :               gcc_assert (gimple_call_internal_p (stmt, IFN_MASK_LOAD));
    2049                 :        2413 :               bool has_gaps = false;
    2050                 :        2413 :               if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
    2051                 :         217 :                 for (stmt_vec_info si = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
    2052                 :         326 :                      si; si = DR_GROUP_NEXT_ELEMENT (si))
    2053                 :         109 :                   if (DR_GROUP_GAP (si) != 1)
    2054                 :          24 :                     has_gaps = true;
    2055                 :             :               /* We cannot handle permuted masked loads directly, see
    2056                 :             :                  PR114375.  We cannot handle strided masked loads or masked
    2057                 :             :                  loads with gaps unless the mask is uniform.  */
    2058                 :        2413 :               if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
    2059                 :         217 :                    && (DR_GROUP_GAP (first_stmt_info) != 0
    2060                 :          91 :                        || (has_gaps
    2061                 :          24 :                            && STMT_VINFO_SLP_VECT_ONLY (first_stmt_info))))
    2062                 :        4676 :                   || STMT_VINFO_STRIDED_P (stmt_info))
    2063                 :             :                 {
    2064                 :         176 :                   load_permutation.release ();
    2065                 :         176 :                   matches[0] = false;
    2066                 :      498042 :                   return NULL;
    2067                 :             :                 }
    2068                 :             : 
    2069                 :             :               /* For permuted masked loads do an unpermuted masked load of
    2070                 :             :                  the whole group followed by a SLP permute node.  */
    2071                 :        2237 :               if (any_permute
    2072                 :        2237 :                   || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
    2073                 :          43 :                       && DR_GROUP_SIZE (first_stmt_info) != group_size))
    2074                 :             :                 {
    2075                 :             :                   /* Discover the whole unpermuted load.  */
    2076                 :          38 :                   vec<stmt_vec_info> stmts2;
    2077                 :          38 :                   unsigned dr_group_size = STMT_VINFO_GROUPED_ACCESS (stmt_info)
    2078                 :          70 :                       ? DR_GROUP_SIZE (first_stmt_info) : 1;
    2079                 :          38 :                   stmts2.create (dr_group_size);
    2080                 :          38 :                   stmts2.quick_grow_cleared (dr_group_size);
    2081                 :          38 :                   unsigned i = 0;
    2082                 :          38 :                   for (stmt_vec_info si = first_stmt_info;
    2083                 :         120 :                        si; si = DR_GROUP_NEXT_ELEMENT (si))
    2084                 :             :                     {
    2085                 :          82 :                       if (si != first_stmt_info)
    2086                 :          44 :                         for (unsigned k = 1; k < DR_GROUP_GAP (si); ++k)
    2087                 :           0 :                           stmts2[i++] = NULL;
    2088                 :          82 :                       stmts2[i++] = si;
    2089                 :             :                     }
    2090                 :          38 :                   bool *matches2 = XALLOCAVEC (bool, dr_group_size);
    2091                 :          38 :                   slp_tree unperm_load
    2092                 :          38 :                     = vect_build_slp_tree (vinfo, stmts2, dr_group_size,
    2093                 :             :                                            &this_max_nunits, matches2, limit,
    2094                 :          38 :                                            &this_tree_size, bst_map);
    2095                 :             :                   /* When we are able to do the full masked load emit that
    2096                 :             :                      followed by 'node' being the desired final permutation.  */
    2097                 :          38 :                   if (unperm_load)
    2098                 :             :                     {
    2099                 :          28 :                       gcc_assert
    2100                 :             :                         (!SLP_TREE_LOAD_PERMUTATION (unperm_load).exists ());
    2101                 :          28 :                       lane_permutation_t lperm;
    2102                 :          28 :                       lperm.create (group_size);
    2103                 :          68 :                       for (unsigned j = 0; j < load_permutation.length (); ++j)
    2104                 :          40 :                         lperm.quick_push
    2105                 :          40 :                           (std::make_pair (0, load_permutation[j]));
    2106                 :          28 :                       SLP_TREE_CODE (node) = VEC_PERM_EXPR;
    2107                 :          28 :                       SLP_TREE_CHILDREN (node).safe_push (unperm_load);
    2108                 :          28 :                       SLP_TREE_LANE_PERMUTATION (node) = lperm;
    2109                 :          28 :                       load_permutation.release ();
    2110                 :          28 :                       return node;
    2111                 :             :                     }
    2112                 :          10 :                   stmts2.release ();
    2113                 :          10 :                   load_permutation.release ();
    2114                 :          10 :                   matches[0] = false;
    2115                 :          10 :                   return NULL;
    2116                 :             :                 }
    2117                 :        2199 :               load_permutation.release ();
    2118                 :             :             }
    2119                 :             :           else
    2120                 :             :             {
    2121                 :      497828 :               if (!any_permute
    2122                 :      425892 :                   && STMT_VINFO_GROUPED_ACCESS (stmt_info)
    2123                 :      707369 :                   && group_size == DR_GROUP_SIZE (first_stmt_info))
    2124                 :      140990 :                 load_permutation.release ();
    2125                 :      497828 :               SLP_TREE_LOAD_PERMUTATION (node) = load_permutation;
    2126                 :      497828 :               return node;
    2127                 :             :             }
    2128                 :             :         }
    2129                 :             :     }
    2130                 :     2757972 :   else if (gimple_assign_single_p (stmt_info->stmt)
    2131                 :     2155332 :            && !gimple_vuse (stmt_info->stmt)
    2132                 :     2765581 :            && gimple_assign_rhs_code (stmt_info->stmt) == BIT_FIELD_REF)
    2133                 :             :     {
    2134                 :             :       /* vect_build_slp_tree_2 determined all BIT_FIELD_REFs reference
    2135                 :             :          the same SSA name vector of a compatible type to vectype.  */
    2136                 :        2323 :       vec<std::pair<unsigned, unsigned> > lperm = vNULL;
    2137                 :        2323 :       tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0);
    2138                 :        2323 :       stmt_vec_info estmt_info;
    2139                 :        7641 :       FOR_EACH_VEC_ELT (stmts, i, estmt_info)
    2140                 :             :         {
    2141                 :        5321 :           gassign *estmt = as_a <gassign *> (estmt_info->stmt);
    2142                 :        5321 :           tree bfref = gimple_assign_rhs1 (estmt);
    2143                 :        5321 :           HOST_WIDE_INT lane;
    2144                 :        5321 :           if (!known_eq (bit_field_size (bfref),
    2145                 :             :                          tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (vectype))))
    2146                 :       10639 :               || !constant_multiple_p (bit_field_offset (bfref),
    2147                 :        5318 :                                        bit_field_size (bfref), &lane))
    2148                 :             :             {
    2149                 :           3 :               lperm.release ();
    2150                 :           3 :               matches[0] = false;
    2151                 :           3 :               return NULL;
    2152                 :             :             }
    2153                 :        5318 :           lperm.safe_push (std::make_pair (0, (unsigned)lane));
    2154                 :             :         }
    2155                 :        2320 :       slp_tree vnode = vect_create_new_slp_node (vNULL);
    2156                 :        2320 :       if (operand_equal_p (TYPE_SIZE (vectype), TYPE_SIZE (TREE_TYPE (vec))))
    2157                 :             :         /* ???  We record vectype here but we hide eventually necessary
    2158                 :             :            punning and instead rely on code generation to materialize
    2159                 :             :            VIEW_CONVERT_EXPRs as necessary.  We instead should make
    2160                 :             :            this explicit somehow.  */
    2161                 :         643 :         SLP_TREE_VECTYPE (vnode) = vectype;
    2162                 :             :       else
    2163                 :             :         {
    2164                 :             :           /* For different size but compatible elements we can still
    2165                 :             :              use VEC_PERM_EXPR without punning.  */
    2166                 :        1677 :           gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec))
    2167                 :             :                       && types_compatible_p (TREE_TYPE (vectype),
    2168                 :             :                                              TREE_TYPE (TREE_TYPE (vec))));
    2169                 :        1677 :           SLP_TREE_VECTYPE (vnode) = TREE_TYPE (vec);
    2170                 :             :         }
    2171                 :        2320 :       auto nunits = TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (vnode));
    2172                 :        2320 :       unsigned HOST_WIDE_INT const_nunits;
    2173                 :        2320 :       if (nunits.is_constant (&const_nunits))
    2174                 :        2320 :         SLP_TREE_LANES (vnode) = const_nunits;
    2175                 :        2320 :       SLP_TREE_VEC_DEFS (vnode).safe_push (vec);
    2176                 :             :       /* We are always building a permutation node even if it is an identity
    2177                 :             :          permute to shield the rest of the vectorizer from the odd node
    2178                 :             :          representing an actual vector without any scalar ops.
    2179                 :             :          ???  We could hide it completely with making the permute node
    2180                 :             :          external?  */
    2181                 :        2320 :       node = vect_create_new_slp_node (node, stmts, 1);
    2182                 :        2320 :       SLP_TREE_CODE (node) = VEC_PERM_EXPR;
    2183                 :        2320 :       SLP_TREE_LANE_PERMUTATION (node) = lperm;
    2184                 :        2320 :       SLP_TREE_VECTYPE (node) = vectype;
    2185                 :        2320 :       SLP_TREE_CHILDREN (node).quick_push (vnode);
    2186                 :        2320 :       return node;
    2187                 :             :     }
    2188                 :             :   /* When discovery reaches an associatable operation see whether we can
    2189                 :             :      improve that to match up lanes in a way superior to the operand
    2190                 :             :      swapping code which at most looks at two defs.
    2191                 :             :      ???  For BB vectorization we cannot do the brute-force search
    2192                 :             :      for matching as we can succeed by means of builds from scalars
    2193                 :             :      and have no good way to "cost" one build against another.  */
    2194                 :     2755649 :   else if (is_a <loop_vec_info> (vinfo)
    2195                 :             :            /* Do not bother for single-lane SLP.  */
    2196                 :     1277214 :            && group_size > 1
    2197                 :             :            /* ???  We don't handle !vect_internal_def defs below.  */
    2198                 :       44907 :            && STMT_VINFO_DEF_TYPE (stmt_info) == vect_internal_def
    2199                 :             :            /* ???  Do not associate a reduction, this will wreck REDUC_IDX
    2200                 :             :               mapping as long as that exists on the stmt_info level.  */
    2201                 :       40042 :            && STMT_VINFO_REDUC_IDX (stmt_info) == -1
    2202                 :       39642 :            && is_gimple_assign (stmt_info->stmt)
    2203                 :       39438 :            && (associative_tree_code (gimple_assign_rhs_code (stmt_info->stmt))
    2204                 :       31773 :                || gimple_assign_rhs_code (stmt_info->stmt) == MINUS_EXPR)
    2205                 :     2765007 :            && ((FLOAT_TYPE_P (vectype) && flag_associative_math)
    2206                 :        7998 :                || (INTEGRAL_TYPE_P (TREE_TYPE (vectype))
    2207                 :        5864 :                    && TYPE_OVERFLOW_WRAPS (TREE_TYPE (vectype)))))
    2208                 :             :     {
    2209                 :             :       /* See if we have a chain of (mixed) adds or subtracts or other
    2210                 :             :          associatable ops.  */
    2211                 :        4517 :       enum tree_code code = gimple_assign_rhs_code (stmt_info->stmt);
    2212                 :        4517 :       if (code == MINUS_EXPR)
    2213                 :         703 :         code = PLUS_EXPR;
    2214                 :        4517 :       stmt_vec_info other_op_stmt_info = NULL;
    2215                 :        4517 :       stmt_vec_info op_stmt_info = NULL;
    2216                 :        4517 :       unsigned chain_len = 0;
    2217                 :        4517 :       auto_vec<chain_op_t> chain;
    2218                 :        4517 :       auto_vec<std::pair<tree_code, gimple *> > worklist;
    2219                 :        4517 :       auto_vec<vec<chain_op_t> > chains (group_size);
    2220                 :        4517 :       auto_vec<slp_tree, 4> children;
    2221                 :        4517 :       bool hard_fail = true;
    2222                 :        5360 :       for (unsigned lane = 0; lane < group_size; ++lane)
    2223                 :             :         {
    2224                 :        5054 :           if (!stmts[lane])
    2225                 :             :             {
    2226                 :             :               /* ???  Below we require lane zero is present.  */
    2227                 :           0 :               if (lane == 0)
    2228                 :             :                 {
    2229                 :             :                   hard_fail = false;
    2230                 :        4211 :                   break;
    2231                 :             :                 }
    2232                 :           0 :               chains.quick_push (vNULL);
    2233                 :           0 :               continue;
    2234                 :             :             }
    2235                 :             :           /* For each lane linearize the addition/subtraction (or other
    2236                 :             :              uniform associatable operation) expression tree.  */
    2237                 :        5054 :           gimple *op_stmt = NULL, *other_op_stmt = NULL;
    2238                 :        5054 :           vect_slp_linearize_chain (vinfo, worklist, chain, code,
    2239                 :        5054 :                                     stmts[lane]->stmt, op_stmt, other_op_stmt,
    2240                 :             :                                     NULL);
    2241                 :        5054 :           if (!op_stmt_info && op_stmt)
    2242                 :        3967 :             op_stmt_info = vinfo->lookup_stmt (op_stmt);
    2243                 :        5054 :           if (!other_op_stmt_info && other_op_stmt)
    2244                 :         727 :             other_op_stmt_info = vinfo->lookup_stmt (other_op_stmt);
    2245                 :        5054 :           if (chain.length () == 2)
    2246                 :             :             {
    2247                 :             :               /* In a chain of just two elements resort to the regular
    2248                 :             :                  operand swapping scheme.  Likewise if we run into a
    2249                 :             :                  length mismatch process regularly as well as we did not
    2250                 :             :                  process the other lanes we cannot report a good hint what
    2251                 :             :                  lanes to try swapping in the parent.  */
    2252                 :             :               hard_fail = false;
    2253                 :             :               break;
    2254                 :             :             }
    2255                 :         843 :           else if (chain_len == 0)
    2256                 :         343 :             chain_len = chain.length ();
    2257                 :        1000 :           else if (chain.length () != chain_len)
    2258                 :             :             {
    2259                 :             :               /* ???  Here we could slip in magic to compensate with
    2260                 :             :                  neutral operands.  */
    2261                 :           0 :               matches[lane] = false;
    2262                 :           0 :               if (lane != group_size - 1)
    2263                 :           0 :                 matches[0] = false;
    2264                 :             :               break;
    2265                 :             :             }
    2266                 :         843 :           chains.quick_push (chain.copy ());
    2267                 :         843 :           chain.truncate (0);
    2268                 :             :         }
    2269                 :        9034 :       if (chains.length () == group_size)
    2270                 :             :         {
    2271                 :             :           /* We cannot yet use SLP_TREE_CODE to communicate the operation.  */
    2272                 :         306 :           if (!op_stmt_info)
    2273                 :             :             {
    2274                 :          15 :               hard_fail = false;
    2275                 :          15 :               goto out;
    2276                 :             :             }
    2277                 :             :           /* Now we have a set of chains with the same length.  */
    2278                 :             :           /* 1. pre-sort according to def_type and operation.  */
    2279                 :        1057 :           for (unsigned lane = 0; lane < group_size; ++lane)
    2280                 :        1532 :             chains[lane].stablesort (dt_sort_cmp, vinfo);
    2281                 :         291 :           if (dump_enabled_p ())
    2282                 :             :             {
    2283                 :         123 :               dump_printf_loc (MSG_NOTE, vect_location,
    2284                 :             :                                "pre-sorted chains of %s\n",
    2285                 :             :                                get_tree_code_name (code));
    2286                 :         522 :               for (unsigned lane = 0; lane < group_size; ++lane)
    2287                 :             :                 {
    2288                 :         399 :                   if (!stmts[lane])
    2289                 :           0 :                     dump_printf (MSG_NOTE, "--");
    2290                 :             :                   else
    2291                 :        1822 :                     for (unsigned opnum = 0; opnum < chain_len; ++opnum)
    2292                 :        2846 :                       dump_printf (MSG_NOTE, "%s %T ",
    2293                 :        1423 :                                    get_tree_code_name (chains[lane][opnum].code),
    2294                 :        1423 :                                    chains[lane][opnum].op);
    2295                 :         399 :                   dump_printf (MSG_NOTE, "\n");
    2296                 :             :                 }
    2297                 :             :             }
    2298                 :             :           /* 2. try to build children nodes, associating as necessary.  */
    2299                 :             :           /* 2a. prepare and perform early checks to avoid eating into
    2300                 :             :              discovery limit unnecessarily.  */
    2301                 :         291 :           vect_def_type *dts = XALLOCAVEC (vect_def_type, chain_len);
    2302                 :        1226 :           for (unsigned n = 0; n < chain_len; ++n)
    2303                 :             :             {
    2304                 :         935 :               vect_def_type dt = chains[0][n].dt;
    2305                 :         935 :               unsigned lane;
    2306                 :        3497 :               for (lane = 0; lane < group_size; ++lane)
    2307                 :        5124 :                 if (stmts[lane] && chains[lane][n].dt != dt)
    2308                 :             :                   {
    2309                 :           0 :                     if (dt == vect_constant_def
    2310                 :           0 :                         && chains[lane][n].dt == vect_external_def)
    2311                 :             :                       dt = vect_external_def;
    2312                 :           0 :                     else if (dt == vect_external_def
    2313                 :           0 :                              && chains[lane][n].dt == vect_constant_def)
    2314                 :             :                       ;
    2315                 :             :                     else
    2316                 :             :                       break;
    2317                 :             :                   }
    2318                 :         935 :               if (lane != group_size)
    2319                 :             :                 {
    2320                 :           0 :                   if (dump_enabled_p ())
    2321                 :           0 :                     dump_printf_loc (MSG_NOTE, vect_location,
    2322                 :             :                                      "giving up on chain due to mismatched "
    2323                 :             :                                      "def types\n");
    2324                 :           0 :                   matches[lane] = false;
    2325                 :           0 :                   if (lane != group_size - 1)
    2326                 :           0 :                     matches[0] = false;
    2327                 :           0 :                   goto out;
    2328                 :             :                 }
    2329                 :         935 :               dts[n] = dt;
    2330                 :         935 :               if (dt == vect_constant_def
    2331                 :         935 :                   || dt == vect_external_def)
    2332                 :             :                 {
    2333                 :             :                   /* Check whether we can build the invariant.  If we can't
    2334                 :             :                      we never will be able to.  */
    2335                 :          78 :                   tree type = TREE_TYPE (chains[0][n].op);
    2336                 :         935 :                   if (!GET_MODE_SIZE (vinfo->vector_mode).is_constant ()
    2337                 :             :                       && (TREE_CODE (type) == BOOLEAN_TYPE
    2338                 :             :                           || !can_duplicate_and_interleave_p (vinfo, group_size,
    2339                 :             :                                                               type)))
    2340                 :             :                     {
    2341                 :             :                       matches[0] = false;
    2342                 :             :                       goto out;
    2343                 :             :                     }
    2344                 :             :                 }
    2345                 :         857 :               else if (dt != vect_internal_def)
    2346                 :             :                 {
    2347                 :             :                   /* Not sure, we might need sth special.
    2348                 :             :                      gcc.dg/vect/pr96854.c,
    2349                 :             :                      gfortran.dg/vect/fast-math-pr37021.f90
    2350                 :             :                      and gfortran.dg/vect/pr61171.f trigger.  */
    2351                 :             :                   /* Soft-fail for now.  */
    2352                 :           0 :                   hard_fail = false;
    2353                 :           0 :                   goto out;
    2354                 :             :                 }
    2355                 :             :             }
    2356                 :             :           /* 2b. do the actual build.  */
    2357                 :        1144 :           for (unsigned n = 0; n < chain_len; ++n)
    2358                 :             :             {
    2359                 :         880 :               vect_def_type dt = dts[n];
    2360                 :         880 :               unsigned lane;
    2361                 :         880 :               if (dt == vect_constant_def
    2362                 :         880 :                   || dt == vect_external_def)
    2363                 :             :                 {
    2364                 :          78 :                   vec<tree> ops;
    2365                 :          78 :                   ops.create (group_size);
    2366                 :         387 :                   for (lane = 0; lane < group_size; ++lane)
    2367                 :         231 :                     if (stmts[lane])
    2368                 :         231 :                       ops.quick_push (chains[lane][n].op);
    2369                 :             :                     else
    2370                 :           0 :                       ops.quick_push (NULL_TREE);
    2371                 :          78 :                   slp_tree child = vect_create_new_slp_node (ops);
    2372                 :          78 :                   SLP_TREE_DEF_TYPE (child) = dt;
    2373                 :          78 :                   children.safe_push (child);
    2374                 :             :                 }
    2375                 :             :               else
    2376                 :             :                 {
    2377                 :         802 :                   vec<stmt_vec_info> op_stmts;
    2378                 :         802 :                   op_stmts.create (group_size);
    2379                 :         802 :                   slp_tree child = NULL;
    2380                 :             :                   /* Brute-force our way.  We have to consider a lane
    2381                 :             :                      failing after fixing an earlier fail up in the
    2382                 :             :                      SLP discovery recursion.  So track the current
    2383                 :             :                      permute per lane.  */
    2384                 :         802 :                   unsigned *perms = XALLOCAVEC (unsigned, group_size);
    2385                 :         802 :                   memset (perms, 0, sizeof (unsigned) * group_size);
    2386                 :         931 :                   do
    2387                 :             :                     {
    2388                 :         931 :                       op_stmts.truncate (0);
    2389                 :        4437 :                       for (lane = 0; lane < group_size; ++lane)
    2390                 :        2575 :                         if (stmts[lane])
    2391                 :        2575 :                           op_stmts.quick_push
    2392                 :        2575 :                             (vinfo->lookup_def (chains[lane][n].op));
    2393                 :             :                         else
    2394                 :           0 :                           op_stmts.quick_push (NULL);
    2395                 :         931 :                       child = vect_build_slp_tree (vinfo, op_stmts,
    2396                 :             :                                                    group_size, &this_max_nunits,
    2397                 :             :                                                    matches, limit,
    2398                 :             :                                                    &this_tree_size, bst_map);
    2399                 :             :                       /* ???  We're likely getting too many fatal mismatches
    2400                 :             :                          here so maybe we want to ignore them (but then we
    2401                 :             :                          have no idea which lanes fatally mismatched).  */
    2402                 :         931 :                       if (child || !matches[0])
    2403                 :             :                         break;
    2404                 :             :                       /* Swap another lane we have not yet matched up into
    2405                 :             :                          lanes that did not match.  If we run out of
    2406                 :             :                          permute possibilities for a lane terminate the
    2407                 :             :                          search.  */
    2408                 :         405 :                       bool term = false;
    2409                 :         405 :                       for (lane = 1; lane < group_size; ++lane)
    2410                 :         276 :                         if (!matches[lane])
    2411                 :             :                           {
    2412                 :         220 :                             if (n + perms[lane] + 1 == chain_len)
    2413                 :             :                               {
    2414                 :             :                                 term = true;
    2415                 :             :                                 break;
    2416                 :             :                               }
    2417                 :         193 :                             if (dump_enabled_p ())
    2418                 :         119 :                               dump_printf_loc (MSG_NOTE, vect_location,
    2419                 :             :                                                "swapping operand %d and %d "
    2420                 :             :                                                "of lane %d\n",
    2421                 :             :                                                n, n + perms[lane] + 1, lane);
    2422                 :         386 :                             std::swap (chains[lane][n],
    2423                 :         193 :                                        chains[lane][n + perms[lane] + 1]);
    2424                 :         193 :                             perms[lane]++;
    2425                 :             :                           }
    2426                 :         156 :                       if (term)
    2427                 :             :                         break;
    2428                 :             :                     }
    2429                 :             :                   while (1);
    2430                 :         802 :                   if (!child)
    2431                 :             :                     {
    2432                 :          27 :                       if (dump_enabled_p ())
    2433                 :          20 :                         dump_printf_loc (MSG_NOTE, vect_location,
    2434                 :             :                                          "failed to match up op %d\n", n);
    2435                 :          27 :                       op_stmts.release ();
    2436                 :          27 :                       if (lane != group_size - 1)
    2437                 :          12 :                         matches[0] = false;
    2438                 :             :                       else
    2439                 :          15 :                         matches[lane] = false;
    2440                 :          27 :                       goto out;
    2441                 :             :                     }
    2442                 :         775 :                   if (dump_enabled_p ())
    2443                 :             :                     {
    2444                 :         312 :                       dump_printf_loc (MSG_NOTE, vect_location,
    2445                 :             :                                        "matched up op %d to\n", n);
    2446                 :         312 :                       vect_print_slp_tree (MSG_NOTE, vect_location, child);
    2447                 :             :                     }
    2448                 :         775 :                   children.safe_push (child);
    2449                 :             :                 }
    2450                 :             :             }
    2451                 :             :           /* 3. build SLP nodes to combine the chain.  */
    2452                 :         936 :           for (unsigned lane = 0; lane < group_size; ++lane)
    2453                 :        1360 :             if (stmts[lane] && chains[lane][0].code != code)
    2454                 :             :               {
    2455                 :             :                 /* See if there's any alternate all-PLUS entry.  */
    2456                 :             :                 unsigned n;
    2457                 :           8 :                 for (n = 1; n < chain_len; ++n)
    2458                 :             :                   {
    2459                 :          36 :                     for (lane = 0; lane < group_size; ++lane)
    2460                 :          56 :                       if (stmts[lane] && chains[lane][n].code != code)
    2461                 :             :                         break;
    2462                 :           8 :                     if (lane == group_size)
    2463                 :             :                       break;
    2464                 :             :                   }
    2465                 :           8 :                 if (n != chain_len)
    2466                 :             :                   {
    2467                 :             :                     /* Swap that in at first position.  */
    2468                 :           8 :                     std::swap (children[0], children[n]);
    2469                 :          36 :                     for (lane = 0; lane < group_size; ++lane)
    2470                 :          28 :                       if (stmts[lane])
    2471                 :          28 :                         std::swap (chains[lane][0], chains[lane][n]);
    2472                 :             :                   }
    2473                 :             :                 else
    2474                 :             :                   {
    2475                 :             :                     /* ???  When this triggers and we end up with two
    2476                 :             :                        vect_constant/external_def up-front things break (ICE)
    2477                 :             :                        spectacularly finding an insertion place for the
    2478                 :             :                        all-constant op.  We should have a fully
    2479                 :             :                        vect_internal_def operand though(?) so we can swap
    2480                 :             :                        that into first place and then prepend the all-zero
    2481                 :             :                        constant.  */
    2482                 :           0 :                     if (dump_enabled_p ())
    2483                 :           0 :                       dump_printf_loc (MSG_NOTE, vect_location,
    2484                 :             :                                        "inserting constant zero to compensate "
    2485                 :             :                                        "for (partially) negated first "
    2486                 :             :                                        "operand\n");
    2487                 :           0 :                     chain_len++;
    2488                 :           0 :                     for (lane = 0; lane < group_size; ++lane)
    2489                 :           0 :                       if (stmts[lane])
    2490                 :           0 :                         chains[lane].safe_insert
    2491                 :           0 :                           (0, chain_op_t (code, vect_constant_def, NULL_TREE));
    2492                 :           0 :                     vec<tree> zero_ops;
    2493                 :           0 :                     zero_ops.create (group_size);
    2494                 :           0 :                     zero_ops.quick_push (build_zero_cst (TREE_TYPE (vectype)));
    2495                 :           0 :                     for (lane = 1; lane < group_size; ++lane)
    2496                 :           0 :                       if (stmts[lane])
    2497                 :           0 :                         zero_ops.quick_push (zero_ops[0]);
    2498                 :             :                       else
    2499                 :           0 :                         zero_ops.quick_push (NULL_TREE);
    2500                 :           0 :                     slp_tree zero = vect_create_new_slp_node (zero_ops);
    2501                 :           0 :                     SLP_TREE_DEF_TYPE (zero) = vect_constant_def;
    2502                 :           0 :                     children.safe_insert (0, zero);
    2503                 :             :                   }
    2504                 :             :                 break;
    2505                 :             :               }
    2506                 :         852 :           for (unsigned i = 1; i < children.length (); ++i)
    2507                 :             :             {
    2508                 :         588 :               slp_tree op0 = children[i - 1];
    2509                 :         588 :               slp_tree op1 = children[i];
    2510                 :         588 :               bool this_two_op = false;
    2511                 :        2011 :               for (unsigned lane = 0; lane < group_size; ++lane)
    2512                 :        3176 :                 if (stmts[lane] && chains[lane][i].code != chains[0][i].code)
    2513                 :             :                   {
    2514                 :             :                     this_two_op = true;
    2515                 :             :                     break;
    2516                 :             :                   }
    2517                 :         588 :               slp_tree child;
    2518                 :         588 :               if (i == children.length () - 1)
    2519                 :         264 :                 child = vect_create_new_slp_node (node, stmts, 2);
    2520                 :             :               else
    2521                 :         324 :                 child = vect_create_new_slp_node (2, ERROR_MARK);
    2522                 :         588 :               if (this_two_op)
    2523                 :             :                 {
    2524                 :         165 :                   vec<std::pair<unsigned, unsigned> > lperm;
    2525                 :         165 :                   lperm.create (group_size);
    2526                 :         603 :                   for (unsigned lane = 0; lane < group_size; ++lane)
    2527                 :         438 :                     lperm.quick_push (std::make_pair
    2528                 :         438 :                       (chains[lane][i].code != chains[0][i].code, lane));
    2529                 :         330 :                   vect_slp_build_two_operator_nodes (child, vectype, op0, op1,
    2530                 :         165 :                                                      (chains[0][i].code == code
    2531                 :             :                                                       ? op_stmt_info
    2532                 :             :                                                       : other_op_stmt_info),
    2533                 :         165 :                                                      (chains[0][i].code == code
    2534                 :             :                                                       ? other_op_stmt_info
    2535                 :             :                                                       : op_stmt_info),
    2536                 :             :                                                      lperm);
    2537                 :             :                 }
    2538                 :             :               else
    2539                 :             :                 {
    2540                 :         423 :                   SLP_TREE_DEF_TYPE (child) = vect_internal_def;
    2541                 :         423 :                   SLP_TREE_VECTYPE (child) = vectype;
    2542                 :         423 :                   SLP_TREE_LANES (child) = group_size;
    2543                 :         423 :                   SLP_TREE_CHILDREN (child).quick_push (op0);
    2544                 :         423 :                   SLP_TREE_CHILDREN (child).quick_push (op1);
    2545                 :         423 :                   SLP_TREE_REPRESENTATIVE (child)
    2546                 :         846 :                     = (chains[0][i].code == code
    2547                 :         423 :                        ? op_stmt_info : other_op_stmt_info);
    2548                 :             :                 }
    2549                 :         588 :               children[i] = child;
    2550                 :             :             }
    2551                 :         264 :           *tree_size += this_tree_size + 1;
    2552                 :         264 :           *max_nunits = this_max_nunits;
    2553                 :        1255 :           while (!chains.is_empty ())
    2554                 :         700 :             chains.pop ().release ();
    2555                 :             :           return node;
    2556                 :             :         }
    2557                 :        4211 : out:
    2558                 :        4253 :       if (dump_enabled_p ())
    2559                 :        3106 :         dump_printf_loc (MSG_NOTE, vect_location,
    2560                 :             :                          "failed to line up SLP graph by re-associating "
    2561                 :             :                          "operations in lanes%s\n",
    2562                 :             :                          !hard_fail ? " trying regular discovery" : "");
    2563                 :        4254 :       while (!children.is_empty ())
    2564                 :           1 :         vect_free_slp_tree (children.pop ());
    2565                 :        4396 :       while (!chains.is_empty ())
    2566                 :         143 :         chains.pop ().release ();
    2567                 :             :       /* Hard-fail, otherwise we might run into quadratic processing of the
    2568                 :             :          chains starting one stmt into the chain again.  */
    2569                 :        4253 :       if (hard_fail)
    2570                 :             :         return NULL;
    2571                 :             :       /* Fall thru to normal processing.  */
    2572                 :        4517 :     }
    2573                 :             : 
    2574                 :             :   /* Get at the operands, verifying they are compatible.  */
    2575                 :     2763775 :   vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
    2576                 :     2763775 :   slp_oprnd_info oprnd_info;
    2577                 :    14787366 :   FOR_EACH_VEC_ELT (stmts, i, stmt_info)
    2578                 :             :     {
    2579                 :    12026210 :       int res = vect_get_and_check_slp_defs (vinfo, swap[i], skip_args,
    2580                 :             :                                              stmts, i, &oprnds_info);
    2581                 :    12026210 :       if (res != 0)
    2582                 :      592327 :         matches[(res == -1) ? 0 : i] = false;
    2583                 :    12026210 :       if (!matches[0])
    2584                 :             :         break;
    2585                 :             :     }
    2586                 :    14452402 :   for (i = 0; i < group_size; ++i)
    2587                 :    11919782 :     if (!matches[i])
    2588                 :             :       {
    2589                 :      231155 :         vect_free_oprnd_info (oprnds_info);
    2590                 :      231155 :         return NULL;
    2591                 :             :       }
    2592                 :     7597860 :   swap = NULL;
    2593                 :             : 
    2594                 :     7597860 :   bool has_two_operators_perm = false;
    2595                 :    15195720 :   auto_vec<unsigned> two_op_perm_indices[2];
    2596                 :     2532620 :   vec<stmt_vec_info> two_op_scalar_stmts[2] = {vNULL, vNULL};
    2597                 :             : 
    2598                 :     2543782 :   if (two_operators && oprnds_info.length () == 2 && group_size > 2)
    2599                 :             :     {
    2600                 :        1345 :       unsigned idx = 0;
    2601                 :        1345 :       hash_map<gimple *, unsigned> seen;
    2602                 :        1345 :       vec<slp_oprnd_info> new_oprnds_info
    2603                 :        1345 :         = vect_create_oprnd_info (1, group_size);
    2604                 :        1345 :       bool success = true;
    2605                 :             : 
    2606                 :        1345 :       enum tree_code code = ERROR_MARK;
    2607                 :        1345 :       if (oprnds_info[0]->def_stmts[0]
    2608                 :        1345 :           && is_a<gassign *> (oprnds_info[0]->def_stmts[0]->stmt))
    2609                 :        1244 :         code = gimple_assign_rhs_code (oprnds_info[0]->def_stmts[0]->stmt);
    2610                 :             : 
    2611                 :        4033 :       for (unsigned j = 0; j < group_size; ++j)
    2612                 :             :         {
    2613                 :        9859 :           FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
    2614                 :             :             {
    2615                 :        7171 :               stmt_vec_info stmt_info = oprnd_info->def_stmts[j];
    2616                 :        6935 :               if (!stmt_info || !stmt_info->stmt
    2617                 :        6935 :                   || !is_a<gassign *> (stmt_info->stmt)
    2618                 :        6932 :                   || gimple_assign_rhs_code (stmt_info->stmt) != code
    2619                 :       13315 :                   || skip_args[i])
    2620                 :             :                 {
    2621                 :        1027 :                   success = false;
    2622                 :        1027 :                   break;
    2623                 :             :                 }
    2624                 :             : 
    2625                 :        6144 :               bool exists;
    2626                 :        6144 :               unsigned &stmt_idx
    2627                 :        6144 :                 = seen.get_or_insert (stmt_info->stmt, &exists);
    2628                 :             : 
    2629                 :        6144 :               if (!exists)
    2630                 :             :                 {
    2631                 :        5192 :                   new_oprnds_info[0]->def_stmts.safe_push (stmt_info);
    2632                 :        5192 :                   new_oprnds_info[0]->ops.safe_push (oprnd_info->ops[j]);
    2633                 :        5192 :                   stmt_idx = idx;
    2634                 :        5192 :                   idx++;
    2635                 :             :                 }
    2636                 :             : 
    2637                 :        6144 :               two_op_perm_indices[i].safe_push (stmt_idx);
    2638                 :             :             }
    2639                 :             : 
    2640                 :        3715 :           if (!success)
    2641                 :             :             break;
    2642                 :             :         }
    2643                 :             : 
    2644                 :        1345 :       if (success && idx == group_size)
    2645                 :             :         {
    2646                 :          41 :           if (dump_enabled_p ())
    2647                 :             :             {
    2648                 :           0 :               dump_printf_loc (MSG_NOTE, vect_location,
    2649                 :             :                                "Replace two_operators operands:\n");
    2650                 :             : 
    2651                 :           0 :               FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
    2652                 :             :                 {
    2653                 :           0 :                   dump_printf_loc (MSG_NOTE, vect_location,
    2654                 :             :                                    "Operand %u:\n", i);
    2655                 :           0 :                   for (unsigned j = 0; j < group_size; j++)
    2656                 :           0 :                     dump_printf_loc (MSG_NOTE, vect_location, "\tstmt %u %G",
    2657                 :           0 :                                      j, oprnd_info->def_stmts[j]->stmt);
    2658                 :             :                 }
    2659                 :             : 
    2660                 :           0 :               dump_printf_loc (MSG_NOTE, vect_location,
    2661                 :             :                                "With a single operand:\n");
    2662                 :           0 :               for (unsigned j = 0; j < group_size; j++)
    2663                 :           0 :                 dump_printf_loc (MSG_NOTE, vect_location, "\tstmt %u %G",
    2664                 :           0 :                                  j, new_oprnds_info[0]->def_stmts[j]->stmt);
    2665                 :             :             }
    2666                 :             : 
    2667                 :          41 :           two_op_scalar_stmts[0].safe_splice (oprnds_info[0]->def_stmts);
    2668                 :          41 :           two_op_scalar_stmts[1].safe_splice (oprnds_info[1]->def_stmts);
    2669                 :             : 
    2670                 :          41 :           new_oprnds_info[0]->first_op_type = oprnds_info[0]->first_op_type;
    2671                 :          41 :           new_oprnds_info[0]->first_dt = oprnds_info[0]->first_dt;
    2672                 :          41 :           new_oprnds_info[0]->any_pattern = oprnds_info[0]->any_pattern;
    2673                 :          41 :           new_oprnds_info[0]->first_gs_p = oprnds_info[0]->first_gs_p;
    2674                 :          41 :           new_oprnds_info[0]->first_gs_info = oprnds_info[0]->first_gs_info;
    2675                 :             : 
    2676                 :          41 :           vect_free_oprnd_info (oprnds_info);
    2677                 :          41 :           oprnds_info = new_oprnds_info;
    2678                 :          41 :           nops = 1;
    2679                 :          41 :           has_two_operators_perm = true;
    2680                 :             :         }
    2681                 :        1345 :     }
    2682                 :             : 
    2683                 :     5065240 :   auto_vec<slp_tree, 4> children;
    2684                 :             : 
    2685                 :     2532620 :   stmt_info = stmts[0];
    2686                 :             : 
    2687                 :             :   /* Create SLP_TREE nodes for the definition node/s.  */
    2688                 :     6156165 :   FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
    2689                 :             :     {
    2690                 :     3872837 :       slp_tree child = nullptr;
    2691                 :     3872837 :       unsigned int j;
    2692                 :             : 
    2693                 :             :       /* We're skipping certain operands from processing, for example
    2694                 :             :          outer loop reduction initial defs.  */
    2695                 :     3872837 :       if (skip_args[i])
    2696                 :             :         {
    2697                 :      386763 :           children.safe_push (NULL);
    2698                 :     3623545 :           continue;
    2699                 :             :         }
    2700                 :             : 
    2701                 :     3486074 :       if (oprnd_info->first_dt == vect_uninitialized_def)
    2702                 :             :         {
    2703                 :             :           /* COND_EXPR have one too many eventually if the condition
    2704                 :             :              is a SSA name.  */
    2705                 :           0 :           gcc_assert (i == 3 && nops == 4);
    2706                 :           0 :           continue;
    2707                 :             :         }
    2708                 :             : 
    2709                 :     3486074 :       if (is_a <bb_vec_info> (vinfo)
    2710                 :     1650754 :           && oprnd_info->first_dt == vect_internal_def
    2711                 :     4386297 :           && !oprnd_info->any_pattern)
    2712                 :             :         {
    2713                 :             :           /* For BB vectorization, if all defs are the same do not
    2714                 :             :              bother to continue the build along the single-lane
    2715                 :             :              graph but use a splat of the scalar value.  */
    2716                 :      855415 :           stmt_vec_info first_def = oprnd_info->def_stmts[0];
    2717                 :      909354 :           for (j = 1; j < group_size; ++j)
    2718                 :      863986 :             if (oprnd_info->def_stmts[j] != first_def)
    2719                 :             :               break;
    2720                 :      855415 :           if (j == group_size
    2721                 :             :               /* But avoid doing this for loads where we may be
    2722                 :             :                  able to CSE things, unless the stmt is not
    2723                 :             :                  vectorizable.  */
    2724                 :      855415 :               && (!STMT_VINFO_VECTORIZABLE (first_def)
    2725                 :       56593 :                   || !gimple_vuse (first_def->stmt)))
    2726                 :             :             {
    2727                 :       36677 :               if (dump_enabled_p ())
    2728                 :          94 :                 dump_printf_loc (MSG_NOTE, vect_location,
    2729                 :             :                                  "Using a splat of the uniform operand %G",
    2730                 :             :                                  first_def->stmt);
    2731                 :       36677 :               oprnd_info->first_dt = vect_external_def;
    2732                 :             :             }
    2733                 :             :         }
    2734                 :             : 
    2735                 :     3486074 :       if (oprnd_info->first_dt == vect_external_def
    2736                 :     3486074 :           || oprnd_info->first_dt == vect_constant_def)
    2737                 :             :         {
    2738                 :     1259284 :           if (!GET_MODE_SIZE (vinfo->vector_mode).is_constant ())
    2739                 :             :             {
    2740                 :             :               tree op0;
    2741                 :             :               tree uniform_val = op0 = oprnd_info->ops[0];
    2742                 :             :               for (j = 1; j < oprnd_info->ops.length (); ++j)
    2743                 :             :                 if (oprnd_info->ops[j]
    2744                 :             :                     && !operand_equal_p (uniform_val, oprnd_info->ops[j]))
    2745                 :             :                   {
    2746                 :             :                     uniform_val = NULL_TREE;
    2747                 :             :                     break;
    2748                 :             :                   }
    2749                 :             :               if (!uniform_val
    2750                 :             :                   && !can_duplicate_and_interleave_p (vinfo,
    2751                 :             :                                                       oprnd_info->ops.length (),
    2752                 :             :                                                       TREE_TYPE (op0)))
    2753                 :             :                 {
    2754                 :             :                   matches[j] = false;
    2755                 :             :                   if (dump_enabled_p ())
    2756                 :             :                     dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2757                 :             :                                      "Build SLP failed: invalid type of def "
    2758                 :             :                                      "for variable-length SLP %T\n", op0);
    2759                 :             :                   goto fail;
    2760                 :             :                 }
    2761                 :             :             }
    2762                 :     1259284 :           slp_tree invnode = vect_create_new_slp_node (oprnd_info->ops);
    2763                 :     1259284 :           SLP_TREE_DEF_TYPE (invnode) = oprnd_info->first_dt;
    2764                 :     1259284 :           oprnd_info->ops = vNULL;
    2765                 :     1259284 :           children.safe_push (invnode);
    2766                 :     1259284 :           continue;
    2767                 :     1259284 :         }
    2768                 :             : 
    2769                 :             :       /* When we have a masked load with uniform mask discover this
    2770                 :             :          as a single-lane mask with a splat permute.  This way we can
    2771                 :             :          recognize this as a masked load-lane by stripping the splat.  */
    2772                 :     2226790 :       if (is_a <gcall *> (STMT_VINFO_STMT (stmt_info))
    2773                 :       33351 :           && gimple_call_internal_p (STMT_VINFO_STMT (stmt_info),
    2774                 :             :                                      IFN_MASK_LOAD)
    2775                 :        4910 :           && STMT_VINFO_GROUPED_ACCESS (stmt_info)
    2776                 :     2226822 :           && ! STMT_VINFO_SLP_VECT_ONLY (DR_GROUP_FIRST_ELEMENT (stmt_info)))
    2777                 :             :         {
    2778                 :           0 :           vec<stmt_vec_info> def_stmts2;
    2779                 :           0 :           def_stmts2.create (1);
    2780                 :           0 :           def_stmts2.quick_push (oprnd_info->def_stmts[0]);
    2781                 :           0 :           child = vect_build_slp_tree (vinfo, def_stmts2, 1,
    2782                 :             :                                        &this_max_nunits,
    2783                 :             :                                        matches, limit,
    2784                 :             :                                        &this_tree_size, bst_map);
    2785                 :           0 :           if (child)
    2786                 :             :             {
    2787                 :           0 :               slp_tree pnode = vect_create_new_slp_node (1, VEC_PERM_EXPR);
    2788                 :           0 :               SLP_TREE_VECTYPE (pnode) = SLP_TREE_VECTYPE (child);
    2789                 :           0 :               SLP_TREE_LANES (pnode) = group_size;
    2790                 :           0 :               SLP_TREE_SCALAR_STMTS (pnode).create (group_size);
    2791                 :           0 :               SLP_TREE_LANE_PERMUTATION (pnode).create (group_size);
    2792                 :           0 :               for (unsigned k = 0; k < group_size; ++k)
    2793                 :             :                 {
    2794                 :           0 :                   SLP_TREE_SCALAR_STMTS (pnode)
    2795                 :           0 :                     .quick_push (oprnd_info->def_stmts[0]);
    2796                 :           0 :                   SLP_TREE_LANE_PERMUTATION (pnode)
    2797                 :           0 :                     .quick_push (std::make_pair (0u, 0u));
    2798                 :             :                 }
    2799                 :           0 :               SLP_TREE_CHILDREN (pnode).quick_push (child);
    2800                 :           0 :               pnode->max_nunits = child->max_nunits;
    2801                 :           0 :               children.safe_push (pnode);
    2802                 :           0 :               oprnd_info->def_stmts = vNULL;
    2803                 :           0 :               continue;
    2804                 :           0 :             }
    2805                 :             :           else
    2806                 :           0 :             def_stmts2.release ();
    2807                 :             :         }
    2808                 :             : 
    2809                 :     2226790 :       if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
    2810                 :             :                                         group_size, &this_max_nunits,
    2811                 :             :                                         matches, limit,
    2812                 :             :                                         &this_tree_size, bst_map)) != NULL)
    2813                 :             :         {
    2814                 :     1709588 :           oprnd_info->def_stmts = vNULL;
    2815                 :     1709588 :           children.safe_push (child);
    2816                 :     1709588 :           continue;
    2817                 :             :         }
    2818                 :             : 
    2819                 :             :       /* If the SLP build for operand zero failed and operand zero
    2820                 :             :          and one can be commutated try that for the scalar stmts
    2821                 :             :          that failed the match.  */
    2822                 :      517202 :       if (i == 0
    2823                 :             :           /* A first scalar stmt mismatch signals a fatal mismatch.  */
    2824                 :      381956 :           && matches[0]
    2825                 :             :           /* ???  For COND_EXPRs we can swap the comparison operands
    2826                 :             :              as well as the arms under some constraints.  */
    2827                 :      229053 :           && nops == 2
    2828                 :      129062 :           && oprnds_info[1]->first_dt == vect_internal_def
    2829                 :       75044 :           && is_gimple_assign (stmt_info->stmt)
    2830                 :             :           /* Swapping operands for reductions breaks assumptions later on.  */
    2831                 :      573175 :           && STMT_VINFO_REDUC_IDX (stmt_info) == -1)
    2832                 :             :         {
    2833                 :             :           /* See whether we can swap the matching or the non-matching
    2834                 :             :              stmt operands.  */
    2835                 :             :           bool swap_not_matching = true;
    2836                 :       69334 :           do
    2837                 :             :             {
    2838                 :     7057256 :               for (j = 0; j < group_size; ++j)
    2839                 :             :                 {
    2840                 :     7013367 :                   if (matches[j] != !swap_not_matching)
    2841                 :       75552 :                     continue;
    2842                 :     6937815 :                   stmt_vec_info stmt_info = stmts[j];
    2843                 :             :                   /* Verify if we can swap operands of this stmt.  */
    2844                 :     6937815 :                   gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
    2845                 :     6937815 :                   if (!stmt
    2846                 :     6937815 :                       || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
    2847                 :             :                     {
    2848                 :       25445 :                       if (!swap_not_matching)
    2849                 :       11940 :                         goto fail;
    2850                 :             :                       swap_not_matching = false;
    2851                 :             :                       break;
    2852                 :             :                     }
    2853                 :             :                 }
    2854                 :             :             }
    2855                 :       57394 :           while (j != group_size);
    2856                 :             : 
    2857                 :             :           /* Swap mismatched definition stmts.  */
    2858                 :       43889 :           if (dump_enabled_p ())
    2859                 :         348 :             dump_printf_loc (MSG_NOTE, vect_location,
    2860                 :             :                              "Re-trying with swapped operands of stmts ");
    2861                 :     7017390 :           for (j = 0; j < group_size; ++j)
    2862                 :     6973501 :             if (matches[j] == !swap_not_matching)
    2863                 :             :               {
    2864                 :    13824476 :                 std::swap (oprnds_info[0]->def_stmts[j],
    2865                 :     6912238 :                            oprnds_info[1]->def_stmts[j]);
    2866                 :    13824476 :                 std::swap (oprnds_info[0]->ops[j],
    2867                 :     6912238 :                            oprnds_info[1]->ops[j]);
    2868                 :     6912238 :                 if (dump_enabled_p ())
    2869                 :         971 :                   dump_printf (MSG_NOTE, "%d ", j);
    2870                 :             :               }
    2871                 :       43889 :           if (dump_enabled_p ())
    2872                 :         348 :             dump_printf (MSG_NOTE, "\n");
    2873                 :             :           /* After swapping some operands we lost track whether an
    2874                 :             :              operand has any pattern defs so be conservative here.  */
    2875                 :       84884 :           if (oprnds_info[0]->any_pattern || oprnds_info[1]->any_pattern)
    2876                 :        3065 :             oprnds_info[0]->any_pattern = oprnds_info[1]->any_pattern = true;
    2877                 :             :           /* And try again with scratch 'matches' ... */
    2878                 :       43889 :           bool *tem = XALLOCAVEC (bool, group_size);
    2879                 :       43889 :           if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
    2880                 :             :                                             group_size, &this_max_nunits,
    2881                 :             :                                             tem, limit,
    2882                 :             :                                             &this_tree_size, bst_map)) != NULL)
    2883                 :             :             {
    2884                 :        7503 :               oprnd_info->def_stmts = vNULL;
    2885                 :        7503 :               children.safe_push (child);
    2886                 :        7503 :               continue;
    2887                 :             :             }
    2888                 :             :         }
    2889                 :      509699 : fail:
    2890                 :             : 
    2891                 :             :       /* If the SLP build failed and we analyze a basic-block
    2892                 :             :          simply treat nodes we fail to build as externally defined
    2893                 :             :          (and thus build vectors from the scalar defs).
    2894                 :             :          The cost model will reject outright expensive cases.
    2895                 :             :          ???  This doesn't treat cases where permutation ultimatively
    2896                 :             :          fails (or we don't try permutation below).  Ideally we'd
    2897                 :             :          even compute a permutation that will end up with the maximum
    2898                 :             :          SLP tree size...  */
    2899                 :      509699 :       if (is_a <bb_vec_info> (vinfo)
    2900                 :             :           /* ???  Rejecting patterns this way doesn't work.  We'd have to
    2901                 :             :              do extra work to cancel the pattern so the uses see the
    2902                 :             :              scalar version.  */
    2903                 :      474416 :           && !is_pattern_stmt_p (stmt_info)
    2904                 :      957282 :           && !oprnd_info->any_pattern)
    2905                 :             :         {
    2906                 :             :           /* But if there's a leading vector sized set of matching stmts
    2907                 :             :              fail here so we can split the group.  This matches the condition
    2908                 :             :              vect_analyze_slp_instance uses.  */
    2909                 :             :           /* ???  We might want to split here and combine the results to support
    2910                 :             :              multiple vector sizes better.  */
    2911                 :      731399 :           for (j = 0; j < group_size; ++j)
    2912                 :      731399 :             if (!matches[j])
    2913                 :             :               break;
    2914                 :      566839 :           if (!known_ge (j, TYPE_VECTOR_SUBPARTS (vectype))
    2915                 :      447249 :               && vect_slp_can_convert_to_external (oprnd_info->def_stmts))
    2916                 :             :             {
    2917                 :      260407 :               if (dump_enabled_p ())
    2918                 :         445 :                 dump_printf_loc (MSG_NOTE, vect_location,
    2919                 :             :                                  "Building vector operands from scalars\n");
    2920                 :      260407 :               this_tree_size++;
    2921                 :      260407 :               child = vect_create_new_slp_node (oprnd_info->ops);
    2922                 :      260407 :               children.safe_push (child);
    2923                 :      260407 :               oprnd_info->ops = vNULL;
    2924                 :      260407 :               continue;
    2925                 :             :             }
    2926                 :             :         }
    2927                 :             : 
    2928                 :      249292 :       gcc_assert (child == NULL);
    2929                 :      289702 :       FOR_EACH_VEC_ELT (children, j, child)
    2930                 :       40410 :         if (child)
    2931                 :       40410 :           vect_free_slp_tree (child);
    2932                 :      249292 :       vect_free_oprnd_info (oprnds_info);
    2933                 :      249292 :       return NULL;
    2934                 :             :     }
    2935                 :             : 
    2936                 :     2283328 :   vect_free_oprnd_info (oprnds_info);
    2937                 :             : 
    2938                 :             :   /* If we have all children of a child built up from uniform scalars
    2939                 :             :      or does more than one possibly expensive vector construction then
    2940                 :             :      just throw that away, causing it built up from scalars.
    2941                 :             :      The exception is the SLP node for the vector store.  */
    2942                 :     2283328 :   if (is_a <bb_vec_info> (vinfo)
    2943                 :     1037853 :       && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
    2944                 :             :       /* ???  Rejecting patterns this way doesn't work.  We'd have to
    2945                 :             :          do extra work to cancel the pattern so the uses see the
    2946                 :             :          scalar version.  */
    2947                 :     2669318 :       && !is_pattern_stmt_p (stmt_info))
    2948                 :             :     {
    2949                 :             :       slp_tree child;
    2950                 :             :       unsigned j;
    2951                 :             :       bool all_uniform_p = true;
    2952                 :             :       unsigned n_vector_builds = 0;
    2953                 :     1066345 :       FOR_EACH_VEC_ELT (children, j, child)
    2954                 :             :         {
    2955                 :      705338 :           if (!child)
    2956                 :             :             ;
    2957                 :      705338 :           else if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
    2958                 :             :             all_uniform_p = false;
    2959                 :      490912 :           else if (!vect_slp_tree_uniform_p (child))
    2960                 :             :             {
    2961                 :      357387 :               all_uniform_p = false;
    2962                 :      357387 :               if (SLP_TREE_DEF_TYPE (child) == vect_external_def)
    2963                 :      324551 :                 n_vector_builds++;
    2964                 :             :             }
    2965                 :             :         }
    2966                 :      361007 :       if (all_uniform_p
    2967                 :      361007 :           || n_vector_builds > 1
    2968                 :      626922 :           || (n_vector_builds == children.length ()
    2969                 :       22190 :               && is_a <gphi *> (stmt_info->stmt)))
    2970                 :             :         {
    2971                 :             :           /* Roll back.  */
    2972                 :      101156 :           matches[0] = false;
    2973                 :      312430 :           FOR_EACH_VEC_ELT (children, j, child)
    2974                 :      211274 :             if (child)
    2975                 :      211274 :               vect_free_slp_tree (child);
    2976                 :             : 
    2977                 :      101156 :           if (dump_enabled_p ())
    2978                 :         114 :             dump_printf_loc (MSG_NOTE, vect_location,
    2979                 :             :                              "Building parent vector operands from "
    2980                 :             :                              "scalars instead\n");
    2981                 :      101156 :           return NULL;
    2982                 :             :         }
    2983                 :             :     }
    2984                 :             : 
    2985                 :     2182172 :   *tree_size += this_tree_size + 1;
    2986                 :     2182172 :   *max_nunits = this_max_nunits;
    2987                 :             : 
    2988                 :     2182172 :   if (two_operators)
    2989                 :             :     {
    2990                 :             :       /* ???  We'd likely want to either cache in bst_map sth like
    2991                 :             :          { a+b, NULL, a+b, NULL } and { NULL, a-b, NULL, a-b } or
    2992                 :             :          the true { a+b, a+b, a+b, a+b } ... but there we don't have
    2993                 :             :          explicit stmts to put in so the keying on 'stmts' doesn't
    2994                 :             :          work (but we have the same issue with nodes that use 'ops').  */
    2995                 :             : 
    2996                 :        3915 :       if (has_two_operators_perm)
    2997                 :             :         {
    2998                 :          21 :           slp_tree child = children[0];
    2999                 :          21 :           children.truncate (0);
    3000                 :          63 :           for (i = 0; i < 2; i++)
    3001                 :             :             {
    3002                 :          42 :               slp_tree pnode
    3003                 :          42 :                 = vect_create_new_slp_node (two_op_scalar_stmts[i], 2);
    3004                 :          42 :               SLP_TREE_CODE (pnode) = VEC_PERM_EXPR;
    3005                 :          42 :               SLP_TREE_VECTYPE (pnode) = vectype;
    3006                 :          42 :               SLP_TREE_CHILDREN (pnode).quick_push (child);
    3007                 :          42 :               SLP_TREE_CHILDREN (pnode).quick_push (child);
    3008                 :          42 :               lane_permutation_t& perm = SLP_TREE_LANE_PERMUTATION (pnode);
    3009                 :          42 :               children.safe_push (pnode);
    3010                 :             : 
    3011                 :         466 :               for (unsigned j = 0; j < stmts.length (); j++)
    3012                 :         424 :                 perm.safe_push (std::make_pair (0, two_op_perm_indices[i][j]));
    3013                 :             :             }
    3014                 :             : 
    3015                 :          21 :           SLP_TREE_REF_COUNT (child) += 4;
    3016                 :             :         }
    3017                 :             : 
    3018                 :        3915 :       slp_tree one = new _slp_tree;
    3019                 :        3915 :       slp_tree two = new _slp_tree;
    3020                 :        3915 :       SLP_TREE_DEF_TYPE (one) = vect_internal_def;
    3021                 :        3915 :       SLP_TREE_DEF_TYPE (two) = vect_internal_def;
    3022                 :        3915 :       SLP_TREE_VECTYPE (one) = vectype;
    3023                 :        3915 :       SLP_TREE_VECTYPE (two) = vectype;
    3024                 :        3915 :       SLP_TREE_CHILDREN (one).safe_splice (children);
    3025                 :        3915 :       SLP_TREE_CHILDREN (two).safe_splice (children);
    3026                 :        3915 :       slp_tree child;
    3027                 :       15660 :       FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (two), i, child)
    3028                 :        7830 :         SLP_TREE_REF_COUNT (child)++;
    3029                 :             : 
    3030                 :             :       /* Here we record the original defs since this
    3031                 :             :          node represents the final lane configuration.  */
    3032                 :        3915 :       node = vect_create_new_slp_node (node, stmts, 2);
    3033                 :        3915 :       SLP_TREE_VECTYPE (node) = vectype;
    3034                 :        3915 :       SLP_TREE_CODE (node) = VEC_PERM_EXPR;
    3035                 :        3915 :       SLP_TREE_CHILDREN (node).quick_push (one);
    3036                 :        3915 :       SLP_TREE_CHILDREN (node).quick_push (two);
    3037                 :        3915 :       gassign *stmt = as_a <gassign *> (stmts[0]->stmt);
    3038                 :        3915 :       enum tree_code code0 = gimple_assign_rhs_code (stmt);
    3039                 :        3915 :       enum tree_code ocode = ERROR_MARK;
    3040                 :        3915 :       stmt_vec_info ostmt_info;
    3041                 :        3915 :       unsigned j = 0;
    3042                 :       14717 :       FOR_EACH_VEC_ELT (stmts, i, ostmt_info)
    3043                 :             :         {
    3044                 :       10802 :           gassign *ostmt = as_a <gassign *> (ostmt_info->stmt);
    3045                 :       10802 :           if (gimple_assign_rhs_code (ostmt) != code0)
    3046                 :             :             {
    3047                 :        5402 :               SLP_TREE_LANE_PERMUTATION (node).safe_push (std::make_pair (1, i));
    3048                 :        5402 :               ocode = gimple_assign_rhs_code (ostmt);
    3049                 :        5402 :               j = i;
    3050                 :             :             }
    3051                 :             :           else
    3052                 :        5400 :             SLP_TREE_LANE_PERMUTATION (node).safe_push (std::make_pair (0, i));
    3053                 :             :         }
    3054                 :             : 
    3055                 :        3915 :       SLP_TREE_CODE (one) = code0;
    3056                 :        3915 :       SLP_TREE_CODE (two) = ocode;
    3057                 :        3915 :       SLP_TREE_LANES (one) = stmts.length ();
    3058                 :        3915 :       SLP_TREE_LANES (two) = stmts.length ();
    3059                 :        3915 :       SLP_TREE_REPRESENTATIVE (one) = stmts[0];
    3060                 :        3915 :       SLP_TREE_REPRESENTATIVE (two) = stmts[j];
    3061                 :             : 
    3062                 :        3915 :       return node;
    3063                 :             :     }
    3064                 :             : 
    3065                 :     2178257 :   node = vect_create_new_slp_node (node, stmts, nops);
    3066                 :     2178257 :   SLP_TREE_VECTYPE (node) = vectype;
    3067                 :     2178257 :   SLP_TREE_CHILDREN (node).splice (children);
    3068                 :     2178257 :   return node;
    3069                 :     7597860 : }
    3070                 :             : 
    3071                 :             : /* Dump a single SLP tree NODE.  */
    3072                 :             : 
    3073                 :             : static void
    3074                 :      455891 : vect_print_slp_tree (dump_flags_t dump_kind, dump_location_t loc,
    3075                 :             :                      slp_tree node)
    3076                 :             : {
    3077                 :      455891 :   unsigned i, j;
    3078                 :      455891 :   slp_tree child;
    3079                 :      455891 :   stmt_vec_info stmt_info;
    3080                 :      455891 :   tree op;
    3081                 :             : 
    3082                 :      455891 :   dump_metadata_t metadata (dump_kind, loc.get_impl_location ());
    3083                 :      455891 :   dump_user_location_t user_loc = loc.get_user_location ();
    3084                 :      455891 :   dump_printf_loc (metadata, user_loc,
    3085                 :             :                    "node%s %p (max_nunits=" HOST_WIDE_INT_PRINT_UNSIGNED
    3086                 :             :                    ", refcnt=%u)",
    3087                 :      455891 :                    SLP_TREE_DEF_TYPE (node) == vect_external_def
    3088                 :             :                    ? " (external)"
    3089                 :             :                    : (SLP_TREE_DEF_TYPE (node) == vect_constant_def
    3090                 :      441078 :                       ? " (constant)"
    3091                 :             :                       : ""), (void *) node,
    3092                 :      455891 :                    estimated_poly_value (node->max_nunits),
    3093                 :             :                                          SLP_TREE_REF_COUNT (node));
    3094                 :      455891 :   if (SLP_TREE_VECTYPE (node))
    3095                 :      383588 :     dump_printf (metadata, " %T", SLP_TREE_VECTYPE (node));
    3096                 :      455891 :   dump_printf (metadata, "\n");
    3097                 :      455891 :   if (SLP_TREE_DEF_TYPE (node) == vect_internal_def)
    3098                 :             :     {
    3099                 :      369697 :       if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
    3100                 :       14452 :         dump_printf_loc (metadata, user_loc, "op: VEC_PERM_EXPR\n");
    3101                 :             :       else
    3102                 :      355245 :         dump_printf_loc (metadata, user_loc, "op template: %G",
    3103                 :      355245 :                          SLP_TREE_REPRESENTATIVE (node)->stmt);
    3104                 :             :     }
    3105                 :      455891 :   if (SLP_TREE_SCALAR_STMTS (node).exists ())
    3106                 :      874226 :     FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
    3107                 :      512692 :       if (stmt_info)
    3108                 :      505470 :         dump_printf_loc (metadata, user_loc, "\t%sstmt %u %G",
    3109                 :      505470 :                          STMT_VINFO_LIVE_P (stmt_info) ? "[l] " : "",
    3110                 :             :                          i, stmt_info->stmt);
    3111                 :             :       else
    3112                 :        7222 :         dump_printf_loc (metadata, user_loc, "\tstmt %u ---\n", i);
    3113                 :             :   else
    3114                 :             :     {
    3115                 :       94357 :       dump_printf_loc (metadata, user_loc, "\t{ ");
    3116                 :      299446 :       FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node), i, op)
    3117                 :      110732 :         dump_printf (metadata, "%T%s ", op,
    3118                 :      110732 :                      i < SLP_TREE_SCALAR_OPS (node).length () - 1 ? "," : "");
    3119                 :       94357 :       dump_printf (metadata, "}\n");
    3120                 :             :     }
    3121                 :      455891 :   if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
    3122                 :             :     {
    3123                 :       62753 :       dump_printf_loc (metadata, user_loc, "\tload permutation {");
    3124                 :      204340 :       FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (node), i, j)
    3125                 :       78834 :         dump_printf (dump_kind, " %u", j);
    3126                 :       62753 :       dump_printf (dump_kind, " }\n");
    3127                 :             :     }
    3128                 :      455891 :   if (SLP_TREE_LANE_PERMUTATION (node).exists ())
    3129                 :             :     {
    3130                 :       14460 :       dump_printf_loc (metadata, user_loc, "\tlane permutation {");
    3131                 :       69337 :       for (i = 0; i < SLP_TREE_LANE_PERMUTATION (node).length (); ++i)
    3132                 :       40417 :         dump_printf (dump_kind, " %u[%u]",
    3133                 :       40417 :                      SLP_TREE_LANE_PERMUTATION (node)[i].first,
    3134                 :       40417 :                      SLP_TREE_LANE_PERMUTATION (node)[i].second);
    3135                 :       14460 :       dump_printf (dump_kind, " }%s\n",
    3136                 :       14460 :                    node->ldst_lanes ? " (load-lanes)" : "");
    3137                 :             :     }
    3138                 :      455891 :   if (SLP_TREE_CHILDREN (node).is_empty ())
    3139                 :      170377 :     return;
    3140                 :      285514 :   dump_printf_loc (metadata, user_loc, "\tchildren");
    3141                 :     1040256 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    3142                 :      469228 :     dump_printf (dump_kind, " %p", (void *)child);
    3143                 :      285514 :   dump_printf (dump_kind, "%s\n",
    3144                 :      285514 :                node->ldst_lanes && !SLP_TREE_LANE_PERMUTATION (node).exists ()
    3145                 :             :                ? " (store-lanes)" : "");
    3146                 :             : }
    3147                 :             : 
    3148                 :             : DEBUG_FUNCTION void
    3149                 :           0 : debug (slp_tree node)
    3150                 :             : {
    3151                 :           0 :   debug_dump_context ctx;
    3152                 :           0 :   vect_print_slp_tree (MSG_NOTE,
    3153                 :           0 :                        dump_location_t::from_location_t (UNKNOWN_LOCATION),
    3154                 :             :                        node);
    3155                 :           0 : }
    3156                 :             : 
    3157                 :             : /* Recursive helper for the dot producer below.  */
    3158                 :             : 
    3159                 :             : static void
    3160                 :           0 : dot_slp_tree (FILE *f, slp_tree node, hash_set<slp_tree> &visited)
    3161                 :             : {
    3162                 :           0 :   if (visited.add (node))
    3163                 :             :     return;
    3164                 :             : 
    3165                 :           0 :   fprintf (f, "\"%p\" [label=\"", (void *)node);
    3166                 :           0 :   vect_print_slp_tree (MSG_NOTE,
    3167                 :           0 :                        dump_location_t::from_location_t (UNKNOWN_LOCATION),
    3168                 :             :                        node);
    3169                 :           0 :   fprintf (f, "\"];\n");
    3170                 :             : 
    3171                 :             : 
    3172                 :           0 :   for (slp_tree child : SLP_TREE_CHILDREN (node))
    3173                 :           0 :     fprintf (f, "\"%p\" -> \"%p\";", (void *)node, (void *)child);
    3174                 :             : 
    3175                 :           0 :   for (slp_tree child : SLP_TREE_CHILDREN (node))
    3176                 :           0 :     if (child)
    3177                 :           0 :       dot_slp_tree (f, child, visited);
    3178                 :             : }
    3179                 :             : 
    3180                 :             : DEBUG_FUNCTION void
    3181                 :           0 : dot_slp_tree (const char *fname, slp_tree node)
    3182                 :             : {
    3183                 :           0 :   FILE *f = fopen (fname, "w");
    3184                 :           0 :   fprintf (f, "digraph {\n");
    3185                 :           0 :   fflush (f);
    3186                 :           0 :     {
    3187                 :           0 :       debug_dump_context ctx (f);
    3188                 :           0 :       hash_set<slp_tree> visited;
    3189                 :           0 :       dot_slp_tree (f, node, visited);
    3190                 :           0 :     }
    3191                 :           0 :   fflush (f);
    3192                 :           0 :   fprintf (f, "}\n");
    3193                 :           0 :   fclose (f);
    3194                 :           0 : }
    3195                 :             : 
    3196                 :             : DEBUG_FUNCTION void
    3197                 :           0 : dot_slp_tree (const char *fname, const vec<slp_instance> &slp_instances)
    3198                 :             : {
    3199                 :           0 :   FILE *f = fopen (fname, "w");
    3200                 :           0 :   fprintf (f, "digraph {\n");
    3201                 :           0 :   fflush (f);
    3202                 :           0 :     {
    3203                 :           0 :       debug_dump_context ctx (f);
    3204                 :           0 :       hash_set<slp_tree> visited;
    3205                 :           0 :       for (auto inst : slp_instances)
    3206                 :           0 :         dot_slp_tree (f, SLP_INSTANCE_TREE (inst), visited);
    3207                 :           0 :     }
    3208                 :           0 :   fflush (f);
    3209                 :           0 :   fprintf (f, "}\n");
    3210                 :           0 :   fclose (f);
    3211                 :           0 : }
    3212                 :             : 
    3213                 :             : /* Dump a slp tree NODE using flags specified in DUMP_KIND.  */
    3214                 :             : 
    3215                 :             : static void
    3216                 :      496441 : vect_print_slp_graph (dump_flags_t dump_kind, dump_location_t loc,
    3217                 :             :                       slp_tree node, hash_set<slp_tree> &visited)
    3218                 :             : {
    3219                 :      496441 :   unsigned i;
    3220                 :      496441 :   slp_tree child;
    3221                 :             : 
    3222                 :      496441 :   if (visited.add (node))
    3223                 :      496441 :     return;
    3224                 :             : 
    3225                 :      455579 :   vect_print_slp_tree (dump_kind, loc, node);
    3226                 :             : 
    3227                 :     1380027 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    3228                 :      468869 :     if (child)
    3229                 :      417829 :       vect_print_slp_graph (dump_kind, loc, child, visited);
    3230                 :             : }
    3231                 :             : 
    3232                 :             : static void
    3233                 :       47477 : vect_print_slp_graph (dump_flags_t dump_kind, dump_location_t loc,
    3234                 :             :                       slp_tree entry)
    3235                 :             : {
    3236                 :       47477 :   hash_set<slp_tree> visited;
    3237                 :       47477 :   vect_print_slp_graph (dump_kind, loc, entry, visited);
    3238                 :       47477 : }
    3239                 :             : 
    3240                 :             : DEBUG_FUNCTION void
    3241                 :           0 : debug (slp_instance instance)
    3242                 :             : {
    3243                 :           0 :   debug_dump_context ctx;
    3244                 :           0 :   vect_print_slp_graph (MSG_NOTE,
    3245                 :           0 :                         dump_location_t::from_location_t (UNKNOWN_LOCATION),
    3246                 :             :                         SLP_INSTANCE_TREE (instance));
    3247                 :           0 : }
    3248                 :             : 
    3249                 :             : /* Mark the tree rooted at NODE with PURE_SLP.  */
    3250                 :             : 
    3251                 :             : static void
    3252                 :     4628258 : vect_mark_slp_stmts (vec_info *vinfo, slp_tree node,
    3253                 :             :                      hash_set<slp_tree> &visited)
    3254                 :             : {
    3255                 :     4628258 :   int i;
    3256                 :     4628258 :   stmt_vec_info stmt_info;
    3257                 :     4628258 :   slp_tree child;
    3258                 :             : 
    3259                 :     4628258 :   if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
    3260                 :             :     return;
    3261                 :             : 
    3262                 :     3244855 :   if (visited.add (node))
    3263                 :             :     return;
    3264                 :             : 
    3265                 :     8021294 :   FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
    3266                 :     5009190 :     if (stmt_info)
    3267                 :             :       {
    3268                 :     4870914 :         STMT_SLP_TYPE (stmt_info) = pure_slp;
    3269                 :             :         /* ???  For .MASK_LOAD and .MASK_STORE detected as load/store-lanes
    3270                 :             :            when there is the mask_conversion pattern applied we have lost the
    3271                 :             :            alternate lanes of the uniform mask which nevertheless
    3272                 :             :            have separate pattern defs.  To not confuse hybrid
    3273                 :             :            analysis we mark those as covered as well here.  */
    3274                 :     4870914 :         if (node->ldst_lanes)
    3275                 :     5009190 :           if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
    3276                 :           0 :             if (gimple_call_internal_p (call, IFN_MASK_LOAD)
    3277                 :           0 :                 || gimple_call_internal_p (call, IFN_MASK_STORE))
    3278                 :             :               {
    3279                 :           0 :                 tree mask = gimple_call_arg (call,
    3280                 :             :                                              internal_fn_mask_index
    3281                 :           0 :                                              (gimple_call_internal_fn (call)));
    3282                 :           0 :                 if (TREE_CODE (mask) == SSA_NAME)
    3283                 :           0 :                   if (stmt_vec_info mask_info = vinfo->lookup_def (mask))
    3284                 :             :                     {
    3285                 :           0 :                       mask_info = vect_stmt_to_vectorize (mask_info);
    3286                 :           0 :                       STMT_SLP_TYPE (mask_info) = pure_slp;
    3287                 :             :                     }
    3288                 :             :               }
    3289                 :             :       }
    3290                 :             : 
    3291                 :     6937142 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    3292                 :     3925038 :     if (child)
    3293                 :     3432888 :       vect_mark_slp_stmts (vinfo, child, visited);
    3294                 :             : }
    3295                 :             : 
    3296                 :             : static void
    3297                 :     1195370 : vect_mark_slp_stmts (vec_info *vinfo, slp_tree node)
    3298                 :             : {
    3299                 :     1195370 :   hash_set<slp_tree> visited;
    3300                 :     1195370 :   vect_mark_slp_stmts (vinfo, node, visited);
    3301                 :     1195370 : }
    3302                 :             : 
    3303                 :             : /* Mark the statements of the tree rooted at NODE as relevant (vect_used).  */
    3304                 :             : 
    3305                 :             : static void
    3306                 :     2211451 : vect_mark_slp_stmts_relevant (slp_tree node, hash_set<slp_tree> &visited)
    3307                 :             : {
    3308                 :     2211451 :   int i;
    3309                 :     2211451 :   stmt_vec_info stmt_info;
    3310                 :     2211451 :   slp_tree child;
    3311                 :             : 
    3312                 :     2211451 :   if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
    3313                 :             :     return;
    3314                 :             : 
    3315                 :     1313778 :   if (visited.add (node))
    3316                 :             :     return;
    3317                 :             : 
    3318                 :     4111149 :   FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
    3319                 :     2885613 :     if (stmt_info)
    3320                 :             :       {
    3321                 :     2885613 :         gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
    3322                 :             :                     || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
    3323                 :     2885613 :         STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
    3324                 :             :       }
    3325                 :             : 
    3326                 :     2680714 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    3327                 :     1455178 :     if (child)
    3328                 :     1455178 :       vect_mark_slp_stmts_relevant (child, visited);
    3329                 :             : }
    3330                 :             : 
    3331                 :             : static void
    3332                 :      756273 : vect_mark_slp_stmts_relevant (slp_tree node)
    3333                 :             : {
    3334                 :      756273 :   hash_set<slp_tree> visited;
    3335                 :      756273 :   vect_mark_slp_stmts_relevant (node, visited);
    3336                 :      756273 : }
    3337                 :             : 
    3338                 :             : 
    3339                 :             : /* Gather loads in the SLP graph NODE and populate the INST loads array.  */
    3340                 :             : 
    3341                 :             : static void
    3342                 :     7776667 : vect_gather_slp_loads (vec<slp_tree> &loads, slp_tree node,
    3343                 :             :                        hash_set<slp_tree> &visited)
    3344                 :             : {
    3345                 :     7776667 :   if (!node || visited.add (node))
    3346                 :     1358781 :     return;
    3347                 :             : 
    3348                 :     6417886 :   if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
    3349                 :             :     return;
    3350                 :             : 
    3351                 :     4586217 :   if (SLP_TREE_CODE (node) != VEC_PERM_EXPR)
    3352                 :             :     {
    3353                 :     4421895 :       stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
    3354                 :     4421895 :       if (STMT_VINFO_DATA_REF (stmt_info)
    3355                 :     1858957 :           && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
    3356                 :      834079 :         loads.safe_push (node);
    3357                 :             :     }
    3358                 :             : 
    3359                 :             :   unsigned i;
    3360                 :             :   slp_tree child;
    3361                 :    10722327 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    3362                 :     6136110 :     vect_gather_slp_loads (loads, child, visited);
    3363                 :             : }
    3364                 :             : 
    3365                 :             : 
    3366                 :             : /* Find the last store in SLP INSTANCE.  */
    3367                 :             : 
    3368                 :             : stmt_vec_info
    3369                 :     2685148 : vect_find_last_scalar_stmt_in_slp (slp_tree node)
    3370                 :             : {
    3371                 :     2685148 :   stmt_vec_info last = NULL;
    3372                 :     2685148 :   stmt_vec_info stmt_vinfo;
    3373                 :             : 
    3374                 :     9671610 :   for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt_vinfo); i++)
    3375                 :     6986462 :     if (stmt_vinfo)
    3376                 :             :       {
    3377                 :     6986462 :         stmt_vinfo = vect_orig_stmt (stmt_vinfo);
    3378                 :     6986462 :         last = last ? get_later_stmt (stmt_vinfo, last) : stmt_vinfo;
    3379                 :             :       }
    3380                 :             : 
    3381                 :     2685148 :   return last;
    3382                 :             : }
    3383                 :             : 
    3384                 :             : /* Find the first stmt in NODE.  */
    3385                 :             : 
    3386                 :             : stmt_vec_info
    3387                 :      540000 : vect_find_first_scalar_stmt_in_slp (slp_tree node)
    3388                 :             : {
    3389                 :      540000 :   stmt_vec_info first = NULL;
    3390                 :      540000 :   stmt_vec_info stmt_vinfo;
    3391                 :             : 
    3392                 :     1802075 :   for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt_vinfo); i++)
    3393                 :     1262075 :     if (stmt_vinfo)
    3394                 :             :       {
    3395                 :     1259547 :         stmt_vinfo = vect_orig_stmt (stmt_vinfo);
    3396                 :     1259547 :         if (!first
    3397                 :     1259547 :             || get_later_stmt (stmt_vinfo, first) == first)
    3398                 :             :           first = stmt_vinfo;
    3399                 :             :       }
    3400                 :             : 
    3401                 :      540000 :   return first;
    3402                 :             : }
    3403                 :             : 
    3404                 :             : /* Splits a group of stores, currently beginning at FIRST_VINFO, into
    3405                 :             :    two groups: one (still beginning at FIRST_VINFO) of size GROUP1_SIZE
    3406                 :             :    (also containing the first GROUP1_SIZE stmts, since stores are
    3407                 :             :    consecutive), the second containing the remainder.
    3408                 :             :    Return the first stmt in the second group.  */
    3409                 :             : 
    3410                 :             : static stmt_vec_info
    3411                 :      145705 : vect_split_slp_store_group (stmt_vec_info first_vinfo, unsigned group1_size)
    3412                 :             : {
    3413                 :      145705 :   gcc_assert (DR_GROUP_FIRST_ELEMENT (first_vinfo) == first_vinfo);
    3414                 :      145705 :   gcc_assert (group1_size > 0);
    3415                 :      145705 :   int group2_size = DR_GROUP_SIZE (first_vinfo) - group1_size;
    3416                 :      145705 :   gcc_assert (group2_size > 0);
    3417                 :      145705 :   DR_GROUP_SIZE (first_vinfo) = group1_size;
    3418                 :             : 
    3419                 :      145705 :   stmt_vec_info stmt_info = first_vinfo;
    3420                 :      488310 :   for (unsigned i = group1_size; i > 1; i--)
    3421                 :             :     {
    3422                 :      342605 :       stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info);
    3423                 :      342605 :       gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
    3424                 :             :     }
    3425                 :             :   /* STMT is now the last element of the first group.  */
    3426                 :      145705 :   stmt_vec_info group2 = DR_GROUP_NEXT_ELEMENT (stmt_info);
    3427                 :      145705 :   DR_GROUP_NEXT_ELEMENT (stmt_info) = 0;
    3428                 :             : 
    3429                 :      145705 :   DR_GROUP_SIZE (group2) = group2_size;
    3430                 :      408976 :   for (stmt_info = group2; stmt_info;
    3431                 :      263271 :        stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info))
    3432                 :             :     {
    3433                 :      263271 :       DR_GROUP_FIRST_ELEMENT (stmt_info) = group2;
    3434                 :      263271 :       gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
    3435                 :             :     }
    3436                 :             : 
    3437                 :             :   /* For the second group, the DR_GROUP_GAP is that before the original group,
    3438                 :             :      plus skipping over the first vector.  */
    3439                 :      145705 :   DR_GROUP_GAP (group2) = DR_GROUP_GAP (first_vinfo) + group1_size;
    3440                 :             : 
    3441                 :             :   /* DR_GROUP_GAP of the first group now has to skip over the second group too.  */
    3442                 :      145705 :   DR_GROUP_GAP (first_vinfo) += group2_size;
    3443                 :             : 
    3444                 :      145705 :   if (dump_enabled_p ())
    3445                 :          59 :     dump_printf_loc (MSG_NOTE, vect_location, "Split group into %d and %d\n",
    3446                 :             :                      group1_size, group2_size);
    3447                 :             : 
    3448                 :      145705 :   return group2;
    3449                 :             : }
    3450                 :             : 
    3451                 :             : /* Calculate the unrolling factor for an SLP instance with GROUP_SIZE
    3452                 :             :    statements and a vector of NUNITS elements.  */
    3453                 :             : 
    3454                 :             : static poly_uint64
    3455                 :     2838589 : calculate_unrolling_factor (poly_uint64 nunits, unsigned int group_size)
    3456                 :             : {
    3457                 :     2838589 :   return exact_div (common_multiple (nunits, group_size), group_size);
    3458                 :             : }
    3459                 :             : 
    3460                 :             : /* Helper that checks to see if a node is a load node.  */
    3461                 :             : 
    3462                 :             : static inline bool
    3463                 :          88 : vect_is_slp_load_node  (slp_tree root)
    3464                 :             : {
    3465                 :          88 :   return (SLP_TREE_CODE (root) != VEC_PERM_EXPR
    3466                 :          76 :           && SLP_TREE_DEF_TYPE (root) == vect_internal_def
    3467                 :          64 :           && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_REPRESENTATIVE (root))
    3468                 :         128 :           && DR_IS_READ (STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (root))));
    3469                 :             : }
    3470                 :             : 
    3471                 :             : 
    3472                 :             : /* Helper function of optimize_load_redistribution that performs the operation
    3473                 :             :    recursively.  */
    3474                 :             : 
    3475                 :             : static slp_tree
    3476                 :        8600 : optimize_load_redistribution_1 (scalar_stmts_to_slp_tree_map_t *bst_map,
    3477                 :             :                                 vec_info *vinfo, unsigned int group_size,
    3478                 :             :                                 hash_map<slp_tree, slp_tree> *load_map,
    3479                 :             :                                 slp_tree root)
    3480                 :             : {
    3481                 :        8600 :   if (slp_tree *leader = load_map->get (root))
    3482                 :         644 :     return *leader;
    3483                 :             : 
    3484                 :        7956 :   slp_tree node;
    3485                 :        7956 :   unsigned i;
    3486                 :             : 
    3487                 :             :   /* For now, we don't know anything about externals so do not do anything.  */
    3488                 :        7956 :   if (!root || SLP_TREE_DEF_TYPE (root) != vect_internal_def)
    3489                 :             :     return NULL;
    3490                 :        5516 :   else if (SLP_TREE_CODE (root) == VEC_PERM_EXPR)
    3491                 :             :     {
    3492                 :             :       /* First convert this node into a load node and add it to the leaves
    3493                 :             :          list and flatten the permute from a lane to a load one.  If it's
    3494                 :             :          unneeded it will be elided later.  */
    3495                 :          68 :       vec<stmt_vec_info> stmts;
    3496                 :          68 :       stmts.create (SLP_TREE_LANES (root));
    3497                 :          68 :       lane_permutation_t lane_perm = SLP_TREE_LANE_PERMUTATION (root);
    3498                 :         108 :       for (unsigned j = 0; j < lane_perm.length (); j++)
    3499                 :             :         {
    3500                 :          88 :           std::pair<unsigned, unsigned> perm = lane_perm[j];
    3501                 :          88 :           node = SLP_TREE_CHILDREN (root)[perm.first];
    3502                 :             : 
    3503                 :          88 :           if (!vect_is_slp_load_node (node)
    3504                 :          88 :               || SLP_TREE_CHILDREN (node).exists ())
    3505                 :             :             {
    3506                 :          48 :               stmts.release ();
    3507                 :          48 :               goto next;
    3508                 :             :             }
    3509                 :             : 
    3510                 :          40 :           stmts.quick_push (SLP_TREE_SCALAR_STMTS (node)[perm.second]);
    3511                 :             :         }
    3512                 :             : 
    3513                 :          20 :       if (dump_enabled_p ())
    3514                 :           0 :         dump_printf_loc (MSG_NOTE, vect_location,
    3515                 :             :                          "converting stmts on permute node %p\n",
    3516                 :             :                          (void *) root);
    3517                 :             : 
    3518                 :          20 :       bool *matches = XALLOCAVEC (bool, group_size);
    3519                 :          20 :       poly_uint64 max_nunits = 1;
    3520                 :          20 :       unsigned tree_size = 0, limit = 1;
    3521                 :          20 :       node = vect_build_slp_tree (vinfo, stmts, group_size, &max_nunits,
    3522                 :             :                                   matches, &limit, &tree_size, bst_map);
    3523                 :          20 :       if (!node)
    3524                 :           0 :         stmts.release ();
    3525                 :             : 
    3526                 :          20 :       load_map->put (root, node);
    3527                 :          20 :       return node;
    3528                 :             :     }
    3529                 :             : 
    3530                 :        5448 : next:
    3531                 :        5496 :   load_map->put (root, NULL);
    3532                 :             : 
    3533                 :       12728 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i , node)
    3534                 :             :     {
    3535                 :        7232 :       slp_tree value
    3536                 :        7232 :         = optimize_load_redistribution_1 (bst_map, vinfo, group_size, load_map,
    3537                 :             :                                           node);
    3538                 :        7232 :       if (value)
    3539                 :             :         {
    3540                 :          20 :           SLP_TREE_REF_COUNT (value)++;
    3541                 :          20 :           SLP_TREE_CHILDREN (root)[i] = value;
    3542                 :             :           /* ???  We know the original leafs of the replaced nodes will
    3543                 :             :              be referenced by bst_map, only the permutes created by
    3544                 :             :              pattern matching are not.  */
    3545                 :          20 :           if (SLP_TREE_REF_COUNT (node) == 1)
    3546                 :          20 :             load_map->remove (node);
    3547                 :          20 :           vect_free_slp_tree (node);
    3548                 :             :         }
    3549                 :             :     }
    3550                 :             : 
    3551                 :             :   return NULL;
    3552                 :             : }
    3553                 :             : 
    3554                 :             : /* Temporary workaround for loads not being CSEd during SLP build.  This
    3555                 :             :    function will traverse the SLP tree rooted in ROOT for INSTANCE and find
    3556                 :             :    VEC_PERM nodes that blend vectors from multiple nodes that all read from the
    3557                 :             :    same DR such that the final operation is equal to a permuted load.  Such
    3558                 :             :    NODES are then directly converted into LOADS themselves.  The nodes are
    3559                 :             :    CSEd using BST_MAP.  */
    3560                 :             : 
    3561                 :             : static void
    3562                 :        1251 : optimize_load_redistribution (scalar_stmts_to_slp_tree_map_t *bst_map,
    3563                 :             :                               vec_info *vinfo, unsigned int group_size,
    3564                 :             :                               hash_map<slp_tree, slp_tree> *load_map,
    3565                 :             :                               slp_tree root)
    3566                 :             : {
    3567                 :        1251 :   slp_tree node;
    3568                 :        1251 :   unsigned i;
    3569                 :             : 
    3570                 :        2619 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i , node)
    3571                 :             :     {
    3572                 :        1368 :       slp_tree value
    3573                 :        1368 :         = optimize_load_redistribution_1 (bst_map, vinfo, group_size, load_map,
    3574                 :             :                                           node);
    3575                 :        1368 :       if (value)
    3576                 :             :         {
    3577                 :           0 :           SLP_TREE_REF_COUNT (value)++;
    3578                 :           0 :           SLP_TREE_CHILDREN (root)[i] = value;
    3579                 :             :           /* ???  We know the original leafs of the replaced nodes will
    3580                 :             :              be referenced by bst_map, only the permutes created by
    3581                 :             :              pattern matching are not.  */
    3582                 :           0 :           if (SLP_TREE_REF_COUNT (node) == 1)
    3583                 :           0 :             load_map->remove (node);
    3584                 :           0 :           vect_free_slp_tree (node);
    3585                 :             :         }
    3586                 :             :     }
    3587                 :        1251 : }
    3588                 :             : 
    3589                 :             : /* Helper function of vect_match_slp_patterns.
    3590                 :             : 
    3591                 :             :    Attempts to match patterns against the slp tree rooted in REF_NODE using
    3592                 :             :    VINFO.  Patterns are matched in post-order traversal.
    3593                 :             : 
    3594                 :             :    If matching is successful the value in REF_NODE is updated and returned, if
    3595                 :             :    not then it is returned unchanged.  */
    3596                 :             : 
    3597                 :             : static bool
    3598                 :     4566958 : vect_match_slp_patterns_2 (slp_tree *ref_node, vec_info *vinfo,
    3599                 :             :                            slp_tree_to_load_perm_map_t *perm_cache,
    3600                 :             :                            slp_compat_nodes_map_t *compat_cache,
    3601                 :             :                            hash_set<slp_tree> *visited)
    3602                 :             : {
    3603                 :     4566958 :   unsigned i;
    3604                 :     4566958 :   slp_tree node = *ref_node;
    3605                 :     4566958 :   bool found_p = false;
    3606                 :     4566958 :   if (!node || visited->add (node))
    3607                 :      658933 :     return false;
    3608                 :             : 
    3609                 :             :   slp_tree child;
    3610                 :     7273523 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    3611                 :     3365498 :     found_p |= vect_match_slp_patterns_2 (&SLP_TREE_CHILDREN (node)[i],
    3612                 :             :                                           vinfo, perm_cache, compat_cache,
    3613                 :             :                                           visited);
    3614                 :             : 
    3615                 :    11724075 :   for (unsigned x = 0; x < num__slp_patterns; x++)
    3616                 :             :     {
    3617                 :     7816050 :       vect_pattern *pattern
    3618                 :     7816050 :         = slp_patterns[x] (perm_cache, compat_cache, ref_node);
    3619                 :     7816050 :       if (pattern)
    3620                 :             :         {
    3621                 :         448 :           pattern->build (vinfo);
    3622                 :         448 :           delete pattern;
    3623                 :         448 :           found_p = true;
    3624                 :             :         }
    3625                 :             :     }
    3626                 :             : 
    3627                 :             :   return found_p;
    3628                 :             : }
    3629                 :             : 
    3630                 :             : /* Applies pattern matching to the given SLP tree rooted in REF_NODE using
    3631                 :             :    vec_info VINFO.
    3632                 :             : 
    3633                 :             :    The modified tree is returned.  Patterns are tried in order and multiple
    3634                 :             :    patterns may match.  */
    3635                 :             : 
    3636                 :             : static bool
    3637                 :     1201460 : vect_match_slp_patterns (slp_instance instance, vec_info *vinfo,
    3638                 :             :                          hash_set<slp_tree> *visited,
    3639                 :             :                          slp_tree_to_load_perm_map_t *perm_cache,
    3640                 :             :                          slp_compat_nodes_map_t *compat_cache)
    3641                 :             : {
    3642                 :     1201460 :   DUMP_VECT_SCOPE ("vect_match_slp_patterns");
    3643                 :     1201460 :   slp_tree *ref_node = &SLP_INSTANCE_TREE (instance);
    3644                 :             : 
    3645                 :     1201460 :   if (dump_enabled_p ())
    3646                 :       32217 :     dump_printf_loc (MSG_NOTE, vect_location,
    3647                 :             :                      "Analyzing SLP tree %p for patterns\n",
    3648                 :       32217 :                      (void *) SLP_INSTANCE_TREE (instance));
    3649                 :             : 
    3650                 :     1201460 :   return vect_match_slp_patterns_2 (ref_node, vinfo, perm_cache, compat_cache,
    3651                 :     1201460 :                                     visited);
    3652                 :             : }
    3653                 :             : 
    3654                 :             : /* STMT_INFO is a store group of size GROUP_SIZE that we are considering
    3655                 :             :    vectorizing with VECTYPE that might be NULL.  MASKED_P indicates whether
    3656                 :             :    the stores are masked.
    3657                 :             :    Return true if we could use IFN_STORE_LANES instead and if that appears
    3658                 :             :    to be the better approach.  */
    3659                 :             : 
    3660                 :             : static bool
    3661                 :        4208 : vect_slp_prefer_store_lanes_p (vec_info *vinfo, stmt_vec_info stmt_info,
    3662                 :             :                                tree vectype, bool masked_p,
    3663                 :             :                                unsigned int group_size,
    3664                 :             :                                unsigned int new_group_size)
    3665                 :             : {
    3666                 :        4208 :   if (!vectype)
    3667                 :             :     {
    3668                 :        4208 :       tree scalar_type = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
    3669                 :        4208 :       vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
    3670                 :             :     }
    3671                 :        4208 :   if (!vectype)
    3672                 :             :     return false;
    3673                 :             :   /* Allow the split if one of the two new groups would operate on full
    3674                 :             :      vectors *within* rather than across one scalar loop iteration.
    3675                 :             :      This is purely a heuristic, but it should work well for group
    3676                 :             :      sizes of 3 and 4, where the possible splits are:
    3677                 :             : 
    3678                 :             :        3->2+1:  OK if the vector has exactly two elements
    3679                 :             :        4->2+2:  Likewise
    3680                 :             :        4->3+1:  Less clear-cut.  */
    3681                 :        4208 :   if (multiple_p (group_size - new_group_size, TYPE_VECTOR_SUBPARTS (vectype))
    3682                 :        2643 :       || multiple_p (new_group_size, TYPE_VECTOR_SUBPARTS (vectype)))
    3683                 :        1565 :     return false;
    3684                 :        2643 :   return vect_store_lanes_supported (vectype, group_size, masked_p) != IFN_LAST;
    3685                 :             : }
    3686                 :             : 
    3687                 :             : /* Analyze an SLP instance starting from a group of grouped stores.  Call
    3688                 :             :    vect_build_slp_tree to build a tree of packed stmts if possible.
    3689                 :             :    Return FALSE if it's impossible to SLP any stmt in the loop.  */
    3690                 :             : 
    3691                 :             : static bool
    3692                 :             : vect_analyze_slp_instance (vec_info *vinfo,
    3693                 :             :                            scalar_stmts_to_slp_tree_map_t *bst_map,
    3694                 :             :                            stmt_vec_info stmt_info, slp_instance_kind kind,
    3695                 :             :                            unsigned max_tree_size, unsigned *limit,
    3696                 :             :                            bool force_single_lane);
    3697                 :             : 
    3698                 :             : /* Build an interleaving scheme for the store sources RHS_NODES from
    3699                 :             :    SCALAR_STMTS.  */
    3700                 :             : 
    3701                 :             : static slp_tree
    3702                 :        6102 : vect_build_slp_store_interleaving (vec<slp_tree> &rhs_nodes,
    3703                 :             :                                    vec<stmt_vec_info> &scalar_stmts,
    3704                 :             :                                    poly_uint64 max_nunits)
    3705                 :             : {
    3706                 :        6102 :   unsigned int group_size = scalar_stmts.length ();
    3707                 :       12204 :   slp_tree node = vect_create_new_slp_node (scalar_stmts,
    3708                 :        6102 :                                             SLP_TREE_CHILDREN
    3709                 :             :                                               (rhs_nodes[0]).length ());
    3710                 :        6102 :   SLP_TREE_VECTYPE (node) = SLP_TREE_VECTYPE (rhs_nodes[0]);
    3711                 :        6102 :   node->max_nunits = max_nunits;
    3712                 :        6102 :   for (unsigned l = 0;
    3713                 :       12239 :        l < SLP_TREE_CHILDREN (rhs_nodes[0]).length (); ++l)
    3714                 :             :     {
    3715                 :             :       /* And a permute merging all RHS SLP trees.  */
    3716                 :        6137 :       slp_tree perm = vect_create_new_slp_node (rhs_nodes.length (),
    3717                 :        6137 :                                                 VEC_PERM_EXPR);
    3718                 :        6137 :       SLP_TREE_CHILDREN (node).quick_push (perm);
    3719                 :        6137 :       SLP_TREE_LANE_PERMUTATION (perm).create (group_size);
    3720                 :        6137 :       SLP_TREE_VECTYPE (perm) = SLP_TREE_VECTYPE (node);
    3721                 :        6137 :       perm->max_nunits = max_nunits;
    3722                 :        6137 :       SLP_TREE_LANES (perm) = group_size;
    3723                 :             :       /* ???  We should set this NULL but that's not expected.  */
    3724                 :        6137 :       SLP_TREE_REPRESENTATIVE (perm)
    3725                 :        6137 :         = SLP_TREE_REPRESENTATIVE (SLP_TREE_CHILDREN (rhs_nodes[0])[l]);
    3726                 :       24143 :       for (unsigned j = 0; j < rhs_nodes.length (); ++j)
    3727                 :             :         {
    3728                 :       18006 :           SLP_TREE_CHILDREN (perm)
    3729                 :       18006 :             .quick_push (SLP_TREE_CHILDREN (rhs_nodes[j])[l]);
    3730                 :       18006 :           SLP_TREE_CHILDREN (rhs_nodes[j])[l]->refcnt++;
    3731                 :       18006 :           for (unsigned k = 0;
    3732                 :       38213 :                k < SLP_TREE_SCALAR_STMTS (rhs_nodes[j]).length (); ++k)
    3733                 :             :             {
    3734                 :             :               /* ???  We should populate SLP_TREE_SCALAR_STMTS
    3735                 :             :                  or SLP_TREE_SCALAR_OPS but then we might have
    3736                 :             :                  a mix of both in our children.  */
    3737                 :       20207 :               SLP_TREE_LANE_PERMUTATION (perm)
    3738                 :       20207 :                 .quick_push (std::make_pair (j, k));
    3739                 :             :             }
    3740                 :             :         }
    3741                 :             : 
    3742                 :             :       /* Now we have a single permute node but we cannot code-generate
    3743                 :             :          the case with more than two inputs.
    3744                 :             :          Perform pairwise reduction, reducing the two inputs
    3745                 :             :          with the least number of lanes to one and then repeat until
    3746                 :             :          we end up with two inputs.  That scheme makes sure we end
    3747                 :             :          up with permutes satisfying the restriction of requiring at
    3748                 :             :          most two vector inputs to produce a single vector output
    3749                 :             :          when the number of lanes is even.  */
    3750                 :       11869 :       while (SLP_TREE_CHILDREN (perm).length () > 2)
    3751                 :             :         {
    3752                 :             :           /* When we have three equal sized groups left the pairwise
    3753                 :             :              reduction does not result in a scheme that avoids using
    3754                 :             :              three vectors.  Instead merge the first two groups
    3755                 :             :              to the final size with do-not-care elements (chosen
    3756                 :             :              from the first group) and then merge with the third.
    3757                 :             :                   { A0, B0,  x, A1, B1,  x, ... }
    3758                 :             :                -> { A0, B0, C0, A1, B1, C1, ... }
    3759                 :             :              This handles group size of three (and at least
    3760                 :             :              power-of-two multiples of that).  */
    3761                 :        5732 :           if (SLP_TREE_CHILDREN (perm).length () == 3
    3762                 :        2620 :               && (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[0])
    3763                 :        2620 :                   == SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[1]))
    3764                 :        5732 :               && (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[0])
    3765                 :        1778 :                   == SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[2])))
    3766                 :             :             {
    3767                 :        1591 :               int ai = 0;
    3768                 :        1591 :               int bi = 1;
    3769                 :        1591 :               slp_tree a = SLP_TREE_CHILDREN (perm)[ai];
    3770                 :        1591 :               slp_tree b = SLP_TREE_CHILDREN (perm)[bi];
    3771                 :        1591 :               unsigned n = SLP_TREE_LANES (perm);
    3772                 :             : 
    3773                 :        1591 :               slp_tree permab = vect_create_new_slp_node (2, VEC_PERM_EXPR);
    3774                 :        1591 :               SLP_TREE_LANES (permab) = n;
    3775                 :        1591 :               SLP_TREE_LANE_PERMUTATION (permab).create (n);
    3776                 :        1591 :               SLP_TREE_VECTYPE (permab) = SLP_TREE_VECTYPE (perm);
    3777                 :        1591 :               permab->max_nunits = max_nunits;
    3778                 :             :               /* ???  Should be NULL but that's not expected.  */
    3779                 :        1591 :               SLP_TREE_REPRESENTATIVE (permab) = SLP_TREE_REPRESENTATIVE (perm);
    3780                 :        1591 :               SLP_TREE_CHILDREN (permab).quick_push (a);
    3781                 :        3193 :               for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
    3782                 :        1602 :                 SLP_TREE_LANE_PERMUTATION (permab)
    3783                 :        1602 :                   .quick_push (std::make_pair (0, k));
    3784                 :        1591 :               SLP_TREE_CHILDREN (permab).quick_push (b);
    3785                 :        3193 :               for (unsigned k = 0; k < SLP_TREE_LANES (b); ++k)
    3786                 :        1602 :                 SLP_TREE_LANE_PERMUTATION (permab)
    3787                 :        1602 :                   .quick_push (std::make_pair (1, k));
    3788                 :             :               /* Push the do-not-care lanes.  */
    3789                 :        3193 :               for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
    3790                 :        1602 :                 SLP_TREE_LANE_PERMUTATION (permab)
    3791                 :        1602 :                   .quick_push (std::make_pair (0, k));
    3792                 :             : 
    3793                 :             :               /* Put the merged node into 'perm', in place of a.  */
    3794                 :        1591 :               SLP_TREE_CHILDREN (perm)[ai] = permab;
    3795                 :             :               /* Adjust the references to b in the permutation
    3796                 :             :                  of perm and to the later children which we'll
    3797                 :             :                  remove.  */
    3798                 :        6397 :               for (unsigned k = 0; k < SLP_TREE_LANES (perm); ++k)
    3799                 :             :                 {
    3800                 :        4806 :                   std::pair<unsigned, unsigned> &p
    3801                 :        4806 :                     = SLP_TREE_LANE_PERMUTATION (perm)[k];
    3802                 :        4806 :                   if (p.first == (unsigned) bi)
    3803                 :             :                     {
    3804                 :        1602 :                       p.first = ai;
    3805                 :        1602 :                       p.second += SLP_TREE_LANES (a);
    3806                 :             :                     }
    3807                 :        3204 :                   else if (p.first > (unsigned) bi)
    3808                 :        1602 :                     p.first--;
    3809                 :             :                 }
    3810                 :        1591 :               SLP_TREE_CHILDREN (perm).ordered_remove (bi);
    3811                 :        1591 :               break;
    3812                 :             :             }
    3813                 :             : 
    3814                 :             :           /* Pick the two nodes with the least number of lanes,
    3815                 :             :              prefer the earliest candidate and maintain ai < bi.  */
    3816                 :             :           int ai = -1;
    3817                 :             :           int bi = -1;
    3818                 :       35360 :           for (unsigned ci = 0; ci < SLP_TREE_CHILDREN (perm).length (); ++ci)
    3819                 :             :             {
    3820                 :       31219 :               if (ai == -1)
    3821                 :        4141 :                 ai = ci;
    3822                 :       27078 :               else if (bi == -1)
    3823                 :        4141 :                 bi = ci;
    3824                 :       22937 :               else if ((SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ci])
    3825                 :       22937 :                         < SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ai]))
    3826                 :       22937 :                        || (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ci])
    3827                 :       18682 :                            < SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[bi])))
    3828                 :             :                 {
    3829                 :        9376 :                   if (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ai])
    3830                 :        4688 :                       <= SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[bi]))
    3831                 :        2174 :                     bi = ci;
    3832                 :             :                   else
    3833                 :             :                     {
    3834                 :        2514 :                       ai = bi;
    3835                 :        2514 :                       bi = ci;
    3836                 :             :                     }
    3837                 :             :                 }
    3838                 :             :             }
    3839                 :             : 
    3840                 :             :           /* Produce a merge of nodes ai and bi.  */
    3841                 :        4141 :           slp_tree a = SLP_TREE_CHILDREN (perm)[ai];
    3842                 :        4141 :           slp_tree b = SLP_TREE_CHILDREN (perm)[bi];
    3843                 :        4141 :           unsigned n = SLP_TREE_LANES (a) + SLP_TREE_LANES (b);
    3844                 :        4141 :           slp_tree permab = vect_create_new_slp_node (2, VEC_PERM_EXPR);
    3845                 :        4141 :           SLP_TREE_LANES (permab) = n;
    3846                 :        4141 :           SLP_TREE_LANE_PERMUTATION (permab).create (n);
    3847                 :        4141 :           SLP_TREE_VECTYPE (permab) = SLP_TREE_VECTYPE (perm);
    3848                 :        4141 :           permab->max_nunits = max_nunits;
    3849                 :             :           /* ???  Should be NULL but that's not expected.  */
    3850                 :        4141 :           SLP_TREE_REPRESENTATIVE (permab) = SLP_TREE_REPRESENTATIVE (perm);
    3851                 :        4141 :           SLP_TREE_CHILDREN (permab).quick_push (a);
    3852                 :       10667 :           for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
    3853                 :        6526 :             SLP_TREE_LANE_PERMUTATION (permab)
    3854                 :        6526 :               .quick_push (std::make_pair (0, k));
    3855                 :        4141 :           SLP_TREE_CHILDREN (permab).quick_push (b);
    3856                 :       10175 :           for (unsigned k = 0; k < SLP_TREE_LANES (b); ++k)
    3857                 :        6034 :             SLP_TREE_LANE_PERMUTATION (permab)
    3858                 :        6034 :               .quick_push (std::make_pair (1, k));
    3859                 :             : 
    3860                 :             :           /* Put the merged node into 'perm', in place of a.  */
    3861                 :        4141 :           SLP_TREE_CHILDREN (perm)[ai] = permab;
    3862                 :             :           /* Adjust the references to b in the permutation
    3863                 :             :              of perm and to the later children which we'll
    3864                 :             :              remove.  */
    3865                 :       55734 :           for (unsigned k = 0; k < SLP_TREE_LANES (perm); ++k)
    3866                 :             :             {
    3867                 :       51593 :               std::pair<unsigned, unsigned> &p
    3868                 :       51593 :                 = SLP_TREE_LANE_PERMUTATION (perm)[k];
    3869                 :       51593 :               if (p.first == (unsigned) bi)
    3870                 :             :                 {
    3871                 :        6034 :                   p.first = ai;
    3872                 :        6034 :                   p.second += SLP_TREE_LANES (a);
    3873                 :             :                 }
    3874                 :       45559 :               else if (p.first > (unsigned) bi)
    3875                 :       18738 :                 p.first--;
    3876                 :             :             }
    3877                 :        4141 :           SLP_TREE_CHILDREN (perm).ordered_remove (bi);
    3878                 :             :         }
    3879                 :             :     }
    3880                 :             : 
    3881                 :        6102 :   return node;
    3882                 :             : }
    3883                 :             : 
    3884                 :             : /* Analyze an SLP instance starting from SCALAR_STMTS which are a group
    3885                 :             :    of KIND.  Return true if successful.  */
    3886                 :             : 
    3887                 :             : static bool
    3888                 :     2568337 : vect_build_slp_instance (vec_info *vinfo,
    3889                 :             :                          slp_instance_kind kind,
    3890                 :             :                          vec<stmt_vec_info> &scalar_stmts,
    3891                 :             :                          vec<stmt_vec_info> &root_stmt_infos,
    3892                 :             :                          vec<tree> &remain,
    3893                 :             :                          unsigned max_tree_size, unsigned *limit,
    3894                 :             :                          scalar_stmts_to_slp_tree_map_t *bst_map,
    3895                 :             :                          /* ???  We need stmt_info for group splitting.  */
    3896                 :             :                          stmt_vec_info stmt_info_,
    3897                 :             :                          bool force_single_lane)
    3898                 :             : {
    3899                 :             :   /* If there's no budget left bail out early.  */
    3900                 :     2568337 :   if (*limit == 0)
    3901                 :             :     return false;
    3902                 :             : 
    3903                 :     2543007 :   if (kind == slp_inst_kind_ctor)
    3904                 :             :     {
    3905                 :        7666 :       if (dump_enabled_p ())
    3906                 :          66 :         dump_printf_loc (MSG_NOTE, vect_location,
    3907                 :             :                          "Analyzing vectorizable constructor: %G\n",
    3908                 :          33 :                          root_stmt_infos[0]->stmt);
    3909                 :             :     }
    3910                 :     2535341 :   else if (kind == slp_inst_kind_gcond)
    3911                 :             :     {
    3912                 :       63413 :       if (dump_enabled_p ())
    3913                 :        3246 :         dump_printf_loc (MSG_NOTE, vect_location,
    3914                 :             :                          "Analyzing vectorizable control flow: %G",
    3915                 :        1623 :                          root_stmt_infos[0]->stmt);
    3916                 :             :     }
    3917                 :             : 
    3918                 :     2543007 :   if (dump_enabled_p ())
    3919                 :             :     {
    3920                 :       35720 :       dump_printf_loc (MSG_NOTE, vect_location,
    3921                 :             :                        "Starting SLP discovery for\n");
    3922                 :       91162 :       for (unsigned i = 0; i < scalar_stmts.length (); ++i)
    3923                 :      110884 :         dump_printf_loc (MSG_NOTE, vect_location,
    3924                 :       55442 :                          "  %G", scalar_stmts[i]->stmt);
    3925                 :             :     }
    3926                 :             : 
    3927                 :             :   /* Build the tree for the SLP instance.  */
    3928                 :     2543007 :   unsigned int group_size = scalar_stmts.length ();
    3929                 :     2543007 :   bool *matches = XALLOCAVEC (bool, group_size);
    3930                 :     2543007 :   poly_uint64 max_nunits = 1;
    3931                 :     2543007 :   unsigned tree_size = 0;
    3932                 :     2543007 :   unsigned i;
    3933                 :             : 
    3934                 :     2543007 :   slp_tree node = NULL;
    3935                 :     2543007 :   if (group_size > 1 && force_single_lane)
    3936                 :             :     {
    3937                 :        1683 :       matches[0] = true;
    3938                 :        1683 :       matches[1] = false;
    3939                 :             :     }
    3940                 :             :   else
    3941                 :     2541324 :     node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
    3942                 :             :                                 &max_nunits, matches, limit,
    3943                 :             :                                 &tree_size, bst_map);
    3944                 :     2543007 :   if (node != NULL)
    3945                 :             :     {
    3946                 :             :       /* Calculate the unrolling factor based on the smallest type.  */
    3947                 :     1195358 :       poly_uint64 unrolling_factor
    3948                 :     1195358 :         = calculate_unrolling_factor (max_nunits, group_size);
    3949                 :             : 
    3950                 :     1195358 :       if (maybe_ne (unrolling_factor, 1U)
    3951                 :     1195358 :           && is_a <bb_vec_info> (vinfo))
    3952                 :             :         {
    3953                 :           0 :           unsigned HOST_WIDE_INT const_max_nunits;
    3954                 :           0 :           if (!max_nunits.is_constant (&const_max_nunits)
    3955                 :           0 :               || const_max_nunits > group_size)
    3956                 :             :             {
    3957                 :           0 :               if (dump_enabled_p ())
    3958                 :           0 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3959                 :             :                                  "Build SLP failed: store group "
    3960                 :             :                                  "size not a multiple of the vector size "
    3961                 :             :                                  "in basic block SLP\n");
    3962                 :           0 :               vect_free_slp_tree (node);
    3963                 :           0 :               return false;
    3964                 :             :             }
    3965                 :             :           /* Fatal mismatch.  */
    3966                 :           0 :           if (dump_enabled_p ())
    3967                 :           0 :             dump_printf_loc (MSG_NOTE, vect_location,
    3968                 :             :                              "SLP discovery succeeded but node needs "
    3969                 :             :                              "splitting\n");
    3970                 :           0 :           memset (matches, true, group_size);
    3971                 :           0 :           matches[group_size / const_max_nunits * const_max_nunits] = false;
    3972                 :           0 :           vect_free_slp_tree (node);
    3973                 :             :         }
    3974                 :             :       else
    3975                 :             :         {
    3976                 :             :           /* Create a new SLP instance.  */
    3977                 :     1195358 :           slp_instance new_instance = XNEW (class _slp_instance);
    3978                 :     1195358 :           SLP_INSTANCE_TREE (new_instance) = node;
    3979                 :     1195358 :           SLP_INSTANCE_LOADS (new_instance) = vNULL;
    3980                 :     1195358 :           SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
    3981                 :     1195358 :           SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
    3982                 :     1195358 :           SLP_INSTANCE_KIND (new_instance) = kind;
    3983                 :     1195358 :           new_instance->reduc_phis = NULL;
    3984                 :     1195358 :           new_instance->cost_vec = vNULL;
    3985                 :     1195358 :           new_instance->subgraph_entries = vNULL;
    3986                 :             : 
    3987                 :     1195358 :           if (dump_enabled_p ())
    3988                 :       31387 :             dump_printf_loc (MSG_NOTE, vect_location,
    3989                 :             :                              "SLP size %u vs. limit %u.\n",
    3990                 :             :                              tree_size, max_tree_size);
    3991                 :             : 
    3992                 :             :           /* Fixup SLP reduction chains.  */
    3993                 :     1195358 :           if (kind == slp_inst_kind_reduc_chain)
    3994                 :             :             {
    3995                 :             :               /* If this is a reduction chain with a conversion in front
    3996                 :             :                  amend the SLP tree with a node for that.  */
    3997                 :         343 :               gimple *scalar_def
    3998                 :         343 :                 = vect_orig_stmt (scalar_stmts[group_size - 1])->stmt;
    3999                 :         343 :               if (STMT_VINFO_DEF_TYPE (scalar_stmts[0]) != vect_reduction_def)
    4000                 :             :                 {
    4001                 :             :                   /* Get at the conversion stmt - we know it's the single use
    4002                 :             :                      of the last stmt of the reduction chain.  */
    4003                 :          48 :                   use_operand_p use_p;
    4004                 :          48 :                   bool r = single_imm_use (gimple_assign_lhs (scalar_def),
    4005                 :             :                                            &use_p, &scalar_def);
    4006                 :          48 :                   gcc_assert (r);
    4007                 :          48 :                   stmt_vec_info next_info = vinfo->lookup_stmt (scalar_def);
    4008                 :          48 :                   next_info = vect_stmt_to_vectorize (next_info);
    4009                 :          48 :                   scalar_stmts = vNULL;
    4010                 :          48 :                   scalar_stmts.create (group_size);
    4011                 :         150 :                   for (unsigned i = 0; i < group_size; ++i)
    4012                 :         102 :                     scalar_stmts.quick_push (next_info);
    4013                 :          48 :                   slp_tree conv = vect_create_new_slp_node (scalar_stmts, 1);
    4014                 :          48 :                   SLP_TREE_VECTYPE (conv) = STMT_VINFO_VECTYPE (next_info);
    4015                 :          48 :                   SLP_TREE_CHILDREN (conv).quick_push (node);
    4016                 :          48 :                   SLP_INSTANCE_TREE (new_instance) = conv;
    4017                 :             :                   /* We also have to fake this conversion stmt as SLP reduction
    4018                 :             :                      group so we don't have to mess with too much code
    4019                 :             :                      elsewhere.  */
    4020                 :          48 :                   REDUC_GROUP_FIRST_ELEMENT (next_info) = next_info;
    4021                 :          48 :                   REDUC_GROUP_NEXT_ELEMENT (next_info) = NULL;
    4022                 :             :                 }
    4023                 :             :               /* Fill the backedge child of the PHI SLP node.  The
    4024                 :             :                  general matching code cannot find it because the
    4025                 :             :                  scalar code does not reflect how we vectorize the
    4026                 :             :                  reduction.  */
    4027                 :         343 :               use_operand_p use_p;
    4028                 :         343 :               imm_use_iterator imm_iter;
    4029                 :         343 :               class loop *loop = LOOP_VINFO_LOOP (as_a <loop_vec_info> (vinfo));
    4030                 :        1092 :               FOR_EACH_IMM_USE_FAST (use_p, imm_iter,
    4031                 :             :                                      gimple_get_lhs (scalar_def))
    4032                 :             :                 /* There are exactly two non-debug uses, the reduction
    4033                 :             :                    PHI and the loop-closed PHI node.  */
    4034                 :         749 :                 if (!is_gimple_debug (USE_STMT (use_p))
    4035                 :         749 :                     && gimple_bb (USE_STMT (use_p)) == loop->header)
    4036                 :             :                   {
    4037                 :         343 :                     auto_vec<stmt_vec_info, 64> phis (group_size);
    4038                 :         343 :                     stmt_vec_info phi_info
    4039                 :         343 :                       = vinfo->lookup_stmt (USE_STMT (use_p));
    4040                 :        2128 :                     for (unsigned i = 0; i < group_size; ++i)
    4041                 :        1785 :                       phis.quick_push (phi_info);
    4042                 :         343 :                     slp_tree *phi_node = bst_map->get (phis);
    4043                 :         343 :                     unsigned dest_idx = loop_latch_edge (loop)->dest_idx;
    4044                 :         686 :                     SLP_TREE_CHILDREN (*phi_node)[dest_idx]
    4045                 :         343 :                       = SLP_INSTANCE_TREE (new_instance);
    4046                 :         343 :                     SLP_INSTANCE_TREE (new_instance)->refcnt++;
    4047                 :         343 :                   }
    4048                 :             :             }
    4049                 :             : 
    4050                 :     1195358 :           vinfo->slp_instances.safe_push (new_instance);
    4051                 :             : 
    4052                 :             :           /* ???  We've replaced the old SLP_INSTANCE_GROUP_SIZE with
    4053                 :             :              the number of scalar stmts in the root in a few places.
    4054                 :             :              Verify that assumption holds.  */
    4055                 :     2390716 :           gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
    4056                 :             :                         .length () == group_size);
    4057                 :             : 
    4058                 :     1195358 :           if (dump_enabled_p ())
    4059                 :             :             {
    4060                 :       31387 :               dump_printf_loc (MSG_NOTE, vect_location,
    4061                 :             :                                "Final SLP tree for instance %p:\n",
    4062                 :             :                                (void *) new_instance);
    4063                 :       31387 :               vect_print_slp_graph (MSG_NOTE, vect_location,
    4064                 :             :                                     SLP_INSTANCE_TREE (new_instance));
    4065                 :             :             }
    4066                 :             : 
    4067                 :     1195358 :           return true;
    4068                 :             :         }
    4069                 :             :     }
    4070                 :             :   /* Failed to SLP.  */
    4071                 :             : 
    4072                 :     1347649 :   stmt_vec_info stmt_info = stmt_info_;
    4073                 :             :   /* Try to break the group up into pieces.  */
    4074                 :     1347649 :   if (*limit > 0 && kind == slp_inst_kind_store)
    4075                 :             :     {
    4076                 :             :       /* ???  We could delay all the actual splitting of store-groups
    4077                 :             :          until after SLP discovery of the original group completed.
    4078                 :             :          Then we can recurse to vect_build_slp_instance directly.  */
    4079                 :     1027455 :       for (i = 0; i < group_size; i++)
    4080                 :     1027455 :         if (!matches[i])
    4081                 :             :           break;
    4082                 :             : 
    4083                 :             :       /* For basic block SLP, try to break the group up into multiples of
    4084                 :             :          a vector size.  */
    4085                 :      352863 :       if (is_a <bb_vec_info> (vinfo)
    4086                 :      352863 :           && (i > 1 && i < group_size))
    4087                 :             :         {
    4088                 :             :           /* Free the allocated memory.  */
    4089                 :      143817 :           scalar_stmts.release ();
    4090                 :             : 
    4091                 :      143817 :           tree scalar_type
    4092                 :      143817 :             = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
    4093                 :      287634 :           tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
    4094                 :      143817 :                                                       1 << floor_log2 (i));
    4095                 :      143817 :           unsigned HOST_WIDE_INT const_nunits;
    4096                 :      143817 :           if (vectype
    4097                 :      143817 :               && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits))
    4098                 :             :             {
    4099                 :             :               /* Split into two groups at the first vector boundary.  */
    4100                 :      143817 :               gcc_assert ((const_nunits & (const_nunits - 1)) == 0);
    4101                 :      143817 :               unsigned group1_size = i & ~(const_nunits - 1);
    4102                 :             : 
    4103                 :      143817 :               if (dump_enabled_p ())
    4104                 :          57 :                 dump_printf_loc (MSG_NOTE, vect_location,
    4105                 :             :                                  "Splitting SLP group at stmt %u\n", i);
    4106                 :      143817 :               stmt_vec_info rest = vect_split_slp_store_group (stmt_info,
    4107                 :             :                                                                group1_size);
    4108                 :      143817 :               bool res = vect_analyze_slp_instance (vinfo, bst_map, stmt_info,
    4109                 :             :                                                     kind, max_tree_size,
    4110                 :             :                                                     limit, false);
    4111                 :             :               /* Split the rest at the failure point and possibly
    4112                 :             :                  re-analyze the remaining matching part if it has
    4113                 :             :                  at least two lanes.  */
    4114                 :      143817 :               if (group1_size < i
    4115                 :        4656 :                   && (i + 1 < group_size
    4116                 :        2790 :                       || i - group1_size > 1))
    4117                 :             :                 {
    4118                 :        1888 :                   stmt_vec_info rest2 = rest;
    4119                 :        1888 :                   rest = vect_split_slp_store_group (rest, i - group1_size);
    4120                 :        1888 :                   if (i - group1_size > 1)
    4121                 :          47 :                     res |= vect_analyze_slp_instance (vinfo, bst_map, rest2,
    4122                 :             :                                                       kind, max_tree_size,
    4123                 :             :                                                       limit, false);
    4124                 :             :                 }
    4125                 :             :               /* Re-analyze the non-matching tail if it has at least
    4126                 :             :                  two lanes.  */
    4127                 :      143817 :               if (i + 1 < group_size)
    4128                 :       20542 :                 res |= vect_analyze_slp_instance (vinfo, bst_map,
    4129                 :             :                                                   rest, kind, max_tree_size,
    4130                 :             :                                                   limit, false);
    4131                 :      143817 :               return res;
    4132                 :             :             }
    4133                 :             :         }
    4134                 :             : 
    4135                 :             :       /* For loop vectorization split the RHS into arbitrary pieces of
    4136                 :             :          size >= 1.  */
    4137                 :      209046 :       else if (is_a <loop_vec_info> (vinfo)
    4138                 :      209046 :                && (group_size != 1 && i < group_size))
    4139                 :             :         {
    4140                 :        6336 :           gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
    4141                 :          35 :           bool masked_p = call
    4142                 :          35 :               && gimple_call_internal_p (call)
    4143                 :          35 :               && internal_fn_mask_index (gimple_call_internal_fn (call)) != -1;
    4144                 :             :           /* There are targets that cannot do even/odd interleaving schemes
    4145                 :             :              so they absolutely need to use load/store-lanes.  For now
    4146                 :             :              force single-lane SLP for them - they would be happy with
    4147                 :             :              uniform power-of-two lanes (but depending on element size),
    4148                 :             :              but even if we can use 'i' as indicator we would need to
    4149                 :             :              backtrack when later lanes fail to discover with the same
    4150                 :             :              granularity.  We cannot turn any of strided or scatter store
    4151                 :             :              into store-lanes.  */
    4152                 :             :           /* ???  If this is not in sync with what get_load_store_type
    4153                 :             :              later decides the SLP representation is not good for other
    4154                 :             :              store vectorization methods.  */
    4155                 :        6336 :           bool want_store_lanes
    4156                 :        6336 :             = (! STMT_VINFO_GATHER_SCATTER_P (stmt_info)
    4157                 :        6336 :                && ! STMT_VINFO_STRIDED_P (stmt_info)
    4158                 :        4256 :                && ! STMT_VINFO_SLP_VECT_ONLY (stmt_info)
    4159                 :        4245 :                && compare_step_with_zero (vinfo, stmt_info) > 0
    4160                 :       10544 :                && vect_slp_prefer_store_lanes_p (vinfo, stmt_info, NULL_TREE,
    4161                 :       12672 :                                                  masked_p, group_size, 1));
    4162                 :        6336 :           if (want_store_lanes || force_single_lane)
    4163                 :             :             i = 1;
    4164                 :             : 
    4165                 :             :           /* A fatal discovery fail doesn't always mean single-lane SLP
    4166                 :             :              isn't a possibility, so try.  */
    4167                 :        4653 :           if (i == 0)
    4168                 :             :             i = 1;
    4169                 :             : 
    4170                 :        6336 :           if (dump_enabled_p ())
    4171                 :         868 :             dump_printf_loc (MSG_NOTE, vect_location,
    4172                 :             :                              "Splitting SLP group at stmt %u\n", i);
    4173                 :             : 
    4174                 :             :           /* Analyze the stored values and pinch them together with
    4175                 :             :              a permute node so we can preserve the whole store group.  */
    4176                 :        6336 :           auto_vec<slp_tree> rhs_nodes;
    4177                 :        6336 :           poly_uint64 max_nunits = 1;
    4178                 :             : 
    4179                 :        6336 :           unsigned int rhs_common_nlanes = 0;
    4180                 :        6336 :           unsigned int start = 0, end = i;
    4181                 :       28250 :           while (start < group_size)
    4182                 :             :             {
    4183                 :       22148 :               gcc_assert (end - start >= 1);
    4184                 :       22148 :               vec<stmt_vec_info> substmts;
    4185                 :       22148 :               substmts.create (end - start);
    4186                 :       67755 :               for (unsigned j = start; j < end; ++j)
    4187                 :       45607 :                 substmts.quick_push (scalar_stmts[j]);
    4188                 :       22148 :               max_nunits = 1;
    4189                 :       22148 :               node = vect_build_slp_tree (vinfo, substmts, end - start,
    4190                 :             :                                           &max_nunits,
    4191                 :             :                                           matches, limit, &tree_size, bst_map);
    4192                 :       22148 :               if (node)
    4193                 :             :                 {
    4194                 :       17918 :                   rhs_nodes.safe_push (node);
    4195                 :       17918 :                   vect_update_max_nunits (&max_nunits, node->max_nunits);
    4196                 :       17918 :                   if (start == 0)
    4197                 :        6102 :                     rhs_common_nlanes = SLP_TREE_LANES (node);
    4198                 :       11816 :                   else if (rhs_common_nlanes != SLP_TREE_LANES (node))
    4199                 :        1387 :                     rhs_common_nlanes = 0;
    4200                 :       17918 :                   start = end;
    4201                 :       17918 :                   if (want_store_lanes || force_single_lane)
    4202                 :        5043 :                     end = start + 1;
    4203                 :             :                   else
    4204                 :             :                     end = group_size;
    4205                 :             :                 }
    4206                 :             :               else
    4207                 :             :                 {
    4208                 :        4230 :                   substmts.release ();
    4209                 :        4230 :                   if (end - start == 1)
    4210                 :             :                     {
    4211                 :             :                       /* Single-lane discovery failed.  Free ressources.  */
    4212                 :         234 :                       for (auto node : rhs_nodes)
    4213                 :           0 :                         vect_free_slp_tree (node);
    4214                 :         234 :                       scalar_stmts.release ();
    4215                 :         234 :                       if (dump_enabled_p ())
    4216                 :          38 :                         dump_printf_loc (MSG_NOTE, vect_location,
    4217                 :             :                                          "SLP discovery failed\n");
    4218                 :         234 :                       return false;
    4219                 :             :                     }
    4220                 :             : 
    4221                 :             :                   /* ???  It really happens that we soft-fail SLP
    4222                 :             :                      build at a mismatch but the matching part hard-fails
    4223                 :             :                      later.  As we know we arrived here with a group
    4224                 :             :                      larger than one try a group of size one!  */
    4225                 :        3996 :                   if (!matches[0])
    4226                 :          66 :                     end = start + 1;
    4227                 :             :                   else
    4228                 :        8968 :                     for (unsigned j = start; j < end; j++)
    4229                 :        8968 :                       if (!matches[j - start])
    4230                 :             :                         {
    4231                 :             :                           end = j;
    4232                 :             :                           break;
    4233                 :             :                         }
    4234                 :             :                 }
    4235                 :             :             }
    4236                 :             : 
    4237                 :             :           /* Now re-assess whether we want store lanes in case the
    4238                 :             :              discovery ended up producing all single-lane RHSs.  */
    4239                 :        6102 :           if (! want_store_lanes
    4240                 :        6102 :               && rhs_common_nlanes == 1
    4241                 :        5119 :               && ! STMT_VINFO_GATHER_SCATTER_P (stmt_info)
    4242                 :        5119 :               && ! STMT_VINFO_STRIDED_P (stmt_info)
    4243                 :        3432 :               && ! STMT_VINFO_SLP_VECT_ONLY (stmt_info)
    4244                 :        3421 :               && compare_step_with_zero (vinfo, stmt_info) > 0
    4245                 :        9500 :               && (vect_store_lanes_supported (SLP_TREE_VECTYPE (rhs_nodes[0]),
    4246                 :             :                                               group_size, masked_p)
    4247                 :             :                   != IFN_LAST))
    4248                 :             :             want_store_lanes = true;
    4249                 :             : 
    4250                 :             :           /* Now we assume we can build the root SLP node from all stores.  */
    4251                 :        6102 :           if (want_store_lanes)
    4252                 :             :             {
    4253                 :             :               /* For store-lanes feed the store node with all RHS nodes
    4254                 :             :                  in order.  */
    4255                 :           0 :               node = vect_create_new_slp_node (scalar_stmts,
    4256                 :           0 :                                                SLP_TREE_CHILDREN
    4257                 :             :                                                  (rhs_nodes[0]).length ());
    4258                 :           0 :               SLP_TREE_VECTYPE (node) = SLP_TREE_VECTYPE (rhs_nodes[0]);
    4259                 :           0 :               node->max_nunits = max_nunits;
    4260                 :           0 :               node->ldst_lanes = true;
    4261                 :           0 :               SLP_TREE_CHILDREN (node)
    4262                 :           0 :                 .reserve_exact (SLP_TREE_CHILDREN (rhs_nodes[0]).length ()
    4263                 :           0 :                                 + rhs_nodes.length () - 1);
    4264                 :             :               /* First store value and possibly mask.  */
    4265                 :           0 :               SLP_TREE_CHILDREN (node)
    4266                 :           0 :                 .splice (SLP_TREE_CHILDREN (rhs_nodes[0]));
    4267                 :             :               /* Rest of the store values.  All mask nodes are the same,
    4268                 :             :                  this should be guaranteed by dataref group discovery.  */
    4269                 :           0 :               for (unsigned j = 1; j < rhs_nodes.length (); ++j)
    4270                 :           0 :                 SLP_TREE_CHILDREN (node)
    4271                 :           0 :                   .quick_push (SLP_TREE_CHILDREN (rhs_nodes[j])[0]);
    4272                 :           0 :               for (slp_tree child : SLP_TREE_CHILDREN (node))
    4273                 :           0 :                 child->refcnt++;
    4274                 :             :             }
    4275                 :             :           else
    4276                 :        6102 :             node = vect_build_slp_store_interleaving (rhs_nodes, scalar_stmts,
    4277                 :             :                                                       max_nunits);
    4278                 :             : 
    4279                 :       24020 :           while (!rhs_nodes.is_empty ())
    4280                 :       17918 :             vect_free_slp_tree (rhs_nodes.pop ());
    4281                 :             : 
    4282                 :             :           /* Create a new SLP instance.  */
    4283                 :        6102 :           slp_instance new_instance = XNEW (class _slp_instance);
    4284                 :        6102 :           SLP_INSTANCE_TREE (new_instance) = node;
    4285                 :        6102 :           SLP_INSTANCE_LOADS (new_instance) = vNULL;
    4286                 :        6102 :           SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
    4287                 :        6102 :           SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
    4288                 :        6102 :           SLP_INSTANCE_KIND (new_instance) = kind;
    4289                 :        6102 :           new_instance->reduc_phis = NULL;
    4290                 :        6102 :           new_instance->cost_vec = vNULL;
    4291                 :        6102 :           new_instance->subgraph_entries = vNULL;
    4292                 :             : 
    4293                 :        6102 :           if (dump_enabled_p ())
    4294                 :         830 :             dump_printf_loc (MSG_NOTE, vect_location,
    4295                 :             :                              "SLP size %u vs. limit %u.\n",
    4296                 :             :                              tree_size, max_tree_size);
    4297                 :             : 
    4298                 :        6102 :           vinfo->slp_instances.safe_push (new_instance);
    4299                 :             : 
    4300                 :             :           /* ???  We've replaced the old SLP_INSTANCE_GROUP_SIZE with
    4301                 :             :              the number of scalar stmts in the root in a few places.
    4302                 :             :              Verify that assumption holds.  */
    4303                 :       12204 :           gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
    4304                 :             :                         .length () == group_size);
    4305                 :             : 
    4306                 :        6102 :           if (dump_enabled_p ())
    4307                 :             :             {
    4308                 :         830 :               dump_printf_loc (MSG_NOTE, vect_location,
    4309                 :             :                                "Final SLP tree for instance %p:\n",
    4310                 :             :                                (void *) new_instance);
    4311                 :         830 :               vect_print_slp_graph (MSG_NOTE, vect_location,
    4312                 :             :                                     SLP_INSTANCE_TREE (new_instance));
    4313                 :             :             }
    4314                 :        6102 :           return true;
    4315                 :        6336 :         }
    4316                 :             :       else
    4317                 :             :         /* Free the allocated memory.  */
    4318                 :      202710 :         scalar_stmts.release ();
    4319                 :             : 
    4320                 :             :       /* Even though the first vector did not all match, we might be able to SLP
    4321                 :             :          (some) of the remainder.  FORNOW ignore this possibility.  */
    4322                 :             :     }
    4323                 :             :   else
    4324                 :             :     /* Free the allocated memory.  */
    4325                 :      994786 :     scalar_stmts.release ();
    4326                 :             : 
    4327                 :             :   /* Failed to SLP.  */
    4328                 :     1197496 :   if (dump_enabled_p ())
    4329                 :        3408 :     dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
    4330                 :             :   return false;
    4331                 :             : }
    4332                 :             : 
    4333                 :             : 
    4334                 :             : /* Analyze an SLP instance starting from a group of grouped stores.  Call
    4335                 :             :    vect_build_slp_tree to build a tree of packed stmts if possible.
    4336                 :             :    Return FALSE if it's impossible to SLP any stmt in the loop.  */
    4337                 :             : 
    4338                 :             : static bool
    4339                 :     1011528 : vect_analyze_slp_instance (vec_info *vinfo,
    4340                 :             :                            scalar_stmts_to_slp_tree_map_t *bst_map,
    4341                 :             :                            stmt_vec_info stmt_info,
    4342                 :             :                            slp_instance_kind kind,
    4343                 :             :                            unsigned max_tree_size, unsigned *limit,
    4344                 :             :                            bool force_single_lane)
    4345                 :             : {
    4346                 :     1011528 :   vec<stmt_vec_info> scalar_stmts;
    4347                 :             : 
    4348                 :     1011528 :   if (is_a <bb_vec_info> (vinfo))
    4349                 :      994076 :     vect_location = stmt_info->stmt;
    4350                 :             : 
    4351                 :     1011528 :   stmt_vec_info next_info = stmt_info;
    4352                 :     1011528 :   if (kind == slp_inst_kind_store)
    4353                 :             :     {
    4354                 :             :       /* Collect the stores and store them in scalar_stmts.  */
    4355                 :     1010918 :       scalar_stmts.create (DR_GROUP_SIZE (stmt_info));
    4356                 :     5004904 :       while (next_info)
    4357                 :             :         {
    4358                 :     2983068 :           scalar_stmts.quick_push (vect_stmt_to_vectorize (next_info));
    4359                 :     2983068 :           next_info = DR_GROUP_NEXT_ELEMENT (next_info);
    4360                 :             :         }
    4361                 :             :     }
    4362                 :         610 :   else if (kind == slp_inst_kind_reduc_chain)
    4363                 :             :     {
    4364                 :             :       /* Collect the reduction stmts and store them in scalar_stmts.  */
    4365                 :         610 :       scalar_stmts.create (REDUC_GROUP_SIZE (stmt_info));
    4366                 :        3784 :       while (next_info)
    4367                 :             :         {
    4368                 :        2564 :           scalar_stmts.quick_push (vect_stmt_to_vectorize (next_info));
    4369                 :        2564 :           next_info = REDUC_GROUP_NEXT_ELEMENT (next_info);
    4370                 :             :         }
    4371                 :             :       /* Mark the first element of the reduction chain as reduction to properly
    4372                 :             :          transform the node.  In the reduction analysis phase only the last
    4373                 :             :          element of the chain is marked as reduction.  */
    4374                 :         610 :       STMT_VINFO_DEF_TYPE (stmt_info)
    4375                 :         610 :         = STMT_VINFO_DEF_TYPE (scalar_stmts.last ());
    4376                 :         610 :       STMT_VINFO_REDUC_DEF (vect_orig_stmt (stmt_info))
    4377                 :         668 :         = STMT_VINFO_REDUC_DEF (vect_orig_stmt (scalar_stmts.last ()));
    4378                 :             :     }
    4379                 :             :   else
    4380                 :           0 :     gcc_unreachable ();
    4381                 :             : 
    4382                 :     1011528 :   vec<stmt_vec_info> roots = vNULL;
    4383                 :     1011528 :   vec<tree> remain = vNULL;
    4384                 :             :   /* Build the tree for the SLP instance.  */
    4385                 :     1012138 :   bool res = vect_build_slp_instance (vinfo, kind, scalar_stmts,
    4386                 :             :                                       roots, remain,
    4387                 :             :                                       max_tree_size, limit, bst_map,
    4388                 :             :                                       kind == slp_inst_kind_store
    4389                 :             :                                       ? stmt_info : NULL, force_single_lane);
    4390                 :             : 
    4391                 :             :   /* ???  If this is slp_inst_kind_store and the above succeeded here's
    4392                 :             :      where we should do store group splitting.  */
    4393                 :             : 
    4394                 :     1011528 :   return res;
    4395                 :             : }
    4396                 :             : 
    4397                 :             : /* qsort comparator ordering SLP load nodes.  */
    4398                 :             : 
    4399                 :             : static int
    4400                 :     1093229 : vllp_cmp (const void *a_, const void *b_)
    4401                 :             : {
    4402                 :     1093229 :   const slp_tree a = *(const slp_tree *)a_;
    4403                 :     1093229 :   const slp_tree b = *(const slp_tree *)b_;
    4404                 :     1093229 :   stmt_vec_info a0 = SLP_TREE_SCALAR_STMTS (a)[0];
    4405                 :     1093229 :   stmt_vec_info b0 = SLP_TREE_SCALAR_STMTS (b)[0];
    4406                 :     1093229 :   if (STMT_VINFO_GROUPED_ACCESS (a0)
    4407                 :      540171 :       && STMT_VINFO_GROUPED_ACCESS (b0)
    4408                 :     1607809 :       && DR_GROUP_FIRST_ELEMENT (a0) == DR_GROUP_FIRST_ELEMENT (b0))
    4409                 :             :     {
    4410                 :             :       /* Same group, order after lanes used.  */
    4411                 :      201871 :       if (SLP_TREE_LANES (a) < SLP_TREE_LANES (b))
    4412                 :             :         return 1;
    4413                 :      199050 :       else if (SLP_TREE_LANES (a) > SLP_TREE_LANES (b))
    4414                 :             :         return -1;
    4415                 :             :       else
    4416                 :             :         {
    4417                 :             :           /* Try to order loads using the same lanes together, breaking
    4418                 :             :              the tie with the lane number that first differs.  */
    4419                 :      195797 :           if (!SLP_TREE_LOAD_PERMUTATION (a).exists ()
    4420                 :      195797 :               && !SLP_TREE_LOAD_PERMUTATION (b).exists ())
    4421                 :             :             return 0;
    4422                 :      195797 :           else if (SLP_TREE_LOAD_PERMUTATION (a).exists ()
    4423                 :      195797 :                    && !SLP_TREE_LOAD_PERMUTATION (b).exists ())
    4424                 :             :             return 1;
    4425                 :      195162 :           else if (!SLP_TREE_LOAD_PERMUTATION (a).exists ()
    4426                 :      195162 :                    && SLP_TREE_LOAD_PERMUTATION (b).exists ())
    4427                 :             :             return -1;
    4428                 :             :           else
    4429                 :             :             {
    4430                 :      194567 :               for (unsigned i = 0; i < SLP_TREE_LANES (a); ++i)
    4431                 :      194567 :                 if (SLP_TREE_LOAD_PERMUTATION (a)[i]
    4432                 :      194567 :                     != SLP_TREE_LOAD_PERMUTATION (b)[i])
    4433                 :             :                   {
    4434                 :             :                     /* In-order lane first, that's what the above case for
    4435                 :             :                        no permutation does.  */
    4436                 :      194278 :                     if (SLP_TREE_LOAD_PERMUTATION (a)[i] == i)
    4437                 :             :                       return -1;
    4438                 :      118506 :                     else if (SLP_TREE_LOAD_PERMUTATION (b)[i] == i)
    4439                 :             :                       return 1;
    4440                 :       67763 :                     else if (SLP_TREE_LOAD_PERMUTATION (a)[i]
    4441                 :       67763 :                              < SLP_TREE_LOAD_PERMUTATION (b)[i])
    4442                 :             :                       return -1;
    4443                 :             :                     else
    4444                 :             :                       return 1;
    4445                 :             :                   }
    4446                 :             :               return 0;
    4447                 :             :             }
    4448                 :             :         }
    4449                 :             :     }
    4450                 :             :   else /* Different groups or non-groups.  */
    4451                 :             :     {
    4452                 :             :       /* Order groups as their first element to keep them together.  */
    4453                 :      891358 :       if (STMT_VINFO_GROUPED_ACCESS (a0))
    4454                 :      891358 :         a0 = DR_GROUP_FIRST_ELEMENT (a0);
    4455                 :      891358 :       if (STMT_VINFO_GROUPED_ACCESS (b0))
    4456                 :      891358 :         b0 = DR_GROUP_FIRST_ELEMENT (b0);
    4457                 :      891358 :       if (a0 == b0)
    4458                 :             :         return 0;
    4459                 :             :       /* Tie using UID.  */
    4460                 :      891334 :       else if (gimple_uid (STMT_VINFO_STMT (a0))
    4461                 :      891334 :                < gimple_uid (STMT_VINFO_STMT (b0)))
    4462                 :             :         return -1;
    4463                 :             :       else
    4464                 :             :         {
    4465                 :      380061 :           gcc_assert (gimple_uid (STMT_VINFO_STMT (a0))
    4466                 :             :                       != gimple_uid (STMT_VINFO_STMT (b0)));
    4467                 :             :           return 1;
    4468                 :             :         }
    4469                 :             :     }
    4470                 :             : }
    4471                 :             : 
    4472                 :             : /* Process the set of LOADS that are all from the same dataref group.  */
    4473                 :             : 
    4474                 :             : static void
    4475                 :       59558 : vect_lower_load_permutations (loop_vec_info loop_vinfo,
    4476                 :             :                               scalar_stmts_to_slp_tree_map_t *bst_map,
    4477                 :             :                               const array_slice<slp_tree> &loads,
    4478                 :             :                               bool force_single_lane)
    4479                 :             : {
    4480                 :             :   /* We at this point want to lower without a fixed VF or vector
    4481                 :             :      size in mind which means we cannot actually compute whether we
    4482                 :             :      need three or more vectors for a load permutation yet.  So always
    4483                 :             :      lower.  */
    4484                 :       59558 :   stmt_vec_info first
    4485                 :       59558 :     = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (loads[0])[0]);
    4486                 :       59558 :   unsigned group_lanes = DR_GROUP_SIZE (first);
    4487                 :             : 
    4488                 :             :   /* Verify if all load permutations can be implemented with a suitably
    4489                 :             :      large element load-lanes operation.  */
    4490                 :       59558 :   unsigned ld_lanes_lanes = SLP_TREE_LANES (loads[0]);
    4491                 :       59558 :   if (STMT_VINFO_STRIDED_P (first)
    4492                 :       57649 :       || compare_step_with_zero (loop_vinfo, first) <= 0
    4493                 :       57403 :       || exact_log2 (ld_lanes_lanes) == -1
    4494                 :             :       /* ???  For now only support the single-lane case as there is
    4495                 :             :          missing support on the store-lane side and code generation
    4496                 :             :          isn't up to the task yet.  */
    4497                 :       55532 :       || ld_lanes_lanes != 1
    4498                 :      109828 :       || vect_load_lanes_supported (SLP_TREE_VECTYPE (loads[0]),
    4499                 :             :                                     group_lanes / ld_lanes_lanes,
    4500                 :             :                                     false) == IFN_LAST)
    4501                 :             :     ld_lanes_lanes = 0;
    4502                 :             :   else
    4503                 :             :     /* Verify the loads access the same number of lanes aligned to
    4504                 :             :        ld_lanes_lanes.  */
    4505                 :           0 :     for (slp_tree load : loads)
    4506                 :             :       {
    4507                 :           0 :         if (SLP_TREE_LANES (load) != ld_lanes_lanes)
    4508                 :             :           {
    4509                 :             :             ld_lanes_lanes = 0;
    4510                 :             :             break;
    4511                 :             :           }
    4512                 :           0 :         unsigned first = SLP_TREE_LOAD_PERMUTATION (load)[0];
    4513                 :           0 :         if (first % ld_lanes_lanes != 0)
    4514                 :             :           {
    4515                 :             :             ld_lanes_lanes = 0;
    4516                 :             :             break;
    4517                 :             :           }
    4518                 :           0 :         for (unsigned i = 1; i < SLP_TREE_LANES (load); ++i)
    4519                 :             :           if (SLP_TREE_LOAD_PERMUTATION (load)[i] != first + i)
    4520                 :             :             {
    4521                 :             :               ld_lanes_lanes = 0;
    4522                 :             :               break;
    4523                 :             :             }
    4524                 :             :       }
    4525                 :             : 
    4526                 :             :   /* Only a power-of-two number of lanes matches interleaving with N levels.
    4527                 :             :      ???  An even number of lanes could be reduced to 1<<ceil_log2(N)-1 lanes
    4528                 :             :      at each step.  */
    4529                 :       90034 :   if (ld_lanes_lanes == 0 && exact_log2 (group_lanes) == -1 && group_lanes != 3)
    4530                 :             :     return;
    4531                 :             : 
    4532                 :      136709 :   for (slp_tree load : loads)
    4533                 :             :     {
    4534                 :             :       /* Leave masked or gather loads alone for now.  */
    4535                 :       84223 :       if (!SLP_TREE_CHILDREN (load).is_empty ())
    4536                 :       33678 :         continue;
    4537                 :             : 
    4538                 :             :       /* We want to pattern-match special cases here and keep those
    4539                 :             :          alone.  Candidates are splats and load-lane.  */
    4540                 :             : 
    4541                 :             :       /* We need to lower only loads of less than half of the groups
    4542                 :             :          lanes, including duplicate lanes.  Note this leaves nodes
    4543                 :             :          with a non-1:1 load permutation around instead of canonicalizing
    4544                 :             :          those into a load and a permute node.  Removing this early
    4545                 :             :          check would do such canonicalization.  */
    4546                 :       84201 :       if (SLP_TREE_LANES (load) >= (group_lanes + 1) / 2
    4547                 :       30255 :           && ld_lanes_lanes == 0)
    4548                 :       30255 :         continue;
    4549                 :             : 
    4550                 :             :       /* Build the permute to get the original load permutation order.  */
    4551                 :       53946 :       bool contiguous = true;
    4552                 :       53946 :       lane_permutation_t final_perm;
    4553                 :       53946 :       final_perm.create (SLP_TREE_LANES (load));
    4554                 :      108413 :       for (unsigned i = 0; i < SLP_TREE_LANES (load); ++i)
    4555                 :             :         {
    4556                 :       54467 :           final_perm.quick_push
    4557                 :       54467 :             (std::make_pair (0, SLP_TREE_LOAD_PERMUTATION (load)[i]));
    4558                 :       54467 :           if (i != 0
    4559                 :       54467 :               && (SLP_TREE_LOAD_PERMUTATION (load)[i]
    4560                 :         521 :                   != SLP_TREE_LOAD_PERMUTATION (load)[i-1] + 1))
    4561                 :             :             contiguous = false;
    4562                 :             :         }
    4563                 :             : 
    4564                 :             :       /* When the load permutation accesses a contiguous unpermuted,
    4565                 :             :          power-of-two aligned and sized chunk leave the load alone.
    4566                 :             :          We can likely (re-)load it more efficiently rather than
    4567                 :             :          extracting it from the larger load.
    4568                 :             :          ???  Long-term some of the lowering should move to where
    4569                 :             :          the vector types involved are fixed.  */
    4570                 :       57347 :       if (!force_single_lane
    4571                 :       53946 :           && ld_lanes_lanes == 0
    4572                 :       30745 :           && contiguous
    4573                 :       30540 :           && (SLP_TREE_LANES (load) > 1 || loads.size () == 1)
    4574                 :        7186 :           && pow2p_hwi (SLP_TREE_LANES (load))
    4575                 :        7186 :           && pow2p_hwi (group_lanes)
    4576                 :        3401 :           && SLP_TREE_LOAD_PERMUTATION (load)[0] % SLP_TREE_LANES (load) == 0
    4577                 :       57347 :           && group_lanes % SLP_TREE_LANES (load) == 0)
    4578                 :             :         {
    4579                 :        3401 :           final_perm.release ();
    4580                 :        3401 :           continue;
    4581                 :             :         }
    4582                 :             : 
    4583                 :             :       /* First build (and possibly re-use) a load node for the
    4584                 :             :          unpermuted group.  Gaps in the middle and on the end are
    4585                 :             :          represented with NULL stmts.  */
    4586                 :       50545 :       vec<stmt_vec_info> stmts;
    4587                 :       50545 :       stmts.create (group_lanes);
    4588                 :      170871 :       for (stmt_vec_info s = first; s; s = DR_GROUP_NEXT_ELEMENT (s))
    4589                 :             :         {
    4590                 :      120326 :           if (s != first)
    4591                 :       73362 :             for (unsigned i = 1; i < DR_GROUP_GAP (s); ++i)
    4592                 :        3581 :               stmts.quick_push (NULL);
    4593                 :      120326 :           stmts.quick_push (s);
    4594                 :             :         }
    4595                 :      136699 :       for (unsigned i = 0; i < DR_GROUP_GAP (first); ++i)
    4596                 :       86154 :         stmts.quick_push (NULL);
    4597                 :       50545 :       poly_uint64 max_nunits = 1;
    4598                 :       50545 :       bool *matches = XALLOCAVEC (bool, group_lanes);
    4599                 :       50545 :       unsigned limit = 1;
    4600                 :       50545 :       unsigned tree_size = 0;
    4601                 :       50545 :       slp_tree l0 = vect_build_slp_tree (loop_vinfo, stmts,
    4602                 :             :                                          group_lanes,
    4603                 :             :                                          &max_nunits, matches, &limit,
    4604                 :       50545 :                                          &tree_size, bst_map);
    4605                 :       50545 :       gcc_assert (!SLP_TREE_LOAD_PERMUTATION (l0).exists ());
    4606                 :             : 
    4607                 :       50545 :       if (ld_lanes_lanes != 0)
    4608                 :             :         {
    4609                 :             :           /* ???  If this is not in sync with what get_load_store_type
    4610                 :             :              later decides the SLP representation is not good for other
    4611                 :             :              store vectorization methods.  */
    4612                 :           0 :           l0->ldst_lanes = true;
    4613                 :           0 :           load->ldst_lanes = true;
    4614                 :             :         }
    4615                 :             : 
    4616                 :      166115 :       while (1)
    4617                 :             :         {
    4618                 :      108330 :           unsigned group_lanes = SLP_TREE_LANES (l0);
    4619                 :      108330 :           if (ld_lanes_lanes != 0
    4620                 :      108330 :               || SLP_TREE_LANES (load) >= (group_lanes + 1) / 2)
    4621                 :             :             break;
    4622                 :             : 
    4623                 :             :           /* Try to lower by reducing the group to half its size using an
    4624                 :             :              interleaving scheme.  For this try to compute whether all
    4625                 :             :              elements needed for this load are in even or odd elements of
    4626                 :             :              an even/odd decomposition with N consecutive elements.
    4627                 :             :              Thus { e, e, o, o, e, e, o, o } woud be an even/odd decomposition
    4628                 :             :              with N == 2.  */
    4629                 :             :           /* ???  Only an even number of lanes can be handed this way, but the
    4630                 :             :              fallback below could work for any number.  We have to make sure
    4631                 :             :              to round up in that case.  */
    4632                 :       57785 :           gcc_assert ((group_lanes & 1) == 0 || group_lanes == 3);
    4633                 :       14174 :           unsigned even = 0, odd = 0;
    4634                 :       14174 :           if ((group_lanes & 1) == 0)
    4635                 :             :             {
    4636                 :       14174 :               even = (1 << ceil_log2 (group_lanes)) - 1;
    4637                 :       14174 :               odd = even;
    4638                 :       57138 :               for (auto l : final_perm)
    4639                 :             :                 {
    4640                 :       14616 :                   even &= ~l.second;
    4641                 :       14616 :                   odd &= l.second;
    4642                 :             :                 }
    4643                 :             :             }
    4644                 :             : 
    4645                 :             :           /* Now build an even or odd extraction from the unpermuted load.  */
    4646                 :       57785 :           lane_permutation_t perm;
    4647                 :       57785 :           perm.create ((group_lanes + 1) / 2);
    4648                 :       57785 :           unsigned even_level = even ? 1 << ctz_hwi (even) : 0;
    4649                 :       57785 :           unsigned odd_level = odd ? 1 << ctz_hwi (odd) : 0;
    4650                 :       57785 :           if (even_level
    4651                 :       13498 :               && group_lanes % (2 * even_level) == 0
    4652                 :             :               /* ???  When code generating permutes we do not try to pun
    4653                 :             :                  to larger component modes so level != 1 isn't a natural
    4654                 :             :                  even/odd extract.  Prefer one if possible.  */
    4655                 :       13498 :               && (even_level == 1 || !odd_level || odd_level != 1))
    4656                 :             :             {
    4657                 :             :               /* { 0, 1, ... 4, 5 ..., } */
    4658                 :       67008 :               for (unsigned i = 0; i < group_lanes / 2 / even_level; ++i)
    4659                 :      111127 :                 for (unsigned j = 0; j < even_level; ++j)
    4660                 :       55646 :                   perm.quick_push (std::make_pair (0, 2 * i * even_level + j));
    4661                 :             :             }
    4662                 :       44287 :           else if (odd_level)
    4663                 :             :             {
    4664                 :             :               /* { ..., 2, 3, ... 6, 7 } */
    4665                 :        2647 :               gcc_assert (group_lanes % (2 * odd_level) == 0);
    4666                 :       11915 :               for (unsigned i = 0; i < group_lanes / 2 / odd_level; ++i)
    4667                 :       18536 :                 for (unsigned j = 0; j < odd_level; ++j)
    4668                 :        9268 :                   perm.quick_push
    4669                 :        9268 :                     (std::make_pair (0, (2 * i + 1) * odd_level + j));
    4670                 :             :             }
    4671                 :             :           else
    4672                 :             :             {
    4673                 :             :               /* As fallback extract all used lanes and fill to half the
    4674                 :             :                  group size by repeating the last element.
    4675                 :             :                  ???  This is quite a bad strathegy for re-use - we could
    4676                 :             :                  brute force our way to find more optimal filling lanes to
    4677                 :             :                  maximize re-use when looking at all loads from the group.  */
    4678                 :       43611 :               auto_bitmap l;
    4679                 :      174444 :               for (auto p : final_perm)
    4680                 :       43611 :                 bitmap_set_bit (l, p.second);
    4681                 :       43611 :               unsigned i = 0;
    4682                 :       43611 :               bitmap_iterator bi;
    4683                 :       87222 :               EXECUTE_IF_SET_IN_BITMAP (l, 0, i, bi)
    4684                 :       43611 :                   perm.quick_push (std::make_pair (0, i));
    4685                 :      174444 :               while (perm.length () < (group_lanes + 1) / 2)
    4686                 :       43611 :                 perm.quick_push (perm.last ());
    4687                 :       43611 :             }
    4688                 :             : 
    4689                 :             :           /* Update final_perm with the intermediate permute.  */
    4690                 :      116012 :           for (unsigned i = 0; i < final_perm.length (); ++i)
    4691                 :             :             {
    4692                 :       58227 :               unsigned l = final_perm[i].second;
    4693                 :       58227 :               unsigned j;
    4694                 :       65308 :               for (j = 0; j < perm.length (); ++j)
    4695                 :       65308 :                 if (perm[j].second == l)
    4696                 :             :                   {
    4697                 :       58227 :                     final_perm[i].second = j;
    4698                 :       58227 :                     break;
    4699                 :             :                   }
    4700                 :       58227 :               gcc_assert (j < perm.length ());
    4701                 :             :             }
    4702                 :             : 
    4703                 :             :           /* And create scalar stmts.  */
    4704                 :       57785 :           vec<stmt_vec_info> perm_stmts;
    4705                 :       57785 :           perm_stmts.create (perm.length ());
    4706                 :      209921 :           for (unsigned i = 0; i < perm.length (); ++i)
    4707                 :      152136 :             perm_stmts.quick_push (SLP_TREE_SCALAR_STMTS (l0)[perm[i].second]);
    4708                 :             : 
    4709                 :       57785 :           slp_tree p = vect_create_new_slp_node (1, VEC_PERM_EXPR);
    4710                 :       57785 :           SLP_TREE_CHILDREN (p).quick_push (l0);
    4711                 :       57785 :           SLP_TREE_LANE_PERMUTATION (p) = perm;
    4712                 :       57785 :           SLP_TREE_VECTYPE (p) = SLP_TREE_VECTYPE (load);
    4713                 :       57785 :           SLP_TREE_LANES (p) = perm.length ();
    4714                 :       57785 :           SLP_TREE_REPRESENTATIVE (p) = SLP_TREE_REPRESENTATIVE (load);
    4715                 :             :           /* ???  As we have scalar stmts for this intermediate permute we
    4716                 :             :              could CSE it via bst_map but we do not want to pick up
    4717                 :             :              another SLP node with a load permutation.  We instead should
    4718                 :             :              have a "local" CSE map here.  */
    4719                 :       57785 :           SLP_TREE_SCALAR_STMTS (p) = perm_stmts;
    4720                 :             : 
    4721                 :             :           /* We now have a node for (group_lanes + 1) / 2 lanes.  */
    4722                 :       57785 :           l0 = p;
    4723                 :       57785 :         }
    4724                 :             : 
    4725                 :             :       /* And finally from the ordered reduction node create the
    4726                 :             :          permute to shuffle the lanes into the original load-permutation
    4727                 :             :          order.  We replace the original load node with this.  */
    4728                 :       50545 :       SLP_TREE_CODE (load) = VEC_PERM_EXPR;
    4729                 :       50545 :       SLP_TREE_LOAD_PERMUTATION (load).release ();
    4730                 :       50545 :       SLP_TREE_LANE_PERMUTATION (load) = final_perm;
    4731                 :       50545 :       SLP_TREE_CHILDREN (load).create (1);
    4732                 :       50545 :       SLP_TREE_CHILDREN (load).quick_push (l0);
    4733                 :             :     }
    4734                 :             : }
    4735                 :             : 
    4736                 :             : /* Transform SLP loads in the SLP graph created by SLP discovery to
    4737                 :             :    group loads from the same group and lower load permutations that
    4738                 :             :    are unlikely to be supported into a series of permutes.
    4739                 :             :    In the degenerate case of having only single-lane SLP instances
    4740                 :             :    this should result in a series of permute nodes emulating an
    4741                 :             :    interleaving scheme.  */
    4742                 :             : 
    4743                 :             : static void
    4744                 :      241907 : vect_lower_load_permutations (loop_vec_info loop_vinfo,
    4745                 :             :                               scalar_stmts_to_slp_tree_map_t *bst_map,
    4746                 :             :                               bool force_single_lane)
    4747                 :             : {
    4748                 :             :   /* Gather and sort loads across all instances.  */
    4749                 :      241907 :   hash_set<slp_tree> visited;
    4750                 :      241907 :   auto_vec<slp_tree> loads;
    4751                 :     1154808 :   for (auto inst : loop_vinfo->slp_instances)
    4752                 :      439097 :     vect_gather_slp_loads (loads, SLP_INSTANCE_TREE (inst), visited);
    4753                 :      241907 :   if (loads.is_empty ())
    4754                 :       64986 :     return;
    4755                 :      176921 :   loads.qsort (vllp_cmp);
    4756                 :             : 
    4757                 :             :   /* Now process each dataref group separately.  */
    4758                 :      176921 :   unsigned firsti = 0;
    4759                 :      320222 :   for (unsigned i = 1; i < loads.length (); ++i)
    4760                 :             :     {
    4761                 :      143301 :       slp_tree first = loads[firsti];
    4762                 :      143301 :       slp_tree next = loads[i];
    4763                 :      143301 :       stmt_vec_info a0 = SLP_TREE_SCALAR_STMTS (first)[0];
    4764                 :      143301 :       stmt_vec_info b0 = SLP_TREE_SCALAR_STMTS (next)[0];
    4765                 :      143301 :       if (STMT_VINFO_GROUPED_ACCESS (a0)
    4766                 :       60794 :           && STMT_VINFO_GROUPED_ACCESS (b0)
    4767                 :      200204 :           && DR_GROUP_FIRST_ELEMENT (a0) == DR_GROUP_FIRST_ELEMENT (b0))
    4768                 :       36050 :         continue;
    4769                 :             :       /* Now we have one or multiple SLP loads of the same group from
    4770                 :             :          firsti to i - 1.  */
    4771                 :      107251 :       if (STMT_VINFO_GROUPED_ACCESS (a0))
    4772                 :       24744 :         vect_lower_load_permutations (loop_vinfo, bst_map,
    4773                 :       24744 :                                       make_array_slice (&loads[firsti],
    4774                 :             :                                                         i - firsti),
    4775                 :             :                                       force_single_lane);
    4776                 :             :       firsti = i;
    4777                 :             :     }
    4778                 :      353842 :   if (firsti < loads.length ()
    4779                 :      353842 :       && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (loads[firsti])[0]))
    4780                 :       34814 :     vect_lower_load_permutations (loop_vinfo, bst_map,
    4781                 :       34814 :                                   make_array_slice (&loads[firsti],
    4782                 :       34814 :                                                     loads.length () - firsti),
    4783                 :             :                                   force_single_lane);
    4784                 :      241907 : }
    4785                 :             : 
    4786                 :             : /* Check if there are stmts in the loop can be vectorized using SLP.  Build SLP
    4787                 :             :    trees of packed scalar stmts if SLP is possible.  */
    4788                 :             : 
    4789                 :             : opt_result
    4790                 :      837809 : vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size,
    4791                 :             :                   bool force_single_lane)
    4792                 :             : {
    4793                 :      837809 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    4794                 :      837809 :   unsigned int i;
    4795                 :      837809 :   stmt_vec_info first_element;
    4796                 :      837809 :   slp_instance instance;
    4797                 :             : 
    4798                 :      837809 :   DUMP_VECT_SCOPE ("vect_analyze_slp");
    4799                 :             : 
    4800                 :      837809 :   unsigned limit = max_tree_size;
    4801                 :             : 
    4802                 :      837809 :   scalar_stmts_to_slp_tree_map_t *bst_map
    4803                 :      837809 :     = new scalar_stmts_to_slp_tree_map_t ();
    4804                 :             : 
    4805                 :             :   /* Find SLP sequences starting from groups of grouped stores.  */
    4806                 :     2522130 :   FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
    4807                 :      846512 :     vect_analyze_slp_instance (vinfo, bst_map, first_element,
    4808                 :             :                                slp_inst_kind_store, max_tree_size, &limit,
    4809                 :             :                                force_single_lane);
    4810                 :             : 
    4811                 :             :   /* For loops also start SLP discovery from non-grouped stores.  */
    4812                 :      837809 :   if (loop_vinfo)
    4813                 :             :     {
    4814                 :             :       data_reference_p dr;
    4815                 :      827191 :       FOR_EACH_VEC_ELT (vinfo->shared->datarefs, i, dr)
    4816                 :      585284 :         if (DR_IS_WRITE (dr))
    4817                 :             :           {
    4818                 :      240088 :             stmt_vec_info stmt_info = vinfo->lookup_dr (dr)->stmt;
    4819                 :             :             /* Grouped stores are already handled above.  */
    4820                 :      240088 :             if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
    4821                 :       65856 :               continue;
    4822                 :      174232 :             vec<stmt_vec_info> stmts;
    4823                 :      174232 :             vec<stmt_vec_info> roots = vNULL;
    4824                 :      174232 :             vec<tree> remain = vNULL;
    4825                 :      174232 :             stmts.create (1);
    4826                 :      174232 :             stmts.quick_push (stmt_info);
    4827                 :      174232 :             vect_build_slp_instance (vinfo, slp_inst_kind_store,
    4828                 :             :                                      stmts, roots, remain, max_tree_size,
    4829                 :             :                                      &limit, bst_map, NULL, force_single_lane);
    4830                 :             :           }
    4831                 :             :     }
    4832                 :             : 
    4833                 :      837809 :   if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo))
    4834                 :             :     {
    4835                 :     1720862 :       for (unsigned i = 0; i < bb_vinfo->roots.length (); ++i)
    4836                 :             :         {
    4837                 :     1124960 :           vect_location = bb_vinfo->roots[i].roots[0]->stmt;
    4838                 :             :           /* Apply patterns.  */
    4839                 :     3525284 :           for (unsigned j = 0; j < bb_vinfo->roots[i].stmts.length (); ++j)
    4840                 :     4800648 :             bb_vinfo->roots[i].stmts[j]
    4841                 :     2456423 :               = vect_stmt_to_vectorize (bb_vinfo->roots[i].stmts[j]);
    4842                 :     1124960 :           if (vect_build_slp_instance (bb_vinfo, bb_vinfo->roots[i].kind,
    4843                 :     1124960 :                                        bb_vinfo->roots[i].stmts,
    4844                 :     1124960 :                                        bb_vinfo->roots[i].roots,
    4845                 :     1124960 :                                        bb_vinfo->roots[i].remain,
    4846                 :             :                                        max_tree_size, &limit, bst_map, NULL,
    4847                 :             :                                        false))
    4848                 :             :             {
    4849                 :      110503 :               bb_vinfo->roots[i].stmts = vNULL;
    4850                 :      110503 :               bb_vinfo->roots[i].roots = vNULL;
    4851                 :      110503 :               bb_vinfo->roots[i].remain = vNULL;
    4852                 :             :             }
    4853                 :             :         }
    4854                 :             :     }
    4855                 :             : 
    4856                 :      837809 :   if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
    4857                 :             :     {
    4858                 :             :       /* Find SLP sequences starting from reduction chains.  */
    4859                 :      242521 :       FOR_EACH_VEC_ELT (loop_vinfo->reduction_chains, i, first_element)
    4860                 :         614 :         if (! STMT_VINFO_RELEVANT_P (first_element)
    4861                 :           4 :             && ! STMT_VINFO_LIVE_P (first_element))
    4862                 :             :           ;
    4863                 :         610 :         else if (force_single_lane
    4864                 :         610 :                  || ! vect_analyze_slp_instance (vinfo, bst_map, first_element,
    4865                 :             :                                                  slp_inst_kind_reduc_chain,
    4866                 :             :                                                  max_tree_size, &limit,
    4867                 :             :                                                  force_single_lane))
    4868                 :             :           {
    4869                 :             :             /* Dissolve reduction chain group.  */
    4870                 :         267 :             stmt_vec_info vinfo = first_element;
    4871                 :         267 :             stmt_vec_info last = NULL;
    4872                 :        1046 :             while (vinfo)
    4873                 :             :               {
    4874                 :         779 :                 stmt_vec_info next = REDUC_GROUP_NEXT_ELEMENT (vinfo);
    4875                 :         779 :                 REDUC_GROUP_FIRST_ELEMENT (vinfo) = NULL;
    4876                 :         779 :                 REDUC_GROUP_NEXT_ELEMENT (vinfo) = NULL;
    4877                 :         779 :                 last = vinfo;
    4878                 :         779 :                 vinfo = next;
    4879                 :             :               }
    4880                 :         267 :             STMT_VINFO_DEF_TYPE (first_element) = vect_internal_def;
    4881                 :             :             /* It can be still vectorized as part of an SLP reduction.  */
    4882                 :         267 :             loop_vinfo->reductions.safe_push (last);
    4883                 :             :           }
    4884                 :             : 
    4885                 :             :       /* Find SLP sequences starting from groups of reductions.  */
    4886                 :      241907 :       if (loop_vinfo->reductions.length () > 0)
    4887                 :             :         {
    4888                 :             :           /* Collect reduction statements we can combine into
    4889                 :             :              a SLP reduction.  */
    4890                 :       46253 :           vec<stmt_vec_info> scalar_stmts;
    4891                 :       46253 :           scalar_stmts.create (loop_vinfo->reductions.length ());
    4892                 :      206570 :           for (auto next_info : loop_vinfo->reductions)
    4893                 :             :             {
    4894                 :       67811 :               next_info = vect_stmt_to_vectorize (next_info);
    4895                 :       67811 :               if ((STMT_VINFO_RELEVANT_P (next_info)
    4896                 :          11 :                    || STMT_VINFO_LIVE_P (next_info))
    4897                 :             :                   /* ???  Make sure we didn't skip a conversion around a
    4898                 :             :                      reduction path.  In that case we'd have to reverse
    4899                 :             :                      engineer that conversion stmt following the chain using
    4900                 :             :                      reduc_idx and from the PHI using reduc_def.  */
    4901                 :       67800 :                   && (STMT_VINFO_DEF_TYPE (next_info) == vect_reduction_def
    4902                 :       67800 :                       || (STMT_VINFO_DEF_TYPE (next_info)
    4903                 :             :                           == vect_double_reduction_def)))
    4904                 :             :                 {
    4905                 :             :                   /* Do not discover SLP reductions combining lane-reducing
    4906                 :             :                      ops, that will fail later.  */
    4907                 :       67722 :                   if (!force_single_lane
    4908                 :       67722 :                       && !lane_reducing_stmt_p (STMT_VINFO_STMT (next_info)))
    4909                 :       50082 :                     scalar_stmts.quick_push (next_info);
    4910                 :             :                   else
    4911                 :             :                     {
    4912                 :             :                       /* Do SLP discovery for single-lane reductions.  */
    4913                 :       17640 :                       vec<stmt_vec_info> stmts;
    4914                 :       17640 :                       vec<stmt_vec_info> roots = vNULL;
    4915                 :       17640 :                       vec<tree> remain = vNULL;
    4916                 :       17640 :                       stmts.create (1);
    4917                 :       17640 :                       stmts.quick_push (next_info);
    4918                 :       17640 :                       vect_build_slp_instance (vinfo,
    4919                 :             :                                                slp_inst_kind_reduc_group,
    4920                 :             :                                                stmts, roots, remain,
    4921                 :             :                                                max_tree_size, &limit,
    4922                 :             :                                                bst_map, NULL,
    4923                 :             :                                                force_single_lane);
    4924                 :             :                     }
    4925                 :             :                 }
    4926                 :             :             }
    4927                 :             :           /* Save for re-processing on failure.  */
    4928                 :       46253 :           vec<stmt_vec_info> saved_stmts = scalar_stmts.copy ();
    4929                 :       46253 :           vec<stmt_vec_info> roots = vNULL;
    4930                 :       46253 :           vec<tree> remain = vNULL;
    4931                 :       46253 :           if (scalar_stmts.length () <= 1
    4932                 :       46253 :               || !vect_build_slp_instance (loop_vinfo,
    4933                 :             :                                            slp_inst_kind_reduc_group,
    4934                 :             :                                            scalar_stmts, roots, remain,
    4935                 :             :                                            max_tree_size, &limit, bst_map,
    4936                 :             :                                            NULL, force_single_lane))
    4937                 :             :             {
    4938                 :       45933 :               if (scalar_stmts.length () <= 1)
    4939                 :       45933 :                 scalar_stmts.release ();
    4940                 :             :               /* Do SLP discovery for single-lane reductions.  */
    4941                 :      158980 :               for (auto stmt_info : saved_stmts)
    4942                 :             :                 {
    4943                 :       49127 :                   vec<stmt_vec_info> stmts;
    4944                 :       49127 :                   vec<stmt_vec_info> roots = vNULL;
    4945                 :       49127 :                   vec<tree> remain = vNULL;
    4946                 :       49127 :                   stmts.create (1);
    4947                 :       49127 :                   stmts.quick_push (vect_stmt_to_vectorize (stmt_info));
    4948                 :       49127 :                   vect_build_slp_instance (vinfo,
    4949                 :             :                                            slp_inst_kind_reduc_group,
    4950                 :             :                                            stmts, roots, remain,
    4951                 :             :                                            max_tree_size, &limit,
    4952                 :             :                                            bst_map, NULL, force_single_lane);
    4953                 :             :                 }
    4954                 :       45933 :               saved_stmts.release ();
    4955                 :             :             }
    4956                 :             :         }
    4957                 :             : 
    4958                 :             :       /* Make sure to vectorize only-live stmts, usually inductions.  */
    4959                 :     1031041 :       for (edge e : get_loop_exit_edges (LOOP_VINFO_LOOP (loop_vinfo)))
    4960                 :      584666 :         for (auto gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
    4961                 :      279346 :              gsi_next (&gsi))
    4962                 :             :           {
    4963                 :      279346 :             gphi *lc_phi = *gsi;
    4964                 :      279346 :             tree def = gimple_phi_arg_def_from_edge (lc_phi, e);
    4965                 :      279346 :             stmt_vec_info stmt_info;
    4966                 :      279346 :             if (TREE_CODE (def) == SSA_NAME
    4967                 :      270632 :                 && !virtual_operand_p (def)
    4968                 :      106522 :                 && (stmt_info = loop_vinfo->lookup_def (def))
    4969                 :      104428 :                 && ((stmt_info = vect_stmt_to_vectorize (stmt_info)), true)
    4970                 :      104428 :                 && STMT_VINFO_RELEVANT (stmt_info) == vect_used_only_live
    4971                 :       87584 :                 && STMT_VINFO_LIVE_P (stmt_info)
    4972                 :      366930 :                 && (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def
    4973                 :       81366 :                     || (STMT_VINFO_DEF_TYPE (stmt_info) == vect_internal_def
    4974                 :       12118 :                         && STMT_VINFO_REDUC_IDX (stmt_info) == -1)))
    4975                 :             :               {
    4976                 :       18234 :                 vec<stmt_vec_info> stmts;
    4977                 :       18234 :                 vec<stmt_vec_info> roots = vNULL;
    4978                 :       18234 :                 vec<tree> remain = vNULL;
    4979                 :       18234 :                 stmts.create (1);
    4980                 :       18234 :                 stmts.quick_push (vect_stmt_to_vectorize (stmt_info));
    4981                 :       18234 :                 vect_build_slp_instance (vinfo,
    4982                 :             :                                          slp_inst_kind_reduc_group,
    4983                 :             :                                          stmts, roots, remain,
    4984                 :             :                                          max_tree_size, &limit,
    4985                 :             :                                          bst_map, NULL, force_single_lane);
    4986                 :             :               }
    4987                 :      241907 :           }
    4988                 :             : 
    4989                 :             :       /* Find SLP sequences starting from gconds.  */
    4990                 :      410570 :       for (auto cond : LOOP_VINFO_LOOP_CONDS (loop_vinfo))
    4991                 :             :         {
    4992                 :       63413 :           auto cond_info = loop_vinfo->lookup_stmt (cond);
    4993                 :             : 
    4994                 :       63413 :           cond_info = vect_stmt_to_vectorize (cond_info);
    4995                 :       63413 :           vec<stmt_vec_info> roots = vNULL;
    4996                 :       63413 :           roots.safe_push (cond_info);
    4997                 :       63413 :           gimple *stmt = STMT_VINFO_STMT (cond_info);
    4998                 :       63413 :           tree args0 = gimple_cond_lhs (stmt);
    4999                 :       63413 :           tree args1 = gimple_cond_rhs (stmt);
    5000                 :             : 
    5001                 :             :           /* These should be enforced by cond lowering.  */
    5002                 :       63413 :           gcc_assert (gimple_cond_code (stmt) == NE_EXPR);
    5003                 :       63413 :           gcc_assert (zerop (args1));
    5004                 :             : 
    5005                 :             :           /* An argument without a loop def will be codegened from vectorizing the
    5006                 :             :              root gcond itself.  As such we don't need to try to build an SLP tree
    5007                 :             :              from them.  It's highly likely that the resulting SLP tree here if both
    5008                 :             :              arguments have a def will be incompatible, but we rely on it being split
    5009                 :             :              later on.  */
    5010                 :       63413 :           auto varg = loop_vinfo->lookup_def (args0);
    5011                 :       63413 :           vec<stmt_vec_info> stmts;
    5012                 :       63413 :           vec<tree> remain = vNULL;
    5013                 :       63413 :           stmts.create (1);
    5014                 :       63413 :           stmts.quick_push (vect_stmt_to_vectorize (varg));
    5015                 :             : 
    5016                 :       63413 :           vect_build_slp_instance (vinfo, slp_inst_kind_gcond,
    5017                 :             :                                    stmts, roots, remain,
    5018                 :             :                                    max_tree_size, &limit,
    5019                 :             :                                    bst_map, NULL, force_single_lane);
    5020                 :             :         }
    5021                 :             : 
    5022                 :             :         /* Find and create slp instances for inductions that have been forced
    5023                 :             :            live due to early break.  */
    5024                 :      241907 :         edge latch_e = loop_latch_edge (LOOP_VINFO_LOOP (loop_vinfo));
    5025                 :      452692 :         for (auto stmt_info : LOOP_VINFO_EARLY_BREAKS_LIVE_IVS (loop_vinfo))
    5026                 :             :           {
    5027                 :      105535 :             vec<stmt_vec_info> stmts;
    5028                 :      105535 :             vec<stmt_vec_info> roots = vNULL;
    5029                 :      105535 :             vec<tree> remain = vNULL;
    5030                 :      105535 :             gphi *lc_phi = as_a<gphi *> (STMT_VINFO_STMT (stmt_info));
    5031                 :      105535 :             tree def = gimple_phi_arg_def_from_edge (lc_phi, latch_e);
    5032                 :      105535 :             stmt_vec_info lc_info = loop_vinfo->lookup_def (def);
    5033                 :      105535 :             stmts.create (1);
    5034                 :      105565 :             stmts.quick_push (vect_stmt_to_vectorize (lc_info));
    5035                 :      105535 :             vect_build_slp_instance (vinfo, slp_inst_kind_reduc_group,
    5036                 :             :                                      stmts, roots, remain,
    5037                 :             :                                      max_tree_size, &limit,
    5038                 :             :                                      bst_map, NULL, force_single_lane);
    5039                 :             :           }
    5040                 :             :     }
    5041                 :             : 
    5042                 :      837809 :   hash_set<slp_tree> visited_patterns;
    5043                 :      837809 :   slp_tree_to_load_perm_map_t perm_cache;
    5044                 :      837809 :   slp_compat_nodes_map_t compat_cache;
    5045                 :             : 
    5046                 :             :   /* See if any patterns can be found in the SLP tree.  */
    5047                 :      837809 :   bool pattern_found = false;
    5048                 :     2877078 :   FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
    5049                 :     1201460 :     pattern_found |= vect_match_slp_patterns (instance, vinfo,
    5050                 :             :                                               &visited_patterns, &perm_cache,
    5051                 :             :                                               &compat_cache);
    5052                 :             : 
    5053                 :             :   /* If any were found optimize permutations of loads.  */
    5054                 :      837809 :   if (pattern_found)
    5055                 :             :     {
    5056                 :         224 :       hash_map<slp_tree, slp_tree> load_map;
    5057                 :        1699 :       FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
    5058                 :             :         {
    5059                 :        1251 :           slp_tree root = SLP_INSTANCE_TREE (instance);
    5060                 :        1251 :           optimize_load_redistribution (bst_map, vinfo, SLP_TREE_LANES (root),
    5061                 :             :                                         &load_map, root);
    5062                 :             :         }
    5063                 :         224 :     }
    5064                 :             : 
    5065                 :             :   /* Check whether we should force some SLP instances to use load/store-lanes
    5066                 :             :      and do so by forcing SLP re-discovery with single lanes.  We used
    5067                 :             :      to cancel SLP when this applied to all instances in a loop but now
    5068                 :             :      we decide this per SLP instance.  It's important to do this only
    5069                 :             :      after SLP pattern recognition.  */
    5070                 :      837809 :   if (is_a <loop_vec_info> (vinfo))
    5071                 :      681004 :     FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
    5072                 :      439097 :       if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store
    5073                 :      186509 :           && !SLP_INSTANCE_TREE (instance)->ldst_lanes)
    5074                 :             :         {
    5075                 :      186509 :           slp_tree slp_root = SLP_INSTANCE_TREE (instance);
    5076                 :      186509 :           int group_size = SLP_TREE_LANES (slp_root);
    5077                 :      186509 :           tree vectype = SLP_TREE_VECTYPE (slp_root);
    5078                 :             : 
    5079                 :      186509 :           stmt_vec_info rep_info = SLP_TREE_REPRESENTATIVE (slp_root);
    5080                 :      186509 :           gimple *rep = STMT_VINFO_STMT (rep_info);
    5081                 :      186509 :           bool masked = (is_gimple_call (rep)
    5082                 :        1678 :                          && gimple_call_internal_p (rep)
    5083                 :      188187 :                          && internal_fn_mask_index
    5084                 :        1678 :                               (gimple_call_internal_fn (rep)) != -1);
    5085                 :      186509 :           if (!STMT_VINFO_GROUPED_ACCESS (rep_info)
    5086                 :       16540 :               || slp_root->ldst_lanes
    5087                 :      203049 :               || (vect_store_lanes_supported (vectype, group_size, masked)
    5088                 :             :                   == IFN_LAST))
    5089                 :      186509 :             continue;
    5090                 :             : 
    5091                 :           0 :           auto_vec<slp_tree> loads;
    5092                 :           0 :           hash_set<slp_tree> visited;
    5093                 :           0 :           vect_gather_slp_loads (loads, slp_root, visited);
    5094                 :             : 
    5095                 :             :           /* Check whether any load in the SLP instance is possibly
    5096                 :             :              permuted.  */
    5097                 :           0 :           bool loads_permuted = false;
    5098                 :           0 :           slp_tree load_node;
    5099                 :           0 :           unsigned j;
    5100                 :           0 :           FOR_EACH_VEC_ELT (loads, j, load_node)
    5101                 :             :             {
    5102                 :           0 :               if (!SLP_TREE_LOAD_PERMUTATION (load_node).exists ())
    5103                 :           0 :                 continue;
    5104                 :             :               unsigned k;
    5105                 :             :               stmt_vec_info load_info;
    5106                 :           0 :               FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), k, load_info)
    5107                 :           0 :                 if (SLP_TREE_LOAD_PERMUTATION (load_node)[k] != k)
    5108                 :             :                   {
    5109                 :             :                     loads_permuted = true;
    5110                 :             :                     break;
    5111                 :             :                   }
    5112                 :             :             }
    5113                 :             : 
    5114                 :             :           /* If the loads and stores can use load/store-lanes force re-discovery
    5115                 :             :              with single lanes.  */
    5116                 :           0 :           if (loads_permuted)
    5117                 :             :             {
    5118                 :           0 :               bool can_use_lanes = true;
    5119                 :           0 :               FOR_EACH_VEC_ELT (loads, j, load_node)
    5120                 :           0 :                 if (STMT_VINFO_GROUPED_ACCESS
    5121                 :             :                       (SLP_TREE_REPRESENTATIVE (load_node)))
    5122                 :             :                   {
    5123                 :           0 :                     stmt_vec_info stmt_vinfo = DR_GROUP_FIRST_ELEMENT
    5124                 :             :                         (SLP_TREE_REPRESENTATIVE (load_node));
    5125                 :           0 :                     rep = STMT_VINFO_STMT (stmt_vinfo);
    5126                 :           0 :                     masked = (is_gimple_call (rep)
    5127                 :           0 :                               && gimple_call_internal_p (rep)
    5128                 :           0 :                               && internal_fn_mask_index
    5129                 :           0 :                                    (gimple_call_internal_fn (rep)));
    5130                 :             :                     /* Use SLP for strided accesses (or if we can't
    5131                 :             :                        load-lanes).  */
    5132                 :           0 :                     if (STMT_VINFO_STRIDED_P (stmt_vinfo)
    5133                 :           0 :                         || compare_step_with_zero (vinfo, stmt_vinfo) <= 0
    5134                 :           0 :                         || vect_load_lanes_supported
    5135                 :           0 :                              (STMT_VINFO_VECTYPE (stmt_vinfo),
    5136                 :           0 :                               DR_GROUP_SIZE (stmt_vinfo), masked) == IFN_LAST
    5137                 :             :                         /* ???  During SLP re-discovery with a single lane
    5138                 :             :                            a masked grouped load will appear permuted and
    5139                 :             :                            discovery will fail.  We have to rework this
    5140                 :             :                            on the discovery side - for now avoid ICEing.  */
    5141                 :           0 :                         || masked)
    5142                 :             :                       {
    5143                 :             :                         can_use_lanes = false;
    5144                 :             :                         break;
    5145                 :             :                       }
    5146                 :             :                   }
    5147                 :             : 
    5148                 :           0 :               if (can_use_lanes)
    5149                 :             :                 {
    5150                 :           0 :                   if (dump_enabled_p ())
    5151                 :           0 :                     dump_printf_loc (MSG_NOTE, vect_location,
    5152                 :             :                                      "SLP instance %p can use load/store-lanes,"
    5153                 :             :                                      " re-discovering with single-lanes\n",
    5154                 :             :                                      (void *) instance);
    5155                 :             : 
    5156                 :           0 :                   stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (slp_root);
    5157                 :             : 
    5158                 :           0 :                   vect_free_slp_instance (instance);
    5159                 :           0 :                   limit = max_tree_size;
    5160                 :           0 :                   bool res = vect_analyze_slp_instance (vinfo, bst_map,
    5161                 :             :                                                         stmt_info,
    5162                 :             :                                                         slp_inst_kind_store,
    5163                 :             :                                                         max_tree_size, &limit,
    5164                 :             :                                                         true);
    5165                 :           0 :                   gcc_assert (res);
    5166                 :           0 :                   auto new_inst = LOOP_VINFO_SLP_INSTANCES (vinfo).pop ();
    5167                 :           0 :                   LOOP_VINFO_SLP_INSTANCES (vinfo)[i] = new_inst;
    5168                 :             :                 }
    5169                 :             :             }
    5170                 :           0 :         }
    5171                 :             : 
    5172                 :             :   /* When we end up with load permutations that we cannot possibly handle,
    5173                 :             :      like those requiring three vector inputs, lower them using interleaving
    5174                 :             :      like schemes.  */
    5175                 :      837809 :   if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
    5176                 :             :     {
    5177                 :      241907 :       vect_lower_load_permutations (loop_vinfo, bst_map, force_single_lane);
    5178                 :      241907 :       if (dump_enabled_p ())
    5179                 :             :         {
    5180                 :       19888 :           dump_printf_loc (MSG_NOTE, vect_location,
    5181                 :             :                            "SLP graph after lowering permutations:\n");
    5182                 :       19888 :           hash_set<slp_tree> visited;
    5183                 :       90328 :           FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
    5184                 :       30974 :             vect_print_slp_graph (MSG_NOTE, vect_location,
    5185                 :             :                                   SLP_INSTANCE_TREE (instance), visited);
    5186                 :       19888 :         }
    5187                 :             :     }
    5188                 :             : 
    5189                 :      837809 :   release_scalar_stmts_to_slp_tree_map (bst_map);
    5190                 :             : 
    5191                 :      837809 :   if (pattern_found && dump_enabled_p ())
    5192                 :             :     {
    5193                 :          22 :       dump_printf_loc (MSG_NOTE, vect_location,
    5194                 :             :                        "Pattern matched SLP tree\n");
    5195                 :          22 :       hash_set<slp_tree> visited;
    5196                 :         142 :       FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
    5197                 :          76 :         vect_print_slp_graph (MSG_NOTE, vect_location,
    5198                 :             :                               SLP_INSTANCE_TREE (instance), visited);
    5199                 :          22 :     }
    5200                 :             : 
    5201                 :      837809 :   return opt_result::success ();
    5202                 :      837809 : }
    5203                 :             : 
    5204                 :             : /* Estimates the cost of inserting layout changes into the SLP graph.
    5205                 :             :    It can also say that the insertion is impossible.  */
    5206                 :             : 
    5207                 :             : struct slpg_layout_cost
    5208                 :             : {
    5209                 :     8248368 :   slpg_layout_cost () = default;
    5210                 :             :   slpg_layout_cost (sreal, bool);
    5211                 :             : 
    5212                 :      323486 :   static slpg_layout_cost impossible () { return { sreal::max (), 0 }; }
    5213                 :     3543348 :   bool is_possible () const { return depth != sreal::max (); }
    5214                 :             : 
    5215                 :             :   bool operator== (const slpg_layout_cost &) const;
    5216                 :             :   bool operator!= (const slpg_layout_cost &) const;
    5217                 :             : 
    5218                 :             :   bool is_better_than (const slpg_layout_cost &, bool) const;
    5219                 :             : 
    5220                 :             :   void add_parallel_cost (const slpg_layout_cost &);
    5221                 :             :   void add_serial_cost (const slpg_layout_cost &);
    5222                 :             :   void split (unsigned int);
    5223                 :             : 
    5224                 :             :   /* The longest sequence of layout changes needed during any traversal
    5225                 :             :      of the partition dag, weighted by execution frequency.
    5226                 :             : 
    5227                 :             :      This is the most important metric when optimizing for speed, since
    5228                 :             :      it helps to ensure that we keep the number of operations on
    5229                 :             :      critical paths to a minimum.  */
    5230                 :             :   sreal depth = 0;
    5231                 :             : 
    5232                 :             :   /* An estimate of the total number of operations needed.  It is weighted by
    5233                 :             :      execution frequency when optimizing for speed but not when optimizing for
    5234                 :             :      size.  In order to avoid double-counting, a node with a fanout of N will
    5235                 :             :      distribute 1/N of its total cost to each successor.
    5236                 :             : 
    5237                 :             :      This is the most important metric when optimizing for size, since
    5238                 :             :      it helps to keep the total number of operations to a minimum,  */
    5239                 :             :   sreal total = 0;
    5240                 :             : };
    5241                 :             : 
    5242                 :             : /* Construct costs for a node with weight WEIGHT.  A higher weight
    5243                 :             :    indicates more frequent execution.  IS_FOR_SIZE is true if we are
    5244                 :             :    optimizing for size rather than speed.  */
    5245                 :             : 
    5246                 :      805348 : slpg_layout_cost::slpg_layout_cost (sreal weight, bool is_for_size)
    5247                 :      806097 :   : depth (weight), total (is_for_size && weight > 0 ? 1 : weight)
    5248                 :             : {
    5249                 :      805348 : }
    5250                 :             : 
    5251                 :             : bool
    5252                 :           0 : slpg_layout_cost::operator== (const slpg_layout_cost &other) const
    5253                 :             : {
    5254                 :           0 :   return depth == other.depth && total == other.total;
    5255                 :             : }
    5256                 :             : 
    5257                 :             : bool
    5258                 :           0 : slpg_layout_cost::operator!= (const slpg_layout_cost &other) const
    5259                 :             : {
    5260                 :           0 :   return !operator== (other);
    5261                 :             : }
    5262                 :             : 
    5263                 :             : /* Return true if these costs are better than OTHER.  IS_FOR_SIZE is
    5264                 :             :    true if we are optimizing for size rather than speed.  */
    5265                 :             : 
    5266                 :             : bool
    5267                 :      216317 : slpg_layout_cost::is_better_than (const slpg_layout_cost &other,
    5268                 :             :                                   bool is_for_size) const
    5269                 :             : {
    5270                 :      216317 :   if (is_for_size)
    5271                 :             :     {
    5272                 :         327 :       if (total != other.total)
    5273                 :         130 :         return total < other.total;
    5274                 :         197 :       return depth < other.depth;
    5275                 :             :     }
    5276                 :             :   else
    5277                 :             :     {
    5278                 :      215990 :       if (depth != other.depth)
    5279                 :       98978 :         return depth < other.depth;
    5280                 :      117012 :       return total < other.total;
    5281                 :             :     }
    5282                 :             : }
    5283                 :             : 
    5284                 :             : /* Increase the costs to account for something with cost INPUT_COST
    5285                 :             :    happening in parallel with the current costs.  */
    5286                 :             : 
    5287                 :             : void
    5288                 :      232588 : slpg_layout_cost::add_parallel_cost (const slpg_layout_cost &input_cost)
    5289                 :             : {
    5290                 :      232588 :   depth = std::max (depth, input_cost.depth);
    5291                 :      232588 :   total += input_cost.total;
    5292                 :      232588 : }
    5293                 :             : 
    5294                 :             : /* Increase the costs to account for something with cost INPUT_COST
    5295                 :             :    happening in series with the current costs.  */
    5296                 :             : 
    5297                 :             : void
    5298                 :     1002299 : slpg_layout_cost::add_serial_cost (const slpg_layout_cost &other)
    5299                 :             : {
    5300                 :     1002299 :   depth += other.depth;
    5301                 :     1002299 :   total += other.total;
    5302                 :     1002299 : }
    5303                 :             : 
    5304                 :             : /* Split the total cost among TIMES successors or predecessors.  */
    5305                 :             : 
    5306                 :             : void
    5307                 :      773581 : slpg_layout_cost::split (unsigned int times)
    5308                 :             : {
    5309                 :      773581 :   if (times > 1)
    5310                 :      250609 :     total /= times;
    5311                 :      773581 : }
    5312                 :             : 
    5313                 :             : /* Information about one node in the SLP graph, for use during
    5314                 :             :    vect_optimize_slp_pass.  */
    5315                 :             : 
    5316                 :             : struct slpg_vertex
    5317                 :             : {
    5318                 :     7982617 :   slpg_vertex (slp_tree node_) : node (node_) {}
    5319                 :             : 
    5320                 :             :   /* The node itself.  */
    5321                 :             :   slp_tree node;
    5322                 :             : 
    5323                 :             :   /* Which partition the node belongs to, or -1 if none.  Nodes outside of
    5324                 :             :      partitions are flexible; they can have whichever layout consumers
    5325                 :             :      want them to have.  */
    5326                 :             :   int partition = -1;
    5327                 :             : 
    5328                 :             :   /* The number of nodes that directly use the result of this one
    5329                 :             :      (i.e. the number of nodes that count this one as a child).  */
    5330                 :             :   unsigned int out_degree = 0;
    5331                 :             : 
    5332                 :             :   /* The execution frequency of the node.  */
    5333                 :             :   sreal weight = 0;
    5334                 :             : 
    5335                 :             :   /* The total execution frequency of all nodes that directly use the
    5336                 :             :      result of this one.  */
    5337                 :             :   sreal out_weight = 0;
    5338                 :             : };
    5339                 :             : 
    5340                 :             : /* Information about one partition of the SLP graph, for use during
    5341                 :             :    vect_optimize_slp_pass.  */
    5342                 :             : 
    5343                 :     2631804 : struct slpg_partition_info
    5344                 :             : {
    5345                 :             :   /* The nodes in the partition occupy indices [NODE_BEGIN, NODE_END)
    5346                 :             :      of m_partitioned_nodes.  */
    5347                 :             :   unsigned int node_begin = 0;
    5348                 :             :   unsigned int node_end = 0;
    5349                 :             : 
    5350                 :             :   /* Which layout we've chosen to use for this partition, or -1 if
    5351                 :             :      we haven't picked one yet.  */
    5352                 :             :   int layout = -1;
    5353                 :             : 
    5354                 :             :   /* The number of predecessors and successors in the partition dag.
    5355                 :             :      The predecessors always have lower partition numbers and the
    5356                 :             :      successors always have higher partition numbers.
    5357                 :             : 
    5358                 :             :      Note that the directions of these edges are not necessarily the
    5359                 :             :      same as in the data flow graph.  For example, if an SCC has separate
    5360                 :             :      partitions for an inner loop and an outer loop, the inner loop's
    5361                 :             :      partition will have at least two incoming edges from the outer loop's
    5362                 :             :      partition: one for a live-in value and one for a live-out value.
    5363                 :             :      In data flow terms, one of these edges would also be from the outer loop
    5364                 :             :      to the inner loop, but the other would be in the opposite direction.  */
    5365                 :             :   unsigned int in_degree = 0;
    5366                 :             :   unsigned int out_degree = 0;
    5367                 :             : };
    5368                 :             : 
    5369                 :             : /* Information about the costs of using a particular layout for a
    5370                 :             :    particular partition.  It can also say that the combination is
    5371                 :             :    impossible.  */
    5372                 :             : 
    5373                 :             : struct slpg_partition_layout_costs
    5374                 :             : {
    5375                 :     1041059 :   bool is_possible () const { return internal_cost.is_possible (); }
    5376                 :       37276 :   void mark_impossible () { internal_cost = slpg_layout_cost::impossible (); }
    5377                 :             : 
    5378                 :             :   /* The costs inherited from predecessor partitions.  */
    5379                 :             :   slpg_layout_cost in_cost;
    5380                 :             : 
    5381                 :             :   /* The inherent cost of the layout within the node itself.  For example,
    5382                 :             :      this is nonzero for a load if choosing a particular layout would require
    5383                 :             :      the load to permute the loaded elements.  It is nonzero for a
    5384                 :             :      VEC_PERM_EXPR if the permutation cannot be eliminated or converted
    5385                 :             :      to full-vector moves.  */
    5386                 :             :   slpg_layout_cost internal_cost;
    5387                 :             : 
    5388                 :             :   /* The costs inherited from successor partitions.  */
    5389                 :             :   slpg_layout_cost out_cost;
    5390                 :             : };
    5391                 :             : 
    5392                 :             : /* This class tries to optimize the layout of vectors in order to avoid
    5393                 :             :    unnecessary shuffling.  At the moment, the set of possible layouts are
    5394                 :             :    restricted to bijective permutations.
    5395                 :             : 
    5396                 :             :    The goal of the pass depends on whether we're optimizing for size or
    5397                 :             :    for speed.  When optimizing for size, the goal is to reduce the overall
    5398                 :             :    number of layout changes (including layout changes implied by things
    5399                 :             :    like load permutations).  When optimizing for speed, the goal is to
    5400                 :             :    reduce the maximum latency attributable to layout changes on any
    5401                 :             :    non-cyclical path through the data flow graph.
    5402                 :             : 
    5403                 :             :    For example, when optimizing a loop nest for speed, we will prefer
    5404                 :             :    to make layout changes outside of a loop rather than inside of a loop,
    5405                 :             :    and will prefer to make layout changes in parallel rather than serially,
    5406                 :             :    even if that increases the overall number of layout changes.
    5407                 :             : 
    5408                 :             :    The high-level procedure is:
    5409                 :             : 
    5410                 :             :    (1) Build a graph in which edges go from uses (parents) to definitions
    5411                 :             :        (children).
    5412                 :             : 
    5413                 :             :    (2) Divide the graph into a dag of strongly-connected components (SCCs).
    5414                 :             : 
    5415                 :             :    (3) When optimizing for speed, partition the nodes in each SCC based
    5416                 :             :        on their containing cfg loop.  When optimizing for size, treat
    5417                 :             :        each SCC as a single partition.
    5418                 :             : 
    5419                 :             :        This gives us a dag of partitions.  The goal is now to assign a
    5420                 :             :        layout to each partition.
    5421                 :             : 
    5422                 :             :    (4) Construct a set of vector layouts that are worth considering.
    5423                 :             :        Record which nodes must keep their current layout.
    5424                 :             : 
    5425                 :             :    (5) Perform a forward walk over the partition dag (from loads to stores)
    5426                 :             :        accumulating the "forward" cost of using each layout.  When visiting
    5427                 :             :        each partition, assign a tentative choice of layout to the partition
    5428                 :             :        and use that choice when calculating the cost of using a different
    5429                 :             :        layout in successor partitions.
    5430                 :             : 
    5431                 :             :    (6) Perform a backward walk over the partition dag (from stores to loads),
    5432                 :             :        accumulating the "backward" cost of using each layout.  When visiting
    5433                 :             :        each partition, make a final choice of layout for that partition based
    5434                 :             :        on the accumulated forward costs (from (5)) and backward costs
    5435                 :             :        (from (6)).
    5436                 :             : 
    5437                 :             :    (7) Apply the chosen layouts to the SLP graph.
    5438                 :             : 
    5439                 :             :    For example, consider the SLP statements:
    5440                 :             : 
    5441                 :             :    S1:      a_1 = load
    5442                 :             :        loop:
    5443                 :             :    S2:      a_2 = PHI<a_1, a_3>
    5444                 :             :    S3:      b_1 = load
    5445                 :             :    S4:      a_3 = a_2 + b_1
    5446                 :             :        exit:
    5447                 :             :    S5:      a_4 = PHI<a_3>
    5448                 :             :    S6:      store a_4
    5449                 :             : 
    5450                 :             :    S2 and S4 form an SCC and are part of the same loop.  Every other
    5451                 :             :    statement is in a singleton SCC.  In this example there is a one-to-one
    5452                 :             :    mapping between SCCs and partitions and the partition dag looks like this;
    5453                 :             : 
    5454                 :             :         S1     S3
    5455                 :             :          \     /
    5456                 :             :           S2+S4
    5457                 :             :             |
    5458                 :             :            S5
    5459                 :             :             |
    5460                 :             :            S6
    5461                 :             : 
    5462                 :             :    S2, S3 and S4 will have a higher execution frequency than the other
    5463                 :             :    statements, so when optimizing for speed, the goal is to avoid any
    5464                 :             :    layout changes:
    5465                 :             : 
    5466                 :             :    - within S3
    5467                 :             :    - within S2+S4
    5468                 :             :    - on the S3->S2+S4 edge
    5469                 :             : 
    5470                 :             :    For example, if S3 was originally a reversing load, the goal of the
    5471                 :             :    pass is to make it an unreversed load and change the layout on the
    5472                 :             :    S1->S2+S4 and S2+S4->S5 edges to compensate.  (Changing the layout
    5473                 :             :    on S1->S2+S4 and S5->S6 would also be acceptable.)
    5474                 :             : 
    5475                 :             :    The difference between SCCs and partitions becomes important if we
    5476                 :             :    add an outer loop:
    5477                 :             : 
    5478                 :             :    S1:      a_1 = ...
    5479                 :             :        loop1:
    5480                 :             :    S2:      a_2 = PHI<a_1, a_6>
    5481                 :             :    S3:      b_1 = load
    5482                 :             :    S4:      a_3 = a_2 + b_1
    5483                 :             :        loop2:
    5484                 :             :    S5:      a_4 = PHI<a_3, a_5>
    5485                 :             :    S6:      c_1 = load
    5486                 :             :    S7:      a_5 = a_4 + c_1
    5487                 :             :        exit2:
    5488                 :             :    S8:      a_6 = PHI<a_5>
    5489                 :             :    S9:      store a_6
    5490                 :             :        exit1:
    5491                 :             : 
    5492                 :             :    Here, S2, S4, S5, S7 and S8 form a single SCC.  However, when optimizing
    5493                 :             :    for speed, we usually do not want restrictions in the outer loop to "infect"
    5494                 :             :    the decision for the inner loop.  For example, if an outer-loop node
    5495                 :             :    in the SCC contains a statement with a fixed layout, that should not
    5496                 :             :    prevent the inner loop from using a different layout.  Conversely,
    5497                 :             :    the inner loop should not dictate a layout to the outer loop: if the
    5498                 :             :    outer loop does a lot of computation, then it may not be efficient to
    5499                 :             :    do all of that computation in the inner loop's preferred layout.
    5500                 :             : 
    5501                 :             :    So when optimizing for speed, we partition the SCC into S2+S4+S8 (outer)
    5502                 :             :    and S5+S7 (inner).  We also try to arrange partitions so that:
    5503                 :             : 
    5504                 :             :    - the partition for an outer loop comes before the partition for
    5505                 :             :      an inner loop
    5506                 :             : 
    5507                 :             :    - if a sibling loop A dominates a sibling loop B, A's partition
    5508                 :             :      comes before B's
    5509                 :             : 
    5510                 :             :    This gives the following partition dag for the example above:
    5511                 :             : 
    5512                 :             :         S1        S3
    5513                 :             :          \        /
    5514                 :             :           S2+S4+S8   S6
    5515                 :             :            |   \\    /
    5516                 :             :            |    S5+S7
    5517                 :             :            |
    5518                 :             :           S9
    5519                 :             : 
    5520                 :             :    There are two edges from S2+S4+S8 to S5+S7: one for the edge S4->S5 and
    5521                 :             :    one for a reversal of the edge S7->S8.
    5522                 :             : 
    5523                 :             :    The backward walk picks a layout for S5+S7 before S2+S4+S8.  The choice
    5524                 :             :    for S2+S4+S8 therefore has to balance the cost of using the outer loop's
    5525                 :             :    preferred layout against the cost of changing the layout on entry to the
    5526                 :             :    inner loop (S4->S5) and on exit from the inner loop (S7->S8 reversed).
    5527                 :             : 
    5528                 :             :    Although this works well when optimizing for speed, it has the downside
    5529                 :             :    when optimizing for size that the choice of layout for S5+S7 is completely
    5530                 :             :    independent of S9, which lessens the chance of reducing the overall number
    5531                 :             :    of permutations.  We therefore do not partition SCCs when optimizing
    5532                 :             :    for size.
    5533                 :             : 
    5534                 :             :    To give a concrete example of the difference between optimizing
    5535                 :             :    for size and speed, consider:
    5536                 :             : 
    5537                 :             :    a[0] = (b[1] << c[3]) - d[1];
    5538                 :             :    a[1] = (b[0] << c[2]) - d[0];
    5539                 :             :    a[2] = (b[3] << c[1]) - d[3];
    5540                 :             :    a[3] = (b[2] << c[0]) - d[2];
    5541                 :             : 
    5542                 :             :    There are three different layouts here: one for a, one for b and d,
    5543                 :             :    and one for c.  When optimizing for speed it is better to permute each
    5544                 :             :    of b, c and d into the order required by a, since those permutations
    5545                 :             :    happen in parallel.  But when optimizing for size, it is better to:
    5546                 :             : 
    5547                 :             :    - permute c into the same order as b
    5548                 :             :    - do the arithmetic
    5549                 :             :    - permute the result into the order required by a
    5550                 :             : 
    5551                 :             :    This gives 2 permutations rather than 3.  */
    5552                 :             : 
    5553                 :             : class vect_optimize_slp_pass
    5554                 :             : {
    5555                 :             : public:
    5556                 :      553379 :   vect_optimize_slp_pass (vec_info *vinfo) : m_vinfo (vinfo) {}
    5557                 :             :   void run ();
    5558                 :             : 
    5559                 :             : private:
    5560                 :             :   /* Graph building.  */
    5561                 :             :   struct loop *containing_loop (slp_tree);
    5562                 :             :   bool is_cfg_latch_edge (graph_edge *);
    5563                 :             :   void build_vertices (hash_set<slp_tree> &, slp_tree);
    5564                 :             :   void build_vertices ();
    5565                 :             :   void build_graph ();
    5566                 :             : 
    5567                 :             :   /* Partitioning.  */
    5568                 :             :   void create_partitions ();
    5569                 :             :   template<typename T> void for_each_partition_edge (unsigned int, T);
    5570                 :             : 
    5571                 :             :   /* Layout selection.  */
    5572                 :             :   bool is_compatible_layout (slp_tree, unsigned int);
    5573                 :             :   int change_layout_cost (slp_tree, unsigned int, unsigned int);
    5574                 :             :   slpg_partition_layout_costs &partition_layout_costs (unsigned int,
    5575                 :             :                                                        unsigned int);
    5576                 :             :   void change_vec_perm_layout (slp_tree, lane_permutation_t &,
    5577                 :             :                                int, unsigned int);
    5578                 :             :   int internal_node_cost (slp_tree, int, unsigned int);
    5579                 :             :   void start_choosing_layouts ();
    5580                 :             : 
    5581                 :             :   /* Cost propagation.  */
    5582                 :             :   slpg_layout_cost edge_layout_cost (graph_edge *, unsigned int,
    5583                 :             :                                      unsigned int, unsigned int);
    5584                 :             :   slpg_layout_cost total_in_cost (unsigned int);
    5585                 :             :   slpg_layout_cost forward_cost (graph_edge *, unsigned int, unsigned int);
    5586                 :             :   slpg_layout_cost backward_cost (graph_edge *, unsigned int, unsigned int);
    5587                 :             :   void forward_pass ();
    5588                 :             :   void backward_pass ();
    5589                 :             : 
    5590                 :             :   /* Rematerialization.  */
    5591                 :             :   slp_tree get_result_with_layout (slp_tree, unsigned int);
    5592                 :             :   void materialize ();
    5593                 :             : 
    5594                 :             :   /* Clean-up.  */
    5595                 :             :   void remove_redundant_permutations ();
    5596                 :             : 
    5597                 :             :   /* Masked load lanes discovery.  */
    5598                 :             :   void decide_masked_load_lanes ();
    5599                 :             : 
    5600                 :             :   void dump ();
    5601                 :             : 
    5602                 :             :   vec_info *m_vinfo;
    5603                 :             : 
    5604                 :             :   /* True if we should optimize the graph for size, false if we should
    5605                 :             :      optimize it for speed.  (It wouldn't be easy to make this decision
    5606                 :             :      more locally.)  */
    5607                 :             :   bool m_optimize_size;
    5608                 :             : 
    5609                 :             :   /* A graph of all SLP nodes, with edges leading from uses to definitions.
    5610                 :             :      In other words, a node's predecessors are its slp_tree parents and
    5611                 :             :      a node's successors are its slp_tree children.  */
    5612                 :             :   graph *m_slpg = nullptr;
    5613                 :             : 
    5614                 :             :   /* The vertices of M_SLPG, indexed by slp_tree::vertex.  */
    5615                 :             :   auto_vec<slpg_vertex> m_vertices;
    5616                 :             : 
    5617                 :             :   /* The list of all leaves of M_SLPG. such as external definitions, constants,
    5618                 :             :      and loads.  */
    5619                 :             :   auto_vec<int> m_leafs;
    5620                 :             : 
    5621                 :             :   /* This array has one entry for every vector layout that we're considering.
    5622                 :             :      Element 0 is null and indicates "no change".  Other entries describe
    5623                 :             :      permutations that are inherent in the current graph and that we would
    5624                 :             :      like to reverse if possible.
    5625                 :             : 
    5626                 :             :      For example, a permutation { 1, 2, 3, 0 } means that something has
    5627                 :             :      effectively been permuted in that way, such as a load group
    5628                 :             :      { a[1], a[2], a[3], a[0] } (viewed as a permutation of a[0:3]).
    5629                 :             :      We'd then like to apply the reverse permutation { 3, 0, 1, 2 }
    5630                 :             :      in order to put things "back" in order.  */
    5631                 :             :   auto_vec<vec<unsigned> > m_perms;
    5632                 :             : 
    5633                 :             :   /* A partitioning of the nodes for which a layout must be chosen.
    5634                 :             :      Each partition represents an <SCC, cfg loop> pair; that is,
    5635                 :             :      nodes in different SCCs belong to different partitions, and nodes
    5636                 :             :      within an SCC can be further partitioned according to a containing
    5637                 :             :      cfg loop.  Partition <SCC1, L1> comes before <SCC2, L2> if:
    5638                 :             : 
    5639                 :             :      - SCC1 != SCC2 and SCC1 is a predecessor of SCC2 in a forward walk
    5640                 :             :        from leaves (such as loads) to roots (such as stores).
    5641                 :             : 
    5642                 :             :      - SCC1 == SCC2 and L1's header strictly dominates L2's header.  */
    5643                 :             :   auto_vec<slpg_partition_info> m_partitions;
    5644                 :             : 
    5645                 :             :   /* The list of all nodes for which a layout must be chosen.  Nodes for
    5646                 :             :      partition P come before the nodes for partition P+1.  Nodes within a
    5647                 :             :      partition are in reverse postorder.  */
    5648                 :             :   auto_vec<unsigned int> m_partitioned_nodes;
    5649                 :             : 
    5650                 :             :   /* Index P * num-layouts + L contains the cost of using layout L
    5651                 :             :      for partition P.  */
    5652                 :             :   auto_vec<slpg_partition_layout_costs> m_partition_layout_costs;
    5653                 :             : 
    5654                 :             :   /* Index N * num-layouts + L, if nonnull, is a node that provides the
    5655                 :             :      original output of node N adjusted to have layout L.  */
    5656                 :             :   auto_vec<slp_tree> m_node_layouts;
    5657                 :             : };
    5658                 :             : 
    5659                 :             : /* Fill the vertices and leafs vector with all nodes in the SLP graph.
    5660                 :             :    Also record whether we should optimize anything for speed rather
    5661                 :             :    than size.  */
    5662                 :             : 
    5663                 :             : void
    5664                 :     8576149 : vect_optimize_slp_pass::build_vertices (hash_set<slp_tree> &visited,
    5665                 :             :                                         slp_tree node)
    5666                 :             : {
    5667                 :     8576149 :   unsigned i;
    5668                 :     8576149 :   slp_tree child;
    5669                 :             : 
    5670                 :     8576149 :   if (visited.add (node))
    5671                 :     8576149 :     return;
    5672                 :             : 
    5673                 :     7982617 :   if (stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node))
    5674                 :             :     {
    5675                 :     5839312 :       basic_block bb = gimple_bb (vect_orig_stmt (rep)->stmt);
    5676                 :     5428196 :       if (optimize_bb_for_speed_p (bb))
    5677                 :     5313656 :         m_optimize_size = false;
    5678                 :             :     }
    5679                 :             : 
    5680                 :     7982617 :   node->vertex = m_vertices.length ();
    5681                 :     7982617 :   m_vertices.safe_push (slpg_vertex (node));
    5682                 :             : 
    5683                 :     7982617 :   bool leaf = true;
    5684                 :     7982617 :   bool force_leaf = false;
    5685                 :    14926712 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    5686                 :     6944095 :     if (child)
    5687                 :             :       {
    5688                 :     6173229 :         leaf = false;
    5689                 :     6173229 :         build_vertices (visited, child);
    5690                 :             :       }
    5691                 :             :     else
    5692                 :             :       force_leaf = true;
    5693                 :             :   /* Since SLP discovery works along use-def edges all cycles have an
    5694                 :             :      entry - but there's the exception of cycles where we do not handle
    5695                 :             :      the entry explicitely (but with a NULL SLP node), like some reductions
    5696                 :             :      and inductions.  Force those SLP PHIs to act as leafs to make them
    5697                 :             :      backwards reachable.  */
    5698                 :     7982617 :   if (leaf || force_leaf)
    5699                 :     3882142 :     m_leafs.safe_push (node->vertex);
    5700                 :             : }
    5701                 :             : 
    5702                 :             : /* Fill the vertices and leafs vector with all nodes in the SLP graph.  */
    5703                 :             : 
    5704                 :             : void
    5705                 :     1106758 : vect_optimize_slp_pass::build_vertices ()
    5706                 :             : {
    5707                 :     1106758 :   hash_set<slp_tree> visited;
    5708                 :     1106758 :   unsigned i;
    5709                 :     1106758 :   slp_instance instance;
    5710                 :     1106758 :   m_vertices.truncate (0);
    5711                 :     1106758 :   m_leafs.truncate (0);
    5712                 :     5723194 :   FOR_EACH_VEC_ELT (m_vinfo->slp_instances, i, instance)
    5713                 :     2402920 :     build_vertices (visited, SLP_INSTANCE_TREE (instance));
    5714                 :     1106758 : }
    5715                 :             : 
    5716                 :             : /* Apply (reverse) bijectite PERM to VEC.  */
    5717                 :             : 
    5718                 :             : template <class T>
    5719                 :             : static void
    5720                 :      139364 : vect_slp_permute (vec<unsigned> perm,
    5721                 :             :                   vec<T> &vec, bool reverse)
    5722                 :             : {
    5723                 :      139364 :   auto_vec<T, 64> saved;
    5724                 :      139364 :   saved.create (vec.length ());
    5725                 :      477902 :   for (unsigned i = 0; i < vec.length (); ++i)
    5726                 :      338538 :     saved.quick_push (vec[i]);
    5727                 :             : 
    5728                 :      139364 :   if (reverse)
    5729                 :             :     {
    5730                 :      948246 :       for (unsigned i = 0; i < vec.length (); ++i)
    5731                 :      337470 :         vec[perm[i]] = saved[i];
    5732                 :      476360 :       for (unsigned i = 0; i < vec.length (); ++i)
    5733                 :      577246 :         gcc_assert (vec[perm[i]] == saved[i]);
    5734                 :             :     }
    5735                 :             :   else
    5736                 :             :     {
    5737                 :        3084 :       for (unsigned i = 0; i < vec.length (); ++i)
    5738                 :        1068 :         vec[i] = saved[perm[i]];
    5739                 :      140432 :       for (unsigned i = 0; i < vec.length (); ++i)
    5740                 :        1602 :         gcc_assert (vec[i] == saved[perm[i]]);
    5741                 :             :     }
    5742                 :      139364 : }
    5743                 :             : 
    5744                 :             : /* Return the cfg loop that contains NODE.  */
    5745                 :             : 
    5746                 :             : struct loop *
    5747                 :     3009082 : vect_optimize_slp_pass::containing_loop (slp_tree node)
    5748                 :             : {
    5749                 :     3009082 :   stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
    5750                 :     3009082 :   if (!rep)
    5751                 :        5335 :     return ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father;
    5752                 :     3218665 :   return gimple_bb (vect_orig_stmt (rep)->stmt)->loop_father;
    5753                 :             : }
    5754                 :             : 
    5755                 :             : /* Return true if UD (an edge from a use to a definition) is associated
    5756                 :             :    with a loop latch edge in the cfg.  */
    5757                 :             : 
    5758                 :             : bool
    5759                 :     6173229 : vect_optimize_slp_pass::is_cfg_latch_edge (graph_edge *ud)
    5760                 :             : {
    5761                 :     6173229 :   slp_tree use = m_vertices[ud->src].node;
    5762                 :     6173229 :   slp_tree def = m_vertices[ud->dest].node;
    5763                 :     6173229 :   if ((SLP_TREE_DEF_TYPE (use) != vect_internal_def
    5764                 :     6173229 :        || SLP_TREE_CODE (use) == VEC_PERM_EXPR)
    5765                 :     5890742 :       || SLP_TREE_DEF_TYPE (def) != vect_internal_def)
    5766                 :             :     return false;
    5767                 :             : 
    5768                 :     3349670 :   stmt_vec_info use_rep = vect_orig_stmt (SLP_TREE_REPRESENTATIVE (use));
    5769                 :     3349670 :   return (is_a<gphi *> (use_rep->stmt)
    5770                 :      289572 :           && bb_loop_header_p (gimple_bb (use_rep->stmt))
    5771                 :     3508970 :           && containing_loop (def) == containing_loop (use));
    5772                 :             : }
    5773                 :             : 
    5774                 :             : /* Build the graph.  Mark edges that correspond to cfg loop latch edges with
    5775                 :             :    a nonnull data field.  */
    5776                 :             : 
    5777                 :             : void
    5778                 :     1106758 : vect_optimize_slp_pass::build_graph ()
    5779                 :             : {
    5780                 :     1106758 :   m_optimize_size = true;
    5781                 :     1106758 :   build_vertices ();
    5782                 :             : 
    5783                 :     2213516 :   m_slpg = new_graph (m_vertices.length ());
    5784                 :    11302891 :   for (slpg_vertex &v : m_vertices)
    5785                 :    24032886 :     for (slp_tree child : SLP_TREE_CHILDREN (v.node))
    5786                 :     6944095 :       if (child)
    5787                 :             :         {
    5788                 :     6173229 :           graph_edge *ud = add_edge (m_slpg, v.node->vertex, child->vertex);
    5789                 :     6173229 :           if (is_cfg_latch_edge (ud))
    5790                 :      151626 :             ud->data = this;
    5791                 :             :         }
    5792                 :     1106758 : }
    5793                 :             : 
    5794                 :             : /* Return true if E corresponds to a loop latch edge in the cfg.  */
    5795                 :             : 
    5796                 :             : static bool
    5797                 :     3162285 : skip_cfg_latch_edges (graph_edge *e)
    5798                 :             : {
    5799                 :     3162285 :   return e->data;
    5800                 :             : }
    5801                 :             : 
    5802                 :             : /* Create the node partitions.  */
    5803                 :             : 
    5804                 :             : void
    5805                 :      553379 : vect_optimize_slp_pass::create_partitions ()
    5806                 :             : {
    5807                 :             :   /* Calculate a postorder of the graph, ignoring edges that correspond
    5808                 :             :      to natural latch edges in the cfg.  Reading the vector from the end
    5809                 :             :      to the beginning gives the reverse postorder.  */
    5810                 :      553379 :   auto_vec<int> initial_rpo;
    5811                 :     1106758 :   graphds_dfs (m_slpg, &m_leafs[0], m_leafs.length (), &initial_rpo,
    5812                 :             :                false, NULL, skip_cfg_latch_edges);
    5813                 :     1660137 :   gcc_assert (initial_rpo.length () == m_vertices.length ());
    5814                 :             : 
    5815                 :             :   /* Calculate the strongly connected components of the graph.  */
    5816                 :      553379 :   auto_vec<int> scc_grouping;
    5817                 :      553379 :   unsigned int num_sccs = graphds_scc (m_slpg, NULL, NULL, &scc_grouping);
    5818                 :             : 
    5819                 :             :   /* Create a new index order in which all nodes from the same SCC are
    5820                 :             :      consecutive.  Use scc_pos to record the index of the first node in
    5821                 :             :      each SCC.  */
    5822                 :      553379 :   auto_vec<unsigned int> scc_pos (num_sccs);
    5823                 :      553379 :   int last_component = -1;
    5824                 :      553379 :   unsigned int node_count = 0;
    5825                 :     5651305 :   for (unsigned int node_i : scc_grouping)
    5826                 :             :     {
    5827                 :     3991168 :       if (last_component != m_slpg->vertices[node_i].component)
    5828                 :             :         {
    5829                 :     3903040 :           last_component = m_slpg->vertices[node_i].component;
    5830                 :     7806080 :           gcc_assert (last_component == int (scc_pos.length ()));
    5831                 :     3903040 :           scc_pos.quick_push (node_count);
    5832                 :             :         }
    5833                 :     3991168 :       node_count += 1;
    5834                 :             :     }
    5835                 :     1106758 :   gcc_assert (node_count == initial_rpo.length ()
    5836                 :             :               && last_component + 1 == int (num_sccs));
    5837                 :             : 
    5838                 :             :   /* Use m_partitioned_nodes to group nodes into SCC order, with the nodes
    5839                 :             :      inside each SCC following the RPO we calculated above.  The fact that
    5840                 :             :      we ignored natural latch edges when calculating the RPO should ensure
    5841                 :             :      that, for natural loop nests:
    5842                 :             : 
    5843                 :             :      - the first node that we encounter in a cfg loop is the loop header phi
    5844                 :             :      - the loop header phis are in dominance order
    5845                 :             : 
    5846                 :             :      Arranging for this is an optimization (see below) rather than a
    5847                 :             :      correctness issue.  Unnatural loops with a tangled mess of backedges
    5848                 :             :      will still work correctly, but might give poorer results.
    5849                 :             : 
    5850                 :             :      Also update scc_pos so that it gives 1 + the index of the last node
    5851                 :             :      in the SCC.  */
    5852                 :      553379 :   m_partitioned_nodes.safe_grow (node_count);
    5853                 :     5097926 :   for (unsigned int old_i = initial_rpo.length (); old_i-- > 0;)
    5854                 :             :     {
    5855                 :     3991168 :       unsigned int node_i = initial_rpo[old_i];
    5856                 :     3991168 :       unsigned int new_i = scc_pos[m_slpg->vertices[node_i].component]++;
    5857                 :     3991168 :       m_partitioned_nodes[new_i] = node_i;
    5858                 :             :     }
    5859                 :             : 
    5860                 :             :   /* When optimizing for speed, partition each SCC based on the containing
    5861                 :             :      cfg loop. The order we constructed above should ensure that, for natural
    5862                 :             :      cfg loops, we'll create sub-SCC partitions for outer loops before
    5863                 :             :      the corresponding sub-SCC partitions for inner loops.  Similarly,
    5864                 :             :      when one sibling loop A dominates another sibling loop B, we should
    5865                 :             :      create a sub-SCC partition for A before a sub-SCC partition for B.
    5866                 :             : 
    5867                 :             :      As above, nothing depends for correctness on whether this achieves
    5868                 :             :      a natural nesting, but we should get better results when it does.  */
    5869                 :     1106758 :   m_partitions.reserve (m_vertices.length ());
    5870                 :      553379 :   unsigned int next_partition_i = 0;
    5871                 :      553379 :   hash_map<struct loop *, int> loop_partitions;
    5872                 :      553379 :   unsigned int rpo_begin = 0;
    5873                 :      553379 :   unsigned int num_partitioned_nodes = 0;
    5874                 :     5563177 :   for (unsigned int rpo_end : scc_pos)
    5875                 :             :     {
    5876                 :     3903040 :       loop_partitions.empty ();
    5877                 :             :       unsigned int partition_i = next_partition_i;
    5878                 :     7894208 :       for (unsigned int rpo_i = rpo_begin; rpo_i < rpo_end; ++rpo_i)
    5879                 :             :         {
    5880                 :             :           /* Handle externals and constants optimistically throughout.
    5881                 :             :              But treat existing vectors as fixed since we do not handle
    5882                 :             :              permuting them.  */
    5883                 :     3991168 :           unsigned int node_i = m_partitioned_nodes[rpo_i];
    5884                 :     3991168 :           auto &vertex = m_vertices[node_i];
    5885                 :     3991168 :           if ((SLP_TREE_DEF_TYPE (vertex.node) == vect_external_def
    5886                 :      427500 :                && !SLP_TREE_VEC_DEFS (vertex.node).exists ())
    5887                 :     3993485 :               || SLP_TREE_DEF_TYPE (vertex.node) == vect_constant_def)
    5888                 :     1271871 :             vertex.partition = -1;
    5889                 :             :           else
    5890                 :             :             {
    5891                 :     2719297 :               bool existed;
    5892                 :     2719297 :               if (m_optimize_size)
    5893                 :       28815 :                 existed = next_partition_i > partition_i;
    5894                 :             :               else
    5895                 :             :                 {
    5896                 :     2690482 :                   struct loop *loop = containing_loop (vertex.node);
    5897                 :     2690482 :                   auto &entry = loop_partitions.get_or_insert (loop, &existed);
    5898                 :     2690482 :                   if (!existed)
    5899                 :     2603085 :                     entry = next_partition_i;
    5900                 :     2690482 :                   partition_i = entry;
    5901                 :             :                 }
    5902                 :     2719297 :               if (!existed)
    5903                 :             :                 {
    5904                 :     2631804 :                   m_partitions.quick_push (slpg_partition_info ());
    5905                 :     2631804 :                   next_partition_i += 1;
    5906                 :             :                 }
    5907                 :     2719297 :               vertex.partition = partition_i;
    5908                 :     2719297 :               num_partitioned_nodes += 1;
    5909                 :     2719297 :               m_partitions[partition_i].node_end += 1;
    5910                 :             :             }
    5911                 :             :         }
    5912                 :     3903040 :       rpo_begin = rpo_end;
    5913                 :             :     }
    5914                 :             : 
    5915                 :             :   /* Assign ranges of consecutive node indices to each partition,
    5916                 :             :      in partition order.  Start with node_end being the same as
    5917                 :             :      node_begin so that the next loop can use it as a counter.  */
    5918                 :      553379 :   unsigned int node_begin = 0;
    5919                 :     4291941 :   for (auto &partition : m_partitions)
    5920                 :             :     {
    5921                 :     2631804 :       partition.node_begin = node_begin;
    5922                 :     2631804 :       node_begin += partition.node_end;
    5923                 :     2631804 :       partition.node_end = partition.node_begin;
    5924                 :             :     }
    5925                 :      553379 :   gcc_assert (node_begin == num_partitioned_nodes);
    5926                 :             : 
    5927                 :             :   /* Finally build the list of nodes in partition order.  */
    5928                 :      553379 :   m_partitioned_nodes.truncate (num_partitioned_nodes);
    5929                 :     4544547 :   for (unsigned int node_i = 0; node_i < m_vertices.length (); ++node_i)
    5930                 :             :     {
    5931                 :     3991168 :       int partition_i = m_vertices[node_i].partition;
    5932                 :     3991168 :       if (partition_i >= 0)
    5933                 :             :         {
    5934                 :     2719297 :           unsigned int order_i = m_partitions[partition_i].node_end++;
    5935                 :     2719297 :           m_partitioned_nodes[order_i] = node_i;
    5936                 :             :         }
    5937                 :             :     }
    5938                 :      553379 : }
    5939                 :             : 
    5940                 :             : /* Look for edges from earlier partitions into node NODE_I and edges from
    5941                 :             :    node NODE_I into later partitions.  Call:
    5942                 :             : 
    5943                 :             :       FN (ud, other_node_i)
    5944                 :             : 
    5945                 :             :    for each such use-to-def edge ud, where other_node_i is the node at the
    5946                 :             :    other end of the edge.  */
    5947                 :             : 
    5948                 :             : template<typename T>
    5949                 :             : void
    5950                 :     3001941 : vect_optimize_slp_pass::for_each_partition_edge (unsigned int node_i, T fn)
    5951                 :             : {
    5952                 :     3001941 :   int partition_i = m_vertices[node_i].partition;
    5953                 :     3001941 :   for (graph_edge *pred = m_slpg->vertices[node_i].pred;
    5954                 :     5057528 :        pred; pred = pred->pred_next)
    5955                 :             :     {
    5956                 :     2055587 :       int src_partition_i = m_vertices[pred->src].partition;
    5957                 :     2055587 :       if (src_partition_i >= 0 && src_partition_i != partition_i)
    5958                 :     1880861 :         fn (pred, pred->src);
    5959                 :             :     }
    5960                 :     3001941 :   for (graph_edge *succ = m_slpg->vertices[node_i].succ;
    5961                 :     6442974 :        succ; succ = succ->succ_next)
    5962                 :             :     {
    5963                 :     3441033 :       int dest_partition_i = m_vertices[succ->dest].partition;
    5964                 :     3441033 :       if (dest_partition_i >= 0 && dest_partition_i != partition_i)
    5965                 :     1887737 :         fn (succ, succ->dest);
    5966                 :             :     }
    5967                 :     3001941 : }
    5968                 :             : 
    5969                 :             : /* Return true if layout LAYOUT_I is compatible with the number of SLP lanes
    5970                 :             :    that NODE would operate on.  This test is independent of NODE's actual
    5971                 :             :    operation.  */
    5972                 :             : 
    5973                 :             : bool
    5974                 :     1054970 : vect_optimize_slp_pass::is_compatible_layout (slp_tree node,
    5975                 :             :                                               unsigned int layout_i)
    5976                 :             : {
    5977                 :     1054970 :   if (layout_i == 0)
    5978                 :             :     return true;
    5979                 :             : 
    5980                 :      625094 :   if (SLP_TREE_LANES (node) != m_perms[layout_i].length ())
    5981                 :        8379 :     return false;
    5982                 :             : 
    5983                 :             :   return true;
    5984                 :             : }
    5985                 :             : 
    5986                 :             : /* Return the cost (in arbtirary units) of going from layout FROM_LAYOUT_I
    5987                 :             :    to layout TO_LAYOUT_I for a node like NODE.  Return -1 if either of the
    5988                 :             :    layouts is incompatible with NODE or if the change is not possible for
    5989                 :             :    some other reason.
    5990                 :             : 
    5991                 :             :    The properties taken from NODE include the number of lanes and the
    5992                 :             :    vector type.  The actual operation doesn't matter.  */
    5993                 :             : 
    5994                 :             : int
    5995                 :      452949 : vect_optimize_slp_pass::change_layout_cost (slp_tree node,
    5996                 :             :                                             unsigned int from_layout_i,
    5997                 :             :                                             unsigned int to_layout_i)
    5998                 :             : {
    5999                 :      452949 :   if (!is_compatible_layout (node, from_layout_i)
    6000                 :      452949 :       || !is_compatible_layout (node, to_layout_i))
    6001                 :         662 :     return -1;
    6002                 :             : 
    6003                 :      452287 :   if (from_layout_i == to_layout_i)
    6004                 :             :     return 0;
    6005                 :             : 
    6006                 :      204561 :   auto_vec<slp_tree, 1> children (1);
    6007                 :      204561 :   children.quick_push (node);
    6008                 :      204561 :   auto_lane_permutation_t perm (SLP_TREE_LANES (node));
    6009                 :      204561 :   if (from_layout_i > 0)
    6010                 :      609188 :     for (unsigned int i : m_perms[from_layout_i])
    6011                 :      279584 :       perm.quick_push ({ 0, i });
    6012                 :             :   else
    6013                 :      319415 :     for (unsigned int i = 0; i < SLP_TREE_LANES (node); ++i)
    6014                 :      224722 :       perm.quick_push ({ 0, i });
    6015                 :      204561 :   if (to_layout_i > 0)
    6016                 :       95124 :     vect_slp_permute (m_perms[to_layout_i], perm, true);
    6017                 :      204561 :   auto count = vectorizable_slp_permutation_1 (m_vinfo, nullptr, node, perm,
    6018                 :             :                                                children, false);
    6019                 :      204561 :   if (count >= 0)
    6020                 :      200346 :     return MAX (count, 1);
    6021                 :             : 
    6022                 :             :   /* ??? In principle we could try changing via layout 0, giving two
    6023                 :             :      layout changes rather than 1.  Doing that would require
    6024                 :             :      corresponding support in get_result_with_layout.  */
    6025                 :             :   return -1;
    6026                 :      204561 : }
    6027                 :             : 
    6028                 :             : /* Return the costs of assigning layout LAYOUT_I to partition PARTITION_I.  */
    6029                 :             : 
    6030                 :             : inline slpg_partition_layout_costs &
    6031                 :      699428 : vect_optimize_slp_pass::partition_layout_costs (unsigned int partition_i,
    6032                 :             :                                                 unsigned int layout_i)
    6033                 :             : {
    6034                 :     1398856 :   return m_partition_layout_costs[partition_i * m_perms.length () + layout_i];
    6035                 :             : }
    6036                 :             : 
    6037                 :             : /* Change PERM in one of two ways:
    6038                 :             : 
    6039                 :             :    - if IN_LAYOUT_I < 0, accept input operand I in the layout that has been
    6040                 :             :      chosen for child I of NODE.
    6041                 :             : 
    6042                 :             :    - if IN_LAYOUT >= 0, accept all inputs operands with that layout.
    6043                 :             : 
    6044                 :             :    In both cases, arrange for the output to have layout OUT_LAYOUT_I  */
    6045                 :             : 
    6046                 :             : void
    6047                 :       22588 : vect_optimize_slp_pass::
    6048                 :             : change_vec_perm_layout (slp_tree node, lane_permutation_t &perm,
    6049                 :             :                         int in_layout_i, unsigned int out_layout_i)
    6050                 :             : {
    6051                 :      133380 :   for (auto &entry : perm)
    6052                 :             :     {
    6053                 :       65616 :       int this_in_layout_i = in_layout_i;
    6054                 :       65616 :       if (this_in_layout_i < 0)
    6055                 :             :         {
    6056                 :       49455 :           slp_tree in_node = SLP_TREE_CHILDREN (node)[entry.first];
    6057                 :       49455 :           unsigned int in_partition_i = m_vertices[in_node->vertex].partition;
    6058                 :       49455 :           if (in_partition_i == -1u)
    6059                 :         329 :             continue;
    6060                 :       49126 :           this_in_layout_i = m_partitions[in_partition_i].layout;
    6061                 :             :         }
    6062                 :       65287 :       if (this_in_layout_i > 0)
    6063                 :       13347 :         entry.second = m_perms[this_in_layout_i][entry.second];
    6064                 :             :     }
    6065                 :       22588 :   if (out_layout_i > 0)
    6066                 :        4652 :     vect_slp_permute (m_perms[out_layout_i], perm, true);
    6067                 :       22588 : }
    6068                 :             : 
    6069                 :             : /* Check whether the target allows NODE to be rearranged so that the node's
    6070                 :             :    output has layout OUT_LAYOUT_I.  Return the cost of the change if so,
    6071                 :             :    in the same arbitrary units as for change_layout_cost.  Return -1 otherwise.
    6072                 :             : 
    6073                 :             :    If NODE is a VEC_PERM_EXPR and IN_LAYOUT_I < 0, also check whether
    6074                 :             :    NODE can adapt to the layout changes that have (perhaps provisionally)
    6075                 :             :    been chosen for NODE's children, so that no extra permutations are
    6076                 :             :    needed on either the input or the output of NODE.
    6077                 :             : 
    6078                 :             :    If NODE is a VEC_PERM_EXPR and IN_LAYOUT_I >= 0, instead assume
    6079                 :             :    that all inputs will be forced into layout IN_LAYOUT_I beforehand.
    6080                 :             : 
    6081                 :             :    IN_LAYOUT_I has no meaning for other types of node.
    6082                 :             : 
    6083                 :             :    Keeping the node as-is is always valid.  If the target doesn't appear
    6084                 :             :    to support the node as-is, but might realistically support other layouts,
    6085                 :             :    then layout 0 instead has the cost of a worst-case permutation.  On the
    6086                 :             :    one hand, this ensures that every node has at least one valid layout,
    6087                 :             :    avoiding what would otherwise be an awkward special case.  On the other,
    6088                 :             :    it still encourages the pass to change an invalid pre-existing layout
    6089                 :             :    choice into a valid one.  */
    6090                 :             : 
    6091                 :             : int
    6092                 :      152884 : vect_optimize_slp_pass::internal_node_cost (slp_tree node, int in_layout_i,
    6093                 :             :                                             unsigned int out_layout_i)
    6094                 :             : {
    6095                 :      152884 :   const int fallback_cost = 1;
    6096                 :             : 
    6097                 :      152884 :   if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
    6098                 :             :     {
    6099                 :       19816 :       auto_lane_permutation_t tmp_perm;
    6100                 :       19816 :       tmp_perm.safe_splice (SLP_TREE_LANE_PERMUTATION (node));
    6101                 :             : 
    6102                 :             :       /* Check that the child nodes support the chosen layout.  Checking
    6103                 :             :          the first child is enough, since any second child would have the
    6104                 :             :          same shape.  */
    6105                 :       19816 :       auto first_child = SLP_TREE_CHILDREN (node)[0];
    6106                 :       19816 :       if (in_layout_i > 0
    6107                 :       19816 :           && !is_compatible_layout (first_child, in_layout_i))
    6108                 :             :         return -1;
    6109                 :             : 
    6110                 :       19157 :       change_vec_perm_layout (node, tmp_perm, in_layout_i, out_layout_i);
    6111                 :       38314 :       int count = vectorizable_slp_permutation_1 (m_vinfo, nullptr,
    6112                 :             :                                                   node, tmp_perm,
    6113                 :       19157 :                                                   SLP_TREE_CHILDREN (node),
    6114                 :             :                                                   false);
    6115                 :       19157 :       if (count < 0)
    6116                 :             :         {
    6117                 :        1600 :           if (in_layout_i == 0 && out_layout_i == 0)
    6118                 :             :             {
    6119                 :             :               /* Use the fallback cost if the node could in principle support
    6120                 :             :                  some nonzero layout for both the inputs and the outputs.
    6121                 :             :                  Otherwise assume that the node will be rejected later
    6122                 :             :                  and rebuilt from scalars.  */
    6123                 :         365 :               if (SLP_TREE_LANES (node) == SLP_TREE_LANES (first_child))
    6124                 :             :                 return fallback_cost;
    6125                 :         287 :               return 0;
    6126                 :             :             }
    6127                 :             :           return -1;
    6128                 :             :         }
    6129                 :             : 
    6130                 :             :       /* We currently have no way of telling whether the new layout is cheaper
    6131                 :             :          or more expensive than the old one.  But at least in principle,
    6132                 :             :          it should be worth making zero permutations (whole-vector shuffles)
    6133                 :             :          cheaper than real permutations, in case the pass is able to remove
    6134                 :             :          the latter.  */
    6135                 :       17557 :       return count == 0 ? 0 : 1;
    6136                 :       19816 :     }
    6137                 :             : 
    6138                 :      133068 :   stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
    6139                 :      133068 :   if (rep
    6140                 :      132005 :       && STMT_VINFO_DATA_REF (rep)
    6141                 :       51601 :       && DR_IS_READ (STMT_VINFO_DATA_REF (rep))
    6142                 :      168664 :       && SLP_TREE_LOAD_PERMUTATION (node).exists ())
    6143                 :             :     {
    6144                 :       31551 :       auto_load_permutation_t tmp_perm;
    6145                 :       31551 :       tmp_perm.safe_splice (SLP_TREE_LOAD_PERMUTATION (node));
    6146                 :       31551 :       if (out_layout_i > 0)
    6147                 :       14343 :         vect_slp_permute (m_perms[out_layout_i], tmp_perm, true);
    6148                 :             : 
    6149                 :       31551 :       poly_uint64 vf = 1;
    6150                 :       31551 :       if (auto loop_vinfo = dyn_cast<loop_vec_info> (m_vinfo))
    6151                 :        2197 :         vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
    6152                 :       31551 :       unsigned int n_perms;
    6153                 :       31551 :       if (!vect_transform_slp_perm_load_1 (m_vinfo, node, tmp_perm, vNULL,
    6154                 :             :                                            nullptr, vf, true, false, &n_perms))
    6155                 :             :         {
    6156                 :        1987 :           auto rep = SLP_TREE_REPRESENTATIVE (node);
    6157                 :        1987 :           if (out_layout_i == 0)
    6158                 :             :             {
    6159                 :             :               /* Use the fallback cost if the load is an N-to-N permutation.
    6160                 :             :                  Otherwise assume that the node will be rejected later
    6161                 :             :                  and rebuilt from scalars.  */
    6162                 :        1498 :               if (STMT_VINFO_GROUPED_ACCESS (rep)
    6163                 :        2996 :                   && (DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (rep))
    6164                 :        1498 :                       == SLP_TREE_LANES (node)))
    6165                 :         915 :                 return fallback_cost;
    6166                 :             :               return 0;
    6167                 :             :             }
    6168                 :             :           return -1;
    6169                 :             :         }
    6170                 :             : 
    6171                 :             :       /* See the comment above the corresponding VEC_PERM_EXPR handling.  */
    6172                 :       29564 :       return n_perms == 0 ? 0 : 1;
    6173                 :       31551 :     }
    6174                 :             : 
    6175                 :             :   return 0;
    6176                 :             : }
    6177                 :             : 
    6178                 :             : /* Decide which element layouts we should consider using.  Calculate the
    6179                 :             :    weights associated with inserting layout changes on partition edges.
    6180                 :             :    Also mark partitions that cannot change layout, by setting their
    6181                 :             :    layout to zero.  */
    6182                 :             : 
    6183                 :             : void
    6184                 :      553379 : vect_optimize_slp_pass::start_choosing_layouts ()
    6185                 :             : {
    6186                 :             :   /* Used to assign unique permutation indices.  */
    6187                 :      553379 :   using perm_hash = unbounded_hashmap_traits<
    6188                 :             :     vec_free_hash_base<int_hash_base<unsigned>>,
    6189                 :             :     int_hash<int, -1, -2>
    6190                 :             :   >;
    6191                 :      553379 :   hash_map<vec<unsigned>, int, perm_hash> layout_ids;
    6192                 :             : 
    6193                 :             :   /* Layout 0 is "no change".  */
    6194                 :      553379 :   m_perms.safe_push (vNULL);
    6195                 :             : 
    6196                 :             :   /* Create layouts from existing permutations.  */
    6197                 :      553379 :   auto_load_permutation_t tmp_perm;
    6198                 :     4379434 :   for (unsigned int node_i : m_partitioned_nodes)
    6199                 :             :     {
    6200                 :             :       /* Leafs also double as entries to the reverse graph.  Allow the
    6201                 :             :          layout of those to be changed.  */
    6202                 :     2719297 :       auto &vertex = m_vertices[node_i];
    6203                 :     2719297 :       auto &partition = m_partitions[vertex.partition];
    6204                 :     2719297 :       if (!m_slpg->vertices[node_i].succ)
    6205                 :      602021 :         partition.layout = 0;
    6206                 :             : 
    6207                 :             :       /* Loads and VEC_PERM_EXPRs are the only things generating permutes.  */
    6208                 :     2719297 :       slp_tree node = vertex.node;
    6209                 :     2719297 :       stmt_vec_info dr_stmt = SLP_TREE_REPRESENTATIVE (node);
    6210                 :     2719297 :       slp_tree child;
    6211                 :     2719297 :       unsigned HOST_WIDE_INT imin, imax = 0;
    6212                 :     2719297 :       bool any_permute = false;
    6213                 :     2719297 :       tmp_perm.truncate (0);
    6214                 :     2719297 :       if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
    6215                 :             :         {
    6216                 :             :           /* If splitting out a SLP_TREE_LANE_PERMUTATION can make the node
    6217                 :             :              unpermuted, record a layout that reverses this permutation.
    6218                 :             : 
    6219                 :             :              We would need more work to cope with loads that are internally
    6220                 :             :              permuted and also have inputs (such as masks for
    6221                 :             :              IFN_MASK_LOADs).  */
    6222                 :      302136 :           gcc_assert (partition.layout == 0 && !m_slpg->vertices[node_i].succ);
    6223                 :      302136 :           if (!STMT_VINFO_GROUPED_ACCESS (dr_stmt))
    6224                 :             :             {
    6225                 :      216351 :               partition.layout = -1;
    6226                 :     2706141 :               continue;
    6227                 :             :             }
    6228                 :       85785 :           dr_stmt = DR_GROUP_FIRST_ELEMENT (dr_stmt);
    6229                 :       85785 :           imin = DR_GROUP_SIZE (dr_stmt) + 1;
    6230                 :       85785 :           tmp_perm.safe_splice (SLP_TREE_LOAD_PERMUTATION (node));
    6231                 :             :         }
    6232                 :     4723647 :       else if (SLP_TREE_CODE (node) == VEC_PERM_EXPR
    6233                 :      125888 :                && SLP_TREE_CHILDREN (node).length () == 1
    6234                 :      110675 :                && (child = SLP_TREE_CHILDREN (node)[0])
    6235                 :     2527836 :                && (TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (child))
    6236                 :      110675 :                    .is_constant (&imin)))
    6237                 :             :         {
    6238                 :             :           /* If the child has the same vector size as this node,
    6239                 :             :              reversing the permutation can make the permutation a no-op.
    6240                 :             :              In other cases it can change a true permutation into a
    6241                 :             :              full-vector extract.  */
    6242                 :      110675 :           tmp_perm.reserve (SLP_TREE_LANES (node));
    6243                 :      319004 :           for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
    6244                 :      208329 :             tmp_perm.quick_push (SLP_TREE_LANE_PERMUTATION (node)[j].second);
    6245                 :             :         }
    6246                 :             :       else
    6247                 :     2306486 :         continue;
    6248                 :             : 
    6249                 :      581428 :       for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
    6250                 :             :         {
    6251                 :      384968 :           unsigned idx = tmp_perm[j];
    6252                 :      384968 :           imin = MIN (imin, idx);
    6253                 :      384968 :           imax = MAX (imax, idx);
    6254                 :      384968 :           if (idx - tmp_perm[0] != j)
    6255                 :      144815 :             any_permute = true;
    6256                 :             :         }
    6257                 :             :       /* If the span doesn't match we'd disrupt VF computation, avoid
    6258                 :             :          that for now.  */
    6259                 :      196460 :       if (imax - imin + 1 != SLP_TREE_LANES (node))
    6260                 :       72565 :         continue;
    6261                 :             :       /* If there's no permute no need to split one out.  In this case
    6262                 :             :          we can consider turning a load into a permuted load, if that
    6263                 :             :          turns out to be cheaper than alternatives.  */
    6264                 :      123895 :       if (!any_permute)
    6265                 :             :         {
    6266                 :      110556 :           partition.layout = -1;
    6267                 :      110556 :           continue;
    6268                 :             :         }
    6269                 :             : 
    6270                 :             :       /* For now only handle true permutes, like
    6271                 :             :          vect_attempt_slp_rearrange_stmts did.  This allows us to be lazy
    6272                 :             :          when permuting constants and invariants keeping the permute
    6273                 :             :          bijective.  */
    6274                 :       13339 :       auto_sbitmap load_index (SLP_TREE_LANES (node));
    6275                 :       13339 :       bitmap_clear (load_index);
    6276                 :       54547 :       for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
    6277                 :       41208 :         bitmap_set_bit (load_index, tmp_perm[j] - imin);
    6278                 :             :       unsigned j;
    6279                 :       53630 :       for (j = 0; j < SLP_TREE_LANES (node); ++j)
    6280                 :       40474 :         if (!bitmap_bit_p (load_index, j))
    6281                 :             :           break;
    6282                 :       13339 :       if (j != SLP_TREE_LANES (node))
    6283                 :         183 :         continue;
    6284                 :             : 
    6285                 :       13156 :       vec<unsigned> perm = vNULL;
    6286                 :       13156 :       perm.safe_grow (SLP_TREE_LANES (node), true);
    6287                 :       53320 :       for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
    6288                 :       40164 :         perm[j] = tmp_perm[j] - imin;
    6289                 :             : 
    6290                 :       26312 :       if (int (m_perms.length ()) >= param_vect_max_layout_candidates)
    6291                 :             :         {
    6292                 :             :           /* Continue to use existing layouts, but don't add any more.  */
    6293                 :           0 :           int *entry = layout_ids.get (perm);
    6294                 :           0 :           partition.layout = entry ? *entry : 0;
    6295                 :           0 :           perm.release ();
    6296                 :             :         }
    6297                 :             :       else
    6298                 :             :         {
    6299                 :       13156 :           bool existed;
    6300                 :       13156 :           int &layout_i = layout_ids.get_or_insert (perm, &existed);
    6301                 :       13156 :           if (existed)
    6302                 :        3455 :             perm.release ();
    6303                 :             :           else
    6304                 :             :             {
    6305                 :        9701 :               layout_i = m_perms.length ();
    6306                 :        9701 :               m_perms.safe_push (perm);
    6307                 :             :             }
    6308                 :       13156 :           partition.layout = layout_i;
    6309                 :             :         }
    6310                 :       13339 :     }
    6311                 :             : 
    6312                 :             :   /* Initially assume that every layout is possible and has zero cost
    6313                 :             :      in every partition.  */
    6314                 :      553379 :   m_partition_layout_costs.safe_grow_cleared (m_partitions.length ()
    6315                 :     1106758 :                                               * m_perms.length ());
    6316                 :             : 
    6317                 :             :   /* We have to mark outgoing permutations facing non-associating-reduction
    6318                 :             :      graph entries that are not represented as to be materialized.
    6319                 :             :      slp_inst_kind_bb_reduc currently only covers associatable reductions.  */
    6320                 :     2861597 :   for (slp_instance instance : m_vinfo->slp_instances)
    6321                 :     1201460 :     if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_ctor)
    6322                 :             :       {
    6323                 :        1385 :         unsigned int node_i = SLP_INSTANCE_TREE (instance)->vertex;
    6324                 :        1385 :         m_partitions[m_vertices[node_i].partition].layout = 0;
    6325                 :             :       }
    6326                 :     1200075 :     else if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_reduc_chain)
    6327                 :             :       {
    6328                 :         343 :         stmt_vec_info stmt_info
    6329                 :         343 :           = SLP_TREE_REPRESENTATIVE (SLP_INSTANCE_TREE (instance));
    6330                 :         343 :         stmt_vec_info reduc_info = info_for_reduction (m_vinfo, stmt_info);
    6331                 :         343 :         if (needs_fold_left_reduction_p (TREE_TYPE
    6332                 :             :                                            (gimple_get_lhs (stmt_info->stmt)),
    6333                 :             :                                          STMT_VINFO_REDUC_CODE (reduc_info)))
    6334                 :             :           {
    6335                 :          71 :             unsigned int node_i = SLP_INSTANCE_TREE (instance)->vertex;
    6336                 :          71 :             m_partitions[m_vertices[node_i].partition].layout = 0;
    6337                 :             :           }
    6338                 :             :       }
    6339                 :             : 
    6340                 :             :   /* Check which layouts each node and partition can handle.  Calculate the
    6341                 :             :      weights associated with inserting layout changes on edges.  */
    6342                 :     4379434 :   for (unsigned int node_i : m_partitioned_nodes)
    6343                 :             :     {
    6344                 :     2719297 :       auto &vertex = m_vertices[node_i];
    6345                 :     2719297 :       auto &partition = m_partitions[vertex.partition];
    6346                 :     2719297 :       slp_tree node = vertex.node;
    6347                 :             : 
    6348                 :     2719297 :       if (stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node))
    6349                 :             :         {
    6350                 :     2713958 :           vertex.weight = vect_slp_node_weight (node);
    6351                 :             : 
    6352                 :             :           /* We do not handle stores with a permutation, so all
    6353                 :             :              incoming permutations must have been materialized.
    6354                 :             : 
    6355                 :             :              We also don't handle masked grouped loads, which lack a
    6356                 :             :              permutation vector.  In this case the memory locations
    6357                 :             :              form an implicit second input to the loads, on top of the
    6358                 :             :              explicit mask input, and the memory input's layout cannot
    6359                 :             :              be changed.
    6360                 :             : 
    6361                 :             :              On the other hand, we do support permuting gather loads and
    6362                 :             :              masked gather loads, where each scalar load is independent
    6363                 :             :              of the others.  This can be useful if the address/index input
    6364                 :             :              benefits from permutation.  */
    6365                 :     2713958 :           if (STMT_VINFO_DATA_REF (rep)
    6366                 :     1394842 :               && STMT_VINFO_GROUPED_ACCESS (rep)
    6367                 :     3713705 :               && !SLP_TREE_LOAD_PERMUTATION (node).exists ())
    6368                 :      913962 :             partition.layout = 0;
    6369                 :             : 
    6370                 :             :           /* We cannot change the layout of an operation that is
    6371                 :             :              not independent on lanes.  Note this is an explicit
    6372                 :             :              negative list since that's much shorter than the respective
    6373                 :             :              positive one but it's critical to keep maintaining it.  */
    6374                 :     2713958 :           if (is_gimple_call (STMT_VINFO_STMT (rep)))
    6375                 :       22639 :             switch (gimple_call_combined_fn (STMT_VINFO_STMT (rep)))
    6376                 :             :               {
    6377                 :         438 :               case CFN_COMPLEX_ADD_ROT90:
    6378                 :         438 :               case CFN_COMPLEX_ADD_ROT270:
    6379                 :         438 :               case CFN_COMPLEX_MUL:
    6380                 :         438 :               case CFN_COMPLEX_MUL_CONJ:
    6381                 :         438 :               case CFN_VEC_ADDSUB:
    6382                 :         438 :               case CFN_VEC_FMADDSUB:
    6383                 :         438 :               case CFN_VEC_FMSUBADD:
    6384                 :         438 :                 partition.layout = 0;
    6385                 :             :               default:;
    6386                 :             :               }
    6387                 :             :         }
    6388                 :             : 
    6389                 :     6009567 :       auto process_edge = [&](graph_edge *ud, unsigned int other_node_i)
    6390                 :             :         {
    6391                 :     3290270 :           auto &other_vertex = m_vertices[other_node_i];
    6392                 :             : 
    6393                 :             :           /* Count the number of edges from earlier partitions and the number
    6394                 :             :              of edges to later partitions.  */
    6395                 :     3290270 :           if (other_vertex.partition < vertex.partition)
    6396                 :     1645135 :             partition.in_degree += 1;
    6397                 :             :           else
    6398                 :     1645135 :             partition.out_degree += 1;
    6399                 :             : 
    6400                 :             :           /* If the current node uses the result of OTHER_NODE_I, accumulate
    6401                 :             :              the effects of that.  */
    6402                 :     3290270 :           if (ud->src == int (node_i))
    6403                 :             :             {
    6404                 :     1645135 :               other_vertex.out_weight += vertex.weight;
    6405                 :     1645135 :               other_vertex.out_degree += 1;
    6406                 :             :             }
    6407                 :     6009567 :         };
    6408                 :     2719297 :       for_each_partition_edge (node_i, process_edge);
    6409                 :             :     }
    6410                 :      553379 : }
    6411                 :             : 
    6412                 :             : /* Return the incoming costs for node NODE_I, assuming that each input keeps
    6413                 :             :    its current (provisional) choice of layout.  The inputs do not necessarily
    6414                 :             :    have the same layout as each other.  */
    6415                 :             : 
    6416                 :             : slpg_layout_cost
    6417                 :        3022 : vect_optimize_slp_pass::total_in_cost (unsigned int node_i)
    6418                 :             : {
    6419                 :        3022 :   auto &vertex = m_vertices[node_i];
    6420                 :        3022 :   slpg_layout_cost cost;
    6421                 :       10816 :   auto add_cost = [&](graph_edge *, unsigned int other_node_i)
    6422                 :             :     {
    6423                 :        7794 :       auto &other_vertex = m_vertices[other_node_i];
    6424                 :        7794 :       if (other_vertex.partition < vertex.partition)
    6425                 :             :         {
    6426                 :        4944 :           auto &other_partition = m_partitions[other_vertex.partition];
    6427                 :        9888 :           auto &other_costs = partition_layout_costs (other_vertex.partition,
    6428                 :        4944 :                                                       other_partition.layout);
    6429                 :        4944 :           slpg_layout_cost this_cost = other_costs.in_cost;
    6430                 :        4944 :           this_cost.add_serial_cost (other_costs.internal_cost);
    6431                 :        4944 :           this_cost.split (other_partition.out_degree);
    6432                 :        4944 :           cost.add_parallel_cost (this_cost);
    6433                 :             :         }
    6434                 :       10816 :     };
    6435                 :        3022 :   for_each_partition_edge (node_i, add_cost);
    6436                 :        3022 :   return cost;
    6437                 :             : }
    6438                 :             : 
    6439                 :             : /* Return the cost of switching between layout LAYOUT1_I (at node NODE1_I)
    6440                 :             :    and layout LAYOUT2_I on cross-partition use-to-def edge UD.  Return
    6441                 :             :    slpg_layout_cost::impossible () if the change isn't possible.  */
    6442                 :             : 
    6443                 :             : slpg_layout_cost
    6444                 :      452949 : vect_optimize_slp_pass::
    6445                 :             : edge_layout_cost (graph_edge *ud, unsigned int node1_i, unsigned int layout1_i,
    6446                 :             :                   unsigned int layout2_i)
    6447                 :             : {
    6448                 :      452949 :   auto &def_vertex = m_vertices[ud->dest];
    6449                 :      452949 :   auto &use_vertex = m_vertices[ud->src];
    6450                 :      452949 :   auto def_layout_i = ud->dest == int (node1_i) ? layout1_i : layout2_i;
    6451                 :      452949 :   auto use_layout_i = ud->dest == int (node1_i) ? layout2_i : layout1_i;
    6452                 :      452949 :   auto factor = change_layout_cost (def_vertex.node, def_layout_i,
    6453                 :             :                                     use_layout_i);
    6454                 :      452949 :   if (factor < 0)
    6455                 :        4877 :     return slpg_layout_cost::impossible ();
    6456                 :             : 
    6457                 :             :   /* We have a choice of putting the layout change at the site of the
    6458                 :             :      definition or at the site of the use.  Prefer the former when
    6459                 :             :      optimizing for size or when the execution frequency of the
    6460                 :             :      definition is no greater than the combined execution frequencies of
    6461                 :             :      the uses.  When putting the layout change at the site of the definition,
    6462                 :             :      divvy up the cost among all consumers.  */
    6463                 :      448072 :   if (m_optimize_size || def_vertex.weight <= def_vertex.out_weight)
    6464                 :             :     {
    6465                 :      433614 :       slpg_layout_cost cost = { def_vertex.weight * factor, m_optimize_size };
    6466                 :      433614 :       cost.split (def_vertex.out_degree);
    6467                 :      433614 :       return cost;
    6468                 :             :     }
    6469                 :       14458 :   return { use_vertex.weight * factor, m_optimize_size };
    6470                 :             : }
    6471                 :             : 
    6472                 :             : /* UD represents a use-def link between FROM_NODE_I and a node in a later
    6473                 :             :    partition; FROM_NODE_I could be the definition node or the use node.
    6474                 :             :    The node at the other end of the link wants to use layout TO_LAYOUT_I.
    6475                 :             :    Return the cost of any necessary fix-ups on edge UD, or return
    6476                 :             :    slpg_layout_cost::impossible () if the change isn't possible.
    6477                 :             : 
    6478                 :             :    At this point, FROM_NODE_I's partition has chosen the cheapest
    6479                 :             :    layout based on the information available so far, but this choice
    6480                 :             :    is only provisional.  */
    6481                 :             : 
    6482                 :             : slpg_layout_cost
    6483                 :      117799 : vect_optimize_slp_pass::forward_cost (graph_edge *ud, unsigned int from_node_i,
    6484                 :             :                                       unsigned int to_layout_i)
    6485                 :             : {
    6486                 :      117799 :   auto &from_vertex = m_vertices[from_node_i];
    6487                 :      117799 :   unsigned int from_partition_i = from_vertex.partition;
    6488                 :      117799 :   slpg_partition_info &from_partition = m_partitions[from_partition_i];
    6489                 :      117799 :   gcc_assert (from_partition.layout >= 0);
    6490                 :             : 
    6491                 :             :   /* First calculate the cost on the assumption that FROM_PARTITION sticks
    6492                 :             :      with its current layout preference.  */
    6493                 :      117799 :   slpg_layout_cost cost = slpg_layout_cost::impossible ();
    6494                 :      117799 :   auto edge_cost = edge_layout_cost (ud, from_node_i,
    6495                 :      117799 :                                      from_partition.layout, to_layout_i);
    6496                 :      117799 :   if (edge_cost.is_possible ())
    6497                 :             :     {
    6498                 :      230274 :       auto &from_costs = partition_layout_costs (from_partition_i,
    6499                 :      115137 :                                                  from_partition.layout);
    6500                 :      115137 :       cost = from_costs.in_cost;
    6501                 :      115137 :       cost.add_serial_cost (from_costs.internal_cost);
    6502                 :      115137 :       cost.split (from_partition.out_degree);
    6503                 :      115137 :       cost.add_serial_cost (edge_cost);
    6504                 :             :     }
    6505                 :        2662 :   else if (from_partition.layout == 0)
    6506                 :             :     /* We must allow the source partition to have layout 0 as a fallback,
    6507                 :             :        in case all other options turn out to be impossible.  */
    6508                 :        2662 :     return cost;
    6509                 :             : 
    6510                 :             :   /* Take the minimum of that cost and the cost that applies if
    6511                 :             :      FROM_PARTITION instead switches to TO_LAYOUT_I.  */
    6512                 :      115137 :   auto &direct_layout_costs = partition_layout_costs (from_partition_i,
    6513                 :             :                                                       to_layout_i);
    6514                 :      115137 :   if (direct_layout_costs.is_possible ())
    6515                 :             :     {
    6516                 :      107379 :       slpg_layout_cost direct_cost = direct_layout_costs.in_cost;
    6517                 :      107379 :       direct_cost.add_serial_cost (direct_layout_costs.internal_cost);
    6518                 :      107379 :       direct_cost.split (from_partition.out_degree);
    6519                 :      107379 :       if (!cost.is_possible ()
    6520                 :      107379 :           || direct_cost.is_better_than (cost, m_optimize_size))
    6521                 :       33172 :         cost = direct_cost;
    6522                 :             :     }
    6523                 :             : 
    6524                 :      115137 :   return cost;
    6525                 :             : }
    6526                 :             : 
    6527                 :             : /* UD represents a use-def link between TO_NODE_I and a node in an earlier
    6528                 :             :    partition; TO_NODE_I could be the definition node or the use node.
    6529                 :             :    The node at the other end of the link wants to use layout FROM_LAYOUT_I;
    6530                 :             :    return the cost of any necessary fix-ups on edge UD, or
    6531                 :             :    slpg_layout_cost::impossible () if the choice cannot be made.
    6532                 :             : 
    6533                 :             :    At this point, TO_NODE_I's partition has a fixed choice of layout.  */
    6534                 :             : 
    6535                 :             : slpg_layout_cost
    6536                 :      112507 : vect_optimize_slp_pass::backward_cost (graph_edge *ud, unsigned int to_node_i,
    6537                 :             :                                        unsigned int from_layout_i)
    6538                 :             : {
    6539                 :      112507 :   auto &to_vertex = m_vertices[to_node_i];
    6540                 :      112507 :   unsigned int to_partition_i = to_vertex.partition;
    6541                 :      112507 :   slpg_partition_info &to_partition = m_partitions[to_partition_i];
    6542                 :      112507 :   gcc_assert (to_partition.layout >= 0);
    6543                 :             : 
    6544                 :             :   /* If TO_NODE_I is a VEC_PERM_EXPR consumer, see whether it can be
    6545                 :             :      adjusted for this input having layout FROM_LAYOUT_I.  Assume that
    6546                 :             :      any other inputs keep their current choice of layout.  */
    6547                 :      112507 :   auto &to_costs = partition_layout_costs (to_partition_i,
    6548                 :             :                                            to_partition.layout);
    6549                 :      112507 :   if (ud->src == int (to_node_i)
    6550                 :      112363 :       && SLP_TREE_CODE (to_vertex.node) == VEC_PERM_EXPR)
    6551                 :             :     {
    6552                 :        8633 :       auto &from_partition = m_partitions[m_vertices[ud->dest].partition];
    6553                 :        8633 :       auto old_layout = from_partition.layout;
    6554                 :        8633 :       from_partition.layout = from_layout_i;
    6555                 :       17266 :       int factor = internal_node_cost (to_vertex.node, -1,
    6556                 :        8633 :                                        to_partition.layout);
    6557                 :        8633 :       from_partition.layout = old_layout;
    6558                 :        8633 :       if (factor >= 0)
    6559                 :             :         {
    6560                 :        7941 :           slpg_layout_cost cost = to_costs.out_cost;
    6561                 :       15882 :           cost.add_serial_cost ({ to_vertex.weight * factor,
    6562                 :        7941 :                                   m_optimize_size });
    6563                 :        7941 :           cost.split (to_partition.in_degree);
    6564                 :        7941 :           return cost;
    6565                 :             :         }
    6566                 :             :     }
    6567                 :             : 
    6568                 :             :   /* Compute the cost if we insert any necessary layout change on edge UD.  */
    6569                 :      104566 :   auto edge_cost = edge_layout_cost (ud, to_node_i,
    6570                 :      104566 :                                      to_partition.layout, from_layout_i);
    6571                 :      104566 :   if (edge_cost.is_possible ())
    6572                 :             :     {
    6573                 :      104566 :       slpg_layout_cost cost = to_costs.out_cost;
    6574                 :      104566 :       cost.add_serial_cost (to_costs.internal_cost);
    6575                 :      104566 :       cost.split (to_partition.in_degree);
    6576                 :      104566 :       cost.add_serial_cost (edge_cost);
    6577                 :      104566 :       return cost;
    6578                 :             :     }
    6579                 :             : 
    6580                 :           0 :   return slpg_layout_cost::impossible ();
    6581                 :             : }
    6582                 :             : 
    6583                 :             : /* Make a forward pass through the partitions, accumulating input costs.
    6584                 :             :    Make a tentative (provisional) choice of layout for each partition,
    6585                 :             :    ensuring that this choice still allows later partitions to keep
    6586                 :             :    their original layout.  */
    6587                 :             : 
    6588                 :             : void
    6589                 :        9178 : vect_optimize_slp_pass::forward_pass ()
    6590                 :             : {
    6591                 :       90945 :   for (unsigned int partition_i = 0; partition_i < m_partitions.length ();
    6592                 :             :        ++partition_i)
    6593                 :             :     {
    6594                 :       81767 :       auto &partition = m_partitions[partition_i];
    6595                 :             : 
    6596                 :             :       /* If the partition consists of a single VEC_PERM_EXPR, precompute
    6597                 :             :          the incoming cost that would apply if every predecessor partition
    6598                 :             :          keeps its current layout.  This is used within the loop below.  */
    6599                 :       81767 :       slpg_layout_cost in_cost;
    6600                 :       81767 :       slp_tree single_node = nullptr;
    6601                 :       81767 :       if (partition.node_end == partition.node_begin + 1)
    6602                 :             :         {
    6603                 :       81288 :           unsigned int node_i = m_partitioned_nodes[partition.node_begin];
    6604                 :       81288 :           single_node = m_vertices[node_i].node;
    6605                 :       81288 :           if (SLP_TREE_CODE (single_node) == VEC_PERM_EXPR)
    6606                 :        3022 :             in_cost = total_in_cost (node_i);
    6607                 :             :         }
    6608                 :             : 
    6609                 :             :       /* Go through the possible layouts.  Decide which ones are valid
    6610                 :             :          for this partition and record which of the valid layouts has
    6611                 :             :          the lowest cost.  */
    6612                 :       81767 :       unsigned int min_layout_i = 0;
    6613                 :       81767 :       slpg_layout_cost min_layout_cost = slpg_layout_cost::impossible ();
    6614                 :      252923 :       for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
    6615                 :             :         {
    6616                 :      171156 :           auto &layout_costs = partition_layout_costs (partition_i, layout_i);
    6617                 :      171156 :           if (!layout_costs.is_possible ())
    6618                 :       37276 :             continue;
    6619                 :             : 
    6620                 :             :           /* If the recorded layout is already 0 then the layout cannot
    6621                 :             :              change.  */
    6622                 :      171156 :           if (partition.layout == 0 && layout_i != 0)
    6623                 :             :             {
    6624                 :       26952 :               layout_costs.mark_impossible ();
    6625                 :       26952 :               continue;
    6626                 :             :             }
    6627                 :             : 
    6628                 :      144204 :           bool is_possible = true;
    6629                 :      282052 :           for (unsigned int order_i = partition.node_begin;
    6630                 :      282052 :                order_i < partition.node_end; ++order_i)
    6631                 :             :             {
    6632                 :      146132 :               unsigned int node_i = m_partitioned_nodes[order_i];
    6633                 :      146132 :               auto &vertex = m_vertices[node_i];
    6634                 :             : 
    6635                 :             :               /* Reject the layout if it is individually incompatible
    6636                 :             :                  with any node in the partition.  */
    6637                 :      146132 :               if (!is_compatible_layout (vertex.node, layout_i))
    6638                 :             :                 {
    6639                 :        7058 :                   is_possible = false;
    6640                 :        8284 :                   break;
    6641                 :             :                 }
    6642                 :             : 
    6643                 :      372420 :               auto add_cost = [&](graph_edge *ud, unsigned int other_node_i)
    6644                 :             :                 {
    6645                 :      233346 :                   auto &other_vertex = m_vertices[other_node_i];
    6646                 :      233346 :                   if (other_vertex.partition < vertex.partition)
    6647                 :             :                     {
    6648                 :             :                       /* Accumulate the incoming costs from earlier
    6649                 :             :                          partitions, plus the cost of any layout changes
    6650                 :             :                          on UD itself.  */
    6651                 :      117799 :                       auto cost = forward_cost (ud, other_node_i, layout_i);
    6652                 :      117799 :                       if (!cost.is_possible ())
    6653                 :        2662 :                         is_possible = false;
    6654                 :             :                       else
    6655                 :      115137 :                         layout_costs.in_cost.add_parallel_cost (cost);
    6656                 :             :                     }
    6657                 :             :                   else
    6658                 :             :                     /* Reject the layout if it would make layout 0 impossible
    6659                 :             :                        for later partitions.  This amounts to testing that the
    6660                 :             :                        target supports reversing the layout change on edges
    6661                 :             :                        to later partitions.
    6662                 :             : 
    6663                 :             :                        In principle, it might be possible to push a layout
    6664                 :             :                        change all the way down a graph, so that it never
    6665                 :             :                        needs to be reversed and so that the target doesn't
    6666                 :             :                        need to support the reverse operation.  But it would
    6667                 :             :                        be awkward to bail out if we hit a partition that
    6668                 :             :                        does not support the new layout, especially since
    6669                 :             :                        we are not dealing with a lattice.  */
    6670                 :      115547 :                     is_possible &= edge_layout_cost (ud, other_node_i, 0,
    6671                 :      115547 :                                                      layout_i).is_possible ();
    6672                 :      372420 :                 };
    6673                 :      139074 :               for_each_partition_edge (node_i, add_cost);
    6674                 :             : 
    6675                 :             :               /* Accumulate the cost of using LAYOUT_I within NODE,
    6676                 :             :                  both for the inputs and the outputs.  */
    6677                 :      139074 :               int factor = internal_node_cost (vertex.node, layout_i,
    6678                 :             :                                                layout_i);
    6679                 :      139074 :               if (factor < 0)
    6680                 :             :                 {
    6681                 :        1226 :                   is_possible = false;
    6682                 :        1226 :                   break;
    6683                 :             :                 }
    6684                 :      137848 :               else if (factor)
    6685                 :       21137 :                 layout_costs.internal_cost.add_serial_cost
    6686                 :       21137 :                   ({ vertex.weight * factor, m_optimize_size });
    6687                 :             :             }
    6688                 :      144204 :           if (!is_possible)
    6689                 :             :             {
    6690                 :       10324 :               layout_costs.mark_impossible ();
    6691                 :       10324 :               continue;
    6692                 :             :             }
    6693                 :             : 
    6694                 :             :           /* Combine the incoming and partition-internal costs.  */
    6695                 :      133880 :           slpg_layout_cost combined_cost = layout_costs.in_cost;
    6696                 :      133880 :           combined_cost.add_serial_cost (layout_costs.internal_cost);
    6697                 :             : 
    6698                 :             :           /* If this partition consists of a single VEC_PERM_EXPR, see
    6699                 :             :              if the VEC_PERM_EXPR can be changed to support output layout
    6700                 :             :              LAYOUT_I while keeping all the provisional choices of input
    6701                 :             :              layout.  */
    6702                 :      133880 :           if (single_node
    6703                 :      132959 :               && SLP_TREE_CODE (single_node) == VEC_PERM_EXPR)
    6704                 :             :             {
    6705                 :        5177 :               int factor = internal_node_cost (single_node, -1, layout_i);
    6706                 :        5177 :               if (factor >= 0)
    6707                 :             :                 {
    6708                 :        4712 :                   auto weight = m_vertices[single_node->vertex].weight;
    6709                 :        4712 :                   slpg_layout_cost internal_cost
    6710                 :        4712 :                     = { weight * factor, m_optimize_size };
    6711                 :             : 
    6712                 :        4712 :                   slpg_layout_cost alt_cost = in_cost;
    6713                 :        4712 :                   alt_cost.add_serial_cost (internal_cost);
    6714                 :        4712 :                   if (alt_cost.is_better_than (combined_cost, m_optimize_size))
    6715                 :             :                     {
    6716                 :        1381 :                       combined_cost = alt_cost;
    6717                 :        1381 :                       layout_costs.in_cost = in_cost;
    6718                 :        1381 :                       layout_costs.internal_cost = internal_cost;
    6719                 :             :                     }
    6720                 :             :                 }
    6721                 :             :             }
    6722                 :             : 
    6723                 :             :           /* Record the layout with the lowest cost.  Prefer layout 0 in
    6724                 :             :              the event of a tie between it and another layout.  */
    6725                 :      133880 :           if (!min_layout_cost.is_possible ()
    6726                 :       52113 :               || combined_cost.is_better_than (min_layout_cost,
    6727                 :       52113 :                                                m_optimize_size))
    6728                 :             :             {
    6729                 :       95611 :               min_layout_i = layout_i;
    6730                 :       95611 :               min_layout_cost = combined_cost;
    6731                 :             :             }
    6732                 :             :         }
    6733                 :             : 
    6734                 :             :       /* This loop's handling of earlier partitions should ensure that
    6735                 :             :          choosing the original layout for the current partition is no
    6736                 :             :          less valid than it was in the original graph, even with the
    6737                 :             :          provisional layout choices for those earlier partitions.  */
    6738                 :       81767 :       gcc_assert (min_layout_cost.is_possible ());
    6739                 :       81767 :       partition.layout = min_layout_i;
    6740                 :             :     }
    6741                 :        9178 : }
    6742                 :             : 
    6743                 :             : /* Make a backward pass through the partitions, accumulating output costs.
    6744                 :             :    Make a final choice of layout for each partition.  */
    6745                 :             : 
    6746                 :             : void
    6747                 :        9178 : vect_optimize_slp_pass::backward_pass ()
    6748                 :             : {
    6749                 :      100123 :   for (unsigned int partition_i = m_partitions.length (); partition_i-- > 0;)
    6750                 :             :     {
    6751                 :       81767 :       auto &partition = m_partitions[partition_i];
    6752                 :             : 
    6753                 :       81767 :       unsigned int min_layout_i = 0;
    6754                 :       81767 :       slpg_layout_cost min_layout_cost = slpg_layout_cost::impossible ();
    6755                 :      252923 :       for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
    6756                 :             :         {
    6757                 :      171156 :           auto &layout_costs = partition_layout_costs (partition_i, layout_i);
    6758                 :      171156 :           if (!layout_costs.is_possible ())
    6759                 :       37276 :             continue;
    6760                 :             : 
    6761                 :             :           /* Accumulate the costs from successor partitions.  */
    6762                 :      133880 :           bool is_possible = true;
    6763                 :      269659 :           for (unsigned int order_i = partition.node_begin;
    6764                 :      269659 :                order_i < partition.node_end; ++order_i)
    6765                 :             :             {
    6766                 :      135779 :               unsigned int node_i = m_partitioned_nodes[order_i];
    6767                 :      135779 :               auto &vertex = m_vertices[node_i];
    6768                 :      363323 :               auto add_cost = [&](graph_edge *ud, unsigned int other_node_i)
    6769                 :             :                 {
    6770                 :      227544 :                   auto &other_vertex = m_vertices[other_node_i];
    6771                 :      227544 :                   auto &other_partition = m_partitions[other_vertex.partition];
    6772                 :      227544 :                   if (other_vertex.partition > vertex.partition)
    6773                 :             :                     {
    6774                 :             :                       /* Accumulate the incoming costs from later
    6775                 :             :                          partitions, plus the cost of any layout changes
    6776                 :             :                          on UD itself.  */
    6777                 :      112507 :                       auto cost = backward_cost (ud, other_node_i, layout_i);
    6778                 :      112507 :                       if (!cost.is_possible ())
    6779                 :           0 :                         is_possible = false;
    6780                 :             :                       else
    6781                 :      112507 :                         layout_costs.out_cost.add_parallel_cost (cost);
    6782                 :             :                     }
    6783                 :             :                   else
    6784                 :             :                     /* Make sure that earlier partitions can (if necessary
    6785                 :             :                        or beneficial) keep the layout that they chose in
    6786                 :             :                        the forward pass.  This ensures that there is at
    6787                 :             :                        least one valid choice of layout.  */
    6788                 :      115037 :                     is_possible &= edge_layout_cost (ud, other_node_i,
    6789                 :      115037 :                                                      other_partition.layout,
    6790                 :      115037 :                                                      layout_i).is_possible ();
    6791                 :      363323 :                 };
    6792                 :      135779 :               for_each_partition_edge (node_i, add_cost);
    6793                 :             :             }
    6794                 :      133880 :           if (!is_possible)
    6795                 :             :             {
    6796                 :           0 :               layout_costs.mark_impossible ();
    6797                 :           0 :               continue;
    6798                 :             :             }
    6799                 :             : 
    6800                 :             :           /* Locally combine the costs from the forward and backward passes.
    6801                 :             :              (This combined cost is not passed on, since that would lead
    6802                 :             :              to double counting.)  */
    6803                 :      133880 :           slpg_layout_cost combined_cost = layout_costs.in_cost;
    6804                 :      133880 :           combined_cost.add_serial_cost (layout_costs.internal_cost);
    6805                 :      133880 :           combined_cost.add_serial_cost (layout_costs.out_cost);
    6806                 :             : 
    6807                 :             :           /* Record the layout with the lowest cost.  Prefer layout 0 in
    6808                 :             :              the event of a tie between it and another layout.  */
    6809                 :      133880 :           if (!min_layout_cost.is_possible ()
    6810                 :       52113 :               || combined_cost.is_better_than (min_layout_cost,
    6811                 :       52113 :                                                m_optimize_size))
    6812                 :             :             {
    6813                 :       93184 :               min_layout_i = layout_i;
    6814                 :       93184 :               min_layout_cost = combined_cost;
    6815                 :             :             }
    6816                 :             :         }
    6817                 :             : 
    6818                 :       81767 :       gcc_assert (min_layout_cost.is_possible ());
    6819                 :       81767 :       partition.layout = min_layout_i;
    6820                 :             :     }
    6821                 :        9178 : }
    6822                 :             : 
    6823                 :             : /* Return a node that applies layout TO_LAYOUT_I to the original form of NODE.
    6824                 :             :    NODE already has the layout that was selected for its partition.  */
    6825                 :             : 
    6826                 :             : slp_tree
    6827                 :       92351 : vect_optimize_slp_pass::get_result_with_layout (slp_tree node,
    6828                 :             :                                                 unsigned int to_layout_i)
    6829                 :             : {
    6830                 :       92351 :   unsigned int result_i = node->vertex * m_perms.length () + to_layout_i;
    6831                 :       92351 :   slp_tree result = m_node_layouts[result_i];
    6832                 :       92351 :   if (result)
    6833                 :             :     return result;
    6834                 :             : 
    6835                 :       91976 :   if (SLP_TREE_DEF_TYPE (node) == vect_constant_def
    6836                 :       91976 :       || (SLP_TREE_DEF_TYPE (node) == vect_external_def
    6837                 :             :           /* We can't permute vector defs in place.  */
    6838                 :       15759 :           && SLP_TREE_VEC_DEFS (node).is_empty ()))
    6839                 :             :     {
    6840                 :             :       /* If the vector is uniform or unchanged, there's nothing to do.  */
    6841                 :       31372 :       if (to_layout_i == 0 || vect_slp_tree_uniform_p (node))
    6842                 :             :         result = node;
    6843                 :             :       else
    6844                 :             :         {
    6845                 :        1509 :           auto scalar_ops = SLP_TREE_SCALAR_OPS (node).copy ();
    6846                 :        1509 :           result = vect_create_new_slp_node (scalar_ops);
    6847                 :        1509 :           vect_slp_permute (m_perms[to_layout_i], scalar_ops, true);
    6848                 :             :         }
    6849                 :             :     }
    6850                 :             :   else
    6851                 :             :     {
    6852                 :       60604 :       unsigned int partition_i = m_vertices[node->vertex].partition;
    6853                 :       60604 :       unsigned int from_layout_i = m_partitions[partition_i].layout;
    6854                 :       60604 :       if (from_layout_i == to_layout_i)
    6855                 :       60318 :         return node;
    6856                 :             : 
    6857                 :             :       /* If NODE is itself a VEC_PERM_EXPR, try to create a parallel
    6858                 :             :          permutation instead of a serial one.  Leave the new permutation
    6859                 :             :          in TMP_PERM on success.  */
    6860                 :         286 :       auto_lane_permutation_t tmp_perm;
    6861                 :         286 :       unsigned int num_inputs = 1;
    6862                 :         286 :       if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
    6863                 :             :         {
    6864                 :           9 :           tmp_perm.safe_splice (SLP_TREE_LANE_PERMUTATION (node));
    6865                 :           9 :           if (from_layout_i != 0)
    6866                 :           9 :             vect_slp_permute (m_perms[from_layout_i], tmp_perm, false);
    6867                 :           9 :           if (to_layout_i != 0)
    6868                 :           4 :             vect_slp_permute (m_perms[to_layout_i], tmp_perm, true);
    6869                 :           9 :           if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
    6870                 :             :                                               tmp_perm,
    6871                 :           9 :                                               SLP_TREE_CHILDREN (node),
    6872                 :             :                                               false) >= 0)
    6873                 :           9 :             num_inputs = SLP_TREE_CHILDREN (node).length ();
    6874                 :             :           else
    6875                 :           0 :             tmp_perm.truncate (0);
    6876                 :             :         }
    6877                 :             : 
    6878                 :         286 :       if (dump_enabled_p ())
    6879                 :             :         {
    6880                 :          61 :           if (tmp_perm.length () > 0)
    6881                 :           6 :             dump_printf_loc (MSG_NOTE, vect_location,
    6882                 :             :                              "duplicating permutation node %p with"
    6883                 :             :                              " layout %d\n",
    6884                 :             :                              (void *) node, to_layout_i);
    6885                 :             :           else
    6886                 :          55 :             dump_printf_loc (MSG_NOTE, vect_location,
    6887                 :             :                              "inserting permutation node in place of %p\n",
    6888                 :             :                              (void *) node);
    6889                 :             :         }
    6890                 :             : 
    6891                 :         286 :       unsigned int num_lanes = SLP_TREE_LANES (node);
    6892                 :         286 :       result = vect_create_new_slp_node (num_inputs, VEC_PERM_EXPR);
    6893                 :         286 :       if (SLP_TREE_SCALAR_STMTS (node).length ())
    6894                 :             :         {
    6895                 :         285 :           auto &stmts = SLP_TREE_SCALAR_STMTS (result);
    6896                 :         285 :           stmts.safe_splice (SLP_TREE_SCALAR_STMTS (node));
    6897                 :         285 :           if (from_layout_i != 0)
    6898                 :         237 :             vect_slp_permute (m_perms[from_layout_i], stmts, false);
    6899                 :         285 :           if (to_layout_i != 0)
    6900                 :          52 :             vect_slp_permute (m_perms[to_layout_i], stmts, true);
    6901                 :             :         }
    6902                 :         286 :       SLP_TREE_REPRESENTATIVE (result) = SLP_TREE_REPRESENTATIVE (node);
    6903                 :         286 :       SLP_TREE_LANES (result) = num_lanes;
    6904                 :         286 :       SLP_TREE_VECTYPE (result) = SLP_TREE_VECTYPE (node);
    6905                 :         286 :       result->vertex = -1;
    6906                 :             : 
    6907                 :         286 :       auto &lane_perm = SLP_TREE_LANE_PERMUTATION (result);
    6908                 :         286 :       if (tmp_perm.length ())
    6909                 :             :         {
    6910                 :           9 :           lane_perm.safe_splice (tmp_perm);
    6911                 :           9 :           SLP_TREE_CHILDREN (result).safe_splice (SLP_TREE_CHILDREN (node));
    6912                 :             :         }
    6913                 :             :       else
    6914                 :             :         {
    6915                 :         277 :           lane_perm.create (num_lanes);
    6916                 :         889 :           for (unsigned j = 0; j < num_lanes; ++j)
    6917                 :         612 :             lane_perm.quick_push ({ 0, j });
    6918                 :         277 :           if (from_layout_i != 0)
    6919                 :         228 :             vect_slp_permute (m_perms[from_layout_i], lane_perm, false);
    6920                 :         277 :           if (to_layout_i != 0)
    6921                 :          49 :             vect_slp_permute (m_perms[to_layout_i], lane_perm, true);
    6922                 :         277 :           SLP_TREE_CHILDREN (result).safe_push (node);
    6923                 :             :         }
    6924                 :        1148 :       for (slp_tree child : SLP_TREE_CHILDREN (result))
    6925                 :         290 :         child->refcnt++;
    6926                 :         286 :     }
    6927                 :       31658 :   m_node_layouts[result_i] = result;
    6928                 :       31658 :   return result;
    6929                 :             : }
    6930                 :             : 
    6931                 :             : /* Apply the chosen vector layouts to the SLP graph.  */
    6932                 :             : 
    6933                 :             : void
    6934                 :        9178 : vect_optimize_slp_pass::materialize ()
    6935                 :             : {
    6936                 :             :   /* We no longer need the costs, so avoid having two O(N * P) arrays
    6937                 :             :      live at the same time.  */
    6938                 :        9178 :   m_partition_layout_costs.release ();
    6939                 :       27534 :   m_node_layouts.safe_grow_cleared (m_vertices.length () * m_perms.length ());
    6940                 :             : 
    6941                 :       18356 :   auto_sbitmap fully_folded (m_vertices.length ());
    6942                 :        9178 :   bitmap_clear (fully_folded);
    6943                 :      110287 :   for (unsigned int node_i : m_partitioned_nodes)
    6944                 :             :     {
    6945                 :       82753 :       auto &vertex = m_vertices[node_i];
    6946                 :       82753 :       slp_tree node = vertex.node;
    6947                 :       82753 :       int layout_i = m_partitions[vertex.partition].layout;
    6948                 :       82753 :       gcc_assert (layout_i >= 0);
    6949                 :             : 
    6950                 :             :       /* Rearrange the scalar statements to match the chosen layout.  */
    6951                 :       82753 :       if (layout_i > 0)
    6952                 :       11625 :         vect_slp_permute (m_perms[layout_i],
    6953                 :       11625 :                           SLP_TREE_SCALAR_STMTS (node), true);
    6954                 :             : 
    6955                 :             :       /* Update load and lane permutations.  */
    6956                 :       82753 :       if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
    6957                 :             :         {
    6958                 :             :           /* First try to absorb the input vector layouts.  If that fails,
    6959                 :             :              force the inputs to have layout LAYOUT_I too.  We checked that
    6960                 :             :              that was possible before deciding to use nonzero output layouts.
    6961                 :             :              (Note that at this stage we don't really have any guarantee that
    6962                 :             :              the target supports the original VEC_PERM_EXPR.)  */
    6963                 :        3066 :           auto &perm = SLP_TREE_LANE_PERMUTATION (node);
    6964                 :        3066 :           auto_lane_permutation_t tmp_perm;
    6965                 :        3066 :           tmp_perm.safe_splice (perm);
    6966                 :        3066 :           change_vec_perm_layout (node, tmp_perm, -1, layout_i);
    6967                 :        3066 :           if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
    6968                 :             :                                               tmp_perm,
    6969                 :        3066 :                                               SLP_TREE_CHILDREN (node),
    6970                 :             :                                               false) >= 0)
    6971                 :             :             {
    6972                 :        2701 :               if (dump_enabled_p ()
    6973                 :        3533 :                   && !std::equal (tmp_perm.begin (), tmp_perm.end (),
    6974                 :             :                                   perm.begin ()))
    6975                 :          54 :                 dump_printf_loc (MSG_NOTE, vect_location,
    6976                 :             :                                  "absorbing input layouts into %p\n",
    6977                 :             :                                  (void *) node);
    6978                 :        8103 :               std::copy (tmp_perm.begin (), tmp_perm.end (), perm.begin ());
    6979                 :        2701 :               bitmap_set_bit (fully_folded, node_i);
    6980                 :             :             }
    6981                 :             :           else
    6982                 :             :             {
    6983                 :             :               /* Not MSG_MISSED because it would make no sense to users.  */
    6984                 :         365 :               if (dump_enabled_p ())
    6985                 :          46 :                 dump_printf_loc (MSG_NOTE, vect_location,
    6986                 :             :                                  "failed to absorb input layouts into %p\n",
    6987                 :             :                                  (void *) node);
    6988                 :         365 :               change_vec_perm_layout (nullptr, perm, layout_i, layout_i);
    6989                 :             :             }
    6990                 :        3066 :         }
    6991                 :             :       else
    6992                 :             :         {
    6993                 :       79687 :           gcc_assert (!SLP_TREE_LANE_PERMUTATION (node).exists ());
    6994                 :       79687 :           auto &load_perm = SLP_TREE_LOAD_PERMUTATION (node);
    6995                 :       79687 :           if (layout_i > 0)
    6996                 :             :             /* ???  When we handle non-bijective permutes the idea
    6997                 :             :                is that we can force the load-permutation to be
    6998                 :             :                { min, min + 1, min + 2, ... max }.  But then the
    6999                 :             :                scalar defs might no longer match the lane content
    7000                 :             :                which means wrong-code with live lane vectorization.
    7001                 :             :                So we possibly have to have NULL entries for those.  */
    7002                 :       11532 :             vect_slp_permute (m_perms[layout_i], load_perm, true);
    7003                 :             :         }
    7004                 :             :     }
    7005                 :             : 
    7006                 :             :   /* Do this before any nodes disappear, since it involves a walk
    7007                 :             :      over the leaves.  */
    7008                 :        9178 :   remove_redundant_permutations ();
    7009                 :             : 
    7010                 :             :   /* Replace each child with a correctly laid-out version.  */
    7011                 :      110287 :   for (unsigned int node_i : m_partitioned_nodes)
    7012                 :             :     {
    7013                 :             :       /* Skip nodes that have already been handled above.  */
    7014                 :       82753 :       if (bitmap_bit_p (fully_folded, node_i))
    7015                 :        2701 :         continue;
    7016                 :             : 
    7017                 :       80052 :       auto &vertex = m_vertices[node_i];
    7018                 :       80052 :       int in_layout_i = m_partitions[vertex.partition].layout;
    7019                 :       80052 :       gcc_assert (in_layout_i >= 0);
    7020                 :             : 
    7021                 :             :       unsigned j;
    7022                 :             :       slp_tree child;
    7023                 :      233034 :       FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (vertex.node), j, child)
    7024                 :             :         {
    7025                 :       92523 :           if (!child)
    7026                 :         172 :             continue;
    7027                 :             : 
    7028                 :       92351 :           slp_tree new_child = get_result_with_layout (child, in_layout_i);
    7029                 :       92351 :           if (new_child != child)
    7030                 :             :             {
    7031                 :        1893 :               vect_free_slp_tree (child);
    7032                 :        1893 :               SLP_TREE_CHILDREN (vertex.node)[j] = new_child;
    7033                 :        1893 :               new_child->refcnt += 1;
    7034                 :             :             }
    7035                 :             :         }
    7036                 :             :     }
    7037                 :        9178 : }
    7038                 :             : 
    7039                 :             : /* Elide load permutations that are not necessary.  Such permutations might
    7040                 :             :    be pre-existing, rather than created by the layout optimizations.  */
    7041                 :             : 
    7042                 :             : void
    7043                 :      553379 : vect_optimize_slp_pass::remove_redundant_permutations ()
    7044                 :             : {
    7045                 :     3601208 :   for (unsigned int node_i : m_leafs)
    7046                 :             :     {
    7047                 :     1941071 :       slp_tree node = m_vertices[node_i].node;
    7048                 :     1941071 :       if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
    7049                 :     1638935 :         continue;
    7050                 :             : 
    7051                 :             :       /* In basic block vectorization we allow any subchain of an interleaving
    7052                 :             :          chain.
    7053                 :             :          FORNOW: not in loop SLP because of realignment complications.  */
    7054                 :      302136 :       if (is_a <bb_vec_info> (m_vinfo))
    7055                 :             :         {
    7056                 :      142483 :           bool subchain_p = true;
    7057                 :             :           stmt_vec_info next_load_info = NULL;
    7058                 :             :           stmt_vec_info load_info;
    7059                 :             :           unsigned j;
    7060                 :      142483 :           FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
    7061                 :             :             {
    7062                 :      113559 :               if (j != 0
    7063                 :      113559 :                   && (next_load_info != load_info
    7064                 :       52151 :                       || ! load_info
    7065                 :       52151 :                       || DR_GROUP_GAP (load_info) != 1))
    7066                 :             :                 {
    7067                 :             :                   subchain_p = false;
    7068                 :             :                   break;
    7069                 :             :                 }
    7070                 :       96136 :               next_load_info = DR_GROUP_NEXT_ELEMENT (load_info);
    7071                 :             :             }
    7072                 :       46347 :           if (subchain_p)
    7073                 :             :             {
    7074                 :       28924 :               SLP_TREE_LOAD_PERMUTATION (node).release ();
    7075                 :       28924 :               continue;
    7076                 :             :             }
    7077                 :             :         }
    7078                 :             :       else
    7079                 :             :         {
    7080                 :      255789 :           loop_vec_info loop_vinfo = as_a<loop_vec_info> (m_vinfo);
    7081                 :      255789 :           stmt_vec_info load_info;
    7082                 :      255789 :           bool this_load_permuted = false;
    7083                 :      255789 :           unsigned j;
    7084                 :      757890 :           FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
    7085                 :      259628 :             if (SLP_TREE_LOAD_PERMUTATION (node)[j] != j)
    7086                 :             :               {
    7087                 :             :                 this_load_permuted = true;
    7088                 :             :                 break;
    7089                 :             :               }
    7090                 :             :           /* When this isn't a grouped access we know it's single element
    7091                 :             :              and contiguous.  */
    7092                 :      255789 :           if (!STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (node)[0]))
    7093                 :             :             {
    7094                 :      216351 :               if (!this_load_permuted
    7095                 :      216351 :                   && (known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U)
    7096                 :      215816 :                       || SLP_TREE_LANES (node) == 1))
    7097                 :      215816 :                 SLP_TREE_LOAD_PERMUTATION (node).release ();
    7098                 :      216351 :               continue;
    7099                 :             :             }
    7100                 :       39438 :           stmt_vec_info first_stmt_info
    7101                 :       39438 :             = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (node)[0]);
    7102                 :       39776 :           if (!this_load_permuted
    7103                 :             :               /* The load requires permutation when unrolling exposes
    7104                 :             :                  a gap either because the group is larger than the SLP
    7105                 :             :                  group-size or because there is a gap between the groups.  */
    7106                 :       39438 :               && (known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U)
    7107                 :       26421 :                   || ((SLP_TREE_LANES (node) == DR_GROUP_SIZE (first_stmt_info))
    7108                 :         102 :                       && DR_GROUP_GAP (first_stmt_info) == 0)))
    7109                 :             :             {
    7110                 :         338 :               SLP_TREE_LOAD_PERMUTATION (node).release ();
    7111                 :         338 :               continue;
    7112                 :             :             }
    7113                 :             :         }
    7114                 :             :     }
    7115                 :      553379 : }
    7116                 :             : 
    7117                 :             : /* Print the partition graph and layout information to the dump file.  */
    7118                 :             : 
    7119                 :             : void
    7120                 :         607 : vect_optimize_slp_pass::dump ()
    7121                 :             : {
    7122                 :         607 :   dump_printf_loc (MSG_NOTE, vect_location,
    7123                 :             :                    "SLP optimize permutations:\n");
    7124                 :        1230 :   for (unsigned int layout_i = 1; layout_i < m_perms.length (); ++layout_i)
    7125                 :             :     {
    7126                 :         623 :       dump_printf_loc (MSG_NOTE, vect_location, "  %d: { ", layout_i);
    7127                 :         623 :       const char *sep = "";
    7128                 :        5329 :       for (unsigned int idx : m_perms[layout_i])
    7129                 :             :         {
    7130                 :        3460 :           dump_printf (MSG_NOTE, "%s%d", sep, idx);
    7131                 :        3460 :           sep = ", ";
    7132                 :             :         }
    7133                 :         623 :       dump_printf (MSG_NOTE, " }\n");
    7134                 :             :     }
    7135                 :         607 :   dump_printf_loc (MSG_NOTE, vect_location,
    7136                 :             :                    "SLP optimize partitions:\n");
    7137                 :        5176 :   for (unsigned int partition_i = 0; partition_i < m_partitions.length ();
    7138                 :             :        ++partition_i)
    7139                 :             :     {
    7140                 :        4569 :       auto &partition = m_partitions[partition_i];
    7141                 :        4569 :       dump_printf_loc (MSG_NOTE, vect_location,  "  -------------\n");
    7142                 :        4569 :       dump_printf_loc (MSG_NOTE, vect_location,
    7143                 :             :                        "  partition %d (layout %d):\n",
    7144                 :             :                        partition_i, partition.layout);
    7145                 :        4569 :       dump_printf_loc (MSG_NOTE, vect_location, "    nodes:\n");
    7146                 :        9338 :       for (unsigned int order_i = partition.node_begin;
    7147                 :        9338 :            order_i < partition.node_end; ++order_i)
    7148                 :             :         {
    7149                 :        4769 :           auto &vertex = m_vertices[m_partitioned_nodes[order_i]];
    7150                 :        9538 :           dump_printf_loc (MSG_NOTE, vect_location, "      - %p:\n",
    7151                 :        4769 :                            (void *) vertex.node);
    7152                 :        4769 :           dump_printf_loc (MSG_NOTE, vect_location,
    7153                 :             :                            "          weight: %f\n",
    7154                 :             :                            vertex.weight.to_double ());
    7155                 :        4769 :           if (vertex.out_degree)
    7156                 :        3754 :             dump_printf_loc (MSG_NOTE, vect_location,
    7157                 :             :                              "          out weight: %f (degree %d)\n",
    7158                 :             :                              vertex.out_weight.to_double (),
    7159                 :             :                              vertex.out_degree);
    7160                 :        4769 :           if (SLP_TREE_CODE (vertex.node) == VEC_PERM_EXPR)
    7161                 :         462 :             dump_printf_loc (MSG_NOTE, vect_location,
    7162                 :             :                              "          op: VEC_PERM_EXPR\n");
    7163                 :        4307 :           else if (auto rep = SLP_TREE_REPRESENTATIVE (vertex.node))
    7164                 :        4289 :             dump_printf_loc (MSG_NOTE, vect_location,
    7165                 :             :                              "          op template: %G", rep->stmt);
    7166                 :             :         }
    7167                 :        4569 :       dump_printf_loc (MSG_NOTE, vect_location, "    edges:\n");
    7168                 :        9338 :       for (unsigned int order_i = partition.node_begin;
    7169                 :        9338 :            order_i < partition.node_end; ++order_i)
    7170                 :             :         {
    7171                 :        4769 :           unsigned int node_i = m_partitioned_nodes[order_i];
    7172                 :        4769 :           auto &vertex = m_vertices[node_i];
    7173                 :       14413 :           auto print_edge = [&](graph_edge *, unsigned int other_node_i)
    7174                 :             :             {
    7175                 :        9644 :               auto &other_vertex = m_vertices[other_node_i];
    7176                 :        9644 :               if (other_vertex.partition < vertex.partition)
    7177                 :        4822 :                 dump_printf_loc (MSG_NOTE, vect_location,
    7178                 :             :                                  "      - %p [%d] --> %p\n",
    7179                 :        4822 :                                  (void *) other_vertex.node,
    7180                 :             :                                  other_vertex.partition,
    7181                 :        4822 :                                  (void *) vertex.node);
    7182                 :             :               else
    7183                 :        4822 :                 dump_printf_loc (MSG_NOTE, vect_location,
    7184                 :             :                                  "      - %p --> [%d] %p\n",
    7185                 :        4822 :                                  (void *) vertex.node,
    7186                 :             :                                  other_vertex.partition,
    7187                 :        4822 :                                  (void *) other_vertex.node);
    7188                 :       14413 :             };
    7189                 :        4769 :           for_each_partition_edge (node_i, print_edge);
    7190                 :             :         }
    7191                 :             : 
    7192                 :       13960 :       for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
    7193                 :             :         {
    7194                 :        9391 :           auto &layout_costs = partition_layout_costs (partition_i, layout_i);
    7195                 :        9391 :           if (layout_costs.is_possible ())
    7196                 :             :             {
    7197                 :        7570 :               dump_printf_loc (MSG_NOTE, vect_location,
    7198                 :             :                                "    layout %d:%s\n", layout_i,
    7199                 :        7570 :                                partition.layout == int (layout_i)
    7200                 :             :                                ? " (*)" : "");
    7201                 :        7570 :               slpg_layout_cost combined_cost = layout_costs.in_cost;
    7202                 :        7570 :               combined_cost.add_serial_cost (layout_costs.internal_cost);
    7203                 :        7570 :               combined_cost.add_serial_cost (layout_costs.out_cost);
    7204                 :             : #define TEMPLATE "{depth: %f, total: %f}"
    7205                 :        7570 :               dump_printf_loc (MSG_NOTE, vect_location,
    7206                 :             :                                "        " TEMPLATE "\n",
    7207                 :             :                                layout_costs.in_cost.depth.to_double (),
    7208                 :             :                                layout_costs.in_cost.total.to_double ());
    7209                 :        7570 :               dump_printf_loc (MSG_NOTE, vect_location,
    7210                 :             :                                "      + " TEMPLATE "\n",
    7211                 :             :                                layout_costs.internal_cost.depth.to_double (),
    7212                 :             :                                layout_costs.internal_cost.total.to_double ());
    7213                 :        7570 :               dump_printf_loc (MSG_NOTE, vect_location,
    7214                 :             :                                "      + " TEMPLATE "\n",
    7215                 :             :                                layout_costs.out_cost.depth.to_double (),
    7216                 :             :                                layout_costs.out_cost.total.to_double ());
    7217                 :        7570 :               dump_printf_loc (MSG_NOTE, vect_location,
    7218                 :             :                                "      = " TEMPLATE "\n",
    7219                 :             :                                combined_cost.depth.to_double (),
    7220                 :             :                                combined_cost.total.to_double ());
    7221                 :             : #undef TEMPLATE
    7222                 :             :             }
    7223                 :             :           else
    7224                 :        1821 :             dump_printf_loc (MSG_NOTE, vect_location,
    7225                 :             :                              "    layout %d: rejected\n", layout_i);
    7226                 :             :         }
    7227                 :             :     }
    7228                 :         607 : }
    7229                 :             : 
    7230                 :             : /* Masked load lanes discovery.  */
    7231                 :             : 
    7232                 :             : void
    7233                 :      553379 : vect_optimize_slp_pass::decide_masked_load_lanes ()
    7234                 :             : {
    7235                 :     5651586 :   for (auto v : m_vertices)
    7236                 :             :     {
    7237                 :     3991449 :       slp_tree node = v.node;
    7238                 :     3991449 :       if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
    7239                 :     2717243 :           || SLP_TREE_CODE (node) == VEC_PERM_EXPR)
    7240                 :     1400375 :         continue;
    7241                 :     2591074 :       stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
    7242                 :     1283562 :       if (! STMT_VINFO_GROUPED_ACCESS (stmt_info)
    7243                 :             :           /* The mask has to be uniform.  */
    7244                 :      888979 :           || STMT_VINFO_SLP_VECT_ONLY (stmt_info)
    7245                 :      888833 :           || ! is_a <gcall *> (STMT_VINFO_STMT (stmt_info))
    7246                 :     2591125 :           || ! gimple_call_internal_p (STMT_VINFO_STMT (stmt_info),
    7247                 :             :                                        IFN_MASK_LOAD))
    7248                 :     2591071 :         continue;
    7249                 :           3 :       stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
    7250                 :           6 :       if (STMT_VINFO_STRIDED_P (stmt_info)
    7251                 :           3 :           || compare_step_with_zero (m_vinfo, stmt_info) <= 0
    7252                 :           3 :           || vect_load_lanes_supported (SLP_TREE_VECTYPE (node),
    7253                 :           0 :                                         DR_GROUP_SIZE (stmt_info),
    7254                 :             :                                         true) == IFN_LAST)
    7255                 :           3 :         continue;
    7256                 :             : 
    7257                 :             :       /* Uniform masks need to be suitably represented.  */
    7258                 :           0 :       slp_tree mask = SLP_TREE_CHILDREN (node)[0];
    7259                 :           0 :       if (SLP_TREE_CODE (mask) != VEC_PERM_EXPR
    7260                 :           0 :           || SLP_TREE_CHILDREN (mask).length () != 1)
    7261                 :           0 :         continue;
    7262                 :           0 :       bool match = true;
    7263                 :           0 :       for (auto perm : SLP_TREE_LANE_PERMUTATION (mask))
    7264                 :           0 :         if (perm.first != 0 || perm.second != 0)
    7265                 :             :           {
    7266                 :             :             match = false;
    7267                 :             :             break;
    7268                 :             :           }
    7269                 :           0 :       if (!match)
    7270                 :           0 :         continue;
    7271                 :             : 
    7272                 :             :       /* Now see if the consumer side matches.  */
    7273                 :           0 :       for (graph_edge *pred = m_slpg->vertices[node->vertex].pred;
    7274                 :           0 :            pred; pred = pred->pred_next)
    7275                 :             :         {
    7276                 :           0 :           slp_tree pred_node = m_vertices[pred->src].node;
    7277                 :             :           /* All consumers should be a permute with a single outgoing lane.  */
    7278                 :           0 :           if (SLP_TREE_CODE (pred_node) != VEC_PERM_EXPR
    7279                 :           0 :               || SLP_TREE_LANES (pred_node) != 1)
    7280                 :             :             {
    7281                 :             :               match = false;
    7282                 :             :               break;
    7283                 :             :             }
    7284                 :           0 :           gcc_assert (SLP_TREE_CHILDREN (pred_node).length () == 1);
    7285                 :             :         }
    7286                 :           0 :       if (!match)
    7287                 :           0 :         continue;
    7288                 :             :       /* Now we can mark the nodes as to use load lanes.  */
    7289                 :           0 :       node->ldst_lanes = true;
    7290                 :           0 :       for (graph_edge *pred = m_slpg->vertices[node->vertex].pred;
    7291                 :           0 :            pred; pred = pred->pred_next)
    7292                 :           0 :         m_vertices[pred->src].node->ldst_lanes = true;
    7293                 :             :       /* The catch is we have to massage the mask.  We have arranged
    7294                 :             :          analyzed uniform masks to be represented by a splat VEC_PERM
    7295                 :             :          which we can now simply elide as we cannot easily re-do SLP
    7296                 :             :          discovery here.  */
    7297                 :           0 :       slp_tree new_mask = SLP_TREE_CHILDREN (mask)[0];
    7298                 :           0 :       SLP_TREE_REF_COUNT (new_mask)++;
    7299                 :           0 :       SLP_TREE_CHILDREN (node)[0] = new_mask;
    7300                 :           0 :       vect_free_slp_tree (mask);
    7301                 :             :     }
    7302                 :      553379 : }
    7303                 :             : 
    7304                 :             : /* Main entry point for the SLP graph optimization pass.  */
    7305                 :             : 
    7306                 :             : void
    7307                 :      553379 : vect_optimize_slp_pass::run ()
    7308                 :             : {
    7309                 :      553379 :   build_graph ();
    7310                 :      553379 :   create_partitions ();
    7311                 :      553379 :   start_choosing_layouts ();
    7312                 :      553379 :   if (m_perms.length () > 1)
    7313                 :             :     {
    7314                 :        9178 :       forward_pass ();
    7315                 :        9178 :       backward_pass ();
    7316                 :        9178 :       if (dump_enabled_p ())
    7317                 :         607 :         dump ();
    7318                 :        9178 :       materialize ();
    7319                 :       37235 :       while (!m_perms.is_empty ())
    7320                 :       18879 :         m_perms.pop ().release ();
    7321                 :             :     }
    7322                 :             :   else
    7323                 :      544201 :     remove_redundant_permutations ();
    7324                 :      553379 :   free_graph (m_slpg);
    7325                 :      553379 :   build_graph ();
    7326                 :      553379 :   decide_masked_load_lanes ();
    7327                 :      553379 :   free_graph (m_slpg);
    7328                 :      553379 : }
    7329                 :             : 
    7330                 :             : /* Apply CSE to NODE and its children using BST_MAP.  */
    7331                 :             : 
    7332                 :             : static void
    7333                 :     4285154 : vect_cse_slp_nodes (scalar_stmts_to_slp_tree_map_t *bst_map, slp_tree& node)
    7334                 :             : {
    7335                 :     4285154 :   bool put_p = false;
    7336                 :     4285154 :   if (SLP_TREE_DEF_TYPE (node) == vect_internal_def
    7337                 :             :       /* Besides some VEC_PERM_EXPR, two-operator nodes also
    7338                 :             :          lack scalar stmts and thus CSE doesn't work via bst_map.  Ideally
    7339                 :             :          we'd have sth that works for all internal and external nodes.  */
    7340                 :     4285154 :       && !SLP_TREE_SCALAR_STMTS (node).is_empty ())
    7341                 :             :     {
    7342                 :     2990775 :       slp_tree *leader = bst_map->get (SLP_TREE_SCALAR_STMTS (node));
    7343                 :     2990775 :       if (leader)
    7344                 :             :         {
    7345                 :             :           /* We've visited this node already.  */
    7346                 :      295537 :           if (!*leader || *leader == node)
    7347                 :             :             return;
    7348                 :             : 
    7349                 :        2111 :           if (dump_enabled_p ())
    7350                 :         940 :             dump_printf_loc (MSG_NOTE, vect_location,
    7351                 :             :                              "re-using SLP tree %p for %p\n",
    7352                 :             :                              (void *)*leader, (void *)node);
    7353                 :        2111 :           vect_free_slp_tree (node);
    7354                 :        2111 :           (*leader)->refcnt += 1;
    7355                 :        2111 :           node = *leader;
    7356                 :        2111 :           return;
    7357                 :             :         }
    7358                 :             : 
    7359                 :             :       /* Avoid creating a cycle by populating the map only after recursion.  */
    7360                 :     2695238 :       bst_map->put (SLP_TREE_SCALAR_STMTS (node).copy (), nullptr);
    7361                 :     2695238 :       node->refcnt += 1;
    7362                 :     2695238 :       put_p = true;
    7363                 :             :       /* And recurse.  */
    7364                 :             :     }
    7365                 :             : 
    7366                 :    12006186 :   for (slp_tree &child : SLP_TREE_CHILDREN (node))
    7367                 :     3469127 :     if (child)
    7368                 :     3083694 :       vect_cse_slp_nodes (bst_map, child);
    7369                 :             : 
    7370                 :             :   /* Now record the node for CSE in other siblings.  */
    7371                 :     3989617 :   if (put_p)
    7372                 :     2695238 :     *bst_map->get (SLP_TREE_SCALAR_STMTS (node)) = node;
    7373                 :             : }
    7374                 :             : 
    7375                 :             : /* Optimize the SLP graph of VINFO.  */
    7376                 :             : 
    7377                 :             : void
    7378                 :      832804 : vect_optimize_slp (vec_info *vinfo)
    7379                 :             : {
    7380                 :      832804 :   if (vinfo->slp_instances.is_empty ())
    7381                 :             :     return;
    7382                 :      553379 :   vect_optimize_slp_pass (vinfo).run ();
    7383                 :             : 
    7384                 :             :   /* Apply CSE again to nodes after permute optimization.  */
    7385                 :      553379 :   scalar_stmts_to_slp_tree_map_t *bst_map
    7386                 :      553379 :     = new scalar_stmts_to_slp_tree_map_t ();
    7387                 :             : 
    7388                 :     2861597 :   for (auto inst : vinfo->slp_instances)
    7389                 :     1201460 :     vect_cse_slp_nodes (bst_map, SLP_INSTANCE_TREE (inst));
    7390                 :             : 
    7391                 :      553379 :   release_scalar_stmts_to_slp_tree_map (bst_map);
    7392                 :             : }
    7393                 :             : 
    7394                 :             : /* Gather loads reachable from the individual SLP graph entries.  */
    7395                 :             : 
    7396                 :             : void
    7397                 :      832804 : vect_gather_slp_loads (vec_info *vinfo)
    7398                 :             : {
    7399                 :      832804 :   unsigned i;
    7400                 :      832804 :   slp_instance instance;
    7401                 :     2034264 :   FOR_EACH_VEC_ELT (vinfo->slp_instances, i, instance)
    7402                 :             :     {
    7403                 :     1201460 :       hash_set<slp_tree> visited;
    7404                 :     1201460 :       vect_gather_slp_loads (SLP_INSTANCE_LOADS (instance),
    7405                 :             :                              SLP_INSTANCE_TREE (instance), visited);
    7406                 :     1201460 :     }
    7407                 :      832804 : }
    7408                 :             : 
    7409                 :             : /* For NODE update VF based on the number of lanes and the vector types
    7410                 :             :    used.  */
    7411                 :             : 
    7412                 :             : static void
    7413                 :     2736350 : vect_update_slp_vf_for_node (slp_tree node, poly_uint64 &vf,
    7414                 :             :                              hash_set<slp_tree> &visited)
    7415                 :             : {
    7416                 :     2736350 :   if (!node || SLP_TREE_DEF_TYPE (node) != vect_internal_def)
    7417                 :     1095878 :     return;
    7418                 :     1885458 :   if (visited.add (node))
    7419                 :             :     return;
    7420                 :             : 
    7421                 :     6635801 :   for (slp_tree child : SLP_TREE_CHILDREN (node))
    7422                 :     2297253 :     vect_update_slp_vf_for_node (child, vf, visited);
    7423                 :             : 
    7424                 :             :   /* We do not visit SLP nodes for constants or externals - those neither
    7425                 :             :      have a vector type set yet (vectorizable_* does this) nor do they
    7426                 :             :      have max_nunits set.  Instead we rely on internal nodes max_nunit
    7427                 :             :      to cover constant/external operands.
    7428                 :             :      Note that when we stop using fixed size vectors externs and constants
    7429                 :             :      shouldn't influence the (minimum) vectorization factor, instead
    7430                 :             :      vectorizable_* should honor the vectorization factor when trying to
    7431                 :             :      assign vector types to constants and externals and cause iteration
    7432                 :             :      to a higher vectorization factor when required.  */
    7433                 :     1640472 :   poly_uint64 node_vf
    7434                 :     1640472 :     = calculate_unrolling_factor (node->max_nunits, SLP_TREE_LANES (node));
    7435                 :     1640472 :   vf = force_common_multiple (vf, node_vf);
    7436                 :             : 
    7437                 :             :   /* For permute nodes that are fed from externs or constants we have to
    7438                 :             :      consider their number of lanes as well.  Likewise for store-lanes.  */
    7439                 :     1640472 :   if (SLP_TREE_CODE (node) == VEC_PERM_EXPR
    7440                 :     1519695 :       || node->ldst_lanes)
    7441                 :      495527 :     for (slp_tree child : SLP_TREE_CHILDREN (node))
    7442                 :      133196 :       if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
    7443                 :             :         {
    7444                 :        2759 :           poly_uint64 child_vf
    7445                 :        2759 :             = calculate_unrolling_factor (node->max_nunits,
    7446                 :             :                                           SLP_TREE_LANES (child));
    7447                 :        2759 :           vf = force_common_multiple (vf, child_vf);
    7448                 :             :         }
    7449                 :             : }
    7450                 :             : 
    7451                 :             : /* For each possible SLP instance decide whether to SLP it and calculate overall
    7452                 :             :    unrolling factor needed to SLP the loop.  Return TRUE if decided to SLP at
    7453                 :             :    least one instance.  */
    7454                 :             : 
    7455                 :             : bool
    7456                 :      241907 : vect_make_slp_decision (loop_vec_info loop_vinfo)
    7457                 :             : {
    7458                 :      241907 :   unsigned int i;
    7459                 :      241907 :   poly_uint64 unrolling_factor = 1;
    7460                 :      241907 :   const vec<slp_instance> &slp_instances
    7461                 :             :     = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
    7462                 :      241907 :   slp_instance instance;
    7463                 :      241907 :   int decided_to_slp = 0;
    7464                 :             : 
    7465                 :      241907 :   DUMP_VECT_SCOPE ("vect_make_slp_decision");
    7466                 :             : 
    7467                 :      241907 :   hash_set<slp_tree> visited;
    7468                 :      922911 :   FOR_EACH_VEC_ELT (slp_instances, i, instance)
    7469                 :             :     {
    7470                 :             :       /* FORNOW: SLP if you can.  */
    7471                 :             :       /* All unroll factors have the form:
    7472                 :             : 
    7473                 :             :            GET_MODE_SIZE (vinfo->vector_mode) * X
    7474                 :             : 
    7475                 :             :          for some rational X, so they must have a common multiple.  */
    7476                 :      439097 :       vect_update_slp_vf_for_node (SLP_INSTANCE_TREE (instance),
    7477                 :             :                                    unrolling_factor, visited);
    7478                 :             : 
    7479                 :             :       /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts.  Later we
    7480                 :             :          call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
    7481                 :             :          loop-based vectorization.  Such stmts will be marked as HYBRID.  */
    7482                 :      439097 :       vect_mark_slp_stmts (loop_vinfo, SLP_INSTANCE_TREE (instance));
    7483                 :      439097 :       decided_to_slp++;
    7484                 :             :     }
    7485                 :             : 
    7486                 :      241907 :   LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
    7487                 :             : 
    7488                 :      241907 :   if (decided_to_slp && dump_enabled_p ())
    7489                 :             :     {
    7490                 :       19578 :       dump_printf_loc (MSG_NOTE, vect_location,
    7491                 :             :                        "Decided to SLP %d instances. Unrolling factor ",
    7492                 :             :                        decided_to_slp);
    7493                 :       19578 :       dump_dec (MSG_NOTE, unrolling_factor);
    7494                 :       19578 :       dump_printf (MSG_NOTE, "\n");
    7495                 :             :     }
    7496                 :             : 
    7497                 :      241907 :   return (decided_to_slp > 0);
    7498                 :      241907 : }
    7499                 :             : 
    7500                 :             : /* Private data for vect_detect_hybrid_slp.  */
    7501                 :             : struct vdhs_data
    7502                 :             : {
    7503                 :             :   loop_vec_info loop_vinfo;
    7504                 :             :   vec<stmt_vec_info> *worklist;
    7505                 :             : };
    7506                 :             : 
    7507                 :             : /* Walker for walk_gimple_op.  */
    7508                 :             : 
    7509                 :             : static tree
    7510                 :       57897 : vect_detect_hybrid_slp (tree *tp, int *, void *data)
    7511                 :             : {
    7512                 :       57897 :   walk_stmt_info *wi = (walk_stmt_info *)data;
    7513                 :       57897 :   vdhs_data *dat = (vdhs_data *)wi->info;
    7514                 :             : 
    7515                 :       57897 :   if (wi->is_lhs)
    7516                 :             :     return NULL_TREE;
    7517                 :             : 
    7518                 :       37634 :   stmt_vec_info def_stmt_info = dat->loop_vinfo->lookup_def (*tp);
    7519                 :       37634 :   if (!def_stmt_info)
    7520                 :             :     return NULL_TREE;
    7521                 :       16487 :   def_stmt_info = vect_stmt_to_vectorize (def_stmt_info);
    7522                 :       16487 :   if (PURE_SLP_STMT (def_stmt_info))
    7523                 :             :     {
    7524                 :        5164 :       if (dump_enabled_p ())
    7525                 :         610 :         dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: %G",
    7526                 :             :                          def_stmt_info->stmt);
    7527                 :        5164 :       STMT_SLP_TYPE (def_stmt_info) = hybrid;
    7528                 :        5164 :       dat->worklist->safe_push (def_stmt_info);
    7529                 :             :     }
    7530                 :             : 
    7531                 :             :   return NULL_TREE;
    7532                 :             : }
    7533                 :             : 
    7534                 :             : /* Look if STMT_INFO is consumed by SLP indirectly and mark it pure_slp
    7535                 :             :    if so, otherwise pushing it to WORKLIST.  */
    7536                 :             : 
    7537                 :             : static void
    7538                 :       76330 : maybe_push_to_hybrid_worklist (vec_info *vinfo,
    7539                 :             :                                vec<stmt_vec_info> &worklist,
    7540                 :             :                                stmt_vec_info stmt_info)
    7541                 :             : {
    7542                 :       76330 :   if (dump_enabled_p ())
    7543                 :        3679 :     dump_printf_loc (MSG_NOTE, vect_location,
    7544                 :             :                      "Processing hybrid candidate : %G", stmt_info->stmt);
    7545                 :       76330 :   stmt_vec_info orig_info = vect_orig_stmt (stmt_info);
    7546                 :       76330 :   imm_use_iterator iter2;
    7547                 :       76330 :   ssa_op_iter iter1;
    7548                 :       76330 :   use_operand_p use_p;
    7549                 :       76330 :   def_operand_p def_p;
    7550                 :       76330 :   bool any_def = false;
    7551                 :      156050 :   FOR_EACH_PHI_OR_STMT_DEF (def_p, orig_info->stmt, iter1, SSA_OP_DEF)
    7552                 :             :     {
    7553                 :       11213 :       any_def = true;
    7554                 :       14904 :       FOR_EACH_IMM_USE_FAST (use_p, iter2, DEF_FROM_PTR (def_p))
    7555                 :             :         {
    7556                 :       11514 :           if (is_gimple_debug (USE_STMT (use_p)))
    7557                 :         193 :             continue;
    7558                 :       11321 :           stmt_vec_info use_info = vinfo->lookup_stmt (USE_STMT (use_p));
    7559                 :             :           /* An out-of loop use means this is a loop_vect sink.  */
    7560                 :       11321 :           if (!use_info)
    7561                 :             :             {
    7562                 :        1214 :               if (dump_enabled_p ())
    7563                 :         168 :                 dump_printf_loc (MSG_NOTE, vect_location,
    7564                 :             :                                  "Found loop_vect sink: %G", stmt_info->stmt);
    7565                 :        1214 :               worklist.safe_push (stmt_info);
    7566                 :       10741 :               return;
    7567                 :             :             }
    7568                 :       12038 :           else if (!STMT_SLP_TYPE (vect_stmt_to_vectorize (use_info)))
    7569                 :             :             {
    7570                 :        6609 :               if (dump_enabled_p ())
    7571                 :         952 :                 dump_printf_loc (MSG_NOTE, vect_location,
    7572                 :             :                                  "Found loop_vect use: %G", use_info->stmt);
    7573                 :        6609 :               worklist.safe_push (stmt_info);
    7574                 :        6609 :               return;
    7575                 :             :             }
    7576                 :             :         }
    7577                 :             :     }
    7578                 :             :   /* No def means this is a loop_vect sink.  Gimple conditionals also don't have a
    7579                 :             :      def but shouldn't be considered sinks.  */
    7580                 :       68507 :   if (!any_def && STMT_VINFO_DEF_TYPE (stmt_info) != vect_condition_def)
    7581                 :             :     {
    7582                 :        1704 :       if (dump_enabled_p ())
    7583                 :         192 :         dump_printf_loc (MSG_NOTE, vect_location,
    7584                 :             :                          "Found loop_vect sink: %G", stmt_info->stmt);
    7585                 :        1704 :       worklist.safe_push (stmt_info);
    7586                 :        1704 :       return;
    7587                 :             :     }
    7588                 :       66803 :   if (dump_enabled_p ())
    7589                 :        2367 :     dump_printf_loc (MSG_NOTE, vect_location,
    7590                 :             :                      "Marked SLP consumed stmt pure: %G", stmt_info->stmt);
    7591                 :       66803 :   STMT_SLP_TYPE (stmt_info) = pure_slp;
    7592                 :             : }
    7593                 :             : 
    7594                 :             : /* Find stmts that must be both vectorized and SLPed.  */
    7595                 :             : 
    7596                 :             : void
    7597                 :      236902 : vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
    7598                 :             : {
    7599                 :      236902 :   DUMP_VECT_SCOPE ("vect_detect_hybrid_slp");
    7600                 :             : 
    7601                 :             :   /* All stmts participating in SLP are marked pure_slp, all other
    7602                 :             :      stmts are loop_vect.
    7603                 :             :      First collect all loop_vect stmts into a worklist.
    7604                 :             :      SLP patterns cause not all original scalar stmts to appear in
    7605                 :             :      SLP_TREE_SCALAR_STMTS and thus not all of them are marked pure_slp.
    7606                 :             :      Rectify this here and do a backward walk over the IL only considering
    7607                 :             :      stmts as loop_vect when they are used by a loop_vect stmt and otherwise
    7608                 :             :      mark them as pure_slp.  */
    7609                 :      236902 :   auto_vec<stmt_vec_info> worklist;
    7610                 :      779408 :   for (int i = LOOP_VINFO_LOOP (loop_vinfo)->num_nodes - 1; i >= 0; --i)
    7611                 :             :     {
    7612                 :      542506 :       basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
    7613                 :     1192483 :       for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
    7614                 :      649977 :            gsi_next (&gsi))
    7615                 :             :         {
    7616                 :      649977 :           gphi *phi = gsi.phi ();
    7617                 :      649977 :           stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (phi);
    7618                 :      649977 :           if (!STMT_SLP_TYPE (stmt_info) && STMT_VINFO_RELEVANT (stmt_info))
    7619                 :        1826 :             maybe_push_to_hybrid_worklist (loop_vinfo,
    7620                 :             :                                            worklist, stmt_info);
    7621                 :             :         }
    7622                 :      542506 :       for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
    7623                 :     6738950 :            gsi_prev (&gsi))
    7624                 :             :         {
    7625                 :     3098222 :           gimple *stmt = gsi_stmt (gsi);
    7626                 :     3098222 :           if (is_gimple_debug (stmt))
    7627                 :      644831 :             continue;
    7628                 :     2453391 :           stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (stmt);
    7629                 :     2453391 :           if (STMT_VINFO_IN_PATTERN_P (stmt_info))
    7630                 :             :             {
    7631                 :      206591 :               for (gimple_stmt_iterator gsi2
    7632                 :      206591 :                      = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info));
    7633                 :      433617 :                    !gsi_end_p (gsi2); gsi_next (&gsi2))
    7634                 :             :                 {
    7635                 :      227026 :                   stmt_vec_info patt_info
    7636                 :      227026 :                     = loop_vinfo->lookup_stmt (gsi_stmt (gsi2));
    7637                 :      227026 :                   if (!STMT_SLP_TYPE (patt_info)
    7638                 :       83384 :                       && STMT_VINFO_RELEVANT (patt_info))
    7639                 :        2837 :                     maybe_push_to_hybrid_worklist (loop_vinfo,
    7640                 :             :                                                    worklist, patt_info);
    7641                 :             :                 }
    7642                 :      206591 :               stmt_info = STMT_VINFO_RELATED_STMT (stmt_info);
    7643                 :             :             }
    7644                 :     2453391 :           if (!STMT_SLP_TYPE (stmt_info) && STMT_VINFO_RELEVANT (stmt_info))
    7645                 :       71667 :             maybe_push_to_hybrid_worklist (loop_vinfo,
    7646                 :             :                                            worklist, stmt_info);
    7647                 :             :         }
    7648                 :             :     }
    7649                 :             : 
    7650                 :             :   /* Now we have a worklist of non-SLP stmts, follow use->def chains and
    7651                 :             :      mark any SLP vectorized stmt as hybrid.
    7652                 :             :      ???  We're visiting def stmts N times (once for each non-SLP and
    7653                 :             :      once for each hybrid-SLP use).  */
    7654                 :      236902 :   walk_stmt_info wi;
    7655                 :      236902 :   vdhs_data dat;
    7656                 :      236902 :   dat.worklist = &worklist;
    7657                 :      236902 :   dat.loop_vinfo = loop_vinfo;
    7658                 :      236902 :   memset (&wi, 0, sizeof (wi));
    7659                 :      236902 :   wi.info = (void *)&dat;
    7660                 :      253759 :   while (!worklist.is_empty ())
    7661                 :             :     {
    7662                 :       14691 :       stmt_vec_info stmt_info = worklist.pop ();
    7663                 :             :       /* Since SSA operands are not set up for pattern stmts we need
    7664                 :             :          to use walk_gimple_op.  */
    7665                 :       14691 :       wi.is_lhs = 0;
    7666                 :       14691 :       walk_gimple_op (stmt_info->stmt, vect_detect_hybrid_slp, &wi);
    7667                 :             :       /* For gather/scatter make sure to walk the offset operand, that
    7668                 :             :          can be a scaling and conversion away.  */
    7669                 :       14691 :       gather_scatter_info gs_info;
    7670                 :       14691 :       if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
    7671                 :       14691 :           && vect_check_gather_scatter (stmt_info, loop_vinfo, &gs_info))
    7672                 :             :         {
    7673                 :         880 :           int dummy;
    7674                 :         880 :           vect_detect_hybrid_slp (&gs_info.offset, &dummy, &wi);
    7675                 :             :         }
    7676                 :             :     }
    7677                 :      236902 : }
    7678                 :             : 
    7679                 :             : 
    7680                 :             : /* Initialize a bb_vec_info struct for the statements in BBS basic blocks.  */
    7681                 :             : 
    7682                 :     2297833 : _bb_vec_info::_bb_vec_info (vec<basic_block> _bbs, vec_info_shared *shared)
    7683                 :             :   : vec_info (vec_info::bb, shared),
    7684                 :     2297833 :     roots (vNULL)
    7685                 :             : {
    7686                 :             :   /* The region we are operating on.  bbs[0] is the entry, excluding
    7687                 :             :      its PHI nodes.  In the future we might want to track an explicit
    7688                 :             :      entry edge to cover bbs[0] PHI nodes and have a region entry
    7689                 :             :      insert location.  */
    7690                 :     2297833 :   bbs = _bbs.address ();
    7691                 :     2297833 :   nbbs = _bbs.length ();
    7692                 :             : 
    7693                 :    17109235 :   for (unsigned i = 0; i < nbbs; ++i)
    7694                 :             :     {
    7695                 :    14811402 :       if (i != 0)
    7696                 :    19476122 :         for (gphi_iterator si = gsi_start_phis (bbs[i]); !gsi_end_p (si);
    7697                 :     6962553 :              gsi_next (&si))
    7698                 :             :           {
    7699                 :     6962553 :             gphi *phi = si.phi ();
    7700                 :     6962553 :             gimple_set_uid (phi, 0);
    7701                 :     6962553 :             add_stmt (phi);
    7702                 :             :           }
    7703                 :    29622804 :       for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
    7704                 :   116192481 :            !gsi_end_p (gsi); gsi_next (&gsi))
    7705                 :             :         {
    7706                 :   101381079 :           gimple *stmt = gsi_stmt (gsi);
    7707                 :   101381079 :           gimple_set_uid (stmt, 0);
    7708                 :   101381079 :           if (is_gimple_debug (stmt))
    7709                 :    59128200 :             continue;
    7710                 :    42252879 :           add_stmt (stmt);
    7711                 :             :         }
    7712                 :             :     }
    7713                 :     2297833 : }
    7714                 :             : 
    7715                 :             : 
    7716                 :             : /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
    7717                 :             :    stmts in the basic block.  */
    7718                 :             : 
    7719                 :     2297833 : _bb_vec_info::~_bb_vec_info ()
    7720                 :             : {
    7721                 :             :   /* Reset region marker.  */
    7722                 :    17109235 :   for (unsigned i = 0; i < nbbs; ++i)
    7723                 :             :     {
    7724                 :    14811402 :       if (i != 0)
    7725                 :    19491216 :         for (gphi_iterator si = gsi_start_phis (bbs[i]); !gsi_end_p (si);
    7726                 :     6977647 :              gsi_next (&si))
    7727                 :             :           {
    7728                 :     6977647 :             gphi *phi = si.phi ();
    7729                 :     6977647 :             gimple_set_uid (phi, -1);
    7730                 :             :           }
    7731                 :    29622804 :       for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
    7732                 :   116135093 :            !gsi_end_p (gsi); gsi_next (&gsi))
    7733                 :             :         {
    7734                 :   101323691 :           gimple *stmt = gsi_stmt (gsi);
    7735                 :   101323691 :           gimple_set_uid (stmt, -1);
    7736                 :             :         }
    7737                 :             :     }
    7738                 :             : 
    7739                 :     3422793 :   for (unsigned i = 0; i < roots.length (); ++i)
    7740                 :             :     {
    7741                 :     1124960 :       roots[i].stmts.release ();
    7742                 :     1124960 :       roots[i].roots.release ();
    7743                 :     1124960 :       roots[i].remain.release ();
    7744                 :             :     }
    7745                 :     2297833 :   roots.release ();
    7746                 :     2297833 : }
    7747                 :             : 
    7748                 :             : /* Subroutine of vect_slp_analyze_node_operations.  Handle the root of NODE,
    7749                 :             :    given then that child nodes have already been processed, and that
    7750                 :             :    their def types currently match their SLP node's def type.  */
    7751                 :             : 
    7752                 :             : static bool
    7753                 :     2212864 : vect_slp_analyze_node_operations_1 (vec_info *vinfo, slp_tree node,
    7754                 :             :                                     slp_instance node_instance,
    7755                 :             :                                     stmt_vector_for_cost *cost_vec)
    7756                 :             : {
    7757                 :     2212864 :   stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
    7758                 :             : 
    7759                 :             :   /* Calculate the number of vector statements to be created for the scalar
    7760                 :             :      stmts in this node.  It is the number of scalar elements in one scalar
    7761                 :             :      iteration (DR_GROUP_SIZE) multiplied by VF divided by the number of
    7762                 :             :      elements in a vector.  For single-defuse-cycle, lane-reducing op, and
    7763                 :             :      PHI statement that starts reduction comprised of only lane-reducing ops,
    7764                 :             :      the number is more than effective vector statements actually required.  */
    7765                 :     2212864 :   SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vect_get_num_copies (vinfo, node);
    7766                 :             : 
    7767                 :             :   /* Handle purely internal nodes.  */
    7768                 :     2212864 :   if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
    7769                 :             :     {
    7770                 :      109558 :       if (!vectorizable_slp_permutation (vinfo, NULL, node, cost_vec))
    7771                 :             :         return false;
    7772                 :             : 
    7773                 :             :       stmt_vec_info slp_stmt_info;
    7774                 :             :       unsigned int i;
    7775                 :      281242 :       FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, slp_stmt_info)
    7776                 :             :         {
    7777                 :      173003 :           if (slp_stmt_info
    7778                 :      166920 :               && STMT_VINFO_LIVE_P (slp_stmt_info)
    7779                 :      173147 :               && !vectorizable_live_operation (vinfo, slp_stmt_info, node,
    7780                 :             :                                                node_instance, i,
    7781                 :             :                                                false, cost_vec))
    7782                 :             :             return false;
    7783                 :             :         }
    7784                 :             :       return true;
    7785                 :             :     }
    7786                 :             : 
    7787                 :     2103306 :   bool dummy;
    7788                 :     2103306 :   return vect_analyze_stmt (vinfo, stmt_info, &dummy,
    7789                 :             :                             node, node_instance, cost_vec);
    7790                 :             : }
    7791                 :             : 
    7792                 :             : /* Verify if we can externalize a set of internal defs.  */
    7793                 :             : 
    7794                 :             : static bool
    7795                 :      401493 : vect_slp_can_convert_to_external (const vec<stmt_vec_info> &stmts)
    7796                 :             : {
    7797                 :      401493 :   basic_block bb = NULL;
    7798                 :     1975981 :   for (stmt_vec_info stmt : stmts)
    7799                 :      893604 :     if (!stmt)
    7800                 :             :       return false;
    7801                 :             :     /* Constant generation uses get_later_stmt which can only handle
    7802                 :             :        defs from the same BB.  */
    7803                 :      893604 :     else if (!bb)
    7804                 :      401493 :       bb = gimple_bb (stmt->stmt);
    7805                 :      492111 :     else if (gimple_bb (stmt->stmt) != bb)
    7806                 :             :       return false;
    7807                 :             :   return true;
    7808                 :             : }
    7809                 :             : 
    7810                 :             : /* Try to build NODE from scalars, returning true on success.
    7811                 :             :    NODE_INSTANCE is the SLP instance that contains NODE.  */
    7812                 :             : 
    7813                 :             : static bool
    7814                 :      355213 : vect_slp_convert_to_external (vec_info *vinfo, slp_tree node,
    7815                 :             :                               slp_instance node_instance)
    7816                 :             : {
    7817                 :      355213 :   stmt_vec_info stmt_info;
    7818                 :      355213 :   unsigned int i;
    7819                 :             : 
    7820                 :      355213 :   if (!is_a <bb_vec_info> (vinfo)
    7821                 :       75593 :       || node == SLP_INSTANCE_TREE (node_instance)
    7822                 :       23683 :       || !SLP_TREE_SCALAR_STMTS (node).exists ()
    7823                 :       23629 :       || vect_contains_pattern_stmt_p (SLP_TREE_SCALAR_STMTS (node))
    7824                 :             :       /* Force the mask use to be built from scalars instead.  */
    7825                 :       21631 :       || VECTOR_BOOLEAN_TYPE_P (SLP_TREE_VECTYPE (node))
    7826                 :      376709 :       || !vect_slp_can_convert_to_external (SLP_TREE_SCALAR_STMTS (node)))
    7827                 :             :     return false;
    7828                 :             : 
    7829                 :       18984 :   if (dump_enabled_p ())
    7830                 :          80 :     dump_printf_loc (MSG_NOTE, vect_location,
    7831                 :             :                      "Building vector operands of %p from scalars instead\n",
    7832                 :             :                      (void *) node);
    7833                 :             : 
    7834                 :             :   /* Don't remove and free the child nodes here, since they could be
    7835                 :             :      referenced by other structures.  The analysis and scheduling phases
    7836                 :             :      (need to) ignore child nodes of anything that isn't vect_internal_def.  */
    7837                 :       18984 :   unsigned int group_size = SLP_TREE_LANES (node);
    7838                 :       18984 :   SLP_TREE_DEF_TYPE (node) = vect_external_def;
    7839                 :             :   /* Invariants get their vector type from the uses.  */
    7840                 :       18984 :   SLP_TREE_VECTYPE (node) = NULL_TREE;
    7841                 :       18984 :   SLP_TREE_SCALAR_OPS (node).safe_grow (group_size, true);
    7842                 :       18984 :   SLP_TREE_LOAD_PERMUTATION (node).release ();
    7843                 :       67307 :   FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
    7844                 :             :     {
    7845                 :       48323 :       tree lhs = gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt);
    7846                 :       48323 :       SLP_TREE_SCALAR_OPS (node)[i] = lhs;
    7847                 :             :     }
    7848                 :             :   return true;
    7849                 :             : }
    7850                 :             : 
    7851                 :             : /* Return true if all elements of the slice are the same.  */
    7852                 :             : bool
    7853                 :      464820 : vect_scalar_ops_slice::all_same_p () const
    7854                 :             : {
    7855                 :      507052 :   for (unsigned int i = 1; i < length; ++i)
    7856                 :      341775 :     if (!operand_equal_p (op (0), op (i)))
    7857                 :             :       return false;
    7858                 :             :   return true;
    7859                 :             : }
    7860                 :             : 
    7861                 :             : hashval_t
    7862                 :      636222 : vect_scalar_ops_slice_hash::hash (const value_type &s)
    7863                 :             : {
    7864                 :      636222 :   hashval_t hash = 0;
    7865                 :     1997396 :   for (unsigned i = 0; i < s.length; ++i)
    7866                 :     1361174 :     hash = iterative_hash_expr (s.op (i), hash);
    7867                 :      636222 :   return hash;
    7868                 :             : }
    7869                 :             : 
    7870                 :             : bool
    7871                 :      349988 : vect_scalar_ops_slice_hash::equal (const value_type &s1,
    7872                 :             :                                    const compare_type &s2)
    7873                 :             : {
    7874                 :      349988 :   if (s1.length != s2.length)
    7875                 :             :     return false;
    7876                 :      546158 :   for (unsigned i = 0; i < s1.length; ++i)
    7877                 :      468206 :     if (!operand_equal_p (s1.op (i), s2.op (i)))
    7878                 :             :       return false;
    7879                 :             :   return true;
    7880                 :             : }
    7881                 :             : 
    7882                 :             : /* Compute the prologue cost for invariant or constant operands represented
    7883                 :             :    by NODE.  */
    7884                 :             : 
    7885                 :             : static void
    7886                 :      933747 : vect_prologue_cost_for_slp (slp_tree node,
    7887                 :             :                             stmt_vector_for_cost *cost_vec)
    7888                 :             : {
    7889                 :             :   /* There's a special case of an existing vector, that costs nothing.  */
    7890                 :      933747 :   if (SLP_TREE_SCALAR_OPS (node).length () == 0
    7891                 :      933747 :       && !SLP_TREE_VEC_DEFS (node).is_empty ())
    7892                 :        1755 :     return;
    7893                 :             :   /* Without looking at the actual initializer a vector of
    7894                 :             :      constants can be implemented as load from the constant pool.
    7895                 :             :      When all elements are the same we can use a splat.  */
    7896                 :      931992 :   tree vectype = SLP_TREE_VECTYPE (node);
    7897                 :      931992 :   unsigned group_size = SLP_TREE_SCALAR_OPS (node).length ();
    7898                 :      931992 :   unsigned HOST_WIDE_INT const_nunits;
    7899                 :      931992 :   unsigned nelt_limit;
    7900                 :      931992 :   auto ops = &SLP_TREE_SCALAR_OPS (node);
    7901                 :      931992 :   auto_vec<unsigned int> starts (SLP_TREE_NUMBER_OF_VEC_STMTS (node));
    7902                 :      931992 :   if (TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits)
    7903                 :      931992 :       && ! multiple_p (const_nunits, group_size))
    7904                 :             :     {
    7905                 :      107267 :       nelt_limit = const_nunits;
    7906                 :      107267 :       hash_set<vect_scalar_ops_slice_hash> vector_ops;
    7907                 :      425023 :       for (unsigned int i = 0; i < SLP_TREE_NUMBER_OF_VEC_STMTS (node); ++i)
    7908                 :      317756 :         if (!vector_ops.add ({ ops, i * nelt_limit, nelt_limit }))
    7909                 :      239804 :           starts.quick_push (i * nelt_limit);
    7910                 :      107267 :     }
    7911                 :             :   else
    7912                 :             :     {
    7913                 :             :       /* If either the vector has variable length or the vectors
    7914                 :             :          are composed of repeated whole groups we only need to
    7915                 :             :          cost construction once.  All vectors will be the same.  */
    7916                 :      824725 :       nelt_limit = group_size;
    7917                 :      824725 :       starts.quick_push (0);
    7918                 :             :     }
    7919                 :             :   /* ???  We're just tracking whether vectors in a single node are the same.
    7920                 :             :      Ideally we'd do something more global.  */
    7921                 :      931992 :   bool passed = false;
    7922                 :     3860505 :   for (unsigned int start : starts)
    7923                 :             :     {
    7924                 :     1064529 :       vect_cost_for_stmt kind;
    7925                 :     1064529 :       if (SLP_TREE_DEF_TYPE (node) == vect_constant_def)
    7926                 :             :         kind = vector_load;
    7927                 :      464820 :       else if (vect_scalar_ops_slice { ops, start, nelt_limit }.all_same_p ())
    7928                 :             :         kind = scalar_to_vec;
    7929                 :             :       else
    7930                 :      299543 :         kind = vec_construct;
    7931                 :             :       /* The target cost hook has no idea which part of the SLP node
    7932                 :             :          we are costing so avoid passing it down more than once.  Pass
    7933                 :             :          it to the first vec_construct or scalar_to_vec part since for those
    7934                 :             :          the x86 backend tries to account for GPR to XMM register moves.  */
    7935                 :     1064529 :       record_stmt_cost (cost_vec, 1, kind,
    7936                 :     1064529 :                         (kind != vector_load && !passed) ? node : nullptr,
    7937                 :             :                         vectype, 0, vect_prologue);
    7938                 :     1064529 :       if (kind != vector_load)
    7939                 :      464820 :         passed = true;
    7940                 :             :     }
    7941                 :      931992 : }
    7942                 :             : 
    7943                 :             : /* Analyze statements contained in SLP tree NODE after recursively analyzing
    7944                 :             :    the subtree.  NODE_INSTANCE contains NODE and VINFO contains INSTANCE.
    7945                 :             : 
    7946                 :             :    Return true if the operations are supported.  */
    7947                 :             : 
    7948                 :             : static bool
    7949                 :     4016344 : vect_slp_analyze_node_operations (vec_info *vinfo, slp_tree node,
    7950                 :             :                                   slp_instance node_instance,
    7951                 :             :                                   hash_set<slp_tree> &visited_set,
    7952                 :             :                                   vec<slp_tree> &visited_vec,
    7953                 :             :                                   stmt_vector_for_cost *cost_vec)
    7954                 :             : {
    7955                 :     4016344 :   int i, j;
    7956                 :     4016344 :   slp_tree child;
    7957                 :             : 
    7958                 :             :   /* Assume we can code-generate all invariants.  */
    7959                 :     4016344 :   if (!node
    7960                 :     3792435 :       || SLP_TREE_DEF_TYPE (node) == vect_constant_def
    7961                 :     3127662 :       || SLP_TREE_DEF_TYPE (node) == vect_external_def)
    7962                 :             :     return true;
    7963                 :             : 
    7964                 :     2666938 :   if (SLP_TREE_DEF_TYPE (node) == vect_uninitialized_def)
    7965                 :             :     {
    7966                 :          18 :       if (dump_enabled_p ())
    7967                 :           0 :         dump_printf_loc (MSG_NOTE, vect_location,
    7968                 :             :                          "Failed cyclic SLP reference in %p\n", (void *) node);
    7969                 :          18 :       return false;
    7970                 :             :     }
    7971                 :     2666920 :   gcc_assert (SLP_TREE_DEF_TYPE (node) == vect_internal_def);
    7972                 :             : 
    7973                 :             :   /* If we already analyzed the exact same set of scalar stmts we're done.
    7974                 :             :      We share the generated vector stmts for those.  */
    7975                 :     2666920 :   if (visited_set.add (node))
    7976                 :             :     return true;
    7977                 :     2419565 :   visited_vec.safe_push (node);
    7978                 :             : 
    7979                 :     2419565 :   bool res = true;
    7980                 :     2419565 :   unsigned visited_rec_start = visited_vec.length ();
    7981                 :     2419565 :   unsigned cost_vec_rec_start = cost_vec->length ();
    7982                 :     2419565 :   bool seen_non_constant_child = false;
    7983                 :     5179516 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    7984                 :             :     {
    7985                 :     2966501 :       res = vect_slp_analyze_node_operations (vinfo, child, node_instance,
    7986                 :             :                                               visited_set, visited_vec,
    7987                 :             :                                               cost_vec);
    7988                 :     2966501 :       if (!res)
    7989                 :             :         break;
    7990                 :     2759951 :       if (child && SLP_TREE_DEF_TYPE (child) != vect_constant_def)
    7991                 :     2759951 :         seen_non_constant_child = true;
    7992                 :             :     }
    7993                 :             :   /* We're having difficulties scheduling nodes with just constant
    7994                 :             :      operands and no scalar stmts since we then cannot compute a stmt
    7995                 :             :      insertion place.  */
    7996                 :     2419565 :   if (res
    7997                 :     2419565 :       && !seen_non_constant_child
    7998                 :     2419565 :       && SLP_TREE_SCALAR_STMTS (node).is_empty ())
    7999                 :             :     {
    8000                 :         151 :       if (dump_enabled_p ())
    8001                 :           6 :         dump_printf_loc (MSG_NOTE, vect_location,
    8002                 :             :                          "Cannot vectorize all-constant op node %p\n",
    8003                 :             :                          (void *) node);
    8004                 :             :       res = false;
    8005                 :             :     }
    8006                 :             : 
    8007                 :     2419414 :   if (res)
    8008                 :     2212864 :     res = vect_slp_analyze_node_operations_1 (vinfo, node, node_instance,
    8009                 :             :                                               cost_vec);
    8010                 :             :   /* If analysis failed we have to pop all recursive visited nodes
    8011                 :             :      plus ourselves.  */
    8012                 :     2419565 :   if (!res)
    8013                 :             :     {
    8014                 :     1979392 :       while (visited_vec.length () >= visited_rec_start)
    8015                 :      634483 :         visited_set.remove (visited_vec.pop ());
    8016                 :      355213 :       cost_vec->truncate (cost_vec_rec_start);
    8017                 :             :     }
    8018                 :             : 
    8019                 :             :   /* When the node can be vectorized cost invariant nodes it references.
    8020                 :             :      This is not done in DFS order to allow the refering node
    8021                 :             :      vectorizable_* calls to nail down the invariant nodes vector type
    8022                 :             :      and possibly unshare it if it needs a different vector type than
    8023                 :             :      other referrers.  */
    8024                 :     2419565 :   if (res)
    8025                 :     4541098 :     FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
    8026                 :     2476746 :       if (child
    8027                 :     2282754 :           && (SLP_TREE_DEF_TYPE (child) == vect_constant_def
    8028                 :     2282754 :               || SLP_TREE_DEF_TYPE (child) == vect_external_def)
    8029                 :             :           /* Perform usual caching, note code-generation still
    8030                 :             :              code-gens these nodes multiple times but we expect
    8031                 :             :              to CSE them later.  */
    8032                 :     3468880 :           && !visited_set.add (child))
    8033                 :             :         {
    8034                 :      967963 :           visited_vec.safe_push (child);
    8035                 :             :           /* ???  After auditing more code paths make a "default"
    8036                 :             :              and push the vector type from NODE to all children
    8037                 :             :              if it is not already set.  */
    8038                 :             :           /* Compute the number of vectors to be generated.  */
    8039                 :      967963 :           tree vector_type = SLP_TREE_VECTYPE (child);
    8040                 :      967963 :           if (!vector_type)
    8041                 :             :             {
    8042                 :             :               /* Masked loads can have an undefined (default SSA definition)
    8043                 :             :                  else operand.  We do not need to cost it.  */
    8044                 :       34216 :               vec<tree> ops = SLP_TREE_SCALAR_OPS (child);
    8045                 :       35387 :               if ((STMT_VINFO_TYPE (SLP_TREE_REPRESENTATIVE (node))
    8046                 :             :                    == load_vec_info_type)
    8047                 :       35387 :                   && ((ops.length ()
    8048                 :        1171 :                        && TREE_CODE (ops[0]) == SSA_NAME
    8049                 :           0 :                        && SSA_NAME_IS_DEFAULT_DEF (ops[0])
    8050                 :           0 :                        && VAR_P (SSA_NAME_VAR (ops[0])))
    8051                 :        1171 :                       || SLP_TREE_DEF_TYPE (child) == vect_constant_def))
    8052                 :        1171 :                 continue;
    8053                 :             : 
    8054                 :             :               /* For shifts with a scalar argument we don't need
    8055                 :             :                  to cost or code-generate anything.
    8056                 :             :                  ???  Represent this more explicitely.  */
    8057                 :       33045 :               gcc_assert ((STMT_VINFO_TYPE (SLP_TREE_REPRESENTATIVE (node))
    8058                 :             :                            == shift_vec_info_type)
    8059                 :             :                           && j == 1);
    8060                 :       33045 :               continue;
    8061                 :       33045 :             }
    8062                 :             : 
    8063                 :      933747 :           SLP_TREE_NUMBER_OF_VEC_STMTS (child)
    8064                 :      933747 :                 = vect_get_num_copies (vinfo, child);
    8065                 :             :           /* And cost them.  */
    8066                 :      933747 :           vect_prologue_cost_for_slp (child, cost_vec);
    8067                 :             :         }
    8068                 :             : 
    8069                 :             :   /* If this node or any of its children can't be vectorized, try pruning
    8070                 :             :      the tree here rather than felling the whole thing.  */
    8071                 :      355213 :   if (!res && vect_slp_convert_to_external (vinfo, node, node_instance))
    8072                 :             :     {
    8073                 :             :       /* We'll need to revisit this for invariant costing and number
    8074                 :             :          of vectorized stmt setting.   */
    8075                 :             :       res = true;
    8076                 :             :     }
    8077                 :             : 
    8078                 :             :   return res;
    8079                 :             : }
    8080                 :             : 
    8081                 :             : /* Given a definition DEF, analyze if it will have any live scalar use after
    8082                 :             :    performing SLP vectorization whose information is represented by BB_VINFO,
    8083                 :             :    and record result into hash map SCALAR_USE_MAP as cache for later fast
    8084                 :             :    check.  If recursion DEPTH exceeds a limit, stop analysis and make a
    8085                 :             :    conservative assumption.  Return 0 if no scalar use, 1 if there is, -1
    8086                 :             :    means recursion is limited.  */
    8087                 :             : 
    8088                 :             : static int
    8089                 :      541044 : vec_slp_has_scalar_use (bb_vec_info bb_vinfo, tree def,
    8090                 :             :                         hash_map<tree, int> &scalar_use_map,
    8091                 :             :                         int depth = 0)
    8092                 :             : {
    8093                 :      541044 :   const int depth_limit = 2;
    8094                 :      541044 :   imm_use_iterator use_iter;
    8095                 :      541044 :   gimple *use_stmt;
    8096                 :             : 
    8097                 :      541044 :   if (int *res = scalar_use_map.get (def))
    8098                 :       15244 :     return *res;
    8099                 :             : 
    8100                 :      525800 :   int scalar_use = 1;
    8101                 :             : 
    8102                 :     1212483 :   FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def)
    8103                 :             :     {
    8104                 :      793489 :       if (is_gimple_debug (use_stmt))
    8105                 :      196570 :         continue;
    8106                 :             : 
    8107                 :      596919 :       stmt_vec_info use_stmt_info = bb_vinfo->lookup_stmt (use_stmt);
    8108                 :             : 
    8109                 :      596919 :       if (!use_stmt_info)
    8110                 :             :         break;
    8111                 :             : 
    8112                 :      598327 :       if (PURE_SLP_STMT (vect_stmt_to_vectorize (use_stmt_info)))
    8113                 :      488399 :         continue;
    8114                 :             : 
    8115                 :             :       /* Do not step forward when encounter PHI statement, since it may
    8116                 :             :          involve cyclic reference and cause infinite recursive invocation.  */
    8117                 :      102365 :       if (gimple_code (use_stmt) == GIMPLE_PHI)
    8118                 :             :         break;
    8119                 :             : 
    8120                 :             :       /* When pattern recognition is involved, a statement whose definition is
    8121                 :             :          consumed in some pattern, may not be included in the final replacement
    8122                 :             :          pattern statements, so would be skipped when building SLP graph.
    8123                 :             : 
    8124                 :             :          * Original
    8125                 :             :           char a_c = *(char *) a;
    8126                 :             :           char b_c = *(char *) b;
    8127                 :             :           unsigned short a_s = (unsigned short) a_c;
    8128                 :             :           int a_i = (int) a_s;
    8129                 :             :           int b_i = (int) b_c;
    8130                 :             :           int r_i = a_i - b_i;
    8131                 :             : 
    8132                 :             :          * After pattern replacement
    8133                 :             :           a_s = (unsigned short) a_c;
    8134                 :             :           a_i = (int) a_s;
    8135                 :             : 
    8136                 :             :           patt_b_s = (unsigned short) b_c;    // b_i = (int) b_c
    8137                 :             :           patt_b_i = (int) patt_b_s;          // b_i = (int) b_c
    8138                 :             : 
    8139                 :             :           patt_r_s = widen_minus(a_c, b_c);   // r_i = a_i - b_i
    8140                 :             :           patt_r_i = (int) patt_r_s;          // r_i = a_i - b_i
    8141                 :             : 
    8142                 :             :          The definitions of a_i(original statement) and b_i(pattern statement)
    8143                 :             :          are related to, but actually not part of widen_minus pattern.
    8144                 :             :          Vectorizing the pattern does not cause these definition statements to
    8145                 :             :          be marked as PURE_SLP.  For this case, we need to recursively check
    8146                 :             :          whether their uses are all absorbed into vectorized code.  But there
    8147                 :             :          is an exception that some use may participate in an vectorized
    8148                 :             :          operation via an external SLP node containing that use as an element.
    8149                 :             :          The parameter "scalar_use_map" tags such kind of SSA as having scalar
    8150                 :             :          use in advance.  */
    8151                 :       82758 :       tree lhs = gimple_get_lhs (use_stmt);
    8152                 :             : 
    8153                 :       82758 :       if (!lhs || TREE_CODE (lhs) != SSA_NAME)
    8154                 :             :         break;
    8155                 :             : 
    8156                 :       49935 :       if (depth_limit && depth >= depth_limit)
    8157                 :        9244 :         return -1;
    8158                 :             : 
    8159                 :       40691 :       if ((scalar_use = vec_slp_has_scalar_use (bb_vinfo, lhs, scalar_use_map,
    8160                 :             :                                                 depth + 1)))
    8161                 :             :         break;
    8162                 :      525800 :     }
    8163                 :             : 
    8164                 :      516556 :   if (end_imm_use_stmt_p (&use_iter))
    8165                 :      418994 :     scalar_use = 0;
    8166                 :             : 
    8167                 :             :   /* If recursion is limited, do not cache result for non-root defs.  */
    8168                 :      516556 :   if (!depth || scalar_use >= 0)
    8169                 :             :     {
    8170                 :      507312 :       bool added = scalar_use_map.put (def, scalar_use);
    8171                 :      507312 :       gcc_assert (!added);
    8172                 :             :     }
    8173                 :             : 
    8174                 :      516556 :   return scalar_use;
    8175                 :             : }
    8176                 :             : 
    8177                 :             : /* Mark lanes of NODE that are live outside of the basic-block vectorized
    8178                 :             :    region and that can be vectorized using vectorizable_live_operation
    8179                 :             :    with STMT_VINFO_LIVE_P.  Not handled live operations will cause the
    8180                 :             :    scalar code computing it to be retained.  */
    8181                 :             : 
    8182                 :             : static void
    8183                 :      883827 : vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo, slp_tree node,
    8184                 :             :                              slp_instance instance,
    8185                 :             :                              stmt_vector_for_cost *cost_vec,
    8186                 :             :                              hash_map<tree, int> &scalar_use_map,
    8187                 :             :                              hash_set<stmt_vec_info> &svisited,
    8188                 :             :                              hash_set<slp_tree> &visited)
    8189                 :             : {
    8190                 :      883827 :   if (visited.add (node))
    8191                 :       26307 :     return;
    8192                 :             : 
    8193                 :      857520 :   unsigned i;
    8194                 :      857520 :   stmt_vec_info stmt_info;
    8195                 :      857520 :   stmt_vec_info last_stmt = vect_find_last_scalar_stmt_in_slp (node);
    8196                 :     3065065 :   FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
    8197                 :             :     {
    8198                 :     2207545 :       if (!stmt_info || svisited.contains (stmt_info))
    8199                 :       23901 :         continue;
    8200                 :     2192065 :       stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
    8201                 :     2192065 :       if (STMT_VINFO_IN_PATTERN_P (orig_stmt_info)
    8202                 :       11201 :           && STMT_VINFO_RELATED_STMT (orig_stmt_info) != stmt_info)
    8203                 :             :         /* Only the pattern root stmt computes the original scalar value.  */
    8204                 :        8421 :         continue;
    8205                 :     2183644 :       bool mark_visited = true;
    8206                 :     2183644 :       gimple *orig_stmt = orig_stmt_info->stmt;
    8207                 :     2183644 :       ssa_op_iter op_iter;
    8208                 :     2183644 :       def_operand_p def_p;
    8209                 :     4867641 :       FOR_EACH_PHI_OR_STMT_DEF (def_p, orig_stmt, op_iter, SSA_OP_DEF)
    8210                 :             :         {
    8211                 :      500353 :           if (vec_slp_has_scalar_use (bb_vinfo, DEF_FROM_PTR (def_p),
    8212                 :             :                                       scalar_use_map))
    8213                 :             :             {
    8214                 :       82827 :               STMT_VINFO_LIVE_P (stmt_info) = true;
    8215                 :       82827 :               if (vectorizable_live_operation (bb_vinfo, stmt_info, node,
    8216                 :             :                                                instance, i, false, cost_vec))
    8217                 :             :                 /* ???  So we know we can vectorize the live stmt from one SLP
    8218                 :             :                    node.  If we cannot do so from all or none consistently
    8219                 :             :                    we'd have to record which SLP node (and lane) we want to
    8220                 :             :                    use for the live operation.  So make sure we can
    8221                 :             :                    code-generate from all nodes.  */
    8222                 :             :                 mark_visited = false;
    8223                 :             :               else
    8224                 :           0 :                 STMT_VINFO_LIVE_P (stmt_info) = false;
    8225                 :             :             }
    8226                 :             : 
    8227                 :             :           /* We have to verify whether we can insert the lane extract
    8228                 :             :              before all uses.  The following is a conservative approximation.
    8229                 :             :              We cannot put this into vectorizable_live_operation because
    8230                 :             :              iterating over all use stmts from inside a FOR_EACH_IMM_USE_STMT
    8231                 :             :              doesn't work.
    8232                 :             :              Note that while the fact that we emit code for loads at the
    8233                 :             :              first load should make this a non-problem leafs we construct
    8234                 :             :              from scalars are vectorized after the last scalar def.
    8235                 :             :              ???  If we'd actually compute the insert location during
    8236                 :             :              analysis we could use sth less conservative than the last
    8237                 :             :              scalar stmt in the node for the dominance check.  */
    8238                 :             :           /* ???  What remains is "live" uses in vector CTORs in the same
    8239                 :             :              SLP graph which is where those uses can end up code-generated
    8240                 :             :              right after their definition instead of close to their original
    8241                 :             :              use.  But that would restrict us to code-generate lane-extracts
    8242                 :             :              from the latest stmt in a node.  So we compensate for this
    8243                 :             :              during code-generation, simply not replacing uses for those
    8244                 :             :              hopefully rare cases.  */
    8245                 :      500353 :           imm_use_iterator use_iter;
    8246                 :      500353 :           gimple *use_stmt;
    8247                 :      500353 :           stmt_vec_info use_stmt_info;
    8248                 :             : 
    8249                 :      500353 :           if (STMT_VINFO_LIVE_P (stmt_info))
    8250                 :      452904 :             FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
    8251                 :      370077 :               if (!is_gimple_debug (use_stmt)
    8252                 :      282636 :                   && (!(use_stmt_info = bb_vinfo->lookup_stmt (use_stmt))
    8253                 :      271782 :                       || !PURE_SLP_STMT (vect_stmt_to_vectorize (use_stmt_info)))
    8254                 :      531600 :                   && !vect_stmt_dominates_stmt_p (last_stmt->stmt, use_stmt))
    8255                 :             :                 {
    8256                 :       12159 :                   if (dump_enabled_p ())
    8257                 :         284 :                     dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    8258                 :             :                                      "Cannot determine insertion place for "
    8259                 :             :                                      "lane extract\n");
    8260                 :       12159 :                   STMT_VINFO_LIVE_P (stmt_info) = false;
    8261                 :       12159 :                   mark_visited = true;
    8262                 :       82827 :                 }
    8263                 :             :         }
    8264                 :     2183644 :       if (mark_visited)
    8265                 :     2111871 :         svisited.add (stmt_info);
    8266                 :             :     }
    8267                 :             : 
    8268                 :             :   slp_tree child;
    8269                 :     2430793 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    8270                 :      833275 :     if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
    8271                 :      225179 :       vect_bb_slp_mark_live_stmts (bb_vinfo, child, instance, cost_vec,
    8272                 :             :                                    scalar_use_map, svisited, visited);
    8273                 :             : }
    8274                 :             : 
    8275                 :             : /* Traverse all slp instances of BB_VINFO, and mark lanes of every node that
    8276                 :             :    are live outside of the basic-block vectorized region and that can be
    8277                 :             :    vectorized using vectorizable_live_operation with STMT_VINFO_LIVE_P.  */
    8278                 :             : 
    8279                 :             : static void
    8280                 :      314290 : vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo)
    8281                 :             : {
    8282                 :      314290 :   if (bb_vinfo->slp_instances.is_empty ())
    8283                 :       34824 :     return;
    8284                 :             : 
    8285                 :      279466 :   hash_set<stmt_vec_info> svisited;
    8286                 :      279466 :   hash_set<slp_tree> visited;
    8287                 :      279466 :   hash_map<tree, int> scalar_use_map;
    8288                 :      279466 :   auto_vec<slp_tree> worklist;
    8289                 :             : 
    8290                 :     1497046 :   for (slp_instance instance : bb_vinfo->slp_instances)
    8291                 :             :     {
    8292                 :      658648 :       if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc)
    8293                 :       41790 :         for (tree op : SLP_INSTANCE_REMAIN_DEFS (instance))
    8294                 :       13703 :           if (TREE_CODE (op) == SSA_NAME)
    8295                 :       12385 :             scalar_use_map.put (op, 1);
    8296                 :      658648 :       if (!visited.add (SLP_INSTANCE_TREE (instance)))
    8297                 :      657235 :         worklist.safe_push (SLP_INSTANCE_TREE (instance));
    8298                 :             :     }
    8299                 :             : 
    8300                 :     1464701 :   do
    8301                 :             :     {
    8302                 :     1464701 :       slp_tree node = worklist.pop ();
    8303                 :             : 
    8304                 :     1464701 :       if (SLP_TREE_DEF_TYPE (node) == vect_external_def)
    8305                 :             :         {
    8306                 :     1466515 :           for (tree op : SLP_TREE_SCALAR_OPS (node))
    8307                 :      646157 :             if (TREE_CODE (op) == SSA_NAME)
    8308                 :      419360 :               scalar_use_map.put (op, 1);
    8309                 :             :         }
    8310                 :             :       else
    8311                 :             :         {
    8312                 :     3504032 :           for (slp_tree child : SLP_TREE_CHILDREN (node))
    8313                 :      833251 :             if (child && !visited.add (child))
    8314                 :      807466 :               worklist.safe_push (child);
    8315                 :             :         }
    8316                 :             :     }
    8317                 :     2929402 :   while (!worklist.is_empty ());
    8318                 :             : 
    8319                 :      279466 :   visited.empty ();
    8320                 :             : 
    8321                 :     1497046 :   for (slp_instance instance : bb_vinfo->slp_instances)
    8322                 :             :     {
    8323                 :      658648 :       vect_location = instance->location ();
    8324                 :      658648 :       vect_bb_slp_mark_live_stmts (bb_vinfo, SLP_INSTANCE_TREE (instance),
    8325                 :             :                                    instance, &instance->cost_vec,
    8326                 :             :                                    scalar_use_map, svisited, visited);
    8327                 :             :     }
    8328                 :      279466 : }
    8329                 :             : 
    8330                 :             : /* Determine whether we can vectorize the reduction epilogue for INSTANCE.  */
    8331                 :             : 
    8332                 :             : static bool
    8333                 :       59987 : vectorizable_bb_reduc_epilogue (slp_instance instance,
    8334                 :             :                                 stmt_vector_for_cost *cost_vec)
    8335                 :             : {
    8336                 :       59987 :   gassign *stmt = as_a <gassign *> (instance->root_stmts[0]->stmt);
    8337                 :       59987 :   enum tree_code reduc_code = gimple_assign_rhs_code (stmt);
    8338                 :       59987 :   if (reduc_code == MINUS_EXPR)
    8339                 :           0 :     reduc_code = PLUS_EXPR;
    8340                 :       59987 :   internal_fn reduc_fn;
    8341                 :       59987 :   tree vectype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (instance));
    8342                 :       59987 :   if (!vectype
    8343                 :       59894 :       || !reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
    8344                 :       59894 :       || reduc_fn == IFN_LAST
    8345                 :       59894 :       || !direct_internal_fn_supported_p (reduc_fn, vectype, OPTIMIZE_FOR_BOTH)
    8346                 :       85908 :       || !useless_type_conversion_p (TREE_TYPE (gimple_assign_lhs (stmt)),
    8347                 :       25921 :                                      TREE_TYPE (vectype)))
    8348                 :             :     {
    8349                 :       44279 :       if (dump_enabled_p ())
    8350                 :         254 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    8351                 :             :                          "not vectorized: basic block reduction epilogue "
    8352                 :             :                          "operation unsupported.\n");
    8353                 :       44279 :       return false;
    8354                 :             :     }
    8355                 :             : 
    8356                 :             :   /* There's no way to cost a horizontal vector reduction via REDUC_FN so
    8357                 :             :      cost log2 vector operations plus shuffles and one extraction.  */
    8358                 :       15708 :   unsigned steps = floor_log2 (vect_nunits_for_cost (vectype));
    8359                 :       15708 :   record_stmt_cost (cost_vec, steps, vector_stmt, instance->root_stmts[0],
    8360                 :             :                     vectype, 0, vect_body);
    8361                 :       15708 :   record_stmt_cost (cost_vec, steps, vec_perm, instance->root_stmts[0],
    8362                 :             :                     vectype, 0, vect_body);
    8363                 :       15708 :   record_stmt_cost (cost_vec, 1, vec_to_scalar, instance->root_stmts[0],
    8364                 :             :                     vectype, 0, vect_body);
    8365                 :             : 
    8366                 :             :   /* Since we replace all stmts of a possibly longer scalar reduction
    8367                 :             :      chain account for the extra scalar stmts for that.  */
    8368                 :       15708 :   record_stmt_cost (cost_vec, instance->remain_defs.length (), scalar_stmt,
    8369                 :       15708 :                     instance->root_stmts[0], 0, vect_body);
    8370                 :       15708 :   return true;
    8371                 :             : }
    8372                 :             : 
    8373                 :             : /* Prune from ROOTS all stmts that are computed as part of lanes of NODE
    8374                 :             :    and recurse to children.  */
    8375                 :             : 
    8376                 :             : static void
    8377                 :      121310 : vect_slp_prune_covered_roots (slp_tree node, hash_set<stmt_vec_info> &roots,
    8378                 :             :                               hash_set<slp_tree> &visited)
    8379                 :             : {
    8380                 :      121310 :   if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
    8381                 :      121310 :       || visited.add (node))
    8382                 :       54237 :     return;
    8383                 :             : 
    8384                 :             :   stmt_vec_info stmt;
    8385                 :             :   unsigned i;
    8386                 :      218943 :   FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
    8387                 :      151870 :     if (stmt)
    8388                 :      154858 :       roots.remove (vect_orig_stmt (stmt));
    8389                 :             : 
    8390                 :             :   slp_tree child;
    8391                 :      158378 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    8392                 :       91305 :     if (child)
    8393                 :       85636 :       vect_slp_prune_covered_roots (child, roots, visited);
    8394                 :             : }
    8395                 :             : 
    8396                 :             : /* Analyze statements in SLP instances of VINFO.  Return true if the
    8397                 :             :    operations are supported. */
    8398                 :             : 
    8399                 :             : bool
    8400                 :      545311 : vect_slp_analyze_operations (vec_info *vinfo)
    8401                 :             : {
    8402                 :      545311 :   slp_instance instance;
    8403                 :      545311 :   int i;
    8404                 :             : 
    8405                 :      545311 :   DUMP_VECT_SCOPE ("vect_slp_analyze_operations");
    8406                 :             : 
    8407                 :      545311 :   hash_set<slp_tree> visited;
    8408                 :     1481323 :   for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
    8409                 :             :     {
    8410                 :     1049843 :       auto_vec<slp_tree> visited_vec;
    8411                 :     1049843 :       stmt_vector_for_cost cost_vec;
    8412                 :     1049843 :       cost_vec.create (2);
    8413                 :     1049843 :       if (is_a <bb_vec_info> (vinfo))
    8414                 :      756273 :         vect_location = instance->location ();
    8415                 :     1049843 :       if (!vect_slp_analyze_node_operations (vinfo,
    8416                 :             :                                              SLP_INSTANCE_TREE (instance),
    8417                 :             :                                              instance, visited, visited_vec,
    8418                 :             :                                              &cost_vec)
    8419                 :             :           /* CTOR instances require vectorized defs for the SLP tree root.  */
    8420                 :      920146 :           || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_ctor
    8421                 :         639 :               && (SLP_TREE_DEF_TYPE (SLP_INSTANCE_TREE (instance))
    8422                 :             :                   != vect_internal_def
    8423                 :             :                   /* Make sure we vectorized with the expected type.  */
    8424                 :         635 :                   || !useless_type_conversion_p
    8425                 :         635 :                         (TREE_TYPE (TREE_TYPE (gimple_assign_rhs1
    8426                 :             :                                               (instance->root_stmts[0]->stmt))),
    8427                 :         635 :                          TREE_TYPE (SLP_TREE_VECTYPE
    8428                 :             :                                             (SLP_INSTANCE_TREE (instance))))))
    8429                 :             :           /* Check we can vectorize the reduction.  */
    8430                 :      920139 :           || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc
    8431                 :       59987 :               && !vectorizable_bb_reduc_epilogue (instance, &cost_vec))
    8432                 :             :           /* Check we can vectorize the gcond.  */
    8433                 :     1925703 :           || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_gcond
    8434                 :       37797 :               && !vectorizable_early_exit (vinfo,
    8435                 :       37797 :                                            SLP_INSTANCE_ROOT_STMTS (instance)[0],
    8436                 :             :                                            NULL, NULL,
    8437                 :             :                                            SLP_INSTANCE_TREE (instance),
    8438                 :             :                                            &cost_vec)))
    8439                 :             :         {
    8440                 :      210027 :           cost_vec.release ();
    8441                 :      210027 :           slp_tree node = SLP_INSTANCE_TREE (instance);
    8442                 :      210027 :           stmt_vec_info stmt_info;
    8443                 :      210027 :           if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
    8444                 :      142014 :             stmt_info = SLP_INSTANCE_ROOT_STMTS (instance)[0];
    8445                 :             :           else
    8446                 :       68013 :             stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
    8447                 :      210027 :           if (is_a <loop_vec_info> (vinfo))
    8448                 :             :             {
    8449                 :      113831 :               if (dump_enabled_p ())
    8450                 :        6552 :                 dump_printf_loc (MSG_NOTE, vect_location,
    8451                 :             :                                  "unsupported SLP instance starting from: %G",
    8452                 :             :                                  stmt_info->stmt);
    8453                 :      113831 :               return false;
    8454                 :             :             }
    8455                 :       96196 :           if (dump_enabled_p ())
    8456                 :         321 :             dump_printf_loc (MSG_NOTE, vect_location,
    8457                 :             :                              "removing SLP instance operations starting from: %G",
    8458                 :             :                              stmt_info->stmt);
    8459                 :       96196 :           vect_free_slp_instance (instance);
    8460                 :       96196 :           vinfo->slp_instances.ordered_remove (i);
    8461                 :     1420070 :           while (!visited_vec.is_empty ())
    8462                 :      291788 :             visited.remove (visited_vec.pop ());
    8463                 :             :         }
    8464                 :             :       else
    8465                 :             :         {
    8466                 :      839816 :           i++;
    8467                 :      839816 :           if (loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo))
    8468                 :             :             {
    8469                 :      179739 :               add_stmt_costs (loop_vinfo->vector_costs, &cost_vec);
    8470                 :      179739 :               cost_vec.release ();
    8471                 :             :             }
    8472                 :             :           else
    8473                 :             :             /* For BB vectorization remember the SLP graph entry
    8474                 :             :                cost for later.  */
    8475                 :      660077 :             instance->cost_vec = cost_vec;
    8476                 :             :         }
    8477                 :     1049843 :     }
    8478                 :             : 
    8479                 :             :   /* Now look for SLP instances with a root that are covered by other
    8480                 :             :      instances and remove them.  */
    8481                 :      431480 :   hash_set<stmt_vec_info> roots;
    8482                 :     1688706 :   for (i = 0; vinfo->slp_instances.iterate (i, &instance); ++i)
    8483                 :      843741 :     if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
    8484                 :       17995 :       roots.add (SLP_INSTANCE_ROOT_STMTS (instance)[0]);
    8485                 :      431480 :   if (!roots.is_empty ())
    8486                 :             :     {
    8487                 :        9654 :       visited.empty ();
    8488                 :       45328 :       for (i = 0; vinfo->slp_instances.iterate (i, &instance); ++i)
    8489                 :       35674 :         vect_slp_prune_covered_roots (SLP_INSTANCE_TREE (instance), roots,
    8490                 :             :                                       visited);
    8491                 :       45328 :       for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
    8492                 :       35674 :         if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ()
    8493                 :       17995 :             && !roots.contains (SLP_INSTANCE_ROOT_STMTS (instance)[0]))
    8494                 :             :           {
    8495                 :        1429 :             stmt_vec_info root = SLP_INSTANCE_ROOT_STMTS (instance)[0];
    8496                 :        1429 :             if (dump_enabled_p ())
    8497                 :          20 :               dump_printf_loc (MSG_NOTE, vect_location,
    8498                 :             :                                "removing SLP instance operations starting "
    8499                 :             :                                "from: %G", root->stmt);
    8500                 :        1429 :             vect_free_slp_instance (instance);
    8501                 :        1429 :             vinfo->slp_instances.ordered_remove (i);
    8502                 :             :           }
    8503                 :             :         else
    8504                 :       34245 :           ++i;
    8505                 :             :     }
    8506                 :             : 
    8507                 :             :   /* Compute vectorizable live stmts.  */
    8508                 :      431480 :   if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo))
    8509                 :      314290 :     vect_bb_slp_mark_live_stmts (bb_vinfo);
    8510                 :             : 
    8511                 :      862960 :   return !vinfo->slp_instances.is_empty ();
    8512                 :      976791 : }
    8513                 :             : 
    8514                 :             : /* Get the SLP instance leader from INSTANCE_LEADER thereby transitively
    8515                 :             :    closing the eventual chain.  */
    8516                 :             : 
    8517                 :             : static slp_instance
    8518                 :      699907 : get_ultimate_leader (slp_instance instance,
    8519                 :             :                      hash_map<slp_instance, slp_instance> &instance_leader)
    8520                 :             : {
    8521                 :      699907 :   auto_vec<slp_instance *, 8> chain;
    8522                 :      699907 :   slp_instance *tem;
    8523                 :      747379 :   while (*(tem = instance_leader.get (instance)) != instance)
    8524                 :             :     {
    8525                 :       47472 :       chain.safe_push (tem);
    8526                 :       47472 :       instance = *tem;
    8527                 :             :     }
    8528                 :      747379 :   while (!chain.is_empty ())
    8529                 :       47472 :     *chain.pop () = instance;
    8530                 :      699907 :   return instance;
    8531                 :      699907 : }
    8532                 :             : 
    8533                 :             : namespace {
    8534                 :             : /* Subroutine of vect_bb_partition_graph_r.  Map KEY to INSTANCE in
    8535                 :             :    KEY_TO_INSTANCE, making INSTANCE the leader of any previous mapping
    8536                 :             :    for KEY.  Return true if KEY was already in KEY_TO_INSTANCE.
    8537                 :             : 
    8538                 :             :    INSTANCE_LEADER is as for get_ultimate_leader.  */
    8539                 :             : 
    8540                 :             : template<typename T>
    8541                 :             : bool
    8542                 :     3157425 : vect_map_to_instance (slp_instance instance, T key,
    8543                 :             :                       hash_map<T, slp_instance> &key_to_instance,
    8544                 :             :                       hash_map<slp_instance, slp_instance> &instance_leader)
    8545                 :             : {
    8546                 :             :   bool existed_p;
    8547                 :     3157425 :   slp_instance &key_instance = key_to_instance.get_or_insert (key, &existed_p);
    8548                 :     3157425 :   if (!existed_p)
    8549                 :             :     ;
    8550                 :      115314 :   else if (key_instance != instance)
    8551                 :             :     {
    8552                 :             :       /* If we're running into a previously marked key make us the
    8553                 :             :          leader of the current ultimate leader.  This keeps the
    8554                 :             :          leader chain acyclic and works even when the current instance
    8555                 :             :          connects two previously independent graph parts.  */
    8556                 :       41259 :       slp_instance key_leader
    8557                 :       41259 :         = get_ultimate_leader (key_instance, instance_leader);
    8558                 :       41259 :       if (key_leader != instance)
    8559                 :       11320 :         instance_leader.put (key_leader, instance);
    8560                 :             :     }
    8561                 :     3157425 :   key_instance = instance;
    8562                 :     3157425 :   return existed_p;
    8563                 :             : }
    8564                 :             : }
    8565                 :             : 
    8566                 :             : /* Worker of vect_bb_partition_graph, recurse on NODE.  */
    8567                 :             : 
    8568                 :             : static void
    8569                 :      883827 : vect_bb_partition_graph_r (bb_vec_info bb_vinfo,
    8570                 :             :                            slp_instance instance, slp_tree node,
    8571                 :             :                            hash_map<stmt_vec_info, slp_instance> &stmt_to_instance,
    8572                 :             :                            hash_map<slp_tree, slp_instance> &node_to_instance,
    8573                 :             :                            hash_map<slp_instance, slp_instance> &instance_leader)
    8574                 :             : {
    8575                 :      883827 :   stmt_vec_info stmt_info;
    8576                 :      883827 :   unsigned i;
    8577                 :             : 
    8578                 :     3157425 :   FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
    8579                 :     2273598 :     if (stmt_info)
    8580                 :     2273598 :       vect_map_to_instance (instance, stmt_info, stmt_to_instance,
    8581                 :             :                             instance_leader);
    8582                 :             : 
    8583                 :      883827 :   if (vect_map_to_instance (instance, node, node_to_instance,
    8584                 :             :                             instance_leader))
    8585                 :      883827 :     return;
    8586                 :             : 
    8587                 :             :   slp_tree child;
    8588                 :     1690795 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    8589                 :      833275 :     if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
    8590                 :      225179 :       vect_bb_partition_graph_r (bb_vinfo, instance, child, stmt_to_instance,
    8591                 :             :                                  node_to_instance, instance_leader);
    8592                 :             : }
    8593                 :             : 
    8594                 :             : /* Partition the SLP graph into pieces that can be costed independently.  */
    8595                 :             : 
    8596                 :             : static void
    8597                 :      279466 : vect_bb_partition_graph (bb_vec_info bb_vinfo)
    8598                 :             : {
    8599                 :      279466 :   DUMP_VECT_SCOPE ("vect_bb_partition_graph");
    8600                 :             : 
    8601                 :             :   /* First walk the SLP graph assigning each involved scalar stmt a
    8602                 :             :      corresponding SLP graph entry and upon visiting a previously
    8603                 :             :      marked stmt, make the stmts leader the current SLP graph entry.  */
    8604                 :      279466 :   hash_map<stmt_vec_info, slp_instance> stmt_to_instance;
    8605                 :      279466 :   hash_map<slp_tree, slp_instance> node_to_instance;
    8606                 :      279466 :   hash_map<slp_instance, slp_instance> instance_leader;
    8607                 :      279466 :   slp_instance instance;
    8608                 :      938114 :   for (unsigned i = 0; bb_vinfo->slp_instances.iterate (i, &instance); ++i)
    8609                 :             :     {
    8610                 :      658648 :       instance_leader.put (instance, instance);
    8611                 :      658648 :       vect_bb_partition_graph_r (bb_vinfo,
    8612                 :             :                                  instance, SLP_INSTANCE_TREE (instance),
    8613                 :             :                                  stmt_to_instance, node_to_instance,
    8614                 :             :                                  instance_leader);
    8615                 :             :     }
    8616                 :             : 
    8617                 :             :   /* Then collect entries to each independent subgraph.  */
    8618                 :     1217580 :   for (unsigned i = 0; bb_vinfo->slp_instances.iterate (i, &instance); ++i)
    8619                 :             :     {
    8620                 :      658648 :       slp_instance leader = get_ultimate_leader (instance, instance_leader);
    8621                 :      658648 :       leader->subgraph_entries.safe_push (instance);
    8622                 :      658648 :       if (dump_enabled_p ()
    8623                 :      658648 :           && leader != instance)
    8624                 :          67 :         dump_printf_loc (MSG_NOTE, vect_location,
    8625                 :             :                          "instance %p is leader of %p\n",
    8626                 :             :                          (void *) leader, (void *) instance);
    8627                 :             :     }
    8628                 :      279466 : }
    8629                 :             : 
    8630                 :             : /* Compute the set of scalar stmts participating in internal and external
    8631                 :             :    nodes.  */
    8632                 :             : 
    8633                 :             : static void
    8634                 :     1478706 : vect_slp_gather_vectorized_scalar_stmts (vec_info *vinfo, slp_tree node,
    8635                 :             :                                          hash_set<slp_tree> &visited,
    8636                 :             :                                          hash_set<stmt_vec_info> &vstmts,
    8637                 :             :                                          hash_set<stmt_vec_info> &estmts)
    8638                 :             : {
    8639                 :     1478706 :   int i;
    8640                 :     1478706 :   stmt_vec_info stmt_info;
    8641                 :     1478706 :   slp_tree child;
    8642                 :             : 
    8643                 :     1478706 :   if (visited.add (node))
    8644                 :       26131 :     return;
    8645                 :             : 
    8646                 :     1452575 :   if (SLP_TREE_DEF_TYPE (node) == vect_internal_def)
    8647                 :             :     {
    8648                 :     3008713 :       FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
    8649                 :     2159853 :         if (stmt_info)
    8650                 :     2159853 :           vstmts.add (stmt_info);
    8651                 :             : 
    8652                 :     3009365 :       FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    8653                 :      823680 :         if (child)
    8654                 :      823680 :           vect_slp_gather_vectorized_scalar_stmts (vinfo, child, visited,
    8655                 :             :                                                    vstmts, estmts);
    8656                 :             :     }
    8657                 :             :   else
    8658                 :     3374117 :     for (tree def : SLP_TREE_SCALAR_OPS (node))
    8659                 :             :       {
    8660                 :     1563954 :         stmt_vec_info def_stmt = vinfo->lookup_def (def);
    8661                 :     1563954 :         if (def_stmt)
    8662                 :      251528 :           estmts.add (def_stmt);
    8663                 :             :       }
    8664                 :             : }
    8665                 :             : 
    8666                 :             : 
    8667                 :             : /* Compute the scalar cost of the SLP node NODE and its children
    8668                 :             :    and return it.  Do not account defs that are marked in LIFE and
    8669                 :             :    update LIFE according to uses of NODE.  */
    8670                 :             : 
    8671                 :             : static void
    8672                 :      874415 : vect_bb_slp_scalar_cost (vec_info *vinfo,
    8673                 :             :                          slp_tree node, vec<bool, va_heap> *life,
    8674                 :             :                          stmt_vector_for_cost *cost_vec,
    8675                 :             :                          hash_set<stmt_vec_info> &vectorized_scalar_stmts,
    8676                 :             :                          hash_set<slp_tree> &visited)
    8677                 :             : {
    8678                 :      874415 :   unsigned i;
    8679                 :      874415 :   stmt_vec_info stmt_info;
    8680                 :      874415 :   slp_tree child;
    8681                 :             : 
    8682                 :      874415 :   if (visited.add (node))
    8683                 :       25538 :     return;
    8684                 :             : 
    8685                 :     3008764 :   FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
    8686                 :             :     {
    8687                 :     2159887 :       ssa_op_iter op_iter;
    8688                 :     2159887 :       def_operand_p def_p;
    8689                 :             : 
    8690                 :     4319774 :       if (!stmt_info || (*life)[i])
    8691                 :       78005 :         continue;
    8692                 :             : 
    8693                 :     2139781 :       stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
    8694                 :     2139781 :       gimple *orig_stmt = orig_stmt_info->stmt;
    8695                 :             : 
    8696                 :             :       /* If there is a non-vectorized use of the defs then the scalar
    8697                 :             :          stmt is kept live in which case we do not account it or any
    8698                 :             :          required defs in the SLP children in the scalar cost.  This
    8699                 :             :          way we make the vectorization more costly when compared to
    8700                 :             :          the scalar cost.  */
    8701                 :     2139781 :       if (!STMT_VINFO_LIVE_P (stmt_info))
    8702                 :             :         {
    8703                 :     2073144 :           auto_vec<gimple *, 8> worklist;
    8704                 :     2073144 :           hash_set<gimple *> *worklist_visited = NULL;
    8705                 :     2073144 :           worklist.quick_push (orig_stmt);
    8706                 :     2077967 :           do
    8707                 :             :             {
    8708                 :     2077967 :               gimple *work_stmt = worklist.pop ();
    8709                 :     4559158 :               FOR_EACH_PHI_OR_STMT_DEF (def_p, work_stmt, op_iter, SSA_OP_DEF)
    8710                 :             :                 {
    8711                 :      416108 :                   imm_use_iterator use_iter;
    8712                 :      416108 :                   gimple *use_stmt;
    8713                 :     1042456 :                   FOR_EACH_IMM_USE_STMT (use_stmt, use_iter,
    8714                 :             :                                          DEF_FROM_PTR (def_p))
    8715                 :      639232 :                     if (!is_gimple_debug (use_stmt))
    8716                 :             :                       {
    8717                 :      458362 :                         stmt_vec_info use_stmt_info
    8718                 :      458362 :                           = vinfo->lookup_stmt (use_stmt);
    8719                 :      458362 :                         if (!use_stmt_info
    8720                 :      458362 :                             || !vectorized_scalar_stmts.contains (use_stmt_info))
    8721                 :             :                           {
    8722                 :       17777 :                             if (use_stmt_info
    8723                 :       16025 :                                 && STMT_VINFO_IN_PATTERN_P (use_stmt_info))
    8724                 :             :                               {
    8725                 :             :                                 /* For stmts participating in patterns we have
    8726                 :             :                                    to check its uses recursively.  */
    8727                 :        4893 :                                 if (!worklist_visited)
    8728                 :        3785 :                                   worklist_visited = new hash_set<gimple *> ();
    8729                 :        4893 :                                 if (!worklist_visited->add (use_stmt))
    8730                 :        4893 :                                   worklist.safe_push (use_stmt);
    8731                 :        4893 :                                 continue;
    8732                 :             :                               }
    8733                 :       12884 :                             (*life)[i] = true;
    8734                 :       12884 :                             goto next_lane;
    8735                 :             :                           }
    8736                 :      416108 :                       }
    8737                 :             :                 }
    8738                 :             :             }
    8739                 :     4130166 :           while (!worklist.is_empty ());
    8740                 :     2060260 : next_lane:
    8741                 :     2073144 :           if (worklist_visited)
    8742                 :        3785 :             delete worklist_visited;
    8743                 :     2073144 :           if ((*life)[i])
    8744                 :       12884 :             continue;
    8745                 :     2073144 :         }
    8746                 :             : 
    8747                 :             :       /* Count scalar stmts only once.  */
    8748                 :     2126897 :       if (gimple_visited_p (orig_stmt))
    8749                 :       22043 :         continue;
    8750                 :     2104854 :       gimple_set_visited (orig_stmt, true);
    8751                 :             : 
    8752                 :     2104854 :       vect_cost_for_stmt kind;
    8753                 :     2104854 :       if (STMT_VINFO_DATA_REF (orig_stmt_info))
    8754                 :             :         {
    8755                 :     1928734 :           data_reference_p dr = STMT_VINFO_DATA_REF (orig_stmt_info);
    8756                 :     1928734 :           tree base = get_base_address (DR_REF (dr));
    8757                 :             :           /* When the scalar access is to a non-global not address-taken
    8758                 :             :              decl that is not BLKmode assume we can access it with a single
    8759                 :             :              non-load/store instruction.  */
    8760                 :     1928734 :           if (DECL_P (base)
    8761                 :     1549838 :               && !is_global_var (base)
    8762                 :     1484154 :               && !TREE_ADDRESSABLE (base)
    8763                 :     2621312 :               && DECL_MODE (base) != BLKmode)
    8764                 :             :             kind = scalar_stmt;
    8765                 :     1671428 :           else if (DR_IS_READ (STMT_VINFO_DATA_REF (orig_stmt_info)))
    8766                 :             :             kind = scalar_load;
    8767                 :             :           else
    8768                 :     1455009 :             kind = scalar_store;
    8769                 :             :         }
    8770                 :      176120 :       else if (vect_nop_conversion_p (orig_stmt_info))
    8771                 :       14607 :         continue;
    8772                 :             :       /* For single-argument PHIs assume coalescing which means zero cost
    8773                 :             :          for the scalar and the vector PHIs.  This avoids artificially
    8774                 :             :          favoring the vector path (but may pessimize it in some cases).  */
    8775                 :      161513 :       else if (is_a <gphi *> (orig_stmt_info->stmt)
    8776                 :      161513 :                && gimple_phi_num_args
    8777                 :       87064 :                     (as_a <gphi *> (orig_stmt_info->stmt)) == 1)
    8778                 :        8365 :         continue;
    8779                 :             :       else
    8780                 :             :         kind = scalar_stmt;
    8781                 :     2081882 :       record_stmt_cost (cost_vec, 1, kind, orig_stmt_info,
    8782                 :             :                         SLP_TREE_VECTYPE (node), 0, vect_body);
    8783                 :             :     }
    8784                 :             : 
    8785                 :     1697754 :   auto_vec<bool, 20> subtree_life;
    8786                 :     2405700 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
    8787                 :             :     {
    8788                 :      823704 :       if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
    8789                 :             :         {
    8790                 :             :           /* Do not directly pass LIFE to the recursive call, copy it to
    8791                 :             :              confine changes in the callee to the current child/subtree.  */
    8792                 :      219389 :           if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
    8793                 :             :             {
    8794                 :        3130 :               subtree_life.safe_grow_cleared (SLP_TREE_LANES (child), true);
    8795                 :       11096 :               for (unsigned j = 0;
    8796                 :       11096 :                    j < SLP_TREE_LANE_PERMUTATION (node).length (); ++j)
    8797                 :             :                 {
    8798                 :        7966 :                   auto perm = SLP_TREE_LANE_PERMUTATION (node)[j];
    8799                 :        7966 :                   if (perm.first == i)
    8800                 :        4170 :                     subtree_life[perm.second] = (*life)[j];
    8801                 :             :                 }
    8802                 :             :             }
    8803                 :             :           else
    8804                 :             :             {
    8805                 :      216259 :               gcc_assert (SLP_TREE_LANES (node) == SLP_TREE_LANES (child));
    8806                 :      216259 :               subtree_life.safe_splice (*life);
    8807                 :             :             }
    8808                 :      219389 :           vect_bb_slp_scalar_cost (vinfo, child, &subtree_life, cost_vec,
    8809                 :             :                                    vectorized_scalar_stmts, visited);
    8810                 :      219389 :           subtree_life.truncate (0);
    8811                 :             :         }
    8812                 :             :     }
    8813                 :             : }
    8814                 :             : 
    8815                 :             : /* Comparator for the loop-index sorted cost vectors.  */
    8816                 :             : 
    8817                 :             : static int
    8818                 :    16898777 : li_cost_vec_cmp (const void *a_, const void *b_)
    8819                 :             : {
    8820                 :    16898777 :   auto *a = (const std::pair<unsigned, stmt_info_for_cost *> *)a_;
    8821                 :    16898777 :   auto *b = (const std::pair<unsigned, stmt_info_for_cost *> *)b_;
    8822                 :    16898777 :   if (a->first < b->first)
    8823                 :             :     return -1;
    8824                 :    16315182 :   else if (a->first == b->first)
    8825                 :    15803271 :     return 0;
    8826                 :             :   return 1;
    8827                 :             : }
    8828                 :             : 
    8829                 :             : /* Check if vectorization of the basic block is profitable for the
    8830                 :             :    subgraph denoted by SLP_INSTANCES.  */
    8831                 :             : 
    8832                 :             : static bool
    8833                 :      643837 : vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo,
    8834                 :             :                                     vec<slp_instance> slp_instances,
    8835                 :             :                                     loop_p orig_loop)
    8836                 :             : {
    8837                 :      643837 :   slp_instance instance;
    8838                 :      643837 :   int i;
    8839                 :      643837 :   unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
    8840                 :      643837 :   unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
    8841                 :             : 
    8842                 :      643837 :   if (dump_enabled_p ())
    8843                 :             :     {
    8844                 :          82 :       dump_printf_loc (MSG_NOTE, vect_location, "Costing subgraph: \n");
    8845                 :          82 :       hash_set<slp_tree> visited;
    8846                 :         331 :       FOR_EACH_VEC_ELT (slp_instances, i, instance)
    8847                 :          85 :         vect_print_slp_graph (MSG_NOTE, vect_location,
    8848                 :             :                               SLP_INSTANCE_TREE (instance), visited);
    8849                 :          82 :     }
    8850                 :             : 
    8851                 :             :   /* Compute the set of scalar stmts we know will go away 'locally' when
    8852                 :             :      vectorizing.  This used to be tracked with just PURE_SLP_STMT but that's
    8853                 :             :      not accurate for nodes promoted extern late or for scalar stmts that
    8854                 :             :      are used both in extern defs and in vectorized defs.  */
    8855                 :      643837 :   hash_set<stmt_vec_info> vectorized_scalar_stmts;
    8856                 :      643837 :   hash_set<stmt_vec_info> scalar_stmts_in_externs;
    8857                 :      643837 :   hash_set<slp_tree> visited;
    8858                 :     1298863 :   FOR_EACH_VEC_ELT (slp_instances, i, instance)
    8859                 :             :     {
    8860                 :      655026 :       vect_slp_gather_vectorized_scalar_stmts (bb_vinfo,
    8861                 :             :                                                SLP_INSTANCE_TREE (instance),
    8862                 :             :                                                visited,
    8863                 :             :                                                vectorized_scalar_stmts,
    8864                 :             :                                                scalar_stmts_in_externs);
    8865                 :      718921 :       for (stmt_vec_info rstmt : SLP_INSTANCE_ROOT_STMTS (instance))
    8866                 :       34507 :         vectorized_scalar_stmts.add (rstmt);
    8867                 :             :     }
    8868                 :             :   /* Scalar stmts used as defs in external nodes need to be preseved, so
    8869                 :             :      remove them from vectorized_scalar_stmts.  */
    8870                 :      868150 :   for (stmt_vec_info stmt : scalar_stmts_in_externs)
    8871                 :      224313 :     vectorized_scalar_stmts.remove (stmt);
    8872                 :             : 
    8873                 :             :   /* Calculate scalar cost and sum the cost for the vector stmts
    8874                 :             :      previously collected.  */
    8875                 :      643837 :   stmt_vector_for_cost scalar_costs = vNULL;
    8876                 :      643837 :   stmt_vector_for_cost vector_costs = vNULL;
    8877                 :      643837 :   visited.empty ();
    8878                 :     1298863 :   FOR_EACH_VEC_ELT (slp_instances, i, instance)
    8879                 :             :     {
    8880                 :      655026 :       auto_vec<bool, 20> life;
    8881                 :      655026 :       life.safe_grow_cleared (SLP_TREE_LANES (SLP_INSTANCE_TREE (instance)),
    8882                 :             :                               true);
    8883                 :      655026 :       if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
    8884                 :       29388 :         record_stmt_cost (&scalar_costs,
    8885                 :       14694 :                           SLP_INSTANCE_ROOT_STMTS (instance).length (),
    8886                 :             :                           scalar_stmt,
    8887                 :       14694 :                           SLP_INSTANCE_ROOT_STMTS (instance)[0], 0, vect_body);
    8888                 :      655026 :       vect_bb_slp_scalar_cost (bb_vinfo,
    8889                 :             :                                SLP_INSTANCE_TREE (instance),
    8890                 :             :                                &life, &scalar_costs, vectorized_scalar_stmts,
    8891                 :             :                                visited);
    8892                 :      655026 :       vector_costs.safe_splice (instance->cost_vec);
    8893                 :      655026 :       instance->cost_vec.release ();
    8894                 :      655026 :     }
    8895                 :             : 
    8896                 :      643837 :   if (dump_enabled_p ())
    8897                 :          82 :     dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
    8898                 :             : 
    8899                 :             :   /* When costing non-loop vectorization we need to consider each covered
    8900                 :             :      loop independently and make sure vectorization is profitable.  For
    8901                 :             :      now we assume a loop may be not entered or executed an arbitrary
    8902                 :             :      number of iterations (???  static information can provide more
    8903                 :             :      precise info here) which means we can simply cost each containing
    8904                 :             :      loops stmts separately.  */
    8905                 :             : 
    8906                 :             :   /* First produce cost vectors sorted by loop index.  */
    8907                 :      643837 :   auto_vec<std::pair<unsigned, stmt_info_for_cost *> >
    8908                 :      643837 :     li_scalar_costs (scalar_costs.length ());
    8909                 :      643837 :   auto_vec<std::pair<unsigned, stmt_info_for_cost *> >
    8910                 :      643837 :     li_vector_costs (vector_costs.length ());
    8911                 :      643837 :   stmt_info_for_cost *cost;
    8912                 :     2740413 :   FOR_EACH_VEC_ELT (scalar_costs, i, cost)
    8913                 :             :     {
    8914                 :     2096576 :       unsigned l = gimple_bb (cost->stmt_info->stmt)->loop_father->num;
    8915                 :     2096576 :       li_scalar_costs.quick_push (std::make_pair (l, cost));
    8916                 :             :     }
    8917                 :             :   /* Use a random used loop as fallback in case the first vector_costs
    8918                 :             :      entry does not have a stmt_info associated with it.  */
    8919                 :      643837 :   unsigned l = li_scalar_costs[0].first;
    8920                 :     2384369 :   FOR_EACH_VEC_ELT (vector_costs, i, cost)
    8921                 :             :     {
    8922                 :             :       /* We inherit from the previous COST, invariants, externals and
    8923                 :             :          extracts immediately follow the cost for the related stmt.  */
    8924                 :     1740532 :       if (cost->stmt_info)
    8925                 :     1020644 :         l = gimple_bb (cost->stmt_info->stmt)->loop_father->num;
    8926                 :     1740532 :       li_vector_costs.quick_push (std::make_pair (l, cost));
    8927                 :             :     }
    8928                 :      643837 :   li_scalar_costs.qsort (li_cost_vec_cmp);
    8929                 :      643837 :   li_vector_costs.qsort (li_cost_vec_cmp);
    8930                 :             : 
    8931                 :             :   /* Now cost the portions individually.  */
    8932                 :             :   unsigned vi = 0;
    8933                 :             :   unsigned si = 0;
    8934                 :     1115170 :   bool profitable = true;
    8935                 :     1115170 :   while (si < li_scalar_costs.length ()
    8936                 :     1763096 :          && vi < li_vector_costs.length ())
    8937                 :             :     {
    8938                 :      647926 :       unsigned sl = li_scalar_costs[si].first;
    8939                 :      647926 :       unsigned vl = li_vector_costs[vi].first;
    8940                 :      647926 :       if (sl != vl)
    8941                 :             :         {
    8942                 :         135 :           if (dump_enabled_p ())
    8943                 :           0 :             dump_printf_loc (MSG_NOTE, vect_location,
    8944                 :             :                              "Scalar %d and vector %d loop part do not "
    8945                 :             :                              "match up, skipping scalar part\n", sl, vl);
    8946                 :             :           /* Skip the scalar part, assuming zero cost on the vector side.  */
    8947                 :         333 :           do
    8948                 :             :             {
    8949                 :         333 :               si++;
    8950                 :             :             }
    8951                 :         333 :           while (si < li_scalar_costs.length ()
    8952                 :         470 :                  && li_scalar_costs[si].first == sl);
    8953                 :         135 :           continue;
    8954                 :             :         }
    8955                 :             : 
    8956                 :      647791 :       class vector_costs *scalar_target_cost_data = init_cost (bb_vinfo, true);
    8957                 :     2082861 :       do
    8958                 :             :         {
    8959                 :     2082861 :           add_stmt_cost (scalar_target_cost_data, li_scalar_costs[si].second);
    8960                 :     2082861 :           si++;
    8961                 :             :         }
    8962                 :     2082861 :       while (si < li_scalar_costs.length ()
    8963                 :     4172898 :              && li_scalar_costs[si].first == sl);
    8964                 :      647791 :       scalar_target_cost_data->finish_cost (nullptr);
    8965                 :      647791 :       scalar_cost = scalar_target_cost_data->body_cost ();
    8966                 :             : 
    8967                 :             :       /* Complete the target-specific vector cost calculation.  */
    8968                 :      647791 :       class vector_costs *vect_target_cost_data = init_cost (bb_vinfo, false);
    8969                 :     1721548 :       do
    8970                 :             :         {
    8971                 :     1721548 :           add_stmt_cost (vect_target_cost_data, li_vector_costs[vi].second);
    8972                 :     1721548 :           vi++;
    8973                 :             :         }
    8974                 :     1721548 :       while (vi < li_vector_costs.length ()
    8975                 :     3450815 :              && li_vector_costs[vi].first == vl);
    8976                 :      647791 :       vect_target_cost_data->finish_cost (scalar_target_cost_data);
    8977                 :      647791 :       vec_prologue_cost = vect_target_cost_data->prologue_cost ();
    8978                 :      647791 :       vec_inside_cost = vect_target_cost_data->body_cost ();
    8979                 :      647791 :       vec_epilogue_cost = vect_target_cost_data->epilogue_cost ();
    8980                 :      647791 :       delete scalar_target_cost_data;
    8981                 :      647791 :       delete vect_target_cost_data;
    8982                 :             : 
    8983                 :      647791 :       vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
    8984                 :             : 
    8985                 :      647791 :       if (dump_enabled_p ())
    8986                 :             :         {
    8987                 :          88 :           dump_printf_loc (MSG_NOTE, vect_location,
    8988                 :             :                            "Cost model analysis for part in loop %d:\n", sl);
    8989                 :          88 :           dump_printf (MSG_NOTE, "  Vector cost: %d\n",
    8990                 :             :                        vec_inside_cost + vec_outside_cost);
    8991                 :          88 :           dump_printf (MSG_NOTE, "  Scalar cost: %d\n", scalar_cost);
    8992                 :             :         }
    8993                 :             : 
    8994                 :             :       /* Vectorization is profitable if its cost is more than the cost of scalar
    8995                 :             :          version.  Note that we err on the vector side for equal cost because
    8996                 :             :          the cost estimate is otherwise quite pessimistic (constant uses are
    8997                 :             :          free on the scalar side but cost a load on the vector side for
    8998                 :             :          example).  */
    8999                 :      647791 :       if (vec_outside_cost + vec_inside_cost > scalar_cost)
    9000                 :             :         {
    9001                 :             :           profitable = false;
    9002                 :             :           break;
    9003                 :             :         }
    9004                 :             :     }
    9005                 :     1111081 :   if (profitable && vi < li_vector_costs.length ())
    9006                 :             :     {
    9007                 :         575 :       if (dump_enabled_p ())
    9008                 :           0 :         dump_printf_loc (MSG_NOTE, vect_location,
    9009                 :             :                          "Excess vector cost for part in loop %d:\n",
    9010                 :           0 :                          li_vector_costs[vi].first);
    9011                 :             :       profitable = false;
    9012                 :             :     }
    9013                 :             : 
    9014                 :             :   /* Unset visited flag.  This is delayed when the subgraph is profitable
    9015                 :             :      and we process the loop for remaining unvectorized if-converted code.  */
    9016                 :      643837 :   if (!orig_loop || !profitable)
    9017                 :     2738956 :     FOR_EACH_VEC_ELT (scalar_costs, i, cost)
    9018                 :     2095246 :       gimple_set_visited  (cost->stmt_info->stmt, false);
    9019                 :             : 
    9020                 :      643837 :   scalar_costs.release ();
    9021                 :      643837 :   vector_costs.release ();
    9022                 :             : 
    9023                 :      643837 :   return profitable;
    9024                 :      643837 : }
    9025                 :             : 
    9026                 :             : /* qsort comparator for lane defs.  */
    9027                 :             : 
    9028                 :             : static int
    9029                 :          40 : vld_cmp (const void *a_, const void *b_)
    9030                 :             : {
    9031                 :          40 :   auto *a = (const std::pair<unsigned, tree> *)a_;
    9032                 :          40 :   auto *b = (const std::pair<unsigned, tree> *)b_;
    9033                 :          40 :   return a->first - b->first;
    9034                 :             : }
    9035                 :             : 
    9036                 :             : /* Return true if USE_STMT is a vector lane insert into VEC and set
    9037                 :             :    *THIS_LANE to the lane number that is set.  */
    9038                 :             : 
    9039                 :             : static bool
    9040                 :         240 : vect_slp_is_lane_insert (gimple *use_stmt, tree vec, unsigned *this_lane)
    9041                 :             : {
    9042                 :         240 :   gassign *use_ass = dyn_cast <gassign *> (use_stmt);
    9043                 :          91 :   if (!use_ass
    9044                 :          91 :       || gimple_assign_rhs_code (use_ass) != BIT_INSERT_EXPR
    9045                 :          22 :       || (vec
    9046                 :          22 :           ? gimple_assign_rhs1 (use_ass) != vec
    9047                 :          24 :           : ((vec = gimple_assign_rhs1 (use_ass)), false))
    9048                 :          46 :       || !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (vec)),
    9049                 :          46 :                                      TREE_TYPE (gimple_assign_rhs2 (use_ass)))
    9050                 :          46 :       || !constant_multiple_p
    9051                 :          46 :             (tree_to_poly_uint64 (gimple_assign_rhs3 (use_ass)),
    9052                 :          92 :              tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (TREE_TYPE (vec)))),
    9053                 :             :              this_lane))
    9054                 :         194 :     return false;
    9055                 :             :   return true;
    9056                 :             : }
    9057                 :             : 
    9058                 :             : /* Find any vectorizable constructors and add them to the grouped_store
    9059                 :             :    array.  */
    9060                 :             : 
    9061                 :             : static void
    9062                 :     2297833 : vect_slp_check_for_roots (bb_vec_info bb_vinfo)
    9063                 :             : {
    9064                 :    17109235 :   for (unsigned i = 0; i < bb_vinfo->nbbs; ++i)
    9065                 :    29622804 :     for (gimple_stmt_iterator gsi = gsi_start_bb (bb_vinfo->bbs[i]);
    9066                 :   116192481 :          !gsi_end_p (gsi); gsi_next (&gsi))
    9067                 :             :     {
    9068                 :   101381079 :       gassign *assign = dyn_cast<gassign *> (gsi_stmt (gsi));
    9069                 :             :       /* This can be used to start SLP discovery for early breaks for BB early breaks
    9070                 :             :          when we get that far.  */
    9071                 :   101381079 :       if (!assign)
    9072                 :   146835886 :         continue;
    9073                 :             : 
    9074                 :    28903269 :       tree rhs = gimple_assign_rhs1 (assign);
    9075                 :    28903269 :       enum tree_code code = gimple_assign_rhs_code (assign);
    9076                 :    28903269 :       use_operand_p use_p;
    9077                 :    28903269 :       gimple *use_stmt;
    9078                 :    28903269 :       if (code == CONSTRUCTOR)
    9079                 :             :         {
    9080                 :     1617913 :           if (!VECTOR_TYPE_P (TREE_TYPE (rhs))
    9081                 :       49505 :               || maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)),
    9082                 :       76492 :                            CONSTRUCTOR_NELTS (rhs))
    9083                 :       35224 :               || VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
    9084                 :     1653136 :               || uniform_vector_p (rhs))
    9085                 :     1610249 :             continue;
    9086                 :             : 
    9087                 :             :           unsigned j;
    9088                 :             :           tree val;
    9089                 :       45634 :           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), j, val)
    9090                 :       37970 :             if (TREE_CODE (val) != SSA_NAME
    9091                 :       37970 :                 || !bb_vinfo->lookup_def (val))
    9092                 :             :               break;
    9093                 :       19716 :           if (j != CONSTRUCTOR_NELTS (rhs))
    9094                 :        2194 :             continue;
    9095                 :             : 
    9096                 :        7664 :           vec<stmt_vec_info> roots = vNULL;
    9097                 :        7664 :           roots.safe_push (bb_vinfo->lookup_stmt (assign));
    9098                 :        7664 :           vec<stmt_vec_info> stmts;
    9099                 :        7664 :           stmts.create (CONSTRUCTOR_NELTS (rhs));
    9100                 :       49830 :           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), j, val)
    9101                 :       34502 :             stmts.quick_push
    9102                 :       34502 :               (vect_stmt_to_vectorize (bb_vinfo->lookup_def (val)));
    9103                 :        7664 :           bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_ctor,
    9104                 :        7664 :                                                stmts, roots));
    9105                 :             :         }
    9106                 :    27285356 :       else if (code == BIT_INSERT_EXPR
    9107                 :         813 :                && VECTOR_TYPE_P (TREE_TYPE (rhs))
    9108                 :         550 :                && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)).is_constant ()
    9109                 :         550 :                && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)).to_constant () > 1
    9110                 :         550 :                && integer_zerop (gimple_assign_rhs3 (assign))
    9111                 :         312 :                && useless_type_conversion_p
    9112                 :         312 :                     (TREE_TYPE (TREE_TYPE (rhs)),
    9113                 :         312 :                      TREE_TYPE (gimple_assign_rhs2 (assign)))
    9114                 :    27285920 :                && bb_vinfo->lookup_def (gimple_assign_rhs2 (assign)))
    9115                 :             :         {
    9116                 :             :           /* We start to match on insert to lane zero but since the
    9117                 :             :              inserts need not be ordered we'd have to search both
    9118                 :             :              the def and the use chains.  */
    9119                 :         211 :           tree vectype = TREE_TYPE (rhs);
    9120                 :         211 :           unsigned nlanes = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
    9121                 :         211 :           auto_vec<std::pair<unsigned, tree> > lane_defs (nlanes);
    9122                 :         211 :           auto_sbitmap lanes (nlanes);
    9123                 :         211 :           bitmap_clear (lanes);
    9124                 :         211 :           bitmap_set_bit (lanes, 0);
    9125                 :         211 :           tree def = gimple_assign_lhs (assign);
    9126                 :         211 :           lane_defs.quick_push
    9127                 :         211 :                       (std::make_pair (0, gimple_assign_rhs2 (assign)));
    9128                 :         211 :           unsigned lanes_found = 1;
    9129                 :             :           /* Start with the use chains, the last stmt will be the root.  */
    9130                 :         211 :           stmt_vec_info last = bb_vinfo->lookup_stmt (assign);
    9131                 :         211 :           vec<stmt_vec_info> roots = vNULL;
    9132                 :         211 :           roots.safe_push (last);
    9133                 :         213 :           do
    9134                 :             :             {
    9135                 :         213 :               use_operand_p use_p;
    9136                 :         213 :               gimple *use_stmt;
    9137                 :         213 :               if (!single_imm_use (def, &use_p, &use_stmt))
    9138                 :             :                 break;
    9139                 :         207 :               unsigned this_lane;
    9140                 :         207 :               if (!bb_vinfo->lookup_stmt (use_stmt)
    9141                 :         207 :                   || !vect_slp_is_lane_insert (use_stmt, def, &this_lane)
    9142                 :         229 :                   || !bb_vinfo->lookup_def (gimple_assign_rhs2 (use_stmt)))
    9143                 :             :                 break;
    9144                 :          22 :               if (bitmap_bit_p (lanes, this_lane))
    9145                 :             :                 break;
    9146                 :           2 :               lanes_found++;
    9147                 :           2 :               bitmap_set_bit (lanes, this_lane);
    9148                 :           2 :               gassign *use_ass = as_a <gassign *> (use_stmt);
    9149                 :           2 :               lane_defs.quick_push (std::make_pair
    9150                 :           2 :                                      (this_lane, gimple_assign_rhs2 (use_ass)));
    9151                 :           2 :               last = bb_vinfo->lookup_stmt (use_ass);
    9152                 :           2 :               roots.safe_push (last);
    9153                 :           2 :               def = gimple_assign_lhs (use_ass);
    9154                 :             :             }
    9155                 :           2 :           while (lanes_found < nlanes);
    9156                 :         211 :           if (roots.length () > 1)
    9157                 :           2 :             std::swap(roots[0], roots[roots.length () - 1]);
    9158                 :         211 :           if (lanes_found < nlanes)
    9159                 :             :             {
    9160                 :             :               /* Now search the def chain.  */
    9161                 :         211 :               def = gimple_assign_rhs1 (assign);
    9162                 :         213 :               do
    9163                 :             :                 {
    9164                 :         213 :                   if (TREE_CODE (def) != SSA_NAME
    9165                 :         213 :                       || !has_single_use (def))
    9166                 :             :                     break;
    9167                 :          56 :                   gimple *def_stmt = SSA_NAME_DEF_STMT (def);
    9168                 :          56 :                   unsigned this_lane;
    9169                 :          56 :                   if (!bb_vinfo->lookup_stmt (def_stmt)
    9170                 :          33 :                       || !vect_slp_is_lane_insert (def_stmt,
    9171                 :             :                                                    NULL_TREE, &this_lane)
    9172                 :          80 :                       || !bb_vinfo->lookup_def (gimple_assign_rhs2 (def_stmt)))
    9173                 :             :                     break;
    9174                 :          24 :                   if (bitmap_bit_p (lanes, this_lane))
    9175                 :             :                     break;
    9176                 :           4 :                   lanes_found++;
    9177                 :           4 :                   bitmap_set_bit (lanes, this_lane);
    9178                 :           8 :                   lane_defs.quick_push (std::make_pair
    9179                 :           4 :                                           (this_lane,
    9180                 :           4 :                                            gimple_assign_rhs2 (def_stmt)));
    9181                 :           4 :                   roots.safe_push (bb_vinfo->lookup_stmt (def_stmt));
    9182                 :           4 :                   def = gimple_assign_rhs1 (def_stmt);
    9183                 :             :                 }
    9184                 :           4 :               while (lanes_found < nlanes);
    9185                 :             :             }
    9186                 :         211 :           if (lanes_found == nlanes)
    9187                 :             :             {
    9188                 :             :               /* Sort lane_defs after the lane index and register the root.  */
    9189                 :           2 :               lane_defs.qsort (vld_cmp);
    9190                 :           2 :               vec<stmt_vec_info> stmts;
    9191                 :           2 :               stmts.create (nlanes);
    9192                 :          10 :               for (unsigned i = 0; i < nlanes; ++i)
    9193                 :           8 :                 stmts.quick_push (bb_vinfo->lookup_def (lane_defs[i].second));
    9194                 :           2 :               bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_ctor,
    9195                 :           2 :                                                    stmts, roots));
    9196                 :             :             }
    9197                 :             :           else
    9198                 :         209 :             roots.release ();
    9199                 :         211 :         }
    9200                 :    27285145 :       else if (!VECTOR_TYPE_P (TREE_TYPE (rhs))
    9201                 :    26427477 :                && (associative_tree_code (code) || code == MINUS_EXPR)
    9202                 :             :                /* ???  This pessimizes a two-element reduction.  PR54400.
    9203                 :             :                   ???  In-order reduction could be handled if we only
    9204                 :             :                   traverse one operand chain in vect_slp_linearize_chain.  */
    9205                 :    30828830 :                && !needs_fold_left_reduction_p (TREE_TYPE (rhs), code)
    9206                 :             :                /* Ops with constants at the tail can be stripped here.  */
    9207                 :     5294392 :                && TREE_CODE (rhs) == SSA_NAME
    9208                 :     5242971 :                && TREE_CODE (gimple_assign_rhs2 (assign)) == SSA_NAME
    9209                 :             :                /* Should be the chain end.  */
    9210                 :    29412067 :                && (!single_imm_use (gimple_assign_lhs (assign),
    9211                 :             :                                     &use_p, &use_stmt)
    9212                 :     1638845 :                    || !is_gimple_assign (use_stmt)
    9213                 :     1092914 :                    || (gimple_assign_rhs_code (use_stmt) != code
    9214                 :      795497 :                        && ((code != PLUS_EXPR && code != MINUS_EXPR)
    9215                 :      431826 :                            || (gimple_assign_rhs_code (use_stmt)
    9216                 :      431826 :                                != (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR))))))
    9217                 :             :         {
    9218                 :             :           /* We start the match at the end of a possible association
    9219                 :             :              chain.  */
    9220                 :     1750707 :           auto_vec<chain_op_t> chain;
    9221                 :     1750707 :           auto_vec<std::pair<tree_code, gimple *> > worklist;
    9222                 :     1750707 :           auto_vec<gimple *> chain_stmts;
    9223                 :     1750707 :           gimple *code_stmt = NULL, *alt_code_stmt = NULL;
    9224                 :     1750707 :           if (code == MINUS_EXPR)
    9225                 :      280808 :             code = PLUS_EXPR;
    9226                 :     1750707 :           internal_fn reduc_fn;
    9227                 :     2020724 :           if (!reduction_fn_for_scalar_code (code, &reduc_fn)
    9228                 :     1750707 :               || reduc_fn == IFN_LAST)
    9229                 :      270017 :             continue;
    9230                 :     1480690 :           vect_slp_linearize_chain (bb_vinfo, worklist, chain, code, assign,
    9231                 :             :                                     /* ??? */
    9232                 :             :                                     code_stmt, alt_code_stmt, &chain_stmts);
    9233                 :     2961380 :           if (chain.length () > 1)
    9234                 :             :             {
    9235                 :             :               /* Sort the chain according to def_type and operation.  */
    9236                 :     1480690 :               chain.sort (dt_sort_cmp, bb_vinfo);
    9237                 :             :               /* ???  Now we'd want to strip externals and constants
    9238                 :             :                  but record those to be handled in the epilogue.  */
    9239                 :             :               /* ???  For now do not allow mixing ops or externs/constants.  */
    9240                 :     1480690 :               bool invalid = false;
    9241                 :     1480690 :               unsigned remain_cnt = 0;
    9242                 :     1480690 :               unsigned last_idx = 0;
    9243                 :     4479308 :               for (unsigned i = 0; i < chain.length (); ++i)
    9244                 :             :                 {
    9245                 :     3299359 :                   if (chain[i].code != code)
    9246                 :             :                     {
    9247                 :             :                       invalid = true;
    9248                 :             :                       break;
    9249                 :             :                     }
    9250                 :     2998618 :                   if (chain[i].dt != vect_internal_def
    9251                 :             :                       /* Avoid stmts where the def is not the LHS, like
    9252                 :             :                          ASMs.  */
    9253                 :     5785338 :                       || (gimple_get_lhs (bb_vinfo->lookup_def
    9254                 :     2786720 :                                                       (chain[i].op)->stmt)
    9255                 :     2786720 :                           != chain[i].op))
    9256                 :      214790 :                     remain_cnt++;
    9257                 :             :                   else
    9258                 :             :                     last_idx = i;
    9259                 :             :                 }
    9260                 :             :               /* Make sure to have an even number of lanes as we later do
    9261                 :             :                  all-or-nothing discovery, not trying to split further.  */
    9262                 :     1480690 :               if ((chain.length () - remain_cnt) & 1)
    9263                 :      171782 :                 remain_cnt++;
    9264                 :     1480690 :               if (!invalid && chain.length () - remain_cnt > 1)
    9265                 :             :                 {
    9266                 :     1117294 :                   vec<stmt_vec_info> stmts;
    9267                 :     1117294 :                   vec<tree> remain = vNULL;
    9268                 :     1117294 :                   stmts.create (chain.length ());
    9269                 :     1117294 :                   if (remain_cnt > 0)
    9270                 :       99738 :                     remain.create (remain_cnt);
    9271                 :     3593349 :                   for (unsigned i = 0; i < chain.length (); ++i)
    9272                 :             :                     {
    9273                 :     2476055 :                       stmt_vec_info stmt_info;
    9274                 :     2476055 :                       if (chain[i].dt == vect_internal_def
    9275                 :     2443699 :                           && ((stmt_info = bb_vinfo->lookup_def (chain[i].op)),
    9276                 :     2443699 :                               gimple_get_lhs (stmt_info->stmt) == chain[i].op)
    9277                 :     4919670 :                           && (i != last_idx
    9278                 :     1117294 :                               || (stmts.length () & 1)))
    9279                 :     2365814 :                         stmts.quick_push (stmt_info);
    9280                 :             :                       else
    9281                 :      110241 :                         remain.quick_push (chain[i].op);
    9282                 :             :                     }
    9283                 :     1117294 :                   vec<stmt_vec_info> roots;
    9284                 :     1117294 :                   roots.create (chain_stmts.length ());
    9285                 :     2476055 :                   for (unsigned i = 0; i < chain_stmts.length (); ++i)
    9286                 :     1358761 :                     roots.quick_push (bb_vinfo->lookup_stmt (chain_stmts[i]));
    9287                 :     1117294 :                   bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_bb_reduc,
    9288                 :     1117294 :                                                        stmts, roots, remain));
    9289                 :             :                 }
    9290                 :             :             }
    9291                 :     1750707 :         }
    9292                 :             :     }
    9293                 :     2297833 : }
    9294                 :             : 
    9295                 :             : /* Walk the grouped store chains and replace entries with their
    9296                 :             :    pattern variant if any.  */
    9297                 :             : 
    9298                 :             : static void
    9299                 :      595902 : vect_fixup_store_groups_with_patterns (vec_info *vinfo)
    9300                 :             : {
    9301                 :      595902 :   stmt_vec_info first_element;
    9302                 :      595902 :   unsigned i;
    9303                 :             : 
    9304                 :     1425572 :   FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
    9305                 :             :     {
    9306                 :             :       /* We also have CTORs in this array.  */
    9307                 :      829670 :       if (!STMT_VINFO_GROUPED_ACCESS (first_element))
    9308                 :           0 :         continue;
    9309                 :      829670 :       if (STMT_VINFO_IN_PATTERN_P (first_element))
    9310                 :             :         {
    9311                 :         238 :           stmt_vec_info orig = first_element;
    9312                 :         238 :           first_element = STMT_VINFO_RELATED_STMT (first_element);
    9313                 :         238 :           DR_GROUP_FIRST_ELEMENT (first_element) = first_element;
    9314                 :         238 :           DR_GROUP_SIZE (first_element) = DR_GROUP_SIZE (orig);
    9315                 :         238 :           DR_GROUP_GAP (first_element) = DR_GROUP_GAP (orig);
    9316                 :         238 :           DR_GROUP_NEXT_ELEMENT (first_element) = DR_GROUP_NEXT_ELEMENT (orig);
    9317                 :         238 :           vinfo->grouped_stores[i] = first_element;
    9318                 :             :         }
    9319                 :      829670 :       stmt_vec_info prev = first_element;
    9320                 :     2305995 :       while (DR_GROUP_NEXT_ELEMENT (prev))
    9321                 :             :         {
    9322                 :     1476325 :           stmt_vec_info elt = DR_GROUP_NEXT_ELEMENT (prev);
    9323                 :     1476325 :           if (STMT_VINFO_IN_PATTERN_P (elt))
    9324                 :             :             {
    9325                 :         855 :               stmt_vec_info orig = elt;
    9326                 :         855 :               elt = STMT_VINFO_RELATED_STMT (elt);
    9327                 :         855 :               DR_GROUP_NEXT_ELEMENT (prev) = elt;
    9328                 :         855 :               DR_GROUP_GAP (elt) = DR_GROUP_GAP (orig);
    9329                 :         855 :               DR_GROUP_NEXT_ELEMENT (elt) = DR_GROUP_NEXT_ELEMENT (orig);
    9330                 :             :             }
    9331                 :     1476325 :           DR_GROUP_FIRST_ELEMENT (elt) = first_element;
    9332                 :     1476325 :           prev = elt;
    9333                 :             :         }
    9334                 :             :     }
    9335                 :      595902 : }
    9336                 :             : 
    9337                 :             : /* Check if the region described by BB_VINFO can be vectorized, returning
    9338                 :             :    true if so.  When returning false, set FATAL to true if the same failure
    9339                 :             :    would prevent vectorization at other vector sizes, false if it is still
    9340                 :             :    worth trying other sizes.  N_STMTS is the number of statements in the
    9341                 :             :    region.  */
    9342                 :             : 
    9343                 :             : static bool
    9344                 :     2297833 : vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int n_stmts, bool &fatal,
    9345                 :             :                        vec<int> *dataref_groups)
    9346                 :             : {
    9347                 :     2297833 :   DUMP_VECT_SCOPE ("vect_slp_analyze_bb");
    9348                 :             : 
    9349                 :     2297833 :   slp_instance instance;
    9350                 :     2297833 :   int i;
    9351                 :     2297833 :   poly_uint64 min_vf = 2;
    9352                 :             : 
    9353                 :             :   /* The first group of checks is independent of the vector size.  */
    9354                 :     2297833 :   fatal = true;
    9355                 :             : 
    9356                 :             :   /* Analyze the data references.  */
    9357                 :             : 
    9358                 :     2297833 :   if (!vect_analyze_data_refs (bb_vinfo, &min_vf, NULL))
    9359                 :             :     {
    9360                 :           0 :       if (dump_enabled_p ())
    9361                 :           0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9362                 :             :                          "not vectorized: unhandled data-ref in basic "
    9363                 :             :                          "block.\n");
    9364                 :           0 :       return false;
    9365                 :             :     }
    9366                 :             : 
    9367                 :     2297833 :   if (!vect_analyze_data_ref_accesses (bb_vinfo, dataref_groups))
    9368                 :             :     {
    9369                 :           0 :      if (dump_enabled_p ())
    9370                 :           0 :        dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9371                 :             :                         "not vectorized: unhandled data access in "
    9372                 :             :                         "basic block.\n");
    9373                 :           0 :       return false;
    9374                 :             :     }
    9375                 :             : 
    9376                 :     2297833 :   vect_slp_check_for_roots (bb_vinfo);
    9377                 :             : 
    9378                 :             :   /* If there are no grouped stores and no constructors in the region
    9379                 :             :      there is no need to continue with pattern recog as vect_analyze_slp
    9380                 :             :      will fail anyway.  */
    9381                 :     2297833 :   if (bb_vinfo->grouped_stores.is_empty ()
    9382                 :     1958644 :       && bb_vinfo->roots.is_empty ())
    9383                 :             :     {
    9384                 :     1701931 :       if (dump_enabled_p ())
    9385                 :        1000 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9386                 :             :                          "not vectorized: no grouped stores in "
    9387                 :             :                          "basic block.\n");
    9388                 :     1701931 :       return false;
    9389                 :             :     }
    9390                 :             : 
    9391                 :             :   /* While the rest of the analysis below depends on it in some way.  */
    9392                 :      595902 :   fatal = false;
    9393                 :             : 
    9394                 :      595902 :   vect_pattern_recog (bb_vinfo);
    9395                 :             : 
    9396                 :             :   /* Update store groups from pattern processing.  */
    9397                 :      595902 :   vect_fixup_store_groups_with_patterns (bb_vinfo);
    9398                 :             : 
    9399                 :             :   /* Check the SLP opportunities in the basic block, analyze and build SLP
    9400                 :             :      trees.  */
    9401                 :      595902 :   if (!vect_analyze_slp (bb_vinfo, n_stmts, false))
    9402                 :             :     {
    9403                 :           0 :       if (dump_enabled_p ())
    9404                 :             :         {
    9405                 :           0 :           dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9406                 :             :                            "Failed to SLP the basic block.\n");
    9407                 :           0 :           dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9408                 :             :                            "not vectorized: failed to find SLP opportunities "
    9409                 :             :                            "in basic block.\n");
    9410                 :             :         }
    9411                 :           0 :       return false;
    9412                 :             :     }
    9413                 :             : 
    9414                 :             :   /* Optimize permutations.  */
    9415                 :      595902 :   vect_optimize_slp (bb_vinfo);
    9416                 :             : 
    9417                 :             :   /* Gather the loads reachable from the SLP graph entries.  */
    9418                 :      595902 :   vect_gather_slp_loads (bb_vinfo);
    9419                 :             : 
    9420                 :      595902 :   vect_record_base_alignments (bb_vinfo);
    9421                 :             : 
    9422                 :             :   /* Analyze and verify the alignment of data references and the
    9423                 :             :      dependence in the SLP instances.  */
    9424                 :     1358265 :   for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); )
    9425                 :             :     {
    9426                 :      762363 :       vect_location = instance->location ();
    9427                 :      762363 :       if (! vect_slp_analyze_instance_alignment (bb_vinfo, instance)
    9428                 :      762363 :           || ! vect_slp_analyze_instance_dependence (bb_vinfo, instance))
    9429                 :             :         {
    9430                 :        6090 :           slp_tree node = SLP_INSTANCE_TREE (instance);
    9431                 :        6090 :           stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
    9432                 :        6090 :           if (dump_enabled_p ())
    9433                 :           6 :             dump_printf_loc (MSG_NOTE, vect_location,
    9434                 :             :                              "removing SLP instance operations starting from: %G",
    9435                 :             :                              stmt_info->stmt);
    9436                 :        6090 :           vect_free_slp_instance (instance);
    9437                 :        6090 :           BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i);
    9438                 :        6090 :           continue;
    9439                 :        6090 :         }
    9440                 :             : 
    9441                 :             :       /* Mark all the statements that we want to vectorize as pure SLP and
    9442                 :             :          relevant.  */
    9443                 :      756273 :       vect_mark_slp_stmts (bb_vinfo, SLP_INSTANCE_TREE (instance));
    9444                 :      756273 :       vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
    9445                 :      756273 :       unsigned j;
    9446                 :      756273 :       stmt_vec_info root;
    9447                 :             :       /* Likewise consider instance root stmts as vectorized.  */
    9448                 :     1654788 :       FOR_EACH_VEC_ELT (SLP_INSTANCE_ROOT_STMTS (instance), j, root)
    9449                 :      142242 :         STMT_SLP_TYPE (root) = pure_slp;
    9450                 :             : 
    9451                 :      756273 :       i++;
    9452                 :             :     }
    9453                 :     2332657 :   if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ())
    9454                 :             :     return false;
    9455                 :             : 
    9456                 :      314290 :   if (!vect_slp_analyze_operations (bb_vinfo))
    9457                 :             :     {
    9458                 :       34824 :       if (dump_enabled_p ())
    9459                 :          93 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9460                 :             :                          "not vectorized: bad operation in basic block.\n");
    9461                 :       34824 :       return false;
    9462                 :             :     }
    9463                 :             : 
    9464                 :      279466 :   vect_bb_partition_graph (bb_vinfo);
    9465                 :             : 
    9466                 :      279466 :   return true;
    9467                 :             : }
    9468                 :             : 
    9469                 :             : /* Subroutine of vect_slp_bb.  Try to vectorize the statements for all
    9470                 :             :    basic blocks in BBS, returning true on success.
    9471                 :             :    The region has N_STMTS statements and has the datarefs given by DATAREFS.  */
    9472                 :             : 
    9473                 :             : static bool
    9474                 :     2004560 : vect_slp_region (vec<basic_block> bbs, vec<data_reference_p> datarefs,
    9475                 :             :                  vec<int> *dataref_groups, unsigned int n_stmts,
    9476                 :             :                  loop_p orig_loop)
    9477                 :             : {
    9478                 :     2004560 :   bb_vec_info bb_vinfo;
    9479                 :     2004560 :   auto_vector_modes vector_modes;
    9480                 :             : 
    9481                 :             :   /* Autodetect first vector size we try.  */
    9482                 :     2004560 :   machine_mode next_vector_mode = VOIDmode;
    9483                 :     2004560 :   targetm.vectorize.autovectorize_vector_modes (&vector_modes, false);
    9484                 :     2004560 :   unsigned int mode_i = 0;
    9485                 :             : 
    9486                 :     2004560 :   vec_info_shared shared;
    9487                 :             : 
    9488                 :     2004560 :   machine_mode autodetected_vector_mode = VOIDmode;
    9489                 :     2591106 :   while (1)
    9490                 :             :     {
    9491                 :     2297833 :       bool vectorized = false;
    9492                 :     2297833 :       bool fatal = false;
    9493                 :     2297833 :       bb_vinfo = new _bb_vec_info (bbs, &shared);
    9494                 :             : 
    9495                 :     2297833 :       bool first_time_p = shared.datarefs.is_empty ();
    9496                 :     2297833 :       BB_VINFO_DATAREFS (bb_vinfo) = datarefs;
    9497                 :     2297833 :       if (first_time_p)
    9498                 :     2027028 :         bb_vinfo->shared->save_datarefs ();
    9499                 :             :       else
    9500                 :      270805 :         bb_vinfo->shared->check_datarefs ();
    9501                 :     2297833 :       bb_vinfo->vector_mode = next_vector_mode;
    9502                 :             : 
    9503                 :     2297833 :       if (vect_slp_analyze_bb_1 (bb_vinfo, n_stmts, fatal, dataref_groups))
    9504                 :             :         {
    9505                 :      279466 :           if (dump_enabled_p ())
    9506                 :             :             {
    9507                 :        1430 :               dump_printf_loc (MSG_NOTE, vect_location,
    9508                 :             :                                "***** Analysis succeeded with vector mode"
    9509                 :         715 :                                " %s\n", GET_MODE_NAME (bb_vinfo->vector_mode));
    9510                 :         715 :               dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB part\n");
    9511                 :             :             }
    9512                 :             : 
    9513                 :      279466 :           bb_vinfo->shared->check_datarefs ();
    9514                 :             : 
    9515                 :      279466 :           bool force_clear = false;
    9516                 :      279466 :           auto_vec<slp_instance> profitable_subgraphs;
    9517                 :     1497046 :           for (slp_instance instance : BB_VINFO_SLP_INSTANCES (bb_vinfo))
    9518                 :             :             {
    9519                 :      658648 :               if (instance->subgraph_entries.is_empty ())
    9520                 :      199808 :                 continue;
    9521                 :             : 
    9522                 :      647328 :               dump_user_location_t saved_vect_location = vect_location;
    9523                 :      647328 :               vect_location = instance->location ();
    9524                 :      647328 :               if (!unlimited_cost_model (NULL)
    9525                 :     1291165 :                   && !vect_bb_vectorization_profitable_p
    9526                 :      643837 :                         (bb_vinfo, instance->subgraph_entries, orig_loop))
    9527                 :             :                 {
    9528                 :      177168 :                   if (dump_enabled_p ())
    9529                 :          14 :                     dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9530                 :             :                                      "not vectorized: vectorization is not "
    9531                 :             :                                      "profitable.\n");
    9532                 :      177168 :                   vect_location = saved_vect_location;
    9533                 :      177168 :                   continue;
    9534                 :             :                 }
    9535                 :             : 
    9536                 :      470160 :               vect_location = saved_vect_location;
    9537                 :      470160 :               if (!dbg_cnt (vect_slp))
    9538                 :             :                 {
    9539                 :           0 :                   force_clear = true;
    9540                 :           0 :                   continue;
    9541                 :             :                 }
    9542                 :             : 
    9543                 :      470160 :               profitable_subgraphs.safe_push (instance);
    9544                 :             :             }
    9545                 :             : 
    9546                 :             :           /* When we're vectorizing an if-converted loop body make sure
    9547                 :             :              we vectorized all if-converted code.  */
    9548                 :      455535 :           if ((!profitable_subgraphs.is_empty () || force_clear) && orig_loop)
    9549                 :             :             {
    9550                 :         125 :               gcc_assert (bb_vinfo->nbbs == 1);
    9551                 :         250 :               for (gimple_stmt_iterator gsi = gsi_start_bb (bb_vinfo->bbs[0]);
    9552                 :        5244 :                    !gsi_end_p (gsi); gsi_next (&gsi))
    9553                 :             :                 {
    9554                 :             :                   /* The costing above left us with DCEable vectorized scalar
    9555                 :             :                      stmts having the visited flag set on profitable
    9556                 :             :                      subgraphs.  Do the delayed clearing of the flag here.  */
    9557                 :        5119 :                   if (gimple_visited_p (gsi_stmt (gsi)))
    9558                 :             :                     {
    9559                 :        1304 :                       gimple_set_visited (gsi_stmt (gsi), false);
    9560                 :        1304 :                       continue;
    9561                 :             :                     }
    9562                 :        3815 :                   if (flag_vect_cost_model == VECT_COST_MODEL_UNLIMITED)
    9563                 :         868 :                     continue;
    9564                 :             : 
    9565                 :        7538 :                   if (gassign *ass = dyn_cast <gassign *> (gsi_stmt (gsi)))
    9566                 :        3100 :                     if (gimple_assign_rhs_code (ass) == COND_EXPR)
    9567                 :             :                       {
    9568                 :         142 :                         if (!profitable_subgraphs.is_empty ()
    9569                 :          48 :                             && dump_enabled_p ())
    9570                 :           0 :                           dump_printf_loc (MSG_NOTE, vect_location,
    9571                 :             :                                            "not profitable because of "
    9572                 :             :                                            "unprofitable if-converted scalar "
    9573                 :             :                                            "code\n");
    9574                 :          94 :                         profitable_subgraphs.truncate (0);
    9575                 :             :                       }
    9576                 :             :                 }
    9577                 :             :             }
    9578                 :             : 
    9579                 :             :           /* Finally schedule the profitable subgraphs.  */
    9580                 :     1101694 :           for (slp_instance instance : profitable_subgraphs)
    9581                 :             :             {
    9582                 :      470090 :               if (!vectorized && dump_enabled_p ())
    9583                 :         702 :                 dump_printf_loc (MSG_NOTE, vect_location,
    9584                 :             :                                  "Basic block will be vectorized "
    9585                 :             :                                  "using SLP\n");
    9586                 :      470090 :               vectorized = true;
    9587                 :             : 
    9588                 :             :               /* Dump before scheduling as store vectorization will remove
    9589                 :             :                  the original stores and mess with the instance tree
    9590                 :             :                  so querying its location will eventually ICE.  */
    9591                 :      470090 :               if (flag_checking)
    9592                 :     1886684 :                 for (slp_instance sub : instance->subgraph_entries)
    9593                 :      476414 :                   gcc_assert (SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub)));
    9594                 :      470090 :               unsigned HOST_WIDE_INT bytes;
    9595                 :      470090 :               if (dump_enabled_p ())
    9596                 :        3327 :                 for (slp_instance sub : instance->subgraph_entries)
    9597                 :             :                   {
    9598                 :         882 :                     tree vtype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub));
    9599                 :        1764 :                     if (GET_MODE_SIZE (TYPE_MODE (vtype)).is_constant (&bytes))
    9600                 :         882 :                       dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
    9601                 :         882 :                                        sub->location (),
    9602                 :             :                                        "basic block part vectorized using %wu "
    9603                 :             :                                        "byte vectors\n", bytes);
    9604                 :             :                     else
    9605                 :             :                       dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
    9606                 :             :                                        sub->location (),
    9607                 :             :                                        "basic block part vectorized using "
    9608                 :             :                                        "variable length vectors\n");
    9609                 :             :                   }
    9610                 :             : 
    9611                 :      470090 :               dump_user_location_t saved_vect_location = vect_location;
    9612                 :      470090 :               vect_location = instance->location ();
    9613                 :             : 
    9614                 :      470090 :               vect_schedule_slp (bb_vinfo, instance->subgraph_entries);
    9615                 :             : 
    9616                 :      470090 :               vect_location = saved_vect_location;
    9617                 :             :             }
    9618                 :             : 
    9619                 :             : 
    9620                 :             :           /* Generate the invariant statements.  */
    9621                 :      279466 :           if (!gimple_seq_empty_p (bb_vinfo->inv_pattern_def_seq))
    9622                 :             :             {
    9623                 :          45 :               if (dump_enabled_p ())
    9624                 :           0 :                 dump_printf_loc (MSG_NOTE, vect_location,
    9625                 :             :                          "------>generating invariant statements\n");
    9626                 :             : 
    9627                 :          45 :               bb_vinfo->insert_seq_on_entry (NULL,
    9628                 :             :                                              bb_vinfo->inv_pattern_def_seq);
    9629                 :             :             }
    9630                 :      279466 :         }
    9631                 :             :       else
    9632                 :             :         {
    9633                 :     2018367 :           if (dump_enabled_p ())
    9634                 :        1359 :             dump_printf_loc (MSG_NOTE, vect_location,
    9635                 :             :                              "***** Analysis failed with vector mode %s\n",
    9636                 :        1359 :                              GET_MODE_NAME (bb_vinfo->vector_mode));
    9637                 :             :         }
    9638                 :             : 
    9639                 :     2297833 :       if (mode_i == 0)
    9640                 :     2004560 :         autodetected_vector_mode = bb_vinfo->vector_mode;
    9641                 :             : 
    9642                 :     2297833 :       if (!fatal)
    9643                 :     3232476 :         while (mode_i < vector_modes.length ()
    9644                 :     1742584 :                && vect_chooses_same_modes_p (bb_vinfo, vector_modes[mode_i]))
    9645                 :             :           {
    9646                 :      338741 :             if (dump_enabled_p ())
    9647                 :        1646 :               dump_printf_loc (MSG_NOTE, vect_location,
    9648                 :             :                                "***** The result for vector mode %s would"
    9649                 :             :                                " be the same\n",
    9650                 :         823 :                                GET_MODE_NAME (vector_modes[mode_i]));
    9651                 :      338741 :             mode_i += 1;
    9652                 :             :           }
    9653                 :             : 
    9654                 :     2297833 :       delete bb_vinfo;
    9655                 :             : 
    9656                 :     2297833 :       if (mode_i < vector_modes.length ()
    9657                 :     2133000 :           && VECTOR_MODE_P (autodetected_vector_mode)
    9658                 :     1994138 :           && (related_vector_mode (vector_modes[mode_i],
    9659                 :             :                                    GET_MODE_INNER (autodetected_vector_mode))
    9660                 :      997069 :               == autodetected_vector_mode)
    9661                 :     4430833 :           && (related_vector_mode (autodetected_vector_mode,
    9662                 :      527392 :                                    GET_MODE_INNER (vector_modes[mode_i]))
    9663                 :     1054784 :               == vector_modes[mode_i]))
    9664                 :             :         {
    9665                 :      527392 :           if (dump_enabled_p ())
    9666                 :         232 :             dump_printf_loc (MSG_NOTE, vect_location,
    9667                 :             :                              "***** Skipping vector mode %s, which would"
    9668                 :             :                              " repeat the analysis for %s\n",
    9669                 :         232 :                              GET_MODE_NAME (vector_modes[mode_i]),
    9670                 :         232 :                              GET_MODE_NAME (autodetected_vector_mode));
    9671                 :      527392 :           mode_i += 1;
    9672                 :             :         }
    9673                 :             : 
    9674                 :     2297833 :       if (vectorized
    9675                 :     2121812 :           || mode_i == vector_modes.length ()
    9676                 :     1957073 :           || autodetected_vector_mode == VOIDmode
    9677                 :             :           /* If vect_slp_analyze_bb_1 signaled that analysis for all
    9678                 :             :              vector sizes will fail do not bother iterating.  */
    9679                 :     3118975 :           || fatal)
    9680                 :     4009120 :         return vectorized;
    9681                 :             : 
    9682                 :             :       /* Try the next biggest vector size.  */
    9683                 :      293273 :       next_vector_mode = vector_modes[mode_i++];
    9684                 :      293273 :       if (dump_enabled_p ())
    9685                 :         255 :         dump_printf_loc (MSG_NOTE, vect_location,
    9686                 :             :                          "***** Re-trying analysis with vector mode %s\n",
    9687                 :         255 :                          GET_MODE_NAME (next_vector_mode));
    9688                 :      293273 :     }
    9689                 :     2004560 : }
    9690                 :             : 
    9691                 :             : 
    9692                 :             : /* Main entry for the BB vectorizer.  Analyze and transform BBS, returns
    9693                 :             :    true if anything in the basic-block was vectorized.  */
    9694                 :             : 
    9695                 :             : static bool
    9696                 :     2004560 : vect_slp_bbs (const vec<basic_block> &bbs, loop_p orig_loop)
    9697                 :             : {
    9698                 :     2004560 :   vec<data_reference_p> datarefs = vNULL;
    9699                 :     2004560 :   auto_vec<int> dataref_groups;
    9700                 :     2004560 :   int insns = 0;
    9701                 :     2004560 :   int current_group = 0;
    9702                 :             : 
    9703                 :    12284787 :   for (unsigned i = 0; i < bbs.length (); i++)
    9704                 :             :     {
    9705                 :    10280227 :       basic_block bb = bbs[i];
    9706                 :    78536450 :       for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
    9707                 :    68256223 :            gsi_next (&gsi))
    9708                 :             :         {
    9709                 :    68256223 :           gimple *stmt = gsi_stmt (gsi);
    9710                 :    68256223 :           if (is_gimple_debug (stmt))
    9711                 :    40199152 :             continue;
    9712                 :             : 
    9713                 :    28057071 :           insns++;
    9714                 :             : 
    9715                 :    28057071 :           if (gimple_location (stmt) != UNKNOWN_LOCATION)
    9716                 :    25522306 :             vect_location = stmt;
    9717                 :             : 
    9718                 :    28057071 :           if (!vect_find_stmt_data_reference (NULL, stmt, &datarefs,
    9719                 :    28057071 :                                               &dataref_groups, current_group))
    9720                 :     4883430 :             ++current_group;
    9721                 :             :         }
    9722                 :             :       /* New BBs always start a new DR group.  */
    9723                 :    10280227 :       ++current_group;
    9724                 :             :     }
    9725                 :             : 
    9726                 :     2004560 :   return vect_slp_region (bbs, datarefs, &dataref_groups, insns, orig_loop);
    9727                 :     2004560 : }
    9728                 :             : 
    9729                 :             : /* Special entry for the BB vectorizer.  Analyze and transform a single
    9730                 :             :    if-converted BB with ORIG_LOOPs body being the not if-converted
    9731                 :             :    representation.  Returns true if anything in the basic-block was
    9732                 :             :    vectorized.  */
    9733                 :             : 
    9734                 :             : bool
    9735                 :       16421 : vect_slp_if_converted_bb (basic_block bb, loop_p orig_loop)
    9736                 :             : {
    9737                 :       16421 :   auto_vec<basic_block> bbs;
    9738                 :       16421 :   bbs.safe_push (bb);
    9739                 :       16421 :   return vect_slp_bbs (bbs, orig_loop);
    9740                 :       16421 : }
    9741                 :             : 
    9742                 :             : /* Main entry for the BB vectorizer.  Analyze and transform BB, returns
    9743                 :             :    true if anything in the basic-block was vectorized.  */
    9744                 :             : 
    9745                 :             : bool
    9746                 :      872849 : vect_slp_function (function *fun)
    9747                 :             : {
    9748                 :      872849 :   bool r = false;
    9749                 :      872849 :   int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
    9750                 :      872849 :   auto_bitmap exit_bbs;
    9751                 :      872849 :   bitmap_set_bit (exit_bbs, EXIT_BLOCK);
    9752                 :      872849 :   edge entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fun));
    9753                 :      872849 :   unsigned n = rev_post_order_and_mark_dfs_back_seme (fun, entry, exit_bbs,
    9754                 :      872849 :                                                       true, rpo, NULL);
    9755                 :             : 
    9756                 :             :   /* For the moment split the function into pieces to avoid making
    9757                 :             :      the iteration on the vector mode moot.  Split at points we know
    9758                 :             :      to not handle well which is CFG merges (SLP discovery doesn't
    9759                 :             :      handle non-loop-header PHIs) and loop exits.  Since pattern
    9760                 :             :      recog requires reverse iteration to visit uses before defs
    9761                 :             :      simply chop RPO into pieces.  */
    9762                 :      872849 :   auto_vec<basic_block> bbs;
    9763                 :    11162242 :   for (unsigned i = 0; i < n; i++)
    9764                 :             :     {
    9765                 :    10289393 :       basic_block bb = BASIC_BLOCK_FOR_FN (fun, rpo[i]);
    9766                 :    10289393 :       bool split = false;
    9767                 :             : 
    9768                 :             :       /* Split when a BB is not dominated by the first block.  */
    9769                 :    19355147 :       if (!bbs.is_empty ()
    9770                 :     9065754 :           && !dominated_by_p (CDI_DOMINATORS, bb, bbs[0]))
    9771                 :             :         {
    9772                 :      779333 :           if (dump_enabled_p ())
    9773                 :         105 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9774                 :             :                              "splitting region at dominance boundary bb%d\n",
    9775                 :             :                              bb->index);
    9776                 :             :           split = true;
    9777                 :             :         }
    9778                 :             :       /* Split when the loop determined by the first block
    9779                 :             :          is exited.  This is because we eventually insert
    9780                 :             :          invariants at region begin.  */
    9781                 :    17796481 :       else if (!bbs.is_empty ()
    9782                 :     8286421 :                && bbs[0]->loop_father != bb->loop_father
    9783                 :     2074347 :                && !flow_loop_nested_p (bbs[0]->loop_father, bb->loop_father))
    9784                 :             :         {
    9785                 :        4918 :           if (dump_enabled_p ())
    9786                 :           6 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9787                 :             :                              "splitting region at loop %d exit at bb%d\n",
    9788                 :           3 :                              bbs[0]->loop_father->num, bb->index);
    9789                 :             :           split = true;
    9790                 :             :         }
    9791                 :     9505142 :       else if (!bbs.is_empty ()
    9792                 :     8281503 :                && bb->loop_father->header == bb
    9793                 :      435959 :                && bb->loop_father->dont_vectorize)
    9794                 :             :         {
    9795                 :        5836 :           if (dump_enabled_p ())
    9796                 :          66 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9797                 :             :                              "splitting region at dont-vectorize loop %d "
    9798                 :             :                              "entry at bb%d\n",
    9799                 :             :                              bb->loop_father->num, bb->index);
    9800                 :             :           split = true;
    9801                 :             :         }
    9802                 :             : 
    9803                 :    11079480 :       if (split && !bbs.is_empty ())
    9804                 :             :         {
    9805                 :      790087 :           r |= vect_slp_bbs (bbs, NULL);
    9806                 :      790087 :           bbs.truncate (0);
    9807                 :             :         }
    9808                 :             : 
    9809                 :    10289393 :       if (bbs.is_empty ())
    9810                 :             :         {
    9811                 :             :           /* We need to be able to insert at the head of the region which
    9812                 :             :              we cannot for region starting with a returns-twice call.  */
    9813                 :     2013726 :           if (gcall *first = safe_dyn_cast <gcall *> (first_stmt (bb)))
    9814                 :      380920 :             if (gimple_call_flags (first) & ECF_RETURNS_TWICE)
    9815                 :             :               {
    9816                 :         287 :                 if (dump_enabled_p ())
    9817                 :           2 :                   dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9818                 :             :                                    "skipping bb%d as start of region as it "
    9819                 :             :                                    "starts with returns-twice call\n",
    9820                 :             :                                    bb->index);
    9821                 :       25587 :                 continue;
    9822                 :             :               }
    9823                 :             :           /* If the loop this BB belongs to is marked as not to be vectorized
    9824                 :             :              honor that also for BB vectorization.  */
    9825                 :     2013439 :           if (bb->loop_father->dont_vectorize)
    9826                 :       25300 :             continue;
    9827                 :             :         }
    9828                 :             : 
    9829                 :    10263806 :       bbs.safe_push (bb);
    9830                 :             : 
    9831                 :             :       /* When we have a stmt ending this block and defining a
    9832                 :             :          value we have to insert on edges when inserting after it for
    9833                 :             :          a vector containing its definition.  Avoid this for now.  */
    9834                 :    20527612 :       if (gimple *last = *gsi_last_bb (bb))
    9835                 :     8156996 :         if (gimple_get_lhs (last)
    9836                 :     8156996 :             && is_ctrl_altering_stmt (last))
    9837                 :             :           {
    9838                 :      325210 :             if (dump_enabled_p ())
    9839                 :           2 :               dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9840                 :             :                                "splitting region at control altering "
    9841                 :             :                                "definition %G", last);
    9842                 :      325210 :             r |= vect_slp_bbs (bbs, NULL);
    9843                 :      325210 :             bbs.truncate (0);
    9844                 :             :           }
    9845                 :             :     }
    9846                 :             : 
    9847                 :      872849 :   if (!bbs.is_empty ())
    9848                 :      872842 :     r |= vect_slp_bbs (bbs, NULL);
    9849                 :             : 
    9850                 :      872849 :   free (rpo);
    9851                 :             : 
    9852                 :      872849 :   return r;
    9853                 :      872849 : }
    9854                 :             : 
    9855                 :             : /* Build a variable-length vector in which the elements in ELTS are repeated
    9856                 :             :    to a fill NRESULTS vectors of type VECTOR_TYPE.  Store the vectors in
    9857                 :             :    RESULTS and add any new instructions to SEQ.
    9858                 :             : 
    9859                 :             :    The approach we use is:
    9860                 :             : 
    9861                 :             :    (1) Find a vector mode VM with integer elements of mode IM.
    9862                 :             : 
    9863                 :             :    (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
    9864                 :             :        ELTS' has mode IM.  This involves creating NELTS' VIEW_CONVERT_EXPRs
    9865                 :             :        from small vectors to IM.
    9866                 :             : 
    9867                 :             :    (3) Duplicate each ELTS'[I] into a vector of mode VM.
    9868                 :             : 
    9869                 :             :    (4) Use a tree of interleaving VEC_PERM_EXPRs to create VMs with the
    9870                 :             :        correct byte contents.
    9871                 :             : 
    9872                 :             :    (5) Use VIEW_CONVERT_EXPR to cast the final VMs to the required type.
    9873                 :             : 
    9874                 :             :    We try to find the largest IM for which this sequence works, in order
    9875                 :             :    to cut down on the number of interleaves.  */
    9876                 :             : 
    9877                 :             : void
    9878                 :           0 : duplicate_and_interleave (vec_info *vinfo, gimple_seq *seq, tree vector_type,
    9879                 :             :                           const vec<tree> &elts, unsigned int nresults,
    9880                 :             :                           vec<tree> &results)
    9881                 :             : {
    9882                 :           0 :   unsigned int nelts = elts.length ();
    9883                 :           0 :   tree element_type = TREE_TYPE (vector_type);
    9884                 :             : 
    9885                 :             :   /* (1) Find a vector mode VM with integer elements of mode IM.  */
    9886                 :           0 :   unsigned int nvectors = 1;
    9887                 :           0 :   tree new_vector_type;
    9888                 :           0 :   tree permutes[2];
    9889                 :           0 :   if (!can_duplicate_and_interleave_p (vinfo, nelts, element_type,
    9890                 :             :                                        &nvectors, &new_vector_type,
    9891                 :             :                                        permutes))
    9892                 :           0 :     gcc_unreachable ();
    9893                 :             : 
    9894                 :             :   /* Get a vector type that holds ELTS[0:NELTS/NELTS'].  */
    9895                 :           0 :   unsigned int partial_nelts = nelts / nvectors;
    9896                 :           0 :   tree partial_vector_type = build_vector_type (element_type, partial_nelts);
    9897                 :             : 
    9898                 :           0 :   tree_vector_builder partial_elts;
    9899                 :           0 :   auto_vec<tree, 32> pieces (nvectors * 2);
    9900                 :           0 :   pieces.quick_grow_cleared (nvectors * 2);
    9901                 :           0 :   for (unsigned int i = 0; i < nvectors; ++i)
    9902                 :             :     {
    9903                 :             :       /* (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
    9904                 :             :              ELTS' has mode IM.  */
    9905                 :           0 :       partial_elts.new_vector (partial_vector_type, partial_nelts, 1);
    9906                 :           0 :       for (unsigned int j = 0; j < partial_nelts; ++j)
    9907                 :           0 :         partial_elts.quick_push (elts[i * partial_nelts + j]);
    9908                 :           0 :       tree t = gimple_build_vector (seq, &partial_elts);
    9909                 :           0 :       t = gimple_build (seq, VIEW_CONVERT_EXPR,
    9910                 :           0 :                         TREE_TYPE (new_vector_type), t);
    9911                 :             : 
    9912                 :             :       /* (3) Duplicate each ELTS'[I] into a vector of mode VM.  */
    9913                 :           0 :       pieces[i] = gimple_build_vector_from_val (seq, new_vector_type, t);
    9914                 :             :     }
    9915                 :             : 
    9916                 :             :   /* (4) Use a tree of VEC_PERM_EXPRs to create a single VM with the
    9917                 :             :          correct byte contents.
    9918                 :             : 
    9919                 :             :      Conceptually, we need to repeat the following operation log2(nvectors)
    9920                 :             :      times, where hi_start = nvectors / 2:
    9921                 :             : 
    9922                 :             :         out[i * 2] = VEC_PERM_EXPR (in[i], in[i + hi_start], lo_permute);
    9923                 :             :         out[i * 2 + 1] = VEC_PERM_EXPR (in[i], in[i + hi_start], hi_permute);
    9924                 :             : 
    9925                 :             :      However, if each input repeats every N elements and the VF is
    9926                 :             :      a multiple of N * 2, the HI result is the same as the LO result.
    9927                 :             :      This will be true for the first N1 iterations of the outer loop,
    9928                 :             :      followed by N2 iterations for which both the LO and HI results
    9929                 :             :      are needed.  I.e.:
    9930                 :             : 
    9931                 :             :         N1 + N2 = log2(nvectors)
    9932                 :             : 
    9933                 :             :      Each "N1 iteration" doubles the number of redundant vectors and the
    9934                 :             :      effect of the process as a whole is to have a sequence of nvectors/2**N1
    9935                 :             :      vectors that repeats 2**N1 times.  Rather than generate these redundant
    9936                 :             :      vectors, we halve the number of vectors for each N1 iteration.  */
    9937                 :             :   unsigned int in_start = 0;
    9938                 :             :   unsigned int out_start = nvectors;
    9939                 :             :   unsigned int new_nvectors = nvectors;
    9940                 :           0 :   for (unsigned int in_repeat = 1; in_repeat < nvectors; in_repeat *= 2)
    9941                 :             :     {
    9942                 :           0 :       unsigned int hi_start = new_nvectors / 2;
    9943                 :           0 :       unsigned int out_i = 0;
    9944                 :           0 :       for (unsigned int in_i = 0; in_i < new_nvectors; ++in_i)
    9945                 :             :         {
    9946                 :           0 :           if ((in_i & 1) != 0
    9947                 :           0 :               && multiple_p (TYPE_VECTOR_SUBPARTS (new_vector_type),
    9948                 :             :                              2 * in_repeat))
    9949                 :           0 :             continue;
    9950                 :             : 
    9951                 :           0 :           tree output = make_ssa_name (new_vector_type);
    9952                 :           0 :           tree input1 = pieces[in_start + (in_i / 2)];
    9953                 :           0 :           tree input2 = pieces[in_start + (in_i / 2) + hi_start];
    9954                 :           0 :           gassign *stmt = gimple_build_assign (output, VEC_PERM_EXPR,
    9955                 :             :                                                input1, input2,
    9956                 :             :                                                permutes[in_i & 1]);
    9957                 :           0 :           gimple_seq_add_stmt (seq, stmt);
    9958                 :           0 :           pieces[out_start + out_i] = output;
    9959                 :           0 :           out_i += 1;
    9960                 :             :         }
    9961                 :           0 :       std::swap (in_start, out_start);
    9962                 :           0 :       new_nvectors = out_i;
    9963                 :             :     }
    9964                 :             : 
    9965                 :             :   /* (5) Use VIEW_CONVERT_EXPR to cast the final VM to the required type.  */
    9966                 :           0 :   results.reserve (nresults);
    9967                 :           0 :   for (unsigned int i = 0; i < nresults; ++i)
    9968                 :           0 :     if (i < new_nvectors)
    9969                 :           0 :       results.quick_push (gimple_build (seq, VIEW_CONVERT_EXPR, vector_type,
    9970                 :           0 :                                         pieces[in_start + i]));
    9971                 :             :     else
    9972                 :           0 :       results.quick_push (results[i - new_nvectors]);
    9973                 :           0 : }
    9974                 :             : 
    9975                 :             : 
    9976                 :             : /* For constant and loop invariant defs in OP_NODE this function creates
    9977                 :             :    vector defs that will be used in the vectorized stmts and stores them
    9978                 :             :    to SLP_TREE_VEC_DEFS of OP_NODE.  */
    9979                 :             : 
    9980                 :             : static void
    9981                 :      471079 : vect_create_constant_vectors (vec_info *vinfo, slp_tree op_node)
    9982                 :             : {
    9983                 :      471079 :   unsigned HOST_WIDE_INT nunits;
    9984                 :      471079 :   tree vec_cst;
    9985                 :      471079 :   unsigned j, number_of_places_left_in_vector;
    9986                 :      471079 :   tree vector_type;
    9987                 :      471079 :   tree vop;
    9988                 :      471079 :   int group_size = op_node->ops.length ();
    9989                 :      471079 :   unsigned int vec_num, i;
    9990                 :      471079 :   unsigned number_of_copies = 1;
    9991                 :      471079 :   bool constant_p;
    9992                 :      471079 :   gimple_seq ctor_seq = NULL;
    9993                 :      471079 :   auto_vec<tree, 16> permute_results;
    9994                 :             : 
    9995                 :             :   /* We always want SLP_TREE_VECTYPE (op_node) here correctly set.  */
    9996                 :      471079 :   vector_type = SLP_TREE_VECTYPE (op_node);
    9997                 :             : 
    9998                 :      471079 :   unsigned int number_of_vectors = SLP_TREE_NUMBER_OF_VEC_STMTS (op_node);
    9999                 :      471079 :   SLP_TREE_VEC_DEFS (op_node).create (number_of_vectors);
   10000                 :      471079 :   auto_vec<tree> voprnds (number_of_vectors);
   10001                 :             : 
   10002                 :             :   /* NUMBER_OF_COPIES is the number of times we need to use the same values in
   10003                 :             :      created vectors. It is greater than 1 if unrolling is performed.
   10004                 :             : 
   10005                 :             :      For example, we have two scalar operands, s1 and s2 (e.g., group of
   10006                 :             :      strided accesses of size two), while NUNITS is four (i.e., four scalars
   10007                 :             :      of this type can be packed in a vector).  The output vector will contain
   10008                 :             :      two copies of each scalar operand: {s1, s2, s1, s2}.  (NUMBER_OF_COPIES
   10009                 :             :      will be 2).
   10010                 :             : 
   10011                 :             :      If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
   10012                 :             :      containing the operands.
   10013                 :             : 
   10014                 :             :      For example, NUNITS is four as before, and the group size is 8
   10015                 :             :      (s1, s2, ..., s8).  We will create two vectors {s1, s2, s3, s4} and
   10016                 :             :      {s5, s6, s7, s8}.  */
   10017                 :             : 
   10018                 :             :   /* When using duplicate_and_interleave, we just need one element for
   10019                 :             :      each scalar statement.  */
   10020                 :      471079 :   if (!TYPE_VECTOR_SUBPARTS (vector_type).is_constant (&nunits))
   10021                 :             :     nunits = group_size;
   10022                 :             : 
   10023                 :      471079 :   number_of_copies = nunits * number_of_vectors / group_size;
   10024                 :             : 
   10025                 :      471079 :   number_of_places_left_in_vector = nunits;
   10026                 :      471079 :   constant_p = true;
   10027                 :      471079 :   tree uniform_elt = NULL_TREE;
   10028                 :      471079 :   tree_vector_builder elts (vector_type, nunits, 1);
   10029                 :      471079 :   elts.quick_grow (nunits);
   10030                 :      471079 :   stmt_vec_info insert_after = NULL;
   10031                 :     1309434 :   for (j = 0; j < number_of_copies; j++)
   10032                 :             :     {
   10033                 :      838355 :       tree op;
   10034                 :     3290860 :       for (i = group_size - 1; op_node->ops.iterate (i, &op); i--)
   10035                 :             :         {
   10036                 :             :           /* Create 'vect_ = {op0,op1,...,opn}'.  */
   10037                 :     1614150 :           tree orig_op = op;
   10038                 :     1614150 :           if (number_of_places_left_in_vector == nunits)
   10039                 :             :             uniform_elt = op;
   10040                 :     1028521 :           else if (uniform_elt && operand_equal_p (uniform_elt, op))
   10041                 :      619594 :             op = elts[number_of_places_left_in_vector];
   10042                 :             :           else
   10043                 :             :             uniform_elt = NULL_TREE;
   10044                 :     1614150 :           number_of_places_left_in_vector--;
   10045                 :     1614150 :           if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
   10046                 :             :             {
   10047                 :      274420 :               if (CONSTANT_CLASS_P (op))
   10048                 :             :                 {
   10049                 :       96359 :                   if (VECTOR_BOOLEAN_TYPE_P (vector_type))
   10050                 :             :                     {
   10051                 :             :                       /* Can't use VIEW_CONVERT_EXPR for booleans because
   10052                 :             :                          of possibly different sizes of scalar value and
   10053                 :             :                          vector element.  */
   10054                 :          76 :                       if (integer_zerop (op))
   10055                 :          76 :                         op = build_int_cst (TREE_TYPE (vector_type), 0);
   10056                 :           0 :                       else if (integer_onep (op))
   10057                 :           0 :                         op = build_all_ones_cst (TREE_TYPE (vector_type));
   10058                 :             :                       else
   10059                 :           0 :                         gcc_unreachable ();
   10060                 :             :                     }
   10061                 :             :                   else
   10062                 :       96283 :                     op = fold_unary (VIEW_CONVERT_EXPR,
   10063                 :             :                                      TREE_TYPE (vector_type), op);
   10064                 :       96359 :                   gcc_assert (op && CONSTANT_CLASS_P (op));
   10065                 :             :                 }
   10066                 :             :               else
   10067                 :             :                 {
   10068                 :      178061 :                   tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
   10069                 :      178061 :                   gimple *init_stmt;
   10070                 :      178061 :                   if (VECTOR_BOOLEAN_TYPE_P (vector_type))
   10071                 :             :                     {
   10072                 :         391 :                       tree true_val
   10073                 :         391 :                         = build_all_ones_cst (TREE_TYPE (vector_type));
   10074                 :         391 :                       tree false_val
   10075                 :         391 :                         = build_zero_cst (TREE_TYPE (vector_type));
   10076                 :         391 :                       gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op)));
   10077                 :         391 :                       init_stmt = gimple_build_assign (new_temp, COND_EXPR,
   10078                 :             :                                                        op, true_val,
   10079                 :             :                                                        false_val);
   10080                 :             :                     }
   10081                 :             :                   else
   10082                 :             :                     {
   10083                 :      177670 :                       op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type),
   10084                 :             :                                    op);
   10085                 :      177670 :                       init_stmt
   10086                 :      177670 :                         = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR,
   10087                 :             :                                                op);
   10088                 :             :                     }
   10089                 :      178061 :                   gimple_seq_add_stmt (&ctor_seq, init_stmt);
   10090                 :      178061 :                   op = new_temp;
   10091                 :             :                 }
   10092                 :             :             }
   10093                 :     1614150 :           elts[number_of_places_left_in_vector] = op;
   10094                 :     1614150 :           if (!CONSTANT_CLASS_P (op))
   10095                 :      326739 :             constant_p = false;
   10096                 :             :           /* For BB vectorization we have to compute an insert location
   10097                 :             :              when a def is inside the analyzed region since we cannot
   10098                 :             :              simply insert at the BB start in this case.  */
   10099                 :     1614150 :           stmt_vec_info opdef;
   10100                 :     1614150 :           if (TREE_CODE (orig_op) == SSA_NAME
   10101                 :      204177 :               && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
   10102                 :      181036 :               && is_a <bb_vec_info> (vinfo)
   10103                 :     1743503 :               && (opdef = vinfo->lookup_def (orig_op)))
   10104                 :             :             {
   10105                 :       82994 :               if (!insert_after)
   10106                 :             :                 insert_after = opdef;
   10107                 :             :               else
   10108                 :       46322 :                 insert_after = get_later_stmt (insert_after, opdef);
   10109                 :             :             }
   10110                 :             : 
   10111                 :     1614150 :           if (number_of_places_left_in_vector == 0)
   10112                 :             :             {
   10113                 :      585629 :               auto type_nunits = TYPE_VECTOR_SUBPARTS (vector_type);
   10114                 :      585629 :               if (uniform_elt)
   10115                 :      606450 :                 vec_cst = gimple_build_vector_from_val (&ctor_seq, vector_type,
   10116                 :      303225 :                                                         elts[0]);
   10117                 :      564808 :               else if (constant_p
   10118                 :      564808 :                        ? multiple_p (type_nunits, nunits)
   10119                 :      109274 :                        : known_eq (type_nunits, nunits))
   10120                 :      282404 :                 vec_cst = gimple_build_vector (&ctor_seq, &elts);
   10121                 :             :               else
   10122                 :             :                 {
   10123                 :           0 :                   if (permute_results.is_empty ())
   10124                 :           0 :                     duplicate_and_interleave (vinfo, &ctor_seq, vector_type,
   10125                 :             :                                               elts, number_of_vectors,
   10126                 :             :                                               permute_results);
   10127                 :           0 :                   vec_cst = permute_results[number_of_vectors - j - 1];
   10128                 :             :                 }
   10129                 :      585629 :               if (!gimple_seq_empty_p (ctor_seq))
   10130                 :             :                 {
   10131                 :      144407 :                   if (insert_after)
   10132                 :             :                     {
   10133                 :       36672 :                       gimple_stmt_iterator gsi;
   10134                 :       36672 :                       if (gimple_code (insert_after->stmt) == GIMPLE_PHI)
   10135                 :             :                         {
   10136                 :        3227 :                           gsi = gsi_after_labels (gimple_bb (insert_after->stmt));
   10137                 :        3227 :                           gsi_insert_seq_before (&gsi, ctor_seq,
   10138                 :             :                                                  GSI_CONTINUE_LINKING);
   10139                 :             :                         }
   10140                 :       33445 :                       else if (!stmt_ends_bb_p (insert_after->stmt))
   10141                 :             :                         {
   10142                 :       33445 :                           gsi = gsi_for_stmt (insert_after->stmt);
   10143                 :       33445 :                           gsi_insert_seq_after (&gsi, ctor_seq,
   10144                 :             :                                                 GSI_CONTINUE_LINKING);
   10145                 :             :                         }
   10146                 :             :                       else
   10147                 :             :                         {
   10148                 :             :                           /* When we want to insert after a def where the
   10149                 :             :                              defining stmt throws then insert on the fallthru
   10150                 :             :                              edge.  */
   10151                 :           0 :                           edge e = find_fallthru_edge
   10152                 :           0 :                                      (gimple_bb (insert_after->stmt)->succs);
   10153                 :           0 :                           basic_block new_bb
   10154                 :           0 :                             = gsi_insert_seq_on_edge_immediate (e, ctor_seq);
   10155                 :           0 :                           gcc_assert (!new_bb);
   10156                 :             :                         }
   10157                 :             :                     }
   10158                 :             :                   else
   10159                 :      107735 :                     vinfo->insert_seq_on_entry (NULL, ctor_seq);
   10160                 :      144407 :                   ctor_seq = NULL;
   10161                 :             :                 }
   10162                 :      585629 :               voprnds.quick_push (vec_cst);
   10163                 :      585629 :               insert_after = NULL;
   10164                 :      585629 :               number_of_places_left_in_vector = nunits;
   10165                 :      585629 :               constant_p = true;
   10166                 :      585629 :               elts.new_vector (vector_type, nunits, 1);
   10167                 :      585629 :               elts.quick_grow (nunits);
   10168                 :             :             }
   10169                 :             :         }
   10170                 :             :     }
   10171                 :             : 
   10172                 :             :   /* Since the vectors are created in the reverse order, we should invert
   10173                 :             :      them.  */
   10174                 :      471079 :   vec_num = voprnds.length ();
   10175                 :     1056708 :   for (j = vec_num; j != 0; j--)
   10176                 :             :     {
   10177                 :      585629 :       vop = voprnds[j - 1];
   10178                 :      585629 :       SLP_TREE_VEC_DEFS (op_node).quick_push (vop);
   10179                 :             :     }
   10180                 :             : 
   10181                 :             :   /* In case that VF is greater than the unrolling factor needed for the SLP
   10182                 :             :      group of stmts, NUMBER_OF_VECTORS to be created is greater than
   10183                 :             :      NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
   10184                 :             :      to replicate the vectors.  */
   10185                 :      471079 :   while (number_of_vectors > SLP_TREE_VEC_DEFS (op_node).length ())
   10186                 :      471079 :     for (i = 0; SLP_TREE_VEC_DEFS (op_node).iterate (i, &vop) && i < vec_num;
   10187                 :             :          i++)
   10188                 :           0 :       SLP_TREE_VEC_DEFS (op_node).quick_push (vop);
   10189                 :      471079 : }
   10190                 :             : 
   10191                 :             : /* Get the Ith vectorized definition from SLP_NODE.  */
   10192                 :             : 
   10193                 :             : tree
   10194                 :      129254 : vect_get_slp_vect_def (slp_tree slp_node, unsigned i)
   10195                 :             : {
   10196                 :      129254 :   return SLP_TREE_VEC_DEFS (slp_node)[i];
   10197                 :             : }
   10198                 :             : 
   10199                 :             : /* Get the vectorized definitions of SLP_NODE in *VEC_DEFS.  */
   10200                 :             : 
   10201                 :             : void
   10202                 :      867148 : vect_get_slp_defs (slp_tree slp_node, vec<tree> *vec_defs)
   10203                 :             : {
   10204                 :      867148 :   vec_defs->create (SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node));
   10205                 :      867148 :   vec_defs->splice (SLP_TREE_VEC_DEFS (slp_node));
   10206                 :      867148 : }
   10207                 :             : 
   10208                 :             : /* Get N vectorized definitions for SLP_NODE.  */
   10209                 :             : 
   10210                 :             : void
   10211                 :        2160 : vect_get_slp_defs (vec_info *,
   10212                 :             :                    slp_tree slp_node, vec<vec<tree> > *vec_oprnds, unsigned n)
   10213                 :             : {
   10214                 :        2160 :   if (n == -1U)
   10215                 :        2160 :     n = SLP_TREE_CHILDREN (slp_node).length ();
   10216                 :             : 
   10217                 :        7851 :   for (unsigned i = 0; i < n; ++i)
   10218                 :             :     {
   10219                 :        5691 :       slp_tree child = SLP_TREE_CHILDREN (slp_node)[i];
   10220                 :        5691 :       vec<tree> vec_defs = vNULL;
   10221                 :        5691 :       vect_get_slp_defs (child, &vec_defs);
   10222                 :        5691 :       vec_oprnds->quick_push (vec_defs);
   10223                 :             :     }
   10224                 :        2160 : }
   10225                 :             : 
   10226                 :             : /* A subroutine of vect_transform_slp_perm_load with two extra arguments:
   10227                 :             :    - PERM gives the permutation that the caller wants to use for NODE,
   10228                 :             :      which might be different from SLP_LOAD_PERMUTATION.
   10229                 :             :    - DUMP_P controls whether the function dumps information.  */
   10230                 :             : 
   10231                 :             : static bool
   10232                 :      127026 : vect_transform_slp_perm_load_1 (vec_info *vinfo, slp_tree node,
   10233                 :             :                                 load_permutation_t &perm,
   10234                 :             :                                 const vec<tree> &dr_chain,
   10235                 :             :                                 gimple_stmt_iterator *gsi, poly_uint64 vf,
   10236                 :             :                                 bool analyze_only, bool dump_p,
   10237                 :             :                                 unsigned *n_perms, unsigned int *n_loads,
   10238                 :             :                                 bool dce_chain)
   10239                 :             : {
   10240                 :      127026 :   stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
   10241                 :      127026 :   int vec_index = 0;
   10242                 :      127026 :   tree vectype = SLP_TREE_VECTYPE (node);
   10243                 :      127026 :   unsigned int group_size = SLP_TREE_SCALAR_STMTS (node).length ();
   10244                 :      127026 :   unsigned int mask_element;
   10245                 :      127026 :   unsigned dr_group_size;
   10246                 :      127026 :   machine_mode mode;
   10247                 :             : 
   10248                 :      127026 :   if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
   10249                 :             :     dr_group_size = 1;
   10250                 :             :   else
   10251                 :             :     {
   10252                 :      125417 :       stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
   10253                 :      125417 :       dr_group_size = DR_GROUP_SIZE (stmt_info);
   10254                 :             :     }
   10255                 :             : 
   10256                 :      127026 :   mode = TYPE_MODE (vectype);
   10257                 :      127026 :   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
   10258                 :      127026 :   unsigned int nstmts = SLP_TREE_NUMBER_OF_VEC_STMTS (node);
   10259                 :             : 
   10260                 :             :   /* Initialize the vect stmts of NODE to properly insert the generated
   10261                 :             :      stmts later.  */
   10262                 :      127026 :   if (! analyze_only)
   10263                 :       32289 :     for (unsigned i = SLP_TREE_VEC_DEFS (node).length (); i < nstmts; i++)
   10264                 :       12791 :       SLP_TREE_VEC_DEFS (node).quick_push (NULL_TREE);
   10265                 :             : 
   10266                 :             :   /* Generate permutation masks for every NODE. Number of masks for each NODE
   10267                 :             :      is equal to GROUP_SIZE.
   10268                 :             :      E.g., we have a group of three nodes with three loads from the same
   10269                 :             :      location in each node, and the vector size is 4. I.e., we have a
   10270                 :             :      a0b0c0a1b1c1... sequence and we need to create the following vectors:
   10271                 :             :      for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
   10272                 :             :      for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
   10273                 :             :      ...
   10274                 :             : 
   10275                 :             :      The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
   10276                 :             :      The last mask is illegal since we assume two operands for permute
   10277                 :             :      operation, and the mask element values can't be outside that range.
   10278                 :             :      Hence, the last mask must be converted into {2,5,5,5}.
   10279                 :             :      For the first two permutations we need the first and the second input
   10280                 :             :      vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
   10281                 :             :      we need the second and the third vectors: {b1,c1,a2,b2} and
   10282                 :             :      {c2,a3,b3,c3}.  */
   10283                 :             : 
   10284                 :      127026 :   int vect_stmts_counter = 0;
   10285                 :      127026 :   unsigned int index = 0;
   10286                 :      127026 :   int first_vec_index = -1;
   10287                 :      127026 :   int second_vec_index = -1;
   10288                 :      127026 :   bool noop_p = true;
   10289                 :      127026 :   *n_perms = 0;
   10290                 :             : 
   10291                 :      127026 :   vec_perm_builder mask;
   10292                 :      127026 :   unsigned int nelts_to_build;
   10293                 :      127026 :   unsigned int nvectors_per_build;
   10294                 :      127026 :   unsigned int in_nlanes;
   10295                 :      127026 :   bool repeating_p = (group_size == dr_group_size
   10296                 :      159827 :                       && multiple_p (nunits, group_size));
   10297                 :      127026 :   if (repeating_p)
   10298                 :             :     {
   10299                 :             :       /* A single vector contains a whole number of copies of the node, so:
   10300                 :             :          (a) all permutes can use the same mask; and
   10301                 :             :          (b) the permutes only need a single vector input.  */
   10302                 :       22724 :       mask.new_vector (nunits, group_size, 3);
   10303                 :       22724 :       nelts_to_build = mask.encoded_nelts ();
   10304                 :             :       /* It's possible to obtain zero nstmts during analyze_only, so make
   10305                 :             :          it at least one to ensure the later computation for n_perms
   10306                 :             :          proceed.  */
   10307                 :       22724 :       nvectors_per_build = nstmts > 0 ? nstmts : 1;
   10308                 :       22724 :       in_nlanes = dr_group_size * 3;
   10309                 :             :     }
   10310                 :             :   else
   10311                 :             :     {
   10312                 :             :       /* We need to construct a separate mask for each vector statement.  */
   10313                 :      104302 :       unsigned HOST_WIDE_INT const_nunits, const_vf;
   10314                 :      104302 :       if (!nunits.is_constant (&const_nunits)
   10315                 :      104302 :           || !vf.is_constant (&const_vf))
   10316                 :             :         return false;
   10317                 :      104302 :       mask.new_vector (const_nunits, const_nunits, 1);
   10318                 :      104302 :       nelts_to_build = const_vf * group_size;
   10319                 :      104302 :       nvectors_per_build = 1;
   10320                 :      104302 :       in_nlanes = const_vf * dr_group_size;
   10321                 :             :     }
   10322                 :      127026 :   auto_sbitmap used_in_lanes (in_nlanes);
   10323                 :      127026 :   bitmap_clear (used_in_lanes);
   10324                 :      127026 :   auto_bitmap used_defs;
   10325                 :             : 
   10326                 :      127026 :   unsigned int count = mask.encoded_nelts ();
   10327                 :      127026 :   mask.quick_grow (count);
   10328                 :      127026 :   vec_perm_indices indices;
   10329                 :             : 
   10330                 :      732188 :   for (unsigned int j = 0; j < nelts_to_build; j++)
   10331                 :             :     {
   10332                 :      610291 :       unsigned int iter_num = j / group_size;
   10333                 :      610291 :       unsigned int stmt_num = j % group_size;
   10334                 :      610291 :       unsigned int i = (iter_num * dr_group_size + perm[stmt_num]);
   10335                 :      610291 :       bitmap_set_bit (used_in_lanes, i);
   10336                 :      610291 :       if (repeating_p)
   10337                 :             :         {
   10338                 :             :           first_vec_index = 0;
   10339                 :             :           mask_element = i;
   10340                 :             :         }
   10341                 :             :       else
   10342                 :             :         {
   10343                 :             :           /* Enforced before the loop when !repeating_p.  */
   10344                 :      453187 :           unsigned int const_nunits = nunits.to_constant ();
   10345                 :      453187 :           vec_index = i / const_nunits;
   10346                 :      453187 :           mask_element = i % const_nunits;
   10347                 :      453187 :           if (vec_index == first_vec_index
   10348                 :      453187 :               || first_vec_index == -1)
   10349                 :             :             {
   10350                 :             :               first_vec_index = vec_index;
   10351                 :             :             }
   10352                 :      164614 :           else if (vec_index == second_vec_index
   10353                 :      164614 :                    || second_vec_index == -1)
   10354                 :             :             {
   10355                 :      162777 :               second_vec_index = vec_index;
   10356                 :      162777 :               mask_element += const_nunits;
   10357                 :             :             }
   10358                 :             :           else
   10359                 :             :             {
   10360                 :        1837 :               if (dump_p)
   10361                 :         153 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   10362                 :             :                                  "permutation requires at "
   10363                 :             :                                  "least three vectors %G",
   10364                 :             :                                  stmt_info->stmt);
   10365                 :        1837 :               gcc_assert (analyze_only);
   10366                 :             :               return false;
   10367                 :             :             }
   10368                 :             : 
   10369                 :      451350 :           gcc_assert (mask_element < 2 * const_nunits);
   10370                 :             :         }
   10371                 :             : 
   10372                 :      608454 :       if (mask_element != index)
   10373                 :      411249 :         noop_p = false;
   10374                 :      608454 :       mask[index++] = mask_element;
   10375                 :             : 
   10376                 :      608454 :       if (index == count)
   10377                 :             :         {
   10378                 :      173052 :           if (!noop_p)
   10379                 :             :             {
   10380                 :      204987 :               indices.new_vector (mask, second_vec_index == -1 ? 1 : 2, nunits);
   10381                 :      120378 :               if (!can_vec_perm_const_p (mode, mode, indices))
   10382                 :             :                 {
   10383                 :        3292 :                   if (dump_p)
   10384                 :             :                     {
   10385                 :          81 :                       dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   10386                 :             :                                        "unsupported vect permute { ");
   10387                 :         721 :                       for (i = 0; i < count; ++i)
   10388                 :             :                         {
   10389                 :         640 :                           dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
   10390                 :         640 :                           dump_printf (MSG_MISSED_OPTIMIZATION, " ");
   10391                 :             :                         }
   10392                 :          81 :                       dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
   10393                 :             :                     }
   10394                 :        3292 :                   gcc_assert (analyze_only);
   10395                 :             :                   return false;
   10396                 :             :                 }
   10397                 :             : 
   10398                 :      117086 :               tree mask_vec = NULL_TREE;
   10399                 :      117086 :               if (!analyze_only)
   10400                 :       11440 :                 mask_vec = vect_gen_perm_mask_checked (vectype, indices);
   10401                 :             : 
   10402                 :      117086 :               if (second_vec_index == -1)
   10403                 :       33772 :                 second_vec_index = first_vec_index;
   10404                 :             : 
   10405                 :      234776 :               for (unsigned int ri = 0; ri < nvectors_per_build; ++ri)
   10406                 :             :                 {
   10407                 :      117690 :                   ++*n_perms;
   10408                 :      117690 :                   if (analyze_only)
   10409                 :      106122 :                     continue;
   10410                 :             :                   /* Generate the permute statement if necessary.  */
   10411                 :       11568 :                   tree first_vec = dr_chain[first_vec_index + ri];
   10412                 :       11568 :                   tree second_vec = dr_chain[second_vec_index + ri];
   10413                 :       11568 :                   gassign *stmt = as_a<gassign *> (stmt_info->stmt);
   10414                 :       11568 :                   tree perm_dest
   10415                 :       11568 :                     = vect_create_destination_var (gimple_assign_lhs (stmt),
   10416                 :             :                                                    vectype);
   10417                 :       11568 :                   perm_dest = make_ssa_name (perm_dest);
   10418                 :       11568 :                   gimple *perm_stmt
   10419                 :       11568 :                     = gimple_build_assign (perm_dest, VEC_PERM_EXPR, first_vec,
   10420                 :             :                                            second_vec, mask_vec);
   10421                 :       11568 :                   vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt,
   10422                 :             :                                                gsi);
   10423                 :       11568 :                   if (dce_chain)
   10424                 :             :                     {
   10425                 :       10945 :                       bitmap_set_bit (used_defs, first_vec_index + ri);
   10426                 :       10945 :                       bitmap_set_bit (used_defs, second_vec_index + ri);
   10427                 :             :                     }
   10428                 :             : 
   10429                 :             :                   /* Store the vector statement in NODE.  */
   10430                 :       11568 :                   SLP_TREE_VEC_DEFS (node)[vect_stmts_counter++] = perm_dest;
   10431                 :             :                 }
   10432                 :             :             }
   10433                 :       52674 :           else if (!analyze_only)
   10434                 :             :             {
   10435                 :        2446 :               for (unsigned int ri = 0; ri < nvectors_per_build; ++ri)
   10436                 :             :                 {
   10437                 :        1223 :                   tree first_vec = dr_chain[first_vec_index + ri];
   10438                 :             :                   /* If mask was NULL_TREE generate the requested
   10439                 :             :                      identity transform.  */
   10440                 :        1223 :                   if (dce_chain)
   10441                 :        1222 :                     bitmap_set_bit (used_defs, first_vec_index + ri);
   10442                 :             : 
   10443                 :             :                   /* Store the vector statement in NODE.  */
   10444                 :        1223 :                   SLP_TREE_VEC_DEFS (node)[vect_stmts_counter++] = first_vec;
   10445                 :             :                 }
   10446                 :             :             }
   10447                 :             : 
   10448                 :             :           index = 0;
   10449                 :             :           first_vec_index = -1;
   10450                 :             :           second_vec_index = -1;
   10451                 :             :           noop_p = true;
   10452                 :             :         }
   10453                 :             :     }
   10454                 :             : 
   10455                 :      121897 :   if (n_loads)
   10456                 :             :     {
   10457                 :        2371 :       if (repeating_p)
   10458                 :         192 :         *n_loads = SLP_TREE_NUMBER_OF_VEC_STMTS (node);
   10459                 :             :       else
   10460                 :             :         {
   10461                 :             :           /* Enforced above when !repeating_p.  */
   10462                 :        2179 :           unsigned int const_nunits = nunits.to_constant ();
   10463                 :        2179 :           *n_loads = 0;
   10464                 :        2179 :           bool load_seen = false;
   10465                 :       26289 :           for (unsigned i = 0; i < in_nlanes; ++i)
   10466                 :             :             {
   10467                 :       24110 :               if (i % const_nunits == 0)
   10468                 :             :                 {
   10469                 :        4328 :                   if (load_seen)
   10470                 :        2149 :                     *n_loads += 1;
   10471                 :             :                   load_seen = false;
   10472                 :             :                 }
   10473                 :       24110 :               if (bitmap_bit_p (used_in_lanes, i))
   10474                 :       12086 :                 load_seen = true;
   10475                 :             :             }
   10476                 :        2179 :           if (load_seen)
   10477                 :        2179 :             *n_loads += 1;
   10478                 :             :         }
   10479                 :             :     }
   10480                 :             : 
   10481                 :      121897 :   if (dce_chain)
   10482                 :      180612 :     for (unsigned i = 0; i < dr_chain.length (); ++i)
   10483                 :       44434 :       if (!bitmap_bit_p (used_defs, i))
   10484                 :             :         {
   10485                 :       26111 :           tree def = dr_chain[i];
   10486                 :       26186 :           do
   10487                 :             :             {
   10488                 :       26186 :               gimple *stmt = SSA_NAME_DEF_STMT (def);
   10489                 :       26186 :               if (is_gimple_assign (stmt)
   10490                 :       26186 :                   && (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
   10491                 :       26186 :                       || gimple_assign_rhs_code (stmt) == CONSTRUCTOR))
   10492                 :         513 :                 def = single_ssa_tree_operand (stmt, SSA_OP_USE);
   10493                 :             :               else
   10494                 :             :                 def = NULL;
   10495                 :       26186 :               gimple_stmt_iterator rgsi = gsi_for_stmt (stmt);
   10496                 :       26186 :               gsi_remove (&rgsi, true);
   10497                 :       26186 :               release_defs (stmt);
   10498                 :             :             }
   10499                 :       26186 :           while (def);
   10500                 :             :         }
   10501                 :             : 
   10502                 :             :   return true;
   10503                 :      127026 : }
   10504                 :             : 
   10505                 :             : /* Generate vector permute statements from a list of loads in DR_CHAIN.
   10506                 :             :    If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
   10507                 :             :    permute statements for the SLP node NODE.  Store the number of vector
   10508                 :             :    permute instructions in *N_PERMS and the number of vector load
   10509                 :             :    instructions in *N_LOADS.  If DCE_CHAIN is true, remove all definitions
   10510                 :             :    that were not needed.  */
   10511                 :             : 
   10512                 :             : bool
   10513                 :       95475 : vect_transform_slp_perm_load (vec_info *vinfo,
   10514                 :             :                               slp_tree node, const vec<tree> &dr_chain,
   10515                 :             :                               gimple_stmt_iterator *gsi, poly_uint64 vf,
   10516                 :             :                               bool analyze_only, unsigned *n_perms,
   10517                 :             :                               unsigned int *n_loads, bool dce_chain)
   10518                 :             : {
   10519                 :       95475 :   return vect_transform_slp_perm_load_1 (vinfo, node,
   10520                 :       95475 :                                          SLP_TREE_LOAD_PERMUTATION (node),
   10521                 :             :                                          dr_chain, gsi, vf, analyze_only,
   10522                 :             :                                          dump_enabled_p (), n_perms, n_loads,
   10523                 :       95475 :                                          dce_chain);
   10524                 :             : }
   10525                 :             : 
   10526                 :             : /* Produce the next vector result for SLP permutation NODE by adding a vector
   10527                 :             :    statement at GSI.  If MASK_VEC is nonnull, add:
   10528                 :             : 
   10529                 :             :       <new SSA name> = VEC_PERM_EXPR <FIRST_DEF, SECOND_DEF, MASK_VEC>
   10530                 :             : 
   10531                 :             :    otherwise add:
   10532                 :             : 
   10533                 :             :       <new SSA name> = FIRST_DEF.  */
   10534                 :             : 
   10535                 :             : static void
   10536                 :       28948 : vect_add_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
   10537                 :             :                           slp_tree node, tree first_def, tree second_def,
   10538                 :             :                           tree mask_vec, poly_uint64 identity_offset)
   10539                 :             : {
   10540                 :       28948 :   tree vectype = SLP_TREE_VECTYPE (node);
   10541                 :             : 
   10542                 :             :   /* ???  We SLP match existing vector element extracts but
   10543                 :             :      allow punning which we need to re-instantiate at uses
   10544                 :             :      but have no good way of explicitly representing.  */
   10545                 :       28948 :   if (operand_equal_p (TYPE_SIZE (TREE_TYPE (first_def)), TYPE_SIZE (vectype))
   10546                 :       28948 :       && !types_compatible_p (TREE_TYPE (first_def), vectype))
   10547                 :             :     {
   10548                 :          26 :       gassign *conv_stmt
   10549                 :          26 :         = gimple_build_assign (make_ssa_name (vectype),
   10550                 :             :                                build1 (VIEW_CONVERT_EXPR, vectype, first_def));
   10551                 :          26 :       vect_finish_stmt_generation (vinfo, NULL, conv_stmt, gsi);
   10552                 :          26 :       first_def = gimple_assign_lhs (conv_stmt);
   10553                 :             :     }
   10554                 :       28948 :   gassign *perm_stmt;
   10555                 :       28948 :   tree perm_dest = make_ssa_name (vectype);
   10556                 :       28948 :   if (mask_vec)
   10557                 :             :     {
   10558                 :       26001 :       if (operand_equal_p (TYPE_SIZE (TREE_TYPE (first_def)),
   10559                 :       26001 :                            TYPE_SIZE (vectype))
   10560                 :       26001 :           && !types_compatible_p (TREE_TYPE (second_def), vectype))
   10561                 :             :         {
   10562                 :           9 :           gassign *conv_stmt
   10563                 :           9 :             = gimple_build_assign (make_ssa_name (vectype),
   10564                 :             :                                    build1 (VIEW_CONVERT_EXPR,
   10565                 :             :                                            vectype, second_def));
   10566                 :           9 :           vect_finish_stmt_generation (vinfo, NULL, conv_stmt, gsi);
   10567                 :           9 :           second_def = gimple_assign_lhs (conv_stmt);
   10568                 :             :         }
   10569                 :       26001 :       perm_stmt = gimple_build_assign (perm_dest, VEC_PERM_EXPR,
   10570                 :             :                                        first_def, second_def,
   10571                 :             :                                        mask_vec);
   10572                 :             :     }
   10573                 :        2947 :   else if (!types_compatible_p (TREE_TYPE (first_def), vectype))
   10574                 :             :     {
   10575                 :             :       /* For identity permutes we still need to handle the case
   10576                 :             :          of offsetted extracts or concats.  */
   10577                 :         196 :       unsigned HOST_WIDE_INT c;
   10578                 :         196 :       auto first_def_nunits
   10579                 :         196 :         = TYPE_VECTOR_SUBPARTS (TREE_TYPE (first_def));
   10580                 :         196 :       if (known_le (TYPE_VECTOR_SUBPARTS (vectype), first_def_nunits))
   10581                 :             :         {
   10582                 :         192 :           unsigned HOST_WIDE_INT elsz
   10583                 :         192 :             = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (first_def))));
   10584                 :         384 :           tree lowpart = build3 (BIT_FIELD_REF, vectype, first_def,
   10585                 :         192 :                                  TYPE_SIZE (vectype),
   10586                 :         192 :                                  bitsize_int (identity_offset * elsz));
   10587                 :         192 :           perm_stmt = gimple_build_assign (perm_dest, lowpart);
   10588                 :             :         }
   10589                 :           4 :       else if (constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
   10590                 :           4 :                                     first_def_nunits, &c) && c == 2)
   10591                 :             :         {
   10592                 :           4 :           tree ctor = build_constructor_va (vectype, 2, NULL_TREE, first_def,
   10593                 :             :                                             NULL_TREE, second_def);
   10594                 :           4 :           perm_stmt = gimple_build_assign (perm_dest, ctor);
   10595                 :             :         }
   10596                 :             :       else
   10597                 :           0 :         gcc_unreachable ();
   10598                 :             :     }
   10599                 :             :   else
   10600                 :             :     {
   10601                 :             :       /* We need a copy here in case the def was external.  */
   10602                 :        2751 :       perm_stmt = gimple_build_assign (perm_dest, first_def);
   10603                 :             :     }
   10604                 :       28948 :   vect_finish_stmt_generation (vinfo, NULL, perm_stmt, gsi);
   10605                 :             :   /* Store the vector statement in NODE.  */
   10606                 :       28948 :   node->push_vec_def (perm_stmt);
   10607                 :       28948 : }
   10608                 :             : 
   10609                 :             : /* Subroutine of vectorizable_slp_permutation.  Check whether the target
   10610                 :             :    can perform permutation PERM on the (1 or 2) input nodes in CHILDREN.
   10611                 :             :    If GSI is nonnull, emit the permutation there.
   10612                 :             : 
   10613                 :             :    When GSI is null, the only purpose of NODE is to give properties
   10614                 :             :    of the result, such as the vector type and number of SLP lanes.
   10615                 :             :    The node does not need to be a VEC_PERM_EXPR.
   10616                 :             : 
   10617                 :             :    If the target supports the operation, return the number of individual
   10618                 :             :    VEC_PERM_EXPRs needed, otherwise return -1.  Print information to the
   10619                 :             :    dump file if DUMP_P is true.  */
   10620                 :             : 
   10621                 :             : static int
   10622                 :      351717 : vectorizable_slp_permutation_1 (vec_info *vinfo, gimple_stmt_iterator *gsi,
   10623                 :             :                                 slp_tree node, lane_permutation_t &perm,
   10624                 :             :                                 vec<slp_tree> &children, bool dump_p)
   10625                 :             : {
   10626                 :      351717 :   tree vectype = SLP_TREE_VECTYPE (node);
   10627                 :             : 
   10628                 :             :   /* ???  We currently only support all same vector input types
   10629                 :             :      while the SLP IL should really do a concat + select and thus accept
   10630                 :             :      arbitrary mismatches.  */
   10631                 :      351717 :   slp_tree child;
   10632                 :      351717 :   unsigned i;
   10633                 :      351717 :   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
   10634                 :      351717 :   bool repeating_p = multiple_p (nunits, SLP_TREE_LANES (node));
   10635                 :             :   /* True if we're permuting a single input of 2N vectors down
   10636                 :             :      to N vectors.  This case doesn't generalize beyond 2 since
   10637                 :             :      VEC_PERM_EXPR only takes 2 inputs.  */
   10638                 :      351717 :   bool pack_p = false;
   10639                 :             :   /* If we're permuting inputs of N vectors each into X*N outputs,
   10640                 :             :      this is the value of X, otherwise it is 1.  */
   10641                 :      351717 :   unsigned int unpack_factor = 1;
   10642                 :      351717 :   tree op_vectype = NULL_TREE;
   10643                 :      353385 :   FOR_EACH_VEC_ELT (children, i, child)
   10644                 :      353174 :     if (SLP_TREE_VECTYPE (child))
   10645                 :             :       {
   10646                 :             :         op_vectype = SLP_TREE_VECTYPE (child);
   10647                 :             :         break;
   10648                 :             :       }
   10649                 :      351717 :   if (!op_vectype)
   10650                 :         211 :     op_vectype = vectype;
   10651                 :      757668 :   FOR_EACH_VEC_ELT (children, i, child)
   10652                 :             :     {
   10653                 :      405957 :       if ((SLP_TREE_DEF_TYPE (child) != vect_internal_def
   10654                 :       11168 :            && !vect_maybe_update_slp_op_vectype (child, op_vectype))
   10655                 :      405957 :           || !types_compatible_p (SLP_TREE_VECTYPE (child), op_vectype)
   10656                 :      811914 :           || !types_compatible_p (TREE_TYPE (vectype), TREE_TYPE (op_vectype)))
   10657                 :             :         {
   10658                 :           6 :           if (dump_p)
   10659                 :           6 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   10660                 :             :                              "Unsupported vector types in lane permutation\n");
   10661                 :           6 :           return -1;
   10662                 :             :         }
   10663                 :      405951 :       auto op_nunits = TYPE_VECTOR_SUBPARTS (op_vectype);
   10664                 :      405951 :       unsigned int this_unpack_factor;
   10665                 :             :       /* Detect permutations of external, pre-existing vectors.  The external
   10666                 :             :          node's SLP_TREE_LANES stores the total number of units in the vector,
   10667                 :             :          or zero if the vector has variable length.
   10668                 :             : 
   10669                 :             :          We are expected to keep the original VEC_PERM_EXPR for such cases.
   10670                 :             :          There is no repetition to model.  */
   10671                 :      405951 :       if (SLP_TREE_DEF_TYPE (child) == vect_external_def
   10672                 :      405951 :           && SLP_TREE_SCALAR_OPS (child).is_empty ())
   10673                 :             :         repeating_p = false;
   10674                 :             :       /* Check whether the input has twice as many lanes per vector.  */
   10675                 :      397370 :       else if (children.length () == 1
   10676                 :      397370 :                && known_eq (SLP_TREE_LANES (child) * nunits,
   10677                 :             :                             SLP_TREE_LANES (node) * op_nunits * 2))
   10678                 :             :         pack_p = true;
   10679                 :             :       /* Check whether the output has N times as many lanes per vector.  */
   10680                 :      405951 :       else if (constant_multiple_p (SLP_TREE_LANES (node) * op_nunits,
   10681                 :      350487 :                                     SLP_TREE_LANES (child) * nunits,
   10682                 :             :                                     &this_unpack_factor)
   10683                 :      311483 :                && (i == 0 || unpack_factor == this_unpack_factor))
   10684                 :             :         unpack_factor = this_unpack_factor;
   10685                 :             :       else
   10686                 :             :         repeating_p = false;
   10687                 :             :     }
   10688                 :             : 
   10689                 :      703422 :   gcc_assert (perm.length () == SLP_TREE_LANES (node));
   10690                 :             : 
   10691                 :             :   /* Load-lanes permute.  This permute only acts as a forwarder to
   10692                 :             :      select the correct vector def of the load-lanes load which
   10693                 :             :      has the permuted vectors in its vector defs like
   10694                 :             :      { v0, w0, r0, v1, w1, r1 ... } for a ld3.  All costs are
   10695                 :             :      accounted for in the costing for the actual load so we
   10696                 :             :      return zero here.  */
   10697                 :      351711 :   if (node->ldst_lanes)
   10698                 :             :     {
   10699                 :           0 :       gcc_assert (children.length () == 1);
   10700                 :           0 :       if (!gsi)
   10701                 :             :         /* This is a trivial op always supported.  */
   10702                 :             :         return 0;
   10703                 :           0 :       slp_tree child = children[0];
   10704                 :           0 :       unsigned vec_idx = (SLP_TREE_LANE_PERMUTATION (node)[0].second
   10705                 :           0 :                           / SLP_TREE_LANES (node));
   10706                 :           0 :       unsigned vec_num = SLP_TREE_LANES (child) / SLP_TREE_LANES (node);
   10707                 :           0 :       for (unsigned i = 0; i < SLP_TREE_NUMBER_OF_VEC_STMTS (node); ++i)
   10708                 :             :         {
   10709                 :           0 :           tree def = SLP_TREE_VEC_DEFS (child)[i * vec_num  + vec_idx];
   10710                 :           0 :           node->push_vec_def (def);
   10711                 :             :         }
   10712                 :             :       return 0;
   10713                 :             :     }
   10714                 :             : 
   10715                 :             :   /* Set REPEATING_P to true if the permutations are cylical wrt UNPACK_FACTOR
   10716                 :             :      and if we can generate the vectors in a vector-length agnostic way.
   10717                 :             :      This requires UNPACK_STEP == NUNITS / UNPACK_FACTOR to be known at
   10718                 :             :      compile time.
   10719                 :             : 
   10720                 :             :      The significance of UNPACK_STEP is that, when PACK_P is false,
   10721                 :             :      output vector I operates on a window of UNPACK_STEP elements from each
   10722                 :             :      input, starting at lane UNPACK_STEP * (I % UNPACK_FACTOR).  For example,
   10723                 :             :      when UNPACK_FACTOR is 2, the first output vector operates on lanes
   10724                 :             :      [0, NUNITS / 2 - 1] of each input vector and the second output vector
   10725                 :             :      operates on lanes [NUNITS / 2, NUNITS - 1] of each input vector.
   10726                 :             : 
   10727                 :             :      When REPEATING_P is true, NOUTPUTS holds the total number of outputs
   10728                 :             :      that we actually need to generate.  */
   10729                 :      351711 :   uint64_t noutputs = 0;
   10730                 :      351711 :   poly_uint64 unpack_step = 0;
   10731                 :      351711 :   loop_vec_info linfo = dyn_cast <loop_vec_info> (vinfo);
   10732                 :      115784 :   if (!linfo
   10733                 :      394024 :       || !multiple_p (nunits, unpack_factor, &unpack_step)
   10734                 :      114716 :       || !constant_multiple_p (LOOP_VINFO_VECT_FACTOR (linfo)
   10735                 :      114716 :                                * SLP_TREE_LANES (node), nunits, &noutputs))
   10736                 :             :     repeating_p = false;
   10737                 :             : 
   10738                 :             :   /* We can handle the conditions described for REPEATING_P above for
   10739                 :             :      both variable- and constant-length vectors.  The fallback requires
   10740                 :             :      us to generate every element of every permute vector explicitly,
   10741                 :             :      which is only possible for constant-length permute vectors.
   10742                 :             : 
   10743                 :             :      Set:
   10744                 :             : 
   10745                 :             :      - NPATTERNS and NELTS_PER_PATTERN to the encoding of the permute
   10746                 :             :        mask vectors that we want to build.
   10747                 :             : 
   10748                 :             :      - NCOPIES to the number of copies of PERM that we need in order
   10749                 :             :        to build the necessary permute mask vectors.  */
   10750                 :      114716 :   uint64_t npatterns;
   10751                 :      114716 :   unsigned nelts_per_pattern;
   10752                 :      114716 :   uint64_t ncopies;
   10753                 :      114716 :   if (repeating_p)
   10754                 :             :     {
   10755                 :             :       /* We need permute mask vectors that have the form:
   10756                 :             : 
   10757                 :             :            { X1, ..., Xn, X1 + n, ..., Xn + n, X1 + 2n, ..., Xn + 2n, ... }
   10758                 :             : 
   10759                 :             :          In other words, the original n-element permute in PERM is
   10760                 :             :          "unrolled" to fill a full vector.  The stepped vector encoding
   10761                 :             :          that we use for permutes requires 3n elements.  */
   10762                 :       72403 :       npatterns = SLP_TREE_LANES (node);
   10763                 :       72403 :       nelts_per_pattern = ncopies = 3;
   10764                 :             :     }
   10765                 :             :   else
   10766                 :             :     {
   10767                 :             :       /* Calculate every element of every permute mask vector explicitly,
   10768                 :             :          instead of relying on the pattern described above.  */
   10769                 :      279308 :       if (!nunits.is_constant (&npatterns)
   10770                 :      279308 :           || !TYPE_VECTOR_SUBPARTS (op_vectype).is_constant ())
   10771                 :             :         {
   10772                 :             :           if (dump_p)
   10773                 :             :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   10774                 :             :                              "unsupported permutation %p on variable-length"
   10775                 :             :                              " vectors\n", (void *) node);
   10776                 :             :           return -1;
   10777                 :             :         }
   10778                 :      279308 :       nelts_per_pattern = ncopies = 1;
   10779                 :      279308 :       if (linfo && !LOOP_VINFO_VECT_FACTOR (linfo).is_constant (&ncopies))
   10780                 :             :         {
   10781                 :             :           if (dump_p)
   10782                 :             :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   10783                 :             :                              "unsupported permutation %p for variable VF\n",
   10784                 :             :                              (void *) node);
   10785                 :             :           return -1;
   10786                 :             :         }
   10787                 :             :       pack_p = false;
   10788                 :             :       unpack_factor = 1;
   10789                 :             :     }
   10790                 :      351711 :   unsigned olanes = unpack_factor * ncopies * SLP_TREE_LANES (node);
   10791                 :      351711 :   gcc_assert (repeating_p || multiple_p (olanes, nunits));
   10792                 :             : 
   10793                 :             :   /* Compute the { { SLP operand, vector index}, lane } permutation sequence
   10794                 :             :      from the { SLP operand, scalar lane } permutation as recorded in the
   10795                 :             :      SLP node as intermediate step.  This part should already work
   10796                 :             :      with SLP children with arbitrary number of lanes.  */
   10797                 :      351711 :   auto_vec<std::pair<std::pair<unsigned, unsigned>, poly_uint64>> vperm;
   10798                 :      351711 :   auto_vec<poly_uint64> active_lane;
   10799                 :      351711 :   vperm.create (olanes);
   10800                 :      351711 :   active_lane.safe_grow_cleared (children.length (), true);
   10801                 :      711232 :   for (unsigned int ui = 0; ui < unpack_factor; ++ui)
   10802                 :             :     {
   10803                 :     1562184 :       for (unsigned j = 0; j < children.length (); ++j)
   10804                 :      421571 :         active_lane[j] = ui * unpack_step;
   10805                 :     1000043 :       for (unsigned i = 0; i < ncopies; ++i)
   10806                 :             :         {
   10807                 :     4031380 :           for (unsigned pi = 0; pi < perm.length (); ++pi)
   10808                 :             :             {
   10809                 :     1375168 :               std::pair<unsigned, unsigned> p = perm[pi];
   10810                 :     1375168 :               tree vtype = SLP_TREE_VECTYPE (children[p.first]);
   10811                 :     1375168 :               if (repeating_p)
   10812                 :      404526 :                 vperm.quick_push ({{p.first, 0},
   10813                 :      404526 :                                    p.second + active_lane[p.first]});
   10814                 :             :               else
   10815                 :             :                 {
   10816                 :             :                   /* We checked above that the vectors are constant-length.  */
   10817                 :      970642 :                   unsigned vnunits = TYPE_VECTOR_SUBPARTS (vtype)
   10818                 :      970642 :                     .to_constant ();
   10819                 :      970642 :                   unsigned lane = active_lane[p.first].to_constant ();
   10820                 :      970642 :                   unsigned vi = (lane + p.second) / vnunits;
   10821                 :      970642 :                   unsigned vl = (lane + p.second) % vnunits;
   10822                 :      970642 :                   vperm.quick_push ({{p.first, vi}, vl});
   10823                 :             :                 }
   10824                 :             :             }
   10825                 :             :           /* Advance to the next group.  */
   10826                 :     1392171 :           for (unsigned j = 0; j < children.length (); ++j)
   10827                 :      751649 :             active_lane[j] += SLP_TREE_LANES (children[j]);
   10828                 :             :         }
   10829                 :             :     }
   10830                 :             : 
   10831                 :      351711 :   if (dump_p)
   10832                 :             :     {
   10833                 :        9023 :       dump_printf_loc (MSG_NOTE, vect_location,
   10834                 :             :                        "vectorizing permutation %p", (void *)node);
   10835                 :       32227 :       for (unsigned i = 0; i < perm.length (); ++i)
   10836                 :       23204 :         dump_printf (MSG_NOTE, " op%u[%u]", perm[i].first, perm[i].second);
   10837                 :        9023 :       if (repeating_p)
   10838                 :        7746 :         dump_printf (MSG_NOTE, " (repeat %d)\n", SLP_TREE_LANES (node));
   10839                 :        9023 :       dump_printf (MSG_NOTE, "\n");
   10840                 :        9023 :       dump_printf_loc (MSG_NOTE, vect_location, "as");
   10841                 :       90792 :       for (unsigned i = 0; i < vperm.length (); ++i)
   10842                 :             :         {
   10843                 :       81769 :           if (i != 0
   10844                 :       81769 :               && (repeating_p
   10845                 :       56913 :                   ? multiple_p (i, npatterns)
   10846                 :       60334 :                   : multiple_p (i, TYPE_VECTOR_SUBPARTS (vectype))))
   10847                 :       24219 :             dump_printf (MSG_NOTE, ",");
   10848                 :       81769 :           dump_printf (MSG_NOTE, " vops%u[%u][",
   10849                 :       81769 :                        vperm[i].first.first, vperm[i].first.second);
   10850                 :       81769 :           dump_dec (MSG_NOTE, vperm[i].second);
   10851                 :       81769 :           dump_printf (MSG_NOTE, "]");
   10852                 :             :         }
   10853                 :        9023 :       dump_printf (MSG_NOTE, "\n");
   10854                 :             :     }
   10855                 :             : 
   10856                 :             :   /* We can only handle two-vector permutes, everything else should
   10857                 :             :      be lowered on the SLP level.  The following is closely inspired
   10858                 :             :      by vect_transform_slp_perm_load and is supposed to eventually
   10859                 :             :      replace it.
   10860                 :             :      ???   As intermediate step do code-gen in the SLP tree representation
   10861                 :             :      somehow?  */
   10862                 :      351711 :   std::pair<unsigned, unsigned> first_vec = std::make_pair (-1U, -1U);
   10863                 :      351711 :   std::pair<unsigned, unsigned> second_vec = std::make_pair (-1U, -1U);
   10864                 :      351711 :   unsigned int index = 0;
   10865                 :      351711 :   poly_uint64 mask_element;
   10866                 :      351711 :   vec_perm_builder mask;
   10867                 :      351711 :   mask.new_vector (nunits, npatterns, nelts_per_pattern);
   10868                 :      351711 :   unsigned int count = mask.encoded_nelts ();
   10869                 :      351711 :   mask.quick_grow (count);
   10870                 :      351711 :   vec_perm_indices indices;
   10871                 :      351711 :   unsigned nperms = 0;
   10872                 :             :   /* When REPEATING_P is true, we only have UNPACK_FACTOR unique permute
   10873                 :             :      vectors to check during analysis, but we need to generate NOUTPUTS
   10874                 :             :      vectors during transformation.  */
   10875                 :      351711 :   unsigned total_nelts = olanes;
   10876                 :      351711 :   if (repeating_p && gsi)
   10877                 :        9363 :     total_nelts = (total_nelts / unpack_factor) * noutputs;
   10878                 :     1731227 :   for (unsigned i = 0; i < total_nelts; ++i)
   10879                 :             :     {
   10880                 :             :       /* VI is the input vector index when generating code for REPEATING_P.  */
   10881                 :     1387009 :       unsigned vi = i / olanes * (pack_p ? 2 : 1);
   10882                 :     1387009 :       unsigned ei = i % olanes;
   10883                 :     1387009 :       mask_element = vperm[ei].second;
   10884                 :     1387009 :       if (pack_p)
   10885                 :             :         {
   10886                 :             :           /* In this case, we have N outputs and the single child provides 2N
   10887                 :             :              inputs.  Output X permutes inputs 2X and 2X+1.
   10888                 :             : 
   10889                 :             :              The mask indices are taken directly from the SLP permutation node.
   10890                 :             :              Index X selects from the first vector if (X / NUNITS) % 2 == 0;
   10891                 :             :              X selects from the second vector otherwise.  These conditions
   10892                 :             :              are only known at compile time for constant-length vectors.  */
   10893                 :             :           first_vec = std::make_pair (0, 0);
   10894                 :             :           second_vec = std::make_pair (0, 1);
   10895                 :             :         }
   10896                 :     1209709 :       else if (first_vec.first == -1U
   10897                 :     1209709 :                || first_vec == vperm[ei].first)
   10898                 :     1025524 :         first_vec = vperm[ei].first;
   10899                 :      184185 :       else if (second_vec.first == -1U
   10900                 :      184185 :                || second_vec == vperm[ei].first)
   10901                 :             :         {
   10902                 :      183553 :           second_vec = vperm[ei].first;
   10903                 :      183553 :           mask_element += nunits;
   10904                 :             :         }
   10905                 :             :       else
   10906                 :             :         {
   10907                 :         632 :           if (dump_p)
   10908                 :          24 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   10909                 :             :                              "permutation requires at "
   10910                 :             :                              "least three vectors\n");
   10911                 :         632 :           gcc_assert (!gsi);
   10912                 :             :           return -1;
   10913                 :             :         }
   10914                 :             : 
   10915                 :     1386377 :       mask[index++] = mask_element;
   10916                 :             : 
   10917                 :     1386377 :       if (index == count)
   10918                 :             :         {
   10919                 :      677926 :           indices.new_vector (mask, second_vec.first == -1U ? 1 : 2,
   10920                 :             :                               TYPE_VECTOR_SUBPARTS (op_vectype));
   10921                 :      536636 :           bool identity_p = (indices.series_p (0, 1, mask[0], 1)
   10922                 :      835323 :                              && constant_multiple_p (mask[0], nunits));
   10923                 :      536636 :           machine_mode vmode = TYPE_MODE (vectype);
   10924                 :      536636 :           machine_mode op_vmode = TYPE_MODE (op_vectype);
   10925                 :      536636 :           unsigned HOST_WIDE_INT c;
   10926                 :      536636 :           if ((!identity_p
   10927                 :      432170 :                && !can_vec_perm_const_p (vmode, op_vmode, indices))
   10928                 :      536636 :               || (identity_p
   10929                 :      104466 :                   && !known_le (nunits,
   10930                 :             :                                 TYPE_VECTOR_SUBPARTS (op_vectype))
   10931                 :        6869 :                   && (!constant_multiple_p (nunits,
   10932                 :           8 :                                             TYPE_VECTOR_SUBPARTS (op_vectype),
   10933                 :           8 :                                             &c) || c != 2)))
   10934                 :             :             {
   10935                 :        6861 :               if (dump_p)
   10936                 :             :                 {
   10937                 :         164 :                   dump_printf_loc (MSG_MISSED_OPTIMIZATION,
   10938                 :             :                                    vect_location,
   10939                 :             :                                    "unsupported vect permute { ");
   10940                 :        1734 :                   for (i = 0; i < count; ++i)
   10941                 :             :                     {
   10942                 :        1570 :                       dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
   10943                 :        1570 :                       dump_printf (MSG_MISSED_OPTIMIZATION, " ");
   10944                 :             :                     }
   10945                 :         164 :                   dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
   10946                 :             :                 }
   10947                 :        6861 :               gcc_assert (!gsi);
   10948                 :        7493 :               return -1;
   10949                 :             :             }
   10950                 :             : 
   10951                 :      529775 :           if (!identity_p)
   10952                 :      425309 :             nperms++;
   10953                 :      529775 :           if (gsi)
   10954                 :             :             {
   10955                 :       28948 :               if (second_vec.first == -1U)
   10956                 :        6494 :                 second_vec = first_vec;
   10957                 :             : 
   10958                 :       28948 :               slp_tree
   10959                 :       28948 :                 first_node = children[first_vec.first],
   10960                 :       28948 :                 second_node = children[second_vec.first];
   10961                 :             : 
   10962                 :       28948 :               tree mask_vec = NULL_TREE;
   10963                 :       28948 :               if (!identity_p)
   10964                 :       26001 :                 mask_vec = vect_gen_perm_mask_checked (vectype, indices);
   10965                 :             : 
   10966                 :       28948 :               tree first_def
   10967                 :       28948 :                 = vect_get_slp_vect_def (first_node, first_vec.second + vi);
   10968                 :       28948 :               tree second_def
   10969                 :       28948 :                 = vect_get_slp_vect_def (second_node, second_vec.second + vi);
   10970                 :       28948 :               vect_add_slp_permutation (vinfo, gsi, node, first_def,
   10971                 :       28948 :                                         second_def, mask_vec, mask[0]);
   10972                 :             :             }
   10973                 :             : 
   10974                 :             :           index = 0;
   10975                 :             :           first_vec = std::make_pair (-1U, -1U);
   10976                 :             :           second_vec = std::make_pair (-1U, -1U);
   10977                 :             :         }
   10978                 :             :     }
   10979                 :             : 
   10980                 :      344218 :   return nperms;
   10981                 :      351711 : }
   10982                 :             : 
   10983                 :             : /* Vectorize the SLP permutations in NODE as specified
   10984                 :             :    in SLP_TREE_LANE_PERMUTATION which is a vector of pairs of SLP
   10985                 :             :    child number and lane number.
   10986                 :             :    Interleaving of two two-lane two-child SLP subtrees (not supported):
   10987                 :             :      [ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } ]
   10988                 :             :    A blend of two four-lane two-child SLP subtrees:
   10989                 :             :      [ { 0, 0 }, { 1, 1 }, { 0, 2 }, { 1, 3 } ]
   10990                 :             :    Highpart of a four-lane one-child SLP subtree (not supported):
   10991                 :             :      [ { 0, 2 }, { 0, 3 } ]
   10992                 :             :    Where currently only a subset is supported by code generating below.  */
   10993                 :             : 
   10994                 :             : static bool
   10995                 :      124924 : vectorizable_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
   10996                 :             :                               slp_tree node, stmt_vector_for_cost *cost_vec)
   10997                 :             : {
   10998                 :      124924 :   tree vectype = SLP_TREE_VECTYPE (node);
   10999                 :      124924 :   lane_permutation_t &perm = SLP_TREE_LANE_PERMUTATION (node);
   11000                 :      124924 :   int nperms = vectorizable_slp_permutation_1 (vinfo, gsi, node, perm,
   11001                 :      124924 :                                                SLP_TREE_CHILDREN (node),
   11002                 :             :                                                dump_enabled_p ());
   11003                 :      124924 :   if (nperms < 0)
   11004                 :             :     return false;
   11005                 :             : 
   11006                 :      123605 :   if (!gsi)
   11007                 :      108239 :     record_stmt_cost (cost_vec, nperms, vec_perm, node, vectype, 0, vect_body);
   11008                 :             : 
   11009                 :             :   return true;
   11010                 :             : }
   11011                 :             : 
   11012                 :             : /* Vectorize SLP NODE.  */
   11013                 :             : 
   11014                 :             : static void
   11015                 :     1394407 : vect_schedule_slp_node (vec_info *vinfo,
   11016                 :             :                         slp_tree node, slp_instance instance)
   11017                 :             : {
   11018                 :     1394407 :   gimple_stmt_iterator si;
   11019                 :     1394407 :   int i;
   11020                 :     1394407 :   slp_tree child;
   11021                 :             : 
   11022                 :             :   /* Vectorize externals and constants.  */
   11023                 :     1394407 :   if (SLP_TREE_DEF_TYPE (node) == vect_constant_def
   11024                 :     1394407 :       || SLP_TREE_DEF_TYPE (node) == vect_external_def)
   11025                 :             :     {
   11026                 :             :       /* ???  vectorizable_shift can end up using a scalar operand which is
   11027                 :             :          currently denoted as !SLP_TREE_VECTYPE.  No need to vectorize the
   11028                 :             :          node in this case.  */
   11029                 :      477304 :       if (!SLP_TREE_VECTYPE (node))
   11030                 :      477304 :         return;
   11031                 :             : 
   11032                 :             :       /* There are two reasons vector defs might already exist.  The first
   11033                 :             :          is that we are vectorizing an existing vector def.  The second is
   11034                 :             :          when performing BB vectorization shared constant/external nodes
   11035                 :             :          are not split apart during partitioning so during the code-gen
   11036                 :             :          DFS walk we can end up visiting them twice.  */
   11037                 :      471751 :       if (! SLP_TREE_VEC_DEFS (node).exists ())
   11038                 :      471079 :         vect_create_constant_vectors (vinfo, node);
   11039                 :      471751 :       return;
   11040                 :             :     }
   11041                 :             : 
   11042                 :      917103 :   gcc_assert (SLP_TREE_VEC_DEFS (node).is_empty ());
   11043                 :             : 
   11044                 :      917103 :   stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
   11045                 :             : 
   11046                 :      917103 :   gcc_assert (SLP_TREE_NUMBER_OF_VEC_STMTS (node) != 0);
   11047                 :      917103 :   SLP_TREE_VEC_DEFS (node).create (SLP_TREE_NUMBER_OF_VEC_STMTS (node));
   11048                 :             : 
   11049                 :      917103 :   if (SLP_TREE_CODE (node) != VEC_PERM_EXPR
   11050                 :      901737 :       && STMT_VINFO_DATA_REF (stmt_info))
   11051                 :             :     {
   11052                 :             :       /* Vectorized loads go before the first scalar load to make it
   11053                 :             :          ready early, vectorized stores go before the last scalar
   11054                 :             :          stmt which is where all uses are ready.  */
   11055                 :      683544 :       stmt_vec_info last_stmt_info = NULL;
   11056                 :      683544 :       if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
   11057                 :      154485 :         last_stmt_info = vect_find_first_scalar_stmt_in_slp (node);
   11058                 :             :       else /* DR_IS_WRITE */
   11059                 :      529059 :         last_stmt_info = vect_find_last_scalar_stmt_in_slp (node);
   11060                 :      683544 :       si = gsi_for_stmt (last_stmt_info->stmt);
   11061                 :      683544 :     }
   11062                 :      233559 :   else if (SLP_TREE_CODE (node) != VEC_PERM_EXPR
   11063                 :      218193 :            && (STMT_VINFO_TYPE (stmt_info) == cycle_phi_info_type
   11064                 :             :                || STMT_VINFO_TYPE (stmt_info) == induc_vec_info_type
   11065                 :             :                || STMT_VINFO_TYPE (stmt_info) == phi_info_type))
   11066                 :             :     {
   11067                 :             :       /* For PHI node vectorization we do not use the insertion iterator.  */
   11068                 :       50963 :       si = gsi_none ();
   11069                 :             :     }
   11070                 :             :   else
   11071                 :             :     {
   11072                 :             :       /* Emit other stmts after the children vectorized defs which is
   11073                 :             :          earliest possible.  */
   11074                 :             :       gimple *last_stmt = NULL;
   11075                 :             :       bool seen_vector_def = false;
   11076                 :      506308 :       FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
   11077                 :      323712 :         if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
   11078                 :             :           {
   11079                 :             :             /* For fold-left reductions we are retaining the scalar
   11080                 :             :                reduction PHI but we still have SLP_TREE_NUM_VEC_STMTS
   11081                 :             :                set so the representation isn't perfect.  Resort to the
   11082                 :             :                last scalar def here.  */
   11083                 :      260807 :             if (SLP_TREE_VEC_DEFS (child).is_empty ())
   11084                 :             :               {
   11085                 :         890 :                 gcc_assert (STMT_VINFO_TYPE (SLP_TREE_REPRESENTATIVE (child))
   11086                 :             :                             == cycle_phi_info_type);
   11087                 :         890 :                 gphi *phi = as_a <gphi *>
   11088                 :         890 :                               (vect_find_last_scalar_stmt_in_slp (child)->stmt);
   11089                 :         890 :                 if (!last_stmt
   11090                 :         890 :                     || vect_stmt_dominates_stmt_p (last_stmt, phi))
   11091                 :             :                   last_stmt = phi;
   11092                 :             :               }
   11093                 :             :             /* We are emitting all vectorized stmts in the same place and
   11094                 :             :                the last one is the last.
   11095                 :             :                ???  Unless we have a load permutation applied and that
   11096                 :             :                figures to re-use an earlier generated load.  */
   11097                 :             :             unsigned j;
   11098                 :             :             tree vdef;
   11099                 :      600430 :             FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child), j, vdef)
   11100                 :             :               {
   11101                 :      339623 :                 gimple *vstmt = SSA_NAME_DEF_STMT (vdef);
   11102                 :      339623 :                 if (!last_stmt
   11103                 :      339623 :                     || vect_stmt_dominates_stmt_p (last_stmt, vstmt))
   11104                 :             :                   last_stmt = vstmt;
   11105                 :             :               }
   11106                 :             :           }
   11107                 :       62905 :         else if (!SLP_TREE_VECTYPE (child))
   11108                 :             :           {
   11109                 :             :             /* For externals we use unvectorized at all scalar defs.  */
   11110                 :             :             unsigned j;
   11111                 :             :             tree def;
   11112                 :       12107 :             FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (child), j, def)
   11113                 :        7136 :               if (TREE_CODE (def) == SSA_NAME
   11114                 :        7136 :                   && !SSA_NAME_IS_DEFAULT_DEF (def))
   11115                 :             :                 {
   11116                 :         168 :                   gimple *stmt = SSA_NAME_DEF_STMT (def);
   11117                 :         168 :                   if (!last_stmt
   11118                 :         168 :                       || vect_stmt_dominates_stmt_p (last_stmt, stmt))
   11119                 :             :                     last_stmt = stmt;
   11120                 :             :                 }
   11121                 :             :           }
   11122                 :             :         else
   11123                 :             :           {
   11124                 :             :             /* For externals we have to look at all defs since their
   11125                 :             :                insertion place is decided per vector.  But beware
   11126                 :             :                of pre-existing vectors where we need to make sure
   11127                 :             :                we do not insert before the region boundary.  */
   11128                 :       57934 :             if (SLP_TREE_SCALAR_OPS (child).is_empty ()
   11129                 :         548 :                 && !vinfo->lookup_def (SLP_TREE_VEC_DEFS (child)[0]))
   11130                 :             :               seen_vector_def = true;
   11131                 :             :             else
   11132                 :             :               {
   11133                 :             :                 unsigned j;
   11134                 :             :                 tree vdef;
   11135                 :      457647 :                 FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child), j, vdef)
   11136                 :       76116 :                   if (TREE_CODE (vdef) == SSA_NAME
   11137                 :       76116 :                       && !SSA_NAME_IS_DEFAULT_DEF (vdef))
   11138                 :             :                     {
   11139                 :       16626 :                       gimple *vstmt = SSA_NAME_DEF_STMT (vdef);
   11140                 :       16626 :                       if (!last_stmt
   11141                 :       16626 :                           || vect_stmt_dominates_stmt_p (last_stmt, vstmt))
   11142                 :             :                         last_stmt = vstmt;
   11143                 :             :                     }
   11144                 :             :               }
   11145                 :             :           }
   11146                 :             :       /* This can happen when all children are pre-existing vectors or
   11147                 :             :          constants.  */
   11148                 :      182596 :       if (!last_stmt)
   11149                 :        1713 :         last_stmt = vect_find_first_scalar_stmt_in_slp (node)->stmt;
   11150                 :        1713 :       if (!last_stmt)
   11151                 :             :         {
   11152                 :           0 :           gcc_assert (seen_vector_def);
   11153                 :           0 :           si = gsi_after_labels (vinfo->bbs[0]);
   11154                 :             :         }
   11155                 :      182596 :       else if (is_ctrl_altering_stmt (last_stmt))
   11156                 :             :         {
   11157                 :             :           /* We split regions to vectorize at control altering stmts
   11158                 :             :              with a definition so this must be an external which
   11159                 :             :              we can insert at the start of the region.  */
   11160                 :           3 :           si = gsi_after_labels (vinfo->bbs[0]);
   11161                 :             :         }
   11162                 :      182593 :       else if (is_a <bb_vec_info> (vinfo)
   11163                 :       13755 :                && SLP_TREE_CODE (node) != VEC_PERM_EXPR
   11164                 :       12701 :                && gimple_bb (last_stmt) != gimple_bb (stmt_info->stmt)
   11165                 :      183526 :                && gimple_could_trap_p (stmt_info->stmt))
   11166                 :             :         {
   11167                 :             :           /* We've constrained possibly trapping operations to all come
   11168                 :             :              from the same basic-block, if vectorized defs would allow earlier
   11169                 :             :              scheduling still force vectorized stmts to the original block.
   11170                 :             :              This is only necessary for BB vectorization since for loop vect
   11171                 :             :              all operations are in a single BB and scalar stmt based
   11172                 :             :              placement doesn't play well with epilogue vectorization.  */
   11173                 :          76 :           gcc_assert (dominated_by_p (CDI_DOMINATORS,
   11174                 :             :                                       gimple_bb (stmt_info->stmt),
   11175                 :             :                                       gimple_bb (last_stmt)));
   11176                 :          76 :           si = gsi_after_labels (gimple_bb (stmt_info->stmt));
   11177                 :             :         }
   11178                 :      182517 :       else if (is_a <gphi *> (last_stmt))
   11179                 :       13747 :         si = gsi_after_labels (gimple_bb (last_stmt));
   11180                 :             :       else
   11181                 :             :         {
   11182                 :      168770 :           si = gsi_for_stmt (last_stmt);
   11183                 :      168770 :           gsi_next (&si);
   11184                 :             : 
   11185                 :             :           /* Avoid scheduling internal defs outside of the loop when
   11186                 :             :              we might have only implicitly tracked loop mask/len defs.  */
   11187                 :      168770 :           if (auto loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
   11188                 :          38 :             if (LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
   11189                 :      155257 :                 || LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
   11190                 :             :               {
   11191                 :          38 :                 gimple_stmt_iterator si2
   11192                 :          38 :                   = gsi_after_labels (LOOP_VINFO_LOOP (loop_vinfo)->header);
   11193                 :          38 :                 if ((gsi_end_p (si2)
   11194                 :           0 :                      && (LOOP_VINFO_LOOP (loop_vinfo)->header
   11195                 :           0 :                          != gimple_bb (last_stmt))
   11196                 :           0 :                      && dominated_by_p (CDI_DOMINATORS,
   11197                 :             :                                         LOOP_VINFO_LOOP (loop_vinfo)->header,
   11198                 :           0 :                                         gimple_bb (last_stmt)))
   11199                 :          38 :                     || (!gsi_end_p (si2)
   11200                 :          38 :                         && last_stmt != *si2
   11201                 :          37 :                         && vect_stmt_dominates_stmt_p (last_stmt, *si2)))
   11202                 :           1 :                   si = si2;
   11203                 :             :               }
   11204                 :             :         }
   11205                 :             :     }
   11206                 :             : 
   11207                 :             :   /* Handle purely internal nodes.  */
   11208                 :      917103 :   if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
   11209                 :             :     {
   11210                 :       15366 :       if (dump_enabled_p ())
   11211                 :        3061 :         dump_printf_loc (MSG_NOTE, vect_location,
   11212                 :             :                          "------>vectorizing SLP permutation node\n");
   11213                 :             :       /* ???  the transform kind is stored to STMT_VINFO_TYPE which might
   11214                 :             :          be shared with different SLP nodes (but usually it's the same
   11215                 :             :          operation apart from the case the stmt is only there for denoting
   11216                 :             :          the actual scalar lane defs ...).  So do not call vect_transform_stmt
   11217                 :             :          but open-code it here (partly).  */
   11218                 :       15366 :       bool done = vectorizable_slp_permutation (vinfo, &si, node, NULL);
   11219                 :       15366 :       gcc_assert (done);
   11220                 :             :       stmt_vec_info slp_stmt_info;
   11221                 :             :       unsigned int i;
   11222                 :      952309 :       FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, slp_stmt_info)
   11223                 :       22861 :         if (slp_stmt_info && STMT_VINFO_LIVE_P (slp_stmt_info))
   11224                 :             :           {
   11225                 :         564 :             done = vectorizable_live_operation (vinfo, slp_stmt_info, node,
   11226                 :             :                                                 instance, i, true, NULL);
   11227                 :         564 :             gcc_assert (done);
   11228                 :             :           }
   11229                 :             :     }
   11230                 :             :   else
   11231                 :             :     {
   11232                 :      901737 :       if (dump_enabled_p ())
   11233                 :       62737 :         dump_printf_loc (MSG_NOTE, vect_location,
   11234                 :             :                          "------>vectorizing SLP node starting from: %G",
   11235                 :             :                          stmt_info->stmt);
   11236                 :      901737 :       vect_transform_stmt (vinfo, stmt_info, &si, node, instance);
   11237                 :             :     }
   11238                 :             : }
   11239                 :             : 
   11240                 :             : /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
   11241                 :             :    For loop vectorization this is done in vectorizable_call, but for SLP
   11242                 :             :    it needs to be deferred until end of vect_schedule_slp, because multiple
   11243                 :             :    SLP instances may refer to the same scalar stmt.  */
   11244                 :             : 
   11245                 :             : static void
   11246                 :      548707 : vect_remove_slp_scalar_calls (vec_info *vinfo,
   11247                 :             :                               slp_tree node, hash_set<slp_tree> &visited)
   11248                 :             : {
   11249                 :      548707 :   gimple *new_stmt;
   11250                 :      548707 :   gimple_stmt_iterator gsi;
   11251                 :      548707 :   int i;
   11252                 :      548707 :   slp_tree child;
   11253                 :      548707 :   tree lhs;
   11254                 :      548707 :   stmt_vec_info stmt_info;
   11255                 :             : 
   11256                 :      548707 :   if (!node || SLP_TREE_DEF_TYPE (node) != vect_internal_def)
   11257                 :      171865 :     return;
   11258                 :             : 
   11259                 :      416819 :   if (visited.add (node))
   11260                 :             :     return;
   11261                 :             : 
   11262                 :      841151 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
   11263                 :      464309 :     vect_remove_slp_scalar_calls (vinfo, child, visited);
   11264                 :             : 
   11265                 :     1188618 :   FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
   11266                 :             :     {
   11267                 :      438871 :       if (!stmt_info)
   11268                 :        3611 :         continue;
   11269                 :      435260 :       gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
   11270                 :        4799 :       if (!stmt || gimple_bb (stmt) == NULL)
   11271                 :      430467 :         continue;
   11272                 :        4793 :       if (is_pattern_stmt_p (stmt_info)
   11273                 :        4793 :           || !PURE_SLP_STMT (stmt_info))
   11274                 :         732 :         continue;
   11275                 :        4061 :       lhs = gimple_call_lhs (stmt);
   11276                 :        4061 :       if (lhs)
   11277                 :        3619 :         new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
   11278                 :             :       else
   11279                 :             :         {
   11280                 :         442 :           new_stmt = gimple_build_nop ();
   11281                 :         442 :           unlink_stmt_vdef (stmt_info->stmt);
   11282                 :             :         }
   11283                 :        4061 :       gsi = gsi_for_stmt (stmt);
   11284                 :        4061 :       vinfo->replace_stmt (&gsi, stmt_info, new_stmt);
   11285                 :        4061 :       if (lhs)
   11286                 :        3619 :         SSA_NAME_DEF_STMT (lhs) = new_stmt;
   11287                 :             :     }
   11288                 :             : }
   11289                 :             : 
   11290                 :             : static void
   11291                 :       84398 : vect_remove_slp_scalar_calls (vec_info *vinfo, slp_tree node)
   11292                 :             : {
   11293                 :       84398 :   hash_set<slp_tree> visited;
   11294                 :       84398 :   vect_remove_slp_scalar_calls (vinfo, node, visited);
   11295                 :       84398 : }
   11296                 :             : 
   11297                 :             : /* Vectorize the instance root.  */
   11298                 :             : 
   11299                 :             : void
   11300                 :        5608 : vectorize_slp_instance_root_stmt (vec_info *vinfo, slp_tree node, slp_instance instance)
   11301                 :             : {
   11302                 :        5608 :   gassign *rstmt = NULL;
   11303                 :             : 
   11304                 :        5608 :   if (instance->kind == slp_inst_kind_ctor)
   11305                 :             :     {
   11306                 :         248 :       if (SLP_TREE_NUMBER_OF_VEC_STMTS (node) == 1)
   11307                 :             :         {
   11308                 :         217 :           tree vect_lhs = SLP_TREE_VEC_DEFS (node)[0];
   11309                 :         217 :           tree root_lhs = gimple_get_lhs (instance->root_stmts[0]->stmt);
   11310                 :         217 :           if (!useless_type_conversion_p (TREE_TYPE (root_lhs),
   11311                 :         217 :                                           TREE_TYPE (vect_lhs)))
   11312                 :           0 :             vect_lhs = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (root_lhs),
   11313                 :             :                                vect_lhs);
   11314                 :         217 :           rstmt = gimple_build_assign (root_lhs, vect_lhs);
   11315                 :             :         }
   11316                 :          31 :       else if (SLP_TREE_NUMBER_OF_VEC_STMTS (node) > 1)
   11317                 :             :         {
   11318                 :          31 :           int nelts = SLP_TREE_NUMBER_OF_VEC_STMTS (node);
   11319                 :          31 :           tree child_def;
   11320                 :          31 :           int j;
   11321                 :          31 :           vec<constructor_elt, va_gc> *v;
   11322                 :          31 :           vec_alloc (v, nelts);
   11323                 :             : 
   11324                 :             :           /* A CTOR can handle V16HI composition from VNx8HI so we
   11325                 :             :              do not need to convert vector elements if the types
   11326                 :             :              do not match.  */
   11327                 :         125 :           FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (node), j, child_def)
   11328                 :          94 :             CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, child_def);
   11329                 :          31 :           tree lhs = gimple_get_lhs (instance->root_stmts[0]->stmt);
   11330                 :          31 :           tree rtype
   11331                 :          31 :             = TREE_TYPE (gimple_assign_rhs1 (instance->root_stmts[0]->stmt));
   11332                 :          31 :           tree r_constructor = build_constructor (rtype, v);
   11333                 :          31 :           rstmt = gimple_build_assign (lhs, r_constructor);
   11334                 :             :         }
   11335                 :             :     }
   11336                 :        5360 :   else if (instance->kind == slp_inst_kind_bb_reduc)
   11337                 :             :     {
   11338                 :             :       /* Largely inspired by reduction chain epilogue handling in
   11339                 :             :          vect_create_epilog_for_reduction.  */
   11340                 :        4055 :       vec<tree> vec_defs = vNULL;
   11341                 :        4055 :       vect_get_slp_defs (node, &vec_defs);
   11342                 :        4055 :       enum tree_code reduc_code
   11343                 :        4055 :         = gimple_assign_rhs_code (instance->root_stmts[0]->stmt);
   11344                 :             :       /* ???  We actually have to reflect signs somewhere.  */
   11345                 :        4055 :       if (reduc_code == MINUS_EXPR)
   11346                 :           0 :         reduc_code = PLUS_EXPR;
   11347                 :        4055 :       gimple_seq epilogue = NULL;
   11348                 :             :       /* We may end up with more than one vector result, reduce them
   11349                 :             :          to one vector.  */
   11350                 :        4055 :       tree vec_def = vec_defs[0];
   11351                 :        4055 :       tree vectype = TREE_TYPE (vec_def);
   11352                 :        4055 :       tree compute_vectype = vectype;
   11353                 :        4055 :       bool pun_for_overflow_p = (ANY_INTEGRAL_TYPE_P (vectype)
   11354                 :        3989 :                                  && TYPE_OVERFLOW_UNDEFINED (vectype)
   11355                 :        6957 :                                  && operation_can_overflow (reduc_code));
   11356                 :        2780 :       if (pun_for_overflow_p)
   11357                 :             :         {
   11358                 :        2780 :           compute_vectype = unsigned_type_for (vectype);
   11359                 :        2780 :           vec_def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
   11360                 :             :                                   compute_vectype, vec_def);
   11361                 :             :         }
   11362                 :        6424 :       for (unsigned i = 1; i < vec_defs.length (); ++i)
   11363                 :             :         {
   11364                 :        2369 :           tree def = vec_defs[i];
   11365                 :        2369 :           if (pun_for_overflow_p)
   11366                 :        2273 :             def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
   11367                 :             :                                 compute_vectype, def);
   11368                 :        2369 :           vec_def = gimple_build (&epilogue, reduc_code, compute_vectype,
   11369                 :             :                                   vec_def, def);
   11370                 :             :         }
   11371                 :        4055 :       vec_defs.release ();
   11372                 :             :       /* ???  Support other schemes than direct internal fn.  */
   11373                 :        4055 :       internal_fn reduc_fn;
   11374                 :        4055 :       if (!reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
   11375                 :        4055 :           || reduc_fn == IFN_LAST)
   11376                 :           0 :         gcc_unreachable ();
   11377                 :        4055 :       tree scalar_def = gimple_build (&epilogue, as_combined_fn (reduc_fn),
   11378                 :        4055 :                                       TREE_TYPE (compute_vectype), vec_def);
   11379                 :        4055 :       if (!SLP_INSTANCE_REMAIN_DEFS (instance).is_empty ())
   11380                 :             :         {
   11381                 :        2643 :           tree rem_def = NULL_TREE;
   11382                 :       12063 :           for (auto def : SLP_INSTANCE_REMAIN_DEFS (instance))
   11383                 :             :             {
   11384                 :        9420 :               def = gimple_convert (&epilogue, TREE_TYPE (scalar_def), def);
   11385                 :        9420 :               if (!rem_def)
   11386                 :             :                 rem_def = def;
   11387                 :             :               else
   11388                 :        6777 :                 rem_def = gimple_build (&epilogue, reduc_code,
   11389                 :        6777 :                                         TREE_TYPE (scalar_def),
   11390                 :             :                                         rem_def, def);
   11391                 :             :             }
   11392                 :        2643 :           scalar_def = gimple_build (&epilogue, reduc_code,
   11393                 :        2643 :                                      TREE_TYPE (scalar_def),
   11394                 :             :                                      scalar_def, rem_def);
   11395                 :             :         }
   11396                 :        4055 :       scalar_def = gimple_convert (&epilogue,
   11397                 :        4055 :                                    TREE_TYPE (vectype), scalar_def);
   11398                 :        4055 :       gimple_stmt_iterator rgsi = gsi_for_stmt (instance->root_stmts[0]->stmt);
   11399                 :        4055 :       gsi_insert_seq_before (&rgsi, epilogue, GSI_SAME_STMT);
   11400                 :        4055 :       gimple_assign_set_rhs_from_tree (&rgsi, scalar_def);
   11401                 :        4055 :       update_stmt (gsi_stmt (rgsi));
   11402                 :        4055 :       return;
   11403                 :             :     }
   11404                 :        1305 :   else if (instance->kind == slp_inst_kind_gcond)
   11405                 :             :     {
   11406                 :             :       /* Only support a single root for now as we can't codegen CFG yet and so we
   11407                 :             :          can't support lane > 1 at this time.  */
   11408                 :        1305 :       gcc_assert (instance->root_stmts.length () == 1);
   11409                 :        1305 :       auto root_stmt_info = instance->root_stmts[0];
   11410                 :        1305 :       auto last_stmt = STMT_VINFO_STMT (vect_orig_stmt (root_stmt_info));
   11411                 :        1305 :       gimple_stmt_iterator rgsi = gsi_for_stmt (last_stmt);
   11412                 :        1305 :       gimple *vec_stmt = NULL;
   11413                 :        1305 :       gcc_assert (!SLP_TREE_VEC_DEFS (node).is_empty ());
   11414                 :        1305 :       bool res = vectorizable_early_exit (vinfo, root_stmt_info, &rgsi,
   11415                 :             :                                           &vec_stmt, node, NULL);
   11416                 :        1305 :       gcc_assert (res);
   11417                 :        1305 :       return;
   11418                 :             :     }
   11419                 :             :   else
   11420                 :           0 :     gcc_unreachable ();
   11421                 :             : 
   11422                 :         248 :   gcc_assert (rstmt);
   11423                 :             : 
   11424                 :         248 :   gimple_stmt_iterator rgsi = gsi_for_stmt (instance->root_stmts[0]->stmt);
   11425                 :         248 :   gsi_replace (&rgsi, rstmt, true);
   11426                 :             : }
   11427                 :             : 
   11428                 :             : struct slp_scc_info
   11429                 :             : {
   11430                 :             :   bool on_stack;
   11431                 :             :   int dfs;
   11432                 :             :   int lowlink;
   11433                 :             : };
   11434                 :             : 
   11435                 :             : /* Schedule the SLP INSTANCE doing a DFS walk and collecting SCCs.  */
   11436                 :             : 
   11437                 :             : static void
   11438                 :     1394407 : vect_schedule_scc (vec_info *vinfo, slp_tree node, slp_instance instance,
   11439                 :             :                    hash_map<slp_tree, slp_scc_info> &scc_info,
   11440                 :             :                    int &maxdfs, vec<slp_tree> &stack)
   11441                 :             : {
   11442                 :     1394407 :   bool existed_p;
   11443                 :     1394407 :   slp_scc_info *info = &scc_info.get_or_insert (node, &existed_p);
   11444                 :     1394407 :   gcc_assert (!existed_p);
   11445                 :     1394407 :   info->dfs = maxdfs;
   11446                 :     1394407 :   info->lowlink = maxdfs;
   11447                 :     1394407 :   maxdfs++;
   11448                 :             : 
   11449                 :             :   /* Leaf.  */
   11450                 :     1394407 :   if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
   11451                 :             :     {
   11452                 :      477304 :       info->on_stack = false;
   11453                 :      477304 :       vect_schedule_slp_node (vinfo, node, instance);
   11454                 :      981981 :       return;
   11455                 :             :     }
   11456                 :             : 
   11457                 :      917103 :   info->on_stack = true;
   11458                 :      917103 :   stack.safe_push (node);
   11459                 :             : 
   11460                 :      917103 :   unsigned i;
   11461                 :      917103 :   slp_tree child;
   11462                 :             :   /* DFS recurse.  */
   11463                 :     1880282 :   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
   11464                 :             :     {
   11465                 :      963179 :       if (!child)
   11466                 :       52051 :         continue;
   11467                 :      911128 :       slp_scc_info *child_info = scc_info.get (child);
   11468                 :      911128 :       if (!child_info)
   11469                 :             :         {
   11470                 :      833759 :           vect_schedule_scc (vinfo, child, instance, scc_info, maxdfs, stack);
   11471                 :             :           /* Recursion might have re-allocated the node.  */
   11472                 :      833759 :           info = scc_info.get (node);
   11473                 :      833759 :           child_info = scc_info.get (child);
   11474                 :      833759 :           info->lowlink = MIN (info->lowlink, child_info->lowlink);
   11475                 :             :         }
   11476                 :       77369 :       else if (child_info->on_stack)
   11477                 :       23345 :         info->lowlink = MIN (info->lowlink, child_info->dfs);
   11478                 :             :     }
   11479                 :      917103 :   if (info->lowlink != info->dfs)
   11480                 :             :     return;
   11481                 :             : 
   11482                 :      889730 :   auto_vec<slp_tree, 4> phis_to_fixup;
   11483                 :             : 
   11484                 :             :   /* Singleton.  */
   11485                 :      889730 :   if (stack.last () == node)
   11486                 :             :     {
   11487                 :      867449 :       stack.pop ();
   11488                 :      867449 :       info->on_stack = false;
   11489                 :      867449 :       vect_schedule_slp_node (vinfo, node, instance);
   11490                 :      867449 :       if (SLP_TREE_CODE (node) != VEC_PERM_EXPR
   11491                 :      867449 :           && is_a <gphi *> (SLP_TREE_REPRESENTATIVE (node)->stmt))
   11492                 :       28633 :         phis_to_fixup.quick_push (node);
   11493                 :             :     }
   11494                 :             :   else
   11495                 :             :     {
   11496                 :             :       /* SCC.  */
   11497                 :       22281 :       int last_idx = stack.length () - 1;
   11498                 :       49654 :       while (stack[last_idx] != node)
   11499                 :       27373 :         last_idx--;
   11500                 :             :       /* We can break the cycle at PHIs who have at least one child
   11501                 :             :          code generated.  Then we could re-start the DFS walk until
   11502                 :             :          all nodes in the SCC are covered (we might have new entries
   11503                 :             :          for only back-reachable nodes).  But it's simpler to just
   11504                 :             :          iterate and schedule those that are ready.  */
   11505                 :       22281 :       unsigned todo = stack.length () - last_idx;
   11506                 :       22434 :       do
   11507                 :             :         {
   11508                 :       95218 :           for (int idx = stack.length () - 1; idx >= last_idx; --idx)
   11509                 :             :             {
   11510                 :       50350 :               slp_tree entry = stack[idx];
   11511                 :       50350 :               if (!entry)
   11512                 :         378 :                 continue;
   11513                 :       49972 :               bool phi = (SLP_TREE_CODE (entry) != VEC_PERM_EXPR
   11514                 :       49972 :                           && is_a <gphi *> (SLP_TREE_REPRESENTATIVE (entry)->stmt));
   11515                 :       49972 :               bool ready = !phi;
   11516                 :      125446 :               FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (entry), i, child)
   11517                 :       98471 :                   if (!child)
   11518                 :             :                     {
   11519                 :       21668 :                       gcc_assert (phi);
   11520                 :             :                       ready = true;
   11521                 :             :                       break;
   11522                 :             :                     }
   11523                 :       76803 :                   else if (scc_info.get (child)->on_stack)
   11524                 :             :                     {
   11525                 :       22000 :                       if (!phi)
   11526                 :             :                         {
   11527                 :             :                           ready = false;
   11528                 :             :                           break;
   11529                 :             :                         }
   11530                 :             :                     }
   11531                 :             :                   else
   11532                 :             :                     {
   11533                 :       54803 :                       if (phi)
   11534                 :             :                         {
   11535                 :             :                           ready = true;
   11536                 :             :                           break;
   11537                 :             :                         }
   11538                 :             :                     }
   11539                 :       28304 :               if (ready)
   11540                 :             :                 {
   11541                 :       49654 :                   vect_schedule_slp_node (vinfo, entry, instance);
   11542                 :       49654 :                   scc_info.get (entry)->on_stack = false;
   11543                 :       49654 :                   stack[idx] = NULL;
   11544                 :       49654 :                   todo--;
   11545                 :       49654 :                   if (phi)
   11546                 :       22703 :                     phis_to_fixup.safe_push (entry);
   11547                 :             :                 }
   11548                 :             :             }
   11549                 :             :         }
   11550                 :       22434 :       while (todo != 0);
   11551                 :             : 
   11552                 :             :       /* Pop the SCC.  */
   11553                 :       22281 :       stack.truncate (last_idx);
   11554                 :             :     }
   11555                 :             : 
   11556                 :             :   /* Now fixup the backedge def of the vectorized PHIs in this SCC.  */
   11557                 :             :   slp_tree phi_node;
   11558                 :     1830796 :   FOR_EACH_VEC_ELT (phis_to_fixup, i, phi_node)
   11559                 :             :     {
   11560                 :       51336 :       gphi *phi = as_a <gphi *> (SLP_TREE_REPRESENTATIVE (phi_node)->stmt);
   11561                 :       51336 :       edge_iterator ei;
   11562                 :       51336 :       edge e;
   11563                 :      159278 :       FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
   11564                 :             :         {
   11565                 :      107942 :           unsigned dest_idx = e->dest_idx;
   11566                 :      107942 :           child = SLP_TREE_CHILDREN (phi_node)[dest_idx];
   11567                 :      107942 :           if (!child || SLP_TREE_DEF_TYPE (child) != vect_internal_def)
   11568                 :       61535 :             continue;
   11569                 :       46407 :           unsigned n = SLP_TREE_VEC_DEFS (phi_node).length ();
   11570                 :             :           /* Simply fill all args.  */
   11571                 :       46407 :           if (STMT_VINFO_DEF_TYPE (SLP_TREE_REPRESENTATIVE (phi_node))
   11572                 :             :               != vect_first_order_recurrence)
   11573                 :       95685 :             for (unsigned i = 0; i < n; ++i)
   11574                 :             :               {
   11575                 :       49310 :                 tree phidef = SLP_TREE_VEC_DEFS (phi_node)[i];
   11576                 :       49310 :                 gphi *phi = as_a <gphi *> (SSA_NAME_DEF_STMT (phidef));
   11577                 :       49310 :                 add_phi_arg (phi, vect_get_slp_vect_def (child, i),
   11578                 :             :                              e, gimple_phi_arg_location (phi, dest_idx));
   11579                 :             :               }
   11580                 :             :           else
   11581                 :             :             {
   11582                 :             :               /* Unless it is a first order recurrence which needs
   11583                 :             :                  args filled in for both the PHI node and the permutes.  */
   11584                 :          32 :               gimple *perm
   11585                 :          32 :                 = SSA_NAME_DEF_STMT (SLP_TREE_VEC_DEFS (phi_node)[0]);
   11586                 :          32 :               gimple *rphi = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (perm));
   11587                 :          32 :               add_phi_arg (as_a <gphi *> (rphi),
   11588                 :             :                            vect_get_slp_vect_def (child, n - 1),
   11589                 :             :                            e, gimple_phi_arg_location (phi, dest_idx));
   11590                 :          84 :               for (unsigned i = 0; i < n; ++i)
   11591                 :             :                 {
   11592                 :          52 :                   gimple *perm
   11593                 :          52 :                     = SSA_NAME_DEF_STMT (SLP_TREE_VEC_DEFS (phi_node)[i]);
   11594                 :          52 :                   if (i > 0)
   11595                 :          20 :                     gimple_assign_set_rhs1 (perm,
   11596                 :             :                                             vect_get_slp_vect_def (child, i - 1));
   11597                 :          52 :                   gimple_assign_set_rhs2 (perm,
   11598                 :             :                                           vect_get_slp_vect_def (child, i));
   11599                 :          52 :                   update_stmt (perm);
   11600                 :             :                 }
   11601                 :             :             }
   11602                 :             :         }
   11603                 :             :     }
   11604                 :      889730 : }
   11605                 :             : 
   11606                 :             : /* Generate vector code for SLP_INSTANCES in the loop/basic block.  */
   11607                 :             : 
   11608                 :             : void
   11609                 :      524801 : vect_schedule_slp (vec_info *vinfo, const vec<slp_instance> &slp_instances)
   11610                 :             : {
   11611                 :      524801 :   slp_instance instance;
   11612                 :      524801 :   unsigned int i;
   11613                 :             : 
   11614                 :      524801 :   hash_map<slp_tree, slp_scc_info> scc_info;
   11615                 :      524801 :   int maxdfs = 0;
   11616                 :     1085613 :   FOR_EACH_VEC_ELT (slp_instances, i, instance)
   11617                 :             :     {
   11618                 :      560812 :       slp_tree node = SLP_INSTANCE_TREE (instance);
   11619                 :      560812 :       if (dump_enabled_p ())
   11620                 :             :         {
   11621                 :       15260 :           dump_printf_loc (MSG_NOTE, vect_location,
   11622                 :             :                            "Vectorizing SLP tree:\n");
   11623                 :             :           /* ???  Dump all?  */
   11624                 :       15260 :           if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
   11625                 :         425 :             dump_printf_loc (MSG_NOTE, vect_location, "Root stmt: %G",
   11626                 :         425 :                          SLP_INSTANCE_ROOT_STMTS (instance)[0]->stmt);
   11627                 :       15260 :           vect_print_slp_graph (MSG_NOTE, vect_location,
   11628                 :             :                                 SLP_INSTANCE_TREE (instance));
   11629                 :             :         }
   11630                 :             :       /* Schedule the tree of INSTANCE, scheduling SCCs in a way to
   11631                 :             :          have a PHI be the node breaking the cycle.  */
   11632                 :      560812 :       auto_vec<slp_tree> stack;
   11633                 :      560812 :       if (!scc_info.get (node))
   11634                 :      560648 :         vect_schedule_scc (vinfo, node, instance, scc_info, maxdfs, stack);
   11635                 :             : 
   11636                 :      560812 :       if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
   11637                 :        5608 :         vectorize_slp_instance_root_stmt (vinfo, node, instance);
   11638                 :             : 
   11639                 :      560812 :       if (dump_enabled_p ())
   11640                 :       15260 :         dump_printf_loc (MSG_NOTE, vect_location,
   11641                 :             :                          "vectorizing stmts using SLP.\n");
   11642                 :      560812 :     }
   11643                 :             : 
   11644                 :     1610414 :   FOR_EACH_VEC_ELT (slp_instances, i, instance)
   11645                 :             :     {
   11646                 :      560812 :       slp_tree root = SLP_INSTANCE_TREE (instance);
   11647                 :      560812 :       stmt_vec_info store_info;
   11648                 :      560812 :       unsigned int j;
   11649                 :             : 
   11650                 :             :       /* Remove scalar call stmts.  Do not do this for basic-block
   11651                 :             :          vectorization as not all uses may be vectorized.
   11652                 :             :          ???  Why should this be necessary?  DCE should be able to
   11653                 :             :          remove the stmts itself.
   11654                 :             :          ???  For BB vectorization we can as well remove scalar
   11655                 :             :          stmts starting from the SLP tree root if they have no
   11656                 :             :          uses.  */
   11657                 :      560812 :       if (is_a <loop_vec_info> (vinfo))
   11658                 :       84398 :         vect_remove_slp_scalar_calls (vinfo, root);
   11659                 :             : 
   11660                 :             :       /* Remove vectorized stores original scalar stmts.  */
   11661                 :     2503723 :       for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store_info); j++)
   11662                 :             :         {
   11663                 :     1413852 :           if (!STMT_VINFO_DATA_REF (store_info)
   11664                 :     1386015 :               || !DR_IS_WRITE (STMT_VINFO_DATA_REF (store_info)))
   11665                 :             :             break;
   11666                 :             : 
   11667                 :     1382099 :           store_info = vect_orig_stmt (store_info);
   11668                 :             :           /* Free the attached stmt_vec_info and remove the stmt.  */
   11669                 :     1382099 :           vinfo->remove_stmt (store_info);
   11670                 :             : 
   11671                 :             :           /* Invalidate SLP_TREE_REPRESENTATIVE in case we released it
   11672                 :             :              to not crash in vect_free_slp_tree later.  */
   11673                 :     1382099 :           if (SLP_TREE_REPRESENTATIVE (root) == store_info)
   11674                 :      528824 :             SLP_TREE_REPRESENTATIVE (root) = NULL;
   11675                 :             :         }
   11676                 :             :     }
   11677                 :      524801 : }
        

Generated by: LCOV version 2.1-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.