From 0a17f0697a726bf714fdeea0366ad364bc030023 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 30 Oct 2023 21:40:08 +1100 Subject: [PATCH] coverage: Push down and inline `bcb_needs_branch_counters` This lets us avoid creating two copies of the node's branch list. --- .../src/coverage/counters.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/counters.rs b/compiler/rustc_mir_transform/src/coverage/counters.rs index f4499433e3f..4b0014e502a 100644 --- a/compiler/rustc_mir_transform/src/coverage/counters.rs +++ b/compiler/rustc_mir_transform/src/coverage/counters.rs @@ -228,9 +228,7 @@ impl<'a> MakeBcbCounters<'a> { debug!("{:?} has at least one coverage span. Get or make its counter", bcb); let branching_counter_operand = self.get_or_make_counter_operand(bcb); - if self.bcb_needs_branch_counters(bcb) { - self.make_branch_counters(&traversal, bcb, branching_counter_operand); - } + self.make_branch_counters(&traversal, bcb, branching_counter_operand); } else { debug!( "{:?} does not have any coverage spans. A counter will only be added if \ @@ -254,6 +252,15 @@ impl<'a> MakeBcbCounters<'a> { branching_counter_operand: CovTerm, ) { let branches = self.bcb_branches(from_bcb); + + // If this node doesn't have multiple out-edges, or all of its out-edges + // already have counters, then we don't need to create edge counters. + let needs_branch_counters = + branches.len() > 1 && branches.iter().any(|branch| self.branch_has_no_counter(branch)); + if !needs_branch_counters { + return; + } + debug!( "{from_bcb:?} has some branch(es) without counters:\n {}", branches @@ -510,12 +517,6 @@ impl<'a> MakeBcbCounters<'a> { .collect::>() } - fn bcb_needs_branch_counters(&self, bcb: BasicCoverageBlock) -> bool { - let branch_needs_a_counter = |branch: &BcbBranch| self.branch_has_no_counter(branch); - let branches = self.bcb_branches(bcb); - branches.len() > 1 && branches.iter().any(branch_needs_a_counter) - } - fn branch_has_no_counter(&self, branch: &BcbBranch) -> bool { self.branch_counter(branch).is_none() }