@@ -41,10 +41,7 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
41
41
should_cleanup = true ;
42
42
continue ;
43
43
}
44
- // unsound: https://github.com/rust-lang/rust/issues/124150
45
- if tcx. sess . opts . unstable_opts . unsound_mir_opts
46
- && SimplifyToExp :: default ( ) . simplify ( tcx, body, bb_idx, param_env) . is_some ( )
47
- {
44
+ if SimplifyToExp :: default ( ) . simplify ( tcx, body, bb_idx, param_env) . is_some ( ) {
48
45
should_cleanup = true ;
49
46
continue ;
50
47
}
@@ -263,33 +260,49 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
263
260
}
264
261
}
265
262
263
+ /// Check if the cast constant using `IntToInt` is equal to the target constant.
264
+ fn can_cast (
265
+ from_val : impl Into < u128 > ,
266
+ from_size : Size ,
267
+ target_scalar : ScalarInt ,
268
+ target_is_signed : bool ,
269
+ ) -> bool {
270
+ let from_scalar = ScalarInt :: try_from_uint ( from_val. into ( ) , from_size) . unwrap ( ) ;
271
+ let to_size = target_scalar. size ( ) ;
272
+ let cast_scalar = if target_is_signed {
273
+ ScalarInt :: try_from_int ( from_scalar. to_int ( from_size) , to_size) . unwrap ( )
274
+ } else {
275
+ ScalarInt :: try_from_uint ( from_scalar. to_uint ( from_size) , to_size) . unwrap ( )
276
+ } ;
277
+ cast_scalar == target_scalar
278
+ }
279
+
266
280
#[ derive( Default ) ]
267
281
struct SimplifyToExp {
268
- transfrom_types : Vec < TransfromType > ,
282
+ transfrom_kinds : Vec < TransfromKind > ,
269
283
}
270
284
271
285
#[ derive( Clone , Copy ) ]
272
- enum CompareType < ' tcx , ' a > {
286
+ enum ExpectedTransformKind < ' tcx , ' a > {
273
287
/// Identical statements.
274
288
Same ( & ' a StatementKind < ' tcx > ) ,
275
289
/// Assignment statements have the same value.
276
- Eq ( & ' a Place < ' tcx > , Ty < ' tcx > , ScalarInt ) ,
290
+ SameByEq { place : & ' a Place < ' tcx > , ty : Ty < ' tcx > , scalar : ScalarInt } ,
277
291
/// Enum variant comparison type.
278
- Discr { place : & ' a Place < ' tcx > , ty : Ty < ' tcx > , is_signed : bool } ,
292
+ Cast { place : & ' a Place < ' tcx > , ty : Ty < ' tcx > } ,
279
293
}
280
294
281
- enum TransfromType {
295
+ enum TransfromKind {
282
296
Same ,
283
- Eq ,
284
- Discr ,
297
+ Cast ,
285
298
}
286
299
287
- impl From < CompareType < ' _ , ' _ > > for TransfromType {
288
- fn from ( compare_type : CompareType < ' _ , ' _ > ) -> Self {
300
+ impl From < ExpectedTransformKind < ' _ , ' _ > > for TransfromKind {
301
+ fn from ( compare_type : ExpectedTransformKind < ' _ , ' _ > ) -> Self {
289
302
match compare_type {
290
- CompareType :: Same ( _) => TransfromType :: Same ,
291
- CompareType :: Eq ( _ , _ , _ ) => TransfromType :: Eq ,
292
- CompareType :: Discr { .. } => TransfromType :: Discr ,
303
+ ExpectedTransformKind :: Same ( _) => TransfromKind :: Same ,
304
+ ExpectedTransformKind :: SameByEq { .. } => TransfromKind :: Same ,
305
+ ExpectedTransformKind :: Cast { .. } => TransfromKind :: Cast ,
293
306
}
294
307
}
295
308
}
@@ -353,7 +366,7 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
353
366
return None ;
354
367
}
355
368
let mut target_iter = targets. iter ( ) ;
356
- let ( first_val , first_target) = target_iter. next ( ) . unwrap ( ) ;
369
+ let ( first_case_val , first_target) = target_iter. next ( ) . unwrap ( ) ;
357
370
let first_terminator_kind = & bbs[ first_target] . terminator ( ) . kind ;
358
371
// Check that destinations are identical, and if not, then don't optimize this block
359
372
if !targets
@@ -365,22 +378,18 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
365
378
366
379
let discr_size = tcx. layout_of ( param_env. and ( discr_ty) ) . unwrap ( ) . size ;
367
380
let first_stmts = & bbs[ first_target] . statements ;
368
- let ( second_val , second_target) = target_iter. next ( ) . unwrap ( ) ;
381
+ let ( second_case_val , second_target) = target_iter. next ( ) . unwrap ( ) ;
369
382
let second_stmts = & bbs[ second_target] . statements ;
370
383
if first_stmts. len ( ) != second_stmts. len ( ) {
371
384
return None ;
372
385
}
373
386
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
387
// We first compare the two branches, and then the other branches need to fulfill the same conditions.
379
- let mut compare_types = Vec :: new ( ) ;
388
+ let mut expected_transform_kinds = Vec :: new ( ) ;
380
389
for ( f, s) in iter:: zip ( first_stmts, second_stmts) {
381
390
let compare_type = match ( & f. kind , & s. kind ) {
382
391
// If two statements are exactly the same, we can optimize.
383
- ( f_s, s_s) if f_s == s_s => CompareType :: Same ( f_s) ,
392
+ ( f_s, s_s) if f_s == s_s => ExpectedTransformKind :: Same ( f_s) ,
384
393
385
394
// If two statements are assignments with the match values to the same place, we can optimize.
386
395
(
@@ -394,22 +403,27 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
394
403
f_c. const_ . try_eval_scalar_int ( tcx, param_env) ,
395
404
s_c. const_ . try_eval_scalar_int ( tcx, param_env) ,
396
405
) {
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.
406
+ ( Some ( f) , Some ( s) ) if f == s => ExpectedTransformKind :: SameByEq {
407
+ place : lhs_f,
408
+ ty : f_c. const_ . ty ( ) ,
409
+ scalar : f,
410
+ } ,
411
+ // Enum variants can also be simplified to an assignment statement,
412
+ // if we can use `IntToInt` cast to get an equal value.
400
413
( 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 ( ) ) ) =>
414
+ if ( can_cast (
415
+ first_case_val,
416
+ discr_size,
417
+ f,
418
+ f_c. const_ . ty ( ) . is_signed ( ) ,
419
+ ) && can_cast (
420
+ second_case_val,
421
+ discr_size,
422
+ s,
423
+ s_c. const_ . ty ( ) . is_signed ( ) ,
424
+ ) ) =>
407
425
{
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
- }
426
+ ExpectedTransformKind :: Cast { place : lhs_f, ty : f_c. const_ . ty ( ) }
413
427
}
414
428
_ => {
415
429
return None ;
@@ -420,47 +434,36 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
420
434
// Otherwise we cannot optimize. Try another block.
421
435
_ => return None ,
422
436
} ;
423
- compare_types . push ( compare_type) ;
437
+ expected_transform_kinds . push ( compare_type) ;
424
438
}
425
439
426
440
// All remaining BBs need to fulfill the same pattern as the two BBs from the previous step.
427
441
for ( other_val, other_target) in target_iter {
428
442
let other_stmts = & bbs[ other_target] . statements ;
429
- if compare_types . len ( ) != other_stmts. len ( ) {
443
+ if expected_transform_kinds . len ( ) != other_stmts. len ( ) {
430
444
return None ;
431
445
}
432
- for ( f, s) in iter:: zip ( & compare_types , other_stmts) {
446
+ for ( f, s) in iter:: zip ( & expected_transform_kinds , other_stmts) {
433
447
match ( * f, & s. kind ) {
434
- ( CompareType :: Same ( f_s) , s_s) if f_s == s_s => { }
448
+ ( ExpectedTransformKind :: Same ( f_s) , s_s) if f_s == s_s => { }
435
449
(
436
- CompareType :: Eq ( lhs_f, f_ty, val ) ,
450
+ ExpectedTransformKind :: SameByEq { place : lhs_f, ty : f_ty, scalar } ,
437
451
StatementKind :: Assign ( box ( lhs_s, Rvalue :: Use ( Operand :: Constant ( s_c) ) ) ) ,
438
452
) if lhs_f == lhs_s
439
453
&& s_c. const_ . ty ( ) == f_ty
440
- && s_c. const_ . try_eval_scalar_int ( tcx, param_env) == Some ( val ) => { }
454
+ && s_c. const_ . try_eval_scalar_int ( tcx, param_env) == Some ( scalar ) => { }
441
455
(
442
- CompareType :: Discr { place : lhs_f, ty : f_ty, is_signed } ,
456
+ ExpectedTransformKind :: Cast { place : lhs_f, ty : f_ty } ,
443
457
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
- }
458
+ ) if let Some ( f) = s_c. const_ . try_eval_scalar_int ( tcx, param_env)
459
+ && lhs_f == lhs_s
460
+ && s_c. const_ . ty ( ) == f_ty
461
+ && can_cast ( other_val, discr_size, f, f_ty. is_signed ( ) ) => { }
459
462
_ => return None ,
460
463
}
461
464
}
462
465
}
463
- self . transfrom_types = compare_types . into_iter ( ) . map ( |c| c. into ( ) ) . collect ( ) ;
466
+ self . transfrom_kinds = expected_transform_kinds . into_iter ( ) . map ( |c| c. into ( ) ) . collect ( ) ;
464
467
Some ( ( ) )
465
468
}
466
469
@@ -478,13 +481,13 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
478
481
let ( _, first) = targets. iter ( ) . next ( ) . unwrap ( ) ;
479
482
let first = & bbs[ first] ;
480
483
481
- for ( t, s) in iter:: zip ( & self . transfrom_types , & first. statements ) {
484
+ for ( t, s) in iter:: zip ( & self . transfrom_kinds , & first. statements ) {
482
485
match ( t, & s. kind ) {
483
- ( TransfromType :: Same , _ ) | ( TransfromType :: Eq , _) => {
486
+ ( TransfromKind :: Same , _) => {
484
487
patch. add_statement ( parent_end, s. kind . clone ( ) ) ;
485
488
}
486
489
(
487
- TransfromType :: Discr ,
490
+ TransfromKind :: Cast ,
488
491
StatementKind :: Assign ( box ( lhs, Rvalue :: Use ( Operand :: Constant ( f_c) ) ) ) ,
489
492
) => {
490
493
let operand = Operand :: Copy ( Place :: from ( discr_local) ) ;
0 commit comments