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 static cl::opt<GVDAGType> ViewBlockFreqPropagationDAG( 31 "view-block-freq-propagation-dags", cl::Hidden, 32 cl::desc("Pop up a window to show a dag displaying how block " 33 "frequencies propagation through the CFG."), 34 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 35 clEnumValN(GVDT_Fraction, "fraction", 36 "display a graph using the " 37 "fractional block frequency representation."), 38 clEnumValN(GVDT_Integer, "integer", 39 "display a graph using the raw " 40 "integer fractional block frequency representation."), 41 clEnumValN(GVDT_Count, "count", "display a graph using the real " 42 "profile count if available."), 43 clEnumValEnd)); 44 45 cl::opt<std::string> 46 ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden, 47 cl::desc("The option to specify " 48 "the name of the function " 49 "whose CFG will be displayed.")); 50 51 cl::opt<unsigned> 52 ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden, 53 cl::desc("An integer in percent used to specify " 54 "the hot blocks/edges to be displayed " 55 "in red: a block or edge whose frequency " 56 "is no less than the max frequency of the " 57 "function multiplied by this percent.")); 58 59 namespace llvm { 60 61 template <> 62 struct GraphTraits<BlockFrequencyInfo *> { 63 typedef const BasicBlock NodeType; 64 typedef succ_const_iterator ChildIteratorType; 65 typedef Function::const_iterator nodes_iterator; 66 67 static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) { 68 return &G->getFunction()->front(); 69 } 70 static ChildIteratorType child_begin(const NodeType *N) { 71 return succ_begin(N); 72 } 73 static ChildIteratorType child_end(const NodeType *N) { 74 return succ_end(N); 75 } 76 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) { 77 return G->getFunction()->begin(); 78 } 79 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) { 80 return G->getFunction()->end(); 81 } 82 }; 83 84 typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo> 85 BFIDOTGTraitsBase; 86 87 template <> 88 struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase { 89 explicit DOTGraphTraits(bool isSimple = false) 90 : BFIDOTGTraitsBase(isSimple) {} 91 92 std::string getNodeLabel(const BasicBlock *Node, 93 const BlockFrequencyInfo *Graph) { 94 95 return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, 96 ViewBlockFreqPropagationDAG); 97 } 98 99 std::string getNodeAttributes(const BasicBlock *Node, 100 const BlockFrequencyInfo *Graph) { 101 return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph, 102 ViewHotFreqPercent); 103 } 104 105 std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI, 106 const BlockFrequencyInfo *BFI) { 107 return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(), 108 ViewHotFreqPercent); 109 } 110 }; 111 112 } // end namespace llvm 113 #endif 114 115 BlockFrequencyInfo::BlockFrequencyInfo() {} 116 117 BlockFrequencyInfo::BlockFrequencyInfo(const Function &F, 118 const BranchProbabilityInfo &BPI, 119 const LoopInfo &LI) { 120 calculate(F, BPI, LI); 121 } 122 123 BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg) 124 : BFI(std::move(Arg.BFI)) {} 125 126 BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) { 127 releaseMemory(); 128 BFI = std::move(RHS.BFI); 129 return *this; 130 } 131 132 void BlockFrequencyInfo::calculate(const Function &F, 133 const BranchProbabilityInfo &BPI, 134 const LoopInfo &LI) { 135 if (!BFI) 136 BFI.reset(new ImplType); 137 BFI->calculate(F, BPI, LI); 138 #ifndef NDEBUG 139 if (ViewBlockFreqPropagationDAG != GVDT_None && 140 (ViewBlockFreqFuncName.empty() || 141 F.getName().equals(ViewBlockFreqFuncName))) { 142 view(); 143 } 144 #endif 145 } 146 147 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const { 148 return BFI ? BFI->getBlockFreq(BB) : 0; 149 } 150 151 Optional<uint64_t> 152 BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const { 153 if (!BFI) 154 return None; 155 156 return BFI->getBlockProfileCount(*getFunction(), BB); 157 } 158 159 void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) { 160 assert(BFI && "Expected analysis to be available"); 161 BFI->setBlockFreq(BB, Freq); 162 } 163 164 /// Pop up a ghostview window with the current block frequency propagation 165 /// rendered using dot. 166 void BlockFrequencyInfo::view() const { 167 // This code is only for debugging. 168 #ifndef NDEBUG 169 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs"); 170 #else 171 errs() << "BlockFrequencyInfo::view is only available in debug builds on " 172 "systems with Graphviz or gv!\n"; 173 #endif // NDEBUG 174 } 175 176 const Function *BlockFrequencyInfo::getFunction() const { 177 return BFI ? BFI->getFunction() : nullptr; 178 } 179 180 const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const { 181 return BFI ? &BFI->getBPI() : nullptr; 182 } 183 184 raw_ostream &BlockFrequencyInfo:: 185 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const { 186 return BFI ? BFI->printBlockFreq(OS, Freq) : OS; 187 } 188 189 raw_ostream & 190 BlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 191 const BasicBlock *BB) const { 192 return BFI ? BFI->printBlockFreq(OS, BB) : OS; 193 } 194 195 uint64_t BlockFrequencyInfo::getEntryFreq() const { 196 return BFI ? BFI->getEntryFreq() : 0; 197 } 198 199 void BlockFrequencyInfo::releaseMemory() { BFI.reset(); } 200 201 void BlockFrequencyInfo::print(raw_ostream &OS) const { 202 if (BFI) 203 BFI->print(OS); 204 } 205 206 207 INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq", 208 "Block Frequency Analysis", true, true) 209 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass) 210 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 211 INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq", 212 "Block Frequency Analysis", true, true) 213 214 char BlockFrequencyInfoWrapperPass::ID = 0; 215 216 217 BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass() 218 : FunctionPass(ID) { 219 initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 220 } 221 222 BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {} 223 224 void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS, 225 const Module *) const { 226 BFI.print(OS); 227 } 228 229 void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 230 AU.addRequired<BranchProbabilityInfoWrapperPass>(); 231 AU.addRequired<LoopInfoWrapperPass>(); 232 AU.setPreservesAll(); 233 } 234 235 void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); } 236 237 bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) { 238 BranchProbabilityInfo &BPI = 239 getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI(); 240 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 241 BFI.calculate(F, BPI, LI); 242 return false; 243 } 244 245 char BlockFrequencyAnalysis::PassID; 246 BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F, 247 AnalysisManager<Function> &AM) { 248 BlockFrequencyInfo BFI; 249 BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F), 250 AM.getResult<LoopAnalysis>(F)); 251 return BFI; 252 } 253 254 PreservedAnalyses 255 BlockFrequencyPrinterPass::run(Function &F, AnalysisManager<Function> &AM) { 256 OS << "Printing analysis results of BFI for function " 257 << "'" << F.getName() << "':" 258 << "\n"; 259 AM.getResult<BlockFrequencyAnalysis>(F).print(OS); 260 return PreservedAnalyses::all(); 261 } 262