1 use crate::transform::{MirPass, MirSource};
2 use crate::util::patch::MirPatch;
3 use rustc_hir::lang_items;
4 use rustc_middle::mir::interpret::Scalar;
5 use rustc_middle::mir::*;
7 use rustc_middle::ty::TyCtxt;
8 use rustc_span::def_id::DefId;
11 /// Inserts call to count_code_region() as a placeholder to be replaced during code generation with
12 /// the intrinsic llvm.instrprof.increment.
13 pub struct InstrumentCoverage;
15 impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
16 fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx>) {
17 if tcx.sess.opts.debugging_opts.instrument_coverage {
18 debug!("instrumenting {:?}", src.def_id());
19 instrument_coverage(tcx, body);
24 // The first counter (start of the function) is index zero.
25 const INIT_FUNCTION_COUNTER: u32 = 0;
27 /// Injects calls to placeholder function `count_code_region()`.
28 // FIXME(richkadel): As a first step, counters are only injected at the top of each function.
29 // The complete solution will inject counters at each conditional code branch.
30 pub fn instrument_coverage<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
31 let span = body.span.shrink_to_lo();
33 let count_code_region_fn = function_handle(
35 tcx.require_lang_item(lang_items::CountCodeRegionFnLangItem, None),
38 let counter_index = Operand::const_from_scalar(
41 Scalar::from_u32(INIT_FUNCTION_COUNTER),
45 let mut patch = MirPatch::new(body);
47 let new_block = patch.new_block(placeholder_block(SourceInfo::outermost(body.span)));
48 let next_block = START_BLOCK;
50 let temp = patch.new_temp(tcx.mk_unit(), body.span);
51 patch.patch_terminator(
53 TerminatorKind::Call {
54 func: count_code_region_fn,
55 args: vec![counter_index],
56 // new_block will swapped with the next_block, after applying patch
57 destination: Some((Place::from(temp), new_block)),
64 patch.add_statement(new_block.start_location(), StatementKind::StorageLive(temp));
65 patch.add_statement(next_block.start_location(), StatementKind::StorageDead(temp));
69 // To insert the `new_block` in front of the first block in the counted branch (for example,
70 // the START_BLOCK, at the top of the function), just swap the indexes, leaving the rest of the
72 body.basic_blocks_mut().swap(next_block, new_block);
75 fn function_handle<'tcx>(tcx: TyCtxt<'tcx>, fn_def_id: DefId, span: Span) -> Operand<'tcx> {
76 let ret_ty = tcx.fn_sig(fn_def_id).output();
77 let ret_ty = ret_ty.no_bound_vars().unwrap();
78 let substs = tcx.mk_substs(::std::iter::once(ty::subst::GenericArg::from(ret_ty)));
79 Operand::function_handle(tcx, fn_def_id, substs, span)
82 fn placeholder_block<'tcx>(source_info: SourceInfo) -> BasicBlockData<'tcx> {
85 terminator: Some(Terminator {
87 // this gets overwritten by the counter Call
88 kind: TerminatorKind::Unreachable,