Browse Source

Cranelift: Add a helper for getting a block's successors (#9067)

Co-authored-by: Trevor Elliott <Telliott@fastly.com>
pull/9071/head
Nick Fitzgerald 3 months ago
committed by GitHub
parent
commit
f763f0e707
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 10
      cranelift/codegen/src/ir/function.rs
  2. 8
      cranelift/codegen/src/traversals.rs

10
cranelift/codegen/src/ir/function.rs

@ -313,6 +313,16 @@ impl FunctionStencil {
Ok(()) Ok(())
} }
/// Returns an iterator over the blocks succeeding the given block.
pub fn block_successors(&self, block: Block) -> impl DoubleEndedIterator<Item = Block> + '_ {
self.layout.last_inst(block).into_iter().flat_map(|inst| {
self.dfg.insts[inst]
.branch_destination(&self.dfg.jump_tables)
.iter()
.map(|block| block.block(&self.dfg.value_lists))
})
}
/// Returns true if the function is function that doesn't call any other functions. This is not /// Returns true if the function is function that doesn't call any other functions. This is not
/// to be confused with a "leaf function" in Windows terminology. /// to be confused with a "leaf function" in Windows terminology.
pub fn is_leaf(&self) -> bool { pub fn is_leaf(&self) -> bool {

8
cranelift/codegen/src/traversals.rs

@ -96,11 +96,9 @@ impl Iterator for DfsIter<'_> {
if event == Event::Enter && self.dfs.seen.insert(block) { if event == Event::Enter && self.dfs.seen.insert(block) {
self.dfs.stack.push((Event::Exit, block)); self.dfs.stack.push((Event::Exit, block));
if let Some(inst) = self.func.layout.last_inst(block) {
self.dfs.stack.extend( self.dfs.stack.extend(
self.func.dfg.insts[inst] self.func
.branch_destination(&self.func.dfg.jump_tables) .block_successors(block)
.iter()
// Heuristic: chase the children in reverse. This puts // Heuristic: chase the children in reverse. This puts
// the first successor block first in the postorder, all // the first successor block first in the postorder, all
// other things being equal, which tends to prioritize // other things being equal, which tends to prioritize
@ -111,7 +109,6 @@ impl Iterator for DfsIter<'_> {
// purely for other consumers of the postorder we cache // purely for other consumers of the postorder we cache
// here. // here.
.rev() .rev()
.map(|block| block.block(&self.func.dfg.value_lists))
// This is purely an optimization to avoid additional // This is purely an optimization to avoid additional
// iterations of the loop, and is not required; it's // iterations of the loop, and is not required; it's
// merely inlining the check from the outer conditional // merely inlining the check from the outer conditional
@ -121,7 +118,6 @@ impl Iterator for DfsIter<'_> {
.map(|block| (Event::Enter, block)), .map(|block| (Event::Enter, block)),
); );
} }
}
Some((event, block)) Some((event, block))
} }

Loading…
Cancel
Save