LCOV - code coverage report
Current view: top level - gcc - tree-vect-generic.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 88.4 % 1286 1137
Test Date: 2026-12-05 16:10:02 Functions: 97.5 % 40 39
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Lower vector operations to scalar operations.
       2              :    Copyright (C) 2004-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify it
       7              : under the terms of the GNU General Public License as published by the
       8              : Free Software Foundation; either version 3, or (at your option) any
       9              : later version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful, but WITHOUT
      12              : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14              : for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : #include "config.h"
      21              : #include "system.h"
      22              : #include "coretypes.h"
      23              : #include "backend.h"
      24              : #include "rtl.h"
      25              : #include "tree.h"
      26              : #include "gimple.h"
      27              : #include "tree-pass.h"
      28              : #include "ssa.h"
      29              : #include "expmed.h"
      30              : #include "optabs-tree.h"
      31              : #include "diagnostic.h"
      32              : #include "fold-const.h"
      33              : #include "stor-layout.h"
      34              : #include "langhooks.h"
      35              : #include "tree-eh.h"
      36              : #include "gimple-iterator.h"
      37              : #include "gimplify-me.h"
      38              : #include "gimplify.h"
      39              : #include "tree-cfg.h"
      40              : #include "tree-vector-builder.h"
      41              : #include "vec-perm-indices.h"
      42              : #include "insn-config.h"
      43              : #include "gimple-fold.h"
      44              : #include "gimple-match.h"
      45              : #include "recog.h"            /* FIXME: for insn_data */
      46              : #include "optabs-libfuncs.h"
      47              : #include "cfgloop.h"
      48              : #include "tree-vectorizer.h"
      49              : 
      50              : 
      51              : /* Build a ternary operation and gimplify it.  Emit code before GSI.
      52              :    Return the gimple_val holding the result.  */
      53              : 
      54              : static tree
      55        12579 : gimplify_build3 (gimple_stmt_iterator *gsi, enum tree_code code,
      56              :                  tree type, tree a, tree b, tree c)
      57              : {
      58        12579 :   location_t loc = gimple_location (gsi_stmt (*gsi));
      59        12579 :   return gimple_build (gsi, true, GSI_SAME_STMT, loc, code, type, a, b, c);
      60              : }
      61              : 
      62              : /* Build a binary operation and gimplify it.  Emit code before GSI.
      63              :    Return the gimple_val holding the result.  */
      64              : 
      65              : static tree
      66        22192 : gimplify_build2 (gimple_stmt_iterator *gsi, enum tree_code code,
      67              :                  tree type, tree a, tree b)
      68              : {
      69        22192 :   location_t loc = gimple_location (gsi_stmt (*gsi));
      70        22192 :   return gimple_build (gsi, true, GSI_SAME_STMT, loc, code, type, a, b);
      71              : }
      72              : 
      73              : /* Build a unary operation and gimplify it.  Emit code before GSI.
      74              :    Return the gimple_val holding the result.  */
      75              : 
      76              : static tree
      77        20161 : gimplify_build1 (gimple_stmt_iterator *gsi, enum tree_code code, tree type,
      78              :                  tree a)
      79              : {
      80        20161 :   location_t loc = gimple_location (gsi_stmt (*gsi));
      81        20161 :   return gimple_build (gsi, true, GSI_SAME_STMT, loc, code, type, a);
      82              : }
      83              : 
      84              : 
      85              : /* Return the number of elements in a vector type TYPE that we have
      86              :    already decided needs to be expanded piecewise.  We don't support
      87              :    this kind of expansion for variable-length vectors, since we should
      88              :    always check for target support before introducing uses of those.  */
      89              : static unsigned int
      90       141124 : nunits_for_known_piecewise_op (const_tree type)
      91              : {
      92       140127 :   return TYPE_VECTOR_SUBPARTS (type).to_constant ();
      93              : }
      94              : 
      95              : /* Return true if TYPE1 has more elements than TYPE2, where either
      96              :    type may be a vector or a scalar.  */
      97              : 
      98              : static inline bool
      99          300 : subparts_gt (tree type1, tree type2)
     100              : {
     101          300 :   poly_uint64 n1 = VECTOR_TYPE_P (type1) ? TYPE_VECTOR_SUBPARTS (type1) : 1;
     102          300 :   poly_uint64 n2 = VECTOR_TYPE_P (type2) ? TYPE_VECTOR_SUBPARTS (type2) : 1;
     103          300 :   return known_gt (n1, n2);
     104              : }
     105              : 
     106              : static GTY(()) tree vector_inner_type;
     107              : static GTY(()) tree vector_last_type;
     108              : static GTY(()) int vector_last_nunits;
     109              : 
     110              : /* Return a suitable vector types made of SUBPARTS units each of mode
     111              :    "word_mode" (the global variable).  */
     112              : static tree
     113           52 : build_word_mode_vector_type (int nunits)
     114              : {
     115           52 :   if (!vector_inner_type)
     116           40 :     vector_inner_type = lang_hooks.types.type_for_mode (word_mode, 1);
     117           12 :   else if (vector_last_nunits == nunits)
     118              :     {
     119           10 :       gcc_assert (TREE_CODE (vector_last_type) == VECTOR_TYPE);
     120              :       return vector_last_type;
     121              :     }
     122              : 
     123           42 :   vector_last_nunits = nunits;
     124           42 :   vector_last_type = build_vector_type (vector_inner_type, nunits);
     125           42 :   return vector_last_type;
     126              : }
     127              : 
     128              : typedef tree (*elem_op_func) (gimple_stmt_iterator *,
     129              :                               tree, tree, tree, tree, tree, enum tree_code,
     130              :                               tree);
     131              : 
     132              : /* Extract the vector element of type TYPE at BITPOS with BITSIZE from T
     133              :    and return it.  */
     134              : 
     135              : tree
     136        77766 : tree_vec_extract (gimple_stmt_iterator *gsi, tree type,
     137              :                   tree t, tree bitsize, tree bitpos)
     138              : {
     139              :   /* We're using the resimplify API and maybe_push_res_to_seq to
     140              :      simplify the BIT_FIELD_REF but restrict the simplification to
     141              :      a single stmt while at the same time following SSA edges for
     142              :      simplification with already emitted CTORs.  */
     143        77766 :   gimple_match_op opr;
     144        77766 :   opr.set_op (BIT_FIELD_REF, type, t, bitsize, bitpos);
     145        77766 :   opr.resimplify (NULL, follow_all_ssa_edges);
     146        77766 :   gimple_seq stmts = NULL;
     147        77766 :   tree res = maybe_push_res_to_seq (&opr, &stmts);
     148        77766 :   if (!res)
     149              :     {
     150              :       /* This can happen if SSA_NAME_OCCURS_IN_ABNORMAL_PHI are
     151              :          used.  Build BIT_FIELD_REF manually otherwise.  */
     152           64 :       t = build3 (BIT_FIELD_REF, type, t, bitsize, bitpos);
     153           64 :       res = make_ssa_name (type);
     154           64 :       gimple *g = gimple_build_assign (res, t);
     155           64 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
     156           64 :       return res;
     157              :     }
     158        77702 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
     159        77702 :   return res;
     160              : }
     161              : 
     162              : static tree
     163          638 : do_unop (gimple_stmt_iterator *gsi, tree inner_type, tree a,
     164              :          tree b ATTRIBUTE_UNUSED, tree bitpos, tree bitsize,
     165              :          enum tree_code code, tree type ATTRIBUTE_UNUSED)
     166              : {
     167          638 :   tree rhs_type = inner_type;
     168              : 
     169              :   /* For ABSU_EXPR, use the signed type for the rhs if the rhs was signed. */
     170          638 :   if (code == ABSU_EXPR
     171            9 :       && ANY_INTEGRAL_TYPE_P (TREE_TYPE (a))
     172          647 :       && !TYPE_UNSIGNED (TREE_TYPE (a)))
     173            9 :     rhs_type = signed_type_for (rhs_type);
     174              : 
     175          638 :   a = tree_vec_extract (gsi, rhs_type, a, bitsize, bitpos);
     176          638 :   return gimplify_build1 (gsi, code, inner_type, a);
     177              : }
     178              : 
     179              : static tree
     180        13527 : do_binop (gimple_stmt_iterator *gsi, tree inner_type, tree a, tree b,
     181              :           tree bitpos, tree bitsize, enum tree_code code,
     182              :           tree type ATTRIBUTE_UNUSED)
     183              : {
     184        13527 :   if (VECTOR_TYPE_P (TREE_TYPE (a)))
     185        13527 :     a = tree_vec_extract (gsi, inner_type, a, bitsize, bitpos);
     186        13527 :   if (VECTOR_TYPE_P (TREE_TYPE (b)))
     187        13207 :     b = tree_vec_extract (gsi, inner_type, b, bitsize, bitpos);
     188        13527 :   return gimplify_build2 (gsi, code, inner_type, a, b);
     189              : }
     190              : 
     191              : /* Construct expression (A[BITPOS] code B[BITPOS]) ? -1 : 0
     192              : 
     193              :    INNER_TYPE is the type of A and B elements
     194              : 
     195              :    returned expression is of signed integer type with the
     196              :    size equal to the size of INNER_TYPE.  */
     197              : static tree
     198         6500 : do_compare (gimple_stmt_iterator *gsi, tree inner_type, tree a, tree b,
     199              :             tree bitpos, tree bitsize, enum tree_code code, tree type)
     200              : {
     201         6500 :   tree stype = TREE_TYPE (type);
     202         6500 :   tree cst_false = build_zero_cst (stype);
     203         6500 :   tree cst_true = build_all_ones_cst (stype);
     204         6500 :   tree cmp;
     205              : 
     206         6500 :   a = tree_vec_extract (gsi, inner_type, a, bitsize, bitpos);
     207         6500 :   b = tree_vec_extract (gsi, inner_type, b, bitsize, bitpos);
     208              : 
     209         6500 :   cmp = build2 (code, boolean_type_node, a, b);
     210         6500 :   return gimplify_build3 (gsi, COND_EXPR, stype, cmp, cst_true, cst_false);
     211              : }
     212              : 
     213              : /* Expand vector addition to scalars.  This does bit twiddling
     214              :    in order to increase parallelism:
     215              : 
     216              :    a + b = (((int) a & 0x7f7f7f7f) + ((int) b & 0x7f7f7f7f)) ^
     217              :            (a ^ b) & 0x80808080
     218              : 
     219              :    a - b =  (((int) a | 0x80808080) - ((int) b & 0x7f7f7f7f)) ^
     220              :             (a ^ ~b) & 0x80808080
     221              : 
     222              :    -b = (0x80808080 - ((int) b & 0x7f7f7f7f)) ^ (~b & 0x80808080)
     223              : 
     224              :    This optimization should be done only if 4 vector items or more
     225              :    fit into a word.  */
     226              : static tree
     227            6 : do_plus_minus (gimple_stmt_iterator *gsi, tree word_type, tree a, tree b,
     228              :                tree bitpos ATTRIBUTE_UNUSED, tree bitsize ATTRIBUTE_UNUSED,
     229              :                enum tree_code code, tree type ATTRIBUTE_UNUSED)
     230              : {
     231            6 :   unsigned int width = vector_element_bits (TREE_TYPE (a));
     232            6 :   tree inner_type = TREE_TYPE (TREE_TYPE (a));
     233            6 :   unsigned HOST_WIDE_INT max;
     234            6 :   tree low_bits, high_bits, a_low, b_low, result_low, signs;
     235              : 
     236            6 :   max = GET_MODE_MASK (TYPE_MODE (inner_type));
     237            6 :   low_bits = build_replicated_int_cst (word_type, width, max >> 1);
     238            6 :   high_bits = build_replicated_int_cst (word_type, width, max & ~(max >> 1));
     239              : 
     240            6 :   a = tree_vec_extract (gsi, word_type, a, bitsize, bitpos);
     241            6 :   b = tree_vec_extract (gsi, word_type, b, bitsize, bitpos);
     242              : 
     243            6 :   signs = gimplify_build2 (gsi, BIT_XOR_EXPR, word_type, a, b);
     244            6 :   b_low = gimplify_build2 (gsi, BIT_AND_EXPR, word_type, b, low_bits);
     245            6 :   if (code == PLUS_EXPR)
     246            2 :     a_low = gimplify_build2 (gsi, BIT_AND_EXPR, word_type, a, low_bits);
     247              :   else
     248              :     {
     249            4 :       a_low = gimplify_build2 (gsi, BIT_IOR_EXPR, word_type, a, high_bits);
     250            4 :       signs = gimplify_build1 (gsi, BIT_NOT_EXPR, word_type, signs);
     251              :     }
     252              : 
     253            6 :   signs = gimplify_build2 (gsi, BIT_AND_EXPR, word_type, signs, high_bits);
     254            6 :   result_low = gimplify_build2 (gsi, code, word_type, a_low, b_low);
     255            6 :   return gimplify_build2 (gsi, BIT_XOR_EXPR, word_type, result_low, signs);
     256              : }
     257              : 
     258              : static tree
     259            0 : do_negate (gimple_stmt_iterator *gsi, tree word_type, tree b,
     260              :            tree unused ATTRIBUTE_UNUSED, tree bitpos ATTRIBUTE_UNUSED,
     261              :            tree bitsize ATTRIBUTE_UNUSED,
     262              :            enum tree_code code ATTRIBUTE_UNUSED,
     263              :            tree type ATTRIBUTE_UNUSED)
     264              : {
     265            0 :   unsigned int width = vector_element_bits (TREE_TYPE (b));
     266            0 :   tree inner_type = TREE_TYPE (TREE_TYPE (b));
     267            0 :   HOST_WIDE_INT max;
     268            0 :   tree low_bits, high_bits, b_low, result_low, signs;
     269              : 
     270            0 :   max = GET_MODE_MASK (TYPE_MODE (inner_type));
     271            0 :   low_bits = build_replicated_int_cst (word_type, width, max >> 1);
     272            0 :   high_bits = build_replicated_int_cst (word_type, width, max & ~(max >> 1));
     273              : 
     274            0 :   b = tree_vec_extract (gsi, word_type, b, bitsize, bitpos);
     275              : 
     276            0 :   b_low = gimplify_build2 (gsi, BIT_AND_EXPR, word_type, b, low_bits);
     277            0 :   signs = gimplify_build1 (gsi, BIT_NOT_EXPR, word_type, b);
     278            0 :   signs = gimplify_build2 (gsi, BIT_AND_EXPR, word_type, signs, high_bits);
     279            0 :   result_low = gimplify_build2 (gsi, MINUS_EXPR, word_type, high_bits, b_low);
     280            0 :   return gimplify_build2 (gsi, BIT_XOR_EXPR, word_type, result_low, signs);
     281              : }
     282              : 
     283              : /* Expand a vector operation to scalars, by using many operations
     284              :    whose type is the vector type's inner type.  */
     285              : static tree
     286         4107 : expand_vector_piecewise (gimple_stmt_iterator *gsi, elem_op_func f,
     287              :                          tree type, tree inner_type,
     288              :                          tree a, tree b, enum tree_code code,
     289              :                          bool parallel_p, tree ret_type = NULL_TREE)
     290              : {
     291         4107 :   vec<constructor_elt, va_gc> *v;
     292         4107 :   tree part_width = TYPE_SIZE (inner_type);
     293         4107 :   tree index = bitsize_int (0);
     294         4107 :   int nunits = nunits_for_known_piecewise_op (type);
     295         4107 :   int delta = (VECTOR_TYPE_P (inner_type)
     296         5104 :                ? nunits_for_known_piecewise_op (inner_type) : 1);
     297         4107 :   int i;
     298         4107 :   location_t loc = gimple_location (gsi_stmt (*gsi));
     299              : 
     300         4107 :   if (nunits == 1
     301         4107 :       || warning_suppressed_p (gsi_stmt (*gsi),
     302              :                                OPT_Wvector_operation_performance))
     303              :     /* Do not diagnose decomposing single element vectors or when
     304              :        decomposing vectorizer produced operations.  */
     305              :     ;
     306         3760 :   else if (ret_type || !parallel_p)
     307         3708 :     warning_at (loc, OPT_Wvector_operation_performance,
     308              :                 "vector operation will be expanded piecewise");
     309              :   else
     310           52 :     warning_at (loc, OPT_Wvector_operation_performance,
     311              :                 "vector operation will be expanded in parallel");
     312              : 
     313         4107 :   if (!ret_type)
     314         3751 :     ret_type = type;
     315         4107 :   vec_alloc (v, (nunits + delta - 1) / delta);
     316         4107 :   bool constant_p = true;
     317        47837 :   for (i = 0; i < nunits;
     318        39623 :        i += delta, index = int_const_binop (PLUS_EXPR, index, part_width))
     319              :     {
     320        39623 :       tree result = f (gsi, inner_type, a, b, index, part_width, code,
     321              :                        ret_type);
     322        39623 :       if (!CONSTANT_CLASS_P (result))
     323        39190 :         constant_p = false;
     324        39623 :       constructor_elt ce = {NULL_TREE, result};
     325        39623 :       v->quick_push (ce);
     326              :     }
     327              : 
     328         4107 :   if (constant_p)
     329            2 :     return build_vector_from_ctor (ret_type, v);
     330              :   else
     331         4105 :     return build_constructor (ret_type, v);
     332              : }
     333              : 
     334              : /* Expand a vector operation to scalars with the freedom to use
     335              :    a scalar integer type, or to use a different size for the items
     336              :    in the vector type.  */
     337              : static tree
     338          104 : expand_vector_parallel (gimple_stmt_iterator *gsi, elem_op_func f, tree type,
     339              :                         tree a, tree b, enum tree_code code)
     340              : {
     341          104 :   tree result, compute_type;
     342          104 :   int n_words = tree_to_uhwi (TYPE_SIZE_UNIT (type)) / UNITS_PER_WORD;
     343          104 :   location_t loc = gimple_location (gsi_stmt (*gsi));
     344              : 
     345              :   /* We have three strategies.  If the type is already correct, just do
     346              :      the operation an element at a time.  Else, if the vector is wider than
     347              :      one word, do it a word at a time; finally, if the vector is smaller
     348              :      than one word, do it as a scalar.  */
     349          104 :   if (TYPE_MODE (TREE_TYPE (type)) == word_mode)
     350            2 :      return expand_vector_piecewise (gsi, f,
     351            2 :                                      type, TREE_TYPE (type),
     352            2 :                                      a, b, code, true);
     353          102 :   else if (n_words > 1)
     354              :     {
     355           52 :       tree word_type = build_word_mode_vector_type (n_words);
     356           52 :       result = expand_vector_piecewise (gsi, f,
     357           52 :                                         word_type, TREE_TYPE (word_type),
     358              :                                         a, b, code, true);
     359           52 :       result = force_gimple_operand_gsi (gsi, result, true, NULL, true,
     360              :                                          GSI_SAME_STMT);
     361              :     }
     362              :   else
     363              :     {
     364              :       /* Use a single scalar operation with a mode no wider than word_mode.  */
     365           50 :       if (!warning_suppressed_p (gsi_stmt (*gsi),
     366              :                                  OPT_Wvector_operation_performance))
     367           50 :         warning_at (loc, OPT_Wvector_operation_performance,
     368              :                     "vector operation will be expanded with a "
     369              :                     "single scalar operation");
     370           50 :       scalar_int_mode mode
     371           50 :         = int_mode_for_size (tree_to_uhwi (TYPE_SIZE (type)), 0).require ();
     372           50 :       compute_type = lang_hooks.types.type_for_mode (mode, 1);
     373           50 :       result = f (gsi, compute_type, a, b, bitsize_zero_node,
     374           50 :                   TYPE_SIZE (compute_type), code, type);
     375              :     }
     376              : 
     377              :   return result;
     378              : }
     379              : 
     380              : /* Expand a vector operation to scalars; for integer types we can use
     381              :    special bit twiddling tricks to do the sums a word at a time, using
     382              :    function F_PARALLEL instead of F.  These tricks are done only if
     383              :    they can process at least four items, that is, only if the vector
     384              :    holds at least four items and if a word can hold four items.  */
     385              : static tree
     386          210 : expand_vector_addition (gimple_stmt_iterator *gsi,
     387              :                         elem_op_func f, elem_op_func f_parallel,
     388              :                         tree type, tree a, tree b, enum tree_code code)
     389              : {
     390          210 :   int parts_per_word = BITS_PER_WORD / vector_element_bits (type);
     391              : 
     392          420 :   if (INTEGRAL_TYPE_P (TREE_TYPE (type))
     393          210 :       && parts_per_word >= 4
     394          220 :       && nunits_for_known_piecewise_op (type) >= 4)
     395            3 :     return expand_vector_parallel (gsi, f_parallel,
     396            3 :                                    type, a, b, code);
     397              :   else
     398          207 :     return expand_vector_piecewise (gsi, f,
     399          207 :                                     type, TREE_TYPE (type),
     400          207 :                                     a, b, code, false);
     401              : }
     402              : 
     403              : /* Expand vector comparison expression OP0 CODE OP1 if the compare optab
     404              :    is not implemented.  */
     405              : 
     406              : static tree
     407        28155 : expand_vector_comparison (gimple_stmt_iterator *gsi, tree type, tree op0,
     408              :                           tree op1, enum tree_code code)
     409              : {
     410        28155 :   if (expand_vec_cmp_expr_p (TREE_TYPE (op0), type, code))
     411              :     return NULL_TREE;
     412              : 
     413          809 :   tree t;
     414          809 :   if (VECTOR_BOOLEAN_TYPE_P (type)
     415          809 :       && SCALAR_INT_MODE_P (TYPE_MODE (type))
     416          935 :       && known_lt (GET_MODE_BITSIZE (TYPE_MODE (type)),
     417              :                    TYPE_VECTOR_SUBPARTS (type)
     418              :                    * GET_MODE_BITSIZE (SCALAR_TYPE_MODE
     419              :                                        (TREE_TYPE (type)))))
     420              :     {
     421            0 :       tree inner_type = TREE_TYPE (TREE_TYPE (op0));
     422            0 :       tree part_width = vector_element_bits_tree (TREE_TYPE (op0));
     423            0 :       tree index = bitsize_int (0);
     424            0 :       int nunits = nunits_for_known_piecewise_op (TREE_TYPE (op0));
     425            0 :       int prec = GET_MODE_PRECISION (SCALAR_TYPE_MODE (type));
     426            0 :       tree ret_type = build_nonstandard_integer_type (prec, 1);
     427            0 :       tree ret_inner_type = boolean_type_node;
     428            0 :       int i;
     429            0 :       location_t loc = gimple_location (gsi_stmt (*gsi));
     430            0 :       t = build_zero_cst (ret_type);
     431              : 
     432            0 :       if (TYPE_PRECISION (ret_inner_type) != 1)
     433            0 :         ret_inner_type = build_nonstandard_integer_type (1, 1);
     434            0 :       if (!warning_suppressed_p (gsi_stmt (*gsi),
     435              :                                  OPT_Wvector_operation_performance))
     436            0 :         warning_at (loc, OPT_Wvector_operation_performance,
     437              :                     "vector operation will be expanded piecewise");
     438            0 :       for (i = 0; i < nunits;
     439            0 :            i++, index = int_const_binop (PLUS_EXPR, index, part_width))
     440              :         {
     441            0 :           tree a = tree_vec_extract (gsi, inner_type, op0, part_width,
     442              :                                      index);
     443            0 :           tree b = tree_vec_extract (gsi, inner_type, op1, part_width,
     444              :                                      index);
     445            0 :           tree result = gimplify_build2 (gsi, code, ret_inner_type, a, b);
     446            0 :           t = gimplify_build3 (gsi, BIT_INSERT_EXPR, ret_type, t, result,
     447            0 :                                bitsize_int (i));
     448              :         }
     449            0 :       t = gimplify_build1 (gsi, VIEW_CONVERT_EXPR, type, t);
     450              :     }
     451              :   else
     452          809 :     t = expand_vector_piecewise (gsi, do_compare, type,
     453          809 :                                  TREE_TYPE (TREE_TYPE (op0)), op0, op1,
     454              :                                  code, false);
     455              : 
     456              :   return t;
     457              : }
     458              : 
     459              : /* Helper function of expand_vector_divmod.  Gimplify a RSHIFT_EXPR in type
     460              :    of OP0 with shift counts in SHIFTCNTS array and return the temporary holding
     461              :    the result if successful, otherwise return NULL_TREE.  */
     462              : static tree
     463          716 : add_shift (gimple_stmt_iterator *gsi, tree type, tree op0, int *shiftcnts,
     464              :            enum tree_code code)
     465              : {
     466          716 :   optab op;
     467          716 :   unsigned int i, nunits = nunits_for_known_piecewise_op (type);
     468          716 :   bool scalar_shift = true;
     469              : 
     470         4204 :   for (i = 1; i < nunits; i++)
     471              :     {
     472         3488 :       if (shiftcnts[i] != shiftcnts[0])
     473           96 :         scalar_shift = false;
     474              :     }
     475              : 
     476          716 :   if (scalar_shift && shiftcnts[0] == 0)
     477              :     return op0;
     478              : 
     479          578 :   if (scalar_shift)
     480              :     {
     481          578 :       op = optab_for_tree_code (code, type, optab_scalar);
     482          578 :       if (op != unknown_optab
     483          578 :           && can_implement_p (op, TYPE_MODE (type)))
     484          578 :         return gimplify_build2 (gsi, code, type, op0,
     485          578 :                                 build_int_cst (integer_type_node, shiftcnts[0]));
     486              :     }
     487              : 
     488           32 :   op = optab_for_tree_code (code, type, optab_vector);
     489           32 :   if (op != unknown_optab
     490           32 :       && can_implement_p (op, TYPE_MODE (type)))
     491              :     {
     492            0 :       tree_vector_builder vec (type, nunits, 1);
     493            0 :       for (i = 0; i < nunits; i++)
     494            0 :         vec.quick_push (build_int_cst (TREE_TYPE (type), shiftcnts[i]));
     495            0 :       return gimplify_build2 (gsi, code, type, op0, vec.build ());
     496            0 :     }
     497              : 
     498              :   return NULL_TREE;
     499              : }
     500              : 
     501              : /* Verify that the target has optabs of VECTYPE to perform all the steps
     502              :    needed by the multiplication-by-immediate synthesis algorithm described by
     503              :    ALG and VAR.  Return true iff the target supports all the steps.  */
     504              : static bool
     505            1 : target_supports_mult_synth_alg (struct algorithm *alg, mult_variant var,
     506              :                                 tree vectype)
     507              : {
     508            1 :   if (alg->op[0] != alg_zero && alg->op[0] != alg_m)
     509              :     return false;
     510              : 
     511            1 :   bool plus_supported_p = target_supports_op_p (vectype, PLUS_EXPR,
     512              :                                                   optab_vector);
     513            1 :   bool minus_supported_p = target_supports_op_p (vectype, MINUS_EXPR,
     514              :                                                   optab_vector);
     515            1 :   bool negate_supported_p = target_supports_op_p (vectype, NEGATE_EXPR,
     516              :                                                   optab_vector);
     517              : 
     518            1 :   if ((var == negate_variant && !negate_supported_p)
     519            1 :       || (var == add_variant && !plus_supported_p))
     520              :     return false;
     521              : 
     522            1 :   bool lshift_supported_p
     523            1 :     = (target_supports_op_p (vectype, LSHIFT_EXPR, optab_scalar)
     524            1 :        || target_supports_op_p (vectype, LSHIFT_EXPR, optab_vector));
     525              : 
     526            5 :   for (int i = 1; i < alg->ops; i++)
     527              :     {
     528            4 :       switch (alg->op[i])
     529              :         {
     530            2 :         case alg_shift:
     531            2 :           if (!lshift_supported_p)
     532              :             return false;
     533              :           break;
     534            0 :         case alg_add_t_m2:
     535            0 :         case alg_add_t2_m:
     536            0 :         case alg_add_factor:
     537            0 :           if (!plus_supported_p || !lshift_supported_p)
     538              :             return false;
     539              :           break;
     540            2 :         case alg_sub_t_m2:
     541            2 :         case alg_sub_t2_m:
     542            2 :         case alg_sub_factor:
     543            2 :           if (!minus_supported_p || !lshift_supported_p)
     544              :             return false;
     545              :           break;
     546              :         case alg_unknown:
     547              :         case alg_m:
     548              :         case alg_zero:
     549              :         case alg_impossible:
     550              :           return false;
     551            0 :         default:
     552            0 :           gcc_unreachable ();
     553              :         }
     554              :     }
     555              : 
     556              :   return true;
     557              : }
     558              : /* For integer multiplications with exact power of 2, use left shifts.
     559              :    In constant is a uniform vector, then check if it can be synthesized with
     560              :    shifts and adds/subs.  Use scalar multiplication cost to compare with the
     561              :    synthesized vector code as done in expmed.cc.  */
     562              : static tree
     563            6 : expand_vector_mult (gimple_stmt_iterator *gsi, tree type, tree op0,
     564              :                     tree op1)
     565              : {
     566            6 :   if (!CONSTANT_CLASS_P (op1))
     567              :     return NULL_TREE;
     568            6 :   unsigned int nunits = nunits_for_known_piecewise_op (type);
     569            6 :   int *shifts = XALLOCAVEC (int, nunits);
     570              : 
     571            6 :   bool perfect_pow2_p = true;
     572            6 :   bool uniform_const_p = true;
     573            6 :   unsigned HOST_WIDE_INT first_cst, const_elt;
     574           13 :   for (unsigned int i = 0; i < nunits; i++)
     575              :     {
     576            7 :       tree cst = VECTOR_CST_ELT (op1, i);
     577            7 :       if (TREE_CODE (cst) != INTEGER_CST || tree_int_cst_sgn (cst) <= 0)
     578              :         return NULL_TREE;
     579              : 
     580            7 :       perfect_pow2_p &= integer_pow2p (cst);
     581              : 
     582            7 :       if (uniform_const_p && tree_fits_uhwi_p (cst))
     583              :         {
     584            2 :           const_elt = TREE_INT_CST_LOW (cst);
     585            2 :           if (i == 0)
     586              :             first_cst = const_elt;
     587            1 :           else if (first_cst != const_elt)
     588            0 :             uniform_const_p = false;
     589              :         }
     590              :       else
     591              :         uniform_const_p = false;
     592              : 
     593            7 :       shifts[i] = tree_log2 (cst);
     594              :     }
     595            6 :   if (perfect_pow2_p)
     596            0 :     return add_shift (gsi, type, op0, shifts, LSHIFT_EXPR);
     597            6 :   else if (uniform_const_p)
     598              :     {
     599            1 :       tree scalar_type = type;
     600            1 :       gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
     601            1 :       scalar_type = TREE_TYPE (type);
     602              : 
     603              :       // handle signed ints
     604            1 :       bool cast_to_unsigned_p = !TYPE_OVERFLOW_WRAPS (scalar_type);
     605            0 :       tree multtype = cast_to_unsigned_p
     606            0 :         ? unsigned_type_for (scalar_type) : scalar_type;
     607            0 :       tree vectype = cast_to_unsigned_p ? unsigned_type_for (type) : type;
     608              : 
     609              :       // estimate the scalar MULT cost by generating dummy rtx insns
     610            1 :       machine_mode mode = TYPE_MODE (multtype);
     611            1 :       rtx rop0 = gen_rtx_REG (mode, 0);
     612            1 :       rtx rop1 = gen_rtx_REG (mode, 1);
     613            1 :       rtx mult_rtx = gen_rtx_MULT (mode, rop0, rop1);
     614            1 :       int max_cost = nunits * set_src_cost (mult_rtx, mode, true);
     615              : 
     616            1 :       struct algorithm alg;
     617            1 :       mult_variant variant;
     618              : 
     619            1 :       bool possible = choose_mult_variant (TYPE_MODE (vectype), const_elt, &alg,
     620              :                                            &variant, max_cost);
     621            1 :       if (!possible || !target_supports_mult_synth_alg (&alg, variant, vectype))
     622              :         return NULL_TREE;
     623            1 :       if (cast_to_unsigned_p)
     624              :         {
     625            0 :           tree tmp_op = gimplify_build1 (gsi, VIEW_CONVERT_EXPR, vectype, op0);
     626            0 :           op0 = tmp_op;
     627              :         }
     628            1 :       tree accumulator, tmp_var;
     629            1 :       if (alg.op[0] == alg_zero)
     630            0 :         accumulator = build_zero_cst (vectype);
     631              :       else
     632              :         accumulator = op0;
     633              : 
     634            5 :       for (int i = 1; i < alg.ops; i++)
     635              :         {
     636           12 :           for (unsigned int j = 0; j < nunits; j++)
     637            8 :             shifts[j] = alg.log[i];
     638              : 
     639            4 :           switch (alg.op[i])
     640              :             {
     641            2 :             case alg_shift:
     642            2 :               accumulator = add_shift (gsi, vectype, accumulator, shifts,
     643              :                                        LSHIFT_EXPR);
     644            2 :               break;
     645            0 :             case alg_add_t_m2:
     646            0 :               tmp_var = add_shift (gsi, vectype, op0, shifts, LSHIFT_EXPR);
     647            0 :               accumulator = gimplify_build2 (gsi, PLUS_EXPR, vectype, tmp_var,
     648              :                                              accumulator);
     649            0 :               break;
     650            1 :             case alg_sub_t_m2:
     651            1 :               tmp_var = add_shift (gsi, vectype, op0, shifts, LSHIFT_EXPR);
     652            1 :               accumulator = gimplify_build2 (gsi, MINUS_EXPR, vectype,
     653              :                                              accumulator, tmp_var);
     654            1 :               break;
     655            0 :             case alg_add_t2_m:
     656            0 :               tmp_var = add_shift (gsi, vectype, accumulator, shifts,
     657              :                                    LSHIFT_EXPR);
     658            0 :               accumulator = gimplify_build2 (gsi, PLUS_EXPR, vectype, tmp_var,
     659              :                                              op0);
     660            0 :               break;
     661            0 :             case alg_sub_t2_m:
     662            0 :               tmp_var = add_shift (gsi, vectype, accumulator, shifts,
     663              :                                    LSHIFT_EXPR);
     664            0 :               accumulator = gimplify_build2 (gsi, MINUS_EXPR, vectype,
     665              :                                              tmp_var, op0);
     666            0 :               break;
     667            0 :             case alg_add_factor:
     668            0 :               tmp_var = add_shift (gsi, vectype, accumulator, shifts,
     669              :                                    LSHIFT_EXPR);
     670            0 :               accumulator = gimplify_build2 (gsi, PLUS_EXPR, vectype,
     671              :                                              accumulator, tmp_var);
     672            0 :               break;
     673            1 :             case alg_sub_factor:
     674            1 :               tmp_var = add_shift (gsi, vectype, accumulator, shifts,
     675              :                                    LSHIFT_EXPR);
     676            1 :               accumulator = gimplify_build2 (gsi, MINUS_EXPR, vectype,
     677              :                                              tmp_var, accumulator);
     678            1 :               break;
     679            0 :             default:
     680            0 :               gcc_unreachable ();
     681              :             }
     682              :         }
     683            1 :       if (variant == negate_variant)
     684            0 :         accumulator = gimplify_build1 (gsi, NEGATE_EXPR, vectype, accumulator);
     685            1 :       else if (variant == add_variant)
     686            0 :         accumulator = gimplify_build2 (gsi, PLUS_EXPR, vectype, accumulator,
     687              :                                        op0);
     688              : 
     689            1 :       if (cast_to_unsigned_p)
     690            0 :         accumulator = gimplify_build1 (gsi, VIEW_CONVERT_EXPR, vectype,
     691              :                                        accumulator);
     692              :       return accumulator;
     693              :     }
     694              :   return NULL_TREE;
     695              : }
     696              : /* Try to expand integer vector division by constant using
     697              :    widening multiply, shifts and additions.  */
     698              : static tree
     699          636 : expand_vector_divmod (gimple_stmt_iterator *gsi, tree type, tree op0,
     700              :                       tree op1, enum tree_code code)
     701              : {
     702          636 :   bool use_pow2 = true;
     703          636 :   bool has_vector_shift = true;
     704          636 :   bool has_vector_negate = true;
     705          636 :   bool use_abs_op1 = false;
     706              :   /* For the power-of-two path: 1 if the quotient is negated (all divisors
     707              :      are negative), 0 if not, -1 if undetermined.  */
     708          636 :   int negate_p = -1;
     709          636 :   int mode = -1, this_mode;
     710          636 :   int pre_shift = -1, post_shift;
     711          636 :   unsigned int nunits = nunits_for_known_piecewise_op (type);
     712          636 :   int *shifts = XALLOCAVEC (int, nunits * 4);
     713          636 :   int *pre_shifts = shifts + nunits;
     714          636 :   int *post_shifts = pre_shifts + nunits;
     715          636 :   int *shift_temps = post_shifts + nunits;
     716          636 :   unsigned HOST_WIDE_INT *mulc = XALLOCAVEC (unsigned HOST_WIDE_INT, nunits);
     717          636 :   int prec = TYPE_PRECISION (TREE_TYPE (type));
     718          636 :   unsigned int i;
     719          636 :   signop sign_p = TYPE_SIGN (TREE_TYPE (type));
     720          636 :   unsigned HOST_WIDE_INT mask = GET_MODE_MASK (TYPE_MODE (TREE_TYPE (type)));
     721          636 :   tree cur_op, mulcst, tem;
     722          636 :   optab op;
     723              : 
     724          636 :   if (prec > HOST_BITS_PER_WIDE_INT)
     725              :     return NULL_TREE;
     726              : 
     727          635 :   op = optab_for_tree_code (RSHIFT_EXPR, type, optab_vector);
     728          635 :   if (op == unknown_optab
     729          635 :       || !can_implement_p (op, TYPE_MODE (type)))
     730              :     has_vector_shift = false;
     731              : 
     732              :   /* expand_unop falls back to 0 - x for a missing vector negation.  */
     733          635 :   op = optab_for_tree_code (NEGATE_EXPR, type, optab_default);
     734          635 :   if (op == unknown_optab
     735          635 :       || !can_implement_p (op, TYPE_MODE (type)))
     736              :     {
     737            0 :       op = optab_for_tree_code (MINUS_EXPR, type, optab_default);
     738            0 :       if (op == unknown_optab
     739            0 :           || !can_implement_p (op, TYPE_MODE (type)))
     740              :         has_vector_negate = false;
     741              :     }
     742              : 
     743              :   /* Analysis phase.  Determine if all op1 elements are either power
     744              :      of two and it is possible to expand it using shifts (or for remainder
     745              :      using masking).  Additionally compute the multiplicative constants
     746              :      and pre and post shifts if the division is to be expanded using
     747              :      widening or high part multiplication plus shifts.  */
     748         4201 :   for (i = 0; i < nunits; i++)
     749              :     {
     750         3576 :       tree cst = VECTOR_CST_ELT (op1, i);
     751         3576 :       unsigned HOST_WIDE_INT ml;
     752              : 
     753         3576 :       if (TREE_CODE (cst) != INTEGER_CST || integer_zerop (cst))
     754           10 :         return NULL_TREE;
     755         3568 :       pre_shifts[i] = 0;
     756         3568 :       post_shifts[i] = 0;
     757         3568 :       mulc[i] = 0;
     758         3568 :       if (use_pow2)
     759              :         {
     760              :           /* Divide by a negative power of two as by its absolute value and
     761              :              negate the quotient, as expand_divmod does.  The quotient needs
     762              :              all elements to have the same sign; the remainder does not
     763              :              depend on the sign, as x % d == x % -d.  INT_MIN is excluded,
     764              :              as its magnitude does not fit.  */
     765         1725 :           int this_neg = 0;
     766         1725 :           wide_int w = wi::to_wide (cst);
     767         1725 :           if (sign_p == SIGNED && wi::neg_p (w))
     768              :             {
     769          590 :               this_neg = code == TRUNC_DIV_EXPR;
     770          590 :               w = wi::neg (w);
     771              :             }
     772         1725 :           int log2 = wi::exact_log2 (w);
     773         1725 :           if (log2 < 0
     774         1439 :               || (sign_p == SIGNED && log2 >= prec - 1)
     775         1400 :               || (this_neg && !has_vector_negate)
     776         1400 :               || (negate_p >= 0 && negate_p != this_neg))
     777              :             use_pow2 = false;
     778              :           else
     779              :             {
     780         1392 :               negate_p = this_neg;
     781         1392 :               shifts[i] = log2;
     782         1392 :               if (shifts[i] != shifts[0]
     783          362 :                   && code == TRUNC_DIV_EXPR
     784          362 :                   && !has_vector_shift)
     785         1725 :                 use_pow2 = false;
     786              :             }
     787         1725 :         }
     788         3568 :       if (mode == -2)
     789         1282 :         continue;
     790         2436 :       if (sign_p == UNSIGNED)
     791              :         {
     792          822 :           unsigned HOST_WIDE_INT mh;
     793          822 :           unsigned HOST_WIDE_INT d = TREE_INT_CST_LOW (cst) & mask;
     794              : 
     795          822 :           if (d >= (HOST_WIDE_INT_1U << (prec - 1)))
     796              :             /* FIXME: Can transform this into op0 >= op1 ? 1 : 0.  */
     797              :             return NULL_TREE;
     798              : 
     799          820 :           if (d <= 1)
     800              :             {
     801           32 :               mode = -2;
     802           32 :               continue;
     803              :             }
     804              : 
     805              :           /* Find a suitable multiplier and right shift count instead of
     806              :              directly dividing by D.  */
     807          788 :           mh = choose_multiplier (d, prec, prec, &ml, &post_shift);
     808              : 
     809              :           /* If the suggested multiplier is more than PREC bits, we can
     810              :              do better for even divisors, using an initial right shift.  */
     811          788 :           if ((mh != 0 && (d & 1) == 0)
     812          646 :               || (!has_vector_shift && pre_shift != -1))
     813              :             {
     814          142 :               if (has_vector_shift)
     815            0 :                 pre_shift = ctz_or_zero (d);
     816          174 :               else if (pre_shift == -1)
     817              :                 {
     818              :                   unsigned int j;
     819          518 :                   for (j = 0; j < nunits; j++)
     820              :                     {
     821          440 :                       tree cst2 = VECTOR_CST_ELT (op1, j);
     822          440 :                       unsigned HOST_WIDE_INT d2;
     823          440 :                       int this_pre_shift;
     824              : 
     825          440 :                       if (!tree_fits_uhwi_p (cst2))
     826              :                         return NULL_TREE;
     827          440 :                       d2 = tree_to_uhwi (cst2) & mask;
     828          440 :                       if (d2 == 0)
     829              :                         return NULL_TREE;
     830          440 :                       this_pre_shift = floor_log2 (d2 & -d2);
     831          440 :                       if (pre_shift == -1 || this_pre_shift < pre_shift)
     832           78 :                         pre_shift = this_pre_shift;
     833              :                     }
     834           78 :                   if (i != 0 && pre_shift != 0)
     835              :                     {
     836              :                       /* Restart.  */
     837            0 :                       i = -1U;
     838            0 :                       mode = -1;
     839            0 :                       continue;
     840              :                     }
     841              :                 }
     842          174 :               if (pre_shift != 0)
     843              :                 {
     844          174 :                   if ((d >> pre_shift) <= 1)
     845              :                     {
     846           46 :                       mode = -2;
     847           46 :                       continue;
     848              :                     }
     849          128 :                   mh = choose_multiplier (d >> pre_shift, prec,
     850              :                                           prec - pre_shift,
     851              :                                           &ml, &post_shift);
     852          128 :                   gcc_assert (!mh);
     853          128 :                   pre_shifts[i] = pre_shift;
     854              :                 }
     855              :             }
     856          742 :           if (!mh)
     857              :             this_mode = 0;
     858              :           else
     859          192 :             this_mode = 1;
     860              :         }
     861              :       else
     862              :         {
     863         1614 :           HOST_WIDE_INT d = TREE_INT_CST_LOW (cst);
     864         1614 :           unsigned HOST_WIDE_INT abs_d;
     865              : 
     866              :           /* Since d might be INT_MIN, we have to cast to
     867              :              unsigned HOST_WIDE_INT before negating to avoid
     868              :              undefined signed overflow.  */
     869         1614 :           abs_d = (d >= 0
     870              :                   ? (unsigned HOST_WIDE_INT) d
     871              :                   : - (unsigned HOST_WIDE_INT) d);
     872              : 
     873              :           /* n rem d = n rem -d */
     874         1614 :           if (code == TRUNC_MOD_EXPR && d < 0)
     875              :             {
     876          367 :               d = abs_d;
     877          367 :               use_abs_op1 = true;
     878              :             }
     879         1614 :           if (abs_d == HOST_WIDE_INT_1U << (prec - 1))
     880              :             {
     881              :               /* This case is not handled correctly below.  */
     882           39 :               mode = -2;
     883           39 :               continue;
     884              :             }
     885         1575 :           if (abs_d <= 1)
     886              :             {
     887           33 :               mode = -2;
     888           33 :               continue;
     889              :             }
     890              : 
     891         1542 :           choose_multiplier (abs_d, prec, prec - 1, &ml,
     892              :                              &post_shift);
     893         1542 :           if (ml >= HOST_WIDE_INT_1U << (prec - 1))
     894              :             {
     895          918 :               this_mode = 4 + (d < 0);
     896          918 :               ml |= HOST_WIDE_INT_M1U << (prec - 1);
     897              :             }
     898              :           else
     899          624 :             this_mode = 2 + (d < 0);
     900              :         }
     901         2284 :       mulc[i] = ml;
     902         2284 :       post_shifts[i] = post_shift;
     903         2284 :       if ((i && !has_vector_shift && post_shifts[0] != post_shift)
     904         2107 :           || post_shift >= prec
     905         2107 :           || pre_shifts[i] >= prec)
     906          177 :         this_mode = -2;
     907              : 
     908         2284 :       if (i == 0)
     909              :         mode = this_mode;
     910         1803 :       else if (mode != this_mode)
     911          185 :         mode = -2;
     912              :     }
     913              : 
     914          625 :   if (use_pow2)
     915              :     {
     916          228 :       tree addend = NULL_TREE;
     917          228 :       if (sign_p == SIGNED)
     918              :         {
     919              :           tree uns_type;
     920              : 
     921              :           /* Both division and remainder sequences need
     922              :              op0 < 0 ? mask : 0 computed.  It can be either computed as
     923              :              (type) (((uns_type) (op0 >> (prec - 1))) >> (prec - shifts[i]))
     924              :              if none of the shifts is 0, or as the conditional.  */
     925          924 :           for (i = 0; i < nunits; i++)
     926          784 :             if (shifts[i] == 0)
     927              :               break;
     928          166 :           uns_type
     929          166 :             = build_vector_type (build_nonstandard_integer_type (prec, 1),
     930          166 :                                  nunits);
     931          166 :           if (i == nunits && TYPE_MODE (uns_type) == TYPE_MODE (type))
     932              :             {
     933          880 :               for (i = 0; i < nunits; i++)
     934          740 :                 shift_temps[i] = prec - 1;
     935          140 :               cur_op = add_shift (gsi, type, op0, shift_temps, RSHIFT_EXPR);
     936          140 :               if (cur_op != NULL_TREE)
     937              :                 {
     938          140 :                   cur_op = gimplify_build1 (gsi, VIEW_CONVERT_EXPR,
     939              :                                             uns_type, cur_op);
     940         1020 :                   for (i = 0; i < nunits; i++)
     941          740 :                     shift_temps[i] = prec - shifts[i];
     942          140 :                   cur_op = add_shift (gsi, uns_type, cur_op, shift_temps,
     943              :                                       RSHIFT_EXPR);
     944          140 :                   if (cur_op != NULL_TREE)
     945          108 :                     addend = gimplify_build1 (gsi, VIEW_CONVERT_EXPR,
     946              :                                               type, cur_op);
     947              :                 }
     948              :             }
     949          166 :           tree mask_type = truth_type_for (type);
     950          166 :           if (addend == NULL_TREE
     951           58 :               && expand_vec_cmp_expr_p (type, mask_type, LT_EXPR)
     952          216 :               && expand_vec_cond_expr_p (type, mask_type))
     953              :             {
     954           50 :               tree zero, cst, mask_type, mask;
     955           50 :               gimple *stmt, *cond;
     956              : 
     957           50 :               mask_type = truth_type_for (type);
     958           50 :               zero = build_zero_cst (type);
     959           50 :               mask = make_ssa_name (mask_type);
     960           50 :               cond = gimple_build_assign (mask, LT_EXPR, op0, zero);
     961           50 :               gsi_insert_before (gsi, cond, GSI_SAME_STMT);
     962           50 :               tree_vector_builder vec (type, nunits, 1);
     963          368 :               for (i = 0; i < nunits; i++)
     964          268 :                 vec.quick_push (build_int_cst (TREE_TYPE (type),
     965              :                                                (HOST_WIDE_INT_1U
     966          268 :                                                 << shifts[i]) - 1));
     967           50 :               cst = vec.build ();
     968           50 :               addend = make_ssa_name (type);
     969           50 :               stmt
     970           50 :                 = gimple_build_assign (addend, VEC_COND_EXPR, mask, cst, zero);
     971           50 :               gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
     972           50 :             }
     973              :         }
     974          228 :       if (code == TRUNC_DIV_EXPR)
     975              :         {
     976           80 :           if (sign_p == UNSIGNED)
     977              :             {
     978              :               /* q = op0 >> shift;  */
     979           30 :               cur_op = add_shift (gsi, type, op0, shifts, RSHIFT_EXPR);
     980           30 :               if (cur_op != NULL_TREE)
     981              :                 return cur_op;
     982              :             }
     983           50 :           else if (addend != NULL_TREE)
     984              :             {
     985              :               /* t1 = op0 + addend;
     986              :                  q = t1 >> shift;
     987              :                  and, for a negative power-of-two divisor, q = -q.  */
     988           50 :               op = optab_for_tree_code (PLUS_EXPR, type, optab_default);
     989           50 :               if (op != unknown_optab
     990           50 :                   && can_implement_p (op, TYPE_MODE (type)))
     991              :                 {
     992           50 :                   cur_op = gimplify_build2 (gsi, PLUS_EXPR, type, op0, addend);
     993           50 :                   cur_op = add_shift (gsi, type, cur_op, shifts, RSHIFT_EXPR);
     994           50 :                   if (cur_op != NULL_TREE)
     995              :                     {
     996           50 :                       if (negate_p == 1)
     997           26 :                         cur_op = gimplify_build1 (gsi, NEGATE_EXPR, type,
     998              :                                                   cur_op);
     999              :                       return cur_op;
    1000              :                     }
    1001              :                 }
    1002              :             }
    1003              :         }
    1004              :       else
    1005              :         {
    1006          148 :           tree mask;
    1007          148 :           tree_vector_builder vec (type, nunits, 1);
    1008         1084 :           for (i = 0; i < nunits; i++)
    1009          788 :             vec.quick_push (build_int_cst (TREE_TYPE (type),
    1010              :                                            (HOST_WIDE_INT_1U
    1011          788 :                                             << shifts[i]) - 1));
    1012          148 :           mask = vec.build ();
    1013          148 :           op = optab_for_tree_code (BIT_AND_EXPR, type, optab_default);
    1014          148 :           if (op != unknown_optab
    1015          148 :               && can_implement_p (op, TYPE_MODE (type)))
    1016              :             {
    1017          148 :               if (sign_p == UNSIGNED)
    1018              :                 /* r = op0 & mask;  */
    1019           32 :                 return gimplify_build2 (gsi, BIT_AND_EXPR, type, op0, mask);
    1020          116 :               else if (addend != NULL_TREE)
    1021              :                 {
    1022              :                   /* t1 = op0 + addend;
    1023              :                      t2 = t1 & mask;
    1024              :                      r = t2 - addend;  */
    1025          108 :                   op = optab_for_tree_code (PLUS_EXPR, type, optab_default);
    1026          108 :                   if (op != unknown_optab
    1027          108 :                       && can_implement_p (op, TYPE_MODE (type)))
    1028              :                     {
    1029          108 :                       cur_op = gimplify_build2 (gsi, PLUS_EXPR, type, op0,
    1030              :                                                 addend);
    1031          108 :                       cur_op = gimplify_build2 (gsi, BIT_AND_EXPR, type,
    1032              :                                                 cur_op, mask);
    1033          108 :                       op = optab_for_tree_code (MINUS_EXPR, type,
    1034              :                                                 optab_default);
    1035          108 :                       if (op != unknown_optab
    1036          108 :                           && can_implement_p (op, TYPE_MODE (type)))
    1037          108 :                         return gimplify_build2 (gsi, MINUS_EXPR, type,
    1038          108 :                                                 cur_op, addend);
    1039              :                     }
    1040              :                 }
    1041              :             }
    1042          148 :         }
    1043              :     }
    1044              : 
    1045          405 :   if (mode == -2 || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
    1046              :     return NULL_TREE;
    1047              : 
    1048          182 :   if (!can_mult_highpart_p (TYPE_MODE (type), TYPE_UNSIGNED (type)))
    1049              :     return NULL_TREE;
    1050              : 
    1051          176 :   cur_op = op0;
    1052              : 
    1053          176 :   switch (mode)
    1054              :     {
    1055           69 :     case 0:
    1056           69 :       gcc_assert (sign_p == UNSIGNED);
    1057              :       /* t1 = oprnd0 >> pre_shift;
    1058              :          t2 = t1 h* ml;
    1059              :          q = t2 >> post_shift;  */
    1060           69 :       cur_op = add_shift (gsi, type, cur_op, pre_shifts, RSHIFT_EXPR);
    1061           69 :       if (cur_op == NULL_TREE)
    1062              :         return NULL_TREE;
    1063              :       break;
    1064           32 :     case 1:
    1065           32 :       gcc_assert (sign_p == UNSIGNED);
    1066          224 :       for (i = 0; i < nunits; i++)
    1067              :         {
    1068          192 :           shift_temps[i] = 1;
    1069          192 :           post_shifts[i]--;
    1070              :         }
    1071              :       break;
    1072           75 :     case 2:
    1073           75 :     case 3:
    1074           75 :     case 4:
    1075           75 :     case 5:
    1076           75 :       gcc_assert (sign_p == SIGNED);
    1077          619 :       for (i = 0; i < nunits; i++)
    1078          544 :         shift_temps[i] = prec - 1;
    1079              :       break;
    1080              :     default:
    1081              :       return NULL_TREE;
    1082              :     }
    1083              : 
    1084          176 :   tree_vector_builder vec (type, nunits, 1);
    1085         1492 :   for (i = 0; i < nunits; i++)
    1086         1140 :     vec.quick_push (build_int_cst (TREE_TYPE (type), mulc[i]));
    1087          176 :   mulcst = vec.build ();
    1088              : 
    1089          176 :   cur_op = gimplify_build2 (gsi, MULT_HIGHPART_EXPR, type, cur_op, mulcst);
    1090              : 
    1091          176 :   switch (mode)
    1092              :     {
    1093           69 :     case 0:
    1094              :       /* t1 = oprnd0 >> pre_shift;
    1095              :          t2 = t1 h* ml;
    1096              :          q = t2 >> post_shift;  */
    1097           69 :       cur_op = add_shift (gsi, type, cur_op, post_shifts, RSHIFT_EXPR);
    1098           69 :       break;
    1099           32 :     case 1:
    1100              :       /* t1 = oprnd0 h* ml;
    1101              :          t2 = oprnd0 - t1;
    1102              :          t3 = t2 >> 1;
    1103              :          t4 = t1 + t3;
    1104              :          q = t4 >> (post_shift - 1);  */
    1105           32 :       op = optab_for_tree_code (MINUS_EXPR, type, optab_default);
    1106           32 :       if (op == unknown_optab
    1107           32 :           || !can_implement_p (op, TYPE_MODE (type)))
    1108              :         return NULL_TREE;
    1109           32 :       tem = gimplify_build2 (gsi, MINUS_EXPR, type, op0, cur_op);
    1110           32 :       tem = add_shift (gsi, type, tem, shift_temps, RSHIFT_EXPR);
    1111           32 :       op = optab_for_tree_code (PLUS_EXPR, type, optab_default);
    1112           32 :       if (op == unknown_optab
    1113           32 :           || !can_implement_p (op, TYPE_MODE (type)))
    1114              :         return NULL_TREE;
    1115           32 :       tem = gimplify_build2 (gsi, PLUS_EXPR, type, cur_op, tem);
    1116           32 :       cur_op = add_shift (gsi, type, tem, post_shifts, RSHIFT_EXPR);
    1117           32 :       if (cur_op == NULL_TREE)
    1118              :         return NULL_TREE;
    1119              :       break;
    1120           75 :     case 2:
    1121           75 :     case 3:
    1122           75 :     case 4:
    1123           75 :     case 5:
    1124              :       /* t1 = oprnd0 h* ml;
    1125              :          t2 = t1; [ iff (mode & 2) != 0 ]
    1126              :          t2 = t1 + oprnd0; [ iff (mode & 2) == 0 ]
    1127              :          t3 = t2 >> post_shift;
    1128              :          t4 = oprnd0 >> (prec - 1);
    1129              :          q = t3 - t4; [ iff (mode & 1) == 0 ]
    1130              :          q = t4 - t3; [ iff (mode & 1) != 0 ]  */
    1131           75 :       if ((mode & 2) == 0)
    1132              :         {
    1133           16 :           op = optab_for_tree_code (PLUS_EXPR, type, optab_default);
    1134           16 :           if (op == unknown_optab
    1135           16 :               || !can_implement_p (op, TYPE_MODE (type)))
    1136              :             return NULL_TREE;
    1137           16 :           cur_op = gimplify_build2 (gsi, PLUS_EXPR, type, cur_op, op0);
    1138              :         }
    1139           75 :       cur_op = add_shift (gsi, type, cur_op, post_shifts, RSHIFT_EXPR);
    1140           75 :       if (cur_op == NULL_TREE)
    1141              :         return NULL_TREE;
    1142           75 :       tem = add_shift (gsi, type, op0, shift_temps, RSHIFT_EXPR);
    1143           75 :       if (tem == NULL_TREE)
    1144              :         return NULL_TREE;
    1145           75 :       op = optab_for_tree_code (MINUS_EXPR, type, optab_default);
    1146           75 :       if (op == unknown_optab
    1147           75 :           || !can_implement_p (op, TYPE_MODE (type)))
    1148              :         return NULL_TREE;
    1149           75 :       if ((mode & 1) == 0)
    1150           75 :         cur_op = gimplify_build2 (gsi, MINUS_EXPR, type, cur_op, tem);
    1151              :       else
    1152            0 :         cur_op = gimplify_build2 (gsi, MINUS_EXPR, type, tem, cur_op);
    1153              :       break;
    1154              :     default:
    1155              :       gcc_unreachable ();
    1156              :     }
    1157              : 
    1158          176 :   if (code == TRUNC_DIV_EXPR)
    1159              :     return cur_op;
    1160              : 
    1161              :   /* We divided.  Now finish by:
    1162              :      t1 = q * oprnd1;
    1163              :      r = oprnd0 - t1;  */
    1164           87 :   op = optab_for_tree_code (MULT_EXPR, type, optab_default);
    1165           87 :   if (op == unknown_optab
    1166           87 :       || !can_implement_p (op, TYPE_MODE (type)))
    1167              :     return NULL_TREE;
    1168           87 :   if (use_abs_op1)
    1169              :     {
    1170            7 :       tree_vector_builder elts;
    1171            7 :       if (!elts.new_unary_operation (type, op1, false))
    1172              :         return NULL_TREE;
    1173            7 :       unsigned int count = elts.encoded_nelts ();
    1174           14 :       for (unsigned int i = 0; i < count; ++i)
    1175              :         {
    1176            7 :           tree elem1 = VECTOR_CST_ELT (op1, i);
    1177              : 
    1178            7 :           tree elt = const_unop (ABS_EXPR, TREE_TYPE (elem1), elem1);
    1179            7 :           if (elt == NULL_TREE)
    1180            0 :             return NULL_TREE;
    1181            7 :           elts.quick_push (elt);
    1182              :         }
    1183            7 :       op1 = elts.build ();
    1184            7 :     }
    1185           87 :   tem = gimplify_build2 (gsi, MULT_EXPR, type, cur_op, op1);
    1186           87 :   op = optab_for_tree_code (MINUS_EXPR, type, optab_default);
    1187           87 :   if (op == unknown_optab
    1188           87 :       || !can_implement_p (op, TYPE_MODE (type)))
    1189              :     return NULL_TREE;
    1190           87 :   return gimplify_build2 (gsi, MINUS_EXPR, type, op0, tem);
    1191              : }
    1192              : 
    1193              : /* Expand a vector condition to scalars, by using many conditions
    1194              :    on the vector's elements.  */
    1195              : 
    1196              : static bool
    1197        24336 : expand_vector_condition (gimple_stmt_iterator *gsi)
    1198              : {
    1199        24336 :   gassign *stmt = as_a <gassign *> (gsi_stmt (*gsi));
    1200        24336 :   tree type = TREE_TYPE (gimple_assign_lhs (stmt));
    1201        24336 :   tree a = gimple_assign_rhs1 (stmt);
    1202        24336 :   tree a1 = a;
    1203        24336 :   tree a2 = NULL_TREE;
    1204        24336 :   bool a_is_scalar_bitmask = false;
    1205        24336 :   tree b = gimple_assign_rhs2 (stmt);
    1206        24336 :   tree c = gimple_assign_rhs3 (stmt);
    1207        24336 :   vec<constructor_elt, va_gc> *v;
    1208        24336 :   tree constr;
    1209        24336 :   tree inner_type = TREE_TYPE (type);
    1210        24336 :   tree width = vector_element_bits_tree (type);
    1211        24336 :   tree cond_type = TREE_TYPE (TREE_TYPE (a));
    1212        24336 :   tree index = bitsize_int (0);
    1213        24336 :   tree comp_width = width;
    1214        24336 :   tree comp_index = index;
    1215        24336 :   location_t loc = gimple_location (gsi_stmt (*gsi));
    1216              : 
    1217        24336 :   gcc_assert (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (a)));
    1218              : 
    1219        24336 :   if (expand_vec_cond_expr_p (type, TREE_TYPE (a)))
    1220              :     return true;
    1221              : 
    1222              :   /* Handle vector boolean types with bitmasks.  We can transform
    1223              :       vbfld_1 = tmp_6 ? vbfld_4 : vbfld_5;
    1224              :      into
    1225              :       tmp_7 = tmp_6 & vbfld_4;
    1226              :       tmp_8 = ~tmp_6;
    1227              :       tmp_9 = tmp_8 & vbfld_5;
    1228              :       vbfld_1 = tmp_7 | tmp_9;  */
    1229          539 :   if (VECTOR_BOOLEAN_TYPE_P (type)
    1230            0 :       && SCALAR_INT_MODE_P (TYPE_MODE (type))
    1231          539 :       && useless_type_conversion_p (type, TREE_TYPE (a)))
    1232              :     {
    1233            0 :       a1 = gimplify_build2 (gsi, BIT_AND_EXPR, type, a, b);
    1234            0 :       a2 = gimplify_build1 (gsi, BIT_NOT_EXPR, type, a);
    1235            0 :       a2 = gimplify_build2 (gsi, BIT_AND_EXPR, type, a2, c);
    1236            0 :       a = gimplify_build2 (gsi, BIT_IOR_EXPR, type, a1, a2);
    1237            0 :       gimple_assign_set_rhs_from_tree (gsi, a);
    1238            0 :       update_stmt (gsi_stmt (*gsi));
    1239            0 :       return true;
    1240              :     }
    1241              : 
    1242              :   /* TODO: try and find a smaller vector type.  */
    1243              : 
    1244          539 :   if (!warning_suppressed_p (stmt, OPT_Wvector_operation_performance))
    1245          539 :     warning_at (loc, OPT_Wvector_operation_performance,
    1246              :                 "vector condition will be expanded piecewise");
    1247              : 
    1248         1042 :   if (SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (a)))
    1249          611 :       && known_lt (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (a))),
    1250              :                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (a))
    1251              :                    * GET_MODE_BITSIZE (SCALAR_TYPE_MODE
    1252              :                                                 (TREE_TYPE (TREE_TYPE (a))))))
    1253              :     {
    1254            0 :       a_is_scalar_bitmask = true;
    1255            0 :       int prec = GET_MODE_PRECISION (SCALAR_TYPE_MODE (TREE_TYPE (a)));
    1256            0 :       tree atype = build_nonstandard_integer_type (prec, 1);
    1257            0 :       a = gimplify_build1 (gsi, VIEW_CONVERT_EXPR, atype, a);
    1258              :     }
    1259              :   else
    1260          539 :     comp_width = vector_element_bits_tree (TREE_TYPE (a));
    1261              : 
    1262          539 :   int nunits = nunits_for_known_piecewise_op (type);
    1263          539 :   vec_alloc (v, nunits);
    1264          539 :   bool constant_p = true;
    1265         7143 :   for (int i = 0; i < nunits; i++)
    1266              :     {
    1267         6065 :       tree aa, result;
    1268         6065 :       tree bb = tree_vec_extract (gsi, inner_type, b, width, index);
    1269         6065 :       tree cc = tree_vec_extract (gsi, inner_type, c, width, index);
    1270         6065 :       if (a_is_scalar_bitmask)
    1271              :         {
    1272            0 :           wide_int w = wi::set_bit_in_zero (i, TYPE_PRECISION (TREE_TYPE (a)));
    1273            0 :           result = gimplify_build2 (gsi, BIT_AND_EXPR, TREE_TYPE (a),
    1274            0 :                                     a, wide_int_to_tree (TREE_TYPE (a), w));
    1275            0 :           aa = gimplify_build2 (gsi, NE_EXPR, boolean_type_node, result,
    1276            0 :                                 build_zero_cst (TREE_TYPE (a)));
    1277            0 :         }
    1278              :       else
    1279              :         {
    1280         6065 :           result = tree_vec_extract (gsi, cond_type, a, comp_width, comp_index);
    1281         6065 :           aa = gimplify_build2 (gsi, NE_EXPR, boolean_type_node, result,
    1282              :                                 build_zero_cst (cond_type));
    1283              :         }
    1284         6065 :       result = gimplify_build3 (gsi, COND_EXPR, inner_type, aa, bb, cc);
    1285         6065 :       if (!CONSTANT_CLASS_P (result))
    1286         5981 :         constant_p = false;
    1287         6065 :       constructor_elt ce = {NULL_TREE, result};
    1288         6065 :       v->quick_push (ce);
    1289         6065 :       index = int_const_binop (PLUS_EXPR, index, width);
    1290         6065 :       if (width == comp_width)
    1291              :         comp_index = index;
    1292              :       else
    1293            0 :         comp_index = int_const_binop (PLUS_EXPR, comp_index, comp_width);
    1294              :     }
    1295              : 
    1296          539 :   if (constant_p)
    1297            0 :     constr = build_vector_from_ctor (type, v);
    1298              :   else
    1299          539 :     constr = build_constructor (type, v);
    1300          539 :   gimple_assign_set_rhs_from_tree (gsi, constr);
    1301          539 :   update_stmt (gsi_stmt (*gsi));
    1302              : 
    1303          539 :   return false;
    1304              : }
    1305              : 
    1306              : static tree
    1307        31537 : expand_vector_operation (gimple_stmt_iterator *gsi, tree type, tree compute_type,
    1308              :                          gassign *assign, enum tree_code code)
    1309              : {
    1310        31537 :   machine_mode compute_mode = TYPE_MODE (compute_type);
    1311              : 
    1312              :   /* If the compute mode is not a vector mode (hence we are not decomposing
    1313              :      a BLKmode vector to smaller, hardware-supported vectors), we may want
    1314              :      to expand the operations in parallel.  */
    1315        31537 :   if (!VECTOR_MODE_P (compute_mode))
    1316        30556 :     switch (code)
    1317              :       {
    1318          351 :       case PLUS_EXPR:
    1319          351 :       case MINUS_EXPR:
    1320          351 :         if (ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_TRAPS (type))
    1321          404 :           return expand_vector_addition (gsi, do_binop, do_plus_minus, type,
    1322              :                                          gimple_assign_rhs1 (assign),
    1323          202 :                                          gimple_assign_rhs2 (assign), code);
    1324              :         break;
    1325              : 
    1326            9 :       case NEGATE_EXPR:
    1327            9 :         if (ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_TRAPS (type))
    1328            8 :           return expand_vector_addition (gsi, do_unop, do_negate, type,
    1329              :                                          gimple_assign_rhs1 (assign),
    1330            8 :                                          NULL_TREE, code);
    1331              :         break;
    1332              : 
    1333           92 :       case BIT_AND_EXPR:
    1334           92 :       case BIT_IOR_EXPR:
    1335           92 :       case BIT_XOR_EXPR:
    1336          184 :         return expand_vector_parallel (gsi, do_binop, type,
    1337              :                                        gimple_assign_rhs1 (assign),
    1338           92 :                                        gimple_assign_rhs2 (assign), code);
    1339              : 
    1340            9 :       case BIT_NOT_EXPR:
    1341            9 :         return expand_vector_parallel (gsi, do_unop, type,
    1342              :                                        gimple_assign_rhs1 (assign),
    1343            9 :                                        NULL_TREE, code);
    1344        28155 :       case EQ_EXPR:
    1345        28155 :       case NE_EXPR:
    1346        28155 :       case GT_EXPR:
    1347        28155 :       case LT_EXPR:
    1348        28155 :       case GE_EXPR:
    1349        28155 :       case LE_EXPR:
    1350        28155 :       case UNEQ_EXPR:
    1351        28155 :       case UNGT_EXPR:
    1352        28155 :       case UNLT_EXPR:
    1353        28155 :       case UNGE_EXPR:
    1354        28155 :       case UNLE_EXPR:
    1355        28155 :       case LTGT_EXPR:
    1356        28155 :       case ORDERED_EXPR:
    1357        28155 :       case UNORDERED_EXPR:
    1358        28155 :         {
    1359        28155 :           tree rhs1 = gimple_assign_rhs1 (assign);
    1360        28155 :           tree rhs2 = gimple_assign_rhs2 (assign);
    1361              : 
    1362        28155 :           return expand_vector_comparison (gsi, type, rhs1, rhs2, code);
    1363              :         }
    1364              : 
    1365          993 :       case TRUNC_DIV_EXPR:
    1366          993 :       case TRUNC_MOD_EXPR:
    1367          993 :         {
    1368          993 :           tree rhs1 = gimple_assign_rhs1 (assign);
    1369          993 :           tree rhs2 = gimple_assign_rhs2 (assign);
    1370          993 :           tree ret;
    1371              : 
    1372          993 :           if (!optimize
    1373          874 :               || !VECTOR_INTEGER_TYPE_P (type)
    1374          874 :               || TREE_CODE (rhs2) != VECTOR_CST
    1375         1635 :               || !VECTOR_MODE_P (TYPE_MODE (type)))
    1376              :             break;
    1377              : 
    1378          636 :           ret = expand_vector_divmod (gsi, type, rhs1, rhs2, code);
    1379          636 :           if (ret != NULL_TREE)
    1380              :             return ret;
    1381              :           break;
    1382              :         }
    1383          109 :       case MULT_EXPR:
    1384          109 :         {
    1385          109 :           tree rhs1 = gimple_assign_rhs1 (assign);
    1386          109 :           tree rhs2 = gimple_assign_rhs2 (assign);
    1387          109 :           tree ret;
    1388              : 
    1389          109 :           if (!optimize
    1390           87 :               || !VECTOR_INTEGER_TYPE_P (type)
    1391           66 :               || TREE_CODE (rhs2) != VECTOR_CST
    1392          117 :               || !VECTOR_MODE_P (TYPE_MODE (type)))
    1393              :             break;
    1394              : 
    1395            6 :           ret = expand_vector_mult (gsi, type, rhs1, rhs2);
    1396            6 :           if (ret != NULL_TREE)
    1397              :             return ret;
    1398              :           break;
    1399              :         }
    1400              :       default:
    1401              :         break;
    1402              :       }
    1403              : 
    1404         2674 :   if (TREE_CODE_CLASS (code) == tcc_unary)
    1405          140 :     return expand_vector_piecewise (gsi, do_unop, type, compute_type,
    1406              :                                     gimple_assign_rhs1 (assign),
    1407          140 :                                     NULL_TREE, code, false);
    1408              :   else
    1409         5068 :     return expand_vector_piecewise (gsi, do_binop, type, compute_type,
    1410              :                                     gimple_assign_rhs1 (assign),
    1411         2534 :                                     gimple_assign_rhs2 (assign), code, false);
    1412              : }
    1413              : 
    1414              : /* Try to optimize
    1415              :    a_5 = { b_7, b_7 + 3, b_7 + 6, b_7 + 9 };
    1416              :    style stmts into:
    1417              :    _9 = { b_7, b_7, b_7, b_7 };
    1418              :    a_5 = _9 + { 0, 3, 6, 9 };
    1419              :    because vector splat operation is usually more efficient
    1420              :    than piecewise initialization of the vector.  */
    1421              : 
    1422              : static void
    1423       148551 : optimize_vector_constructor (gimple_stmt_iterator *gsi)
    1424              : {
    1425       148551 :   gassign *stmt = as_a <gassign *> (gsi_stmt (*gsi));
    1426       148551 :   tree lhs = gimple_assign_lhs (stmt);
    1427       148551 :   tree rhs = gimple_assign_rhs1 (stmt);
    1428       148551 :   tree type = TREE_TYPE (rhs);
    1429       148551 :   unsigned int i, j;
    1430       148551 :   unsigned HOST_WIDE_INT nelts;
    1431       148551 :   bool all_same = true;
    1432       148551 :   constructor_elt *elt;
    1433       148551 :   gimple *g;
    1434       148551 :   tree base = NULL_TREE;
    1435       148551 :   optab op;
    1436              : 
    1437       150317 :   if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts)
    1438       148551 :       || nelts <= 2
    1439        49200 :       || CONSTRUCTOR_NELTS (rhs) != nelts)
    1440       148523 :     return;
    1441        22837 :   op = optab_for_tree_code (PLUS_EXPR, type, optab_default);
    1442        22837 :   if (op == unknown_optab
    1443        22837 :       || !can_implement_p (op, TYPE_MODE (type)))
    1444              :     return;
    1445       111684 :   FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (rhs), i, elt)
    1446        98424 :     if (TREE_CODE (elt->value) != SSA_NAME
    1447        98424 :         || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
    1448              :       return;
    1449              :     else
    1450              :       {
    1451        96255 :         tree this_base = elt->value;
    1452        96255 :         if (this_base != CONSTRUCTOR_ELT (rhs, 0)->value)
    1453         6958 :           all_same = false;
    1454        97449 :         for (j = 0; j < nelts + 1; j++)
    1455              :           {
    1456        97449 :             auto g = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (this_base));
    1457        61434 :             if (g
    1458        61434 :                 && gimple_assign_rhs_code (g) == PLUS_EXPR
    1459         2735 :                 && TREE_CODE (gimple_assign_rhs2 (g)) == INTEGER_CST
    1460         1194 :                 && TREE_CODE (gimple_assign_rhs1 (g)) == SSA_NAME
    1461         1194 :                 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (g)))
    1462         1194 :               this_base = gimple_assign_rhs1 (g);
    1463              :             else
    1464              :               break;
    1465              :           }
    1466        96255 :         if (i == 0)
    1467              :           base = this_base;
    1468        74484 :         else if (this_base != base)
    1469              :           return;
    1470              :       }
    1471        13260 :   if (all_same)
    1472              :     return;
    1473           28 :   tree_vector_builder cst (type, nelts, 1);
    1474          168 :   for (i = 0; i < nelts; i++)
    1475              :     {
    1476          112 :       tree this_base = CONSTRUCTOR_ELT (rhs, i)->value;
    1477          112 :       tree elt = build_zero_cst (TREE_TYPE (base));
    1478          204 :       while (this_base != base)
    1479              :         {
    1480           92 :           g = SSA_NAME_DEF_STMT (this_base);
    1481           92 :           elt = fold_binary (PLUS_EXPR, TREE_TYPE (base),
    1482              :                              elt, gimple_assign_rhs2 (g));
    1483           92 :           if (elt == NULL_TREE
    1484           92 :               || TREE_CODE (elt) != INTEGER_CST
    1485          184 :               || TREE_OVERFLOW (elt))
    1486            0 :             return;
    1487           92 :           this_base = gimple_assign_rhs1 (g);
    1488              :         }
    1489          112 :       cst.quick_push (elt);
    1490              :     }
    1491          140 :   for (i = 0; i < nelts; i++)
    1492          112 :     CONSTRUCTOR_ELT (rhs, i)->value = base;
    1493           28 :   g = gimple_build_assign (make_ssa_name (type), rhs);
    1494           28 :   gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1495           28 :   g = gimple_build_assign (lhs, PLUS_EXPR, gimple_assign_lhs (g),
    1496              :                            cst.build ());
    1497           28 :   gsi_replace (gsi, g, false);
    1498           28 : }
    1499              : 
    1500              : /* Return a type for the widest vector mode with the same element type as
    1501              :    type ORIGINAL_VECTOR_TYPE, with at most the same number of elements as type
    1502              :    ORIGINAL_VECTOR_TYPE and that is supported by the target for an operation
    1503              :    with optab OP, or return NULL_TREE if none is found.  */
    1504              : 
    1505              : static tree
    1506         3887 : type_for_widest_vector_mode (tree original_vector_type, optab op)
    1507              : {
    1508         3887 :   gcc_assert (VECTOR_TYPE_P (original_vector_type));
    1509         3887 :   tree type = TREE_TYPE (original_vector_type);
    1510         3887 :   machine_mode inner_mode = TYPE_MODE (type);
    1511         3887 :   machine_mode best_mode = VOIDmode, mode;
    1512         3887 :   poly_int64 best_nunits = 0;
    1513              : 
    1514         3887 :   if (SCALAR_FLOAT_MODE_P (inner_mode))
    1515              :     mode = MIN_MODE_VECTOR_FLOAT;
    1516         3524 :   else if (SCALAR_FRACT_MODE_P (inner_mode))
    1517              :     mode = MIN_MODE_VECTOR_FRACT;
    1518         3524 :   else if (SCALAR_UFRACT_MODE_P (inner_mode))
    1519              :     mode = MIN_MODE_VECTOR_UFRACT;
    1520         3524 :   else if (SCALAR_ACCUM_MODE_P (inner_mode))
    1521              :     mode = MIN_MODE_VECTOR_ACCUM;
    1522         3524 :   else if (SCALAR_UACCUM_MODE_P (inner_mode))
    1523              :     mode = MIN_MODE_VECTOR_UACCUM;
    1524         3524 :   else if (inner_mode == BImode)
    1525              :     mode = MIN_MODE_VECTOR_BOOL;
    1526              :   else
    1527         3887 :     mode = MIN_MODE_VECTOR_INT;
    1528              : 
    1529       122206 :   FOR_EACH_MODE_FROM (mode, mode)
    1530       118319 :     if (GET_MODE_INNER (mode) == inner_mode
    1531        45998 :         && maybe_gt (GET_MODE_NUNITS (mode), best_nunits)
    1532        22999 :         && can_implement_p (op, mode)
    1533       144338 :         && known_le (GET_MODE_NUNITS (mode),
    1534              :                      TYPE_VECTOR_SUBPARTS (original_vector_type)))
    1535         4482 :       best_mode = mode, best_nunits = GET_MODE_NUNITS (mode);
    1536              : 
    1537         3887 :   if (best_mode == VOIDmode)
    1538              :     return NULL_TREE;
    1539              :   else
    1540         1205 :     return build_vector_type_for_mode (type, best_mode);
    1541              : }
    1542              : 
    1543              : 
    1544              : /* Build a reference to the element of the vector VECT.  Function
    1545              :    returns either the element itself, either BIT_FIELD_REF, or an
    1546              :    ARRAY_REF expression.
    1547              : 
    1548              :    GSI is required to insert temporary variables while building a
    1549              :    reference to the element of the vector VECT.
    1550              : 
    1551              :    PTMPVEC is a pointer to the temporary variable for caching
    1552              :    purposes.  In case when PTMPVEC is NULL new temporary variable
    1553              :    will be created.  */
    1554              : static tree
    1555       134079 : vector_element (gimple_stmt_iterator *gsi, tree vect, tree idx, tree *ptmpvec)
    1556              : {
    1557       134079 :   tree vect_type, vect_elt_type;
    1558       134079 :   gimple *asgn;
    1559       134079 :   tree tmpvec;
    1560       134079 :   tree arraytype;
    1561       134079 :   bool need_asgn = true;
    1562       134079 :   unsigned int elements;
    1563              : 
    1564       134079 :   vect_type = TREE_TYPE (vect);
    1565       134079 :   vect_elt_type = TREE_TYPE (vect_type);
    1566       134079 :   elements = nunits_for_known_piecewise_op (vect_type);
    1567              : 
    1568       134079 :   if (TREE_CODE (idx) == INTEGER_CST)
    1569              :     {
    1570       124154 :       unsigned HOST_WIDE_INT index;
    1571              : 
    1572              :       /* Given that we're about to compute a binary modulus,
    1573              :          we don't care about the high bits of the value.  */
    1574       124154 :       index = TREE_INT_CST_LOW (idx);
    1575       124154 :       if (!tree_fits_uhwi_p (idx) || index >= elements)
    1576              :         {
    1577            0 :           index &= elements - 1;
    1578            0 :           idx = build_int_cst (TREE_TYPE (idx), index);
    1579              :         }
    1580              : 
    1581              :       /* When lowering a vector statement sequence do some easy
    1582              :          simplification by looking through intermediate vector results.  */
    1583       124154 :       if (TREE_CODE (vect) == SSA_NAME)
    1584              :         {
    1585        65443 :           auto *def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (vect));
    1586        65280 :           if (def_stmt
    1587        65280 :               && (gimple_assign_rhs_code (def_stmt) == VECTOR_CST
    1588        63993 :                   || gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR))
    1589         3965 :             vect = gimple_assign_rhs1 (def_stmt);
    1590              :         }
    1591              : 
    1592       124154 :       if (TREE_CODE (vect) == VECTOR_CST)
    1593        59998 :         return VECTOR_CST_ELT (vect, index);
    1594        64156 :       else if (TREE_CODE (vect) == CONSTRUCTOR
    1595        64156 :                && (CONSTRUCTOR_NELTS (vect) == 0
    1596         2678 :                    || TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (vect, 0)->value))
    1597              :                       != VECTOR_TYPE))
    1598              :         {
    1599          485 :           if (index < CONSTRUCTOR_NELTS (vect))
    1600          485 :             return CONSTRUCTOR_ELT (vect, index)->value;
    1601            0 :           return build_zero_cst (vect_elt_type);
    1602              :         }
    1603              :       else
    1604              :         {
    1605        63671 :           tree size = vector_element_bits_tree (vect_type);
    1606        63671 :           tree pos = fold_build2 (MULT_EXPR, bitsizetype, bitsize_int (index),
    1607              :                                   size);
    1608        63671 :           return fold_build3 (BIT_FIELD_REF, vect_elt_type, vect, size, pos);
    1609              :         }
    1610              :     }
    1611              : 
    1612         9925 :   if (!ptmpvec)
    1613            0 :     tmpvec = create_tmp_var (vect_type, "vectmp");
    1614         9925 :   else if (!*ptmpvec)
    1615         1296 :     tmpvec = *ptmpvec = create_tmp_var (vect_type, "vectmp");
    1616              :   else
    1617              :     {
    1618              :       tmpvec = *ptmpvec;
    1619              :       need_asgn = false;
    1620              :     }
    1621              : 
    1622         1296 :   if (need_asgn)
    1623              :     {
    1624         1296 :       TREE_ADDRESSABLE (tmpvec) = 1;
    1625         1296 :       asgn = gimple_build_assign (tmpvec, vect);
    1626         1296 :       gsi_insert_before (gsi, asgn, GSI_SAME_STMT);
    1627              :     }
    1628              : 
    1629         9925 :   arraytype = build_array_type_nelts (vect_elt_type, elements);
    1630         9925 :   return build4 (ARRAY_REF, vect_elt_type,
    1631              :                  build1 (VIEW_CONVERT_EXPR, arraytype, tmpvec),
    1632         9925 :                  idx, NULL_TREE, NULL_TREE);
    1633              : }
    1634              : 
    1635              : /* Check if VEC_PERM_EXPR within the given setting is supported
    1636              :    by hardware, or lower it piecewise.
    1637              : 
    1638              :    When VEC_PERM_EXPR has the same first and second operands:
    1639              :    VEC_PERM_EXPR <v0, v0, mask> the lowered version would be
    1640              :    {v0[mask[0]], v0[mask[1]], ...}
    1641              :    MASK and V0 must have the same number of elements.
    1642              : 
    1643              :    Otherwise VEC_PERM_EXPR <v0, v1, mask> is lowered to
    1644              :    {mask[0] < len(v0) ? v0[mask[0]] : v1[mask[0]], ...}
    1645              :    V0 and V1 must have the same type.  MASK, V0, V1 must have the
    1646              :    same number of arguments.  */
    1647              : 
    1648              : static void
    1649        92806 : lower_vec_perm (gimple_stmt_iterator *gsi)
    1650              : {
    1651        92806 :   gassign *stmt = as_a <gassign *> (gsi_stmt (*gsi));
    1652        92806 :   tree mask = gimple_assign_rhs3 (stmt);
    1653        92806 :   tree vec0 = gimple_assign_rhs1 (stmt);
    1654        92806 :   tree vec1 = gimple_assign_rhs2 (stmt);
    1655        92806 :   tree res_vect_type = TREE_TYPE (gimple_assign_lhs (stmt));
    1656        92806 :   tree vect_type = TREE_TYPE (vec0);
    1657        92806 :   tree mask_type = TREE_TYPE (mask);
    1658        92806 :   tree vect_elt_type = TREE_TYPE (vect_type);
    1659              :   /* Perform the index computations in an unsigned type, the mask
    1660              :      elements are interpreted modulo the number of elements and with
    1661              :      a signed type masking with ELEMENTS - 1 can be a no-op when
    1662              :      ELEMENTS - 1 is not representable, leaving negative indices.  */
    1663        92806 :   tree mask_elt_type = unsigned_type_for (TREE_TYPE (mask_type));
    1664        92806 :   unsigned HOST_WIDE_INT elements, in_elements;
    1665        92806 :   vec<constructor_elt, va_gc> *v;
    1666        92806 :   tree constr, t, si, i_val;
    1667        92806 :   tree vec0tmp = NULL_TREE, vec1tmp = NULL_TREE, masktmp = NULL_TREE;
    1668        92806 :   bool two_operand_p = !operand_equal_p (vec0, vec1, 0);
    1669        92806 :   location_t loc = gimple_location (gsi_stmt (*gsi));
    1670        92806 :   unsigned i;
    1671              : 
    1672        92806 :   if (!TYPE_VECTOR_SUBPARTS (res_vect_type).is_constant (&elements)
    1673        92806 :       || !TYPE_VECTOR_SUBPARTS (vect_type).is_constant (&in_elements))
    1674        83246 :     return;
    1675              : 
    1676        92806 :   if (TREE_CODE (mask) == SSA_NAME)
    1677              :     {
    1678          901 :       auto *def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (mask));
    1679         1772 :       if (def_stmt && gimple_assign_rhs_code (def_stmt) == VECTOR_CST)
    1680           13 :         mask = gimple_assign_rhs1 (def_stmt);
    1681              :     }
    1682              : 
    1683              : 
    1684        92806 :   vec_perm_indices indices;
    1685        92806 :   if (TREE_CODE (mask) == VECTOR_CST
    1686        92806 :       && tree_to_vec_perm_indices (&indices, vec0, vec1, mask))
    1687              :     {
    1688        91917 :       machine_mode vmode = TYPE_MODE (vect_type);
    1689        91917 :       tree lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
    1690        91917 :       machine_mode lhs_mode = TYPE_MODE (lhs_type);
    1691        91917 :       if (can_vec_perm_const_p (lhs_mode, vmode, indices, true))
    1692              :         {
    1693        83227 :           gimple_assign_set_rhs3 (stmt, mask);
    1694        83227 :           update_stmt (stmt);
    1695        83227 :           return;
    1696              :         }
    1697              :     }
    1698          889 :   else if (can_vec_perm_var_p (TYPE_MODE (vect_type)))
    1699              :     return;
    1700              : 
    1701         9560 :   if (!warning_suppressed_p (stmt, OPT_Wvector_operation_performance))
    1702         9560 :     warning_at (loc, OPT_Wvector_operation_performance,
    1703              :                 "vector shuffling operation will be expanded piecewise");
    1704              : 
    1705         9560 :   vec_alloc (v, elements);
    1706         9560 :   bool constant_p = true;
    1707        84920 :   for (i = 0; i < elements; i++)
    1708              :     {
    1709        65800 :       si = size_int (i);
    1710        65800 :       i_val = vector_element (gsi, mask, si, &masktmp);
    1711        65800 :       i_val = fold_convert (mask_elt_type, i_val);
    1712              : 
    1713        65800 :       if (TREE_CODE (i_val) == INTEGER_CST)
    1714              :         {
    1715        58354 :           unsigned HOST_WIDE_INT index;
    1716              : 
    1717        58354 :           index = TREE_INT_CST_LOW (i_val);
    1718        58354 :           if (!tree_fits_uhwi_p (i_val) || index >= in_elements)
    1719        18903 :             i_val = build_int_cst (mask_elt_type, index & (in_elements - 1));
    1720              : 
    1721        58354 :           if (two_operand_p && (index & in_elements) != 0)
    1722        18771 :             t = vector_element (gsi, vec1, i_val, &vec1tmp);
    1723              :           else
    1724        39583 :             t = vector_element (gsi, vec0, i_val, &vec0tmp);
    1725              : 
    1726        58354 :           t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
    1727              :                                         true, GSI_SAME_STMT);
    1728              :         }
    1729              :       else
    1730              :         {
    1731         7446 :           tree cond = NULL_TREE, v0_val;
    1732              : 
    1733         7446 :           if (two_operand_p)
    1734              :             {
    1735         2479 :               cond = fold_build2 (BIT_AND_EXPR, mask_elt_type, i_val,
    1736              :                                   build_int_cst (mask_elt_type, elements));
    1737         2479 :               cond = force_gimple_operand_gsi (gsi, cond, true, NULL_TREE,
    1738              :                                                true, GSI_SAME_STMT);
    1739              :             }
    1740              : 
    1741         7446 :           i_val = fold_build2 (BIT_AND_EXPR, mask_elt_type, i_val,
    1742              :                                build_int_cst (mask_elt_type, elements - 1));
    1743         7446 :           i_val = force_gimple_operand_gsi (gsi, i_val, true, NULL_TREE,
    1744              :                                             true, GSI_SAME_STMT);
    1745              : 
    1746         7446 :           v0_val = vector_element (gsi, vec0, i_val, &vec0tmp);
    1747         7446 :           v0_val = force_gimple_operand_gsi (gsi, v0_val, true, NULL_TREE,
    1748              :                                              true, GSI_SAME_STMT);
    1749              : 
    1750         7446 :           if (two_operand_p)
    1751              :             {
    1752         2479 :               tree v1_val;
    1753              : 
    1754         2479 :               v1_val = vector_element (gsi, vec1, i_val, &vec1tmp);
    1755         2479 :               v1_val = force_gimple_operand_gsi (gsi, v1_val, true, NULL_TREE,
    1756              :                                                  true, GSI_SAME_STMT);
    1757              : 
    1758         2479 :               cond = fold_build2 (EQ_EXPR, boolean_type_node,
    1759              :                                   cond, build_zero_cst (mask_elt_type));
    1760         2479 :               cond = fold_build3 (COND_EXPR, vect_elt_type,
    1761              :                                   cond, v0_val, v1_val);
    1762         2479 :               t = force_gimple_operand_gsi (gsi, cond, true, NULL_TREE,
    1763              :                                             true, GSI_SAME_STMT);
    1764              :             }
    1765              :           else
    1766              :             t = v0_val;
    1767              :         }
    1768              : 
    1769        65800 :       if (!CONSTANT_CLASS_P (t))
    1770        63533 :         constant_p = false;
    1771        65800 :       CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t);
    1772              :     }
    1773              : 
    1774         9560 :   if (constant_p)
    1775            8 :     constr = build_vector_from_ctor (res_vect_type, v);
    1776              :   else
    1777         9552 :     constr = build_constructor (res_vect_type, v);
    1778         9560 :   gimple_assign_set_rhs_from_tree (gsi, constr);
    1779         9560 :   update_stmt (gsi_stmt (*gsi));
    1780        92806 : }
    1781              : 
    1782              : /* Return the type that should be used to implement OP on type TYPE.
    1783              :    This is TYPE itself if the target can do the operation directly,
    1784              :    otherwise it is a scalar type or a smaller vector type.  */
    1785              : 
    1786              : static tree
    1787       237902 : get_compute_type (optab op, tree type)
    1788              : {
    1789       237902 :   if (op)
    1790              :     {
    1791       419494 :       if (VECTOR_MODE_P (TYPE_MODE (type))
    1792       417866 :           && can_implement_p (op, TYPE_MODE (type)))
    1793              :         return type;
    1794              : 
    1795              :       /* For very wide vectors, try using a smaller vector mode.  */
    1796         3810 :       tree vector_compute_type = type_for_widest_vector_mode (type, op);
    1797         3810 :       if (vector_compute_type != NULL_TREE
    1798         3810 :           && maybe_ne (TYPE_VECTOR_SUBPARTS (vector_compute_type), 1U)
    1799         4883 :           && can_implement_p (op, TYPE_MODE (vector_compute_type)))
    1800         1073 :         return vector_compute_type;
    1801              :     }
    1802              : 
    1803              :   /* There is no operation in hardware, so fall back to scalars.  */
    1804        30892 :   return TREE_TYPE (type);
    1805              : }
    1806              : 
    1807              : static tree
    1808           14 : do_cond (gimple_stmt_iterator *gsi, tree inner_type, tree a, tree b,
    1809              :          tree bitpos, tree bitsize, enum tree_code code,
    1810              :          tree type ATTRIBUTE_UNUSED)
    1811              : {
    1812           14 :   if (VECTOR_TYPE_P (TREE_TYPE (a)))
    1813           14 :     a = tree_vec_extract (gsi, inner_type, a, bitsize, bitpos);
    1814           14 :   if (VECTOR_TYPE_P (TREE_TYPE (b)))
    1815           14 :     b = tree_vec_extract (gsi, inner_type, b, bitsize, bitpos);
    1816           14 :   tree cond = gimple_assign_rhs1 (gsi_stmt (*gsi));
    1817           14 :   return gimplify_build3 (gsi, code, inner_type, unshare_expr (cond), a, b);
    1818              : }
    1819              : 
    1820              : /* Expand a vector COND_EXPR to scalars, piecewise.  */
    1821              : static void
    1822            7 : expand_vector_scalar_condition (gimple_stmt_iterator *gsi)
    1823              : {
    1824            7 :   gassign *stmt = as_a <gassign *> (gsi_stmt (*gsi));
    1825            7 :   tree lhs = gimple_assign_lhs (stmt);
    1826            7 :   tree type = TREE_TYPE (lhs);
    1827            7 :   tree compute_type = get_compute_type (mov_optab, type);
    1828            7 :   machine_mode compute_mode = TYPE_MODE (compute_type);
    1829            7 :   gcc_assert (compute_mode != BLKmode);
    1830            7 :   tree rhs2 = gimple_assign_rhs2 (stmt);
    1831            7 :   tree rhs3 = gimple_assign_rhs3 (stmt);
    1832            7 :   tree new_rhs;
    1833              : 
    1834              :   /* If the compute mode is not a vector mode (hence we are not decomposing
    1835              :      a BLKmode vector to smaller, hardware-supported vectors), we may want
    1836              :      to expand the operations in parallel.  */
    1837            7 :   if (!VECTOR_MODE_P (compute_mode))
    1838            0 :     new_rhs = expand_vector_parallel (gsi, do_cond, type, rhs2, rhs3,
    1839              :                                       COND_EXPR);
    1840              :   else
    1841            7 :     new_rhs = expand_vector_piecewise (gsi, do_cond, type, compute_type,
    1842              :                                        rhs2, rhs3, COND_EXPR, false);
    1843            7 :   if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (new_rhs)))
    1844            0 :     new_rhs = gimplify_build1 (gsi, VIEW_CONVERT_EXPR, TREE_TYPE (lhs),
    1845              :                                new_rhs);
    1846              : 
    1847              :   /* NOTE:  We should avoid using gimple_assign_set_rhs_from_tree. One
    1848              :      way to do it is change expand_vector_operation and its callees to
    1849              :      return a tree_code, RHS1 and RHS2 instead of a tree. */
    1850            7 :   gimple_assign_set_rhs_from_tree (gsi, new_rhs);
    1851            7 :   update_stmt (gsi_stmt (*gsi));
    1852            7 : }
    1853              : 
    1854              : /* Callback for expand_vector_piecewise to do VEC_CONVERT ifn call
    1855              :    lowering.  If INNER_TYPE is not a vector type, this is a scalar
    1856              :    fallback.  */
    1857              : 
    1858              : static tree
    1859        18972 : do_vec_conversion (gimple_stmt_iterator *gsi, tree inner_type, tree a,
    1860              :                    tree decl, tree bitpos, tree bitsize,
    1861              :                    enum tree_code code, tree type)
    1862              : {
    1863        18972 :   a = tree_vec_extract (gsi, inner_type, a, bitsize, bitpos);
    1864        18972 :   if (!VECTOR_TYPE_P (inner_type))
    1865        18972 :     return gimplify_build1 (gsi, code, TREE_TYPE (type), a);
    1866            0 :   if (code == CALL_EXPR)
    1867              :     {
    1868            0 :       gimple *g = gimple_build_call (decl, 1, a);
    1869            0 :       tree lhs = make_ssa_name (TREE_TYPE (TREE_TYPE (decl)));
    1870            0 :       gimple_call_set_lhs (g, lhs);
    1871            0 :       gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1872            0 :       return lhs;
    1873              :     }
    1874              :   else
    1875              :     {
    1876            0 :       tree outer_type = build_vector_type (TREE_TYPE (type),
    1877            0 :                                            TYPE_VECTOR_SUBPARTS (inner_type));
    1878            0 :       return gimplify_build1 (gsi, code, outer_type, a);
    1879              :     }
    1880              : }
    1881              : 
    1882              : /* Similarly, but for narrowing conversion.  */
    1883              : 
    1884              : static tree
    1885           53 : do_vec_narrow_conversion (gimple_stmt_iterator *gsi, tree inner_type, tree a,
    1886              :                           tree, tree bitpos, tree, enum tree_code code,
    1887              :                           tree type)
    1888              : {
    1889           53 :   tree itype = build_vector_type (TREE_TYPE (inner_type),
    1890           53 :                                   exact_div (TYPE_VECTOR_SUBPARTS (inner_type),
    1891              :                                              2));
    1892           53 :   tree b = tree_vec_extract (gsi, itype, a, TYPE_SIZE (itype), bitpos);
    1893           53 :   tree c = tree_vec_extract (gsi, itype, a, TYPE_SIZE (itype),
    1894              :                              int_const_binop (PLUS_EXPR, bitpos,
    1895           53 :                                               TYPE_SIZE (itype)));
    1896           53 :   tree outer_type = build_vector_type (TREE_TYPE (type),
    1897           53 :                                        TYPE_VECTOR_SUBPARTS (inner_type));
    1898           53 :   return gimplify_build2 (gsi, code, outer_type, b, c);
    1899              : }
    1900              : 
    1901              : /* Expand VEC_CONVERT ifn call.  */
    1902              : 
    1903              : static void
    1904         1201 : expand_vector_conversion (gimple_stmt_iterator *gsi)
    1905              : {
    1906         1201 :   gimple *stmt = gsi_stmt (*gsi);
    1907         1201 :   gimple *g;
    1908         1201 :   tree lhs = gimple_call_lhs (stmt);
    1909         1201 :   if (lhs == NULL_TREE)
    1910              :     {
    1911            1 :       g = gimple_build_nop ();
    1912            1 :       gsi_replace (gsi, g, false);
    1913          855 :       return;
    1914              :     }
    1915         1200 :   tree arg = gimple_call_arg (stmt, 0);
    1916         1200 :   tree ret_type = TREE_TYPE (lhs);
    1917         1200 :   tree arg_type = TREE_TYPE (arg);
    1918         1200 :   tree new_rhs, new_lhs, compute_type = TREE_TYPE (arg_type);
    1919         1200 :   enum tree_code code = NOP_EXPR;
    1920         1200 :   enum tree_code code1 = ERROR_MARK;
    1921         1200 :   enum { NARROW, NONE, WIDEN } modifier = NONE;
    1922         1200 :   optab optab1 = unknown_optab;
    1923              : 
    1924         1200 :   gcc_checking_assert (VECTOR_TYPE_P (ret_type) && VECTOR_TYPE_P (arg_type));
    1925         2400 :   if (INTEGRAL_TYPE_P (TREE_TYPE (ret_type))
    1926         2028 :       && SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg_type)))
    1927              :     code = FIX_TRUNC_EXPR;
    1928         1884 :   else if (INTEGRAL_TYPE_P (TREE_TYPE (arg_type))
    1929         1788 :            && SCALAR_FLOAT_TYPE_P (TREE_TYPE (ret_type)))
    1930              :     code = FLOAT_EXPR;
    1931         1200 :   unsigned int ret_elt_bits = vector_element_bits (ret_type);
    1932         1200 :   unsigned int arg_elt_bits = vector_element_bits (arg_type);
    1933         1200 :   if (ret_elt_bits < arg_elt_bits)
    1934              :     modifier = NARROW;
    1935          740 :   else if (ret_elt_bits > arg_elt_bits)
    1936          476 :     modifier = WIDEN;
    1937              : 
    1938         1200 :   auto_vec<std::pair<tree, tree_code>, 2> converts;
    1939         1200 :   if (supportable_indirect_convert_operation (code, ret_type, arg_type,
    1940              :                                               converts))
    1941              :     {
    1942          692 :       new_rhs = arg;
    1943          692 :       gcc_assert (!converts.is_empty ());
    1944         1520 :       for (unsigned int i = 0; i < converts.length () - 1; i++)
    1945              :         {
    1946           68 :           new_lhs = make_ssa_name (converts[i].first);
    1947           68 :           g = gimple_build_assign (new_lhs, converts[i].second, new_rhs);
    1948           68 :           new_rhs = new_lhs;
    1949           68 :           gsi_insert_before (gsi, g, GSI_SAME_STMT);
    1950              :         }
    1951         2076 :       g = gimple_build_assign (lhs,
    1952          692 :                                converts[converts.length() - 1].second,
    1953              :                                new_rhs);
    1954          692 :       gsi_replace (gsi, g, false);
    1955          692 :       return;
    1956              :     }
    1957              : 
    1958          508 :   if (modifier == NONE && (code == FIX_TRUNC_EXPR || code == FLOAT_EXPR))
    1959              :     {
    1960              :       /* Can't use get_compute_type here, as supportable_convert_operation
    1961              :          doesn't necessarily use an optab and needs two arguments.  */
    1962           77 :       tree vec_compute_type
    1963           77 :         = type_for_widest_vector_mode (arg_type, mov_optab);
    1964           77 :       if (vec_compute_type
    1965           77 :           && VECTOR_MODE_P (TYPE_MODE (vec_compute_type)))
    1966              :         {
    1967           77 :           unsigned HOST_WIDE_INT nelts
    1968           77 :             = constant_lower_bound (TYPE_VECTOR_SUBPARTS (vec_compute_type));
    1969          156 :           while (nelts > 1)
    1970              :             {
    1971           79 :               tree ret1_type = build_vector_type (TREE_TYPE (ret_type), nelts);
    1972           79 :               tree arg1_type = build_vector_type (TREE_TYPE (arg_type), nelts);
    1973           79 :               if (supportable_convert_operation (code, ret1_type, arg1_type))
    1974              :                 {
    1975            0 :                   new_rhs = expand_vector_piecewise (gsi, do_vec_conversion,
    1976              :                                                      ret_type, arg1_type, arg,
    1977              :                                                      NULL_TREE, code, false);
    1978            0 :                   g = gimple_build_assign (lhs, new_rhs);
    1979            0 :                   gsi_replace (gsi, g, false);
    1980            0 :                   return;
    1981              :                 }
    1982           79 :               nelts = nelts / 2;
    1983              :             }
    1984              :         }
    1985              :     }
    1986          431 :   else if (modifier == NARROW)
    1987              :     {
    1988          270 :       switch (code)
    1989              :         {
    1990          210 :         CASE_CONVERT:
    1991          210 :           code1 = VEC_PACK_TRUNC_EXPR;
    1992          210 :           optab1 = optab_for_tree_code (code1, arg_type, optab_default);
    1993          210 :           break;
    1994           50 :         case FIX_TRUNC_EXPR:
    1995           50 :           code1 = VEC_PACK_FIX_TRUNC_EXPR;
    1996              :           /* The signedness is determined from output operand.  */
    1997           50 :           optab1 = optab_for_tree_code (code1, ret_type, optab_default);
    1998           50 :           break;
    1999           10 :         case FLOAT_EXPR:
    2000           10 :           code1 = VEC_PACK_FLOAT_EXPR;
    2001           10 :           optab1 = optab_for_tree_code (code1, arg_type, optab_default);
    2002           10 :           break;
    2003              :         default:
    2004              :           gcc_unreachable ();
    2005              :         }
    2006              : 
    2007          270 :       if (optab1)
    2008          270 :         compute_type = get_compute_type (optab1, arg_type);
    2009          270 :       enum insn_code icode1;
    2010          270 :       if (VECTOR_TYPE_P (compute_type)
    2011          259 :           && ((icode1 = optab_handler (optab1, TYPE_MODE (compute_type)))
    2012              :               != CODE_FOR_nothing)
    2013          529 :           && VECTOR_MODE_P (insn_data[icode1].operand[0].mode))
    2014              :         {
    2015          259 :           tree cretd_type
    2016          259 :             = build_vector_type (TREE_TYPE (ret_type),
    2017          259 :                                  TYPE_VECTOR_SUBPARTS (compute_type) * 2);
    2018          259 :           if (insn_data[icode1].operand[0].mode == TYPE_MODE (cretd_type))
    2019              :             {
    2020          127 :               if (compute_type == arg_type)
    2021              :                 {
    2022           81 :                   new_rhs = gimplify_build2 (gsi, code1, cretd_type,
    2023              :                                              arg, build_zero_cst (arg_type));
    2024          162 :                   new_rhs = tree_vec_extract (gsi, ret_type, new_rhs,
    2025           81 :                                               TYPE_SIZE (ret_type),
    2026              :                                               bitsize_int (0));
    2027           81 :                   g = gimple_build_assign (lhs, new_rhs);
    2028           81 :                   gsi_replace (gsi, g, false);
    2029           81 :                   return;
    2030              :                 }
    2031           46 :               tree dcompute_type
    2032           46 :                 = build_vector_type (TREE_TYPE (compute_type),
    2033           46 :                                      TYPE_VECTOR_SUBPARTS (compute_type) * 2);
    2034           46 :               if (TYPE_MAIN_VARIANT (dcompute_type)
    2035           46 :                   == TYPE_MAIN_VARIANT (arg_type))
    2036           37 :                 new_rhs = do_vec_narrow_conversion (gsi, dcompute_type, arg,
    2037              :                                                     NULL_TREE, bitsize_int (0),
    2038              :                                                     NULL_TREE, code1,
    2039              :                                                     ret_type);
    2040              :               else
    2041            9 :                 new_rhs = expand_vector_piecewise (gsi,
    2042              :                                                    do_vec_narrow_conversion,
    2043              :                                                    arg_type, dcompute_type,
    2044              :                                                    arg, NULL_TREE, code1,
    2045              :                                                    false, ret_type);
    2046           46 :               g = gimple_build_assign (lhs, new_rhs);
    2047           46 :               gsi_replace (gsi, g, false);
    2048           46 :               return;
    2049              :             }
    2050              :         }
    2051              :     }
    2052          161 :   else if (modifier == WIDEN)
    2053              :     {
    2054          158 :       enum tree_code code2 = ERROR_MARK;
    2055          158 :       optab optab2 = unknown_optab;
    2056          158 :       switch (code)
    2057              :         {
    2058              :         CASE_CONVERT:
    2059              :           code1 = VEC_UNPACK_LO_EXPR;
    2060              :           code2 = VEC_UNPACK_HI_EXPR;
    2061              :           break;
    2062            0 :         case FIX_TRUNC_EXPR:
    2063            0 :           code1 = VEC_UNPACK_FIX_TRUNC_LO_EXPR;
    2064            0 :           code2 = VEC_UNPACK_FIX_TRUNC_HI_EXPR;
    2065            0 :           break;
    2066              :         case FLOAT_EXPR:
    2067              :           code1 = VEC_UNPACK_FLOAT_LO_EXPR;
    2068              :           code2 = VEC_UNPACK_FLOAT_HI_EXPR;
    2069              :           break;
    2070              :         default:
    2071              :           gcc_unreachable ();
    2072              :         }
    2073           68 :       if (BYTES_BIG_ENDIAN)
    2074              :         std::swap (code1, code2);
    2075              : 
    2076           68 :       if (code == FIX_TRUNC_EXPR)
    2077              :         {
    2078              :           /* The signedness is determined from output operand.  */
    2079            0 :           optab1 = optab_for_tree_code (code1, ret_type, optab_default);
    2080            0 :           optab2 = optab_for_tree_code (code2, ret_type, optab_default);
    2081              :         }
    2082              :       else
    2083              :         {
    2084          158 :           optab1 = optab_for_tree_code (code1, arg_type, optab_default);
    2085          158 :           optab2 = optab_for_tree_code (code2, arg_type, optab_default);
    2086              :         }
    2087              : 
    2088          158 :       if (optab1 && optab2)
    2089          158 :         compute_type = get_compute_type (optab1, arg_type);
    2090              : 
    2091          158 :       enum insn_code icode1, icode2;
    2092          158 :       if (VECTOR_TYPE_P (compute_type)
    2093           54 :           && ((icode1 = optab_handler (optab1, TYPE_MODE (compute_type)))
    2094              :               != CODE_FOR_nothing)
    2095           54 :           && ((icode2 = optab_handler (optab2, TYPE_MODE (compute_type)))
    2096              :               != CODE_FOR_nothing)
    2097           54 :           && VECTOR_MODE_P (insn_data[icode1].operand[0].mode)
    2098          158 :           && (insn_data[icode1].operand[0].mode
    2099           54 :               == insn_data[icode2].operand[0].mode))
    2100              :         {
    2101           54 :           poly_uint64 nunits
    2102           54 :             = exact_div (TYPE_VECTOR_SUBPARTS (compute_type), 2);
    2103           54 :           tree cretd_type = build_vector_type (TREE_TYPE (ret_type), nunits);
    2104           54 :           if (insn_data[icode1].operand[0].mode == TYPE_MODE (cretd_type))
    2105              :             {
    2106           34 :               vec<constructor_elt, va_gc> *v;
    2107           34 :               tree part_width = TYPE_SIZE (compute_type);
    2108           34 :               tree index = bitsize_int (0);
    2109           34 :               int nunits = nunits_for_known_piecewise_op (arg_type);
    2110           34 :               int delta = tree_to_uhwi (part_width) / arg_elt_bits;
    2111           34 :               int i;
    2112           34 :               location_t loc = gimple_location (gsi_stmt (*gsi));
    2113              : 
    2114           34 :               if (compute_type != arg_type)
    2115              :                 {
    2116            0 :                   if (!warning_suppressed_p (gsi_stmt (*gsi),
    2117              :                                              OPT_Wvector_operation_performance))
    2118            0 :                     warning_at (loc, OPT_Wvector_operation_performance,
    2119              :                                 "vector operation will be expanded piecewise");
    2120              :                 }
    2121              :               else
    2122              :                 {
    2123              :                   nunits = 1;
    2124              :                   delta = 1;
    2125              :                 }
    2126              : 
    2127           34 :               vec_alloc (v, (nunits + delta - 1) / delta * 2);
    2128           34 :               bool constant_p = true;
    2129          102 :               for (i = 0; i < nunits;
    2130           34 :                    i += delta, index = int_const_binop (PLUS_EXPR, index,
    2131              :                                                         part_width))
    2132              :                 {
    2133           34 :                   tree a = arg;
    2134           34 :                   if (compute_type != arg_type)
    2135            0 :                     a = tree_vec_extract (gsi, compute_type, a, part_width,
    2136              :                                           index);
    2137           34 :                   tree result = gimplify_build1 (gsi, code1, cretd_type, a);
    2138           34 :                   constructor_elt ce = { NULL_TREE, result };
    2139           34 :                   if (!CONSTANT_CLASS_P (ce.value))
    2140           34 :                     constant_p = false;
    2141           34 :                   v->quick_push (ce);
    2142           34 :                   ce.value = gimplify_build1 (gsi, code2, cretd_type, a);
    2143           34 :                   if (!CONSTANT_CLASS_P (ce.value))
    2144           34 :                     constant_p = false;
    2145           34 :                   v->quick_push (ce);
    2146              :                 }
    2147              : 
    2148           34 :               if (constant_p)
    2149            0 :                 new_rhs = build_vector_from_ctor (ret_type, v);
    2150              :               else
    2151           34 :                 new_rhs = build_constructor (ret_type, v);
    2152           34 :               g = gimple_build_assign (lhs, new_rhs);
    2153           34 :               gsi_replace (gsi, g, false);
    2154           34 :               return;
    2155              :             }
    2156              :         }
    2157              :     }
    2158              : 
    2159          347 :   new_rhs = expand_vector_piecewise (gsi, do_vec_conversion, arg_type,
    2160          347 :                                      TREE_TYPE (arg_type), arg,
    2161              :                                      NULL_TREE, code, false, ret_type);
    2162          347 :   g = gimple_build_assign (lhs, new_rhs);
    2163          347 :   gsi_replace (gsi, g, false);
    2164         1200 : }
    2165              : 
    2166              : /* Process one statement.  If we identify a vector operation, expand it.  */
    2167              : 
    2168              : static void
    2169    103293019 : expand_vector_operations_1 (gimple_stmt_iterator *gsi)
    2170              : {
    2171    103293019 :   tree lhs, rhs1, rhs2 = NULL, type, compute_type = NULL_TREE;
    2172    103293019 :   enum tree_code code;
    2173    103293019 :   optab op = unknown_optab;
    2174    103293019 :   enum gimple_rhs_class rhs_class;
    2175    103293019 :   tree new_rhs;
    2176              : 
    2177              :   /* Only consider code == GIMPLE_ASSIGN. */
    2178    103293019 :   gassign *stmt = dyn_cast <gassign *> (gsi_stmt (*gsi));
    2179    103293019 :   if (!stmt)
    2180              :     {
    2181     68607604 :       if (gimple_call_internal_p (gsi_stmt (*gsi), IFN_VEC_CONVERT))
    2182         1201 :         expand_vector_conversion (gsi);
    2183              :       return;
    2184              :     }
    2185              : 
    2186     34685415 :   code = gimple_assign_rhs_code (stmt);
    2187     34685415 :   rhs_class = get_gimple_rhs_class (code);
    2188     34685415 :   lhs = gimple_assign_lhs (stmt);
    2189              : 
    2190     34685415 :   if (code == VEC_PERM_EXPR)
    2191              :     {
    2192        92806 :       lower_vec_perm (gsi);
    2193        92806 :       return;
    2194              :     }
    2195              : 
    2196     34592609 :   if (code == VEC_COND_EXPR)
    2197              :     {
    2198        24336 :       expand_vector_condition (gsi);
    2199        24336 :       return;
    2200              :     }
    2201              : 
    2202     34568273 :   if (code == COND_EXPR
    2203        14204 :       && TREE_CODE (TREE_TYPE (gimple_assign_lhs (stmt))) == VECTOR_TYPE
    2204     34568280 :       && TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt))) == BLKmode)
    2205              :     {
    2206            7 :       expand_vector_scalar_condition (gsi);
    2207            7 :       return;
    2208              :     }
    2209              : 
    2210     34568266 :   if (code == CONSTRUCTOR
    2211      1911193 :       && TREE_CODE (lhs) == SSA_NAME
    2212       151294 :       && VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (lhs)))
    2213       150518 :       && !gimple_clobber_p (stmt)
    2214     34718784 :       && optimize)
    2215              :     {
    2216       148551 :       optimize_vector_constructor (gsi);
    2217       148551 :       return;
    2218              :     }
    2219              : 
    2220     34419715 :   if (rhs_class != GIMPLE_UNARY_RHS && rhs_class != GIMPLE_BINARY_RHS)
    2221              :     return;
    2222              : 
    2223     12687945 :   rhs1 = gimple_assign_rhs1 (stmt);
    2224     12687945 :   if (rhs_class == GIMPLE_BINARY_RHS)
    2225      8607311 :     rhs2 = gimple_assign_rhs2 (stmt);
    2226              : 
    2227     12687945 :   type = TREE_TYPE (lhs);
    2228     12687945 :   if (!VECTOR_TYPE_P (type)
    2229     12687945 :       || !VECTOR_TYPE_P (TREE_TYPE (rhs1)))
    2230              :     return;
    2231              : 
    2232              :   /* A scalar operation pretending to be a vector one.  */
    2233       279998 :   if (VECTOR_BOOLEAN_TYPE_P (type)
    2234        39268 :       && !VECTOR_MODE_P (TYPE_MODE (type))
    2235        12004 :       && TYPE_MODE (type) != BLKmode
    2236        11670 :       && (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) != tcc_comparison
    2237         6613 :           || (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (rhs1))
    2238            0 :               && !VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (rhs1)))
    2239            0 :               && TYPE_MODE (TREE_TYPE (rhs1)) != BLKmode)))
    2240              :     return;
    2241              : 
    2242              :   /* If the vector operation is operating on all same vector elements
    2243              :      implement it with a scalar operation and a splat if the target
    2244              :      supports the scalar operation.  */
    2245       274941 :   tree srhs1, srhs2 = NULL_TREE;
    2246       274941 :   if ((srhs1 = ssa_uniform_vector_p (rhs1)) != NULL_TREE
    2247        18809 :       && (rhs2 == NULL_TREE
    2248        18568 :           || (! VECTOR_TYPE_P (TREE_TYPE (rhs2))
    2249              :               && (srhs2 = rhs2))
    2250        18548 :           || (srhs2 = ssa_uniform_vector_p (rhs2)) != NULL_TREE)
    2251              :       /* As we query direct optabs restrict to non-convert operations.  */
    2252       276629 :       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (TREE_TYPE (srhs1)))
    2253              :     {
    2254         1500 :       op = optab_for_tree_code (code, TREE_TYPE (type), optab_scalar);
    2255         1500 :       if (op >= FIRST_NORM_OPTAB && op <= LAST_NORM_OPTAB
    2256         1500 :           && can_implement_p (op, TYPE_MODE (TREE_TYPE (type))))
    2257              :         {
    2258         1042 :           tree stype = TREE_TYPE (TREE_TYPE (lhs));
    2259         1042 :           tree slhs = (rhs2 != NULL_TREE)
    2260         1042 :                       ? gimplify_build2 (gsi, code, stype, srhs1, srhs2)
    2261          103 :                       : gimplify_build1 (gsi, code, stype, srhs1);
    2262         1042 :           gimple_assign_set_rhs_from_tree (gsi,
    2263              :                                            build_vector_from_val (type, slhs));
    2264         1042 :           update_stmt (stmt);
    2265         1042 :           return;
    2266              :         }
    2267              :     }
    2268              : 
    2269              :   /* Plain moves do not need lowering.  */
    2270       273899 :   if (code == SSA_NAME
    2271       273899 :       || code == VIEW_CONVERT_EXPR
    2272              :       || code == PAREN_EXPR)
    2273              :     return;
    2274              : 
    2275              :   if (CONVERT_EXPR_CODE_P (code)
    2276              :       || code == FLOAT_EXPR
    2277              :       || code == FIX_TRUNC_EXPR)
    2278              :     return;
    2279              : 
    2280              :   /* The signedness is determined from input argument.  */
    2281              :   if (code == VEC_UNPACK_FLOAT_HI_EXPR
    2282              :       || code == VEC_UNPACK_FLOAT_LO_EXPR
    2283              :       || code == VEC_PACK_FLOAT_EXPR)
    2284              :     {
    2285              :       /* We do not know how to scalarize those.  */
    2286              :       return;
    2287              :     }
    2288              : 
    2289              :   /* For widening/narrowing vector operations, the relevant type is of the
    2290              :      arguments, not the widened result.  VEC_UNPACK_FLOAT_*_EXPR is
    2291              :      calculated in the same way above.  */
    2292              :   if (code == WIDEN_SUM_EXPR
    2293              :       || code == VEC_WIDEN_MULT_HI_EXPR
    2294              :       || code == VEC_WIDEN_MULT_LO_EXPR
    2295              :       || code == VEC_WIDEN_MULT_EVEN_EXPR
    2296              :       || code == VEC_WIDEN_MULT_ODD_EXPR
    2297              :       || code == VEC_UNPACK_HI_EXPR
    2298              :       || code == VEC_UNPACK_LO_EXPR
    2299              :       || code == VEC_UNPACK_FIX_TRUNC_HI_EXPR
    2300              :       || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR
    2301              :       || code == VEC_PACK_TRUNC_EXPR
    2302              :       || code == VEC_PACK_SAT_EXPR
    2303              :       || code == VEC_PACK_FIX_TRUNC_EXPR
    2304              :       || code == VEC_WIDEN_LSHIFT_HI_EXPR
    2305              :       || code == VEC_WIDEN_LSHIFT_LO_EXPR)
    2306              :     {
    2307              :       /* We do not know how to scalarize those.  */
    2308              :       return;
    2309              :     }
    2310              : 
    2311              :   /* Choose between vector shift/rotate by vector and vector shift/rotate by
    2312              :      scalar */
    2313              :   if (code == LSHIFT_EXPR
    2314              :       || code == RSHIFT_EXPR
    2315              :       || code == LROTATE_EXPR
    2316              :       || code == RROTATE_EXPR)
    2317              :     {
    2318        12073 :       optab opv;
    2319              : 
    2320              :       /* Check whether we have vector <op> {x,x,x,x} where x
    2321              :          could be a scalar variable or a constant.  Transform
    2322              :          vector <op> {x,x,x,x} ==> vector <op> scalar.  */
    2323        12073 :       if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (rhs2)))
    2324              :         {
    2325         2129 :           tree first;
    2326              : 
    2327         2129 :           if ((first = ssa_uniform_vector_p (rhs2)) != NULL_TREE)
    2328              :             {
    2329          103 :               gimple_assign_set_rhs2 (stmt, first);
    2330          103 :               update_stmt (stmt);
    2331          103 :               rhs2 = first;
    2332              :             }
    2333              :         }
    2334              : 
    2335        12073 :       opv = optab_for_tree_code (code, type, optab_vector);
    2336        12073 :       if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (rhs2)))
    2337              :         op = opv;
    2338              :       else
    2339              :         {
    2340        10047 :           op = optab_for_tree_code (code, type, optab_scalar);
    2341              : 
    2342        10047 :           compute_type = get_compute_type (op, type);
    2343        10047 :           if (compute_type == type)
    2344              :             return;
    2345              :           /* The rtl expander will expand vector/scalar as vector/vector
    2346              :              if necessary.  Pick one with wider vector type.  */
    2347          180 :           tree compute_vtype = get_compute_type (opv, type);
    2348          180 :           if (subparts_gt (compute_vtype, compute_type))
    2349              :             {
    2350            0 :               compute_type = compute_vtype;
    2351            0 :               op = opv;
    2352              :             }
    2353              :         }
    2354              : 
    2355         2206 :       if (code == LROTATE_EXPR || code == RROTATE_EXPR)
    2356              :         {
    2357           41 :           if (compute_type == NULL_TREE)
    2358            6 :             compute_type = get_compute_type (op, type);
    2359           41 :           if (compute_type == type)
    2360              :             return;
    2361              :           /* Before splitting vector rotates into scalar rotates,
    2362              :              see if we can't use vector shifts and BIT_IOR_EXPR
    2363              :              instead.  For vector by vector rotates we'd also
    2364              :              need to check BIT_AND_EXPR and NEGATE_EXPR, punt there
    2365              :              for now, fold doesn't seem to create such rotates anyway.  */
    2366           35 :           if (compute_type == TREE_TYPE (type)
    2367           35 :               && !VECTOR_INTEGER_TYPE_P (TREE_TYPE (rhs2)))
    2368              :             {
    2369           30 :               optab oplv = vashl_optab, opl = ashl_optab;
    2370           30 :               optab oprv = vlshr_optab, opr = lshr_optab, opo = ior_optab;
    2371           30 :               tree compute_lvtype = get_compute_type (oplv, type);
    2372           30 :               tree compute_rvtype = get_compute_type (oprv, type);
    2373           30 :               tree compute_otype = get_compute_type (opo, type);
    2374           30 :               tree compute_ltype = get_compute_type (opl, type);
    2375           30 :               tree compute_rtype = get_compute_type (opr, type);
    2376              :               /* The rtl expander will expand vector/scalar as vector/vector
    2377              :                  if necessary.  Pick one with wider vector type.  */
    2378           30 :               if (subparts_gt (compute_lvtype, compute_ltype))
    2379              :                 {
    2380            0 :                   compute_ltype = compute_lvtype;
    2381            0 :                   opl = oplv;
    2382              :                 }
    2383           30 :               if (subparts_gt (compute_rvtype, compute_rtype))
    2384              :                 {
    2385            0 :                   compute_rtype = compute_rvtype;
    2386            0 :                   opr = oprv;
    2387              :                 }
    2388              :               /* Pick the narrowest type from LSHIFT_EXPR, RSHIFT_EXPR and
    2389              :                  BIT_IOR_EXPR.  */
    2390           30 :               compute_type = compute_ltype;
    2391           30 :               if (subparts_gt (compute_type, compute_rtype))
    2392            0 :                 compute_type = compute_rtype;
    2393           30 :               if (subparts_gt (compute_type, compute_otype))
    2394            0 :                 compute_type = compute_otype;
    2395              :               /* Verify all 3 operations can be performed in that type.  */
    2396           30 :               if (compute_type != TREE_TYPE (type))
    2397              :                 {
    2398           30 :                   if (!can_implement_p (opl, TYPE_MODE (compute_type))
    2399           30 :                       || !can_implement_p (opr, TYPE_MODE (compute_type))
    2400           60 :                       || !can_implement_p (opo, TYPE_MODE (compute_type)))
    2401            0 :                     compute_type = TREE_TYPE (type);
    2402              :                 }
    2403              :             }
    2404              :         }
    2405              :     }
    2406              :   else
    2407       225064 :     op = optab_for_tree_code (code, type, optab_default);
    2408              : 
    2409              :   /* Optabs will try converting a negation into a subtraction, so
    2410              :      look for it as well.  TODO: negation of floating-point vectors
    2411              :      might be turned into an exclusive OR toggling the sign bit.  */
    2412       227264 :   if (op == unknown_optab
    2413       227264 :       && code == NEGATE_EXPR
    2414       227264 :       && INTEGRAL_TYPE_P (TREE_TYPE (type)))
    2415            0 :     op = optab_for_tree_code (MINUS_EXPR, type, optab_default);
    2416              : 
    2417       227264 :   if (compute_type == NULL_TREE)
    2418       227084 :     compute_type = get_compute_type (op, type);
    2419       227264 :   if (compute_type == type)
    2420              :     return;
    2421              : 
    2422        31537 :   new_rhs = expand_vector_operation (gsi, type, compute_type, stmt, code);
    2423              : 
    2424              :   /* Leave expression untouched for later expansion.  */
    2425        31537 :   if (new_rhs == NULL_TREE)
    2426              :     return;
    2427              : 
    2428         4191 :   if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (new_rhs)))
    2429          102 :     new_rhs = gimplify_build1 (gsi, VIEW_CONVERT_EXPR, TREE_TYPE (lhs),
    2430              :                                new_rhs);
    2431              : 
    2432              :   /* NOTE:  We should avoid using gimple_assign_set_rhs_from_tree. One
    2433              :      way to do it is change expand_vector_operation and its callees to
    2434              :      return a tree_code, RHS1 and RHS2 instead of a tree. */
    2435         4191 :   gimple_assign_set_rhs_from_tree (gsi, new_rhs);
    2436         4191 :   update_stmt (gsi_stmt (*gsi));
    2437              : }
    2438              : 
    2439              : /* Use this to lower vector operations introduced by the vectorizer,
    2440              :    if it may need the bit-twiddling tricks implemented in this file.  */
    2441              : 
    2442              : static unsigned int
    2443      1532070 : expand_vector_operations (void)
    2444              : {
    2445      1532070 :   gimple_stmt_iterator gsi;
    2446      1532070 :   basic_block bb;
    2447      1532070 :   bool cfg_changed = false;
    2448              : 
    2449     16265950 :   FOR_EACH_BB_FN (bb, cfun)
    2450              :     {
    2451    132760779 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    2452              :         {
    2453    103293019 :           expand_vector_operations_1 (&gsi);
    2454              :           /* ???  If we do not cleanup EH then we will ICE in
    2455              :              verification.  But in reality we have created wrong-code
    2456              :              as we did not properly transition EH info and edges to
    2457              :              the piecewise computations.  */
    2458    103293019 :           if (maybe_clean_eh_stmt (gsi_stmt (gsi))
    2459    103293019 :               && gimple_purge_dead_eh_edges (bb))
    2460              :             cfg_changed = true;
    2461              :           /* If a .LOOP_DIST_ALIAS call prevailed loops got elided
    2462              :              before vectorization got a chance to get at them.  Simply
    2463              :              fold as if loop distribution wasn't performed.  */
    2464    103293019 :           if (gimple_call_internal_p (gsi_stmt (gsi), IFN_LOOP_DIST_ALIAS))
    2465              :             {
    2466            0 :               fold_loop_internal_call (gsi_stmt (gsi), boolean_false_node);
    2467            0 :               cfg_changed = true;
    2468              :             }
    2469              :         }
    2470              :     }
    2471              : 
    2472      1532070 :   return cfg_changed ? TODO_cleanup_cfg : 0;
    2473              : }
    2474              : 
    2475              : namespace {
    2476              : 
    2477              : const pass_data pass_data_lower_vector =
    2478              : {
    2479              :   GIMPLE_PASS, /* type */
    2480              :   "veclower", /* name */
    2481              :   OPTGROUP_VEC, /* optinfo_flags */
    2482              :   TV_NONE, /* tv_id */
    2483              :   PROP_cfg, /* properties_required */
    2484              :   PROP_gimple_lvec, /* properties_provided */
    2485              :   0, /* properties_destroyed */
    2486              :   0, /* todo_flags_start */
    2487              :   TODO_update_ssa, /* todo_flags_finish */
    2488              : };
    2489              : 
    2490              : class pass_lower_vector : public gimple_opt_pass
    2491              : {
    2492              : public:
    2493       302985 :   pass_lower_vector (gcc::context *ctxt)
    2494       605970 :     : gimple_opt_pass (pass_data_lower_vector, ctxt)
    2495              :   {}
    2496              : 
    2497              :   /* opt_pass methods: */
    2498      1531899 :   bool gate (function *fun) final override
    2499              :     {
    2500      1531899 :       return !(fun->curr_properties & PROP_gimple_lvec);
    2501              :     }
    2502              : 
    2503       461345 :   unsigned int execute (function *) final override
    2504              :     {
    2505       461345 :       return expand_vector_operations ();
    2506              :     }
    2507              : 
    2508              : }; // class pass_lower_vector
    2509              : 
    2510              : } // anon namespace
    2511              : 
    2512              : gimple_opt_pass *
    2513       302985 : make_pass_lower_vector (gcc::context *ctxt)
    2514              : {
    2515       302985 :   return new pass_lower_vector (ctxt);
    2516              : }
    2517              : 
    2518              : namespace {
    2519              : 
    2520              : const pass_data pass_data_lower_vector_ssa =
    2521              : {
    2522              :   GIMPLE_PASS, /* type */
    2523              :   "veclower2", /* name */
    2524              :   OPTGROUP_VEC, /* optinfo_flags */
    2525              :   TV_NONE, /* tv_id */
    2526              :   PROP_cfg, /* properties_required */
    2527              :   PROP_gimple_lvec, /* properties_provided */
    2528              :   0, /* properties_destroyed */
    2529              :   0, /* todo_flags_start */
    2530              :   ( TODO_update_ssa
    2531              :     | TODO_cleanup_cfg ), /* todo_flags_finish */
    2532              : };
    2533              : 
    2534              : class pass_lower_vector_ssa : public gimple_opt_pass
    2535              : {
    2536              : public:
    2537       605970 :   pass_lower_vector_ssa (gcc::context *ctxt)
    2538      1211940 :     : gimple_opt_pass (pass_data_lower_vector_ssa, ctxt)
    2539              :   {}
    2540              : 
    2541              :   /* opt_pass methods: */
    2542       302985 :   opt_pass * clone () final override
    2543              :   {
    2544       302985 :     return new pass_lower_vector_ssa (m_ctxt);
    2545              :   }
    2546      1070725 :   unsigned int execute (function *) final override
    2547              :     {
    2548      1070725 :       return expand_vector_operations ();
    2549              :     }
    2550              : 
    2551              : }; // class pass_lower_vector_ssa
    2552              : 
    2553              : } // anon namespace
    2554              : 
    2555              : gimple_opt_pass *
    2556       302985 : make_pass_lower_vector_ssa (gcc::context *ctxt)
    2557              : {
    2558       302985 :   return new pass_lower_vector_ssa (ctxt);
    2559              : }
    2560              : 
    2561              : #include "gt-tree-vect-generic.h"
        

Generated by: LCOV version 2.6-beta

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