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))))])
+
; Zba does not provide W-forms of sh[123]add(.uw)?, which leads to an
; interesting irregularity: we can generate a signed 32-bit result
; using slli(.uw)?+ addw, but an unsigned 32-bit result can be more
diff --git a/gcc/testsuite/gcc.target/riscv/zba-shNadd-13.c
b/gcc/testsuite/gcc.target/riscv/zba-shNadd-13.c
new file mode 100644
index 00000000000..47907221ef9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zba-shNadd-13.c
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gc_zba_zbb -mabi=lp64" } */
+
+unsigned int
+rol1 (unsigned int x, unsigned int y, unsigned int n)
+{
+ return __builtin_stdc_rotate_left ((x << 1) + y, n);
+}
+
+unsigned int
+ror2 (unsigned int x, unsigned int y, unsigned int n)
+{
+ /* This mask changes the rotation count and must be retained. */
+ return __builtin_stdc_rotate_right ((x << 2) + y, n & 7);
+}
+
+unsigned int
+ror3 (unsigned int x, unsigned int y)
+{
+ return __builtin_stdc_rotate_right ((x << 3) + y, 7);
+}
+
+/* { dg-final { scan-assembler-times "sh1add\t" 1 } } */
+/* { dg-final { scan-assembler-times "sh2add\t" 1 } } */
+/* { dg-final { scan-assembler-times "sh3add\t" 1 } } */
+/* { dg-final { scan-assembler-times "rolw\t" 1 } } */
+/* { dg-final { scan-assembler-times "rorw\t" 1 } } */
+/* { dg-final { scan-assembler-times "roriw\t" 1 } } */
+/* { dg-final { scan-assembler-times {andi\t[^\n]*,7\n} 1 } } */
+/* { dg-final { scan-assembler-not "slliw\t" } } */
+/* { dg-final { scan-assembler-not "addw\t" } } */