Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

refactor(cranelift): merge DominatorTreePreorder into DominatorTree (#11596)

Paul Nodet3a14fa398ef0
diff --git a/cranelift/codegen/src/alias_analysis.rs b/cranelift/codegen/src/alias_analysis.rs
index d2ff9a1caa..93adce052c 100644
--- a/cranelift/codegen/src/alias_analysis.rs
+++ b/cranelift/codegen/src/alias_analysis.rs
@@ -63,7 +63,7 @@
use crate::{
cursor::{Cursor, FuncCursor},
- dominator_tree::DominatorTreePreorder,
+ dominator_tree::DominatorTree,
inst_predicates::{
has_memory_fence_semantics, inst_addr_offset_type, inst_store_data, visit_block_succs,
},
@@ -176,7 +176,7 @@ struct MemoryLoc {
/// An alias-analysis pass.
pub struct AliasAnalysis<'a> {
/// The domtree for the function.
- domtree: &'a DominatorTreePreorder,
+ domtree: &'a DominatorTree,
/// Input state to a basic block.
block_input: FxHashMap<Block, LastStores>,
@@ -191,7 +191,7 @@ pub struct AliasAnalysis<'a> {
impl<'a> AliasAnalysis<'a> {
/// Perform an alias analysis pass.
- pub fn new(func: &Function, domtree: &'a DominatorTreePreorder) -> AliasAnalysis<'a> {
+ pub fn new(func: &Function, domtree: &'a DominatorTree) -> AliasAnalysis<'a> {
trace!("alias analysis: input is:\n{:?}", func);
let mut analysis = AliasAnalysis {
domtree,
@@ -333,7 +333,7 @@ impl<'a> AliasAnalysis<'a> {
value.index(),
def_inst.index()
);
- if self.domtree.dominates_inst(def_inst, inst, &func.layout) {
+ if self.domtree.dominates(def_inst, inst, &func.layout) {
trace!(
" -> dominates; value equiv from v{} to v{} inserted",
load_result.index(),
diff --git a/cranelift/codegen/src/context.rs b/cranelift/codegen/src/context.rs
index eed7a2a71b..c37de05497 100644
--- a/cranelift/codegen/src/context.rs
+++ b/cranelift/codegen/src/context.rs
@@ -11,7 +11,6 @@
use crate::alias_analysis::AliasAnalysis;
use crate::dominator_tree::DominatorTree;
-use crate::dominator_tree::DominatorTreePreorder;
use crate::egraph::EgraphPass;
use crate::flowgraph::ControlFlowGraph;
use crate::inline::{Inline, do_inlining};
@@ -48,9 +47,6 @@ pub struct Context {
/// Dominator tree for `func`.
pub domtree: DominatorTree,
- /// Dominator tree with dominance stored implicitly via visit-order indices for `func`
- domtree_preorder: DominatorTreePreorder,
-
/// Loop analysis of `func`.
pub loop_analysis: LoopAnalysis,
@@ -79,7 +75,6 @@ impl Context {
func,
cfg: ControlFlowGraph::new(),
domtree: DominatorTree::new(),
- domtree_preorder: DominatorTreePreorder::new(),
loop_analysis: LoopAnalysis::new(),
compiled_code: None,
want_disasm: false,
@@ -319,7 +314,6 @@ impl Context {
/// Compute dominator tree.
pub fn compute_domtree(&mut self) {
self.domtree.compute(&self.func, &self.cfg);
- self.domtree_preorder.compute(&self.domtree);
}
/// Compute the loop analysis.
@@ -349,7 +343,7 @@ impl Context {
/// by a store instruction to the same instruction (so-called
/// "store-to-load forwarding").
pub fn replace_redundant_loads(&mut self) -> CodegenResult<()> {
- let mut analysis = AliasAnalysis::new(&self.func, &self.domtree_preorder);
+ let mut analysis = AliasAnalysis::new(&self.func, &self.domtree);
analysis.compute_and_update_aliases(&mut self.func);
Ok(())
}
@@ -381,7 +375,7 @@ impl Context {
);
let fisa = fisa.into();
self.compute_loop_analysis();
- let mut alias_analysis = AliasAnalysis::new(&self.func, &self.domtree_preorder);
+ let mut alias_analysis = AliasAnalysis::new(&self.func, &self.domtree);
let mut pass = EgraphPass::new(
&mut self.func,
&self.domtree,
diff --git a/cranelift/codegen/src/dominator_tree.rs b/cranelift/codegen/src/dominator_tree.rs
index bd288c7e6a..10ab9c891e 100644
--- a/cranelift/codegen/src/dominator_tree.rs
+++ b/cranelift/codegen/src/dominator_tree.rs
@@ -2,7 +2,7 @@
use crate::entity::SecondaryMap;
use crate::flowgraph::{BlockPredecessor, ControlFlowGraph};
-use crate::ir::{Block, Function, Inst, Layout, ProgramPoint};
+use crate::ir::{Block, Function, Layout, ProgramPoint};
use crate::packed_option::PackedOption;
use crate::timing;
use alloc::vec::Vec;
@@ -41,7 +41,7 @@ const NOT_VISITED: u32 = 0;
/// It's not required because function's CFG in Cranelift always have
/// a singular root, but helps to avoid additional checks.
/// Numbering nodes from 0 also follows the convention in
-/// `SimpleDominatorTree` and `DominatorTreePreorder`.
+/// `SimpleDominatorTree`.
#[derive(Clone, Default)]
struct SpanningTree {
nodes: Vec<SpanningTreeNode>,
@@ -123,6 +123,20 @@ struct DominatorTreeNode {
idom: PackedOption<Block>,
/// Preorder traversal number, zero for unreachable blocks.
pre_number: u32,
+
+ /// First child node in the domtree.
+ child: PackedOption<Block>,
+
+ /// Next sibling node in the domtree. This linked list is ordered according to the CFG RPO.
+ sibling: PackedOption<Block>,
+
+ /// Sequence number for this node in a pre-order traversal of the dominator tree.
+ /// Unreachable blocks have number 0, the entry block is 1.
+ dom_pre_number: u32,
+
+ /// Maximum `dom_pre_number` for the sub-tree of the dominator tree that is rooted at this node.
+ /// This is always >= `dom_pre_number`.
+ dom_pre_max: u32,
}
/// The dominator tree for a single function,
@@ -236,20 +250,22 @@ impl DominatorTree {
/// Returns `true` if `block_a` dominates `block_b`.
///
/// A block is considered to dominate itself.
- fn block_dominates(&self, block_a: Block, mut block_b: Block) -> bool {
- let pre_a = self.nodes[block_a].pre_number;
-
- // Run a finger up the dominator tree from b until we see a.
- // Do nothing if b is unreachable.
- while pre_a < self.nodes[block_b].pre_number {
- let idom = match self.idom(block_b) {
- Some(idom) => idom,
- None => return false, // a is unreachable, so we climbed past the entry
- };
- block_b = idom;
- }
+ /// This uses preorder numbers for O(1) constant time performance.
+ pub fn block_dominates(&self, block_a: Block, block_b: Block) -> bool {
+ let na = &self.nodes[block_a];
+ let nb = &self.nodes[block_b];
+ na.dom_pre_number <= nb.dom_pre_number && na.dom_pre_max >= nb.dom_pre_max
+ }
- block_a == block_b
+ /// Get an iterator over the direct children of `block` in the dominator tree.
+ ///
+ /// These are the blocks whose immediate dominator is `block`, ordered according
+ /// to the CFG reverse post-order.
+ pub fn children(&self, block: Block) -> ChildIter<'_> {
+ ChildIter {
+ domtree: self,
+ next: self.nodes[block].child,
+ }
}
}
@@ -297,6 +313,7 @@ impl DominatorTree {
self.clear();
self.compute_spanning_tree(func);
self.compute_domtree(cfg);
+ self.compute_domtree_preorder();
self.valid = true;
}
@@ -447,93 +464,50 @@ impl DominatorTree {
self.nodes[block].idom = self.stree[idom].block;
}
}
-}
-
-/// Optional pre-order information that can be computed for a dominator tree.
-///
-/// This data structure is computed from a `DominatorTree` and provides:
-///
-/// - A forward traversable dominator tree through the `children()` iterator.
-/// - An ordering of blocks according to a dominator tree pre-order.
-/// - Constant time dominance checks at the block granularity.
-///
-/// The information in this auxiliary data structure is not easy to update when the control flow
-/// graph changes, which is why it is kept separate.
-pub struct DominatorTreePreorder {
- nodes: SecondaryMap<Block, ExtraNode>,
-
- // Scratch memory used by `compute_postorder()`.
- stack: Vec<Block>,
-}
-
-#[derive(Default, Clone)]
-struct ExtraNode {
- /// First child node in the domtree.
- child: PackedOption<Block>,
-
- /// Next sibling node in the domtree. This linked list is ordered according to the CFG RPO.
- sibling: PackedOption<Block>,
-
- /// Sequence number for this node in a pre-order traversal of the dominator tree.
- /// Unreachable blocks have number 0, the entry block is 1.
- pre_number: u32,
-
- /// Maximum `pre_number` for the sub-tree of the dominator tree that is rooted at this node.
- /// This is always >= `pre_number`.
- pre_max: u32,
-}
-
-/// Creating and computing the dominator tree pre-order.
-impl DominatorTreePreorder {
- /// Create a new blank `DominatorTreePreorder`.
- pub fn new() -> Self {
- Self {
- nodes: SecondaryMap::new(),
- stack: Vec::new(),
- }
- }
-
- /// Recompute this data structure to match `domtree`.
- pub fn compute(&mut self, domtree: &DominatorTree) {
- self.nodes.clear();
+ /// Compute dominator tree preorder information.
+ ///
+ /// This populates child/sibling links and preorder numbers for fast dominance checks.
+ fn compute_domtree_preorder(&mut self) {
// Step 1: Populate the child and sibling links.
//
// By following the CFG post-order and pushing to the front of the lists, we make sure that
// sibling lists are ordered according to the CFG reverse post-order.
- for &block in domtree.cfg_postorder() {
- if let Some(idom) = domtree.idom(block) {
+ for &block in &self.postorder {
+ if let Some(idom) = self.idom(block) {
let sib = mem::replace(&mut self.nodes[idom].child, block.into());
self.nodes[block].sibling = sib;
} else {
// The only block without an immediate dominator is the entry.
- self.stack.push(block);
+ self.dfs_worklist.push(TraversalEvent::Enter(0, block));
}
}
// Step 2. Assign pre-order numbers from a DFS of the dominator tree.
- debug_assert!(self.stack.len() <= 1);
+ debug_assert!(self.dfs_worklist.len() <= 1);
let mut n = 0;
- while let Some(block) = self.stack.pop() {
- n += 1;
- let node = &mut self.nodes[block];
- node.pre_number = n;
- node.pre_max = n;
- if let Some(n) = node.sibling.expand() {
- self.stack.push(n);
- }
- if let Some(n) = node.child.expand() {
- self.stack.push(n);
+ while let Some(event) = self.dfs_worklist.pop() {
+ if let TraversalEvent::Enter(_, block) = event {
+ n += 1;
+ let node = &mut self.nodes[block];
+ node.dom_pre_number = n;
+ node.dom_pre_max = n;
+ if let Some(sibling) = node.sibling.expand() {
+ self.dfs_worklist.push(TraversalEvent::Enter(0, sibling));
+ }
+ if let Some(child) = node.child.expand() {
+ self.dfs_worklist.push(TraversalEvent::Enter(0, child));
+ }
}
}
- // Step 3. Propagate the `pre_max` numbers up the tree.
+ // Step 3. Propagate the `dom_pre_max` numbers up the tree.
// The CFG post-order is topologically ordered w.r.t. dominance so a node comes after all
// its dominator tree children.
- for &block in domtree.cfg_postorder() {
- if let Some(idom) = domtree.idom(block) {
- let pre_max = cmp::max(self.nodes[block].pre_max, self.nodes[idom].pre_max);
- self.nodes[idom].pre_max = pre_max;
+ for &block in &self.postorder {
+ if let Some(idom) = self.idom(block) {
+ let pre_max = cmp::max(self.nodes[block].dom_pre_max, self.nodes[idom].dom_pre_max);
+ self.nodes[idom].dom_pre_max = pre_max;
}
}
}
@@ -541,7 +515,7 @@ impl DominatorTreePreorder {
/// An iterator that enumerates the direct children of a block in the dominator tree.
pub struct ChildIter<'a> {
- dtpo: &'a DominatorTreePreorder,
+ domtree: &'a DominatorTree,
next: PackedOption<Block>,
}
@@ -551,73 +525,12 @@ impl<'a> Iterator for ChildIter<'a> {
fn next(&mut self) -> Option<Block> {
let n = self.next.expand();
if let Some(block) = n {
- self.next = self.dtpo.nodes[block].sibling;
+ self.next = self.domtree.nodes[block].sibling;
}
n
}
}
-/// Query interface for the dominator tree pre-order.
-impl DominatorTreePreorder {
- /// Get an iterator over the direct children of `block` in the dominator tree.
- ///
- /// These are the block's whose immediate dominator is an instruction in `block`, ordered according
- /// to the CFG reverse post-order.
- pub fn children(&self, block: Block) -> ChildIter<'_> {
- ChildIter {
- dtpo: self,
- next: self.nodes[block].child,
- }
- }
-
- /// Fast, constant time dominance check with block granularity.
- ///
- /// This computes the same result as `domtree.dominates(a, b)`, but in guaranteed fast constant
- /// time. This is less general than the `DominatorTree` method because it only works with block
- /// program points.
- ///
- /// A block is considered to dominate itself.
- pub fn dominates(&self, a: Block, b: Block) -> bool {
- let na = &self.nodes[a];
- let nb = &self.nodes[b];
- na.pre_number <= nb.pre_number && na.pre_max >= nb.pre_max
- }
-
- /// Checks if one instruction dominates another.
- pub fn dominates_inst(&self, a: Inst, b: Inst, layout: &Layout) -> bool {
- match (layout.inst_block(a), layout.inst_block(b)) {
- (Some(block_a), Some(block_b)) => {
- if block_a == block_b {
- layout.pp_cmp(a, b) != Ordering::Greater
- } else {
- self.dominates(block_a, block_b)
- }
- }
- _ => false,
- }
- }
-
- /// Compare two blocks according to the dominator pre-order.
- pub fn pre_cmp_block(&self, a: Block, b: Block) -> Ordering {
- self.nodes[a].pre_number.cmp(&self.nodes[b].pre_number)
- }
-
- /// Compare two program points according to the dominator tree pre-order.
- ///
- /// This ordering of program points have the property that given a program point, pp, all the
- /// program points dominated by pp follow immediately and contiguously after pp in the order.
- pub fn pre_cmp<A, B>(&self, a: A, b: B, layout: &Layout) -> Ordering
- where
- A: Into<ProgramPoint>,
- B: Into<ProgramPoint>,
- {
- let a = a.into();
- let b = b.into();
- self.pre_cmp_block(layout.pp_block(a), layout.pp_block(b))
- .then_with(|| layout.pp_cmp(a, b))
- }
-}
-
#[cfg(test)]
mod tests {
use super::*;
@@ -633,9 +546,6 @@ mod tests {
let dtree = DominatorTree::with_function(&func, &cfg);
assert_eq!(0, dtree.nodes.keys().count());
assert_eq!(dtree.cfg_postorder(), &[]);
-
- let mut dtpo = DominatorTreePreorder::new();
- dtpo.compute(&dtree);
}
#[test]
@@ -681,17 +591,15 @@ mod tests {
assert!(!dt.dominates(v2_def, block0, &cur.func.layout));
assert!(!dt.dominates(block0, v2_def, &cur.func.layout));
- let mut dtpo = DominatorTreePreorder::new();
- dtpo.compute(&dt);
- assert!(dtpo.dominates(block0, block0));
- assert!(!dtpo.dominates(block0, block1));
- assert!(dtpo.dominates(block0, block2));
- assert!(!dtpo.dominates(block1, block0));
- assert!(dtpo.dominates(block1, block1));
- assert!(!dtpo.dominates(block1, block2));
- assert!(!dtpo.dominates(block2, block0));
- assert!(!dtpo.dominates(block2, block1));
- assert!(dtpo.dominates(block2, block2));
+ assert!(dt.block_dominates(block0, block0));
+ assert!(!dt.block_dominates(block0, block1));
+ assert!(dt.block_dominates(block0, block2));
+ assert!(!dt.block_dominates(block1, block0));
+ assert!(dt.block_dominates(block1, block1));
+ assert!(!dt.block_dominates(block1, block2));
+ assert!(!dt.block_dominates(block2, block0));
+ assert!(!dt.block_dominates(block2, block1));
+ assert!(dt.block_dominates(block2, block2));
}
#[test]
diff --git a/cranelift/codegen/src/egraph.rs b/cranelift/codegen/src/egraph.rs
index 3ab3dfcc19..ca0e4468b3 100644
--- a/cranelift/codegen/src/egraph.rs
+++ b/cranelift/codegen/src/egraph.rs
@@ -3,7 +3,7 @@
use crate::alias_analysis::{AliasAnalysis, LastStores};
use crate::ctxhash::{CtxEq, CtxHash, NullCtx};
use crate::cursor::{Cursor, CursorPosition, FuncCursor};
-use crate::dominator_tree::{DominatorTree, DominatorTreePreorder};
+use crate::dominator_tree::DominatorTree;
use crate::egraph::elaborate::Elaborator;
use crate::inst_predicates::{is_mergeable_for_egraph, is_pure_for_egraph};
use crate::ir::pcc::Fact;
@@ -54,7 +54,7 @@ pub struct EgraphPass<'a> {
/// Dominator tree for the CFG, used to visit blocks in pre-order
/// so we see value definitions before their uses, and also used for
/// O(1) dominance checks.
- domtree: DominatorTreePreorder,
+ domtree: &'a DominatorTree,
/// Alias analysis, used during optimization.
alias_analysis: &'a mut AliasAnalysis<'a>,
/// Loop analysis results, used for built-in LICM during
@@ -92,7 +92,7 @@ where
pub(crate) gvn_map_blocks: &'opt Vec<Block>,
pub(crate) remat_values: &'opt mut FxHashSet<Value>,
pub(crate) stats: &'opt mut Stats,
- domtree: &'opt DominatorTreePreorder,
+ domtree: &'opt DominatorTree,
pub(crate) alias_analysis: &'opt mut AliasAnalysis<'analysis>,
pub(crate) alias_analysis_state: &'opt mut LastStores,
flags: &'opt Flags,
@@ -264,10 +264,10 @@ where
block
})
.max_by(|&x, &y| {
- if self.domtree.dominates(x, y) {
+ if self.domtree.block_dominates(x, y) {
Ordering::Less
} else {
- debug_assert!(self.domtree.dominates(y, x));
+ debug_assert!(self.domtree.block_dominates(y, x));
Ordering::Greater
}
})
@@ -407,7 +407,11 @@ where
fn merge_availability(&self, a: Value, b: Value) -> Block {
let a = self.available_block[a];
let b = self.available_block[b];
- if self.domtree.dominates(a, b) { a } else { b }
+ if self.domtree.block_dominates(a, b) {
+ a
+ } else {
+ b
+ }
}
/// Optimize a "skeleton" instruction.
@@ -714,14 +718,12 @@ impl<'a> EgraphPass<'a> {
/// Create a new EgraphPass.
pub fn new(
func: &'a mut Function,
- raw_domtree: &'a DominatorTree,
+ domtree: &'a DominatorTree,
loop_analysis: &'a LoopAnalysis,
alias_analysis: &'a mut AliasAnalysis<'a>,
flags: &'a Flags,
ctrl_plane: &'a mut ControlPlane,
) -> Self {
- let mut domtree = DominatorTreePreorder::new();
- domtree.compute(raw_domtree);
Self {
func,
domtree,
diff --git a/cranelift/codegen/src/egraph/elaborate.rs b/cranelift/codegen/src/egraph/elaborate.rs
index f2c70f48fc..b8d4ac159d 100644
--- a/cranelift/codegen/src/egraph/elaborate.rs
+++ b/cranelift/codegen/src/egraph/elaborate.rs
@@ -4,7 +4,7 @@
use super::Stats;
use super::cost::Cost;
use crate::ctxhash::NullCtx;
-use crate::dominator_tree::DominatorTreePreorder;
+use crate::dominator_tree::DominatorTree;
use crate::hash_map::Entry as HashEntry;
use crate::inst_predicates::is_pure_for_egraph;
use crate::ir::{Block, Function, Inst, Value, ValueDef};
@@ -19,7 +19,7 @@ use smallvec::{SmallVec, smallvec};
pub(crate) struct Elaborator<'a> {
func: &'a mut Function,
- domtree: &'a DominatorTreePreorder,
+ domtree: &'a DominatorTree,
loop_analysis: &'a LoopAnalysis,
/// Map from Value that is produced by a pure Inst (and was thus
/// not in the side-effecting skeleton) to the value produced by
@@ -140,7 +140,7 @@ enum BlockStackEntry {
impl<'a> Elaborator<'a> {
pub(crate) fn new(
func: &'a mut Function,
- domtree: &'a DominatorTreePreorder,
+ domtree: &'a DominatorTree,
loop_analysis: &'a LoopAnalysis,
remat_values: &'a FxHashSet<Value>,
stats: &'a mut Stats,
@@ -560,29 +560,30 @@ impl<'a> Elaborator<'a> {
// place the instruction. This is the current
// block *unless* we hoist above a loop when all
// args are loop-invariant (and this op is pure).
- let (scope_depth, before, insert_block) =
- if loop_hoist_level == self.loop_stack.len() {
- // Depends on some value at the current
- // loop depth, or remat forces it here:
- // place it at the current location.
- (
- self.value_to_elaborated_value.depth(),
- before,
- self.func.layout.inst_block(before).unwrap(),
- )
- } else {
- // Does not depend on any args at current
- // loop depth: hoist out of loop.
- self.stats.elaborate_licm_hoist += 1;
- let data = &self.loop_stack[loop_hoist_level];
- // `data.hoist_block` should dominate `before`'s block.
- let before_block = self.func.layout.inst_block(before).unwrap();
- debug_assert!(self.domtree.dominates(data.hoist_block, before_block));
- // Determine the instruction at which we
- // insert in `data.hoist_block`.
- let before = self.func.layout.last_inst(data.hoist_block).unwrap();
- (data.scope_depth as usize, before, data.hoist_block)
- };
+ let (scope_depth, before, insert_block) = if loop_hoist_level
+ == self.loop_stack.len()
+ {
+ // Depends on some value at the current
+ // loop depth, or remat forces it here:
+ // place it at the current location.
+ (
+ self.value_to_elaborated_value.depth(),
+ before,
+ self.func.layout.inst_block(before).unwrap(),
+ )
+ } else {
+ // Does not depend on any args at current
+ // loop depth: hoist out of loop.
+ self.stats.elaborate_licm_hoist += 1;
+ let data = &self.loop_stack[loop_hoist_level];
+ // `data.hoist_block` should dominate `before`'s block.
+ let before_block = self.func.layout.inst_block(before).unwrap();
+ debug_assert!(self.domtree.block_dominates(data.hoist_block, before_block));
+ // Determine the instruction at which we
+ // insert in `data.hoist_block`.
+ let before = self.func.layout.last_inst(data.hoist_block).unwrap();
+ (data.scope_depth as usize, before, data.hoist_block)
+ };
trace!(
" -> decided to place: before {} insert_block {}",
@@ -778,7 +779,7 @@ impl<'a> Elaborator<'a> {
}
}
- fn elaborate_domtree(&mut self, domtree: &DominatorTreePreorder) {
+ fn elaborate_domtree(&mut self, domtree: &DominatorTree) {
self.block_stack.push(BlockStackEntry::Elaborate {
block: self.func.layout.entry_block().unwrap(),
idom: None,
diff --git a/cranelift/codegen/src/loop_analysis.rs b/cranelift/codegen/src/loop_analysis.rs
index 0f457bca2f..b9ac3a7c6a 100644
--- a/cranelift/codegen/src/loop_analysis.rs
+++ b/cranelift/codegen/src/loop_analysis.rs
@@ -6,7 +6,7 @@ use crate::entity::SecondaryMap;
use crate::entity::entity_impl;
use crate::entity::{Keys, PrimaryMap};
use crate::flowgraph::ControlFlowGraph;
-use crate::ir::{Block, Function, Layout};
+use crate::ir::{Block, Function};
use crate::packed_option::PackedOption;
use crate::timing;
use alloc::vec::Vec;
@@ -166,8 +166,8 @@ impl LoopAnalysis {
self.loops.clear();
self.block_loop_map.clear();
self.block_loop_map.resize(func.dfg.num_blocks());
- self.find_loop_headers(cfg, domtree, &func.layout);
- self.discover_loop_blocks(cfg, domtree, &func.layout);
+ self.find_loop_headers(cfg, domtree);
+ self.discover_loop_blocks(cfg, domtree);
self.assign_loop_levels();
self.valid = true;
}
@@ -192,28 +192,18 @@ impl LoopAnalysis {
// Determines if a block dominates any predecessor
// and thus is a loop header.
- fn is_block_loop_header(
- block: Block,
- cfg: &ControlFlowGraph,
- domtree: &DominatorTree,
- layout: &Layout,
- ) -> bool {
+ fn is_block_loop_header(block: Block, cfg: &ControlFlowGraph, domtree: &DominatorTree) -> bool {
// A block is a loop header if it dominates any of its predecessors.
cfg.pred_iter(block)
- .any(|pred| domtree.dominates(block, pred.inst, layout))
+ .any(|pred| domtree.block_dominates(block, pred.block))
}
// Traverses the CFG in reverse postorder and create a loop object for every block having a
// back edge.
- fn find_loop_headers(
- &mut self,
- cfg: &ControlFlowGraph,
- domtree: &DominatorTree,
- layout: &Layout,
- ) {
+ fn find_loop_headers(&mut self, cfg: &ControlFlowGraph, domtree: &DominatorTree) {
for &block in domtree
.cfg_rpo()
- .filter(|&&block| Self::is_block_loop_header(block, cfg, domtree, layout))
+ .filter(|&&block| Self::is_block_loop_header(block, cfg, domtree))
{
// This block is a loop header, so we create its associated loop
let lp = self.loops.push(LoopData::new(block, None));
@@ -224,12 +214,7 @@ impl LoopAnalysis {
// Intended to be called after `find_loop_headers`. For each detected loop header,
// discovers all the block belonging to the loop and its inner loops. After a call to this
// function, the loop tree is fully constructed.
- fn discover_loop_blocks(
- &mut self,
- cfg: &ControlFlowGraph,
- domtree: &DominatorTree,
- layout: &Layout,
- ) {
+ fn discover_loop_blocks(&mut self, cfg: &ControlFlowGraph, domtree: &DominatorTree) {
let mut stack: Vec<Block> = Vec::new();
// We handle each loop header in reverse order, corresponding to a pseudo postorder
// traversal of the graph.
@@ -239,7 +224,7 @@ impl LoopAnalysis {
cfg.pred_iter(self.loops[lp].header)
.filter(|pred| {
// We follow the back edges
- domtree.dominates(self.loops[lp].header, pred.inst, layout)
+ domtree.block_dominates(self.loops[lp].header, pred.block)
})
.map(|pred| pred.block),
);
diff --git a/cranelift/codegen/src/verifier/mod.rs b/cranelift/codegen/src/verifier/mod.rs
index 1c19270f6e..b07bab2626 100644
--- a/cranelift/codegen/src/verifier/mod.rs
+++ b/cranelift/codegen/src/verifier/mod.rs
@@ -65,7 +65,6 @@
use crate::dbg::DisplayList;
use crate::dominator_tree::DominatorTree;
-use crate::dominator_tree::DominatorTreePreorder;
use crate::entity::SparseSet;
use crate::flowgraph::{BlockPredecessor, ControlFlowGraph};
use crate::ir::ExceptionTableItem;
@@ -303,7 +302,6 @@ enum BlockCallTargetType {
struct Verifier<'a> {
func: &'a Function,
expected_cfg: ControlFlowGraph,
- expected_domtree_preorder: DominatorTreePreorder,
expected_domtree: DominatorTree,
isa: Option<&'a dyn TargetIsa>,
}
@@ -312,13 +310,10 @@ impl<'a> Verifier<'a> {
pub fn new(func: &'a Function, fisa: FlagsOrIsa<'a>) -> Self {
let expected_cfg = ControlFlowGraph::with_function(func);
let expected_domtree = DominatorTree::with_function(func, &expected_cfg);
- let mut expected_domtree_preorder = DominatorTreePreorder::new();
- expected_domtree_preorder.compute(&expected_domtree);
Self {
func,
expected_cfg,
expected_domtree,
- expected_domtree_preorder,
isa: fisa.isa,
}
}
@@ -1006,11 +1001,10 @@ impl<'a> Verifier<'a> {
}
// Defining instruction dominates the instruction that uses the value.
if is_reachable {
- if !self.expected_domtree_preorder.dominates_inst(
- def_inst,
- loc_inst,
- &self.func.layout,
- ) {
+ if !self
+ .expected_domtree
+ .dominates(def_inst, loc_inst, &self.func.layout)
+ {
return errors.fatal((
loc_inst,
self.context(loc_inst),
@@ -1045,7 +1039,7 @@ impl<'a> Verifier<'a> {
}
let user_block = self.func.layout.inst_block(loc_inst).expect("Expected instruction to be in a block as we're traversing code already in layout");
// The defining block dominates the instruction using this value.
- if is_reachable && !self.expected_domtree_preorder.dominates(block, user_block) {
+ if is_reachable && !self.expected_domtree.block_dominates(block, user_block) {
return errors.fatal((
loc_inst,
self.context(loc_inst),
diff --git a/cranelift/filetests/src/test_domtree.rs b/cranelift/filetests/src/test_domtree.rs
index 7255c728cb..81b5d09dd9 100644
--- a/cranelift/filetests/src/test_domtree.rs
+++ b/cranelift/filetests/src/test_domtree.rs
@@ -14,7 +14,7 @@
use crate::match_directive::match_directive;
use crate::subtest::{Context, SubTest, run_filecheck};
-use cranelift_codegen::dominator_tree::{DominatorTree, DominatorTreePreorder};
+use cranelift_codegen::dominator_tree::DominatorTree;
use cranelift_codegen::flowgraph::ControlFlowGraph;
use cranelift_codegen::ir::Function;
use cranelift_codegen::ir::entities::AnyEntity;
@@ -134,14 +134,12 @@ fn filecheck_text(func: &Function, domtree: &DominatorTree) -> Result<String, fm
// Compute and print out a pre-order of the dominator tree.
writeln!(s, "domtree_preorder {{")?;
- let mut dtpo = DominatorTreePreorder::new();
- dtpo.compute(domtree);
let mut stack = Vec::new();
stack.extend(func.layout.entry_block());
while let Some(block) = stack.pop() {
write!(s, " {block}:")?;
let i = stack.len();
- for ch in dtpo.children(block) {
+ for ch in domtree.children(block) {
write!(s, " {ch}")?;
stack.push(ch);
}