| 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 |
// This file defines algorithms related to dominance. |
| 8 |
|
| 9 |
// Dominator tree construction ---------------------------------------- |
| 10 |
// |
| 11 |
// We use the algorithm described in Lengauer & Tarjan. 1979. A fast |
| 12 |
// algorithm for finding dominators in a flowgraph. |
| 13 |
// https://doi.acm.org/10.1145/357062.357071 |
| 14 |
// |
| 15 |
// We also apply the optimizations to SLT described in Georgiadis et |
| 16 |
// al, Finding Dominators in Practice, JGAA 2006, |
| 17 |
// https://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf |
| 18 |
// to avoid the need for buckets of size > 1. |
| 19 |
|
| 20 |
import ( |
| 21 |
"bytes" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"math/big" |
| 25 |
"os" |
| 26 |
"slices" |
| 27 |
"sort" |
| 28 |
) |
| 29 |
|
| 30 |
// Idom returns the block that immediately dominates b: |
| 31 |
// its parent in the dominator tree, if any. |
| 32 |
// Neither the entry node (b.Index==0) nor recover node |
| 33 |
// (b==b.Parent().Recover()) have a parent. |
| 34 |
func (b *BasicBlock) Idom() *BasicBlock { return b.dom.idom } |
| 35 |
|
| 36 |
// Dominees returns the list of blocks that b immediately dominates: |
| 37 |
// its children in the dominator tree. |
| 38 |
func (b *BasicBlock) Dominees() []*BasicBlock { return b.dom.children } |
| 39 |
|
| 40 |
// Dominates reports whether b dominates c. |
| 41 |
func (b *BasicBlock) Dominates(c *BasicBlock) bool { |
| 42 |
return b.dom.pre <= c.dom.pre && c.dom.post <= b.dom.post |
| 43 |
} |
| 44 |
|
| 45 |
// DomPreorder returns a new slice containing the blocks of f |
| 46 |
// in a preorder traversal of the dominator tree. |
| 47 |
func (f *Function) DomPreorder() []*BasicBlock { |
| 48 |
slice := slices.Clone(f.Blocks) |
| 49 |
sort.Slice(slice, func(i, j int) bool { |
| 50 |
return slice[i].dom.pre < slice[j].dom.pre |
| 51 |
}) |
| 52 |
return slice |
| 53 |
} |
| 54 |
|
| 55 |
// DomPostorder returns a new slice containing the blocks of f |
| 56 |
// in a postorder traversal of the dominator tree. |
| 57 |
// (This is not the same as a postdominance order.) |
| 58 |
func (f *Function) DomPostorder() []*BasicBlock { |
| 59 |
slice := slices.Clone(f.Blocks) |
| 60 |
sort.Slice(slice, func(i, j int) bool { |
| 61 |
return slice[i].dom.post < slice[j].dom.post |
| 62 |
}) |
| 63 |
return slice |
| 64 |
} |
| 65 |
|
| 66 |
// domInfo contains a BasicBlock's dominance information. |
| 67 |
type domInfo struct { |
| 68 |
idom *BasicBlock // immediate dominator (parent in domtree) |
| 69 |
children []*BasicBlock // nodes immediately dominated by this one |
| 70 |
pre, post int32 // pre- and post-order numbering within domtree |
| 71 |
} |
| 72 |
|
| 73 |
// ltState holds the working state for Lengauer-Tarjan algorithm |
| 74 |
// (during which domInfo.pre is repurposed for CFG DFS preorder number). |
| 75 |
type ltState struct { |
| 76 |
// Each slice is indexed by b.Index. |
| 77 |
sdom []*BasicBlock // b's semidominator |
| 78 |
parent []*BasicBlock // b's parent in DFS traversal of CFG |
| 79 |
ancestor []*BasicBlock // b's ancestor with least sdom |
| 80 |
} |
| 81 |
|
| 82 |
// dfs implements the depth-first search part of the LT algorithm. |
| 83 |
func (lt *ltState) dfs(v *BasicBlock, i int32, preorder []*BasicBlock) int32 { |
| 84 |
preorder[i] = v |
| 85 |
v.dom.pre = i // For now: DFS preorder of spanning tree of CFG |
| 86 |
i++ |
| 87 |
lt.sdom[v.Index] = v |
| 88 |
lt.link(nil, v) |
| 89 |
for _, w := range v.Succs { |
| 90 |
if lt.sdom[w.Index] == nil { |
| 91 |
lt.parent[w.Index] = v |
| 92 |
i = lt.dfs(w, i, preorder) |
| 93 |
} |
| 94 |
} |
| 95 |
return i |
| 96 |
} |
| 97 |
|
| 98 |
// eval implements the EVAL part of the LT algorithm. |
| 99 |
func (lt *ltState) eval(v *BasicBlock) *BasicBlock { |
| 100 |
// TODO(adonovan): opt: do path compression per simple LT. |
| 101 |
u := v |
| 102 |
for ; lt.ancestor[v.Index] != nil; v = lt.ancestor[v.Index] { |
| 103 |
if lt.sdom[v.Index].dom.pre < lt.sdom[u.Index].dom.pre { |
| 104 |
u = v |
| 105 |
} |
| 106 |
} |
| 107 |
return u |
| 108 |
} |
| 109 |
|
| 110 |
// link implements the LINK part of the LT algorithm. |
| 111 |
func (lt *ltState) link(v, w *BasicBlock) { |
| 112 |
lt.ancestor[w.Index] = v |
| 113 |
} |
| 114 |
|
| 115 |
// buildDomTree computes the dominator tree of f using the LT algorithm. |
| 116 |
// Precondition: all blocks are reachable (e.g. optimizeBlocks has been run). |
| 117 |
func buildDomTree(fn *Function) { |
| 118 |
// The step numbers refer to the original LT paper; the |
| 119 |
// reordering is due to Georgiadis. |
| 120 |
|
| 121 |
// Clear any previous domInfo. |
| 122 |
for _, b := range fn.Blocks { |
| 123 |
b.dom = domInfo{} |
| 124 |
} |
| 125 |
|
| 126 |
n := len(fn.Blocks) |
| 127 |
// Allocate space for 5 contiguous [n]*BasicBlock arrays: |
| 128 |
// sdom, parent, ancestor, preorder, buckets. |
| 129 |
space := make([]*BasicBlock, 5*n) |
| 130 |
lt := ltState{ |
| 131 |
sdom: space[0:n], |
| 132 |
parent: space[n : 2*n], |
| 133 |
ancestor: space[2*n : 3*n], |
| 134 |
} |
| 135 |
|
| 136 |
// Step 1. Number vertices by depth-first preorder. |
| 137 |
preorder := space[3*n : 4*n] |
| 138 |
root := fn.Blocks[0] |
| 139 |
prenum := lt.dfs(root, 0, preorder) |
| 140 |
recover := fn.Recover |
| 141 |
if recover != nil { |
| 142 |
lt.dfs(recover, prenum, preorder) |
| 143 |
} |
| 144 |
|
| 145 |
buckets := space[4*n : 5*n] |
| 146 |
copy(buckets, preorder) |
| 147 |
|
| 148 |
// In reverse preorder... |
| 149 |
for i := int32(n) - 1; i > 0; i-- { |
| 150 |
w := preorder[i] |
| 151 |
|
| 152 |
// Step 3. Implicitly define the immediate dominator of each node. |
| 153 |
for v := buckets[i]; v != w; v = buckets[v.dom.pre] { |
| 154 |
u := lt.eval(v) |
| 155 |
if lt.sdom[u.Index].dom.pre < i { |
| 156 |
v.dom.idom = u |
| 157 |
} else { |
| 158 |
v.dom.idom = w |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// Step 2. Compute the semidominators of all nodes. |
| 163 |
lt.sdom[w.Index] = lt.parent[w.Index] |
| 164 |
for _, v := range w.Preds { |
| 165 |
u := lt.eval(v) |
| 166 |
if lt.sdom[u.Index].dom.pre < lt.sdom[w.Index].dom.pre { |
| 167 |
lt.sdom[w.Index] = lt.sdom[u.Index] |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
lt.link(lt.parent[w.Index], w) |
| 172 |
|
| 173 |
if lt.parent[w.Index] == lt.sdom[w.Index] { |
| 174 |
w.dom.idom = lt.parent[w.Index] |
| 175 |
} else { |
| 176 |
buckets[i] = buckets[lt.sdom[w.Index].dom.pre] |
| 177 |
buckets[lt.sdom[w.Index].dom.pre] = w |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
// The final 'Step 3' is now outside the loop. |
| 182 |
for v := buckets[0]; v != root; v = buckets[v.dom.pre] { |
| 183 |
v.dom.idom = root |
| 184 |
} |
| 185 |
|
| 186 |
// Step 4. Explicitly define the immediate dominator of each |
| 187 |
// node, in preorder. |
| 188 |
for _, w := range preorder[1:] { |
| 189 |
if w == root || w == recover { |
| 190 |
w.dom.idom = nil |
| 191 |
} else { |
| 192 |
if w.dom.idom != lt.sdom[w.Index] { |
| 193 |
w.dom.idom = w.dom.idom.dom.idom |
| 194 |
} |
| 195 |
// Calculate Children relation as inverse of Idom. |
| 196 |
w.dom.idom.dom.children = append(w.dom.idom.dom.children, w) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
pre, post := numberDomTree(root, 0, 0) |
| 201 |
if recover != nil { |
| 202 |
numberDomTree(recover, pre, post) |
| 203 |
} |
| 204 |
|
| 205 |
// printDomTreeDot(os.Stderr, f) // debugging |
| 206 |
// printDomTreeText(os.Stderr, root, 0) // debugging |
| 207 |
|
| 208 |
if fn.Prog.mode&SanityCheckFunctions != 0 { |
| 209 |
sanityCheckDomTree(fn) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
// numberDomTree sets the pre- and post-order numbers of a depth-first |
| 214 |
// traversal of the dominator tree rooted at v. These are used to |
| 215 |
// answer dominance queries in constant time. |
| 216 |
func numberDomTree(v *BasicBlock, pre, post int32) (int32, int32) { |
| 217 |
v.dom.pre = pre |
| 218 |
pre++ |
| 219 |
for _, child := range v.dom.children { |
| 220 |
pre, post = numberDomTree(child, pre, post) |
| 221 |
} |
| 222 |
v.dom.post = post |
| 223 |
post++ |
| 224 |
return pre, post |
| 225 |
} |
| 226 |
|
| 227 |
// Testing utilities ---------------------------------------- |
| 228 |
|
| 229 |
// sanityCheckDomTree checks the correctness of the dominator tree |
| 230 |
// computed by the LT algorithm by comparing against the dominance |
| 231 |
// relation computed by a naive Kildall-style forward dataflow |
| 232 |
// analysis (Algorithm 10.16 from the "Dragon" book). |
| 233 |
func sanityCheckDomTree(f *Function) { |
| 234 |
n := len(f.Blocks) |
| 235 |
|
| 236 |
// D[i] is the set of blocks that dominate f.Blocks[i], |
| 237 |
// represented as a bit-set of block indices. |
| 238 |
D := make([]big.Int, n) |
| 239 |
|
| 240 |
one := big.NewInt(1) |
| 241 |
|
| 242 |
// all is the set of all blocks; constant. |
| 243 |
var all big.Int |
| 244 |
all.Set(one).Lsh(&all, uint(n)).Sub(&all, one) |
| 245 |
|
| 246 |
// Initialization. |
| 247 |
for i, b := range f.Blocks { |
| 248 |
if i == 0 || b == f.Recover { |
| 249 |
// A root is dominated only by itself. |
| 250 |
D[i].SetBit(&D[0], 0, 1) |
| 251 |
} else { |
| 252 |
// All other blocks are (initially) dominated |
| 253 |
// by every block. |
| 254 |
D[i].Set(&all) |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
// Iteration until fixed point. |
| 259 |
for changed := true; changed; { |
| 260 |
changed = false |
| 261 |
for i, b := range f.Blocks { |
| 262 |
if i == 0 || b == f.Recover { |
| 263 |
continue |
| 264 |
} |
| 265 |
// Compute intersection across predecessors. |
| 266 |
var x big.Int |
| 267 |
x.Set(&all) |
| 268 |
for _, pred := range b.Preds { |
| 269 |
x.And(&x, &D[pred.Index]) |
| 270 |
} |
| 271 |
x.SetBit(&x, i, 1) // a block always dominates itself. |
| 272 |
if D[i].Cmp(&x) != 0 { |
| 273 |
D[i].Set(&x) |
| 274 |
changed = true |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
// Check the entire relation. O(n^2). |
| 280 |
// The Recover block (if any) must be treated specially so we skip it. |
| 281 |
ok := true |
| 282 |
for i := range n { |
| 283 |
for j := range n { |
| 284 |
b, c := f.Blocks[i], f.Blocks[j] |
| 285 |
if c == f.Recover { |
| 286 |
continue |
| 287 |
} |
| 288 |
actual := b.Dominates(c) |
| 289 |
expected := D[j].Bit(i) == 1 |
| 290 |
if actual != expected { |
| 291 |
fmt.Fprintf(os.Stderr, "dominates(%s, %s)==%t, want %t\n", b, c, actual, expected) |
| 292 |
ok = false |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
preorder := f.DomPreorder() |
| 298 |
for _, b := range f.Blocks { |
| 299 |
if got := preorder[b.dom.pre]; got != b { |
| 300 |
fmt.Fprintf(os.Stderr, "preorder[%d]==%s, want %s\n", b.dom.pre, got, b) |
| 301 |
ok = false |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if !ok { |
| 306 |
panic("sanityCheckDomTree failed for " + f.String()) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
// Printing functions ---------------------------------------- |
| 311 |
|
| 312 |
// printDomTreeText prints the dominator tree as text, using indentation. |
| 313 |
// |
| 314 |
//lint:ignore U1000 used during debugging |
| 315 |
func printDomTreeText(buf *bytes.Buffer, v *BasicBlock, indent int) { |
| 316 |
fmt.Fprintf(buf, "%*s%s\n", 4*indent, "", v) |
| 317 |
for _, child := range v.dom.children { |
| 318 |
printDomTreeText(buf, child, indent+1) |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
// printDomTreeDot prints the dominator tree of f in AT&T GraphViz |
| 323 |
// (.dot) format. |
| 324 |
// |
| 325 |
//lint:ignore U1000 used during debugging |
| 326 |
func printDomTreeDot(buf io.Writer, f *Function) { |
| 327 |
fmt.Fprintln(buf, "//", f) |
| 328 |
fmt.Fprintln(buf, "digraph domtree {") |
| 329 |
for i, b := range f.Blocks { |
| 330 |
v := b.dom |
| 331 |
fmt.Fprintf(buf, "\tn%d [label=\"%s (%d, %d)\",shape=\"rectangle\"];\n", v.pre, b, v.pre, v.post) |
| 332 |
// TODO(adonovan): improve appearance of edges |
| 333 |
// belonging to both dominator tree and CFG. |
| 334 |
|
| 335 |
// Dominator tree edge. |
| 336 |
if i != 0 { |
| 337 |
fmt.Fprintf(buf, "\tn%d -> n%d [style=\"solid\",weight=100];\n", v.idom.dom.pre, v.pre) |
| 338 |
} |
| 339 |
// CFG edges. |
| 340 |
for _, pred := range b.Preds { |
| 341 |
fmt.Fprintf(buf, "\tn%d -> n%d [style=\"dotted\",weight=0];\n", pred.dom.pre, v.pre) |
| 342 |
} |
| 343 |
} |
| 344 |
fmt.Fprintln(buf, "}") |
| 345 |
} |