Skip to content

Commit

Permalink
Switch TerminatorKind::Call to StatementKind.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Mar 28, 2017
1 parent 49b486b commit ff12d78
Show file tree
Hide file tree
Showing 25 changed files with 655 additions and 620 deletions.
126 changes: 63 additions & 63 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,18 +515,6 @@ pub enum TerminatorKind<'tcx> {
target: Block,
unwind: Option<Block>,
},

/// Block ends with a call of a converging function
Call {
/// The function that’s being called
func: Operand<'tcx>,
/// Arguments the function is called with
args: Vec<Operand<'tcx>>,
/// Destination for the return value. If some, the call is converging.
destination: Option<(Lvalue<'tcx>, Block)>,
/// Cleanups to be done if the call unwinds.
cleanup: Option<Block>
},
}

impl<'tcx> Terminator<'tcx> {
Expand Down Expand Up @@ -559,11 +547,6 @@ impl<'tcx> TerminatorKind<'tcx> {
Resume => (&[]).into_cow(),
Return => (&[]).into_cow(),
Unreachable => (&[]).into_cow(),
Call { destination: Some((_, t)), cleanup: Some(c), .. } => vec![t, c].into_cow(),
Call { destination: Some((_, ref t)), cleanup: None, .. } =>
slice::ref_slice(t).into_cow(),
Call { destination: None, cleanup: Some(ref c), .. } => slice::ref_slice(c).into_cow(),
Call { destination: None, cleanup: None, .. } => (&[]).into_cow(),
DropAndReplace { target, unwind: Some(unwind), .. } |
Drop { target, unwind: Some(unwind), .. } => {
vec![target, unwind].into_cow()
Expand All @@ -585,10 +568,6 @@ impl<'tcx> TerminatorKind<'tcx> {
Resume => Vec::new(),
Return => Vec::new(),
Unreachable => Vec::new(),
Call { destination: Some((_, ref mut t)), cleanup: Some(ref mut c), .. } => vec![t, c],
Call { destination: Some((_, ref mut t)), cleanup: None, .. } => vec![t],
Call { destination: None, cleanup: Some(ref mut c), .. } => vec![c],
Call { destination: None, cleanup: None, .. } => vec![],
DropAndReplace { ref mut target, unwind: Some(ref mut unwind), .. } |
Drop { ref mut target, unwind: Some(ref mut unwind), .. } => vec![target, unwind],
DropAndReplace { ref mut target, unwind: None, .. } |
Expand Down Expand Up @@ -663,19 +642,6 @@ impl<'tcx> TerminatorKind<'tcx> {
Drop { ref location, .. } => write!(fmt, "drop({:?})", location),
DropAndReplace { ref location, ref value, .. } =>
write!(fmt, "replace({:?} <- {:?})", location, value),
Call { ref func, ref args, ref destination, .. } => {
if let Some((ref destination, _)) = *destination {
write!(fmt, "{:?} = ", destination)?;
}
write!(fmt, "{:?}(", func)?;
for (index, arg) in args.iter().enumerate() {
if index > 0 {
write!(fmt, ", ")?;
}
write!(fmt, "{:?}", arg)?;
}
write!(fmt, ")")
}
}
}

Expand All @@ -695,11 +661,6 @@ impl<'tcx> TerminatorKind<'tcx> {
.chain(iter::once(String::from("otherwise").into()))
.collect()
}
Call { destination: Some(_), cleanup: Some(_), .. } =>
vec!["return".into_cow(), "unwind".into_cow()],
Call { destination: Some(_), cleanup: None, .. } => vec!["return".into_cow()],
Call { destination: None, cleanup: Some(_), .. } => vec!["unwind".into_cow()],
Call { destination: None, cleanup: None, .. } => vec![],
DropAndReplace { unwind: None, .. } |
Drop { unwind: None, .. } => vec!["return".into_cow()],
DropAndReplace { unwind: Some(_), .. } |
Expand Down Expand Up @@ -737,19 +698,39 @@ impl<'tcx> Statement<'tcx> {

pub fn cleanup_target(&self) -> Option<Block> {
match self.kind {
StatementKind::Assert { cleanup: Some(unwind), .. } => {
Some(unwind)
StatementKind::Assign(..) |
StatementKind::SetDiscriminant { .. } |
StatementKind::StorageLive(..) |
StatementKind::StorageDead(..) |
StatementKind::InlineAsm { .. } |
StatementKind::Nop => None,
StatementKind::Assert { cleanup: unwind, .. } |
StatementKind::Call { cleanup: unwind, .. } => {
if let Some(unwind) = unwind {
Some(unwind)
} else {
None
}
}
_ => None
}
}

pub fn cleanup_target_mut(&mut self) -> Option<&mut Block> {
match self.kind {
StatementKind::Assert { cleanup: Some(ref mut unwind), .. } => {
Some(unwind)
StatementKind::Assign(..) |
StatementKind::SetDiscriminant { .. } |
StatementKind::StorageLive(..) |
StatementKind::StorageDead(..) |
StatementKind::InlineAsm { .. } |
StatementKind::Nop => None,
StatementKind::Assert { cleanup: ref mut unwind, .. } |
StatementKind::Call { cleanup: ref mut unwind, .. } => {
if let Some(ref mut unwind) = *unwind {
Some(unwind)
} else {
None
}
}
_ => None
}
}
}
Expand Down Expand Up @@ -783,6 +764,18 @@ pub enum StatementKind<'tcx> {
cleanup: Option<Block>
},

/// Block ends with a call of a converging function
Call {
/// The function that’s being called
func: Operand<'tcx>,
/// Arguments the function is called with
args: Vec<Operand<'tcx>>,
/// Destination for the return value.
destination: Lvalue<'tcx>,
/// Cleanups to be done if the call unwinds.
cleanup: Option<Block>
},

/// No-op. Useful for deleting instructions without affecting statement indices.
Nop,
}
Expand Down Expand Up @@ -820,6 +813,17 @@ impl<'tcx> Debug for Statement<'tcx> {

write!(fmt, ")")
},
Call { ref func, ref args, ref destination, .. } => {
write!(fmt, "{:?} = ", destination)?;
write!(fmt, "{:?}(", func)?;
for (index, arg) in args.iter().enumerate() {
if index > 0 {
write!(fmt, ", ")?;
}
write!(fmt, "{:?}", arg)?;
}
write!(fmt, ")")
},
Nop => write!(fmt, "nop"),
}
}
Expand Down Expand Up @@ -1459,6 +1463,16 @@ impl<'tcx> TypeFoldable<'tcx> for Statement<'tcx> {
cleanup: cleanup
}
},
Call { ref func, ref args, ref destination, cleanup } => {
let dest = destination.fold_with(folder);

Call {
func: func.fold_with(folder),
args: args.fold_with(folder),
destination: dest,
cleanup: cleanup
}
},
Nop => Nop,
};
Statement {
Expand Down Expand Up @@ -1488,6 +1502,10 @@ impl<'tcx> TypeFoldable<'tcx> for Statement<'tcx> {
false
}
},
Call { ref func, ref args, ref destination, .. } => {
destination.visit_with(visitor) || func.visit_with(visitor) ||
args.visit_with(visitor)
},
Nop => false,
}
}
Expand Down Expand Up @@ -1516,18 +1534,6 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
target: target,
unwind: unwind
},
Call { ref func, ref args, ref destination, cleanup } => {
let dest = destination.as_ref().map(|&(ref loc, dest)| {
(loc.fold_with(folder), dest)
});

Call {
func: func.fold_with(folder),
args: args.fold_with(folder),
destination: dest,
cleanup: cleanup
}
},
Resume => Resume,
Return => Return,
Unreachable => Unreachable,
Expand All @@ -1547,12 +1553,6 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
Drop { ref location, ..} => location.visit_with(visitor),
DropAndReplace { ref location, ref value, ..} =>
location.visit_with(visitor) || value.visit_with(visitor),
Call { ref func, ref args, ref destination, .. } => {
let dest = if let Some((ref loc, _)) = *destination {
loc.visit_with(visitor)
} else { false };
dest || func.visit_with(visitor) || args.visit_with(visitor)
},
Goto { .. } |
Resume |
Return |
Expand Down
26 changes: 11 additions & 15 deletions src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,17 @@ macro_rules! make_mir_visitor {
self.visit_assert_message(msg, location);
cleanup.map(|t| self.visit_branch(block, t));
}
StatementKind::Call { ref $($mutability)* func,
ref $($mutability)* args,
ref $($mutability)* destination,
cleanup } => {
self.visit_operand(func, location);
for arg in args {
self.visit_operand(arg, location);
}
self.visit_lvalue(destination, LvalueContext::Call, location);
cleanup.map(|t| self.visit_branch(block, t));
}
StatementKind::Nop => {}
}
}
Expand Down Expand Up @@ -423,21 +434,6 @@ macro_rules! make_mir_visitor {
self.visit_branch(block, target);
unwind.map(|t| self.visit_branch(block, t));
}

