1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 04:32:44 +01:00

Add getBlockProfileCount method to BlockFrequencyInfo

Differential Revision: http://reviews.llvm.org/D18233

llvm-svn: 264179
This commit is contained in:
Easwaran Raman 2016-03-23 18:18:26 +00:00
parent d3a96ba00d
commit 0f47538f64
6 changed files with 110 additions and 20 deletions

View File

@ -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 <climits>
@ -45,6 +46,11 @@ public:
/// 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<uint64_t> getBlockProfileCount(const BasicBlock *BB) const;
// Set the frequency of the given basic block.
void setBlockFreq(const BasicBlock *BB, uint64_t Freq);

View File

@ -185,18 +185,5 @@ SummaryEntryVector &ProfileSummary::getDetailedSummary() {
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

View File

@ -129,6 +129,20 @@ BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
return BFI ? BFI->getBlockFreq(BB) : 0;
}
Optional<uint64_t>
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");

View File

@ -34,7 +34,6 @@
#include "llvm/IR/Operator.h"
#include "llvm/IR/UseListOrder.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/ProfileData/ProfileCommon.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
@ -2575,14 +2574,11 @@ static void WriteFunction(
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);

View File

@ -0,0 +1,86 @@
//===- 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/DataTypes.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<BranchProbabilityInfo> BPI;
std::unique_ptr<DominatorTree> DT;
std::unique_ptr<LoopInfo> 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<Module> 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(), UINT64_C(100));
EXPECT_EQ(BFI.getBlockProfileCount(BB3).getValue(), UINT64_C(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

View File

@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
add_llvm_unittest(AnalysisTests
AliasAnalysisTest.cpp
BlockFrequencyInfoTest.cpp
CallGraphTest.cpp
CFGTest.cpp
CGSCCPassManagerTest.cpp