cmd/compile: fold boolean x == x & x != x

Change-Id: I0e0a5919536b643477a6f9278fcc60492ea5a759
Reviewed-on: https://go-review.googlesource.com/c/go/+/750540
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Jorropo
2026-03-02 07:28:39 +01:00
committed by Gopher Robot
parent ef160abf2d
commit 74e9214b53
2 changed files with 24 additions and 2 deletions

View File

@@ -273,12 +273,12 @@
(IsSliceInBounds (Const64 [c]) (Const64 [d])) => (ConstBool [0 <= c && c <= d])
(IsSliceInBounds (SliceLen x) (SliceCap x)) => (ConstBool [true])
(Eq(64|32|16|8) x x) => (ConstBool [true])
(Eq(64|32|16|8|B) x x) => (ConstBool [true])
(EqB (ConstBool [c]) (ConstBool [d])) => (ConstBool [c == d])
(EqB (ConstBool [false]) x) => (Not x)
(EqB (ConstBool [true]) x) => x
(Neq(64|32|16|8) x x) => (ConstBool [false])
(Neq(64|32|16|8|B) x x) => (ConstBool [false])
(NeqB (ConstBool [c]) (ConstBool [d])) => (ConstBool [c != d])
(NeqB (ConstBool [false]) x) => x
(NeqB (ConstBool [true]) x) => (Not x)

View File

@@ -8488,6 +8488,17 @@ func rewriteValuegeneric_OpEq8(v *Value) bool {
func rewriteValuegeneric_OpEqB(v *Value) bool {
v_1 := v.Args[1]
v_0 := v.Args[0]
// match: (EqB x x)
// result: (ConstBool [true])
for {
x := v_0
if x != v_1 {
break
}
v.reset(OpConstBool)
v.AuxInt = boolToAuxInt(true)
return true
}
// match: (EqB (ConstBool [c]) (ConstBool [d]))
// result: (ConstBool [c == d])
for {
@@ -19671,6 +19682,17 @@ func rewriteValuegeneric_OpNeq8(v *Value) bool {
func rewriteValuegeneric_OpNeqB(v *Value) bool {
v_1 := v.Args[1]
v_0 := v.Args[0]
// match: (NeqB x x)
// result: (ConstBool [false])
for {
x := v_0
if x != v_1 {
break
}
v.reset(OpConstBool)
v.AuxInt = boolToAuxInt(false)
return true
}
// match: (NeqB (ConstBool [c]) (ConstBool [d]))
// result: (ConstBool [c != d])
for {