1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // Simple block optimizations to simplify the control flow graph.
9 // TODO(adonovan): opt: instead of creating several "unreachable" blocks
10 // per function in the Builder, reuse a single one (e.g. at Blocks[1])
18 // If true, perform sanity checking and show progress at each
19 // successive iteration of optimizeBlocks. Very verbose.
20 const debugBlockOpt = false
22 // markReachable sets Index=-1 for all blocks reachable from b.
23 func markReachable(b *BasicBlock) {
25 for _, succ := range b.Succs {
32 // deleteUnreachableBlocks marks all reachable blocks of f and
33 // eliminates (nils) all others, including possibly cyclic subgraphs.
35 func deleteUnreachableBlocks(f *Function) {
36 const white, black = 0, -1
37 // We borrow b.Index temporarily as the mark bit.
38 for _, b := range f.Blocks {
41 markReachable(f.Blocks[0])
43 markReachable(f.Recover)
45 for i, b := range f.Blocks {
47 for _, c := range b.Succs {
49 c.removePred(b) // delete white->black edge
53 fmt.Fprintln(os.Stderr, "unreachable", b)
55 f.Blocks[i] = nil // delete b
61 // jumpThreading attempts to apply simple jump-threading to block b,
62 // in which a->b->c become a->c if b is just a Jump.
63 // The result is true if the optimization was applied.
65 func jumpThreading(f *Function, b *BasicBlock) bool {
67 return false // don't apply to entry block
72 if _, ok := b.Instrs[0].(*Jump); !ok {
73 return false // not just a jump
77 return false // don't apply to degenerate jump-to-self.
80 return false // not sound without more effort
82 for j, a := range b.Preds {
85 // If a now has two edges to c, replace its degenerate If by Jump.
86 if len(a.Succs) == 2 && a.Succs[0] == c && a.Succs[1] == c {
89 a.Instrs[len(a.Instrs)-1] = jump
96 c.Preds = append(c.Preds, a)
101 fmt.Fprintln(os.Stderr, "jumpThreading", a, b, c)
104 f.Blocks[b.Index] = nil // delete b
108 // fuseBlocks attempts to apply the block fusion optimization to block
109 // a, in which a->b becomes ab if len(a.Succs)==len(b.Preds)==1.
110 // The result is true if the optimization was applied.
112 func fuseBlocks(f *Function, a *BasicBlock) bool {
113 if len(a.Succs) != 1 {
117 if len(b.Preds) != 1 {
121 // Degenerate &&/|| ops may result in a straight-line CFG
122 // containing φ-nodes. (Ideally we'd replace such them with
123 // their sole operand but that requires Referrers, built later.)
125 return false // not sound without further effort
128 // Eliminate jump at end of A, then copy all of B across.
129 a.Instrs = append(a.Instrs[:len(a.Instrs)-1], b.Instrs...)
130 for _, instr := range b.Instrs {
134 // A inherits B's successors
135 a.Succs = append(a.succs2[:0], b.Succs...)
137 // Fix up Preds links of all successors of B.
138 for _, c := range b.Succs {
143 fmt.Fprintln(os.Stderr, "fuseBlocks", a, b)
146 f.Blocks[b.Index] = nil // delete b
150 // optimizeBlocks() performs some simple block optimizations on a
151 // completed function: dead block elimination, block fusion, jump
154 func optimizeBlocks(f *Function) {
155 deleteUnreachableBlocks(f)
157 // Loop until no further progress.
164 mustSanityCheck(f, nil)
167 for _, b := range f.Blocks {
168 // f.Blocks will temporarily contain nils to indicate
169 // deleted blocks; we remove them at the end.
174 // Fuse blocks. b->c becomes bc.
175 if fuseBlocks(f, b) {
179 // a->b->c becomes a->c if b contains only a Jump.
180 if jumpThreading(f, b) {
182 continue // (b was disconnected)