@@ -19,32 +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 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 {
27
26
TerminatorKind :: SwitchInt {
28
27
discr : ref _discr @ ( Operand :: Copy ( _) | Operand :: Move ( _) ) ,
29
28
ref targets,
30
29
..
31
30
// 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 ) => { }
33
32
// Only optimize switch int statements
34
33
_ => continue ,
35
34
} ;
36
35
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 ;
39
38
continue ;
40
39
}
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 ;
43
42
continue ;
44
43
}
45
44
}
46
45
47
- if should_cleanup {
46
+ if apply_patch {
47
+ patch. apply ( body) ;
48
48
simplify_cfg ( body) ;
49
49
}
50
50
}
@@ -61,7 +61,8 @@ trait SimplifyMatch<'tcx> {
61
61
fn simplify (
62
62
& mut self ,
63
63
tcx : TyCtxt < ' tcx > ,
64
- body : & mut Body < ' tcx > ,
64
+ body : & Body < ' tcx > ,
65
+ patch : & mut MirPatch < ' tcx > ,
65
66
switch_bb_idx : BasicBlock ,
66
67
typing_env : ty:: TypingEnv < ' tcx > ,
67
68
) -> Option < ( ) > {
@@ -74,8 +75,6 @@ trait SimplifyMatch<'tcx> {
74
75
let discr_ty = discr. ty ( body. local_decls ( ) , tcx) ;
75
76
self . can_simplify ( tcx, targets, typing_env, bbs, discr_ty) ?;
76
77
77
- let mut patch = MirPatch :: new ( body) ;
78
-
79
78
// Take ownership of items now that we know we can optimize.
80
79
let discr = discr. clone ( ) ;
81
80
@@ -88,19 +87,9 @@ trait SimplifyMatch<'tcx> {
88
87
let parent_end = Location { block : switch_bb_idx, statement_index } ;
89
88
patch. add_statement ( parent_end, StatementKind :: StorageLive ( discr_local) ) ;
90
89
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) ;
101
91
patch. add_statement ( parent_end, StatementKind :: StorageDead ( discr_local) ) ;
102
92
patch. patch_terminator ( switch_bb_idx, bbs[ first] . terminator ( ) . kind . clone ( ) ) ;
103
- patch. apply ( body) ;
104
93
Some ( ( ) )
105
94
}
106
95
0 commit comments