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