Skip non-branching blocks now that we're using basic blocks

This is a rebase of [1]. In the long term, we'll want to simplify these
analysis passes. For now, this is simple and will reduce the number of
instructions processed in certain cases.

[1] https://github.com/bytecodealliance/cranelift/pull/866
This commit is contained in:
Ryan Hunt
2020-03-03 13:34:04 -06:00
committed by Benjamin Bouvier
parent 135a48ca7e
commit 4aa8776a9b
4 changed files with 21 additions and 3 deletions

View File

@@ -647,6 +647,24 @@ impl Layout {
}
}
/// Iterate over a limited set of instruction which are likely the branches of `block` in layout
/// order. Any instruction not visited by this iterator is not a branch, but an instruction visited by this may not be a branch.
pub fn block_likely_branches(&self, block: Block) -> Insts {
// Note: Checking whether an instruction is a branch or not while walking backward might add
// extra overhead. However, we know that the number of branches is limited to 2 at the end of
// each block, and therefore we can just iterate over the last 2 instructions.
let mut iter = self.block_insts(block);
let head = iter.head;
let tail = iter.tail;
iter.next_back();
let head = iter.next_back().or(head);
Insts {
layout: self,
head,
tail,
}
}
/// Split the block containing `before` in two.
///
/// Insert `new_block` after the old block and move `before` and the following instructions to