1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::ty::TyCtxt;
13 use rustc_data_structures::bit_set::BitSet;
14 use transform::{MirPass, MirSource};
15 use util::patch::MirPatch;
17 /// A pass that removes no-op landing pads and replaces jumps to them with
18 /// `None`. This is important because otherwise LLVM generates terrible
20 pub struct RemoveNoopLandingPads;
22 pub fn remove_noop_landing_pads<'a, 'tcx>(
23 tcx: TyCtxt<'a, 'tcx, 'tcx>,
26 if tcx.sess.no_landing_pads() {
29 debug!("remove_noop_landing_pads({:?})", mir);
31 RemoveNoopLandingPads.remove_nop_landing_pads(mir)
34 impl MirPass for RemoveNoopLandingPads {
35 fn run_pass<'a, 'tcx>(&self,
36 tcx: TyCtxt<'a, 'tcx, 'tcx>,
38 mir: &mut Mir<'tcx>) {
39 remove_noop_landing_pads(tcx, mir);
43 impl RemoveNoopLandingPads {
44 fn is_nop_landing_pad(
48 nop_landing_pads: &BitSet<BasicBlock>,
50 for stmt in &mir[bb].statements {
52 StatementKind::FakeRead(..) |
53 StatementKind::StorageLive(_) |
54 StatementKind::StorageDead(_) |
55 StatementKind::AscribeUserType(..) |
56 StatementKind::Nop => {
57 // These are all nops in a landing pad
60 StatementKind::Assign(Place::Local(_), box Rvalue::Use(_)) => {
61 // Writing to a local (e.g. a drop flag) does not
62 // turn a landing pad to a non-nop
65 StatementKind::Assign { .. } |
66 StatementKind::SetDiscriminant { .. } |
67 StatementKind::InlineAsm { .. } |
68 StatementKind::Retag { .. } |
69 StatementKind::EscapeToRaw { .. } => {
75 let terminator = mir[bb].terminator();
76 match terminator.kind {
77 TerminatorKind::Goto { .. } |
78 TerminatorKind::Resume |
79 TerminatorKind::SwitchInt { .. } |
80 TerminatorKind::FalseEdges { .. } |
81 TerminatorKind::FalseUnwind { .. } => {
82 terminator.successors().all(|&succ| {
83 nop_landing_pads.contains(succ)
86 TerminatorKind::GeneratorDrop |
87 TerminatorKind::Yield { .. } |
88 TerminatorKind::Return |
89 TerminatorKind::Abort |
90 TerminatorKind::Unreachable |
91 TerminatorKind::Call { .. } |
92 TerminatorKind::Assert { .. } |
93 TerminatorKind::DropAndReplace { .. } |
94 TerminatorKind::Drop { .. } => {
100 fn remove_nop_landing_pads(&self, mir: &mut Mir) {
101 // make sure there's a single resume block
103 let patch = MirPatch::new(mir);
104 let resume_block = patch.resume_block();
108 debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
110 let mut jumps_folded = 0;
111 let mut landing_pads_removed = 0;
112 let mut nop_landing_pads = BitSet::new_empty(mir.basic_blocks().len());
114 // This is a post-order traversal, so that if A post-dominates B
115 // then A will be visited before B.
116 let postorder: Vec<_> = traversal::postorder(mir).map(|(bb, _)| bb).collect();
117 for bb in postorder {
118 debug!(" processing {:?}", bb);
119 for target in mir[bb].terminator_mut().successors_mut() {
120 if *target != resume_block && nop_landing_pads.contains(*target) {
121 debug!(" folding noop jump to {:?} to resume block", target);
122 *target = resume_block;
127 match mir[bb].terminator_mut().unwind_mut() {
129 if *unwind == Some(resume_block) {
130 debug!(" removing noop landing pad");
132 landing_pads_removed += 1;
139 let is_nop_landing_pad = self.is_nop_landing_pad(bb, mir, &nop_landing_pads);
140 if is_nop_landing_pad {
141 nop_landing_pads.insert(bb);
143 debug!(" is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
146 debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);