Skip to content

Commit 4ee01fa

Browse files
committed
Do not re-simplify SSA locals.
1 parent e26c9a4 commit 4ee01fa

File tree

5 files changed

+29
-26
lines changed

5 files changed

+29
-26
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -1015,23 +1015,32 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
10151015
}
10161016

10171017
fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, location: Location) {
1018-
if let StatementKind::Assign(box (_, ref mut rvalue)) = stmt.kind
1018+
if let StatementKind::Assign(box (ref mut lhs, ref mut rvalue)) = stmt.kind {
1019+
self.simplify_place_projection(lhs, location);
1020+
10191021
// Do not try to simplify a constant, it's already in canonical shape.
1020-
&& !matches!(rvalue, Rvalue::Use(Operand::Constant(_)))
1021-
{
1022-
if let Some(value) = self.simplify_rvalue(rvalue, location) {
1023-
if let Some(const_) = self.try_as_constant(value) {
1024-
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
1025-
} else if let Some(local) = self.try_as_local(value, location)
1026-
&& *rvalue != Rvalue::Use(Operand::Move(local.into()))
1027-
{
1028-
*rvalue = Rvalue::Use(Operand::Copy(local.into()));
1029-
self.reused_locals.insert(local);
1030-
}
1022+
if matches!(rvalue, Rvalue::Use(Operand::Constant(_))) {
1023+
return;
10311024
}
1032-
} else {
1033-
self.super_statement(stmt, location);
1025+
1026+
let value = lhs
1027+
.as_local()
1028+
.and_then(|local| self.locals[local])
1029+
.or_else(|| self.simplify_rvalue(rvalue, location));
1030+
let Some(value) = value else { return };
1031+
1032+
if let Some(const_) = self.try_as_constant(value) {
1033+
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
1034+
} else if let Some(local) = self.try_as_local(value, location)
1035+
&& *rvalue != Rvalue::Use(Operand::Move(local.into()))
1036+
{
1037+
*rvalue = Rvalue::Use(Operand::Copy(local.into()));
1038+
self.reused_locals.insert(local);
1039+
}
1040+
1041+
return;
10341042
}
1043+
self.super_statement(stmt, location);
10351044
}
10361045
}
10371046

tests/mir-opt/const_prop/issue_67019.main.GVN.panic-abort.diff

-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
+ }
3434
+
3535
+ ALLOC1 (size: 2, align: 1) {
36-
+ 01 02 │ ..
37-
+ }
38-
+
39-
+ ALLOC2 (size: 2, align: 1) {
4036
+ 01 02 │ ..
4137
}
4238

tests/mir-opt/const_prop/issue_67019.main.GVN.panic-unwind.diff

-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
+ }
3434
+
3535
+ ALLOC1 (size: 2, align: 1) {
36-
+ 01 02 │ ..
37-
+ }
38-
+
39-
+ ALLOC2 (size: 2, align: 1) {
4036
+ 01 02 │ ..
4137
}
4238

tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.32bit.diff

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
StorageLive(_1);
1616
- StorageLive(_2);
1717
- _2 = const 1_usize as &mut Never (Transmute);
18+
- _1 = &mut (*_2);
19+
- StorageDead(_2);
1820
+ nop;
1921
+ _2 = const {0x1 as &mut Never};
20-
_1 = &mut (*_2);
21-
- StorageDead(_2);
22+
+ _1 = const {0x1 as &mut Never};
2223
+ nop;
2324
unreachable;
2425
}

tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.64bit.diff

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
StorageLive(_1);
1616
- StorageLive(_2);
1717
- _2 = const 1_usize as &mut Never (Transmute);
18+
- _1 = &mut (*_2);
19+
- StorageDead(_2);
1820
+ nop;
1921
+ _2 = const {0x1 as &mut Never};
20-
_1 = &mut (*_2);
21-
- StorageDead(_2);
22+
+ _1 = const {0x1 as &mut Never};
2223
+ nop;
2324
unreachable;
2425
}

0 commit comments

Comments
 (0)