]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir/src/transform/remove_noop_landing_pads.rs
Rollup merge of #82255 - nhwn:nonzero-err-as-bug, r=davidtwco
[rust.git] / compiler / rustc_mir / src / transform / remove_noop_landing_pads.rs
1 use crate::transform::MirPass;
2 use crate::util::patch::MirPatch;
3 use rustc_index::bit_set::BitSet;
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 pub fn remove_noop_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
14     if tcx.sess.panic_strategy() == PanicStrategy::Abort {
15         return;
16     }
17     debug!("remove_noop_landing_pads({:?})", body);
18
19     RemoveNoopLandingPads.remove_nop_landing_pads(body)
20 }
21
22 impl<'tcx> MirPass<'tcx> for RemoveNoopLandingPads {
23     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
24         remove_noop_landing_pads(tcx, body);
25     }
26 }
27
28 impl RemoveNoopLandingPads {
29     fn is_nop_landing_pad(
30         &self,
31         bb: BasicBlock,
32         body: &Body<'_>,
33         nop_landing_pads: &BitSet<BasicBlock>,
34     ) -> bool {
35         for stmt in &body[bb].statements {
36             match &stmt.kind {
37                 StatementKind::FakeRead(..)
38                 | StatementKind::StorageLive(_)
39                 | StatementKind::StorageDead(_)
40                 | StatementKind::AscribeUserType(..)
41                 | StatementKind::Coverage(..)
42                 | StatementKind::Nop => {
43                     // These are all nops in a landing pad
44                 }
45
46                 StatementKind::Assign(box (place, Rvalue::Use(_) | Rvalue::Discriminant(_))) => {
47                     if place.as_local().is_some() {
48                         // Writing to a local (e.g., a drop flag) does not
49                         // turn a landing pad to a non-nop
50                     } else {
51                         return false;
52                     }
53                 }
54
55                 StatementKind::Assign { .. }
56                 | StatementKind::SetDiscriminant { .. }
57                 | StatementKind::LlvmInlineAsm { .. }
58                 | StatementKind::Retag { .. } => {
59                     return false;
60                 }
61             }
62         }
63
64         let terminator = body[bb].terminator();
65         match terminator.kind {
66             TerminatorKind::Goto { .. }
67             | TerminatorKind::Resume
68             | TerminatorKind::SwitchInt { .. }
69             | TerminatorKind::FalseEdge { .. }
70             | TerminatorKind::FalseUnwind { .. } => {
71                 terminator.successors().all(|&succ| nop_landing_pads.contains(succ))
72             }
73             TerminatorKind::GeneratorDrop
74             | TerminatorKind::Yield { .. }
75             | TerminatorKind::Return
76             | TerminatorKind::Abort
77             | TerminatorKind::Unreachable
78             | TerminatorKind::Call { .. }
79             | TerminatorKind::Assert { .. }
80             | TerminatorKind::DropAndReplace { .. }
81             | TerminatorKind::Drop { .. }
82             | TerminatorKind::InlineAsm { .. } => false,
83         }
84     }
85
86     fn remove_nop_landing_pads(&self, body: &mut Body<'_>) {
87         // make sure there's a single resume block
88         let resume_block = {
89             let 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 }