From 37f83fb11d187aea102340b1be20f3f65c4c2cac Mon Sep 17 00:00:00 2001 From: dianqk Date: Sat, 10 Jan 2026 20:27:08 +0800 Subject: [PATCH] Use Copy in the SwitchInt terminator Move can be used only when both the compared operand and the operand on switch are move operands. This commit directly changes to Copy, because I don't know if Move has beneficial. --- .../src/simplify_comparison_integral.rs | 2 +- ..._exponential_common.GVN.32bit.panic-abort.diff | 2 +- ...exponential_common.GVN.32bit.panic-unwind.diff | 2 +- ..._exponential_common.GVN.64bit.panic-abort.diff | 2 +- ...exponential_common.GVN.64bit.panic-unwind.diff | 2 +- ...ove_comparison.SimplifyComparisonIntegral.diff | 2 +- ...n_int.opt_char.SimplifyComparisonIntegral.diff | 2 +- ...ion_int.opt_i8.SimplifyComparisonIntegral.diff | 2 +- ...t_multiple_ifs.SimplifyComparisonIntegral.diff | 4 ++-- ...t.opt_negative.SimplifyComparisonIntegral.diff | 2 +- ...on_int.opt_u32.SimplifyComparisonIntegral.diff | 2 +- tests/mir-opt/if_condition_int.rs | 15 +++++++-------- ...o_digit.PreCodegen.after.32bit.panic-abort.mir | 2 +- ..._digit.PreCodegen.after.32bit.panic-unwind.mir | 2 +- ...o_digit.PreCodegen.after.64bit.panic-abort.mir | 2 +- ..._digit.PreCodegen.after.64bit.panic-unwind.mir | 2 +- 16 files changed, 23 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_mir_transform/src/simplify_comparison_integral.rs b/compiler/rustc_mir_transform/src/simplify_comparison_integral.rs index 53a796b1179a..b7445a0f7c2e 100644 --- a/compiler/rustc_mir_transform/src/simplify_comparison_integral.rs +++ b/compiler/rustc_mir_transform/src/simplify_comparison_integral.rs @@ -135,7 +135,7 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyComparisonIntegral { let terminator = bb.terminator_mut(); terminator.kind = - TerminatorKind::SwitchInt { discr: Operand::Move(opt.to_switch_on), targets }; + TerminatorKind::SwitchInt { discr: Operand::Copy(opt.to_switch_on), targets }; } for (idx, bb_idx) in storage_deads_to_remove { diff --git a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.32bit.panic-abort.diff b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.32bit.panic-abort.diff index 6baa902b6f4b..2b77aa380a0f 100644 --- a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.32bit.panic-abort.diff +++ b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.32bit.panic-abort.diff @@ -74,7 +74,7 @@ _23 = copy (((*_1).0: std::fmt::FormattingOptions).0: u32); _22 = BitAnd(move _23, const core::fmt::flags::PRECISION_FLAG); StorageDead(_23); - switchInt(move _22) -> [0: bb10, otherwise: bb11]; + switchInt(copy _22) -> [0: bb10, otherwise: bb11]; } bb4: { diff --git a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.32bit.panic-unwind.diff b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.32bit.panic-unwind.diff index 36540e038654..ba6d2f3e155c 100644 --- a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.32bit.panic-unwind.diff +++ b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.32bit.panic-unwind.diff @@ -74,7 +74,7 @@ _23 = copy (((*_1).0: std::fmt::FormattingOptions).0: u32); _22 = BitAnd(move _23, const core::fmt::flags::PRECISION_FLAG); StorageDead(_23); - switchInt(move _22) -> [0: bb10, otherwise: bb11]; + switchInt(copy _22) -> [0: bb10, otherwise: bb11]; } bb4: { diff --git a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.64bit.panic-abort.diff b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.64bit.panic-abort.diff index 41c350f3eaeb..bf6d9d864d57 100644 --- a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.64bit.panic-abort.diff +++ b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.64bit.panic-abort.diff @@ -74,7 +74,7 @@ _23 = copy (((*_1).0: std::fmt::FormattingOptions).0: u32); _22 = BitAnd(move _23, const core::fmt::flags::PRECISION_FLAG); StorageDead(_23); - switchInt(move _22) -> [0: bb10, otherwise: bb11]; + switchInt(copy _22) -> [0: bb10, otherwise: bb11]; } bb4: { diff --git a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.64bit.panic-unwind.diff b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.64bit.panic-unwind.diff index b839bf81eaf4..01c87fd5317a 100644 --- a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.64bit.panic-unwind.diff +++ b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.64bit.panic-unwind.diff @@ -74,7 +74,7 @@ _23 = copy (((*_1).0: std::fmt::FormattingOptions).0: u32); _22 = BitAnd(move _23, const core::fmt::flags::PRECISION_FLAG); StorageDead(_23); - switchInt(move _22) -> [0: bb10, otherwise: bb11]; + switchInt(copy _22) -> [0: bb10, otherwise: bb11]; } bb4: { diff --git a/tests/mir-opt/if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff index 29d57722355b..f5f2e0317ead 100644 --- a/tests/mir-opt/if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff @@ -21,7 +21,7 @@ _2 = Eq(copy _1, const 17_i8); StorageDead(_3); - switchInt(copy _2) -> [0: bb2, otherwise: bb1]; -+ switchInt(move _1) -> [17: bb1, otherwise: bb2]; ++ switchInt(copy _1) -> [17: bb1, otherwise: bb2]; } bb1: { diff --git a/tests/mir-opt/if_condition_int.opt_char.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.opt_char.SimplifyComparisonIntegral.diff index 37b41c2aa5ab..22f6443a6c0f 100644 --- a/tests/mir-opt/if_condition_int.opt_char.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.opt_char.SimplifyComparisonIntegral.diff @@ -14,7 +14,7 @@ - _2 = Eq(copy _1, const 'x'); - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + nop; -+ switchInt(move _1) -> [120: bb1, otherwise: bb2]; ++ switchInt(copy _1) -> [120: bb1, otherwise: bb2]; } bb1: { diff --git a/tests/mir-opt/if_condition_int.opt_i8.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.opt_i8.SimplifyComparisonIntegral.diff index c92a606090e4..d5c48fd04ca6 100644 --- a/tests/mir-opt/if_condition_int.opt_i8.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.opt_i8.SimplifyComparisonIntegral.diff @@ -14,7 +14,7 @@ - _2 = Eq(copy _1, const 42_i8); - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + nop; -+ switchInt(move _1) -> [42: bb1, otherwise: bb2]; ++ switchInt(copy _1) -> [42: bb1, otherwise: bb2]; } bb1: { diff --git a/tests/mir-opt/if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff index a73670323b1f..84d62b813607 100644 --- a/tests/mir-opt/if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff @@ -16,7 +16,7 @@ - _2 = Eq(copy _1, const 42_u32); - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + nop; -+ switchInt(move _1) -> [42: bb1, otherwise: bb2]; ++ switchInt(copy _1) -> [42: bb1, otherwise: bb2]; } bb1: { @@ -33,7 +33,7 @@ - _4 = Ne(copy _1, const 21_u32); - switchInt(move _4) -> [0: bb4, otherwise: bb3]; + nop; -+ switchInt(move _1) -> [21: bb4, otherwise: bb3]; ++ switchInt(copy _1) -> [21: bb4, otherwise: bb3]; } bb3: { diff --git a/tests/mir-opt/if_condition_int.opt_negative.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.opt_negative.SimplifyComparisonIntegral.diff index 36145ab02bea..a1cbaff796b3 100644 --- a/tests/mir-opt/if_condition_int.opt_negative.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.opt_negative.SimplifyComparisonIntegral.diff @@ -14,7 +14,7 @@ - _2 = Eq(copy _1, const -42_i32); - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + nop; -+ switchInt(move _1) -> [4294967254: bb1, otherwise: bb2]; ++ switchInt(copy _1) -> [4294967254: bb1, otherwise: bb2]; } bb1: { diff --git a/tests/mir-opt/if_condition_int.opt_u32.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.opt_u32.SimplifyComparisonIntegral.diff index 2cc4a8b76973..8101de8cbf38 100644 --- a/tests/mir-opt/if_condition_int.opt_u32.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.opt_u32.SimplifyComparisonIntegral.diff @@ -14,7 +14,7 @@ - _2 = Eq(copy _1, const 42_u32); - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + nop; -+ switchInt(move _1) -> [42: bb1, otherwise: bb2]; ++ switchInt(copy _1) -> [42: bb1, otherwise: bb2]; } bb1: { diff --git a/tests/mir-opt/if_condition_int.rs b/tests/mir-opt/if_condition_int.rs index b49f8768253a..5e0961da3cce 100644 --- a/tests/mir-opt/if_condition_int.rs +++ b/tests/mir-opt/if_condition_int.rs @@ -10,8 +10,7 @@ use core::intrinsics::mir::*; // EMIT_MIR if_condition_int.opt_u32.SimplifyComparisonIntegral.diff fn opt_u32(x: u32) -> u32 { // CHECK-LABEL: fn opt_u32( - // FIXME: This should be copy. - // CHECK: switchInt(move _1) -> [42: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: switchInt(copy _1) -> [42: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; // CHECK: [[BB1]]: // CHECK: _0 = const 0_u32; // CHECK: [[BB2]]: @@ -34,7 +33,7 @@ fn dont_opt_bool(x: bool) -> u32 { // EMIT_MIR if_condition_int.opt_char.SimplifyComparisonIntegral.diff fn opt_char(x: char) -> u32 { // CHECK-LABEL: fn opt_char( - // CHECK: switchInt(move _1) -> [120: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: switchInt(copy _1) -> [120: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; // CHECK: [[BB1]]: // CHECK: _0 = const 0_u32; // CHECK: [[BB2]]: @@ -45,7 +44,7 @@ fn opt_char(x: char) -> u32 { // EMIT_MIR if_condition_int.opt_i8.SimplifyComparisonIntegral.diff fn opt_i8(x: i8) -> u32 { // CHECK-LABEL: fn opt_i8( - // CHECK: switchInt(move _1) -> [42: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: switchInt(copy _1) -> [42: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; // CHECK: [[BB1]]: // CHECK: _0 = const 0_u32; // CHECK: [[BB2]]: @@ -56,7 +55,7 @@ fn opt_i8(x: i8) -> u32 { // EMIT_MIR if_condition_int.opt_negative.SimplifyComparisonIntegral.diff fn opt_negative(x: i32) -> u32 { // CHECK-LABEL: fn opt_negative( - // CHECK: switchInt(move _1) -> [4294967254: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: switchInt(copy _1) -> [4294967254: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; // CHECK: [[BB1]]: // CHECK: _0 = const 0_u32; // CHECK: [[BB2]]: @@ -67,11 +66,11 @@ fn opt_negative(x: i32) -> u32 { // EMIT_MIR if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff fn opt_multiple_ifs(x: u32) -> u32 { // CHECK-LABEL: fn opt_multiple_ifs( - // CHECK: switchInt(move _1) -> [42: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: switchInt(copy _1) -> [42: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; // CHECK: [[BB1]]: // CHECK: _0 = const 0_u32; // CHECK: [[BB2]]: - // CHECK: switchInt(move _1) -> [21: [[BB4:bb.*]], otherwise: [[BB3:bb.*]]]; + // CHECK: switchInt(copy _1) -> [21: [[BB4:bb.*]], otherwise: [[BB3:bb.*]]]; // CHECK: [[BB3]]: // CHECK: _0 = const 1_u32; // CHECK: [[BB4]]: @@ -90,7 +89,7 @@ fn opt_multiple_ifs(x: u32) -> u32 { fn dont_remove_comparison(a: i8) -> i32 { // CHECK-LABEL: fn dont_remove_comparison( // CHECK: [[b:_.*]] = Eq(copy _1, const 17_i8); - // CHECK: switchInt(move _1) -> [17: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: switchInt(copy _1) -> [17: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; // CHECK: [[BB1]]: // CHECK: [[cast_1:_.*]] = copy [[b]] as i32 (IntToInt); // CHECK: _0 = Add(const 100_i32, move [[cast_1]]); diff --git a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-abort.mir b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-abort.mir index b5c23822162c..f3c83f62edf6 100644 --- a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-abort.mir +++ b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-abort.mir @@ -28,7 +28,7 @@ fn num_to_digit(_1: char) -> u32 { StorageLive(_3); _3 = discriminant(_2); StorageDead(_2); - switchInt(move _3) -> [1: bb2, otherwise: bb7]; + switchInt(copy _3) -> [1: bb2, otherwise: bb7]; } bb2: { diff --git a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-unwind.mir b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-unwind.mir index f22b8835735d..0e55df24cc57 100644 --- a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-unwind.mir +++ b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-unwind.mir @@ -28,7 +28,7 @@ fn num_to_digit(_1: char) -> u32 { StorageLive(_3); _3 = discriminant(_2); StorageDead(_2); - switchInt(move _3) -> [1: bb2, otherwise: bb7]; + switchInt(copy _3) -> [1: bb2, otherwise: bb7]; } bb2: { diff --git a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.64bit.panic-abort.mir b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.64bit.panic-abort.mir index b5c23822162c..f3c83f62edf6 100644 --- a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.64bit.panic-abort.mir +++ b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.64bit.panic-abort.mir @@ -28,7 +28,7 @@ fn num_to_digit(_1: char) -> u32 { StorageLive(_3); _3 = discriminant(_2); StorageDead(_2); - switchInt(move _3) -> [1: bb2, otherwise: bb7]; + switchInt(copy _3) -> [1: bb2, otherwise: bb7]; } bb2: { diff --git a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.64bit.panic-unwind.mir b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.64bit.panic-unwind.mir index f22b8835735d..0e55df24cc57 100644 --- a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.64bit.panic-unwind.mir +++ b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.64bit.panic-unwind.mir @@ -28,7 +28,7 @@ fn num_to_digit(_1: char) -> u32 { StorageLive(_3); _3 = discriminant(_2); StorageDead(_2); - switchInt(move _3) -> [1: bb2, otherwise: bb7]; + switchInt(copy _3) -> [1: bb2, otherwise: bb7]; } bb2: {