Skip to content

Commit 6bfeff3

Browse files
committed
coverage: Rename Operand to CovTerm
Later patches in this PR will use `CovTerm` to represent things that are not expression operands.
1 parent 851c319 commit 6bfeff3

File tree

4 files changed

+51
-49
lines changed

4 files changed

+51
-49
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_middle::mir::coverage::{CounterId, ExpressionId, Operand};
1+
use rustc_middle::mir::coverage::{CounterId, CovTerm, ExpressionId};
22

33
/// Must match the layout of `LLVMRustCounterKind`.
44
#[derive(Copy, Clone, Debug)]
@@ -43,11 +43,11 @@ impl Counter {
4343
Self { kind: CounterKind::Expression, id: expression_id.as_u32() }
4444
}
4545

46-
pub(crate) fn from_operand(operand: Operand) -> Self {
47-
match operand {
48-
Operand::Zero => Self::ZERO,
49-
Operand::Counter(id) => Self::counter_value_reference(id),
50-
Operand::Expression(id) => Self::expression(id),
46+
pub(crate) fn from_term(term: CovTerm) -> Self {
47+
match term {
48+
CovTerm::Zero => Self::ZERO,
49+
CovTerm::Counter(id) => Self::counter_value_reference(id),
50+
CovTerm::Expression(id) => Self::expression(id),
5151
}
5252
}
5353
}

compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use crate::coverageinfo::ffi::{Counter, CounterExpression, ExprKind};
33
use rustc_data_structures::fx::FxIndexSet;
44
use rustc_index::IndexVec;
55
use rustc_middle::mir::coverage::{
6-
CodeRegion, CounterId, ExpressionId, FunctionCoverageInfo, Op, Operand,
6+
CodeRegion, CounterId, CovTerm, ExpressionId, FunctionCoverageInfo, Op,
77
};
88
use rustc_middle::ty::Instance;
99

1010
#[derive(Clone, Debug, PartialEq)]
1111
pub struct Expression {
12-
lhs: Operand,
12+
lhs: CovTerm,
1313
op: Op,
14-
rhs: Operand,
14+
rhs: CovTerm,
1515
code_regions: Vec<CodeRegion>,
1616
}
1717

@@ -101,15 +101,15 @@ impl<'tcx> FunctionCoverage<'tcx> {
101101
/// code regions mapped to that expression.
102102
///
103103
/// Both counters and "counter expressions" (or simply, "expressions") can be operands in other
104-
/// expressions. These are tracked as separate variants of `Operand`, so there is no ambiguity
104+
/// expressions. These are tracked as separate variants of `CovTerm`, so there is no ambiguity
105105
/// between operands that are counter IDs and operands that are expression IDs.
106106
#[instrument(level = "debug", skip(self))]
107107
pub(crate) fn add_counter_expression(
108108
&mut self,
109109
expression_id: ExpressionId,
110-
lhs: Operand,
110+
lhs: CovTerm,
111111
op: Op,
112-
rhs: Operand,
112+
rhs: CovTerm,
113113
code_regions: &[CodeRegion],
114114
) {
115115
debug_assert!(
@@ -151,7 +151,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
151151
// The set of expressions that either were optimized out entirely, or
152152
// have zero as both of their operands, and will therefore always have
153153
// a value of zero. Other expressions that refer to these as operands
154-
// can have those operands replaced with `Operand::Zero`.
154+
// can have those operands replaced with `CovTerm::Zero`.
155155
let mut zero_expressions = FxIndexSet::default();
156156

157157
// For each expression, perform simplifications based on lower-numbered
@@ -168,10 +168,10 @@ impl<'tcx> FunctionCoverage<'tcx> {
168168
};
169169

170170
// If an operand refers to an expression that is always zero, then
171-
// that operand can be replaced with `Operand::Zero`.
172-
let maybe_set_operand_to_zero = |operand: &mut Operand| match &*operand {
173-
Operand::Expression(id) if zero_expressions.contains(id) => {
174-
*operand = Operand::Zero;
171+
// that operand can be replaced with `CovTerm::Zero`.
172+
let maybe_set_operand_to_zero = |operand: &mut CovTerm| match &*operand {
173+
CovTerm::Expression(id) if zero_expressions.contains(id) => {
174+
*operand = CovTerm::Zero;
175175
}
176176
_ => (),
177177
};
@@ -181,13 +181,13 @@ impl<'tcx> FunctionCoverage<'tcx> {
181181
// Coverage counter values cannot be negative, so if an expression
182182
// involves subtraction from zero, assume that its RHS must also be zero.
183183
// (Do this after simplifications that could set the LHS to zero.)
184-
if let Expression { lhs: Operand::Zero, op: Op::Subtract, .. } = expression {
185-
expression.rhs = Operand::Zero;
184+
if let Expression { lhs: CovTerm::Zero, op: Op::Subtract, .. } = expression {
185+
expression.rhs = CovTerm::Zero;
186186
}
187187

188188
// After the above simplifications, if both operands are zero, then
189189
// we know that this expression is always zero too.
190-
if let Expression { lhs: Operand::Zero, rhs: Operand::Zero, .. } = expression {
190+
if let Expression { lhs: CovTerm::Zero, rhs: CovTerm::Zero, .. } = expression {
191191
zero_expressions.insert(id);
192192
}
193193
}
@@ -254,12 +254,12 @@ impl<'tcx> FunctionCoverage<'tcx> {
254254
&Some(Expression { lhs, op, rhs, .. }) => {
255255
// Convert the operands and operator as normal.
256256
CounterExpression::new(
257-
Counter::from_operand(lhs),
257+
Counter::from_term(lhs),
258258
match op {
259259
Op::Add => ExprKind::Add,
260260
Op::Subtract => ExprKind::Subtract,
261261
},
262-
Counter::from_operand(rhs),
262+
Counter::from_term(rhs),
263263
)
264264
}
265265
})

compiler/rustc_middle/src/mir/coverage.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,21 @@ impl ExpressionId {
3535
pub const START: Self = Self::from_u32(0);
3636
}
3737

38-
/// Operand of a coverage-counter expression.
38+
/// Enum that can hold a constant zero value, the ID of an physical coverage
39+
/// counter, or the ID of a coverage-counter expression.
3940
///
40-
/// Operands can be a constant zero value, an actual coverage counter, or another
41-
/// expression. Counter/expression operands are referred to by ID.
41+
/// This was originally only used for expression operands (and named `Operand`),
42+
/// but the zero/counter/expression distinction is also useful for representing
43+
/// the value of code/gap mappings, and the true/false arms of branch mappings.
4244
#[derive(Copy, Clone, PartialEq, Eq)]
4345
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
44-
pub enum Operand {
46+
pub enum CovTerm {
4547
Zero,
4648
Counter(CounterId),
4749
Expression(ExpressionId),
4850
}
4951

50-
impl Debug for Operand {
52+
impl Debug for CovTerm {
5153
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
5254
match self {
5355
Self::Zero => write!(f, "Zero"),
@@ -68,9 +70,9 @@ pub enum CoverageKind {
6870
/// ID of this coverage-counter expression within its enclosing function.
6971
/// Other expressions in the same function can refer to it as an operand.
7072
id: ExpressionId,
71-
lhs: Operand,
73+
lhs: CovTerm,
7274
op: Op,
73-
rhs: Operand,
75+
rhs: CovTerm,
7476
},
7577
Unreachable,
7678
}

compiler/rustc_mir_transform/src/coverage/counters.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ const NESTED_INDENT: &str = " ";
1919
#[derive(Clone)]
2020
pub(super) enum BcbCounter {
2121
Counter { id: CounterId },
22-
Expression { id: ExpressionId, lhs: Operand, op: Op, rhs: Operand },
22+
Expression { id: ExpressionId, lhs: CovTerm, op: Op, rhs: CovTerm },
2323
}
2424

2525
impl BcbCounter {
2626
fn is_expression(&self) -> bool {
2727
matches!(self, Self::Expression { .. })
2828
}
2929

30-
pub(super) fn as_operand(&self) -> Operand {
30+
pub(super) fn as_term(&self) -> CovTerm {
3131
match *self {
32-
BcbCounter::Counter { id, .. } => Operand::Counter(id),
33-
BcbCounter::Expression { id, .. } => Operand::Expression(id),
32+
BcbCounter::Counter { id, .. } => CovTerm::Counter(id),
33+
BcbCounter::Expression { id, .. } => CovTerm::Expression(id),
3434
}
3535
}
3636
}
@@ -106,7 +106,7 @@ impl CoverageCounters {
106106
BcbCounter::Counter { id }
107107
}
108108

109-
fn make_expression(&mut self, lhs: Operand, op: Op, rhs: Operand) -> BcbCounter {
109+
fn make_expression(&mut self, lhs: CovTerm, op: Op, rhs: CovTerm) -> BcbCounter {
110110
let id = self.next_expression();
111111
BcbCounter::Expression { id, lhs, op, rhs }
112112
}
@@ -138,22 +138,22 @@ impl CoverageCounters {
138138
&mut self,
139139
bcb: BasicCoverageBlock,
140140
counter_kind: BcbCounter,
141-
) -> Result<Operand, Error> {
141+
) -> Result<CovTerm, Error> {
142142
debug_assert!(
143143
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
144144
// have an expression (to be injected into an existing `BasicBlock` represented by this
145145
// `BasicCoverageBlock`).
146146
counter_kind.is_expression() || !self.bcb_has_incoming_edge_counters.contains(bcb),
147147
"attempt to add a `Counter` to a BCB target with existing incoming edge counters"
148148
);
149-
let operand = counter_kind.as_operand();
149+
let term = counter_kind.as_term();
150150
if let Some(replaced) = self.bcb_counters[bcb].replace(counter_kind) {
151151
Error::from_string(format!(
152152
"attempt to set a BasicCoverageBlock coverage counter more than once; \
153153
{bcb:?} already had counter {replaced:?}",
154154
))
155155
} else {
156-
Ok(operand)
156+
Ok(term)
157157
}
158158
}
159159

@@ -162,7 +162,7 @@ impl CoverageCounters {
162162
from_bcb: BasicCoverageBlock,
163163
to_bcb: BasicCoverageBlock,
164164
counter_kind: BcbCounter,
165-
) -> Result<Operand, Error> {
165+
) -> Result<CovTerm, Error> {
166166
if level_enabled!(tracing::Level::DEBUG) {
167167
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
168168
// have an expression (to be injected into an existing `BasicBlock` represented by this
@@ -175,14 +175,14 @@ impl CoverageCounters {
175175
}
176176
}
177177
self.bcb_has_incoming_edge_counters.insert(to_bcb);
178-
let operand = counter_kind.as_operand();
178+
let term = counter_kind.as_term();
179179
if let Some(replaced) = self.bcb_edge_counters.insert((from_bcb, to_bcb), counter_kind) {
180180
Error::from_string(format!(
181181
"attempt to set an edge counter more than once; from_bcb: \
182182
{from_bcb:?} already had counter {replaced:?}",
183183
))
184184
} else {
185-
Ok(operand)
185+
Ok(term)
186186
}
187187
}
188188

@@ -284,7 +284,7 @@ impl<'a> MakeBcbCounters<'a> {
284284
&mut self,
285285
traversal: &mut TraverseCoverageGraphWithLoops,
286286
branching_bcb: BasicCoverageBlock,
287-
branching_counter_operand: Operand,
287+
branching_counter_operand: CovTerm,
288288
) -> Result<(), Error> {
289289
let branches = self.bcb_branches(branching_bcb);
290290
debug!(
@@ -332,7 +332,7 @@ impl<'a> MakeBcbCounters<'a> {
332332
sumup_counter_operand,
333333
);
334334
debug!(" [new intermediate expression: {:?}]", intermediate_expression);
335-
let intermediate_expression_operand = intermediate_expression.as_operand();
335+
let intermediate_expression_operand = intermediate_expression.as_term();
336336
self.coverage_counters.intermediate_expressions.push(intermediate_expression);
337337
some_sumup_counter_operand.replace(intermediate_expression_operand);
338338
}
@@ -364,15 +364,15 @@ impl<'a> MakeBcbCounters<'a> {
364364
Ok(())
365365
}
366366

367-
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> Result<Operand, Error> {
367+
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> Result<CovTerm, Error> {
368368
self.recursive_get_or_make_counter_operand(bcb, 1)
369369
}
370370

371371
fn recursive_get_or_make_counter_operand(
372372
&mut self,
373373
bcb: BasicCoverageBlock,
374374
debug_indent_level: usize,
375-
) -> Result<Operand, Error> {
375+
) -> Result<CovTerm, Error> {
376376
// If the BCB already has a counter, return it.
377377
if let Some(counter_kind) = &self.coverage_counters.bcb_counters[bcb] {
378378
debug!(
@@ -381,7 +381,7 @@ impl<'a> MakeBcbCounters<'a> {
381381
bcb,
382382
counter_kind,
383383
);
384-
return Ok(counter_kind.as_operand());
384+
return Ok(counter_kind.as_term());
385385
}
386386

387387
// A BCB with only one incoming edge gets a simple `Counter` (via `make_counter()`).
@@ -445,7 +445,7 @@ impl<'a> MakeBcbCounters<'a> {
445445
NESTED_INDENT.repeat(debug_indent_level),
446446
intermediate_expression
447447
);
448-
let intermediate_expression_operand = intermediate_expression.as_operand();
448+
let intermediate_expression_operand = intermediate_expression.as_term();
449449
self.coverage_counters.intermediate_expressions.push(intermediate_expression);
450450
some_sumup_edge_counter_operand.replace(intermediate_expression_operand);
451451
}
@@ -468,7 +468,7 @@ impl<'a> MakeBcbCounters<'a> {
468468
&mut self,
469469
from_bcb: BasicCoverageBlock,
470470
to_bcb: BasicCoverageBlock,
471-
) -> Result<Operand, Error> {
471+
) -> Result<CovTerm, Error> {
472472
self.recursive_get_or_make_edge_counter_operand(from_bcb, to_bcb, 1)
473473
}
474474

@@ -477,7 +477,7 @@ impl<'a> MakeBcbCounters<'a> {
477477
from_bcb: BasicCoverageBlock,
478478
to_bcb: BasicCoverageBlock,
479479
debug_indent_level: usize,
480-
) -> Result<Operand, Error> {
480+
) -> Result<CovTerm, Error> {
481481
// If the source BCB has only one successor (assumed to be the given target), an edge
482482
// counter is unnecessary. Just get or make a counter for the source BCB.
483483
let successors = self.bcb_successors(from_bcb).iter();
@@ -496,7 +496,7 @@ impl<'a> MakeBcbCounters<'a> {
496496
to_bcb,
497497
counter_kind
498498
);
499-
return Ok(counter_kind.as_operand());
499+
return Ok(counter_kind.as_term());
500500
}
501501

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

0 commit comments

Comments
 (0)