1 //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Loops should be simplified before this analysis.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
15 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
16 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineLoopInfo.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/GraphWriter.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "block-freq"
28 
29 #ifndef NDEBUG
30 enum GVDAGType {
31   GVDT_None,
32   GVDT_Fraction,
33   GVDT_Integer
34 };
35 
36 static cl::opt<GVDAGType>
37 ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
38                                    cl::Hidden,
39           cl::desc("Pop up a window to show a dag displaying how machine block "
40                    "frequencies propagate through the CFG."),
41           cl::values(
42             clEnumValN(GVDT_None, "none",
43                        "do not display graphs."),
44             clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
45                        "fractional block frequency representation."),
46             clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
47                        "integer fractional block frequency representation."),
48             clEnumValEnd));
49 
50 namespace llvm {
51 
52 template <>
53 struct GraphTraits<MachineBlockFrequencyInfo *> {
54   typedef const MachineBasicBlock NodeType;
55   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
56   typedef MachineFunction::const_iterator nodes_iterator;
57 
58   static inline
59   const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
60     return G->getFunction()->begin();
61   }
62 
63   static ChildIteratorType child_begin(const NodeType *N) {
64     return N->succ_begin();
65   }
66 
67   static ChildIteratorType child_end(const NodeType *N) {
68     return N->succ_end();
69   }
70 
71   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
72     return G->getFunction()->begin();
73   }
74 
75   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
76     return G->getFunction()->end();
77   }
78 };
79 
80 template<>
81 struct DOTGraphTraits<MachineBlockFrequencyInfo*> :
82     public DefaultDOTGraphTraits {
83   explicit DOTGraphTraits(bool isSimple=false) :
84     DefaultDOTGraphTraits(isSimple) {}
85 
86   static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
87     return G->getFunction()->getName();
88   }
89 
90   std::string getNodeLabel(const MachineBasicBlock *Node,
91                            const MachineBlockFrequencyInfo *Graph) {
92     string_ostream OS;
93 
94     OS << Node->getName() << ":";
95     switch (ViewMachineBlockFreqPropagationDAG) {
96     case GVDT_Fraction:
97       Graph->printBlockFreq(OS, Node);
98       break;
99     case GVDT_Integer:
100       OS << Graph->getBlockFreq(Node).getFrequency();
101       break;
102     case GVDT_None:
103       llvm_unreachable("If we are not supposed to render a graph we should "
104                        "never reach this point.");
105     }
106 
107     return OS.str();
108   }
109 };
110 
111 
112 } // end namespace llvm
113 #endif
114 
115 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
116                       "Machine Block Frequency Analysis", true, true)
117 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
118 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
119 INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
120                     "Machine Block Frequency Analysis", true, true)
121 
122 char MachineBlockFrequencyInfo::ID = 0;
123 
124 
125 MachineBlockFrequencyInfo::
126 MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
127   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
128 }
129 
130 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
131 
132 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
133   AU.addRequired<MachineBranchProbabilityInfo>();
134   AU.addRequired<MachineLoopInfo>();
135   AU.setPreservesAll();
136   MachineFunctionPass::getAnalysisUsage(AU);
137 }
138 
139 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
140   MachineBranchProbabilityInfo &MBPI =
141       getAnalysis<MachineBranchProbabilityInfo>();
142   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
143   if (!MBFI)
144     MBFI.reset(new ImplType);
145   MBFI->doFunction(&F, &MBPI, &MLI);
146 #ifndef NDEBUG
147   if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
148     view();
149   }
150 #endif
151   return false;
152 }
153 
154 void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
155 
156 /// Pop up a ghostview window with the current block frequency propagation
157 /// rendered using dot.
158 void MachineBlockFrequencyInfo::view() const {
159 // This code is only for debugging.
160 #ifndef NDEBUG
161   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
162             "MachineBlockFrequencyDAGs");
163 #else
164   errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
165     "on systems with Graphviz or gv!\n";
166 #endif // NDEBUG
167 }
168 
169 BlockFrequency MachineBlockFrequencyInfo::
170 getBlockFreq(const MachineBasicBlock *MBB) const {
171   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
172 }
173 
174 const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
175   return MBFI ? MBFI->getFunction() : nullptr;
176 }
177 
178 raw_ostream &
179 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
180                                           const BlockFrequency Freq) const {
181   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
182 }
183 
184 raw_ostream &
185 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
186                                           const MachineBasicBlock *MBB) const {
187   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
188 }
189 
190 uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
191   return MBFI ? MBFI->getEntryFreq() : 0;
192 }
193