@@ -619,8 +619,19 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
619619 }
620620 }
621621
622- fn should_const_prop ( & self ) -> bool {
623- self . tcx . sess . opts . debugging_opts . mir_opt_level >= 2
622+ fn should_const_prop ( & mut self , op : OpTy < ' tcx > ) -> bool {
623+ if self . tcx . sess . opts . debugging_opts . mir_opt_level >= 2 {
624+ return true ;
625+ }
626+
627+ match * op {
628+ interpret:: Operand :: Immediate ( Immediate :: Scalar ( ScalarMaybeUndef :: Scalar ( s) ) ) =>
629+ s. is_bits ( ) ,
630+ interpret:: Operand :: Immediate ( Immediate :: ScalarPair ( ScalarMaybeUndef :: Scalar ( l) ,
631+ ScalarMaybeUndef :: Scalar ( r) ) ) =>
632+ l. is_bits ( ) && r. is_bits ( ) ,
633+ _ => false
634+ }
624635 }
625636}
626637
@@ -719,15 +730,15 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
719730 if self . can_const_prop [ local] {
720731 trace ! ( "propagated into {:?}" , local) ;
721732
722- if self . should_const_prop ( ) {
723- let value =
724- self . get_const ( local ) . expect ( "local was dead/uninitialized" ) ;
725- trace ! ( "replacing {:?} with {:?}" , rval , value ) ;
726- self . replace_with_const (
727- rval ,
728- value ,
729- statement . source_info ,
730- ) ;
733+ if let Some ( value ) = self . get_const ( local ) {
734+ if self . should_const_prop ( value) {
735+ trace ! ( "replacing {:?} with {:?}" , rval , value ) ;
736+ self . replace_with_const (
737+ rval ,
738+ value ,
739+ statement . source_info ,
740+ ) ;
741+ }
731742 }
732743 } else {
733744 trace ! ( "can't propagate into {:?}" , local) ;
@@ -827,7 +838,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
827838 & msg,
828839 ) ;
829840 } else {
830- if self . should_const_prop ( ) {
841+ if self . should_const_prop ( value ) {
831842 if let ScalarMaybeUndef :: Scalar ( scalar) = value_const {
832843 * cond = self . operand_from_scalar (
833844 scalar,
@@ -840,8 +851,8 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
840851 }
841852 } ,
842853 TerminatorKind :: SwitchInt { ref mut discr, switch_ty, .. } => {
843- if self . should_const_prop ( ) {
844- if let Some ( value ) = self . eval_operand ( & discr , source_info ) {
854+ if let Some ( value ) = self . eval_operand ( & discr , source_info ) {
855+ if self . should_const_prop ( value ) {
845856 if let ScalarMaybeUndef :: Scalar ( scalar) =
846857 self . ecx . read_scalar ( value) . unwrap ( ) {
847858 * discr = self . operand_from_scalar ( scalar, switch_ty, source_info. span ) ;
0 commit comments