blob: 13203d55c467931c325c4544e3d7a28332ead8ee [file] [log] [blame]
Stephen Hines36b56882014-04-23 16:57:46 -07001//====------ MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis ------====//
Jakub Staszak59a9dab2011-07-16 20:23:20 +00002//
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
Jakub Staszakf55c1c82011-07-25 19:25:40 +000014#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/Analysis/BlockFrequencyImpl.h"
Jakub Staszak59a9dab2011-07-16 20:23:20 +000016#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/CodeGen/Passes.h"
18#include "llvm/InitializePasses.h"
Stephen Hines36b56882014-04-23 16:57:46 -070019#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/GraphWriter.h"
Jakub Staszak59a9dab2011-07-16 20:23:20 +000022
23using namespace llvm;
24
Stephen Hines36b56882014-04-23 16:57:46 -070025#ifndef NDEBUG
26enum GVDAGType {
27 GVDT_None,
28 GVDT_Fraction,
29 GVDT_Integer
30};
31
32static cl::opt<GVDAGType>
33ViewMachineBlockFreqPropagationDAG("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 propagate 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
46namespace llvm {
47
48template <>
49struct 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
55 const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
56 return G->getFunction()->begin();
57 }
58
59 static ChildIteratorType child_begin(const NodeType *N) {
60 return N->succ_begin();
61 }
62
63 static ChildIteratorType child_end(const NodeType *N) {
64 return N->succ_end();
65 }
66
67 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
68 return G->getFunction()->begin();
69 }
70
71 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
72 return G->getFunction()->end();
73 }
74};
75
76template<>
77struct DOTGraphTraits<MachineBlockFrequencyInfo*> :
78 public DefaultDOTGraphTraits {
79 explicit DOTGraphTraits(bool isSimple=false) :
80 DefaultDOTGraphTraits(isSimple) {}
81
82 static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
83 return G->getFunction()->getName();
84 }
85
86 std::string getNodeLabel(const MachineBasicBlock *Node,
87 const MachineBlockFrequencyInfo *Graph) {
88 std::string Result;
89 raw_string_ostream OS(Result);
90
91 OS << Node->getName().str() << ":";
92 switch (ViewMachineBlockFreqPropagationDAG) {
93 case GVDT_Fraction:
94 Graph->printBlockFreq(OS, Node);
95 break;
96 case GVDT_Integer:
97 OS << Graph->getBlockFreq(Node).getFrequency();
98 break;
99 case GVDT_None:
100 llvm_unreachable("If we are not supposed to render a graph we should "
101 "never reach this point.");
102 }
103
104 return Result;
105 }
106};
107
108
109} // end namespace llvm
110#endif
111
Jakub Staszakf55c1c82011-07-25 19:25:40 +0000112INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
Jakub Staszak59a9dab2011-07-16 20:23:20 +0000113 "Machine Block Frequency Analysis", true, true)
114INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Jakub Staszakf55c1c82011-07-25 19:25:40 +0000115INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
Jakub Staszak59a9dab2011-07-16 20:23:20 +0000116 "Machine Block Frequency Analysis", true, true)
117
Jakub Staszakf55c1c82011-07-25 19:25:40 +0000118char MachineBlockFrequencyInfo::ID = 0;
Jakub Staszak59a9dab2011-07-16 20:23:20 +0000119
120
Stephen Hines36b56882014-04-23 16:57:46 -0700121MachineBlockFrequencyInfo::
122MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
Jakub Staszakf55c1c82011-07-25 19:25:40 +0000123 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
Jakub Staszak59a9dab2011-07-16 20:23:20 +0000124}
125
Stephen Hines36b56882014-04-23 16:57:46 -0700126MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
Jakub Staszak59a9dab2011-07-16 20:23:20 +0000127
Jakub Staszakf55c1c82011-07-25 19:25:40 +0000128void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Jakub Staszak59a9dab2011-07-16 20:23:20 +0000129 AU.addRequired<MachineBranchProbabilityInfo>();
130 AU.setPreservesAll();
Jakub Staszak9d81c972011-07-21 22:59:09 +0000131 MachineFunctionPass::getAnalysisUsage(AU);
Jakub Staszak59a9dab2011-07-16 20:23:20 +0000132}
133
Jakub Staszakf55c1c82011-07-25 19:25:40 +0000134bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
Stephen Hines36b56882014-04-23 16:57:46 -0700135 MachineBranchProbabilityInfo &MBPI =
136 getAnalysis<MachineBranchProbabilityInfo>();
137 if (!MBFI)
138 MBFI.reset(new ImplType);
Jakub Staszak59a9dab2011-07-16 20:23:20 +0000139 MBFI->doFunction(&F, &MBPI);
Stephen Hines36b56882014-04-23 16:57:46 -0700140#ifndef NDEBUG
141 if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
142 view();
143 }
144#endif
Jakub Staszak59a9dab2011-07-16 20:23:20 +0000145 return false;
146}
147
Stephen Hines36b56882014-04-23 16:57:46 -0700148void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
149
150/// Pop up a ghostview window with the current block frequency propagation
151/// rendered using dot.
152void MachineBlockFrequencyInfo::view() const {
153// This code is only for debugging.
154#ifndef NDEBUG
155 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
156 "MachineBlockFrequencyDAGs");
157#else
158 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
159 "on systems with Graphviz or gv!\n";
160#endif // NDEBUG
161}
162
Jakub Staszak8ea45232011-08-03 21:30:57 +0000163BlockFrequency MachineBlockFrequencyInfo::
Jakub Staszak25101bb2011-12-20 20:03:10 +0000164getBlockFreq(const MachineBasicBlock *MBB) const {
Stephen Hines36b56882014-04-23 16:57:46 -0700165 return MBFI ? MBFI->getBlockFreq(MBB) : 0;
166}
167
168const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
169 return MBFI ? MBFI->Fn : nullptr;
170}
171
172raw_ostream &
173MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
174 const BlockFrequency Freq) const {
175 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
176}
177
178raw_ostream &
179MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
180 const MachineBasicBlock *MBB) const {
181 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
182}
183
184uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
185 return MBFI ? MBFI->getEntryFreq() : 0;
Jakub Staszak59a9dab2011-07-16 20:23:20 +0000186}