Skip to content

Commit bc5a5c2

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 d0787a7 commit bc5a5c2

File tree

5 files changed

+61
-59
lines changed

5 files changed

+61
-59
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: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ use std::fmt::{self, Debug};
2121
#[derive(Clone)]
2222
pub(super) enum BcbCounter {
2323
Counter { id: CounterId },
24-
Expression { id: ExpressionId, lhs: Operand, op: Op, rhs: Operand },
24+
Expression { id: ExpressionId, lhs: CovTerm, op: Op, rhs: CovTerm },
2525
}
2626

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

32-
pub(super) fn as_operand(&self) -> Operand {
32+
pub(super) fn as_term(&self) -> CovTerm {
3333
match *self {
34-
BcbCounter::Counter { id, .. } => Operand::Counter(id),
35-
BcbCounter::Expression { id, .. } => Operand::Expression(id),
34+
BcbCounter::Counter { id, .. } => CovTerm::Counter(id),
35+
BcbCounter::Expression { id, .. } => CovTerm::Expression(id),
3636
}
3737
}
3838
}
@@ -126,9 +126,9 @@ impl CoverageCounters {
126126

127127
fn make_expression<F>(
128128
&mut self,
129-
lhs: Operand,
129+
lhs: CovTerm,
130130
op: Op,
131-
rhs: Operand,
131+
rhs: CovTerm,
132132
debug_block_label_fn: F,
133133
) -> BcbCounter
134134
where
@@ -169,22 +169,22 @@ impl CoverageCounters {
169169
&mut self,
170170
bcb: BasicCoverageBlock,
171171
counter_kind: BcbCounter,
172-
) -> Result<Operand, Error> {
172+
) -> Result<CovTerm, Error> {
173173
debug_assert!(
174174
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
175175
// have an expression (to be injected into an existing `BasicBlock` represented by this
176176
// `BasicCoverageBlock`).
177177
counter_kind.is_expression() || !self.bcb_has_incoming_edge_counters.contains(bcb),
178178
"attempt to add a `Counter` to a BCB target with existing incoming edge counters"
179179
);
180-
let operand = counter_kind.as_operand();
180+
let term = counter_kind.as_term();
181181
if let Some(replaced) = self.bcb_counters[bcb].replace(counter_kind) {
182182
Error::from_string(format!(
183183
"attempt to set a BasicCoverageBlock coverage counter more than once; \
184184
{bcb:?} already had counter {replaced:?}",
185185
))
186186
} else {
187-
Ok(operand)
187+
Ok(term)
188188
}
189189
}
190190

@@ -193,7 +193,7 @@ impl CoverageCounters {
193193
from_bcb: BasicCoverageBlock,
194194
to_bcb: BasicCoverageBlock,
195195
counter_kind: BcbCounter,
196-
) -> Result<Operand, Error> {
196+
) -> Result<CovTerm, Error> {
197197
if level_enabled!(tracing::Level::DEBUG) {
198198
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
199199
// have an expression (to be injected into an existing `BasicBlock` represented by this
@@ -206,14 +206,14 @@ impl CoverageCounters {
206206
}
207207
}
208208
self.bcb_has_incoming_edge_counters.insert(to_bcb);
209-
let operand = counter_kind.as_operand();
209+
let term = counter_kind.as_term();
210210
if let Some(replaced) = self.bcb_edge_counters.insert((from_bcb, to_bcb), counter_kind) {
211211
Error::from_string(format!(
212212
"attempt to set an edge counter more than once; from_bcb: \
213213
{from_bcb:?} already had counter {replaced:?}",
214214
))
215215
} else {
216-
Ok(operand)
216+
Ok(term)
217217
}
218218
}
219219

@@ -318,7 +318,7 @@ impl<'a> MakeBcbCounters<'a> {
318318
&mut self,
319319
traversal: &mut TraverseCoverageGraphWithLoops,
320320
branching_bcb: BasicCoverageBlock,
321-
branching_counter_operand: Operand,
321+
branching_counter_operand: CovTerm,
322322
) -> Result<(), Error> {
323323
let branches = self.bcb_branches(branching_bcb);
324324
debug!(
@@ -370,7 +370,7 @@ impl<'a> MakeBcbCounters<'a> {
370370
" [new intermediate expression: {}]",
371371
self.format_counter(&intermediate_expression)
372372
);
373-
let intermediate_expression_operand = intermediate_expression.as_operand();
373+
let intermediate_expression_operand = intermediate_expression.as_term();
374374
self.coverage_counters.intermediate_expressions.push(intermediate_expression);
375375
some_sumup_counter_operand.replace(intermediate_expression_operand);
376376
}
@@ -403,15 +403,15 @@ impl<'a> MakeBcbCounters<'a> {
403403
Ok(())
404404
}
405405

406-
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> Result<Operand, Error> {
406+
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> Result<CovTerm, Error> {
407407
self.recursive_get_or_make_counter_operand(bcb, 1)
408408
}
409409

410410
fn recursive_get_or_make_counter_operand(
411411
&mut self,
412412
bcb: BasicCoverageBlock,
413413
debug_indent_level: usize,
414-
) -> Result<Operand, Error> {
414+
) -> Result<CovTerm, Error> {
415415
// If the BCB already has a counter, return it.
416416
if let Some(counter_kind) = &self.coverage_counters.bcb_counters[bcb] {
417417
debug!(
@@ -420,7 +420,7 @@ impl<'a> MakeBcbCounters<'a> {
420420
bcb,
421421
self.format_counter(counter_kind),
422422
);
423-
return Ok(counter_kind.as_operand());
423+
return Ok(counter_kind.as_term());
424424
}
425425

426426
// A BCB with only one incoming edge gets a simple `Counter` (via `make_counter()`).
@@ -485,7 +485,7 @@ impl<'a> MakeBcbCounters<'a> {
485485
NESTED_INDENT.repeat(debug_indent_level),
486486
self.format_counter(&intermediate_expression)
487487
);
488-
let intermediate_expression_operand = intermediate_expression.as_operand();
488+
let intermediate_expression_operand = intermediate_expression.as_term();
489489
self.coverage_counters.intermediate_expressions.push(intermediate_expression);
490490
some_sumup_edge_counter_operand.replace(intermediate_expression_operand);
491491
}
@@ -509,7 +509,7 @@ impl<'a> MakeBcbCounters<'a> {
509509
&mut self,
510510
from_bcb: BasicCoverageBlock,
511511
to_bcb: BasicCoverageBlock,
512-
) -> Result<Operand, Error> {
512+
) -> Result<CovTerm, Error> {
513513
self.recursive_get_or_make_edge_counter_operand(from_bcb, to_bcb, 1)
514514
}
515515

@@ -518,7 +518,7 @@ impl<'a> MakeBcbCounters<'a> {
518518
from_bcb: BasicCoverageBlock,
519519
to_bcb: BasicCoverageBlock,
520520
debug_indent_level: usize,
521-
) -> Result<Operand, Error> {
521+
) -> Result<CovTerm, Error> {
522522
// If the source BCB has only one successor (assumed to be the given target), an edge
523523
// counter is unnecessary. Just get or make a counter for the source BCB.
524524
let successors = self.bcb_successors(from_bcb).iter();
@@ -537,7 +537,7 @@ impl<'a> MakeBcbCounters<'a> {
537537
to_bcb,
538538
self.format_counter(counter_kind)
539539
);
540-
return Ok(counter_kind.as_operand());
540+
return Ok(counter_kind.as_term());
541541
}
542542

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

compiler/rustc_mir_transform/src/coverage/debug.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl Default for ExpressionFormat {
247247
}
248248
}
249249

250-
/// If enabled, this struct maintains a map from `BcbCounter` IDs (as `Operand`) to
250+
/// If enabled, this struct maintains a map from `BcbCounter` IDs (as `CovTerm`) to
251251
/// the `BcbCounter` data and optional label (normally, the counter's associated
252252
/// `BasicCoverageBlock` format string, if any).
253253
///
@@ -259,7 +259,7 @@ impl Default for ExpressionFormat {
259259
/// `DebugCounters` supports a recursive rendering of `Expression` counters, so they can be
260260
/// presented as nested expressions such as `(bcb3 - (bcb0 + bcb1))`.
261261
pub(super) struct DebugCounters {
262-
some_counters: Option<FxHashMap<Operand, DebugCounter>>,
262+
some_counters: Option<FxHashMap<CovTerm, DebugCounter>>,
263263
}
264264

265265
impl DebugCounters {
@@ -278,7 +278,7 @@ impl DebugCounters {
278278

279279
pub fn add_counter(&mut self, counter_kind: &BcbCounter, some_block_label: Option<String>) {
280280
if let Some(counters) = &mut self.some_counters {
281-
let id = counter_kind.as_operand();
281+
let id = counter_kind.as_term();
282282
counters
283283
.try_insert(id, DebugCounter::new(counter_kind.clone(), some_block_label))
284284
.expect("attempt to add the same counter_kind to DebugCounters more than once");
@@ -317,7 +317,7 @@ impl DebugCounters {
317317
}
318318
}
319319

320-
let id = counter_kind.as_operand();
320+
let id = counter_kind.as_term();
321321
if self.some_counters.is_some() && (counter_format.block || !counter_format.id) {
322322
let counters = self.some_counters.as_ref().unwrap();
323323
if let Some(DebugCounter { some_block_label: Some(block_label), .. }) =
@@ -333,8 +333,8 @@ impl DebugCounters {
333333
format!("#{:?}", id)
334334
}
335335

336-
fn format_operand(&self, operand: Operand) -> String {
337-
if matches!(operand, Operand::Zero) {
336+
fn format_operand(&self, operand: CovTerm) -> String {
337+
if matches!(operand, CovTerm::Zero) {
338338
return String::from("0");
339339
}
340340
if let Some(counters) = &self.some_counters {
@@ -480,7 +480,7 @@ impl GraphvizData {
480480
/// _not_ used are retained in the `unused_expressions` Vec, to be included in debug output (logs
481481
/// and/or a `CoverageGraph` graphviz output).
482482
pub(super) struct UsedExpressions {
483-
some_used_expression_operands: Option<FxHashMap<Operand, Vec<ExpressionId>>>,
483+
some_used_expression_operands: Option<FxHashMap<CovTerm, Vec<ExpressionId>>>,
484484
some_unused_expressions:
485485
Option<Vec<(BcbCounter, Option<BasicCoverageBlock>, BasicCoverageBlock)>>,
486486
}
@@ -511,7 +511,7 @@ impl UsedExpressions {
511511

512512
pub fn expression_is_used(&self, expression: &BcbCounter) -> bool {
513513
if let Some(used_expression_operands) = self.some_used_expression_operands.as_ref() {
514-
used_expression_operands.contains_key(&expression.as_operand())
514+
used_expression_operands.contains_key(&expression.as_term())
515515
} else {
516516
false
517517
}
@@ -524,7 +524,7 @@ impl UsedExpressions {
524524
target_bcb: BasicCoverageBlock,
525525
) {
526526
if let Some(used_expression_operands) = self.some_used_expression_operands.as_ref() {
527-
if !used_expression_operands.contains_key(&expression.as_operand()) {
527+
if !used_expression_operands.contains_key(&expression.as_term()) {
528528
self.some_unused_expressions.as_mut().unwrap().push((
529529
expression.clone(),
530530
edge_from_bcb,

0 commit comments

Comments
 (0)