]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: eliminate dead code in if statements after typechecking
authorJosh Bleecher Snyder <josharian@gmail.com>
Mon, 27 Mar 2017 18:38:20 +0000 (11:38 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Tue, 18 Apr 2017 17:13:30 +0000 (17:13 +0000)
This is a more thorough and cleaner fix
than doing dead code elimination separately
during inlining, escape analysis, and export.

Unfortunately, it does add another full walk of the AST.
The performance impact is very small, but not non-zero.

If a label or goto is present in the dead code, it is not eliminated.
This restriction can be removed once label/goto checking occurs
much earlier in the compiler. In practice, it probably doesn't
matter much.

Updates #19699
Fixes #19705

name       old alloc/op      new alloc/op      delta
Template        39.2MB ± 0%       39.3MB ± 0%  +0.28%  (p=0.008 n=5+5)
Unicode         29.8MB ± 0%       29.8MB ± 0%    ~     (p=1.000 n=5+5)
GoTypes          113MB ± 0%        113MB ± 0%  -0.55%  (p=0.008 n=5+5)
SSA             1.25GB ± 0%       1.25GB ± 0%  +0.02%  (p=0.008 n=5+5)
Flate           25.3MB ± 0%       25.3MB ± 0%  -0.24%  (p=0.032 n=5+5)
GoParser        31.7MB ± 0%       31.8MB ± 0%  +0.31%  (p=0.008 n=5+5)
Reflect         78.2MB ± 0%       78.3MB ± 0%    ~     (p=0.421 n=5+5)
Tar             26.6MB ± 0%       26.7MB ± 0%  +0.21%  (p=0.008 n=5+5)
XML             42.2MB ± 0%       42.2MB ± 0%    ~     (p=0.056 n=5+5)

name       old allocs/op     new allocs/op     delta
Template          385k ± 0%         387k ± 0%  +0.51%  (p=0.016 n=5+5)
Unicode           321k ± 0%         321k ± 0%    ~     (p=1.000 n=5+5)
GoTypes          1.14M ± 0%        1.14M ± 0%    ~     (p=1.000 n=5+5)
SSA              9.71M ± 0%        9.72M ± 0%  +0.10%  (p=0.008 n=5+5)
Flate             234k ± 1%         234k ± 1%    ~     (p=0.690 n=5+5)
GoParser          315k ± 0%         317k ± 0%  +0.71%  (p=0.008 n=5+5)
Reflect           980k ± 0%         983k ± 0%  +0.30%  (p=0.032 n=5+5)
Tar               251k ± 0%         252k ± 0%  +0.55%  (p=0.016 n=5+5)
XML               392k ± 0%         393k ± 0%  +0.30%  (p=0.008 n=5+5)

Change-Id: Ia10ff4bbf5c6eae782582cc9cbc9785494d4fb83
Reviewed-on: https://go-review.googlesource.com/38773
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/bexport.go
src/cmd/compile/internal/gc/esc.go
src/cmd/compile/internal/gc/inl.go
src/cmd/compile/internal/gc/main.go
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue19699.dir/a.go [new file with mode: 0644]
test/fixedbugs/issue19699.dir/b.go [new file with mode: 0644]
test/fixedbugs/issue19699.go [new file with mode: 0644]
test/fixedbugs/issue19699b.go [new file with mode: 0644]
test/fixedbugs/issue19705.go [new file with mode: 0644]

index 5d0feea3131ba26f1307919fdec9e3a1bc72fee5..3637804a12e657634cc938c9943a7ecdeb5f9783 100644 (file)
@@ -1469,22 +1469,8 @@ func (p *exporter) stmt(n *Node) {
                p.pos(n)
                p.stmtList(n.Ninit)
                p.expr(n.Left)
-               nbody := n.Nbody
-               rlist := n.Rlist
-               if Isconst(n.Left, CTBOOL) {
-                       // if false { ... } or if true { ... }
-                       // Only export the taken branch.
-                       // This is more efficient,
-                       // and avoids trying to export
-                       // un-exportable nodes.
-                       if n.Left.Bool() {
-                               rlist = Nodes{}
-                       } else {
-                               nbody = Nodes{}
-                       }
-               }
-               p.stmtList(nbody)
-               p.stmtList(rlist)
+               p.stmtList(n.Nbody)
+               p.stmtList(n.Rlist)
 
        case OFOR:
                p.op(OFOR)
index 75adb8eecb16365fd5d86047257542eb53526c19..4565c4aa0246fc1a34272d9c32b296fda46e452f 100644 (file)
@@ -690,20 +690,11 @@ func (e *EscState) esc(n *Node, parent *Node) {
                e.escassignSinkWhy(n, n, "too large for stack") // TODO category: tooLarge
        }
 
-       if n.Op == OIF && Isconst(n.Left, CTBOOL) {
-               // Don't examine dead code.
-               if n.Left.Bool() {
-                       e.esclist(n.Nbody, n)
-               } else {
-                       e.esclist(n.Rlist, n)
-               }
-       } else {
-               e.esc(n.Left, n)
-               e.esc(n.Right, n)
-               e.esclist(n.Nbody, n)
-               e.esclist(n.List, n)
-               e.esclist(n.Rlist, n)
-       }
+       e.esc(n.Left, n)
+       e.esc(n.Right, n)
+       e.esclist(n.Nbody, n)
+       e.esclist(n.List, n)
+       e.esclist(n.Rlist, n)
 
        if n.Op == OFOR || n.Op == OFORUNTIL || n.Op == ORANGE {
                e.loopdepth--
index 3ecca14f10cfcd6312454ef344a6a4202c9bcdaf..b7a387141b6a0e469371d9d886b0f4a467976d74 100644 (file)
@@ -279,16 +279,6 @@ func ishairy(n *Node, budget *int32, reason *string) bool {
                return true
        }
 
-       if n.Op == OIF && Isconst(n.Left, CTBOOL) {
-               var taken Nodes // statements for the branch that is always taken
-               if n.Left.Bool() {
-                       taken = n.Nbody // then case
-               } else {
-                       taken = n.Rlist // else case
-               }
-               return ishairylist(n.Ninit, budget, reason) || ishairylist(taken, budget, reason)
-       }
-
        return ishairy(n.Left, budget, reason) || ishairy(n.Right, budget, reason) ||
                ishairylist(n.List, budget, reason) || ishairylist(n.Rlist, budget, reason) ||
                ishairylist(n.Ninit, budget, reason) || ishairylist(n.Nbody, budget, reason)
index 79e95958ef2311fad1e044c24786195d6a2a4ec9..560e7c68e557e391da9350fcd3fd9d5d746076b6 100644 (file)
@@ -443,6 +443,9 @@ func Main(archInit func(*Arch)) {
                        if nerrors != 0 {
                                Curfn.Nbody.Set(nil) // type errors; do not compile
                        }
+                       // Now that we've checked whether n terminates,
+                       // we can eliminate some obviously dead code.
+                       deadcode(Curfn)
                        fcount++
                }
        }
index e78634a86728e13c3b9a27cf0137e6d74a94b7f1..b8c81b528a1d8d539084357e6924d4f1fca78596 100644 (file)
@@ -3842,13 +3842,7 @@ func markbreak(n *Node, implicit *Node) {
                                lab.SetHasBreak(true)
                        }
                }
-
-       case OFOR,
-               OFORUNTIL,
-               OSWITCH,
-               OTYPESW,
-               OSELECT,
-               ORANGE:
+       case OFOR, OFORUNTIL, OSWITCH, OTYPESW, OSELECT, ORANGE:
                implicit = n
                fallthrough
        default:
@@ -3883,8 +3877,7 @@ func markbreaklist(l Nodes, implicit *Node) {
        }
 }
 
