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 // Similar option above, but used to control BFI display only after MBP pass 47 cl::opt<GVDAGType> ViewBlockLayoutWithBFI( 48 "view-block-layout-with-bfi", cl::Hidden, 49 cl::desc( 50 "Pop up a window to show a dag displaying MBP layout and associated " 51 "block frequencies of the CFG."), 52 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 53 clEnumValN(GVDT_Fraction, "fraction", 54 "display a graph using the " 55 "fractional block frequency representation."), 56 clEnumValN(GVDT_Integer, "integer", 57 "display a graph using the raw " 58 "integer fractional block frequency representation."), 59 clEnumValN(GVDT_Count, "count", 60 "display a graph using the real " 61 "profile count if available."))); 62 63 extern cl::opt<std::string> ViewBlockFreqFuncName; 64 extern cl::opt<unsigned> ViewHotFreqPercent; 65 66 static GVDAGType getGVDT() { 67 if (ViewBlockLayoutWithBFI != GVDT_None) 68 return ViewBlockLayoutWithBFI; 69 70 return ViewMachineBlockFreqPropagationDAG; 71 } 72 73 namespace llvm { 74 75 template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 76 typedef const MachineBasicBlock *NodeRef; 77 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 78 typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator; 79 80 static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) { 81 return &G->getFunction()->front(); 82 } 83 84 static ChildIteratorType child_begin(const NodeRef N) { 85 return N->succ_begin(); 86 } 87 88 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); } 89 90 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 91 return nodes_iterator(G->getFunction()->begin()); 92 } 93 94 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 95 return nodes_iterator(G->getFunction()->end()); 96 } 97 }; 98 99 typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, 100 MachineBranchProbabilityInfo> 101 MBFIDOTGraphTraitsBase; 102 template <> 103 struct DOTGraphTraits<MachineBlockFrequencyInfo *> 104 : public MBFIDOTGraphTraitsBase { 105 explicit DOTGraphTraits(bool isSimple = false) 106 : MBFIDOTGraphTraitsBase(isSimple), CurFunc(nullptr), LayoutOrderMap() {} 107 108 const MachineFunction *CurFunc; 109 DenseMap<const MachineBasicBlock *, int> LayoutOrderMap; 110 111 std::string getNodeLabel(const MachineBasicBlock *Node, 112 const MachineBlockFrequencyInfo *Graph) { 113 114 int layout_order = -1; 115 // Attach additional ordering information if 'isSimple' is false. 116 if (!isSimple()) { 117 const MachineFunction *F = Node->getParent(); 118 if (!CurFunc || F != CurFunc) { 119 if (CurFunc) 120 LayoutOrderMap.clear(); 121 122 CurFunc = F; 123 int O = 0; 124 for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) { 125 LayoutOrderMap[&*MBI] = O; 126 } 127 } 128 layout_order = LayoutOrderMap[Node]; 129 } 130 return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(), 131 layout_order); 132 } 133 134 std::string getNodeAttributes(const MachineBasicBlock *Node, 135 const MachineBlockFrequencyInfo *Graph) { 136 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, 137 ViewHotFreqPercent); 138 } 139 140 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, 141 const MachineBlockFrequencyInfo *MBFI) { 142 return MBFIDOTGraphTraitsBase::getEdgeAttributes( 143 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); 144 } 145 }; 146 147 } // end namespace llvm 148 #endif 149 150 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq", 151 "Machine Block Frequency Analysis", true, true) 152 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 153 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 154 INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq", 155 "Machine Block Frequency Analysis", true, true) 156 157 char MachineBlockFrequencyInfo::ID = 0; 158 159 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() 160 : MachineFunctionPass(ID) { 161 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 162 } 163 164 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {} 165 166 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 167 AU.addRequired<MachineBranchProbabilityInfo>(); 168 AU.addRequired<MachineLoopInfo>(); 169 AU.setPreservesAll(); 170 MachineFunctionPass::getAnalysisUsage(AU); 171 } 172 173 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 174 MachineBranchProbabilityInfo &MBPI = 175 getAnalysis<MachineBranchProbabilityInfo>(); 176 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 177 if (!MBFI) 178 MBFI.reset(new ImplType); 179 MBFI->calculate(F, MBPI, MLI); 180 #ifndef NDEBUG 181 if (ViewMachineBlockFreqPropagationDAG != GVDT_None && 182 (ViewBlockFreqFuncName.empty() || 183 F.getName().equals(ViewBlockFreqFuncName))) { 184 view(); 185 } 186 #endif 187 return false; 188 } 189 190 void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 191 192 /// Pop up a ghostview window with the current block frequency propagation 193 /// rendered using dot. 194 void MachineBlockFrequencyInfo::view(bool isSimple) const { 195 // This code is only for debugging. 196 #ifndef NDEBUG 197 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), 198 "MachineBlockFrequencyDAGs", isSimple); 199 #else 200 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds " 201 "on systems with Graphviz or gv!\n"; 202 #endif // NDEBUG 203 } 204 205 BlockFrequency 206 MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { 207 return MBFI ? MBFI->getBlockFreq(MBB) : 0; 208 } 209 210 Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( 211 const MachineBasicBlock *MBB) const { 212 const Function *F = MBFI->getFunction()->getFunction(); 213 return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None; 214 } 215 216 Optional<uint64_t> 217 MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 218 const Function *F = MBFI->getFunction()->getFunction(); 219 return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None; 220 } 221 222 const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 223 return MBFI ? MBFI->getFunction() : nullptr; 224 } 225 226 const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { 227 return MBFI ? &MBFI->getBPI() : nullptr; 228 } 229 230 raw_ostream & 231 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 232 const BlockFrequency Freq) const { 233 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 234 } 235 236 raw_ostream & 237 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 238 const MachineBasicBlock *MBB) const { 239 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 240 } 241 242 uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 243 return MBFI ? MBFI->getEntryFreq() : 0; 244 } 245