From: Stefan Schulze Frielinghaus <[email protected]>

Sashiko pointed out that in expand_perm_as_a_vlbr_vstbr_candidate,
d.target and d.op0 were wrapped with gen_rtx_SUBREG before emitting
bswap / rotate insn.  Since targetm.vectorize.vec_perm_const is invoked
in expand_vec_perm_const before operands are forced into registers,
d.op0 is not guaranteed to be a REG.  Blindly wrapping d.op0 with
gen_rtx_SUBREG creates invalid RTL (such as nested subregs, subregs of
constant vectors, or subregs of memory).  Furthermore, wrapping d.target
in a subreg generates ill-formed RTL.

Fixed by simplifying subregs with simplify_gen_subreg, falling back to
force_reg on d.op0 if necessary, and expanding the instruction via
expand_insn with create_output_operand and create_input_operand to
properly legitimize operands matching predicates.

Similarly for expand_perm_with_merge ensure that simplify_gen_subreg
cannot fail by forcing operands into registers.

Committing this without tests since I couldn't come up with one which
would expose those findings.  Nonetheless, they should be fixed.

gcc/ChangeLog:

        * config/s390/s390.cc (expand_perm_as_a_vlbr_vstbr_candidate): Use
        simplify_gen_subreg and expand_insn instead of gen_rtx_SUBREG.
        (expand_perm_with_merge): Force operands into registers.
---
 Bootstrap and regtest are still running.  Will commit afterwards and
 wait for Sashikos results.

 gcc/config/s390/s390.cc | 72 ++++++++++++++++++++---------------------
 1 file changed, 36 insertions(+), 36 deletions(-)

diff --git a/gcc/config/s390/s390.cc b/gcc/config/s390/s390.cc
index 507c53734e6..e902c97599b 100644
--- a/gcc/config/s390/s390.cc
+++ b/gcc/config/s390/s390.cc
@@ -18044,6 +18044,10 @@ expand_perm_with_merge (const struct expand_vec_perm_d 
&d)
       op0 = d.op0;
       op1 = d.op1;
     }
+
+  op0 = force_reg (d.vmode, op0);
+  op1 = force_reg (d.vmode, op1);
+
   if (mergemode != d.vmode)
     {
       target = simplify_gen_subreg (mergemode, target, d.vmode, 0);
@@ -18239,56 +18243,52 @@ expand_perm_as_a_vlbr_vstbr_candidate (const struct 
expand_vec_perm_d &d)
   if (d.vmode != V16QImode || d.op0 != d.op1)
     return false;
 
+  insn_code icode;
+  machine_mode mode;
+
   /* For halfwords a byte-swap is a rotate by 8 bits (verllh), which does not
      require VXE2.  */
   if (memcmp (d.perm, perm[0], MAX_VECT_LEN) == 0)
     {
-      if (!d.testing_p)
-       {
-         rtx target = gen_rtx_SUBREG (V8HImode, d.target, 0);
-         rtx op0 = gen_rtx_SUBREG (V8HImode, d.op0, 0);
-         emit_insn (gen_bswapv8hi (target, op0));
-       }
-      return true;
+      mode = V8HImode;
+      icode = CODE_FOR_bswapv8hi;
     }
-
-  if (!TARGET_VXE2)
+  /* All remaining operations require VXE2.  */
+  else if (!TARGET_VXE2)
     return false;
-
-  if (memcmp (d.perm, perm[1], MAX_VECT_LEN) == 0)
+  else if (memcmp (d.perm, perm[1], MAX_VECT_LEN) == 0)
     {
-      if (!d.testing_p)
-       {
-         rtx target = gen_rtx_SUBREG (V4SImode, d.target, 0);
-         rtx op0 = gen_rtx_SUBREG (V4SImode, d.op0, 0);
-         emit_insn (gen_bswapv4si (target, op0));
-       }
-      return true;
+      mode = V4SImode;
+      icode = CODE_FOR_bswapv4si;
     }
-
-  if (memcmp (d.perm, perm[2], MAX_VECT_LEN) == 0)
+  else if (memcmp (d.perm, perm[2], MAX_VECT_LEN) == 0)
     {
-      if (!d.testing_p)
-       {
-         rtx target = gen_rtx_SUBREG (V2DImode, d.target, 0);
-         rtx op0 = gen_rtx_SUBREG (V2DImode, d.op0, 0);
-         emit_insn (gen_bswapv2di (target, op0));
-       }
-      return true;
+      mode = V2DImode;
+      icode = CODE_FOR_bswapv2di;
+    }
+  else if (memcmp (d.perm, perm[3], MAX_VECT_LEN) == 0)
+    {
+      mode = V1TImode;
+      icode = CODE_FOR_bswapv1ti;
     }
+  else
+    return false;
 
-  if (memcmp (d.perm, perm[3], MAX_VECT_LEN) == 0)
+  if (!d.testing_p)
     {
-      if (!d.testing_p)
-       {
-         rtx target = gen_rtx_SUBREG (V1TImode, d.target, 0);
-         rtx op0 = gen_rtx_SUBREG (V1TImode, d.op0, 0);
-         emit_insn (gen_bswapv1ti (target, op0));
-       }
-      return true;
+      rtx target = simplify_gen_subreg (mode, d.target, d.vmode, 0);
+      rtx op0 = simplify_gen_subreg (mode, d.op0, d.vmode, 0);
+      if (op0 == NULL_RTX)
+       op0
+         = simplify_gen_subreg (mode, force_reg (d.vmode, d.op0), d.vmode, 0);
+
+      class expand_operand ops[2];
+      create_output_operand (&ops[0], target, mode);
+      create_input_operand (&ops[1], op0, mode);
+      expand_insn (icode, 2, ops);
     }
 
-  return false;
+  return true;
 }
 
 static bool
-- 
2.55.0

Reply via email to