Skip to content

Commit 9c20043

Browse files
committed
mir-opt: Use one MirPatch in MatchBranchSimplification
1 parent 934880f commit 9c20043

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

compiler/rustc_mir_transform/src/match_branches.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ impl<'tcx> crate::MirPass<'tcx> for MatchBranchSimplification {
1919

2020
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2121
let typing_env = body.typing_env(tcx);
22-
let mut should_cleanup = false;
23-
for i in 0..body.basic_blocks.len() {
24-
let bbs = &*body.basic_blocks;
25-
let bb_idx = BasicBlock::from_usize(i);
26-
match bbs[bb_idx].terminator().kind {
22+
let mut apply_patch = false;
23+
let mut patch = MirPatch::new(body);
24+
for (bb, data) in body.basic_blocks.iter_enumerated() {
25+
match data.terminator().kind {
2726
TerminatorKind::SwitchInt {
2827
discr: ref _discr @ (Operand::Copy(_) | Operand::Move(_)),
2928
ref targets,
3029
..
3130
// We require that the possible target blocks don't contain this block.
32-
} if !targets.all_targets().contains(&bb_idx) => {}
31+
} if !targets.all_targets().contains(&bb) => {}
3332
// Only optimize switch int statements
3433
_ => continue,
3534
};
3635

37-
if SimplifyToIf.simplify(tcx, body, bb_idx, typing_env).is_some() {
38-
should_cleanup = true;
36+
if SimplifyToIf.simplify(tcx, body, &mut patch, bb, typing_env).is_some() {
37+
apply_patch = true;
3938
continue;
4039
}
41-
if SimplifyToExp::default().simplify(tcx, body, bb_idx, typing_env).is_some() {
42-
should_cleanup = true;
40+
if SimplifyToExp::default().simplify(tcx, body, &mut patch, bb, typing_env).is_some() {
41+
apply_patch = true;
4342
continue;
4443
}
4544
}
4645

47-
if should_cleanup {
46+
if apply_patch {
47+
patch.apply(body);
4848
simplify_cfg(body);
4949
}
5050
}
@@ -61,7 +61,8 @@ trait SimplifyMatch<'tcx> {
6161
fn simplify(
6262
&mut self,
6363
tcx: TyCtxt<'tcx>,
64-
body: &mut Body<'tcx>,
64+
body: &Body<'tcx>,
65+
patch: &mut MirPatch<'tcx>,
6566
switch_bb_idx: BasicBlock,
6667
typing_env: ty::TypingEnv<'tcx>,
6768
) -> Option<()> {
@@ -74,8 +75,6 @@ trait SimplifyMatch<'tcx> {
7475
let discr_ty = discr.ty(body.local_decls(), tcx);
7576
self.can_simplify(tcx, targets, typing_env, bbs, discr_ty)?;
7677

77-
let mut patch = MirPatch::new(body);
78-
7978
// Take ownership of items now that we know we can optimize.
8079
let discr = discr.clone();
8180

@@ -88,19 +87,9 @@ trait SimplifyMatch<'tcx> {
8887
let parent_end = Location { block: switch_bb_idx, statement_index };
8988
patch.add_statement(parent_end, StatementKind::StorageLive(discr_local));
9089
patch.add_assign(parent_end, Place::from(discr_local), Rvalue::Use(discr));
91-
self.new_stmts(
92-
tcx,
93-
targets,
94-
typing_env,
95-
&mut patch,
96-
parent_end,
97-
bbs,
98-
discr_local,
99-
discr_ty,
100-
);
90+
self.new_stmts(tcx, targets, typing_env, patch, parent_end, bbs, discr_local, discr_ty);
10191
patch.add_statement(parent_end, StatementKind::StorageDead(discr_local));
10292
patch.patch_terminator(switch_bb_idx, bbs[first].terminator().kind.clone());
103-
patch.apply(body);
10493
Some(())
10594
}
10695

0 commit comments

Comments
 (0)