Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/librustc_data_structures/control_flow_graph/dominators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ pub fn dominators_given_rpo<G: ControlFlowGraph>(graph: &G,
// (*)
// (*) dominators for `pred` have been calculated
new_idom = intersect_opt(&post_order_rank,
&immediate_dominators,
new_idom,
Some(pred));
&immediate_dominators,
new_idom,
Some(pred));
}
}

Expand All @@ -77,10 +77,10 @@ pub fn dominators_given_rpo<G: ControlFlowGraph>(graph: &G,
}

fn intersect_opt<Node: Idx>(post_order_rank: &IndexVec<Node, usize>,
immediate_dominators: &IndexVec<Node, Option<Node>>,
node1: Option<Node>,
node2: Option<Node>)
-> Option<Node> {
immediate_dominators: &IndexVec<Node, Option<Node>>,
node1: Option<Node>,
node2: Option<Node>)
-> Option<Node> {
match (node1, node2) {
(None, None) => None,
(Some(n), None) | (None, Some(n)) => Some(n),
Expand All @@ -89,10 +89,10 @@ fn intersect_opt<Node: Idx>(post_order_rank: &IndexVec<Node, usize>,
}

fn intersect<Node: Idx>(post_order_rank: &IndexVec<Node, usize>,
immediate_dominators: &IndexVec<Node, Option<Node>>,
mut node1: Node,
mut node2: Node)
-> Node {
immediate_dominators: &IndexVec<Node, Option<Node>>,
mut node1: Node,
mut node2: Node)
-> Node {
while node1 != node2 {
while post_order_rank[node1] < post_order_rank[node2] {
node1 = immediate_dominators[node1].unwrap();
Expand Down Expand Up @@ -142,9 +142,9 @@ impl<Node: Idx> Dominators<Node> {
"node {:?} is not reachable",
node2);
intersect::<Node>(&self.post_order_rank,
&self.immediate_dominators,
node1,
node2)
&self.immediate_dominators,
node1,
node2)
}

pub fn mutual_dominator<I>(&self, iter: I) -> Option<Node>
Expand Down
22 changes: 4 additions & 18 deletions src/librustc_data_structures/control_flow_graph/dominators/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ use super::*;

#[test]
fn diamond() {
let graph = TestGraph::new(0, &[
(0, 1),
(0, 2),
(1, 3),
(2, 3),
]);
let graph = TestGraph::new(0, &[(0, 1), (0, 2), (1, 3), (2, 3)]);

let dominators = dominators(&graph);
let immediate_dominators = dominators.all_immediate_dominators();
Expand All @@ -32,17 +27,9 @@ fn diamond() {
#[test]
fn paper() {
// example from the paper:
let graph = TestGraph::new(6, &[
(6, 5),
(6, 4),
(5, 1),
(4, 2),
(4, 3),
(1, 2),
(2, 3),
(3, 2),
(2, 1),
]);
let graph = TestGraph::new(6,
&[(6, 5), (6, 4), (5, 1), (4, 2), (4, 3), (1, 2), (2, 3), (3, 2),
(2, 1)]);

let dominators = dominators(&graph);
let immediate_dominators = dominators.all_immediate_dominators();
Expand All @@ -54,4 +41,3 @@ fn paper() {
assert_eq!(immediate_dominators[5], Some(6));
assert_eq!(immediate_dominators[6], Some(6));
}

20 changes: 3 additions & 17 deletions src/librustc_data_structures/control_flow_graph/iterate/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ use super::*;

#[test]
fn diamond_post_order() {
let graph = TestGraph::new(0, &[
(0, 1),
(0, 2),
(1, 3),
(2, 3),
]);
let graph = TestGraph::new(0, &[(0, 1), (0, 2), (1, 3), (2, 3)]);

let result = post_order_from(&graph, 0);
assert_eq!(result, vec![3, 1, 2, 0]);
Expand All @@ -33,16 +28,8 @@ fn rev_post_order_inner_loop() {
// ^ ^ v |
// | 6 <- 4 |
// +-----------------+
let graph = TestGraph::new(0, &[
(0, 1),
(1, 2),
(2, 3),
(3, 5),
(3, 1),
(2, 4),
(4, 6),
(6, 2),
]);
let graph = TestGraph::new(0,
&[(0, 1), (1, 2), (2, 3), (3, 5), (3, 1), (2, 4), (4, 6), (6, 2)]);

let rev_graph = TransposedGraph::new(&graph);

Expand All @@ -52,4 +39,3 @@ fn rev_post_order_inner_loop() {
let result = post_order_from_to(&rev_graph, 3, Some(1));
assert_eq!(result, vec![4, 6, 2, 3]);
}

6 changes: 3 additions & 3 deletions src/librustc_data_structures/control_flow_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ pub trait ControlFlowGraph

pub trait GraphPredecessors<'graph> {
type Item;
type Iter: Iterator<Item=Self::Item>;
type Iter: Iterator<Item = Self::Item>;
}

pub trait GraphSuccessors<'graph> {
type Item;
type Iter: Iterator<Item=Self::Item>;
}
type Iter: Iterator<Item = Self::Item>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use super::super::indexed_vec::{IndexVec, Idx};
#[cfg(test)]
mod test;

pub fn reachable<G: ControlFlowGraph>(graph: &G)
-> Reachability<G::Node> {
pub fn reachable<G: ControlFlowGraph>(graph: &G) -> Reachability<G::Node> {
let reverse_post_order = reverse_post_order(graph, graph.start_node());
reachable_given_rpo(graph, &reverse_post_order)
}
Expand Down Expand Up @@ -53,12 +52,10 @@ pub struct Reachability<Node: Idx> {
impl<Node: Idx> Reachability<Node> {
fn new<G: ControlFlowGraph>(graph: &G) -> Self {
let num_nodes = graph.num_nodes();
Reachability {
bits: IndexVec::from_elem_n(BitVector::new(num_nodes), num_nodes),
}
Reachability { bits: IndexVec::from_elem_n(BitVector::new(num_nodes), num_nodes) }
}

pub fn can_reach(&self, source: Node, target: Node)-> bool {
pub fn can_reach(&self, source: Node, target: Node) -> bool {
let bit: usize = target.index();
self.bits[source].contains(bit)
}
Expand Down
22 changes: 4 additions & 18 deletions src/librustc_data_structures/control_flow_graph/reachable/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@ fn test1() {
// 0 -> 1 -> 2 -> 3
// ^ v
// 6 <- 4 -> 5
let graph = TestGraph::new(0, &[
(0, 1),
(1, 2),
(2, 3),
(2, 4),
(4, 5),
(4, 6),
(6, 1),
]);
let graph = TestGraph::new(0, &[(0, 1), (1, 2), (2, 3), (2, 4), (4, 5), (4, 6), (6, 1)]);
let reachable = reachable(&graph);
assert!((0..6).all(|i| reachable.can_reach(0, i)));
assert!((1..6).all(|i| reachable.can_reach(1, i)));
Expand All @@ -43,15 +35,9 @@ fn test2() {
// 30 -> 31 -> 32 -> 33
// ^ v
// 36 <- 34 -> 35
let graph = TestGraph::new(30, &[
(30, 31),
(31, 32),
(32, 33),
(32, 34),
(34, 35),
(34, 36),
(36, 31),
]);
let graph = TestGraph::new(30,
&[(30, 31), (31, 32), (32, 33), (32, 34), (34, 35), (34, 36),
(36, 31)]);
let reachable = reachable(&graph);
assert!((30..36).all(|i| reachable.can_reach(30, i)));
assert!((31..36).all(|i| reachable.can_reach(31, i)));
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_data_structures/control_flow_graph/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ impl<'graph, G: ControlFlowGraph> ControlFlowGraph for &'graph G {
(**self).start_node()
}

fn predecessors<'iter>(&'iter self, node: Self::Node)
-> <Self as GraphPredecessors<'iter>>::Iter {
fn predecessors<'iter>(&'iter self,
node: Self::Node)
-> <Self as GraphPredecessors<'iter>>::Iter {
(**self).predecessors(node)
}

fn successors<'iter>(&'iter self, node: Self::Node)
-> <Self as GraphSuccessors<'iter>>::Iter {
fn successors<'iter>(&'iter self, node: Self::Node) -> <Self as GraphSuccessors<'iter>>::Iter {
(**self).successors(node)
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/librustc_data_structures/control_flow_graph/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl TestGraph {
num_nodes: start_node + 1,
start_node: start_node,
successors: HashMap::new(),
predecessors: HashMap::new()
predecessors: HashMap::new(),
};
for &(source, target) in edges {
graph.num_nodes = max(graph.num_nodes, source + 1);
Expand All @@ -55,13 +55,13 @@ impl ControlFlowGraph for TestGraph {
self.num_nodes
}

fn predecessors<'graph>(&'graph self, node: usize)
fn predecessors<'graph>(&'graph self,
node: usize)
-> <Self as GraphPredecessors<'graph>>::Iter {
self.predecessors[&node].iter().cloned()
self.predecessors[&node].iter().cloned()
}

fn successors<'graph>(&'graph self, node: usize)
-> <Self as GraphSuccessors<'graph>>::Iter {
fn successors<'graph>(&'graph self, node: usize) -> <Self as GraphSuccessors<'graph>>::Iter {
self.successors[&node].iter().cloned()
}
}
Expand All @@ -75,4 +75,3 @@ impl<'graph> GraphSuccessors<'graph> for TestGraph {
type Item = usize;
type Iter = iter::Cloned<slice::Iter<'graph, usize>>;
}

11 changes: 8 additions & 3 deletions src/librustc_data_structures/control_flow_graph/transpose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ impl<G: ControlFlowGraph> TransposedGraph<G> {
}

pub fn with_start(base_graph: G, start_node: G::Node) -> Self {
TransposedGraph { base_graph: base_graph, start_node: start_node }
TransposedGraph {
base_graph: base_graph,
start_node: start_node,
}
}
}

Expand All @@ -37,12 +40,14 @@ impl<G: ControlFlowGraph> ControlFlowGraph for TransposedGraph<G> {
self.start_node
}

fn predecessors<'graph>(&'graph self, node: Self::Node)
fn predecessors<'graph>(&'graph self,
node: Self::Node)
-> <Self as GraphPredecessors<'graph>>::Iter {
self.base_graph.successors(node)
}

fn successors<'graph>(&'graph self, node: Self::Node)
fn successors<'graph>(&'graph self,
node: Self::Node)
-> <Self as GraphSuccessors<'graph>>::Iter {
self.base_graph.predecessors(node)
}
Expand Down