Skip to content

Commit 6be4691

Browse files
committed
deconstruct the ControlFlowGraph trait into more granular traits
1 parent 537c667 commit 6be4691

File tree

5 files changed

+130
-64
lines changed

5 files changed

+130
-64
lines changed

src/librustc/mir/mod.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use mir::visit::MirVisitable;
2222
use rustc_apfloat::ieee::{Double, Single};
2323
use rustc_apfloat::Float;
2424
use rustc_data_structures::control_flow_graph::dominators::{dominators, Dominators};
25-
use rustc_data_structures::control_flow_graph::ControlFlowGraph;
25+
use rustc_data_structures::control_flow_graph;
2626
use rustc_data_structures::control_flow_graph::{GraphPredecessors, GraphSuccessors};
2727
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
2828
use rustc_data_structures::small_vec::SmallVec;
@@ -2277,23 +2277,32 @@ fn item_path_str(def_id: DefId) -> String {
22772277
ty::tls::with(|tcx| tcx.item_path_str(def_id))
22782278
}
22792279

2280-
impl<'tcx> ControlFlowGraph for Mir<'tcx> {
2280+
impl<'tcx> control_flow_graph::DirectedGraph for Mir<'tcx> {
22812281
type Node = BasicBlock;
2282+
}
22822283

2284+
impl<'tcx> control_flow_graph::WithNumNodes for Mir<'tcx> {
22832285
fn num_nodes(&self) -> usize {
22842286
self.basic_blocks.len()
22852287
}
2288+
}
22862289

2290+
impl<'tcx> control_flow_graph::WithStartNode for Mir<'tcx> {
22872291
fn start_node(&self) -> Self::Node {
22882292
START_BLOCK
22892293
}
2294+
}
22902295

2296+
impl<'tcx> control_flow_graph::WithPredecessors for Mir<'tcx> {
22912297
fn predecessors<'graph>(
22922298
&'graph self,
22932299
node: Self::Node,
22942300
) -> <Self as GraphPredecessors<'graph>>::Iter {
22952301
self.predecessors_for(node).clone().into_iter()
22962302
}
2303+
}
2304+
2305+
impl<'tcx> control_flow_graph::WithSuccessors for Mir<'tcx> {
22972306
fn successors<'graph>(
22982307
&'graph self,
22992308
node: Self::Node,
@@ -2302,12 +2311,12 @@ impl<'tcx> ControlFlowGraph for Mir<'tcx> {
23022311
}
23032312
}
23042313

