@@ -2,7 +2,6 @@ use std::cmp::Ordering;
22
33use either:: Either ;
44use itertools:: Itertools ;
5- use rustc_data_structures:: captures:: Captures ;
65use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
76use rustc_data_structures:: graph:: DirectedGraph ;
87use rustc_index:: IndexVec ;
@@ -11,31 +10,35 @@ use rustc_middle::mir::coverage::{CounterId, CovTerm, Expression, ExpressionId,
1110
1211use crate :: coverage:: counters:: balanced_flow:: BalancedFlowGraph ;
1312use crate :: coverage:: counters:: node_flow:: {
14- CounterTerm , NodeCounters , make_node_counters , node_flow_data_for_balanced_graph,
13+ CounterTerm , NodeCounters , NodeFlowData , node_flow_data_for_balanced_graph,
1514} ;
1615use crate :: coverage:: graph:: { BasicCoverageBlock , CoverageGraph } ;
1716
1817mod balanced_flow;
19- mod node_flow;
18+ pub ( crate ) mod node_flow;
2019mod union_find;
2120
22- /// Ensures that each BCB node needing a counter has one, by creating physical
23- /// counters or counter expressions for nodes as required.
24- pub ( super ) fn make_bcb_counters (
25- graph : & CoverageGraph ,
26- bcb_needs_counter : & DenseBitSet < BasicCoverageBlock > ,
27- ) -> CoverageCounters {
21+ /// Struct containing the results of [`prepare_bcb_counters_data`].
22+ pub ( crate ) struct BcbCountersData {
23+ pub ( crate ) node_flow_data : NodeFlowData < BasicCoverageBlock > ,
24+ pub ( crate ) priority_list : Vec < BasicCoverageBlock > ,
25+ }
26+
27+ /// Analyzes the coverage graph to create intermediate data structures that
28+ /// will later be used (during codegen) to create physical counters or counter
29+ /// expressions for each BCB node that needs one.
30+ pub ( crate ) fn prepare_bcb_counters_data ( graph : & CoverageGraph ) -> BcbCountersData {
2831 // Create the derived graphs that are necessary for subsequent steps.
2932 let balanced_graph = BalancedFlowGraph :: for_graph ( graph, |n| !graph[ n] . is_out_summable ) ;
3033 let node_flow_data = node_flow_data_for_balanced_graph ( & balanced_graph) ;
3134
32- // Use those graphs to determine which nodes get physical counters, and how
33- // to compute the execution counts of other nodes from those counters.
35+ // Also create a "priority list" of coverage graph nodes, to help determine
36+ // which ones get physical counters or counter expressions. This needs to
37+ // be done now, because the later parts of the counter-creation process
38+ // won't have access to the original coverage graph.
3439 let priority_list = make_node_flow_priority_list ( graph, balanced_graph) ;
35- let node_counters = make_node_counters ( & node_flow_data, & priority_list) ;
3640
37- // Convert the counters into a form suitable for embedding into MIR.
38- transcribe_counters ( & node_counters, bcb_needs_counter)
41+ BcbCountersData { node_flow_data, priority_list }
3942}
4043
4144/// Arranges the nodes in `balanced_graph` into a list, such that earlier nodes
@@ -74,7 +77,7 @@ fn make_node_flow_priority_list(
7477}
7578
7679// Converts node counters into a form suitable for embedding into MIR.
77- fn transcribe_counters (
80+ pub ( crate ) fn transcribe_counters (
7881 old : & NodeCounters < BasicCoverageBlock > ,
7982 bcb_needs_counter : & DenseBitSet < BasicCoverageBlock > ,
8083) -> CoverageCounters {
@@ -129,15 +132,15 @@ fn transcribe_counters(
129132pub ( super ) struct CoverageCounters {
130133 /// List of places where a counter-increment statement should be injected
131134 /// into MIR, each with its corresponding counter ID.
132- phys_counter_for_node : FxIndexMap < BasicCoverageBlock , CounterId > ,
135+ pub ( crate ) phys_counter_for_node : FxIndexMap < BasicCoverageBlock , CounterId > ,
133136 next_counter_id : CounterId ,
134137
135138 /// Coverage counters/expressions that are associated with individual BCBs.
136139 pub ( crate ) node_counters : IndexVec < BasicCoverageBlock , Option < CovTerm > > ,
137140
138141 /// Table of expression data, associating each expression ID with its
139142 /// corresponding operator (+ or -) and its LHS/RHS operands.
140- expressions : IndexVec < ExpressionId , Expression > ,
143+ pub ( crate ) expressions : IndexVec < ExpressionId , Expression > ,
141144 /// Remember expressions that have already been created (or simplified),
142145 /// so that we don't create unnecessary duplicates.
143146 expressions_memo : FxHashMap < Expression , CovTerm > ,
@@ -188,12 +191,6 @@ impl CoverageCounters {
188191 self . make_expression ( lhs, Op :: Subtract , rhs_sum)
189192 }
190193
191- pub ( super ) fn num_counters ( & self ) -> usize {
192- let num_counters = self . phys_counter_for_node . len ( ) ;
193- assert_eq ! ( num_counters, self . next_counter_id. as_usize( ) ) ;
194- num_counters
195- }
196-
197194 fn set_node_counter ( & mut self , bcb : BasicCoverageBlock , counter : CovTerm ) -> CovTerm {
198195 let existing = self . node_counters [ bcb] . replace ( counter) ;
199196 assert ! (
@@ -202,30 +199,4 @@ impl CoverageCounters {
202199 ) ;
203200 counter
204201 }
205-
206- /// Returns an iterator over all the nodes in the coverage graph that
207- /// should have a counter-increment statement injected into MIR, along with
208- /// each site's corresponding counter ID.
209- pub ( super ) fn counter_increment_sites (
210- & self ,
211- ) -> impl Iterator < Item = ( CounterId , BasicCoverageBlock ) > + Captures < ' _ > {
212- self . phys_counter_for_node . iter ( ) . map ( |( & site, & id) | ( id, site) )
213- }
214-
215- /// Returns an iterator over the subset of BCB nodes that have been associated
216- /// with a counter *expression*, along with the ID of that expression.
217- pub ( super ) fn bcb_nodes_with_coverage_expressions (
218- & self ,
219- ) -> impl Iterator < Item = ( BasicCoverageBlock , ExpressionId ) > + Captures < ' _ > {
220- self . node_counters . iter_enumerated ( ) . filter_map ( |( bcb, & counter) | match counter {
221- // Yield the BCB along with its associated expression ID.
222- Some ( CovTerm :: Expression ( id) ) => Some ( ( bcb, id) ) ,
223- // This BCB is associated with a counter or nothing, so skip it.
224- Some ( CovTerm :: Counter { .. } | CovTerm :: Zero ) | None => None ,
225- } )
226- }
227-
228- pub ( super ) fn into_expressions ( self ) -> IndexVec < ExpressionId , Expression > {
229- self . expressions
230- }
231202}
0 commit comments