1689a5073SDuncan P. N. Exon Smith //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===// 2875ebd5fSJakub Staszak // 3875ebd5fSJakub Staszak // The LLVM Compiler Infrastructure 4875ebd5fSJakub Staszak // 5875ebd5fSJakub Staszak // This file is distributed under the University of Illinois Open Source 6875ebd5fSJakub Staszak // License. See LICENSE.TXT for details. 7875ebd5fSJakub Staszak // 8875ebd5fSJakub Staszak //===----------------------------------------------------------------------===// 9875ebd5fSJakub Staszak // 10875ebd5fSJakub Staszak // Loops should be simplified before this analysis. 11875ebd5fSJakub Staszak // 12875ebd5fSJakub Staszak //===----------------------------------------------------------------------===// 13875ebd5fSJakub Staszak 14875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 155df3d890SEugene Zelenko #include "llvm/ADT/DenseMap.h" 165df3d890SEugene Zelenko #include "llvm/ADT/None.h" 175df3d890SEugene Zelenko #include "llvm/ADT/iterator.h" 18689a5073SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h" 195df3d890SEugene Zelenko #include "llvm/CodeGen/MachineBasicBlock.h" 20875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" 2110be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineFunction.h" 2210be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineLoopInfo.h" 235df3d890SEugene Zelenko #include "llvm/Pass.h" 2465bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h" 2565bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h" 265df3d890SEugene Zelenko #include <string> 27875ebd5fSJakub Staszak 28875ebd5fSJakub Staszak using namespace llvm; 29875ebd5fSJakub Staszak 301527baabSMatthias Braun #define DEBUG_TYPE "machine-block-freq" 311b9dde08SChandler Carruth 32bc157084SXinliang David Li static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG( 33bc157084SXinliang David Li "view-machine-block-freq-propagation-dags", cl::Hidden, 3465bbcdfaSMichael Gottesman cl::desc("Pop up a window to show a dag displaying how machine block " 35748fe483SMichael Gottesman "frequencies propagate through the CFG."), 36bc157084SXinliang David Li cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 37bc157084SXinliang David Li clEnumValN(GVDT_Fraction, "fraction", 38bc157084SXinliang David Li "display a graph using the " 3965bbcdfaSMichael Gottesman "fractional block frequency representation."), 40bc157084SXinliang David Li clEnumValN(GVDT_Integer, "integer", 41bc157084SXinliang David Li "display a graph using the raw " 4265bbcdfaSMichael Gottesman "integer fractional block frequency representation."), 4330c50f3cSXinliang David Li clEnumValN(GVDT_Count, "count", "display a graph using the real " 44732afdd0SMehdi Amini "profile count if available."))); 455df3d890SEugene Zelenko 46fd3f645fSXinliang David Li // Similar option above, but used to control BFI display only after MBP pass 47fd3f645fSXinliang David Li cl::opt<GVDAGType> ViewBlockLayoutWithBFI( 48fd3f645fSXinliang David Li "view-block-layout-with-bfi", cl::Hidden, 49fd3f645fSXinliang David Li cl::desc( 50fd3f645fSXinliang David Li "Pop up a window to show a dag displaying MBP layout and associated " 51fd3f645fSXinliang David Li "block frequencies of the CFG."), 52fd3f645fSXinliang David Li cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 53fd3f645fSXinliang David Li clEnumValN(GVDT_Fraction, "fraction", 54fd3f645fSXinliang David Li "display a graph using the " 55fd3f645fSXinliang David Li "fractional block frequency representation."), 56fd3f645fSXinliang David Li clEnumValN(GVDT_Integer, "integer", 57fd3f645fSXinliang David Li "display a graph using the raw " 58fd3f645fSXinliang David Li "integer fractional block frequency representation."), 59fd3f645fSXinliang David Li clEnumValN(GVDT_Count, "count", 60fd3f645fSXinliang David Li "display a graph using the real " 61fd3f645fSXinliang David Li "profile count if available."))); 6265bbcdfaSMichael Gottesman 6358fcc9bdSXinliang David Li // Command line option to specify the name of the function for CFG dump 6458fcc9bdSXinliang David Li // Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name= 658dd5ce97SXinliang David Li extern cl::opt<std::string> ViewBlockFreqFuncName; 665df3d890SEugene Zelenko 6758fcc9bdSXinliang David Li // Command line option to specify hot frequency threshold. 6858fcc9bdSXinliang David Li // Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc= 692d531580SSimon Pilgrim extern cl::opt<unsigned> ViewHotFreqPercent; 7080457ce5SXinliang David Li 71*63e17ebfSHiroshi Yamauchi static cl::opt<bool> PrintMachineBlockFreq( 72*63e17ebfSHiroshi Yamauchi "print-machine-bfi", cl::init(false), cl::Hidden, 73*63e17ebfSHiroshi Yamauchi cl::desc("Print the machine block frequency info.")); 74*63e17ebfSHiroshi Yamauchi 75*63e17ebfSHiroshi Yamauchi // Command line option to specify the name of the function for block frequency 76*63e17ebfSHiroshi Yamauchi // dump. Defined in Analysis/BlockFrequencyInfo.cpp. 77*63e17ebfSHiroshi Yamauchi extern cl::opt<std::string> PrintBlockFreqFuncName; 78*63e17ebfSHiroshi Yamauchi 79fd3f645fSXinliang David Li static GVDAGType getGVDT() { 80fd3f645fSXinliang David Li if (ViewBlockLayoutWithBFI != GVDT_None) 81fd3f645fSXinliang David Li return ViewBlockLayoutWithBFI; 82fd3f645fSXinliang David Li 83fd3f645fSXinliang David Li return ViewMachineBlockFreqPropagationDAG; 84fd3f645fSXinliang David Li } 85fd3f645fSXinliang David Li 8665bbcdfaSMichael Gottesman namespace llvm { 8765bbcdfaSMichael Gottesman 88bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 895df3d890SEugene Zelenko using NodeRef = const MachineBasicBlock *; 905df3d890SEugene Zelenko using ChildIteratorType = MachineBasicBlock::const_succ_iterator; 915df3d890SEugene Zelenko using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>; 9265bbcdfaSMichael Gottesman 9348f814e8STim Shen static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) { 940ac8eb91SDuncan P. N. Exon Smith return &G->getFunction()->front(); 9565bbcdfaSMichael Gottesman } 96748fe483SMichael Gottesman 97f2187ed3STim Shen static ChildIteratorType child_begin(const NodeRef N) { 9865bbcdfaSMichael Gottesman return N->succ_begin(); 9965bbcdfaSMichael Gottesman } 100748fe483SMichael Gottesman 101f2187ed3STim Shen static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); } 102748fe483SMichael Gottesman 10365bbcdfaSMichael Gottesman static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 104b5e0f5acSTim Shen return nodes_iterator(G->getFunction()->begin()); 10565bbcdfaSMichael Gottesman } 106748fe483SMichael Gottesman 10765bbcdfaSMichael Gottesman static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 108b5e0f5acSTim Shen return nodes_iterator(G->getFunction()->end()); 10965bbcdfaSMichael Gottesman } 11065bbcdfaSMichael Gottesman }; 11165bbcdfaSMichael Gottesman 1125df3d890SEugene Zelenko using MBFIDOTGraphTraitsBase = 1135df3d890SEugene Zelenko BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, 1145df3d890SEugene Zelenko MachineBranchProbabilityInfo>; 1155df3d890SEugene Zelenko 11665bbcdfaSMichael Gottesman template <> 117bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *> 11855415f25SXinliang David Li : public MBFIDOTGraphTraitsBase { 1195df3d890SEugene Zelenko const MachineFunction *CurFunc = nullptr; 120fd3f645fSXinliang David Li DenseMap<const MachineBasicBlock *, int> LayoutOrderMap; 12165bbcdfaSMichael Gottesman 1225df3d890SEugene Zelenko explicit DOTGraphTraits(bool isSimple = false) 1235df3d890SEugene Zelenko : MBFIDOTGraphTraitsBase(isSimple) {} 1245df3d890SEugene Zelenko 12565bbcdfaSMichael Gottesman std::string getNodeLabel(const MachineBasicBlock *Node, 12665bbcdfaSMichael Gottesman const MachineBlockFrequencyInfo *Graph) { 127fd3f645fSXinliang David Li int layout_order = -1; 128fd3f645fSXinliang David Li // Attach additional ordering information if 'isSimple' is false. 129fd3f645fSXinliang David Li if (!isSimple()) { 130fd3f645fSXinliang David Li const MachineFunction *F = Node->getParent(); 131fd3f645fSXinliang David Li if (!CurFunc || F != CurFunc) { 132fd3f645fSXinliang David Li if (CurFunc) 133fd3f645fSXinliang David Li LayoutOrderMap.clear(); 134fd3f645fSXinliang David Li 135fd3f645fSXinliang David Li CurFunc = F; 136fd3f645fSXinliang David Li int O = 0; 137fd3f645fSXinliang David Li for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) { 138fd3f645fSXinliang David Li LayoutOrderMap[&*MBI] = O; 139fd3f645fSXinliang David Li } 140fd3f645fSXinliang David Li } 141fd3f645fSXinliang David Li layout_order = LayoutOrderMap[Node]; 142fd3f645fSXinliang David Li } 143fd3f645fSXinliang David Li return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(), 144fd3f645fSXinliang David Li layout_order); 14555415f25SXinliang David Li } 14665bbcdfaSMichael Gottesman 1473e176c77SXinliang David Li std::string getNodeAttributes(const MachineBasicBlock *Node, 1483e176c77SXinliang David Li const MachineBlockFrequencyInfo *Graph) { 1493e176c77SXinliang David Li return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, 1503e176c77SXinliang David Li ViewHotFreqPercent); 1513e176c77SXinliang David Li } 1523e176c77SXinliang David Li 15355415f25SXinliang David Li std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, 15469317f2eSXinliang David Li const MachineBlockFrequencyInfo *MBFI) { 1553e176c77SXinliang David Li return MBFIDOTGraphTraitsBase::getEdgeAttributes( 1563e176c77SXinliang David Li Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); 15769317f2eSXinliang David Li } 15865bbcdfaSMichael Gottesman }; 15965bbcdfaSMichael Gottesman 16065bbcdfaSMichael Gottesman } // end namespace llvm 16165bbcdfaSMichael Gottesman 1621527baabSMatthias Braun INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE, 163875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 164875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 16510be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 1661527baabSMatthias Braun INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE, 167875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 168875ebd5fSJakub Staszak 169875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0; 170875ebd5fSJakub Staszak 171bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() 172bc157084SXinliang David Li : MachineFunctionPass(ID) { 173875ebd5fSJakub Staszak initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 174875ebd5fSJakub Staszak } 175875ebd5fSJakub Staszak 1765df3d890SEugene Zelenko MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default; 177875ebd5fSJakub Staszak 178875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 179875ebd5fSJakub Staszak AU.addRequired<MachineBranchProbabilityInfo>(); 18010be9a88SDuncan P. N. Exon Smith AU.addRequired<MachineLoopInfo>(); 181875ebd5fSJakub Staszak AU.setPreservesAll(); 182875ebd5fSJakub Staszak MachineFunctionPass::getAnalysisUsage(AU); 183875ebd5fSJakub Staszak } 184875ebd5fSJakub Staszak 185bbb141c7SAdam Nemet void MachineBlockFrequencyInfo::calculate( 186bbb141c7SAdam Nemet const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, 187bbb141c7SAdam Nemet const MachineLoopInfo &MLI) { 1883dbe1050SDuncan P. N. Exon Smith if (!MBFI) 1893dbe1050SDuncan P. N. Exon Smith MBFI.reset(new ImplType); 1905e67b666SCong Hou MBFI->calculate(F, MBPI, MLI); 19180457ce5SXinliang David Li if (ViewMachineBlockFreqPropagationDAG != GVDT_None && 1928dd5ce97SXinliang David Li (ViewBlockFreqFuncName.empty() || 1938dd5ce97SXinliang David Li F.getName().equals(ViewBlockFreqFuncName))) { 194538d6668SXinliang David Li view("MachineBlockFrequencyDAGS." + F.getName()); 19565bbcdfaSMichael Gottesman } 196*63e17ebfSHiroshi Yamauchi if (PrintMachineBlockFreq && 197*63e17ebfSHiroshi Yamauchi (PrintBlockFreqFuncName.empty() || 198*63e17ebfSHiroshi Yamauchi F.getName().equals(PrintBlockFreqFuncName))) { 199*63e17ebfSHiroshi Yamauchi MBFI->print(dbgs()); 200*63e17ebfSHiroshi Yamauchi } 201bbb141c7SAdam Nemet } 202bbb141c7SAdam Nemet 203bbb141c7SAdam Nemet bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 204bbb141c7SAdam Nemet MachineBranchProbabilityInfo &MBPI = 205bbb141c7SAdam Nemet getAnalysis<MachineBranchProbabilityInfo>(); 206bbb141c7SAdam Nemet MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 207bbb141c7SAdam Nemet calculate(F, MBPI, MLI); 208875ebd5fSJakub Staszak return false; 209875ebd5fSJakub Staszak } 210875ebd5fSJakub Staszak 2113dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 2123dbe1050SDuncan P. N. Exon Smith 21365bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation 21465bbcdfaSMichael Gottesman /// rendered using dot. 215538d6668SXinliang David Li void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const { 21665bbcdfaSMichael Gottesman // This code is only for debugging. 217538d6668SXinliang David Li ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple); 21865bbcdfaSMichael Gottesman } 21965bbcdfaSMichael Gottesman 220bc157084SXinliang David Li BlockFrequency 221bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { 2223dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getBlockFreq(MBB) : 0; 223875ebd5fSJakub Staszak } 22465bbcdfaSMichael Gottesman 22530c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( 22630c50f3cSXinliang David Li const MachineBasicBlock *MBB) const { 22730c50f3cSXinliang David Li const Function *F = MBFI->getFunction()->getFunction(); 22830c50f3cSXinliang David Li return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None; 22930c50f3cSXinliang David Li } 23030c50f3cSXinliang David Li 231f801575fSSean Silva Optional<uint64_t> 232f801575fSSean Silva MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 233f801575fSSean Silva const Function *F = MBFI->getFunction()->getFunction(); 234f801575fSSean Silva return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None; 235f801575fSSean Silva } 236f801575fSSean Silva 237936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 23810be9a88SDuncan P. N. Exon Smith return MBFI ? MBFI->getFunction() : nullptr; 23965bbcdfaSMichael Gottesman } 240fd5c4b2cSMichael Gottesman 2413264fdd3SXinliang David Li const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { 2423264fdd3SXinliang David Li return MBFI ? &MBFI->getBPI() : nullptr; 2433264fdd3SXinliang David Li } 2443264fdd3SXinliang David Li 245fd5c4b2cSMichael Gottesman raw_ostream & 246fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 247fd5c4b2cSMichael Gottesman const BlockFrequency Freq) const { 2483dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 249fd5c4b2cSMichael Gottesman } 250fd5c4b2cSMichael Gottesman 251fd5c4b2cSMichael Gottesman raw_ostream & 252fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 253fd5c4b2cSMichael Gottesman const MachineBasicBlock *MBB) const { 2543dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 255fd5c4b2cSMichael Gottesman } 256fd5c4b2cSMichael Gottesman 2575e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 2583dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getEntryFreq() : 0; 259fd5c4b2cSMichael Gottesman } 260