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
32*30c50f3cSXinliang David Li enum GVDAGType { GVDT_None, GVDT_Fraction, GVDT_Integer, GVDT_Count };
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."),
45*30c50f3cSXinliang David Li                clEnumValN(GVDT_Count, "count", "display a graph using the real "
46*30c50f3cSXinliang David Li                                                "profile count if available."),
47*30c50f3cSXinliang David Li 
4865bbcdfaSMichael Gottesman                clEnumValEnd));
4965bbcdfaSMichael Gottesman 
5080457ce5SXinliang David Li static cl::opt<std::string> ViewMachineBlockFreqFuncName("view-mbfi-func-name",
5180457ce5SXinliang David Li                                                          cl::Hidden);
5280457ce5SXinliang David Li 
5365bbcdfaSMichael Gottesman namespace llvm {
5465bbcdfaSMichael Gottesman 
55bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
5665bbcdfaSMichael Gottesman   typedef const MachineBasicBlock NodeType;
5765bbcdfaSMichael Gottesman   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
5865bbcdfaSMichael Gottesman   typedef MachineFunction::const_iterator nodes_iterator;
5965bbcdfaSMichael Gottesman 
60bc157084SXinliang David Li   static inline const NodeType *
61bc157084SXinliang David Li   getEntryNode(const MachineBlockFrequencyInfo *G) {
620ac8eb91SDuncan P. N. Exon Smith     return &G->getFunction()->front();
6365bbcdfaSMichael Gottesman   }
64748fe483SMichael Gottesman 
6565bbcdfaSMichael Gottesman   static ChildIteratorType child_begin(const NodeType *N) {
6665bbcdfaSMichael Gottesman     return N->succ_begin();
6765bbcdfaSMichael Gottesman   }
68748fe483SMichael Gottesman 
6965bbcdfaSMichael Gottesman   static ChildIteratorType child_end(const NodeType *N) {
7065bbcdfaSMichael Gottesman     return N->succ_end();
7165bbcdfaSMichael Gottesman   }
72748fe483SMichael Gottesman 
7365bbcdfaSMichael Gottesman   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
7465bbcdfaSMichael Gottesman     return G->getFunction()->begin();
7565bbcdfaSMichael Gottesman   }
76748fe483SMichael Gottesman 
7765bbcdfaSMichael Gottesman   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
7865bbcdfaSMichael Gottesman     return G->getFunction()->end();
7965bbcdfaSMichael Gottesman   }
8065bbcdfaSMichael Gottesman };
8165bbcdfaSMichael Gottesman 
8265bbcdfaSMichael Gottesman template <>
83bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *>
84bc157084SXinliang David Li     : public DefaultDOTGraphTraits {
85bc157084SXinliang David Li   explicit DOTGraphTraits(bool isSimple = false)
86bc157084SXinliang David Li       : DefaultDOTGraphTraits(isSimple) {}
8765bbcdfaSMichael Gottesman 
8869317f2eSXinliang David Li   typedef MachineBasicBlock::const_succ_iterator EdgeIter;
8965bbcdfaSMichael Gottesman   static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
9065bbcdfaSMichael Gottesman     return G->getFunction()->getName();
9165bbcdfaSMichael Gottesman   }
9265bbcdfaSMichael Gottesman 
9365bbcdfaSMichael Gottesman   std::string getNodeLabel(const MachineBasicBlock *Node,
9465bbcdfaSMichael Gottesman                            const MachineBlockFrequencyInfo *Graph) {
95e69170a1SAlp Toker     std::string Result;
96e69170a1SAlp Toker     raw_string_ostream OS(Result);
9765bbcdfaSMichael Gottesman 
98e69170a1SAlp Toker     OS << Node->getName().str() << " : ";
9965bbcdfaSMichael Gottesman     switch (ViewMachineBlockFreqPropagationDAG) {
10065bbcdfaSMichael Gottesman     case GVDT_Fraction:
101b0c1ed8fSMichael Gottesman       Graph->printBlockFreq(OS, Node);
10265bbcdfaSMichael Gottesman       break;
10365bbcdfaSMichael Gottesman     case GVDT_Integer:
10465bbcdfaSMichael Gottesman       OS << Graph->getBlockFreq(Node).getFrequency();
10565bbcdfaSMichael Gottesman       break;
106*30c50f3cSXinliang David Li     case GVDT_Count: {
107*30c50f3cSXinliang David Li       auto Count = Graph->getBlockProfileCount(Node);
108*30c50f3cSXinliang David Li       if (Count)
109*30c50f3cSXinliang David Li         OS << Count.getValue();
110*30c50f3cSXinliang David Li       else
111*30c50f3cSXinliang David Li         OS << "Unknown";
112*30c50f3cSXinliang David Li       break;
113*30c50f3cSXinliang David Li     }
11465bbcdfaSMichael Gottesman     case GVDT_None:
11565bbcdfaSMichael Gottesman       llvm_unreachable("If we are not supposed to render a graph we should "
11665bbcdfaSMichael Gottesman                        "never reach this point.");
11765bbcdfaSMichael Gottesman     }
118e69170a1SAlp Toker     return Result;
11965bbcdfaSMichael Gottesman   }
12069317f2eSXinliang David Li   static std::string getEdgeAttributes(const MachineBasicBlock *Node,
12169317f2eSXinliang David Li                                        EdgeIter EI,
12269317f2eSXinliang David Li                                        const MachineBlockFrequencyInfo *MBFI) {
12369317f2eSXinliang David Li     MachineBranchProbabilityInfo &MBPI =
12469317f2eSXinliang David Li         MBFI->getAnalysis<MachineBranchProbabilityInfo>();
12569317f2eSXinliang David Li     BranchProbability BP = MBPI.getEdgeProbability(Node, EI);
12669317f2eSXinliang David Li     uint32_t N = BP.getNumerator();
12769317f2eSXinliang David Li     uint32_t D = BP.getDenominator();
12869317f2eSXinliang David Li     double Percent = 100.0 * N / D;
12969317f2eSXinliang David Li     std::string Str;
13069317f2eSXinliang David Li     raw_string_ostream OS(Str);
13169317f2eSXinliang David Li     OS << format("label=\"%.1f%%\"", Percent);
13269317f2eSXinliang David Li     OS.flush();
13369317f2eSXinliang David Li     return Str;
13469317f2eSXinliang David Li   }
13565bbcdfaSMichael Gottesman };
13665bbcdfaSMichael Gottesman 
13765bbcdfaSMichael Gottesman } // end namespace llvm
13865bbcdfaSMichael Gottesman #endif
13965bbcdfaSMichael Gottesman 
140875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
141875ebd5fSJakub Staszak                       "Machine Block Frequency Analysis", true, true)
142875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
14310be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
144875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
145875ebd5fSJakub Staszak                     "Machine Block Frequency Analysis", true, true)
146875ebd5fSJakub Staszak 
147875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0;
148875ebd5fSJakub Staszak 
149bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
150bc157084SXinliang David Li     : MachineFunctionPass(ID) {
151875ebd5fSJakub Staszak   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
152875ebd5fSJakub Staszak }
153875ebd5fSJakub Staszak 
1543dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
155875ebd5fSJakub Staszak 
156875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
157875ebd5fSJakub Staszak   AU.addRequired<MachineBranchProbabilityInfo>();
15810be9a88SDuncan P. N. Exon Smith   AU.addRequired<MachineLoopInfo>();
159875ebd5fSJakub Staszak   AU.setPreservesAll();
160875ebd5fSJakub Staszak   MachineFunctionPass::getAnalysisUsage(AU);
161875ebd5fSJakub Staszak }
162875ebd5fSJakub Staszak 
163875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
164748fe483SMichael Gottesman   MachineBranchProbabilityInfo &MBPI =
165748fe483SMichael Gottesman       getAnalysis<MachineBranchProbabilityInfo>();
16610be9a88SDuncan P. N. Exon Smith   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
1673dbe1050SDuncan P. N. Exon Smith   if (!MBFI)
1683dbe1050SDuncan P. N. Exon Smith     MBFI.reset(new ImplType);
1695e67b666SCong Hou   MBFI->calculate(F, MBPI, MLI);
17065bbcdfaSMichael Gottesman #ifndef NDEBUG
17180457ce5SXinliang David Li   if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
17280457ce5SXinliang David Li       (ViewMachineBlockFreqFuncName.empty() ||
17380457ce5SXinliang David Li        F.getName().equals(ViewMachineBlockFreqFuncName))) {
17465bbcdfaSMichael Gottesman     view();
17565bbcdfaSMichael Gottesman   }
17665bbcdfaSMichael Gottesman #endif
177875ebd5fSJakub Staszak   return false;
178875ebd5fSJakub Staszak }
179875ebd5fSJakub Staszak 
1803dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
1813dbe1050SDuncan P. N. Exon Smith 
18265bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation
18365bbcdfaSMichael Gottesman /// rendered using dot.
18465bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const {
18565bbcdfaSMichael Gottesman // This code is only for debugging.
18665bbcdfaSMichael Gottesman #ifndef NDEBUG
18765bbcdfaSMichael Gottesman   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
18865bbcdfaSMichael Gottesman             "MachineBlockFrequencyDAGs");
18965bbcdfaSMichael Gottesman #else
190748fe483SMichael Gottesman   errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
191748fe483SMichael Gottesman             "on systems with Graphviz or gv!\n";
19265bbcdfaSMichael Gottesman #endif // NDEBUG
19365bbcdfaSMichael Gottesman }
19465bbcdfaSMichael Gottesman 
195bc157084SXinliang David Li BlockFrequency
196bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
1973dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
198875ebd5fSJakub Staszak }
19965bbcdfaSMichael Gottesman 
200*30c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
201*30c50f3cSXinliang David Li     const MachineBasicBlock *MBB) const {
202*30c50f3cSXinliang David Li   const Function *F = MBFI->getFunction()->getFunction();
203*30c50f3cSXinliang David Li   return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
204*30c50f3cSXinliang David Li }
205*30c50f3cSXinliang David Li 
206936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
20710be9a88SDuncan P. N. Exon Smith   return MBFI ? MBFI->getFunction() : nullptr;
20865bbcdfaSMichael Gottesman }
209fd5c4b2cSMichael Gottesman 
210fd5c4b2cSMichael Gottesman raw_ostream &
211fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
212fd5c4b2cSMichael Gottesman                                           const BlockFrequency Freq) const {
2133dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
214fd5c4b2cSMichael Gottesman }
215fd5c4b2cSMichael Gottesman 
216fd5c4b2cSMichael Gottesman raw_ostream &
217fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
218fd5c4b2cSMichael Gottesman                                           const MachineBasicBlock *MBB) const {
2193dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
220fd5c4b2cSMichael Gottesman }
221fd5c4b2cSMichael Gottesman 
2225e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
2233dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getEntryFreq() : 0;
224fd5c4b2cSMichael Gottesman }
225