Skip to content

Commit 35adce2

Browse files
committed
Refactor remove_noop_landing_pads in two loops.
1 parent ce6daf3 commit 35adce2

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
@@ -2,7 +2,7 @@ use rustc_index::bit_set::DenseBitSet;
22
use rustc_middle::mir::*;
33
use rustc_middle::ty::TyCtxt;
44
use rustc_target::spec::PanicStrategy;
5-
use tracing::debug;
5+
use tracing::{debug, instrument};
66

77
use crate::patch::MirPatch;
88

@@ -16,6 +16,7 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
1616
sess.panic_strategy() != PanicStrategy::Abort
1717
}
1818

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

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

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

4645
// This is a post-order traversal, so that if A post-dominates B
4746
// then A will be visited before B.
48-
let postorder: Vec<_> = traversal::postorder(body).map(|(bb, _)| bb).collect();
49-
for bb in postorder {
50-
debug!(" processing {:?}", bb);
51-
if let Some(unwind) = body[bb].terminator_mut().unwind_mut()
47+
for &bb in body.basic_blocks.reverse_postorder().iter().rev() {
48+
let is_nop_landing_pad = self.is_nop_landing_pad(bb, body, &nop_landing_pads);
49+
debug!("is_nop_landing_pad({bb:?}) = {is_nop_landing_pad}");
50+
if is_nop_landing_pad {
51+
nop_landing_pads.insert(bb);
52+
}
53+
}
54+
55+
if nop_landing_pads.is_empty() {
56+
debug!("no nop landing pads in MIR");
57+
return;
58+
}
59+
60+
let basic_blocks = body.basic_blocks.as_mut();
61+
for (bb, bbdata) in basic_blocks.iter_enumerated_mut() {
62+
debug!("processing {:?}", bb);
63+
64+
if let Some(unwind) = bbdata.terminator_mut().unwind_mut()
5265
&& let UnwindAction::Cleanup(unwind_bb) = *unwind
5366
&& nop_landing_pads.contains(unwind_bb)
5467
{
5568
debug!(" removing noop landing pad");
56-
landing_pads_removed += 1;
5769
*unwind = UnwindAction::Continue;
5870
}
5971

60-
body[bb].terminator_mut().successors_mut(|target| {
72+
bbdata.terminator_mut().successors_mut(|target| {
6173
if *target != resume_block && nop_landing_pads.contains(*target) {
6274
debug!(" folding noop jump to {:?} to resume block", target);
6375
*target = resume_block;
64-
jumps_folded += 1;
6576
}
6677
});
67-
68-
let is_nop_landing_pad = self.is_nop_landing_pad(bb, body, &nop_landing_pads);
69-
if is_nop_landing_pad {
70-
nop_landing_pads.insert(bb);
71-
}
72-
debug!(" is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
7378
}
74-
75-
debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);
7679
}
7780

7881
fn is_required(&self) -> bool {

0 commit comments

Comments
 (0)