On 11/9/26 9:53 AM, Jiawei wrote:
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")])
Any chance this can be a simpler define_split? It looks like you're
generating two output insns and it looks likely you have 3 or 4 input
insns. We generally prefer define_split over define_insn_and_split as
the latter bypasses some critical combine costing heuristics and the
output RTL is not generally available to further optimize. Note we may
also get xori dest,src,1 rather than seqz. So we probably need to
account for that possibility in the scan output.
No issues flagged by Sashiko. OK with the changes noted above.
Thanks,
Jeff
ps. I just realized you don't have commit privileges. We should have
probably have corrected that a while back.
http://gcc.gnu.org/gitwrite.html should have a pointer to a form to fill
out. List me as your sponsor.