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 | |
Xinliang David Li | 3e176c7 | 2016-06-28 06:58:21 +0000 | [diff] [blame] | 45 | cl::opt<std::string> |
| 46 | ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden, |
| 47 | cl::desc("The option to specify " |
| 48 | "the name of the function " |
| 49 | "whose CFG will be displayed.")); |
| 50 | |
| 51 | cl::opt<unsigned> |
| 52 | ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden, |
| 53 | cl::desc("An integer in percent used to specify " |
| 54 | "the hot blocks/edges to be displayed " |
| 55 | "in red: a block or edge whose frequency " |
| 56 | "is no less than the max frequency of the " |
| 57 | "function multiplied by this percent.")); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 58 | |
| 59 | namespace llvm { |
| 60 | |
| 61 | template <> |
| 62 | struct GraphTraits<BlockFrequencyInfo *> { |
Tim Shen | eb3958f | 2016-08-17 20:07:29 +0000 | [diff] [blame] | 63 | typedef const BasicBlock *NodeRef; |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 64 | typedef succ_const_iterator ChildIteratorType; |
Tim Shen | b5e0f5a | 2016-08-19 21:20:13 +0000 | [diff] [blame] | 65 | typedef pointer_iterator<Function::const_iterator> nodes_iterator; |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 66 | |
Tim Shen | 48f814e | 2016-08-31 16:48:13 +0000 | [diff] [blame] | 67 | static NodeRef getEntryNode(const BlockFrequencyInfo *G) { |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 68 | return &G->getFunction()->front(); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 69 | } |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 70 | static ChildIteratorType child_begin(const NodeRef N) { |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 71 | return succ_begin(N); |
| 72 | } |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 73 | static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); } |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 74 | static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) { |
Tim Shen | b5e0f5a | 2016-08-19 21:20:13 +0000 | [diff] [blame] | 75 | return nodes_iterator(G->getFunction()->begin()); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 76 | } |
| 77 | static nodes_iterator nodes_end(const BlockFrequencyInfo *G) { |
Tim Shen | b5e0f5a | 2016-08-19 21:20:13 +0000 | [diff] [blame] | 78 | return nodes_iterator(G->getFunction()->end()); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 82 | typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo> |
| 83 | BFIDOTGTraitsBase; |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 84 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 85 | template <> |
| 86 | struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase { |
| 87 | explicit DOTGraphTraits(bool isSimple = false) |
| 88 | : BFIDOTGTraitsBase(isSimple) {} |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 89 | |
| 90 | std::string getNodeLabel(const BasicBlock *Node, |
| 91 | const BlockFrequencyInfo *Graph) { |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 92 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 93 | return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, |
| 94 | ViewBlockFreqPropagationDAG); |
| 95 | } |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 96 | |
Xinliang David Li | 3e176c7 | 2016-06-28 06:58:21 +0000 | [diff] [blame] | 97 | std::string getNodeAttributes(const BasicBlock *Node, |
| 98 | const BlockFrequencyInfo *Graph) { |
| 99 | return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph, |
| 100 | ViewHotFreqPercent); |
| 101 | } |
| 102 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 103 | std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI, |
| 104 | const BlockFrequencyInfo *BFI) { |
Xinliang David Li | 3e176c7 | 2016-06-28 06:58:21 +0000 | [diff] [blame] | 105 | return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(), |
| 106 | ViewHotFreqPercent); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 107 | } |
| 108 | }; |
| 109 | |
| 110 | } // end namespace llvm |
| 111 | #endif |
| 112 | |
Cong Hou | 9b4f6b2 | 2015-07-16 23:23:35 +0000 | [diff] [blame] | 113 | BlockFrequencyInfo::BlockFrequencyInfo() {} |
| 114 | |
| 115 | BlockFrequencyInfo::BlockFrequencyInfo(const Function &F, |
| 116 | const BranchProbabilityInfo &BPI, |
| 117 | const LoopInfo &LI) { |
| 118 | calculate(F, BPI, LI); |
| 119 | } |
| 120 | |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 121 | BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg) |
| 122 | : BFI(std::move(Arg.BFI)) {} |
| 123 | |
| 124 | BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) { |
| 125 | releaseMemory(); |
| 126 | BFI = std::move(RHS.BFI); |
| 127 | return *this; |
| 128 | } |
| 129 | |
Adam Nemet | c2f791d | 2016-07-13 05:01:48 +0000 | [diff] [blame] | 130 | // Explicitly define the default constructor otherwise it would be implicitly |
| 131 | // defined at the first ODR-use which is the BFI member in the |
| 132 | // LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl |
| 133 | // template instantiated which is not available in the header. |
| 134 | BlockFrequencyInfo::~BlockFrequencyInfo() {} |
| 135 | |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 136 | void BlockFrequencyInfo::calculate(const Function &F, |
| 137 | const BranchProbabilityInfo &BPI, |
| 138 | const LoopInfo &LI) { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 139 | if (!BFI) |
| 140 | BFI.reset(new ImplType); |
Cong Hou | 5e67b66 | 2015-07-15 19:58:26 +0000 | [diff] [blame] | 141 | BFI->calculate(F, BPI, LI); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 142 | #ifndef NDEBUG |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame] | 143 | if (ViewBlockFreqPropagationDAG != GVDT_None && |
| 144 | (ViewBlockFreqFuncName.empty() || |
| 145 | F.getName().equals(ViewBlockFreqFuncName))) { |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 146 | view(); |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame] | 147 | } |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 148 | #endif |
Chandler Carruth | 343fad4 | 2011-10-19 10:12:41 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Jakub Staszak | 96f8c55 | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 151 | BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 152 | return BFI ? BFI->getBlockFreq(BB) : 0; |
Jakub Staszak | 668c6fa | 2011-06-23 21:56:59 +0000 | [diff] [blame] | 153 | } |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 154 | |
Easwaran Raman | 12b79aa | 2016-03-23 18:18:26 +0000 | [diff] [blame] | 155 | Optional<uint64_t> |
| 156 | BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const { |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 157 | if (!BFI) |
Easwaran Raman | 12b79aa | 2016-03-23 18:18:26 +0000 | [diff] [blame] | 158 | return None; |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 159 | |
| 160 | return BFI->getBlockProfileCount(*getFunction(), BB); |
Easwaran Raman | 12b79aa | 2016-03-23 18:18:26 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 163 | Optional<uint64_t> |
| 164 | BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { |
| 165 | if (!BFI) |
| 166 | return None; |
| 167 | return BFI->getProfileCountFromFreq(*getFunction(), Freq); |
| 168 | } |
| 169 | |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 170 | void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) { |
Manman Ren | 72d44b1 | 2015-10-15 14:59:40 +0000 | [diff] [blame] | 171 | assert(BFI && "Expected analysis to be available"); |
| 172 | BFI->setBlockFreq(BB, Freq); |
| 173 | } |
| 174 | |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 175 | /// Pop up a ghostview window with the current block frequency propagation |
| 176 | /// rendered using dot. |
| 177 | void BlockFrequencyInfo::view() const { |
| 178 | // This code is only for debugging. |
| 179 | #ifndef NDEBUG |
| 180 | ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs"); |
| 181 | #else |
| 182 | errs() << "BlockFrequencyInfo::view is only available in debug builds on " |
| 183 | "systems with Graphviz or gv!\n"; |
| 184 | #endif // NDEBUG |
| 185 | } |
| 186 | |
| 187 | const Function *BlockFrequencyInfo::getFunction() const { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 188 | return BFI ? BFI->getFunction() : nullptr; |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 189 | } |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 190 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 191 | const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const { |
| 192 | return BFI ? &BFI->getBPI() : nullptr; |
| 193 | } |
| 194 | |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 195 | raw_ostream &BlockFrequencyInfo:: |
| 196 | printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 197 | return BFI ? BFI->printBlockFreq(OS, Freq) : OS; |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | raw_ostream & |
| 201 | BlockFrequencyInfo::printBlockFreq(raw_ostream &OS, |
| 202 | const BasicBlock *BB) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 203 | return BFI ? BFI->printBlockFreq(OS, BB) : OS; |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 204 | } |
Yuchen Wu | 5947c8f | 2013-12-20 22:11:11 +0000 | [diff] [blame] | 205 | |
| 206 | uint64_t BlockFrequencyInfo::getEntryFreq() const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 207 | return BFI ? BFI->getEntryFreq() : 0; |
Yuchen Wu | 5947c8f | 2013-12-20 22:11:11 +0000 | [diff] [blame] | 208 | } |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 209 | |
| 210 | void BlockFrequencyInfo::releaseMemory() { BFI.reset(); } |
| 211 | |
| 212 | void BlockFrequencyInfo::print(raw_ostream &OS) const { |
| 213 | if (BFI) |
| 214 | BFI->print(OS); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq", |
| 219 | "Block Frequency Analysis", true, true) |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 220 | INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass) |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 221 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 222 | INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq", |
| 223 | "Block Frequency Analysis", true, true) |
| 224 | |
| 225 | char BlockFrequencyInfoWrapperPass::ID = 0; |
| 226 | |
| 227 | |
| 228 | BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass() |
| 229 | : FunctionPass(ID) { |
| 230 | initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 231 | } |
| 232 | |
| 233 | BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {} |
| 234 | |
| 235 | void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS, |
| 236 | const Module *) const { |
| 237 | BFI.print(OS); |
| 238 | } |
| 239 | |
| 240 | void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 241 | AU.addRequired<BranchProbabilityInfoWrapperPass>(); |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 242 | AU.addRequired<LoopInfoWrapperPass>(); |
| 243 | AU.setPreservesAll(); |
| 244 | } |
| 245 | |
| 246 | void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); } |
| 247 | |
| 248 | bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) { |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 249 | BranchProbabilityInfo &BPI = |
| 250 | getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI(); |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 251 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 252 | BFI.calculate(F, BPI, LI); |
| 253 | return false; |
| 254 | } |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 255 | |
| 256 | char BlockFrequencyAnalysis::PassID; |
| 257 | BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 258 | FunctionAnalysisManager &AM) { |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 259 | BlockFrequencyInfo BFI; |
| 260 | BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F), |
| 261 | AM.getResult<LoopAnalysis>(F)); |
| 262 | return BFI; |
| 263 | } |
| 264 | |
| 265 | PreservedAnalyses |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 266 | BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 267 | OS << "Printing analysis results of BFI for function " |
| 268 | << "'" << F.getName() << "':" |
| 269 | << "\n"; |
| 270 | AM.getResult<BlockFrequencyAnalysis>(F).print(OS); |
| 271 | return PreservedAnalyses::all(); |
| 272 | } |