@@ -46,27 +46,32 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
4646
4747 for opt_to_apply in opts_to_apply {
4848 trace ! ( "SUCCESS: found optimization possibility to apply: {:?}" , & opt_to_apply) ;
49- // create the patch using MirPatch
50- let mut patch = MirPatch :: new ( body) ;
5149
52- // create temp to store second discriminant in
53- let discr_type = opt_to_apply. infos [ 0 ] . second_switch_info . discr_ty ;
54- let discr_span = opt_to_apply. infos [ 0 ] . second_switch_info . discr_source_info . span ;
55- let temp = patch. new_temp ( discr_type, discr_span) ;
5650 let statements_before =
5751 body. basic_blocks ( ) [ opt_to_apply. basic_block_first_switch ] . statements . len ( ) ;
5852 let end_of_block_location = Location {
5953 block : opt_to_apply. basic_block_first_switch ,
6054 statement_index : statements_before,
6155 } ;
62- patch. add_statement ( end_of_block_location, StatementKind :: StorageLive ( temp) ) ;
56+
57+ let mut patch = MirPatch :: new ( body) ;
58+
59+ // create temp to store second discriminant in
60+ let discr_type = opt_to_apply. infos [ 0 ] . second_switch_info . discr_ty ;
61+ let discr_span = opt_to_apply. infos [ 0 ] . second_switch_info . discr_source_info . span ;
62+ let second_discriminant_temp = patch. new_temp ( discr_type, discr_span) ;
63+
64+ patch. add_statement (
65+ end_of_block_location,
66+ StatementKind :: StorageLive ( second_discriminant_temp) ,
67+ ) ;
6368
6469 // create assignment of discriminant
6570 let place_of_adt_to_get_discriminant_of =
6671 opt_to_apply. infos [ 0 ] . second_switch_info . place_of_adt_discr_read ;
6772 patch. add_assign (
6873 end_of_block_location,
69- Place :: from ( temp ) ,
74+ Place :: from ( second_discriminant_temp ) ,
7075 Rvalue :: Discriminant ( place_of_adt_to_get_discriminant_of) ,
7176 ) ;
7277
@@ -81,7 +86,7 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
8186 opt_to_apply. infos [ 0 ] . first_switch_info . discr_used_in_switch ;
8287 let not_equal_rvalue = Rvalue :: BinaryOp (
8388 not_equal,
84- Operand :: Copy ( Place :: from ( temp ) ) ,
89+ Operand :: Copy ( Place :: from ( second_discriminant_temp ) ) ,
8590 Operand :: Copy ( Place :: from ( first_descriminant_place) ) ,
8691 ) ;
8792 patch. add_statement (
@@ -126,8 +131,19 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
126131 ) ,
127132 ) ;
128133
129- // generate StorageDead for the temp not in use anymore. We use the not_equal_temp in the switch, so we can't mark that dead
130- patch. add_statement ( end_of_block_location, StatementKind :: StorageDead ( temp) ) ;
134+ // generate StorageDead for the second_discriminant_temp not in use anymore
135+ patch. add_statement (
136+ end_of_block_location,
137+ StatementKind :: StorageDead ( second_discriminant_temp) ,
138+ ) ;
139+
140+ // Generate a StorageDead for not_equal_temp in each of the targets, since we moved it into the switch
141+ for bb in [ false_case, true_case] . iter ( ) {
142+ patch. add_statement (
143+ Location { block : * bb, statement_index : 0 } ,
144+ StatementKind :: StorageDead ( not_equal_temp) ,
145+ ) ;
146+ }
131147
132148 patch. apply ( body) ;
133149 }
0 commit comments