-// Isterminating whether the Nodes list ends with a terminating
-// statement.
+// isterminating reports whether the Nodes list ends with a terminating statement.
 func (l Nodes) isterminating() bool {
        s := l.Slice()
        c := len(s)
@@ -3894,7 +3887,7 @@ func (l Nodes) isterminating() bool {
        return s[c-1].isterminating()
 }
 
-// Isterminating returns whether the node n, the last one in a
+// Isterminating reports whether the node n, the last one in a
 // statement list, is a terminating statement.
 func (n *Node) isterminating() bool {
        switch n.Op {
@@ -3906,11 +3899,7 @@ func (n *Node) isterminating() bool {
        case OBLOCK:
                return n.List.isterminating()
 
-       case OGOTO,
-               ORETURN,
-               ORETJMP,
-               OPANIC,
-               OXFALL:
+       case OGOTO, ORETURN, ORETJMP, OPANIC, OXFALL:
                return true
 
        case OFOR, OFORUNTIL:
@@ -3948,6 +3937,7 @@ func (n *Node) isterminating() bool {
        return false
 }
 
+// checkreturn makes sure that fn terminates appropriately.
 func checkreturn(fn *Node) {
        if fn.Type.Results().NumFields() != 0 && fn.Nbody.Len() != 0 {
                markbreaklist(fn.Nbody, nil)
@@ -3956,3 +3946,48 @@ func checkreturn(fn *Node) {
                }
        }
 }
+
+func deadcode(fn *Node) {
+       deadcodeslice(fn.Nbody)
+}
+
+func deadcodeslice(nn Nodes) {
+       for _, n := range nn.Slice() {
+               if n == nil {
+                       continue
+               }
+               if n.Op == OIF && Isconst(n.Left, CTBOOL) {
+                       var dead *Nodes
+                       if n.Left.Bool() {
+                               dead = &n.Rlist
+                       } else {
+                               dead = &n.Nbody
+                       }
+                       // TODO(mdempsky/josharian): eliminate need for haslabelgoto
+                       // by checking labels and gotos earlier. See issue 19699.
+                       if !(*dead).haslabelgoto() {
+                               *dead = Nodes{}
+                       }
+               }
+               deadcodeslice(n.Ninit)
+               deadcodeslice(n.Nbody)
+               deadcodeslice(n.List)
+               deadcodeslice(n.Rlist)
+       }
+}
+
+// haslabelgoto reports whether the Nodes list contains any label or goto statements.
+func (l Nodes) haslabelgoto() bool {
+       for _, n := range l.Slice() {
+               if n == nil {
+                       continue
+               }
+               if n.Op == OLABEL || n.Op == OGOTO {
+                       return true
+               }
+               if n.Ninit.haslabelgoto() || n.Nbody.haslabelgoto() || n.List.haslabelgoto() || n.Rlist.haslabelgoto() {
+                       return true
+               }
+       }
+       return false
+}
diff --git a/test/fixedbugs/issue19699.dir/a.go b/test/fixedbugs/issue19699.dir/a.go
new file mode 100644 (file)
index 0000000..83be926
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package a
+
+func F() {
+l1:
+       if false {
+               goto l1
+       }
+}
diff --git a/test/fixedbugs/issue19699.dir/b.go b/test/fixedbugs/issue19699.dir/b.go
new file mode 100644 (file)
index 0000000..e727133
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import "./a"
+
+func main() {
+       a.F()
+}
diff --git a/test/fixedbugs/issue19699.go b/test/fixedbugs/issue19699.go
new file mode 100644 (file)
index 0000000..8000a52
--- /dev/null
@@ -0,0 +1,7 @@
+// compiledir
+
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package ignored
diff --git a/test/fixedbugs/issue19699b.go b/test/fixedbugs/issue19699b.go
new file mode 100644 (file)
index 0000000..4afc0ca
--- /dev/null
@@ -0,0 +1,14 @@
+// errorcheck
+
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func f() bool {
+       if false {
+       } else {
+               return true
+       }
+} // ERROR "missing return at end of function"
diff --git a/test/fixedbugs/issue19705.go b/test/fixedbugs/issue19705.go
new file mode 100644 (file)
index 0000000..6157945
--- /dev/null
@@ -0,0 +1,17 @@
+// compile
+
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func f1() {
+       f2()
+}
+
+func f2() {
+       if false {
+               _ = func() {}
+       }
+}