Debian Codegraph @

go/ir/blockopt.go

raw
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.
4
5 package ir
6
7 // Simple block optimizations to simplify the control flow graph.
8
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])
11 // to reduce garbage.
12
13 import (
14 "fmt"
15 "os"
16 )
17
18 // If true, perform sanity checking and show progress at each
19 // successive iteration of optimizeBlocks. Very verbose.
20 const debugBlockOpt = false
21
22 // markReachable sets Index=-1 for all blocks reachable from b.
23 func markReachable(b *BasicBlock) {
24 b.gaps = -1
25 for _, succ := range b.Succs {
26 if succ.gaps == 0 {
27 markReachable(succ)
28 }
29 }
30 }
31
32 // deleteUnreachableBlocks marks all reachable blocks of f and
33 // eliminates (nils) all others, including possibly cyclic subgraphs.
34 func deleteUnreachableBlocks(f *Function) {
35 const white, black = 0, -1
36 // We borrow b.gaps temporarily as the mark bit.
37 for _, b := range f.Blocks {
38 b.gaps = white
39 }
40 markReachable(f.Blocks[0])
41 if f.Recover != nil {
42 markReachable(f.Recover)
43 }
44 for i, b := range f.Blocks {
45 if b.gaps == white {
46 for _, c := range b.Succs {
47 if c.gaps == black {
48 c.removePred(b) // delete white->black edge
49 }
50 }
51 if debugBlockOpt {
52 fmt.Fprintln(os.Stderr, "unreachable", b)
53 }
54 f.Blocks[i] = nil // delete b
55 }
56 }
57 f.removeNilBlocks()
58 }
59
60 // jumpThreading attempts to apply simple jump-threading to block b,
61 // in which a->b->c become a->c if b is just a Jump.
62 // The result is true if the optimization was applied.
63 func jumpThreading(f *Function, b *BasicBlock) bool {
64 if b.Index == 0 {
65 return false // don't apply to entry block
66 }
67 if len(b.Instrs) == 0 {
68 return false
69 }
70 for _, pred := range b.Preds {
71 switch pred.Control().(type) {
72 case *ConstantSwitch:
73 // don't optimize away the head blocks of switch statements
74 return false
75 }
76 }
77 if _, ok := b.Instrs[0].(*Jump); !ok {
78 return false // not just a jump
79 }
80 c := b.Succs[0]
81 if c == b {
82 return false // don't apply to degenerate jump-to-self.
83 }
84 if c.hasPhi() {
85 return false // not sound without more effort
86 }
87 for j, a := range b.Preds {
88 a.replaceSucc(b, c)
89
90 // If a now has two edges to c, replace its degenerate If by Jump.
91 if len(a.Succs) == 2 && a.Succs[0] == c && a.Succs[1] == c {
92 jump := new(Jump)
93 jump.setBlock(a)
94 a.Instrs[len(a.Instrs)-1] = jump
95 a.Succs = a.Succs[:1]
96 c.removePred(b)
97 } else {
98 if j == 0 {
99 c.replacePred(b, a)
100 } else {
101 c.Preds = append(c.Preds, a)
102 }
103 }
104
105 if debugBlockOpt {
106 fmt.Fprintln(os.Stderr, "jumpThreading", a, b, c)
107 }
108 }
109 f.Blocks[b.Index] = nil // delete b
110 return true
111 }
112
113 // fuseBlocks attempts to apply the block fusion optimization to block
114 // a, in which a->b becomes ab if len(a.Succs)==len(b.Preds)==1.
115 // The result is true if the optimization was applied.
116 func fuseBlocks(f *Function, a *BasicBlock) bool {
117 if len(a.Succs) != 1 {
118 return false
119 }
120 b := a.Succs[0]
121 if len(b.Preds) != 1 {
122 return false
123 }
124 if _, ok := a.Instrs[len(a.Instrs)-1].(*Panic); ok {
125 // panics aren't simple jumps, they have side effects.
126 return false
127 }
128
129 // Degenerate &&/|| ops may result in a straight-line CFG
130 // containing φ-nodes. (Ideally we'd replace such them with
131 // their sole operand but that requires Referrers, built later.)
132 if b.hasPhi() {
133 return false // not sound without further effort
134 }
135
136 // Eliminate jump at end of A, then copy all of B across.
137 a.Instrs = append(a.Instrs[:len(a.Instrs)-1], b.Instrs...)
138 for _, instr := range b.Instrs {
139 instr.setBlock(a)
140 }
141
142 // A inherits B's successors
143 a.Succs = append(a.succs2[:0], b.Succs...)
144
145 // Fix up Preds links of all successors of B.
146 for _, c := range b.Succs {
147 c.replacePred(b, a)
148 }
149
150 if debugBlockOpt {
151 fmt.Fprintln(os.Stderr, "fuseBlocks", a, b)
152 }
153
154 f.Blocks[b.Index] = nil // delete b
155 return true
156 }
157
158 // optimizeBlocks() performs some simple block optimizations on a
159 // completed function: dead block elimination, block fusion, jump
160 // threading.
161 func optimizeBlocks(f *Function) {
162 deleteUnreachableBlocks(f)
163
164 // Loop until no further progress.
165 changed := true
166 for changed {
167 changed = false
168
169 if debugBlockOpt {
170 f.WriteTo(os.Stderr)
171 mustSanityCheck(f, nil)
172 }
173
174 for _, b := range f.Blocks {
175 // f.Blocks will temporarily contain nils to indicate
176 // deleted blocks; we remove them at the end.
177 if b == nil {
178 continue
179 }
180
181 // Fuse blocks. b->c becomes bc.
182 if fuseBlocks(f, b) {
183 changed = true
184 }
185
186 // a->b->c becomes a->c if b contains only a Jump.
187 if jumpThreading(f, b) {
188 changed = true
189 continue // (b was disconnected)
190 }
191 }
192 }
193 f.removeNilBlocks()
194 }