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