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 |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame^] | 30 | static cl::opt<GVDAGType> ViewBlockFreqPropagationDAG( |
| 31 | "view-block-freq-propagation-dags", cl::Hidden, |
| 32 | cl::desc("Pop up a window to show a dag displaying how block " |
| 33 | "frequencies propagation through the CFG."), |
| 34 | cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), |
| 35 | clEnumValN(GVDT_Fraction, "fraction", |
| 36 | "display a graph using the " |
| 37 | "fractional block frequency representation."), |
| 38 | clEnumValN(GVDT_Integer, "integer", |
| 39 | "display a graph using the raw " |
| 40 | "integer fractional block frequency representation."), |
| 41 | clEnumValN(GVDT_Count, "count", "display a graph using the real " |
| 42 | "profile count if available."), |
| 43 | clEnumValEnd)); |
| 44 | |
| 45 | cl::opt<std::string> ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 46 | |
| 47 | namespace llvm { |
| 48 | |
| 49 | template <> |
| 50 | struct GraphTraits<BlockFrequencyInfo *> { |
| 51 | typedef const BasicBlock NodeType; |
| 52 | typedef succ_const_iterator ChildIteratorType; |
| 53 | typedef Function::const_iterator nodes_iterator; |
| 54 | |
| 55 | static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) { |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 56 | return &G->getFunction()->front(); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 57 | } |
| 58 | static ChildIteratorType child_begin(const NodeType *N) { |
| 59 | return succ_begin(N); |
| 60 | } |
| 61 | static ChildIteratorType child_end(const NodeType *N) { |
| 62 | return succ_end(N); |
| 63 | } |
| 64 | static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) { |
| 65 | return G->getFunction()->begin(); |
| 66 | } |
| 67 | static nodes_iterator nodes_end(const BlockFrequencyInfo *G) { |
| 68 | return G->getFunction()->end(); |
| 69 | } |
| 70 | }; |
| 71 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 72 | typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo> |
| 73 | BFIDOTGTraitsBase; |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 74 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 75 | template <> |
| 76 | struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase { |
| 77 | explicit DOTGraphTraits(bool isSimple = false) |
| 78 | : BFIDOTGTraitsBase(isSimple) {} |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 79 | |
| 80 | std::string getNodeLabel(const BasicBlock *Node, |
| 81 | const BlockFrequencyInfo *Graph) { |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 82 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 83 | return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, |
| 84 | ViewBlockFreqPropagationDAG); |
| 85 | } |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 86 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 87 | std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI, |
| 88 | const BlockFrequencyInfo *BFI) { |
| 89 | return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI->getBPI()); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 90 | } |
| 91 | }; |
| 92 | |
| 93 | } // end namespace llvm |
| 94 | #endif |
| 95 | |
Cong Hou | 9b4f6b2 | 2015-07-16 23:23:35 +0000 | [diff] [blame] | 96 | BlockFrequencyInfo::BlockFrequencyInfo() {} |
| 97 | |
| 98 | BlockFrequencyInfo::BlockFrequencyInfo(const Function &F, |
| 99 | const BranchProbabilityInfo &BPI, |
| 100 | const LoopInfo &LI) { |
| 101 | calculate(F, BPI, LI); |
| 102 | } |
| 103 | |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 104 | BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg) |
| 105 | : BFI(std::move(Arg.BFI)) {} |
| 106 | |
| 107 | BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) { |
| 108 | releaseMemory(); |
| 109 | BFI = std::move(RHS.BFI); |
| 110 | return *this; |
| 111 | } |
| 112 | |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 113 | void BlockFrequencyInfo::calculate(const Function &F, |
| 114 | const BranchProbabilityInfo &BPI, |
| 115 | const LoopInfo &LI) { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 116 | if (!BFI) |
| 117 | BFI.reset(new ImplType); |
Cong Hou | 5e67b66 | 2015-07-15 19:58:26 +0000 | [diff] [blame] | 118 | BFI->calculate(F, BPI, LI); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 119 | #ifndef NDEBUG |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame^] | 120 | if (ViewBlockFreqPropagationDAG != GVDT_None && |
| 121 | (ViewBlockFreqFuncName.empty() || |
| 122 | F.getName().equals(ViewBlockFreqFuncName))) { |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 123 | view(); |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame^] | 124 | } |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 125 | #endif |
Chandler Carruth | 343fad4 | 2011-10-19 10:12:41 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Jakub Staszak | 96f8c55 | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 128 | BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 129 | return BFI ? BFI->getBlockFreq(BB) : 0; |
Jakub Staszak | 668c6fa | 2011-06-23 21:56:59 +0000 | [diff] [blame] | 130 | } |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 131 | |
Easwaran Raman | 12b79aa | 2016-03-23 18:18:26 +0000 | [diff] [blame] | 132 | Optional<uint64_t> |
| 133 | BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const { |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 134 | if (!BFI) |
Easwaran Raman | 12b79aa | 2016-03-23 18:18:26 +0000 | [diff] [blame] | 135 | return None; |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 136 | |
| 137 | return BFI->getBlockProfileCount(*getFunction(), BB); |
Easwaran Raman | 12b79aa | 2016-03-23 18:18:26 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 140 | void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) { |
Manman Ren | 72d44b1 | 2015-10-15 14:59:40 +0000 | [diff] [blame] | 141 | assert(BFI && "Expected analysis to be available"); |
| 142 | BFI->setBlockFreq(BB, Freq); |
| 143 | } |
| 144 | |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 145 | /// Pop up a ghostview window with the current block frequency propagation |
| 146 | /// rendered using dot. |
| 147 | void BlockFrequencyInfo::view() const { |
| 148 | // This code is only for debugging. |
| 149 | #ifndef NDEBUG |
| 150 | ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs"); |
| 151 | #else |
| 152 | errs() << "BlockFrequencyInfo::view is only available in debug builds on " |
| 153 | "systems with Graphviz or gv!\n"; |
| 154 | #endif // NDEBUG |
| 155 | } |
| 156 | |
| 157 | const Function *BlockFrequencyInfo::getFunction() const { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 158 | return BFI ? BFI->getFunction() : nullptr; |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 159 | } |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 160 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 161 | const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const { |
| 162 | return BFI ? &BFI->getBPI() : nullptr; |
| 163 | } |
| 164 | |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 165 | raw_ostream &BlockFrequencyInfo:: |
| 166 | printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 167 | return BFI ? BFI->printBlockFreq(OS, Freq) : OS; |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | raw_ostream & |
| 171 | BlockFrequencyInfo::printBlockFreq(raw_ostream &OS, |
| 172 | const BasicBlock *BB) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 173 | return BFI ? BFI->printBlockFreq(OS, BB) : OS; |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 174 | } |
Yuchen Wu | 5947c8f | 2013-12-20 22:11:11 +0000 | [diff] [blame] | 175 | |
| 176 | uint64_t BlockFrequencyInfo::getEntryFreq() const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 177 | return BFI ? BFI->getEntryFreq() : 0; |
Yuchen Wu | 5947c8f | 2013-12-20 22:11:11 +0000 | [diff] [blame] | 178 | } |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 179 | |
| 180 | void BlockFrequencyInfo::releaseMemory() { BFI.reset(); } |
| 181 | |
| 182 | void BlockFrequencyInfo::print(raw_ostream &OS) const { |
| 183 | if (BFI) |
| 184 | BFI->print(OS); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq", |
| 189 | "Block Frequency Analysis", true, true) |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 190 | INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass) |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 191 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 192 | INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq", |
| 193 | "Block Frequency Analysis", true, true) |
| 194 | |
| 195 | char BlockFrequencyInfoWrapperPass::ID = 0; |
| 196 | |
| 197 | |
| 198 | BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass() |
| 199 | : FunctionPass(ID) { |
| 200 | initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 201 | } |
| 202 | |
| 203 | BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {} |
| 204 | |
| 205 | void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS, |
| 206 | const Module *) const { |
| 207 | BFI.print(OS); |
| 208 | } |
| 209 | |
| 210 | void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 211 | AU.addRequired<BranchProbabilityInfoWrapperPass>(); |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 212 | AU.addRequired<LoopInfoWrapperPass>(); |
| 213 | AU.setPreservesAll(); |
| 214 | } |
| 215 | |
| 216 | void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); } |
| 217 | |
| 218 | bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) { |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 219 | BranchProbabilityInfo &BPI = |
| 220 | getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI(); |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 221 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 222 | BFI.calculate(F, BPI, LI); |
| 223 | return false; |
| 224 | } |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 225 | |
| 226 | char BlockFrequencyAnalysis::PassID; |
| 227 | BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F, |
| 228 | AnalysisManager<Function> &AM) { |
| 229 | BlockFrequencyInfo BFI; |
| 230 | BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F), |
| 231 | AM.getResult<LoopAnalysis>(F)); |
| 232 | return BFI; |
| 233 | } |
| 234 | |
| 235 | PreservedAnalyses |
| 236 | BlockFrequencyPrinterPass::run(Function &F, AnalysisManager<Function> &AM) { |
| 237 | OS << "Printing analysis results of BFI for function " |
| 238 | << "'" << F.getName() << "':" |
| 239 | << "\n"; |
| 240 | AM.getResult<BlockFrequencyAnalysis>(F).print(OS); |
| 241 | return PreservedAnalyses::all(); |
| 242 | } |