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 3065bbcdfaSMichael Gottesman enum GVDAGType { 3165bbcdfaSMichael Gottesman GVDT_None, 3265bbcdfaSMichael Gottesman GVDT_Fraction, 3365bbcdfaSMichael Gottesman GVDT_Integer 3465bbcdfaSMichael Gottesman }; 3565bbcdfaSMichael Gottesman 3665bbcdfaSMichael Gottesman static cl::opt<GVDAGType> 3765bbcdfaSMichael Gottesman ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags", 3865bbcdfaSMichael Gottesman cl::Hidden, 3965bbcdfaSMichael Gottesman cl::desc("Pop up a window to show a dag displaying how machine block " 40748fe483SMichael Gottesman "frequencies propagate through the CFG."), 4165bbcdfaSMichael Gottesman cl::values( 4265bbcdfaSMichael Gottesman clEnumValN(GVDT_None, "none", 4365bbcdfaSMichael Gottesman "do not display graphs."), 4465bbcdfaSMichael Gottesman clEnumValN(GVDT_Fraction, "fraction", "display a graph using the " 4565bbcdfaSMichael Gottesman "fractional block frequency representation."), 4665bbcdfaSMichael Gottesman clEnumValN(GVDT_Integer, "integer", "display a graph using the raw " 4765bbcdfaSMichael Gottesman "integer fractional block frequency representation."), 4865bbcdfaSMichael Gottesman clEnumValEnd)); 4965bbcdfaSMichael Gottesman 5065bbcdfaSMichael Gottesman namespace llvm { 5165bbcdfaSMichael Gottesman 5265bbcdfaSMichael Gottesman template <> 5365bbcdfaSMichael Gottesman struct GraphTraits<MachineBlockFrequencyInfo *> { 5465bbcdfaSMichael Gottesman typedef const MachineBasicBlock NodeType; 5565bbcdfaSMichael Gottesman typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 5665bbcdfaSMichael Gottesman typedef MachineFunction::const_iterator nodes_iterator; 5765bbcdfaSMichael Gottesman 58748fe483SMichael Gottesman static inline 59748fe483SMichael Gottesman const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) { 6065bbcdfaSMichael Gottesman return G->getFunction()->begin(); 6165bbcdfaSMichael Gottesman } 62748fe483SMichael Gottesman 6365bbcdfaSMichael Gottesman static ChildIteratorType child_begin(const NodeType *N) { 6465bbcdfaSMichael Gottesman return N->succ_begin(); 6565bbcdfaSMichael Gottesman } 66748fe483SMichael Gottesman 6765bbcdfaSMichael Gottesman static ChildIteratorType child_end(const NodeType *N) { 6865bbcdfaSMichael Gottesman return N->succ_end(); 6965bbcdfaSMichael Gottesman } 70748fe483SMichael Gottesman 7165bbcdfaSMichael Gottesman static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 7265bbcdfaSMichael Gottesman return G->getFunction()->begin(); 7365bbcdfaSMichael Gottesman } 74748fe483SMichael Gottesman 7565bbcdfaSMichael Gottesman static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 7665bbcdfaSMichael Gottesman return G->getFunction()->end(); 7765bbcdfaSMichael Gottesman } 7865bbcdfaSMichael Gottesman }; 7965bbcdfaSMichael Gottesman 8065bbcdfaSMichael Gottesman template<> 81748fe483SMichael Gottesman struct DOTGraphTraits<MachineBlockFrequencyInfo*> : 82748fe483SMichael Gottesman public DefaultDOTGraphTraits { 8365bbcdfaSMichael Gottesman explicit DOTGraphTraits(bool isSimple=false) : 8465bbcdfaSMichael Gottesman DefaultDOTGraphTraits(isSimple) {} 8565bbcdfaSMichael Gottesman 8665bbcdfaSMichael Gottesman static std::string getGraphName(const MachineBlockFrequencyInfo *G) { 8765bbcdfaSMichael Gottesman return G->getFunction()->getName(); 8865bbcdfaSMichael Gottesman } 8965bbcdfaSMichael Gottesman 9065bbcdfaSMichael Gottesman std::string getNodeLabel(const MachineBasicBlock *Node, 9165bbcdfaSMichael Gottesman const MachineBlockFrequencyInfo *Graph) { 92*e69170a1SAlp Toker std::string Result; 93*e69170a1SAlp Toker raw_string_ostream OS(Result); 9465bbcdfaSMichael Gottesman 95*e69170a1SAlp Toker OS << Node->getName().str() << ":"; 9665bbcdfaSMichael Gottesman switch (ViewMachineBlockFreqPropagationDAG) { 9765bbcdfaSMichael Gottesman case GVDT_Fraction: 98b0c1ed8fSMichael Gottesman Graph->printBlockFreq(OS, Node); 9965bbcdfaSMichael Gottesman break; 10065bbcdfaSMichael Gottesman case GVDT_Integer: 10165bbcdfaSMichael Gottesman OS << Graph->getBlockFreq(Node).getFrequency(); 10265bbcdfaSMichael Gottesman break; 10365bbcdfaSMichael Gottesman case GVDT_None: 10465bbcdfaSMichael Gottesman llvm_unreachable("If we are not supposed to render a graph we should " 10565bbcdfaSMichael Gottesman "never reach this point."); 10665bbcdfaSMichael Gottesman } 10765bbcdfaSMichael Gottesman 108*e69170a1SAlp Toker return Result; 10965bbcdfaSMichael Gottesman } 11065bbcdfaSMichael Gottesman }; 11165bbcdfaSMichael Gottesman 11265bbcdfaSMichael Gottesman 11365bbcdfaSMichael Gottesman } // end namespace llvm 11465bbcdfaSMichael Gottesman #endif 11565bbcdfaSMichael Gottesman 116875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq", 117875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 118875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 11910be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 120875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq", 121875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 122875ebd5fSJakub Staszak 123875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0; 124875ebd5fSJakub Staszak 125875ebd5fSJakub Staszak 126748fe483SMichael Gottesman MachineBlockFrequencyInfo:: 127748fe483SMichael Gottesman MachineBlockFrequencyInfo() :MachineFunctionPass(ID) { 128875ebd5fSJakub Staszak initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 129875ebd5fSJakub Staszak } 130875ebd5fSJakub Staszak 1313dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {} 132875ebd5fSJakub Staszak 133875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 134875ebd5fSJakub Staszak AU.addRequired<MachineBranchProbabilityInfo>(); 13510be9a88SDuncan P. N. Exon Smith AU.addRequired<MachineLoopInfo>(); 136875ebd5fSJakub Staszak AU.setPreservesAll(); 137875ebd5fSJakub Staszak MachineFunctionPass::getAnalysisUsage(AU); 138875ebd5fSJakub Staszak } 139875ebd5fSJakub Staszak 140875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 141748fe483SMichael Gottesman MachineBranchProbabilityInfo &MBPI = 142748fe483SMichael Gottesman getAnalysis<MachineBranchProbabilityInfo>(); 14310be9a88SDuncan P. N. Exon Smith MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 1443dbe1050SDuncan P. N. Exon Smith if (!MBFI) 1453dbe1050SDuncan P. N. Exon Smith MBFI.reset(new ImplType); 14610be9a88SDuncan P. N. Exon Smith MBFI->doFunction(&F, &MBPI, &MLI); 14765bbcdfaSMichael Gottesman #ifndef NDEBUG 14865bbcdfaSMichael Gottesman if (ViewMachineBlockFreqPropagationDAG != GVDT_None) { 14965bbcdfaSMichael Gottesman view(); 15065bbcdfaSMichael Gottesman } 15165bbcdfaSMichael Gottesman #endif 152875ebd5fSJakub Staszak return false; 153875ebd5fSJakub Staszak } 154875ebd5fSJakub Staszak 1553dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } 1563dbe1050SDuncan P. N. Exon Smith 15765bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation 15865bbcdfaSMichael Gottesman /// rendered using dot. 15965bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const { 16065bbcdfaSMichael Gottesman // This code is only for debugging. 16165bbcdfaSMichael Gottesman #ifndef NDEBUG 16265bbcdfaSMichael Gottesman ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), 16365bbcdfaSMichael Gottesman "MachineBlockFrequencyDAGs"); 16465bbcdfaSMichael Gottesman #else 165748fe483SMichael Gottesman errs() << "MachineBlockFrequencyInfo::view is only available in debug builds " 166748fe483SMichael Gottesman "on systems with Graphviz or gv!\n"; 16765bbcdfaSMichael Gottesman #endif // NDEBUG 16865bbcdfaSMichael Gottesman } 16965bbcdfaSMichael Gottesman 170a60d130fSJakub Staszak BlockFrequency MachineBlockFrequencyInfo:: 17196f8c551SJakub Staszak getBlockFreq(const MachineBasicBlock *MBB) const { 1723dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getBlockFreq(MBB) : 0; 173875ebd5fSJakub Staszak } 17465bbcdfaSMichael Gottesman 175936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 17610be9a88SDuncan P. N. Exon Smith return MBFI ? MBFI->getFunction() : nullptr; 17765bbcdfaSMichael Gottesman } 178fd5c4b2cSMichael Gottesman 179fd5c4b2cSMichael Gottesman raw_ostream & 180fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 181fd5c4b2cSMichael Gottesman const BlockFrequency Freq) const { 1823dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; 183fd5c4b2cSMichael Gottesman } 184fd5c4b2cSMichael Gottesman 185fd5c4b2cSMichael Gottesman raw_ostream & 186fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 187fd5c4b2cSMichael Gottesman const MachineBasicBlock *MBB) const { 1883dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; 189fd5c4b2cSMichael Gottesman } 190fd5c4b2cSMichael Gottesman 1915e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 1923dbe1050SDuncan P. N. Exon Smith return MBFI ? MBFI->getEntryFreq() : 0; 193fd5c4b2cSMichael Gottesman } 194