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/ADT/DenseMap.h" 16 #include "llvm/ADT/None.h" 17 #include "llvm/ADT/iterator.h" 18 #include "llvm/Analysis/BlockFrequencyInfoImpl.h" 19 #include "llvm/CodeGen/MachineBasicBlock.h" 20 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" 21 #include "llvm/CodeGen/MachineFunction.h" 22 #include "llvm/CodeGen/MachineLoopInfo.h" 23 #include "llvm/Pass.h" 24 #include "llvm/Support/CommandLine.h" 25 #include "llvm/Support/GraphWriter.h" 26 #include <string> 27 28 using namespace llvm; 29 30 #define DEBUG_TYPE "machine-block-freq" 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 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 // Command line option to specify the name of the function for CFG dump 64 // Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name= 65 extern cl::opt<std::string> ViewBlockFreqFuncName; 66 67 // Command line option to specify hot frequency threshold. 68 // Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc= 69 extern cl::opt<unsigned> ViewHotFreqPercent; 70 71 static GVDAGType getGVDT() { 72 if (ViewBlockLayoutWithBFI != GVDT_None) 73 return ViewBlockLayoutWithBFI; 74 75 return ViewMachineBlockFreqPropagationDAG; 76 } 77 78 namespace llvm { 79 80 template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 81 using NodeRef = const MachineBasicBlock *; 82 using ChildIteratorType = MachineBasicBlock::const_succ_iterator; 83 using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>; 84 85 static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) { 86 return &G->getFunction()->front(); 87 } 88 89 static ChildIteratorType child_begin(const NodeRef N) { 90 return N->succ_begin(); 91 } 92 93 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); } 94 95 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 96 return nodes_iterator(G->getFunction()->begin()); 97 } 98 99 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 100 return nodes_iterator(G->getFunction()->end()); 101 } 102 }; 103 104 using MBFIDOTGraphTraitsBase = 105 BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, 106 MachineBranchProbabilityInfo>; 107 108 template <> 109 struct DOTGraphTraits<MachineBlockFrequencyInfo *> 110 : public MBFIDOTGraphTraitsBase { 111 const MachineFunction *CurFunc = nullptr; 112 DenseMap<const MachineBasicBlock *, int> LayoutOrderMap; 113 114 explicit DOTGraphTraits(bool isSimple = false) 115 : MBFIDOTGraphTraitsBase(isSimple) {} 116 117 std::string getNodeLabel(const MachineBasicBlock *Node, 118 const MachineBlockFrequencyInfo *Graph) { 119 int layout_order = -1; 120 // Attach additional ordering information if 'isSimple' is false. 121 if (!isSimple()) { 122 const MachineFunction *F = Node->getParent(); 123 if (!CurFunc || F != CurFunc) { 124 if (CurFunc) 125 LayoutOrderMap.clear(); 126 127 CurFunc = F; 128 int O = 0; 129 for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) { 130 LayoutOrderMap[&*MBI] = O; 131 } 132 } 133 layout_order = LayoutOrderMap[Node]; 134 } 135 return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(), 136 layout_order); 137 } 138 139 std::string getNodeAttributes(const MachineBasicBlock *Node, 140 const MachineBlockFrequencyInfo *Graph) { 141 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, 142 ViewHotFreqPercent); 143 } 144 145 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, 146 const MachineBlockFrequencyInfo *MBFI) { 147 return MBFIDOTGraphTraitsBase::getEdgeAttributes( 148 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); 149 } 150 }; 151 152 } // end namespace llvm 153 154 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE, 155 "Machine Block Frequency Analysis", true, true) 156 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 157 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 158 INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE, 159 "Machine Block Frequency Analysis", true, true) 160 161 char MachineBlockFrequencyInfo::ID = 0; 162 163 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() 164 : MachineFunctionPass(ID) { 165 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 166 } 167 168 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default; 169 170 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 171 AU.addRequired<MachineBranchProbabilityInfo>(); 172 AU.addRequired<MachineLoopInfo>(); 173 AU.setPreservesAll(); 174 MachineFunctionPass::getAnalysisUsage(AU); 175 } 176 177 void MachineBlockFrequencyInfo::calculate( 178 const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, 179 const MachineLoopInfo &MLI) { 180 if (!MBFI) 181 MBFI.reset(new ImplType); 182 MBFI->calculate(F, MBPI, MLI); 183 if (ViewMachineBlockFreqPropagationDAG != GVDT_None && 184 (ViewBlockFreqFuncName.empty() || 185 F.getName().equals(ViewBlockFreqFuncName))) { 186 view("MachineBlockFrequencyDAGS." + F.getName()); 187 } 188 } 189 190 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 191 MachineBranchProbabilityInfo &MBPI = 192 getAnalysis<MachineBranchProbabilityInfo>(); 193 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 194 calculate(F, MBPI, MLI); 195 return false; 196 } 197 198 void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 199 200 /// Pop up a ghostview window with the current block frequency propagation 201 /// rendered using dot. 202 void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const { 203 // This code is only for debugging. 204 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple); 205 } 206 207 BlockFrequency 208 MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { 209 return MBFI ? MBFI->getBlockFreq(MBB) : 0; 210 } 211 212 Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( 213 const MachineBasicBlock *MBB) const { 214 const Function *F = MBFI->getFunction()->getFunction(); 215 return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None; 216 } 217 218 Optional<uint64_t> 219 MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 220 const Function *F = MBFI->getFunction()->getFunction(); 221 return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None; 222 } 223 224 const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 225 return MBFI ? MBFI->getFunction() : nullptr; 226 } 227 228 const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { 229 return MBFI ? &MBFI->getBPI() : nullptr; 230 } 231 232 raw_ostream & 233 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 234 const BlockFrequency Freq) const { 235 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 236 } 237 238 raw_ostream & 239 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 240 const MachineBasicBlock *MBB) const { 241 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 242 } 243 244 uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 245 return MBFI ? MBFI->getEntryFreq() : 0; 246 } 247