1
0
mirror of https://github.com/golang/go synced 2024-11-22 19:05:01 -07:00

cmd/compile: remove redundant nil checks

Optimize them away if we can.

If not, be more careful about splicing them out after scheduling.

Change-Id: I660e54649d753dc456d2e25d389d375a16d76940
Reviewed-on: https://go-review.googlesource.com/c/go/+/627418
Reviewed-by: Shengwei Zhao <wingrez@126.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Keith Randall 2024-11-12 20:14:31 -08:00
parent 76e4efdc77
commit 7d2e3da243
3 changed files with 20 additions and 4 deletions

View File

@ -2065,6 +2065,10 @@
(NilCheck ptr:(Addr {_} (SB)) _) => ptr (NilCheck ptr:(Addr {_} (SB)) _) => ptr
(NilCheck ptr:(Convert (Addr {_} (SB)) _) _) => ptr (NilCheck ptr:(Convert (Addr {_} (SB)) _) _) => ptr
// Nil checks of nil checks are redundant.
// See comment at the end of https://go-review.googlesource.com/c/go/+/537775.
(NilCheck ptr:(NilCheck _ _) _ ) => ptr
// for late-expanded calls, recognize memequal applied to a single constant byte // for late-expanded calls, recognize memequal applied to a single constant byte
// Support is limited by 1, 2, 4, 8 byte sizes // Support is limited by 1, 2, 4, 8 byte sizes
(StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [1]) mem) (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [1]) mem)

View File

@ -20535,6 +20535,16 @@ func rewriteValuegeneric_OpNilCheck(v *Value) bool {
v.copyOf(ptr) v.copyOf(ptr)
return true return true
} }
// match: (NilCheck ptr:(NilCheck _ _) _ )
// result: ptr
for {
ptr := v_0
if ptr.Op != OpNilCheck {
break
}
v.copyOf(ptr)
return true
}
return false return false
} }
func rewriteValuegeneric_OpNot(v *Value) bool { func rewriteValuegeneric_OpNot(v *Value) bool {

View File

@ -314,14 +314,16 @@ func schedule(f *Func) {
for _, b := range f.Blocks { for _, b := range f.Blocks {
for _, v := range b.Values { for _, v := range b.Values {
for i, a := range v.Args { for i, a := range v.Args {
if a.Op == OpSPanchored || opcodeTable[a.Op].nilCheck { for a.Op == OpSPanchored || opcodeTable[a.Op].nilCheck {
v.SetArg(i, a.Args[0]) a = a.Args[0]
v.SetArg(i, a)
} }
} }
} }
for i, c := range b.ControlValues() { for i, c := range b.ControlValues() {
if c.Op == OpSPanchored || opcodeTable[c.Op].nilCheck { for c.Op == OpSPanchored || opcodeTable[c.Op].nilCheck {
b.ReplaceControl(i, c.Args[0]) c = c.Args[0]
b.ReplaceControl(i, c)
} }
} }
} }