Skip to content

Commit

Permalink
MIR: Refactor mir::Terminator to use tuples instead of a fixed-size a…
Browse files Browse the repository at this point in the history
…rrays.
  • Loading branch information
michaelwoerister committed Dec 10, 2015
1 parent d62de4c commit 33d2970
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 25 deletions.
13 changes: 7 additions & 6 deletions src/librustc/mir/repr.rs
Expand Up @@ -13,6 +13,7 @@ use middle::def_id::DefId;
use middle::subst::Substs;
use middle::ty::{AdtDef, ClosureSubsts, FnOutput, Region, Ty};
use rustc_back::slice;
use rustc_data_structures::tuple_slice::TupleSlice;
use rustc_front::hir::InlineAsm;
use syntax::ast::Name;
use syntax::codemap::Span;
Expand Down Expand Up @@ -206,7 +207,7 @@ pub enum Terminator<'tcx> {
/// jump to branch 0 if this lvalue evaluates to true
If {
cond: Operand<'tcx>,
targets: [BasicBlock; 2],
targets: (BasicBlock, BasicBlock),
},

/// lvalue evaluates to some enum; jump depending on the branch
Expand Down Expand Up @@ -254,7 +255,7 @@ pub enum Terminator<'tcx> {
/// unwinding.
Call {
data: CallData<'tcx>,
targets: [BasicBlock; 2],
targets: (BasicBlock, BasicBlock),
},
}

Expand All @@ -264,12 +265,12 @@ impl<'tcx> Terminator<'tcx> {
match *self {
Goto { target: ref b } => slice::ref_slice(b),
Panic { target: ref b } => slice::ref_slice(b),
If { cond: _, targets: ref b } => b,
If { cond: _, targets: ref b } => b.as_slice(),
Switch { targets: ref b, .. } => b,
SwitchInt { targets: ref b, .. } => b,
Diverge => &[],
Return => &[],
Call { data: _, targets: ref b } => b,
Call { data: _, targets: ref b } => b.as_slice(),
}
}

