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