coverage: Use BcbCounter to build coverage expressions

This means that we no longer have to manage the distinction between
`BcbCounter` and `CovTerm` when preparing expressions.
This commit is contained in:
Zalathar 2023-11-01 11:23:27 +11:00
parent df23279e1e
commit 31113c5f56

View File

@ -12,7 +12,7 @@ use std::fmt::{self, Debug};
/// The coverage counter or counter expression associated with a particular /// The coverage counter or counter expression associated with a particular
/// BCB node or BCB edge. /// BCB node or BCB edge.
#[derive(Clone)] #[derive(Clone, Copy)]
pub(super) enum BcbCounter { pub(super) enum BcbCounter {
Counter { id: CounterId }, Counter { id: CounterId },
Expression { id: ExpressionId }, Expression { id: ExpressionId },
@ -88,8 +88,9 @@ impl CoverageCounters {
BcbCounter::Counter { id } BcbCounter::Counter { id }
} }
fn make_expression(&mut self, lhs: CovTerm, op: Op, rhs: CovTerm) -> BcbCounter { fn make_expression(&mut self, lhs: BcbCounter, op: Op, rhs: BcbCounter) -> BcbCounter {
let id = self.expressions.push(Expression { lhs, op, rhs }); let expression = Expression { lhs: lhs.as_term(), op, rhs: rhs.as_term() };
let id = self.expressions.push(expression);
BcbCounter::Expression { id } BcbCounter::Expression { id }
} }
@ -109,7 +110,7 @@ impl CoverageCounters {
self.expressions.len() self.expressions.len()
} }
fn set_bcb_counter(&mut self, bcb: BasicCoverageBlock, counter_kind: BcbCounter) -> CovTerm { fn set_bcb_counter(&mut self, bcb: BasicCoverageBlock, counter_kind: BcbCounter) -> BcbCounter {
assert!( assert!(
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also // If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
// have an expression (to be injected into an existing `BasicBlock` represented by this // have an expression (to be injected into an existing `BasicBlock` represented by this
@ -118,14 +119,13 @@ impl CoverageCounters {
"attempt to add a `Counter` to a BCB target with existing incoming edge counters" "attempt to add a `Counter` to a BCB target with existing incoming edge counters"
); );
let term = counter_kind.as_term();
if let Some(replaced) = self.bcb_counters[bcb].replace(counter_kind) { if let Some(replaced) = self.bcb_counters[bcb].replace(counter_kind) {
bug!( bug!(
"attempt to set a BasicCoverageBlock coverage counter more than once; \ "attempt to set a BasicCoverageBlock coverage counter more than once; \
{bcb:?} already had counter {replaced:?}", {bcb:?} already had counter {replaced:?}",
); );
} else { } else {
term counter_kind
} }
} }
@ -134,7 +134,7 @@ impl CoverageCounters {
from_bcb: BasicCoverageBlock, from_bcb: BasicCoverageBlock,
to_bcb: BasicCoverageBlock, to_bcb: BasicCoverageBlock,
counter_kind: BcbCounter, counter_kind: BcbCounter,
) -> CovTerm { ) -> BcbCounter {
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also // If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
// have an expression (to be injected into an existing `BasicBlock` represented by this // have an expression (to be injected into an existing `BasicBlock` represented by this
// `BasicCoverageBlock`). // `BasicCoverageBlock`).
@ -148,14 +148,13 @@ impl CoverageCounters {
} }
self.bcb_has_incoming_edge_counters.insert(to_bcb); self.bcb_has_incoming_edge_counters.insert(to_bcb);
let term = counter_kind.as_term();
if let Some(replaced) = self.bcb_edge_counters.insert((from_bcb, to_bcb), counter_kind) { if let Some(replaced) = self.bcb_edge_counters.insert((from_bcb, to_bcb), counter_kind) {
bug!( bug!(
"attempt to set an edge counter more than once; from_bcb: \ "attempt to set an edge counter more than once; from_bcb: \
{from_bcb:?} already had counter {replaced:?}", {from_bcb:?} already had counter {replaced:?}",
); );
} else { } else {
term counter_kind
} }
} }
@ -305,8 +304,7 @@ impl<'a> MakeBcbCounters<'a> {
sumup_counter_operand, sumup_counter_operand,
); );
debug!(" [new intermediate expression: {:?}]", intermediate_expression); debug!(" [new intermediate expression: {:?}]", intermediate_expression);
let intermediate_expression_operand = intermediate_expression.as_term(); some_sumup_counter_operand.replace(intermediate_expression);
some_sumup_counter_operand.replace(intermediate_expression_operand);
} }
} }
} }
@ -336,11 +334,11 @@ impl<'a> MakeBcbCounters<'a> {
} }
#[instrument(level = "debug", skip(self))] #[instrument(level = "debug", skip(self))]
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> CovTerm { fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> BcbCounter {
// If the BCB already has a counter, return it. // If the BCB already has a counter, return it.
if let Some(counter_kind) = &self.coverage_counters.bcb_counters[bcb] { if let Some(counter_kind) = self.coverage_counters.bcb_counters[bcb] {
debug!("{bcb:?} already has a counter: {counter_kind:?}"); debug!("{bcb:?} already has a counter: {counter_kind:?}");
return counter_kind.as_term(); return counter_kind;
} }
// A BCB with only one incoming edge gets a simple `Counter` (via `make_counter()`). // A BCB with only one incoming edge gets a simple `Counter` (via `make_counter()`).
@ -382,8 +380,7 @@ impl<'a> MakeBcbCounters<'a> {
edge_counter_operand, edge_counter_operand,
); );
debug!("new intermediate expression: {intermediate_expression:?}"); debug!("new intermediate expression: {intermediate_expression:?}");
let intermediate_expression_operand = intermediate_expression.as_term(); some_sumup_edge_counter_operand.replace(intermediate_expression);
some_sumup_edge_counter_operand.replace(intermediate_expression_operand);
} }
} }
let counter_kind = self.coverage_counters.make_expression( let counter_kind = self.coverage_counters.make_expression(
@ -402,7 +399,7 @@ impl<'a> MakeBcbCounters<'a> {
&mut self, &mut self,
from_bcb: BasicCoverageBlock, from_bcb: BasicCoverageBlock,
to_bcb: BasicCoverageBlock, to_bcb: BasicCoverageBlock,
) -> CovTerm { ) -> BcbCounter {
// If the source BCB has only one successor (assumed to be the given target), an edge // If the source BCB has only one successor (assumed to be the given target), an edge
// counter is unnecessary. Just get or make a counter for the source BCB. // counter is unnecessary. Just get or make a counter for the source BCB.
let successors = self.bcb_successors(from_bcb).iter(); let successors = self.bcb_successors(from_bcb).iter();
@ -411,11 +408,11 @@ impl<'a> MakeBcbCounters<'a> {
} }
// If the edge already has a counter, return it. // If the edge already has a counter, return it.
if let Some(counter_kind) = if let Some(&counter_kind) =
self.coverage_counters.bcb_edge_counters.get(&(from_bcb, to_bcb)) self.coverage_counters.bcb_edge_counters.get(&(from_bcb, to_bcb))
{ {
debug!("Edge {from_bcb:?}->{to_bcb:?} already has a counter: {counter_kind:?}"); debug!("Edge {from_bcb:?}->{to_bcb:?} already has a counter: {counter_kind:?}");
return counter_kind.as_term(); return counter_kind;
} }
// Make a new counter to count this edge. // Make a new counter to count this edge.