Skip to content

Commit

Permalink
make the basic_blocks field private
Browse files Browse the repository at this point in the history
  • Loading branch information
arielb1 authored and Ariel Ben-Yehuda committed Jun 9, 2016
1 parent bc1eb67 commit e3af9fa
Show file tree
Hide file tree
Showing 24 changed files with 203 additions and 200 deletions.
41 changes: 30 additions & 11 deletions src/librustc/mir/repr.rs
Expand Up @@ -55,7 +55,7 @@ macro_rules! newtype_index {
pub struct Mir<'tcx> {
/// List of basic blocks. References to basic block use a newtyped index type `BasicBlock`
/// that indexes into this vector.
pub basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,

/// List of visibility (lexical) scopes; these are referenced by statements
/// and used (eventually) for debuginfo. Indexed by a `VisibilityScope`.
Expand Down Expand Up @@ -94,18 +94,37 @@ pub struct Mir<'tcx> {
pub const START_BLOCK: BasicBlock = BasicBlock(0);

impl<'tcx> Mir<'tcx> {
pub fn all_basic_blocks(&self) -> Vec<BasicBlock> {
(0..self.basic_blocks.len())
.map(|i| BasicBlock::new(i))
.collect()
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
promoted: IndexVec<Promoted, Mir<'tcx>>,
return_ty: FnOutput<'tcx>,
var_decls: IndexVec<Var, VarDecl<'tcx>>,
arg_decls: IndexVec<Arg, ArgDecl<'tcx>>,
temp_decls: IndexVec<Temp, TempDecl<'tcx>>,
upvar_decls: Vec<UpvarDecl>,
span: Span) -> Self
{
Mir {
basic_blocks: basic_blocks,
visibility_scopes: visibility_scopes,
promoted: promoted,
return_ty: return_ty,
var_decls: var_decls,
arg_decls: arg_decls,
temp_decls: temp_decls,
upvar_decls: upvar_decls,
span: span
}
}

pub fn basic_block_data(&self, bb: BasicBlock) -> &BasicBlockData<'tcx> {
&self.basic_blocks[bb]
#[inline]
pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
&self.basic_blocks
}

pub fn basic_block_data_mut(&mut self, bb: BasicBlock) -> &mut BasicBlockData<'tcx> {
&mut self.basic_blocks[bb]
#[inline]
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
&mut self.basic_blocks
}
}

Expand All @@ -114,14 +133,14 @@ impl<'tcx> Index<BasicBlock> for Mir<'tcx> {

#[inline]
fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
self.basic_block_data(index)
&self.basic_blocks()[index]
}
}

impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
#[inline]
fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
self.basic_block_data_mut(index)
&mut self.basic_blocks_mut()[index]
}
}

Expand Down
22 changes: 7 additions & 15 deletions src/librustc/mir/traversal.rs
Expand Up @@ -45,7 +45,7 @@ impl<'a, 'tcx> Preorder<'a, 'tcx> {

Preorder {
mir: mir,
visited: BitVector::new(mir.basic_blocks.len()),
visited: BitVector::new(mir.basic_blocks().len()),
worklist: worklist
}
}
Expand All @@ -64,7 +64,7 @@ impl<'a, 'tcx> Iterator for Preorder<'a, 'tcx> {
continue;
}

let data = self.mir.basic_block_data(idx);
let data = &self.mir[idx];

