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" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/None.h" |
| 17 | #include "llvm/ADT/iterator.h" |
Duncan P. N. Exon Smith | 689a507 | 2014-04-11 23:20:58 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/BlockFrequencyInfoImpl.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Jakub Staszak | 668c6fa | 2011-06-23 21:56:59 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CFG.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Function.h" |
| 23 | #include "llvm/IR/PassManager.h" |
| 24 | #include "llvm/Pass.h" |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 26 | #include "llvm/Support/GraphWriter.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include <algorithm> |
| 29 | #include <cassert> |
| 30 | #include <string> |
Jakub Staszak | 668c6fa | 2011-06-23 21:56:59 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "block-freq" |
| 35 | |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame] | 36 | static cl::opt<GVDAGType> ViewBlockFreqPropagationDAG( |
| 37 | "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(clEnumValN(GVDT_None, "none", "do not display graphs."), |
| 41 | clEnumValN(GVDT_Fraction, "fraction", |
| 42 | "display a graph using the " |
| 43 | "fractional block frequency representation."), |
| 44 | clEnumValN(GVDT_Integer, "integer", |
| 45 | "display a graph using the raw " |
| 46 | "integer fractional block frequency representation."), |
| 47 | clEnumValN(GVDT_Count, "count", "display a graph using the real " |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 48 | "profile count if available."))); |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame] | 49 | |
Xinliang David Li | 3e176c7 | 2016-06-28 06:58:21 +0000 | [diff] [blame] | 50 | cl::opt<std::string> |
| 51 | ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden, |
| 52 | cl::desc("The option to specify " |
| 53 | "the name of the function " |
| 54 | "whose CFG will be displayed.")); |
| 55 | |
| 56 | cl::opt<unsigned> |
| 57 | ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden, |
| 58 | cl::desc("An integer in percent used to specify " |
| 59 | "the hot blocks/edges to be displayed " |
| 60 | "in red: a block or edge whose frequency " |
| 61 | "is no less than the max frequency of the " |
| 62 | "function multiplied by this percent.")); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 63 | |
Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 64 | // Command line option to turn on CFG dot dump after profile annotation. |
Xinliang David Li | 58fcc9b | 2017-02-02 21:29:17 +0000 | [diff] [blame] | 65 | cl::opt<bool> |
| 66 | PGOViewCounts("pgo-view-counts", cl::init(false), cl::Hidden, |
| 67 | cl::desc("A boolean option to show CFG dag with " |
| 68 | "block profile counts and branch probabilities " |
| 69 | "right after PGO profile annotation step. The " |
| 70 | "profile counts are computed using branch " |
| 71 | "probabilities from the runtime profile data and " |
| 72 | "block frequency propagation algorithm. To view " |
| 73 | "the raw counts from the profile, use option " |
| 74 | "-pgo-view-raw-counts instead. To limit graph " |
| 75 | "display to only one function, use filtering option " |
| 76 | "-view-bfi-func-name.")); |
Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 77 | |
Hiroshi Yamauchi | 63e17eb | 2017-08-26 00:31:00 +0000 | [diff] [blame] | 78 | static cl::opt<bool> PrintBlockFreq( |
| 79 | "print-bfi", cl::init(false), cl::Hidden, |
| 80 | cl::desc("Print the block frequency info.")); |
| 81 | |
| 82 | cl::opt<std::string> PrintBlockFreqFuncName( |
| 83 | "print-bfi-func-name", cl::Hidden, |
| 84 | cl::desc("The option to specify the name of the function " |
| 85 | "whose block frequency info is printed.")); |
| 86 | |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 87 | namespace llvm { |
| 88 | |
Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 89 | static GVDAGType getGVDT() { |
Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 90 | if (PGOViewCounts) |
| 91 | return GVDT_Count; |
| 92 | return ViewBlockFreqPropagationDAG; |
| 93 | } |
| 94 | |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 95 | template <> |
| 96 | struct GraphTraits<BlockFrequencyInfo *> { |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 97 | using NodeRef = const BasicBlock *; |
| 98 | using ChildIteratorType = succ_const_iterator; |
| 99 | using nodes_iterator = pointer_iterator<Function::const_iterator>; |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 100 | |
Tim Shen | 48f814e | 2016-08-31 16:48:13 +0000 | [diff] [blame] | 101 | static NodeRef getEntryNode(const BlockFrequencyInfo *G) { |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 102 | return &G->getFunction()->front(); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 103 | } |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 104 | |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 105 | static ChildIteratorType child_begin(const NodeRef N) { |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 106 | return succ_begin(N); |
| 107 | } |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 108 | |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 109 | static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); } |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 110 | |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 111 | static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) { |
Tim Shen | b5e0f5a | 2016-08-19 21:20:13 +0000 | [diff] [blame] | 112 | return nodes_iterator(G->getFunction()->begin()); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 113 | } |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 114 | |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 115 | static nodes_iterator nodes_end(const BlockFrequencyInfo *G) { |
Tim Shen | b5e0f5a | 2016-08-19 21:20:13 +0000 | [diff] [blame] | 116 | return nodes_iterator(G->getFunction()->end()); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 117 | } |
| 118 | }; |
| 119 | |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 120 | using BFIDOTGTraitsBase = |
| 121 | BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>; |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 122 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 123 | template <> |
| 124 | struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase { |
| 125 | explicit DOTGraphTraits(bool isSimple = false) |
| 126 | : BFIDOTGTraitsBase(isSimple) {} |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 127 | |
| 128 | std::string getNodeLabel(const BasicBlock *Node, |
| 129 | const BlockFrequencyInfo *Graph) { |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 130 | |
Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 131 | return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, getGVDT()); |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 132 | } |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 133 | |
Xinliang David Li | 3e176c7 | 2016-06-28 06:58:21 +0000 | [diff] [blame] | 134 | std::string getNodeAttributes(const BasicBlock *Node, |
| 135 | const BlockFrequencyInfo *Graph) { |
| 136 | return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph, |
| 137 | ViewHotFreqPercent); |
| 138 | } |
| 139 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 140 | std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI, |
| 141 | const BlockFrequencyInfo *BFI) { |
Xinliang David Li | 3e176c7 | 2016-06-28 06:58:21 +0000 | [diff] [blame] | 142 | return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(), |
| 143 | ViewHotFreqPercent); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 144 | } |
| 145 | }; |
| 146 | |
| 147 | } // end namespace llvm |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 148 | |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 149 | BlockFrequencyInfo::BlockFrequencyInfo() = default; |
Cong Hou | 9b4f6b2 | 2015-07-16 23:23:35 +0000 | [diff] [blame] | 150 | |
| 151 | BlockFrequencyInfo::BlockFrequencyInfo(const Function &F, |
| 152 | const BranchProbabilityInfo &BPI, |
| 153 | const LoopInfo &LI) { |
| 154 | calculate(F, BPI, LI); |
| 155 | } |
| 156 | |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 157 | BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg) |
| 158 | : BFI(std::move(Arg.BFI)) {} |
| 159 | |
| 160 | BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) { |
| 161 | releaseMemory(); |
| 162 | BFI = std::move(RHS.BFI); |
| 163 | return *this; |
| 164 | } |
| 165 | |
Adam Nemet | c2f791d | 2016-07-13 05:01:48 +0000 | [diff] [blame] | 166 | // Explicitly define the default constructor otherwise it would be implicitly |
| 167 | // defined at the first ODR-use which is the BFI member in the |
| 168 | // LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl |
| 169 | // template instantiated which is not available in the header. |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 170 | BlockFrequencyInfo::~BlockFrequencyInfo() = default; |
Adam Nemet | c2f791d | 2016-07-13 05:01:48 +0000 | [diff] [blame] | 171 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 172 | bool BlockFrequencyInfo::invalidate(Function &F, const PreservedAnalyses &PA, |
| 173 | FunctionAnalysisManager::Invalidator &) { |
| 174 | // Check whether the analysis, all analyses on functions, or the function's |
| 175 | // CFG have been preserved. |
| 176 | auto PAC = PA.getChecker<BlockFrequencyAnalysis>(); |
| 177 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() || |
| 178 | PAC.preservedSet<CFGAnalyses>()); |
| 179 | } |
| 180 | |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 181 | void BlockFrequencyInfo::calculate(const Function &F, |
| 182 | const BranchProbabilityInfo &BPI, |
| 183 | const LoopInfo &LI) { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 184 | if (!BFI) |
| 185 | BFI.reset(new ImplType); |
Cong Hou | 5e67b66 | 2015-07-15 19:58:26 +0000 | [diff] [blame] | 186 | BFI->calculate(F, BPI, LI); |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame] | 187 | if (ViewBlockFreqPropagationDAG != GVDT_None && |
| 188 | (ViewBlockFreqFuncName.empty() || |
| 189 | F.getName().equals(ViewBlockFreqFuncName))) { |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 190 | view(); |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame] | 191 | } |
Hiroshi Yamauchi | 63e17eb | 2017-08-26 00:31:00 +0000 | [diff] [blame] | 192 | if (PrintBlockFreq && |
| 193 | (PrintBlockFreqFuncName.empty() || |
| 194 | F.getName().equals(PrintBlockFreqFuncName))) { |
| 195 | print(dbgs()); |
| 196 | } |
Chandler Carruth | 343fad4 | 2011-10-19 10:12:41 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Jakub Staszak | 96f8c55 | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 199 | BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 200 | return BFI ? BFI->getBlockFreq(BB) : 0; |
Jakub Staszak | 668c6fa | 2011-06-23 21:56:59 +0000 | [diff] [blame] | 201 | } |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 202 | |
Easwaran Raman | 12b79aa | 2016-03-23 18:18:26 +0000 | [diff] [blame] | 203 | Optional<uint64_t> |
| 204 | BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const { |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 205 | if (!BFI) |
Easwaran Raman | 12b79aa | 2016-03-23 18:18:26 +0000 | [diff] [blame] | 206 | return None; |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 207 | |
| 208 | return BFI->getBlockProfileCount(*getFunction(), BB); |
Easwaran Raman | 12b79aa | 2016-03-23 18:18:26 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 211 | Optional<uint64_t> |
| 212 | BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { |
| 213 | if (!BFI) |
| 214 | return None; |
| 215 | return BFI->getProfileCountFromFreq(*getFunction(), Freq); |
| 216 | } |
| 217 | |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 218 | void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) { |
Manman Ren | 72d44b1 | 2015-10-15 14:59:40 +0000 | [diff] [blame] | 219 | assert(BFI && "Expected analysis to be available"); |
| 220 | BFI->setBlockFreq(BB, Freq); |
| 221 | } |
| 222 | |
Easwaran Raman | 6c8f511 | 2017-01-19 18:53:16 +0000 | [diff] [blame] | 223 | void BlockFrequencyInfo::setBlockFreqAndScale( |
| 224 | const BasicBlock *ReferenceBB, uint64_t Freq, |
| 225 | SmallPtrSetImpl<BasicBlock *> &BlocksToScale) { |
| 226 | assert(BFI && "Expected analysis to be available"); |
| 227 | // Use 128 bits APInt to avoid overflow. |
| 228 | APInt NewFreq(128, Freq); |
| 229 | APInt OldFreq(128, BFI->getBlockFreq(ReferenceBB).getFrequency()); |
| 230 | APInt BBFreq(128, 0); |
| 231 | for (auto *BB : BlocksToScale) { |
| 232 | BBFreq = BFI->getBlockFreq(BB).getFrequency(); |
| 233 | // Multiply first by NewFreq and then divide by OldFreq |
| 234 | // to minimize loss of precision. |
| 235 | BBFreq *= NewFreq; |
| 236 | // udiv is an expensive operation in the general case. If this ends up being |
| 237 | // a hot spot, one of the options proposed in |
| 238 | // https://reviews.llvm.org/D28535#650071 could be used to avoid this. |
| 239 | BBFreq = BBFreq.udiv(OldFreq); |
| 240 | BFI->setBlockFreq(BB, BBFreq.getLimitedValue()); |
| 241 | } |
| 242 | BFI->setBlockFreq(ReferenceBB, Freq); |
| 243 | } |
| 244 | |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 245 | /// Pop up a ghostview window with the current block frequency propagation |
| 246 | /// rendered using dot. |
| 247 | void BlockFrequencyInfo::view() const { |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 248 | ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs"); |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | const Function *BlockFrequencyInfo::getFunction() const { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 252 | return BFI ? BFI->getFunction() : nullptr; |
Michael Gottesman | fd8aee7 | 2013-11-14 02:27:46 +0000 | [diff] [blame] | 253 | } |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 254 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 255 | const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const { |
| 256 | return BFI ? &BFI->getBPI() : nullptr; |
| 257 | } |
| 258 | |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 259 | raw_ostream &BlockFrequencyInfo:: |
| 260 | printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 261 | return BFI ? BFI->printBlockFreq(OS, Freq) : OS; |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | raw_ostream & |
| 265 | BlockFrequencyInfo::printBlockFreq(raw_ostream &OS, |
| 266 | const BasicBlock *BB) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 267 | return BFI ? BFI->printBlockFreq(OS, BB) : OS; |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 268 | } |
Yuchen Wu | 5947c8f | 2013-12-20 22:11:11 +0000 | [diff] [blame] | 269 | |
| 270 | uint64_t BlockFrequencyInfo::getEntryFreq() const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 271 | return BFI ? BFI->getEntryFreq() : 0; |
Yuchen Wu | 5947c8f | 2013-12-20 22:11:11 +0000 | [diff] [blame] | 272 | } |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 273 | |
| 274 | void BlockFrequencyInfo::releaseMemory() { BFI.reset(); } |
| 275 | |
| 276 | void BlockFrequencyInfo::print(raw_ostream &OS) const { |
| 277 | if (BFI) |
| 278 | BFI->print(OS); |
| 279 | } |
| 280 | |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 281 | INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq", |
| 282 | "Block Frequency Analysis", true, true) |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 283 | INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass) |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 284 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 285 | INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq", |
| 286 | "Block Frequency Analysis", true, true) |
| 287 | |
| 288 | char BlockFrequencyInfoWrapperPass::ID = 0; |
| 289 | |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 290 | BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass() |
| 291 | : FunctionPass(ID) { |
| 292 | initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 293 | } |
| 294 | |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 295 | BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() = default; |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 296 | |
| 297 | void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS, |
| 298 | const Module *) const { |
| 299 | BFI.print(OS); |
| 300 | } |
| 301 | |
| 302 | void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 303 | AU.addRequired<BranchProbabilityInfoWrapperPass>(); |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 304 | AU.addRequired<LoopInfoWrapperPass>(); |
| 305 | AU.setPreservesAll(); |
| 306 | } |
| 307 | |
| 308 | void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); } |
| 309 | |
| 310 | bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) { |
Cong Hou | ab23bfb | 2015-07-15 22:48:29 +0000 | [diff] [blame] | 311 | BranchProbabilityInfo &BPI = |
| 312 | getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI(); |
Wei Mi | deee61e | 2015-07-14 23:40:50 +0000 | [diff] [blame] | 313 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 314 | BFI.calculate(F, BPI, LI); |
| 315 | return false; |
| 316 | } |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 317 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 318 | AnalysisKey BlockFrequencyAnalysis::Key; |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 319 | BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 320 | FunctionAnalysisManager &AM) { |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 321 | BlockFrequencyInfo BFI; |
| 322 | BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F), |
| 323 | AM.getResult<LoopAnalysis>(F)); |
| 324 | return BFI; |
| 325 | } |
| 326 | |
| 327 | PreservedAnalyses |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 328 | BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 329 | OS << "Printing analysis results of BFI for function " |
| 330 | << "'" << F.getName() << "':" |
| 331 | << "\n"; |
| 332 | AM.getResult<BlockFrequencyAnalysis>(F).print(OS); |
| 333 | return PreservedAnalyses::all(); |
| 334 | } |