1 use crate::transform::{MirPass, MirSource};
2 use crate::util::patch::MirPatch;
3 use rustc_index::bit_set::BitSet;
4 use rustc_middle::mir::*;
5 use rustc_middle::ty::TyCtxt;
7 /// A pass that removes noop landing pads and replaces jumps to them with
8 /// `None`. This is important because otherwise LLVM generates terrible
10 pub struct RemoveNoopLandingPads;
12 pub fn remove_noop_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut BodyAndCache<'tcx>) {
13 if tcx.sess.no_landing_pads() {
16 debug!("remove_noop_landing_pads({:?})", body);
18 RemoveNoopLandingPads.remove_nop_landing_pads(body)
21 impl<'tcx> MirPass<'tcx> for RemoveNoopLandingPads {
22 fn run_pass(&self, tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
23 remove_noop_landing_pads(tcx, body);
27 impl RemoveNoopLandingPads {
28 fn is_nop_landing_pad(
32 nop_landing_pads: &BitSet<BasicBlock>,
34 for stmt in &body[bb].statements {
36 StatementKind::FakeRead(..)
37 | StatementKind::StorageLive(_)
38 | StatementKind::StorageDead(_)
39 | StatementKind::AscribeUserType(..)
40 | StatementKind::Nop => {
41 // These are all nops in a landing pad
44 StatementKind::Assign(box (place, Rvalue::Use(_))) => {
45 if place.as_local().is_some() {
46 // Writing to a local (e.g., a drop flag) does not
47 // turn a landing pad to a non-nop
53 StatementKind::Assign { .. }
54 | StatementKind::SetDiscriminant { .. }
55 | StatementKind::LlvmInlineAsm { .. }
56 | StatementKind::Retag { .. } => {
62 let terminator = body[bb].terminator();
63 match terminator.kind {
64 TerminatorKind::Goto { .. }
65 | TerminatorKind::Resume
66 | TerminatorKind::SwitchInt { .. }
67 | TerminatorKind::FalseEdges { .. }
68 | TerminatorKind::FalseUnwind { .. } => {
69 terminator.successors().all(|&succ| nop_landing_pads.contains(succ))
71 TerminatorKind::GeneratorDrop
72 | TerminatorKind::Yield { .. }
73 | TerminatorKind::Return
74 | TerminatorKind::Abort
75 | TerminatorKind::Unreachable
76 | TerminatorKind::Call { .. }
77 | TerminatorKind::Assert { .. }
78 | TerminatorKind::DropAndReplace { .. }
79 | TerminatorKind::Drop { .. } => false,
83 fn remove_nop_landing_pads(&self, body: &mut BodyAndCache<'_>) {
84 // make sure there's a single resume block
86 let patch = MirPatch::new(body);
87 let resume_block = patch.resume_block();
91 debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
93 let mut jumps_folded = 0;
94 let mut landing_pads_removed = 0;
95 let mut nop_landing_pads = BitSet::new_empty(body.basic_blocks().len());
97 // This is a post-order traversal, so that if A post-dominates B
98 // then A will be visited before B.
99 let postorder: Vec<_> = traversal::postorder(body).map(|(bb, _)| bb).collect();
100 for bb in postorder {
101 debug!(" processing {:?}", bb);
102 for target in body[bb].terminator_mut().successors_mut() {
103 if *target != resume_block && nop_landing_pads.contains(*target) {
104 debug!(" folding noop jump to {:?} to resume block", target);
105 *target = resume_block;
110 if let Some(unwind) = body[bb].terminator_mut().unwind_mut() {
111 if *unwind == Some(resume_block) {
112 debug!(" removing noop landing pad");
114 landing_pads_removed += 1;
119 let is_nop_landing_pad = self.is_nop_landing_pad(bb, body, &nop_landing_pads);
120 if is_nop_landing_pad {
121 nop_landing_pads.insert(bb);
123 debug!(" is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
126 debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);