Cost word packing as one instruction for IOR, XOR and PLUS. This lets
combine select pack when Zba is enabled. Keep the existing no-Zba test
and add a Zba variant that also checks the old sequence is removed.

The targeted riscv.exp tests pass on RV64. C selftests and QEMU checks
on RV32 and RV64 also passed.

At -O2 with RV64 Zba+Zbkb, the packing microbenchmark retires 11.10%
fewer instructions in gem5; cycles decrease by 9.87% on Minor and
32.77% on O3. For the complete SQLite varint decoder, instructions
decrease by 0.94%; Minor cycles decrease by 0.38%, while O3 cycles
increase by 0.63%. A direct pack instruction probe raised SIGILL on K1,
so this optimization was measured only in gem5.

gcc/ChangeLog:

        * config/riscv/riscv.cc (riscv_rtx_costs): Cost word packing as one
        instruction for IOR, XOR and PLUS.

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/pack64-zba.c: New test.
---

 gcc/config/riscv/riscv.cc                   | 21 +++++++++++++++++++++
 gcc/testsuite/gcc.target/riscv/pack64-zba.c |  9 +++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/pack64-zba.c

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index d83e26351c9..ae4fa0efb56 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -4598,6 +4598,27 @@ riscv_rtx_costs (rtx x, machine_mode mode, int 
outer_code, int opno ATTRIBUTE_UN
       return true;
     }
 
+  /* Match the exposed RTL of riscv_xpack_<code>_<X:mode>_<HX:mode>_2.
+     All three operations combine disjoint halves into one pack.  */
+  if (TARGET_ZBKB
+      && mode == word_mode
+      && GET_MODE (x) == mode
+      && (GET_CODE (x) == IOR || GET_CODE (x) == XOR
+         || GET_CODE (x) == PLUS)
+      && GET_CODE (XEXP (x, 0)) == ASHIFT
+      && GET_MODE (XEXP (x, 0)) == mode
+      && register_operand (XEXP (XEXP (x, 0), 0), mode)
+      && CONST_INT_P (XEXP (XEXP (x, 0), 1))
+      && INTVAL (XEXP (XEXP (x, 0), 1)) == BITS_PER_WORD / 2
+      && GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
+      && GET_MODE (XEXP (x, 1)) == mode
+      && register_operand (XEXP (XEXP (x, 1), 0),
+                          TARGET_64BIT ? SImode : HImode))
+    {
+      *total = COSTS_N_INSNS (1);
+      return true;
+    }
+
   bool float_mode_p = FLOAT_MODE_P (mode);
   int cost;
 
diff --git a/gcc/testsuite/gcc.target/riscv/pack64-zba.c 
b/gcc/testsuite/gcc.target/riscv/pack64-zba.c
new file mode 100644
index 00000000000..ef92a43ffee
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pack64-zba.c
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gc_zba_zbkb -mabi=lp64" } */
+/* { dg-skip-if "" { *-*-* } { "-g" "-flto" "-O0" } } */
+
+#include "pack64.c"
+
+/* { dg-final { scan-assembler-times "\\spack\\s" 2 } } */
+/* { dg-final { scan-assembler-not "\\sslli\\s" } } */
+/* { dg-final { scan-assembler-not "\\sadd\\.uw\\s" } } */

Reply via email to