1689a5073SDuncan P. N. Exon Smith //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===// 2875ebd5fSJakub Staszak // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6875ebd5fSJakub Staszak // 7875ebd5fSJakub Staszak //===----------------------------------------------------------------------===// 8875ebd5fSJakub Staszak // 9875ebd5fSJakub Staszak // Loops should be simplified before this analysis. 10875ebd5fSJakub Staszak // 11875ebd5fSJakub Staszak //===----------------------------------------------------------------------===// 12875ebd5fSJakub Staszak 13875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 145df3d890SEugene Zelenko #include "llvm/ADT/DenseMap.h" 155df3d890SEugene Zelenko #include "llvm/ADT/None.h" 165df3d890SEugene Zelenko #include "llvm/ADT/iterator.h" 17689a5073SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h" 185df3d890SEugene Zelenko #include "llvm/CodeGen/MachineBasicBlock.h" 19875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" 2010be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineFunction.h" 2110be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineLoopInfo.h" 22*05da2fe5SReid Kleckner #include "llvm/InitializePasses.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 7163e17ebfSHiroshi Yamauchi static cl::opt<bool> PrintMachineBlockFreq( 7263e17ebfSHiroshi Yamauchi "print-machine-bfi", cl::init(false), cl::Hidden, 7363e17ebfSHiroshi Yamauchi cl::desc("Print the machine block frequency info.")); 7463e17ebfSHiroshi Yamauchi 7563e17ebfSHiroshi Yamauchi // Command line option to specify the name of the function for block frequency 7663e17ebfSHiroshi Yamauchi // dump. Defined in Analysis/BlockFrequencyInfo.cpp. 7763e17ebfSHiroshi Yamauchi extern cl::opt<std::string> PrintBlockFreqFuncName; 7863e17ebfSHiroshi 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 17675f72f6bSHiroshi Yamauchi MachineBlockFrequencyInfo::MachineBlockFrequencyInfo( 17775f72f6bSHiroshi Yamauchi MachineFunction &F, 17875f72f6bSHiroshi Yamauchi MachineBranchProbabilityInfo &MBPI, 17975f72f6bSHiroshi Yamauchi MachineLoopInfo &MLI) : MachineFunctionPass(ID) { 18075f72f6bSHiroshi Yamauchi calculate(F, MBPI, MLI); 18175f72f6bSHiroshi Yamauchi } 18275f72f6bSHiroshi Yamauchi 1835df3d890SEugene Zelenko MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default; 184875ebd5fSJakub Staszak 185875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 186875ebd5fSJakub Staszak AU.addRequired<MachineBranchProbabilityInfo>(); 18710be9a88SDuncan P. N. Exon Smith AU.addRequired<MachineLoopInfo>(); 188875ebd5fSJakub Staszak AU.setPreservesAll(); 189875ebd5fSJakub Staszak MachineFunctionPass::getAnalysisUsage(AU); 190875ebd5fSJakub Staszak } 191875ebd5fSJakub Staszak 192bbb141c7SAdam Nemet void MachineBlockFrequencyInfo::calculate( 193bbb141c7SAdam Nemet const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, 194bbb141c7SAdam Nemet const MachineLoopInfo &MLI) { 1953dbe1050SDuncan P. N. Exon Smith if (!MBFI) 1963dbe1050SDuncan P. N. Exon Smith MBFI.reset(new ImplType); 1975e67b666SCong Hou MBFI->calculate(F, MBPI, MLI); 19880457ce5SXinliang David Li if (ViewMachineBlockFreqPropagationDAG != GVDT_None && 1998dd5ce97SXinliang David Li (ViewBlockFreqFuncName.empty() || 2008dd5ce97SXinliang David Li F.getName().equals(ViewBlockFreqFuncName))) { 201538d6668SXinliang David Li view("MachineBlockFrequencyDAGS." + F.getName()); 20265bbcdfaSMichael Gottesman } 20363e17ebfSHiroshi Yamauchi if (PrintMachineBlockFreq && 20463e17ebfSHiroshi Yamauchi (PrintBlockFreqFuncName.empty() || 20563e17ebfSHiroshi Yamauchi F.getName().equals(PrintBlockFreqFuncName))) { 20663e17ebfSHiroshi Yamauchi MBFI->print(dbgs()); 20763e17ebfSHiroshi Yamauchi } 208bbb141c7SAdam Nemet } 209bbb141c7SAdam Nemet 210bbb141c7SAdam Nemet bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 211bbb141c7SAdam Nemet MachineBranchProbabilityInfo &MBPI = 212bbb141c7SAdam Nemet getAnalysis<MachineBranchProbabilityInfo>(); 213bbb141c7SAdam Nemet MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 214bbb141c7SAdam Nemet calculate(F, MBPI, MLI); 215875ebd5fSJakub Staszak return false; 216875ebd5fSJakub Staszak } 217875ebd5fSJakub Staszak 2183dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 2193dbe1050SDuncan P. N. Exon Smith 22065bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation 22165bbcdfaSMichael Gottesman /// rendered using dot. 222538d6668SXinliang David Li void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const { 22365bbcdfaSMichael Gottesman // This code is only for debugging. 224538d6668SXinliang David Li ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple); 22565bbcdfaSMichael Gottesman } 22665bbcdfaSMichael Gottesman 227bc157084SXinliang David Li BlockFrequency 228bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { 2293dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getBlockFreq(MBB) : 0; 230875ebd5fSJakub Staszak } 23165bbcdfaSMichael Gottesman 23230c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( 23330c50f3cSXinliang David Li const MachineBasicBlock *MBB) const { 234f1caa283SMatthias Braun const Function &F = MBFI->getFunction()->getFunction(); 235f1caa283SMatthias Braun return MBFI ? MBFI->getBlockProfileCount(F, MBB) : None; 23630c50f3cSXinliang David Li } 23730c50f3cSXinliang David Li 238f801575fSSean Silva Optional<uint64_t> 239f801575fSSean Silva MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 240f1caa283SMatthias Braun const Function &F = MBFI->getFunction()->getFunction(); 241f1caa283SMatthias Braun return MBFI ? MBFI->getProfileCountFromFreq(F, Freq) : None; 242f801575fSSean Silva } 243f801575fSSean Silva 244dce9def3SHiroshi Yamauchi bool 245dce9def3SHiroshi Yamauchi MachineBlockFrequencyInfo::isIrrLoopHeader(const MachineBasicBlock *MBB) { 246dce9def3SHiroshi Yamauchi assert(MBFI && "Expected analysis to be available"); 247dce9def3SHiroshi Yamauchi return MBFI->isIrrLoopHeader(MBB); 248dce9def3SHiroshi Yamauchi } 249dce9def3SHiroshi Yamauchi 250936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 25110be9a88SDuncan P. N. Exon Smith return MBFI ? MBFI->getFunction() : nullptr; 25265bbcdfaSMichael Gottesman } 253fd5c4b2cSMichael Gottesman 2543264fdd3SXinliang David Li const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { 2553264fdd3SXinliang David Li return MBFI ? &MBFI->getBPI() : nullptr; 2563264fdd3SXinliang David Li } 2573264fdd3SXinliang David Li 258fd5c4b2cSMichael Gottesman raw_ostream & 259fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 260fd5c4b2cSMichael Gottesman const BlockFrequency Freq) const { 2613dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 262fd5c4b2cSMichael Gottesman } 263fd5c4b2cSMichael Gottesman 264fd5c4b2cSMichael Gottesman raw_ostream & 265fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 266fd5c4b2cSMichael Gottesman const MachineBasicBlock *MBB) const { 2673dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 268fd5c4b2cSMichael Gottesman } 269fd5c4b2cSMichael Gottesman 2705e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 2713dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getEntryFreq() : 0; 272fd5c4b2cSMichael Gottesman } 273