Index: include/llvm/Analysis/BlockFrequencyInfo.h =================================================================== --- include/llvm/Analysis/BlockFrequencyInfo.h +++ include/llvm/Analysis/BlockFrequencyInfo.h @@ -14,6 +14,7 @@ #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H #define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H +#include "llvm/ADT/Optional.h" #include "llvm/Pass.h" #include "llvm/Support/BlockFrequency.h" #include @@ -45,6 +46,11 @@ /// floating points. BlockFrequency getBlockFreq(const BasicBlock *BB) const; + /// \brief Returns the estimated profile count of \p BB. + /// This computes the relative block frequency of \p BB and multiplies it by + /// the enclosing function's count (if available) and returns the value. + Optional getBlockProfileCount(const BasicBlock *BB) const; + // Set the frequency of the given basic block. void setBlockFreq(const BasicBlock *BB, uint64_t Freq); Index: include/llvm/ProfileData/ProfileCommon.h =================================================================== --- include/llvm/ProfileData/ProfileCommon.h +++ include/llvm/ProfileData/ProfileCommon.h @@ -185,18 +185,5 @@ return DetailedSummary; } -/// Helper to compute the profile count for a block, based on the -/// ratio of its frequency to the entry block frequency, multiplied -/// by the entry block count. -inline uint64_t getBlockProfileCount(uint64_t BlockFreq, uint64_t EntryFreq, - uint64_t EntryCount) { - APInt ScaledCount(128, EntryCount); - APInt BlockFreqAPInt(128, BlockFreq); - APInt EntryFreqAPInt(128, EntryFreq); - ScaledCount *= BlockFreqAPInt; - ScaledCount = ScaledCount.udiv(EntryFreqAPInt); - return ScaledCount.getLimitedValue(); -} - } // end namespace llvm #endif Index: lib/Analysis/BlockFrequencyInfo.cpp =================================================================== --- lib/Analysis/BlockFrequencyInfo.cpp +++ lib/Analysis/BlockFrequencyInfo.cpp @@ -129,6 +129,20 @@ return BFI ? BFI->getBlockFreq(BB) : 0; } +Optional +BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const { + auto EntryCount = getFunction()->getEntryCount(); + if (!EntryCount) + return None; + // Use 128 bit APInt to do the arithmetic to avoid overflow. + APInt BlockCount(128, EntryCount.getValue()); + APInt BlockFreq(128, getBlockFreq(BB).getFrequency()); + APInt EntryFreq(128, getEntryFreq()); + BlockCount *= BlockFreq; + BlockCount = BlockCount.udiv(EntryFreq); + return BlockCount.getLimitedValue(); +} + void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) { assert(BFI && "Expected analysis to be available"); Index: lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- lib/Bitcode/Writer/BitcodeWriter.cpp +++ lib/Bitcode/Writer/BitcodeWriter.cpp @@ -2575,14 +2575,11 @@ auto *CalledFunction = CS.getCalledFunction(); if (CalledFunction && CalledFunction->hasName() && !CalledFunction->isIntrinsic()) { - uint64_t ScaledCount = 0; - if (HasProfileData) - ScaledCount = getBlockProfileCount( - BFI->getBlockFreq(&(*BB)).getFrequency(), BFI->getEntryFreq(), - F.getEntryCount().getValue()); + auto ScaledCount = BFI ? BFI->getBlockProfileCount(&*BB) : None; unsigned CalleeId = VE.getValueID( M->getValueSymbolTable().lookup(CalledFunction->getName())); - CallGraphEdges[CalleeId] += ScaledCount; + CallGraphEdges[CalleeId] += + (ScaledCount ? ScaledCount.getValue() : 0); } } findRefEdges(&*I, VE, RefEdges, Visited);