Skip to content

Commit

Permalink
dse: Remove a pair of unused StorageLive and StorageDead statements
Browse files Browse the repository at this point in the history
  • Loading branch information
DianQK committed Aug 26, 2024
1 parent 93b3f30 commit a2bc389
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 49 deletions.
29 changes: 26 additions & 3 deletions compiler/rustc_mir_transform/src/dead_store_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//!

use rustc_middle::bug;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::visit::*;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use rustc_mir_dataflow::debuginfo::debuginfo_locals;
Expand Down Expand Up @@ -95,7 +95,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if !place.is_indirect() && !always_live.contains(place.local) {
live.seek_before_primary_effect(loc);
if !live.get().contains(place.local) {
patch.push(loc);
patch.push((place.local, loc));
}
}
}
Expand All @@ -120,8 +120,31 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
}

let bbs = body.basic_blocks.as_mut_preserves_cfg();
for Location { block, statement_index } in patch {
for (local, Location { block, statement_index }) in patch {
bbs[block].statements[statement_index].make_nop();
if bbs[block].statements.iter().all(|stmt| match stmt.kind {
StatementKind::Assign(box (place, _))
| StatementKind::SetDiscriminant { place: box place, .. }
| StatementKind::Deinit(box place) => place.local != local,
_ => true,
}) {
if let Some(storage_live_index) = bbs[block]
.statements
.iter()
.take(statement_index)
.position(|stmt| stmt.kind == StatementKind::StorageLive(local))
{
if let Some(storage_dead_index) = bbs[block]
.statements
.iter()
.skip(statement_index)
.position(|stmt| stmt.kind == StatementKind::StorageDead(local))
{
bbs[block].statements[storage_live_index].make_nop();
bbs[block].statements[storage_dead_index + statement_index].make_nop();
}
}
}
}
for (block, argument_index) in call_operands_to_move {
let TerminatorKind::Call { ref mut args, .. } = bbs[block].terminator_mut().kind else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@
}

bb2: {
StorageLive(_7);
StorageLive(_8);
StorageLive(_9);
StorageDead(_9);
StorageDead(_8);
StorageDead(_7);
StorageDead(_4);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@
}

bb2: {
StorageLive(_7);
StorageLive(_8);
StorageLive(_9);
StorageDead(_9);
StorageDead(_8);
StorageDead(_7);
StorageDead(_4);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@
StorageDead(_16);
StorageDead(_12);
StorageDead(_6);
StorageLive(_17);
nop;
nop;
StorageDead(_17);
nop;
nop;
StorageDead(_5);
nop;
StorageDead(_4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@

bb1: {
StorageDead(_6);
StorageLive(_12);
nop;
nop;
StorageDead(_12);
nop;
nop;
StorageDead(_5);
nop;
StorageDead(_4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@
StorageDead(_16);
StorageDead(_12);
StorageDead(_6);
StorageLive(_17);
nop;
nop;
StorageDead(_17);
nop;
nop;
StorageDead(_5);
nop;
StorageDead(_4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@

bb1: {
StorageDead(_6);
StorageLive(_12);
nop;
nop;
StorageDead(_12);
nop;
nop;
StorageDead(_5);
nop;
StorageDead(_4);
Expand Down
48 changes: 24 additions & 24 deletions tests/mir-opt/pre-codegen/loops.vec_move.PreCodegen.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -208,31 +208,32 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
_5 = move (((_1.0: alloc::raw_vec::RawVec<impl Sized>).0: alloc::raw_vec::RawVecInner).2: std::alloc::Global);
_6 = move ((_1.0: alloc::raw_vec::RawVec<impl Sized>).1: std::marker::PhantomData<impl Sized>);
_7 = move (_1.1: usize);
StorageLive(_29);
StorageLive(_30);
StorageLive(_31);
StorageLive(_10);
StorageLive(_32);
StorageLive(_33);
StorageLive(_11);
StorageLive(_34);
StorageLive(_35);
StorageLive(_36);
StorageLive(_37);
StorageLive(_39);
StorageLive(_9);
StorageLive(_8);
StorageLive(_16);
StorageLive(_46);
StorageLive(_28);
StorageLive(_29);
StorageLive(_30);
StorageLive(_31);
StorageDead(_31);
StorageLive(_40);
StorageLive(_41);
StorageDead(_41);
StorageDead(_40);
StorageDead(_30);
StorageDead(_29);
StorageLive(_32);
StorageLive(_33);
StorageDead(_33);
StorageLive(_42);
_8 = copy (_2.0: *const u8);
_9 = copy _8 as *const impl Sized (PtrToPtr);
_10 = NonNull::<impl Sized> { pointer: copy _9 };
StorageDead(_42);
StorageDead(_32);
_11 = copy _8 as *mut impl Sized (PtrToPtr);
StorageLive(_14);
switchInt(const <impl Sized as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
Expand All @@ -241,10 +242,6 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
bb1: {
StorageLive(_13);
StorageLive(_12);
StorageLive(_36);
StorageLive(_37);
StorageDead(_37);
StorageDead(_36);
_12 = Offset(copy _11, copy _7);
_13 = move _12 as *const impl Sized (PtrToPtr);
StorageDead(_12);
Expand All @@ -254,10 +251,7 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
}

bb2: {
StorageLive(_34);
StorageLive(_35);
StorageDead(_35);
StorageDead(_34);
StorageLive(_45);
StorageLive(_43);
StorageLive(_44);
StorageLive(_15);
Expand All @@ -268,20 +262,15 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
bb3: {
StorageDead(_15);
StorageDead(_44);
StorageLive(_45);
StorageLive(_46);
StorageDead(_46);
StorageDead(_45);
StorageDead(_43);
StorageDead(_45);
_14 = copy _16 as *const impl Sized (PtrToPtr);
goto -> bb4;
}

bb4: {
StorageLive(_18);
StorageLive(_38);
StorageLive(_39);
StorageDead(_39);
StorageLive(_47);
StorageLive(_17);
_17 = SizeOf(impl Sized);
Expand Down Expand Up @@ -312,11 +301,22 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
StorageDead(_18);
StorageDead(_14);
StorageDead(_28);
StorageDead(_46);
StorageDead(_16);
StorageDead(_8);
StorageDead(_9);
StorageDead(_39);
StorageDead(_37);
StorageDead(_36);
StorageDead(_35);
StorageDead(_34);
StorageDead(_11);
StorageDead(_33);
StorageDead(_32);
StorageDead(_10);
StorageDead(_31);
StorageDead(_30);
StorageDead(_29);
StorageDead(_3);
StorageDead(_4);
StorageDead(_5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@
}

bb5: {
StorageLive(_8);
_0 = const Option::<i32>::None;
StorageDead(_8);
goto -> bb7;
}

Expand Down

0 comments on commit a2bc389

Please sign in to comment.