Commit dffe5ac9 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

cmd/compile: eliminate dead code in if statements after typechecking

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: 's avatarMatthew Dempsky <mdempsky@google.com>
parent 2ef340f2
......@@ -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)
......
......@@ -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--
......
......@@ -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)
......
......@@ -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++
}
}
......
......@@ -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
}
// 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
}
}
// 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()
}
// 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
// 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"
// 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() {}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment