Hi,

Only change from v1 is the exclusion of single-lane vectors.  The diff 
obfuscates this, unfortunately...

Currently, for a situation like

  _1 = BIT_FIELD_REF <src1_9(D), 32, 0>;
  _2 = BIT_FIELD_REF <src1_9(D), 32, 32>;
  _3 = BIT_FIELD_REF <src1_9(D), 32, 64>;
  _4 = BIT_FIELD_REF <src1_9(D), 32, 96>;
  _5 = BIT_FIELD_REF <src2_16(D), 32, 0>;
  _6 = BIT_FIELD_REF <src2_16(D), 32, 32>;
  _7 = BIT_FIELD_REF <src2_16(D), 32, 64>;
  _8 = BIT_FIELD_REF <src2_16(D), 32, 96>;
  _21 = {_1, _2, _3, _4, _5, _6, _7, _8};

we give up because the vec_init, looking through the BIT_FIELD_REFs, sees
two source vectors but we only expect one.

This patch adds support for a second one, leading to a permutation being
created.  We can do the same optimization in forwprop but that's too
late as we have already confused vect costing by the (usually) expensive
vec_init.  In order to not confuse target patterns, the optimization is
only done for vectors with at least two lanes.

Bootstrapped and regtested on x86, power10, and aarch64.
Regtested on riscv64.

Regards
 Robin

        PR tree-optimization/105816

gcc/ChangeLog:

        * tree-vect-slp.cc (compatible_bit_field_ref_source): New helper
        function.
        (vect_build_slp_tree_1): Use helper.
        (build_perm_source_node): New helper function.
        (vect_build_slp_tree_2): Use helper and allow a second source
        vector.

gcc/testsuite/ChangeLog:

        * gcc.dg/vect/pr105816.c: New test.
---
 gcc/testsuite/gcc.dg/vect/pr105816.c |  23 ++++
 gcc/tree-vect-slp.cc                 | 162 ++++++++++++++++++++-------
 2 files changed, 145 insertions(+), 40 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/pr105816.c

diff --git a/gcc/testsuite/gcc.dg/vect/pr105816.c 
b/gcc/testsuite/gcc.dg/vect/pr105816.c
new file mode 100644
index 00000000000..9f9a24aea4e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr105816.c
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target vect_int } */
+/* { dg-additional-options "-fdump-tree-slp-details" } */
+
+void test_lo (short * __restrict dst, short *src1, short *src2, int n)
+{
+  for (int i = 0; i < n; ++i)
+    {
+      dst[0] = src1[0];
+      dst[1] = src1[1];
+      dst[2] = src1[2];
+      dst[3] = src1[3];
+      dst[4] = src2[0];
+      dst[5] = src2[1];
+      dst[6] = src2[2];
+      dst[7] = src2[3];
+      dst+=8;
+      src1+=4;
+      src2+=4;
+    }
+}
+
+/* { dg-final { scan-tree-dump-not "different BIT_FIELD_REF arguments" "slp1" 
} } */
diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
index ee05e1767cc..6c0f95fc0c4 100644
--- a/gcc/tree-vect-slp.cc
+++ b/gcc/tree-vect-slp.cc
@@ -1099,6 +1099,24 @@ compatible_calls_p (gcall *call1, gcall *call2, bool 
allow_two_operators)
   return true;
 }
 
