From: Robin Dapp <[email protected]>

This patch adds a way of extracting vectors from vectors by building a
consecutive permute pattern for the target to handle.

gcc/ChangeLog:

        * expmed.cc (vec_extract_perm_p): New function.
        (extract_vector_from_vector_perm): New function.
        (extract_vector_from_vector): Allow vector extraction via
        permute.
        * expmed.h (vec_extract_perm_p): Declare.
---
 gcc/expmed.cc | 105 +++++++++++++++++++++++++++++++++++++++++++++++++-
 gcc/expmed.h  |   3 ++
 2 files changed, 107 insertions(+), 1 deletion(-)

diff --git a/gcc/expmed.cc b/gcc/expmed.cc
index 367e8af46e5..fcf100df90d 100644
--- a/gcc/expmed.cc
+++ b/gcc/expmed.cc
@@ -747,6 +747,48 @@ store_bit_field_using_insv (const extraction_insn *insv, 
rtx op0,
   return false;
 }
 
+/* See if we can extract a vector of mode SUBVEC_MODE from a larger vector
+   of mode VEC_MODE at EXTRACT_IDX.  EXTRACT_IDX counts in elements
+   of VEC_MOD and SUBVEC_MODE.  If we can, return the possibly punned
+   input mode.  */
+
+opt_machine_mode
+vec_extract_perm_p (machine_mode vec_mode, machine_mode subvec_mode,
+                   poly_uint64 extract_idx)
+{
+  if (!VECTOR_MODE_P (vec_mode) || !VECTOR_MODE_P (subvec_mode))
+    return opt_machine_mode ();
+
+  if (maybe_ne (GET_MODE_UNIT_SIZE (vec_mode),
+               GET_MODE_UNIT_SIZE (subvec_mode)))
+    return opt_machine_mode ();
+
+  if (maybe_gt (GET_MODE_NUNITS (subvec_mode) + extract_idx,
+               GET_MODE_NUNITS (vec_mode)))
+    return opt_machine_mode ();
+
+  /* Look for a matching mode for the vector elements.
+     Do not pun the whole vector with a mode of different element size
+     here (e.g. V2DI and V4SI).  We expect a target to do that itself.  */
+  if (GET_MODE_INNER (vec_mode) != GET_MODE_INNER (subvec_mode)
+      && !related_vector_mode (vec_mode, GET_MODE_INNER (subvec_mode),
+                              GET_MODE_NUNITS (vec_mode)).exists
+          (&vec_mode))
+    return opt_machine_mode ();
+
+  poly_uint64 out_nunits = GET_MODE_NUNITS (subvec_mode);
+  poly_uint64 in_nunits = GET_MODE_NUNITS (vec_mode);
+
+  vec_perm_builder sel (out_nunits, 1, 3);
+  for (unsigned int i = 0; i < 3; ++i)
+    sel.quick_push (extract_idx + i);
+
+  vec_perm_indices indices (sel, 2, in_nunits);
+  if (can_vec_perm_const_p (subvec_mode, vec_mode, indices))
+    return vec_mode;
+  return opt_machine_mode ();
+}
+
 /* A subroutine of store_bit_field, with the same arguments.  Return true
    if the operation could be implemented.
 
@@ -1698,9 +1740,65 @@ extract_bit_field_as_subreg (machine_mode mode, rtx op0,
   return NULL_RTX;
 }
 
+/* Same as extract_vector_from_vector but try with a permutation.  */
+
+static rtx
+extract_vector_from_vector_perm (rtx vec, rtx target, machine_mode mode,
+                                machine_mode subvec_mode, poly_uint64 bitsize,
+                                poly_uint64 bitnum, rtx *alt_rtl)
+{
+  machine_mode vec_mode = GET_MODE (vec);
+  poly_uint64 extract_idx;
+  if (!VECTOR_MODE_P (vec_mode)
+      || MEM_P (vec)
+      || !VECTOR_MODE_P (subvec_mode)
+      || maybe_ne (bitsize, GET_MODE_PRECISION (subvec_mode))
+      || known_le (GET_MODE_SIZE (GET_MODE (vec)),
+                  GET_MODE_SIZE (subvec_mode))
+      || !multiple_p (bitnum, GET_MODE_UNIT_BITSIZE (subvec_mode),
+                     &extract_idx))
+    return NULL_RTX;
+
+  /* We don't want to handle single-element vector modes here.  */
+  if (known_le (GET_MODE_NUNITS (subvec_mode), 1)
+      || known_le (GET_MODE_NUNITS (vec_mode), 1))
+    return NULL_RTX;
+
+  machine_mode punned_vec_mode;
+  if (!vec_extract_perm_p (vec_mode, subvec_mode, extract_idx).exists
+      (&punned_vec_mode))
+    return NULL_RTX;
+
+  if (punned_vec_mode != vec_mode)
+    vec = gen_lowpart (punned_vec_mode, vec);
+
+  poly_uint64 out_nunits = GET_MODE_NUNITS (subvec_mode);
+
+  vec_perm_builder sel (out_nunits, 1, 3);
+  for (unsigned int i = 0; i < 3; ++i)
+    sel.quick_push (extract_idx + i);
+
+  rtx tmp = subvec_mode == mode ? target : NULL_RTX;
+  rtx res = expand_vec_perm_const (subvec_mode, vec, vec, sel, BLKmode, tmp);
+  if (!res)
+    return NULL_RTX;
+
+  if (res == target && alt_rtl)
+    *alt_rtl = target;
+
+  if (subvec_mode != mode)
+    res = gen_lowpart (mode, res);
+
+  return res;
+}
+
 /* Try to extract a subvector of mode SUBVEC_MODE (which has BITSIZE bits)
    from vector VEC, starting at bit BITNUM, then extract the MODE lowpart of
-   the subvector.  Return the result on success or NULL_RTX on failure.  */
+   the subvector.
+   If the vec_extract-based attempt failed, build a vector permute to extract
+   the part and hand it to the target.
+
+   Return the result on success or NULL_RTX on failure.  */
 
 static rtx
 extract_vector_from_vector (rtx vec, rtx target, machine_mode mode,
@@ -1755,6 +1853,11 @@ extract_vector_from_vector (rtx vec, rtx target, 
machine_mode mode,
        }
     }
 
+  rtx res = extract_vector_from_vector_perm (vec, target, mode, subvec_mode,
+                                            bitsize, bitnum, alt_rtl);
+  if (res)
+    return res;
+
   return NULL_RTX;
 }
 
diff --git a/gcc/expmed.h b/gcc/expmed.h
index b4b44bba1d7..2e2d9009c84 100644
--- a/gcc/expmed.h
+++ b/gcc/expmed.h
@@ -730,6 +730,9 @@ extern rtx expmed_mult_highpart_optab (scalar_int_mode, 
rtx, rtx, rtx,
                                       int, int);
 extern rtx expand_rotate_as_vec_perm (machine_mode, rtx, rtx, rtx);
 
+extern opt_machine_mode vec_extract_perm_p (machine_mode, machine_mode,
+                                           poly_uint64);
+
 /* The constant divisor of the last division expanded, reset per function
    by prepare_function_start.  */
 extern HOST_WIDE_INT last_div_const;
-- 
2.56.0

Reply via email to