Auto merge of #140115 - dianqk:gvn-matchbr, r= · rust-lang/rust@1aadc7f (original) (raw)

File tree

13 files changed

lines changed

13 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -699,8 +699,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
699 699 // Now, we need to shrink the generated MIR.
700 700 &ref_prop::ReferencePropagation,
701 701 &sroa::ScalarReplacementOfAggregates,
702 -&match_branches::MatchBranchSimplification,
703 -// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
704 702 &multiple_return_terminators::MultipleReturnTerminators,
705 703 // After simplifycfg, it allows us to discover new opportunities for peephole
706 704 // optimizations.
@@ -709,6 +707,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
709 707 &dead_store_elimination::DeadStoreElimination::Initial,
710 708 &gvn::GVN,
711 709 &simplify::SimplifyLocals::AfterGVN,
710 +&match_branches::MatchBranchSimplification,
712 711 &dataflow_const_prop::DataflowConstProp,
713 712 &single_use_consts::SingleUseConsts,
714 713 &o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@
8 8 let mut _3: u16;
9 9 let mut _4: u32;
10 10 + scope 1 (inlined core::num::::unchecked_shl) {
11 -+ let mut _5: bool;
12 -+ let _6: ();
11 ++ let _5: ();
13 12 + scope 2 (inlined core::ub_checks::check_language_ub) {
13 ++ let mut _6: bool;
14 14 + scope 3 (inlined core::ub_checks::check_language_ub::runtime) {
15 15 + }
16 16 + }
@@ -22,20 +22,20 @@
22 22 StorageLive(_4);
23 23 _4 = copy _2;
24 24 - _0 = core::num::::unchecked_shl(move _3, move _4) -> [return: bb1, unwind unreachable];
25 -+ StorageLive(_6);
26 25 + 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];
29 29 }
30 30
31 31 bb1: {
32 -+ _6 = core::num::::unchecked_shl::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
32 ++ _5 = core::num::::unchecked_shl::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
33 33 + }
34 34 +
35 35 + bb2: {
36 -+ StorageDead(_5);
37 36 + _0 = ShlUnchecked(copy _3, copy _4);
38 37 + StorageDead(_6);
38 ++ StorageDead(_5);
39 39 StorageDead(_4);
40 40 StorageDead(_3);
41 41 return;
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@
8 8 let mut _3: u16;
9 9 let mut _4: u32;
10 10 + scope 1 (inlined core::num::::unchecked_shl) {
11 -+ let mut _5: bool;
12 -+ let _6: ();
11 ++ let _5: ();
13 12 + scope 2 (inlined core::ub_checks::check_language_ub) {
13 ++ let mut _6: bool;
14 14 + scope 3 (inlined core::ub_checks::check_language_ub::runtime) {
15 15 + }
16 16 + }
@@ -22,20 +22,20 @@
22 22 StorageLive(_4);
23 23 _4 = copy _2;
24 24 - _0 = core::num::::unchecked_shl(move _3, move _4) -> [return: bb1, unwind continue];
25 -+ StorageLive(_6);
26 25 + 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];
29 29 }
30 30
31 31 bb1: {
32 -+ _6 = core::num::::unchecked_shl::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
32 ++ _5 = core::num::::unchecked_shl::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
33 33 + }
34 34 +
35 35 + bb2: {
36 -+ StorageDead(_5);
37 36 + _0 = ShlUnchecked(copy _3, copy _4);
38 37 + StorageDead(_6);
38 ++ StorageDead(_5);
39 39 StorageDead(_4);
40 40 StorageDead(_3);
41 41 return;
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@
8 8 let mut _3: i64;
9 9 let mut _4: u32;
10 10 + scope 1 (inlined core::num::::unchecked_shr) {
11 -+ let mut _5: bool;
12 -+ let _6: ();
11 ++ let _5: ();
13 12 + scope 2 (inlined core::ub_checks::check_language_ub) {
13 ++ let mut _6: bool;
14 14 + scope 3 (inlined core::ub_checks::check_language_ub::runtime) {
15 15 + }
16 16 + }
@@ -22,20 +22,20 @@
22 22 StorageLive(_4);
23 23 _4 = copy _2;
24 24 - _0 = core::num::::unchecked_shr(move _3, move _4) -> [return: bb1, unwind unreachable];
25 -+ StorageLive(_6);
26 25 + 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];
29 29 }
30 30
31 31 bb1: {
32 -+ _6 = core::num::::unchecked_shr::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
32 ++ _5 = core::num::::unchecked_shr::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
33 33 + }
34 34 +
35 35 + bb2: {
36 -+ StorageDead(_5);
37 36 + _0 = ShrUnchecked(copy _3, copy _4);
38 37 + StorageDead(_6);
38 ++ StorageDead(_5);
39 39 StorageDead(_4);
40 40 StorageDead(_3);
41 41 return;
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@
8 8 let mut _3: i64;
9 9 let mut _4: u32;
10 10 + scope 1 (inlined core::num::::unchecked_shr) {
11 -+ let mut _5: bool;
12 -+ let _6: ();
11 ++ let _5: ();
13 12 + scope 2 (inlined core::ub_checks::check_language_ub) {
13 ++ let mut _6: bool;
14 14 + scope 3 (inlined core::ub_checks::check_language_ub::runtime) {
15 15 + }
16 16 + }
@@ -22,20 +22,20 @@
22 22 StorageLive(_4);
23 23 _4 = copy _2;
24 24 - _0 = core::num::::unchecked_shr(move _3, move _4) -> [return: bb1, unwind continue];
25 -+ StorageLive(_6);
26 25 + 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];
29 29 }
30 30
31 31 bb1: {
32 -+ _6 = core::num::::unchecked_shr::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
32 ++ _5 = core::num::::unchecked_shr::precondition_check(copy _4) -> [return: bb2, unwind unreachable];
33 33 + }
34 34 +
35 35 + bb2: {
36 -+ StorageDead(_5);
37 36 + _0 = ShrUnchecked(copy _3, copy _4);
38 37 + StorageDead(_6);
38 ++ StorageDead(_5);
39 39 StorageDead(_4);
40 40 StorageDead(_3);
41 41 return;
Original file line number Diff line number Diff line change
@@ -10,9 +10,9 @@
10 10 + scope 2 {
11 11 + }
12 12 + scope 3 (inlined unreachable_unchecked) {
13 -+ let mut _4: bool;
14 -+ let _5: ();
13 ++ let _4: ();
15 14 + scope 4 (inlined core::ub_checks::check_language_ub) {
15 ++ let mut _5: bool;
16 16 + scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
17 17 + }
18 18 + }
@@ -24,7 +24,7 @@
24 24 _2 = move _1;
25 25 - _0 = Option::::unwrap_unchecked(move _2) -> [return: bb1, unwind unreachable];
26 26 + StorageLive(_3);
27 -+ StorageLive(_5);
27 ++ StorageLive(_4);
28 28 + _3 = discriminant(_2);
29 29 + switchInt(move _3) -> [0: bb2, 1: bb3, otherwise: bb1];
30 30 }
@@ -34,15 +34,15 @@
34 34 + }
35 35 +
36 36 + 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];
41 41 + }
42 42 +
43 43 + bb3: {
44 44 + _0 = move ((_2 as Some).0: T);
45 -+ StorageDead(_5);
45 ++ StorageDead(_4);
46 46 + StorageDead(_3);
47 47 StorageDead(_2);
48 48 return;
Original file line number Diff line number Diff line change
@@ -10,9 +10,9 @@
10 10 + scope 2 {
11 11 + }
12 12 + scope 3 (inlined unreachable_unchecked) {
13 -+ let mut _4: bool;
14 -+ let _5: ();
13 ++ let _4: ();
15 14 + scope 4 (inlined core::ub_checks::check_language_ub) {
15 ++ let mut _5: bool;
16 16 + scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
17 17 + }
18 18 + }
@@ -24,7 +24,7 @@
24 24 _2 = move _1;
25 25 - _0 = Option::::unwrap_unchecked(move _2) -> [return: bb1, unwind: bb2];
26 26 + StorageLive(_3);
27 -+ StorageLive(_5);
27 ++ StorageLive(_4);
28 28 + _3 = discriminant(_2);
29 29 + switchInt(move _3) -> [0: bb2, 1: bb3, otherwise: bb1];
30 30 }
@@ -38,15 +38,15 @@
38 38 - bb2 (cleanup): {
39 39 - resume;
40 40 + 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];
45 45 + }
46 46 +
47 47 + bb3: {
48 48 + _0 = move ((_2 as Some).0: T);
49 -+ StorageDead(_5);
49 ++ StorageDead(_4);
50 50 + StorageDead(_3);
51 51 + StorageDead(_2);
52 52 + return;
Original file line number Diff line number Diff line change
@@ -10,9 +10,9 @@
10 10 scope 2 {
11 11 }
12 12 scope 3 (inlined unreachable_unchecked) {
13 - let mut _4: bool;
14 - let _5: ();
13 + let _4: ();
15 14 scope 4 (inlined core::ub_checks::check_language_ub) {
15 + let mut _5: bool;
16 16 scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
17 17 }
18 18 }
@@ -23,7 +23,7 @@
23 23 StorageLive(_2);
24 24 _2 = copy _1;
25 25 StorageLive(_3);
26 - StorageLive(_5);
26 + StorageLive(_4);
27 27 _3 = discriminant(_2);
28 28 switchInt(move _3) -> [0: bb2, 1: bb3, otherwise: bb1];
29 29 }
@@ -33,16 +33,16 @@
33 33 }
34 34
35 35 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];
41 41 }
42 42
43 43 bb3: {
44 44 _0 = move ((_2 as Some).0: i32);
45 - StorageDead(_5);
45 + StorageDead(_4);
46 46 StorageDead(_3);
47 47 StorageDead(_2);
48 48 return;
Original file line number Diff line number Diff line change
@@ -5,23 +5,18 @@
5 5 debug bytes => _1;
6 6 let mut _0: std::option::Option<[u8; 4]>;
7 7 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];
11 9 scope 1 {
12 10 debug dwords => _2;
13 11 scope 2 {
14 - debug ip => _4;
15 - let _4: u32;
12 + debug ip => _3;
13 + let _3: u32;
16 14 }
17 15 }
18 16
19 17 bb0: {
20 18 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);
25 20 switchInt(copy _2[0 of 4]) -> [0: bb1, otherwise: bb4];
26 21 }
27 22
@@ -34,15 +29,10 @@
34 29 }
35 30
36 31 bb3: {
32 + _3 = copy _2[3 of 4];
37 33 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);
46 36 StorageDead(_4);
47 37 goto -> bb5;
48 38 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1 +// MIR for `match1` after PreCodegen
2 +
3 +fn match1(_1: bool, _2: i32, _3: i32) -> i32 {
4 +debug c => _1;
5 +debug v1 => _2;
6 +debug v2 => _3;
7 +let mut _0: i32;
8 +
9 +bb0: {
10 +_0 = Sub(copy _2, copy _3);
11 +return;
12 +}
13 +}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1 +#![crate_type = "lib"]
2 +
3 +// EMIT_MIR matchbr.match1.PreCodegen.after.mir
4 +pub fn match1(c: bool, v1: i32, v2: i32) -> i32 {
5 +// CHECK-LABEL: fn match1(
6 +// CHECK: bb0:
7 +// CHECK-NEXT: _0 = Sub
8 +// CHECK-NEXT: return;
9 +if c { v1 - v2 } else { v1 - v2 }
10 +}
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@
8 8 let mut _3: std::option::Option;
9 9 let mut _4: isize;
10 10 let mut _5: isize;
11 +- let mut _7: bool;
12 +- let mut _8: u8;
13 +- let mut _9: bool;
11 14 scope 1 {
12 15 debug a => _6;
13 16 let _6: u8;
@@ -32,9 +35,7 @@
32 35 }
33 36
34 37 bb2: {
35 - StorageLive(_6);
36 38 _6 = copy (((_1.0: std::option::Option) as Some).0: u8);
37 - StorageDead(_6);
38 39 goto -> bb3;
39 40 }
40 41