xref: /llvm-project/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp (revision c193bb7e9eee02a435944506c732db9b480f0c84)
1 //===- CoverageMapping.cpp - Code coverage mapping support ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for clang's and llvm's instrumentation based
10 // code coverage.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallBitVector.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Object/BuildID.h"
22 #include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
23 #include "llvm/ProfileData/InstrProfReader.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/Errc.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/VirtualFileSystem.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <algorithm>
32 #include <cassert>
33 #include <cmath>
34 #include <cstdint>
35 #include <iterator>
36 #include <map>
37 #include <memory>
38 #include <optional>
39 #include <string>
40 #include <system_error>
41 #include <utility>
42 #include <vector>
43 
44 using namespace llvm;
45 using namespace coverage;
46 
47 #define DEBUG_TYPE "coverage-mapping"
48 
49 Counter CounterExpressionBuilder::get(const CounterExpression &E) {
50   auto It = ExpressionIndices.find(E);
51   if (It != ExpressionIndices.end())
52     return Counter::getExpression(It->second);
53   unsigned I = Expressions.size();
54   Expressions.push_back(E);
55   ExpressionIndices[E] = I;
56   return Counter::getExpression(I);
57 }
58 
59 void CounterExpressionBuilder::extractTerms(Counter C, int Factor,
60                                             SmallVectorImpl<Term> &Terms) {
61   switch (C.getKind()) {
62   case Counter::Zero:
63     break;
64   case Counter::CounterValueReference:
65     Terms.emplace_back(C.getCounterID(), Factor);
66     break;
67   case Counter::Expression:
68     const auto &E = Expressions[C.getExpressionID()];
69     extractTerms(E.LHS, Factor, Terms);
70     extractTerms(
71         E.RHS, E.Kind == CounterExpression::Subtract ? -Factor : Factor, Terms);
72     break;
73   }
74 }
75 
76 Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
77   // Gather constant terms.
78   SmallVector<Term, 32> Terms;
79   extractTerms(ExpressionTree, +1, Terms);
80 
81   // If there are no terms, this is just a zero. The algorithm below assumes at
82   // least one term.
83   if (Terms.size() == 0)
84     return Counter::getZero();
85 
86   // Group the terms by counter ID.
87   llvm::sort(Terms, [](const Term &LHS, const Term &RHS) {
88     return LHS.CounterID < RHS.CounterID;
89   });
90 
91   // Combine terms by counter ID to eliminate counters that sum to zero.
92   auto Prev = Terms.begin();
93   for (auto I = Prev + 1, E = Terms.end(); I != E; ++I) {
94     if (I->CounterID == Prev->CounterID) {
95       Prev->Factor += I->Factor;
96       continue;
97     }
98     ++Prev;
99     *Prev = *I;
100   }
101   Terms.erase(++Prev, Terms.end());
102 
103   Counter C;
104   // Create additions. We do this before subtractions to avoid constructs like
105   // ((0 - X) + Y), as opposed to (Y - X).
106   for (auto T : Terms) {
107     if (T.Factor <= 0)
108       continue;
109     for (int I = 0; I < T.Factor; ++I)
110       if (C.isZero())
111         C = Counter::getCounter(T.CounterID);
112       else
113         C = get(CounterExpression(CounterExpression::Add, C,
114                                   Counter::getCounter(T.CounterID)));
115   }
116 
117   // Create subtractions.
118   for (auto T : Terms) {
119     if (T.Factor >= 0)
120       continue;
121     for (int I = 0; I < -T.Factor; ++I)
122       C = get(CounterExpression(CounterExpression::Subtract, C,
123                                 Counter::getCounter(T.CounterID)));
124   }
125   return C;
126 }
127 
128 Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS, bool Simplify) {
129   auto Cnt = get(CounterExpression(CounterExpression::Add, LHS, RHS));
130   return Simplify ? simplify(Cnt) : Cnt;
131 }
132 
133 Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
134                                            bool Simplify) {
135   auto Cnt = get(CounterExpression(CounterExpression::Subtract, LHS, RHS));
136   return Simplify ? simplify(Cnt) : Cnt;
137 }
138 
139 void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
140   switch (C.getKind()) {
141   case Counter::Zero:
142     OS << '0';
143     return;
144   case Counter::CounterValueReference:
145     OS << '#' << C.getCounterID();
146     break;
147   case Counter::Expression: {
148     if (C.getExpressionID() >= Expressions.size())
149       return;
150     const auto &E = Expressions[C.getExpressionID()];
151     OS << '(';
152     dump(E.LHS, OS);
153     OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
154     dump(E.RHS, OS);
155     OS << ')';
156     break;
157   }
158   }
159   if (CounterValues.empty())
160     return;
161   Expected<int64_t> Value = evaluate(C);
162   if (auto E = Value.takeError()) {
163     consumeError(std::move(E));
164     return;
165   }
166   OS << '[' << *Value << ']';
167 }
168 
169 Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
170   struct StackElem {
171     Counter ICounter;
172     int64_t LHS = 0;
173     enum {
174       KNeverVisited = 0,
175       KVisitedOnce = 1,
176       KVisitedTwice = 2,
177     } VisitCount = KNeverVisited;
178   };
179 
180   std::stack<StackElem> CounterStack;
181   CounterStack.push({C});
182 
183   int64_t LastPoppedValue;
184 
185   while (!CounterStack.empty()) {
186     StackElem &Current = CounterStack.top();
187 
188     switch (Current.ICounter.getKind()) {
189     case Counter::Zero:
190       LastPoppedValue = 0;
191       CounterStack.pop();
192       break;
193     case Counter::CounterValueReference:
194       if (Current.ICounter.getCounterID() >= CounterValues.size())
195         return errorCodeToError(errc::argument_out_of_domain);
196       LastPoppedValue = CounterValues[Current.ICounter.getCounterID()];
197       CounterStack.pop();
198       break;
199     case Counter::Expression: {
200       if (Current.ICounter.getExpressionID() >= Expressions.size())
201         return errorCodeToError(errc::argument_out_of_domain);
202       const auto &E = Expressions[Current.ICounter.getExpressionID()];
203       if (Current.VisitCount == StackElem::KNeverVisited) {
204         CounterStack.push(StackElem{E.LHS});
205         Current.VisitCount = StackElem::KVisitedOnce;
206       } else if (Current.VisitCount == StackElem::KVisitedOnce) {
207         Current.LHS = LastPoppedValue;
208         CounterStack.push(StackElem{E.RHS});
209         Current.VisitCount = StackElem::KVisitedTwice;
210       } else {
211         int64_t LHS = Current.LHS;
212         int64_t RHS = LastPoppedValue;
213         LastPoppedValue =
214             E.Kind == CounterExpression::Subtract ? LHS - RHS : LHS + RHS;
215         CounterStack.pop();
216       }
217       break;
218     }
219     }
220   }
221 
222   return LastPoppedValue;
223 }
224 
225 Expected<BitVector> CounterMappingContext::evaluateBitmap(
226     const CounterMappingRegion *MCDCDecision) const {
227   unsigned ID = MCDCDecision->MCDCParams.BitmapIdx;
228   unsigned NC = MCDCDecision->MCDCParams.NumConditions;
229   unsigned SizeInBits = llvm::alignTo(uint64_t(1) << NC, CHAR_BIT);
230   unsigned SizeInBytes = SizeInBits / CHAR_BIT;
231 
232   assert(ID + SizeInBytes <= BitmapBytes.size() && "BitmapBytes overrun");
233   ArrayRef<uint8_t> Bytes(&BitmapBytes[ID], SizeInBytes);
234 
235   // Mask each bitmap byte into the BitVector. Go in reverse so that the
236   // bitvector can just be shifted over by one byte on each iteration.
237   BitVector Result(SizeInBits, false);
238   for (auto Byte = std::rbegin(Bytes); Byte != std::rend(Bytes); ++Byte) {
239     uint32_t Data = *Byte;
240     Result <<= CHAR_BIT;
241     Result.setBitsInMask(&Data, 1);
242   }
243   return Result;
244 }
245 
246 class MCDCRecordProcessor {
247   /// A bitmap representing the executed test vectors for a boolean expression.
248   /// Each index of the bitmap corresponds to a possible test vector. An index
249   /// with a bit value of '1' indicates that the corresponding Test Vector
250   /// identified by that index was executed.
251   const BitVector &ExecutedTestVectorBitmap;
252 
253   /// Decision Region to which the ExecutedTestVectorBitmap applies.
254   const CounterMappingRegion &Region;
255 
256   /// Array of branch regions corresponding each conditions in the boolean
257   /// expression.
258   ArrayRef<const CounterMappingRegion *> Branches;
259 
260   /// Total number of conditions in the boolean expression.
261   unsigned NumConditions;
262 
263   /// Mapping of a condition ID to its corresponding branch region.
264   llvm::DenseMap<unsigned, const CounterMappingRegion *> Map;
265 
266   /// Vector used to track whether a condition is constant folded.
267   MCDCRecord::BoolVector Folded;
268 
269   /// Mapping of calculated MC/DC Independence Pairs for each condition.
270   MCDCRecord::TVPairMap IndependencePairs;
271 
272   /// Total number of possible Test Vectors for the boolean expression.
273   MCDCRecord::TestVectors TestVectors;
274 
275   /// Actual executed Test Vectors for the boolean expression, based on
276   /// ExecutedTestVectorBitmap.
277   MCDCRecord::TestVectors ExecVectors;
278 
279 public:
280   MCDCRecordProcessor(const BitVector &Bitmap,
281                       const CounterMappingRegion &Region,
282                       ArrayRef<const CounterMappingRegion *> Branches)
283       : ExecutedTestVectorBitmap(Bitmap), Region(Region), Branches(Branches),
284         NumConditions(Region.MCDCParams.NumConditions),
285         Folded(NumConditions, false), IndependencePairs(NumConditions),
286         TestVectors((size_t)1 << NumConditions) {}
287 
288 private:
289   void recordTestVector(MCDCRecord::TestVector &TV,
290                         MCDCRecord::CondState Result) {
291     // Calculate an index that is used to identify the test vector in a vector
292     // of test vectors.  This index also corresponds to the index values of an
293     // MCDC Region's bitmap (see findExecutedTestVectors()).
294     unsigned Index = 0;
295     for (auto Cond = std::rbegin(TV); Cond != std::rend(TV); ++Cond) {
296       Index <<= 1;
297       Index |= (*Cond == MCDCRecord::MCDC_True) ? 0x1 : 0x0;
298     }
299 
300     // Copy the completed test vector to the vector of testvectors.
301     TestVectors[Index] = TV;
302 
303     // The final value (T,F) is equal to the last non-dontcare state on the
304     // path (in a short-circuiting system).
305     TestVectors[Index].push_back(Result);
306   }
307 
308   void shouldCopyOffTestVectorForTruePath(MCDCRecord::TestVector &TV,
309                                           unsigned ID) {
310     // Branch regions are hashed based on an ID.
311     const CounterMappingRegion *Branch = Map[ID];
312 
313     TV[ID - 1] = MCDCRecord::MCDC_True;
314     if (Branch->MCDCParams.TrueID > 0)
315       buildTestVector(TV, Branch->MCDCParams.TrueID);
316     else
317       recordTestVector(TV, MCDCRecord::MCDC_True);
318   }
319 
320   void shouldCopyOffTestVectorForFalsePath(MCDCRecord::TestVector &TV,
321                                            unsigned ID) {
322     // Branch regions are hashed based on an ID.
323     const CounterMappingRegion *Branch = Map[ID];
324 
325     TV[ID - 1] = MCDCRecord::MCDC_False;
326     if (Branch->MCDCParams.FalseID > 0)
327       buildTestVector(TV, Branch->MCDCParams.FalseID);
328     else
329       recordTestVector(TV, MCDCRecord::MCDC_False);
330   }
331 
332   /// Starting with the base test vector, build a comprehensive list of
333   /// possible test vectors by recursively walking the branch condition IDs
334   /// provided. Once an end node is reached, record the test vector in a vector
335   /// of test vectors that can be matched against during MC/DC analysis, and
336   /// then reset the positions to 'DontCare'.
337   void buildTestVector(MCDCRecord::TestVector &TV, unsigned ID = 1) {
338     shouldCopyOffTestVectorForTruePath(TV, ID);
339     shouldCopyOffTestVectorForFalsePath(TV, ID);
340 
341     // Reset back to DontCare.
342     TV[ID - 1] = MCDCRecord::MCDC_DontCare;
343   }
344 
345   /// Walk the bits in the bitmap.  A bit set to '1' indicates that the test
346   /// vector at the corresponding index was executed during a test run.
347   void findExecutedTestVectors(const BitVector &ExecutedTestVectorBitmap) {
348     for (unsigned Idx = 0; Idx < ExecutedTestVectorBitmap.size(); ++Idx) {
349       if (ExecutedTestVectorBitmap[Idx] == 0)
350         continue;
351       assert(!TestVectors[Idx].empty() && "Test Vector doesn't exist.");
352       ExecVectors.push_back(TestVectors[Idx]);
353     }
354   }
355 
356   /// For a given condition and two executed Test Vectors, A and B, see if the
357   /// two test vectors match forming an Independence Pair for the condition.
358   /// For two test vectors to match, the following must be satisfied:
359   /// - The condition's value in each test vector must be opposite.
360   /// - The result's value in each test vector must be opposite.
361   /// - All other conditions' values must be equal or marked as "don't care".
362   bool matchTestVectors(unsigned Aidx, unsigned Bidx, unsigned ConditionIdx) {
363     const MCDCRecord::TestVector &A = ExecVectors[Aidx];
364     const MCDCRecord::TestVector &B = ExecVectors[Bidx];
365 
366     // If condition values in both A and B aren't opposites, no match.
367     // Because a value can be 0 (false), 1 (true), or -1 (DontCare), a check
368     // that "XOR != 1" will ensure that the values are opposites and that
369     // neither of them is a DontCare.
370     //  1 XOR  0 ==  1 | 0 XOR  0 ==  0 | -1 XOR  0 == -1
371     //  1 XOR  1 ==  0 | 0 XOR  1 ==  1 | -1 XOR  1 == -2
372     //  1 XOR -1 == -2 | 0 XOR -1 == -1 | -1 XOR -1 ==  0
373     if ((A[ConditionIdx] ^ B[ConditionIdx]) != 1)
374       return false;
375 
376     // If the results of both A and B aren't opposites, no match.
377     if ((A[NumConditions] ^ B[NumConditions]) != 1)
378       return false;
379 
380     for (unsigned Idx = 0; Idx < NumConditions; ++Idx) {
381       // Look for other conditions that don't match. Skip over the given
382       // Condition as well as any conditions marked as "don't care".
383       const auto ARecordTyForCond = A[Idx];
384       const auto BRecordTyForCond = B[Idx];
385       if (Idx == ConditionIdx ||
386           ARecordTyForCond == MCDCRecord::MCDC_DontCare ||
387           BRecordTyForCond == MCDCRecord::MCDC_DontCare)
388         continue;
389 
390       // If there is a condition mismatch with any of the other conditions,
391       // there is no match for the test vectors.
392       if (ARecordTyForCond != BRecordTyForCond)
393         return false;
394     }
395 
396     // Otherwise, match.
397     return true;
398   }
399 
400   /// Find all possible Independence Pairs for a boolean expression given its
401   /// executed Test Vectors.  This process involves looking at each condition
402   /// and attempting to find two Test Vectors that "match", giving us a pair.
403   void findIndependencePairs() {
404     unsigned NumTVs = ExecVectors.size();
405 
406     // For each condition.
407     for (unsigned C = 0; C < NumConditions; ++C) {
408       bool PairFound = false;
409 
410       // For each executed test vector.
411       for (unsigned I = 0; !PairFound && I < NumTVs; ++I) {
412         // Compared to every other executed test vector.
413         for (unsigned J = 0; !PairFound && J < NumTVs; ++J) {
414           if (I == J)
415             continue;
416 
417           // If a matching pair of vectors is found, record them.
418           if ((PairFound = matchTestVectors(I, J, C)))
419             IndependencePairs[C] = std::make_pair(I + 1, J + 1);
420         }
421       }
422     }
423   }
424 
425 public:
426   /// Process the MC/DC Record in order to produce a result for a boolean
427   /// expression. This process includes tracking the conditions that comprise
428   /// the decision region, calculating the list of all possible test vectors,
429   /// marking the executed test vectors, and then finding an Independence Pair
430   /// out of the executed test vectors for each condition in the boolean
431   /// expression. A condition is tracked to ensure that its ID can be mapped to
432   /// its ordinal position in the boolean expression. The condition's source
433   /// location is also tracked, as well as whether it is constant folded (in
434   /// which case it is excuded from the metric).
435   MCDCRecord processMCDCRecord() {
436     unsigned I = 0;
437     MCDCRecord::CondIDMap PosToID;
438     MCDCRecord::LineColPairMap CondLoc;
439 
440     // Walk the Record's BranchRegions (representing Conditions) in order to:
441     // - Hash the condition based on its corresponding ID. This will be used to
442     //   calculate the test vectors.
443     // - Keep a map of the condition's ordinal position (1, 2, 3, 4) to its
444     //   actual ID.  This will be used to visualize the conditions in the
445     //   correct order.
446     // - Keep track of the condition source location. This will be used to
447     //   visualize where the condition is.
448     // - Record whether the condition is constant folded so that we exclude it
449     //   from being measured.
450     for (const auto *B : Branches) {
451       Map[B->MCDCParams.ID] = B;
452       PosToID[I] = B->MCDCParams.ID - 1;
453       CondLoc[I] = B->startLoc();
454       Folded[I++] = (B->Count.isZero() && B->FalseCount.isZero());
455     }
456 
457     // Initialize a base test vector as 'DontCare'.
458     MCDCRecord::TestVector TV(NumConditions, MCDCRecord::MCDC_DontCare);
459 
460     // Use the base test vector to build the list of all possible test vectors.
461     buildTestVector(TV);
462 
463     // Using Profile Bitmap from runtime, mark the executed test vectors.
464     findExecutedTestVectors(ExecutedTestVectorBitmap);
465 
466     // Compare executed test vectors against each other to find an independence
467     // pairs for each condition.  This processing takes the most time.
468     findIndependencePairs();
469 
470     // Record Test vectors, executed vectors, and independence pairs.
471     MCDCRecord Res(Region, ExecVectors, IndependencePairs, Folded, PosToID,
472                    CondLoc);
473     return Res;
474   }
475 };
476 
477 Expected<MCDCRecord> CounterMappingContext::evaluateMCDCRegion(
478     const CounterMappingRegion &Region,
479     const BitVector &ExecutedTestVectorBitmap,
480     ArrayRef<const CounterMappingRegion *> Branches) {
481 
482   MCDCRecordProcessor MCDCProcessor(ExecutedTestVectorBitmap, Region, Branches);
483   return MCDCProcessor.processMCDCRecord();
484 }
485 
486 unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const {
487   struct StackElem {
488     Counter ICounter;
489     int64_t LHS = 0;
490     enum {
491       KNeverVisited = 0,
492       KVisitedOnce = 1,
493       KVisitedTwice = 2,
494     } VisitCount = KNeverVisited;
495   };
496 
497   std::stack<StackElem> CounterStack;
498   CounterStack.push({C});
499 
500   int64_t LastPoppedValue;
501 
502   while (!CounterStack.empty()) {
503     StackElem &Current = CounterStack.top();
504 
505     switch (Current.ICounter.getKind()) {
506     case Counter::Zero:
507       LastPoppedValue = 0;
508       CounterStack.pop();
509       break;
510     case Counter::CounterValueReference:
511       LastPoppedValue = Current.ICounter.getCounterID();
512       CounterStack.pop();
513       break;
514     case Counter::Expression: {
515       if (Current.ICounter.getExpressionID() >= Expressions.size()) {
516         LastPoppedValue = 0;
517         CounterStack.pop();
518       } else {
519         const auto &E = Expressions[Current.ICounter.getExpressionID()];
520         if (Current.VisitCount == StackElem::KNeverVisited) {
521           CounterStack.push(StackElem{E.LHS});
522           Current.VisitCount = StackElem::KVisitedOnce;
523         } else if (Current.VisitCount == StackElem::KVisitedOnce) {
524           Current.LHS = LastPoppedValue;
525           CounterStack.push(StackElem{E.RHS});
526           Current.VisitCount = StackElem::KVisitedTwice;
527         } else {
528           int64_t LHS = Current.LHS;
529           int64_t RHS = LastPoppedValue;
530           LastPoppedValue = std::max(LHS, RHS);
531           CounterStack.pop();
532         }
533       }
534       break;
535     }
536     }
537   }
538 
539   return LastPoppedValue;
540 }
541 
542 void FunctionRecordIterator::skipOtherFiles() {
543   while (Current != Records.end() && !Filename.empty() &&
544          Filename != Current->Filenames[0])
545     ++Current;
546   if (Current == Records.end())
547     *this = FunctionRecordIterator();
548 }
549 
550 ArrayRef<unsigned> CoverageMapping::getImpreciseRecordIndicesForFilename(
551     StringRef Filename) const {
552   size_t FilenameHash = hash_value(Filename);
553   auto RecordIt = FilenameHash2RecordIndices.find(FilenameHash);
554   if (RecordIt == FilenameHash2RecordIndices.end())
555     return {};
556   return RecordIt->second;
557 }
558 
559 static unsigned getMaxCounterID(const CounterMappingContext &Ctx,
560                                 const CoverageMappingRecord &Record) {
561   unsigned MaxCounterID = 0;
562   for (const auto &Region : Record.MappingRegions) {
563     MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count));
564   }
565   return MaxCounterID;
566 }
567 
568 static unsigned getMaxBitmapSize(const CounterMappingContext &Ctx,
569                                  const CoverageMappingRecord &Record) {
570   unsigned MaxBitmapID = 0;
571   unsigned NumConditions = 0;
572   // Scan max(BitmapIdx).
573   // Note that `<=` is used insted of `<`, because `BitmapIdx == 0` is valid
574   // and `MaxBitmapID is `unsigned`. `BitmapIdx` is unique in the record.
575   for (const auto &Region : reverse(Record.MappingRegions)) {
576     if (Region.Kind == CounterMappingRegion::MCDCDecisionRegion &&
577         MaxBitmapID <= Region.MCDCParams.BitmapIdx) {
578       MaxBitmapID = Region.MCDCParams.BitmapIdx;
579       NumConditions = Region.MCDCParams.NumConditions;
580     }
581   }
582   unsigned SizeInBits = llvm::alignTo(uint64_t(1) << NumConditions, CHAR_BIT);
583   return MaxBitmapID + (SizeInBits / CHAR_BIT);
584 }
585 
586 Error CoverageMapping::loadFunctionRecord(
587     const CoverageMappingRecord &Record,
588     IndexedInstrProfReader &ProfileReader) {
589   StringRef OrigFuncName = Record.FunctionName;
590   if (OrigFuncName.empty())
591     return make_error<CoverageMapError>(coveragemap_error::malformed,
592                                         "record function name is empty");
593 
594   if (Record.Filenames.empty())
595     OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
596   else
597     OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
598 
599   CounterMappingContext Ctx(Record.Expressions);
600 
601   std::vector<uint64_t> Counts;
602   if (Error E = ProfileReader.getFunctionCounts(Record.FunctionName,
603                                                 Record.FunctionHash, Counts)) {
604     instrprof_error IPE = std::get<0>(InstrProfError::take(std::move(E)));
605     if (IPE == instrprof_error::hash_mismatch) {
606       FuncHashMismatches.emplace_back(std::string(Record.FunctionName),
607                                       Record.FunctionHash);
608       return Error::success();
609     }
610     if (IPE != instrprof_error::unknown_function)
611       return make_error<InstrProfError>(IPE);
612     Counts.assign(getMaxCounterID(Ctx, Record) + 1, 0);
613   }
614   Ctx.setCounts(Counts);
615 
616   std::vector<uint8_t> BitmapBytes;
617   if (Error E = ProfileReader.getFunctionBitmapBytes(
618           Record.FunctionName, Record.FunctionHash, BitmapBytes)) {
619     instrprof_error IPE = std::get<0>(InstrProfError::take(std::move(E)));
620     if (IPE == instrprof_error::hash_mismatch) {
621       FuncHashMismatches.emplace_back(std::string(Record.FunctionName),
622                                       Record.FunctionHash);
623       return Error::success();
624     }
625     if (IPE != instrprof_error::unknown_function)
626       return make_error<InstrProfError>(IPE);
627     BitmapBytes.assign(getMaxBitmapSize(Ctx, Record) + 1, 0);
628   }
629   Ctx.setBitmapBytes(BitmapBytes);
630 
631   assert(!Record.MappingRegions.empty() && "Function has no regions");
632 
633   // This coverage record is a zero region for a function that's unused in
634   // some TU, but used in a different TU. Ignore it. The coverage maps from the
635   // the other TU will either be loaded (providing full region counts) or they
636   // won't (in which case we don't unintuitively report functions as uncovered
637   // when they have non-zero counts in the profile).
638   if (Record.MappingRegions.size() == 1 &&
639       Record.MappingRegions[0].Count.isZero() && Counts[0] > 0)
640     return Error::success();
641 
642   unsigned NumConds = 0;
643   const CounterMappingRegion *MCDCDecision;
644   std::vector<const CounterMappingRegion *> MCDCBranches;
645 
646   FunctionRecord Function(OrigFuncName, Record.Filenames);
647   for (const auto &Region : Record.MappingRegions) {
648     // If an MCDCDecisionRegion is seen, track the BranchRegions that follow
649     // it according to Region.NumConditions.
650     if (Region.Kind == CounterMappingRegion::MCDCDecisionRegion) {
651       assert(NumConds == 0);
652       MCDCDecision = &Region;
653       NumConds = Region.MCDCParams.NumConditions;
654       continue;
655     }
656     Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
657     if (auto E = ExecutionCount.takeError()) {
658       consumeError(std::move(E));
659       return Error::success();
660     }
661     Expected<int64_t> AltExecutionCount = Ctx.evaluate(Region.FalseCount);
662     if (auto E = AltExecutionCount.takeError()) {
663       consumeError(std::move(E));
664       return Error::success();
665     }
666     Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount);
667 
668     // If a MCDCDecisionRegion was seen, store the BranchRegions that
669     // correspond to it in a vector, according to the number of conditions
670     // recorded for the region (tracked by NumConds).
671     if (NumConds > 0 && Region.Kind == CounterMappingRegion::MCDCBranchRegion) {
672       MCDCBranches.push_back(&Region);
673 
674       // As we move through all of the MCDCBranchRegions that follow the
675       // MCDCDecisionRegion, decrement NumConds to make sure we account for
676       // them all before we calculate the bitmap of executed test vectors.
677       if (--NumConds == 0) {
678         // Evaluating the test vector bitmap for the decision region entails
679         // calculating precisely what bits are pertinent to this region alone.
680         // This is calculated based on the recorded offset into the global
681         // profile bitmap; the length is calculated based on the recorded
682         // number of conditions.
683         Expected<BitVector> ExecutedTestVectorBitmap =
684             Ctx.evaluateBitmap(MCDCDecision);
685         if (auto E = ExecutedTestVectorBitmap.takeError()) {
686           consumeError(std::move(E));
687           return Error::success();
688         }
689 
690         // Since the bitmap identifies the executed test vectors for an MC/DC
691         // DecisionRegion, all of the information is now available to process.
692         // This is where the bulk of the MC/DC progressing takes place.
693         Expected<MCDCRecord> Record = Ctx.evaluateMCDCRegion(
694             *MCDCDecision, *ExecutedTestVectorBitmap, MCDCBranches);
695         if (auto E = Record.takeError()) {
696           consumeError(std::move(E));
697           return Error::success();
698         }
699 
700         // Save the MC/DC Record so that it can be visualized later.
701         Function.pushMCDCRecord(*Record);
702         MCDCBranches.clear();
703       }
704     }
705   }
706 
707   // Don't create records for (filenames, function) pairs we've already seen.
708   auto FilenamesHash = hash_combine_range(Record.Filenames.begin(),
709                                           Record.Filenames.end());
710   if (!RecordProvenance[FilenamesHash].insert(hash_value(OrigFuncName)).second)
711     return Error::success();
712 
713   Functions.push_back(std::move(Function));
714 
715   // Performance optimization: keep track of the indices of the function records
716   // which correspond to each filename. This can be used to substantially speed
717   // up queries for coverage info in a file.
718   unsigned RecordIndex = Functions.size() - 1;
719   for (StringRef Filename : Record.Filenames) {
720     auto &RecordIndices = FilenameHash2RecordIndices[hash_value(Filename)];
721     // Note that there may be duplicates in the filename set for a function
722     // record, because of e.g. macro expansions in the function in which both
723     // the macro and the function are defined in the same file.
724     if (RecordIndices.empty() || RecordIndices.back() != RecordIndex)
725       RecordIndices.push_back(RecordIndex);
726   }
727 
728   return Error::success();
729 }
730 
731 // This function is for memory optimization by shortening the lifetimes
732 // of CoverageMappingReader instances.
733 Error CoverageMapping::loadFromReaders(
734     ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
735     IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage) {
736   for (const auto &CoverageReader : CoverageReaders) {
737     for (auto RecordOrErr : *CoverageReader) {
738       if (Error E = RecordOrErr.takeError())
739         return E;
740       const auto &Record = *RecordOrErr;
741       if (Error E = Coverage.loadFunctionRecord(Record, ProfileReader))
742         return E;
743     }
744   }
745   return Error::success();
746 }
747 
748 Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
749     ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
750     IndexedInstrProfReader &ProfileReader) {
751   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
752   if (Error E = loadFromReaders(CoverageReaders, ProfileReader, *Coverage))
753     return std::move(E);
754   return std::move(Coverage);
755 }
756 
757 // If E is a no_data_found error, returns success. Otherwise returns E.
758 static Error handleMaybeNoDataFoundError(Error E) {
759   return handleErrors(
760       std::move(E), [](const CoverageMapError &CME) {
761         if (CME.get() == coveragemap_error::no_data_found)
762           return static_cast<Error>(Error::success());
763         return make_error<CoverageMapError>(CME.get(), CME.getMessage());
764       });
765 }
766 
767 Error CoverageMapping::loadFromFile(
768     StringRef Filename, StringRef Arch, StringRef CompilationDir,
769     IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage,
770     bool &DataFound, SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
771   auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(
772       Filename, /*IsText=*/false, /*RequiresNullTerminator=*/false);
773   if (std::error_code EC = CovMappingBufOrErr.getError())
774     return createFileError(Filename, errorCodeToError(EC));
775   MemoryBufferRef CovMappingBufRef =
776       CovMappingBufOrErr.get()->getMemBufferRef();
777   SmallVector<std::unique_ptr<MemoryBuffer>, 4> Buffers;
778 
779   SmallVector<object::BuildIDRef> BinaryIDs;
780   auto CoverageReadersOrErr = BinaryCoverageReader::create(
781       CovMappingBufRef, Arch, Buffers, CompilationDir,
782       FoundBinaryIDs ? &BinaryIDs : nullptr);
783   if (Error E = CoverageReadersOrErr.takeError()) {
784     E = handleMaybeNoDataFoundError(std::move(E));
785     if (E)
786       return createFileError(Filename, std::move(E));
787     return E;
788   }
789 
790   SmallVector<std::unique_ptr<CoverageMappingReader>, 4> Readers;
791   for (auto &Reader : CoverageReadersOrErr.get())
792     Readers.push_back(std::move(Reader));
793   if (FoundBinaryIDs && !Readers.empty()) {
794     llvm::append_range(*FoundBinaryIDs,
795                        llvm::map_range(BinaryIDs, [](object::BuildIDRef BID) {
796                          return object::BuildID(BID);
797                        }));
798   }
799   DataFound |= !Readers.empty();
800   if (Error E = loadFromReaders(Readers, ProfileReader, Coverage))
801     return createFileError(Filename, std::move(E));
802   return Error::success();
803 }
804 
805 Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
806     ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
807     vfs::FileSystem &FS, ArrayRef<StringRef> Arches, StringRef CompilationDir,
808     const object::BuildIDFetcher *BIDFetcher, bool CheckBinaryIDs) {
809   auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename, FS);
810   if (Error E = ProfileReaderOrErr.takeError())
811     return createFileError(ProfileFilename, std::move(E));
812   auto ProfileReader = std::move(ProfileReaderOrErr.get());
813   auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
814   bool DataFound = false;
815 
816   auto GetArch = [&](size_t Idx) {
817     if (Arches.empty())
818       return StringRef();
819     if (Arches.size() == 1)
820       return Arches.front();
821     return Arches[Idx];
822   };
823 
824   SmallVector<object::BuildID> FoundBinaryIDs;
825   for (const auto &File : llvm::enumerate(ObjectFilenames)) {
826     if (Error E =
827             loadFromFile(File.value(), GetArch(File.index()), CompilationDir,
828                          *ProfileReader, *Coverage, DataFound, &FoundBinaryIDs))
829       return std::move(E);
830   }
831 
832   if (BIDFetcher) {
833     std::vector<object::BuildID> ProfileBinaryIDs;
834     if (Error E = ProfileReader->readBinaryIds(ProfileBinaryIDs))
835       return createFileError(ProfileFilename, std::move(E));
836 
837     SmallVector<object::BuildIDRef> BinaryIDsToFetch;
838     if (!ProfileBinaryIDs.empty()) {
839       const auto &Compare = [](object::BuildIDRef A, object::BuildIDRef B) {
840         return std::lexicographical_compare(A.begin(), A.end(), B.begin(),
841                                             B.end());
842       };
843       llvm::sort(FoundBinaryIDs, Compare);
844       std::set_difference(
845           ProfileBinaryIDs.begin(), ProfileBinaryIDs.end(),
846           FoundBinaryIDs.begin(), FoundBinaryIDs.end(),
847           std::inserter(BinaryIDsToFetch, BinaryIDsToFetch.end()), Compare);
848     }
849 
850     for (object::BuildIDRef BinaryID : BinaryIDsToFetch) {
851       std::optional<std::string> PathOpt = BIDFetcher->fetch(BinaryID);
852       if (PathOpt) {
853         std::string Path = std::move(*PathOpt);
854         StringRef Arch = Arches.size() == 1 ? Arches.front() : StringRef();
855         if (Error E = loadFromFile(Path, Arch, CompilationDir, *ProfileReader,
856                                   *Coverage, DataFound))
857           return std::move(E);
858       } else if (CheckBinaryIDs) {
859         return createFileError(
860             ProfileFilename,
861             createStringError(errc::no_such_file_or_directory,
862                               "Missing binary ID: " +
863                                   llvm::toHex(BinaryID, /*LowerCase=*/true)));
864       }
865     }
866   }
867 
868   if (!DataFound)
869     return createFileError(
870         join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "),
871         make_error<CoverageMapError>(coveragemap_error::no_data_found));
872   return std::move(Coverage);
873 }
874 
875 namespace {
876 
877 /// Distributes functions into instantiation sets.
878 ///
879 /// An instantiation set is a collection of functions that have the same source
880 /// code, ie, template functions specializations.
881 class FunctionInstantiationSetCollector {
882   using MapT = std::map<LineColPair, std::vector<const FunctionRecord *>>;
883   MapT InstantiatedFunctions;
884 
885 public:
886   void insert(const FunctionRecord &Function, unsigned FileID) {
887     auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
888     while (I != E && I->FileID != FileID)
889       ++I;
890     assert(I != E && "function does not cover the given file");
891     auto &Functions = InstantiatedFunctions[I->startLoc()];
892     Functions.push_back(&Function);
893   }
894 
895   MapT::iterator begin() { return InstantiatedFunctions.begin(); }
896   MapT::iterator end() { return InstantiatedFunctions.end(); }
897 };
898 
899 class SegmentBuilder {
900   std::vector<CoverageSegment> &Segments;
901   SmallVector<const CountedRegion *, 8> ActiveRegions;
902 
903   SegmentBuilder(std::vector<CoverageSegment> &Segments) : Segments(Segments) {}
904 
905   /// Emit a segment with the count from \p Region starting at \p StartLoc.
906   //
907   /// \p IsRegionEntry: The segment is at the start of a new non-gap region.
908   /// \p EmitSkippedRegion: The segment must be emitted as a skipped region.
909   void startSegment(const CountedRegion &Region, LineColPair StartLoc,
910                     bool IsRegionEntry, bool EmitSkippedRegion = false) {
911     bool HasCount = !EmitSkippedRegion &&
912                     (Region.Kind != CounterMappingRegion::SkippedRegion);
913 
914     // If the new segment wouldn't affect coverage rendering, skip it.
915     if (!Segments.empty() && !IsRegionEntry && !EmitSkippedRegion) {
916       const auto &Last = Segments.back();
917       if (Last.HasCount == HasCount && Last.Count == Region.ExecutionCount &&
918           !Last.IsRegionEntry)
919         return;
920     }
921 
922     if (HasCount)
923       Segments.emplace_back(StartLoc.first, StartLoc.second,
924                             Region.ExecutionCount, IsRegionEntry,
925                             Region.Kind == CounterMappingRegion::GapRegion);
926     else
927       Segments.emplace_back(StartLoc.first, StartLoc.second, IsRegionEntry);
928 
929     LLVM_DEBUG({
930       const auto &Last = Segments.back();
931       dbgs() << "Segment at " << Last.Line << ":" << Last.Col
932              << " (count = " << Last.Count << ")"
933              << (Last.IsRegionEntry ? ", RegionEntry" : "")
934              << (!Last.HasCount ? ", Skipped" : "")
935              << (Last.IsGapRegion ? ", Gap" : "") << "\n";
936     });
937   }
938 
939   /// Emit segments for active regions which end before \p Loc.
940   ///
941   /// \p Loc: The start location of the next region. If std::nullopt, all active
942   /// regions are completed.
943   /// \p FirstCompletedRegion: Index of the first completed region.
944   void completeRegionsUntil(std::optional<LineColPair> Loc,
945                             unsigned FirstCompletedRegion) {
946     // Sort the completed regions by end location. This makes it simple to
947     // emit closing segments in sorted order.
948     auto CompletedRegionsIt = ActiveRegions.begin() + FirstCompletedRegion;
949     std::stable_sort(CompletedRegionsIt, ActiveRegions.end(),
950                       [](const CountedRegion *L, const CountedRegion *R) {
951                         return L->endLoc() < R->endLoc();
952                       });
953 
954     // Emit segments for all completed regions.
955     for (unsigned I = FirstCompletedRegion + 1, E = ActiveRegions.size(); I < E;
956          ++I) {
957       const auto *CompletedRegion = ActiveRegions[I];
958       assert((!Loc || CompletedRegion->endLoc() <= *Loc) &&
959              "Completed region ends after start of new region");
960 
961       const auto *PrevCompletedRegion = ActiveRegions[I - 1];
962       auto CompletedSegmentLoc = PrevCompletedRegion->endLoc();
963 
964       // Don't emit any more segments if they start where the new region begins.
965       if (Loc && CompletedSegmentLoc == *Loc)
966         break;
967 
968       // Don't emit a segment if the next completed region ends at the same
969       // location as this one.
970       if (CompletedSegmentLoc == CompletedRegion->endLoc())
971         continue;
972 
973       // Use the count from the last completed region which ends at this loc.
974       for (unsigned J = I + 1; J < E; ++J)
975         if (CompletedRegion->endLoc() == ActiveRegions[J]->endLoc())
976           CompletedRegion = ActiveRegions[J];
977 
978       startSegment(*CompletedRegion, CompletedSegmentLoc, false);
979     }
980 
981     auto Last = ActiveRegions.back();
982     if (FirstCompletedRegion && Last->endLoc() != *Loc) {
983       // If there's a gap after the end of the last completed region and the
984       // start of the new region, use the last active region to fill the gap.
985       startSegment(*ActiveRegions[FirstCompletedRegion - 1], Last->endLoc(),
986                    false);
987     } else if (!FirstCompletedRegion && (!Loc || *Loc != Last->endLoc())) {
988       // Emit a skipped segment if there are no more active regions. This
989       // ensures that gaps between functions are marked correctly.
990       startSegment(*Last, Last->endLoc(), false, true);
991     }
992 
993     // Pop the completed regions.
994     ActiveRegions.erase(CompletedRegionsIt, ActiveRegions.end());
995   }
996 
997   void buildSegmentsImpl(ArrayRef<CountedRegion> Regions) {
998     for (const auto &CR : enumerate(Regions)) {
999       auto CurStartLoc = CR.value().startLoc();
1000 
1001       // Active regions which end before the current region need to be popped.
1002       auto CompletedRegions =
1003           std::stable_partition(ActiveRegions.begin(), ActiveRegions.end(),
1004                                 [&](const CountedRegion *Region) {
1005                                   return !(Region->endLoc() <= CurStartLoc);
1006                                 });
1007       if (CompletedRegions != ActiveRegions.end()) {
1008         unsigned FirstCompletedRegion =
1009             std::distance(ActiveRegions.begin(), CompletedRegions);
1010         completeRegionsUntil(CurStartLoc, FirstCompletedRegion);
1011       }
1012 
1013       bool GapRegion = CR.value().Kind == CounterMappingRegion::GapRegion;
1014 
1015       // Try to emit a segment for the current region.
1016       if (CurStartLoc == CR.value().endLoc()) {
1017         // Avoid making zero-length regions active. If it's the last region,
1018         // emit a skipped segment. Otherwise use its predecessor's count.
1019         const bool Skipped =
1020             (CR.index() + 1) == Regions.size() ||
1021             CR.value().Kind == CounterMappingRegion::SkippedRegion;
1022         startSegment(ActiveRegions.empty() ? CR.value() : *ActiveRegions.back(),
1023                      CurStartLoc, !GapRegion, Skipped);
1024         // If it is skipped segment, create a segment with last pushed
1025         // regions's count at CurStartLoc.
1026         if (Skipped && !ActiveRegions.empty())
1027           startSegment(*ActiveRegions.back(), CurStartLoc, false);
1028         continue;
1029       }
1030       if (CR.index() + 1 == Regions.size() ||
1031           CurStartLoc != Regions[CR.index() + 1].startLoc()) {
1032         // Emit a segment if the next region doesn't start at the same location
1033         // as this one.
1034         startSegment(CR.value(), CurStartLoc, !GapRegion);
1035       }
1036 
1037       // This region is active (i.e not completed).
1038       ActiveRegions.push_back(&CR.value());
1039     }
1040 
1041     // Complete any remaining active regions.
1042     if (!ActiveRegions.empty())
1043       completeRegionsUntil(std::nullopt, 0);
1044   }
1045 
1046   /// Sort a nested sequence of regions from a single file.
1047   static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) {
1048     llvm::sort(Regions, [](const CountedRegion &LHS, const CountedRegion &RHS) {
1049       if (LHS.startLoc() != RHS.startLoc())
1050         return LHS.startLoc() < RHS.startLoc();
1051       if (LHS.endLoc() != RHS.endLoc())
1052         // When LHS completely contains RHS, we sort LHS first.
1053         return RHS.endLoc() < LHS.endLoc();
1054       // If LHS and RHS cover the same area, we need to sort them according
1055       // to their kinds so that the most suitable region will become "active"
1056       // in combineRegions(). Because we accumulate counter values only from
1057       // regions of the same kind as the first region of the area, prefer
1058       // CodeRegion to ExpansionRegion and ExpansionRegion to SkippedRegion.
1059       static_assert(CounterMappingRegion::CodeRegion <
1060                             CounterMappingRegion::ExpansionRegion &&
1061                         CounterMappingRegion::ExpansionRegion <
1062                             CounterMappingRegion::SkippedRegion,
1063                     "Unexpected order of region kind values");
1064       return LHS.Kind < RHS.Kind;
1065     });
1066   }
1067 
1068   /// Combine counts of regions which cover the same area.
1069   static ArrayRef<CountedRegion>
1070   combineRegions(MutableArrayRef<CountedRegion> Regions) {
1071     if (Regions.empty())
1072       return Regions;
1073     auto Active = Regions.begin();
1074     auto End = Regions.end();
1075     for (auto I = Regions.begin() + 1; I != End; ++I) {
1076       if (Active->startLoc() != I->startLoc() ||
1077           Active->endLoc() != I->endLoc()) {
1078         // Shift to the next region.
1079         ++Active;
1080         if (Active != I)
1081           *Active = *I;
1082         continue;
1083       }
1084       // Merge duplicate region.
1085       // If CodeRegions and ExpansionRegions cover the same area, it's probably
1086       // a macro which is fully expanded to another macro. In that case, we need
1087       // to accumulate counts only from CodeRegions, or else the area will be
1088       // counted twice.
1089       // On the other hand, a macro may have a nested macro in its body. If the
1090       // outer macro is used several times, the ExpansionRegion for the nested
1091       // macro will also be added several times. These ExpansionRegions cover
1092       // the same source locations and have to be combined to reach the correct
1093       // value for that area.
1094       // We add counts of the regions of the same kind as the active region
1095       // to handle the both situations.
1096       if (I->Kind == Active->Kind)
1097         Active->ExecutionCount += I->ExecutionCount;
1098     }
1099     return Regions.drop_back(std::distance(++Active, End));
1100   }
1101 
1102 public:
1103   /// Build a sorted list of CoverageSegments from a list of Regions.
1104   static std::vector<CoverageSegment>
1105   buildSegments(MutableArrayRef<CountedRegion> Regions) {
1106     std::vector<CoverageSegment> Segments;
1107     SegmentBuilder Builder(Segments);
1108 
1109     sortNestedRegions(Regions);
1110     ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
1111 
1112     LLVM_DEBUG({
1113       dbgs() << "Combined regions:\n";
1114       for (const auto &CR : CombinedRegions)
1115         dbgs() << "  " << CR.LineStart << ":" << CR.ColumnStart << " -> "
1116                << CR.LineEnd << ":" << CR.ColumnEnd
1117                << " (count=" << CR.ExecutionCount << ")\n";
1118     });
1119 
1120     Builder.buildSegmentsImpl(CombinedRegions);
1121 
1122 #ifndef NDEBUG
1123     for (unsigned I = 1, E = Segments.size(); I < E; ++I) {
1124       const auto &L = Segments[I - 1];
1125       const auto &R = Segments[I];
1126       if (!(L.Line < R.Line) && !(L.Line == R.Line && L.Col < R.Col)) {
1127         if (L.Line == R.Line && L.Col == R.Col && !L.HasCount)
1128           continue;
1129         LLVM_DEBUG(dbgs() << " ! Segment " << L.Line << ":" << L.Col
1130                           << " followed by " << R.Line << ":" << R.Col << "\n");
1131         assert(false && "Coverage segments not unique or sorted");
1132       }
1133     }
1134 #endif
1135 
1136     return Segments;
1137   }
1138 };
1139 
1140 } // end anonymous namespace
1141 
1142 std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
1143   std::vector<StringRef> Filenames;
1144   for (const auto &Function : getCoveredFunctions())
1145     llvm::append_range(Filenames, Function.Filenames);
1146   llvm::sort(Filenames);
1147   auto Last = std::unique(Filenames.begin(), Filenames.end());
1148   Filenames.erase(Last, Filenames.end());
1149   return Filenames;
1150 }
1151 
1152 static SmallBitVector gatherFileIDs(StringRef SourceFile,
1153                                     const FunctionRecord &Function) {
1154   SmallBitVector FilenameEquivalence(Function.Filenames.size(), false);
1155   for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
1156     if (SourceFile == Function.Filenames[I])
1157       FilenameEquivalence[I] = true;
1158   return FilenameEquivalence;
1159 }
1160 
1161 /// Return the ID of the file where the definition of the function is located.
1162 static std::optional<unsigned>
1163 findMainViewFileID(const FunctionRecord &Function) {
1164   SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
1165   for (const auto &CR : Function.CountedRegions)
1166     if (CR.Kind == CounterMappingRegion::ExpansionRegion)
1167       IsNotExpandedFile[CR.ExpandedFileID] = false;
1168   int I = IsNotExpandedFile.find_first();
1169   if (I == -1)
1170     return std::nullopt;
1171   return I;
1172 }
1173 
1174 /// Check if SourceFile is the file that contains the definition of
1175 /// the Function. Return the ID of the file in that case or std::nullopt
1176 /// otherwise.
1177 static std::optional<unsigned>
1178 findMainViewFileID(StringRef SourceFile, const FunctionRecord &Function) {
1179   std::optional<unsigned> I = findMainViewFileID(Function);
1180   if (I && SourceFile == Function.Filenames[*I])
1181     return I;
1182   return std::nullopt;
1183 }
1184 
1185 static bool isExpansion(const CountedRegion &R, unsigned FileID) {
1186   return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
1187 }
1188 
1189 CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
1190   CoverageData FileCoverage(Filename);
1191   std::vector<CountedRegion> Regions;
1192 
1193   // Look up the function records in the given file. Due to hash collisions on
1194   // the filename, we may get back some records that are not in the file.
1195   ArrayRef<unsigned> RecordIndices =
1196       getImpreciseRecordIndicesForFilename(Filename);
1197   for (unsigned RecordIndex : RecordIndices) {
1198     const FunctionRecord &Function = Functions[RecordIndex];
1199     auto MainFileID = findMainViewFileID(Filename, Function);
1200     auto FileIDs = gatherFileIDs(Filename, Function);
1201     for (const auto &CR : Function.CountedRegions)
1202       if (FileIDs.test(CR.FileID)) {
1203         Regions.push_back(CR);
1204         if (MainFileID && isExpansion(CR, *MainFileID))
1205           FileCoverage.Expansions.emplace_back(CR, Function);
1206       }
1207     // Capture branch regions specific to the function (excluding expansions).
1208     for (const auto &CR : Function.CountedBranchRegions)
1209       if (FileIDs.test(CR.FileID) && (CR.FileID == CR.ExpandedFileID))
1210         FileCoverage.BranchRegions.push_back(CR);
1211     // Capture MCDC records specific to the function.
1212     for (const auto &MR : Function.MCDCRecords)
1213       if (FileIDs.test(MR.getDecisionRegion().FileID))
1214         FileCoverage.MCDCRecords.push_back(MR);
1215   }
1216 
1217   LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
1218   FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
1219 
1220   return FileCoverage;
1221 }
1222 
1223 std::vector<InstantiationGroup>
1224 CoverageMapping::getInstantiationGroups(StringRef Filename) const {
1225   FunctionInstantiationSetCollector InstantiationSetCollector;
1226   // Look up the function records in the given file. Due to hash collisions on
1227   // the filename, we may get back some records that are not in the file.
1228   ArrayRef<unsigned> RecordIndices =
1229       getImpreciseRecordIndicesForFilename(Filename);
1230   for (unsigned RecordIndex : RecordIndices) {
1231     const FunctionRecord &Function = Functions[RecordIndex];
1232     auto MainFileID = findMainViewFileID(Filename, Function);
1233     if (!MainFileID)
1234       continue;
1235     InstantiationSetCollector.insert(Function, *MainFileID);
1236   }
1237 
1238   std::vector<InstantiationGroup> Result;
1239   for (auto &InstantiationSet : InstantiationSetCollector) {
1240     InstantiationGroup IG{InstantiationSet.first.first,
1241                           InstantiationSet.first.second,
1242                           std::move(InstantiationSet.second)};
1243     Result.emplace_back(std::move(IG));
1244   }
1245   return Result;
1246 }
1247 
1248 CoverageData
1249 CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const {
1250   auto MainFileID = findMainViewFileID(Function);
1251   if (!MainFileID)
1252     return CoverageData();
1253 
1254   CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
1255   std::vector<CountedRegion> Regions;
1256   for (const auto &CR : Function.CountedRegions)
1257     if (CR.FileID == *MainFileID) {
1258       Regions.push_back(CR);
1259       if (isExpansion(CR, *MainFileID))
1260         FunctionCoverage.Expansions.emplace_back(CR, Function);
1261     }
1262   // Capture branch regions specific to the function (excluding expansions).
1263   for (const auto &CR : Function.CountedBranchRegions)
1264     if (CR.FileID == *MainFileID)
1265       FunctionCoverage.BranchRegions.push_back(CR);
1266 
1267   // Capture MCDC records specific to the function.
1268   for (const auto &MR : Function.MCDCRecords)
1269     if (MR.getDecisionRegion().FileID == *MainFileID)
1270       FunctionCoverage.MCDCRecords.push_back(MR);
1271 
1272   LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name
1273                     << "\n");
1274   FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
1275 
1276   return FunctionCoverage;
1277 }
1278 
1279 CoverageData CoverageMapping::getCoverageForExpansion(
1280     const ExpansionRecord &Expansion) const {
1281   CoverageData ExpansionCoverage(
1282       Expansion.Function.Filenames[Expansion.FileID]);
1283   std::vector<CountedRegion> Regions;
1284   for (const auto &CR : Expansion.Function.CountedRegions)
1285     if (CR.FileID == Expansion.FileID) {
1286       Regions.push_back(CR);
1287       if (isExpansion(CR, Expansion.FileID))
1288         ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
1289     }
1290   for (const auto &CR : Expansion.Function.CountedBranchRegions)
1291     // Capture branch regions that only pertain to the corresponding expansion.
1292     if (CR.FileID == Expansion.FileID)
1293       ExpansionCoverage.BranchRegions.push_back(CR);
1294 
1295   LLVM_DEBUG(dbgs() << "Emitting segments for expansion of file "
1296                     << Expansion.FileID << "\n");
1297   ExpansionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
1298 
1299   return ExpansionCoverage;
1300 }
1301 
1302 LineCoverageStats::LineCoverageStats(
1303     ArrayRef<const CoverageSegment *> LineSegments,
1304     const CoverageSegment *WrappedSegment, unsigned Line)
1305     : ExecutionCount(0), HasMultipleRegions(false), Mapped(false), Line(Line),
1306       LineSegments(LineSegments), WrappedSegment(WrappedSegment) {
1307   // Find the minimum number of regions which start in this line.
1308   unsigned MinRegionCount = 0;
1309   auto isStartOfRegion = [](const CoverageSegment *S) {
1310     return !S->IsGapRegion && S->HasCount && S->IsRegionEntry;
1311   };
1312   for (unsigned I = 0; I < LineSegments.size() && MinRegionCount < 2; ++I)
1313     if (isStartOfRegion(LineSegments[I]))
1314       ++MinRegionCount;
1315 
1316   bool StartOfSkippedRegion = !LineSegments.empty() &&
1317                               !LineSegments.front()->HasCount &&
1318                               LineSegments.front()->IsRegionEntry;
1319 
1320   HasMultipleRegions = MinRegionCount > 1;
1321   Mapped =
1322       !StartOfSkippedRegion &&
1323       ((WrappedSegment && WrappedSegment->HasCount) || (MinRegionCount > 0));
1324 
1325   // if there is any starting segment at this line with a counter, it must be
1326   // mapped
1327   Mapped |= std::any_of(
1328       LineSegments.begin(), LineSegments.end(),
1329       [](const auto *Seq) { return Seq->IsRegionEntry && Seq->HasCount; });
1330 
1331   if (!Mapped) {
1332     return;
1333   }
1334 
1335   // Pick the max count from the non-gap, region entry segments and the
1336   // wrapped count.
1337   if (WrappedSegment)
1338     ExecutionCount = WrappedSegment->Count;
1339   if (!MinRegionCount)
1340     return;
1341   for (const auto *LS : LineSegments)
1342     if (isStartOfRegion(LS))
1343       ExecutionCount = std::max(ExecutionCount, LS->Count);
1344 }
1345 
1346 LineCoverageIterator &LineCoverageIterator::operator++() {
1347   if (Next == CD.end()) {
1348     Stats = LineCoverageStats();
1349     Ended = true;
1350     return *this;
1351   }
1352   if (Segments.size())
1353     WrappedSegment = Segments.back();
1354   Segments.clear();
1355   while (Next != CD.end() && Next->Line == Line)
1356     Segments.push_back(&*Next++);
1357   Stats = LineCoverageStats(Segments, WrappedSegment, Line);
1358   ++Line;
1359   return *this;
1360 }
1361 
1362 static std::string getCoverageMapErrString(coveragemap_error Err,
1363                                            const std::string &ErrMsg = "") {
1364   std::string Msg;
1365   raw_string_ostream OS(Msg);
1366 
1367   switch (Err) {
1368   case coveragemap_error::success:
1369     OS << "success";
1370     break;
1371   case coveragemap_error::eof:
1372     OS << "end of File";
1373     break;
1374   case coveragemap_error::no_data_found:
1375     OS << "no coverage data found";
1376     break;
1377   case coveragemap_error::unsupported_version:
1378     OS << "unsupported coverage format version";
1379     break;
1380   case coveragemap_error::truncated:
1381     OS << "truncated coverage data";
1382     break;
1383   case coveragemap_error::malformed:
1384     OS << "malformed coverage data";
1385     break;
1386   case coveragemap_error::decompression_failed:
1387     OS << "failed to decompress coverage data (zlib)";
1388     break;
1389   case coveragemap_error::invalid_or_missing_arch_specifier:
1390     OS << "`-arch` specifier is invalid or missing for universal binary";
1391     break;
1392   }
1393 
1394   // If optional error message is not empty, append it to the message.
1395   if (!ErrMsg.empty())
1396     OS << ": " << ErrMsg;
1397 
1398   return Msg;
1399 }
1400 
1401 namespace {
1402 
1403 // FIXME: This class is only here to support the transition to llvm::Error. It
1404 // will be removed once this transition is complete. Clients should prefer to
1405 // deal with the Error value directly, rather than converting to error_code.
1406 class CoverageMappingErrorCategoryType : public std::error_category {
1407   const char *name() const noexcept override { return "llvm.coveragemap"; }
1408   std::string message(int IE) const override {
1409     return getCoverageMapErrString(static_cast<coveragemap_error>(IE));
1410   }
1411 };
1412 
1413 } // end anonymous namespace
1414 
1415 std::string CoverageMapError::message() const {
1416   return getCoverageMapErrString(Err, Msg);
1417 }
1418 
1419 const std::error_category &llvm::coverage::coveragemap_category() {
1420   static CoverageMappingErrorCategoryType ErrorCategory;
1421   return ErrorCategory;
1422 }
1423 
1424 char CoverageMapError::ID = 0;
1425