Reland 196270 "Generalize debug info / EH emission in AsmPrinter"
[oota-llvm.git] / lib / CodeGen / MachineBlockFrequencyInfo.cpp
1 //====----- MachineBlockFrequencyInfo.cpp - Machine Block 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/BlockFrequencyImpl.h"
16 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/GraphWriter.h"
22
23 using namespace llvm;
24
25 #ifndef NDEBUG
26 enum GVDAGType {
27   GVDT_None,
28   GVDT_Fraction,
29   GVDT_Integer
30 };
31
32 static cl::opt<GVDAGType>
33 ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
34                                    cl::Hidden,
35           cl::desc("Pop up a window to show a dag displaying how machine block "
36                    "frequencies propgate through the CFG."),
37           cl::values(
38             clEnumValN(GVDT_None, "none",
39                        "do not display graphs."),
40             clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
41                        "fractional block frequency representation."),
42             clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
43                        "integer fractional block frequency representation."),
44             clEnumValEnd));
45
46 namespace llvm {
47
48 template <>
49 struct GraphTraits<MachineBlockFrequencyInfo *> {
50   typedef const MachineBasicBlock NodeType;
51   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
52   typedef MachineFunction::const_iterator nodes_iterator;
53
54   static inline const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
55     return G->getFunction()->begin();
56   }
57   static ChildIteratorType child_begin(const NodeType *N) {
58     return N->succ_begin();
59   }
60   static ChildIteratorType child_end(const NodeType *N) {
61     return N->succ_end();
62   }
63   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
64     return G->getFunction()->begin();
65   }
66   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
67     return G->getFunction()->end();
68   }
69 };
70
71 template<>
72 struct DOTGraphTraits<MachineBlockFrequencyInfo*> : public DefaultDOTGraphTraits {
73   explicit DOTGraphTraits(bool isSimple=false) :
74     DefaultDOTGraphTraits(isSimple) {}
75
76   static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
77     return G->getFunction()->getName();
78   }
79
80   std::string getNodeLabel(const MachineBasicBlock *Node,
81                            const MachineBlockFrequencyInfo *Graph) {
82     std::string Result;
83     raw_string_ostream OS(Result);
84
85     OS << Node->getName().str() << ":";
86     switch (ViewMachineBlockFreqPropagationDAG) {
87     case GVDT_Fraction:
88       Graph->getBlockFreq(Node).print(OS);
89       break;
90     case GVDT_Integer:
91       OS << Graph->getBlockFreq(Node).getFrequency();
92       break;
93     case GVDT_None:
94       llvm_unreachable("If we are not supposed to render a graph we should "
95                        "never reach this point.");
96     }
97
98     return Result;
99   }
100 };
101
102
103 } // end namespace llvm
104 #endif
105
106 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
107                       "Machine Block Frequency Analysis", true, true)
108 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
109 INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
110                     "Machine Block Frequency Analysis", true, true)
111
112 char MachineBlockFrequencyInfo::ID = 0;
113
114
115 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() : MachineFunctionPass(ID) {
116   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
117   MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction,
118                                 MachineBranchProbabilityInfo>();
119 }
120
121 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {
122   delete MBFI;
123 }
124
125 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
126   AU.addRequired<MachineBranchProbabilityInfo>();
127   AU.setPreservesAll();
128   MachineFunctionPass::getAnalysisUsage(AU);
129 }
130
131 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
132   MachineBranchProbabilityInfo &MBPI = getAnalysis<MachineBranchProbabilityInfo>();
133   MBFI->doFunction(&F, &MBPI);
134 #ifndef NDEBUG
135   if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
136     view();
137   }
138 #endif
139   return false;
140 }
141
142 /// Pop up a ghostview window with the current block frequency propagation
143 /// rendered using dot.
144 void MachineBlockFrequencyInfo::view() const {
145 // This code is only for debugging.
146 #ifndef NDEBUG
147   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
148             "MachineBlockFrequencyDAGs");
149 #else
150   errs() << "BlockFrequencyInfo::view is only available in debug builds on "
151             "systems with Graphviz or gv!\n";
152 #endif // NDEBUG
153 }
154
155 BlockFrequency MachineBlockFrequencyInfo::
156 getBlockFreq(const MachineBasicBlock *MBB) const {
157   return MBFI->getBlockFreq(MBB);
158 }
159
160 MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
161   return MBFI->Fn;
162 }
163