Perform reference propagation earlier.

This commit is contained in:
Camille GILLOT 2023-03-12 14:10:30 +00:00
parent b74a144a5f
commit 4de2d8fb66
20 changed files with 96 additions and 86 deletions

View File

@ -553,6 +553,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&normalize_array_len::NormalizeArrayLen, // has to run after `slice::len` lowering
&const_goto::ConstGoto,
&remove_unneeded_drops::RemoveUnneededDrops,
&ref_prop::ReferencePropagation,
&sroa::ScalarReplacementOfAggregates,
&match_branches::MatchBranchSimplification,
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
@ -560,7 +561,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&instsimplify::InstSimplify,
&simplify::SimplifyLocals::BeforeConstProp,
&copy_prop::CopyProp,
&ref_prop::ReferencePropagation,
// Perform `SeparateConstSwitch` after SSA-based analyses, as cloning blocks may
// destroy the SSA property. It should still happen before const-propagation, so the
// latter pass will leverage the created opportunities.

View File

@ -44,48 +44,48 @@ pub fn is_zero_array(data: &[u8; 4]) -> bool {
// equality for non-byte types also just emit a `bcmp`, not a loop.
// CHECK-LABEL: @eq_slice_of_nested_u8(
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %1
// CHECK-SAME: [[USIZE]] noundef %3
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %x.1
// CHECK-SAME: [[USIZE]] noundef %y.1
#[no_mangle]
fn eq_slice_of_nested_u8(x: &[[u8; 3]], y: &[[u8; 3]]) -> bool {
// CHECK: icmp eq [[USIZE]] %1, %3
// CHECK: %[[BYTES:.+]] = mul nsw [[USIZE]] %1, 3
// CHECK: icmp eq [[USIZE]] %x.1, %y.1
// CHECK: %[[BYTES:.+]] = mul nsw [[USIZE]] %x.1, 3
// CHECK: tail call{{( noundef)?}} i32 @{{bcmp|memcmp}}({{i8\*|ptr}}
// CHECK-SAME: , [[USIZE]]{{( noundef)?}} %[[BYTES]])
x == y
}
// CHECK-LABEL: @eq_slice_of_i32(
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %1
// CHECK-SAME: [[USIZE]] noundef %3
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %x.1
// CHECK-SAME: [[USIZE]] noundef %y.1
#[no_mangle]
fn eq_slice_of_i32(x: &[i32], y: &[i32]) -> bool {
// CHECK: icmp eq [[USIZE]] %1, %3
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %1, 2
// CHECK: icmp eq [[USIZE]] %x.1, %y.1
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %x.1, 2
// CHECK: tail call{{( noundef)?}} i32 @{{bcmp|memcmp}}({{i32\*|ptr}}
// CHECK-SAME: , [[USIZE]]{{( noundef)?}} %[[BYTES]])
x == y
}
// CHECK-LABEL: @eq_slice_of_nonzero(
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %1
// CHECK-SAME: [[USIZE]] noundef %3
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %x.1
// CHECK-SAME: [[USIZE]] noundef %y.1
#[no_mangle]
fn eq_slice_of_nonzero(x: &[NonZeroU32], y: &[NonZeroU32]) -> bool {
// CHECK: icmp eq [[USIZE]] %1, %3
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %1, 2
// CHECK: icmp eq [[USIZE]] %x.1, %y.1
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %x.1, 2
// CHECK: tail call{{( noundef)?}} i32 @{{bcmp|memcmp}}({{i32\*|ptr}}
// CHECK-SAME: , [[USIZE]]{{( noundef)?}} %[[BYTES]])
x == y
}
// CHECK-LABEL: @eq_slice_of_option_of_nonzero(
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %1
// CHECK-SAME: [[USIZE]] noundef %3
// CHECK-SAME: [[USIZE:i16|i32|i64]] noundef %x.1
// CHECK-SAME: [[USIZE]] noundef %y.1
#[no_mangle]
fn eq_slice_of_option_of_nonzero(x: &[Option<NonZeroI16>], y: &[Option<NonZeroI16>]) -> bool {
// CHECK: icmp eq [[USIZE]] %1, %3
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %1, 1
// CHECK: icmp eq [[USIZE]] %x.1, %y.1
// CHECK: %[[BYTES:.+]] = shl nsw [[USIZE]] %x.1, 1
// CHECK: tail call{{( noundef)?}} i32 @{{bcmp|memcmp}}({{i16\*|ptr}}
// CHECK-SAME: , [[USIZE]]{{( noundef)?}} %[[BYTES]])
x == y

View File

@ -7,7 +7,7 @@
let mut _2: *const &u8;
let mut _3: *const &u8;
scope 1 (inlined generic_cast::<&u8, &u8>) {
debug x => _3;
debug x => _1;
}
bb0: {

View File

@ -1,3 +1,5 @@
// unit-test: CopyProp
//
// This attempts to mutate `a` via a pointer derived from `addr_of!(a)`. That is UB
// according to Miri. However, the decision to make this UB - and to allow
// rustc to rely on that fact for the purpose of optimizations - has not been

View File

@ -6,20 +6,16 @@
let mut _0: ();
let _2: &[T];
let mut _3: &[T; 3];
let _4: &[T; 3];
let _5: [T; 3];
let _4: [T; 3];
let mut _5: T;
let mut _6: T;
let mut _7: T;
let mut _8: T;
let mut _8: usize;
let mut _9: usize;
let mut _10: usize;
let mut _11: bool;
let mut _15: !;
let mut _10: bool;
let mut _11: !;
scope 1 {
debug v => _2;
let _12: &T;
let _13: &T;
let _14: &T;
scope 2 {
debug v1 => &(*_2)[0 of 3];
debug v2 => &(*_2)[1 of 3];
@ -28,26 +24,26 @@
}
bb0: {
StorageLive(_2);
StorageLive(_5);
_5 = [_1, _1, _1];
_4 = &_5;
_2 = _4 as &[T] (PointerCoercion(Unsize));
_9 = Len((*_2));
_10 = const 3_usize;
- _11 = Eq(move _9, const 3_usize);
- switchInt(move _11) -> [0: bb1, otherwise: bb2];
StorageLive(_3);
StorageLive(_4);
_4 = [_1, _1, _1];
_3 = &_4;
_2 = move _3 as &[T] (PointerCoercion(Unsize));
StorageDead(_3);
_8 = Len((*_2));
_9 = const 3_usize;
- _10 = Eq(move _8, const 3_usize);
- switchInt(move _10) -> [0: bb1, otherwise: bb2];
+ nop;
+ switchInt(move _9) -> [3: bb2, otherwise: bb1];
+ switchInt(move _8) -> [3: bb2, otherwise: bb1];
}
bb1: {
_15 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind unreachable;
_11 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind unreachable;
}
bb2: {
StorageDead(_5);
StorageDead(_2);
StorageDead(_4);
return;
}
}

View File

@ -6,20 +6,16 @@
let mut _0: ();
let _2: &[T];
let mut _3: &[T; 3];
let _4: &[T; 3];
let _5: [T; 3];
let _4: [T; 3];
let mut _5: T;
let mut _6: T;
let mut _7: T;
let mut _8: T;
let mut _8: usize;
let mut _9: usize;
let mut _10: usize;
let mut _11: bool;
let mut _15: !;
let mut _10: bool;
let mut _11: !;
scope 1 {
debug v => _2;
let _12: &T;
let _13: &T;
let _14: &T;
scope 2 {
debug v1 => &(*_2)[0 of 3];
debug v2 => &(*_2)[1 of 3];
@ -28,26 +24,26 @@
}
bb0: {
StorageLive(_2);
StorageLive(_5);
_5 = [_1, _1, _1];
_4 = &_5;
_2 = _4 as &[T] (PointerCoercion(Unsize));
_9 = Len((*_2));
_10 = const 3_usize;
- _11 = Eq(move _9, const 3_usize);
- switchInt(move _11) -> [0: bb1, otherwise: bb2];
StorageLive(_3);
StorageLive(_4);
_4 = [_1, _1, _1];
_3 = &_4;
_2 = move _3 as &[T] (PointerCoercion(Unsize));
StorageDead(_3);
_8 = Len((*_2));
_9 = const 3_usize;
- _10 = Eq(move _8, const 3_usize);
- switchInt(move _10) -> [0: bb1, otherwise: bb2];
+ nop;
+ switchInt(move _9) -> [3: bb2, otherwise: bb1];
+ switchInt(move _8) -> [3: bb2, otherwise: bb1];
}
bb1: {
_15 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind continue;
_11 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind continue;
}
bb2: {
StorageDead(_5);
StorageDead(_2);
StorageDead(_4);
return;
}
}

View File

@ -38,11 +38,13 @@ fn mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> U) -> () {
bb2: {
StorageLive(_7);
StorageLive(_6);
_6 = &mut _5;
_7 = <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next(_6) -> [return: bb3, unwind: bb9];
_7 = <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next(move _6) -> [return: bb3, unwind: bb9];
}
bb3: {
StorageDead(_6);
_8 = discriminant(_7);
switchInt(move _8) -> [0: bb4, 1: bb6, otherwise: bb8];
}

View File

@ -30,11 +30,13 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
bb2: {
StorageLive(_5);
StorageLive(_4);
_4 = &mut _3;
_5 = <std::vec::IntoIter<impl Sized> as Iterator>::next(_4) -> [return: bb3, unwind: bb9];
_5 = <std::vec::IntoIter<impl Sized> as Iterator>::next(move _4) -> [return: bb3, unwind: bb9];
}
bb3: {
StorageDead(_4);
_6 = discriminant(_5);
switchInt(move _6) -> [0: bb4, 1: bb6, otherwise: bb8];
}

View File

@ -40,11 +40,13 @@ fn inclusive_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () {
bb1: {
StorageLive(_7);
StorageLive(_6);
_6 = &mut _5;
_7 = <RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(_6) -> [return: bb2, unwind unreachable];
}
bb2: {
StorageDead(_6);
_8 = discriminant(_7);
switchInt(move _8) -> [0: bb3, 1: bb5, otherwise: bb7];
}

View File

@ -40,11 +40,13 @@ fn inclusive_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () {
bb1: {
StorageLive(_7);
StorageLive(_6);
_6 = &mut _5;
_7 = <RangeInclusive<u32> as iter::range::RangeInclusiveIteratorImpl>::spec_next(_6) -> [return: bb2, unwind: bb8];
}
bb2: {
StorageDead(_6);
_8 = discriminant(_7);
switchInt(move _8) -> [0: bb3, 1: bb5, otherwise: bb7];
}

View File

@ -53,7 +53,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
}
bb0: {
StorageLive(_8);
StorageLive(_7);
StorageLive(_4);
StorageLive(_3);
_3 = Len((*_1));
@ -68,7 +68,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
}
bb2: {
StorageLive(_7);
StorageLive(_8);
StorageLive(_5);
_5 = &raw mut (*_1);
StorageLive(_9);
@ -79,14 +79,14 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
StorageDead(_9);
StorageDead(_5);
_8 = &mut (*_7);
_0 = Option::<&mut u32>::Some(_8);
StorageDead(_7);
_0 = Option::<&mut u32>::Some(move _8);
StorageDead(_8);
goto -> bb3;
}
bb3: {
StorageDead(_4);
StorageDead(_8);
StorageDead(_7);
return;
}
}

View File

@ -53,7 +53,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
}
bb0: {
StorageLive(_8);
StorageLive(_7);
StorageLive(_4);
StorageLive(_3);
_3 = Len((*_1));
@ -68,7 +68,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
}
bb2: {
StorageLive(_7);
StorageLive(_8);
StorageLive(_5);
_5 = &raw mut (*_1);
StorageLive(_9);
@ -79,14 +79,14 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
StorageDead(_9);
StorageDead(_5);
_8 = &mut (*_7);
_0 = Option::<&mut u32>::Some(_8);
StorageDead(_7);
_0 = Option::<&mut u32>::Some(move _8);
StorageDead(_8);
goto -> bb3;
}
bb3: {
StorageDead(_4);
StorageDead(_8);
StorageDead(_7);
return;
}
}

View File

@ -61,7 +61,6 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) ->
bb0: {
_3 = move (_2.0: usize);
_4 = move (_2.1: usize);
StorageLive(_13);
StorageLive(_5);
_5 = &raw mut (*_1);
StorageLive(_14);
@ -92,7 +91,6 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) ->
StorageDead(_15);
StorageDead(_5);
_0 = &mut (*_13);
StorageDead(_13);
return;
}
}