Expand All @@ -278,12 +279,12 @@ impl<'tcx> Terminator<'tcx> {
match *self {
Goto { target: ref mut b } => slice::mut_ref_slice(b),
Panic { target: ref mut b } => slice::mut_ref_slice(b),
If { cond: _, targets: ref mut b } => b,
If { cond: _, targets: ref mut b } => b.as_mut_slice(),
Switch { targets: ref mut b, .. } => b,
SwitchInt { targets: ref mut b, .. } => b,
Diverge => &mut [],
Return => &mut [],
Call { data: _, targets: ref mut b } => b,
Call { data: _, targets: ref mut b } => b.as_mut_slice(),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/mir/visit.rs
Expand Up @@ -10,6 +10,7 @@

use middle::ty::Region;
use mir::repr::*;
use rustc_data_structures::tuple_slice::TupleSlice;

pub trait Visitor<'tcx> {
// Override these, and call `self.super_xxx` to revert back to the
Expand Down Expand Up @@ -97,7 +98,7 @@ pub trait Visitor<'tcx> {

Terminator::If { ref cond, ref targets } => {
self.visit_operand(cond);
for &target in &targets[..] {
for &target in targets.as_slice() {
self.visit_branch(block, target);
}
}
Expand Down Expand Up @@ -126,7 +127,7 @@ pub trait Visitor<'tcx> {
for arg in &data.args {
self.visit_operand(arg);
}
for &target in &targets[..] {
for &target in targets.as_slice() {
self.visit_branch(block, target);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/expr/as_lvalue.rs
Expand Up @@ -69,7 +69,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
this.cfg.terminate(block,
Terminator::If {
cond: Operand::Consume(lt),
targets: [success, failure],
targets: (success, failure),
});
this.panic(failure);
success.and(slice.index(idx))
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_mir/build/expr/into.rs
Expand Up @@ -53,7 +53,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
let mut else_block = this.cfg.start_new_block();
this.cfg.terminate(block, Terminator::If {
cond: operand,
targets: [then_block, else_block]
targets: (then_block, else_block)
});

unpack!(then_block = this.into(destination, then_block, then_expr));
Expand Down Expand Up @@ -84,15 +84,15 @@ impl<'a,'tcx> Builder<'a,'tcx> {

let lhs = unpack!(block = this.as_operand(block, lhs));
let blocks = match op {
LogicalOp::And => [else_block, false_block],
LogicalOp::Or => [true_block, else_block],
LogicalOp::And => (else_block, false_block),
LogicalOp::Or => (true_block, else_block),
};
this.cfg.terminate(block, Terminator::If { cond: lhs, targets: blocks });

let rhs = unpack!(else_block = this.as_operand(else_block, rhs));
this.cfg.terminate(else_block, Terminator::If {
cond: rhs,
targets: [true_block, false_block]
targets: (true_block, false_block)
});

this.cfg.push_assign_constant(
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
this.cfg.terminate(loop_block_end,
Terminator::If {
cond: cond,
targets: [body_block, exit_block]
targets: (body_block, exit_block)
});
} else {
body_block = loop_block;
Expand Down Expand Up @@ -225,7 +225,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
func: fun,
args: args,
},
targets: [success, panic],
targets: (success, panic),
});
success.unit()
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/matches/mod.rs
Expand Up @@ -555,7 +555,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
let cond = unpack!(block = self.as_operand(block, guard));
let otherwise = self.cfg.start_new_block();
self.cfg.terminate(block, Terminator::If { cond: cond,
targets: [arm_block, otherwise]});
targets: (arm_block, otherwise)});
Some(otherwise)
} else {
self.cfg.terminate(block, Terminator::Goto { target: arm_block });
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_mir/build/matches/test.rs
Expand Up @@ -232,7 +232,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
self.cfg.start_new_block()];
self.cfg.terminate(block, Terminator::If {
cond: Operand::Consume(result),
targets: [target_blocks[0], target_blocks[1]]
targets: (target_blocks[0], target_blocks[1])
});

target_blocks
Expand All @@ -252,7 +252,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
let bool_ty = self.hir.bool_ty();
let eq_result = self.temp(bool_ty);
let func = self.item_ref_operand(span, item_ref);
let call_blocks = [self.cfg.start_new_block(), self.diverge_cleanup()];
let call_blocks = (self.cfg.start_new_block(), self.diverge_cleanup());
self.cfg.terminate(block,
Terminator::Call {
data: CallData {
Expand All @@ -264,10 +264,10 @@ impl<'a,'tcx> Builder<'a,'tcx> {
});

// check the result
self.cfg.terminate(call_blocks[0],
self.cfg.terminate(call_blocks.0,
Terminator::If {
cond: Operand::Consume(eq_result),
targets: [target_blocks[0], target_blocks[1]],
targets: (target_blocks[0], target_blocks[1]),
});

target_blocks
Expand Down
11 changes: 7 additions & 4 deletions src/librustc_mir/transform/simplify_cfg.rs
Expand Up @@ -96,18 +96,21 @@ impl SimplifyCfg {
mem::swap(&mut terminator, &mut mir.basic_block_data_mut(bb).terminator);

mir.basic_block_data_mut(bb).terminator = match terminator {
Terminator::If { ref targets, .. } if targets[0] == targets[1] => {
Terminator::If { ref targets, .. } if targets.0 == targets.1 => {
changed = true;
Terminator::Goto { target: targets[0] }
Terminator::Goto { target: targets.0 }
}
Terminator::If { ref targets, cond: Operand::Constant(Constant {
literal: Literal::Value {
value: ConstVal::Bool(cond)
}, ..
}) } => {
changed = true;
let target_idx = if cond { 0 } else { 1 };
Terminator::Goto { target: targets[target_idx] }
if cond {
Terminator::Goto { target: targets.0 }
} else {
Terminator::Goto { target: targets.1 }
}
}
Terminator::SwitchInt { ref targets, .. } if targets.len() == 1 => {
Terminator::Goto { target: targets[0] }
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/mir/block.rs
Expand Up @@ -39,7 +39,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
unimplemented!()
}

mir::Terminator::If { ref cond, targets: [true_bb, false_bb] } => {
mir::Terminator::If { ref cond, targets: (true_bb, false_bb) } => {
let cond = self.trans_operand(bcx, cond);
let lltrue = self.llblock(true_bb);
let llfalse = self.llblock(false_bb);
Expand Down

0 comments on commit 33d2970

Please sign in to comment.