Skip to content

Commit 0519b68

Browse files
committed
Revert "try to merge mir_cfg into Mir"
This reverts commit 9640675.
1 parent 9640675 commit 0519b68

File tree

3 files changed

+82
-128
lines changed

3 files changed

+82
-128
lines changed

src/librustc/mir/cache.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use std::cell::{Ref, RefCell};
1212
use rustc_data_structures::indexed_vec::IndexVec;
1313
use rustc_data_structures::graph_algorithms::dominators::{Dominators, dominators};
14-
//use mir::mir_cfg::MirCfg;
14+
use mir::mir_cfg::MirCfg;
1515

1616
use mir::repr::{Mir, BasicBlock};
1717

@@ -20,17 +20,17 @@ use rustc_serialize as serialize;
2020
#[derive(Clone)]
2121
pub struct Cache {
2222
predecessors: RefCell<Option<IndexVec<BasicBlock, Vec<BasicBlock>>>>,
23-
//dominators: RefCell<Option<Dominators<Mir<'tcx>>>>,
23+
dominators: RefCell<Option<Dominators<MirCfg>>>,
2424
}
2525

2626

27-
impl<'a> serialize::Encodable for Cache {
27+
impl serialize::Encodable for Cache {
2828
fn encode<S: serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
2929
serialize::Encodable::encode(&(), s)
3030
}
3131
}
3232

33-
impl<'a> serialize::Decodable for Cache {
33+
impl serialize::Decodable for Cache {
3434
fn decode<D: serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
3535
serialize::Decodable::decode(d).map(|_v: ()| Self::new())
3636
}
@@ -40,7 +40,7 @@ impl Cache {
4040
pub fn new() -> Self {
4141
Cache {
4242
predecessors: RefCell::new(None),
43-
//dominators: RefCell::new(None),
43+
dominators: RefCell::new(None),
4444
}
4545
}
4646

@@ -57,14 +57,12 @@ impl Cache {
5757
Ref::map(self.predecessors.borrow(), |p| p.as_ref().unwrap())
5858
}
5959

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-
//}
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+
}
6564

66-
//Ref::map(self.dominators.borrow(), |p| p.as_ref().unwrap())
67-
dominators(mir)
65+
Ref::map(self.dominators.borrow(), |p| p.as_ref().unwrap())
6866
}
6967
}
7068

@@ -81,3 +79,7 @@ fn calculate_predecessors(mir: &Mir) -> IndexVec<BasicBlock, Vec<BasicBlock>> {
8179
result
8280
}
8381

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

src/librustc/mir/mir_cfg.rs

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

11-
//use std::{iter, slice};
12-
//use rustc_data_structures::indexed_vec::IndexVec;
13-
//use rustc_data_structures::graph_algorithms::{Graph, GraphPredecessors, GraphSuccessors};
14-
//use mir::repr::{Mir, BasicBlock, START_BLOCK};
15-
//use mir::cache::Cache;
11+
use std::{iter, slice};
12+
use rustc_data_structures::indexed_vec::IndexVec;
13+
use rustc_data_structures::graph_algorithms::{Graph, GraphPredecessors, GraphSuccessors};
14+
use mir::repr::{Mir, BasicBlock, START_BLOCK};
15+
use mir::cache::Cache;
1616

