@@ -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///
@@ -27,17 +29,18 @@ pub(super) struct SimplifyComparisonIntegral;
2729
2830impl < ' tcx > crate :: MirPass < ' tcx > for SimplifyComparisonIntegral {
2931 fn is_enabled ( & self , sess : & rustc_session:: Session ) -> bool {
30- sess. mir_opt_level ( ) > 0
32+ sess. mir_opt_level ( ) > 1
3133 }
3234
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 ! [ ] ;
40- let typing_env = body. typing_env ( tcx) ;
4144 for opt in opts {
4245 trace ! ( "SUCCESS: Applying {:?}" , opt) ;
4346 // replace terminator with a switchInt that switches on the integer directly
@@ -132,7 +135,7 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyComparisonIntegral {
132135
133136 let terminator = bb. terminator_mut ( ) ;
134137 terminator. kind =
135- TerminatorKind :: SwitchInt { discr : Operand :: Move ( opt. to_switch_on ) , targets } ;
138+ TerminatorKind :: SwitchInt { discr : Operand :: Copy ( opt. to_switch_on ) , targets } ;
136139 }
137140
138141 for ( idx, bb_idx) in storage_deads_to_remove {
@@ -154,19 +157,18 @@ struct OptimizationFinder<'a, 'tcx> {
154157}
155158
156159impl < ' tcx > OptimizationFinder < ' _ , ' tcx > {
157- fn find_optimizations ( & self ) -> Vec < OptimizationInfo < ' tcx > > {
160+ fn find_optimizations ( & self , ssa : & SsaLocals ) -> Vec < OptimizationInfo < ' tcx > > {
158161 self . body
159162 . basic_blocks
160163 . iter_enumerated ( )
161164 . filter_map ( |( bb_idx, bb) | {
162165 // 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- } ?;
166+ let ( discr, targets) = bb. terminator ( ) . kind . as_switch ( ) ?;
167+ let place_switched_on = discr. place ( ) ?;
168+ // Make sure that the place is not modified.
169+ if !ssa. is_ssa ( place_switched_on. local ) || !place_switched_on. is_stable_offset ( ) {
170+ return None ;
171+ }
170172
171173 // find the statement that assigns the place being switched on
172174 bb. statements . iter ( ) . enumerate ( ) . rev ( ) . find_map ( |( stmt_idx, stmt) | {
@@ -180,12 +182,12 @@ impl<'tcx> OptimizationFinder<'_, 'tcx> {
180182 box ( left, right) ,
181183 ) => {
182184 let ( branch_value_scalar, branch_value_ty, to_switch_on) =
183- find_branch_value_info ( left, right) ?;
185+ find_branch_value_info ( left, right, ssa ) ?;
184186
185187 Some ( OptimizationInfo {
186188 bin_op_stmt_idx : stmt_idx,
187189 bb_idx,
188- can_remove_bin_op_stmt : place_switched_on_moved ,
190+ can_remove_bin_op_stmt : discr . is_move ( ) ,
189191 to_switch_on,
190192 branch_value_scalar,
191193 branch_value_ty,
@@ -207,13 +209,18 @@ impl<'tcx> OptimizationFinder<'_, 'tcx> {
207209fn find_branch_value_info < ' tcx > (
208210 left : & Operand < ' tcx > ,
209211 right : & Operand < ' tcx > ,
212+ ssa : & SsaLocals ,
210213) -> Option < ( Scalar , Ty < ' tcx > , Place < ' tcx > ) > {
211214 // check that either left or right is a constant.
212215 // if any are, we can use the other to switch on, and the constant as a value in a switch
213216 use Operand :: * ;
214217 match ( left, right) {
215218 ( Constant ( branch_value) , Copy ( to_switch_on) | Move ( to_switch_on) )
216219 | ( Copy ( to_switch_on) | Move ( to_switch_on) , Constant ( branch_value) ) => {
220+ // Make sure that the place is not modified.
221+ if !ssa. is_ssa ( to_switch_on. local ) || !to_switch_on. is_stable_offset ( ) {
222+ return None ;
223+ }
217224 let branch_value_ty = branch_value. const_ . ty ( ) ;
218225 // we only want to apply this optimization if we are matching on integrals (and chars),
219226 // as it is not possible to switch on floats
0 commit comments