Create a wrapper pass for BlockFrequencyInfo.

This is useful when we want to do block frequency analysis
conditionally (e.g. only in PGO mode) but don't want to add
one more pass dependence.

Patch by congh.
Approved by dexonsmith.
Differential Revision: http://reviews.llvm.org/D11196

llvm-svn: 242248
This commit is contained in:
Wei Mi 2015-07-14 23:40:50 +00:00
parent ff7f4d08cf
commit 3e99add8f8
5 changed files with 80 additions and 49 deletions

View File

@ -21,26 +21,16 @@
namespace llvm {
class BranchProbabilityInfo;
class LoopInfo;
template <class BlockT> class BlockFrequencyInfoImpl;
/// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
/// estimate IR basic block frequencies.
class BlockFrequencyInfo : public FunctionPass {
class BlockFrequencyInfo {
typedef BlockFrequencyInfoImpl<BasicBlock> ImplType;
std::unique_ptr<ImplType> BFI;
public:
static char ID;
BlockFrequencyInfo();
~BlockFrequencyInfo() override;
void getAnalysisUsage(AnalysisUsage &AU) const override;
bool runOnFunction(Function &F) override;
void releaseMemory() override;
void print(raw_ostream &O, const Module *M) const override;
const Function *getFunction() const;
void view() const;
@ -51,6 +41,10 @@ public:
/// floating points.
BlockFrequency getBlockFreq(const BasicBlock *BB) const;
/// calculate - compute block frequency info for the given function.
void calculate(const Function &F, const BranchProbabilityInfo &BPI,
const LoopInfo &LI);
// Print the block frequency Freq to OS using the current functions entry
// frequency to convert freq into a relative decimal form.
raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
@ -60,7 +54,28 @@ public:
raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
uint64_t getEntryFreq() const;
void releaseMemory();
void print(raw_ostream &OS) const;
};
/// \brief Legacy analysis pass which computes \c BlockFrequencyInfo.
class BlockFrequencyInfoWrapperPass : public FunctionPass {
BlockFrequencyInfo BFI;
public:
static char ID;
BlockFrequencyInfoWrapperPass();
~BlockFrequencyInfoWrapperPass() override;
BlockFrequencyInfo &getBFI() { return BFI; }
const BlockFrequencyInfo &getBFI() const { return BFI; }
void getAnalysisUsage(AnalysisUsage &AU) const override;
bool runOnFunction(Function &F) override;
void releaseMemory() override;
void print(raw_ostream &OS, const Module *M) const override;
};
}

View File

@ -79,7 +79,7 @@ void initializeBarrierNoopPass(PassRegistry&);
void initializeBasicAliasAnalysisPass(PassRegistry&);
void initializeCallGraphWrapperPassPass(PassRegistry &);
void initializeBlockExtractorPassPass(PassRegistry&);
void initializeBlockFrequencyInfoPass(PassRegistry&);
void initializeBlockFrequencyInfoWrapperPassPass(PassRegistry&);
void initializeBoundsCheckingPass(PassRegistry&);
void initializeBranchFolderPassPass(PassRegistry&);
void initializeBranchProbabilityInfoPass(PassRegistry&);

View File

@ -27,7 +27,7 @@ void llvm::initializeAnalysis(PassRegistry &Registry) {
initializeAliasSetPrinterPass(Registry);
initializeNoAAPass(Registry);
initializeBasicAliasAnalysisPass(Registry);
initializeBlockFrequencyInfoPass(Registry);
initializeBlockFrequencyInfoWrapperPassPass(Registry);
initializeBranchProbabilityInfoPass(Registry);
initializeCostModelAnalysisPass(Registry);
initializeCFGViewerPass(Registry);

View File

@ -105,31 +105,9 @@ struct DOTGraphTraits<BlockFrequencyInfo*> : public DefaultDOTGraphTraits {
} // end namespace llvm
#endif
INITIALIZE_PASS_BEGIN(BlockFrequencyInfo, "block-freq",
"Block Frequency Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfo)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_END(BlockFrequencyInfo, "block-freq",
"Block Frequency Analysis", true, true)
char BlockFrequencyInfo::ID = 0;
BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) {
initializeBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
}
BlockFrequencyInfo::~BlockFrequencyInfo() {}
void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<BranchProbabilityInfo>();
AU.addRequired<LoopInfoWrapperPass>();
AU.setPreservesAll();
}
bool BlockFrequencyInfo::runOnFunction(Function &F) {
BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
void BlockFrequencyInfo::calculate(const Function &F,
const BranchProbabilityInfo &BPI,
const LoopInfo &LI) {
if (!BFI)
BFI.reset(new ImplType);
BFI->doFunction(&F, &BPI, &LI);
@ -137,13 +115,6 @@ bool BlockFrequencyInfo::runOnFunction(Function &F) {
if (ViewBlockFreqPropagationDAG != GVDT_None)
view();
#endif
return false;
}
void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
void BlockFrequencyInfo::print(raw_ostream &O, const Module *) const {
if (BFI) BFI->print(O);
}
BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
@ -180,3 +151,48 @@ BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
uint64_t BlockFrequencyInfo::getEntryFreq() const {
return BFI ? BFI->getEntryFreq() : 0;
}
void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
void BlockFrequencyInfo::print(raw_ostream &OS) const {
if (BFI)
BFI->print(OS);
}
INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
"Block Frequency Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfo)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
"Block Frequency Analysis", true, true)
char BlockFrequencyInfoWrapperPass::ID = 0;
BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
: FunctionPass(ID) {
initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
}
BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {}
void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
const Module *) const {
BFI.print(OS);
}
void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<BranchProbabilityInfo>();
AU.addRequired<LoopInfoWrapperPass>();
AU.setPreservesAll();
}
void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
BFI.calculate(F, BPI, LI);
return false;
}

View File

@ -1454,7 +1454,7 @@ struct LoopVectorize : public FunctionPass {
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
BFI = &getAnalysis<BlockFrequencyInfo>();
BFI = &getAnalysis<BlockFrequencyInfoWrapperPass>().getBFI();
auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
TLI = TLIP ? &TLIP->getTLI() : nullptr;
AA = &getAnalysis<AliasAnalysis>();
@ -1703,7 +1703,7 @@ struct LoopVectorize : public FunctionPass {
AU.addRequired<AssumptionCacheTracker>();
AU.addRequiredID(LoopSimplifyID);
AU.addRequiredID(LCSSAID);
AU.addRequired<BlockFrequencyInfo>();
AU.addRequired<BlockFrequencyInfoWrapperPass>();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<LoopInfoWrapperPass>();
AU.addRequired<ScalarEvolution>();
@ -5242,7 +5242,7 @@ INITIALIZE_PASS_BEGIN(LoopVectorize, LV_NAME, lv_name, false, false)
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfo)
INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
INITIALIZE_PASS_DEPENDENCY(LCSSA)