@@ -2,92 +2,33 @@ use super::*;
22
33use rustc_data_structures:: captures:: Captures ;
44use rustc_middle:: mir:: coverage:: * ;
5- use rustc_middle:: mir:: { self , Body , Coverage , CoverageInfo } ;
5+ use rustc_middle:: mir:: { self , Body , Coverage , CoverageIdsInfo } ;
66use rustc_middle:: query:: Providers ;
77use rustc_middle:: ty:: { self , TyCtxt } ;
88use rustc_span:: def_id:: DefId ;
99
1010/// A `query` provider for retrieving coverage information injected into MIR.
1111pub ( crate ) fn provide ( providers : & mut Providers ) {
12- providers. coverageinfo = |tcx, def_id| coverageinfo ( tcx, def_id) ;
12+ providers. coverage_ids_info = |tcx, def_id| coverage_ids_info ( tcx, def_id) ;
1313 providers. covered_code_regions = |tcx, def_id| covered_code_regions ( tcx, def_id) ;
1414}
1515
16- /// Coverage codegen needs to know the total number of counter IDs and expression IDs that have
17- /// been used by a function's coverage mappings. These totals are used to create vectors to hold
18- /// the relevant counter and expression data, and the maximum counter ID (+ 1) is also needed by
19- /// the `llvm.instrprof.increment` intrinsic.
20- ///
21- /// MIR optimization may split and duplicate some BasicBlock sequences, or optimize out some code
22- /// including injected counters. (It is OK if some counters are optimized out, but those counters
23- /// are still included in the total `num_counters` or `num_expressions`.) Simply counting the
24- /// calls may not work; but computing the number of counters or expressions by adding `1` to the
25- /// highest ID (for a given instrumented function) is valid.
26- ///
27- /// It's possible for a coverage expression to remain in MIR while one or both of its operands
28- /// have been optimized away. To avoid problems in codegen, we include those operands' IDs when
29- /// determining the maximum counter/expression ID, even if the underlying counter/expression is
30- /// no longer present.
31- struct CoverageVisitor {
32- max_counter_id : CounterId ,
33- max_expression_id : ExpressionId ,
34- }
35-
36- impl CoverageVisitor {
37- /// Updates `max_counter_id` to the maximum encountered counter ID.
38- #[ inline( always) ]
39- fn update_max_counter_id ( & mut self , counter_id : CounterId ) {
40- self . max_counter_id = self . max_counter_id . max ( counter_id) ;
41- }
42-
43- /// Updates `max_expression_id` to the maximum encountered expression ID.
44- #[ inline( always) ]
45- fn update_max_expression_id ( & mut self , expression_id : ExpressionId ) {
46- self . max_expression_id = self . max_expression_id . max ( expression_id) ;
47- }
48-
49- fn update_from_expression_operand ( & mut self , operand : Operand ) {
50- match operand {
51- Operand :: Counter ( id) => self . update_max_counter_id ( id) ,
52- Operand :: Expression ( id) => self . update_max_expression_id ( id) ,
53- Operand :: Zero => { }
54- }
55- }
56-
57- fn visit_body ( & mut self , body : & Body < ' _ > ) {
58- for coverage in all_coverage_in_mir_body ( body) {
59- self . visit_coverage ( coverage) ;
60- }
61- }
62-
63- fn visit_coverage ( & mut self , coverage : & Coverage ) {
64- match coverage. kind {
65- CoverageKind :: Counter { id, .. } => self . update_max_counter_id ( id) ,
66- CoverageKind :: Expression { id, lhs, rhs, .. } => {
67- self . update_max_expression_id ( id) ;
68- self . update_from_expression_operand ( lhs) ;
69- self . update_from_expression_operand ( rhs) ;
70- }
71- CoverageKind :: Unreachable => { }
72- }
73- }
74- }
75-
76- fn coverageinfo < ' tcx > ( tcx : TyCtxt < ' tcx > , instance_def : ty:: InstanceDef < ' tcx > ) -> CoverageInfo {
16+ /// Query implementation for `coverage_ids_info`.
17+ fn coverage_ids_info < ' tcx > (
18+ tcx : TyCtxt < ' tcx > ,
19+ instance_def : ty:: InstanceDef < ' tcx > ,
20+ ) -> CoverageIdsInfo {
7721 let mir_body = tcx. instance_mir ( instance_def) ;
7822
79- let mut coverage_visitor = CoverageVisitor {
80- max_counter_id : CounterId :: START ,
81- max_expression_id : ExpressionId :: START ,
82- } ;
83-
84- coverage_visitor. visit_body ( mir_body) ;
23+ let max_counter_id = all_coverage_in_mir_body ( mir_body)
24+ . filter_map ( |coverage| match coverage. kind {
25+ CoverageKind :: Counter { id } => Some ( id) ,
26+ _ => None ,
27+ } )
28+ . max ( )
29+ . unwrap_or ( CounterId :: START ) ;
8530
86- // Add 1 to the highest IDs to get the total number of IDs.
87- CoverageInfo {
88- num_counters : ( coverage_visitor. max_counter_id + 1 ) . as_u32 ( ) ,
89- num_expressions : ( coverage_visitor. max_expression_id + 1 ) . as_u32 ( ) ,
90- }
31+ CoverageIdsInfo { max_counter_id }
9132}
9233
9334fn covered_code_regions ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> Vec < & CodeRegion > {
0 commit comments