1
1
//! Performs various peephole optimizations.
2
2
3
3
use crate :: simplify:: simplify_duplicate_switch_targets;
4
+ use rustc_ast:: attr;
4
5
use rustc_middle:: mir:: * ;
5
6
use rustc_middle:: ty:: layout;
6
7
use rustc_middle:: ty:: layout:: ValidityRequirement ;
7
8
use rustc_middle:: ty:: { self , GenericArgsRef , ParamEnv , Ty , TyCtxt } ;
9
+ use rustc_span:: sym;
8
10
use rustc_span:: symbol:: Symbol ;
9
11
use rustc_target:: abi:: FieldIdx ;
10
12
use rustc_target:: spec:: abi:: Abi ;
@@ -22,10 +24,15 @@ impl<'tcx> MirPass<'tcx> for InstSimplify {
22
24
local_decls : & body. local_decls ,
23
25
param_env : tcx. param_env_reveal_all_normalized ( body. source . def_id ( ) ) ,
24
26
} ;
27
+ let preserve_ub_checks =
28
+ attr:: contains_name ( tcx. hir ( ) . krate_attrs ( ) , sym:: rustc_preserve_ub_checks) ;
25
29
for block in body. basic_blocks . as_mut ( ) {
26
30
for statement in block. statements . iter_mut ( ) {
27
31
match statement. kind {
28
32
StatementKind :: Assign ( box ( _place, ref mut rvalue) ) => {
33
+ if !preserve_ub_checks {
34
+ ctx. simplify_ub_check ( & statement. source_info , rvalue) ;
35
+ }
29
36
ctx. simplify_bool_cmp ( & statement. source_info , rvalue) ;
30
37
ctx. simplify_ref_deref ( & statement. source_info , rvalue) ;
31
38
ctx. simplify_len ( & statement. source_info , rvalue) ;
@@ -140,6 +147,14 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
140
147
}
141
148
}
142
149
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
+
143
158
fn simplify_cast ( & self , rvalue : & mut Rvalue < ' tcx > ) {
144
159
if let Rvalue :: Cast ( kind, operand, cast_ty) = rvalue {
145
160
let operand_ty = operand. ty ( self . local_decls , self . tcx ) ;
0 commit comments