Skip to content

Commit 4de2d8f

Browse files
committed
Perform reference propagation earlier.
1 parent b74a144 commit 4de2d8f

20 files changed

+96
-86
lines changed

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,14 +553,14 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
553553
&normalize_array_len::NormalizeArrayLen, // has to run after `slice::len` lowering
554554
&const_goto::ConstGoto,
555555
&remove_unneeded_drops::RemoveUnneededDrops,
556+
&ref_prop::ReferencePropagation,
556557
&sroa::ScalarReplacementOfAggregates,
557558
&match_branches::MatchBranchSimplification,
558559
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
559560
&multiple_return_terminators::MultipleReturnTerminators,
560561
&instsimplify::InstSimplify,
561562
&simplify::SimplifyLocals::BeforeConstProp,
562563
&copy_prop::CopyProp,
563-
&ref_prop::ReferencePropagation,
564564
// Perform `SeparateConstSwitch` after SSA-based analyses, as cloning blocks may
565565
// destroy the SSA property. It should still happen before const-propagation, so the
566566
// latter pass will leverage the created opportunities.

tests/codegen/slice-ref-equality.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,48 +44,48 @@ pub fn is_zero_array(data: &[u8; 4]) -> bool {
4444
// equality for non-byte types also just emit a `bcmp`, not a loop.
4545

4646
// CHECK-LABEL: @eq_slice_of_nested_u8(
47-
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %1
48-
// CHECK-SAME: [[USIZE]] noundef %3
47+
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %x.1
48+
// CHECK-SAME: [[USIZE]] noundef %y.1
4949
#[no_mangle]
5050
fn eq_slice_of_nested_u8(x: &[[u8; 3]], y: &[[u8; 3]]) -> bool {
51-
// CHECK: icmp eq [[USIZE]] %1, %3
52-
// CHECK: %[[BYTES:.+]] = mul nsw [[USIZE]] %1, 3
51+
// CHECK: icmp eq [[USIZE]] %x.1, %y.1
52+
// CHECK: %[[BYTES:.+]] = mul nsw [[USIZE]] %x.1, 3
5353
// CHECK: tail call{{( noundef)?}} i32 @{{bcmp|memcmp}}({{i8\*|ptr}}
5454
// CHECK-SAME: , [[USIZE]]{{( noundef)?}} %[[BYTES]])
5555
x == y
5656
}
5757

5858
// CHECK-LABEL: @eq_slice_of_i32(
59-
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %1
60-
// CHECK-SAME: [[USIZE]] noundef %3
59+
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %x.1
60+
// CHECK-SAME: [[USIZE]] noundef %y.1
6161
#[no_mangle]
6262
fn eq_slice_of_i32(x: &[i32], y: &[i32]) -> bool {
63-
// CHECK: icmp eq [[USIZE]] %1, %3
64-
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %1, 2
63+
// CHECK: icmp eq [[USIZE]] %x.1, %y.1
64+
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %x.1, 2
6565
// CHECK: tail call{{( noundef)?}} i32 @{{bcmp|memcmp}}({{i32\*|ptr}}
6666
// CHECK-SAME: , [[USIZE]]{{( noundef)?}} %[[BYTES]])
6767
x == y
6868
}
6969

7070
// CHECK-LABEL: @eq_slice_of_nonzero(
71-
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %1
72-
// CHECK-SAME: [[USIZE]] noundef %3
71+
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %x.1
72+
// CHECK-SAME: [[USIZE]] noundef %y.1
7373
#[no_mangle]
7474
fn eq_slice_of_nonzero(x: &[NonZeroU32], y: &[NonZeroU32]) -> bool {
75-
// CHECK: icmp eq [[USIZE]] %1, %3
76-
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %1, 2
75+
// CHECK: icmp eq [[USIZE]] %x.1, %y.1
76+
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %x.1, 2
7777
// CHECK: tail call{{( noundef)?}} i32 @{{bcmp|memcmp}}({{i32\*|ptr}}
7878
// CHECK-SAME: , [[USIZE]]{{( noundef)?}} %[[BYTES]])
7979
x == y
8080
}
8181

8282
// CHECK-LABEL: @eq_slice_of_option_of_nonzero(
83-
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %1
84-
// CHECK-SAME: [[USIZE]] noundef %3
83+
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %x.1
84+
// CHECK-SAME: [[USIZE]] noundef %y.1
8585
#[no_mangle]
8686
fn eq_slice_of_option_of_nonzero(x: &[Option<NonZeroI16>], y: &[Option<NonZeroI16>]) -> bool {
87-
// CHECK: icmp eq [[USIZE]] %1, %3
88-
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %1, 1
87+
// CHECK: icmp eq [[USIZE]] %x.1, %y.1
88+
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %x.1, 1
8989
// CHECK: tail call{{( noundef)?}} i32 @{{bcmp|memcmp}}({{i16\*|ptr}}
9090
// CHECK-SAME: , [[USIZE]]{{( noundef)?}} %[[BYTES]])
9191
x == y

tests/mir-opt/casts.redundant.InstSimplify.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let mut _2: *const &u8;
88
let mut _3: *const &u8;
99
scope 1 (inlined generic_cast::<&u8, &u8>) {
10-
debug x => _3;
10+
debug x => _1;
1111
}
1212

1313
bb0: {

tests/mir-opt/copy-prop/mutate_through_pointer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// unit-test: CopyProp
2+
//
13
// This attempts to mutate `a` via a pointer derived from `addr_of!(a)`. That is UB
24
// according to Miri. However, the decision to make this UB - and to allow
35
// rustc to rely on that fact for the purpose of optimizations - has not been

tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66
let mut _0: ();
77
let _2: &[T];
88
let mut _3: &[T; 3];
9-
let _4: &[T; 3];
10-
let _5: [T; 3];
9+
let _4: [T; 3];
10+
let mut _5: T;
1111
let mut _6: T;
1212
let mut _7: T;
13-
let mut _8: T;
13+
let mut _8: usize;
1414
let mut _9: usize;
15-
let mut _10: usize;
16-
let mut _11: bool;
17-
let mut _15: !;
15+
let mut _10: bool;
16+
let mut _11: !;
1817
scope 1 {
1918
debug v => _2;
20-
let _12: &T;
21-
let _13: &T;
22-
let _14: &T;
2319
scope 2 {
2420
debug v1 => &(*_2)[0 of 3];
2521
debug v2 => &(*_2)[1 of 3];
@@ -28,26 +24,26 @@
2824
}
2925

3026
bb0: {
31-
StorageLive(_2);
32-
StorageLive(_5);
33-
_5 = [_1, _1, _1];
34-
_4 = &_5;
35-
_2 = _4 as &[T] (PointerCoercion(Unsize));
36-
_9 = Len((*_2));
37-
_10 = const 3_usize;
38-
- _11 = Eq(move _9, const 3_usize);
39-
- switchInt(move _11) -> [0: bb1, otherwise: bb2];
27+
StorageLive(_3);
28+
StorageLive(_4);
29+
_4 = [_1, _1, _1];
30+
_3 = &_4;
31+
_2 = move _3 as &[T] (PointerCoercion(Unsize));
32+
StorageDead(_3);
33+
_8 = Len((*_2));
34+
_9 = const 3_usize;
35+
- _10 = Eq(move _8, const 3_usize);
36+
- switchInt(move _10) -> [0: bb1, otherwise: bb2];
4037
+ nop;
41-
+ switchInt(move _9) -> [3: bb2, otherwise: bb1];
38+
+ switchInt(move _8) -> [3: bb2, otherwise: bb1];
4239
}
4340

4441
bb1: {
45-
_15 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind unreachable;
42+
_11 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind unreachable;
4643
}
4744

4845
bb2: {
49-
StorageDead(_5);
50-
StorageDead(_2);
46+
StorageDead(_4);
5147
return;
5248
}
5349
}

tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66
let mut _0: ();
77
let _2: &[T];
88
let mut _3: &[T; 3];
9-
let _4: &[T; 3];
10-
let _5: [T; 3];
9+
let _4: [T; 3];
10+
let mut _5: T;
1111
let mut _6: T;
1212
let mut _7: T;
13-
let mut _8: T;
13+
let mut _8: usize;
1414
let mut _9: usize;
15-
let mut _10: usize;
16-
let mut _11: bool;
17-
let mut _15: !;
15+
let mut _10: bool;
16+
let mut _11: !;
1817
scope 1 {
1918
debug v => _2;
20-
let _12: &T;
21-
let _13: &T;
22-
let _14: &T;
2319
scope 2 {
2420
debug v1 => &(*_2)[0 of 3];
2521
debug v2 => &(*_2)[1 of 3];
@@ -28,26 +24,26 @@
2824
}
2925

3026
bb0: {
31-
StorageLive(_2);
32-
StorageLive(_5);
33-
_5 = [_1, _1, _1];
34-
_4 = &_5;
35-
_2 = _4 as &[T] (PointerCoercion(Unsize));
36-
_9 = Len((*_2));
37-
_10 = const 3_usize;
38-
- _11 = Eq(move _9, const 3_usize);
39-
- switchInt(move _11) -> [0: bb1, otherwise: bb2];
27+
StorageLive(_3);
28+
StorageLive(_4);
29+
_4 = [_1, _1, _1];
30+
_3 = &_4;
31+
_2 = move _3 as &[T] (PointerCoercion(Unsize));
32+
StorageDead(_3);
33+
_8 = Len((*_2));
34+
_9 = const 3_usize;
35+
- _10 = Eq(move _8, const 3_usize);
36+
- switchInt(move _10) -> [0: bb1, otherwise: bb2];
4037
+ nop;
41-
+ switchInt(move _9) -> [3: bb2, otherwise: bb1];
38+
+ switchInt(move _8) -> [3: bb2, otherwise: bb1];
4239
}
4340

4441
bb1: {
45-
_15 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind continue;
42+
_11 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind continue;
4643
}
4744

4845
bb2: {
49-
StorageDead(_5);
50-
StorageDead(_2);
46+
StorageDead(_4);
5147
return;
5248
}
5349
}

tests/mir-opt/pre-codegen/loops.mapped.PreCodegen.after.mir

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ fn mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> U) -> () {
3838

3939
bb2: {
4040
StorageLive(_7);
41+
StorageLive(_6);
4142
_6 = &mut _5;
42-
_7 = <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next(_6) -> [return: bb3, unwind: bb9];
43+
_7 = <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next(move _6) -> [return: bb3, unwind: bb9];
4344
}
4445

4546
bb3: {
47+
StorageDead(_6);
4648
_8 = discriminant(_7);
4749
switchInt(move _8) -> [0: bb4, 1: bb6, otherwise: bb8];
4850
}

tests/mir-opt/pre-codegen/loops.vec_move.PreCodegen.after.mir

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
3030

3131
bb2: {
3232
StorageLive(_5);
33+
StorageLive(_4);
3334
_4 = &mut _3;
34-
_5 = <std::vec::IntoIter<impl Sized> as Iterator>::next(_4) -> [return: bb3, unwind: bb9];
35+
_5 = <std::vec::IntoIter<impl Sized> as Iterator>::next(move _4) -> [return: bb3, unwind: bb9];
3536
}
3637

3738
bb3: {
39+
StorageDead(_4);
3840
_6 = discriminant(_5);
3941
switchInt(move _6) -> [0: bb4, 1: bb6, otherwise: bb8];
4042
}

tests/mir-opt/pre-codegen/range_iter.inclusive_loop.PreCodegen.after.panic-abort.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ fn inclusive_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () {
4040

4141
bb1: {
4242
StorageLive(_7);
43+
StorageLive(_6);
4344
_6 = &mut _5;
4445
_7 = <RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(_6) -> [return: bb2, unwind unreachable];
4546
}
4647

4748
bb2: {
49+
StorageDead(_6);
4850
_8 = discriminant(_7);
4951
switchInt(move _8) -> [0: bb3, 1: bb5, otherwise: bb7];
5052
}

tests/mir-opt/pre-codegen/range_iter.inclusive_loop.PreCodegen.after.panic-unwind.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ fn inclusive_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () {
4040

4141
bb1: {
4242
StorageLive(_7);
43+
StorageLive(_6);
4344
_6 = &mut _5;
4445
_7 = <RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(_6) -> [return: bb2, unwind: bb8];
4546
}
4647

4748
bb2: {
49+
StorageDead(_6);
4850
_8 = discriminant(_7);
4951
switchInt(move _8) -> [0: bb3, 1: bb5, otherwise: bb7];
5052
}

tests/mir-opt/pre-codegen/slice_index.slice_get_mut_usize.PreCodegen.after.panic-abort.mir

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
5353
}
5454

5555
bb0: {
56-
StorageLive(_8);
56+
StorageLive(_7);
5757
StorageLive(_4);
5858
StorageLive(_3);
5959
_3 = Len((*_1));
@@ -68,7 +68,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
6868
}
6969

7070
bb2: {
71-
StorageLive(_7);
71+
StorageLive(_8);
7272
StorageLive(_5);
7373
_5 = &raw mut (*_1);
7474
StorageLive(_9);
@@ -79,14 +79,14 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
7979
StorageDead(_9);
8080
StorageDead(_5);
8181
_8 = &mut (*_7);
82-
_0 = Option::<&mut u32>::Some(_8);
83-
StorageDead(_7);
82+
_0 = Option::<&mut u32>::Some(move _8);
83+
StorageDead(_8);
8484
goto -> bb3;
8585
}
8686

8787
bb3: {
8888
StorageDead(_4);
89-
StorageDead(_8);
89+
StorageDead(_7);
9090
return;
9191
}
9292
}

tests/mir-opt/pre-codegen/slice_index.slice_get_mut_usize.PreCodegen.after.panic-unwind.mir

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
5353
}
5454

5555
bb0: {
56-
StorageLive(_8);
56+
StorageLive(_7);
5757
StorageLive(_4);
5858
StorageLive(_3);
5959
_3 = Len((*_1));
@@ -68,7 +68,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
6868
}
6969

7070
bb2: {
71-
StorageLive(_7);
71+
StorageLive(_8);
7272
StorageLive(_5);
7373
_5 = &raw mut (*_1);
7474
StorageLive(_9);
@@ -79,14 +79,14 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
7979
StorageDead(_9);
8080
StorageDead(_5);
8181
_8 = &mut (*_7);
82-
_0 = Option::<&mut u32>::Some(_8);
83-
StorageDead(_7);
82+
_0 = Option::<&mut u32>::Some(move _8);
83+
StorageDead(_8);
8484
goto -> bb3;
8585
}
8686

8787
bb3: {
8888
StorageDead(_4);
89-
StorageDead(_8);
89+
StorageDead(_7);
9090
return;
9191
}
9292
}

tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-abort.mir

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) ->
6161
bb0: {
6262
_3 = move (_2.0: usize);
6363
_4 = move (_2.1: usize);
64-
StorageLive(_13);
6564
StorageLive(_5);
6665
_5 = &raw mut (*_1);
6766
StorageLive(_14);
@@ -92,7 +91,6 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) ->
9291
StorageDead(_15);
9392
StorageDead(_5);
9493
_0 = &mut (*_13);
95-
StorageDead(_13);
9694
return;
9795
}
9896
}

tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-unwind.mir

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) ->
6161
bb0: {
6262
_3 = move (_2.0: usize);
6363
_4 = move (_2.1: usize);
64-
StorageLive(_13);
6564
StorageLive(_5);
6665
_5 = &raw mut (*_1);
6766
StorageLive(_14);
@@ -92,7 +91,6 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) ->
9291
StorageDead(_15);
9392
StorageDead(_5);
9493
_0 = &mut (*_13);
95-
StorageDead(_13);
9694
return;
9795
}
9896
}

0 commit comments

Comments
 (0)