@@ -3,8 +3,7 @@ pub use super::ffi::*;
3
3
use rustc_index:: { IndexSlice , IndexVec } ;
4
4
use rustc_middle:: bug;
5
5
use rustc_middle:: mir:: coverage:: {
6
- CodeRegion , CounterValueReference , InjectedExpressionId , InjectedExpressionIndex ,
7
- MappedExpressionIndex , Op , Operand ,
6
+ CodeRegion , CounterValueReference , ExpressionId , MappedExpressionIndex , Op , Operand ,
8
7
} ;
9
8
use rustc_middle:: ty:: Instance ;
10
9
use rustc_middle:: ty:: TyCtxt ;
@@ -19,8 +18,7 @@ pub struct Expression {
19
18
20
19
/// Collects all of the coverage regions associated with (a) injected counters, (b) counter
21
20
/// 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`,
24
22
/// computed during instrumentation, and forwarded with counters.
25
23
///
26
24
/// Note, it may be important to understand LLVM's definitions of `unreachable` regions versus "gap
@@ -35,7 +33,7 @@ pub struct FunctionCoverage<'tcx> {
35
33
source_hash : u64 ,
36
34
is_used : bool ,
37
35
counters : IndexVec < CounterValueReference , Option < CodeRegion > > ,
38
- expressions : IndexVec < InjectedExpressionIndex , Option < Expression > > ,
36
+ expressions : IndexVec < ExpressionId , Option < Expression > > ,
39
37
unreachable_regions : Vec < CodeRegion > ,
40
38
}
41
39
@@ -89,22 +87,11 @@ impl<'tcx> FunctionCoverage<'tcx> {
89
87
}
90
88
91
89
/// 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.
105
92
pub fn add_counter_expression (
106
93
& mut self ,
107
- expression_id : InjectedExpressionId ,
94
+ expression_id : ExpressionId ,
108
95
lhs : Operand ,
109
96
op : Op ,
110
97
rhs : Operand ,
@@ -114,16 +101,15 @@ impl<'tcx> FunctionCoverage<'tcx> {
114
101
"add_counter_expression({:?}, lhs={:?}, op={:?}, rhs={:?} at {:?}" ,
115
102
expression_id, lhs, op, rhs, region
116
103
) ;
117
- let expression_index = self . expression_index ( expression_id) ;
118
104
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() = {}
121
107
for {:?}" ,
122
- expression_index . as_usize( ) ,
108
+ expression_id . as_usize( ) ,
123
109
self . expressions. len( ) ,
124
110
self ,
125
111
) ;
126
- if let Some ( previous_expression) = self . expressions [ expression_index ] . replace ( Expression {
112
+ if let Some ( previous_expression) = self . expressions [ expression_id ] . replace ( Expression {
127
113
lhs,
128
114
op,
129
115
rhs,
@@ -190,7 +176,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
190
176
//
191
177
// Expressions will be returned from this function in a sequential vector (array) of
192
178
// `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.
194
180
//
195
181
// An `Expression` as an operand will have already been encountered as an `Expression` with
196
182
// operands, so its new_index will already have been generated (as a 1-up index value).
@@ -203,7 +189,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
203
189
// `expression_index`s lower than the referencing `Expression`. Therefore, it is
204
190
// reasonable to look up the new index of an expression operand while the `new_indexes`
205
191
// vector is only complete up to the current `ExpressionIndex`.
206
- type NewIndexes = IndexSlice < InjectedExpressionIndex , Option < MappedExpressionIndex > > ;
192
+ type NewIndexes = IndexSlice < ExpressionId , Option < MappedExpressionIndex > > ;
207
193
let id_to_counter = |new_indexes : & NewIndexes , operand : Operand | match operand {
208
194
Operand :: Zero => Some ( Counter :: zero ( ) ) ,
209
195
Operand :: Counter ( id) => {
@@ -219,15 +205,14 @@ impl<'tcx> FunctionCoverage<'tcx> {
219
205
Some ( Counter :: counter_value_reference ( index) )
220
206
}
221
207
Operand :: Expression ( id) => {
222
- let index = self . expression_index ( id) ;
223
208
self . expressions
224
- . get ( index )
209
+ . get ( id )
225
210
. expect ( "expression id is out of range" )
226
211
. as_ref ( )
227
212
// If an expression was optimized out, assume it would have produced a count
228
213
// of zero. This ensures that expressions dependent on optimized-out
229
214
// 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) )
231
216
}
232
217
} ;
233
218
@@ -334,10 +319,4 @@ impl<'tcx> FunctionCoverage<'tcx> {
334
319
fn unreachable_regions ( & self ) -> impl Iterator < Item = ( Counter , & CodeRegion ) > {
335
320
self . unreachable_regions . iter ( ) . map ( |region| ( Counter :: zero ( ) , region) )
336
321
}
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
- }
343
322
}
0 commit comments