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,7 @@ 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" ) ;
29
30
return ;
30
31
}
31
32
@@ -36,42 +37,44 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
36
37
patch. apply ( body) ;
37
38
resume_block
38
39
} ;
39
- debug ! ( "remove_noop_landing_pads: resume block is {:?}" , resume_block) ;
40
+ debug ! ( ? resume_block) ;
40
41
41
- let mut jumps_folded = 0 ;
42
- let mut landing_pads_removed = 0 ;
43
42
let mut nop_landing_pads = DenseBitSet :: new_empty ( body. basic_blocks . len ( ) ) ;
44
43
45
44
// This is a post-order traversal, so that if A post-dominates B
46
45
// 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 ( )
46
+ for & bb in body. basic_blocks . reverse_postorder ( ) . iter ( ) . rev ( ) {
47
+ let is_nop_landing_pad = self . is_nop_landing_pad ( bb, body, & nop_landing_pads) ;
48
+ debug ! ( "is_nop_landing_pad({bb:?}) = {is_nop_landing_pad}" ) ;
49
+ if is_nop_landing_pad {
50
+ nop_landing_pads. insert ( bb) ;
51
+ }
52
+ }
53
+
54
+ if nop_landing_pads. is_empty ( ) {
55
+ debug ! ( "no nop landing pads in MIR" ) ;
56
+ return ;
57
+ }
58
+
59
+ let basic_blocks = body. basic_blocks . as_mut ( ) ;
60
+ for ( bb, bbdata) in basic_blocks. iter_enumerated_mut ( ) {
61
+ debug ! ( "processing {:?}" , bb) ;
62
+
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 {
0 commit comments