1f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===- LazyBlockFrequencyInfo.h - Lazy Block Frequency Analysis -*- 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 BlockFrequencyInfoWrapperPass. The 11f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// difference is that with this pass the block frequencies are not computed when 12f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// the analysis pass is executed but rather when the BFI result is explicitly 13f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// requested by the analysis client. 14f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// 15f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===----------------------------------------------------------------------===// 16f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 17f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#ifndef LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H 18f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#define LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H 19f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 20f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/Analysis/BlockFrequencyInfo.h" 21f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/Analysis/LazyBranchProbabilityInfo.h" 22f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/Pass.h" 23f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 24f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotnamespace llvm { 25f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass AnalysisUsage; 26f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass BranchProbabilityInfo; 27f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass Function; 28f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass LoopInfo; 29f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 30f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// Wraps a BFI to allow lazy computation of the block frequencies. 31f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 32f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// A pass that only conditionally uses BFI can uncondtionally require the 33f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// analysis without paying for the overhead if BFI doesn't end up being used. 34f3014761c955345d6e05491608e73228d014afbandroid-build-team Robottemplate <typename FunctionT, typename BranchProbabilityInfoPassT, 35f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot typename LoopInfoT, typename BlockFrequencyInfoT> 36f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass LazyBlockFrequencyInfo { 37f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotpublic: 38f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot LazyBlockFrequencyInfo() 39f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot : Calculated(false), F(nullptr), BPIPass(nullptr), LI(nullptr) {} 40f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 41f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Set up the per-function input. 42f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void setAnalysis(const FunctionT *F, BranchProbabilityInfoPassT *BPIPass, 43f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const LoopInfoT *LI) { 44f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot this->F = F; 45f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot this->BPIPass = BPIPass; 46f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot this->LI = LI; 47f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 48f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 49f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Retrieve the BFI with the block frequencies computed. 50f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BlockFrequencyInfoT &getCalculated() { 51f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot if (!Calculated) { 52f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot assert(F && BPIPass && LI && "call setAnalysis"); 53f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BFI.calculate( 54f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot *F, BPIPassTrait<BranchProbabilityInfoPassT>::getBPI(BPIPass), *LI); 55f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Calculated = true; 56f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 57f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return BFI; 58f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 59f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 60f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const BlockFrequencyInfoT &getCalculated() const { 61f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot return const_cast<LazyBlockFrequencyInfo *>(this)->getCalculated(); 62f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 63f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 64f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void releaseMemory() { 65f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BFI.releaseMemory(); 66f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot Calculated = false; 67f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot setAnalysis(nullptr, nullptr, nullptr); 68f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot } 69f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 70f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotprivate: 71f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BlockFrequencyInfoT BFI; 72f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool Calculated; 73f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const FunctionT *F; 74f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BranchProbabilityInfoPassT *BPIPass; 75f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const LoopInfoT *LI; 76f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot}; 77f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 78f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// \brief This is an alternative analysis pass to 79f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// BlockFrequencyInfoWrapperPass. The difference is that with this pass the 80f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// block frequencies are not computed when the analysis pass is executed but 81f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// rather when the BFI result is explicitly requested by the analysis client. 82f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 83f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// There are some additional requirements for any client pass that wants to use 84f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// the analysis: 85f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 86f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 1. The pass needs to initialize dependent passes with: 87f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 88f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// INITIALIZE_PASS_DEPENDENCY(LazyBFIPass) 89f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 90f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 2. Similarly, getAnalysisUsage should call: 91f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 92f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU) 93f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 94f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 3. The computed BFI should be requested with 95f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() before either LoopInfo 96f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// or BPI could be invalidated for example by changing the CFG. 97f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// 98f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// Note that it is expected that we wouldn't need this functionality for the 99f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// new PM since with the new PM, analyses are executed on demand. 100f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 101f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass LazyBlockFrequencyInfoPass : public FunctionPass { 102f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotprivate: 103f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot LazyBlockFrequencyInfo<Function, LazyBranchProbabilityInfoPass, LoopInfo, 104f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BlockFrequencyInfo> 105f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot LBFI; 106f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 107f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotpublic: 108f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static char ID; 109f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 110f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot LazyBlockFrequencyInfoPass(); 111f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 112f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Compute and return the block frequencies. 113f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot BlockFrequencyInfo &getBFI() { return LBFI.getCalculated(); } 114f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 115f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// \brief Compute and return the block frequencies. 116f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot const BlockFrequencyInfo &getBFI() const { return LBFI.getCalculated(); } 117f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 118f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void getAnalysisUsage(AnalysisUsage &AU) const override; 119f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 120f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// Helper for client passes to set up the analysis usage on behalf of this 121f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot /// pass. 122f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot static void getLazyBFIAnalysisUsage(AnalysisUsage &AU); 123f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 124f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot bool runOnFunction(Function &F) override; 125f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void releaseMemory() override; 126f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot void print(raw_ostream &OS, const Module *M) const override; 127f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot}; 128f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot 129f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot/// \brief Helper for client passes to initialize dependent passes for LBFI. 130f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotvoid initializeLazyBFIPassPass(PassRegistry &Registry); 131f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot} 132f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#endif 133