pub struct Cfg { /* private fields */ }Expand description
Control-flow graph for a single function.
Block indices map directly to BlockId(i as u32). The entry block is
always BlockId(0).
§Reachability
The CFG stores edges for all blocks, including those unreachable from the entry. Methods that return block counts or iterate over blocks document whether they include or exclude unreachable blocks:
| Method | Includes unreachable? |
|---|---|
num_blocks() | yes |
num_reachable_blocks() | no |
rpo() / post_order() | no |
is_reachable() | — |
Implementations§
Source§impl Cfg
impl Cfg
Sourcepub fn compute(func: &Function) -> Self
pub fn compute(func: &Function) -> Self
Build the CFG for func by walking each block’s terminator.
Sourcepub fn num_blocks(&self) -> usize
pub fn num_blocks(&self) -> usize
Total number of blocks in the function, including unreachable blocks.
To iterate only reachable blocks use rpo.
To count only reachable blocks use num_reachable_blocks.
Sourcepub fn num_reachable_blocks(&self) -> usize
pub fn num_reachable_blocks(&self) -> usize
Number of blocks reachable from the entry block.
This is an O(1) operation. Equivalent to cfg.rpo().len() but without
the allocation or DFS traversal cost.
Sourcepub fn is_reachable(&self, bid: BlockId) -> bool
pub fn is_reachable(&self, bid: BlockId) -> bool
Returns true if bid is reachable from the entry block.
§Panics
Panics if bid is not a valid block index for this function (same
behaviour as successors and
predecessors).
Sourcepub fn successors(&self, bid: BlockId) -> &[BlockId]
pub fn successors(&self, bid: BlockId) -> &[BlockId]
Successor blocks of bid.
Sourcepub fn predecessors(&self, bid: BlockId) -> &[BlockId]
pub fn predecessors(&self, bid: BlockId) -> &[BlockId]
Predecessor blocks of bid.
Sourcepub fn post_order(&self) -> Vec<BlockId>
pub fn post_order(&self) -> Vec<BlockId>
Blocks in post-order (a block appears after all blocks it can reach).
Unreachable blocks are omitted. Use num_blocks
if you need a count that includes them.
Sourcepub fn rpo(&self) -> Vec<BlockId>
pub fn rpo(&self) -> Vec<BlockId>
Blocks in reverse post-order (RPO). The entry block is first; a block always appears before any block it dominates.
Unreachable blocks are omitted. Use num_blocks
if you need a count that includes them.