Skip to content

Commit

Permalink
Make MatchBranchSimplification clean up after itself
Browse files Browse the repository at this point in the history
  • Loading branch information
simonvandel committed Feb 21, 2021
1 parent ccecc4f commit 1e86570
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 247 deletions.
11 changes: 10 additions & 1 deletion compiler/rustc_mir/src/transform/match_branches.rs
Expand Up @@ -2,6 +2,8 @@ use crate::transform::MirPass;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;

use super::simplify::simplify_cfg;

pub struct MatchBranchSimplification;

/// If a source block is found that switches between two blocks that are exactly
Expand Down Expand Up @@ -42,9 +44,11 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
return;
}

let param_env = tcx.param_env(body.source.def_id());
let def_id = body.source.def_id();
let param_env = tcx.param_env(def_id);

let (bbs, local_decls) = body.basic_blocks_and_local_decls_mut();
let mut should_cleanup = false;
'outer: for bb_idx in bbs.indices() {
if !tcx.consider_optimizing(|| format!("MatchBranchSimplification {:?} ", def_id)) {
continue;
Expand Down Expand Up @@ -159,6 +163,11 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
from.statements
.push(Statement { source_info, kind: StatementKind::StorageDead(discr_local) });
from.terminator_mut().kind = first.terminator().kind.clone();
should_cleanup = true;
}

