https://gcc.gnu.org/g:6b770d84867c112bbe0db9176da6f860705e99c5

commit r17-6492-g6b770d84867c112bbe0db9176da6f860705e99c5
Author: Bohan Lei <[email protected]>
Date:   Mon Nov 16 11:20:50 2026 +0800

    RISC-V: Reuse legal VTYPE for whole-register moves
    
    Whole-register moves require vill to be clear, but do not depend on the
    other VTYPE fields or on VL.  The vsetvl pass currently derives a ratio
    and policy requirement from their RTL mode.  This can introduce a
    vsetivli with AVL zero before a move and another configuration after it,
    even when a suitable legal configuration is already available.
    
    Represent these moves as valid demands without SEW/LMUL, policy or AVL
    constraints.  Extend the existing demand rules so that any known legal
    configuration satisfies such a demand, while the demand alone cannot
    satisfy a subsequent instruction's specific SEW/LMUL requirements.
    
    Regtested on rv64gcv and rv32gcv, no regression.
    
    gcc/ChangeLog:
    
            * config/riscv/riscv-vsetvl.cc (enum class sew_lmul_demand_type):
            Add legal_vtype.
            (vsetvl_info::normalize_demand): Handle it.
            (vsetvl_info::parse_insn): Give whole-register moves an 
unconstrained
            valid demand.
            (vsetvl_info::dump): Print the legal-VTYPE demand.
            (pre_vsetvl::preds_all_same_avl_and_ratio_p): Reject 
legal-VTYPE-only
            demands when inferring the incoming ratio.
            (pre_vsetvl::pre_global_vsetvl_info): Likewise for local inference.
            * config/riscv/riscv-vsetvl.def: New rules.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/riscv/rvv/base/whole-move-vtype-1.c: New test.
            * gcc.target/riscv/rvv/base/whole-move-vtype-2.c: New test.

Diff:
---
 gcc/config/riscv/riscv-vsetvl.cc                   | 20 +++++++++++++++++++-
 gcc/config/riscv/riscv-vsetvl.def                  | 22 ++++++++++++++++++++++
 .../gcc.target/riscv/rvv/base/whole-move-vtype-1.c | 18 ++++++++++++++++++
 .../gcc.target/riscv/rvv/base/whole-move-vtype-2.c | 17 +++++++++++++++++
 4 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/gcc/config/riscv/riscv-vsetvl.cc b/gcc/config/riscv/riscv-vsetvl.cc
