/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 | 304k | fn merge_sets( |
23 | 304k | idom: &[Block], // map from Block to Block |
24 | 304k | block_to_rpo: &[Option<u32>], |
25 | 304k | mut node1: Block, |
26 | 304k | mut node2: Block, |
27 | 304k | ) -> Block { |
28 | 6.63M | while node1 != node2 { |
29 | 6.32M | if node1.is_invalid() || node2.is_invalid() { |
30 | 0 | return Block::invalid(); |
31 | 6.32M | } |
32 | 6.32M | let rpo1 = block_to_rpo[node1.index()].unwrap(); |
33 | 6.32M | let rpo2 = block_to_rpo[node2.index()].unwrap(); |
34 | 6.32M | if rpo1 > rpo2 { |
35 | 468k | node1 = idom[node1.index()]; |
36 | 5.85M | } else if rpo2 > rpo1 { |
37 | 5.85M | node2 = idom[node2.index()]; |
38 | 5.85M | } |
39 | | } |
40 | 304k | debug_assert!(node1 == node2); |
41 | 304k | node1 |
42 | 304k | } |
43 | | |
44 | 19.7k | pub fn calculate<'a, PredFn: Fn(Block) -> &'a [Block]>( |
45 | 19.7k | num_blocks: usize, |
46 | 19.7k | preds: PredFn, |
47 | 19.7k | post_ord: &[Block], |
48 | 19.7k | block_to_rpo_scratch: &mut Vec<Option<u32>>, |
49 | 19.7k | out: &mut Vec<Block>, |
50 | 19.7k | start: Block, |
51 | 19.7k | ) { |
52 | | // We have post_ord, which is the postorder sequence. |
53 | | // Compute maps from RPO to block number and vice-versa. |
54 | 19.7k | let block_to_rpo = block_to_rpo_scratch.repopulate(num_blocks, None); |
55 | 879k | for (i, rpo_block) in post_ord.iter().rev().enumerate() { |
56 | 879k | block_to_rpo[rpo_block.index()] = Some(i as u32); |
57 | 879k | } |
58 | | |
59 | 19.7k | let idom = out.repopulate(num_blocks, Block::invalid()); |
60 | | // The start node must have itself as a parent. |
61 | 19.7k | idom[start.index()] = start; |
62 | | |
63 | 19.7k | let mut changed = true; |
64 | 58.6k | while changed { |
65 | 38.9k | changed = false; |
66 | | // Consider blocks in reverse postorder. Skip any that are unreachable. |
67 | 1.96M | for &node in post_ord.iter().rev() { |
68 | 1.96M | let rponum = block_to_rpo[node.index()].unwrap(); |
69 | | |
70 | 1.96M | let mut parent = Block::invalid(); |
71 | 2.00M | for &pred in preds(node).iter() { |
72 | 2.00M | let pred_rpo = match block_to_rpo[pred.index()] { |
73 | | None => { |
74 | | // Skip unreachable preds. |
75 | 0 | continue; |
76 | | } |
77 | 2.00M | Some(r) => r, |
78 | | }; |
79 | 2.00M | if pred_rpo < rponum { |
80 | 1.92M | parent = pred; |
81 | 1.92M | break; |
82 | 77.3k | } |
83 | | } |
84 | | |
85 | 1.96M | if parent.is_valid() { |
86 | 2.36M | for &pred in preds(node).iter() { |
87 | 2.36M | if pred == parent { |
88 | 1.92M | continue; |
89 | 444k | } |
90 | 444k | if idom[pred.index()].is_invalid() { |
91 | 140k | continue; |
92 | 304k | } |
93 | 304k | parent = merge_sets(&idom, &block_to_rpo[..], parent, pred); |
94 | | } |
95 | 38.9k | } |
96 | | |
97 | 1.96M | if parent.is_valid() && parent != idom[node.index()] { |
98 | 864k | idom[node.index()] = parent; |
99 | 864k | changed = true; |
100 | 1.10M | } |
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 | 19.7k | idom[start.index()] = Block::invalid(); |
107 | 19.7k | } regalloc2::domtree::calculate::<<regalloc2::cfg::CFGInfo>::init<regalloc2::fuzzing::func::Func>::{closure#1}>Line | Count | Source | 44 | 9.84k | pub fn calculate<'a, PredFn: Fn(Block) -> &'a [Block]>( | 45 | 9.84k | num_blocks: usize, | 46 | 9.84k | preds: PredFn, | 47 | 9.84k | post_ord: &[Block], | 48 | 9.84k | block_to_rpo_scratch: &mut Vec<Option<u32>>, | 49 | 9.84k | out: &mut Vec<Block>, | 50 | 9.84k | start: Block, | 51 | 9.84k | ) { | 52 | | // We have post_ord, which is the postorder sequence. | 53 | | // Compute maps from RPO to block number and vice-versa. | 54 | 9.84k | let block_to_rpo = block_to_rpo_scratch.repopulate(num_blocks, None); | 55 | 439k | for (i, rpo_block) in post_ord.iter().rev().enumerate() { | 56 | 439k | block_to_rpo[rpo_block.index()] = Some(i as u32); | 57 | 439k | } | 58 | | | 59 | 9.84k | let idom = out.repopulate(num_blocks, Block::invalid()); | 60 | | // The start node must have itself as a parent. | 61 | 9.84k | idom[start.index()] = start; | 62 | | | 63 | 9.84k | let mut changed = true; | 64 | 29.2k | while changed { | 65 | 19.4k | changed = false; | 66 | | // Consider blocks in reverse postorder. Skip any that are unreachable. | 67 | 982k | for &node in post_ord.iter().rev() { | 68 | 982k | let rponum = block_to_rpo[node.index()].unwrap(); | 69 | | | 70 | 982k | let mut parent = Block::invalid(); | 71 | 1.00M | for &pred in preds(node).iter() { | 72 | 1.00M | let pred_rpo = match block_to_rpo[pred.index()] { | 73 | | None => { | 74 | | // Skip unreachable preds. | 75 | 0 | continue; | 76 | | } | 77 | 1.00M | Some(r) => r, | 78 | | }; | 79 | 1.00M | if pred_rpo < rponum { | 80 | 962k | parent = pred; | 81 | 962k | break; | 82 | 38.5k | } | 83 | | } | 84 | | | 85 | 982k | if parent.is_valid() { | 86 | 1.18M | for &pred in preds(node).iter() { | 87 | 1.18M | if pred == parent { | 88 | 959k | continue; | 89 | 222k | } | 90 | 222k | if idom[pred.index()].is_invalid() { | 91 | 70.0k | continue; | 92 | 152k | } | 93 | 152k | parent = merge_sets(&idom, &block_to_rpo[..], parent, pred); | 94 | | } | 95 | 19.4k | } | 96 | | | 97 | 982k | if parent.is_valid() && parent != idom[node.index()] { | 98 | 431k | idom[node.index()] = parent; | 99 | 431k | changed = true; | 100 | 550k | } | 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 | 9.84k | idom[start.index()] = Block::invalid(); | 107 | 9.84k | } |
regalloc2::domtree::calculate::<<regalloc2::fuzzing::func::FuncBuilder>::compute_doms::{closure#1}>Line | Count | Source | 44 | 9.87k | pub fn calculate<'a, PredFn: Fn(Block) -> &'a [Block]>( | 45 | 9.87k | num_blocks: usize, | 46 | 9.87k | preds: PredFn, | 47 | 9.87k | post_ord: &[Block], | 48 | 9.87k | block_to_rpo_scratch: &mut Vec<Option<u32>>, | 49 | 9.87k | out: &mut Vec<Block>, | 50 | 9.87k | start: Block, | 51 | 9.87k | ) { | 52 | | // We have post_ord, which is the postorder sequence. | 53 | | // Compute maps from RPO to block number and vice-versa. | 54 | 9.87k | let block_to_rpo = block_to_rpo_scratch.repopulate(num_blocks, None); | 55 | 440k | for (i, rpo_block) in post_ord.iter().rev().enumerate() { | 56 | 440k | block_to_rpo[rpo_block.index()] = Some(i as u32); | 57 | 440k | } | 58 | | | 59 | 9.87k | let idom = out.repopulate(num_blocks, Block::invalid()); | 60 | | // The start node must have itself as a parent. | 61 | 9.87k | idom[start.index()] = start; | 62 | | | 63 | 9.87k | let mut changed = true; | 64 | 29.3k | while changed { | 65 | 19.4k | changed = false; | 66 | | // Consider blocks in reverse postorder. Skip any that are unreachable. | 67 | 985k | for &node in post_ord.iter().rev() { | 68 | 985k | let rponum = block_to_rpo[node.index()].unwrap(); | 69 | | | 70 | 985k | let mut parent = Block::invalid(); | 71 | 1.00M | for &pred in preds(node).iter() { | 72 | 1.00M | let pred_rpo = match block_to_rpo[pred.index()] { | 73 | | None => { | 74 | | // Skip unreachable preds. | 75 | 0 | continue; | 76 | | } | 77 | 1.00M | Some(r) => r, | 78 | | }; | 79 | 1.00M | if pred_rpo < rponum { | 80 | 965k | parent = pred; | 81 | 965k | break; | 82 | 38.7k | } | 83 | | } | 84 | | | 85 | 985k | if parent.is_valid() { | 86 | 1.18M | for &pred in preds(node).iter() { | 87 | 1.18M | if pred == parent { | 88 | 961k | continue; | 89 | 222k | } | 90 | 222k | if idom[pred.index()].is_invalid() { | 91 | 70.1k | continue; | 92 | 152k | } | 93 | 152k | parent = merge_sets(&idom, &block_to_rpo[..], parent, pred); | 94 | | } | 95 | 19.4k | } | 96 | | | 97 | 985k | if parent.is_valid() && parent != idom[node.index()] { | 98 | 432k | idom[node.index()] = parent; | 99 | 432k | changed = true; | 100 | 552k | } | 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 | 9.87k | idom[start.index()] = Block::invalid(); | 107 | 9.87k | } |
Unexecuted instantiation: regalloc2::domtree::calculate::<regalloc2::fuzzing::domtree::check::{closure#1}> |
108 | | |
109 | 385k | pub fn dominates(idom: &[Block], a: Block, mut b: Block) -> bool { |
110 | | loop { |
111 | 3.06M | if a == b { |
112 | 385k | return true; |
113 | 2.67M | } |
114 | 2.67M | if b.is_invalid() { |
115 | 0 | return false; |
116 | 2.67M | } |
117 | 2.67M | b = idom[b.index()]; |
118 | | } |
119 | 385k | } |