#ifndef LLVM_PROFILEDATA_INSTRPROF_READER_H_
#define LLVM_PROFILEDATA_INSTRPROF_READER_H_
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/OnDiskHashTable.h"
#include <iterator>
namespace llvm {
class InstrProfReader;
struct InstrProfRecord {
InstrProfRecord() {}
InstrProfRecord(StringRef Name, uint64_t Hash, ArrayRef<uint64_t> Counts)
: Name(Name), Hash(Hash), Counts(Counts) {}
StringRef Name;
uint64_t Hash;
ArrayRef<uint64_t> Counts;
};
class InstrProfIterator : public std::iterator<std::input_iterator_tag,
InstrProfRecord> {
InstrProfReader *Reader;
InstrProfRecord Record;
void Increment();
public:
InstrProfIterator() : Reader(nullptr) {}
InstrProfIterator(InstrProfReader *Reader) : Reader(Reader) { Increment(); }
InstrProfIterator &operator++() { Increment(); return *this; }
bool operator==(const InstrProfIterator &RHS) { return Reader == RHS.Reader; }
bool operator!=(const InstrProfIterator &RHS) { return Reader != RHS.Reader; }
InstrProfRecord &operator*() { return Record; }
InstrProfRecord *operator->() { return &Record; }
};
class InstrProfReader {
error_code LastError;
public:
InstrProfReader() : LastError(instrprof_error::success) {}
virtual ~InstrProfReader() {}
virtual error_code readHeader() = 0;
virtual error_code readNextRecord(InstrProfRecord &Record) = 0;
InstrProfIterator begin() { return InstrProfIterator(this); }
InstrProfIterator end() { return InstrProfIterator(); }
protected:
error_code error(error_code EC) {
LastError = EC;
return EC;
}
error_code success() { return error(instrprof_error::success); }
public:
bool isEOF() { return LastError == instrprof_error::eof; }
bool hasError() { return LastError && !isEOF(); }
error_code getError() { return LastError; }
static error_code create(std::string Path,
std::unique_ptr<InstrProfReader> &Result);
};
class TextInstrProfReader : public InstrProfReader {
private:
std::unique_ptr<MemoryBuffer> DataBuffer;
line_iterator Line;
std::vector<uint64_t> Counts;
TextInstrProfReader(const TextInstrProfReader &) LLVM_DELETED_FUNCTION;
TextInstrProfReader &operator=(const TextInstrProfReader &)
LLVM_DELETED_FUNCTION;
public:
TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
: DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, '#') {}
error_code readHeader() override { return success(); }
error_code readNextRecord(InstrProfRecord &Record) override;
};
template <class IntPtrT>
class RawInstrProfReader : public InstrProfReader {
private:
std::unique_ptr<MemoryBuffer> DataBuffer;
std::vector<uint64_t> Counts;
struct ProfileData {
const uint32_t NameSize;
const uint32_t NumCounters;
const uint64_t FuncHash;
const IntPtrT NamePtr;
const IntPtrT CounterPtr;
};
struct RawHeader {
const uint64_t Magic;
const uint64_t Version;
const uint64_t DataSize;
const uint64_t CountersSize;
const uint64_t NamesSize;
const uint64_t CountersDelta;
const uint64_t NamesDelta;
};
bool ShouldSwapBytes;
uint64_t CountersDelta;
uint64_t NamesDelta;
const ProfileData *Data;
const ProfileData *DataEnd;
const uint64_t *CountersStart;
const char *NamesStart;
const char *ProfileEnd;
RawInstrProfReader(const TextInstrProfReader &) LLVM_DELETED_FUNCTION;
RawInstrProfReader &operator=(const TextInstrProfReader &)
LLVM_DELETED_FUNCTION;
public:
RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
: DataBuffer(std::move(DataBuffer)) { }
static bool hasFormat(const MemoryBuffer &DataBuffer);
error_code readHeader() override;
error_code readNextRecord(InstrProfRecord &Record) override;
private:
error_code readNextHeader(const char *CurrentPos);
error_code readHeader(const RawHeader &Header);
template <class IntT>
IntT swap(IntT Int) const {
return ShouldSwapBytes ? sys::SwapByteOrder(Int) : Int;
}
const uint64_t *getCounter(IntPtrT CounterPtr) const {
ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
return CountersStart + Offset;
}
const char *getName(IntPtrT NamePtr) const {
ptrdiff_t Offset = (swap(NamePtr) - NamesDelta) / sizeof(char);
return NamesStart + Offset;
}
};
typedef RawInstrProfReader<uint32_t> RawInstrProfReader32;
typedef RawInstrProfReader<uint64_t> RawInstrProfReader64;
namespace IndexedInstrProf {
enum class HashT : uint32_t;
}
class InstrProfLookupTrait {
std::vector<uint64_t> CountBuffer;
IndexedInstrProf::HashT HashType;
public:
InstrProfLookupTrait(IndexedInstrProf::HashT HashType) : HashType(HashType) {}
typedef InstrProfRecord data_type;
typedef StringRef internal_key_type;
typedef StringRef external_key_type;
typedef uint64_t hash_value_type;
typedef uint64_t offset_type;
static bool EqualKey(StringRef A, StringRef B) { return A == B; }
static StringRef GetInternalKey(StringRef K) { return K; }
hash_value_type ComputeHash(StringRef K);
static std::pair<offset_type, offset_type>
ReadKeyDataLength(const unsigned char *&D) {
using namespace support;
offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(D);
offset_type DataLen = endian::readNext<offset_type, little, unaligned>(D);
return std::make_pair(KeyLen, DataLen);
}
StringRef ReadKey(const unsigned char *D, offset_type N) {
return StringRef((const char *)D, N);
}
InstrProfRecord ReadData(StringRef K, const unsigned char *D, offset_type N) {
if (N < 2 * sizeof(uint64_t) || N % sizeof(uint64_t)) {
CountBuffer.clear();
return InstrProfRecord("", 0, CountBuffer);
}
using namespace support;
uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
unsigned NumCounters = N / sizeof(uint64_t) - 1;
CountBuffer.clear();
CountBuffer.reserve(NumCounters - 1);
for (unsigned I = 0; I < NumCounters; ++I)
CountBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
return InstrProfRecord(K, Hash, CountBuffer);
}
};
typedef OnDiskIterableChainedHashTable<InstrProfLookupTrait>
InstrProfReaderIndex;
class IndexedInstrProfReader : public InstrProfReader {
private:
std::unique_ptr<MemoryBuffer> DataBuffer;
std::unique_ptr<InstrProfReaderIndex> Index;
InstrProfReaderIndex::data_iterator RecordIterator;
uint64_t MaxFunctionCount;
IndexedInstrProfReader(const IndexedInstrProfReader &) LLVM_DELETED_FUNCTION;
IndexedInstrProfReader &operator=(const IndexedInstrProfReader &)
LLVM_DELETED_FUNCTION;
public:
IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
: DataBuffer(std::move(DataBuffer)), Index(nullptr),
RecordIterator(InstrProfReaderIndex::data_iterator()) {}
static bool hasFormat(const MemoryBuffer &DataBuffer);
error_code readHeader() override;
error_code readNextRecord(InstrProfRecord &Record) override;
error_code getFunctionCounts(StringRef FuncName, uint64_t &FuncHash,
std::vector<uint64_t> &Counts);
uint64_t getMaximumFunctionCount() { return MaxFunctionCount; }
static error_code create(std::string Path,
std::unique_ptr<IndexedInstrProfReader> &Result);
};
}
#endif // LLVM_PROFILEDATA_INSTRPROF_READER_H_