71 lines · cpp
1//===- LazyBlockFrequencyInfo.cpp - Lazy Block Frequency Analysis ---------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This is an alternative analysis pass to BlockFrequencyInfoWrapperPass. The10// difference is that with this pass the block frequencies are not computed when11// the analysis pass is executed but rather when the BFI result is explicitly12// requested by the analysis client.13//14//===----------------------------------------------------------------------===//15 16#include "llvm/Analysis/LazyBlockFrequencyInfo.h"17#include "llvm/Analysis/LazyBranchProbabilityInfo.h"18#include "llvm/Analysis/LoopInfo.h"19#include "llvm/IR/Dominators.h"20#include "llvm/InitializePasses.h"21 22using namespace llvm;23 24#define DEBUG_TYPE "lazy-block-freq"25 26INITIALIZE_PASS_BEGIN(LazyBlockFrequencyInfoPass, DEBUG_TYPE,27 "Lazy Block Frequency Analysis", true, true)28INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)29INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)30INITIALIZE_PASS_END(LazyBlockFrequencyInfoPass, DEBUG_TYPE,31 "Lazy Block Frequency Analysis", true, true)32 33char LazyBlockFrequencyInfoPass::ID = 0;34 35LazyBlockFrequencyInfoPass::LazyBlockFrequencyInfoPass() : FunctionPass(ID) {}36 37void LazyBlockFrequencyInfoPass::print(raw_ostream &OS, const Module *) const {38 LBFI.getCalculated().print(OS);39}40 41void LazyBlockFrequencyInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {42 LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU);43 // We require DT so it's available when LI is available. The LI updating code44 // asserts that DT is also present so if we don't make sure that we have DT45 // here, that assert will trigger.46 AU.addRequiredTransitive<DominatorTreeWrapperPass>();47 AU.addRequiredTransitive<LoopInfoWrapperPass>();48 AU.setPreservesAll();49}50 51void LazyBlockFrequencyInfoPass::releaseMemory() { LBFI.releaseMemory(); }52 53bool LazyBlockFrequencyInfoPass::runOnFunction(Function &F) {54 auto &BPIPass = getAnalysis<LazyBranchProbabilityInfoPass>();55 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();56 LBFI.setAnalysis(&F, &BPIPass, &LI);57 return false;58}59 60void LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AnalysisUsage &AU) {61 LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU);62 AU.addRequiredTransitive<LazyBlockFrequencyInfoPass>();63 AU.addRequiredTransitive<LoopInfoWrapperPass>();64}65 66void llvm::initializeLazyBFIPassPass(PassRegistry &Registry) {67 initializeLazyBPIPassPass(Registry);68 INITIALIZE_PASS_DEPENDENCY(LazyBlockFrequencyInfoPass);69 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);70}71