1 //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===// 2 // 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 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" 25 26 using namespace llvm; 27 28 #ifndef NDEBUG 29 enum GVDAGType { 30 GVDT_None, 31 GVDT_Fraction, 32 GVDT_Integer 33 }; 34 35 static cl::opt<GVDAGType> 36 ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags", 37 cl::Hidden, 38 cl::desc("Pop up a window to show a dag displaying how machine block " 39 "frequencies propagate through the CFG."), 40 cl::values( 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."), 47 clEnumValEnd)); 48 49 namespace llvm { 50 51 template <> 52 struct GraphTraits<MachineBlockFrequencyInfo *> { 53 typedef const MachineBasicBlock NodeType; 54 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 55 typedef MachineFunction::const_iterator nodes_iterator; 56 57 static inline 58 const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) { 59 return G->getFunction()->begin(); 60 } 61 62 static ChildIteratorType child_begin(const NodeType *N) { 63 return N->succ_begin(); 64 } 65 66 static ChildIteratorType child_end(const NodeType *N) { 67 return N->succ_end(); 68 } 69 70 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 71 return G->getFunction()->begin(); 72 } 73 74 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 75 return G->getFunction()->end(); 76 } 77 }; 78 79 template<> 80 struct DOTGraphTraits<MachineBlockFrequencyInfo*> : 81 public DefaultDOTGraphTraits { 82 explicit DOTGraphTraits(bool isSimple=false) : 83 DefaultDOTGraphTraits(isSimple) {} 84 85 static std::string getGraphName(const MachineBlockFrequencyInfo *G) { 86 return G->getFunction()->getName(); 87 } 88 89 std::string getNodeLabel(const MachineBasicBlock *Node, 90 const MachineBlockFrequencyInfo *Graph) { 91 std::string Result; 92 raw_string_ostream OS(Result); 93 94 OS << Node->getName().str() << ":"; 95 switch (ViewMachineBlockFreqPropagationDAG) { 96 case GVDT_Fraction: 97 Graph->printBlockFreq(OS, Node); 98 break; 99 case GVDT_Integer: 100 OS << Graph->getBlockFreq(Node).getFrequency(); 101 break; 102 case GVDT_None: 103 llvm_unreachable("If we are not supposed to render a graph we should " 104 "never reach this point."); 105 } 106 107 return Result; 108 } 109 }; 110 111 112 } // end namespace llvm 113 #endif 114 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) 121 122 char MachineBlockFrequencyInfo::ID = 0; 123 124 125 MachineBlockFrequencyInfo:: 126 MachineBlockFrequencyInfo() :MachineFunctionPass(ID) { 127 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 128 } 129 130 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {} 131 132 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 133 AU.addRequired<MachineBranchProbabilityInfo>(); 134 AU.addRequired<MachineLoopInfo>(); 135 AU.setPreservesAll(); 136 MachineFunctionPass::getAnalysisUsage(AU); 137 } 138 139 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 140 MachineBranchProbabilityInfo &MBPI = 141 getAnalysis<MachineBranchProbabilityInfo>(); 142 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 143 if (!MBFI) 144 MBFI.reset(new ImplType); 145 MBFI->doFunction(&F, &MBPI, &MLI); 146 #ifndef NDEBUG 147 if (ViewMachineBlockFreqPropagationDAG != GVDT_None) { 148 view(); 149 } 150 #endif 151 return false; 152 } 153 154 void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 155 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. 160 #ifndef NDEBUG 161 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), 162 "MachineBlockFrequencyDAGs"); 163 #else 164 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds " 165 "on systems with Graphviz or gv!\n"; 166 #endif // NDEBUG 167 } 168 169 BlockFrequency MachineBlockFrequencyInfo:: 170 getBlockFreq(const MachineBasicBlock *MBB) const { 171 return MBFI ? MBFI->getBlockFreq(MBB) : 0; 172 } 173 174 const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 175 return MBFI ? MBFI->getFunction() : nullptr; 176 } 177 178 raw_ostream & 179 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 180 const BlockFrequency Freq) const { 181 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 182 } 183 184 raw_ostream & 185 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 186 const MachineBasicBlock *MBB) const { 187 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 188 } 189 190 uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 191 return MBFI ? MBFI->getEntryFreq() : 0; 192 } 193