View File

@ -61,7 +61,6 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) ->
bb0: {
_3 = move (_2.0: usize);
_4 = move (_2.1: usize);
StorageLive(_13);
StorageLive(_5);
_5 = &raw mut (*_1);
StorageLive(_14);
@ -92,7 +91,6 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) ->
StorageDead(_15);
StorageDead(_5);
_0 = &mut (*_13);
StorageDead(_13);
return;
}
}

View File

@ -152,11 +152,13 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
bb4: {
StorageLive(_17);
StorageLive(_16);
_16 = &mut _15;
_17 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(_16) -> [return: bb5, unwind unreachable];
_17 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(move _16) -> [return: bb5, unwind unreachable];
}
bb5: {
StorageDead(_16);
_18 = discriminant(_17);
switchInt(move _18) -> [0: bb6, 1: bb8, otherwise: bb10];
}

View File

@ -152,11 +152,13 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
bb4: {
StorageLive(_17);
StorageLive(_16);
_16 = &mut _15;
_17 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(_16) -> [return: bb5, unwind: bb11];
_17 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(move _16) -> [return: bb5, unwind: bb11];
}
bb5: {
StorageDead(_16);
_18 = discriminant(_17);
switchInt(move _18) -> [0: bb6, 1: bb8, otherwise: bb10];
}

