]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/remove_noop_landing_pads.rs
Rollup merge of #68424 - estebank:suggest-borrow-for-non-copy-vec, r=davidtwco
[rust.git] / src / librustc_mir / transform / remove_noop_landing_pads.rs
1 use crate::transform::{MirPass, MirSource};
2 use crate::util::patch::MirPatch;
3 use rustc::mir::*;
4 use rustc::ty::TyCtxt;
5 use rustc_index::bit_set::BitSet;
6
7 /// A pass that removes noop landing pads and replaces jumps to them with
8 /// `None`. This is important because otherwise LLVM generates terrible
9 /// code for these.
10 pub struct RemoveNoopLandingPads;
11
12 pub fn remove_noop_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut BodyAndCache<'tcx>) {
13     if tcx.sess.no_landing_pads() {
14         return;
15     }
16     debug!("remove_noop_landing_pads({:?})", body);
17
18     RemoveNoopLandingPads.remove_nop_landing_pads(body)
19 }
20
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);
24     }
25 }
26
27 impl RemoveNoopLandingPads {
28     fn is_nop_landing_pad(
29         &self,
30         bb: BasicBlock,
31         body: &Body<'_>,
32         nop_landing_pads: &BitSet<BasicBlock>,
33     ) -> bool {
34         for stmt in &body[bb].statements {
35             match &stmt.kind {
36                 StatementKind::FakeRead(..)
37                 | StatementKind::StorageLive(_)
38                 | StatementKind::StorageDead(_)
39                 | StatementKind::AscribeUserType(..)
40                 | StatementKind::Nop => {
41                     // These are all nops in a landing pad
42                 }
43
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
48                     } else {
49                         return false;
50                     }
51                 }
52
53                 StatementKind::Assign { .. }
54                 | StatementKind::SetDiscriminant { .. }
55                 | StatementKind::InlineAsm { .. }
56                 | StatementKind::Retag { .. } => {
57                     return false;
58                 }
59             }
60         }
61
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))
70             }
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,
80         }
81     }
82
83     fn remove_nop_landing_pads(&self, body: &mut BodyAndCache<'_>) {
84         // make sure there's a single resume block
85         let resume_block = {
86             let patch = MirPatch::new(body);
87             let resume_block = patch.resume_block();
88             patch.apply(body);
89             resume_block
90         };
91         debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
92
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());
96
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;
106                     jumps_folded += 1;
107                 }
108             }
109
110             match body[bb].terminator_mut().unwind_mut() {
111                 Some(unwind) => {
112                     if *unwind == Some(resume_block) {
113                         debug!("    removing noop landing pad");
114                         jumps_folded -= 1;
115                         landing_pads_removed += 1;
116                         *unwind = None;
117                     }
118                 }
119                 _ => {}
120             }
121
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);
125             }
126             debug!("    is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
127         }
128
129         debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);
130     }
131 }