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>
diff --git a/src/cmd/compile/internal/ssa/shortcircuit.go b/src/cmd/compile/internal/ssa/shortcircuit.go
index f589b7a..ff05a04 100644
--- a/src/cmd/compile/internal/ssa/shortcircuit.go
+++ b/src/cmd/compile/internal/ssa/shortcircuit.go
@@ -28,14 +28,15 @@
continue
}
for i, a := range v.Args {
- p := b.Preds[i]
+ e := b.Preds[i]
+ p := e.b
if p.Kind != BlockIf {
continue
}
if p.Control != a {
continue
}
- if p.Succs[0] == b {
+ if e.i == 0 {
v.SetArg(i, ct)
} else {
v.SetArg(i, cf)
@@ -91,39 +92,37 @@
}
// The predecessor we come in from.
- p := b.Preds[i]
+ e1 := b.Preds[i]
+ p := e1.b
+ pi := e1.i
+
// The successor we always go to when coming in
// from that predecessor.
- t := b.Succs[1-a.AuxInt]
+ e2 := b.Succs[1-a.AuxInt]
+ t := e2.b
+ ti := e2.i
- // Change the edge p->b to p->t.
- for j, x := range p.Succs {
- if x == b {
- p.Succs[j] = t
- break
- }
- }
-
- // Fix up t to have one more predecessor.
- j := predIdx(t, b)
- t.Preds = append(t.Preds, p)
- for _, w := range t.Values {
- if w.Op != OpPhi {
- continue
- }
- w.AddArg(w.Args[j])
- }
-
- // Fix up b to have one less predecessor.
- n := len(b.Preds) - 1
- b.Preds[i] = b.Preds[n]
- b.Preds[n] = nil
- b.Preds = b.Preds[:n]
+ // Remove b's incoming edge from p.
+ b.removePred(i)
+ n := len(b.Preds)
v.Args[i].Uses--
v.Args[i] = v.Args[n]
v.Args[n] = nil
v.Args = v.Args[:n]
- if n == 1 {
+
+ // Redirect p's outgoing edge to t.
+ p.Succs[pi] = Edge{t, len(t.Preds)}
+
+ // Fix up t to have one more predecessor.
+ t.Preds = append(t.Preds, Edge{p, pi})
+ for _, w := range t.Values {
+ if w.Op != OpPhi {
+ continue
+ }
+ w.AddArg(w.Args[ti])
+ }
+
+ if len(b.Preds) == 1 {
v.Op = OpCopy
// No longer a phi, stop optimizing here.
break
@@ -132,14 +131,3 @@
}
}
}
-
-// predIdx returns the index where p appears in the predecessor list of b.
-// p must be in the predecessor list of b.
-func predIdx(b, p *Block) int {
- for i, x := range b.Preds {
- if x == p {
- return i
- }
- }
- panic("predecessor not found")
-}