172208a82SEugene Zelenko //===- CoverageMapping.cpp - Code coverage mapping support ----------------===//
2dc707122SEaswaran Raman //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6dc707122SEaswaran Raman //
7dc707122SEaswaran Raman //===----------------------------------------------------------------------===//
8dc707122SEaswaran Raman //
9dc707122SEaswaran Raman // This file contains support for clang's and llvm's instrumentation based
10dc707122SEaswaran Raman // code coverage.
11dc707122SEaswaran Raman //
12dc707122SEaswaran Raman //===----------------------------------------------------------------------===//
13dc707122SEaswaran Raman 
146bda14b3SChandler Carruth #include "llvm/ProfileData/Coverage/CoverageMapping.h"
15e78d131aSEugene Zelenko #include "llvm/ADT/ArrayRef.h"
16dc707122SEaswaran Raman #include "llvm/ADT/DenseMap.h"
17e78d131aSEugene Zelenko #include "llvm/ADT/None.h"
18dc707122SEaswaran Raman #include "llvm/ADT/Optional.h"
19dc707122SEaswaran Raman #include "llvm/ADT/SmallBitVector.h"
20e78d131aSEugene Zelenko #include "llvm/ADT/SmallVector.h"
21e78d131aSEugene Zelenko #include "llvm/ADT/StringRef.h"
22dc707122SEaswaran Raman #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
23dc707122SEaswaran Raman #include "llvm/ProfileData/InstrProfReader.h"
24dc707122SEaswaran Raman #include "llvm/Support/Debug.h"
25dc707122SEaswaran Raman #include "llvm/Support/Errc.h"
26e78d131aSEugene Zelenko #include "llvm/Support/Error.h"
27dc707122SEaswaran Raman #include "llvm/Support/ErrorHandling.h"
28dc707122SEaswaran Raman #include "llvm/Support/ManagedStatic.h"
29e78d131aSEugene Zelenko #include "llvm/Support/MemoryBuffer.h"
30dc707122SEaswaran Raman #include "llvm/Support/raw_ostream.h"
31e78d131aSEugene Zelenko #include <algorithm>
32e78d131aSEugene Zelenko #include <cassert>
33e78d131aSEugene Zelenko #include <cstdint>
34e78d131aSEugene Zelenko #include <iterator>
357bef6da6SVedant Kumar #include <map>
36e78d131aSEugene Zelenko #include <memory>
37e78d131aSEugene Zelenko #include <string>
38e78d131aSEugene Zelenko #include <system_error>
39e78d131aSEugene Zelenko #include <utility>
40e78d131aSEugene Zelenko #include <vector>
41dc707122SEaswaran Raman 
42dc707122SEaswaran Raman using namespace llvm;
43dc707122SEaswaran Raman using namespace coverage;
44dc707122SEaswaran Raman 
45dc707122SEaswaran Raman #define DEBUG_TYPE "coverage-mapping"
46dc707122SEaswaran Raman 
47dc707122SEaswaran Raman Counter CounterExpressionBuilder::get(const CounterExpression &E) {
48dc707122SEaswaran Raman   auto It = ExpressionIndices.find(E);
49dc707122SEaswaran Raman   if (It != ExpressionIndices.end())
50dc707122SEaswaran Raman     return Counter::getExpression(It->second);
51dc707122SEaswaran Raman   unsigned I = Expressions.size();
52dc707122SEaswaran Raman   Expressions.push_back(E);
53dc707122SEaswaran Raman   ExpressionIndices[E] = I;
54dc707122SEaswaran Raman   return Counter::getExpression(I);
55dc707122SEaswaran Raman }
56dc707122SEaswaran Raman 
5771b3d721SVedant Kumar void CounterExpressionBuilder::extractTerms(Counter C, int Factor,
5871b3d721SVedant Kumar                                             SmallVectorImpl<Term> &Terms) {
59dc707122SEaswaran Raman   switch (C.getKind()) {
60dc707122SEaswaran Raman   case Counter::Zero:
61dc707122SEaswaran Raman     break;
62dc707122SEaswaran Raman   case Counter::CounterValueReference:
6371b3d721SVedant Kumar     Terms.emplace_back(C.getCounterID(), Factor);
64dc707122SEaswaran Raman     break;
65dc707122SEaswaran Raman   case Counter::Expression:
66dc707122SEaswaran Raman     const auto &E = Expressions[C.getExpressionID()];
6771b3d721SVedant Kumar     extractTerms(E.LHS, Factor, Terms);
6871b3d721SVedant Kumar     extractTerms(
6971b3d721SVedant Kumar         E.RHS, E.Kind == CounterExpression::Subtract ? -Factor : Factor, Terms);
70dc707122SEaswaran Raman     break;
71dc707122SEaswaran Raman   }
72dc707122SEaswaran Raman }
73dc707122SEaswaran Raman 
74dc707122SEaswaran Raman Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
75dc707122SEaswaran Raman   // Gather constant terms.
7671b3d721SVedant Kumar   SmallVector<Term, 32> Terms;
77dc707122SEaswaran Raman   extractTerms(ExpressionTree, +1, Terms);
78dc707122SEaswaran Raman 
79dc707122SEaswaran Raman   // If there are no terms, this is just a zero. The algorithm below assumes at
80dc707122SEaswaran Raman   // least one term.
81dc707122SEaswaran Raman   if (Terms.size() == 0)
82dc707122SEaswaran Raman     return Counter::getZero();
83dc707122SEaswaran Raman 
84dc707122SEaswaran Raman   // Group the terms by counter ID.
850cac726aSFangrui Song   llvm::sort(Terms, [](const Term &LHS, const Term &RHS) {
8671b3d721SVedant Kumar     return LHS.CounterID < RHS.CounterID;
87dc707122SEaswaran Raman   });
88dc707122SEaswaran Raman 
89dc707122SEaswaran Raman   // Combine terms by counter ID to eliminate counters that sum to zero.
90dc707122SEaswaran Raman   auto Prev = Terms.begin();
91dc707122SEaswaran Raman   for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) {
9271b3d721SVedant Kumar     if (I->CounterID == Prev->CounterID) {
9371b3d721SVedant Kumar       Prev->Factor += I->Factor;
94dc707122SEaswaran Raman       continue;
95dc707122SEaswaran Raman     }
96dc707122SEaswaran Raman     ++Prev;
97dc707122SEaswaran Raman     *Prev = *I;
98dc707122SEaswaran Raman   }
99dc707122SEaswaran Raman   Terms.erase(++Prev, Terms.end());
100dc707122SEaswaran Raman 
101dc707122SEaswaran Raman   Counter C;
102dc707122SEaswaran Raman   // Create additions. We do this before subtractions to avoid constructs like
103dc707122SEaswaran Raman   // ((0 - X) + Y), as opposed to (Y - X).
10471b3d721SVedant Kumar   for (auto T : Terms) {
10571b3d721SVedant Kumar     if (T.Factor <= 0)
106dc707122SEaswaran Raman       continue;
10771b3d721SVedant Kumar     for (int I = 0; I < T.Factor; ++I)
108dc707122SEaswaran Raman       if (C.isZero())
10971b3d721SVedant Kumar         C = Counter::getCounter(T.CounterID);
110dc707122SEaswaran Raman       else
111dc707122SEaswaran Raman         C = get(CounterExpression(CounterExpression::Add, C,
11271b3d721SVedant Kumar                                   Counter::getCounter(T.CounterID)));
113dc707122SEaswaran Raman   }
114dc707122SEaswaran Raman 
115dc707122SEaswaran Raman   // Create subtractions.
11671b3d721SVedant Kumar   for (auto T : Terms) {
11771b3d721SVedant Kumar     if (T.Factor >= 0)
118dc707122SEaswaran Raman       continue;
11971b3d721SVedant Kumar     for (int I = 0; I < -T.Factor; ++I)
120dc707122SEaswaran Raman       C = get(CounterExpression(CounterExpression::Subtract, C,
12171b3d721SVedant Kumar                                 Counter::getCounter(T.CounterID)));
122dc707122SEaswaran Raman   }
123dc707122SEaswaran Raman   return C;
124dc707122SEaswaran Raman }
125dc707122SEaswaran Raman 
126dc707122SEaswaran Raman Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) {
127dc707122SEaswaran Raman   return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS)));
128dc707122SEaswaran Raman }
129dc707122SEaswaran Raman 
130dc707122SEaswaran Raman Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) {
131dc707122SEaswaran Raman   return simplify(
132dc707122SEaswaran Raman       get(CounterExpression(CounterExpression::Subtract, LHS, RHS)));
133dc707122SEaswaran Raman }
134dc707122SEaswaran Raman 
135e78d131aSEugene Zelenko void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
136dc707122SEaswaran Raman   switch (C.getKind()) {
137dc707122SEaswaran Raman   case Counter::Zero:
138dc707122SEaswaran Raman     OS << '0';
139dc707122SEaswaran Raman     return;
140dc707122SEaswaran Raman   case Counter::CounterValueReference:
141dc707122SEaswaran Raman     OS << '#' << C.getCounterID();
142dc707122SEaswaran Raman     break;
143dc707122SEaswaran Raman   case Counter::Expression: {
144dc707122SEaswaran Raman     if (C.getExpressionID() >= Expressions.size())
145dc707122SEaswaran Raman       return;
146dc707122SEaswaran Raman     const auto &E = Expressions[C.getExpressionID()];
147dc707122SEaswaran Raman     OS << '(';
148dc707122SEaswaran Raman     dump(E.LHS, OS);
149dc707122SEaswaran Raman     OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
150dc707122SEaswaran Raman     dump(E.RHS, OS);
151dc707122SEaswaran Raman     OS << ')';
152dc707122SEaswaran Raman     break;
153dc707122SEaswaran Raman   }
154dc707122SEaswaran Raman   }
155dc707122SEaswaran Raman   if (CounterValues.empty())
156dc707122SEaswaran Raman     return;
1579152fd17SVedant Kumar   Expected<int64_t> Value = evaluate(C);
1589152fd17SVedant Kumar   if (auto E = Value.takeError()) {
159e78d131aSEugene Zelenko     consumeError(std::move(E));
160dc707122SEaswaran Raman     return;
1619152fd17SVedant Kumar   }
162dc707122SEaswaran Raman   OS << '[' << *Value << ']';
163dc707122SEaswaran Raman }
164dc707122SEaswaran Raman 
1659152fd17SVedant Kumar Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
166dc707122SEaswaran Raman   switch (C.getKind()) {
167dc707122SEaswaran Raman   case Counter::Zero:
168dc707122SEaswaran Raman     return 0;
169dc707122SEaswaran Raman   case Counter::CounterValueReference:
170dc707122SEaswaran Raman     if (C.getCounterID() >= CounterValues.size())
1719152fd17SVedant Kumar       return errorCodeToError(errc::argument_out_of_domain);
172dc707122SEaswaran Raman     return CounterValues[C.getCounterID()];
173dc707122SEaswaran Raman   case Counter::Expression: {
174dc707122SEaswaran Raman     if (C.getExpressionID() >= Expressions.size())
1759152fd17SVedant Kumar       return errorCodeToError(errc::argument_out_of_domain);
176dc707122SEaswaran Raman     const auto &E = Expressions[C.getExpressionID()];
1779152fd17SVedant Kumar     Expected<int64_t> LHS = evaluate(E.LHS);
178dc707122SEaswaran Raman     if (!LHS)
179dc707122SEaswaran Raman       return LHS;
1809152fd17SVedant Kumar     Expected<int64_t> RHS = evaluate(E.RHS);
181dc707122SEaswaran Raman     if (!RHS)
182dc707122SEaswaran Raman       return RHS;
183dc707122SEaswaran Raman     return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
184dc707122SEaswaran Raman   }
185dc707122SEaswaran Raman   }
186dc707122SEaswaran Raman   llvm_unreachable("Unhandled CounterKind");
187dc707122SEaswaran Raman }
188dc707122SEaswaran Raman 
189*e4274cfeSPirama Arumuga Nainar unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const {
190*e4274cfeSPirama Arumuga Nainar   switch (C.getKind()) {
191*e4274cfeSPirama Arumuga Nainar   case Counter::Zero:
192*e4274cfeSPirama Arumuga Nainar     return 0;
193*e4274cfeSPirama Arumuga Nainar   case Counter::CounterValueReference:
194*e4274cfeSPirama Arumuga Nainar     return C.getCounterID();
195*e4274cfeSPirama Arumuga Nainar   case Counter::Expression: {
196*e4274cfeSPirama Arumuga Nainar     if (C.getExpressionID() >= Expressions.size())
197*e4274cfeSPirama Arumuga Nainar       return 0;
198*e4274cfeSPirama Arumuga Nainar     const auto &E = Expressions[C.getExpressionID()];
199*e4274cfeSPirama Arumuga Nainar     return std::max(getMaxCounterID(E.LHS), getMaxCounterID(E.RHS));
200*e4274cfeSPirama Arumuga Nainar   }
201*e4274cfeSPirama Arumuga Nainar   }
202*e4274cfeSPirama Arumuga Nainar   llvm_unreachable("Unhandled CounterKind");
203*e4274cfeSPirama Arumuga Nainar }
204*e4274cfeSPirama Arumuga Nainar 
205dc707122SEaswaran Raman void FunctionRecordIterator::skipOtherFiles() {
206dc707122SEaswaran Raman   while (Current != Records.end() && !Filename.empty() &&
207dc707122SEaswaran Raman          Filename != Current->Filenames[0])
208dc707122SEaswaran Raman     ++Current;
209dc707122SEaswaran Raman   if (Current == Records.end())
210dc707122SEaswaran Raman     *this = FunctionRecordIterator();
211dc707122SEaswaran Raman }
212dc707122SEaswaran Raman 
213413647d7SVedant Kumar ArrayRef<unsigned> CoverageMapping::getImpreciseRecordIndicesForFilename(
214413647d7SVedant Kumar     StringRef Filename) const {
215413647d7SVedant Kumar   size_t FilenameHash = hash_value(Filename);
216413647d7SVedant Kumar   auto RecordIt = FilenameHash2RecordIndices.find(FilenameHash);
217413647d7SVedant Kumar   if (RecordIt == FilenameHash2RecordIndices.end())
218413647d7SVedant Kumar     return {};
219413647d7SVedant Kumar   return RecordIt->second;
220413647d7SVedant Kumar }
221413647d7SVedant Kumar 
222*e4274cfeSPirama Arumuga Nainar static unsigned getMaxCounterID(const CounterMappingContext &Ctx,
223*e4274cfeSPirama Arumuga Nainar                                 const CoverageMappingRecord &Record) {
224*e4274cfeSPirama Arumuga Nainar   unsigned MaxCounterID = 0;
225*e4274cfeSPirama Arumuga Nainar   for (const auto &Region : Record.MappingRegions) {
226*e4274cfeSPirama Arumuga Nainar     MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count));
227*e4274cfeSPirama Arumuga Nainar   }
228*e4274cfeSPirama Arumuga Nainar   return MaxCounterID;
229*e4274cfeSPirama Arumuga Nainar }
230*e4274cfeSPirama Arumuga Nainar 
23168216d7bSVedant Kumar Error CoverageMapping::loadFunctionRecord(
23268216d7bSVedant Kumar     const CoverageMappingRecord &Record,
233dc707122SEaswaran Raman     IndexedInstrProfReader &ProfileReader) {
234743574b8SVedant Kumar   StringRef OrigFuncName = Record.FunctionName;
235b1d331a3SVedant Kumar   if (OrigFuncName.empty())
236b1d331a3SVedant Kumar     return make_error<CoverageMapError>(coveragemap_error::malformed);
237b1d331a3SVedant Kumar 
238743574b8SVedant Kumar   if (Record.Filenames.empty())
239743574b8SVedant Kumar     OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
240743574b8SVedant Kumar   else
241743574b8SVedant Kumar     OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
242743574b8SVedant Kumar 
243dc707122SEaswaran Raman   CounterMappingContext Ctx(Record.Expressions);
244dc707122SEaswaran Raman 
24568216d7bSVedant Kumar   std::vector<uint64_t> Counts;
24668216d7bSVedant Kumar   if (Error E = ProfileReader.getFunctionCounts(Record.FunctionName,
24768216d7bSVedant Kumar                                                 Record.FunctionHash, Counts)) {
2489152fd17SVedant Kumar     instrprof_error IPE = InstrProfError::take(std::move(E));
2499152fd17SVedant Kumar     if (IPE == instrprof_error::hash_mismatch) {
250a9bc7b83SBenjamin Kramer       FuncHashMismatches.emplace_back(std::string(Record.FunctionName),
251a9bc7b83SBenjamin Kramer                                       Record.FunctionHash);
25268216d7bSVedant Kumar       return Error::success();
2539152fd17SVedant Kumar     } else if (IPE != instrprof_error::unknown_function)
2549152fd17SVedant Kumar       return make_error<InstrProfError>(IPE);
255*e4274cfeSPirama Arumuga Nainar     Counts.assign(getMaxCounterID(Ctx, Record) + 1, 0);
256dc707122SEaswaran Raman   }
257dc707122SEaswaran Raman   Ctx.setCounts(Counts);
258dc707122SEaswaran Raman 
259dc707122SEaswaran Raman   assert(!Record.MappingRegions.empty() && "Function has no regions");
260dc707122SEaswaran Raman 
261381e9d23SVedant Kumar   // This coverage record is a zero region for a function that's unused in
262381e9d23SVedant Kumar   // some TU, but used in a different TU. Ignore it. The coverage maps from the
263381e9d23SVedant Kumar   // the other TU will either be loaded (providing full region counts) or they
264381e9d23SVedant Kumar   // won't (in which case we don't unintuitively report functions as uncovered
265381e9d23SVedant Kumar   // when they have non-zero counts in the profile).
266381e9d23SVedant Kumar   if (Record.MappingRegions.size() == 1 &&
267381e9d23SVedant Kumar       Record.MappingRegions[0].Count.isZero() && Counts[0] > 0)
268381e9d23SVedant Kumar     return Error::success();
269381e9d23SVedant Kumar 
270dc707122SEaswaran Raman   FunctionRecord Function(OrigFuncName, Record.Filenames);
271dc707122SEaswaran Raman   for (const auto &Region : Record.MappingRegions) {
2729152fd17SVedant Kumar     Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
2739152fd17SVedant Kumar     if (auto E = ExecutionCount.takeError()) {
274e78d131aSEugene Zelenko       consumeError(std::move(E));
27568216d7bSVedant Kumar       return Error::success();
2769152fd17SVedant Kumar     }
2779f2967bcSAlan Phipps     Expected<int64_t> AltExecutionCount = Ctx.evaluate(Region.FalseCount);
2789f2967bcSAlan Phipps     if (auto E = AltExecutionCount.takeError()) {
2799f2967bcSAlan Phipps       consumeError(std::move(E));
2809f2967bcSAlan Phipps       return Error::success();
2819f2967bcSAlan Phipps     }
2829f2967bcSAlan Phipps     Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount);
283dc707122SEaswaran Raman   }
284dc707122SEaswaran Raman 
285381e9d23SVedant Kumar   // Don't create records for (filenames, function) pairs we've already seen.
286381e9d23SVedant Kumar   auto FilenamesHash = hash_combine_range(Record.Filenames.begin(),
287381e9d23SVedant Kumar                                           Record.Filenames.end());
288381e9d23SVedant Kumar   if (!RecordProvenance[FilenamesHash].insert(hash_value(OrigFuncName)).second)
289381e9d23SVedant Kumar     return Error::success();
290381e9d23SVedant Kumar 
29168216d7bSVedant Kumar   Functions.push_back(std::move(Function));
292413647d7SVedant Kumar 
293413647d7SVedant Kumar   // Performance optimization: keep track of the indices of the function records
294413647d7SVedant Kumar   // which correspond to each filename. This can be used to substantially speed
295413647d7SVedant Kumar   // up queries for coverage info in a file.
296413647d7SVedant Kumar   unsigned RecordIndex = Functions.size() - 1;
297413647d7SVedant Kumar   for (StringRef Filename : Record.Filenames) {
298413647d7SVedant Kumar     auto &RecordIndices = FilenameHash2RecordIndices[hash_value(Filename)];
299413647d7SVedant Kumar     // Note that there may be duplicates in the filename set for a function
300413647d7SVedant Kumar     // record, because of e.g. macro expansions in the function in which both
301413647d7SVedant Kumar     // the macro and the function are defined in the same file.
302413647d7SVedant Kumar     if (RecordIndices.empty() || RecordIndices.back() != RecordIndex)
303413647d7SVedant Kumar       RecordIndices.push_back(RecordIndex);
304413647d7SVedant Kumar   }
305413647d7SVedant Kumar 
30668216d7bSVedant Kumar   return Error::success();
307dc707122SEaswaran Raman }
308dc707122SEaswaran Raman 
309772e1dd1SChoongwoo Han // This function is for memory optimization by shortening the lifetimes
310772e1dd1SChoongwoo Han // of CoverageMappingReader instances.
311772e1dd1SChoongwoo Han Error CoverageMapping::loadFromReaders(
312772e1dd1SChoongwoo Han     ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
313772e1dd1SChoongwoo Han     IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage) {
314772e1dd1SChoongwoo Han   for (const auto &CoverageReader : CoverageReaders) {
315772e1dd1SChoongwoo Han     for (auto RecordOrErr : *CoverageReader) {
316772e1dd1SChoongwoo Han       if (Error E = RecordOrErr.takeError())
317772e1dd1SChoongwoo Han         return E;
318772e1dd1SChoongwoo Han       const auto &Record = *RecordOrErr;
319772e1dd1SChoongwoo Han       if (Error E = Coverage.loadFunctionRecord(Record, ProfileReader))
320772e1dd1SChoongwoo Han         return E;
321772e1dd1SChoongwoo Han     }
322772e1dd1SChoongwoo Han   }
323772e1dd1SChoongwoo Han   return Error::success();
324772e1dd1SChoongwoo Han }
325772e1dd1SChoongwoo Han 
326743574b8SVedant Kumar Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
327743574b8SVedant Kumar     ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
328743574b8SVedant Kumar     IndexedInstrProfReader &ProfileReader) {
329743574b8SVedant Kumar   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
330772e1dd1SChoongwoo Han   if (Error E = loadFromReaders(CoverageReaders, ProfileReader, *Coverage))
331c55cf4afSBill Wendling     return std::move(E);
332c55cf4afSBill Wendling   return std::move(Coverage);
333743574b8SVedant Kumar }
334743574b8SVedant Kumar 
335f025968bSJames Y Knight // If E is a no_data_found error, returns success. Otherwise returns E.
336f025968bSJames Y Knight static Error handleMaybeNoDataFoundError(Error E) {
337f025968bSJames Y Knight   return handleErrors(
338f025968bSJames Y Knight       std::move(E), [](const CoverageMapError &CME) {
339f025968bSJames Y Knight         if (CME.get() == coveragemap_error::no_data_found)
340f025968bSJames Y Knight           return static_cast<Error>(Error::success());
341f025968bSJames Y Knight         return make_error<CoverageMapError>(CME.get());
342f025968bSJames Y Knight       });
343f025968bSJames Y Knight }
344f025968bSJames Y Knight 
345743574b8SVedant Kumar Expected<std::unique_ptr<CoverageMapping>>
346743574b8SVedant Kumar CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames,
3478280ece0SPetr Hosek                       StringRef ProfileFilename, ArrayRef<StringRef> Arches,
3488280ece0SPetr Hosek                       StringRef CompilationDir) {
349dc707122SEaswaran Raman   auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
3509152fd17SVedant Kumar   if (Error E = ProfileReaderOrErr.takeError())
351c55cf4afSBill Wendling     return std::move(E);
352dc707122SEaswaran Raman   auto ProfileReader = std::move(ProfileReaderOrErr.get());
353772e1dd1SChoongwoo Han   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
354772e1dd1SChoongwoo Han   bool DataFound = false;
355743574b8SVedant Kumar 
3564b102c3dSVedant Kumar   for (const auto &File : llvm::enumerate(ObjectFilenames)) {
357772e1dd1SChoongwoo Han     auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(
358f5349922SAbhina Sreeskantharajan         File.value(), /*IsText=*/false, /*RequiresNullTerminator=*/false);
359743574b8SVedant Kumar     if (std::error_code EC = CovMappingBufOrErr.getError())
360743574b8SVedant Kumar       return errorCodeToError(EC);
3614b102c3dSVedant Kumar     StringRef Arch = Arches.empty() ? StringRef() : Arches[File.index()];
362901d04fcSVedant Kumar     MemoryBufferRef CovMappingBufRef =
363901d04fcSVedant Kumar         CovMappingBufOrErr.get()->getMemBufferRef();
364772e1dd1SChoongwoo Han     SmallVector<std::unique_ptr<MemoryBuffer>, 4> Buffers;
3658280ece0SPetr Hosek     auto CoverageReadersOrErr = BinaryCoverageReader::create(
3668280ece0SPetr Hosek         CovMappingBufRef, Arch, Buffers, CompilationDir);
367f025968bSJames Y Knight     if (Error E = CoverageReadersOrErr.takeError()) {
368f025968bSJames Y Knight       E = handleMaybeNoDataFoundError(std::move(E));
369f025968bSJames Y Knight       if (E)
370c55cf4afSBill Wendling         return std::move(E);
371f025968bSJames Y Knight       // E == success (originally a no_data_found error).
372f025968bSJames Y Knight       continue;
373f025968bSJames Y Knight     }
374772e1dd1SChoongwoo Han 
375772e1dd1SChoongwoo Han     SmallVector<std::unique_ptr<CoverageMappingReader>, 4> Readers;
376901d04fcSVedant Kumar     for (auto &Reader : CoverageReadersOrErr.get())
377901d04fcSVedant Kumar       Readers.push_back(std::move(Reader));
378772e1dd1SChoongwoo Han     DataFound |= !Readers.empty();
379772e1dd1SChoongwoo Han     if (Error E = loadFromReaders(Readers, *ProfileReader, *Coverage))
380772e1dd1SChoongwoo Han       return std::move(E);
381743574b8SVedant Kumar   }
382f025968bSJames Y Knight   // If no readers were created, either no objects were provided or none of them
383f025968bSJames Y Knight   // had coverage data. Return an error in the latter case.
384772e1dd1SChoongwoo Han   if (!DataFound && !ObjectFilenames.empty())
385f025968bSJames Y Knight     return make_error<CoverageMapError>(coveragemap_error::no_data_found);
386772e1dd1SChoongwoo Han   return std::move(Coverage);
387dc707122SEaswaran Raman }
388dc707122SEaswaran Raman 
389dc707122SEaswaran Raman namespace {
390e78d131aSEugene Zelenko 
3915f8f34e4SAdrian Prantl /// Distributes functions into instantiation sets.
392dc707122SEaswaran Raman ///
393dc707122SEaswaran Raman /// An instantiation set is a collection of functions that have the same source
394dc707122SEaswaran Raman /// code, ie, template functions specializations.
395dc707122SEaswaran Raman class FunctionInstantiationSetCollector {
3967bef6da6SVedant Kumar   using MapT = std::map<LineColPair, std::vector<const FunctionRecord *>>;
397dc707122SEaswaran Raman   MapT InstantiatedFunctions;
398dc707122SEaswaran Raman 
399dc707122SEaswaran Raman public:
400dc707122SEaswaran Raman   void insert(const FunctionRecord &Function, unsigned FileID) {
401dc707122SEaswaran Raman     auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
402dc707122SEaswaran Raman     while (I != E && I->FileID != FileID)
403dc707122SEaswaran Raman       ++I;
404dc707122SEaswaran Raman     assert(I != E && "function does not cover the given file");
405dc707122SEaswaran Raman     auto &Functions = InstantiatedFunctions[I->startLoc()];
406dc707122SEaswaran Raman     Functions.push_back(&Function);
407dc707122SEaswaran Raman   }
408dc707122SEaswaran Raman 
409dc707122SEaswaran Raman   MapT::iterator begin() { return InstantiatedFunctions.begin(); }
410dc707122SEaswaran Raman   MapT::iterator end() { return InstantiatedFunctions.end(); }
411dc707122SEaswaran Raman };
412dc707122SEaswaran Raman 
413dc707122SEaswaran Raman class SegmentBuilder {
414dc707122SEaswaran Raman   std::vector<CoverageSegment> &Segments;
415dc707122SEaswaran Raman   SmallVector<const CountedRegion *, 8> ActiveRegions;
416dc707122SEaswaran Raman 
417dc707122SEaswaran Raman   SegmentBuilder(std::vector<CoverageSegment> &Segments) : Segments(Segments) {}
418dc707122SEaswaran Raman 
41979a1b5eeSVedant Kumar   /// Emit a segment with the count from \p Region starting at \p StartLoc.
42079a1b5eeSVedant Kumar   //
421ad8f637bSVedant Kumar   /// \p IsRegionEntry: The segment is at the start of a new non-gap region.
42279a1b5eeSVedant Kumar   /// \p EmitSkippedRegion: The segment must be emitted as a skipped region.
42379a1b5eeSVedant Kumar   void startSegment(const CountedRegion &Region, LineColPair StartLoc,
42479a1b5eeSVedant Kumar                     bool IsRegionEntry, bool EmitSkippedRegion = false) {
42579a1b5eeSVedant Kumar     bool HasCount = !EmitSkippedRegion &&
42679a1b5eeSVedant Kumar                     (Region.Kind != CounterMappingRegion::SkippedRegion);
42779a1b5eeSVedant Kumar 
42879a1b5eeSVedant Kumar     // If the new segment wouldn't affect coverage rendering, skip it.
42979a1b5eeSVedant Kumar     if (!Segments.empty() && !IsRegionEntry && !EmitSkippedRegion) {
43079a1b5eeSVedant Kumar       const auto &Last = Segments.back();
43179a1b5eeSVedant Kumar       if (Last.HasCount == HasCount && Last.Count == Region.ExecutionCount &&
43279a1b5eeSVedant Kumar           !Last.IsRegionEntry)
43379a1b5eeSVedant Kumar         return;
434dc707122SEaswaran Raman     }
435dc707122SEaswaran Raman 
43679a1b5eeSVedant Kumar     if (HasCount)
43779a1b5eeSVedant Kumar       Segments.emplace_back(StartLoc.first, StartLoc.second,
438ad8f637bSVedant Kumar                             Region.ExecutionCount, IsRegionEntry,
439ad8f637bSVedant Kumar                             Region.Kind == CounterMappingRegion::GapRegion);
440dc707122SEaswaran Raman     else
44179a1b5eeSVedant Kumar       Segments.emplace_back(StartLoc.first, StartLoc.second, IsRegionEntry);
44279a1b5eeSVedant Kumar 
443d34e60caSNicola Zaghen     LLVM_DEBUG({
44479a1b5eeSVedant Kumar       const auto &Last = Segments.back();
44579a1b5eeSVedant Kumar       dbgs() << "Segment at " << Last.Line << ":" << Last.Col
44679a1b5eeSVedant Kumar              << " (count = " << Last.Count << ")"
44779a1b5eeSVedant Kumar              << (Last.IsRegionEntry ? ", RegionEntry" : "")
448ad8f637bSVedant Kumar              << (!Last.HasCount ? ", Skipped" : "")
449ad8f637bSVedant Kumar              << (Last.IsGapRegion ? ", Gap" : "") << "\n";
45079a1b5eeSVedant Kumar     });
45179a1b5eeSVedant Kumar   }
45279a1b5eeSVedant Kumar 
45379a1b5eeSVedant Kumar   /// Emit segments for active regions which end before \p Loc.
45479a1b5eeSVedant Kumar   ///
45579a1b5eeSVedant Kumar   /// \p Loc: The start location of the next region. If None, all active
45679a1b5eeSVedant Kumar   /// regions are completed.
45779a1b5eeSVedant Kumar   /// \p FirstCompletedRegion: Index of the first completed region.
45879a1b5eeSVedant Kumar   void completeRegionsUntil(Optional<LineColPair> Loc,
45979a1b5eeSVedant Kumar                             unsigned FirstCompletedRegion) {
46079a1b5eeSVedant Kumar     // Sort the completed regions by end location. This makes it simple to
46179a1b5eeSVedant Kumar     // emit closing segments in sorted order.
46279a1b5eeSVedant Kumar     auto CompletedRegionsIt = ActiveRegions.begin() + FirstCompletedRegion;
46379a1b5eeSVedant Kumar     std::stable_sort(CompletedRegionsIt, ActiveRegions.end(),
46479a1b5eeSVedant Kumar                       [](const CountedRegion *L, const CountedRegion *R) {
46579a1b5eeSVedant Kumar                         return L->endLoc() < R->endLoc();
46679a1b5eeSVedant Kumar                       });
46779a1b5eeSVedant Kumar 
46879a1b5eeSVedant Kumar     // Emit segments for all completed regions.
46979a1b5eeSVedant Kumar     for (unsigned I = FirstCompletedRegion + 1, E = ActiveRegions.size(); I < E;
47079a1b5eeSVedant Kumar          ++I) {
47179a1b5eeSVedant Kumar       const auto *CompletedRegion = ActiveRegions[I];
47279a1b5eeSVedant Kumar       assert((!Loc || CompletedRegion->endLoc() <= *Loc) &&
47379a1b5eeSVedant Kumar              "Completed region ends after start of new region");
47479a1b5eeSVedant Kumar 
47579a1b5eeSVedant Kumar       const auto *PrevCompletedRegion = ActiveRegions[I - 1];
47679a1b5eeSVedant Kumar       auto CompletedSegmentLoc = PrevCompletedRegion->endLoc();
47779a1b5eeSVedant Kumar 
47879a1b5eeSVedant Kumar       // Don't emit any more segments if they start where the new region begins.
47979a1b5eeSVedant Kumar       if (Loc && CompletedSegmentLoc == *Loc)
48079a1b5eeSVedant Kumar         break;
48179a1b5eeSVedant Kumar 
48279a1b5eeSVedant Kumar       // Don't emit a segment if the next completed region ends at the same
48379a1b5eeSVedant Kumar       // location as this one.
48479a1b5eeSVedant Kumar       if (CompletedSegmentLoc == CompletedRegion->endLoc())
48579a1b5eeSVedant Kumar         continue;
48679a1b5eeSVedant Kumar 
487337b0db1SVedant Kumar       // Use the count from the last completed region which ends at this loc.
488337b0db1SVedant Kumar       for (unsigned J = I + 1; J < E; ++J)
489337b0db1SVedant Kumar         if (CompletedRegion->endLoc() == ActiveRegions[J]->endLoc())
490337b0db1SVedant Kumar           CompletedRegion = ActiveRegions[J];
49180fbb855SVedant Kumar 
49279a1b5eeSVedant Kumar       startSegment(*CompletedRegion, CompletedSegmentLoc, false);
49379a1b5eeSVedant Kumar     }
49479a1b5eeSVedant Kumar 
49579a1b5eeSVedant Kumar     auto Last = ActiveRegions.back();
49679a1b5eeSVedant Kumar     if (FirstCompletedRegion && Last->endLoc() != *Loc) {
49779a1b5eeSVedant Kumar       // If there's a gap after the end of the last completed region and the
49879a1b5eeSVedant Kumar       // start of the new region, use the last active region to fill the gap.
49979a1b5eeSVedant Kumar       startSegment(*ActiveRegions[FirstCompletedRegion - 1], Last->endLoc(),
50079a1b5eeSVedant Kumar                    false);
50179a1b5eeSVedant Kumar     } else if (!FirstCompletedRegion && (!Loc || *Loc != Last->endLoc())) {
50279a1b5eeSVedant Kumar       // Emit a skipped segment if there are no more active regions. This
50379a1b5eeSVedant Kumar       // ensures that gaps between functions are marked correctly.
50479a1b5eeSVedant Kumar       startSegment(*Last, Last->endLoc(), false, true);
50579a1b5eeSVedant Kumar     }
50679a1b5eeSVedant Kumar 
50779a1b5eeSVedant Kumar     // Pop the completed regions.
50879a1b5eeSVedant Kumar     ActiveRegions.erase(CompletedRegionsIt, ActiveRegions.end());
509dc707122SEaswaran Raman   }
510dc707122SEaswaran Raman 
511dc707122SEaswaran Raman   void buildSegmentsImpl(ArrayRef<CountedRegion> Regions) {
51279a1b5eeSVedant Kumar     for (const auto &CR : enumerate(Regions)) {
51379a1b5eeSVedant Kumar       auto CurStartLoc = CR.value().startLoc();
51479a1b5eeSVedant Kumar 
51579a1b5eeSVedant Kumar       // Active regions which end before the current region need to be popped.
51679a1b5eeSVedant Kumar       auto CompletedRegions =
51779a1b5eeSVedant Kumar           std::stable_partition(ActiveRegions.begin(), ActiveRegions.end(),
51879a1b5eeSVedant Kumar                                 [&](const CountedRegion *Region) {
51979a1b5eeSVedant Kumar                                   return !(Region->endLoc() <= CurStartLoc);
52079a1b5eeSVedant Kumar                                 });
52179a1b5eeSVedant Kumar       if (CompletedRegions != ActiveRegions.end()) {
52279a1b5eeSVedant Kumar         unsigned FirstCompletedRegion =
52379a1b5eeSVedant Kumar             std::distance(ActiveRegions.begin(), CompletedRegions);
52479a1b5eeSVedant Kumar         completeRegionsUntil(CurStartLoc, FirstCompletedRegion);
525dc707122SEaswaran Raman       }
52679a1b5eeSVedant Kumar 
527ad8f637bSVedant Kumar       bool GapRegion = CR.value().Kind == CounterMappingRegion::GapRegion;
528ad8f637bSVedant Kumar 
52979a1b5eeSVedant Kumar       // Try to emit a segment for the current region.
53079a1b5eeSVedant Kumar       if (CurStartLoc == CR.value().endLoc()) {
53179a1b5eeSVedant Kumar         // Avoid making zero-length regions active. If it's the last region,
53279a1b5eeSVedant Kumar         // emit a skipped segment. Otherwise use its predecessor's count.
5339caa3fbeSZequan Wu         const bool Skipped =
5349caa3fbeSZequan Wu             (CR.index() + 1) == Regions.size() ||
5359caa3fbeSZequan Wu             CR.value().Kind == CounterMappingRegion::SkippedRegion;
53679a1b5eeSVedant Kumar         startSegment(ActiveRegions.empty() ? CR.value() : *ActiveRegions.back(),
537ad8f637bSVedant Kumar                      CurStartLoc, !GapRegion, Skipped);
5389caa3fbeSZequan Wu         // If it is skipped segment, create a segment with last pushed
5399caa3fbeSZequan Wu         // regions's count at CurStartLoc.
5409caa3fbeSZequan Wu         if (Skipped && !ActiveRegions.empty())
5419caa3fbeSZequan Wu           startSegment(*ActiveRegions.back(), CurStartLoc, false);
54279a1b5eeSVedant Kumar         continue;
54379a1b5eeSVedant Kumar       }
54479a1b5eeSVedant Kumar       if (CR.index() + 1 == Regions.size() ||
54579a1b5eeSVedant Kumar           CurStartLoc != Regions[CR.index() + 1].startLoc()) {
54679a1b5eeSVedant Kumar         // Emit a segment if the next region doesn't start at the same location
54779a1b5eeSVedant Kumar         // as this one.
548ad8f637bSVedant Kumar         startSegment(CR.value(), CurStartLoc, !GapRegion);
54979a1b5eeSVedant Kumar       }
55079a1b5eeSVedant Kumar 
55179a1b5eeSVedant Kumar       // This region is active (i.e not completed).
55279a1b5eeSVedant Kumar       ActiveRegions.push_back(&CR.value());
55379a1b5eeSVedant Kumar     }
55479a1b5eeSVedant Kumar 
55579a1b5eeSVedant Kumar     // Complete any remaining active regions.
55679a1b5eeSVedant Kumar     if (!ActiveRegions.empty())
55779a1b5eeSVedant Kumar       completeRegionsUntil(None, 0);
558dc707122SEaswaran Raman   }
559dc707122SEaswaran Raman 
560dc707122SEaswaran Raman   /// Sort a nested sequence of regions from a single file.
561dc707122SEaswaran Raman   static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) {
5620cac726aSFangrui Song     llvm::sort(Regions, [](const CountedRegion &LHS, const CountedRegion &RHS) {
56327d8dd39SIgor Kudrin       if (LHS.startLoc() != RHS.startLoc())
56427d8dd39SIgor Kudrin         return LHS.startLoc() < RHS.startLoc();
56527d8dd39SIgor Kudrin       if (LHS.endLoc() != RHS.endLoc())
566dc707122SEaswaran Raman         // When LHS completely contains RHS, we sort LHS first.
567dc707122SEaswaran Raman         return RHS.endLoc() < LHS.endLoc();
56827d8dd39SIgor Kudrin       // If LHS and RHS cover the same area, we need to sort them according
56927d8dd39SIgor Kudrin       // to their kinds so that the most suitable region will become "active"
57027d8dd39SIgor Kudrin       // in combineRegions(). Because we accumulate counter values only from
57127d8dd39SIgor Kudrin       // regions of the same kind as the first region of the area, prefer
57227d8dd39SIgor Kudrin       // CodeRegion to ExpansionRegion and ExpansionRegion to SkippedRegion.
573e78d131aSEugene Zelenko       static_assert(CounterMappingRegion::CodeRegion <
574e78d131aSEugene Zelenko                             CounterMappingRegion::ExpansionRegion &&
575e78d131aSEugene Zelenko                         CounterMappingRegion::ExpansionRegion <
576e78d131aSEugene Zelenko                             CounterMappingRegion::SkippedRegion,
57727d8dd39SIgor Kudrin                     "Unexpected order of region kind values");
57827d8dd39SIgor Kudrin       return LHS.Kind < RHS.Kind;
579dc707122SEaswaran Raman     });
580dc707122SEaswaran Raman   }
581dc707122SEaswaran Raman 
582dc707122SEaswaran Raman   /// Combine counts of regions which cover the same area.
583dc707122SEaswaran Raman   static ArrayRef<CountedRegion>
584dc707122SEaswaran Raman   combineRegions(MutableArrayRef<CountedRegion> Regions) {
585dc707122SEaswaran Raman     if (Regions.empty())
586dc707122SEaswaran Raman       return Regions;
587dc707122SEaswaran Raman     auto Active = Regions.begin();
588dc707122SEaswaran Raman     auto End = Regions.end();
589dc707122SEaswaran Raman     for (auto I = Regions.begin() + 1; I != End; ++I) {
590dc707122SEaswaran Raman       if (Active->startLoc() != I->startLoc() ||
591dc707122SEaswaran Raman           Active->endLoc() != I->endLoc()) {
592dc707122SEaswaran Raman         // Shift to the next region.
593dc707122SEaswaran Raman         ++Active;
594dc707122SEaswaran Raman         if (Active != I)
595dc707122SEaswaran Raman           *Active = *I;
596dc707122SEaswaran Raman         continue;
597dc707122SEaswaran Raman       }
598dc707122SEaswaran Raman       // Merge duplicate region.
59927d8dd39SIgor Kudrin       // If CodeRegions and ExpansionRegions cover the same area, it's probably
60027d8dd39SIgor Kudrin       // a macro which is fully expanded to another macro. In that case, we need
60127d8dd39SIgor Kudrin       // to accumulate counts only from CodeRegions, or else the area will be
60227d8dd39SIgor Kudrin       // counted twice.
60327d8dd39SIgor Kudrin       // On the other hand, a macro may have a nested macro in its body. If the
60427d8dd39SIgor Kudrin       // outer macro is used several times, the ExpansionRegion for the nested
60527d8dd39SIgor Kudrin       // macro will also be added several times. These ExpansionRegions cover
60627d8dd39SIgor Kudrin       // the same source locations and have to be combined to reach the correct
60727d8dd39SIgor Kudrin       // value for that area.
60827d8dd39SIgor Kudrin       // We add counts of the regions of the same kind as the active region
60927d8dd39SIgor Kudrin       // to handle the both situations.
61027d8dd39SIgor Kudrin       if (I->Kind == Active->Kind)
611dc707122SEaswaran Raman         Active->ExecutionCount += I->ExecutionCount;
612dc707122SEaswaran Raman     }
613dc707122SEaswaran Raman     return Regions.drop_back(std::distance(++Active, End));
614dc707122SEaswaran Raman   }
615dc707122SEaswaran Raman 
616dc707122SEaswaran Raman public:
61779a1b5eeSVedant Kumar   /// Build a sorted list of CoverageSegments from a list of Regions.
618dc707122SEaswaran Raman   static std::vector<CoverageSegment>
619dc707122SEaswaran Raman   buildSegments(MutableArrayRef<CountedRegion> Regions) {
620dc707122SEaswaran Raman     std::vector<CoverageSegment> Segments;
621dc707122SEaswaran Raman     SegmentBuilder Builder(Segments);
622dc707122SEaswaran Raman 
623dc707122SEaswaran Raman     sortNestedRegions(Regions);
624dc707122SEaswaran Raman     ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
625dc707122SEaswaran Raman 
626d34e60caSNicola Zaghen     LLVM_DEBUG({
62779a1b5eeSVedant Kumar       dbgs() << "Combined regions:\n";
62879a1b5eeSVedant Kumar       for (const auto &CR : CombinedRegions)
62979a1b5eeSVedant Kumar         dbgs() << "  " << CR.LineStart << ":" << CR.ColumnStart << " -> "
63079a1b5eeSVedant Kumar                << CR.LineEnd << ":" << CR.ColumnEnd
63179a1b5eeSVedant Kumar                << " (count=" << CR.ExecutionCount << ")\n";
63279a1b5eeSVedant Kumar     });
63379a1b5eeSVedant Kumar 
634dc707122SEaswaran Raman     Builder.buildSegmentsImpl(CombinedRegions);
63579a1b5eeSVedant Kumar 
63679a1b5eeSVedant Kumar #ifndef NDEBUG
63779a1b5eeSVedant Kumar     for (unsigned I = 1, E = Segments.size(); I < E; ++I) {
63879a1b5eeSVedant Kumar       const auto &L = Segments[I - 1];
63979a1b5eeSVedant Kumar       const auto &R = Segments[I];
64079a1b5eeSVedant Kumar       if (!(L.Line < R.Line) && !(L.Line == R.Line && L.Col < R.Col)) {
6419caa3fbeSZequan Wu         if (L.Line == R.Line && L.Col == R.Col && !L.HasCount)
6429caa3fbeSZequan Wu           continue;
643d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << " ! Segment " << L.Line << ":" << L.Col
64479a1b5eeSVedant Kumar                           << " followed by " << R.Line << ":" << R.Col << "\n");
64579a1b5eeSVedant Kumar         assert(false && "Coverage segments not unique or sorted");
64679a1b5eeSVedant Kumar       }
64779a1b5eeSVedant Kumar     }
64879a1b5eeSVedant Kumar #endif
64979a1b5eeSVedant Kumar 
650dc707122SEaswaran Raman     return Segments;
651dc707122SEaswaran Raman   }
652dc707122SEaswaran Raman };
653e78d131aSEugene Zelenko 
654e78d131aSEugene Zelenko } // end anonymous namespace
655dc707122SEaswaran Raman 
656dc707122SEaswaran Raman std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
657dc707122SEaswaran Raman   std::vector<StringRef> Filenames;
658dc707122SEaswaran Raman   for (const auto &Function : getCoveredFunctions())
6591d0bc055SKazu Hirata     llvm::append_range(Filenames, Function.Filenames);
6600cac726aSFangrui Song   llvm::sort(Filenames);
661dc707122SEaswaran Raman   auto Last = std::unique(Filenames.begin(), Filenames.end());
662dc707122SEaswaran Raman   Filenames.erase(Last, Filenames.end());
663dc707122SEaswaran Raman   return Filenames;
664dc707122SEaswaran Raman }
665dc707122SEaswaran Raman 
666dc707122SEaswaran Raman static SmallBitVector gatherFileIDs(StringRef SourceFile,
667dc707122SEaswaran Raman                                     const FunctionRecord &Function) {
668dc707122SEaswaran Raman   SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
669dc707122SEaswaran Raman   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
670dc707122SEaswaran Raman     if (SourceFile == Function.Filenames[I])
671dc707122SEaswaran Raman       FilenameEquivalence[I] = true;
672dc707122SEaswaran Raman   return FilenameEquivalence;
673dc707122SEaswaran Raman }
674dc707122SEaswaran Raman 
675dc707122SEaswaran Raman /// Return the ID of the file where the definition of the function is located.
676dc707122SEaswaran Raman static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
677dc707122SEaswaran Raman   SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
678dc707122SEaswaran Raman   for (const auto &CR : Function.CountedRegions)
679dc707122SEaswaran Raman     if (CR.Kind == CounterMappingRegion::ExpansionRegion)
680dc707122SEaswaran Raman       IsNotExpandedFile[CR.ExpandedFileID] = false;
681dc707122SEaswaran Raman   int I = IsNotExpandedFile.find_first();
682dc707122SEaswaran Raman   if (I == -1)
683dc707122SEaswaran Raman     return None;
684dc707122SEaswaran Raman   return I;
685dc707122SEaswaran Raman }
686dc707122SEaswaran Raman 
687dc707122SEaswaran Raman /// Check if SourceFile is the file that contains the definition of
688dc707122SEaswaran Raman /// the Function. Return the ID of the file in that case or None otherwise.
689dc707122SEaswaran Raman static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
690dc707122SEaswaran Raman                                              const FunctionRecord &Function) {
691dc707122SEaswaran Raman   Optional<unsigned> I = findMainViewFileID(Function);
692dc707122SEaswaran Raman   if (I && SourceFile == Function.Filenames[*I])
693dc707122SEaswaran Raman     return I;
694dc707122SEaswaran Raman   return None;
695dc707122SEaswaran Raman }
696dc707122SEaswaran Raman 
697dc707122SEaswaran Raman static bool isExpansion(const CountedRegion &R, unsigned FileID) {
698dc707122SEaswaran Raman   return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
699dc707122SEaswaran Raman }
700dc707122SEaswaran Raman 
7017fcc5472SVedant Kumar CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
702dc707122SEaswaran Raman   CoverageData FileCoverage(Filename);
703e78d131aSEugene Zelenko   std::vector<CountedRegion> Regions;
704dc707122SEaswaran Raman 
705413647d7SVedant Kumar   // Look up the function records in the given file. Due to hash collisions on
706413647d7SVedant Kumar   // the filename, we may get back some records that are not in the file.
707413647d7SVedant Kumar   ArrayRef<unsigned> RecordIndices =
708413647d7SVedant Kumar       getImpreciseRecordIndicesForFilename(Filename);
709413647d7SVedant Kumar   for (unsigned RecordIndex : RecordIndices) {
710413647d7SVedant Kumar     const FunctionRecord &Function = Functions[RecordIndex];
711dc707122SEaswaran Raman     auto MainFileID = findMainViewFileID(Filename, Function);
712dc707122SEaswaran Raman     auto FileIDs = gatherFileIDs(Filename, Function);
713dc707122SEaswaran Raman     for (const auto &CR : Function.CountedRegions)
714dc707122SEaswaran Raman       if (FileIDs.test(CR.FileID)) {
715dc707122SEaswaran Raman         Regions.push_back(CR);
716dc707122SEaswaran Raman         if (MainFileID && isExpansion(CR, *MainFileID))
717dc707122SEaswaran Raman           FileCoverage.Expansions.emplace_back(CR, Function);
718dc707122SEaswaran Raman       }
7199f2967bcSAlan Phipps     // Capture branch regions specific to the function (excluding expansions).
7209f2967bcSAlan Phipps     for (const auto &CR : Function.CountedBranchRegions)
7219f2967bcSAlan Phipps       if (FileIDs.test(CR.FileID) && (CR.FileID == CR.ExpandedFileID))
7229f2967bcSAlan Phipps         FileCoverage.BranchRegions.push_back(CR);
723dc707122SEaswaran Raman   }
724dc707122SEaswaran Raman 
725d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
726dc707122SEaswaran Raman   FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
727dc707122SEaswaran Raman 
728dc707122SEaswaran Raman   return FileCoverage;
729dc707122SEaswaran Raman }
730dc707122SEaswaran Raman 
731dde19c5aSVedant Kumar std::vector<InstantiationGroup>
732dde19c5aSVedant Kumar CoverageMapping::getInstantiationGroups(StringRef Filename) const {
733dc707122SEaswaran Raman   FunctionInstantiationSetCollector InstantiationSetCollector;
734413647d7SVedant Kumar   // Look up the function records in the given file. Due to hash collisions on
735413647d7SVedant Kumar   // the filename, we may get back some records that are not in the file.
736413647d7SVedant Kumar   ArrayRef<unsigned> RecordIndices =
737413647d7SVedant Kumar       getImpreciseRecordIndicesForFilename(Filename);
738413647d7SVedant Kumar   for (unsigned RecordIndex : RecordIndices) {
739413647d7SVedant Kumar     const FunctionRecord &Function = Functions[RecordIndex];
740dc707122SEaswaran Raman     auto MainFileID = findMainViewFileID(Filename, Function);
741dc707122SEaswaran Raman     if (!MainFileID)
742dc707122SEaswaran Raman       continue;
743dc707122SEaswaran Raman     InstantiationSetCollector.insert(Function, *MainFileID);
744dc707122SEaswaran Raman   }
745dc707122SEaswaran Raman 
746dde19c5aSVedant Kumar   std::vector<InstantiationGroup> Result;
74724cb28bbSBenjamin Kramer   for (auto &InstantiationSet : InstantiationSetCollector) {
748dde19c5aSVedant Kumar     InstantiationGroup IG{InstantiationSet.first.first,
749dde19c5aSVedant Kumar                           InstantiationSet.first.second,
750dde19c5aSVedant Kumar                           std::move(InstantiationSet.second)};
751dde19c5aSVedant Kumar     Result.emplace_back(std::move(IG));
752dc707122SEaswaran Raman   }
753dc707122SEaswaran Raman   return Result;
754dc707122SEaswaran Raman }
755dc707122SEaswaran Raman 
756dc707122SEaswaran Raman CoverageData
757f681e2e5SVedant Kumar CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const {
758dc707122SEaswaran Raman   auto MainFileID = findMainViewFileID(Function);
759dc707122SEaswaran Raman   if (!MainFileID)
760dc707122SEaswaran Raman     return CoverageData();
761dc707122SEaswaran Raman 
762dc707122SEaswaran Raman   CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
763e78d131aSEugene Zelenko   std::vector<CountedRegion> Regions;
764dc707122SEaswaran Raman   for (const auto &CR : Function.CountedRegions)
765dc707122SEaswaran Raman     if (CR.FileID == *MainFileID) {
766dc707122SEaswaran Raman       Regions.push_back(CR);
767dc707122SEaswaran Raman       if (isExpansion(CR, *MainFileID))
768dc707122SEaswaran Raman         FunctionCoverage.Expansions.emplace_back(CR, Function);
769dc707122SEaswaran Raman     }
7709f2967bcSAlan Phipps   // Capture branch regions specific to the function (excluding expansions).
7719f2967bcSAlan Phipps   for (const auto &CR : Function.CountedBranchRegions)
7729f2967bcSAlan Phipps     if (CR.FileID == *MainFileID)
7739f2967bcSAlan Phipps       FunctionCoverage.BranchRegions.push_back(CR);
774dc707122SEaswaran Raman 
775d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name
776d34e60caSNicola Zaghen                     << "\n");
777dc707122SEaswaran Raman   FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
778dc707122SEaswaran Raman 
779dc707122SEaswaran Raman   return FunctionCoverage;
780dc707122SEaswaran Raman }
781dc707122SEaswaran Raman 
782f681e2e5SVedant Kumar CoverageData CoverageMapping::getCoverageForExpansion(
783f681e2e5SVedant Kumar     const ExpansionRecord &Expansion) const {
784dc707122SEaswaran Raman   CoverageData ExpansionCoverage(
785dc707122SEaswaran Raman       Expansion.Function.Filenames[Expansion.FileID]);
786e78d131aSEugene Zelenko   std::vector<CountedRegion> Regions;
787dc707122SEaswaran Raman   for (const auto &CR : Expansion.Function.CountedRegions)
788dc707122SEaswaran Raman     if (CR.FileID == Expansion.FileID) {
789dc707122SEaswaran Raman       Regions.push_back(CR);
790dc707122SEaswaran Raman       if (isExpansion(CR, Expansion.FileID))
791dc707122SEaswaran Raman         ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
792dc707122SEaswaran Raman     }
7939f2967bcSAlan Phipps   for (const auto &CR : Expansion.Function.CountedBranchRegions)
7949f2967bcSAlan Phipps     // Capture branch regions that only pertain to the corresponding expansion.
7959f2967bcSAlan Phipps     if (CR.FileID == Expansion.FileID)
7969f2967bcSAlan Phipps       ExpansionCoverage.BranchRegions.push_back(CR);
797dc707122SEaswaran Raman 
798d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Emitting segments for expansion of file "
799d34e60caSNicola Zaghen                     << Expansion.FileID << "\n");
800dc707122SEaswaran Raman   ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
801dc707122SEaswaran Raman 
802dc707122SEaswaran Raman   return ExpansionCoverage;
803dc707122SEaswaran Raman }
804dc707122SEaswaran Raman 
805821160d5SVedant Kumar LineCoverageStats::LineCoverageStats(
806f5f153ddSVedant Kumar     ArrayRef<const CoverageSegment *> LineSegments,
807f5f153ddSVedant Kumar     const CoverageSegment *WrappedSegment, unsigned Line)
808821160d5SVedant Kumar     : ExecutionCount(0), HasMultipleRegions(false), Mapped(false), Line(Line),
809821160d5SVedant Kumar       LineSegments(LineSegments), WrappedSegment(WrappedSegment) {
810821160d5SVedant Kumar   // Find the minimum number of regions which start in this line.
811821160d5SVedant Kumar   unsigned MinRegionCount = 0;
812f5f153ddSVedant Kumar   auto isStartOfRegion = [](const CoverageSegment *S) {
813821160d5SVedant Kumar     return !S->IsGapRegion && S->HasCount && S->IsRegionEntry;
814821160d5SVedant Kumar   };
815821160d5SVedant Kumar   for (unsigned I = 0; I < LineSegments.size() && MinRegionCount < 2; ++I)
816821160d5SVedant Kumar     if (isStartOfRegion(LineSegments[I]))
817821160d5SVedant Kumar       ++MinRegionCount;
818821160d5SVedant Kumar 
819821160d5SVedant Kumar   bool StartOfSkippedRegion = !LineSegments.empty() &&
820821160d5SVedant Kumar                               !LineSegments.front()->HasCount &&
821821160d5SVedant Kumar                               LineSegments.front()->IsRegionEntry;
822821160d5SVedant Kumar 
823821160d5SVedant Kumar   HasMultipleRegions = MinRegionCount > 1;
824821160d5SVedant Kumar   Mapped =
825821160d5SVedant Kumar       !StartOfSkippedRegion &&
826821160d5SVedant Kumar       ((WrappedSegment && WrappedSegment->HasCount) || (MinRegionCount > 0));
827821160d5SVedant Kumar 
828821160d5SVedant Kumar   if (!Mapped)
829821160d5SVedant Kumar     return;
830821160d5SVedant Kumar 
83143247f05SVedant Kumar   // Pick the max count from the non-gap, region entry segments and the
83243247f05SVedant Kumar   // wrapped count.
83343247f05SVedant Kumar   if (WrappedSegment)
834821160d5SVedant Kumar     ExecutionCount = WrappedSegment->Count;
83543247f05SVedant Kumar   if (!MinRegionCount)
836821160d5SVedant Kumar     return;
837821160d5SVedant Kumar   for (const auto *LS : LineSegments)
838821160d5SVedant Kumar     if (isStartOfRegion(LS))
839821160d5SVedant Kumar       ExecutionCount = std::max(ExecutionCount, LS->Count);
840821160d5SVedant Kumar }
841821160d5SVedant Kumar 
842821160d5SVedant Kumar LineCoverageIterator &LineCoverageIterator::operator++() {
843821160d5SVedant Kumar   if (Next == CD.end()) {
844821160d5SVedant Kumar     Stats = LineCoverageStats();
845821160d5SVedant Kumar     Ended = true;
846821160d5SVedant Kumar     return *this;
847821160d5SVedant Kumar   }
848821160d5SVedant Kumar   if (Segments.size())
849821160d5SVedant Kumar     WrappedSegment = Segments.back();
850821160d5SVedant Kumar   Segments.clear();
851821160d5SVedant Kumar   while (Next != CD.end() && Next->Line == Line)
852821160d5SVedant Kumar     Segments.push_back(&*Next++);
853821160d5SVedant Kumar   Stats = LineCoverageStats(Segments, WrappedSegment, Line);
854821160d5SVedant Kumar   ++Line;
855821160d5SVedant Kumar   return *this;
856821160d5SVedant Kumar }
857821160d5SVedant Kumar 
858e78d131aSEugene Zelenko static std::string getCoverageMapErrString(coveragemap_error Err) {
8599152fd17SVedant Kumar   switch (Err) {
860dc707122SEaswaran Raman   case coveragemap_error::success:
861dc707122SEaswaran Raman     return "Success";
862dc707122SEaswaran Raman   case coveragemap_error::eof:
863dc707122SEaswaran Raman     return "End of File";
864dc707122SEaswaran Raman   case coveragemap_error::no_data_found:
865dc707122SEaswaran Raman     return "No coverage data found";
866dc707122SEaswaran Raman   case coveragemap_error::unsupported_version:
867dc707122SEaswaran Raman     return "Unsupported coverage format version";
868dc707122SEaswaran Raman   case coveragemap_error::truncated:
869dc707122SEaswaran Raman     return "Truncated coverage data";
870dc707122SEaswaran Raman   case coveragemap_error::malformed:
871dc707122SEaswaran Raman     return "Malformed coverage data";
872dd1ea9deSVedant Kumar   case coveragemap_error::decompression_failed:
873dd1ea9deSVedant Kumar     return "Failed to decompress coverage data (zlib)";
8747fafaa07SVedant Kumar   case coveragemap_error::invalid_or_missing_arch_specifier:
8757fafaa07SVedant Kumar     return "`-arch` specifier is invalid or missing for universal binary";
876dc707122SEaswaran Raman   }
877dc707122SEaswaran Raman   llvm_unreachable("A value of coveragemap_error has no message.");
878dc707122SEaswaran Raman }
8799152fd17SVedant Kumar 
880e78d131aSEugene Zelenko namespace {
881e78d131aSEugene Zelenko 
8824718f8b5SPeter Collingbourne // FIXME: This class is only here to support the transition to llvm::Error. It
8834718f8b5SPeter Collingbourne // will be removed once this transition is complete. Clients should prefer to
8844718f8b5SPeter Collingbourne // deal with the Error value directly, rather than converting to error_code.
8859152fd17SVedant Kumar class CoverageMappingErrorCategoryType : public std::error_category {
886990504e6SReid Kleckner   const char *name() const noexcept override { return "llvm.coveragemap"; }
8879152fd17SVedant Kumar   std::string message(int IE) const override {
8889152fd17SVedant Kumar     return getCoverageMapErrString(static_cast<coveragemap_error>(IE));
8899152fd17SVedant Kumar   }
890dc707122SEaswaran Raman };
891e78d131aSEugene Zelenko 
8929152fd17SVedant Kumar } // end anonymous namespace
8939152fd17SVedant Kumar 
8949152fd17SVedant Kumar std::string CoverageMapError::message() const {
8959152fd17SVedant Kumar   return getCoverageMapErrString(Err);
896dc707122SEaswaran Raman }
897dc707122SEaswaran Raman 
898dc707122SEaswaran Raman static ManagedStatic<CoverageMappingErrorCategoryType> ErrorCategory;
899dc707122SEaswaran Raman 
900dc707122SEaswaran Raman const std::error_category &llvm::coverage::coveragemap_category() {
901dc707122SEaswaran Raman   return *ErrorCategory;
902dc707122SEaswaran Raman }
9039152fd17SVedant Kumar 
9049152fd17SVedant Kumar char CoverageMapError::ID = 0;
905