Skip to content

Commit 8cd9bff

Browse files
committed
!! (WIP) Rename Operand to CovTerm
1 parent e631c2f commit 8cd9bff

File tree

6 files changed

+72
-70
lines changed

6 files changed

+72
-70
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
@@ -2,15 +2,15 @@ use crate::coverageinfo::ffi::{Counter, CounterExpression, ExprKind};
22

33
use rustc_data_structures::fx::FxIndexSet;
44
use rustc_index::IndexVec;
5-
use rustc_middle::mir::coverage::{CodeRegion, CounterId, ExpressionId, Op, Operand};
5+
use rustc_middle::mir::coverage::{CodeRegion, CounterId, CovTerm, ExpressionId, Op};
66
use rustc_middle::ty::Instance;
77
use rustc_middle::ty::TyCtxt;
88

99
#[derive(Clone, Debug, PartialEq)]
1010
pub struct Expression {
11-
lhs: Operand,
11+
lhs: CovTerm,
1212
op: Op,
13-
rhs: Operand,
13+
rhs: CovTerm,
1414
code_regions: Vec<CodeRegion>,
1515
}
1616

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

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

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

187187
// After the above simplifications, if both operands are zero, then
188188
// we know that this expression is always zero too.
189-
if let Expression { lhs: Operand::Zero, rhs: Operand::Zero, .. } = expression {
189+
if let Expression { lhs: CovTerm::Zero, rhs: CovTerm::Zero, .. } = expression {
190190
zero_expressions.insert(id);
191191
}
192192
}
@@ -257,12 +257,12 @@ impl<'tcx> FunctionCoverage<'tcx> {
257257
CounterExpression::DUMMY
258258
}
259259
&Some(Expression { lhs, op, rhs, .. }) => CounterExpression::new(
260-
Counter::from_operand(lhs),
260+
Counter::from_term(lhs),
261261
match op {
262262
Op::Add => ExprKind::Add,
263263
Op::Subtract => ExprKind::Subtract,
264264
},
265-
Counter::from_operand(rhs),
265+
Counter::from_term(rhs),
266266
),
267267
})
268268
.collect::<Vec<_>>()

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"),
@@ -69,9 +71,9 @@ pub enum CoverageKind {
6971
/// ID of this coverage-counter expression within its enclosing function.
7072
/// Other expressions in the same function can refer to it as an operand.
7173
id: ExpressionId,
72-
lhs: Operand,
74+
lhs: CovTerm,
7375
op: Op,
74-
rhs: Operand,
76+
rhs: CovTerm,
7577
},
7678
Unreachable,
7779
}

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
@@ -161,22 +161,22 @@ impl CoverageCounters {
161161
&mut self,
162162
bcb: BasicCoverageBlock,
163163
counter_kind: BcbCounter,
164-
) -> Result<Operand, Error> {
164+
) -> Result<CovTerm, Error> {
165165
debug_assert!(
166166
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
167167
// have an expression (to be injected into an existing `BasicBlock` represented by this
168168
// `BasicCoverageBlock`).
169169
counter_kind.is_expression() || !self.bcb_has_incoming_edge_counters.contains(bcb),
170170
"attempt to add a `Counter` to a BCB target with existing incoming edge counters"
171171
);
172-
let operand = counter_kind.as_operand();
172+
let term = counter_kind.as_term();
173173
if let Some(replaced) = self.bcb_counters[bcb].replace(counter_kind) {
174174
Error::from_string(format!(
175175
"attempt to set a BasicCoverageBlock coverage counter more than once; \
176176
{bcb:?} already had counter {replaced:?}",
177177
))
178178
} else {
179-
Ok(operand)
179+
Ok(term)
180180
}
181181
}
182182

@@ -185,7 +185,7 @@ impl CoverageCounters {
185185
from_bcb: BasicCoverageBlock,
186186
to_bcb: BasicCoverageBlock,
187187
counter_kind: BcbCounter,
188-
) -> Result<Operand, Error> {
188+
) -> Result<CovTerm, Error> {
189189
if level_enabled!(tracing::Level::DEBUG) {
190190
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
191191
// have an expression (to be injected into an existing `BasicBlock` represented by this
@@ -198,14 +198,14 @@ impl CoverageCounters {
198198
}
199199
}
200200
self.bcb_has_incoming_edge_counters.insert(to_bcb);
201-
let operand = counter_kind.as_operand();
201+
let term = counter_kind.as_term();
202202
if let Some(replaced) = self.bcb_edge_counters.insert((from_bcb, to_bcb), counter_kind) {
203203
Error::from_string(format!(
204204
"attempt to set an edge counter more than once; from_bcb: \
205205
{from_bcb:?} already had counter {replaced:?}",
206206
))
207207
} else {
208-
Ok(operand)
208+
Ok(term)
209209
}
210210
}
211211

