]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs
Auto merge of #107574 - compiler-errors:back-to-old-mac-builders-pls, r=pietroalbini
[rust.git] / compiler / rustc_mir_transform / src / remove_noop_landing_pads.rs
1 use crate::MirPass;
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;
7
8 /// A pass that removes noop landing pads and replaces jumps to them with
9 /// `None`. This is important because otherwise LLVM generates terrible
10 /// code for these.
11 pub struct RemoveNoopLandingPads;
12
13 impl<'tcx> MirPass<'tcx> for RemoveNoopLandingPads {
14     fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
15         sess.panic_strategy() != PanicStrategy::Abort
16     }
17
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)
21     }
22 }
23
24 impl RemoveNoopLandingPads {
25     fn is_nop_landing_pad(
26         &self,
27         bb: BasicBlock,
28         body: &Body<'_>,
29         nop_landing_pads: &BitSet<BasicBlock>,
30     ) -> bool {
31         for stmt in &body[bb].statements {
32             match &stmt.kind {
33                 StatementKind::FakeRead(..)
34                 | StatementKind::StorageLive(_)
35                 | StatementKind::StorageDead(_)
36                 | StatementKind::AscribeUserType(..)
37                 | StatementKind::Coverage(..)
38                 | StatementKind::ConstEvalCounter
39                 | StatementKind::Nop => {
40                     // These are all noops in a landing pad
41                 }
42
43                 StatementKind::Assign(box (place, Rvalue::Use(_) | Rvalue::Discriminant(_))) => {
44                     if place.as_local().is_some() {
45                         // Writing to a local (e.g., a drop flag) does not
46                         // turn a landing pad to a non-nop
47                     } else {
48                         return false;
49                     }
50                 }
51
52                 StatementKind::Assign { .. }
53                 | StatementKind::SetDiscriminant { .. }
54                 | StatementKind::Deinit(..)
55                 | StatementKind::Intrinsic(..)
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::FalseEdge { .. }
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 { .. }
80             | TerminatorKind::InlineAsm { .. } => false,
81         }
82     }
83
84     fn remove_nop_landing_pads(&self, body: &mut Body<'_>) {
85         debug!("body: {:#?}", body);
86
87         // make sure there's a resume block
88         let resume_block = {
89             let mut patch = MirPatch::new(body);
90             let resume_block = patch.resume_block();
91             patch.apply(body);
92             resume_block
93         };
94         debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
95
96         let mut jumps_folded = 0;
97         let mut landing_pads_removed = 0;
98         let mut nop_landing_pads = BitSet::new_empty(body.basic_blocks.len());
99
100         // This is a post-order traversal, so that if A post-dominates B
101         // then A will be visited before B.
102         let postorder: Vec<_> = traversal::postorder(body).map(|(bb, _)| bb).collect();
103         for bb in postorder {
104             debug!("  processing {:?}", bb);
105             if let Some(unwind) = body[bb].terminator_mut().unwind_mut() {
106                 if let Some(unwind_bb) = *unwind {
107                     if nop_landing_pads.contains(unwind_bb) {
108                         debug!("    removing noop landing pad");
109                         landing_pads_removed += 1;
110                         *unwind = None;
111                     }
112                 }
113             }
114
115             for target in body[bb].terminator_mut().successors_mut() {
116                 if *target != resume_block && nop_landing_pads.contains(*target) {
117                     debug!("    folding noop jump to {:?} to resume block", target);
118                     *target = resume_block;
119                     jumps_folded += 1;
120                 }
121             }
122
123             let is_nop_landing_pad = self.is_nop_landing_pad(bb, body, &nop_landing_pads);
124             if is_nop_landing_pad {
125                 nop_landing_pads.insert(bb);
126             }
127             debug!("    is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
128         }
129
130         debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);
131     }
132 }