]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/add_call_guards.rs
Rollup merge of #67113 - Centril:enum-vis-pretty-fix, r=davidtwco
[rust.git] / src / librustc_mir / transform / add_call_guards.rs
1 use rustc::ty::TyCtxt;
2 use rustc::mir::*;
3 use rustc_index::vec::{Idx, IndexVec};
4 use crate::transform::{MirPass, MirSource};
5
6 #[derive(PartialEq)]
7 pub enum AddCallGuards {
8     AllCallEdges,
9     CriticalCallEdges,
10 }
11 pub use self::AddCallGuards::*;
12
13 /**
14  * Breaks outgoing critical edges for call terminators in the MIR.
15  *
16  * Critical edges are edges that are neither the only edge leaving a
17  * block, nor the only edge entering one.
18  *
19  * When you want something to happen "along" an edge, you can either
20  * do at the end of the predecessor block, or at the start of the
21  * successor block. Critical edges have to be broken in order to prevent
22  * "edge actions" from affecting other edges. We need this for calls that are
23  * codegened to LLVM invoke instructions, because invoke is a block terminator
24  * in LLVM so we can't insert any code to handle the call's result into the
25  * block that performs the call.
26  *
27  * This function will break those edges by inserting new blocks along them.
28  *
29  * NOTE: Simplify CFG will happily undo most of the work this pass does.
30  *
31  */
32
33 impl<'tcx> MirPass<'tcx> for AddCallGuards {
34     fn run_pass(
35         &self, _tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>
36     ) {
37         self.add_call_guards(body);
38     }
39 }
40
41 impl AddCallGuards {
42     pub fn add_call_guards(&self, body: &mut BodyAndCache<'_>) {
43         let pred_count: IndexVec<_, _> = body.predecessors().iter().map(|ps| ps.len()).collect();
44
45         // We need a place to store the new blocks generated
46         let mut new_blocks = Vec::new();
47
48         let cur_len = body.basic_blocks().len();
49
50         for block in body.basic_blocks_mut() {
51             match block.terminator {
52                 Some(Terminator {
53                     kind: TerminatorKind::Call {
54                         destination: Some((_, ref mut destination)),
55                         cleanup,
56                         ..
57                     }, source_info
58                 }) if pred_count[*destination] > 1 &&
59                       (cleanup.is_some() || self == &AllCallEdges) =>
60                 {
61                     // It's a critical edge, break it
62                     let call_guard = BasicBlockData {
63                         statements: vec![],
64                         is_cleanup: block.is_cleanup,
65                         terminator: Some(Terminator {
66                             source_info,
67                             kind: TerminatorKind::Goto { target: *destination }
68                         })
69                     };
70
71                     // Get the index it will be when inserted into the MIR
72                     let idx = cur_len + new_blocks.len();
73                     new_blocks.push(call_guard);
74                     *destination = BasicBlock::new(idx);
75                 }
76                 _ => {}
77             }
78         }
79
80         debug!("Broke {} N edges", new_blocks.len());
81
82         body.basic_blocks_mut().extend(new_blocks);
83     }
84 }