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"
28e78d131aSEugene Zelenko #include "llvm/Support/MemoryBuffer.h"
29dc707122SEaswaran Raman #include "llvm/Support/raw_ostream.h"
30e78d131aSEugene Zelenko #include <algorithm>
31e78d131aSEugene Zelenko #include <cassert>
32e78d131aSEugene Zelenko #include <cstdint>
33e78d131aSEugene Zelenko #include <iterator>
347bef6da6SVedant Kumar #include <map>
35e78d131aSEugene Zelenko #include <memory>
36e78d131aSEugene Zelenko #include <string>
37e78d131aSEugene Zelenko #include <system_error>
38e78d131aSEugene Zelenko #include <utility>
39e78d131aSEugene Zelenko #include <vector>
40dc707122SEaswaran Raman 
41dc707122SEaswaran Raman using namespace llvm;
42dc707122SEaswaran Raman using namespace coverage;
43dc707122SEaswaran Raman 
44dc707122SEaswaran Raman #define DEBUG_TYPE "coverage-mapping"
45dc707122SEaswaran Raman 
get(const CounterExpression & E)46dc707122SEaswaran Raman Counter CounterExpressionBuilder::get(const CounterExpression &E) {
47dc707122SEaswaran Raman   auto It = ExpressionIndices.find(E);
48dc707122SEaswaran Raman   if (It != ExpressionIndices.end())
49dc707122SEaswaran Raman     return Counter::getExpression(It->second);
50dc707122SEaswaran Raman   unsigned I = Expressions.size();
51dc707122SEaswaran Raman   Expressions.push_back(E);
52dc707122SEaswaran Raman   ExpressionIndices[E] = I;
53dc707122SEaswaran Raman   return Counter::getExpression(I);
54dc707122SEaswaran Raman }
55dc707122SEaswaran Raman 
extractTerms(Counter C,int Factor,SmallVectorImpl<Term> & Terms)5671b3d721SVedant Kumar void CounterExpressionBuilder::extractTerms(Counter C, int Factor,
5771b3d721SVedant Kumar                                             SmallVectorImpl<Term> &Terms) {
58dc707122SEaswaran Raman   switch (C.getKind()) {
59dc707122SEaswaran Raman   case Counter::Zero:
60dc707122SEaswaran Raman     break;
61dc707122SEaswaran Raman   case Counter::CounterValueReference:
6271b3d721SVedant Kumar     Terms.emplace_back(C.getCounterID(), Factor);
63dc707122SEaswaran Raman     break;
64dc707122SEaswaran Raman   case Counter::Expression:
65dc707122SEaswaran Raman     const auto &E = Expressions[C.getExpressionID()];
6671b3d721SVedant Kumar     extractTerms(E.LHS, Factor, Terms);
6771b3d721SVedant Kumar     extractTerms(
6871b3d721SVedant Kumar         E.RHS, E.Kind == CounterExpression::Subtract ? -Factor : Factor, Terms);
69dc707122SEaswaran Raman     break;
70dc707122SEaswaran Raman   }
71dc707122SEaswaran Raman }
72dc707122SEaswaran Raman 
simplify(Counter ExpressionTree)73dc707122SEaswaran Raman Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
74dc707122SEaswaran Raman   // Gather constant terms.
7571b3d721SVedant Kumar   SmallVector<Term, 32> Terms;
76dc707122SEaswaran Raman   extractTerms(ExpressionTree, +1, Terms);
77dc707122SEaswaran Raman 
78dc707122SEaswaran Raman   // If there are no terms, this is just a zero. The algorithm below assumes at
79dc707122SEaswaran Raman   // least one term.
80dc707122SEaswaran Raman   if (Terms.size() == 0)
81dc707122SEaswaran Raman     return Counter::getZero();
82dc707122SEaswaran Raman 
83dc707122SEaswaran Raman   // Group the terms by counter ID.
840cac726aSFangrui Song   llvm::sort(Terms, [](const Term &LHS, const Term &RHS) {
8571b3d721SVedant Kumar     return LHS.CounterID < RHS.CounterID;
86dc707122SEaswaran Raman   });
87dc707122SEaswaran Raman 
88dc707122SEaswaran Raman   // Combine terms by counter ID to eliminate counters that sum to zero.
89dc707122SEaswaran Raman   auto Prev = Terms.begin();
90dc707122SEaswaran Raman   for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) {
9171b3d721SVedant Kumar     if (I->CounterID == Prev->CounterID) {
9271b3d721SVedant Kumar       Prev->Factor += I->Factor;
93dc707122SEaswaran Raman       continue;
94dc707122SEaswaran Raman     }
95dc707122SEaswaran Raman     ++Prev;
96dc707122SEaswaran Raman     *Prev = *I;
97dc707122SEaswaran Raman   }
98dc707122SEaswaran Raman   Terms.erase(++Prev, Terms.end());
99dc707122SEaswaran Raman 
100dc707122SEaswaran Raman   Counter C;
101dc707122SEaswaran Raman   // Create additions. We do this before subtractions to avoid constructs like
102dc707122SEaswaran Raman   // ((0 - X) + Y), as opposed to (Y - X).
10371b3d721SVedant Kumar   for (auto T : Terms) {
10471b3d721SVedant Kumar     if (T.Factor <= 0)
105dc707122SEaswaran Raman       continue;
10671b3d721SVedant Kumar     for (int I = 0; I < T.Factor; ++I)
107dc707122SEaswaran Raman       if (C.isZero())
10871b3d721SVedant Kumar         C = Counter::getCounter(T.CounterID);
109dc707122SEaswaran Raman       else
110dc707122SEaswaran Raman         C = get(CounterExpression(CounterExpression::Add, C,
11171b3d721SVedant Kumar                                   Counter::getCounter(T.CounterID)));
112dc707122SEaswaran Raman   }
113dc707122SEaswaran Raman 
114dc707122SEaswaran Raman   // Create subtractions.
11571b3d721SVedant Kumar   for (auto T : Terms) {
11671b3d721SVedant Kumar     if (T.Factor >= 0)
117dc707122SEaswaran Raman       continue;
11871b3d721SVedant Kumar     for (int I = 0; I < -T.Factor; ++I)
119dc707122SEaswaran Raman       C = get(CounterExpression(CounterExpression::Subtract, C,
12071b3d721SVedant Kumar                                 Counter::getCounter(T.CounterID)));
121dc707122SEaswaran Raman   }
122dc707122SEaswaran Raman   return C;
123dc707122SEaswaran Raman }
124dc707122SEaswaran Raman 
add(Counter LHS,Counter RHS,bool Simplify)125ce54b226SBruno Cardoso Lopes Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS, bool Simplify) {
126ce54b226SBruno Cardoso Lopes   auto Cnt = get(CounterExpression(CounterExpression::Add, LHS, RHS));
127ce54b226SBruno Cardoso Lopes   return Simplify ? simplify(Cnt) : Cnt;
128dc707122SEaswaran Raman }
129dc707122SEaswaran Raman 
subtract(Counter LHS,Counter RHS,bool Simplify)130ce54b226SBruno Cardoso Lopes Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
131ce54b226SBruno Cardoso Lopes                                            bool Simplify) {
132ce54b226SBruno Cardoso Lopes   auto Cnt = get(CounterExpression(CounterExpression::Subtract, LHS, RHS));
133ce54b226SBruno Cardoso Lopes   return Simplify ? simplify(Cnt) : Cnt;
134dc707122SEaswaran Raman }
135dc707122SEaswaran Raman 
dump(const Counter & C,raw_ostream & OS) const136e78d131aSEugene Zelenko void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
137dc707122SEaswaran Raman   switch (C.getKind()) {
138dc707122SEaswaran Raman   case Counter::Zero:
139dc707122SEaswaran Raman     OS << '0';
140dc707122SEaswaran Raman     return;
141dc707122SEaswaran Raman   case Counter::CounterValueReference:
142dc707122SEaswaran Raman     OS << '#' << C.getCounterID();
143dc707122SEaswaran Raman     break;
144dc707122SEaswaran Raman   case Counter::Expression: {
145dc707122SEaswaran Raman     if (C.getExpressionID() >= Expressions.size())
146dc707122SEaswaran Raman       return;
147dc707122SEaswaran Raman     const auto &E = Expressions[C.getExpressionID()];
148dc707122SEaswaran Raman     OS << '(';
149dc707122SEaswaran Raman     dump(E.LHS, OS);
150dc707122SEaswaran Raman     OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
151dc707122SEaswaran Raman     dump(E.RHS, OS);
152dc707122SEaswaran Raman     OS << ')';
153dc707122SEaswaran Raman     break;
154dc707122SEaswaran Raman   }
155dc707122SEaswaran Raman   }
156dc707122SEaswaran Raman   if (CounterValues.empty())
157dc707122SEaswaran Raman     return;
1589152fd17SVedant Kumar   Expected<int64_t> Value = evaluate(C);
1599152fd17SVedant Kumar   if (auto E = Value.takeError()) {
160e78d131aSEugene Zelenko     consumeError(std::move(E));
161dc707122SEaswaran Raman     return;
1629152fd17SVedant Kumar   }
163dc707122SEaswaran Raman   OS << '[' << *Value << ']';
164dc707122SEaswaran Raman }
165dc707122SEaswaran Raman 
evaluate(const Counter & C) const1669152fd17SVedant Kumar Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
167dc707122SEaswaran Raman   switch (C.getKind()) {
168dc707122SEaswaran Raman   case Counter::Zero:
169dc707122SEaswaran Raman     return 0;
170dc707122SEaswaran Raman   case Counter::CounterValueReference:
171dc707122SEaswaran Raman     if (C.getCounterID() >= CounterValues.size())
1729152fd17SVedant Kumar       return errorCodeToError(errc::argument_out_of_domain);
173dc707122SEaswaran Raman     return CounterValues[C.getCounterID()];
174dc707122SEaswaran Raman   case Counter::Expression: {
175dc707122SEaswaran Raman     if (C.getExpressionID() >= Expressions.size())
1769152fd17SVedant Kumar       return errorCodeToError(errc::argument_out_of_domain);
177dc707122SEaswaran Raman     const auto &E = Expressions[C.getExpressionID()];
1789152fd17SVedant Kumar     Expected<int64_t> LHS = evaluate(E.LHS);
179dc707122SEaswaran Raman     if (!LHS)
180dc707122SEaswaran Raman       return LHS;
1819152fd17SVedant Kumar     Expected<int64_t> RHS = evaluate(E.RHS);
182dc707122SEaswaran Raman     if (!RHS)
183dc707122SEaswaran Raman       return RHS;
184dc707122SEaswaran Raman     return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
185dc707122SEaswaran Raman   }
186dc707122SEaswaran Raman   }
187dc707122SEaswaran Raman   llvm_unreachable("Unhandled CounterKind");
188dc707122SEaswaran Raman }
189dc707122SEaswaran Raman 
getMaxCounterID(const Counter & C) const190e4274cfeSPirama Arumuga Nainar unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const {
191e4274cfeSPirama Arumuga Nainar   switch (C.getKind()) {
192e4274cfeSPirama Arumuga Nainar   case Counter::Zero:
193e4274cfeSPirama Arumuga Nainar     return 0;
194e4274cfeSPirama Arumuga Nainar   case Counter::CounterValueReference:
195e4274cfeSPirama Arumuga Nainar     return C.getCounterID();
196e4274cfeSPirama Arumuga Nainar   case Counter::Expression: {
197e4274cfeSPirama Arumuga Nainar     if (C.getExpressionID() >= Expressions.size())
198e4274cfeSPirama Arumuga Nainar       return 0;
199e4274cfeSPirama Arumuga Nainar     const auto &E = Expressions[C.getExpressionID()];
200e4274cfeSPirama Arumuga Nainar     return std::max(getMaxCounterID(E.LHS), getMaxCounterID(E.RHS));
201e4274cfeSPirama Arumuga Nainar   }
202e4274cfeSPirama Arumuga Nainar   }
203e4274cfeSPirama Arumuga Nainar   llvm_unreachable("Unhandled CounterKind");
204e4274cfeSPirama Arumuga Nainar }
205e4274cfeSPirama Arumuga Nainar 
skipOtherFiles()206dc707122SEaswaran Raman void FunctionRecordIterator::skipOtherFiles() {
207dc707122SEaswaran Raman   while (Current != Records.end() && !Filename.empty() &&
208dc707122SEaswaran Raman          Filename != Current->Filenames[0])
209dc707122SEaswaran Raman     ++Current;
210dc707122SEaswaran Raman   if (Current == Records.end())
211dc707122SEaswaran Raman     *this = FunctionRecordIterator();
212dc707122SEaswaran Raman }
213dc707122SEaswaran Raman 
getImpreciseRecordIndicesForFilename(StringRef Filename) const214413647d7SVedant Kumar ArrayRef<unsigned> CoverageMapping::getImpreciseRecordIndicesForFilename(
215413647d7SVedant Kumar     StringRef Filename) const {
216413647d7SVedant Kumar   size_t FilenameHash = hash_value(Filename);
217413647d7SVedant Kumar   auto RecordIt = FilenameHash2RecordIndices.find(FilenameHash);
218413647d7SVedant Kumar   if (RecordIt == FilenameHash2RecordIndices.end())
219413647d7SVedant Kumar     return {};
220413647d7SVedant Kumar   return RecordIt->second;
221413647d7SVedant Kumar }
222413647d7SVedant Kumar 
getMaxCounterID(const CounterMappingContext & Ctx,const CoverageMappingRecord & Record)223e4274cfeSPirama Arumuga Nainar static unsigned getMaxCounterID(const CounterMappingContext &Ctx,
224e4274cfeSPirama Arumuga Nainar                                 const CoverageMappingRecord &Record) {
225e4274cfeSPirama Arumuga Nainar   unsigned MaxCounterID = 0;
226e4274cfeSPirama Arumuga Nainar   for (const auto &Region : Record.MappingRegions) {
227e4274cfeSPirama Arumuga Nainar     MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count));
228e4274cfeSPirama Arumuga Nainar   }
229e4274cfeSPirama Arumuga Nainar   return MaxCounterID;
230e4274cfeSPirama Arumuga Nainar }
231e4274cfeSPirama Arumuga Nainar 
loadFunctionRecord(const CoverageMappingRecord & Record,IndexedInstrProfReader & ProfileReader)23268216d7bSVedant Kumar Error CoverageMapping::loadFunctionRecord(
23368216d7bSVedant Kumar     const CoverageMappingRecord &Record,
234dc707122SEaswaran Raman     IndexedInstrProfReader &ProfileReader) {
235743574b8SVedant Kumar   StringRef OrigFuncName = Record.FunctionName;
236b1d331a3SVedant Kumar   if (OrigFuncName.empty())
237b1d331a3SVedant Kumar     return make_error<CoverageMapError>(coveragemap_error::malformed);
238b1d331a3SVedant Kumar 
239743574b8SVedant Kumar   if (Record.Filenames.empty())
240743574b8SVedant Kumar     OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
241743574b8SVedant Kumar   else
242743574b8SVedant Kumar     OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
243743574b8SVedant Kumar 
244dc707122SEaswaran Raman   CounterMappingContext Ctx(Record.Expressions);
245dc707122SEaswaran Raman 
24668216d7bSVedant Kumar   std::vector<uint64_t> Counts;
24768216d7bSVedant Kumar   if (Error E = ProfileReader.getFunctionCounts(Record.FunctionName,
24868216d7bSVedant Kumar                                                 Record.FunctionHash, Counts)) {
2499152fd17SVedant Kumar     instrprof_error IPE = InstrProfError::take(std::move(E));
2509152fd17SVedant Kumar     if (IPE == instrprof_error::hash_mismatch) {
251a9bc7b83SBenjamin Kramer       FuncHashMismatches.emplace_back(std::string(Record.FunctionName),
252a9bc7b83SBenjamin Kramer                                       Record.FunctionHash);
25368216d7bSVedant Kumar       return Error::success();
2549152fd17SVedant Kumar     } else if (IPE != instrprof_error::unknown_function)
2559152fd17SVedant Kumar       return make_error<InstrProfError>(IPE);
256e4274cfeSPirama Arumuga Nainar     Counts.assign(getMaxCounterID(Ctx, Record) + 1, 0);
257dc707122SEaswaran Raman   }
258dc707122SEaswaran Raman   Ctx.setCounts(Counts);
259dc707122SEaswaran Raman 
260dc707122SEaswaran Raman   assert(!Record.MappingRegions.empty() && "Function has no regions");
261dc707122SEaswaran Raman 
262381e9d23SVedant Kumar   // This coverage record is a zero region for a function that's unused in
263381e9d23SVedant Kumar   // some TU, but used in a different TU. Ignore it. The coverage maps from the
264381e9d23SVedant Kumar   // the other TU will either be loaded (providing full region counts) or they
265381e9d23SVedant Kumar   // won't (in which case we don't unintuitively report functions as uncovered
266381e9d23SVedant Kumar   // when they have non-zero counts in the profile).
267381e9d23SVedant Kumar   if (Record.MappingRegions.size() == 1 &&
268381e9d23SVedant Kumar       Record.MappingRegions[0].Count.isZero() && Counts[0] > 0)
269381e9d23SVedant Kumar     return Error::success();
270381e9d23SVedant Kumar 
271dc707122SEaswaran Raman   FunctionRecord Function(OrigFuncName, Record.Filenames);
272dc707122SEaswaran Raman   for (const auto &Region : Record.MappingRegions) {
2739152fd17SVedant Kumar     Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
2749152fd17SVedant Kumar     if (auto E = ExecutionCount.takeError()) {
275e78d131aSEugene Zelenko       consumeError(std::move(E));
27668216d7bSVedant Kumar       return Error::success();
2779152fd17SVedant Kumar     }
2789f2967bcSAlan Phipps     Expected<int64_t> AltExecutionCount = Ctx.evaluate(Region.FalseCount);
2799f2967bcSAlan Phipps     if (auto E = AltExecutionCount.takeError()) {
2809f2967bcSAlan Phipps       consumeError(std::move(E));
2819f2967bcSAlan Phipps       return Error::success();
2829f2967bcSAlan Phipps     }
2839f2967bcSAlan Phipps     Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount);
284dc707122SEaswaran Raman   }
285dc707122SEaswaran Raman 
286381e9d23SVedant Kumar   // Don't create records for (filenames, function) pairs we've already seen.
287381e9d23SVedant Kumar   auto FilenamesHash = hash_combine_range(Record.Filenames.begin(),
288381e9d23SVedant Kumar                                           Record.Filenames.end());
289381e9d23SVedant Kumar   if (!RecordProvenance[FilenamesHash].insert(hash_value(OrigFuncName)).second)
290381e9d23SVedant Kumar     return Error::success();
291381e9d23SVedant Kumar 
29268216d7bSVedant Kumar   Functions.push_back(std::move(Function));
293413647d7SVedant Kumar 
294413647d7SVedant Kumar   // Performance optimization: keep track of the indices of the function records
295413647d7SVedant Kumar   // which correspond to each filename. This can be used to substantially speed
296413647d7SVedant Kumar   // up queries for coverage info in a file.
297413647d7SVedant Kumar   unsigned RecordIndex = Functions.size() - 1;
298413647d7SVedant Kumar   for (StringRef Filename : Record.Filenames) {
299413647d7SVedant Kumar     auto &RecordIndices = FilenameHash2RecordIndices[hash_value(Filename)];
300413647d7SVedant Kumar     // Note that there may be duplicates in the filename set for a function
301413647d7SVedant Kumar     // record, because of e.g. macro expansions in the function in which both
302413647d7SVedant Kumar     // the macro and the function are defined in the same file.
303413647d7SVedant Kumar     if (RecordIndices.empty() || RecordIndices.back() != RecordIndex)
304413647d7SVedant Kumar       RecordIndices.push_back(RecordIndex);
305413647d7SVedant Kumar   }
306413647d7SVedant Kumar 
30768216d7bSVedant Kumar   return Error::success();
308dc707122SEaswaran Raman }
309dc707122SEaswaran Raman 
310772e1dd1SChoongwoo Han // This function is for memory optimization by shortening the lifetimes
311772e1dd1SChoongwoo Han // of CoverageMappingReader instances.
loadFromReaders(ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,IndexedInstrProfReader & ProfileReader,CoverageMapping & Coverage)312772e1dd1SChoongwoo Han Error CoverageMapping::loadFromReaders(
313772e1dd1SChoongwoo Han     ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
314772e1dd1SChoongwoo Han     IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage) {
315772e1dd1SChoongwoo Han   for (const auto &CoverageReader : CoverageReaders) {
316772e1dd1SChoongwoo Han     for (auto RecordOrErr : *CoverageReader) {
317772e1dd1SChoongwoo Han       if (Error E = RecordOrErr.takeError())
318772e1dd1SChoongwoo Han         return E;
319772e1dd1SChoongwoo Han       const auto &Record = *RecordOrErr;
320772e1dd1SChoongwoo Han       if (Error E = Coverage.loadFunctionRecord(Record, ProfileReader))
321772e1dd1SChoongwoo Han         return E;
322772e1dd1SChoongwoo Han     }
323772e1dd1SChoongwoo Han   }
324772e1dd1SChoongwoo Han   return Error::success();
325772e1dd1SChoongwoo Han }
326772e1dd1SChoongwoo Han 
load(ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,IndexedInstrProfReader & ProfileReader)327743574b8SVedant Kumar Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
328743574b8SVedant Kumar     ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
329743574b8SVedant Kumar     IndexedInstrProfReader &ProfileReader) {
330743574b8SVedant Kumar   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
331772e1dd1SChoongwoo Han   if (Error E = loadFromReaders(CoverageReaders, ProfileReader, *Coverage))
332c55cf4afSBill Wendling     return std::move(E);
333c55cf4afSBill Wendling   return std::move(Coverage);
334743574b8SVedant Kumar }
335743574b8SVedant Kumar 
336f025968bSJames Y Knight // If E is a no_data_found error, returns success. Otherwise returns E.
handleMaybeNoDataFoundError(Error E)337f025968bSJames Y Knight static Error handleMaybeNoDataFoundError(Error E) {
338f025968bSJames Y Knight   return handleErrors(
339f025968bSJames Y Knight       std::move(E), [](const CoverageMapError &CME) {
340f025968bSJames Y Knight         if (CME.get() == coveragemap_error::no_data_found)
341f025968bSJames Y Knight           return static_cast<Error>(Error::success());
342f025968bSJames Y Knight         return make_error<CoverageMapError>(CME.get());
343f025968bSJames Y Knight       });
344f025968bSJames Y Knight }
345f025968bSJames Y Knight 
346743574b8SVedant Kumar Expected<std::unique_ptr<CoverageMapping>>
load(ArrayRef<StringRef> ObjectFilenames,StringRef ProfileFilename,ArrayRef<StringRef> Arches,StringRef CompilationDir)347743574b8SVedant Kumar CoverageMapping::load(ArrayRef<StringRef> ObjectFilenames,
3488280ece0SPetr Hosek                       StringRef ProfileFilename, ArrayRef<StringRef> Arches,
3498280ece0SPetr Hosek                       StringRef CompilationDir) {
350dc707122SEaswaran Raman   auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
3519152fd17SVedant Kumar   if (Error E = ProfileReaderOrErr.takeError())
352*4979b16dSZequan Wu     return createFileError(ProfileFilename, std::move(E));
353dc707122SEaswaran Raman   auto ProfileReader = std::move(ProfileReaderOrErr.get());
354772e1dd1SChoongwoo Han   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
355772e1dd1SChoongwoo Han   bool DataFound = false;
356743574b8SVedant Kumar 
3574b102c3dSVedant Kumar   for (const auto &File : llvm::enumerate(ObjectFilenames)) {
358772e1dd1SChoongwoo Han     auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(
359f5349922SAbhina Sreeskantharajan         File.value(), /*IsText=*/false, /*RequiresNullTerminator=*/false);
360743574b8SVedant Kumar     if (std::error_code EC = CovMappingBufOrErr.getError())
361*4979b16dSZequan Wu       return createFileError(File.value(), errorCodeToError(EC));
3624b102c3dSVedant Kumar     StringRef Arch = Arches.empty() ? StringRef() : Arches[File.index()];
363901d04fcSVedant Kumar     MemoryBufferRef CovMappingBufRef =
364901d04fcSVedant Kumar         CovMappingBufOrErr.get()->getMemBufferRef();
365772e1dd1SChoongwoo Han     SmallVector<std::unique_ptr<MemoryBuffer>, 4> Buffers;
3668280ece0SPetr Hosek     auto CoverageReadersOrErr = BinaryCoverageReader::create(
3678280ece0SPetr Hosek         CovMappingBufRef, Arch, Buffers, CompilationDir);
368f025968bSJames Y Knight     if (Error E = CoverageReadersOrErr.takeError()) {
369f025968bSJames Y Knight       E = handleMaybeNoDataFoundError(std::move(E));
370f025968bSJames Y Knight       if (E)
371*4979b16dSZequan Wu         return createFileError(File.value(), std::move(E));
372f025968bSJames Y Knight       // E == success (originally a no_data_found error).
373f025968bSJames Y Knight       continue;
374f025968bSJames Y Knight     }
375772e1dd1SChoongwoo Han 
376772e1dd1SChoongwoo Han     SmallVector<std::unique_ptr<CoverageMappingReader>, 4> Readers;
377901d04fcSVedant Kumar     for (auto &Reader : CoverageReadersOrErr.get())
378901d04fcSVedant Kumar       Readers.push_back(std::move(Reader));
379772e1dd1SChoongwoo Han     DataFound |= !Readers.empty();
380772e1dd1SChoongwoo Han     if (Error E = loadFromReaders(Readers, *ProfileReader, *Coverage))
381*4979b16dSZequan Wu       return createFileError(File.value(), std::move(E));
382743574b8SVedant Kumar   }
383f025968bSJames Y Knight   // If no readers were created, either no objects were provided or none of them
384f025968bSJames Y Knight   // had coverage data. Return an error in the latter case.
385772e1dd1SChoongwoo Han   if (!DataFound && !ObjectFilenames.empty())
386*4979b16dSZequan Wu     return createFileError(
387*4979b16dSZequan Wu         join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "),
388*4979b16dSZequan Wu         make_error<CoverageMapError>(coveragemap_error::no_data_found));
389772e1dd1SChoongwoo Han   return std::move(Coverage);
390dc707122SEaswaran Raman }
391dc707122SEaswaran Raman 
392dc707122SEaswaran Raman namespace {
393e78d131aSEugene Zelenko 
3945f8f34e4SAdrian Prantl /// Distributes functions into instantiation sets.
395dc707122SEaswaran Raman ///
396dc707122SEaswaran Raman /// An instantiation set is a collection of functions that have the same source
397dc707122SEaswaran Raman /// code, ie, template functions specializations.
398dc707122SEaswaran Raman class FunctionInstantiationSetCollector {
3997bef6da6SVedant Kumar   using MapT = std::map<LineColPair, std::vector<const FunctionRecord *>>;
400dc707122SEaswaran Raman   MapT InstantiatedFunctions;
401dc707122SEaswaran Raman 
402dc707122SEaswaran Raman public:
insert(const FunctionRecord & Function,unsigned FileID)403dc707122SEaswaran Raman   void insert(const FunctionRecord &Function, unsigned FileID) {
404dc707122SEaswaran Raman     auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
405dc707122SEaswaran Raman     while (I != E && I->FileID != FileID)
406dc707122SEaswaran Raman       ++I;
407dc707122SEaswaran Raman     assert(I != E && "function does not cover the given file");
408dc707122SEaswaran Raman     auto &Functions = InstantiatedFunctions[I->startLoc()];
409dc707122SEaswaran Raman     Functions.push_back(&Function);
410dc707122SEaswaran Raman   }
411dc707122SEaswaran Raman 
begin()412dc707122SEaswaran Raman   MapT::iterator begin() { return InstantiatedFunctions.begin(); }
end()413dc707122SEaswaran Raman   MapT::iterator end() { return InstantiatedFunctions.end(); }
414dc707122SEaswaran Raman };
415dc707122SEaswaran Raman 
416dc707122SEaswaran Raman class SegmentBuilder {
417dc707122SEaswaran Raman   std::vector<CoverageSegment> &Segments;
418dc707122SEaswaran Raman   SmallVector<const CountedRegion *, 8> ActiveRegions;
419dc707122SEaswaran Raman 
SegmentBuilder(std::vector<CoverageSegment> & Segments)420dc707122SEaswaran Raman   SegmentBuilder(std::vector<CoverageSegment> &Segments) : Segments(Segments) {}
421dc707122SEaswaran Raman 
42279a1b5eeSVedant Kumar   /// Emit a segment with the count from \p Region starting at \p StartLoc.
42379a1b5eeSVedant Kumar   //
424ad8f637bSVedant Kumar   /// \p IsRegionEntry: The segment is at the start of a new non-gap region.
42579a1b5eeSVedant Kumar   /// \p EmitSkippedRegion: The segment must be emitted as a skipped region.
startSegment(const CountedRegion & Region,LineColPair StartLoc,bool IsRegionEntry,bool EmitSkippedRegion=false)42679a1b5eeSVedant Kumar   void startSegment(const CountedRegion &Region, LineColPair StartLoc,
42779a1b5eeSVedant Kumar                     bool IsRegionEntry, bool EmitSkippedRegion = false) {
42879a1b5eeSVedant Kumar     bool HasCount = !EmitSkippedRegion &&
42979a1b5eeSVedant Kumar                     (Region.Kind != CounterMappingRegion::SkippedRegion);
43079a1b5eeSVedant Kumar 
43179a1b5eeSVedant Kumar     // If the new segment wouldn't affect coverage rendering, skip it.
43279a1b5eeSVedant Kumar     if (!Segments.empty() && !IsRegionEntry && !EmitSkippedRegion) {
43379a1b5eeSVedant Kumar       const auto &Last = Segments.back();
43479a1b5eeSVedant Kumar       if (Last.HasCount == HasCount && Last.Count == Region.ExecutionCount &&
43579a1b5eeSVedant Kumar           !Last.IsRegionEntry)
43679a1b5eeSVedant Kumar         return;
437dc707122SEaswaran Raman     }
438dc707122SEaswaran Raman 
43979a1b5eeSVedant Kumar     if (HasCount)
44079a1b5eeSVedant Kumar       Segments.emplace_back(StartLoc.first, StartLoc.second,
441ad8f637bSVedant Kumar                             Region.ExecutionCount, IsRegionEntry,
442ad8f637bSVedant Kumar                             Region.Kind == CounterMappingRegion::GapRegion);
443dc707122SEaswaran Raman     else
44479a1b5eeSVedant Kumar       Segments.emplace_back(StartLoc.first, StartLoc.second, IsRegionEntry);
44579a1b5eeSVedant Kumar 
446d34e60caSNicola Zaghen     LLVM_DEBUG({
44779a1b5eeSVedant Kumar       const auto &Last = Segments.back();
44879a1b5eeSVedant Kumar       dbgs() << "Segment at " << Last.Line << ":" << Last.Col
44979a1b5eeSVedant Kumar              << " (count = " << Last.Count << ")"
45079a1b5eeSVedant Kumar              << (Last.IsRegionEntry ? ", RegionEntry" : "")
451ad8f637bSVedant Kumar              << (!Last.HasCount ? ", Skipped" : "")
452ad8f637bSVedant Kumar              << (Last.IsGapRegion ? ", Gap" : "") << "\n";
45379a1b5eeSVedant Kumar     });
45479a1b5eeSVedant Kumar   }
45579a1b5eeSVedant Kumar 
45679a1b5eeSVedant Kumar   /// Emit segments for active regions which end before \p Loc.
45779a1b5eeSVedant Kumar   ///
45879a1b5eeSVedant Kumar   /// \p Loc: The start location of the next region. If None, all active
45979a1b5eeSVedant Kumar   /// regions are completed.
46079a1b5eeSVedant Kumar   /// \p FirstCompletedRegion: Index of the first completed region.
completeRegionsUntil(Optional<LineColPair> Loc,unsigned FirstCompletedRegion)46179a1b5eeSVedant Kumar   void completeRegionsUntil(Optional<LineColPair> Loc,
46279a1b5eeSVedant Kumar                             unsigned FirstCompletedRegion) {
46379a1b5eeSVedant Kumar     // Sort the completed regions by end location. This makes it simple to
46479a1b5eeSVedant Kumar     // emit closing segments in sorted order.
46579a1b5eeSVedant Kumar     auto CompletedRegionsIt = ActiveRegions.begin() + FirstCompletedRegion;
46679a1b5eeSVedant Kumar     std::stable_sort(CompletedRegionsIt, ActiveRegions.end(),
46779a1b5eeSVedant Kumar                       [](const CountedRegion *L, const CountedRegion *R) {
46879a1b5eeSVedant Kumar                         return L->endLoc() < R->endLoc();
46979a1b5eeSVedant Kumar                       });
47079a1b5eeSVedant Kumar 
47179a1b5eeSVedant Kumar     // Emit segments for all completed regions.
47279a1b5eeSVedant Kumar     for (unsigned I = FirstCompletedRegion + 1, E = ActiveRegions.size(); I < E;
47379a1b5eeSVedant Kumar          ++I) {
47479a1b5eeSVedant Kumar       const auto *CompletedRegion = ActiveRegions[I];
47579a1b5eeSVedant Kumar       assert((!Loc || CompletedRegion->endLoc() <= *Loc) &&
47679a1b5eeSVedant Kumar              "Completed region ends after start of new region");
47779a1b5eeSVedant Kumar 
47879a1b5eeSVedant Kumar       const auto *PrevCompletedRegion = ActiveRegions[I - 1];
47979a1b5eeSVedant Kumar       auto CompletedSegmentLoc = PrevCompletedRegion->endLoc();
48079a1b5eeSVedant Kumar 
48179a1b5eeSVedant Kumar       // Don't emit any more segments if they start where the new region begins.
48279a1b5eeSVedant Kumar       if (Loc && CompletedSegmentLoc == *Loc)
48379a1b5eeSVedant Kumar         break;
48479a1b5eeSVedant Kumar 
48579a1b5eeSVedant Kumar       // Don't emit a segment if the next completed region ends at the same
48679a1b5eeSVedant Kumar       // location as this one.
48779a1b5eeSVedant Kumar       if (CompletedSegmentLoc == CompletedRegion->endLoc())
48879a1b5eeSVedant Kumar         continue;
48979a1b5eeSVedant Kumar 
490337b0db1SVedant Kumar       // Use the count from the last completed region which ends at this loc.
491337b0db1SVedant Kumar       for (unsigned J = I + 1; J < E; ++J)
492337b0db1SVedant Kumar         if (CompletedRegion->endLoc() == ActiveRegions[J]->endLoc())
493337b0db1SVedant Kumar           CompletedRegion = ActiveRegions[J];
49480fbb855SVedant Kumar 
49579a1b5eeSVedant Kumar       startSegment(*CompletedRegion, CompletedSegmentLoc, false);
49679a1b5eeSVedant Kumar     }
49779a1b5eeSVedant Kumar 
49879a1b5eeSVedant Kumar     auto Last = ActiveRegions.back();
49979a1b5eeSVedant Kumar     if (FirstCompletedRegion && Last->endLoc() != *Loc) {
50079a1b5eeSVedant Kumar       // If there's a gap after the end of the last completed region and the
50179a1b5eeSVedant Kumar       // start of the new region, use the last active region to fill the gap.
50279a1b5eeSVedant Kumar       startSegment(*ActiveRegions[FirstCompletedRegion - 1], Last->endLoc(),
50379a1b5eeSVedant Kumar                    false);
50479a1b5eeSVedant Kumar     } else if (!FirstCompletedRegion && (!Loc || *Loc != Last->endLoc())) {
50579a1b5eeSVedant Kumar       // Emit a skipped segment if there are no more active regions. This
50679a1b5eeSVedant Kumar       // ensures that gaps between functions are marked correctly.
50779a1b5eeSVedant Kumar       startSegment(*Last, Last->endLoc(), false, true);
50879a1b5eeSVedant Kumar     }
50979a1b5eeSVedant Kumar 
51079a1b5eeSVedant Kumar     // Pop the completed regions.
51179a1b5eeSVedant Kumar     ActiveRegions.erase(CompletedRegionsIt, ActiveRegions.end());
512dc707122SEaswaran Raman   }
513dc707122SEaswaran Raman 
buildSegmentsImpl(ArrayRef<CountedRegion> Regions)514dc707122SEaswaran Raman   void buildSegmentsImpl(ArrayRef<CountedRegion> Regions) {
51579a1b5eeSVedant Kumar     for (const auto &CR : enumerate(Regions)) {
51679a1b5eeSVedant Kumar       auto CurStartLoc = CR.value().startLoc();
51779a1b5eeSVedant Kumar 
51879a1b5eeSVedant Kumar       // Active regions which end before the current region need to be popped.
51979a1b5eeSVedant Kumar       auto CompletedRegions =
52079a1b5eeSVedant Kumar           std::stable_partition(ActiveRegions.begin(), ActiveRegions.end(),
52179a1b5eeSVedant Kumar                                 [&](const CountedRegion *Region) {
52279a1b5eeSVedant Kumar                                   return !(Region->endLoc() <= CurStartLoc);
52379a1b5eeSVedant Kumar                                 });
52479a1b5eeSVedant Kumar       if (CompletedRegions != ActiveRegions.end()) {
52579a1b5eeSVedant Kumar         unsigned FirstCompletedRegion =
52679a1b5eeSVedant Kumar             std::distance(ActiveRegions.begin(), CompletedRegions);
52779a1b5eeSVedant Kumar         completeRegionsUntil(CurStartLoc, FirstCompletedRegion);
528dc707122SEaswaran Raman       }
52979a1b5eeSVedant Kumar 
530ad8f637bSVedant Kumar       bool GapRegion = CR.value().Kind == CounterMappingRegion::GapRegion;
531ad8f637bSVedant Kumar 
53279a1b5eeSVedant Kumar       // Try to emit a segment for the current region.
53379a1b5eeSVedant Kumar       if (CurStartLoc == CR.value().endLoc()) {
53479a1b5eeSVedant Kumar         // Avoid making zero-length regions active. If it's the last region,
53579a1b5eeSVedant Kumar         // emit a skipped segment. Otherwise use its predecessor's count.
5369caa3fbeSZequan Wu         const bool Skipped =
5379caa3fbeSZequan Wu             (CR.index() + 1) == Regions.size() ||
5389caa3fbeSZequan Wu             CR.value().Kind == CounterMappingRegion::SkippedRegion;
53979a1b5eeSVedant Kumar         startSegment(ActiveRegions.empty() ? CR.value() : *ActiveRegions.back(),
540ad8f637bSVedant Kumar                      CurStartLoc, !GapRegion, Skipped);
5419caa3fbeSZequan Wu         // If it is skipped segment, create a segment with last pushed
5429caa3fbeSZequan Wu         // regions's count at CurStartLoc.
5439caa3fbeSZequan Wu         if (Skipped && !ActiveRegions.empty())
5449caa3fbeSZequan Wu           startSegment(*ActiveRegions.back(), CurStartLoc, false);
54579a1b5eeSVedant Kumar         continue;
54679a1b5eeSVedant Kumar       }
54779a1b5eeSVedant Kumar       if (CR.index() + 1 == Regions.size() ||
54879a1b5eeSVedant Kumar           CurStartLoc != Regions[CR.index() + 1].startLoc()) {
54979a1b5eeSVedant Kumar         // Emit a segment if the next region doesn't start at the same location
55079a1b5eeSVedant Kumar         // as this one.
551ad8f637bSVedant Kumar         startSegment(CR.value(), CurStartLoc, !GapRegion);
55279a1b5eeSVedant Kumar       }
55379a1b5eeSVedant Kumar 
55479a1b5eeSVedant Kumar       // This region is active (i.e not completed).
55579a1b5eeSVedant Kumar       ActiveRegions.push_back(&CR.value());
55679a1b5eeSVedant Kumar     }
55779a1b5eeSVedant Kumar 
55879a1b5eeSVedant Kumar     // Complete any remaining active regions.
55979a1b5eeSVedant Kumar     if (!ActiveRegions.empty())
56079a1b5eeSVedant Kumar       completeRegionsUntil(None, 0);
561dc707122SEaswaran Raman   }
562dc707122SEaswaran Raman 
563dc707122SEaswaran Raman   /// Sort a nested sequence of regions from a single file.
sortNestedRegions(MutableArrayRef<CountedRegion> Regions)564dc707122SEaswaran Raman   static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) {
5650cac726aSFangrui Song     llvm::sort(Regions, [](const CountedRegion &LHS, const CountedRegion &RHS) {
56627d8dd39SIgor Kudrin       if (LHS.startLoc() != RHS.startLoc())
56727d8dd39SIgor Kudrin         return LHS.startLoc() < RHS.startLoc();
56827d8dd39SIgor Kudrin       if (LHS.endLoc() != RHS.endLoc())
569dc707122SEaswaran Raman         // When LHS completely contains RHS, we sort LHS first.
570dc707122SEaswaran Raman         return RHS.endLoc() < LHS.endLoc();
57127d8dd39SIgor Kudrin       // If LHS and RHS cover the same area, we need to sort them according
57227d8dd39SIgor Kudrin       // to their kinds so that the most suitable region will become "active"
57327d8dd39SIgor Kudrin       // in combineRegions(). Because we accumulate counter values only from
57427d8dd39SIgor Kudrin       // regions of the same kind as the first region of the area, prefer
57527d8dd39SIgor Kudrin       // CodeRegion to ExpansionRegion and ExpansionRegion to SkippedRegion.
576e78d131aSEugene Zelenko       static_assert(CounterMappingRegion::CodeRegion <
577e78d131aSEugene Zelenko                             CounterMappingRegion::ExpansionRegion &&
578e78d131aSEugene Zelenko                         CounterMappingRegion::ExpansionRegion <
579e78d131aSEugene Zelenko                             CounterMappingRegion::SkippedRegion,
58027d8dd39SIgor Kudrin                     "Unexpected order of region kind values");
58127d8dd39SIgor Kudrin       return LHS.Kind < RHS.Kind;
582dc707122SEaswaran Raman     });
583dc707122SEaswaran Raman   }
584dc707122SEaswaran Raman 
585dc707122SEaswaran Raman   /// Combine counts of regions which cover the same area.
586dc707122SEaswaran Raman   static ArrayRef<CountedRegion>
combineRegions(MutableArrayRef<CountedRegion> Regions)587dc707122SEaswaran Raman   combineRegions(MutableArrayRef<CountedRegion> Regions) {
588dc707122SEaswaran Raman     if (Regions.empty())
589dc707122SEaswaran Raman       return Regions;
590dc707122SEaswaran Raman     auto Active = Regions.begin();
591dc707122SEaswaran Raman     auto End = Regions.end();
592dc707122SEaswaran Raman     for (auto I = Regions.begin() + 1; I != End; ++I) {
593dc707122SEaswaran Raman       if (Active->startLoc() != I->startLoc() ||
594dc707122SEaswaran Raman           Active->endLoc() != I->endLoc()) {
595dc707122SEaswaran Raman         // Shift to the next region.
596dc707122SEaswaran Raman         ++Active;
597dc707122SEaswaran Raman         if (Active != I)
598dc707122SEaswaran Raman           *Active = *I;
599dc707122SEaswaran Raman         continue;
600dc707122SEaswaran Raman       }
601dc707122SEaswaran Raman       // Merge duplicate region.
60227d8dd39SIgor Kudrin       // If CodeRegions and ExpansionRegions cover the same area, it's probably
60327d8dd39SIgor Kudrin       // a macro which is fully expanded to another macro. In that case, we need
60427d8dd39SIgor Kudrin       // to accumulate counts only from CodeRegions, or else the area will be
60527d8dd39SIgor Kudrin       // counted twice.
60627d8dd39SIgor Kudrin       // On the other hand, a macro may have a nested macro in its body. If the
60727d8dd39SIgor Kudrin       // outer macro is used several times, the ExpansionRegion for the nested
60827d8dd39SIgor Kudrin       // macro will also be added several times. These ExpansionRegions cover
60927d8dd39SIgor Kudrin       // the same source locations and have to be combined to reach the correct
61027d8dd39SIgor Kudrin       // value for that area.
61127d8dd39SIgor Kudrin       // We add counts of the regions of the same kind as the active region
61227d8dd39SIgor Kudrin       // to handle the both situations.
61327d8dd39SIgor Kudrin       if (I->Kind == Active->Kind)
614dc707122SEaswaran Raman         Active->ExecutionCount += I->ExecutionCount;
615dc707122SEaswaran Raman     }
616dc707122SEaswaran Raman     return Regions.drop_back(std::distance(++Active, End));
617dc707122SEaswaran Raman   }
618dc707122SEaswaran Raman 
619dc707122SEaswaran Raman public:
62079a1b5eeSVedant Kumar   /// Build a sorted list of CoverageSegments from a list of Regions.
621dc707122SEaswaran Raman   static std::vector<CoverageSegment>
buildSegments(MutableArrayRef<CountedRegion> Regions)622dc707122SEaswaran Raman   buildSegments(MutableArrayRef<CountedRegion> Regions) {
623dc707122SEaswaran Raman     std::vector<CoverageSegment> Segments;
624dc707122SEaswaran Raman     SegmentBuilder Builder(Segments);
625dc707122SEaswaran Raman 
626dc707122SEaswaran Raman     sortNestedRegions(Regions);
627dc707122SEaswaran Raman     ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
628dc707122SEaswaran Raman 
629d34e60caSNicola Zaghen     LLVM_DEBUG({
63079a1b5eeSVedant Kumar       dbgs() << "Combined regions:\n";
63179a1b5eeSVedant Kumar       for (const auto &CR : CombinedRegions)
63279a1b5eeSVedant Kumar         dbgs() << "  " << CR.LineStart << ":" << CR.ColumnStart << " -> "
63379a1b5eeSVedant Kumar                << CR.LineEnd << ":" << CR.ColumnEnd
63479a1b5eeSVedant Kumar                << " (count=" << CR.ExecutionCount << ")\n";
63579a1b5eeSVedant Kumar     });
63679a1b5eeSVedant Kumar 
637dc707122SEaswaran Raman     Builder.buildSegmentsImpl(CombinedRegions);
63879a1b5eeSVedant Kumar 
63979a1b5eeSVedant Kumar #ifndef NDEBUG
64079a1b5eeSVedant Kumar     for (unsigned I = 1, E = Segments.size(); I < E; ++I) {
64179a1b5eeSVedant Kumar       const auto &L = Segments[I - 1];
64279a1b5eeSVedant Kumar       const auto &R = Segments[I];
64379a1b5eeSVedant Kumar       if (!(L.Line < R.Line) && !(L.Line == R.Line && L.Col < R.Col)) {
6449caa3fbeSZequan Wu         if (L.Line == R.Line && L.Col == R.Col && !L.HasCount)
6459caa3fbeSZequan Wu           continue;
646d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << " ! Segment " << L.Line << ":" << L.Col
64779a1b5eeSVedant Kumar                           << " followed by " << R.Line << ":" << R.Col << "\n");
64879a1b5eeSVedant Kumar         assert(false && "Coverage segments not unique or sorted");
64979a1b5eeSVedant Kumar       }
65079a1b5eeSVedant Kumar     }
65179a1b5eeSVedant Kumar #endif
65279a1b5eeSVedant Kumar 
653dc707122SEaswaran Raman     return Segments;
654dc707122SEaswaran Raman   }
655dc707122SEaswaran Raman };
656e78d131aSEugene Zelenko 
657e78d131aSEugene Zelenko } // end anonymous namespace
658dc707122SEaswaran Raman 
getUniqueSourceFiles() const659dc707122SEaswaran Raman std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
660dc707122SEaswaran Raman   std::vector<StringRef> Filenames;
661dc707122SEaswaran Raman   for (const auto &Function : getCoveredFunctions())
6621d0bc055SKazu Hirata     llvm::append_range(Filenames, Function.Filenames);
6630cac726aSFangrui Song   llvm::sort(Filenames);
664dc707122SEaswaran Raman   auto Last = std::unique(Filenames.begin(), Filenames.end());
665dc707122SEaswaran Raman   Filenames.erase(Last, Filenames.end());
666dc707122SEaswaran Raman   return Filenames;
667dc707122SEaswaran Raman }
668dc707122SEaswaran Raman 
gatherFileIDs(StringRef SourceFile,const FunctionRecord & Function)669dc707122SEaswaran Raman static SmallBitVector gatherFileIDs(StringRef SourceFile,
670dc707122SEaswaran Raman                                     const FunctionRecord &Function) {
671dc707122SEaswaran Raman   SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
672dc707122SEaswaran Raman   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
673dc707122SEaswaran Raman     if (SourceFile == Function.Filenames[I])
674dc707122SEaswaran Raman       FilenameEquivalence[I] = true;
675dc707122SEaswaran Raman   return FilenameEquivalence;
676dc707122SEaswaran Raman }
677dc707122SEaswaran Raman 
678dc707122SEaswaran Raman /// Return the ID of the file where the definition of the function is located.
findMainViewFileID(const FunctionRecord & Function)679dc707122SEaswaran Raman static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
680dc707122SEaswaran Raman   SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
681dc707122SEaswaran Raman   for (const auto &CR : Function.CountedRegions)
682dc707122SEaswaran Raman     if (CR.Kind == CounterMappingRegion::ExpansionRegion)
683dc707122SEaswaran Raman       IsNotExpandedFile[CR.ExpandedFileID] = false;
684dc707122SEaswaran Raman   int I = IsNotExpandedFile.find_first();
685dc707122SEaswaran Raman   if (I == -1)
686dc707122SEaswaran Raman     return None;
687dc707122SEaswaran Raman   return I;
688dc707122SEaswaran Raman }
689dc707122SEaswaran Raman 
690dc707122SEaswaran Raman /// Check if SourceFile is the file that contains the definition of
691dc707122SEaswaran Raman /// the Function. Return the ID of the file in that case or None otherwise.
findMainViewFileID(StringRef SourceFile,const FunctionRecord & Function)692dc707122SEaswaran Raman static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
693dc707122SEaswaran Raman                                              const FunctionRecord &Function) {
694dc707122SEaswaran Raman   Optional<unsigned> I = findMainViewFileID(Function);
695dc707122SEaswaran Raman   if (I && SourceFile == Function.Filenames[*I])
696dc707122SEaswaran Raman     return I;
697dc707122SEaswaran Raman   return None;
698dc707122SEaswaran Raman }
699dc707122SEaswaran Raman 
isExpansion(const CountedRegion & R,unsigned FileID)700dc707122SEaswaran Raman static bool isExpansion(const CountedRegion &R, unsigned FileID) {
701dc707122SEaswaran Raman   return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
702dc707122SEaswaran Raman }
703dc707122SEaswaran Raman 
getCoverageForFile(StringRef Filename) const7047fcc5472SVedant Kumar CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
705dc707122SEaswaran Raman   CoverageData FileCoverage(Filename);
706e78d131aSEugene Zelenko   std::vector<CountedRegion> Regions;
707dc707122SEaswaran Raman 
708413647d7SVedant Kumar   // Look up the function records in the given file. Due to hash collisions on
709413647d7SVedant Kumar   // the filename, we may get back some records that are not in the file.
710413647d7SVedant Kumar   ArrayRef<unsigned> RecordIndices =
711413647d7SVedant Kumar       getImpreciseRecordIndicesForFilename(Filename);
712413647d7SVedant Kumar   for (unsigned RecordIndex : RecordIndices) {
713413647d7SVedant Kumar     const FunctionRecord &Function = Functions[RecordIndex];
714dc707122SEaswaran Raman     auto MainFileID = findMainViewFileID(Filename, Function);
715dc707122SEaswaran Raman     auto FileIDs = gatherFileIDs(Filename, Function);
716dc707122SEaswaran Raman     for (const auto &CR : Function.CountedRegions)
717dc707122SEaswaran Raman       if (FileIDs.test(CR.FileID)) {
718dc707122SEaswaran Raman         Regions.push_back(CR);
719dc707122SEaswaran Raman         if (MainFileID && isExpansion(CR, *MainFileID))
720dc707122SEaswaran Raman           FileCoverage.Expansions.emplace_back(CR, Function);
721dc707122SEaswaran Raman       }
7229f2967bcSAlan Phipps     // Capture branch regions specific to the function (excluding expansions).
7239f2967bcSAlan Phipps     for (const auto &CR : Function.CountedBranchRegions)
7249f2967bcSAlan Phipps       if (FileIDs.test(CR.FileID) && (CR.FileID == CR.ExpandedFileID))
7259f2967bcSAlan Phipps         FileCoverage.BranchRegions.push_back(CR);
726dc707122SEaswaran Raman   }
727dc707122SEaswaran Raman 
728d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
729dc707122SEaswaran Raman   FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
730dc707122SEaswaran Raman 
731dc707122SEaswaran Raman   return FileCoverage;
732dc707122SEaswaran Raman }
733dc707122SEaswaran Raman 
734dde19c5aSVedant Kumar std::vector<InstantiationGroup>
getInstantiationGroups(StringRef Filename) const735dde19c5aSVedant Kumar CoverageMapping::getInstantiationGroups(StringRef Filename) const {
736dc707122SEaswaran Raman   FunctionInstantiationSetCollector InstantiationSetCollector;
737413647d7SVedant Kumar   // Look up the function records in the given file. Due to hash collisions on
738413647d7SVedant Kumar   // the filename, we may get back some records that are not in the file.
739413647d7SVedant Kumar   ArrayRef<unsigned> RecordIndices =
740413647d7SVedant Kumar       getImpreciseRecordIndicesForFilename(Filename);
741413647d7SVedant Kumar   for (unsigned RecordIndex : RecordIndices) {
742413647d7SVedant Kumar     const FunctionRecord &Function = Functions[RecordIndex];
743dc707122SEaswaran Raman     auto MainFileID = findMainViewFileID(Filename, Function);
744dc707122SEaswaran Raman     if (!MainFileID)
745dc707122SEaswaran Raman       continue;
746dc707122SEaswaran Raman     InstantiationSetCollector.insert(Function, *MainFileID);
747dc707122SEaswaran Raman   }
748dc707122SEaswaran Raman 
749dde19c5aSVedant Kumar   std::vector<InstantiationGroup> Result;
75024cb28bbSBenjamin Kramer   for (auto &InstantiationSet : InstantiationSetCollector) {
751dde19c5aSVedant Kumar     InstantiationGroup IG{InstantiationSet.first.first,
752dde19c5aSVedant Kumar                           InstantiationSet.first.second,
753dde19c5aSVedant Kumar                           std::move(InstantiationSet.second)};
754dde19c5aSVedant Kumar     Result.emplace_back(std::move(IG));
755dc707122SEaswaran Raman   }
756dc707122SEaswaran Raman   return Result;
757dc707122SEaswaran Raman }
758dc707122SEaswaran Raman 
759dc707122SEaswaran Raman CoverageData
getCoverageForFunction(const FunctionRecord & Function) const760f681e2e5SVedant Kumar CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const {
761dc707122SEaswaran Raman   auto MainFileID = findMainViewFileID(Function);
762dc707122SEaswaran Raman   if (!MainFileID)
763dc707122SEaswaran Raman     return CoverageData();
764dc707122SEaswaran Raman 
765dc707122SEaswaran Raman   CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
766e78d131aSEugene Zelenko   std::vector<CountedRegion> Regions;
767dc707122SEaswaran Raman   for (const auto &CR : Function.CountedRegions)
768dc707122SEaswaran Raman     if (CR.FileID == *MainFileID) {
769dc707122SEaswaran Raman       Regions.push_back(CR);
770dc707122SEaswaran Raman       if (isExpansion(CR, *MainFileID))
771dc707122SEaswaran Raman         FunctionCoverage.Expansions.emplace_back(CR, Function);
772dc707122SEaswaran Raman     }
7739f2967bcSAlan Phipps   // Capture branch regions specific to the function (excluding expansions).
7749f2967bcSAlan Phipps   for (const auto &CR : Function.CountedBranchRegions)
7759f2967bcSAlan Phipps     if (CR.FileID == *MainFileID)
7769f2967bcSAlan Phipps       FunctionCoverage.BranchRegions.push_back(CR);
777dc707122SEaswaran Raman 
778d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name
779d34e60caSNicola Zaghen                     << "\n");
780dc707122SEaswaran Raman   FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
781dc707122SEaswaran Raman 
782dc707122SEaswaran Raman   return FunctionCoverage;
783dc707122SEaswaran Raman }
784dc707122SEaswaran Raman 
getCoverageForExpansion(const ExpansionRecord & Expansion) const785f681e2e5SVedant Kumar CoverageData CoverageMapping::getCoverageForExpansion(
786f681e2e5SVedant Kumar     const ExpansionRecord &Expansion) const {
787dc707122SEaswaran Raman   CoverageData ExpansionCoverage(
788dc707122SEaswaran Raman       Expansion.Function.Filenames[Expansion.FileID]);
789e78d131aSEugene Zelenko   std::vector<CountedRegion> Regions;
790dc707122SEaswaran Raman   for (const auto &CR : Expansion.Function.CountedRegions)
791dc707122SEaswaran Raman     if (CR.FileID == Expansion.FileID) {
792dc707122SEaswaran Raman       Regions.push_back(CR);
793dc707122SEaswaran Raman       if (isExpansion(CR, Expansion.FileID))
794dc707122SEaswaran Raman         ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
795dc707122SEaswaran Raman     }
7969f2967bcSAlan Phipps   for (const auto &CR : Expansion.Function.CountedBranchRegions)
7979f2967bcSAlan Phipps     // Capture branch regions that only pertain to the corresponding expansion.
7989f2967bcSAlan Phipps     if (CR.FileID == Expansion.FileID)
7999f2967bcSAlan Phipps       ExpansionCoverage.BranchRegions.push_back(CR);
800dc707122SEaswaran Raman 
801d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Emitting segments for expansion of file "
802d34e60caSNicola Zaghen                     << Expansion.FileID << "\n");
803dc707122SEaswaran Raman   ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
804dc707122SEaswaran Raman 
805dc707122SEaswaran Raman   return ExpansionCoverage;
806dc707122SEaswaran Raman }
807dc707122SEaswaran Raman 
LineCoverageStats(ArrayRef<const CoverageSegment * > LineSegments,const CoverageSegment * WrappedSegment,unsigned Line)808821160d5SVedant Kumar LineCoverageStats::LineCoverageStats(
809f5f153ddSVedant Kumar     ArrayRef<const CoverageSegment *> LineSegments,
810f5f153ddSVedant Kumar     const CoverageSegment *WrappedSegment, unsigned Line)
811821160d5SVedant Kumar     : ExecutionCount(0), HasMultipleRegions(false), Mapped(false), Line(Line),
812821160d5SVedant Kumar       LineSegments(LineSegments), WrappedSegment(WrappedSegment) {
813821160d5SVedant Kumar   // Find the minimum number of regions which start in this line.
814821160d5SVedant Kumar   unsigned MinRegionCount = 0;
815f5f153ddSVedant Kumar   auto isStartOfRegion = [](const CoverageSegment *S) {
816821160d5SVedant Kumar     return !S->IsGapRegion && S->HasCount && S->IsRegionEntry;
817821160d5SVedant Kumar   };
818821160d5SVedant Kumar   for (unsigned I = 0; I < LineSegments.size() && MinRegionCount < 2; ++I)
819821160d5SVedant Kumar     if (isStartOfRegion(LineSegments[I]))
820821160d5SVedant Kumar       ++MinRegionCount;
821821160d5SVedant Kumar 
822821160d5SVedant Kumar   bool StartOfSkippedRegion = !LineSegments.empty() &&
823821160d5SVedant Kumar                               !LineSegments.front()->HasCount &&
824821160d5SVedant Kumar                               LineSegments.front()->IsRegionEntry;
825821160d5SVedant Kumar 
826821160d5SVedant Kumar   HasMultipleRegions = MinRegionCount > 1;
827821160d5SVedant Kumar   Mapped =
828821160d5SVedant Kumar       !StartOfSkippedRegion &&
829821160d5SVedant Kumar       ((WrappedSegment && WrappedSegment->HasCount) || (MinRegionCount > 0));
830821160d5SVedant Kumar 
831821160d5SVedant Kumar   if (!Mapped)
832821160d5SVedant Kumar     return;
833821160d5SVedant Kumar 
83443247f05SVedant Kumar   // Pick the max count from the non-gap, region entry segments and the
83543247f05SVedant Kumar   // wrapped count.
83643247f05SVedant Kumar   if (WrappedSegment)
837821160d5SVedant Kumar     ExecutionCount = WrappedSegment->Count;
83843247f05SVedant Kumar   if (!MinRegionCount)
839821160d5SVedant Kumar     return;
840821160d5SVedant Kumar   for (const auto *LS : LineSegments)
841821160d5SVedant Kumar     if (isStartOfRegion(LS))
842821160d5SVedant Kumar       ExecutionCount = std::max(ExecutionCount, LS->Count);
843821160d5SVedant Kumar }
844821160d5SVedant Kumar 
operator ++()845821160d5SVedant Kumar LineCoverageIterator &LineCoverageIterator::operator++() {
846821160d5SVedant Kumar   if (Next == CD.end()) {
847821160d5SVedant Kumar     Stats = LineCoverageStats();
848821160d5SVedant Kumar     Ended = true;
849821160d5SVedant Kumar     return *this;
850821160d5SVedant Kumar   }
851821160d5SVedant Kumar   if (Segments.size())
852821160d5SVedant Kumar     WrappedSegment = Segments.back();
853821160d5SVedant Kumar   Segments.clear();
854821160d5SVedant Kumar   while (Next != CD.end() && Next->Line == Line)
855821160d5SVedant Kumar     Segments.push_back(&*Next++);
856821160d5SVedant Kumar   Stats = LineCoverageStats(Segments, WrappedSegment, Line);
857821160d5SVedant Kumar   ++Line;
858821160d5SVedant Kumar   return *this;
859821160d5SVedant Kumar }
860821160d5SVedant Kumar 
getCoverageMapErrString(coveragemap_error Err)861e78d131aSEugene Zelenko static std::string getCoverageMapErrString(coveragemap_error Err) {
8629152fd17SVedant Kumar   switch (Err) {
863dc707122SEaswaran Raman   case coveragemap_error::success:
864dc707122SEaswaran Raman     return "Success";
865dc707122SEaswaran Raman   case coveragemap_error::eof:
866dc707122SEaswaran Raman     return "End of File";
867dc707122SEaswaran Raman   case coveragemap_error::no_data_found:
868dc707122SEaswaran Raman     return "No coverage data found";
869dc707122SEaswaran Raman   case coveragemap_error::unsupported_version:
870dc707122SEaswaran Raman     return "Unsupported coverage format version";
871dc707122SEaswaran Raman   case coveragemap_error::truncated:
872dc707122SEaswaran Raman     return "Truncated coverage data";
873dc707122SEaswaran Raman   case coveragemap_error::malformed:
874dc707122SEaswaran Raman     return "Malformed coverage data";
875dd1ea9deSVedant Kumar   case coveragemap_error::decompression_failed:
876dd1ea9deSVedant Kumar     return "Failed to decompress coverage data (zlib)";
8777fafaa07SVedant Kumar   case coveragemap_error::invalid_or_missing_arch_specifier:
8787fafaa07SVedant Kumar     return "`-arch` specifier is invalid or missing for universal binary";
879dc707122SEaswaran Raman   }
880dc707122SEaswaran Raman   llvm_unreachable("A value of coveragemap_error has no message.");
881dc707122SEaswaran Raman }
8829152fd17SVedant Kumar 
883e78d131aSEugene Zelenko namespace {
884e78d131aSEugene Zelenko 
8854718f8b5SPeter Collingbourne // FIXME: This class is only here to support the transition to llvm::Error. It
8864718f8b5SPeter Collingbourne // will be removed once this transition is complete. Clients should prefer to
8874718f8b5SPeter Collingbourne // deal with the Error value directly, rather than converting to error_code.
8889152fd17SVedant Kumar class CoverageMappingErrorCategoryType : public std::error_category {
name() const889990504e6SReid Kleckner   const char *name() const noexcept override { return "llvm.coveragemap"; }
message(int IE) const8909152fd17SVedant Kumar   std::string message(int IE) const override {
8919152fd17SVedant Kumar     return getCoverageMapErrString(static_cast<coveragemap_error>(IE));
8929152fd17SVedant Kumar   }
893dc707122SEaswaran Raman };
894e78d131aSEugene Zelenko 
8959152fd17SVedant Kumar } // end anonymous namespace
8969152fd17SVedant Kumar 
message() const8979152fd17SVedant Kumar std::string CoverageMapError::message() const {
8989152fd17SVedant Kumar   return getCoverageMapErrString(Err);
899dc707122SEaswaran Raman }
900dc707122SEaswaran Raman 
coveragemap_category()901dc707122SEaswaran Raman const std::error_category &llvm::coverage::coveragemap_category() {
902ede60037SNicolai Hähnle   static CoverageMappingErrorCategoryType ErrorCategory;
903ede60037SNicolai Hähnle   return ErrorCategory;
904dc707122SEaswaran Raman }
9059152fd17SVedant Kumar 
9069152fd17SVedant Kumar char CoverageMapError::ID = 0;
907