LLVM review of x264 code on RISC-V showed that we were missing splitters
for (LT/LTU & EQ/NE) and (LE/LEU & EQ/NE). Essentially with the logical
AND we can get the operands in either order and a subset of the
splitters didn't have forms with the two operands reversed.
This adds the two missing splitters. It should save ~10M instructions
on x264 input #1, so pretty tiny, but very clearly an oversight in
zicond.md.
Regression tested on riscv32-elf and riscv64-elf. Bootstrapped and
regression tested on the C920 and K3. K1 is queued. Pushing to the trunk.
jeff
commit 21737cb4a88facdf0a039ff5785f84ce8329b9fc
Author: Jeff Law <[email protected]>
Date: Thu Nov 5 07:23:32 2026 -0700
[RISC-V] Add missing splitters for zicond sequences
LLVM review of x264 code on RISC-V showed that we were missing splitters for
(LT/LTU & EQ/NE) and (LE/LEU & EQ/NE). Essentially with the logical AND we
can
get the operands in either order and a subset of the splitters didn't have
forms with the two operands reversed.
This adds the two missing splitters. It should save ~10M instructions on
x264
input #1, so pretty tiny, but very clearly an oversight in zicond.md.
Regression tested on riscv32-elf and riscv64-elf. Bootstrapped and
regression
tested on the C920 and K3. K1 is queued. Pushing to the trunk.
gcc/
* config/riscv/zicond.md (zicond splitters): Add missing splits for
(LT/LTU & EQ_NE) and (LE/LEU & EQ/NE).
gcc/testsuite
* gcc.target/riscv/pr57650-2.c: New test.
diff --git a/gcc/config/riscv/zicond.md b/gcc/config/riscv/zicond.md
index b07aec8feb2..859096ead21 100644
--- a/gcc/config/riscv/zicond.md
+++ b/gcc/config/riscv/zicond.md
@@ -192,6 +192,24 @@
operands[2],
CONST0_RTX (GET_MODE (operands[1]))); })
+(define_split
+ [(set (match_operand:X 0 "register_operand")
+ (and:X (any_lt:X (match_operand:X 3 "register_operand")
+ (match_operand:X 4 "arith_operand"))
+ (match_operator:X 1 "equality_operator"
+ [(match_operand:X 2 "register_operand") (const_int 0)])))
+ (clobber (match_operand:X 5 "register_operand"))]
+ "TARGET_ZICOND || TARGET_XTHEADCONDMOV"
+ [(set (match_dup 5) (any_lt:X (match_dup 3) (match_dup 4)))
+ (set (match_dup 0) (if_then_else:X (match_op_dup:X 1
+ [(match_dup 2) (const_int 0)])
+ (const_int 0)
+ (match_dup 5)))]
+ { operands[1] = gen_rtx_fmt_ee (GET_CODE (operands[1]) == EQ ? NE : EQ,
+ GET_MODE (operands[1]),
+ operands[2],
+ CONST0_RTX (GET_MODE (operands[1]))); })
+
;; Finally LE/LEU which requires sle_operand.
(define_split
[(set (match_operand:X 0 "register_operand")
@@ -211,6 +229,24 @@
operands[2],
CONST0_RTX (GET_MODE (operands[1]))); })
+(define_split
+ [(set (match_operand:X 0 "register_operand")
+ (and:X (any_le:X (match_operand:X 3 "register_operand")
+ (match_operand:X 4 "sle_operand"))
+ (match_operator:X 1 "equality_operator"
+ [(match_operand:X 2 "register_operand") (const_int 0)])))
+ (clobber (match_operand:X 5 "register_operand"))]
+ "TARGET_ZICOND || TARGET_XTHEADCONDMOV"
+ [(set (match_dup 5) (any_le:X (match_dup 3) (match_dup 4)))
+ (set (match_dup 0) (if_then_else:X (match_op_dup:X 1
+ [(match_dup 2) (const_int 0)])
+ (const_int 0)
+ (match_dup 5)))]
+ { operands[1] = gen_rtx_fmt_ee (GET_CODE (operands[1]) == EQ ? NE : EQ,
+ GET_MODE (operands[1]),
+ operands[2],
+ CONST0_RTX (GET_MODE (operands[1]))); })
+
;; We can splat the sign bit across a GPR with an arithmetic right shift
;; which gives us a 0, -1 result. We then turn on bit #0 unconditionally
;; which results in 1, -1. There's probably other cases that could be
diff --git a/gcc/testsuite/gcc.target/riscv/pr57650-2.c
b/gcc/testsuite/gcc.target/riscv/pr57650-2.c
new file mode 100644
index 00000000000..c5a5451ed11
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pr57650-2.c
@@ -0,0 +1,37 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gc_zicond -mabi=lp64d" { target rv64 } } */
+/* { dg-options "-O2 -march=rv64gc_zicond -mabi=lp64d" { target rv32 } } */
+
+/* (p != 0) & (s CC t) is p ? (s CC t) : 0, i.e. an sCC feeding czero.eqz. */
+
+/* lt: register operand. */
+long
+f_lt (const void *p, long s, long t)
+{
+ return (p != 0) & (s < t);
+}
+
+/* ltu: register operand. */
+long
+f_ltu (const void *p, unsigned long s, unsigned long t)
+{
+ return (p != 0) & (s < t);
+}
+
+/* le: s < 4 is canonicalized to s <= 3. */
+long
+f_le (const void *p, long s)
+{
+ return (p != 0) & (s < 4);
+}
+
+/* leu: s < 4 is canonicalized to s <=u 3. */
+long
+f_leu (const void *p, unsigned long s)
+{
+ return (p != 0) & (s < 4);
+}
+
+/* { dg-final { scan-assembler-times "czero.eqz" 4 } } */
+/* { dg-final { scan-assembler-not "snez" } } */
+