about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-06-11 02:03:12 +0000
committerbors <bors@rust-lang.org>2024-06-11 02:03:12 +0000
commitfa1681c9f6a66f0240c46c98bfef6209c9d6df23 (patch)
tree6c543041d9010fdf1956d23f607bc4eaa1065b06
parentb5b13568fb5da4ac988bde370008d6134d3dfe6c (diff)
parent8fbab183d7202880f87f6cc722d63cc4bbd1d140 (diff)
downloadrust-fa1681c9f6a66f0240c46c98bfef6209c9d6df23.tar.gz
rust-fa1681c9f6a66f0240c46c98bfef6209c9d6df23.zip
Auto merge of #125910 - scottmcm:single-use-consts, r=saethlin
Add `SingleUseConsts` mir-opt pass

The goal here is to make a pass that can be run in debug builds to simplify the common case of constants that are used just once -- that doesn't need SSA handling and avoids any potential downside of multi-use constants.  In particular, to simplify the `if T::IS_ZST` pattern that's common in the standard library.

By also handling the case of constants that are *never* actually used this fully replaces the `ConstDebugInfo` pass, since it has all the information needed to do that naturally from the traversal it needs to do anyway.

This is roughly a wash on instructions on its own (a couple regressions, a few improvements https://github.com/rust-lang/rust/pull/125910#issuecomment-2144963361), with a bunch of size improvements.  So I'd like to land it as its own PR, then do follow-ups to take more advantage of it (in the inliner, cg_ssa, etc).

r? `@saethlin`
-rw-r--r--compiler/rustc_mir_transform/src/const_debuginfo.rs102
-rw-r--r--compiler/rustc_mir_transform/src/lib.rs4
-rw-r--r--compiler/rustc_mir_transform/src/single_use_consts.rs199
-rw-r--r--tests/mir-opt/building/match/sort_candidates.rs6
-rw-r--r--tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff (renamed from tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff)46
-rw-r--r--tests/mir-opt/const_debuginfo.rs6
-rw-r--r--tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-abort.diff2
-rw-r--r--tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-unwind.diff2
-rw-r--r--tests/mir-opt/inline/issue_106141.outer.Inline.panic-abort.diff2
-rw-r--r--tests/mir-opt/inline/issue_106141.outer.Inline.panic-unwind.diff2
-rw-r--r--tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff4
-rw-r--r--tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff4
-rw-r--r--tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-abort.diff32
-rw-r--r--tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-abort.diff32
-rw-r--r--tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-abort.mir102
-rw-r--r--tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-unwind.mir102
-rw-r--r--tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-abort.mir88
-rw-r--r--tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-unwind.mir88
-rw-r--r--tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-abort.mir96
-rw-r--r--tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-unwind.mir96
-rw-r--r--tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-abort.diff12
-rw-r--r--tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-unwind.diff12
-rw-r--r--tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-abort.diff31
-rw-r--r--tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-unwind.diff31
-rw-r--r--tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-abort.diff46
-rw-r--r--tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-unwind.diff46
-rw-r--r--tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-abort.diff14
-rw-r--r--tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-unwind.diff14
-rw-r--r--tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-abort.diff36
-rw-r--r--tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-unwind.diff36
-rw-r--r--tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-abort.diff47
-rw-r--r--tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-unwind.diff47
-rw-r--r--tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-abort.diff21
-rw-r--r--tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-unwind.diff21
-rw-r--r--tests/mir-opt/single_use_consts.rs80
35 files changed, 1042 insertions, 467 deletions
diff --git a/compiler/rustc_mir_transform/src/const_debuginfo.rs b/compiler/rustc_mir_transform/src/const_debuginfo.rs
deleted file mode 100644
index e4e4270c499..00000000000
--- a/compiler/rustc_mir_transform/src/const_debuginfo.rs
+++ /dev/null
@@ -1,102 +0,0 @@
-//! Finds locals which are assigned once to a const and unused except for debuginfo and converts
-//! their debuginfo to use the const directly, allowing the local to be removed.
-
-use rustc_middle::{
-    mir::{
-        visit::{PlaceContext, Visitor},
-        Body, ConstOperand, Local, Location, Operand, Rvalue, StatementKind, VarDebugInfoContents,
-    },
-    ty::TyCtxt,
-};
-
-use crate::MirPass;
-use rustc_index::{bit_set::BitSet, IndexVec};
-
-pub struct ConstDebugInfo;
-
-impl<'tcx> MirPass<'tcx> for ConstDebugInfo {
-    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
-        sess.mir_opt_level() > 0
-    }
-
-    fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
-        trace!("running ConstDebugInfo on {:?}", body.source);
-
-        for (local, constant) in find_optimization_opportunities(body) {
-            for debuginfo in &mut body.var_debug_info {
-                if let VarDebugInfoContents::Place(p) = debuginfo.value {
-                    if p.local == local && p.projection.is_empty() {
-                        trace!(
-                            "changing debug info for {:?} from place {:?} to constant {:?}",
-                            debuginfo.name,
-                            p,
-                            constant
-                        );
-                        debuginfo.value = VarDebugInfoContents::Const(constant);
-                    }
-                }
-            }
-        }
-    }
-}
-
-struct LocalUseVisitor {
-    local_mutating_uses: IndexVec<Local, u8>,
-    local_assignment_locations: IndexVec<Local, Option<Location>>,
-}
-
-fn find_optimization_opportunities<'tcx>(body: &Body<'tcx>) -> Vec<(Local, ConstOperand<'tcx>)> {
-    let mut visitor = LocalUseVisitor {
-        local_mutating_uses: IndexVec::from_elem(0, &body.local_decls),
-        local_assignment_locations: IndexVec::from_elem(None, &body.local_decls),
-    };
-
-    visitor.visit_body(body);
-
-    let mut locals_to_debuginfo = BitSet::new_empty(body.local_decls.len());
-    for debuginfo in &body.var_debug_info {
-        if let VarDebugInfoContents::Place(p) = debuginfo.value
-            && let Some(l) = p.as_local()
-        {
-            locals_to_debuginfo.insert(l);
-        }
-    }
-
-    let mut eligible_locals = Vec::new();
-    for (local, mutating_uses) in visitor.local_mutating_uses.drain_enumerated(..) {
-        if mutating_uses != 1 || !locals_to_debuginfo.contains(local) {
-            continue;
-        }
-
-        if let Some(location) = visitor.local_assignment_locations[local] {
-            let bb = &body[location.block];
-
-            // The value is assigned as the result of a call, not a constant
-            if bb.statements.len() == location.statement_index {
-                continue;
-            }
-
-            if let StatementKind::Assign(box (p, Rvalue::Use(Operand::Constant(box c)))) =
-                &bb.statements[location.statement_index].kind
-            {
-                if let Some(local) = p.as_local() {
-                    eligible_locals.push((local, *c));
-                }
-            }
-        }
-    }
-
-    eligible_locals
-}
-
-impl Visitor<'_> for LocalUseVisitor {
-    fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
-        if context.is_mutating_use() {
-            self.local_mutating_uses[local] = self.local_mutating_uses[local].saturating_add(1);
-
-            if context.is_place_assignment() {
-                self.local_assignment_locations[local] = Some(location);
-            }
-        }
-    }
-}
diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs
index e4670633914..551760f4703 100644
--- a/compiler/rustc_mir_transform/src/lib.rs
+++ b/compiler/rustc_mir_transform/src/lib.rs
@@ -55,7 +55,6 @@ mod remove_place_mention;
 // This pass is public to allow external drivers to perform MIR cleanup
 mod add_subtyping_projections;
 pub mod cleanup_post_borrowck;
-mod const_debuginfo;
 mod copy_prop;
 mod coroutine;
 mod cost_checker;
@@ -106,6 +105,7 @@ mod check_alignment;
 pub mod simplify;
 mod simplify_branches;
 mod simplify_comparison_integral;
+mod single_use_consts;
 mod sroa;
 mod unreachable_enum_branching;
 mod unreachable_prop;
@@ -593,7 +593,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
             &gvn::GVN,
             &simplify::SimplifyLocals::AfterGVN,
             &dataflow_const_prop::DataflowConstProp,
-            &const_debuginfo::ConstDebugInfo,
+            &single_use_consts::SingleUseConsts,
             &o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
             &jump_threading::JumpThreading,
             &early_otherwise_branch::EarlyOtherwiseBranch,
