File indexing completed on 2026-05-10 08:44:44
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
0014 #define LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
0015
0016 #include "llvm/Analysis/ProfileSummaryInfo.h"
0017 #include "llvm/Support/CommandLine.h"
0018
0019 namespace llvm {
0020 extern cl::opt<bool> EnablePGSO;
0021 extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
0022 extern cl::opt<bool> PGSOColdCodeOnly;
0023 extern cl::opt<bool> PGSOColdCodeOnlyForInstrPGO;
0024 extern cl::opt<bool> PGSOColdCodeOnlyForSamplePGO;
0025 extern cl::opt<bool> PGSOColdCodeOnlyForPartialSamplePGO;
0026 extern cl::opt<bool> ForcePGSO;
0027 extern cl::opt<int> PgsoCutoffInstrProf;
0028 extern cl::opt<int> PgsoCutoffSampleProf;
0029
0030 class BasicBlock;
0031 class BlockFrequencyInfo;
0032 class Function;
0033
0034 enum class PGSOQueryType {
0035 IRPass,
0036 Test,
0037 Other,
0038 };
0039
0040 static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
0041 return PGSOColdCodeOnly ||
0042 (PSI->hasInstrumentationProfile() && PGSOColdCodeOnlyForInstrPGO) ||
0043 (PSI->hasSampleProfile() &&
0044 ((!PSI->hasPartialSampleProfile() && PGSOColdCodeOnlyForSamplePGO) ||
0045 (PSI->hasPartialSampleProfile() &&
0046 PGSOColdCodeOnlyForPartialSamplePGO))) ||
0047 (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize());
0048 }
0049
0050 template <typename FuncT, typename BFIT>
0051 bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI,
0052 BFIT *BFI, PGSOQueryType QueryType) {
0053 assert(F);
0054 if (!PSI || !BFI || !PSI->hasProfileSummary())
0055 return false;
0056 if (ForcePGSO)
0057 return true;
0058 if (!EnablePGSO)
0059 return false;
0060 if (isPGSOColdCodeOnly(PSI))
0061 return PSI->isFunctionColdInCallGraph(F, *BFI);
0062 if (PSI->hasSampleProfile())
0063
0064
0065 return PSI->isFunctionColdInCallGraphNthPercentile(PgsoCutoffSampleProf, F,
0066 *BFI);
0067 return !PSI->isFunctionHotInCallGraphNthPercentile(PgsoCutoffInstrProf, F,
0068 *BFI);
0069 }
0070
0071 template <typename BlockTOrBlockFreq, typename BFIT>
0072 bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq,
0073 ProfileSummaryInfo *PSI, BFIT *BFI,
0074 PGSOQueryType QueryType) {
0075 if (!PSI || !BFI || !PSI->hasProfileSummary())
0076 return false;
0077 if (ForcePGSO)
0078 return true;
0079 if (!EnablePGSO)
0080 return false;
0081 if (isPGSOColdCodeOnly(PSI))
0082 return PSI->isColdBlock(BBOrBlockFreq, BFI);
0083 if (PSI->hasSampleProfile())
0084
0085
0086 return PSI->isColdBlockNthPercentile(PgsoCutoffSampleProf, BBOrBlockFreq,
0087 BFI);
0088 return !PSI->isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq, BFI);
0089 }
0090
0091
0092
0093 bool shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
0094 BlockFrequencyInfo *BFI,
0095 PGSOQueryType QueryType = PGSOQueryType::Other);
0096
0097
0098
0099 bool shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
0100 BlockFrequencyInfo *BFI,
0101 PGSOQueryType QueryType = PGSOQueryType::Other);
0102
0103 }
0104
0105 #endif