if let Some(ref term) = data.terminator {
for &succ in term.successors().iter() {
Expand Down Expand Up @@ -107,12 +107,12 @@ impl<'a, 'tcx> Postorder<'a, 'tcx> {
pub fn new(mir: &'a Mir<'tcx>, root: BasicBlock) -> Postorder<'a, 'tcx> {
let mut po = Postorder {
mir: mir,
visited: BitVector::new(mir.basic_blocks.len()),
visited: BitVector::new(mir.basic_blocks().len()),
visit_stack: Vec::new()
};


let data = po.mir.basic_block_data(root);
let data = &po.mir[root];

if let Some(ref term) = data.terminator {
po.visited.insert(root.index());
Expand Down Expand Up @@ -186,9 +186,7 @@ impl<'a, 'tcx> Postorder<'a, 'tcx> {
};

if self.visited.insert(bb.index()) {
let data = self.mir.basic_block_data(bb);

if let Some(ref term) = data.terminator {
if let Some(ref term) = self.mir[bb].terminator {
let succs = term.successors().into_owned().into_iter();
self.visit_stack.push((bb, succs));
}
Expand All @@ -210,10 +208,7 @@ impl<'a, 'tcx> Iterator for Postorder<'a, 'tcx> {
self.traverse_successor();
}

next.map(|(bb, _)| {
let data = self.mir.basic_block_data(bb);
(bb, data)
})
next.map(|(bb, _)| (bb, &self.mir[bb]))
}
}

Expand Down Expand Up @@ -279,9 +274,6 @@ impl<'a, 'tcx> Iterator for ReversePostorder<'a, 'tcx> {
if self.idx == 0 { return None; }
self.idx -= 1;

self.blocks.get(self.idx).map(|&bb| {
let data = self.mir.basic_block_data(bb);
(bb, data)
})
self.blocks.get(self.idx).map(|&bb| (bb, &self.mir[bb]))
}
}
28 changes: 8 additions & 20 deletions src/librustc/mir/visit.rs
Expand Up @@ -252,42 +252,30 @@ macro_rules! make_mir_visitor {

fn super_mir(&mut self,
mir: & $($mutability)* Mir<'tcx>) {
let Mir {
ref $($mutability)* basic_blocks,
ref $($mutability)* visibility_scopes,
promoted: _, // Visited by passes separately.
ref $($mutability)* return_ty,
ref $($mutability)* var_decls,
ref $($mutability)* arg_decls,
ref $($mutability)* temp_decls,
upvar_decls: _,
ref $($mutability)* span,
} = *mir;

for (index, data) in basic_blocks.into_iter().enumerate() {
for index in 0..mir.basic_blocks().len() {
let block = BasicBlock::new(index);
self.visit_basic_block_data(block, data);
self.visit_basic_block_data(block, &$($mutability)* mir[block]);
}

for scope in visibility_scopes {
for scope in &$($mutability)* mir.visibility_scopes {
self.visit_visibility_scope_data(scope);
}

self.visit_fn_output(return_ty);
self.visit_fn_output(&$($mutability)* mir.return_ty);

for var_decl in var_decls {
for var_decl in &$($mutability)* mir.var_decls {
self.visit_var_decl(var_decl);
}

for arg_decl in arg_decls {
for arg_decl in &$($mutability)* mir.arg_decls {
self.visit_arg_decl(arg_decl);
}

for temp_decl in temp_decls {
for temp_decl in &$($mutability)* mir.temp_decls {
self.visit_temp_decl(temp_decl);
}

self.visit_span(span);
self.visit_span(&$($mutability)* mir.span);
}

fn super_basic_block_data(&mut self,
Expand Down
15 changes: 9 additions & 6 deletions src/librustc_borrowck/borrowck/mir/dataflow/graphviz.rs
Expand Up @@ -127,7 +127,7 @@ pub type Node = BasicBlock;
pub struct Edge { source: BasicBlock, index: usize }

fn outgoing(mir: &Mir, bb: BasicBlock) -> Vec<Edge> {
let succ_len = mir.basic_block_data(bb).terminator().successors().len();
let succ_len = mir[bb].terminator().successors().len();
(0..succ_len).map(|index| Edge { source: bb, index: index}).collect()
}

Expand Down Expand Up @@ -313,17 +313,20 @@ impl<'a, 'tcx, MWF, P> dot::GraphWalk<'a> for Graph<'a, 'tcx, MWF, P>
type Node = Node;
type Edge = Edge;
fn nodes(&self) -> dot::Nodes<Node> {
self.mbcx.mir().all_basic_blocks().into_cow()
self.mbcx.mir()
.basic_blocks()
.indices()
.collect::<Vec<_>>()
.into_cow()
}

fn edges(&self) -> dot::Edges<Edge> {
let mir = self.mbcx.mir();
let blocks = mir.all_basic_blocks();
// base initial capacity on assumption every block has at
// least one outgoing edge (Which should be true for all
// blocks but one, the exit-block).
let mut edges = Vec::with_capacity(blocks.len());
for bb in blocks {
let mut edges = Vec::with_capacity(mir.basic_blocks().len());
for bb in mir.basic_blocks().indices() {
let outgoing = outgoing(mir, bb);
edges.extend(outgoing.into_iter());
}
Expand All @@ -336,6 +339,6 @@ impl<'a, 'tcx, MWF, P> dot::GraphWalk<'a> for Graph<'a, 'tcx, MWF, P>

fn target(&self, edge: &Edge) -> Node {
let mir = self.mbcx.mir();
mir.basic_block_data(edge.source).terminator().successors()[edge.index]
mir[edge.source].terminator().successors()[edge.index]
}
}
4 changes: 2 additions & 2 deletions src/librustc_borrowck/borrowck/mir/dataflow/impls.rs
Expand Up @@ -426,7 +426,7 @@ impl<'a, 'tcx> BitDenotation for MovingOutStatements<'a, 'tcx> {
bb: repr::BasicBlock,
idx: usize) {
let (tcx, mir, move_data) = (self.tcx, self.mir, &ctxt.move_data);
let stmt = &mir.basic_block_data(bb).statements[idx];
let stmt = &mir[bb].statements[idx];
let loc_map = &move_data.loc_map;
let path_map = &move_data.path_map;
let rev_lookup = &move_data.rev_lookup;
Expand Down Expand Up @@ -466,7 +466,7 @@ impl<'a, 'tcx> BitDenotation for MovingOutStatements<'a, 'tcx> {
statements_len: usize)
{
let (mir, move_data) = (self.mir, &ctxt.move_data);
let term = mir.basic_block_data(bb).terminator.as_ref().unwrap();
let term = mir[bb].terminator();
let loc_map = &move_data.loc_map;
let loc = Location { block: bb, index: statements_len };
debug!("terminator {:?} at loc {:?} moves out of move_indexes {:?}",
Expand Down
9 changes: 4 additions & 5 deletions src/librustc_borrowck/borrowck/mir/dataflow/mod.rs
Expand Up @@ -83,11 +83,10 @@ impl<'a, 'tcx: 'a, BD> DataflowAnalysis<'a, 'tcx, BD>
self.flow_state.operator.start_block_effect(&self.ctxt, sets);
}

for bb in self.mir.all_basic_blocks() {
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
let &repr::BasicBlockData { ref statements,
ref terminator,
is_cleanup: _ } =
self.mir.basic_block_data(bb);
is_cleanup: _ } = data;

let sets = &mut self.flow_state.sets.for_block(bb.index());
for j_stmt in 0..statements.len() {
Expand All @@ -114,7 +113,7 @@ impl<'b, 'a: 'b, 'tcx: 'a, BD> PropagationContext<'b, 'a, 'tcx, BD>

fn walk_cfg(&mut self, in_out: &mut IdxSet<BD::Idx>) {
let mir = self.builder.mir;
for (bb_idx, bb_data) in mir.basic_blocks.iter().enumerate() {
for (bb_idx, bb_data) in mir.basic_blocks().iter().enumerate() {
let builder = &mut self.builder;
{
let sets = builder.flow_state.sets.for_block(bb_idx);
Expand Down Expand Up @@ -398,7 +397,7 @@ impl<'a, 'tcx: 'a, D> DataflowAnalysis<'a, 'tcx, D>
// (now rounded up to multiple of word size)
let bits_per_block = words_per_block * usize_bits;

let num_blocks = mir.basic_blocks.len();
let num_blocks = mir.basic_blocks().len();
let num_overall = num_blocks * bits_per_block;

let zeroes = Bits::new(IdxSetBuf::new_empty(num_overall));
Expand Down
10 changes: 4 additions & 6 deletions src/librustc_borrowck/borrowck/mir/dataflow/sanity_check.rs
Expand Up @@ -50,8 +50,7 @@ pub fn sanity_check_via_rustc_peek<'a, 'tcx, O>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// `dataflow::build_sets`. (But note it is doing non-standard
// stuff, so such generalization may not be realistic.)

let blocks = mir.all_basic_blocks();
'next_block: for bb in blocks {
for bb in mir.basic_blocks().indices() {
each_block(tcx, mir, flow_ctxt, results, bb);
}
}
Expand All @@ -64,10 +63,9 @@ fn each_block<'a, 'tcx, O>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
O: BitDenotation<Ctxt=MoveDataParamEnv<'tcx>, Idx=MovePathIndex>
{
let move_data = &ctxt.move_data;
let bb_data = mir.basic_block_data(bb);
let &repr::BasicBlockData { ref statements,
ref terminator,
is_cleanup: _ } = bb_data;
let repr::BasicBlockData { ref statements,
ref terminator,
is_cleanup: _ } = mir[bb];

let (args, span) = match is_rustc_peek(tcx, terminator) {
Some(args_and_span) => args_and_span,
Expand Down
14 changes: 5 additions & 9 deletions src/librustc_borrowck/borrowck/mir/elaborate_drops.rs
Expand Up @@ -225,8 +225,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {

fn collect_drop_flags(&mut self)
{
for bb in self.mir.all_basic_blocks() {
let data = self.mir.basic_block_data(bb);
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
let terminator = data.terminator();
let location = match terminator.kind {
TerminatorKind::Drop { ref location, .. } |
Expand Down Expand Up @@ -262,8 +261,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {

fn elaborate_drops(&mut self)
{
for bb in self.mir.all_basic_blocks() {
let data = self.mir.basic_block_data(bb);
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
let loc = Location { block: bb, index: data.statements.len() };
let terminator = data.terminator();

Expand Down Expand Up @@ -323,7 +321,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
unwind: Option<BasicBlock>)
{
let bb = loc.block;
let data = self.mir.basic_block_data(bb);
let data = &self.mir[bb];
let terminator = data.terminator();

let assign = Statement {
Expand Down Expand Up @@ -942,8 +940,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
}

fn drop_flags_for_fn_rets(&mut self) {
for bb in self.mir.all_basic_blocks() {
let data = self.mir.basic_block_data(bb);
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
if let TerminatorKind::Call {
destination: Some((ref lv, tgt)), cleanup: Some(_), ..
} = data.terminator().kind {
Expand Down Expand Up @@ -975,8 +972,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
// drop flags by themselves, to avoid the drop flags being
// clobbered before they are read.

for bb in self.mir.all_basic_blocks() {
let data = self.mir.basic_block_data(bb);
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
debug!("drop_flags_for_locs({:?})", data);
for i in 0..(data.statements.len()+1) {
debug!("drop_flag_for_locs: stmt {}", i);
Expand Down
15 changes: 7 additions & 8 deletions src/librustc_borrowck/borrowck/mir/gather_moves.rs
Expand Up @@ -519,9 +519,9 @@ enum StmtKind {
fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> MoveData<'tcx> {
use self::StmtKind as SK;

let bbs = mir.all_basic_blocks();
let mut moves = Vec::with_capacity(bbs.len());
let mut loc_map: Vec<_> = iter::repeat(Vec::new()).take(bbs.len()).collect();
let bb_count = mir.basic_blocks().len();
let mut moves = vec![];
let mut loc_map: Vec<_> = iter::repeat(Vec::new()).take(bb_count).collect();
let mut path_map = Vec::new();

// this is mutable only because we will move it to and fro' the
Expand All @@ -541,22 +541,21 @@ fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> MoveD
assert!(mir.var_decls.len() <= ::std::u32::MAX as usize);
assert!(mir.arg_decls.len() <= ::std::u32::MAX as usize);
assert!(mir.temp_decls.len() <= ::std::u32::MAX as usize);
for (var, _) in mir.var_decls.iter_enumerated() {
for var in mir.var_decls.indices() {
let path_idx = builder.move_path_for(&Lvalue::Var(var));
path_map.fill_to(path_idx.index());
}
for (arg, _) in mir.arg_decls.iter_enumerated() {
for arg in mir.arg_decls.indices() {
let path_idx = builder.move_path_for(&Lvalue::Arg(arg));
path_map.fill_to(path_idx.index());
}
for (temp, _) in mir.temp_decls.iter_enumerated() {
for temp in mir.temp_decls.indices() {
let path_idx = builder.move_path_for(&Lvalue::Temp(temp));
path_map.fill_to(path_idx.index());
}

for bb in bbs {
for (bb, bb_data) in mir.basic_blocks().iter_enumerated() {
let loc_map_bb = &mut loc_map[bb.index()];
let bb_data = mir.basic_block_data(bb);

debug_assert!(loc_map_bb.len() == 0);
let len = bb_data.statements.len();
Expand Down

0 comments on commit e3af9fa

Please sign in to comment.