aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/ssa/deadcode.go
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2016-04-28 16:52:47 -0700
committerKeith Randall <khr@golang.org>2016-05-05 15:58:59 +0000
commit4fa050024fbdfff9eb43c930c5b1f73b1a16fafa (patch)
treece7071052304a7e11d0fb62b8acef4053ebf136d /src/cmd/compile/internal/ssa/deadcode.go
parentbcd4b84bc56889b5a9a8a5d457f35fc0188e8315 (diff)
downloadgo-4fa050024fbdfff9eb43c930c5b1f73b1a16fafa.tar.gz
go-4fa050024fbdfff9eb43c930c5b1f73b1a16fafa.zip
cmd/compile: enable constant-time CFG editing
Provide indexes along with block pointers for Preds and Succs arrays. This allows us to splice edges in and out of those arrays in constant time. Fixes worst-case O(n^2) behavior in deadcode and fuse. benchmark old ns/op new ns/op delta BenchmarkFuse1-8 2065 2057 -0.39% BenchmarkFuse10-8 9408 9073 -3.56% BenchmarkFuse100-8 105238 76277 -27.52% BenchmarkFuse1000-8 3982562 1026750 -74.22% BenchmarkFuse10000-8 301220329 12824005 -95.74% BenchmarkDeadCode1-8 1588 1566 -1.39% BenchmarkDeadCode10-8 4333 4250 -1.92% BenchmarkDeadCode100-8 32031 32574 +1.70% BenchmarkDeadCode1000-8 590407 468275 -20.69% BenchmarkDeadCode10000-8 17822890 5000818 -71.94% BenchmarkDeadCode100000-8 1388706640 78021127 -94.38% BenchmarkDeadCode200000-8 5372518479 168598762 -96.86% Change-Id: Iccabdbb9343fd1c921ba07bbf673330a1c36ee17 Reviewed-on: https://go-review.googlesource.com/22589 Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/ssa/deadcode.go')
-rw-r--r--src/cmd/compile/internal/ssa/deadcode.go64
1 files changed, 27 insertions, 37 deletions
diff --git a/src/cmd/compile/internal/ssa/deadcode.go b/src/cmd/compile/internal/ssa/deadcode.go
index ae990026f5..5ccf39027f 100644
--- a/src/cmd/compile/internal/ssa/deadcode.go
+++ b/src/cmd/compile/internal/ssa/deadcode.go
@@ -25,7 +25,8 @@ func reachableBlocks(f *Func) []bool {
if b.Kind == BlockFirst {
s = s[:1]
}
- for _, c := range s {
+ for _, e := range s {
+ c := e.b
if !reachable[c.ID] {
reachable[c.ID] = true
p = append(p, c) // push
@@ -69,7 +70,7 @@ func liveValues(f *Func, reachable []bool) []bool {
v := q[len(q)-1]
q = q[:len(q)-1]
for i, x := range v.Args {
- if v.Op == OpPhi && !reachable[v.Block.Preds[i].ID] {
+ if v.Op == OpPhi && !reachable[v.Block.Preds[i].b.ID] {
continue
}
if !live[x.ID] {
@@ -100,9 +101,12 @@ func deadcode(f *Func) {
if reachable[b.ID] {
continue
}
- for _, c := range b.Succs {
- if reachable[c.ID] {
- c.removePred(b)
+ for i := 0; i < len(b.Succs); {
+ e := b.Succs[i]
+ if reachable[e.b.ID] {
+ b.removeEdge(i)
+ } else {
+ i++
}
}
}
@@ -115,16 +119,9 @@ func deadcode(f *Func) {
if b.Kind != BlockFirst {
continue
}
- c := b.Succs[1]
- b.Succs[1] = nil
- b.Succs = b.Succs[:1]
+ b.removeEdge(1)
b.Kind = BlockPlain
b.Likely = BranchUnknown
-
- if reachable[c.ID] {
- // Note: c must be reachable through some other edge.
- c.removePred(b)
- }
}
// Splice out any copies introduced during dead block removal.
@@ -217,35 +214,28 @@ func deadcode(f *Func) {
f.Blocks = f.Blocks[:i]
}
-// removePred removes the predecessor p from b's predecessor list.
-func (b *Block) removePred(p *Block) {
- var i int
- found := false
- for j, q := range b.Preds {
- if q == p {
- i = j
- found = true
- break
- }
- }
- // TODO: the above loop could make the deadcode pass take quadratic time
- if !found {
- b.Fatalf("can't find predecessor %v of %v\n", p, b)
- }
+// removeEdge removes the i'th outgoing edge from b (and
+// the corresponding incoming edge from b.Succs[i].b).
+func (b *Block) removeEdge(i int) {
+ e := b.Succs[i]
+ c := e.b
+ j := e.i
+
+ // Adjust b.Succs
+ b.removeSucc(i)
- n := len(b.Preds) - 1
- b.Preds[i] = b.Preds[n]
- b.Preds[n] = nil // aid GC
- b.Preds = b.Preds[:n]
+ // Adjust c.Preds
+ c.removePred(j)
- // rewrite phi ops to match the new predecessor list
- for _, v := range b.Values {
+ // Remove phi args from c's phis.
+ n := len(c.Preds)
+ for _, v := range c.Values {
if v.Op != OpPhi {
continue
}
- v.Args[i].Uses--
- v.Args[i] = v.Args[n]
- v.Args[n] = nil // aid GC
+ v.Args[j].Uses--
+ v.Args[j] = v.Args[n]
+ v.Args[n] = nil
v.Args = v.Args[:n]
phielimValue(v)
// Note: this is trickier than it looks. Replacing