@@ -46,27 +46,32 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
46
46
47
47
for opt_to_apply in opts_to_apply {
48
48
trace ! ( "SUCCESS: found optimization possibility to apply: {:?}" , & opt_to_apply) ;
49
- // create the patch using MirPatch
50
- let mut patch = MirPatch :: new ( body) ;
51
49
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) ;
56
50
let statements_before =
57
51
body. basic_blocks ( ) [ opt_to_apply. basic_block_first_switch ] . statements . len ( ) ;
58
52
let end_of_block_location = Location {
59
53
block : opt_to_apply. basic_block_first_switch ,
60
54
statement_index : statements_before,
61
55
} ;
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
+ ) ;
63
68
64
69
// create assignment of discriminant
65
70
let place_of_adt_to_get_discriminant_of =
66
71
opt_to_apply. infos [ 0 ] . second_switch_info . place_of_adt_discr_read ;
67
72
patch. add_assign (
68
73
end_of_block_location,
69
- Place :: from ( temp ) ,
74
+ Place :: from ( second_discriminant_temp ) ,
70
75
Rvalue :: Discriminant ( place_of_adt_to_get_discriminant_of) ,
71
76
) ;
72
77
@@ -81,7 +86,7 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
81
86
opt_to_apply. infos [ 0 ] . first_switch_info . discr_used_in_switch ;
82
87
let not_equal_rvalue = Rvalue :: BinaryOp (
83
88
not_equal,
84
- Operand :: Copy ( Place :: from ( temp ) ) ,
89
+ Operand :: Copy ( Place :: from ( second_discriminant_temp ) ) ,
85
90
Operand :: Copy ( Place :: from ( first_descriminant_place) ) ,
86
91
) ;
87
92
patch. add_statement (
@@ -126,8 +131,19 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
126
131
) ,
127
132
) ;
128
133
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
+ }
131
147
132
148
patch. apply ( body) ;
133
149
}
0 commit comments