+/* Return true if VEC is an SSA name, of compatible vector type and its
+   type of the same size as VECTYPE.  */
+
+static bool
+compatible_bit_field_ref_source (vec_info *vinfo, tree vec, tree vectype)
+{
+  return is_a <bb_vec_info> (vinfo)
+    && TREE_CODE (vec) == SSA_NAME
+    && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vec)
+    /* When the element types are not compatible we pun the
+       source to the target vectype which requires equal size.  */
+    && ((VECTOR_TYPE_P (TREE_TYPE (vec))
+        && types_compatible_p (TREE_TYPE (vectype),
+                               TREE_TYPE (TREE_TYPE (vec))))
+       || operand_equal_p (TYPE_SIZE (vectype),
+                           TYPE_SIZE (TREE_TYPE (vec))));
+}
+
 /* Verify if the scalar stmts STMTS are isomorphic, require data
    permutation or are of unsupported types of operation.
    Return false if at least one (or all in case of BB vectorization)
@@ -1151,6 +1169,7 @@ vect_build_slp_tree_3 (vec_info *vinfo, 
vec<stmt_vec_info> stmts,
   bool first_stmt_ldst_p = false, first_stmt_ldst_masklen_p = false;
   bool first_stmt_phi_p = false;
   int first_reduc_idx = -1;
+  tree other_bfref_source = NULL_TREE;
 
   basic_block common_bb = NULL;
   gimple *trapping_stmt = NULL;
@@ -1327,16 +1346,7 @@ vect_build_slp_tree_3 (vec_info *vinfo, 
vec<stmt_vec_info> stmts,
       if (!ldst_p && rhs_code == BIT_FIELD_REF)
        {
          tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
-         if (!is_a <bb_vec_info> (vinfo)
-             || TREE_CODE (vec) != SSA_NAME
-             || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vec)
-             /* When the element types are not compatible we pun the
-                source to the target vectype which requires equal size.  */
-             || ((!VECTOR_TYPE_P (TREE_TYPE (vec))
-                  || !types_compatible_p (TREE_TYPE (vectype),
-                                          TREE_TYPE (TREE_TYPE (vec))))
-                 && !operand_equal_p (TYPE_SIZE (vectype),
-                                      TYPE_SIZE (TREE_TYPE (vec)))))
+         if (!compatible_bit_field_ref_source (vinfo, vec, vectype))
            {
              if (dump_enabled_p ())
                dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
@@ -1475,17 +1485,40 @@ vect_build_slp_tree_3 (vec_info *vinfo, 
vec<stmt_vec_info> stmts,
              continue;
            }
 
+         /* Check if the number of distinct BIT_FIELD_REF source operands
+            is at most 2.  */
          if (!ldst_p
              && first_stmt_code == BIT_FIELD_REF
              && (TREE_OPERAND (gimple_assign_rhs1 (first_stmt_info->stmt), 0)
                  != TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0)))
            {
-             if (dump_enabled_p ())
-               dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
-                                "Build SLP failed: different BIT_FIELD_REF "
-                                "arguments in %G", stmt);
-             /* Mismatch.  */
-             continue;
+             tree cur = TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0);
+             if (!other_bfref_source)
+               {
+                 if (!compatible_bit_field_ref_source (vinfo, cur, vectype))
+                   {
+                     if (dump_enabled_p ())
+                       dump_printf_loc (MSG_MISSED_OPTIMIZATION,
+                                        vect_location,
+                                        "Build SLP failed: different "
+                                        "BIT_FIELD_REF arguments in "
+                                        "%G", stmt);
+                     /* Mismatch.  */
+                     continue;
+                   }
+                 other_bfref_source = cur;
+               }
+             else if (cur != other_bfref_source)
+               {
+                 if (dump_enabled_p ())
+                   dump_printf_loc (MSG_MISSED_OPTIMIZATION,
+                                    vect_location,
+                                    "Build SLP failed: different "
+                                    "BIT_FIELD_REF arguments in "
+                                    "%G", stmt);
+                 /* Mismatch.  */
+                 continue;
+               }
            }
 
          if (call_stmt
@@ -2120,6 +2153,40 @@ force_split_matches (match_elt_t *matches, unsigned 
group_size,
     matches[i] = (int)(group_size - tail);
 }
 
+/* Helper for creating a permutation source node when converting a
+    vec[0] = BIT_FIELD_REF (vec, ...)
+   into a vector permutation.
+
+   Initializes an SLP_TREE with the type of VECTYPE, sets its number of units,
+   and adds VEC to its VEC_DEFs, returning the new node.  */
+
+static slp_tree
+build_perm_source_node (tree vec, tree vectype)
+{
+  slp_tree vnode = vect_create_new_slp_node (vNULL);
+  if (operand_equal_p (TYPE_SIZE (vectype), TYPE_SIZE (TREE_TYPE (vec))))
+    /* ???  We record vectype here but we hide eventually necessary
+       punning and instead rely on code generation to materialize
+       VIEW_CONVERT_EXPRs as necessary.  We instead should make
+       this explicit somehow.  */
+    SLP_TREE_VECTYPE (vnode) = vectype;
+  else
+    {
+      /* For different size but compatible elements we can still
+        use VEC_PERM_EXPR without punning.  */
+      gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec))
+                 && types_compatible_p (TREE_TYPE (vectype),
+                                        TREE_TYPE (TREE_TYPE (vec))));
+      SLP_TREE_VECTYPE (vnode) = TREE_TYPE (vec);
+    }
+  auto nunits = TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (vnode));
+  unsigned HOST_WIDE_INT const_nunits;
+  if (nunits.is_constant (&const_nunits))
+    SLP_TREE_LANES (vnode) = const_nunits;
+  SLP_TREE_VEC_DEFS (vnode).safe_push (vec);
+  return vnode;
+}
+
 /* Recursively build an SLP tree starting from NODE.
    Fail (and return a value not equal to zero) if def-stmts are not
    isomorphic, require data permutation or are of unsupported types of
@@ -2371,15 +2438,23 @@ vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
                               TYPE_VECTOR_SUBPARTS (vectype).to_constant ());
          return NULL;
        }
-      /* vect_build_slp_tree_2 determined all BIT_FIELD_REFs reference
-        the same SSA name vector of a compatible type to vectype.  */
+      /* vect_build_slp_tree_1 determined all BIT_FIELD_REFs reference
+        at most two SSA name vectors of a compatible type to vectype.  */
       vec<std::pair<unsigned, unsigned> > lperm = vNULL;
       tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0);
