Skip to content

Commit c0ea8f3

Browse files
committed
Remove StorageDead and StorageLive statements using MIR locals of type ()
1 parent 42ce9b4 commit c0ea8f3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/librustc_mir/transform/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub mod no_landing_pads;
3131
pub mod promote_consts;
3232
pub mod qualify_min_const_fn;
3333
pub mod remove_noop_landing_pads;
34+
pub mod remove_unit_storage;
3435
pub mod rustc_peek;
3536
pub mod simplify;
3637
pub mod simplify_branches;
@@ -300,6 +301,7 @@ fn run_optimization_passes<'tcx>(
300301
// From here on out, regions are gone.
301302
&erase_regions::EraseRegions,
302303
// Optimizations begin.
304+
&remove_unit_storage::RemoveUnitStorage,
303305
&unreachable_prop::UnreachablePropagation,
304306
&uninhabited_enum_branching::UninhabitedEnumBranching,
305307
&simplify::SimplifyCfg::new("after-uninhabited-enum-branching"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! The `RemoveUnitStorage` pass removes `StorageLive` and `StorageDead` statements
2+
//! which operates on locals of type `()`.
3+
4+
use crate::transform::{MirPass, MirSource};
5+
use rustc::mir::*;
6+
use rustc::ty::TyCtxt;
7+
use smallvec::SmallVec;
8+
9+
pub struct RemoveUnitStorage;
10+
11+
impl<'tcx> MirPass<'tcx> for RemoveUnitStorage {
12+
fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
13+
let unit_locals: SmallVec<[_; 32]> =
14+
body.local_decls.iter().map(|local| local.ty == tcx.types.unit).collect();
15+
16+
for block in body.basic_blocks_mut() {
17+
for stmt in &mut block.statements {
18+
match &stmt.kind {
19+
StatementKind::StorageLive(l) | StatementKind::StorageDead(l) => {
20+
if unit_locals[l.as_usize()] {
21+
stmt.make_nop();
22+
}
23+
}
24+
_ => (),
25+
}
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)