From a7d48fcc15f9f71917927144e1e1ece4f6f5e5a4 Mon Sep 17 00:00:00 2001 From: DianQK Date: Sun, 28 Jul 2024 13:28:44 +0800 Subject: [PATCH] Simplify some of the copy-like forms --- .../rustc_mir_transform/src/instsimplify.rs | 18 +- ...l_copy.InstSimplify-after-simplifycfg.diff | 47 +++++ ...t_type.InstSimplify-after-simplifycfg.diff | 46 +++++ ...hanged.InstSimplify-after-simplifycfg.diff | 47 +++++ ...y_move.InstSimplify-after-simplifycfg.diff | 46 +++++ ..._ret_2.InstSimplify-after-simplifycfg.diff | 68 ++++++++ ...hanged.InstSimplify-after-simplifycfg.diff | 53 ++++++ ...nged_2.InstSimplify-after-simplifycfg.diff | 52 ++++++ ...ruct_1.InstSimplify-after-simplifycfg.diff | 13 ++ ...ruct_2.InstSimplify-after-simplifycfg.diff | 13 ++ ...t_copy.InstSimplify-after-simplifycfg.diff | 68 ++++++++ tests/mir-opt/instsimplify/copy_like.rs | 160 ++++++++++++++++++ ..._index.InstSimplify-after-simplifycfg.diff | 35 ++++ 13 files changed, 663 insertions(+), 3 deletions(-) create mode 100644 tests/mir-opt/instsimplify/copy_like.all_copy.InstSimplify-after-simplifycfg.diff create mode 100644 tests/mir-opt/instsimplify/copy_like.all_copy_different_type.InstSimplify-after-simplifycfg.diff create mode 100644 tests/mir-opt/instsimplify/copy_like.all_copy_has_changed.InstSimplify-after-simplifycfg.diff create mode 100644 tests/mir-opt/instsimplify/copy_like.all_copy_move.InstSimplify-after-simplifycfg.diff create mode 100644 tests/mir-opt/instsimplify/copy_like.all_copy_ret_2.InstSimplify-after-simplifycfg.diff create mode 100644 tests/mir-opt/instsimplify/copy_like.all_copy_use_changed.InstSimplify-after-simplifycfg.diff create mode 100644 tests/mir-opt/instsimplify/copy_like.all_copy_use_changed_2.InstSimplify-after-simplifycfg.diff create mode 100644 tests/mir-opt/instsimplify/copy_like.empty_struct_1.InstSimplify-after-simplifycfg.diff create mode 100644 tests/mir-opt/instsimplify/copy_like.empty_struct_2.InstSimplify-after-simplifycfg.diff create mode 100644 tests/mir-opt/instsimplify/copy_like.nest_copy.InstSimplify-after-simplifycfg.diff create mode 100644 tests/mir-opt/instsimplify/copy_like.rs create mode 100644 tests/mir-opt/instsimplify/copy_like.same_type_different_index.InstSimplify-after-simplifycfg.diff diff --git a/compiler/rustc_mir_transform/src/instsimplify.rs b/compiler/rustc_mir_transform/src/instsimplify.rs index 51bc648bf95a9..c84fb32992bbe 100644 --- a/compiler/rustc_mir_transform/src/instsimplify.rs +++ b/compiler/rustc_mir_transform/src/instsimplify.rs @@ -265,8 +265,18 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> { } // Collect available assignments, including those transformed from `Aggregate`. if let Some(local) = dest.as_local() { - assignments[local] = if let Rvalue::Use(Operand::Copy(place)) = rvalue { - Some(*place) + assignments[local] = if let Rvalue::Use(operand) = rvalue + && let Some(place) = operand.place() + { + if let Some(rvalue_local) = place.as_local() { + let place = assignments[rvalue_local]; + if operand.is_move() { + assignments[rvalue_local] = None; + } + place + } else { + Some(place) + } } else { // This assignment generally comes from debuginfo (e.g., Ref), // but we still need to check if a local is being overridden. @@ -277,7 +287,9 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> { assignments.reset_all(None); } } - StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => {} + StatementKind::StorageLive(_) + | StatementKind::StorageDead(_) + | StatementKind::Nop => {} _ => { assignments.reset_all(None); } diff --git a/tests/mir-opt/instsimplify/copy_like.all_copy.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.all_copy.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..0f0408cc4c1e9 --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.all_copy.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,47 @@ +- // MIR for `all_copy` before InstSimplify-after-simplifycfg ++ // MIR for `all_copy` after InstSimplify-after-simplifycfg + + fn all_copy(_1: &AllCopy) -> AllCopy { + debug v => _1; + let mut _0: AllCopy; + let _2: i32; + let mut _5: i32; + let mut _6: u64; + let mut _7: [i8; 3]; + scope 1 { + debug a => _2; + let _3: u64; + scope 2 { + debug b => _3; + let _4: [i8; 3]; + scope 3 { + debug c => _4; + } + } + } + + bb0: { + StorageLive(_2); + _2 = ((*_1).0: i32); + StorageLive(_3); + _3 = ((*_1).1: u64); + StorageLive(_4); + _4 = ((*_1).2: [i8; 3]); + StorageLive(_5); + _5 = _2; + StorageLive(_6); + _6 = _3; + StorageLive(_7); + _7 = _4; +- _0 = AllCopy { a: move _5, b: move _6, c: move _7 }; ++ _0 = (*_1); + StorageDead(_7); + StorageDead(_6); + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); + StorageDead(_2); + return; + } + } + diff --git a/tests/mir-opt/instsimplify/copy_like.all_copy_different_type.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.all_copy_different_type.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..5bbe3328fbb53 --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.all_copy_different_type.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,46 @@ +- // MIR for `all_copy_different_type` before InstSimplify-after-simplifycfg ++ // MIR for `all_copy_different_type` after InstSimplify-after-simplifycfg + + fn all_copy_different_type(_1: &AllCopy) -> AllCopy2 { + debug v => _1; + let mut _0: AllCopy2; + let _2: i32; + let mut _5: i32; + let mut _6: u64; + let mut _7: [i8; 3]; + scope 1 { + debug a => _2; + let _3: u64; + scope 2 { + debug b => _3; + let _4: [i8; 3]; + scope 3 { + debug c => _4; + } + } + } + + bb0: { + StorageLive(_2); + _2 = ((*_1).0: i32); + StorageLive(_3); + _3 = ((*_1).1: u64); + StorageLive(_4); + _4 = ((*_1).2: [i8; 3]); + StorageLive(_5); + _5 = _2; + StorageLive(_6); + _6 = _3; + StorageLive(_7); + _7 = _4; + _0 = AllCopy2 { a: move _5, b: move _6, c: move _7 }; + StorageDead(_7); + StorageDead(_6); + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); + StorageDead(_2); + return; + } + } + diff --git a/tests/mir-opt/instsimplify/copy_like.all_copy_has_changed.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.all_copy_has_changed.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..037c114da0318 --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.all_copy_has_changed.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,47 @@ +- // MIR for `all_copy_has_changed` before InstSimplify-after-simplifycfg ++ // MIR for `all_copy_has_changed` after InstSimplify-after-simplifycfg + + fn all_copy_has_changed(_1: &mut AllCopy) -> AllCopy { + debug v => _1; + let mut _0: AllCopy; + let _2: i32; + let mut _5: i32; + let mut _6: u64; + let mut _7: [i8; 3]; + scope 1 { + debug a => _2; + let _3: u64; + scope 2 { + debug b => _3; + let _4: [i8; 3]; + scope 3 { + debug c => _4; + } + } + } + + bb0: { + StorageLive(_2); + _2 = ((*_1).0: i32); + StorageLive(_3); + _3 = ((*_1).1: u64); + StorageLive(_4); + _4 = ((*_1).2: [i8; 3]); + ((*_1).0: i32) = const 1_i32; + StorageLive(_5); + _5 = _2; + StorageLive(_6); + _6 = _3; + StorageLive(_7); + _7 = _4; + _0 = AllCopy { a: move _5, b: move _6, c: move _7 }; + StorageDead(_7); + StorageDead(_6); + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); + StorageDead(_2); + return; + } + } + diff --git a/tests/mir-opt/instsimplify/copy_like.all_copy_move.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.all_copy_move.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..0fde7c3d4e53b --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.all_copy_move.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,46 @@ +- // MIR for `all_copy_move` before InstSimplify-after-simplifycfg ++ // MIR for `all_copy_move` after InstSimplify-after-simplifycfg + + fn all_copy_move(_1: AllCopy) -> AllCopy { + debug v => _1; + let mut _0: AllCopy; + let _2: i32; + let mut _5: i32; + let mut _6: u64; + let mut _7: [i8; 3]; + scope 1 { + debug a => _2; + let _3: u64; + scope 2 { + debug b => _3; + let _4: [i8; 3]; + scope 3 { + debug c => _4; + } + } + } + + bb0: { + StorageLive(_2); + _2 = (_1.0: i32); + StorageLive(_3); + _3 = (_1.1: u64); + StorageLive(_4); + _4 = (_1.2: [i8; 3]); + StorageLive(_5); + _5 = _2; + StorageLive(_6); + _6 = _3; + StorageLive(_7); + _7 = _4; + _0 = AllCopy { a: move _5, b: move _6, c: move _7 }; + StorageDead(_7); + StorageDead(_6); + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); + StorageDead(_2); + return; + } + } + diff --git a/tests/mir-opt/instsimplify/copy_like.all_copy_ret_2.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.all_copy_ret_2.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..f6c91b6535fed --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.all_copy_ret_2.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,68 @@ +- // MIR for `all_copy_ret_2` before InstSimplify-after-simplifycfg ++ // MIR for `all_copy_ret_2` after InstSimplify-after-simplifycfg + + fn all_copy_ret_2(_1: &AllCopy) -> (AllCopy, AllCopy) { + debug v => _1; + let mut _0: (AllCopy, AllCopy); + let _2: i32; + let mut _5: AllCopy; + let mut _6: i32; + let mut _7: u64; + let mut _8: [i8; 3]; + let mut _9: AllCopy; + let mut _10: i32; + let mut _11: u64; + let mut _12: [i8; 3]; + scope 1 { + debug a => _2; + let _3: u64; + scope 2 { + debug b => _3; + let _4: [i8; 3]; + scope 3 { + debug c => _4; + } + } + } + + bb0: { + StorageLive(_2); + _2 = ((*_1).0: i32); + StorageLive(_3); + _3 = ((*_1).1: u64); + StorageLive(_4); + _4 = ((*_1).2: [i8; 3]); + StorageLive(_5); + StorageLive(_6); + _6 = _2; + StorageLive(_7); + _7 = _3; + StorageLive(_8); + _8 = _4; +- _5 = AllCopy { a: move _6, b: move _7, c: move _8 }; ++ _5 = (*_1); + StorageDead(_8); + StorageDead(_7); + StorageDead(_6); + StorageLive(_9); + StorageLive(_10); + _10 = _2; + StorageLive(_11); + _11 = _3; + StorageLive(_12); + _12 = _4; +- _9 = AllCopy { a: move _10, b: move _11, c: move _12 }; ++ _9 = (*_1); + StorageDead(_12); + StorageDead(_11); + StorageDead(_10); + _0 = (move _5, move _9); + StorageDead(_9); + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); + StorageDead(_2); + return; + } + } + diff --git a/tests/mir-opt/instsimplify/copy_like.all_copy_use_changed.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.all_copy_use_changed.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..b7ea5b62eb0f0 --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.all_copy_use_changed.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,53 @@ +- // MIR for `all_copy_use_changed` before InstSimplify-after-simplifycfg ++ // MIR for `all_copy_use_changed` after InstSimplify-after-simplifycfg + + fn all_copy_use_changed(_1: &mut AllCopy) -> AllCopy { + debug v => _1; + let mut _0: AllCopy; + let mut _2: i32; + let mut _3: i32; + let mut _6: i32; + let mut _7: u64; + let mut _8: [i8; 3]; + scope 1 { + debug a => _2; + let _4: u64; + scope 2 { + debug b => _4; + let _5: [i8; 3]; + scope 3 { + debug c => _5; + } + } + } + + bb0: { + StorageLive(_2); + _2 = ((*_1).0: i32); + ((*_1).0: i32) = const 1_i32; + StorageLive(_3); + _3 = ((*_1).0: i32); + _2 = move _3; + StorageDead(_3); + StorageLive(_4); + _4 = ((*_1).1: u64); + StorageLive(_5); + _5 = ((*_1).2: [i8; 3]); + StorageLive(_6); + _6 = _2; + StorageLive(_7); + _7 = _4; + StorageLive(_8); + _8 = _5; +- _0 = AllCopy { a: move _6, b: move _7, c: move _8 }; ++ _0 = (*_1); + StorageDead(_8); + StorageDead(_7); + StorageDead(_6); + StorageDead(_5); + StorageDead(_4); + StorageDead(_2); + return; + } + } + diff --git a/tests/mir-opt/instsimplify/copy_like.all_copy_use_changed_2.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.all_copy_use_changed_2.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..c1a2e6c977623 --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.all_copy_use_changed_2.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,52 @@ +- // MIR for `all_copy_use_changed_2` before InstSimplify-after-simplifycfg ++ // MIR for `all_copy_use_changed_2` after InstSimplify-after-simplifycfg + + fn all_copy_use_changed_2(_1: &mut AllCopy) -> AllCopy { + debug v => _1; + let mut _0: AllCopy; + let mut _2: i32; + let mut _5: i32; + let mut _6: i32; + let mut _7: u64; + let mut _8: [i8; 3]; + scope 1 { + debug a => _2; + let _3: u64; + scope 2 { + debug b => _3; + let _4: [i8; 3]; + scope 3 { + debug c => _4; + } + } + } + + bb0: { + StorageLive(_2); + _2 = ((*_1).0: i32); + StorageLive(_3); + _3 = ((*_1).1: u64); + StorageLive(_4); + _4 = ((*_1).2: [i8; 3]); + ((*_1).0: i32) = const 1_i32; + StorageLive(_5); + _5 = ((*_1).0: i32); + _2 = move _5; + StorageDead(_5); + StorageLive(_6); + _6 = _2; + StorageLive(_7); + _7 = _3; + StorageLive(_8); + _8 = _4; + _0 = AllCopy { a: move _6, b: move _7, c: move _8 }; + StorageDead(_8); + StorageDead(_7); + StorageDead(_6); + StorageDead(_4); + StorageDead(_3); + StorageDead(_2); + return; + } + } + diff --git a/tests/mir-opt/instsimplify/copy_like.empty_struct_1.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.empty_struct_1.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..e39dec643fa2a --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.empty_struct_1.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,13 @@ +- // MIR for `empty_struct_1` before InstSimplify-after-simplifycfg ++ // MIR for `empty_struct_1` after InstSimplify-after-simplifycfg + + fn empty_struct_1(_1: &EmptyStruct1) -> EmptyStruct1 { + debug v => _1; + let mut _0: EmptyStruct1; + + bb0: { + _0 = EmptyStruct1; + return; + } + } + diff --git a/tests/mir-opt/instsimplify/copy_like.empty_struct_2.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.empty_struct_2.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..fbf2b0133f5ed --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.empty_struct_2.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,13 @@ +- // MIR for `empty_struct_2` before InstSimplify-after-simplifycfg ++ // MIR for `empty_struct_2` after InstSimplify-after-simplifycfg + + fn empty_struct_2(_1: &EmptyStruct2) -> EmptyStruct2 { + debug v => _1; + let mut _0: EmptyStruct2; + + bb0: { + _0 = EmptyStruct2; + return; + } + } + diff --git a/tests/mir-opt/instsimplify/copy_like.nest_copy.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.nest_copy.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..aba8b651be31a --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.nest_copy.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,68 @@ +- // MIR for `nest_copy` before InstSimplify-after-simplifycfg ++ // MIR for `nest_copy` after InstSimplify-after-simplifycfg + + fn nest_copy(_1: &NestCopy) -> NestCopy { + debug v => _1; + let mut _0: NestCopy; + let _2: i32; + let mut _6: i32; + let mut _7: u64; + let mut _8: [i8; 3]; + let mut _10: i32; + let mut _11: AllCopy; + scope 1 { + debug a => _2; + let _3: u64; + scope 2 { + debug b => _3; + let _4: [i8; 3]; + scope 3 { + debug c => _4; + let _5: AllCopy; + scope 4 { + debug all_copy => _5; + let _9: i32; + scope 5 { + debug d => _9; + } + } + } + } + } + + bb0: { + StorageLive(_2); + _2 = (((*_1).1: AllCopy).0: i32); + StorageLive(_3); + _3 = (((*_1).1: AllCopy).1: u64); + StorageLive(_4); + _4 = (((*_1).1: AllCopy).2: [i8; 3]); + StorageLive(_5); + StorageLive(_6); + _6 = _2; + StorageLive(_7); + _7 = _3; + StorageLive(_8); + _8 = _4; + _5 = AllCopy { a: move _6, b: move _7, c: move _8 }; + StorageDead(_8); + StorageDead(_7); + StorageDead(_6); + StorageLive(_9); + _9 = ((*_1).0: i32); + StorageLive(_10); + _10 = _9; + StorageLive(_11); + _11 = move _5; + _0 = NestCopy { d: move _10, all_copy: move _11 }; + StorageDead(_11); + StorageDead(_10); + StorageDead(_9); + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); + StorageDead(_2); + return; + } + } + diff --git a/tests/mir-opt/instsimplify/copy_like.rs b/tests/mir-opt/instsimplify/copy_like.rs new file mode 100644 index 0000000000000..143474d0f0c6f --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.rs @@ -0,0 +1,160 @@ +//@ test-mir-pass: InstSimplify-after-simplifycfg + +struct AllCopy { + a: i32, + b: u64, + c: [i8; 3], +} + +// EMIT_MIR copy_like.all_copy.InstSimplify-after-simplifycfg.diff +fn all_copy(v: &AllCopy) -> AllCopy { + // CHECK-LABEL: fn all_copy( + // CHECK: bb0: { + // CHECK-NOT: = AllCopy { {{.*}} }; + // CHECK: _0 = (*_1); + let a = v.a; + let b = v.b; + let c = v.c; + AllCopy { a, b, c } +} + +// FIXME: This can be transformed into `Copy`. +// EMIT_MIR copy_like.all_copy_move.InstSimplify-after-simplifycfg.diff +fn all_copy_move(v: AllCopy) -> AllCopy { + // CHECK-LABEL: fn all_copy_move( + // CHECK: bb0: { + // CHECK-NOT: _0 = (*_1); + // CHECK: = AllCopy { {{.*}} }; + let a = v.a; + let b = v.b; + let c = v.c; + AllCopy { a, b, c } +} + +// EMIT_MIR copy_like.all_copy_ret_2.InstSimplify-after-simplifycfg.diff +fn all_copy_ret_2(v: &AllCopy) -> (AllCopy, AllCopy) { + // CHECK-LABEL: fn all_copy_ret_2( + // CHECK: bb0: { + // CHECK-NOT: = AllCopy { {{.*}} }; + // CHECK: [[V1:_.*]] = (*_1); + // CHECK: [[V2:_.*]] = (*_1); + // CHECK: _0 = (move [[V1]], move [[V2]]); + let a = v.a; + let b = v.b; + let c = v.c; + (AllCopy { a, b, c }, AllCopy { a, b, c }) +} + +struct AllCopy2 { + a: i32, + b: u64, + c: [i8; 3], +} + +// EMIT_MIR copy_like.all_copy_different_type.InstSimplify-after-simplifycfg.diff +fn all_copy_different_type(v: &AllCopy) -> AllCopy2 { + // CHECK-LABEL: fn all_copy_different_type( + // CHECK: bb0: { + // CHECK: _0 = AllCopy2 { {{.*}} }; + let a = v.a; + let b = v.b; + let c = v.c; + AllCopy2 { a, b, c } +} + +struct SameType { + a: i32, + b: i32, +} + +// EMIT_MIR copy_like.same_type_different_index.InstSimplify-after-simplifycfg.diff +fn same_type_different_index(v: &SameType) -> SameType { + // CHECK-LABEL: fn same_type_different_index( + // CHECK: bb0: { + // CHECK: _0 = SameType { {{.*}} }; + let a = v.b; + let b = v.a; + SameType { a, b } +} + +// EMIT_MIR copy_like.all_copy_has_changed.InstSimplify-after-simplifycfg.diff +fn all_copy_has_changed(v: &mut AllCopy) -> AllCopy { + // CHECK-LABEL: fn all_copy_has_changed( + // CHECK: bb0: { + // CHECK: _0 = AllCopy { {{.*}} }; + let a = v.a; + let b = v.b; + let c = v.c; + v.a = 1; + AllCopy { a, b, c } +} + +// EMIT_MIR copy_like.all_copy_use_changed.InstSimplify-after-simplifycfg.diff +fn all_copy_use_changed(v: &mut AllCopy) -> AllCopy { + // CHECK-LABEL: fn all_copy_use_changed( + // CHECK: bb0: { + // CHECK-NOT: = AllCopy { {{.*}} }; + // CHECK: _0 = (*_1); + let mut a = v.a; + v.a = 1; + a = v.a; + let b = v.b; + let c = v.c; + AllCopy { a, b, c } +} + +// We deleted the records for b and c, +// because we couldn't easily determine which field had been updated. +// EMIT_MIR copy_like.all_copy_use_changed_2.InstSimplify-after-simplifycfg.diff +fn all_copy_use_changed_2(v: &mut AllCopy) -> AllCopy { + // CHECK-LABEL: fn all_copy_use_changed_2( + // CHECK: bb0: { + // CHECK-NOT: _0 = (*_1); + // CHECK: = AllCopy { {{.*}} }; + let mut a = v.a; + let b = v.b; + let c = v.c; + v.a = 1; + a = v.a; + AllCopy { a, b, c } +} + +struct NestCopy { + d: i32, + all_copy: AllCopy, +} + +// FIXME: Consider supporting multi-level fields? +// (Note: clone becomes a single-level field after inlining) +// EMIT_MIR copy_like.nest_copy.InstSimplify-after-simplifycfg.diff +fn nest_copy(v: &NestCopy) -> NestCopy { + // CHECK-LABEL: fn nest_copy( + // CHECK: bb0: { + // CHECK: = AllCopy { {{.*}} }; + // CHECK: = NestCopy { {{.*}} }; + let a = v.all_copy.a; + let b = v.all_copy.b; + let c = v.all_copy.c; + let all_copy = AllCopy { a, b, c }; + let d = v.d; + NestCopy { d, all_copy } +} + +struct EmptyStruct1; +struct EmptyStruct2 {} + +// EMIT_MIR copy_like.empty_struct_1.InstSimplify-after-simplifycfg.diff +fn empty_struct_1(v: &EmptyStruct1) -> EmptyStruct1 { + // CHECK-LABEL: fn empty_struct_1( + // CHECK: bb0: { + // CHECK: _0 = EmptyStruct1; + EmptyStruct1 +} + +// EMIT_MIR copy_like.empty_struct_2.InstSimplify-after-simplifycfg.diff +fn empty_struct_2(v: &EmptyStruct2) -> EmptyStruct2 { + // CHECK-LABEL: fn empty_struct_2( + // CHECK: bb0: { + // CHECK: _0 = EmptyStruct2; + EmptyStruct2 {} +} diff --git a/tests/mir-opt/instsimplify/copy_like.same_type_different_index.InstSimplify-after-simplifycfg.diff b/tests/mir-opt/instsimplify/copy_like.same_type_different_index.InstSimplify-after-simplifycfg.diff new file mode 100644 index 0000000000000..4e53266bc42bc --- /dev/null +++ b/tests/mir-opt/instsimplify/copy_like.same_type_different_index.InstSimplify-after-simplifycfg.diff @@ -0,0 +1,35 @@ +- // MIR for `same_type_different_index` before InstSimplify-after-simplifycfg ++ // MIR for `same_type_different_index` after InstSimplify-after-simplifycfg + + fn same_type_different_index(_1: &SameType) -> SameType { + debug v => _1; + let mut _0: SameType; + let _2: i32; + let mut _4: i32; + let mut _5: i32; + scope 1 { + debug a => _2; + let _3: i32; + scope 2 { + debug b => _3; + } + } + + bb0: { + StorageLive(_2); + _2 = ((*_1).1: i32); + StorageLive(_3); + _3 = ((*_1).0: i32); + StorageLive(_4); + _4 = _2; + StorageLive(_5); + _5 = _3; + _0 = SameType { a: move _4, b: move _5 }; + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); + StorageDead(_2); + return; + } + } +