/src/regalloc2/src/domtree.rs
Line | Count | Source (jump to first uncovered line) |
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 | 708k | fn merge_sets( |
23 | 708k | idom: &[Block], // map from Block to Block |
24 | 708k | block_to_rpo: &[Option<u32>], |
25 | 708k | mut node1: Block, |
26 | 708k | mut node2: Block, |
27 | 708k | ) -> Block { |
28 | 12.0M | while node1 != node2 { |
29 | 11.3M | if node1.is_invalid() || node2.is_invalid() { |
30 | 0 | return Block::invalid(); |
31 | 11.3M | } |
32 | 11.3M | let rpo1 = block_to_rpo[node1.index()].unwrap(); |
33 | 11.3M | let rpo2 = block_to_rpo[node2.index()].unwrap(); |
34 | 11.3M | if rpo1 > rpo2 { |
35 | 1.04M | node1 = idom[node1.index()]; |
36 | 10.2M | } else if rpo2 > rpo1 { |
37 | 10.2M | node2 = idom[node2.index()]; |
38 | 10.2M | } |
39 | | } |
40 | 708k | debug_assert!(node1 == node2); |
41 | 708k | node1 |
42 | 708k | } |
43 | | |
44 | 34.0k | pub fn calculate<'a, PredFn: Fn(Block) -> &'a [Block]>( |
45 | 34.0k | num_blocks: usize, |
46 | 34.0k | preds: PredFn, |
47 | 34.0k | post_ord: &[Block], |
48 | 34.0k | block_to_rpo_scratch: &mut Vec<Option<u32>>, |
49 | 34.0k | out: &mut Vec<Block>, |
50 | 34.0k | start: Block, |
51 | 34.0k | ) { |
52 | 34.0k | // We have post_ord, which is the postorder sequence. |
53 | 34.0k | // Compute maps from RPO to block number and vice-versa. |
54 | 34.0k | let block_to_rpo = block_to_rpo_scratch.repopulate(num_blocks, None); |
55 | 1.68M | for (i, rpo_block) in post_ord.iter().rev().enumerate() { |
56 | 1.68M | block_to_rpo[rpo_block.index()] = Some(i as u32); |
57 | 1.68M | } |
58 | | |
59 | 34.0k | let idom = out.repopulate(num_blocks, Block::invalid()); |
60 | 34.0k | // The start node must have itself as a parent. |
61 | 34.0k | idom[start.index()] = start; |
62 | 34.0k | |
63 | 34.0k | let mut changed = true; |
64 | 106k | while changed { |
65 | 72.7k | changed = false; |
66 | | // Consider blocks in reverse postorder. Skip any that are unreachable. |
67 | 4.23M | for &node in post_ord.iter().rev() { |
68 | 4.23M | let rponum = block_to_rpo[node.index()].unwrap(); |
69 | 4.23M | |
70 | 4.23M | let mut parent = Block::invalid(); |
71 | 4.31M | for &pred in preds(node).iter() { |
72 | 4.31M | let pred_rpo = match block_to_rpo[pred.index()] { |
73 | | None => { |
74 | | // Skip unreachable preds. |
75 | 0 | continue; |
76 | | } |
77 | 4.31M | Some(r) => r, |
78 | 4.31M | }; |
79 | 4.31M | if pred_rpo < rponum { |
80 | 4.16M | parent = pred; |
81 | 4.16M | break; |
82 | 150k | } |
83 | | } |
84 | | |
85 | 4.23M | if parent.is_valid() { |
86 | 5.12M | for &pred in preds(node).iter() { |
87 | 5.12M | if pred == parent { |
88 | 4.14M | continue; |
89 | 978k | } |
90 | 978k | if idom[pred.index()].is_invalid() { |
91 | 270k | continue; |
92 | 708k | } |
93 | 708k | parent = merge_sets(&idom, &block_to_rpo[..], parent, pred); |
94 | | } |
95 | 72.7k | } |
96 | | |
97 | 4.23M | if parent.is_valid() && parent != idom[node.index()] { |
98 | 1.67M | idom[node.index()] = parent; |
99 | 1.67M | changed = true; |
100 | 2.55M | } |
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 | 34.0k | idom[start.index()] = Block::invalid(); |
107 | 34.0k | } regalloc2::domtree::calculate::<<regalloc2::cfg::CFGInfo>::init<regalloc2::fuzzing::func::Func>::{closure#1}> Line | Count | Source | 44 | 11.3k | pub fn calculate<'a, PredFn: Fn(Block) -> &'a [Block]>( | 45 | 11.3k | num_blocks: usize, | 46 | 11.3k | preds: PredFn, | 47 | 11.3k | post_ord: &[Block], | 48 | 11.3k | block_to_rpo_scratch: &mut Vec<Option<u32>>, | 49 | 11.3k | out: &mut Vec<Block>, | 50 | 11.3k | start: Block, | 51 | 11.3k | ) { | 52 | 11.3k | // We have post_ord, which is the postorder sequence. | 53 | 11.3k | // Compute maps from RPO to block number and vice-versa. | 54 | 11.3k | let block_to_rpo = block_to_rpo_scratch.repopulate(num_blocks, None); | 55 | 559k | for (i, rpo_block) in post_ord.iter().rev().enumerate() { | 56 | 559k | block_to_rpo[rpo_block.index()] = Some(i as u32); | 57 | 559k | } | 58 | | | 59 | 11.3k | let idom = out.repopulate(num_blocks, Block::invalid()); | 60 | 11.3k | // The start node must have itself as a parent. | 61 | 11.3k | idom[start.index()] = start; | 62 | 11.3k | | 63 | 11.3k | let mut changed = true; | 64 | 35.4k | while changed { | 65 | 24.1k | changed = false; | 66 | | // Consider blocks in reverse postorder. Skip any that are unreachable. | 67 | 1.40M | for &node in post_ord.iter().rev() { | 68 | 1.40M | let rponum = block_to_rpo[node.index()].unwrap(); | 69 | 1.40M | | 70 | 1.40M | let mut parent = Block::invalid(); | 71 | 1.43M | for &pred in preds(node).iter() { | 72 | 1.43M | let pred_rpo = match block_to_rpo[pred.index()] { | 73 | | None => { | 74 | | // Skip unreachable preds. | 75 | 0 | continue; | 76 | | } | 77 | 1.43M | Some(r) => r, | 78 | 1.43M | }; | 79 | 1.43M | if pred_rpo < rponum { | 80 | 1.38M | parent = pred; | 81 | 1.38M | break; | 82 | 46.4k | } | 83 | | } | 84 | | | 85 | 1.40M | if parent.is_valid() { | 86 | 1.70M | for &pred in preds(node).iter() { | 87 | 1.70M | if pred == parent { | 88 | 1.38M | continue; | 89 | 323k | } | 90 | 323k | if idom[pred.index()].is_invalid() { | 91 | 90.0k | continue; | 92 | 233k | } | 93 | 233k | parent = merge_sets(&idom, &block_to_rpo[..], parent, pred); | 94 | | } | 95 | 24.1k | } | 96 | | | 97 | 1.40M | if parent.is_valid() && parent != idom[node.index()] { | 98 | 558k | idom[node.index()] = parent; | 99 | 558k | changed = true; | 100 | 850k | } | 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 | 11.3k | idom[start.index()] = Block::invalid(); | 107 | 11.3k | } |
regalloc2::domtree::calculate::<<regalloc2::fuzzing::func::FuncBuilder>::compute_doms::{closure#1}> Line | Count | Source | 44 | 22.6k | pub fn calculate<'a, PredFn: Fn(Block) -> &'a [Block]>( | 45 | 22.6k | num_blocks: usize, | 46 | 22.6k | preds: PredFn, | 47 | 22.6k | post_ord: &[Block], | 48 | 22.6k | block_to_rpo_scratch: &mut Vec<Option<u32>>, | 49 | 22.6k | out: &mut Vec<Block>, | 50 | 22.6k | start: Block, | 51 | 22.6k | ) { | 52 | 22.6k | // We have post_ord, which is the postorder sequence. | 53 | 22.6k | // Compute maps from RPO to block number and vice-versa. | 54 | 22.6k | let block_to_rpo = block_to_rpo_scratch.repopulate(num_blocks, None); | 55 | 1.12M | for (i, rpo_block) in post_ord.iter().rev().enumerate() { | 56 | 1.12M | block_to_rpo[rpo_block.index()] = Some(i as u32); | 57 | 1.12M | } | 58 | | | 59 | 22.6k | let idom = out.repopulate(num_blocks, Block::invalid()); | 60 | 22.6k | // The start node must have itself as a parent. | 61 | 22.6k | idom[start.index()] = start; | 62 | 22.6k | | 63 | 22.6k | let mut changed = true; | 64 | 71.2k | while changed { | 65 | 48.5k | changed = false; | 66 | | // Consider blocks in reverse postorder. Skip any that are unreachable. | 67 | 2.82M | for &node in post_ord.iter().rev() { | 68 | 2.82M | let rponum = block_to_rpo[node.index()].unwrap(); | 69 | 2.82M | | 70 | 2.82M | let mut parent = Block::invalid(); | 71 | 2.88M | for &pred in preds(node).iter() { | 72 | 2.88M | let pred_rpo = match block_to_rpo[pred.index()] { | 73 | | None => { | 74 | | // Skip unreachable preds. | 75 | 0 | continue; | 76 | | } | 77 | 2.88M | Some(r) => r, | 78 | 2.88M | }; | 79 | 2.88M | if pred_rpo < rponum { | 80 | 2.77M | parent = pred; | 81 | 2.77M | break; | 82 | 103k | } | 83 | | } | 84 | | | 85 | 2.82M | if parent.is_valid() { | 86 | 3.41M | for &pred in preds(node).iter() { | 87 | 3.41M | if pred == parent { | 88 | 2.76M | continue; | 89 | 655k | } | 90 | 655k | if idom[pred.index()].is_invalid() { | 91 | 180k | continue; | 92 | 474k | } | 93 | 474k | parent = merge_sets(&idom, &block_to_rpo[..], parent, pred); | 94 | | } | 95 | 48.5k | } | 96 | | | 97 | 2.82M | if parent.is_valid() && parent != idom[node.index()] { | 98 | 1.12M | idom[node.index()] = parent; | 99 | 1.12M | changed = true; | 100 | 1.70M | } | 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 | 22.6k | idom[start.index()] = Block::invalid(); | 107 | 22.6k | } |
|
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 | } |