@@ -5,6 +5,7 @@ use tracing::trace;
55use  crate :: patch:: MirPatch ; 
66
77pub ( super )  enum  SimplifyConstCondition  { 
8+     AfterInstSimplify , 
89    AfterConstProp , 
910    Final , 
1011} 
@@ -13,6 +14,9 @@ pub(super) enum SimplifyConstCondition {
1314impl < ' tcx >  crate :: MirPass < ' tcx >  for  SimplifyConstCondition  { 
1415    fn  name ( & self )  -> & ' static  str  { 
1516        match  self  { 
17+             SimplifyConstCondition :: AfterInstSimplify  => { 
18+                 "SimplifyConstCondition-after-inst-simplify" 
19+             } 
1620            SimplifyConstCondition :: AfterConstProp  => "SimplifyConstCondition-after-const-prop" , 
1721            SimplifyConstCondition :: Final  => "SimplifyConstCondition-final" , 
1822        } 
@@ -24,19 +28,39 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyConstCondition {
2428        let  mut  patch = MirPatch :: new ( body) ; 
2529
2630        ' blocks:  for  ( bb,  block)  in  body. basic_blocks . iter_enumerated ( )  { 
31+             let  mut  pre_local_const:  Option < ( Local ,  & ' _  ConstOperand < ' _ > ) >  = None ; 
32+ 
2733            for  ( statement_index,  stmt)  in  block. statements . iter ( ) . enumerate ( )  { 
34+                 let  has_local_const = pre_local_const. take ( ) ; 
2835                // Simplify `assume` of a known value: either a NOP or unreachable. 
2936                if  let  StatementKind :: Intrinsic ( box ref  intrinsic)  = stmt. kind 
3037                    && let  NonDivergingIntrinsic :: Assume ( discr)  = intrinsic
31-                     && let  Operand :: Constant ( c)  = discr
32-                     && let  Some ( constant)  = c. const_ . try_eval_bool ( tcx,  typing_env) 
3338                { 
39+                     let  c = if  let  Operand :: Constant ( c)  = discr { 
40+                         c
41+                     }  else  if  let  Some ( ( local,  c) )  = has_local_const
42+                         && let  Some ( assume_local)  = discr. place ( ) . and_then ( |p| p. as_local ( ) ) 
43+                         && local == assume_local
44+                     { 
45+                         c
46+                     }  else  { 
47+                         continue ; 
48+                     } ; 
49+                     let  Some ( constant)  = c. const_ . try_eval_bool ( tcx,  typing_env)  else  { 
50+                         continue ; 
51+                     } ; 
3452                    if  constant { 
3553                        patch. nop_statement ( Location  {  block :  bb,  statement_index } ) ; 
3654                    }  else  { 
3755                        patch. patch_terminator ( bb,  TerminatorKind :: Unreachable ) ; 
3856                        continue  ' blocks; 
3957                    } 
58+                 }  else  if  let  StatementKind :: Assign ( box ( ref  lhs,  ref  rvalue) )  = stmt. kind 
59+                     && let  Some ( local)  = lhs. as_local ( ) 
60+                     && let  Rvalue :: Use ( Operand :: Constant ( c) )  = rvalue
61+                     && c. const_ . ty ( ) . is_bool ( ) 
62+                 { 
63+                     pre_local_const = Some ( ( local,  c) ) ; 
4064                } 
4165            } 
4266
0 commit comments