/
dominator.go
280 lines (233 loc) · 7.13 KB
/
dominator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** This is an implementation of the Lengauer-Tarjan algorithm described in
* https://doi.org/10.1145%2F357062.357071
*/
package ssa
import (
`sort`
`github.com/cloudwego/frugal/internal/rt`
`github.com/oleiade/lane`
)
type _LtNode struct {
semi int
node *BasicBlock
dom *_LtNode
label *_LtNode
parent *_LtNode
ancestor *_LtNode
pred []*_LtNode
bucket map[*_LtNode]struct{}
}
type _LengauerTarjan struct {
nodes []*_LtNode
vertex map[int]int
}
func newLengauerTarjan() *_LengauerTarjan {
return &_LengauerTarjan {
vertex: make(map[int]int),
}
}
func (self *_LengauerTarjan) dfs(bb *BasicBlock) {
i := len(self.nodes)
self.vertex[bb.Id] = i
/* create a new node */
p := &_LtNode {
semi : i,
node : bb,
bucket : make(map[*_LtNode]struct{}),
}
/* add to node list */
p.label = p
self.nodes = append(self.nodes, p)
/* get it's successors iterator */
tr := bb.Term
it := tr.Successors()
/* traverse the successors */
for it.Next() {
w := it.Block()
idx, ok := self.vertex[w.Id]
/* not visited yet */
if !ok {
self.dfs(w)
idx = self.vertex[w.Id]
self.nodes[idx].parent = p
}
/* add predecessors */
q := self.nodes[idx]
q.pred = append(q.pred, p)
}
}
func (self *_LengauerTarjan) eval(p *_LtNode) *_LtNode {
if p.ancestor == nil {
return p
} else {
self.compress(p)
return p.label
}
}
func (self *_LengauerTarjan) link(p *_LtNode, q *_LtNode) {
q.ancestor = p
}
func (self *_LengauerTarjan) relable(p *_LtNode) {
if p.label.semi > p.ancestor.label.semi {
p.label = p.ancestor.label
}
}
func (self *_LengauerTarjan) compress(p *_LtNode) {
if p.ancestor.ancestor != nil {
self.compress(p.ancestor)
self.relable(p)
p.ancestor = p.ancestor.ancestor
}
}
type _NodeDepth struct {
d int
bb int
}
func updateDominatorTree(cfg *CFG) {
rt.MapClear(cfg.DominatedBy)
rt.MapClear(cfg.DominatorOf)
/* Step 1: Carry out a depth-first search of the problem graph. Number the vertices
* from 1 to n as they are reached during the search. Initialize the variables used
* in succeeding steps. */
lt := newLengauerTarjan()
lt.dfs(cfg.Root)
/* perform Step 2 and Step 3 for every node */
for i := len(lt.nodes) - 1; i > 0; i-- {
p := lt.nodes[i]
q := (*_LtNode)(nil)
/* Step 2: Compute the semidominators of all vertices by applying Theorem 4.
* Carry out the computation vertex by vertex in decreasing order by number. */
for _, v := range p.pred {
q = lt.eval(v)
p.semi = minint(p.semi, q.semi)
}
/* link the ancestor */
lt.link(p.parent, p)
lt.nodes[p.semi].bucket[p] = struct{}{}
/* Step 3: Implicitly define the immediate dominator of each vertex by applying Corollary 1 */
for v := range p.parent.bucket {
if q = lt.eval(v); q.semi < v.semi {
v.dom = q
} else {
v.dom = p.parent
}
}
/* clear the bucket */
for v := range p.parent.bucket {
delete(p.parent.bucket, v)
}
}
/* Step 4: Explicitly define the immediate dominator of each vertex, carrying out the
* computation vertex by vertex in increasing order by number. */
for _, p := range lt.nodes[1:] {
if p.dom.node.Id != lt.nodes[p.semi].node.Id {
p.dom = p.dom.dom
}
}
/* map the dominator relationship */
for _, p := range lt.nodes[1:] {
cfg.DominatedBy[p.node.Id] = p.dom.node
cfg.DominatorOf[p.dom.node.Id] = append(cfg.DominatorOf[p.dom.node.Id], p.node)
}
/* sort the dominators */
for _, p := range cfg.DominatorOf {
sort.Slice(p, func(i int, j int) bool {
return p[i].Id < p[j].Id
})
}
}
func updateDominatorDepth(cfg *CFG) {
r := cfg.Root.Id
q := lane.NewQueue()
/* add the root node */
q.Enqueue(_NodeDepth { bb: r })
rt.MapClear(cfg.Depth)
/* calculate depth for every block */
for !q.Empty() {
d := q.Dequeue().(_NodeDepth)
cfg.Depth[d.bb] = d.d
/* add all the dominated nodes */
for _, p := range cfg.DominatorOf[d.bb] {
q.Enqueue(_NodeDepth {
d : d.d + 1,
bb : p.Id,
})
}
}
}
func updateDominatorFrontier(cfg *CFG) {
r := cfg.Root
q := lane.NewQueue()
/* add the root node */
q.Enqueue(r)
rt.MapClear(cfg.DominanceFrontier)
/* calculate dominance frontier for every block */
for !q.Empty() {
k := q.Dequeue().(*BasicBlock)
addImmediateDominated(cfg.DominatorOf, k, q)
computeDominanceFrontier(cfg.DominatorOf, k, cfg.DominanceFrontier)
}
}
func isStrictlyDominates(dom map[int][]*BasicBlock, p *BasicBlock, q *BasicBlock) bool {
for _, v := range dom[p.Id] { if v != p && (v == q || isStrictlyDominates(dom, v, q)) { return true } }
return false
}
func addImmediateDominated(dom map[int][]*BasicBlock, node *BasicBlock, q *lane.Queue) {
for _, p := range dom[node.Id] {
q.Enqueue(p)
}
}
func computeDominanceFrontier(dom map[int][]*BasicBlock, node *BasicBlock, dfm map[int][]*BasicBlock) []*BasicBlock {
var it IrSuccessors
var df map[*BasicBlock]struct{}
/* check for cached values */
if v, ok := dfm[node.Id]; ok {
return v
}
/* get the successor iterator */
it = node.Term.Successors()
df = make(map[*BasicBlock]struct{})
/* local(X) = set of successors of X that X does not immediately dominate */
for it.Next() {
if y := it.Block(); !isStrictlyDominates(dom, node, y) {
df[y] = struct{}{}
}
}
/* df(X) = union of local(X) and ( union of up(K) for all K that are children of X ) */
for _, k := range dom[node.Id] {
for _, y := range computeDominanceFrontier(dom, k, dfm) {
if !isStrictlyDominates(dom, node, y) {
df[y] = struct{}{}
}
}
}
/* convert to slice */
nb := len(df)
ret := make([]*BasicBlock, 0, nb)
/* extract all the keys */
for bb := range df {
ret = append(ret, bb)
}
/* sort by ID */
sort.Slice(ret, func(i int, j int) bool {
return ret[i].Id < ret[j].Id
})
/* add to cache */
dfm[node.Id] = ret
return ret
}