https://gcc.gnu.org/g:fb66e0c3e24743e415c50eff069e61a935134e92
commit r17-6412-gfb66e0c3e24743e415c50eff069e61a935134e92 Author: Jiawei <[email protected]> Date: Tue Nov 17 15:56:59 2026 +0800 RISC-V: Combine zero-extended word masks with Zbs operations Use bset, binv and bclr when a zero-extended word-sized bit mask is combined with a 64-bit value. Match the canonical complemented form directly with Zbs alone, keeping the SImode shift and its count mask. gcc/ChangeLog: * config/riscv/bitmanip.md (*<bit_optab>di_zext_si): New pattern. (*bclrdi_zext_si): Ditto. (*bclrdi_zext_si_canonical): Ditto. gcc/testsuite/ChangeLog: * gcc.target/riscv/zbs-word-mask-2.c: New test. * gcc.target/riscv/zbs-word-mask.c: New test. Diff: --- gcc/config/riscv/bitmanip.md | 43 ++++++++++++++++++++++++ gcc/testsuite/gcc.target/riscv/zbs-word-mask-2.c | 13 +++++++ gcc/testsuite/gcc.target/riscv/zbs-word-mask.c | 25 ++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index c421c86cf495..2871515d5436 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -718,6 +718,49 @@ "bset\t%0,x0,%1" [(set_attr "type" "bitmanip")]) +;; A zero-extended SImode bit mask can also modify a DImode value. +;; Keep the count masking: bset/binv/bclr use six count bits on RV64. +(define_insn "*<bit_optab>di_zext_si" + [(set (match_operand:DI 0 "register_operand" "=r") + (any_or:DI + (zero_extend:DI + (ashift:SI (const_int 1) + (match_operand:QI 1 "register_operand" "r"))) + (match_operand:DI 2 "register_operand" "r")))] + "TARGET_64BIT && TARGET_ZBS" + "<bit_optab>\t%0,%2,%1" + [(set_attr "type" "bitmanip")]) + +(define_insn "*bclrdi_zext_si" + [(set (match_operand:DI 0 "register_operand" "=r") + (and:DI + (not:DI + (zero_extend:DI + (ashift:SI (const_int 1) + (match_operand:QI 1 "register_operand" "r")))) + (match_operand:DI 2 "register_operand" "r")))] + "TARGET_64BIT && TARGET_ZBS" + "bclr\t%0,%2,%1" + [(set_attr "type" "bitmanip")]) + +;; Combine can express a complemented zero-extended word mask using a +;; paradoxical subreg and ones in the upper word. Those ones make the +;; undefined upper bits irrelevant. Keep the shift in SImode so that +;; count masking remains visible in RTL. +(define_insn "*bclrdi_zext_si_canonical" + [(set (match_operand:DI 0 "register_operand" "=r") + (and:DI + (ior:DI + (not:DI + (subreg:DI + (ashift:SI (const_int 1) + (match_operand:QI 1 "register_operand" "r")) 0)) + (const_int -4294967296)) + (match_operand:DI 2 "register_operand" "r")))] + "TARGET_64BIT && TARGET_ZBS" + "bclr\t%0,%2,%1" + [(set_attr "type" "bitmanip")]) + ;; These two splitters take advantage of the limited range of the ;; shift constant. With the limited range we know the SImode sign ;; bit is never set, thus we can treat this as zero extending and diff --git a/gcc/testsuite/gcc.target/riscv/zbs-word-mask-2.c b/gcc/testsuite/gcc.target/riscv/zbs-word-mask-2.c new file mode 100644 index 000000000000..30afe4512a72 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zbs-word-mask-2.c @@ -0,0 +1,13 @@ +/* { dg-do compile { target rv64 } } */ +/* { dg-options "-O2 -march=rv64gc_zbs -mabi=lp64" } */ + +unsigned long +clear (unsigned long x, unsigned int b, unsigned int e) +{ + e >>= b; + return x & ~(unsigned long) (1U << (e & 31)); +} + +/* { dg-final { scan-assembler-times "srlw\t" 1 } } */ +/* { dg-final { scan-assembler-times "andi\t" 1 } } */ +/* { dg-final { scan-assembler-times "bclr\t" 1 } } */ diff --git a/gcc/testsuite/gcc.target/riscv/zbs-word-mask.c b/gcc/testsuite/gcc.target/riscv/zbs-word-mask.c new file mode 100644 index 000000000000..7d2ed5b392bd --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zbs-word-mask.c @@ -0,0 +1,25 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc_zbb_zbs -mabi=lp64" } */ + +unsigned long +set (unsigned long x, unsigned int n) +{ + return x | (unsigned long) (1U << (n & 31)); +} + +unsigned long +invert (unsigned long x, unsigned int n) +{ + return x ^ (unsigned long) (1U << (n & 31)); +} + +unsigned long +clear (unsigned long x, unsigned int n) +{ + return x & ~(unsigned long) (1U << (n & 31)); +} + +/* { dg-final { scan-assembler-times "bset\t" 1 } } */ +/* { dg-final { scan-assembler-times "binv\t" 1 } } */ +/* { dg-final { scan-assembler-times "bclr\t" 1 } } */ +/* { dg-final { scan-assembler-times "andi\t" 3 } } */
