1 //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Loops should be simplified before this analysis.
12 //===----------------------------------------------------------------------===//
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"
27 #define DEBUG_TYPE "block-freq"
36 static cl::opt<GVDAGType>
37 ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
39 cl::desc("Pop up a window to show a dag displaying how machine block "
40 "frequencies propagate through the CFG."),
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."),
53 struct GraphTraits<MachineBlockFrequencyInfo *> {
54 typedef const MachineBasicBlock NodeType;
55 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
56 typedef MachineFunction::const_iterator nodes_iterator;
59 const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
60 return G->getFunction()->begin();
63 static ChildIteratorType child_begin(const NodeType *N) {
64 return N->succ_begin();
67 static ChildIteratorType child_end(const NodeType *N) {
71 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
72 return G->getFunction()->begin();
75 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
76 return G->getFunction()->end();
81 struct DOTGraphTraits<MachineBlockFrequencyInfo*> :
82 public DefaultDOTGraphTraits {
83 explicit DOTGraphTraits(bool isSimple=false) :
84 DefaultDOTGraphTraits(isSimple) {}
86 static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
87 return G->getFunction()->getName();
90 std::string getNodeLabel(const MachineBasicBlock *Node,
91 const MachineBlockFrequencyInfo *Graph) {
93 raw_string_ostream OS(Result);
95 OS << Node->getName().str() << ":";
96 switch (ViewMachineBlockFreqPropagationDAG) {
98 Graph->printBlockFreq(OS, Node);
101 OS << Graph->getBlockFreq(Node).getFrequency();
104 llvm_unreachable("If we are not supposed to render a graph we should "
105 "never reach this point.");
113 } // end namespace llvm
116 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
117 "Machine Block Frequency Analysis", true, true)
118 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
119 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
120 INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
121 "Machine Block Frequency Analysis", true, true)
123 char MachineBlockFrequencyInfo::ID = 0;
126 MachineBlockFrequencyInfo::
127 MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
128 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
131 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
133 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
134 AU.addRequired<MachineBranchProbabilityInfo>();
135 AU.addRequired<MachineLoopInfo>();
136 AU.setPreservesAll();
137 MachineFunctionPass::getAnalysisUsage(AU);
140 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
141 MachineBranchProbabilityInfo &MBPI =
142 getAnalysis<MachineBranchProbabilityInfo>();
143 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
145 MBFI.reset(new ImplType);
146 MBFI->doFunction(&F, &MBPI, &MLI);
148 if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
155 void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
157 /// Pop up a ghostview window with the current block frequency propagation
158 /// rendered using dot.
159 void MachineBlockFrequencyInfo::view() const {
160 // This code is only for debugging.
162 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
163 "MachineBlockFrequencyDAGs");
165 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
166 "on systems with Graphviz or gv!\n";
170 BlockFrequency MachineBlockFrequencyInfo::
171 getBlockFreq(const MachineBasicBlock *MBB) const {
172 return MBFI ? MBFI->getBlockFreq(MBB) : 0;
175 const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
176 return MBFI ? MBFI->getFunction() : nullptr;
180 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
181 const BlockFrequency Freq) const {
182 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
186 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
187 const MachineBasicBlock *MBB) const {
188 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
191 uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
192 return MBFI ? MBFI->getEntryFreq() : 0;