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" 2365bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h" 24875ebd5fSJakub Staszak 25875ebd5fSJakub Staszak using namespace llvm; 26875ebd5fSJakub Staszak 271b9dde08SChandler Carruth #define DEBUG_TYPE "block-freq" 281b9dde08SChandler Carruth 2965bbcdfaSMichael Gottesman #ifndef NDEBUG 30*bc157084SXinliang David Li enum GVDAGType { GVDT_None, GVDT_Fraction, GVDT_Integer }; 3165bbcdfaSMichael Gottesman 32*bc157084SXinliang David Li static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG( 33*bc157084SXinliang 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."), 36*bc157084SXinliang David Li cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 37*bc157084SXinliang David Li clEnumValN(GVDT_Fraction, "fraction", 38*bc157084SXinliang David Li "display a graph using the " 3965bbcdfaSMichael Gottesman "fractional block frequency representation."), 40*bc157084SXinliang David Li clEnumValN(GVDT_Integer, "integer", 41*bc157084SXinliang David Li "display a graph using the raw " 4265bbcdfaSMichael Gottesman "integer fractional block frequency representation."), 4365bbcdfaSMichael Gottesman clEnumValEnd)); 4465bbcdfaSMichael Gottesman 4565bbcdfaSMichael Gottesman namespace llvm { 4665bbcdfaSMichael Gottesman 47*bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 4865bbcdfaSMichael Gottesman typedef const MachineBasicBlock NodeType; 4965bbcdfaSMichael Gottesman typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 5065bbcdfaSMichael Gottesman typedef MachineFunction::const_iterator nodes_iterator; 5165bbcdfaSMichael Gottesman 52*bc157084SXinliang David Li static inline const NodeType * 53*bc157084SXinliang David Li getEntryNode(const MachineBlockFrequencyInfo *G) { 540ac8eb91SDuncan P. N. Exon Smith return &G->getFunction()->front(); 5565bbcdfaSMichael Gottesman } 56748fe483SMichael Gottesman 5765bbcdfaSMichael Gottesman static ChildIteratorType child_begin(const NodeType *N) { 5865bbcdfaSMichael Gottesman return N->succ_begin(); 5965bbcdfaSMichael Gottesman } 60748fe483SMichael Gottesman 6165bbcdfaSMichael Gottesman static ChildIteratorType child_end(const NodeType *N) { 6265bbcdfaSMichael Gottesman return N->succ_end(); 6365bbcdfaSMichael Gottesman } 64748fe483SMichael Gottesman 6565bbcdfaSMichael Gottesman static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 6665bbcdfaSMichael Gottesman return G->getFunction()->begin(); 6765bbcdfaSMichael Gottesman } 68748fe483SMichael Gottesman 6965bbcdfaSMichael Gottesman static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 7065bbcdfaSMichael Gottesman return G->getFunction()->end(); 7165bbcdfaSMichael Gottesman } 7265bbcdfaSMichael Gottesman }; 7365bbcdfaSMichael Gottesman 7465bbcdfaSMichael Gottesman template <> 75*bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *> 76*bc157084SXinliang David Li : public DefaultDOTGraphTraits { 77*bc157084SXinliang David Li explicit DOTGraphTraits(bool isSimple = false) 78*bc157084SXinliang David Li : DefaultDOTGraphTraits(isSimple) {} 7965bbcdfaSMichael Gottesman 8065bbcdfaSMichael Gottesman static std::string getGraphName(const MachineBlockFrequencyInfo *G) { 8165bbcdfaSMichael Gottesman return G->getFunction()->getName(); 8265bbcdfaSMichael Gottesman } 8365bbcdfaSMichael Gottesman 8465bbcdfaSMichael Gottesman std::string getNodeLabel(const MachineBasicBlock *Node, 8565bbcdfaSMichael Gottesman const MachineBlockFrequencyInfo *Graph) { 86e69170a1SAlp Toker std::string Result; 87e69170a1SAlp Toker raw_string_ostream OS(Result); 8865bbcdfaSMichael Gottesman 89e69170a1SAlp Toker OS << Node->getName().str() << ":"; 9065bbcdfaSMichael Gottesman switch (ViewMachineBlockFreqPropagationDAG) { 9165bbcdfaSMichael Gottesman case GVDT_Fraction: 92b0c1ed8fSMichael Gottesman Graph->printBlockFreq(OS, Node); 9365bbcdfaSMichael Gottesman break; 9465bbcdfaSMichael Gottesman case GVDT_Integer: 9565bbcdfaSMichael Gottesman OS << Graph->getBlockFreq(Node).getFrequency(); 9665bbcdfaSMichael Gottesman break; 9765bbcdfaSMichael Gottesman case GVDT_None: 9865bbcdfaSMichael Gottesman llvm_unreachable("If we are not supposed to render a graph we should " 9965bbcdfaSMichael Gottesman "never reach this point."); 10065bbcdfaSMichael Gottesman } 10165bbcdfaSMichael Gottesman 102e69170a1SAlp Toker return Result; 10365bbcdfaSMichael Gottesman } 10465bbcdfaSMichael Gottesman }; 10565bbcdfaSMichael Gottesman 10665bbcdfaSMichael Gottesman } // end namespace llvm 10765bbcdfaSMichael Gottesman #endif 10865bbcdfaSMichael Gottesman 109875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq", 110875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 111875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 11210be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 113875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq", 114875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 115875ebd5fSJakub Staszak 116875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0; 117875ebd5fSJakub Staszak 118*bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() 119*bc157084SXinliang David Li : MachineFunctionPass(ID) { 120875ebd5fSJakub Staszak initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 121875ebd5fSJakub Staszak } 122875ebd5fSJakub Staszak 1233dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {} 124875ebd5fSJakub Staszak 125875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 126875ebd5fSJakub Staszak AU.addRequired<MachineBranchProbabilityInfo>(); 12710be9a88SDuncan P. N. Exon Smith AU.addRequired<MachineLoopInfo>(); 128875ebd5fSJakub Staszak AU.setPreservesAll(); 129875ebd5fSJakub Staszak MachineFunctionPass::getAnalysisUsage(AU); 130875ebd5fSJakub Staszak } 131875ebd5fSJakub Staszak 132875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 133748fe483SMichael Gottesman MachineBranchProbabilityInfo &MBPI = 134748fe483SMichael Gottesman getAnalysis<MachineBranchProbabilityInfo>(); 13510be9a88SDuncan P. N. Exon Smith MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 1363dbe1050SDuncan P. N. Exon Smith if (!MBFI) 1373dbe1050SDuncan P. N. Exon Smith MBFI.reset(new ImplType); 1385e67b666SCong Hou MBFI->calculate(F, MBPI, MLI); 13965bbcdfaSMichael Gottesman #ifndef NDEBUG 14065bbcdfaSMichael Gottesman if (ViewMachineBlockFreqPropagationDAG != GVDT_None) { 14165bbcdfaSMichael Gottesman view(); 14265bbcdfaSMichael Gottesman } 14365bbcdfaSMichael Gottesman #endif 144875ebd5fSJakub Staszak return false; 145875ebd5fSJakub Staszak } 146875ebd5fSJakub Staszak 1473dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 1483dbe1050SDuncan P. N. Exon Smith 14965bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation 15065bbcdfaSMichael Gottesman /// rendered using dot. 15165bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const { 15265bbcdfaSMichael Gottesman // This code is only for debugging. 15365bbcdfaSMichael Gottesman #ifndef NDEBUG 15465bbcdfaSMichael Gottesman ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), 15565bbcdfaSMichael Gottesman "MachineBlockFrequencyDAGs"); 15665bbcdfaSMichael Gottesman #else 157748fe483SMichael Gottesman errs() << "MachineBlockFrequencyInfo::view is only available in debug builds " 158748fe483SMichael Gottesman "on systems with Graphviz or gv!\n"; 15965bbcdfaSMichael Gottesman #endif // NDEBUG 16065bbcdfaSMichael Gottesman } 16165bbcdfaSMichael Gottesman 162*bc157084SXinliang David Li BlockFrequency 163*bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { 1643dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getBlockFreq(MBB) : 0; 165875ebd5fSJakub Staszak } 16665bbcdfaSMichael Gottesman 167936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 16810be9a88SDuncan P. N. Exon Smith return MBFI ? MBFI->getFunction() : nullptr; 16965bbcdfaSMichael Gottesman } 170fd5c4b2cSMichael Gottesman 171fd5c4b2cSMichael Gottesman raw_ostream & 172fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 173fd5c4b2cSMichael Gottesman const BlockFrequency Freq) const { 1743dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 175fd5c4b2cSMichael Gottesman } 176fd5c4b2cSMichael Gottesman 177fd5c4b2cSMichael Gottesman raw_ostream & 178fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 179fd5c4b2cSMichael Gottesman const MachineBasicBlock *MBB) const { 1803dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 181fd5c4b2cSMichael Gottesman } 182fd5c4b2cSMichael Gottesman 1835e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 1843dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getEntryFreq() : 0; 185fd5c4b2cSMichael Gottesman } 186