Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ pub use helper::*;

mod helper {
use super::*;
pub type Successors<'a> = impl DoubleEndedIterator<Item = BasicBlock> + 'a;
pub type Successors<'a> = impl DoubleEndedIterator<Item = BasicBlock> + 'a + Clone;

// Note: this method ensures all paths below produce an iterator with the same concrete type.
#[inline]
Expand Down
36 changes: 26 additions & 10 deletions compiler/rustc_middle/src/mir/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ use super::*;
pub struct Preorder<'a, 'tcx> {
body: &'a Body<'tcx>,
visited: DenseBitSet<BasicBlock>,
worklist: Vec<BasicBlock>,
worklist: Vec<Successors<'a>>,
current: Option<BasicBlock>,
}

impl<'a, 'tcx> Preorder<'a, 'tcx> {
pub fn new(body: &'a Body<'tcx>, root: BasicBlock) -> Preorder<'a, 'tcx> {
let worklist = vec![root];

Preorder { body, visited: DenseBitSet::new_empty(body.basic_blocks.len()), worklist }
let mut visited = DenseBitSet::new_empty(body.basic_blocks.len());
visited.insert(root);
Preorder { body, visited, worklist: Vec::new(), current: Some(root) }
}
}

Expand All @@ -47,14 +48,29 @@ impl<'a, 'tcx> Iterator for Preorder<'a, 'tcx> {
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);

fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
while let Some(idx) = self.worklist.pop() {
if !self.visited.insert(idx) {
continue;
}

if let Some(idx) = self.current.take() {
let data = &self.body[idx];

self.worklist.extend(data.terminator.successors());
let mut successors = data.terminator.successors();

'outer: loop {
while let Some(next) = successors.next_back() {
let is_fresh = self.visited.insert(next);
if is_fresh {
self.current = Some(next);
break 'outer;
}
}
if let Some(next_succ) = self.worklist.pop() {
successors = next_succ;
} else {
break 'outer;
}
}

if successors.clone().next_back().is_some() {
self.worklist.push(successors);
}

return Some((idx, data));
}
Expand Down
Loading