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" 225df3d890SEugene Zelenko #include "llvm/Pass.h" 2365bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h" 2465bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h" 255df3d890SEugene Zelenko #include <string> 26875ebd5fSJakub Staszak 27875ebd5fSJakub Staszak using namespace llvm; 28875ebd5fSJakub Staszak 291527baabSMatthias Braun #define DEBUG_TYPE "machine-block-freq" 301b9dde08SChandler Carruth 31bc157084SXinliang David Li static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG( 32bc157084SXinliang David Li "view-machine-block-freq-propagation-dags", cl::Hidden, 3365bbcdfaSMichael Gottesman cl::desc("Pop up a window to show a dag displaying how machine block " 34748fe483SMichael Gottesman "frequencies propagate through the CFG."), 35bc157084SXinliang David Li cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 36bc157084SXinliang David Li clEnumValN(GVDT_Fraction, "fraction", 37bc157084SXinliang David Li "display a graph using the " 3865bbcdfaSMichael Gottesman "fractional block frequency representation."), 39bc157084SXinliang David Li clEnumValN(GVDT_Integer, "integer", 40bc157084SXinliang David Li "display a graph using the raw " 4165bbcdfaSMichael Gottesman "integer fractional block frequency representation."), 4230c50f3cSXinliang David Li clEnumValN(GVDT_Count, "count", "display a graph using the real " 43732afdd0SMehdi Amini "profile count if available."))); 445df3d890SEugene Zelenko 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; 655df3d890SEugene Zelenko 6658fcc9bdSXinliang David Li // Command line option to specify hot frequency threshold. 6758fcc9bdSXinliang David Li // Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc= 682d531580SSimon Pilgrim extern cl::opt<unsigned> ViewHotFreqPercent; 6980457ce5SXinliang David Li 7063e17ebfSHiroshi Yamauchi static cl::opt<bool> PrintMachineBlockFreq( 7163e17ebfSHiroshi Yamauchi "print-machine-bfi", cl::init(false), cl::Hidden, 7263e17ebfSHiroshi Yamauchi cl::desc("Print the machine block frequency info.")); 7363e17ebfSHiroshi Yamauchi 7463e17ebfSHiroshi Yamauchi // Command line option to specify the name of the function for block frequency 7563e17ebfSHiroshi Yamauchi // dump. Defined in Analysis/BlockFrequencyInfo.cpp. 7663e17ebfSHiroshi Yamauchi extern cl::opt<std::string> PrintBlockFreqFuncName; 7763e17ebfSHiroshi Yamauchi 78fd3f645fSXinliang David Li static GVDAGType getGVDT() { 79fd3f645fSXinliang David Li if (ViewBlockLayoutWithBFI != GVDT_None) 80fd3f645fSXinliang David Li return ViewBlockLayoutWithBFI; 81fd3f645fSXinliang David Li 82fd3f645fSXinliang David Li return ViewMachineBlockFreqPropagationDAG; 83fd3f645fSXinliang David Li } 84fd3f645fSXinliang David Li 8565bbcdfaSMichael Gottesman namespace llvm { 8665bbcdfaSMichael Gottesman 87bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 885df3d890SEugene Zelenko using NodeRef = const MachineBasicBlock *; 895df3d890SEugene Zelenko using ChildIteratorType = MachineBasicBlock::const_succ_iterator; 905df3d890SEugene Zelenko using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>; 9165bbcdfaSMichael Gottesman 9248f814e8STim Shen static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) { 930ac8eb91SDuncan P. N. Exon Smith return &G->getFunction()->front(); 9465bbcdfaSMichael Gottesman } 95748fe483SMichael Gottesman 96f2187ed3STim Shen static ChildIteratorType child_begin(const NodeRef N) { 9765bbcdfaSMichael Gottesman return N->succ_begin(); 9865bbcdfaSMichael Gottesman } 99748fe483SMichael Gottesman 100f2187ed3STim Shen static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); } 101748fe483SMichael Gottesman 10265bbcdfaSMichael Gottesman static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 103b5e0f5acSTim Shen return nodes_iterator(G->getFunction()->begin()); 10465bbcdfaSMichael Gottesman } 105748fe483SMichael Gottesman 10665bbcdfaSMichael Gottesman static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 107b5e0f5acSTim Shen return nodes_iterator(G->getFunction()->end()); 10865bbcdfaSMichael Gottesman } 10965bbcdfaSMichael Gottesman }; 11065bbcdfaSMichael Gottesman 1115df3d890SEugene Zelenko using MBFIDOTGraphTraitsBase = 1125df3d890SEugene Zelenko BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, 1135df3d890SEugene Zelenko MachineBranchProbabilityInfo>; 1145df3d890SEugene Zelenko 11565bbcdfaSMichael Gottesman template <> 116bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *> 11755415f25SXinliang David Li : public MBFIDOTGraphTraitsBase { 1185df3d890SEugene Zelenko const MachineFunction *CurFunc = nullptr; 119fd3f645fSXinliang David Li DenseMap<const MachineBasicBlock *, int> LayoutOrderMap; 12065bbcdfaSMichael Gottesman 1215df3d890SEugene Zelenko explicit DOTGraphTraits(bool isSimple = false) 1225df3d890SEugene Zelenko : MBFIDOTGraphTraitsBase(isSimple) {} 1235df3d890SEugene Zelenko 12465bbcdfaSMichael Gottesman std::string getNodeLabel(const MachineBasicBlock *Node, 12565bbcdfaSMichael Gottesman const MachineBlockFrequencyInfo *Graph) { 126fd3f645fSXinliang David Li int layout_order = -1; 127fd3f645fSXinliang David Li // Attach additional ordering information if 'isSimple' is false. 128fd3f645fSXinliang David Li if (!isSimple()) { 129fd3f645fSXinliang David Li const MachineFunction *F = Node->getParent(); 130fd3f645fSXinliang David Li if (!CurFunc || F != CurFunc) { 131fd3f645fSXinliang David Li if (CurFunc) 132fd3f645fSXinliang David Li LayoutOrderMap.clear(); 133fd3f645fSXinliang David Li 134fd3f645fSXinliang David Li CurFunc = F; 135fd3f645fSXinliang David Li int O = 0; 136fd3f645fSXinliang David Li for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) { 137fd3f645fSXinliang David Li LayoutOrderMap[&*MBI] = O; 138fd3f645fSXinliang David Li } 139fd3f645fSXinliang David Li } 140fd3f645fSXinliang David Li layout_order = LayoutOrderMap[Node]; 141fd3f645fSXinliang David Li } 142fd3f645fSXinliang David Li return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(), 143fd3f645fSXinliang David Li layout_order); 14455415f25SXinliang David Li } 14565bbcdfaSMichael Gottesman 1463e176c77SXinliang David Li std::string getNodeAttributes(const MachineBasicBlock *Node, 1473e176c77SXinliang David Li const MachineBlockFrequencyInfo *Graph) { 1483e176c77SXinliang David Li return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, 1493e176c77SXinliang David Li ViewHotFreqPercent); 1503e176c77SXinliang David Li } 1513e176c77SXinliang David Li 15255415f25SXinliang David Li std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, 15369317f2eSXinliang David Li const MachineBlockFrequencyInfo *MBFI) { 1543e176c77SXinliang David Li return MBFIDOTGraphTraitsBase::getEdgeAttributes( 1553e176c77SXinliang David Li Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); 15669317f2eSXinliang David Li } 15765bbcdfaSMichael Gottesman }; 15865bbcdfaSMichael Gottesman 15965bbcdfaSMichael Gottesman } // end namespace llvm 16065bbcdfaSMichael Gottesman 1611527baabSMatthias Braun INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE, 162875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 163875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 16410be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 1651527baabSMatthias Braun INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE, 166875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 167875ebd5fSJakub Staszak 168875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0; 169875ebd5fSJakub Staszak 170bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() 171bc157084SXinliang David Li : MachineFunctionPass(ID) { 172875ebd5fSJakub Staszak initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 173875ebd5fSJakub Staszak } 174875ebd5fSJakub Staszak 175*75f72f6bSHiroshi Yamauchi MachineBlockFrequencyInfo::MachineBlockFrequencyInfo( 176*75f72f6bSHiroshi Yamauchi MachineFunction &F, 177*75f72f6bSHiroshi Yamauchi MachineBranchProbabilityInfo &MBPI, 178*75f72f6bSHiroshi Yamauchi MachineLoopInfo &MLI) : MachineFunctionPass(ID) { 179*75f72f6bSHiroshi Yamauchi calculate(F, MBPI, MLI); 180*75f72f6bSHiroshi Yamauchi } 181*75f72f6bSHiroshi Yamauchi 1825df3d890SEugene Zelenko MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default; 183875ebd5fSJakub Staszak 184875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 185875ebd5fSJakub Staszak AU.addRequired<MachineBranchProbabilityInfo>(); 18610be9a88SDuncan P. N. Exon Smith AU.addRequired<MachineLoopInfo>(); 187875ebd5fSJakub Staszak AU.setPreservesAll(); 188875ebd5fSJakub Staszak MachineFunctionPass::getAnalysisUsage(AU); 189875ebd5fSJakub Staszak } 190875ebd5fSJakub Staszak 191bbb141c7SAdam Nemet void MachineBlockFrequencyInfo::calculate( 192bbb141c7SAdam Nemet const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, 193bbb141c7SAdam Nemet const MachineLoopInfo &MLI) { 1943dbe1050SDuncan P. N. Exon Smith if (!MBFI) 1953dbe1050SDuncan P. N. Exon Smith MBFI.reset(new ImplType); 1965e67b666SCong Hou MBFI->calculate(F, MBPI, MLI); 19780457ce5SXinliang David Li if (ViewMachineBlockFreqPropagationDAG != GVDT_None && 1988dd5ce97SXinliang David Li (ViewBlockFreqFuncName.empty() || 1998dd5ce97SXinliang David Li F.getName().equals(ViewBlockFreqFuncName))) { 200538d6668SXinliang David Li view("MachineBlockFrequencyDAGS." + F.getName()); 20165bbcdfaSMichael Gottesman } 20263e17ebfSHiroshi Yamauchi if (PrintMachineBlockFreq && 20363e17ebfSHiroshi Yamauchi (PrintBlockFreqFuncName.empty() || 20463e17ebfSHiroshi Yamauchi F.getName().equals(PrintBlockFreqFuncName))) { 20563e17ebfSHiroshi Yamauchi MBFI->print(dbgs()); 20663e17ebfSHiroshi Yamauchi } 207bbb141c7SAdam Nemet } 208bbb141c7SAdam Nemet 209bbb141c7SAdam Nemet bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 210bbb141c7SAdam Nemet MachineBranchProbabilityInfo &MBPI = 211bbb141c7SAdam Nemet getAnalysis<MachineBranchProbabilityInfo>(); 212bbb141c7SAdam Nemet MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 213bbb141c7SAdam Nemet calculate(F, MBPI, MLI); 214875ebd5fSJakub Staszak return false; 215875ebd5fSJakub Staszak } 216875ebd5fSJakub Staszak 2173dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 2183dbe1050SDuncan P. N. Exon Smith 21965bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation 22065bbcdfaSMichael Gottesman /// rendered using dot. 221538d6668SXinliang David Li void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const { 22265bbcdfaSMichael Gottesman // This code is only for debugging. 223538d6668SXinliang David Li ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple); 22465bbcdfaSMichael Gottesman } 22565bbcdfaSMichael Gottesman 226bc157084SXinliang David Li BlockFrequency 227bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { 2283dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getBlockFreq(MBB) : 0; 229875ebd5fSJakub Staszak } 23065bbcdfaSMichael Gottesman 23130c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( 23230c50f3cSXinliang David Li const MachineBasicBlock *MBB) const { 233f1caa283SMatthias Braun const Function &F = MBFI->getFunction()->getFunction(); 234f1caa283SMatthias Braun return MBFI ? MBFI->getBlockProfileCount(F, MBB) : None; 23530c50f3cSXinliang David Li } 23630c50f3cSXinliang David Li 237f801575fSSean Silva Optional<uint64_t> 238f801575fSSean Silva MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 239f1caa283SMatthias Braun const Function &F = MBFI->getFunction()->getFunction(); 240f1caa283SMatthias Braun return MBFI ? MBFI->getProfileCountFromFreq(F, Freq) : None; 241f801575fSSean Silva } 242f801575fSSean Silva 243dce9def3SHiroshi Yamauchi bool 244dce9def3SHiroshi Yamauchi MachineBlockFrequencyInfo::isIrrLoopHeader(const MachineBasicBlock *MBB) { 245dce9def3SHiroshi Yamauchi assert(MBFI && "Expected analysis to be available"); 246dce9def3SHiroshi Yamauchi return MBFI->isIrrLoopHeader(MBB); 247dce9def3SHiroshi Yamauchi } 248dce9def3SHiroshi Yamauchi 249936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 25010be9a88SDuncan P. N. Exon Smith return MBFI ? MBFI->getFunction() : nullptr; 25165bbcdfaSMichael Gottesman } 252fd5c4b2cSMichael Gottesman 2533264fdd3SXinliang David Li const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { 2543264fdd3SXinliang David Li return MBFI ? &MBFI->getBPI() : nullptr; 2553264fdd3SXinliang David Li } 2563264fdd3SXinliang David Li 257fd5c4b2cSMichael Gottesman raw_ostream & 258fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 259fd5c4b2cSMichael Gottesman const BlockFrequency Freq) const { 2603dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 261fd5c4b2cSMichael Gottesman } 262fd5c4b2cSMichael Gottesman 263fd5c4b2cSMichael Gottesman raw_ostream & 264fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 265fd5c4b2cSMichael Gottesman const MachineBasicBlock *MBB) const { 2663dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 267fd5c4b2cSMichael Gottesman } 268fd5c4b2cSMichael Gottesman 2695e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 2703dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getEntryFreq() : 0; 271fd5c4b2cSMichael Gottesman } 272