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 
14*10be9a88SDuncan P. N. Exon Smith #define DEBUG_TYPE "block-freq"
15875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
16689a5073SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
17875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
18*10be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineFunction.h"
19*10be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineLoopInfo.h"
20ed0881b2SChandler Carruth #include "llvm/CodeGen/Passes.h"
21ed0881b2SChandler Carruth #include "llvm/InitializePasses.h"
2265bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h"
2365bbcdfaSMichael Gottesman #include "llvm/Support/Debug.h"
2465bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h"
25875ebd5fSJakub Staszak 
26875ebd5fSJakub Staszak using namespace llvm;
27875ebd5fSJakub Staszak 
2865bbcdfaSMichael Gottesman #ifndef NDEBUG
2965bbcdfaSMichael Gottesman enum GVDAGType {
3065bbcdfaSMichael Gottesman   GVDT_None,
3165bbcdfaSMichael Gottesman   GVDT_Fraction,
3265bbcdfaSMichael Gottesman   GVDT_Integer
3365bbcdfaSMichael Gottesman };
3465bbcdfaSMichael Gottesman 
3565bbcdfaSMichael Gottesman static cl::opt<GVDAGType>
3665bbcdfaSMichael Gottesman ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
3765bbcdfaSMichael Gottesman                                    cl::Hidden,
3865bbcdfaSMichael Gottesman           cl::desc("Pop up a window to show a dag displaying how machine block "
39748fe483SMichael Gottesman                    "frequencies propagate through the CFG."),
4065bbcdfaSMichael Gottesman           cl::values(
4165bbcdfaSMichael Gottesman             clEnumValN(GVDT_None, "none",
4265bbcdfaSMichael Gottesman                        "do not display graphs."),
4365bbcdfaSMichael Gottesman             clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
4465bbcdfaSMichael Gottesman                        "fractional block frequency representation."),
4565bbcdfaSMichael Gottesman             clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
4665bbcdfaSMichael Gottesman                        "integer fractional block frequency representation."),
4765bbcdfaSMichael Gottesman             clEnumValEnd));
4865bbcdfaSMichael Gottesman 
4965bbcdfaSMichael Gottesman namespace llvm {
5065bbcdfaSMichael Gottesman 
5165bbcdfaSMichael Gottesman template <>
5265bbcdfaSMichael Gottesman 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 
57748fe483SMichael Gottesman   static inline
58748fe483SMichael Gottesman   const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
5965bbcdfaSMichael Gottesman     return G->getFunction()->begin();
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<>
80748fe483SMichael Gottesman struct DOTGraphTraits<MachineBlockFrequencyInfo*> :
81748fe483SMichael Gottesman     public DefaultDOTGraphTraits {
8265bbcdfaSMichael Gottesman   explicit DOTGraphTraits(bool isSimple=false) :
8365bbcdfaSMichael Gottesman     DefaultDOTGraphTraits(isSimple) {}
8465bbcdfaSMichael Gottesman 
8565bbcdfaSMichael Gottesman   static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
8665bbcdfaSMichael Gottesman     return G->getFunction()->getName();
8765bbcdfaSMichael Gottesman   }
8865bbcdfaSMichael Gottesman 
8965bbcdfaSMichael Gottesman   std::string getNodeLabel(const MachineBasicBlock *Node,
9065bbcdfaSMichael Gottesman                            const MachineBlockFrequencyInfo *Graph) {
9165bbcdfaSMichael Gottesman     std::string Result;
9265bbcdfaSMichael Gottesman     raw_string_ostream OS(Result);
9365bbcdfaSMichael Gottesman 
9465bbcdfaSMichael Gottesman     OS << Node->getName().str() << ":";
9565bbcdfaSMichael Gottesman     switch (ViewMachineBlockFreqPropagationDAG) {
9665bbcdfaSMichael Gottesman     case GVDT_Fraction:
97b0c1ed8fSMichael Gottesman       Graph->printBlockFreq(OS, Node);
9865bbcdfaSMichael Gottesman       break;
9965bbcdfaSMichael Gottesman     case GVDT_Integer:
10065bbcdfaSMichael Gottesman       OS << Graph->getBlockFreq(Node).getFrequency();
10165bbcdfaSMichael Gottesman       break;
10265bbcdfaSMichael Gottesman     case GVDT_None:
10365bbcdfaSMichael Gottesman       llvm_unreachable("If we are not supposed to render a graph we should "
10465bbcdfaSMichael Gottesman                        "never reach this point.");
10565bbcdfaSMichael Gottesman     }
10665bbcdfaSMichael Gottesman 
10765bbcdfaSMichael Gottesman     return Result;
10865bbcdfaSMichael Gottesman   }
10965bbcdfaSMichael Gottesman };
11065bbcdfaSMichael Gottesman 
11165bbcdfaSMichael Gottesman 
11265bbcdfaSMichael Gottesman } // end namespace llvm
11365bbcdfaSMichael Gottesman #endif
11465bbcdfaSMichael Gottesman 
115875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
116875ebd5fSJakub Staszak                       "Machine Block Frequency Analysis", true, true)
117875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
118*10be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
119875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
120875ebd5fSJakub Staszak                     "Machine Block Frequency Analysis", true, true)
121875ebd5fSJakub Staszak 
122875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0;
123875ebd5fSJakub Staszak 
124875ebd5fSJakub Staszak 
125748fe483SMichael Gottesman MachineBlockFrequencyInfo::
126748fe483SMichael Gottesman MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
127875ebd5fSJakub Staszak   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
128875ebd5fSJakub Staszak }
129875ebd5fSJakub Staszak 
1303dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
131875ebd5fSJakub Staszak 
132875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
133875ebd5fSJakub Staszak   AU.addRequired<MachineBranchProbabilityInfo>();
134*10be9a88SDuncan P. N. Exon Smith   AU.addRequired<MachineLoopInfo>();
135875ebd5fSJakub Staszak   AU.setPreservesAll();
136875ebd5fSJakub Staszak   MachineFunctionPass::getAnalysisUsage(AU);
137875ebd5fSJakub Staszak }
138875ebd5fSJakub Staszak 
139875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
140748fe483SMichael Gottesman   MachineBranchProbabilityInfo &MBPI =
141748fe483SMichael Gottesman       getAnalysis<MachineBranchProbabilityInfo>();
142*10be9a88SDuncan P. N. Exon Smith   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
1433dbe1050SDuncan P. N. Exon Smith   if (!MBFI)
1443dbe1050SDuncan P. N. Exon Smith     MBFI.reset(new ImplType);
145*10be9a88SDuncan P. N. Exon Smith   MBFI->doFunction(&F, &MBPI, &MLI);
14665bbcdfaSMichael Gottesman #ifndef NDEBUG
14765bbcdfaSMichael Gottesman   if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
14865bbcdfaSMichael Gottesman     view();
14965bbcdfaSMichael Gottesman   }
15065bbcdfaSMichael Gottesman #endif
151875ebd5fSJakub Staszak   return false;
152875ebd5fSJakub Staszak }
153875ebd5fSJakub Staszak 
1543dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
1553dbe1050SDuncan P. N. Exon Smith 
15665bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation
15765bbcdfaSMichael Gottesman /// rendered using dot.
15865bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const {
15965bbcdfaSMichael Gottesman // This code is only for debugging.
16065bbcdfaSMichael Gottesman #ifndef NDEBUG
16165bbcdfaSMichael Gottesman   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
16265bbcdfaSMichael Gottesman             "MachineBlockFrequencyDAGs");
16365bbcdfaSMichael Gottesman #else
164748fe483SMichael Gottesman   errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
165748fe483SMichael Gottesman     "on systems with Graphviz or gv!\n";
16665bbcdfaSMichael Gottesman #endif // NDEBUG
16765bbcdfaSMichael Gottesman }
16865bbcdfaSMichael Gottesman 
169a60d130fSJakub Staszak BlockFrequency MachineBlockFrequencyInfo::
17096f8c551SJakub Staszak getBlockFreq(const MachineBasicBlock *MBB) const {
1713dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
172875ebd5fSJakub Staszak }
17365bbcdfaSMichael Gottesman 
174936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
175*10be9a88SDuncan P. N. Exon Smith   return MBFI ? MBFI->getFunction() : nullptr;
17665bbcdfaSMichael Gottesman }
177fd5c4b2cSMichael Gottesman 
178fd5c4b2cSMichael Gottesman raw_ostream &
179fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
180fd5c4b2cSMichael Gottesman                                           const BlockFrequency Freq) const {
1813dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
182fd5c4b2cSMichael Gottesman }
183fd5c4b2cSMichael Gottesman 
184fd5c4b2cSMichael Gottesman raw_ostream &
185fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
186fd5c4b2cSMichael Gottesman                                           const MachineBasicBlock *MBB) const {
1873dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
188fd5c4b2cSMichael Gottesman }
189fd5c4b2cSMichael Gottesman 
1905e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
1913dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getEntryFreq() : 0;
192fd5c4b2cSMichael Gottesman }
193