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 "machine-block-freq" 30 31 32 static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG( 33 "view-machine-block-freq-propagation-dags", cl::Hidden, 34 cl::desc("Pop up a window to show a dag displaying how machine block " 35 "frequencies propagate through the CFG."), 36 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 37 clEnumValN(GVDT_Fraction, "fraction", 38 "display a graph using the " 39 "fractional block frequency representation."), 40 clEnumValN(GVDT_Integer, "integer", 41 "display a graph using the raw " 42 "integer fractional block frequency representation."), 43 clEnumValN(GVDT_Count, "count", "display a graph using the real " 44 "profile count if available."))); 45 // Similar option above, but used to control BFI display only after MBP pass 46 cl::opt<GVDAGType> ViewBlockLayoutWithBFI( 47 "view-block-layout-with-bfi", cl::Hidden, 48 cl::desc( 49 "Pop up a window to show a dag displaying MBP layout and associated " 50 "block frequencies of the CFG."), 51 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 52 clEnumValN(GVDT_Fraction, "fraction", 53 "display a graph using the " 54 "fractional block frequency representation."), 55 clEnumValN(GVDT_Integer, "integer", 56 "display a graph using the raw " 57 "integer fractional block frequency representation."), 58 clEnumValN(GVDT_Count, "count", 59 "display a graph using the real " 60 "profile count if available."))); 61 62 // Command line option to specify the name of the function for CFG dump 63 // Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name= 64 extern cl::opt<std::string> ViewBlockFreqFuncName; 65 // Command line option to specify hot frequency threshold. 66 // Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc= 67 extern cl::opt<unsigned> ViewHotFreqPercent; 68 69 static GVDAGType getGVDT() { 70 if (ViewBlockLayoutWithBFI != GVDT_None) 71 return ViewBlockLayoutWithBFI; 72 73 return ViewMachineBlockFreqPropagationDAG; 74 } 75 76 namespace llvm { 77 78 template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 79 typedef const MachineBasicBlock *NodeRef; 80 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 81 typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator; 82 83 static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) { 84 return &G->getFunction()->front(); 85 } 86 87 static ChildIteratorType child_begin(const NodeRef N) { 88 return N->succ_begin(); 89 } 90 91 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); } 92 93 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 94 return nodes_iterator(G->getFunction()->begin()); 95 } 96 97 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 98 return nodes_iterator(G->getFunction()->end()); 99 } 100 }; 101 102 typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, 103 MachineBranchProbabilityInfo> 104 MBFIDOTGraphTraitsBase; 105 template <> 106 struct DOTGraphTraits<MachineBlockFrequencyInfo *> 107 : public MBFIDOTGraphTraitsBase { 108 explicit DOTGraphTraits(bool isSimple = false) 109 : MBFIDOTGraphTraitsBase(isSimple), CurFunc(nullptr), LayoutOrderMap() {} 110 111 const MachineFunction *CurFunc; 112 DenseMap<const MachineBasicBlock *, int> LayoutOrderMap; 113 114 std::string getNodeLabel(const MachineBasicBlock *Node, 115 const MachineBlockFrequencyInfo *Graph) { 116 117 int layout_order = -1; 118 // Attach additional ordering information if 'isSimple' is false. 119 if (!isSimple()) { 120 const MachineFunction *F = Node->getParent(); 121 if (!CurFunc || F != CurFunc) { 122 if (CurFunc) 123 LayoutOrderMap.clear(); 124 125 CurFunc = F; 126 int O = 0; 127 for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) { 128 LayoutOrderMap[&*MBI] = O; 129 } 130 } 131 layout_order = LayoutOrderMap[Node]; 132 } 133 return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(), 134 layout_order); 135 } 136 137 std::string getNodeAttributes(const MachineBasicBlock *Node, 138 const MachineBlockFrequencyInfo *Graph) { 139 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, 140 ViewHotFreqPercent); 141 } 142 143 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, 144 const MachineBlockFrequencyInfo *MBFI) { 145 return MBFIDOTGraphTraitsBase::getEdgeAttributes( 146 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); 147 } 148 }; 149 150 } // end namespace llvm 151 152 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE, 153 "Machine Block Frequency Analysis", true, true) 154 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 155 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 156 INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE, 157 "Machine Block Frequency Analysis", true, true) 158 159 char MachineBlockFrequencyInfo::ID = 0; 160 161 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() 162 : MachineFunctionPass(ID) { 163 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 164 } 165 166 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {} 167 168 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 169 AU.addRequired<MachineBranchProbabilityInfo>(); 170 AU.addRequired<MachineLoopInfo>(); 171 AU.setPreservesAll(); 172 MachineFunctionPass::getAnalysisUsage(AU); 173 } 174 175 void MachineBlockFrequencyInfo::calculate( 176 const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, 177 const MachineLoopInfo &MLI) { 178 if (!MBFI) 179 MBFI.reset(new ImplType); 180 MBFI->calculate(F, MBPI, MLI); 181 if (ViewMachineBlockFreqPropagationDAG != GVDT_None && 182 (ViewBlockFreqFuncName.empty() || 183 F.getName().equals(ViewBlockFreqFuncName))) { 184 view("MachineBlockFrequencyDAGS." + F.getName()); 185 } 186 } 187 188 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 189 MachineBranchProbabilityInfo &MBPI = 190 getAnalysis<MachineBranchProbabilityInfo>(); 191 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 192 calculate(F, MBPI, MLI); 193 return false; 194 } 195 196 void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 197 198 /// Pop up a ghostview window with the current block frequency propagation 199 /// rendered using dot. 200 void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const { 201 // This code is only for debugging. 202 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple); 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