Skip to content

Commit 5638bd3

Browse files
committed
mir-opt: run MatchBranchSimplification after GVN
1 parent 796e760 commit 5638bd3

11 files changed

+67
-76
lines changed

compiler/rustc_mir_transform/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
697697
// Now, we need to shrink the generated MIR.
698698
&ref_prop::ReferencePropagation,
699699
&sroa::ScalarReplacementOfAggregates,
700-
&match_branches::MatchBranchSimplification,
701-
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
702700
&multiple_return_terminators::MultipleReturnTerminators,
703701
// After simplifycfg, it allows us to discover new opportunities for peephole
704702
// optimizations.
@@ -707,6 +705,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
707705
&dead_store_elimination::DeadStoreElimination::Initial,
708706
&gvn::GVN,
709707
&simplify::SimplifyLocals::AfterGVN,
708+
&match_branches::MatchBranchSimplification,
710709
&dataflow_const_prop::DataflowConstProp,
711710
&single_use_consts::SingleUseConsts,
712711
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),

tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.Inline.panic-abort.diff

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
let mut _3: u16;
99
let mut _4: u32;
1010
+ scope 1 (inlined core::num::<impl u16>::unchecked_shl) {
11-
+ let mut _5: bool;
12-
+ let _6: ();
11+
+ let _5: ();
1312
+ scope 2 (inlined core::ub_checks::check_language_ub) {
13+
+ let mut _6: bool;
1414
+ scope 3 (inlined core::ub_checks::check_language_ub::runtime) {
1515
+ }
1616
+ }
@@ -22,20 +22,20 @@
2222
StorageLive(_4);
2323
_4 = copy _2;
2424
- _0 = core::num::<impl u16>::unchecked_shl(move _3, move _4) -> [return: bb1, unwind unreachable];
25-
+ StorageLive(_6);
2625
+ StorageLive(_5);
27-
+ _5 = UbChecks();
28-
+ switchInt(move _5) -> [0: bb2, otherwise: bb1];
26+
+ StorageLive(_6);
27+
+ _6 = UbChecks();
28+
+ switchInt(copy _6) -> [0: bb2, otherwise: bb1];
2929
}
3030