@@ -310,7 +310,7 @@ impl<'a> MakeBcbCounters<'a> {
310310
&mut self,
311311
traversal: &mut TraverseCoverageGraphWithLoops,
312312
branching_bcb: BasicCoverageBlock,
313-
branching_counter_operand: Operand,
313+
branching_counter_operand: CovTerm,
314314
) -> Result<(), Error> {
315315
let branches = self.bcb_branches(branching_bcb);
316316
debug!(
@@ -362,7 +362,7 @@ impl<'a> MakeBcbCounters<'a> {
362362
" [new intermediate expression: {}]",
363363
self.format_counter(&intermediate_expression)
364364
);
365-
let intermediate_expression_operand = intermediate_expression.as_operand();
365+
let intermediate_expression_operand = intermediate_expression.as_term();
366366
self.coverage_counters.intermediate_expressions.push(intermediate_expression);
367367
some_sumup_counter_operand.replace(intermediate_expression_operand);
368368
}
@@ -395,15 +395,15 @@ impl<'a> MakeBcbCounters<'a> {
395395
Ok(())
396396
}
397397

398-
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> Result<Operand, Error> {
398+
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> Result<CovTerm, Error> {
399399
self.recursive_get_or_make_counter_operand(bcb, 1)
400400
}
401401

402402
fn recursive_get_or_make_counter_operand(
403403
&mut self,
404404
bcb: BasicCoverageBlock,
405405
debug_indent_level: usize,
406-
) -> Result<Operand, Error> {
406+
) -> Result<CovTerm, Error> {
407407
// If the BCB already has a counter, return it.
408408
if let Some(counter_kind) = &self.coverage_counters.bcb_counters[bcb] {
409409
debug!(
@@ -412,7 +412,7 @@ impl<'a> MakeBcbCounters<'a> {
412412
bcb,
413413
self.format_counter(counter_kind),
414414
);
415-
return Ok(counter_kind.as_operand());
415+
return Ok(counter_kind.as_term());
416416
}
417417

418418
// A BCB with only one incoming edge gets a simple `Counter` (via `make_counter()`).
@@ -477,7 +477,7 @@ impl<'a> MakeBcbCounters<'a> {
477477
NESTED_INDENT.repeat(debug_indent_level),
478478
self.format_counter(&intermediate_expression)
479479
);
480-
let intermediate_expression_operand = intermediate_expression.as_operand();
480+
let intermediate_expression_operand = intermediate_expression.as_term();
481481
self.coverage_counters.intermediate_expressions.push(intermediate_expression);
482482
some_sumup_edge_counter_operand.replace(intermediate_expression_operand);
483483
}
@@ -501,7 +501,7 @@ impl<'a> MakeBcbCounters<'a> {
501501
&mut self,
502502
from_bcb: BasicCoverageBlock,
503503
to_bcb: BasicCoverageBlock,
504-
) -> Result<Operand, Error> {
504+
) -> Result<CovTerm, Error> {
505505
self.recursive_get_or_make_edge_counter_operand(from_bcb, to_bcb, 1)
506506
}
507507

@@ -510,7 +510,7 @@ impl<'a> MakeBcbCounters<'a> {
510510
from_bcb: BasicCoverageBlock,
511511
to_bcb: BasicCoverageBlock,
512512
debug_indent_level: usize,
513-
) -> Result<Operand, Error> {
513+
) -> Result<CovTerm, Error> {
514514
// If the source BCB has only one successor (assumed to be the given target), an edge
515515
// counter is unnecessary. Just get or make a counter for the source BCB.
516516
let successors = self.bcb_successors(from_bcb).iter();
@@ -529,7 +529,7 @@ impl<'a> MakeBcbCounters<'a> {
529529
to_bcb,
530530
self.format_counter(counter_kind)
531531
);
532-
return Ok(counter_kind.as_operand());
532+
return Ok(counter_kind.as_term());
533533
}
534534

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

0 commit comments

Comments
 (0)