View File

@ -140,11 +140,13 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
bb4: {
StorageLive(_16);
StorageLive(_15);
_15 = &mut _14;
_16 = <std::slice::Iter<'_, T> as Iterator>::next(_15) -> [return: bb5, unwind unreachable];
_16 = <std::slice::Iter<'_, T> as Iterator>::next(move _15) -> [return: bb5, unwind unreachable];
}
bb5: {
StorageDead(_15);
_17 = discriminant(_16);
switchInt(move _17) -> [0: bb6, 1: bb8, otherwise: bb10];
}

View File

@ -140,11 +140,13 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
bb4: {
StorageLive(_16);
StorageLive(_15);
_15 = &mut _14;
_16 = <std::slice::Iter<'_, T> as Iterator>::next(_15) -> [return: bb5, unwind: bb11];
_16 = <std::slice::Iter<'_, T> as Iterator>::next(move _15) -> [return: bb5, unwind: bb11];
}
bb5: {
StorageDead(_15);
_17 = discriminant(_16);
switchInt(move _17) -> [0: bb6, 1: bb8, otherwise: bb10];
}

View File

@ -3,17 +3,19 @@
fn outer(_1: u8) -> u8 {
debug v => _1; // in scope 0 at $DIR/spans.rs:10:14: 10:15
let mut _0: u8; // return place in scope 0 at $DIR/spans.rs:10:24: 10:26
let _2: &u8; // in scope 0 at $DIR/spans.rs:11:11: 11:13
let mut _2: &u8; // in scope 0 at $DIR/spans.rs:11:11: 11:13
bb0: {
StorageLive(_2); // scope 0 at $DIR/spans.rs:11:11: 11:13
_2 = &_1; // scope 0 at $DIR/spans.rs:11:11: 11:13
_0 = inner(_2) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/spans.rs:11:5: 11:14
_0 = inner(move _2) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/spans.rs:11:5: 11:14
// mir::Constant
// + span: $DIR/spans.rs:11:5: 11:10
// + literal: Const { ty: for<'a> fn(&'a u8) -> u8 {inner}, val: Value(<ZST>) }
}
bb1: {
StorageDead(_2); // scope 0 at $DIR/spans.rs:11:13: 11:14
return; // scope 0 at $DIR/spans.rs:12:2: 12:2
}
}

