LLVM: lib/ProfileData/Coverage/CoverageMappingWriter.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
22#include
23#include
24#include
25
26using namespace llvm;
28
31 : Filenames(Filenames) {
32#ifndef NDEBUG
35 assert(NameSet.insert(Name).second && "Duplicate filename");
36#endif
37}
38
40 std::string FilenamesStr;
41 {
43 for (const auto &Filename : Filenames) {
45 FilenamesOS << Filename;
46 }
47 }
48
52 if (doCompression)
54 CompressedStr,
56
57
58
59
60
65}
66
67namespace {
68
69
70
71class CounterExpressionsMinimizer {
74 std::vector AdjustedExpressionIDs;
75
76public:
79 : Expressions(Expressions) {
80 AdjustedExpressionIDs.resize(Expressions.size(), 0);
81 for (const auto &I : MappingRegions) {
82 mark(I.Count);
83 mark(I.FalseCount);
84 }
85 for (const auto &I : MappingRegions) {
86 gatherUsed(I.Count);
87 gatherUsed(I.FalseCount);
88 }
89 }
90
91 void mark(Counter C) {
92 if (.isExpression())
93 return;
94 unsigned ID = C.getExpressionID();
95 AdjustedExpressionIDs[ID] = 1;
98 }
99
100 void gatherUsed(Counter C) {
101 if (.isExpression() || !AdjustedExpressionIDs[C.getExpressionID()])
102 return;
103 AdjustedExpressionIDs[C.getExpressionID()] = UsedExpressions.size();
104 const auto &E = Expressions[C.getExpressionID()];
106 gatherUsed(E.LHS);
107 gatherUsed(E.RHS);
108 }
109
111
112
113
114 Counter adjust(Counter C) const {
115 if (C.isExpression())
117 return C;
118 }
119};
120
121}
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
139 unsigned Tag = unsigned(C.getKind());
140 if (C.isExpression())
141 Tag += Expressions[C.getExpressionID()].Kind;
142 unsigned ID = C.getCounterID();
146}
147
152
154
158 }) &&
159 "Source region does not begin before it ends");
160
161
162
165 if (LHS.FileID != RHS.FileID)
166 return LHS.FileID < RHS.FileID;
167 if (LHS.startLoc() != RHS.startLoc())
168 return LHS.startLoc() < RHS.startLoc();
169
170
174 : 2 * Kind);
175 };
176
177 return getKindKey(LHS.Kind) < getKindKey(RHS.Kind);
178 });
179
180
182 for (const auto &FileID : VirtualFileMapping)
184
185
186 CounterExpressionsMinimizer Minimizer(Expressions, MappingRegions);
187 auto MinExpressions = Minimizer.getExpressions();
189 for (const auto &E : MinExpressions) {
190 writeCounter(MinExpressions, Minimizer.adjust(E.LHS), OS);
191 writeCounter(MinExpressions, Minimizer.adjust(E.RHS), OS);
192 }
193
194
195
196
197 unsigned PrevLineStart = 0;
198 unsigned CurrentFileID = ~0U;
199 for (auto I = MappingRegions.begin(), E = MappingRegions.end(); I != E; ++I) {
200 if (I->FileID != CurrentFileID) {
201
202 assert(I->FileID == (CurrentFileID + 1));
203
204 unsigned RegionCount = 1;
205 for (auto J = I + 1; J != E && I->FileID == J->FileID; ++J)
206 ++RegionCount;
207
209
210 CurrentFileID = I->FileID;
211 PrevLineStart = 0;
212 }
214 Counter FalseCount = Minimizer.adjust(I->FalseCount);
215 bool ParamsShouldBeNull = true;
216 switch (I->Kind) {
220 break;
223 assert(I->ExpandedFileID <=
224 (std::numeric_limits::max() >>
226
227
228 unsigned EncodedTagExpandedFileID =
230 (I->ExpandedFileID
233 break;
234 }
239 OS);
240 break;
244 OS);
246 writeCounter(MinExpressions, FalseCount, OS);
247 break;
251 OS);
253 writeCounter(MinExpressions, FalseCount, OS);
254 {
255
256 const auto &BranchParams = I->getBranchParams();
257 ParamsShouldBeNull = false;
258 unsigned ID1 = BranchParams.ID + 1;
259 unsigned TID1 = BranchParams.Conds[true] + 1;
260 unsigned FID1 = BranchParams.Conds[false] + 1;
264 }
265 break;
269 OS);
270 {
271 const auto &DecisionParams = I->getDecisionParams();
272 ParamsShouldBeNull = false;
273 encodeULEB128(static_cast<unsigned>(DecisionParams.BitmapIdx), OS);
274 encodeULEB128(static_cast<unsigned>(DecisionParams.NumConditions), OS);
275 }
276 break;
277 }
278 assert(I->LineStart >= PrevLineStart);
281 assert(I->LineEnd >= I->LineStart);
284 PrevLineStart = I->LineStart;
285 assert((!ParamsShouldBeNull || std::get_if<0>(&I->MCDCParams)) &&
286 "MCDCParams should be empty");
287 (void)ParamsShouldBeNull;
288 }
289
290 assert(CurrentFileID == (VirtualFileMapping.size() - 1));
291}
292
296 };
297
298
300 OS.write(reinterpret_cast<char *>(&Magic), sizeof(Magic));
301
302
304 OS.write(reinterpret_cast<char *>(&VersionLittle), sizeof(VersionLittle));
305
306
309 OS << ProfileNamesData;
310
311
312
315
316
319 OS << CoverageMappingData;
320
321
324 OS << CoverageRecordsData;
325}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void writeCounter(ArrayRef< CounterExpression > Expressions, Counter C, raw_ostream &OS)
Definition CoverageMappingWriter.cpp:148
static unsigned encodeCounter(ArrayRef< CounterExpression > Expressions, Counter C)
Encode the counter.
Definition CoverageMappingWriter.cpp:137
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
StringSet - A wrapper for StringMap that provides set-like functionality.
std::pair< typename Base::iterator, bool > insert(StringRef key)
LLVM_ABI void write(raw_ostream &OS, bool Compress=true)
Write encoded filenames to the given output stream.
Definition CoverageMappingWriter.cpp:39
LLVM_ABI CoverageFilenamesSectionWriter(ArrayRef< std::string > Filenames)
Definition CoverageMappingWriter.cpp:29
LLVM_ABI void write(raw_ostream &OS)
Write encoded coverage mapping data to the given output stream.
Definition CoverageMappingWriter.cpp:153
LLVM_ABI void write(raw_ostream &OS, TestingFormatVersion Version=TestingFormatVersion::CurrentVersion)
Encode to the given output stream.
Definition CoverageMappingWriter.cpp:293
This class implements an extremely fast bulk output stream that can only output to a stream.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an std::string.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
LLVM_ABI void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
LLVM_ABI bool isAvailable()
constexpr int BestSizeCompression
constexpr uint64_t TestingFormatMagic
value_type byte_swap(value_type value, endianness endian)
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
LLVM_ABI cl::opt< bool > DoInstrProfNameCompression
FunctionAddr VTableAddr uintptr_t uintptr_t Version
FunctionAddr VTableAddr Count
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
ArrayRef(const T &OneElt) -> ArrayRef< T >
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
This struct is a compact representation of a valid (non-zero power of two) alignment.
A Counter mapping region associates a source range with a specific counter.
LineColPair endLoc() const
LineColPair startLoc() const
@ ExpansionRegion
An ExpansionRegion represents a file expansion region that associates a source range with the expansi...
@ MCDCDecisionRegion
A DecisionRegion represents a top-level boolean expression and is associated with a variable length b...
@ MCDCBranchRegion
A Branch Region can be extended to include IDs to facilitate MC/DC.
@ SkippedRegion
A SkippedRegion represents a source range with code that was skipped by a preprocessor or similar mea...
@ GapRegion
A GapRegion is like a CodeRegion, but its count is only set as the line execution count when its the ...
@ BranchRegion
A BranchRegion represents leaf-level boolean expressions and is associated with two counters,...
@ CodeRegion
A CodeRegion associates some code with a counter.
A Counter is an abstract value that describes how to compute the execution count for a region of code...
static const unsigned EncodingTagBits
static const unsigned EncodingCounterTagAndExpansionRegionTagBits
static Counter getExpression(unsigned ExpressionId)
Return the counter that corresponds to a specific addition counter expression.