2305-
impl<'a, 'b> GraphPredecessors<'b> for Mir<'a> {
2314+
impl<'a, 'b> control_flow_graph::GraphPredecessors<'b> for Mir<'a> {
23062315
type Item = BasicBlock;
23072316
type Iter = IntoIter<BasicBlock>;
23082317
}
23092318

2310-
impl<'a, 'b> GraphSuccessors<'b> for Mir<'a> {
2319+
impl<'a, 'b> control_flow_graph::GraphSuccessors<'b> for Mir<'a> {
23112320
type Item = BasicBlock;
23122321
type Iter = iter::Cloned<Successors<'b>>;
23132322
}

src/librustc_data_structures/control_flow_graph/dominators/mod.rs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
//! Rice Computer Science TS-06-33870
1515
//! <https://www.cs.rice.edu/~keith/EMBED/dom.pdf>
1616
17-
use super::ControlFlowGraph;
17+
use super::super::indexed_vec::{Idx, IndexVec};
1818
use super::iterate::reverse_post_order;
19-
use super::super::indexed_vec::{IndexVec, Idx};
19+
use super::ControlFlowGraph;
2020

2121
use std::fmt;
2222

@@ -29,15 +29,16 @@ pub fn dominators<G: ControlFlowGraph>(graph: &G) -> Dominators<G::Node> {
2929
dominators_given_rpo(graph, &rpo)
3030
}
3131

32-
pub fn dominators_given_rpo<G: ControlFlowGraph>(graph: &G,
33-
rpo: &[G::Node])
34-
-> Dominators<G::Node> {
32+
pub fn dominators_given_rpo<G: ControlFlowGraph>(
33+
graph: &G,
34+
rpo: &[G::Node],
35+
) -> Dominators<G::Node> {
3536
let start_node = graph.start_node();
3637
assert_eq!(rpo[0], start_node);
3738

3839
// compute the post order index (rank) for each node
39-
let mut post_order_rank: IndexVec<G::Node, usize> = IndexVec::from_elem_n(usize::default(),
40-
graph.num_nodes());
40+
let mut post_order_rank: IndexVec<G::Node, usize> =
41+
IndexVec::from_elem_n(usize::default(), graph.num_nodes());
4142
for (index, node) in rpo.iter().rev().cloned().enumerate() {
4243
post_order_rank[node] = index;
4344
}
@@ -56,10 +57,12 @@ pub fn dominators_given_rpo<G: ControlFlowGraph>(graph: &G,
5657
if immediate_dominators[pred].is_some() {
5758
// (*)
5859
// (*) dominators for `pred` have been calculated
59-
new_idom = intersect_opt(&post_order_rank,
60-
&immediate_dominators,
61-
new_idom,
62-
Some(pred));
60+
new_idom = intersect_opt(
61+
&post_order_rank,
62+
&immediate_dominators,
63+
new_idom,
64+
Some(pred),
65+
);
6366
}
6467
}
6568

@@ -76,23 +79,25 @@ pub fn dominators_given_rpo<G: ControlFlowGraph>(graph: &G,
7679
}
7780
}
7881

79-
fn intersect_opt<Node: Idx>(post_order_rank: &IndexVec<Node, usize>,
80-
immediate_dominators: &IndexVec<Node, Option<Node>>,
81-
node1: Option<Node>,
82-
node2: Option<Node>)
83-
-> Option<Node> {
82+
fn intersect_opt<Node: Idx>(
83+
post_order_rank: &IndexVec<Node, usize>,
84+
immediate_dominators: &IndexVec<Node, Option<Node>>,
85+
node1: Option<Node>,
86+
node2: Option<Node>,
87+
) -> Option<Node> {
8488
match (node1, node2) {
8589
(None, None) => None,
8690
(Some(n), None) | (None, Some(n)) => Some(n),
8791
(Some(n1), Some(n2)) => Some(intersect(post_order_rank, immediate_dominators, n1, n2)),
8892
}
8993
}
9094

91-
fn intersect<Node: Idx>(post_order_rank: &IndexVec<Node, usize>,
92-
immediate_dominators: &IndexVec<Node, Option<Node>>,
93-
mut node1: Node,
94-
mut node2: Node)
95-
-> Node {
95+
fn intersect<Node: Idx>(
96+
post_order_rank: &IndexVec<Node, usize>,
97+
immediate_dominators: &IndexVec<Node, Option<Node>>,
98+
mut node1: Node,
99+
mut node2: Node,
100+
) -> Node {
96101
while node1 != node2 {
97102
while post_order_rank[node1] < post_order_rank[node2] {
98103
node1 = immediate_dominators[node1].unwrap();
@@ -176,11 +181,13 @@ impl<Node: Idx> DominatorTree<Node> {
176181

177182
impl<Node: Idx> fmt::Debug for DominatorTree<Node> {
178183
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
179-
fmt::Debug::fmt(&DominatorTreeNode {
180-
tree: self,
181-
node: self.root,
182-
},
183-
fmt)
184+
fmt::Debug::fmt(
185+
&DominatorTreeNode {
186+
tree: self,
187+
node: self.root,
188+
},
189+
fmt,
190+
)
184191
}
185192
}
186193

@@ -194,11 +201,9 @@ impl<'tree, Node: Idx> fmt::Debug for DominatorTreeNode<'tree, Node> {
194201
let subtrees: Vec<_> = self.tree
195202
.children(self.node)
196203
.iter()
197-
.map(|&child| {
198-
DominatorTreeNode {
199-
tree: self.tree,
200-
node: child,
201-
}
204+
.map(|&child| DominatorTreeNode {
205+
tree: self.tree,
206+
node: child,
202207
})
203208
.collect();
204209
fmt.debug_tuple("")

src/librustc_data_structures/control_flow_graph/iterate/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use super::ControlFlowGraph;
1211
use super::super::indexed_vec::IndexVec;
12+
use super::{DirectedGraph, WithSuccessors, WithNumNodes};
1313

1414
#[cfg(test)]
1515
mod test;
1616

17-
pub fn post_order_from<G: ControlFlowGraph>(graph: &G, start_node: G::Node) -> Vec<G::Node> {
17+
pub fn post_order_from<G: DirectedGraph + WithSuccessors + WithNumNodes>(
18+
graph: &G,
19+
start_node: G::Node,
20+
) -> Vec<G::Node> {
1821
post_order_from_to(graph, start_node, None)
1922
}
2023

21-
pub fn post_order_from_to<G: ControlFlowGraph>(graph: &G,
22-
start_node: G::Node,
23-
end_node: Option<G::Node>)
24-
-> Vec<G::Node> {
24+
pub fn post_order_from_to<G: DirectedGraph + WithSuccessors + WithNumNodes>(
25+
graph: &G,
26+
start_node: G::Node,
27+
end_node: Option<G::Node>,
28+
) -> Vec<G::Node> {
2529
let mut visited: IndexVec<G::Node, bool> = IndexVec::from_elem_n(false, graph.num_nodes());
2630
let mut result: Vec<G::Node> = Vec::with_capacity(graph.num_nodes());
2731
if let Some(end_node) = end_node {
@@ -31,10 +35,12 @@ pub fn post_order_from_to<G: ControlFlowGraph>(graph: &G,
3135
result
3236
}
3337

34-
fn post_order_walk<G: ControlFlowGraph>(graph: &G,
35-
node: G::Node,
36-
result: &mut Vec<G::Node>,
37-
visited: &mut IndexVec<G::Node, bool>) {
38+
fn post_order_walk<G: DirectedGraph + WithSuccessors + WithNumNodes>(
39+
graph: &G,
40+
node: G::Node,
41+
result: &mut Vec<G::Node>,
42+
visited: &mut IndexVec<G::Node, bool>,
43+
) {
3844
if visited[node] {
3945
return;
4046
}
@@ -47,7 +53,10 @@ fn post_order_walk<G: ControlFlowGraph>(graph: &G,
4753
result.push(node);
4854
}
4955

50-
pub fn reverse_post_order<G: ControlFlowGraph>(graph: &G, start_node: G::Node) -> Vec<G::Node> {
56+
pub fn reverse_post_order<G: DirectedGraph + WithSuccessors + WithNumNodes>(
57+
graph: &G,
58+
start_node: G::Node,
59+
) -> Vec<G::Node> {
5160
let mut vec = post_order_from(graph, start_node);
5261
vec.reverse();
5362
vec

src/librustc_data_structures/control_flow_graph/mod.rs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,61 @@ mod reference;
1717
#[cfg(test)]
1818
mod test;
1919

20-
pub trait ControlFlowGraph
21-
where Self: for<'graph> GraphPredecessors<'graph, Item=<Self as ControlFlowGraph>::Node>,
22-
Self: for<'graph> GraphSuccessors<'graph, Item=<Self as ControlFlowGraph>::Node>
23-
{
20+
pub trait DirectedGraph {
2421
type Node: Idx;
22+
}
2523

24+
pub trait WithNumNodes: DirectedGraph {
2625
fn num_nodes(&self) -> usize;
27-
fn start_node(&self) -> Self::Node;
28-
fn predecessors<'graph>(&'graph self, node: Self::Node)
29-
-> <Self as GraphPredecessors<'graph>>::Iter;
30-
fn successors<'graph>(&'graph self, node: Self::Node)
31-
-> <Self as GraphSuccessors<'graph>>::Iter;
3226
}
3327

34-
pub trait GraphPredecessors<'graph> {
28+
pub trait WithSuccessors: DirectedGraph
29+
where
30+
Self: for<'graph> GraphSuccessors<'graph, Item = <Self as DirectedGraph>::Node>,
31+
{
32+
fn successors<'graph>(
33+
&'graph self,
34+
node: Self::Node,
35+
) -> <Self as GraphSuccessors<'graph>>::Iter;
36+
}
37+
38+
pub trait GraphSuccessors<'graph> {
3539
type Item;
3640
type Iter: Iterator<Item = Self::Item>;
3741
}
3842

39-
pub trait GraphSuccessors<'graph> {
43+
pub trait WithPredecessors: DirectedGraph
44+
where
45+
Self: for<'graph> GraphPredecessors<'graph, Item = <Self as DirectedGraph>::Node>,
46+
{
47+
fn predecessors<'graph>(
48+
&'graph self,
49+
node: Self::Node,
50+
) -> <Self as GraphPredecessors<'graph>>::Iter;
51+
}
52+
53+
pub trait GraphPredecessors<'graph> {
4054
type Item;
4155
type Iter: Iterator<Item = Self::Item>;
4256
}
57+
58+
pub trait WithStartNode: DirectedGraph {
59+
fn start_node(&self) -> Self::Node;
60+
}
61+
62+
pub trait ControlFlowGraph:
63+
DirectedGraph + WithStartNode + WithPredecessors + WithStartNode + WithSuccessors + WithNumNodes
64+
{
65+
// convenient trait
66+
}
67+
68+
impl<T> ControlFlowGraph for T
69+
where
70+
T: DirectedGraph
71+
+ WithStartNode
72+
+ WithPredecessors
73+
+ WithStartNode
74+
+ WithSuccessors
75+
+ WithNumNodes,
76+
{
77+
}

src/librustc_data_structures/control_flow_graph/reference.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,42 @@
1010

1111
use super::*;
1212

13-
impl<'graph, G: ControlFlowGraph> ControlFlowGraph for &'graph G {
13+
impl<'graph, G: DirectedGraph> DirectedGraph for &'graph G {
1414
type Node = G::Node;
15+
}
1516

17+
impl<'graph, G: WithNumNodes> WithNumNodes for &'graph G {
1618
fn num_nodes(&self) -> usize {
1719
(**self).num_nodes()
1820
}
21+
}
1922

23+
impl<'graph, G: WithStartNode> WithStartNode for &'graph G {
2024
fn start_node(&self) -> Self::Node {
2125
(**self).start_node()
2226
}
27+
}
28+
29+
impl<'graph, G: WithSuccessors> WithSuccessors for &'graph G {
30+
fn successors<'iter>(&'iter self, node: Self::Node) -> <Self as GraphSuccessors<'iter>>::Iter {
31+
(**self).successors(node)
32+
}
33+
}
2334

35+
impl<'graph, G: WithPredecessors> WithPredecessors for &'graph G {
2436
fn predecessors<'iter>(&'iter self,
2537
node: Self::Node)
2638
-> <Self as GraphPredecessors<'iter>>::Iter {
2739
(**self).predecessors(node)
2840
}
29-
30-
fn successors<'iter>(&'iter self, node: Self::Node) -> <Self as GraphSuccessors<'iter>>::Iter {
31-
(**self).successors(node)
32-
}
3341
}
3442

35-
impl<'iter, 'graph, G: ControlFlowGraph> GraphPredecessors<'iter> for &'graph G {
43+
impl<'iter, 'graph, G: WithPredecessors> GraphPredecessors<'iter> for &'graph G {
3644
type Item = G::Node;
3745
type Iter = <G as GraphPredecessors<'iter>>::Iter;
3846
}
3947

40-
impl<'iter, 'graph, G: ControlFlowGraph> GraphSuccessors<'iter> for &'graph G {
48+
impl<'iter, 'graph, G: WithSuccessors> GraphSuccessors<'iter> for &'graph G {
4149
type Item = G::Node;
4250
type Iter = <G as GraphSuccessors<'iter>>::Iter;
4351
}

0 commit comments

Comments
 (0)