Skip to content

Commit 2d3ab15

Browse files
committed
Merge branch 'dominator-cache3' of github.com:scottcarr/rust into dominator-cache
2 parents 0519b68 + 1cc95c9 commit 2d3ab15

File tree

3 files changed

+128
-17
lines changed

3 files changed

+128
-17
lines changed

src/librustc/mir/cache.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
use std::cell::{Ref, RefCell};
1212
use rustc_data_structures::indexed_vec::IndexVec;
13-
use rustc_data_structures::graph_algorithms::dominators::{Dominators, dominators};
14-
use mir::mir_cfg::MirCfg;
1513

1614
use mir::repr::{Mir, BasicBlock};
1715

@@ -20,7 +18,6 @@ use rustc_serialize as serialize;
2018
#[derive(Clone)]
2119
pub struct Cache {
2220
predecessors: RefCell<Option<IndexVec<BasicBlock, Vec<BasicBlock>>>>,
23-
dominators: RefCell<Option<Dominators<MirCfg>>>,
2421
}
2522

2623

@@ -40,7 +37,6 @@ impl Cache {
4037
pub fn new() -> Self {
4138
Cache {
4239
predecessors: RefCell::new(None),
43-
dominators: RefCell::new(None),
4440
}
4541
}
4642

@@ -56,14 +52,6 @@ impl Cache {
5652

5753
Ref::map(self.predecessors.borrow(), |p| p.as_ref().unwrap())
5854
}
59-
60-
pub fn dominators(&self, mir: &Mir) -> Ref<Dominators<MirCfg>> {
61-
if self.dominators.borrow().is_none() {
62-
*self.dominators.borrow_mut() = Some(calculate_dominators(mir, self));
63-
}
64-
65-
Ref::map(self.dominators.borrow(), |p| p.as_ref().unwrap())
66-
}
6755
}
6856

6957
fn calculate_predecessors(mir: &Mir) -> IndexVec<BasicBlock, Vec<BasicBlock>> {
@@ -78,8 +66,3 @@ fn calculate_predecessors(mir: &Mir) -> IndexVec<BasicBlock, Vec<BasicBlock>> {
7866

7967
result
8068
}
81-
82-
fn calculate_dominators(mir: &Mir, cache: &Cache) -> Dominators<MirCfg> {
83-
let m = MirCfg::new(mir, cache);
84-
dominators(&m)
85-
}

src/librustc/mir/repr.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use graphviz::IntoCow;
1212
use middle::const_val::ConstVal;
1313
use rustc_const_math::{ConstUsize, ConstInt, ConstMathErr};
1414
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
15+
use rustc_data_structures::graph_algorithms::dominators::{Dominators, dominators};
16+
use rustc_data_structures::graph_algorithms::{Graph, GraphPredecessors, GraphSuccessors};
1517
use hir::def_id::DefId;
1618
use ty::subst::Substs;
1719
use ty::{self, AdtDef, ClosureSubsts, FnOutput, Region, Ty};
@@ -144,6 +146,16 @@ impl<'tcx> Mir<'tcx> {
144146
pub fn predecessors_for(&self, bb: BasicBlock) -> Ref<Vec<BasicBlock>> {
145147
Ref::map(self.predecessors(), |p| &p[bb])
146148
}
149+
<<<<<<< HEAD
150+
=======
151+
152+
#[inline]
153+
//pub fn dominators(&'tcx self) -> Ref<Dominators<Self>> {
154+
pub fn dominators(&self) -> Dominators<Self> {
155+
//self.cache.dominators(self)
156+
dominators(self)
157+
}
158+
>>>>>>> 1cc95c9c198110db2840e76e0a00015ac39785f8
147159
}
148160

149161
impl<'tcx> Index<BasicBlock> for Mir<'tcx> {
@@ -1167,3 +1179,32 @@ fn node_to_string(node_id: ast::NodeId) -> String {
11671179
fn item_path_str(def_id: DefId) -> String {
11681180
ty::tls::with(|tcx| tcx.item_path_str(def_id))
11691181
}
1182+
impl<'tcx> Graph for Mir<'tcx> {
1183+
1184+
type Node = BasicBlock;
1185+
1186+
fn num_nodes(&self) -> usize { self.basic_blocks.len() }
1187+
1188+
fn start_node(&self) -> Self::Node { START_BLOCK }
1189+
1190+
fn predecessors<'graph>(&'graph self, node: Self::Node)
1191+
-> <Self as GraphPredecessors<'graph>>::Iter
1192+
{
1193+
self.predecessors_for(node).clone().into_iter()
1194+
}
1195+
fn successors<'graph>(&'graph self, node: Self::Node)
1196+
-> <Self as GraphSuccessors<'graph>>::Iter
1197+
{
1198+
self.basic_blocks[node].terminator().successors().into_owned().into_iter()
1199+
}
1200+
}
1201+
1202+
impl<'a, 'b> GraphPredecessors<'b> for Mir<'a> {
1203+
type Item = BasicBlock;
1204+
type Iter = std::vec::IntoIter<BasicBlock>;
1205+
}
1206+
1207+
impl<'a, 'b> GraphSuccessors<'b> for Mir<'a> {
1208+
type Item = BasicBlock;
1209+
type Iter = std::vec::IntoIter<BasicBlock>;
1210+
}

src/librustc_data_structures/graph_algorithms/mod.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
// except according to those terms.
1010

1111
use super::indexed_vec::Idx;
12+
use core::marker::{PhantomData};
13+
pub use std::slice::Iter;
14+
use std::ops::{Index, IndexMut};
15+
use std::clone::Clone;
16+
use std;
1217

1318
//pub mod bit_set;
1419
pub mod dominators;
@@ -24,15 +29,20 @@ mod test;
2429
pub trait Graph
2530
where Self: for<'graph> GraphPredecessors<'graph, Item=<Self as Graph>::Node>,
2631
Self: for<'graph> GraphSuccessors<'graph, Item=<Self as Graph>::Node>
32+
//where Self: Sized
2733
{
2834
type Node: Idx;
2935

3036
fn num_nodes(&self) -> usize;
3137
fn start_node(&self) -> Self::Node;
3238
fn predecessors<'graph>(&'graph self, node: Self::Node)
3339
-> <Self as GraphPredecessors<'graph>>::Iter;
40+
// why is returning an iterator so complicated?
41+
//-> NodeVec<Self, Self::Node>;
3442
fn successors<'graph>(&'graph self, node: Self::Node)
3543
-> <Self as GraphSuccessors<'graph>>::Iter;
44+
//-> NodeVec<Self, Self::Node>;
45+
//-> std::slice::Iter<'graph, Self::Node>;
3646
}
3747

