1689a5073SDuncan P. N. Exon Smith //===- BlockFrequencyInfo.cpp - Block Frequency Analysis ------------------===//
2875ebd5fSJakub Staszak //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6875ebd5fSJakub Staszak //
7875ebd5fSJakub Staszak //===----------------------------------------------------------------------===//
8875ebd5fSJakub Staszak //
9875ebd5fSJakub Staszak // Loops should be simplified before this analysis.
10875ebd5fSJakub Staszak //
11875ebd5fSJakub Staszak //===----------------------------------------------------------------------===//
12875ebd5fSJakub Staszak
13875ebd5fSJakub Staszak #include "llvm/Analysis/BlockFrequencyInfo.h"
1438c02bc7SEugene Zelenko #include "llvm/ADT/APInt.h"
1538c02bc7SEugene Zelenko #include "llvm/ADT/None.h"
1638c02bc7SEugene Zelenko #include "llvm/ADT/iterator.h"
17689a5073SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
18ed0881b2SChandler Carruth #include "llvm/Analysis/BranchProbabilityInfo.h"
19875ebd5fSJakub Staszak #include "llvm/Analysis/LoopInfo.h"
201305dc33SChandler Carruth #include "llvm/IR/CFG.h"
2138c02bc7SEugene Zelenko #include "llvm/IR/Function.h"
2238c02bc7SEugene Zelenko #include "llvm/IR/PassManager.h"
2305da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
2438c02bc7SEugene Zelenko #include "llvm/Pass.h"
25fd8aee76SMichael Gottesman #include "llvm/Support/CommandLine.h"
26fd8aee76SMichael Gottesman #include "llvm/Support/GraphWriter.h"
2738c02bc7SEugene Zelenko #include "llvm/Support/raw_ostream.h"
2838c02bc7SEugene Zelenko #include <cassert>
2938c02bc7SEugene Zelenko #include <string>
30875ebd5fSJakub Staszak
31875ebd5fSJakub Staszak using namespace llvm;
32875ebd5fSJakub Staszak
33f1221bd0SChandler Carruth #define DEBUG_TYPE "block-freq"
34f1221bd0SChandler Carruth
358dd5ce97SXinliang David Li static cl::opt<GVDAGType> ViewBlockFreqPropagationDAG(
368dd5ce97SXinliang David Li "view-block-freq-propagation-dags", cl::Hidden,
37fd8aee76SMichael Gottesman cl::desc("Pop up a window to show a dag displaying how block "
38fd8aee76SMichael Gottesman "frequencies propagation through the CFG."),
398dd5ce97SXinliang David Li cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
408dd5ce97SXinliang David Li clEnumValN(GVDT_Fraction, "fraction",
418dd5ce97SXinliang David Li "display a graph using the "
42fd8aee76SMichael Gottesman "fractional block frequency representation."),
438dd5ce97SXinliang David Li clEnumValN(GVDT_Integer, "integer",
448dd5ce97SXinliang David Li "display a graph using the raw "
45fd8aee76SMichael Gottesman "integer fractional block frequency representation."),
468dd5ce97SXinliang David Li clEnumValN(GVDT_Count, "count", "display a graph using the real "
47732afdd0SMehdi Amini "profile count if available.")));
48fd8aee76SMichael Gottesman
49*d8aba75aSFangrui Song namespace llvm {
503e176c77SXinliang David Li cl::opt<std::string>
513e176c77SXinliang David Li ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden,
523e176c77SXinliang David Li cl::desc("The option to specify "
533e176c77SXinliang David Li "the name of the function "
543e176c77SXinliang David Li "whose CFG will be displayed."));
553e176c77SXinliang David Li
563e176c77SXinliang David Li cl::opt<unsigned>
573e176c77SXinliang David Li ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden,
583e176c77SXinliang David Li cl::desc("An integer in percent used to specify "
593e176c77SXinliang David Li "the hot blocks/edges to be displayed "
603e176c77SXinliang David Li "in red: a block or edge whose frequency "
613e176c77SXinliang David Li "is no less than the max frequency of the "
623e176c77SXinliang David Li "function multiplied by this percent."));
638dd5ce97SXinliang David Li
64a43913cfSHiroshi Yamauchi // Command line option to turn on CFG dot or text dump after profile annotation.
65a43913cfSHiroshi Yamauchi cl::opt<PGOViewCountsType> PGOViewCounts(
66a43913cfSHiroshi Yamauchi "pgo-view-counts", cl::Hidden,
67a43913cfSHiroshi Yamauchi cl::desc("A boolean option to show CFG dag or text with "
6858fcc9bdSXinliang David Li "block profile counts and branch probabilities "
6958fcc9bdSXinliang David Li "right after PGO profile annotation step. The "
7058fcc9bdSXinliang David Li "profile counts are computed using branch "
7158fcc9bdSXinliang David Li "probabilities from the runtime profile data and "
7258fcc9bdSXinliang David Li "block frequency propagation algorithm. To view "
7358fcc9bdSXinliang David Li "the raw counts from the profile, use option "
7458fcc9bdSXinliang David Li "-pgo-view-raw-counts instead. To limit graph "
7558fcc9bdSXinliang David Li "display to only one function, use filtering option "
76a43913cfSHiroshi Yamauchi "-view-bfi-func-name."),
77a43913cfSHiroshi Yamauchi cl::values(clEnumValN(PGOVCT_None, "none", "do not show."),
78a43913cfSHiroshi Yamauchi clEnumValN(PGOVCT_Graph, "graph", "show a graph."),
79a43913cfSHiroshi Yamauchi clEnumValN(PGOVCT_Text, "text", "show in text.")));
80cb253ce9SXinliang David Li
8163e17ebfSHiroshi Yamauchi static cl::opt<bool> PrintBlockFreq(
8263e17ebfSHiroshi Yamauchi "print-bfi", cl::init(false), cl::Hidden,
8363e17ebfSHiroshi Yamauchi cl::desc("Print the block frequency info."));
8463e17ebfSHiroshi Yamauchi
8563e17ebfSHiroshi Yamauchi cl::opt<std::string> PrintBlockFreqFuncName(
8663e17ebfSHiroshi Yamauchi "print-bfi-func-name", cl::Hidden,
8763e17ebfSHiroshi Yamauchi cl::desc("The option to specify the name of the function "
8863e17ebfSHiroshi Yamauchi "whose block frequency info is printed."));
89*d8aba75aSFangrui Song } // namespace llvm
9063e17ebfSHiroshi Yamauchi
91fd8aee76SMichael Gottesman namespace llvm {
92fd8aee76SMichael Gottesman
getGVDT()93cb253ce9SXinliang David Li static GVDAGType getGVDT() {
94a43913cfSHiroshi Yamauchi if (PGOViewCounts == PGOVCT_Graph)
95cb253ce9SXinliang David Li return GVDT_Count;
96cb253ce9SXinliang David Li return ViewBlockFreqPropagationDAG;
97cb253ce9SXinliang David Li }
98cb253ce9SXinliang David Li
99fd8aee76SMichael Gottesman template <>
100fd8aee76SMichael Gottesman struct GraphTraits<BlockFrequencyInfo *> {
10138c02bc7SEugene Zelenko using NodeRef = const BasicBlock *;
1023abcbf99SAlina Sbirlea using ChildIteratorType = const_succ_iterator;
10338c02bc7SEugene Zelenko using nodes_iterator = pointer_iterator<Function::const_iterator>;
104fd8aee76SMichael Gottesman
getEntryNodellvm::GraphTraits10548f814e8STim Shen static NodeRef getEntryNode(const BlockFrequencyInfo *G) {
1065a82c916SDuncan P. N. Exon Smith return &G->getFunction()->front();
107fd8aee76SMichael Gottesman }
10838c02bc7SEugene Zelenko
child_beginllvm::GraphTraits109f2187ed3STim Shen static ChildIteratorType child_begin(const NodeRef N) {
110fd8aee76SMichael Gottesman return succ_begin(N);
111fd8aee76SMichael Gottesman }
11238c02bc7SEugene Zelenko
child_endllvm::GraphTraits113f2187ed3STim Shen static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); }
11438c02bc7SEugene Zelenko
nodes_beginllvm::GraphTraits115fd8aee76SMichael Gottesman static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
116b5e0f5acSTim Shen return nodes_iterator(G->getFunction()->begin());
117fd8aee76SMichael Gottesman }
11838c02bc7SEugene Zelenko
nodes_endllvm::GraphTraits119fd8aee76SMichael Gottesman static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
120b5e0f5acSTim Shen return nodes_iterator(G->getFunction()->end());
121fd8aee76SMichael Gottesman }
122fd8aee76SMichael Gottesman };
123fd8aee76SMichael Gottesman
12438c02bc7SEugene Zelenko using BFIDOTGTraitsBase =
12538c02bc7SEugene Zelenko BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>;
126fd8aee76SMichael Gottesman
12755415f25SXinliang David Li template <>
12855415f25SXinliang David Li struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
DOTGraphTraitsllvm::DOTGraphTraits12955415f25SXinliang David Li explicit DOTGraphTraits(bool isSimple = false)
13055415f25SXinliang David Li : BFIDOTGTraitsBase(isSimple) {}
131fd8aee76SMichael Gottesman
getNodeLabelllvm::DOTGraphTraits132fd8aee76SMichael Gottesman std::string getNodeLabel(const BasicBlock *Node,
133fd8aee76SMichael Gottesman const BlockFrequencyInfo *Graph) {
134fd8aee76SMichael Gottesman
135cb253ce9SXinliang David Li return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, getGVDT());
136fd8aee76SMichael Gottesman }
137fd8aee76SMichael Gottesman
getNodeAttributesllvm::DOTGraphTraits1383e176c77SXinliang David Li std::string getNodeAttributes(const BasicBlock *Node,
1393e176c77SXinliang David Li const BlockFrequencyInfo *Graph) {
1403e176c77SXinliang David Li return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
1413e176c77SXinliang David Li ViewHotFreqPercent);
1423e176c77SXinliang David Li }
1433e176c77SXinliang David Li
getEdgeAttributesllvm::DOTGraphTraits14455415f25SXinliang David Li std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
14555415f25SXinliang David Li const BlockFrequencyInfo *BFI) {
1463e176c77SXinliang David Li return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
1473e176c77SXinliang David Li ViewHotFreqPercent);
148fd8aee76SMichael Gottesman }
149fd8aee76SMichael Gottesman };
150fd8aee76SMichael Gottesman
151fd8aee76SMichael Gottesman } // end namespace llvm
152fd8aee76SMichael Gottesman
15338c02bc7SEugene Zelenko BlockFrequencyInfo::BlockFrequencyInfo() = default;
1549b4f6b26SCong Hou
BlockFrequencyInfo(const Function & F,const BranchProbabilityInfo & BPI,const LoopInfo & LI)1559b4f6b26SCong Hou BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
1569b4f6b26SCong Hou const BranchProbabilityInfo &BPI,
1579b4f6b26SCong Hou const LoopInfo &LI) {
1589b4f6b26SCong Hou calculate(F, BPI, LI);
1599b4f6b26SCong Hou }
1609b4f6b26SCong Hou
BlockFrequencyInfo(BlockFrequencyInfo && Arg)16128a93274SXinliang David Li BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
16228a93274SXinliang David Li : BFI(std::move(Arg.BFI)) {}
16328a93274SXinliang David Li
operator =(BlockFrequencyInfo && RHS)16428a93274SXinliang David Li BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
16528a93274SXinliang David Li releaseMemory();
16628a93274SXinliang David Li BFI = std::move(RHS.BFI);
16728a93274SXinliang David Li return *this;
16828a93274SXinliang David Li }
16928a93274SXinliang David Li
170c2f791d8SAdam Nemet // Explicitly define the default constructor otherwise it would be implicitly
171c2f791d8SAdam Nemet // defined at the first ODR-use which is the BFI member in the
172c2f791d8SAdam Nemet // LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl
173c2f791d8SAdam Nemet // template instantiated which is not available in the header.
17438c02bc7SEugene Zelenko BlockFrequencyInfo::~BlockFrequencyInfo() = default;
175c2f791d8SAdam Nemet
invalidate(Function & F,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator &)176ca68a3ecSChandler Carruth bool BlockFrequencyInfo::invalidate(Function &F, const PreservedAnalyses &PA,
177ca68a3ecSChandler Carruth FunctionAnalysisManager::Invalidator &) {
178ca68a3ecSChandler Carruth // Check whether the analysis, all analyses on functions, or the function's
179ca68a3ecSChandler Carruth // CFG have been preserved.
180ca68a3ecSChandler Carruth auto PAC = PA.getChecker<BlockFrequencyAnalysis>();
181ca68a3ecSChandler Carruth return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
182ca68a3ecSChandler Carruth PAC.preservedSet<CFGAnalyses>());
183ca68a3ecSChandler Carruth }
184ca68a3ecSChandler Carruth
calculate(const Function & F,const BranchProbabilityInfo & BPI,const LoopInfo & LI)185deee61e4SWei Mi void BlockFrequencyInfo::calculate(const Function &F,
186deee61e4SWei Mi const BranchProbabilityInfo &BPI,
187deee61e4SWei Mi const LoopInfo &LI) {
1883dbe1050SDuncan P. N. Exon Smith if (!BFI)
1893dbe1050SDuncan P. N. Exon Smith BFI.reset(new ImplType);
1905e67b666SCong Hou BFI->calculate(F, BPI, LI);
1918dd5ce97SXinliang David Li if (ViewBlockFreqPropagationDAG != GVDT_None &&
1928dd5ce97SXinliang David Li (ViewBlockFreqFuncName.empty() ||
1938dd5ce97SXinliang David Li F.getName().equals(ViewBlockFreqFuncName))) {
194fd8aee76SMichael Gottesman view();
1958dd5ce97SXinliang David Li }
19663e17ebfSHiroshi Yamauchi if (PrintBlockFreq &&
19763e17ebfSHiroshi Yamauchi (PrintBlockFreqFuncName.empty() ||
19863e17ebfSHiroshi Yamauchi F.getName().equals(PrintBlockFreqFuncName))) {
19963e17ebfSHiroshi Yamauchi print(dbgs());
20063e17ebfSHiroshi Yamauchi }
201343fad44SChandler Carruth }
202343fad44SChandler Carruth
getBlockFreq(const BasicBlock * BB) const20396f8c551SJakub Staszak BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
2043dbe1050SDuncan P. N. Exon Smith return BFI ? BFI->getBlockFreq(BB) : 0;
205875ebd5fSJakub Staszak }
206fd8aee76SMichael Gottesman
20712b79aa0SEaswaran Raman Optional<uint64_t>
getBlockProfileCount(const BasicBlock * BB,bool AllowSynthetic) const208499c80b8SXinliang David Li BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB,
209499c80b8SXinliang David Li bool AllowSynthetic) const {
210b12b353aSXinliang David Li if (!BFI)
21112b79aa0SEaswaran Raman return None;
212b12b353aSXinliang David Li
213499c80b8SXinliang David Li return BFI->getBlockProfileCount(*getFunction(), BB, AllowSynthetic);
21412b79aa0SEaswaran Raman }
21512b79aa0SEaswaran Raman
216f801575fSSean Silva Optional<uint64_t>
getProfileCountFromFreq(uint64_t Freq) const217f801575fSSean Silva BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
218f801575fSSean Silva if (!BFI)
219f801575fSSean Silva return None;
220f801575fSSean Silva return BFI->getProfileCountFromFreq(*getFunction(), Freq);
221f801575fSSean Silva }
222f801575fSSean Silva
isIrrLoopHeader(const BasicBlock * BB)223dce9def3SHiroshi Yamauchi bool BlockFrequencyInfo::isIrrLoopHeader(const BasicBlock *BB) {
224dce9def3SHiroshi Yamauchi assert(BFI && "Expected analysis to be available");
225dce9def3SHiroshi Yamauchi return BFI->isIrrLoopHeader(BB);
226dce9def3SHiroshi Yamauchi }
227dce9def3SHiroshi Yamauchi
setBlockFreq(const BasicBlock * BB,uint64_t Freq)228b12b353aSXinliang David Li void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
22972d44b1bSManman Ren assert(BFI && "Expected analysis to be available");
23072d44b1bSManman Ren BFI->setBlockFreq(BB, Freq);
23172d44b1bSManman Ren }
23272d44b1bSManman Ren
setBlockFreqAndScale(const BasicBlock * ReferenceBB,uint64_t Freq,SmallPtrSetImpl<BasicBlock * > & BlocksToScale)2336c8f511fSEaswaran Raman void BlockFrequencyInfo::setBlockFreqAndScale(
2346c8f511fSEaswaran Raman const BasicBlock *ReferenceBB, uint64_t Freq,
2356c8f511fSEaswaran Raman SmallPtrSetImpl<BasicBlock *> &BlocksToScale) {
2366c8f511fSEaswaran Raman assert(BFI && "Expected analysis to be available");
2376c8f511fSEaswaran Raman // Use 128 bits APInt to avoid overflow.
2386c8f511fSEaswaran Raman APInt NewFreq(128, Freq);
2396c8f511fSEaswaran Raman APInt OldFreq(128, BFI->getBlockFreq(ReferenceBB).getFrequency());
2406c8f511fSEaswaran Raman APInt BBFreq(128, 0);
2416c8f511fSEaswaran Raman for (auto *BB : BlocksToScale) {
2426c8f511fSEaswaran Raman BBFreq = BFI->getBlockFreq(BB).getFrequency();
2436c8f511fSEaswaran Raman // Multiply first by NewFreq and then divide by OldFreq
2446c8f511fSEaswaran Raman // to minimize loss of precision.
2456c8f511fSEaswaran Raman BBFreq *= NewFreq;
2466c8f511fSEaswaran Raman // udiv is an expensive operation in the general case. If this ends up being
2476c8f511fSEaswaran Raman // a hot spot, one of the options proposed in
2486c8f511fSEaswaran Raman // https://reviews.llvm.org/D28535#650071 could be used to avoid this.
2496c8f511fSEaswaran Raman BBFreq = BBFreq.udiv(OldFreq);
2506c8f511fSEaswaran Raman BFI->setBlockFreq(BB, BBFreq.getLimitedValue());
2516c8f511fSEaswaran Raman }
2526c8f511fSEaswaran Raman BFI->setBlockFreq(ReferenceBB, Freq);
2536c8f511fSEaswaran Raman }
2546c8f511fSEaswaran Raman
255fd8aee76SMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation
256fd8aee76SMichael Gottesman /// rendered using dot.
view(StringRef title) const2573ef0f444SDavid Callahan void BlockFrequencyInfo::view(StringRef title) const {
2583ef0f444SDavid Callahan ViewGraph(const_cast<BlockFrequencyInfo *>(this), title);
259fd8aee76SMichael Gottesman }
260fd8aee76SMichael Gottesman
getFunction() const261fd8aee76SMichael Gottesman const Function *BlockFrequencyInfo::getFunction() const {
26210be9a88SDuncan P. N. Exon Smith return BFI ? BFI->getFunction() : nullptr;
263fd8aee76SMichael Gottesman }
264fd5c4b2cSMichael Gottesman
getBPI() const26555415f25SXinliang David Li const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
26655415f25SXinliang David Li return BFI ? &BFI->getBPI() : nullptr;
26755415f25SXinliang David Li }
26855415f25SXinliang David Li
269fd5c4b2cSMichael Gottesman raw_ostream &BlockFrequencyInfo::
printBlockFreq(raw_ostream & OS,const BlockFrequency Freq) const270fd5c4b2cSMichael Gottesman printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
2713dbe1050SDuncan P. N. Exon Smith return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
272fd5c4b2cSMichael Gottesman }
273fd5c4b2cSMichael Gottesman
274fd5c4b2cSMichael Gottesman raw_ostream &
printBlockFreq(raw_ostream & OS,const BasicBlock * BB) const275fd5c4b2cSMichael Gottesman BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
276fd5c4b2cSMichael Gottesman const BasicBlock *BB) const {
2773dbe1050SDuncan P. N. Exon Smith return BFI ? BFI->printBlockFreq(OS, BB) : OS;
278fd5c4b2cSMichael Gottesman }
2795947c8faSYuchen Wu
getEntryFreq() const2805947c8faSYuchen Wu uint64_t BlockFrequencyInfo::getEntryFreq() const {
2813dbe1050SDuncan P. N. Exon Smith return BFI ? BFI->getEntryFreq() : 0;
2825947c8faSYuchen Wu }
283deee61e4SWei Mi
releaseMemory()284deee61e4SWei Mi void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
285deee61e4SWei Mi
print(raw_ostream & OS) const286deee61e4SWei Mi void BlockFrequencyInfo::print(raw_ostream &OS) const {
287deee61e4SWei Mi if (BFI)
288deee61e4SWei Mi BFI->print(OS);
289deee61e4SWei Mi }
290deee61e4SWei Mi
verifyMatch(BlockFrequencyInfo & Other) const2911b4e3defSHiroshi Yamauchi void BlockFrequencyInfo::verifyMatch(BlockFrequencyInfo &Other) const {
2921b4e3defSHiroshi Yamauchi if (BFI)
2931b4e3defSHiroshi Yamauchi BFI->verifyMatch(*Other.BFI);
2941b4e3defSHiroshi Yamauchi }
2951b4e3defSHiroshi Yamauchi
296deee61e4SWei Mi INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
297deee61e4SWei Mi "Block Frequency Analysis", true, true)
298ab23bfbcSCong Hou INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
299deee61e4SWei Mi INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
300deee61e4SWei Mi INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
301deee61e4SWei Mi "Block Frequency Analysis", true, true)
302deee61e4SWei Mi
303deee61e4SWei Mi char BlockFrequencyInfoWrapperPass::ID = 0;
304deee61e4SWei Mi
BlockFrequencyInfoWrapperPass()305deee61e4SWei Mi BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
306deee61e4SWei Mi : FunctionPass(ID) {
307deee61e4SWei Mi initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
308deee61e4SWei Mi }
309deee61e4SWei Mi
31038c02bc7SEugene Zelenko BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() = default;
311deee61e4SWei Mi
print(raw_ostream & OS,const Module *) const312deee61e4SWei Mi void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
313deee61e4SWei Mi const Module *) const {
314deee61e4SWei Mi BFI.print(OS);
315deee61e4SWei Mi }
316deee61e4SWei Mi
getAnalysisUsage(AnalysisUsage & AU) const317deee61e4SWei Mi void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
318ab23bfbcSCong Hou AU.addRequired<BranchProbabilityInfoWrapperPass>();
319deee61e4SWei Mi AU.addRequired<LoopInfoWrapperPass>();
320deee61e4SWei Mi AU.setPreservesAll();
321deee61e4SWei Mi }
322deee61e4SWei Mi
releaseMemory()323deee61e4SWei Mi void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
324deee61e4SWei Mi
runOnFunction(Function & F)325deee61e4SWei Mi bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
326ab23bfbcSCong Hou BranchProbabilityInfo &BPI =
327ab23bfbcSCong Hou getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
328deee61e4SWei Mi LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
329deee61e4SWei Mi BFI.calculate(F, BPI, LI);
330deee61e4SWei Mi return false;
331deee61e4SWei Mi }
33228a93274SXinliang David Li
333dab4eae2SChandler Carruth AnalysisKey BlockFrequencyAnalysis::Key;
run(Function & F,FunctionAnalysisManager & AM)33428a93274SXinliang David Li BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
33536e0d01eSSean Silva FunctionAnalysisManager &AM) {
33628a93274SXinliang David Li BlockFrequencyInfo BFI;
33728a93274SXinliang David Li BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
33828a93274SXinliang David Li AM.getResult<LoopAnalysis>(F));
33928a93274SXinliang David Li return BFI;
34028a93274SXinliang David Li }
34128a93274SXinliang David Li
34228a93274SXinliang David Li PreservedAnalyses
run(Function & F,FunctionAnalysisManager & AM)34336e0d01eSSean Silva BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
34428a93274SXinliang David Li OS << "Printing analysis results of BFI for function "
34528a93274SXinliang David Li << "'" << F.getName() << "':"
34628a93274SXinliang David Li << "\n";
34728a93274SXinliang David Li AM.getResult<BlockFrequencyAnalysis>(F).print(OS);
34828a93274SXinliang David Li return PreservedAnalyses::all();
34928a93274SXinliang David Li }
350