if should_cleanup {
simplify_cfg(body);
}
}
}
Expand Up @@ -3,47 +3,45 @@
fn num_to_digit(_1: char) -> u32 {
debug num => _1; // in scope 0 at $DIR/issue-59352.rs:12:21: 12:24
let mut _0: u32; // return place in scope 0 at $DIR/issue-59352.rs:12:35: 12:38
let mut _2: bool; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
let mut _3: std::option::Option<u32>; // in scope 0 at $DIR/issue-59352.rs:14:26: 14:41
let mut _4: char; // in scope 0 at $DIR/issue-59352.rs:14:26: 14:29
let mut _5: u32; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
let mut _10: isize; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
let mut _2: std::option::Option<u32>; // in scope 0 at $DIR/issue-59352.rs:14:26: 14:41
let mut _3: char; // in scope 0 at $DIR/issue-59352.rs:14:26: 14:29
let mut _4: u32; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
let mut _9: isize; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
scope 1 (inlined char::methods::<impl char>::is_digit) { // at $DIR/issue-59352.rs:14:8: 14:23
debug self => _8; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
debug radix => _5; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
let mut _6: &std::option::Option<u32>; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
let _7: std::option::Option<u32>; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
let mut _8: char; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
debug self => _7; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
debug radix => _4; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
let mut _5: &std::option::Option<u32>; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
let _6: std::option::Option<u32>; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
let mut _7: char; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
scope 2 (inlined Option::<u32>::is_some) { // at $DIR/issue-59352.rs:14:8: 14:23
debug self => _6; // in scope 2 at $DIR/issue-59352.rs:14:8: 14:23
debug self => _5; // in scope 2 at $DIR/issue-59352.rs:14:8: 14:23
}
}
scope 3 (inlined #[track_caller] Option::<u32>::unwrap) { // at $DIR/issue-59352.rs:14:26: 14:50
debug self => _3; // in scope 3 at $DIR/issue-59352.rs:14:26: 14:50
let mut _9: isize; // in scope 3 at $DIR/issue-59352.rs:14:26: 14:50
debug self => _2; // in scope 3 at $DIR/issue-59352.rs:14:26: 14:50
let mut _8: isize; // in scope 3 at $DIR/issue-59352.rs:14:26: 14:50
scope 4 {
debug val => _0; // in scope 4 at $DIR/issue-59352.rs:14:26: 14:50
}
}

bb0: {
StorageLive(_2); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
_8 = _1; // scope 0 at $DIR/issue-59352.rs:14:8: 14:11
StorageLive(_5); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
_5 = const 8_u32; // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
_7 = _1; // scope 0 at $DIR/issue-59352.rs:14:8: 14:11
StorageLive(_4); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
_4 = const 8_u32; // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
StorageLive(_5); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
StorageLive(_6); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
StorageLive(_7); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
_7 = char::methods::<impl char>::to_digit(move _8, const 8_u32) -> bb5; // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
_6 = char::methods::<impl char>::to_digit(move _7, const 8_u32) -> bb5; // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
// mir::Constant
// + span: $DIR/issue-59352.rs:14:8: 14:23
// + literal: Const { ty: fn(char, u32) -> std::option::Option<u32> {std::char::methods::<impl char>::to_digit}, val: Value(Scalar(<ZST>)) }
}

bb1: {
StorageLive(_3); // scope 0 at $DIR/issue-59352.rs:14:26: 14:41
StorageLive(_4); // scope 0 at $DIR/issue-59352.rs:14:26: 14:29
_4 = _1; // scope 0 at $DIR/issue-59352.rs:14:26: 14:29
_3 = char::methods::<impl char>::to_digit(move _4, const 8_u32) -> bb3; // scope 0 at $DIR/issue-59352.rs:14:26: 14:41
StorageLive(_2); // scope 0 at $DIR/issue-59352.rs:14:26: 14:41
StorageLive(_3); // scope 0 at $DIR/issue-59352.rs:14:26: 14:29
_3 = _1; // scope 0 at $DIR/issue-59352.rs:14:26: 14:29
_2 = char::methods::<impl char>::to_digit(move _3, const 8_u32) -> bb3; // scope 0 at $DIR/issue-59352.rs:14:26: 14:41
// mir::Constant
// + span: $DIR/issue-59352.rs:14:30: 14:38
// + literal: Const { ty: fn(char, u32) -> std::option::Option<u32> {std::char::methods::<impl char>::to_digit}, val: Value(Scalar(<ZST>)) }
Expand All @@ -55,25 +53,23 @@ fn num_to_digit(_1: char) -> u32 {
}

bb3: {
StorageDead(_4); // scope 0 at $DIR/issue-59352.rs:14:40: 14:41
StorageLive(_9); // scope 0 at $DIR/issue-59352.rs:14:26: 14:50
_9 = discriminant(_3); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
switchInt(move _9) -> [0_isize: bb6, 1_isize: bb8, otherwise: bb7]; // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
StorageDead(_3); // scope 0 at $DIR/issue-59352.rs:14:40: 14:41
StorageLive(_8); // scope 0 at $DIR/issue-59352.rs:14:26: 14:50
_8 = discriminant(_2); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
switchInt(move _8) -> [0_isize: bb6, 1_isize: bb8, otherwise: bb7]; // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
}

bb4: {
StorageDead(_2); // scope 0 at $DIR/issue-59352.rs:14:62: 14:63
return; // scope 0 at $DIR/issue-59352.rs:15:2: 15:2
}

bb5: {
_6 = &_7; // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
_10 = discriminant((*_6)); // scope 2 at $DIR/issue-59352.rs:14:8: 14:23
_2 = Eq(_10, const 1_isize); // scope 2 at $DIR/issue-59352.rs:14:8: 14:23
_5 = &_6; // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
_9 = discriminant((*_5)); // scope 2 at $DIR/issue-59352.rs:14:8: 14:23
StorageDead(_5); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
StorageDead(_6); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
StorageDead(_7); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
StorageDead(_5); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/issue-59352.rs:14:5: 14:63
StorageDead(_4); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
switchInt(move _9) -> [1_isize: bb1, otherwise: bb2]; // scope 0 at $DIR/issue-59352.rs:14:5: 14:63
}

bb6: {
Expand All @@ -94,9 +90,9 @@ fn num_to_digit(_1: char) -> u32 {
}

bb8: {
_0 = move ((_3 as Some).0: u32); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
StorageDead(_9); // scope 0 at $DIR/issue-59352.rs:14:26: 14:50
StorageDead(_3); // scope 0 at $DIR/issue-59352.rs:14:49: 14:50
_0 = move ((_2 as Some).0: u32); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
StorageDead(_8); // scope 0 at $DIR/issue-59352.rs:14:26: 14:50
StorageDead(_2); // scope 0 at $DIR/issue-59352.rs:14:49: 14:50
goto -> bb4; // scope 0 at $DIR/issue-59352.rs:14:5: 14:63
}
}
Expand Up @@ -34,33 +34,30 @@
StorageLive(_5); // scope 3 at $DIR/matches_reduce_branches.rs:16:9: 16:10
StorageLive(_6); // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
- switchInt(_1) -> [7_i32: bb2, otherwise: bb1]; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
- }
-
- bb1: {
- _2 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:27:13: 27:21
- _3 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:28:13: 28:22
- _4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:29:13: 29:22
- _5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:30:13: 30:21
- goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
- }
-
- bb2: {
- _2 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
- _3 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
+ StorageLive(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
+ _11 = _1; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
+ _2 = Ne(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
+ _3 = Eq(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
+ _4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:22:13: 22:22
+ _5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:23:13: 23:21
+ StorageDead(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
+ goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
}

bb1: {
_2 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:27:13: 27:21
_3 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:28:13: 28:22
_4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:29:13: 29:22
_5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:30:13: 30:21
goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
}

bb2: {
_2 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
_3 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
_4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:22:13: 22:22
_5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:23:13: 23:21
goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
}

bb3: {
- goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
- }
-
- bb3: {
+ StorageDead(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
StorageDead(_6); // scope 4 at $DIR/matches_reduce_branches.rs:33:6: 33:7
StorageLive(_7); // scope 4 at $DIR/matches_reduce_branches.rs:35:6: 35:7
_7 = _2; // scope 4 at $DIR/matches_reduce_branches.rs:35:6: 35:7
Expand Down
Expand Up @@ -34,33 +34,30 @@
StorageLive(_5); // scope 3 at $DIR/matches_reduce_branches.rs:16:9: 16:10
StorageLive(_6); // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
- switchInt(_1) -> [7_i32: bb2, otherwise: bb1]; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
- }
-
- bb1: {
- _2 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:27:13: 27:21
- _3 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:28:13: 28:22
- _4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:29:13: 29:22
- _5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:30:13: 30:21
- goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
- }
-
- bb2: {
- _2 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
- _3 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
+ StorageLive(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
+ _11 = _1; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
+ _2 = Ne(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
+ _3 = Eq(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
+ _4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:22:13: 22:22
+ _5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:23:13: 23:21
+ StorageDead(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
+ goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
}

bb1: {
_2 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:27:13: 27:21
_3 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:28:13: 28:22
_4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:29:13: 29:22
_5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:30:13: 30:21
goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
}

bb2: {
_2 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
_3 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
_4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:22:13: 22:22
_5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:23:13: 23:21
goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
}

bb3: {
- goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
- }
-
- bb3: {
+ StorageDead(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
StorageDead(_6); // scope 4 at $DIR/matches_reduce_branches.rs:33:6: 33:7
StorageLive(_7); // scope 4 at $DIR/matches_reduce_branches.rs:35:6: 35:7
_7 = _2; // scope 4 at $DIR/matches_reduce_branches.rs:35:6: 35:7
Expand Down
Expand Up @@ -16,29 +16,31 @@
+ _4 = move _3; // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26
+ _2 = Eq(_4, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
+ StorageDead(_4); // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26
+ goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26
+ switchInt(move _2) -> [false: bb1, otherwise: bb2]; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
}

bb1: {
_2 = const false; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
}
bb2: {
_2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
}
bb3: {
switchInt(move _2) -> [false: bb4, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
}
bb4: {
- _2 = const false; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- }
-
- bb2: {
- _2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- }
-
- bb3: {
- switchInt(move _2) -> [false: bb4, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
- }
-
- bb4: {
_0 = const (); // scope 0 at $DIR/matches_reduce_branches.rs:9:6: 9:6
goto -> bb5; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
- goto -> bb5; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
+ goto -> bb2; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
}

bb5: {
- bb5: {
+ bb2: {
StorageDead(_2); // scope 0 at $DIR/matches_reduce_branches.rs:9:5: 9:6
return; // scope 0 at $DIR/matches_reduce_branches.rs:10:2: 10:2
}
Expand Down

0 comments on commit 1e86570

Please sign in to comment.