Rollup merge of #81338 - bugadani:dominator-cleanup, r=davidtwco

Clean up `dominators_given_rpo`
This commit is contained in:
Jonas Schievink 2021-01-24 22:10:10 +01:00 committed by GitHub
commit 529f15f9b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,6 @@
use super::iterate::reverse_post_order; use super::iterate::reverse_post_order;
use super::ControlFlowGraph; use super::ControlFlowGraph;
use rustc_index::vec::{Idx, IndexVec}; use rustc_index::vec::{Idx, IndexVec};
use std::borrow::BorrowMut;
use std::cmp::Ordering; use std::cmp::Ordering;
#[cfg(test)] #[cfg(test)]
@ -20,22 +19,17 @@ pub fn dominators<G: ControlFlowGraph>(graph: G) -> Dominators<G::Node> {
dominators_given_rpo(graph, &rpo) dominators_given_rpo(graph, &rpo)
} }
fn dominators_given_rpo<G: ControlFlowGraph + BorrowMut<G>>( fn dominators_given_rpo<G: ControlFlowGraph>(graph: G, rpo: &[G::Node]) -> Dominators<G::Node> {
mut graph: G, let start_node = graph.start_node();
rpo: &[G::Node],
) -> Dominators<G::Node> {
let start_node = graph.borrow().start_node();
assert_eq!(rpo[0], start_node); assert_eq!(rpo[0], start_node);
// compute the post order index (rank) for each node // compute the post order index (rank) for each node
let mut post_order_rank: IndexVec<G::Node, usize> = let mut post_order_rank = IndexVec::from_elem_n(0, graph.num_nodes());
(0..graph.borrow().num_nodes()).map(|_| 0).collect();
for (index, node) in rpo.iter().rev().cloned().enumerate() { for (index, node) in rpo.iter().rev().cloned().enumerate() {
post_order_rank[node] = index; post_order_rank[node] = index;
} }
let mut immediate_dominators: IndexVec<G::Node, Option<G::Node>> = let mut immediate_dominators = IndexVec::from_elem_n(None, graph.num_nodes());
(0..graph.borrow().num_nodes()).map(|_| None).collect();
immediate_dominators[start_node] = Some(start_node); immediate_dominators[start_node] = Some(start_node);
let mut changed = true; let mut changed = true;
@ -44,7 +38,7 @@ fn dominators_given_rpo<G: ControlFlowGraph + BorrowMut<G>>(
for &node in &rpo[1..] { for &node in &rpo[1..] {
let mut new_idom = None; let mut new_idom = None;
for pred in graph.borrow_mut().predecessors(node) { for pred in graph.predecessors(node) {
if immediate_dominators[pred].is_some() { if immediate_dominators[pred].is_some() {
// (*) dominators for `pred` have been calculated // (*) dominators for `pred` have been calculated
new_idom = Some(if let Some(new_idom) = new_idom { new_idom = Some(if let Some(new_idom) = new_idom {