1 //=-- CoverageMapping.cpp - Code coverage mapping support ---------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for clang's and llvm's instrumentation based
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/CoverageMapping.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/SmallBitVector.h"
19 #include "llvm/ProfileData/CoverageMappingReader.h"
20 #include "llvm/ProfileData/InstrProfReader.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
25 using namespace coverage;
27 #define DEBUG_TYPE "coverage-mapping"
29 Counter CounterExpressionBuilder::get(const CounterExpression &E) {
30 auto It = ExpressionIndices.find(E);
31 if (It != ExpressionIndices.end())
32 return Counter::getExpression(It->second);
33 unsigned I = Expressions.size();
34 Expressions.push_back(E);
35 ExpressionIndices[E] = I;
36 return Counter::getExpression(I);
39 void CounterExpressionBuilder::extractTerms(
40 Counter C, int Sign, SmallVectorImpl<std::pair<unsigned, int>> &Terms) {
41 switch (C.getKind()) {
44 case Counter::CounterValueReference:
45 Terms.push_back(std::make_pair(C.getCounterID(), Sign));
47 case Counter::Expression:
48 const auto &E = Expressions[C.getExpressionID()];
49 extractTerms(E.LHS, Sign, Terms);
50 extractTerms(E.RHS, E.Kind == CounterExpression::Subtract ? -Sign : Sign,
56 Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
57 // Gather constant terms.
58 llvm::SmallVector<std::pair<unsigned, int>, 32> Terms;
59 extractTerms(ExpressionTree, +1, Terms);
61 // If there are no terms, this is just a zero. The algorithm below assumes at
63 if (Terms.size() == 0)
64 return Counter::getZero();
66 // Group the terms by counter ID.
67 std::sort(Terms.begin(), Terms.end(),
68 [](const std::pair<unsigned, int> &LHS,
69 const std::pair<unsigned, int> &RHS) {
70 return LHS.first < RHS.first;
73 // Combine terms by counter ID to eliminate counters that sum to zero.
74 auto Prev = Terms.begin();
75 for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) {
76 if (I->first == Prev->first) {
77 Prev->second += I->second;
83 Terms.erase(++Prev, Terms.end());
86 // Create additions. We do this before subtractions to avoid constructs like
87 // ((0 - X) + Y), as opposed to (Y - X).
88 for (auto Term : Terms) {
91 for (int I = 0; I < Term.second; ++I)
93 C = Counter::getCounter(Term.first);
95 C = get(CounterExpression(CounterExpression::Add, C,
96 Counter::getCounter(Term.first)));
99 // Create subtractions.
100 for (auto Term : Terms) {
101 if (Term.second >= 0)
103 for (int I = 0; I < -Term.second; ++I)
104 C = get(CounterExpression(CounterExpression::Subtract, C,
105 Counter::getCounter(Term.first)));
110 Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) {
111 return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS)));
114 Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) {
116 get(CounterExpression(CounterExpression::Subtract, LHS, RHS)));
119 void CounterMappingContext::dump(const Counter &C,
120 llvm::raw_ostream &OS) const {
121 switch (C.getKind()) {
125 case Counter::CounterValueReference:
126 OS << '#' << C.getCounterID();
128 case Counter::Expression: {
129 if (C.getExpressionID() >= Expressions.size())
131 const auto &E = Expressions[C.getExpressionID()];
134 OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
140 if (CounterValues.empty())
142 ErrorOr<int64_t> Value = evaluate(C);
145 OS << '[' << *Value << ']';
148 ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
149 switch (C.getKind()) {
152 case Counter::CounterValueReference:
153 if (C.getCounterID() >= CounterValues.size())
154 return std::make_error_code(std::errc::argument_out_of_domain);
155 return CounterValues[C.getCounterID()];
156 case Counter::Expression: {
157 if (C.getExpressionID() >= Expressions.size())
158 return std::make_error_code(std::errc::argument_out_of_domain);
159 const auto &E = Expressions[C.getExpressionID()];
160 ErrorOr<int64_t> LHS = evaluate(E.LHS);
163 ErrorOr<int64_t> RHS = evaluate(E.RHS);
166 return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
169 llvm_unreachable("Unhandled CounterKind");
172 void FunctionRecordIterator::skipOtherFiles() {
173 while (Current != Records.end() && !Filename.empty() &&
174 Filename != Current->Filenames[0])
176 if (Current == Records.end())
177 *this = FunctionRecordIterator();
180 ErrorOr<std::unique_ptr<CoverageMapping>>
181 CoverageMapping::load(CoverageMappingReader &CoverageReader,
182 IndexedInstrProfReader &ProfileReader) {
183 auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
185 std::vector<uint64_t> Counts;
186 for (const auto &Record : CoverageReader) {
187 CounterMappingContext Ctx(Record.Expressions);
190 if (std::error_code EC = ProfileReader.getFunctionCounts(
191 Record.FunctionName, Record.FunctionHash, Counts)) {
192 if (EC == instrprof_error::hash_mismatch) {
193 Coverage->MismatchedFunctionCount++;
195 } else if (EC != instrprof_error::unknown_function)
198 Ctx.setCounts(Counts);
200 assert(!Record.MappingRegions.empty() && "Function has no regions");
201 FunctionRecord Function(Record.FunctionName, Record.Filenames);
202 for (const auto &Region : Record.MappingRegions) {
203 ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
206 Function.pushRegion(Region, *ExecutionCount);
208 if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
209 Coverage->MismatchedFunctionCount++;
213 Coverage->Functions.push_back(std::move(Function));
216 return std::move(Coverage);
219 ErrorOr<std::unique_ptr<CoverageMapping>>
220 CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename,
221 Triple::ArchType Arch) {
222 auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
223 if (std::error_code EC = CounterMappingBuff.getError())
225 auto CoverageReaderOrErr =
226 BinaryCoverageReader::create(CounterMappingBuff.get(), Arch);
227 if (std::error_code EC = CoverageReaderOrErr.getError())
229 auto CoverageReader = std::move(CoverageReaderOrErr.get());
230 auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
231 if (auto EC = ProfileReaderOrErr.getError())
233 auto ProfileReader = std::move(ProfileReaderOrErr.get());
234 return load(*CoverageReader, *ProfileReader);
238 /// \brief Distributes functions into instantiation sets.
240 /// An instantiation set is a collection of functions that have the same source
241 /// code, ie, template functions specializations.
242 class FunctionInstantiationSetCollector {
243 typedef DenseMap<std::pair<unsigned, unsigned>,
244 std::vector<const FunctionRecord *>> MapT;
245 MapT InstantiatedFunctions;
248 void insert(const FunctionRecord &Function, unsigned FileID) {
249 auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
250 while (I != E && I->FileID != FileID)
252 assert(I != E && "function does not cover the given file");
253 auto &Functions = InstantiatedFunctions[I->startLoc()];
254 Functions.push_back(&Function);
257 MapT::iterator begin() { return InstantiatedFunctions.begin(); }
259 MapT::iterator end() { return InstantiatedFunctions.end(); }
262 class SegmentBuilder {
263 std::vector<CoverageSegment> Segments;
264 SmallVector<const CountedRegion *, 8> ActiveRegions;
266 /// Start a segment with no count specified.
267 void startSegment(unsigned Line, unsigned Col) {
268 DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n");
269 Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false);
272 /// Start a segment with the given Region's count.
273 void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry,
274 const CountedRegion &Region) {
275 if (Segments.empty())
276 Segments.emplace_back(Line, Col, IsRegionEntry);
277 CoverageSegment S = Segments.back();
278 // Avoid creating empty regions.
279 if (S.Line != Line || S.Col != Col) {
280 Segments.emplace_back(Line, Col, IsRegionEntry);
283 DEBUG(dbgs() << "Segment at " << Line << ":" << Col);
284 // Set this region's count.
285 if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) {
286 DEBUG(dbgs() << " with count " << Region.ExecutionCount);
287 Segments.back().setCount(Region.ExecutionCount);
289 DEBUG(dbgs() << "\n");
292 /// Start a segment for the given region.
293 void startSegment(const CountedRegion &Region) {
294 startSegment(Region.LineStart, Region.ColumnStart, true, Region);
297 /// Pop the top region off of the active stack, starting a new segment with
298 /// the containing Region's count.
300 const CountedRegion *Active = ActiveRegions.back();
301 unsigned Line = Active->LineEnd, Col = Active->ColumnEnd;
302 ActiveRegions.pop_back();
303 if (ActiveRegions.empty())
304 startSegment(Line, Col);
306 startSegment(Line, Col, false, *ActiveRegions.back());
310 /// Build a list of CoverageSegments from a sorted list of Regions.
311 std::vector<CoverageSegment> buildSegments(ArrayRef<CountedRegion> Regions) {
312 const CountedRegion *PrevRegion = nullptr;
313 for (const auto &Region : Regions) {
314 // Pop any regions that end before this one starts.
315 while (!ActiveRegions.empty() &&
316 ActiveRegions.back()->endLoc() <= Region.startLoc())
318 if (PrevRegion && PrevRegion->startLoc() == Region.startLoc() &&
319 PrevRegion->endLoc() == Region.endLoc()) {
320 if (Region.Kind == coverage::CounterMappingRegion::CodeRegion)
321 Segments.back().addCount(Region.ExecutionCount);
323 // Add this region to the stack.
324 ActiveRegions.push_back(&Region);
325 startSegment(Region);
327 PrevRegion = &Region;
329 // Pop any regions that are left in the stack.
330 while (!ActiveRegions.empty())
337 std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
338 std::vector<StringRef> Filenames;
339 for (const auto &Function : getCoveredFunctions())
340 Filenames.insert(Filenames.end(), Function.Filenames.begin(),
341 Function.Filenames.end());
342 std::sort(Filenames.begin(), Filenames.end());
343 auto Last = std::unique(Filenames.begin(), Filenames.end());
344 Filenames.erase(Last, Filenames.end());
348 static SmallBitVector gatherFileIDs(StringRef SourceFile,
349 const FunctionRecord &Function) {
350 SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
351 for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
352 if (SourceFile == Function.Filenames[I])
353 FilenameEquivalence[I] = true;
354 return FilenameEquivalence;
357 static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
358 const FunctionRecord &Function) {
359 SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
360 SmallBitVector FilenameEquivalence = gatherFileIDs(SourceFile, Function);
361 for (const auto &CR : Function.CountedRegions)
362 if (CR.Kind == CounterMappingRegion::ExpansionRegion &&
363 FilenameEquivalence[CR.FileID])
364 IsNotExpandedFile[CR.ExpandedFileID] = false;
365 IsNotExpandedFile &= FilenameEquivalence;
366 int I = IsNotExpandedFile.find_first();
372 static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
373 SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
374 for (const auto &CR : Function.CountedRegions)
375 if (CR.Kind == CounterMappingRegion::ExpansionRegion)
376 IsNotExpandedFile[CR.ExpandedFileID] = false;
377 int I = IsNotExpandedFile.find_first();
383 /// Sort a nested sequence of regions from a single file.
384 template <class It> static void sortNestedRegions(It First, It Last) {
385 std::sort(First, Last,
386 [](const CountedRegion &LHS, const CountedRegion &RHS) {
387 if (LHS.startLoc() == RHS.startLoc())
388 // When LHS completely contains RHS, we sort LHS first.
389 return RHS.endLoc() < LHS.endLoc();
390 return LHS.startLoc() < RHS.startLoc();
394 static bool isExpansion(const CountedRegion &R, unsigned FileID) {
395 return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
398 CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) {
399 CoverageData FileCoverage(Filename);
400 std::vector<coverage::CountedRegion> Regions;
402 for (const auto &Function : Functions) {
403 auto MainFileID = findMainViewFileID(Filename, Function);
406 auto FileIDs = gatherFileIDs(Filename, Function);
407 for (const auto &CR : Function.CountedRegions)
408 if (FileIDs.test(CR.FileID)) {
409 Regions.push_back(CR);
410 if (isExpansion(CR, *MainFileID))
411 FileCoverage.Expansions.emplace_back(CR, Function);
415 sortNestedRegions(Regions.begin(), Regions.end());
416 DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
417 FileCoverage.Segments = SegmentBuilder().buildSegments(Regions);
422 std::vector<const FunctionRecord *>
423 CoverageMapping::getInstantiations(StringRef Filename) {
424 FunctionInstantiationSetCollector InstantiationSetCollector;
425 for (const auto &Function : Functions) {
426 auto MainFileID = findMainViewFileID(Filename, Function);
429 InstantiationSetCollector.insert(Function, *MainFileID);
432 std::vector<const FunctionRecord *> Result;
433 for (const auto &InstantiationSet : InstantiationSetCollector) {
434 if (InstantiationSet.second.size() < 2)
436 Result.insert(Result.end(), InstantiationSet.second.begin(),
437 InstantiationSet.second.end());
443 CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) {
444 auto MainFileID = findMainViewFileID(Function);
446 return CoverageData();
448 CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
449 std::vector<coverage::CountedRegion> Regions;
450 for (const auto &CR : Function.CountedRegions)
451 if (CR.FileID == *MainFileID) {
452 Regions.push_back(CR);
453 if (isExpansion(CR, *MainFileID))
454 FunctionCoverage.Expansions.emplace_back(CR, Function);
457 sortNestedRegions(Regions.begin(), Regions.end());
458 DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n");
459 FunctionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
461 return FunctionCoverage;
465 CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) {
466 CoverageData ExpansionCoverage(
467 Expansion.Function.Filenames[Expansion.FileID]);
468 std::vector<coverage::CountedRegion> Regions;
469 for (const auto &CR : Expansion.Function.CountedRegions)
470 if (CR.FileID == Expansion.FileID) {
471 Regions.push_back(CR);
472 if (isExpansion(CR, Expansion.FileID))
473 ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
476 sortNestedRegions(Regions.begin(), Regions.end());
477 DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID
479 ExpansionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
481 return ExpansionCoverage;