From 9ad64b98bb6b89813ec1a3959da033548cb675d3 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 24 Sep 2023 08:27:42 +0000 Subject: [PATCH] Only emit `!=` assumptions if the otherwise target is reachable. --- .../rustc_mir_transform/src/unreachable_prop.rs | 13 ++++++------- ...ecked.unwrap_unchecked.Inline.panic-abort.diff | 7 +------ ...cked.unwrap_unchecked.Inline.panic-unwind.diff | 7 +------ ...rap_unchecked.PreCodegen.after.panic-abort.mir | 15 +++++---------- ...ap_unchecked.PreCodegen.after.panic-unwind.mir | 15 +++++---------- ...te_switch_targets.ub_if_b.PreCodegen.after.mir | 5 +---- ...Cfg-after-uninhabited-enum-branching.after.mir | 8 +------- ...Cfg-after-uninhabited-enum-branching.after.mir | 12 ------------ ...e.main.UnreachablePropagation.panic-abort.diff | 5 ++--- ....main.UnreachablePropagation.panic-unwind.diff | 5 ++--- 10 files changed, 24 insertions(+), 68 deletions(-) diff --git a/compiler/rustc_mir_transform/src/unreachable_prop.rs b/compiler/rustc_mir_transform/src/unreachable_prop.rs index d25c6d471cd2a..919e8d6a2340f 100644 --- a/compiler/rustc_mir_transform/src/unreachable_prop.rs +++ b/compiler/rustc_mir_transform/src/unreachable_prop.rs @@ -116,22 +116,21 @@ fn remove_successors_from_switch<'tcx>( patch.add_statement(location, StatementKind::Intrinsic(Box::new(assume))); }; + let otherwise = targets.otherwise(); + let otherwise_unreachable = is_unreachable(otherwise); + let reachable_iter = targets.iter().filter(|&(value, bb)| { let is_unreachable = is_unreachable(bb); - if is_unreachable { - // We remove this target from the switch, so record the inequality using `Assume`. + // We remove this target from the switch, so record the inequality using `Assume`. + if is_unreachable && !otherwise_unreachable { add_assumption(BinOp::Ne, value); - false - } else { - true } + !is_unreachable }); - let otherwise = targets.otherwise(); let new_targets = SwitchTargets::new(reachable_iter, otherwise); let num_targets = new_targets.all_targets().len(); - let otherwise_unreachable = is_unreachable(otherwise); let fully_unreachable = num_targets == 1 && otherwise_unreachable; let terminator = match (num_targets, otherwise_unreachable) { diff --git a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-abort.diff b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-abort.diff index 4ca7a2c4294ff..2a36ad9230e4b 100644 --- a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-abort.diff +++ b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-abort.diff @@ -10,7 +10,6 @@ + let mut _3: &std::option::Option; + let mut _4: isize; + let mut _5: bool; -+ let mut _6: bool; + scope 2 { + debug val => _0; + } @@ -37,14 +36,10 @@ + StorageLive(_3); + StorageLive(_4); + StorageLive(_5); -+ StorageLive(_6); + _4 = discriminant(_2); -+ _5 = Ne(_4, const 0_isize); ++ _5 = Eq(_4, const 1_isize); + assume(move _5); -+ _6 = Eq(_4, const 1_isize); -+ assume(move _6); + _0 = move ((_2 as Some).0: T); -+ StorageDead(_6); + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); diff --git a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff index caabb7ea46358..14c8c671d3fe2 100644 --- a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff +++ b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff @@ -10,7 +10,6 @@ + let mut _3: &std::option::Option; + let mut _4: isize; + let mut _5: bool; -+ let mut _6: bool; + scope 2 { + debug val => _0; + } @@ -37,14 +36,10 @@ + StorageLive(_3); + StorageLive(_4); + StorageLive(_5); -+ StorageLive(_6); + _4 = discriminant(_2); -+ _5 = Ne(_4, const 0_isize); ++ _5 = Eq(_4, const 1_isize); + assume(move _5); -+ _6 = Eq(_4, const 1_isize); -+ assume(move _6); + _0 = move ((_2 as Some).0: T); -+ StorageDead(_6); + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); diff --git a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-abort.mir b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-abort.mir index 50ce7ad64bda8..aeb93bd334fe6 100644 --- a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-abort.mir +++ b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-abort.mir @@ -7,8 +7,7 @@ fn unwrap_unchecked(_1: Option) -> T { debug self => _1; let mut _2: isize; let mut _3: bool; - let mut _4: bool; - let mut _5: &std::option::Option; + let mut _4: &std::option::Option; scope 2 { debug val => _0; } @@ -21,25 +20,21 @@ fn unwrap_unchecked(_1: Option) -> T { } } scope 4 (inlined Option::::is_some) { - debug self => _5; + debug self => _4; } } bb0: { - StorageLive(_5); - StorageLive(_3); StorageLive(_4); StorageLive(_2); + StorageLive(_3); _2 = discriminant(_1); - _3 = Ne(_2, const 0_isize); + _3 = Eq(_2, const 1_isize); assume(move _3); - _4 = Eq(_2, const 1_isize); - assume(move _4); _0 = move ((_1 as Some).0: T); + StorageDead(_3); StorageDead(_2); StorageDead(_4); - StorageDead(_3); - StorageDead(_5); return; } } diff --git a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir index 50ce7ad64bda8..aeb93bd334fe6 100644 --- a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir @@ -7,8 +7,7 @@ fn unwrap_unchecked(_1: Option) -> T { debug self => _1; let mut _2: isize; let mut _3: bool; - let mut _4: bool; - let mut _5: &std::option::Option; + let mut _4: &std::option::Option; scope 2 { debug val => _0; } @@ -21,25 +20,21 @@ fn unwrap_unchecked(_1: Option) -> T { } } scope 4 (inlined Option::::is_some) { - debug self => _5; + debug self => _4; } } bb0: { - StorageLive(_5); - StorageLive(_3); StorageLive(_4); StorageLive(_2); + StorageLive(_3); _2 = discriminant(_1); - _3 = Ne(_2, const 0_isize); + _3 = Eq(_2, const 1_isize); assume(move _3); - _4 = Eq(_2, const 1_isize); - assume(move _4); _0 = move ((_1 as Some).0: T); + StorageDead(_3); StorageDead(_2); StorageDead(_4); - StorageDead(_3); - StorageDead(_5); return; } } diff --git a/tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir index fe7beadc8188c..0114309dbb58e 100644 --- a/tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir @@ -5,7 +5,6 @@ fn ub_if_b(_1: Thing) -> Thing { let mut _0: Thing; let mut _2: isize; let mut _3: bool; - let mut _4: bool; scope 1 (inlined unreachable_unchecked) { scope 2 { scope 3 (inlined unreachable_unchecked::runtime) { @@ -15,10 +14,8 @@ fn ub_if_b(_1: Thing) -> Thing { bb0: { _2 = discriminant(_1); - _3 = Ne(_2, const 1_isize); + _3 = Eq(_2, const 0_isize); assume(move _3); - _4 = Eq(_2, const 0_isize); - assume(move _4); _0 = move _1; return; } diff --git a/tests/mir-opt/uninhabited_enum_branching.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir b/tests/mir-opt/uninhabited_enum_branching.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir index 1df3b74b3e7d9..1ee44e48c8ec8 100644 --- a/tests/mir-opt/uninhabited_enum_branching.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir +++ b/tests/mir-opt/uninhabited_enum_branching.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir @@ -12,20 +12,14 @@ fn main() -> () { let mut _8: isize; let _9: &str; let mut _10: bool; - let mut _11: bool; - let mut _12: bool; bb0: { StorageLive(_1); StorageLive(_2); _2 = Test1::C; _3 = discriminant(_2); - _10 = Ne(_3, const 0_isize); + _10 = Eq(_3, const 2_isize); assume(move _10); - _11 = Ne(_3, const 1_isize); - assume(move _11); - _12 = Eq(_3, const 2_isize); - assume(move _12); StorageLive(_5); _5 = const "C"; _1 = &(*_5); diff --git a/tests/mir-opt/uninhabited_enum_branching2.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir b/tests/mir-opt/uninhabited_enum_branching2.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir index 06375b3ffaebf..9c0c5d18917ae 100644 --- a/tests/mir-opt/uninhabited_enum_branching2.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir +++ b/tests/mir-opt/uninhabited_enum_branching2.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir @@ -15,10 +15,6 @@ fn main() -> () { let _11: &str; let _12: &str; let _13: &str; - let mut _14: bool; - let mut _15: bool; - let mut _16: bool; - let mut _17: bool; scope 1 { debug plop => _1; } @@ -33,10 +29,6 @@ fn main() -> () { StorageLive(_4); _4 = &(_1.1: Test1); _5 = discriminant((*_4)); - _16 = Ne(_5, const 0_isize); - assume(move _16); - _17 = Ne(_5, const 1_isize); - assume(move _17); switchInt(move _5) -> [2: bb3, 3: bb1, otherwise: bb2]; } @@ -65,10 +57,6 @@ fn main() -> () { StorageDead(_3); StorageLive(_9); _10 = discriminant((_1.1: Test1)); - _14 = Ne(_10, const 0_isize); - assume(move _14); - _15 = Ne(_10, const 1_isize); - assume(move _15); switchInt(move _10) -> [2: bb6, 3: bb5, otherwise: bb2]; } diff --git a/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-abort.diff b/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-abort.diff index 6f4faefce145e..a5ffe079b66cb 100644 --- a/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-abort.diff +++ b/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-abort.diff @@ -9,7 +9,6 @@ let mut _6: bool; let mut _7: !; + let mut _8: bool; -+ let mut _9: bool; scope 1 { debug _x => _3; let _3: Empty; @@ -27,8 +26,8 @@ bb1: { _2 = discriminant(_1); - switchInt(move _2) -> [1: bb2, otherwise: bb6]; -+ _9 = Ne(_2, const 1_isize); -+ assume(move _9); ++ _8 = Ne(_2, const 1_isize); ++ assume(move _8); + goto -> bb6; } diff --git a/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-unwind.diff b/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-unwind.diff index 5bacb42bed3f2..3753d643e558d 100644 --- a/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-unwind.diff +++ b/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-unwind.diff @@ -9,7 +9,6 @@ let mut _6: bool; let mut _7: !; + let mut _8: bool; -+ let mut _9: bool; scope 1 { debug _x => _3; let _3: Empty; @@ -27,8 +26,8 @@ bb1: { _2 = discriminant(_1); - switchInt(move _2) -> [1: bb2, otherwise: bb6]; -+ _9 = Ne(_2, const 1_isize); -+ assume(move _9); ++ _8 = Ne(_2, const 1_isize); ++ assume(move _8); + goto -> bb6; }