@@ -3,8 +3,7 @@ pub use super::ffi::*;
33use rustc_index:: { IndexSlice , IndexVec } ;
44use rustc_middle:: bug;
55use rustc_middle:: mir:: coverage:: {
6- CodeRegion , CounterValueReference , InjectedExpressionId , InjectedExpressionIndex ,
7- MappedExpressionIndex , Op , Operand ,
6+ CodeRegion , CounterValueReference , ExpressionId , MappedExpressionIndex , Op , Operand ,
87} ;
98use rustc_middle:: ty:: Instance ;
109use rustc_middle:: ty:: TyCtxt ;
@@ -19,8 +18,7 @@ pub struct Expression {
1918
2019/// Collects all of the coverage regions associated with (a) injected counters, (b) counter
2120/// expressions (additions or subtraction), and (c) unreachable regions (always counted as zero),
22- /// for a given Function. Counters and counter expressions have non-overlapping `id`s because they
23- /// can both be operands in an expression. This struct also stores the `function_source_hash`,
21+ /// for a given Function. This struct also stores the `function_source_hash`,
2422/// computed during instrumentation, and forwarded with counters.
2523///
2624/// Note, it may be important to understand LLVM's definitions of `unreachable` regions versus "gap
@@ -35,7 +33,7 @@ pub struct FunctionCoverage<'tcx> {
3533 source_hash : u64 ,
3634 is_used : bool ,
3735 counters : IndexVec < CounterValueReference , Option < CodeRegion > > ,
38- expressions : IndexVec < InjectedExpressionIndex , Option < Expression > > ,
36+ expressions : IndexVec < ExpressionId , Option < Expression > > ,
3937 unreachable_regions : Vec < CodeRegion > ,
4038}
4139
@@ -89,22 +87,11 @@ impl<'tcx> FunctionCoverage<'tcx> {
8987 }
9088
9189 /// Both counters and "counter expressions" (or simply, "expressions") can be operands in other
92- /// expressions. Expression IDs start from `u32::MAX` and go down, so the range of expression
93- /// IDs will not overlap with the range of counter IDs. Counters and expressions can be added in
94- /// any order, and expressions can still be assigned contiguous (though descending) IDs, without
95- /// knowing what the last counter ID will be.
96- ///
97- /// When storing the expression data in the `expressions` vector in the `FunctionCoverage`
98- /// struct, its vector index is computed, from the given expression ID, by subtracting from
99- /// `u32::MAX`.
100- ///
101- /// Since the expression operands (`lhs` and `rhs`) can reference either counters or
102- /// expressions, an operand that references an expression also uses its original ID, descending
103- /// from `u32::MAX`. Theses operands are translated only during code generation, after all
104- /// counters and expressions have been added.
90+ /// expressions. These are tracked as separate variants of `Operand`, so there is no ambiguity
91+ /// between operands that are counter IDs and operands that are expression IDs.
10592 pub fn add_counter_expression (
10693 & mut self ,
107- expression_id : InjectedExpressionId ,
94+ expression_id : ExpressionId ,
10895 lhs : Operand ,
10996 op : Op ,
11097 rhs : Operand ,
@@ -114,16 +101,15 @@ impl<'tcx> FunctionCoverage<'tcx> {
114101 "add_counter_expression({:?}, lhs={:?}, op={:?}, rhs={:?} at {:?}" ,
115102 expression_id, lhs, op, rhs, region
116103 ) ;
117- let expression_index = self . expression_index ( expression_id) ;
118104 debug_assert ! (
119- expression_index . as_usize( ) < self . expressions. len( ) ,
120- "expression_index {} is out of range for expressions.len() = {}
105+ expression_id . as_usize( ) < self . expressions. len( ) ,
106+ "expression_id {} is out of range for expressions.len() = {}
121107 for {:?}" ,
122- expression_index . as_usize( ) ,
108+ expression_id . as_usize( ) ,
123109 self . expressions. len( ) ,
124110 self ,
125111 ) ;
126- if let Some ( previous_expression) = self . expressions [ expression_index ] . replace ( Expression {
112+ if let Some ( previous_expression) = self . expressions [ expression_id ] . replace ( Expression {
127113 lhs,
128114 op,
129115 rhs,
@@ -190,7 +176,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
190176 //
191177 // Expressions will be returned from this function in a sequential vector (array) of
192178 // `CounterExpression`, so the expression IDs must be mapped from their original,
193- // potentially sparse set of indexes, originally in reverse order from `u32::MAX` .
179+ // potentially sparse set of indexes.
194180 //
195181 // An `Expression` as an operand will have already been encountered as an `Expression` with
196182 // operands, so its new_index will already have been generated (as a 1-up index value).
@@ -203,7 +189,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
203189 // `expression_index`s lower than the referencing `Expression`. Therefore, it is
204190 // reasonable to look up the new index of an expression operand while the `new_indexes`
205191 // vector is only complete up to the current `ExpressionIndex`.
206- type NewIndexes = IndexSlice < InjectedExpressionIndex , Option < MappedExpressionIndex > > ;
192+ type NewIndexes = IndexSlice < ExpressionId , Option < MappedExpressionIndex > > ;
207193 let id_to_counter = |new_indexes : & NewIndexes , operand : Operand | match operand {
208194 Operand :: Zero => Some ( Counter :: zero ( ) ) ,
209195 Operand :: Counter ( id) => {
@@ -219,15 +205,14 @@ impl<'tcx> FunctionCoverage<'tcx> {
219205 Some ( Counter :: counter_value_reference ( index) )
220206 }
221207 Operand :: Expression ( id) => {
222- let index = self . expression_index ( id) ;
223208 self . expressions
224- . get ( index )
209+ . get ( id )
225210 . expect ( "expression id is out of range" )
226211 . as_ref ( )
227212 // If an expression was optimized out, assume it would have produced a count
228213 // of zero. This ensures that expressions dependent on optimized-out
229214 // expressions are still valid.
230- . map_or ( Some ( Counter :: zero ( ) ) , |_| new_indexes[ index ] . map ( Counter :: expression) )
215+ . map_or ( Some ( Counter :: zero ( ) ) , |_| new_indexes[ id ] . map ( Counter :: expression) )
231216 }
232217 } ;
233218
@@ -334,10 +319,4 @@ impl<'tcx> FunctionCoverage<'tcx> {
334319 fn unreachable_regions ( & self ) -> impl Iterator < Item = ( Counter , & CodeRegion ) > {
335320 self . unreachable_regions . iter ( ) . map ( |region| ( Counter :: zero ( ) , region) )
336321 }
337-
338- fn expression_index ( & self , id : InjectedExpressionId ) -> InjectedExpressionIndex {
339- debug_assert ! ( id. as_usize( ) >= self . counters. len( ) ) ;
340- let id_descending_from_max = id. as_u32 ( ) ;
341- InjectedExpressionIndex :: from ( u32:: MAX - id_descending_from_max)
342- }
343322}
0 commit comments