rustc_mir_transform/
remove_noop_landing_pads.rs
1use 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 if let UnwindAction::Cleanup(unwind_bb) = *unwind {
53 if nop_landing_pads.contains(unwind_bb) {
54 debug!(" removing noop landing pad");
55 landing_pads_removed += 1;
56 *unwind = UnwindAction::Continue;
57 }
58 }
59 }
60
61 for target in body[bb].terminator_mut().successors_mut() {
62 if *target != resume_block && nop_landing_pads.contains(*target) {
63 debug!(" folding noop jump to {:?} to resume block", target);
64 *target = resume_block;
65 jumps_folded += 1;
66 }
67 }
68
69 let is_nop_landing_pad = self.is_nop_landing_pad(bb, body, &nop_landing_pads);
70 if is_nop_landing_pad {
71 nop_landing_pads.insert(bb);
72 }
73 debug!(" is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
74 }
75
76 debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);
77 }
78
79 fn is_required(&self) -> bool {
80 true
81 }
82}
83
84impl RemoveNoopLandingPads {
85 fn is_nop_landing_pad(
86 &self,
87 bb: BasicBlock,
88 body: &Body<'_>,
89 nop_landing_pads: &DenseBitSet<BasicBlock>,
90 ) -> bool {
91 for stmt in &body[bb].statements {
92 match &stmt.kind {
93 StatementKind::FakeRead(..)
94 | StatementKind::StorageLive(_)
95 | StatementKind::StorageDead(_)
96 | StatementKind::PlaceMention(..)
97 | StatementKind::AscribeUserType(..)
98 | StatementKind::Coverage(..)
99 | StatementKind::ConstEvalCounter
100 | StatementKind::BackwardIncompatibleDropHint { .. }
101 | StatementKind::Nop => {
102 }
104
105 StatementKind::Assign(box (place, Rvalue::Use(_) | Rvalue::Discriminant(_))) => {
106 if place.as_local().is_some() {
107 } else {
110 return false;
111 }
112 }
113
114 StatementKind::Assign { .. }
115 | StatementKind::SetDiscriminant { .. }
116 | StatementKind::Deinit(..)
117 | StatementKind::Intrinsic(..)
118 | StatementKind::Retag { .. } => {
119 return false;
120 }
121 }
122 }
123
124 let terminator = body[bb].terminator();
125 match terminator.kind {
126 TerminatorKind::Goto { .. }
127 | TerminatorKind::UnwindResume
128 | TerminatorKind::SwitchInt { .. }
129 | TerminatorKind::FalseEdge { .. }
130 | TerminatorKind::FalseUnwind { .. } => {
131 terminator.successors().all(|succ| nop_landing_pads.contains(succ))
132 }
133 TerminatorKind::CoroutineDrop
134 | TerminatorKind::Yield { .. }
135 | TerminatorKind::Return
136 | TerminatorKind::UnwindTerminate(_)
137 | TerminatorKind::Unreachable
138 | TerminatorKind::Call { .. }
139 | TerminatorKind::TailCall { .. }
140 | TerminatorKind::Assert { .. }
141 | TerminatorKind::Drop { .. }
142 | TerminatorKind::InlineAsm { .. } => false,
143 }
144 }
145}