Skip to content

Commit

Permalink
coverage: Unbox and simplify bcb_filtered_successors
Browse files Browse the repository at this point in the history
There is no need to box this iterator if we instead add appropriate lifetime
captures, and make `ShortCircuitPreorder` generic over the type of iterator it
expects.

We can also greatly simplify the function's implementation by observing that
the only difference between its two cases is whether we take all of a BB's
successors, or just the first one.
  • Loading branch information
Zalathar committed Oct 10, 2023
1 parent 68ee550 commit f155194
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions compiler/rustc_mir_transform/src/coverage/graph.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::cmp::Ordering;
use std::ops::{Index, IndexMut};

use rustc_data_structures::captures::Captures;
use rustc_data_structures::graph::dominators::{self, Dominators};
use rustc_data_structures::graph::{self, GraphSuccessors, WithNumNodes, WithStartNode};
use rustc_index::bit_set::BitSet;
use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::mir::{self, BasicBlock, TerminatorKind};

use std::cmp::Ordering;
use std::ops::{Index, IndexMut};

/// A coverage-specific simplification of the MIR control flow graph (CFG). The `CoverageGraph`s
/// nodes are `BasicCoverageBlock`s, which encompass one or more MIR `BasicBlock`s.
#[derive(Debug)]
Expand Down Expand Up @@ -363,21 +364,21 @@ impl std::fmt::Debug for BcbBranch {
fn bcb_filtered_successors<'a, 'tcx>(
body: &'a mir::Body<'tcx>,
bb: BasicBlock,
) -> Box<dyn Iterator<Item = BasicBlock> + 'a> {
let term_kind = &body[bb].terminator().kind;
Box::new(
match &term_kind {
// SwitchInt successors are never unwind, and all of them should be traversed.
TerminatorKind::SwitchInt { ref targets, .. } => {
None.into_iter().chain(targets.all_targets().into_iter().copied())
}
// For all other kinds, return only the first successor, if any, and ignore unwinds.
// NOTE: `chain(&[])` is required to coerce the `option::iter` (from
// `next().into_iter()`) into the `mir::Successors` aliased type.
_ => term_kind.successors().next().into_iter().chain((&[]).into_iter().copied()),
}
.filter(move |&successor| body[successor].terminator().kind != TerminatorKind::Unreachable),
)
) -> impl Iterator<Item = BasicBlock> + Captures<'a> + Captures<'tcx> {
let terminator = body[bb].terminator();

let take_n_successors = match terminator.kind {
// SwitchInt successors are never unwinds, so all of them should be traversed.
TerminatorKind::SwitchInt { .. } => usize::MAX,
// For all other kinds, return only the first successor (if any), ignoring any
// unwind successors.
_ => 1,
};

terminator
.successors()
.take(take_n_successors)
.filter(move |&successor| body[successor].terminator().kind != TerminatorKind::Unreachable)
}

/// Maintains separate worklists for each loop in the BasicCoverageBlock CFG, plus one for the
Expand Down Expand Up @@ -556,9 +557,10 @@ struct ShortCircuitPreorder<'a, 'tcx, F> {
filtered_successors: F,
}

impl<'a, 'tcx, F> ShortCircuitPreorder<'a, 'tcx, F>
impl<'a, 'tcx, F, Iter> ShortCircuitPreorder<'a, 'tcx, F>
where
F: Fn(&'a mir::Body<'tcx>, BasicBlock) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
F: Fn(&'a mir::Body<'tcx>, BasicBlock) -> Iter,
Iter: Iterator<Item = BasicBlock>,
{
fn new(body: &'a mir::Body<'tcx>, filtered_successors: F) -> Self {
Self {
Expand All @@ -570,9 +572,10 @@ where
}
}

impl<'a, 'tcx, F> Iterator for ShortCircuitPreorder<'a, 'tcx, F>
impl<'a, 'tcx, F, Iter> Iterator for ShortCircuitPreorder<'a, 'tcx, F>
where
F: Fn(&'a mir::Body<'tcx>, BasicBlock) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
F: Fn(&'a mir::Body<'tcx>, BasicBlock) -> Iter,
Iter: Iterator<Item = BasicBlock>,
{
type Item = BasicBlock;

Expand Down

0 comments on commit f155194

Please sign in to comment.