Hi, han. I understand you are trying to support optimize vector-splat_vector 
into vector-scalar in "expand" stage, that is,


vv -> vx or vv -> vf.


It's a known issue that we know for a long time.


This patch is trying to transform vv->vf when the splat vector is duplicate 
from a constant (by recognize it is a CONST_VECTOR in expand stage),
but can't transform vv->vf when splat vector is duplicate from a 
register. 


For example, like a[i] = b[i] > x ? c[i] : d[i], the x is a register, this 
case can not be optimized with your patch.


Actually, we have a solution to do all possible transformation (including the 
case I mentioned above) from vv to vx or vf by late-combine PASS which
is contributed by ARM Richard 
Sandiford: https://patchwork.ozlabs.org/project/gcc/patch/mptr0ljn9eh....@arm.com/
You can try to apply this patch and experiment it locally yourself.


And I believe it will be landed in GCC-15. So I don't think we need this patch 
to do the optimization.


Thanks.
 
------------------ Original ------------------
From: &nbsp;"demin.han"<demin....@starfivetech.com&gt;;
Date: &nbsp;Fri, Mar 1, 2024 02:27 PM
To: &nbsp;"gcc-patches"<gcc-patches@gcc.gnu.org&gt;; 
Cc: &nbsp;"juzhe.zhong"<juzhe.zh...@rivai.ai&gt;; 
"kito.cheng"<kito.ch...@gmail.com&gt;; "Li, Pan2"<pan2...@intel.com&gt;; 
"jeffreyalaw"<jeffreya...@gmail.com&gt;; 
Subject: &nbsp;[PATCH 3/5] RISC-V: Support vmfxx.vf for autovec comparison of 
vec and imm

&nbsp;

Currently, following instructions generated in autovector:
&nbsp;&nbsp;&nbsp; flw
&nbsp;&nbsp;&nbsp; vsetvli
&nbsp;&nbsp;&nbsp; vfmv.v.f
&nbsp;&nbsp;&nbsp; ...
&nbsp;&nbsp;&nbsp; vmfxx.vv
Two issues:
&nbsp; 1. Additional vsetvl and vfmv instructions
&nbsp; 2. Occupy one vector register and may results in smaller lmul

We expect:
&nbsp;&nbsp;&nbsp; flw
&nbsp;&nbsp;&nbsp; ...
&nbsp;&nbsp;&nbsp; vmfxx.vf

Tested on RV32 and RV64

gcc/ChangeLog:

        * config/riscv/autovec.md: Accept imm
        * config/riscv/riscv-v.cc (get_cmp_insn_code): Select scalar pattern
        (expand_vec_cmp): Ditto
        * config/riscv/riscv.cc (riscv_const_insns): Exclude float mode

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/rvv/autovec/cmp/vcond-1.c: Add new tests

