Skip to content

Commit dabec66

Browse files
committed
mir-opt: Postpone the MatchBranchSimplification to after the final DSE
This may not be the best phase, but we do need a CFG simplification. A potential improvement would be to implement a trivial simplification.
1 parent 1b79b87 commit dabec66

15 files changed

+116
-85
lines changed

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,6 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
703703
&dead_store_elimination::DeadStoreElimination::Initial,
704704
&gvn::GVN,
705705
&simplify::SimplifyLocals::AfterGVN,
706-
&match_branches::MatchBranchSimplification,
707706
&dataflow_const_prop::DataflowConstProp,
708707
&single_use_consts::SingleUseConsts,
709708
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
@@ -718,6 +717,7 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
718717
&strip_debuginfo::StripDebugInfo,
719718
&copy_prop::CopyProp,
720719
&dead_store_elimination::DeadStoreElimination::Final,
720+
&match_branches::MatchBranchSimplification,
721721
&nrvo::RenameReturnPlace,
722722
&simplify::SimplifyLocals::Final,
723723
&multiple_return_terminators::MultipleReturnTerminators,

tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-abort.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ fn num_to_digit(_1: char) -> u32 {
1818
}
1919

2020
bb0: {
21+
StorageLive(_3);
2122
StorageLive(_2);
2223
_2 = char::methods::<impl char>::to_digit(copy _1, const 8_u32) -> [return: bb1, unwind unreachable];
2324
}
2425

