Skip to content

Commit 1cc95c9

Browse files
committed
simple impl of graph for mir
1 parent 9640675 commit 1cc95c9

File tree

3 files changed

+95
-30
lines changed

3 files changed

+95
-30
lines changed

src/librustc/mir/cache.rs

Lines changed: 0 additions & 14 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<Mir<'tcx>>>>,
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,16 +52,6 @@ impl Cache {
5652

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

7157
fn calculate_predecessors(mir: &Mir) -> IndexVec<BasicBlock, Vec<BasicBlock>> {

src/librustc/mir/repr.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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;
15+
use rustc_data_structures::graph_algorithms::dominators::{Dominators, dominators};
1616
use rustc_data_structures::graph_algorithms::{Graph, GraphPredecessors, GraphSuccessors};
1717
use hir::def_id::DefId;
1818
use ty::subst::Substs;
@@ -151,7 +151,8 @@ impl<'tcx> Mir<'tcx> {
151151
#[inline]
152152
//pub fn dominators(&'tcx self) -> Ref<Dominators<Self>> {
153153
pub fn dominators(&self) -> Dominators<Self> {
154-
self.cache.dominators(self)
154+
//self.cache.dominators(self)
155+
dominators(self)
155156
}
156157
}
157158

@@ -1188,30 +1189,21 @@ impl<'tcx> Graph for Mir<'tcx> {
11881189
fn predecessors<'graph>(&'graph self, node: Self::Node)
11891190
-> <Self as GraphPredecessors<'graph>>::Iter
11901191
{
1191-
//self.predecessors_for(node).iter().cloned()
1192-
panic!("not implemented!");
1192+
self.predecessors_for(node).clone().into_iter()
11931193
}
11941194
fn successors<'graph>(&'graph self, node: Self::Node)
1195-
-> <Self as GraphSuccessors<'graph>>::Iter
1195+
-> <Self as GraphSuccessors<'graph>>::Iter
11961196
{
1197-
//match self.basic_blocks[node].terminator {
1198-
// Some(term) => {
1199-
// term.successors().iter().cloned()
1200-
// },
1201-
// _ => vec![].iter().clone()
1202-
//}
1203-
panic!("not implemented!");
1197+
self.basic_blocks[node].terminator().successors().into_owned().into_iter()
12041198
}
12051199
}
12061200

12071201
impl<'a, 'b> GraphPredecessors<'b> for Mir<'a> {
12081202
type Item = BasicBlock;
1209-
//type Iter = std::slice::Iter<'b, BasicBlock>;
1210-
type Iter = iter::Cloned<std::slice::Iter<'b, BasicBlock>>;
1203+
type Iter = std::vec::IntoIter<BasicBlock>;
12111204
}
12121205

12131206
impl<'a, 'b> GraphSuccessors<'b> for Mir<'a> {
12141207
type Item = BasicBlock;
1215-
type Iter = iter::Cloned<std::slice::Iter<'b, BasicBlock>>;
1216-
//type Iter = std::slice::Iter<'b, BasicBlock>;
1208+
type Iter = std::vec::IntoIter<BasicBlock>;
12171209
}

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)