@@ -19,30 +19,32 @@ impl<'tcx> crate::MirPass<'tcx> for MatchBranchSimplification {
19
19
20
20
fn run_pass ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
21
21
let typing_env = body. typing_env ( tcx) ;
22
- let mut should_cleanup = false ;
23
- for bb_idx in body. basic_blocks . indices ( ) {
24
- match & body. basic_blocks [ bb_idx] . terminator ( ) . kind {
22
+ let mut apply_patch = false ;
23
+ let mut patch = MirPatch :: new ( body) ;
24
+ for ( bb, bb_data) in body. basic_blocks . iter_enumerated ( ) {
25
+ match & bb_data. terminator ( ) . kind {
25
26
TerminatorKind :: SwitchInt {
26
27
discr : Operand :: Copy ( _) | Operand :: Move ( _) ,
27
28
targets,
28
29
..
29
30
// We require that the possible target blocks don't contain this block.
30
- } if !targets. all_targets ( ) . contains ( & bb_idx ) => { }
31
+ } if !targets. all_targets ( ) . contains ( & bb ) => { }
31
32
// Only optimize switch int statements
32
33
_ => continue ,
33
34
} ;
34
35
35
- if SimplifyToIf . simplify ( tcx, body, bb_idx , typing_env) . is_some ( ) {
36
- should_cleanup = true ;
36
+ if SimplifyToIf . simplify ( tcx, body, & mut patch , bb , typing_env) . is_some ( ) {
37
+ apply_patch = true ;
37
38
continue ;
38
39
}
39
- if SimplifyToExp :: default ( ) . simplify ( tcx, body, bb_idx , typing_env) . is_some ( ) {
40
- should_cleanup = true ;
40
+ if SimplifyToExp :: default ( ) . simplify ( tcx, body, & mut patch , bb , typing_env) . is_some ( ) {
41
+ apply_patch = true ;
41
42
continue ;
42
43
}
43
44
}
44
45
45
- if should_cleanup {
46
+ if apply_patch {
47
+ patch. apply ( body) ;
46
48
simplify_cfg ( tcx, body) ;
47
49
}
48
50
}
@@ -59,7 +61,8 @@ trait SimplifyMatch<'tcx> {
59
61
fn simplify (
60
62
& mut self ,
61
63
tcx : TyCtxt < ' tcx > ,
62
- body : & mut Body < ' tcx > ,
64
+ body : & Body < ' tcx > ,
65
+ patch : & mut MirPatch < ' tcx > ,
63
66
switch_bb_idx : BasicBlock ,
64
67
typing_env : ty:: TypingEnv < ' tcx > ,
65
68
) -> Option < ( ) > {
@@ -73,8 +76,6 @@ trait SimplifyMatch<'tcx> {
73
76
let discr_ty = discr. ty ( body. local_decls ( ) , tcx) ;
74
77
self . can_simplify ( tcx, targets, typing_env, bbs, discr_ty) ?;
75
78
76
- let mut patch = MirPatch :: new ( body) ;
77
-
78
79
// Take ownership of items now that we know we can optimize.
79
80
let discr = discr. clone ( ) ;
80
81
@@ -87,19 +88,9 @@ trait SimplifyMatch<'tcx> {
87
88
let parent_end = Location { block : switch_bb_idx, statement_index } ;
88
89
patch. add_statement ( parent_end, StatementKind :: StorageLive ( discr_local) ) ;
89
90
patch. add_assign ( parent_end, Place :: from ( discr_local) , Rvalue :: Use ( discr) ) ;
90
- self . new_stmts (
91
- tcx,
92
- targets,
93
- typing_env,
94
- & mut patch,
95
- parent_end,
96
- bbs,
97
- discr_local,
98
- discr_ty,
99
- ) ;
91
+ self . new_stmts ( tcx, targets, typing_env, patch, parent_end, bbs, discr_local, discr_ty) ;
100
92
patch. add_statement ( parent_end, StatementKind :: StorageDead ( discr_local) ) ;
101
93
patch. patch_terminator ( switch_bb_idx, bbs[ first] . terminator ( ) . kind . clone ( ) ) ;
102
- patch. apply ( body) ;
103
94
Some ( ( ) )
104
95
}
105
96
0 commit comments