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 *NodeRef; 64 typedef succ_const_iterator ChildIteratorType; 65 typedef pointer_iterator<Function::const_iterator> nodes_iterator; 66 67 static NodeRef getEntryNode(const BlockFrequencyInfo *G) { 68 return &G->getFunction()->front(); 69 } 70 static ChildIteratorType child_begin(const NodeRef N) { 71 return succ_begin(N); 72 } 73 static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); } 74 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) { 75 return nodes_iterator(G->getFunction()->begin()); 76 } 77 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) { 78 return nodes_iterator(G->getFunction()->end()); 79 } 80 }; 81 82 typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo> 83 BFIDOTGTraitsBase; 84 85 template <> 86 struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase { 87 explicit DOTGraphTraits(bool isSimple = false) 88 : BFIDOTGTraitsBase(isSimple) {} 89 90 std::string getNodeLabel(const BasicBlock *Node, 91 const BlockFrequencyInfo *Graph) { 92 93 return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, 94 ViewBlockFreqPropagationDAG); 95 } 96 97 std::string getNodeAttributes(const BasicBlock *Node, 98 const BlockFrequencyInfo *Graph) { 99 return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph, 100 ViewHotFreqPercent); 101 } 102 103 std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI, 104 const BlockFrequencyInfo *BFI) { 105 return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(), 106 ViewHotFreqPercent); 107 } 108 }; 109 110 } // end namespace llvm 111 #endif 112 113 BlockFrequencyInfo::BlockFrequencyInfo() {} 114 115 BlockFrequencyInfo::BlockFrequencyInfo(const Function &F, 116 const BranchProbabilityInfo &BPI, 117 const LoopInfo &LI) { 118 calculate(F, BPI, LI); 119 } 120 121 BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg) 122 : BFI(std::move(Arg.BFI)) {} 123 124 BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) { 125 releaseMemory(); 126 BFI = std::move(RHS.BFI); 127 return *this; 128 } 129 130 // Explicitly define the default constructor otherwise it would be implicitly 131 // defined at the first ODR-use which is the BFI member in the 132 // LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl 133 // template instantiated which is not available in the header. 134 BlockFrequencyInfo::~BlockFrequencyInfo() {} 135 136 void BlockFrequencyInfo::calculate(const Function &F, 137 const BranchProbabilityInfo &BPI, 138 const LoopInfo &LI) { 139 if (!BFI) 140 BFI.reset(new ImplType); 141 BFI->calculate(F, BPI, LI); 142 #ifndef NDEBUG 143 if (ViewBlockFreqPropagationDAG != GVDT_None && 144 (ViewBlockFreqFuncName.empty() || 145 F.getName().equals(ViewBlockFreqFuncName))) { 146 view(); 147 } 148 #endif 149 } 150 151 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const { 152 return BFI ? BFI->getBlockFreq(BB) : 0; 153 } 154 155 Optional<uint64_t> 156 BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const { 157 if (!BFI) 158 return None; 159 160 return BFI->getBlockProfileCount(*getFunction(), BB); 161 } 162 163 Optional<uint64_t> 164 BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 165 if (!BFI) 166 return None; 167 return BFI->getProfileCountFromFreq(*getFunction(), Freq); 168 } 169 170 void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) { 171 assert(BFI && "Expected analysis to be available"); 172 BFI->setBlockFreq(BB, Freq); 173 } 174 175 /// Pop up a ghostview window with the current block frequency propagation 176 /// rendered using dot. 177 void BlockFrequencyInfo::view() const { 178 // This code is only for debugging. 179 #ifndef NDEBUG 180 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs"); 181 #else 182 errs() << "BlockFrequencyInfo::view is only available in debug builds on " 183 "systems with Graphviz or gv!\n"; 184 #endif // NDEBUG 185 } 186 187 const Function *BlockFrequencyInfo::getFunction() const { 188 return BFI ? BFI->getFunction() : nullptr; 189 } 190 191 const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const { 192 return BFI ? &BFI->getBPI() : nullptr; 193 } 194 195 raw_ostream &BlockFrequencyInfo:: 196 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const { 197 return BFI ? BFI->printBlockFreq(OS, Freq) : OS; 198 } 199 200 raw_ostream & 201 BlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 202 const BasicBlock *BB) const { 203 return BFI ? BFI->printBlockFreq(OS, BB) : OS; 204 } 205 206 uint64_t BlockFrequencyInfo::getEntryFreq() const { 207 return BFI ? BFI->getEntryFreq() : 0; 208 } 209 210 void BlockFrequencyInfo::releaseMemory() { BFI.reset(); } 211 212 void BlockFrequencyInfo::print(raw_ostream &OS) const { 213 if (BFI) 214 BFI->print(OS); 215 } 216 217 218 INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq", 219 "Block Frequency Analysis", true, true) 220 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass) 221 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 222 INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq", 223 "Block Frequency Analysis", true, true) 224 225 char BlockFrequencyInfoWrapperPass::ID = 0; 226 227 228 BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass() 229 : FunctionPass(ID) { 230 initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 231 } 232 233 BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {} 234 235 void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS, 236 const Module *) const { 237 BFI.print(OS); 238 } 239 240 void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 241 AU.addRequired<BranchProbabilityInfoWrapperPass>(); 242 AU.addRequired<LoopInfoWrapperPass>(); 243 AU.setPreservesAll(); 244 } 245 246 void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); } 247 248 bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) { 249 BranchProbabilityInfo &BPI = 250 getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI(); 251 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 252 BFI.calculate(F, BPI, LI); 253 return false; 254 } 255 256 char BlockFrequencyAnalysis::PassID; 257 BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F, 258 FunctionAnalysisManager &AM) { 259 BlockFrequencyInfo BFI; 260 BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F), 261 AM.getResult<LoopAnalysis>(F)); 262 return BFI; 263 } 264 265 PreservedAnalyses 266 BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { 267 OS << "Printing analysis results of BFI for function " 268 << "'" << F.getName() << "':" 269 << "\n"; 270 AM.getResult<BlockFrequencyAnalysis>(F).print(OS); 271 return PreservedAnalyses::all(); 272 } 273