1*689a5073SDuncan 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"
15*689a5073SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
16875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
17ed0881b2SChandler Carruth #include "llvm/CodeGen/Passes.h"
18ed0881b2SChandler Carruth #include "llvm/InitializePasses.h"
1965bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h"
2065bbcdfaSMichael Gottesman #include "llvm/Support/Debug.h"
2165bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h"
22875ebd5fSJakub Staszak 
23875ebd5fSJakub Staszak using namespace llvm;
24875ebd5fSJakub Staszak 
2565bbcdfaSMichael Gottesman #ifndef NDEBUG
2665bbcdfaSMichael Gottesman enum GVDAGType {
2765bbcdfaSMichael Gottesman   GVDT_None,
2865bbcdfaSMichael Gottesman   GVDT_Fraction,
2965bbcdfaSMichael Gottesman   GVDT_Integer
3065bbcdfaSMichael Gottesman };
3165bbcdfaSMichael Gottesman 
3265bbcdfaSMichael Gottesman static cl::opt<GVDAGType>
3365bbcdfaSMichael Gottesman ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
3465bbcdfaSMichael Gottesman                                    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."),
3765bbcdfaSMichael Gottesman           cl::values(
3865bbcdfaSMichael Gottesman             clEnumValN(GVDT_None, "none",
3965bbcdfaSMichael Gottesman                        "do not display graphs."),
4065bbcdfaSMichael Gottesman             clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
4165bbcdfaSMichael Gottesman                        "fractional block frequency representation."),
4265bbcdfaSMichael Gottesman             clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
4365bbcdfaSMichael Gottesman                        "integer fractional block frequency representation."),
4465bbcdfaSMichael Gottesman             clEnumValEnd));
4565bbcdfaSMichael Gottesman 
4665bbcdfaSMichael Gottesman namespace llvm {
4765bbcdfaSMichael Gottesman 
4865bbcdfaSMichael Gottesman template <>
4965bbcdfaSMichael Gottesman struct GraphTraits<MachineBlockFrequencyInfo *> {
5065bbcdfaSMichael Gottesman   typedef const MachineBasicBlock NodeType;
5165bbcdfaSMichael Gottesman   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
5265bbcdfaSMichael Gottesman   typedef MachineFunction::const_iterator nodes_iterator;
5365bbcdfaSMichael Gottesman 
54748fe483SMichael Gottesman   static inline
55748fe483SMichael Gottesman   const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
5665bbcdfaSMichael Gottesman     return G->getFunction()->begin();
5765bbcdfaSMichael Gottesman   }
58748fe483SMichael Gottesman 
5965bbcdfaSMichael Gottesman   static ChildIteratorType child_begin(const NodeType *N) {
6065bbcdfaSMichael Gottesman     return N->succ_begin();
6165bbcdfaSMichael Gottesman   }
62748fe483SMichael Gottesman 
6365bbcdfaSMichael Gottesman   static ChildIteratorType child_end(const NodeType *N) {
6465bbcdfaSMichael Gottesman     return N->succ_end();
6565bbcdfaSMichael Gottesman   }
66748fe483SMichael Gottesman 
6765bbcdfaSMichael Gottesman   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
6865bbcdfaSMichael Gottesman     return G->getFunction()->begin();
6965bbcdfaSMichael Gottesman   }
70748fe483SMichael Gottesman 
7165bbcdfaSMichael Gottesman   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
7265bbcdfaSMichael Gottesman     return G->getFunction()->end();
7365bbcdfaSMichael Gottesman   }
7465bbcdfaSMichael Gottesman };
7565bbcdfaSMichael Gottesman 
7665bbcdfaSMichael Gottesman template<>
77748fe483SMichael Gottesman struct DOTGraphTraits<MachineBlockFrequencyInfo*> :
78748fe483SMichael Gottesman     public DefaultDOTGraphTraits {
7965bbcdfaSMichael Gottesman   explicit DOTGraphTraits(bool isSimple=false) :
8065bbcdfaSMichael Gottesman     DefaultDOTGraphTraits(isSimple) {}
8165bbcdfaSMichael Gottesman 
8265bbcdfaSMichael Gottesman   static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
8365bbcdfaSMichael Gottesman     return G->getFunction()->getName();
8465bbcdfaSMichael Gottesman   }
8565bbcdfaSMichael Gottesman 
8665bbcdfaSMichael Gottesman   std::string getNodeLabel(const MachineBasicBlock *Node,
8765bbcdfaSMichael Gottesman                            const MachineBlockFrequencyInfo *Graph) {
8865bbcdfaSMichael Gottesman     std::string Result;
8965bbcdfaSMichael Gottesman     raw_string_ostream OS(Result);
9065bbcdfaSMichael Gottesman 
9165bbcdfaSMichael Gottesman     OS << Node->getName().str() << ":";
9265bbcdfaSMichael Gottesman     switch (ViewMachineBlockFreqPropagationDAG) {
9365bbcdfaSMichael Gottesman     case GVDT_Fraction:
94b0c1ed8fSMichael Gottesman       Graph->printBlockFreq(OS, Node);
9565bbcdfaSMichael Gottesman       break;
9665bbcdfaSMichael Gottesman     case GVDT_Integer:
9765bbcdfaSMichael Gottesman       OS << Graph->getBlockFreq(Node).getFrequency();
9865bbcdfaSMichael Gottesman       break;
9965bbcdfaSMichael Gottesman     case GVDT_None:
10065bbcdfaSMichael Gottesman       llvm_unreachable("If we are not supposed to render a graph we should "
10165bbcdfaSMichael Gottesman                        "never reach this point.");
10265bbcdfaSMichael Gottesman     }
10365bbcdfaSMichael Gottesman 
10465bbcdfaSMichael Gottesman     return Result;
10565bbcdfaSMichael Gottesman   }
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)
115875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
116875ebd5fSJakub Staszak                     "Machine Block Frequency Analysis", true, true)
117875ebd5fSJakub Staszak 
118875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0;
119875ebd5fSJakub Staszak 
120875ebd5fSJakub Staszak 
121748fe483SMichael Gottesman MachineBlockFrequencyInfo::
122748fe483SMichael Gottesman MachineBlockFrequencyInfo() :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>();
130875ebd5fSJakub Staszak   AU.setPreservesAll();
131875ebd5fSJakub Staszak   MachineFunctionPass::getAnalysisUsage(AU);
132875ebd5fSJakub Staszak }
133875ebd5fSJakub Staszak 
134875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
135748fe483SMichael Gottesman   MachineBranchProbabilityInfo &MBPI =
136748fe483SMichael Gottesman     getAnalysis<MachineBranchProbabilityInfo>();
1373dbe1050SDuncan P. N. Exon Smith   if (!MBFI)
1383dbe1050SDuncan P. N. Exon Smith     MBFI.reset(new ImplType);
139875ebd5fSJakub Staszak   MBFI->doFunction(&F, &MBPI);
14065bbcdfaSMichael Gottesman #ifndef NDEBUG
14165bbcdfaSMichael Gottesman   if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
14265bbcdfaSMichael Gottesman     view();
14365bbcdfaSMichael Gottesman   }
14465bbcdfaSMichael Gottesman #endif
145875ebd5fSJakub Staszak   return false;
146875ebd5fSJakub Staszak }
147875ebd5fSJakub Staszak 
1483dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
1493dbe1050SDuncan P. N. Exon Smith 
15065bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation
15165bbcdfaSMichael Gottesman /// rendered using dot.
15265bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const {
15365bbcdfaSMichael Gottesman // This code is only for debugging.
15465bbcdfaSMichael Gottesman #ifndef NDEBUG
15565bbcdfaSMichael Gottesman   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
15665bbcdfaSMichael Gottesman             "MachineBlockFrequencyDAGs");
15765bbcdfaSMichael Gottesman #else
158748fe483SMichael Gottesman   errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
159748fe483SMichael Gottesman     "on systems with Graphviz or gv!\n";
16065bbcdfaSMichael Gottesman #endif // NDEBUG
16165bbcdfaSMichael Gottesman }
16265bbcdfaSMichael Gottesman 
163a60d130fSJakub Staszak BlockFrequency MachineBlockFrequencyInfo::
16496f8c551SJakub Staszak getBlockFreq(const MachineBasicBlock *MBB) const {
1653dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
166875ebd5fSJakub Staszak }
16765bbcdfaSMichael Gottesman 
168936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
1693dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->Fn : nullptr;
17065bbcdfaSMichael Gottesman }
171fd5c4b2cSMichael Gottesman 
172fd5c4b2cSMichael Gottesman raw_ostream &
173fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
174fd5c4b2cSMichael Gottesman                                           const BlockFrequency Freq) const {
1753dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
176fd5c4b2cSMichael Gottesman }
177fd5c4b2cSMichael Gottesman 
178fd5c4b2cSMichael Gottesman raw_ostream &
179fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
180fd5c4b2cSMichael Gottesman                                           const MachineBasicBlock *MBB) const {
1813dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
182fd5c4b2cSMichael Gottesman }
183fd5c4b2cSMichael Gottesman 
1845e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
1853dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getEntryFreq() : 0;
186fd5c4b2cSMichael Gottesman }
187