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