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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use crate::{
analysis::PredecessorTable,
ir::{prelude::*, ValueData},
table::TableKey,
};
use hibitset::BitSet;
use std::{
collections::{HashMap, HashSet},
sync::atomic::{AtomicU64, Ordering},
time::Instant,
};
#[derive(Debug, Clone)]
pub struct DominatorTree {
dominates: HashMap<Block, HashSet<Block>>,
dominated: HashMap<Block, HashSet<Block>>,
doms: Vec<Block>,
post_order: Vec<Block>,
inv_post_order: Vec<u32>,
}
impl DominatorTree {
#[deprecated(since = "0.13.0", note = "use unit.domtree() instead")]
pub fn new(unit: &Unit, pred: &PredecessorTable) -> Self {
let t0 = Instant::now();
let post_order = Self::compute_blocks_post_order(unit, pred);
let length = post_order.len();
let undef = std::u32::MAX;
let mut doms = vec![undef; length];
let mut inv_post_order = vec![undef; unit.block_id_bound()];
for (i, &bb) in post_order.iter().enumerate() {
inv_post_order[bb.index()] = i as u32;
}
for root in Some(unit.entry())
.into_iter()
.chain(unit.blocks().filter(|&id| pred.pred_set(id).is_empty()))
{
let poidx = inv_post_order[root.index()];
doms[poidx as usize] = poidx;
}
let mut changed = true;
while changed {
changed = false;
for idx in (0..length).rev() {
if doms[idx] == idx as u32 {
continue;
}
let bb = post_order[idx];
let mut preds = pred
.pred_set(bb)
.iter()
.map(|id| inv_post_order[id.index()])
.filter(|&p| doms[p as usize] != undef);
let new_idom = preds.next().unwrap();
let new_idom = preds.fold(new_idom, |mut i1, mut i2| {
let i1_init = i1;
while i1 != i2 {
if i1 < i2 {
if i1 == doms[i1 as usize] {
return i1;
}
i1 = doms[i1 as usize];
} else if i2 < i1 {
if i2 == doms[i2 as usize] {
return i1_init;
}
i2 = doms[i2 as usize];
}
}
i1
});
debug_assert!(new_idom < length as u32);
if doms[idx] != new_idom {
doms[idx] = new_idom;
changed = true;
}
}
}
let mut doms_final = vec![Block::invalid(); unit.block_id_bound()];
for bb in &post_order {
doms_final[bb.index()] = post_order[doms[inv_post_order[bb.index()] as usize] as usize];
}
let mut dominated = HashMap::new();
for block in unit.blocks() {
let mut s = HashSet::new();
let mut bb = block;
loop {
s.insert(bb);
let next = doms_final[bb.index()];
if next == bb {
break;
}
bb = next;
}
dominated.insert(block, s);
}
let mut dominates: HashMap<Block, HashSet<Block>> =
unit.blocks().map(|bb| (bb, HashSet::new())).collect();
for (&bb, dom) in &dominated {
for d in dom {
dominates.get_mut(d).unwrap().insert(bb);
}
}
let t1 = Instant::now();
DOMINATOR_TREE_TIME.fetch_add((t1 - t0).as_nanos() as u64, Ordering::Relaxed);
Self {
dominates,
dominated,
doms: doms_final,
post_order,
inv_post_order,
}
}
fn compute_blocks_post_order(unit: &Unit, pred: &PredecessorTable) -> Vec<Block> {
let mut order = Vec::with_capacity(pred.all_pred_sets().len());
let mut stack = Vec::with_capacity(8);
let mut discovered = BitSet::with_capacity(pred.all_pred_sets().len() as u32);
let mut finished = BitSet::with_capacity(pred.all_pred_sets().len() as u32);
stack.push(unit.entry());
stack.extend(unit.blocks().filter(|&id| pred.pred_set(id).is_empty()));
while let Some(&next) = stack.last() {
if !discovered.add(next.index() as u32) {
for &succ in pred.succ_set(next) {
if !discovered.contains(succ.index() as u32) {
stack.push(succ);
}
}
} else {
stack.pop();
if !finished.add(next.index() as u32) {
order.push(next);
}
}
}
order
}
pub fn blocks_post_order(&self) -> &[Block] {
&self.post_order
}
pub fn block_order(&self, block: Block) -> usize {
self.inv_post_order[block.index()] as usize
}
pub fn dominates(&self, dominator: Block, follower: Block) -> bool {
self.dominates
.get(&dominator)
.map(|d| d.contains(&follower))
.unwrap_or(false)
}
pub fn dominator(&self, block: Block) -> Block {
self.doms[block.index()]
}
pub fn dominators(&self, follower: Block) -> &HashSet<Block> {
&self.dominated[&follower]
}
pub fn dominated_by(&self, dominator: Block) -> &HashSet<Block> {
&self.dominates[&dominator]
}
pub fn block_dominates_block(&self, parent: Block, mut child: Block) -> bool {
while parent != child {
let next = self.dominator(child);
if next == child {
return false;
}
child = next;
}
true
}
pub fn inst_dominates_block(&self, unit: &Unit, parent: Inst, child: Block) -> bool {
match unit.inst_block(parent) {
Some(bb) => self.block_dominates_block(bb, child),
None => false,
}
}
pub fn value_dominates_block(&self, unit: &Unit, parent: Value, child: Block) -> bool {
match unit[parent] {
ValueData::Inst { inst, .. } => self.inst_dominates_block(unit, inst, child),
ValueData::Arg { .. } => true,
_ => false,
}
}
pub fn block_dominates_inst(&self, unit: &Unit, parent: Block, child: Inst) -> bool {
match unit.inst_block(child) {
Some(bb) => self.block_dominates_block(parent, bb),
None => false,
}
}
pub fn inst_dominates_inst(&self, unit: &Unit, parent: Inst, child: Inst) -> bool {
if parent == child {
return true;
}
let parent_bb = unit.inst_block(parent);
let child_bb = unit.inst_block(child);
let (parent_bb, child_bb) = match (parent_bb, child_bb) {
(Some(a), Some(b)) => (a, b),
_ => return false,
};
let data = &unit[child];
if let (Opcode::Phi, Some(parent_result)) = (data.opcode(), unit.get_inst_result(parent)) {
for (&v, &bb) in data.args().iter().zip(data.blocks().iter()) {
if v == parent_result {
return parent_bb == bb || self.inst_dominates_block(unit, parent, bb);
}
}
}
if parent_bb == child_bb {
let mut pi = parent;
let mut ci = child;
loop {
if let Some(pci) = unit.prev_inst(ci) {
if pci == parent {
return true;
}
ci = pci;
} else {
return false;
}
if let Some(ppi) = unit.prev_inst(pi) {
if ppi == child {
return false;
}
pi = ppi;
} else {
return true;
}
}
}
self.block_dominates_block(parent_bb, child_bb)
}
pub fn value_dominates_inst(&self, unit: &Unit, parent: Value, child: Inst) -> bool {
match unit[parent] {
ValueData::Inst { inst, .. } => self.inst_dominates_inst(unit, inst, child),
ValueData::Arg { .. } => true,
_ => false,
}
}
pub fn block_dominates_value(&self, unit: &Unit, parent: Block, child: Value) -> bool {
unit.get_value_inst(child)
.map(move |inst| self.block_dominates_inst(unit, parent, inst))
.unwrap_or(false)
}
pub fn inst_dominates_value(&self, unit: &Unit, parent: Inst, child: Value) -> bool {
unit.get_value_inst(child)
.map(|inst| self.inst_dominates_inst(unit, parent, inst))
.unwrap_or(false)
}
pub fn value_dominates_value(&self, unit: &Unit, parent: Value, child: Value) -> bool {
unit.get_value_inst(child)
.map(|inst| self.value_dominates_inst(unit, parent, inst))
.unwrap_or(false)
}
}
pub static DOMINATOR_TREE_TIME: AtomicU64 = AtomicU64::new(0);