]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs
Auto merge of #92353 - Kobzol:doc-attr-lists-gat, r=GuillaumeGomez
[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, _: 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::Nop => {
39                     // These are all nops in a landing pad
40                 }
41
42                 StatementKind::Assign(box (place, Rvalue::Use(_) | Rvalue::Discriminant(_))) => {
43                     if place.as_local().is_some() {
44                         // Writing to a local (e.g., a drop flag) does not
45                         // turn a landing pad to a non-nop
46                     } else {
47                         return false;
48                     }
49                 }
50
51                 StatementKind::Assign { .. }
52                 | StatementKind::SetDiscriminant { .. }
53                 | StatementKind::CopyNonOverlapping(..)
54                 | StatementKind::Retag { .. } => {
55                     return false;
56                 }
57             }
58         }
59
60         let terminator = body[bb].terminator();
61         match terminator.kind {
62             TerminatorKind::Goto { .. }
63             | TerminatorKind::Resume
64             | TerminatorKind::SwitchInt { .. }
65             | TerminatorKind::FalseEdge { .. }
66             | TerminatorKind::FalseUnwind { .. } => {
67                 terminator.successors().all(|&succ| nop_landing_pads.contains(succ))
68             }
69             TerminatorKind::GeneratorDrop
70             | TerminatorKind::Yield { .. }
71             | TerminatorKind::Return
72             | TerminatorKind::Abort
73             | TerminatorKind::Unreachable
74             | TerminatorKind::Call { .. }
75             | TerminatorKind::Assert { .. }
76             | TerminatorKind::DropAndReplace { .. }
77             | TerminatorKind::Drop { .. }
78             | TerminatorKind::InlineAsm { .. } => false,
79         }
80     }
81
82     fn remove_nop_landing_pads(&self, body: &mut Body<'_>) {
83         // make sure there's a single resume block
84         let resume_block = {
85             let patch = MirPatch::new(body);
86             let resume_block = patch.resume_block();
87             patch.apply(body);
88             resume_block
89         };
90         debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
91
92         let mut jumps_folded = 0;
93         let mut landing_pads_removed = 0;
94         let mut nop_landing_pads = BitSet::new_empty(body.basic_blocks().len());
95
96         // This is a post-order traversal, so that if A post-dominates B
97         // then A will be visited before B.
98         let postorder: Vec<_> = traversal::postorder(body).map(|(bb, _)| bb).collect();
99         for bb in postorder {
100             debug!("  processing {:?}", bb);
101             if let Some(unwind) = body[bb].terminator_mut().unwind_mut() {
102                 if let Some(unwind_bb) = *unwind {
103                     if nop_landing_pads.contains(unwind_bb) {
104                         debug!("    removing noop landing pad");
105                         landing_pads_removed += 1;
106                         *unwind = None;
107                     }
108                 }
109             }
110
111             for target in body[bb].terminator_mut().successors_mut() {
112                 if *target != resume_block && nop_landing_pads.contains(*target) {
113                     debug!("    folding noop jump to {:?} to resume block", target);
114                     *target = resume_block;
115                     jumps_folded += 1;
116                 }
117             }
118
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);
122             }
123             debug!("    is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
124         }
125
126         debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);
127     }
128 }