2526
bb1: {
26-
StorageLive(_3);
2727
_3 = discriminant(_2);
2828
StorageDead(_2);
2929
switchInt(move _3) -> [1: bb2, otherwise: bb7];

tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-unwind.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ fn num_to_digit(_1: char) -> u32 {
1818
}
1919

2020
bb0: {
21+
StorageLive(_3);
2122
StorageLive(_2);
2223
_2 = char::methods::<impl char>::to_digit(copy _1, const 8_u32) -> [return: bb1, unwind continue];
2324
}
2425

2526
bb1: {
26-
StorageLive(_3);
2727
_3 = discriminant(_2);
2828
StorageDead(_2);
2929
switchInt(move _3) -> [1: bb2, otherwise: bb7];

tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.64bit.panic-abort.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ fn num_to_digit(_1: char) -> u32 {
1818
}
1919

2020
bb0: {
21+
StorageLive(_3);
2122
StorageLive(_2);
2223
_2 = char::methods::<impl char>::to_digit(copy _1, const 8_u32) -> [return: bb1, unwind unreachable];
2324
}
2425

2526
bb1: {
26-
StorageLive(_3);
2727
_3 = discriminant(_2);
2828
StorageDead(_2);
2929
switchInt(move _3) -> [1: bb2, otherwise: bb7];

tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.64bit.panic-unwind.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ fn num_to_digit(_1: char) -> u32 {
1818
}
1919

2020
bb0: {
21+
StorageLive(_3);
2122
StorageLive(_2);
2223
_2 = char::methods::<impl char>::to_digit(copy _1, const 8_u32) -> [return: bb1, unwind continue];
2324
}
2425

2526
bb1: {
26-
StorageLive(_3);
2727
_3 = discriminant(_2);
2828
StorageDead(_2);
2929
switchInt(move _3) -> [1: bb2, otherwise: bb7];

tests/mir-opt/issues/issue_75439.foo.MatchBranchSimplification.diff

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
let mut _0: std::option::Option<[u8; 4]>;
77
let _2: [u32; 4];
88
let mut _4: [u8; 4];
9+
let mut _5: bool;
910
scope 1 {
1011
debug dwords => _2;
1112
scope 2 {
@@ -17,34 +18,35 @@
1718
bb0: {
1819
StorageLive(_2);
1920
_2 = copy _1 as [u32; 4] (Transmute);
20-
switchInt(copy _2[0 of 4]) -> [0: bb1, otherwise: bb4];
21+
_5 = Ne(copy _2[0 of 4], copy _2[1 of 4]);
22+
switchInt(move _5) -> [0: bb5, otherwise: bb3];
2123
}
2224

2325
bb1: {
24-
switchInt(copy _2[1 of 4]) -> [0: bb2, otherwise: bb4];
26+
switchInt(copy _2[2 of 4]) -> [0: bb2, 4294901760: bb2, otherwise: bb3];
2527
}
2628

2729
bb2: {
28-
switchInt(copy _2[2 of 4]) -> [0: bb3, 4294901760: bb3, otherwise: bb4];
29-
}
30-
31-
bb3: {
3230
_3 = copy _2[3 of 4];
3331
StorageLive(_4);
3432
_4 = copy _3 as [u8; 4] (Transmute);
3533
_0 = Option::<[u8; 4]>::Some(move _4);
3634
StorageDead(_4);
37-
goto -> bb5;
35+
goto -> bb4;
3836
}
3937

40-
bb4: {
38+
bb3: {
4139
_0 = Option::<[u8; 4]>::None;
42-
goto -> bb5;
40+
goto -> bb4;
4341
}
4442

45-
bb5: {
43+
bb4: {
4644
StorageDead(_2);
4745
return;
4846
}
47+
48+
bb5: {
49+
switchInt(copy _2[0 of 4]) -> [0: bb1, otherwise: bb3];
50+
}
4951
}
5052

tests/mir-opt/pre-codegen/clone_as_copy.clone_as_copy.PreCodegen.after.mir

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/mir-opt/pre-codegen/clone_as_copy.enum_clone_as_copy.PreCodegen.after.mir

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/mir-opt/pre-codegen/clone_as_copy.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@ enum Enum1 {
2222
B(NestCopy),
2323
}
2424

25-
// EMIT_MIR clone_as_copy.clone_as_copy.PreCodegen.after.mir
26-
fn clone_as_copy(v: &NestCopy) -> NestCopy {
27-
// CHECK-LABEL: fn clone_as_copy(
28-
// CHECK: let [[DEAD_VAR:_.*]]: &AllCopy;
29-
// CHECK: bb0: {
30-
// CHECK-NEXT: DBG: [[DEAD_VAR]] = &((*_1).1: AllCopy)
31-
// CHECK-NEXT: _0 = copy (*_1);
32-
// CHECK-NEXT: return;
33-
v.clone()
34-
}
25+
// EMIT_MIR clone_as_copy.{impl#0}-clone.PreCodegen.after.mir
26+
// CHECK-LABEL: fn <impl at {{.*}}>::clone(_1: &AllCopy) -> AllCopy {
27+
// CHECK: bb0: {
28+
// CHECK-NEXT: _0 = copy (*_1);
29+
// CHECK-NEXT: return;
3530

36-
// EMIT_MIR clone_as_copy.enum_clone_as_copy.PreCodegen.after.mir
37-
fn enum_clone_as_copy(v: &Enum1) -> Enum1 {
38-
// CHECK-LABEL: fn enum_clone_as_copy(
39-
// CHECK: bb0: {
40-
// CHECK-NEXT: _0 = copy (*_1);
41-
// CHECK-NEXT: return;
42-
v.clone()
43-
}
31+
// EMIT_MIR clone_as_copy.{impl#1}-clone.PreCodegen.after.mir
32+
// CHECK-LABEL: fn <impl at {{.*}}>::clone(_1: &NestCopy) -> NestCopy {
33+
// CHECK: scope 1 (inlined <AllCopy as Clone>::clone) {
34+
// CHECK-NEXT: debug self => [[inlined_AllCopy_self:_[0-9]+]];
35+
// CHECK: bb0: {
36+
// CHECK-NEXT: DBG: [[inlined_AllCopy_self]] = &((*_1).1: AllCopy)
37+
// CHECK-NEXT: _0 = copy (*_1);
38+
// CHECK-NEXT: return;
39+
40+
// EMIT_MIR clone_as_copy.{impl#2}-clone.PreCodegen.after.mir
41+
// CHECK-LABEL: fn <impl at {{.*}}>::clone(_1: &Enum1) -> Enum1 {
42+
// CHECK: bb0: {
43+
// CHECK-NEXT: _0 = copy (*_1);
44+
// CHECK-NEXT: return;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// MIR for `<impl at $DIR/clone_as_copy.rs:5:10: 5:15>::clone` after PreCodegen
2+
3+
fn <impl at $DIR/clone_as_copy.rs:5:10: 5:15>::clone(_1: &AllCopy) -> AllCopy {
4+
debug self => _1;
5+
let mut _0: AllCopy;
6+
7+
bb0: {
8+
_0 = copy (*_1);
9+
return;
10+
}
11+
}

0 commit comments

Comments
 (0)