+      tree other_vec = NULL_TREE;
+      unsigned other_lanes = 0;
       stmt_vec_info estmt_info;
       FOR_EACH_VEC_ELT (stmts, i, estmt_info)
        {
          gassign *estmt = as_a <gassign *> (estmt_info->stmt);
          tree bfref = gimple_assign_rhs1 (estmt);
+         tree cur_vec = TREE_OPERAND (bfref, 0);
+         if (cur_vec != vec)
+           {
+             other_vec = cur_vec;
+             other_lanes++;
+           }
          HOST_WIDE_INT lane;
          if (!known_eq (bit_field_size (bfref),
                         tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (vectype))))
@@ -2390,39 +2465,46 @@ vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
              matches[0] = -1;
              return NULL;
            }
-         lperm.safe_push (std::make_pair (0, (unsigned)lane));
+         lperm.safe_push (std::make_pair (cur_vec == vec ? 0 : 1,
+                                          (unsigned) lane));
        }
-      slp_tree vnode = vect_create_new_slp_node (vNULL);
-      if (operand_equal_p (TYPE_SIZE (vectype), TYPE_SIZE (TREE_TYPE (vec))))
-       /* ???  We record vectype here but we hide eventually necessary
-          punning and instead rely on code generation to materialize
-          VIEW_CONVERT_EXPRs as necessary.  We instead should make
-          this explicit somehow.  */
-       SLP_TREE_VECTYPE (vnode) = vectype;
-      else
+
+      /* Don't continue for single-lane permutes.
+        Backends can have patterns for these which we don't want
+        to confuse.  */
+      if (other_vec
+         && (other_lanes < 2 || stmts.length () - other_lanes < 2))
        {
-         /* For different size but compatible elements we can still
-            use VEC_PERM_EXPR without punning.  */
-         gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec))
-                     && types_compatible_p (TREE_TYPE (vectype),
-                                            TREE_TYPE (TREE_TYPE (vec))));
-         SLP_TREE_VECTYPE (vnode) = TREE_TYPE (vec);
-       }
-      auto nunits = TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (vnode));
-      unsigned HOST_WIDE_INT const_nunits;
-      if (nunits.is_constant (&const_nunits))
-       SLP_TREE_LANES (vnode) = const_nunits;
-      SLP_TREE_VEC_DEFS (vnode).safe_push (vec);
+         lperm.release ();
+         int other_start = -1;
+         FOR_EACH_VEC_ELT (stmts, i, estmt_info)
+           if (TREE_OPERAND (gimple_assign_rhs1 (estmt_info->stmt), 0)
+               == other_vec)
+             {
+               if (other_start == -1)
+                 other_start = i;
+               matches[i] = other_start;
+             }
+         return NULL;
+       }
+      slp_tree vnode = build_perm_source_node (vec, vectype);
+      slp_tree vnode2 = NULL;
+      if (other_vec)
+       vnode2 = build_perm_source_node (other_vec, vectype);
+
       /* We are always building a permutation node even if it is an identity
         permute to shield the rest of the vectorizer from the odd node
         representing an actual vector without any scalar ops.
         ???  We could hide it completely with making the permute node
         external?  */
-      node = vect_create_new_slp_node (node, stmts, 1);
+      node = vect_create_new_slp_node (node, stmts, !other_vec ? 1 : 2);
       SLP_TREE_CODE (node) = VEC_PERM_EXPR;
       SLP_TREE_LANE_PERMUTATION (node) = lperm;
       SLP_TREE_VECTYPE (node) = vectype;
       SLP_TREE_CHILDREN (node).quick_push (vnode);
+      if (other_vec)
+       SLP_TREE_CHILDREN (node).quick_push (vnode2);
+
       SLP_TREE_REPRESENTATIVE (node) = NULL;
       return node;
     }

base-commit: 635d3209171d20bce4de35041097064a3afeff66
-- 
2.56.0

Reply via email to