rust/src/librustc_mir/transform/add_call_guards.rs

85 lines
2.9 KiB
Rust
Raw Normal View History

use rustc::ty::TyCtxt;
2016-09-19 15:50:00 -05:00
use rustc::mir::*;
use rustc_index::vec::{Idx, IndexVec};
2019-02-07 15:28:15 -06:00
use crate::transform::{MirPass, MirSource};
#[derive(PartialEq)]
pub enum AddCallGuards {
AllCallEdges,
CriticalCallEdges,
}
pub use self::AddCallGuards::*;
/**
* Breaks outgoing critical edges for call terminators in the MIR.
*
* Critical edges are edges that are neither the only edge leaving a
* block, nor the only edge entering one.
*
* When you want something to happen "along" an edge, you can either
* do at the end of the predecessor block, or at the start of the
* successor block. Critical edges have to be broken in order to prevent
* "edge actions" from affecting other edges. We need this for calls that are
2018-05-08 08:10:16 -05:00
* codegened to LLVM invoke instructions, because invoke is a block terminator
* in LLVM so we can't insert any code to handle the call's result into the
* block that performs the call.
*
* This function will break those edges by inserting new blocks along them.
*
* NOTE: Simplify CFG will happily undo most of the work this pass does.
*
*/
2019-08-04 15:20:00 -05:00
impl<'tcx> MirPass<'tcx> for AddCallGuards {
2019-10-28 17:16:25 -05:00
fn run_pass(
&self, _tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>
2019-10-28 17:16:25 -05:00
) {
self.add_call_guards(body);
2017-03-09 12:36:01 -06:00
}
}
impl AddCallGuards {
pub fn add_call_guards(&self, body: &mut BodyAndCache<'_>) {
let pred_count: IndexVec<_, _> = body.predecessors().iter().map(|ps| ps.len()).collect();
// We need a place to store the new blocks generated
let mut new_blocks = Vec::new();
let cur_len = body.basic_blocks().len();
for block in body.basic_blocks_mut() {
match block.terminator {
Some(Terminator {
kind: TerminatorKind::Call {
destination: Some((_, ref mut destination)),
cleanup,
..
}, source_info
}) if pred_count[*destination] > 1 &&
(cleanup.is_some() || self == &AllCallEdges) =>
{
// It's a critical edge, break it
let call_guard = BasicBlockData {
statements: vec![],
is_cleanup: block.is_cleanup,
terminator: Some(Terminator {
source_info,
kind: TerminatorKind::Goto { target: *destination }
})
};
// Get the index it will be when inserted into the MIR
let idx = cur_len + new_blocks.len();
new_blocks.push(call_guard);
*destination = BasicBlock::new(idx);
}
_ => {}
}
}
debug!("Broke {} N edges", new_blocks.len());
body.basic_blocks_mut().extend(new_blocks);
}
}