#include "llvm/ProfileData/InstrProf.h"
#include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Compression.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <limits>
#include <vector>
using namespace llvm;
using namespace coverage;
CoverageFilenamesSectionWriter::CoverageFilenamesSectionWriter(
ArrayRef<std::string> Filenames)
: Filenames(Filenames) {
#ifndef NDEBUG
StringSet<> NameSet;
for (StringRef Name : Filenames)
assert(NameSet.insert(Name).second && "Duplicate filename");
#endif
}
void CoverageFilenamesSectionWriter::write(raw_ostream &OS, bool Compress) {
std::string FilenamesStr;
{
raw_string_ostream FilenamesOS{FilenamesStr};
for (const auto &Filename : Filenames) {
encodeULEB128(Filename.size(), FilenamesOS);
FilenamesOS << Filename;
}
}
SmallVector<uint8_t, 128> CompressedStr;
bool doCompression = Compress && compression::zlib::isAvailable() &&
DoInstrProfNameCompression;
if (doCompression)
compression::zlib::compress(arrayRefFromStringRef(FilenamesStr),
CompressedStr,
compression::zlib::BestSizeCompression);
encodeULEB128(Filenames.size(), OS);
encodeULEB128(FilenamesStr.size(), OS);
encodeULEB128(doCompression ? CompressedStr.size() : 0U, OS);
OS << (doCompression ? toStringRef(CompressedStr) : StringRef(FilenamesStr));
}
namespace {
class CounterExpressionsMinimizer {
ArrayRef<CounterExpression> Expressions;
SmallVector<CounterExpression, 16> UsedExpressions;
std::vector<unsigned> AdjustedExpressionIDs;
public:
CounterExpressionsMinimizer(ArrayRef<CounterExpression> Expressions,
ArrayRef<CounterMappingRegion> MappingRegions)
: Expressions(Expressions) {
AdjustedExpressionIDs.resize(Expressions.size(), 0);
for (const auto &I : MappingRegions) {
mark(I.Count);
mark(I.FalseCount);
}
for (const auto &I : MappingRegions) {
gatherUsed(I.Count);
gatherUsed(I.FalseCount);
}
}
void mark(Counter C) {
if (!C.isExpression())
return;
unsigned ID = C.getExpressionID();
AdjustedExpressionIDs[ID] = 1;
mark(Expressions[ID].LHS);
mark(Expressions[ID].RHS);
}
void gatherUsed(Counter C) {
if (!C.isExpression() || !AdjustedExpressionIDs[C.getExpressionID()])
return;
AdjustedExpressionIDs[C.getExpressionID()] = UsedExpressions.size();
const auto &E = Expressions[C.getExpressionID()];
UsedExpressions.push_back(E);
gatherUsed(E.LHS);
gatherUsed(E.RHS);
}
ArrayRef<CounterExpression> getExpressions() const { return UsedExpressions; }
Counter adjust(Counter C) const {
if (C.isExpression())
C = Counter::getExpression(AdjustedExpressionIDs[C.getExpressionID()]);
return C;
}
};
}
static unsigned encodeCounter(ArrayRef<CounterExpression> Expressions,
Counter C) {
unsigned Tag = unsigned(C.getKind());
if (C.isExpression())
Tag += Expressions[C.getExpressionID()].Kind;
unsigned ID = C.getCounterID();
assert(ID <=
(std::numeric_limits<unsigned>::max() >> Counter::EncodingTagBits));
return Tag | (ID << Counter::EncodingTagBits);
}
static void writeCounter(ArrayRef<CounterExpression> Expressions, Counter C,
raw_ostream &OS) {
encodeULEB128(encodeCounter(Expressions, C), OS);
}
void CoverageMappingWriter::write(raw_ostream &OS) {
assert(all_of(MappingRegions,
[](const CounterMappingRegion &CMR) {
return CMR.startLoc() <= CMR.endLoc();
}) &&
"Source region does not begin before it ends");
llvm::stable_sort(MappingRegions, [](const CounterMappingRegion &LHS,
const CounterMappingRegion &RHS) {
if (LHS.FileID != RHS.FileID)
return LHS.FileID < RHS.FileID;
if (LHS.startLoc() != RHS.startLoc())
return LHS.startLoc() < RHS.startLoc();
return LHS.Kind < RHS.Kind;
});
encodeULEB128(VirtualFileMapping.size(), OS);
for (const auto &FileID : VirtualFileMapping)
encodeULEB128(FileID, OS);
CounterExpressionsMinimizer Minimizer(Expressions, MappingRegions);
auto MinExpressions = Minimizer.getExpressions();
encodeULEB128(MinExpressions.size(), OS);
for (const auto &E : MinExpressions) {
writeCounter(MinExpressions, Minimizer.adjust(E.LHS), OS);
writeCounter(MinExpressions, Minimizer.adjust(E.RHS), OS);
}
unsigned PrevLineStart = 0;
unsigned CurrentFileID = ~0U;
for (auto I = MappingRegions.begin(), E = MappingRegions.end(); I != E; ++I) {
if (I->FileID != CurrentFileID) {
assert(I->FileID == (CurrentFileID + 1));
unsigned RegionCount = 1;
for (auto J = I + 1; J != E && I->FileID == J->FileID; ++J)
++RegionCount;
encodeULEB128(RegionCount, OS);
CurrentFileID = I->FileID;
PrevLineStart = 0;
}
Counter Count = Minimizer.adjust(I->Count);
Counter FalseCount = Minimizer.adjust(I->FalseCount);
switch (I->Kind) {
case CounterMappingRegion::CodeRegion:
case CounterMappingRegion::GapRegion:
writeCounter(MinExpressions, Count, OS);
break;
case CounterMappingRegion::ExpansionRegion: {
assert(Count.isZero());
assert(I->ExpandedFileID <=
(std::numeric_limits<unsigned>::max() >>
Counter::EncodingCounterTagAndExpansionRegionTagBits));
unsigned EncodedTagExpandedFileID =
(1 << Counter::EncodingTagBits) |
(I->ExpandedFileID
<< Counter::EncodingCounterTagAndExpansionRegionTagBits);
encodeULEB128(EncodedTagExpandedFileID, OS);
break;
}
case CounterMappingRegion::SkippedRegion:
assert(Count.isZero());
encodeULEB128(unsigned(I->Kind)
<< Counter::EncodingCounterTagAndExpansionRegionTagBits,
OS);
break;
case CounterMappingRegion::BranchRegion:
encodeULEB128(unsigned(I->Kind)
<< Counter::EncodingCounterTagAndExpansionRegionTagBits,
OS);
writeCounter(MinExpressions, Count, OS);
writeCounter(MinExpressions, FalseCount, OS);
break;
}
assert(I->LineStart >= PrevLineStart);
encodeULEB128(I->LineStart - PrevLineStart, OS);
encodeULEB128(I->ColumnStart, OS);
assert(I->LineEnd >= I->LineStart);
encodeULEB128(I->LineEnd - I->LineStart, OS);
encodeULEB128(I->ColumnEnd, OS);
PrevLineStart = I->LineStart;
}
assert(CurrentFileID == (VirtualFileMapping.size() - 1));
}