On 11/16/26 5:22 AM, Jiawei wrote:
Form the shift-add at XLEN width before a word rotation. The rotation
uses only the low word and supplies the final sign extension. Preserve
the count expression for the existing rotate patterns.
The targeted riscv.exp test passes on RV64. Code-generation checks with
Zba and either Zbb or Zbkb passed at O2 and Os. C selftests and QEMU
checks on RV32 and RV64 also passed.
At -O2 with RV64 Zba+Zbb, the left- and right-rotation microbenchmarks
retire 9.99% fewer instructions in gem5 and about 10.00% fewer on K1.
With 64-byte-aligned buffers, Minor/O3 cycles are unchanged; K1 cycles
decrease by 19.78-19.79%. The original 8-byte-aligned layout increased
gem5 O3 cycles by 98.64% through an LSQ address-aliasing effect, which
was removed by 64-byte alignment.
gcc/ChangeLog:
* config/riscv/bitmanip.md: Add a split for shift-add followed by
word rotations.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/zba-shNadd-13.c: New test.
---
gcc/config/riscv/bitmanip.md | 22 +++++++++++++
.../gcc.target/riscv/zba-shNadd-13.c | 31 +++++++++++++++++++
2 files changed, 53 insertions(+)
create mode 100644 gcc/testsuite/gcc.target/riscv/zba-shNadd-13.c
diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md
index dcb10cb4d47..7f461e49d02 100644
--- a/gcc/config/riscv/bitmanip.md
+++ b/gcc/config/riscv/bitmanip.md
@@ -57,6 +57,28 @@
[(set (match_dup 5) (plus:DI (ashift:DI (match_dup 1) (match_dup 2))
(match_dup 3)))
(set (match_dup 0) (sign_extend:DI (any_div:SI (subreg:SI (match_dup 5)
0) (match_dup 4))))])
+;; Word rotations only use the low 32 bits of their input. Form the
+;; shift-add at XLEN width and let rolw/rorw perform the final sign extension.
+;; Leave the count predicate empty to preserve masked expressions as well
+;; as register and immediate counts for the existing rotate patterns.
+(define_split
+ [(set (match_operand:DI 0 "register_operand")
+ (sign_extend:DI
+ (bitmanip_rotate:SI
+ (plus:SI
+ (ashift:SI (subreg:SI
+ (match_operand:DI 1 "register_operand") 0)
+ (match_operand:QI 2 "imm123_operand"))
+ (subreg:SI (match_operand:DI 3 "register_operand") 0))
+ (match_operand 4))))
+ (clobber (match_operand:DI 5 "register_operand"))]
+ "TARGET_64BIT && TARGET_ZBA && (TARGET_ZBB || TARGET_ZBKB)"
+ [(set (match_dup 5)
+ (plus:DI (ashift:DI (match_dup 1) (match_dup 2)) (match_dup 3)))
+ (set (match_dup 0)
+ (sign_extend:DI
+ (bitmanip_rotate:SI (subreg:SI (match_dup 5) 0) (match_dup 4))))])
I worry about the rewriting the SI shift into DI here too. I think we
need to look at that more closely given the past problems we've had with
this.
In general, don't write (subreg:M (match_operand:N "register_operand") 0)
Intead use (match_operand:M "register_operand"). Generic parts of GCC
should do the right thing automatically.
On the output side, use gen_lowpart if you want to get the lower part of
a register rather than explicit subreg if you can.
Jeff