Make coverage expression IDs count up from 0, not down from u32::MAX

Operand types are now tracked explicitly, so there is no need for expression
IDs to avoid counter IDs by descending from `u32::MAX`. Instead they can just
count up from 0, and can be used directly as indices when necessary.
This commit is contained in:
Zalathar 2023-06-29 12:14:04 +10:00
parent 1a014d42f4
commit f103db894f
9 changed files with 49 additions and 71 deletions

View file

@ -17,7 +17,7 @@ use rustc_middle::mir::coverage::*;
pub(super) struct CoverageCounters {
function_source_hash: u64,
next_counter_id: u32,
num_expressions: u32,
next_expression_id: ExpressionId,
pub debug_counters: DebugCounters,
}
@ -26,7 +26,7 @@ impl CoverageCounters {
Self {
function_source_hash,
next_counter_id: CounterValueReference::START.as_u32(),
num_expressions: 0,
next_expression_id: ExpressionId::START,
debug_counters: DebugCounters::new(),
}
}
@ -94,20 +94,17 @@ impl CoverageCounters {
/// Counter IDs start from one and go up.
fn next_counter(&mut self) -> CounterValueReference {
assert!(self.next_counter_id < u32::MAX - self.num_expressions);
let next = self.next_counter_id;
self.next_counter_id += 1;
CounterValueReference::from(next)
}
/// Expression IDs start from u32::MAX and go down because an Expression can reference
/// (add or subtract counts) of both Counter regions and Expression regions. The counter
/// expression operand IDs must be unique across both types.
fn next_expression(&mut self) -> InjectedExpressionId {
assert!(self.next_counter_id < u32::MAX - self.num_expressions);
let next = u32::MAX - self.num_expressions;
self.num_expressions += 1;
InjectedExpressionId::from(next)
/// Expression IDs start from 0 and go up.
/// (Counter IDs and Expression IDs are distinguished by the `Operand` enum.)
fn next_expression(&mut self) -> ExpressionId {
let next = self.next_expression_id;
self.next_expression_id = next.next_id();
next
}
}

View file

@ -485,7 +485,7 @@ impl GraphvizData {
/// _not_ used are retained in the `unused_expressions` Vec, to be included in debug output (logs
/// and/or a `CoverageGraph` graphviz output).
pub(super) struct UsedExpressions {
some_used_expression_operands: Option<FxHashMap<Operand, Vec<InjectedExpressionId>>>,
some_used_expression_operands: Option<FxHashMap<Operand, Vec<ExpressionId>>>,
some_unused_expressions:
Option<Vec<(CoverageKind, Option<BasicCoverageBlock>, BasicCoverageBlock)>>,
}

View file

@ -52,12 +52,11 @@ impl CoverageVisitor {
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.
/// Updates `num_expressions` to the maximum encountered expression ID plus 1.
#[inline(always)]
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_num_expressions(&mut self, expression_id: ExpressionId) {
let expression_id = expression_id.as_u32();
self.info.num_expressions = std::cmp::max(self.info.num_expressions, expression_id + 1);
}
fn update_from_expression_operand(&mut self, operand: Operand) {