1f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===- LazyBranchProbabilityInfo.h - Lazy Branch Probability ----*- C++ -*-===// 2f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 3f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// The LLVM Compiler Infrastructure 4f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 5f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// This file is distributed under the University of Illinois Open Source 6f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// License. See LICENSE.TXT for details. 7f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 8f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===----------------------------------------------------------------------===// 9f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 10f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// This is an alternative analysis pass to BranchProbabilityInfoWrapperPass. 11f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// The difference is that with this pass the branch probabilities are not 12f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// computed when the analysis pass is executed but rather when the BPI results 13f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// is explicitly requested by the analysis client. 14f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 15f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===----------------------------------------------------------------------===// 16f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 17f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#ifndef LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H 18f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#define LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H 19f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 20f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/Analysis/BranchProbabilityInfo.h" 21f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/Pass.h" 22f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 23f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotnamespace llvm { 24f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass AnalysisUsage; 25f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass Function; 26f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass LoopInfo; 27f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass TargetLibraryInfo; 28f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 29f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// \brief This is an alternative analysis pass to 30f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// BranchProbabilityInfoWrapperPass. The difference is that with this pass the 31f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// branch probabilities are not computed when the analysis pass is executed but 32f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// rather when the BPI results is explicitly requested by the analysis client. 33f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 34f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// There are some additional requirements for any client pass that wants to use 35f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// the analysis: 36f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 37f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 1. The pass needs to initialize dependent passes with: 38f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 39f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// INITIALIZE_PASS_DEPENDENCY(LazyBPIPass) 40f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 41f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 2. Similarly, getAnalysisUsage should call: 42f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 43f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU) 44f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 45f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 3. The computed BPI should be requested with 46f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// getAnalysis<LazyBranchProbabilityInfoPass>().getBPI() before LoopInfo 47f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// could be invalidated for example by changing the CFG. 48f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 49f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// Note that it is expected that we wouldn't need this functionality for the 50f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// new PM since with the new PM, analyses are executed on demand. 51f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass LazyBranchProbabilityInfoPass : public FunctionPass { 52f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 53f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Wraps a BPI to allow lazy computation of the branch probabilities. 54f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// 55f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// A pass that only conditionally uses BPI can uncondtionally require the 56f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// analysis without paying for the overhead if BPI doesn't end up being used. 57f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot class LazyBranchProbabilityInfo { 58f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot public: 59f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot LazyBranchProbabilityInfo(const Function *F, const LoopInfo *LI, 60f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const TargetLibraryInfo *TLI) 61f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot : Calculated(false), F(F), LI(LI), TLI(TLI) {} 62f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 63f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Retrieve the BPI with the branch probabilities computed. 64f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BranchProbabilityInfo &getCalculated() { 65f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot if (!Calculated) { 66f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot assert(F && LI && "call setAnalysis"); 67f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BPI.calculate(*F, *LI, TLI); 68f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Calculated = true; 69f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 70f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return BPI; 71f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 72f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 73f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const BranchProbabilityInfo &getCalculated() const { 74f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<LazyBranchProbabilityInfo *>(this)->getCalculated(); 75f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 76f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 77f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot private: 78f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BranchProbabilityInfo BPI; 79f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool Calculated; 80f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const Function *F; 81f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const LoopInfo *LI; 82f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const TargetLibraryInfo *TLI; 83f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot }; 84f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 85f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot std::unique_ptr<LazyBranchProbabilityInfo> LBPI; 86f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 87f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotpublic: 88f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static char ID; 89f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 90f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot LazyBranchProbabilityInfoPass(); 91f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 92f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Compute and return the branch probabilities. 93f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BranchProbabilityInfo &getBPI() { return LBPI->getCalculated(); } 94f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 95f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Compute and return the branch probabilities. 96f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const BranchProbabilityInfo &getBPI() const { return LBPI->getCalculated(); } 97f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 98f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void getAnalysisUsage(AnalysisUsage &AU) const override; 99f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 100f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Helper for client passes to set up the analysis usage on behalf of this 101f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// pass. 102f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static void getLazyBPIAnalysisUsage(AnalysisUsage &AU); 103f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 104f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool runOnFunction(Function &F) override; 105f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void releaseMemory() override; 106f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void print(raw_ostream &OS, const Module *M) const override; 107f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot}; 108f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 109f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// \brief Helper for client passes to initialize dependent passes for LBPI. 110f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotvoid initializeLazyBPIPassPass(PassRegistry &Registry); 111f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 112f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// \brief Simple trait class that provides a mapping between BPI passes and the 113f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// corresponding BPInfo. 114f3014761c955345d6e05491608e73228d014afbandroid-build-team Robottemplate <typename PassT> struct BPIPassTrait { 115f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static PassT &getBPI(PassT *P) { return *P; } 116f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot}; 117f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 118f3014761c955345d6e05491608e73228d014afbandroid-build-team Robottemplate <> struct BPIPassTrait<LazyBranchProbabilityInfoPass> { 119f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static BranchProbabilityInfo &getBPI(LazyBranchProbabilityInfoPass *P) { 120f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return P->getBPI(); 121f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 122f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot}; 123f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot} 124f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#endif 125