Provide BB layout info externally in terms of code offsets.

This is sometimes useful when performing analyses on the generated
machine code: for example, some kinds of code verifiers will want to do
a control-flow analysis, and it is much easier to do this if one does
not have to recover the CFG from the machine code (doing so requires
heavyweight analysis when indirect branches are involved). If one trusts
the control-flow lowering and only needs to verify other properties of
the code, this can be very useful.
This commit is contained in:
Chris Fallin
2021-04-28 17:52:42 -07:00
committed by Chris Fallin
parent 76c6b83f6a
commit 11a2ef01e7
8 changed files with 81 additions and 7 deletions

View File

@@ -473,13 +473,20 @@ impl<I: VCodeInst> VCode<I> {
/// Emit the instructions to a `MachBuffer`, containing fixed-up code and external
/// reloc/trap/etc. records ready for use.
pub fn emit(&self) -> MachBuffer<I>
pub fn emit(
&self,
) -> (
MachBuffer<I>,
Vec<CodeOffset>,
Vec<(CodeOffset, CodeOffset)>,
)
where
I: MachInstEmit,
{
let _tt = timing::vcode_emit();
let mut buffer = MachBuffer::new();
let mut state = I::State::new(&*self.abi);
let mut bb_starts: Vec<Option<CodeOffset>> = vec![];
// The first M MachLabels are reserved for block indices, the next N MachLabels for
// constants.
@@ -491,6 +498,7 @@ impl<I: VCodeInst> VCode<I> {
let mut safepoint_idx = 0;
let mut cur_srcloc = None;
let mut last_offset = None;
for block in 0..self.num_blocks() {
let block = block as BlockIndex;
let new_offset = I::align_basic_block(buffer.cur_offset());
@@ -504,6 +512,21 @@ impl<I: VCodeInst> VCode<I> {
let (start, end) = self.block_ranges[block as usize];
buffer.bind_label(MachLabel::from_block(block));
label_insn_iix[block as usize] = start;
// Track BB starts. If we have backed up due to MachBuffer
// branch opts, note that the removed blocks were removed.
let cur_offset = buffer.cur_offset();
if last_offset.is_some() && cur_offset <= last_offset.unwrap() {
for i in (0..bb_starts.len()).rev() {
if bb_starts[i].is_some() && cur_offset > bb_starts[i].unwrap() {
break;
}
bb_starts[i] = None;
}
}
bb_starts.push(Some(cur_offset));
last_offset = Some(cur_offset);
for iix in start..end {
let srcloc = self.srclocs[iix as usize];
if cur_srcloc != Some(srcloc) {
@@ -580,7 +603,26 @@ impl<I: VCodeInst> VCode<I> {
*self.insts_layout.borrow_mut() = (inst_ends, label_insn_iix, buffer.cur_offset());
}
buffer
// Create `bb_edges` and final (filtered) `bb_starts`.
let mut final_bb_starts = vec![];
let mut bb_edges = vec![];
for block in 0..self.num_blocks() {
if bb_starts[block].is_none() {
// Block was deleted by MachBuffer; skip.
continue;
}
let from = bb_starts[block].unwrap();
final_bb_starts.push(from);
// Resolve each `succ` label and add edges.
let succs = self.block_succs(BlockIx::new(block as u32));
for succ in succs.iter() {
let to = buffer.resolve_label_offset(MachLabel::from_block(succ.get()));
bb_edges.push((from, to));
}
}
(buffer, final_bb_starts, bb_edges)
}
/// Generates value-label ranges.