1 //===- BlockFrequencyInfo.cpp - Block 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/Analysis/BlockFrequencyInfo.h" 15 #include "llvm/Analysis/BlockFrequencyInfoImpl.h" 16 #include "llvm/Analysis/BranchProbabilityInfo.h" 17 #include "llvm/Analysis/LoopInfo.h" 18 #include "llvm/Analysis/Passes.h" 19 #include "llvm/IR/CFG.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 { 31 GVDT_None, 32 GVDT_Fraction, 33 GVDT_Integer 34 }; 35 36 static cl::opt<GVDAGType> 37 ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden, 38 cl::desc("Pop up a window to show a dag displaying how block " 39 "frequencies propagation 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<BlockFrequencyInfo *> { 53 typedef const BasicBlock NodeType; 54 typedef succ_const_iterator ChildIteratorType; 55 typedef Function::const_iterator nodes_iterator; 56 57 static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) { 58 return G->getFunction()->begin(); 59 } 60 static ChildIteratorType child_begin(const NodeType *N) { 61 return succ_begin(N); 62 } 63 static ChildIteratorType child_end(const NodeType *N) { 64 return succ_end(N); 65 } 66 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) { 67 return G->getFunction()->begin(); 68 } 69 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) { 70 return G->getFunction()->end(); 71 } 72 }; 73 74 template<> 75 struct DOTGraphTraits<BlockFrequencyInfo*> : public DefaultDOTGraphTraits { 76 explicit DOTGraphTraits(bool isSimple=false) : 77 DefaultDOTGraphTraits(isSimple) {} 78 79 static std::string getGraphName(const BlockFrequencyInfo *G) { 80 return G->getFunction()->getName(); 81 } 82 83 std::string getNodeLabel(const BasicBlock *Node, 84 const BlockFrequencyInfo *Graph) { 85 string_ostream OS; 86 87 OS << Node->getName() << ":"; 88 switch (ViewBlockFreqPropagationDAG) { 89 case GVDT_Fraction: 90 Graph->printBlockFreq(OS, Node); 91 break; 92 case GVDT_Integer: 93 OS << Graph->getBlockFreq(Node).getFrequency(); 94 break; 95 case GVDT_None: 96 llvm_unreachable("If we are not supposed to render a graph we should " 97 "never reach this point."); 98 } 99 100 return OS.str(); 101 } 102 }; 103 104 } // end namespace llvm 105 #endif 106 107 INITIALIZE_PASS_BEGIN(BlockFrequencyInfo, "block-freq", 108 "Block Frequency Analysis", true, true) 109 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfo) 110 INITIALIZE_PASS_DEPENDENCY(LoopInfo) 111 INITIALIZE_PASS_END(BlockFrequencyInfo, "block-freq", 112 "Block Frequency Analysis", true, true) 113 114 char BlockFrequencyInfo::ID = 0; 115 116 117 BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) { 118 initializeBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 119 } 120 121 BlockFrequencyInfo::~BlockFrequencyInfo() {} 122 123 void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 124 AU.addRequired<BranchProbabilityInfo>(); 125 AU.addRequired<LoopInfo>(); 126 AU.setPreservesAll(); 127 } 128 129 bool BlockFrequencyInfo::runOnFunction(Function &F) { 130 BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>(); 131 LoopInfo &LI = getAnalysis<LoopInfo>(); 132 if (!BFI) 133 BFI.reset(new ImplType); 134 BFI->doFunction(&F, &BPI, &LI); 135 #ifndef NDEBUG 136 if (ViewBlockFreqPropagationDAG != GVDT_None) 137 view(); 138 #endif 139 return false; 140 } 141 142 void BlockFrequencyInfo::releaseMemory() { BFI.reset(); } 143 144 void BlockFrequencyInfo::print(raw_ostream &O, const Module *) const { 145 if (BFI) BFI->print(O); 146 } 147 148 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const { 149 return BFI ? BFI->getBlockFreq(BB) : 0; 150 } 151 152 /// Pop up a ghostview window with the current block frequency propagation 153 /// rendered using dot. 154 void BlockFrequencyInfo::view() const { 155 // This code is only for debugging. 156 #ifndef NDEBUG 157 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs"); 158 #else 159 errs() << "BlockFrequencyInfo::view is only available in debug builds on " 160 "systems with Graphviz or gv!\n"; 161 #endif // NDEBUG 162 } 163 164 const Function *BlockFrequencyInfo::getFunction() const { 165 return BFI ? BFI->getFunction() : nullptr; 166 } 167 168 raw_ostream &BlockFrequencyInfo:: 169 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const { 170 return BFI ? BFI->printBlockFreq(OS, Freq) : OS; 171 } 172 173 raw_ostream & 174 BlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 175 const BasicBlock *BB) const { 176 return BFI ? BFI->printBlockFreq(OS, BB) : OS; 177 } 178 179 uint64_t BlockFrequencyInfo::getEntryFreq() const { 180 return BFI ? BFI->getEntryFreq() : 0; 181 } 182