]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: remove redundant nil checks
authorKeith Randall <khr@golang.org>
Wed, 13 Nov 2024 04:14:31 +0000 (20:14 -0800)
committerKeith Randall <khr@golang.org>
Wed, 20 Nov 2024 16:28:52 +0000 (16:28 +0000)
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>

src/cmd/compile/internal/ssa/_gen/generic.rules
src/cmd/compile/internal/ssa/rewritegeneric.go
src/cmd/compile/internal/ssa/schedule.go

index 2c79c9dcb0f090c070032a785171609d02cc3e6f..9a2bb96a1b2d94d58c539a258d4a149d0f03c0ad 100644 (file)
 (NilCheck          ptr:(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
 // Support is limited by 1, 2, 4, 8 byte sizes
 (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [1]) mem)
index 59199379812e5e64b6b90bc23d2033865cdbbdd1..e4d6b45cf2cb8b37ffddbdaca99b075915761670 100644 (file)
@@ -20535,6 +20535,16 @@ func rewriteValuegeneric_OpNilCheck(v *Value) bool {
                v.copyOf(ptr)
                return true
        }
+       // match: (NilCheck ptr:(NilCheck _ _) _ )
+       // result: ptr
+       for {
+               ptr := v_0
+               if ptr.Op != OpNilCheck {
+                       break
+               }
+               v.copyOf(ptr)
+               return true
+       }
        return false
 }
 func rewriteValuegeneric_OpNot(v *Value) bool {
index ffdcedef0897727edf1004ce5a4e80cef5913ae8..bce0108dcbbb1cf46cc5ebc23f892d19b208e105 100644 (file)
@@ -314,14 +314,16 @@ func schedule(f *Func) {
        for _, b := range f.Blocks {
                for _, v := range b.Values {
                        for i, a := range v.Args {
-                               if a.Op == OpSPanchored || opcodeTable[a.Op].nilCheck {
-                                       v.SetArg(i, a.Args[0])
+                               for a.Op == OpSPanchored || opcodeTable[a.Op].nilCheck {
+                                       a = a.Args[0]
+                                       v.SetArg(i, a)
                                }
                        }
                }
                for i, c := range b.ControlValues() {
-                       if c.Op == OpSPanchored || opcodeTable[c.Op].nilCheck {
-                               b.ReplaceControl(i, c.Args[0])
+                       for c.Op == OpSPanchored || opcodeTable[c.Op].nilCheck {
+                               c = c.Args[0]
+                               b.ReplaceControl(i, c)
                        }
                }
        }