1
Fork 0

Replace ExpressionOperandId with enum Operand

Because the three kinds of operand are now distinguished explicitly, we no
longer need fiddly code to disambiguate counter IDs and expression IDs based on
the total number of counters/expressions in a function.

This does increase the size of operands from 4 bytes to 8 bytes, but that
shouldn't be a big deal since they are mostly stored inside boxed structures,
and the current coverage code is not particularly size-optimized anyway.
This commit is contained in:
Zalathar 2023-06-26 22:28:48 +10:00
parent 5a808d40f4
commit 1a014d42f4
11 changed files with 96 additions and 147 deletions

View file

@ -47,39 +47,24 @@ impl CoverageVisitor {
/// final computed number of counters should be the number of all `CoverageKind::Counter`
/// statements in the MIR *plus one* for the implicit `ZERO` counter.
#[inline(always)]
fn update_num_counters(&mut self, counter_id: u32) {
fn update_num_counters(&mut self, counter_id: CounterValueReference) {
let counter_id = counter_id.as_u32();
self.info.num_counters = std::cmp::max(self.info.num_counters, counter_id + 1);
}
/// Computes an expression index for each expression ID, and updates `num_expressions` to the
/// maximum encountered index plus 1.
#[inline(always)]
fn update_num_expressions(&mut self, expression_id: u32) {
let expression_index = u32::MAX - expression_id;
fn update_num_expressions(&mut self, expression_id: InjectedExpressionId) {
let expression_index = u32::MAX - expression_id.as_u32();
self.info.num_expressions = std::cmp::max(self.info.num_expressions, expression_index + 1);
}
fn update_from_expression_operand(&mut self, operand_id: u32) {
if operand_id >= self.info.num_counters {
let operand_as_expression_index = u32::MAX - operand_id;
if operand_as_expression_index >= self.info.num_expressions {
// The operand ID is outside the known range of counter IDs and also outside the
// known range of expression IDs. In either case, the result of a missing operand
// (if and when used in an expression) will be zero, so from a computation
// perspective, it doesn't matter whether it is interpreted as a counter or an
// expression.
//
// However, the `num_counters` and `num_expressions` query results are used to
// allocate arrays when generating the coverage map (during codegen), so choose
// the type that grows either `num_counters` or `num_expressions` the least.
if operand_id - self.info.num_counters
< operand_as_expression_index - self.info.num_expressions
{
self.update_num_counters(operand_id)
} else {
self.update_num_expressions(operand_id)
}
}
fn update_from_expression_operand(&mut self, operand: Operand) {
match operand {
Operand::Counter(id) => self.update_num_counters(id),
Operand::Expression(id) => self.update_num_expressions(id),
Operand::Zero => {}
}
}
@ -100,19 +85,15 @@ impl CoverageVisitor {
if self.add_missing_operands {
match coverage.kind {
CoverageKind::Expression { lhs, rhs, .. } => {
self.update_from_expression_operand(u32::from(lhs));
self.update_from_expression_operand(u32::from(rhs));
self.update_from_expression_operand(lhs);
self.update_from_expression_operand(rhs);
}
_ => {}
}
} else {
match coverage.kind {
CoverageKind::Counter { id, .. } => {
self.update_num_counters(u32::from(id));
}
CoverageKind::Expression { id, .. } => {
self.update_num_expressions(u32::from(id));
}
CoverageKind::Counter { id, .. } => self.update_num_counters(id),
CoverageKind::Expression { id, .. } => self.update_num_expressions(id),
_ => {}
}
}