]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/add_call_guards.rs
Changed usages of `mir` in librustc::mir and librustc_mir to `body`
[rust.git] / src / librustc_mir / transform / add_call_guards.rs
1 use rustc::ty::TyCtxt;
2 use rustc::mir::*;
3 use rustc_data_structures::indexed_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 MirPass for AddCallGuards {
34     fn run_pass<'a, 'tcx>(&self,
35                           _tcx: TyCtxt<'a, 'tcx, 'tcx>,
36                           _src: MirSource<'tcx>,
37                           body: &mut Body<'tcx>) {
38         self.add_call_guards(body);
39     }
40 }
41
42 impl AddCallGuards {
43     pub fn add_call_guards(&self, body: &mut Body<'_>) {
44         let pred_count: IndexVec<_, _> =
45             body.predecessors().iter().map(|ps| ps.len()).collect();
46
47         // We need a place to store the new blocks generated
48         let mut new_blocks = Vec::new();
49
50         let cur_len = body.basic_blocks().len();
51
52         for block in body.basic_blocks_mut() {
53             match block.terminator {
54                 Some(Terminator {
55                     kind: TerminatorKind::Call {
56                         destination: Some((_, ref mut destination)),
57                         cleanup,
58                         ..
59                     }, source_info
60                 }) if pred_count[*destination] > 1 &&
61                       (cleanup.is_some() || self == &AllCallEdges) =>
62                 {
63                     // It's a critical edge, break it
64                     let call_guard = BasicBlockData {
65                         statements: vec![],
66                         is_cleanup: block.is_cleanup,
67                         terminator: Some(Terminator {
68                             source_info,
69                             kind: TerminatorKind::Goto { target: *destination }
70                         })
71                     };
72
73                     // Get the index it will be when inserted into the MIR
74                     let idx = cur_len + new_blocks.len();
75                     new_blocks.push(call_guard);
76                     *destination = BasicBlock::new(idx);
77                 }
78                 _ => {}
79             }
80         }
81
82         debug!("Broke {} N edges", new_blocks.len());
83
84         body.basic_blocks_mut().extend(new_blocks);
85     }
86 }