106f32e7eSjoerg //===- BlockFrequencyInfo.cpp - Block Frequency Analysis ------------------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // Loops should be simplified before this analysis.
1006f32e7eSjoerg //
1106f32e7eSjoerg //===----------------------------------------------------------------------===//
1206f32e7eSjoerg 
1306f32e7eSjoerg #include "llvm/Analysis/BlockFrequencyInfo.h"
1406f32e7eSjoerg #include "llvm/ADT/APInt.h"
1506f32e7eSjoerg #include "llvm/ADT/None.h"
1606f32e7eSjoerg #include "llvm/ADT/iterator.h"
1706f32e7eSjoerg #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
1806f32e7eSjoerg #include "llvm/Analysis/BranchProbabilityInfo.h"
1906f32e7eSjoerg #include "llvm/Analysis/LoopInfo.h"
2006f32e7eSjoerg #include "llvm/IR/CFG.h"
2106f32e7eSjoerg #include "llvm/IR/Function.h"
2206f32e7eSjoerg #include "llvm/IR/PassManager.h"
23*da58b97aSjoerg #include "llvm/InitializePasses.h"
2406f32e7eSjoerg #include "llvm/Pass.h"
2506f32e7eSjoerg #include "llvm/Support/CommandLine.h"
2606f32e7eSjoerg #include "llvm/Support/GraphWriter.h"
2706f32e7eSjoerg #include "llvm/Support/raw_ostream.h"
2806f32e7eSjoerg #include <algorithm>
2906f32e7eSjoerg #include <cassert>
3006f32e7eSjoerg #include <string>
3106f32e7eSjoerg 
3206f32e7eSjoerg using namespace llvm;
3306f32e7eSjoerg 
3406f32e7eSjoerg #define DEBUG_TYPE "block-freq"
3506f32e7eSjoerg 
3606f32e7eSjoerg static cl::opt<GVDAGType> ViewBlockFreqPropagationDAG(
3706f32e7eSjoerg     "view-block-freq-propagation-dags", cl::Hidden,
3806f32e7eSjoerg     cl::desc("Pop up a window to show a dag displaying how block "
3906f32e7eSjoerg              "frequencies propagation through the CFG."),
4006f32e7eSjoerg     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
4106f32e7eSjoerg                clEnumValN(GVDT_Fraction, "fraction",
4206f32e7eSjoerg                           "display a graph using the "
4306f32e7eSjoerg                           "fractional block frequency representation."),
4406f32e7eSjoerg                clEnumValN(GVDT_Integer, "integer",
4506f32e7eSjoerg                           "display a graph using the raw "
4606f32e7eSjoerg                           "integer fractional block frequency representation."),
4706f32e7eSjoerg                clEnumValN(GVDT_Count, "count", "display a graph using the real "
4806f32e7eSjoerg                                                "profile count if available.")));
4906f32e7eSjoerg 
50*da58b97aSjoerg namespace llvm {
5106f32e7eSjoerg cl::opt<std::string>
5206f32e7eSjoerg     ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden,
5306f32e7eSjoerg                           cl::desc("The option to specify "
5406f32e7eSjoerg                                    "the name of the function "
5506f32e7eSjoerg                                    "whose CFG will be displayed."));
5606f32e7eSjoerg 
5706f32e7eSjoerg cl::opt<unsigned>
5806f32e7eSjoerg     ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden,
5906f32e7eSjoerg                        cl::desc("An integer in percent used to specify "
6006f32e7eSjoerg                                 "the hot blocks/edges to be displayed "
6106f32e7eSjoerg                                 "in red: a block or edge whose frequency "
6206f32e7eSjoerg                                 "is no less than the max frequency of the "
6306f32e7eSjoerg                                 "function multiplied by this percent."));
6406f32e7eSjoerg 
6506f32e7eSjoerg // Command line option to turn on CFG dot or text dump after profile annotation.
6606f32e7eSjoerg cl::opt<PGOViewCountsType> PGOViewCounts(
6706f32e7eSjoerg     "pgo-view-counts", cl::Hidden,
6806f32e7eSjoerg     cl::desc("A boolean option to show CFG dag or text with "
6906f32e7eSjoerg              "block profile counts and branch probabilities "
7006f32e7eSjoerg              "right after PGO profile annotation step. The "
7106f32e7eSjoerg              "profile counts are computed using branch "
7206f32e7eSjoerg              "probabilities from the runtime profile data and "
7306f32e7eSjoerg              "block frequency propagation algorithm. To view "
7406f32e7eSjoerg              "the raw counts from the profile, use option "
7506f32e7eSjoerg              "-pgo-view-raw-counts instead. To limit graph "
7606f32e7eSjoerg              "display to only one function, use filtering option "
7706f32e7eSjoerg              "-view-bfi-func-name."),
7806f32e7eSjoerg     cl::values(clEnumValN(PGOVCT_None, "none", "do not show."),
7906f32e7eSjoerg                clEnumValN(PGOVCT_Graph, "graph", "show a graph."),
8006f32e7eSjoerg                clEnumValN(PGOVCT_Text, "text", "show in text.")));
8106f32e7eSjoerg 
8206f32e7eSjoerg static cl::opt<bool> PrintBlockFreq(
8306f32e7eSjoerg     "print-bfi", cl::init(false), cl::Hidden,
8406f32e7eSjoerg     cl::desc("Print the block frequency info."));
8506f32e7eSjoerg 
8606f32e7eSjoerg cl::opt<std::string> PrintBlockFreqFuncName(
8706f32e7eSjoerg     "print-bfi-func-name", cl::Hidden,
8806f32e7eSjoerg     cl::desc("The option to specify the name of the function "
8906f32e7eSjoerg              "whose block frequency info is printed."));
90*da58b97aSjoerg } // namespace llvm
9106f32e7eSjoerg 
9206f32e7eSjoerg namespace llvm {
9306f32e7eSjoerg 
getGVDT()9406f32e7eSjoerg static GVDAGType getGVDT() {
9506f32e7eSjoerg   if (PGOViewCounts == PGOVCT_Graph)
9606f32e7eSjoerg     return GVDT_Count;
9706f32e7eSjoerg   return ViewBlockFreqPropagationDAG;
9806f32e7eSjoerg }
9906f32e7eSjoerg 
10006f32e7eSjoerg template <>
10106f32e7eSjoerg struct GraphTraits<BlockFrequencyInfo *> {
10206f32e7eSjoerg   using NodeRef = const BasicBlock *;
103*da58b97aSjoerg   using ChildIteratorType = const_succ_iterator;
10406f32e7eSjoerg   using nodes_iterator = pointer_iterator<Function::const_iterator>;
10506f32e7eSjoerg 
getEntryNodellvm::GraphTraits10606f32e7eSjoerg   static NodeRef getEntryNode(const BlockFrequencyInfo *G) {
10706f32e7eSjoerg     return &G->getFunction()->front();
10806f32e7eSjoerg   }
10906f32e7eSjoerg 
child_beginllvm::GraphTraits11006f32e7eSjoerg   static ChildIteratorType child_begin(const NodeRef N) {
11106f32e7eSjoerg     return succ_begin(N);
11206f32e7eSjoerg   }
11306f32e7eSjoerg 
child_endllvm::GraphTraits11406f32e7eSjoerg   static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); }
11506f32e7eSjoerg 
nodes_beginllvm::GraphTraits11606f32e7eSjoerg   static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
11706f32e7eSjoerg     return nodes_iterator(G->getFunction()->begin());
11806f32e7eSjoerg   }
11906f32e7eSjoerg 
nodes_endllvm::GraphTraits12006f32e7eSjoerg   static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
12106f32e7eSjoerg     return nodes_iterator(G->getFunction()->end());
12206f32e7eSjoerg   }
12306f32e7eSjoerg };
12406f32e7eSjoerg 
12506f32e7eSjoerg using BFIDOTGTraitsBase =
12606f32e7eSjoerg     BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>;
12706f32e7eSjoerg 
12806f32e7eSjoerg template <>
12906f32e7eSjoerg struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
DOTGraphTraitsllvm::DOTGraphTraits13006f32e7eSjoerg   explicit DOTGraphTraits(bool isSimple = false)
13106f32e7eSjoerg       : BFIDOTGTraitsBase(isSimple) {}
13206f32e7eSjoerg 
getNodeLabelllvm::DOTGraphTraits13306f32e7eSjoerg   std::string getNodeLabel(const BasicBlock *Node,
13406f32e7eSjoerg                            const BlockFrequencyInfo *Graph) {
13506f32e7eSjoerg 
13606f32e7eSjoerg     return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, getGVDT());
13706f32e7eSjoerg   }
13806f32e7eSjoerg 
getNodeAttributesllvm::DOTGraphTraits13906f32e7eSjoerg   std::string getNodeAttributes(const BasicBlock *Node,
14006f32e7eSjoerg                                 const BlockFrequencyInfo *Graph) {
14106f32e7eSjoerg     return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
14206f32e7eSjoerg                                                 ViewHotFreqPercent);
14306f32e7eSjoerg   }
14406f32e7eSjoerg 
getEdgeAttributesllvm::DOTGraphTraits14506f32e7eSjoerg   std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
14606f32e7eSjoerg                                 const BlockFrequencyInfo *BFI) {
14706f32e7eSjoerg     return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
14806f32e7eSjoerg                                                 ViewHotFreqPercent);
14906f32e7eSjoerg   }
15006f32e7eSjoerg };
15106f32e7eSjoerg 
15206f32e7eSjoerg } // end namespace llvm
15306f32e7eSjoerg 
15406f32e7eSjoerg BlockFrequencyInfo::BlockFrequencyInfo() = default;
15506f32e7eSjoerg 
BlockFrequencyInfo(const Function & F,const BranchProbabilityInfo & BPI,const LoopInfo & LI)15606f32e7eSjoerg BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
15706f32e7eSjoerg                                        const BranchProbabilityInfo &BPI,
15806f32e7eSjoerg                                        const LoopInfo &LI) {
15906f32e7eSjoerg   calculate(F, BPI, LI);
16006f32e7eSjoerg }
16106f32e7eSjoerg 
BlockFrequencyInfo(BlockFrequencyInfo && Arg)16206f32e7eSjoerg BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
16306f32e7eSjoerg     : BFI(std::move(Arg.BFI)) {}
16406f32e7eSjoerg 
operator =(BlockFrequencyInfo && RHS)16506f32e7eSjoerg BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
16606f32e7eSjoerg   releaseMemory();
16706f32e7eSjoerg   BFI = std::move(RHS.BFI);
16806f32e7eSjoerg   return *this;
16906f32e7eSjoerg }
17006f32e7eSjoerg 
17106f32e7eSjoerg // Explicitly define the default constructor otherwise it would be implicitly
17206f32e7eSjoerg // defined at the first ODR-use which is the BFI member in the
17306f32e7eSjoerg // LazyBlockFrequencyInfo header.  The dtor needs the BlockFrequencyInfoImpl
17406f32e7eSjoerg // template instantiated which is not available in the header.
17506f32e7eSjoerg BlockFrequencyInfo::~BlockFrequencyInfo() = default;
17606f32e7eSjoerg 
invalidate(Function & F,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator &)17706f32e7eSjoerg bool BlockFrequencyInfo::invalidate(Function &F, const PreservedAnalyses &PA,
17806f32e7eSjoerg                                     FunctionAnalysisManager::Invalidator &) {
17906f32e7eSjoerg   // Check whether the analysis, all analyses on functions, or the function's
18006f32e7eSjoerg   // CFG have been preserved.
18106f32e7eSjoerg   auto PAC = PA.getChecker<BlockFrequencyAnalysis>();
18206f32e7eSjoerg   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
18306f32e7eSjoerg            PAC.preservedSet<CFGAnalyses>());
18406f32e7eSjoerg }
18506f32e7eSjoerg 
calculate(const Function & F,const BranchProbabilityInfo & BPI,const LoopInfo & LI)18606f32e7eSjoerg void BlockFrequencyInfo::calculate(const Function &F,
18706f32e7eSjoerg                                    const BranchProbabilityInfo &BPI,
18806f32e7eSjoerg                                    const LoopInfo &LI) {
18906f32e7eSjoerg   if (!BFI)
19006f32e7eSjoerg     BFI.reset(new ImplType);
19106f32e7eSjoerg   BFI->calculate(F, BPI, LI);
19206f32e7eSjoerg   if (ViewBlockFreqPropagationDAG != GVDT_None &&
19306f32e7eSjoerg       (ViewBlockFreqFuncName.empty() ||
19406f32e7eSjoerg        F.getName().equals(ViewBlockFreqFuncName))) {
19506f32e7eSjoerg     view();
19606f32e7eSjoerg   }
19706f32e7eSjoerg   if (PrintBlockFreq &&
19806f32e7eSjoerg       (PrintBlockFreqFuncName.empty() ||
19906f32e7eSjoerg        F.getName().equals(PrintBlockFreqFuncName))) {
20006f32e7eSjoerg     print(dbgs());
20106f32e7eSjoerg   }
20206f32e7eSjoerg }
20306f32e7eSjoerg 
getBlockFreq(const BasicBlock * BB) const20406f32e7eSjoerg BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
20506f32e7eSjoerg   return BFI ? BFI->getBlockFreq(BB) : 0;
20606f32e7eSjoerg }
20706f32e7eSjoerg 
20806f32e7eSjoerg Optional<uint64_t>
getBlockProfileCount(const BasicBlock * BB,bool AllowSynthetic) const20906f32e7eSjoerg BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB,
21006f32e7eSjoerg                                          bool AllowSynthetic) const {
21106f32e7eSjoerg   if (!BFI)
21206f32e7eSjoerg     return None;
21306f32e7eSjoerg 
21406f32e7eSjoerg   return BFI->getBlockProfileCount(*getFunction(), BB, AllowSynthetic);
21506f32e7eSjoerg }
21606f32e7eSjoerg 
21706f32e7eSjoerg Optional<uint64_t>
getProfileCountFromFreq(uint64_t Freq) const21806f32e7eSjoerg BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
21906f32e7eSjoerg   if (!BFI)
22006f32e7eSjoerg     return None;
22106f32e7eSjoerg   return BFI->getProfileCountFromFreq(*getFunction(), Freq);
22206f32e7eSjoerg }
22306f32e7eSjoerg 
isIrrLoopHeader(const BasicBlock * BB)22406f32e7eSjoerg bool BlockFrequencyInfo::isIrrLoopHeader(const BasicBlock *BB) {
22506f32e7eSjoerg   assert(BFI && "Expected analysis to be available");
22606f32e7eSjoerg   return BFI->isIrrLoopHeader(BB);
22706f32e7eSjoerg }
22806f32e7eSjoerg 
setBlockFreq(const BasicBlock * BB,uint64_t Freq)22906f32e7eSjoerg void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
23006f32e7eSjoerg   assert(BFI && "Expected analysis to be available");
23106f32e7eSjoerg   BFI->setBlockFreq(BB, Freq);
23206f32e7eSjoerg }
23306f32e7eSjoerg 
setBlockFreqAndScale(const BasicBlock * ReferenceBB,uint64_t Freq,SmallPtrSetImpl<BasicBlock * > & BlocksToScale)23406f32e7eSjoerg void BlockFrequencyInfo::setBlockFreqAndScale(
23506f32e7eSjoerg     const BasicBlock *ReferenceBB, uint64_t Freq,
23606f32e7eSjoerg     SmallPtrSetImpl<BasicBlock *> &BlocksToScale) {
23706f32e7eSjoerg   assert(BFI && "Expected analysis to be available");
23806f32e7eSjoerg   // Use 128 bits APInt to avoid overflow.
23906f32e7eSjoerg   APInt NewFreq(128, Freq);
24006f32e7eSjoerg   APInt OldFreq(128, BFI->getBlockFreq(ReferenceBB).getFrequency());
24106f32e7eSjoerg   APInt BBFreq(128, 0);
24206f32e7eSjoerg   for (auto *BB : BlocksToScale) {
24306f32e7eSjoerg     BBFreq = BFI->getBlockFreq(BB).getFrequency();
24406f32e7eSjoerg     // Multiply first by NewFreq and then divide by OldFreq
24506f32e7eSjoerg     // to minimize loss of precision.
24606f32e7eSjoerg     BBFreq *= NewFreq;
24706f32e7eSjoerg     // udiv is an expensive operation in the general case. If this ends up being
24806f32e7eSjoerg     // a hot spot, one of the options proposed in
24906f32e7eSjoerg     // https://reviews.llvm.org/D28535#650071 could be used to avoid this.
25006f32e7eSjoerg     BBFreq = BBFreq.udiv(OldFreq);
25106f32e7eSjoerg     BFI->setBlockFreq(BB, BBFreq.getLimitedValue());
25206f32e7eSjoerg   }
25306f32e7eSjoerg   BFI->setBlockFreq(ReferenceBB, Freq);
25406f32e7eSjoerg }
25506f32e7eSjoerg 
25606f32e7eSjoerg /// Pop up a ghostview window with the current block frequency propagation
25706f32e7eSjoerg /// rendered using dot.
view(StringRef title) const25806f32e7eSjoerg void BlockFrequencyInfo::view(StringRef title) const {
25906f32e7eSjoerg   ViewGraph(const_cast<BlockFrequencyInfo *>(this), title);
26006f32e7eSjoerg }
26106f32e7eSjoerg 
getFunction() const26206f32e7eSjoerg const Function *BlockFrequencyInfo::getFunction() const {
26306f32e7eSjoerg   return BFI ? BFI->getFunction() : nullptr;
26406f32e7eSjoerg }
26506f32e7eSjoerg 
getBPI() const26606f32e7eSjoerg const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
26706f32e7eSjoerg   return BFI ? &BFI->getBPI() : nullptr;
26806f32e7eSjoerg }
26906f32e7eSjoerg 
27006f32e7eSjoerg raw_ostream &BlockFrequencyInfo::
printBlockFreq(raw_ostream & OS,const BlockFrequency Freq) const27106f32e7eSjoerg printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
27206f32e7eSjoerg   return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
27306f32e7eSjoerg }
27406f32e7eSjoerg 
27506f32e7eSjoerg raw_ostream &
printBlockFreq(raw_ostream & OS,const BasicBlock * BB) const27606f32e7eSjoerg BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
27706f32e7eSjoerg                                    const BasicBlock *BB) const {
27806f32e7eSjoerg   return BFI ? BFI->printBlockFreq(OS, BB) : OS;
27906f32e7eSjoerg }
28006f32e7eSjoerg 
getEntryFreq() const28106f32e7eSjoerg uint64_t BlockFrequencyInfo::getEntryFreq() const {
28206f32e7eSjoerg   return BFI ? BFI->getEntryFreq() : 0;
28306f32e7eSjoerg }
28406f32e7eSjoerg 
releaseMemory()28506f32e7eSjoerg void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
28606f32e7eSjoerg 
print(raw_ostream & OS) const28706f32e7eSjoerg void BlockFrequencyInfo::print(raw_ostream &OS) const {
28806f32e7eSjoerg   if (BFI)
28906f32e7eSjoerg     BFI->print(OS);
29006f32e7eSjoerg }
29106f32e7eSjoerg 
verifyMatch(BlockFrequencyInfo & Other) const292*da58b97aSjoerg void BlockFrequencyInfo::verifyMatch(BlockFrequencyInfo &Other) const {
293*da58b97aSjoerg   if (BFI)
294*da58b97aSjoerg     BFI->verifyMatch(*Other.BFI);
295*da58b97aSjoerg }
296*da58b97aSjoerg 
29706f32e7eSjoerg INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
29806f32e7eSjoerg                       "Block Frequency Analysis", true, true)
29906f32e7eSjoerg INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
30006f32e7eSjoerg INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
30106f32e7eSjoerg INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
30206f32e7eSjoerg                     "Block Frequency Analysis", true, true)
30306f32e7eSjoerg 
30406f32e7eSjoerg char BlockFrequencyInfoWrapperPass::ID = 0;
30506f32e7eSjoerg 
BlockFrequencyInfoWrapperPass()30606f32e7eSjoerg BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
30706f32e7eSjoerg     : FunctionPass(ID) {
30806f32e7eSjoerg   initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
30906f32e7eSjoerg }
31006f32e7eSjoerg 
31106f32e7eSjoerg BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() = default;
31206f32e7eSjoerg 
print(raw_ostream & OS,const Module *) const31306f32e7eSjoerg void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
31406f32e7eSjoerg                                           const Module *) const {
31506f32e7eSjoerg   BFI.print(OS);
31606f32e7eSjoerg }
31706f32e7eSjoerg 
getAnalysisUsage(AnalysisUsage & AU) const31806f32e7eSjoerg void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
31906f32e7eSjoerg   AU.addRequired<BranchProbabilityInfoWrapperPass>();
32006f32e7eSjoerg   AU.addRequired<LoopInfoWrapperPass>();
32106f32e7eSjoerg   AU.setPreservesAll();
32206f32e7eSjoerg }
32306f32e7eSjoerg 
releaseMemory()32406f32e7eSjoerg void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
32506f32e7eSjoerg 
runOnFunction(Function & F)32606f32e7eSjoerg bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
32706f32e7eSjoerg   BranchProbabilityInfo &BPI =
32806f32e7eSjoerg       getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
32906f32e7eSjoerg   LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
33006f32e7eSjoerg   BFI.calculate(F, BPI, LI);
33106f32e7eSjoerg   return false;
33206f32e7eSjoerg }
33306f32e7eSjoerg 
33406f32e7eSjoerg AnalysisKey BlockFrequencyAnalysis::Key;
run(Function & F,FunctionAnalysisManager & AM)33506f32e7eSjoerg BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
33606f32e7eSjoerg                                                FunctionAnalysisManager &AM) {
33706f32e7eSjoerg   BlockFrequencyInfo BFI;
33806f32e7eSjoerg   BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
33906f32e7eSjoerg                 AM.getResult<LoopAnalysis>(F));
34006f32e7eSjoerg   return BFI;
34106f32e7eSjoerg }
34206f32e7eSjoerg 
34306f32e7eSjoerg PreservedAnalyses
run(Function & F,FunctionAnalysisManager & AM)34406f32e7eSjoerg BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
34506f32e7eSjoerg   OS << "Printing analysis results of BFI for function "
34606f32e7eSjoerg      << "'" << F.getName() << "':"
34706f32e7eSjoerg      << "\n";
34806f32e7eSjoerg   AM.getResult<BlockFrequencyAnalysis>(F).print(OS);
34906f32e7eSjoerg   return PreservedAnalyses::all();
35006f32e7eSjoerg }
351