Skip to content

Commit bca761d

Browse files
move to separate pass, cache layouts
1 parent 90562b4 commit bca761d

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

compiler/rustc_mir/src/transform/instcombine.rs

+1-26
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ pub struct InstCombine;
1212

1313
impl<'tcx> MirPass<'tcx> for InstCombine {
1414
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
15-
let param_env = tcx.param_env(body.source.def_id());
1615
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
17-
let ctx = InstCombineContext { tcx, local_decls, param_env };
16+
let ctx = InstCombineContext { tcx, local_decls };
1817
for block in basic_blocks.iter_mut() {
1918
for statement in block.statements.iter_mut() {
20-
ctx.combine_zst(&statement.source_info, &mut statement.kind);
2119
match statement.kind {
2220
StatementKind::Assign(box (_place, ref mut rvalue)) => {
2321
ctx.combine_bool_cmp(&statement.source_info, rvalue);
@@ -34,7 +32,6 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
3432
struct InstCombineContext<'tcx, 'a> {
3533
tcx: TyCtxt<'tcx>,
3634
local_decls: &'a LocalDecls<'tcx>,
37-
param_env: ty::ParamEnv<'tcx>,
3835
}
3936

4037
impl<'tcx, 'a> InstCombineContext<'tcx, 'a> {
@@ -44,28 +41,6 @@ impl<'tcx, 'a> InstCombineContext<'tcx, 'a> {
4441
})
4542
}
4643

47-
/// Remove assignments to inhabited ZST places.
48-
fn combine_zst(&self, source_info: &SourceInfo, kind: &mut StatementKind<'tcx>) {
49-
match kind {
50-
StatementKind::Assign(box (place, _)) => {
51-
let place_ty = place.ty(self.local_decls, self.tcx).ty;
52-
if let Ok(layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
53-
if layout.is_zst() && !layout.abi.is_uninhabited() {
54-
if self.tcx.consider_optimizing(|| {
55-
format!(
56-
"InstCombine ZST - Place: {:?} SourceInfo: {:?}",
57-
place, source_info
58-
)
59-
}) {
60-
*kind = StatementKind::Nop;
61-
}
62-
}
63-
}
64-
}
65-
_ => {}
66-
}
67-
}
68-
6944
/// Transform boolean comparisons into logical operations.
7045
fn combine_bool_cmp(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
7146
match rvalue {

compiler/rustc_mir/src/transform/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub mod promote_consts;
4444
pub mod remove_noop_landing_pads;
4545
pub mod remove_storage_markers;
4646
pub mod remove_unneeded_drops;
47+
pub mod remove_zsts;
4748
pub mod required_consts;
4849
pub mod rustc_peek;
4950
pub mod simplify;
@@ -494,6 +495,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
494495
// The main optimizations that we do on MIR.
495496
let optimizations: &[&dyn MirPass<'tcx>] = &[
496497
&remove_storage_markers::RemoveStorageMarkers,
498+
&remove_zsts::RemoveZsts,
497499
&const_goto::ConstGoto,
498500
&remove_unneeded_drops::RemoveUnneededDrops,
499501
&match_branches::MatchBranchSimplification,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! Removes assignments to ZST places.
2+
3+
use crate::transform::MirPass;
4+
use rustc_data_structures::fx::FxHashMap;
5+
use rustc_middle::mir::{Body, StatementKind};
6+
use rustc_middle::ty::TyCtxt;
7+
8+
pub struct RemoveZsts;
9+
10+
impl<'tcx> MirPass<'tcx> for RemoveZsts {
11+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
12+
let param_env = tcx.param_env(body.source.def_id());
13+
14+
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
15+
16+
let mut is_zst_cache = FxHashMap::default();
17+
18+
for block in basic_blocks.iter_mut() {
19+
for statement in block.statements.iter_mut() {
20+
match statement.kind {
21+
StatementKind::Assign(box (place, _)) => {
22+
let place_ty = place.ty(local_decls, tcx).ty;
23+
24+
let is_inhabited_zst = *is_zst_cache.entry(place_ty).or_insert_with(|| {
25+
if let Ok(layout) = tcx.layout_of(param_env.and(place_ty)) {
26+
if layout.is_zst() && !layout.abi.is_uninhabited() {
27+
return true;
28+
}
29+
}
30+
false
31+
});
32+
33+
if is_inhabited_zst {
34+
if tcx.consider_optimizing(|| {
35+
format!(
36+
"RemoveZsts - Place: {:?} SourceInfo: {:?}",
37+
place, statement.source_info
38+
)
39+
}) {
40+
statement.make_nop();
41+
}
42+
}
43+
}
44+
_ => {}
45+
}
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)