1
Fork 0

Remove storage markers if they won't be used during code generation

The storage markers constitute a substantial portion of all MIR
statements. At the same time, for builds without any optimizations,
the storage markers have no further use during and after MIR
optimization phase.

If storage markers are not necessary for code generation, remove them.
This commit is contained in:
Tomasz Miąsko 2021-01-13 00:00:00 +00:00
parent 1b1d85fd4c
commit 8b184ff1b8
5 changed files with 153 additions and 2 deletions

View file

@ -42,6 +42,7 @@ pub mod no_landing_pads;
pub mod nrvo;
pub mod promote_consts;
pub mod remove_noop_landing_pads;
pub mod remove_storage_markers;
pub mod remove_unneeded_drops;
pub mod required_consts;
pub mod rustc_peek;
@ -493,6 +494,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// The main optimizations that we do on MIR.
let optimizations: &[&dyn MirPass<'tcx>] = &[
&remove_storage_markers::RemoveStorageMarkers,
&const_goto::ConstGoto,
&remove_unneeded_drops::RemoveUnneededDrops,
&match_branches::MatchBranchSimplification,

View file

@ -0,0 +1,25 @@
//! This pass removes storage markers if they won't be emitted during codegen.
use crate::transform::MirPass;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
pub struct RemoveStorageMarkers;
impl<'tcx> MirPass<'tcx> for RemoveStorageMarkers {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if tcx.sess.emit_lifetime_markers() {
return;
}
trace!("Running RemoveStorageMarkers on {:?}", body.source);
for data in body.basic_blocks_mut() {
data.statements.retain(|statement| match statement.kind {
StatementKind::StorageLive(..)
| StatementKind::StorageDead(..)
| StatementKind::Nop => false,
_ => true,
})
}
}
}