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