1//===- CoverageMappingWriter.cpp - Code coverage mapping writer -----------===// 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 7//===----------------------------------------------------------------------===// 9// This file contains support for writing coverage mapping data for 10// instrumentation based coverage. 12//===----------------------------------------------------------------------===// 27using namespacecoverage;
31 : Filenames(Filenames) {
40 std::string FilenamesStr;
43for (
constauto &Filename : Filenames) {
45 FilenamesOS << Filename;
59// <compressed-len-or-zero> 60// (<compressed-filenames> | <uncompressed-filenames>) 64OS << (doCompression ? toStringRef(CompressedStr) :
StringRef(FilenamesStr));
69/// Gather only the expressions that are used by the mapping 70/// regions in this function. 71classCounterExpressionsMinimizer {
74 std::vector<unsigned> AdjustedExpressionIDs;
79 : Expressions(Expressions) {
80 AdjustedExpressionIDs.resize(Expressions.
size(), 0);
81for (
constauto &
I : MappingRegions) {
85for (
constauto &
I : MappingRegions) {
87 gatherUsed(
I.FalseCount);
94unsignedID =
C.getExpressionID();
95 AdjustedExpressionIDs[
ID] = 1;
96 mark(Expressions[
ID].
LHS);
97 mark(Expressions[
ID].
RHS);
101if (!
C.isExpression() || !AdjustedExpressionIDs[
C.getExpressionID()])
103 AdjustedExpressionIDs[
C.getExpressionID()] = UsedExpressions.
size();
104constauto &
E = Expressions[
C.getExpressionID()];
112 /// Adjust the given counter to correctly transition from the old 113 /// expression ids to the new expression ids. 121}
// end anonymous namespace 123/// Encode the counter. 125/// The encoding uses the following format: 127/// Counter::Zero(0) - A Counter with kind Counter::Zero 128/// Counter::CounterValueReference(1) - A counter with kind 129/// Counter::CounterValueReference 130/// Counter::Expression(2) + CounterExpression::Subtract(0) - 131/// A counter with kind Counter::Expression and an expression 132/// with kind CounterExpression::Subtract 133/// Counter::Expression(2) + CounterExpression::Add(1) - 134/// A counter with kind Counter::Expression and an expression 135/// with kind CounterExpression::Add 136/// Remaining bits - Counter/Expression ID. 141Tag += Expressions[
C.getExpressionID()].Kind;
142unsignedID =
C.getCounterID();
154// Check that we don't have any bogus regions. 159"Source region does not begin before it ends");
161// Sort the regions in an ascending order by the file id and the starting 162// location. Sort by region kinds to ensure stable order for tests. 165if (
LHS.FileID !=
RHS.FileID)
166returnLHS.FileID <
RHS.FileID;
168returnLHS.startLoc() <
RHS.startLoc();
170// Put `Decision` before `Expansion`. 172 return (Kind == CounterMappingRegion::MCDCDecisionRegion
173 ? 2 * CounterMappingRegion::ExpansionRegion - 1
177return getKindKey(
LHS.Kind) < getKindKey(
RHS.Kind);
180// Write out the fileid -> filename mapping. 182for (
constauto &FileID : VirtualFileMapping)
185// Write out the expressions. 186 CounterExpressionsMinimizer Minimizer(Expressions, MappingRegions);
187auto MinExpressions = Minimizer.getExpressions();
189for (
constauto &E : MinExpressions) {
194// Write out the mapping regions. 195// Split the regions into subarrays where each region in a 196// subarray has a fileID which is the index of that subarray. 197unsigned PrevLineStart = 0;
198unsigned CurrentFileID = ~0U;
199for (
autoI = MappingRegions.begin(), E = MappingRegions.end();
I != E; ++
I) {
200if (
I->FileID != CurrentFileID) {
201// Ensure that all file ids have at least one mapping region. 202assert(
I->FileID == (CurrentFileID + 1));
203// Find the number of regions with this file id. 204unsigned RegionCount = 1;
205for (
auto J =
I + 1; J != E &&
I->FileID == J->FileID; ++J)
207// Start a new region sub-array. 210 CurrentFileID =
I->FileID;
213Counter Count = Minimizer.adjust(
I->Count);
214Counter FalseCount = Minimizer.adjust(
I->FalseCount);
215bool ParamsShouldBeNull =
true;
224 (std::numeric_limits<unsigned>::max() >>
226// Mark an expansion region with a set bit that follows the counter tag, 227// and pack the expanded file id into the remaining bits. 228unsigned EncodedTagExpandedFileID =
255// They are written as internal values plus 1. 256constauto &BranchParams =
I->getBranchParams();
257 ParamsShouldBeNull =
false;
258unsigned ID1 = BranchParams.ID + 1;
259unsigned TID1 = BranchParams.Conds[
true] + 1;
260unsigned FID1 = BranchParams.Conds[
false] + 1;
271constauto &DecisionParams =
I->getDecisionParams();
272 ParamsShouldBeNull =
false;
278assert(
I->LineStart >= PrevLineStart);
284 PrevLineStart =
I->LineStart;
285assert((!ParamsShouldBeNull || std::get_if<0>(&
I->MCDCParams)) &&
286"MCDCParams should be empty");
287 (void)ParamsShouldBeNull;
289// Ensure that all file ids have at least one mapping region. 290assert(CurrentFileID == (VirtualFileMapping.size() - 1));
295return support::endian::byte_swap<uint64_t, llvm::endianness::little>(
N);
298// Output a 64bit magic number. 300OS.
write(
reinterpret_cast<char *
>(&Magic),
sizeof(Magic));
302// Output a 64bit version field. 304OS.
write(
reinterpret_cast<char *
>(&VersionLittle),
sizeof(VersionLittle));
306// Output the ProfileNames data. 309OS << ProfileNamesData;
311// Version2 adds an extra field to indicate the size of the 312// CoverageMappingData. 316// Coverage mapping data is expected to have an alignment of 8. 319OS << CoverageMappingData;
321// Coverage records data is expected to have an alignment of 8. 324OS << CoverageRecordsData;
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void writeCounter(ArrayRef< CounterExpression > Expressions, Counter C, raw_ostream &OS)
static unsigned encodeCounter(ArrayRef< CounterExpression > Expressions, Counter C)
Encode the counter.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
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.
constexpr size_t size() const
size - Get the string size.
StringSet - A wrapper for StringMap that provides set-like functionality.
std::pair< typename Base::iterator, bool > insert(StringRef key)
void write(raw_ostream &OS, bool Compress=true)
Write encoded filenames to the given output stream.
CoverageFilenamesSectionWriter(ArrayRef< std::string > Filenames)
void write(raw_ostream &OS)
Write encoded coverage mapping data to the given output stream.
void write(raw_ostream &OS, TestingFormatVersion Version=TestingFormatVersion::CurrentVersion)
Encode to the given output stream.
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.
@ C
The default llvm calling convention, compatible with C.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
constexpr int BestSizeCompression
constexpr uint64_t TestingFormatMagic
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.
cl::opt< bool > DoInstrProfNameCompression
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...
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
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.