diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 696067b1f0e6..e8b86d1327d6 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -1015,23 +1015,32 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> { } fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, location: Location) { - if let StatementKind::Assign(box (_, ref mut rvalue)) = stmt.kind + if let StatementKind::Assign(box (ref mut lhs, ref mut rvalue)) = stmt.kind { + self.simplify_place_projection(lhs, location); + // Do not try to simplify a constant, it's already in canonical shape. - && !matches!(rvalue, Rvalue::Use(Operand::Constant(_))) - { - if let Some(value) = self.simplify_rvalue(rvalue, location) { - if let Some(const_) = self.try_as_constant(value) { - *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_))); - } else if let Some(local) = self.try_as_local(value, location) - && *rvalue != Rvalue::Use(Operand::Move(local.into())) - { - *rvalue = Rvalue::Use(Operand::Copy(local.into())); - self.reused_locals.insert(local); - } + if matches!(rvalue, Rvalue::Use(Operand::Constant(_))) { + return; } - } else { - self.super_statement(stmt, location); + + let value = lhs + .as_local() + .and_then(|local| self.locals[local]) + .or_else(|| self.simplify_rvalue(rvalue, location)); + let Some(value) = value else { return }; + + if let Some(const_) = self.try_as_constant(value) { + *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_))); + } else if let Some(local) = self.try_as_local(value, location) + && *rvalue != Rvalue::Use(Operand::Move(local.into())) + { + *rvalue = Rvalue::Use(Operand::Copy(local.into())); + self.reused_locals.insert(local); + } + + return; } + self.super_statement(stmt, location); } } diff --git a/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-abort.diff b/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-abort.diff index fc0c8afd4cfd..dfab4959516d 100644 --- a/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-abort.diff +++ b/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-abort.diff @@ -33,10 +33,6 @@ + } + + ALLOC1 (size: 2, align: 1) { -+ 01 02 │ .. -+ } -+ -+ ALLOC2 (size: 2, align: 1) { + 01 02 │ .. } diff --git a/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-unwind.diff b/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-unwind.diff index cf4089598e73..b6d697323166 100644 --- a/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-unwind.diff +++ b/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-unwind.diff @@ -33,10 +33,6 @@ + } + + ALLOC1 (size: 2, align: 1) { -+ 01 02 │ .. -+ } -+ -+ ALLOC2 (size: 2, align: 1) { + 01 02 │ .. } diff --git a/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.32bit.diff b/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.32bit.diff index 93dfef96cf15..2dc3aa9afca2 100644 --- a/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.32bit.diff +++ b/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.32bit.diff @@ -15,10 +15,11 @@ StorageLive(_1); - StorageLive(_2); - _2 = const 1_usize as &mut Never (Transmute); +- _1 = &mut (*_2); +- StorageDead(_2); + nop; + _2 = const {0x1 as &mut Never}; - _1 = &mut (*_2); -- StorageDead(_2); ++ _1 = const {0x1 as &mut Never}; + nop; unreachable; } diff --git a/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.64bit.diff b/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.64bit.diff index 93dfef96cf15..2dc3aa9afca2 100644 --- a/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.64bit.diff +++ b/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.64bit.diff @@ -15,10 +15,11 @@ StorageLive(_1); - StorageLive(_2); - _2 = const 1_usize as &mut Never (Transmute); +- _1 = &mut (*_2); +- StorageDead(_2); + nop; + _2 = const {0x1 as &mut Never}; - _1 = &mut (*_2); -- StorageDead(_2); ++ _1 = const {0x1 as &mut Never}; + nop; unreachable; }