mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
move to separate pass, cache layouts
This commit is contained in:
parent
90562b401e
commit
bca761dc5d
@ -12,12 +12,10 @@ pub struct InstCombine;
|
||||
|
||||
impl<'tcx> MirPass<'tcx> for InstCombine {
|
||||
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
||||
let param_env = tcx.param_env(body.source.def_id());
|
||||
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
|
||||
let ctx = InstCombineContext { tcx, local_decls, param_env };
|
||||
let ctx = InstCombineContext { tcx, local_decls };
|
||||
for block in basic_blocks.iter_mut() {
|
||||
for statement in block.statements.iter_mut() {
|
||||
ctx.combine_zst(&statement.source_info, &mut statement.kind);
|
||||
match statement.kind {
|
||||
StatementKind::Assign(box (_place, ref mut rvalue)) => {
|
||||
ctx.combine_bool_cmp(&statement.source_info, rvalue);
|
||||
@ -34,7 +32,6 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
|
||||
struct InstCombineContext<'tcx, 'a> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
local_decls: &'a LocalDecls<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
}
|
||||
|
||||
impl<'tcx, 'a> InstCombineContext<'tcx, 'a> {
|
||||
@ -44,28 +41,6 @@ impl<'tcx, 'a> InstCombineContext<'tcx, 'a> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Remove assignments to inhabited ZST places.
|
||||
fn combine_zst(&self, source_info: &SourceInfo, kind: &mut StatementKind<'tcx>) {
|
||||
match kind {
|
||||
StatementKind::Assign(box (place, _)) => {
|
||||
let place_ty = place.ty(self.local_decls, self.tcx).ty;
|
||||
if let Ok(layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
|
||||
if layout.is_zst() && !layout.abi.is_uninhabited() {
|
||||
if self.tcx.consider_optimizing(|| {
|
||||
format!(
|
||||
"InstCombine ZST - Place: {:?} SourceInfo: {:?}",
|
||||
place, source_info
|
||||
)
|
||||
}) {
|
||||
*kind = StatementKind::Nop;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Transform boolean comparisons into logical operations.
|
||||
fn combine_bool_cmp(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
|
||||
match rvalue {
|
||||
|
@ -44,6 +44,7 @@ pub mod promote_consts;
|
||||
pub mod remove_noop_landing_pads;
|
||||
pub mod remove_storage_markers;
|
||||
pub mod remove_unneeded_drops;
|
||||
pub mod remove_zsts;
|
||||
pub mod required_consts;
|
||||
pub mod rustc_peek;
|
||||
pub mod simplify;
|
||||
@ -494,6 +495,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,
|
||||
&remove_zsts::RemoveZsts,
|
||||
&const_goto::ConstGoto,
|
||||
&remove_unneeded_drops::RemoveUnneededDrops,
|
||||
&match_branches::MatchBranchSimplification,
|
||||
|
49
compiler/rustc_mir/src/transform/remove_zsts.rs
Normal file
49
compiler/rustc_mir/src/transform/remove_zsts.rs
Normal file
@ -0,0 +1,49 @@
|
||||
//! Removes assignments to ZST places.
|
||||
|
||||
use crate::transform::MirPass;
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_middle::mir::{Body, StatementKind};
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
|
||||
pub struct RemoveZsts;
|
||||
|
||||
impl<'tcx> MirPass<'tcx> for RemoveZsts {
|
||||
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
||||
let param_env = tcx.param_env(body.source.def_id());
|
||||
|
||||
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
|
||||
|
||||
let mut is_zst_cache = FxHashMap::default();
|
||||
|
||||
for block in basic_blocks.iter_mut() {
|
||||
for statement in block.statements.iter_mut() {
|
||||
match statement.kind {
|
||||
StatementKind::Assign(box (place, _)) => {
|
||||
let place_ty = place.ty(local_decls, tcx).ty;
|
||||
|
||||
let is_inhabited_zst = *is_zst_cache.entry(place_ty).or_insert_with(|| {
|
||||
if let Ok(layout) = tcx.layout_of(param_env.and(place_ty)) {
|
||||
if layout.is_zst() && !layout.abi.is_uninhabited() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
});
|
||||
|
||||
if is_inhabited_zst {
|
||||
if tcx.consider_optimizing(|| {
|
||||
format!(
|
||||
"RemoveZsts - Place: {:?} SourceInfo: {:?}",
|
||||
place, statement.source_info
|
||||
)
|
||||
}) {
|
||||
statement.make_nop();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user