]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir/src/transform/no_landing_pads.rs
Add comments to explain memory usage optimization
[rust.git] / compiler / rustc_mir / src / transform / no_landing_pads.rs
1 //! This pass removes the unwind branch of all the terminators when the no-landing-pads option is
2 //! specified.
3
4 use crate::transform::MirPass;
5 use rustc_middle::mir::visit::MutVisitor;
6 use rustc_middle::mir::*;
7 use rustc_middle::ty::TyCtxt;
8 use rustc_target::spec::PanicStrategy;
9
10 pub struct NoLandingPads<'tcx> {
11     tcx: TyCtxt<'tcx>,
12 }
13
14 impl<'tcx> NoLandingPads<'tcx> {
15     pub fn new(tcx: TyCtxt<'tcx>) -> Self {
16         NoLandingPads { tcx }
17     }
18 }
19
20 impl<'tcx> MirPass<'tcx> for NoLandingPads<'tcx> {
21     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
22         no_landing_pads(tcx, body)
23     }
24 }
25
26 pub fn no_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
27     if tcx.sess.panic_strategy() == PanicStrategy::Abort {
28         NoLandingPads::new(tcx).visit_body(body);
29     }
30 }
31
32 impl<'tcx> MutVisitor<'tcx> for NoLandingPads<'tcx> {
33     fn tcx(&self) -> TyCtxt<'tcx> {
34         self.tcx
35     }
36
37     fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
38         if let Some(unwind) = terminator.kind.unwind_mut() {
39             unwind.take();
40         }
41         self.super_terminator(terminator, location);
42     }
43 }