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 #define DEBUG_TYPE "block-freq"
15 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
16 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
17 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineLoopInfo.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/InitializePasses.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/GraphWriter.h"
35 static cl::opt<GVDAGType>
36 ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
38 cl::desc("Pop up a window to show a dag displaying how machine block "
39 "frequencies propagate through the CFG."),
41 clEnumValN(GVDT_None, "none",
42 "do not display graphs."),
43 clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
44 "fractional block frequency representation."),
45 clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
46 "integer fractional block frequency representation."),
52 struct GraphTraits<MachineBlockFrequencyInfo *> {
53 typedef const MachineBasicBlock NodeType;
54 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
55 typedef MachineFunction::const_iterator nodes_iterator;
58 const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
59 return G->getFunction()->begin();
62 static ChildIteratorType child_begin(const NodeType *N) {
63 return N->succ_begin();
66 static ChildIteratorType child_end(const NodeType *N) {
70 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
71 return G->getFunction()->begin();
74 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
75 return G->getFunction()->end();
80 struct DOTGraphTraits<MachineBlockFrequencyInfo*> :
81 public DefaultDOTGraphTraits {
82 explicit DOTGraphTraits(bool isSimple=false) :
83 DefaultDOTGraphTraits(isSimple) {}
85 static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
86 return G->getFunction()->getName();
89 std::string getNodeLabel(const MachineBasicBlock *Node,
90 const MachineBlockFrequencyInfo *Graph) {
92 raw_string_ostream OS(Result);
94 OS << Node->getName().str() << ":";
95 switch (ViewMachineBlockFreqPropagationDAG) {
97 Graph->printBlockFreq(OS, Node);
100 OS << Graph->getBlockFreq(Node).getFrequency();
103 llvm_unreachable("If we are not supposed to render a graph we should "
104 "never reach this point.");
112 } // end namespace llvm
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)
122 char MachineBlockFrequencyInfo::ID = 0;
125 MachineBlockFrequencyInfo::
126 MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
127 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
130 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
132 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
133 AU.addRequired<MachineBranchProbabilityInfo>();
134 AU.addRequired<MachineLoopInfo>();
135 AU.setPreservesAll();
136 MachineFunctionPass::getAnalysisUsage(AU);
139 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
140 MachineBranchProbabilityInfo &MBPI =
141 getAnalysis<MachineBranchProbabilityInfo>();
142 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
144 MBFI.reset(new ImplType);
145 MBFI->doFunction(&F, &MBPI, &MLI);
147 if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
154 void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
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.
161 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
162 "MachineBlockFrequencyDAGs");
164 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
165 "on systems with Graphviz or gv!\n";
169 BlockFrequency MachineBlockFrequencyInfo::
170 getBlockFreq(const MachineBasicBlock *MBB) const {
171 return MBFI ? MBFI->getBlockFreq(MBB) : 0;
174 const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
175 return MBFI ? MBFI->getFunction() : nullptr;
179 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
180 const BlockFrequency Freq) const {
181 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
185 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
186 const MachineBasicBlock *MBB) const {
187 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
190 uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
191 return MBFI ? MBFI->getEntryFreq() : 0;