Skip to content

Commit 6000d5c

Browse files
committed
coverage: Remove BcbCounter and BcbExpression
Making these separate types from `CovTerm` and `Expression` was historically very helpful, but now that most of the counter-creation work is handled by `node_flow` they are no longer needed.
1 parent 4170b93 commit 6000d5c

File tree

2 files changed

+18
-70
lines changed

2 files changed

+18
-70
lines changed

compiler/rustc_middle/src/mir/coverage.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,7 @@ impl ConditionId {
7171

7272
/// Enum that can hold a constant zero value, the ID of an physical coverage
7373
/// counter, or the ID of a coverage-counter expression.
74-
///
75-
/// This was originally only used for expression operands (and named `Operand`),
76-
/// but the zero/counter/expression distinction is also useful for representing
77-
/// the value of code/gap mappings, and the true/false arms of branch mappings.
78-
#[derive(Copy, Clone, PartialEq, Eq)]
74+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
7975
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
8076
pub enum CovTerm {
8177
Zero,
@@ -171,7 +167,7 @@ impl Op {
171167
}
172168
}
173169

174-
#[derive(Clone, Debug)]
170+
#[derive(Clone, Debug, PartialEq, Eq)]
175171
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
176172
pub struct Expression {
177173
pub lhs: CovTerm,

compiler/rustc_mir_transform/src/coverage/counters.rs

+16-64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::cmp::Ordering;
2-
use std::fmt::{self, Debug};
32

43
use either::Either;
54
use itertools::Itertools;
@@ -110,39 +109,6 @@ fn transcribe_counters(
110109
new
111110
}
112111

113-
/// The coverage counter or counter expression associated with a particular
114-
/// BCB node or BCB edge.
115-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
116-
enum BcbCounter {
117-
Counter { id: CounterId },
118-
Expression { id: ExpressionId },
119-
}
120-
121-
impl BcbCounter {
122-
fn as_term(&self) -> CovTerm {
123-
match *self {
124-
BcbCounter::Counter { id, .. } => CovTerm::Counter(id),
125-
BcbCounter::Expression { id, .. } => CovTerm::Expression(id),
126-
}
127-
}
128-
}
129-
130-
impl Debug for BcbCounter {
131-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
132-
match self {
133-
Self::Counter { id, .. } => write!(fmt, "Counter({:?})", id.index()),
134-
Self::Expression { id } => write!(fmt, "Expression({:?})", id.index()),
135-
}
136-
}
137-
}
138-
139-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
140-
struct BcbExpression {
141-
lhs: BcbCounter,
142-
op: Op,
143-
rhs: BcbCounter,
144-
}
145-
146112
/// Generates and stores coverage counter and coverage expression information
147113
/// associated with nodes in the coverage graph.
148114
pub(super) struct CoverageCounters {
@@ -152,14 +118,14 @@ pub(super) struct CoverageCounters {
152118
next_counter_id: CounterId,
153119

154120
/// Coverage counters/expressions that are associated with individual BCBs.
155-
node_counters: IndexVec<BasicCoverageBlock, Option<BcbCounter>>,
121+
node_counters: IndexVec<BasicCoverageBlock, Option<CovTerm>>,
156122

157123
/// Table of expression data, associating each expression ID with its
158124
/// corresponding operator (+ or -) and its LHS/RHS operands.
159-
expressions: IndexVec<ExpressionId, BcbExpression>,
125+
expressions: IndexVec<ExpressionId, Expression>,
160126
/// Remember expressions that have already been created (or simplified),
161127
/// so that we don't create unnecessary duplicates.
162-
expressions_memo: FxHashMap<BcbExpression, BcbCounter>,
128+
expressions_memo: FxHashMap<Expression, CovTerm>,
163129
}
164130

165131
impl CoverageCounters {
@@ -174,35 +140,35 @@ impl CoverageCounters {
174140
}
175141

176142
/// Returns the physical counter for the given node, creating it if necessary.
177-
fn ensure_phys_counter(&mut self, bcb: BasicCoverageBlock) -> BcbCounter {
143+
fn ensure_phys_counter(&mut self, bcb: BasicCoverageBlock) -> CovTerm {
178144
let id = *self.phys_counter_for_node.entry(bcb).or_insert_with(|| {
179145
let id = self.next_counter_id;
180146
self.next_counter_id = id + 1;
181147
id
182148
});
183-
BcbCounter::Counter { id }
149+
CovTerm::Counter(id)
184150
}
185151

186-
fn make_expression(&mut self, lhs: BcbCounter, op: Op, rhs: BcbCounter) -> BcbCounter {
187-
let new_expr = BcbExpression { lhs, op, rhs };
188-
*self.expressions_memo.entry(new_expr).or_insert_with(|| {
152+
fn make_expression(&mut self, lhs: CovTerm, op: Op, rhs: CovTerm) -> CovTerm {
153+
let new_expr = Expression { lhs, op, rhs };
154+
*self.expressions_memo.entry(new_expr.clone()).or_insert_with(|| {
189155
let id = self.expressions.push(new_expr);
190-
BcbCounter::Expression { id }
156+
CovTerm::Expression(id)
191157
})
192158
}
193159

194160
/// Creates a counter that is the sum of the given counters.
195161
///
196162
/// Returns `None` if the given list of counters was empty.
197-
fn make_sum(&mut self, counters: &[BcbCounter]) -> Option<BcbCounter> {
163+
fn make_sum(&mut self, counters: &[CovTerm]) -> Option<CovTerm> {
198164
counters
199165
.iter()
200166
.copied()
201167
.reduce(|accum, counter| self.make_expression(accum, Op::Add, counter))
202168
}
203169

204170
/// Creates a counter whose value is `lhs - SUM(rhs)`.
205-
fn make_subtracted_sum(&mut self, lhs: BcbCounter, rhs: &[BcbCounter]) -> BcbCounter {
171+
fn make_subtracted_sum(&mut self, lhs: CovTerm, rhs: &[CovTerm]) -> CovTerm {
206172
let Some(rhs_sum) = self.make_sum(rhs) else { return lhs };
207173
self.make_expression(lhs, Op::Subtract, rhs_sum)
208174
}
@@ -213,7 +179,7 @@ impl CoverageCounters {
213179
num_counters
214180
}
215181

216-
fn set_node_counter(&mut self, bcb: BasicCoverageBlock, counter: BcbCounter) -> BcbCounter {
182+
fn set_node_counter(&mut self, bcb: BasicCoverageBlock, counter: CovTerm) -> CovTerm {
217183
let existing = self.node_counters[bcb].replace(counter);
218184
assert!(
219185
existing.is_none(),
@@ -223,7 +189,7 @@ impl CoverageCounters {
223189
}
224190

225191
pub(super) fn term_for_bcb(&self, bcb: BasicCoverageBlock) -> Option<CovTerm> {
226-
self.node_counters[bcb].map(|counter| counter.as_term())
192+
self.node_counters[bcb]
227193
}
228194

229195
/// Returns an iterator over all the nodes in the coverage graph that
@@ -242,27 +208,13 @@ impl CoverageCounters {
242208
) -> impl Iterator<Item = (BasicCoverageBlock, ExpressionId)> + Captures<'_> {
243209
self.node_counters.iter_enumerated().filter_map(|(bcb, &counter)| match counter {
244210
// Yield the BCB along with its associated expression ID.
245-
Some(BcbCounter::Expression { id }) => Some((bcb, id)),
211+
Some(CovTerm::Expression(id)) => Some((bcb, id)),
246212
// This BCB is associated with a counter or nothing, so skip it.
247-
Some(BcbCounter::Counter { .. }) | None => None,
213+
Some(CovTerm::Counter { .. } | CovTerm::Zero) | None => None,
248214
})
249215
}
250216

251217
pub(super) fn into_expressions(self) -> IndexVec<ExpressionId, Expression> {
252-
let old_len = self.expressions.len();
253-
let expressions = self
254-
.expressions
255-
.into_iter()
256-
.map(|BcbExpression { lhs, op, rhs }| Expression {
257-
lhs: lhs.as_term(),
258-
op,
259-
rhs: rhs.as_term(),
260-
})
261-
.collect::<IndexVec<ExpressionId, _>>();
262-
263-
// Expression IDs are indexes into this vector, so make sure we didn't
264-
// accidentally invalidate them by changing its length.
265-
assert_eq!(old_len, expressions.len());
266-
expressions
218+
self.expressions
267219
}
268220
}

0 commit comments

Comments
 (0)