Skip to content

Commit 7e39100

Browse files
committed
Avoid recording no-op replacements.
1 parent 4ee01fa commit 7e39100

File tree

35 files changed

+126
-189
lines changed

35 files changed

+126
-189
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
637637
if place.is_indirect()
638638
&& let Some(base) = self.locals[place.local]
639639
&& let Some(new_local) = self.try_as_local(base, location)
640+
&& place.local != new_local
640641
{
641642
place.local = new_local;
642643
self.reused_locals.insert(new_local);
@@ -646,18 +647,20 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
646647

647648
for i in 0..projection.len() {
648649
let elem = projection[i];
649-
if let ProjectionElem::Index(idx) = elem
650-
&& let Some(idx) = self.locals[idx]
650+
if let ProjectionElem::Index(idx_local) = elem
651+
&& let Some(idx) = self.locals[idx_local]
651652
{
652653
if let Some(offset) = self.evaluated[idx].as_ref()
653654
&& let Ok(offset) = self.ecx.read_target_usize(offset)
654655
&& let Some(min_length) = offset.checked_add(1)
655656
{
656657
projection.to_mut()[i] =
657658
ProjectionElem::ConstantIndex { offset, min_length, from_end: false };
658-
} else if let Some(new_idx) = self.try_as_local(idx, location) {
659-
projection.to_mut()[i] = ProjectionElem::Index(new_idx);
660-
self.reused_locals.insert(new_idx);
659+
} else if let Some(new_idx_local) = self.try_as_local(idx, location)
660+
&& idx_local != new_idx_local
661+
{
662+
projection.to_mut()[i] = ProjectionElem::Index(new_idx_local);
663+
self.reused_locals.insert(new_idx_local);
661664
}
662665
}
663666
}

tests/mir-opt/const_allocation.main.GVN.after.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation.main.GVN.after.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation2.main.GVN.after.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC9: &&[(Option<i32>, &[&u8])]};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation2.main.GVN.after.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC9: &&[(Option<i32>, &[&u8])]};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation3.main.GVN.after.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC4: &&Packed};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation3.main.GVN.after.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC2: &&Packed};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_prop/address_of_pair.fn0.GVN.diff

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
bb0: {
2525
StorageLive(_2);
2626
- _2 = (const 1_i32, const false);
27-
- StorageLive(_3);
2827
+ _2 = const (1_i32, false);
29-
+ nop;
28+
StorageLive(_3);
3029
_3 = &raw mut (_2.1: bool);
3130
- _2 = (const 1_i32, const false);
3231
+ _2 = const (1_i32, false);
@@ -42,9 +41,8 @@
4241
StorageDead(_6);
4342
_0 = _5;
4443
- StorageDead(_5);
45-
- StorageDead(_3);
46-
+ nop;
4744
+ nop;
45+
StorageDead(_3);
4846
StorageDead(_2);
4947
return;
5048
}

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.GVN.32bit.panic-abort.diff

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
}
2323

2424
bb0: {
25-
- StorageLive(_1);
26-
+ nop;
25+
StorageLive(_1);
2726
StorageLive(_2);
28-
- StorageLive(_3);
29-
+ nop;
27+
StorageLive(_3);
3028
_9 = const _;
3129
_3 = &(*_9);
3230
_2 = &raw const (*_3);
3331
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
3432
StorageDead(_2);
35-
- StorageDead(_3);
36-
+ nop;
33+
StorageDead(_3);
3734
StorageLive(_5);
3835
StorageLive(_6);
3936
_6 = const 3_usize;
@@ -50,8 +47,7 @@
5047
StorageDead(_6);
5148
_0 = const ();
5249
StorageDead(_5);
53-
- StorageDead(_1);
54-
+ nop;
50+
StorageDead(_1);
5551
return;
5652
}
5753
}

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.GVN.32bit.panic-unwind.diff

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
}
2323

2424
bb0: {
25-
- StorageLive(_1);
26-
+ nop;
25+
StorageLive(_1);
2726
StorageLive(_2);
28-
- StorageLive(_3);
29-
+ nop;
27+
StorageLive(_3);
3028
_9 = const _;
3129
_3 = &(*_9);
3230
_2 = &raw const (*_3);
3331
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
3432
StorageDead(_2);
35-
- StorageDead(_3);
36-
+ nop;
33+
StorageDead(_3);
3734
StorageLive(_5);
3835
StorageLive(_6);
3936
_6 = const 3_usize;
@@ -50,8 +47,7 @@
5047
StorageDead(_6);
5148
_0 = const ();
5249
StorageDead(_5);
53-
- StorageDead(_1);
54-
+ nop;
50+
StorageDead(_1);
5551
return;
5652
}
5753
}

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.GVN.64bit.panic-abort.diff

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
}
2323

2424
bb0: {
25-
- StorageLive(_1);
26-
+ nop;
25+
StorageLive(_1);
2726
StorageLive(_2);
28-
- StorageLive(_3);
29-
+ nop;
27+
StorageLive(_3);
3028
_9 = const _;
3129
_3 = &(*_9);
3230
_2 = &raw const (*_3);
3331
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
3432
StorageDead(_2);
35-
- StorageDead(_3);
36-
+ nop;
33+
StorageDead(_3);
3734
StorageLive(_5);
3835
StorageLive(_6);
3936
_6 = const 3_usize;
@@ -50,8 +47,7 @@
5047
StorageDead(_6);
5148
_0 = const ();
5249
StorageDead(_5);
53-
- StorageDead(_1);
54-
+ nop;
50+
StorageDead(_1);
5551
return;
5652
}
5753
}

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.GVN.64bit.panic-unwind.diff

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
}
2323