Signed-off-by: demin.han <demin....@starfivetech.com&gt;
---
&nbsp;gcc/config/riscv/autovec.md&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 |&nbsp; 2 +-
&nbsp;gcc/config/riscv/riscv-v.cc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 | 23 +++++++++----
&nbsp;gcc/config/riscv/riscv.cc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 |&nbsp; 2 +-
&nbsp;.../riscv/rvv/autovec/cmp/vcond-1.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 | 34 +++++++++++++++++++
&nbsp;4 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/gcc/config/riscv/autovec.md b/gcc/config/riscv/autovec.md
index 3b32369f68c..6cfb0800c45 100644
--- a/gcc/config/riscv/autovec.md
+++ b/gcc/config/riscv/autovec.md
@@ -690,7 +690,7 @@ (define_expand "vec_cmp<mode&gt;<vm&gt;"
&nbsp;&nbsp; [(set (match_operand:<VM&gt; 0 "register_operand")
&nbsp;  (match_operator:<VM&gt; 1 "comparison_operator"
&nbsp;  &nbsp; [(match_operand:V_VLSF 2 "register_operand")
-       &nbsp;&nbsp; (match_operand:V_VLSF 3 "register_operand")]))]
+       &nbsp;&nbsp; (match_operand:V_VLSF 3 "nonmemory_operand")]))]
&nbsp;&nbsp; "TARGET_VECTOR"
&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp; riscv_vector::expand_vec_cmp_float (operands[0], 
GET_CODE (operands[1]),
diff --git a/gcc/config/riscv/riscv-v.cc b/gcc/config/riscv/riscv-v.cc
index 14e75b9a117..2a188ac78e0 100644
--- a/gcc/config/riscv/riscv-v.cc
+++ b/gcc/config/riscv/riscv-v.cc
@@ -2610,9 +2610,15 @@ expand_vec_init (rtx target, rtx vals)
&nbsp;/* Get insn code for corresponding comparison.&nbsp; */
&nbsp;
&nbsp;static insn_code
-get_cmp_insn_code (rtx_code code, machine_mode mode)
+get_cmp_insn_code (rtx_code code, machine_mode mode, bool scalar_p)
&nbsp;{
&nbsp;&nbsp; insn_code icode;
+&nbsp; if (FLOAT_MODE_P (mode))
+&nbsp;&nbsp;&nbsp; {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; icode = !scalar_p ? code_for_pred_cmp (mode)
+                       : code_for_pred_cmp_scalar (mode);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return icode;
+&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp; switch (code)
&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp; case EQ:
@@ -2628,10 +2634,7 @@ get_cmp_insn_code (rtx_code code, machine_mode mode)
&nbsp;&nbsp;&nbsp;&nbsp; case LTU:
&nbsp;&nbsp;&nbsp;&nbsp; case GE:
&nbsp;&nbsp;&nbsp;&nbsp; case GEU:
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (FLOAT_MODE_P (mode))
-       icode = code_for_pred_cmp (mode);
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else
-       icode = code_for_pred_ltge (mode);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; icode = code_for_pred_ltge (mode);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;
&nbsp;&nbsp;&nbsp;&nbsp; default:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gcc_unreachable ();
@@ -2757,7 +2760,6 @@ expand_vec_cmp (rtx target, rtx_code code, rtx op0, rtx 
op1, rtx mask,
&nbsp;{
&nbsp;&nbsp; machine_mode mask_mode = GET_MODE (target);
&nbsp;&nbsp; machine_mode data_mode = GET_MODE (op0);
-&nbsp; insn_code icode = get_cmp_insn_code (code, data_mode);
&nbsp;
&nbsp;&nbsp; if (code == LTGT)
&nbsp;&nbsp;&nbsp;&nbsp; {
@@ -2765,12 +2767,19 @@ expand_vec_cmp (rtx target, rtx_code code, rtx op0, rtx 
op1, rtx mask,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rtx gt = gen_reg_rtx (mask_mode);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; expand_vec_cmp (lt, LT, op0, op1, mask, 
maskoff);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; expand_vec_cmp (gt, GT, op0, op1, mask, 
maskoff);
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; icode = code_for_pred (IOR, mask_mode);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insn_code icode = code_for_pred (IOR, 
mask_mode);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rtx ops[] = {target, lt, gt};
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emit_vlmax_insn (icode, BINARY_MASK_OP, 
ops);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;
&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;
+&nbsp; rtx elt;
+&nbsp; machine_mode scalar_mode = GET_MODE_INNER (GET_MODE (op1));
+&nbsp; bool scalar_p = const_vec_duplicate_p (op1, &amp;elt) &amp;&amp; 
FLOAT_MODE_P (data_mode);
+&nbsp; if (scalar_p)
+&nbsp;&nbsp;&nbsp; op1 = force_reg (scalar_mode, elt);
+&nbsp; insn_code icode = get_cmp_insn_code (code, data_mode, scalar_p);
+
&nbsp;&nbsp; rtx cmp = gen_rtx_fmt_ee (code, mask_mode, op0, op1);
&nbsp;&nbsp; if (!mask &amp;&amp; !maskoff)
&nbsp;&nbsp;&nbsp;&nbsp; {
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 4100abc9dd1..1ffe4865c19 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -1760,7 +1760,7 @@ riscv_const_insns (rtx x)
&nbsp;          &nbsp;&nbsp; register vec_duplicate into vmv.v.x.&nbsp; */
&nbsp;          scalar_mode smode = GET_MODE_INNER (GET_MODE (x));
&nbsp;          if (maybe_gt (GET_MODE_SIZE (smode), UNITS_PER_WORD)
-               &nbsp;&nbsp;&nbsp; &amp;&amp; !immediate_operand (elt, Pmode))
+               &nbsp;&nbsp;&nbsp; &amp;&amp; !FLOAT_MODE_P (smode) &amp;&amp; 
!immediate_operand (elt, Pmode))
&nbsp;          &nbsp; return 0;
&nbsp;          /* Constants from -16 to 15 can be loaded with vmv.v.i.
&nbsp;          &nbsp;&nbsp; The Wc0, Wc1 constraints are already covered by the
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/cmp/vcond-1.c 
b/gcc/testsuite/gcc.target/riscv/rvv/autovec/cmp/vcond-1.c
index 99a230d1c8a..7f6738518ee 100644
--- a/gcc/testsuite/gcc.target/riscv/rvv/autovec/cmp/vcond-1.c
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/cmp/vcond-1.c
@@ -141,6 +141,34 @@
&nbsp;TEST_VAR_ALL (DEF_VCOND_VAR)
&nbsp;TEST_IMM_ALL (DEF_VCOND_IMM)
&nbsp;
+#define TEST_COND_IMM_FLOAT(T, COND, IMM, SUFFIX)                      \
+&nbsp; T (float, float, COND, IMM, SUFFIX##_float_float)                       
\
+&nbsp; T (double, double, COND, IMM, SUFFIX##_double_double)
+
+#define TEST_IMM_FLOAT_ALL(T)                                          \
+&nbsp; TEST_COND_IMM_FLOAT (T, &gt;, 0.0, _gt)                                 
\
+&nbsp; TEST_COND_IMM_FLOAT (T, <, 0.0, _lt)                                    
\
+&nbsp; TEST_COND_IMM_FLOAT (T, &gt;=, 0.0, _ge)                                
        \
+&nbsp; TEST_COND_IMM_FLOAT (T, <=, 0.0, _le)                                   
\
+&nbsp; TEST_COND_IMM_FLOAT (T, ==, 0.0, _eq)                                   
\
+&nbsp; TEST_COND_IMM_FLOAT (T, !=, 0.0, _ne)                                   
\
+                                                                       \
+&nbsp; TEST_COND_IMM_FLOAT (T, &gt;, 1.0, _gt1)                                
        \
+&nbsp; TEST_COND_IMM_FLOAT (T, <, 1.0, _lt1)                                   
\
+&nbsp; TEST_COND_IMM_FLOAT (T, &gt;=, 1.0, _ge1)                               
\
+&nbsp; TEST_COND_IMM_FLOAT (T, <=, 1.0, _le1)                          \
+&nbsp; TEST_COND_IMM_FLOAT (T, ==, 1.0, _eq1)                          \
+&nbsp; TEST_COND_IMM_FLOAT (T, !=, 1.0, _ne1)                          \
+                                                                       \
+&nbsp; TEST_COND_IMM_FLOAT (T, &gt;, -1.0, _gt2)                               
\
+&nbsp; TEST_COND_IMM_FLOAT (T, <, -1.0, _lt2)                          \
+&nbsp; TEST_COND_IMM_FLOAT (T, &gt;=, -1.0, _ge2)                              
\
+&nbsp; TEST_COND_IMM_FLOAT (T, <=, -1.0, _le2)                         \
+&nbsp; TEST_COND_IMM_FLOAT (T, ==, -1.0, _eq2)                         \
+&nbsp; TEST_COND_IMM_FLOAT (T, !=, -1.0, _ne2)
+
+TEST_IMM_FLOAT_ALL (DEF_VCOND_IMM)
+
&nbsp;/* { dg-final { scan-assembler-times {\tvmseq\.vi} 42 } } */
&nbsp;/* { dg-final { scan-assembler-times {\tvmsne\.vi} 42 } } */
&nbsp;/* { dg-final { scan-assembler-times {\tvmsgt\.vi} 30 } } */
@@ -155,3 +183,9 @@ TEST_IMM_ALL (DEF_VCOND_IMM)
&nbsp;/* { dg-final { scan-assembler-times {\tvmslt} 38 } } */
&nbsp;/* { dg-final { scan-assembler-times {\tvmsge} 38 } } */
&nbsp;/* { dg-final { scan-assembler-times {\tvmsle} 82 } } */
+/* { dg-final { scan-assembler-times {\tvmfgt.vf} 6 } } */
+/* { dg-final { scan-assembler-times {\tvmflt.vf} 6 } } */
+/* { dg-final { scan-assembler-times {\tvmfge.vf} 6 } } */
+/* { dg-final { scan-assembler-times {\tvmfle.vf} 6 } } */
+/* { dg-final { scan-assembler-times {\tvmfeq.vf} 6 } } */
+/* { dg-final { scan-assembler-times {\tvmfne.vf} 6 } } */
-- 
2.43.2

Reply via email to