1e78d131aSEugene Zelenko //===- CoverageMappingWriter.cpp - Code coverage mapping writer -----------===//
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 writing coverage mapping data for
10dc707122SEaswaran Raman // instrumentation based coverage.
11dc707122SEaswaran Raman //
12dc707122SEaswaran Raman //===----------------------------------------------------------------------===//
13dc707122SEaswaran Raman 
14dd1ea9deSVedant Kumar #include "llvm/ProfileData/InstrProf.h"
156bda14b3SChandler Carruth #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
16e78d131aSEugene Zelenko #include "llvm/ADT/ArrayRef.h"
17e78d131aSEugene Zelenko #include "llvm/ADT/SmallVector.h"
18dd1ea9deSVedant Kumar #include "llvm/Support/Compression.h"
19dc707122SEaswaran Raman #include "llvm/Support/LEB128.h"
20e78d131aSEugene Zelenko #include "llvm/Support/raw_ostream.h"
21e78d131aSEugene Zelenko #include <algorithm>
22e78d131aSEugene Zelenko #include <cassert>
23e78d131aSEugene Zelenko #include <limits>
24e78d131aSEugene Zelenko #include <vector>
25dc707122SEaswaran Raman 
26dc707122SEaswaran Raman using namespace llvm;
27dc707122SEaswaran Raman using namespace coverage;
28dc707122SEaswaran Raman 
2995de2497SVedant Kumar CoverageFilenamesSectionWriter::CoverageFilenamesSectionWriter(
305fbd1a33SPetr Hosek     ArrayRef<std::string> Filenames)
3195de2497SVedant Kumar     : Filenames(Filenames) {
3295de2497SVedant Kumar #ifndef NDEBUG
3395de2497SVedant Kumar   StringSet<> NameSet;
3495de2497SVedant Kumar   for (StringRef Name : Filenames)
3595de2497SVedant Kumar     assert(NameSet.insert(Name).second && "Duplicate filename");
3695de2497SVedant Kumar #endif
3795de2497SVedant Kumar }
3895de2497SVedant Kumar 
39dd1ea9deSVedant Kumar void CoverageFilenamesSectionWriter::write(raw_ostream &OS, bool Compress) {
40dd1ea9deSVedant Kumar   std::string FilenamesStr;
41dd1ea9deSVedant Kumar   {
42dd1ea9deSVedant Kumar     raw_string_ostream FilenamesOS{FilenamesStr};
4333888717SVedant Kumar     for (const auto &Filename : Filenames) {
44dd1ea9deSVedant Kumar       encodeULEB128(Filename.size(), FilenamesOS);
45dd1ea9deSVedant Kumar       FilenamesOS << Filename;
4633888717SVedant Kumar     }
4799317124SVedant Kumar   }
4899317124SVedant Kumar 
49dd1ea9deSVedant Kumar   SmallString<128> CompressedStr;
50*ea61750cSCole Kissane   bool doCompression = Compress && compression::zlib::isAvailable() &&
51*ea61750cSCole Kissane                        DoInstrProfNameCompression;
52407c721cSFangrui Song   if (doCompression)
53*ea61750cSCole Kissane     compression::zlib::compress(FilenamesStr, CompressedStr,
54*ea61750cSCole Kissane                                 compression::zlib::BestSizeCompression);
55dd1ea9deSVedant Kumar 
56dd1ea9deSVedant Kumar   // ::= <num-filenames>
57dd1ea9deSVedant Kumar   //     <uncompressed-len>
58dd1ea9deSVedant Kumar   //     <compressed-len-or-zero>
59dd1ea9deSVedant Kumar   //     (<compressed-filenames> | <uncompressed-filenames>)
60dd1ea9deSVedant Kumar   encodeULEB128(Filenames.size(), OS);
61dd1ea9deSVedant Kumar   encodeULEB128(FilenamesStr.size(), OS);
62dd1ea9deSVedant Kumar   encodeULEB128(doCompression ? CompressedStr.size() : 0U, OS);
631def2579SDavid Blaikie   OS << (doCompression ? CompressedStr.str() : StringRef(FilenamesStr));
64dd1ea9deSVedant Kumar }
65dd1ea9deSVedant Kumar 
66dc707122SEaswaran Raman namespace {
67e78d131aSEugene Zelenko 
685f8f34e4SAdrian Prantl /// Gather only the expressions that are used by the mapping
69dc707122SEaswaran Raman /// regions in this function.
70dc707122SEaswaran Raman class CounterExpressionsMinimizer {
71dc707122SEaswaran Raman   ArrayRef<CounterExpression> Expressions;
72e78d131aSEugene Zelenko   SmallVector<CounterExpression, 16> UsedExpressions;
73dc707122SEaswaran Raman   std::vector<unsigned> AdjustedExpressionIDs;
74dc707122SEaswaran Raman 
75dc707122SEaswaran Raman public:
76e78d131aSEugene Zelenko   CounterExpressionsMinimizer(ArrayRef<CounterExpression> Expressions,
77e78d131aSEugene Zelenko                               ArrayRef<CounterMappingRegion> MappingRegions)
78e78d131aSEugene Zelenko       : Expressions(Expressions) {
79e78d131aSEugene Zelenko     AdjustedExpressionIDs.resize(Expressions.size(), 0);
809f2967bcSAlan Phipps     for (const auto &I : MappingRegions) {
81e78d131aSEugene Zelenko       mark(I.Count);
829f2967bcSAlan Phipps       mark(I.FalseCount);
839f2967bcSAlan Phipps     }
849f2967bcSAlan Phipps     for (const auto &I : MappingRegions) {
85e78d131aSEugene Zelenko       gatherUsed(I.Count);
869f2967bcSAlan Phipps       gatherUsed(I.FalseCount);
879f2967bcSAlan Phipps     }
88e78d131aSEugene Zelenko   }
89e78d131aSEugene Zelenko 
90dc707122SEaswaran Raman   void mark(Counter C) {
91dc707122SEaswaran Raman     if (!C.isExpression())
92dc707122SEaswaran Raman       return;
93dc707122SEaswaran Raman     unsigned ID = C.getExpressionID();
94dc707122SEaswaran Raman     AdjustedExpressionIDs[ID] = 1;
95dc707122SEaswaran Raman     mark(Expressions[ID].LHS);
96dc707122SEaswaran Raman     mark(Expressions[ID].RHS);
97dc707122SEaswaran Raman   }
98dc707122SEaswaran Raman 
99dc707122SEaswaran Raman   void gatherUsed(Counter C) {
100dc707122SEaswaran Raman     if (!C.isExpression() || !AdjustedExpressionIDs[C.getExpressionID()])
101dc707122SEaswaran Raman       return;
102dc707122SEaswaran Raman     AdjustedExpressionIDs[C.getExpressionID()] = UsedExpressions.size();
103dc707122SEaswaran Raman     const auto &E = Expressions[C.getExpressionID()];
104dc707122SEaswaran Raman     UsedExpressions.push_back(E);
105dc707122SEaswaran Raman     gatherUsed(E.LHS);
106dc707122SEaswaran Raman     gatherUsed(E.RHS);
107dc707122SEaswaran Raman   }
108dc707122SEaswaran Raman 
109dc707122SEaswaran Raman   ArrayRef<CounterExpression> getExpressions() const { return UsedExpressions; }
110dc707122SEaswaran Raman 
1115f8f34e4SAdrian Prantl   /// Adjust the given counter to correctly transition from the old
112dc707122SEaswaran Raman   /// expression ids to the new expression ids.
113dc707122SEaswaran Raman   Counter adjust(Counter C) const {
114dc707122SEaswaran Raman     if (C.isExpression())
115dc707122SEaswaran Raman       C = Counter::getExpression(AdjustedExpressionIDs[C.getExpressionID()]);
116dc707122SEaswaran Raman     return C;
117dc707122SEaswaran Raman   }
118dc707122SEaswaran Raman };
119e78d131aSEugene Zelenko 
120e78d131aSEugene Zelenko } // end anonymous namespace
121dc707122SEaswaran Raman 
1225f8f34e4SAdrian Prantl /// Encode the counter.
123dc707122SEaswaran Raman ///
124dc707122SEaswaran Raman /// The encoding uses the following format:
125dc707122SEaswaran Raman /// Low 2 bits - Tag:
126dc707122SEaswaran Raman ///   Counter::Zero(0) - A Counter with kind Counter::Zero
127dc707122SEaswaran Raman ///   Counter::CounterValueReference(1) - A counter with kind
128dc707122SEaswaran Raman ///     Counter::CounterValueReference
129dc707122SEaswaran Raman ///   Counter::Expression(2) + CounterExpression::Subtract(0) -
130dc707122SEaswaran Raman ///     A counter with kind Counter::Expression and an expression
131dc707122SEaswaran Raman ///     with kind CounterExpression::Subtract
132dc707122SEaswaran Raman ///   Counter::Expression(2) + CounterExpression::Add(1) -
133dc707122SEaswaran Raman ///     A counter with kind Counter::Expression and an expression
134dc707122SEaswaran Raman ///     with kind CounterExpression::Add
135dc707122SEaswaran Raman /// Remaining bits - Counter/Expression ID.
136dc707122SEaswaran Raman static unsigned encodeCounter(ArrayRef<CounterExpression> Expressions,
137dc707122SEaswaran Raman                               Counter C) {
138dc707122SEaswaran Raman   unsigned Tag = unsigned(C.getKind());
139dc707122SEaswaran Raman   if (C.isExpression())
140dc707122SEaswaran Raman     Tag += Expressions[C.getExpressionID()].Kind;
141dc707122SEaswaran Raman   unsigned ID = C.getCounterID();
142dc707122SEaswaran Raman   assert(ID <=
143dc707122SEaswaran Raman          (std::numeric_limits<unsigned>::max() >> Counter::EncodingTagBits));
144dc707122SEaswaran Raman   return Tag | (ID << Counter::EncodingTagBits);
145dc707122SEaswaran Raman }
146dc707122SEaswaran Raman 
147dc707122SEaswaran Raman static void writeCounter(ArrayRef<CounterExpression> Expressions, Counter C,
148dc707122SEaswaran Raman                          raw_ostream &OS) {
149dc707122SEaswaran Raman   encodeULEB128(encodeCounter(Expressions, C), OS);
150dc707122SEaswaran Raman }
151dc707122SEaswaran Raman 
152dc707122SEaswaran Raman void CoverageMappingWriter::write(raw_ostream &OS) {
153bae83970SVedant Kumar   // Check that we don't have any bogus regions.
154bae83970SVedant Kumar   assert(all_of(MappingRegions,
155bae83970SVedant Kumar                 [](const CounterMappingRegion &CMR) {
156bae83970SVedant Kumar                   return CMR.startLoc() <= CMR.endLoc();
157bae83970SVedant Kumar                 }) &&
158bae83970SVedant Kumar          "Source region does not begin before it ends");
159bae83970SVedant Kumar 
160dc707122SEaswaran Raman   // Sort the regions in an ascending order by the file id and the starting
161f3c8a9cfSIgor Kudrin   // location. Sort by region kinds to ensure stable order for tests.
162efd94c56SFangrui Song   llvm::stable_sort(MappingRegions, [](const CounterMappingRegion &LHS,
163efd94c56SFangrui Song                                        const CounterMappingRegion &RHS) {
164f3c8a9cfSIgor Kudrin     if (LHS.FileID != RHS.FileID)
165f3c8a9cfSIgor Kudrin       return LHS.FileID < RHS.FileID;
166f3c8a9cfSIgor Kudrin     if (LHS.startLoc() != RHS.startLoc())
167f3c8a9cfSIgor Kudrin       return LHS.startLoc() < RHS.startLoc();
168f3c8a9cfSIgor Kudrin     return LHS.Kind < RHS.Kind;
169f3c8a9cfSIgor Kudrin   });
170dc707122SEaswaran Raman 
171dc707122SEaswaran Raman   // Write out the fileid -> filename mapping.
172dc707122SEaswaran Raman   encodeULEB128(VirtualFileMapping.size(), OS);
173dc707122SEaswaran Raman   for (const auto &FileID : VirtualFileMapping)
174dc707122SEaswaran Raman     encodeULEB128(FileID, OS);
175dc707122SEaswaran Raman 
176dc707122SEaswaran Raman   // Write out the expressions.
177dc707122SEaswaran Raman   CounterExpressionsMinimizer Minimizer(Expressions, MappingRegions);
178dc707122SEaswaran Raman   auto MinExpressions = Minimizer.getExpressions();
179dc707122SEaswaran Raman   encodeULEB128(MinExpressions.size(), OS);
180dc707122SEaswaran Raman   for (const auto &E : MinExpressions) {
181dc707122SEaswaran Raman     writeCounter(MinExpressions, Minimizer.adjust(E.LHS), OS);
182dc707122SEaswaran Raman     writeCounter(MinExpressions, Minimizer.adjust(E.RHS), OS);
183dc707122SEaswaran Raman   }
184dc707122SEaswaran Raman 
185dc707122SEaswaran Raman   // Write out the mapping regions.
186dc707122SEaswaran Raman   // Split the regions into subarrays where each region in a
187dc707122SEaswaran Raman   // subarray has a fileID which is the index of that subarray.
188dc707122SEaswaran Raman   unsigned PrevLineStart = 0;
189dc707122SEaswaran Raman   unsigned CurrentFileID = ~0U;
190dc707122SEaswaran Raman   for (auto I = MappingRegions.begin(), E = MappingRegions.end(); I != E; ++I) {
191dc707122SEaswaran Raman     if (I->FileID != CurrentFileID) {
192dc707122SEaswaran Raman       // Ensure that all file ids have at least one mapping region.
193dc707122SEaswaran Raman       assert(I->FileID == (CurrentFileID + 1));
194dc707122SEaswaran Raman       // Find the number of regions with this file id.
195dc707122SEaswaran Raman       unsigned RegionCount = 1;
196dc707122SEaswaran Raman       for (auto J = I + 1; J != E && I->FileID == J->FileID; ++J)
197dc707122SEaswaran Raman         ++RegionCount;
198dc707122SEaswaran Raman       // Start a new region sub-array.
199dc707122SEaswaran Raman       encodeULEB128(RegionCount, OS);
200dc707122SEaswaran Raman 
201dc707122SEaswaran Raman       CurrentFileID = I->FileID;
202dc707122SEaswaran Raman       PrevLineStart = 0;
203dc707122SEaswaran Raman     }
204dc707122SEaswaran Raman     Counter Count = Minimizer.adjust(I->Count);
2059f2967bcSAlan Phipps     Counter FalseCount = Minimizer.adjust(I->FalseCount);
206dc707122SEaswaran Raman     switch (I->Kind) {
207dc707122SEaswaran Raman     case CounterMappingRegion::CodeRegion:
208ad8f637bSVedant Kumar     case CounterMappingRegion::GapRegion:
209dc707122SEaswaran Raman       writeCounter(MinExpressions, Count, OS);
210dc707122SEaswaran Raman       break;
211dc707122SEaswaran Raman     case CounterMappingRegion::ExpansionRegion: {
212dc707122SEaswaran Raman       assert(Count.isZero());
213dc707122SEaswaran Raman       assert(I->ExpandedFileID <=
214dc707122SEaswaran Raman              (std::numeric_limits<unsigned>::max() >>
215dc707122SEaswaran Raman               Counter::EncodingCounterTagAndExpansionRegionTagBits));
216dc707122SEaswaran Raman       // Mark an expansion region with a set bit that follows the counter tag,
217dc707122SEaswaran Raman       // and pack the expanded file id into the remaining bits.
218dc707122SEaswaran Raman       unsigned EncodedTagExpandedFileID =
219dc707122SEaswaran Raman           (1 << Counter::EncodingTagBits) |
220dc707122SEaswaran Raman           (I->ExpandedFileID
221dc707122SEaswaran Raman            << Counter::EncodingCounterTagAndExpansionRegionTagBits);
222dc707122SEaswaran Raman       encodeULEB128(EncodedTagExpandedFileID, OS);
223dc707122SEaswaran Raman       break;
224dc707122SEaswaran Raman     }
225dc707122SEaswaran Raman     case CounterMappingRegion::SkippedRegion:
226dc707122SEaswaran Raman       assert(Count.isZero());
227dc707122SEaswaran Raman       encodeULEB128(unsigned(I->Kind)
228dc707122SEaswaran Raman                         << Counter::EncodingCounterTagAndExpansionRegionTagBits,
229dc707122SEaswaran Raman                     OS);
230dc707122SEaswaran Raman       break;
2319f2967bcSAlan Phipps     case CounterMappingRegion::BranchRegion:
2329f2967bcSAlan Phipps       encodeULEB128(unsigned(I->Kind)
2339f2967bcSAlan Phipps                         << Counter::EncodingCounterTagAndExpansionRegionTagBits,
2349f2967bcSAlan Phipps                     OS);
2359f2967bcSAlan Phipps       writeCounter(MinExpressions, Count, OS);
2369f2967bcSAlan Phipps       writeCounter(MinExpressions, FalseCount, OS);
2379f2967bcSAlan Phipps       break;
238dc707122SEaswaran Raman     }
239dc707122SEaswaran Raman     assert(I->LineStart >= PrevLineStart);
240dc707122SEaswaran Raman     encodeULEB128(I->LineStart - PrevLineStart, OS);
241dc707122SEaswaran Raman     encodeULEB128(I->ColumnStart, OS);
242dc707122SEaswaran Raman     assert(I->LineEnd >= I->LineStart);
243dc707122SEaswaran Raman     encodeULEB128(I->LineEnd - I->LineStart, OS);
244dc707122SEaswaran Raman     encodeULEB128(I->ColumnEnd, OS);
245dc707122SEaswaran Raman     PrevLineStart = I->LineStart;
246dc707122SEaswaran Raman   }
247dc707122SEaswaran Raman   // Ensure that all file ids have at least one mapping region.
248dc707122SEaswaran Raman   assert(CurrentFileID == (VirtualFileMapping.size() - 1));
249dc707122SEaswaran Raman }
250