1use rustc_index::{Idx, IndexVec};
2use rustc_middle::mir::*;
3use rustc_middle::ty::TyCtxt;
4use tracing::debug;
56#[derive(PartialEq)]
7pub(super) enum AddCallGuards {
8 AllCallEdges,
9 CriticalCallEdges,
10}
11pub(super) use self::AddCallGuards::*;
1213/**
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 */
3233impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
34fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
35let mut pred_count = IndexVec::from_elem(0u8, &body.basic_blocks);
36for (_, data) in body.basic_blocks.iter_enumerated() {
37for succ in data.terminator().successors() {
38 pred_count[succ] = pred_count[succ].saturating_add(1);
39 }
40 }
4142// We need a place to store the new blocks generated
43let mut new_blocks = Vec::new();
4445let cur_len = body.basic_blocks.len();
46let mut new_block = |source_info: SourceInfo, is_cleanup: bool, target: BasicBlock| {
47let block = BasicBlockData {
48 statements: vec![],
49is_cleanup,
50 terminator: Some(Terminator { source_info, kind: TerminatorKind::Goto { target } }),
51 };
52let idx = cur_len + new_blocks.len();
53new_blocks.push(block);
54BasicBlock::new(idx)
55 };
5657for block in body.basic_blocks_mut() {
58match block.terminator {
59Some(Terminator {
60 kind: TerminatorKind::Call { target: Some(ref mut destination), unwind, .. },
61 source_info,
62 }) if pred_count[*destination] > 1
63&& (generates_invoke(unwind) || self == &AllCallEdges) =>
64 {
65// It's a critical edge, break it
66*destination = new_block(source_info, block.is_cleanup, *destination);
67 }
68Some(Terminator {
69 kind:
70 TerminatorKind::InlineAsm {
71 asm_macro: InlineAsmMacro::Asm,
72ref mut targets,
73ref operands,
74 unwind,
75 ..
76 },
77 source_info,
78 }) if self == &CriticalCallEdges => {
79let has_outputs = operands.iter().any(|op| {
80matches!(op, InlineAsmOperand::InOut { .. } | InlineAsmOperand::Out { .. })
81 });
82let has_labels =
83 operands.iter().any(|op| matches!(op, InlineAsmOperand::Label { .. }));
84if has_outputs && (has_labels || generates_invoke(unwind)) {
85for target in targets.iter_mut() {
86if pred_count[*target] > 1 {
87*target = new_block(source_info, block.is_cleanup, *target);
88 }
89 }
90 }
91 }
92_ => {}
93 }
94 }
9596debug!("Broke {} N edges", new_blocks.len());
9798body.basic_blocks_mut().extend(new_blocks);
99 }
100101fn is_required(&self) -> bool {
102true
103}
104}
105106/// Returns true if this unwind action is code generated as an invoke as opposed to a call.
107fn generates_invoke(unwind: UnwindAction) -> bool {
108match unwind {
109UnwindAction::Continue | UnwindAction::Unreachable => false,
110UnwindAction::Cleanup(_) | UnwindAction::Terminate(_) => true,
111 }
112}