diff --git a/compiler/rustc_mir_transform/src/single_use_consts.rs b/compiler/rustc_mir_transform/src/single_use_consts.rs
new file mode 100644
index 00000000000..93736e55996
--- /dev/null
+++ b/compiler/rustc_mir_transform/src/single_use_consts.rs
@@ -0,0 +1,199 @@
+use rustc_index::{bit_set::BitSet, IndexVec};
+use rustc_middle::bug;
+use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
+use rustc_middle::mir::*;
+use rustc_middle::ty::TyCtxt;
+
+/// Various parts of MIR building introduce temporaries that are commonly not needed.
+///
+/// Notably, `if CONST` and `match CONST` end up being used-once temporaries, which
+/// obfuscates the structure for other passes and codegen, which would like to always
+/// be able to just see the constant directly.
+///
+/// At higher optimization levels fancier passes like GVN will take care of this
+/// in a more general fashion, but this handles the easy cases so can run in debug.
+///
+/// This only removes constants with a single-use because re-evaluating constants
+/// isn't always an improvement, especially for large ones.
+///
+/// It also removes *never*-used constants, since it had all the information
+/// needed to do that too, including updating the debug info.
+pub struct SingleUseConsts;
+
+impl<'tcx> MirPass<'tcx> for SingleUseConsts {
+    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
+        sess.mir_opt_level() > 0
+    }
+
+    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
+        let mut finder = SingleUseConstsFinder {
+            ineligible_locals: BitSet::new_empty(body.local_decls.len()),
+            locations: IndexVec::from_elem(LocationPair::new(), &body.local_decls),
+            locals_in_debug_info: BitSet::new_empty(body.local_decls.len()),
+        };
+
+        finder.ineligible_locals.insert_range(..=Local::from_usize(body.arg_count));
+
+        finder.visit_body(body);
+
+        for (local, locations) in finder.locations.iter_enumerated() {
+            if finder.ineligible_locals.contains(local) {
+                continue;
+            }
+
+            let Some(init_loc) = locations.init_loc else {
+                continue;
+            };
+
+            // We're only changing an operand, not the terminator kinds or successors
+            let basic_blocks = body.basic_blocks.as_mut_preserves_cfg();
+            let init_statement =
+                basic_blocks[init_loc.block].statements[init_loc.statement_index].replace_nop();
+            let StatementKind::Assign(place_and_rvalue) = init_statement.kind else {
+                bug!("No longer an assign?");
+            };
+            let (place, rvalue) = *place_and_rvalue;
+            assert_eq!(place.as_local(), Some(local));
+            let Rvalue::Use(operand) = rvalue else { bug!("No longer a use?") };
+
+            let mut replacer = LocalReplacer { tcx, local, operand: Some(operand) };
+
+            if finder.locals_in_debug_info.contains(local) {
+                for var_debug_info in &mut body.var_debug_info {
+                    replacer.visit_var_debug_info(var_debug_info);
+                }
+            }
+
+            let Some(use_loc) = locations.use_loc else { continue };
+
+            let use_block = &mut basic_blocks[use_loc.block];
+            if let Some(use_statement) = use_block.statements.get_mut(use_loc.statement_index) {
+                replacer.visit_statement(use_statement, use_loc);
+            } else {
+                replacer.visit_terminator(use_block.terminator_mut(), use_loc);
+            }
+
+            if replacer.operand.is_some() {
+                bug!(
+                    "operand wasn't used replacing local {local:?} with locations {locations:?} in body {body:#?}"
+                );
+            }
+        }
+    }
+}
+
+#[derive(Copy, Clone, Debug)]
+struct LocationPair {
+    init_loc: Option<Location>,
+    use_loc: Option<Location>,
+}
+
+impl LocationPair {
+    fn new() -> Self {
+        Self { init_loc: None, use_loc: None }
+    }
+}
+
+struct SingleUseConstsFinder {
+    ineligible_locals: BitSet<Local>,
+    locations: IndexVec<Local, LocationPair>,
+    locals_in_debug_info: BitSet<Local>,
+}
+
+impl<'tcx> Visitor<'tcx> for SingleUseConstsFinder {
+    fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
+        if let Some(local) = place.as_local()
+            && let Rvalue::Use(operand) = rvalue
+            && let Operand::Constant(_) = operand
+        {
+            let locations = &mut self.locations[local];
+            if locations.init_loc.is_some() {
+                self.ineligible_locals.insert(local);
+            } else {
+                locations.init_loc = Some(location);
+            }
+        } else {
+            self.super_assign(place, rvalue, location);
+        }
+    }
+
+    fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
+        if let Some(place) = operand.place()
+            && let Some(local) = place.as_local()
+        {
+            let locations = &mut self.locations[local];
+            if locations.use_loc.is_some() {
+                self.ineligible_locals.insert(local);
+            } else {
+                locations.use_loc = Some(location);
+            }
+        } else {
+            self.super_operand(operand, location);
+        }
+    }
+
+    fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
+        match &statement.kind {
+            // Storage markers are irrelevant to this.
+            StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => {}
+            _ => self.super_statement(statement, location),
+        }
+    }
+
+    fn visit_var_debug_info(&mut self, var_debug_info: &VarDebugInfo<'tcx>) {
+        if let VarDebugInfoContents::Place(place) = &var_debug_info.value
+            && let Some(local) = place.as_local()
+        {
+            self.locals_in_debug_info.insert(local);
+        } else {
+            self.super_var_debug_info(var_debug_info);
+        }
+    }
+
+    fn visit_local(&mut self, local: Local, _context: PlaceContext, _location: Location) {
+        // If there's any path that gets here, rather than being understood elsewhere,
+        // then we'd better not do anything with this local.
+        self.ineligible_locals.insert(local);
+    }
+}
+
+struct LocalReplacer<'tcx> {
+    tcx: TyCtxt<'tcx>,
+    local: Local,
+    operand: Option<Operand<'tcx>>,
+}
+
+impl<'tcx> MutVisitor<'tcx> for LocalReplacer<'tcx> {
+    fn tcx(&self) -> TyCtxt<'tcx> {
+        self.tcx
+    }
+
+    fn visit_operand(&mut self, operand: &mut Operand<'tcx>, _location: Location) {
+        if let Operand::Copy(place) | Operand::Move(place) = operand
+            && let Some(local) = place.as_local()
+            && local == self.local
+        {
+            *operand = self.operand.take().unwrap_or_else(|| {
+                bug!("there was a second use of the operand");
+            });
+        }
+    }
+
+    fn visit_var_debug_info(&mut self, var_debug_info: &mut VarDebugInfo<'tcx>) {
+        if let VarDebugInfoContents::Place(place) = &var_debug_info.value
+            && let Some(local) = place.as_local()
+            && local == self.local
+        {
+            let const_op = self
+                .operand
+                .as_ref()
+                .unwrap_or_else(|| {
+                    bug!("the operand was already stolen");
+                })
+                .constant()
+                .unwrap()
+                .clone();
+            var_debug_info.value = VarDebugInfoContents::Const(const_op);
+        }
+    }
+}
diff --git a/tests/mir-opt/building/match/sort_candidates.rs b/tests/mir-opt/building/match/sort_candidates.rs
index f207f0b3234..593a975a7a4 100644
--- a/tests/mir-opt/building/match/sort_candidates.rs
+++ b/tests/mir-opt/building/match/sort_candidates.rs
@@ -5,9 +5,9 @@ fn constant_eq(s: &str, b: bool) -> u32 {
     // Check that we only test "a" once
 
     // CHECK-LABEL: fn constant_eq(
-    // CHECK: bb0: {
-    // CHECK: [[a:_.*]] = const "a";
-    // CHECK-NOT: {{_.*}} = const "a";
+    // CHECK-NOT: const "a"
+    // CHECK: {{_[0-9]+}} = const "a" as &[u8] (Transmute);
+    // CHECK-NOT: const "a"
     match (s, b) {
         ("a", _) if true => 1,
         ("b", true) => 2,
diff --git a/tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff b/tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff
index c1529dbee13..ac33f51984c 100644
--- a/tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff
+++ b/tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff
@@ -1,5 +1,5 @@
-- // MIR for `main` before ConstDebugInfo
-+ // MIR for `main` after ConstDebugInfo
+- // MIR for `main` before SingleUseConsts
++ // MIR for `main` after SingleUseConsts
   
   fn main() -> () {
       let mut _0: ();
@@ -56,39 +56,53 @@
   
       bb0: {
           nop;
-          _1 = const 1_u8;
+-         _1 = const 1_u8;
           nop;
-          _2 = const 2_u8;
+-         _2 = const 2_u8;
           nop;
-          _3 = const 3_u8;
+-         _3 = const 3_u8;
++         nop;
++         nop;
++         nop;
           StorageLive(_4);
           StorageLive(_5);
           StorageLive(_6);
-          _6 = const 1_u8;
+-         _6 = const 1_u8;
++         nop;
           StorageLive(_7);
-          _7 = const 2_u8;
-          _5 = const 3_u8;
+-         _7 = const 2_u8;
+-         _5 = const 3_u8;
++         nop;
++         nop;
           StorageDead(_7);
           StorageDead(_6);
           StorageLive(_8);
-          _8 = const 3_u8;
-          _4 = const 6_u8;
+-         _8 = const 3_u8;
+-         _4 = const 6_u8;
++         nop;
++         nop;
           StorageDead(_8);
           StorageDead(_5);
           StorageLive(_9);
-          _9 = const "hello, world!";
+-         _9 = const "hello, world!";
++         nop;
           StorageLive(_10);
           _10 = (const true, const false, const 123_u32);
           StorageLive(_11);
-          _11 = const Option::<u16>::Some(99_u16);
+-         _11 = const Option::<u16>::Some(99_u16);
++         nop;
           StorageLive(_12);
-          _12 = const Point {{ x: 32_u32, y: 32_u32 }};
+-         _12 = const Point {{ x: 32_u32, y: 32_u32 }};
++         nop;
           StorageLive(_13);
           nop;
-          _14 = const 32_u32;
+-         _14 = const 32_u32;
++         nop;
           StorageLive(_15);
-          _15 = const 32_u32;
-          _13 = const 64_u32;
+-         _15 = const 32_u32;
+-         _13 = const 64_u32;
++         nop;
++         nop;
           StorageDead(_15);
           nop;
           _0 = const ();
diff --git a/tests/mir-opt/const_debuginfo.rs b/tests/mir-opt/const_debuginfo.rs
index b880d7e07a6..907d7fef067 100644
--- a/tests/mir-opt/const_debuginfo.rs
+++ b/tests/mir-opt/const_debuginfo.rs
@@ -1,12 +1,14 @@
-//@ test-mir-pass: ConstDebugInfo
+//@ test-mir-pass: SingleUseConsts
 //@ compile-flags: -C overflow-checks=no -Zmir-enable-passes=+GVN
 
+#![allow(unused)]
+
 struct Point {
     x: u32,
     y: u32,
 }
 
-// EMIT_MIR const_debuginfo.main.ConstDebugInfo.diff
+// EMIT_MIR const_debuginfo.main.SingleUseConsts.diff
 fn main() {
     // CHECK-LABEL: fn main(
     // CHECK: debug x => const 1_u8;
diff --git a/tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-abort.diff b/tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-abort.diff
index 570ec129f06..b596e25ddfd 100644
--- a/tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-abort.diff
+++ b/tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-abort.diff
@@ -18,7 +18,7 @@
       bb0: {
           StorageLive(_1);
           StorageLive(_2);
-          _2 = const 1_u32;
+          nop;
           _1 = Un { us: const 1_u32 };
           StorageDead(_2);
           StorageLive(_3);
diff --git a/tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-unwind.diff b/tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-unwind.diff
index 570ec129f06..b596e25ddfd 100644
--- a/tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-unwind.diff
+++ b/tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-unwind.diff
@@ -18,7 +18,7 @@
       bb0: {
           StorageLive(_1);
           StorageLive(_2);
-          _2 = const 1_u32;
+          nop;
           _1 = Un { us: const 1_u32 };
           StorageDead(_2);
           StorageLive(_3);
diff --git a/tests/mir-opt/inline/issue_106141.outer.Inline.panic-abort.diff b/tests/mir-opt/inline/issue_106141.outer.Inline.panic-abort.diff
index 24e5587f963..2ab79cc2a50 100644
--- a/tests/mir-opt/inline/issue_106141.outer.Inline.panic-abort.diff
+++ b/tests/mir-opt/inline/issue_106141.outer.Inline.panic-abort.diff
@@ -8,7 +8,7 @@
 +         let mut _2: bool;
 +         let mut _3: bool;
 +         scope 2 {
-+             debug buffer => const inner::promoted[0];
++             debug buffer => _1;
 +             scope 3 {
 +                 debug index => _0;
 +             }
diff --git a/tests/mir-opt/inline/issue_106141.outer.Inline.panic-unwind.diff b/tests/mir-opt/inline/issue_106141.outer.Inline.panic-unwind.diff
index 4eb8a5c9847..4d96a862885 100644
--- a/tests/mir-opt/inline/issue_106141.outer.Inline.panic-unwind.diff
+++ b/tests/mir-opt/inline/issue_106141.outer.Inline.panic-unwind.diff
@@ -8,7 +8,7 @@
 +         let mut _2: bool;
 +         let mut _3: bool;
 +         scope 2 {
-+             debug buffer => const inner::promoted[0];
++             debug buffer => _1;
 +             scope 3 {
 +                 debug index => _0;
 +             }
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 b2539f391d1..1f88339b586 100644
--- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff
+++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff
@@ -29,8 +29,8 @@
           _3 = &_4;
           _2 = move _3 as &[T] (PointerCoercion(Unsize));
           StorageDead(_3);
-          _5 = const 3_usize;
-          _6 = const true;
+          nop;
+          nop;
           goto -> bb2;
       }
   
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 ff7f12c093c..19a581ba3f0 100644
--- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff
+++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff
@@ -29,8 +29,8 @@
           _3 = &_4;
           _2 = move _3 as &[T] (PointerCoercion(Unsize));
           StorageDead(_3);
-          _5 = const 3_usize;
-          _6 = const true;
+          nop;
+          nop;
           goto -> bb2;
       }
   
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 17d83752fc1..465cb1a9b1f 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
@@ -23,16 +23,15 @@
               let mut _12: isize;
               let _13: std::alloc::AllocError;
               let mut _14: !;
-              let _15: &str;
-              let mut _16: &dyn std::fmt::Debug;
-              let mut _17: &std::alloc::AllocError;
+              let mut _15: &dyn std::fmt::Debug;
+              let mut _16: &std::alloc::AllocError;
               scope 7 {
               }
               scope 8 {
               }
           }
           scope 9 (inlined NonNull::<[u8]>::as_ptr) {
-              let mut _18: *const [u8];
+              let mut _17: *const [u8];
           }
       }
       scope 3 (inlined #[track_caller] Option::<Layout>::unwrap) {
@@ -87,36 +86,33 @@
           StorageDead(_8);
           StorageDead(_7);
           StorageLive(_12);
-          StorageLive(_15);
           _12 = discriminant(_6);
           switchInt(move _12) -> [0: bb6, 1: bb5, otherwise: bb1];
       }
   
       bb5: {
-          _15 = const "called `Result::unwrap()` on an `Err` value";
+          StorageLive(_15);
           StorageLive(_16);
-          StorageLive(_17);
-          _17 = &_13;
-          _16 = move _17 as &dyn std::fmt::Debug (PointerCoercion(Unsize));
-          StorageDead(_17);
-          _14 = result::unwrap_failed(move _15, move _16) -> unwind unreachable;
+          _16 = &_13;
+          _15 = move _16 as &dyn std::fmt::Debug (PointerCoercion(Unsize));
+          StorageDead(_16);
+          _14 = result::unwrap_failed(const "called `Result::unwrap()` on an `Err` value", move _15) -> unwind unreachable;
       }
   
       bb6: {
           _5 = move ((_6 as Ok).0: std::ptr::NonNull<[u8]>);
-          StorageDead(_15);
           StorageDead(_12);
           StorageDead(_6);
--         StorageLive(_18);
+-         StorageLive(_17);
 +         nop;
-          _18 = (_5.0: *const [u8]);
--         _4 = move _18 as *mut [u8] (PtrToPtr);
--         StorageDead(_18);
-+         _4 = _18 as *mut [u8] (PtrToPtr);
+          _17 = (_5.0: *const [u8]);
+-         _4 = move _17 as *mut [u8] (PtrToPtr);
+-         StorageDead(_17);
++         _4 = _17 as *mut [u8] (PtrToPtr);
 +         nop;
           StorageDead(_5);
 -         _3 = move _4 as *mut u8 (PtrToPtr);
-+         _3 = _18 as *mut u8 (PtrToPtr);
++         _3 = _17 as *mut u8 (PtrToPtr);
           StorageDead(_4);
           StorageDead(_3);
 -         StorageDead(_1);
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 1cf950402c3..925d8997b8a 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
@@ -23,16 +23,15 @@
               let mut _12: isize;
               let _13: std::alloc::AllocError;
               let mut _14: !;
-              let _15: &str;
-              let mut _16: &dyn std::fmt::Debug;
-              let mut _17: &std::alloc::AllocError;
+              let mut _15: &dyn std::fmt::Debug;
+              let mut _16: &std::alloc::AllocError;
               scope 7 {
               }
               scope 8 {
               }
           }
           scope 9 (inlined NonNull::<[u8]>::as_ptr) {
-              let mut _18: *const [u8];
+              let mut _17: *const [u8];
           }
       }
       scope 3 (inlined #[track_caller] Option::<Layout>::unwrap) {
@@ -87,36 +86,33 @@
           StorageDead(_8);
           StorageDead(_7);
           StorageLive(_12);
-          StorageLive(_15);
           _12 = discriminant(_6);
           switchInt(move _12) -> [0: bb6, 1: bb5, otherwise: bb1];
       }
   
       bb5: {
-          _15 = const "called `Result::unwrap()` on an `Err` value";
+          StorageLive(_15);
           StorageLive(_16);
-          StorageLive(_17);
-          _17 = &_13;
-          _16 = move _17 as &dyn std::fmt::Debug (PointerCoercion(Unsize));
-          StorageDead(_17);
-          _14 = result::unwrap_failed(move _15, move _16) -> unwind unreachable;
+          _16 = &_13;
+          _15 = move _16 as &dyn std::fmt::Debug (PointerCoercion(Unsize));
+          StorageDead(_16);
+          _14 = result::unwrap_failed(const "called `Result::unwrap()` on an `Err` value", move _15) -> unwind unreachable;
       }
   
       bb6: {
           _5 = move ((_6 as Ok).0: std::ptr::NonNull<[u8]>);
-          StorageDead(_15);
           StorageDead(_12);
           StorageDead(_6);
--         StorageLive(_18);
+-         StorageLive(_17);
 +         nop;
-          _18 = (_5.0: *const [u8]);
--         _4 = move _18 as *mut [u8] (PtrToPtr);
--         StorageDead(_18);
-+         _4 = _18 as *mut [u8] (PtrToPtr);
+          _17 = (_5.0: *const [u8]);
+-         _4 = move _17 as *mut [u8] (PtrToPtr);
+-         StorageDead(_17);
++         _4 = _17 as *mut [u8] (PtrToPtr);
 +         nop;
           StorageDead(_5);
 -         _3 = move _4 as *mut u8 (PtrToPtr);
-+         _3 = _18 as *mut u8 (PtrToPtr);
++         _3 = _17 as *mut u8 (PtrToPtr);
           StorageDead(_4);
           StorageDead(_3);
 -         StorageDead(_1);
diff --git a/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-abort.mir
index d979c5ec1d5..8f41fb70925 100644
--- a/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-abort.mir
+++ b/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-abort.mir
@@ -4,35 +4,34 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
     debug slice => _1;
     debug f => _2;
     let mut _0: ();
-    let mut _12: std::slice::Iter<'_, T>;
+    let mut _11: std::slice::Iter<'_, T>;
+    let mut _12: std::iter::Enumerate<std::slice::Iter<'_, T>>;
     let mut _13: std::iter::Enumerate<std::slice::Iter<'_, T>>;
-    let mut _14: std::iter::Enumerate<std::slice::Iter<'_, T>>;
-    let mut _15: &mut std::iter::Enumerate<std::slice::Iter<'_, T>>;
-    let mut _16: std::option::Option<(usize, &T)>;
-    let mut _17: isize;
-    let mut _20: &impl Fn(usize, &T);
-    let mut _21: (usize, &T);
-    let _22: ();
+    let mut _14: &mut std::iter::Enumerate<std::slice::Iter<'_, T>>;
+    let mut _15: std::option::Option<(usize, &T)>;
+    let mut _16: isize;
+    let mut _19: &impl Fn(usize, &T);
+    let mut _20: (usize, &T);
+    let _21: ();
     scope 1 {
-        debug iter => _14;
-        let _18: usize;
-        let _19: &T;
+        debug iter => _13;
+        let _17: usize;
+        let _18: &T;
         scope 2 {
-            debug i => _18;
-            debug x => _19;
+            debug i => _17;
+            debug x => _18;
         }
     }
     scope 3 (inlined core::slice::<impl [T]>::iter) {
         scope 4 (inlined std::slice::Iter::<'_, T>::new) {
             let _3: usize;
-            let mut _7: bool;
+            let mut _7: *mut T;
             let mut _8: *mut T;
-            let mut _9: *mut T;
-            let mut _11: *const T;
+            let mut _10: *const T;
             scope 5 {
                 let _6: std::ptr::NonNull<T>;
                 scope 6 {
-                    let _10: *const T;
+                    let _9: *const T;
                     scope 7 {
                     }
                     scope 11 (inlined without_provenance::<T>) {
@@ -61,7 +60,7 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
     }
 
     bb0: {
-        StorageLive(_12);
+        StorageLive(_11);
         StorageLive(_3);
         StorageLive(_6);
         StorageLive(_4);
@@ -70,62 +69,59 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
         _4 = &raw const (*_1);
         _5 = _4 as *const T (PtrToPtr);
         _6 = NonNull::<T> { pointer: _5 };
-        StorageLive(_10);
-        StorageLive(_7);
-        _7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
-        switchInt(move _7) -> [0: bb1, otherwise: bb2];
+        StorageLive(_9);
+        switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
     }
 
     bb1: {
-        StorageLive(_9);
         StorageLive(_8);
-        _8 = _4 as *mut T (PtrToPtr);
-        _9 = Offset(_8, _3);
+        StorageLive(_7);
+        _7 = _4 as *mut T (PtrToPtr);
+        _8 = Offset(_7, _3);
+        StorageDead(_7);
+        _9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
         StorageDead(_8);
-        _10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
-        StorageDead(_9);
         goto -> bb3;
     }
 
     bb2: {
-        _10 = _3 as *const T (Transmute);
+        _9 = _3 as *const T (Transmute);
         goto -> bb3;
     }
 
     bb3: {
-        StorageDead(_7);
-        StorageLive(_11);
-        _11 = _10;
-        _12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
-        StorageDead(_11);
+        StorageLive(_10);
+        _10 = _9;
+        _11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
         StorageDead(_10);
+        StorageDead(_9);
         StorageDead(_5);
         StorageDead(_4);
         StorageDead(_6);
         StorageDead(_3);
-        _13 = Enumerate::<std::slice::Iter<'_, T>> { iter: _12, count: const 0_usize };
-        StorageDead(_12);
-        StorageLive(_14);
-        _14 = _13;
+        _12 = Enumerate::<std::slice::Iter<'_, T>> { iter: _11, count: const 0_usize };
+        StorageDead(_11);
+        StorageLive(_13);
+        _13 = _12;
         goto -> bb4;
     }
 
     bb4: {
-        StorageLive(_16);
         StorageLive(_15);
-        _15 = &mut _14;
-        _16 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(move _15) -> [return: bb5, unwind unreachable];
+        StorageLive(_14);
+        _14 = &mut _13;
+        _15 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(move _14) -> [return: bb5, unwind unreachable];
     }
 
     bb5: {
-        StorageDead(_15);
-        _17 = discriminant(_16);
-        switchInt(move _17) -> [0: bb6, 1: bb8, otherwise: bb10];
+        StorageDead(_14);
+        _16 = discriminant(_15);
+        switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
     }
 
     bb6: {
-        StorageDead(_16);
-        StorageDead(_14);
+        StorageDead(_15);
+        StorageDead(_13);
         drop(_2) -> [return: bb7, unwind unreachable];
     }
 
@@ -134,19 +130,19 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
     }
 
     bb8: {
-        _18 = (((_16 as Some).0: (usize, &T)).0: usize);
-        _19 = (((_16 as Some).0: (usize, &T)).1: &T);
+        _17 = (((_15 as Some).0: (usize, &T)).0: usize);
+        _18 = (((_15 as Some).0: (usize, &T)).1: &T);
+        StorageLive(_19);
+        _19 = &_2;
         StorageLive(_20);
-        _20 = &_2;
-        StorageLive(_21);
-        _21 = (_18, _19);
-        _22 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _20, move _21) -> [return: bb9, unwind unreachable];
+        _20 = (_17, _18);
+        _21 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _19, move _20) -> [return: bb9, unwind unreachable];
     }
 
     bb9: {
-        StorageDead(_21);
         StorageDead(_20);
-        StorageDead(_16);
+        StorageDead(_19);
+        StorageDead(_15);
         goto -> bb4;
     }
 
diff --git a/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-unwind.mir
index 8491c49f767..17cf305468e 100644
--- a/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-unwind.mir
+++ b/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-unwind.mir
@@ -4,35 +4,34 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
     debug slice => _1;
     debug f => _2;
     let mut _0: ();
-    let mut _12: std::slice::Iter<'_, T>;
+    let mut _11: std::slice::Iter<'_, T>;
+    let mut _12: std::iter::Enumerate<std::slice::Iter<'_, T>>;
     let mut _13: std::iter::Enumerate<std::slice::Iter<'_, T>>;
-    let mut _14: std::iter::Enumerate<std::slice::Iter<'_, T>>;
-    let mut _15: &mut std::iter::Enumerate<std::slice::Iter<'_, T>>;
-    let mut _16: std::option::Option<(usize, &T)>;
-    let mut _17: isize;
-    let mut _20: &impl Fn(usize, &T);
-    let mut _21: (usize, &T);
-    let _22: ();
+    let mut _14: &mut std::iter::Enumerate<std::slice::Iter<'_, T>>;
+    let mut _15: std::option::Option<(usize, &T)>;
+    let mut _16: isize;
+    let mut _19: &impl Fn(usize, &T);
+    let mut _20: (usize, &T);
+    let _21: ();
     scope 1 {
-        debug iter => _14;
-        let _18: usize;
-        let _19: &T;
+        debug iter => _13;
+        let _17: usize;
+        let _18: &T;
         scope 2 {
-            debug i => _18;
-            debug x => _19;
+            debug i => _17;
+            debug x => _18;
         }
     }
     scope 3 (inlined core::slice::<impl [T]>::iter) {
         scope 4 (inlined std::slice::Iter::<'_, T>::new) {
             let _3: usize;
-            let mut _7: bool;
+            let mut _7: *mut T;
             let mut _8: *mut T;
-            let mut _9: *mut T;
-            let mut _11: *const T;
+            let mut _10: *const T;
             scope 5 {
                 let _6: std::ptr::NonNull<T>;
                 scope 6 {
-                    let _10: *const T;
+                    let _9: *const T;
                     scope 7 {
                     }
                     scope 11 (inlined without_provenance::<T>) {
@@ -61,7 +60,7 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
     }
 
     bb0: {
-        StorageLive(_12);
+        StorageLive(_11);
         StorageLive(_3);
         StorageLive(_6);
         StorageLive(_4);
@@ -70,62 +69,59 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
         _4 = &raw const (*_1);
         _5 = _4 as *const T (PtrToPtr);
         _6 = NonNull::<T> { pointer: _5 };
-        StorageLive(_10);
-        StorageLive(_7);
-        _7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
-        switchInt(move _7) -> [0: bb1, otherwise: bb2];
+        StorageLive(_9);
+        switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
     }
 
     bb1: {
-        StorageLive(_9);
         StorageLive(_8);
-        _8 = _4 as *mut T (PtrToPtr);
-        _9 = Offset(_8, _3);
+        StorageLive(_7);
+        _7 = _4 as *mut T (PtrToPtr);
+        _8 = Offset(_7, _3);
+        StorageDead(_7);
+        _9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
         StorageDead(_8);
-        _10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
-        StorageDead(_9);
         goto -> bb3;
     }
 
     bb2: {
-        _10 = _3 as *const T (Transmute);
+        _9 = _3 as *const T (Transmute);
         goto -> bb3;
     }
 
     bb3: {
-        StorageDead(_7);
-        StorageLive(_11);
-        _11 = _10;
-        _12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
-        StorageDead(_11);
+        StorageLive(_10);
+        _10 = _9;
+        _11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
         StorageDead(_10);
+        StorageDead(_9);
         StorageDead(_5);
         StorageDead(_4);
         StorageDead(_6);
         StorageDead(_3);
-        _13 = Enumerate::<std::slice::Iter<'_, T>> { iter: _12, count: const 0_usize };
-        StorageDead(_12);
-        StorageLive(_14);
-        _14 = _13;
+        _12 = Enumerate::<std::slice::Iter<'_, T>> { iter: _11, count: const 0_usize };
+        StorageDead(_11);
+        StorageLive(_13);
+        _13 = _12;
         goto -> bb4;
     }
 
     bb4: {
-        StorageLive(_16);
         StorageLive(_15);
-        _15 = &mut _14;
-        _16 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(move _15) -> [return: bb5, unwind: bb11];
+        StorageLive(_14);
+        _14 = &mut _13;
+        _15 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(move _14) -> [return: bb5, unwind: bb11];
     }
 
     bb5: {
-        StorageDead(_15);
-        _17 = discriminant(_16);
-        switchInt(move _17) -> [0: bb6, 1: bb8, otherwise: bb10];
+        StorageDead(_14);
+        _16 = discriminant(_15);
+        switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
     }
 
     bb6: {
-        StorageDead(_16);
-        StorageDead(_14);
+        StorageDead(_15);
+        StorageDead(_13);
         drop(_2) -> [return: bb7, unwind continue];
     }
 
@@ -134,19 +130,19 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
     }
 
     bb8: {
-        _18 = (((_16 as Some).0: (usize, &T)).0: usize);
-        _19 = (((_16 as Some).0: (usize, &T)).1: &T);
+        _17 = (((_15 as Some).0: (usize, &T)).0: usize);
+        _18 = (((_15 as Some).0: (usize, &T)).1: &T);
+        StorageLive(_19);
+        _19 = &_2;
         StorageLive(_20);
-        _20 = &_2;
-        StorageLive(_21);
-        _21 = (_18, _19);
-        _22 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _20, move _21) -> [return: bb9, unwind: bb11];
+        _20 = (_17, _18);
+        _21 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _19, move _20) -> [return: bb9, unwind: bb11];
     }
 
     bb9: {
-        StorageDead(_21);
         StorageDead(_20);
-        StorageDead(_16);
+        StorageDead(_19);
+        StorageDead(_15);
         goto -> bb4;
     }
 
diff --git a/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-abort.mir
index 67dd0c85ea1..3c1bbdc8742 100644
--- a/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-abort.mir
+++ b/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-abort.mir
@@ -4,32 +4,31 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
     debug slice => _1;
     debug f => _2;
     let mut _0: ();
+    let mut _11: std::slice::Iter<'_, T>;
     let mut _12: std::slice::Iter<'_, T>;
-    let mut _13: std::slice::Iter<'_, T>;
-    let mut _14: &mut std::slice::Iter<'_, T>;
-    let mut _15: std::option::Option<&T>;
-    let mut _16: isize;
-    let mut _18: &impl Fn(&T);
-    let mut _19: (&T,);
-    let _20: ();
+    let mut _13: &mut std::slice::Iter<'_, T>;
+    let mut _14: std::option::Option<&T>;
+    let mut _15: isize;
+    let mut _17: &impl Fn(&T);
+    let mut _18: (&T,);
+    let _19: ();
     scope 1 {
-        debug iter => _13;
-        let _17: &T;
+        debug iter => _12;
+        let _16: &T;
         scope 2 {
-            debug x => _17;
+            debug x => _16;
         }
     }
     scope 3 (inlined core::slice::<impl [T]>::iter) {
         scope 4 (inlined std::slice::Iter::<'_, T>::new) {
             let _3: usize;
-            let mut _7: bool;
+            let mut _7: *mut T;
             let mut _8: *mut T;
-            let mut _9: *mut T;
-            let mut _11: *const T;
+            let mut _10: *const T;
             scope 5 {
                 let _6: std::ptr::NonNull<T>;
                 scope 6 {
-                    let _10: *const T;
+                    let _9: *const T;
                     scope 7 {
                     }
                     scope 11 (inlined without_provenance::<T>) {
@@ -62,60 +61,57 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
         _4 = &raw const (*_1);
         _5 = _4 as *const T (PtrToPtr);
         _6 = NonNull::<T> { pointer: _5 };
-        StorageLive(_10);
-        StorageLive(_7);
-        _7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
-        switchInt(move _7) -> [0: bb1, otherwise: bb2];
+        StorageLive(_9);
+        switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
     }
 
     bb1: {
-        StorageLive(_9);
         StorageLive(_8);
-        _8 = _4 as *mut T (PtrToPtr);
-        _9 = Offset(_8, _3);
+        StorageLive(_7);
+        _7 = _4 as *mut T (PtrToPtr);
+        _8 = Offset(_7, _3);
+        StorageDead(_7);
+        _9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
         StorageDead(_8);
-        _10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
-        StorageDead(_9);
         goto -> bb3;
     }
 
     bb2: {
-        _10 = _3 as *const T (Transmute);
+        _9 = _3 as *const T (Transmute);
         goto -> bb3;
     }
 
     bb3: {
-        StorageDead(_7);
-        StorageLive(_11);
-        _11 = _10;
-        _12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
-        StorageDead(_11);
+        StorageLive(_10);
+        _10 = _9;
+        _11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
         StorageDead(_10);
+        StorageDead(_9);
         StorageDead(_5);
         StorageDead(_4);
         StorageDead(_6);
         StorageDead(_3);
-        StorageLive(_13);
-        _13 = _12;
+        StorageLive(_12);
+        _12 = _11;
         goto -> bb4;
     }
 
     bb4: {
-        StorageLive(_15);
         StorageLive(_14);
-        _14 = &mut _13;
-        _15 = <std::slice::Iter<'_, T> as Iterator>::next(move _14) -> [return: bb5, unwind unreachable];
+        StorageLive(_13);
+        _13 = &mut _12;
+        _14 = <std::slice::Iter<'_, T> as Iterator>::next(move _13) -> [return: bb5, unwind unreachable];
     }
 
     bb5: {
-        StorageDead(_14);
-        _16 = discriminant(_15);
-        switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
+        StorageDead(_13);
+        _15 = discriminant(_14);
+        switchInt(move _15) -> [0: bb6, 1: bb8, otherwise: bb10];
     }
 
     bb6: {
-        StorageDead(_15);
-        StorageDead(_13);
+        StorageDead(_14);
+        StorageDead(_12);
         drop(_2) -> [return: bb7, unwind unreachable];
     }
 
@@ -124,18 +120,18 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
     }
 
     bb8: {
-        _17 = ((_15 as Some).0: &T);
+        _16 = ((_14 as Some).0: &T);
+        StorageLive(_17);
+        _17 = &_2;
         StorageLive(_18);
-        _18 = &_2;
-        StorageLive(_19);
-        _19 = (_17,);
-        _20 = <impl Fn(&T) as Fn<(&T,)>>::call(move _18, move _19) -> [return: bb9, unwind unreachable];
+        _18 = (_16,);
+        _19 = <impl Fn(&T) as Fn<(&T,)>>::call(move _17, move _18) -> [return: bb9, unwind unreachable];
     }
 
     bb9: {
-        StorageDead(_19);
         StorageDead(_18);
-        StorageDead(_15);
+        StorageDead(_17);
+        StorageDead(_14);
         goto -> bb4;
     }
 
diff --git a/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-unwind.mir
index 7c41e9e1f1b..b2ec1ea7b9f 100644
--- a/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-unwind.mir
+++ b/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.panic-unwind.mir
@@ -4,32 +4,31 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
     debug slice => _1;
     debug f => _2;
     let mut _0: ();
+    let mut _11: std::slice::Iter<'_, T>;
     let mut _12: std::slice::Iter<'_, T>;
-    let mut _13: std::slice::Iter<'_, T>;
-    let mut _14: &mut std::slice::Iter<'_, T>;
-    let mut _15: std::option::Option<&T>;
-    let mut _16: isize;
-    let mut _18: &impl Fn(&T);
-    let mut _19: (&T,);
-    let _20: ();
+    let mut _13: &mut std::slice::Iter<'_, T>;
+    let mut _14: std::option::Option<&T>;
+    let mut _15: isize;
+    let mut _17: &impl Fn(&T);
+    let mut _18: (&T,);
+    let _19: ();
     scope 1 {
-        debug iter => _13;
-        let _17: &T;
+        debug iter => _12;
+        let _16: &T;
         scope 2 {
-            debug x => _17;
+            debug x => _16;
         }
     }
     scope 3 (inlined core::slice::<impl [T]>::iter) {
         scope 4 (inlined std::slice::Iter::<'_, T>::new) {
             let _3: usize;
-            let mut _7: bool;
+            let mut _7: *mut T;
             let mut _8: *mut T;
-            let mut _9: *mut T;
-            let mut _11: *const T;
+            let mut _10: *const T;
             scope 5 {
                 let _6: std::ptr::NonNull<T>;
                 scope 6 {
-                    let _10: *const T;
+                    let _9: *const T;
                     scope 7 {
                     }
                     scope 11 (inlined without_provenance::<T>) {
@@ -62,60 +61,57 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
         _4 = &raw const (*_1);
         _5 = _4 as *const T (PtrToPtr);
         _6 = NonNull::<T> { pointer: _5 };
-        StorageLive(_10);
-        StorageLive(_7);
-        _7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
-        switchInt(move _7) -> [0: bb1, otherwise: bb2];
+        StorageLive(_9);
+        switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
     }
 
     bb1: {
-        StorageLive(_9);
         StorageLive(_8);
-        _8 = _4 as *mut T (PtrToPtr);
-        _9 = Offset(_8, _3);
+        StorageLive(_7);
+        _7 = _4 as *mut T (PtrToPtr);
+        _8 = Offset(_7, _3);
+        StorageDead(_7);
+        _9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
         StorageDead(_8);
-        _10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
-        StorageDead(_9);
         goto -> bb3;
     }
 
     bb2: {
-        _10 = _3 as *const T (Transmute);
+        _9 = _3 as *const T (Transmute);
         goto -> bb3;
     }
 
     bb3: {
-        StorageDead(_7);
-        StorageLive(_11);
-        _11 = _10;
-        _12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
-        StorageDead(_11);
+        StorageLive(_10);
+        _10 = _9;
+        _11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
         StorageDead(_10);
+        StorageDead(_9);
         StorageDead(_5);
         StorageDead(_4);
         StorageDead(_6);
         StorageDead(_3);
-        StorageLive(_13);
-        _13 = _12;
+        StorageLive(_12);
+        _12 = _11;
         goto -> bb4;
     }
 
     bb4: {
-        StorageLive(_15);
         StorageLive(_14);
-        _14 = &mut _13;
-        _15 = <std::slice::Iter<'_, T> as Iterator>::next(move _14) -> [return: bb5, unwind: bb11];
+        StorageLive(_13);
+        _13 = &mut _12;
+        _14 = <std::slice::Iter<'_, T> as Iterator>::next(move _13) -> [return: bb5, unwind: bb11];
     }
 
     bb5: {
-        StorageDead(_14);
-        _16 = discriminant(_15);
-        switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
+        StorageDead(_13);
+        _15 = discriminant(_14);
+        switchInt(move _15) -> [0: bb6, 1: bb8, otherwise: bb10];
     }
 
     bb6: {
-        StorageDead(_15);
-        StorageDead(_13);
+        StorageDead(_14);
+        StorageDead(_12);
         drop(_2) -> [return: bb7, unwind continue];
     }
 
@@ -124,18 +120,18 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
     }
 
     bb8: {
-        _17 = ((_15 as Some).0: &T);
+        _16 = ((_14 as Some).0: &T);
+        StorageLive(_17);
+        _17 = &_2;
         StorageLive(_18);
-        _18 = &_2;
-        StorageLive(_19);
-        _19 = (_17,);
-        _20 = <impl Fn(&T) as Fn<(&T,)>>::call(move _18, move _19) -> [return: bb9, unwind: bb11];
+        _18 = (_16,);
+        _19 = <impl Fn(&T) as Fn<(&T,)>>::call(move _17, move _18) -> [return: bb9, unwind: bb11];
     }
 
     bb9: {
-        StorageDead(_19);
         StorageDead(_18);
-        StorageDead(_15);
+        StorageDead(_17);
+        StorageDead(_14);
         goto -> bb4;
     }
 
diff --git a/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-abort.mir
index ffeef1e04a1..bf982f076de 100644
--- a/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-abort.mir
+++ b/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-abort.mir
@@ -4,35 +4,34 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
     debug slice => _1;
     debug f => _2;
     let mut _0: ();
-    let mut _12: std::slice::Iter<'_, T>;
+    let mut _11: std::slice::Iter<'_, T>;
+    let mut _12: std::iter::Rev<std::slice::Iter<'_, T>>;
     let mut _13: std::iter::Rev<std::slice::Iter<'_, T>>;
-    let mut _14: std::iter::Rev<std::slice::Iter<'_, T>>;
-    let mut _16: std::option::Option<&T>;
-    let mut _17: isize;
-    let mut _19: &impl Fn(&T);
-    let mut _20: (&T,);
-    let _21: ();
+    let mut _15: std::option::Option<&T>;
+    let mut _16: isize;
+    let mut _18: &impl Fn(&T);
+    let mut _19: (&T,);
+    let _20: ();
     scope 1 {
-        debug iter => _14;
-        let _18: &T;
+        debug iter => _13;
+        let _17: &T;
         scope 2 {
-            debug x => _18;
+            debug x => _17;
         }
         scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
-            let mut _15: &mut std::slice::Iter<'_, T>;
+            let mut _14: &mut std::slice::Iter<'_, T>;
         }
     }
     scope 3 (inlined core::slice::<impl [T]>::iter) {
         scope 4 (inlined std::slice::Iter::<'_, T>::new) {
             let _3: usize;
-            let mut _7: bool;
+            let mut _7: *mut T;
             let mut _8: *mut T;
-            let mut _9: *mut T;
-            let mut _11: *const T;
+            let mut _10: *const T;
             scope 5 {
                 let _6: std::ptr::NonNull<T>;
                 scope 6 {
-                    let _10: *const T;
+                    let _9: *const T;
                     scope 7 {
                     }
                     scope 11 (inlined without_provenance::<T>) {
@@ -61,7 +60,7 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
     }
 
     bb0: {
-        StorageLive(_12);
+        StorageLive(_11);
         StorageLive(_3);
         StorageLive(_6);
         StorageLive(_4);
@@ -70,62 +69,59 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
         _4 = &raw const (*_1);
         _5 = _4 as *const T (PtrToPtr);
         _6 = NonNull::<T> { pointer: _5 };
-        StorageLive(_10);
-        StorageLive(_7);
-        _7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
-        switchInt(move _7) -> [0: bb1, otherwise: bb2];
+        StorageLive(_9);
+        switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
     }
 
     bb1: {
-        StorageLive(_9);
         StorageLive(_8);
-        _8 = _4 as *mut T (PtrToPtr);
-        _9 = Offset(_8, _3);
+        StorageLive(_7);
+        _7 = _4 as *mut T (PtrToPtr);
+        _8 = Offset(_7, _3);
+        StorageDead(_7);
+        _9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
         StorageDead(_8);
-        _10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
-        StorageDead(_9);
         goto -> bb3;
     }
 
     bb2: {
-        _10 = _3 as *const T (Transmute);
+        _9 = _3 as *const T (Transmute);
         goto -> bb3;
     }
 
     bb3: {
-        StorageDead(_7);
-        StorageLive(_11);
-        _11 = _10;
-        _12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
-        StorageDead(_11);
+        StorageLive(_10);
+        _10 = _9;
+        _11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
         StorageDead(_10);
+        StorageDead(_9);
         StorageDead(_5);
         StorageDead(_4);
         StorageDead(_6);
         StorageDead(_3);
-        _13 = Rev::<std::slice::Iter<'_, T>> { iter: _12 };
-        StorageDead(_12);
-        StorageLive(_14);
-        _14 = _13;
+        _12 = Rev::<std::slice::Iter<'_, T>> { iter: _11 };
+        StorageDead(_11);
+        StorageLive(_13);
+        _13 = _12;
         goto -> bb4;
     }
 
     bb4: {
-        StorageLive(_16);
         StorageLive(_15);
-        _15 = &mut (_14.0: std::slice::Iter<'_, T>);
-        _16 = <std::slice::Iter<'_, T> as DoubleEndedIterator>::next_back(move _15) -> [return: bb5, unwind unreachable];
+        StorageLive(_14);
+        _14 = &mut (_13.0: std::slice::Iter<'_, T>);
+        _15 = <std::slice::Iter<'_, T> as DoubleEndedIterator>::next_back(move _14) -> [return: bb5, unwind unreachable];
     }
 
     bb5: {
-        StorageDead(_15);
-        _17 = discriminant(_16);
-        switchInt(move _17) -> [0: bb6, 1: bb8, otherwise: bb10];
+        StorageDead(_14);
+        _16 = discriminant(_15);
+        switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
     }
 
     bb6: {
-        StorageDead(_16);
-        StorageDead(_14);
+        StorageDead(_15);
+        StorageDead(_13);
         drop(_2) -> [return: bb7, unwind unreachable];
     }
 
@@ -134,18 +130,18 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
     }
 
     bb8: {
-        _18 = ((_16 as Some).0: &T);
+        _17 = ((_15 as Some).0: &T);
+        StorageLive(_18);
+        _18 = &_2;
         StorageLive(_19);
-        _19 = &_2;
-        StorageLive(_20);
-        _20 = (_18,);
-        _21 = <impl Fn(&T) as Fn<(&T,)>>::call(move _19, move _20) -> [return: bb9, unwind unreachable];
+        _19 = (_17,);
+        _20 = <impl Fn(&T) as Fn<(&T,)>>::call(move _18, move _19) -> [return: bb9, unwind unreachable];
     }
 
     bb9: {
-        StorageDead(_20);
         StorageDead(_19);
-        StorageDead(_16);
+        StorageDead(_18);
+        StorageDead(_15);
         goto -> bb4;
     }
 
diff --git a/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-unwind.mir
index c7cd37afd86..532b8162521 100644
--- a/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-unwind.mir
+++ b/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.panic-unwind.mir
@@ -4,35 +4,34 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
     debug slice => _1;
     debug f => _2;
     let mut _0: ();
-    let mut _12: std::slice::Iter<'_, T>;
+    let mut _11: std::slice::Iter<'_, T>;
+    let mut _12: std::iter::Rev<std::slice::Iter<'_, T>>;
     let mut _13: std::iter::Rev<std::slice::Iter<'_, T>>;
-    let mut _14: std::iter::Rev<std::slice::Iter<'_, T>>;
-    let mut _16: std::option::Option<&T>;
-    let mut _17: isize;
-    let mut _19: &impl Fn(&T);
-    let mut _20: (&T,);
-    let _21: ();
+    let mut _15: std::option::Option<&T>;
+    let mut _16: isize;
+    let mut _18: &impl Fn(&T);
+    let mut _19: (&T,);
+    let _20: ();
     scope 1 {
-        debug iter => _14;
-        let _18: &T;
+        debug iter => _13;
+        let _17: &T;
         scope 2 {
-            debug x => _18;
+            debug x => _17;
         }
         scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
-            let mut _15: &mut std::slice::Iter<'_, T>;
+            let mut _14: &mut std::slice::Iter<'_, T>;
         }
     }
     scope 3 (inlined core::slice::<impl [T]>::iter) {
         scope 4 (inlined std::slice::Iter::<'_, T>::new) {
             let _3: usize;
-            let mut _7: bool;
+            let mut _7: *mut T;
             let mut _8: *mut T;
-            let mut _9: *mut T;
-            let mut _11: *const T;
+            let mut _10: *const T;
             scope 5 {
                 let _6: std::ptr::NonNull<T>;
                 scope 6 {
-                    let _10: *const T;
+                    let _9: *const T;
                     scope 7 {
                     }
                     scope 11 (inlined without_provenance::<T>) {
@@ -61,7 +60,7 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
     }
 
     bb0: {
-        StorageLive(_12);
+        StorageLive(_11);
         StorageLive(_3);
         StorageLive(_6);
         StorageLive(_4);
@@ -70,62 +69,59 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
         _4 = &raw const (*_1);
         _5 = _4 as *const T (PtrToPtr);
         _6 = NonNull::<T> { pointer: _5 };
-        StorageLive(_10);
-        StorageLive(_7);
-        _7 = const <T as std::mem::SizedTypeProperties>::IS_ZST;
-        switchInt(move _7) -> [0: bb1, otherwise: bb2];
+        StorageLive(_9);
+        switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
     }
 
     bb1: {
-        StorageLive(_9);
         StorageLive(_8);
-        _8 = _4 as *mut T (PtrToPtr);
-        _9 = Offset(_8, _3);
+        StorageLive(_7);
+        _7 = _4 as *mut T (PtrToPtr);
+        _8 = Offset(_7, _3);
+        StorageDead(_7);
+        _9 = move _8 as *const T (PointerCoercion(MutToConstPointer));
         StorageDead(_8);
-        _10 = move _9 as *const T (PointerCoercion(MutToConstPointer));
-        StorageDead(_9);
         goto -> bb3;
     }
 
     bb2: {
-        _10 = _3 as *const T (Transmute);
+        _9 = _3 as *const T (Transmute);
         goto -> bb3;
     }
 
     bb3: {
-        StorageDead(_7);
-        StorageLive(_11);
-        _11 = _10;
-        _12 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _11, _marker: const ZeroSized: PhantomData<&T> };
-        StorageDead(_11);
+        StorageLive(_10);
+        _10 = _9;
+        _11 = std::slice::Iter::<'_, T> { ptr: _6, end_or_len: move _10, _marker: const ZeroSized: PhantomData<&T> };
         StorageDead(_10);
+        StorageDead(_9);
         StorageDead(_5);
         StorageDead(_4);
         StorageDead(_6);
         StorageDead(_3);
-        _13 = Rev::<std::slice::Iter<'_, T>> { iter: _12 };
-        StorageDead(_12);
-        StorageLive(_14);
-        _14 = _13;
+        _12 = Rev::<std::slice::Iter<'_, T>> { iter: _11 };
+        StorageDead(_11);
+        StorageLive(_13);
+        _13 = _12;
         goto -> bb4;
     }
 
     bb4: {
-        StorageLive(_16);
         StorageLive(_15);
-        _15 = &mut (_14.0: std::slice::Iter<'_, T>);
-        _16 = <std::slice::Iter<'_, T> as DoubleEndedIterator>::next_back(move _15) -> [return: bb5, unwind: bb11];
+        StorageLive(_14);
+        _14 = &mut (_13.0: std::slice::Iter<'_, T>);
+        _15 = <std::slice::Iter<'_, T> as DoubleEndedIterator>::next_back(move _14) -> [return: bb5, unwind: bb11];
     }
 
     bb5: {
-        StorageDead(_15);
-        _17 = discriminant(_16);
-        switchInt(move _17) -> [0: bb6, 1: bb8, otherwise: bb10];
+        StorageDead(_14);
+        _16 = discriminant(_15);
+        switchInt(move _16) -> [0: bb6, 1: bb8, otherwise: bb10];
     }
 
     bb6: {
-        StorageDead(_16);
-        StorageDead(_14);
+        StorageDead(_15);
+        StorageDead(_13);
         drop(_2) -> [return: bb7, unwind continue];
     }
 
@@ -134,18 +130,18 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
     }
 
     bb8: {
-        _18 = ((_16 as Some).0: &T);
+        _17 = ((_15 as Some).0: &T);
+        StorageLive(_18);
+        _18 = &_2;
         StorageLive(_19);
-        _19 = &_2;
-        StorageLive(_20);
-        _20 = (_18,);
-        _21 = <impl Fn(&T) as Fn<(&T,)>>::call(move _19, move _20) -> [return: bb9, unwind: bb11];
+        _19 = (_17,);
+        _20 = <impl Fn(&T) as Fn<(&T,)>>::call(move _18, move _19) -> [return: bb9, unwind: bb11];
     }
 
     bb9: {
-        StorageDead(_20);
         StorageDead(_19);
-        StorageDead(_16);
+        StorageDead(_18);
+        StorageDead(_15);
         goto -> bb4;
     }
 
diff --git a/tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-abort.diff b/tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-abort.diff
new file mode 100644
index 00000000000..8818c891e94
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-abort.diff
@@ -0,0 +1,12 @@
+- // MIR for `assign_const_to_return` before SingleUseConsts
++ // MIR for `assign_const_to_return` after SingleUseConsts
+  
+  fn assign_const_to_return() -> bool {
+      let mut _0: bool;
+  
+      bb0: {
+          _0 = const <T as MyTrait>::ASSOC_BOOL;
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-unwind.diff b/tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-unwind.diff
new file mode 100644
index 00000000000..8818c891e94
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-unwind.diff
@@ -0,0 +1,12 @@
+- // MIR for `assign_const_to_return` before SingleUseConsts
++ // MIR for `assign_const_to_return` after SingleUseConsts
+  
+  fn assign_const_to_return() -> bool {
+      let mut _0: bool;
+  
+      bb0: {
+          _0 = const <T as MyTrait>::ASSOC_BOOL;
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-abort.diff b/tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-abort.diff
new file mode 100644
index 00000000000..468076e5ee3
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-abort.diff
@@ -0,0 +1,31 @@
+- // MIR for `if_const` before SingleUseConsts
++ // MIR for `if_const` after SingleUseConsts
+  
+  fn if_const() -> i32 {
+      let mut _0: i32;
+      let mut _1: bool;
+  
+      bb0: {
+          StorageLive(_1);
+-         _1 = const <T as MyTrait>::ASSOC_BOOL;
+-         switchInt(move _1) -> [0: bb2, otherwise: bb1];
++         nop;
++         switchInt(const <T as MyTrait>::ASSOC_BOOL) -> [0: bb2, otherwise: bb1];
+      }
+  
+      bb1: {
+          _0 = const 7_i32;
+          goto -> bb3;
+      }
+  
+      bb2: {
+          _0 = const 42_i32;
+          goto -> bb3;
+      }
+  
+      bb3: {
+          StorageDead(_1);
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-unwind.diff b/tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-unwind.diff
new file mode 100644
index 00000000000..468076e5ee3
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-unwind.diff
@@ -0,0 +1,31 @@
+- // MIR for `if_const` before SingleUseConsts
++ // MIR for `if_const` after SingleUseConsts
+  
+  fn if_const() -> i32 {
+      let mut _0: i32;
+      let mut _1: bool;
+  
+      bb0: {
+          StorageLive(_1);
+-         _1 = const <T as MyTrait>::ASSOC_BOOL;
+-         switchInt(move _1) -> [0: bb2, otherwise: bb1];
++         nop;
++         switchInt(const <T as MyTrait>::ASSOC_BOOL) -> [0: bb2, otherwise: bb1];
+      }
+  
+      bb1: {
+          _0 = const 7_i32;
+          goto -> bb3;
+      }
+  
+      bb2: {
+          _0 = const 42_i32;
+          goto -> bb3;
+      }
+  
+      bb3: {
+          StorageDead(_1);
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-abort.diff b/tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-abort.diff
new file mode 100644
index 00000000000..ad1a2b300f2
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-abort.diff
@@ -0,0 +1,46 @@
+- // MIR for `if_const_debug` before SingleUseConsts
++ // MIR for `if_const_debug` after SingleUseConsts
+  
+  fn if_const_debug() -> i32 {
+      let mut _0: i32;
+      let _1: bool;
+      let _2: ();
+      let mut _3: bool;
+      scope 1 {
+-         debug my_bool => _1;
++         debug my_bool => const <T as MyTrait>::ASSOC_BOOL;
+      }
+  
+      bb0: {
+          StorageLive(_1);
+-         _1 = const <T as MyTrait>::ASSOC_BOOL;
++         nop;
+          StorageLive(_2);
+          _2 = do_whatever() -> [return: bb1, unwind unreachable];
+      }
+  
+      bb1: {
+          StorageDead(_2);
+          StorageLive(_3);
+-         _3 = _1;
++         _3 = const <T as MyTrait>::ASSOC_BOOL;
+          switchInt(move _3) -> [0: bb3, otherwise: bb2];
+      }
+  
+      bb2: {
+          _0 = const 7_i32;
+          goto -> bb4;
+      }
+  
+      bb3: {
+          _0 = const 42_i32;
+          goto -> bb4;
+      }
+  
+      bb4: {
+          StorageDead(_3);
+          StorageDead(_1);
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-unwind.diff b/tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-unwind.diff
new file mode 100644
index 00000000000..827a292e5d0
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-unwind.diff
@@ -0,0 +1,46 @@
+- // MIR for `if_const_debug` before SingleUseConsts
++ // MIR for `if_const_debug` after SingleUseConsts
+  
+  fn if_const_debug() -> i32 {
+      let mut _0: i32;
+      let _1: bool;
+      let _2: ();
+      let mut _3: bool;
+      scope 1 {
+-         debug my_bool => _1;
++         debug my_bool => const <T as MyTrait>::ASSOC_BOOL;
+      }
+  
+      bb0: {
+          StorageLive(_1);
+-         _1 = const <T as MyTrait>::ASSOC_BOOL;
++         nop;
+          StorageLive(_2);
+          _2 = do_whatever() -> [return: bb1, unwind continue];
+      }
+  
+      bb1: {
+          StorageDead(_2);
+          StorageLive(_3);
+-         _3 = _1;
++         _3 = const <T as MyTrait>::ASSOC_BOOL;
+          switchInt(move _3) -> [0: bb3, otherwise: bb2];
+      }
+  
+      bb2: {
+          _0 = const 7_i32;
+          goto -> bb4;
+      }
+  
+      bb3: {
+          _0 = const 42_i32;
+          goto -> bb4;
+      }
+  
+      bb4: {
+          StorageDead(_3);
+          StorageDead(_1);
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-abort.diff b/tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-abort.diff
new file mode 100644
index 00000000000..f7d823af9e3
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-abort.diff
@@ -0,0 +1,14 @@
+- // MIR for `keep_parameter` before SingleUseConsts
++ // MIR for `keep_parameter` after SingleUseConsts
+  
+  fn keep_parameter(_1: i32) -> () {
+      debug other => _1;
+      let mut _0: ();
+  
+      bb0: {
+          _1 = const <T as MyTrait>::ASSOC_INT;
+          _0 = const ();
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-unwind.diff b/tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-unwind.diff
new file mode 100644
index 00000000000..f7d823af9e3
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-unwind.diff
@@ -0,0 +1,14 @@
+- // MIR for `keep_parameter` before SingleUseConsts
++ // MIR for `keep_parameter` after SingleUseConsts
+  
+  fn keep_parameter(_1: i32) -> () {
+      debug other => _1;
+      let mut _0: ();
+  
+      bb0: {
+          _1 = const <T as MyTrait>::ASSOC_INT;
+          _0 = const ();
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-abort.diff b/tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-abort.diff
new file mode 100644
index 00000000000..8d87438a47a
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-abort.diff
@@ -0,0 +1,36 @@
+- // MIR for `match_const` before SingleUseConsts
++ // MIR for `match_const` after SingleUseConsts
+  
+  fn match_const() -> &str {
+      let mut _0: &str;
+      let mut _1: i32;
+  
+      bb0: {
+          StorageLive(_1);
+-         _1 = const <T as MyTrait>::ASSOC_INT;
+-         switchInt(_1) -> [7: bb2, 42: bb3, otherwise: bb1];
++         nop;
++         switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb2, 42: bb3, otherwise: bb1];
+      }
+  
+      bb1: {
+          _0 = const "world";
+          goto -> bb4;
+      }
+  
+      bb2: {
+          _0 = const "hello";
+          goto -> bb4;
+      }
+  
+      bb3: {
+          _0 = const "towel";
+          goto -> bb4;
+      }
+  
+      bb4: {
+          StorageDead(_1);
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-unwind.diff b/tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-unwind.diff
new file mode 100644
index 00000000000..8d87438a47a
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-unwind.diff
@@ -0,0 +1,36 @@
+- // MIR for `match_const` before SingleUseConsts
++ // MIR for `match_const` after SingleUseConsts
+  
+  fn match_const() -> &str {
+      let mut _0: &str;
+      let mut _1: i32;
+  
+      bb0: {
+          StorageLive(_1);
+-         _1 = const <T as MyTrait>::ASSOC_INT;
+-         switchInt(_1) -> [7: bb2, 42: bb3, otherwise: bb1];
++         nop;
++         switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb2, 42: bb3, otherwise: bb1];
+      }
+  
+      bb1: {
+          _0 = const "world";
+          goto -> bb4;
+      }
+  
+      bb2: {
+          _0 = const "hello";
+          goto -> bb4;
+      }
+  
+      bb3: {
+          _0 = const "towel";
+          goto -> bb4;
+      }
+  
+      bb4: {
+          StorageDead(_1);
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-abort.diff b/tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-abort.diff
new file mode 100644
index 00000000000..f192f3feb96
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-abort.diff
@@ -0,0 +1,47 @@
+- // MIR for `match_const_debug` before SingleUseConsts
++ // MIR for `match_const_debug` after SingleUseConsts
+  
+  fn match_const_debug() -> &str {
+      let mut _0: &str;
+      let _1: i32;
+      let _2: ();
+      scope 1 {
+-         debug my_int => _1;
++         debug my_int => const <T as MyTrait>::ASSOC_INT;
+      }
+  
+      bb0: {
+          StorageLive(_1);
+-         _1 = const <T as MyTrait>::ASSOC_INT;
++         nop;
+          StorageLive(_2);
+          _2 = do_whatever() -> [return: bb1, unwind unreachable];
+      }
+  
+      bb1: {
+          StorageDead(_2);
+-         switchInt(_1) -> [7: bb3, 42: bb4, otherwise: bb2];
++         switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb3, 42: bb4, otherwise: bb2];
+      }
+  
+      bb2: {
+          _0 = const "world";
+          goto -> bb5;
+      }
+  
+      bb3: {
+          _0 = const "hello";
+          goto -> bb5;
+      }
+  
+      bb4: {
+          _0 = const "towel";
+          goto -> bb5;
+      }
+  
+      bb5: {
+          StorageDead(_1);
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-unwind.diff b/tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-unwind.diff
new file mode 100644
index 00000000000..261faf415f3
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-unwind.diff
@@ -0,0 +1,47 @@
+- // MIR for `match_const_debug` before SingleUseConsts
++ // MIR for `match_const_debug` after SingleUseConsts
+  
+  fn match_const_debug() -> &str {
+      let mut _0: &str;
+      let _1: i32;
+      let _2: ();
+      scope 1 {
+-         debug my_int => _1;
++         debug my_int => const <T as MyTrait>::ASSOC_INT;
+      }
+  
+      bb0: {
+          StorageLive(_1);
+-         _1 = const <T as MyTrait>::ASSOC_INT;
++         nop;
+          StorageLive(_2);
+          _2 = do_whatever() -> [return: bb1, unwind continue];
+      }
+  
+      bb1: {
+          StorageDead(_2);
+-         switchInt(_1) -> [7: bb3, 42: bb4, otherwise: bb2];
++         switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb3, 42: bb4, otherwise: bb2];
+      }
+  
+      bb2: {
+          _0 = const "world";
+          goto -> bb5;
+      }
+  
+      bb3: {
+          _0 = const "hello";
+          goto -> bb5;
+      }
+  
+      bb4: {
+          _0 = const "towel";
+          goto -> bb5;
+      }
+  
+      bb5: {
+          StorageDead(_1);
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-abort.diff b/tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-abort.diff
new file mode 100644
index 00000000000..8ef94a790a3
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-abort.diff
@@ -0,0 +1,21 @@
+- // MIR for `never_used_debug` before SingleUseConsts
++ // MIR for `never_used_debug` after SingleUseConsts
+  
+  fn never_used_debug() -> () {
+      let mut _0: ();
+      let _1: i32;
+      scope 1 {
+-         debug my_int => _1;
++         debug my_int => const <T as MyTrait>::ASSOC_INT;
+      }
+  
+      bb0: {
+          StorageLive(_1);
+-         _1 = const <T as MyTrait>::ASSOC_INT;
++         nop;
+          _0 = const ();
+          StorageDead(_1);
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-unwind.diff b/tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-unwind.diff
new file mode 100644
index 00000000000..8ef94a790a3
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-unwind.diff
@@ -0,0 +1,21 @@
+- // MIR for `never_used_debug` before SingleUseConsts
++ // MIR for `never_used_debug` after SingleUseConsts
+  
+  fn never_used_debug() -> () {
+      let mut _0: ();
+      let _1: i32;
+      scope 1 {
+-         debug my_int => _1;
++         debug my_int => const <T as MyTrait>::ASSOC_INT;
+      }
+  
+      bb0: {
+          StorageLive(_1);
+-         _1 = const <T as MyTrait>::ASSOC_INT;
++         nop;
+          _0 = const ();
+          StorageDead(_1);
+          return;
+      }
+  }
+  
diff --git a/tests/mir-opt/single_use_consts.rs b/tests/mir-opt/single_use_consts.rs
new file mode 100644
index 00000000000..ecb602c647a
--- /dev/null
+++ b/tests/mir-opt/single_use_consts.rs
@@ -0,0 +1,80 @@
+//@ test-mir-pass: SingleUseConsts
+//@ compile-flags: -C debuginfo=full
+// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
+
+trait MyTrait {
+    const ASSOC_BOOL: bool;
+    const ASSOC_INT: i32;
+}
+
+// EMIT_MIR single_use_consts.if_const.SingleUseConsts.diff
+fn if_const<T: MyTrait>() -> i32 {
+    // CHECK-LABEL: fn if_const(
+    // CHECK: switchInt(const <T as MyTrait>::ASSOC_BOOL)
+    if T::ASSOC_BOOL { 7 } else { 42 }
+}
+
+// EMIT_MIR single_use_consts.match_const.SingleUseConsts.diff
+fn match_const<T: MyTrait>() -> &'static str {
+    // CHECK-LABEL: fn match_const(
+    // CHECK: switchInt(const <T as MyTrait>::ASSOC_INT)
+    match T::ASSOC_INT {
+        7 => "hello",
+        42 => "towel",
+        _ => "world",
+    }
+}
+
+// EMIT_MIR single_use_consts.if_const_debug.SingleUseConsts.diff
+fn if_const_debug<T: MyTrait>() -> i32 {
+    // CHECK-LABEL: fn if_const_debug(
+    // CHECK: my_bool => const <T as MyTrait>::ASSOC_BOOL;
+    // FIXME: `if` forces a temporary (unlike `match`), so the const isn't direct
+    // CHECK: _3 = const <T as MyTrait>::ASSOC_BOOL;
+    // CHECK: switchInt(move _3)
+    let my_bool = T::ASSOC_BOOL;
+    do_whatever();
+    if my_bool { 7 } else { 42 }
+}
+
+// EMIT_MIR single_use_consts.match_const_debug.SingleUseConsts.diff
+fn match_const_debug<T: MyTrait>() -> &'static str {
+    // CHECK-LABEL: fn match_const_debug(
+    // CHECK: my_int => const <T as MyTrait>::ASSOC_INT;
+    // CHECK: switchInt(const <T as MyTrait>::ASSOC_INT)
+    let my_int = T::ASSOC_INT;
+    do_whatever();
+    match my_int {
+        7 => "hello",
+        42 => "towel",
+        _ => "world",
+    }
+}
+
+// EMIT_MIR single_use_consts.never_used_debug.SingleUseConsts.diff
+#[allow(unused_variables)]
+fn never_used_debug<T: MyTrait>() {
+    // CHECK-LABEL: fn never_used_debug(
+    // CHECK: my_int => const <T as MyTrait>::ASSOC_INT;
+    // CHECK-NOT: ASSOC_INT
+    // CHECK: nop
+    // CHECK-NOT: ASSOC_INT
+    let my_int = T::ASSOC_INT;
+}
+
+// EMIT_MIR single_use_consts.assign_const_to_return.SingleUseConsts.diff
+fn assign_const_to_return<T: MyTrait>() -> bool {
+    // CHECK-LABEL: fn assign_const_to_return(
+    // CHECK: _0 = const <T as MyTrait>::ASSOC_BOOL;
+    T::ASSOC_BOOL
+}
+
+// EMIT_MIR single_use_consts.keep_parameter.SingleUseConsts.diff
+fn keep_parameter<T: MyTrait>(mut other: i32) {
+    // CHECK-LABEL: fn keep_parameter(
+    // CHECK: _1 = const <T as MyTrait>::ASSOC_INT;
+    // CHECK: _0 = const ();
+    other = T::ASSOC_INT;
+}
+
+fn do_whatever() {}