blob: 07a2a9229fd543bf5928e074fe5f475548cbb737 [file] [log] [blame]
Duncan P. N. Exon Smith689a5072014-04-11 23:20:58 +00001//===- BlockFrequencyInfo.cpp - Block Frequency Analysis ------------------===//
Jakub Staszak668c6fa2011-06-23 21:56:59 +00002//
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 Staszak875ebd52011-07-25 19:25:40 +000014#include "llvm/Analysis/BlockFrequencyInfo.h"
Duncan P. N. Exon Smith689a5072014-04-11 23:20:58 +000015#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Analysis/BranchProbabilityInfo.h"
Jakub Staszak668c6fa2011-06-23 21:56:59 +000017#include "llvm/Analysis/LoopInfo.h"
18#include "llvm/Analysis/Passes.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000019#include "llvm/IR/CFG.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/InitializePasses.h"
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/GraphWriter.h"
Jakub Staszak668c6fa2011-06-23 21:56:59 +000024
25using namespace llvm;
26
Chandler Carruthf1221bd2014-04-22 02:48:03 +000027#define DEBUG_TYPE "block-freq"
28
Xinliang David Li8dd5ce92016-06-28 04:07:03 +000029static cl::opt<GVDAGType> ViewBlockFreqPropagationDAG(
30 "view-block-freq-propagation-dags", cl::Hidden,
31 cl::desc("Pop up a window to show a dag displaying how block "
32 "frequencies propagation through the CFG."),
33 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
34 clEnumValN(GVDT_Fraction, "fraction",
35 "display a graph using the "
36 "fractional block frequency representation."),
37 clEnumValN(GVDT_Integer, "integer",
38 "display a graph using the raw "
39 "integer fractional block frequency representation."),
40 clEnumValN(GVDT_Count, "count", "display a graph using the real "
Mehdi Amini732afdd2016-10-08 19:41:06 +000041 "profile count if available.")));
Xinliang David Li8dd5ce92016-06-28 04:07:03 +000042
Xinliang David Li3e176c72016-06-28 06:58:21 +000043cl::opt<std::string>
44 ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden,
45 cl::desc("The option to specify "
46 "the name of the function "
47 "whose CFG will be displayed."));
48
49cl::opt<unsigned>
50 ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden,
51 cl::desc("An integer in percent used to specify "
52 "the hot blocks/edges to be displayed "
53 "in red: a block or edge whose frequency "
54 "is no less than the max frequency of the "
55 "function multiplied by this percent."));
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000056
Xinliang David Licb253ce2017-01-23 18:58:24 +000057// Command line option to turn on CFG dot dump after profile annotation.
Xinliang David Li58fcc9b2017-02-02 21:29:17 +000058cl::opt<bool>
59 PGOViewCounts("pgo-view-counts", cl::init(false), cl::Hidden,
60 cl::desc("A boolean option to show CFG dag with "
61 "block profile counts and branch probabilities "
62 "right after PGO profile annotation step. The "
63 "profile counts are computed using branch "
64 "probabilities from the runtime profile data and "
65 "block frequency propagation algorithm. To view "
66 "the raw counts from the profile, use option "
67 "-pgo-view-raw-counts instead. To limit graph "
68 "display to only one function, use filtering option "
69 "-view-bfi-func-name."));
Xinliang David Licb253ce2017-01-23 18:58:24 +000070
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000071namespace llvm {
72
Xinliang David Licb253ce2017-01-23 18:58:24 +000073static GVDAGType getGVDT() {
74
75 if (PGOViewCounts)
76 return GVDT_Count;
77 return ViewBlockFreqPropagationDAG;
78}
79
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000080template <>
81struct GraphTraits<BlockFrequencyInfo *> {
Tim Sheneb3958f2016-08-17 20:07:29 +000082 typedef const BasicBlock *NodeRef;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000083 typedef succ_const_iterator ChildIteratorType;
Tim Shenb5e0f5a2016-08-19 21:20:13 +000084 typedef pointer_iterator<Function::const_iterator> nodes_iterator;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000085
Tim Shen48f814e2016-08-31 16:48:13 +000086 static NodeRef getEntryNode(const BlockFrequencyInfo *G) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +000087 return &G->getFunction()->front();
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000088 }
Tim Shenf2187ed2016-08-22 21:09:30 +000089 static ChildIteratorType child_begin(const NodeRef N) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000090 return succ_begin(N);
91 }
Tim Shenf2187ed2016-08-22 21:09:30 +000092 static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000093 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000094 return nodes_iterator(G->getFunction()->begin());
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000095 }
96 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000097 return nodes_iterator(G->getFunction()->end());
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000098 }
99};
100
Xinliang David Li55415f22016-06-28 03:41:29 +0000101typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>
102 BFIDOTGTraitsBase;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000103
Xinliang David Li55415f22016-06-28 03:41:29 +0000104template <>
105struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
106 explicit DOTGraphTraits(bool isSimple = false)
107 : BFIDOTGTraitsBase(isSimple) {}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000108
109 std::string getNodeLabel(const BasicBlock *Node,
110 const BlockFrequencyInfo *Graph) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000111
Xinliang David Licb253ce2017-01-23 18:58:24 +0000112 return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, getGVDT());
Xinliang David Li55415f22016-06-28 03:41:29 +0000113 }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000114
Xinliang David Li3e176c72016-06-28 06:58:21 +0000115 std::string getNodeAttributes(const BasicBlock *Node,
116 const BlockFrequencyInfo *Graph) {
117 return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
118 ViewHotFreqPercent);
119 }
120
Xinliang David Li55415f22016-06-28 03:41:29 +0000121 std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
122 const BlockFrequencyInfo *BFI) {
Xinliang David Li3e176c72016-06-28 06:58:21 +0000123 return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
124 ViewHotFreqPercent);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000125 }
126};
127
128} // end namespace llvm
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000129
Cong Hou9b4f6b22015-07-16 23:23:35 +0000130BlockFrequencyInfo::BlockFrequencyInfo() {}
131
132BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
133 const BranchProbabilityInfo &BPI,
134 const LoopInfo &LI) {
135 calculate(F, BPI, LI);
136}
137
Xinliang David Li28a93272016-05-05 21:13:27 +0000138BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
139 : BFI(std::move(Arg.BFI)) {}
140
141BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
142 releaseMemory();
143 BFI = std::move(RHS.BFI);
144 return *this;
145}
146
Adam Nemetc2f791d2016-07-13 05:01:48 +0000147// Explicitly define the default constructor otherwise it would be implicitly
148// defined at the first ODR-use which is the BFI member in the
149// LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl
150// template instantiated which is not available in the header.
151BlockFrequencyInfo::~BlockFrequencyInfo() {}
152
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000153bool BlockFrequencyInfo::invalidate(Function &F, const PreservedAnalyses &PA,
154 FunctionAnalysisManager::Invalidator &) {
155 // Check whether the analysis, all analyses on functions, or the function's
156 // CFG have been preserved.
157 auto PAC = PA.getChecker<BlockFrequencyAnalysis>();
158 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
159 PAC.preservedSet<CFGAnalyses>());
160}
161
Wei Mideee61e2015-07-14 23:40:50 +0000162void BlockFrequencyInfo::calculate(const Function &F,
163 const BranchProbabilityInfo &BPI,
164 const LoopInfo &LI) {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000165 if (!BFI)
166 BFI.reset(new ImplType);
Cong Hou5e67b662015-07-15 19:58:26 +0000167 BFI->calculate(F, BPI, LI);
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000168 if (ViewBlockFreqPropagationDAG != GVDT_None &&
169 (ViewBlockFreqFuncName.empty() ||
170 F.getName().equals(ViewBlockFreqFuncName))) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000171 view();
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000172 }
Chandler Carruth343fad42011-10-19 10:12:41 +0000173}
174
Jakub Staszak96f8c552011-12-20 20:03:10 +0000175BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000176 return BFI ? BFI->getBlockFreq(BB) : 0;
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000177}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000178
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000179Optional<uint64_t>
180BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const {
Xinliang David Lib12b3532016-06-22 17:12:12 +0000181 if (!BFI)
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000182 return None;
Xinliang David Lib12b3532016-06-22 17:12:12 +0000183
184 return BFI->getBlockProfileCount(*getFunction(), BB);
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000185}
186
Sean Silvaf8015752016-08-02 02:15:45 +0000187Optional<uint64_t>
188BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
189 if (!BFI)
190 return None;
191 return BFI->getProfileCountFromFreq(*getFunction(), Freq);
192}
193
Xinliang David Lib12b3532016-06-22 17:12:12 +0000194void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
Manman Ren72d44b12015-10-15 14:59:40 +0000195 assert(BFI && "Expected analysis to be available");
196 BFI->setBlockFreq(BB, Freq);
197}
198
Easwaran Raman6c8f5112017-01-19 18:53:16 +0000199void BlockFrequencyInfo::setBlockFreqAndScale(
200 const BasicBlock *ReferenceBB, uint64_t Freq,
201 SmallPtrSetImpl<BasicBlock *> &BlocksToScale) {
202 assert(BFI && "Expected analysis to be available");
203 // Use 128 bits APInt to avoid overflow.
204 APInt NewFreq(128, Freq);
205 APInt OldFreq(128, BFI->getBlockFreq(ReferenceBB).getFrequency());
206 APInt BBFreq(128, 0);
207 for (auto *BB : BlocksToScale) {
208 BBFreq = BFI->getBlockFreq(BB).getFrequency();
209 // Multiply first by NewFreq and then divide by OldFreq
210 // to minimize loss of precision.
211 BBFreq *= NewFreq;
212 // udiv is an expensive operation in the general case. If this ends up being
213 // a hot spot, one of the options proposed in
214 // https://reviews.llvm.org/D28535#650071 could be used to avoid this.
215 BBFreq = BBFreq.udiv(OldFreq);
216 BFI->setBlockFreq(BB, BBFreq.getLimitedValue());
217 }
218 BFI->setBlockFreq(ReferenceBB, Freq);
219}
220
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000221/// Pop up a ghostview window with the current block frequency propagation
222/// rendered using dot.
223void BlockFrequencyInfo::view() const {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000224 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000225}
226
227const Function *BlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000228 return BFI ? BFI->getFunction() : nullptr;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000229}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000230
Xinliang David Li55415f22016-06-28 03:41:29 +0000231const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
232 return BFI ? &BFI->getBPI() : nullptr;
233}
234
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000235raw_ostream &BlockFrequencyInfo::
236printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000237 return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000238}
239
240raw_ostream &
241BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
242 const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000243 return BFI ? BFI->printBlockFreq(OS, BB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000244}
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000245
246uint64_t BlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000247 return BFI ? BFI->getEntryFreq() : 0;
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000248}
Wei Mideee61e2015-07-14 23:40:50 +0000249
250void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
251
252void BlockFrequencyInfo::print(raw_ostream &OS) const {
253 if (BFI)
254 BFI->print(OS);
255}
256
257
258INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
259 "Block Frequency Analysis", true, true)
Cong Houab23bfb2015-07-15 22:48:29 +0000260INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
Wei Mideee61e2015-07-14 23:40:50 +0000261INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
262INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
263 "Block Frequency Analysis", true, true)
264
265char BlockFrequencyInfoWrapperPass::ID = 0;
266
267
268BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
269 : FunctionPass(ID) {
270 initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
271}
272
273BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {}
274
275void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
276 const Module *) const {
277 BFI.print(OS);
278}
279
280void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Cong Houab23bfb2015-07-15 22:48:29 +0000281 AU.addRequired<BranchProbabilityInfoWrapperPass>();
Wei Mideee61e2015-07-14 23:40:50 +0000282 AU.addRequired<LoopInfoWrapperPass>();
283 AU.setPreservesAll();
284}
285
286void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
287
288bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
Cong Houab23bfb2015-07-15 22:48:29 +0000289 BranchProbabilityInfo &BPI =
290 getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
Wei Mideee61e2015-07-14 23:40:50 +0000291 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
292 BFI.calculate(F, BPI, LI);
293 return false;
294}
Xinliang David Li28a93272016-05-05 21:13:27 +0000295
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000296AnalysisKey BlockFrequencyAnalysis::Key;
Xinliang David Li28a93272016-05-05 21:13:27 +0000297BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000298 FunctionAnalysisManager &AM) {
Xinliang David Li28a93272016-05-05 21:13:27 +0000299 BlockFrequencyInfo BFI;
300 BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
301 AM.getResult<LoopAnalysis>(F));
302 return BFI;
303}
304
305PreservedAnalyses
Sean Silva36e0d012016-08-09 00:28:15 +0000306BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
Xinliang David Li28a93272016-05-05 21:13:27 +0000307 OS << "Printing analysis results of BFI for function "
308 << "'" << F.getName() << "':"
309 << "\n";
310 AM.getResult<BlockFrequencyAnalysis>(F).print(OS);
311 return PreservedAnalyses::all();
312}