1875ebd5fSJakub Staszak //====----- MachineBlockFrequencyInfo.cpp - Machine Block 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" 19*65bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h" 20*65bbcdfaSMichael Gottesman #include "llvm/Support/Debug.h" 21*65bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h" 22875ebd5fSJakub Staszak 23875ebd5fSJakub Staszak using namespace llvm; 24875ebd5fSJakub Staszak 25*65bbcdfaSMichael Gottesman #ifndef NDEBUG 26*65bbcdfaSMichael Gottesman enum GVDAGType { 27*65bbcdfaSMichael Gottesman GVDT_None, 28*65bbcdfaSMichael Gottesman GVDT_Fraction, 29*65bbcdfaSMichael Gottesman GVDT_Integer 30*65bbcdfaSMichael Gottesman }; 31*65bbcdfaSMichael Gottesman 32*65bbcdfaSMichael Gottesman static cl::opt<GVDAGType> 33*65bbcdfaSMichael Gottesman ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags", 34*65bbcdfaSMichael Gottesman cl::Hidden, 35*65bbcdfaSMichael Gottesman cl::desc("Pop up a window to show a dag displaying how machine block " 36*65bbcdfaSMichael Gottesman "frequencies propgate through the CFG."), 37*65bbcdfaSMichael Gottesman cl::values( 38*65bbcdfaSMichael Gottesman clEnumValN(GVDT_None, "none", 39*65bbcdfaSMichael Gottesman "do not display graphs."), 40*65bbcdfaSMichael Gottesman clEnumValN(GVDT_Fraction, "fraction", "display a graph using the " 41*65bbcdfaSMichael Gottesman "fractional block frequency representation."), 42*65bbcdfaSMichael Gottesman clEnumValN(GVDT_Integer, "integer", "display a graph using the raw " 43*65bbcdfaSMichael Gottesman "integer fractional block frequency representation."), 44*65bbcdfaSMichael Gottesman clEnumValEnd)); 45*65bbcdfaSMichael Gottesman 46*65bbcdfaSMichael Gottesman namespace llvm { 47*65bbcdfaSMichael Gottesman 48*65bbcdfaSMichael Gottesman template <> 49*65bbcdfaSMichael Gottesman struct GraphTraits<MachineBlockFrequencyInfo *> { 50*65bbcdfaSMichael Gottesman typedef const MachineBasicBlock NodeType; 51*65bbcdfaSMichael Gottesman typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 52*65bbcdfaSMichael Gottesman typedef MachineFunction::const_iterator nodes_iterator; 53*65bbcdfaSMichael Gottesman 54*65bbcdfaSMichael Gottesman static inline const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) { 55*65bbcdfaSMichael Gottesman return G->getFunction()->begin(); 56*65bbcdfaSMichael Gottesman } 57*65bbcdfaSMichael Gottesman static ChildIteratorType child_begin(const NodeType *N) { 58*65bbcdfaSMichael Gottesman return N->succ_begin(); 59*65bbcdfaSMichael Gottesman } 60*65bbcdfaSMichael Gottesman static ChildIteratorType child_end(const NodeType *N) { 61*65bbcdfaSMichael Gottesman return N->succ_end(); 62*65bbcdfaSMichael Gottesman } 63*65bbcdfaSMichael Gottesman static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { 64*65bbcdfaSMichael Gottesman return G->getFunction()->begin(); 65*65bbcdfaSMichael Gottesman } 66*65bbcdfaSMichael Gottesman static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { 67*65bbcdfaSMichael Gottesman return G->getFunction()->end(); 68*65bbcdfaSMichael Gottesman } 69*65bbcdfaSMichael Gottesman }; 70*65bbcdfaSMichael Gottesman 71*65bbcdfaSMichael Gottesman template<> 72*65bbcdfaSMichael Gottesman struct DOTGraphTraits<MachineBlockFrequencyInfo*> : public DefaultDOTGraphTraits { 73*65bbcdfaSMichael Gottesman explicit DOTGraphTraits(bool isSimple=false) : 74*65bbcdfaSMichael Gottesman DefaultDOTGraphTraits(isSimple) {} 75*65bbcdfaSMichael Gottesman 76*65bbcdfaSMichael Gottesman static std::string getGraphName(const MachineBlockFrequencyInfo *G) { 77*65bbcdfaSMichael Gottesman return G->getFunction()->getName(); 78*65bbcdfaSMichael Gottesman } 79*65bbcdfaSMichael Gottesman 80*65bbcdfaSMichael Gottesman std::string getNodeLabel(const MachineBasicBlock *Node, 81*65bbcdfaSMichael Gottesman const MachineBlockFrequencyInfo *Graph) { 82*65bbcdfaSMichael Gottesman std::string Result; 83*65bbcdfaSMichael Gottesman raw_string_ostream OS(Result); 84*65bbcdfaSMichael Gottesman 85*65bbcdfaSMichael Gottesman OS << Node->getName().str() << ":"; 86*65bbcdfaSMichael Gottesman switch (ViewMachineBlockFreqPropagationDAG) { 87*65bbcdfaSMichael Gottesman case GVDT_Fraction: 88*65bbcdfaSMichael Gottesman Graph->getBlockFreq(Node).print(OS); 89*65bbcdfaSMichael Gottesman break; 90*65bbcdfaSMichael Gottesman case GVDT_Integer: 91*65bbcdfaSMichael Gottesman OS << Graph->getBlockFreq(Node).getFrequency(); 92*65bbcdfaSMichael Gottesman break; 93*65bbcdfaSMichael Gottesman case GVDT_None: 94*65bbcdfaSMichael Gottesman llvm_unreachable("If we are not supposed to render a graph we should " 95*65bbcdfaSMichael Gottesman "never reach this point."); 96*65bbcdfaSMichael Gottesman } 97*65bbcdfaSMichael Gottesman 98*65bbcdfaSMichael Gottesman return Result; 99*65bbcdfaSMichael Gottesman } 100*65bbcdfaSMichael Gottesman }; 101*65bbcdfaSMichael Gottesman 102*65bbcdfaSMichael Gottesman 103*65bbcdfaSMichael Gottesman } // end namespace llvm 104*65bbcdfaSMichael Gottesman #endif 105*65bbcdfaSMichael Gottesman 106875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq", 107875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 108875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 109875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq", 110875ebd5fSJakub Staszak "Machine Block Frequency Analysis", true, true) 111875ebd5fSJakub Staszak 112875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0; 113875ebd5fSJakub Staszak 114875ebd5fSJakub Staszak 115875ebd5fSJakub Staszak MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() : MachineFunctionPass(ID) { 116875ebd5fSJakub Staszak initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); 117875ebd5fSJakub Staszak MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction, 118875ebd5fSJakub Staszak MachineBranchProbabilityInfo>(); 119875ebd5fSJakub Staszak } 120875ebd5fSJakub Staszak 121875ebd5fSJakub Staszak MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() { 122875ebd5fSJakub Staszak delete MBFI; 123875ebd5fSJakub Staszak } 124875ebd5fSJakub Staszak 125875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { 126875ebd5fSJakub Staszak AU.addRequired<MachineBranchProbabilityInfo>(); 127875ebd5fSJakub Staszak AU.setPreservesAll(); 128875ebd5fSJakub Staszak MachineFunctionPass::getAnalysisUsage(AU); 129875ebd5fSJakub Staszak } 130875ebd5fSJakub Staszak 131875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { 132875ebd5fSJakub Staszak MachineBranchProbabilityInfo &MBPI = getAnalysis<MachineBranchProbabilityInfo>(); 133875ebd5fSJakub Staszak MBFI->doFunction(&F, &MBPI); 134*65bbcdfaSMichael Gottesman #ifndef NDEBUG 135*65bbcdfaSMichael Gottesman if (ViewMachineBlockFreqPropagationDAG != GVDT_None) { 136*65bbcdfaSMichael Gottesman view(); 137*65bbcdfaSMichael Gottesman } 138*65bbcdfaSMichael Gottesman #endif 139875ebd5fSJakub Staszak return false; 140875ebd5fSJakub Staszak } 141875ebd5fSJakub Staszak 142*65bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation 143*65bbcdfaSMichael Gottesman /// rendered using dot. 144*65bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const { 145*65bbcdfaSMichael Gottesman // This code is only for debugging. 146*65bbcdfaSMichael Gottesman #ifndef NDEBUG 147*65bbcdfaSMichael Gottesman ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), 148*65bbcdfaSMichael Gottesman "MachineBlockFrequencyDAGs"); 149*65bbcdfaSMichael Gottesman #else 150*65bbcdfaSMichael Gottesman errs() << "BlockFrequencyInfo::view is only available in debug builds on " 151*65bbcdfaSMichael Gottesman "systems with Graphviz or gv!\n"; 152*65bbcdfaSMichael Gottesman #endif // NDEBUG 153*65bbcdfaSMichael Gottesman } 154*65bbcdfaSMichael Gottesman 155a60d130fSJakub Staszak BlockFrequency MachineBlockFrequencyInfo:: 15696f8c551SJakub Staszak getBlockFreq(const MachineBasicBlock *MBB) const { 157875ebd5fSJakub Staszak return MBFI->getBlockFreq(MBB); 158875ebd5fSJakub Staszak } 159*65bbcdfaSMichael Gottesman 160*65bbcdfaSMichael Gottesman MachineFunction *MachineBlockFrequencyInfo::getFunction() const { 161*65bbcdfaSMichael Gottesman return MBFI->Fn; 162*65bbcdfaSMichael Gottesman } 163*65bbcdfaSMichael Gottesman 164