Skip to content

Commit

Permalink
Simplify some of the copy-like forms
Browse files Browse the repository at this point in the history
  • Loading branch information
DianQK committed Jul 29, 2024
1 parent f1e1d90 commit a7d48fc
Show file tree
Hide file tree
Showing 13 changed files with 663 additions and 3 deletions.
18 changes: 15 additions & 3 deletions compiler/rustc_mir_transform/src/instsimplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}

Loading

0 comments on commit a7d48fc

Please sign in to comment.