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 #ifndef NDEBUG 3265bbcdfaSMichael Gottesman 33bc157084SXinliang David Li static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG( 34bc157084SXinliang David Li "view-machine-block-freq-propagation-dags", cl::Hidden, 3565bbcdfaSMichael Gottesman cl::desc("Pop up a window to show a dag displaying how machine block " 36748fe483SMichael Gottesman "frequencies propagate through the CFG."), 37bc157084SXinliang David Li cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), 38bc157084SXinliang David Li clEnumValN(GVDT_Fraction, "fraction", 39bc157084SXinliang David Li "display a graph using the " 4065bbcdfaSMichael Gottesman "fractional block frequency representation."), 41bc157084SXinliang David Li clEnumValN(GVDT_Integer, "integer", 42bc157084SXinliang David Li "display a graph using the raw " 4365bbcdfaSMichael Gottesman "integer fractional block frequency representation."), 4430c50f3cSXinliang David Li clEnumValN(GVDT_Count, "count", "display a graph using the real " 4530c50f3cSXinliang David Li "profile count if available."), 4630c50f3cSXinliang David Li 4765bbcdfaSMichael Gottesman clEnumValEnd)); 4865bbcdfaSMichael Gottesman 498dd5ce97SXinliang David Li extern cl::opt<std::string> ViewBlockFreqFuncName; 502d531580SSimon Pilgrim extern cl::opt<unsigned> ViewHotFreqPercent; 5180457ce5SXinliang David Li 5265bbcdfaSMichael Gottesman namespace llvm { 5365bbcdfaSMichael Gottesman 54bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> { 5565bbcdfaSMichael Gottesman typedef const MachineBasicBlock NodeType; 5665bbcdfaSMichael Gottesman typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 5765bbcdfaSMichael Gottesman typedef MachineFunction::const_iterator nodes_iterator; 5865bbcdfaSMichael Gottesman 59bc157084SXinliang David Li static inline const NodeType * 60bc157084SXinliang David Li getEntryNode(const MachineBlockFrequencyInfo *G) { 610ac8eb91SDuncan P. N. Exon Smith return &G->getFunction()->front(); 6265bbcdfaSMichael Gottesman } 63748fe483SMichael Gottesman 6465bbcdfaSMichael Gottesman static ChildIteratorType child_begin(const NodeType *N) { 6565bbcdfaSMichael Gottesman return N->succ_begin(); 6665bbcdfaSMichael Gottesman } 67748fe483SMichael Gottesman 6865bbcdfaSMichael Gottesman static ChildIteratorType child_end(const NodeType *N) { 6965bbcdfaSMichael Gottesman return N->succ_end(); 7065bbcdfaSMichael Gottesman } 71748fe483SMichael Gottesman 7265bbcdfaSMichael Gottesman static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 7365bbcdfaSMichael Gottesman return G->getFunction()->begin(); 7465bbcdfaSMichael Gottesman } 75748fe483SMichael Gottesman 7665bbcdfaSMichael Gottesman static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 7765bbcdfaSMichael Gottesman return G->getFunction()->end(); 7865bbcdfaSMichael Gottesman } 7965bbcdfaSMichael Gottesman }; 8065bbcdfaSMichael Gottesman 8155415f25SXinliang David Li typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, 8255415f25SXinliang David Li MachineBranchProbabilityInfo> 8355415f25SXinliang David Li MBFIDOTGraphTraitsBase; 8465bbcdfaSMichael Gottesman template <> 85bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *> 8655415f25SXinliang David Li : public MBFIDOTGraphTraitsBase { 87bc157084SXinliang David Li explicit DOTGraphTraits(bool isSimple = false) 8855415f25SXinliang David Li : MBFIDOTGraphTraitsBase(isSimple) {} 8965bbcdfaSMichael Gottesman 9065bbcdfaSMichael Gottesman std::string getNodeLabel(const MachineBasicBlock *Node, 9165bbcdfaSMichael Gottesman const MachineBlockFrequencyInfo *Graph) { 9255415f25SXinliang David Li return MBFIDOTGraphTraitsBase::getNodeLabel( 9355415f25SXinliang David Li Node, Graph, ViewMachineBlockFreqPropagationDAG); 9455415f25SXinliang David Li } 9565bbcdfaSMichael Gottesman 963e176c77SXinliang David Li std::string getNodeAttributes(const MachineBasicBlock *Node, 973e176c77SXinliang David Li const MachineBlockFrequencyInfo *Graph) { 983e176c77SXinliang David Li return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, 993e176c77SXinliang David Li ViewHotFreqPercent); 1003e176c77SXinliang David Li } 1013e176c77SXinliang David Li 10255415f25SXinliang David Li std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, 10369317f2eSXinliang David Li const MachineBlockFrequencyInfo *MBFI) { 1043e176c77SXinliang David Li return MBFIDOTGraphTraitsBase::getEdgeAttributes( 1053e176c77SXinliang David Li Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); 10669317f2eSXinliang David Li } 10765bbcdfaSMichael Gottesman }; 10865bbcdfaSMichael Gottesman 10965bbcdfaSMichael Gottesman } // end namespace llvm 11065bbcdfaSMichael Gottesman #endif 11165bbcdfaSMichael Gottesman 112875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq", 113875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 114875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 11510be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 116875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq", 117875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 118875ebd5fSJakub Staszak 119875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0; 120875ebd5fSJakub Staszak 121bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() 122bc157084SXinliang David Li : MachineFunctionPass(ID) { 123875ebd5fSJakub Staszak initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 124875ebd5fSJakub Staszak } 125875ebd5fSJakub Staszak 1263dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {} 127875ebd5fSJakub Staszak 128875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 129875ebd5fSJakub Staszak AU.addRequired<MachineBranchProbabilityInfo>(); 13010be9a88SDuncan P. N. Exon Smith AU.addRequired<MachineLoopInfo>(); 131875ebd5fSJakub Staszak AU.setPreservesAll(); 132875ebd5fSJakub Staszak MachineFunctionPass::getAnalysisUsage(AU); 133875ebd5fSJakub Staszak } 134875ebd5fSJakub Staszak 135875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 136748fe483SMichael Gottesman MachineBranchProbabilityInfo &MBPI = 137748fe483SMichael Gottesman getAnalysis<MachineBranchProbabilityInfo>(); 13810be9a88SDuncan P. N. Exon Smith MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 1393dbe1050SDuncan P. N. Exon Smith if (!MBFI) 1403dbe1050SDuncan P. N. Exon Smith MBFI.reset(new ImplType); 1415e67b666SCong Hou MBFI->calculate(F, MBPI, MLI); 14265bbcdfaSMichael Gottesman #ifndef NDEBUG 14380457ce5SXinliang David Li if (ViewMachineBlockFreqPropagationDAG != GVDT_None && 1448dd5ce97SXinliang David Li (ViewBlockFreqFuncName.empty() || 1458dd5ce97SXinliang David Li F.getName().equals(ViewBlockFreqFuncName))) { 14665bbcdfaSMichael Gottesman view(); 14765bbcdfaSMichael Gottesman } 14865bbcdfaSMichael Gottesman #endif 149875ebd5fSJakub Staszak return false; 150875ebd5fSJakub Staszak } 151875ebd5fSJakub Staszak 1523dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 1533dbe1050SDuncan P. N. Exon Smith 15465bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation 15565bbcdfaSMichael Gottesman /// rendered using dot. 15665bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const { 15765bbcdfaSMichael Gottesman // This code is only for debugging. 15865bbcdfaSMichael Gottesman #ifndef NDEBUG 15965bbcdfaSMichael Gottesman ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), 16065bbcdfaSMichael Gottesman "MachineBlockFrequencyDAGs"); 16165bbcdfaSMichael Gottesman #else 162748fe483SMichael Gottesman errs() << "MachineBlockFrequencyInfo::view is only available in debug builds " 163748fe483SMichael Gottesman "on systems with Graphviz or gv!\n"; 16465bbcdfaSMichael Gottesman #endif // NDEBUG 16565bbcdfaSMichael Gottesman } 16665bbcdfaSMichael Gottesman 167bc157084SXinliang David Li BlockFrequency 168bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { 1693dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getBlockFreq(MBB) : 0; 170875ebd5fSJakub Staszak } 17165bbcdfaSMichael Gottesman 17230c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( 17330c50f3cSXinliang David Li const MachineBasicBlock *MBB) const { 17430c50f3cSXinliang David Li const Function *F = MBFI->getFunction()->getFunction(); 17530c50f3cSXinliang David Li return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None; 17630c50f3cSXinliang David Li } 17730c50f3cSXinliang David Li 178*f801575fSSean Silva Optional<uint64_t> 179*f801575fSSean Silva MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { 180*f801575fSSean Silva const Function *F = MBFI->getFunction()->getFunction(); 181*f801575fSSean Silva return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None; 182*f801575fSSean Silva } 183*f801575fSSean Silva 184936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 18510be9a88SDuncan P. N. Exon Smith return MBFI ? MBFI->getFunction() : nullptr; 18665bbcdfaSMichael Gottesman } 187fd5c4b2cSMichael Gottesman 1883264fdd3SXinliang David Li const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { 1893264fdd3SXinliang David Li return MBFI ? &MBFI->getBPI() : nullptr; 1903264fdd3SXinliang David Li } 1913264fdd3SXinliang David Li 192fd5c4b2cSMichael Gottesman raw_ostream & 193fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 194fd5c4b2cSMichael Gottesman const BlockFrequency Freq) const { 1953dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 196fd5c4b2cSMichael Gottesman } 197fd5c4b2cSMichael Gottesman 198fd5c4b2cSMichael Gottesman raw_ostream & 199fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 200fd5c4b2cSMichael Gottesman const MachineBasicBlock *MBB) const { 2013dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 202fd5c4b2cSMichael Gottesman } 203fd5c4b2cSMichael Gottesman 2045e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 2053dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getEntryFreq() : 0; 206fd5c4b2cSMichael Gottesman } 207