Cost recognized pack instructions as one instruction in riscv_insn_cost,
using a new pack subtype. This lets combine select pack with Zba enabled.
Keep the existing no-Zba test and add a Zba variant that checks the old
sequence is removed.
The pack-family riscv.exp tests pass for RV32 and 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 was measured on gem5 only.
gcc/ChangeLog:
* config/riscv/crypto.md (riscv_pack_<X:mode><HX:mode>)
(riscv_xpack_<code>_<X:mode>_<HX:mode>_2): Set subtype to pack.
* config/riscv/riscv.cc (riscv_insn_cost): Cost pack as one
instruction by using subtype.
* config/riscv/riscv.md (subtype): New attribute.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/pack64-zba.c: New test.
---
Changes in v2: Use a pack subtype and riscv_insn_cost instead of
matching the RTL in riscv_rtx_costs.
gcc/config/riscv/crypto.md | 6 ++++--
gcc/config/riscv/riscv.cc | 5 +++++
gcc/config/riscv/riscv.md | 3 +++
gcc/testsuite/gcc.target/riscv/pack64-zba.c | 9 +++++++++
4 files changed, 21 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gcc.target/riscv/pack64-zba.c
diff --git a/gcc/config/riscv/crypto.md b/gcc/config/riscv/crypto.md
index 8ad673579bb..4261a7d28e9 100644
--- a/gcc/config/riscv/crypto.md
+++ b/gcc/config/riscv/crypto.md
@@ -102,7 +102,8 @@
UNSPEC_PACK))]
"TARGET_ZBKB"
"pack\t%0,%1,%2"
- [(set_attr "type" "crypto")])
+ [(set_attr "type" "crypto")
+ (set_attr "subtype" "pack")])
;; This is slightly more complex than the other pack patterns
;; that fully expose the RTL as it needs to self-adjust to
@@ -115,7 +116,8 @@
(match_operand:HX 3 "register_operand" "r"))))]
"TARGET_ZBKB && INTVAL (operands[2]) == BITS_PER_WORD / 2"
"pack\t%0,%3,%1"
- [(set_attr "type" "crypto")])
+ [(set_attr "type" "crypto")
+ (set_attr "subtype" "pack")])
(define_insn "riscv_packh_<mode>"
[(set (match_operand:X 0 "register_operand" "=r")
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index d83e26351c9..3b68e8a4816 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -5273,6 +5273,11 @@ riscv_address_cost (rtx addr, machine_mode mode,
static int
riscv_insn_cost (rtx_insn *insn, bool speed)
{
+ if (recog_memoized (insn) >= 0
+ && get_attr_type (insn) == TYPE_CRYPTO
+ && get_attr_subtype (insn) == SUBTYPE_PACK)
+ return COSTS_N_INSNS (1);
+
rtx x = PATTERN (insn);
int cost = pattern_cost (x, speed);
diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
index 4135b60be6c..46bf20d6fa5 100644
--- a/gcc/config/riscv/riscv.md
+++ b/gcc/config/riscv/riscv.md
@@ -555,6 +555,9 @@
(eq_attr "move_type" "rdvlenb") (const_string "rdvlenb")]
(const_string "unknown")))
+;; Classify instructions within a type.
+(define_attr "subtype" "none,pack" (const_string "none"))
+
;; True if the float point vector is disabled.
(define_attr "fp_vector_disabled" "no,yes"
(cond [
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" } } */