3 use rustc_middle::mir::coverage::*;
4 use rustc_middle::mir::Coverage;
5 use rustc_middle::mir::SourceScope;
9 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10 pub fn codegen_coverage(&self, bx: &mut Bx, coverage: Coverage, scope: SourceScope) {
11 // Determine the instance that coverage data was originally generated for.
12 let scope_data = &self.mir.source_scopes[scope];
13 let instance = if let Some((inlined_instance, _)) = scope_data.inlined {
14 self.monomorphize(inlined_instance)
15 } else if let Some(inlined_scope) = scope_data.inlined_parent_scope {
16 self.monomorphize(self.mir.source_scopes[inlined_scope].inlined.unwrap().0)
21 let Coverage { kind, code_region } = coverage;
23 CoverageKind::Counter { function_source_hash, id } => {
24 if bx.set_function_source_hash(instance, function_source_hash) {
25 // If `set_function_source_hash()` returned true, the coverage map is enabled,
26 // so continue adding the counter.
27 if let Some(code_region) = code_region {
28 // Note: Some counters do not have code regions, but may still be referenced
29 // from expressions. In that case, don't add the counter to the coverage map,
30 // but do inject the counter intrinsic.
31 bx.add_coverage_counter(instance, id, code_region);
34 let coverageinfo = bx.tcx().coverageinfo(instance.def);
36 let fn_name = bx.get_pgo_func_name_var(instance);
37 let hash = bx.const_u64(function_source_hash);
38 let num_counters = bx.const_u32(coverageinfo.num_counters);
39 let index = bx.const_u32(id.zero_based_index());
41 "codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?}, index={:?})",
42 fn_name, hash, num_counters, index,
44 bx.instrprof_increment(fn_name, hash, num_counters, index);
47 CoverageKind::Expression { id, lhs, op, rhs } => {
48 bx.add_coverage_counter_expression(instance, id, lhs, op, rhs, code_region);
50 CoverageKind::Unreachable => {
51 bx.add_coverage_unreachable(
53 code_region.expect("unreachable regions always have code regions"),