diff --git a/compiler/rustc_mir_transform/src/dead_store_elimination.rs b/compiler/rustc_mir_transform/src/dead_store_elimination.rs index 137ae8836bc0a..71feca0c5d730 100644 --- a/compiler/rustc_mir_transform/src/dead_store_elimination.rs +++ b/compiler/rustc_mir_transform/src/dead_store_elimination.rs @@ -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; @@ -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)); } } } @@ -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 { diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff index b0550e36f69f1..59e0a99d1d0d1 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff @@ -37,12 +37,6 @@ } bb2: { - StorageLive(_7); - StorageLive(_8); - StorageLive(_9); - StorageDead(_9); - StorageDead(_8); - StorageDead(_7); StorageDead(_4); return; } diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff index b2264cf79d1d9..ba486cb201228 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff @@ -37,12 +37,6 @@ } bb2: { - StorageLive(_7); - StorageLive(_8); - StorageLive(_9); - StorageDead(_9); - StorageDead(_8); - StorageDead(_7); StorageDead(_4); return; } diff --git a/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-abort.diff b/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-abort.diff index 6bdd646c09349..b680af4142169 100644 --- a/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-abort.diff +++ b/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-abort.diff @@ -102,10 +102,10 @@ StorageDead(_16); StorageDead(_12); StorageDead(_6); - StorageLive(_17); nop; nop; - StorageDead(_17); + nop; + nop; StorageDead(_5); nop; StorageDead(_4); diff --git a/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-unwind.diff b/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-unwind.diff index 8c96e03ef699c..6884c5041a16f 100644 --- a/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-unwind.diff +++ b/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-unwind.diff @@ -45,10 +45,10 @@ bb1: { StorageDead(_6); - StorageLive(_12); nop; nop; - StorageDead(_12); + nop; + nop; StorageDead(_5); nop; StorageDead(_4); diff --git a/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-abort.diff b/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-abort.diff index 5d97c192a01dd..d6b76faf52c2d 100644 --- a/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-abort.diff +++ b/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-abort.diff @@ -102,10 +102,10 @@ StorageDead(_16); StorageDead(_12); StorageDead(_6); - StorageLive(_17); nop; nop; - StorageDead(_17); + nop; + nop; StorageDead(_5); nop; StorageDead(_4); diff --git a/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-unwind.diff b/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-unwind.diff index 70939c4ad376c..911bebaf2129b 100644 --- a/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-unwind.diff +++ b/tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-unwind.diff @@ -45,10 +45,10 @@ bb1: { StorageDead(_6); - StorageLive(_12); nop; nop; - StorageDead(_12); + nop; + nop; StorageDead(_5); nop; StorageDead(_4); diff --git a/tests/mir-opt/pre-codegen/loops.vec_move.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/loops.vec_move.PreCodegen.after.mir index 66c064019022d..7daf85fb9c787 100644 --- a/tests/mir-opt/pre-codegen/loops.vec_move.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/loops.vec_move.PreCodegen.after.mir @@ -208,31 +208,32 @@ fn vec_move(_1: Vec) -> () { _5 = move (((_1.0: alloc::raw_vec::RawVec).0: alloc::raw_vec::RawVecInner).2: std::alloc::Global); _6 = move ((_1.0: alloc::raw_vec::RawVec).1: std::marker::PhantomData); _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:: { pointer: copy _9 }; StorageDead(_42); - StorageDead(_32); _11 = copy _8 as *mut impl Sized (PtrToPtr); StorageLive(_14); switchInt(const ::IS_ZST) -> [0: bb1, otherwise: bb2]; @@ -241,10 +242,6 @@ fn vec_move(_1: Vec) -> () { 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); @@ -254,10 +251,7 @@ fn vec_move(_1: Vec) -> () { } bb2: { - StorageLive(_34); - StorageLive(_35); - StorageDead(_35); - StorageDead(_34); + StorageLive(_45); StorageLive(_43); StorageLive(_44); StorageLive(_15); @@ -268,11 +262,8 @@ fn vec_move(_1: Vec) -> () { 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; } @@ -280,8 +271,6 @@ fn vec_move(_1: Vec) -> () { bb4: { StorageLive(_18); StorageLive(_38); - StorageLive(_39); - StorageDead(_39); StorageLive(_47); StorageLive(_17); _17 = SizeOf(impl Sized); @@ -312,11 +301,22 @@ fn vec_move(_1: Vec) -> () { 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); diff --git a/tests/mir-opt/separate_const_switch.too_complex.JumpThreading.diff b/tests/mir-opt/separate_const_switch.too_complex.JumpThreading.diff index c57b0e8ab3d07..ed3f54e9653e8 100644 --- a/tests/mir-opt/separate_const_switch.too_complex.JumpThreading.diff +++ b/tests/mir-opt/separate_const_switch.too_complex.JumpThreading.diff @@ -54,9 +54,7 @@ } bb5: { - StorageLive(_8); _0 = const Option::::None; - StorageDead(_8); goto -> bb7; }