1 //====----- MachineBlockFrequencyInfo.cpp - Machine Block 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 propgate 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;
54 static inline const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
55 return G->getFunction()->begin();
57 static ChildIteratorType child_begin(const NodeType *N) {
58 return N->succ_begin();
60 static ChildIteratorType child_end(const NodeType *N) {
63 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
64 return G->getFunction()->begin();
66 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
67 return G->getFunction()->end();
72 struct DOTGraphTraits<MachineBlockFrequencyInfo*> : public DefaultDOTGraphTraits {
73 explicit DOTGraphTraits(bool isSimple=false) :
74 DefaultDOTGraphTraits(isSimple) {}
76 static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
77 return G->getFunction()->getName();
80 std::string getNodeLabel(const MachineBasicBlock *Node,
81 const MachineBlockFrequencyInfo *Graph) {
83 raw_string_ostream OS(Result);
85 OS << Node->getName().str() << ":";
86 switch (ViewMachineBlockFreqPropagationDAG) {
88 Graph->getBlockFreq(Node).print(OS);
91 OS << Graph->getBlockFreq(Node).getFrequency();
94 llvm_unreachable("If we are not supposed to render a graph we should "
95 "never reach this point.");
103 } // end namespace llvm
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)
112 char MachineBlockFrequencyInfo::ID = 0;
115 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() : MachineFunctionPass(ID) {
116 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
117 MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction,
118 MachineBranchProbabilityInfo>();
121 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {
125 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
126 AU.addRequired<MachineBranchProbabilityInfo>();
127 AU.setPreservesAll();
128 MachineFunctionPass::getAnalysisUsage(AU);
131 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
132 MachineBranchProbabilityInfo &MBPI = getAnalysis<MachineBranchProbabilityInfo>();
133 MBFI->doFunction(&F, &MBPI);
135 if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
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.
147 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
148 "MachineBlockFrequencyDAGs");
150 errs() << "BlockFrequencyInfo::view is only available in debug builds on "
151 "systems with Graphviz or gv!\n";
155 BlockFrequency MachineBlockFrequencyInfo::
156 getBlockFreq(const MachineBasicBlock *MBB) const {
157 return MBFI->getBlockFreq(MBB);
160 MachineFunction *MachineBlockFrequencyInfo::getFunction() const {