]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: treat slice pointers as non-nil
authorKeith Randall <khr@google.com>
Mon, 25 Feb 2019 22:51:58 +0000 (14:51 -0800)
committerKeith Randall <khr@golang.org>
Tue, 26 Feb 2019 20:44:52 +0000 (20:44 +0000)
var a []int = ...
p := &a[0]
_ = *p

We don't need to nil check on the 3rd line. If the bounds check on the 2nd
line passes, we know p is non-nil.

We rely on the fact that any cap>0 slice has a non-nil pointer as its
pointer to the backing array. This is true for all safely-constructed slices,
and I don't see any reason why someone would violate this rule using unsafe.

R=go1.13

Fixes #30366

Change-Id: I3ed764fcb72cfe1fbf963d8c1a82e24e3b6dead7
Reviewed-on: https://go-review.googlesource.com/c/163740
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
src/cmd/compile/internal/ssa/nilcheck.go
test/codegen/slices.go

index 5f58e2d7ec1efa3d58c227daccf05a7bded5499a..5369a51023cfe06a6ce476d73f0c529c9619e675 100644 (file)
@@ -49,7 +49,9 @@ func nilcheckelim(f *Func) {
                        // value, or a value constructed from an offset of a
                        // non-nil ptr (OpAddPtr) implies it is non-nil
                        // We also assume unsafe pointer arithmetic generates non-nil pointers. See #27180.
-                       if v.Op == OpAddr || v.Op == OpLocalAddr || v.Op == OpAddPtr || v.Op == OpOffPtr || v.Op == OpAdd32 || v.Op == OpAdd64 || v.Op == OpSub32 || v.Op == OpSub64 {
+                       // We assume that SlicePtr is non-nil because we do a bounds check
+                       // before the slice access (and all cap>0 slices have a non-nil ptr). See #30366.
+                       if v.Op == OpAddr || v.Op == OpLocalAddr || v.Op == OpAddPtr || v.Op == OpOffPtr || v.Op == OpAdd32 || v.Op == OpAdd64 || v.Op == OpSub32 || v.Op == OpSub64 || v.Op == OpSlicePtr {
                                nonNilValues[v.ID] = true
                        }
                }
index 15dbcee7372d89765dc27a9c70f079eeb49e273c..6477c6f6c78a8c9017b164530aa55624d526c33a 100644 (file)
@@ -61,3 +61,13 @@ func SliceExtensionInt64(s []int, l64 int64) []int {
        // 386:-`.*runtime\.memclr`
        return append(s, make([]int, l64)...)
 }
+
+// ---------------------- //
+//   Nil check of &s[0]   //
+// ---------------------- //
+// See issue 30366
+func SliceNilCheck(s []int) {
+       p := &s[0]
+       // amd64:-`TESTB`
+       _ = *p
+}