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"
23*69317f2eSXinliang David Li #include "llvm/Support/Format.h"
2465bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h"
25*69317f2eSXinliang 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
32bc157084SXinliang David Li enum GVDAGType { GVDT_None, GVDT_Fraction, GVDT_Integer };
3365bbcdfaSMichael Gottesman 
34bc157084SXinliang David Li static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
35bc157084SXinliang David Li     "view-machine-block-freq-propagation-dags", cl::Hidden,
3665bbcdfaSMichael Gottesman     cl::desc("Pop up a window to show a dag displaying how machine block "
37748fe483SMichael Gottesman              "frequencies propagate through the CFG."),
38bc157084SXinliang David Li     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
39bc157084SXinliang David Li                clEnumValN(GVDT_Fraction, "fraction",
40bc157084SXinliang David Li                           "display a graph using the "
4165bbcdfaSMichael Gottesman                           "fractional block frequency representation."),
42bc157084SXinliang David Li                clEnumValN(GVDT_Integer, "integer",
43bc157084SXinliang David Li                           "display a graph using the raw "
4465bbcdfaSMichael Gottesman                           "integer fractional block frequency representation."),
4565bbcdfaSMichael Gottesman                clEnumValEnd));
4665bbcdfaSMichael Gottesman 
4780457ce5SXinliang David Li static cl::opt<std::string> ViewMachineBlockFreqFuncName("view-mbfi-func-name",
4880457ce5SXinliang David Li                                                          cl::Hidden);
4980457ce5SXinliang David Li 
5065bbcdfaSMichael Gottesman namespace llvm {
5165bbcdfaSMichael Gottesman 
52bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
5365bbcdfaSMichael Gottesman   typedef const MachineBasicBlock NodeType;
5465bbcdfaSMichael Gottesman   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
5565bbcdfaSMichael Gottesman   typedef MachineFunction::const_iterator nodes_iterator;
5665bbcdfaSMichael Gottesman 
57bc157084SXinliang David Li   static inline const NodeType *
58bc157084SXinliang David Li   getEntryNode(const MachineBlockFrequencyInfo *G) {
590ac8eb91SDuncan P. N. Exon Smith     return &G->getFunction()->front();
6065bbcdfaSMichael Gottesman   }
61748fe483SMichael Gottesman 
6265bbcdfaSMichael Gottesman   static ChildIteratorType child_begin(const NodeType *N) {
6365bbcdfaSMichael Gottesman     return N->succ_begin();
6465bbcdfaSMichael Gottesman   }
65748fe483SMichael Gottesman 
6665bbcdfaSMichael Gottesman   static ChildIteratorType child_end(const NodeType *N) {
6765bbcdfaSMichael Gottesman     return N->succ_end();
6865bbcdfaSMichael Gottesman   }
69748fe483SMichael Gottesman 
7065bbcdfaSMichael Gottesman   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
7165bbcdfaSMichael Gottesman     return G->getFunction()->begin();
7265bbcdfaSMichael Gottesman   }
73748fe483SMichael Gottesman 
7465bbcdfaSMichael Gottesman   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
7565bbcdfaSMichael Gottesman     return G->getFunction()->end();
7665bbcdfaSMichael Gottesman   }
7765bbcdfaSMichael Gottesman };
7865bbcdfaSMichael Gottesman 
7965bbcdfaSMichael Gottesman template <>
80bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *>
81bc157084SXinliang David Li     : public DefaultDOTGraphTraits {
82bc157084SXinliang David Li   explicit DOTGraphTraits(bool isSimple = false)
83bc157084SXinliang David Li       : DefaultDOTGraphTraits(isSimple) {}
8465bbcdfaSMichael Gottesman 
85*69317f2eSXinliang David Li   typedef MachineBasicBlock::const_succ_iterator EdgeIter;
8665bbcdfaSMichael Gottesman   static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
8765bbcdfaSMichael Gottesman     return G->getFunction()->getName();
8865bbcdfaSMichael Gottesman   }
8965bbcdfaSMichael Gottesman 
9065bbcdfaSMichael Gottesman   std::string getNodeLabel(const MachineBasicBlock *Node,
9165bbcdfaSMichael Gottesman                            const MachineBlockFrequencyInfo *Graph) {
92e69170a1SAlp Toker     std::string Result;
93e69170a1SAlp Toker     raw_string_ostream OS(Result);
9465bbcdfaSMichael Gottesman 
95e69170a1SAlp Toker     OS << Node->getName().str() << ":";
9665bbcdfaSMichael Gottesman     switch (ViewMachineBlockFreqPropagationDAG) {
9765bbcdfaSMichael Gottesman     case GVDT_Fraction:
98b0c1ed8fSMichael Gottesman       Graph->printBlockFreq(OS, Node);
9965bbcdfaSMichael Gottesman       break;
10065bbcdfaSMichael Gottesman     case GVDT_Integer:
10165bbcdfaSMichael Gottesman       OS << Graph->getBlockFreq(Node).getFrequency();
10265bbcdfaSMichael Gottesman       break;
10365bbcdfaSMichael Gottesman     case GVDT_None:
10465bbcdfaSMichael Gottesman       llvm_unreachable("If we are not supposed to render a graph we should "
10565bbcdfaSMichael Gottesman                        "never reach this point.");
10665bbcdfaSMichael Gottesman     }
10765bbcdfaSMichael Gottesman 
108e69170a1SAlp Toker     return Result;
10965bbcdfaSMichael Gottesman   }
110*69317f2eSXinliang David Li   static std::string getEdgeAttributes(const MachineBasicBlock *Node,
111*69317f2eSXinliang David Li                                        EdgeIter EI,
112*69317f2eSXinliang David Li                                        const MachineBlockFrequencyInfo *MBFI) {
113*69317f2eSXinliang David Li     MachineBranchProbabilityInfo &MBPI =
114*69317f2eSXinliang David Li         MBFI->getAnalysis<MachineBranchProbabilityInfo>();
115*69317f2eSXinliang David Li     BranchProbability BP = MBPI.getEdgeProbability(Node, EI);
116*69317f2eSXinliang David Li     uint32_t N = BP.getNumerator();
117*69317f2eSXinliang David Li     uint32_t D = BP.getDenominator();
118*69317f2eSXinliang David Li     double Percent = 100.0 * N / D;
119*69317f2eSXinliang David Li     std::string Str;
120*69317f2eSXinliang David Li     raw_string_ostream OS(Str);
121*69317f2eSXinliang David Li     OS << format("label=\"%.1f%%\"", Percent);
122*69317f2eSXinliang David Li     OS.flush();
123*69317f2eSXinliang David Li     return Str;
124*69317f2eSXinliang David Li   }
12565bbcdfaSMichael Gottesman };
12665bbcdfaSMichael Gottesman 
12765bbcdfaSMichael Gottesman } // end namespace llvm
12865bbcdfaSMichael Gottesman #endif
12965bbcdfaSMichael Gottesman 
130875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
131875ebd5fSJakub Staszak                       "Machine Block Frequency Analysis", true, true)
132875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
13310be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
134875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
135875ebd5fSJakub Staszak                     "Machine Block Frequency Analysis", true, true)
136875ebd5fSJakub Staszak 
137875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0;
138875ebd5fSJakub Staszak 
139bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
140bc157084SXinliang David Li     : MachineFunctionPass(ID) {
141875ebd5fSJakub Staszak   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
142875ebd5fSJakub Staszak }
143875ebd5fSJakub Staszak 
1443dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
145875ebd5fSJakub Staszak 
146875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
147875ebd5fSJakub Staszak   AU.addRequired<MachineBranchProbabilityInfo>();
14810be9a88SDuncan P. N. Exon Smith   AU.addRequired<MachineLoopInfo>();
149875ebd5fSJakub Staszak   AU.setPreservesAll();
150875ebd5fSJakub Staszak   MachineFunctionPass::getAnalysisUsage(AU);
151875ebd5fSJakub Staszak }
152875ebd5fSJakub Staszak 
153875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
154748fe483SMichael Gottesman   MachineBranchProbabilityInfo &MBPI =
155748fe483SMichael Gottesman       getAnalysis<MachineBranchProbabilityInfo>();
15610be9a88SDuncan P. N. Exon Smith   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
1573dbe1050SDuncan P. N. Exon Smith   if (!MBFI)
1583dbe1050SDuncan P. N. Exon Smith     MBFI.reset(new ImplType);
1595e67b666SCong Hou   MBFI->calculate(F, MBPI, MLI);
16065bbcdfaSMichael Gottesman #ifndef NDEBUG
16180457ce5SXinliang David Li   if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
16280457ce5SXinliang David Li       (ViewMachineBlockFreqFuncName.empty() ||
16380457ce5SXinliang David Li        F.getName().equals(ViewMachineBlockFreqFuncName))) {
16465bbcdfaSMichael Gottesman     view();
16565bbcdfaSMichael Gottesman   }
16665bbcdfaSMichael Gottesman #endif
167875ebd5fSJakub Staszak   return false;
168875ebd5fSJakub Staszak }
169875ebd5fSJakub Staszak 
1703dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
1713dbe1050SDuncan P. N. Exon Smith 
17265bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation
17365bbcdfaSMichael Gottesman /// rendered using dot.
17465bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const {
17565bbcdfaSMichael Gottesman // This code is only for debugging.
17665bbcdfaSMichael Gottesman #ifndef NDEBUG
17765bbcdfaSMichael Gottesman   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
17865bbcdfaSMichael Gottesman             "MachineBlockFrequencyDAGs");
17965bbcdfaSMichael Gottesman #else
180748fe483SMichael Gottesman   errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
181748fe483SMichael Gottesman             "on systems with Graphviz or gv!\n";
18265bbcdfaSMichael Gottesman #endif // NDEBUG
18365bbcdfaSMichael Gottesman }
18465bbcdfaSMichael Gottesman 
185bc157084SXinliang David Li BlockFrequency
186bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
1873dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
188875ebd5fSJakub Staszak }
18965bbcdfaSMichael Gottesman 
190936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
19110be9a88SDuncan P. N. Exon Smith   return MBFI ? MBFI->getFunction() : nullptr;
19265bbcdfaSMichael Gottesman }
193fd5c4b2cSMichael Gottesman 
194fd5c4b2cSMichael Gottesman raw_ostream &
195fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
196fd5c4b2cSMichael Gottesman                                           const BlockFrequency Freq) const {
1973dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
198fd5c4b2cSMichael Gottesman }
199fd5c4b2cSMichael Gottesman 
200fd5c4b2cSMichael Gottesman raw_ostream &
201fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
202fd5c4b2cSMichael Gottesman                                           const MachineBasicBlock *MBB) const {
2033dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
204fd5c4b2cSMichael Gottesman }
205fd5c4b2cSMichael Gottesman 
2065e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
2073dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getEntryFreq() : 0;
208fd5c4b2cSMichael Gottesman }
209