Skip to content

Commit efe634f

Browse files
Add reachable and friends to mir::traversal module
1 parent 0c03aee commit efe634f

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/librustc_middle/mir/traversal.rs

+17
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,20 @@ impl<'a, 'tcx> Iterator for ReversePostorder<'a, 'tcx> {
292292
}
293293

294294
impl<'a, 'tcx> ExactSizeIterator for ReversePostorder<'a, 'tcx> {}
295+
296+
/// Returns an iterator over all basic blocks reachable from the `START_BLOCK` in no particular
297+
/// order.
298+
///
299+
/// This is clearer than writing `preorder` in cases where the order doesn't matter.
300+
pub fn reachable<'a, 'tcx>(
301+
body: &'a Body<'tcx>,
302+
) -> impl 'a + Iterator<Item = (BasicBlock, &'a BasicBlockData<'tcx>)> {
303+
preorder(body)
304+
}
305+
306+
/// Returns a `BitSet` containing all basic blocks reachable from the `START_BLOCK`.
307+
pub fn reachable_as_bitset(body: &Body<'tcx>) -> BitSet<BasicBlock> {
308+
let mut iter = preorder(body);
309+
(&mut iter).for_each(drop);
310+
iter.visited
311+
}

src/librustc_mir/dataflow/framework/engine.rs

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ where
5252
visit_results(body, blocks, self, vis)
5353
}
5454

55+
pub fn visit_reachable_with(
56+
&self,
57+
body: &'mir mir::Body<'tcx>,
58+
vis: &mut impl ResultsVisitor<'mir, 'tcx, FlowState = BitSet<A::Idx>>,
59+
) {
60+
let blocks = mir::traversal::reachable(body);
61+
visit_results(body, blocks.map(|(bb, _)| bb), self, vis)
62+
}
63+
5564
pub fn visit_in_rpo_with(
5665
&self,
5766
body: &'mir mir::Body<'tcx>,

src/librustc_mir/transform/generator.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,7 @@ fn compute_storage_conflicts(
624624
local_conflicts: BitMatrix::from_row_n(&ineligible_locals, body.local_decls.len()),
625625
};
626626

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

631629
let local_conflicts = visitor.local_conflicts;
632630

0 commit comments

Comments
 (0)