Skip to content

Commit aee7d70

Browse files
committed
Mark reads in statements to avoid overlapping assingments.
1 parent d51aec7 commit aee7d70

6 files changed

+31
-24
lines changed

compiler/rustc_mir_transform/src/dest_prop.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -567,13 +567,15 @@ fn save_as_intervals<'tcx>(
567567
// the written-to locals as live in the second half of the statement.
568568
// We also ensure that operands read by terminators conflict with writes by that terminator.
569569
// For instance a function call may read args after having written to the destination.
570-
VisitPlacesWith(|place, ctxt| match DefUse::for_place(place, ctxt) {
571-
DefUse::Def | DefUse::Use | DefUse::PartialWrite => {
572-
if let Some(relevant) = relevant.shrink[place.local] {
573-
values.insert(relevant, twostep);
570+
VisitPlacesWith(|place: Place<'tcx>, ctxt| {
571+
if let Some(relevant) = relevant.shrink[place.local] {
572+
match DefUse::for_place(place, ctxt) {
573+
DefUse::Def | DefUse::Use | DefUse::PartialWrite => {
574+
values.insert(relevant, twostep);
575+
}
576+
DefUse::NonUse => {}
574577
}
575578
}
576-
DefUse::NonUse => {}
577579
})
578580
.visit_terminator(term, loc);
579581

@@ -590,13 +592,20 @@ fn save_as_intervals<'tcx>(
590592
append_at(&mut values, &state, twostep);
591593
// Ensure we have a non-zero live range even for dead stores. This is done by marking
592594
// all the written-to locals as live in the second half of the statement.
593-
VisitPlacesWith(|place, ctxt| match DefUse::for_place(place, ctxt) {
594-
DefUse::Def | DefUse::PartialWrite => {
595-
if let Some(relevant) = relevant.shrink[place.local] {
596-
values.insert(relevant, twostep);
595+
let is_simple_assignment =
596+
matches!(stmt.kind, StatementKind::Assign(box (_, Rvalue::Use(_))));
597+
VisitPlacesWith(|place: Place<'tcx>, ctxt| {
598+
if let Some(relevant) = relevant.shrink[place.local] {
599+
match DefUse::for_place(place, ctxt) {
600+
DefUse::Def | DefUse::PartialWrite => {
601+
values.insert(relevant, twostep);
602+
}
603+
DefUse::Use if !is_simple_assignment => {
604+
values.insert(relevant, twostep);
605+
}
606+
DefUse::Use | DefUse::NonUse => {}
597607
}
598608
}
599-
DefUse::Use | DefUse::NonUse => {}
600609
})
601610
.visit_statement(stmt, loc);
602611

tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-abort.diff

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
bb0: {
1010
- (_1.0: u8) = const 0_u8;
1111
- _0 = copy _1;
12-
- _2 = (copy (_0.0: u8),);
13-
- _0 = copy _2;
1412
+ (_0.0: u8) = const 0_u8;
1513
+ nop;
16-
+ _0 = (copy (_0.0: u8),);
17-
+ nop;
14+
_2 = (copy (_0.0: u8),);
15+
_0 = copy _2;
1816
return;
1917
}
2018
}

tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-unwind.diff

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
bb0: {
1010
- (_1.0: u8) = const 0_u8;
1111
- _0 = copy _1;
12-
- _2 = (copy (_0.0: u8),);
13-
- _0 = copy _2;
1412
+ (_0.0: u8) = const 0_u8;
1513
+ nop;
16-
+ _0 = (copy (_0.0: u8),);
17-
+ nop;
14+
_2 = (copy (_0.0: u8),);
15+
_0 = copy _2;
1816
return;
1917
}
2018
}

tests/mir-opt/dest-prop/aggregate.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ fn dump_var<T>(_: T) {}
1414
fn rewrap() -> (u8,) {
1515
// CHECK-LABEL: fn rewrap(
1616
// CHECK: (_0.0: u8) = const 0_u8;
17-
// CHECK: _0 = (copy (_0.0: u8),);
17+
// CHECK: _2 = (copy (_0.0: u8),);
18+
// CHECK: _0 = copy _2;
1819
mir! {
1920
let _1: (u8,);
2021
let _2: (u8,);
@@ -33,7 +34,8 @@ fn rewrap() -> (u8,) {
3334
fn swap() -> (MaybeUninit<[u8; 10]>, MaybeUninit<[u8; 10]>) {
3435
// CHECK-LABEL: fn swap(
3536
// CHECK: _0 = const
36-
// CHECK: _0 = (copy (_0.1: {{.*}}), copy (_0.0: {{.*}}));
37+
// CHECK: _2 = copy _0;
38+
// CHECK: _0 = (copy (_2.1: {{.*}}), copy (_2.0: {{.*}}));
3739
mir! {
3840
let _1: (MaybeUninit<[u8; 10]>, MaybeUninit<[u8; 10]>);
3941
let _2: (MaybeUninit<[u8; 10]>, MaybeUninit<[u8; 10]>);

tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-abort.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
- _1 = (copy (_2.1: std::mem::MaybeUninit<[u8; 10]>), copy (_2.0: std::mem::MaybeUninit<[u8; 10]>));
1414
- _0 = copy _1;
1515
+ _0 = const swap::{constant#6};
16-
+ nop;
17-
+ _0 = (copy (_0.1: std::mem::MaybeUninit<[u8; 10]>), copy (_0.0: std::mem::MaybeUninit<[u8; 10]>));
16+
+ _2 = copy _0;
17+
+ _0 = (copy (_2.1: std::mem::MaybeUninit<[u8; 10]>), copy (_2.0: std::mem::MaybeUninit<[u8; 10]>));
1818
+ nop;
1919
return;
2020
}

tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-unwind.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
- _1 = (copy (_2.1: std::mem::MaybeUninit<[u8; 10]>), copy (_2.0: std::mem::MaybeUninit<[u8; 10]>));
1414
- _0 = copy _1;
1515
+ _0 = const swap::{constant#6};
16-
+ nop;
17-
+ _0 = (copy (_0.1: std::mem::MaybeUninit<[u8; 10]>), copy (_0.0: std::mem::MaybeUninit<[u8; 10]>));
16+
+ _2 = copy _0;
17+
+ _0 = (copy (_2.1: std::mem::MaybeUninit<[u8; 10]>), copy (_2.0: std::mem::MaybeUninit<[u8; 10]>));
1818
+ nop;
1919
return;
2020
}

0 commit comments

Comments
 (0)