3848
pub trait GraphPredecessors<'graph> {
@@ -51,3 +61,80 @@ pub trait GraphSuccessors<'graph> {
5161
// }
5262
//}
5363

64+
//#[derive(Clone, Debug)]
65+
//pub struct NodeVec<G: Graph, T> {
66+
// pub vec: Vec<T>,
67+
// graph: PhantomData<G>,
68+
//}
69+
//
70+
//impl<G: Graph, T: Clone> NodeVec<G, T> {
71+
// pub fn from_elem(graph: &G, default: &T) -> Self {
72+
// NodeVec::from_fn(graph, |_| default.clone())
73+
// }
74+
//
75+
// pub fn from_elem_with_len(num_nodes: usize, default: &T) -> Self {
76+
// NodeVec::from_fn_with_len(num_nodes, |_| default.clone())
77+
// }
78+
//}
79+
//
80+
//impl<G: Graph, T: Default> NodeVec<G, T> {
81+
// pub fn from_default(graph: &G) -> Self {
82+
// NodeVec::from_fn(graph, |_| T::default())
83+
// }
84+
//
85+
// pub fn from_default_with_len(num_nodes: usize) -> Self {
86+
// NodeVec::from_fn_with_len(num_nodes, |_| T::default())
87+
// }
88+
//}
89+
//
90+
//impl<G: Graph, T> NodeVec<G, T> {
91+
// pub fn from_vec(v: Vec<T>) -> Self
92+
// where T: Clone
93+
// {
94+
//
95+
// NodeVec {
96+
// vec: v.clone(),
97+
// graph: PhantomData,
98+
// }
99+
// }
100+
//
101+
// pub fn from_fn<F>(graph: &G, f: F) -> Self
102+
// where F: FnMut(G::Node) -> T
103+
// {
104+
// Self::from_fn_with_len(graph.num_nodes(), f)
105+
// }
106+
//
107+
// pub fn from_fn_with_len<F>(num_nodes: usize, f: F) -> Self
108+
// where F: FnMut(G::Node) -> T
109+
// {
110+
// NodeVec {
111+
// vec: (0..num_nodes).map(G::Node::new).map(f).collect(),
112+
// graph: PhantomData,
113+
// }
114+
// }
115+
//
116+
// pub fn iter(&self) -> Iter<T> {
117+
// self.vec.iter()
118+
// }
119+
//
120+
// pub fn len(&self) -> usize {
121+
// self.vec.len()
122+
// }
123+
//}
124+
//
125+
//impl<G: Graph, T> Index<G::Node> for NodeVec<G, T> {
126+
// type Output = T;
127+
//
128+
// fn index(&self, index: G::Node) -> &T {
129+
// let index: usize = index.index();
130+
// &self.vec[index]
131+
// }
132+
//}
133+
//
134+
//impl<G: Graph, T> IndexMut<G::Node> for NodeVec<G, T> {
135+
// fn index_mut(&mut self, index: G::Node) -> &mut T {
136+
// let index: usize = index.index();
137+
// &mut self.vec[index]
138+
// }
139+
//}
140+

0 commit comments

Comments
 (0)