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" 15*5df3d890SEugene Zelenko #include "llvm/ADT/DenseMap.h" 16*5df3d890SEugene Zelenko #include "llvm/ADT/None.h" 17*5df3d890SEugene Zelenko #include "llvm/ADT/iterator.h" 18689a5073SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h" 19*5df3d890SEugene 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" 23*5df3d890SEugene Zelenko #include "llvm/Pass.h" 2465bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h" 2565bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h" 26*5df3d890SEugene 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."))); 45*5df3d890SEugene 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; 66*5df3d890SEugene 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 71fd3f645fSXinliang David Li static GVDAGType getGVDT() { 72fd3f645fSXinliang David Li if (ViewBlockLayoutWithBFI != GVDT_None) 73fd3f645fSXinliang David Li return ViewBlockLayoutWithBFI; 74fd3f645fSXinliang David Li 75fd3f645fSXinliang David Li return ViewMachineBlockFreqPropagationDAG; 76fd3f645fSXinliang David Li } 77fd3f645fSXinliang David Li 7865bbcdfaSMichael Gottesman namespace llvm { 7965bbcdfaSMichael Gottesman 80bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 81*5df3d890SEugene Zelenko using NodeRef = const MachineBasicBlock *; 82*5df3d890SEugene Zelenko using ChildIteratorType = MachineBasicBlock::const_succ_iterator; 83*5df3d890SEugene Zelenko using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>; 8465bbcdfaSMichael Gottesman 8548f814e8STim Shen static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) { 860ac8eb91SDuncan P. N. Exon Smith return &G->getFunction()->front(); 8765bbcdfaSMichael Gottesman } 88748fe483SMichael Gottesman 89f2187ed3STim Shen static ChildIteratorType child_begin(const NodeRef N) { 9065bbcdfaSMichael Gottesman return N->succ_begin(); 9165bbcdfaSMichael Gottesman } 92748fe483SMichael Gottesman 93f2187ed3STim Shen static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); } 94748fe483SMichael Gottesman 9565bbcdfaSMichael Gottesman static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 96b5e0f5acSTim Shen return nodes_iterator(G->getFunction()->begin()); 9765bbcdfaSMichael Gottesman } 98748fe483SMichael Gottesman 9965bbcdfaSMichael Gottesman static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 100b5e0f5acSTim Shen return nodes_iterator(G->getFunction()->end()); 10165bbcdfaSMichael Gottesman } 10265bbcdfaSMichael Gottesman }; 10365bbcdfaSMichael Gottesman 104*5df3d890SEugene Zelenko using MBFIDOTGraphTraitsBase = 105*5df3d890SEugene Zelenko BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, 106*5df3d890SEugene Zelenko MachineBranchProbabilityInfo>; 107*5df3d890SEugene Zelenko 10865bbcdfaSMichael Gottesman template <> 109bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *> 11055415f25SXinliang David Li : public MBFIDOTGraphTraitsBase { 111*5df3d890SEugene Zelenko const MachineFunction *CurFunc = nullptr; 112fd3f645fSXinliang David Li DenseMap<const MachineBasicBlock *, int> LayoutOrderMap; 11365bbcdfaSMichael Gottesman 114*5df3d890SEugene Zelenko explicit DOTGraphTraits(bool isSimple = false) 115*5df3d890SEugene Zelenko : MBFIDOTGraphTraitsBase(isSimple) {} 116*5df3d890SEugene Zelenko 11765bbcdfaSMichael Gottesman std::string getNodeLabel(const MachineBasicBlock *Node, 11865bbcdfaSMichael Gottesman const MachineBlockFrequencyInfo *Graph) { 119fd3f645fSXinliang David Li int layout_order = -1; 120fd3f645fSXinliang David Li // Attach additional ordering information if 'isSimple' is false. 121fd3f645fSXinliang David Li if (!isSimple()) { 122fd3f645fSXinliang David Li const MachineFunction *F = Node->getParent(); 123fd3f645fSXinliang David Li if (!CurFunc || F != CurFunc) { 124fd3f645fSXinliang David Li if (CurFunc) 125fd3f645fSXinliang David Li LayoutOrderMap.clear(); 126fd3f645fSXinliang David Li 127fd3f645fSXinliang David Li CurFunc = F; 128fd3f645fSXinliang David Li int O = 0; 129fd3f645fSXinliang David Li for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) { 130fd3f645fSXinliang David Li LayoutOrderMap[&*MBI] = O; 131fd3f645fSXinliang David Li } 132fd3f645fSXinliang David Li } 133fd3f645fSXinliang David Li layout_order = LayoutOrderMap[Node]; 134fd3f645fSXinliang David Li } 135fd3f645fSXinliang David Li return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(), 136fd3f645fSXinliang David Li layout_order); 13755415f25SXinliang David Li } 13865bbcdfaSMichael Gottesman 1393e176c77SXinliang David Li std::string getNodeAttributes(const MachineBasicBlock *Node, 1403e176c77SXinliang David Li const MachineBlockFrequencyInfo *Graph) { 1413e176c77SXinliang David Li return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, 1423e176c77SXinliang David Li ViewHotFreqPercent); 1433e176c77SXinliang David Li } 1443e176c77SXinliang David Li 14555415f25SXinliang David Li std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, 14669317f2eSXinliang David Li const MachineBlockFrequencyInfo *MBFI) { 1473e176c77SXinliang David Li return MBFIDOTGraphTraitsBase::getEdgeAttributes( 1483e176c77SXinliang David Li Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); 14969317f2eSXinliang David Li } 15065bbcdfaSMichael Gottesman }; 15165bbcdfaSMichael Gottesman 15265bbcdfaSMichael Gottesman } // end namespace llvm 15365bbcdfaSMichael Gottesman 1541527baabSMatthias Braun INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE, 155875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 156875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 15710be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 1581527baabSMatthias Braun INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE, 159875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 160875ebd5fSJakub Staszak 161875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0; 162875ebd5fSJakub Staszak 163bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() 164bc157084SXinliang David Li : MachineFunctionPass(ID) { 165875ebd5fSJakub Staszak initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 166875ebd5fSJakub Staszak } 167875ebd5fSJakub Staszak 168*5df3d890SEugene Zelenko MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default; 169875ebd5fSJakub Staszak 170875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 171875ebd5fSJakub Staszak AU.addRequired<MachineBranchProbabilityInfo>(); 17210be9a88SDuncan P. N. Exon Smith AU.addRequired<MachineLoopInfo>(); 173875ebd5fSJakub Staszak AU.setPreservesAll(); 174875ebd5fSJakub Staszak MachineFunctionPass::getAnalysisUsage(AU); 175875ebd5fSJakub Staszak } 176875ebd5fSJakub Staszak 177bbb141c7SAdam Nemet void MachineBlockFrequencyInfo::calculate( 178bbb141c7SAdam Nemet const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, 179bbb141c7SAdam Nemet const MachineLoopInfo &MLI) { 1803dbe1050SDuncan P. N. Exon Smith if (!MBFI) 1813dbe1050SDuncan P. N. Exon Smith MBFI.reset(new ImplType); 1825e67b666SCong Hou MBFI->calculate(F, MBPI, MLI); 18380457ce5SXinliang David Li if (ViewMachineBlockFreqPropagationDAG != GVDT_None && 1848dd5ce97SXinliang David Li (ViewBlockFreqFuncName.empty() || 1858dd5ce97SXinliang David Li F.getName().equals(ViewBlockFreqFuncName))) { 186538d6668SXinliang David Li view("MachineBlockFrequencyDAGS." + F.getName()); 18765bbcdfaSMichael Gottesman } 188bbb141c7SAdam Nemet } 189bbb141c7SAdam Nemet 190bbb141c7SAdam Nemet bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 191bbb141c7SAdam Nemet MachineBranchProbabilityInfo &MBPI = 192bbb141c7SAdam Nemet getAnalysis<MachineBranchProbabilityInfo>(); 193bbb141c7SAdam Nemet MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 194bbb141c7SAdam Nemet calculate(F, MBPI, MLI); 195875ebd5fSJakub Staszak return false; 196875ebd5fSJakub Staszak } 197875ebd5fSJakub Staszak 1983dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 1993dbe1050SDuncan P. N. Exon Smith 20065bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation 20165bbcdfaSMichael Gottesman /// rendered using dot. 202538d6668SXinliang David Li void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const { 20365bbcdfaSMichael Gottesman // This code is only for debugging. 204538d6668SXinliang David Li ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple); 20565bbcdfaSMichael Gottesman } 20665bbcdfaSMichael Gottesman 207bc157084SXinliang David Li BlockFrequency 208bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { 2093dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getBlockFreq(MBB) : 0; 210875ebd5fSJakub Staszak } 21165bbcdfaSMichael Gottesman 21230c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( 21330c50f3cSXinliang David Li const MachineBasicBlock *MBB) const { 21430c50f3cSXinliang David Li const Function *F = MBFI->getFunction()->getFunction(); 21530c50f3cSXinliang David Li return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None; 21630c50f3cSXinliang David Li } 21730c50f3cSXinliang David Li 218f801575fSSean Silva Optional<uint64_t> 219f801575fSSean Silva MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 220f801575fSSean Silva const Function *F = MBFI->getFunction()->getFunction(); 221f801575fSSean Silva return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None; 222f801575fSSean Silva } 223f801575fSSean Silva 224936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 22510be9a88SDuncan P. N. Exon Smith return MBFI ? MBFI->getFunction() : nullptr; 22665bbcdfaSMichael Gottesman } 227fd5c4b2cSMichael Gottesman 2283264fdd3SXinliang David Li const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { 2293264fdd3SXinliang David Li return MBFI ? &MBFI->getBPI() : nullptr; 2303264fdd3SXinliang David Li } 2313264fdd3SXinliang David Li 232fd5c4b2cSMichael Gottesman raw_ostream & 233fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 234fd5c4b2cSMichael Gottesman const BlockFrequency Freq) const { 2353dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 236fd5c4b2cSMichael Gottesman } 237fd5c4b2cSMichael Gottesman 238fd5c4b2cSMichael Gottesman raw_ostream & 239fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 240fd5c4b2cSMichael Gottesman const MachineBasicBlock *MBB) const { 2413dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 242fd5c4b2cSMichael Gottesman } 243fd5c4b2cSMichael Gottesman 2445e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 2453dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getEntryFreq() : 0; 246fd5c4b2cSMichael Gottesman } 247