xref: /llvm-project/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp (revision 064535c1eace2b43f3f628f43551422fb5d054f5)
1 //=-- CoverageMapping.cpp - Code coverage mapping support ---------*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for clang's and llvm's instrumentation based
11 // code coverage.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/SmallBitVector.h"
19 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
20 #include "llvm/ProfileData/InstrProfReader.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/Errc.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27 
28 using namespace llvm;
29 using namespace coverage;
30 
31 #define DEBUG_TYPE "coverage-mapping"
32 
33 Counter CounterExpressionBuilder::get(const CounterExpression &E) {
34   auto It = ExpressionIndices.find(E);
35   if (It != ExpressionIndices.end())
36     return Counter::getExpression(It->second);
37   unsigned I = Expressions.size();
38   Expressions.push_back(E);
39   ExpressionIndices[E] = I;
40   return Counter::getExpression(I);
41 }
42 
43 void CounterExpressionBuilder::extractTerms(
44     Counter C, int Sign, SmallVectorImpl<std::pair<unsigned, int>> &Terms) {
45   switch (C.getKind()) {
46   case Counter::Zero:
47     break;
48   case Counter::CounterValueReference:
49     Terms.push_back(std::make_pair(C.getCounterID(), Sign));
50     break;
51   case Counter::Expression:
52     const auto &E = Expressions[C.getExpressionID()];
53     extractTerms(E.LHS, Sign, Terms);
54     extractTerms(E.RHS, E.Kind == CounterExpression::Subtract ? -Sign : Sign,
55                  Terms);
56     break;
57   }
58 }
59 
60 Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
61   // Gather constant terms.
62   llvm::SmallVector<std::pair<unsigned, int>, 32> Terms;
63   extractTerms(ExpressionTree, +1, Terms);
64 
65   // If there are no terms, this is just a zero. The algorithm below assumes at
66   // least one term.
67   if (Terms.size() == 0)
68     return Counter::getZero();
69 
70   // Group the terms by counter ID.
71   std::sort(Terms.begin(), Terms.end(),
72             [](const std::pair<unsigned, int> &LHS,
73                const std::pair<unsigned, int> &RHS) {
74     return LHS.first < RHS.first;
75   });
76 
77   // Combine terms by counter ID to eliminate counters that sum to zero.
78   auto Prev = Terms.begin();
79   for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) {
80     if (I->first == Prev->first) {
81       Prev->second += I->second;
82       continue;
83     }
84     ++Prev;
85     *Prev = *I;
86   }
87   Terms.erase(++Prev, Terms.end());
88 
89   Counter C;
90   // Create additions. We do this before subtractions to avoid constructs like
91   // ((0 - X) + Y), as opposed to (Y - X).
92   for (auto Term : Terms) {
93     if (Term.second <= 0)
94       continue;
95     for (int I = 0; I < Term.second; ++I)
96       if (C.isZero())
97         C = Counter::getCounter(Term.first);
98       else
99         C = get(CounterExpression(CounterExpression::Add, C,
100                                   Counter::getCounter(Term.first)));
101   }
102 
103   // Create subtractions.
104   for (auto Term : Terms) {
105     if (Term.second >= 0)
106       continue;
107     for (int I = 0; I < -Term.second; ++I)
108       C = get(CounterExpression(CounterExpression::Subtract, C,
109                                 Counter::getCounter(Term.first)));
110   }
111   return C;
112 }
113 
114 Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) {
115   return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS)));
116 }
117 
118 Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) {
119   return simplify(
120       get(CounterExpression(CounterExpression::Subtract, LHS, RHS)));
121 }
122 
123 void CounterMappingContext::dump(const Counter &C,
124                                  llvm::raw_ostream &OS) const {
125   switch (C.getKind()) {
126   case Counter::Zero:
127     OS << '0';
128     return;
129   case Counter::CounterValueReference:
130     OS << '#' << C.getCounterID();
131     break;
132   case Counter::Expression: {
133     if (C.getExpressionID() >= Expressions.size())
134       return;
135     const auto &E = Expressions[C.getExpressionID()];
136     OS << '(';
137     dump(E.LHS, OS);
138     OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
139     dump(E.RHS, OS);
140     OS << ')';
141     break;
142   }
143   }
144   if (CounterValues.empty())
145     return;
146   ErrorOr<int64_t> Value = evaluate(C);
147   if (!Value)
148     return;
149   OS << '[' << *Value << ']';
150 }
151 
152 ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
153   switch (C.getKind()) {
154   case Counter::Zero:
155     return 0;
156   case Counter::CounterValueReference:
157     if (C.getCounterID() >= CounterValues.size())
158       return make_error_code(errc::argument_out_of_domain);
159     return CounterValues[C.getCounterID()];
160   case Counter::Expression: {
161     if (C.getExpressionID() >= Expressions.size())
162       return make_error_code(errc::argument_out_of_domain);
163     const auto &E = Expressions[C.getExpressionID()];
164     ErrorOr<int64_t> LHS = evaluate(E.LHS);
165     if (!LHS)
166       return LHS;
167     ErrorOr<int64_t> RHS = evaluate(E.RHS);
168     if (!RHS)
169       return RHS;
170     return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
171   }
172   }
173   llvm_unreachable("Unhandled CounterKind");
174 }
175 
176 void FunctionRecordIterator::skipOtherFiles() {
177   while (Current != Records.end() && !Filename.empty() &&
178          Filename != Current->Filenames[0])
179     ++Current;
180   if (Current == Records.end())
181     *this = FunctionRecordIterator();
182 }
183 
184 ErrorOr<std::unique_ptr<CoverageMapping>>
185 CoverageMapping::load(CoverageMappingReader &CoverageReader,
186                       IndexedInstrProfReader &ProfileReader) {
187   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
188 
189   std::vector<uint64_t> Counts;
190   for (const auto &Record : CoverageReader) {
191     CounterMappingContext Ctx(Record.Expressions);
192 
193     Counts.clear();
194     if (std::error_code EC = ProfileReader.getFunctionCounts(
195             Record.FunctionName, Record.FunctionHash, Counts)) {
196       if (EC == instrprof_error::hash_mismatch) {
197         Coverage->MismatchedFunctionCount++;
198         continue;
199       } else if (EC != instrprof_error::unknown_function)
200         return EC;
201       Counts.assign(Record.MappingRegions.size(), 0);
202     }
203     Ctx.setCounts(Counts);
204 
205     assert(!Record.MappingRegions.empty() && "Function has no regions");
206 
207     StringRef OrigFuncName = Record.FunctionName;
208     if (Record.Filenames.empty())
209       OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
210     else
211       OrigFuncName =
212           getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
213     FunctionRecord Function(OrigFuncName, Record.Filenames);
214     for (const auto &Region : Record.MappingRegions) {
215       ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
216       if (!ExecutionCount)
217         break;
218       Function.pushRegion(Region, *ExecutionCount);
219     }
220     if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
221       Coverage->MismatchedFunctionCount++;
222       continue;
223     }
224 
225     Coverage->Functions.push_back(std::move(Function));
226   }
227 
228   return std::move(Coverage);
229 }
230 
231 ErrorOr<std::unique_ptr<CoverageMapping>>
232 CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename,
233                       StringRef Arch) {
234   auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
235   if (std::error_code EC = CounterMappingBuff.getError())
236     return EC;
237   auto CoverageReaderOrErr =
238       BinaryCoverageReader::create(CounterMappingBuff.get(), Arch);
239   if (std::error_code EC = CoverageReaderOrErr.getError())
240     return EC;
241   auto CoverageReader = std::move(CoverageReaderOrErr.get());
242   auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
243   if (auto EC = ProfileReaderOrErr.getError())
244     return EC;
245   auto ProfileReader = std::move(ProfileReaderOrErr.get());
246   return load(*CoverageReader, *ProfileReader);
247 }
248 
249 namespace {
250 /// \brief Distributes functions into instantiation sets.
251 ///
252 /// An instantiation set is a collection of functions that have the same source
253 /// code, ie, template functions specializations.
254 class FunctionInstantiationSetCollector {
255   typedef DenseMap<std::pair<unsigned, unsigned>,
256                    std::vector<const FunctionRecord *>> MapT;
257   MapT InstantiatedFunctions;
258 
259 public:
260   void insert(const FunctionRecord &Function, unsigned FileID) {
261     auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
262     while (I != E && I->FileID != FileID)
263       ++I;
264     assert(I != E && "function does not cover the given file");
265     auto &Functions = InstantiatedFunctions[I->startLoc()];
266     Functions.push_back(&Function);
267   }
268 
269   MapT::iterator begin() { return InstantiatedFunctions.begin(); }
270 
271   MapT::iterator end() { return InstantiatedFunctions.end(); }
272 };
273 
274 class SegmentBuilder {
275   std::vector<CoverageSegment> &Segments;
276   SmallVector<const CountedRegion *, 8> ActiveRegions;
277 
278   SegmentBuilder(std::vector<CoverageSegment> &Segments) : Segments(Segments) {}
279 
280   /// Start a segment with no count specified.
281   void startSegment(unsigned Line, unsigned Col) {
282     DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n");
283     Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false);
284   }
285 
286   /// Start a segment with the given Region's count.
287   void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry,
288                     const CountedRegion &Region) {
289     // Avoid creating empty regions.
290     if (!Segments.empty() && Segments.back().Line == Line &&
291         Segments.back().Col == Col)
292       Segments.pop_back();
293     DEBUG(dbgs() << "Segment at " << Line << ":" << Col);
294     // Set this region's count.
295     if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) {
296       DEBUG(dbgs() << " with count " << Region.ExecutionCount);
297       Segments.emplace_back(Line, Col, Region.ExecutionCount, IsRegionEntry);
298     } else
299       Segments.emplace_back(Line, Col, IsRegionEntry);
300     DEBUG(dbgs() << "\n");
301   }
302 
303   /// Start a segment for the given region.
304   void startSegment(const CountedRegion &Region) {
305     startSegment(Region.LineStart, Region.ColumnStart, true, Region);
306   }
307 
308   /// Pop the top region off of the active stack, starting a new segment with
309   /// the containing Region's count.
310   void popRegion() {
311     const CountedRegion *Active = ActiveRegions.back();
312     unsigned Line = Active->LineEnd, Col = Active->ColumnEnd;
313     ActiveRegions.pop_back();
314     if (ActiveRegions.empty())
315       startSegment(Line, Col);
316     else
317       startSegment(Line, Col, false, *ActiveRegions.back());
318   }
319 
320   void buildSegmentsImpl(ArrayRef<CountedRegion> Regions) {
321     for (const auto &Region : Regions) {
322       // Pop any regions that end before this one starts.
323       while (!ActiveRegions.empty() &&
324              ActiveRegions.back()->endLoc() <= Region.startLoc())
325         popRegion();
326       // Add this region to the stack.
327       ActiveRegions.push_back(&Region);
328       startSegment(Region);
329     }
330     // Pop any regions that are left in the stack.
331     while (!ActiveRegions.empty())
332       popRegion();
333   }
334 
335   /// Sort a nested sequence of regions from a single file.
336   static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) {
337     std::sort(Regions.begin(), Regions.end(), [](const CountedRegion &LHS,
338                                                  const CountedRegion &RHS) {
339       if (LHS.startLoc() != RHS.startLoc())
340         return LHS.startLoc() < RHS.startLoc();
341       if (LHS.endLoc() != RHS.endLoc())
342         // When LHS completely contains RHS, we sort LHS first.
343         return RHS.endLoc() < LHS.endLoc();
344       // If LHS and RHS cover the same area, we need to sort them according
345       // to their kinds so that the most suitable region will become "active"
346       // in combineRegions(). Because we accumulate counter values only from
347       // regions of the same kind as the first region of the area, prefer
348       // CodeRegion to ExpansionRegion and ExpansionRegion to SkippedRegion.
349       static_assert(coverage::CounterMappingRegion::CodeRegion <
350                             coverage::CounterMappingRegion::ExpansionRegion &&
351                         coverage::CounterMappingRegion::ExpansionRegion <
352                             coverage::CounterMappingRegion::SkippedRegion,
353                     "Unexpected order of region kind values");
354       return LHS.Kind < RHS.Kind;
355     });
356   }
357 
358   /// Combine counts of regions which cover the same area.
359   static ArrayRef<CountedRegion>
360   combineRegions(MutableArrayRef<CountedRegion> Regions) {
361     if (Regions.empty())
362       return Regions;
363     auto Active = Regions.begin();
364     auto End = Regions.end();
365     for (auto I = Regions.begin() + 1; I != End; ++I) {
366       if (Active->startLoc() != I->startLoc() ||
367           Active->endLoc() != I->endLoc()) {
368         // Shift to the next region.
369         ++Active;
370         if (Active != I)
371           *Active = *I;
372         continue;
373       }
374       // Merge duplicate region.
375       // If CodeRegions and ExpansionRegions cover the same area, it's probably
376       // a macro which is fully expanded to another macro. In that case, we need
377       // to accumulate counts only from CodeRegions, or else the area will be
378       // counted twice.
379       // On the other hand, a macro may have a nested macro in its body. If the
380       // outer macro is used several times, the ExpansionRegion for the nested
381       // macro will also be added several times. These ExpansionRegions cover
382       // the same source locations and have to be combined to reach the correct
383       // value for that area.
384       // We add counts of the regions of the same kind as the active region
385       // to handle the both situations.
386       if (I->Kind == Active->Kind)
387         Active->ExecutionCount += I->ExecutionCount;
388     }
389     return Regions.drop_back(std::distance(++Active, End));
390   }
391 
392 public:
393   /// Build a list of CoverageSegments from a list of Regions.
394   static std::vector<CoverageSegment>
395   buildSegments(MutableArrayRef<CountedRegion> Regions) {
396     std::vector<CoverageSegment> Segments;
397     SegmentBuilder Builder(Segments);
398 
399     sortNestedRegions(Regions);
400     ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
401 
402     Builder.buildSegmentsImpl(CombinedRegions);
403     return Segments;
404   }
405 };
406 }
407 
408 std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
409   std::vector<StringRef> Filenames;
410   for (const auto &Function : getCoveredFunctions())
411     Filenames.insert(Filenames.end(), Function.Filenames.begin(),
412                      Function.Filenames.end());
413   std::sort(Filenames.begin(), Filenames.end());
414   auto Last = std::unique(Filenames.begin(), Filenames.end());
415   Filenames.erase(Last, Filenames.end());
416   return Filenames;
417 }
418 
419 static SmallBitVector gatherFileIDs(StringRef SourceFile,
420                                     const FunctionRecord &Function) {
421   SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
422   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
423     if (SourceFile == Function.Filenames[I])
424       FilenameEquivalence[I] = true;
425   return FilenameEquivalence;
426 }
427 
428 /// Return the ID of the file where the definition of the function is located.
429 static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
430   SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
431   for (const auto &CR : Function.CountedRegions)
432     if (CR.Kind == CounterMappingRegion::ExpansionRegion)
433       IsNotExpandedFile[CR.ExpandedFileID] = false;
434   int I = IsNotExpandedFile.find_first();
435   if (I == -1)
436     return None;
437   return I;
438 }
439 
440 /// Check if SourceFile is the file that contains the definition of
441 /// the Function. Return the ID of the file in that case or None otherwise.
442 static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
443                                              const FunctionRecord &Function) {
444   Optional<unsigned> I = findMainViewFileID(Function);
445   if (I && SourceFile == Function.Filenames[*I])
446     return I;
447   return None;
448 }
449 
450 static bool isExpansion(const CountedRegion &R, unsigned FileID) {
451   return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
452 }
453 
454 CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) {
455   CoverageData FileCoverage(Filename);
456   std::vector<coverage::CountedRegion> Regions;
457 
458   for (const auto &Function : Functions) {
459     auto MainFileID = findMainViewFileID(Filename, Function);
460     auto FileIDs = gatherFileIDs(Filename, Function);
461     for (const auto &CR : Function.CountedRegions)
462       if (FileIDs.test(CR.FileID)) {
463         Regions.push_back(CR);
464         if (MainFileID && isExpansion(CR, *MainFileID))
465           FileCoverage.Expansions.emplace_back(CR, Function);
466       }
467   }
468 
469   DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
470   FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
471 
472   return FileCoverage;
473 }
474 
475 std::vector<const FunctionRecord *>
476 CoverageMapping::getInstantiations(StringRef Filename) {
477   FunctionInstantiationSetCollector InstantiationSetCollector;
478   for (const auto &Function : Functions) {
479     auto MainFileID = findMainViewFileID(Filename, Function);
480     if (!MainFileID)
481       continue;
482     InstantiationSetCollector.insert(Function, *MainFileID);
483   }
484 
485   std::vector<const FunctionRecord *> Result;
486   for (const auto &InstantiationSet : InstantiationSetCollector) {
487     if (InstantiationSet.second.size() < 2)
488       continue;
489     Result.insert(Result.end(), InstantiationSet.second.begin(),
490                   InstantiationSet.second.end());
491   }
492   return Result;
493 }
494 
495 CoverageData
496 CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) {
497   auto MainFileID = findMainViewFileID(Function);
498   if (!MainFileID)
499     return CoverageData();
500 
501   CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
502   std::vector<coverage::CountedRegion> Regions;
503   for (const auto &CR : Function.CountedRegions)
504     if (CR.FileID == *MainFileID) {
505       Regions.push_back(CR);
506       if (isExpansion(CR, *MainFileID))
507         FunctionCoverage.Expansions.emplace_back(CR, Function);
508     }
509 
510   DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n");
511   FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
512 
513   return FunctionCoverage;
514 }
515 
516 CoverageData
517 CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) {
518   CoverageData ExpansionCoverage(
519       Expansion.Function.Filenames[Expansion.FileID]);
520   std::vector<coverage::CountedRegion> Regions;
521   for (const auto &CR : Expansion.Function.CountedRegions)
522     if (CR.FileID == Expansion.FileID) {
523       Regions.push_back(CR);
524       if (isExpansion(CR, Expansion.FileID))
525         ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
526     }
527 
528   DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID
529                << "\n");
530   ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
531 
532   return ExpansionCoverage;
533 }
534 
535 namespace {
536 class CoverageMappingErrorCategoryType : public std::error_category {
537   const char *name() const LLVM_NOEXCEPT override { return "llvm.coveragemap"; }
538   std::string message(int IE) const override {
539     auto E = static_cast<coveragemap_error>(IE);
540     switch (E) {
541     case coveragemap_error::success:
542       return "Success";
543     case coveragemap_error::eof:
544       return "End of File";
545     case coveragemap_error::no_data_found:
546       return "No coverage data found";
547     case coveragemap_error::unsupported_version:
548       return "Unsupported coverage format version";
549     case coveragemap_error::truncated:
550       return "Truncated coverage data";
551     case coveragemap_error::malformed:
552       return "Malformed coverage data";
553     }
554     llvm_unreachable("A value of coveragemap_error has no message.");
555   }
556 };
557 }
558 
559 static ManagedStatic<CoverageMappingErrorCategoryType> ErrorCategory;
560 
561 const std::error_category &llvm::coverage::coveragemap_category() {
562   return *ErrorCategory;
563 }
564