@@ -11,7 +11,8 @@ use rustc_codegen_ssa::traits::{
1111 BaseTypeCodegenMethods , ConstCodegenMethods , StaticCodegenMethods ,
1212} ;
1313use rustc_middle:: mir:: coverage:: {
14- CovTerm , CoverageIdsInfo , Expression , FunctionCoverageInfo , Mapping , MappingKind , Op ,
14+ BasicCoverageBlock , CovTerm , CoverageIdsInfo , Expression , FunctionCoverageInfo , Mapping ,
15+ MappingKind , Op ,
1516} ;
1617use rustc_middle:: ty:: { Instance , TyCtxt } ;
1718use rustc_span:: Span ;
@@ -53,7 +54,7 @@ pub(crate) fn prepare_covfun_record<'tcx>(
5354 let fn_cov_info = tcx. instance_mir ( instance. def ) . function_coverage_info . as_deref ( ) ?;
5455 let ids_info = tcx. coverage_ids_info ( instance. def ) ?;
5556
56- let expressions = prepare_expressions ( fn_cov_info , ids_info, is_used ) ;
57+ let expressions = prepare_expressions ( ids_info) ;
5758
5859 let mut covfun = CovfunRecord {
5960 mangled_function_name : tcx. symbol_name ( instance) . name ,
@@ -75,26 +76,14 @@ pub(crate) fn prepare_covfun_record<'tcx>(
7576}
7677
7778/// Convert the function's coverage-counter expressions into a form suitable for FFI.
78- fn prepare_expressions (
79- fn_cov_info : & FunctionCoverageInfo ,
80- ids_info : & CoverageIdsInfo ,
81- is_used : bool ,
82- ) -> Vec < ffi:: CounterExpression > {
83- // If any counters or expressions were removed by MIR opts, replace their
84- // terms with zero.
85- let counter_for_term = |term| {
86- if !is_used || ids_info. is_zero_term ( term) {
87- ffi:: Counter :: ZERO
88- } else {
89- ffi:: Counter :: from_term ( term)
90- }
91- } ;
79+ fn prepare_expressions ( ids_info : & CoverageIdsInfo ) -> Vec < ffi:: CounterExpression > {
80+ let counter_for_term = ffi:: Counter :: from_term;
9281
9382 // We know that LLVM will optimize out any unused expressions before
9483 // producing the final coverage map, so there's no need to do the same
9584 // thing on the Rust side unless we're confident we can do much better.
9685 // (See `CounterExpressionsMinimizer` in `CoverageMappingWriter.cpp`.)
97- fn_cov_info
86+ ids_info
9887 . expressions
9988 . iter ( )
10089 . map ( move |& Expression { lhs, op, rhs } | ffi:: CounterExpression {
@@ -136,11 +125,16 @@ fn fill_region_tables<'tcx>(
136125
137126 // For each counter/region pair in this function+file, convert it to a
138127 // form suitable for FFI.
139- let is_zero_term = |term| !covfun. is_used || ids_info. is_zero_term ( term) ;
140128 for & Mapping { ref kind, span } in & fn_cov_info. mappings {
141- // If the mapping refers to counters/expressions that were removed by
142- // MIR opts, replace those occurrences with zero.
143- let kind = kind. map_terms ( |term| if is_zero_term ( term) { CovTerm :: Zero } else { term } ) ;
129+ // If this function is unused, replace all counters with zero.
130+ let counter_for_bcb = |bcb : BasicCoverageBlock | -> ffi:: Counter {
131+ let term = if covfun. is_used {
132+ ids_info. term_for_bcb [ bcb] . expect ( "every BCB in a mapping was given a term" )
133+ } else {
134+ CovTerm :: Zero
135+ } ;
136+ ffi:: Counter :: from_term ( term)
137+ } ;
144138
145139 // Convert the `Span` into coordinates that we can pass to LLVM, or
146140 // discard the span if conversion fails. In rare, cases _all_ of a
@@ -154,23 +148,22 @@ fn fill_region_tables<'tcx>(
154148 continue ;
155149 }
156150
157- match kind {
158- MappingKind :: Code ( term) => {
159- code_regions
160- . push ( ffi:: CodeRegion { cov_span, counter : ffi:: Counter :: from_term ( term) } ) ;
151+ match * kind {
152+ MappingKind :: Code { bcb } => {
153+ code_regions. push ( ffi:: CodeRegion { cov_span, counter : counter_for_bcb ( bcb) } ) ;
161154 }
162- MappingKind :: Branch { true_term , false_term } => {
155+ MappingKind :: Branch { true_bcb , false_bcb } => {
163156 branch_regions. push ( ffi:: BranchRegion {
164157 cov_span,
165- true_counter : ffi :: Counter :: from_term ( true_term ) ,
166- false_counter : ffi :: Counter :: from_term ( false_term ) ,
158+ true_counter : counter_for_bcb ( true_bcb ) ,
159+ false_counter : counter_for_bcb ( false_bcb ) ,
167160 } ) ;
168161 }
169- MappingKind :: MCDCBranch { true_term , false_term , mcdc_params } => {
162+ MappingKind :: MCDCBranch { true_bcb , false_bcb , mcdc_params } => {
170163 mcdc_branch_regions. push ( ffi:: MCDCBranchRegion {
171164 cov_span,
172- true_counter : ffi :: Counter :: from_term ( true_term ) ,
173- false_counter : ffi :: Counter :: from_term ( false_term ) ,
165+ true_counter : counter_for_bcb ( true_bcb ) ,
166+ false_counter : counter_for_bcb ( false_bcb ) ,
174167 mcdc_branch_params : ffi:: mcdc:: BranchParameters :: from ( mcdc_params) ,
175168 } ) ;
176169 }
0 commit comments