1748fe483SMichael Gottesman //====------ 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" 15ed0881b2SChandler Carruth #include "llvm/Analysis/BlockFrequencyImpl.h" 16875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" 17ed0881b2SChandler Carruth #include "llvm/CodeGen/Passes.h" 18ed0881b2SChandler Carruth #include "llvm/InitializePasses.h" 1965bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h" 2065bbcdfaSMichael Gottesman #include "llvm/Support/Debug.h" 2165bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h" 22875ebd5fSJakub Staszak 23875ebd5fSJakub Staszak using namespace llvm; 24875ebd5fSJakub Staszak 2565bbcdfaSMichael Gottesman #ifndef NDEBUG 2665bbcdfaSMichael Gottesman enum GVDAGType { 2765bbcdfaSMichael Gottesman GVDT_None, 2865bbcdfaSMichael Gottesman GVDT_Fraction, 2965bbcdfaSMichael Gottesman GVDT_Integer 3065bbcdfaSMichael Gottesman }; 3165bbcdfaSMichael Gottesman 3265bbcdfaSMichael Gottesman static cl::opt<GVDAGType> 3365bbcdfaSMichael Gottesman ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags", 3465bbcdfaSMichael Gottesman 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."), 3765bbcdfaSMichael Gottesman cl::values( 3865bbcdfaSMichael Gottesman clEnumValN(GVDT_None, "none", 3965bbcdfaSMichael Gottesman "do not display graphs."), 4065bbcdfaSMichael Gottesman clEnumValN(GVDT_Fraction, "fraction", "display a graph using the " 4165bbcdfaSMichael Gottesman "fractional block frequency representation."), 4265bbcdfaSMichael Gottesman clEnumValN(GVDT_Integer, "integer", "display a graph using the raw " 4365bbcdfaSMichael Gottesman "integer fractional block frequency representation."), 4465bbcdfaSMichael Gottesman clEnumValEnd)); 4565bbcdfaSMichael Gottesman 4665bbcdfaSMichael Gottesman namespace llvm { 4765bbcdfaSMichael Gottesman 4865bbcdfaSMichael Gottesman template <> 4965bbcdfaSMichael Gottesman struct GraphTraits<MachineBlockFrequencyInfo *> { 5065bbcdfaSMichael Gottesman typedef const MachineBasicBlock NodeType; 5165bbcdfaSMichael Gottesman typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 5265bbcdfaSMichael Gottesman typedef MachineFunction::const_iterator nodes_iterator; 5365bbcdfaSMichael Gottesman 54748fe483SMichael Gottesman static inline 55748fe483SMichael Gottesman const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) { 5665bbcdfaSMichael Gottesman return G->getFunction()->begin(); 5765bbcdfaSMichael Gottesman } 58748fe483SMichael Gottesman 5965bbcdfaSMichael Gottesman static ChildIteratorType child_begin(const NodeType *N) { 6065bbcdfaSMichael Gottesman return N->succ_begin(); 6165bbcdfaSMichael Gottesman } 62748fe483SMichael Gottesman 6365bbcdfaSMichael Gottesman static ChildIteratorType child_end(const NodeType *N) { 6465bbcdfaSMichael Gottesman return N->succ_end(); 6565bbcdfaSMichael Gottesman } 66748fe483SMichael Gottesman 6765bbcdfaSMichael Gottesman static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 6865bbcdfaSMichael Gottesman return G->getFunction()->begin(); 6965bbcdfaSMichael Gottesman } 70748fe483SMichael Gottesman 7165bbcdfaSMichael Gottesman static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 7265bbcdfaSMichael Gottesman return G->getFunction()->end(); 7365bbcdfaSMichael Gottesman } 7465bbcdfaSMichael Gottesman }; 7565bbcdfaSMichael Gottesman 7665bbcdfaSMichael Gottesman template<> 77748fe483SMichael Gottesman struct DOTGraphTraits<MachineBlockFrequencyInfo*> : 78748fe483SMichael Gottesman public DefaultDOTGraphTraits { 7965bbcdfaSMichael Gottesman explicit DOTGraphTraits(bool isSimple=false) : 8065bbcdfaSMichael Gottesman DefaultDOTGraphTraits(isSimple) {} 8165bbcdfaSMichael Gottesman 8265bbcdfaSMichael Gottesman static std::string getGraphName(const MachineBlockFrequencyInfo *G) { 8365bbcdfaSMichael Gottesman return G->getFunction()->getName(); 8465bbcdfaSMichael Gottesman } 8565bbcdfaSMichael Gottesman 8665bbcdfaSMichael Gottesman std::string getNodeLabel(const MachineBasicBlock *Node, 8765bbcdfaSMichael Gottesman const MachineBlockFrequencyInfo *Graph) { 8865bbcdfaSMichael Gottesman std::string Result; 8965bbcdfaSMichael Gottesman raw_string_ostream OS(Result); 9065bbcdfaSMichael Gottesman 9165bbcdfaSMichael Gottesman OS << Node->getName().str() << ":"; 9265bbcdfaSMichael Gottesman switch (ViewMachineBlockFreqPropagationDAG) { 9365bbcdfaSMichael Gottesman case GVDT_Fraction: 94b0c1ed8fSMichael Gottesman Graph->printBlockFreq(OS, Node); 9565bbcdfaSMichael Gottesman break; 9665bbcdfaSMichael Gottesman case GVDT_Integer: 9765bbcdfaSMichael Gottesman OS << Graph->getBlockFreq(Node).getFrequency(); 9865bbcdfaSMichael Gottesman break; 9965bbcdfaSMichael Gottesman case GVDT_None: 10065bbcdfaSMichael Gottesman llvm_unreachable("If we are not supposed to render a graph we should " 10165bbcdfaSMichael Gottesman "never reach this point."); 10265bbcdfaSMichael Gottesman } 10365bbcdfaSMichael Gottesman 10465bbcdfaSMichael Gottesman return Result; 10565bbcdfaSMichael Gottesman } 10665bbcdfaSMichael Gottesman }; 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) 115875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq", 116875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 117875ebd5fSJakub Staszak 118875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0; 119875ebd5fSJakub Staszak 120875ebd5fSJakub Staszak 121748fe483SMichael Gottesman MachineBlockFrequencyInfo:: 122748fe483SMichael Gottesman MachineBlockFrequencyInfo() :MachineFunctionPass(ID) { 123875ebd5fSJakub Staszak initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 124875ebd5fSJakub Staszak MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction, 125875ebd5fSJakub Staszak MachineBranchProbabilityInfo>(); 126875ebd5fSJakub Staszak } 127875ebd5fSJakub Staszak 128875ebd5fSJakub Staszak MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() { 129875ebd5fSJakub Staszak delete MBFI; 130875ebd5fSJakub Staszak } 131875ebd5fSJakub Staszak 132875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 133875ebd5fSJakub Staszak AU.addRequired<MachineBranchProbabilityInfo>(); 134875ebd5fSJakub Staszak AU.setPreservesAll(); 135875ebd5fSJakub Staszak MachineFunctionPass::getAnalysisUsage(AU); 136875ebd5fSJakub Staszak } 137875ebd5fSJakub Staszak 138875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 139748fe483SMichael Gottesman MachineBranchProbabilityInfo &MBPI = 140748fe483SMichael Gottesman getAnalysis<MachineBranchProbabilityInfo>(); 141875ebd5fSJakub Staszak MBFI->doFunction(&F, &MBPI); 14265bbcdfaSMichael Gottesman #ifndef NDEBUG 14365bbcdfaSMichael Gottesman if (ViewMachineBlockFreqPropagationDAG != GVDT_None) { 14465bbcdfaSMichael Gottesman view(); 14565bbcdfaSMichael Gottesman } 14665bbcdfaSMichael Gottesman #endif 147875ebd5fSJakub Staszak return false; 148875ebd5fSJakub Staszak } 149875ebd5fSJakub Staszak 15065bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation 15165bbcdfaSMichael Gottesman /// rendered using dot. 15265bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const { 15365bbcdfaSMichael Gottesman // This code is only for debugging. 15465bbcdfaSMichael Gottesman #ifndef NDEBUG 15565bbcdfaSMichael Gottesman ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), 15665bbcdfaSMichael Gottesman "MachineBlockFrequencyDAGs"); 15765bbcdfaSMichael Gottesman #else 158748fe483SMichael Gottesman errs() << "MachineBlockFrequencyInfo::view is only available in debug builds " 159748fe483SMichael Gottesman "on systems with Graphviz or gv!\n"; 16065bbcdfaSMichael Gottesman #endif // NDEBUG 16165bbcdfaSMichael Gottesman } 16265bbcdfaSMichael Gottesman 163a60d130fSJakub Staszak BlockFrequency MachineBlockFrequencyInfo:: 16496f8c551SJakub Staszak getBlockFreq(const MachineBasicBlock *MBB) const { 165875ebd5fSJakub Staszak return MBFI->getBlockFreq(MBB); 166875ebd5fSJakub Staszak } 16765bbcdfaSMichael Gottesman 16865bbcdfaSMichael Gottesman MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 16965bbcdfaSMichael Gottesman return MBFI->Fn; 17065bbcdfaSMichael Gottesman } 171fd5c4b2cSMichael Gottesman 172fd5c4b2cSMichael Gottesman raw_ostream & 173fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 174fd5c4b2cSMichael Gottesman const BlockFrequency Freq) const { 175fd5c4b2cSMichael Gottesman return MBFI->printBlockFreq(OS, Freq); 176fd5c4b2cSMichael Gottesman } 177fd5c4b2cSMichael Gottesman 178fd5c4b2cSMichael Gottesman raw_ostream & 179fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, 180fd5c4b2cSMichael Gottesman const MachineBasicBlock *MBB) const { 181fd5c4b2cSMichael Gottesman return MBFI->printBlockFreq(OS, MBB); 182fd5c4b2cSMichael Gottesman } 183fd5c4b2cSMichael Gottesman 184*5e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { 185*5e985ee5SMichael Gottesman return MBFI->getEntryFreq(); 186fd5c4b2cSMichael Gottesman } 187