xref: /llvm-project/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp (revision 7a7f5348a78f11ccc91c41e637847a4994b09fbf)
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   Expected<int64_t> Value = evaluate(C);
147   if (auto E = Value.takeError()) {
148     llvm::consumeError(std::move(E));
149     return;
150   }
151   OS << '[' << *Value << ']';
152 }
153 
154 Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
155   switch (C.getKind()) {
156   case Counter::Zero:
157     return 0;
158   case Counter::CounterValueReference:
159     if (C.getCounterID() >= CounterValues.size())
160       return errorCodeToError(errc::argument_out_of_domain);
161     return CounterValues[C.getCounterID()];
162   case Counter::Expression: {
163     if (C.getExpressionID() >= Expressions.size())
164       return errorCodeToError(errc::argument_out_of_domain);
165     const auto &E = Expressions[C.getExpressionID()];
166     Expected<int64_t> LHS = evaluate(E.LHS);
167     if (!LHS)
168       return LHS;
169     Expected<int64_t> RHS = evaluate(E.RHS);
170     if (!RHS)
171       return RHS;
172     return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
173   }
174   }
175   llvm_unreachable("Unhandled CounterKind");
176 }
177 
178 void FunctionRecordIterator::skipOtherFiles() {
179   while (Current != Records.end() && !Filename.empty() &&
180          Filename != Current->Filenames[0])
181     ++Current;
182   if (Current == Records.end())
183     *this = FunctionRecordIterator();
184 }
185 
186 Expected<std::unique_ptr<CoverageMapping>>
187 CoverageMapping::load(CoverageMappingReader &CoverageReader,
188                       IndexedInstrProfReader &ProfileReader) {
189   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
190 
191   std::vector<uint64_t> Counts;
192   for (const auto &Record : CoverageReader) {
193     CounterMappingContext Ctx(Record.Expressions);
194 
195     Counts.clear();
196     if (Error E = ProfileReader.getFunctionCounts(
197             Record.FunctionName, Record.FunctionHash, Counts)) {
198       instrprof_error IPE = InstrProfError::take(std::move(E));
199       if (IPE == instrprof_error::hash_mismatch) {
200         Coverage->MismatchedFunctionCount++;
201         continue;
202       } else if (IPE != instrprof_error::unknown_function)
203         return make_error<InstrProfError>(IPE);
204       Counts.assign(Record.MappingRegions.size(), 0);
205     }
206     Ctx.setCounts(Counts);
207 
208     assert(!Record.MappingRegions.empty() && "Function has no regions");
209 
210     StringRef OrigFuncName = Record.FunctionName;
211     if (Record.Filenames.empty())
212       OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
213     else
214       OrigFuncName =
215           getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
216     FunctionRecord Function(OrigFuncName, Record.Filenames);
217     for (const auto &Region : Record.MappingRegions) {
218       Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
219       if (auto E = ExecutionCount.takeError()) {
220         llvm::consumeError(std::move(E));
221         break;
222       }
223       Function.pushRegion(Region, *ExecutionCount);
224     }
225     if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
226       Coverage->MismatchedFunctionCount++;
227       continue;
228     }
229 
230     Coverage->Functions.push_back(std::move(Function));
231   }
232 
233   return std::move(Coverage);
234 }
235 
236 Expected<std::unique_ptr<CoverageMapping>>
237 CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename,
238                       StringRef Arch) {
239   auto CounterMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
240   if (std::error_code EC = CounterMappingBufOrErr.getError())
241     return errorCodeToError(EC);
242   std::unique_ptr<MemoryBuffer> ObjectBuffer =
243       std::move(CounterMappingBufOrErr.get());
244   auto CoverageReaderOrErr =
245       BinaryCoverageReader::create(*ObjectBuffer.get(), Arch);
246   if (Error E = CoverageReaderOrErr.takeError())
247     return std::move(E);
248   auto CoverageReader = std::move(CoverageReaderOrErr.get());
249   auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
250   if (Error E = ProfileReaderOrErr.takeError())
251     return std::move(E);
252   auto ProfileReader = std::move(ProfileReaderOrErr.get());
253   return load(*CoverageReader, *ProfileReader);
254 }
255 
256 namespace {
257 /// \brief Distributes functions into instantiation sets.
258 ///
259 /// An instantiation set is a collection of functions that have the same source
260 /// code, ie, template functions specializations.
261 class FunctionInstantiationSetCollector {
262   typedef DenseMap<std::pair<unsigned, unsigned>,
263                    std::vector<const FunctionRecord *>> MapT;
264   MapT InstantiatedFunctions;
265 
266 public:
267   void insert(const FunctionRecord &Function, unsigned FileID) {
268     auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
269     while (I != E && I->FileID != FileID)
270       ++I;
271     assert(I != E && "function does not cover the given file");
272     auto &Functions = InstantiatedFunctions[I->startLoc()];
273     Functions.push_back(&Function);
274   }
275 
276   MapT::iterator begin() { return InstantiatedFunctions.begin(); }
277 
278   MapT::iterator end() { return InstantiatedFunctions.end(); }
279 };
280 
281 class SegmentBuilder {
282   std::vector<CoverageSegment> &Segments;
283   SmallVector<const CountedRegion *, 8> ActiveRegions;
284 
285   SegmentBuilder(std::vector<CoverageSegment> &Segments) : Segments(Segments) {}
286 
287   /// Start a segment with no count specified.
288   void startSegment(unsigned Line, unsigned Col) {
289     DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n");
290     Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false);
291   }
292 
293   /// Start a segment with the given Region's count.
294   void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry,
295                     const CountedRegion &Region) {
296     // Avoid creating empty regions.
297     if (!Segments.empty() && Segments.back().Line == Line &&
298         Segments.back().Col == Col)
299       Segments.pop_back();
300     DEBUG(dbgs() << "Segment at " << Line << ":" << Col);
301     // Set this region's count.
302     if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) {
303       DEBUG(dbgs() << " with count " << Region.ExecutionCount);
304       Segments.emplace_back(Line, Col, Region.ExecutionCount, IsRegionEntry);
305     } else
306       Segments.emplace_back(Line, Col, IsRegionEntry);
307     DEBUG(dbgs() << "\n");
308   }
309 
310   /// Start a segment for the given region.
311   void startSegment(const CountedRegion &Region) {
312     startSegment(Region.LineStart, Region.ColumnStart, true, Region);
313   }
314 
315   /// Pop the top region off of the active stack, starting a new segment with
316   /// the containing Region's count.
317   void popRegion() {
318     const CountedRegion *Active = ActiveRegions.back();
319     unsigned Line = Active->LineEnd, Col = Active->ColumnEnd;
320     ActiveRegions.pop_back();
321     if (ActiveRegions.empty())
322       startSegment(Line, Col);
323     else
324       startSegment(Line, Col, false, *ActiveRegions.back());
325   }
326 
327   void buildSegmentsImpl(ArrayRef<CountedRegion> Regions) {
328     for (const auto &Region : Regions) {
329       // Pop any regions that end before this one starts.
330       while (!ActiveRegions.empty() &&
331              ActiveRegions.back()->endLoc() <= Region.startLoc())
332         popRegion();
333       // Add this region to the stack.
334       ActiveRegions.push_back(&Region);
335       startSegment(Region);
336     }
337     // Pop any regions that are left in the stack.
338     while (!ActiveRegions.empty())
339       popRegion();
340   }
341 
342   /// Sort a nested sequence of regions from a single file.
343   static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) {
344     std::sort(Regions.begin(), Regions.end(), [](const CountedRegion &LHS,
345                                                  const CountedRegion &RHS) {
346       if (LHS.startLoc() != RHS.startLoc())
347         return LHS.startLoc() < RHS.startLoc();
348       if (LHS.endLoc() != RHS.endLoc())
349         // When LHS completely contains RHS, we sort LHS first.
350         return RHS.endLoc() < LHS.endLoc();
351       // If LHS and RHS cover the same area, we need to sort them according
352       // to their kinds so that the most suitable region will become "active"
353       // in combineRegions(). Because we accumulate counter values only from
354       // regions of the same kind as the first region of the area, prefer
355       // CodeRegion to ExpansionRegion and ExpansionRegion to SkippedRegion.
356       static_assert(coverage::CounterMappingRegion::CodeRegion <
357                             coverage::CounterMappingRegion::ExpansionRegion &&
358                         coverage::CounterMappingRegion::ExpansionRegion <
359                             coverage::CounterMappingRegion::SkippedRegion,
360                     "Unexpected order of region kind values");
361       return LHS.Kind < RHS.Kind;
362     });
363   }
364 
365   /// Combine counts of regions which cover the same area.
366   static ArrayRef<CountedRegion>
367   combineRegions(MutableArrayRef<CountedRegion> Regions) {
368     if (Regions.empty())
369       return Regions;
370     auto Active = Regions.begin();
371     auto End = Regions.end();
372     for (auto I = Regions.begin() + 1; I != End; ++I) {
373       if (Active->startLoc() != I->startLoc() ||
374           Active->endLoc() != I->endLoc()) {
375         // Shift to the next region.
376         ++Active;
377         if (Active != I)
378           *Active = *I;
379         continue;
380       }
381       // Merge duplicate region.
382       // If CodeRegions and ExpansionRegions cover the same area, it's probably
383       // a macro which is fully expanded to another macro. In that case, we need
384       // to accumulate counts only from CodeRegions, or else the area will be
385       // counted twice.
386       // On the other hand, a macro may have a nested macro in its body. If the
387       // outer macro is used several times, the ExpansionRegion for the nested
388       // macro will also be added several times. These ExpansionRegions cover
389       // the same source locations and have to be combined to reach the correct
390       // value for that area.
391       // We add counts of the regions of the same kind as the active region
392       // to handle the both situations.
393       if (I->Kind == Active->Kind)
394         Active->ExecutionCount += I->ExecutionCount;
395     }
396     return Regions.drop_back(std::distance(++Active, End));
397   }
398 
399 public:
400   /// Build a list of CoverageSegments from a list of Regions.
401   static std::vector<CoverageSegment>
402   buildSegments(MutableArrayRef<CountedRegion> Regions) {
403     std::vector<CoverageSegment> Segments;
404     SegmentBuilder Builder(Segments);
405 
406     sortNestedRegions(Regions);
407     ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
408 
409     Builder.buildSegmentsImpl(CombinedRegions);
410     return Segments;
411   }
412 };
413 }
414 
415 std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
416   std::vector<StringRef> Filenames;
417   for (const auto &Function : getCoveredFunctions())
418     Filenames.insert(Filenames.end(), Function.Filenames.begin(),
419                      Function.Filenames.end());
420   std::sort(Filenames.begin(), Filenames.end());
421   auto Last = std::unique(Filenames.begin(), Filenames.end());
422   Filenames.erase(Last, Filenames.end());
423   return Filenames;
424 }
425 
426 static SmallBitVector gatherFileIDs(StringRef SourceFile,
427                                     const FunctionRecord &Function) {
428   SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
429   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
430     if (SourceFile == Function.Filenames[I])
431       FilenameEquivalence[I] = true;
432   return FilenameEquivalence;
433 }
434 
435 /// Return the ID of the file where the definition of the function is located.
436 static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
437   SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
438   for (const auto &CR : Function.CountedRegions)
439     if (CR.Kind == CounterMappingRegion::ExpansionRegion)
440       IsNotExpandedFile[CR.ExpandedFileID] = false;
441   int I = IsNotExpandedFile.find_first();
442   if (I == -1)
443     return None;
444   return I;
445 }
446 
447 /// Check if SourceFile is the file that contains the definition of
448 /// the Function. Return the ID of the file in that case or None otherwise.
449 static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
450                                              const FunctionRecord &Function) {
451   Optional<unsigned> I = findMainViewFileID(Function);
452   if (I && SourceFile == Function.Filenames[*I])
453     return I;
454   return None;
455 }
456 
457 static bool isExpansion(const CountedRegion &R, unsigned FileID) {
458   return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
459 }
460 
461 CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) {
462   CoverageData FileCoverage(Filename);
463   std::vector<coverage::CountedRegion> Regions;
464 
465   for (const auto &Function : Functions) {
466     auto MainFileID = findMainViewFileID(Filename, Function);
467     auto FileIDs = gatherFileIDs(Filename, Function);
468     for (const auto &CR : Function.CountedRegions)
469       if (FileIDs.test(CR.FileID)) {
470         Regions.push_back(CR);
471         if (MainFileID && isExpansion(CR, *MainFileID))
472           FileCoverage.Expansions.emplace_back(CR, Function);
473       }
474   }
475 
476   DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
477   FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
478 
479   return FileCoverage;
480 }
481 
482 std::vector<const FunctionRecord *>
483 CoverageMapping::getInstantiations(StringRef Filename) {
484   FunctionInstantiationSetCollector InstantiationSetCollector;
485   for (const auto &Function : Functions) {
486     auto MainFileID = findMainViewFileID(Filename, Function);
487     if (!MainFileID)
488       continue;
489     InstantiationSetCollector.insert(Function, *MainFileID);
490   }
491 
492   std::vector<const FunctionRecord *> Result;
493   for (const auto &InstantiationSet : InstantiationSetCollector) {
494     if (InstantiationSet.second.size() < 2)
495       continue;
496     Result.insert(Result.end(), InstantiationSet.second.begin(),
497                   InstantiationSet.second.end());
498   }
499   return Result;
500 }
501 
502 CoverageData
503 CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) {
504   auto MainFileID = findMainViewFileID(Function);
505   if (!MainFileID)
506     return CoverageData();
507 
508   CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
509   std::vector<coverage::CountedRegion> Regions;
510   for (const auto &CR : Function.CountedRegions)
511     if (CR.FileID == *MainFileID) {
512       Regions.push_back(CR);
513       if (isExpansion(CR, *MainFileID))
514         FunctionCoverage.Expansions.emplace_back(CR, Function);
515     }
516 
517   DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n");
518   FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
519 
520   return FunctionCoverage;
521 }
522 
523 CoverageData
524 CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) {
525   CoverageData ExpansionCoverage(
526       Expansion.Function.Filenames[Expansion.FileID]);
527   std::vector<coverage::CountedRegion> Regions;
528   for (const auto &CR : Expansion.Function.CountedRegions)
529     if (CR.FileID == Expansion.FileID) {
530       Regions.push_back(CR);
531       if (isExpansion(CR, Expansion.FileID))
532         ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
533     }
534 
535   DEBUG(dbgs() << "Emitting segments for expansion of file " << Expansion.FileID
536                << "\n");
537   ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
538 
539   return ExpansionCoverage;
540 }
541 
542 namespace {
543 std::string getCoverageMapErrString(coveragemap_error Err) {
544   switch (Err) {
545   case coveragemap_error::success:
546     return "Success";
547   case coveragemap_error::eof:
548     return "End of File";
549   case coveragemap_error::no_data_found:
550     return "No coverage data found";
551   case coveragemap_error::unsupported_version:
552     return "Unsupported coverage format version";
553   case coveragemap_error::truncated:
554     return "Truncated coverage data";
555   case coveragemap_error::malformed:
556     return "Malformed coverage data";
557   }
558   llvm_unreachable("A value of coveragemap_error has no message.");
559 }
560 
561 // FIXME: This class is only here to support the transition to llvm::Error. It
562 // will be removed once this transition is complete. Clients should prefer to
563 // deal with the Error value directly, rather than converting to error_code.
564 class CoverageMappingErrorCategoryType : public std::error_category {
565   const char *name() const LLVM_NOEXCEPT override { return "llvm.coveragemap"; }
566   std::string message(int IE) const override {
567     return getCoverageMapErrString(static_cast<coveragemap_error>(IE));
568   }
569 };
570 } // end anonymous namespace
571 
572 std::string CoverageMapError::message() const {
573   return getCoverageMapErrString(Err);
574 }
575 
576 static ManagedStatic<CoverageMappingErrorCategoryType> ErrorCategory;
577 
578 const std::error_category &llvm::coverage::coveragemap_category() {
579   return *ErrorCategory;
580 }
581 
582 char CoverageMapError::ID = 0;
583