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 extern cl::opt<std::string> ViewBlockFreqFuncName; 48 extern cl::opt<unsigned> ViewHotFreqPercent; 49 50 namespace llvm { 51 52 template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 53 typedef const MachineBasicBlock *NodeRef; 54 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 55 typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator; 56 57 static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) { 58 return &G->getFunction()->front(); 59 } 60 61 static ChildIteratorType child_begin(const NodeRef N) { 62 return N->succ_begin(); 63 } 64 65 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); } 66 67 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 68 return nodes_iterator(G->getFunction()->begin()); 69 } 70 71 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 72 return nodes_iterator(G->getFunction()->end()); 73 } 74 }; 75 76 typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, 77 MachineBranchProbabilityInfo> 78 MBFIDOTGraphTraitsBase; 79 template <> 80 struct DOTGraphTraits<MachineBlockFrequencyInfo *> 81 : public MBFIDOTGraphTraitsBase { 82 explicit DOTGraphTraits(bool isSimple = false) 83 : MBFIDOTGraphTraitsBase(isSimple) {} 84 85 std::string getNodeLabel(const MachineBasicBlock *Node, 86 const MachineBlockFrequencyInfo *Graph) { 87 return MBFIDOTGraphTraitsBase::getNodeLabel( 88 Node, Graph, ViewMachineBlockFreqPropagationDAG); 89 } 90 91 std::string getNodeAttributes(const MachineBasicBlock *Node, 92 const MachineBlockFrequencyInfo *Graph) { 93 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, 94 ViewHotFreqPercent); 95 } 96 97 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, 98 const MachineBlockFrequencyInfo *MBFI) { 99 return MBFIDOTGraphTraitsBase::getEdgeAttributes( 100 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); 101 } 102 }; 103 104 } // end namespace llvm 105 #endif 106 107 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq", 108 "Machine Block Frequency Analysis", true, true) 109 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 110 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 111 INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq", 112 "Machine Block Frequency Analysis", true, true) 113 114 char MachineBlockFrequencyInfo::ID = 0; 115 116 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() 117 : MachineFunctionPass(ID) { 118 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 119 } 120 121 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {} 122 123 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 124 AU.addRequired<MachineBranchProbabilityInfo>(); 125 AU.addRequired<MachineLoopInfo>(); 126 AU.setPreservesAll(); 127 MachineFunctionPass::getAnalysisUsage(AU); 128 } 129 130 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 131 MachineBranchProbabilityInfo &MBPI = 132 getAnalysis<MachineBranchProbabilityInfo>(); 133 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 134 if (!MBFI) 135 MBFI.reset(new ImplType); 136 MBFI->calculate(F, MBPI, MLI); 137 #ifndef NDEBUG 138 if (ViewMachineBlockFreqPropagationDAG != GVDT_None && 139 (ViewBlockFreqFuncName.empty() || 140 F.getName().equals(ViewBlockFreqFuncName))) { 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 Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( 168 const MachineBasicBlock *MBB) const { 169 const Function *F = MBFI->getFunction()->getFunction(); 170 return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None; 171 } 172 173 Optional<uint64_t> 174 MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 175 const Function *F = MBFI->getFunction()->getFunction(); 176 return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None; 177 } 178 179 const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 180 return MBFI ? MBFI->getFunction() : nullptr; 181 } 182 183 const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { 184 return MBFI ? &MBFI->getBPI() : nullptr; 185 } 186 187 raw_ostream & 188 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 189 const BlockFrequency Freq) const { 190 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 191 } 192 193 raw_ostream & 194 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 195 const MachineBasicBlock *MBB) const { 196 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 197 } 198 199 uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 200 return MBFI ? MBFI->getEntryFreq() : 0; 201 } 202