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); Index: unittests/Analysis/BlockFrequencyInfoTest.cpp =================================================================== --- /dev/null +++ unittests/Analysis/BlockFrequencyInfoTest.cpp @@ -0,0 +1,85 @@ +//===- BlockFrequencyInfoTest.cpp - BlockFrequencyInfo unit tests ---------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/BlockFrequencyInfo.h" +#include "llvm/Analysis/BlockFrequencyInfoImpl.h" +#include "llvm/Analysis/BranchProbabilityInfo.h" +#include "llvm/Analysis/LoopInfo.h" +#include "llvm/AsmParser/Parser.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/Dominators.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/raw_ostream.h" +#include "gtest/gtest.h" + +namespace llvm { +namespace { + +class BlockFrequencyInfoTest : public testing::Test { +protected: + std::unique_ptr BPI; + std::unique_ptr DT; + std::unique_ptr LI; + + BlockFrequencyInfo buildBFI(Function &F) { + DT.reset(new DominatorTree(F)); + LI.reset(new LoopInfo(*DT)); + BPI.reset(new BranchProbabilityInfo(F, *LI)); + return BlockFrequencyInfo(F, *BPI, *LI); + } + std::unique_ptr makeLLVMModule() { + const char *ModuleStrig = "define i32 @f(i32 %x) {\n" + "bb0:\n" + " %y1 = icmp eq i32 %x, 0 \n" + " br i1 %y1, label %bb1, label %bb2 \n" + "bb1:\n" + " br label %bb3\n" + "bb2:\n" + " br label %bb3\n" + "bb3:\n" + " %y2 = phi i32 [0, %bb1], [1, %bb2] \n" + " ret i32 %y2\n" + "}\n"; + LLVMContext &C = getGlobalContext(); + SMDiagnostic Err; + return parseAssemblyString(ModuleStrig, Err, C); + } +}; + +TEST_F(BlockFrequencyInfoTest, Basic) { + auto M = makeLLVMModule(); + Function *F = M->getFunction("f"); + F->setEntryCount(100); + + BlockFrequencyInfo BFI = buildBFI(*F); + BasicBlock &BB0 = F->getEntryBlock(); + BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0); + BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1); + BasicBlock *BB3 = BB1->getSingleSuccessor(); + + uint64_t BB0Freq = BFI.getBlockFreq(&BB0).getFrequency(); + uint64_t BB1Freq = BFI.getBlockFreq(BB1).getFrequency(); + uint64_t BB2Freq = BFI.getBlockFreq(BB2).getFrequency(); + uint64_t BB3Freq = BFI.getBlockFreq(BB3).getFrequency(); + + EXPECT_EQ(BB0Freq, BB3Freq); + EXPECT_EQ(BB0Freq, BB1Freq + BB2Freq); + EXPECT_EQ(BB0Freq, BB3Freq); + + EXPECT_EQ(BFI.getBlockProfileCount(&BB0).getValue(), 100); + EXPECT_EQ(BFI.getBlockProfileCount(BB3).getValue(), 100); + EXPECT_EQ(BFI.getBlockProfileCount(BB1).getValue(), 100 * BB1Freq / BB0Freq); + EXPECT_EQ(BFI.getBlockProfileCount(BB2).getValue(), 100 * BB2Freq / BB0Freq); +} + +} // end anonymous namespace +} // end namespace llvm Index: unittests/Analysis/CMakeLists.txt =================================================================== --- unittests/Analysis/CMakeLists.txt +++ unittests/Analysis/CMakeLists.txt @@ -7,6 +7,7 @@ add_llvm_unittest(AnalysisTests AliasAnalysisTest.cpp + BlockFrequencyInfoTest.cpp CallGraphTest.cpp CFGTest.cpp CGSCCPassManagerTest.cpp