Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Operand::Move to Operand::Copy #105190

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod instcombine;
mod lower_intrinsics;
mod lower_slice_len;
mod match_branches;
mod move_to_copy;
mod multiple_return_terminators;
mod normalize_array_len;
mod nrvo;
Expand Down Expand Up @@ -557,6 +558,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&normalize_array_len::NormalizeArrayLen, // has to run after `slice::len` lowering
&unreachable_prop::UnreachablePropagation,
&uninhabited_enum_branching::UninhabitedEnumBranching,
&move_to_copy::MoveToCopy,
&o1(simplify::SimplifyCfg::new("after-uninhabited-enum-branching")),
&inline::Inline,
&remove_storage_markers::RemoveStorageMarkers,
Expand Down
97 changes: 97 additions & 0 deletions compiler/rustc_mir_transform/src/move_to_copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use crate::MirPass;
use rustc_middle::mir::visit::MutVisitor;
use rustc_middle::mir::*;
use rustc_middle::ty::{FnDef, TyCtxt};

/// Within-block replacement empowers ConstProp, DataflowConstProp, and DestProp. (based on looking
/// at the tests)
///
/// SwitchInt replacement causes some basic blocks which only contain a goto to be deleted. (based
/// on looking at core and alloc)
/// It also makes EarlyOtherwiseBranch much more effective, but that pass is currently marked as
/// unsound so this effect is not useful yet.
///
/// At time of writing, Assert replacement has no effect. Most likely we don't often need the
/// asserted predicate for anything else, and aren't smart enough to optimize into a form that
/// could use it anyway.
///
/// Enabling this pass for Call arguments breaks the moved-from local reuse optimization that
/// Inline does, so without DestinationPropagation, modifying call arguments is just a regression.
/// Except for calls to intrinsics, because those cannot be inlined.
pub struct MoveToCopy;

impl<'tcx> MirPass<'tcx> for MoveToCopy {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let mut visitor = MoveToCopyVisitor { tcx };
let mut call_visitor = MoveToCopyCallVisitor { tcx, local_decls: &body.local_decls };
for (block, block_data) in body.basic_blocks.as_mut().iter_enumerated_mut() {
for (statement_index, statement) in block_data.statements.iter_mut().enumerate() {
visitor.visit_statement(statement, Location { block, statement_index });
}
let Some(terminator) = &mut block_data.terminator else {
continue;
};
match &terminator.kind {
TerminatorKind::SwitchInt { .. } | TerminatorKind::Assert { .. } => {
visitor.visit_terminator(
terminator,
Location { block, statement_index: block_data.statements.len() },
);
}
TerminatorKind::Call { func, .. } => {
let func_ty = func.ty(call_visitor.local_decls, tcx);
let is_intrinsic = if let FnDef(def_id, _) = *func_ty.kind() {
tcx.is_intrinsic(def_id)
} else {
false
};
if is_intrinsic || tcx.sess.mir_opt_level() >= 3 {
call_visitor.visit_terminator(
terminator,
Location { block, statement_index: block_data.statements.len() },
);
}
}
_ => {}
}
}
}
}

struct MoveToCopyCallVisitor<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
local_decls: &'a LocalDecls<'tcx>,
}

impl<'a, 'tcx> MutVisitor<'tcx> for MoveToCopyCallVisitor<'a, 'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
if let Operand::Move(place) = operand {
let ty = place.ty(self.local_decls, self.tcx).ty;
if ty.is_numeric() || ty.is_bool() || ty.is_char() {
*operand = Operand::Copy(*place);
}
}
self.super_operand(operand, location);
}
}

struct MoveToCopyVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
}

