1689a5073SDuncan P. N. Exon Smith //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
2875ebd5fSJakub Staszak //
3875ebd5fSJakub Staszak //                     The LLVM Compiler Infrastructure
4875ebd5fSJakub Staszak //
5875ebd5fSJakub Staszak // This file is distributed under the University of Illinois Open Source
6875ebd5fSJakub Staszak // License. See LICENSE.TXT for details.
7875ebd5fSJakub Staszak //
8875ebd5fSJakub Staszak //===----------------------------------------------------------------------===//
9875ebd5fSJakub Staszak //
10875ebd5fSJakub Staszak // Loops should be simplified before this analysis.
11875ebd5fSJakub Staszak //
12875ebd5fSJakub Staszak //===----------------------------------------------------------------------===//
13875ebd5fSJakub Staszak 
14875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
15689a5073SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
16875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
1710be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineFunction.h"
1810be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineLoopInfo.h"
19ed0881b2SChandler Carruth #include "llvm/CodeGen/Passes.h"
20ed0881b2SChandler Carruth #include "llvm/InitializePasses.h"
2165bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h"
2265bbcdfaSMichael Gottesman #include "llvm/Support/Debug.h"
2369317f2eSXinliang David Li #include "llvm/Support/Format.h"
2465bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h"
2569317f2eSXinliang David Li #include "llvm/Support/raw_ostream.h"
26875ebd5fSJakub Staszak 
27875ebd5fSJakub Staszak using namespace llvm;
28875ebd5fSJakub Staszak 
291b9dde08SChandler Carruth #define DEBUG_TYPE "block-freq"
301b9dde08SChandler Carruth 
3165bbcdfaSMichael Gottesman #ifndef NDEBUG
3265bbcdfaSMichael Gottesman 
33bc157084SXinliang David Li static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
34bc157084SXinliang David Li     "view-machine-block-freq-propagation-dags", cl::Hidden,
3565bbcdfaSMichael Gottesman     cl::desc("Pop up a window to show a dag displaying how machine block "
36748fe483SMichael Gottesman              "frequencies propagate through the CFG."),
37bc157084SXinliang David Li     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
38bc157084SXinliang David Li                clEnumValN(GVDT_Fraction, "fraction",
39bc157084SXinliang David Li                           "display a graph using the "
4065bbcdfaSMichael Gottesman                           "fractional block frequency representation."),
41bc157084SXinliang David Li                clEnumValN(GVDT_Integer, "integer",
42bc157084SXinliang David Li                           "display a graph using the raw "
4365bbcdfaSMichael Gottesman                           "integer fractional block frequency representation."),
4430c50f3cSXinliang David Li                clEnumValN(GVDT_Count, "count", "display a graph using the real "
45732afdd0SMehdi Amini                                                "profile count if available.")));
46*fd3f645fSXinliang David Li // Similar option above, but used to control BFI display only after MBP pass
47*fd3f645fSXinliang David Li cl::opt<GVDAGType> ViewBlockLayoutWithBFI(
48*fd3f645fSXinliang David Li     "view-block-layout-with-bfi", cl::Hidden,
49*fd3f645fSXinliang David Li     cl::desc(
50*fd3f645fSXinliang David Li         "Pop up a window to show a dag displaying MBP layout and associated "
51*fd3f645fSXinliang David Li         "block frequencies of the CFG."),
52*fd3f645fSXinliang David Li     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
53*fd3f645fSXinliang David Li                clEnumValN(GVDT_Fraction, "fraction",
54*fd3f645fSXinliang David Li                           "display a graph using the "
55*fd3f645fSXinliang David Li                           "fractional block frequency representation."),
56*fd3f645fSXinliang David Li                clEnumValN(GVDT_Integer, "integer",
57*fd3f645fSXinliang David Li                           "display a graph using the raw "
58*fd3f645fSXinliang David Li                           "integer fractional block frequency representation."),
59*fd3f645fSXinliang David Li                clEnumValN(GVDT_Count, "count",
60*fd3f645fSXinliang David Li                           "display a graph using the real "
61*fd3f645fSXinliang David Li                           "profile count if available.")));
6265bbcdfaSMichael Gottesman 
638dd5ce97SXinliang David Li extern cl::opt<std::string> ViewBlockFreqFuncName;
642d531580SSimon Pilgrim extern cl::opt<unsigned> ViewHotFreqPercent;
6580457ce5SXinliang David Li 
66*fd3f645fSXinliang David Li static GVDAGType getGVDT() {
67*fd3f645fSXinliang David Li   if (ViewBlockLayoutWithBFI != GVDT_None)
68*fd3f645fSXinliang David Li     return ViewBlockLayoutWithBFI;
69*fd3f645fSXinliang David Li 
70*fd3f645fSXinliang David Li   return ViewMachineBlockFreqPropagationDAG;
71*fd3f645fSXinliang David Li }
72*fd3f645fSXinliang David Li 
7365bbcdfaSMichael Gottesman namespace llvm {
7465bbcdfaSMichael Gottesman 
75bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
76eb3958faSTim Shen   typedef const MachineBasicBlock *NodeRef;
7765bbcdfaSMichael Gottesman   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
78b5e0f5acSTim Shen   typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator;
7965bbcdfaSMichael Gottesman 
8048f814e8STim Shen   static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
810ac8eb91SDuncan P. N. Exon Smith     return &G->getFunction()->front();
8265bbcdfaSMichael Gottesman   }
83748fe483SMichael Gottesman 
84f2187ed3STim Shen   static ChildIteratorType child_begin(const NodeRef N) {
8565bbcdfaSMichael Gottesman     return N->succ_begin();
8665bbcdfaSMichael Gottesman   }
87748fe483SMichael Gottesman 
88f2187ed3STim Shen   static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
89748fe483SMichael Gottesman 
9065bbcdfaSMichael Gottesman   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
91b5e0f5acSTim Shen     return nodes_iterator(G->getFunction()->begin());
9265bbcdfaSMichael Gottesman   }
93748fe483SMichael Gottesman 
9465bbcdfaSMichael Gottesman   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
95b5e0f5acSTim Shen     return nodes_iterator(G->getFunction()->end());
9665bbcdfaSMichael Gottesman   }
9765bbcdfaSMichael Gottesman };
9865bbcdfaSMichael Gottesman 
9955415f25SXinliang David Li typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
10055415f25SXinliang David Li                               MachineBranchProbabilityInfo>
10155415f25SXinliang David Li     MBFIDOTGraphTraitsBase;
10265bbcdfaSMichael Gottesman template <>
103bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *>
10455415f25SXinliang David Li     : public MBFIDOTGraphTraitsBase {
105bc157084SXinliang David Li   explicit DOTGraphTraits(bool isSimple = false)
106*fd3f645fSXinliang David Li       : MBFIDOTGraphTraitsBase(isSimple), CurFunc(nullptr), LayoutOrderMap() {}
107*fd3f645fSXinliang David Li 
108*fd3f645fSXinliang David Li   const MachineFunction *CurFunc;
109*fd3f645fSXinliang David Li   DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;
11065bbcdfaSMichael Gottesman 
11165bbcdfaSMichael Gottesman   std::string getNodeLabel(const MachineBasicBlock *Node,
11265bbcdfaSMichael Gottesman                            const MachineBlockFrequencyInfo *Graph) {
113*fd3f645fSXinliang David Li 
114*fd3f645fSXinliang David Li     int layout_order = -1;
115*fd3f645fSXinliang David Li     // Attach additional ordering information if 'isSimple' is false.
116*fd3f645fSXinliang David Li     if (!isSimple()) {
117*fd3f645fSXinliang David Li       const MachineFunction *F = Node->getParent();
118*fd3f645fSXinliang David Li       if (!CurFunc || F != CurFunc) {
119*fd3f645fSXinliang David Li         if (CurFunc)
120*fd3f645fSXinliang David Li           LayoutOrderMap.clear();
121*fd3f645fSXinliang David Li 
122*fd3f645fSXinliang David Li         CurFunc = F;
123*fd3f645fSXinliang David Li         int O = 0;
124*fd3f645fSXinliang David Li         for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {
125*fd3f645fSXinliang David Li           LayoutOrderMap[&*MBI] = O;
126*fd3f645fSXinliang David Li         }
127*fd3f645fSXinliang David Li       }
128*fd3f645fSXinliang David Li       layout_order = LayoutOrderMap[Node];
129*fd3f645fSXinliang David Li     }
130*fd3f645fSXinliang David Li     return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),
131*fd3f645fSXinliang David Li                                                 layout_order);
13255415f25SXinliang David Li   }
13365bbcdfaSMichael Gottesman 
1343e176c77SXinliang David Li   std::string getNodeAttributes(const MachineBasicBlock *Node,
1353e176c77SXinliang David Li                                 const MachineBlockFrequencyInfo *Graph) {
1363e176c77SXinliang David Li     return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
1373e176c77SXinliang David Li                                                      ViewHotFreqPercent);
1383e176c77SXinliang David Li   }
1393e176c77SXinliang David Li 
14055415f25SXinliang David Li   std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
14169317f2eSXinliang David Li                                 const MachineBlockFrequencyInfo *MBFI) {
1423e176c77SXinliang David Li     return MBFIDOTGraphTraitsBase::getEdgeAttributes(
1433e176c77SXinliang David Li         Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
14469317f2eSXinliang David Li   }
14565bbcdfaSMichael Gottesman };
14665bbcdfaSMichael Gottesman 
14765bbcdfaSMichael Gottesman } // end namespace llvm
14865bbcdfaSMichael Gottesman #endif
14965bbcdfaSMichael Gottesman 
150875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
151875ebd5fSJakub Staszak                       "Machine Block Frequency Analysis", true, true)
152875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
15310be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
154875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
155875ebd5fSJakub Staszak                     "Machine Block Frequency Analysis", true, true)
156875ebd5fSJakub Staszak 
157875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0;
158875ebd5fSJakub Staszak 
159bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
160bc157084SXinliang David Li     : MachineFunctionPass(ID) {
161875ebd5fSJakub Staszak   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
162875ebd5fSJakub Staszak }
163875ebd5fSJakub Staszak 
1643dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
165875ebd5fSJakub Staszak 
166875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
167875ebd5fSJakub Staszak   AU.addRequired<MachineBranchProbabilityInfo>();
16810be9a88SDuncan P. N. Exon Smith   AU.addRequired<MachineLoopInfo>();
169875ebd5fSJakub Staszak   AU.setPreservesAll();
170875ebd5fSJakub Staszak   MachineFunctionPass::getAnalysisUsage(AU);
171875ebd5fSJakub Staszak }
172875ebd5fSJakub Staszak 
173875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
174748fe483SMichael Gottesman   MachineBranchProbabilityInfo &MBPI =
175748fe483SMichael Gottesman       getAnalysis<MachineBranchProbabilityInfo>();
17610be9a88SDuncan P. N. Exon Smith   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
1773dbe1050SDuncan P. N. Exon Smith   if (!MBFI)
1783dbe1050SDuncan P. N. Exon Smith     MBFI.reset(new ImplType);
1795e67b666SCong Hou   MBFI->calculate(F, MBPI, MLI);
18065bbcdfaSMichael Gottesman #ifndef NDEBUG
18180457ce5SXinliang David Li   if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
1828dd5ce97SXinliang David Li       (ViewBlockFreqFuncName.empty() ||
1838dd5ce97SXinliang David Li        F.getName().equals(ViewBlockFreqFuncName))) {
18465bbcdfaSMichael Gottesman     view();
18565bbcdfaSMichael Gottesman   }
18665bbcdfaSMichael Gottesman #endif
187875ebd5fSJakub Staszak   return false;
188875ebd5fSJakub Staszak }
189875ebd5fSJakub Staszak 
1903dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
1913dbe1050SDuncan P. N. Exon Smith 
19265bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation
19365bbcdfaSMichael Gottesman /// rendered using dot.
194*fd3f645fSXinliang David Li void MachineBlockFrequencyInfo::view(bool isSimple) const {
19565bbcdfaSMichael Gottesman // This code is only for debugging.
19665bbcdfaSMichael Gottesman #ifndef NDEBUG
19765bbcdfaSMichael Gottesman   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
198*fd3f645fSXinliang David Li             "MachineBlockFrequencyDAGs", isSimple);
19965bbcdfaSMichael Gottesman #else
200748fe483SMichael Gottesman   errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
201748fe483SMichael Gottesman             "on systems with Graphviz or gv!\n";
20265bbcdfaSMichael Gottesman #endif // NDEBUG
20365bbcdfaSMichael Gottesman }
20465bbcdfaSMichael Gottesman 
205bc157084SXinliang David Li BlockFrequency
206bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
2073dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
208875ebd5fSJakub Staszak }
20965bbcdfaSMichael Gottesman 
21030c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
21130c50f3cSXinliang David Li     const MachineBasicBlock *MBB) const {
21230c50f3cSXinliang David Li   const Function *F = MBFI->getFunction()->getFunction();
21330c50f3cSXinliang David Li   return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
21430c50f3cSXinliang David Li }
21530c50f3cSXinliang David Li 
216f801575fSSean Silva Optional<uint64_t>
217f801575fSSean Silva MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
218f801575fSSean Silva   const Function *F = MBFI->getFunction()->getFunction();
219f801575fSSean Silva   return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None;
220f801575fSSean Silva }
221f801575fSSean Silva 
222936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
22310be9a88SDuncan P. N. Exon Smith   return MBFI ? MBFI->getFunction() : nullptr;
22465bbcdfaSMichael Gottesman }
225fd5c4b2cSMichael Gottesman 
2263264fdd3SXinliang David Li const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
2273264fdd3SXinliang David Li   return MBFI ? &MBFI->getBPI() : nullptr;
2283264fdd3SXinliang David Li }
2293264fdd3SXinliang David Li 
230fd5c4b2cSMichael Gottesman raw_ostream &
231fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
232fd5c4b2cSMichael Gottesman                                           const BlockFrequency Freq) const {
2333dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
234fd5c4b2cSMichael Gottesman }
235fd5c4b2cSMichael Gottesman 
236fd5c4b2cSMichael Gottesman raw_ostream &
237fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
238fd5c4b2cSMichael Gottesman                                           const MachineBasicBlock *MBB) const {
2393dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
240fd5c4b2cSMichael Gottesman }
241fd5c4b2cSMichael Gottesman 
2425e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
2433dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getEntryFreq() : 0;
244fd5c4b2cSMichael Gottesman }
245