@@ -9,6 +9,8 @@ use rustc_middle::mir::{
99use rustc_middle:: ty:: { Ty , TyCtxt } ;
1010use tracing:: trace;
1111
12+ use crate :: ssa:: SsaLocals ;
13+
1214/// Pass to convert `if` conditions on integrals into switches on the integral.
1315/// For an example, it turns something like
1416///
@@ -33,8 +35,10 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyComparisonIntegral {
3335 fn run_pass ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
3436 trace ! ( "Running SimplifyComparisonIntegral on {:?}" , body. source) ;
3537
38+ let typing_env = body. typing_env ( tcx) ;
39+ let ssa = SsaLocals :: new ( tcx, body, typing_env) ;
3640 let helper = OptimizationFinder { body } ;
37- let opts = helper. find_optimizations ( ) ;
41+ let opts = helper. find_optimizations ( & ssa ) ;
3842 let mut storage_deads_to_insert = vec ! [ ] ;
3943 let mut storage_deads_to_remove: Vec < ( usize , BasicBlock ) > = vec ! [ ] ;
4044 let typing_env = body. typing_env ( tcx) ;
@@ -154,19 +158,18 @@ struct OptimizationFinder<'a, 'tcx> {
154158}
155159
156160impl < ' tcx > OptimizationFinder < ' _ , ' tcx > {
157- fn find_optimizations ( & self ) -> Vec < OptimizationInfo < ' tcx > > {
161+ fn find_optimizations ( & self , ssa : & SsaLocals ) -> Vec < OptimizationInfo < ' tcx > > {
158162 self . body
159163 . basic_blocks
160164 . iter_enumerated ( )
161165 . filter_map ( |( bb_idx, bb) | {
162166 // find switch
163- let ( place_switched_on, targets, place_switched_on_moved) =
164- match & bb. terminator ( ) . kind {
165- rustc_middle:: mir:: TerminatorKind :: SwitchInt { discr, targets, .. } => {
166- Some ( ( discr. place ( ) ?, targets, discr. is_move ( ) ) )
167- }
168- _ => None ,
169- } ?;
167+ let ( discr, targets) = bb. terminator ( ) . kind . as_switch ( ) ?;
168+ let place_switched_on = discr. place ( ) ?;
169+ // Make sure that the place is not modified.
170+ if !ssa. is_ssa ( place_switched_on. local ) || !place_switched_on. is_stable_offset ( ) {
171+ return None ;
172+ }
170173
171174 // find the statement that assigns the place being switched on
172175 bb. statements . iter ( ) . enumerate ( ) . rev ( ) . find_map ( |( stmt_idx, stmt) | {
@@ -180,12 +183,12 @@ impl<'tcx> OptimizationFinder<'_, 'tcx> {
180183 box ( left, right) ,
181184 ) => {
182185 let ( branch_value_scalar, branch_value_ty, to_switch_on) =
183- find_branch_value_info ( left, right) ?;
186+ find_branch_value_info ( left, right, ssa ) ?;
184187
185188 Some ( OptimizationInfo {
186189 bin_op_stmt_idx : stmt_idx,
187190 bb_idx,
188- can_remove_bin_op_stmt : place_switched_on_moved ,
191+ can_remove_bin_op_stmt : discr . is_move ( ) ,
189192 to_switch_on,
190193 branch_value_scalar,
191194 branch_value_ty,
@@ -207,13 +210,18 @@ impl<'tcx> OptimizationFinder<'_, 'tcx> {
207210fn find_branch_value_info < ' tcx > (
208211 left : & Operand < ' tcx > ,
209212 right : & Operand < ' tcx > ,
213+ ssa : & SsaLocals ,
210214) -> Option < ( Scalar , Ty < ' tcx > , Place < ' tcx > ) > {
211215 // check that either left or right is a constant.
212216 // if any are, we can use the other to switch on, and the constant as a value in a switch
213217 use Operand :: * ;
214218 match ( left, right) {
215219 ( Constant ( branch_value) , Copy ( to_switch_on) | Move ( to_switch_on) )
216220 | ( Copy ( to_switch_on) | Move ( to_switch_on) , Constant ( branch_value) ) => {
221+ // Make sure that the place is not modified.
222+ if !ssa. is_ssa ( to_switch_on. local ) || !to_switch_on. is_stable_offset ( ) {
223+ return None ;
224+ }
217225 let branch_value_ty = branch_value. const_ . ty ( ) ;
218226 // we only want to apply this optimization if we are matching on integrals (and chars),
219227 // as it is not possible to switch on floats
0 commit comments