Skip to content

Commit

Permalink
Add reachable and friends to mir::traversal module
Browse files Browse the repository at this point in the history
  • Loading branch information
ecstatic-morse committed Jul 8, 2020
1 parent 0c03aee commit efe634f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/librustc_middle/mir/traversal.rs
Expand Up @@ -292,3 +292,20 @@ impl<'a, 'tcx> Iterator for ReversePostorder<'a, 'tcx> {
}

impl<'a, 'tcx> ExactSizeIterator for ReversePostorder<'a, 'tcx> {}

/// Returns an iterator over all basic blocks reachable from the `START_BLOCK` in no particular
/// order.
///
/// This is clearer than writing `preorder` in cases where the order doesn't matter.
pub fn reachable<'a, 'tcx>(
body: &'a Body<'tcx>,
) -> impl 'a + Iterator<Item = (BasicBlock, &'a BasicBlockData<'tcx>)> {
preorder(body)
}

/// Returns a `BitSet` containing all basic blocks reachable from the `START_BLOCK`.
pub fn reachable_as_bitset(body: &Body<'tcx>) -> BitSet<BasicBlock> {
let mut iter = preorder(body);
(&mut iter).for_each(drop);
iter.visited
}
9 changes: 9 additions & 0 deletions src/librustc_mir/dataflow/framework/engine.rs
Expand Up @@ -52,6 +52,15 @@ where
visit_results(body, blocks, self, vis)
}

pub fn visit_reachable_with(
&self,
body: &'mir mir::Body<'tcx>,
vis: &mut impl ResultsVisitor<'mir, 'tcx, FlowState = BitSet<A::Idx>>,
) {
let blocks = mir::traversal::reachable(body);
visit_results(body, blocks.map(|(bb, _)| bb), self, vis)
}

pub fn visit_in_rpo_with(
&self,
body: &'mir mir::Body<'tcx>,
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_mir/transform/generator.rs
Expand Up @@ -624,9 +624,7 @@ fn compute_storage_conflicts(
local_conflicts: BitMatrix::from_row_n(&ineligible_locals, body.local_decls.len()),
};

// Visit only reachable basic blocks. The exact order is not important.
let reachable_blocks = traversal::preorder(body).map(|(bb, _)| bb);
requires_storage.visit_with(body, reachable_blocks, &mut visitor);
requires_storage.visit_reachable_with(body, &mut visitor);

let local_conflicts = visitor.local_conflicts;

Expand Down

0 comments on commit efe634f

Please sign in to comment.