17-
//impl MirCfg {
18-
// pub fn new<'a, 'tcx>(mir: &Mir, cache: &Cache) -> Self {
19-
// MirCfg {
20-
// predecessors: cache.predecessors(mir).clone(),
21-
// successors: calculate_successors(mir),
22-
// n_nodes: mir.basic_blocks().len(),
23-
// start_node: START_BLOCK,
24-
// }
25-
// }
26-
//}
27-
//
28-
//#[derive(Clone, Debug)]
29-
//pub struct MirCfg {
30-
// predecessors: IndexVec<BasicBlock,Vec<BasicBlock>>,
31-
// successors: IndexVec<BasicBlock,Vec<BasicBlock>>,
32-
// start_node: BasicBlock,
33-
// n_nodes: usize,
34-
//}
35-
//
36-
//impl Graph for MirCfg {
37-
//
38-
// type Node = BasicBlock;
39-
//
40-
// fn num_nodes(&self) -> usize { self.n_nodes }
41-
//
42-
// fn start_node(&self) -> Self::Node { self.start_node }
43-
//
44-
// fn predecessors<'graph>(&'graph self, node: Self::Node)
45-
// -> <Self as GraphPredecessors<'graph>>::Iter
46-
// {
47-
// self.predecessors[node].iter().cloned()
48-
// }
49-
// fn successors<'graph>(&'graph self, node: Self::Node)
50-
// -> <Self as GraphSuccessors<'graph>>::Iter
51-
// {
52-
// self.successors[node].iter().cloned()
53-
// }
54-
//}
55-
//
56-
//impl<'g> GraphPredecessors<'g> for MirCfg {
57-
// type Item = BasicBlock;
58-
// type Iter = iter::Cloned<slice::Iter<'g, BasicBlock>>;
59-
//}
60-
//
61-
//impl<'g> GraphSuccessors<'g> for MirCfg {
62-
// type Item = BasicBlock;
63-
// type Iter = iter::Cloned<slice::Iter<'g, BasicBlock>>;
64-
//}
65-
//
66-
//fn calculate_successors<'a, 'tcx>(mir: &'a Mir<'tcx>) -> IndexVec<BasicBlock, Vec<BasicBlock>> {
67-
// let mut successors = IndexVec::from_elem(vec![], mir.basic_blocks());
68-
// for (bb, data) in mir.basic_blocks().iter_enumerated() {
69-
// if let Some(ref term) = data.terminator {
70-
// successors[bb].append(term.successors().to_mut());
71-
// }
72-
// }
73-
// for ss in successors.iter_mut() {
74-
// ss.sort();
75-
// ss.dedup();
76-
// }
77-
// successors
78-
//}
17+
impl MirCfg {
18+
pub fn new<'a, 'tcx>(mir: &Mir, cache: &Cache) -> Self {
19+
MirCfg {
20+
predecessors: cache.predecessors(mir).clone(),
21+
successors: calculate_successors(mir),
22+
n_nodes: mir.basic_blocks().len(),
23+
start_node: START_BLOCK,
24+
}
25+
}
26+
}
27+
28+
#[derive(Clone, Debug)]
29+
pub struct MirCfg {
30+
predecessors: IndexVec<BasicBlock,Vec<BasicBlock>>,
31+
successors: IndexVec<BasicBlock,Vec<BasicBlock>>,
32+
start_node: BasicBlock,
33+
n_nodes: usize,
34+
}
35+
36+
impl Graph for MirCfg {
37+
38+
type Node = BasicBlock;
39+
40+
fn num_nodes(&self) -> usize { self.n_nodes }
41+
42+
fn start_node(&self) -> Self::Node { self.start_node }
43+
44+
fn predecessors<'graph>(&'graph self, node: Self::Node)
45+
-> <Self as GraphPredecessors<'graph>>::Iter
46+
{
47+
self.predecessors[node].iter().cloned()
48+
}
49+
fn successors<'graph>(&'graph self, node: Self::Node)
50+
-> <Self as GraphSuccessors<'graph>>::Iter
51+
{
52+
self.successors[node].iter().cloned()
53+
}
54+
}
55+
56+
impl<'g> GraphPredecessors<'g> for MirCfg {
57+
type Item = BasicBlock;
58+
type Iter = iter::Cloned<slice::Iter<'g, BasicBlock>>;
59+
}
60+
61+
impl<'g> GraphSuccessors<'g> for MirCfg {
62+
type Item = BasicBlock;
63+
type Iter = iter::Cloned<slice::Iter<'g, BasicBlock>>;
64+
}
65+
66+
fn calculate_successors<'a, 'tcx>(mir: &'a Mir<'tcx>) -> IndexVec<BasicBlock, Vec<BasicBlock>> {
67+
let mut successors = IndexVec::from_elem(vec![], mir.basic_blocks());
68+
for (bb, data) in mir.basic_blocks().iter_enumerated() {
69+
if let Some(ref term) = data.terminator {
70+
successors[bb].append(term.successors().to_mut());
71+
}
72+
}
73+
for ss in successors.iter_mut() {
74+
ss.sort();
75+
ss.dedup();
76+
}
77+
successors
78+
}

