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"
2365bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h"
24875ebd5fSJakub Staszak 
25875ebd5fSJakub Staszak using namespace llvm;
26875ebd5fSJakub Staszak 
271b9dde08SChandler Carruth #define DEBUG_TYPE "block-freq"
281b9dde08SChandler Carruth 
2965bbcdfaSMichael Gottesman #ifndef NDEBUG
30bc157084SXinliang David Li enum GVDAGType { GVDT_None, GVDT_Fraction, GVDT_Integer };
3165bbcdfaSMichael Gottesman 
32bc157084SXinliang David Li static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
33bc157084SXinliang David Li     "view-machine-block-freq-propagation-dags", cl::Hidden,
3465bbcdfaSMichael Gottesman     cl::desc("Pop up a window to show a dag displaying how machine block "
35748fe483SMichael Gottesman              "frequencies propagate through the CFG."),
36bc157084SXinliang David Li     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
37bc157084SXinliang David Li                clEnumValN(GVDT_Fraction, "fraction",
38bc157084SXinliang David Li                           "display a graph using the "
3965bbcdfaSMichael Gottesman                           "fractional block frequency representation."),
40bc157084SXinliang David Li                clEnumValN(GVDT_Integer, "integer",
41bc157084SXinliang David Li                           "display a graph using the raw "
4265bbcdfaSMichael Gottesman                           "integer fractional block frequency representation."),
4365bbcdfaSMichael Gottesman                clEnumValEnd));
4465bbcdfaSMichael Gottesman 
45*80457ce5SXinliang David Li static cl::opt<std::string> ViewMachineBlockFreqFuncName("view-mbfi-func-name",
46*80457ce5SXinliang David Li                                                          cl::Hidden);
47*80457ce5SXinliang David Li 
4865bbcdfaSMichael Gottesman namespace llvm {
4965bbcdfaSMichael Gottesman 
50bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
5165bbcdfaSMichael Gottesman   typedef const MachineBasicBlock NodeType;
5265bbcdfaSMichael Gottesman   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
5365bbcdfaSMichael Gottesman   typedef MachineFunction::const_iterator nodes_iterator;
5465bbcdfaSMichael Gottesman 
55bc157084SXinliang David Li   static inline const NodeType *
56bc157084SXinliang David Li   getEntryNode(const MachineBlockFrequencyInfo *G) {
570ac8eb91SDuncan P. N. Exon Smith     return &G->getFunction()->front();
5865bbcdfaSMichael Gottesman   }
59748fe483SMichael Gottesman 
6065bbcdfaSMichael Gottesman   static ChildIteratorType child_begin(const NodeType *N) {
6165bbcdfaSMichael Gottesman     return N->succ_begin();
6265bbcdfaSMichael Gottesman   }
63748fe483SMichael Gottesman 
6465bbcdfaSMichael Gottesman   static ChildIteratorType child_end(const NodeType *N) {
6565bbcdfaSMichael Gottesman     return N->succ_end();
6665bbcdfaSMichael Gottesman   }
67748fe483SMichael Gottesman 
6865bbcdfaSMichael Gottesman   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
6965bbcdfaSMichael Gottesman     return G->getFunction()->begin();
7065bbcdfaSMichael Gottesman   }
71748fe483SMichael Gottesman 
7265bbcdfaSMichael Gottesman   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
7365bbcdfaSMichael Gottesman     return G->getFunction()->end();
7465bbcdfaSMichael Gottesman   }
7565bbcdfaSMichael Gottesman };
7665bbcdfaSMichael Gottesman 
7765bbcdfaSMichael Gottesman template <>
78bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *>
79bc157084SXinliang David Li     : public DefaultDOTGraphTraits {
80bc157084SXinliang David Li   explicit DOTGraphTraits(bool isSimple = false)
81bc157084SXinliang David Li       : DefaultDOTGraphTraits(isSimple) {}
8265bbcdfaSMichael Gottesman 
8365bbcdfaSMichael Gottesman   static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
8465bbcdfaSMichael Gottesman     return G->getFunction()->getName();
8565bbcdfaSMichael Gottesman   }
8665bbcdfaSMichael Gottesman 
8765bbcdfaSMichael Gottesman   std::string getNodeLabel(const MachineBasicBlock *Node,
8865bbcdfaSMichael Gottesman                            const MachineBlockFrequencyInfo *Graph) {
89e69170a1SAlp Toker     std::string Result;
90e69170a1SAlp Toker     raw_string_ostream OS(Result);
9165bbcdfaSMichael Gottesman 
92e69170a1SAlp Toker     OS << Node->getName().str() << ":";
9365bbcdfaSMichael Gottesman     switch (ViewMachineBlockFreqPropagationDAG) {
9465bbcdfaSMichael Gottesman     case GVDT_Fraction:
95b0c1ed8fSMichael Gottesman       Graph->printBlockFreq(OS, Node);
9665bbcdfaSMichael Gottesman       break;
9765bbcdfaSMichael Gottesman     case GVDT_Integer:
9865bbcdfaSMichael Gottesman       OS << Graph->getBlockFreq(Node).getFrequency();
9965bbcdfaSMichael Gottesman       break;
10065bbcdfaSMichael Gottesman     case GVDT_None:
10165bbcdfaSMichael Gottesman       llvm_unreachable("If we are not supposed to render a graph we should "
10265bbcdfaSMichael Gottesman                        "never reach this point.");
10365bbcdfaSMichael Gottesman     }
10465bbcdfaSMichael Gottesman 
105e69170a1SAlp Toker     return Result;
10665bbcdfaSMichael Gottesman   }
10765bbcdfaSMichael Gottesman };
10865bbcdfaSMichael Gottesman 
10965bbcdfaSMichael Gottesman } // end namespace llvm
11065bbcdfaSMichael Gottesman #endif
11165bbcdfaSMichael Gottesman 
112875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
113875ebd5fSJakub Staszak                       "Machine Block Frequency Analysis", true, true)
114875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
11510be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
116875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
117875ebd5fSJakub Staszak                     "Machine Block Frequency Analysis", true, true)
118875ebd5fSJakub Staszak 
119875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0;
120875ebd5fSJakub Staszak 
121bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
122bc157084SXinliang David Li     : MachineFunctionPass(ID) {
123875ebd5fSJakub Staszak   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
124875ebd5fSJakub Staszak }
125875ebd5fSJakub Staszak 
1263dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
127875ebd5fSJakub Staszak 
128875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
129875ebd5fSJakub Staszak   AU.addRequired<MachineBranchProbabilityInfo>();
13010be9a88SDuncan P. N. Exon Smith   AU.addRequired<MachineLoopInfo>();
131875ebd5fSJakub Staszak   AU.setPreservesAll();
132875ebd5fSJakub Staszak   MachineFunctionPass::getAnalysisUsage(AU);
133875ebd5fSJakub Staszak }
134875ebd5fSJakub Staszak 
135875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
136748fe483SMichael Gottesman   MachineBranchProbabilityInfo &MBPI =
137748fe483SMichael Gottesman       getAnalysis<MachineBranchProbabilityInfo>();
13810be9a88SDuncan P. N. Exon Smith   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
1393dbe1050SDuncan P. N. Exon Smith   if (!MBFI)
1403dbe1050SDuncan P. N. Exon Smith     MBFI.reset(new ImplType);
1415e67b666SCong Hou   MBFI->calculate(F, MBPI, MLI);
14265bbcdfaSMichael Gottesman #ifndef NDEBUG
143*80457ce5SXinliang David Li   if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
144*80457ce5SXinliang David Li       (ViewMachineBlockFreqFuncName.empty() ||
145*80457ce5SXinliang David Li        F.getName().equals(ViewMachineBlockFreqFuncName))) {
14665bbcdfaSMichael Gottesman     view();
14765bbcdfaSMichael Gottesman   }
14865bbcdfaSMichael Gottesman #endif
149875ebd5fSJakub Staszak   return false;
150875ebd5fSJakub Staszak }
151875ebd5fSJakub Staszak 
1523dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
1533dbe1050SDuncan P. N. Exon Smith 
15465bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation
15565bbcdfaSMichael Gottesman /// rendered using dot.
15665bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const {
15765bbcdfaSMichael Gottesman // This code is only for debugging.
15865bbcdfaSMichael Gottesman #ifndef NDEBUG
15965bbcdfaSMichael Gottesman   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
16065bbcdfaSMichael Gottesman             "MachineBlockFrequencyDAGs");
16165bbcdfaSMichael Gottesman #else
162748fe483SMichael Gottesman   errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
163748fe483SMichael Gottesman             "on systems with Graphviz or gv!\n";
16465bbcdfaSMichael Gottesman #endif // NDEBUG
16565bbcdfaSMichael Gottesman }
16665bbcdfaSMichael Gottesman 
167bc157084SXinliang David Li BlockFrequency
168bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
1693dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
170875ebd5fSJakub Staszak }
17165bbcdfaSMichael Gottesman 
172936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
17310be9a88SDuncan P. N. Exon Smith   return MBFI ? MBFI->getFunction() : nullptr;
17465bbcdfaSMichael Gottesman }
175fd5c4b2cSMichael Gottesman 
176fd5c4b2cSMichael Gottesman raw_ostream &
177fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
178fd5c4b2cSMichael Gottesman                                           const BlockFrequency Freq) const {
1793dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
180fd5c4b2cSMichael Gottesman }
181fd5c4b2cSMichael Gottesman 
182fd5c4b2cSMichael Gottesman raw_ostream &
183fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
184fd5c4b2cSMichael Gottesman                                           const MachineBasicBlock *MBB) const {
1853dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
186fd5c4b2cSMichael Gottesman }
187fd5c4b2cSMichael Gottesman 
1885e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
1893dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getEntryFreq() : 0;
190fd5c4b2cSMichael Gottesman }
191