]>
Commit | Line | Data |
---|---|---|
3157f602 | 1 | use rustc::ty::TyCtxt; |
c30ab7b3 | 2 | use rustc::mir::*; |
e74abb32 | 3 | use rustc_index::vec::{Idx, IndexVec}; |
9fa01778 | 4 | use crate::transform::{MirPass, MirSource}; |
3157f602 | 5 | |
3b2f2976 XL |
6 | #[derive(PartialEq)] |
7 | pub enum AddCallGuards { | |
8 | AllCallEdges, | |
9 | CriticalCallEdges, | |
10 | } | |
11 | pub use self::AddCallGuards::*; | |
3157f602 XL |
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 | |
94b46f34 | 23 | * codegened to LLVM invoke instructions, because invoke is a block terminator |
3157f602 XL |
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 | ||
e1599b0c XL |
33 | impl<'tcx> MirPass<'tcx> for AddCallGuards { |
34 | fn run_pass(&self, _tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut Body<'tcx>) { | |
dc9dc135 | 35 | self.add_call_guards(body); |
cc61c64b XL |
36 | } |
37 | } | |
38 | ||
3b2f2976 | 39 | impl AddCallGuards { |
dc9dc135 | 40 | pub fn add_call_guards(&self, body: &mut Body<'_>) { |
3b2f2976 | 41 | let pred_count: IndexVec<_, _> = |
dc9dc135 | 42 | body.predecessors().iter().map(|ps| ps.len()).collect(); |
3157f602 | 43 | |
3b2f2976 XL |
44 | // We need a place to store the new blocks generated |
45 | let mut new_blocks = Vec::new(); | |
3157f602 | 46 | |
dc9dc135 | 47 | let cur_len = body.basic_blocks().len(); |
3157f602 | 48 | |
dc9dc135 | 49 | for block in body.basic_blocks_mut() { |
3b2f2976 XL |
50 | match block.terminator { |
51 | Some(Terminator { | |
52 | kind: TerminatorKind::Call { | |
53 | destination: Some((_, ref mut destination)), | |
54 | cleanup, | |
55 | .. | |
56 | }, source_info | |
57 | }) if pred_count[*destination] > 1 && | |
58 | (cleanup.is_some() || self == &AllCallEdges) => | |
59 | { | |
60 | // It's a critical edge, break it | |
61 | let call_guard = BasicBlockData { | |
62 | statements: vec![], | |
63 | is_cleanup: block.is_cleanup, | |
64 | terminator: Some(Terminator { | |
65 | source_info, | |
66 | kind: TerminatorKind::Goto { target: *destination } | |
67 | }) | |
68 | }; | |
3157f602 | 69 | |
3b2f2976 XL |
70 | // Get the index it will be when inserted into the MIR |
71 | let idx = cur_len + new_blocks.len(); | |
72 | new_blocks.push(call_guard); | |
73 | *destination = BasicBlock::new(idx); | |
74 | } | |
75 | _ => {} | |
3157f602 XL |
76 | } |
77 | } | |
78 | ||
3b2f2976 | 79 | debug!("Broke {} N edges", new_blocks.len()); |
3157f602 | 80 | |
dc9dc135 | 81 | body.basic_blocks_mut().extend(new_blocks); |
3b2f2976 | 82 | } |
3157f602 | 83 | } |