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