cmd/compile: additional optimisation for CZEROEQZ/CZERONEZ on riscv64

Negation on a condition can be eliminated.

Change-Id: I94fab5f019cbaebb2ca589e1d8796a9cb72f3894
Reviewed-on: https://go-review.googlesource.com/c/go/+/748401
Reviewed-by: Xueqi Luo <1824368278@qq.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Julian Zhu <jz531210@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@golang.org>
This commit is contained in:
Joel Sing
2026-02-21 03:29:09 +11:00
committed by Gopher Robot
parent 426ea5cd22
commit 3a29ebeef9
3 changed files with 26 additions and 1 deletions

View File

@@ -866,6 +866,7 @@
(OR (CZEROEQZ <t> x (MOVBUreg <typ.UInt64> cond)) (CZERONEZ <t> y (MOVBUreg <typ.UInt64> cond)))
(CZERO(EQ|NE)Z x (SNEZ y)) => (CZERO(EQ|NE)Z x y)
(CZERO(EQ|NE)Z x (SEQZ y)) => (CZERO(NE|EQ)Z x y)
(CZERO(EQ|NE)Z x (NEG y)) => (CZERO(EQ|NE)Z x y)
(CZEROEQZ x x) => x
(CZERONEZ x x) => (MOVDconst [0])
(CZERO(EQ|NE)Z (MOVDconst [0]) _) => (MOVDconst [0])

View File

@@ -3588,6 +3588,18 @@ func rewriteValueRISCV64_OpRISCV64CZEROEQZ(v *Value) bool {
v.AddArg2(x, y)
return true
}
// match: (CZEROEQZ x (NEG y))
// result: (CZEROEQZ x y)
for {
x := v_0
if v_1.Op != OpRISCV64NEG {
break
}
y := v_1.Args[0]
v.reset(OpRISCV64CZEROEQZ)
v.AddArg2(x, y)
return true
}
// match: (CZEROEQZ x x)
// result: x
for {
@@ -3637,6 +3649,18 @@ func rewriteValueRISCV64_OpRISCV64CZERONEZ(v *Value) bool {
v.AddArg2(x, y)
return true
}
// match: (CZERONEZ x (NEG y))
// result: (CZERONEZ x y)
for {
x := v_0
if v_1.Op != OpRISCV64NEG {
break
}
y := v_1.Args[0]
v.reset(OpRISCV64CZERONEZ)
v.AddArg2(x, y)
return true
}
// match: (CZERONEZ x x)
// result: (MOVDconst [0])
for {

View File

@@ -534,6 +534,6 @@ func constantTimeSelect(v, x, y int) int {
// amd64:"CMOVQ"
// arm64:"CSEL"
// riscv64/rva20u64,riscv64/rva22u64:"SNEZ" "NEG" "AND" "OR"
// riscv64/rva23u64:"NEG" "CZERONEZ" "CZEROEQZ" "OR" -"SNEZ" -"AND"
// riscv64/rva23u64:"CZERONEZ" "CZEROEQZ" "OR" -"SNEZ" -"NEG" -"AND"
return subtle.ConstantTimeSelect(v, x, y)
}