diff --git a/llvm/include/llvm/Analysis/ProfileSummaryInfo.h b/llvm/include/llvm/Analysis/ProfileSummaryInfo.h index e650e1c9d68909..9fcceb93dbc40e 100644 --- a/llvm/include/llvm/Analysis/ProfileSummaryInfo.h +++ b/llvm/include/llvm/Analysis/ProfileSummaryInfo.h @@ -52,7 +52,7 @@ class ProfileSummaryInfo { // percentile is above a large threshold. Optional HasLargeWorkingSetSize; // Compute the threshold for a given cutoff. - Optional computeThreshold(int PercentileCutoff); + Optional computeThreshold(int PercentileCutoff) const; // The map that caches the threshold values. The keys are the percentile // cutoff values and the values are the corresponding threshold values. mutable DenseMap ThresholdCache; @@ -68,19 +68,19 @@ class ProfileSummaryInfo { bool hasProfileSummary() const { return Summary != nullptr; } /// Returns true if module \c M has sample profile. - bool hasSampleProfile() { + bool hasSampleProfile() const { return hasProfileSummary() && Summary->getKind() == ProfileSummary::PSK_Sample; } /// Returns true if module \c M has instrumentation profile. - bool hasInstrumentationProfile() { + bool hasInstrumentationProfile() const { return hasProfileSummary() && Summary->getKind() == ProfileSummary::PSK_Instr; } /// Returns true if module \c M has context sensitive instrumentation profile. - bool hasCSInstrumentationProfile() { + bool hasCSInstrumentationProfile() const { return hasProfileSummary() && Summary->getKind() == ProfileSummary::PSK_CSInstr; } @@ -99,84 +99,86 @@ class ProfileSummaryInfo { /// Returns the profile count for \p CallInst. Optional getProfileCount(const CallBase &CallInst, BlockFrequencyInfo *BFI, - bool AllowSynthetic = false); + bool AllowSynthetic = false) const; /// Returns true if module \c M has partial-profile sample profile. - bool hasPartialSampleProfile(); + bool hasPartialSampleProfile() const; /// Returns true if the working set size of the code is considered huge. - bool hasHugeWorkingSetSize(); + bool hasHugeWorkingSetSize() const; /// Returns true if the working set size of the code is considered large. - bool hasLargeWorkingSetSize(); + bool hasLargeWorkingSetSize() const; /// Returns true if \p F has hot function entry. - bool isFunctionEntryHot(const Function *F); + bool isFunctionEntryHot(const Function *F) const; /// Returns true if \p F contains hot code. - bool isFunctionHotInCallGraph(const Function *F, BlockFrequencyInfo &BFI); + bool isFunctionHotInCallGraph(const Function *F, + BlockFrequencyInfo &BFI) const; /// Returns true if \p F has cold function entry. - bool isFunctionEntryCold(const Function *F); + bool isFunctionEntryCold(const Function *F) const; /// Returns true if \p F contains only cold code. - bool isFunctionColdInCallGraph(const Function *F, BlockFrequencyInfo &BFI); + bool isFunctionColdInCallGraph(const Function *F, + BlockFrequencyInfo &BFI) const; /// Returns true if the hotness of \p F is unknown. - bool isFunctionHotnessUnknown(const Function &F); + bool isFunctionHotnessUnknown(const Function &F) const; /// Returns true if \p F contains hot code with regard to a given hot /// percentile cutoff value. bool isFunctionHotInCallGraphNthPercentile(int PercentileCutoff, const Function *F, - BlockFrequencyInfo &BFI); + BlockFrequencyInfo &BFI) const; /// Returns true if \p F contains cold code with regard to a given cold /// percentile cutoff value. bool isFunctionColdInCallGraphNthPercentile(int PercentileCutoff, const Function *F, - BlockFrequencyInfo &BFI); + BlockFrequencyInfo &BFI) const; /// Returns true if count \p C is considered hot. - bool isHotCount(uint64_t C); + bool isHotCount(uint64_t C) const; /// Returns true if count \p C is considered cold. - bool isColdCount(uint64_t C); + bool isColdCount(uint64_t C) const; /// Returns true if count \p C is considered hot with regard to a given /// hot percentile cutoff value. - bool isHotCountNthPercentile(int PercentileCutoff, uint64_t C); + bool isHotCountNthPercentile(int PercentileCutoff, uint64_t C) const; /// Returns true if count \p C is considered cold with regard to a given /// cold percentile cutoff value. - bool isColdCountNthPercentile(int PercentileCutoff, uint64_t C); + bool isColdCountNthPercentile(int PercentileCutoff, uint64_t C) const; /// Returns true if BasicBlock \p BB is considered hot. - bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI); + bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const; /// Returns true if BasicBlock \p BB is considered cold. - bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI); + bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const; /// Returns true if BasicBlock \p BB is considered hot with regard to a given /// hot percentile cutoff value. - bool isHotBlockNthPercentile(int PercentileCutoff, - const BasicBlock *BB, BlockFrequencyInfo *BFI); + bool isHotBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB, + BlockFrequencyInfo *BFI) const; /// Returns true if BasicBlock \p BB is considered cold with regard to a given /// cold percentile cutoff value. - bool isColdBlockNthPercentile(int PercentileCutoff, - const BasicBlock *BB, BlockFrequencyInfo *BFI); + bool isColdBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB, + BlockFrequencyInfo *BFI) const; /// Returns true if the call site \p CB is considered hot. - bool isHotCallSite(const CallBase &CB, BlockFrequencyInfo *BFI); + bool isHotCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const; /// Returns true if call site \p CB is considered cold. - bool isColdCallSite(const CallBase &CB, BlockFrequencyInfo *BFI); + bool isColdCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const; /// Returns HotCountThreshold if set. Recompute HotCountThreshold /// if not set. - uint64_t getOrCompHotCountThreshold(); + uint64_t getOrCompHotCountThreshold() const; /// Returns ColdCountThreshold if set. Recompute HotCountThreshold /// if not set. - uint64_t getOrCompColdCountThreshold(); + uint64_t getOrCompColdCountThreshold() const; /// Returns HotCountThreshold if set. - uint64_t getHotCountThreshold() { + uint64_t getHotCountThreshold() const { return HotCountThreshold ? HotCountThreshold.getValue() : 0; } /// Returns ColdCountThreshold if set. - uint64_t getColdCountThreshold() { + uint64_t getColdCountThreshold() const { return ColdCountThreshold ? ColdCountThreshold.getValue() : 0; } private: - template - bool isFunctionHotOrColdInCallGraphNthPercentile(int PercentileCutoff, - const Function *F, - BlockFrequencyInfo &BFI); - template - bool isHotOrColdCountNthPercentile(int PercentileCutoff, uint64_t C); - template - bool isHotOrColdBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB, - BlockFrequencyInfo *BFI); + template + bool isFunctionHotOrColdInCallGraphNthPercentile( + int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const; + template + bool isHotOrColdCountNthPercentile(int PercentileCutoff, uint64_t C) const; + template + bool isHotOrColdBlockNthPercentile(int PercentileCutoff, + const BasicBlock *BB, + BlockFrequencyInfo *BFI) const; }; /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo. diff --git a/llvm/lib/Analysis/ProfileSummaryInfo.cpp b/llvm/lib/Analysis/ProfileSummaryInfo.cpp index ec7649c516e040..3360fd4c37c02b 100644 --- a/llvm/lib/Analysis/ProfileSummaryInfo.cpp +++ b/llvm/lib/Analysis/ProfileSummaryInfo.cpp @@ -106,9 +106,8 @@ void ProfileSummaryInfo::refresh() { computeThresholds(); } -Optional ProfileSummaryInfo::getProfileCount(const CallBase &Call, - BlockFrequencyInfo *BFI, - bool AllowSynthetic) { +Optional ProfileSummaryInfo::getProfileCount( + const CallBase &Call, BlockFrequencyInfo *BFI, bool AllowSynthetic) const { assert((isa(Call) || isa(Call)) && "We can only get profile count for call/invoke instruction."); if (hasSampleProfile()) { @@ -129,7 +128,7 @@ Optional ProfileSummaryInfo::getProfileCount(const CallBase &Call, /// Returns true if the function's entry is hot. If it returns false, it /// either means it is not hot or it is unknown whether it is hot or not (for /// example, no profile data is available). -bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) { +bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) const { if (!F || !hasProfileSummary()) return false; auto FunctionCount = F->getEntryCount(); @@ -144,8 +143,8 @@ bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) { /// hot total call edge count. /// If it returns false, it either means it is not hot or it is unknown /// (for example, no profile data is available). -bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F, - BlockFrequencyInfo &BFI) { +bool ProfileSummaryInfo::isFunctionHotInCallGraph( + const Function *F, BlockFrequencyInfo &BFI) const { if (!F || !hasProfileSummary()) return false; if (auto FunctionCount = F->getEntryCount()) @@ -173,8 +172,8 @@ bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F, /// the total call edge count is cold. /// If it returns false, it either means it is not cold or it is unknown /// (for example, no profile data is available). -bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F, - BlockFrequencyInfo &BFI) { +bool ProfileSummaryInfo::isFunctionColdInCallGraph( + const Function *F, BlockFrequencyInfo &BFI) const { if (!F || !hasProfileSummary()) return false; if (auto FunctionCount = F->getEntryCount()) @@ -197,14 +196,14 @@ bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F, return true; } -bool ProfileSummaryInfo::isFunctionHotnessUnknown(const Function &F) { +bool ProfileSummaryInfo::isFunctionHotnessUnknown(const Function &F) const { assert(hasPartialSampleProfile() && "Expect partial sample profile"); return !F.getEntryCount().hasValue(); } -template +template bool ProfileSummaryInfo::isFunctionHotOrColdInCallGraphNthPercentile( - int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) { + int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const { if (!F || !hasProfileSummary()) return false; if (auto FunctionCount = F->getEntryCount()) { @@ -238,13 +237,13 @@ bool ProfileSummaryInfo::isFunctionHotOrColdInCallGraphNthPercentile( // Like isFunctionHotInCallGraph but for a given cutoff. bool ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile( - int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) { + int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const { return isFunctionHotOrColdInCallGraphNthPercentile( PercentileCutoff, F, BFI); } bool ProfileSummaryInfo::isFunctionColdInCallGraphNthPercentile( - int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) { + int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const { return isFunctionHotOrColdInCallGraphNthPercentile( PercentileCutoff, F, BFI); } @@ -252,7 +251,7 @@ bool ProfileSummaryInfo::isFunctionColdInCallGraphNthPercentile( /// Returns true if the function's entry is a cold. If it returns false, it /// either means it is not cold or it is unknown whether it is cold or not (for /// example, no profile data is available). -bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) { +bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) const { if (!F) return false; if (F->hasFnAttribute(Attribute::Cold)) @@ -287,7 +286,8 @@ void ProfileSummaryInfo::computeThresholds() { HotEntry.NumCounts > ProfileSummaryLargeWorkingSetSizeThreshold; } -Optional ProfileSummaryInfo::computeThreshold(int PercentileCutoff) { +Optional +ProfileSummaryInfo::computeThreshold(int PercentileCutoff) const { if (!hasProfileSummary()) return None; auto iter = ThresholdCache.find(PercentileCutoff); @@ -302,25 +302,25 @@ Optional ProfileSummaryInfo::computeThreshold(int PercentileCutoff) { return CountThreshold; } -bool ProfileSummaryInfo::hasHugeWorkingSetSize() { +bool ProfileSummaryInfo::hasHugeWorkingSetSize() const { return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue(); } -bool ProfileSummaryInfo::hasLargeWorkingSetSize() { +bool ProfileSummaryInfo::hasLargeWorkingSetSize() const { return HasLargeWorkingSetSize && HasLargeWorkingSetSize.getValue(); } -bool ProfileSummaryInfo::isHotCount(uint64_t C) { +bool ProfileSummaryInfo::isHotCount(uint64_t C) const { return HotCountThreshold && C >= HotCountThreshold.getValue(); } -bool ProfileSummaryInfo::isColdCount(uint64_t C) { +bool ProfileSummaryInfo::isColdCount(uint64_t C) const { return ColdCountThreshold && C <= ColdCountThreshold.getValue(); } -template +template bool ProfileSummaryInfo::isHotOrColdCountNthPercentile(int PercentileCutoff, - uint64_t C) { + uint64_t C) const { auto CountThreshold = computeThreshold(PercentileCutoff); if (isHot) return CountThreshold && C >= CountThreshold.getValue(); @@ -328,37 +328,39 @@ bool ProfileSummaryInfo::isHotOrColdCountNthPercentile(int PercentileCutoff, return CountThreshold && C <= CountThreshold.getValue(); } -bool ProfileSummaryInfo::isHotCountNthPercentile(int PercentileCutoff, uint64_t C) { +bool ProfileSummaryInfo::isHotCountNthPercentile(int PercentileCutoff, + uint64_t C) const { return isHotOrColdCountNthPercentile(PercentileCutoff, C); } -bool ProfileSummaryInfo::isColdCountNthPercentile(int PercentileCutoff, uint64_t C) { +bool ProfileSummaryInfo::isColdCountNthPercentile(int PercentileCutoff, + uint64_t C) const { return isHotOrColdCountNthPercentile(PercentileCutoff, C); } -uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() { +uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() const { return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX; } -uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() { +uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() const { return ColdCountThreshold ? ColdCountThreshold.getValue() : 0; } -bool ProfileSummaryInfo::isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) { +bool ProfileSummaryInfo::isHotBlock(const BasicBlock *BB, + BlockFrequencyInfo *BFI) const { auto Count = BFI->getBlockProfileCount(BB); return Count && isHotCount(*Count); } bool ProfileSummaryInfo::isColdBlock(const BasicBlock *BB, - BlockFrequencyInfo *BFI) { + BlockFrequencyInfo *BFI) const { auto Count = BFI->getBlockProfileCount(BB); return Count && isColdCount(*Count); } -template -bool ProfileSummaryInfo::isHotOrColdBlockNthPercentile(int PercentileCutoff, - const BasicBlock *BB, - BlockFrequencyInfo *BFI) { +template +bool ProfileSummaryInfo::isHotOrColdBlockNthPercentile( + int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const { auto Count = BFI->getBlockProfileCount(BB); if (isHot) return Count && isHotCountNthPercentile(PercentileCutoff, *Count); @@ -366,26 +368,24 @@ bool ProfileSummaryInfo::isHotOrColdBlockNthPercentile(int PercentileCutoff, return Count && isColdCountNthPercentile(PercentileCutoff, *Count); } -bool ProfileSummaryInfo::isHotBlockNthPercentile(int PercentileCutoff, - const BasicBlock *BB, - BlockFrequencyInfo *BFI) { +bool ProfileSummaryInfo::isHotBlockNthPercentile( + int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const { return isHotOrColdBlockNthPercentile(PercentileCutoff, BB, BFI); } -bool ProfileSummaryInfo::isColdBlockNthPercentile(int PercentileCutoff, - const BasicBlock *BB, - BlockFrequencyInfo *BFI) { +bool ProfileSummaryInfo::isColdBlockNthPercentile( + int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const { return isHotOrColdBlockNthPercentile(PercentileCutoff, BB, BFI); } bool ProfileSummaryInfo::isHotCallSite(const CallBase &CB, - BlockFrequencyInfo *BFI) { + BlockFrequencyInfo *BFI) const { auto C = getProfileCount(CB, BFI); return C && isHotCount(*C); } bool ProfileSummaryInfo::isColdCallSite(const CallBase &CB, - BlockFrequencyInfo *BFI) { + BlockFrequencyInfo *BFI) const { auto C = getProfileCount(CB, BFI); if (C) return isColdCount(*C); @@ -395,7 +395,7 @@ bool ProfileSummaryInfo::isColdCallSite(const CallBase &CB, return hasSampleProfile() && CB.getCaller()->hasProfileData(); } -bool ProfileSummaryInfo::hasPartialSampleProfile() { +bool ProfileSummaryInfo::hasPartialSampleProfile() const { return hasProfileSummary() && Summary->getKind() == ProfileSummary::PSK_Sample && (PartialProfile || Summary->isPartialProfile());