2 use rustc_index::bit_set::BitSet;
3 use rustc_middle::mir::patch::MirPatch;
4 use rustc_middle::mir::*;
5 use rustc_middle::ty::TyCtxt;
6 use rustc_target::spec::PanicStrategy;
8 /// A pass that removes noop landing pads and replaces jumps to them with
9 /// `None`. This is important because otherwise LLVM generates terrible
11 pub struct RemoveNoopLandingPads;
13 impl<'tcx> MirPass<'tcx> for RemoveNoopLandingPads {
14 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
15 sess.panic_strategy() != PanicStrategy::Abort
18 fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
19 debug!("remove_noop_landing_pads({:?})", body);
20 self.remove_nop_landing_pads(body)
24 impl RemoveNoopLandingPads {
25 fn is_nop_landing_pad(
29 nop_landing_pads: &BitSet<BasicBlock>,
31 for stmt in &body[bb].statements {
33 StatementKind::FakeRead(..)
34 | StatementKind::StorageLive(_)
35 | StatementKind::StorageDead(_)
36 | StatementKind::AscribeUserType(..)
37 | StatementKind::Coverage(..)
38 | StatementKind::Nop => {
39 // These are all noops in a landing pad
42 StatementKind::Assign(box (place, Rvalue::Use(_) | Rvalue::Discriminant(_))) => {
43 if place.as_local().is_some() {
44 // Writing to a local (e.g., a drop flag) does not
45 // turn a landing pad to a non-nop
51 StatementKind::Assign { .. }
52 | StatementKind::SetDiscriminant { .. }
53 | StatementKind::Deinit(..)
54 | StatementKind::Intrinsic(..)
55 | StatementKind::Retag { .. } => {
61 let terminator = body[bb].terminator();
62 match terminator.kind {
63 TerminatorKind::Goto { .. }
64 | TerminatorKind::Resume
65 | TerminatorKind::SwitchInt { .. }
66 | TerminatorKind::FalseEdge { .. }
67 | TerminatorKind::FalseUnwind { .. } => {
68 terminator.successors().all(|succ| nop_landing_pads.contains(succ))
70 TerminatorKind::GeneratorDrop
71 | TerminatorKind::Yield { .. }
72 | TerminatorKind::Return
73 | TerminatorKind::Abort
74 | TerminatorKind::Unreachable
75 | TerminatorKind::Call { .. }
76 | TerminatorKind::Assert { .. }
77 | TerminatorKind::DropAndReplace { .. }
78 | TerminatorKind::Drop { .. }
79 | TerminatorKind::InlineAsm { .. } => false,
83 fn remove_nop_landing_pads(&self, body: &mut Body<'_>) {
84 debug!("body: {:#?}", body);
86 // make sure there's a resume block
88 let mut patch = MirPatch::new(body);
89 let resume_block = patch.resume_block();
93 debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
95 let mut jumps_folded = 0;
96 let mut landing_pads_removed = 0;
97 let mut nop_landing_pads = BitSet::new_empty(body.basic_blocks.len());
99 // This is a post-order traversal, so that if A post-dominates B
100 // then A will be visited before B.
101 let postorder: Vec<_> = traversal::postorder(body).map(|(bb, _)| bb).collect();
102 for bb in postorder {
103 debug!(" processing {:?}", bb);
104 if let Some(unwind) = body[bb].terminator_mut().unwind_mut() {
105 if let Some(unwind_bb) = *unwind {
106 if nop_landing_pads.contains(unwind_bb) {
107 debug!(" removing noop landing pad");
108 landing_pads_removed += 1;
114 for target in body[bb].terminator_mut().successors_mut() {
115 if *target != resume_block && nop_landing_pads.contains(*target) {
116 debug!(" folding noop jump to {:?} to resume block", target);
117 *target = resume_block;
122 let is_nop_landing_pad = self.is_nop_landing_pad(bb, body, &nop_landing_pads);
123 if is_nop_landing_pad {
124 nop_landing_pads.insert(bb);
126 debug!(" is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
129 debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);