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