Coverage Report

Created: 2026-04-09 08:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/regalloc2/src/domtree.rs
Line
Count
Source
1
/*
2
 * Derives from the dominator tree implementation in regalloc.rs, which is
3
 * licensed under the Apache Public License 2.0 with LLVM Exception. See:
4
 * https://github.com/bytecodealliance/regalloc.rs
5
 */
6
7
// This is an implementation of the algorithm described in
8
//
9
//   A Simple, Fast Dominance Algorithm
10
//   Keith D. Cooper, Timothy J. Harvey, and Ken Kennedy
11
//   Department of Computer Science, Rice University, Houston, Texas, USA
12
//   TR-06-33870
13
//   https://www.cs.rice.edu/~keith/EMBED/dom.pdf
14
15
use core::u32;
16
17
use alloc::vec::Vec;
18
19
use crate::{Block, VecExt};
20
21
// Helper
22
0
fn merge_sets(
23
0
    idom: &[Block], // map from Block to Block
24
0
    block_to_rpo: &[Option<u32>],
25
0
    mut node1: Block,
26
0
    mut node2: Block,
27
0
) -> Block {
28
0
    while node1 != node2 {
29
0
        if node1.is_invalid() || node2.is_invalid() {
30
0
            return Block::invalid();
31
0
        }
32
0
        let rpo1 = block_to_rpo[node1.index()].unwrap();
33
0
        let rpo2 = block_to_rpo[node2.index()].unwrap();
34
0
        if rpo1 > rpo2 {
35
0
            node1 = idom[node1.index()];
36
0
        } else if rpo2 > rpo1 {
37
0
            node2 = idom[node2.index()];
38
0
        }
39
    }
40
0
    debug_assert!(node1 == node2);
41
0
    node1
42
0
}
43
44
0
pub fn calculate<'a, PredFn: Fn(Block) -> &'a [Block]>(
45
0
    num_blocks: usize,
46
0
    preds: PredFn,
47
0
    post_ord: &[Block],
48
0
    block_to_rpo_scratch: &mut Vec<Option<u32>>,
49
0
    out: &mut Vec<Block>,
50
0
    start: Block,
51
0
) {
52
    // We have post_ord, which is the postorder sequence.
53
    // Compute maps from RPO to block number and vice-versa.
54
0
    let block_to_rpo = block_to_rpo_scratch.repopulate(num_blocks, None);
55
0
    for (i, rpo_block) in post_ord.iter().rev().enumerate() {
56
0
        block_to_rpo[rpo_block.index()] = Some(i as u32);
57
0
    }
58
59
0
    let idom = out.repopulate(num_blocks, Block::invalid());
60
    // The start node must have itself as a parent.
61
0
    idom[start.index()] = start;
62
63
0
    let mut changed = true;
64
0
    while changed {
65
0
        changed = false;
66
        // Consider blocks in reverse postorder. Skip any that are unreachable.
67
0
        for &node in post_ord.iter().rev() {
68
0
            let rponum = block_to_rpo[node.index()].unwrap();
69
70
0
            let mut parent = Block::invalid();
71
0
            for &pred in preds(node).iter() {
72
0
                let pred_rpo = match block_to_rpo[pred.index()] {
73
                    None => {
74
                        // Skip unreachable preds.
75
0
                        continue;
76
                    }
77
0
                    Some(r) => r,
78
                };
79
0
                if pred_rpo < rponum {
80
0
                    parent = pred;
81
0
                    break;
82
0
                }
83
            }
84
85
0
            if parent.is_valid() {
86
0
                for &pred in preds(node).iter() {
87
0
                    if pred == parent {
88
0
                        continue;
89
0
                    }
90
0
                    if idom[pred.index()].is_invalid() {
91
0
                        continue;
92
0
                    }
93
0
                    parent = merge_sets(&idom, &block_to_rpo[..], parent, pred);
94
                }
95
0
            }
96
97
0
            if parent.is_valid() && parent != idom[node.index()] {
98
0
                idom[node.index()] = parent;
99
0
                changed = true;
100
0
            }
101
        }
102
    }
103
104
    // Now set the start node's dominator-tree parent to "invalid";
105
    // this allows the loop in `dominates` to terminate.
106
0
    idom[start.index()] = Block::invalid();
107
0
}
Unexecuted instantiation: regalloc2::domtree::calculate::<<regalloc2::cfg::CFGInfo>::init<regalloc2::fuzzing::func::Func>::{closure#1}>
Unexecuted instantiation: regalloc2::domtree::calculate::<<regalloc2::fuzzing::func::FuncBuilder>::compute_doms::{closure#1}>
Unexecuted instantiation: regalloc2::domtree::calculate::<regalloc2::fuzzing::domtree::check::{closure#1}>
108
109
0
pub fn dominates(idom: &[Block], a: Block, mut b: Block) -> bool {
110
    loop {
111
0
        if a == b {
112
0
            return true;
113
0
        }
114
0
        if b.is_invalid() {
115
0
            return false;
116
0
        }
117
0
        b = idom[b.index()];
118
    }
119
0
}