From 8991002644dc23588ca57c99d33001a13ec24061 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 8 May 2021 19:50:50 -0400 Subject: [PATCH] Remove separate RPO traversal This integrates the preorder and postorder traversals into one. --- .../src/graph/dominators/mod.rs | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_data_structures/src/graph/dominators/mod.rs b/compiler/rustc_data_structures/src/graph/dominators/mod.rs index b819b7f419a..043e706c398 100644 --- a/compiler/rustc_data_structures/src/graph/dominators/mod.rs +++ b/compiler/rustc_data_structures/src/graph/dominators/mod.rs @@ -4,7 +4,6 @@ //! "Linear-Time Algorithms for Dominators and Related Problems", //! ftp://ftp.cs.princeton.edu/techreports/2005/737.pdf -use super::iterate::reverse_post_order; use super::ControlFlowGraph; use rustc_index::vec::{Idx, IndexVec}; use std::cmp::Ordering; @@ -12,38 +11,26 @@ #[cfg(test)] mod tests; -pub fn dominators(graph: G) -> Dominators { - let start_node = graph.start_node(); - let rpo = reverse_post_order(&graph, start_node); - dominators_given_rpo(graph, &rpo) -} - struct PreOrderFrame { node: Node, iter: Iter, } -fn dominators_given_rpo(graph: G, rpo: &[G::Node]) -> Dominators { - let start_node = graph.start_node(); - assert_eq!(rpo[0], start_node); - +pub fn dominators(graph: G) -> Dominators { // compute the post order index (rank) for each node let mut post_order_rank = IndexVec::from_elem_n(0, graph.num_nodes()); - for (index, node) in rpo.iter().rev().cloned().enumerate() { - post_order_rank[node] = index; - } - let mut visited = BitSet::new_empty(graph.num_nodes()); - let mut parent: IndexVec> = IndexVec::from_elem_n(None, rpo.len()); + let mut parent: IndexVec> = IndexVec::from_elem_n(None, graph.num_nodes()); let mut stack = vec![PreOrderFrame { node: 0, iter: graph.successors(graph.start_node()) }]; visited.insert(graph.start_node()); - let mut pre_order_to_real = Vec::with_capacity(rpo.len()); + let mut pre_order_to_real = Vec::with_capacity(graph.num_nodes()); let mut real_to_pre_order: IndexVec> = IndexVec::from_elem_n(None, graph.num_nodes()); pre_order_to_real.push(graph.start_node()); real_to_pre_order[graph.start_node()] = Some(0); let mut idx = 1; + let mut post_order_idx = 0; 'recurse: while let Some(frame) = stack.last_mut() { while let Some(successor) = frame.iter.next() { @@ -57,6 +44,9 @@ fn dominators_given_rpo(graph: G, rpo: &[G::Node]) -> Domin continue 'recurse; } } + post_order_rank[pre_order_to_real[frame.node]] = post_order_idx; + post_order_idx += 1; + stack.pop(); }