mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-20 02:43:45 +00:00
make traversal::reverse_postorder
use RPO cache
This commit is contained in:
parent
8f6e65136c
commit
0eec39b67d
@ -295,34 +295,11 @@ pub fn reachable_as_bitset(body: &Body<'_>) -> BitSet<BasicBlock> {
|
||||
iter.visited
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ReversePostorderIter<'a, 'tcx> {
|
||||
/// Creates an iterator over the `Body`'s basic blocks, that:
|
||||
/// - returns basic blocks in a reverse postorder,
|
||||
/// - makes use of the `BasicBlocks` CFG cache's reverse postorder.
|
||||
pub fn reverse_postorder<'a, 'tcx>(
|
||||
body: &'a Body<'tcx>,
|
||||
blocks: &'a [BasicBlock],
|
||||
idx: usize,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> Iterator for ReversePostorderIter<'a, 'tcx> {
|
||||
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
|
||||
|
||||
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
|
||||
if self.idx == 0 {
|
||||
return None;
|
||||
}
|
||||
self.idx -= 1;
|
||||
|
||||
self.blocks.get(self.idx).map(|&bb| (bb, &self.body[bb]))
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.idx, Some(self.idx))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> ExactSizeIterator for ReversePostorderIter<'a, 'tcx> {}
|
||||
|
||||
pub fn reverse_postorder<'a, 'tcx>(body: &'a Body<'tcx>) -> ReversePostorderIter<'a, 'tcx> {
|
||||
let blocks = body.basic_blocks.postorder();
|
||||
let len = blocks.len();
|
||||
ReversePostorderIter { body, blocks, idx: len }
|
||||
) -> impl Iterator<Item = (BasicBlock, &'a BasicBlockData<'tcx>)> + ExactSizeIterator {
|
||||
body.basic_blocks.reverse_postorder().iter().map(|&bb| (bb, &body.basic_blocks[bb]))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user