#ifndef LLVM_PROFILEDATA_COVERAGEMAPPING_H_
#define LLVM_PROFILEDATA_COVERAGEMAPPING_H_
#include "llvm-c/ProfileData.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ADT/iterator.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/raw_ostream.h"
#include <system_error>
#include <tuple>
namespace llvm {
namespace coverage {
enum class coveragemap_error {
success = 0,
eof,
no_data_found,
unsupported_version,
truncated,
malformed
};
} }
namespace std {
template <>
struct is_error_code_enum<llvm::coverage::coveragemap_error> : std::true_type {
};
}
namespace llvm {
class IndexedInstrProfReader;
namespace coverage {
class CoverageMappingReader;
class CoverageMapping;
struct CounterExpressions;
struct Counter {
enum CounterKind { Zero, CounterValueReference, Expression };
static const unsigned EncodingTagBits = 2;
static const unsigned EncodingTagMask = 0x3;
static const unsigned EncodingCounterTagAndExpansionRegionTagBits =
EncodingTagBits + 1;
private:
CounterKind Kind;
unsigned ID;
Counter(CounterKind Kind, unsigned ID) : Kind(Kind), ID(ID) {}
public:
Counter() : Kind(Zero), ID(0) {}
CounterKind getKind() const { return Kind; }
bool isZero() const { return Kind == Zero; }
bool isExpression() const { return Kind == Expression; }
unsigned getCounterID() const { return ID; }
unsigned getExpressionID() const { return ID; }
friend bool operator==(const Counter &LHS, const Counter &RHS) {
return LHS.Kind == RHS.Kind && LHS.ID == RHS.ID;
}
friend bool operator!=(const Counter &LHS, const Counter &RHS) {
return !(LHS == RHS);
}
friend bool operator<(const Counter &LHS, const Counter &RHS) {
return std::tie(LHS.Kind, LHS.ID) < std::tie(RHS.Kind, RHS.ID);
}
static Counter getZero() { return Counter(); }
static Counter getCounter(unsigned CounterId) {
return Counter(CounterValueReference, CounterId);
}
static Counter getExpression(unsigned ExpressionId) {
return Counter(Expression, ExpressionId);
}
};
struct CounterExpression {
enum ExprKind { Subtract, Add };
ExprKind Kind;
Counter LHS, RHS;
CounterExpression(ExprKind Kind, Counter LHS, Counter RHS)
: Kind(Kind), LHS(LHS), RHS(RHS) {}
};
class CounterExpressionBuilder {
std::vector<CounterExpression> Expressions;
llvm::DenseMap<CounterExpression, unsigned> ExpressionIndices;
Counter get(const CounterExpression &E);
void extractTerms(Counter C, int Sign,
SmallVectorImpl<std::pair<unsigned, int>> &Terms);
Counter simplify(Counter ExpressionTree);
public:
ArrayRef<CounterExpression> getExpressions() const { return Expressions; }
Counter add(Counter LHS, Counter RHS);
Counter subtract(Counter LHS, Counter RHS);
};
struct CounterMappingRegion {
enum RegionKind {
CodeRegion,
ExpansionRegion,
SkippedRegion
};
Counter Count;
unsigned FileID, ExpandedFileID;
unsigned LineStart, ColumnStart, LineEnd, ColumnEnd;
RegionKind Kind;
CounterMappingRegion(Counter Count, unsigned FileID, unsigned ExpandedFileID,
unsigned LineStart, unsigned ColumnStart,
unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind)
: Count(Count), FileID(FileID), ExpandedFileID(ExpandedFileID),
LineStart(LineStart), ColumnStart(ColumnStart), LineEnd(LineEnd),
ColumnEnd(ColumnEnd), Kind(Kind) {}
static CounterMappingRegion
makeRegion(Counter Count, unsigned FileID, unsigned LineStart,
unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
return CounterMappingRegion(Count, FileID, 0, LineStart, ColumnStart,
LineEnd, ColumnEnd, CodeRegion);
}
static CounterMappingRegion
makeExpansion(unsigned FileID, unsigned ExpandedFileID, unsigned LineStart,
unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
return CounterMappingRegion(Counter(), FileID, ExpandedFileID, LineStart,
ColumnStart, LineEnd, ColumnEnd,
ExpansionRegion);
}
static CounterMappingRegion
makeSkipped(unsigned FileID, unsigned LineStart, unsigned ColumnStart,
unsigned LineEnd, unsigned ColumnEnd) {
return CounterMappingRegion(Counter(), FileID, 0, LineStart, ColumnStart,
LineEnd, ColumnEnd, SkippedRegion);
}
inline std::pair<unsigned, unsigned> startLoc() const {
return std::pair<unsigned, unsigned>(LineStart, ColumnStart);
}
inline std::pair<unsigned, unsigned> endLoc() const {
return std::pair<unsigned, unsigned>(LineEnd, ColumnEnd);
}
bool operator<(const CounterMappingRegion &Other) const {
if (FileID != Other.FileID)
return FileID < Other.FileID;
return startLoc() < Other.startLoc();
}
bool contains(const CounterMappingRegion &Other) const {
if (FileID != Other.FileID)
return false;
if (startLoc() > Other.startLoc())
return false;
if (endLoc() < Other.endLoc())
return false;
return true;
}
};
struct CountedRegion : public CounterMappingRegion {
uint64_t ExecutionCount;
CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount)
: CounterMappingRegion(R), ExecutionCount(ExecutionCount) {}
};
class CounterMappingContext {
ArrayRef<CounterExpression> Expressions;
ArrayRef<uint64_t> CounterValues;
public:
CounterMappingContext(ArrayRef<CounterExpression> Expressions,
ArrayRef<uint64_t> CounterValues = None)
: Expressions(Expressions), CounterValues(CounterValues) {}
void setCounts(ArrayRef<uint64_t> Counts) { CounterValues = Counts; }
void dump(const Counter &C, llvm::raw_ostream &OS) const;
void dump(const Counter &C) const { dump(C, dbgs()); }
ErrorOr<int64_t> evaluate(const Counter &C) const;
};
struct FunctionRecord {
std::string Name;
std::vector<std::string> Filenames;
std::vector<CountedRegion> CountedRegions;
uint64_t ExecutionCount;
FunctionRecord(StringRef Name, ArrayRef<StringRef> Filenames)
: Name(Name), Filenames(Filenames.begin(), Filenames.end()) {}
void pushRegion(CounterMappingRegion Region, uint64_t Count) {
if (CountedRegions.empty())
ExecutionCount = Count;
CountedRegions.emplace_back(Region, Count);
}
};
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(FunctionRecord, LLVMCoverageFunctionRef)
class FunctionRecordIterator
: public iterator_facade_base<FunctionRecordIterator,
std::forward_iterator_tag, FunctionRecord> {
ArrayRef<FunctionRecord> Records;
ArrayRef<FunctionRecord>::iterator Current;
StringRef Filename;
void skipOtherFiles();
public:
FunctionRecordIterator(ArrayRef<FunctionRecord> Records_,
StringRef Filename = "")
: Records(Records_), Current(Records.begin()), Filename(Filename) {
skipOtherFiles();
}
FunctionRecordIterator() : Current(Records.begin()) {}
bool operator==(const FunctionRecordIterator &RHS) const {
return Current == RHS.Current && Filename == RHS.Filename;
}
const FunctionRecord &operator*() const { return *Current; }
FunctionRecordIterator &operator++() {
assert(Current != Records.end() && "incremented past end");
++Current;
skipOtherFiles();
return *this;
}
};
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(FunctionRecordIterator,
LLVMCoverageFunctionRangeRef)
struct ExpansionRecord {
unsigned FileID;
const CountedRegion &Region;
const FunctionRecord &Function;
ExpansionRecord(const CountedRegion &Region,
const FunctionRecord &Function)
: FileID(Region.ExpandedFileID), Region(Region), Function(Function) {}
};
struct CoverageSegment {
unsigned Line;
unsigned Col;
uint64_t Count;
bool HasCount;
bool IsRegionEntry;
CoverageSegment(unsigned Line, unsigned Col, bool IsRegionEntry)
: Line(Line), Col(Col), Count(0), HasCount(false),
IsRegionEntry(IsRegionEntry) {}
CoverageSegment(unsigned Line, unsigned Col, uint64_t Count,
bool IsRegionEntry)
: Line(Line), Col(Col), Count(Count), HasCount(true),
IsRegionEntry(IsRegionEntry) {}
friend bool operator==(const CoverageSegment &L, const CoverageSegment &R) {
return std::tie(L.Line, L.Col, L.Count, L.HasCount, L.IsRegionEntry) ==
std::tie(R.Line, R.Col, R.Count, R.HasCount, R.IsRegionEntry);
}
};
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CoverageSegment, LLVMCoverageSegmentRef)
class CoverageData {
std::string Filename;
std::vector<CoverageSegment> Segments;
std::vector<ExpansionRecord> Expansions;
friend class CoverageMapping;
public:
CoverageData() {}
CoverageData(StringRef Filename) : Filename(Filename) {}
CoverageData(CoverageData &&RHS)
: Filename(std::move(RHS.Filename)), Segments(std::move(RHS.Segments)),
Expansions(std::move(RHS.Expansions)) {}
StringRef getFilename() { return Filename; }
std::vector<CoverageSegment>::iterator begin() { return Segments.begin(); }
std::vector<CoverageSegment>::iterator end() { return Segments.end(); }
size_t size() const { return Segments.size(); }
bool empty() const { return Segments.empty(); }
std::vector<ExpansionRecord> getExpansions() { return Expansions; }
};
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CoverageData, LLVMCoverageDataRef)
class CoverageMapping {
std::vector<FunctionRecord> Functions;
unsigned MismatchedFunctionCount;
CoverageMapping() : MismatchedFunctionCount(0) {}
public:
static ErrorOr<std::unique_ptr<CoverageMapping>>
load(CoverageMappingReader &CoverageReader,
IndexedInstrProfReader &ProfileReader);
static ErrorOr<std::unique_ptr<CoverageMapping>>
load(StringRef ObjectFilename, StringRef ProfileFilename,
StringRef Arch = StringRef());
unsigned getMismatchedCount() { return MismatchedFunctionCount; }
std::vector<StringRef> getUniqueSourceFiles() const;
CoverageData getCoverageForFile(StringRef Filename);
iterator_range<FunctionRecordIterator> getCoveredFunctions() const {
return make_range(FunctionRecordIterator(Functions),
FunctionRecordIterator());
}
iterator_range<FunctionRecordIterator>
getCoveredFunctions(StringRef Filename) const {
return make_range(FunctionRecordIterator(Functions, Filename),
FunctionRecordIterator());
}
std::vector<const FunctionRecord *> getInstantiations(StringRef Filename);
CoverageData getCoverageForFunction(const FunctionRecord &Function);
CoverageData getCoverageForExpansion(const ExpansionRecord &Expansion);
};
const std::error_category &coveragemap_category();
inline std::error_code make_error_code(coveragemap_error E) {
return std::error_code(static_cast<int>(E), coveragemap_category());
}
LLVM_PACKED_START
template <class IntPtrT> struct CovMapFunctionRecordV1 {
#define COVMAP_V1
#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
#include "llvm/ProfileData/InstrProfData.inc"
#undef COVMAP_V1
template <support::endianness Endian> uint64_t getFuncHash() const {
return support::endian::byte_swap<uint64_t, Endian>(FuncHash);
}
template <support::endianness Endian> uint32_t getDataSize() const {
return support::endian::byte_swap<uint32_t, Endian>(DataSize);
}
template <support::endianness Endian> IntPtrT getFuncNameRef() const {
return support::endian::byte_swap<IntPtrT, Endian>(NamePtr);
}
template <support::endianness Endian>
std::error_code getFuncName(InstrProfSymtab &ProfileNames,
StringRef &FuncName) const {
IntPtrT NameRef = getFuncNameRef<Endian>();
uint32_t NameS = support::endian::byte_swap<uint32_t, Endian>(NameSize);
FuncName = ProfileNames.getFuncName(NameRef, NameS);
if (NameS && FuncName.empty())
return coveragemap_error::malformed;
return std::error_code();
}
};
struct CovMapFunctionRecord {
#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
#include "llvm/ProfileData/InstrProfData.inc"
template <support::endianness Endian> uint64_t getFuncHash() const {
return support::endian::byte_swap<uint64_t, Endian>(FuncHash);
}
template <support::endianness Endian> uint32_t getDataSize() const {
return support::endian::byte_swap<uint32_t, Endian>(DataSize);
}
template <support::endianness Endian> uint64_t getFuncNameRef() const {
return support::endian::byte_swap<uint64_t, Endian>(NameRef);
}
template <support::endianness Endian>
std::error_code getFuncName(InstrProfSymtab &ProfileNames,
StringRef &FuncName) const {
uint64_t NameRef = getFuncNameRef<Endian>();
FuncName = ProfileNames.getFuncName(NameRef);
return std::error_code();
}
};
struct CovMapHeader {
#define COVMAP_HEADER(Type, LLVMType, Name, Init) Type Name;
#include "llvm/ProfileData/InstrProfData.inc"
template <support::endianness Endian> uint32_t getNRecords() const {
return support::endian::byte_swap<uint32_t, Endian>(NRecords);
}
template <support::endianness Endian> uint32_t getFilenamesSize() const {
return support::endian::byte_swap<uint32_t, Endian>(FilenamesSize);
}
template <support::endianness Endian> uint32_t getCoverageSize() const {
return support::endian::byte_swap<uint32_t, Endian>(CoverageSize);
}
template <support::endianness Endian> uint32_t getVersion() const {
return support::endian::byte_swap<uint32_t, Endian>(Version);
}
};
LLVM_PACKED_END
enum CovMapVersion {
Version1 = 0,
Version2 = 1,
CurrentVersion = INSTR_PROF_COVMAP_VERSION
};
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CoverageMapping, LLVMCoverageMappingRef)
template <int CovMapVersion, class IntPtrT> struct CovMapTraits {
typedef CovMapFunctionRecord CovMapFuncRecordType;
typedef uint64_t NameRefType;
};
template <class IntPtrT> struct CovMapTraits<CovMapVersion::Version1, IntPtrT> {
typedef CovMapFunctionRecordV1<IntPtrT> CovMapFuncRecordType;
typedef IntPtrT NameRefType;
};
}
template<> struct DenseMapInfo<coverage::CounterExpression> {
static inline coverage::CounterExpression getEmptyKey() {
using namespace coverage;
return CounterExpression(CounterExpression::ExprKind::Subtract,
Counter::getCounter(~0U),
Counter::getCounter(~0U));
}
static inline coverage::CounterExpression getTombstoneKey() {
using namespace coverage;
return CounterExpression(CounterExpression::ExprKind::Add,
Counter::getCounter(~0U),
Counter::getCounter(~0U));
}
static unsigned getHashValue(const coverage::CounterExpression &V) {
return static_cast<unsigned>(
hash_combine(V.Kind, V.LHS.getKind(), V.LHS.getCounterID(),
V.RHS.getKind(), V.RHS.getCounterID()));
}
static bool isEqual(const coverage::CounterExpression &LHS,
const coverage::CounterExpression &RHS) {
return LHS.Kind == RHS.Kind && LHS.LHS == RHS.LHS && LHS.RHS == RHS.RHS;
}
};
}
#endif // LLVM_PROFILEDATA_COVERAGEMAPPING_H_