3131
bb1: {
32-
+ _6 = core::num::<impl u16>::unchecked_shl::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
32+
+ _5 = core::num::<impl u16>::unchecked_shl::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
3333
+ }
3434
+
3535
+ bb2: {
36-
+ StorageDead(_5);
3736
+ _0 = ShlUnchecked(copy _3, copy _4);
3837
+ StorageDead(_6);
38+
+ StorageDead(_5);
3939
StorageDead(_4);
4040
StorageDead(_3);
4141
return;

tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.Inline.panic-unwind.diff

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
let mut _3: u16;
99
let mut _4: u32;
1010
+ scope 1 (inlined core::num::<impl u16>::unchecked_shl) {
11-
+ let mut _5: bool;
12-
+ let _6: ();
11+
+ let _5: ();
1312
+ scope 2 (inlined core::ub_checks::check_language_ub) {
13+
+ let mut _6: bool;
1414
+ scope 3 (inlined core::ub_checks::check_language_ub::runtime) {
1515
+ }
1616
+ }
@@ -22,20 +22,20 @@
2222
StorageLive(_4);
2323
_4 = copy _2;
2424
- _0 = core::num::<impl u16>::unchecked_shl(move _3, move _4) -> [return: bb1, unwind continue];
25-
+ StorageLive(_6);
2625
+ StorageLive(_5);
27-
+ _5 = UbChecks();
28-
+ switchInt(move _5) -> [0: bb2, otherwise: bb1];
26+
+ StorageLive(_6);
27+
+ _6 = UbChecks();
28+
+ switchInt(copy _6) -> [0: bb2, otherwise: bb1];
2929
}
3030

3131
bb1: {
32-
+ _6 = core::num::<impl u16>::unchecked_shl::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
32+
+ _5 = core::num::<impl u16>::unchecked_shl::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
3333
+ }
3434
+
3535
+ bb2: {
36-
+ StorageDead(_5);
3736
+ _0 = ShlUnchecked(copy _3, copy _4);
3837
+ StorageDead(_6);
38+
+ StorageDead(_5);
3939
StorageDead(_4);
4040
StorageDead(_3);
4141
return;

tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_bigger.Inline.panic-abort.diff

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
let mut _3: i64;
99
let mut _4: u32;
1010
+ scope 1 (inlined core::num::<impl i64>::unchecked_shr) {
11-
+ let mut _5: bool;
12-
+ let _6: ();
11+
+ let _5: ();
1312
+ scope 2 (inlined core::ub_checks::check_language_ub) {
13+
+ let mut _6: bool;
1414
+ scope 3 (inlined core::ub_checks::check_language_ub::runtime) {
1515
+ }
1616
+ }
@@ -22,20 +22,20 @@
2222
StorageLive(_4);
2323
_4 = copy _2;
2424
- _0 = core::num::<impl i64>::unchecked_shr(move _3, move _4) -> [return: bb1, unwind unreachable];
25-
+ StorageLive(_6);
2625
+ StorageLive(_5);
27-
+ _5 = UbChecks();
28-
+ switchInt(move _5) -> [0: bb2, otherwise: bb1];
26+
+ StorageLive(_6);
27+
+ _6 = UbChecks();
28+
+ switchInt(copy _6) -> [0: bb2, otherwise: bb1];
2929
}
3030

3131
bb1: {
32-
+ _6 = core::num::<impl i64>::unchecked_shr::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
32+
+ _5 = core::num::<impl i64>::unchecked_shr::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
3333
+ }
3434
+
3535
+ bb2: {
36-
+ StorageDead(_5);
3736
+ _0 = ShrUnchecked(copy _3, copy _4);
3837
+ StorageDead(_6);
38+
+ StorageDead(_5);
3939
StorageDead(_4);
4040
StorageDead(_3);
4141
return;

tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_bigger.Inline.panic-unwind.diff

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
let mut _3: i64;
99
let mut _4: u32;
1010
+ scope 1 (inlined core::num::<impl i64>::unchecked_shr) {
11-
+ let mut _5: bool;
12-
+ let _6: ();
11+
+ let _5: ();
1312
+ scope 2 (inlined core::ub_checks::check_language_ub) {
13+
+ let mut _6: bool;
1414
+ scope 3 (inlined core::ub_checks::check_language_ub::runtime) {
1515
+ }
1616
+ }
@@ -22,20 +22,20 @@
2222
StorageLive(_4);
2323
_4 = copy _2;
2424
- _0 = core::num::<impl i64>::unchecked_shr(move _3, move _4) -> [return: bb1, unwind continue];
25-
+ StorageLive(_6);
2625
+ StorageLive(_5);
27-
+ _5 = UbChecks();
28-
+ switchInt(move _5) -> [0: bb2, otherwise: bb1];
26+
+ StorageLive(_6);
27+
+ _6 = UbChecks();
28+
+ switchInt(copy _6) -> [0: bb2, otherwise: bb1];
2929
}
3030

3131
bb1: {
32-
+ _6 = core::num::<impl i64>::unchecked_shr::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
32+
+ _5 = core::num::<impl i64>::unchecked_shr::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
3333
+ }
3434
+
3535
+ bb2: {
36-
+ StorageDead(_5);
3736
+ _0 = ShrUnchecked(copy _3, copy _4);
3837
+ StorageDead(_6);
38+
+ StorageDead(_5);
3939
StorageDead(_4);
4040
StorageDead(_3);
4141
return;

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-abort.diff

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
+ scope 2 {
1111
+ }
1212
+ scope 3 (inlined unreachable_unchecked) {
13-
+ let mut _4: bool;
14-
+ let _5: ();
13+
+ let _4: ();
1514
+ scope 4 (inlined core::ub_checks::check_language_ub) {
15+
+ let mut _5: bool;
1616
+ scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
1717
+ }
1818
+ }
@@ -24,7 +24,7 @@
2424
_2 = move _1;
2525
- _0 = Option::<T>::unwrap_unchecked(move _2) -> [return: bb1, unwind unreachable];
2626
+ StorageLive(_3);
27-
+ StorageLive(_5);
27+
+ StorageLive(_4);
2828
+ _3 = discriminant(_2);
2929
+ switchInt(move _3) -> [0: bb2, 1: bb3, otherwise: bb1];
3030
}
@@ -34,15 +34,15 @@
3434
+ }
3535
+
3636
+ bb2: {
37-
+ StorageLive(_4);
38-
+ _4 = UbChecks();
39-
+ assume(copy _4);
40-
+ _5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
37+
+ StorageLive(_5);
38+
+ _5 = UbChecks();
39+
+ assume(copy _5);
40+
+ _4 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
4141
+ }
4242
+
4343
+ bb3: {
4444
+ _0 = move ((_2 as Some).0: T);
45-
+ StorageDead(_5);
45+
+ StorageDead(_4);
4646
+ StorageDead(_3);
4747
StorageDead(_2);
4848
return;

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
+ scope 2 {
1111
+ }
1212
+ scope 3 (inlined unreachable_unchecked) {
13-
+ let mut _4: bool;
14-
+ let _5: ();
13+
+ let _4: ();
1514
+ scope 4 (inlined core::ub_checks::check_language_ub) {
15+
+ let mut _5: bool;
1616
+ scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
1717
+ }
1818
+ }
@@ -24,7 +24,7 @@
2424
_2 = move _1;
2525
- _0 = Option::<T>::unwrap_unchecked(move _2) -> [return: bb1, unwind: bb2];
2626
+ StorageLive(_3);
27-
+ StorageLive(_5);
27+
+ StorageLive(_4);
2828
+ _3 = discriminant(_2);
2929
+ switchInt(move _3) -> [0: bb2, 1: bb3, otherwise: bb1];
3030
}
@@ -38,15 +38,15 @@
3838
- bb2 (cleanup): {
3939
- resume;
4040
+ bb2: {
41-
+ StorageLive(_4);
42-
+ _4 = UbChecks();
43-
+ assume(copy _4);
44-
+ _5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
41+
+ StorageLive(_5);
42+
+ _5 = UbChecks();
43+
+ assume(copy _5);
44+
+ _4 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
4545
+ }
4646
+
4747
+ bb3: {
4848
+ _0 = move ((_2 as Some).0: T);
49-
+ StorageDead(_5);
49+
+ StorageDead(_4);
5050
+ StorageDead(_3);
5151
+ StorageDead(_2);
5252
+ return;

tests/mir-opt/instsimplify/ub_check.unwrap_unchecked.InstSimplify-after-simplifycfg.diff

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
scope 2 {
1111
}
1212
scope 3 (inlined unreachable_unchecked) {
13-
let mut _4: bool;
14-
let _5: ();
13+
let _4: ();
1514
scope 4 (inlined core::ub_checks::check_language_ub) {
15+
let mut _5: bool;
1616
scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
1717
}
1818
}
@@ -23,7 +23,7 @@
2323
StorageLive(_2);
2424
_2 = copy _1;
2525
StorageLive(_3);
26-
StorageLive(_5);
26+
StorageLive(_4);
2727
_3 = discriminant(_2);
2828
switchInt(move _3) -> [0: bb2, 1: bb3, otherwise: bb1];
2929
}
@@ -33,16 +33,16 @@
3333
}
3434

3535
bb2: {
36-
StorageLive(_4);
37-
- _4 = UbChecks();
38-
+ _4 = const false;
39-
assume(copy _4);
40-
_5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
36+
StorageLive(_5);
37+
- _5 = UbChecks();
38+
+ _5 = const false;
39+
assume(copy _5);
40+
_4 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
4141
}
4242

4343
bb3: {
4444
_0 = move ((_2 as Some).0: i32);
45-
StorageDead(_5);
45+
StorageDead(_4);
4646
StorageDead(_3);
4747
StorageDead(_2);
4848
return;

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

+7-17
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@
55
debug bytes => _1;
66
let mut _0: std::option::Option<[u8; 4]>;
77
let _2: [u32; 4];
8-
let mut _3: [u8; 16];
9-
let mut _5: [u8; 4];
10-
let mut _6: u32;
8+
let mut _4: [u8; 4];
119
scope 1 {
1210
debug dwords => _2;
1311
scope 2 {
14-
debug ip => _4;
15-
let _4: u32;
12+
debug ip => _3;
13+
let _3: u32;
1614
}
1715
}
1816

1917
bb0: {
2018
StorageLive(_2);
21-
StorageLive(_3);
22-
_3 = copy _1;
23-
_2 = move _3 as [u32; 4] (Transmute);
24-
StorageDead(_3);
19+
_2 = copy _1 as [u32; 4] (Transmute);
2520
switchInt(copy _2[0 of 4]) -> [0: bb1, otherwise: bb4];
2621
}
2722

@@ -34,15 +29,10 @@
3429
}
3530

3631
bb3: {
32+
_3 = copy _2[3 of 4];
3733
StorageLive(_4);
38-
_4 = copy _2[3 of 4];
39-
StorageLive(_5);
40-
StorageLive(_6);
41-
_6 = copy _4;
42-
_5 = move _6 as [u8; 4] (Transmute);
43-
StorageDead(_6);
44-
_0 = Option::<[u8; 4]>::Some(move _5);
45-
StorageDead(_5);
34+
_4 = copy _3 as [u8; 4] (Transmute);
35+
_0 = Option::<[u8; 4]>::Some(move _4);
4636
StorageDead(_4);
4737
goto -> bb5;
4838
}

tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-abort.diff

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
let mut _3: std::option::Option<T>;
99
let mut _4: isize;
1010
let mut _5: isize;
11+
- let mut _7: bool;
12+
- let mut _8: u8;
13+
- let mut _9: bool;
1114
scope 1 {
1215
debug a => _6;
1316
let _6: u8;
@@ -32,9 +35,7 @@
3235
}
3336

3437
bb2: {
35-
StorageLive(_6);
3638
_6 = copy (((_1.0: std::option::Option<u8>) as Some).0: u8);
37-
StorageDead(_6);
3839
goto -> bb3;
3940
}
4041

tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
let mut _3: std::option::Option<T>;
99
let mut _4: isize;
1010
let mut _5: isize;
11+
- let mut _7: bool;
12+
- let mut _8: u8;
13+
- let mut _9: bool;
1114
scope 1 {
1215
debug a => _6;
1316
let _6: u8;
@@ -32,9 +35,7 @@
3235
}
3336

3437
bb2: {
35-
StorageLive(_6);
3638
_6 = copy (((_1.0: std::option::Option<u8>) as Some).0: u8);
37-
StorageDead(_6);
3839
goto -> bb3;
3940
}
4041

0 commit comments

Comments
 (0)