waffle/src/cfg/domtree.rs

118 lines
3.4 KiB
Rust
Raw Normal View History

2021-11-15 07:56:56 +00:00
/*
* Derives from the dominator tree implementation in regalloc.rs, which is
* licensed under the Apache Public License 2.0 with LLVM Exception. See:
* https://github.com/bytecodealliance/regalloc.rs
*/
// This is an implementation of the algorithm described in
//
// A Simple, Fast Dominance Algorithm
// Keith D. Cooper, Timothy J. Harvey, and Ken Kennedy
// Department of Computer Science, Rice University, Houston, Texas, USA
// TR-06-33870
// https://www.cs.rice.edu/~keith/EMBED/dom.pdf
2022-11-02 03:43:47 +00:00
use crate::entity::{EntityRef, PerEntity};
use crate::ir::Block;
2021-11-15 07:56:56 +00:00
// Helper
fn merge_sets(
2022-11-02 03:43:47 +00:00
idom: &PerEntity<Block, Block>, // map from Block to Block
block_to_rpo: &PerEntity<Block, Option<u32>>,
mut node1: Block,
mut node2: Block,
) -> Block {
2021-11-15 07:56:56 +00:00
while node1 != node2 {
2022-11-02 03:43:47 +00:00
if node1.is_invalid() || node2.is_invalid() {
return Block::invalid();
2021-11-15 07:56:56 +00:00
}
let rpo1 = block_to_rpo[node1].unwrap();
let rpo2 = block_to_rpo[node2].unwrap();
if rpo1 > rpo2 {
node1 = idom[node1];
} else if rpo2 > rpo1 {
node2 = idom[node2];
}
}
assert!(node1 == node2);
node1
}
2022-11-02 03:43:47 +00:00
pub fn calculate<'a, PredFn: Fn(Block) -> &'a [Block]>(
2021-11-15 07:56:56 +00:00
preds: PredFn,
2022-11-02 03:43:47 +00:00
post_ord: &[Block],
start: Block,
) -> PerEntity<Block, Block> {
2021-11-15 07:56:56 +00:00
// We have post_ord, which is the postorder sequence.
// Compute maps from RPO to block number and vice-versa.
2022-11-02 03:43:47 +00:00
let mut block_to_rpo: PerEntity<Block, Option<u32>> = PerEntity::default();
2021-11-15 07:56:56 +00:00
for (i, rpo_block) in post_ord.iter().rev().enumerate() {
block_to_rpo[*rpo_block] = Some(i as u32);
}
2022-11-02 03:43:47 +00:00
let mut idom: PerEntity<Block, Block> = PerEntity::default();
2021-11-15 07:56:56 +00:00
// The start node must have itself as a parent.
idom[start] = start;
let mut changed = true;
while changed {
changed = false;
// Consider blocks in reverse postorder. Skip any that are unreachable.
for &node in post_ord.iter().rev() {
let rponum = block_to_rpo[node].unwrap();
2022-11-02 03:43:47 +00:00
let mut parent = Block::invalid();
2021-11-15 07:56:56 +00:00
for &pred in preds(node).iter() {
let pred_rpo = match block_to_rpo[pred] {
Some(r) => r,
None => {
// Skip unreachable preds.
continue;
}
};
if pred_rpo < rponum {
parent = pred;
break;
}
}
2022-11-02 03:43:47 +00:00
if parent != Block::invalid() {
2021-11-15 07:56:56 +00:00
for &pred in preds(node).iter() {
if pred == parent {
continue;
}
2022-11-02 03:43:47 +00:00
if idom[pred] == Block::invalid() {
2021-11-15 07:56:56 +00:00
continue;
}
2022-11-02 03:43:47 +00:00
parent = merge_sets(&idom, &block_to_rpo, parent, pred);
2021-11-15 07:56:56 +00:00
}
}
2022-11-02 03:43:47 +00:00
if parent != Block::invalid() && parent != idom[node] {
2021-11-15 07:56:56 +00:00
idom[node] = parent;
changed = true;
}
}
}
// Now set the start node's dominator-tree parent to "invalid";
// this allows the loop in `dominates` to terminate.
2022-11-02 03:43:47 +00:00
idom[start] = Block::invalid();
2021-11-15 07:56:56 +00:00
idom
}
2022-11-02 03:43:47 +00:00
pub fn dominates(idom: &PerEntity<Block, Block>, a: Block, mut b: Block) -> bool {
2021-11-15 07:56:56 +00:00
loop {
if a == b {
return true;
}
2022-11-02 03:43:47 +00:00
if b.is_invalid() {
2021-11-15 07:56:56 +00:00
return false;
}
b = idom[b];
}
}