TerminatorKind::Call { ref $($mutability)* func,
ref $($mutability)* args,
ref $($mutability)* destination,
cleanup } => {
self.visit_operand(func, source_location);
for arg in args {
self.visit_operand(arg, source_location);
}
if let Some((ref $($mutability)* destination, target)) = *destination {
self.visit_lvalue(destination, LvalueContext::Call, source_location);
self.visit_branch(block, target);
}
cleanup.map(|t| self.visit_branch(block, t));
}
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/librustc_borrowck/borrowck/mir/dataflow/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ impl<'a, 'tcx> BitDenotation for MaybeInitializedLvals<'a, 'tcx> {
fn propagate_call_return(&self,
in_out: &mut IdxSet<MovePathIndex>,
_call_bb: mir::Block,
_dest_bb: mir::Block,
dest_lval: &mir::Lvalue) {
// when a call returns successfully, that means we need to set
// the bits for that dest_lval to 1 (initialized).
Expand Down Expand Up @@ -357,7 +356,6 @@ impl<'a, 'tcx> BitDenotation for MaybeUninitializedLvals<'a, 'tcx> {
fn propagate_call_return(&self,
in_out: &mut IdxSet<MovePathIndex>,
_call_bb: mir::Block,
_dest_bb: mir::Block,
dest_lval: &mir::Lvalue) {
// when a call returns successfully, that means we need to set
// the bits for that dest_lval to 0 (initialized).
Expand Down Expand Up @@ -413,7 +411,6 @@ impl<'a, 'tcx> BitDenotation for DefinitelyInitializedLvals<'a, 'tcx> {
fn propagate_call_return(&self,
in_out: &mut IdxSet<MovePathIndex>,
_call_bb: mir::Block,
_dest_bb: mir::Block,
dest_lval: &mir::Lvalue) {
// when a call returns successfully, that means we need to set
// the bits for that dest_lval to 1 (initialized).
Expand Down Expand Up @@ -475,6 +472,7 @@ impl<'a, 'tcx> BitDenotation for MovingOutStatements<'a, 'tcx> {
mir::StatementKind::StorageDead(_) |
mir::StatementKind::InlineAsm { .. } |
mir::StatementKind::Assert { .. } |
mir::StatementKind::Call { .. } |
mir::StatementKind::Nop => {}
}
}
Expand All @@ -500,7 +498,6 @@ impl<'a, 'tcx> BitDenotation for MovingOutStatements<'a, 'tcx> {
fn propagate_call_return(&self,
in_out: &mut IdxSet<MoveOutIndex>,
_call_bb: mir::Block,
_dest_bb: mir::Block,
dest_lval: &mir::Lvalue) {
let move_data = self.move_data();
let bits_per_block = self.bits_per_block();
Expand Down
25 changes: 16 additions & 9 deletions src/librustc_borrowck/borrowck/mir/dataflow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ pub trait BitDenotation {
fn propagate_call_return(&self,
in_out: &mut IdxSet<Self::Idx>,
call_bb: mir::Block,
dest_bb: mir::Block,
dest_lval: &mir::Lvalue);
}

Expand Down Expand Up @@ -457,16 +456,24 @@ impl<'a, 'tcx: 'a, D> DataflowAnalysis<'a, 'tcx, D>
self.propagate_bits_into_entry_set_for(in_out, changed, target);
}
}
mir::TerminatorKind::Call { ref cleanup, ref destination, func: _, args: _ } => {
if let Some(ref unwind) = *cleanup {
self.propagate_bits_into_entry_set_for(in_out, changed, unwind);
}
if let Some((ref dest_lval, ref dest_bb)) = *destination {
}

for stmt in bb_data.statements.iter() {
match stmt.kind {
mir::StatementKind::Assign(..) |
mir::StatementKind::SetDiscriminant { .. } |
mir::StatementKind::StorageLive(..) |
mir::StatementKind::StorageDead(..) |
mir::StatementKind::InlineAsm { .. } |
mir::StatementKind::Assert { .. } |
mir::StatementKind::Nop => {},
mir::StatementKind::Call { ref cleanup, ref destination, func: _, args: _ } => {
if let Some(ref unwind) = *cleanup {
self.propagate_bits_into_entry_set_for(in_out, changed, unwind);
}
// N.B.: This must be done *last*, after all other
// propagation, as documented in comment above.
self.flow_state.operator.propagate_call_return(
in_out, bb, *dest_bb, dest_lval);
self.propagate_bits_into_entry_set_for(in_out, changed, dest_bb);
self.flow_state.operator.propagate_call_return(in_out, bb, destination);
}
}
}
Expand Down
Loading

0 comments on commit ff12d78

Please sign in to comment.