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