#ifndef LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
#define LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Support/CommandLine.h"
namespace llvm {
extern cl::opt<bool> EnablePGSO;
extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
extern cl::opt<bool> PGSOColdCodeOnly;
extern cl::opt<bool> PGSOColdCodeOnlyForInstrPGO;
extern cl::opt<bool> PGSOColdCodeOnlyForSamplePGO;
extern cl::opt<bool> PGSOColdCodeOnlyForPartialSamplePGO;
extern cl::opt<bool> ForcePGSO;
extern cl::opt<int> PgsoCutoffInstrProf;
extern cl::opt<int> PgsoCutoffSampleProf;
class BasicBlock;
class BlockFrequencyInfo;
class Function;
enum class PGSOQueryType {
IRPass, Test, Other, };
static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
return PGSOColdCodeOnly ||
(PSI->hasInstrumentationProfile() && PGSOColdCodeOnlyForInstrPGO) ||
(PSI->hasSampleProfile() &&
((!PSI->hasPartialSampleProfile() && PGSOColdCodeOnlyForSamplePGO) ||
(PSI->hasPartialSampleProfile() &&
PGSOColdCodeOnlyForPartialSamplePGO))) ||
(PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize());
}
template<typename AdapterT, typename FuncT, typename BFIT>
bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI,
BFIT *BFI, PGSOQueryType QueryType) {
assert(F);
if (!PSI || !BFI || !PSI->hasProfileSummary())
return false;
if (ForcePGSO)
return true;
if (!EnablePGSO)
return false;
if (isPGSOColdCodeOnly(PSI))
return AdapterT::isFunctionColdInCallGraph(F, PSI, *BFI);
if (PSI->hasSampleProfile())
return AdapterT::isFunctionColdInCallGraphNthPercentile(
PgsoCutoffSampleProf, F, PSI, *BFI);
return !AdapterT::isFunctionHotInCallGraphNthPercentile(PgsoCutoffInstrProf,
F, PSI, *BFI);
}
template<typename AdapterT, typename BlockTOrBlockFreq, typename BFIT>
bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq, ProfileSummaryInfo *PSI,
BFIT *BFI, PGSOQueryType QueryType) {
if (!PSI || !BFI || !PSI->hasProfileSummary())
return false;
if (ForcePGSO)
return true;
if (!EnablePGSO)
return false;
if (isPGSOColdCodeOnly(PSI))
return AdapterT::isColdBlock(BBOrBlockFreq, PSI, BFI);
if (PSI->hasSampleProfile())
return AdapterT::isColdBlockNthPercentile(PgsoCutoffSampleProf,
BBOrBlockFreq, PSI, BFI);
return !AdapterT::isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq,
PSI, BFI);
}
bool shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
BlockFrequencyInfo *BFI,
PGSOQueryType QueryType = PGSOQueryType::Other);
bool shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
BlockFrequencyInfo *BFI,
PGSOQueryType QueryType = PGSOQueryType::Other);
}
#endif