Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 1 | //====------ MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis ------====// |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Loops should be simplified before this analysis. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/BlockFrequencyImpl.h" |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/Passes.h" |
| 18 | #include "llvm/InitializePasses.h" |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/GraphWriter.h" |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 25 | #ifndef NDEBUG |
| 26 | enum GVDAGType { |
| 27 | GVDT_None, |
| 28 | GVDT_Fraction, |
| 29 | GVDT_Integer |
| 30 | }; |
| 31 | |
| 32 | static cl::opt<GVDAGType> |
| 33 | ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags", |
| 34 | cl::Hidden, |
| 35 | cl::desc("Pop up a window to show a dag displaying how machine block " |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 36 | "frequencies propagate through the CFG."), |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 37 | cl::values( |
| 38 | clEnumValN(GVDT_None, "none", |
| 39 | "do not display graphs."), |
| 40 | clEnumValN(GVDT_Fraction, "fraction", "display a graph using the " |
| 41 | "fractional block frequency representation."), |
| 42 | clEnumValN(GVDT_Integer, "integer", "display a graph using the raw " |
| 43 | "integer fractional block frequency representation."), |
| 44 | clEnumValEnd)); |
| 45 | |
| 46 | namespace llvm { |
| 47 | |
| 48 | template <> |
| 49 | struct GraphTraits<MachineBlockFrequencyInfo *> { |
| 50 | typedef const MachineBasicBlock NodeType; |
| 51 | typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; |
| 52 | typedef MachineFunction::const_iterator nodes_iterator; |
| 53 | |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 54 | static inline |
| 55 | const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) { |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 56 | return G->getFunction()->begin(); |
| 57 | } |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 58 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 59 | static ChildIteratorType child_begin(const NodeType *N) { |
| 60 | return N->succ_begin(); |
| 61 | } |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 62 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 63 | static ChildIteratorType child_end(const NodeType *N) { |
| 64 | return N->succ_end(); |
| 65 | } |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 66 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 67 | static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { |
| 68 | return G->getFunction()->begin(); |
| 69 | } |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 70 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 71 | static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { |
| 72 | return G->getFunction()->end(); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | template<> |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 77 | struct DOTGraphTraits<MachineBlockFrequencyInfo*> : |
| 78 | public DefaultDOTGraphTraits { |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 79 | explicit DOTGraphTraits(bool isSimple=false) : |
| 80 | DefaultDOTGraphTraits(isSimple) {} |
| 81 | |
| 82 | static std::string getGraphName(const MachineBlockFrequencyInfo *G) { |
| 83 | return G->getFunction()->getName(); |
| 84 | } |
| 85 | |
| 86 | std::string getNodeLabel(const MachineBasicBlock *Node, |
| 87 | const MachineBlockFrequencyInfo *Graph) { |
| 88 | std::string Result; |
| 89 | raw_string_ostream OS(Result); |
| 90 | |
| 91 | OS << Node->getName().str() << ":"; |
| 92 | switch (ViewMachineBlockFreqPropagationDAG) { |
| 93 | case GVDT_Fraction: |
Michael Gottesman | b0c1ed8 | 2013-12-14 00:25:42 +0000 | [diff] [blame] | 94 | Graph->printBlockFreq(OS, Node); |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 95 | break; |
| 96 | case GVDT_Integer: |
| 97 | OS << Graph->getBlockFreq(Node).getFrequency(); |
| 98 | break; |
| 99 | case GVDT_None: |
| 100 | llvm_unreachable("If we are not supposed to render a graph we should " |
| 101 | "never reach this point."); |
| 102 | } |
| 103 | |
| 104 | return Result; |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | |
| 109 | } // end namespace llvm |
| 110 | #endif |
| 111 | |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 112 | INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq", |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 113 | "Machine Block Frequency Analysis", true, true) |
| 114 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 115 | INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq", |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 116 | "Machine Block Frequency Analysis", true, true) |
| 117 | |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 118 | char MachineBlockFrequencyInfo::ID = 0; |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 119 | |
| 120 | |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 121 | MachineBlockFrequencyInfo:: |
| 122 | MachineBlockFrequencyInfo() :MachineFunctionPass(ID) { |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 123 | initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 124 | MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction, |
| 125 | MachineBranchProbabilityInfo>(); |
| 126 | } |
| 127 | |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 128 | MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() { |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 129 | delete MBFI; |
| 130 | } |
| 131 | |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 132 | void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 133 | AU.addRequired<MachineBranchProbabilityInfo>(); |
| 134 | AU.setPreservesAll(); |
Jakub Staszak | cb7c0a4 | 2011-07-21 22:59:09 +0000 | [diff] [blame] | 135 | MachineFunctionPass::getAnalysisUsage(AU); |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 138 | bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 139 | MachineBranchProbabilityInfo &MBPI = |
| 140 | getAnalysis<MachineBranchProbabilityInfo>(); |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 141 | MBFI->doFunction(&F, &MBPI); |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 142 | #ifndef NDEBUG |
| 143 | if (ViewMachineBlockFreqPropagationDAG != GVDT_None) { |
| 144 | view(); |
| 145 | } |
| 146 | #endif |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 150 | /// Pop up a ghostview window with the current block frequency propagation |
| 151 | /// rendered using dot. |
| 152 | void MachineBlockFrequencyInfo::view() const { |
| 153 | // This code is only for debugging. |
| 154 | #ifndef NDEBUG |
| 155 | ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), |
| 156 | "MachineBlockFrequencyDAGs"); |
| 157 | #else |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 158 | errs() << "MachineBlockFrequencyInfo::view is only available in debug builds " |
| 159 | "on systems with Graphviz or gv!\n"; |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 160 | #endif // NDEBUG |
| 161 | } |
| 162 | |
Jakub Staszak | a60d130 | 2011-08-03 21:30:57 +0000 | [diff] [blame] | 163 | BlockFrequency MachineBlockFrequencyInfo:: |
Jakub Staszak | 96f8c55 | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 164 | getBlockFreq(const MachineBasicBlock *MBB) const { |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 165 | return MBFI->getBlockFreq(MBB); |
| 166 | } |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 167 | |
| 168 | MachineFunction *MachineBlockFrequencyInfo::getFunction() const { |
| 169 | return MBFI->Fn; |
| 170 | } |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 171 | |
| 172 | raw_ostream & |
| 173 | MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, |
| 174 | const BlockFrequency Freq) const { |
| 175 | return MBFI->printBlockFreq(OS, Freq); |
| 176 | } |
| 177 | |
| 178 | raw_ostream & |
| 179 | MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, |
| 180 | const MachineBasicBlock *MBB) const { |
| 181 | return MBFI->printBlockFreq(OS, MBB); |
| 182 | } |
| 183 | |
| 184 | uint64_t MachineBlockFrequencyInfo::getEntryFrequency() const { |
| 185 | return MBFI->getEntryFrequency(); |
| 186 | } |