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" 15689a5073SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h" 16875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" 1710be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineFunction.h" 1810be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineLoopInfo.h" 19ed0881b2SChandler Carruth #include "llvm/CodeGen/Passes.h" 20ed0881b2SChandler Carruth #include "llvm/InitializePasses.h" 2165bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h" 2265bbcdfaSMichael Gottesman #include "llvm/Support/Debug.h" 2369317f2eSXinliang David Li #include "llvm/Support/Format.h" 2465bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h" 2569317f2eSXinliang David Li #include "llvm/Support/raw_ostream.h" 26875ebd5fSJakub Staszak 27875ebd5fSJakub Staszak using namespace llvm; 28875ebd5fSJakub Staszak 291b9dde08SChandler Carruth #define DEBUG_TYPE "block-freq" 301b9dde08SChandler Carruth 3165bbcdfaSMichael Gottesman 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."))); 45fd3f645fSXinliang David Li // Similar option above, but used to control BFI display only after MBP pass 46fd3f645fSXinliang David Li cl::opt<GVDAGType> ViewBlockLayoutWithBFI( 47fd3f645fSXinliang David Li "view-block-layout-with-bfi", cl::Hidden, 48fd3f645fSXinliang David Li cl::desc( 49fd3f645fSXinliang David Li "Pop up a window to show a dag displaying MBP layout and associated " 50fd3f645fSXinliang David Li "block frequencies of the CFG."), 51fd3f645fSXinliang David Li cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 52fd3f645fSXinliang David Li clEnumValN(GVDT_Fraction, "fraction", 53fd3f645fSXinliang David Li "display a graph using the " 54fd3f645fSXinliang David Li "fractional block frequency representation."), 55fd3f645fSXinliang David Li clEnumValN(GVDT_Integer, "integer", 56fd3f645fSXinliang David Li "display a graph using the raw " 57fd3f645fSXinliang David Li "integer fractional block frequency representation."), 58fd3f645fSXinliang David Li clEnumValN(GVDT_Count, "count", 59fd3f645fSXinliang David Li "display a graph using the real " 60fd3f645fSXinliang David Li "profile count if available."))); 6165bbcdfaSMichael Gottesman 6258fcc9bdSXinliang David Li // Command line option to specify the name of the function for CFG dump 6358fcc9bdSXinliang David Li // Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name= 648dd5ce97SXinliang David Li extern cl::opt<std::string> ViewBlockFreqFuncName; 6558fcc9bdSXinliang David Li // Command line option to specify hot frequency threshold. 6658fcc9bdSXinliang David Li // Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc= 672d531580SSimon Pilgrim extern cl::opt<unsigned> ViewHotFreqPercent; 6880457ce5SXinliang David Li 69fd3f645fSXinliang David Li static GVDAGType getGVDT() { 70fd3f645fSXinliang David Li if (ViewBlockLayoutWithBFI != GVDT_None) 71fd3f645fSXinliang David Li return ViewBlockLayoutWithBFI; 72fd3f645fSXinliang David Li 73fd3f645fSXinliang David Li return ViewMachineBlockFreqPropagationDAG; 74fd3f645fSXinliang David Li } 75fd3f645fSXinliang David Li 7665bbcdfaSMichael Gottesman namespace llvm { 7765bbcdfaSMichael Gottesman 78bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 79eb3958faSTim Shen typedef const MachineBasicBlock *NodeRef; 8065bbcdfaSMichael Gottesman typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 81b5e0f5acSTim Shen typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator; 8265bbcdfaSMichael Gottesman 8348f814e8STim Shen static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) { 840ac8eb91SDuncan P. N. Exon Smith return &G->getFunction()->front(); 8565bbcdfaSMichael Gottesman } 86748fe483SMichael Gottesman 87f2187ed3STim Shen static ChildIteratorType child_begin(const NodeRef N) { 8865bbcdfaSMichael Gottesman return N->succ_begin(); 8965bbcdfaSMichael Gottesman } 90748fe483SMichael Gottesman 91f2187ed3STim Shen static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); } 92748fe483SMichael Gottesman 9365bbcdfaSMichael Gottesman static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 94b5e0f5acSTim Shen return nodes_iterator(G->getFunction()->begin()); 9565bbcdfaSMichael Gottesman } 96748fe483SMichael Gottesman 9765bbcdfaSMichael Gottesman static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 98b5e0f5acSTim Shen return nodes_iterator(G->getFunction()->end()); 9965bbcdfaSMichael Gottesman } 10065bbcdfaSMichael Gottesman }; 10165bbcdfaSMichael Gottesman 10255415f25SXinliang David Li typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, 10355415f25SXinliang David Li MachineBranchProbabilityInfo> 10455415f25SXinliang David Li MBFIDOTGraphTraitsBase; 10565bbcdfaSMichael Gottesman template <> 106bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *> 10755415f25SXinliang David Li : public MBFIDOTGraphTraitsBase { 108bc157084SXinliang David Li explicit DOTGraphTraits(bool isSimple = false) 109fd3f645fSXinliang David Li : MBFIDOTGraphTraitsBase(isSimple), CurFunc(nullptr), LayoutOrderMap() {} 110fd3f645fSXinliang David Li 111fd3f645fSXinliang David Li const MachineFunction *CurFunc; 112fd3f645fSXinliang David Li DenseMap<const MachineBasicBlock *, int> LayoutOrderMap; 11365bbcdfaSMichael Gottesman 11465bbcdfaSMichael Gottesman std::string getNodeLabel(const MachineBasicBlock *Node, 11565bbcdfaSMichael Gottesman const MachineBlockFrequencyInfo *Graph) { 116fd3f645fSXinliang David Li 117fd3f645fSXinliang David Li int layout_order = -1; 118fd3f645fSXinliang David Li // Attach additional ordering information if 'isSimple' is false. 119fd3f645fSXinliang David Li if (!isSimple()) { 120fd3f645fSXinliang David Li const MachineFunction *F = Node->getParent(); 121fd3f645fSXinliang David Li if (!CurFunc || F != CurFunc) { 122fd3f645fSXinliang David Li if (CurFunc) 123fd3f645fSXinliang David Li LayoutOrderMap.clear(); 124fd3f645fSXinliang David Li 125fd3f645fSXinliang David Li CurFunc = F; 126fd3f645fSXinliang David Li int O = 0; 127fd3f645fSXinliang David Li for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) { 128fd3f645fSXinliang David Li LayoutOrderMap[&*MBI] = O; 129fd3f645fSXinliang David Li } 130fd3f645fSXinliang David Li } 131fd3f645fSXinliang David Li layout_order = LayoutOrderMap[Node]; 132fd3f645fSXinliang David Li } 133fd3f645fSXinliang David Li return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(), 134fd3f645fSXinliang David Li layout_order); 13555415f25SXinliang David Li } 13665bbcdfaSMichael Gottesman 1373e176c77SXinliang David Li std::string getNodeAttributes(const MachineBasicBlock *Node, 1383e176c77SXinliang David Li const MachineBlockFrequencyInfo *Graph) { 1393e176c77SXinliang David Li return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, 1403e176c77SXinliang David Li ViewHotFreqPercent); 1413e176c77SXinliang David Li } 1423e176c77SXinliang David Li 14355415f25SXinliang David Li std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, 14469317f2eSXinliang David Li const MachineBlockFrequencyInfo *MBFI) { 1453e176c77SXinliang David Li return MBFIDOTGraphTraitsBase::getEdgeAttributes( 1463e176c77SXinliang David Li Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); 14769317f2eSXinliang David Li } 14865bbcdfaSMichael Gottesman }; 14965bbcdfaSMichael Gottesman 15065bbcdfaSMichael Gottesman } // end namespace llvm 15165bbcdfaSMichael Gottesman 152875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq", 153875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 154875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 15510be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 156875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq", 157875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 158875ebd5fSJakub Staszak 159875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0; 160875ebd5fSJakub Staszak 161bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() 162bc157084SXinliang David Li : MachineFunctionPass(ID) { 163875ebd5fSJakub Staszak initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 164875ebd5fSJakub Staszak } 165875ebd5fSJakub Staszak 1663dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {} 167875ebd5fSJakub Staszak 168875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 169875ebd5fSJakub Staszak AU.addRequired<MachineBranchProbabilityInfo>(); 17010be9a88SDuncan P. N. Exon Smith AU.addRequired<MachineLoopInfo>(); 171875ebd5fSJakub Staszak AU.setPreservesAll(); 172875ebd5fSJakub Staszak MachineFunctionPass::getAnalysisUsage(AU); 173875ebd5fSJakub Staszak } 174875ebd5fSJakub Staszak 175*bbb141c7SAdam Nemet void MachineBlockFrequencyInfo::calculate( 176*bbb141c7SAdam Nemet const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, 177*bbb141c7SAdam Nemet const MachineLoopInfo &MLI) { 1783dbe1050SDuncan P. N. Exon Smith if (!MBFI) 1793dbe1050SDuncan P. N. Exon Smith MBFI.reset(new ImplType); 1805e67b666SCong Hou MBFI->calculate(F, MBPI, MLI); 18180457ce5SXinliang David Li if (ViewMachineBlockFreqPropagationDAG != GVDT_None && 1828dd5ce97SXinliang David Li (ViewBlockFreqFuncName.empty() || 1838dd5ce97SXinliang David Li F.getName().equals(ViewBlockFreqFuncName))) { 18465bbcdfaSMichael Gottesman view(); 18565bbcdfaSMichael Gottesman } 186*bbb141c7SAdam Nemet } 187*bbb141c7SAdam Nemet 188*bbb141c7SAdam Nemet bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 189*bbb141c7SAdam Nemet MachineBranchProbabilityInfo &MBPI = 190*bbb141c7SAdam Nemet getAnalysis<MachineBranchProbabilityInfo>(); 191*bbb141c7SAdam Nemet MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 192*bbb141c7SAdam Nemet calculate(F, MBPI, MLI); 193875ebd5fSJakub Staszak return false; 194875ebd5fSJakub Staszak } 195875ebd5fSJakub Staszak 1963dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 1973dbe1050SDuncan P. N. Exon Smith 19865bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation 19965bbcdfaSMichael Gottesman /// rendered using dot. 200fd3f645fSXinliang David Li void MachineBlockFrequencyInfo::view(bool isSimple) const { 20165bbcdfaSMichael Gottesman // This code is only for debugging. 20265bbcdfaSMichael Gottesman ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), 203fd3f645fSXinliang David Li "MachineBlockFrequencyDAGs", isSimple); 20465bbcdfaSMichael Gottesman } 20565bbcdfaSMichael Gottesman 206bc157084SXinliang David Li BlockFrequency 207bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { 2083dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getBlockFreq(MBB) : 0; 209875ebd5fSJakub Staszak } 21065bbcdfaSMichael Gottesman 21130c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( 21230c50f3cSXinliang David Li const MachineBasicBlock *MBB) const { 21330c50f3cSXinliang David Li const Function *F = MBFI->getFunction()->getFunction(); 21430c50f3cSXinliang David Li return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None; 21530c50f3cSXinliang David Li } 21630c50f3cSXinliang David Li 217f801575fSSean Silva Optional<uint64_t> 218f801575fSSean Silva MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 219f801575fSSean Silva const Function *F = MBFI->getFunction()->getFunction(); 220f801575fSSean Silva return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None; 221f801575fSSean Silva } 222f801575fSSean Silva 223936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 22410be9a88SDuncan P. N. Exon Smith return MBFI ? MBFI->getFunction() : nullptr; 22565bbcdfaSMichael Gottesman } 226fd5c4b2cSMichael Gottesman 2273264fdd3SXinliang David Li const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { 2283264fdd3SXinliang David Li return MBFI ? &MBFI->getBPI() : nullptr; 2293264fdd3SXinliang David Li } 2303264fdd3SXinliang David Li 231fd5c4b2cSMichael Gottesman raw_ostream & 232fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 233fd5c4b2cSMichael Gottesman const BlockFrequency Freq) const { 2343dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 235fd5c4b2cSMichael Gottesman } 236fd5c4b2cSMichael Gottesman 237fd5c4b2cSMichael Gottesman raw_ostream & 238fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 239fd5c4b2cSMichael Gottesman const MachineBasicBlock *MBB) const { 2403dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 241fd5c4b2cSMichael Gottesman } 242fd5c4b2cSMichael Gottesman 2435e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 2443dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getEntryFreq() : 0; 245fd5c4b2cSMichael Gottesman } 246