@@ -19,30 +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  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  { 
2526                TerminatorKind :: SwitchInt  { 
2627                    discr :  Operand :: Copy ( _)  | Operand :: Move ( _) , 
2728                    targets, 
2829                    ..
2930                    // 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 )  => { } 
3132                // Only optimize switch int statements 
3233                _ => continue , 
3334            } ; 
3435
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 ; 
3738                continue ; 
3839            } 
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 ; 
4142                continue ; 
4243            } 
4344        } 
4445
45-         if  should_cleanup { 
46+         if  apply_patch { 
47+             patch. apply ( body) ; 
4648            simplify_cfg ( tcx,  body) ; 
4749        } 
4850    } 
@@ -59,7 +61,8 @@ trait SimplifyMatch<'tcx> {
5961     fn  simplify ( 
6062        & mut  self , 
6163        tcx :  TyCtxt < ' tcx > , 
62-         body :  & mut  Body < ' tcx > , 
64+         body :  & Body < ' tcx > , 
65+         patch :  & mut  MirPatch < ' tcx > , 
6366        switch_bb_idx :  BasicBlock , 
6467        typing_env :  ty:: TypingEnv < ' tcx > , 
6568    )  -> Option < ( ) >  { 
@@ -73,8 +76,6 @@ trait SimplifyMatch<'tcx> {
7376        let  discr_ty = discr. ty ( body. local_decls ( ) ,  tcx) ; 
7477        self . can_simplify ( tcx,  targets,  typing_env,  bbs,  discr_ty) ?; 
7578
76-         let  mut  patch = MirPatch :: new ( body) ; 
77- 
7879        // Take ownership of items now that we know we can optimize. 
7980        let  discr = discr. clone ( ) ; 
8081
@@ -87,19 +88,9 @@ trait SimplifyMatch<'tcx> {
8788        let  parent_end = Location  {  block :  switch_bb_idx,  statement_index } ; 
8889        patch. add_statement ( parent_end,  StatementKind :: StorageLive ( discr_local) ) ; 
8990        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) ; 
10092        patch. add_statement ( parent_end,  StatementKind :: StorageDead ( discr_local) ) ; 
10193        patch. patch_terminator ( switch_bb_idx,  bbs[ first] . terminator ( ) . kind . clone ( ) ) ; 
102-         patch. apply ( body) ; 
10394        Some ( ( ) ) 
10495    } 
10596
0 commit comments