11//! Performs various peephole optimizations.
22
33use crate :: simplify:: simplify_duplicate_switch_targets;
4+ use rustc_ast:: attr;
45use rustc_middle:: mir:: * ;
56use rustc_middle:: ty:: layout;
67use rustc_middle:: ty:: layout:: ValidityRequirement ;
78use rustc_middle:: ty:: { self , GenericArgsRef , ParamEnv , Ty , TyCtxt } ;
9+ use rustc_span:: sym;
810use rustc_span:: symbol:: Symbol ;
911use rustc_target:: abi:: FieldIdx ;
1012use rustc_target:: spec:: abi:: Abi ;
@@ -22,10 +24,15 @@ impl<'tcx> MirPass<'tcx> for InstSimplify {
2224 local_decls : & body. local_decls ,
2325 param_env : tcx. param_env_reveal_all_normalized ( body. source . def_id ( ) ) ,
2426 } ;
27+ let preserve_ub_checks =
28+ attr:: contains_name ( tcx. hir ( ) . krate_attrs ( ) , sym:: rustc_preserve_ub_checks) ;
2529 for block in body. basic_blocks . as_mut ( ) {
2630 for statement in block. statements . iter_mut ( ) {
2731 match statement. kind {
2832 StatementKind :: Assign ( box ( _place, ref mut rvalue) ) => {
33+ if !preserve_ub_checks {
34+ ctx. simplify_ub_check ( & statement. source_info , rvalue) ;
35+ }
2936 ctx. simplify_bool_cmp ( & statement. source_info , rvalue) ;
3037 ctx. simplify_ref_deref ( & statement. source_info , rvalue) ;
3138 ctx. simplify_len ( & statement. source_info , rvalue) ;
@@ -140,6 +147,14 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
140147 }
141148 }
142149
150+ fn simplify_ub_check ( & self , source_info : & SourceInfo , rvalue : & mut Rvalue < ' tcx > ) {
151+ if let Rvalue :: NullaryOp ( NullOp :: UbChecks , _) = * rvalue {
152+ let const_ = Const :: from_bool ( self . tcx , self . tcx . sess . opts . debug_assertions ) ;
153+ let constant = ConstOperand { span : source_info. span , const_, user_ty : None } ;
154+ * rvalue = Rvalue :: Use ( Operand :: Constant ( Box :: new ( constant) ) ) ;
155+ }
156+ }
157+
143158 fn simplify_cast ( & self , rvalue : & mut Rvalue < ' tcx > ) {
144159 if let Rvalue :: Cast ( kind, operand, cast_ty) = rvalue {
145160 let operand_ty = operand. ty ( self . local_decls , self . tcx ) ;
0 commit comments