@@ -3,6 +3,7 @@ use rustc_middle::mir::patch::MirPatch;
3
3
use rustc_middle:: mir:: * ;
4
4
use rustc_middle:: ty:: { ParamEnv , ScalarInt , Ty , TyCtxt } ;
5
5
use rustc_target:: abi:: Size ;
6
+ use std:: cmp:: Ordering ;
6
7
use std:: iter;
7
8
8
9
use super :: simplify:: simplify_cfg;
@@ -263,33 +264,59 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
263
264
}
264
265
}
265
266
267
+ /// Check if the cast constant using `IntToInt` is equal to the target constant.
268
+ fn can_cast (
269
+ from_val : impl Into < u128 > ,
270
+ from_size : Size ,
271
+ target_scalar : ScalarInt ,
272
+ from_is_signed : bool ,
273
+ ) -> bool {
274
+ let from_val = from_val. into ( ) ;
275
+ let from_scalar = ScalarInt :: try_from_uint ( from_val, from_size) . unwrap ( ) ;
276
+ let to_size = target_scalar. size ( ) ;
277
+ let cast_scalar = match to_size. cmp ( & from_size) {
278
+ // Truncate to the target size.
279
+ Ordering :: Less => ScalarInt :: truncate_from_uint ( from_val, to_size) . 0 ,
280
+ Ordering :: Equal => from_scalar,
281
+ Ordering :: Greater => {
282
+ if from_is_signed {
283
+ // Extend to the target size using sext.
284
+ ScalarInt :: try_from_int ( from_scalar. to_int ( from_size) , to_size) . unwrap ( )
285
+ } else {
286
+ // Extend to the target size using zext.
287
+ ScalarInt :: try_from_uint ( from_scalar. to_uint ( from_size) , to_size) . unwrap ( )
288
+ }
289
+ }
290
+ } ;
291
+ cast_scalar == target_scalar
292
+ }
293
+
266
294
#[ derive( Default ) ]
267
295
struct SimplifyToExp {
268
- transfrom_types : Vec < TransfromType > ,
296
+ transfrom_kinds : Vec < TransfromKind > ,
269
297
}
270
298
271
299
#[ derive( Clone , Copy ) ]
272
- enum CompareType < ' tcx , ' a > {
300
+ enum ExpectedTransformKind < ' tcx , ' a > {
273
301
/// Identical statements.
274
302
Same ( & ' a StatementKind < ' tcx > ) ,
275
303
/// Assignment statements have the same value.
276
- Eq ( & ' a Place < ' tcx > , Ty < ' tcx > , ScalarInt ) ,
304
+ SameByEq { place : & ' a Place < ' tcx > , ty : Ty < ' tcx > , scalar : ScalarInt } ,
277
305
/// Enum variant comparison type.
278
- Discr { place : & ' a Place < ' tcx > , ty : Ty < ' tcx > , is_signed : bool } ,
306
+ Cast { place : & ' a Place < ' tcx > , ty : Ty < ' tcx > } ,
279
307
}
280
308
281
- enum TransfromType {
309
+ enum TransfromKind {
282
310
Same ,
283
- Eq ,
284
- Discr ,
311
+ Cast ,
285
312
}
286
313
287
- impl From < CompareType < ' _ , ' _ > > for TransfromType {
288
- fn from ( compare_type : CompareType < ' _ , ' _ > ) -> Self {
314
+ impl From < ExpectedTransformKind < ' _ , ' _ > > for TransfromKind {
315
+ fn from ( compare_type : ExpectedTransformKind < ' _ , ' _ > ) -> Self {
289
316
match compare_type {
290
- CompareType :: Same ( _) => TransfromType :: Same ,
291
- CompareType :: Eq ( _ , _ , _ ) => TransfromType :: Eq ,
292
- CompareType :: Discr { .. } => TransfromType :: Discr ,
317
+ ExpectedTransformKind :: Same ( _) => TransfromKind :: Same ,
318
+ ExpectedTransformKind :: SameByEq { .. } => TransfromKind :: Same ,
319
+ ExpectedTransformKind :: Cast { .. } => TransfromKind :: Cast ,
293
320
}
294
321
}
295
322
}
@@ -353,7 +380,7 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
353
380
return None ;
354
381
}
355
382
let mut target_iter = targets. iter ( ) ;
356
- let ( first_val , first_target) = target_iter. next ( ) . unwrap ( ) ;
383
+ let ( first_case_val , first_target) = target_iter. next ( ) . unwrap ( ) ;
357
384
let first_terminator_kind = & bbs[ first_target] . terminator ( ) . kind ;
358
385
// Check that destinations are identical, and if not, then don't optimize this block
359
386
if !targets
@@ -365,22 +392,18 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
365
392
366
393
let discr_size = tcx. layout_of ( param_env. and ( discr_ty) ) . unwrap ( ) . size ;
367
394
let first_stmts = & bbs[ first_target] . statements ;
368
- let ( second_val , second_target) = target_iter. next ( ) . unwrap ( ) ;
395
+ let ( second_case_val , second_target) = target_iter. next ( ) . unwrap ( ) ;
369
396
let second_stmts = & bbs[ second_target] . statements ;
370
397
if first_stmts. len ( ) != second_stmts. len ( ) {
371
398
return None ;
372
399
}
373
400
374
- fn int_equal ( l : ScalarInt , r : impl Into < u128 > , size : Size ) -> bool {
375
- l. to_bits_unchecked ( ) == ScalarInt :: try_from_uint ( r, size) . unwrap ( ) . to_bits_unchecked ( )
376
- }
377
-
378
401
// We first compare the two branches, and then the other branches need to fulfill the same conditions.
379
- let mut compare_types = Vec :: new ( ) ;
402
+ let mut expected_transform_kinds = Vec :: new ( ) ;
380
403
for ( f, s) in iter:: zip ( first_stmts, second_stmts) {
381
404
let compare_type = match ( & f. kind , & s. kind ) {
382
405
// If two statements are exactly the same, we can optimize.
383
- ( f_s, s_s) if f_s == s_s => CompareType :: Same ( f_s) ,
406
+ ( f_s, s_s) if f_s == s_s => ExpectedTransformKind :: Same ( f_s) ,
384
407
385
408
// If two statements are assignments with the match values to the same place, we can optimize.
386
409
(
@@ -394,22 +417,23 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
394
417
f_c. const_ . try_eval_scalar_int ( tcx, param_env) ,
395
418
s_c. const_ . try_eval_scalar_int ( tcx, param_env) ,
396
419
) {
397
- ( Some ( f) , Some ( s) ) if f == s => CompareType :: Eq ( lhs_f, f_c. const_ . ty ( ) , f) ,
398
- // Enum variants can also be simplified to an assignment statement if their values are equal.
399
- // We need to consider both unsigned and signed scenarios here.
420
+ ( Some ( f) , Some ( s) ) if f == s => ExpectedTransformKind :: SameByEq {
421
+ place : lhs_f,
422
+ ty : f_c. const_ . ty ( ) ,
423
+ scalar : f,
424
+ } ,
425
+ // Enum variants can also be simplified to an assignment statement,
426
+ // if we can use `IntToInt` cast to get an equal value.
400
427
( Some ( f) , Some ( s) )
401
- if ( ( f_c. const_ . ty ( ) . is_signed ( ) || discr_ty. is_signed ( ) )
402
- && int_equal ( f, first_val, discr_size)
403
- && int_equal ( s, second_val, discr_size) )
404
- || ( Some ( f) == ScalarInt :: try_from_uint ( first_val, f. size ( ) )
405
- && Some ( s)
406
- == ScalarInt :: try_from_uint ( second_val, s. size ( ) ) ) =>
428
+ if ( can_cast ( first_case_val, discr_size, f, discr_ty. is_signed ( ) )
429
+ && can_cast (
430
+ second_case_val,
431
+ discr_size,
432
+ s,
433
+ discr_ty. is_signed ( ) ,
434
+ ) ) =>
407
435
{
408
- CompareType :: Discr {
409
- place : lhs_f,
410
- ty : f_c. const_ . ty ( ) ,
411
- is_signed : f_c. const_ . ty ( ) . is_signed ( ) || discr_ty. is_signed ( ) ,
412
- }
436
+ ExpectedTransformKind :: Cast { place : lhs_f, ty : f_c. const_ . ty ( ) }
413
437
}
414
438
_ => {
415
439
return None ;
@@ -420,47 +444,36 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
420
444
// Otherwise we cannot optimize. Try another block.
421
445
_ => return None ,
422
446
} ;
423
- compare_types . push ( compare_type) ;
447
+ expected_transform_kinds . push ( compare_type) ;
424
448
}
425
449
426
450
// All remaining BBs need to fulfill the same pattern as the two BBs from the previous step.
427
451
for ( other_val, other_target) in target_iter {
428
452
let other_stmts = & bbs[ other_target] . statements ;
429
- if compare_types . len ( ) != other_stmts. len ( ) {
453
+ if expected_transform_kinds . len ( ) != other_stmts. len ( ) {
430
454
return None ;
431
455
}
432
- for ( f, s) in iter:: zip ( & compare_types , other_stmts) {
456
+ for ( f, s) in iter:: zip ( & expected_transform_kinds , other_stmts) {
433
457
match ( * f, & s. kind ) {
434
- ( CompareType :: Same ( f_s) , s_s) if f_s == s_s => { }
458
+ ( ExpectedTransformKind :: Same ( f_s) , s_s) if f_s == s_s => { }
435
459
(
436
- CompareType :: Eq ( lhs_f, f_ty, val ) ,
460
+ ExpectedTransformKind :: SameByEq { place : lhs_f, ty : f_ty, scalar } ,
437
461
StatementKind :: Assign ( box ( lhs_s, Rvalue :: Use ( Operand :: Constant ( s_c) ) ) ) ,
438
462
) if lhs_f == lhs_s
439
463
&& s_c. const_ . ty ( ) == f_ty
440
- && s_c. const_ . try_eval_scalar_int ( tcx, param_env) == Some ( val ) => { }
464
+ && s_c. const_ . try_eval_scalar_int ( tcx, param_env) == Some ( scalar ) => { }
441
465
(
442
- CompareType :: Discr { place : lhs_f, ty : f_ty, is_signed } ,
466
+ ExpectedTransformKind :: Cast { place : lhs_f, ty : f_ty } ,
443
467
StatementKind :: Assign ( box ( lhs_s, Rvalue :: Use ( Operand :: Constant ( s_c) ) ) ) ,
444
- ) if lhs_f == lhs_s && s_c. const_ . ty ( ) == f_ty => {
445
- let Some ( f) = s_c. const_ . try_eval_scalar_int ( tcx, param_env) else {
446
- return None ;
447
- } ;
448
- if is_signed
449
- && s_c. const_ . ty ( ) . is_signed ( )
450
- && int_equal ( f, other_val, discr_size)
451
- {
452
- continue ;
453
- }
454
- if Some ( f) == ScalarInt :: try_from_uint ( other_val, f. size ( ) ) {
455
- continue ;
456
- }
457
- return None ;
458
- }
468
+ ) if let Some ( f) = s_c. const_ . try_eval_scalar_int ( tcx, param_env)
469
+ && lhs_f == lhs_s
470
+ && s_c. const_ . ty ( ) == f_ty
471
+ && can_cast ( other_val, discr_size, f, discr_ty. is_signed ( ) ) => { }
459
472
_ => return None ,
460
473
}
461
474
}
462
475
}
463
- self . transfrom_types = compare_types . into_iter ( ) . map ( |c| c. into ( ) ) . collect ( ) ;
476
+ self . transfrom_kinds = expected_transform_kinds . into_iter ( ) . map ( |c| c. into ( ) ) . collect ( ) ;
464
477
Some ( ( ) )
465
478
}
466
479
@@ -478,13 +491,13 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
478
491
let ( _, first) = targets. iter ( ) . next ( ) . unwrap ( ) ;
479
492
let first = & bbs[ first] ;
480
493
481
- for ( t, s) in iter:: zip ( & self . transfrom_types , & first. statements ) {
494
+ for ( t, s) in iter:: zip ( & self . transfrom_kinds , & first. statements ) {
482
495
match ( t, & s. kind ) {
483
- ( TransfromType :: Same , _ ) | ( TransfromType :: Eq , _) => {
496
+ ( TransfromKind :: Same , _) => {
484
497
patch. add_statement ( parent_end, s. kind . clone ( ) ) ;
485
498
}
486
499
(
487
- TransfromType :: Discr ,
500
+ TransfromKind :: Cast ,
488
501
StatementKind :: Assign ( box ( lhs, Rvalue :: Use ( Operand :: Constant ( f_c) ) ) ) ,
489
502
) => {
490
503
let operand = Operand :: Copy ( Place :: from ( discr_local) ) ;
0 commit comments