2424
bb0: {
25-
- StorageLive(_1);
26-
+ nop;
25+
StorageLive(_1);
2726
StorageLive(_2);
28-
- StorageLive(_3);
29-
+ nop;
27+
StorageLive(_3);
3028
_9 = const _;
3129
_3 = &(*_9);
3230
_2 = &raw const (*_3);
3331
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
3432
StorageDead(_2);
35-
- StorageDead(_3);
36-
+ nop;
33+
StorageDead(_3);
3734
StorageLive(_5);
3835
StorageLive(_6);
3936
_6 = const 3_usize;
@@ -50,8 +47,7 @@
5047
StorageDead(_6);
5148
_0 = const ();
5249
StorageDead(_5);
53-
- StorageDead(_1);
54-
+ nop;
50+
StorageDead(_1);
5551
return;
5652
}
5753
}

tests/mir-opt/const_prop/indirect_mutation.bar.GVN.diff

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
- _1 = (const 1_i32,);
2323
+ _1 = const (1_i32,);
2424
StorageLive(_2);
25-
- StorageLive(_3);
26-
+ nop;
25+
StorageLive(_3);
2726
_3 = &raw mut (_1.0: i32);
2827
(*_3) = const 5_i32;
29-
- StorageDead(_3);
30-
+ nop;
28+
StorageDead(_3);
3129
_2 = const ();
3230
StorageDead(_2);
3331
StorageLive(_4);

tests/mir-opt/const_prop/indirect_mutation.foo.GVN.diff

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
bb0: {
1818
StorageLive(_1);
1919
- _1 = (const 1_i32,);
20-
- StorageLive(_2);
2120
+ _1 = const (1_i32,);
22-
+ nop;
21+
StorageLive(_2);
2322
_2 = &mut (_1.0: i32);
2423
(*_2) = const 5_i32;
25-
- StorageDead(_2);
26-
+ nop;
24+
StorageDead(_2);
2725
StorageLive(_3);
2826
StorageLive(_4);
2927
_4 = (_1.0: i32);

tests/mir-opt/const_prop/mutable_variable_aggregate_mut_ref.main.GVN.diff

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@
1919
bb0: {
2020
StorageLive(_1);
2121
- _1 = (const 42_i32, const 43_i32);
22-
- StorageLive(_2);
2322
+ _1 = const (42_i32, 43_i32);
24-
+ nop;
23+
StorageLive(_2);
2524
_2 = &mut _1;
2625
((*_2).1: i32) = const 99_i32;
2726
StorageLive(_3);
2827
_3 = _1;
2928
_0 = const ();
3029
StorageDead(_3);
31-
- StorageDead(_2);
32-
+ nop;
30+
StorageDead(_2);
3331
StorageDead(_1);
3432
return;
3533
}

tests/mir-opt/const_prop/mutable_variable_no_prop.main.GVN.diff

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
_1 = const 42_u32;
2323
StorageLive(_2);
2424
StorageLive(_3);
25-
- StorageLive(_4);
26-
+ nop;
25+
StorageLive(_4);
2726
_4 = const {ALLOC0: *mut u32};
2827
_3 = (*_4);
2928
_1 = move _3;
3029
StorageDead(_3);
31-
- StorageDead(_4);
32-
+ nop;
30+
StorageDead(_4);
3331
_2 = const ();
3432
StorageDead(_2);
3533
StorageLive(_5);

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
- StorageLive(_1);
1717
+ nop;
1818
StorageLive(_2);
19-
- StorageLive(_3);
20-
+ nop;
19+
StorageLive(_3);
2120
_3 = const _;
2221
_2 = &raw const (*_3);
2322
_1 = move _2 as usize (PointerExposeAddress);
2423
StorageDead(_2);
25-
- StorageDead(_3);
26-
+ nop;
24+
StorageDead(_3);
2725
StorageLive(_4);
2826
StorageLive(_5);
2927
_5 = _1;

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
- StorageLive(_1);
1717
+ nop;
1818
StorageLive(_2);
19-
- StorageLive(_3);
20-
+ nop;
19+
StorageLive(_3);
2120
_3 = const _;
2221
_2 = &raw const (*_3);
2322
_1 = move _2 as usize (PointerExposeAddress);
2423
StorageDead(_2);
25-
- StorageDead(_3);
26-
+ nop;
24+
StorageDead(_3);
2725
StorageLive(_4);
2826
StorageLive(_5);
2927
_5 = _1;

tests/mir-opt/const_prop/ref_deref.main.GVN.diff

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313

1414
bb0: {
1515
StorageLive(_1);
16-
- StorageLive(_2);
17-
+ nop;
16+
StorageLive(_2);
1817
_4 = const _;
1918
_2 = &(*_4);
2019
- _1 = (*_2);
21-
- StorageDead(_2);
2220
+ _1 = const 4_i32;
23-
+ nop;
21+
StorageDead(_2);
2422
_0 = const ();
2523
StorageDead(_1);
2624
return;

tests/mir-opt/const_prop/ref_deref_project.main.GVN.diff

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313

1414
bb0: {
1515
StorageLive(_1);
16-
- StorageLive(_2);
17-
+ nop;
16+
StorageLive(_2);
1817
_4 = const _;
1918
_2 = &((*_4).1: i32);
2019
- _1 = (*_2);
21-
- StorageDead(_2);
2220
+ _1 = const 5_i32;
23-
+ nop;
21+
StorageDead(_2);
2422
_0 = const ();
2523
StorageDead(_1);
2624
return;

0 commit comments

Comments
 (0)