]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/add_call_guards.rs
b7c7a1774dd356f3ab2aac43decccb7002904a36
[rust.git] / src / librustc_mir / transform / add_call_guards.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc::ty::TyCtxt;
12 use rustc::mir::*;
13 use rustc::mir::transform::{MirPass, MirSource};
14 use rustc_data_structures::indexed_vec::{Idx, IndexVec};
15
16 pub struct AddCallGuards;
17
18 /**
19  * Breaks outgoing critical edges for call terminators in the MIR.
20  *
21  * Critical edges are edges that are neither the only edge leaving a
22  * block, nor the only edge entering one.
23  *
24  * When you want something to happen "along" an edge, you can either
25  * do at the end of the predecessor block, or at the start of the
26  * successor block. Critical edges have to be broken in order to prevent
27  * "edge actions" from affecting other edges. We need this for calls that are
28  * translated to LLVM invoke instructions, because invoke is a block terminator
29  * in LLVM so we can't insert any code to handle the call's result into the
30  * block that performs the call.
31  *
32  * This function will break those edges by inserting new blocks along them.
33  *
34  * NOTE: Simplify CFG will happily undo most of the work this pass does.
35  *
36  */
37
38 impl MirPass for AddCallGuards {
39     fn run_pass<'a, 'tcx>(&self,
40                           _tcx: TyCtxt<'a, 'tcx, 'tcx>,
41                           _src: MirSource,
42                           mir: &mut Mir<'tcx>) {
43         add_call_guards(mir);
44     }
45 }
46
47 pub fn add_call_guards(mir: &mut Mir) {
48     let pred_count: IndexVec<_, _> =
49         mir.predecessors().iter().map(|ps| ps.len()).collect();
50
51     // We need a place to store the new blocks generated
52     let mut new_blocks = Vec::new();
53
54     let cur_len = mir.basic_blocks().len();
55
56     for block in mir.basic_blocks_mut() {
57         match block.terminator {
58             Some(Terminator {
59                 kind: TerminatorKind::Call {
60                     destination: Some((_, ref mut destination)),
61                     cleanup: Some(_),
62                     ..
63                 }, source_info
64             }) if pred_count[*destination] > 1 => {
65                 // It's a critical edge, break it
66                 let call_guard = BasicBlockData {
67                     statements: vec![],
68                     is_cleanup: block.is_cleanup,
69                     terminator: Some(Terminator {
70                         source_info: source_info,
71                         kind: TerminatorKind::Goto { target: *destination }
72                     })
73                 };
74
75                 // Get the index it will be when inserted into the MIR
76                 let idx = cur_len + new_blocks.len();
77                 new_blocks.push(call_guard);
78                 *destination = BasicBlock::new(idx);
79             }
80             _ => {}
81         }
82     }
83
84     debug!("Broke {} N edges", new_blocks.len());
85
86     mir.basic_blocks_mut().extend(new_blocks);
87 }