Skip to content

Commit

Permalink
Remove StorageDead and StorageLive statements using MIR locals of…
Browse files Browse the repository at this point in the history
… type `()`
  • Loading branch information
Zoxc committed Mar 14, 2020
1 parent 42ce9b4 commit c0ea8f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/librustc_mir/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod no_landing_pads;
pub mod promote_consts;
pub mod qualify_min_const_fn;
pub mod remove_noop_landing_pads;
pub mod remove_unit_storage;
pub mod rustc_peek;
pub mod simplify;
pub mod simplify_branches;
Expand Down Expand Up @@ -300,6 +301,7 @@ fn run_optimization_passes<'tcx>(
// From here on out, regions are gone.
&erase_regions::EraseRegions,
// Optimizations begin.
&remove_unit_storage::RemoveUnitStorage,
&unreachable_prop::UnreachablePropagation,
&uninhabited_enum_branching::UninhabitedEnumBranching,
&simplify::SimplifyCfg::new("after-uninhabited-enum-branching"),
Expand Down
29 changes: 29 additions & 0 deletions src/librustc_mir/transform/remove_unit_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! The `RemoveUnitStorage` pass removes `StorageLive` and `StorageDead` statements
//! which operates on locals of type `()`.

use crate::transform::{MirPass, MirSource};
use rustc::mir::*;
use rustc::ty::TyCtxt;
use smallvec::SmallVec;

pub struct RemoveUnitStorage;

impl<'tcx> MirPass<'tcx> for RemoveUnitStorage {
fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
let unit_locals: SmallVec<[_; 32]> =
body.local_decls.iter().map(|local| local.ty == tcx.types.unit).collect();

for block in body.basic_blocks_mut() {
for stmt in &mut block.statements {
match &stmt.kind {
StatementKind::StorageLive(l) | StatementKind::StorageDead(l) => {
if unit_locals[l.as_usize()] {
stmt.make_nop();
}
}
_ => (),
}
}
}
}
}

0 comments on commit c0ea8f3

Please sign in to comment.