Skip to content

Commit cf341ea

Browse files
committed
Refactor remove_noop_landing_pads in two loops.
1 parent 99b9a88 commit cf341ea

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_index::bit_set::DenseBitSet;
22
use rustc_middle::mir::*;
33
use rustc_middle::ty::TyCtxt;
4-
use tracing::debug;
4+
use tracing::{debug, instrument};
55

66
use crate::patch::MirPatch;
77

@@ -15,6 +15,7 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
1515
sess.panic_strategy().unwinds()
1616
}
1717

18+
#[instrument(level = "debug", skip(self, _tcx, body))]
1819
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1920
let def_id = body.source.def_id();
2021
debug!(?def_id);
@@ -25,7 +26,7 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
2526
.iter_enumerated()
2627
.any(|(_bb, block)| matches!(block.terminator().kind, TerminatorKind::UnwindResume));
2728
if !has_resume {
28-
debug!("remove_noop_landing_pads: no resume block in MIR");
29+
debug!("no resume block in MIR");
2930
return;
3031
}
3132

@@ -36,42 +37,44 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
3637
patch.apply(body);
3738
resume_block
3839
};
39-
debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
40+
debug!(?resume_block);
4041

41-
let mut jumps_folded = 0;
42-
let mut landing_pads_removed = 0;
4342
let mut nop_landing_pads = DenseBitSet::new_empty(body.basic_blocks.len());
4443

4544
// This is a post-order traversal, so that if A post-dominates B
4645
// 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()
5164
&& let UnwindAction::Cleanup(unwind_bb) = *unwind
5265
&& nop_landing_pads.contains(unwind_bb)
5366
{
5467
debug!(" removing noop landing pad");
55-
landing_pads_removed += 1;
5668
*unwind = UnwindAction::Continue;
5769
}
5870

59-
body[bb].terminator_mut().successors_mut(|target| {
71+
bbdata.terminator_mut().successors_mut(|target| {
6072
if *target != resume_block && nop_landing_pads.contains(*target) {
6173
debug!(" folding noop jump to {:?} to resume block", target);
6274
*target = resume_block;
63-
jumps_folded += 1;
6475
}
6576
});
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);
7277
}
73-
74-
debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);
7578
}
7679

7780
fn is_required(&self) -> bool {

0 commit comments

Comments
 (0)