#ifndef LLVM_IR_PROFILESUMMARY_H
#define LLVM_IR_PROFILESUMMARY_H
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <vector>
namespace llvm {
class LLVMContext;
class Metadata;
class raw_ostream;
struct ProfileSummaryEntry {
const uint32_t Cutoff; const uint64_t MinCount; const uint64_t NumCounts;
ProfileSummaryEntry(uint32_t TheCutoff, uint64_t TheMinCount,
uint64_t TheNumCounts)
: Cutoff(TheCutoff), MinCount(TheMinCount), NumCounts(TheNumCounts) {}
};
using SummaryEntryVector = std::vector<ProfileSummaryEntry>;
class ProfileSummary {
public:
enum Kind { PSK_Instr, PSK_CSInstr, PSK_Sample };
private:
const Kind PSK;
const SummaryEntryVector DetailedSummary;
const uint64_t TotalCount, MaxCount, MaxInternalCount, MaxFunctionCount;
const uint32_t NumCounts, NumFunctions;
bool Partial = false;
double PartialProfileRatio = 0.0;
Metadata *getDetailedSummaryMD(LLVMContext &Context);
public:
static const int Scale = 1000000;
ProfileSummary(Kind K, const SummaryEntryVector &DetailedSummary,
uint64_t TotalCount, uint64_t MaxCount,
uint64_t MaxInternalCount, uint64_t MaxFunctionCount,
uint32_t NumCounts, uint32_t NumFunctions,
bool Partial = false, double PartialProfileRatio = 0)
: PSK(K), DetailedSummary(std::move(DetailedSummary)),
TotalCount(TotalCount), MaxCount(MaxCount),
MaxInternalCount(MaxInternalCount), MaxFunctionCount(MaxFunctionCount),
NumCounts(NumCounts), NumFunctions(NumFunctions), Partial(Partial),
PartialProfileRatio(PartialProfileRatio) {}
Kind getKind() const { return PSK; }
Metadata *getMD(LLVMContext &Context, bool AddPartialField = true,
bool AddPartialProfileRatioField = true);
static ProfileSummary *getFromMD(Metadata *MD);
const SummaryEntryVector &getDetailedSummary() { return DetailedSummary; }
uint32_t getNumFunctions() const { return NumFunctions; }
uint64_t getMaxFunctionCount() const { return MaxFunctionCount; }
uint32_t getNumCounts() const { return NumCounts; }
uint64_t getTotalCount() const { return TotalCount; }
uint64_t getMaxCount() const { return MaxCount; }
uint64_t getMaxInternalCount() const { return MaxInternalCount; }
void setPartialProfile(bool PP) { Partial = PP; }
bool isPartialProfile() const { return Partial; }
double getPartialProfileRatio() const { return PartialProfileRatio; }
void setPartialProfileRatio(double R) {
assert(isPartialProfile() && "Unexpected when not partial profile");
PartialProfileRatio = R;
}
void printSummary(raw_ostream &OS) const;
void printDetailedSummary(raw_ostream &OS) const;
};
}
#endif