View File

@ -3,17 +3,19 @@
fn outer(_1: u8) -> u8 {
debug v => _1; // in scope 0 at $DIR/spans.rs:10:14: 10:15
let mut _0: u8; // return place in scope 0 at $DIR/spans.rs:10:24: 10:26
let _2: &u8; // in scope 0 at $DIR/spans.rs:11:11: 11:13
let mut _2: &u8; // in scope 0 at $DIR/spans.rs:11:11: 11:13
bb0: {
StorageLive(_2); // scope 0 at $DIR/spans.rs:11:11: 11:13
_2 = &_1; // scope 0 at $DIR/spans.rs:11:11: 11:13
_0 = inner(_2) -> [return: bb1, unwind continue]; // scope 0 at $DIR/spans.rs:11:5: 11:14
_0 = inner(move _2) -> [return: bb1, unwind continue]; // scope 0 at $DIR/spans.rs:11:5: 11:14
// mir::Constant
// + span: $DIR/spans.rs:11:5: 11:10
// + literal: Const { ty: for<'a> fn(&'a u8) -> u8 {inner}, val: Value(<ZST>) }
}
bb1: {
StorageDead(_2); // scope 0 at $DIR/spans.rs:11:13: 11:14
return; // scope 0 at $DIR/spans.rs:12:2: 12:2
}
}