Eliminate redundant count masks before bext and seqz.
gcc/ChangeLog:
* config/riscv/bitmanip.md (*bextseqz<X:mode>_mask): New pattern.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/zbs-bext-3.c: New test.
---
diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md
index
d710d594def0fcc41a6109c9eadef7704d24486e..5483ee34be86cf50ba3884e41c7d980e642f4653
100644
--- a/gcc/config/riscv/bitmanip.md
+++ b/gcc/config/riscv/bitmanip.md
@@ -1101,6 +1101,36 @@
"operands[1] = gen_lowpart (word_mode, operands[1]);"
[(set_attr "type" "bitmanip")])
+;; Keep the masked count in the compound pattern until it is split into
+;; bext and seqz. Otherwise combine leaves a separate masking instruction.
+(define_insn_and_split "*bextseqz<X:mode>_mask"
+ [(set (match_operand:X 0 "register_operand" "=r")
+ (and:X
+ (not:X
+ (lshiftrt:X
+ (match_operand:X 1 "register_operand" "r")
+ (match_operator 4 "subreg_lowpart_operator"
+ [(and:GPR (match_operand:GPR 2 "register_operand" "r")
+ (match_operand 3 "<X:shiftm1>"))])))
+ (const_int 1)))]
+ "TARGET_ZBS"
+ "#"
+ "&& 1"
+ [(set (match_dup 0)
+ (zero_extract:X (match_dup 1)
+ (const_int 1)
+ (match_dup 4)))
+ (set (match_dup 0) (eq:X (match_dup 0) (const_int 0)))]
+{
+ /* Keep the bit position in range in the split RTL. The masked bext
+ patterns omit this mask when emitting the instruction. */
+ operands[4] = gen_rtx_AND (<GPR:MODE>mode, operands[2],
+ GEN_INT (GET_MODE_BITSIZE (<X:MODE>mode) - 1));
+ if (<GPR:MODE>mode != <X:MODE>mode)
+ operands[4] = gen_rtx_ZERO_EXTEND (<X:MODE>mode, operands[4]);
+}
+ [(set_attr "type" "bitmanip")])
+
;; The logical-and against 0x1 implicitly extends the result. So we can treat
;; an SImode bext as-if it's DImode without any explicit extension.
(define_insn "*bextdisi"
diff --git a/gcc/testsuite/gcc.target/riscv/zbs-bext-3.c
b/gcc/testsuite/gcc.target/riscv/zbs-bext-3.c
new file mode 100644
index
0000000000000000000000000000000000000000..3c0eea56999727a8de0e3ae9d4ae05f38f6701d0
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zbs-bext-3.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zbs -mabi=lp64" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zbs -mabi=ilp32" { target { rv32 } } } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */
+
+unsigned long
+foo (unsigned long x, unsigned int n)
+{
+ return (~(x >> (n & (__riscv_xlen - 1)))) & 1UL;
+}
+
+/* A narrower mask must be retained. */
+unsigned long
+bar (unsigned long x, unsigned int n)
+{
+ return (~(x >> (n & 7))) & 1UL;
+}
+
+/* { dg-final { scan-assembler-times "bext\t" 2 } } */
+/* { dg-final { scan-assembler-times "seqz\t" 2 } } */
+/* { dg-final { scan-assembler-times "andi\t" 1 } } */