@@ -30,26 +30,30 @@ impl<'l, 'tcx> MirPass<'tcx> for SimplifyBranches<'l> {
30
30
for block in mir. basic_blocks_mut ( ) {
31
31
let terminator = block. terminator_mut ( ) ;
32
32
terminator. kind = match terminator. kind {
33
- // TerminatorKind::If { ref targets, cond: Operand::Constant(Constant {
34
- // literal: Literal::Value {
35
- // value: ConstVal::Bool(cond)
36
- // }, ..
37
- // }) } => {
38
- // if cond {
39
- // TerminatorKind::Goto { target: targets.0 }
40
- // } else {
41
- // TerminatorKind::Goto { target: targets.1 }
42
- // }
43
- // }
44
-
33
+ TerminatorKind :: SwitchInt { discr : Operand :: Constant ( Constant {
34
+ literal : Literal :: Value { ref value } , ..
35
+ } ) , ref values, ref targets, .. } => {
36
+ if let Some ( ref constint) = value. to_const_int ( ) {
37
+ let ( otherwise, targets) = targets. split_last ( ) . unwrap ( ) ;
38
+ let mut ret = TerminatorKind :: Goto { target : * otherwise } ;
39
+ for ( v, t) in values. iter ( ) . zip ( targets. iter ( ) ) {
40
+ if v == constint {
41
+ ret = TerminatorKind :: Goto { target : * t } ;
42
+ break ;
43
+ }
44
+ }
45
+ ret
46
+ } else {
47
+ continue
48
+ }
49
+ } ,
45
50
TerminatorKind :: Assert { target, cond : Operand :: Constant ( Constant {
46
51
literal : Literal :: Value {
47
52
value : ConstVal :: Bool ( cond)
48
53
} , ..
49
54
} ) , expected, .. } if cond == expected => {
50
55
TerminatorKind :: Goto { target : target }
51
- }
52
-
56
+ } ,
53
57
_ => continue
54
58
} ;
55
59
}
0 commit comments