https://gcc.gnu.org/g:ef1af9e6a6cc75c29a575295e685d44fe2a16f60

commit r17-6354-gef1af9e6a6cc75c29a575295e685d44fe2a16f60
Author: Jiawei <[email protected]>
Date:   Tue Nov 17 15:06:35 2026 +0800

    RISC-V: Combine Zba shift-add with word rotations
    
    Form shifts by one to three bits and the following addition at XLEN
    width before a word rotation. The low word is unchanged.
    
    The targeted riscv.exp test passes on RV64. C selftests and RV32/RV64
    QEMU checks passed, including arbitrary high input bits and count bounds.
    
    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.

Diff:
---
 gcc/config/riscv/bitmanip.md                   | 28 ++++++++++++++++++++++
 gcc/testsuite/gcc.target/riscv/zba-shNadd-13.c | 33 ++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md
index f145292e91d1..c421c86cf495 100644
--- a/gcc/config/riscv/bitmanip.md
+++ b/gcc/config/riscv/bitmanip.md
@@ -57,6 +57,34 @@
    [(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))))])
 
+;; Shifts by 1, 2 or 3 and addition have the same low word at either
+;; width, even if the widened operands have undefined upper bits.
+;; Form the shift-add at XLEN width and let the word rotation perform
+;; the final sign extension.  Keep its count expression unchanged.
+;; Leave the count predicate empty to accept 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 (match_operand:SI 1 "register_operand")
+                        (match_operand:QI 2 "imm123_operand"))
+             (match_operand:SI 3 "register_operand"))
+           (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 (match_dup 6) (match_dup 4))))]
+{
+  operands[1] = gen_lowpart (DImode, operands[1]);
+  operands[3] = gen_lowpart (DImode, operands[3]);
+  operands[6] = gen_lowpart (SImode, operands[5]);
+})
+
 ; 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 000000000000..a0f4b152c972
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zba-shNadd-13.c
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gc_zba_zbb -mabi=lp64" } */
+
+unsigned int
+rol1 (unsigned long x, unsigned long y, unsigned int n)
+{
+  return __builtin_stdc_rotate_left (((unsigned int) x << 1)
+                                    + (unsigned int) y, n);
+}
+
+unsigned int
+ror2 (unsigned long x, unsigned long y, unsigned int n)
+{
+  /* This mask changes the rotation count and must be retained.  */
+  return __builtin_stdc_rotate_right (((unsigned int) x << 2)
+                                     + (unsigned int) 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" } } */

Reply via email to