]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: enable optimizer logging for bounds checking
authorDavid Chase <drchase@google.com>
Tue, 29 Oct 2019 19:59:08 +0000 (15:59 -0400)
committerDavid Chase <drchase@google.com>
Sun, 10 Nov 2019 17:12:35 +0000 (17:12 +0000)
Change-Id: Ic1fc271589b7212e7f604ece93cfe34feff909b2
Reviewed-on: https://go-review.googlesource.com/c/go/+/204160
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
src/cmd/compile/internal/logopt/logopt_test.go
src/cmd/compile/internal/ssa/checkbce.go

index ef71a78a1a67c6f0a119282cedb9eee90e985f09..f2270fc978fcfb7537166a1c9f17495a187ef1ef 100644 (file)
@@ -20,12 +20,15 @@ type pair struct {a,b int}
 func bar(y *pair) *int {
        return &y.b
 }
-
+var a []int
 func foo(w, z *pair) *int {
        if *bar(w) > 0 {
                return bar(z)
        }
-       return nil
+       if a[1] > 0 {
+               a = a[:2]
+       }
+       return &a[0]
 }
 `
 
@@ -102,6 +105,7 @@ func TestLogOpt(t *testing.T) {
                t.Logf("%s", slogged)
                // below shows proper inlining and nilcheck
                want(t, slogged, `{"range":{"start":{"line":9,"character":13},"end":{"line":9,"character":13}},"severity":3,"code":"nilcheck","source":"go compiler","message":"","relatedInformation":[{"location":{"uri":"file://tmpdir/file.go","range":{"start":{"line":4,"character":11},"end":{"line":4,"character":11}}},"message":"inlineLoc"}]}`)
+               want(t, slogged, `{"range":{"start":{"line":11,"character":6},"end":{"line":11,"character":6}},"severity":3,"code":"isInBounds","source":"go compiler","message":""}`)
        })
 }
 
index ab842b4296f1ce1595437bfd4be5990cacd87006..6a9ce2be0aacab01ae33e81eef7bdf734a1d8446 100644 (file)
@@ -4,19 +4,31 @@
 
 package ssa
 
+import "cmd/compile/internal/logopt"
+
 // checkbce prints all bounds checks that are present in the function.
 // Useful to find regressions. checkbce is only activated when with
 // corresponding debug options, so it's off by default.
 // See test/checkbce.go
 func checkbce(f *Func) {
-       if f.pass.debug <= 0 {
+       if f.pass.debug <= 0 && !logopt.Enabled() {
                return
        }
 
        for _, b := range f.Blocks {
                for _, v := range b.Values {
                        if v.Op == OpIsInBounds || v.Op == OpIsSliceInBounds {
-                               f.Warnl(v.Pos, "Found %v", v.Op)
+                               if f.pass.debug > 0 {
+                                       f.Warnl(v.Pos, "Found %v", v.Op)
+                               }
+                               if logopt.Enabled() {
+                                       if v.Op == OpIsInBounds {
+                                               logopt.LogOpt(v.Pos, "isInBounds", "checkbce", f.Name)
+                                       }
+                                       if v.Op == OpIsSliceInBounds {
+                                               logopt.LogOpt(v.Pos, "isSliceInBounds", "checkbce", f.Name)
+                                       }
+                               }
                        }
                }
        }