rustc_mir_transform/
remove_noop_landing_pads.rs1use rustc_index::bit_set::DenseBitSet;
2use rustc_middle::mir::*;
3use rustc_middle::ty::TyCtxt;
4use rustc_target::spec::PanicStrategy;
5use tracing::debug;
6
7use crate::patch::MirPatch;
8
9pub(super) struct RemoveNoopLandingPads;
13
14impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
15 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
16 sess.panic_strategy() != PanicStrategy::Abort
17 }
18
19 fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
20 let def_id = body.source.def_id();
21 debug!(?def_id);
22
23 let has_resume = body
25 .basic_blocks
26 .iter_enumerated()
27 .any(|(_bb, block)| matches!(block.terminator().kind, TerminatorKind::UnwindResume));
28 if !has_resume {
29 debug!("remove_noop_landing_pads: no resume block in MIR");
30 return;
31 }
32
33 let resume_block = {
35 let mut patch = MirPatch::new(body);
36 let resume_block = patch.resume_block();
37 patch.apply(body);
38 resume_block
39 };
40 debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
41
42 let mut jumps_folded = 0;
43 let mut landing_pads_removed = 0;
44 let mut nop_landing_pads = DenseBitSet::new_empty(body.basic_blocks.len());
45
46 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()
52 && let UnwindAction::Cleanup(unwind_bb) = *unwind
53 && nop_landing_pads.contains(unwind_bb)
54 {
55 debug!(" removing noop landing pad");
56 landing_pads_removed += 1;
57 *unwind = UnwindAction::Continue;
58 }
59
60 body[bb].terminator_mut().successors_mut(|target| {
61 if *target != resume_block && nop_landing_pads.contains(*target) {
62 debug!(" folding noop jump to {:?} to resume block", target);
63 *target = resume_block;
64 jumps_folded += 1;
65 }
66 });
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);
73 }
74
75 debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);
76 }
77
78 fn is_required(&self) -> bool {
79 true
80 }
81}
82
83impl RemoveNoopLandingPads {
84 fn is_nop_landing_pad(
85 &self,
86 bb: BasicBlock,
87 body: &Body<'_>,
88 nop_landing_pads: &DenseBitSet<BasicBlock>,
89 ) -> bool {
90 for stmt in &body[bb].statements {
91 match &stmt.kind {
92 StatementKind::FakeRead(..)
93 | StatementKind::StorageLive(_)
94 | StatementKind::StorageDead(_)
95 | StatementKind::PlaceMention(..)
96 | StatementKind::AscribeUserType(..)
97 | StatementKind::Coverage(..)
98 | StatementKind::ConstEvalCounter
99 | StatementKind::BackwardIncompatibleDropHint { .. }
100 | StatementKind::Nop => {
101 }
103
104 StatementKind::Assign(box (place, Rvalue::Use(_) | Rvalue::Discriminant(_))) => {
105 if place.as_local().is_some() {
106 } else {
109 return false;
110 }
111 }
112
113 StatementKind::Assign { .. }
114 | StatementKind::SetDiscriminant { .. }
115 | StatementKind::Deinit(..)
116 | StatementKind::Intrinsic(..)
117 | StatementKind::Retag { .. } => {
118 return false;
119 }
120 }
121 }
122
123 let terminator = body[bb].terminator();
124 match terminator.kind {
125 TerminatorKind::Goto { .. }
126 | TerminatorKind::UnwindResume
127 | TerminatorKind::SwitchInt { .. }
128 | TerminatorKind::FalseEdge { .. }
129 | TerminatorKind::FalseUnwind { .. } => {
130 terminator.successors().all(|succ| nop_landing_pads.contains(succ))
131 }
132 TerminatorKind::CoroutineDrop
133 | TerminatorKind::Yield { .. }
134 | TerminatorKind::Return
135 | TerminatorKind::UnwindTerminate(_)
136 | TerminatorKind::Unreachable
137 | TerminatorKind::Call { .. }
138 | TerminatorKind::TailCall { .. }
139 | TerminatorKind::Assert { .. }
140 | TerminatorKind::Drop { .. }
141 | TerminatorKind::InlineAsm { .. } => false,
142 }
143 }
144}