Skip to content

Commit 6cd8248

Browse files
committed
coverage: Extract CoverageGraph::bcb_has_multiple_in_edges
This was previously a helper method in `MakeBcbCounters`, but putting it in the graph lets us call it from `BcbBranch`, and gives us a more fine-grained borrow.
1 parent 55eb922 commit 6cd8248

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

compiler/rustc_mir_transform/src/coverage/counters.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl<'a> MakeBcbCounters<'a> {
339339
// A BCB with only one incoming edge gets a simple `Counter` (via `make_counter()`).
340340
// Also, a BCB that loops back to itself gets a simple `Counter`. This may indicate the
341341
// program results in a tight infinite loop, but it should still compile.
342-
let one_path_to_target = self.bcb_has_one_path_to_target(bcb);
342+
let one_path_to_target = !self.basic_coverage_blocks.bcb_has_multiple_in_edges(bcb);
343343
if one_path_to_target || self.bcb_predecessors(bcb).contains(&bcb) {
344344
let counter_kind = self.coverage_counters.make_counter();
345345
if one_path_to_target {
@@ -508,11 +508,4 @@ impl<'a> MakeBcbCounters<'a> {
508508
self.coverage_counters.bcb_counters[to_bcb].as_ref()
509509
}
510510
}
511-
512-
/// Returns true if the BasicCoverageBlock has zero or one incoming edge. (If zero, it should be
513-
/// the entry point for the function.)
514-
#[inline]
515-
fn bcb_has_one_path_to_target(&self, bcb: BasicCoverageBlock) -> bool {
516-
self.bcb_predecessors(bcb).len() <= 1
517-
}
518511
}

compiler/rustc_mir_transform/src/coverage/graph.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ impl CoverageGraph {
199199
pub fn cmp_in_dominator_order(&self, a: BasicCoverageBlock, b: BasicCoverageBlock) -> Ordering {
200200
self.dominators.as_ref().unwrap().cmp_in_dominator_order(a, b)
201201
}
202+
203+
/// Returns true if the given node has 2 or more in-edges, i.e. 2 or more
204+
/// predecessors.
205+
///
206+
/// This property is interesting to code that assigns counters to nodes and
207+
/// edges, because if a node _doesn't_ have multiple in-edges, then there's
208+
/// no benefit in having a separate counter for its in-edge, because it
209+
/// would have the same value as the node's own counter.
210+
///
211+
/// FIXME: That assumption might not be true for [`TerminatorKind::Yield`]?
212+
#[inline(always)]
213+
pub(super) fn bcb_has_multiple_in_edges(&self, bcb: BasicCoverageBlock) -> bool {
214+
self.predecessors[bcb].len() > 1
215+
}
202216
}
203217

204218
impl Index<BasicCoverageBlock> for CoverageGraph {
@@ -334,7 +348,7 @@ impl BcbBranch {
334348
to_bcb: BasicCoverageBlock,
335349
basic_coverage_blocks: &CoverageGraph,
336350
) -> Self {
337-
let edge_from_bcb = if basic_coverage_blocks.predecessors[to_bcb].len() > 1 {
351+
let edge_from_bcb = if basic_coverage_blocks.bcb_has_multiple_in_edges(from_bcb) {
338352
Some(from_bcb)
339353
} else {
340354
None

0 commit comments

Comments
 (0)