llvm-cov: Use the number of executed functions for the function coverage metric.
[oota-llvm.git] / lib / ProfileData / CoverageMapping.cpp
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/CoverageMapping.h"
16
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ProfileData/CoverageMappingReader.h"
21 #include "llvm/ProfileData/InstrProfReader.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24
25 using namespace llvm;
26 using namespace coverage;
27
28 #define DEBUG_TYPE "coverage-mapping"
29
30 CounterExpressionBuilder::CounterExpressionBuilder(unsigned NumCounterValues) {
31   Terms.resize(NumCounterValues);
32 }
33
34 Counter CounterExpressionBuilder::get(const CounterExpression &E) {
35   for (unsigned I = 0, S = Expressions.size(); I < S; ++I) {
36     if (Expressions[I] == E)
37       return Counter::getExpression(I);
38   }
39   Expressions.push_back(E);
40   return Counter::getExpression(Expressions.size() - 1);
41 }
42
43 void CounterExpressionBuilder::extractTerms(Counter C, int Sign) {
44   switch (C.getKind()) {
45   case Counter::Zero:
46     break;
47   case Counter::CounterValueReference:
48     Terms[C.getCounterID()] += Sign;
49     break;
50   case Counter::Expression:
51     const auto &E = Expressions[C.getExpressionID()];
52     extractTerms(E.LHS, Sign);
53     extractTerms(E.RHS, E.Kind == CounterExpression::Subtract ? -Sign : Sign);
54     break;
55   }
56 }
57
58 Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
59   // Gather constant terms.
60   for (auto &I : Terms)
61     I = 0;
62   extractTerms(ExpressionTree);
63
64   Counter C;
65   // Create additions.
66   // Note: the additions are created first
67   // to avoid creation of a tree like ((0 - X) + Y) instead of (Y - X).
68   for (unsigned I = 0, S = Terms.size(); I < S; ++I) {
69     if (Terms[I] <= 0)
70       continue;
71     for (int J = 0; J < Terms[I]; ++J) {
72       if (C.isZero())
73         C = Counter::getCounter(I);
74       else
75         C = get(CounterExpression(CounterExpression::Add, C,
76                                   Counter::getCounter(I)));
77     }
78   }
79
80   // Create subtractions.
81   for (unsigned I = 0, S = Terms.size(); I < S; ++I) {
82     if (Terms[I] >= 0)
83       continue;
84     for (int J = 0; J < (-Terms[I]); ++J)
85       C = get(CounterExpression(CounterExpression::Subtract, C,
86                                 Counter::getCounter(I)));
87   }
88   return C;
89 }
90
91 Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) {
92   return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS)));
93 }
94
95 Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) {
96   return simplify(
97       get(CounterExpression(CounterExpression::Subtract, LHS, RHS)));
98 }
99
100 void CounterMappingContext::dump(const Counter &C,
101                                  llvm::raw_ostream &OS) const {
102   switch (C.getKind()) {
103   case Counter::Zero:
104     OS << '0';
105     return;
106   case Counter::CounterValueReference:
107     OS << '#' << C.getCounterID();
108     break;
109   case Counter::Expression: {
110     if (C.getExpressionID() >= Expressions.size())
111       return;
112     const auto &E = Expressions[C.getExpressionID()];
113     OS << '(';
114     dump(E.LHS, OS);
115     OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
116     dump(E.RHS, OS);
117     OS << ')';
118     break;
119   }
120   }
121   if (CounterValues.empty())
122     return;
123   ErrorOr<int64_t> Value = evaluate(C);
124   if (!Value)
125     return;
126   OS << '[' << *Value << ']';
127 }
128
129 ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
130   switch (C.getKind()) {
131   case Counter::Zero:
132     return 0;
133   case Counter::CounterValueReference:
134     if (C.getCounterID() >= CounterValues.size())
135       return std::make_error_code(std::errc::argument_out_of_domain);
136     return CounterValues[C.getCounterID()];
137   case Counter::Expression: {
138     if (C.getExpressionID() >= Expressions.size())
139       return std::make_error_code(std::errc::argument_out_of_domain);
140     const auto &E = Expressions[C.getExpressionID()];
141     ErrorOr<int64_t> LHS = evaluate(E.LHS);
142     if (!LHS)
143       return LHS;
144     ErrorOr<int64_t> RHS = evaluate(E.RHS);
145     if (!RHS)
146       return RHS;
147     return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
148   }
149   }
150   llvm_unreachable("Unhandled CounterKind");
151 }
152
153 ErrorOr<std::unique_ptr<CoverageMapping>>
154 CoverageMapping::load(ObjectFileCoverageMappingReader &CoverageReader,
155                       IndexedInstrProfReader &ProfileReader) {
156   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
157
158   std::vector<uint64_t> Counts;
159   for (const auto &Record : CoverageReader) {
160     Counts.clear();
161     if (std::error_code EC = ProfileReader.getFunctionCounts(
162             Record.FunctionName, Record.FunctionHash, Counts)) {
163       if (EC != instrprof_error::hash_mismatch &&
164           EC != instrprof_error::unknown_function)
165         return EC;
166       Coverage->MismatchedFunctionCount++;
167       continue;
168     }
169
170     assert(Counts.size() != 0 && "Function's counts are empty");
171     FunctionRecord Function(Record.FunctionName, Record.Filenames,
172                             Counts.front());
173     CounterMappingContext Ctx(Record.Expressions, Counts);
174     for (const auto &Region : Record.MappingRegions) {
175       ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
176       if (!ExecutionCount)
177         break;
178       Function.CountedRegions.push_back(CountedRegion(Region, *ExecutionCount));
179     }
180     if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
181       Coverage->MismatchedFunctionCount++;
182       continue;
183     }
184
185     Coverage->Functions.push_back(Function);
186   }
187
188   return std::move(Coverage);
189 }
190
191 ErrorOr<std::unique_ptr<CoverageMapping>>
192 CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename) {
193   auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
194   if (auto EC = CounterMappingBuff.getError())
195     return EC;
196   ObjectFileCoverageMappingReader CoverageReader(CounterMappingBuff.get());
197   if (auto EC = CoverageReader.readHeader())
198     return EC;
199   std::unique_ptr<IndexedInstrProfReader> ProfileReader;
200   if (auto EC = IndexedInstrProfReader::create(ProfileFilename, ProfileReader))
201     return EC;
202   return load(CoverageReader, *ProfileReader);
203 }
204
205 namespace {
206 /// \brief Distributes functions into instantiation sets.
207 ///
208 /// An instantiation set is a collection of functions that have the same source
209 /// code, ie, template functions specializations.
210 class FunctionInstantiationSetCollector {
211   typedef DenseMap<std::pair<unsigned, unsigned>,
212                    std::vector<const FunctionRecord *>> MapT;
213   MapT InstantiatedFunctions;
214
215 public:
216   void insert(const FunctionRecord &Function, unsigned FileID) {
217     auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
218     while (I != E && I->FileID != FileID)
219       ++I;
220     assert(I != E && "function does not cover the given file");
221     auto &Functions = InstantiatedFunctions[I->startLoc()];
222     Functions.push_back(&Function);
223   }
224
225   MapT::iterator begin() { return InstantiatedFunctions.begin(); }
226
227   MapT::iterator end() { return InstantiatedFunctions.end(); }
228 };
229
230 class SegmentBuilder {
231   std::vector<CoverageSegment> Segments;
232   SmallVector<const CountedRegion *, 8> ActiveRegions;
233
234   /// Start a segment with no count specified.
235   void startSegment(unsigned Line, unsigned Col) {
236     DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n");
237     Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false);
238   }
239
240   /// Start a segment with the given Region's count.
241   void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry,
242                     const CountedRegion &Region) {
243     if (Segments.empty())
244       Segments.emplace_back(Line, Col, IsRegionEntry);
245     CoverageSegment S = Segments.back();
246     // Avoid creating empty regions.
247     if (S.Line != Line || S.Col != Col) {
248       Segments.emplace_back(Line, Col, IsRegionEntry);
249       S = Segments.back();
250     }
251     DEBUG(dbgs() << "Segment at " << Line << ":" << Col);
252     // Set this region's count.
253     if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) {
254       DEBUG(dbgs() << " with count " << Region.ExecutionCount);
255       Segments.back().setCount(Region.ExecutionCount);
256     }
257     DEBUG(dbgs() << "\n");
258   }
259
260   /// Start a segment for the given region.
261   void startSegment(const CountedRegion &Region) {
262     startSegment(Region.LineStart, Region.ColumnStart, true, Region);
263   }
264
265   /// Pop the top region off of the active stack, starting a new segment with
266   /// the containing Region's count.
267   void popRegion() {
268     const CountedRegion *Active = ActiveRegions.back();
269     unsigned Line = Active->LineEnd, Col = Active->ColumnEnd;
270     ActiveRegions.pop_back();
271     if (ActiveRegions.empty())
272       startSegment(Line, Col);
273     else
274       startSegment(Line, Col, false, *ActiveRegions.back());
275   }
276
277 public:
278   /// Build a list of CoverageSegments from a sorted list of Regions.
279   std::vector<CoverageSegment> buildSegments(ArrayRef<CountedRegion> Regions) {
280     for (const auto &Region : Regions) {
281       // Pop any regions that end before this one starts.
282       while (!ActiveRegions.empty() &&
283              ActiveRegions.back()->endLoc() <= Region.startLoc())
284         popRegion();
285       if (Segments.size() && Segments.back().Line == Region.LineStart &&
286           Segments.back().Col == Region.ColumnStart) {
287         if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion)
288           Segments.back().addCount(Region.ExecutionCount);
289       } else {
290         // Add this region to the stack.
291         ActiveRegions.push_back(&Region);
292         startSegment(Region);
293       }
294     }
295     // Pop any regions that are left in the stack.
296     while (!ActiveRegions.empty())
297       popRegion();
298     return Segments;
299   }
300 };
301 }
302
303 std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() {
304   std::vector<StringRef> Filenames;
305   for (const auto &Function : getCoveredFunctions())
306     for (const auto &Filename : Function.Filenames)
307       Filenames.push_back(Filename);
308   std::sort(Filenames.begin(), Filenames.end());
309   auto Last = std::unique(Filenames.begin(), Filenames.end());
310   Filenames.erase(Last, Filenames.end());
311   return Filenames;
312 }
313
314 static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
315                                              const FunctionRecord &Function) {
316   llvm::SmallVector<bool, 8> IsExpandedFile(Function.Filenames.size(), false);
317   llvm::SmallVector<bool, 8> FilenameEquivalence(Function.Filenames.size(),
318                                                  false);
319   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
320     if (SourceFile == Function.Filenames[I])
321       FilenameEquivalence[I] = true;
322   for (const auto &CR : Function.CountedRegions)
323     if (CR.Kind == CounterMappingRegion::ExpansionRegion &&
324         FilenameEquivalence[CR.FileID])
325       IsExpandedFile[CR.ExpandedFileID] = true;
326   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
327     if (FilenameEquivalence[I] && !IsExpandedFile[I])
328       return I;
329   return None;
330 }
331
332 static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
333   llvm::SmallVector<bool, 8> IsExpandedFile(Function.Filenames.size(), false);
334   for (const auto &CR : Function.CountedRegions)
335     if (CR.Kind == CounterMappingRegion::ExpansionRegion)
336       IsExpandedFile[CR.ExpandedFileID] = true;
337   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
338     if (!IsExpandedFile[I])
339       return I;
340   return None;
341 }
342
343 static SmallSet<unsigned, 8> gatherFileIDs(StringRef SourceFile,
344                                            const FunctionRecord &Function) {
345   SmallSet<unsigned, 8> IDs;
346   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
347     if (SourceFile == Function.Filenames[I])
348       IDs.insert(I);
349   return IDs;
350 }
351
352 /// Sort a nested sequence of regions from a single file.
353 template <class It> static void sortNestedRegions(It First, It Last) {
354   std::sort(First, Last,
355             [](const CountedRegion &LHS, const CountedRegion &RHS) {
356     if (LHS.startLoc() == RHS.startLoc())
357       // When LHS completely contains RHS, we sort LHS first.
358       return RHS.endLoc() < LHS.endLoc();
359     return LHS.startLoc() < RHS.startLoc();
360   });
361 }
362
363 static bool isExpansion(const CountedRegion &R, unsigned FileID) {
364   return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
365 }
366
367 CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) {
368   CoverageData FileCoverage(Filename);
369   std::vector<coverage::CountedRegion> Regions;
370
371   for (const auto &Function : Functions) {
372     auto MainFileID = findMainViewFileID(Filename, Function);
373     if (!MainFileID)
374       continue;
375     auto FileIDs = gatherFileIDs(Filename, Function);
376     for (const auto &CR : Function.CountedRegions)
377       if (FileIDs.count(CR.FileID)) {
378         Regions.push_back(CR);
379         if (isExpansion(CR, *MainFileID))
380           FileCoverage.Expansions.emplace_back(CR, Function);
381       }
382   }
383
384   sortNestedRegions(Regions.begin(), Regions.end());
385   FileCoverage.Segments = SegmentBuilder().buildSegments(Regions);
386
387   return FileCoverage;
388 }
389
390 std::vector<const FunctionRecord *>
391 CoverageMapping::getInstantiations(StringRef Filename) {
392   FunctionInstantiationSetCollector InstantiationSetCollector;
393   for (const auto &Function : Functions) {
394     auto MainFileID = findMainViewFileID(Filename, Function);
395     if (!MainFileID)
396       continue;
397     InstantiationSetCollector.insert(Function, *MainFileID);
398   }
399
400   std::vector<const FunctionRecord *> Result;
401   for (const auto &InstantiationSet : InstantiationSetCollector) {
402     if (InstantiationSet.second.size() < 2)
403       continue;
404     for (auto Function : InstantiationSet.second)
405       Result.push_back(Function);
406   }
407   return Result;
408 }
409
410 CoverageData
411 CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) {
412   auto MainFileID = findMainViewFileID(Function);
413   if (!MainFileID)
414     return CoverageData();
415
416   CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
417   std::vector<coverage::CountedRegion> Regions;
418   for (const auto &CR : Function.CountedRegions)
419     if (CR.FileID == *MainFileID) {
420       Regions.push_back(CR);
421       if (isExpansion(CR, *MainFileID))
422         FunctionCoverage.Expansions.emplace_back(CR, Function);
423     }
424
425   sortNestedRegions(Regions.begin(), Regions.end());
426   FunctionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
427
428   return FunctionCoverage;
429 }
430
431 CoverageData
432 CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) {
433   CoverageData ExpansionCoverage(
434       Expansion.Function.Filenames[Expansion.FileID]);
435   std::vector<coverage::CountedRegion> Regions;
436   for (const auto &CR : Expansion.Function.CountedRegions)
437     if (CR.FileID == Expansion.FileID) {
438       Regions.push_back(CR);
439       if (isExpansion(CR, Expansion.FileID))
440         ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
441     }
442
443   sortNestedRegions(Regions.begin(), Regions.end());
444   ExpansionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
445
446   return ExpansionCoverage;
447 }