#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/iterator.h"
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/raw_ostream.h"
#include <system_error>
namespace llvm {
class IndexedInstrProfReader;
namespace coverage {
class ObjectFileCoverageMappingReader;
class CoverageMapping;
struct CounterExpressions;
enum CoverageMappingVersion { CoverageMappingVersion1 };
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; }
bool operator==(const Counter &Other) const {
return Kind == Other.Kind && ID == Other.ID;
}
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
};
static const unsigned EncodingHasCodeBeforeBits = 1;
Counter Count;
unsigned FileID, ExpandedFileID;
unsigned LineStart, ColumnStart, LineEnd, ColumnEnd;
RegionKind Kind;
bool HasCodeBefore;
CounterMappingRegion(Counter Count, unsigned FileID, unsigned LineStart,
unsigned ColumnStart, unsigned LineEnd,
unsigned ColumnEnd, bool HasCodeBefore = false,
RegionKind Kind = CodeRegion)
: Count(Count), FileID(FileID), ExpandedFileID(0), LineStart(LineStart),
ColumnStart(ColumnStart), LineEnd(LineEnd), ColumnEnd(ColumnEnd),
Kind(Kind), HasCodeBefore(HasCodeBefore) {}
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 = ArrayRef<uint64_t>())
: Expressions(Expressions), CounterValues(CounterValues) {}
void dump(const Counter &C, llvm::raw_ostream &OS) const;
void dump(const Counter &C) const { dump(C, llvm::outs()); }
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,
uint64_t ExecutionCount)
: Name(Name), Filenames(Filenames.begin(), Filenames.end()),
ExecutionCount(ExecutionCount) {}
};
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) {}
void setCount(uint64_t NewCount) {
Count = NewCount;
HasCount = true;
}
void addCount(uint64_t NewCount) { setCount(Count + NewCount); }
};
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(ObjectFileCoverageMappingReader &CoverageReader,
IndexedInstrProfReader &ProfileReader);
static ErrorOr<std::unique_ptr<CoverageMapping>>
load(StringRef ObjectFilename, StringRef ProfileFilename);
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);
};
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CoverageMapping, LLVMCoverageMappingRef)
}
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_