index 342087e8ab10..8ea85b65a665 100644
--- a/gcc/config/riscv/riscv-vsetvl.cc
+++ b/gcc/config/riscv/riscv-vsetvl.cc
@@ -865,6 +865,7 @@ enum demand_flags : unsigned
 
 enum class sew_lmul_demand_type : unsigned
 {
+  legal_vtype = demand_flags::DEMAND_EMPTY_P,
   sew_lmul = demand_flags::DEMAND_SEW_P | demand_flags::DEMAND_LMUL_P,
   ratio_only = demand_flags::DEMAND_RATIO_P,
   sew_only = demand_flags::DEMAND_SEW_P,
@@ -1088,6 +1089,9 @@ public:
     switch (demand_flags
            & (DEMAND_SEW_P | DEMAND_LMUL_P | DEMAND_RATIO_P | DEMAND_GE_SEW_P))
       {
+      case (unsigned) sew_lmul_demand_type::legal_vtype:
+       m_sew_lmul_demand = sew_lmul_demand_type::legal_vtype;
+       break;
       case (unsigned) sew_lmul_demand_type::sew_lmul:
        m_sew_lmul_demand = sew_lmul_demand_type::sew_lmul;
        break;
@@ -1246,6 +1250,14 @@ public:
 
     /* Determine the demand info of the RVV insn.  */
     m_max_sew = get_max_int_sew ();
+    /* Whole-register moves need a legal VTYPE, but do not depend on its
+       fields or on VL.  */
+    if (get_attr_type (insn->rtl ()) == TYPE_VMOV && !has_vl_op (insn->rtl ()))
+      {
+       normalize_demand (DEMAND_EMPTY_P);
+       return;
+      }
+
     unsigned dflags = 0;
     if (vector_config_insn_p (insn->rtl ()))
       {
@@ -1440,7 +1452,9 @@ public:
       gcc_unreachable ();
 
     fprintf (file, "%sDemand fields:", indent);
-    if (m_sew_lmul_demand == sew_lmul_demand_type::sew_lmul)
+    if (m_sew_lmul_demand == sew_lmul_demand_type::legal_vtype)
+      fprintf (file, " demand_legal_vtype");
+    else if (m_sew_lmul_demand == sew_lmul_demand_type::sew_lmul)
       fprintf (file, " demand_sew_lmul");
     else if (m_sew_lmul_demand == sew_lmul_demand_type::ratio_only)
       fprintf (file, " demand_ratio_only");
@@ -2649,6 +2663,8 @@ private:
       {
        const vsetvl_info &prev_info = *m_vsetvl_def_exprs[expr_index];
        if (!prev_info.valid_p ()
+           || prev_info.get_sew_lmul_demand ()
+                == sew_lmul_demand_type::legal_vtype
            || !m_dem.avl_available_p (prev_info, curr_info)
            || prev_info.get_ratio () != curr_info.get_ratio ())
          return false;
@@ -3664,6 +3680,8 @@ pre_vsetvl::pre_global_vsetvl_info ()
       for (auto &curr_info : block_info.local_infos)
        {
          if (prev_info.valid_p () && curr_info.valid_p ()
+             && prev_info.get_sew_lmul_demand ()
+                  != sew_lmul_demand_type::legal_vtype
              && m_dem.avl_available_p (prev_info, curr_info)
              && prev_info.get_ratio () == curr_info.get_ratio ())
            curr_info.set_change_vtype_only ();
diff --git a/gcc/config/riscv/riscv-vsetvl.def 
b/gcc/config/riscv/riscv-vsetvl.def
index 4b8b556ffac4..79068045920e 100644
--- a/gcc/config/riscv/riscv-vsetvl.def
+++ b/gcc/config/riscv/riscv-vsetvl.def
@@ -49,6 +49,28 @@ along with GCC; see the file COPYING3.  If not see
 #endif
 
 /* Define SEW and LMUL rules.  */
+DEF_SEW_LMUL_RULE (legal_vtype, legal_vtype, legal_vtype, always_true,
+                  always_true, nop)
+DEF_SEW_LMUL_RULE (sew_lmul, legal_vtype, sew_lmul, always_true, always_true,
+                  nop)
+DEF_SEW_LMUL_RULE (legal_vtype, sew_lmul, sew_lmul, always_true, always_false,
+                  use_next_sew_lmul)
+DEF_SEW_LMUL_RULE (ratio_only, legal_vtype, ratio_only, always_true,
+                  always_true, nop)
+DEF_SEW_LMUL_RULE (legal_vtype, ratio_only, ratio_only, always_true,
+                  always_false, use_next_sew_lmul)
+DEF_SEW_LMUL_RULE (sew_only, legal_vtype, sew_only, always_true, always_true,
+                  nop)
+DEF_SEW_LMUL_RULE (legal_vtype, sew_only, sew_only, always_true, always_false,
+                  use_next_sew_lmul)
+DEF_SEW_LMUL_RULE (ge_sew, legal_vtype, ge_sew, always_true, always_true, nop)
+DEF_SEW_LMUL_RULE (legal_vtype, ge_sew, ge_sew, always_true, always_false,
+                  use_next_sew_lmul)
+DEF_SEW_LMUL_RULE (ratio_and_ge_sew, legal_vtype, ratio_and_ge_sew, 
always_true,
+                  always_true, nop)
+DEF_SEW_LMUL_RULE (legal_vtype, ratio_and_ge_sew, ratio_and_ge_sew, 
always_true,
+                  always_false, use_next_sew_lmul)
+
 DEF_SEW_LMUL_RULE (sew_lmul, sew_lmul, sew_lmul, sew_lmul_eq_p, sew_lmul_eq_p,
                   nop)
 DEF_SEW_LMUL_RULE (sew_lmul, ratio_only, sew_lmul, ratio_eq_p, ratio_eq_p, nop)
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/whole-move-vtype-1.c 
b/gcc/testsuite/gcc.target/riscv/rvv/base/whole-move-vtype-1.c
new file mode 100644
index 000000000000..1d39e4042065
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/base/whole-move-vtype-1.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gcv -mabi=lp64d" } */
+
+#include <riscv_vector.h>
+
+/* Returning B needs whole-register copies.  The preceding e32,m1
+   configuration is sufficient; do not switch to the tuple's e8,m1.  */
+vint8m1x8_t
+foo (vint8m8_t a, vint8m1x8_t b, int32_t *out, const int32_t *in, size_t n)
+{
+  vint32m1_t x = __riscv_vle32_v_i32m1 (in, n);
+  __riscv_vse32_v_i32m1 (out, x, n);
+  return b;
+}
+
+/* { dg-final { scan-assembler-times {vsetvli} 1 } } */
+/* { dg-final { scan-assembler-not {vsetivli} } } */
+/* { dg-final { scan-assembler-times {vmv1r\.v} 8 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/whole-move-vtype-2.c 
b/gcc/testsuite/gcc.target/riscv/rvv/base/whole-move-vtype-2.c
new file mode 100644
index 000000000000..be19d3a8793c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/base/whole-move-vtype-2.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gcv -mabi=lp64d" } */
+
+#include <riscv_vector.h>
+
+extern vint32m1_t get_vector (void);
+
+/* The call returns in v8 and leaves VTYPE unspecified.  Establish a
+   legal VTYPE before copying the result to v16.  */
+void
+foo (void)
+{
+  register vint32m1_t x asm ("v16") = get_vector ();
+  asm volatile ("" : : "vr" (x));
+}
+
+/* { dg-final { scan-assembler 
{call\s+get_vector[^\n]*\n\s+vseti?vli\s+[^\n]+\n\s+vmv1r\.v\s+v16,v8} } } */

Reply via email to