Skip to content

Commit 1c04334

Browse files
committed
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.
1 parent 17e52f2 commit 1c04334

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

compiler/rustc_mir_transform/src/coverage/counters.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::fmt::{self, Debug};
1212

1313
/// The coverage counter or counter expression associated with a particular
1414
/// BCB node or BCB edge.
15-
#[derive(Clone)]
15+
#[derive(Clone, Copy)]
1616
pub(super) enum BcbCounter {
1717
Counter { id: CounterId },
1818
Expression { id: ExpressionId },
@@ -88,8 +88,9 @@ impl CoverageCounters {
8888
BcbCounter::Counter { id }
8989
}
9090

91-
fn make_expression(&mut self, lhs: CovTerm, op: Op, rhs: CovTerm) -> BcbCounter {
92-
let id = self.expressions.push(Expression { lhs, op, rhs });
91+
fn make_expression(&mut self, lhs: BcbCounter, op: Op, rhs: BcbCounter) -> BcbCounter {
92+
let expression = Expression { lhs: lhs.as_term(), op, rhs: rhs.as_term() };
93+
let id = self.expressions.push(expression);
9394
BcbCounter::Expression { id }
9495
}
9596

@@ -109,7 +110,7 @@ impl CoverageCounters {
109110
self.expressions.len()
110111
}
111112

112-
fn set_bcb_counter(&mut self, bcb: BasicCoverageBlock, counter_kind: BcbCounter) -> CovTerm {
113+
fn set_bcb_counter(&mut self, bcb: BasicCoverageBlock, counter_kind: BcbCounter) -> BcbCounter {
113114
assert!(
114115
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
115116
// have an expression (to be injected into an existing `BasicBlock` represented by this
@@ -118,14 +119,13 @@ impl CoverageCounters {
118119
"attempt to add a `Counter` to a BCB target with existing incoming edge counters"
119120
);
120121

121-
let term = counter_kind.as_term();
122122
if let Some(replaced) = self.bcb_counters[bcb].replace(counter_kind) {
123123
bug!(
124124
"attempt to set a BasicCoverageBlock coverage counter more than once; \
125125
{bcb:?} already had counter {replaced:?}",
126126
);
127127
} else {
128-
term
128+
counter_kind
129129
}
130130
}
131131

@@ -134,7 +134,7 @@ impl CoverageCounters {
134134
from_bcb: BasicCoverageBlock,
135135
to_bcb: BasicCoverageBlock,
136136
counter_kind: BcbCounter,
137-
) -> CovTerm {
137+
) -> BcbCounter {
138138
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
139139
// have an expression (to be injected into an existing `BasicBlock` represented by this
140140
// `BasicCoverageBlock`).
@@ -146,14 +146,13 @@ impl CoverageCounters {
146146
}
147147

148148
self.bcb_has_incoming_edge_counters.insert(to_bcb);
149-
let term = counter_kind.as_term();
150149
if let Some(replaced) = self.bcb_edge_counters.insert((from_bcb, to_bcb), counter_kind) {
151150
bug!(
152151
"attempt to set an edge counter more than once; from_bcb: \
153152
{from_bcb:?} already had counter {replaced:?}",
154153
);
155154
} else {
156-
term
155+
counter_kind
157156
}
158157
}
159158

@@ -303,8 +302,7 @@ impl<'a> MakeBcbCounters<'a> {
303302
sumup_counter_operand,
304303
);
305304
debug!(" [new intermediate expression: {:?}]", intermediate_expression);
306-
let intermediate_expression_operand = intermediate_expression.as_term();
307-
some_sumup_counter_operand.replace(intermediate_expression_operand);
305+
some_sumup_counter_operand.replace(intermediate_expression);
308306
}
309307
}
310308
}
@@ -334,11 +332,11 @@ impl<'a> MakeBcbCounters<'a> {
334332
}
335333

336334
#[instrument(level = "debug", skip(self))]
337-
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> CovTerm {
335+
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> BcbCounter {
338336
// If the BCB already has a counter, return it.
339-
if let Some(counter_kind) = &self.coverage_counters.bcb_counters[bcb] {
337+
if let Some(counter_kind) = self.coverage_counters.bcb_counters[bcb] {
340338
debug!("{bcb:?} already has a counter: {counter_kind:?}");
341-
return counter_kind.as_term();
339+
return counter_kind;
342340
}
343341

344342
// A BCB with only one incoming edge gets a simple `Counter` (via `make_counter()`).
@@ -380,8 +378,7 @@ impl<'a> MakeBcbCounters<'a> {
380378
edge_counter_operand,
381379
);
382380
debug!("new intermediate expression: {intermediate_expression:?}");
383-
let intermediate_expression_operand = intermediate_expression.as_term();
384-
some_sumup_edge_counter_operand.replace(intermediate_expression_operand);
381+
some_sumup_edge_counter_operand.replace(intermediate_expression);
385382
}
386383
}
387384
let counter_kind = self.coverage_counters.make_expression(
@@ -400,7 +397,7 @@ impl<'a> MakeBcbCounters<'a> {
400397
&mut self,
401398
from_bcb: BasicCoverageBlock,
402399
to_bcb: BasicCoverageBlock,
403-
) -> CovTerm {
400+
) -> BcbCounter {
404401
// If the source BCB has only one successor (assumed to be the given target), an edge
405402
// counter is unnecessary. Just get or make a counter for the source BCB.
406403
let successors = self.bcb_successors(from_bcb).iter();
@@ -409,11 +406,11 @@ impl<'a> MakeBcbCounters<'a> {
409406
}
410407

411408
// If the edge already has a counter, return it.
412-
if let Some(counter_kind) =
409+
if let Some(&counter_kind) =
413410
self.coverage_counters.bcb_edge_counters.get(&(from_bcb, to_bcb))
414411
{
415412
debug!("Edge {from_bcb:?}->{to_bcb:?} already has a counter: {counter_kind:?}");
416-
return counter_kind.as_term();
413+
return counter_kind;
417414
}
418415

419416
// Make a new counter to count this edge.

0 commit comments

Comments
 (0)