LCOV - code coverage report
Current view: top level - gcc - tree-vect-stmts.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 80.0 % 7269 5813
Test Date: 2026-05-30 15:37:04 Functions: 88.8 % 107 95
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Statement Analysis and Transformation for Vectorization
       2              :    Copyright (C) 2003-2026 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              : #include "system.h"
      24              : #include "coretypes.h"
      25              : #include "backend.h"
      26              : #include "target.h"
      27              : #include "rtl.h"
      28              : #include "tree.h"
      29              : #include "gimple.h"
      30              : #include "ssa.h"
      31              : #include "optabs-tree.h"
      32              : #include "insn-config.h"
      33              : #include "recog.h"            /* FIXME: for insn_data */
      34              : #include "cgraph.h"
      35              : #include "dumpfile.h"
      36              : #include "alias.h"
      37              : #include "fold-const.h"
      38              : #include "stor-layout.h"
      39              : #include "tree-eh.h"
      40              : #include "gimplify.h"
      41              : #include "gimple-iterator.h"
      42              : #include "gimplify-me.h"
      43              : #include "tree-cfg.h"
      44              : #include "tree-ssa-loop-manip.h"
      45              : #include "cfgloop.h"
      46              : #include "explow.h"
      47              : #include "tree-ssa-loop.h"
      48              : #include "tree-scalar-evolution.h"
      49              : #include "tree-vectorizer.h"
      50              : #include "builtins.h"
      51              : #include "internal-fn.h"
      52              : #include "tree-vector-builder.h"
      53              : #include "vec-perm-indices.h"
      54              : #include "gimple-range.h"
      55              : #include "tree-ssa-loop-niter.h"
      56              : #include "gimple-fold.h"
      57              : #include "regs.h"
      58              : #include "attribs.h"
      59              : #include "optabs-libfuncs.h"
      60              : #include "tree-dfa.h"
      61              : 
      62              : /* For lang_hooks.types.type_for_mode.  */
      63              : #include "langhooks.h"
      64              : 
      65              : static tree vector_vector_composition_type (tree, poly_uint64, tree *,
      66              :                                             bool = false);
      67              : 
      68              : /* Return TRUE iff the given statement is in an inner loop relative to
      69              :    the loop being vectorized.  */
      70              : bool
      71      5835096 : stmt_in_inner_loop_p (vec_info *vinfo, class _stmt_vec_info *stmt_info)
      72              : {
      73      5835096 :   gimple *stmt = STMT_VINFO_STMT (stmt_info);
      74      5835096 :   basic_block bb = gimple_bb (stmt);
      75      5835096 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
      76      2760714 :   class loop* loop;
      77              : 
      78      2760714 :   if (!loop_vinfo)
      79              :     return false;
      80              : 
      81      2760714 :   loop = LOOP_VINFO_LOOP (loop_vinfo);
      82              : 
      83      2760714 :   return (bb->loop_father == loop->inner);
      84              : }
      85              : 
      86              : /* Record the cost of a statement, either by directly informing the
      87              :    target model or by saving it in a vector for later processing.
      88              :    Return a preliminary estimate of the statement's cost.  */
      89              : 
      90              : unsigned
      91      8893358 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
      92              :                   enum vect_cost_for_stmt kind,
      93              :                   stmt_vec_info stmt_info, slp_tree node,
      94              :                   tree vectype, int misalign,
      95              :                   enum vect_cost_model_location where)
      96              : {
      97      8893358 :   if ((kind == vector_load || kind == unaligned_load)
      98      1587527 :       && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
      99              :     kind = vector_gather_load;
     100      8893358 :   if ((kind == vector_store || kind == unaligned_store)
     101      1021614 :       && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
     102      8893358 :     kind = vector_scatter_store;
     103              : 
     104      8893358 :   stmt_info_for_cost si
     105      8893358 :     = { count, kind, where, stmt_info, node, vectype, misalign };
     106      8893358 :   body_cost_vec->safe_push (si);
     107              : 
     108      8893358 :   return (unsigned)
     109      8893358 :       (builtin_vectorization_cost (kind, vectype, misalign) * count);
     110              : }
     111              : 
     112              : unsigned
     113      3994565 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
     114              :                   enum vect_cost_for_stmt kind, stmt_vec_info stmt_info,
     115              :                   tree vectype, int misalign,
     116              :                   enum vect_cost_model_location where)
     117              : {
     118      3994565 :   return record_stmt_cost (body_cost_vec, count, kind, stmt_info, NULL,
     119      3994565 :                            vectype, misalign, where);
     120              : }
     121              : 
     122              : unsigned
     123      1815767 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
     124              :                   enum vect_cost_for_stmt kind, slp_tree node,
     125              :                   tree vectype, int misalign,
     126              :                   enum vect_cost_model_location where)
     127              : {
     128      1815767 :   return record_stmt_cost (body_cost_vec, count, kind,
     129              :                            SLP_TREE_REPRESENTATIVE (node), node,
     130      1815767 :                            vectype, misalign, where);
     131              : }
     132              : 
     133              : unsigned
     134            0 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
     135              :                   enum vect_cost_for_stmt kind,
     136              :                   enum vect_cost_model_location where)
     137              : {
     138            0 :   gcc_assert (kind == cond_branch_taken || kind == cond_branch_not_taken
     139              :               || kind == scalar_stmt);
     140            0 :   return record_stmt_cost (body_cost_vec, count, kind, NULL, NULL,
     141            0 :                            NULL_TREE, 0, where);
     142              : }
     143              : 
     144              : /* Return a variable of type ELEM_TYPE[NELEMS].  */
     145              : 
     146              : static tree
     147            0 : create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
     148              : {
     149            0 :   return create_tmp_var (build_array_type_nelts (elem_type, nelems),
     150            0 :                          "vect_array");
     151              : }
     152              : 
     153              : /* ARRAY is an array of vectors created by create_vector_array.
     154              :    Return an SSA_NAME for the vector in index N.  The reference
     155              :    is part of the vectorization of STMT_INFO and the vector is associated
     156              :    with scalar destination SCALAR_DEST.
     157              :    If we need to ensure that inactive elements are set to zero,
     158              :    NEED_ZEROING is true, MASK contains the loop mask to be used.  */
     159              : 
     160              : static tree
     161            0 : read_vector_array (vec_info *vinfo,
     162              :                    stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
     163              :                    tree scalar_dest, tree array, unsigned HOST_WIDE_INT n,
     164              :                    bool need_zeroing, tree mask)
     165              : {
     166            0 :   tree vect_type, vect, vect_name, tmp, tmp_name, array_ref;
     167            0 :   gimple *new_stmt;
     168              : 
     169            0 :   gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
     170            0 :   vect_type = TREE_TYPE (TREE_TYPE (array));
     171            0 :   tmp = vect_create_destination_var (scalar_dest, vect_type);
     172            0 :   vect = vect_create_destination_var (scalar_dest, vect_type);
     173            0 :   array_ref = build4 (ARRAY_REF, vect_type, array,
     174            0 :                       build_int_cst (size_type_node, n),
     175              :                       NULL_TREE, NULL_TREE);
     176              : 
     177            0 :   new_stmt = gimple_build_assign (tmp, array_ref);
     178            0 :   tmp_name = make_ssa_name (vect, new_stmt);
     179            0 :   gimple_assign_set_lhs (new_stmt, tmp_name);
     180            0 :   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
     181              : 
     182            0 :   if (need_zeroing)
     183              :     {
     184            0 :       tree vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
     185              :                                               vect_type);
     186            0 :       vect_name = make_ssa_name (vect, new_stmt);
     187            0 :       new_stmt
     188            0 :         = gimple_build_assign (vect_name, VEC_COND_EXPR,
     189              :                                mask, tmp_name, vec_els);
     190            0 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
     191              :     }
     192              :   else
     193              :     vect_name = tmp_name;
     194              : 
     195            0 :   return vect_name;
     196              : }
     197              : 
     198              : /* ARRAY is an array of vectors created by create_vector_array.
     199              :    Emit code to store SSA_NAME VECT in index N of the array.
     200              :    The store is part of the vectorization of STMT_INFO.  */
     201              : 
     202              : static void
     203            0 : write_vector_array (vec_info *vinfo,
     204              :                     stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
     205              :                     tree vect, tree array, unsigned HOST_WIDE_INT n)
     206              : {
     207            0 :   tree array_ref;
     208            0 :   gimple *new_stmt;
     209              : 
     210            0 :   array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
     211            0 :                       build_int_cst (size_type_node, n),
     212              :                       NULL_TREE, NULL_TREE);
     213              : 
     214            0 :   new_stmt = gimple_build_assign (array_ref, vect);
     215            0 :   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
     216            0 : }
     217              : 
     218              : /* PTR is a pointer to an array of type TYPE.  Return a representation
     219              :    of *PTR.  The memory reference replaces those in FIRST_DR
     220              :    (and its group).  */
     221              : 
     222              : static tree
     223            0 : create_array_ref (tree type, tree ptr, tree alias_ptr_type)
     224              : {
     225            0 :   tree mem_ref;
     226              : 
     227            0 :   mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
     228              :   /* Arrays have the same alignment as their type.  */
     229            0 :   set_ptr_info_alignment (get_ptr_info (ptr), TYPE_ALIGN_UNIT (type), 0);
     230            0 :   return mem_ref;
     231              : }
     232              : 
     233              : /* Add a clobber of variable VAR to the vectorization of STMT_INFO.
     234              :    Emit the clobber before *GSI.  */
     235              : 
     236              : static void
     237           15 : vect_clobber_variable (vec_info *vinfo, stmt_vec_info stmt_info,
     238              :                        gimple_stmt_iterator *gsi, tree var)
     239              : {
     240           15 :   tree clobber = build_clobber (TREE_TYPE (var));
     241           15 :   gimple *new_stmt = gimple_build_assign (var, clobber);
     242           15 :   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
     243           15 : }
     244              : 
     245              : /* Utility functions used by vect_mark_stmts_to_be_vectorized.  */
     246              : 
     247              : /* Function vect_mark_relevant.
     248              : 
     249              :    Mark STMT_INFO as "relevant for vectorization" and add it to WORKLIST.  */
     250              : 
     251              : static void
     252      3206349 : vect_mark_relevant (vec<stmt_vec_info> *worklist, stmt_vec_info stmt_info,
     253              :                     enum vect_relevant relevant, bool live_p)
     254              : {
     255      3206349 :   enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
     256      3206349 :   bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
     257              : 
     258      3206349 :   if (dump_enabled_p ())
     259       163433 :     dump_printf_loc (MSG_NOTE, vect_location,
     260              :                      "mark relevant %d, live %d: %G", relevant, live_p,
     261              :                      stmt_info->stmt);
     262              : 
     263              :   /* If this stmt is an original stmt in a pattern, we might need to mark its
     264              :      related pattern stmt instead of the original stmt.  However, such stmts
     265              :      may have their own uses that are not in any pattern, in such cases the
     266              :      stmt itself should be marked.  */
     267      3206349 :   if (STMT_VINFO_IN_PATTERN_P (stmt_info))
     268              :     {
     269              :       /* This is the last stmt in a sequence that was detected as a
     270              :          pattern that can potentially be vectorized.  Don't mark the stmt
     271              :          as relevant/live because it's not going to be vectorized.
     272              :          Instead mark the pattern-stmt that replaces it.  */
     273              : 
     274       242512 :       if (dump_enabled_p ())
     275         2801 :         dump_printf_loc (MSG_NOTE, vect_location,
     276              :                          "last stmt in pattern. don't mark"
     277              :                          " relevant/live.\n");
     278              : 
     279       242512 :       stmt_vec_info old_stmt_info = stmt_info;
     280       242512 :       stmt_info = STMT_VINFO_RELATED_STMT (stmt_info);
     281       242512 :       gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == old_stmt_info);
     282       242512 :       save_relevant = STMT_VINFO_RELEVANT (stmt_info);
     283       242512 :       save_live_p = STMT_VINFO_LIVE_P (stmt_info);
     284              : 
     285       242512 :       if (live_p && relevant == vect_unused_in_scope)
     286              :         {
     287          110 :           if (dump_enabled_p ())
     288           10 :             dump_printf_loc (MSG_NOTE, vect_location,
     289              :                              "vec_stmt_relevant_p: forcing live pattern stmt "
     290              :                              "relevant.\n");
     291              :           relevant = vect_used_only_live;
     292              :         }
     293              : 
     294       242512 :       if (dump_enabled_p ())
     295         2801 :         dump_printf_loc (MSG_NOTE, vect_location,
     296              :                          "mark relevant %d, live %d: %G", relevant, live_p,
     297              :                          stmt_info->stmt);
     298              :     }
     299              : 
     300      3206349 :   STMT_VINFO_LIVE_P (stmt_info) |= live_p;
     301      3206349 :   if (relevant > STMT_VINFO_RELEVANT (stmt_info))
     302      2876363 :     STMT_VINFO_RELEVANT (stmt_info) = relevant;
     303              : 
     304      3206349 :   if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
     305       329986 :       && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
     306              :     {
     307       329278 :       if (dump_enabled_p ())
     308        19470 :         dump_printf_loc (MSG_NOTE, vect_location,
     309              :                          "already marked relevant/live.\n");
     310       329278 :       return;
     311              :     }
     312              : 
     313      2877071 :   worklist->safe_push (stmt_info);
     314              : }
     315              : 
     316              : 
     317              : /* Function is_simple_and_all_uses_invariant
     318              : 
     319              :    Return true if STMT_INFO is simple and all uses of it are invariant.  */
     320              : 
     321              : bool
     322       248529 : is_simple_and_all_uses_invariant (stmt_vec_info stmt_info,
     323              :                                   loop_vec_info loop_vinfo)
     324              : {
     325       248529 :   tree op;
     326       248529 :   ssa_op_iter iter;
     327              : 
     328       442558 :   gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
     329       194847 :   if (!stmt)
     330              :     return false;
     331              : 
     332       202394 :   FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
     333              :     {
     334       201576 :       enum vect_def_type dt = vect_uninitialized_def;
     335              : 
     336       201576 :       if (!vect_is_simple_use (op, loop_vinfo, &dt))
     337              :         {
     338         5344 :           if (dump_enabled_p ())
     339           16 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
     340              :                              "use not simple.\n");
     341       194029 :           return false;
     342              :         }
     343              : 
     344       196232 :       if (dt != vect_external_def && dt != vect_constant_def)
     345              :         return false;
     346              :     }
     347              :   return true;
     348              : }
     349              : 
     350              : /* Function vect_stmt_relevant_p.
     351              : 
     352              :    Return true if STMT_INFO, in the loop that is represented by LOOP_VINFO,
     353              :    is "relevant for vectorization".
     354              : 
     355              :    A stmt is considered "relevant for vectorization" if:
     356              :    - it has uses outside the loop.
     357              :    - it has vdefs (it alters memory).
     358              :    - control stmts in the loop (except for the exit condition).
     359              : 
     360              :    CHECKME: what other side effects would the vectorizer allow?  */
     361              : 
     362              : static bool
     363      5156499 : vect_stmt_relevant_p (stmt_vec_info stmt_info, loop_vec_info loop_vinfo,
     364              :                       enum vect_relevant *relevant, bool *live_p)
     365              : {
     366      5156499 :   class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
     367      5156499 :   ssa_op_iter op_iter;
     368      5156499 :   imm_use_iterator imm_iter;
     369      5156499 :   use_operand_p use_p;
     370      5156499 :   def_operand_p def_p;
     371              : 
     372      5156499 :   *relevant = vect_unused_in_scope;
     373      5156499 :   *live_p = false;
     374              : 
     375              :   /* cond stmt other than loop exit cond.  */
     376      5156499 :   gimple *stmt = STMT_VINFO_STMT (stmt_info);
     377      5156499 :   if (is_ctrl_stmt (stmt)
     378       605475 :       && LOOP_VINFO_LOOP_IV_COND (loop_vinfo) != stmt
     379      5385630 :       && (!loop->inner || gimple_bb (stmt)->loop_father == loop))
     380       227144 :     *relevant = vect_used_in_scope;
     381              : 
     382              :   /* changing memory.  */
     383      5156499 :   if (gimple_code (stmt_info->stmt) != GIMPLE_PHI)
     384      4276890 :     if (gimple_vdef (stmt_info->stmt)
     385      3671415 :         && !gimple_clobber_p (stmt_info->stmt))
     386              :       {
     387       368987 :         if (dump_enabled_p ())
     388        27840 :           dump_printf_loc (MSG_NOTE, vect_location,
     389              :                            "vec_stmt_relevant_p: stmt has vdefs.\n");
     390       368987 :         *relevant = vect_used_in_scope;
     391       368987 :         if (! STMT_VINFO_DATA_REF (stmt_info)
     392       368987 :             && zero_ssa_operands (stmt_info->stmt, SSA_OP_DEF))
     393           20 :           LOOP_VINFO_ALTERNATE_DEFS (loop_vinfo).safe_push (stmt_info);
     394              :       }
     395              : 
     396              :   /* uses outside the loop.  */
     397     14488554 :   FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt_info->stmt, op_iter, SSA_OP_DEF)
     398              :     {
     399     15350399 :       FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
     400              :         {
     401      6999287 :           basic_block bb = gimple_bb (USE_STMT (use_p));
     402      6999287 :           if (!flow_bb_inside_loop_p (loop, bb))
     403              :             {
     404       263291 :               if (is_gimple_debug (USE_STMT (use_p)))
     405         1075 :                 continue;
     406              : 
     407       262216 :               if (dump_enabled_p ())
     408         5968 :                 dump_printf_loc (MSG_NOTE, vect_location,
     409              :                                  "vec_stmt_relevant_p: used out of loop.\n");
     410              : 
     411              :               /* We expect all such uses to be in the loop exit phis
     412              :                  (because of loop closed form)   */
     413       262216 :               gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
     414              : 
     415       262216 :               *live_p = true;
     416       262216 :               LOOP_VINFO_EARLY_BRK_NEEDS_EPILOG (loop_vinfo) = true;
     417              :             }
     418      4175556 :         }
     419              :     }
     420              : 
     421       248531 :   if (*live_p && *relevant == vect_unused_in_scope
     422      5405028 :       && !is_simple_and_all_uses_invariant (stmt_info, loop_vinfo))
     423              :     {
     424       247711 :       if (dump_enabled_p ())
     425         5824 :         dump_printf_loc (MSG_NOTE, vect_location,
     426              :                          "vec_stmt_relevant_p: stmt live but not relevant.\n");
     427       247711 :       *relevant = vect_used_only_live;
     428              :     }
     429              : 
     430      5156499 :   return (*live_p || *relevant);
     431              : }
     432              : 
     433              : 
     434              : /* Function exist_non_indexing_operands_for_use_p
     435              : 
     436              :    USE is one of the uses attached to STMT_INFO.  Check if USE is
     437              :    used in STMT_INFO for anything other than indexing an array.  */
     438              : 
     439              : static bool
     440      4298293 : exist_non_indexing_operands_for_use_p (tree use, stmt_vec_info stmt_info)
     441              : {
     442      4298293 :   tree operand;
     443              : 
     444              :   /* USE corresponds to some operand in STMT.  If there is no data
     445              :      reference in STMT, then any operand that corresponds to USE
     446              :      is not indexing an array.  */
     447      4298293 :   if (!STMT_VINFO_DATA_REF (stmt_info))
     448              :     return true;
     449              : 
     450              :   /* STMT has a data_ref. FORNOW this means that its of one of
     451              :      the following forms:
     452              :      -1- ARRAY_REF = var
     453              :      -2- var = ARRAY_REF
     454              :      (This should have been verified in analyze_data_refs).
     455              : 
     456              :      'var' in the second case corresponds to a def, not a use,
     457              :      so USE cannot correspond to any operands that are not used
     458              :      for array indexing.
     459              : 
     460              :      Therefore, all we need to check is if STMT falls into the
     461              :      first case, and whether var corresponds to USE.  */
     462              : 
     463      1467629 :   gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
     464      1449399 :   if (!assign || !gimple_assign_copy_p (assign))
     465              :     {
     466       786125 :       gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
     467        18230 :       if (call && gimple_call_internal_p (call))
     468              :         {
     469        18230 :           internal_fn ifn = gimple_call_internal_fn (call);
     470        18230 :           int mask_index = internal_fn_mask_index (ifn);
     471        18230 :           if (mask_index >= 0
     472        18230 :               && use == gimple_call_arg (call, mask_index))
     473              :             return true;
     474        11833 :           int els_index = internal_fn_else_index (ifn);
     475        11833 :           if (els_index >= 0
     476        11833 :               && use == gimple_call_arg (call, els_index))
     477              :             return true;
     478        10328 :           int stored_value_index = internal_fn_stored_value_index (ifn);
     479        10328 :           if (stored_value_index >= 0
     480        10328 :               && use == gimple_call_arg (call, stored_value_index))
     481              :             return true;
     482         8098 :           if (internal_gather_scatter_fn_p (ifn)
     483         8098 :               && use == gimple_call_arg (call, 1))
     484              :             return true;
     485              :         }
     486       775993 :       return false;
     487              :     }
     488              : 
     489       681504 :   if (TREE_CODE (gimple_assign_lhs (assign)) == SSA_NAME)
     490              :     return false;
     491       681504 :   operand = gimple_assign_rhs1 (assign);
     492       681504 :   if (TREE_CODE (operand) != SSA_NAME)
     493              :     return false;
     494              : 
     495       590306 :   if (operand == use)
     496              :     return true;
     497              : 
     498              :   return false;
     499              : }
     500              : 
     501              : 
     502              : /*
     503              :    Function process_use.
     504              : 
     505              :    Inputs:
     506              :    - a USE in STMT_VINFO in a loop represented by LOOP_VINFO
     507              :    - RELEVANT - enum value to be set in the STMT_VINFO of the stmt
     508              :      that defined USE.  This is done by calling mark_relevant and passing it
     509              :      the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
     510              :    - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
     511              :      be performed.
     512              : 
     513              :    Outputs:
     514              :    Generally, LIVE_P and RELEVANT are used to define the liveness and
     515              :    relevance info of the DEF_STMT of this USE:
     516              :        STMT_VINFO_LIVE_P (DEF_stmt_vinfo) <-- live_p
     517              :        STMT_VINFO_RELEVANT (DEF_stmt_vinfo) <-- relevant
     518              :    Exceptions:
     519              :    - case 1: If USE is used only for address computations (e.g. array indexing),
     520              :    which does not need to be directly vectorized, then the liveness/relevance
     521              :    of the respective DEF_STMT is left unchanged.
     522              :    - case 2: If STMT_VINFO is a reduction phi and DEF_STMT is a reduction stmt,
     523              :    we skip DEF_STMT cause it had already been processed.
     524              :    - case 3: If DEF_STMT and STMT_VINFO are in different nests, then
     525              :    "relevant" will be modified accordingly.
     526              : 
     527              :    Return true if everything is as expected. Return false otherwise.  */
     528              : 
     529              : static opt_result
     530      4354138 : process_use (stmt_vec_info stmt_vinfo, tree use, loop_vec_info loop_vinfo,
     531              :              enum vect_relevant relevant, vec<stmt_vec_info> *worklist,
     532              :              bool force)
     533              : {
     534      4354138 :   stmt_vec_info dstmt_vinfo;
     535      4354138 :   enum vect_def_type dt;
     536              : 
     537              :   /* case 1: we are only interested in uses that need to be vectorized.  Uses
     538              :      that are used for address computation are not considered relevant.  */
     539      4354138 :   if (!force && !exist_non_indexing_operands_for_use_p (use, stmt_vinfo))
     540      1178760 :     return opt_result::success ();
     541              : 
     542      3175378 :   if (!vect_is_simple_use (use, loop_vinfo, &dt, &dstmt_vinfo))
     543        34799 :     return opt_result::failure_at (stmt_vinfo->stmt,
     544              :                                    "not vectorized:"
     545              :                                    " unsupported use in stmt.\n");
     546              : 
     547      3140579 :   if (!dstmt_vinfo)
     548       590855 :     return opt_result::success ();
     549              : 
     550      2549724 :   basic_block def_bb = gimple_bb (dstmt_vinfo->stmt);
     551      2549724 :   basic_block bb = gimple_bb (stmt_vinfo->stmt);
     552              : 
     553              :   /* case 2: A reduction phi (STMT) defined by a reduction stmt (DSTMT_VINFO).
     554              :      We have to force the stmt live since the epilogue loop needs it to
     555              :      continue computing the reduction.  */
     556      2549724 :   if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
     557       268775 :       && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
     558        84585 :       && gimple_code (dstmt_vinfo->stmt) != GIMPLE_PHI
     559        84585 :       && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
     560      2634309 :       && bb->loop_father == def_bb->loop_father)
     561              :     {
     562        84585 :       if (dump_enabled_p ())
     563         3906 :         dump_printf_loc (MSG_NOTE, vect_location,
     564              :                          "reduc-stmt defining reduc-phi in the same nest.\n");
     565        84585 :       vect_mark_relevant (worklist, dstmt_vinfo, relevant, true);
     566        84585 :       return opt_result::success ();
     567              :     }
     568              : 
     569              :   /* case 3a: outer-loop stmt defining an inner-loop stmt:
     570              :         outer-loop-header-bb:
     571              :                 d = dstmt_vinfo
     572              :         inner-loop:
     573              :                 stmt # use (d)
     574              :         outer-loop-tail-bb:
     575              :                 ...               */
     576      2465139 :   if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
     577              :     {
     578         2227 :       if (dump_enabled_p ())
     579          321 :         dump_printf_loc (MSG_NOTE, vect_location,
     580              :                          "outer-loop def-stmt defining inner-loop stmt.\n");
     581              : 
     582         2227 :       switch (relevant)
     583              :         {
     584            0 :         case vect_unused_in_scope:
     585            0 :           relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
     586              :                       vect_used_in_scope : vect_unused_in_scope;
     587              :           break;
     588              : 
     589          766 :         case vect_used_in_outer_by_reduction:
     590          766 :           gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
     591              :           relevant = vect_used_by_reduction;
     592              :           break;
     593              : 
     594         1181 :         case vect_used_in_outer:
     595         1181 :           gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
     596              :           relevant = vect_used_in_scope;
     597              :           break;
     598              : 
     599              :         case vect_used_in_scope:
     600              :           break;
     601              : 
     602            0 :         default:
     603            0 :           gcc_unreachable ();
     604              :         }
     605              :     }
     606              : 
     607              :   /* case 3b: inner-loop stmt defining an outer-loop stmt:
     608              :         outer-loop-header-bb:
     609              :                 ...
     610              :         inner-loop:
     611              :                 d = dstmt_vinfo
     612              :         outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
     613              :                 stmt # use (d)          */
     614      2462912 :   else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
     615              :     {
     616         2090 :       if (dump_enabled_p ())
     617          626 :         dump_printf_loc (MSG_NOTE, vect_location,
     618              :                          "inner-loop def-stmt defining outer-loop stmt.\n");
     619              : 
     620         2090 :       switch (relevant)
     621              :         {
     622            0 :         case vect_unused_in_scope:
     623            0 :           relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
     624            0 :             || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
     625              :                       vect_used_in_outer_by_reduction : vect_unused_in_scope;
     626              :           break;
     627              : 
     628              :         case vect_used_by_reduction:
     629              :         case vect_used_only_live:
     630              :           relevant = vect_used_in_outer_by_reduction;
     631              :           break;
     632              : 
     633              :         case vect_used_in_scope:
     634      2287293 :           relevant = vect_used_in_outer;
     635              :           break;
     636              : 
     637            0 :         default:
     638            0 :           gcc_unreachable ();
     639              :         }
     640              :     }
     641              :   /* We are also not interested in uses on loop PHI backedges that are
     642              :      inductions.  Otherwise we'll needlessly vectorize the IV increment
     643              :      and cause hybrid SLP for SLP inductions.  */
     644      2460822 :   else if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
     645       180871 :            && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_induction_def
     646      2638668 :            && (PHI_ARG_DEF_FROM_EDGE (stmt_vinfo->stmt,
     647              :                                       loop_latch_edge (bb->loop_father))
     648              :                == use))
     649              :     {
     650       177846 :       if (dump_enabled_p ())
     651         4854 :         dump_printf_loc (MSG_NOTE, vect_location,
     652              :                          "induction value on backedge.\n");
     653       177846 :       return opt_result::success ();
     654              :     }
     655              : 
     656      2287293 :   vect_mark_relevant (worklist, dstmt_vinfo, relevant, false);
     657      2287293 :   return opt_result::success ();
     658              : }
     659              : 
     660              : 
     661              : /* Function vect_mark_stmts_to_be_vectorized.
     662              : 
     663              :    Not all stmts in the loop need to be vectorized. For example:
     664              : 
     665              :      for i...
     666              :        for j...
     667              :    1.    T0 = i + j
     668              :    2.    T1 = a[T0]
     669              : 
     670              :    3.    j = j + 1
     671              : 
     672              :    Stmt 1 and 3 do not need to be vectorized, because loop control and
     673              :    addressing of vectorized data-refs are handled differently.
     674              : 
     675              :    This pass detects such stmts.  */
     676              : 
     677              : opt_result
     678       432450 : vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo, bool *fatal)
     679              : {
     680       432450 :   class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
     681       432450 :   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
     682       432450 :   unsigned int nbbs = loop->num_nodes;
     683       432450 :   gimple_stmt_iterator si;
     684       432450 :   unsigned int i;
     685       432450 :   basic_block bb;
     686       432450 :   bool live_p;
     687       432450 :   enum vect_relevant relevant;
     688              : 
     689       432450 :   DUMP_VECT_SCOPE ("vect_mark_stmts_to_be_vectorized");
     690              : 
     691       432450 :   auto_vec<stmt_vec_info, 64> worklist;
     692              : 
     693              :   /* 1. Init worklist.  */
     694      1464040 :   for (i = 0; i < nbbs; i++)
     695              :     {
     696      1041923 :       bb = bbs[i];
     697      2141443 :       for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
     698              :         {
     699      2219418 :           if (virtual_operand_p (gimple_phi_result (gsi_stmt (si))))
     700       230100 :             continue;
     701       879609 :           stmt_vec_info phi_info = loop_vinfo->lookup_stmt (gsi_stmt (si));
     702       879609 :           if (dump_enabled_p ())
     703        41544 :             dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? %G",
     704              :                              phi_info->stmt);
     705              : 
     706       879609 :           if (vect_stmt_relevant_p (phi_info, loop_vinfo, &relevant, &live_p))
     707              :             {
     708        44794 :               if (STMT_VINFO_DEF_TYPE (phi_info) == vect_unknown_def_type)
     709        10189 :                 return opt_result::failure_at
     710        10189 :                   (*si, "not vectorized: unhandled relevant PHI: %G", *si);
     711        34605 :               vect_mark_relevant (&worklist, phi_info, relevant, live_p);
     712              :             }
     713              :         }
     714      8258265 :       for (si = gsi_after_labels (bb); !gsi_end_p (si); gsi_next (&si))
     715              :         {
     716      7226675 :           gimple *stmt = gsi_stmt (si);
     717      7226675 :           if (is_gimple_debug (stmt))
     718      2949641 :             continue;
     719      4277034 :           stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (stmt);
     720      4277034 :           if (dump_enabled_p ())
     721       221942 :               dump_printf_loc (MSG_NOTE, vect_location,
     722              :                                "init: stmt relevant? %G", stmt);
     723              : 
     724      4277034 :           if (gimple_get_lhs (stmt) == NULL_TREE
     725       611782 :               && !is_a <gcond *> (stmt)
     726      4283341 :               && !is_a <gcall *> (stmt))
     727          144 :             return opt_result::failure_at
     728          144 :                 (stmt, "not vectorized: irregular stmt: %G", stmt);
     729              : 
     730      4276890 :           if (vect_stmt_relevant_p (stmt_info, loop_vinfo, &relevant, &live_p))
     731       799866 :             vect_mark_relevant (&worklist, stmt_info, relevant, live_p);
     732              :         }
     733              :     }
     734              : 
     735              :   /* 2. Process_worklist */
     736      3175812 :   while (worklist.length () > 0)
     737              :     {
     738      2788496 :       use_operand_p use_p;
     739      2788496 :       ssa_op_iter iter;
     740              : 
     741      2788496 :       stmt_vec_info stmt_vinfo = worklist.pop ();
     742      2788496 :       if (dump_enabled_p ())
     743       143353 :         dump_printf_loc (MSG_NOTE, vect_location,
     744              :                          "worklist: examine stmt: %G", stmt_vinfo->stmt);
     745              : 
     746              :       /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
     747              :          (DEF_STMT) as relevant/irrelevant according to the relevance property
     748              :          of STMT.  */
     749      2788496 :       relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
     750              : 
     751              :       /* Generally, the relevance property of STMT (in STMT_VINFO_RELEVANT) is
     752              :          propagated as is to the DEF_STMTs of its USEs.
     753              : 
     754              :          One exception is when STMT has been identified as defining a reduction
     755              :          variable; in this case we set the relevance to vect_used_by_reduction.
     756              :          This is because we distinguish between two kinds of relevant stmts -
     757              :          those that are used by a reduction computation, and those that are
     758              :          (also) used by a regular computation.  This allows us later on to
     759              :          identify stmts that are used solely by a reduction, and therefore the
     760              :          order of the results that they produce does not have to be kept.  */
     761              : 
     762      2788496 :       switch (STMT_VINFO_DEF_TYPE (stmt_vinfo))
     763              :         {
     764       171259 :           case vect_reduction_def:
     765       171259 :             gcc_assert (relevant != vect_unused_in_scope);
     766       171259 :             if (relevant != vect_unused_in_scope
     767       171259 :                 && relevant != vect_used_in_scope
     768       171259 :                 && relevant != vect_used_by_reduction
     769       171259 :                 && relevant != vect_used_only_live)
     770            0 :               return opt_result::failure_at
     771            0 :                 (stmt_vinfo->stmt, "unsupported use of reduction.\n");
     772              :             break;
     773              : 
     774         2204 :           case vect_nested_cycle:
     775         2204 :             if (relevant != vect_unused_in_scope
     776         2204 :                 && relevant != vect_used_in_outer_by_reduction
     777         1614 :                 && relevant != vect_used_in_outer)
     778            2 :               return opt_result::failure_at
     779            2 :                 (stmt_vinfo->stmt, "unsupported use of nested cycle.\n");
     780              :             break;
     781              : 
     782         1194 :           case vect_double_reduction_def:
     783         1194 :             if (relevant != vect_unused_in_scope
     784         1194 :                 && relevant != vect_used_by_reduction
     785          404 :                 && relevant != vect_used_only_live)
     786            0 :               return opt_result::failure_at
     787            0 :                 (stmt_vinfo->stmt, "unsupported use of double reduction.\n");
     788              :             break;
     789              : 
     790              :           default:
     791              :             break;
     792              :         }
     793              : 
     794      2788494 :       if (is_pattern_stmt_p (stmt_vinfo))
     795              :         {
     796              :           /* Pattern statements are not inserted into the code, so
     797              :              FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
     798              :              have to scan the RHS or function arguments instead.  */
     799       625021 :           if (gassign *assign = dyn_cast <gassign *> (stmt_vinfo->stmt))
     800              :             {
     801       407074 :               enum tree_code rhs_code = gimple_assign_rhs_code (assign);
     802       407074 :               tree op = gimple_assign_rhs1 (assign);
     803              : 
     804       407074 :               i = 1;
     805       407074 :               if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
     806              :                 {
     807            0 :                   opt_result res
     808            0 :                     = process_use (stmt_vinfo, TREE_OPERAND (op, 0),
     809              :                                    loop_vinfo, relevant, &worklist, false);
     810            0 :                   if (!res)
     811            0 :                     return res;
     812            0 :                   res = process_use (stmt_vinfo, TREE_OPERAND (op, 1),
     813              :                                      loop_vinfo, relevant, &worklist, false);
     814            0 :                   if (!res)
     815            0 :                     return res;
     816              :                   i = 2;
     817              :                 }
     818      1171790 :               for (; i < gimple_num_ops (assign); i++)
     819              :                 {
     820       768428 :                   op = gimple_op (assign, i);
     821       768428 :                   if (TREE_CODE (op) == SSA_NAME)
     822              :                     {
     823       584041 :                       opt_result res
     824       584041 :                         = process_use (stmt_vinfo, op, loop_vinfo, relevant,
     825              :                                        &worklist, false);
     826       584041 :                       if (!res)
     827         3712 :                         return res;
     828              :                     }
     829              :                  }
     830              :             }
     831       217947 :           else if (gcond *cond = dyn_cast <gcond *> (stmt_vinfo->stmt))
     832              :             {
     833       211314 :               tree_code rhs_code = gimple_cond_code (cond);
     834       211314 :               gcc_assert (TREE_CODE_CLASS (rhs_code) == tcc_comparison);
     835       211314 :               opt_result res
     836       211314 :                 = process_use (stmt_vinfo, gimple_cond_lhs (cond),
     837              :                                loop_vinfo, relevant, &worklist, false);
     838       211314 :               if (!res)
     839        34801 :                 return res;
     840       211314 :               res = process_use (stmt_vinfo, gimple_cond_rhs (cond),
     841              :                                 loop_vinfo, relevant, &worklist, false);
     842       211314 :               if (!res)
     843            0 :                 return res;
     844              :             }
     845         6633 :           else if (gcall *call = dyn_cast <gcall *> (stmt_vinfo->stmt))
     846              :             {
     847        31717 :               for (i = 0; i < gimple_call_num_args (call); i++)
     848              :                 {
     849        25084 :                   tree arg = gimple_call_arg (call, i);
     850        25084 :                   opt_result res
     851        25084 :                     = process_use (stmt_vinfo, arg, loop_vinfo, relevant,
     852              :                                    &worklist, false);
     853        25084 :                   if (!res)
     854            0 :                     return res;
     855              :                 }
     856              :             }
     857              :           else
     858            0 :             gcc_unreachable ();
     859              :         }
     860              :       else
     861      7575572 :         FOR_EACH_PHI_OR_STMT_USE (use_p, stmt_vinfo->stmt, iter, SSA_OP_USE)
     862              :           {
     863      3266540 :             tree op = USE_FROM_PTR (use_p);
     864      3266540 :             opt_result res
     865      3266540 :               = process_use (stmt_vinfo, op, loop_vinfo, relevant,
     866              :                              &worklist, false);
     867      3266540 :             if (!res)
     868        17914 :               return res;
     869              :           }
     870              : 
     871      2766868 :       if (STMT_VINFO_GATHER_SCATTER_P (stmt_vinfo))
     872              :         {
     873        55845 :           gather_scatter_info gs_info;
     874        55845 :           if (!vect_check_gather_scatter (stmt_vinfo,
     875              :                                           STMT_VINFO_VECTYPE (stmt_vinfo),
     876              :                                           loop_vinfo, &gs_info))
     877            0 :             gcc_unreachable ();
     878        55845 :           opt_result res
     879        55845 :             = process_use (stmt_vinfo, gs_info.offset, loop_vinfo, relevant,
     880              :                            &worklist, true);
     881        55845 :           if (!res)
     882              :             {
     883        13173 :               if (fatal)
     884        13173 :                 *fatal = false;
     885        13173 :               return res;
     886              :             }
     887              :         }
     888              :     } /* while worklist */
     889              : 
     890       387316 :   return opt_result::success ();
     891       432450 : }
     892              : 
     893              : /* Function vect_model_simple_cost.
     894              : 
     895              :    Models cost for simple operations, i.e. those that only emit N operations
     896              :    of the same KIND.  */
     897              : 
     898              : static void
     899       798137 : vect_model_simple_cost (vec_info *vinfo, int n, slp_tree node,
     900              :                         stmt_vector_for_cost *cost_vec,
     901              :                         vect_cost_for_stmt kind = vector_stmt)
     902              : {
     903       798137 :   int inside_cost = 0, prologue_cost = 0;
     904              : 
     905       798137 :   gcc_assert (cost_vec != NULL);
     906              : 
     907       798137 :   n *= vect_get_num_copies (vinfo, node);
     908              : 
     909              :   /* Pass the inside-of-loop statements to the target-specific cost model.  */
     910       798137 :   inside_cost += record_stmt_cost (cost_vec, n, kind, node, 0, vect_body);
     911              : 
     912       798137 :   if (dump_enabled_p ())
     913        33159 :     dump_printf_loc (MSG_NOTE, vect_location,
     914              :                      "vect_model_simple_cost: inside_cost = %d, "
     915              :                      "prologue_cost = %d .\n", inside_cost, prologue_cost);
     916       798137 : }
     917              : 
     918              : 
     919              : /* Model cost for type demotion and promotion operations.  PWR is
     920              :    normally zero for single-step promotions and demotions.  It will be
     921              :    one if two-step promotion/demotion is required, and so on.  NCOPIES
     922              :    is the number of vector results (and thus number of instructions)
     923              :    for the narrowest end of the operation chain.  Each additional
     924              :    step doubles the number of instructions required.  If WIDEN_ARITH
     925              :    is true the stmt is doing widening arithmetic.  */
     926              : 
     927              : static void
     928        68264 : vect_model_promotion_demotion_cost (slp_tree slp_node,
     929              :                                     unsigned int ncopies, int pwr,
     930              :                                     stmt_vector_for_cost *cost_vec,
     931              :                                     bool widen_arith)
     932              : {
     933        68264 :   int i;
     934        68264 :   int inside_cost = 0, prologue_cost = 0;
     935              : 
     936       159238 :   for (i = 0; i < pwr + 1; i++)
     937              :     {
     938       180226 :       inside_cost += record_stmt_cost (cost_vec, ncopies,
     939              :                                        widen_arith
     940              :                                        ? vector_stmt : vec_promote_demote,
     941              :                                        slp_node, 0, vect_body);
     942        90974 :       ncopies *= 2;
     943              :     }
     944              : 
     945        68264 :   if (dump_enabled_p ())
     946         6378 :     dump_printf_loc (MSG_NOTE, vect_location,
     947              :                      "vect_model_promotion_demotion_cost: inside_cost = %d, "
     948              :                      "prologue_cost = %d .\n", inside_cost, prologue_cost);
     949        68264 : }
     950              : 
     951              : /* Returns true if the current function returns DECL.  */
     952              : 
     953              : static bool
     954       559296 : cfun_returns (tree decl)
     955              : {
     956       559296 :   edge_iterator ei;
     957       559296 :   edge e;
     958      1101231 :   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
     959              :     {
     960      1107358 :       greturn *ret = safe_dyn_cast <greturn *> (*gsi_last_bb (e->src));
     961       553679 :       if (!ret)
     962            0 :         continue;
     963       553679 :       if (gimple_return_retval (ret) == decl)
     964              :         return true;
     965              :       /* We often end up with an aggregate copy to the result decl,
     966              :          handle that case as well.  First skip intermediate clobbers
     967              :          though.  */
     968              :       gimple *def = ret;
     969      1672421 :       do
     970              :         {
     971      3344842 :           def = SSA_NAME_DEF_STMT (gimple_vuse (def));
     972              :         }
     973      1672421 :       while (gimple_clobber_p (def));
     974       542708 :       if (is_a <gassign *> (def)
     975        61384 :           && gimple_assign_lhs (def) == gimple_return_retval (ret)
     976       549841 :           && gimple_assign_rhs1 (def) == decl)
     977              :         return true;
     978              :     }
     979              :   return false;
     980              : }
     981              : 
     982              : /* Calculate cost of DR's memory access.  */
     983              : void
     984      1011576 : vect_get_store_cost (vec_info *, stmt_vec_info stmt_info, slp_tree slp_node,
     985              :                      int ncopies, dr_alignment_support alignment_support_scheme,
     986              :                      int misalignment,
     987              :                      unsigned int *inside_cost,
     988              :                      stmt_vector_for_cost *body_cost_vec)
     989              : {
     990      1011576 :   tree vectype
     991      1011576 :     = slp_node ? SLP_TREE_VECTYPE (slp_node) : STMT_VINFO_VECTYPE (stmt_info);
     992      1011576 :   switch (alignment_support_scheme)
     993              :     {
     994       552655 :     case dr_aligned:
     995       552655 :       {
     996       552655 :         *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
     997              :                                           vector_store, stmt_info, slp_node,
     998              :                                           vectype, 0, vect_body);
     999              : 
    1000       552655 :         if (dump_enabled_p ())
    1001        14502 :           dump_printf_loc (MSG_NOTE, vect_location,
    1002              :                            "vect_model_store_cost: aligned.\n");
    1003              :         break;
    1004              :       }
    1005              : 
    1006       458921 :     case dr_unaligned_supported:
    1007       458921 :       {
    1008              :         /* Here, we assign an additional cost for the unaligned store.  */
    1009       458921 :         *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
    1010              :                                           unaligned_store, stmt_info, slp_node,
    1011              :                                           vectype, misalignment, vect_body);
    1012       458921 :         if (dump_enabled_p ())
    1013        12904 :           dump_printf_loc (MSG_NOTE, vect_location,
    1014              :                            "vect_model_store_cost: unaligned supported by "
    1015              :                            "hardware.\n");
    1016              :         break;
    1017              :       }
    1018              : 
    1019            0 :     case dr_unaligned_unsupported:
    1020            0 :       {
    1021            0 :         *inside_cost = VECT_MAX_COST;
    1022              : 
    1023            0 :         if (dump_enabled_p ())
    1024            0 :           dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1025              :                            "vect_model_store_cost: unsupported access.\n");
    1026              :         break;
    1027              :       }
    1028              : 
    1029            0 :     default:
    1030            0 :       gcc_unreachable ();
    1031              :     }
    1032      1011576 : }
    1033              : 
    1034              : /* Calculate cost of DR's memory access.  */
    1035              : void
    1036       926863 : vect_get_load_cost (vec_info *, stmt_vec_info stmt_info, slp_tree slp_node,
    1037              :                     int ncopies, dr_alignment_support alignment_support_scheme,
    1038              :                     int misalignment,
    1039              :                     bool add_realign_cost, unsigned int *inside_cost,
    1040              :                     unsigned int *prologue_cost,
    1041              :                     stmt_vector_for_cost *prologue_cost_vec,
    1042              :                     stmt_vector_for_cost *body_cost_vec,
    1043              :                     bool record_prologue_costs)
    1044              : {
    1045       926863 :   tree vectype
    1046       926863 :     = slp_node ? SLP_TREE_VECTYPE (slp_node) : STMT_VINFO_VECTYPE (stmt_info);
    1047       926863 :   switch (alignment_support_scheme)
    1048              :     {
    1049       525773 :     case dr_aligned:
    1050       525773 :       {
    1051       525773 :         *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
    1052              :                                           stmt_info, slp_node, vectype,
    1053              :                                           0, vect_body);
    1054              : 
    1055       525773 :         if (dump_enabled_p ())
    1056        18844 :           dump_printf_loc (MSG_NOTE, vect_location,
    1057              :                            "vect_model_load_cost: aligned.\n");
    1058              : 
    1059              :         break;
    1060              :       }
    1061       345379 :     case dr_unaligned_supported:
    1062       345379 :       {
    1063              :         /* Here, we assign an additional cost for the unaligned load.  */
    1064       345379 :         *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
    1065              :                                           unaligned_load, stmt_info, slp_node,
    1066              :                                           vectype, misalignment, vect_body);
    1067              : 
    1068       345379 :         if (dump_enabled_p ())
    1069        22266 :           dump_printf_loc (MSG_NOTE, vect_location,
    1070              :                            "vect_model_load_cost: unaligned supported by "
    1071              :                            "hardware.\n");
    1072              : 
    1073              :         break;
    1074              :       }
    1075            0 :     case dr_explicit_realign:
    1076            0 :       {
    1077            0 :         *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
    1078              :                                           vector_load, stmt_info, slp_node,
    1079              :                                           vectype, 0, vect_body);
    1080            0 :         *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
    1081              :                                           vec_perm, stmt_info, slp_node,
    1082              :                                           vectype, 0, vect_body);
    1083              : 
    1084              :         /* FIXME: If the misalignment remains fixed across the iterations of
    1085              :            the containing loop, the following cost should be added to the
    1086              :            prologue costs.  */
    1087            0 :         if (targetm.vectorize.builtin_mask_for_load)
    1088            0 :           *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
    1089              :                                             stmt_info, slp_node, vectype,
    1090              :                                             0, vect_body);
    1091              : 
    1092            0 :         if (dump_enabled_p ())
    1093            0 :           dump_printf_loc (MSG_NOTE, vect_location,
    1094              :                            "vect_model_load_cost: explicit realign\n");
    1095              : 
    1096              :         break;
    1097              :       }
    1098            0 :     case dr_explicit_realign_optimized:
    1099            0 :       {
    1100            0 :         if (dump_enabled_p ())
    1101            0 :           dump_printf_loc (MSG_NOTE, vect_location,
    1102              :                            "vect_model_load_cost: unaligned software "
    1103              :                            "pipelined.\n");
    1104              : 
    1105              :         /* Unaligned software pipeline has a load of an address, an initial
    1106              :            load, and possibly a mask operation to "prime" the loop.  However,
    1107              :            if this is an access in a group of loads, which provide grouped
    1108              :            access, then the above cost should only be considered for one
    1109              :            access in the group.  Inside the loop, there is a load op
    1110              :            and a realignment op.  */
    1111              : 
    1112            0 :         if (add_realign_cost && record_prologue_costs)
    1113              :           {
    1114            0 :             *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
    1115              :                                                 vector_stmt, stmt_info,
    1116              :                                                 slp_node, vectype,
    1117              :                                                 0, vect_prologue);
    1118            0 :             if (targetm.vectorize.builtin_mask_for_load)
    1119            0 :               *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
    1120              :                                                   vector_stmt, stmt_info,
    1121              :                                                   slp_node, vectype,
    1122              :                                                   0, vect_prologue);
    1123              :           }
    1124              : 
    1125            0 :         *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
    1126              :                                           stmt_info, slp_node, vectype,
    1127              :                                           0, vect_body);
    1128            0 :         *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
    1129              :                                           stmt_info, slp_node, vectype,
    1130              :                                           0, vect_body);
    1131              : 
    1132            0 :         if (dump_enabled_p ())
    1133            0 :           dump_printf_loc (MSG_NOTE, vect_location,
    1134              :                            "vect_model_load_cost: explicit realign optimized"
    1135              :                            "\n");
    1136              : 
    1137              :         break;
    1138              :       }
    1139              : 
    1140        55711 :     case dr_unaligned_unsupported:
    1141        55711 :       {
    1142        55711 :         *inside_cost = VECT_MAX_COST;
    1143              : 
    1144        55711 :         if (dump_enabled_p ())
    1145          104 :           dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1146              :                            "vect_model_load_cost: unsupported access.\n");
    1147              :         break;
    1148              :       }
    1149              : 
    1150            0 :     default:
    1151            0 :       gcc_unreachable ();
    1152              :     }
    1153       926863 : }
    1154              : 
    1155              : /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
    1156              :    the loop preheader for the vectorized stmt STMT_VINFO.  */
    1157              : 
    1158              : static void
    1159         6076 : vect_init_vector_1 (vec_info *vinfo, stmt_vec_info stmt_vinfo, gimple *new_stmt,
    1160              :                     gimple_stmt_iterator *gsi)
    1161              : {
    1162         6076 :   if (gsi)
    1163         2767 :     vect_finish_stmt_generation (vinfo, stmt_vinfo, new_stmt, gsi);
    1164              :   else
    1165         3309 :     vinfo->insert_on_entry (stmt_vinfo, new_stmt);
    1166              : 
    1167         6076 :   if (dump_enabled_p ())
    1168         1807 :     dump_printf_loc (MSG_NOTE, vect_location,
    1169              :                      "created new init_stmt: %G", new_stmt);
    1170         6076 : }
    1171              : 
    1172              : /* Function vect_init_vector.
    1173              : 
    1174              :    Insert a new stmt (INIT_STMT) that initializes a new variable of type
    1175              :    TYPE with the value VAL.  If TYPE is a vector type and VAL does not have
    1176              :    vector type a vector with all elements equal to VAL is created first.
    1177              :    Place the initialization at GSI if it is not NULL.  Otherwise, place the
    1178              :    initialization at the loop preheader.
    1179              :    Return the DEF of INIT_STMT.
    1180              :    It will be used in the vectorization of STMT_INFO.  */
    1181              : 
    1182              : tree
    1183         4360 : vect_init_vector (vec_info *vinfo, stmt_vec_info stmt_info, tree val, tree type,
    1184              :                   gimple_stmt_iterator *gsi)
    1185              : {
    1186         4360 :   gimple *init_stmt;
    1187         4360 :   tree new_temp;
    1188              : 
    1189              :   /* We abuse this function to push sth to a SSA name with initial 'val'.  */
    1190         4360 :   if (! useless_type_conversion_p (type, TREE_TYPE (val)))
    1191              :     {
    1192         1348 :       gcc_assert (VECTOR_TYPE_P (type));
    1193         1348 :       if (! types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
    1194              :         {
    1195              :           /* Scalar boolean value should be transformed into
    1196              :              all zeros or all ones value before building a vector.  */
    1197            8 :           if (VECTOR_BOOLEAN_TYPE_P (type))
    1198              :             {
    1199            0 :               tree true_val = build_all_ones_cst (TREE_TYPE (type));
    1200            0 :               tree false_val = build_zero_cst (TREE_TYPE (type));
    1201              : 
    1202            0 :               if (CONSTANT_CLASS_P (val))
    1203            0 :                 val = integer_zerop (val) ? false_val : true_val;
    1204              :               else
    1205              :                 {
    1206            0 :                   new_temp = make_ssa_name (TREE_TYPE (type));
    1207            0 :                   init_stmt = gimple_build_assign (new_temp, COND_EXPR,
    1208              :                                                    val, true_val, false_val);
    1209            0 :                   vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
    1210            0 :                   val = new_temp;
    1211              :                 }
    1212              :             }
    1213              :           else
    1214              :             {
    1215            8 :               gimple_seq stmts = NULL;
    1216            8 :               if (! INTEGRAL_TYPE_P (TREE_TYPE (val)))
    1217            8 :                 val = gimple_build (&stmts, VIEW_CONVERT_EXPR,
    1218            8 :                                     TREE_TYPE (type), val);
    1219              :               else
    1220              :                 /* ???  Condition vectorization expects us to do
    1221              :                    promotion of invariant/external defs.  */
    1222            0 :                 val = gimple_convert (&stmts, TREE_TYPE (type), val);
    1223           16 :               for (gimple_stmt_iterator gsi2 = gsi_start (stmts);
    1224           16 :                    !gsi_end_p (gsi2); )
    1225              :                 {
    1226            8 :                   init_stmt = gsi_stmt (gsi2);
    1227            8 :                   gsi_remove (&gsi2, false);
    1228            8 :                   vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
    1229              :                 }
    1230              :             }
    1231              :         }
    1232         1348 :       val = build_vector_from_val (type, val);
    1233              :     }
    1234              : 
    1235         4360 :   new_temp = vect_get_new_ssa_name (type, vect_simple_var, "cst_");
    1236         4360 :   init_stmt = gimple_build_assign (new_temp, val);
    1237         4360 :   vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
    1238         4360 :   return new_temp;
    1239              : }
    1240              : 
    1241              : 
    1242              : /* Get vectorized definitions for OP0 and OP1.  */
    1243              : 
    1244              : void
    1245       186741 : vect_get_vec_defs (vec_info *, slp_tree slp_node,
    1246              :                    tree op0, vec<tree> *vec_oprnds0,
    1247              :                    tree op1, vec<tree> *vec_oprnds1,
    1248              :                    tree op2, vec<tree> *vec_oprnds2,
    1249              :                    tree op3, vec<tree> *vec_oprnds3)
    1250              : {
    1251       186741 :   if (op0)
    1252       185088 :     vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_oprnds0);
    1253       186741 :   if (op1)
    1254       137459 :     vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[1], vec_oprnds1);
    1255       186741 :   if (op2)
    1256         9214 :     vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[2], vec_oprnds2);
    1257       186741 :   if (op3)
    1258            0 :     vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[3], vec_oprnds3);
    1259       186741 : }
    1260              : 
    1261              : /* Helper function called by vect_finish_replace_stmt and
    1262              :    vect_finish_stmt_generation.  Set the location of the new
    1263              :    statement and create and return a stmt_vec_info for it.  */
    1264              : 
    1265              : static void
    1266      1430761 : vect_finish_stmt_generation_1 (vec_info *,
    1267              :                                stmt_vec_info stmt_info, gimple *vec_stmt)
    1268              : {
    1269      1430761 :   if (dump_enabled_p ())
    1270       147840 :     dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: %G", vec_stmt);
    1271              : 
    1272      1430761 :   if (stmt_info)
    1273              :     {
    1274      1399476 :       gimple_set_location (vec_stmt, gimple_location (stmt_info->stmt));
    1275              : 
    1276              :       /* While EH edges will generally prevent vectorization, stmt might
    1277              :          e.g. be in a must-not-throw region.  Ensure newly created stmts
    1278              :          that could throw are part of the same region.  */
    1279      1399476 :       int lp_nr = lookup_stmt_eh_lp (stmt_info->stmt);
    1280      1399476 :       if (lp_nr != 0 && stmt_could_throw_p (cfun, vec_stmt))
    1281           48 :         add_stmt_to_eh_lp (vec_stmt, lp_nr);
    1282              :     }
    1283              :   else
    1284        31285 :     gcc_assert (!stmt_could_throw_p (cfun, vec_stmt));
    1285      1430761 : }
    1286              : 
    1287              : /* Replace the scalar statement STMT_INFO with a new vector statement VEC_STMT,
    1288              :    which sets the same scalar result as STMT_INFO did.  Create and return a
    1289              :    stmt_vec_info for VEC_STMT.  */
    1290              : 
    1291              : void
    1292          847 : vect_finish_replace_stmt (vec_info *vinfo,
    1293              :                           stmt_vec_info stmt_info, gimple *vec_stmt)
    1294              : {
    1295          847 :   gimple *scalar_stmt = vect_orig_stmt (stmt_info)->stmt;
    1296          847 :   gcc_assert (gimple_get_lhs (scalar_stmt) == gimple_get_lhs (vec_stmt));
    1297              : 
    1298          847 :   gimple_stmt_iterator gsi = gsi_for_stmt (scalar_stmt);
    1299          847 :   gsi_replace (&gsi, vec_stmt, true);
    1300              : 
    1301          847 :   vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
    1302          847 : }
    1303              : 
    1304              : /* Add VEC_STMT to the vectorized implementation of STMT_INFO and insert it
    1305              :    before *GSI.  Create and return a stmt_vec_info for VEC_STMT.  */
    1306              : 
    1307              : void
    1308      1429914 : vect_finish_stmt_generation (vec_info *vinfo,
    1309              :                              stmt_vec_info stmt_info, gimple *vec_stmt,
    1310              :                              gimple_stmt_iterator *gsi)
    1311              : {
    1312      1429914 :   gcc_assert (!stmt_info || gimple_code (stmt_info->stmt) != GIMPLE_LABEL);
    1313              : 
    1314      1429914 :   if (!gsi_end_p (*gsi)
    1315      2858553 :       && gimple_has_mem_ops (vec_stmt))
    1316              :     {
    1317      1428639 :       gimple *at_stmt = gsi_stmt (*gsi);
    1318      1428639 :       tree vuse = gimple_vuse (at_stmt);
    1319      1422298 :       if (vuse && TREE_CODE (vuse) == SSA_NAME)
    1320              :         {
    1321      1281255 :           tree vdef = gimple_vdef (at_stmt);
    1322      1281255 :           gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
    1323      1281255 :           gimple_set_modified (vec_stmt, true);
    1324              :           /* If we have an SSA vuse and insert a store, update virtual
    1325              :              SSA form to avoid triggering the renamer.  Do so only
    1326              :              if we can easily see all uses - which is what almost always
    1327              :              happens with the way vectorized stmts are inserted.  */
    1328       755169 :           if ((vdef && TREE_CODE (vdef) == SSA_NAME)
    1329      2036388 :               && ((is_gimple_assign (vec_stmt)
    1330       754261 :                    && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
    1331        65559 :                   || (is_gimple_call (vec_stmt)
    1332          872 :                       && (!(gimple_call_flags (vec_stmt)
    1333          872 :                             & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
    1334            1 :                           || (gimple_call_lhs (vec_stmt)
    1335            1 :                               && !is_gimple_reg (gimple_call_lhs (vec_stmt)))))))
    1336              :             {
    1337       690445 :               tree new_vdef = copy_ssa_name (vuse, vec_stmt);
    1338       690445 :               gimple_set_vdef (vec_stmt, new_vdef);
    1339       690445 :               SET_USE (gimple_vuse_op (at_stmt), new_vdef);
    1340              :             }
    1341              :         }
    1342              :     }
    1343      1429914 :   gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
    1344      1429914 :   vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
    1345      1429914 : }
    1346              : 
    1347              : /* We want to vectorize a call to combined function CFN with function
    1348              :    decl FNDECL, using VECTYPE_OUT as the type of the output and VECTYPE_IN
    1349              :    as the types of all inputs.  Check whether this is possible using
    1350              :    an internal function, returning its code if so or IFN_LAST if not.  */
    1351              : 
    1352              : static internal_fn
    1353        16379 : vectorizable_internal_function (combined_fn cfn, tree fndecl,
    1354              :                                 tree vectype_out, tree vectype_in)
    1355              : {
    1356        16379 :   internal_fn ifn;
    1357        16379 :   if (internal_fn_p (cfn))
    1358        13875 :     ifn = as_internal_fn (cfn);
    1359              :   else
    1360         2504 :     ifn = associated_internal_fn (fndecl);
    1361        16379 :   if (ifn != IFN_LAST && direct_internal_fn_p (ifn))
    1362              :     {
    1363        12923 :       const direct_internal_fn_info &info = direct_internal_fn (ifn);
    1364        12923 :       if (info.vectorizable)
    1365              :         {
    1366        12923 :           bool same_size_p = TYPE_SIZE (vectype_in) == TYPE_SIZE (vectype_out);
    1367        12923 :           tree type0 = (info.type0 < 0 ? vectype_out : vectype_in);
    1368        12923 :           tree type1 = (info.type1 < 0 ? vectype_out : vectype_in);
    1369              : 
    1370              :           /* The type size of both the vectype_in and vectype_out should be
    1371              :              exactly the same when vectype_out isn't participating the optab.
    1372              :              While there is no restriction for type size when vectype_out
    1373              :              is part of the optab query.  */
    1374        12923 :           if (type0 != vectype_out && type1 != vectype_out && !same_size_p)
    1375              :             return IFN_LAST;
    1376              : 
    1377        12903 :           if (direct_internal_fn_supported_p (ifn, tree_pair (type0, type1),
    1378              :                                               OPTIMIZE_FOR_SPEED))
    1379              :             return ifn;
    1380              :         }
    1381              :     }
    1382              :   return IFN_LAST;
    1383              : }
    1384              : 
    1385              : 
    1386              : static tree permute_vec_elements (vec_info *, tree, tree, tree, stmt_vec_info,
    1387              :                                   gimple_stmt_iterator *);
    1388              : 
    1389              : /* Check whether a load or store statement in the loop described by
    1390              :    LOOP_VINFO is possible in a loop using partial vectors.  This is
    1391              :    testing whether the vectorizer pass has the appropriate support,
    1392              :    as well as whether the target does.
    1393              : 
    1394              :    VLS_TYPE says whether the statement is a load or store and VECTYPE
    1395              :    is the type of the vector being loaded or stored.  SLP_NODE is the SLP
    1396              :    node that contains the statement, or null if none.  MEMORY_ACCESS_TYPE
    1397              :    says how the load or store is going to be implemented and GROUP_SIZE
    1398              :    is the number of load or store statements in the containing group.
    1399              :    If the access is a gather load or scatter store, GS_INFO describes
    1400              :    its arguments.  If the load or store is conditional, SCALAR_MASK is the
    1401              :    condition under which it occurs.
    1402              : 
    1403              :    Clear LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P if a loop using partial
    1404              :    vectors is not supported, otherwise record the required rgroup control
    1405              :    types.
    1406              : 
    1407              :    If partial vectors can be used and ELSVALS is nonzero the supported
    1408              :    else values will be added to the vector ELSVALS points to.  */
    1409              : 
    1410              : static void
    1411       288979 : check_load_store_for_partial_vectors (loop_vec_info loop_vinfo, tree vectype,
    1412              :                                       slp_tree slp_node,
    1413              :                                       vec_load_store_type vls_type,
    1414              :                                       int group_size,
    1415              :                                       vect_load_store_data *ls,
    1416              :                                       slp_tree mask_node,
    1417              :                                       vec<int> *elsvals = nullptr)
    1418              : {
    1419       288979 :   vect_memory_access_type memory_access_type = ls->memory_access_type;
    1420              : 
    1421              :   /* Invariant loads need no special support.  */
    1422       288979 :   if (memory_access_type == VMAT_INVARIANT)
    1423        29102 :     return;
    1424              : 
    1425              :   /* Figure whether the mask is uniform.  scalar_mask is used to
    1426              :      populate the scalar_cond_masked_set.  */
    1427       287752 :   tree scalar_mask = NULL_TREE;
    1428       287752 :   if (mask_node)
    1429         4968 :     for (unsigned i = 0; i < SLP_TREE_LANES (mask_node); ++i)
    1430              :       {
    1431         2535 :         tree def = vect_get_slp_scalar_def (mask_node, i);
    1432         2535 :         if (!def
    1433         2535 :             || (scalar_mask && def != scalar_mask))
    1434              :           {
    1435              :             scalar_mask = NULL;
    1436              :             break;
    1437              :           }
    1438              :         else
    1439         2504 :           scalar_mask = def;
    1440              :       }
    1441              : 
    1442       287752 :   unsigned int nvectors = vect_get_num_copies (loop_vinfo, slp_node);
    1443       287752 :   vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
    1444       287752 :   vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
    1445       287752 :   machine_mode vecmode = TYPE_MODE (vectype);
    1446       287752 :   bool is_load = (vls_type == VLS_LOAD);
    1447       287752 :   if (memory_access_type == VMAT_LOAD_STORE_LANES)
    1448              :     {
    1449            0 :       nvectors /= group_size;
    1450            0 :       internal_fn ifn
    1451            0 :         = (is_load ? vect_load_lanes_supported (vectype, group_size, true,
    1452              :                                                 elsvals)
    1453            0 :                    : vect_store_lanes_supported (vectype, group_size, true));
    1454            0 :       if (ifn == IFN_MASK_LEN_LOAD_LANES || ifn == IFN_MASK_LEN_STORE_LANES)
    1455            0 :         vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
    1456            0 :       else if (ifn == IFN_MASK_LOAD_LANES || ifn == IFN_MASK_STORE_LANES)
    1457            0 :         vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
    1458              :                                scalar_mask);
    1459              :       else
    1460              :         {
    1461            0 :           if (dump_enabled_p ())
    1462            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1463              :                              "can't operate on partial vectors because"
    1464              :                              " the target doesn't have an appropriate"
    1465              :                              " load/store-lanes instruction.\n");
    1466            0 :           LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    1467              :         }
    1468            0 :       return;
    1469              :     }
    1470              : 
    1471       287752 :   if (mat_gather_scatter_p (memory_access_type))
    1472              :     {
    1473         1750 :       internal_fn ifn = (is_load
    1474         1750 :                          ? IFN_MASK_GATHER_LOAD
    1475              :                          : IFN_MASK_SCATTER_STORE);
    1476          421 :       internal_fn len_ifn = (is_load
    1477              :                              ? IFN_MASK_LEN_GATHER_LOAD
    1478              :                              : IFN_MASK_LEN_SCATTER_STORE);
    1479         1750 :       stmt_vec_info repr = SLP_TREE_REPRESENTATIVE (slp_node);
    1480         1750 :       tree off_vectype = (STMT_VINFO_GATHER_SCATTER_P (repr)
    1481         1750 :                           ? SLP_TREE_VECTYPE (SLP_TREE_CHILDREN (slp_node)[0])
    1482         1750 :                           : ls->strided_offset_vectype);
    1483         1750 :       tree memory_type = TREE_TYPE (DR_REF (STMT_VINFO_DR_INFO (repr)->dr));
    1484         1750 :       int scale = SLP_TREE_GS_SCALE (slp_node);
    1485              : 
    1486              :       /* The following "supported" checks just verify what we established in
    1487              :          get_load_store_type and don't try different offset types.
    1488              :          Therefore, off_vectype must be a supported offset type.  In case
    1489              :          we chose a different one use this instead.  */
    1490         1750 :       if (ls->supported_offset_vectype)
    1491            0 :         off_vectype = ls->supported_offset_vectype;
    1492              :       /* Same for scale.  */
    1493         1750 :       if (ls->supported_scale)
    1494            0 :         scale = ls->supported_scale;
    1495              : 
    1496         1750 :       if (internal_gather_scatter_fn_supported_p (len_ifn, vectype,
    1497              :                                                   memory_type,
    1498              :                                                   off_vectype, scale,
    1499              :                                                   elsvals))
    1500            0 :         vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
    1501         1750 :       else if (internal_gather_scatter_fn_supported_p (ifn, vectype,
    1502              :                                                        memory_type,
    1503              :                                                        off_vectype, scale,
    1504              :                                                        elsvals)
    1505         1750 :                || memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
    1506          566 :         vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
    1507              :                                scalar_mask);
    1508              :       else
    1509              :         {
    1510         1184 :           if (dump_enabled_p ())
    1511           26 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1512              :                              "can't operate on partial vectors because"
    1513              :                              " the target doesn't have an appropriate"
    1514              :                              " gather load or scatter store instruction.\n");
    1515         1184 :           LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    1516              :         }
    1517         1750 :       return;
    1518              :     }
    1519              : 
    1520       286002 :   if (memory_access_type != VMAT_CONTIGUOUS)
    1521              :     {
    1522              :       /* Element X of the data must come from iteration i * VF + X of the
    1523              :          scalar loop.  We need more work to support other mappings.  */
    1524        26125 :       if (dump_enabled_p ())
    1525          728 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1526              :                          "can't operate on partial vectors because an"
    1527              :                          " access isn't contiguous.\n");
    1528        26125 :       LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    1529        26125 :       return;
    1530              :     }
    1531              : 
    1532       259877 :   if (!VECTOR_MODE_P (vecmode))
    1533              :     {
    1534            0 :       if (dump_enabled_p ())
    1535            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1536              :                          "can't operate on partial vectors when emulating"
    1537              :                          " vector operations.\n");
    1538            0 :       LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    1539            0 :       return;
    1540              :     }
    1541              : 
    1542              :   /* We might load more scalars than we need for permuting SLP loads.
    1543              :      We checked in get_load_store_type that the extra elements
    1544              :      don't leak into a new vector.  */
    1545       349562 :   auto group_memory_nvectors = [](poly_uint64 size, poly_uint64 nunits)
    1546              :   {
    1547        89685 :     unsigned int nvectors;
    1548       179370 :     if (can_div_away_from_zero_p (size, nunits, &nvectors))
    1549        89685 :       return nvectors;
    1550              :     gcc_unreachable ();
    1551              :   };
    1552              : 
    1553       259877 :   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
    1554       259877 :   poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
    1555       259877 :   machine_mode mask_mode;
    1556       259877 :   machine_mode vmode;
    1557       259877 :   bool using_partial_vectors_p = false;
    1558       259877 :   if (get_len_load_store_mode
    1559       259877 :       (vecmode, is_load, nullptr, elsvals).exists (&vmode))
    1560              :     {
    1561            0 :       nvectors = group_memory_nvectors (group_size * vf, nunits);
    1562            0 :       unsigned factor = (vecmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vecmode);
    1563            0 :       vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, factor);
    1564            0 :       using_partial_vectors_p = true;
    1565              :     }
    1566       349562 :   else if (targetm.vectorize.get_mask_mode (vecmode).exists (&mask_mode)
    1567       259877 :            && can_vec_mask_load_store_p (vecmode, mask_mode, is_load, NULL,
    1568              :                                          elsvals))
    1569              :     {
    1570        89685 :       nvectors = group_memory_nvectors (group_size * vf, nunits);
    1571        89685 :       vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype, scalar_mask);
    1572        89685 :       using_partial_vectors_p = true;
    1573              :     }
    1574              : 
    1575        89685 :   if (!using_partial_vectors_p)
    1576              :     {
    1577       170192 :       if (dump_enabled_p ())
    1578        11639 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1579              :                          "can't operate on partial vectors because the"
    1580              :                          " target doesn't have the appropriate partial"
    1581              :                          " vectorization load or store.\n");
    1582       170192 :       LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    1583              :     }
    1584              : }
    1585              : 
    1586              : /* Return the mask input to a masked load or store.  VEC_MASK is the vectorized
    1587              :    form of the scalar mask condition and LOOP_MASK, if nonnull, is the mask
    1588              :    that needs to be applied to all loads and stores in a vectorized loop.
    1589              :    Return VEC_MASK if LOOP_MASK is null or if VEC_MASK is already masked,
    1590              :    otherwise return VEC_MASK & LOOP_MASK.
    1591              : 
    1592              :    MASK_TYPE is the type of both masks.  If new statements are needed,
    1593              :    insert them before GSI.  */
    1594              : 
    1595              : tree
    1596         1702 : prepare_vec_mask (loop_vec_info loop_vinfo, tree mask_type, tree loop_mask,
    1597              :                   tree vec_mask, gimple_stmt_iterator *gsi)
    1598              : {
    1599         1702 :   gcc_assert (useless_type_conversion_p (mask_type, TREE_TYPE (vec_mask)));
    1600         1702 :   if (!loop_mask)
    1601              :     return vec_mask;
    1602              : 
    1603          139 :   gcc_assert (TREE_TYPE (loop_mask) == mask_type);
    1604              : 
    1605          139 :   if (loop_vinfo->vec_cond_masked_set.contains ({ vec_mask, loop_mask }))
    1606              :     return vec_mask;
    1607              : 
    1608          139 :   tree and_res = make_temp_ssa_name (mask_type, NULL, "vec_mask_and");
    1609          139 :   gimple *and_stmt = gimple_build_assign (and_res, BIT_AND_EXPR,
    1610              :                                           vec_mask, loop_mask);
    1611              : 
    1612          139 :   gsi_insert_before (gsi, and_stmt, GSI_SAME_STMT);
    1613          139 :   return and_res;
    1614              : }
    1615              : 
    1616              : /* Determine whether we can use a gather load or scatter store to vectorize
    1617              :    strided load or store STMT_INFO by truncating the current offset to a
    1618              :    smaller width.  We need to be able to construct an offset vector:
    1619              : 
    1620              :      { 0, X, X*2, X*3, ... }
    1621              : 
    1622              :    without loss of precision, where X is STMT_INFO's DR_STEP.
    1623              : 
    1624              :    Return true if this is possible, describing the gather load or scatter
    1625              :    store in GS_INFO.  MASKED_P is true if the load or store is conditional.
    1626              : 
    1627              :    If we can use gather/scatter and ELSVALS is nonzero the supported
    1628              :    else values will be stored in the vector ELSVALS points to.  */
    1629              : 
    1630              : static bool
    1631        64332 : vect_truncate_gather_scatter_offset (stmt_vec_info stmt_info, tree vectype,
    1632              :                                      loop_vec_info loop_vinfo, bool masked_p,
    1633              :                                      gather_scatter_info *gs_info,
    1634              :                                      vec<int> *elsvals)
    1635              : {
    1636        64332 :   dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
    1637        64332 :   data_reference *dr = dr_info->dr;
    1638        64332 :   tree step = DR_STEP (dr);
    1639        64332 :   if (TREE_CODE (step) != INTEGER_CST)
    1640              :     {
    1641              :       /* ??? Perhaps we could use range information here?  */
    1642        28852 :       if (dump_enabled_p ())
    1643          229 :         dump_printf_loc (MSG_NOTE, vect_location,
    1644              :                          "cannot truncate variable step.\n");
    1645        28852 :       return false;
    1646              :     }
    1647              : 
    1648              :   /* Get the number of bits in an element.  */
    1649        35480 :   scalar_mode element_mode = SCALAR_TYPE_MODE (TREE_TYPE (vectype));
    1650        35480 :   unsigned int element_bits = GET_MODE_BITSIZE (element_mode);
    1651              : 
    1652              :   /* Set COUNT to the upper limit on the number of elements - 1.
    1653              :      Start with the maximum vectorization factor.  */
    1654        35480 :   unsigned HOST_WIDE_INT count = vect_max_vf (loop_vinfo) - 1;
    1655              : 
    1656              :   /* Try lowering COUNT to the number of scalar latch iterations.  */
    1657        35480 :   class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
    1658        35480 :   widest_int max_iters;
    1659        35480 :   if (max_loop_iterations (loop, &max_iters)
    1660        70231 :       && max_iters < count)
    1661         2065 :     count = max_iters.to_shwi ();
    1662              : 
    1663              :   /* Try scales of 1 and the element size.  */
    1664        35480 :   unsigned int scales[] = { 1, vect_get_scalar_dr_size (dr_info) };
    1665        35480 :   wi::overflow_type overflow = wi::OVF_NONE;
    1666       106440 :   for (int i = 0; i < 2; ++i)
    1667              :     {
    1668        70960 :       unsigned int scale = scales[i];
    1669        70960 :       widest_int factor;
    1670        70960 :       if (!wi::multiple_of_p (wi::to_widest (step), scale, SIGNED, &factor))
    1671            0 :         continue;
    1672              : 
    1673              :       /* Determine the minimum precision of (COUNT - 1) * STEP / SCALE.  */
    1674        70960 :       widest_int range = wi::mul (count, factor, SIGNED, &overflow);
    1675        70960 :       if (overflow)
    1676            0 :         continue;
    1677        70960 :       signop sign = range >= 0 ? UNSIGNED : SIGNED;
    1678        70960 :       unsigned int min_offset_bits = wi::min_precision (range, sign);
    1679              : 
    1680              :       /* Find the narrowest viable offset type.  */
    1681        70960 :       unsigned int offset_bits = 1U << ceil_log2 (min_offset_bits);
    1682        70960 :       tree offset_type = build_nonstandard_integer_type (offset_bits,
    1683              :                                                          sign == UNSIGNED);
    1684              : 
    1685              :       /* See whether the target supports the operation with an offset
    1686              :          no narrower than OFFSET_TYPE.  */
    1687        70960 :       tree memory_type = TREE_TYPE (DR_REF (dr));
    1688        70960 :       tree tmp_offset_vectype;
    1689        70960 :       int tmp_scale;
    1690        70960 :       if (!vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr), masked_p,
    1691              :                                      vectype, memory_type, offset_type,
    1692              :                                      scale, &tmp_scale,
    1693              :                                      &gs_info->ifn, &gs_info->offset_vectype,
    1694              :                                      &tmp_offset_vectype, elsvals)
    1695        70960 :           || gs_info->ifn == IFN_LAST)
    1696        70960 :         continue;
    1697              : 
    1698            0 :       gs_info->decl = NULL_TREE;
    1699              :       /* Logically the sum of DR_BASE_ADDRESS, DR_INIT and DR_OFFSET,
    1700              :          but we don't need to store that here.  */
    1701            0 :       gs_info->base = NULL_TREE;
    1702            0 :       gs_info->alias_ptr = build_int_cst
    1703            0 :         (reference_alias_ptr_type (DR_REF (dr)),
    1704            0 :          get_object_alignment (DR_REF (dr)));
    1705            0 :       gs_info->element_type = TREE_TYPE (vectype);
    1706            0 :       gs_info->offset = fold_convert (offset_type, step);
    1707            0 :       gs_info->scale = scale;
    1708            0 :       gs_info->memory_type = memory_type;
    1709            0 :       return true;
    1710       141920 :     }
    1711              : 
    1712        35480 :   if (overflow && dump_enabled_p ())
    1713            0 :     dump_printf_loc (MSG_NOTE, vect_location,
    1714              :                      "truncating gather/scatter offset to %d bits"
    1715              :                      " might change its value.\n", element_bits);
    1716              : 
    1717              :   return false;
    1718        35480 : }
    1719              : 
    1720              : /* Return true if we can use gather/scatter or strided internal functions
    1721              :    to vectorize STMT_INFO, which is a grouped or strided load or store
    1722              :    with multiple lanes and will be implemented by a type-punned access
    1723              :    of a vector with element size that matches the number of lanes.
    1724              : 
    1725              :    MASKED_P is true if load or store is conditional.
    1726              :    When returning true, fill in GS_INFO with the information required to
    1727              :    perform the operation.  Also, store the punning type in PUNNED_VECTYPE.
    1728              : 
    1729              :    If successful and ELSVALS is nonzero the supported
    1730              :    else values will be stored in the vector ELSVALS points to.  */
    1731              : 
    1732              : static bool
    1733         4687 : vect_use_grouped_gather (dr_vec_info *dr_info, tree vectype,
    1734              :                          loop_vec_info loop_vinfo, bool masked_p,
    1735              :                          unsigned int nelts,
    1736              :                          gather_scatter_info *info, vec<int> *elsvals,
    1737              :                          tree *pun_vectype)
    1738              : {
    1739         4687 :   data_reference *dr = dr_info->dr;
    1740              : 
    1741              :   /* TODO: We can support nelts > BITS_PER_UNIT or non-power-of-two by
    1742              :      multiple gathers/scatter.  */
    1743         9047 :   if (nelts > BITS_PER_UNIT || !pow2p_hwi (nelts))
    1744              :     return false;
    1745              : 
    1746              :   /* Pun the vectype with one of the same size but an element spanning
    1747              :      NELTS elements of VECTYPE.
    1748              :      The punned type of a V16QI with NELTS = 4 would be V4SI.
    1749              :      */
    1750         4061 :   tree tmp;
    1751         4061 :   unsigned int pieces;
    1752         4061 :   if (!can_div_trunc_p (TYPE_VECTOR_SUBPARTS (vectype), nelts, &pieces)
    1753         4061 :       || !pieces)
    1754          352 :     return false;
    1755              : 
    1756         3709 :   *pun_vectype = vector_vector_composition_type (vectype, pieces, &tmp, true);
    1757              : 
    1758         3709 :   if (!*pun_vectype || !VECTOR_TYPE_P (*pun_vectype))
    1759              :     return false;
    1760              : 
    1761         3335 :   internal_fn ifn;
    1762         3335 :   tree offset_vectype = *pun_vectype;
    1763              : 
    1764         2231 :   internal_fn strided_ifn = DR_IS_READ (dr)
    1765         3335 :     ? IFN_MASK_LEN_STRIDED_LOAD : IFN_MASK_LEN_STRIDED_STORE;
    1766              : 
    1767              :   /* Check if we have a gather/scatter with the new type.  We're just trying
    1768              :      with the type itself as offset for now.  If not, check if we have a
    1769              :      strided load/store.  These have fewer constraints (for example no offset
    1770              :      type must exist) so it is possible that even though a gather/scatter is
    1771              :      not available we still have a strided load/store.  */
    1772         3335 :   bool ok = false;
    1773         3335 :   tree tmp_vectype;
    1774         3335 :   int tmp_scale;
    1775         3335 :   if (vect_gather_scatter_fn_p
    1776         3335 :       (loop_vinfo, DR_IS_READ (dr), masked_p, *pun_vectype,
    1777         3335 :        TREE_TYPE (*pun_vectype), *pun_vectype, 1, &tmp_scale, &ifn,
    1778              :        &offset_vectype, &tmp_vectype, elsvals))
    1779              :     ok = true;
    1780         3335 :   else if (internal_strided_fn_supported_p (strided_ifn, *pun_vectype,
    1781              :                                             elsvals))
    1782              :     {
    1783              :       /* Use gather/scatter IFNs, vect_get_strided_load_store_ops
    1784              :          will switch back to the strided variants.  */
    1785            0 :       ifn = DR_IS_READ (dr) ? IFN_MASK_LEN_GATHER_LOAD :
    1786              :         IFN_MASK_LEN_SCATTER_STORE;
    1787            0 :       ok = true;
    1788              :     }
    1789              : 
    1790            0 :   if (ok)
    1791              :     {
    1792            0 :       info->ifn = ifn;
    1793            0 :       info->decl = NULL_TREE;
    1794            0 :       info->base = dr->ref;
    1795            0 :       info->alias_ptr = build_int_cst
    1796            0 :         (reference_alias_ptr_type (DR_REF (dr)),
    1797            0 :          get_object_alignment (DR_REF (dr)));
    1798            0 :       info->element_type = TREE_TYPE (*pun_vectype);
    1799            0 :       info->offset_vectype = offset_vectype;
    1800              :       /* No need to set the offset, vect_get_strided_load_store_ops
    1801              :          will do that.  */
    1802            0 :       info->scale = 1;
    1803            0 :       info->memory_type = TREE_TYPE (DR_REF (dr));
    1804            0 :       return true;
    1805              :     }
    1806              : 
    1807              :   return false;
    1808              : }
    1809              : 
    1810              : 
    1811              : /* Return true if we can use gather/scatter internal functions to
    1812              :    vectorize STMT_INFO, which is a grouped or strided load or store.
    1813              :    MASKED_P is true if load or store is conditional.  When returning
    1814              :    true, fill in GS_INFO with the information required to perform the
    1815              :    operation.
    1816              : 
    1817              :    If we can use gather/scatter and ELSVALS is nonzero the supported
    1818              :    else values will be stored in the vector ELSVALS points to.  */
    1819              : 
    1820              : static bool
    1821        64332 : vect_use_strided_gather_scatters_p (stmt_vec_info stmt_info, tree vectype,
    1822              :                                     loop_vec_info loop_vinfo, bool masked_p,
    1823              :                                     gather_scatter_info *gs_info,
    1824              :                                     vec<int> *elsvals,
    1825              :                                     unsigned int group_size,
    1826              :                                     bool single_element_p)
    1827              : {
    1828        64332 :   if (!vect_check_gather_scatter (stmt_info, vectype,
    1829              :                                   loop_vinfo, gs_info, elsvals)
    1830        64332 :       || gs_info->ifn == IFN_LAST)
    1831              :     {
    1832        64332 :       if (!vect_truncate_gather_scatter_offset (stmt_info, vectype, loop_vinfo,
    1833              :                                                 masked_p, gs_info, elsvals))
    1834              :         return false;
    1835              :     }
    1836              : 
    1837            0 :   if (!single_element_p
    1838            0 :       && !targetm.vectorize.prefer_gather_scatter (TYPE_MODE (vectype),
    1839              :                                                    gs_info->scale,
    1840              :                                                    group_size))
    1841              :     return false;
    1842              : 
    1843            0 :   if (dump_enabled_p ())
    1844            0 :     dump_printf_loc (MSG_NOTE, vect_location,
    1845              :                      "using gather/scatter for strided/grouped access,"
    1846              :                      " scale = %d\n", gs_info->scale);
    1847              : 
    1848              :   return true;
    1849              : }
    1850              : 
    1851              : /* STMT_INFO is a non-strided load or store, meaning that it accesses
    1852              :    elements with a known constant step.  Return -1 if that step
    1853              :    is negative, 0 if it is zero, and 1 if it is greater than zero.  */
    1854              : 
    1855              : int
    1856      1476830 : compare_step_with_zero (vec_info *vinfo, stmt_vec_info stmt_info)
    1857              : {
    1858      1476830 :   dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
    1859      1476830 :   return tree_int_cst_compare (vect_dr_behavior (vinfo, dr_info)->step,
    1860      1476830 :                                size_zero_node);
    1861              : }
    1862              : 
    1863              : /* If the target supports a permute mask that reverses the elements in
    1864              :    a vector of type VECTYPE, return that mask, otherwise return null.  */
    1865              : 
    1866              : tree
    1867         9173 : perm_mask_for_reverse (tree vectype)
    1868              : {
    1869         9173 :   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
    1870              : 
    1871              :   /* The encoding has a single stepped pattern.  */
    1872         9173 :   vec_perm_builder sel (nunits, 1, 3);
    1873        36692 :   for (int i = 0; i < 3; ++i)
    1874        27519 :     sel.quick_push (nunits - 1 - i);
    1875              : 
    1876         9173 :   vec_perm_indices indices (sel, 1, nunits);
    1877         9173 :   if (!can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype),
    1878              :                              indices))
    1879              :     return NULL_TREE;
    1880         8021 :   return vect_gen_perm_mask_checked (vectype, indices);
    1881         9173 : }
    1882              : 
    1883              : /* A subroutine of get_load_store_type, with a subset of the same
    1884              :    arguments.  Handle the case where STMT_INFO is a load or store that
    1885              :    accesses consecutive elements with a negative step.  Sets *POFFSET
    1886              :    to the offset to be applied to the DR for the first access.  */
    1887              : 
    1888              : static vect_memory_access_type
    1889        12220 : get_negative_load_store_type (vec_info *vinfo,
    1890              :                               stmt_vec_info stmt_info, tree vectype,
    1891              :                               vec_load_store_type vls_type,
    1892              :                               unsigned int ncopies, poly_int64 *poffset)
    1893              : {
    1894        12220 :   dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
    1895        12220 :   dr_alignment_support alignment_support_scheme;
    1896              : 
    1897        12220 :   if (ncopies > 1)
    1898              :     {
    1899            0 :       if (dump_enabled_p ())
    1900            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1901              :                          "multiple types with negative step.\n");
    1902            0 :       return VMAT_ELEMENTWISE;
    1903              :     }
    1904              : 
    1905              :   /* For backward running DRs the first access in vectype actually is
    1906              :      N-1 elements before the address of the DR.  */
    1907        12220 :   *poffset = ((-TYPE_VECTOR_SUBPARTS (vectype) + 1)
    1908        12220 :               * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
    1909              : 
    1910        12220 :   int misalignment = dr_misalignment (dr_info, vectype, *poffset);
    1911        12220 :   alignment_support_scheme
    1912        12220 :     = vect_supportable_dr_alignment (vinfo, dr_info, vectype, misalignment);
    1913        12220 :   if (alignment_support_scheme != dr_aligned
    1914        12220 :       && alignment_support_scheme != dr_unaligned_supported)
    1915              :     {
    1916         4434 :       if (dump_enabled_p ())
    1917            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1918              :                          "negative step but alignment required.\n");
    1919         4434 :       *poffset = 0;
    1920         4434 :       return VMAT_ELEMENTWISE;
    1921              :     }
    1922              : 
    1923         7786 :   if (vls_type == VLS_STORE_INVARIANT)
    1924              :     {
    1925         1197 :       if (dump_enabled_p ())
    1926           21 :         dump_printf_loc (MSG_NOTE, vect_location,
    1927              :                          "negative step with invariant source;"
    1928              :                          " no permute needed.\n");
    1929         1197 :       return VMAT_CONTIGUOUS_DOWN;
    1930              :     }
    1931              : 
    1932         6589 :   if (!perm_mask_for_reverse (vectype))
    1933              :     {
    1934         1152 :       if (dump_enabled_p ())
    1935           52 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    1936              :                          "negative step and reversing not supported.\n");
    1937         1152 :       *poffset = 0;
    1938         1152 :       return VMAT_ELEMENTWISE;
    1939              :     }
    1940              : 
    1941              :   return VMAT_CONTIGUOUS_REVERSE;
    1942              : }
    1943              : 
    1944              : /* STMT_INFO is either a masked or unconditional store.  Return the value
    1945              :    being stored.  */
    1946              : 
    1947              : tree
    1948            0 : vect_get_store_rhs (stmt_vec_info stmt_info)
    1949              : {
    1950            0 :   if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
    1951              :     {
    1952            0 :       gcc_assert (gimple_assign_single_p (assign));
    1953            0 :       return gimple_assign_rhs1 (assign);
    1954              :     }
    1955            0 :   if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
    1956              :     {
    1957            0 :       internal_fn ifn = gimple_call_internal_fn (call);
    1958            0 :       int index = internal_fn_stored_value_index (ifn);
    1959            0 :       gcc_assert (index >= 0);
    1960            0 :       return gimple_call_arg (call, index);
    1961              :     }
    1962            0 :   gcc_unreachable ();
    1963              : }
    1964              : 
    1965              : /* Function VECTOR_VECTOR_COMPOSITION_TYPE
    1966              : 
    1967              :    This function returns a vector type which can be composed with NELTS pieces,
    1968              :    whose type is recorded in PTYPE.  VTYPE should be a vector type, and has the
    1969              :    same vector size as the return vector.  It checks target whether supports
    1970              :    pieces-size vector mode for construction firstly, if target fails to, check
    1971              :    pieces-size scalar mode for construction further.  It returns NULL_TREE if
    1972              :    fails to find the available composition.  If the caller only wants scalar
    1973              :    pieces where PTYPE e.g. is a possible gather/scatter element type
    1974              :    SCALAR_PTYPE_ONLY must be true.
    1975              : 
    1976              :    For example, for (vtype=V16QI, nelts=4), we can probably get:
    1977              :      - V16QI with PTYPE V4QI.
    1978              :      - V4SI with PTYPE SI.
    1979              :      - NULL_TREE.  */
    1980              : 
    1981              : static tree
    1982        13936 : vector_vector_composition_type (tree vtype, poly_uint64 nelts, tree *ptype,
    1983              :                                 bool scalar_ptype_only)
    1984              : {
    1985        13936 :   gcc_assert (VECTOR_TYPE_P (vtype));
    1986        13936 :   gcc_assert (known_gt (nelts, 0U));
    1987              : 
    1988        13936 :   machine_mode vmode = TYPE_MODE (vtype);
    1989        13936 :   if (!VECTOR_MODE_P (vmode))
    1990              :     return NULL_TREE;
    1991              : 
    1992              :   /* When we are asked to compose the vector from its components let
    1993              :      that happen directly.  */
    1994        13936 :   if (known_eq (TYPE_VECTOR_SUBPARTS (vtype), nelts))
    1995              :     {
    1996         5947 :       *ptype = TREE_TYPE (vtype);
    1997         5947 :       return vtype;
    1998              :     }
    1999              : 
    2000        15978 :   poly_uint64 vbsize = GET_MODE_BITSIZE (vmode);
    2001         7989 :   unsigned int pbsize;
    2002         7989 :   if (constant_multiple_p (vbsize, nelts, &pbsize))
    2003              :     {
    2004              :       /* First check if vec_init optab supports construction from
    2005              :          vector pieces directly.  */
    2006         7989 :       scalar_mode elmode = SCALAR_TYPE_MODE (TREE_TYPE (vtype));
    2007        15978 :       poly_uint64 inelts = pbsize / GET_MODE_BITSIZE (elmode);
    2008         7989 :       machine_mode rmode;
    2009         7989 :       if (!scalar_ptype_only
    2010         4280 :           && related_vector_mode (vmode, elmode, inelts).exists (&rmode)
    2011        11861 :           && (convert_optab_handler (vec_init_optab, vmode, rmode)
    2012              :               != CODE_FOR_nothing))
    2013              :         {
    2014         3218 :           *ptype = build_vector_type (TREE_TYPE (vtype), inelts);
    2015         3218 :           return vtype;
    2016              :         }
    2017              : 
    2018              :       /* Otherwise check if exists an integer type of the same piece size and
    2019              :          if vec_init optab supports construction from it directly.  */
    2020         4771 :       if (int_mode_for_size (pbsize, 0).exists (&elmode)
    2021         4771 :           && related_vector_mode (vmode, elmode, nelts).exists (&rmode))
    2022              :         {
    2023         4367 :           if (scalar_ptype_only
    2024         4367 :               || convert_optab_handler (vec_init_optab, rmode, elmode)
    2025              :               != CODE_FOR_nothing)
    2026              :             {
    2027         4367 :               *ptype = build_nonstandard_integer_type (pbsize, 1);
    2028         4367 :               return build_vector_type (*ptype, nelts);
    2029              :             }
    2030              :         }
    2031              :     }
    2032              : 
    2033              :   return NULL_TREE;
    2034              : }
    2035              : 
    2036              : /* Check if the load permutation of NODE only refers to a consecutive
    2037              :    subset of the group indices where GROUP_SIZE is the size of the
    2038              :    dataref's group.  We also assert that the length of the permutation
    2039              :    divides the group size and is a power of two.
    2040              :    Such load permutations can be elided in strided access schemes as
    2041              :    we can "jump over" the gap they leave.  */
    2042              : 
    2043              : bool
    2044        45390 : has_consecutive_load_permutation (slp_tree node, unsigned group_size)
    2045              : {
    2046        45390 :   load_permutation_t perm = SLP_TREE_LOAD_PERMUTATION (node);
    2047        45390 :   if (!perm.exists ()
    2048         1935 :       || perm.length () <= 1
    2049          491 :       || !pow2p_hwi (perm.length ())
    2050        45865 :       || group_size % perm.length ())
    2051              :     return false;
    2052              : 
    2053          428 :   return vect_load_perm_consecutive_p (node);
    2054              : }
    2055              : 
    2056              : 
    2057              : /* Analyze load or store SLP_NODE of type VLS_TYPE.  Return true
    2058              :    if there is a memory access type that the vectorized form can use,
    2059              :    storing it in *MEMORY_ACCESS_TYPE if so.  If we decide to use gathers
    2060              :    or scatters, fill in GS_INFO accordingly.  In addition
    2061              :    *ALIGNMENT_SUPPORT_SCHEME is filled out and false is returned if
    2062              :    the target does not support the alignment scheme.  *MISALIGNMENT
    2063              :    is set according to the alignment of the access (including
    2064              :    DR_MISALIGNMENT_UNKNOWN when it is unknown).
    2065              : 
    2066              :    MASKED_P is true if the statement is conditional on a vectorized mask.
    2067              :    VECTYPE is the vector type that the vectorized statements will use.
    2068              : 
    2069              :    If ELSVALS is nonzero the supported else values will be stored in the
    2070              :    vector ELSVALS points to.  */
    2071              : 
    2072              : static bool
    2073      1363031 : get_load_store_type (vec_info  *vinfo, stmt_vec_info stmt_info,
    2074              :                      tree vectype, slp_tree slp_node,
    2075              :                      bool masked_p, vec_load_store_type vls_type,
    2076              :                      vect_load_store_data *ls)
    2077              : {
    2078      1363031 :   vect_memory_access_type *memory_access_type = &ls->memory_access_type;
    2079      1363031 :   poly_int64 *poffset = &ls->poffset;
    2080      1363031 :   dr_alignment_support *alignment_support_scheme
    2081              :     = &ls->alignment_support_scheme;
    2082      1363031 :   int *misalignment = &ls->misalignment;
    2083      1363031 :   internal_fn *lanes_ifn = &ls->lanes_ifn;
    2084      1363031 :   vec<int> *elsvals = &ls->elsvals;
    2085      1363031 :   tree *ls_type = &ls->ls_type;
    2086      1363031 :   bool *slp_perm = &ls->slp_perm;
    2087      1363031 :   unsigned *n_perms = &ls->n_perms;
    2088      1363031 :   unsigned *n_loads = &ls->n_loads;
    2089      1363031 :   tree *supported_offset_vectype = &ls->supported_offset_vectype;
    2090      1363031 :   int *supported_scale = &ls->supported_scale;
    2091      1363031 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    2092      1363031 :   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
    2093      1363031 :   class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
    2094      1363031 :   stmt_vec_info first_stmt_info;
    2095      1363031 :   unsigned int group_size;
    2096      1363031 :   unsigned HOST_WIDE_INT gap;
    2097      1363031 :   bool single_element_p;
    2098      1363031 :   poly_int64 neg_ldst_offset = 0;
    2099              : 
    2100      1363031 :   *misalignment = DR_MISALIGNMENT_UNKNOWN;
    2101      1363031 :   *poffset = 0;
    2102      1363031 :   *ls_type = NULL_TREE;
    2103      1363031 :   *slp_perm = false;
    2104      1363031 :   *n_perms = -1U;
    2105      1363031 :   *n_loads = -1U;
    2106      1363031 :   ls->subchain_p = false;
    2107              : 
    2108      1363031 :   bool perm_ok = true;
    2109      1363031 :   poly_int64 vf = loop_vinfo ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
    2110              : 
    2111      1363031 :   if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
    2112        72748 :     perm_ok = vect_transform_slp_perm_load (vinfo, slp_node, vNULL, NULL,
    2113        72748 :                                             vf, true, n_perms, n_loads);
    2114              : 
    2115      1363031 :   if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
    2116              :     {
    2117       875809 :       first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
    2118       875809 :       group_size = DR_GROUP_SIZE (first_stmt_info);
    2119       875809 :       gap = DR_GROUP_GAP (first_stmt_info);
    2120       875809 :       single_element_p = (stmt_info == first_stmt_info
    2121       875809 :                           && !DR_GROUP_NEXT_ELEMENT (stmt_info));
    2122              :     }
    2123              :   else
    2124              :     {
    2125              :       first_stmt_info = stmt_info;
    2126              :       group_size = 1;
    2127              :       gap = 0;
    2128              :       single_element_p = true;
    2129              :     }
    2130      1363031 :   dr_vec_info *first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
    2131              : 
    2132              :   /* True if the vectorized statements would access beyond the last
    2133              :      statement in the group.  */
    2134      1363031 :   bool overrun_p = false;
    2135              : 
    2136              :   /* True if we can cope with such overrun by peeling for gaps, so that
    2137              :      there is at least one final scalar iteration after the vector loop.  */
    2138      2726062 :   bool can_overrun_p = (!masked_p
    2139      1363031 :                         && vls_type == VLS_LOAD
    2140       542509 :                         && loop_vinfo
    2141      1774490 :                         && !loop->inner);
    2142              : 
    2143              :   /* There can only be a gap at the end of the group if the stride is
    2144              :      known at compile time.  */
    2145      1363031 :   gcc_assert (!STMT_VINFO_STRIDED_P (first_stmt_info) || gap == 0);
    2146              : 
    2147              :   /* For SLP vectorization we directly vectorize a subchain
    2148              :      without permutation.  */
    2149      1363031 :   if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
    2150      1290283 :     first_dr_info = STMT_VINFO_DR_INFO (SLP_TREE_SCALAR_STMTS (slp_node)[0]);
    2151              : 
    2152      1363031 :   if (STMT_VINFO_STRIDED_P (first_stmt_info))
    2153              :     {
    2154              :       /* Try to use consecutive accesses of as many elements as possible,
    2155              :          separated by the stride, until we have a complete vector.
    2156              :          Fall back to scalar accesses if that isn't possible.  */
    2157        45390 :       *memory_access_type = VMAT_STRIDED_SLP;
    2158              : 
    2159              :       /* If the load permutation is consecutive we can reduce the group to
    2160              :          the elements the permutation accesses.  Then we release the
    2161              :          permutation.  */
    2162        45390 :       if (has_consecutive_load_permutation (slp_node, group_size))
    2163              :         {
    2164           32 :           ls->subchain_p = true;
    2165           32 :           group_size = SLP_TREE_LANES (slp_node);
    2166           32 :           SLP_TREE_LOAD_PERMUTATION (slp_node).release ();
    2167              :         }
    2168              :     }
    2169      1317641 :   else if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
    2170              :     {
    2171        10848 :       slp_tree offset_node = SLP_TREE_CHILDREN (slp_node)[0];
    2172        10848 :       tree offset_vectype = SLP_TREE_VECTYPE (offset_node);
    2173        10848 :       int scale = SLP_TREE_GS_SCALE (slp_node);
    2174        10848 :       tree memory_type = TREE_TYPE (DR_REF (first_dr_info->dr));
    2175        10848 :       tree tem;
    2176        10848 :       if (vect_gather_scatter_fn_p (loop_vinfo, vls_type == VLS_LOAD,
    2177              :                                     masked_p, vectype, memory_type,
    2178              :                                     offset_vectype, scale, supported_scale,
    2179              :                                     &ls->gs.ifn, &tem,
    2180              :                                     supported_offset_vectype, elsvals))
    2181              :         {
    2182            0 :           if (dump_enabled_p ())
    2183              :             {
    2184            0 :               dump_printf_loc (MSG_NOTE, vect_location,
    2185              :                                "gather/scatter with required "
    2186              :                                "offset type "
    2187              :                                "%T and offset scale %d.\n",
    2188              :                                offset_vectype, scale);
    2189            0 :               if (*supported_offset_vectype)
    2190            0 :                 dump_printf_loc (MSG_NOTE, vect_location,
    2191              :                                  " target supports offset type %T.\n",
    2192              :                                  *supported_offset_vectype);
    2193            0 :               if (*supported_scale)
    2194            0 :                 dump_printf_loc (MSG_NOTE, vect_location,
    2195              :                                  " target supports offset scale %d.\n",
    2196              :                                  *supported_scale);
    2197              :             }
    2198            0 :           *memory_access_type = VMAT_GATHER_SCATTER_IFN;
    2199              :         }
    2200        10848 :       else if (vls_type == VLS_LOAD
    2201        10848 :                ? (targetm.vectorize.builtin_gather
    2202         9282 :                   && (ls->gs.decl
    2203         9282 :                         = targetm.vectorize.builtin_gather (vectype,
    2204         9282 :                                                             TREE_TYPE
    2205              :                                                               (offset_vectype),
    2206              :                                                             scale)))
    2207         1566 :                : (targetm.vectorize.builtin_scatter
    2208         1566 :                   && (ls->gs.decl
    2209         1566 :                         = targetm.vectorize.builtin_scatter (vectype,
    2210         1566 :                                                              TREE_TYPE
    2211              :                                                                (offset_vectype),
    2212              :                                                              scale))))
    2213          574 :         *memory_access_type = VMAT_GATHER_SCATTER_LEGACY;
    2214              :       else
    2215              :         {
    2216              :           /* GATHER_SCATTER_EMULATED_P.  */
    2217        10274 :           if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant ()
    2218        10274 :               || !TYPE_VECTOR_SUBPARTS (offset_vectype).is_constant ()
    2219        10274 :               || VECTOR_BOOLEAN_TYPE_P (offset_vectype)
    2220        10274 :               || !constant_multiple_p (TYPE_VECTOR_SUBPARTS (offset_vectype),
    2221        10274 :                                        TYPE_VECTOR_SUBPARTS (vectype)))
    2222              :             {
    2223         2692 :               if (dump_enabled_p ())
    2224          466 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2225              :                                  "unsupported vector types for emulated "
    2226              :                                  "gather.\n");
    2227         2692 :               return false;
    2228              :             }
    2229         7582 :           *memory_access_type = VMAT_GATHER_SCATTER_EMULATED;
    2230              :         }
    2231              :     }
    2232              :   else
    2233              :     {
    2234      1306793 :       int cmp = compare_step_with_zero (vinfo, stmt_info);
    2235      1306793 :       if (cmp < 0)
    2236              :         {
    2237        12402 :           if (single_element_p)
    2238              :             /* ???  The VMAT_CONTIGUOUS_REVERSE code generation is
    2239              :                only correct for single element "interleaving" SLP.  */
    2240        12220 :             *memory_access_type = get_negative_load_store_type
    2241        12220 :                 (vinfo, stmt_info, vectype, vls_type, 1,
    2242              :                  &neg_ldst_offset);
    2243              :           else
    2244              :             /* We can fall back to VMAT_STRIDED_SLP since that does
    2245              :                not care whether the stride between the group instances
    2246              :                is positive or negative.  */
    2247          182 :             *memory_access_type = VMAT_STRIDED_SLP;
    2248              :         }
    2249      1294391 :       else if (cmp == 0 && loop_vinfo)
    2250              :         {
    2251         3381 :           gcc_assert (vls_type == VLS_LOAD);
    2252         3381 :           *memory_access_type = VMAT_INVARIANT;
    2253              :         }
    2254              :       /* Try using LOAD/STORE_LANES.  */
    2255      1291010 :       else if (slp_node->ldst_lanes
    2256      1291010 :                && (*lanes_ifn
    2257            0 :                    = (vls_type == VLS_LOAD
    2258            0 :                       ? vect_load_lanes_supported (vectype, group_size,
    2259              :                                                    masked_p, elsvals)
    2260            0 :                       : vect_store_lanes_supported (vectype, group_size,
    2261              :                                                     masked_p))) != IFN_LAST)
    2262            0 :         *memory_access_type = VMAT_LOAD_STORE_LANES;
    2263      1291010 :       else if (!loop_vinfo && slp_node->avoid_stlf_fail)
    2264              :         {
    2265           70 :           *memory_access_type = VMAT_ELEMENTWISE;
    2266           70 :           if (dump_enabled_p ())
    2267            2 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2268              :                              "using element-wise load to avoid disrupting "
    2269              :                              "cross iteration store-to-load forwarding\n");
    2270              :         }
    2271              :       else
    2272      1290940 :         *memory_access_type = VMAT_CONTIGUOUS;
    2273              : 
    2274              :       /* If this is single-element interleaving with an element
    2275              :          distance that leaves unused vector loads around fall back
    2276              :          to elementwise access if possible - we otherwise least
    2277              :          create very sub-optimal code in that case (and
    2278              :          blow up memory, see PR65518).  */
    2279      1306793 :       if (loop_vinfo
    2280      1306793 :           && single_element_p
    2281       467270 :           && (*memory_access_type == VMAT_CONTIGUOUS
    2282        15601 :               || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
    2283      1774063 :           && maybe_gt (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
    2284              :         {
    2285        17810 :           *memory_access_type = VMAT_ELEMENTWISE;
    2286        17810 :           if (dump_enabled_p ())
    2287          198 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2288              :                              "single-element interleaving not supported "
    2289              :                              "for not adjacent vector loads, using "
    2290              :                              "elementwise access\n");
    2291              :         }
    2292              : 
    2293              :       /* Also fall back to elementwise access in case we did not lower a
    2294              :          permutation and cannot code generate it.  */
    2295      1306793 :       if (loop_vinfo
    2296       521734 :           && *memory_access_type != VMAT_ELEMENTWISE
    2297       498338 :           && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
    2298      1335289 :           && !perm_ok)
    2299              :         {
    2300         2053 :           *memory_access_type = VMAT_ELEMENTWISE;
    2301         2053 :           if (dump_enabled_p ())
    2302          246 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2303              :                              "permutation not supported, using elementwise "
    2304              :                              "access\n");
    2305              :         }
    2306              : 
    2307       521734 :       overrun_p = (loop_vinfo && gap != 0
    2308      1349701 :                    && *memory_access_type != VMAT_ELEMENTWISE);
    2309      1306793 :       if (overrun_p && vls_type != VLS_LOAD)
    2310              :         {
    2311            0 :           dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2312              :                            "Grouped store with gaps requires"
    2313              :                            " non-consecutive accesses\n");
    2314            9 :           return false;
    2315              :         }
    2316              : 
    2317      1306793 :       unsigned HOST_WIDE_INT dr_size = vect_get_scalar_dr_size (first_dr_info);
    2318      1306793 :       poly_int64 off = 0;
    2319      1306793 :       if (*memory_access_type == VMAT_CONTIGUOUS_REVERSE)
    2320         5278 :         off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
    2321              : 
    2322              :       /* An overrun is fine if the trailing elements are smaller
    2323              :          than the alignment boundary B.  Every vector access will
    2324              :          be a multiple of B and so we are guaranteed to access a
    2325              :          non-gap element in the same B-sized block.  */
    2326      1306793 :       if (overrun_p
    2327      1306793 :           && gap < (vect_known_alignment_in_bytes (first_dr_info,
    2328        22958 :                                                    vectype, off) / dr_size))
    2329              :         overrun_p = false;
    2330              : 
    2331              :       /* When we have a contiguous access across loop iterations
    2332              :          but the access in the loop doesn't cover the full vector
    2333              :          we can end up with no gap recorded but still excess
    2334              :          elements accessed, see PR103116.  Make sure we peel for
    2335              :          gaps if necessary and sufficient and give up if not.
    2336              : 
    2337              :          If there is a combination of the access not covering the full
    2338              :          vector and a gap recorded then we may need to peel twice.  */
    2339      1306793 :       bool large_vector_overrun_p = false;
    2340      1306793 :       if (loop_vinfo
    2341       521734 :           && (*memory_access_type == VMAT_CONTIGUOUS
    2342        35484 :               || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
    2343       491528 :           && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
    2344      1332819 :           && !multiple_p (group_size * LOOP_VINFO_VECT_FACTOR (loop_vinfo),
    2345              :                           nunits))
    2346              :         large_vector_overrun_p = overrun_p = true;
    2347              : 
    2348              :       /* If the gap splits the vector in half and the target
    2349              :          can do half-vector operations avoid the epilogue peeling
    2350              :          by simply loading half of the vector only.  Usually
    2351              :          the construction with an upper zero half will be elided.  */
    2352      1306793 :       dr_alignment_support alss;
    2353      1306793 :       int misalign = dr_misalignment (first_dr_info, vectype, off);
    2354      1306793 :       tree half_vtype;
    2355      1306793 :       poly_uint64 remain;
    2356      1306793 :       unsigned HOST_WIDE_INT tem, num;
    2357      1306793 :       if (overrun_p
    2358      1306793 :           && !masked_p
    2359        17447 :           && *memory_access_type != VMAT_LOAD_STORE_LANES
    2360        17447 :           && (((alss = vect_supportable_dr_alignment (vinfo, first_dr_info,
    2361              :                                                       vectype, misalign)))
    2362              :               == dr_aligned
    2363        14971 :               || alss == dr_unaligned_supported)
    2364         9879 :           && can_div_trunc_p (group_size
    2365         9879 :                               * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
    2366              :                               nunits, &tem, &remain)
    2367      1316672 :           && (known_eq (remain, 0u)
    2368         7424 :               || (known_ne (remain, 0u)
    2369         5763 :                   && constant_multiple_p (nunits, remain, &num)
    2370      1304338 :                   && (vector_vector_composition_type (vectype, num, &half_vtype)
    2371              :                       != NULL_TREE))))
    2372         8218 :         overrun_p = false;
    2373              : 
    2374      1306793 :       if (overrun_p && !can_overrun_p)
    2375              :         {
    2376            6 :           if (dump_enabled_p ())
    2377            6 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2378              :                              "Peeling for outer loop is not supported\n");
    2379            6 :           return false;
    2380              :         }
    2381              : 
    2382              :       /* Peeling for gaps assumes that a single scalar iteration
    2383              :          is enough to make sure the last vector iteration doesn't
    2384              :          access excess elements.  */
    2385      1306787 :       if (overrun_p
    2386      1306787 :           && (!can_div_trunc_p (group_size
    2387         9223 :                                 * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
    2388              :                                 nunits, &tem, &remain)
    2389         9223 :               || maybe_lt (remain + group_size, nunits)))
    2390              :         {
    2391              :           /* But peeling a single scalar iteration is enough if
    2392              :              we can use the next power-of-two sized partial
    2393              :              access and that is sufficiently small to be covered
    2394              :              by the single scalar iteration.  */
    2395           16 :           unsigned HOST_WIDE_INT cnunits, cvf, cremain, cpart_size;
    2396           16 :           if (masked_p
    2397           16 :               || !nunits.is_constant (&cnunits)
    2398           16 :               || !LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant (&cvf)
    2399           16 :               || (((cremain = (group_size * cvf - gap) % cnunits), true)
    2400           16 :                   && ((cpart_size = (1 << ceil_log2 (cremain))), true)
    2401           16 :                   && (cremain + group_size < cpart_size
    2402           13 :                       || (vector_vector_composition_type (vectype,
    2403           13 :                                                          cnunits / cpart_size,
    2404              :                                                          &half_vtype)
    2405              :                           == NULL_TREE))))
    2406              :             {
    2407              :               /* If all fails we can still resort to niter masking unless
    2408              :                  the vectors used are too big, so enforce the use of
    2409              :                  partial vectors.  */
    2410            3 :               if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
    2411            3 :                   && !large_vector_overrun_p)
    2412              :                 {
    2413            0 :                   if (dump_enabled_p ())
    2414            0 :                     dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2415              :                                      "peeling for gaps insufficient for "
    2416              :                                      "access unless using partial "
    2417              :                                      "vectors\n");
    2418            0 :                   LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
    2419              :                 }
    2420              :               else
    2421              :                 {
    2422            3 :                   if (dump_enabled_p ())
    2423            3 :                     dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2424              :                                      "peeling for gaps insufficient for "
    2425              :                                      "access\n");
    2426            3 :                   return false;
    2427              :                 }
    2428              :             }
    2429           13 :           else if (large_vector_overrun_p)
    2430              :             {
    2431           13 :               if (dump_enabled_p ())
    2432           12 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2433              :                                  "can't operate on partial vectors because "
    2434              :                                  "only unmasked loads handle access "
    2435              :                                  "shortening required because of gaps at "
    2436              :                                  "the end of the access\n");
    2437           13 :               LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    2438              :             }
    2439              :         }
    2440              :     }
    2441              : 
    2442              :   /* As a last resort, trying using a gather load or scatter store.
    2443              : 
    2444              :      ??? Although the code can handle all group sizes correctly,
    2445              :      it probably isn't a win to use separate strided accesses based
    2446              :      on nearby locations.  Or, even if it's a win over scalar code,
    2447              :      it might not be a win over vectorizing at a lower VF, if that
    2448              :      allows us to use contiguous accesses.  */
    2449      1360330 :   vect_memory_access_type grouped_gather_fallback = VMAT_UNINITIALIZED;
    2450      1360330 :   if (loop_vinfo
    2451       575271 :       && (*memory_access_type == VMAT_ELEMENTWISE
    2452       575271 :           || *memory_access_type == VMAT_STRIDED_SLP))
    2453              :     {
    2454        71018 :       gather_scatter_info gs_info;
    2455        71018 :       tree tem;
    2456        71018 :       if (SLP_TREE_LANES (slp_node) == 1
    2457        66136 :           && (!SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
    2458        21358 :               || single_element_p)
    2459       135350 :           && vect_use_strided_gather_scatters_p (stmt_info, vectype, loop_vinfo,
    2460              :                                                  masked_p, &gs_info, elsvals,
    2461              :                                                  group_size, single_element_p))
    2462              :         {
    2463              :           /* vect_use_strided_gather_scatters_p does not save the actually
    2464              :              supported scale and offset type so do that here.
    2465              :              We need it later in check_load_store_for_partial_vectors
    2466              :              where we only check if the given internal function is supported
    2467              :              (to choose whether to use the IFN, LEGACY, or EMULATED flavor
    2468              :              of gather/scatter) and don't re-do the full analysis.  */
    2469            0 :           tree tmp;
    2470            0 :           gcc_assert (vect_gather_scatter_fn_p
    2471              :                       (loop_vinfo, vls_type == VLS_LOAD, masked_p, vectype,
    2472              :                        gs_info.memory_type, TREE_TYPE (gs_info.offset),
    2473              :                        gs_info.scale, supported_scale, &gs_info.ifn,
    2474              :                        &tmp, supported_offset_vectype, elsvals));
    2475              : 
    2476            0 :           SLP_TREE_GS_SCALE (slp_node) = gs_info.scale;
    2477            0 :           SLP_TREE_GS_BASE (slp_node) = error_mark_node;
    2478            0 :           ls->gs.ifn = gs_info.ifn;
    2479            0 :           ls->strided_offset_vectype = gs_info.offset_vectype;
    2480            0 :           *memory_access_type = VMAT_GATHER_SCATTER_IFN;
    2481              :         }
    2482        71018 :       else if (SLP_TREE_LANES (slp_node) > 1
    2483              :                && !masked_p
    2484         4882 :                && !single_element_p
    2485        75705 :                && vect_use_grouped_gather (STMT_VINFO_DR_INFO (stmt_info),
    2486              :                                            vectype, loop_vinfo,
    2487              :                                            masked_p, group_size,
    2488              :                                            &gs_info, elsvals, &tem))
    2489              :         {
    2490            0 :           SLP_TREE_GS_SCALE (slp_node) = gs_info.scale;
    2491            0 :           SLP_TREE_GS_BASE (slp_node) = error_mark_node;
    2492            0 :           grouped_gather_fallback = *memory_access_type;
    2493            0 :           *memory_access_type = VMAT_GATHER_SCATTER_IFN;
    2494            0 :           ls->gs.ifn = gs_info.ifn;
    2495            0 :           vectype = *ls_type = tem;
    2496            0 :           ls->strided_offset_vectype = gs_info.offset_vectype;
    2497              :         }
    2498              :     }
    2499              : 
    2500      1360330 :   if (*memory_access_type == VMAT_CONTIGUOUS_DOWN
    2501      1360330 :       || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
    2502         6471 :     *poffset = neg_ldst_offset;
    2503              : 
    2504      1360330 :   if (*memory_access_type == VMAT_ELEMENTWISE
    2505      1334811 :       || *memory_access_type == VMAT_GATHER_SCATTER_LEGACY
    2506      1334237 :       || *memory_access_type == VMAT_STRIDED_SLP
    2507      1288668 :       || *memory_access_type == VMAT_INVARIANT)
    2508              :     {
    2509        75043 :       *alignment_support_scheme = dr_unaligned_supported;
    2510        75043 :       *misalignment = DR_MISALIGNMENT_UNKNOWN;
    2511              :     }
    2512              :   else
    2513              :     {
    2514      1285287 :       if (mat_gather_scatter_p (*memory_access_type)
    2515              :           && !first_dr_info)
    2516              :         *misalignment = DR_MISALIGNMENT_UNKNOWN;
    2517              :       else
    2518      1285287 :         *misalignment = dr_misalignment (first_dr_info, vectype, *poffset);
    2519      1285287 :       *alignment_support_scheme
    2520      1285287 :         = vect_supportable_dr_alignment
    2521      1285287 :            (vinfo, first_dr_info, vectype, *misalignment,
    2522      1285287 :             mat_gather_scatter_p (*memory_access_type));
    2523      1285287 :       if (grouped_gather_fallback != VMAT_UNINITIALIZED
    2524            0 :           && *alignment_support_scheme != dr_aligned
    2525            0 :           && *alignment_support_scheme != dr_unaligned_supported)
    2526              :         {
    2527              :           /* No supportable alignment for a grouped gather, fall back to the
    2528              :              original memory access type.  Even though VMAT_STRIDED_SLP might
    2529              :              also try aligned vector loads it can still choose vector
    2530              :              construction from scalars.  */
    2531            0 :           *memory_access_type = grouped_gather_fallback;
    2532            0 :           *alignment_support_scheme = dr_unaligned_supported;
    2533            0 :           *misalignment = DR_MISALIGNMENT_UNKNOWN;
    2534              :         }
    2535              :     }
    2536              : 
    2537      1360330 :   if (overrun_p)
    2538              :     {
    2539         9220 :       gcc_assert (can_overrun_p);
    2540         9220 :       if (dump_enabled_p ())
    2541          511 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2542              :                          "Data access with gaps requires scalar "
    2543              :                          "epilogue loop\n");
    2544         9220 :       LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
    2545              :     }
    2546              : 
    2547      1360330 :   if ((*memory_access_type == VMAT_ELEMENTWISE
    2548      1360330 :        || *memory_access_type == VMAT_STRIDED_SLP)
    2549              :       && !nunits.is_constant ())
    2550              :     {
    2551              :       if (dump_enabled_p ())
    2552              :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2553              :                          "Not using elementwise accesses due to variable "
    2554              :                          "vectorization factor.\n");
    2555              :       return false;
    2556              :     }
    2557              : 
    2558              :   /* Checks if all scalar iterations are known to be inbounds.  */
    2559      1360330 :   bool inbounds = DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (stmt_info));
    2560              : 
    2561              :   /* Check if we support the operation if early breaks are needed.  Here we
    2562              :      must ensure that we don't access any more than the scalar code would
    2563              :      have.  A masked operation would ensure this, so for these load types
    2564              :      force masking.  */
    2565      1360330 :   if (loop_vinfo
    2566       575271 :       && dr_safe_speculative_read_required (stmt_info)
    2567      1539297 :       && LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
    2568              :     {
    2569       178967 :       if (mat_gather_scatter_p (*memory_access_type)
    2570       178967 :           || *memory_access_type == VMAT_STRIDED_SLP)
    2571              :         {
    2572         9252 :           if (dump_enabled_p ())
    2573            8 :             dump_printf_loc (MSG_NOTE, vect_location,
    2574              :                              "early break not supported: cannot peel for "
    2575              :                              "alignment. With non-contiguous memory vectorization"
    2576              :                              " could read out of bounds at %G ",
    2577              :                              STMT_VINFO_STMT (stmt_info));
    2578         9252 :           if (inbounds)
    2579            0 :             LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
    2580              :           else
    2581              :             return false;
    2582              :         }
    2583              :       /* Block-level alignment: Even though individual accesses of
    2584              :          VMAT_ELEMENTWISE type do not cause alignment problems, loading the
    2585              :          whole vector's worth of values in a speculative early-break context
    2586              :          might cross a page boundary.  Set the alignment scheme to `dr_aligned'
    2587              :          here in order to force checking of whether such accesses meet
    2588              :          alignment criteria.  */
    2589       169715 :       else if (*memory_access_type == VMAT_ELEMENTWISE && !inbounds)
    2590        14941 :         *alignment_support_scheme = dr_aligned;
    2591              :     }
    2592              : 
    2593              :   /* If this DR needs alignment for correctness, we must ensure the target
    2594              :      alignment is a constant power-of-two multiple of the amount read per
    2595              :      vector iteration or force masking.  */
    2596      1351078 :   if (dr_safe_speculative_read_required (stmt_info)
    2597      1351078 :       && (*alignment_support_scheme == dr_aligned
    2598       102392 :           && !mat_gather_scatter_p (*memory_access_type)))
    2599              :     {
    2600              :       /* We can only peel for loops, of course.  */
    2601       102392 :       gcc_checking_assert (loop_vinfo);
    2602              : 
    2603       102392 :       poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
    2604       102392 :       poly_uint64 read_amount
    2605       102392 :         = vf * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
    2606       102392 :       if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
    2607       102392 :         read_amount *= group_size;
    2608              : 
    2609       102392 :       auto target_alignment
    2610       102392 :         = DR_TARGET_ALIGNMENT (STMT_VINFO_DR_INFO (stmt_info));
    2611       102392 :       if (!multiple_p (target_alignment, read_amount))
    2612              :         {
    2613        12716 :           if (dump_enabled_p ())
    2614              :             {
    2615           28 :               dump_printf_loc (MSG_NOTE, vect_location,
    2616              :                                "desired alignment not met, target was ");
    2617           28 :               dump_dec (MSG_NOTE, target_alignment);
    2618           28 :               dump_printf (MSG_NOTE, " previously, but read amount is ");
    2619           28 :               dump_dec (MSG_NOTE, read_amount);
    2620           28 :               dump_printf (MSG_NOTE, " at %G.\n", STMT_VINFO_STMT (stmt_info));
    2621              :             }
    2622        14905 :           return false;
    2623              :         }
    2624              : 
    2625              :       /* When using a group access the first element may be aligned but the
    2626              :          subsequent loads may not be.  For LOAD_LANES since the loads are based
    2627              :          on the first DR then all loads in the group are aligned.  For
    2628              :          non-LOAD_LANES this is not the case. In particular a load + blend when
    2629              :          there are gaps can have the non first loads issued unaligned, even
    2630              :          partially overlapping the memory of the first load in order to simplify
    2631              :          the blend.  This is what the x86_64 backend does for instance.  As
    2632              :          such only the first load in the group is aligned, the rest are not.
    2633              :          Because of this the permutes may break the alignment requirements that
    2634              :          have been set, and as such we should for now, reject them.  */
    2635        89676 :       if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
    2636              :         {
    2637         2189 :           if (dump_enabled_p ())
    2638           75 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2639              :                              "loads with load permutations not supported for "
    2640              :                              "speculative early break loads for %G",
    2641              :                              STMT_VINFO_STMT (stmt_info));
    2642         2189 :           return false;
    2643              :         }
    2644              : 
    2645              :       /* Reject vectorization if we know the read mount per vector iteration
    2646              :          exceeds the min page size.  */
    2647        87487 :       if (known_gt (read_amount, (unsigned) param_min_pagesize))
    2648              :         {
    2649            0 :           if (dump_enabled_p ())
    2650              :             {
    2651            0 :               dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2652              :                                "alignment required for correctness (");
    2653            0 :               dump_dec (MSG_MISSED_OPTIMIZATION, read_amount);
    2654            0 :               dump_printf (MSG_NOTE, ") may exceed page size.\n");
    2655              :             }
    2656            0 :           return false;
    2657              :         }
    2658              : 
    2659        87487 :       if (!vf.is_constant ())
    2660              :         {
    2661              :           /* For VLA modes, we need a runtime check to ensure any speculative
    2662              :              read amount does not exceed the page size.  Here we record the max
    2663              :              possible read amount for the check.  */
    2664              :           if (maybe_gt (read_amount,
    2665              :                         LOOP_VINFO_MAX_SPEC_READ_AMOUNT (loop_vinfo)))
    2666              :             LOOP_VINFO_MAX_SPEC_READ_AMOUNT (loop_vinfo) = read_amount;
    2667              : 
    2668              :           /* For VLA modes, we must use partial vectors.  */
    2669              :           LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
    2670              :         }
    2671              :     }
    2672              : 
    2673      1336173 :   if (*alignment_support_scheme == dr_unaligned_unsupported)
    2674              :     {
    2675        63997 :       if (dump_enabled_p ())
    2676          256 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2677              :                          "unsupported unaligned access\n");
    2678        63997 :       return false;
    2679              :     }
    2680              : 
    2681              :   /* FIXME: At the moment the cost model seems to underestimate the
    2682              :      cost of using elementwise accesses.  This check preserves the
    2683              :      traditional behavior until that can be fixed.  */
    2684      1272176 :   if (*memory_access_type == VMAT_ELEMENTWISE
    2685        14778 :       && !STMT_VINFO_STRIDED_P (first_stmt_info)
    2686      1286954 :       && !(STMT_VINFO_GROUPED_ACCESS (stmt_info)
    2687         9629 :            && single_element_p
    2688         8988 :            && !pow2p_hwi (group_size)))
    2689              :     {
    2690         9106 :       if (dump_enabled_p ())
    2691          362 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2692              :                          "not falling back to elementwise accesses\n");
    2693         9106 :       return false;
    2694              :     }
    2695              : 
    2696              :   /* For BB vectorization build up the vector from existing scalar defs.  */
    2697      1263070 :   if (!loop_vinfo && *memory_access_type == VMAT_ELEMENTWISE)
    2698              :     return false;
    2699              : 
    2700              :   /* Some loads need to explicitly permute the loaded data if there
    2701              :      is a load permutation.  Among those are:
    2702              :       - VMAT_ELEMENTWISE.
    2703              :       - VMAT_STRIDED_SLP.
    2704              :       - VMAT_GATHER_SCATTER:
    2705              :         - Strided gather (fallback for VMAT_STRIDED_SLP if #lanes == 1).
    2706              :         - Grouped strided gather (ditto but for #lanes > 1).
    2707              : 
    2708              :      For VMAT_ELEMENTWISE we can fold the load permutation into the
    2709              :      individual indices we access directly, eliding the permutation.
    2710              :      Strided gather only allows load permutations for the
    2711              :      single-element case.  */
    2712              : 
    2713      1263070 :   if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
    2714      1263070 :       && !(*memory_access_type == VMAT_ELEMENTWISE
    2715        45864 :            || (mat_gather_scatter_p (*memory_access_type)
    2716            0 :                && SLP_TREE_LANES (slp_node) == 1
    2717            0 :                && single_element_p)))
    2718              :     {
    2719        45864 :       if (!loop_vinfo)
    2720              :         {
    2721              :           /* In BB vectorization we may not actually use a loaded vector
    2722              :              accessing elements in excess of DR_GROUP_SIZE.  */
    2723        24185 :           stmt_vec_info group_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
    2724        24185 :           group_info = DR_GROUP_FIRST_ELEMENT (group_info);
    2725        24185 :           unsigned HOST_WIDE_INT nunits;
    2726        24185 :           unsigned j, k, maxk = 0;
    2727        85737 :           FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (slp_node), j, k)
    2728        61552 :             if (k > maxk)
    2729              :               maxk = k;
    2730        24185 :           tree vectype = SLP_TREE_VECTYPE (slp_node);
    2731        44031 :           if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits)
    2732        24185 :               || maxk >= (DR_GROUP_SIZE (group_info) & ~(nunits - 1)))
    2733              :             {
    2734         4339 :               if (dump_enabled_p ())
    2735           31 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2736              :                                  "BB vectorization with gaps at the end of "
    2737              :                                  "a load is not supported\n");
    2738         4339 :               return false;
    2739              :             }
    2740              :         }
    2741              : 
    2742        41525 :       if (!perm_ok)
    2743              :         {
    2744         1974 :           if (dump_enabled_p ())
    2745            8 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION,
    2746              :                              vect_location,
    2747              :                              "unsupported load permutation\n");
    2748         1974 :           return false;
    2749              :         }
    2750              : 
    2751        39551 :       *slp_perm = true;
    2752              :     }
    2753              : 
    2754              :   return true;
    2755              : }
    2756              : 
    2757              : /* Return true if boolean argument at MASK_INDEX is suitable for vectorizing
    2758              :    conditional operation STMT_INFO.  When returning true, store the mask
    2759              :    in *MASK_NODE, the type of its definition in *MASK_DT_OUT and the type of
    2760              :    the vectorized mask in *MASK_VECTYPE_OUT.  */
    2761              : 
    2762              : static bool
    2763        12691 : vect_check_scalar_mask (vec_info *vinfo,
    2764              :                         slp_tree slp_node, unsigned mask_index,
    2765              :                         slp_tree *mask_node,
    2766              :                         vect_def_type *mask_dt_out, tree *mask_vectype_out)
    2767              : {
    2768        12691 :   enum vect_def_type mask_dt;
    2769        12691 :   tree mask_vectype;
    2770        12691 :   slp_tree mask_node_1;
    2771        12691 :   tree mask_;
    2772        12691 :   if (!vect_is_simple_use (vinfo, slp_node, mask_index,
    2773              :                            &mask_, &mask_node_1, &mask_dt, &mask_vectype))
    2774              :     {
    2775            0 :       if (dump_enabled_p ())
    2776            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2777              :                          "mask use not simple.\n");
    2778            0 :       return false;
    2779              :     }
    2780              : 
    2781        12691 :   if ((mask_dt == vect_constant_def || mask_dt == vect_external_def)
    2782        12691 :       && !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (mask_)))
    2783              :     {
    2784            0 :       if (dump_enabled_p ())
    2785            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2786              :                          "mask argument is not a boolean.\n");
    2787            0 :       return false;
    2788              :     }
    2789              : 
    2790        12691 :   tree vectype = SLP_TREE_VECTYPE (slp_node);
    2791        12691 :   if (!mask_vectype)
    2792           19 :     mask_vectype = get_mask_type_for_scalar_type (vinfo, TREE_TYPE (vectype),
    2793              :                                                   mask_node_1);
    2794              : 
    2795        12691 :   if (!mask_vectype || !VECTOR_BOOLEAN_TYPE_P (mask_vectype))
    2796              :     {
    2797            0 :       if (dump_enabled_p ())
    2798            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2799              :                          "could not find an appropriate vector mask type.\n");
    2800            0 :       return false;
    2801              :     }
    2802              : 
    2803        12691 :   if (maybe_ne (TYPE_VECTOR_SUBPARTS (mask_vectype),
    2804        25382 :                 TYPE_VECTOR_SUBPARTS (vectype)))
    2805              :     {
    2806            0 :       if (dump_enabled_p ())
    2807            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2808              :                          "vector mask type %T"
    2809              :                          " does not match vector data type %T.\n",
    2810              :                          mask_vectype, vectype);
    2811              : 
    2812            0 :       return false;
    2813              :     }
    2814              : 
    2815        12691 :   *mask_dt_out = mask_dt;
    2816        12691 :   *mask_vectype_out = mask_vectype;
    2817        12691 :   *mask_node = mask_node_1;
    2818        12691 :   return true;
    2819              : }
    2820              : 
    2821              : 
    2822              : /* Return true if stored value is suitable for vectorizing store
    2823              :    statement STMT_INFO.  When returning true, store the scalar stored
    2824              :    in *RHS and *RHS_NODE, the type of the definition in *RHS_DT_OUT,
    2825              :    the type of the vectorized store value in
    2826              :    *RHS_VECTYPE_OUT and the type of the store in *VLS_TYPE_OUT.  */
    2827              : 
    2828              : static bool
    2829      1364983 : vect_check_store_rhs (vec_info *vinfo, stmt_vec_info stmt_info,
    2830              :                       slp_tree slp_node, slp_tree *rhs_node,
    2831              :                       vect_def_type *rhs_dt_out, tree *rhs_vectype_out,
    2832              :                       vec_load_store_type *vls_type_out)
    2833              : {
    2834      1364983 :   int op_no = 0;
    2835      1364983 :   if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
    2836              :     {
    2837         1901 :       if (gimple_call_internal_p (call)
    2838         1901 :           && internal_store_fn_p (gimple_call_internal_fn (call)))
    2839         1901 :         op_no = internal_fn_stored_value_index (gimple_call_internal_fn (call));
    2840              :     }
    2841      1364983 :   op_no = vect_slp_child_index_for_operand (stmt_info, op_no);
    2842              : 
    2843      1364983 :   enum vect_def_type rhs_dt;
    2844      1364983 :   tree rhs_vectype;
    2845      1364983 :   tree rhs;
    2846      1364983 :   if (!vect_is_simple_use (vinfo, slp_node, op_no,
    2847              :                            &rhs, rhs_node, &rhs_dt, &rhs_vectype))
    2848              :     {
    2849            0 :       if (dump_enabled_p ())
    2850            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2851              :                          "use not simple.\n");
    2852            0 :       return false;
    2853              :     }
    2854              : 
    2855              :   /* In the case this is a store from a constant make sure
    2856              :      native_encode_expr can handle it.  */
    2857      1364983 :   if (rhs_dt == vect_constant_def
    2858      1364983 :       && CONSTANT_CLASS_P (rhs) && native_encode_expr (rhs, NULL, 64) == 0)
    2859              :     {
    2860            0 :       if (dump_enabled_p ())
    2861            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2862              :                          "cannot encode constant as a byte sequence.\n");
    2863            0 :       return false;
    2864              :     }
    2865              : 
    2866      1364983 :   tree vectype = SLP_TREE_VECTYPE (slp_node);
    2867      1364983 :   if (rhs_vectype && !useless_type_conversion_p (vectype, rhs_vectype))
    2868              :     {
    2869           24 :       if (dump_enabled_p ())
    2870           24 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    2871              :                          "incompatible vector types.\n");
    2872           24 :       return false;
    2873              :     }
    2874              : 
    2875      1364959 :   *rhs_dt_out = rhs_dt;
    2876      1364959 :   *rhs_vectype_out = rhs_vectype;
    2877      1364959 :   if (rhs_dt == vect_constant_def || rhs_dt == vect_external_def)
    2878      1005674 :     *vls_type_out = VLS_STORE_INVARIANT;
    2879              :   else
    2880       359285 :     *vls_type_out = VLS_STORE;
    2881              :   return true;
    2882              : }
    2883              : 
    2884              : /* Build an all-ones vector mask of type MASKTYPE while vectorizing STMT_INFO.
    2885              :    Note that we support masks with floating-point type, in which case the
    2886              :    floats are interpreted as a bitmask.  */
    2887              : 
    2888              : static tree
    2889          170 : vect_build_all_ones_mask (vec_info *vinfo,
    2890              :                           stmt_vec_info stmt_info, tree masktype)
    2891              : {
    2892          170 :   if (TREE_CODE (masktype) == INTEGER_TYPE)
    2893           98 :     return build_int_cst (masktype, -1);
    2894           72 :   else if (VECTOR_BOOLEAN_TYPE_P (masktype)
    2895          144 :            || TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
    2896              :     {
    2897           19 :       tree mask = build_int_cst (TREE_TYPE (masktype), -1);
    2898           19 :       mask = build_vector_from_val (masktype, mask);
    2899           19 :       return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
    2900              :     }
    2901           53 :   else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
    2902              :     {
    2903              :       REAL_VALUE_TYPE r;
    2904              :       long tmp[6];
    2905          371 :       for (int j = 0; j < 6; ++j)
    2906          318 :         tmp[j] = -1;
    2907           53 :       real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
    2908           53 :       tree mask = build_real (TREE_TYPE (masktype), r);
    2909           53 :       mask = build_vector_from_val (masktype, mask);
    2910           53 :       return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
    2911              :     }
    2912            0 :   gcc_unreachable ();
    2913              : }
    2914              : 
    2915              : /* Build an all-zero merge value of type VECTYPE while vectorizing
    2916              :    STMT_INFO as a gather load.  */
    2917              : 
    2918              : static tree
    2919          158 : vect_build_zero_merge_argument (vec_info *vinfo,
    2920              :                                 stmt_vec_info stmt_info, tree vectype)
    2921              : {
    2922          158 :   tree merge;
    2923          158 :   if (TREE_CODE (TREE_TYPE (vectype)) == INTEGER_TYPE)
    2924           49 :     merge = build_int_cst (TREE_TYPE (vectype), 0);
    2925          109 :   else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (vectype)))
    2926              :     {
    2927              :       REAL_VALUE_TYPE r;
    2928              :       long tmp[6];
    2929          763 :       for (int j = 0; j < 6; ++j)
    2930          654 :         tmp[j] = 0;
    2931          109 :       real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (vectype)));
    2932          109 :       merge = build_real (TREE_TYPE (vectype), r);
    2933              :     }
    2934              :   else
    2935            0 :     gcc_unreachable ();
    2936          158 :   merge = build_vector_from_val (vectype, merge);
    2937          158 :   return vect_init_vector (vinfo, stmt_info, merge, vectype, NULL);
    2938              : }
    2939              : 
    2940              : /* Return the corresponding else value for an else value constant
    2941              :    ELSVAL with type TYPE.  */
    2942              : 
    2943              : tree
    2944         1944 : vect_get_mask_load_else (int elsval, tree type)
    2945              : {
    2946         1944 :   tree els;
    2947         1944 :   if (elsval == MASK_LOAD_ELSE_UNDEFINED)
    2948              :     {
    2949            0 :       tree tmp = create_tmp_var (type);
    2950              :       /* No need to warn about anything.  */
    2951            0 :       TREE_NO_WARNING (tmp) = 1;
    2952            0 :       els = get_or_create_ssa_default_def (cfun, tmp);
    2953              :     }
    2954         1944 :   else if (elsval == MASK_LOAD_ELSE_M1)
    2955            0 :     els = build_minus_one_cst (type);
    2956         1944 :   else if (elsval == MASK_LOAD_ELSE_ZERO)
    2957         1944 :     els = build_zero_cst (type);
    2958              :   else
    2959            0 :     gcc_unreachable ();
    2960              : 
    2961         1944 :   return els;
    2962              : }
    2963              : 
    2964              : /* Build a gather load call while vectorizing STMT_INFO.  Insert new
    2965              :    instructions before GSI and add them to VEC_STMT.  GS_INFO describes
    2966              :    the gather load operation.  If the load is conditional, MASK is the
    2967              :    vectorized condition, otherwise MASK is null.  PTR is the base
    2968              :    pointer and OFFSET is the vectorized offset.  */
    2969              : 
    2970              : static gimple *
    2971          346 : vect_build_one_gather_load_call (vec_info *vinfo, stmt_vec_info stmt_info,
    2972              :                                  slp_tree slp_node, tree vectype,
    2973              :                                  gimple_stmt_iterator *gsi, tree decl,
    2974              :                                  tree ptr, tree offset, tree mask)
    2975              : {
    2976          346 :   tree arglist = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2977          346 :   tree rettype = TREE_TYPE (TREE_TYPE (decl));
    2978          346 :   tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
    2979          346 :   /* ptrtype */ arglist = TREE_CHAIN (arglist);
    2980          346 :   tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
    2981          346 :   tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
    2982          346 :   tree scaletype = TREE_VALUE (arglist);
    2983          346 :   tree var;
    2984          346 :   gcc_checking_assert (types_compatible_p (srctype, rettype)
    2985              :                        && (!mask
    2986              :                            || TREE_CODE (masktype) == INTEGER_TYPE
    2987              :                            || types_compatible_p (srctype, masktype)));
    2988              : 
    2989          346 :   tree op = offset;
    2990          346 :   if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
    2991              :     {
    2992          100 :       gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
    2993              :                             TYPE_VECTOR_SUBPARTS (idxtype)));
    2994          100 :       var = vect_get_new_ssa_name (idxtype, vect_simple_var);
    2995          100 :       op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
    2996          100 :       gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
    2997          100 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    2998          100 :       op = var;
    2999              :     }
    3000              : 
    3001          346 :   tree src_op = NULL_TREE;
    3002          346 :   tree mask_op = NULL_TREE;
    3003          346 :   if (mask)
    3004              :     {
    3005          188 :       if (!useless_type_conversion_p (masktype, TREE_TYPE (mask)))
    3006              :         {
    3007          188 :           tree utype, optype = TREE_TYPE (mask);
    3008          188 :           if (VECTOR_TYPE_P (masktype)
    3009          188 :               || TYPE_MODE (masktype) == TYPE_MODE (optype))
    3010              :             utype = masktype;
    3011              :           else
    3012            6 :             utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
    3013          188 :           var = vect_get_new_ssa_name (utype, vect_scalar_var);
    3014          188 :           tree mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask);
    3015          188 :           gassign *new_stmt
    3016          188 :               = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
    3017          188 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3018          188 :           mask_arg = var;
    3019          188 :           if (!useless_type_conversion_p (masktype, utype))
    3020              :             {
    3021            6 :               gcc_assert (TYPE_PRECISION (utype)
    3022              :                           <= TYPE_PRECISION (masktype));
    3023            6 :               var = vect_get_new_ssa_name (masktype, vect_scalar_var);
    3024            6 :               new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
    3025            6 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3026            6 :               mask_arg = var;
    3027              :             }
    3028          188 :           src_op = build_zero_cst (srctype);
    3029          188 :           mask_op = mask_arg;
    3030              :         }
    3031              :       else
    3032              :         {
    3033              :           src_op = mask;
    3034              :           mask_op = mask;
    3035              :         }
    3036              :     }
    3037              :   else
    3038              :     {
    3039          158 :       src_op = vect_build_zero_merge_argument (vinfo, stmt_info, rettype);
    3040          158 :       mask_op = vect_build_all_ones_mask (vinfo, stmt_info, masktype);
    3041              :     }
    3042              : 
    3043          346 :   tree scale = build_int_cst (scaletype, SLP_TREE_GS_SCALE (slp_node));
    3044          346 :   gimple *new_stmt = gimple_build_call (decl, 5, src_op, ptr, op,
    3045              :                                         mask_op, scale);
    3046              : 
    3047          346 :   if (!useless_type_conversion_p (vectype, rettype))
    3048              :     {
    3049           49 :       gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
    3050              :                             TYPE_VECTOR_SUBPARTS (rettype)));
    3051           49 :       op = vect_get_new_ssa_name (rettype, vect_simple_var);
    3052           49 :       gimple_call_set_lhs (new_stmt, op);
    3053           49 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3054           49 :       op = build1 (VIEW_CONVERT_EXPR, vectype, op);
    3055           49 :       new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR, op);
    3056              :     }
    3057              : 
    3058          346 :   return new_stmt;
    3059              : }
    3060              : 
    3061              : /* Build a scatter store call while vectorizing STMT_INFO.  Insert new
    3062              :    instructions before GSI.  GS_INFO describes the scatter store operation.
    3063              :    PTR is the base pointer, OFFSET the vectorized offsets and OPRND the
    3064              :    vectorized data to store.
    3065              :    If the store is conditional, MASK is the vectorized condition, otherwise
    3066              :    MASK is null.  */
    3067              : 
    3068              : static gimple *
    3069          161 : vect_build_one_scatter_store_call (vec_info *vinfo, stmt_vec_info stmt_info,
    3070              :                                    slp_tree slp_node,
    3071              :                                    gimple_stmt_iterator *gsi,
    3072              :                                    tree decl,
    3073              :                                    tree ptr, tree offset, tree oprnd, tree mask)
    3074              : {
    3075          161 :   tree rettype = TREE_TYPE (TREE_TYPE (decl));
    3076          161 :   tree arglist = TYPE_ARG_TYPES (TREE_TYPE (decl));
    3077          161 :   /* tree ptrtype = TREE_VALUE (arglist); */ arglist = TREE_CHAIN (arglist);
    3078          161 :   tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
    3079          161 :   tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
    3080          161 :   tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
    3081          161 :   tree scaletype = TREE_VALUE (arglist);
    3082          161 :   gcc_checking_assert (TREE_CODE (masktype) == INTEGER_TYPE
    3083              :                        && TREE_CODE (rettype) == VOID_TYPE);
    3084              : 
    3085          161 :   tree mask_arg = NULL_TREE;
    3086          161 :   if (mask)
    3087              :     {
    3088          110 :       mask_arg = mask;
    3089          110 :       tree optype = TREE_TYPE (mask_arg);
    3090          110 :       tree utype;
    3091          110 :       if (TYPE_MODE (masktype) == TYPE_MODE (optype))
    3092              :         utype = masktype;
    3093              :       else
    3094            8 :         utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
    3095          110 :       tree var = vect_get_new_ssa_name (utype, vect_scalar_var);
    3096          110 :       mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask_arg);
    3097          110 :       gassign *new_stmt
    3098          110 :         = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
    3099          110 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3100          110 :       mask_arg = var;
    3101          110 :       if (!useless_type_conversion_p (masktype, utype))
    3102              :         {
    3103            8 :           gcc_assert (TYPE_PRECISION (utype) <= TYPE_PRECISION (masktype));
    3104            8 :           tree var = vect_get_new_ssa_name (masktype, vect_scalar_var);
    3105            8 :           new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
    3106            8 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3107            8 :           mask_arg = var;
    3108              :         }
    3109              :     }
    3110              :   else
    3111              :     {
    3112           51 :       mask_arg = build_int_cst (masktype, -1);
    3113           51 :       mask_arg = vect_init_vector (vinfo, stmt_info, mask_arg, masktype, NULL);
    3114              :     }
    3115              : 
    3116          161 :   tree src = oprnd;
    3117          161 :   if (!useless_type_conversion_p (srctype, TREE_TYPE (src)))
    3118              :     {
    3119            0 :       gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (src)),
    3120              :                             TYPE_VECTOR_SUBPARTS (srctype)));
    3121            0 :       tree var = vect_get_new_ssa_name (srctype, vect_simple_var);
    3122            0 :       src = build1 (VIEW_CONVERT_EXPR, srctype, src);
    3123            0 :       gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, src);
    3124            0 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3125            0 :       src = var;
    3126              :     }
    3127              : 
    3128          161 :   tree op = offset;
    3129          161 :   if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
    3130              :     {
    3131           16 :       gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
    3132              :                             TYPE_VECTOR_SUBPARTS (idxtype)));
    3133           16 :       tree var = vect_get_new_ssa_name (idxtype, vect_simple_var);
    3134           16 :       op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
    3135           16 :       gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
    3136           16 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3137           16 :       op = var;
    3138              :     }
    3139              : 
    3140          161 :   tree scale = build_int_cst (scaletype, SLP_TREE_GS_SCALE (slp_node));
    3141          161 :   gcall *new_stmt
    3142          161 :     = gimple_build_call (decl, 5, ptr, mask_arg, op, src, scale);
    3143          161 :   return new_stmt;
    3144              : }
    3145              : 
    3146              : /* Prepare the base and offset in GS_INFO for vectorization.
    3147              :    Set *DATAREF_PTR to the loop-invariant base address and *VEC_OFFSET
    3148              :    to the vectorized offset argument for the first copy of STMT_INFO.
    3149              :    STMT_INFO is the statement described by GS_INFO and LOOP is the
    3150              :    containing loop.  */
    3151              : 
    3152              : static void
    3153         1217 : vect_get_gather_scatter_ops (class loop *loop, slp_tree slp_node,
    3154              :                              tree *dataref_ptr, vec<tree> *vec_offset)
    3155              : {
    3156         1217 :   gimple_seq stmts = NULL;
    3157         1217 :   *dataref_ptr = force_gimple_operand (SLP_TREE_GS_BASE (slp_node),
    3158              :                                        &stmts, true, NULL_TREE);
    3159         1217 :   if (stmts != NULL)
    3160              :     {
    3161          984 :       basic_block new_bb;
    3162          984 :       edge pe = loop_preheader_edge (loop);
    3163          984 :       new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
    3164          984 :       gcc_assert (!new_bb);
    3165              :     }
    3166         1217 :   vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_offset);
    3167         1217 : }
    3168              : 
    3169              : /* Prepare to implement a grouped or strided load or store using
    3170              :    the gather load or scatter store operation described by GS_INFO.
    3171              :    STMT_INFO is the load or store statement.
    3172              : 
    3173              :    Set *DATAREF_BUMP to the amount that should be added to the base
    3174              :    address after each copy of the vectorized statement.  Set *VEC_OFFSET
    3175              :    to an invariant offset vector in which element I has the value
    3176              :    I * DR_STEP / SCALE.  */
    3177              : 
    3178              : static void
    3179            0 : vect_get_strided_load_store_ops (stmt_vec_info stmt_info, slp_tree node,
    3180              :                                  tree vectype, tree offset_vectype,
    3181              :                                  loop_vec_info loop_vinfo,
    3182              :                                  gimple_stmt_iterator *gsi,
    3183              :                                  tree *dataref_bump, tree *vec_offset,
    3184              :                                  vec_loop_lens *loop_lens)
    3185              : {
    3186            0 :   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
    3187              : 
    3188            0 :   if (LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
    3189              :     {
    3190              :       /* _31 = .SELECT_VL (ivtmp_29, POLY_INT_CST [4, 4]);
    3191              :          ivtmp_8 = _31 * 16 (step in bytes);
    3192              :          .MASK_LEN_SCATTER_STORE (vectp_a.9_7, ... );
    3193              :          vectp_a.9_26 = vectp_a.9_7 + ivtmp_8;  */
    3194            0 :       tree loop_len
    3195            0 :         = vect_get_loop_len (loop_vinfo, gsi, loop_lens, 1, vectype, 0, 0, true);
    3196            0 :       tree tmp
    3197            0 :         = fold_build2 (MULT_EXPR, sizetype,
    3198              :                        fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
    3199              :                        loop_len);
    3200            0 :       *dataref_bump = force_gimple_operand_gsi (gsi, tmp, true, NULL_TREE, true,
    3201              :                                                 GSI_SAME_STMT);
    3202              :     }
    3203              :   else
    3204              :     {
    3205            0 :       tree bump
    3206            0 :         = size_binop (MULT_EXPR,
    3207              :                       fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
    3208              :                       size_int (TYPE_VECTOR_SUBPARTS (vectype)));
    3209            0 :       *dataref_bump = cse_and_gimplify_to_preheader (loop_vinfo, bump);
    3210              :     }
    3211              : 
    3212            0 :   internal_fn ifn
    3213            0 :     = DR_IS_READ (dr) ? IFN_MASK_LEN_STRIDED_LOAD : IFN_MASK_LEN_STRIDED_STORE;
    3214            0 :   if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
    3215              :     {
    3216            0 :       *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo,
    3217              :                                                    unshare_expr (DR_STEP (dr)));
    3218            0 :       return;
    3219              :     }
    3220              : 
    3221              :   /* The offset given in GS_INFO can have pointer type, so use the element
    3222              :      type of the vector instead.  */
    3223            0 :   tree offset_type = TREE_TYPE (offset_vectype);
    3224              : 
    3225              :   /* Calculate X = DR_STEP / SCALE and convert it to the appropriate type.  */
    3226            0 :   tree step = size_binop (EXACT_DIV_EXPR, unshare_expr (DR_STEP (dr)),
    3227              :                           ssize_int (SLP_TREE_GS_SCALE (node)));
    3228            0 :   step = fold_convert (offset_type, step);
    3229              : 
    3230              :   /* Create {0, X, X*2, X*3, ...}.  */
    3231            0 :   tree offset = fold_build2 (VEC_SERIES_EXPR, offset_vectype,
    3232              :                              build_zero_cst (offset_type), step);
    3233            0 :   *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo, offset);
    3234              : }
    3235              : 
    3236              : /* Prepare the pointer IVs which needs to be updated by a variable amount.
    3237              :    Such variable amount is the outcome of .SELECT_VL. In this case, we can
    3238              :    allow each iteration process the flexible number of elements as long as
    3239              :    the number <= vf elments.
    3240              : 
    3241              :    Return data reference according to SELECT_VL.
    3242              :    If new statements are needed, insert them before GSI.  */
    3243              : 
    3244              : static tree
    3245            0 : vect_get_loop_variant_data_ptr_increment (
    3246              :   vec_info *vinfo, tree aggr_type, gimple_stmt_iterator *gsi,
    3247              :   vec_loop_lens *loop_lens, dr_vec_info *dr_info,
    3248              :   vect_memory_access_type memory_access_type)
    3249              : {
    3250            0 :   loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
    3251            0 :   tree step = vect_dr_behavior (vinfo, dr_info)->step;
    3252              : 
    3253              :   /* gather/scatter never reach here.  */
    3254            0 :   gcc_assert (!mat_gather_scatter_p (memory_access_type));
    3255              : 
    3256              :   /* When we support SELECT_VL pattern, we dynamic adjust
    3257              :      the memory address by .SELECT_VL result.
    3258              : 
    3259              :      The result of .SELECT_VL is the number of elements to
    3260              :      be processed of each iteration. So the memory address
    3261              :      adjustment operation should be:
    3262              : 
    3263              :      addr = addr + .SELECT_VL (ARG..) * step;
    3264              :   */
    3265            0 :   tree loop_len
    3266            0 :     = vect_get_loop_len (loop_vinfo, gsi, loop_lens, 1, aggr_type, 0, 0, true);
    3267            0 :   tree len_type = TREE_TYPE (loop_len);
    3268              :   /* Since the outcome of .SELECT_VL is element size, we should adjust
    3269              :      it into bytesize so that it can be used in address pointer variable
    3270              :      amount IVs adjustment.  */
    3271            0 :   tree tmp = fold_build2 (MULT_EXPR, len_type, loop_len,
    3272              :                           wide_int_to_tree (len_type, wi::to_widest (step)));
    3273            0 :   tree bump = make_temp_ssa_name (len_type, NULL, "ivtmp");
    3274            0 :   gassign *assign = gimple_build_assign (bump, tmp);
    3275            0 :   gsi_insert_before (gsi, assign, GSI_SAME_STMT);
    3276            0 :   return bump;
    3277              : }
    3278              : 
    3279              : /* Return the amount that should be added to a vector pointer to move
    3280              :    to the next or previous copy of AGGR_TYPE.  DR_INFO is the data reference
    3281              :    being vectorized and MEMORY_ACCESS_TYPE describes the type of
    3282              :    vectorization.  */
    3283              : 
    3284              : static tree
    3285       704507 : vect_get_data_ptr_increment (vec_info *vinfo, gimple_stmt_iterator *gsi,
    3286              :                              dr_vec_info *dr_info, tree aggr_type,
    3287              :                              vect_memory_access_type memory_access_type,
    3288              :                              vec_loop_lens *loop_lens)
    3289              : {
    3290       704507 :   if (memory_access_type == VMAT_INVARIANT)
    3291            0 :     return size_zero_node;
    3292              : 
    3293       704507 :   loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
    3294       134427 :   if (loop_vinfo && LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
    3295            0 :     return vect_get_loop_variant_data_ptr_increment (vinfo, aggr_type, gsi,
    3296              :                                                      loop_lens, dr_info,
    3297            0 :                                                      memory_access_type);
    3298              : 
    3299       704507 :   tree iv_step = TYPE_SIZE_UNIT (aggr_type);
    3300       704507 :   tree step = vect_dr_behavior (vinfo, dr_info)->step;
    3301       704507 :   if (tree_int_cst_sgn (step) == -1)
    3302         2841 :     iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
    3303              :   return iv_step;
    3304              : }
    3305              : 
    3306              : /* Check and perform vectorization of BUILT_IN_BSWAP{16,32,64,128}.  */
    3307              : 
    3308              : static bool
    3309          206 : vectorizable_bswap (vec_info *vinfo,
    3310              :                     stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
    3311              :                     slp_tree slp_node,
    3312              :                     slp_tree *slp_op,
    3313              :                     tree vectype_in, stmt_vector_for_cost *cost_vec)
    3314              : {
    3315          206 :   tree op, vectype;
    3316          206 :   gcall *stmt = as_a <gcall *> (stmt_info->stmt);
    3317              : 
    3318          206 :   op = gimple_call_arg (stmt, 0);
    3319          206 :   vectype = SLP_TREE_VECTYPE (slp_node);
    3320          206 :   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
    3321              : 
    3322          206 :   if (TYPE_SIZE (vectype_in) != TYPE_SIZE (vectype))
    3323              :     {
    3324            0 :       if (dump_enabled_p ())
    3325            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3326              :                          "mismatched vector sizes %T and %T\n",
    3327              :                          vectype_in, vectype);
    3328            0 :       return false;
    3329              :     }
    3330              : 
    3331          206 :   tree char_vectype = get_same_sized_vectype (char_type_node, vectype_in);
    3332          206 :   if (! char_vectype)
    3333              :     return false;
    3334              : 
    3335          206 :   poly_uint64 num_bytes = TYPE_VECTOR_SUBPARTS (char_vectype);
    3336          206 :   unsigned word_bytes;
    3337          206 :   if (!constant_multiple_p (num_bytes, nunits, &word_bytes))
    3338              :     return false;
    3339              : 
    3340              :   /* The encoding uses one stepped pattern for each byte in the word.  */
    3341          206 :   vec_perm_builder elts (num_bytes, word_bytes, 3);
    3342          824 :   for (unsigned i = 0; i < 3; ++i)
    3343         3318 :     for (unsigned j = 0; j < word_bytes; ++j)
    3344         2700 :       elts.quick_push ((i + 1) * word_bytes - j - 1);
    3345              : 
    3346          206 :   vec_perm_indices indices (elts, 1, num_bytes);
    3347          206 :   machine_mode vmode = TYPE_MODE (char_vectype);
    3348          206 :   if (!can_vec_perm_const_p (vmode, vmode, indices))
    3349              :     return false;
    3350              : 
    3351          152 :   if (cost_vec)
    3352              :     {
    3353          140 :       if (!vect_maybe_update_slp_op_vectype (slp_op[0], vectype_in))
    3354              :         {
    3355            0 :           if (dump_enabled_p ())
    3356            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3357              :                              "incompatible vector types for invariants\n");
    3358            0 :           return false;
    3359              :         }
    3360              : 
    3361          140 :       SLP_TREE_TYPE (slp_node) = call_vec_info_type;
    3362          140 :       DUMP_VECT_SCOPE ("vectorizable_bswap");
    3363          140 :       record_stmt_cost (cost_vec,
    3364              :                         1, vector_stmt, slp_node, 0, vect_prologue);
    3365          140 :       record_stmt_cost (cost_vec,
    3366          140 :                         vect_get_num_copies (vinfo, slp_node),
    3367              :                         vec_perm, slp_node, 0, vect_body);
    3368          140 :       return true;
    3369              :     }
    3370              : 
    3371           12 :   tree bswap_vconst = vec_perm_indices_to_tree (char_vectype, indices);
    3372              : 
    3373              :   /* Transform.  */
    3374           12 :   vec<tree> vec_oprnds = vNULL;
    3375           12 :   vect_get_vec_defs (vinfo, slp_node, op, &vec_oprnds);
    3376              :   /* Arguments are ready. create the new vector stmt.  */
    3377           12 :   unsigned i;
    3378           12 :   tree vop;
    3379           24 :   FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
    3380              :     {
    3381           12 :       gimple *new_stmt;
    3382           12 :       tree tem = make_ssa_name (char_vectype);
    3383           12 :       new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
    3384              :                                                    char_vectype, vop));
    3385           12 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3386           12 :       tree tem2 = make_ssa_name (char_vectype);
    3387           12 :       new_stmt = gimple_build_assign (tem2, VEC_PERM_EXPR,
    3388              :                                       tem, tem, bswap_vconst);
    3389           12 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3390           12 :       tem = make_ssa_name (vectype);
    3391           12 :       new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
    3392              :                                                    vectype, tem2));
    3393           12 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3394           12 :       slp_node->push_vec_def (new_stmt);
    3395              :     }
    3396              : 
    3397           12 :   vec_oprnds.release ();
    3398           12 :   return true;
    3399          206 : }
    3400              : 
    3401              : /* Return true if vector types VECTYPE_IN and VECTYPE_OUT have
    3402              :    integer elements and if we can narrow VECTYPE_IN to VECTYPE_OUT
    3403              :    in a single step.  On success, store the binary pack code in
    3404              :    *CONVERT_CODE.  */
    3405              : 
    3406              : static bool
    3407          180 : simple_integer_narrowing (tree vectype_out, tree vectype_in,
    3408              :                           code_helper *convert_code)
    3409              : {
    3410          360 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype_out))
    3411          360 :       || !INTEGRAL_TYPE_P (TREE_TYPE (vectype_in)))
    3412              :     return false;
    3413              : 
    3414           70 :   code_helper code;
    3415           70 :   int multi_step_cvt = 0;
    3416           70 :   auto_vec <tree, 8> interm_types;
    3417          101 :   if (!supportable_narrowing_operation (NOP_EXPR, vectype_out, vectype_in,
    3418              :                                         &code, &multi_step_cvt, &interm_types)
    3419           70 :       || multi_step_cvt)
    3420           31 :     return false;
    3421              : 
    3422           39 :   *convert_code = code;
    3423           39 :   return true;
    3424           70 : }
    3425              : 
    3426              : /* Function vectorizable_call.
    3427              : 
    3428              :    Check if STMT_INFO performs a function call that can be vectorized.
    3429              :    If COST_VEC is passed, calculate costs but don't change anything,
    3430              :    otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
    3431              :    it, and insert it at GSI.
    3432              :    Return true if STMT_INFO is vectorizable in this way.  */
    3433              : 
    3434              : static bool
    3435      2700884 : vectorizable_call (vec_info *vinfo,
    3436              :                    stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
    3437              :                    slp_tree slp_node,
    3438              :                    stmt_vector_for_cost *cost_vec)
    3439              : {
    3440      2700884 :   gcall *stmt;
    3441      2700884 :   tree vec_dest;
    3442      2700884 :   tree scalar_dest;
    3443      2700884 :   tree op;
    3444      2700884 :   tree vec_oprnd0 = NULL_TREE;
    3445      2700884 :   tree vectype_out, vectype_in;
    3446      2700884 :   poly_uint64 nunits_in;
    3447      2700884 :   poly_uint64 nunits_out;
    3448      2700884 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    3449      2700884 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
    3450      2700884 :   tree fndecl, new_temp, rhs_type;
    3451      2700884 :   enum vect_def_type dt[5]
    3452              :     = { vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type,
    3453              :         vect_unknown_def_type, vect_unknown_def_type };
    3454      2700884 :   tree vectypes[ARRAY_SIZE (dt)] = {};
    3455      2700884 :   slp_tree slp_op[ARRAY_SIZE (dt)] = {};
    3456      2700884 :   auto_vec<tree, 8> vargs;
    3457      2700884 :   enum { NARROW, NONE, WIDEN } modifier;
    3458      2700884 :   size_t i, nargs;
    3459      2700884 :   tree clz_ctz_arg1 = NULL_TREE;
    3460              : 
    3461      2700884 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
    3462              :     return false;
    3463              : 
    3464      2700884 :   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
    3465       234770 :       && cost_vec)
    3466              :     return false;
    3467              : 
    3468              :   /* Is STMT_INFO a vectorizable call?   */
    3469      2714642 :   stmt = dyn_cast <gcall *> (stmt_info->stmt);
    3470        25044 :   if (!stmt)
    3471              :     return false;
    3472              : 
    3473        25044 :   if (gimple_call_internal_p (stmt)
    3474        25044 :       && (internal_load_fn_p (gimple_call_internal_fn (stmt))
    3475        16533 :           || internal_store_fn_p (gimple_call_internal_fn (stmt))))
    3476              :     /* Handled by vectorizable_load and vectorizable_store.  */
    3477         3816 :     return false;
    3478              : 
    3479        21228 :   if (gimple_call_lhs (stmt) == NULL_TREE
    3480        21228 :       || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
    3481              :     return false;
    3482              : 
    3483        21222 :   gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
    3484              : 
    3485        21222 :   vectype_out = SLP_TREE_VECTYPE (slp_node);
    3486              : 
    3487              :   /* Process function arguments.  */
    3488        21222 :   rhs_type = NULL_TREE;
    3489        21222 :   vectype_in = NULL_TREE;
    3490        21222 :   nargs = gimple_call_num_args (stmt);
    3491              : 
    3492              :   /* Bail out if the function has more than four arguments, we do not have
    3493              :      interesting builtin functions to vectorize with more than two arguments
    3494              :      except for fma (cond_fma has more).  No arguments is also not good.  */
    3495        21222 :   if (nargs == 0 || nargs > 5)
    3496              :     return false;
    3497              : 
    3498              :   /* Ignore the arguments of IFN_GOMP_SIMD_LANE, they are magic.  */
    3499        21142 :   combined_fn cfn = gimple_call_combined_fn (stmt);
    3500        21142 :   if (cfn == CFN_GOMP_SIMD_LANE)
    3501              :     {
    3502         3207 :       nargs = 0;
    3503         3207 :       rhs_type = unsigned_type_node;
    3504              :     }
    3505              :   /* Similarly pretend IFN_CLZ and IFN_CTZ only has one argument, the second
    3506              :      argument just says whether it is well-defined at zero or not and what
    3507              :      value should be returned for it.  */
    3508        21142 :   if ((cfn == CFN_CLZ || cfn == CFN_CTZ) && nargs == 2)
    3509              :     {
    3510          168 :       nargs = 1;
    3511          168 :       clz_ctz_arg1 = gimple_call_arg (stmt, 1);
    3512              :     }
    3513              : 
    3514        21142 :   int mask_opno = -1;
    3515        21142 :   if (internal_fn_p (cfn))
    3516              :     {
    3517              :       /* We can only handle direct internal masked calls here,
    3518              :          vectorizable_simd_clone_call is for the rest.  */
    3519        18088 :       if (cfn == CFN_MASK_CALL)
    3520              :         return false;
    3521        17934 :       mask_opno = internal_fn_mask_index (as_internal_fn (cfn));
    3522              :     }
    3523              : 
    3524        66153 :   for (i = 0; i < nargs; i++)
    3525              :     {
    3526        46451 :       if ((int) i == mask_opno)
    3527              :         {
    3528         7694 :           if (!vect_check_scalar_mask (vinfo, slp_node, mask_opno,
    3529              :                                        &slp_op[i], &dt[i], &vectypes[i]))
    3530              :             return false;
    3531         7694 :           continue;
    3532              :         }
    3533              : 
    3534        38757 :       if (!vect_is_simple_use (vinfo, slp_node,
    3535              :                                i, &op, &slp_op[i], &dt[i], &vectypes[i]))
    3536              :         {
    3537            0 :           if (dump_enabled_p ())
    3538            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3539              :                              "use not simple.\n");
    3540            0 :           return false;
    3541              :         }
    3542              : 
    3543              :       /* We can only handle calls with arguments of the same type.  */
    3544        38757 :       if (rhs_type
    3545        38757 :           && !types_compatible_p (rhs_type, TREE_TYPE (op)))
    3546              :         {
    3547         1286 :           if (dump_enabled_p ())
    3548          200 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3549              :                              "argument types differ.\n");
    3550         1286 :           return false;
    3551              :         }
    3552        37471 :       if (!rhs_type)
    3553        17781 :         rhs_type = TREE_TYPE (op);
    3554              : 
    3555        37471 :       if (!vectype_in)
    3556        18295 :         vectype_in = vectypes[i];
    3557        19176 :       else if (vectypes[i]
    3558        19176 :                && !types_compatible_p (vectypes[i], vectype_in))
    3559              :         {
    3560            0 :           if (dump_enabled_p ())
    3561            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3562              :                              "argument vector types differ.\n");
    3563            0 :           return false;
    3564              :         }
    3565              :     }
    3566              :   /* If all arguments are external or constant defs, infer the vector type
    3567              :      from the scalar type.  */
    3568        19702 :   if (!vectype_in)
    3569         5560 :     vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
    3570        19702 :   if (!cost_vec)
    3571         4215 :     gcc_assert (vectype_in);
    3572        15487 :   if (!vectype_in)
    3573              :     {
    3574         1031 :       if (dump_enabled_p ())
    3575            4 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3576              :                          "no vectype for scalar type %T\n", rhs_type);
    3577              : 
    3578         1031 :       return false;
    3579              :     }
    3580              : 
    3581        37342 :   if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
    3582        18671 :       != VECTOR_BOOLEAN_TYPE_P (vectype_in))
    3583              :     {
    3584           12 :       if (dump_enabled_p ())
    3585           12 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3586              :                          "mixed mask and nonmask vector types\n");
    3587           12 :       return false;
    3588              :     }
    3589              : 
    3590        18659 :   if (vect_emulated_vector_p (vectype_in)
    3591        18659 :       || vect_emulated_vector_p (vectype_out))
    3592              :     {
    3593            0 :       if (dump_enabled_p ())
    3594            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3595              :                          "use emulated vector type for call\n");
    3596            0 :       return false;
    3597              :     }
    3598              : 
    3599              :   /* FORNOW */
    3600        18659 :   nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
    3601        18659 :   nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
    3602        18659 :   if (known_eq (nunits_in * 2, nunits_out))
    3603              :     modifier = NARROW;
    3604        18074 :   else if (known_eq (nunits_out, nunits_in))
    3605              :     modifier = NONE;
    3606           45 :   else if (known_eq (nunits_out * 2, nunits_in))
    3607              :     modifier = WIDEN;
    3608              :   else
    3609              :     return false;
    3610              : 
    3611              :   /* We only handle functions that do not read or clobber memory.  */
    3612        37318 :   if (gimple_vuse (stmt))
    3613              :     {
    3614         1277 :       if (dump_enabled_p ())
    3615           14 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3616              :                          "function reads from or writes to memory.\n");
    3617         1277 :       return false;
    3618              :     }
    3619              : 
    3620              :   /* For now, we only vectorize functions if a target specific builtin
    3621              :      is available.  TODO -- in some cases, it might be profitable to
    3622              :      insert the calls for pieces of the vector, in order to be able
    3623              :      to vectorize other operations in the loop.  */
    3624        17382 :   fndecl = NULL_TREE;
    3625        17382 :   internal_fn ifn = IFN_LAST;
    3626        17382 :   tree callee = gimple_call_fndecl (stmt);
    3627              : 
    3628              :   /* First try using an internal function.  */
    3629        17382 :   code_helper convert_code = MAX_TREE_CODES;
    3630        17382 :   if (cfn != CFN_LAST
    3631        17382 :       && (modifier == NONE
    3632          192 :           || (modifier == NARROW
    3633          180 :               && simple_integer_narrowing (vectype_out, vectype_in,
    3634              :                                            &convert_code))))
    3635        16379 :     ifn = vectorizable_internal_function (cfn, callee, vectype_out,
    3636              :                                           vectype_in);
    3637              : 
    3638              :   /* Check if the operation traps.  */
    3639        17382 :   bool could_trap = gimple_could_trap_p (STMT_VINFO_STMT (stmt_info));
    3640        17382 :   if (could_trap && cost_vec && loop_vinfo)
    3641              :     {
    3642              :       /* If the operation can trap it must be conditional, otherwise fail.  */
    3643          474 :       internal_fn cond_fn = (internal_fn_mask_index (ifn) != -1
    3644          474 :                              ? ifn : get_conditional_internal_fn (ifn));
    3645          474 :       internal_fn cond_len_fn = get_len_internal_fn (cond_fn);
    3646          474 :       if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
    3647              :         {
    3648              :           /* We assume that BB SLP fills all lanes, so no inactive lanes can
    3649              :              cause issues.  */
    3650           84 :           if ((cond_fn == IFN_LAST
    3651           56 :                || !direct_internal_fn_supported_p (cond_fn, vectype_out,
    3652              :                                                    OPTIMIZE_FOR_SPEED))
    3653          140 :               && (cond_len_fn == IFN_LAST
    3654           56 :                   || !direct_internal_fn_supported_p (cond_len_fn, vectype_out,
    3655              :                                                       OPTIMIZE_FOR_SPEED)))
    3656              :             {
    3657           84 :               if (dump_enabled_p ())
    3658           10 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3659              :                                  "can't use a fully-masked loop because no"
    3660              :                                  " conditional operation is available.\n");
    3661           84 :               LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    3662              :             }
    3663              :         }
    3664              :     }
    3665              : 
    3666              :   /* If that fails, try asking for a target-specific built-in function.  */
    3667        17382 :   if (ifn == IFN_LAST)
    3668              :     {
    3669         9889 :       if (cfn != CFN_LAST)
    3670         9039 :         fndecl = targetm.vectorize.builtin_vectorized_function
    3671         9039 :           (cfn, vectype_out, vectype_in);
    3672          850 :       else if (callee && fndecl_built_in_p (callee, BUILT_IN_MD))
    3673           24 :         fndecl = targetm.vectorize.builtin_md_vectorized_function
    3674           24 :           (callee, vectype_out, vectype_in);
    3675              :     }
    3676              : 
    3677        17382 :   if (ifn == IFN_LAST && !fndecl)
    3678              :     {
    3679         9509 :       if (cfn == CFN_GOMP_SIMD_LANE
    3680         3207 :           && SLP_TREE_LANES (slp_node) == 1
    3681         3207 :           && loop_vinfo
    3682         3207 :           && LOOP_VINFO_LOOP (loop_vinfo)->simduid
    3683         3207 :           && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
    3684        15923 :           && LOOP_VINFO_LOOP (loop_vinfo)->simduid
    3685         3207 :              == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
    3686              :         {
    3687              :           /* We can handle IFN_GOMP_SIMD_LANE by returning a
    3688              :              { 0, 1, 2, ... vf - 1 } vector.  */
    3689         3207 :           gcc_assert (nargs == 0);
    3690              :         }
    3691         6302 :       else if (modifier == NONE
    3692         6302 :                && (gimple_call_builtin_p (stmt, BUILT_IN_BSWAP16)
    3693         5968 :                    || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP32)
    3694         5820 :                    || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP64)
    3695         5788 :                    || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP128)))
    3696          206 :         return vectorizable_bswap (vinfo, stmt_info, gsi, slp_node,
    3697          206 :                                    slp_op, vectype_in, cost_vec);
    3698              :       else
    3699              :         {
    3700         6096 :           if (dump_enabled_p ())
    3701          274 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3702              :                              "function is not vectorizable.\n");
    3703         6096 :           return false;
    3704              :         }
    3705              :     }
    3706              : 
    3707        11080 :   int reduc_idx = SLP_TREE_REDUC_IDX (slp_node);
    3708        11080 :   internal_fn cond_fn = (internal_fn_mask_index (ifn) != -1
    3709        11080 :                          ? ifn : get_conditional_internal_fn (ifn));
    3710        11080 :   internal_fn cond_len_fn = get_len_internal_fn (cond_fn);
    3711        11080 :   vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
    3712         9180 :   vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
    3713        11080 :   unsigned int nvectors = vect_get_num_copies (vinfo, slp_node);
    3714        11080 :   if (cost_vec) /* transformation not required.  */
    3715              :     {
    3716        21801 :       for (i = 0; i < nargs; ++i)
    3717        14924 :         if (!vect_maybe_update_slp_op_vectype (slp_op[i],
    3718        14924 :                                                vectypes[i]
    3719              :                                                ? vectypes[i] : vectype_in))
    3720              :           {
    3721            0 :             if (dump_enabled_p ())
    3722            0 :               dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3723              :                                "incompatible vector types for invariants\n");
    3724            0 :             return false;
    3725              :           }
    3726         6877 :       SLP_TREE_TYPE (slp_node) = call_vec_info_type;
    3727         6877 :       DUMP_VECT_SCOPE ("vectorizable_call");
    3728         6877 :       vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
    3729              : 
    3730         6877 :       if (loop_vinfo
    3731         5923 :           && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
    3732         4056 :           && (reduc_idx >= 0 || could_trap || mask_opno >= 0))
    3733              :         {
    3734         2558 :           if (reduc_idx >= 0
    3735         1631 :               && (cond_fn == IFN_LAST
    3736         1631 :                   || !direct_internal_fn_supported_p (cond_fn, vectype_out,
    3737              :                                                       OPTIMIZE_FOR_SPEED))
    3738         2570 :               && (cond_len_fn == IFN_LAST
    3739           12 :                   || !direct_internal_fn_supported_p (cond_len_fn, vectype_out,
    3740              :                                                       OPTIMIZE_FOR_SPEED)))
    3741              :             {
    3742           12 :               if (dump_enabled_p ())
    3743            6 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    3744              :                                  "can't use a fully-masked loop because no"
    3745              :                                  " conditional operation is available.\n");
    3746           12 :               LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    3747              :             }
    3748              :           else
    3749              :             {
    3750         2546 :               tree scalar_mask = NULL_TREE;
    3751         2546 :               if (mask_opno >= 0)
    3752         2546 :                 scalar_mask = gimple_call_arg (stmt_info->stmt, mask_opno);
    3753         2546 :               if (cond_len_fn != IFN_LAST
    3754         2546 :                   && direct_internal_fn_supported_p (cond_len_fn, vectype_out,
    3755              :                                                      OPTIMIZE_FOR_SPEED))
    3756            0 :                 vect_record_loop_len (loop_vinfo, lens, nvectors, vectype_out,
    3757              :                                       1);
    3758              :               else
    3759         2546 :                 vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype_out,
    3760              :                                        scalar_mask);
    3761              :             }
    3762              :         }
    3763         6877 :       return true;
    3764              :     }
    3765              : 
    3766              :   /* Transform.  */
    3767              : 
    3768         4203 :   if (dump_enabled_p ())
    3769          416 :     dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
    3770              : 
    3771              :   /* Handle def.  */
    3772         4203 :   scalar_dest = gimple_call_lhs (stmt);
    3773         4203 :   vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
    3774              : 
    3775         4203 :   bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
    3776         3257 :   bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
    3777         4203 :   unsigned int vect_nargs = nargs;
    3778         4203 :   if (len_loop_p && (reduc_idx >= 0 || could_trap || mask_opno >= 0))
    3779              :     {
    3780            0 :       ifn = cond_len_fn;
    3781              :       /* COND_* -> COND_LEN_* takes 2 extra arguments:LEN,BIAS.  */
    3782            0 :       vect_nargs += 2;
    3783              :       /* But unless there's a mask argument already we need that
    3784              :          as well, and an else value.  */
    3785            0 :       if (mask_opno == -1)
    3786            0 :         vect_nargs += 2;
    3787              :     }
    3788         4203 :   else if (masked_loop_p && mask_opno == -1 && (reduc_idx >= 0 || could_trap))
    3789              :     {
    3790            0 :       ifn = cond_fn;
    3791            0 :       vect_nargs += 2;
    3792              :     }
    3793         4203 :   int len_opno = internal_fn_len_index (ifn);
    3794         4203 :   if (clz_ctz_arg1)
    3795           59 :     ++vect_nargs;
    3796              : 
    3797         4203 :   if (modifier == NONE || ifn != IFN_LAST)
    3798              :     {
    3799         4171 :       tree prev_res = NULL_TREE;
    3800         4171 :       vargs.safe_grow (vect_nargs, true);
    3801         4171 :       auto_vec<vec<tree> > vec_defs (nargs);
    3802              : 
    3803              :       /* Build argument list for the vectorized call.  */
    3804         4171 :       if (cfn == CFN_GOMP_SIMD_LANE)
    3805              :         {
    3806         3308 :           for (i = 0; i < nvectors; ++i)
    3807              :             {
    3808              :               /* ???  For multi-lane SLP we'd need to build
    3809              :                  { 0, 0, .., 1, 1, ... }.  */
    3810         1708 :               tree cst = build_index_vector (vectype_out,
    3811              :                                              i * nunits_out, 1);
    3812         1708 :               tree new_var
    3813         1708 :                 = vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
    3814         1708 :               gimple *init_stmt = gimple_build_assign (new_var, cst);
    3815         1708 :               vect_init_vector_1 (vinfo, stmt_info, init_stmt, NULL);
    3816         1708 :               new_temp = make_ssa_name (vec_dest);
    3817         1708 :               gimple *new_stmt = gimple_build_assign (new_temp, new_var);
    3818         1708 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3819         1708 :               slp_node->push_vec_def (new_stmt);
    3820              :             }
    3821              :         }
    3822              :       else
    3823              :         {
    3824         2571 :           vec<tree> vec_oprnds0;
    3825         2571 :           vect_get_slp_defs (vinfo, slp_node, &vec_defs);
    3826         2571 :           vec_oprnds0 = vec_defs[0];
    3827              : 
    3828              :           /* Arguments are ready.  Create the new vector stmt.  */
    3829         5283 :           FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
    3830              :             {
    3831         2712 :               int varg = 0;
    3832              :               /* Add the mask if necessary.  */
    3833           38 :               if ((masked_loop_p || len_loop_p) && mask_opno == -1
    3834         2714 :                   && internal_fn_mask_index (ifn) != -1)
    3835              :                 {
    3836            0 :                   gcc_assert (internal_fn_mask_index (ifn) == varg);
    3837            0 :                   if (masked_loop_p)
    3838              :                     {
    3839            0 :                       unsigned int vec_num = vec_oprnds0.length ();
    3840            0 :                       vargs[varg++] = vect_get_loop_mask (loop_vinfo, gsi,
    3841              :                                                           masks, vec_num,
    3842              :                                                           vectype_out, i);
    3843              :                     }
    3844              :                   else
    3845              :                     {
    3846            0 :                       tree mask_vectype = truth_type_for (vectype_out);
    3847            0 :                       vargs[varg++] = vect_build_all_ones_mask (loop_vinfo,
    3848              :                                                                 stmt_info,
    3849              :                                                                 mask_vectype);
    3850              :                     }
    3851              :                 }
    3852              :               size_t k;
    3853         9937 :               for (k = 0; k < nargs; k++)
    3854              :                 {
    3855         7225 :                   vec<tree> vec_oprndsk = vec_defs[k];
    3856         7225 :                   vargs[varg++] = vec_oprndsk[i];
    3857              :                 }
    3858              :               /* Add the else value if necessary.  */
    3859           38 :               if ((masked_loop_p || len_loop_p) && mask_opno == -1
    3860         2714 :                   && internal_fn_else_index (ifn) != -1)
    3861              :                 {
    3862            0 :                   gcc_assert (internal_fn_else_index (ifn) == varg);
    3863            0 :                   if (reduc_idx >= 0)
    3864            0 :                     vargs[varg++] = vargs[reduc_idx + 1];
    3865              :                   else
    3866              :                     {
    3867            0 :                       auto else_value = targetm.preferred_else_value
    3868            0 :                         (ifn, vectype_out, varg - 1, &vargs[1]);
    3869            0 :                       vargs[varg++] = else_value;
    3870              :                     }
    3871              :                 }
    3872         2712 :               if (clz_ctz_arg1)
    3873           59 :                 vargs[varg++] = clz_ctz_arg1;
    3874              : 
    3875         2712 :               gimple *new_stmt;
    3876         2712 :               if (modifier == NARROW)
    3877              :                 {
    3878              :                   /* We don't define any narrowing conditional functions
    3879              :                      at present.  */
    3880            0 :                   gcc_assert (mask_opno < 0);
    3881            0 :                   tree half_res = make_ssa_name (vectype_in);
    3882            0 :                   gcall *call = gimple_build_call_internal_vec (ifn, vargs);
    3883            0 :                   gimple_call_set_lhs (call, half_res);
    3884            0 :                   gimple_call_set_nothrow (call, true);
    3885            0 :                   vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
    3886            0 :                   if ((i & 1) == 0)
    3887              :                     {
    3888            0 :                       prev_res = half_res;
    3889            0 :                       continue;
    3890              :                     }
    3891            0 :                   new_temp = make_ssa_name (vec_dest);
    3892            0 :                   new_stmt = vect_gimple_build (new_temp, convert_code,
    3893              :                                                 prev_res, half_res);
    3894            0 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    3895              :                 }
    3896              :               else
    3897              :                 {
    3898         2712 :                   if (len_opno >= 0 && len_loop_p)
    3899              :                     {
    3900            0 :                       unsigned int vec_num = vec_oprnds0.length ();
    3901            0 :                       tree len = vect_get_loop_len (loop_vinfo, gsi, lens,
    3902              :                                                     vec_num, vectype_out, i, 1, true);
    3903            0 :                       signed char biasval
    3904            0 :                         = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
    3905            0 :                       tree bias = build_int_cst (intQI_type_node, biasval);
    3906            0 :                       vargs[len_opno] = len;
    3907            0 :                       vargs[len_opno + 1] = bias;
    3908              :                     }
    3909         2712 :                   else if (mask_opno >= 0 && masked_loop_p)
    3910              :                     {
    3911           36 :                       unsigned int vec_num = vec_oprnds0.length ();
    3912           36 :                       tree mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
    3913              :                                                       vec_num, vectype_out, i);
    3914           36 :                       vargs[mask_opno]
    3915           72 :                         = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
    3916           36 :                                             vargs[mask_opno], gsi);
    3917              :                     }
    3918              : 
    3919         2712 :                   gcall *call;
    3920         2712 :                   if (ifn != IFN_LAST)
    3921         2631 :                     call = gimple_build_call_internal_vec (ifn, vargs);
    3922              :                   else
    3923           81 :                     call = gimple_build_call_vec (fndecl, vargs);
    3924         2712 :                   new_temp = make_ssa_name (vec_dest, call);
    3925         2712 :                   gimple_call_set_lhs (call, new_temp);
    3926         2712 :                   gimple_call_set_nothrow (call, true);
    3927         2712 :                   vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
    3928         2712 :                   new_stmt = call;
    3929              :                 }
    3930         2712 :               slp_node->push_vec_def (new_stmt);
    3931              :             }
    3932              :         }
    3933              : 
    3934        11058 :       for (i = 0; i < nargs; i++)
    3935              :         {
    3936         6887 :           vec<tree> vec_oprndsi = vec_defs[i];
    3937         6887 :           vec_oprndsi.release ();
    3938              :         }
    3939         4171 :     }
    3940           32 :   else if (modifier == NARROW)
    3941              :     {
    3942           32 :       auto_vec<vec<tree> > vec_defs (nargs);
    3943              :       /* We don't define any narrowing conditional functions at present.  */
    3944           32 :       gcc_assert (mask_opno < 0);
    3945              : 
    3946              :       /* Build argument list for the vectorized call.  */
    3947           32 :       vargs.create (nargs * 2);
    3948              : 
    3949           32 :       vect_get_slp_defs (vinfo, slp_node, &vec_defs);
    3950           32 :       vec<tree> vec_oprnds0 = vec_defs[0];
    3951              : 
    3952              :       /* Arguments are ready.  Create the new vector stmt.  */
    3953           64 :       for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
    3954              :         {
    3955           32 :           size_t k;
    3956           32 :           vargs.truncate (0);
    3957           64 :           for (k = 0; k < nargs; k++)
    3958              :             {
    3959           32 :               vec<tree> vec_oprndsk = vec_defs[k];
    3960           32 :               vargs.quick_push (vec_oprndsk[i]);
    3961           32 :               vargs.quick_push (vec_oprndsk[i + 1]);
    3962              :             }
    3963           32 :           gcall *call;
    3964           32 :           if (ifn != IFN_LAST)
    3965              :             call = gimple_build_call_internal_vec (ifn, vargs);
    3966              :           else
    3967           32 :             call = gimple_build_call_vec (fndecl, vargs);
    3968           32 :           new_temp = make_ssa_name (vec_dest, call);
    3969           32 :           gimple_call_set_lhs (call, new_temp);
    3970           32 :           gimple_call_set_nothrow (call, true);
    3971           32 :           vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
    3972           32 :           slp_node->push_vec_def (call);
    3973              :         }
    3974              : 
    3975           64 :       for (i = 0; i < nargs; i++)
    3976              :         {
    3977           32 :           vec<tree> vec_oprndsi = vec_defs[i];
    3978           32 :           vec_oprndsi.release ();
    3979              :         }
    3980           32 :     }
    3981              :   else
    3982              :     /* No current target implements this case.  */
    3983              :     return false;
    3984              : 
    3985         4203 :   vargs.release ();
    3986              : 
    3987         4203 :   return true;
    3988      2700884 : }
    3989              : 
    3990              : 
    3991              : struct simd_call_arg_info
    3992              : {
    3993              :   tree vectype;
    3994              :   tree op;
    3995              :   HOST_WIDE_INT linear_step;
    3996              :   enum vect_def_type dt;
    3997              :   unsigned int align;
    3998              :   bool simd_lane_linear;
    3999              : };
    4000              : 
    4001              : /* Helper function of vectorizable_simd_clone_call.  If OP, an SSA_NAME,
    4002              :    is linear within simd lane (but not within whole loop), note it in
    4003              :    *ARGINFO.  */
    4004              : 
    4005              : static void
    4006           15 : vect_simd_lane_linear (tree op, class loop *loop,
    4007              :                        struct simd_call_arg_info *arginfo)
    4008              : {
    4009           15 :   gimple *def_stmt = SSA_NAME_DEF_STMT (op);
    4010              : 
    4011           15 :   if (!is_gimple_assign (def_stmt)
    4012           15 :       || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR
    4013           27 :       || !is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
    4014            3 :     return;
    4015              : 
    4016           12 :   tree base = gimple_assign_rhs1 (def_stmt);
    4017           12 :   HOST_WIDE_INT linear_step = 0;
    4018           12 :   tree v = gimple_assign_rhs2 (def_stmt);
    4019           48 :   while (TREE_CODE (v) == SSA_NAME)
    4020              :     {
    4021           36 :       tree t;
    4022           36 :       def_stmt = SSA_NAME_DEF_STMT (v);
    4023           36 :       if (is_gimple_assign (def_stmt))
    4024           24 :         switch (gimple_assign_rhs_code (def_stmt))
    4025              :           {
    4026            0 :           case PLUS_EXPR:
    4027            0 :             t = gimple_assign_rhs2 (def_stmt);
    4028            0 :             if (linear_step || TREE_CODE (t) != INTEGER_CST)
    4029              :               return;
    4030            0 :             base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, t);
    4031            0 :             v = gimple_assign_rhs1 (def_stmt);
    4032            0 :             continue;
    4033           12 :           case MULT_EXPR:
    4034           12 :             t = gimple_assign_rhs2 (def_stmt);
    4035           12 :             if (linear_step || !tree_fits_shwi_p (t) || integer_zerop (t))
    4036            0 :               return;
    4037           12 :             linear_step = tree_to_shwi (t);
    4038           12 :             v = gimple_assign_rhs1 (def_stmt);
    4039           12 :             continue;
    4040           12 :           CASE_CONVERT:
    4041           12 :             t = gimple_assign_rhs1 (def_stmt);
    4042           12 :             if (TREE_CODE (TREE_TYPE (t)) != INTEGER_TYPE
    4043           12 :                 || (TYPE_PRECISION (TREE_TYPE (v))
    4044           12 :                     < TYPE_PRECISION (TREE_TYPE (t))))
    4045              :               return;
    4046           12 :             if (!linear_step)
    4047            0 :               linear_step = 1;
    4048           12 :             v = t;
    4049           12 :             continue;
    4050              :           default:
    4051              :             return;
    4052              :           }
    4053           12 :       else if (gimple_call_internal_p (def_stmt, IFN_GOMP_SIMD_LANE)
    4054           12 :                && loop->simduid
    4055           12 :                && TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME
    4056           24 :                && (SSA_NAME_VAR (gimple_call_arg (def_stmt, 0))
    4057              :                    == loop->simduid))
    4058              :         {
    4059           12 :           if (!linear_step)
    4060            0 :             linear_step = 1;
    4061           12 :           arginfo->linear_step = linear_step;
    4062           12 :           arginfo->op = base;
    4063           12 :           arginfo->simd_lane_linear = true;
    4064           12 :           return;
    4065              :         }
    4066              :     }
    4067              : }
    4068              : 
    4069              : /* Function vectorizable_simd_clone_call.
    4070              : 
    4071              :    Check if STMT_INFO performs a function call that can be vectorized
    4072              :    by calling a simd clone of the function.
    4073              :    If COST_VEC is passed, calculate costs but don't change anything,
    4074              :    otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
    4075              :    it, and insert it at GSI.
    4076              :    Return true if STMT_INFO is vectorizable in this way.  */
    4077              : 
    4078              : static bool
    4079      2690014 : vectorizable_simd_clone_call (vec_info *vinfo, stmt_vec_info stmt_info,
    4080              :                               gimple_stmt_iterator *gsi,
    4081              :                               slp_tree slp_node,
    4082              :                               stmt_vector_for_cost *cost_vec)
    4083              : {
    4084      2690014 :   tree vec_dest;
    4085      2690014 :   tree scalar_dest;
    4086      2690014 :   tree vec_oprnd0 = NULL_TREE;
    4087      2690014 :   tree vectype;
    4088      2690014 :   poly_uint64 nunits;
    4089      2690014 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    4090      2690014 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
    4091      2690014 :   class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
    4092      2690014 :   tree fndecl, new_temp;
    4093      2690014 :   int j;
    4094      2690014 :   auto_vec<simd_call_arg_info> arginfo;
    4095      2690014 :   vec<tree> vargs = vNULL;
    4096      2690014 :   size_t i, nargs;
    4097      2690014 :   tree rtype, ratype;
    4098      2690014 :   vec<constructor_elt, va_gc> *ret_ctor_elts = NULL;
    4099      2690014 :   int masked_call_offset = 0;
    4100              : 
    4101              :   /* Is STMT a vectorizable call?   */
    4102      2690014 :   gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
    4103        15370 :   if (!stmt)
    4104              :     return false;
    4105              : 
    4106        15370 :   fndecl = gimple_call_fndecl (stmt);
    4107        15370 :   if (fndecl == NULL_TREE
    4108        15370 :       && gimple_call_internal_p (stmt, IFN_MASK_CALL))
    4109              :     {
    4110          220 :       fndecl = gimple_call_arg (stmt, 0);
    4111          220 :       gcc_checking_assert (TREE_CODE (fndecl) == ADDR_EXPR);
    4112          220 :       fndecl = TREE_OPERAND (fndecl, 0);
    4113          220 :       gcc_checking_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
    4114              :       masked_call_offset = 1;
    4115              :     }
    4116        15150 :   if (fndecl == NULL_TREE)
    4117              :     return false;
    4118              : 
    4119         4927 :   struct cgraph_node *node = cgraph_node::get (fndecl);
    4120         4927 :   if (node == NULL || node->simd_clones == NULL)
    4121              :     return false;
    4122              : 
    4123         1536 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
    4124              :     return false;
    4125              : 
    4126         1536 :   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
    4127            0 :       && cost_vec)
    4128              :     return false;
    4129              : 
    4130         1536 :   if (gimple_call_lhs (stmt)
    4131         1536 :       && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
    4132              :     return false;
    4133              : 
    4134         1536 :   gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
    4135              : 
    4136         1536 :   vectype = SLP_TREE_VECTYPE (slp_node);
    4137              : 
    4138      2690078 :   if (loop_vinfo && nested_in_vect_loop_p (loop, stmt_info))
    4139              :     return false;
    4140              : 
    4141              :   /* Process function arguments.  */
    4142         1536 :   nargs = gimple_call_num_args (stmt) - masked_call_offset;
    4143              : 
    4144              :   /* Bail out if the function has zero arguments.  */
    4145         1536 :   if (nargs == 0)
    4146              :     return false;
    4147              : 
    4148         1472 :   vect_simd_clone_data _data;
    4149         1472 :   vect_simd_clone_data &data = slp_node->get_data (_data);
    4150         1472 :   vec<tree>& simd_clone_info = data.simd_clone_info;
    4151         1472 :   arginfo.reserve (nargs, true);
    4152         1472 :   auto_vec<slp_tree> slp_op;
    4153         1472 :   slp_op.safe_grow_cleared (nargs);
    4154              : 
    4155         4173 :   for (i = 0; i < nargs; i++)
    4156              :     {
    4157         2701 :       simd_call_arg_info thisarginfo;
    4158         2701 :       affine_iv iv;
    4159         2701 :       tree op;
    4160              : 
    4161         2701 :       thisarginfo.linear_step = 0;
    4162         2701 :       thisarginfo.align = 0;
    4163         2701 :       thisarginfo.op = NULL_TREE;
    4164         2701 :       thisarginfo.simd_lane_linear = false;
    4165              : 
    4166         5402 :       int op_no = vect_slp_child_index_for_operand (stmt_info,
    4167         2701 :                                                     i + masked_call_offset);
    4168         5402 :       if (!vect_is_simple_use (vinfo, slp_node,
    4169         2701 :                                op_no, &op, &slp_op[i],
    4170              :                                &thisarginfo.dt, &thisarginfo.vectype)
    4171         2701 :           || thisarginfo.dt == vect_uninitialized_def)
    4172              :         {
    4173            0 :           if (dump_enabled_p ())
    4174            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    4175              :                              "use not simple.\n");
    4176            0 :           return false;
    4177              :         }
    4178              : 
    4179         2701 :       if (thisarginfo.dt == vect_constant_def
    4180         2701 :           || thisarginfo.dt == vect_external_def)
    4181              :         {
    4182              :           /* With SLP we determine the vector type of constants/externals
    4183              :              at analysis time, handling conflicts via
    4184              :              vect_maybe_update_slp_op_vectype.  At transform time
    4185              :              we have a vector type recorded for SLP.  */
    4186          740 :           gcc_assert (cost_vec
    4187              :                       || thisarginfo.vectype != NULL_TREE);
    4188              :           if (cost_vec)
    4189          609 :             thisarginfo.vectype = get_vectype_for_scalar_type (vinfo,
    4190          609 :                                                                TREE_TYPE (op),
    4191              :                                                                slp_node);
    4192              :         }
    4193              :       else
    4194         1961 :         gcc_assert (thisarginfo.vectype != NULL_TREE);
    4195              : 
    4196              :       /* For linear arguments, the analyze phase should have saved
    4197              :          the base and step.  */
    4198         2570 :       if (!cost_vec
    4199         1594 :           && i * 3 + 4 <= simd_clone_info.length ()
    4200         2780 :           && simd_clone_info[i * 3 + 2])
    4201              :         {
    4202          118 :           thisarginfo.linear_step = tree_to_shwi (simd_clone_info[i * 3 + 2]);
    4203          118 :           thisarginfo.op = simd_clone_info[i * 3 + 1];
    4204          118 :           thisarginfo.simd_lane_linear
    4205          118 :             = (simd_clone_info[i * 3 + 3] == boolean_true_node);
    4206              :           /* If loop has been peeled for alignment, we need to adjust it.  */
    4207          118 :           tree n1 = LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo);
    4208          118 :           tree n2 = LOOP_VINFO_NITERS (loop_vinfo);
    4209          118 :           if (n1 != n2 && !thisarginfo.simd_lane_linear)
    4210              :             {
    4211            0 :               tree bias = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n1, n2);
    4212            0 :               tree step = simd_clone_info[i * 3 + 2];
    4213            0 :               tree opt = TREE_TYPE (thisarginfo.op);
    4214            0 :               bias = fold_convert (TREE_TYPE (step), bias);
    4215            0 :               bias = fold_build2 (MULT_EXPR, TREE_TYPE (step), bias, step);
    4216            0 :               thisarginfo.op
    4217            0 :                 = fold_build2 (POINTER_TYPE_P (opt)
    4218              :                                ? POINTER_PLUS_EXPR : PLUS_EXPR, opt,
    4219              :                                thisarginfo.op, bias);
    4220              :             }
    4221              :         }
    4222         2583 :       else if (cost_vec
    4223         1904 :                && thisarginfo.dt != vect_constant_def
    4224         1777 :                && thisarginfo.dt != vect_external_def
    4225         1295 :                && loop_vinfo
    4226         1290 :                && SLP_TREE_LANES (slp_node) == 1
    4227         1266 :                && TREE_CODE (op) == SSA_NAME
    4228         2532 :                && simple_iv (loop, loop_containing_stmt (stmt), op,
    4229              :                              &iv, false)
    4230         2795 :                && tree_fits_shwi_p (iv.step))
    4231              :         {
    4232          212 :           thisarginfo.linear_step = tree_to_shwi (iv.step);
    4233          212 :           thisarginfo.op = iv.base;
    4234              :         }
    4235         2371 :       else if ((thisarginfo.dt == vect_constant_def
    4236         2371 :                 || thisarginfo.dt == vect_external_def)
    4237          740 :                && SLP_TREE_LANES (slp_node) == 1
    4238         2677 :                && POINTER_TYPE_P (TREE_TYPE (op)))
    4239           86 :         thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
    4240              :       /* Addresses of array elements indexed by GOMP_SIMD_LANE are
    4241              :          linear too.  */
    4242         2701 :       if (SLP_TREE_LANES (slp_node) == 1
    4243         2221 :           && POINTER_TYPE_P (TREE_TYPE (op))
    4244          196 :           && !thisarginfo.linear_step
    4245          112 :           && cost_vec
    4246           58 :           && thisarginfo.dt != vect_constant_def
    4247           58 :           && thisarginfo.dt != vect_external_def
    4248           15 :           && loop_vinfo
    4249         2716 :           && TREE_CODE (op) == SSA_NAME)
    4250           15 :         vect_simd_lane_linear (op, loop, &thisarginfo);
    4251              : 
    4252         2701 :       if (!vectype)
    4253           12 :         vectype = thisarginfo.vectype;
    4254         2701 :       arginfo.quick_push (thisarginfo);
    4255              :     }
    4256              : 
    4257         1472 :   poly_uint64 vf = loop_vinfo ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
    4258         1472 :   unsigned group_size = SLP_TREE_LANES (slp_node);
    4259         1472 :   unsigned int badness = 0;
    4260         1472 :   unsigned int badness_inbranch = 0;
    4261         1472 :   struct cgraph_node *bestn = NULL;
    4262         1472 :   struct cgraph_node *bestn_inbranch = NULL;
    4263         1472 :   if (!cost_vec)
    4264          362 :     bestn = ((loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
    4265          362 :              ? data.clone_inbranch : data.clone);
    4266              :   else
    4267         6376 :     for (struct cgraph_node *n = node->simd_clones; n != NULL;
    4268         5266 :          n = n->simdclone->next_clone)
    4269              :       {
    4270         5266 :         unsigned int this_badness = 0;
    4271         5266 :         unsigned int num_calls;
    4272              :         /* The number of arguments in the call and the number of parameters in
    4273              :            the simdclone should match.  However, when the simdclone is
    4274              :            'inbranch', it could have one more paramater than nargs when using
    4275              :            an inbranch simdclone to call a non-inbranch call, either in a
    4276              :            non-masked loop using a all true constant mask, or inside a masked
    4277              :            loop using it's mask.  */
    4278         5266 :         size_t simd_nargs = n->simdclone->nargs;
    4279         5266 :         if (!masked_call_offset && n->simdclone->inbranch)
    4280         2511 :           simd_nargs--;
    4281         5266 :         if (!constant_multiple_p (vf * group_size, n->simdclone->simdlen,
    4282              :                                   &num_calls)
    4283         1974 :             || (!n->simdclone->inbranch && (masked_call_offset > 0))
    4284         1790 :             || (nargs != simd_nargs))
    4285         3476 :           continue;
    4286         1790 :         if (num_calls != 1)
    4287         1142 :           this_badness += floor_log2 (num_calls) * 4096;
    4288         1790 :         if (n->simdclone->inbranch)
    4289          771 :           this_badness += 8192;
    4290              : 
    4291              :         /* If SLP_TREE_VECTYPE has not been set yet pass the general vector
    4292              :            mode,  which for targets that use it will determine what ISA we can
    4293              :            vectorize this code with.  */
    4294         1790 :         machine_mode vector_mode = vinfo->vector_mode;
    4295         1790 :         if (vectype)
    4296         1790 :           vector_mode = TYPE_MODE (vectype);
    4297         1790 :         int target_badness = targetm.simd_clone.usable (n, vector_mode);
    4298         1790 :         if (target_badness < 0)
    4299          368 :           continue;
    4300         1422 :         this_badness += target_badness * 512;
    4301         4192 :         for (i = 0; i < nargs; i++)
    4302              :           {
    4303         3018 :             switch (n->simdclone->args[i].arg_type)
    4304              :               {
    4305         2088 :               case SIMD_CLONE_ARG_TYPE_VECTOR:
    4306         2088 :                 if (VECTOR_BOOLEAN_TYPE_P (n->simdclone->args[i].vector_type))
    4307              :                   /* Vector mask arguments are not supported.  */
    4308              :                   i = -1;
    4309         2080 :                 else if (!useless_type_conversion_p
    4310         2080 :                          (n->simdclone->args[i].orig_type,
    4311         2080 :                           TREE_TYPE (gimple_call_arg (stmt,
    4312              :                                                       i + masked_call_offset))))
    4313              :                   i = -1;
    4314         2080 :                 else if (arginfo[i].dt == vect_constant_def
    4315         1973 :                          || arginfo[i].dt == vect_external_def
    4316         3989 :                          || arginfo[i].linear_step)
    4317          399 :                   this_badness += 64;
    4318              :                 break;
    4319          310 :               case SIMD_CLONE_ARG_TYPE_UNIFORM:
    4320          310 :                 if ((arginfo[i].dt != vect_constant_def
    4321          145 :                      && arginfo[i].dt != vect_external_def)
    4322          410 :                     || SLP_TREE_LANES (slp_node) != 1)
    4323              :                   i = -1;
    4324              :                 break;
    4325          324 :               case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
    4326          324 :               case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
    4327          324 :                 if (arginfo[i].dt == vect_constant_def
    4328          324 :                     || arginfo[i].dt == vect_external_def
    4329          324 :                     || (arginfo[i].linear_step
    4330          324 :                         != n->simdclone->args[i].linear_step))
    4331              :                   i = -1;
    4332              :                 break;
    4333              :               case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
    4334              :               case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
    4335              :               case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
    4336              :               case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
    4337              :               case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
    4338              :               case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
    4339              :                 /* FORNOW */
    4340              :                 i = -1;
    4341              :                 break;
    4342          296 :               case SIMD_CLONE_ARG_TYPE_MASK:
    4343          296 :                 if (!SCALAR_INT_MODE_P (n->simdclone->mask_mode)
    4344          264 :                     && n->simdclone->mask_mode != VOIDmode)
    4345              :                   i = -1;
    4346              :                 /* While we can create a traditional data vector from
    4347              :                    an incoming integer mode mask we have no good way to
    4348              :                    force generate an integer mode mask from a traditional
    4349              :                    boolean vector input.  */
    4350          296 :                 else if (SCALAR_INT_MODE_P (n->simdclone->mask_mode)
    4351          296 :                          && !SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
    4352              :                   i = -1;
    4353          290 :                 else if (n->simdclone->mask_mode == VOIDmode
    4354              :                          /* FORNOW we only have partial support for vector-type
    4355              :                             masks that can't hold all of simdlen. */
    4356          554 :                          && (maybe_ne (TYPE_VECTOR_SUBPARTS (n->simdclone->args[i].vector_type),
    4357          264 :                                        TYPE_VECTOR_SUBPARTS (arginfo[i].vectype))
    4358              :                              /* Verify we can compute the mask argument.  */
    4359          111 :                              || !expand_vec_cond_expr_p (n->simdclone->args[i].vector_type,
    4360          111 :                                                          arginfo[i].vectype)))
    4361              :                   i = -1;
    4362          125 :                 else if (SCALAR_INT_MODE_P (n->simdclone->mask_mode)
    4363              :                          /* FORNOW we only have partial support for
    4364              :                             integer-type masks that represent the same number
    4365              :                             of lanes as the vectorized mask inputs.  */
    4366          151 :                          && maybe_ne (exact_div (n->simdclone->simdlen,
    4367              :                                                  n->simdclone->args[i].linear_step),
    4368           26 :                                       TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
    4369              :                   i = -1;
    4370          107 :                 else if (!SCALAR_INT_MODE_P (n->simdclone->mask_mode)
    4371          107 :                          && SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
    4372            8 :                   this_badness += 2048;
    4373              :                 break;
    4374              :               }
    4375          183 :             if (i == (size_t) -1)
    4376              :               break;
    4377         2770 :             if (n->simdclone->args[i].alignment > arginfo[i].align)
    4378              :               {
    4379              :                 i = -1;
    4380              :                 break;
    4381              :               }
    4382         2770 :             if (arginfo[i].align)
    4383          110 :               this_badness += (exact_log2 (arginfo[i].align)
    4384          160 :                                - exact_log2 (n->simdclone->args[i].alignment));
    4385              :           }
    4386         1422 :         if (i == (size_t) -1)
    4387          248 :           continue;
    4388         1174 :         if (masked_call_offset == 0
    4389         1067 :             && n->simdclone->inbranch
    4390          347 :             && n->simdclone->nargs > nargs)
    4391              :           {
    4392          347 :             gcc_assert (n->simdclone->args[n->simdclone->nargs - 1].arg_type ==
    4393              :                         SIMD_CLONE_ARG_TYPE_MASK);
    4394              :             /* Penalize using a masked SIMD clone in a non-masked loop, that is
    4395              :                not in a branch, as we'd have to construct an all-true mask.  */
    4396          347 :             this_badness += 64;
    4397              :           }
    4398         1174 :         if (bestn == NULL || this_badness < badness)
    4399              :           {
    4400          817 :             bestn = n;
    4401          817 :             badness = this_badness;
    4402              :           }
    4403         1174 :         if (n->simdclone->inbranch
    4404          454 :             && (bestn_inbranch == NULL || this_badness < badness_inbranch))
    4405              :           {
    4406         5266 :             bestn_inbranch = n;
    4407         5266 :             badness_inbranch = this_badness;
    4408              :           }
    4409              :       }
    4410              : 
    4411         1472 :   if (bestn == NULL)
    4412              :     return false;
    4413              : 
    4414          829 :   fndecl = bestn->decl;
    4415          829 :   nunits = bestn->simdclone->simdlen;
    4416          829 :   int ncopies = vector_unroll_factor (vf * group_size, nunits);
    4417              : 
    4418              :   /* If the function isn't const, only allow it in simd loops where user
    4419              :      has asserted that at least nunits consecutive iterations can be
    4420              :      performed using SIMD instructions.  */
    4421          824 :   if ((loop == NULL || maybe_lt ((unsigned) loop->safelen, nunits))
    4422         1006 :       && gimple_vuse (stmt))
    4423              :     return false;
    4424              : 
    4425              :   /* ncopies is the number of SIMD clone calls we create, since simdlen
    4426              :      is not necessarily matching nunits of the vector types used, track
    4427              :      that in ncopies_in.  */
    4428          829 :   int ncopies_in = vect_get_num_vectors (vf * group_size, vectype);
    4429              : 
    4430              :   /* Sanity check: make sure that at least one copy of the vectorized stmt
    4431              :      needs to be generated.  */
    4432          829 :   gcc_assert (ncopies >= 1);
    4433              : 
    4434          829 :   if (cost_vec) /* transformation not required.  */
    4435              :     {
    4436         1514 :       for (unsigned i = 0; i < nargs; ++i)
    4437         1047 :         if (!vect_maybe_update_slp_op_vectype (slp_op[i], arginfo[i].vectype))
    4438              :           {
    4439            0 :             if (dump_enabled_p ())
    4440            0 :               dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    4441              :                                "incompatible vector types for invariants\n");
    4442            0 :             return false;
    4443              :           }
    4444              : 
    4445          467 :       if (!bestn_inbranch && loop_vinfo)
    4446              :         {
    4447          248 :           if (dump_enabled_p ()
    4448          248 :               && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
    4449          171 :             dump_printf_loc (MSG_NOTE, vect_location,
    4450              :                              "can't use a fully-masked loop because no"
    4451              :                              " masked simd clone was available.\n");
    4452          248 :           LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    4453              :         }
    4454              : 
    4455              :       /* When the original call is pure or const but the SIMD ABI dictates
    4456              :          an aggregate return we will have to use a virtual definition and
    4457              :          in a loop eventually even need to add a virtual PHI.  That's
    4458              :          not straight-forward so allow to fix this up via renaming.  */
    4459          467 :       if (gimple_call_lhs (stmt)
    4460          461 :           && !gimple_vdef (stmt)
    4461          832 :           && TREE_CODE (TREE_TYPE (TREE_TYPE (bestn->decl))) == ARRAY_TYPE)
    4462           27 :         vinfo->any_known_not_updated_vssa = true;
    4463              :       /* ???  For SLP code-gen we end up inserting after the last
    4464              :          vector argument def rather than at the original call position
    4465              :          so automagic virtual operand updating doesn't work.  */
    4466          934 :       if (gimple_vuse (stmt))
    4467          139 :         vinfo->any_known_not_updated_vssa = true;
    4468              : 
    4469          467 :       data.clone = bestn;
    4470          467 :       data.clone_inbranch = bestn_inbranch;
    4471              : 
    4472          467 :       simd_clone_info.safe_push (NULL_TREE);
    4473         1663 :       for (i = 0;
    4474         2502 :            i < (bestn_inbranch ? bestn_inbranch : bestn)->simdclone->nargs; i++)
    4475              :         {
    4476         1196 :           if (loop_vinfo
    4477         1190 :               && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
    4478          482 :               && (bestn_inbranch->simdclone->args[i].arg_type
    4479              :                   == SIMD_CLONE_ARG_TYPE_MASK))
    4480              :             {
    4481          174 :               if (masked_call_offset)
    4482              :                 /* When there is an explicit mask we require the
    4483              :                    number of elements to match up.  */
    4484           49 :                 vect_record_loop_mask (loop_vinfo,
    4485              :                                        &LOOP_VINFO_MASKS (loop_vinfo),
    4486              :                                        ncopies_in, vectype, NULL_TREE);
    4487              :               else
    4488              :                 {
    4489              :                   /* When there is no explicit mask on the call we have
    4490              :                      more relaxed requirements.  */
    4491          125 :                   tree masktype;
    4492          125 :                   poly_uint64 callee_nelements;
    4493          125 :                   if (SCALAR_INT_MODE_P (bestn_inbranch->simdclone->mask_mode))
    4494              :                     {
    4495           12 :                       callee_nelements
    4496           12 :                           = exact_div (bestn_inbranch->simdclone->simdlen,
    4497              :                                        bestn_inbranch->simdclone->args[i].linear_step);
    4498           12 :                       masktype = get_related_vectype_for_scalar_type
    4499           12 :                           (vinfo->vector_mode, TREE_TYPE (vectype),
    4500              :                            callee_nelements);
    4501              :                     }
    4502              :                   else
    4503              :                     {
    4504          113 :                       masktype = bestn_inbranch->simdclone->args[i].vector_type;
    4505              :                       /* The aarch64 port will add custom attributes to types
    4506              :                          for SVE simdclones which make the types different.  We
    4507              :                          should use canonincal types for masks within the
    4508              :                          vectorizer, hence we construct the related vectype
    4509              :                          here.  */
    4510          113 :                       masktype
    4511              :                         = build_truth_vector_type_for_mode
    4512          113 :                           (TYPE_VECTOR_SUBPARTS (masktype),
    4513          113 :                            TYPE_MODE (masktype));
    4514          113 :                       callee_nelements = TYPE_VECTOR_SUBPARTS (masktype);
    4515              :                     }
    4516          125 :                   auto o = vector_unroll_factor (nunits, callee_nelements);
    4517          125 :                   vect_record_loop_mask (loop_vinfo,
    4518              :                                          &LOOP_VINFO_MASKS (loop_vinfo),
    4519              :                                          ncopies  * o, masktype, NULL_TREE);
    4520              :                 }
    4521              :             }
    4522         1022 :           else if ((bestn->simdclone->args[i].arg_type
    4523              :                     == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
    4524          915 :                    || (bestn->simdclone->args[i].arg_type
    4525              :                        == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP)
    4526          904 :                    || (bestn_inbranch
    4527          364 :                        && ((bestn_inbranch->simdclone->args[i].arg_type
    4528              :                             == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
    4529          364 :                            || (bestn_inbranch->simdclone->args[i].arg_type
    4530              :                                == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP))))
    4531              :             {
    4532          118 :               simd_clone_info.safe_grow_cleared (i * 3 + 1, true);
    4533          118 :               simd_clone_info.safe_push (arginfo[i].op);
    4534          202 :               tree lst = (POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
    4535          202 :                           ? size_type_node : TREE_TYPE (arginfo[i].op));
    4536          118 :               tree ls = build_int_cst (lst, arginfo[i].linear_step);
    4537          118 :               simd_clone_info.safe_push (ls);
    4538          118 :               tree sll = (arginfo[i].simd_lane_linear
    4539          118 :                           ? boolean_true_node : boolean_false_node);
    4540          118 :               simd_clone_info.safe_push (sll);
    4541              :             }
    4542              :         }
    4543              : 
    4544          467 :       SLP_TREE_TYPE (slp_node) = call_simd_clone_vec_info_type;
    4545          467 :       slp_node->data = new vect_simd_clone_data (std::move (_data));
    4546          467 :       DUMP_VECT_SCOPE ("vectorizable_simd_clone_call");
    4547              :       /* ???  We're confused by calls w/o LHS.  */
    4548          467 :       if (SLP_TREE_VECTYPE (slp_node))
    4549          461 :         vect_model_simple_cost (vinfo, ncopies, slp_node, cost_vec);
    4550          467 :       return true;
    4551              :     }
    4552              : 
    4553              :   /* Transform.  */
    4554              : 
    4555          362 :   if (dump_enabled_p ())
    4556          246 :     dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
    4557              : 
    4558              :   /* Handle def.  */
    4559          362 :   scalar_dest = gimple_call_lhs (stmt);
    4560          362 :   vec_dest = NULL_TREE;
    4561          362 :   rtype = NULL_TREE;
    4562          362 :   ratype = NULL_TREE;
    4563          362 :   if (scalar_dest)
    4564              :     {
    4565          356 :       vec_dest = vect_create_destination_var (scalar_dest, vectype);
    4566          356 :       rtype = TREE_TYPE (TREE_TYPE (fndecl));
    4567          356 :       if (TREE_CODE (rtype) == ARRAY_TYPE)
    4568              :         {
    4569            9 :           ratype = rtype;
    4570            9 :           rtype = TREE_TYPE (ratype);
    4571              :         }
    4572              :     }
    4573              : 
    4574          724 :   auto_vec<vec<tree> > vec_oprnds;
    4575          362 :   auto_vec<unsigned> vec_oprnds_i;
    4576          362 :   vec_oprnds_i.safe_grow_cleared (nargs, true);
    4577          362 :   vec_oprnds.reserve_exact (nargs);
    4578          362 :   vect_get_slp_defs (vinfo, slp_node, &vec_oprnds);
    4579          833 :   for (j = 0; j < ncopies; ++j)
    4580              :     {
    4581          471 :       poly_uint64 callee_nelements;
    4582          471 :       poly_uint64 caller_nelements;
    4583              :       /* Build argument list for the vectorized call.  */
    4584          471 :       if (j == 0)
    4585          362 :         vargs.create (nargs);
    4586              :       else
    4587          109 :         vargs.truncate (0);
    4588              : 
    4589         1580 :       for (i = 0; i < nargs; i++)
    4590              :         {
    4591         1109 :           unsigned int k, l, m, o;
    4592         1109 :           tree atype;
    4593         1109 :           tree op = gimple_call_arg (stmt, i + masked_call_offset);
    4594         1109 :           switch (bestn->simdclone->args[i].arg_type)
    4595              :             {
    4596          820 :             case SIMD_CLONE_ARG_TYPE_VECTOR:
    4597          820 :               atype = bestn->simdclone->args[i].vector_type;
    4598          820 :               caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
    4599          820 :               callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
    4600          820 :               o = vector_unroll_factor (nunits, callee_nelements);
    4601         1870 :               for (m = j * o; m < (j + 1) * o; m++)
    4602              :                 {
    4603         1050 :                   if (known_lt (callee_nelements, caller_nelements))
    4604              :                     {
    4605          516 :                       poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
    4606          258 :                       if (!constant_multiple_p (caller_nelements,
    4607              :                                                 callee_nelements, &k))
    4608            0 :                         gcc_unreachable ();
    4609              : 
    4610          258 :                       gcc_assert ((k & (k - 1)) == 0);
    4611          258 :                       if (m == 0)
    4612              :                         {
    4613           57 :                           vec_oprnds_i[i] = 0;
    4614           57 :                           vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
    4615              :                         }
    4616              :                       else
    4617              :                         {
    4618          201 :                           vec_oprnd0 = arginfo[i].op;
    4619          201 :                           if ((m & (k - 1)) == 0)
    4620           72 :                             vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
    4621              :                         }
    4622          258 :                       arginfo[i].op = vec_oprnd0;
    4623          258 :                       vec_oprnd0
    4624          258 :                         = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
    4625          258 :                                   bitsize_int (prec),
    4626          258 :                                   bitsize_int ((m & (k - 1)) * prec));
    4627          258 :                       gassign *new_stmt
    4628          258 :                         = gimple_build_assign (make_ssa_name (atype),
    4629              :                                                vec_oprnd0);
    4630          258 :                       vect_finish_stmt_generation (vinfo, stmt_info,
    4631              :                                                    new_stmt, gsi);
    4632          258 :                       vargs.safe_push (gimple_assign_lhs (new_stmt));
    4633              :                     }
    4634              :                   else
    4635              :                     {
    4636          792 :                       if (!constant_multiple_p (callee_nelements,
    4637              :                                                 caller_nelements, &k))
    4638            0 :                         gcc_unreachable ();
    4639          792 :                       gcc_assert ((k & (k - 1)) == 0);
    4640          792 :                       vec<constructor_elt, va_gc> *ctor_elts;
    4641          792 :                       if (k != 1)
    4642           14 :                         vec_alloc (ctor_elts, k);
    4643              :                       else
    4644          778 :                         ctor_elts = NULL;
    4645          820 :                       for (l = 0; l < k; l++)
    4646              :                         {
    4647          806 :                           if (m == 0 && l == 0)
    4648              :                             {
    4649          454 :                               vec_oprnds_i[i] = 0;
    4650          454 :                               vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
    4651              :                             }
    4652              :                           else
    4653          352 :                             vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
    4654          806 :                           arginfo[i].op = vec_oprnd0;
    4655          806 :                           if (k == 1)
    4656              :                             break;
    4657           28 :                           CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
    4658              :                                                   vec_oprnd0);
    4659              :                         }
    4660          792 :                       if (k == 1)
    4661          778 :                         if (!useless_type_conversion_p (TREE_TYPE (vec_oprnd0),
    4662              :                                                        atype))
    4663              :                           {
    4664            0 :                             vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, atype,
    4665              :                                                  vec_oprnd0);
    4666            0 :                             gassign *new_stmt
    4667            0 :                               = gimple_build_assign (make_ssa_name (atype),
    4668              :                                                      vec_oprnd0);
    4669            0 :                             vect_finish_stmt_generation (vinfo, stmt_info,
    4670              :                                                          new_stmt, gsi);
    4671            0 :                             vargs.safe_push (gimple_get_lhs (new_stmt));
    4672              :                           }
    4673              :                         else
    4674          778 :                           vargs.safe_push (vec_oprnd0);
    4675              :                       else
    4676              :                         {
    4677           14 :                           vec_oprnd0 = build_constructor (atype, ctor_elts);
    4678           14 :                           gassign *new_stmt
    4679           14 :                             = gimple_build_assign (make_ssa_name (atype),
    4680              :                                                    vec_oprnd0);
    4681           14 :                           vect_finish_stmt_generation (vinfo, stmt_info,
    4682              :                                                        new_stmt, gsi);
    4683           14 :                           vargs.safe_push (gimple_assign_lhs (new_stmt));
    4684              :                         }
    4685              :                     }
    4686              :                 }
    4687              :               break;
    4688           66 :             case SIMD_CLONE_ARG_TYPE_MASK:
    4689           66 :               if (bestn->simdclone->mask_mode == VOIDmode)
    4690              :                 {
    4691           60 :                   atype = bestn->simdclone->args[i].vector_type;
    4692           60 :                   tree elt_type = TREE_TYPE (atype);
    4693           60 :                   tree one = fold_convert (elt_type, integer_one_node);
    4694           60 :                   tree zero = fold_convert (elt_type, integer_zero_node);
    4695           60 :                   callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
    4696           60 :                   caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
    4697           60 :                   o = vector_unroll_factor (nunits, callee_nelements);
    4698          120 :                   for (m = j * o; m < (j + 1) * o; m++)
    4699              :                     {
    4700           60 :                       if (maybe_lt (callee_nelements, caller_nelements))
    4701              :                         {
    4702              :                           /* The mask type has fewer elements than simdlen.  */
    4703              : 
    4704              :                           /* FORNOW */
    4705            0 :                           gcc_unreachable ();
    4706              :                         }
    4707           60 :                       else if (known_eq (callee_nelements, caller_nelements))
    4708              :                         {
    4709              :                           /* The SIMD clone function has the same number of
    4710              :                              elements as the current function.  */
    4711           60 :                           if (m == 0)
    4712           60 :                             vec_oprnds_i[i] = 0;
    4713           60 :                           vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
    4714           60 :                           if (loop_vinfo
    4715           60 :                               && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
    4716              :                             {
    4717            0 :                               vec_loop_masks *loop_masks
    4718              :                                 = &LOOP_VINFO_MASKS (loop_vinfo);
    4719            0 :                               tree loop_mask
    4720            0 :                                 = vect_get_loop_mask (loop_vinfo, gsi,
    4721              :                                                       loop_masks, ncopies_in,
    4722            0 :                                                       vectype, j);
    4723            0 :                               vec_oprnd0
    4724            0 :                                 = prepare_vec_mask (loop_vinfo,
    4725            0 :                                                     TREE_TYPE (loop_mask),
    4726              :                                                     loop_mask, vec_oprnd0,
    4727              :                                                     gsi);
    4728            0 :                               loop_vinfo->vec_cond_masked_set.add ({ vec_oprnd0,
    4729              :                                                                      loop_mask });
    4730              : 
    4731              :                             }
    4732           60 :                           vec_oprnd0
    4733           60 :                             = build3 (VEC_COND_EXPR, atype, vec_oprnd0,
    4734              :                                       build_vector_from_val (atype, one),
    4735              :                                       build_vector_from_val (atype, zero));
    4736           60 :                           gassign *new_stmt
    4737           60 :                             = gimple_build_assign (make_ssa_name (atype),
    4738              :                                                    vec_oprnd0);
    4739           60 :                           vect_finish_stmt_generation (vinfo, stmt_info,
    4740              :                                                        new_stmt, gsi);
    4741           60 :                           vargs.safe_push (gimple_assign_lhs (new_stmt));
    4742              :                         }
    4743              :                       else
    4744              :                         {
    4745              :                           /* The mask type has more elements than simdlen.  */
    4746              : 
    4747              :                           /* FORNOW */
    4748            0 :                           gcc_unreachable ();
    4749              :                         }
    4750              :                     }
    4751              :                 }
    4752            6 :               else if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
    4753              :                 {
    4754            6 :                   atype = bestn->simdclone->args[i].vector_type;
    4755            6 :                   poly_uint64 atype_subparts
    4756            6 :                     = exact_div (bestn->simdclone->simdlen,
    4757              :                                  bestn->simdclone->args[i].linear_step);
    4758            6 :                   o = bestn->simdclone->args[i].linear_step;
    4759           12 :                   for (m = j * o; m < (j + 1) * o; m++)
    4760              :                     {
    4761            6 :                       if (m == 0)
    4762            6 :                         vec_oprnds_i[i] = 0;
    4763            6 :                       if (maybe_lt (atype_subparts,
    4764            6 :                                     TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
    4765              :                         {
    4766              :                           /* The mask argument has fewer elements than the
    4767              :                              input vector.  */
    4768              :                           /* FORNOW */
    4769            0 :                           gcc_unreachable ();
    4770              :                         }
    4771            6 :                       else if (known_eq (atype_subparts,
    4772              :                                          TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
    4773              :                         {
    4774            6 :                           vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
    4775            6 :                           if (loop_vinfo
    4776            6 :                               && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
    4777              :                             {
    4778            1 :                               vec_loop_masks *loop_masks
    4779              :                                 = &LOOP_VINFO_MASKS (loop_vinfo);
    4780            1 :                               tree loop_mask
    4781            1 :                                 = vect_get_loop_mask (loop_vinfo, gsi,
    4782              :                                                       loop_masks, ncopies_in,
    4783              :                                                       vectype, j);
    4784            1 :                               vec_oprnd0
    4785            1 :                                 = prepare_vec_mask (loop_vinfo,
    4786            1 :                                                     TREE_TYPE (loop_mask),
    4787              :                                                     loop_mask, vec_oprnd0,
    4788              :                                                     gsi);
    4789              :                             }
    4790              :                           /* The vector mask argument matches the input
    4791              :                              in the number of lanes, but not necessarily
    4792              :                              in the mode.  */
    4793            6 :                           tree st = lang_hooks.types.type_for_mode
    4794            6 :                                       (TYPE_MODE (TREE_TYPE (vec_oprnd0)), 1);
    4795            6 :                           vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, st,
    4796              :                                                vec_oprnd0);
    4797            6 :                           gassign *new_stmt
    4798            6 :                             = gimple_build_assign (make_ssa_name (st),
    4799              :                                                    vec_oprnd0);
    4800            6 :                           vect_finish_stmt_generation (vinfo, stmt_info,
    4801              :                                                        new_stmt, gsi);
    4802            6 :                           if (!types_compatible_p (atype, st))
    4803              :                             {
    4804            6 :                               new_stmt
    4805            6 :                                 = gimple_build_assign (make_ssa_name (atype),
    4806              :                                                        NOP_EXPR,
    4807              :                                                        gimple_assign_lhs
    4808              :                                                          (new_stmt));
    4809            6 :                               vect_finish_stmt_generation (vinfo, stmt_info,
    4810              :                                                            new_stmt, gsi);
    4811              :                             }
    4812            6 :                           vargs.safe_push (gimple_assign_lhs (new_stmt));
    4813              :                         }
    4814              :                       else
    4815              :                         {
    4816              :                           /* The mask argument has more elements than the
    4817              :                              input vector.  */
    4818              :                           /* FORNOW */
    4819            0 :                           gcc_unreachable ();
    4820              :                         }
    4821              :                     }
    4822              :                 }
    4823              :               else
    4824            0 :                 gcc_unreachable ();
    4825              :               break;
    4826          102 :             case SIMD_CLONE_ARG_TYPE_UNIFORM:
    4827          102 :               vargs.safe_push (op);
    4828          102 :               break;
    4829          121 :             case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
    4830          121 :             case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
    4831          121 :               if (j == 0)
    4832              :                 {
    4833          118 :                   gimple_seq stmts;
    4834          118 :                   arginfo[i].op
    4835          118 :                     = force_gimple_operand (unshare_expr (arginfo[i].op),
    4836              :                                             &stmts, true, NULL_TREE);
    4837          118 :                   if (stmts != NULL)
    4838              :                     {
    4839            0 :                       basic_block new_bb;
    4840            0 :                       edge pe = loop_preheader_edge (loop);
    4841            0 :                       new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
    4842            0 :                       gcc_assert (!new_bb);
    4843              :                     }
    4844          118 :                   if (arginfo[i].simd_lane_linear)
    4845              :                     {
    4846            6 :                       vargs.safe_push (arginfo[i].op);
    4847            6 :                       break;
    4848              :                     }
    4849          112 :                   tree phi_res = copy_ssa_name (op);
    4850          112 :                   gphi *new_phi = create_phi_node (phi_res, loop->header);
    4851          112 :                   add_phi_arg (new_phi, arginfo[i].op,
    4852              :                                loop_preheader_edge (loop), UNKNOWN_LOCATION);
    4853          112 :                   enum tree_code code
    4854          196 :                     = POINTER_TYPE_P (TREE_TYPE (op))
    4855          112 :                       ? POINTER_PLUS_EXPR : PLUS_EXPR;
    4856          196 :                   tree type = POINTER_TYPE_P (TREE_TYPE (op))
    4857          196 :                               ? sizetype : TREE_TYPE (op);
    4858          112 :                   poly_widest_int cst
    4859          112 :                     = wi::mul (bestn->simdclone->args[i].linear_step,
    4860          112 :                                ncopies * nunits);
    4861          112 :                   tree tcst = wide_int_to_tree (type, cst);
    4862          112 :                   tree phi_arg = copy_ssa_name (op);
    4863          112 :                   gassign *new_stmt
    4864          112 :                     = gimple_build_assign (phi_arg, code, phi_res, tcst);
    4865          112 :                   gimple_stmt_iterator si = gsi_after_labels (loop->header);
    4866          112 :                   gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
    4867          112 :                   add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
    4868              :                                UNKNOWN_LOCATION);
    4869          112 :                   arginfo[i].op = phi_res;
    4870          112 :                   vargs.safe_push (phi_res);
    4871          112 :                 }
    4872              :               else
    4873              :                 {
    4874            3 :                   enum tree_code code
    4875            6 :                     = POINTER_TYPE_P (TREE_TYPE (op))
    4876            3 :                       ? POINTER_PLUS_EXPR : PLUS_EXPR;
    4877            6 :                   tree type = POINTER_TYPE_P (TREE_TYPE (op))
    4878            6 :                               ? sizetype : TREE_TYPE (op);
    4879            3 :                   poly_widest_int cst
    4880            3 :                     = wi::mul (bestn->simdclone->args[i].linear_step,
    4881            3 :                                j * nunits);
    4882            3 :                   tree tcst = wide_int_to_tree (type, cst);
    4883            3 :                   new_temp = make_ssa_name (TREE_TYPE (op));
    4884            3 :                   gassign *new_stmt
    4885            6 :                     = gimple_build_assign (new_temp, code,
    4886            3 :                                            arginfo[i].op, tcst);
    4887            3 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    4888            3 :                   vargs.safe_push (new_temp);
    4889            3 :                 }
    4890              :               break;
    4891            0 :             case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
    4892            0 :             case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
    4893            0 :             case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
    4894            0 :             case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
    4895            0 :             case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
    4896            0 :             case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
    4897            0 :             default:
    4898            0 :               gcc_unreachable ();
    4899              :             }
    4900              :         }
    4901              : 
    4902          471 :       if (masked_call_offset == 0
    4903          405 :           && bestn->simdclone->inbranch
    4904           13 :           && bestn->simdclone->nargs > nargs)
    4905              :         {
    4906           13 :           unsigned long m, o;
    4907           13 :           size_t mask_i = bestn->simdclone->nargs - 1;
    4908           13 :           tree mask;
    4909           13 :           gcc_assert (bestn->simdclone->args[mask_i].arg_type ==
    4910              :                       SIMD_CLONE_ARG_TYPE_MASK);
    4911              : 
    4912           13 :           tree mask_argtype = bestn->simdclone->args[mask_i].vector_type;
    4913           13 :           tree mask_vectype;
    4914           13 :           if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
    4915              :             {
    4916            2 :               callee_nelements = exact_div (bestn->simdclone->simdlen,
    4917              :                                             bestn->simdclone->args[i].linear_step);
    4918            2 :               mask_vectype = get_related_vectype_for_scalar_type
    4919            2 :                   (vinfo->vector_mode, TREE_TYPE (vectype), callee_nelements);
    4920              :             }
    4921              :           else
    4922              :             {
    4923           11 :               mask_vectype = mask_argtype;
    4924           11 :               callee_nelements = TYPE_VECTOR_SUBPARTS (mask_vectype);
    4925              :             }
    4926           13 :           o = vector_unroll_factor (nunits, callee_nelements);
    4927           26 :           for (m = j * o; m < (j + 1) * o; m++)
    4928              :             {
    4929           13 :               if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
    4930              :                 {
    4931            1 :                   vec_loop_masks *loop_masks = &LOOP_VINFO_MASKS (loop_vinfo);
    4932            1 :                   mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
    4933              :                                              ncopies * o, mask_vectype, m);
    4934              :                 }
    4935              :               else
    4936           12 :                 mask = vect_build_all_ones_mask (vinfo, stmt_info,
    4937              :                                                  mask_argtype);
    4938              : 
    4939           13 :               gassign *new_stmt;
    4940           13 :               if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
    4941              :                 {
    4942              :                   /* This means we are dealing with integer mask modes.
    4943              :                      First convert to an integer type with the same size as
    4944              :                      the current vector type.  */
    4945            2 :                   unsigned HOST_WIDE_INT intermediate_size
    4946            2 :                       = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (mask)));
    4947            2 :                   tree mid_int_type =
    4948            2 :                       build_nonstandard_integer_type (intermediate_size, 1);
    4949            2 :                   mask = build1 (VIEW_CONVERT_EXPR, mid_int_type, mask);
    4950            2 :                   new_stmt
    4951            2 :                       = gimple_build_assign (make_ssa_name (mid_int_type),
    4952              :                                              mask);
    4953            2 :                   gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    4954              :                   /* Then zero-extend to the mask mode.  */
    4955            2 :                   mask = fold_build1 (NOP_EXPR, mask_argtype,
    4956              :                                       gimple_get_lhs (new_stmt));
    4957              :                 }
    4958           11 :               else if (bestn->simdclone->mask_mode == VOIDmode)
    4959           11 :                 mask = build3 (VEC_COND_EXPR, mask_argtype, mask,
    4960              :                                build_one_cst (mask_argtype),
    4961              :                                build_zero_cst (mask_argtype));
    4962              :               else
    4963            0 :                 gcc_unreachable ();
    4964              : 
    4965           13 :               new_stmt = gimple_build_assign (make_ssa_name (mask_argtype),
    4966              :                                               mask);
    4967           13 :               vect_finish_stmt_generation (vinfo, stmt_info,
    4968              :                                            new_stmt, gsi);
    4969           13 :               mask = gimple_assign_lhs (new_stmt);
    4970           13 :               vargs.safe_push (mask);
    4971              :             }
    4972              :         }
    4973              : 
    4974          471 :       gcall *new_call = gimple_build_call_vec (fndecl, vargs);
    4975          471 :       if (vec_dest)
    4976              :         {
    4977          465 :           gcc_assert (ratype
    4978              :                       || known_eq (TYPE_VECTOR_SUBPARTS (rtype), nunits));
    4979          465 :           if (ratype)
    4980           15 :             new_temp = create_tmp_var (ratype);
    4981          450 :           else if (useless_type_conversion_p (vectype, rtype))
    4982          428 :             new_temp = make_ssa_name (vec_dest, new_call);
    4983              :           else
    4984           22 :             new_temp = make_ssa_name (rtype, new_call);
    4985          465 :           gimple_call_set_lhs (new_call, new_temp);
    4986              :         }
    4987          471 :       vect_finish_stmt_generation (vinfo, stmt_info, new_call, gsi);
    4988          471 :       gimple *new_stmt = new_call;
    4989              : 
    4990          471 :       if (vec_dest)
    4991              :         {
    4992          465 :           if (!multiple_p (TYPE_VECTOR_SUBPARTS (vectype), nunits))
    4993              :             {
    4994           21 :               unsigned int k, l;
    4995           42 :               poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
    4996           42 :               poly_uint64 bytes = GET_MODE_SIZE (TYPE_MODE (vectype));
    4997           21 :               k = vector_unroll_factor (nunits,
    4998              :                                         TYPE_VECTOR_SUBPARTS (vectype));
    4999           21 :               gcc_assert ((k & (k - 1)) == 0);
    5000           75 :               for (l = 0; l < k; l++)
    5001              :                 {
    5002           54 :                   tree t;
    5003           54 :                   if (ratype)
    5004              :                     {
    5005           42 :                       t = build_fold_addr_expr (new_temp);
    5006           42 :                       t = build2 (MEM_REF, vectype, t,
    5007           42 :                                   build_int_cst (TREE_TYPE (t), l * bytes));
    5008              :                     }
    5009              :                   else
    5010           12 :                     t = build3 (BIT_FIELD_REF, vectype, new_temp,
    5011           12 :                                 bitsize_int (prec), bitsize_int (l * prec));
    5012           54 :                   new_stmt = gimple_build_assign (make_ssa_name (vectype), t);
    5013           54 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5014              : 
    5015           54 :                   SLP_TREE_VEC_DEFS (slp_node)
    5016           54 :                     .quick_push (gimple_assign_lhs (new_stmt));
    5017              :                 }
    5018              : 
    5019           21 :               if (ratype)
    5020           15 :                 vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
    5021           21 :               continue;
    5022           21 :             }
    5023          444 :           else if (!multiple_p (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
    5024              :             {
    5025           16 :               unsigned int k;
    5026           16 :               if (!constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
    5027           16 :                                         TYPE_VECTOR_SUBPARTS (rtype), &k))
    5028            0 :                 gcc_unreachable ();
    5029           16 :               gcc_assert ((k & (k - 1)) == 0);
    5030           16 :               if ((j & (k - 1)) == 0)
    5031            8 :                 vec_alloc (ret_ctor_elts, k);
    5032           16 :               if (ratype)
    5033              :                 {
    5034            0 :                   unsigned int m, o;
    5035            0 :                   o = vector_unroll_factor (nunits,
    5036              :                                             TYPE_VECTOR_SUBPARTS (rtype));
    5037            0 :                   for (m = 0; m < o; m++)
    5038              :                     {
    5039            0 :                       tree tem = build4 (ARRAY_REF, rtype, new_temp,
    5040            0 :                                          size_int (m), NULL_TREE, NULL_TREE);
    5041            0 :                       new_stmt = gimple_build_assign (make_ssa_name (rtype),
    5042              :                                                       tem);
    5043            0 :                       vect_finish_stmt_generation (vinfo, stmt_info,
    5044              :                                                    new_stmt, gsi);
    5045            0 :                       CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
    5046              :                                               gimple_assign_lhs (new_stmt));
    5047              :                     }
    5048            0 :                   vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
    5049              :                 }
    5050              :               else
    5051           16 :                 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
    5052           16 :               if ((j & (k - 1)) != k - 1)
    5053            8 :                 continue;
    5054            8 :               vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
    5055            8 :               new_stmt
    5056            8 :                 = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
    5057            8 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5058              : 
    5059            8 :               SLP_TREE_VEC_DEFS (slp_node)
    5060            8 :                 .quick_push (gimple_assign_lhs (new_stmt));
    5061            8 :               continue;
    5062            8 :             }
    5063          428 :           else if (ratype)
    5064              :             {
    5065            0 :               tree t = build_fold_addr_expr (new_temp);
    5066            0 :               t = build2 (MEM_REF, vectype, t,
    5067            0 :                           build_int_cst (TREE_TYPE (t), 0));
    5068            0 :               new_stmt = gimple_build_assign (make_ssa_name (vec_dest), t);
    5069            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5070            0 :               vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
    5071              :             }
    5072          428 :           else if (!useless_type_conversion_p (vectype, rtype))
    5073              :             {
    5074            0 :               vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, vectype, new_temp);
    5075            0 :               new_stmt
    5076            0 :                 = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
    5077            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5078              :             }
    5079              :         }
    5080              : 
    5081          434 :       if (gimple_get_lhs (new_stmt))
    5082          428 :         SLP_TREE_VEC_DEFS (slp_node).quick_push (gimple_get_lhs (new_stmt));
    5083              :     }
    5084              : 
    5085         1159 :   for (i = 0; i < nargs; ++i)
    5086              :     {
    5087          797 :       vec<tree> oprndsi = vec_oprnds[i];
    5088          797 :       oprndsi.release ();
    5089              :     }
    5090          362 :   vargs.release ();
    5091              : 
    5092              :   /* Mark the clone as no longer being a candidate for GC.  */
    5093          362 :   bestn->gc_candidate = false;
    5094              : 
    5095          362 :   return true;
    5096         1472 : }
    5097              : 
    5098              : 
    5099              : /* Function vect_gen_widened_results_half
    5100              : 
    5101              :    Create a vector stmt whose code, type, number of arguments, and result
    5102              :    variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
    5103              :    VEC_OPRND0 and VEC_OPRND1.  The new vector stmt is to be inserted at GSI.
    5104              :    In the case that CODE is a CALL_EXPR, this means that a call to DECL
    5105              :    needs to be created (DECL is a function-decl of a target-builtin).
    5106              :    STMT_INFO is the original scalar stmt that we are vectorizing.  */
    5107              : 
    5108              : static gimple *
    5109        31674 : vect_gen_widened_results_half (vec_info *vinfo, code_helper ch,
    5110              :                                tree vec_oprnd0, tree vec_oprnd1, int op_type,
    5111              :                                tree vec_dest, gimple_stmt_iterator *gsi,
    5112              :                                stmt_vec_info stmt_info)
    5113              : {
    5114        31674 :   gimple *new_stmt;
    5115        31674 :   tree new_temp;
    5116              : 
    5117              :   /* Generate half of the widened result:  */
    5118        31674 :   if (op_type != binary_op)
    5119        30564 :     vec_oprnd1 = NULL;
    5120        31674 :   new_stmt = vect_gimple_build (vec_dest, ch, vec_oprnd0, vec_oprnd1);
    5121        31674 :   new_temp = make_ssa_name (vec_dest, new_stmt);
    5122        31674 :   gimple_set_lhs (new_stmt, new_temp);
    5123        31674 :   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5124              : 
    5125        31674 :   return new_stmt;
    5126              : }
    5127              : 
    5128              : 
    5129              : /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
    5130              :    For multi-step conversions store the resulting vectors and call the function
    5131              :    recursively. When NARROW_SRC_P is true, there's still a conversion after
    5132              :    narrowing, don't store the vectors in the SLP_NODE or in vector info of
    5133              :    the scalar statement(or in STMT_VINFO_RELATED_STMT chain).  */
    5134              : 
    5135              : static void
    5136        12038 : vect_create_vectorized_demotion_stmts (vec_info *vinfo, vec<tree> *vec_oprnds,
    5137              :                                        int multi_step_cvt,
    5138              :                                        stmt_vec_info stmt_info,
    5139              :                                        vec<tree> &vec_dsts,
    5140              :                                        gimple_stmt_iterator *gsi,
    5141              :                                        slp_tree slp_node, code_helper code,
    5142              :                                        bool narrow_src_p)
    5143              : {
    5144        12038 :   unsigned int i;
    5145        12038 :   tree vop0, vop1, new_tmp, vec_dest;
    5146              : 
    5147        12038 :   vec_dest = vec_dsts.pop ();
    5148              : 
    5149        28485 :   for (i = 0; i < vec_oprnds->length (); i += 2)
    5150              :     {
    5151              :       /* Create demotion operation.  */
    5152        16447 :       vop0 = (*vec_oprnds)[i];
    5153        16447 :       vop1 = (*vec_oprnds)[i + 1];
    5154        16447 :       gimple *new_stmt = vect_gimple_build (vec_dest, code, vop0, vop1);
    5155        16447 :       new_tmp = make_ssa_name (vec_dest, new_stmt);
    5156        16447 :       gimple_set_lhs (new_stmt, new_tmp);
    5157        16447 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5158        16447 :       if (multi_step_cvt || narrow_src_p)
    5159              :         /* Store the resulting vector for next recursive call,
    5160              :            or return the resulting vector_tmp for NARROW FLOAT_EXPR.  */
    5161         6745 :         (*vec_oprnds)[i/2] = new_tmp;
    5162              :       else
    5163              :         {
    5164              :           /* This is the last step of the conversion sequence. Store the
    5165              :              vectors in SLP_NODE.  */
    5166         9702 :           slp_node->push_vec_def (new_stmt);
    5167              :         }
    5168              :     }
    5169              : 
    5170              :   /* For multi-step demotion operations we first generate demotion operations
    5171              :      from the source type to the intermediate types, and then combine the
    5172              :      results (stored in VEC_OPRNDS) in demotion operation to the destination
    5173              :      type.  */
    5174        12038 :   if (multi_step_cvt)
    5175              :     {
    5176              :       /* At each level of recursion we have half of the operands we had at the
    5177              :          previous level.  */
    5178         2998 :       vec_oprnds->truncate ((i+1)/2);
    5179         2998 :       vect_create_vectorized_demotion_stmts (vinfo, vec_oprnds,
    5180              :                                              multi_step_cvt - 1,
    5181              :                                              stmt_info, vec_dsts, gsi,
    5182         2998 :                                              slp_node, VEC_PACK_TRUNC_EXPR,
    5183              :                                              narrow_src_p);
    5184              :     }
    5185              : 
    5186        12038 :   vec_dsts.quick_push (vec_dest);
    5187        12038 : }
    5188              : 
    5189              : 
    5190              : /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
    5191              :    and VEC_OPRNDS1, for a binary operation associated with scalar statement
    5192              :    STMT_INFO.  For multi-step conversions store the resulting vectors and
    5193              :    call the function recursively.  */
    5194              : 
    5195              : static void
    5196        11538 : vect_create_vectorized_promotion_stmts (vec_info *vinfo,
    5197              :                                         vec<tree> *vec_oprnds0,
    5198              :                                         vec<tree> *vec_oprnds1,
    5199              :                                         stmt_vec_info stmt_info, tree vec_dest,
    5200              :                                         gimple_stmt_iterator *gsi,
    5201              :                                         code_helper ch1,
    5202              :                                         code_helper ch2, int op_type)
    5203              : {
    5204        11538 :   int i;
    5205        11538 :   tree vop0, vop1, new_tmp1, new_tmp2;
    5206        11538 :   gimple *new_stmt1, *new_stmt2;
    5207        11538 :   vec<tree> vec_tmp = vNULL;
    5208              : 
    5209        11538 :   vec_tmp.create (vec_oprnds0->length () * 2);
    5210        38913 :   FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
    5211              :     {
    5212        15837 :       if (op_type == binary_op)
    5213          555 :         vop1 = (*vec_oprnds1)[i];
    5214              :       else
    5215              :         vop1 = NULL_TREE;
    5216              : 
    5217              :       /* Generate the two halves of promotion operation.  */
    5218        15837 :       new_stmt1 = vect_gen_widened_results_half (vinfo, ch1, vop0, vop1,
    5219              :                                                  op_type, vec_dest, gsi,
    5220              :                                                  stmt_info);
    5221        15837 :       new_stmt2 = vect_gen_widened_results_half (vinfo, ch2, vop0, vop1,
    5222              :                                                  op_type, vec_dest, gsi,
    5223              :                                                  stmt_info);
    5224        15837 :       if (is_gimple_call (new_stmt1))
    5225              :         {
    5226            0 :           new_tmp1 = gimple_call_lhs (new_stmt1);
    5227            0 :           new_tmp2 = gimple_call_lhs (new_stmt2);
    5228              :         }
    5229              :       else
    5230              :         {
    5231        15837 :           new_tmp1 = gimple_assign_lhs (new_stmt1);
    5232        15837 :           new_tmp2 = gimple_assign_lhs (new_stmt2);
    5233              :         }
    5234              : 
    5235              :       /* Store the results for the next step.  */
    5236        15837 :       vec_tmp.quick_push (new_tmp1);
    5237        15837 :       vec_tmp.quick_push (new_tmp2);
    5238              :     }
    5239              : 
    5240        11538 :   vec_oprnds0->release ();
    5241        11538 :   *vec_oprnds0 = vec_tmp;
    5242        11538 : }
    5243              : 
    5244              : /* Create vectorized promotion stmts for widening stmts using only half the
    5245              :    potential vector size for input.  */
    5246              : static void
    5247           14 : vect_create_half_widening_stmts (vec_info *vinfo,
    5248              :                                         vec<tree> *vec_oprnds0,
    5249              :                                         vec<tree> *vec_oprnds1,
    5250              :                                         stmt_vec_info stmt_info, tree vec_dest,
    5251              :                                         gimple_stmt_iterator *gsi,
    5252              :                                         code_helper code1,
    5253              :                                         int op_type)
    5254              : {
    5255           14 :   int i;
    5256           14 :   tree vop0, vop1;
    5257           14 :   gimple *new_stmt1;
    5258           14 :   gimple *new_stmt2;
    5259           14 :   gimple *new_stmt3;
    5260           14 :   vec<tree> vec_tmp = vNULL;
    5261              : 
    5262           14 :   vec_tmp.create (vec_oprnds0->length ());
    5263           28 :   FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
    5264              :     {
    5265           14 :       tree new_tmp1, new_tmp2, new_tmp3, out_type;
    5266              : 
    5267           14 :       gcc_assert (op_type == binary_op);
    5268           14 :       vop1 = (*vec_oprnds1)[i];
    5269              : 
    5270              :       /* Widen the first vector input.  */
    5271           14 :       out_type = TREE_TYPE (vec_dest);
    5272           14 :       new_tmp1 = make_ssa_name (out_type);
    5273           14 :       new_stmt1 = gimple_build_assign (new_tmp1, NOP_EXPR, vop0);
    5274           14 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt1, gsi);
    5275           14 :       if (VECTOR_TYPE_P (TREE_TYPE (vop1)))
    5276              :         {
    5277              :           /* Widen the second vector input.  */
    5278           14 :           new_tmp2 = make_ssa_name (out_type);
    5279           14 :           new_stmt2 = gimple_build_assign (new_tmp2, NOP_EXPR, vop1);
    5280           14 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt2, gsi);
    5281              :           /* Perform the operation.  With both vector inputs widened.  */
    5282           14 :           new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, new_tmp2);
    5283              :         }
    5284              :       else
    5285              :         {
    5286              :           /* Perform the operation.  With the single vector input widened.  */
    5287            0 :           new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, vop1);
    5288              :         }
    5289              : 
    5290           14 :       new_tmp3 = make_ssa_name (vec_dest, new_stmt3);
    5291           14 :       gimple_assign_set_lhs (new_stmt3, new_tmp3);
    5292           14 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt3, gsi);
    5293              : 
    5294              :       /* Store the results for the next step.  */
    5295           14 :       vec_tmp.quick_push (new_tmp3);
    5296              :     }
    5297              : 
    5298           14 :   vec_oprnds0->release ();
    5299           14 :   *vec_oprnds0 = vec_tmp;
    5300           14 : }
    5301              : 
    5302              : 
    5303              : /* Check if STMT_INFO performs a conversion operation that can be vectorized.
    5304              :    If COST_VEC is passed, calculate costs but don't change anything,
    5305              :    otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
    5306              :    it, and insert it at GSI.
    5307              :    Return true if STMT_INFO is vectorizable in this way.  */
    5308              : 
    5309              : static bool
    5310      2712005 : vectorizable_conversion (vec_info *vinfo,
    5311              :                          stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
    5312              :                          slp_tree slp_node,
    5313              :                          stmt_vector_for_cost *cost_vec)
    5314              : {
    5315      2712005 :   tree vec_dest, cvt_op = NULL_TREE;
    5316      2712005 :   tree scalar_dest;
    5317      2712005 :   tree op0, op1 = NULL_TREE;
    5318      2712005 :   tree_code tc1;
    5319      2712005 :   code_helper code, code1, code2;
    5320      2712005 :   code_helper codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
    5321      2712005 :   tree new_temp;
    5322      2712005 :   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
    5323      2712005 :   poly_uint64 nunits_in;
    5324      2712005 :   poly_uint64 nunits_out;
    5325      2712005 :   tree vectype_out, vectype_in;
    5326      2712005 :   int i;
    5327      2712005 :   tree lhs_type, rhs_type;
    5328              :   /* For conversions between floating point and integer, there're 2 NARROW
    5329              :      cases. NARROW_SRC is for FLOAT_EXPR, means
    5330              :      integer --DEMOTION--> integer --FLOAT_EXPR--> floating point.
    5331              :      This is safe when the range of the source integer can fit into the lower
    5332              :      precision. NARROW_DST is for FIX_TRUNC_EXPR, means
    5333              :      floating point --FIX_TRUNC_EXPR--> integer --DEMOTION--> INTEGER.
    5334              :      For other conversions, when there's narrowing, NARROW_DST is used as
    5335              :      default.  */
    5336      2712005 :   enum { NARROW_SRC, NARROW_DST, NONE, WIDEN } modifier;
    5337      2712005 :   vec<tree> vec_oprnds0 = vNULL;
    5338      2712005 :   vec<tree> vec_oprnds1 = vNULL;
    5339      2712005 :   tree vop0;
    5340      2712005 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
    5341      2712005 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    5342      2712005 :   int multi_step_cvt = 0;
    5343      2712005 :   vec<tree> interm_types = vNULL;
    5344      2712005 :   tree intermediate_type, cvt_type = NULL_TREE;
    5345      2712005 :   int op_type;
    5346      2712005 :   unsigned short fltsz;
    5347              : 
    5348              :   /* Is STMT a vectorizable conversion?   */
    5349              : 
    5350      2712005 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
    5351              :     return false;
    5352              : 
    5353      2712005 :   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
    5354       234770 :       && cost_vec)
    5355              :     return false;
    5356              : 
    5357      2477235 :   gimple* stmt = stmt_info->stmt;
    5358      2477235 :   if (!(is_gimple_assign (stmt) || is_gimple_call (stmt)))
    5359              :     return false;
    5360              : 
    5361      2418108 :   if (gimple_get_lhs (stmt) == NULL_TREE
    5362      2418108 :       || TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
    5363       818094 :     return false;
    5364              : 
    5365      1600014 :   if (TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
    5366              :     return false;
    5367              : 
    5368      1600014 :   if (is_gimple_assign (stmt))
    5369              :     {
    5370      1588025 :       code = gimple_assign_rhs_code (stmt);
    5371      1588025 :       op_type = TREE_CODE_LENGTH ((tree_code) code);
    5372              :     }
    5373        11989 :   else if (gimple_call_internal_p (stmt))
    5374              :     {
    5375         7864 :       code = gimple_call_internal_fn (stmt);
    5376         7864 :       op_type = gimple_call_num_args (stmt);
    5377              :     }
    5378              :   else
    5379              :     return false;
    5380              : 
    5381      1595889 :   bool widen_arith = (code == WIDEN_MULT_EXPR
    5382      1593559 :                  || code == WIDEN_LSHIFT_EXPR
    5383      3189448 :                  || widening_fn_p (code));
    5384              : 
    5385      1593559 :   if (!widen_arith
    5386      1593559 :       && !CONVERT_EXPR_CODE_P (code)
    5387      1430971 :       && code != FIX_TRUNC_EXPR
    5388      1429235 :       && code != FLOAT_EXPR)
    5389              :     return false;
    5390              : 
    5391              :   /* Check types of lhs and rhs.  */
    5392       184899 :   scalar_dest = gimple_get_lhs (stmt);
    5393       184899 :   lhs_type = TREE_TYPE (scalar_dest);
    5394       184899 :   vectype_out = SLP_TREE_VECTYPE (slp_node);
    5395              : 
    5396              :   /* Check the operands of the operation.  */
    5397       184899 :   slp_tree slp_op0, slp_op1 = NULL;
    5398       184899 :   if (!vect_is_simple_use (vinfo, slp_node,
    5399              :                            0, &op0, &slp_op0, &dt[0], &vectype_in))
    5400              :     {
    5401            0 :       if (dump_enabled_p ())
    5402            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    5403              :                          "use not simple.\n");
    5404            0 :       return false;
    5405              :     }
    5406              : 
    5407       184899 :   rhs_type = TREE_TYPE (op0);
    5408       183163 :   if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
    5409       353247 :       && !((INTEGRAL_TYPE_P (lhs_type)
    5410       154974 :             && INTEGRAL_TYPE_P (rhs_type))
    5411              :            || (SCALAR_FLOAT_TYPE_P (lhs_type)
    5412         8823 :                && SCALAR_FLOAT_TYPE_P (rhs_type))))
    5413              :     return false;
    5414              : 
    5415       180348 :   if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
    5416       160203 :       && INTEGRAL_TYPE_P (lhs_type)
    5417       313483 :       && !type_has_mode_precision_p (lhs_type))
    5418              :     {
    5419          447 :       if (dump_enabled_p ())
    5420            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    5421              :                          "type conversion to bit-precision unsupported\n");
    5422          447 :       return false;
    5423              :     }
    5424              : 
    5425       179901 :   if (op_type == binary_op)
    5426              :     {
    5427         2330 :       gcc_assert (code == WIDEN_MULT_EXPR
    5428              :                   || code == WIDEN_LSHIFT_EXPR
    5429              :                   || widening_fn_p (code));
    5430              : 
    5431         2330 :       op1 = is_gimple_assign (stmt) ? gimple_assign_rhs2 (stmt) :
    5432            0 :                                      gimple_call_arg (stmt, 0);
    5433         2330 :       tree vectype1_in;
    5434         2330 :       if (!vect_is_simple_use (vinfo, slp_node, 1,
    5435              :                                &op1, &slp_op1, &dt[1], &vectype1_in))
    5436              :         {
    5437            0 :           if (dump_enabled_p ())
    5438            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    5439              :                              "use not simple.\n");
    5440            0 :           return false;
    5441              :         }
    5442              :       /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
    5443              :          OP1.  */
    5444         2330 :       if (!vectype_in)
    5445          101 :         vectype_in = vectype1_in;
    5446              :     }
    5447              : 
    5448              :   /* If op0 is an external or constant def, infer the vector type
    5449              :      from the scalar type.  */
    5450       179901 :   if (!vectype_in)
    5451        20097 :     vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
    5452       179901 :   if (!cost_vec)
    5453        22820 :     gcc_assert (vectype_in);
    5454       179901 :   if (!vectype_in)
    5455              :     {
    5456          257 :       if (dump_enabled_p ())
    5457            2 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    5458              :                          "no vectype for scalar type %T\n", rhs_type);
    5459              : 
    5460          257 :       return false;
    5461              :     }
    5462              : 
    5463       359288 :   if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
    5464       179644 :       != VECTOR_BOOLEAN_TYPE_P (vectype_in))
    5465              :     {
    5466          229 :       if (dump_enabled_p ())
    5467           36 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    5468              :                          "can't convert between boolean and non "
    5469              :                          "boolean vectors %T\n", rhs_type);
    5470              : 
    5471          229 :       return false;
    5472              :     }
    5473              : 
    5474       179415 :   nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
    5475       179415 :   nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
    5476       179415 :   if (known_eq (nunits_out, nunits_in))
    5477        85715 :     if (widen_arith)
    5478              :       modifier = WIDEN;
    5479              :     else
    5480       179415 :       modifier = NONE;
    5481        93700 :   else if (multiple_p (nunits_out, nunits_in))
    5482              :     modifier = NARROW_DST;
    5483              :   else
    5484              :     {
    5485        51896 :       gcc_checking_assert (multiple_p (nunits_in, nunits_out));
    5486              :       modifier = WIDEN;
    5487              :     }
    5488              : 
    5489       179415 :   bool found_mode = false;
    5490       179415 :   scalar_mode lhs_mode = SCALAR_TYPE_MODE (lhs_type);
    5491       179415 :   scalar_mode rhs_mode = SCALAR_TYPE_MODE (rhs_type);
    5492       179415 :   opt_scalar_mode rhs_mode_iter;
    5493       179415 :   auto_vec<std::pair<tree, tree_code>, 2> converts;
    5494       179415 :   bool evenodd_ok = false;
    5495              : 
    5496              :   /* Supportable by target?  */
    5497       179415 :   switch (modifier)
    5498              :     {
    5499        85472 :     case NONE:
    5500        85472 :       if (code != FIX_TRUNC_EXPR
    5501        84430 :           && code != FLOAT_EXPR
    5502       160924 :           && !CONVERT_EXPR_CODE_P (code))
    5503              :         return false;
    5504        85472 :       gcc_assert (code.is_tree_code ());
    5505        85472 :       if (supportable_indirect_convert_operation (code,
    5506              :                                                   vectype_out, vectype_in,
    5507              :                                                   converts, op0, slp_op0))
    5508              :         {
    5509        18999 :           gcc_assert (converts.length () <= 2);
    5510        18999 :           if (converts.length () == 1)
    5511        18925 :             code1 = converts[0].second;
    5512              :           else
    5513              :             {
    5514           74 :               cvt_type = NULL_TREE;
    5515           74 :               multi_step_cvt = converts.length () - 1;
    5516           74 :               codecvt1 = converts[0].second;
    5517           74 :               code1 = converts[1].second;
    5518           74 :               interm_types.safe_push (converts[0].first);
    5519              :             }
    5520              :           break;
    5521              :         }
    5522              : 
    5523              :       /* FALLTHRU */
    5524        66473 :     unsupported:
    5525        73326 :       if (dump_enabled_p ())
    5526         5992 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    5527              :                          "conversion not supported by target.\n");
    5528              :       return false;
    5529              : 
    5530        52139 :     case WIDEN:
    5531        52139 :       if (known_eq (nunits_in, nunits_out))
    5532              :         {
    5533          486 :           if (!(code.is_tree_code ()
    5534          243 :                 && supportable_half_widening_operation ((tree_code) code,
    5535              :                                                         vectype_out, vectype_in,
    5536              :                                                         &tc1)))
    5537           74 :             goto unsupported;
    5538          169 :           code1 = tc1;
    5539          169 :           gcc_assert (!(multi_step_cvt && op_type == binary_op));
    5540              :           break;
    5541              :         }
    5542              :       /* Elements in a vector can only be reordered if used in a reduction
    5543              :          operation only.  */
    5544        51896 :       if (code == WIDEN_MULT_EXPR
    5545         2087 :           && loop_vinfo
    5546         2038 :           && !nested_in_vect_loop_p (LOOP_VINFO_LOOP (loop_vinfo), stmt_info)
    5547              :           /* For a SLP reduction we cannot swizzle lanes, detecting a
    5548              :              reduction chain isn't possible here.  */
    5549        53912 :           && SLP_TREE_LANES (slp_node) == 1)
    5550              :         {
    5551              :           /* ???  There is no way to look for SLP uses, so work on
    5552              :              the stmt and what the stmt-based cycle detection gives us.  */
    5553         1914 :           tree lhs = gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt);
    5554         1914 :           stmt_vec_info use_stmt_info
    5555         1914 :             = lhs ? loop_vinfo->lookup_single_use (lhs) : NULL;
    5556         1914 :           if (use_stmt_info
    5557         1765 :               && STMT_VINFO_REDUC_DEF (use_stmt_info))
    5558        51896 :             evenodd_ok = true;
    5559              :         }
    5560        51896 :       if (supportable_widening_operation (code, vectype_out, vectype_in,
    5561              :                                           evenodd_ok, &code1,
    5562              :                                           &code2, &multi_step_cvt,
    5563              :                                           &interm_types))
    5564              :         {
    5565              :           /* Binary widening operation can only be supported directly by the
    5566              :              architecture.  */
    5567        49970 :           gcc_assert (!(multi_step_cvt && op_type == binary_op));
    5568              :           break;
    5569              :         }
    5570              : 
    5571         1926 :       if (code != FLOAT_EXPR
    5572         2298 :           || GET_MODE_SIZE (lhs_mode) <= GET_MODE_SIZE (rhs_mode))
    5573         1740 :         goto unsupported;
    5574              : 
    5575          186 :       fltsz = GET_MODE_SIZE (lhs_mode);
    5576          273 :       FOR_EACH_2XWIDER_MODE (rhs_mode_iter, rhs_mode)
    5577              :         {
    5578          273 :           rhs_mode = rhs_mode_iter.require ();
    5579          546 :           if (GET_MODE_SIZE (rhs_mode) > fltsz)
    5580              :             break;
    5581              : 
    5582          273 :           cvt_type
    5583          273 :             = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
    5584          273 :           cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
    5585          273 :           if (cvt_type == NULL_TREE)
    5586            0 :             goto unsupported;
    5587              : 
    5588          546 :           if (GET_MODE_SIZE (rhs_mode) == fltsz)
    5589              :             {
    5590           81 :               tc1 = ERROR_MARK;
    5591           81 :               gcc_assert (code.is_tree_code ());
    5592           81 :               if (!supportable_convert_operation ((tree_code) code, vectype_out,
    5593              :                                                   cvt_type, &tc1))
    5594           22 :                 goto unsupported;
    5595           59 :               codecvt1 = tc1;
    5596              :             }
    5597          192 :           else if (!supportable_widening_operation (code, vectype_out,
    5598              :                                                     cvt_type, evenodd_ok,
    5599              :                                                     &codecvt1,
    5600              :                                                     &codecvt2, &multi_step_cvt,
    5601              :                                                     &interm_types))
    5602           87 :             continue;
    5603              :           else
    5604          105 :             gcc_assert (multi_step_cvt == 0);
    5605              : 
    5606          164 :           if (supportable_widening_operation (NOP_EXPR, cvt_type,
    5607              :                                               vectype_in, evenodd_ok, &code1,
    5608              :                                               &code2, &multi_step_cvt,
    5609              :                                               &interm_types))
    5610              :             {
    5611              :               found_mode = true;
    5612              :               break;
    5613              :             }
    5614              :         }
    5615              : 
    5616          164 :       if (!found_mode)
    5617            0 :         goto unsupported;
    5618              : 
    5619          328 :       if (GET_MODE_SIZE (rhs_mode) == fltsz)
    5620           59 :         codecvt2 = ERROR_MARK;
    5621              :       else
    5622              :         {
    5623          105 :           multi_step_cvt++;
    5624          105 :           interm_types.safe_push (cvt_type);
    5625          105 :           cvt_type = NULL_TREE;
    5626              :         }
    5627              :       break;
    5628              : 
    5629        41804 :     case NARROW_DST:
    5630        41804 :       gcc_assert (op_type == unary_op);
    5631        41804 :       if (supportable_narrowing_operation (code, vectype_out, vectype_in,
    5632              :                                            &code1, &multi_step_cvt,
    5633              :                                            &interm_types))
    5634              :         break;
    5635              : 
    5636        15435 :       if (GET_MODE_SIZE (lhs_mode) >= GET_MODE_SIZE (rhs_mode))
    5637          984 :         goto unsupported;
    5638              : 
    5639         4161 :       if (code == FIX_TRUNC_EXPR)
    5640              :         {
    5641          107 :           cvt_type
    5642          107 :             = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
    5643          107 :           cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
    5644          107 :           if (cvt_type == NULL_TREE)
    5645            0 :             goto unsupported;
    5646          107 :           if (supportable_convert_operation ((tree_code) code, cvt_type, vectype_in,
    5647              :                                               &tc1))
    5648          105 :             codecvt1 = tc1;
    5649              :           else
    5650            2 :             goto unsupported;
    5651          105 :           if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
    5652              :                                                &code1, &multi_step_cvt,
    5653              :                                                &interm_types))
    5654              :             break;
    5655              :         }
    5656              :       /* If op0 can be represented with low precision integer,
    5657              :          truncate it to cvt_type and the do FLOAT_EXPR.  */
    5658         4054 :       else if (code == FLOAT_EXPR)
    5659              :         {
    5660          137 :           if (cost_vec)
    5661              :             {
    5662          132 :               wide_int op_min_value, op_max_value;
    5663          132 :               tree def;
    5664              : 
    5665              :               /* ???  Merge ranges in case of more than one lane.  */
    5666          132 :               if (SLP_TREE_LANES (slp_op0) != 1
    5667          130 :                   || !(def = vect_get_slp_scalar_def (slp_op0, 0))
    5668          262 :                   || !vect_get_range_info (def, &op_min_value, &op_max_value))
    5669          106 :                 goto unsupported;
    5670              : 
    5671           26 :               if ((wi::min_precision (op_max_value, SIGNED)
    5672           26 :                    > GET_MODE_BITSIZE (lhs_mode))
    5673           26 :                   || (wi::min_precision (op_min_value, SIGNED)
    5674           24 :                       > GET_MODE_BITSIZE (lhs_mode)))
    5675            2 :                 goto unsupported;
    5676          132 :             }
    5677              : 
    5678           29 :           cvt_type
    5679           29 :             = build_nonstandard_integer_type (GET_MODE_BITSIZE (lhs_mode), 0);
    5680           29 :           cvt_type = get_same_sized_vectype (cvt_type, vectype_out);
    5681           29 :           if (cvt_type == NULL_TREE)
    5682            0 :             goto unsupported;
    5683           29 :           if (!supportable_narrowing_operation (NOP_EXPR, cvt_type, vectype_in,
    5684              :                                                 &code1, &multi_step_cvt,
    5685              :                                                 &interm_types))
    5686            2 :             goto unsupported;
    5687           27 :           if (supportable_convert_operation ((tree_code) code, vectype_out,
    5688              :                                              cvt_type, &tc1))
    5689              :             {
    5690           27 :               codecvt1 = tc1;
    5691           27 :               modifier = NARROW_SRC;
    5692           27 :               break;
    5693              :             }
    5694              :         }
    5695              : 
    5696         3921 :       goto unsupported;
    5697              : 
    5698              :     default:
    5699              :       gcc_unreachable ();
    5700              :     }
    5701              : 
    5702       106089 :   if (modifier == WIDEN
    5703       106089 :       && loop_vinfo
    5704        49149 :       && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
    5705       127125 :       && (code1 == VEC_WIDEN_MULT_EVEN_EXPR
    5706        21014 :           || widening_evenodd_fn_p (code1)))
    5707              :     {
    5708           22 :       if (dump_enabled_p ())
    5709            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    5710              :                          "can't use a fully-masked loop because"
    5711              :                          " widening operation on even/odd elements"
    5712              :                          " mixes up lanes.\n");
    5713           22 :       LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    5714              :     }
    5715              : 
    5716       106089 :   if (cost_vec)         /* transformation not required.  */
    5717              :     {
    5718        83269 :       if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype_in)
    5719        83269 :           || !vect_maybe_update_slp_op_vectype (slp_op1, vectype_in))
    5720              :         {
    5721            0 :           if (dump_enabled_p ())
    5722            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    5723              :                              "incompatible vector types for invariants\n");
    5724            0 :           return false;
    5725              :         }
    5726        83269 :       DUMP_VECT_SCOPE ("vectorizable_conversion");
    5727        83269 :       unsigned int nvectors = vect_get_num_copies (vinfo, slp_node);
    5728        83269 :       if (modifier == NONE)
    5729              :         {
    5730        15005 :           SLP_TREE_TYPE (slp_node) = type_conversion_vec_info_type;
    5731        15005 :           vect_model_simple_cost (vinfo, (1 + multi_step_cvt),
    5732              :                                   slp_node, cost_vec);
    5733              :         }
    5734        68264 :       else if (modifier == NARROW_SRC || modifier == NARROW_DST)
    5735              :         {
    5736        27747 :           SLP_TREE_TYPE (slp_node) = type_demotion_vec_info_type;
    5737              :           /* The final packing step produces one vector result per copy.  */
    5738        27747 :           vect_model_promotion_demotion_cost (slp_node, nvectors,
    5739              :                                               multi_step_cvt, cost_vec,
    5740              :                                               widen_arith);
    5741              :         }
    5742              :       else
    5743              :         {
    5744        40517 :           SLP_TREE_TYPE (slp_node) = type_promotion_vec_info_type;
    5745              :           /* The initial unpacking step produces two vector results
    5746              :              per copy.  MULTI_STEP_CVT is 0 for a single conversion,
    5747              :              so >> MULTI_STEP_CVT divides by 2^(number of steps - 1).  */
    5748        40517 :           vect_model_promotion_demotion_cost (slp_node,
    5749              :                                               nvectors >> multi_step_cvt,
    5750              :                                               multi_step_cvt, cost_vec,
    5751              :                                               widen_arith);
    5752              :         }
    5753        83269 :       interm_types.release ();
    5754        83269 :       return true;
    5755        83269 :     }
    5756              : 
    5757              :   /* Transform.  */
    5758        22820 :   if (dump_enabled_p ())
    5759         4272 :     dump_printf_loc (MSG_NOTE, vect_location, "transform conversion.\n");
    5760              : 
    5761        22820 :   if (op_type == binary_op)
    5762              :     {
    5763          508 :       if (CONSTANT_CLASS_P (op0))
    5764            0 :         op0 = fold_convert (TREE_TYPE (op1), op0);
    5765          508 :       else if (CONSTANT_CLASS_P (op1))
    5766          234 :         op1 = fold_convert (TREE_TYPE (op0), op1);
    5767              :     }
    5768              : 
    5769              :   /* In case of multi-step conversion, we first generate conversion operations
    5770              :      to the intermediate types, and then from that types to the final one.
    5771              :      We create vector destinations for the intermediate type (TYPES) received
    5772              :      from supportable_*_operation, and store them in the correct order
    5773              :      for future use in vect_create_vectorized_*_stmts ().  */
    5774        22820 :   auto_vec<tree> vec_dsts (multi_step_cvt + 1);
    5775        22820 :   bool widen_or_narrow_float_p
    5776        22820 :     = cvt_type && (modifier == WIDEN || modifier == NARROW_SRC);
    5777        22820 :   vec_dest = vect_create_destination_var (scalar_dest,
    5778              :                                           widen_or_narrow_float_p
    5779              :                                           ? cvt_type : vectype_out);
    5780        22820 :   vec_dsts.quick_push (vec_dest);
    5781              : 
    5782        22820 :   if (multi_step_cvt)
    5783              :     {
    5784         9078 :       for (i = interm_types.length () - 1;
    5785         9078 :            interm_types.iterate (i, &intermediate_type); i--)
    5786              :         {
    5787         4785 :           vec_dest = vect_create_destination_var (scalar_dest,
    5788              :                                                   intermediate_type);
    5789         4785 :           vec_dsts.quick_push (vec_dest);
    5790              :         }
    5791              :     }
    5792              : 
    5793        22820 :   if (cvt_type)
    5794           73 :     vec_dest = vect_create_destination_var (scalar_dest,
    5795              :                                             widen_or_narrow_float_p
    5796              :                                             ? vectype_out : cvt_type);
    5797              : 
    5798        22820 :   switch (modifier)
    5799              :     {
    5800         3994 :     case NONE:
    5801         3994 :       vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0);
    5802              :       /* vec_dest is intermediate type operand when multi_step_cvt.  */
    5803         3994 :       if (multi_step_cvt)
    5804              :         {
    5805           21 :           cvt_op = vec_dest;
    5806           21 :           vec_dest = vec_dsts[0];
    5807              :         }
    5808              : 
    5809         8364 :       FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
    5810              :         {
    5811              :           /* Arguments are ready, create the new vector stmt.  */
    5812         4370 :           gimple* new_stmt;
    5813         4370 :           if (multi_step_cvt)
    5814              :             {
    5815           21 :               gcc_assert (multi_step_cvt == 1);
    5816           21 :               new_stmt = vect_gimple_build (cvt_op, codecvt1, vop0);
    5817           21 :               new_temp = make_ssa_name (cvt_op, new_stmt);
    5818           21 :               gimple_assign_set_lhs (new_stmt, new_temp);
    5819           21 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5820           21 :               vop0 = new_temp;
    5821              :             }
    5822         4370 :           new_stmt = vect_gimple_build (vec_dest, code1, vop0);
    5823         4370 :           new_temp = make_ssa_name (vec_dest, new_stmt);
    5824         4370 :           gimple_set_lhs (new_stmt, new_temp);
    5825         4370 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5826              : 
    5827         4370 :           slp_node->push_vec_def (new_stmt);
    5828              :         }
    5829              :       break;
    5830              : 
    5831         9786 :     case WIDEN:
    5832              :       /* In case the vectorization factor (VF) is bigger than the number
    5833              :          of elements that we can fit in a vectype (nunits), we have to
    5834              :          generate more than one vector stmt - i.e - we need to "unroll"
    5835              :          the vector stmt by a factor VF/nunits.  */
    5836         9786 :       vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0,
    5837         9786 :                          code == WIDEN_LSHIFT_EXPR ? NULL_TREE : op1,
    5838              :                          &vec_oprnds1);
    5839         9786 :       if (code == WIDEN_LSHIFT_EXPR)
    5840              :         {
    5841            0 :           int oprnds_size = vec_oprnds0.length ();
    5842            0 :           vec_oprnds1.create (oprnds_size);
    5843            0 :           for (i = 0; i < oprnds_size; ++i)
    5844            0 :             vec_oprnds1.quick_push (op1);
    5845              :         }
    5846              :       /* Arguments are ready.  Create the new vector stmts.  */
    5847        21338 :       for (i = multi_step_cvt; i >= 0; i--)
    5848              :         {
    5849        11552 :           tree this_dest = vec_dsts[i];
    5850        11552 :           code_helper c1 = code1, c2 = code2;
    5851        11552 :           if (i == 0 && codecvt2 != ERROR_MARK)
    5852              :             {
    5853           48 :               c1 = codecvt1;
    5854           48 :               c2 = codecvt2;
    5855              :             }
    5856        11552 :           if (known_eq (nunits_out, nunits_in))
    5857           14 :             vect_create_half_widening_stmts (vinfo, &vec_oprnds0, &vec_oprnds1,
    5858              :                                              stmt_info, this_dest, gsi, c1,
    5859              :                                              op_type);
    5860              :           else
    5861        11538 :             vect_create_vectorized_promotion_stmts (vinfo, &vec_oprnds0,
    5862              :                                                     &vec_oprnds1, stmt_info,
    5863              :                                                     this_dest, gsi,
    5864              :                                                     c1, c2, op_type);
    5865              :         }
    5866              : 
    5867        37330 :       FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
    5868              :         {
    5869        27544 :           gimple *new_stmt;
    5870        27544 :           if (cvt_type)
    5871              :             {
    5872          120 :               new_temp = make_ssa_name (vec_dest);
    5873          120 :               new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
    5874          120 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5875              :             }
    5876              :           else
    5877        27424 :             new_stmt = SSA_NAME_DEF_STMT (vop0);
    5878              : 
    5879        27544 :           slp_node->push_vec_def (new_stmt);
    5880              :         }
    5881              :       break;
    5882              : 
    5883         9040 :     case NARROW_SRC:
    5884         9040 :     case NARROW_DST:
    5885              :       /* In case the vectorization factor (VF) is bigger than the number
    5886              :          of elements that we can fit in a vectype (nunits), we have to
    5887              :          generate more than one vector stmt - i.e - we need to "unroll"
    5888              :          the vector stmt by a factor VF/nunits.  */
    5889         9040 :       vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0);
    5890              :       /* Arguments are ready.  Create the new vector stmts.  */
    5891         9040 :       if (cvt_type && modifier == NARROW_DST)
    5892          153 :         FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
    5893              :           {
    5894          124 :             new_temp = make_ssa_name (vec_dest);
    5895          124 :             gimple *new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
    5896          124 :             vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5897          124 :             vec_oprnds0[i] = new_temp;
    5898              :           }
    5899              : 
    5900         9040 :       vect_create_vectorized_demotion_stmts (vinfo, &vec_oprnds0,
    5901              :                                              multi_step_cvt,
    5902              :                                              stmt_info, vec_dsts, gsi,
    5903              :                                              slp_node, code1,
    5904              :                                              modifier == NARROW_SRC);
    5905              :       /* After demoting op0 to cvt_type, convert it to dest.  */
    5906         9040 :       if (cvt_type && code == FLOAT_EXPR)
    5907              :         {
    5908           10 :           for (unsigned int i = 0; i != vec_oprnds0.length() / 2;  i++)
    5909              :             {
    5910              :               /* Arguments are ready, create the new vector stmt.  */
    5911            5 :               gcc_assert (TREE_CODE_LENGTH ((tree_code) codecvt1) == unary_op);
    5912            5 :               gimple *new_stmt
    5913            5 :                 = vect_gimple_build (vec_dest, codecvt1, vec_oprnds0[i]);
    5914            5 :               new_temp = make_ssa_name (vec_dest, new_stmt);
    5915            5 :               gimple_set_lhs (new_stmt, new_temp);
    5916            5 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    5917              : 
    5918              :               /* This is the last step of the conversion sequence. Store the
    5919              :                  vectors in SLP_NODE or in vector info of the scalar statement
    5920              :                  (or in STMT_VINFO_RELATED_STMT chain).  */
    5921            5 :               slp_node->push_vec_def (new_stmt);
    5922              :             }
    5923              :         }
    5924              :       break;
    5925              :     }
    5926              : 
    5927        22820 :   vec_oprnds0.release ();
    5928        22820 :   vec_oprnds1.release ();
    5929        22820 :   interm_types.release ();
    5930              : 
    5931        22820 :   return true;
    5932       179415 : }
    5933              : 
    5934              : /* Return true if we can assume from the scalar form of STMT_INFO that
    5935              :    neither the scalar nor the vector forms will generate code.  STMT_INFO
    5936              :    is known not to involve a data reference.  */
    5937              : 
    5938              : bool
    5939      3170076 : vect_nop_conversion_p (stmt_vec_info stmt_info)
    5940              : {
    5941      3170076 :   gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
    5942      2895516 :   if (!stmt || STMT_VINFO_DATA_REF (stmt_info))
    5943              :     return false;
    5944              : 
    5945       931619 :   tree lhs = gimple_assign_lhs (stmt);
    5946       931619 :   tree_code code = gimple_assign_rhs_code (stmt);
    5947       931619 :   tree rhs = gimple_assign_rhs1 (stmt);
    5948              : 
    5949       931619 :   if (code == SSA_NAME || code == VIEW_CONVERT_EXPR)
    5950              :     return true;
    5951              : 
    5952       928717 :   if (CONVERT_EXPR_CODE_P (code))
    5953       229862 :     return tree_nop_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs));
    5954              : 
    5955              :   return false;
    5956              : }
    5957              : 
    5958              : /* Function vectorizable_assignment.
    5959              : 
    5960              :    Check if STMT_INFO performs an assignment (copy) that can be vectorized.
    5961              :    If COST_VEC is passed, calculate costs but don't change anything,
    5962              :    otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
    5963              :    it, and insert it at GSI.
    5964              :    Return true if STMT_INFO is vectorizable in this way.  */
    5965              : 
    5966              : static bool
    5967      2062999 : vectorizable_assignment (vec_info *vinfo,
    5968              :                          stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
    5969              :                          slp_tree slp_node,
    5970              :                          stmt_vector_for_cost *cost_vec)
    5971              : {
    5972      2062999 :   tree vec_dest;
    5973      2062999 :   tree scalar_dest;
    5974      2062999 :   tree op;
    5975      2062999 :   tree new_temp;
    5976      2062999 :   enum vect_def_type dt[1] = {vect_unknown_def_type};
    5977      2062999 :   int i;
    5978      2062999 :   vec<tree> vec_oprnds = vNULL;
    5979      2062999 :   tree vop;
    5980      2062999 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
    5981      2062999 :   enum tree_code code;
    5982      2062999 :   tree vectype_in;
    5983              : 
    5984      2062999 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
    5985              :     return false;
    5986              : 
    5987      2062999 :   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
    5988       234770 :       && cost_vec)
    5989              :     return false;
    5990              : 
    5991              :   /* Is vectorizable assignment?  */
    5992      3733378 :   gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
    5993      1755757 :   if (!stmt)
    5994              :     return false;
    5995              : 
    5996      1755757 :   scalar_dest = gimple_assign_lhs (stmt);
    5997      1755757 :   if (TREE_CODE (scalar_dest) != SSA_NAME)
    5998              :     return false;
    5999              : 
    6000       939019 :   if (STMT_VINFO_DATA_REF (stmt_info))
    6001              :     return false;
    6002              : 
    6003       396115 :   code = gimple_assign_rhs_code (stmt);
    6004       396115 :   if (!(gimple_assign_single_p (stmt)
    6005       394586 :         || code == PAREN_EXPR
    6006       393410 :         || CONVERT_EXPR_CODE_P (code)))
    6007              :     return false;
    6008              : 
    6009        96025 :   tree vectype = SLP_TREE_VECTYPE (slp_node);
    6010        96025 :   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
    6011              : 
    6012        96025 :   slp_tree slp_op;
    6013        96025 :   if (!vect_is_simple_use (vinfo, slp_node, 0, &op, &slp_op,
    6014              :                            &dt[0], &vectype_in))
    6015              :     {
    6016            0 :       if (dump_enabled_p ())
    6017            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6018              :                          "use not simple.\n");
    6019            0 :       return false;
    6020              :     }
    6021        96025 :   if (!vectype_in)
    6022        17840 :     vectype_in = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op), slp_node);
    6023              : 
    6024              :   /* We can handle VIEW_CONVERT conversions that do not change the number
    6025              :      of elements or the vector size or other conversions when the component
    6026              :      types are nop-convertible.  */
    6027        96025 :   if (!vectype_in
    6028        95748 :       || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype_in), nunits)
    6029        88617 :       || (code == VIEW_CONVERT_EXPR
    6030         2802 :           && maybe_ne (GET_MODE_SIZE (TYPE_MODE (vectype)),
    6031         2802 :                        GET_MODE_SIZE (TYPE_MODE (vectype_in))))
    6032       184642 :       || (CONVERT_EXPR_CODE_P (code)
    6033        85944 :           && !tree_nop_conversion_p (TREE_TYPE (vectype),
    6034        85944 :                                      TREE_TYPE (vectype_in))))
    6035        10379 :     return false;
    6036              : 
    6037       256842 :   if (VECTOR_BOOLEAN_TYPE_P (vectype) != VECTOR_BOOLEAN_TYPE_P (vectype_in))
    6038              :     {
    6039            2 :       if (dump_enabled_p ())
    6040            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6041              :                          "can't convert between boolean and non "
    6042            0 :                          "boolean vectors %T\n", TREE_TYPE (op));
    6043              : 
    6044            2 :       return false;
    6045              :     }
    6046              : 
    6047              :   /* We do not handle bit-precision changes.  */
    6048        85644 :   if ((CONVERT_EXPR_CODE_P (code)
    6049         2673 :        || code == VIEW_CONVERT_EXPR)
    6050        84372 :       && ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
    6051        83035 :            && !type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
    6052        84057 :           || (INTEGRAL_TYPE_P (TREE_TYPE (op))
    6053        79254 :               && !type_has_mode_precision_p (TREE_TYPE (op))))
    6054              :       /* But a conversion that does not change the bit-pattern is ok.  */
    6055        86374 :       && !(INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
    6056          730 :            && INTEGRAL_TYPE_P (TREE_TYPE (op))
    6057          730 :            && (((TYPE_PRECISION (TREE_TYPE (scalar_dest))
    6058          730 :                > TYPE_PRECISION (TREE_TYPE (op)))
    6059          415 :              && TYPE_UNSIGNED (TREE_TYPE (op)))
    6060          331 :                || (TYPE_PRECISION (TREE_TYPE (scalar_dest))
    6061          331 :                    == TYPE_PRECISION (TREE_TYPE (op))))))
    6062              :     {
    6063          266 :       if (dump_enabled_p ())
    6064            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6065              :                          "type conversion to/from bit-precision "
    6066              :                          "unsupported.\n");
    6067          266 :       return false;
    6068              :     }
    6069              : 
    6070        85378 :   if (cost_vec) /* transformation not required.  */
    6071              :     {
    6072        69416 :       if (!vect_maybe_update_slp_op_vectype (slp_op, vectype_in))
    6073              :         {
    6074            0 :           if (dump_enabled_p ())
    6075            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6076              :                              "incompatible vector types for invariants\n");
    6077            0 :           return false;
    6078              :         }
    6079        69416 :       SLP_TREE_TYPE (slp_node) = assignment_vec_info_type;
    6080        69416 :       DUMP_VECT_SCOPE ("vectorizable_assignment");
    6081        69416 :       if (!vect_nop_conversion_p (stmt_info))
    6082          962 :         vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
    6083        69416 :       return true;
    6084              :     }
    6085              : 
    6086              :   /* Transform.  */
    6087        15962 :   if (dump_enabled_p ())
    6088         3595 :     dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
    6089              : 
    6090              :   /* Handle def.  */
    6091        15962 :   vec_dest = vect_create_destination_var (scalar_dest, vectype);
    6092              : 
    6093              :   /* Handle use.  */
    6094        15962 :   vect_get_vec_defs (vinfo, slp_node, op, &vec_oprnds);
    6095              : 
    6096              :   /* Arguments are ready. create the new vector stmt.  */
    6097        36061 :   FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
    6098              :     {
    6099        20099 :       if (CONVERT_EXPR_CODE_P (code)
    6100          679 :           || code == VIEW_CONVERT_EXPR)
    6101        19554 :         vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
    6102        20099 :       gassign *new_stmt = gimple_build_assign (vec_dest, vop);
    6103        20099 :       new_temp = make_ssa_name (vec_dest, new_stmt);
    6104        20099 :       gimple_assign_set_lhs (new_stmt, new_temp);
    6105        20099 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    6106        20099 :       slp_node->push_vec_def (new_stmt);
    6107              :     }
    6108              : 
    6109        15962 :   vec_oprnds.release ();
    6110        15962 :   return true;
    6111              : }
    6112              : 
    6113              : 
    6114              : /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
    6115              :    either as shift by a scalar or by a vector.  */
    6116              : 
    6117              : bool
    6118       298516 : vect_supportable_shift (vec_info *vinfo, enum tree_code code, tree scalar_type)
    6119              : {
    6120       298516 :   optab optab;
    6121       298516 :   tree vectype;
    6122              : 
    6123       298516 :   vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
    6124       298516 :   if (!vectype)
    6125              :     return false;
    6126              : 
    6127       298516 :   optab = optab_for_tree_code (code, vectype, optab_scalar);
    6128       298516 :   if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
    6129              :     return true;
    6130              : 
    6131       262523 :   optab = optab_for_tree_code (code, vectype, optab_vector);
    6132       262523 :   if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
    6133              :     return true;
    6134              : 
    6135              :   return false;
    6136              : }
    6137              : 
    6138              : 
    6139              : /* Function vectorizable_shift.
    6140              : 
    6141              :    Check if STMT_INFO performs a shift operation that can be vectorized.
    6142              :    If COST_VEC is passed, calculate costs but don't change anything,
    6143              :    otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
    6144              :    it, and insert it at GSI.
    6145              :    Return true if STMT_INFO is vectorizable in this way.  */
    6146              : 
    6147              : static bool
    6148       730466 : vectorizable_shift (vec_info *vinfo,
    6149              :                     stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
    6150              :                     slp_tree slp_node,
    6151              :                     stmt_vector_for_cost *cost_vec)
    6152              : {
    6153       730466 :   tree vec_dest;
    6154       730466 :   tree scalar_dest;
    6155       730466 :   tree op0, op1 = NULL;
    6156       730466 :   tree vec_oprnd1 = NULL_TREE;
    6157       730466 :   tree vectype;
    6158       730466 :   enum tree_code code;
    6159       730466 :   machine_mode vec_mode;
    6160       730466 :   tree new_temp;
    6161       730466 :   optab optab;
    6162       730466 :   int icode;
    6163       730466 :   machine_mode optab_op2_mode;
    6164       730466 :   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
    6165       730466 :   poly_uint64 nunits_in;
    6166       730466 :   poly_uint64 nunits_out;
    6167       730466 :   tree vectype_out;
    6168       730466 :   tree op1_vectype;
    6169       730466 :   int i;
    6170       730466 :   vec<tree> vec_oprnds0 = vNULL;
    6171       730466 :   vec<tree> vec_oprnds1 = vNULL;
    6172       730466 :   tree vop0, vop1;
    6173       730466 :   unsigned int k;
    6174       730466 :   bool scalar_shift_arg = true;
    6175       730466 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
    6176       730466 :   bool incompatible_op1_vectype_p = false;
    6177              : 
    6178       730466 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
    6179              :     return false;
    6180              : 
    6181       730466 :   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
    6182       234770 :       && STMT_VINFO_DEF_TYPE (stmt_info) != vect_nested_cycle
    6183       233280 :       && cost_vec)
    6184              :     return false;
    6185              : 
    6186              :   /* Is STMT a vectorizable binary/unary operation?   */
    6187      1095486 :   gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
    6188       425715 :   if (!stmt)
    6189              :     return false;
    6190              : 
    6191       425715 :   if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
    6192              :     return false;
    6193              : 
    6194       425197 :   code = gimple_assign_rhs_code (stmt);
    6195              : 
    6196       425197 :   if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
    6197              :       || code == RROTATE_EXPR))
    6198              :     return false;
    6199              : 
    6200        66817 :   scalar_dest = gimple_assign_lhs (stmt);
    6201        66817 :   vectype_out = SLP_TREE_VECTYPE (slp_node);
    6202        66817 :   if (!type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
    6203              :     {
    6204            0 :       if (dump_enabled_p ())
    6205            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6206              :                          "bit-precision shifts not supported.\n");
    6207            0 :       return false;
    6208              :     }
    6209              : 
    6210        66817 :   slp_tree slp_op0;
    6211        66817 :   if (!vect_is_simple_use (vinfo, slp_node,
    6212              :                            0, &op0, &slp_op0, &dt[0], &vectype))
    6213              :     {
    6214            0 :       if (dump_enabled_p ())
    6215            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6216              :                          "use not simple.\n");
    6217            0 :       return false;
    6218              :     }
    6219              :   /* If op0 is an external or constant def, infer the vector type
    6220              :      from the scalar type.  */
    6221        66817 :   if (!vectype)
    6222        15031 :     vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0), slp_node);
    6223        66817 :   if (!cost_vec)
    6224         8497 :     gcc_assert (vectype);
    6225        66817 :   if (!vectype)
    6226              :     {
    6227            0 :       if (dump_enabled_p ())
    6228            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6229              :                          "no vectype for scalar type\n");
    6230            0 :       return false;
    6231              :     }
    6232              : 
    6233        66817 :   nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
    6234        66817 :   nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
    6235        66817 :   if (maybe_ne (nunits_out, nunits_in))
    6236              :     return false;
    6237              : 
    6238        66817 :   stmt_vec_info op1_def_stmt_info;
    6239        66817 :   slp_tree slp_op1;
    6240        66817 :   if (!vect_is_simple_use (vinfo, slp_node, 1, &op1, &slp_op1,
    6241              :                            &dt[1], &op1_vectype, &op1_def_stmt_info))
    6242              :     {
    6243            0 :       if (dump_enabled_p ())
    6244            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6245              :                          "use not simple.\n");
    6246            0 :       return false;
    6247              :     }
    6248              : 
    6249              :   /* Determine whether the shift amount is a vector, or scalar.  If the
    6250              :      shift/rotate amount is a vector, use the vector/vector shift optabs.  */
    6251              : 
    6252        66817 :   if ((dt[1] == vect_internal_def
    6253        66817 :        || dt[1] == vect_induction_def
    6254        50502 :        || dt[1] == vect_nested_cycle)
    6255        16333 :       && SLP_TREE_LANES (slp_node) == 1)
    6256              :     scalar_shift_arg = false;
    6257        50539 :   else if (dt[1] == vect_constant_def
    6258              :            || dt[1] == vect_external_def
    6259        50539 :            || dt[1] == vect_internal_def)
    6260              :     {
    6261              :       /* In SLP, need to check whether the shift count is the same,
    6262              :          in loops if it is a constant or invariant, it is always
    6263              :          a scalar shift.  */
    6264        50533 :       vec<stmt_vec_info> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
    6265        50533 :       stmt_vec_info slpstmt_info;
    6266              : 
    6267       133546 :       FOR_EACH_VEC_ELT (stmts, k, slpstmt_info)
    6268        83013 :         if (slpstmt_info)
    6269              :           {
    6270        83013 :             gassign *slpstmt = as_a <gassign *> (slpstmt_info->stmt);
    6271       166026 :             if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
    6272        83013 :               scalar_shift_arg = false;
    6273              :           }
    6274              : 
    6275              :       /* For internal SLP defs we have to make sure we see scalar stmts
    6276              :          for all vector elements.
    6277              :          ???  For different vectors we could resort to a different
    6278              :          scalar shift operand but code-generation below simply always
    6279              :          takes the first.  */
    6280        50533 :       if (dt[1] == vect_internal_def
    6281        50582 :           && maybe_ne (nunits_out * vect_get_num_copies (vinfo, slp_node),
    6282           49 :                        stmts.length ()))
    6283              :         scalar_shift_arg = false;
    6284              : 
    6285              :       /* If the shift amount is computed by a pattern stmt we cannot
    6286              :          use the scalar amount directly thus give up and use a vector
    6287              :          shift.  */
    6288        50533 :       if (op1_def_stmt_info && is_pattern_stmt_p (op1_def_stmt_info))
    6289              :         scalar_shift_arg = false;
    6290              :     }
    6291              :   else
    6292              :     {
    6293            6 :       if (dump_enabled_p ())
    6294            6 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6295              :                          "operand mode requires invariant argument.\n");
    6296            6 :       return false;
    6297              :     }
    6298              : 
    6299              :   /* Vector shifted by vector.  */
    6300        66849 :   bool was_scalar_shift_arg = scalar_shift_arg;
    6301        50524 :   if (!scalar_shift_arg)
    6302              :     {
    6303        16325 :       optab = optab_for_tree_code (code, vectype, optab_vector);
    6304        16325 :       if (dump_enabled_p ())
    6305         1205 :         dump_printf_loc (MSG_NOTE, vect_location,
    6306              :                          "vector/vector shift/rotate found.\n");
    6307              : 
    6308        16325 :       if (!op1_vectype)
    6309           15 :         op1_vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op1),
    6310              :                                                    slp_op1);
    6311        16325 :       incompatible_op1_vectype_p
    6312        32650 :         = (op1_vectype == NULL_TREE
    6313        16325 :            || maybe_ne (TYPE_VECTOR_SUBPARTS (op1_vectype),
    6314        16325 :                         TYPE_VECTOR_SUBPARTS (vectype))
    6315        32648 :            || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype));
    6316        16318 :       if (incompatible_op1_vectype_p
    6317            7 :           && (SLP_TREE_DEF_TYPE (slp_op1) != vect_constant_def
    6318            1 :               || slp_op1->refcnt != 1))
    6319              :         {
    6320            6 :           if (dump_enabled_p ())
    6321            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6322              :                              "unusable type for last operand in"
    6323              :                              " vector/vector shift/rotate.\n");
    6324            6 :           return false;
    6325              :         }
    6326              :     }
    6327              :   /* See if the machine has a vector shifted by scalar insn and if not
    6328              :      then see if it has a vector shifted by vector insn.  */
    6329              :   else
    6330              :     {
    6331        50486 :       optab = optab_for_tree_code (code, vectype, optab_scalar);
    6332        50486 :       if (optab
    6333        50486 :           && can_implement_p (optab, TYPE_MODE (vectype)))
    6334              :         {
    6335        50486 :           if (dump_enabled_p ())
    6336         4930 :             dump_printf_loc (MSG_NOTE, vect_location,
    6337              :                              "vector/scalar shift/rotate found.\n");
    6338              :         }
    6339              :       else
    6340              :         {
    6341            0 :           optab = optab_for_tree_code (code, vectype, optab_vector);
    6342            0 :           if (optab
    6343            0 :               && can_implement_p (optab, TYPE_MODE (vectype)))
    6344              :             {
    6345            0 :               scalar_shift_arg = false;
    6346              : 
    6347            0 :               if (dump_enabled_p ())
    6348            0 :                 dump_printf_loc (MSG_NOTE, vect_location,
    6349              :                                  "vector/vector shift/rotate found.\n");
    6350              : 
    6351            0 :               if (!op1_vectype)
    6352            0 :                 op1_vectype = get_vectype_for_scalar_type (vinfo,
    6353            0 :                                                            TREE_TYPE (op1),
    6354              :                                                            slp_op1);
    6355              : 
    6356              :               /* Unlike the other binary operators, shifts/rotates have
    6357              :                  the rhs being int, instead of the same type as the lhs,
    6358              :                  so make sure the scalar is the right type if we are
    6359              :                  dealing with vectors of long long/long/short/char.  */
    6360            0 :               incompatible_op1_vectype_p
    6361            0 :                 = (!op1_vectype
    6362            0 :                    || !tree_nop_conversion_p (TREE_TYPE (vectype),
    6363            0 :                                               TREE_TYPE (op1)));
    6364            0 :               if (incompatible_op1_vectype_p
    6365            0 :                   && dt[1] == vect_internal_def)
    6366              :                 {
    6367            0 :                   if (dump_enabled_p ())
    6368            0 :                     dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6369              :                                      "unusable type for last operand in"
    6370              :                                      " vector/vector shift/rotate.\n");
    6371            0 :                   return false;
    6372              :                 }
    6373              :             }
    6374              :         }
    6375              :     }
    6376              : 
    6377              :   /* Supportable by target?  */
    6378        66805 :   if (!optab)
    6379              :     {
    6380            0 :       if (dump_enabled_p ())
    6381            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6382              :                          "no shift optab for %s and %T.\n",
    6383              :                          get_tree_code_name (code), vectype);
    6384            0 :       return false;
    6385              :     }
    6386        66805 :   vec_mode = TYPE_MODE (vectype);
    6387        66805 :   icode = (int) optab_handler (optab, vec_mode);
    6388        66805 :   if (icode == CODE_FOR_nothing)
    6389              :     {
    6390         6110 :       if (dump_enabled_p ())
    6391          900 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6392              :                          "shift op not supported by target.\n");
    6393         6110 :       return false;
    6394              :     }
    6395              :   /* vector lowering cannot optimize vector shifts using word arithmetic.  */
    6396        60695 :   if (vect_emulated_vector_p (vectype))
    6397              :     return false;
    6398              : 
    6399        60695 :   if (cost_vec) /* transformation not required.  */
    6400              :     {
    6401        52198 :       if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
    6402        52198 :           || ((!scalar_shift_arg || dt[1] == vect_internal_def)
    6403         8072 :               && (!incompatible_op1_vectype_p
    6404            1 :                   || dt[1] == vect_constant_def)
    6405         8072 :               && !vect_maybe_update_slp_op_vectype
    6406         8072 :                          (slp_op1,
    6407              :                           incompatible_op1_vectype_p ? vectype : op1_vectype)))
    6408              :         {
    6409            0 :           if (dump_enabled_p ())
    6410            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6411              :                              "incompatible vector types for invariants\n");
    6412            0 :           return false;
    6413              :         }
    6414              :       /* Now adjust the constant shift amount in place.  */
    6415        52198 :       if (incompatible_op1_vectype_p
    6416            1 :           && dt[1] == vect_constant_def)
    6417            4 :         for (unsigned i = 0;
    6418            5 :              i < SLP_TREE_SCALAR_OPS (slp_op1).length (); ++i)
    6419              :           {
    6420            4 :             SLP_TREE_SCALAR_OPS (slp_op1)[i]
    6421            4 :               = fold_convert (TREE_TYPE (vectype),
    6422              :                               SLP_TREE_SCALAR_OPS (slp_op1)[i]);
    6423            4 :             gcc_assert ((TREE_CODE (SLP_TREE_SCALAR_OPS (slp_op1)[i])
    6424              :                          == INTEGER_CST));
    6425              :           }
    6426        52198 :       SLP_TREE_TYPE (slp_node) = shift_vec_info_type;
    6427        52198 :       DUMP_VECT_SCOPE ("vectorizable_shift");
    6428        52198 :       vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
    6429        52198 :       return true;
    6430              :     }
    6431              : 
    6432              :   /* Transform.  */
    6433              : 
    6434         8497 :   if (dump_enabled_p ())
    6435         2018 :     dump_printf_loc (MSG_NOTE, vect_location,
    6436              :                      "transform binary/unary operation.\n");
    6437              : 
    6438              :   /* Handle def.  */
    6439         8497 :   vec_dest = vect_create_destination_var (scalar_dest, vectype);
    6440              : 
    6441         8497 :   unsigned nvectors = vect_get_num_copies (vinfo, slp_node);
    6442         8497 :   if (scalar_shift_arg && dt[1] != vect_internal_def)
    6443              :     {
    6444              :       /* Vector shl and shr insn patterns can be defined with scalar
    6445              :          operand 2 (shift operand).  In this case, use constant or loop
    6446              :          invariant op1 directly, without extending it to vector mode
    6447              :          first.  */
    6448         6340 :       optab_op2_mode = insn_data[icode].operand[2].mode;
    6449         6340 :       if (!VECTOR_MODE_P (optab_op2_mode))
    6450              :         {
    6451         6340 :           if (dump_enabled_p ())
    6452         1903 :             dump_printf_loc (MSG_NOTE, vect_location,
    6453              :                              "operand 1 using scalar mode.\n");
    6454         6340 :           vec_oprnd1 = op1;
    6455         6340 :           vec_oprnds1.create (nvectors);
    6456         6340 :           vec_oprnds1.quick_push (vec_oprnd1);
    6457              :           /* Store vec_oprnd1 for every vector stmt to be created.
    6458              :              We check during the analysis that all the shift arguments
    6459              :              are the same.
    6460              :              TODO: Allow different constants for different vector
    6461              :              stmts generated for an SLP instance.  */
    6462        14725 :           for (k = 0; k < nvectors - 1; k++)
    6463         2045 :             vec_oprnds1.quick_push (vec_oprnd1);
    6464              :         }
    6465              :     }
    6466         2157 :   else if (!scalar_shift_arg && incompatible_op1_vectype_p)
    6467              :     {
    6468            0 :       if (was_scalar_shift_arg)
    6469              :         {
    6470              :           /* If the argument was the same in all lanes create the
    6471              :              correctly typed vector shift amount directly.  Note
    6472              :              we made SLP scheduling think we use the original scalars,
    6473              :              so place the compensation code next to the shift which
    6474              :              is conservative.  See PR119640 where it otherwise breaks.  */
    6475            0 :           op1 = fold_convert (TREE_TYPE (vectype), op1);
    6476            0 :           op1 = vect_init_vector (vinfo, stmt_info, op1, TREE_TYPE (vectype),
    6477              :                                   gsi);
    6478            0 :           vec_oprnd1 = vect_init_vector (vinfo, stmt_info, op1, vectype,
    6479              :                                          gsi);
    6480            0 :           vec_oprnds1.create (nvectors);
    6481            0 :           for (k = 0; k < nvectors; k++)
    6482            0 :             vec_oprnds1.quick_push (vec_oprnd1);
    6483              :         }
    6484            0 :       else if (dt[1] == vect_constant_def)
    6485              :         /* The constant shift amount has been adjusted in place.  */
    6486              :         ;
    6487              :       else
    6488            0 :         gcc_assert (TYPE_MODE (op1_vectype) == TYPE_MODE (vectype));
    6489              :     }
    6490              : 
    6491              :   /* vec_oprnd1 is available if operand 1 should be of a scalar-type
    6492              :      (a special case for certain kind of vector shifts); otherwise,
    6493              :      operand 1 should be of a vector type (the usual case).  */
    6494         2157 :   vect_get_vec_defs (vinfo, slp_node,
    6495              :                      op0, &vec_oprnds0,
    6496         8497 :                      vec_oprnd1 ? NULL_TREE : op1, &vec_oprnds1);
    6497              : 
    6498              :   /* Arguments are ready.  Create the new vector stmt.  */
    6499        22541 :   FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
    6500              :     {
    6501              :       /* For internal defs where we need to use a scalar shift arg
    6502              :          extract the first lane.  */
    6503        14044 :       if (scalar_shift_arg && dt[1] == vect_internal_def)
    6504              :         {
    6505           10 :           vop1 = vec_oprnds1[0];
    6506           10 :           new_temp = make_ssa_name (TREE_TYPE (TREE_TYPE (vop1)));
    6507           10 :           gassign *new_stmt
    6508           10 :             = gimple_build_assign (new_temp,
    6509           10 :                                    build3 (BIT_FIELD_REF, TREE_TYPE (new_temp),
    6510              :                                            vop1,
    6511           10 :                                            TYPE_SIZE (TREE_TYPE (new_temp)),
    6512              :                                            bitsize_zero_node));
    6513           10 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    6514           10 :           vop1 = new_temp;
    6515           10 :         }
    6516              :       else
    6517        14034 :         vop1 = vec_oprnds1[i];
    6518        14044 :       gassign *new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
    6519        14044 :       new_temp = make_ssa_name (vec_dest, new_stmt);
    6520        14044 :       gimple_assign_set_lhs (new_stmt, new_temp);
    6521        14044 :       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    6522        14044 :       slp_node->push_vec_def (new_stmt);
    6523              :     }
    6524              : 
    6525         8497 :   vec_oprnds0.release ();
    6526         8497 :   vec_oprnds1.release ();
    6527              : 
    6528         8497 :   return true;
    6529              : }
    6530              : 
    6531              : /* Function vectorizable_operation.
    6532              : 
    6533              :    Check if STMT_INFO performs a binary, unary or ternary operation that can
    6534              :    be vectorized.
    6535              :    If COST_VEC is passed, calculate costs but don't change anything,
    6536              :    otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
    6537              :    it, and insert it at GSI.
    6538              :    Return true if STMT_INFO is vectorizable in this way.  */
    6539              : 
    6540              : static bool
    6541      2719978 : vectorizable_operation (vec_info *vinfo,
    6542              :                         stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
    6543              :                         slp_tree slp_node,
    6544              :                         stmt_vector_for_cost *cost_vec)
    6545              : {
    6546      2719978 :   tree vec_dest;
    6547      2719978 :   tree scalar_dest;
    6548      2719978 :   tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
    6549      2719978 :   tree vectype;
    6550      2719978 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    6551      2719978 :   enum tree_code code, orig_code;
    6552      2719978 :   machine_mode vec_mode;
    6553      2719978 :   tree new_temp;
    6554      2719978 :   int op_type;
    6555      2719978 :   optab optab;
    6556      2719978 :   bool target_support_p;
    6557      2719978 :   enum vect_def_type dt[3]
    6558              :     = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
    6559      2719978 :   poly_uint64 nunits_in;
    6560      2719978 :   poly_uint64 nunits_out;
    6561      2719978 :   tree vectype_out;
    6562      2719978 :   int i;
    6563      2719978 :   vec<tree> vec_oprnds0 = vNULL;
    6564      2719978 :   vec<tree> vec_oprnds1 = vNULL;
    6565      2719978 :   vec<tree> vec_oprnds2 = vNULL;
    6566      2719978 :   tree vop0, vop1, vop2;
    6567      2719978 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
    6568              : 
    6569      2719978 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
    6570              :     return false;
    6571              : 
    6572      2719978 :   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
    6573       234770 :       && cost_vec)
    6574              :     return false;
    6575              : 
    6576              :   /* Is STMT a vectorizable binary/unary operation?   */
    6577      4459773 :   gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
    6578      2412736 :   if (!stmt)
    6579              :     return false;
    6580              : 
    6581              :   /* Loads and stores are handled in vectorizable_{load,store}.  */
    6582      2412736 :   if (STMT_VINFO_DATA_REF (stmt_info))
    6583              :     return false;
    6584              : 
    6585      1053094 :   orig_code = code = gimple_assign_rhs_code (stmt);
    6586              : 
    6587              :   /* Shifts are handled in vectorizable_shift.  */
    6588      1053094 :   if (code == LSHIFT_EXPR
    6589              :       || code == RSHIFT_EXPR
    6590              :       || code == LROTATE_EXPR
    6591      1053094 :       || code == RROTATE_EXPR)
    6592              :    return false;
    6593              : 
    6594              :   /* Comparisons are handled in vectorizable_comparison.  */
    6595       994774 :   if (TREE_CODE_CLASS (code) == tcc_comparison)
    6596              :     return false;
    6597              : 
    6598              :   /* Conditions are handled in vectorizable_condition.  */
    6599       810620 :   if (code == COND_EXPR)
    6600              :     return false;
    6601              : 
    6602              :   /* For pointer addition and subtraction, we should use the normal
    6603              :      plus and minus for the vector operation.  */
    6604       784261 :   if (code == POINTER_PLUS_EXPR)
    6605              :     code = PLUS_EXPR;
    6606       765036 :   if (code == POINTER_DIFF_EXPR)
    6607          991 :     code = MINUS_EXPR;
    6608              : 
    6609              :   /* Support only unary or binary operations.  */
    6610       784261 :   op_type = TREE_CODE_LENGTH (code);
    6611       784261 :   if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
    6612              :     {
    6613            0 :       if (dump_enabled_p ())
    6614            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6615              :                          "num. args = %d (not unary/binary/ternary op).\n",
    6616              :                          op_type);
    6617            0 :       return false;
    6618              :     }
    6619              : 
    6620       784261 :   scalar_dest = gimple_assign_lhs (stmt);
    6621       784261 :   vectype_out = SLP_TREE_VECTYPE (slp_node);
    6622              : 
    6623              :   /* Most operations cannot handle bit-precision types without extra
    6624              :      truncations.  */
    6625       784261 :   bool mask_op_p = VECTOR_BOOLEAN_TYPE_P (vectype_out);
    6626       773246 :   if (!mask_op_p
    6627       773246 :       && !type_has_mode_precision_p (TREE_TYPE (scalar_dest))
    6628              :       /* Exception are bitwise binary operations.  */
    6629              :       && code != BIT_IOR_EXPR
    6630         1420 :       && code != BIT_XOR_EXPR
    6631          906 :       && code != BIT_AND_EXPR)
    6632              :     {
    6633          690 :       if (dump_enabled_p ())
    6634            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6635              :                          "bit-precision arithmetic not supported.\n");
    6636          690 :       return false;
    6637              :     }
    6638              : 
    6639       783571 :   slp_tree slp_op0;
    6640       783571 :   if (!vect_is_simple_use (vinfo, slp_node,
    6641              :                            0, &op0, &slp_op0, &dt[0], &vectype))
    6642              :     {
    6643            0 :       if (dump_enabled_p ())
    6644            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6645              :                          "use not simple.\n");
    6646            0 :       return false;
    6647              :     }
    6648       783571 :   bool is_invariant = (dt[0] == vect_external_def
    6649       783571 :                        || dt[0] == vect_constant_def);
    6650              :   /* If op0 is an external or constant def, infer the vector type
    6651              :      from the scalar type.  */
    6652       783571 :   if (!vectype)
    6653              :     {
    6654              :       /* For boolean type we cannot determine vectype by
    6655              :          invariant value (don't know whether it is a vector
    6656              :          of booleans or vector of integers).  We use output
    6657              :          vectype because operations on boolean don't change
    6658              :          type.  */
    6659        72425 :       if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op0)))
    6660              :         {
    6661         1456 :           if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (scalar_dest)))
    6662              :             {
    6663          242 :               if (dump_enabled_p ())
    6664            0 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6665              :                                  "not supported operation on bool value.\n");
    6666          242 :               return false;
    6667              :             }
    6668         1214 :           vectype = vectype_out;
    6669              :         }
    6670              :       else
    6671        70969 :         vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0),
    6672              :                                                slp_node);
    6673              :     }
    6674       783329 :   if (!cost_vec)
    6675       114062 :     gcc_assert (vectype);
    6676       783329 :   if (!vectype)
    6677              :     {
    6678          289 :       if (dump_enabled_p ())
    6679            2 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6680              :                          "no vectype for scalar type %T\n",
    6681            2 :                          TREE_TYPE (op0));
    6682              : 
    6683          289 :       return false;
    6684              :     }
    6685              : 
    6686       783040 :   nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
    6687       783040 :   nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
    6688       783040 :   if (maybe_ne (nunits_out, nunits_in)
    6689       783040 :       || !tree_nop_conversion_p (TREE_TYPE (vectype_out), TREE_TYPE (vectype)))
    6690        11768 :     return false;
    6691              : 
    6692       771272 :   tree vectype2 = NULL_TREE, vectype3 = NULL_TREE;
    6693       771272 :   slp_tree slp_op1 = NULL, slp_op2 = NULL;
    6694       771272 :   if (op_type == binary_op || op_type == ternary_op)
    6695              :     {
    6696       690562 :       if (!vect_is_simple_use (vinfo, slp_node,
    6697              :                                1, &op1, &slp_op1, &dt[1], &vectype2))
    6698              :         {
    6699            0 :           if (dump_enabled_p ())
    6700            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6701              :                              "use not simple.\n");
    6702            0 :           return false;
    6703              :         }
    6704       690562 :       is_invariant &= (dt[1] == vect_external_def
    6705       690562 :                        || dt[1] == vect_constant_def);
    6706       690562 :       if (vectype2
    6707      1173491 :           && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype2))
    6708       482929 :               || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
    6709       482929 :                                          TREE_TYPE (vectype2))))
    6710            4 :         return false;
    6711              :     }
    6712       771268 :   if (op_type == ternary_op)
    6713              :     {
    6714            0 :       if (!vect_is_simple_use (vinfo, slp_node,
    6715              :                                2, &op2, &slp_op2, &dt[2], &vectype3))
    6716              :         {
    6717            0 :           if (dump_enabled_p ())
    6718            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6719              :                              "use not simple.\n");
    6720            0 :           return false;
    6721              :         }
    6722            0 :       is_invariant &= (dt[2] == vect_external_def
    6723            0 :                        || dt[2] == vect_constant_def);
    6724            0 :       if (vectype3
    6725            0 :           && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype3))
    6726            0 :               || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
    6727            0 :                                          TREE_TYPE (vectype3))))
    6728            0 :         return false;
    6729              :     }
    6730              : 
    6731              :   /* Multiple types in SLP are handled by creating the appropriate number of
    6732              :      vectorized stmts for each SLP node.  */
    6733       771268 :   auto vec_num = vect_get_num_copies (vinfo, slp_node);
    6734              : 
    6735              :   /* Reject attempts to combine mask types with nonmask types, e.g. if
    6736              :      we have an AND between a (nonmask) boolean loaded from memory and
    6737              :      a (mask) boolean result of a comparison.
    6738              : 
    6739              :      TODO: We could easily fix these cases up using pattern statements.  */
    6740       771268 :   if (VECTOR_BOOLEAN_TYPE_P (vectype) != mask_op_p
    6741      1246319 :       || (vectype2 && VECTOR_BOOLEAN_TYPE_P (vectype2) != mask_op_p)
    6742      1542536 :       || (vectype3 && VECTOR_BOOLEAN_TYPE_P (vectype3) != mask_op_p))
    6743              :     {
    6744            0 :       if (dump_enabled_p ())
    6745            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6746              :                          "mixed mask and nonmask vector types\n");
    6747            0 :       return false;
    6748              :     }
    6749              : 
    6750              :   /* Supportable by target?  */
    6751              : 
    6752       771268 :   vec_mode = TYPE_MODE (vectype);
    6753       771268 :   optab = optab_for_tree_code (code, vectype, optab_default);
    6754       771268 :   if (!optab)
    6755              :     {
    6756        68694 :       if (dump_enabled_p ())
    6757         5875 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6758              :                          "no optab for %s and %T.\n",
    6759              :                          get_tree_code_name (code), vectype);
    6760        68694 :       return false;
    6761              :     }
    6762       702574 :   target_support_p = can_implement_p (optab, vec_mode);
    6763              : 
    6764       702574 :   bool using_emulated_vectors_p = vect_emulated_vector_p (vectype);
    6765       702574 :   if (!target_support_p || using_emulated_vectors_p)
    6766              :     {
    6767        30035 :       if (dump_enabled_p ())
    6768         1124 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6769              :                          "op not supported by target.\n");
    6770              :       /* When vec_mode is not a vector mode and we verified ops we
    6771              :          do not have to lower like AND are natively supported let
    6772              :          those through even when the mode isn't word_mode.  For
    6773              :          ops we have to lower the lowering code assumes we are
    6774              :          dealing with word_mode.  */
    6775        60070 :       if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype))
    6776        29893 :           || !GET_MODE_SIZE (vec_mode).is_constant ()
    6777        29893 :           || (((code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
    6778        24803 :                || !target_support_p)
    6779        63521 :               && maybe_ne (GET_MODE_SIZE (vec_mode), UNITS_PER_WORD))
    6780              :           /* Check only during analysis.  */
    6781        41732 :           || (cost_vec && !vect_can_vectorize_without_simd_p (code)))
    6782              :         {
    6783        29473 :           if (dump_enabled_p ())
    6784         1122 :             dump_printf (MSG_NOTE, "using word mode not possible.\n");
    6785        29473 :           return false;
    6786              :         }
    6787          562 :       if (dump_enabled_p ())
    6788            2 :         dump_printf_loc (MSG_NOTE, vect_location,
    6789              :                          "proceeding using word mode.\n");
    6790              :       using_emulated_vectors_p = true;
    6791              :     }
    6792              : 
    6793       673101 :   int reduc_idx = SLP_TREE_REDUC_IDX (slp_node);
    6794       673101 :   vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
    6795       432368 :   vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
    6796       673101 :   internal_fn cond_fn = get_conditional_internal_fn (code);
    6797       673101 :   internal_fn cond_len_fn = get_conditional_len_internal_fn (code);
    6798              : 
    6799              :   /* If operating on inactive elements could generate spurious traps,
    6800              :      we need to restrict the operation to active lanes.  Note that this
    6801              :      specifically doesn't apply to unhoisted invariants, since they
    6802              :      operate on the same value for every lane.
    6803              : 
    6804              :      Similarly, if this operation is part of a reduction, a fully-masked
    6805              :      loop should only change the active lanes of the reduction chain,
    6806              :      keeping the inactive lanes as-is.  */
    6807       644811 :   bool mask_out_inactive = ((!is_invariant && gimple_could_trap_p (stmt))
    6808      1254482 :                             || reduc_idx >= 0);
    6809              : 
    6810       673101 :   if (cost_vec) /* transformation not required.  */
    6811              :     {
    6812       559039 :       if (loop_vinfo
    6813       329965 :           && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
    6814        88430 :           && mask_out_inactive)
    6815              :         {
    6816        20370 :           if (cond_len_fn != IFN_LAST
    6817        20370 :               && direct_internal_fn_supported_p (cond_len_fn, vectype,
    6818              :                                                  OPTIMIZE_FOR_SPEED))
    6819            0 :             vect_record_loop_len (loop_vinfo, lens, vec_num, vectype,
    6820              :                                   1);
    6821        20370 :           else if (cond_fn != IFN_LAST
    6822        20370 :                    && direct_internal_fn_supported_p (cond_fn, vectype,
    6823              :                                                       OPTIMIZE_FOR_SPEED))
    6824         8506 :             vect_record_loop_mask (loop_vinfo, masks, vec_num,
    6825              :                                    vectype, NULL);
    6826              :           else
    6827              :             {
    6828        11864 :               if (dump_enabled_p ())
    6829          608 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6830              :                                  "can't use a fully-masked loop because no"
    6831              :                                  " conditional operation is available.\n");
    6832        11864 :               LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
    6833              :             }
    6834              :         }
    6835              : 
    6836              :       /* Put types on constant and invariant SLP children.  */
    6837       559039 :       if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
    6838       558977 :           || !vect_maybe_update_slp_op_vectype (slp_op1, vectype)
    6839      1117918 :           || !vect_maybe_update_slp_op_vectype (slp_op2, vectype))
    6840              :         {
    6841          160 :           if (dump_enabled_p ())
    6842            3 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    6843              :                              "incompatible vector types for invariants\n");
    6844          160 :           return false;
    6845              :         }
    6846              : 
    6847       558879 :       SLP_TREE_TYPE (slp_node) = op_vec_info_type;
    6848       558879 :       DUMP_VECT_SCOPE ("vectorizable_operation");
    6849       558879 :       vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
    6850       558879 :       if (using_emulated_vectors_p)
    6851              :         {
    6852              :           /* The above vect_model_simple_cost call handles constants
    6853              :              in the prologue and (mis-)costs one of the stmts as
    6854              :              vector stmt.  See below for the actual lowering that will
    6855              :              be applied.  */
    6856          560 :           unsigned n = vect_get_num_copies (vinfo, slp_node);
    6857          560 :           switch (code)
    6858              :             {
    6859          201 :             case PLUS_EXPR:
    6860          201 :               n *= 5;
    6861          201 :               break;
    6862          328 :             case MINUS_EXPR:
    6863          328 :               n *= 6;
    6864          328 :               break;
    6865            0 :             case NEGATE_EXPR:
    6866            0 :               n *= 4;
    6867            0 :               break;
    6868              :             default:
    6869              :               /* Bit operations do not have extra cost and are accounted
    6870              :                  as vector stmt by vect_model_simple_cost.  */
    6871              :               n = 0;
    6872              :               break;
    6873              :             }
    6874          529 :           if (n != 0)
    6875              :             {
    6876              :               /* We also need to materialize two large constants.  */
    6877          529 :               record_stmt_cost (cost_vec, 2, scalar_stmt, stmt_info,
    6878              :                                 0, vect_prologue);
    6879          529 :               record_stmt_cost (cost_vec, n, scalar_stmt, stmt_info,
    6880              :                                 0, vect_body);
    6881              :             }
    6882              :         }
    6883       558879 :       return true;
    6884              :     }
    6885              : 
    6886              :   /* Transform.  */
    6887              : 
    6888       114062 :   if (dump_enabled_p ())
    6889        16441 :     dump_printf_loc (MSG_NOTE, vect_location,
    6890              :                      "transform binary/unary operation.\n");
    6891              : 
    6892       114062 :   bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
    6893       102403 :   bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
    6894              : 
    6895              :   /* POINTER_DIFF_EXPR has pointer arguments which are vectorized as
    6896              :      vectors with unsigned elements, but the result is signed.  So, we
    6897              :      need to compute the MINUS_EXPR into vectype temporary and
    6898              :      VIEW_CONVERT_EXPR it into the final vectype_out result.  */
    6899       114062 :   tree vec_cvt_dest = NULL_TREE;
    6900       114062 :   if (orig_code == POINTER_DIFF_EXPR)
    6901              :     {
    6902          110 :       vec_dest = vect_create_destination_var (scalar_dest, vectype);
    6903          110 :       vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
    6904              :     }
    6905              :   /* For reduction operations with undefined overflow behavior make sure to
    6906              :      pun them to unsigned since we change the order of evaluation.
    6907              :      ???  Avoid for in-order reductions?  */
    6908       113952 :   else if (arith_code_with_undefined_signed_overflow (orig_code)
    6909        97383 :            && ANY_INTEGRAL_TYPE_P (vectype)
    6910        47845 :            && TYPE_OVERFLOW_UNDEFINED (vectype)
    6911       139995 :            && SLP_TREE_REDUC_IDX (slp_node) != -1)
    6912              :     {
    6913         2507 :       gcc_assert (orig_code == PLUS_EXPR || orig_code == MINUS_EXPR
    6914              :                   || orig_code == MULT_EXPR || orig_code == POINTER_PLUS_EXPR);
    6915         2507 :       vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
    6916         2507 :       vectype = unsigned_type_for (vectype);
    6917         2507 :       vec_dest = vect_create_destination_var (scalar_dest, vectype);
    6918              :     }
    6919              :   /* Handle def.  */
    6920              :   else
    6921       111445 :     vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
    6922              : 
    6923       114062 :   vect_get_vec_defs (vinfo, slp_node,
    6924              :                      op0, &vec_oprnds0, op1, &vec_oprnds1, op2, &vec_oprnds2);
    6925              :   /* Arguments are ready.  Create the new vector stmt.  */
    6926       252309 :   FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
    6927              :     {
    6928       138247 :       gimple *new_stmt = NULL;
    6929       276494 :       vop1 = ((op_type == binary_op || op_type == ternary_op)
    6930       138247 :               ? vec_oprnds1[i] : NULL_TREE);
    6931       138247 :       vop2 = ((op_type == ternary_op) ? vec_oprnds2[i] : NULL_TREE);
    6932              : 
    6933       138247 :       if (vec_cvt_dest
    6934       138247 :           && !useless_type_conversion_p (vectype, TREE_TYPE (vop0)))
    6935              :         {
    6936         2917 :           new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop0);
    6937         2917 :           new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
    6938              :                                           new_temp);
    6939         2917 :           new_temp = make_ssa_name (vec_dest, new_stmt);
    6940         2917 :           gimple_assign_set_lhs (new_stmt, new_temp);
    6941         2917 :           vect_finish_stmt_generation (vinfo, stmt_info,
    6942              :                                        new_stmt, gsi);
    6943         2917 :           vop0 = new_temp;
    6944              :         }
    6945       138247 :       if (vop1
    6946       135687 :           && vec_cvt_dest
    6947       141289 :           && !useless_type_conversion_p (vectype, TREE_TYPE (vop1)))
    6948              :         {
    6949         2917 :           new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop1);
    6950         2917 :           new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
    6951              :                                           new_temp);
    6952         2917 :           new_temp = make_ssa_name (vec_dest, new_stmt);
    6953         2917 :           gimple_assign_set_lhs (new_stmt, new_temp);
    6954         2917 :           vect_finish_stmt_generation (vinfo, stmt_info,
    6955              :                                        new_stmt, gsi);
    6956         2917 :           vop1 = new_temp;
    6957              :         }
    6958       138247 :       if (vop2
    6959            0 :           && vec_cvt_dest
    6960       138247 :           && !useless_type_conversion_p (vectype, TREE_TYPE (vop2)))
    6961              :         {
    6962            0 :           new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop2);
    6963            0 :           new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
    6964              :                                           new_temp);
    6965            0 :           new_temp = make_ssa_name (vec_dest, new_stmt);
    6966            0 :           gimple_assign_set_lhs (new_stmt, new_temp);
    6967            0 :           vect_finish_stmt_generation (vinfo, stmt_info,
    6968              :                                        new_stmt, gsi);
    6969            0 :           vop2 = new_temp;
    6970              :         }
    6971              : 
    6972       138247 :       if (using_emulated_vectors_p)
    6973              :         {
    6974              :           /* Lower the operation.  This follows vector lowering.  */
    6975            2 :           tree word_type = build_nonstandard_integer_type
    6976            2 :                              (GET_MODE_BITSIZE (vec_mode).to_constant (), 1);
    6977            2 :           tree wvop0 = make_ssa_name (word_type);
    6978            2 :           new_stmt = gimple_build_assign (wvop0, VIEW_CONVERT_EXPR,
    6979              :                                           build1 (VIEW_CONVERT_EXPR,
    6980              :                                                   word_type, vop0));
    6981            2 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    6982            2 :           tree wvop1 = NULL_TREE;
    6983            2 :           if (vop1)
    6984              :             {
    6985            2 :               wvop1 = make_ssa_name (word_type);
    6986            2 :               new_stmt = gimple_build_assign (wvop1, VIEW_CONVERT_EXPR,
    6987              :                                               build1 (VIEW_CONVERT_EXPR,
    6988              :                                                       word_type, vop1));
    6989            2 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    6990              :             }
    6991              : 
    6992            2 :           tree result_low;
    6993            2 :           if (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
    6994              :             {
    6995            1 :               unsigned int width = vector_element_bits (vectype);
    6996            1 :               tree inner_type = TREE_TYPE (vectype);
    6997            1 :               HOST_WIDE_INT max = GET_MODE_MASK (TYPE_MODE (inner_type));
    6998            1 :               tree low_bits
    6999            1 :                 = build_replicated_int_cst (word_type, width, max >> 1);
    7000            1 :               tree high_bits
    7001            2 :                 = build_replicated_int_cst (word_type,
    7002            1 :                                             width, max & ~(max >> 1));
    7003            1 :               tree signs;
    7004            1 :               if (code == PLUS_EXPR || code == MINUS_EXPR)
    7005              :                 {
    7006            1 :                   signs = make_ssa_name (word_type);
    7007            1 :                   new_stmt = gimple_build_assign (signs,
    7008              :                                                   BIT_XOR_EXPR, wvop0, wvop1);
    7009            1 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7010            1 :                   tree b_low = make_ssa_name (word_type);
    7011            1 :                   new_stmt = gimple_build_assign (b_low, BIT_AND_EXPR,
    7012              :                                                   wvop1, low_bits);
    7013            1 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7014            1 :                   tree a_low = make_ssa_name (word_type);
    7015            1 :                   if (code == PLUS_EXPR)
    7016            1 :                     new_stmt = gimple_build_assign (a_low, BIT_AND_EXPR,
    7017              :                                                     wvop0, low_bits);
    7018              :                   else
    7019            0 :                     new_stmt = gimple_build_assign (a_low, BIT_IOR_EXPR,
    7020              :                                                     wvop0, high_bits);
    7021            1 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7022            1 :                   if (code == MINUS_EXPR)
    7023              :                     {
    7024            0 :                       new_stmt = gimple_build_assign (NULL_TREE,
    7025              :                                                       BIT_NOT_EXPR, signs);
    7026            0 :                       signs = make_ssa_name (word_type);
    7027            0 :                       gimple_assign_set_lhs (new_stmt, signs);
    7028            0 :                       vect_finish_stmt_generation (vinfo, stmt_info,
    7029              :                                                    new_stmt, gsi);
    7030              :                     }
    7031            1 :                   new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR,
    7032              :                                                   signs, high_bits);
    7033            1 :                   signs = make_ssa_name (word_type);
    7034            1 :                   gimple_assign_set_lhs (new_stmt, signs);
    7035            1 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7036            1 :                   result_low = make_ssa_name (word_type);
    7037            1 :                   new_stmt = gimple_build_assign (result_low, code,
    7038              :                                                   a_low, b_low);
    7039            1 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7040              :                 }
    7041              :               else /* if (code == NEGATE_EXPR) */
    7042              :                 {
    7043            0 :                   tree a_low = make_ssa_name (word_type);
    7044            0 :                   new_stmt = gimple_build_assign (a_low, BIT_AND_EXPR,
    7045              :                                                   wvop0, low_bits);
    7046            0 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7047            0 :                   signs = make_ssa_name (word_type);
    7048            0 :                   new_stmt = gimple_build_assign (signs, BIT_NOT_EXPR, wvop0);
    7049            0 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7050            0 :                   new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR,
    7051              :                                                   signs, high_bits);
    7052            0 :                   signs = make_ssa_name (word_type);
    7053            0 :                   gimple_assign_set_lhs (new_stmt, signs);
    7054            0 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7055            0 :                   result_low = make_ssa_name (word_type);
    7056            0 :                   new_stmt = gimple_build_assign (result_low,
    7057              :                                                   MINUS_EXPR, high_bits, a_low);
    7058            0 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7059              :                 }
    7060            1 :               new_stmt = gimple_build_assign (NULL_TREE, BIT_XOR_EXPR,
    7061              :                                               result_low, signs);
    7062            1 :               result_low = make_ssa_name (word_type);
    7063            1 :               gimple_assign_set_lhs (new_stmt, result_low);
    7064            1 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7065              :             }
    7066              :           else
    7067              :             {
    7068            1 :               new_stmt = gimple_build_assign (NULL_TREE, code, wvop0, wvop1);
    7069            1 :               result_low = make_ssa_name (word_type);
    7070            1 :               gimple_assign_set_lhs (new_stmt, result_low);
    7071            1 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7072              : 
    7073              :             }
    7074            2 :           new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR,
    7075              :                                           build1 (VIEW_CONVERT_EXPR,
    7076              :                                                   vectype, result_low));
    7077            2 :           new_temp = make_ssa_name (vectype);
    7078            2 :           gimple_assign_set_lhs (new_stmt, new_temp);
    7079            2 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7080              :         }
    7081       138245 :       else if ((masked_loop_p || len_loop_p) && mask_out_inactive)
    7082              :         {
    7083           16 :           tree mask;
    7084           16 :           if (masked_loop_p)
    7085           16 :             mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
    7086              :                                        vec_num, vectype, i);
    7087              :           else
    7088              :             /* Dummy mask.  */
    7089            0 :             mask = build_minus_one_cst (truth_type_for (vectype));
    7090           16 :           auto_vec<tree> vops (6);
    7091           16 :           vops.quick_push (mask);
    7092           16 :           vops.quick_push (vop0);
    7093           16 :           if (vop1)
    7094           16 :             vops.quick_push (vop1);
    7095           16 :           if (vop2)
    7096            0 :             vops.quick_push (vop2);
    7097           16 :           if (reduc_idx >= 0)
    7098              :             {
    7099              :               /* Perform the operation on active elements only and take
    7100              :                  inactive elements from the reduction chain input.  */
    7101            8 :               gcc_assert (!vop2);
    7102            8 :               vops.quick_push (reduc_idx == 1 ? vop1 : vop0);
    7103              :             }
    7104              :           else
    7105              :             {
    7106            8 :               auto else_value = targetm.preferred_else_value
    7107            8 :                 (cond_fn, vectype, vops.length () - 1, &vops[1]);
    7108            8 :               vops.quick_push (else_value);
    7109              :             }
    7110           16 :           if (len_loop_p)
    7111              :             {
    7112            0 :               tree len = vect_get_loop_len (loop_vinfo, gsi, lens,
    7113            0 :                                             vec_num, vectype, i, 1, true);
    7114            0 :               signed char biasval
    7115            0 :                 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
    7116            0 :               tree bias = build_int_cst (intQI_type_node, biasval);
    7117            0 :               vops.quick_push (len);
    7118            0 :               vops.quick_push (bias);
    7119              :             }
    7120           16 :           gcall *call
    7121           16 :             = gimple_build_call_internal_vec (masked_loop_p ? cond_fn
    7122              :                                                             : cond_len_fn,
    7123              :                                               vops);
    7124           16 :           new_temp = make_ssa_name (vec_dest, call);
    7125           16 :           gimple_call_set_lhs (call, new_temp);
    7126           16 :           gimple_call_set_nothrow (call, true);
    7127           16 :           vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
    7128           16 :           new_stmt = call;
    7129           16 :         }
    7130              :       else
    7131              :         {
    7132       138229 :           tree mask = NULL_TREE;
    7133              :           /* When combining two masks check if either of them is elsewhere
    7134              :              combined with a loop mask, if that's the case we can mark that the
    7135              :              new combined mask doesn't need to be combined with a loop mask.  */
    7136       138229 :           if (masked_loop_p
    7137       138229 :               && code == BIT_AND_EXPR
    7138       138229 :               && VECTOR_BOOLEAN_TYPE_P (vectype))
    7139              :             {
    7140            8 :               if (loop_vinfo->scalar_cond_masked_set.contains ({ op0, vec_num }))
    7141              :                 {
    7142            0 :                   mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
    7143              :                                              vec_num, vectype, i);
    7144              : 
    7145            0 :                   vop0 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
    7146              :                                            vop0, gsi);
    7147              :                 }
    7148              : 
    7149            8 :               if (loop_vinfo->scalar_cond_masked_set.contains ({ op1, vec_num }))
    7150              :                 {
    7151            0 :                   mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
    7152              :                                              vec_num, vectype, i);
    7153              : 
    7154            0 :                   vop1 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
    7155              :                                            vop1, gsi);
    7156              :                 }
    7157              :             }
    7158              : 
    7159       138229 :           new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
    7160       138229 :           new_temp = make_ssa_name (vec_dest, new_stmt);
    7161       138229 :           gimple_assign_set_lhs (new_stmt, new_temp);
    7162       138229 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    7163       138229 :           if (using_emulated_vectors_p)
    7164              :             suppress_warning (new_stmt, OPT_Wvector_operation_performance);
    7165              : 
    7166              :           /* Enter the combined value into the vector cond hash so we don't
    7167              :              AND it with a loop mask again.  */
    7168       138229 :           if (mask)
    7169            0 :             loop_vinfo->vec_cond_masked_set.add ({ new_temp, mask });
    7170              :         }
    7171              : 
    7172       138247 :       if (vec_cvt_dest)
    7173              :         {
    7174         3042 :           new_temp = build1 (VIEW_CONVERT_EXPR, vectype_out, new_temp);
    7175         3042 :           new_stmt = gimple_build_assign (vec_cvt_dest, VIEW_CONVERT_EXPR,
    7176              :                                           new_temp);
    7177         3042 :           new_temp = make_ssa_name (vec_cvt_dest, new_stmt);
    7178         3042 :           gimple_assign_set_lhs (new_stmt, new_temp);
    7179         3042 :           vect_finish_stmt_generation (vinfo, stmt_info,
    7180              :                                        new_stmt, gsi);
    7181              :         }
    7182              : 
    7183       138247 :       slp_node->push_vec_def (new_stmt);
    7184              :     }
    7185              : 
    7186       114062 :   vec_oprnds0.release ();
    7187       114062 :   vec_oprnds1.release ();
    7188       114062 :   vec_oprnds2.release ();
    7189              : 
    7190       114062 :   return true;
    7191              : }
    7192              : 
    7193              : /* A helper function to ensure data reference DR_INFO's base alignment.  */
    7194              : 
    7195              : static void
    7196      1968782 : ensure_base_align (dr_vec_info *dr_info)
    7197              : {
    7198              :   /* Alignment is only analyzed for the first element of a DR group,
    7199              :      use that to look at base alignment we need to enforce.  */
    7200      1968782 :   if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt))
    7201      1433145 :     dr_info = STMT_VINFO_DR_INFO (DR_GROUP_FIRST_ELEMENT (dr_info->stmt));
    7202              : 
    7203      1968782 :   gcc_assert (dr_info->misalignment != DR_MISALIGNMENT_UNINITIALIZED);
    7204              : 
    7205      1968782 :   if (dr_info->base_misaligned)
    7206              :     {
    7207       169172 :       tree base_decl = dr_info->base_decl;
    7208              : 
    7209              :       // We should only be able to increase the alignment of a base object if
    7210              :       // we know what its new alignment should be at compile time.
    7211       169172 :       unsigned HOST_WIDE_INT align_base_to =
    7212       169172 :         DR_TARGET_ALIGNMENT (dr_info).to_constant () * BITS_PER_UNIT;
    7213              : 
    7214       169172 :       if (decl_in_symtab_p (base_decl))
    7215         4717 :         symtab_node::get (base_decl)->increase_alignment (align_base_to);
    7216       164455 :       else if (DECL_ALIGN (base_decl) < align_base_to)
    7217              :         {
    7218       131514 :           SET_DECL_ALIGN (base_decl, align_base_to);
    7219       131514 :           DECL_USER_ALIGN (base_decl) = 1;
    7220              :         }
    7221       169172 :       dr_info->base_misaligned = false;
    7222              :     }
    7223      1968782 : }
    7224              : 
    7225              : 
    7226              : /* Function get_group_alias_ptr_type.
    7227              : 
    7228              :    Return the alias type for the group starting at FIRST_STMT_INFO.  */
    7229              : 
    7230              : static tree
    7231      1638757 : get_group_alias_ptr_type (stmt_vec_info first_stmt_info)
    7232              : {
    7233      1638757 :   struct data_reference *first_dr, *next_dr;
    7234              : 
    7235      1638757 :   first_dr = STMT_VINFO_DATA_REF (first_stmt_info);
    7236      1638757 :   stmt_vec_info next_stmt_info = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
    7237      3948456 :   while (next_stmt_info)
    7238              :     {
    7239      2444579 :       next_dr = STMT_VINFO_DATA_REF (next_stmt_info);
    7240      4889158 :       if (get_alias_set (DR_REF (first_dr))
    7241      2444579 :           != get_alias_set (DR_REF (next_dr)))
    7242              :         {
    7243       134880 :           if (dump_enabled_p ())
    7244           30 :             dump_printf_loc (MSG_NOTE, vect_location,
    7245              :                              "conflicting alias set types.\n");
    7246       134880 :           return ptr_type_node;
    7247              :         }
    7248      2309699 :       next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
    7249              :     }
    7250      1503877 :   return reference_alias_ptr_type (DR_REF (first_dr));
    7251              : }
    7252              : 
    7253              : 
    7254              : /* Function scan_operand_equal_p.
    7255              : 
    7256              :    Helper function for check_scan_store.  Compare two references
    7257              :    with .GOMP_SIMD_LANE bases.  */
    7258              : 
    7259              : static bool
    7260         1284 : scan_operand_equal_p (tree ref1, tree ref2)
    7261              : {
    7262         1284 :   tree ref[2] = { ref1, ref2 };
    7263         1284 :   poly_int64 bitsize[2], bitpos[2];
    7264              :   tree offset[2], base[2];
    7265         3852 :   for (int i = 0; i < 2; ++i)
    7266              :     {
    7267         2568 :       machine_mode mode;
    7268         2568 :       int unsignedp, reversep, volatilep = 0;
    7269         2568 :       base[i] = get_inner_reference (ref[i], &bitsize[i], &bitpos[i],
    7270              :                                      &offset[i], &mode, &unsignedp,
    7271              :                                      &reversep, &volatilep);
    7272         2568 :       if (reversep || volatilep || maybe_ne (bitpos[i], 0))
    7273            0 :         return false;
    7274         2568 :       if (TREE_CODE (base[i]) == MEM_REF
    7275           42 :           && offset[i] == NULL_TREE
    7276         2610 :           && TREE_CODE (TREE_OPERAND (base[i], 0)) == SSA_NAME)
    7277              :         {
    7278           42 :           gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base[i], 0));
    7279           42 :           if (is_gimple_assign (def_stmt)
    7280           42 :               && gimple_assign_rhs_code (def_stmt) == POINTER_PLUS_EXPR
    7281           42 :               && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == ADDR_EXPR
    7282           84 :               && TREE_CODE (gimple_assign_rhs2 (def_stmt)) == SSA_NAME)
    7283              :             {
    7284           42 :               if (maybe_ne (mem_ref_offset (base[i]), 0))
    7285              :                 return false;
    7286           42 :               base[i] = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
    7287           42 :               offset[i] = gimple_assign_rhs2 (def_stmt);
    7288              :             }
    7289              :         }
    7290              :     }
    7291              : 
    7292         1284 :   if (!operand_equal_p (base[0], base[1], 0))
    7293              :     return false;
    7294          934 :   if (maybe_ne (bitsize[0], bitsize[1]))
    7295              :     return false;
    7296          934 :   if (offset[0] != offset[1])
    7297              :     {
    7298          916 :       if (!offset[0] || !offset[1])
    7299              :         return false;
    7300          916 :       if (!operand_equal_p (offset[0], offset[1], 0))
    7301              :         {
    7302              :           tree step[2];
    7303            0 :           for (int i = 0; i < 2; ++i)
    7304              :             {
    7305            0 :               step[i] = integer_one_node;
    7306            0 :               if (TREE_CODE (offset[i]) == SSA_NAME)
    7307              :                 {
    7308            0 :                   gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
    7309            0 :                   if (is_gimple_assign (def_stmt)
    7310            0 :                       && gimple_assign_rhs_code (def_stmt) == MULT_EXPR
    7311            0 :                       && (TREE_CODE (gimple_assign_rhs2 (def_stmt))
    7312              :                           == INTEGER_CST))
    7313              :                     {
    7314            0 :                       step[i] = gimple_assign_rhs2 (def_stmt);
    7315            0 :                       offset[i] = gimple_assign_rhs1 (def_stmt);
    7316              :                     }
    7317              :                 }
    7318            0 :               else if (TREE_CODE (offset[i]) == MULT_EXPR)
    7319              :                 {
    7320            0 :                   step[i] = TREE_OPERAND (offset[i], 1);
    7321            0 :                   offset[i] = TREE_OPERAND (offset[i], 0);
    7322              :                 }
    7323            0 :               tree rhs1 = NULL_TREE;
    7324            0 :               if (TREE_CODE (offset[i]) == SSA_NAME)
    7325              :                 {
    7326            0 :                   gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
    7327            0 :                   if (gimple_assign_cast_p (def_stmt))
    7328            0 :                     rhs1 = gimple_assign_rhs1 (def_stmt);
    7329              :                 }
    7330            0 :               else if (CONVERT_EXPR_P (offset[i]))
    7331            0 :                 rhs1 = TREE_OPERAND (offset[i], 0);
    7332            0 :               if (rhs1
    7333            0 :                   && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
    7334            0 :                   && INTEGRAL_TYPE_P (TREE_TYPE (offset[i]))
    7335            0 :                   && (TYPE_PRECISION (TREE_TYPE (offset[i]))
    7336            0 :                       >= TYPE_PRECISION (TREE_TYPE (rhs1))))
    7337            0 :                 offset[i] = rhs1;
    7338              :             }
    7339            0 :           if (!operand_equal_p (offset[0], offset[1], 0)
    7340            0 :               || !operand_equal_p (step[0], step[1], 0))
    7341            0 :             return false;
    7342              :         }
    7343              :     }
    7344              :   return true;
    7345              : }
    7346              : 
    7347              : 
    7348              : enum scan_store_kind {
    7349              :   /* Normal permutation.  */
    7350              :   scan_store_kind_perm,
    7351              : 
    7352              :   /* Whole vector left shift permutation with zero init.  */
    7353              :   scan_store_kind_lshift_zero,
    7354              : 
    7355              :   /* Whole vector left shift permutation and VEC_COND_EXPR.  */
    7356              :   scan_store_kind_lshift_cond
    7357              : };
    7358              : 
    7359              : /* Function check_scan_store.
    7360              : 
    7361              :    Verify if we can perform the needed permutations or whole vector shifts.
    7362              :    Return -1 on failure, otherwise exact log2 of vectype's nunits.
    7363              :    USE_WHOLE_VECTOR is a vector of enum scan_store_kind which operation
    7364              :    to do at each step.  */
    7365              : 
    7366              : static int
    7367         1024 : scan_store_can_perm_p (tree vectype, tree init,
    7368              :                        vec<enum scan_store_kind> *use_whole_vector = NULL)
    7369              : {
    7370         1024 :   enum machine_mode vec_mode = TYPE_MODE (vectype);
    7371         1024 :   unsigned HOST_WIDE_INT nunits;
    7372         1024 :   if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
    7373              :     return -1;
    7374         1024 :   int units_log2 = exact_log2 (nunits);
    7375         1024 :   if (units_log2 <= 0)
    7376              :     return -1;
    7377              : 
    7378              :   int i;
    7379              :   enum scan_store_kind whole_vector_shift_kind = scan_store_kind_perm;
    7380         4784 :   for (i = 0; i <= units_log2; ++i)
    7381              :     {
    7382         3760 :       unsigned HOST_WIDE_INT j, k;
    7383         3760 :       enum scan_store_kind kind = scan_store_kind_perm;
    7384         3760 :       vec_perm_builder sel (nunits, nunits, 1);
    7385         3760 :       sel.quick_grow (nunits);
    7386         3760 :       if (i == units_log2)
    7387              :         {
    7388         9728 :           for (j = 0; j < nunits; ++j)
    7389         8704 :             sel[j] = nunits - 1;
    7390              :         }
    7391              :       else
    7392              :         {
    7393        10416 :           for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
    7394         7680 :             sel[j] = j;
    7395        26416 :           for (k = 0; j < nunits; ++j, ++k)
    7396        23680 :             sel[j] = nunits + k;
    7397              :         }
    7398         6496 :       vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
    7399         3760 :       if (!can_vec_perm_const_p (vec_mode, vec_mode, indices))
    7400              :         {
    7401            0 :           if (i == units_log2)
    7402              :             return -1;
    7403              : 
    7404            0 :           if (whole_vector_shift_kind == scan_store_kind_perm)
    7405              :             {
    7406            0 :               if (!can_implement_p (vec_shl_optab, vec_mode))
    7407              :                 return -1;
    7408            0 :               whole_vector_shift_kind = scan_store_kind_lshift_zero;
    7409              :               /* Whole vector shifts shift in zeros, so if init is all zero
    7410              :                  constant, there is no need to do anything further.  */
    7411            0 :               if ((TREE_CODE (init) != INTEGER_CST
    7412            0 :                    && TREE_CODE (init) != REAL_CST)
    7413            0 :                   || !initializer_zerop (init))
    7414              :                 {
    7415            0 :                   tree masktype = truth_type_for (vectype);
    7416            0 :                   if (!expand_vec_cond_expr_p (vectype, masktype))
    7417              :                     return -1;
    7418              :                   whole_vector_shift_kind = scan_store_kind_lshift_cond;
    7419              :                 }
    7420              :             }
    7421            0 :           kind = whole_vector_shift_kind;
    7422              :         }
    7423         3760 :       if (use_whole_vector)
    7424              :         {
    7425         1880 :           if (kind != scan_store_kind_perm && use_whole_vector->is_empty ())
    7426            0 :             use_whole_vector->safe_grow_cleared (i, true);
    7427         5640 :           if (kind != scan_store_kind_perm || !use_whole_vector->is_empty ())
    7428            0 :             use_whole_vector->safe_push (kind);
    7429              :         }
    7430         3760 :     }
    7431              : 
    7432              :   return units_log2;
    7433              : }
    7434              : 
    7435              : 
    7436              : /* Function check_scan_store.
    7437              : 
    7438              :    Check magic stores for #pragma omp scan {in,ex}clusive reductions.  */
    7439              : 
    7440              : static bool
    7441         1076 : check_scan_store (vec_info *vinfo, stmt_vec_info stmt_info, tree vectype,
    7442              :                   enum vect_def_type rhs_dt, slp_tree slp_node,
    7443              :                   slp_tree mask_node,
    7444              :                   vect_memory_access_type memory_access_type)
    7445              : {
    7446         1076 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    7447         1076 :   dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
    7448         1076 :   tree ref_type;
    7449              : 
    7450         1076 :   gcc_assert (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1);
    7451         1076 :   if (SLP_TREE_LANES (slp_node) > 1
    7452         1076 :       || mask_node
    7453         1076 :       || memory_access_type != VMAT_CONTIGUOUS
    7454         1076 :       || TREE_CODE (DR_BASE_ADDRESS (dr_info->dr)) != ADDR_EXPR
    7455         1076 :       || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0))
    7456         1076 :       || loop_vinfo == NULL
    7457         1076 :       || LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
    7458         1076 :       || LOOP_VINFO_EPILOGUE_P (loop_vinfo)
    7459         1076 :       || STMT_VINFO_GROUPED_ACCESS (stmt_info)
    7460         1076 :       || !integer_zerop (get_dr_vinfo_offset (vinfo, dr_info))
    7461         1076 :       || !integer_zerop (DR_INIT (dr_info->dr))
    7462         1076 :       || !(ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr)))
    7463         2152 :       || !alias_sets_conflict_p (get_alias_set (vectype),
    7464         1076 :                                  get_alias_set (TREE_TYPE (ref_type))))
    7465              :     {
    7466            0 :       if (dump_enabled_p ())
    7467            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    7468              :                          "unsupported OpenMP scan store.\n");
    7469            0 :       return false;
    7470              :     }
    7471              : 
    7472              :   /* We need to pattern match code built by OpenMP lowering and simplified
    7473              :      by following optimizations into something we can handle.
    7474              :      #pragma omp simd reduction(inscan,+:r)
    7475              :      for (...)
    7476              :        {
    7477              :          r += something ();
    7478              :          #pragma omp scan inclusive (r)
    7479              :          use (r);
    7480              :        }
    7481              :      shall have body with:
    7482              :        // Initialization for input phase, store the reduction initializer:
    7483              :        _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
    7484              :        _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
    7485              :        D.2042[_21] = 0;
    7486              :        // Actual input phase:
    7487              :        ...
    7488              :        r.0_5 = D.2042[_20];
    7489              :        _6 = _4 + r.0_5;
    7490              :        D.2042[_20] = _6;
    7491              :        // Initialization for scan phase:
    7492              :        _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 2);
    7493              :        _26 = D.2043[_25];
    7494              :        _27 = D.2042[_25];
    7495              :        _28 = _26 + _27;
    7496              :        D.2043[_25] = _28;
    7497              :        D.2042[_25] = _28;
    7498              :        // Actual scan phase:
    7499              :        ...
    7500              :        r.1_8 = D.2042[_20];
    7501              :        ...
    7502              :      The "omp simd array" variable D.2042 holds the privatized copy used
    7503              :      inside of the loop and D.2043 is another one that holds copies of
    7504              :      the current original list item.  The separate GOMP_SIMD_LANE ifn
    7505              :      kinds are there in order to allow optimizing the initializer store
    7506              :      and combiner sequence, e.g. if it is originally some C++ish user
    7507              :      defined reduction, but allow the vectorizer to pattern recognize it
    7508              :      and turn into the appropriate vectorized scan.
    7509              : 
    7510              :      For exclusive scan, this is slightly different:
    7511              :      #pragma omp simd reduction(inscan,+:r)
    7512              :      for (...)
    7513              :        {
    7514              :          use (r);
    7515              :          #pragma omp scan exclusive (r)
    7516              :          r += something ();
    7517              :        }
    7518              :      shall have body with:
    7519              :        // Initialization for input phase, store the reduction initializer:
    7520              :        _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
    7521              :        _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
    7522              :        D.2042[_21] = 0;
    7523              :        // Actual input phase:
    7524              :        ...
    7525              :        r.0_5 = D.2042[_20];
    7526              :        _6 = _4 + r.0_5;
    7527              :        D.2042[_20] = _6;
    7528              :        // Initialization for scan phase:
    7529              :        _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 3);
    7530              :        _26 = D.2043[_25];
    7531              :        D.2044[_25] = _26;
    7532              :        _27 = D.2042[_25];
    7533              :        _28 = _26 + _27;
    7534              :        D.2043[_25] = _28;
    7535              :        // Actual scan phase:
    7536              :        ...
    7537              :        r.1_8 = D.2044[_20];
    7538              :        ...  */
    7539              : 
    7540         1076 :   if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 2)
    7541              :     {
    7542              :       /* Match the D.2042[_21] = 0; store above.  Just require that
    7543              :          it is a constant or external definition store.  */
    7544          564 :       if (rhs_dt != vect_constant_def && rhs_dt != vect_external_def)
    7545              :         {
    7546            0 :          fail_init:
    7547            0 :           if (dump_enabled_p ())
    7548            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    7549              :                              "unsupported OpenMP scan initializer store.\n");
    7550            0 :           return false;
    7551              :         }
    7552              : 
    7553          564 :       if (! loop_vinfo->scan_map)
    7554          322 :         loop_vinfo->scan_map = new hash_map<tree, tree>;
    7555          564 :       tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
    7556          564 :       tree &cached = loop_vinfo->scan_map->get_or_insert (var);
    7557          564 :       if (cached)
    7558            0 :         goto fail_init;
    7559          564 :       cached = gimple_assign_rhs1 (STMT_VINFO_STMT (stmt_info));
    7560              : 
    7561              :       /* These stores can be vectorized normally.  */
    7562          564 :       return true;
    7563              :     }
    7564              : 
    7565          512 :   if (rhs_dt != vect_internal_def)
    7566              :     {
    7567            0 :      fail:
    7568            0 :       if (dump_enabled_p ())
    7569            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    7570              :                          "unsupported OpenMP scan combiner pattern.\n");
    7571            0 :       return false;
    7572              :     }
    7573              : 
    7574          512 :   gimple *stmt = STMT_VINFO_STMT (stmt_info);
    7575          512 :   tree rhs = gimple_assign_rhs1 (stmt);
    7576          512 :   if (TREE_CODE (rhs) != SSA_NAME)
    7577            0 :     goto fail;
    7578              : 
    7579          512 :   gimple *other_store_stmt = NULL;
    7580          512 :   tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
    7581          512 :   bool inscan_var_store
    7582          512 :     = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
    7583              : 
    7584          512 :   if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
    7585              :     {
    7586          252 :       if (!inscan_var_store)
    7587              :         {
    7588          126 :           use_operand_p use_p;
    7589          126 :           imm_use_iterator iter;
    7590          378 :           FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
    7591              :             {
    7592          252 :               gimple *use_stmt = USE_STMT (use_p);
    7593          252 :               if (use_stmt == stmt || is_gimple_debug (use_stmt))
    7594          126 :                 continue;
    7595          126 :               if (gimple_bb (use_stmt) != gimple_bb (stmt)
    7596          126 :                   || !is_gimple_assign (use_stmt)
    7597          126 :                   || gimple_assign_rhs_class (use_stmt) != GIMPLE_BINARY_RHS
    7598          126 :                   || other_store_stmt
    7599          252 :                   || TREE_CODE (gimple_assign_lhs (use_stmt)) != SSA_NAME)
    7600            0 :                 goto fail;
    7601          126 :               other_store_stmt = use_stmt;
    7602            0 :             }
    7603          126 :           if (other_store_stmt == NULL)
    7604            0 :             goto fail;
    7605          126 :           rhs = gimple_assign_lhs (other_store_stmt);
    7606          126 :           if (!single_imm_use (rhs, &use_p, &other_store_stmt))
    7607            0 :             goto fail;
    7608              :         }
    7609              :     }
    7610          260 :   else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3)
    7611              :     {
    7612          260 :       use_operand_p use_p;
    7613          260 :       imm_use_iterator iter;
    7614         1040 :       FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
    7615              :         {
    7616          520 :           gimple *use_stmt = USE_STMT (use_p);
    7617          520 :           if (use_stmt == stmt || is_gimple_debug (use_stmt))
    7618          260 :             continue;
    7619          260 :           if (other_store_stmt)
    7620            0 :             goto fail;
    7621          260 :           other_store_stmt = use_stmt;
    7622          260 :         }
    7623              :     }
    7624              :   else
    7625            0 :     goto fail;
    7626              : 
    7627          512 :   gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
    7628          512 :   if (gimple_bb (def_stmt) != gimple_bb (stmt)
    7629          512 :       || !is_gimple_assign (def_stmt)
    7630         1024 :       || gimple_assign_rhs_class (def_stmt) != GIMPLE_BINARY_RHS)
    7631            0 :     goto fail;
    7632              : 
    7633          512 :   enum tree_code code = gimple_assign_rhs_code (def_stmt);
    7634              :   /* For pointer addition, we should use the normal plus for the vector
    7635              :      operation.  */
    7636          512 :   switch (code)
    7637              :     {
    7638            0 :     case POINTER_PLUS_EXPR:
    7639            0 :       code = PLUS_EXPR;
    7640            0 :       break;
    7641            0 :     case MULT_HIGHPART_EXPR:
    7642            0 :       goto fail;
    7643              :     default:
    7644              :       break;
    7645              :     }
    7646          512 :   if (TREE_CODE_LENGTH (code) != binary_op || !commutative_tree_code (code))
    7647            0 :     goto fail;
    7648              : 
    7649          512 :   tree rhs1 = gimple_assign_rhs1 (def_stmt);
    7650          512 :   tree rhs2 = gimple_assign_rhs2 (def_stmt);
    7651          512 :   if (TREE_CODE (rhs1) != SSA_NAME || TREE_CODE (rhs2) != SSA_NAME)
    7652            0 :     goto fail;
    7653              : 
    7654          512 :   gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
    7655          512 :   gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
    7656          512 :   if (gimple_bb (load1_stmt) != gimple_bb (stmt)
    7657          512 :       || !gimple_assign_load_p (load1_stmt)
    7658          512 :       || gimple_bb (load2_stmt) != gimple_bb (stmt)
    7659         1024 :       || !gimple_assign_load_p (load2_stmt))
    7660            0 :     goto fail;
    7661              : 
    7662          512 :   stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
    7663          512 :   stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
    7664          512 :   if (load1_stmt_info == NULL
    7665          512 :       || load2_stmt_info == NULL
    7666          512 :       || (STMT_VINFO_SIMD_LANE_ACCESS_P (load1_stmt_info)
    7667          512 :           != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info))
    7668          512 :       || (STMT_VINFO_SIMD_LANE_ACCESS_P (load2_stmt_info)
    7669          512 :           != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
    7670            0 :     goto fail;
    7671              : 
    7672          512 :   if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && inscan_var_store)
    7673              :     {
    7674          126 :       dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
    7675          126 :       if (TREE_CODE (DR_BASE_ADDRESS (load1_dr_info->dr)) != ADDR_EXPR
    7676          126 :           || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0)))
    7677            0 :         goto fail;
    7678          126 :       tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
    7679          126 :       tree lrhs;
    7680          126 :       if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
    7681              :         lrhs = rhs1;
    7682              :       else
    7683           16 :         lrhs = rhs2;
    7684          126 :       use_operand_p use_p;
    7685          126 :       imm_use_iterator iter;
    7686          504 :       FOR_EACH_IMM_USE_FAST (use_p, iter, lrhs)
    7687              :         {
    7688          252 :           gimple *use_stmt = USE_STMT (use_p);
    7689          252 :           if (use_stmt == def_stmt || is_gimple_debug (use_stmt))
    7690          126 :             continue;
    7691          126 :           if (other_store_stmt)
    7692            0 :             goto fail;
    7693          126 :           other_store_stmt = use_stmt;
    7694          126 :         }
    7695              :     }
    7696              : 
    7697          512 :   if (other_store_stmt == NULL)
    7698            0 :     goto fail;
    7699          512 :   if (gimple_bb (other_store_stmt) != gimple_bb (stmt)
    7700          512 :       || !gimple_store_p (other_store_stmt))
    7701            0 :     goto fail;
    7702              : 
    7703          512 :   stmt_vec_info other_store_stmt_info
    7704          512 :     = loop_vinfo->lookup_stmt (other_store_stmt);
    7705          512 :   if (other_store_stmt_info == NULL
    7706          512 :       || (STMT_VINFO_SIMD_LANE_ACCESS_P (other_store_stmt_info)
    7707          512 :           != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
    7708            0 :     goto fail;
    7709              : 
    7710          512 :   gimple *stmt1 = stmt;
    7711          512 :   gimple *stmt2 = other_store_stmt;
    7712          512 :   if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
    7713              :     std::swap (stmt1, stmt2);
    7714          512 :   if (scan_operand_equal_p (gimple_assign_lhs (stmt1),
    7715              :                             gimple_assign_rhs1 (load2_stmt)))
    7716              :     {
    7717          162 :       std::swap (rhs1, rhs2);
    7718          162 :       std::swap (load1_stmt, load2_stmt);
    7719          162 :       std::swap (load1_stmt_info, load2_stmt_info);
    7720              :     }
    7721          512 :   if (!scan_operand_equal_p (gimple_assign_lhs (stmt1),
    7722              :                              gimple_assign_rhs1 (load1_stmt)))
    7723            0 :     goto fail;
    7724              : 
    7725          512 :   tree var3 = NULL_TREE;
    7726          512 :   if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3
    7727          512 :       && !scan_operand_equal_p (gimple_assign_lhs (stmt2),
    7728              :                                 gimple_assign_rhs1 (load2_stmt)))
    7729            0 :     goto fail;
    7730          512 :   else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
    7731              :     {
    7732          252 :       dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
    7733          252 :       if (TREE_CODE (DR_BASE_ADDRESS (load2_dr_info->dr)) != ADDR_EXPR
    7734          252 :           || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0)))
    7735            0 :         goto fail;
    7736          252 :       var3 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
    7737          252 :       if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var3))
    7738          252 :           || lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var3))
    7739          504 :           || lookup_attribute ("omp simd inscan exclusive",
    7740          252 :                                DECL_ATTRIBUTES (var3)))
    7741            0 :         goto fail;
    7742              :     }
    7743              : 
    7744          512 :   dr_vec_info *other_dr_info = STMT_VINFO_DR_INFO (other_store_stmt_info);
    7745          512 :   if (TREE_CODE (DR_BASE_ADDRESS (other_dr_info->dr)) != ADDR_EXPR
    7746          512 :       || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0)))
    7747            0 :     goto fail;
    7748              : 
    7749          512 :   tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
    7750          512 :   tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0);
    7751          512 :   if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var1))
    7752          512 :       || !lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var2))
    7753         1024 :       || (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
    7754          512 :          == (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var2))))
    7755            0 :     goto fail;
    7756              : 
    7757          512 :   if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
    7758          256 :     std::swap (var1, var2);
    7759              : 
    7760          512 :   if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
    7761              :     {
    7762          252 :       if (!lookup_attribute ("omp simd inscan exclusive",
    7763          252 :                              DECL_ATTRIBUTES (var1)))
    7764            0 :         goto fail;
    7765          252 :       var1 = var3;
    7766              :     }
    7767              : 
    7768          512 :   if (loop_vinfo->scan_map == NULL)
    7769            0 :     goto fail;
    7770          512 :   tree *init = loop_vinfo->scan_map->get (var1);
    7771          512 :   if (init == NULL)
    7772            0 :     goto fail;
    7773              : 
    7774              :   /* The IL is as expected, now check if we can actually vectorize it.
    7775              :      Inclusive scan:
    7776              :        _26 = D.2043[_25];
    7777              :        _27 = D.2042[_25];
    7778              :        _28 = _26 + _27;
    7779              :        D.2043[_25] = _28;
    7780              :        D.2042[_25] = _28;
    7781              :      should be vectorized as (where _40 is the vectorized rhs
    7782              :      from the D.2042[_21] = 0; store):
    7783              :        _30 = MEM <vector(8) int> [(int *)&D.2043];
    7784              :        _31 = MEM <vector(8) int> [(int *)&D.2042];
    7785              :        _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
    7786              :        _33 = _31 + _32;
    7787              :        // _33 = { _31[0], _31[0]+_31[1], _31[1]+_31[2], ..., _31[6]+_31[7] };
    7788              :        _34 = VEC_PERM_EXPR <_40, _33, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
    7789              :        _35 = _33 + _34;
    7790              :        // _35 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
    7791              :        //         _31[1]+.._31[4], ... _31[4]+.._31[7] };
    7792              :        _36 = VEC_PERM_EXPR <_40, _35, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
    7793              :        _37 = _35 + _36;
    7794              :        // _37 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
    7795              :        //         _31[0]+.._31[4], ... _31[0]+.._31[7] };
    7796              :        _38 = _30 + _37;
    7797              :        _39 = VEC_PERM_EXPR <_38, _38, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
    7798              :        MEM <vector(8) int> [(int *)&D.2043] = _39;
    7799              :        MEM <vector(8) int> [(int *)&D.2042] = _38;
    7800              :      Exclusive scan:
    7801              :        _26 = D.2043[_25];
    7802              :        D.2044[_25] = _26;
    7803              :        _27 = D.2042[_25];
    7804              :        _28 = _26 + _27;
    7805              :        D.2043[_25] = _28;
    7806              :      should be vectorized as (where _40 is the vectorized rhs
    7807              :      from the D.2042[_21] = 0; store):
    7808              :        _30 = MEM <vector(8) int> [(int *)&D.2043];
    7809              :        _31 = MEM <vector(8) int> [(int *)&D.2042];
    7810              :        _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
    7811              :        _33 = VEC_PERM_EXPR <_40, _32, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
    7812              :        _34 = _32 + _33;
    7813              :        // _34 = { 0, _31[0], _31[0]+_31[1], _31[1]+_31[2], _31[2]+_31[3],
    7814              :        //         _31[3]+_31[4], ... _31[5]+.._31[6] };
    7815              :        _35 = VEC_PERM_EXPR <_40, _34, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
    7816              :        _36 = _34 + _35;
    7817              :        // _36 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
    7818              :        //         _31[1]+.._31[4], ... _31[3]+.._31[6] };
    7819              :        _37 = VEC_PERM_EXPR <_40, _36, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
    7820              :        _38 = _36 + _37;
    7821              :        // _38 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
    7822              :        //         _31[0]+.._31[4], ... _31[0]+.._31[6] };
    7823              :        _39 = _30 + _38;
    7824              :        _50 = _31 + _39;
    7825              :        _51 = VEC_PERM_EXPR <_50, _50, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
    7826              :        MEM <vector(8) int> [(int *)&D.2044] = _39;
    7827              :        MEM <vector(8) int> [(int *)&D.2042] = _51;  */
    7828          512 :   enum machine_mode vec_mode = TYPE_MODE (vectype);
    7829          512 :   optab optab = optab_for_tree_code (code, vectype, optab_default);
    7830          512 :   if (!optab || !can_implement_p (optab, vec_mode))
    7831            0 :     goto fail;
    7832              : 
    7833          512 :   int units_log2 = scan_store_can_perm_p (vectype, *init);
    7834          512 :   if (units_log2 == -1)
    7835            0 :     goto fail;
    7836              : 
    7837              :   return true;
    7838              : }
    7839              : 
    7840              : 
    7841              : /* Function vectorizable_scan_store.
    7842              : 
    7843              :    Helper of vectorizable_score, arguments like on vectorizable_store.
    7844              :    Handle only the transformation, checking is done in check_scan_store.  */
    7845              : 
    7846              : static bool
    7847          512 : vectorizable_scan_store (vec_info *vinfo, stmt_vec_info stmt_info,
    7848              :                          slp_tree slp_node, gimple_stmt_iterator *gsi)
    7849              : {
    7850          512 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    7851          512 :   dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
    7852          512 :   tree ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
    7853          512 :   tree vectype = SLP_TREE_VECTYPE (slp_node);
    7854              : 
    7855          512 :   if (dump_enabled_p ())
    7856          492 :     dump_printf_loc (MSG_NOTE, vect_location,
    7857              :                      "transform scan store.\n");
    7858              : 
    7859          512 :   gimple *stmt = STMT_VINFO_STMT (stmt_info);
    7860          512 :   tree rhs = gimple_assign_rhs1 (stmt);
    7861          512 :   gcc_assert (TREE_CODE (rhs) == SSA_NAME);
    7862              : 
    7863          512 :   tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
    7864          512 :   bool inscan_var_store
    7865          512 :     = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
    7866              : 
    7867          512 :   if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
    7868              :     {
    7869          126 :       use_operand_p use_p;
    7870          126 :       imm_use_iterator iter;
    7871          252 :       FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
    7872              :         {
    7873          126 :           gimple *use_stmt = USE_STMT (use_p);
    7874          126 :           if (use_stmt == stmt || is_gimple_debug (use_stmt))
    7875            0 :             continue;
    7876          126 :           rhs = gimple_assign_lhs (use_stmt);
    7877          126 :           break;
    7878          126 :         }
    7879              :     }
    7880              : 
    7881          512 :   gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
    7882          512 :   enum tree_code code = gimple_assign_rhs_code (def_stmt);
    7883          512 :   if (code == POINTER_PLUS_EXPR)
    7884            0 :     code = PLUS_EXPR;
    7885          512 :   gcc_assert (TREE_CODE_LENGTH (code) == binary_op
    7886              :               && commutative_tree_code (code));
    7887          512 :   tree rhs1 = gimple_assign_rhs1 (def_stmt);
    7888          512 :   tree rhs2 = gimple_assign_rhs2 (def_stmt);
    7889          512 :   gcc_assert (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME);
    7890          512 :   gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
    7891          512 :   gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
    7892          512 :   stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
    7893          512 :   stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
    7894          512 :   dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
    7895          512 :   dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
    7896          512 :   tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
    7897          512 :   tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
    7898              : 
    7899          512 :   if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
    7900              :     {
    7901          436 :       std::swap (rhs1, rhs2);
    7902          436 :       std::swap (var1, var2);
    7903          436 :       std::swap (load1_dr_info, load2_dr_info);
    7904              :     }
    7905              : 
    7906          512 :   tree *init = loop_vinfo->scan_map->get (var1);
    7907          512 :   gcc_assert (init);
    7908              : 
    7909          512 :   unsigned HOST_WIDE_INT nunits;
    7910          512 :   if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
    7911              :     gcc_unreachable ();
    7912          512 :   auto_vec<enum scan_store_kind, 16> use_whole_vector;
    7913          512 :   int units_log2 = scan_store_can_perm_p (vectype, *init, &use_whole_vector);
    7914          512 :   gcc_assert (units_log2 > 0);
    7915          512 :   auto_vec<tree, 16> perms;
    7916          512 :   perms.quick_grow (units_log2 + 1);
    7917          512 :   tree zero_vec = NULL_TREE, masktype = NULL_TREE;
    7918         2392 :   for (int i = 0; i <= units_log2; ++i)
    7919              :     {
    7920         1880 :       unsigned HOST_WIDE_INT j, k;
    7921         1880 :       vec_perm_builder sel (nunits, nunits, 1);
    7922         1880 :       sel.quick_grow (nunits);
    7923         1880 :       if (i == units_log2)
    7924         4864 :         for (j = 0; j < nunits; ++j)
    7925         4352 :           sel[j] = nunits - 1;
    7926              :       else
    7927              :         {
    7928         5208 :           for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
    7929         3840 :             sel[j] = j;
    7930        13208 :           for (k = 0; j < nunits; ++j, ++k)
    7931        11840 :             sel[j] = nunits + k;
    7932              :         }
    7933         3248 :       vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
    7934         1880 :       if (!use_whole_vector.is_empty ()
    7935            0 :           && use_whole_vector[i] != scan_store_kind_perm)
    7936              :         {
    7937            0 :           if (zero_vec == NULL_TREE)
    7938            0 :             zero_vec = build_zero_cst (vectype);
    7939            0 :           if (masktype == NULL_TREE
    7940            0 :               && use_whole_vector[i] == scan_store_kind_lshift_cond)
    7941            0 :             masktype = truth_type_for (vectype);
    7942            0 :           perms[i] = vect_gen_perm_mask_any (vectype, indices);
    7943              :         }
    7944              :       else
    7945         1880 :         perms[i] = vect_gen_perm_mask_checked (vectype, indices);
    7946         1880 :     }
    7947              : 
    7948          512 :   vec_loop_lens *loop_lens
    7949          512 :     = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
    7950              :        ? &LOOP_VINFO_LENS (loop_vinfo)
    7951            0 :        : NULL);
    7952              : 
    7953          512 :   tree vec_oprnd1 = NULL_TREE;
    7954          512 :   tree vec_oprnd2 = NULL_TREE;
    7955          512 :   tree vec_oprnd3 = NULL_TREE;
    7956          512 :   tree dataref_ptr = DR_BASE_ADDRESS (dr_info->dr);
    7957          512 :   tree dataref_offset = build_int_cst (ref_type, 0);
    7958          512 :   tree bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info,
    7959              :                                            vectype, VMAT_CONTIGUOUS,
    7960              :                                            loop_lens);
    7961          512 :   tree ldataref_ptr = NULL_TREE;
    7962          512 :   tree orig = NULL_TREE;
    7963          512 :   if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
    7964          126 :     ldataref_ptr = DR_BASE_ADDRESS (load1_dr_info->dr);
    7965              :   /* The initialization is invariant.  */
    7966          512 :   vec_oprnd1 = vect_init_vector (vinfo, stmt_info, *init, vectype, NULL);
    7967          512 :   auto_vec<tree> vec_oprnds2;
    7968          512 :   auto_vec<tree> vec_oprnds3;
    7969          512 :   if (ldataref_ptr == NULL)
    7970              :     {
    7971              :       /* We want to lookup the vector operands of the reduction, not those
    7972              :          of the store - for SLP we have to use the proper SLP node for the
    7973              :          lookup, which should be the single child of the scan store.  */
    7974          386 :       vect_get_vec_defs (vinfo, SLP_TREE_CHILDREN (slp_node)[0],
    7975              :                          rhs1, &vec_oprnds2, rhs2, &vec_oprnds3);
    7976              :       /* ???  For SLP we do not key the def on 'rhs1' or 'rhs2' but get
    7977              :          them in SLP child order.  So we have to swap here with logic
    7978              :          similar to above.  */
    7979          386 :       stmt_vec_info load
    7980          386 :         = SLP_TREE_SCALAR_STMTS (SLP_TREE_CHILDREN
    7981          386 :                                    (SLP_TREE_CHILDREN (slp_node)[0])[0])[0];
    7982          386 :       dr_vec_info *dr_info = STMT_VINFO_DR_INFO (load);
    7983          386 :       tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
    7984          386 :       if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)))
    7985          820 :         for (unsigned i = 0; i < vec_oprnds2.length (); ++i)
    7986          494 :           std::swap (vec_oprnds2[i], vec_oprnds3[i]);;
    7987              :     }
    7988              :   else
    7989          126 :     vect_get_vec_defs (vinfo, slp_node,
    7990              :                        rhs2, &vec_oprnds3);
    7991         1248 :   for (unsigned j = 0; j < vec_oprnds3.length (); j++)
    7992              :     {
    7993          736 :       if (ldataref_ptr == NULL)
    7994          554 :         vec_oprnd2 = vec_oprnds2[j];
    7995          736 :       vec_oprnd3 = vec_oprnds3[j];
    7996          736 :       if (j == 0)
    7997              :         orig = vec_oprnd3;
    7998          224 :       else if (!inscan_var_store)
    7999          112 :         dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
    8000              : 
    8001          736 :       if (ldataref_ptr)
    8002              :         {
    8003          182 :           vec_oprnd2 = make_ssa_name (vectype);
    8004          182 :           tree data_ref = fold_build2 (MEM_REF, vectype,
    8005              :                                        unshare_expr (ldataref_ptr),
    8006              :                                        dataref_offset);
    8007          182 :           vect_copy_ref_info (data_ref, DR_REF (load1_dr_info->dr));
    8008          182 :           gimple *g = gimple_build_assign (vec_oprnd2, data_ref);
    8009          182 :           vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
    8010              :         }
    8011              : 
    8012          736 :       tree v = vec_oprnd2;
    8013         3068 :       for (int i = 0; i < units_log2; ++i)
    8014              :         {
    8015         2332 :           tree new_temp = make_ssa_name (vectype);
    8016         2332 :           gimple *g = gimple_build_assign (new_temp, VEC_PERM_EXPR,
    8017              :                                            (zero_vec
    8018            0 :                                             && (use_whole_vector[i]
    8019            0 :                                                 != scan_store_kind_perm))
    8020              :                                            ? zero_vec : vec_oprnd1, v,
    8021         2332 :                                            perms[i]);
    8022         2332 :           vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
    8023              : 
    8024         2332 :           if (zero_vec && use_whole_vector[i] == scan_store_kind_lshift_cond)
    8025              :             {
    8026              :               /* Whole vector shift shifted in zero bits, but if *init
    8027              :                  is not initializer_zerop, we need to replace those elements
    8028              :                  with elements from vec_oprnd1.  */
    8029            0 :               tree_vector_builder vb (masktype, nunits, 1);
    8030            0 :               for (unsigned HOST_WIDE_INT k = 0; k < nunits; ++k)
    8031            0 :                 vb.quick_push (k < (HOST_WIDE_INT_1U << i)
    8032              :                                ? boolean_false_node : boolean_true_node);
    8033              : 
    8034            0 :               tree new_temp2 = make_ssa_name (vectype);
    8035            0 :               g = gimple_build_assign (new_temp2, VEC_COND_EXPR, vb.build (),
    8036              :                                        new_temp, vec_oprnd1);
    8037            0 :               vect_finish_stmt_generation (vinfo, stmt_info,
    8038              :                                                            g, gsi);
    8039            0 :               new_temp = new_temp2;
    8040            0 :             }
    8041              : 
    8042              :           /* For exclusive scan, perform the perms[i] permutation once
    8043              :              more.  */
    8044         2332 :           if (i == 0
    8045         1100 :               && STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4
    8046          728 :               && v == vec_oprnd2)
    8047              :             {
    8048          364 :               v = new_temp;
    8049          364 :               --i;
    8050          364 :               continue;
    8051              :             }
    8052              : 
    8053         1968 :           tree new_temp2 = make_ssa_name (vectype);
    8054         1968 :           g = gimple_build_assign (new_temp2, code, v, new_temp);
    8055         1968 :           vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
    8056              : 
    8057         1968 :           v = new_temp2;
    8058              :         }
    8059              : 
    8060          736 :       tree new_temp = make_ssa_name (vectype);
    8061          736 :       gimple *g = gimple_build_assign (new_temp, code, orig, v);
    8062          736 :       vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
    8063              : 
    8064          736 :       tree last_perm_arg = new_temp;
    8065              :       /* For exclusive scan, new_temp computed above is the exclusive scan
    8066              :          prefix sum.  Turn it into inclusive prefix sum for the broadcast
    8067              :          of the last element into orig.  */
    8068          736 :       if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
    8069              :         {
    8070          364 :           last_perm_arg = make_ssa_name (vectype);
    8071          364 :           g = gimple_build_assign (last_perm_arg, code, new_temp, vec_oprnd2);
    8072          364 :           vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
    8073              :         }
    8074              : 
    8075          736 :       orig = make_ssa_name (vectype);
    8076         2208 :       g = gimple_build_assign (orig, VEC_PERM_EXPR, last_perm_arg,
    8077          736 :                                last_perm_arg, perms[units_log2]);
    8078          736 :       vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
    8079              : 
    8080          736 :       if (!inscan_var_store)
    8081              :         {
    8082          368 :           tree data_ref = fold_build2 (MEM_REF, vectype,
    8083              :                                        unshare_expr (dataref_ptr),
    8084              :                                        dataref_offset);
    8085          368 :           vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
    8086          368 :           g = gimple_build_assign (data_ref, new_temp);
    8087          368 :           vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
    8088              :         }
    8089              :     }
    8090              : 
    8091          512 :   if (inscan_var_store)
    8092          624 :     for (unsigned j = 0; j < vec_oprnds3.length (); j++)
    8093              :       {
    8094          368 :         if (j != 0)
    8095          112 :           dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
    8096              : 
    8097          368 :         tree data_ref = fold_build2 (MEM_REF, vectype,
    8098              :                                      unshare_expr (dataref_ptr),
    8099              :                                      dataref_offset);
    8100          368 :         vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
    8101          368 :         gimple *g = gimple_build_assign (data_ref, orig);
    8102          368 :         vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
    8103              :       }
    8104          512 :   return true;
    8105          512 : }
    8106              : 
    8107              : 
    8108              : /* Function vectorizable_store.
    8109              : 
    8110              :    Check if STMT_INFO defines a non scalar data-ref (array/pointer/structure)
    8111              :    that can be vectorized.
    8112              :    If COST_VEC is passed, calculate costs but don't change anything,
    8113              :    otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
    8114              :    it, and insert it at GSI.
    8115              :    Return true if STMT_INFO is vectorizable in this way.  */
    8116              : 
    8117              : static bool
    8118      2086142 : vectorizable_store (vec_info *vinfo,
    8119              :                     stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
    8120              :                     slp_tree slp_node,
    8121              :                     stmt_vector_for_cost *cost_vec)
    8122              : {
    8123      2086142 :   tree data_ref;
    8124      2086142 :   tree vec_oprnd = NULL_TREE;
    8125      2086142 :   tree elem_type;
    8126      2086142 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    8127      2086142 :   class loop *loop = NULL;
    8128      2086142 :   machine_mode vec_mode;
    8129      2086142 :   tree dummy;
    8130      2086142 :   enum vect_def_type rhs_dt = vect_unknown_def_type;
    8131      2086142 :   enum vect_def_type mask_dt = vect_unknown_def_type;
    8132      2086142 :   tree dataref_ptr = NULL_TREE;
    8133      2086142 :   tree dataref_offset = NULL_TREE;
    8134      2086142 :   gimple *ptr_incr = NULL;
    8135      2086142 :   int j;
    8136      2086142 :   stmt_vec_info first_stmt_info;
    8137      2086142 :   bool grouped_store;
    8138      2086142 :   unsigned int group_size, i;
    8139      2086142 :   unsigned int vec_num;
    8140      2086142 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
    8141      2086142 :   tree aggr_type;
    8142      2086142 :   poly_uint64 vf;
    8143      2086142 :   vec_load_store_type vls_type;
    8144      2086142 :   tree ref_type;
    8145              : 
    8146      2086142 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
    8147              :     return false;
    8148              : 
    8149      2086142 :   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
    8150       234770 :       && cost_vec)
    8151              :     return false;
    8152              : 
    8153              :   /* Is vectorizable store? */
    8154              : 
    8155      1851372 :   tree mask_vectype = NULL_TREE;
    8156      1851372 :   slp_tree mask_node = NULL;
    8157      1851372 :   if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
    8158              :     {
    8159      1779790 :       tree scalar_dest = gimple_assign_lhs (assign);
    8160      1779790 :       if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
    8161      1779790 :           && is_pattern_stmt_p (stmt_info))
    8162         1672 :         scalar_dest = TREE_OPERAND (scalar_dest, 0);
    8163      1779790 :       if (TREE_CODE (scalar_dest) != ARRAY_REF
    8164      1779790 :           && TREE_CODE (scalar_dest) != BIT_FIELD_REF
    8165              :           && TREE_CODE (scalar_dest) != INDIRECT_REF
    8166              :           && TREE_CODE (scalar_dest) != COMPONENT_REF
    8167              :           && TREE_CODE (scalar_dest) != IMAGPART_EXPR
    8168              :           && TREE_CODE (scalar_dest) != REALPART_EXPR
    8169              :           && TREE_CODE (scalar_dest) != MEM_REF)
    8170              :         return false;
    8171              :     }
    8172              :   else
    8173              :     {
    8174       733638 :       gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
    8175        12455 :       if (!call || !gimple_call_internal_p (call))
    8176              :         return false;
    8177              : 
    8178         8330 :       internal_fn ifn = gimple_call_internal_fn (call);
    8179         8330 :       if (!internal_store_fn_p (ifn))
    8180              :         return false;
    8181              : 
    8182         1901 :       int mask_index = internal_fn_mask_index (ifn);
    8183         1901 :       if (mask_index >= 0)
    8184         1901 :         mask_index = vect_slp_child_index_for_operand (stmt_info, mask_index);
    8185         1901 :       if (mask_index >= 0
    8186         1901 :           && !vect_check_scalar_mask (vinfo, slp_node, mask_index,
    8187              :                                       &mask_node, &mask_dt,
    8188              :                                       &mask_vectype))
    8189              :         return false;
    8190              :     }
    8191              : 
    8192      1364991 :   tree vectype = SLP_TREE_VECTYPE (slp_node), rhs_vectype = NULL_TREE;
    8193      1364991 :   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
    8194              : 
    8195      1364991 :   if (loop_vinfo)
    8196              :     {
    8197       226996 :       loop = LOOP_VINFO_LOOP (loop_vinfo);
    8198       226996 :       vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
    8199              :     }
    8200              :   else
    8201              :     vf = 1;
    8202      1364991 :   vec_num = vect_get_num_copies (vinfo, slp_node);
    8203              : 
    8204              :   /* FORNOW.  This restriction should be relaxed.  */
    8205      1364991 :   if (loop
    8206      1365266 :       && nested_in_vect_loop_p (loop, stmt_info)
    8207      1365274 :       && vec_num > 1)
    8208              :     {
    8209            8 :       if (dump_enabled_p ())
    8210            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    8211              :                          "multiple types in nested loop.\n");
    8212            8 :       return false;
    8213              :     }
    8214              : 
    8215      1364983 :   slp_tree op_node;
    8216      1364983 :   if (!vect_check_store_rhs (vinfo, stmt_info, slp_node,
    8217              :                              &op_node, &rhs_dt, &rhs_vectype, &vls_type))
    8218              :     return false;
    8219              : 
    8220      1364959 :   elem_type = TREE_TYPE (vectype);
    8221      1364959 :   vec_mode = TYPE_MODE (vectype);
    8222              : 
    8223      1364959 :   if (!STMT_VINFO_DATA_REF (stmt_info))
    8224              :     return false;
    8225              : 
    8226      1364959 :   vect_load_store_data _ls_data{};
    8227      1364959 :   vect_load_store_data &ls = slp_node->get_data (_ls_data);
    8228      1364959 :   if (cost_vec
    8229      1364959 :       && !get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask_node,
    8230              :                                vls_type, &_ls_data))
    8231              :     return false;
    8232              :   /* Temporary aliases to analysis data, should not be modified through
    8233              :      these.  */
    8234      1364383 :   const vect_memory_access_type memory_access_type = ls.memory_access_type;
    8235      1364383 :   const dr_alignment_support alignment_support_scheme
    8236              :     = ls.alignment_support_scheme;
    8237      1364383 :   const int misalignment = ls.misalignment;
    8238      1364383 :   const poly_int64 poffset = ls.poffset;
    8239              : 
    8240      1364383 :   if (slp_node->ldst_lanes
    8241            0 :       && memory_access_type != VMAT_LOAD_STORE_LANES)
    8242              :     {
    8243            0 :       if (dump_enabled_p ())
    8244            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    8245              :                          "discovered store-lane but cannot use it.\n");
    8246            0 :       return false;
    8247              :     }
    8248              : 
    8249      1364383 :   if (mask_node)
    8250              :     {
    8251         1811 :       if (memory_access_type == VMAT_CONTIGUOUS)
    8252              :         {
    8253          616 :           if (!VECTOR_MODE_P (vec_mode)
    8254         3090 :               || !can_vec_mask_load_store_p (vec_mode,
    8255         1545 :                                              TYPE_MODE (mask_vectype), false))
    8256          114 :             return false;
    8257              :         }
    8258          266 :       else if (memory_access_type != VMAT_LOAD_STORE_LANES
    8259          266 :                && (!mat_gather_scatter_p (memory_access_type)
    8260          242 :                    || (memory_access_type == VMAT_GATHER_SCATTER_LEGACY
    8261          170 :                        && !VECTOR_BOOLEAN_TYPE_P (mask_vectype))))
    8262              :         {
    8263           24 :           if (dump_enabled_p ())
    8264           24 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    8265              :                              "unsupported access type for masked store.\n");
    8266           24 :           return false;
    8267              :         }
    8268          242 :       else if (memory_access_type == VMAT_GATHER_SCATTER_EMULATED)
    8269              :         {
    8270           72 :           if (dump_enabled_p ())
    8271           24 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    8272              :                              "unsupported masked emulated scatter.\n");
    8273           72 :           return false;
    8274              :         }
    8275              :     }
    8276              :   else
    8277              :     {
    8278              :       /* FORNOW. In some cases can vectorize even if data-type not supported
    8279              :          (e.g. - array initialization with 0).  */
    8280      1362572 :       if (!can_implement_p (mov_optab, vec_mode))
    8281              :         return false;
    8282              :     }
    8283              : 
    8284      1364173 :   dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
    8285      1364173 :   grouped_store = (STMT_VINFO_GROUPED_ACCESS (stmt_info)
    8286      2522152 :                    && !mat_gather_scatter_p (memory_access_type));
    8287      1157979 :   if (grouped_store)
    8288              :     {
    8289      1157979 :       first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
    8290      1157979 :       first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
    8291      1157979 :       group_size = DR_GROUP_SIZE (first_stmt_info);
    8292              :     }
    8293              :   else
    8294              :     {
    8295      1364173 :       first_stmt_info = stmt_info;
    8296      1364173 :       first_dr_info = dr_info;
    8297              :       group_size = 1;
    8298              :     }
    8299              : 
    8300      1364173 :   if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1 && cost_vec)
    8301              :     {
    8302         1076 :       if (!check_scan_store (vinfo, stmt_info, vectype, rhs_dt, slp_node,
    8303              :                              mask_node, memory_access_type))
    8304              :         return false;
    8305              :     }
    8306              : 
    8307      2727578 :   bool costing_p = cost_vec;
    8308      1363405 :   if (costing_p) /* transformation not required.  */
    8309              :     {
    8310       817276 :       if (loop_vinfo
    8311       163270 :           && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
    8312        76549 :         check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
    8313              :                                               vls_type, group_size, &ls,
    8314              :                                               mask_node);
    8315              : 
    8316       817276 :       if (!vect_maybe_update_slp_op_vectype (op_node, vectype)
    8317       817276 :           || (mask_node
    8318         1056 :               && !vect_maybe_update_slp_op_vectype (mask_node,
    8319              :                                                     mask_vectype)))
    8320              :         {
    8321            0 :           if (dump_enabled_p ())
    8322            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    8323              :                              "incompatible vector types for invariants\n");
    8324            0 :           return false;
    8325              :         }
    8326              : 
    8327       817276 :       if (dump_enabled_p ()
    8328              :           && memory_access_type != VMAT_ELEMENTWISE
    8329        15070 :           && memory_access_type != VMAT_STRIDED_SLP
    8330        14394 :           && memory_access_type != VMAT_INVARIANT
    8331       831670 :           && alignment_support_scheme != dr_aligned)
    8332         4983 :         dump_printf_loc (MSG_NOTE, vect_location,
    8333              :                          "Vectorizing an unaligned access.\n");
    8334              :     }
    8335              : 
    8336              :   /* Transform.  */
    8337              : 
    8338      1364173 :   ensure_base_align (dr_info);
    8339              : 
    8340      1364173 :   if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) >= 3)
    8341              :     {
    8342         1024 :       gcc_assert (memory_access_type == VMAT_CONTIGUOUS);
    8343         1024 :       gcc_assert (SLP_TREE_LANES (slp_node) == 1);
    8344         1024 :       if (costing_p)
    8345              :         {
    8346          512 :           unsigned int inside_cost = 0, prologue_cost = 0;
    8347          512 :           if (vls_type == VLS_STORE_INVARIANT)
    8348            0 :             prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
    8349              :                                                slp_node, 0, vect_prologue);
    8350          512 :           vect_get_store_cost (vinfo, stmt_info, slp_node, 1,
    8351              :                                alignment_support_scheme, misalignment,
    8352              :                                &inside_cost, cost_vec);
    8353              : 
    8354          512 :           if (dump_enabled_p ())
    8355          492 :             dump_printf_loc (MSG_NOTE, vect_location,
    8356              :                              "vect_model_store_cost: inside_cost = %d, "
    8357              :                              "prologue_cost = %d .\n",
    8358              :                              inside_cost, prologue_cost);
    8359              : 
    8360          512 :           SLP_TREE_TYPE (slp_node) = store_vec_info_type;
    8361          512 :           slp_node->data = new vect_load_store_data (std::move (ls));
    8362              : 
    8363          512 :           return true;
    8364              :         }
    8365          512 :       return vectorizable_scan_store (vinfo, stmt_info, slp_node, gsi);
    8366              :     }
    8367              : 
    8368              :   /* FORNOW */
    8369      1363149 :   gcc_assert (!grouped_store
    8370              :               || !loop
    8371              :               || !nested_in_vect_loop_p (loop, stmt_info));
    8372              : 
    8373      1363149 :   grouped_store = false;
    8374      1363149 :   first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
    8375      1363149 :   gcc_assert (!STMT_VINFO_GROUPED_ACCESS (first_stmt_info)
    8376              :               || (DR_GROUP_FIRST_ELEMENT (first_stmt_info) == first_stmt_info));
    8377      1363149 :   first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
    8378              : 
    8379      1363149 :   ref_type = get_group_alias_ptr_type (first_stmt_info);
    8380              : 
    8381      1363149 :   if (!costing_p && dump_enabled_p ())
    8382        12245 :     dump_printf_loc (MSG_NOTE, vect_location, "transform store.\n");
    8383              : 
    8384      1363149 :   if (memory_access_type == VMAT_ELEMENTWISE
    8385      1363149 :       || memory_access_type == VMAT_STRIDED_SLP)
    8386              :     {
    8387        29835 :       unsigned inside_cost = 0, prologue_cost = 0;
    8388        29835 :       gimple_stmt_iterator incr_gsi;
    8389        29835 :       bool insert_after;
    8390        29835 :       tree offvar = NULL_TREE;
    8391        29835 :       tree ivstep;
    8392        29835 :       tree running_off;
    8393        29835 :       tree stride_base, stride_step, alias_off;
    8394        29835 :       tree vec_oprnd = NULL_TREE;
    8395        29835 :       tree dr_offset;
    8396              :       /* Checked by get_load_store_type.  */
    8397        29835 :       unsigned int const_nunits = nunits.to_constant ();
    8398              : 
    8399        29835 :       gcc_assert (!LOOP_VINFO_FULLY_MASKED_P (loop_vinfo));
    8400        29835 :       gcc_assert (!nested_in_vect_loop_p (loop, stmt_info));
    8401              : 
    8402        29835 :       dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
    8403        29835 :       stride_base
    8404        29835 :         = fold_build_pointer_plus
    8405              :             (DR_BASE_ADDRESS (first_dr_info->dr),
    8406              :              size_binop (PLUS_EXPR,
    8407              :                          convert_to_ptrofftype (dr_offset),
    8408              :                          convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
    8409        29835 :       stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
    8410              : 
    8411              :       /* For a store with loop-invariant (but other than power-of-2)
    8412              :          stride (i.e. not a grouped access) like so:
    8413              : 
    8414              :            for (i = 0; i < n; i += stride)
    8415              :              array[i] = ...;
    8416              : 
    8417              :          we generate a new induction variable and new stores from
    8418              :          the components of the (vectorized) rhs:
    8419              : 
    8420              :            for (j = 0; ; j += VF*stride)
    8421              :              vectemp = ...;
    8422              :              tmp1 = vectemp[0];
    8423              :              array[j] = tmp1;
    8424              :              tmp2 = vectemp[1];
    8425              :              array[j + stride] = tmp2;
    8426              :              ...
    8427              :          */
    8428              : 
    8429              :       /* ???  Modify local copies of alignment_support_scheme and
    8430              :          misalignment, but this part of analysis should be done
    8431              :          earlier and remembered, likewise the chosen load mode.  */
    8432        29835 :       const dr_alignment_support tem = alignment_support_scheme;
    8433        29835 :       dr_alignment_support alignment_support_scheme = tem;
    8434        29835 :       const int tem2 = misalignment;
    8435        29835 :       int misalignment = tem2;
    8436              : 
    8437        29835 :       unsigned nstores = const_nunits;
    8438        29835 :       unsigned lnel = 1;
    8439        29835 :       tree ltype = elem_type;
    8440        29835 :       tree lvectype = vectype;
    8441        29835 :       HOST_WIDE_INT n = gcd (group_size, const_nunits);
    8442        29835 :       if (n == const_nunits)
    8443              :         {
    8444         2934 :           int mis_align = dr_misalignment (first_dr_info, vectype);
    8445              :           /* With VF > 1 we advance the DR by step, if that is constant
    8446              :              and only aligned when performed VF times, DR alignment
    8447              :              analysis can analyze this as aligned since it assumes
    8448              :              contiguous accesses.  But that is not how we code generate
    8449              :              here, so adjust for this.  */
    8450         2934 :           if (maybe_gt (vf, 1u)
    8451         4457 :               && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
    8452         4228 :                               DR_TARGET_ALIGNMENT (first_dr_info)))
    8453          229 :             mis_align = -1;
    8454         2934 :           dr_alignment_support dr_align
    8455         2934 :               = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
    8456              :                                                mis_align);
    8457         2934 :           if (dr_align == dr_aligned
    8458         2934 :               || dr_align == dr_unaligned_supported)
    8459              :             {
    8460        29835 :               nstores = 1;
    8461        29835 :               lnel = const_nunits;
    8462        29835 :               ltype = vectype;
    8463        29835 :               lvectype = vectype;
    8464        29835 :               alignment_support_scheme = dr_align;
    8465        29835 :               misalignment = mis_align;
    8466              :             }
    8467              :         }
    8468        26901 :       else if (n > 1)
    8469              :         {
    8470         2061 :           nstores = const_nunits / n;
    8471         2061 :           lnel = n;
    8472         2061 :           ltype = build_vector_type (elem_type, n);
    8473         2061 :           lvectype = vectype;
    8474         2061 :           int mis_align = dr_misalignment (first_dr_info, ltype);
    8475         2061 :           if (maybe_gt (vf, 1u)
    8476         4122 :               && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
    8477         3426 :                               DR_TARGET_ALIGNMENT (first_dr_info)))
    8478          696 :             mis_align = -1;
    8479         2061 :           dr_alignment_support dr_align
    8480         2061 :             = vect_supportable_dr_alignment (vinfo, dr_info, ltype,
    8481              :                                              mis_align);
    8482         2061 :           alignment_support_scheme = dr_align;
    8483         2061 :           misalignment = mis_align;
    8484              : 
    8485              :           /* First check if vec_extract optab doesn't support extraction
    8486              :              of vector elts directly.  */
    8487         2061 :           scalar_mode elmode = SCALAR_TYPE_MODE (elem_type);
    8488         2061 :           machine_mode vmode;
    8489         4122 :           if (!VECTOR_MODE_P (TYPE_MODE (vectype))
    8490         2243 :               || !related_vector_mode (TYPE_MODE (vectype), elmode,
    8491         2061 :                                        n).exists (&vmode)
    8492         1845 :               || (convert_optab_handler (vec_extract_optab,
    8493         1845 :                                          TYPE_MODE (vectype), vmode)
    8494              :                   == CODE_FOR_nothing)
    8495         2061 :               || !(dr_align == dr_aligned
    8496          182 :                    || dr_align == dr_unaligned_supported))
    8497              :             {
    8498              :               /* Try to avoid emitting an extract of vector elements
    8499              :                  by performing the extracts using an integer type of the
    8500              :                  same size, extracting from a vector of those and then
    8501              :                  re-interpreting it as the original vector type if
    8502              :                  supported.  */
    8503         1879 :               unsigned lsize = n * GET_MODE_BITSIZE (elmode);
    8504         1879 :               unsigned int lnunits = const_nunits / n;
    8505              :               /* If we can't construct such a vector fall back to
    8506              :                  element extracts from the original vector type and
    8507              :                  element size stores.  */
    8508         1879 :               if (int_mode_for_size (lsize, 0).exists (&elmode)
    8509         1879 :                   && VECTOR_MODE_P (TYPE_MODE (vectype))
    8510         1879 :                   && related_vector_mode (TYPE_MODE (vectype), elmode,
    8511         1879 :                                           lnunits).exists (&vmode)
    8512         1853 :                   && (convert_optab_handler (vec_extract_optab,
    8513              :                                              vmode, elmode)
    8514              :                       != CODE_FOR_nothing))
    8515              :                 {
    8516         1853 :                   nstores = lnunits;
    8517         1853 :                   lnel = n;
    8518         1853 :                   ltype = build_nonstandard_integer_type (lsize, 1);
    8519         1853 :                   lvectype = build_vector_type (ltype, nstores);
    8520              :                 }
    8521              :               /* Else fall back to vector extraction anyway.
    8522              :                  Fewer stores are more important than avoiding spilling
    8523              :                  of the vector we extract from.  Compared to the
    8524              :                  construction case in vectorizable_load no store-forwarding
    8525              :                  issue exists here for reasonable archs.  But only
    8526              :                  if the store is supported.  */
    8527           26 :                  else if (!(dr_align == dr_aligned
    8528           26 :                             || dr_align == dr_unaligned_supported))
    8529              :                    {
    8530        29835 :                      nstores = const_nunits;
    8531        29835 :                      lnel = 1;
    8532        29835 :                      ltype = elem_type;
    8533        29835 :                      lvectype = vectype;
    8534              :                    }
    8535              :             }
    8536              :         }
    8537              : 
    8538        29835 :       if (costing_p)
    8539              :         /* Record the decomposition type for target access during costing.  */
    8540        26480 :         ls.ls_type = lvectype;
    8541              :       else
    8542         3355 :         gcc_assert (ls.ls_type == lvectype);
    8543              : 
    8544        29835 :       unsigned align;
    8545        29835 :       if (alignment_support_scheme == dr_aligned)
    8546         1249 :         align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
    8547              :       else
    8548        28586 :         align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
    8549              :       /* Alignment is at most the access size if we do multiple stores.  */
    8550        29835 :       if (nstores > 1)
    8551        26901 :         align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
    8552        29835 :       ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
    8553        29835 :       int ncopies = vec_num;
    8554              : 
    8555        29835 :       if (!costing_p)
    8556              :         {
    8557         3355 :           ivstep = stride_step;
    8558         3355 :           ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
    8559              :                                 build_int_cst (TREE_TYPE (ivstep), vf));
    8560              : 
    8561         3355 :           standard_iv_increment_position (loop, &incr_gsi, &insert_after);
    8562              : 
    8563         3355 :           stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
    8564         3355 :           ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
    8565         3355 :           create_iv (stride_base, PLUS_EXPR, ivstep, NULL, loop, &incr_gsi,
    8566              :                      insert_after, &offvar, NULL);
    8567              : 
    8568         3355 :           stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
    8569              :         }
    8570              : 
    8571        29835 :       alias_off = build_int_cst (ref_type, 0);
    8572        29835 :       auto_vec<tree> vec_oprnds;
    8573              :       /* For costing some adjacent vector stores, we'd like to cost with
    8574              :          the total number of them once instead of cost each one by one. */
    8575        29835 :       unsigned int n_adjacent_stores = 0;
    8576        29835 :       running_off = offvar;
    8577        29835 :       if (!costing_p)
    8578         3355 :         vect_get_slp_defs (op_node, &vec_oprnds);
    8579        29835 :       unsigned int group_el = 0;
    8580        29835 :       unsigned HOST_WIDE_INT elsz
    8581        29835 :         = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
    8582        70981 :       for (j = 0; j < ncopies; j++)
    8583              :         {
    8584        41146 :           if (!costing_p)
    8585              :             {
    8586         5186 :               vec_oprnd = vec_oprnds[j];
    8587              :               /* Pun the vector to extract from if necessary.  */
    8588         5186 :               if (lvectype != vectype)
    8589              :                 {
    8590         1122 :                   tree tem = make_ssa_name (lvectype);
    8591         1122 :                   tree cvt = build1 (VIEW_CONVERT_EXPR, lvectype, vec_oprnd);
    8592         1122 :                   gimple *pun = gimple_build_assign (tem, cvt);
    8593         1122 :                   vect_finish_stmt_generation (vinfo, stmt_info, pun, gsi);
    8594         1122 :                   vec_oprnd = tem;
    8595              :                 }
    8596              :             }
    8597       183071 :           for (i = 0; i < nstores; i++)
    8598              :             {
    8599       141925 :               if (costing_p)
    8600              :                 {
    8601       125475 :                   n_adjacent_stores++;
    8602       125475 :                   continue;
    8603              :                 }
    8604        16450 :               tree newref, newoff;
    8605        16450 :               gimple *incr, *assign;
    8606        16450 :               tree size = TYPE_SIZE (ltype);
    8607              :               /* Extract the i'th component.  */
    8608        16450 :               tree pos = fold_build2 (MULT_EXPR, bitsizetype,
    8609              :                                       bitsize_int (i), size);
    8610        16450 :               tree elem = fold_build3 (BIT_FIELD_REF, ltype, vec_oprnd,
    8611              :                                        size, pos);
    8612              : 
    8613        16450 :               elem = force_gimple_operand_gsi (gsi, elem, true, NULL_TREE, true,
    8614              :                                                GSI_SAME_STMT);
    8615              : 
    8616        16450 :               tree this_off = build_int_cst (TREE_TYPE (alias_off),
    8617        16450 :                                              group_el * elsz);
    8618        16450 :               newref = build2 (MEM_REF, ltype, running_off, this_off);
    8619        16450 :               vect_copy_ref_info (newref, DR_REF (first_dr_info->dr));
    8620              : 
    8621              :               /* And store it to *running_off.  */
    8622        16450 :               assign = gimple_build_assign (newref, elem);
    8623        16450 :               vect_finish_stmt_generation (vinfo, stmt_info, assign, gsi);
    8624              : 
    8625        16450 :               group_el += lnel;
    8626        16450 :               if (group_el == group_size)
    8627              :                 {
    8628        14773 :                   newoff = copy_ssa_name (running_off, NULL);
    8629        14773 :                   incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
    8630              :                                               running_off, stride_step);
    8631        14773 :                   vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
    8632              : 
    8633        14773 :                   running_off = newoff;
    8634        14773 :                   group_el = 0;
    8635              :                 }
    8636              :             }
    8637              :         }
    8638              : 
    8639        29835 :       if (costing_p)
    8640              :         {
    8641        26480 :           if (n_adjacent_stores > 0)
    8642              :             {
    8643              :               /* Take a single lane vector type store as scalar
    8644              :                  store to avoid ICE like 110776.  */
    8645        26480 :               if (VECTOR_TYPE_P (ltype)
    8646        26480 :                   && maybe_ne (TYPE_VECTOR_SUBPARTS (ltype), 1U))
    8647         1648 :                 vect_get_store_cost (vinfo, stmt_info, slp_node,
    8648              :                                      n_adjacent_stores, alignment_support_scheme,
    8649              :                                      misalignment, &inside_cost, cost_vec);
    8650              :               else
    8651        24832 :                 inside_cost
    8652        24832 :                   += record_stmt_cost (cost_vec, n_adjacent_stores,
    8653              :                                        scalar_store, slp_node, 0, vect_body);
    8654              :               /* Only need vector extracting when there are more
    8655              :                  than one stores.  */
    8656        26480 :               if (nstores > 1)
    8657        24430 :                 inside_cost
    8658        24430 :                   += record_stmt_cost (cost_vec, n_adjacent_stores,
    8659              :                                        vec_to_scalar, slp_node, 0, vect_body);
    8660              :             }
    8661        26480 :           if (dump_enabled_p ())
    8662          676 :             dump_printf_loc (MSG_NOTE, vect_location,
    8663              :                              "vect_model_store_cost: inside_cost = %d, "
    8664              :                              "prologue_cost = %d .\n",
    8665              :                              inside_cost, prologue_cost);
    8666              : 
    8667        26480 :           SLP_TREE_TYPE (slp_node) = store_vec_info_type;
    8668        26480 :           slp_node->data = new vect_load_store_data (std::move (ls));
    8669              :         }
    8670              : 
    8671        29835 :       return true;
    8672        29835 :     }
    8673              : 
    8674      1333314 :   gcc_assert (alignment_support_scheme);
    8675      1333314 :   vec_loop_masks *loop_masks
    8676       195319 :     = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
    8677      1333314 :        ? &LOOP_VINFO_MASKS (loop_vinfo)
    8678           11 :        : NULL);
    8679           11 :   vec_loop_lens *loop_lens
    8680       195319 :     = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
    8681              :        ? &LOOP_VINFO_LENS (loop_vinfo)
    8682            0 :        : NULL);
    8683              : 
    8684              :   /* The vect_transform_stmt and vect_analyze_stmt will go here but there
    8685              :      are some difference here.  We cannot enable both the lens and masks
    8686              :      during transform but it is allowed during analysis.
    8687              :      Shouldn't go with length-based approach if fully masked.  */
    8688      1333314 :   if (cost_vec == NULL)
    8689              :     /* The cost_vec is NULL during transfrom.  */
    8690       543030 :     gcc_assert ((!loop_lens || !loop_masks));
    8691              : 
    8692              :   /* Targets with store-lane instructions must not require explicit
    8693              :      realignment.  vect_supportable_dr_alignment always returns either
    8694              :      dr_aligned or dr_unaligned_supported for masked operations.  */
    8695      1333314 :   gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
    8696              :                && !mask_node
    8697              :                && !loop_masks)
    8698              :               || alignment_support_scheme == dr_aligned
    8699              :               || alignment_support_scheme == dr_unaligned_supported);
    8700              : 
    8701      1333314 :   tree offset = NULL_TREE;
    8702      1333314 :   if (!known_eq (poffset, 0))
    8703         4651 :     offset = size_int (poffset);
    8704              : 
    8705      1333314 :   tree bump;
    8706      1333314 :   tree vec_offset = NULL_TREE;
    8707      1333314 :   if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
    8708              :     {
    8709         1470 :       aggr_type = NULL_TREE;
    8710         1470 :       bump = NULL_TREE;
    8711              :     }
    8712      1331844 :   else if (mat_gather_scatter_p (memory_access_type))
    8713              :     {
    8714            0 :       aggr_type = elem_type;
    8715            0 :       if (!costing_p)
    8716              :         {
    8717            0 :           tree vtype = ls.ls_type ? ls.ls_type : vectype;
    8718            0 :           vect_get_strided_load_store_ops (stmt_info, slp_node, vtype,
    8719              :                                            ls.strided_offset_vectype,
    8720              :                                            loop_vinfo, gsi,
    8721              :                                            &bump, &vec_offset, loop_lens);
    8722              :         }
    8723              :     }
    8724              :   else
    8725              :     {
    8726      1331844 :       if (memory_access_type == VMAT_LOAD_STORE_LANES)
    8727            0 :         aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
    8728              :       else
    8729              :         aggr_type = vectype;
    8730      1331844 :       if (!costing_p)
    8731       542560 :         bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
    8732              :                                             memory_access_type, loop_lens);
    8733              :     }
    8734              : 
    8735      1333314 :   if (loop_vinfo && mask_node && !costing_p)
    8736          544 :     LOOP_VINFO_HAS_MASK_STORE (loop_vinfo) = true;
    8737              : 
    8738              :   /* In case the vectorization factor (VF) is bigger than the number
    8739              :      of elements that we can fit in a vectype (nunits), we have to generate
    8740              :      more than one vector stmt - i.e - we need to "unroll" the
    8741              :      vector stmt by a factor VF/nunits.  */
    8742              : 
    8743      1333314 :   auto_vec<tree> dr_chain (group_size);
    8744      1333314 :   auto_vec<tree> vec_masks;
    8745      1333314 :   tree vec_mask = NULL;
    8746      1333314 :   auto_delete_vec<auto_vec<tree>> gvec_oprnds (group_size);
    8747      6009543 :   for (i = 0; i < group_size; i++)
    8748      3342915 :     gvec_oprnds.quick_push (new auto_vec<tree> ());
    8749              : 
    8750      1333314 :   if (memory_access_type == VMAT_LOAD_STORE_LANES)
    8751              :     {
    8752            0 :       const internal_fn lanes_ifn = ls.lanes_ifn;
    8753              : 
    8754            0 :       if (costing_p)
    8755              :         /* Update all incoming store operand nodes, the general handling
    8756              :            above only handles the mask and the first store operand node.  */
    8757            0 :         for (slp_tree child : SLP_TREE_CHILDREN (slp_node))
    8758            0 :           if (child != mask_node
    8759            0 :               && !vect_maybe_update_slp_op_vectype (child, vectype))
    8760              :             {
    8761            0 :               if (dump_enabled_p ())
    8762            0 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    8763              :                                  "incompatible vector types for invariants\n");
    8764            0 :               return false;
    8765              :             }
    8766            0 :       unsigned inside_cost = 0, prologue_cost = 0;
    8767              :       /* For costing some adjacent vector stores, we'd like to cost with
    8768              :          the total number of them once instead of cost each one by one. */
    8769            0 :       unsigned int n_adjacent_stores = 0;
    8770            0 :       int ncopies = vec_num / group_size;
    8771            0 :       for (j = 0; j < ncopies; j++)
    8772              :         {
    8773            0 :           if (j == 0)
    8774              :             {
    8775            0 :               if (!costing_p)
    8776              :                 {
    8777            0 :                   if (mask_node)
    8778              :                     {
    8779            0 :                       vect_get_slp_defs (mask_node, &vec_masks);
    8780            0 :                       vec_mask = vec_masks[0];
    8781              :                     }
    8782            0 :                   dataref_ptr
    8783            0 :                     = vect_create_data_ref_ptr (vinfo, first_stmt_info,
    8784              :                                                 aggr_type, NULL, offset, &dummy,
    8785              :                                                 gsi, &ptr_incr, false, bump);
    8786              :                 }
    8787              :             }
    8788            0 :           else if (!costing_p)
    8789              :             {
    8790            0 :               gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
    8791            0 :               if (mask_node)
    8792            0 :                 vec_mask = vec_masks[j];
    8793            0 :               dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
    8794              :                                              stmt_info, bump);
    8795              :             }
    8796              : 
    8797            0 :           if (costing_p)
    8798              :             {
    8799            0 :               n_adjacent_stores += group_size;
    8800            0 :               continue;
    8801              :             }
    8802              : 
    8803              :           /* Get an array into which we can store the individual vectors.  */
    8804            0 :           tree vec_array = create_vector_array (vectype, group_size);
    8805              : 
    8806              :           /* Invalidate the current contents of VEC_ARRAY.  This should
    8807              :              become an RTL clobber too, which prevents the vector registers
    8808              :              from being upward-exposed.  */
    8809            0 :           vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
    8810              : 
    8811              :           /* Store the individual vectors into the array.  */
    8812            0 :           for (i = 0; i < group_size; i++)
    8813              :             {
    8814            0 :               slp_tree child;
    8815            0 :               if (i == 0 || !mask_node)
    8816            0 :                 child = SLP_TREE_CHILDREN (slp_node)[i];
    8817              :               else
    8818            0 :                 child = SLP_TREE_CHILDREN (slp_node)[i + 1];
    8819            0 :               vec_oprnd = SLP_TREE_VEC_DEFS (child)[j];
    8820            0 :               write_vector_array (vinfo, stmt_info, gsi, vec_oprnd, vec_array,
    8821              :                                   i);
    8822              :             }
    8823              : 
    8824            0 :           tree final_mask = NULL;
    8825            0 :           tree final_len = NULL;
    8826            0 :           tree bias = NULL;
    8827            0 :           if (loop_masks)
    8828            0 :             final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
    8829              :                                              ncopies, vectype, j);
    8830            0 :           if (vec_mask)
    8831            0 :             final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
    8832              :                                            vec_mask, gsi);
    8833              : 
    8834            0 :           if (lanes_ifn == IFN_MASK_LEN_STORE_LANES)
    8835              :             {
    8836            0 :               if (loop_lens)
    8837            0 :                 final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
    8838              :                                                ncopies, vectype, j, 1, true);
    8839              :               else
    8840            0 :                 final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
    8841            0 :               signed char biasval
    8842            0 :                 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
    8843            0 :               bias = build_int_cst (intQI_type_node, biasval);
    8844            0 :               if (!final_mask)
    8845              :                 {
    8846            0 :                   mask_vectype = truth_type_for (vectype);
    8847            0 :                   final_mask = build_minus_one_cst (mask_vectype);
    8848              :                 }
    8849              :             }
    8850              : 
    8851            0 :           gcall *call;
    8852            0 :           if (final_len && final_mask)
    8853              :             {
    8854              :               /* Emit:
    8855              :                    MASK_LEN_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
    8856              :                                          LEN, BIAS, VEC_ARRAY).  */
    8857            0 :               unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
    8858            0 :               tree alias_ptr = build_int_cst (ref_type, align);
    8859            0 :               call = gimple_build_call_internal (IFN_MASK_LEN_STORE_LANES, 6,
    8860              :                                                  dataref_ptr, alias_ptr,
    8861              :                                                  final_mask, final_len, bias,
    8862              :                                                  vec_array);
    8863              :             }
    8864            0 :           else if (final_mask)
    8865              :             {
    8866              :               /* Emit:
    8867              :                    MASK_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
    8868              :                                      VEC_ARRAY).  */
    8869            0 :               unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
    8870            0 :               tree alias_ptr = build_int_cst (ref_type, align);
    8871            0 :               call = gimple_build_call_internal (IFN_MASK_STORE_LANES, 4,
    8872              :                                                  dataref_ptr, alias_ptr,
    8873              :                                                  final_mask, vec_array);
    8874              :             }
    8875              :           else
    8876              :             {
    8877              :               /* Emit:
    8878              :                    MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY).  */
    8879            0 :               data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
    8880            0 :               call = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
    8881            0 :               gimple_call_set_lhs (call, data_ref);
    8882              :             }
    8883            0 :           gimple_call_set_nothrow (call, true);
    8884            0 :           vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
    8885              : 
    8886              :           /* Record that VEC_ARRAY is now dead.  */
    8887            0 :           vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
    8888              :         }
    8889              : 
    8890            0 :       if (costing_p)
    8891              :         {
    8892            0 :           if (n_adjacent_stores > 0)
    8893            0 :             vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
    8894              :                                  alignment_support_scheme, misalignment,
    8895              :                                  &inside_cost, cost_vec);
    8896            0 :           if (dump_enabled_p ())
    8897            0 :             dump_printf_loc (MSG_NOTE, vect_location,
    8898              :                              "vect_model_store_cost: inside_cost = %d, "
    8899              :                              "prologue_cost = %d .\n",
    8900              :                              inside_cost, prologue_cost);
    8901              : 
    8902            0 :           SLP_TREE_TYPE (slp_node) = store_vec_info_type;
    8903            0 :           slp_node->data = new vect_load_store_data (std::move (ls));
    8904              :         }
    8905              : 
    8906            0 :       return true;
    8907              :     }
    8908              : 
    8909      1333314 :   if (mat_gather_scatter_p (memory_access_type))
    8910              :     {
    8911         1470 :       gcc_assert (!grouped_store || ls.ls_type);
    8912         1470 :       if (ls.ls_type)
    8913            0 :         vectype = ls.ls_type;
    8914         1470 :       auto_vec<tree> vec_offsets;
    8915         1470 :       unsigned int inside_cost = 0, prologue_cost = 0;
    8916         1470 :       int num_stmts = vec_num;
    8917         3340 :       for (j = 0; j < num_stmts; j++)
    8918              :         {
    8919         1870 :           gimple *new_stmt;
    8920         1870 :           if (j == 0)
    8921              :             {
    8922         1470 :               if (costing_p && vls_type == VLS_STORE_INVARIANT)
    8923          210 :                 prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
    8924              :                                                    slp_node, 0, vect_prologue);
    8925              :               else if (!costing_p)
    8926              :                 {
    8927              :                   /* Since the store is not grouped, DR_GROUP_SIZE is 1, and
    8928              :                      DR_CHAIN is of size 1.  */
    8929          470 :                   gcc_assert (group_size == 1);
    8930          470 :                   vect_get_slp_defs (op_node, gvec_oprnds[0]);
    8931          470 :                   if (mask_node)
    8932           70 :                     vect_get_slp_defs (mask_node, &vec_masks);
    8933              : 
    8934          470 :                   if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
    8935          470 :                     vect_get_gather_scatter_ops (loop, slp_node,
    8936              :                                                  &dataref_ptr, &vec_offsets);
    8937              :                   else
    8938            0 :                     dataref_ptr
    8939            0 :                       = vect_create_data_ref_ptr (vinfo, first_stmt_info,
    8940              :                                                   aggr_type, NULL, offset,
    8941              :                                                   &dummy, gsi, &ptr_incr, false,
    8942              :                                                   bump);
    8943              :                 }
    8944              :             }
    8945          400 :           else if (!costing_p)
    8946              :             {
    8947           34 :               gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
    8948           34 :               if (!STMT_VINFO_GATHER_SCATTER_P (stmt_info))
    8949            0 :                 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
    8950              :                                                gsi, stmt_info, bump);
    8951              :             }
    8952              : 
    8953         2584 :           new_stmt = NULL;
    8954          714 :           if (!costing_p)
    8955              :             {
    8956          504 :               vec_oprnd = (*gvec_oprnds[0])[j];
    8957          504 :               if (mask_node)
    8958           90 :                 vec_mask = vec_masks[j];
    8959              :               /* We should have caught mismatched types earlier.  */
    8960          504 :               gcc_assert (ls.ls_type
    8961              :                           || useless_type_conversion_p
    8962              :                           (vectype, TREE_TYPE (vec_oprnd)));
    8963              :             }
    8964          504 :           tree final_mask = NULL_TREE;
    8965         2374 :           tree final_len = NULL_TREE;
    8966         2374 :           tree bias = NULL_TREE;
    8967          504 :           if (!costing_p)
    8968              :             {
    8969          504 :               if (loop_masks)
    8970            0 :                 final_mask = vect_get_loop_mask (loop_vinfo, gsi,
    8971              :                                                  loop_masks, num_stmts,
    8972              :                                                  vectype, j);
    8973          504 :               if (vec_mask)
    8974           90 :                 final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
    8975              :                                                final_mask, vec_mask, gsi);
    8976              :             }
    8977              : 
    8978         1870 :           unsigned align = get_object_alignment (DR_REF (first_dr_info->dr));
    8979         1870 :           tree alias_align_ptr = build_int_cst (ref_type, align);
    8980         1870 :           if (memory_access_type == VMAT_GATHER_SCATTER_IFN)
    8981              :             {
    8982            0 :               if (costing_p)
    8983              :                 {
    8984            0 :                   if (ls.supported_offset_vectype)
    8985            0 :                     inside_cost
    8986            0 :                       += record_stmt_cost (cost_vec, 1, vector_stmt,
    8987              :                                            slp_node, 0, vect_body);
    8988            0 :                   if (ls.supported_scale)
    8989            0 :                     inside_cost
    8990            0 :                       += record_stmt_cost (cost_vec, 1, vector_stmt,
    8991              :                                            slp_node, 0, vect_body);
    8992              : 
    8993            0 :                   unsigned int cnunits = vect_nunits_for_cost (vectype);
    8994            0 :                   inside_cost
    8995            0 :                     += record_stmt_cost (cost_vec, cnunits, scalar_store,
    8996              :                                          slp_node, 0, vect_body);
    8997         1870 :                   continue;
    8998            0 :                 }
    8999              : 
    9000            0 :               if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
    9001            0 :                 vec_offset = vec_offsets[j];
    9002              : 
    9003            0 :               tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
    9004            0 :               bool strided = !VECTOR_TYPE_P (TREE_TYPE (vec_offset));
    9005              : 
    9006              :               /* Perform the offset conversion and scaling if necessary.  */
    9007            0 :               if (!strided
    9008            0 :                   && (ls.supported_offset_vectype || ls.supported_scale))
    9009              :                 {
    9010            0 :                   gimple_seq stmts = NULL;
    9011            0 :                   if (ls.supported_offset_vectype)
    9012            0 :                     vec_offset = gimple_convert
    9013            0 :                       (&stmts, ls.supported_offset_vectype, vec_offset);
    9014            0 :                   if (ls.supported_scale)
    9015              :                     {
    9016              :                       /* Only scale the vec_offset if we haven't already.  */
    9017            0 :                       if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
    9018            0 :                           || j == 0)
    9019              :                         {
    9020            0 :                           tree mult_cst = build_int_cst
    9021            0 :                             (TREE_TYPE (TREE_TYPE (vec_offset)),
    9022            0 :                              SLP_TREE_GS_SCALE (slp_node) / ls.supported_scale);
    9023            0 :                           tree mult = build_vector_from_val
    9024            0 :                             (TREE_TYPE (vec_offset), mult_cst);
    9025            0 :                           vec_offset = gimple_build
    9026            0 :                             (&stmts, MULT_EXPR, TREE_TYPE (vec_offset),
    9027              :                              vec_offset, mult);
    9028              :                         }
    9029            0 :                       scale = size_int (ls.supported_scale);
    9030              :                     }
    9031            0 :                   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    9032              :                 }
    9033              : 
    9034            0 :               if (ls.gs.ifn == IFN_MASK_LEN_SCATTER_STORE)
    9035              :                 {
    9036            0 :                   if (loop_lens)
    9037            0 :                     final_len = vect_get_loop_len (loop_vinfo, gsi,
    9038              :                                                    loop_lens, num_stmts,
    9039              :                                                    vectype, j, 1, true);
    9040              :                   else
    9041            0 :                     final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
    9042              : 
    9043            0 :                   signed char biasval
    9044            0 :                     = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
    9045            0 :                   bias = build_int_cst (intQI_type_node, biasval);
    9046            0 :                   if (!final_mask)
    9047              :                     {
    9048            0 :                       mask_vectype = truth_type_for (vectype);
    9049            0 :                       final_mask = build_minus_one_cst (mask_vectype);
    9050              :                     }
    9051              :                 }
    9052              : 
    9053            0 :               if (ls.ls_type)
    9054              :                 {
    9055            0 :                   gimple *conv_stmt
    9056            0 :                     = gimple_build_assign (make_ssa_name (vectype),
    9057              :                                            VIEW_CONVERT_EXPR,
    9058              :                                            build1 (VIEW_CONVERT_EXPR, vectype,
    9059              :                                                    vec_oprnd));
    9060            0 :                   vect_finish_stmt_generation (vinfo, stmt_info, conv_stmt,
    9061              :                                                gsi);
    9062            0 :                   vec_oprnd = gimple_get_lhs (conv_stmt);
    9063              :                 }
    9064              : 
    9065            0 :               gcall *call;
    9066            0 :               if (final_len && final_mask)
    9067              :                 {
    9068            0 :                   if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
    9069            0 :                     call = gimple_build_call_internal (
    9070              :                             IFN_MASK_LEN_SCATTER_STORE, 8, dataref_ptr,
    9071              :                             alias_align_ptr,
    9072              :                             vec_offset, scale, vec_oprnd, final_mask, final_len,
    9073              :                             bias);
    9074              :                   else
    9075              :                     /* Non-vector offset indicates that prefer to take
    9076              :                        MASK_LEN_STRIDED_STORE instead of the
    9077              :                        IFN_MASK_SCATTER_STORE with direct stride arg.
    9078              :                        Similar to the gather case we have checked the
    9079              :                        alignment for a scatter already and assume
    9080              :                        that the strided store has the same requirements.  */
    9081            0 :                     call = gimple_build_call_internal (
    9082              :                             IFN_MASK_LEN_STRIDED_STORE, 6, dataref_ptr,
    9083              :                             vec_offset, vec_oprnd, final_mask, final_len, bias);
    9084              :                 }
    9085            0 :               else if (final_mask)
    9086            0 :                 call = gimple_build_call_internal
    9087            0 :                              (IFN_MASK_SCATTER_STORE, 6, dataref_ptr,
    9088              :                               alias_align_ptr,
    9089              :                               vec_offset, scale, vec_oprnd, final_mask);
    9090              :               else
    9091            0 :                 call = gimple_build_call_internal (IFN_SCATTER_STORE, 5,
    9092              :                                                    dataref_ptr,
    9093              :                                                    alias_align_ptr,
    9094              :                                                    vec_offset,
    9095              :                                                    scale, vec_oprnd);
    9096            0 :               gimple_call_set_nothrow (call, true);
    9097            0 :               vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
    9098            0 :               new_stmt = call;
    9099              :             }
    9100         1870 :           else if (memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
    9101              :             {
    9102              :               /* The builtin decls path for scatter is legacy, x86 only.  */
    9103          330 :               gcc_assert (nunits.is_constant ()
    9104              :                           && (!final_mask
    9105              :                               || SCALAR_INT_MODE_P
    9106              :                                    (TYPE_MODE (TREE_TYPE (final_mask)))));
    9107          330 :               if (costing_p)
    9108              :                 {
    9109          199 :                   unsigned int cnunits = vect_nunits_for_cost (vectype);
    9110          199 :                   inside_cost
    9111          199 :                     += record_stmt_cost (cost_vec, cnunits, scalar_store,
    9112              :                                          slp_node, 0, vect_body);
    9113          199 :                   continue;
    9114          199 :                 }
    9115              : 
    9116          131 :                 tree offset_vectype = TREE_TYPE (vec_offsets[0]);
    9117          131 :                 poly_uint64 offset_nunits
    9118          131 :                   = TYPE_VECTOR_SUBPARTS (offset_vectype);
    9119          131 :                 if (known_eq (nunits, offset_nunits))
    9120              :                   {
    9121           55 :                     new_stmt = vect_build_one_scatter_store_call
    9122          110 :                                    (vinfo, stmt_info, slp_node, gsi,
    9123           55 :                                     ls.gs.decl, dataref_ptr, vec_offsets[j],
    9124              :                                     vec_oprnd, final_mask);
    9125           55 :                     vect_finish_stmt_generation (vinfo, stmt_info,
    9126              :                                                  new_stmt, gsi);
    9127              :                   }
    9128           76 :                 else if (known_eq (nunits, offset_nunits * 2))
    9129              :                   {
    9130              :                     /* We have a offset vector with half the number of
    9131              :                        lanes but the builtins will store full vectype
    9132              :                        data from the lower lanes.  */
    9133           30 :                     new_stmt = vect_build_one_scatter_store_call
    9134           60 :                                    (vinfo, stmt_info, slp_node, gsi, ls.gs.decl,
    9135           30 :                                     dataref_ptr, vec_offsets[2 * j],
    9136              :                                     vec_oprnd, final_mask);
    9137           30 :                     vect_finish_stmt_generation (vinfo, stmt_info,
    9138              :                                                    new_stmt, gsi);
    9139           30 :                     int count = nunits.to_constant ();
    9140           30 :                     vec_perm_builder sel (count, count, 1);
    9141           30 :                     sel.quick_grow (count);
    9142          382 :                     for (int i = 0; i < count; ++i)
    9143          352 :                       sel[i] = i | (count / 2);
    9144           30 :                     vec_perm_indices indices (sel, 2, count);
    9145           30 :                     tree perm_mask
    9146           30 :                       = vect_gen_perm_mask_checked (vectype, indices);
    9147           30 :                     new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
    9148              :                                                     vec_oprnd, vec_oprnd,
    9149              :                                                     perm_mask);
    9150           30 :                     vec_oprnd = make_ssa_name (vectype);
    9151           30 :                     gimple_set_lhs (new_stmt, vec_oprnd);
    9152           30 :                     vect_finish_stmt_generation (vinfo, stmt_info,
    9153              :                                                  new_stmt, gsi);
    9154           30 :                     if (final_mask)
    9155              :                       {
    9156           20 :                         new_stmt = gimple_build_assign (NULL_TREE,
    9157              :                                                         VEC_UNPACK_HI_EXPR,
    9158              :                                                         final_mask);
    9159           20 :                         final_mask = make_ssa_name
    9160           20 :                                       (truth_type_for (offset_vectype));
    9161           20 :                         gimple_set_lhs (new_stmt, final_mask);
    9162           20 :                         vect_finish_stmt_generation (vinfo, stmt_info,
    9163              :                                                      new_stmt, gsi);
    9164              :                         }
    9165              : 
    9166           30 :                     new_stmt = vect_build_one_scatter_store_call
    9167           60 :                                   (vinfo, stmt_info, slp_node, gsi, ls.gs.decl,
    9168           30 :                                    dataref_ptr, vec_offsets[2 * j + 1],
    9169              :                                    vec_oprnd, final_mask);
    9170           30 :                     vect_finish_stmt_generation (vinfo, stmt_info,
    9171              :                                                  new_stmt, gsi);
    9172           30 :                   }
    9173           46 :                 else if (known_eq (nunits * 2, offset_nunits))
    9174              :                   {
    9175              :                     /* We have a offset vector with double the number of
    9176              :                        lanes.  Select the low/high part accordingly.  */
    9177           46 :                     vec_offset = vec_offsets[j / 2];
    9178           46 :                     if (j & 1)
    9179              :                       {
    9180           23 :                         int count = offset_nunits.to_constant ();
    9181           23 :                         vec_perm_builder sel (count, count, 1);
    9182           23 :                         sel.quick_grow (count);
    9183          263 :                         for (int i = 0; i < count; ++i)
    9184          240 :                           sel[i] = i | (count / 2);
    9185           23 :                         vec_perm_indices indices (sel, 2, count);
    9186           23 :                         tree perm_mask = vect_gen_perm_mask_checked
    9187           23 :                                            (TREE_TYPE (vec_offset), indices);
    9188           23 :                         new_stmt = gimple_build_assign (NULL_TREE,
    9189              :                                                         VEC_PERM_EXPR,
    9190              :                                                         vec_offset,
    9191              :                                                         vec_offset,
    9192              :                                                         perm_mask);
    9193           23 :                         vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
    9194           23 :                         gimple_set_lhs (new_stmt, vec_offset);
    9195           23 :                         vect_finish_stmt_generation (vinfo, stmt_info,
    9196              :                                                      new_stmt, gsi);
    9197           23 :                       }
    9198              : 
    9199           46 :                     new_stmt = vect_build_one_scatter_store_call
    9200           46 :                                    (vinfo, stmt_info, slp_node, gsi,
    9201              :                                     ls.gs.decl, dataref_ptr, vec_offset,
    9202              :                                     vec_oprnd, final_mask);
    9203           46 :                     vect_finish_stmt_generation (vinfo, stmt_info,
    9204              :                                                  new_stmt, gsi);
    9205              :                   }
    9206              :                 else
    9207            0 :                   gcc_unreachable ();
    9208              :             }
    9209              :           else
    9210              :             {
    9211              :               /* Emulated scatter.  */
    9212         1540 :               gcc_assert (!final_mask);
    9213         1540 :               if (costing_p)
    9214              :                 {
    9215         1167 :                   unsigned int cnunits = vect_nunits_for_cost (vectype);
    9216              :                   /* For emulated scatter N offset vector element extracts
    9217              :                      (we assume the scalar scaling and ptr + offset add is
    9218              :                      consumed by the load).  */
    9219         1167 :                   inside_cost
    9220         1167 :                     += record_stmt_cost (cost_vec, cnunits, vec_to_scalar,
    9221              :                                          slp_node, 0, vect_body);
    9222              :                   /* N scalar stores plus extracting the elements.  */
    9223         1167 :                   inside_cost
    9224         1167 :                     += record_stmt_cost (cost_vec, cnunits, vec_to_scalar,
    9225              :                                          slp_node, 0, vect_body);
    9226         1167 :                   inside_cost
    9227         1167 :                     += record_stmt_cost (cost_vec, cnunits, scalar_store,
    9228              :                                          slp_node, 0, vect_body);
    9229         1167 :                   continue;
    9230         1167 :                 }
    9231              : 
    9232          373 :               tree offset_vectype = TREE_TYPE (vec_offsets[0]);
    9233          373 :               unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
    9234          373 :               unsigned HOST_WIDE_INT const_offset_nunits
    9235          373 :                 = TYPE_VECTOR_SUBPARTS (offset_vectype).to_constant ();
    9236          373 :               vec<constructor_elt, va_gc> *ctor_elts;
    9237          373 :               vec_alloc (ctor_elts, const_nunits);
    9238          373 :               gimple_seq stmts = NULL;
    9239          373 :               tree elt_type = TREE_TYPE (vectype);
    9240          373 :               unsigned HOST_WIDE_INT elt_size
    9241          373 :                 = tree_to_uhwi (TYPE_SIZE (elt_type));
    9242              :               /* We support offset vectors with more elements
    9243              :                  than the data vector for now.  */
    9244          373 :               unsigned HOST_WIDE_INT factor
    9245              :                 = const_offset_nunits / const_nunits;
    9246          373 :               vec_offset = vec_offsets[j / factor];
    9247          373 :               unsigned elt_offset
    9248          373 :                 = (j % factor) * const_nunits;
    9249          373 :               tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
    9250          373 :               tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
    9251          373 :               tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
    9252         1519 :               for (unsigned k = 0; k < const_nunits; ++k)
    9253              :                 {
    9254              :                   /* Compute the offsetted pointer.  */
    9255         1146 :                   tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
    9256              :                                           bitsize_int (k + elt_offset));
    9257         1146 :                   tree idx
    9258         2292 :                     = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
    9259         1146 :                                     vec_offset, TYPE_SIZE (idx_type), boff);
    9260         1146 :                   idx = gimple_convert (&stmts, sizetype, idx);
    9261         1146 :                   idx = gimple_build (&stmts, MULT_EXPR, sizetype,
    9262              :                                       idx, scale);
    9263         1146 :                   tree ptr
    9264         1146 :                     = gimple_build (&stmts, PLUS_EXPR,
    9265         1146 :                                     TREE_TYPE (dataref_ptr),
    9266              :                                     dataref_ptr, idx);
    9267         1146 :                   ptr = gimple_convert (&stmts, ptr_type_node, ptr);
    9268              :                   /* Extract the element to be stored.  */
    9269         1146 :                   tree elt
    9270         2292 :                     = gimple_build (&stmts, BIT_FIELD_REF,
    9271         1146 :                                     TREE_TYPE (vectype),
    9272         1146 :                                     vec_oprnd, TYPE_SIZE (elt_type),
    9273         1146 :                                     bitsize_int (k * elt_size));
    9274         1146 :                   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    9275         1146 :                   stmts = NULL;
    9276         1146 :                   tree ref
    9277         1146 :                     = build2 (MEM_REF, ltype, ptr,
    9278              :                               build_int_cst (ref_type, 0));
    9279         1146 :                   new_stmt = gimple_build_assign (ref, elt);
    9280         1146 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    9281              :                 }
    9282              : 
    9283          373 :               slp_node->push_vec_def (new_stmt);
    9284              :             }
    9285              :         }
    9286              : 
    9287         1470 :       if (costing_p)
    9288              :         {
    9289         1000 :           if (dump_enabled_p ())
    9290           80 :             dump_printf_loc (MSG_NOTE, vect_location,
    9291              :                              "vect_model_store_cost: inside_cost = %d, "
    9292              :                              "prologue_cost = %d .\n",
    9293              :                              inside_cost, prologue_cost);
    9294         1000 :           SLP_TREE_TYPE (slp_node) = store_vec_info_type;
    9295         1000 :           slp_node->data = new vect_load_store_data (std::move (ls));
    9296              :         }
    9297              : 
    9298         1470 :       return true;
    9299         1470 :     }
    9300              : 
    9301      1331844 :   gcc_assert (memory_access_type == VMAT_CONTIGUOUS
    9302              :               || memory_access_type == VMAT_CONTIGUOUS_DOWN
    9303              :               || memory_access_type == VMAT_CONTIGUOUS_REVERSE);
    9304              : 
    9305      1331844 :   unsigned inside_cost = 0, prologue_cost = 0;
    9306              :   /* For costing some adjacent vector stores, we'd like to cost with
    9307              :      the total number of them once instead of cost each one by one. */
    9308      1331844 :   unsigned int n_adjacent_stores = 0;
    9309      1331844 :   auto_vec<tree> result_chain (group_size);
    9310      1331844 :   auto_vec<tree, 1> vec_oprnds;
    9311      1331844 :   gimple *new_stmt;
    9312      1331844 :   if (!costing_p)
    9313              :     {
    9314              :       /* Get vectorized arguments for SLP_NODE.  */
    9315       542560 :       vect_get_slp_defs (op_node, &vec_oprnds);
    9316       542560 :       vec_oprnd = vec_oprnds[0];
    9317       542560 :       if (mask_node)
    9318              :         {
    9319          475 :           vect_get_slp_defs (mask_node, &vec_masks);
    9320          475 :           vec_mask = vec_masks[0];
    9321              :         }
    9322              :     }
    9323              : 
    9324              :   /* We should have caught mismatched types earlier.  */
    9325       542560 :   gcc_assert (costing_p
    9326              :               || useless_type_conversion_p (vectype, TREE_TYPE (vec_oprnd)));
    9327      1331844 :   bool simd_lane_access_p
    9328      1331844 :       = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
    9329      1331844 :   if (!costing_p
    9330      1331844 :       && simd_lane_access_p
    9331         4374 :       && !loop_masks
    9332         4374 :       && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
    9333         4374 :       && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
    9334         4374 :       && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
    9335         4374 :       && integer_zerop (DR_INIT (first_dr_info->dr))
    9336      1336218 :       && alias_sets_conflict_p (get_alias_set (aggr_type),
    9337         4374 :                                 get_alias_set (TREE_TYPE (ref_type))))
    9338              :     {
    9339         4366 :       dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
    9340         4366 :       dataref_offset = build_int_cst (ref_type, 0);
    9341              :     }
    9342      1327478 :   else if (!costing_p)
    9343      1076380 :     dataref_ptr = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
    9344              :                                             simd_lane_access_p ? loop : NULL,
    9345              :                                             offset, &dummy, gsi, &ptr_incr,
    9346              :                                             simd_lane_access_p, bump);
    9347              : 
    9348      1331844 :   new_stmt = NULL;
    9349      1331844 :   gcc_assert (!grouped_store);
    9350      2960608 :   for (i = 0; i < vec_num; i++)
    9351              :     {
    9352      1628764 :       if (!costing_p)
    9353       671950 :         vec_oprnd = vec_oprnds[i];
    9354              : 
    9355      1628764 :       if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
    9356              :         {
    9357         3331 :           if (costing_p)
    9358         2191 :             inside_cost += record_stmt_cost (cost_vec, 1, vec_perm,
    9359              :                                              slp_node, 0, vect_body);
    9360              :           else
    9361              :             {
    9362         1140 :               tree perm_mask = perm_mask_for_reverse (vectype);
    9363         1140 :               tree new_temp = make_ssa_name (vectype);
    9364              : 
    9365              :               /* Generate the permute statement.  */
    9366         1140 :               gimple *perm_stmt
    9367         1140 :                 = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
    9368              :                                        vec_oprnd, perm_mask);
    9369         1140 :               vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
    9370              : 
    9371         1140 :               perm_stmt = SSA_NAME_DEF_STMT (new_temp);
    9372      1628764 :               vec_oprnd = new_temp;
    9373              :             }
    9374              :         }
    9375              : 
    9376      1628764 :       if (costing_p)
    9377              :         {
    9378       956814 :           n_adjacent_stores++;
    9379       956814 :           continue;
    9380              :         }
    9381              : 
    9382       671950 :       tree final_mask = NULL_TREE;
    9383       671950 :       tree final_len = NULL_TREE;
    9384       671950 :       tree bias = NULL_TREE;
    9385       671950 :       if (loop_masks)
    9386           77 :         final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
    9387              :                                          vec_num, vectype, i);
    9388       671950 :       if (vec_mask)
    9389          696 :         vec_mask = vec_masks[i];
    9390          696 :       if (vec_mask)
    9391          696 :         final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
    9392              :                                        vec_mask, gsi);
    9393              : 
    9394       671950 :       if (i > 0)
    9395              :         /* Bump the vector pointer.  */
    9396       129390 :         dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
    9397              :                                        stmt_info, bump);
    9398              : 
    9399       671950 :       unsigned misalign;
    9400       671950 :       unsigned HOST_WIDE_INT align;
    9401       671950 :       align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
    9402       671950 :       if (alignment_support_scheme == dr_aligned)
    9403              :         misalign = 0;
    9404       309411 :       else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
    9405              :         {
    9406       160987 :           align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
    9407       160987 :           misalign = 0;
    9408              :         }
    9409              :       else
    9410       148424 :         misalign = misalignment;
    9411       671950 :       if (dataref_offset == NULL_TREE
    9412       666570 :           && TREE_CODE (dataref_ptr) == SSA_NAME)
    9413       182519 :         set_ptr_info_alignment (get_ptr_info (dataref_ptr), align, misalign);
    9414       671950 :       align = least_bit_hwi (misalign | align);
    9415              : 
    9416              :       /* Compute IFN when LOOP_LENS or final_mask valid.  */
    9417       671950 :       machine_mode vmode = TYPE_MODE (vectype);
    9418       671950 :       machine_mode new_vmode = vmode;
    9419       671950 :       internal_fn partial_ifn = IFN_LAST;
    9420       671950 :       if (loop_lens)
    9421              :         {
    9422            0 :           opt_machine_mode new_ovmode
    9423            0 :             = get_len_load_store_mode (vmode, false, &partial_ifn);
    9424            0 :           new_vmode = new_ovmode.require ();
    9425            0 :           unsigned factor
    9426            0 :             = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
    9427            0 :           final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
    9428              :                                          vec_num, vectype, i, factor, true);
    9429              :         }
    9430       671950 :       else if (final_mask)
    9431              :         {
    9432          708 :           if (!can_vec_mask_load_store_p (vmode,
    9433          708 :                                           TYPE_MODE (TREE_TYPE (final_mask)),
    9434              :                                           false, &partial_ifn))
    9435            0 :             gcc_unreachable ();
    9436              :         }
    9437              : 
    9438       671950 :       if (partial_ifn == IFN_MASK_LEN_STORE)
    9439              :         {
    9440            0 :           if (!final_len)
    9441              :             {
    9442              :               /* Pass VF value to 'len' argument of
    9443              :                  MASK_LEN_STORE if LOOP_LENS is invalid.  */
    9444            0 :               final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
    9445              :             }
    9446            0 :           if (!final_mask)
    9447              :             {
    9448              :               /* Pass all ones value to 'mask' argument of
    9449              :                  MASK_LEN_STORE if final_mask is invalid.  */
    9450            0 :               mask_vectype = truth_type_for (vectype);
    9451            0 :               final_mask = build_minus_one_cst (mask_vectype);
    9452              :             }
    9453              :         }
    9454       671950 :       if (final_len)
    9455              :         {
    9456            0 :           signed char biasval = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
    9457            0 :           bias = build_int_cst (intQI_type_node, biasval);
    9458              :         }
    9459              : 
    9460              :       /* Arguments are ready.  Create the new vector stmt.  */
    9461       671950 :       if (final_len)
    9462              :         {
    9463            0 :           gcall *call;
    9464            0 :           tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
    9465              :           /* Need conversion if it's wrapped with VnQI.  */
    9466            0 :           if (vmode != new_vmode)
    9467              :             {
    9468            0 :               tree new_vtype
    9469            0 :                 = build_vector_type_for_mode (unsigned_intQI_type_node,
    9470              :                                               new_vmode);
    9471            0 :               tree var = vect_get_new_ssa_name (new_vtype, vect_simple_var);
    9472            0 :               vec_oprnd = build1 (VIEW_CONVERT_EXPR, new_vtype, vec_oprnd);
    9473            0 :               gassign *new_stmt
    9474            0 :                 = gimple_build_assign (var, VIEW_CONVERT_EXPR, vec_oprnd);
    9475            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    9476            0 :               vec_oprnd = var;
    9477              :             }
    9478              : 
    9479            0 :           if (partial_ifn == IFN_MASK_LEN_STORE)
    9480            0 :             call = gimple_build_call_internal (IFN_MASK_LEN_STORE, 6,
    9481              :                                                dataref_ptr, ptr, final_mask,
    9482              :                                                final_len, bias, vec_oprnd);
    9483              :           else
    9484            0 :             call = gimple_build_call_internal (IFN_LEN_STORE, 5,
    9485              :                                                dataref_ptr, ptr, final_len,
    9486              :                                                bias, vec_oprnd);
    9487            0 :           gimple_call_set_nothrow (call, true);
    9488            0 :           vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
    9489            0 :           new_stmt = call;
    9490              :         }
    9491       671950 :       else if (final_mask)
    9492              :         {
    9493          708 :           tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
    9494          708 :           gcall *call
    9495          708 :             = gimple_build_call_internal (IFN_MASK_STORE, 4, dataref_ptr,
    9496              :                                           ptr, final_mask, vec_oprnd);
    9497          708 :           gimple_call_set_nothrow (call, true);
    9498          708 :           vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
    9499          708 :           new_stmt = call;
    9500              :         }
    9501              :       else
    9502              :         {
    9503       671242 :           data_ref = fold_build2 (MEM_REF, vectype, dataref_ptr,
    9504              :                                   dataref_offset ? dataref_offset
    9505              :                                   : build_int_cst (ref_type, 0));
    9506       671242 :           if (alignment_support_scheme == dr_aligned
    9507       671242 :               && align >= TYPE_ALIGN_UNIT (vectype))
    9508              :             ;
    9509              :           else
    9510       308873 :             TREE_TYPE (data_ref)
    9511       617746 :               = build_aligned_type (TREE_TYPE (data_ref),
    9512              :                                     align * BITS_PER_UNIT);
    9513       671242 :           vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
    9514       671242 :           new_stmt = gimple_build_assign (data_ref, vec_oprnd);
    9515       671242 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
    9516              :         }
    9517              :     }
    9518              : 
    9519      1331844 :   if (costing_p)
    9520              :     {
    9521       789284 :       if (n_adjacent_stores > 0)
    9522       789284 :         vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
    9523              :                              alignment_support_scheme, misalignment,
    9524              :                              &inside_cost, cost_vec);
    9525              : 
    9526              :       /* When vectorizing a store into the function result assign
    9527              :          a penalty if the function returns in a multi-register location.
    9528              :          In this case we assume we'll end up with having to spill the
    9529              :          vector result and do piecewise loads as a conservative estimate.  */
    9530       789284 :       tree base = get_base_address (STMT_VINFO_DATA_REF (stmt_info)->ref);
    9531       789284 :       if (base
    9532       789284 :           && (TREE_CODE (base) == RESULT_DECL
    9533       738993 :               || (DECL_P (base) && cfun_returns (base)))
    9534       851319 :           && !aggregate_value_p (base, cfun->decl))
    9535              :         {
    9536        11073 :           rtx reg = hard_function_value (TREE_TYPE (base), cfun->decl, 0, 1);
    9537              :           /* ???  Handle PARALLEL in some way.  */
    9538        11073 :           if (REG_P (reg))
    9539              :             {
    9540        10869 :               int nregs = hard_regno_nregs (REGNO (reg), GET_MODE (reg));
    9541              :               /* Assume that a single reg-reg move is possible and cheap,
    9542              :                  do not account for vector to gp register move cost.  */
    9543        10869 :               if (nregs > 1)
    9544              :                 {
    9545              :                   /* Spill.  */
    9546        10038 :                   prologue_cost
    9547        10038 :                     += record_stmt_cost (cost_vec, 1, vector_store,
    9548              :                                          slp_node, 0, vect_epilogue);
    9549              :                   /* Loads.  */
    9550        10038 :                   prologue_cost
    9551        10038 :                     += record_stmt_cost (cost_vec, nregs, scalar_load,
    9552              :                                          slp_node, 0, vect_epilogue);
    9553              :                 }
    9554              :             }
    9555              :         }
    9556       789284 :       if (dump_enabled_p ())
    9557        13822 :         dump_printf_loc (MSG_NOTE, vect_location,
    9558              :                          "vect_model_store_cost: inside_cost = %d, "
    9559              :                          "prologue_cost = %d .\n",
    9560              :                          inside_cost, prologue_cost);
    9561              : 
    9562       789284 :       SLP_TREE_TYPE (slp_node) = store_vec_info_type;
    9563       789284 :       slp_node->data = new vect_load_store_data (std::move (ls));
    9564              :     }
    9565              : 
    9566      1331844 :   return true;
    9567      2696803 : }
    9568              : 
    9569              : /* Given a vector type VECTYPE, turns permutation SEL into the equivalent
    9570              :    VECTOR_CST mask.  No checks are made that the target platform supports the
    9571              :    mask, so callers may wish to test can_vec_perm_const_p separately, or use
    9572              :    vect_gen_perm_mask_checked.  */
    9573              : 
    9574              : tree
    9575        61516 : vect_gen_perm_mask_any (tree vectype, const vec_perm_indices &sel)
    9576              : {
    9577        61516 :   tree mask_type;
    9578              : 
    9579        61516 :   poly_uint64 nunits = sel.length ();
    9580        61516 :   gcc_assert (known_eq (nunits, TYPE_VECTOR_SUBPARTS (vectype)));
    9581              : 
    9582        61516 :   mask_type = build_vector_type (ssizetype, nunits);
    9583        61516 :   return vec_perm_indices_to_tree (mask_type, sel);
    9584              : }
    9585              : 
    9586              : /* Checked version of vect_gen_perm_mask_any.  Asserts can_vec_perm_const_p,
    9587              :    i.e. that the target supports the pattern _for arbitrary input vectors_.  */
    9588              : 
    9589              : tree
    9590        58654 : vect_gen_perm_mask_checked (tree vectype, const vec_perm_indices &sel)
    9591              : {
    9592        58654 :   machine_mode vmode = TYPE_MODE (vectype);
    9593        58654 :   gcc_assert (can_vec_perm_const_p (vmode, vmode, sel));
    9594        58654 :   return vect_gen_perm_mask_any (vectype, sel);
    9595              : }
    9596              : 
    9597              : /* Given a vector variable X and Y, that was generated for the scalar
    9598              :    STMT_INFO, generate instructions to permute the vector elements of X and Y
    9599              :    using permutation mask MASK_VEC, insert them at *GSI and return the
    9600              :    permuted vector variable.  */
    9601              : 
    9602              : static tree
    9603         1444 : permute_vec_elements (vec_info *vinfo,
    9604              :                       tree x, tree y, tree mask_vec, stmt_vec_info stmt_info,
    9605              :                       gimple_stmt_iterator *gsi)
    9606              : {
    9607         1444 :   tree vectype = TREE_TYPE (x);
    9608         1444 :   tree perm_dest, data_ref;
    9609         1444 :   gimple *perm_stmt;
    9610              : 
    9611         1444 :   tree scalar_dest = gimple_get_lhs (stmt_info->stmt);
    9612         1444 :   if (scalar_dest && TREE_CODE (scalar_dest) == SSA_NAME)
    9613         1444 :     perm_dest = vect_create_destination_var (scalar_dest, vectype);
    9614              :   else
    9615            0 :     perm_dest = vect_get_new_vect_var (vectype, vect_simple_var, NULL);
    9616         1444 :   data_ref = make_ssa_name (perm_dest);
    9617              : 
    9618              :   /* Generate the permute statement.  */
    9619         1444 :   perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
    9620         1444 :   vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
    9621              : 
    9622         1444 :   return data_ref;
    9623              : }
    9624              : 
    9625              : /* Hoist the definitions of all SSA uses on STMT_INFO out of the loop LOOP,
    9626              :    inserting them on the loops preheader edge.  Returns true if we
    9627              :    were successful in doing so (and thus STMT_INFO can be moved then),
    9628              :    otherwise returns false.  HOIST_P indicates if we want to hoist the
    9629              :    definitions of all SSA uses, it would be false when we are costing.  */
    9630              : 
    9631              : static bool
    9632         4150 : hoist_defs_of_uses (gimple *stmt, class loop *loop, bool hoist_p)
    9633              : {
    9634         4150 :   ssa_op_iter i;
    9635         4150 :   use_operand_p use_p;
    9636         4150 :   auto_vec<use_operand_p, 8> to_hoist;
    9637              : 
    9638         7899 :   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
    9639              :     {
    9640         3777 :       gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
    9641         3777 :       if (!gimple_nop_p (def_stmt)
    9642         3777 :           && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
    9643              :         {
    9644              :           /* Make sure we don't need to recurse.  While we could do
    9645              :              so in simple cases when there are more complex use webs
    9646              :              we don't have an easy way to preserve stmt order to fulfil
    9647              :              dependencies within them.  */
    9648          111 :           tree op2;
    9649          111 :           ssa_op_iter i2;
    9650          111 :           if (gimple_code (def_stmt) == GIMPLE_PHI
    9651          111 :               || (single_ssa_def_operand (def_stmt, SSA_OP_DEF)
    9652              :                   == NULL_DEF_OPERAND_P))
    9653           28 :             return false;
    9654          226 :           FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
    9655              :             {
    9656          143 :               gimple *def_stmt2 = SSA_NAME_DEF_STMT (op2);
    9657          143 :               if (!gimple_nop_p (def_stmt2)
    9658          143 :                   && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
    9659              :                 return false;
    9660              :             }
    9661           83 :           to_hoist.safe_push (use_p);
    9662              :         }
    9663              :     }
    9664              : 
    9665         8244 :   if (to_hoist.is_empty ())
    9666              :     return true;
    9667              : 
    9668           59 :   if (!hoist_p)
    9669              :     return true;
    9670              : 
    9671              :   /* Instead of moving defs we copy them so we can zero their UID to not
    9672              :      confuse dominance queries in the preheader.  */
    9673            9 :   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    9674           36 :   for (use_operand_p use_p : to_hoist)
    9675              :     {
    9676            9 :       gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
    9677            9 :       gimple *copy = gimple_copy (def_stmt);
    9678            9 :       gimple_set_uid (copy, 0);
    9679            9 :       def_operand_p def_p = single_ssa_def_operand (def_stmt, SSA_OP_DEF);
    9680            9 :       tree new_def = duplicate_ssa_name (DEF_FROM_PTR (def_p), copy);
    9681            9 :       update_stmt (copy);
    9682            9 :       def_p = single_ssa_def_operand (copy, SSA_OP_DEF);
    9683            9 :       SET_DEF (def_p, new_def);
    9684            9 :       SET_USE (use_p, new_def);
    9685            9 :       gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
    9686              :     }
    9687              : 
    9688              :   return true;
    9689         4150 : }
    9690              : 
    9691              : /* vectorizable_load.
    9692              : 
    9693              :    Check if STMT_INFO reads a non scalar data-ref (array/pointer/structure)
    9694              :    that can be vectorized.
    9695              :    If COST_VEC is passed, calculate costs but don't change anything,
    9696              :    otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
    9697              :    it, and insert it at GSI.
    9698              :    Return true if STMT_INFO is vectorizable in this way.  */
    9699              : 
    9700              : static bool
    9701      2143854 : vectorizable_load (vec_info *vinfo,
    9702              :                    stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
    9703              :                    slp_tree slp_node,
    9704              :                    stmt_vector_for_cost *cost_vec)
    9705              : {
    9706      2143854 :   tree scalar_dest;
    9707      2143854 :   tree vec_dest = NULL;
    9708      2143854 :   tree data_ref = NULL;
    9709      2143854 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    9710      2143854 :   class loop *loop = NULL;
    9711      2143854 :   class loop *containing_loop = gimple_bb (stmt_info->stmt)->loop_father;
    9712      2143854 :   bool nested_in_vect_loop = false;
    9713      2143854 :   tree elem_type;
    9714              :   /* Avoid false positive uninitialized warning, see PR110652.  */
    9715      2143854 :   tree new_temp = NULL_TREE;
    9716      2143854 :   machine_mode mode;
    9717      2143854 :   tree dummy;
    9718      2143854 :   tree dataref_ptr = NULL_TREE;
    9719      2143854 :   tree dataref_offset = NULL_TREE;
    9720      2143854 :   gimple *ptr_incr = NULL;
    9721      2143854 :   int i, j;
    9722      2143854 :   unsigned int group_size;
    9723      2143854 :   poly_uint64 group_gap_adj;
    9724      2143854 :   tree msq = NULL_TREE, lsq;
    9725      2143854 :   tree realignment_token = NULL_TREE;
    9726      2143854 :   gphi *phi = NULL;
    9727      2143854 :   bool grouped_load = false;
    9728      2143854 :   stmt_vec_info first_stmt_info;
    9729      2143854 :   stmt_vec_info first_stmt_info_for_drptr = NULL;
    9730      2143854 :   bool compute_in_loop = false;
    9731      2143854 :   class loop *at_loop;
    9732      2143854 :   int vec_num;
    9733      2143854 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
    9734      2143854 :   poly_uint64 vf;
    9735      2143854 :   tree aggr_type;
    9736      2143854 :   tree ref_type;
    9737      2143854 :   enum vect_def_type mask_dt = vect_unknown_def_type;
    9738      2143854 :   enum vect_def_type els_dt = vect_unknown_def_type;
    9739              : 
    9740      2143854 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
    9741              :     return false;
    9742              : 
    9743      2143854 :   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
    9744       234770 :       && cost_vec)
    9745              :     return false;
    9746              : 
    9747      1909084 :   if (!STMT_VINFO_DATA_REF (stmt_info))
    9748              :     return false;
    9749              : 
    9750      1529691 :   tree mask_vectype = NULL_TREE;
    9751      1529691 :   tree els = NULL_TREE; tree els_vectype = NULL_TREE;
    9752              : 
    9753      1529691 :   int mask_index = -1;
    9754      1529691 :   int els_index = -1;
    9755      1529691 :   slp_tree mask_node = NULL;
    9756      1529691 :   slp_tree els_op = NULL;
    9757      1529691 :   if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
    9758              :     {
    9759      1525239 :       scalar_dest = gimple_assign_lhs (assign);
    9760      1525239 :       if (TREE_CODE (scalar_dest) != SSA_NAME)
    9761              :         return false;
    9762              : 
    9763       708501 :       tree_code code = gimple_assign_rhs_code (assign);
    9764       708501 :       if (code != ARRAY_REF
    9765       708501 :           && code != BIT_FIELD_REF
    9766       708501 :           && code != INDIRECT_REF
    9767       488921 :           && code != COMPONENT_REF
    9768       488921 :           && code != IMAGPART_EXPR
    9769       353050 :           && code != REALPART_EXPR
    9770       353050 :           && code != MEM_REF
    9771          283 :           && TREE_CODE_CLASS (code) != tcc_declaration)
    9772              :         return false;
    9773              :     }
    9774              :   else
    9775              :     {
    9776      1437104 :       gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
    9777         4452 :       if (!call || !gimple_call_internal_p (call))
    9778              :         return false;
    9779              : 
    9780         4452 :       internal_fn ifn = gimple_call_internal_fn (call);
    9781         4452 :       if (!internal_load_fn_p (ifn))
    9782              :         return false;
    9783              : 
    9784         3096 :       scalar_dest = gimple_call_lhs (call);
    9785         3096 :       if (!scalar_dest)
    9786              :         return false;
    9787              : 
    9788         3096 :       mask_index = internal_fn_mask_index (ifn);
    9789         3096 :       if (mask_index >= 0)
    9790         3096 :         mask_index = vect_slp_child_index_for_operand (stmt_info, mask_index);
    9791         3096 :       if (mask_index >= 0
    9792         3096 :           && !vect_check_scalar_mask (vinfo, slp_node, mask_index,
    9793              :                                       &mask_node, &mask_dt, &mask_vectype))
    9794              :         return false;
    9795              : 
    9796         3096 :       els_index = internal_fn_else_index (ifn);
    9797         3096 :       if (els_index >= 0)
    9798         3096 :         els_index = vect_slp_child_index_for_operand (stmt_info, els_index);
    9799         3096 :       if (els_index >= 0
    9800         3096 :           && !vect_is_simple_use (vinfo, slp_node, els_index,
    9801              :                                   &els, &els_op, &els_dt, &els_vectype))
    9802              :         return false;
    9803              :     }
    9804              : 
    9805       711530 :   tree vectype = SLP_TREE_VECTYPE (slp_node);
    9806       711530 :   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
    9807              : 
    9808       711530 :   if (loop_vinfo)
    9809              :     {
    9810       494386 :       loop = LOOP_VINFO_LOOP (loop_vinfo);
    9811       494386 :       nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
    9812       494386 :       vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
    9813              :     }
    9814              :   else
    9815              :     vf = 1;
    9816              : 
    9817       711530 :   vec_num = vect_get_num_copies (vinfo, slp_node);
    9818              : 
    9819              :   /* FORNOW. This restriction should be relaxed.  */
    9820       711530 :   if (nested_in_vect_loop && vec_num > 1)
    9821              :     {
    9822          316 :       if (dump_enabled_p ())
    9823           66 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9824              :                          "multiple types in nested loop.\n");
    9825          316 :       return false;
    9826              :     }
    9827              : 
    9828       711214 :   elem_type = TREE_TYPE (vectype);
    9829       711214 :   mode = TYPE_MODE (vectype);
    9830              : 
    9831              :   /* FORNOW. In some cases can vectorize even if data-type not supported
    9832              :     (e.g. - data copies).  */
    9833       711214 :   if (!can_implement_p (mov_optab, mode))
    9834              :     {
    9835            0 :       if (dump_enabled_p ())
    9836            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9837              :                          "Aligned load, but unsupported type.\n");
    9838            0 :       return false;
    9839              :     }
    9840              : 
    9841              :   /* Check if the load is a part of an interleaving chain.  */
    9842       711214 :   if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
    9843              :     {
    9844       311186 :       grouped_load = true;
    9845              :       /* FORNOW */
    9846       311186 :       gcc_assert (!nested_in_vect_loop);
    9847       311186 :       gcc_assert (!STMT_VINFO_GATHER_SCATTER_P (stmt_info));
    9848              : 
    9849       311186 :       first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
    9850       311186 :       group_size = DR_GROUP_SIZE (first_stmt_info);
    9851              : 
    9852              :       /* Invalidate assumptions made by dependence analysis when vectorization
    9853              :          on the unrolled body effectively re-orders stmts.  */
    9854       311186 :       if (STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
    9855       311186 :           && maybe_gt (LOOP_VINFO_VECT_FACTOR (loop_vinfo),
    9856              :                        STMT_VINFO_MIN_NEG_DIST (stmt_info)))
    9857              :         {
    9858           12 :           if (dump_enabled_p ())
    9859           12 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9860              :                              "cannot perform implicit CSE when performing "
    9861              :                              "group loads with negative dependence distance\n");
    9862           12 :           return false;
    9863              :         }
    9864              :     }
    9865              :   else
    9866              :     group_size = 1;
    9867              : 
    9868       711202 :   vect_load_store_data _ls_data{};
    9869       711202 :   vect_load_store_data &ls = slp_node->get_data (_ls_data);
    9870       711202 :   if (cost_vec
    9871       711202 :       && !get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask_node,
    9872              :                                VLS_LOAD, &ls))
    9873              :     return false;
    9874              :   /* Temporary aliases to analysis data, should not be modified through
    9875              :      these.  */
    9876       605504 :   const vect_memory_access_type memory_access_type = ls.memory_access_type;
    9877       605504 :   const dr_alignment_support alignment_support_scheme
    9878              :     = ls.alignment_support_scheme;
    9879       605504 :   const int misalignment = ls.misalignment;
    9880       605504 :   const poly_int64 poffset = ls.poffset;
    9881       605504 :   const vec<int> &elsvals = ls.elsvals;
    9882              : 
    9883       605504 :   int maskload_elsval = 0;
    9884       605504 :   bool need_zeroing = false;
    9885              : 
    9886              :   /* We might need to explicitly zero inactive elements if there are
    9887              :      padding bits in the type that might leak otherwise.
    9888              :      Refer to PR115336.  */
    9889       605504 :   tree scalar_type = TREE_TYPE (scalar_dest);
    9890       605504 :   bool type_mode_padding_p
    9891      1211008 :     = TYPE_PRECISION (scalar_type) < GET_MODE_PRECISION (GET_MODE_INNER (mode));
    9892              : 
    9893       605504 :   if (slp_node->ldst_lanes
    9894            0 :       && memory_access_type != VMAT_LOAD_STORE_LANES)
    9895              :     {
    9896            0 :       if (dump_enabled_p ())
    9897            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9898              :                          "discovered load-lane but cannot use it.\n");
    9899            0 :       return false;
    9900              :     }
    9901              : 
    9902       605504 :   if (mask_node)
    9903              :     {
    9904         2966 :       if (memory_access_type == VMAT_CONTIGUOUS)
    9905              :         {
    9906         2100 :           machine_mode vec_mode = TYPE_MODE (vectype);
    9907          721 :           if (!VECTOR_MODE_P (vec_mode)
    9908         4200 :               || !can_vec_mask_load_store_p (vec_mode,
    9909         2100 :                                              TYPE_MODE (mask_vectype),
    9910              :                                              true, NULL, &ls.elsvals))
    9911          351 :             return false;
    9912              :         }
    9913          866 :       else if (memory_access_type == VMAT_ELEMENTWISE
    9914          866 :                || memory_access_type == VMAT_STRIDED_SLP)
    9915              :         {
    9916            0 :           if (dump_enabled_p ())
    9917            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9918              :                              "unsupported masked strided access.\n");
    9919            0 :           return false;
    9920              :         }
    9921          866 :       else if (memory_access_type != VMAT_LOAD_STORE_LANES
    9922          866 :                && !mat_gather_scatter_p (memory_access_type))
    9923              :         {
    9924           62 :           if (dump_enabled_p ())
    9925            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9926              :                              "unsupported access type for masked load.\n");
    9927           62 :           return false;
    9928              :         }
    9929          804 :       else if (memory_access_type == VMAT_GATHER_SCATTER_EMULATED)
    9930              :         {
    9931          482 :           if (dump_enabled_p ())
    9932           28 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9933              :                              "unsupported masked emulated gather.\n");
    9934          482 :           return false;
    9935              :         }
    9936              :     }
    9937              : 
    9938       604609 :   bool costing_p = cost_vec;
    9939              : 
    9940       604609 :   if (costing_p) /* transformation not required.  */
    9941              :     {
    9942       438376 :       if (loop_vinfo
    9943       313697 :           && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
    9944       212430 :         check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
    9945              :                                               VLS_LOAD, group_size, &ls,
    9946              :                                               mask_node, &ls.elsvals);
    9947              : 
    9948              :       /* If the type needs padding we must zero inactive elements.
    9949              :          Check if we can do that with a VEC_COND_EXPR and store the
    9950              :          elsval we choose in MASKLOAD_ELSVAL.  */
    9951       438376 :       if (ls.elsvals.length ()
    9952        60180 :           && type_mode_padding_p
    9953            7 :           && !ls.elsvals.contains (MASK_LOAD_ELSE_ZERO)
    9954        60180 :           && !expand_vec_cond_expr_p (vectype, truth_type_for (vectype)))
    9955              :         {
    9956            0 :           if (dump_enabled_p ())
    9957            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9958              :                              "cannot zero inactive elements.\n");
    9959            0 :           return false;
    9960              :         }
    9961              : 
    9962       438376 :       if (mask_node
    9963       438376 :           && !vect_maybe_update_slp_op_vectype (mask_node,
    9964              :                                                 mask_vectype))
    9965              :         {
    9966            0 :           if (dump_enabled_p ())
    9967            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
    9968              :                              "incompatible vector types for invariants\n");
    9969            0 :           return false;
    9970              :         }
    9971              : 
    9972       438376 :       if (dump_enabled_p ()
    9973        25433 :           && memory_access_type != VMAT_ELEMENTWISE
    9974        25322 :           && !mat_gather_scatter_p (memory_access_type)
    9975        25005 :           && memory_access_type != VMAT_STRIDED_SLP
    9976        25005 :           && memory_access_type != VMAT_INVARIANT
    9977       462450 :           && alignment_support_scheme != dr_aligned)
    9978         9896 :         dump_printf_loc (MSG_NOTE, vect_location,
    9979              :                          "Vectorizing an unaligned access.\n");
    9980              : 
    9981       438376 :       if (memory_access_type == VMAT_LOAD_STORE_LANES)
    9982            0 :         vinfo->any_known_not_updated_vssa = true;
    9983              :     }
    9984              : 
    9985              :   /* For now just use the first available else value.
    9986              :      get_supported_else_vals tries MASK_LOAD_ELSE_ZERO first so we will
    9987              :      select it here if it is supported.  */
    9988       604609 :   if (elsvals.length ())
    9989        83279 :     maskload_elsval = *elsvals.begin ();
    9990              : 
    9991       604609 :   if (dump_enabled_p () && !costing_p)
    9992        16642 :     dump_printf_loc (MSG_NOTE, vect_location, "transform load.\n");
    9993              : 
    9994              :   /* Transform.  */
    9995              : 
    9996       604609 :   dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
    9997       604609 :   ensure_base_align (dr_info);
    9998              : 
    9999       604609 :   if (memory_access_type == VMAT_INVARIANT)
   10000              :     {
   10001         4169 :       gcc_assert (!grouped_load && !mask_node && !bb_vinfo);
   10002              :       /* If we have versioned for aliasing or the loop doesn't
   10003              :          have any data dependencies that would preclude this,
   10004              :          then we are sure this is a loop invariant load and
   10005              :          thus we can insert it on the preheader edge.
   10006              :          TODO: hoist_defs_of_uses should ideally be computed
   10007              :          once at analysis time, remembered and used in the
   10008              :          transform time.  */
   10009         8338 :       bool hoist_p = (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
   10010         4169 :                       && !nested_in_vect_loop);
   10011              : 
   10012         4169 :       bool uniform_p = true;
   10013        17506 :       for (stmt_vec_info sinfo : SLP_TREE_SCALAR_STMTS (slp_node))
   10014              :         {
   10015              :           /* It is unsafe to hoist a conditional load over the conditions that
   10016              :              make it valid.  When early break this means that any invariant load
   10017              :              can't be hoisted unless it's in the loop header or if we know
   10018              :              something else has verified the load is valid to do.  Alignment
   10019              :              peeling would do this since getting through the prologue means the
   10020              :              load was done at least once and so the vector main body is free to
   10021              :              hoist it.  However today GCC will hoist the load above the PFA
   10022              :              loop.  As such that makes it still invalid and so we can't allow it
   10023              :              today.  */
   10024         4999 :           if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
   10025         1052 :               && !DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (sinfo))
   10026         6019 :               && gimple_bb (STMT_VINFO_STMT (vect_orig_stmt (sinfo)))
   10027         1020 :                   != loop->header)
   10028              :             {
   10029          920 :               if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
   10030          920 :                   && dump_enabled_p ())
   10031            6 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   10032              :                              "not hoisting invariant load due to early break"
   10033              :                              "constraints\n");
   10034          914 :               else if (dump_enabled_p ())
   10035           16 :                dump_printf_loc (MSG_NOTE, vect_location,
   10036              :                              "not hoisting invariant load due to early break"
   10037              :                              "constraints\n");
   10038              :             hoist_p = false;
   10039              :           }
   10040              : 
   10041         4079 :           hoist_p = hoist_p && hoist_defs_of_uses (sinfo->stmt, loop, false);
   10042         4999 :           if (sinfo != SLP_TREE_SCALAR_STMTS (slp_node)[0])
   10043          279 :             uniform_p = false;
   10044              :         }
   10045         4169 :       if (costing_p)
   10046              :         {
   10047         3319 :           if (!uniform_p && (!hoist_p || !vf.is_constant ()))
   10048              :             {
   10049            0 :               if (dump_enabled_p ())
   10050            0 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   10051              :                                  "not vectorizing non-uniform invariant "
   10052              :                                  "load\n");
   10053            0 :               return false;
   10054              :             }
   10055         1423 :           enum vect_cost_model_location cost_loc
   10056         3319 :             = hoist_p ? vect_prologue : vect_body;
   10057         3319 :           unsigned int cost = record_stmt_cost (cost_vec, 1, scalar_load,
   10058              :                                                 slp_node, 0, cost_loc);
   10059         3319 :           cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
   10060              :                                     slp_node, 0, cost_loc);
   10061         3319 :           unsigned int prologue_cost = hoist_p ? cost : 0;
   10062         1423 :           unsigned int inside_cost = hoist_p ? 0 : cost;
   10063         3319 :           if (dump_enabled_p ())
   10064          546 :             dump_printf_loc (MSG_NOTE, vect_location,
   10065              :                              "vect_model_load_cost: inside_cost = %d, "
   10066              :                              "prologue_cost = %d .\n",
   10067              :                              inside_cost, prologue_cost);
   10068         3319 :           SLP_TREE_TYPE (slp_node) = load_vec_info_type;
   10069         3319 :           slp_node->data = new vect_load_store_data (std::move (ls));
   10070         3319 :           return true;
   10071              :         }
   10072          850 :       if (hoist_p)
   10073              :         {
   10074              :           /* ???  For non-uniform lanes there could be still duplicates.
   10075              :              We're leaving those to post-vectorizer CSE for the moment.  */
   10076          653 :           auto_vec<tree> scalar_defs (SLP_TREE_LANES (slp_node));
   10077         2100 :           for (stmt_vec_info sinfo : SLP_TREE_SCALAR_STMTS (slp_node))
   10078              :             {
   10079          743 :               gassign *stmt = as_a <gassign *> (sinfo->stmt);
   10080          743 :               if (dump_enabled_p ())
   10081          352 :                 dump_printf_loc (MSG_NOTE, vect_location,
   10082              :                                  "hoisting out of the vectorized loop: %G",
   10083              :                                  (gimple *) stmt);
   10084          743 :               scalar_dest = copy_ssa_name (gimple_assign_lhs (stmt));
   10085          743 :               tree rhs = unshare_expr (gimple_assign_rhs1 (stmt));
   10086          743 :               edge pe = loop_preheader_edge (loop);
   10087          743 :               gphi *vphi = get_virtual_phi (loop->header);
   10088          743 :               tree vuse;
   10089          743 :               if (vphi)
   10090          737 :                 vuse = PHI_ARG_DEF_FROM_EDGE (vphi, pe);
   10091              :               else
   10092            6 :                 vuse = gimple_vuse (gsi_stmt (*gsi));
   10093          743 :               gimple *new_stmt = gimple_build_assign (scalar_dest, rhs);
   10094          743 :               gimple_set_vuse (new_stmt, vuse);
   10095          743 :               gsi_insert_on_edge_immediate (pe, new_stmt);
   10096          743 :               hoist_defs_of_uses (new_stmt, loop, true);
   10097          743 :               if (!useless_type_conversion_p (TREE_TYPE (vectype),
   10098          743 :                                               TREE_TYPE (scalar_dest)))
   10099              :                 {
   10100           12 :                   tree tem = make_ssa_name (TREE_TYPE (vectype));
   10101           12 :                   new_stmt = gimple_build_assign (tem,
   10102              :                                                   NOP_EXPR, scalar_dest);
   10103           12 :                   gsi_insert_on_edge_immediate (pe, new_stmt);
   10104           12 :                   scalar_dest = tem;
   10105              :                 }
   10106          743 :               scalar_defs.quick_push (scalar_dest);
   10107          743 :               if (uniform_p)
   10108              :                 break;
   10109              :             }
   10110          653 :           if (!uniform_p)
   10111              :             {
   10112           51 :               unsigned const_nunits
   10113           51 :                 = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
   10114          116 :               for (j = 0; j < (int) vec_num; ++j)
   10115              :                 {
   10116           65 :                   vec<constructor_elt, va_gc> *v = NULL;
   10117           65 :                   vec_safe_reserve (v, const_nunits, true);
   10118          369 :                   for (unsigned i = 0; i < const_nunits; ++i)
   10119              :                     {
   10120          304 :                       unsigned def_idx
   10121          304 :                         = (j * const_nunits + i) % SLP_TREE_LANES (slp_node);
   10122          304 :                       CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
   10123              :                                               scalar_defs[def_idx]);
   10124              :                     }
   10125           65 :                   scalar_dest = build_constructor (vectype, v);
   10126           65 :                   new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
   10127              :                                                vectype, NULL);
   10128           65 :                   slp_node->push_vec_def (new_temp);
   10129              :                 }
   10130           51 :               return true;
   10131              :             }
   10132          602 :           new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
   10133              :                                        vectype, NULL);
   10134          653 :         }
   10135              :       else
   10136              :         {
   10137          197 :           gcc_assert (uniform_p);
   10138          197 :           gimple_stmt_iterator gsi2 = *gsi;
   10139          197 :           gsi_next (&gsi2);
   10140          197 :           new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
   10141              :                                        vectype, &gsi2);
   10142              :         }
   10143         1672 :       for (j = 0; j < (int) vec_num; ++j)
   10144          873 :         slp_node->push_vec_def (new_temp);
   10145              :       return true;
   10146              :     }
   10147              : 
   10148       600440 :   if (memory_access_type == VMAT_ELEMENTWISE
   10149       600440 :       || memory_access_type == VMAT_STRIDED_SLP)
   10150              :     {
   10151        23021 :       gimple_stmt_iterator incr_gsi;
   10152        23021 :       bool insert_after;
   10153        23021 :       tree offvar = NULL_TREE;
   10154        23021 :       tree ivstep;
   10155        23021 :       tree running_off;
   10156        23021 :       vec<constructor_elt, va_gc> *v = NULL;
   10157        23021 :       tree stride_base, stride_step = NULL_TREE, alias_off;
   10158              :       /* Checked by get_load_store_type.  */
   10159        23021 :       unsigned int const_nunits = nunits.to_constant ();
   10160        23021 :       unsigned HOST_WIDE_INT cst_offset = 0;
   10161        23021 :       tree dr_offset;
   10162        23021 :       unsigned int inside_cost = 0;
   10163              : 
   10164        23021 :       gcc_assert (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo));
   10165        23021 :       gcc_assert (!nested_in_vect_loop);
   10166              : 
   10167        23021 :       if (grouped_load)
   10168              :         {
   10169              :           /* If we elided a consecutive load permutation, don't
   10170              :              use the original first statement (which could be elided)
   10171              :              but the one the load permutation starts with.
   10172              :              This ensures the stride_base below is correct.  */
   10173        10304 :           if (!ls.subchain_p)
   10174        10260 :             first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
   10175              :           else
   10176           44 :             first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
   10177        10304 :           first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
   10178        10304 :           ref_type = get_group_alias_ptr_type (first_stmt_info);
   10179              :         }
   10180              :       else
   10181              :         {
   10182        12717 :           first_stmt_info = stmt_info;
   10183        12717 :           first_dr_info = dr_info;
   10184        12717 :           ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
   10185              :         }
   10186              : 
   10187        23021 :       if (grouped_load)
   10188              :         {
   10189        10304 :           if (memory_access_type == VMAT_STRIDED_SLP)
   10190              :             {
   10191              :               /* If we elided a consecutive load permutation, adjust
   10192              :                  the group size here.  */
   10193         3795 :               if (!ls.subchain_p)
   10194         3751 :                 group_size = DR_GROUP_SIZE (first_stmt_info);
   10195              :               else
   10196           44 :                 group_size = SLP_TREE_LANES (slp_node);
   10197              :             }
   10198              :           else /* VMAT_ELEMENTWISE */
   10199         6509 :             group_size = SLP_TREE_LANES (slp_node);
   10200              :         }
   10201              :       else
   10202              :         group_size = 1;
   10203              : 
   10204        23021 :       if (!costing_p)
   10205              :         {
   10206         3201 :           dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
   10207         3201 :           stride_base = fold_build_pointer_plus (
   10208              :             DR_BASE_ADDRESS (first_dr_info->dr),
   10209              :             size_binop (PLUS_EXPR, convert_to_ptrofftype (dr_offset),
   10210              :                         convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
   10211         3201 :           stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
   10212              : 
   10213              :           /* For a load with loop-invariant (but other than power-of-2)
   10214              :              stride (i.e. not a grouped access) like so:
   10215              : 
   10216              :                for (i = 0; i < n; i += stride)
   10217              :                  ... = array[i];
   10218              : 
   10219              :              we generate a new induction variable and new accesses to
   10220              :              form a new vector (or vectors, depending on ncopies):
   10221              : 
   10222              :                for (j = 0; ; j += VF*stride)
   10223              :                  tmp1 = array[j];
   10224              :                  tmp2 = array[j + stride];
   10225              :                  ...
   10226              :                  vectemp = {tmp1, tmp2, ...}
   10227              :              */
   10228              : 
   10229         3201 :           ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (stride_step), stride_step,
   10230              :                                 build_int_cst (TREE_TYPE (stride_step), vf));
   10231              : 
   10232         3201 :           standard_iv_increment_position (loop, &incr_gsi, &insert_after);
   10233              : 
   10234         3201 :           stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
   10235         3201 :           ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
   10236         3201 :           create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
   10237              :                      loop, &incr_gsi, insert_after,
   10238              :                      &offvar, NULL);
   10239              : 
   10240         3201 :           stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
   10241              :         }
   10242              : 
   10243        23021 :       running_off = offvar;
   10244        23021 :       alias_off = build_int_cst (ref_type, 0);
   10245        23021 :       int nloads = const_nunits;
   10246        23021 :       int lnel = 1;
   10247        23021 :       tree ltype = TREE_TYPE (vectype);
   10248        23021 :       tree lvectype = vectype;
   10249        23021 :       auto_vec<tree> dr_chain;
   10250              :       /* ???  Modify local copies of alignment_support_scheme and
   10251              :          misalignment, but this part of analysis should be done
   10252              :          earlier and remembered, likewise the chosen load mode.  */
   10253        23021 :       const dr_alignment_support tem = alignment_support_scheme;
   10254        23021 :       dr_alignment_support alignment_support_scheme = tem;
   10255        23021 :       const int tem2 = misalignment;
   10256        23021 :       int misalignment = tem2;
   10257        23021 :       if (memory_access_type == VMAT_STRIDED_SLP)
   10258              :         {
   10259        16512 :           HOST_WIDE_INT n = gcd (group_size, const_nunits);
   10260              :           /* Use the target vector type if the group size is a multiple
   10261              :              of it.  */
   10262        16512 :           if (n == const_nunits)
   10263              :             {
   10264         2143 :               int mis_align = dr_misalignment (first_dr_info, vectype);
   10265              :               /* With VF > 1 we advance the DR by step, if that is constant
   10266              :                  and only aligned when performed VF times, DR alignment
   10267              :                  analysis can analyze this as aligned since it assumes
   10268              :                  contiguous accesses.  But that is not how we code generate
   10269              :                  here, so adjust for this.  */
   10270         2143 :               if (maybe_gt (vf, 1u)
   10271         3415 :                   && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
   10272         3203 :                                   DR_TARGET_ALIGNMENT (first_dr_info)))
   10273          212 :                 mis_align = -1;
   10274         2143 :               dr_alignment_support dr_align
   10275         2143 :                 = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
   10276              :                                                  mis_align);
   10277         2143 :               if (dr_align == dr_aligned
   10278         2143 :                   || dr_align == dr_unaligned_supported)
   10279              :                 {
   10280        16512 :                   nloads = 1;
   10281        16512 :                   lnel = const_nunits;
   10282        16512 :                   ltype = vectype;
   10283        16512 :                   alignment_support_scheme = dr_align;
   10284        16512 :                   misalignment = mis_align;
   10285              :                 }
   10286              :             }
   10287              :           /* Else use the biggest vector we can load the group without
   10288              :              accessing excess elements.  */
   10289        14369 :           else if (n > 1)
   10290              :             {
   10291         1653 :               tree ptype;
   10292         1653 :               tree vtype
   10293         1653 :                 = vector_vector_composition_type (vectype, const_nunits / n,
   10294              :                                                   &ptype);
   10295         1653 :               if (vtype != NULL_TREE)
   10296              :                 {
   10297         1623 :                   dr_alignment_support dr_align;
   10298         1623 :                   int mis_align = 0;
   10299         1623 :                   if (VECTOR_TYPE_P (ptype))
   10300              :                     {
   10301          733 :                       mis_align = dr_misalignment (first_dr_info, ptype);
   10302          733 :                       if (maybe_gt (vf, 1u)
   10303         1436 :                           && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
   10304          739 :                                           DR_TARGET_ALIGNMENT (first_dr_info)))
   10305          697 :                         mis_align = -1;
   10306          733 :                       dr_align
   10307          733 :                         = vect_supportable_dr_alignment (vinfo, dr_info, ptype,
   10308              :                                                          mis_align);
   10309              :                     }
   10310              :                   else
   10311              :                     dr_align = dr_unaligned_supported;
   10312         1623 :                   if (dr_align == dr_aligned
   10313         1623 :                       || dr_align == dr_unaligned_supported)
   10314              :                     {
   10315         1623 :                       nloads = const_nunits / n;
   10316         1623 :                       lnel = n;
   10317         1623 :                       lvectype = vtype;
   10318         1623 :                       ltype = ptype;
   10319         1623 :                       alignment_support_scheme = dr_align;
   10320         1623 :                       misalignment = mis_align;
   10321              :                     }
   10322              :                 }
   10323              :             }
   10324        16512 :           unsigned align;
   10325        16512 :           if (alignment_support_scheme == dr_aligned)
   10326           20 :             align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
   10327              :           else
   10328        16492 :             align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
   10329              :           /* Alignment is at most the access size if we do multiple loads.  */
   10330        16512 :           if (nloads > 1)
   10331        14369 :             align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
   10332        16512 :           ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
   10333              :         }
   10334              : 
   10335        23021 :       if (costing_p)
   10336              :         /* Record the composition type for target access during costing.  */
   10337        19820 :         ls.ls_type = lvectype;
   10338              :       else
   10339         3201 :         gcc_assert (ls.ls_type == lvectype);
   10340              : 
   10341              :       /* For SLP permutation support we need to load the whole group,
   10342              :          not only the number of vector stmts the permutation result
   10343              :          fits in.  */
   10344        23021 :       int ncopies;
   10345        23021 :       if (ls.slp_perm)
   10346              :         {
   10347         2403 :           gcc_assert (memory_access_type != VMAT_ELEMENTWISE);
   10348              :           /* We don't yet generate SLP_TREE_LOAD_PERMUTATIONs for
   10349              :              variable VF.  */
   10350         2403 :           unsigned int const_vf = vf.to_constant ();
   10351         2403 :           ncopies = CEIL (group_size * const_vf, const_nunits);
   10352         2403 :           dr_chain.create (ncopies);
   10353              :         }
   10354              :       else
   10355              :         ncopies = vec_num;
   10356              : 
   10357        23021 :       unsigned int group_el = 0;
   10358        23021 :       unsigned HOST_WIDE_INT
   10359        23021 :         elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
   10360        23021 :       unsigned int n_groups = 0;
   10361              :       /* For costing some adjacent vector loads, we'd like to cost with
   10362              :          the total number of them once instead of cost each one by one. */
   10363        23021 :       unsigned int n_adjacent_loads = 0;
   10364        54676 :       for (j = 0; j < ncopies; j++)
   10365              :         {
   10366        31655 :           if (nloads > 1 && !costing_p)
   10367         2566 :             vec_alloc (v, nloads);
   10368              :           gimple *new_stmt = NULL;
   10369       134698 :           for (i = 0; i < nloads; i++)
   10370              :             {
   10371       103043 :               if (costing_p)
   10372              :                 {
   10373              :                   /* For VMAT_ELEMENTWISE, just cost it as scalar_load to
   10374              :                      avoid ICE, see PR110776.  */
   10375        93864 :                   if (VECTOR_TYPE_P (ltype)
   10376         4916 :                       && memory_access_type != VMAT_ELEMENTWISE)
   10377         4916 :                     n_adjacent_loads++;
   10378              :                   else
   10379        88948 :                     inside_cost += record_stmt_cost (cost_vec, 1, scalar_load,
   10380              :                                                      slp_node, 0, vect_body);
   10381        93864 :                   continue;
   10382              :                 }
   10383         9179 :               unsigned int load_el = group_el;
   10384              :               /* For elementwise accesses apply a load permutation directly.  */
   10385         9179 :               if (memory_access_type == VMAT_ELEMENTWISE
   10386         9179 :                   && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
   10387         2006 :                 load_el = SLP_TREE_LOAD_PERMUTATION (slp_node)[group_el];
   10388         9179 :               tree this_off = build_int_cst (TREE_TYPE (alias_off),
   10389         9179 :                                              load_el * elsz + cst_offset);
   10390         9179 :               tree data_ref = build2 (MEM_REF, ltype, running_off, this_off);
   10391         9179 :               vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
   10392         9179 :               new_temp = make_ssa_name (ltype);
   10393         9179 :               new_stmt = gimple_build_assign (new_temp, data_ref);
   10394         9179 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   10395         9179 :               if (nloads > 1)
   10396         7460 :                 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_temp);
   10397              : 
   10398         9179 :               group_el += lnel;
   10399         9179 :               if (group_el == group_size)
   10400              :                 {
   10401         8808 :                   n_groups++;
   10402              :                   /* When doing SLP make sure to not load elements from
   10403              :                      the next vector iteration, those will not be accessed
   10404              :                      so just use the last element again.  See PR107451.  */
   10405         8808 :                   if (known_lt (n_groups, vf))
   10406              :                     {
   10407         5587 :                       tree newoff = copy_ssa_name (running_off);
   10408         5587 :                       gimple *incr
   10409         5587 :                         = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
   10410              :                                                running_off, stride_step);
   10411         5587 :                       vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
   10412         5587 :                       running_off = newoff;
   10413              :                     }
   10414              :                   group_el = 0;
   10415              :                 }
   10416              :             }
   10417              : 
   10418        31655 :           if (nloads > 1)
   10419              :             {
   10420        23020 :               if (costing_p)
   10421        20454 :                 inside_cost += record_stmt_cost (cost_vec, 1, vec_construct,
   10422              :                                                  slp_node, 0, vect_body);
   10423              :               else
   10424              :                 {
   10425         2566 :                   tree vec_inv = build_constructor (lvectype, v);
   10426         2566 :                   new_temp = vect_init_vector (vinfo, stmt_info, vec_inv,
   10427              :                                                lvectype, gsi);
   10428         2566 :                   new_stmt = SSA_NAME_DEF_STMT (new_temp);
   10429         2566 :                   if (lvectype != vectype)
   10430              :                     {
   10431          232 :                       new_stmt
   10432          232 :                         = gimple_build_assign (make_ssa_name (vectype),
   10433              :                                                VIEW_CONVERT_EXPR,
   10434              :                                                build1 (VIEW_CONVERT_EXPR,
   10435              :                                                        vectype, new_temp));
   10436          232 :                       vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
   10437              :                                                    gsi);
   10438              :                     }
   10439              :                 }
   10440              :             }
   10441         8635 :           else if (!costing_p && ltype != vectype)
   10442              :             {
   10443         1700 :               new_stmt = gimple_build_assign (make_ssa_name (vectype),
   10444              :                                               VIEW_CONVERT_EXPR,
   10445              :                                               build1 (VIEW_CONVERT_EXPR,
   10446              :                                                       vectype, new_temp));
   10447         1700 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
   10448              :                                            gsi);
   10449              :             }
   10450              : 
   10451        31655 :           if (!costing_p)
   10452              :             {
   10453         4285 :               if (ls.slp_perm)
   10454         1208 :                 dr_chain.quick_push (gimple_assign_lhs (new_stmt));
   10455              :               else
   10456         3077 :                 slp_node->push_vec_def (new_stmt);
   10457              :             }
   10458              :         }
   10459        23021 :       if (ls.slp_perm)
   10460              :         {
   10461         2403 :           if (costing_p)
   10462              :             {
   10463         1847 :               gcc_assert (ls.n_perms != -1U);
   10464         1847 :               inside_cost += record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
   10465              :                                                slp_node, 0, vect_body);
   10466              :             }
   10467              :           else
   10468              :             {
   10469          556 :               unsigned n_perms2;
   10470          556 :               vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, vf,
   10471              :                                             false, &n_perms2);
   10472          556 :               gcc_assert (ls.n_perms == n_perms2);
   10473              :             }
   10474              :         }
   10475              : 
   10476        23021 :       if (costing_p)
   10477              :         {
   10478        19820 :           if (n_adjacent_loads > 0)
   10479         1938 :             vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
   10480              :                                 alignment_support_scheme, misalignment, false,
   10481              :                                 &inside_cost, nullptr, cost_vec, cost_vec,
   10482              :                                 true);
   10483        19820 :           if (dump_enabled_p ())
   10484          496 :             dump_printf_loc (MSG_NOTE, vect_location,
   10485              :                              "vect_model_load_cost: inside_cost = %u, "
   10486              :                              "prologue_cost = 0 .\n",
   10487              :                              inside_cost);
   10488        19820 :           SLP_TREE_TYPE (slp_node) = load_vec_info_type;
   10489        19820 :           slp_node->data = new vect_load_store_data (std::move (ls));
   10490              :         }
   10491              : 
   10492        23021 :       return true;
   10493        23021 :     }
   10494              : 
   10495       577419 :   if (mat_gather_scatter_p (memory_access_type)
   10496       577419 :       && !ls.ls_type)
   10497              :     grouped_load = false;
   10498              : 
   10499       574418 :   if (grouped_load
   10500       577419 :       || SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
   10501              :     {
   10502       265304 :       if (grouped_load)
   10503              :         {
   10504       264862 :           first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
   10505       264862 :           group_size = DR_GROUP_SIZE (first_stmt_info);
   10506              :         }
   10507              :       else
   10508              :         {
   10509              :           first_stmt_info = stmt_info;
   10510              :           group_size = 1;
   10511              :         }
   10512              :       /* For SLP vectorization we directly vectorize a subchain
   10513              :          without permutation.  */
   10514       265304 :       if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
   10515       210994 :         first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
   10516              :       /* For BB vectorization always use the first stmt to base
   10517              :          the data ref pointer on.  */
   10518       265304 :       if (bb_vinfo)
   10519       210770 :         first_stmt_info_for_drptr
   10520       210770 :           = vect_find_first_scalar_stmt_in_slp (slp_node);
   10521              : 
   10522       265304 :       first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
   10523       265304 :       group_gap_adj = 0;
   10524              : 
   10525              :       /* VEC_NUM is the number of vect stmts to be created for this group.  */
   10526       265304 :       grouped_load = false;
   10527              :       /* If an SLP permutation is from N elements to N elements,
   10528              :          and if one vector holds a whole number of N, we can load
   10529              :          the inputs to the permutation in the same way as an
   10530              :          unpermuted sequence.  In other cases we need to load the
   10531              :          whole group, not only the number of vector stmts the
   10532              :          permutation result fits in.  */
   10533       265304 :       unsigned scalar_lanes = SLP_TREE_LANES (slp_node);
   10534       265304 :       if (nested_in_vect_loop)
   10535              :         /* We do not support grouped accesses in a nested loop,
   10536              :            instead the access is contiguous but it might be
   10537              :            permuted.  No gap adjustment is needed though.  */
   10538              :         ;
   10539       265302 :       else if (ls.slp_perm
   10540       265302 :                && (group_size != scalar_lanes
   10541        11332 :                    || !multiple_p (nunits, group_size)))
   10542              :         {
   10543              :           /* We don't yet generate such SLP_TREE_LOAD_PERMUTATIONs for
   10544              :              variable VF; see vect_transform_slp_perm_load.  */
   10545        44143 :           unsigned int const_vf = vf.to_constant ();
   10546        44143 :           unsigned int const_nunits = nunits.to_constant ();
   10547        44143 :           vec_num = CEIL (group_size * const_vf, const_nunits);
   10548        44143 :           group_gap_adj = vf * group_size - nunits * vec_num;
   10549              :         }
   10550              :       else
   10551              :         {
   10552       221159 :           group_gap_adj = group_size - scalar_lanes;
   10553              :         }
   10554              : 
   10555       265304 :       ref_type = get_group_alias_ptr_type (first_stmt_info);
   10556              :     }
   10557              :   else
   10558              :     {
   10559       312115 :       first_stmt_info = stmt_info;
   10560       312115 :       first_dr_info = dr_info;
   10561       312115 :       group_size = 1;
   10562       312115 :       group_gap_adj = 0;
   10563       312115 :       ref_type = reference_alias_ptr_type (DR_REF (first_dr_info->dr));
   10564              :     }
   10565              : 
   10566       577419 :   vec_loop_masks *loop_masks
   10567       366649 :     = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
   10568       577419 :        ? &LOOP_VINFO_MASKS (loop_vinfo)
   10569           31 :        : NULL);
   10570           31 :   vec_loop_lens *loop_lens
   10571       366649 :     = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
   10572              :        ? &LOOP_VINFO_LENS (loop_vinfo)
   10573            0 :        : NULL);
   10574              : 
   10575              :   /* The vect_transform_stmt and vect_analyze_stmt will go here but there
   10576              :      are some difference here.  We cannot enable both the lens and masks
   10577              :      during transform but it is allowed during analysis.
   10578              :      Shouldn't go with length-based approach if fully masked.  */
   10579       577419 :   if (cost_vec == NULL)
   10580              :     /* The cost_vec is NULL during transfrom.  */
   10581       162182 :     gcc_assert ((!loop_lens || !loop_masks));
   10582              : 
   10583              :   /* Targets with store-lane instructions must not require explicit
   10584              :      realignment.  vect_supportable_dr_alignment always returns either
   10585              :      dr_aligned or dr_unaligned_supported for (non-length) masked
   10586              :      operations.  */
   10587       577419 :   gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
   10588              :                && !mask_node
   10589              :                && !loop_masks)
   10590              :               || mat_gather_scatter_p (memory_access_type)
   10591              :               || alignment_support_scheme == dr_aligned
   10592              :               || alignment_support_scheme == dr_unaligned_supported);
   10593              : 
   10594              :   /* In case the vectorization factor (VF) is bigger than the number
   10595              :      of elements that we can fit in a vectype (nunits), we have to generate
   10596              :      more than one vector stmt - i.e - we need to "unroll" the
   10597              :      vector stmt by a factor VF/nunits.  In doing so, we record a pointer
   10598              :      from one copy of the vector stmt to the next, in the field
   10599              :      STMT_VINFO_RELATED_STMT.  This is necessary in order to allow following
   10600              :      stages to find the correct vector defs to be used when vectorizing
   10601              :      stmts that use the defs of the current stmt.  The example below
   10602              :      illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
   10603              :      need to create 4 vectorized stmts):
   10604              : 
   10605              :      before vectorization:
   10606              :                                 RELATED_STMT    VEC_STMT
   10607              :         S1:     x = memref      -               -
   10608              :         S2:     z = x + 1       -               -
   10609              : 
   10610              :      step 1: vectorize stmt S1:
   10611              :         We first create the vector stmt VS1_0, and, as usual, record a
   10612              :         pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
   10613              :         Next, we create the vector stmt VS1_1, and record a pointer to
   10614              :         it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
   10615              :         Similarly, for VS1_2 and VS1_3.  This is the resulting chain of
   10616              :         stmts and pointers:
   10617              :                                 RELATED_STMT    VEC_STMT
   10618              :         VS1_0:  vx0 = memref0   VS1_1           -
   10619              :         VS1_1:  vx1 = memref1   VS1_2           -
   10620              :         VS1_2:  vx2 = memref2   VS1_3           -
   10621              :         VS1_3:  vx3 = memref3   -               -
   10622              :         S1:     x = load        -               VS1_0
   10623              :         S2:     z = x + 1       -               -
   10624              :   */
   10625              : 
   10626              :   /* If the data reference is aligned (dr_aligned) or potentially unaligned
   10627              :      on a target that supports unaligned accesses (dr_unaligned_supported)
   10628              :      we generate the following code:
   10629              :          p = initial_addr;
   10630              :          indx = 0;
   10631              :          loop {
   10632              :            p = p + indx * vectype_size;
   10633              :            vec_dest = *(p);
   10634              :            indx = indx + 1;
   10635              :          }
   10636              : 
   10637              :      Otherwise, the data reference is potentially unaligned on a target that
   10638              :      does not support unaligned accesses (dr_explicit_realign_optimized) -
   10639              :      then generate the following code, in which the data in each iteration is
   10640              :      obtained by two vector loads, one from the previous iteration, and one
   10641              :      from the current iteration:
   10642              :          p1 = initial_addr;
   10643              :          msq_init = *(floor(p1))
   10644              :          p2 = initial_addr + VS - 1;
   10645              :          realignment_token = call target_builtin;
   10646              :          indx = 0;
   10647              :          loop {
   10648              :            p2 = p2 + indx * vectype_size
   10649              :            lsq = *(floor(p2))
   10650              :            vec_dest = realign_load (msq, lsq, realignment_token)
   10651              :            indx = indx + 1;
   10652              :            msq = lsq;
   10653              :          }   */
   10654              : 
   10655              :   /* If the misalignment remains the same throughout the execution of the
   10656              :      loop, we can create the init_addr and permutation mask at the loop
   10657              :      preheader.  Otherwise, it needs to be created inside the loop.
   10658              :      This can only occur when vectorizing memory accesses in the inner-loop
   10659              :      nested within an outer-loop that is being vectorized.  */
   10660              : 
   10661       577419 :   if (nested_in_vect_loop
   10662       577419 :       && !multiple_p (DR_STEP_ALIGNMENT (dr_info->dr),
   10663         1234 :                       GET_MODE_SIZE (TYPE_MODE (vectype))))
   10664              :     {
   10665          195 :       gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
   10666              :       compute_in_loop = true;
   10667              :     }
   10668              : 
   10669       577419 :   bool diff_first_stmt_info
   10670       577419 :     = first_stmt_info_for_drptr && first_stmt_info != first_stmt_info_for_drptr;
   10671              : 
   10672       577419 :   tree offset = NULL_TREE;
   10673       577419 :   if ((alignment_support_scheme == dr_explicit_realign_optimized
   10674       577419 :        || alignment_support_scheme == dr_explicit_realign)
   10675            0 :       && !compute_in_loop)
   10676              :     {
   10677              :       /* If we have different first_stmt_info, we can't set up realignment
   10678              :          here, since we can't guarantee first_stmt_info DR has been
   10679              :          initialized yet, use first_stmt_info_for_drptr DR by bumping the
   10680              :          distance from first_stmt_info DR instead as below.  */
   10681            0 :       if (!costing_p)
   10682              :         {
   10683            0 :           if (!diff_first_stmt_info)
   10684            0 :             msq = vect_setup_realignment (vinfo, first_stmt_info, vectype, gsi,
   10685              :                                           &realignment_token,
   10686              :                                           alignment_support_scheme, NULL_TREE,
   10687              :                                           &at_loop);
   10688            0 :           if (alignment_support_scheme == dr_explicit_realign_optimized)
   10689              :             {
   10690            0 :               phi = as_a<gphi *> (SSA_NAME_DEF_STMT (msq));
   10691            0 :               offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
   10692              :                                    size_one_node);
   10693            0 :               gcc_assert (!first_stmt_info_for_drptr);
   10694              :             }
   10695              :         }
   10696              :     }
   10697              :   else
   10698       577419 :     at_loop = loop;
   10699              : 
   10700       577419 :   if (!known_eq (poffset, 0))
   10701         4635 :     offset = (offset
   10702         4635 :               ? size_binop (PLUS_EXPR, offset, size_int (poffset))
   10703         4635 :               : size_int (poffset));
   10704              : 
   10705       577419 :   tree bump;
   10706       577419 :   tree vec_offset = NULL_TREE;
   10707              : 
   10708       577419 :   auto_vec<tree> vec_offsets;
   10709       577419 :   auto_vec<tree> vec_masks;
   10710       577419 :   if (mask_node && !costing_p)
   10711          636 :     vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[mask_index],
   10712              :                        &vec_masks);
   10713              : 
   10714       577419 :   tree vec_mask = NULL_TREE;
   10715       577419 :   tree vec_els = NULL_TREE;
   10716       577419 :   if (memory_access_type == VMAT_LOAD_STORE_LANES)
   10717              :     {
   10718            0 :       const internal_fn lanes_ifn = ls.lanes_ifn;
   10719              : 
   10720            0 :       gcc_assert (alignment_support_scheme == dr_aligned
   10721              :                   || alignment_support_scheme == dr_unaligned_supported);
   10722              : 
   10723            0 :       aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
   10724            0 :       if (!costing_p)
   10725            0 :         bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
   10726              :                                             memory_access_type, loop_lens);
   10727              : 
   10728            0 :       unsigned int inside_cost = 0, prologue_cost = 0;
   10729              :       /* For costing some adjacent vector loads, we'd like to cost with
   10730              :          the total number of them once instead of cost each one by one. */
   10731            0 :       unsigned int n_adjacent_loads = 0;
   10732            0 :       int ncopies = vec_num / group_size;
   10733            0 :       for (j = 0; j < ncopies; j++)
   10734              :         {
   10735            0 :           if (costing_p)
   10736              :             {
   10737              :               /* An IFN_LOAD_LANES will load all its vector results,
   10738              :                  regardless of which ones we actually need.  Account
   10739              :                  for the cost of unused results.  */
   10740            0 :               if (first_stmt_info == stmt_info)
   10741              :                 {
   10742            0 :                   unsigned int gaps = DR_GROUP_SIZE (first_stmt_info);
   10743            0 :                   stmt_vec_info next_stmt_info = first_stmt_info;
   10744            0 :                   do
   10745              :                     {
   10746            0 :                       gaps -= 1;
   10747            0 :                       next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
   10748              :                     }
   10749            0 :                   while (next_stmt_info);
   10750            0 :                   if (gaps)
   10751              :                     {
   10752            0 :                       if (dump_enabled_p ())
   10753            0 :                         dump_printf_loc (MSG_NOTE, vect_location,
   10754              :                                          "vect_model_load_cost: %d "
   10755              :                                          "unused vectors.\n",
   10756              :                                          gaps);
   10757            0 :                       vect_get_load_cost (vinfo, stmt_info, slp_node, gaps,
   10758              :                                           alignment_support_scheme,
   10759              :                                           misalignment, false, &inside_cost,
   10760              :                                           &prologue_cost, cost_vec, cost_vec,
   10761              :                                           true);
   10762              :                     }
   10763              :                 }
   10764            0 :               n_adjacent_loads++;
   10765            0 :               continue;
   10766            0 :             }
   10767              : 
   10768              :           /* 1. Create the vector or array pointer update chain.  */
   10769            0 :           if (j == 0)
   10770            0 :             dataref_ptr
   10771            0 :               = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
   10772              :                                           at_loop, offset, &dummy, gsi,
   10773              :                                           &ptr_incr, false, bump);
   10774              :           else
   10775              :             {
   10776            0 :               gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
   10777            0 :               dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
   10778              :                                              stmt_info, bump);
   10779              :             }
   10780            0 :           if (mask_node)
   10781            0 :             vec_mask = vec_masks[j];
   10782              : 
   10783            0 :           tree vec_array = create_vector_array (vectype, group_size);
   10784              : 
   10785            0 :           tree final_mask = NULL_TREE;
   10786            0 :           tree final_len = NULL_TREE;
   10787            0 :           tree bias = NULL_TREE;
   10788            0 :           if (loop_masks)
   10789            0 :             final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
   10790              :                                              ncopies, vectype, j);
   10791            0 :           if (vec_mask)
   10792            0 :             final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
   10793              :                                            vec_mask, gsi);
   10794              : 
   10795            0 :           if (lanes_ifn == IFN_MASK_LEN_LOAD_LANES)
   10796              :             {
   10797            0 :               if (loop_lens)
   10798            0 :                 final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
   10799              :                                                ncopies, vectype, j, 1, true);
   10800              :               else
   10801            0 :                 final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
   10802            0 :               signed char biasval
   10803            0 :                 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
   10804            0 :               bias = build_int_cst (intQI_type_node, biasval);
   10805            0 :               if (!final_mask)
   10806              :                 {
   10807            0 :                   mask_vectype = truth_type_for (vectype);
   10808            0 :                   final_mask = build_minus_one_cst (mask_vectype);
   10809              :                 }
   10810              :             }
   10811              : 
   10812            0 :           if (final_mask)
   10813              :             {
   10814            0 :               vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
   10815            0 :               if (type_mode_padding_p
   10816            0 :                   && maskload_elsval != MASK_LOAD_ELSE_ZERO)
   10817            0 :                 need_zeroing = true;
   10818              :             }
   10819              : 
   10820            0 :           gcall *call;
   10821            0 :           if (final_len && final_mask)
   10822              :             {
   10823              :               /* Emit:
   10824              :                    VEC_ARRAY = MASK_LEN_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
   10825              :                                                     VEC_MASK, LEN, BIAS).  */
   10826            0 :               unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
   10827            0 :               tree alias_ptr = build_int_cst (ref_type, align);
   10828            0 :               call = gimple_build_call_internal (IFN_MASK_LEN_LOAD_LANES, 6,
   10829              :                                                  dataref_ptr, alias_ptr,
   10830              :                                                  final_mask, vec_els,
   10831              :                                                  final_len, bias);
   10832              :             }
   10833            0 :           else if (final_mask)
   10834              :             {
   10835              :               /* Emit:
   10836              :                    VEC_ARRAY = MASK_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
   10837              :                                                 VEC_MASK).  */
   10838            0 :               unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
   10839            0 :               tree alias_ptr = build_int_cst (ref_type, align);
   10840            0 :               call = gimple_build_call_internal (IFN_MASK_LOAD_LANES, 4,
   10841              :                                                  dataref_ptr, alias_ptr,
   10842              :                                                  final_mask, vec_els);
   10843              :             }
   10844              :           else
   10845              :             {
   10846              :               /* Emit:
   10847              :                    VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]).  */
   10848            0 :               data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
   10849            0 :               call = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
   10850              :             }
   10851            0 :           gimple_call_set_lhs (call, vec_array);
   10852            0 :           gimple_call_set_nothrow (call, true);
   10853            0 :           vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
   10854              : 
   10855              :           /* Extract each vector into an SSA_NAME.  */
   10856            0 :           for (unsigned i = 0; i < group_size; i++)
   10857              :             {
   10858            0 :               new_temp = read_vector_array (vinfo, stmt_info, gsi, scalar_dest,
   10859              :                                             vec_array, i, need_zeroing,
   10860              :                                             final_mask);
   10861            0 :               slp_node->push_vec_def (new_temp);
   10862              :             }
   10863              : 
   10864              :           /* Record that VEC_ARRAY is now dead.  */
   10865            0 :           vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
   10866              :         }
   10867              : 
   10868            0 :       if (costing_p)
   10869              :         {
   10870            0 :           if (n_adjacent_loads > 0)
   10871            0 :             vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
   10872              :                                 alignment_support_scheme, misalignment, false,
   10873              :                                 &inside_cost, &prologue_cost, cost_vec,
   10874              :                                 cost_vec, true);
   10875            0 :           if (dump_enabled_p ())
   10876            0 :             dump_printf_loc (MSG_NOTE, vect_location,
   10877              :                              "vect_model_load_cost: inside_cost = %u, "
   10878              :                              "prologue_cost = %u .\n",
   10879              :                              inside_cost, prologue_cost);
   10880            0 :           SLP_TREE_TYPE (slp_node) = load_vec_info_type;
   10881            0 :           slp_node->data = new vect_load_store_data (std::move (ls));
   10882              :         }
   10883              : 
   10884            0 :       return true;
   10885              :     }
   10886              : 
   10887       577419 :   if (mat_gather_scatter_p (memory_access_type))
   10888              :     {
   10889         3001 :       gcc_assert ((!grouped_load && !ls.slp_perm) || ls.ls_type);
   10890              : 
   10891         3001 :       auto_vec<tree> dr_chain (vec_num);
   10892              : 
   10893              :       /* If we pun the original vectype the loads as well as costing, length,
   10894              :          etc. is performed with the new type.  After loading we VIEW_CONVERT
   10895              :          the data to the original vectype.  */
   10896         3001 :       tree original_vectype = vectype;
   10897         3001 :       if (ls.ls_type)
   10898            0 :         vectype = ls.ls_type;
   10899              : 
   10900              :       /* 1. Create the vector or array pointer update chain.  */
   10901         3001 :       if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
   10902              :         {
   10903         3001 :           aggr_type = NULL_TREE;
   10904         3001 :           bump = NULL_TREE;
   10905         3001 :           if (!costing_p)
   10906          747 :             vect_get_gather_scatter_ops (loop, slp_node, &dataref_ptr,
   10907              :                                          &vec_offsets);
   10908              :         }
   10909              :       else
   10910              :         {
   10911            0 :           aggr_type = elem_type;
   10912            0 :           if (!costing_p)
   10913              :             {
   10914            0 :               vect_get_strided_load_store_ops (stmt_info, slp_node, vectype,
   10915              :                                                ls.strided_offset_vectype,
   10916              :                                                loop_vinfo, gsi,
   10917              :                                                &bump, &vec_offset, loop_lens);
   10918            0 :               dataref_ptr
   10919            0 :                   = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
   10920              :                                               at_loop, offset, &dummy, gsi,
   10921              :                                               &ptr_incr, false, bump);
   10922              :             }
   10923              :         }
   10924              : 
   10925              :       unsigned int inside_cost = 0, prologue_cost = 0;
   10926              : 
   10927         6777 :       gimple *new_stmt = NULL;
   10928         6777 :       for (i = 0; i < vec_num; i++)
   10929              :         {
   10930         3776 :           tree final_mask = NULL_TREE;
   10931         3776 :           tree final_len = NULL_TREE;
   10932         3776 :           tree bias = NULL_TREE;
   10933         3776 :           if (!costing_p)
   10934              :             {
   10935          959 :               if (mask_node)
   10936          153 :                 vec_mask = vec_masks[i];
   10937          959 :               if (loop_masks)
   10938            0 :                 final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
   10939              :                                                  vec_num, vectype, i);
   10940          959 :               if (vec_mask)
   10941          153 :                 final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
   10942              :                                                final_mask, vec_mask, gsi);
   10943              : 
   10944          959 :               if (i > 0 && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
   10945            0 :                 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
   10946              :                                                gsi, stmt_info, bump);
   10947              :             }
   10948              : 
   10949              :           /* 2. Create the vector-load in the loop.  */
   10950         3776 :           unsigned align = get_object_alignment (DR_REF (first_dr_info->dr));
   10951         3776 :           tree alias_align_ptr = build_int_cst (ref_type, align);
   10952         3776 :           if (memory_access_type == VMAT_GATHER_SCATTER_IFN)
   10953              :             {
   10954            0 :               if (costing_p)
   10955              :                 {
   10956            0 :                   if (ls.supported_offset_vectype)
   10957            0 :                     inside_cost
   10958            0 :                       += record_stmt_cost (cost_vec, 1, vector_stmt,
   10959              :                                            slp_node, 0, vect_body);
   10960            0 :                   if (ls.supported_scale)
   10961            0 :                     inside_cost
   10962            0 :                       += record_stmt_cost (cost_vec, 1, vector_stmt,
   10963              :                                            slp_node, 0, vect_body);
   10964              : 
   10965            0 :                   unsigned int cnunits = vect_nunits_for_cost (vectype);
   10966            0 :                   inside_cost
   10967            0 :                     = record_stmt_cost (cost_vec, cnunits, scalar_load,
   10968              :                                         slp_node, 0, vect_body);
   10969         3776 :                   continue;
   10970            0 :                 }
   10971            0 :               if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
   10972            0 :                 vec_offset = vec_offsets[i];
   10973            0 :               tree zero = build_zero_cst (vectype);
   10974            0 :               tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
   10975            0 :               bool strided = !VECTOR_TYPE_P (TREE_TYPE (vec_offset));
   10976              : 
   10977              :               /* Perform the offset conversion and scaling if necessary.  */
   10978            0 :               if (!strided
   10979            0 :                   && (ls.supported_offset_vectype || ls.supported_scale))
   10980              :                 {
   10981            0 :                   gimple_seq stmts = NULL;
   10982            0 :                   if (ls.supported_offset_vectype)
   10983            0 :                     vec_offset = gimple_convert
   10984            0 :                       (&stmts, ls.supported_offset_vectype, vec_offset);
   10985            0 :                   if (ls.supported_scale)
   10986              :                     {
   10987              :                       /* Only scale the vec_offset if we haven't already.  */
   10988            0 :                       if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
   10989            0 :                           || i == 0)
   10990              :                         {
   10991            0 :                           tree mult_cst = build_int_cst
   10992            0 :                             (TREE_TYPE (TREE_TYPE (vec_offset)),
   10993            0 :                              SLP_TREE_GS_SCALE (slp_node) / ls.supported_scale);
   10994            0 :                           tree mult = build_vector_from_val
   10995            0 :                             (TREE_TYPE (vec_offset), mult_cst);
   10996            0 :                           vec_offset = gimple_build
   10997            0 :                             (&stmts, MULT_EXPR, TREE_TYPE (vec_offset),
   10998              :                              vec_offset, mult);
   10999              :                         }
   11000            0 :                       scale = size_int (ls.supported_scale);
   11001              :                     }
   11002            0 :                   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
   11003              :                 }
   11004              : 
   11005            0 :               if (ls.gs.ifn == IFN_MASK_LEN_GATHER_LOAD)
   11006              :                 {
   11007            0 :                   if (loop_lens)
   11008            0 :                     final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
   11009              :                                                    vec_num, vectype, i, 1, true);
   11010              :                   else
   11011            0 :                     final_len = build_int_cst (sizetype,
   11012            0 :                                                TYPE_VECTOR_SUBPARTS (vectype));
   11013            0 :                   signed char biasval
   11014            0 :                     = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
   11015            0 :                   bias = build_int_cst (intQI_type_node, biasval);
   11016            0 :                   if (!final_mask)
   11017              :                     {
   11018            0 :                       mask_vectype = truth_type_for (vectype);
   11019            0 :                       final_mask = build_minus_one_cst (mask_vectype);
   11020              :                     }
   11021              :                 }
   11022              : 
   11023            0 :               if (final_mask)
   11024              :                 {
   11025            0 :                   vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
   11026            0 :                   if (type_mode_padding_p
   11027            0 :                       && maskload_elsval != MASK_LOAD_ELSE_ZERO)
   11028            0 :                     need_zeroing = true;
   11029              :                 }
   11030              : 
   11031            0 :               gcall *call;
   11032            0 :               if (final_len && final_mask)
   11033              :                 {
   11034            0 :                   if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
   11035            0 :                     call = gimple_build_call_internal (IFN_MASK_LEN_GATHER_LOAD,
   11036              :                                                        9, dataref_ptr,
   11037              :                                                        alias_align_ptr,
   11038              :                                                        vec_offset, scale, zero,
   11039              :                                                        final_mask, vec_els,
   11040              :                                                        final_len, bias);
   11041              :                   else
   11042              :                     /* Non-vector offset indicates that prefer to take
   11043              :                        MASK_LEN_STRIDED_LOAD instead of the
   11044              :                        MASK_LEN_GATHER_LOAD with direct stride arg.  */
   11045            0 :                     call = gimple_build_call_internal
   11046            0 :                              (IFN_MASK_LEN_STRIDED_LOAD, 7, dataref_ptr,
   11047              :                               vec_offset, zero, final_mask, vec_els, final_len,
   11048              :                               bias);
   11049              :                 }
   11050            0 :               else if (final_mask)
   11051            0 :                 call = gimple_build_call_internal (IFN_MASK_GATHER_LOAD,
   11052              :                                                    7, dataref_ptr,
   11053              :                                                    alias_align_ptr,
   11054              :                                                    vec_offset, scale,
   11055              :                                                    zero, final_mask, vec_els);
   11056              :               else
   11057            0 :                 call = gimple_build_call_internal (IFN_GATHER_LOAD, 5,
   11058              :                                                    dataref_ptr,
   11059              :                                                    alias_align_ptr,
   11060              :                                                    vec_offset, scale, zero);
   11061            0 :               gimple_call_set_nothrow (call, true);
   11062            0 :               new_stmt = call;
   11063            0 :               data_ref = NULL_TREE;
   11064              :             }
   11065         3776 :           else if (memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
   11066              :             {
   11067              :               /* The builtin decls path for gather is legacy, x86 only.  */
   11068          849 :               gcc_assert (!final_len && nunits.is_constant ());
   11069          849 :               if (costing_p)
   11070              :                 {
   11071          566 :                   unsigned int cnunits = vect_nunits_for_cost (vectype);
   11072          566 :                   inside_cost
   11073          566 :                     = record_stmt_cost (cost_vec, cnunits, scalar_load,
   11074              :                                         slp_node, 0, vect_body);
   11075          566 :                   continue;
   11076          566 :                 }
   11077          283 :               tree offset_vectype = TREE_TYPE (vec_offsets[0]);
   11078          283 :               poly_uint64 offset_nunits = TYPE_VECTOR_SUBPARTS (offset_vectype);
   11079          283 :               if (known_eq (nunits, offset_nunits))
   11080              :                 {
   11081          134 :                   new_stmt = vect_build_one_gather_load_call
   11082          134 :                                (vinfo, stmt_info, slp_node, vectype, gsi,
   11083          134 :                                 ls.gs.decl, dataref_ptr, vec_offsets[i],
   11084              :                                 final_mask);
   11085          134 :                   data_ref = NULL_TREE;
   11086              :                 }
   11087          149 :               else if (known_eq (nunits, offset_nunits * 2))
   11088              :                 {
   11089              :                   /* We have a offset vector with half the number of
   11090              :                      lanes but the builtins will produce full vectype
   11091              :                      data with just the lower lanes filled.  */
   11092           63 :                   new_stmt = vect_build_one_gather_load_call
   11093          126 :                                (vinfo, stmt_info, slp_node, vectype, gsi,
   11094           63 :                                 ls.gs.decl, dataref_ptr, vec_offsets[2 * i],
   11095              :                                 final_mask);
   11096           63 :                   tree low = make_ssa_name (vectype);
   11097           63 :                   gimple_set_lhs (new_stmt, low);
   11098           63 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11099              : 
   11100              :                   /* now put upper half of final_mask in final_mask low. */
   11101           63 :                   if (final_mask
   11102           63 :                       && !SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (final_mask))))
   11103              :                     {
   11104           11 :                       int count = nunits.to_constant ();
   11105           11 :                       vec_perm_builder sel (count, count, 1);
   11106           11 :                       sel.quick_grow (count);
   11107           87 :                       for (int i = 0; i < count; ++i)
   11108           76 :                         sel[i] = i | (count / 2);
   11109           11 :                       vec_perm_indices indices (sel, 2, count);
   11110           11 :                       tree perm_mask = vect_gen_perm_mask_checked
   11111           11 :                                          (TREE_TYPE (final_mask), indices);
   11112           11 :                       new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
   11113              :                                                       final_mask, final_mask,
   11114              :                                                       perm_mask);
   11115           11 :                       final_mask = make_ssa_name (TREE_TYPE (final_mask));
   11116           11 :                       gimple_set_lhs (new_stmt, final_mask);
   11117           11 :                       vect_finish_stmt_generation (vinfo, stmt_info,
   11118              :                                                    new_stmt, gsi);
   11119           11 :                     }
   11120           52 :                   else if (final_mask)
   11121              :                     {
   11122           24 :                       new_stmt = gimple_build_assign (NULL_TREE,
   11123              :                                                       VEC_UNPACK_HI_EXPR,
   11124              :                                                       final_mask);
   11125           24 :                       final_mask = make_ssa_name
   11126           24 :                                     (truth_type_for (offset_vectype));
   11127           24 :                       gimple_set_lhs (new_stmt, final_mask);
   11128           24 :                       vect_finish_stmt_generation (vinfo, stmt_info,
   11129              :                                                    new_stmt, gsi);
   11130              :                     }
   11131              : 
   11132           63 :                   new_stmt = vect_build_one_gather_load_call
   11133          126 :                                (vinfo, stmt_info, slp_node, vectype, gsi,
   11134              :                                 ls.gs.decl, dataref_ptr,
   11135           63 :                                 vec_offsets[2 * i + 1], final_mask);
   11136           63 :                   tree high = make_ssa_name (vectype);
   11137           63 :                   gimple_set_lhs (new_stmt, high);
   11138           63 :                   vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11139              : 
   11140              :                   /* compose low + high.  */
   11141           63 :                   int count = nunits.to_constant ();
   11142           63 :                   vec_perm_builder sel (count, count, 1);
   11143           63 :                   sel.quick_grow (count);
   11144          647 :                   for (int i = 0; i < count; ++i)
   11145          584 :                     sel[i] = i < count / 2 ? i : i + count / 2;
   11146           63 :                   vec_perm_indices indices (sel, 2, count);
   11147           63 :                   tree perm_mask
   11148           63 :                     = vect_gen_perm_mask_checked (vectype, indices);
   11149           63 :                   new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
   11150              :                                                   low, high, perm_mask);
   11151           63 :                   data_ref = NULL_TREE;
   11152           63 :                 }
   11153           86 :               else if (known_eq (nunits * 2, offset_nunits))
   11154              :                 {
   11155              :                   /* We have a offset vector with double the number of
   11156              :                      lanes.  Select the low/high part accordingly.  */
   11157           86 :                   vec_offset = vec_offsets[i / 2];
   11158           86 :                   if (i & 1)
   11159              :                     {
   11160           43 :                       int count = offset_nunits.to_constant ();
   11161           43 :                       vec_perm_builder sel (count, count, 1);
   11162           43 :                       sel.quick_grow (count);
   11163          463 :                       for (int i = 0; i < count; ++i)
   11164          420 :                         sel[i] = i | (count / 2);
   11165           43 :                       vec_perm_indices indices (sel, 2, count);
   11166           43 :                       tree perm_mask = vect_gen_perm_mask_checked
   11167           43 :                                          (TREE_TYPE (vec_offset), indices);
   11168           43 :                       new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
   11169              :                                                       vec_offset, vec_offset,
   11170              :                                                       perm_mask);
   11171           43 :                       vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
   11172           43 :                       gimple_set_lhs (new_stmt, vec_offset);
   11173           43 :                       vect_finish_stmt_generation (vinfo, stmt_info,
   11174              :                                                    new_stmt, gsi);
   11175           43 :                     }
   11176           86 :                   new_stmt = vect_build_one_gather_load_call
   11177           86 :                                (vinfo, stmt_info, slp_node, vectype, gsi,
   11178              :                                 ls.gs.decl,
   11179              :                                 dataref_ptr, vec_offset, final_mask);
   11180           86 :                   data_ref = NULL_TREE;
   11181              :                 }
   11182              :               else
   11183            0 :                 gcc_unreachable ();
   11184              :             }
   11185              :           else
   11186              :             {
   11187              :               /* Emulated gather-scatter.  */
   11188         2927 :               gcc_assert (!final_mask);
   11189         2927 :               unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
   11190         2927 :               if (costing_p)
   11191              :                 {
   11192              :                   /* For emulated gathers N offset vector element
   11193              :                      offset add is consumed by the load).  */
   11194         2251 :                   inside_cost = record_stmt_cost (cost_vec, const_nunits,
   11195              :                                                   vec_to_scalar,
   11196              :                                                   slp_node, 0, vect_body);
   11197              :                   /* N scalar loads plus gathering them into a
   11198              :                      vector.  */
   11199         2251 :                   inside_cost
   11200         2251 :                     = record_stmt_cost (cost_vec, const_nunits, scalar_load,
   11201              :                                         slp_node, 0, vect_body);
   11202         2251 :                   inside_cost
   11203         2251 :                     = record_stmt_cost (cost_vec, 1, vec_construct,
   11204              :                                         slp_node, 0, vect_body);
   11205         2251 :                   continue;
   11206              :                 }
   11207          676 :               tree offset_vectype = TREE_TYPE (vec_offsets[0]);
   11208          676 :               unsigned HOST_WIDE_INT const_offset_nunits
   11209          676 :                 = TYPE_VECTOR_SUBPARTS (offset_vectype).to_constant ();
   11210          676 :               vec<constructor_elt, va_gc> *ctor_elts;
   11211          676 :               vec_alloc (ctor_elts, const_nunits);
   11212          676 :               gimple_seq stmts = NULL;
   11213              :               /* We support offset vectors with more elements
   11214              :                  than the data vector for now.  */
   11215          676 :               unsigned HOST_WIDE_INT factor
   11216              :                 = const_offset_nunits / const_nunits;
   11217          676 :               vec_offset = vec_offsets[i / factor];
   11218          676 :               unsigned elt_offset = (i % factor) * const_nunits;
   11219          676 :               tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
   11220          676 :               tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
   11221          676 :               tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
   11222         2762 :               for (unsigned k = 0; k < const_nunits; ++k)
   11223              :                 {
   11224         2086 :                   tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
   11225              :                                           bitsize_int (k + elt_offset));
   11226         6258 :                   tree idx = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
   11227         2086 :                                            vec_offset, TYPE_SIZE (idx_type),
   11228              :                                            boff);
   11229         2086 :                   idx = gimple_convert (&stmts, sizetype, idx);
   11230         2086 :                   idx = gimple_build (&stmts, MULT_EXPR, sizetype, idx, scale);
   11231         2086 :                   tree ptr = gimple_build (&stmts, PLUS_EXPR,
   11232         2086 :                                            TREE_TYPE (dataref_ptr),
   11233              :                                            dataref_ptr, idx);
   11234         2086 :                   ptr = gimple_convert (&stmts, ptr_type_node, ptr);
   11235         2086 :                   tree elt = make_ssa_name (TREE_TYPE (vectype));
   11236         2086 :                   tree ref = build2 (MEM_REF, ltype, ptr,
   11237              :                                      build_int_cst (ref_type, 0));
   11238         2086 :                   new_stmt = gimple_build_assign (elt, ref);
   11239         4172 :                   gimple_set_vuse (new_stmt, gimple_vuse (gsi_stmt (*gsi)));
   11240         2086 :                   gimple_seq_add_stmt (&stmts, new_stmt);
   11241         2086 :                   CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE, elt);
   11242              :                 }
   11243          676 :               gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
   11244          676 :               new_stmt = gimple_build_assign (NULL_TREE,
   11245              :                                               build_constructor (vectype,
   11246              :                                                                  ctor_elts));
   11247          676 :               data_ref = NULL_TREE;
   11248              :             }
   11249              : 
   11250          959 :           vec_dest = vect_create_destination_var (scalar_dest, vectype);
   11251              :           /* DATA_REF is null if we've already built the statement.  */
   11252          959 :           if (data_ref)
   11253              :             {
   11254              :               vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
   11255              :               new_stmt = gimple_build_assign (vec_dest, data_ref);
   11256              :             }
   11257         1918 :           new_temp = (need_zeroing
   11258          959 :                       ? make_ssa_name (vectype)
   11259          959 :                       : make_ssa_name (vec_dest, new_stmt));
   11260          959 :           gimple_set_lhs (new_stmt, new_temp);
   11261          959 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11262              : 
   11263              :           /* If we need to explicitly zero inactive elements emit a
   11264              :              VEC_COND_EXPR that does so.  */
   11265          959 :           if (need_zeroing)
   11266              :             {
   11267            0 :               vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
   11268              :                                                  vectype);
   11269              : 
   11270            0 :               tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
   11271            0 :               new_stmt = gimple_build_assign (new_temp2, VEC_COND_EXPR,
   11272              :                                               final_mask, new_temp, vec_els);
   11273            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11274            0 :               new_temp = new_temp2;
   11275              :             }
   11276              : 
   11277          959 :           if (ls.ls_type)
   11278              :             {
   11279            0 :               new_stmt = gimple_build_assign (make_ssa_name
   11280              :                                               (original_vectype),
   11281              :                                               VIEW_CONVERT_EXPR,
   11282              :                                               build1 (VIEW_CONVERT_EXPR,
   11283              :                                                       original_vectype,
   11284              :                                                       new_temp));
   11285            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11286              :             }
   11287              : 
   11288              :           /* Store vector loads in the corresponding SLP_NODE.  */
   11289          959 :           if (!costing_p)
   11290              :             {
   11291          959 :               if (ls.slp_perm)
   11292            0 :                 dr_chain.quick_push (gimple_assign_lhs (new_stmt));
   11293              :               else
   11294          959 :                 slp_node->push_vec_def (new_stmt);
   11295              :             }
   11296              :         }
   11297              : 
   11298         3001 :       if (ls.slp_perm)
   11299              :         {
   11300            0 :           if (costing_p)
   11301              :             {
   11302            0 :               gcc_assert (ls.n_perms != -1U);
   11303            0 :               inside_cost += record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
   11304              :                                                slp_node, 0, vect_body);
   11305              :             }
   11306              :           else
   11307              :             {
   11308            0 :               unsigned n_perms2;
   11309            0 :               vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, vf,
   11310              :                                             false, &n_perms2);
   11311            0 :               gcc_assert (ls.n_perms == n_perms2);
   11312              :             }
   11313              :         }
   11314              : 
   11315         3001 :       if (costing_p)
   11316              :         {
   11317         2254 :           if (dump_enabled_p ())
   11318          317 :             dump_printf_loc (MSG_NOTE, vect_location,
   11319              :                              "vect_model_load_cost: inside_cost = %u, "
   11320              :                              "prologue_cost = %u .\n",
   11321              :                              inside_cost, prologue_cost);
   11322         2254 :           SLP_TREE_TYPE (slp_node) = load_vec_info_type;
   11323         2254 :           slp_node->data = new vect_load_store_data (std::move (ls));
   11324              :         }
   11325         3001 :       return true;
   11326         3001 :     }
   11327              : 
   11328       574418 :   aggr_type = vectype;
   11329       574418 :   if (!costing_p)
   11330       161435 :     bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
   11331              :                                         memory_access_type, loop_lens);
   11332              : 
   11333       574418 :   poly_uint64 group_elt = 0;
   11334       574418 :   unsigned int inside_cost = 0, prologue_cost = 0;
   11335              :   /* For costing some adjacent vector loads, we'd like to cost with
   11336              :      the total number of them once instead of cost each one by one. */
   11337       574418 :   unsigned int n_adjacent_loads = 0;
   11338              : 
   11339              :   /* 1. Create the vector or array pointer update chain.  */
   11340       574418 :   if (!costing_p)
   11341              :     {
   11342       161435 :       bool simd_lane_access_p
   11343       161435 :           = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
   11344       161435 :       if (simd_lane_access_p
   11345         1629 :           && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
   11346         1629 :           && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
   11347         1629 :           && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
   11348         1629 :           && integer_zerop (DR_INIT (first_dr_info->dr))
   11349         1629 :           && alias_sets_conflict_p (get_alias_set (aggr_type),
   11350         1629 :                                     get_alias_set (TREE_TYPE (ref_type)))
   11351       161435 :           && (alignment_support_scheme == dr_aligned
   11352         1629 :               || alignment_support_scheme == dr_unaligned_supported))
   11353              :         {
   11354         1629 :           dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
   11355         1629 :           dataref_offset = build_int_cst (ref_type, 0);
   11356              :         }
   11357       159806 :       else if (diff_first_stmt_info)
   11358              :         {
   11359         3578 :           dataref_ptr
   11360         3578 :             = vect_create_data_ref_ptr (vinfo, first_stmt_info_for_drptr,
   11361              :                                         aggr_type, at_loop, offset, &dummy,
   11362              :                                         gsi, &ptr_incr, simd_lane_access_p,
   11363              :                                         bump);
   11364              :           /* Adjust the pointer by the difference to first_stmt.  */
   11365         3578 :           data_reference_p ptrdr
   11366              :             = STMT_VINFO_DATA_REF (first_stmt_info_for_drptr);
   11367         3578 :           tree diff = fold_convert (sizetype,
   11368              :                                     size_binop (MINUS_EXPR,
   11369              :                                                 DR_INIT (first_dr_info->dr),
   11370              :                                                 DR_INIT (ptrdr)));
   11371         3578 :           dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
   11372              :                                          stmt_info, diff);
   11373         3578 :           if (alignment_support_scheme == dr_explicit_realign)
   11374              :             {
   11375            0 :               msq = vect_setup_realignment (vinfo, first_stmt_info_for_drptr,
   11376              :                                             vectype, gsi,
   11377              :                                             &realignment_token,
   11378              :                                             alignment_support_scheme,
   11379              :                                             dataref_ptr, &at_loop);
   11380            0 :               gcc_assert (!compute_in_loop);
   11381              :             }
   11382              :         }
   11383              :       else
   11384       156228 :         dataref_ptr
   11385       156228 :           = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
   11386              :                                       at_loop,
   11387              :                                       offset, &dummy, gsi, &ptr_incr,
   11388              :                                       simd_lane_access_p, bump);
   11389              :     }
   11390              :   else if (!costing_p)
   11391              :     {
   11392              :       gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
   11393              :       if (dataref_offset)
   11394              :         dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
   11395              :       else
   11396              :         dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
   11397              :                                        stmt_info, bump);
   11398              :     }
   11399              : 
   11400       574418 :   auto_vec<tree> dr_chain;
   11401       574418 :   if (grouped_load || ls.slp_perm)
   11402        54310 :     dr_chain.create (vec_num);
   11403              : 
   11404              :   gimple *new_stmt = NULL;
   11405      1496537 :   for (i = 0; i < vec_num; i++)
   11406              :     {
   11407       922119 :       tree final_mask = NULL_TREE;
   11408       922119 :       tree final_len = NULL_TREE;
   11409       922119 :       tree bias = NULL_TREE;
   11410              : 
   11411       922119 :       if (!costing_p)
   11412              :         {
   11413       252174 :           if (mask_node)
   11414          709 :             vec_mask = vec_masks[i];
   11415       252174 :           if (loop_masks)
   11416           48 :             final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
   11417              :                                              vec_num, vectype, i);
   11418       252174 :           if (vec_mask)
   11419          709 :             final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
   11420              :                                            final_mask, vec_mask, gsi);
   11421              : 
   11422       252174 :           if (i > 0)
   11423        90739 :             dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
   11424              :                                            gsi, stmt_info, bump);
   11425              :         }
   11426              : 
   11427              :       /* 2. Create the vector-load in the loop.  */
   11428       922119 :       switch (alignment_support_scheme)
   11429              :         {
   11430       922119 :         case dr_aligned:
   11431       922119 :         case dr_unaligned_supported:
   11432       922119 :           {
   11433       922119 :             if (costing_p)
   11434              :               break;
   11435              : 
   11436       252174 :             unsigned int misalign;
   11437       252174 :             unsigned HOST_WIDE_INT align;
   11438       252174 :             align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
   11439       252174 :             if (alignment_support_scheme == dr_aligned)
   11440              :               misalign = 0;
   11441       161970 :             else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
   11442              :               {
   11443       123240 :                 align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
   11444       123240 :                 misalign = 0;
   11445              :               }
   11446              :             else
   11447        38730 :               misalign = misalignment;
   11448       252174 :             if (dataref_offset == NULL_TREE
   11449       250047 :                 && TREE_CODE (dataref_ptr) == SSA_NAME)
   11450       169437 :               set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
   11451              :                                       misalign);
   11452       252174 :             align = least_bit_hwi (misalign | align);
   11453              : 
   11454              :             /* Compute IFN when LOOP_LENS or final_mask valid.  */
   11455       252174 :             machine_mode vmode = TYPE_MODE (vectype);
   11456       252174 :             machine_mode new_vmode = vmode;
   11457       252174 :             internal_fn partial_ifn = IFN_LAST;
   11458       252174 :             if (loop_lens)
   11459              :               {
   11460            0 :                 opt_machine_mode new_ovmode
   11461            0 :                   = get_len_load_store_mode (vmode, true, &partial_ifn);
   11462            0 :                 new_vmode = new_ovmode.require ();
   11463            0 :                 unsigned factor
   11464            0 :                   = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
   11465            0 :                 final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
   11466              :                                                vec_num, vectype, i, factor, true);
   11467              :               }
   11468       252174 :             else if (final_mask)
   11469              :               {
   11470          737 :                 if (!can_vec_mask_load_store_p (vmode,
   11471          737 :                                                 TYPE_MODE
   11472              :                                                   (TREE_TYPE (final_mask)),
   11473              :                                                 true, &partial_ifn))
   11474            0 :                   gcc_unreachable ();
   11475              :               }
   11476              : 
   11477       252174 :             if (partial_ifn == IFN_MASK_LEN_LOAD)
   11478              :               {
   11479            0 :                 if (!final_len)
   11480              :                   {
   11481              :                     /* Pass VF value to 'len' argument of
   11482              :                        MASK_LEN_LOAD if LOOP_LENS is invalid.  */
   11483            0 :                     final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
   11484              :                   }
   11485            0 :                 if (!final_mask)
   11486              :                   {
   11487              :                     /* Pass all ones value to 'mask' argument of
   11488              :                        MASK_LEN_LOAD if final_mask is invalid.  */
   11489            0 :                     mask_vectype = truth_type_for (vectype);
   11490            0 :                     final_mask = build_minus_one_cst (mask_vectype);
   11491              :                   }
   11492              :               }
   11493       252174 :             if (final_len)
   11494              :               {
   11495            0 :                 signed char biasval
   11496            0 :                   = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
   11497            0 :                 bias = build_int_cst (intQI_type_node, biasval);
   11498              :               }
   11499              : 
   11500       252174 :             tree vec_els;
   11501              : 
   11502       252174 :             if (final_len)
   11503              :               {
   11504            0 :                 tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
   11505            0 :                 gcall *call;
   11506              : 
   11507              :                 /* Need conversion if the vectype is punned by VnQI.  */
   11508            0 :                 els_vectype = vectype;
   11509            0 :                 if (vmode != new_vmode)
   11510            0 :                   els_vectype
   11511            0 :                     = build_vector_type_for_mode (unsigned_intQI_type_node,
   11512              :                                                   new_vmode);
   11513            0 :                 vec_els = vect_get_mask_load_else (maskload_elsval,
   11514              :                                                    els_vectype);
   11515              : 
   11516            0 :                 if (partial_ifn == IFN_MASK_LEN_LOAD)
   11517              :                   {
   11518            0 :                     if (type_mode_padding_p
   11519            0 :                         && maskload_elsval != MASK_LOAD_ELSE_ZERO)
   11520            0 :                       need_zeroing = true;
   11521            0 :                     call = gimple_build_call_internal (IFN_MASK_LEN_LOAD,
   11522              :                                                        6, dataref_ptr, ptr,
   11523              :                                                        final_mask, vec_els,
   11524              :                                                        final_len, bias);
   11525              :                   }
   11526              :                 else
   11527            0 :                   call = gimple_build_call_internal (IFN_LEN_LOAD, 5,
   11528              :                                                      dataref_ptr, ptr,
   11529              :                                                      vec_els, final_len,
   11530              :                                                      bias);
   11531            0 :                 gimple_call_set_nothrow (call, true);
   11532            0 :                 new_stmt = call;
   11533            0 :                 data_ref = NULL_TREE;
   11534              : 
   11535              :                 /* Need conversion if it's wrapped with VnQI.  */
   11536            0 :                 if (vmode != new_vmode)
   11537              :                   {
   11538            0 :                     tree new_vtype
   11539            0 :                       = build_vector_type_for_mode (unsigned_intQI_type_node,
   11540              :                                                     new_vmode);
   11541            0 :                     tree var = vect_get_new_ssa_name (new_vtype,
   11542              :                                                       vect_simple_var);
   11543            0 :                     gimple_set_lhs (call, var);
   11544            0 :                     vect_finish_stmt_generation (vinfo, stmt_info, call,
   11545              :                                                  gsi);
   11546            0 :                     tree op = build1 (VIEW_CONVERT_EXPR, vectype, var);
   11547            0 :                     new_stmt = gimple_build_assign (vec_dest,
   11548              :                                                     VIEW_CONVERT_EXPR, op);
   11549              :                   }
   11550              :               }
   11551       252174 :             else if (final_mask)
   11552              :               {
   11553          737 :                 tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
   11554          737 :                 vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
   11555          737 :                 if (type_mode_padding_p
   11556          737 :                     && maskload_elsval != MASK_LOAD_ELSE_ZERO)
   11557            0 :                   need_zeroing = true;
   11558          737 :                 gcall *call = gimple_build_call_internal (IFN_MASK_LOAD, 4,
   11559              :                                                           dataref_ptr, ptr,
   11560              :                                                           final_mask,
   11561              :                                                           vec_els);
   11562          737 :                 gimple_call_set_nothrow (call, true);
   11563          737 :                 new_stmt = call;
   11564          737 :                 data_ref = NULL_TREE;
   11565              :               }
   11566              :             else
   11567              :               {
   11568       251437 :                 tree ltype = vectype;
   11569       251437 :                 tree new_vtype = NULL_TREE;
   11570       251437 :                 unsigned HOST_WIDE_INT gap = DR_GROUP_GAP (first_stmt_info);
   11571       251437 :                 unsigned HOST_WIDE_INT dr_size
   11572       251437 :                   = vect_get_scalar_dr_size (first_dr_info);
   11573       251437 :                 poly_int64 off = 0;
   11574       251437 :                 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
   11575         1444 :                   off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
   11576       251437 :                 unsigned int vect_align
   11577       251437 :                   = vect_known_alignment_in_bytes (first_dr_info, vectype,
   11578       251437 :                                                    off);
   11579              :                 /* Try to use a single smaller load when we are about
   11580              :                    to load excess elements compared to the unrolled
   11581              :                    scalar loop.  */
   11582       251437 :                 if (known_gt ((i + 1) * nunits,
   11583              :                               (group_size * vf - gap)))
   11584              :                   {
   11585         6860 :                     poly_uint64 remain = ((group_size * vf - gap) - i * nunits);
   11586         6860 :                     if (known_ge ((i + 1) * nunits - (group_size * vf - gap),
   11587              :                                   nunits))
   11588              :                       /* DR will be unused.  */
   11589              :                       ltype = NULL_TREE;
   11590         2201 :                     else if (known_ge (vect_align,
   11591              :                                        tree_to_poly_uint64
   11592              :                                          (TYPE_SIZE_UNIT (vectype))))
   11593              :                       /* Aligned access to excess elements is OK if
   11594              :                          at least one element is accessed in the
   11595              :                          scalar loop.  */
   11596              :                       ;
   11597         1854 :                     else if (known_gt (vect_align,
   11598              :                                        ((nunits - remain) * dr_size)))
   11599              :                       /* Aligned access to the gap area when there's
   11600              :                          at least one element in it is OK.  */
   11601              :                       ;
   11602              :                     else
   11603              :                       {
   11604              :                         /* remain should now be > 0 and < nunits.  */
   11605         1851 :                         unsigned num;
   11606         1851 :                         if (known_ne (remain, 0u)
   11607         1851 :                             && constant_multiple_p (nunits, remain, &num))
   11608              :                           {
   11609         1393 :                             tree ptype;
   11610         1393 :                             new_vtype
   11611         1393 :                               = vector_vector_composition_type (vectype, num,
   11612              :                                                                 &ptype);
   11613         1393 :                             if (new_vtype)
   11614         1393 :                               ltype = ptype;
   11615              :                           }
   11616              :                         /* Else use multiple loads or a masked load?  */
   11617              :                         /* For loop vectorization we now should have
   11618              :                            an alternate type or LOOP_VINFO_PEELING_FOR_GAPS
   11619              :                            set.  */
   11620         1851 :                         if (loop_vinfo)
   11621         1600 :                           gcc_assert (new_vtype
   11622              :                                       || LOOP_VINFO_PEELING_FOR_GAPS
   11623              :                                            (loop_vinfo));
   11624              :                         /* But still reduce the access size to the next
   11625              :                            required power-of-two so peeling a single
   11626              :                            scalar iteration is sufficient.  */
   11627         1851 :                         unsigned HOST_WIDE_INT cremain;
   11628         1851 :                         if (remain.is_constant (&cremain))
   11629              :                           {
   11630         1851 :                             unsigned HOST_WIDE_INT cpart_size
   11631         1851 :                               = 1 << ceil_log2 (cremain);
   11632         1851 :                             if (known_gt (nunits, cpart_size)
   11633         1851 :                                 && constant_multiple_p (nunits, cpart_size,
   11634              :                                                         &num))
   11635              :                               {
   11636         1405 :                                 tree ptype;
   11637         1405 :                                 new_vtype
   11638         2810 :                                   = vector_vector_composition_type (vectype,
   11639         1405 :                                                                     num,
   11640              :                                                                     &ptype);
   11641         1405 :                                 if (new_vtype)
   11642         1405 :                                   ltype = ptype;
   11643              :                               }
   11644              :                           }
   11645              :                       }
   11646              :                   }
   11647       251437 :                 tree offset = (dataref_offset ? dataref_offset
   11648       249310 :                                : build_int_cst (ref_type, 0));
   11649       251437 :                 if (!ltype)
   11650              :                   ;
   11651       246778 :                 else if (ltype != vectype
   11652       246778 :                          && memory_access_type == VMAT_CONTIGUOUS_REVERSE)
   11653              :                   {
   11654           25 :                     poly_uint64 gap_offset
   11655           25 :                       = (tree_to_poly_uint64 (TYPE_SIZE_UNIT (vectype))
   11656           25 :                          - tree_to_poly_uint64 (TYPE_SIZE_UNIT (ltype)));
   11657           25 :                     tree gapcst = build_int_cstu (ref_type, gap_offset);
   11658           25 :                     offset = size_binop (PLUS_EXPR, offset, gapcst);
   11659              :                   }
   11660       251437 :                 if (ltype)
   11661              :                   {
   11662       246778 :                     data_ref = fold_build2 (MEM_REF, ltype,
   11663              :                                             dataref_ptr, offset);
   11664       246778 :                     if (alignment_support_scheme == dr_aligned
   11665       246778 :                         && align >= TYPE_ALIGN_UNIT (ltype))
   11666              :                       ;
   11667              :                     else
   11668       160264 :                       TREE_TYPE (data_ref)
   11669       320528 :                         = build_aligned_type (TREE_TYPE (data_ref),
   11670              :                                               align * BITS_PER_UNIT);
   11671              :                   }
   11672       251437 :                 if (!ltype)
   11673         4659 :                   data_ref = build_constructor (vectype, NULL);
   11674       246778 :                 else if (ltype != vectype)
   11675              :                   {
   11676         1405 :                     vect_copy_ref_info (data_ref,
   11677         1405 :                                         DR_REF (first_dr_info->dr));
   11678         1405 :                     tree tem = make_ssa_name (ltype);
   11679         1405 :                     new_stmt = gimple_build_assign (tem, data_ref);
   11680         1405 :                     vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
   11681              :                                                  gsi);
   11682         1405 :                     data_ref = NULL;
   11683         1405 :                     vec<constructor_elt, va_gc> *v;
   11684              :                     /* We've computed 'num' above to statically two
   11685              :                        or via constant_multiple_p.  */
   11686         1405 :                     unsigned num
   11687         1405 :                       = (exact_div (tree_to_poly_uint64
   11688         1405 :                                       (TYPE_SIZE_UNIT (vectype)),
   11689              :                                     tree_to_poly_uint64
   11690         1405 :                                       (TYPE_SIZE_UNIT (ltype)))
   11691         1405 :                          .to_constant ());
   11692         1405 :                     vec_alloc (v, num);
   11693         1405 :                     if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
   11694              :                       {
   11695           62 :                         while (--num)
   11696           62 :                           CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
   11697              :                                                   build_zero_cst (ltype));
   11698           25 :                         CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
   11699              :                       }
   11700              :                     else
   11701              :                       {
   11702         1380 :                         CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
   11703         1380 :                         while (--num)
   11704         3114 :                           CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
   11705              :                                                   build_zero_cst (ltype));
   11706              :                       }
   11707         1405 :                     gcc_assert (new_vtype != NULL_TREE);
   11708         1405 :                     if (new_vtype == vectype)
   11709         1373 :                       new_stmt
   11710         1373 :                         = gimple_build_assign (vec_dest,
   11711              :                                                build_constructor (vectype, v));
   11712              :                     else
   11713              :                       {
   11714           32 :                         tree new_vname = make_ssa_name (new_vtype);
   11715           32 :                         new_stmt
   11716           32 :                           = gimple_build_assign (new_vname,
   11717              :                                                  build_constructor (new_vtype,
   11718              :                                                                     v));
   11719           32 :                         vect_finish_stmt_generation (vinfo, stmt_info,
   11720              :                                                      new_stmt, gsi);
   11721           32 :                         new_stmt
   11722           32 :                           = gimple_build_assign (vec_dest,
   11723              :                                                  build1 (VIEW_CONVERT_EXPR,
   11724              :                                                          vectype, new_vname));
   11725              :                       }
   11726              :                   }
   11727              :               }
   11728              :             break;
   11729              :           }
   11730            0 :         case dr_explicit_realign:
   11731            0 :           {
   11732            0 :             if (costing_p)
   11733              :               break;
   11734            0 :             tree ptr, bump;
   11735              : 
   11736            0 :             tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
   11737              : 
   11738            0 :             if (compute_in_loop)
   11739            0 :               msq = vect_setup_realignment (vinfo, first_stmt_info, vectype,
   11740              :                                             gsi, &realignment_token,
   11741              :                                             dr_explicit_realign,
   11742              :                                             dataref_ptr, NULL);
   11743              : 
   11744            0 :             if (TREE_CODE (dataref_ptr) == SSA_NAME)
   11745            0 :               ptr = copy_ssa_name (dataref_ptr);
   11746              :             else
   11747            0 :               ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
   11748              :             // For explicit realign the target alignment should be
   11749              :             // known at compile time.
   11750            0 :             unsigned HOST_WIDE_INT align
   11751            0 :               = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
   11752            0 :             new_stmt = gimple_build_assign (ptr, BIT_AND_EXPR, dataref_ptr,
   11753              :                                             build_int_cst
   11754            0 :                                               (TREE_TYPE (dataref_ptr),
   11755            0 :                                                -(HOST_WIDE_INT) align));
   11756            0 :             vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11757            0 :             data_ref = build2 (MEM_REF, vectype,
   11758              :                                ptr, build_int_cst (ref_type, 0));
   11759            0 :             vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
   11760            0 :             vec_dest = vect_create_destination_var (scalar_dest, vectype);
   11761            0 :             new_stmt = gimple_build_assign (vec_dest, data_ref);
   11762            0 :             new_temp = make_ssa_name (vec_dest, new_stmt);
   11763            0 :             gimple_assign_set_lhs (new_stmt, new_temp);
   11764            0 :             gimple_move_vops (new_stmt, stmt_info->stmt);
   11765            0 :             vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11766            0 :             msq = new_temp;
   11767              : 
   11768            0 :             bump = size_binop (MULT_EXPR, vs, TYPE_SIZE_UNIT (elem_type));
   11769            0 :             bump = size_binop (MINUS_EXPR, bump, size_one_node);
   11770            0 :             ptr = bump_vector_ptr (vinfo, dataref_ptr, NULL, gsi, stmt_info,
   11771              :                                    bump);
   11772            0 :             new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR, ptr,
   11773            0 :                                             build_int_cst (TREE_TYPE (ptr),
   11774            0 :                                                            -(HOST_WIDE_INT) align));
   11775            0 :             if (TREE_CODE (ptr) == SSA_NAME)
   11776            0 :               ptr = copy_ssa_name (ptr, new_stmt);
   11777              :             else
   11778            0 :               ptr = make_ssa_name (TREE_TYPE (ptr), new_stmt);
   11779            0 :             gimple_assign_set_lhs (new_stmt, ptr);
   11780            0 :             vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11781            0 :             data_ref = build2 (MEM_REF, vectype,
   11782              :                                ptr, build_int_cst (ref_type, 0));
   11783            0 :             break;
   11784              :           }
   11785            0 :         case dr_explicit_realign_optimized:
   11786            0 :           {
   11787            0 :             if (costing_p)
   11788              :               break;
   11789            0 :             if (TREE_CODE (dataref_ptr) == SSA_NAME)
   11790            0 :               new_temp = copy_ssa_name (dataref_ptr);
   11791              :             else
   11792            0 :               new_temp = make_ssa_name (TREE_TYPE (dataref_ptr));
   11793              :             // We should only be doing this if we know the target
   11794              :             // alignment at compile time.
   11795            0 :             unsigned HOST_WIDE_INT align
   11796            0 :               = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
   11797            0 :             new_stmt = gimple_build_assign (new_temp, BIT_AND_EXPR, dataref_ptr,
   11798            0 :                                             build_int_cst (TREE_TYPE (dataref_ptr),
   11799            0 :                                                            -(HOST_WIDE_INT) align));
   11800            0 :             vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11801            0 :             data_ref = build2 (MEM_REF, vectype, new_temp,
   11802              :                                build_int_cst (ref_type, 0));
   11803            0 :             break;
   11804              :           }
   11805            0 :         default:
   11806            0 :         gcc_unreachable ();
   11807              :         }
   11808              : 
   11809              :       /* One common place to cost the above vect load for different
   11810              :          alignment support schemes.  */
   11811       922119 :       if (costing_p)
   11812              :         {
   11813              :           /* For the prologue cost for realign,
   11814              :              we only need to count it once for the whole group.  */
   11815       669945 :           bool first_stmt_info_p = first_stmt_info == stmt_info;
   11816       669945 :           bool add_realign_cost = first_stmt_info_p && i == 0;
   11817       669945 :           if (memory_access_type == VMAT_CONTIGUOUS
   11818       669945 :               || memory_access_type == VMAT_CONTIGUOUS_REVERSE)
   11819              :             {
   11820              :               /* Leave realign cases alone to keep them simple.  */
   11821       669945 :               if (alignment_support_scheme == dr_explicit_realign_optimized
   11822              :                   || alignment_support_scheme == dr_explicit_realign)
   11823            0 :                 vect_get_load_cost (vinfo, stmt_info, slp_node, 1,
   11824              :                                     alignment_support_scheme, misalignment,
   11825              :                                     add_realign_cost, &inside_cost,
   11826              :                                     &prologue_cost, cost_vec, cost_vec,
   11827              :                                     true);
   11828              :               else
   11829       669945 :                 n_adjacent_loads++;
   11830              :             }
   11831              :         }
   11832              :       else
   11833              :         {
   11834       252174 :           vec_dest = vect_create_destination_var (scalar_dest, vectype);
   11835              :           /* DATA_REF is null if we've already built the statement.  */
   11836       252174 :           if (data_ref)
   11837              :             {
   11838       250032 :               vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
   11839       250032 :               new_stmt = gimple_build_assign (vec_dest, data_ref);
   11840              :             }
   11841              : 
   11842       504348 :           new_temp = (need_zeroing
   11843       252174 :                       ? make_ssa_name (vectype)
   11844       252174 :                       : make_ssa_name (vec_dest, new_stmt));
   11845       252174 :           gimple_set_lhs (new_stmt, new_temp);
   11846       252174 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11847              : 
   11848              :           /* If we need to explicitly zero inactive elements emit a
   11849              :              VEC_COND_EXPR that does so.  */
   11850       252174 :           if (need_zeroing)
   11851              :             {
   11852            0 :               vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
   11853              :                                                  vectype);
   11854              : 
   11855            0 :               tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
   11856            0 :               new_stmt = gimple_build_assign (new_temp2, VEC_COND_EXPR,
   11857              :                                               final_mask, new_temp, vec_els);
   11858            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
   11859              :                                            gsi);
   11860            0 :               new_temp = new_temp2;
   11861              :             }
   11862              :         }
   11863              : 
   11864              :       /* 3. Handle explicit realignment if necessary/supported.
   11865              :          Create in loop:
   11866              :          vec_dest = realign_load (msq, lsq, realignment_token)  */
   11867       922119 :       if (!costing_p
   11868       252174 :           && (alignment_support_scheme == dr_explicit_realign_optimized
   11869              :               || alignment_support_scheme == dr_explicit_realign))
   11870              :         {
   11871            0 :           lsq = gimple_assign_lhs (new_stmt);
   11872            0 :           if (!realignment_token)
   11873            0 :             realignment_token = dataref_ptr;
   11874            0 :           vec_dest = vect_create_destination_var (scalar_dest, vectype);
   11875            0 :           new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR, msq,
   11876              :                                           lsq, realignment_token);
   11877            0 :           new_temp = make_ssa_name (vec_dest, new_stmt);
   11878            0 :           gimple_assign_set_lhs (new_stmt, new_temp);
   11879            0 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   11880              : 
   11881            0 :           if (alignment_support_scheme == dr_explicit_realign_optimized)
   11882              :             {
   11883            0 :               gcc_assert (phi);
   11884            0 :               if (i == vec_num - 1)
   11885            0 :                 add_phi_arg (phi, lsq, loop_latch_edge (containing_loop),
   11886              :                              UNKNOWN_LOCATION);
   11887              :               msq = lsq;
   11888              :             }
   11889              :         }
   11890              : 
   11891       922119 :       if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
   11892              :         {
   11893         5941 :           if (costing_p)
   11894         4497 :             inside_cost = record_stmt_cost (cost_vec, 1, vec_perm,
   11895              :                                             slp_node, 0, vect_body);
   11896              :           else
   11897              :             {
   11898         1444 :               tree perm_mask = perm_mask_for_reverse (vectype);
   11899         1444 :               new_temp = permute_vec_elements (vinfo, new_temp, new_temp,
   11900              :                                                perm_mask, stmt_info, gsi);
   11901         1444 :               new_stmt = SSA_NAME_DEF_STMT (new_temp);
   11902              :             }
   11903              :         }
   11904              : 
   11905              :       /* Collect vector loads and later create their permutation in
   11906              :          vect_transform_slp_perm_load.  */
   11907       922119 :       if (!costing_p && (grouped_load || ls.slp_perm))
   11908        71954 :         dr_chain.quick_push (new_temp);
   11909              : 
   11910              :       /* Store vector loads in the corresponding SLP_NODE.  */
   11911       252174 :       if (!costing_p && !ls.slp_perm)
   11912       180220 :         slp_node->push_vec_def (new_stmt);
   11913              : 
   11914              :       /* With SLP permutation we load the gaps as well, without
   11915              :          we need to skip the gaps after we manage to fully load
   11916              :          all elements.  group_gap_adj is DR_GROUP_SIZE here.  */
   11917       922119 :       group_elt += nunits;
   11918       922119 :       if (!costing_p
   11919       252174 :           && maybe_ne (group_gap_adj, 0U)
   11920        46182 :           && !ls.slp_perm
   11921       943422 :           && known_eq (group_elt, group_size - group_gap_adj))
   11922              :         {
   11923        16622 :           poly_wide_int bump_val
   11924        16622 :             = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
   11925        16622 :           if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
   11926            0 :             bump_val = -bump_val;
   11927        16622 :           tree bump = wide_int_to_tree (sizetype, bump_val);
   11928        16622 :           dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
   11929              :                                          stmt_info, bump);
   11930        16622 :           group_elt = 0;
   11931        16622 :         }
   11932              :     }
   11933              :   /* Bump the vector pointer to account for a gap or for excess
   11934              :      elements loaded for a permuted SLP load.  */
   11935       574418 :   if (!costing_p
   11936       161435 :       && maybe_ne (group_gap_adj, 0U)
   11937       591494 :       && ls.slp_perm)
   11938              :     {
   11939          454 :       poly_wide_int bump_val
   11940          454 :         = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
   11941          454 :       if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
   11942            9 :         bump_val = -bump_val;
   11943          454 :       tree bump = wide_int_to_tree (sizetype, bump_val);
   11944          454 :       dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
   11945              :                                      stmt_info, bump);
   11946          454 :     }
   11947              : 
   11948       574418 :   if (ls.slp_perm)
   11949              :     {
   11950              :       /* For SLP we know we've seen all possible uses of dr_chain so
   11951              :          direct vect_transform_slp_perm_load to DCE the unused parts.
   11952              :          ???  This is a hack to prevent compile-time issues as seen
   11953              :          in PR101120 and friends.  */
   11954        54310 :       if (costing_p)
   11955              :         {
   11956        37320 :           gcc_assert (ls.n_perms != -1U && ls.n_loads != -1U);
   11957        37320 :           if (ls.n_perms != 0)
   11958        36815 :             inside_cost = record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
   11959              :                                             slp_node, 0, vect_body);
   11960        37320 :           if (n_adjacent_loads > 0)
   11961        37320 :             n_adjacent_loads = ls.n_loads;
   11962              :         }
   11963              :       else
   11964              :         {
   11965        16990 :           unsigned n_perms2, n_loads2;
   11966        16990 :           bool ok = vect_transform_slp_perm_load (vinfo, slp_node, dr_chain,
   11967              :                                                   gsi, vf, false, &n_perms2,
   11968              :                                                   &n_loads2, true);
   11969        16990 :           gcc_assert (ok && ls.n_perms == n_perms2 && ls.n_loads == n_loads2);
   11970              :         }
   11971              :     }
   11972              : 
   11973       574418 :   if (costing_p)
   11974              :     {
   11975       412983 :       gcc_assert (memory_access_type == VMAT_CONTIGUOUS
   11976              :                   || memory_access_type == VMAT_CONTIGUOUS_REVERSE);
   11977       412983 :       if (n_adjacent_loads > 0)
   11978       412983 :         vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
   11979              :                             alignment_support_scheme, misalignment, false,
   11980              :                             &inside_cost, &prologue_cost, cost_vec, cost_vec,
   11981              :                             true);
   11982       412983 :       if (dump_enabled_p ())
   11983        24074 :         dump_printf_loc (MSG_NOTE, vect_location,
   11984              :                          "vect_model_load_cost: inside_cost = %u, "
   11985              :                          "prologue_cost = %u .\n",
   11986              :                          inside_cost, prologue_cost);
   11987       412983 :       SLP_TREE_TYPE (slp_node) = load_vec_info_type;
   11988       412983 :       slp_node->data = new vect_load_store_data (std::move (ls));
   11989              :     }
   11990              : 
   11991       574418 :   return true;
   11992      1863039 : }
   11993              : 
   11994              : /* Function vect_is_simple_cond.
   11995              : 
   11996              :    Input:
   11997              :    LOOP - the loop that is being vectorized.
   11998              :    COND - Condition that is checked for simple use.
   11999              : 
   12000              :    Output:
   12001              :    *COMP_VECTYPE - the vector type for the comparison.
   12002              :    *DTS - The def types for the arguments of the comparison
   12003              : 
   12004              :    Returns whether a COND can be vectorized.  Checks whether
   12005              :    condition operands are supportable using vec_is_simple_use.  */
   12006              : 
   12007              : static bool
   12008        34860 : vect_is_simple_cond (tree cond, vec_info *vinfo,
   12009              :                      slp_tree slp_node, tree *comp_vectype,
   12010              :                      enum vect_def_type *dts, tree vectype)
   12011              : {
   12012        34860 :   tree lhs, rhs;
   12013        34860 :   tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
   12014        34860 :   slp_tree slp_op;
   12015              : 
   12016              :   /* Mask case.  */
   12017        34860 :   if (TREE_CODE (cond) == SSA_NAME
   12018        34860 :       && VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (cond)))
   12019              :     {
   12020        34848 :       if (!vect_is_simple_use (vinfo, slp_node, 0, &cond,
   12021              :                                &slp_op, &dts[0], comp_vectype)
   12022        34848 :           || !*comp_vectype
   12023        69685 :           || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
   12024              :         return false;
   12025              :       return true;
   12026              :     }
   12027              : 
   12028           12 :   if (!COMPARISON_CLASS_P (cond))
   12029              :     return false;
   12030              : 
   12031            0 :   lhs = TREE_OPERAND (cond, 0);
   12032            0 :   rhs = TREE_OPERAND (cond, 1);
   12033              : 
   12034            0 :   if (TREE_CODE (lhs) == SSA_NAME)
   12035              :     {
   12036            0 :       if (!vect_is_simple_use (vinfo, slp_node, 0,
   12037              :                                &lhs, &slp_op, &dts[0], &vectype1))
   12038              :         return false;
   12039              :     }
   12040            0 :   else if (TREE_CODE (lhs) == INTEGER_CST || TREE_CODE (lhs) == REAL_CST
   12041            0 :            || TREE_CODE (lhs) == FIXED_CST)
   12042            0 :     dts[0] = vect_constant_def;
   12043              :   else
   12044              :     return false;
   12045              : 
   12046            0 :   if (TREE_CODE (rhs) == SSA_NAME)
   12047              :     {
   12048            0 :       if (!vect_is_simple_use (vinfo, slp_node, 1,
   12049              :                                &rhs, &slp_op, &dts[1], &vectype2))
   12050              :         return false;
   12051              :     }
   12052            0 :   else if (TREE_CODE (rhs) == INTEGER_CST || TREE_CODE (rhs) == REAL_CST
   12053            0 :            || TREE_CODE (rhs) == FIXED_CST)
   12054            0 :     dts[1] = vect_constant_def;
   12055              :   else
   12056              :     return false;
   12057              : 
   12058            0 :   if (vectype1 && vectype2
   12059            0 :       && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
   12060            0 :                    TYPE_VECTOR_SUBPARTS (vectype2)))
   12061            0 :     return false;
   12062              : 
   12063            0 :   *comp_vectype = vectype1 ? vectype1 : vectype2;
   12064              :   /* Invariant comparison.  */
   12065            0 :   if (! *comp_vectype)
   12066              :     {
   12067            0 :       tree scalar_type = TREE_TYPE (lhs);
   12068            0 :       if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
   12069            0 :         *comp_vectype = truth_type_for (vectype);
   12070              :       else
   12071              :         {
   12072              :           /* If we can widen the comparison to match vectype do so.  */
   12073            0 :           if (INTEGRAL_TYPE_P (scalar_type)
   12074            0 :               && !slp_node
   12075            0 :               && tree_int_cst_lt (TYPE_SIZE (scalar_type),
   12076            0 :                                   TYPE_SIZE (TREE_TYPE (vectype))))
   12077            0 :             scalar_type = build_nonstandard_integer_type
   12078            0 :               (vector_element_bits (vectype), TYPE_UNSIGNED (scalar_type));
   12079            0 :           *comp_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
   12080              :                                                        slp_node);
   12081              :         }
   12082              :     }
   12083              : 
   12084              :   return true;
   12085              : }
   12086              : 
   12087              : /* vectorizable_condition.
   12088              : 
   12089              :    Check if STMT_INFO is conditional modify expression that can be vectorized.
   12090              :    If COST_VEC is passed, calculate costs but don't change anything,
   12091              :    otherwise, vectorize STMT_INFO: create a vectorized stmt using
   12092              :    VEC_COND_EXPR to replace it, and insert it at GSI.
   12093              : 
   12094              :    When STMT_INFO is vectorized as a nested cycle, for_reduction is true.
   12095              : 
   12096              :    Return true if STMT_INFO is vectorizable in this way.  */
   12097              : 
   12098              : static bool
   12099       678272 : vectorizable_condition (vec_info *vinfo,
   12100              :                         stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
   12101              :                         slp_tree slp_node, stmt_vector_for_cost *cost_vec)
   12102              : {
   12103       678272 :   tree scalar_dest = NULL_TREE;
   12104       678272 :   tree vec_dest = NULL_TREE;
   12105       678272 :   tree cond_expr, cond_expr0 = NULL_TREE, cond_expr1 = NULL_TREE;
   12106       678272 :   tree then_clause, else_clause;
   12107       678272 :   tree comp_vectype = NULL_TREE;
   12108       678272 :   tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
   12109       678272 :   tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
   12110       678272 :   tree vec_compare;
   12111       678272 :   tree new_temp;
   12112       678272 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
   12113       678272 :   enum vect_def_type dts[4]
   12114              :     = {vect_unknown_def_type, vect_unknown_def_type,
   12115              :        vect_unknown_def_type, vect_unknown_def_type};
   12116       678272 :   enum tree_code code, cond_code, bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
   12117       678272 :   int i;
   12118       678272 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
   12119       678272 :   vec<tree> vec_oprnds0 = vNULL;
   12120       678272 :   vec<tree> vec_oprnds1 = vNULL;
   12121       678272 :   vec<tree> vec_oprnds2 = vNULL;
   12122       678272 :   vec<tree> vec_oprnds3 = vNULL;
   12123       678272 :   tree vec_cmp_type;
   12124       678272 :   bool masked = false;
   12125              : 
   12126       678272 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
   12127              :     return false;
   12128              : 
   12129              :   /* Is vectorizable conditional operation?  */
   12130      1023730 :   gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
   12131       380289 :   if (!stmt)
   12132              :     return false;
   12133              : 
   12134       380289 :   code = gimple_assign_rhs_code (stmt);
   12135       380289 :   if (code != COND_EXPR)
   12136              :     return false;
   12137              : 
   12138        34860 :   int reduc_index = SLP_TREE_REDUC_IDX (slp_node);
   12139        34860 :   vect_reduction_type reduction_type = TREE_CODE_REDUCTION;
   12140        34860 :   bool nested_cycle_p = false;
   12141        34860 :   bool for_reduction = vect_is_reduction (stmt_info);
   12142        34860 :   if (for_reduction)
   12143              :     {
   12144          574 :       if (SLP_TREE_LANES (slp_node) > 1)
   12145              :         return false;
   12146              :       /* ???  With a reduction path we do not get at the reduction info from
   12147              :          every stmt, use the conservative default setting then.  */
   12148          654 :       if (STMT_VINFO_REDUC_DEF (vect_orig_stmt (stmt_info)))
   12149              :         {
   12150          556 :           vect_reduc_info reduc_info
   12151          556 :             = info_for_reduction (loop_vinfo, slp_node);
   12152          556 :           reduction_type = VECT_REDUC_INFO_TYPE (reduc_info);
   12153          556 :           nested_cycle_p = nested_in_vect_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
   12154              :                                                   stmt_info);
   12155              :         }
   12156              :     }
   12157              :   else
   12158              :     {
   12159        34286 :       if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
   12160              :         return false;
   12161              :     }
   12162              : 
   12163        34860 :   tree vectype = SLP_TREE_VECTYPE (slp_node);
   12164        34860 :   tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
   12165              : 
   12166        34860 :   int vec_num = vect_get_num_copies (vinfo, slp_node);
   12167              : 
   12168        34860 :   cond_expr = gimple_assign_rhs1 (stmt);
   12169        34860 :   gcc_assert (! COMPARISON_CLASS_P (cond_expr));
   12170              : 
   12171        34860 :   if (!vect_is_simple_cond (cond_expr, vinfo, slp_node,
   12172              :                             &comp_vectype, &dts[0], vectype)
   12173        34860 :       || !comp_vectype)
   12174              :     return false;
   12175              : 
   12176        34837 :   unsigned op_adjust = COMPARISON_CLASS_P (cond_expr) ? 1 : 0;
   12177        34837 :   slp_tree then_slp_node, else_slp_node;
   12178        34837 :   if (!vect_is_simple_use (vinfo, slp_node, 1 + op_adjust,
   12179              :                            &then_clause, &then_slp_node, &dts[2], &vectype1))
   12180              :     return false;
   12181        34837 :   if (!vect_is_simple_use (vinfo, slp_node, 2 + op_adjust,
   12182              :                            &else_clause, &else_slp_node, &dts[3], &vectype2))
   12183              :     return false;
   12184              : 
   12185        34837 :   if (vectype1 && !useless_type_conversion_p (vectype, vectype1))
   12186              :     return false;
   12187              : 
   12188        34837 :   if (vectype2 && !useless_type_conversion_p (vectype, vectype2))
   12189              :     return false;
   12190              : 
   12191        34837 :   masked = !COMPARISON_CLASS_P (cond_expr);
   12192        34837 :   vec_cmp_type = truth_type_for (comp_vectype);
   12193        34837 :   if (vec_cmp_type == NULL_TREE
   12194        69674 :       || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype),
   12195        34837 :                    TYPE_VECTOR_SUBPARTS (vec_cmp_type)))
   12196            0 :     return false;
   12197              : 
   12198        34837 :   cond_code = TREE_CODE (cond_expr);
   12199        34837 :   if (!masked)
   12200              :     {
   12201            0 :       cond_expr0 = TREE_OPERAND (cond_expr, 0);
   12202            0 :       cond_expr1 = TREE_OPERAND (cond_expr, 1);
   12203              :     }
   12204              : 
   12205              :   /* For conditional reductions, the "then" value needs to be the candidate
   12206              :      value calculated by this iteration while the "else" value needs to be
   12207              :      the result carried over from previous iterations.  If the COND_EXPR
   12208              :      is the other way around, we need to swap it.  */
   12209        34837 :   bool must_invert_cmp_result = false;
   12210        34837 :   if (reduction_type == EXTRACT_LAST_REDUCTION && reduc_index == 1)
   12211              :     {
   12212            0 :       if (masked)
   12213            0 :         must_invert_cmp_result = true;
   12214              :       else
   12215              :         {
   12216            0 :           bool honor_nans = HONOR_NANS (TREE_TYPE (cond_expr0));
   12217            0 :           tree_code new_code = invert_tree_comparison (cond_code, honor_nans);
   12218            0 :           if (new_code == ERROR_MARK)
   12219              :             must_invert_cmp_result = true;
   12220              :           else
   12221              :             {
   12222            0 :               cond_code = new_code;
   12223              :               /* Make sure we don't accidentally use the old condition.  */
   12224            0 :               cond_expr = NULL_TREE;
   12225              :             }
   12226              :         }
   12227              :       /* ???  The vectorized operand query below doesn't allow swapping
   12228              :          this way for SLP.  */
   12229            0 :       return false;
   12230              :       /* std::swap (then_clause, else_clause); */
   12231              :     }
   12232              : 
   12233        34837 :   if (!masked && VECTOR_BOOLEAN_TYPE_P (comp_vectype))
   12234              :     {
   12235              :       /* Boolean values may have another representation in vectors
   12236              :          and therefore we prefer bit operations over comparison for
   12237              :          them (which also works for scalar masks).  We store opcodes
   12238              :          to use in bitop1 and bitop2.  Statement is vectorized as
   12239              :          BITOP2 (rhs1 BITOP1 rhs2) or rhs1 BITOP2 (BITOP1 rhs2)
   12240              :          depending on bitop1 and bitop2 arity.  */
   12241            0 :       switch (cond_code)
   12242              :         {
   12243              :         case GT_EXPR:
   12244              :           bitop1 = BIT_NOT_EXPR;
   12245              :           bitop2 = BIT_AND_EXPR;
   12246              :           break;
   12247            0 :         case GE_EXPR:
   12248            0 :           bitop1 = BIT_NOT_EXPR;
   12249            0 :           bitop2 = BIT_IOR_EXPR;
   12250            0 :           break;
   12251            0 :         case LT_EXPR:
   12252            0 :           bitop1 = BIT_NOT_EXPR;
   12253            0 :           bitop2 = BIT_AND_EXPR;
   12254            0 :           std::swap (cond_expr0, cond_expr1);
   12255            0 :           break;
   12256            0 :         case LE_EXPR:
   12257            0 :           bitop1 = BIT_NOT_EXPR;
   12258            0 :           bitop2 = BIT_IOR_EXPR;
   12259            0 :           std::swap (cond_expr0, cond_expr1);
   12260            0 :           break;
   12261            0 :         case NE_EXPR:
   12262            0 :           bitop1 = BIT_XOR_EXPR;
   12263            0 :           break;
   12264            0 :         case EQ_EXPR:
   12265            0 :           bitop1 = BIT_XOR_EXPR;
   12266            0 :           bitop2 = BIT_NOT_EXPR;
   12267            0 :           break;
   12268              :         default:
   12269              :           return false;
   12270              :         }
   12271              :       cond_code = SSA_NAME;
   12272              :     }
   12273              : 
   12274        34837 :   if (TREE_CODE_CLASS (cond_code) == tcc_comparison
   12275            0 :       && reduction_type == EXTRACT_LAST_REDUCTION
   12276        34837 :       && !expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type, cond_code))
   12277              :     {
   12278            0 :       if (dump_enabled_p ())
   12279            0 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   12280              :                          "reduction comparison operation not supported.\n");
   12281            0 :       return false;
   12282              :     }
   12283              : 
   12284        34837 :   if (cost_vec)
   12285              :     {
   12286        26336 :       if (bitop1 != NOP_EXPR)
   12287              :         {
   12288            0 :           machine_mode mode = TYPE_MODE (comp_vectype);
   12289            0 :           optab optab;
   12290              : 
   12291            0 :           optab = optab_for_tree_code (bitop1, comp_vectype, optab_default);
   12292            0 :           if (!optab || !can_implement_p (optab, mode))
   12293            0 :             return false;
   12294              : 
   12295            0 :           if (bitop2 != NOP_EXPR)
   12296              :             {
   12297            0 :               optab = optab_for_tree_code (bitop2, comp_vectype,
   12298              :                                            optab_default);
   12299            0 :               if (!optab || !can_implement_p (optab, mode))
   12300            0 :                 return false;
   12301              :             }
   12302              :         }
   12303              : 
   12304        26336 :       vect_cost_for_stmt kind = vector_stmt;
   12305        26336 :       if (reduction_type == EXTRACT_LAST_REDUCTION)
   12306              :         /* Count one reduction-like operation per vector.  */
   12307              :         kind = vec_to_scalar;
   12308        26336 :       else if ((masked && !expand_vec_cond_expr_p (vectype, comp_vectype))
   12309        26336 :                || (!masked
   12310            0 :                    && (!expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type,
   12311              :                                                cond_code)
   12312            0 :                        || !expand_vec_cond_expr_p (vectype, vec_cmp_type))))
   12313            6 :         return false;
   12314              : 
   12315        26330 :       if (!vect_maybe_update_slp_op_vectype (SLP_TREE_CHILDREN (slp_node)[0],
   12316              :                                              comp_vectype)
   12317        26330 :           || (op_adjust == 1
   12318            0 :               && !vect_maybe_update_slp_op_vectype
   12319            0 :                               (SLP_TREE_CHILDREN (slp_node)[1], comp_vectype))
   12320        26330 :           || !vect_maybe_update_slp_op_vectype (then_slp_node, vectype)
   12321        52660 :           || !vect_maybe_update_slp_op_vectype (else_slp_node, vectype))
   12322              :         {
   12323            0 :           if (dump_enabled_p ())
   12324            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   12325              :                              "incompatible vector types for invariants\n");
   12326            0 :           return false;
   12327              :         }
   12328              : 
   12329        26330 :       if (loop_vinfo && for_reduction
   12330          427 :           && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
   12331              :         {
   12332           68 :           if (reduction_type == EXTRACT_LAST_REDUCTION)
   12333              :             {
   12334            0 :               if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
   12335              :                                                   vectype, OPTIMIZE_FOR_SPEED))
   12336            0 :                 vect_record_loop_len (loop_vinfo,
   12337              :                                       &LOOP_VINFO_LENS (loop_vinfo),
   12338              :                                       vec_num, vectype, 1);
   12339              :               else
   12340            0 :                 vect_record_loop_mask (loop_vinfo,
   12341              :                                        &LOOP_VINFO_MASKS (loop_vinfo),
   12342              :                                        vec_num, vectype, NULL);
   12343              :             }
   12344              :           /* Extra inactive lanes should be safe for vect_nested_cycle.  */
   12345           68 :           else if (!nested_cycle_p)
   12346              :             {
   12347           68 :               if (dump_enabled_p ())
   12348            8 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   12349              :                                  "conditional reduction prevents the use"
   12350              :                                  " of partial vectors.\n");
   12351           68 :               LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
   12352              :             }
   12353              :         }
   12354              : 
   12355        26330 :       SLP_TREE_TYPE (slp_node) = condition_vec_info_type;
   12356        26330 :       vect_model_simple_cost (vinfo, 1, slp_node, cost_vec, kind);
   12357        26330 :       return true;
   12358              :     }
   12359              : 
   12360              :   /* Transform.  */
   12361              : 
   12362              :   /* Handle def.  */
   12363         8501 :   scalar_dest = gimple_assign_lhs (stmt);
   12364         8501 :   if (reduction_type != EXTRACT_LAST_REDUCTION)
   12365         8501 :     vec_dest = vect_create_destination_var (scalar_dest, vectype);
   12366              : 
   12367         8501 :   bool swap_cond_operands = false;
   12368              : 
   12369              :   /* See whether another part of the vectorized code applies a loop
   12370              :      mask to the condition, or to its inverse.  */
   12371              : 
   12372         8501 :   vec_loop_masks *masks = NULL;
   12373         8501 :   vec_loop_lens *lens = NULL;
   12374         8501 :   if (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
   12375              :     {
   12376            0 :       if (reduction_type == EXTRACT_LAST_REDUCTION)
   12377            0 :         lens = &LOOP_VINFO_LENS (loop_vinfo);
   12378              :     }
   12379         8501 :   else if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
   12380              :     {
   12381            3 :       if (reduction_type == EXTRACT_LAST_REDUCTION)
   12382            0 :         masks = &LOOP_VINFO_MASKS (loop_vinfo);
   12383              :       else
   12384              :         {
   12385            3 :           scalar_cond_masked_key cond (cond_expr, 1);
   12386            3 :           if (loop_vinfo->scalar_cond_masked_set.contains (cond))
   12387            0 :             masks = &LOOP_VINFO_MASKS (loop_vinfo);
   12388              :           else
   12389              :             {
   12390            3 :               bool honor_nans = HONOR_NANS (TREE_TYPE (cond.op0));
   12391            3 :               tree_code orig_code = cond.code;
   12392            3 :               cond.code = invert_tree_comparison (cond.code, honor_nans);
   12393            3 :               if (!masked && loop_vinfo->scalar_cond_masked_set.contains (cond))
   12394              :                 {
   12395            0 :                   masks = &LOOP_VINFO_MASKS (loop_vinfo);
   12396            0 :                   cond_code = cond.code;
   12397            0 :                   swap_cond_operands = true;
   12398              :                 }
   12399              :               else
   12400              :                 {
   12401              :                   /* Try the inverse of the current mask.  We check if the
   12402              :                      inverse mask is live and if so we generate a negate of
   12403              :                      the current mask such that we still honor NaNs.  */
   12404            3 :                   cond.inverted_p = true;
   12405            3 :                   cond.code = orig_code;
   12406            3 :                   if (loop_vinfo->scalar_cond_masked_set.contains (cond))
   12407              :                     {
   12408            0 :                       masks = &LOOP_VINFO_MASKS (loop_vinfo);
   12409            0 :                       cond_code = cond.code;
   12410            0 :                       swap_cond_operands = true;
   12411            0 :                       must_invert_cmp_result = true;
   12412              :                     }
   12413              :                 }
   12414              :             }
   12415              :         }
   12416              :     }
   12417              : 
   12418              :   /* Handle cond expr.  */
   12419         8501 :   if (masked)
   12420         8501 :     vect_get_vec_defs (vinfo, slp_node,
   12421              :                        cond_expr, &vec_oprnds0,
   12422              :                        then_clause, &vec_oprnds2,
   12423              :                        reduction_type != EXTRACT_LAST_REDUCTION
   12424              :                        ? else_clause : NULL, &vec_oprnds3);
   12425              :   else
   12426            0 :     vect_get_vec_defs (vinfo, slp_node,
   12427              :                        cond_expr0, &vec_oprnds0,
   12428              :                        cond_expr1, &vec_oprnds1,
   12429              :                        then_clause, &vec_oprnds2,
   12430              :                        reduction_type != EXTRACT_LAST_REDUCTION
   12431              :                        ? else_clause : NULL, &vec_oprnds3);
   12432              : 
   12433         8501 :   if (reduction_type == EXTRACT_LAST_REDUCTION)
   12434            0 :     vec_else_clause = else_clause;
   12435              : 
   12436              :   /* Arguments are ready.  Create the new vector stmt.  */
   12437        20011 :   FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
   12438              :     {
   12439        11510 :       vec_then_clause = vec_oprnds2[i];
   12440        11510 :       if (reduction_type != EXTRACT_LAST_REDUCTION)
   12441        11510 :         vec_else_clause = vec_oprnds3[i];
   12442              : 
   12443        11510 :       if (swap_cond_operands)
   12444            0 :         std::swap (vec_then_clause, vec_else_clause);
   12445              : 
   12446        11510 :       if (masked)
   12447              :         vec_compare = vec_cond_lhs;
   12448              :       else
   12449              :         {
   12450            0 :           vec_cond_rhs = vec_oprnds1[i];
   12451            0 :           if (bitop1 == NOP_EXPR)
   12452              :             {
   12453            0 :               gimple_seq stmts = NULL;
   12454            0 :               vec_compare = gimple_build (&stmts, cond_code, vec_cmp_type,
   12455              :                                            vec_cond_lhs, vec_cond_rhs);
   12456            0 :               gsi_insert_before (gsi, stmts, GSI_SAME_STMT);
   12457              :             }
   12458              :           else
   12459              :             {
   12460            0 :               new_temp = make_ssa_name (vec_cmp_type);
   12461            0 :               gassign *new_stmt;
   12462            0 :               if (bitop1 == BIT_NOT_EXPR)
   12463            0 :                 new_stmt = gimple_build_assign (new_temp, bitop1,
   12464              :                                                 vec_cond_rhs);
   12465              :               else
   12466            0 :                 new_stmt
   12467            0 :                   = gimple_build_assign (new_temp, bitop1, vec_cond_lhs,
   12468              :                                          vec_cond_rhs);
   12469            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   12470            0 :               if (bitop2 == NOP_EXPR)
   12471              :                 vec_compare = new_temp;
   12472            0 :               else if (bitop2 == BIT_NOT_EXPR
   12473            0 :                        && reduction_type != EXTRACT_LAST_REDUCTION)
   12474              :                 {
   12475              :                   /* Instead of doing ~x ? y : z do x ? z : y.  */
   12476              :                   vec_compare = new_temp;
   12477              :                   std::swap (vec_then_clause, vec_else_clause);
   12478              :                 }
   12479              :               else
   12480              :                 {
   12481            0 :                   vec_compare = make_ssa_name (vec_cmp_type);
   12482            0 :                   if (bitop2 == BIT_NOT_EXPR)
   12483            0 :                     new_stmt
   12484            0 :                       = gimple_build_assign (vec_compare, bitop2, new_temp);
   12485              :                   else
   12486            0 :                     new_stmt
   12487            0 :                       = gimple_build_assign (vec_compare, bitop2,
   12488              :                                              vec_cond_lhs, new_temp);
   12489            0 :                   vect_finish_stmt_generation (vinfo, stmt_info,
   12490              :                                                new_stmt, gsi);
   12491              :                 }
   12492              :             }
   12493              :         }
   12494              : 
   12495              :       /* If we decided to apply a loop mask to the result of the vector
   12496              :          comparison, AND the comparison with the mask now.  Later passes
   12497              :          should then be able to reuse the AND results between mulitple
   12498              :          vector statements.
   12499              : 
   12500              :          For example:
   12501              :          for (int i = 0; i < 100; ++i)
   12502              :          x[i] = y[i] ? z[i] : 10;
   12503              : 
   12504              :          results in following optimized GIMPLE:
   12505              : 
   12506              :          mask__35.8_43 = vect__4.7_41 != { 0, ... };
   12507              :          vec_mask_and_46 = loop_mask_40 & mask__35.8_43;
   12508              :          _19 = &MEM[base: z_12(D), index: ivtmp_56, step: 4, offset: 0B];
   12509              :          vect_iftmp.11_47 = .MASK_LOAD (_19, 4B, vec_mask_and_46);
   12510              :          vect_iftmp.12_52 = VEC_COND_EXPR <vec_mask_and_46,
   12511              :          vect_iftmp.11_47, { 10, ... }>;
   12512              : 
   12513              :          instead of using a masked and unmasked forms of
   12514              :          vec != { 0, ... } (masked in the MASK_LOAD,
   12515              :          unmasked in the VEC_COND_EXPR).  */
   12516              : 
   12517              :       /* Force vec_compare to be an SSA_NAME rather than a comparison,
   12518              :          in cases where that's necessary.  */
   12519              : 
   12520        11510 :       tree len = NULL_TREE, bias = NULL_TREE;
   12521        11510 :       if (masks || lens || reduction_type == EXTRACT_LAST_REDUCTION)
   12522              :         {
   12523            0 :           if (!is_gimple_val (vec_compare))
   12524              :             {
   12525            0 :               tree vec_compare_name = make_ssa_name (vec_cmp_type);
   12526            0 :               gassign *new_stmt = gimple_build_assign (vec_compare_name,
   12527              :                                                        vec_compare);
   12528            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   12529            0 :               vec_compare = vec_compare_name;
   12530              :             }
   12531              : 
   12532            0 :           if (must_invert_cmp_result)
   12533              :             {
   12534            0 :               tree vec_compare_name = make_ssa_name (vec_cmp_type);
   12535            0 :               gassign *new_stmt = gimple_build_assign (vec_compare_name,
   12536              :                                                        BIT_NOT_EXPR,
   12537              :                                                        vec_compare);
   12538            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   12539            0 :               vec_compare = vec_compare_name;
   12540              :             }
   12541              : 
   12542            0 :           if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
   12543              :                                               vectype, OPTIMIZE_FOR_SPEED))
   12544              :             {
   12545            0 :               if (lens)
   12546              :                 {
   12547              :                   /* ??? Do we really want the adjusted LEN here?  Isn't this
   12548              :                      based on number of elements?  */
   12549            0 :                   len = vect_get_loop_len (loop_vinfo, gsi, lens,
   12550              :                                            vec_num, vectype, i, 1, true);
   12551            0 :                   signed char biasval
   12552            0 :                     = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
   12553            0 :                   bias = build_int_cst (intQI_type_node, biasval);
   12554              :                 }
   12555              :               else
   12556              :                 {
   12557            0 :                   len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
   12558            0 :                   bias = build_int_cst (intQI_type_node, 0);
   12559              :                 }
   12560              :             }
   12561            0 :           if (masks)
   12562              :             {
   12563            0 :               tree loop_mask
   12564            0 :                 = vect_get_loop_mask (loop_vinfo, gsi, masks, vec_num,
   12565              :                                       vectype, i);
   12566            0 :               tree tmp2 = make_ssa_name (vec_cmp_type);
   12567            0 :               gassign *g
   12568            0 :                 = gimple_build_assign (tmp2, BIT_AND_EXPR, vec_compare,
   12569              :                                        loop_mask);
   12570            0 :               vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
   12571            0 :               vec_compare = tmp2;
   12572              :             }
   12573              :         }
   12574              : 
   12575            0 :       gimple *new_stmt;
   12576            0 :       if (reduction_type == EXTRACT_LAST_REDUCTION)
   12577              :         {
   12578            0 :           gimple *old_stmt = vect_orig_stmt (stmt_info)->stmt;
   12579            0 :           tree lhs = gimple_get_lhs (old_stmt);
   12580            0 :           if ((unsigned)i != vec_oprnds0.length () - 1)
   12581            0 :             lhs = copy_ssa_name (lhs);
   12582            0 :           if (len)
   12583            0 :             new_stmt = gimple_build_call_internal
   12584            0 :                 (IFN_LEN_FOLD_EXTRACT_LAST, 5, vec_else_clause, vec_compare,
   12585              :                  vec_then_clause, len, bias);
   12586              :           else
   12587            0 :             new_stmt = gimple_build_call_internal
   12588            0 :                 (IFN_FOLD_EXTRACT_LAST, 3, vec_else_clause, vec_compare,
   12589              :                  vec_then_clause);
   12590            0 :           gimple_call_set_lhs (new_stmt, lhs);
   12591            0 :           SSA_NAME_DEF_STMT (lhs) = new_stmt;
   12592            0 :           if ((unsigned)i != vec_oprnds0.length () - 1)
   12593              :             {
   12594            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   12595            0 :               vec_else_clause = lhs;
   12596              :             }
   12597            0 :           else if (old_stmt == gsi_stmt (*gsi))
   12598            0 :             vect_finish_replace_stmt (vinfo, stmt_info, new_stmt);
   12599              :           else
   12600              :             {
   12601              :               /* In this case we're moving the definition to later in the
   12602              :                  block.  That doesn't matter because the only uses of the
   12603              :                  lhs are in phi statements.  */
   12604            0 :               gimple_stmt_iterator old_gsi = gsi_for_stmt (old_stmt);
   12605            0 :               gsi_remove (&old_gsi, true);
   12606            0 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   12607              :             }
   12608              :         }
   12609              :       else
   12610              :         {
   12611        11510 :           new_temp = make_ssa_name (vec_dest);
   12612        11510 :           new_stmt = gimple_build_assign (new_temp, VEC_COND_EXPR, vec_compare,
   12613              :                                           vec_then_clause, vec_else_clause);
   12614        11510 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   12615              :         }
   12616        11510 :       slp_node->push_vec_def (new_stmt);
   12617              :     }
   12618              : 
   12619         8501 :   vec_oprnds0.release ();
   12620         8501 :   vec_oprnds1.release ();
   12621         8501 :   vec_oprnds2.release ();
   12622         8501 :   vec_oprnds3.release ();
   12623              : 
   12624         8501 :   return true;
   12625              : }
   12626              : 
   12627              : /* Helper of vectorizable_comparison.
   12628              : 
   12629              :    Check if STMT_INFO is comparison expression CODE that can be vectorized.
   12630              :    If COST_VEC is passed, calculate costs but don't change anything,
   12631              :    otherwise, vectorize STMT_INFO: create a vectorized comparison, and insert
   12632              :    it at GSI.
   12633              : 
   12634              :    Return true if STMT_INFO is vectorizable in this way.  */
   12635              : 
   12636              : static bool
   12637       353742 : vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
   12638              :                            stmt_vec_info stmt_info, tree_code code,
   12639              :                            gimple_stmt_iterator *gsi,
   12640              :                            slp_tree slp_node, stmt_vector_for_cost *cost_vec)
   12641              : {
   12642       353742 :   tree lhs, rhs1, rhs2;
   12643       353742 :   tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
   12644       353742 :   tree vec_rhs1 = NULL_TREE, vec_rhs2 = NULL_TREE;
   12645       353742 :   tree new_temp;
   12646       353742 :   enum vect_def_type dts[2] = {vect_unknown_def_type, vect_unknown_def_type};
   12647       353742 :   poly_uint64 nunits;
   12648       353742 :   enum tree_code bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
   12649       353742 :   int i;
   12650       353742 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
   12651       353742 :   vec<tree> vec_oprnds0 = vNULL;
   12652       353742 :   vec<tree> vec_oprnds1 = vNULL;
   12653       353742 :   tree mask_type;
   12654       353742 :   tree mask = NULL_TREE;
   12655              : 
   12656       353742 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
   12657              :     return false;
   12658              : 
   12659       353742 :   if (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype))
   12660              :     return false;
   12661              : 
   12662       159766 :   mask_type = vectype;
   12663       159766 :   nunits = TYPE_VECTOR_SUBPARTS (vectype);
   12664              : 
   12665       159766 :   if (TREE_CODE_CLASS (code) != tcc_comparison)
   12666              :     return false;
   12667              : 
   12668       157967 :   slp_tree slp_rhs1, slp_rhs2;
   12669       157967 :   if (!vect_is_simple_use (vinfo, slp_node,
   12670              :                            0, &rhs1, &slp_rhs1, &dts[0], &vectype1))
   12671              :     return false;
   12672              : 
   12673       157967 :   if (!vect_is_simple_use (vinfo, slp_node,
   12674              :                            1, &rhs2, &slp_rhs2, &dts[1], &vectype2))
   12675              :     return false;
   12676              : 
   12677       122691 :   if (vectype1 && vectype2
   12678       230810 :       && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
   12679        72843 :                    TYPE_VECTOR_SUBPARTS (vectype2)))
   12680           16 :     return false;
   12681              : 
   12682       157951 :   vectype = vectype1 ? vectype1 : vectype2;
   12683              : 
   12684              :   /* Invariant comparison.  */
   12685       157951 :   if (!vectype)
   12686              :     {
   12687        30369 :       vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (rhs1), slp_node);
   12688        30369 :       if (!vectype || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype), nunits))
   12689            7 :         return false;
   12690              :     }
   12691       127582 :   else if (maybe_ne (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
   12692              :     return false;
   12693              : 
   12694              :   /* Can't compare mask and non-mask types.  */
   12695       122675 :   if (vectype1 && vectype2
   12696       375881 :       && (VECTOR_BOOLEAN_TYPE_P (vectype1) ^ VECTOR_BOOLEAN_TYPE_P (vectype2)))
   12697              :     return false;
   12698              : 
   12699              :   /* Boolean values may have another representation in vectors
   12700              :      and therefore we prefer bit operations over comparison for
   12701              :      them (which also works for scalar masks).  We store opcodes
   12702              :      to use in bitop1 and bitop2.  Statement is vectorized as
   12703              :        BITOP2 (rhs1 BITOP1 rhs2) or
   12704              :        rhs1 BITOP2 (BITOP1 rhs2)
   12705              :      depending on bitop1 and bitop2 arity.  */
   12706       157936 :   bool swap_p = false;
   12707       157936 :   if (VECTOR_BOOLEAN_TYPE_P (vectype))
   12708              :     {
   12709          654 :       if (code == GT_EXPR)
   12710              :         {
   12711              :           bitop1 = BIT_NOT_EXPR;
   12712              :           bitop2 = BIT_AND_EXPR;
   12713              :         }
   12714              :       else if (code == GE_EXPR)
   12715              :         {
   12716              :           bitop1 = BIT_NOT_EXPR;
   12717              :           bitop2 = BIT_IOR_EXPR;
   12718              :         }
   12719              :       else if (code == LT_EXPR)
   12720              :         {
   12721              :           bitop1 = BIT_NOT_EXPR;
   12722              :           bitop2 = BIT_AND_EXPR;
   12723              :           swap_p = true;
   12724              :         }
   12725              :       else if (code == LE_EXPR)
   12726              :         {
   12727              :           bitop1 = BIT_NOT_EXPR;
   12728              :           bitop2 = BIT_IOR_EXPR;
   12729              :           swap_p = true;
   12730              :         }
   12731              :       else
   12732              :         {
   12733              :           bitop1 = BIT_XOR_EXPR;
   12734              :           if (code == EQ_EXPR)
   12735              :             bitop2 = BIT_NOT_EXPR;
   12736              :         }
   12737              :     }
   12738              : 
   12739       157936 :   if (cost_vec)
   12740              :     {
   12741       145486 :       if (bitop1 == NOP_EXPR)
   12742              :         {
   12743       144964 :           if (!expand_vec_cmp_expr_p (vectype, mask_type, code))
   12744              :             return false;
   12745              :         }
   12746              :       else
   12747              :         {
   12748          522 :           machine_mode mode = TYPE_MODE (vectype);
   12749          522 :           optab optab;
   12750              : 
   12751          522 :           optab = optab_for_tree_code (bitop1, vectype, optab_default);
   12752          522 :           if (!optab || !can_implement_p (optab, mode))
   12753            0 :             return false;
   12754              : 
   12755          522 :           if (bitop2 != NOP_EXPR)
   12756              :             {
   12757           91 :               optab = optab_for_tree_code (bitop2, vectype, optab_default);
   12758           91 :               if (!optab || !can_implement_p (optab, mode))
   12759            0 :                 return false;
   12760              :             }
   12761              :         }
   12762              : 
   12763              :       /* Put types on constant and invariant SLP children.  */
   12764       137427 :       if (!vect_maybe_update_slp_op_vectype (slp_rhs1, vectype)
   12765       137427 :           || !vect_maybe_update_slp_op_vectype (slp_rhs2, vectype))
   12766              :         {
   12767            2 :           if (dump_enabled_p ())
   12768            2 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   12769              :                              "incompatible vector types for invariants\n");
   12770            2 :           return false;
   12771              :         }
   12772              : 
   12773       137425 :       vect_model_simple_cost (vinfo, 1 + (bitop2 != NOP_EXPR),
   12774              :                               slp_node, cost_vec);
   12775       137425 :       return true;
   12776              :     }
   12777              : 
   12778              :   /* Transform.  */
   12779              : 
   12780              :   /* Handle def.  */
   12781        12450 :   lhs = gimple_get_lhs (STMT_VINFO_STMT (stmt_info));
   12782        12450 :   if (lhs)
   12783        12450 :     mask = vect_create_destination_var (lhs, mask_type);
   12784              : 
   12785        12450 :   vect_get_vec_defs (vinfo, slp_node, rhs1, &vec_oprnds0, rhs2, &vec_oprnds1);
   12786        12450 :   if (swap_p)
   12787           58 :     std::swap (vec_oprnds0, vec_oprnds1);
   12788              : 
   12789              :   /* Arguments are ready.  Create the new vector stmt.  */
   12790        31399 :   FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_rhs1)
   12791              :     {
   12792        18949 :       gimple *new_stmt;
   12793        18949 :       vec_rhs2 = vec_oprnds1[i];
   12794              : 
   12795        18949 :       if (lhs)
   12796        18949 :         new_temp = make_ssa_name (mask);
   12797              :       else
   12798            0 :         new_temp = make_temp_ssa_name (mask_type, NULL, "cmp");
   12799        18949 :       if (bitop1 == NOP_EXPR)
   12800              :         {
   12801        18807 :           new_stmt = gimple_build_assign (new_temp, code,
   12802              :                                           vec_rhs1, vec_rhs2);
   12803        18807 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   12804              :         }
   12805              :       else
   12806              :         {
   12807          142 :           if (bitop1 == BIT_NOT_EXPR)
   12808           84 :             new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs2);
   12809              :           else
   12810           58 :             new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs1,
   12811              :                                             vec_rhs2);
   12812          142 :           vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   12813          142 :           if (bitop2 != NOP_EXPR)
   12814              :             {
   12815           84 :               tree res = make_ssa_name (mask);
   12816           84 :               if (bitop2 == BIT_NOT_EXPR)
   12817            0 :                 new_stmt = gimple_build_assign (res, bitop2, new_temp);
   12818              :               else
   12819           84 :                 new_stmt = gimple_build_assign (res, bitop2, vec_rhs1,
   12820              :                                                 new_temp);
   12821           84 :               vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
   12822              :             }
   12823              :         }
   12824        18949 :       slp_node->push_vec_def (new_stmt);
   12825              :     }
   12826              : 
   12827        12450 :   vec_oprnds0.release ();
   12828        12450 :   vec_oprnds1.release ();
   12829              : 
   12830        12450 :   return true;
   12831              : }
   12832              : 
   12833              : /* vectorizable_comparison.
   12834              : 
   12835              :    Check if STMT_INFO is comparison expression that can be vectorized.
   12836              :    If COST_VEC is passed, calculate costs but don't change anything,
   12837              :    otherwise, vectorize STMT_INFO: create a vectorized comparison, and insert
   12838              :    it at GSI.
   12839              : 
   12840              :    Return true if STMT_INFO is vectorizable in this way.  */
   12841              : 
   12842              : static bool
   12843       655891 : vectorizable_comparison (vec_info *vinfo,
   12844              :                          stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
   12845              :                          slp_tree slp_node, stmt_vector_for_cost *cost_vec)
   12846              : {
   12847       655891 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
   12848              : 
   12849       655891 :   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
   12850              :     return false;
   12851              : 
   12852       655891 :   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
   12853              :     return false;
   12854              : 
   12855       859758 :   gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
   12856       351140 :   if (!stmt)
   12857              :     return false;
   12858              : 
   12859       351140 :   enum tree_code code = gimple_assign_rhs_code (stmt);
   12860       351140 :   tree vectype = SLP_TREE_VECTYPE (slp_node);
   12861       351140 :   if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
   12862              :                                   slp_node, cost_vec))
   12863              :     return false;
   12864              : 
   12865       147273 :   if (cost_vec)
   12866       134823 :     SLP_TREE_TYPE (slp_node) = comparison_vec_info_type;
   12867              : 
   12868              :   return true;
   12869              : }
   12870              : 
   12871              : /* Check to see if the target supports any of the compare and branch optabs for
   12872              :    vectors with MODE as these would be required when expanding.  */
   12873              : static bool
   12874        61107 : supports_vector_compare_and_branch (loop_vec_info loop_vinfo, machine_mode mode)
   12875              : {
   12876        61107 :   bool masked_loop_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
   12877        61107 :   bool len_loop_p = LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
   12878              : 
   12879              :   /* The vectorizer only produces vec_cbranch_any_optab directly.  So only
   12880              :      check for support for that or vec_cbranch_any_optab when masked.
   12881              :      We can't produce vcond_cbranch_any directly from the vectorizer as we
   12882              :      want to keep gimple_cond as the GIMPLE representation.  But we'll fold
   12883              :      it in expand.  For that reason we require a backend to support the
   12884              :      unconditional vector cbranch optab if they support the conditional one,
   12885              :      which is just an optimization on the unconditional one.  */
   12886        61107 :   if (masked_loop_p
   12887        61107 :       && direct_optab_handler (cond_vec_cbranch_any_optab, mode)
   12888              :                 != CODE_FOR_nothing)
   12889              :     return true;
   12890        61107 :   else if (len_loop_p
   12891        61107 :            && direct_optab_handler (cond_len_vec_cbranch_any_optab, mode)
   12892              :                 != CODE_FOR_nothing)
   12893              :     return true;
   12894        61107 :   else if (!masked_loop_p && !len_loop_p
   12895       122214 :            && direct_optab_handler (vec_cbranch_any_optab, mode)
   12896              :                 != CODE_FOR_nothing)
   12897              :     return true;
   12898              : 
   12899              :   /* The target can implement cbranch to distinguish between boolean vector
   12900              :      types and data types if they don't have a different mode for both.  */
   12901        61107 :   return direct_optab_handler (cbranch_optab, mode) != CODE_FOR_nothing;
   12902              : }
   12903              : 
   12904              : /* Determine the type to use for early break vectorization's scalar IV.  If
   12905              :    no type is possible return false.  */
   12906              : 
   12907              : static bool
   12908         2602 : vect_compute_type_for_early_break_scalar_iv (loop_vec_info loop_vinfo)
   12909              : {
   12910              :   /* Check if we have a usable scalar IV type for vectorization.  */
   12911         2602 :   tree iters_vf_type = sizetype;
   12912         2602 :   if (!LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo))
   12913              :     {
   12914              :       /* Find the type with the minimum precision we can use
   12915              :          for the scalar IV.  */
   12916         2379 :       tree cand_type = TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo));
   12917              : 
   12918              :       /* Work out how many bits we need to represent the limit.  */
   12919         2379 :       unsigned int min_ni_width
   12920         2379 :         = vect_min_prec_for_max_niters (loop_vinfo, 1);
   12921              : 
   12922              :       /* Check if we're using PFA, if so we need a signed IV and an
   12923              :          extra bit for the sign.  */
   12924         2379 :       if (TYPE_UNSIGNED (cand_type)
   12925         2379 :           && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
   12926         3923 :           && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo))
   12927          156 :         min_ni_width += 1;
   12928              : 
   12929         2379 :       if (TYPE_PRECISION (cand_type) >= min_ni_width)
   12930         2308 :         iters_vf_type = unsigned_type_for (cand_type);
   12931              :       else
   12932              :         {
   12933           71 :           opt_scalar_int_mode cmp_mode_iter;
   12934           71 :           tree iv_type = NULL_TREE;
   12935          347 :           FOR_EACH_MODE_IN_CLASS (cmp_mode_iter, MODE_INT)
   12936              :             {
   12937          347 :               auto cmp_mode = cmp_mode_iter.require ();
   12938          347 :               unsigned int cmp_bits = GET_MODE_BITSIZE (cmp_mode);
   12939          347 :               if (cmp_bits >= min_ni_width
   12940          347 :                   && targetm.scalar_mode_supported_p (cmp_mode))
   12941              :                 {
   12942           71 :                   iv_type = build_nonstandard_integer_type (cmp_bits, true);
   12943           71 :                   if (iv_type)
   12944              :                     break;
   12945              :                 }
   12946              :             }
   12947              : 
   12948           71 :           if (!iv_type)
   12949              :             {
   12950            0 :               if (dump_enabled_p ())
   12951            0 :                 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   12952              :                        "can't vectorize early exit because the "
   12953              :                        "target doesn't support a scalar type wide "
   12954              :                        "wide enough to hold niters.\n");
   12955            0 :               return false;
   12956              :             }
   12957           71 :           iters_vf_type = iv_type;
   12958              :         }
   12959              :     }
   12960              : 
   12961         2602 :   LOOP_VINFO_EARLY_BRK_IV_TYPE (loop_vinfo) = iters_vf_type;
   12962         2602 :   return true;
   12963              : }
   12964              : 
   12965              : /* Check to see if the current early break given in STMT_INFO is valid for
   12966              :    vectorization.  */
   12967              : 
   12968              : bool
   12969       243592 : vectorizable_early_exit (loop_vec_info loop_vinfo, stmt_vec_info stmt_info,
   12970              :                          gimple_stmt_iterator *gsi,
   12971              :                          slp_tree slp_node, stmt_vector_for_cost *cost_vec)
   12972              : {
   12973       243592 :   if (!is_a <gcond *> (STMT_VINFO_STMT (stmt_info)))
   12974              :     return false;
   12975              : 
   12976        62675 :   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_condition_def)
   12977              :     return false;
   12978              : 
   12979        62675 :   if (!STMT_VINFO_RELEVANT_P (stmt_info))
   12980              :     return false;
   12981              : 
   12982        62675 :   DUMP_VECT_SCOPE ("vectorizable_early_exit");
   12983              : 
   12984        62675 :   auto code = gimple_cond_code (STMT_VINFO_STMT (stmt_info));
   12985              : 
   12986              :   /* For SLP we don't want to use the type of the operands of the SLP node, when
   12987              :      vectorizing using SLP slp_node will be the children of the gcond and we
   12988              :      want to use the type of the direct children which since the gcond is root
   12989              :      will be the current node, rather than a child node as vect_is_simple_use
   12990              :      assumes.  */
   12991        62675 :   tree vectype = SLP_TREE_VECTYPE (slp_node);
   12992        62675 :   if (!vectype)
   12993              :     return false;
   12994              : 
   12995        62675 :   machine_mode mode = TYPE_MODE (vectype);
   12996        62675 :   int vec_num = vect_get_num_copies (loop_vinfo, slp_node);
   12997              : 
   12998        62675 :   vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
   12999        62675 :   vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
   13000        62675 :   bool masked_loop_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
   13001        62675 :   bool len_loop_p = LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
   13002              : 
   13003              :   /* Now build the new conditional.  Pattern gimple_conds get dropped during
   13004              :      codegen so we must replace the original insn.  */
   13005        62675 :   gimple *orig_stmt = STMT_VINFO_STMT (vect_orig_stmt (stmt_info));
   13006        62675 :   gcond *cond_stmt = as_a <gcond *>(orig_stmt);
   13007              : 
   13008        62675 :   tree vectype_out = vectype;
   13009        62675 :   auto bb = gimple_bb (cond_stmt);
   13010        62675 :   edge exit_true_edge = EDGE_SUCC (bb, 0);
   13011        62675 :   if (exit_true_edge->flags & EDGE_FALSE_VALUE)
   13012          660 :     exit_true_edge = EDGE_SUCC (bb, 1);
   13013        62675 :   gcc_assert (exit_true_edge->flags & EDGE_TRUE_VALUE);
   13014              : 
   13015              :   /* When vectorizing we assume that if the branch edge is taken that we're
   13016              :      exiting the loop.  This is not however always the case as the compiler will
   13017              :      rewrite conditions to always be a comparison against 0.  To do this it
   13018              :      sometimes flips the edges.  This is fine for scalar,  but for vector we
   13019              :      then have to negate the result of the test, as we're still assuming that if
   13020              :      you take the branch edge that we found the exit condition.  i.e. we need to
   13021              :      know whether we are generating a `forall` or an `exist` condition.  */
   13022       125350 :   bool flipped = flow_bb_inside_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
   13023        62675 :                                         exit_true_edge->dest);
   13024              : 
   13025              :   /* See if we support ADDHN and use that for the reduction.  */
   13026        62675 :   internal_fn ifn = IFN_VEC_TRUNC_ADD_HIGH;
   13027        62675 :   bool addhn_supported_p
   13028        62675 :     = direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_BOTH);
   13029        62675 :   tree narrow_type = NULL_TREE;
   13030        62675 :   if (addhn_supported_p)
   13031              :     {
   13032              :       /* Calculate the narrowing type for the result.  */
   13033            0 :       auto halfprec = TYPE_PRECISION (TREE_TYPE (vectype)) / 2;
   13034            0 :       auto unsignedp = TYPE_UNSIGNED (TREE_TYPE (vectype));
   13035            0 :       tree itype = build_nonstandard_integer_type (halfprec, unsignedp);
   13036            0 :       tree tmp_type = build_vector_type (itype, TYPE_VECTOR_SUBPARTS (vectype));
   13037            0 :       narrow_type = truth_type_for (tmp_type);
   13038              : 
   13039            0 :       if (!supports_vector_compare_and_branch (loop_vinfo,
   13040            0 :                                                TYPE_MODE (narrow_type)))
   13041              :         {
   13042            0 :           if (dump_enabled_p ())
   13043            0 :               dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   13044              :                                "can't use ADDHN reduction because cbranch for "
   13045              :                                "the narrowed type is not supported by the "
   13046              :                                "target.\n");
   13047              :           addhn_supported_p = false;
   13048              :         }
   13049              :     }
   13050              : 
   13051              :   /* Analyze only.  */
   13052        62675 :   if (cost_vec)
   13053              :     {
   13054        61107 :       if (!addhn_supported_p
   13055        61107 :           && !supports_vector_compare_and_branch (loop_vinfo, mode))
   13056              :         {
   13057        58505 :           if (dump_enabled_p ())
   13058          589 :               dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   13059              :                                "can't vectorize early exit because the "
   13060              :                                "target doesn't support flag setting vector "
   13061              :                                "comparisons.\n");
   13062        58505 :           return false;
   13063              :         }
   13064              : 
   13065         2602 :       if (!vectorizable_comparison_1 (loop_vinfo, vectype, stmt_info, code, gsi,
   13066              :                                       slp_node, cost_vec))
   13067              :         return false;
   13068              : 
   13069         2602 :       if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
   13070              :         {
   13071         1544 :           if (direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
   13072              :                                               OPTIMIZE_FOR_SPEED))
   13073            0 :             vect_record_loop_len (loop_vinfo, lens, vec_num, vectype, 1);
   13074              :           else
   13075         1544 :             vect_record_loop_mask (loop_vinfo, masks, vec_num, vectype, NULL);
   13076              :         }
   13077              : 
   13078         2602 :       if (!vect_compute_type_for_early_break_scalar_iv (loop_vinfo))
   13079              :         return false;
   13080              : 
   13081              :       return true;
   13082              :     }
   13083              : 
   13084              :   /* Tranform.  */
   13085              : 
   13086         1568 :   tree new_temp = NULL_TREE;
   13087         1568 :   gimple *new_stmt = NULL;
   13088              : 
   13089         1568 :   if (dump_enabled_p ())
   13090          403 :     dump_printf_loc (MSG_NOTE, vect_location, "transform early-exit.\n");
   13091              : 
   13092              :   /* For SLP we don't do codegen of the body starting from the gcond, the gconds are
   13093              :      roots and so by the time we get to them we have already codegened the SLP tree
   13094              :      and so we shouldn't try to do so again.  The arguments have already been
   13095              :      vectorized.  It's not very clean to do this here, But the masking code below is
   13096              :      complex and this keeps it all in one place to ease fixes and backports.  Once we
   13097              :      drop the non-SLP loop vect or split vectorizable_* this can be simplified.  */
   13098              : 
   13099         1568 :   gimple *stmt = STMT_VINFO_STMT (stmt_info);
   13100         1568 :   basic_block cond_bb = gimple_bb (stmt);
   13101         1568 :   gimple_stmt_iterator  cond_gsi = gsi_last_bb (cond_bb);
   13102              : 
   13103         1568 :   auto_vec<tree> stmts;
   13104         1568 :   stmts.safe_splice (SLP_TREE_VEC_DEFS (slp_node));
   13105              : 
   13106              :   /* If we're comparing against a previous forall we need to negate the resullts
   13107              :      before we do the final comparison or reduction.  */
   13108         1568 :   if (flipped)
   13109              :     {
   13110              :       /* Rewrite the if(all(mask)) into if (!all(mask)) which is the same as
   13111              :          if (any(~mask)) by negating the masks and flipping the branches.
   13112              : 
   13113              :         1. For unmasked loops we simply reduce the ~mask.
   13114              :         2. For masked loops we reduce (~mask & loop_mask) which is the same as
   13115              :            doing (mask & loop_mask) ^ loop_mask.  */
   13116          294 :       for (unsigned i = 0; i < stmts.length (); i++)
   13117              :         {
   13118          173 :           tree inv_lhs = make_temp_ssa_name (vectype, NULL, "vexit_inv");
   13119          173 :           auto inv_stmt = gimple_build_assign (inv_lhs, BIT_NOT_EXPR, stmts[i]);
   13120          173 :           vect_finish_stmt_generation (loop_vinfo, stmt_info, inv_stmt,
   13121              :                                        &cond_gsi);
   13122          173 :           stmts[i] = inv_lhs;
   13123              :         }
   13124              : 
   13125          121 :       EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
   13126          121 :       EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
   13127              :     }
   13128              : 
   13129              :   /* Determine if we need to reduce the final value.  */
   13130         1568 :   if (stmts.length () > 1)
   13131              :     {
   13132              :       /* We build the reductions in a way to maintain as much parallelism as
   13133              :          possible.  */
   13134          141 :       auto_vec<tree> workset (stmts.length ());
   13135              : 
   13136              :       /* Mask the statements as we queue them up.  Normally we loop over
   13137              :          vec_num,  but since we inspect the exact results of vectorization
   13138              :          we don't need to and instead can just use the stmts themselves.  */
   13139          141 :       if (masked_loop_p)
   13140            0 :         for (unsigned i = 0; i < stmts.length (); i++)
   13141              :           {
   13142            0 :             tree stmt_mask
   13143            0 :               = vect_get_loop_mask (loop_vinfo, gsi, masks, vec_num,
   13144              :                                     vectype, i);
   13145            0 :             stmt_mask
   13146            0 :               = prepare_vec_mask (loop_vinfo, TREE_TYPE (stmt_mask), stmt_mask,
   13147            0 :                                   stmts[i], &cond_gsi);
   13148            0 :             workset.quick_push (stmt_mask);
   13149              :           }
   13150          141 :       else if (len_loop_p)
   13151            0 :         for (unsigned i = 0; i < stmts.length (); i++)
   13152              :           {
   13153            0 :             tree len_mask = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi,
   13154              :                                                     lens, vec_num,
   13155            0 :                                                     vectype, stmts[i], i, 1);
   13156              : 
   13157            0 :             workset.quick_push (len_mask);
   13158              :           }
   13159              :       else
   13160          141 :         workset.splice (stmts);
   13161              : 
   13162          430 :       while (workset.length () > 1)
   13163              :         {
   13164          289 :           tree arg0 = workset.pop ();
   13165          289 :           tree arg1 = workset.pop ();
   13166          289 :           if (addhn_supported_p && workset.length () == 0)
   13167              :             {
   13168            0 :               new_stmt = gimple_build_call_internal (ifn, 2, arg0, arg1);
   13169            0 :               vectype_out = narrow_type;
   13170            0 :               new_temp = make_temp_ssa_name (vectype_out, NULL, "vexit_reduc");
   13171            0 :               gimple_call_set_lhs (as_a <gcall *> (new_stmt), new_temp);
   13172            0 :               gimple_call_set_nothrow (as_a <gcall *> (new_stmt), true);
   13173              :             }
   13174              :           else
   13175              :             {
   13176          289 :               new_temp = make_temp_ssa_name (vectype_out, NULL, "vexit_reduc");
   13177          289 :               new_stmt
   13178          289 :                 = gimple_build_assign (new_temp, BIT_IOR_EXPR, arg0, arg1);
   13179              :             }
   13180          289 :           vect_finish_stmt_generation (loop_vinfo, stmt_info, new_stmt,
   13181              :                                        &cond_gsi);
   13182          289 :           workset.quick_insert (0, new_temp);
   13183              :         }
   13184          141 :     }
   13185              :   else
   13186              :     {
   13187         1427 :       new_temp = stmts[0];
   13188         1427 :       if (masked_loop_p)
   13189              :         {
   13190            0 :           tree mask
   13191            0 :             = vect_get_loop_mask (loop_vinfo, gsi, masks, 1, vectype, 0);
   13192            0 :           new_temp = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
   13193              :                                        new_temp, &cond_gsi);
   13194              :         }
   13195         1427 :       else if (len_loop_p)
   13196            0 :         new_temp = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi, lens,
   13197              :                                            1, vectype, new_temp, 0, 1);
   13198              :     }
   13199              : 
   13200         1568 :   gcc_assert (new_temp);
   13201              : 
   13202         1568 :   tree cst = build_zero_cst (vectype_out);
   13203         1568 :   gimple_cond_set_condition (cond_stmt, NE_EXPR, new_temp, cst);
   13204         1568 :   update_stmt (orig_stmt);
   13205              : 
   13206              :   /* ??? */
   13207         1568 :   SLP_TREE_VEC_DEFS (slp_node).truncate (0);
   13208              : 
   13209         1568 :   return true;
   13210         1568 : }
   13211              : 
   13212              : /* If SLP_NODE is nonnull, return true if vectorizable_live_operation
   13213              :    can handle all live statements in the node.  Otherwise return true
   13214              :    if STMT_INFO is not live or if vectorizable_live_operation can handle it.
   13215              :    VEC_STMT_P is as for vectorizable_live_operation.  */
   13216              : 
   13217              : static bool
   13218      1291684 : can_vectorize_live_stmts (vec_info *vinfo,
   13219              :                           slp_tree slp_node, slp_instance slp_node_instance,
   13220              :                           bool vec_stmt_p,
   13221              :                           stmt_vector_for_cost *cost_vec)
   13222              : {
   13223      1291684 :   stmt_vec_info slp_stmt_info;
   13224      1291684 :   unsigned int i;
   13225      2723684 :   FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (slp_node), i, slp_stmt_info)
   13226              :     {
   13227      1432000 :       if (slp_stmt_info
   13228      1415921 :           && STMT_VINFO_LIVE_P (slp_stmt_info)
   13229      1567285 :           && !vectorizable_live_operation (vinfo, slp_stmt_info, slp_node,
   13230              :                                            slp_node_instance, i,
   13231              :                                            vec_stmt_p, cost_vec))
   13232              :         return false;
   13233              :     }
   13234              : 
   13235              :   return true;
   13236              : }
   13237              : 
   13238              : /* Make sure the statement is vectorizable.  */
   13239              : 
   13240              : opt_result
   13241      2696669 : vect_analyze_stmt (vec_info *vinfo,
   13242              :                    slp_tree node, slp_instance node_instance,
   13243              :                    stmt_vector_for_cost *cost_vec)
   13244              : {
   13245      2696669 :   stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
   13246      2696669 :   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
   13247      2696669 :   enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
   13248      2696669 :   bool ok;
   13249              : 
   13250      2696669 :   if (dump_enabled_p ())
   13251       100315 :     dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: %G",
   13252              :                      stmt_info->stmt);
   13253              : 
   13254      5107405 :   if (gimple_has_volatile_ops (stmt_info->stmt))
   13255              :     {
   13256              :       /* ???  This shouldn't really happen, volatile stmts should
   13257              :          not end up in the SLP graph.  */
   13258            0 :       return opt_result::failure_at (stmt_info->stmt,
   13259              :                                      "not vectorized:"
   13260              :                                      " stmt has volatile operands: %G\n",
   13261              :                                      stmt_info->stmt);
   13262              :     }
   13263              : 
   13264              :   /* Skip stmts that do not need to be vectorized.  */
   13265      2696669 :   if (!STMT_VINFO_RELEVANT_P (stmt_info)
   13266            0 :       && !STMT_VINFO_LIVE_P (stmt_info))
   13267              :     {
   13268            0 :       if (dump_enabled_p ())
   13269            0 :         dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
   13270              : 
   13271              :       /* ???  This shouldn't really happen, irrelevant stmts should
   13272              :          not end up in the SLP graph.  */
   13273            0 :       return opt_result::failure_at (stmt_info->stmt,
   13274              :                                      "not vectorized:"
   13275              :                                      " irrelevant stmt as SLP node %p "
   13276              :                                      "representative.\n",
   13277              :                                      (void *)node);
   13278              :     }
   13279              : 
   13280      2696669 :   switch (STMT_VINFO_DEF_TYPE (stmt_info))
   13281              :     {
   13282              :       case vect_internal_def:
   13283              :       case vect_condition_def:
   13284              :         break;
   13285              : 
   13286        83906 :       case vect_reduction_def:
   13287        83906 :       case vect_nested_cycle:
   13288        83906 :          gcc_assert (!bb_vinfo
   13289              :                      && (relevance == vect_used_in_outer
   13290              :                          || relevance == vect_used_in_outer_by_reduction
   13291              :                          || relevance == vect_used_by_reduction
   13292              :                          || relevance == vect_unused_in_scope
   13293              :                          || relevance == vect_used_only_live));
   13294              :          break;
   13295              : 
   13296          312 :       case vect_double_reduction_def:
   13297          312 :         gcc_assert (!bb_vinfo && node);
   13298              :         break;
   13299              : 
   13300       150546 :       case vect_induction_def:
   13301       150546 :       case vect_first_order_recurrence:
   13302       150546 :         gcc_assert (!bb_vinfo);
   13303              :         break;
   13304              : 
   13305            0 :       case vect_constant_def:
   13306            0 :       case vect_external_def:
   13307            0 :       case vect_unknown_def_type:
   13308            0 :       default:
   13309            0 :         gcc_unreachable ();
   13310              :     }
   13311              : 
   13312      2696669 :   tree saved_vectype = STMT_VINFO_VECTYPE (stmt_info);
   13313      2696669 :   STMT_VINFO_VECTYPE (stmt_info) = NULL_TREE;
   13314              : 
   13315      2696669 :   if (STMT_VINFO_RELEVANT_P (stmt_info))
   13316              :     {
   13317      2696669 :       gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
   13318      2696669 :       gcc_assert (SLP_TREE_VECTYPE (node)
   13319              :                   || gimple_code (stmt_info->stmt) == GIMPLE_COND
   13320              :                   || (call && gimple_call_lhs (call) == NULL_TREE));
   13321              :     }
   13322              : 
   13323      2696669 :   ok = true;
   13324      2696669 :   if (bb_vinfo
   13325      1472601 :       || (STMT_VINFO_RELEVANT_P (stmt_info)
   13326            0 :           || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
   13327              :     /* Prefer vectorizable_call over vectorizable_simd_clone_call so
   13328              :        -mveclibabi= takes preference over library functions with
   13329              :        the simd attribute.  */
   13330      2696669 :     ok = (vectorizable_call (vinfo, stmt_info, NULL, node, cost_vec)
   13331      2689652 :           || vectorizable_simd_clone_call (vinfo, stmt_info, NULL, node,
   13332              :                                            cost_vec)
   13333      2689185 :           || vectorizable_conversion (vinfo, stmt_info, NULL, node, cost_vec)
   13334      2605916 :           || vectorizable_operation (vinfo, stmt_info, NULL, node, cost_vec)
   13335      2047037 :           || vectorizable_assignment (vinfo, stmt_info, NULL, node, cost_vec)
   13336      1977621 :           || vectorizable_load (vinfo, stmt_info, NULL, node, cost_vec)
   13337      1539245 :           || vectorizable_store (vinfo, stmt_info, NULL, node, cost_vec)
   13338       721969 :           || vectorizable_shift (vinfo, stmt_info, NULL, node, cost_vec)
   13339       669771 :           || vectorizable_condition (vinfo, stmt_info, NULL, node, cost_vec)
   13340       643441 :           || vectorizable_comparison (vinfo, stmt_info, NULL, node, cost_vec)
   13341       508618 :           || (bb_vinfo
   13342       126297 :               && vectorizable_phi (bb_vinfo, stmt_info, node, cost_vec))
   13343      3147017 :           || (is_a <loop_vec_info> (vinfo)
   13344       382321 :               && (vectorizable_lane_reducing (as_a <loop_vec_info> (vinfo),
   13345              :                                               stmt_info, node, cost_vec)
   13346       381607 :                   || vectorizable_reduction (as_a <loop_vec_info> (vinfo),
   13347              :                                              stmt_info,
   13348              :                                              node, node_instance, cost_vec)
   13349       300186 :                   || vectorizable_induction (as_a <loop_vec_info> (vinfo),
   13350              :                                              stmt_info, node, cost_vec)
   13351       181999 :                   || vectorizable_lc_phi (as_a <loop_vec_info> (vinfo),
   13352              :                                           stmt_info, node)
   13353       181178 :                   || vectorizable_recurr (as_a <loop_vec_info> (vinfo),
   13354              :                                           stmt_info, node, cost_vec)
   13355       180917 :                   || vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
   13356              :                                               stmt_info, NULL, node,
   13357              :                                               cost_vec))));
   13358              : 
   13359      2696669 :   STMT_VINFO_VECTYPE (stmt_info) = saved_vectype;
   13360              : 
   13361      2447725 :   if (!ok)
   13362       248944 :     return opt_result::failure_at (stmt_info->stmt,
   13363              :                                    "not vectorized:"
   13364              :                                    " relevant stmt not supported: %G",
   13365              :                                    stmt_info->stmt);
   13366              : 
   13367              :   /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
   13368              :       need extra handling, except for vectorizable reductions.  */
   13369      2447725 :   if (!bb_vinfo
   13370      1291684 :       && (SLP_TREE_TYPE (node) != lc_phi_info_type
   13371          821 :           || SLP_TREE_DEF_TYPE (node) == vect_internal_def)
   13372      1291684 :       && (!node->ldst_lanes || SLP_TREE_PERMUTE_P (node))
   13373      3739409 :       && !can_vectorize_live_stmts (as_a <loop_vec_info> (vinfo),
   13374              :                                     node, node_instance,
   13375              :                                     false, cost_vec))
   13376            0 :     return opt_result::failure_at (stmt_info->stmt,
   13377              :                                    "not vectorized:"
   13378              :                                    " live stmt not supported: %G",
   13379              :                                    stmt_info->stmt);
   13380              : 
   13381      2447725 :   return opt_result::success ();
   13382              : }
   13383              : 
   13384              : 
   13385              : /* Function vect_transform_stmt.
   13386              : 
   13387              :    Create a vectorized stmt to replace STMT_INFO, and insert it at GSI.  */
   13388              : 
   13389              : bool
   13390       973573 : vect_transform_stmt (vec_info *vinfo,
   13391              :                      stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
   13392              :                      slp_tree slp_node, slp_instance slp_node_instance)
   13393              : {
   13394       973573 :   bool is_store = false;
   13395       973573 :   bool done;
   13396              : 
   13397       973573 :   gcc_assert (slp_node);
   13398              : 
   13399       973573 :   if (stmt_info)
   13400       972732 :     STMT_VINFO_VECTYPE (stmt_info) = NULL_TREE;
   13401              : 
   13402       973573 :   switch (SLP_TREE_TYPE (slp_node))
   13403              :     {
   13404        22820 :     case type_demotion_vec_info_type:
   13405        22820 :     case type_promotion_vec_info_type:
   13406        22820 :     case type_conversion_vec_info_type:
   13407        22820 :       done = vectorizable_conversion (vinfo, stmt_info, gsi, slp_node, NULL);
   13408        22820 :       gcc_assert (done);
   13409              :       break;
   13410              : 
   13411        16281 :     case induc_vec_info_type:
   13412        16281 :       done = vectorizable_induction (as_a <loop_vec_info> (vinfo),
   13413              :                                      stmt_info, slp_node, NULL);
   13414        16281 :       gcc_assert (done);
   13415              :       break;
   13416              : 
   13417         8497 :     case shift_vec_info_type:
   13418         8497 :       done = vectorizable_shift (vinfo, stmt_info, gsi, slp_node, NULL);
   13419         8497 :       gcc_assert (done);
   13420              :       break;
   13421              : 
   13422       114062 :     case op_vec_info_type:
   13423       114062 :       done = vectorizable_operation (vinfo, stmt_info, gsi, slp_node, NULL);
   13424       114062 :       gcc_assert (done);
   13425              :       break;
   13426              : 
   13427        15962 :     case assignment_vec_info_type:
   13428        15962 :       done = vectorizable_assignment (vinfo, stmt_info, gsi, slp_node, NULL);
   13429        15962 :       gcc_assert (done);
   13430              :       break;
   13431              : 
   13432       166233 :     case load_vec_info_type:
   13433       166233 :       done = vectorizable_load (vinfo, stmt_info, gsi, slp_node, NULL);
   13434       166233 :       gcc_assert (done);
   13435              :       break;
   13436              : 
   13437       546897 :     case store_vec_info_type:
   13438       546897 :       done = vectorizable_store (vinfo, stmt_info, gsi, slp_node, NULL);
   13439       546897 :       gcc_assert (done);
   13440              :       is_store = true;
   13441              :       break;
   13442              : 
   13443         8501 :     case condition_vec_info_type:
   13444         8501 :       done = vectorizable_condition (vinfo, stmt_info, gsi, slp_node, NULL);
   13445         8501 :       gcc_assert (done);
   13446              :       break;
   13447              : 
   13448        12450 :     case comparison_vec_info_type:
   13449        12450 :       done = vectorizable_comparison (vinfo, stmt_info, gsi, slp_node, NULL);
   13450        12450 :       gcc_assert (done);
   13451              :       break;
   13452              : 
   13453         4215 :     case call_vec_info_type:
   13454         4215 :       done = vectorizable_call (vinfo, stmt_info, gsi, slp_node, NULL);
   13455         4215 :       break;
   13456              : 
   13457          362 :     case call_simd_clone_vec_info_type:
   13458          362 :       done = vectorizable_simd_clone_call (vinfo, stmt_info, gsi,
   13459              :                                            slp_node, NULL);
   13460          362 :       break;
   13461              : 
   13462         2589 :     case reduc_vec_info_type:
   13463         2589 :       done = vect_transform_reduction (as_a <loop_vec_info> (vinfo), stmt_info,
   13464              :                                        gsi, slp_node);
   13465         2589 :       gcc_assert (done);
   13466              :       break;
   13467              : 
   13468        23484 :     case cycle_phi_info_type:
   13469        23484 :       done = vect_transform_cycle_phi (as_a <loop_vec_info> (vinfo), stmt_info,
   13470              :                                        slp_node, slp_node_instance);
   13471        23484 :       gcc_assert (done);
   13472              :       break;
   13473              : 
   13474          530 :     case lc_phi_info_type:
   13475          530 :       done = vect_transform_lc_phi (as_a <loop_vec_info> (vinfo),
   13476              :                                     stmt_info, slp_node);
   13477          530 :       gcc_assert (done);
   13478              :       break;
   13479              : 
   13480           43 :     case recurr_info_type:
   13481           43 :       done = vectorizable_recurr (as_a <loop_vec_info> (vinfo),
   13482              :                                   stmt_info, slp_node, NULL);
   13483           43 :       gcc_assert (done);
   13484              :       break;
   13485              : 
   13486        14180 :     case phi_info_type:
   13487        14180 :       done = vectorizable_phi (as_a <bb_vec_info> (vinfo),
   13488              :                                stmt_info, slp_node, NULL);
   13489        14180 :       gcc_assert (done);
   13490              :       break;
   13491              : 
   13492            0 :     case loop_exit_ctrl_vec_info_type:
   13493            0 :       done = vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
   13494              :                                       stmt_info, gsi, slp_node, NULL);
   13495            0 :       gcc_assert (done);
   13496              :       break;
   13497              : 
   13498        16467 :     case permute_info_type:
   13499        16467 :       done = vectorizable_slp_permutation (vinfo, gsi, slp_node, NULL);
   13500        16467 :       gcc_assert (done);
   13501              :       break;
   13502              : 
   13503            0 :     default:
   13504            0 :       if (!STMT_VINFO_LIVE_P (stmt_info))
   13505              :         {
   13506            0 :           if (dump_enabled_p ())
   13507            0 :             dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   13508              :                              "stmt not supported.\n");
   13509            0 :           gcc_unreachable ();
   13510              :         }
   13511       973573 :       done = true;
   13512              :     }
   13513              : 
   13514       973573 :   if (SLP_TREE_TYPE (slp_node) != store_vec_info_type
   13515       426676 :       && (!slp_node->ldst_lanes || SLP_TREE_PERMUTE_P (slp_node)))
   13516              :     {
   13517              :       /* Handle stmts whose DEF is used outside the loop-nest that is
   13518              :          being vectorized.  */
   13519       575935 :       for (unsigned lane : SLP_TREE_LIVE_LANES (slp_node))
   13520              :         {
   13521        61629 :           stmt_vec_info slp_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[lane];
   13522        61629 :           done = vectorizable_live_operation (vinfo, slp_stmt_info, slp_node,
   13523              :                                               slp_node_instance, lane,
   13524              :                                               true, NULL);
   13525        61629 :           gcc_assert (done);
   13526              :         }
   13527              :     }
   13528              : 
   13529       973573 :   return is_store;
   13530              : }
   13531              : 
   13532              : 
   13533              : /* Remove a group of stores (for SLP or interleaving), free their
   13534              :    stmt_vec_info.  */
   13535              : 
   13536              : void
   13537            0 : vect_remove_stores (vec_info *vinfo, stmt_vec_info first_stmt_info)
   13538              : {
   13539            0 :   stmt_vec_info next_stmt_info = first_stmt_info;
   13540              : 
   13541            0 :   while (next_stmt_info)
   13542              :     {
   13543            0 :       stmt_vec_info tmp = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
   13544            0 :       next_stmt_info = vect_orig_stmt (next_stmt_info);
   13545              :       /* Free the attached stmt_vec_info and remove the stmt.  */
   13546            0 :       vinfo->remove_stmt (next_stmt_info);
   13547            0 :       next_stmt_info = tmp;
   13548              :     }
   13549            0 : }
   13550              : 
   13551              : /* If NUNITS is nonzero, return a vector type that contains NUNITS
   13552              :    elements of type SCALAR_TYPE, or null if the target doesn't support
   13553              :    such a type.
   13554              : 
   13555              :    If NUNITS is zero, return a vector type that contains elements of
   13556              :    type SCALAR_TYPE, choosing whichever vector size the target prefers.
   13557              : 
   13558              :    If PREVAILING_MODE is VOIDmode, we have not yet chosen a vector mode
   13559              :    for this vectorization region and want to "autodetect" the best choice.
   13560              :    Otherwise, PREVAILING_MODE is a previously-chosen vector TYPE_MODE
   13561              :    and we want the new type to be interoperable with it.   PREVAILING_MODE
   13562              :    in this case can be a scalar integer mode or a vector mode; when it
   13563              :    is a vector mode, the function acts like a tree-level version of
   13564              :    related_vector_mode.  */
   13565              : 
   13566              : tree
   13567     31035223 : get_related_vectype_for_scalar_type (machine_mode prevailing_mode,
   13568              :                                      tree scalar_type, poly_uint64 nunits)
   13569              : {
   13570     31035223 :   tree orig_scalar_type = scalar_type;
   13571     31035223 :   scalar_mode inner_mode;
   13572     31035223 :   machine_mode simd_mode;
   13573     31035223 :   tree vectype;
   13574              : 
   13575     31035223 :   if ((!INTEGRAL_TYPE_P (scalar_type)
   13576     10517973 :        && !POINTER_TYPE_P (scalar_type)
   13577      1788798 :        && !SCALAR_FLOAT_TYPE_P (scalar_type))
   13578     41051463 :       || (!is_int_mode (TYPE_MODE (scalar_type), &inner_mode)
   13579      1287147 :           && !is_float_mode (TYPE_MODE (scalar_type), &inner_mode)))
   13580       504956 :     return NULL_TREE;
   13581              : 
   13582     30530267 :   unsigned int nbytes = GET_MODE_SIZE (inner_mode);
   13583              : 
   13584              :   /* Interoperability between modes requires one to be a constant multiple
   13585              :      of the other, so that the number of vectors required for each operation
   13586              :      is a compile-time constant.  */
   13587     30530267 :   if (prevailing_mode != VOIDmode
   13588     29401120 :       && !constant_multiple_p (nunits * nbytes,
   13589     29401120 :                                GET_MODE_SIZE (prevailing_mode))
   13590     32008486 :       && !constant_multiple_p (GET_MODE_SIZE (prevailing_mode),
   13591      1478219 :                                nunits * nbytes))
   13592              :     return NULL_TREE;
   13593              : 
   13594              :   /* For vector types of elements whose mode precision doesn't
   13595              :      match their types precision we use a element type of mode
   13596              :      precision.  The vectorization routines will have to make sure
   13597              :      they support the proper result truncation/extension.
   13598              :      We also make sure to build vector types with INTEGER_TYPE
   13599              :      component type only.  */
   13600     30530267 :   if (INTEGRAL_TYPE_P (scalar_type)
   13601     51047435 :       && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
   13602     19034543 :           || TREE_CODE (scalar_type) != INTEGER_TYPE))
   13603      1696015 :     scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
   13604      1696015 :                                                   TYPE_UNSIGNED (scalar_type));
   13605              : 
   13606              :   /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
   13607              :      When the component mode passes the above test simply use a type
   13608              :      corresponding to that mode.  The theory is that any use that
   13609              :      would cause problems with this will disable vectorization anyway.  */
   13610     28834252 :   else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
   13611              :            && !INTEGRAL_TYPE_P (scalar_type))
   13612      8729175 :     scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
   13613              : 
   13614              :   /* We can't build a vector type of elements with alignment bigger than
   13615              :      their size.  */
   13616     20105077 :   else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
   13617       377078 :     scalar_type = lang_hooks.types.type_for_mode (inner_mode,
   13618       188539 :                                                   TYPE_UNSIGNED (scalar_type));
   13619              : 
   13620              :   /* If we felt back to using the mode fail if there was
   13621              :      no scalar type for it.  */
   13622     30530267 :   if (scalar_type == NULL_TREE)
   13623              :     return NULL_TREE;
   13624              : 
   13625              :   /* If no prevailing mode was supplied, use the mode the target prefers.
   13626              :      Otherwise lookup a vector mode based on the prevailing mode.  */
   13627     30530267 :   if (prevailing_mode == VOIDmode)
   13628              :     {
   13629      1129147 :       gcc_assert (known_eq (nunits, 0U));
   13630      1129147 :       simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
   13631      1129147 :       if (SCALAR_INT_MODE_P (simd_mode))
   13632              :         {
   13633              :           /* Traditional behavior is not to take the integer mode
   13634              :              literally, but simply to use it as a way of determining
   13635              :              the vector size.  It is up to mode_for_vector to decide
   13636              :              what the TYPE_MODE should be.
   13637              : 
   13638              :              Note that nunits == 1 is allowed in order to support single
   13639              :              element vector types.  */
   13640        58256 :           if (!multiple_p (GET_MODE_SIZE (simd_mode), nbytes, &nunits)
   13641          545 :               || !mode_for_vector (inner_mode, nunits).exists (&simd_mode))
   13642        28583 :             return NULL_TREE;
   13643              :         }
   13644              :     }
   13645     29401120 :   else if (SCALAR_INT_MODE_P (prevailing_mode)
   13646     29401120 :            || !related_vector_mode (prevailing_mode,
   13647     27355946 :                                     inner_mode, nunits).exists (&simd_mode))
   13648              :     {
   13649              :       /* Fall back to using mode_for_vector, mostly in the hope of being
   13650              :          able to use an integer mode.  */
   13651      2045174 :       if (known_eq (nunits, 0U)
   13652      4767333 :           && !multiple_p (GET_MODE_SIZE (prevailing_mode), nbytes, &nunits))
   13653              :         return NULL_TREE;
   13654              : 
   13655       150634 :       if (!mode_for_vector (inner_mode, nunits).exists (&simd_mode))
   13656       140687 :         return NULL_TREE;
   13657              :     }
   13658              : 
   13659     28466457 :   vectype = build_vector_type_for_mode (scalar_type, simd_mode);
   13660              : 
   13661              :   /* In cases where the mode was chosen by mode_for_vector, check that
   13662              :      the target actually supports the chosen mode, or that it at least
   13663              :      allows the vector mode to be replaced by a like-sized integer.  */
   13664     56932914 :   if (!VECTOR_MODE_P (TYPE_MODE (vectype))
   13665     28476661 :       && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
   13666              :     return NULL_TREE;
   13667              : 
   13668              :   /* Re-attach the address-space qualifier if we canonicalized the scalar
   13669              :      type.  */
   13670     28458414 :   if (TYPE_ADDR_SPACE (orig_scalar_type) != TYPE_ADDR_SPACE (vectype))
   13671            5 :     return build_qualified_type
   13672            5 :              (vectype, KEEP_QUAL_ADDR_SPACE (TYPE_QUALS (orig_scalar_type)));
   13673              : 
   13674              :   return vectype;
   13675              : }
   13676              : 
   13677              : /* Function get_vectype_for_scalar_type.
   13678              : 
   13679              :    Returns the vector type corresponding to SCALAR_TYPE as supported
   13680              :    by the target.  If GROUP_SIZE is nonzero and we're performing BB
   13681              :    vectorization, make sure that the number of elements in the vector
   13682              :    is no bigger than GROUP_SIZE.  */
   13683              : 
   13684              : tree
   13685     26553877 : get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type,
   13686              :                              unsigned int group_size)
   13687              : {
   13688              :   /* For BB vectorization, we should always have a group size once we've
   13689              :      constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
   13690              :      are tentative requests during things like early data reference
   13691              :      analysis and pattern recognition.  */
   13692     26553877 :   if (is_a <bb_vec_info> (vinfo))
   13693     23645700 :     gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
   13694              :   else
   13695              :     group_size = 0;
   13696              : 
   13697     26553877 :   tree vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
   13698              :                                                       scalar_type);
   13699     26553877 :   if (vectype && vinfo->vector_mode == VOIDmode)
   13700      1054134 :     vinfo->vector_mode = TYPE_MODE (vectype);
   13701              : 
   13702              :   /* Register the natural choice of vector type, before the group size
   13703              :      has been applied.  */
   13704            0 :   if (vectype)
   13705     24139661 :     vinfo->used_vector_modes.add (TYPE_MODE (vectype));
   13706              : 
   13707              :   /* If the natural choice of vector type doesn't satisfy GROUP_SIZE,
   13708              :      try again with an explicit number of elements.  */
   13709     24139661 :   if (vectype
   13710     24139661 :       && group_size
   13711     26553877 :       && maybe_ge (TYPE_VECTOR_SUBPARTS (vectype), group_size))
   13712              :     {
   13713              :       /* Start with the biggest number of units that fits within
   13714              :          GROUP_SIZE and halve it until we find a valid vector type.
   13715              :          Usually either the first attempt will succeed or all will
   13716              :          fail (in the latter case because GROUP_SIZE is too small
   13717              :          for the target), but it's possible that a target could have
   13718              :          a hole between supported vector types.
   13719              : 
   13720              :          If GROUP_SIZE is not a power of 2, this has the effect of
   13721              :          trying the largest power of 2 that fits within the group,
   13722              :          even though the group is not a multiple of that vector size.
   13723              :          The BB vectorizer will then try to carve up the group into
   13724              :          smaller pieces.  */
   13725      3048923 :       unsigned int nunits = 1 << floor_log2 (group_size);
   13726      3048923 :       do
   13727              :         {
   13728      3048923 :           vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
   13729      3048923 :                                                          scalar_type, nunits);
   13730      3048923 :           nunits /= 2;
   13731              :         }
   13732      3048923 :       while (nunits > 1 && !vectype);
   13733              :     }
   13734              : 
   13735     26553877 :   return vectype;
   13736              : }
   13737              : 
   13738              : /* Return the vector type corresponding to SCALAR_TYPE as supported
   13739              :    by the target.  NODE, if nonnull, is the SLP tree node that will
   13740              :    use the returned vector type.  */
   13741              : 
   13742              : tree
   13743       167897 : get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type, slp_tree node)
   13744              : {
   13745       167897 :   unsigned int group_size = 0;
   13746       167897 :   if (node)
   13747       167897 :     group_size = SLP_TREE_LANES (node);
   13748       167897 :   return get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
   13749              : }
   13750              : 
   13751              : /* Function get_mask_type_for_scalar_type.
   13752              : 
   13753              :    Returns the mask type corresponding to a result of comparison
   13754              :    of vectors of specified SCALAR_TYPE as supported by target.
   13755              :    If GROUP_SIZE is nonzero and we're performing BB vectorization,
   13756              :    make sure that the number of elements in the vector is no bigger
   13757              :    than GROUP_SIZE.  */
   13758              : 
   13759              : tree
   13760      1100996 : get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
   13761              :                                unsigned int group_size)
   13762              : {
   13763      1100996 :   tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
   13764              : 
   13765      1100996 :   if (!vectype)
   13766              :     return NULL;
   13767              : 
   13768      1078964 :   return truth_type_for (vectype);
   13769              : }
   13770              : 
   13771              : /* Function get_mask_type_for_scalar_type.
   13772              : 
   13773              :    Returns the mask type corresponding to a result of comparison
   13774              :    of vectors of specified SCALAR_TYPE as supported by target.
   13775              :    NODE, if nonnull, is the SLP tree node that will use the returned
   13776              :    vector type.  */
   13777              : 
   13778              : tree
   13779           19 : get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
   13780              :                                slp_tree node)
   13781              : {
   13782           19 :   tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, node);
   13783              : 
   13784           19 :   if (!vectype)
   13785              :     return NULL;
   13786              : 
   13787           19 :   return truth_type_for (vectype);
   13788              : }
   13789              : 
   13790              : /* Function get_same_sized_vectype
   13791              : 
   13792              :    Returns a vector type corresponding to SCALAR_TYPE of size
   13793              :    VECTOR_TYPE if supported by the target.  */
   13794              : 
   13795              : tree
   13796       158316 : get_same_sized_vectype (tree scalar_type, tree vector_type)
   13797              : {
   13798       158316 :   if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
   13799            0 :     return truth_type_for (vector_type);
   13800              : 
   13801       158316 :   poly_uint64 nunits;
   13802       316632 :   if (!multiple_p (GET_MODE_SIZE (TYPE_MODE (vector_type)),
   13803       316632 :                    GET_MODE_SIZE (TYPE_MODE (scalar_type)), &nunits))
   13804              :     return NULL_TREE;
   13805              : 
   13806       158316 :   return get_related_vectype_for_scalar_type (TYPE_MODE (vector_type),
   13807       158316 :                                               scalar_type, nunits);
   13808              : }
   13809              : 
   13810              : /* Return true if replacing LOOP_VINFO->vector_mode with VECTOR_MODE
   13811              :    would not change the chosen vector modes.  */
   13812              : 
   13813              : bool
   13814      1583998 : vect_chooses_same_modes_p (vec_info *vinfo, machine_mode vector_mode)
   13815              : {
   13816      1583998 :   for (vec_info::mode_set::iterator i = vinfo->used_vector_modes.begin ();
   13817      3608462 :        i != vinfo->used_vector_modes.end (); ++i)
   13818      1851201 :     if (!VECTOR_MODE_P (*i)
   13819      5553603 :         || related_vector_mode (vector_mode, GET_MODE_INNER (*i), 0) != *i)
   13820       838969 :       return false;
   13821       745029 :   return true;
   13822              : }
   13823              : 
   13824              : /* Return true if replacing VECTOR_MODE with ALT_VECTOR_MODE would not
   13825              :    change the chosen vector modes for analysis of a loop.  */
   13826              : 
   13827              : bool
   13828       383763 : vect_chooses_same_modes_p (machine_mode vector_mode,
   13829              :                            machine_mode alt_vector_mode)
   13830              : {
   13831        63349 :   return (VECTOR_MODE_P (vector_mode)
   13832       383763 :           && VECTOR_MODE_P (alt_vector_mode)
   13833       767526 :           && (related_vector_mode (vector_mode,
   13834              :                                    GET_MODE_INNER (alt_vector_mode))
   13835       383763 :               == alt_vector_mode)
   13836       410041 :           && (related_vector_mode (alt_vector_mode,
   13837              :                                    GET_MODE_INNER (vector_mode))
   13838        13139 :               == vector_mode));
   13839              : }
   13840              : 
   13841              : /* Function vect_is_simple_use.
   13842              : 
   13843              :    Input:
   13844              :    VINFO - the vect info of the loop or basic block that is being vectorized.
   13845              :    OPERAND - operand in the loop or bb.
   13846              :    Output:
   13847              :    DEF_STMT_INFO_OUT (optional) - information about the defining stmt in
   13848              :      case OPERAND is an SSA_NAME that is defined in the vectorizable region
   13849              :    DEF_STMT_OUT (optional) - the defining stmt in case OPERAND is an SSA_NAME;
   13850              :      the definition could be anywhere in the function
   13851              :    DT - the type of definition
   13852              : 
   13853              :    Returns whether a stmt with OPERAND can be vectorized.
   13854              :    For loops, supportable operands are constants, loop invariants, and operands
   13855              :    that are defined by the current iteration of the loop.  Unsupportable
   13856              :    operands are those that are defined by a previous iteration of the loop (as
   13857              :    is the case in reduction/induction computations).
   13858              :    For basic blocks, supportable operands are constants and bb invariants.
   13859              :    For now, operands defined outside the basic block are not supported.  */
   13860              : 
   13861              : bool
   13862     41867795 : vect_is_simple_use (tree operand, vec_info *vinfo, enum vect_def_type *dt,
   13863              :                     stmt_vec_info *def_stmt_info_out, gimple **def_stmt_out)
   13864              : {
   13865     41867795 :   if (def_stmt_info_out)
   13866     39635709 :     *def_stmt_info_out = NULL;
   13867     41867795 :   if (def_stmt_out)
   13868      9803883 :     *def_stmt_out = NULL;
   13869     41867795 :   *dt = vect_unknown_def_type;
   13870              : 
   13871     41867795 :   if (dump_enabled_p ())
   13872              :     {
   13873       763317 :       dump_printf_loc (MSG_NOTE, vect_location,
   13874              :                        "vect_is_simple_use: operand ");
   13875       763317 :       if (TREE_CODE (operand) == SSA_NAME
   13876       763317 :           && !SSA_NAME_IS_DEFAULT_DEF (operand))
   13877       700395 :         dump_gimple_expr (MSG_NOTE, TDF_SLIM, SSA_NAME_DEF_STMT (operand), 0);
   13878              :       else
   13879        62922 :         dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
   13880              :     }
   13881              : 
   13882     41867795 :   if (CONSTANT_CLASS_P (operand))
   13883      2805448 :     *dt = vect_constant_def;
   13884     39062347 :   else if (is_gimple_min_invariant (operand))
   13885       333335 :     *dt = vect_external_def;
   13886     38729012 :   else if (TREE_CODE (operand) != SSA_NAME)
   13887          995 :     *dt = vect_unknown_def_type;
   13888     38728017 :   else if (SSA_NAME_IS_DEFAULT_DEF (operand))
   13889       507344 :     *dt = vect_external_def;
   13890              :   else
   13891              :     {
   13892     38220673 :       gimple *def_stmt = SSA_NAME_DEF_STMT (operand);
   13893     38220673 :       stmt_vec_info stmt_vinfo = vinfo->lookup_def (operand);
   13894     38220673 :       if (!stmt_vinfo)
   13895       838285 :         *dt = vect_external_def;
   13896              :       else
   13897              :         {
   13898     37382388 :           stmt_vinfo = vect_stmt_to_vectorize (stmt_vinfo);
   13899     37382388 :           def_stmt = stmt_vinfo->stmt;
   13900     37382388 :           *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
   13901     37382388 :           if (def_stmt_info_out)
   13902     35159314 :             *def_stmt_info_out = stmt_vinfo;
   13903              :         }
   13904     38220673 :       if (def_stmt_out)
   13905      9596012 :         *def_stmt_out = def_stmt;
   13906              :     }
   13907              : 
   13908     41867795 :   if (dump_enabled_p ())
   13909              :     {
   13910       763317 :       dump_printf (MSG_NOTE, ", type of def: ");
   13911       763317 :       switch (*dt)
   13912              :         {
   13913            0 :         case vect_uninitialized_def:
   13914            0 :           dump_printf (MSG_NOTE, "uninitialized\n");
   13915            0 :           break;
   13916        52226 :         case vect_constant_def:
   13917        52226 :           dump_printf (MSG_NOTE, "constant\n");
   13918        52226 :           break;
   13919        26191 :         case vect_external_def:
   13920        26191 :           dump_printf (MSG_NOTE, "external\n");
   13921        26191 :           break;
   13922       545904 :         case vect_internal_def:
   13923       545904 :           dump_printf (MSG_NOTE, "internal\n");
   13924       545904 :           break;
   13925       107941 :         case vect_induction_def:
   13926       107941 :           dump_printf (MSG_NOTE, "induction\n");
   13927       107941 :           break;
   13928        27708 :         case vect_reduction_def:
   13929        27708 :           dump_printf (MSG_NOTE, "reduction\n");
   13930        27708 :           break;
   13931          482 :         case vect_double_reduction_def:
   13932          482 :           dump_printf (MSG_NOTE, "double reduction\n");
   13933          482 :           break;
   13934         2173 :         case vect_nested_cycle:
   13935         2173 :           dump_printf (MSG_NOTE, "nested cycle\n");
   13936         2173 :           break;
   13937          264 :         case vect_first_order_recurrence:
   13938          264 :           dump_printf (MSG_NOTE, "first order recurrence\n");
   13939          264 :           break;
   13940            0 :         case vect_condition_def:
   13941            0 :           dump_printf (MSG_NOTE, "control flow\n");
   13942            0 :           break;
   13943          428 :         case vect_unknown_def_type:
   13944          428 :           dump_printf (MSG_NOTE, "unknown\n");
   13945          428 :           break;
   13946              :         }
   13947              :     }
   13948              : 
   13949     41867795 :   if (*dt == vect_unknown_def_type)
   13950              :     {
   13951        57230 :       if (dump_enabled_p ())
   13952          428 :         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
   13953              :                          "Unsupported pattern.\n");
   13954        57230 :       return false;
   13955              :     }
   13956              : 
   13957              :   return true;
   13958              : }
   13959              : 
   13960              : /* Function vect_is_simple_use.
   13961              : 
   13962              :    Same as vect_is_simple_use but determines the operand by operand
   13963              :    position OPERAND from either STMT or SLP_NODE, filling in *OP
   13964              :    and *SLP_DEF (when SLP_NODE is not NULL).  */
   13965              : 
   13966              : bool
   13967      3896925 : vect_is_simple_use (vec_info *vinfo, slp_tree slp_node,
   13968              :                     unsigned operand, tree *op, slp_tree *slp_def,
   13969              :                     enum vect_def_type *dt,
   13970              :                     tree *vectype, stmt_vec_info *def_stmt_info_out)
   13971              : {
   13972      3896925 :   slp_tree child = SLP_TREE_CHILDREN (slp_node)[operand];
   13973      3896925 :   *slp_def = child;
   13974      3896925 :   *vectype = SLP_TREE_VECTYPE (child);
   13975      3896925 :   if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
   13976              :     {
   13977              :       /* ???  VEC_PERM nodes might be intermediate and their lane value
   13978              :          have no representative (nor do we build a VEC_PERM stmt for
   13979              :          the actual operation).  Note for two-operator nodes we set
   13980              :          a representative but leave scalar stmts empty as we'd only
   13981              :          have one for a subset of lanes.  Ideally no caller would
   13982              :          require *op for internal defs.  */
   13983      2164678 :       if (SLP_TREE_REPRESENTATIVE (child))
   13984              :         {
   13985      2163873 :           *op = gimple_get_lhs (SLP_TREE_REPRESENTATIVE (child)->stmt);
   13986      2163873 :           return vect_is_simple_use (*op, vinfo, dt, def_stmt_info_out);
   13987              :         }
   13988              :       else
   13989              :         {
   13990          805 :           gcc_assert (SLP_TREE_PERMUTE_P (child));
   13991          805 :           *op = error_mark_node;
   13992          805 :           *dt = vect_internal_def;
   13993          805 :           if (def_stmt_info_out)
   13994            0 :             *def_stmt_info_out = NULL;
   13995          805 :           return true;
   13996              :         }
   13997              :     }
   13998              :   else
   13999              :     {
   14000      1732247 :       if (def_stmt_info_out)
   14001        57875 :         *def_stmt_info_out = NULL;
   14002      1732247 :       *op = SLP_TREE_SCALAR_OPS (child)[0];
   14003      1732247 :       *dt = SLP_TREE_DEF_TYPE (child);
   14004      1732247 :       return true;
   14005              :     }
   14006              : }
   14007              : 
   14008              : /* If OP is not NULL and is external or constant update its vector
   14009              :    type with VECTYPE.  Returns true if successful or false if not,
   14010              :    for example when conflicting vector types are present.  */
   14011              : 
   14012              : bool
   14013      3569518 : vect_maybe_update_slp_op_vectype (slp_tree op, tree vectype)
   14014              : {
   14015      3569518 :   if (!op || SLP_TREE_DEF_TYPE (op) == vect_internal_def)
   14016              :     return true;
   14017      1166309 :   if (SLP_TREE_VECTYPE (op))
   14018       108584 :     return types_compatible_p (SLP_TREE_VECTYPE (op), vectype);
   14019              :   /* For external defs refuse to produce VECTOR_BOOLEAN_TYPE_P, those
   14020              :      should be handled by patters.  Allow vect_constant_def for now
   14021              :      as well as the trivial single-lane uniform vect_external_def case
   14022              :      both of which we code-generate reasonably.  */
   14023      1057725 :   if (VECTOR_BOOLEAN_TYPE_P (vectype)
   14024         1540 :       && SLP_TREE_DEF_TYPE (op) == vect_external_def
   14025      1058877 :       && SLP_TREE_LANES (op) > 1)
   14026              :     return false;
   14027      1057545 :   SLP_TREE_VECTYPE (op) = vectype;
   14028      1057545 :   return true;
   14029              : }
   14030              : 
   14031              : /* Function supportable_widening_operation
   14032              : 
   14033              :    Check whether an operation represented by the code CODE is a
   14034              :    widening operation that is supported by the target platform in
   14035              :    vector form (i.e., when operating on arguments of type VECTYPE_IN
   14036              :    producing a result of type VECTYPE_OUT).
   14037              : 
   14038              :    Widening operations we currently support are NOP (CONVERT), FLOAT,
   14039              :    FIX_TRUNC and WIDEN_MULT.  This function checks if these operations
   14040              :    are supported by the target platform either directly (via vector
   14041              :    tree-codes), or via target builtins.
   14042              : 
   14043              :    When EVENODD_OK then also lane-swizzling operations are considered.
   14044              : 
   14045              :    Output:
   14046              :    - CODE1 and CODE2 are codes of vector operations to be used when
   14047              :    vectorizing the operation, if available.
   14048              :    - MULTI_STEP_CVT determines the number of required intermediate steps in
   14049              :    case of multi-step conversion (like char->short->int - in that case
   14050              :    MULTI_STEP_CVT will be 1).
   14051              :    - INTERM_TYPES contains the intermediate type required to perform the
   14052              :    widening operation (short in the above example).  */
   14053              : 
   14054              : bool
   14055       484798 : supportable_widening_operation (code_helper code,
   14056              :                                 tree vectype_out, tree vectype_in,
   14057              :                                 bool evenodd_ok,
   14058              :                                 code_helper *code1,
   14059              :                                 code_helper *code2,
   14060              :                                 int *multi_step_cvt,
   14061              :                                 vec<tree> *interm_types)
   14062              : {
   14063       484798 :   machine_mode vec_mode;
   14064       484798 :   enum insn_code icode1, icode2;
   14065       484798 :   optab optab1 = unknown_optab, optab2 = unknown_optab;
   14066       484798 :   tree vectype = vectype_in;
   14067       484798 :   tree wide_vectype = vectype_out;
   14068       484798 :   tree_code c1 = MAX_TREE_CODES, c2 = MAX_TREE_CODES;
   14069       484798 :   int i;
   14070       484798 :   tree prev_type, intermediate_type;
   14071       484798 :   machine_mode intermediate_mode, prev_mode;
   14072       484798 :   optab optab3, optab4;
   14073              : 
   14074       484798 :   *multi_step_cvt = 0;
   14075              : 
   14076       484798 :   switch (code.safe_as_tree_code ())
   14077              :     {
   14078              :     case MAX_TREE_CODES:
   14079              :       /* Don't set c1 and c2 if code is not a tree_code.  */
   14080              :       break;
   14081              : 
   14082       186416 :     case WIDEN_MULT_EXPR:
   14083              :       /* The result of a vectorized widening operation usually requires
   14084              :          two vectors (because the widened results do not fit into one vector).
   14085              :          The generated vector results would normally be expected to be
   14086              :          generated in the same order as in the original scalar computation,
   14087              :          i.e. if 8 results are generated in each vector iteration, they are
   14088              :          to be organized as follows:
   14089              :                 vect1: [res1,res2,res3,res4],
   14090              :                 vect2: [res5,res6,res7,res8].
   14091              : 
   14092              :          However, in the special case that the result of the widening
   14093              :          operation is used in a reduction computation only, the order doesn't
   14094              :          matter (because when vectorizing a reduction we change the order of
   14095              :          the computation).  Some targets can take advantage of this and
   14096              :          generate more efficient code.  For example, targets like Altivec,
   14097              :          that support widen_mult using a sequence of {mult_even,mult_odd}
   14098              :          generate the following vectors:
   14099              :                 vect1: [res1,res3,res5,res7],
   14100              :                 vect2: [res2,res4,res6,res8].
   14101              : 
   14102              :          When vectorizing outer-loops, we execute the inner-loop sequentially
   14103              :          (each vectorized inner-loop iteration contributes to VF outer-loop
   14104              :          iterations in parallel).  We therefore don't allow to change the
   14105              :          order of the computation in the inner-loop during outer-loop
   14106              :          vectorization.  */
   14107              :       /* TODO: Another case in which order doesn't *really* matter is when we
   14108              :          widen and then contract again, e.g. (short)((int)x * y >> 8).
   14109              :          Normally, pack_trunc performs an even/odd permute, whereas the
   14110              :          repack from an even/odd expansion would be an interleave, which
   14111              :          would be significantly simpler for e.g. AVX2.  */
   14112              :       /* In any case, in order to avoid duplicating the code below, recurse
   14113              :          on VEC_WIDEN_MULT_EVEN_EXPR.  If it succeeds, all the return values
   14114              :          are properly set up for the caller.  If we fail, we'll continue with
   14115              :          a VEC_WIDEN_MULT_LO/HI_EXPR check.  */
   14116       186416 :       if (evenodd_ok
   14117       186416 :           && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
   14118              :                                              vectype_out, vectype_in,
   14119              :                                              evenodd_ok, code1,
   14120              :                                              code2, multi_step_cvt,
   14121              :                                              interm_types))
   14122        97755 :         return true;
   14123              :       c1 = VEC_WIDEN_MULT_LO_EXPR;
   14124              :       c2 = VEC_WIDEN_MULT_HI_EXPR;
   14125              :       break;
   14126              : 
   14127              :     case DOT_PROD_EXPR:
   14128       387043 :       c1 = DOT_PROD_EXPR;
   14129       387043 :       c2 = DOT_PROD_EXPR;
   14130              :       break;
   14131              : 
   14132            0 :     case SAD_EXPR:
   14133            0 :       c1 = SAD_EXPR;
   14134            0 :       c2 = SAD_EXPR;
   14135            0 :       break;
   14136              : 
   14137       184464 :     case VEC_WIDEN_MULT_EVEN_EXPR:
   14138              :       /* Support the recursion induced just above.  */
   14139       184464 :       c1 = VEC_WIDEN_MULT_EVEN_EXPR;
   14140       184464 :       c2 = VEC_WIDEN_MULT_ODD_EXPR;
   14141       184464 :       break;
   14142              : 
   14143         9425 :     case WIDEN_LSHIFT_EXPR:
   14144         9425 :       c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
   14145         9425 :       c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
   14146         9425 :       break;
   14147              : 
   14148        40870 :     CASE_CONVERT:
   14149        40870 :       c1 = VEC_UNPACK_LO_EXPR;
   14150        40870 :       c2 = VEC_UNPACK_HI_EXPR;
   14151        40870 :       break;
   14152              : 
   14153         9176 :     case FLOAT_EXPR:
   14154         9176 :       c1 = VEC_UNPACK_FLOAT_LO_EXPR;
   14155         9176 :       c2 = VEC_UNPACK_FLOAT_HI_EXPR;
   14156         9176 :       break;
   14157              : 
   14158          119 :     case FIX_TRUNC_EXPR:
   14159          119 :       c1 = VEC_UNPACK_FIX_TRUNC_LO_EXPR;
   14160          119 :       c2 = VEC_UNPACK_FIX_TRUNC_HI_EXPR;
   14161          119 :       break;
   14162              : 
   14163            0 :     default:
   14164            0 :       gcc_unreachable ();
   14165              :     }
   14166              : 
   14167       387043 :   if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
   14168              :     std::swap (c1, c2);
   14169              : 
   14170       387043 :   if (code == FIX_TRUNC_EXPR)
   14171              :     {
   14172              :       /* The signedness is determined from output operand.  */
   14173          119 :       optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
   14174          119 :       optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
   14175              :     }
   14176       687052 :   else if (CONVERT_EXPR_CODE_P (code.safe_as_tree_code ())
   14177        40870 :            && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
   14178         8032 :            && VECTOR_BOOLEAN_TYPE_P (vectype)
   14179         8032 :            && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
   14180       333060 :            && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
   14181              :     {
   14182              :       /* If the input and result modes are the same, a different optab
   14183              :          is needed where we pass in the number of units in vectype.  */
   14184              :       optab1 = vec_unpacks_sbool_lo_optab;
   14185              :       optab2 = vec_unpacks_sbool_hi_optab;
   14186              :     }
   14187              : 
   14188       387043 :   vec_mode = TYPE_MODE (vectype);
   14189       387043 :   if (widening_fn_p (code))
   14190              :      {
   14191              :        /* If this is an internal fn then we must check whether the target
   14192              :           supports either a low-high split or an even-odd split.  */
   14193        54328 :       internal_fn ifn = as_internal_fn ((combined_fn) code);
   14194              : 
   14195        54328 :       internal_fn lo, hi, even, odd;
   14196        54328 :       lookup_hilo_internal_fn (ifn, &lo, &hi);
   14197        54328 :       if (BYTES_BIG_ENDIAN)
   14198              :         std::swap (lo, hi);
   14199        54328 :       *code1 = as_combined_fn (lo);
   14200        54328 :       *code2 = as_combined_fn (hi);
   14201        54328 :       optab1 = direct_internal_fn_optab (lo, {vectype, vectype});
   14202        54328 :       optab2 = direct_internal_fn_optab (hi, {vectype, vectype});
   14203              : 
   14204              :       /* If we don't support low-high, then check for even-odd.  */
   14205        54328 :       if (!optab1
   14206        54328 :           || (icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
   14207            0 :           || !optab2
   14208        54328 :           || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
   14209              :         {
   14210        54328 :           lookup_evenodd_internal_fn (ifn, &even, &odd);
   14211        54328 :           *code1 = as_combined_fn (even);
   14212        54328 :           *code2 = as_combined_fn (odd);
   14213        54328 :           optab1 = direct_internal_fn_optab (even, {vectype, vectype});
   14214        54328 :           optab2 = direct_internal_fn_optab (odd, {vectype, vectype});
   14215              :         }
   14216              :     }
   14217       332715 :   else if (code.is_tree_code ())
   14218              :     {
   14219       332715 :       if (code == FIX_TRUNC_EXPR)
   14220              :         {
   14221              :           /* The signedness is determined from output operand.  */
   14222          119 :           optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
   14223          119 :           optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
   14224              :         }
   14225       332596 :       else if (CONVERT_EXPR_CODE_P ((tree_code) code.safe_as_tree_code ())
   14226        40870 :                && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
   14227         8032 :                && VECTOR_BOOLEAN_TYPE_P (vectype)
   14228         8032 :                && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
   14229       333060 :                && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
   14230              :         {
   14231              :           /* If the input and result modes are the same, a different optab
   14232              :              is needed where we pass in the number of units in vectype.  */
   14233              :           optab1 = vec_unpacks_sbool_lo_optab;
   14234              :           optab2 = vec_unpacks_sbool_hi_optab;
   14235              :         }
   14236              :       else
   14237              :         {
   14238       332132 :           optab1 = optab_for_tree_code (c1, vectype, optab_default);
   14239       332132 :           optab2 = optab_for_tree_code (c2, vectype, optab_default);
   14240              :         }
   14241       332715 :       *code1 = c1;
   14242       332715 :       *code2 = c2;
   14243              :     }
   14244              : 
   14245       387043 :   if (!optab1 || !optab2)
   14246              :     return false;
   14247              : 
   14248       387043 :   if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
   14249       387043 :        || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
   14250       230346 :     return false;
   14251              : 
   14252              : 
   14253       156697 :   if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
   14254       156697 :       && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
   14255              :     {
   14256       145318 :       if (!VECTOR_BOOLEAN_TYPE_P (vectype))
   14257              :         return true;
   14258              :       /* For scalar masks we may have different boolean
   14259              :          vector types having the same QImode.  Thus we
   14260              :          add additional check for elements number.  */
   14261         4245 :       if (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
   14262              :                     TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
   14263              :         return true;
   14264              :     }
   14265              : 
   14266              :   /* Check if it's a multi-step conversion that can be done using intermediate
   14267              :      types.  */
   14268              : 
   14269        11584 :   prev_type = vectype;
   14270        11584 :   prev_mode = vec_mode;
   14271              : 
   14272       242215 :   if (!CONVERT_EXPR_CODE_P (code.safe_as_tree_code ()))
   14273              :     return false;
   14274              : 
   14275              :   /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
   14276              :      intermediate steps in promotion sequence.  We try
   14277              :      MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
   14278              :      not.  */
   14279        11532 :   interm_types->create (MAX_INTERM_CVT_STEPS);
   14280        12922 :   for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
   14281              :     {
   14282        12922 :       intermediate_mode = insn_data[icode1].operand[0].mode;
   14283        12922 :       if (VECTOR_BOOLEAN_TYPE_P (prev_type))
   14284         4795 :         intermediate_type
   14285         4795 :           = vect_halve_mask_nunits (prev_type, intermediate_mode);
   14286         8127 :       else if (VECTOR_MODE_P (intermediate_mode))
   14287              :         {
   14288         8127 :           tree intermediate_element_type
   14289         8127 :             = lang_hooks.types.type_for_mode (GET_MODE_INNER (intermediate_mode),
   14290         8127 :                                               TYPE_UNSIGNED (prev_type));
   14291         8127 :           intermediate_type
   14292         8127 :             = build_vector_type_for_mode (intermediate_element_type,
   14293              :                                           intermediate_mode);
   14294         8127 :         }
   14295              :       else
   14296            0 :         intermediate_type
   14297            0 :           = lang_hooks.types.type_for_mode (intermediate_mode,
   14298            0 :                                             TYPE_UNSIGNED (prev_type));
   14299              : 
   14300        12922 :       if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
   14301         4795 :           && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
   14302         4795 :           && intermediate_mode == TYPE_MODE (wide_vectype)
   14303        13191 :           && SCALAR_INT_MODE_P (intermediate_mode))
   14304              :         {
   14305              :           /* If the input and result modes are the same, a different optab
   14306              :              is needed where we pass in the number of units in vectype.  */
   14307              :           optab3 = vec_unpacks_sbool_lo_optab;
   14308              :           optab4 = vec_unpacks_sbool_hi_optab;
   14309              :         }
   14310              :       else
   14311              :         {
   14312        12653 :           optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
   14313        12653 :           optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
   14314              :         }
   14315              : 
   14316        12922 :       if (!optab3 || !optab4
   14317        12922 :           || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
   14318        12890 :           || insn_data[icode1].operand[0].mode != intermediate_mode
   14319        12890 :           || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
   14320        12890 :           || insn_data[icode2].operand[0].mode != intermediate_mode
   14321        12890 :           || ((icode1 = optab_handler (optab3, intermediate_mode))
   14322              :               == CODE_FOR_nothing)
   14323        25559 :           || ((icode2 = optab_handler (optab4, intermediate_mode))
   14324              :               == CODE_FOR_nothing))
   14325              :         break;
   14326              : 
   14327        12637 :       interm_types->quick_push (intermediate_type);
   14328        12637 :       (*multi_step_cvt)++;
   14329              : 
   14330        12637 :       if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
   14331        12637 :           && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
   14332              :         {
   14333        11311 :           if (!VECTOR_BOOLEAN_TYPE_P (vectype))
   14334              :             return true;
   14335         3785 :           if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type),
   14336              :                         TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
   14337              :             return true;
   14338              :         }
   14339              : 
   14340         1390 :       prev_type = intermediate_type;
   14341         1390 :       prev_mode = intermediate_mode;
   14342              :     }
   14343              : 
   14344          285 :   interm_types->release ();
   14345          285 :   return false;
   14346              : }
   14347              : 
   14348              : 
   14349              : /* Function supportable_narrowing_operation
   14350              : 
   14351              :    Check whether an operation represented by the code CODE is a
   14352              :    narrowing operation that is supported by the target platform in
   14353              :    vector form (i.e., when operating on arguments of type VECTYPE_IN
   14354              :    and producing a result of type VECTYPE_OUT).
   14355              : 
   14356              :    Narrowing operations we currently support are NOP (CONVERT), FIX_TRUNC
   14357              :    and FLOAT.  This function checks if these operations are supported by
   14358              :    the target platform directly via vector tree-codes.
   14359              : 
   14360              :    Output:
   14361              :    - CODE1 is the code of a vector operation to be used when
   14362              :    vectorizing the operation, if available.
   14363              :    - MULTI_STEP_CVT determines the number of required intermediate steps in
   14364              :    case of multi-step conversion (like int->short->char - in that case
   14365              :    MULTI_STEP_CVT will be 1).
   14366              :    - INTERM_TYPES contains the intermediate type required to perform the
   14367              :    narrowing operation (short in the above example).   */
   14368              : 
   14369              : bool
   14370        42008 : supportable_narrowing_operation (code_helper code,
   14371              :                                  tree vectype_out, tree vectype_in,
   14372              :                                  code_helper *code1, int *multi_step_cvt,
   14373              :                                  vec<tree> *interm_types)
   14374              : {
   14375        42008 :   machine_mode vec_mode;
   14376        42008 :   enum insn_code icode1;
   14377        42008 :   optab optab1, interm_optab;
   14378        42008 :   tree vectype = vectype_in;
   14379        42008 :   tree narrow_vectype = vectype_out;
   14380        42008 :   enum tree_code c1;
   14381        42008 :   tree intermediate_type, prev_type;
   14382        42008 :   machine_mode intermediate_mode, prev_mode;
   14383        42008 :   int i;
   14384        42008 :   unsigned HOST_WIDE_INT n_elts;
   14385        42008 :   bool uns;
   14386              : 
   14387        42008 :   if (!code.is_tree_code ())
   14388              :     return false;
   14389              : 
   14390        42008 :   *multi_step_cvt = 0;
   14391        42008 :   switch ((tree_code) code)
   14392              :     {
   14393        41164 :     CASE_CONVERT:
   14394        41164 :       c1 = VEC_PACK_TRUNC_EXPR;
   14395        41164 :       if (VECTOR_BOOLEAN_TYPE_P (narrow_vectype)
   14396        11662 :           && VECTOR_BOOLEAN_TYPE_P (vectype)
   14397        11662 :           && SCALAR_INT_MODE_P (TYPE_MODE (vectype))
   14398         5264 :           && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&n_elts)
   14399        46428 :           && n_elts < BITS_PER_UNIT)
   14400              :         optab1 = vec_pack_sbool_trunc_optab;
   14401              :       else
   14402        38679 :         optab1 = optab_for_tree_code (c1, vectype, optab_default);
   14403              :       break;
   14404              : 
   14405          561 :     case FIX_TRUNC_EXPR:
   14406          561 :       c1 = VEC_PACK_FIX_TRUNC_EXPR;
   14407              :       /* The signedness is determined from output operand.  */
   14408          561 :       optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
   14409          561 :       break;
   14410              : 
   14411          283 :     case FLOAT_EXPR:
   14412          283 :       c1 = VEC_PACK_FLOAT_EXPR;
   14413          283 :       optab1 = optab_for_tree_code (c1, vectype, optab_default);
   14414          283 :       break;
   14415              : 
   14416            0 :     default:
   14417            0 :       gcc_unreachable ();
   14418              :     }
   14419              : 
   14420        42008 :   if (!optab1)
   14421              :     return false;
   14422              : 
   14423        42008 :   vec_mode = TYPE_MODE (vectype);
   14424        42008 :   if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
   14425              :     return false;
   14426              : 
   14427        37741 :   *code1 = c1;
   14428              : 
   14429        37741 :   if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
   14430              :     {
   14431        23544 :       if (!VECTOR_BOOLEAN_TYPE_P (vectype))
   14432              :         return true;
   14433              :       /* For scalar masks we may have different boolean
   14434              :          vector types having the same QImode.  Thus we
   14435              :          add additional check for elements number.  */
   14436         5821 :       if (known_eq (TYPE_VECTOR_SUBPARTS (vectype) * 2,
   14437              :                     TYPE_VECTOR_SUBPARTS (narrow_vectype)))
   14438              :         return true;
   14439              :     }
   14440              : 
   14441        14348 :   if (code == FLOAT_EXPR)
   14442              :     return false;
   14443              : 
   14444              :   /* Check if it's a multi-step conversion that can be done using intermediate
   14445              :      types.  */
   14446        14348 :   prev_mode = vec_mode;
   14447        14348 :   prev_type = vectype;
   14448        14348 :   if (code == FIX_TRUNC_EXPR)
   14449           94 :     uns = TYPE_UNSIGNED (vectype_out);
   14450              :   else
   14451        14254 :     uns = TYPE_UNSIGNED (vectype);
   14452              : 
   14453              :   /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
   14454              :      conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
   14455              :      costly than signed.  */
   14456        14348 :   if (code == FIX_TRUNC_EXPR && uns)
   14457              :     {
   14458           28 :       enum insn_code icode2;
   14459              : 
   14460           28 :       intermediate_type
   14461           28 :         = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
   14462           28 :       interm_optab
   14463           28 :         = optab_for_tree_code (c1, intermediate_type, optab_default);
   14464           28 :       if (interm_optab != unknown_optab
   14465           28 :           && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
   14466           28 :           && insn_data[icode1].operand[0].mode
   14467           28 :              == insn_data[icode2].operand[0].mode)
   14468              :         {
   14469              :           uns = false;
   14470              :           optab1 = interm_optab;
   14471              :           icode1 = icode2;
   14472              :         }
   14473              :     }
   14474              : 
   14475              :   /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
   14476              :      intermediate steps in promotion sequence.  We try
   14477              :      MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not.  */
   14478        14348 :   interm_types->create (MAX_INTERM_CVT_STEPS);
   14479        30844 :   for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
   14480              :     {
   14481        16496 :       intermediate_mode = insn_data[icode1].operand[0].mode;
   14482        16496 :       if (VECTOR_BOOLEAN_TYPE_P (prev_type))
   14483         7186 :         intermediate_type
   14484         7186 :           = vect_double_mask_nunits (prev_type, intermediate_mode);
   14485              :       else
   14486         9310 :         intermediate_type
   14487         9310 :           = lang_hooks.types.type_for_mode (intermediate_mode, uns);
   14488        16496 :       if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
   14489         7186 :           && VECTOR_BOOLEAN_TYPE_P (prev_type)
   14490         7186 :           && SCALAR_INT_MODE_P (prev_mode)
   14491         3134 :           && TYPE_VECTOR_SUBPARTS (intermediate_type).is_constant (&n_elts)
   14492        19630 :           && n_elts < BITS_PER_UNIT)
   14493              :         interm_optab = vec_pack_sbool_trunc_optab;
   14494              :       else
   14495        16142 :         interm_optab
   14496        16142 :           = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
   14497              :                                  optab_default);
   14498          354 :       if (!interm_optab
   14499        16496 :           || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
   14500        16496 :           || insn_data[icode1].operand[0].mode != intermediate_mode
   14501        32638 :           || ((icode1 = optab_handler (interm_optab, intermediate_mode))
   14502              :               == CODE_FOR_nothing))
   14503              :         break;
   14504              : 
   14505        15581 :       interm_types->quick_push (intermediate_type);
   14506        15581 :       (*multi_step_cvt)++;
   14507              : 
   14508        15581 :       if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
   14509              :         {
   14510        13433 :           if (!VECTOR_BOOLEAN_TYPE_P (vectype))
   14511              :             return true;
   14512         5008 :           if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type) * 2,
   14513              :                         TYPE_VECTOR_SUBPARTS (narrow_vectype)))
   14514              :             return true;
   14515              :         }
   14516              : 
   14517         2148 :       prev_mode = intermediate_mode;
   14518         2148 :       prev_type = intermediate_type;
   14519         2148 :       optab1 = interm_optab;
   14520              :     }
   14521              : 
   14522          915 :   interm_types->release ();
   14523          915 :   return false;
   14524              : }
   14525              : 
   14526              : /* Function supportable_indirect_convert_operation
   14527              : 
   14528              :    Check whether an operation represented by the code CODE is single or multi
   14529              :    operations that are supported by the target platform in
   14530              :    vector form (i.e., when operating on arguments of type VECTYPE_IN
   14531              :    producing a result of type VECTYPE_OUT).
   14532              : 
   14533              :    Convert operations we currently support directly are FIX_TRUNC and FLOAT.
   14534              :    This function checks if these operations are supported
   14535              :    by the target platform directly (via vector tree-codes).
   14536              : 
   14537              :    Output:
   14538              :    - converts contains some pairs to perform the convert operation,
   14539              :    the pair's first is the intermediate type, and its second is the code of
   14540              :    a vector operation to be used when converting the operation from the
   14541              :    previous type to the intermediate type. */
   14542              : bool
   14543        86209 : supportable_indirect_convert_operation (code_helper code,
   14544              :                                         tree vectype_out,
   14545              :                                         tree vectype_in,
   14546              :                                         vec<std::pair<tree, tree_code> > &converts,
   14547              :                                         tree op0, slp_tree slp_op0)
   14548              : {
   14549        86209 :   bool found_mode = false;
   14550        86209 :   scalar_mode lhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_out));
   14551        86209 :   scalar_mode rhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_in));
   14552        86209 :   tree_code tc1, tc2, code1, code2;
   14553              : 
   14554        86209 :   tree cvt_type = NULL_TREE;
   14555        86209 :   poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (vectype_in);
   14556              : 
   14557        86209 :   if (supportable_convert_operation ((tree_code) code,
   14558              :                                      vectype_out,
   14559              :                                      vectype_in,
   14560              :                                      &tc1))
   14561              :     {
   14562        19268 :       converts.safe_push (std::make_pair (vectype_out, tc1));
   14563        19268 :       return true;
   14564              :     }
   14565              : 
   14566              :   /* For conversions between float and integer types try whether
   14567              :      we can use intermediate signed integer types to support the
   14568              :      conversion.  */
   14569       133882 :   if (GET_MODE_SIZE (lhs_mode) != GET_MODE_SIZE (rhs_mode)
   14570        66941 :       && (code == FLOAT_EXPR
   14571         3196 :           || (code == FIX_TRUNC_EXPR && !flag_trapping_math)))
   14572              :     {
   14573          476 :       bool demotion = GET_MODE_SIZE (rhs_mode) > GET_MODE_SIZE (lhs_mode);
   14574          238 :       bool float_expr_p = code == FLOAT_EXPR;
   14575          238 :       unsigned short target_size;
   14576          238 :       scalar_mode intermediate_mode;
   14577          238 :       if (demotion)
   14578              :         {
   14579           84 :           intermediate_mode = lhs_mode;
   14580           84 :           target_size = GET_MODE_SIZE (rhs_mode);
   14581              :         }
   14582              :       else
   14583              :         {
   14584          154 :           target_size = GET_MODE_SIZE (lhs_mode);
   14585          154 :           if (!int_mode_for_size
   14586          154 :               (GET_MODE_BITSIZE (rhs_mode), 0).exists (&intermediate_mode))
   14587          132 :             return false;
   14588              :         }
   14589          238 :       code1 = float_expr_p ? (tree_code) code : NOP_EXPR;
   14590              :       code2 = float_expr_p ? NOP_EXPR : (tree_code) code;
   14591          238 :       opt_scalar_mode mode_iter;
   14592          417 :       FOR_EACH_2XWIDER_MODE (mode_iter, intermediate_mode)
   14593              :         {
   14594          417 :           intermediate_mode = mode_iter.require ();
   14595              : 
   14596          834 :           if (GET_MODE_SIZE (intermediate_mode) > target_size)
   14597              :             break;
   14598              : 
   14599          349 :           scalar_mode cvt_mode;
   14600          349 :           if (!int_mode_for_size
   14601          349 :               (GET_MODE_BITSIZE (intermediate_mode), 0).exists (&cvt_mode))
   14602              :             break;
   14603              : 
   14604          319 :           cvt_type = build_nonstandard_integer_type
   14605          319 :             (GET_MODE_BITSIZE (cvt_mode), 0);
   14606              : 
   14607              :           /* Check if the intermediate type can hold OP0's range.
   14608              :              When converting from float to integer this is not necessary
   14609              :              because values that do not fit the (smaller) target type are
   14610              :              unspecified anyway.  */
   14611          319 :           if (demotion && float_expr_p)
   14612              :             {
   14613            8 :               wide_int op_min_value, op_max_value;
   14614              :               /* For vector form, it looks like op0 doesn't have RANGE_INFO.
   14615              :                  In the future, if it is supported, changes may need to be made
   14616              :                  to this part, such as checking the RANGE of each element
   14617              :                  in the vector.  */
   14618            8 :               if (slp_op0)
   14619              :                 {
   14620            4 :                   tree def;
   14621              :                   /* ???  Merge ranges in case of more than one lane.  */
   14622            4 :                   if (SLP_TREE_LANES (slp_op0) != 1
   14623            0 :                       || !(def = vect_get_slp_scalar_def (slp_op0, 0))
   14624            4 :                       || !vect_get_range_info (def,
   14625              :                                                &op_min_value, &op_max_value))
   14626              :                     break;
   14627              :                 }
   14628            4 :               else if (!op0
   14629            0 :                        || TREE_CODE (op0) != SSA_NAME
   14630            0 :                        || !SSA_NAME_RANGE_INFO (op0)
   14631            4 :                        || !vect_get_range_info (op0, &op_min_value,
   14632              :                                                 &op_max_value))
   14633              :                 break;
   14634              : 
   14635            0 :               if (cvt_type == NULL_TREE
   14636            0 :                   || (wi::min_precision (op_max_value, SIGNED)
   14637            0 :                       > TYPE_PRECISION (cvt_type))
   14638            0 :                   || (wi::min_precision (op_min_value, SIGNED)
   14639            0 :                       > TYPE_PRECISION (cvt_type)))
   14640            0 :                 continue;
   14641            8 :             }
   14642              : 
   14643          311 :           cvt_type = get_related_vectype_for_scalar_type (TYPE_MODE (vectype_in),
   14644              :                                                           cvt_type,
   14645              :                                                           nelts);
   14646              :           /* This should only happened for SLP as long as loop vectorizer
   14647              :              only supports same-sized vector.  */
   14648          490 :           if (cvt_type == NULL_TREE
   14649          443 :               || maybe_ne (TYPE_VECTOR_SUBPARTS (cvt_type), nelts)
   14650          311 :               || !supportable_convert_operation ((tree_code) code1,
   14651              :                                                  vectype_out,
   14652              :                                                  cvt_type, &tc1)
   14653          521 :               || !supportable_convert_operation ((tree_code) code2,
   14654              :                                                  cvt_type,
   14655              :                                                  vectype_in, &tc2))
   14656          179 :             continue;
   14657              : 
   14658              :           found_mode = true;
   14659              :           break;
   14660              :         }
   14661              : 
   14662          238 :       if (found_mode)
   14663              :         {
   14664          132 :           converts.safe_push (std::make_pair (cvt_type, tc2));
   14665          132 :           if (TYPE_MODE (cvt_type) != TYPE_MODE (vectype_out))
   14666          132 :             converts.safe_push (std::make_pair (vectype_out, tc1));
   14667          132 :           return true;
   14668              :         }
   14669              :     }
   14670              :   return false;
   14671              : }
   14672              : 
   14673              : /* Generate and return a vector mask of MASK_TYPE such that
   14674              :    mask[I] is true iff J + START_INDEX < END_INDEX for all J <= I.
   14675              :    Add the statements to SEQ.  */
   14676              : 
   14677              : tree
   14678            0 : vect_gen_while (gimple_seq *seq, tree mask_type, tree start_index,
   14679              :                 tree end_index, const char *name)
   14680              : {
   14681            0 :   tree cmp_type = TREE_TYPE (start_index);
   14682            0 :   gcc_checking_assert (direct_internal_fn_supported_p (IFN_WHILE_ULT,
   14683              :                                                        cmp_type, mask_type,
   14684              :                                                        OPTIMIZE_FOR_SPEED));
   14685            0 :   gcall *call = gimple_build_call_internal (IFN_WHILE_ULT, 3,
   14686              :                                             start_index, end_index,
   14687              :                                             build_zero_cst (mask_type));
   14688            0 :   tree tmp;
   14689            0 :   if (name)
   14690            0 :     tmp = make_temp_ssa_name (mask_type, NULL, name);
   14691              :   else
   14692            0 :     tmp = make_ssa_name (mask_type);
   14693            0 :   gimple_call_set_lhs (call, tmp);
   14694            0 :   gimple_seq_add_stmt (seq, call);
   14695            0 :   return tmp;
   14696              : }
   14697              : 
   14698              : /* Generate a vector mask of type MASK_TYPE for which index I is false iff
   14699              :    J + START_INDEX < END_INDEX for all J <= I.  Add the statements to SEQ.  */
   14700              : 
   14701              : tree
   14702            0 : vect_gen_while_not (gimple_seq *seq, tree mask_type, tree start_index,
   14703              :                     tree end_index)
   14704              : {
   14705            0 :   tree tmp = vect_gen_while (seq, mask_type, start_index, end_index);
   14706            0 :   return gimple_build (seq, BIT_NOT_EXPR, mask_type, tmp);
   14707              : }
   14708              : 
   14709              : /* Try to compute the vector types required to vectorize STMT_INFO,
   14710              :    returning true on success and false if vectorization isn't possible.
   14711              :    If GROUP_SIZE is nonzero and we're performing BB vectorization,
   14712              :    take sure that the number of elements in the vectors is no bigger
   14713              :    than GROUP_SIZE.
   14714              : 
   14715              :    On success:
   14716              : 
   14717              :    - Set *STMT_VECTYPE_OUT to:
   14718              :      - NULL_TREE if the statement doesn't need to be vectorized;
   14719              :      - the equivalent of STMT_VINFO_VECTYPE otherwise.
   14720              : 
   14721              :    - Set *NUNITS_VECTYPE_OUT to the vector type that contains the maximum
   14722              :      number of units needed to vectorize STMT_INFO, or NULL_TREE if the
   14723              :      statement does not help to determine the overall number of units.  */
   14724              : 
   14725              : opt_result
   14726      5762971 : vect_get_vector_types_for_stmt (vec_info *vinfo, stmt_vec_info stmt_info,
   14727              :                                 tree *stmt_vectype_out,
   14728              :                                 tree *nunits_vectype_out,
   14729              :                                 unsigned int group_size)
   14730              : {
   14731      5762971 :   gimple *stmt = stmt_info->stmt;
   14732              : 
   14733              :   /* For BB vectorization, we should always have a group size once we've
   14734              :      constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
   14735              :      are tentative requests during things like early data reference
   14736              :      analysis and pattern recognition.  */
   14737      5762971 :   if (is_a <bb_vec_info> (vinfo))
   14738      4512247 :     gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
   14739              :   else
   14740              :     group_size = 0;
   14741              : 
   14742      5762971 :   *stmt_vectype_out = NULL_TREE;
   14743      5762971 :   *nunits_vectype_out = NULL_TREE;
   14744              : 
   14745      5762971 :   if (gimple_get_lhs (stmt) == NULL_TREE
   14746              :       /* Allow vector conditionals through here.  */
   14747         2737 :       && !is_a <gcond *> (stmt)
   14748              :       /* MASK_STORE and friends have no lhs, but are ok.  */
   14749      5768425 :       && !(is_gimple_call (stmt)
   14750         2737 :            && gimple_call_internal_p (stmt)
   14751         2717 :            && internal_store_fn_p (gimple_call_internal_fn (stmt))))
   14752              :     {
   14753           20 :       if (is_a <gcall *> (stmt))
   14754              :         {
   14755              :           /* Ignore calls with no lhs.  These must be calls to
   14756              :              #pragma omp simd functions, and what vectorization factor
   14757              :              it really needs can't be determined until
   14758              :              vectorizable_simd_clone_call.  */
   14759           20 :           if (dump_enabled_p ())
   14760           18 :             dump_printf_loc (MSG_NOTE, vect_location,
   14761              :                              "defer to SIMD clone analysis.\n");
   14762           20 :           return opt_result::success ();
   14763              :         }
   14764              : 
   14765            0 :       return opt_result::failure_at (stmt,
   14766              :                                      "not vectorized: irregular stmt: %G", stmt);
   14767              :     }
   14768              : 
   14769      5762951 :   tree vectype;
   14770      5762951 :   tree scalar_type = NULL_TREE;
   14771      5762951 :   if (group_size == 0 && STMT_VINFO_VECTYPE (stmt_info))
   14772              :     {
   14773      1578238 :       vectype = STMT_VINFO_VECTYPE (stmt_info);
   14774      1578238 :       if (dump_enabled_p ())
   14775        79359 :         dump_printf_loc (MSG_NOTE, vect_location,
   14776              :                          "precomputed vectype: %T\n", vectype);
   14777              :     }
   14778      4184713 :   else if (vect_use_mask_type_p (stmt_info))
   14779              :     {
   14780       193890 :       unsigned int precision = stmt_info->mask_precision;
   14781       193890 :       scalar_type = build_nonstandard_integer_type (precision, 1);
   14782       193890 :       vectype = get_mask_type_for_scalar_type (vinfo, scalar_type, group_size);
   14783       193890 :       if (!vectype)
   14784            0 :         return opt_result::failure_at (stmt, "not vectorized: unsupported"
   14785              :                                        " data-type %T\n", scalar_type);
   14786       193890 :       if (dump_enabled_p ())
   14787         4709 :         dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
   14788              :     }
   14789              :   else
   14790              :     {
   14791              :       /* If we got here with a gcond it means that the target had no available vector
   14792              :          mode for the scalar type.  We can't vectorize so abort.  */
   14793      3990823 :       if (is_a <gcond *> (stmt))
   14794            0 :         return opt_result::failure_at (stmt,
   14795              :                                        "not vectorized:"
   14796              :                                        " unsupported data-type for gcond %T\n",
   14797              :                                        scalar_type);
   14798              : 
   14799      3990823 :       if (data_reference *dr = STMT_VINFO_DATA_REF (stmt_info))
   14800      1460212 :         scalar_type = TREE_TYPE (DR_REF (dr));
   14801              :       else
   14802      2530611 :         scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
   14803              : 
   14804      3990823 :       if (dump_enabled_p ())
   14805              :         {
   14806        61914 :           if (group_size)
   14807         7539 :             dump_printf_loc (MSG_NOTE, vect_location,
   14808              :                              "get vectype for scalar type (group size %d):"
   14809              :                              " %T\n", group_size, scalar_type);
   14810              :           else
   14811        54375 :             dump_printf_loc (MSG_NOTE, vect_location,
   14812              :                              "get vectype for scalar type: %T\n", scalar_type);
   14813              :         }
   14814      3990823 :       vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
   14815      3990823 :       if (!vectype)
   14816       207224 :         return opt_result::failure_at (stmt,
   14817              :                                        "not vectorized:"
   14818              :                                        " unsupported data-type %T\n",
   14819              :                                        scalar_type);
   14820              : 
   14821      3783599 :       if (dump_enabled_p ())
   14822        61715 :         dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
   14823              :     }
   14824              : 
   14825      4056848 :   if (scalar_type && VECTOR_MODE_P (TYPE_MODE (scalar_type)))
   14826            0 :     return opt_result::failure_at (stmt,
   14827              :                                    "not vectorized: vector stmt in loop:%G",
   14828              :                                    stmt);
   14829              : 
   14830      5555727 :   *stmt_vectype_out = vectype;
   14831              : 
   14832              :   /* Don't try to compute scalar types if the stmt produces a boolean
   14833              :      vector; use the existing vector type instead.  */
   14834      5555727 :   tree nunits_vectype = vectype;
   14835      5555727 :   if (!VECTOR_BOOLEAN_TYPE_P (vectype))
   14836              :     {
   14837              :       /* The number of units is set according to the smallest scalar
   14838              :          type (or the largest vector size, but we only support one
   14839              :          vector size per vectorization).  */
   14840      5040953 :       scalar_type = vect_get_smallest_scalar_type (stmt_info,
   14841      5040953 :                                                    TREE_TYPE (vectype));
   14842      5040953 :       if (!types_compatible_p (scalar_type, TREE_TYPE (vectype)))
   14843              :         {
   14844       995417 :           if (dump_enabled_p ())
   14845         9768 :             dump_printf_loc (MSG_NOTE, vect_location,
   14846              :                              "get vectype for smallest scalar type: %T\n",
   14847              :                              scalar_type);
   14848       995417 :           nunits_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
   14849              :                                                         group_size);
   14850       995417 :           if (!nunits_vectype)
   14851           10 :             return opt_result::failure_at
   14852           10 :               (stmt, "not vectorized: unsupported data-type %T\n",
   14853              :                scalar_type);
   14854       995407 :           if (dump_enabled_p ())
   14855         9768 :             dump_printf_loc (MSG_NOTE, vect_location, "nunits vectype: %T\n",
   14856              :                              nunits_vectype);
   14857              :         }
   14858              :     }
   14859              : 
   14860      5555717 :   if (!multiple_p (TYPE_VECTOR_SUBPARTS (nunits_vectype),
   14861      5555717 :                    TYPE_VECTOR_SUBPARTS (*stmt_vectype_out)))
   14862            0 :     return opt_result::failure_at (stmt,
   14863              :                                    "Not vectorized: Incompatible number "
   14864              :                                    "of vector subparts between %T and %T\n",
   14865              :                                    nunits_vectype, *stmt_vectype_out);
   14866              : 
   14867      5555717 :   if (dump_enabled_p ())
   14868              :     {
   14869       145783 :       dump_printf_loc (MSG_NOTE, vect_location, "nunits = ");
   14870       145783 :       dump_dec (MSG_NOTE, TYPE_VECTOR_SUBPARTS (nunits_vectype));
   14871       145783 :       dump_printf (MSG_NOTE, "\n");
   14872              :     }
   14873              : 
   14874      5555717 :   *nunits_vectype_out = nunits_vectype;
   14875      5555717 :   return opt_result::success ();
   14876              : }
   14877              : 
   14878              : /* Generate and return statement sequence that sets vector length LEN that is:
   14879              : 
   14880              :    min_of_start_and_end = min (START_INDEX, END_INDEX);
   14881              :    left_len = END_INDEX - min_of_start_and_end;
   14882              :    rhs = min (left_len, LEN_LIMIT);
   14883              :    LEN = rhs;
   14884              : 
   14885              :    Note: the cost of the code generated by this function is modeled
   14886              :    by vect_estimate_min_profitable_iters, so changes here may need
   14887              :    corresponding changes there.  */
   14888              : 
   14889              : gimple_seq
   14890            0 : vect_gen_len (tree len, tree start_index, tree end_index, tree len_limit)
   14891              : {
   14892            0 :   gimple_seq stmts = NULL;
   14893            0 :   tree len_type = TREE_TYPE (len);
   14894            0 :   gcc_assert (TREE_TYPE (start_index) == len_type);
   14895              : 
   14896            0 :   tree min = gimple_build (&stmts, MIN_EXPR, len_type, start_index, end_index);
   14897            0 :   tree left_len = gimple_build (&stmts, MINUS_EXPR, len_type, end_index, min);
   14898            0 :   tree rhs = gimple_build (&stmts, MIN_EXPR, len_type, left_len, len_limit);
   14899            0 :   gimple* stmt = gimple_build_assign (len, rhs);
   14900            0 :   gimple_seq_add_stmt (&stmts, stmt);
   14901              : 
   14902            0 :   return stmts;
   14903              : }
   14904              : 
        

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