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/Format.h" 24 #include "llvm/Support/GraphWriter.h" 25 #include "llvm/Support/raw_ostream.h" 26 27 using namespace llvm; 28 29 #define DEBUG_TYPE "block-freq" 30 31 #ifndef NDEBUG 32 33 static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG( 34 "view-machine-block-freq-propagation-dags", cl::Hidden, 35 cl::desc("Pop up a window to show a dag displaying how machine block " 36 "frequencies propagate through the CFG."), 37 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 38 clEnumValN(GVDT_Fraction, "fraction", 39 "display a graph using the " 40 "fractional block frequency representation."), 41 clEnumValN(GVDT_Integer, "integer", 42 "display a graph using the raw " 43 "integer fractional block frequency representation."), 44 clEnumValN(GVDT_Count, "count", "display a graph using the real " 45 "profile count if available."), 46 47 clEnumValEnd)); 48 49 extern cl::opt<std::string> ViewBlockFreqFuncName; 50 extern cl::opt<unsigned> ViewHotFreqPercent; 51 52 namespace llvm { 53 54 template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 55 typedef const MachineBasicBlock *NodeRef; 56 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 57 typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator; 58 59 static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) { 60 return &G->getFunction()->front(); 61 } 62 63 static ChildIteratorType child_begin(const NodeRef N) { 64 return N->succ_begin(); 65 } 66 67 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); } 68 69 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 70 return nodes_iterator(G->getFunction()->begin()); 71 } 72 73 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 74 return nodes_iterator(G->getFunction()->end()); 75 } 76 }; 77 78 typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, 79 MachineBranchProbabilityInfo> 80 MBFIDOTGraphTraitsBase; 81 template <> 82 struct DOTGraphTraits<MachineBlockFrequencyInfo *> 83 : public MBFIDOTGraphTraitsBase { 84 explicit DOTGraphTraits(bool isSimple = false) 85 : MBFIDOTGraphTraitsBase(isSimple) {} 86 87 std::string getNodeLabel(const MachineBasicBlock *Node, 88 const MachineBlockFrequencyInfo *Graph) { 89 return MBFIDOTGraphTraitsBase::getNodeLabel( 90 Node, Graph, ViewMachineBlockFreqPropagationDAG); 91 } 92 93 std::string getNodeAttributes(const MachineBasicBlock *Node, 94 const MachineBlockFrequencyInfo *Graph) { 95 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, 96 ViewHotFreqPercent); 97 } 98 99 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, 100 const MachineBlockFrequencyInfo *MBFI) { 101 return MBFIDOTGraphTraitsBase::getEdgeAttributes( 102 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); 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 (ViewBlockFreqFuncName.empty() || 142 F.getName().equals(ViewBlockFreqFuncName))) { 143 view(); 144 } 145 #endif 146 return false; 147 } 148 149 void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 150 151 /// Pop up a ghostview window with the current block frequency propagation 152 /// rendered using dot. 153 void MachineBlockFrequencyInfo::view() const { 154 // This code is only for debugging. 155 #ifndef NDEBUG 156 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), 157 "MachineBlockFrequencyDAGs"); 158 #else 159 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds " 160 "on systems with Graphviz or gv!\n"; 161 #endif // NDEBUG 162 } 163 164 BlockFrequency 165 MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { 166 return MBFI ? MBFI->getBlockFreq(MBB) : 0; 167 } 168 169 Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( 170 const MachineBasicBlock *MBB) const { 171 const Function *F = MBFI->getFunction()->getFunction(); 172 return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None; 173 } 174 175 Optional<uint64_t> 176 MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 177 const Function *F = MBFI->getFunction()->getFunction(); 178 return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None; 179 } 180 181 const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 182 return MBFI ? MBFI->getFunction() : nullptr; 183 } 184 185 const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { 186 return MBFI ? &MBFI->getBPI() : nullptr; 187 } 188 189 raw_ostream & 190 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 191 const BlockFrequency Freq) const { 192 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 193 } 194 195 raw_ostream & 196 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 197 const MachineBasicBlock *MBB) const { 198 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 199 } 200 201 uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 202 return MBFI ? MBFI->getEntryFreq() : 0; 203 } 204