impl<'tcx> MutVisitor<'tcx> for MoveToCopyVisitor<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
if let Operand::Move(place) = operand {
*operand = Operand::Copy(*place);
}
self.super_operand(operand, location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

bb0: {
_2 = discriminant(_1); // scope 0 at $DIR/76803_regression.rs:+1:11: +1:12
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/76803_regression.rs:+1:5: +1:12
switchInt(_2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/76803_regression.rs:+1:5: +1:12
}

bb1: {
_0 = move _1; // scope 0 at $DIR/76803_regression.rs:+3:14: +3:15
_0 = _1; // scope 0 at $DIR/76803_regression.rs:+3:14: +3:15
goto -> bb3; // scope 0 at $DIR/76803_regression.rs:+3:14: +3:15
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/mir-opt/bool_compare.opt1.InstCombine.diff
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
StorageLive(_2); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
StorageLive(_3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:9
_3 = _1; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:9
- _2 = Ne(move _3, const true); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
- _2 = Ne(_3, const true); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
+ _2 = Not(_3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
switchInt(_2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
}

bb1: {
Expand Down
6 changes: 3 additions & 3 deletions src/test/mir-opt/bool_compare.opt2.InstCombine.diff
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
StorageLive(_2); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
StorageLive(_3); // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
_3 = _1; // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
- _2 = Ne(const true, move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
- _2 = Ne(const true, _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
+ _2 = Not(_3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
switchInt(_2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
}

bb1: {
Expand Down
6 changes: 3 additions & 3 deletions src/test/mir-opt/bool_compare.opt3.InstCombine.diff
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
StorageLive(_2); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
StorageLive(_3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:9
_3 = _1; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:9
- _2 = Eq(move _3, const false); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
- _2 = Eq(_3, const false); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
+ _2 = Not(_3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
switchInt(_2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
}

bb1: {
Expand Down
6 changes: 3 additions & 3 deletions src/test/mir-opt/bool_compare.opt4.InstCombine.diff
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
StorageLive(_2); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
StorageLive(_3); // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
_3 = _1; // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
- _2 = Eq(const false, move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
- _2 = Eq(const false, _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
+ _2 = Not(_3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
switchInt(_2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
}

bb1: {
Expand Down
10 changes: 5 additions & 5 deletions src/test/mir-opt/combine_array_len.norm2.InstCombine.diff
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
- _4 = Len(_1); // scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17
+ _4 = const 2_usize; // scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17
_5 = Lt(_3, _4); // scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17
assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17
assert(_5, "index out of bounds: the length is {} but the index is {}", _4, _3) -> bb1; // scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17
}

bb1: {
Expand All @@ -44,7 +44,7 @@
- _8 = Len(_1); // scope 1 at $DIR/combine_array_len.rs:+2:13: +2:17
+ _8 = const 2_usize; // scope 1 at $DIR/combine_array_len.rs:+2:13: +2:17
_9 = Lt(_7, _8); // scope 1 at $DIR/combine_array_len.rs:+2:13: +2:17
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> bb2; // scope 1 at $DIR/combine_array_len.rs:+2:13: +2:17
assert(_9, "index out of bounds: the length is {} but the index is {}", _8, _7) -> bb2; // scope 1 at $DIR/combine_array_len.rs:+2:13: +2:17
}

bb2: {
Expand All @@ -55,18 +55,18 @@
_11 = _2; // scope 2 at $DIR/combine_array_len.rs:+3:5: +3:6
StorageLive(_12); // scope 2 at $DIR/combine_array_len.rs:+3:7: +3:8
_12 = _2; // scope 2 at $DIR/combine_array_len.rs:+3:7: +3:8
_10 = Mul(move _11, move _12); // scope 2 at $DIR/combine_array_len.rs:+3:5: +3:8
_10 = Mul(_11, _12); // scope 2 at $DIR/combine_array_len.rs:+3:5: +3:8
StorageDead(_12); // scope 2 at $DIR/combine_array_len.rs:+3:7: +3:8
StorageDead(_11); // scope 2 at $DIR/combine_array_len.rs:+3:7: +3:8
StorageLive(_13); // scope 2 at $DIR/combine_array_len.rs:+3:11: +3:14
StorageLive(_14); // scope 2 at $DIR/combine_array_len.rs:+3:11: +3:12
_14 = _6; // scope 2 at $DIR/combine_array_len.rs:+3:11: +3:12
StorageLive(_15); // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14
_15 = _6; // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14
_13 = Mul(move _14, move _15); // scope 2 at $DIR/combine_array_len.rs:+3:11: +3:14
_13 = Mul(_14, _15); // scope 2 at $DIR/combine_array_len.rs:+3:11: +3:14
StorageDead(_15); // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14
StorageDead(_14); // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14
_0 = Add(move _10, move _13); // scope 2 at $DIR/combine_array_len.rs:+3:5: +3:14
_0 = Add(_10, _13); // scope 2 at $DIR/combine_array_len.rs:+3:5: +3:14
StorageDead(_13); // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14
StorageDead(_10); // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14
StorageDead(_6); // scope 1 at $DIR/combine_array_len.rs:+4:1: +4:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
bb3: {
StorageDead(_9); // scope 0 at $DIR/combine_clone_of_primitives.rs:10:15: 10:16
Deinit(_0); // scope 0 at $DIR/combine_clone_of_primitives.rs:+0:10: +0:15
(_0.0: T) = move _2; // scope 0 at $DIR/combine_clone_of_primitives.rs:+0:10: +0:15
(_0.1: u64) = move _5; // scope 0 at $DIR/combine_clone_of_primitives.rs:+0:10: +0:15
(_0.2: [f32; 3]) = move _8; // scope 0 at $DIR/combine_clone_of_primitives.rs:+0:10: +0:15
(_0.0: T) = _2; // scope 0 at $DIR/combine_clone_of_primitives.rs:+0:10: +0:15
(_0.1: u64) = _5; // scope 0 at $DIR/combine_clone_of_primitives.rs:+0:10: +0:15
(_0.2: [f32; 3]) = _8; // scope 0 at $DIR/combine_clone_of_primitives.rs:+0:10: +0:15
StorageDead(_8); // scope 0 at $DIR/combine_clone_of_primitives.rs:+0:14: +0:15
StorageDead(_5); // scope 0 at $DIR/combine_clone_of_primitives.rs:+0:14: +0:15
StorageDead(_2); // scope 0 at $DIR/combine_clone_of_primitives.rs:+0:14: +0:15
Expand Down
6 changes: 3 additions & 3 deletions src/test/mir-opt/const_goto.issue_77355_opt.ConstGoto.diff
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
bb0: {
- StorageLive(_2); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- _3 = discriminant(_1); // scope 0 at $DIR/const_goto.rs:+1:17: +1:20
- switchInt(move _3) -> [1: bb2, 2: bb2, otherwise: bb1]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- switchInt(_3) -> [1: bb2, 2: bb2, otherwise: bb1]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
+ _2 = discriminant(_1); // scope 0 at $DIR/const_goto.rs:+1:17: +1:20
+ switchInt(move _2) -> [1: bb2, 2: bb2, otherwise: bb1]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
+ switchInt(_2) -> [1: bb2, 2: bb2, otherwise: bb1]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
}

bb1: {
Expand All @@ -29,7 +29,7 @@
- }
-
- bb3: {
- switchInt(move _2) -> [0: bb5, otherwise: bb4]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- switchInt(_2) -> [0: bb5, otherwise: bb4]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- }
-
- bb4: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
- StorageLive(_5); // scope 0 at $DIR/const_goto_storage.rs:+2:21: +2:52
- StorageLive(_6); // scope 0 at $DIR/const_goto_storage.rs:+2:24: +2:28
- _6 = const true; // scope 0 at $DIR/const_goto_storage.rs:+2:24: +2:28
- switchInt(move _6) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/const_goto_storage.rs:+2:24: +2:28
- switchInt(_6) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/const_goto_storage.rs:+2:24: +2:28
+ StorageLive(_2); // scope 0 at $DIR/const_goto_storage.rs:+2:24: +2:28
+ _2 = const true; // scope 0 at $DIR/const_goto_storage.rs:+2:24: +2:28
+ switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/const_goto_storage.rs:+2:24: +2:28
+ switchInt(_2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/const_goto_storage.rs:+2:24: +2:28
}

bb1: {
Expand All @@ -41,7 +41,7 @@
-
- bb3: {
- StorageDead(_6); // scope 0 at $DIR/const_goto_storage.rs:+2:51: +2:52
- switchInt(move _5) -> [0: bb5, otherwise: bb4]; // scope 0 at $DIR/const_goto_storage.rs:+2:21: +2:52
- switchInt(_5) -> [0: bb5, otherwise: bb4]; // scope 0 at $DIR/const_goto_storage.rs:+2:21: +2:52
- }
-
- bb4: {
Expand All @@ -56,7 +56,7 @@
-
- bb6: {
- StorageDead(_5); // scope 0 at $DIR/const_goto_storage.rs:+2:75: +2:76
- switchInt(move _4) -> [0: bb8, otherwise: bb7]; // scope 0 at $DIR/const_goto_storage.rs:+2:18: +2:76
- switchInt(_4) -> [0: bb8, otherwise: bb7]; // scope 0 at $DIR/const_goto_storage.rs:+2:18: +2:76
- }
-
- bb7: {
Expand All @@ -70,7 +70,7 @@
- }
-
- bb9: {
- switchInt(move _3) -> [0: bb11, otherwise: bb10]; // scope 0 at $DIR/const_goto_storage.rs:+2:15: +6:10
- switchInt(_3) -> [0: bb11, otherwise: bb10]; // scope 0 at $DIR/const_goto_storage.rs:+2:15: +6:10
- }
-
- bb10: {
Expand Down
2 changes: 1 addition & 1 deletion src/test/mir-opt/const_prop/aggregate.main.ConstProp.diff
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
(_3.1: i32) = const 1_i32; // scope 0 at $DIR/aggregate.rs:+1:13: +1:22
(_3.2: i32) = const 2_i32; // scope 0 at $DIR/aggregate.rs:+1:13: +1:22
- _2 = (_3.1: i32); // scope 0 at $DIR/aggregate.rs:+1:13: +1:24
- _1 = Add(move _2, const 0_i32); // scope 0 at $DIR/aggregate.rs:+1:13: +1:28
- _1 = Add(_2, const 0_i32); // scope 0 at $DIR/aggregate.rs:+1:13: +1:28
+ _2 = const 1_i32; // scope 0 at $DIR/aggregate.rs:+1:13: +1:24
+ _1 = const 1_i32; // scope 0 at $DIR/aggregate.rs:+1:13: +1:28
StorageDead(_2); // scope 0 at $DIR/aggregate.rs:+1:27: +1:28
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
_3 = const 2_usize; // scope 0 at $DIR/array_index.rs:+1:31: +1:32
- _4 = Len(_2); // scope 0 at $DIR/array_index.rs:+1:18: +1:33
- _5 = Lt(_3, _4); // scope 0 at $DIR/array_index.rs:+1:18: +1:33
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
- assert(_5, "index out of bounds: the length is {} but the index is {}", _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
+ _4 = const 4_usize; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
+ _5 = const true; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
+ assert(const true, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
+ assert(const true, "index out of bounds: the length is {} but the index is {}", _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
_3 = const 2_usize; // scope 0 at $DIR/array_index.rs:+1:31: +1:32
- _4 = Len(_2); // scope 0 at $DIR/array_index.rs:+1:18: +1:33
- _5 = Lt(_3, _4); // scope 0 at $DIR/array_index.rs:+1:18: +1:33
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
- assert(_5, "index out of bounds: the length is {} but the index is {}", _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
+ _4 = const 4_usize; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
+ _5 = const true; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
+ assert(const true, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
+ assert(const true, "index out of bounds: the length is {} but the index is {}", _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:+1:18: +1:33
}

bb1: {
Expand Down
Loading