Skip to content

Commit 2e6a918

Browse files
committed
librustc: Add a new nop statement to the MIR.
This is useful when passes want to remove statements without affecting `Location`s.
1 parent 5f6f838 commit 2e6a918

File tree

9 files changed

+33
-5
lines changed

9 files changed

+33
-5
lines changed

src/librustc/mir/repr.rs

+20
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ impl<'tcx> Mir<'tcx> {
187187
self.var_decls.len() +
188188
self.temp_decls.len() + 1
189189
}
190+
191+
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
192+
/// invalidating statement indices in `Location`s.
193+
pub fn make_statement_nop(&mut self, location: Location) {
194+
let block = &mut self[location.block];
195+
debug_assert!(location.statement_index < block.statements.len());
196+
block.statements[location.statement_index].make_nop()
197+
}
190198
}
191199

192200
impl<'tcx> Index<BasicBlock> for Mir<'tcx> {
@@ -686,6 +694,14 @@ pub struct Statement<'tcx> {
686694
pub kind: StatementKind<'tcx>,
687695
}
688696

697+
impl<'tcx> Statement<'tcx> {
698+
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
699+
/// invalidating statement indices in `Location`s.
700+
pub fn make_nop(&mut self) {
701+
self.kind = StatementKind::Nop
702+
}
703+
}
704+
689705
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
690706
pub enum StatementKind<'tcx> {
691707
/// Write the RHS Rvalue to the LHS Lvalue.
@@ -699,6 +715,9 @@ pub enum StatementKind<'tcx> {
699715

700716
/// End the current live range for the storage of the local.
701717
StorageDead(Lvalue<'tcx>),
718+
719+
/// No-op. Useful for deleting instructions without affecting statement indices.
720+
Nop,
702721
}
703722

704723
impl<'tcx> Debug for Statement<'tcx> {
@@ -711,6 +730,7 @@ impl<'tcx> Debug for Statement<'tcx> {
711730
SetDiscriminant{lvalue: ref lv, variant_index: index} => {
712731
write!(fmt, "discriminant({:?}) = {:?}", lv, index)
713732
}
733+
Nop => write!(fmt, "nop"),
714734
}
715735
}
716736
}

src/librustc/mir/visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ macro_rules! make_mir_visitor {
346346
StatementKind::StorageDead(ref $($mutability)* lvalue) => {
347347
self.visit_lvalue(lvalue, LvalueContext::StorageDead, location);
348348
}
349+
StatementKind::Nop => {}
349350
}
350351
}
351352

src/librustc_borrowck/borrowck/mir/dataflow/impls.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ impl<'a, 'tcx> BitDenotation for MovingOutStatements<'a, 'tcx> {
455455
});
456456
}
457457
repr::StatementKind::StorageLive(_) |
458-
repr::StatementKind::StorageDead(_) => {}
458+
repr::StatementKind::StorageDead(_) |
459+
repr::StatementKind::Nop => {}
459460
}
460461
}
461462

src/librustc_borrowck/borrowck/mir/dataflow/sanity_check.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ fn each_block<'a, 'tcx, O>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
105105
(lvalue, rvalue)
106106
}
107107
repr::StatementKind::StorageLive(_) |
108-
repr::StatementKind::StorageDead(_) => continue,
108+
repr::StatementKind::StorageDead(_) |
109+
repr::StatementKind::Nop => continue,
109110
repr::StatementKind::SetDiscriminant{ .. } =>
110111
span_bug!(stmt.source_info.span,
111112
"sanity_check should run before Deaggregator inserts SetDiscriminant"),

src/librustc_borrowck/borrowck/mir/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ fn drop_flag_effects_for_location<'a, 'tcx, F>(
389389
|moi| callback(moi, DropFlagState::Present))
390390
}
391391
repr::StatementKind::StorageLive(_) |
392-
repr::StatementKind::StorageDead(_) => {}
392+
repr::StatementKind::StorageDead(_) |
393+
repr::StatementKind::Nop => {}
393394
},
394395
None => {
395396
debug!("drop_flag_effects: replace {:?}", block.terminator());

src/librustc_mir/transform/qualify_consts.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
910910
}
911911
StatementKind::SetDiscriminant { .. } |
912912
StatementKind::StorageLive(_) |
913-
StatementKind::StorageDead(_) => {}
913+
StatementKind::StorageDead(_) |
914+
StatementKind::Nop => {}
914915
}
915916
});
916917
}

src/librustc_mir/transform/type_check.rs

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
385385
}
386386
}
387387
}
388+
StatementKind::Nop => {}
388389
}
389390
}
390391

src/librustc_trans/mir/constant.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
292292
}
293293
}
294294
mir::StatementKind::StorageLive(_) |
295-
mir::StatementKind::StorageDead(_) => {}
295+
mir::StatementKind::StorageDead(_) |
296+
mir::StatementKind::Nop => {}
296297
mir::StatementKind::SetDiscriminant{ .. } => {
297298
span_bug!(span, "SetDiscriminant should not appear in constants?");
298299
}

src/librustc_trans/mir/statement.rs

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
7878
mir::StatementKind::StorageDead(ref lvalue) => {
7979
self.trans_storage_liveness(bcx, lvalue, base::Lifetime::End)
8080
}
81+
mir::StatementKind::Nop => bcx,
8182
}
8283
}
8384

0 commit comments

Comments
 (0)