1
1
use rustc_index:: bit_set:: DenseBitSet ;
2
2
use rustc_middle:: mir:: * ;
3
3
use rustc_middle:: ty:: TyCtxt ;
4
- use tracing:: debug;
4
+ use tracing:: { debug, instrument } ;
5
5
6
6
use crate :: patch:: MirPatch ;
7
7
@@ -15,6 +15,7 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
15
15
sess. panic_strategy ( ) . unwinds ( )
16
16
}
17
17
18
+ #[ instrument( level = "debug" , skip( self , _tcx, body) ) ]
18
19
fn run_pass ( & self , _tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
19
20
let def_id = body. source . def_id ( ) ;
20
21
debug ! ( ?def_id) ;
@@ -25,7 +26,24 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
25
26
. iter_enumerated ( )
26
27
. any ( |( _bb, block) | matches ! ( block. terminator( ) . kind, TerminatorKind :: UnwindResume ) ) ;
27
28
if !has_resume {
28
- debug ! ( "remove_noop_landing_pads: no resume block in MIR" ) ;
29
+ debug ! ( "no resume block in MIR" ) ;
30
+ return ;
31
+ }
32
+
33
+ let mut nop_landing_pads = DenseBitSet :: new_empty ( body. basic_blocks . len ( ) ) ;
34
+
35
+ // This is a post-order traversal, so that if A post-dominates B
36
+ // then A will be visited before B.
37
+ for ( bb, bbdata) in traversal:: postorder ( body) {
38
+ let is_nop_landing_pad = self . is_nop_landing_pad ( bbdata, & nop_landing_pads) ;
39
+ debug ! ( "is_nop_landing_pad({bb:?}) = {is_nop_landing_pad}" ) ;
40
+ if is_nop_landing_pad {
41
+ nop_landing_pads. insert ( bb) ;
42
+ }
43
+ }
44
+
45
+ if nop_landing_pads. is_empty ( ) {
46
+ debug ! ( "no nop landing pads in MIR" ) ;
29
47
return ;
30
48
}
31
49
@@ -36,42 +54,27 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
36
54
patch. apply ( body) ;
37
55
resume_block
38
56
} ;
39
- debug ! ( "remove_noop_landing_pads: resume block is {:?}" , resume_block) ;
57
+ debug ! ( ? resume_block) ;
40
58
41
- let mut jumps_folded = 0 ;
42
- let mut landing_pads_removed = 0 ;
43
- let mut nop_landing_pads = DenseBitSet :: new_empty ( body . basic_blocks . len ( ) ) ;
59
+ let basic_blocks = body . basic_blocks . as_mut ( ) ;
60
+ for ( bb , bbdata ) in basic_blocks . iter_enumerated_mut ( ) {
61
+ debug ! ( "processing {:?}" , bb ) ;
44
62
45
- // This is a post-order traversal, so that if A post-dominates B
46
- // then A will be visited before B.
47
- let postorder: Vec < _ > = traversal:: postorder ( body) . map ( |( bb, _) | bb) . collect ( ) ;
48
- for bb in postorder {
49
- debug ! ( " processing {:?}" , bb) ;
50
- if let Some ( unwind) = body[ bb] . terminator_mut ( ) . unwind_mut ( )
63
+ if let Some ( unwind) = bbdata. terminator_mut ( ) . unwind_mut ( )
51
64
&& let UnwindAction :: Cleanup ( unwind_bb) = * unwind
52
65
&& nop_landing_pads. contains ( unwind_bb)
53
66
{
54
67
debug ! ( " removing noop landing pad" ) ;
55
- landing_pads_removed += 1 ;
56
68
* unwind = UnwindAction :: Continue ;
57
69
}
58
70
59
- body [ bb ] . terminator_mut ( ) . successors_mut ( |target| {
71
+ bbdata . terminator_mut ( ) . successors_mut ( |target| {
60
72
if * target != resume_block && nop_landing_pads. contains ( * target) {
61
73
debug ! ( " folding noop jump to {:?} to resume block" , target) ;
62
74
* target = resume_block;
63
- jumps_folded += 1 ;
64
75
}
65
76
} ) ;
66
-
67
- let is_nop_landing_pad = self . is_nop_landing_pad ( bb, body, & nop_landing_pads) ;
68
- if is_nop_landing_pad {
69
- nop_landing_pads. insert ( bb) ;
70
- }
71
- debug ! ( " is_nop_landing_pad({:?}) = {}" , bb, is_nop_landing_pad) ;
72
77
}
73
-
74
- debug ! ( "removed {:?} jumps and {:?} landing pads" , jumps_folded, landing_pads_removed) ;
75
78
}
76
79
77
80
fn is_required ( & self ) -> bool {
@@ -82,11 +85,10 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
82
85
impl RemoveNoopLandingPads {
83
86
fn is_nop_landing_pad (
84
87
& self ,
85
- bb : BasicBlock ,
86
- body : & Body < ' _ > ,
88
+ bbdata : & BasicBlockData < ' _ > ,
87
89
nop_landing_pads : & DenseBitSet < BasicBlock > ,
88
90
) -> bool {
89
- for stmt in & body [ bb ] . statements {
91
+ for stmt in & bbdata . statements {
90
92
match & stmt. kind {
91
93
StatementKind :: FakeRead ( ..)
92
94
| StatementKind :: StorageLive ( _)
@@ -119,7 +121,7 @@ impl RemoveNoopLandingPads {
119
121
}
120
122
}
121
123
122
- let terminator = body [ bb ] . terminator ( ) ;
124
+ let terminator = bbdata . terminator ( ) ;
123
125
match terminator. kind {
124
126
TerminatorKind :: Goto { .. }
125
127
| TerminatorKind :: UnwindResume
0 commit comments