3 use rustc_data_structures::bit_set::BitSet;
4 use crate::transform::{MirPass, MirSource};
5 use crate::util::patch::MirPatch;
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<'a, 'tcx>(
13 tcx: TyCtxt<'a, 'tcx, 'tcx>,
14 body: &mut Body<'tcx>)
16 if tcx.sess.no_landing_pads() {
19 debug!("remove_noop_landing_pads({:?})", body);
21 RemoveNoopLandingPads.remove_nop_landing_pads(body)
24 impl MirPass for RemoveNoopLandingPads {
25 fn run_pass<'a, 'tcx>(&self,
26 tcx: TyCtxt<'a, 'tcx, 'tcx>,
27 _src: MirSource<'tcx>,
28 body: &mut Body<'tcx>) {
29 remove_noop_landing_pads(tcx, body);
33 impl RemoveNoopLandingPads {
34 fn is_nop_landing_pad(
38 nop_landing_pads: &BitSet<BasicBlock>,
40 for stmt in &body[bb].statements {
42 StatementKind::FakeRead(..) |
43 StatementKind::StorageLive(_) |
44 StatementKind::StorageDead(_) |
45 StatementKind::AscribeUserType(..) |
46 StatementKind::Nop => {
47 // These are all nops in a landing pad
50 StatementKind::Assign(Place::Base(PlaceBase::Local(_)), box Rvalue::Use(_)) => {
51 // Writing to a local (e.g., a drop flag) does not
52 // turn a landing pad to a non-nop
55 StatementKind::Assign { .. } |
56 StatementKind::SetDiscriminant { .. } |
57 StatementKind::InlineAsm { .. } |
58 StatementKind::Retag { .. } => {
64 let terminator = body[bb].terminator();
65 match terminator.kind {
66 TerminatorKind::Goto { .. } |
67 TerminatorKind::Resume |
68 TerminatorKind::SwitchInt { .. } |
69 TerminatorKind::FalseEdges { .. } |
70 TerminatorKind::FalseUnwind { .. } => {
71 terminator.successors().all(|&succ| {
72 nop_landing_pads.contains(succ)
75 TerminatorKind::GeneratorDrop |
76 TerminatorKind::Yield { .. } |
77 TerminatorKind::Return |
78 TerminatorKind::Abort |
79 TerminatorKind::Unreachable |
80 TerminatorKind::Call { .. } |
81 TerminatorKind::Assert { .. } |
82 TerminatorKind::DropAndReplace { .. } |
83 TerminatorKind::Drop { .. } => {
89 fn remove_nop_landing_pads(&self, body: &mut Body<'_>) {
90 // make sure there's a single resume block
92 let patch = MirPatch::new(body);
93 let resume_block = patch.resume_block();
97 debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
99 let mut jumps_folded = 0;
100 let mut landing_pads_removed = 0;
101 let mut nop_landing_pads = BitSet::new_empty(body.basic_blocks().len());
103 // This is a post-order traversal, so that if A post-dominates B
104 // then A will be visited before B.
105 let postorder: Vec<_> = traversal::postorder(body).map(|(bb, _)| bb).collect();
106 for bb in postorder {
107 debug!(" processing {:?}", bb);
108 for target in body[bb].terminator_mut().successors_mut() {
109 if *target != resume_block && nop_landing_pads.contains(*target) {
110 debug!(" folding noop jump to {:?} to resume block", target);
111 *target = resume_block;
116 match body[bb].terminator_mut().unwind_mut() {
118 if *unwind == Some(resume_block) {
119 debug!(" removing noop landing pad");
121 landing_pads_removed += 1;
128 let is_nop_landing_pad = self.is_nop_landing_pad(bb, body, &nop_landing_pads);
129 if is_nop_landing_pad {
130 nop_landing_pads.insert(bb);
132 debug!(" is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
135 debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);