src/librustc/mir/repr.rs

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ 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;
16-
use rustc_data_structures::graph_algorithms::{Graph, GraphPredecessors, GraphSuccessors};
1715
use hir::def_id::DefId;
1816
use ty::subst::Substs;
1917
use ty::{self, AdtDef, ClosureSubsts, FnOutput, Region, Ty};
@@ -28,7 +26,6 @@ use std::{iter, u32};
2826
use std::ops::{Index, IndexMut};
2927
use syntax::ast::{self, Name};
3028
use syntax::codemap::Span;
31-
use std;
3229

3330
use super::cache::Cache;
3431

@@ -96,7 +93,7 @@ pub struct Mir<'tcx> {
9693
pub span: Span,
9794

9895
/// A cache for various calculations
99-
cache: Cache,
96+
cache: Cache
10097
}
10198

10299
/// where execution begins
@@ -147,12 +144,6 @@ impl<'tcx> Mir<'tcx> {
147144
pub fn predecessors_for(&self, bb: BasicBlock) -> Ref<Vec<BasicBlock>> {
148145
Ref::map(self.predecessors(), |p| &p[bb])
149146
}
150-
151-
#[inline]
152-
//pub fn dominators(&'tcx self) -> Ref<Dominators<Self>> {
153-
pub fn dominators(&self) -> Dominators<Self> {
154-
self.cache.dominators(self)
155-
}
156147
}
157148

158149
impl<'tcx> Index<BasicBlock> for Mir<'tcx> {
@@ -1176,42 +1167,3 @@ fn node_to_string(node_id: ast::NodeId) -> String {
11761167
fn item_path_str(def_id: DefId) -> String {
11771168
ty::tls::with(|tcx| tcx.item_path_str(def_id))
11781169
}
1179-
1180-
impl<'tcx> Graph for Mir<'tcx> {
1181-
1182-
type Node = BasicBlock;
1183-
1184-
fn num_nodes(&self) -> usize { self.basic_blocks.len() }
1185-
1186-
fn start_node(&self) -> Self::Node { START_BLOCK }
1187-
1188-
fn predecessors<'graph>(&'graph self, node: Self::Node)
1189-
-> <Self as GraphPredecessors<'graph>>::Iter
1190-
{
1191-
//self.predecessors_for(node).iter().cloned()
1192-
panic!("not implemented!");
1193-
}
1194-
fn successors<'graph>(&'graph self, node: Self::Node)
1195-
-> <Self as GraphSuccessors<'graph>>::Iter
1196-
{
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!");
1204-
}
1205-
}
1206-
1207-
impl<'a, 'b> GraphPredecessors<'b> for Mir<'a> {
1208-
type Item = BasicBlock;
1209-
//type Iter = std::slice::Iter<'b, BasicBlock>;
1210-
type Iter = iter::Cloned<std::slice::Iter<'b, BasicBlock>>;
1211-
}
1212-
1213-
impl<'a, 'b> GraphSuccessors<'b> for Mir<'a> {
1214-
type Item = BasicBlock;
1215-
type Iter = iter::Cloned<std::slice::Iter<'b, BasicBlock>>;
1216-
//type Iter = std::slice::Iter<'b, BasicBlock>;
1217-
}

0 commit comments

Comments
 (0)