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/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"
32 static cl::opt<GVDAGType>
33 ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
35 cl::desc("Pop up a window to show a dag displaying how machine block "
36 "frequencies propagate through the CFG."),
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."),
49 struct GraphTraits<MachineBlockFrequencyInfo *> {
50 typedef const MachineBasicBlock NodeType;
51 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
52 typedef MachineFunction::const_iterator nodes_iterator;
55 const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
56 return G->getFunction()->begin();
59 static ChildIteratorType child_begin(const NodeType *N) {
60 return N->succ_begin();
63 static ChildIteratorType child_end(const NodeType *N) {
67 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
68 return G->getFunction()->begin();
71 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
72 return G->getFunction()->end();
77 struct DOTGraphTraits<MachineBlockFrequencyInfo*> :
78 public DefaultDOTGraphTraits {
79 explicit DOTGraphTraits(bool isSimple=false) :
80 DefaultDOTGraphTraits(isSimple) {}
82 static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
83 return G->getFunction()->getName();
86 std::string getNodeLabel(const MachineBasicBlock *Node,
87 const MachineBlockFrequencyInfo *Graph) {
89 raw_string_ostream OS(Result);
91 OS << Node->getName().str() << ":";
92 switch (ViewMachineBlockFreqPropagationDAG) {
94 Graph->getBlockFreq(Node).print(OS);
97 OS << Graph->getBlockFreq(Node).getFrequency();
100 llvm_unreachable("If we are not supposed to render a graph we should "
101 "never reach this point.");
109 } // end namespace llvm
112 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
113 "Machine Block Frequency Analysis", true, true)
114 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
115 INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
116 "Machine Block Frequency Analysis", true, true)
118 char MachineBlockFrequencyInfo::ID = 0;
121 MachineBlockFrequencyInfo::
122 MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
123 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
124 MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction,
125 MachineBranchProbabilityInfo>();
128 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {
132 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
133 AU.addRequired<MachineBranchProbabilityInfo>();
134 AU.setPreservesAll();
135 MachineFunctionPass::getAnalysisUsage(AU);
138 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
139 MachineBranchProbabilityInfo &MBPI =
140 getAnalysis<MachineBranchProbabilityInfo>();
141 MBFI->doFunction(&F, &MBPI);
143 if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
150 /// Pop up a ghostview window with the current block frequency propagation
151 /// rendered using dot.
152 void MachineBlockFrequencyInfo::view() const {
153 // This code is only for debugging.
155 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
156 "MachineBlockFrequencyDAGs");
158 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
159 "on systems with Graphviz or gv!\n";
163 BlockFrequency MachineBlockFrequencyInfo::
164 getBlockFreq(const MachineBasicBlock *MBB) const {
165 return MBFI->getBlockFreq(MBB);
168 MachineFunction *MachineBlockFrequencyInfo::getFunction() const {