blob: b98869d677ec7fd0f2ceebe5005e1f7f9b12f072 [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
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000029#ifndef NDEBUG
Xinliang David Li8dd5ce92016-06-28 04:07:03 +000030static 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 Li3e176c72016-06-28 06:58:21 +000045cl::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
51cl::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 Gottesmanfd8aee72013-11-14 02:27:46 +000058
59namespace llvm {
60
61template <>
62struct GraphTraits<BlockFrequencyInfo *> {
Tim Sheneb3958f2016-08-17 20:07:29 +000063 typedef const BasicBlock *NodeRef;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000064 typedef succ_const_iterator ChildIteratorType;
Tim Shenb5e0f5a2016-08-19 21:20:13 +000065 typedef pointer_iterator<Function::const_iterator> nodes_iterator;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000066
Tim Shen48f814e2016-08-31 16:48:13 +000067 static NodeRef getEntryNode(const BlockFrequencyInfo *G) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +000068 return &G->getFunction()->front();
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000069 }
Tim Shenf2187ed2016-08-22 21:09:30 +000070 static ChildIteratorType child_begin(const NodeRef N) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000071 return succ_begin(N);
72 }
Tim Shenf2187ed2016-08-22 21:09:30 +000073 static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000074 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000075 return nodes_iterator(G->getFunction()->begin());
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000076 }
77 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000078 return nodes_iterator(G->getFunction()->end());
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000079 }
80};
81
Xinliang David Li55415f22016-06-28 03:41:29 +000082typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>
83 BFIDOTGTraitsBase;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000084
Xinliang David Li55415f22016-06-28 03:41:29 +000085template <>
86struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
87 explicit DOTGraphTraits(bool isSimple = false)
88 : BFIDOTGTraitsBase(isSimple) {}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000089
90 std::string getNodeLabel(const BasicBlock *Node,
91 const BlockFrequencyInfo *Graph) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000092
Xinliang David Li55415f22016-06-28 03:41:29 +000093 return BFIDOTGTraitsBase::getNodeLabel(Node, Graph,
94 ViewBlockFreqPropagationDAG);
95 }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000096
Xinliang David Li3e176c72016-06-28 06:58:21 +000097 std::string getNodeAttributes(const BasicBlock *Node,
98 const BlockFrequencyInfo *Graph) {
99 return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
100 ViewHotFreqPercent);
101 }
102
Xinliang David Li55415f22016-06-28 03:41:29 +0000103 std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
104 const BlockFrequencyInfo *BFI) {
Xinliang David Li3e176c72016-06-28 06:58:21 +0000105 return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
106 ViewHotFreqPercent);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000107 }
108};
109
110} // end namespace llvm
111#endif
112
Cong Hou9b4f6b22015-07-16 23:23:35 +0000113BlockFrequencyInfo::BlockFrequencyInfo() {}
114
115BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
116 const BranchProbabilityInfo &BPI,
117 const LoopInfo &LI) {
118 calculate(F, BPI, LI);
119}
120
Xinliang David Li28a93272016-05-05 21:13:27 +0000121BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
122 : BFI(std::move(Arg.BFI)) {}
123
124BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
125 releaseMemory();
126 BFI = std::move(RHS.BFI);
127 return *this;
128}
129
Adam Nemetc2f791d2016-07-13 05:01:48 +0000130// 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.
134BlockFrequencyInfo::~BlockFrequencyInfo() {}
135
Wei Mideee61e2015-07-14 23:40:50 +0000136void BlockFrequencyInfo::calculate(const Function &F,
137 const BranchProbabilityInfo &BPI,
138 const LoopInfo &LI) {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000139 if (!BFI)
140 BFI.reset(new ImplType);
Cong Hou5e67b662015-07-15 19:58:26 +0000141 BFI->calculate(F, BPI, LI);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000142#ifndef NDEBUG
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000143 if (ViewBlockFreqPropagationDAG != GVDT_None &&
144 (ViewBlockFreqFuncName.empty() ||
145 F.getName().equals(ViewBlockFreqFuncName))) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000146 view();
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000147 }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000148#endif
Chandler Carruth343fad42011-10-19 10:12:41 +0000149}
150
Jakub Staszak96f8c552011-12-20 20:03:10 +0000151BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000152 return BFI ? BFI->getBlockFreq(BB) : 0;
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000153}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000154
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000155Optional<uint64_t>
156BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const {
Xinliang David Lib12b3532016-06-22 17:12:12 +0000157 if (!BFI)
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000158 return None;
Xinliang David Lib12b3532016-06-22 17:12:12 +0000159
160 return BFI->getBlockProfileCount(*getFunction(), BB);
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000161}
162
Sean Silvaf8015752016-08-02 02:15:45 +0000163Optional<uint64_t>
164BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
165 if (!BFI)
166 return None;
167 return BFI->getProfileCountFromFreq(*getFunction(), Freq);
168}
169
Xinliang David Lib12b3532016-06-22 17:12:12 +0000170void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
Manman Ren72d44b12015-10-15 14:59:40 +0000171 assert(BFI && "Expected analysis to be available");
172 BFI->setBlockFreq(BB, Freq);
173}
174
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000175/// Pop up a ghostview window with the current block frequency propagation
176/// rendered using dot.
177void 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
187const Function *BlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000188 return BFI ? BFI->getFunction() : nullptr;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000189}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000190
Xinliang David Li55415f22016-06-28 03:41:29 +0000191const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
192 return BFI ? &BFI->getBPI() : nullptr;
193}
194
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000195raw_ostream &BlockFrequencyInfo::
196printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000197 return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000198}
199
200raw_ostream &
201BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
202 const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000203 return BFI ? BFI->printBlockFreq(OS, BB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000204}
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000205
206uint64_t BlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000207 return BFI ? BFI->getEntryFreq() : 0;
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000208}
Wei Mideee61e2015-07-14 23:40:50 +0000209
210void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
211
212void BlockFrequencyInfo::print(raw_ostream &OS) const {
213 if (BFI)
214 BFI->print(OS);
215}
216
217
218INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
219 "Block Frequency Analysis", true, true)
Cong Houab23bfb2015-07-15 22:48:29 +0000220INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
Wei Mideee61e2015-07-14 23:40:50 +0000221INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
222INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
223 "Block Frequency Analysis", true, true)
224
225char BlockFrequencyInfoWrapperPass::ID = 0;
226
227
228BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
229 : FunctionPass(ID) {
230 initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
231}
232
233BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {}
234
235void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
236 const Module *) const {
237 BFI.print(OS);
238}
239
240void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Cong Houab23bfb2015-07-15 22:48:29 +0000241 AU.addRequired<BranchProbabilityInfoWrapperPass>();
Wei Mideee61e2015-07-14 23:40:50 +0000242 AU.addRequired<LoopInfoWrapperPass>();
243 AU.setPreservesAll();
244}
245
246void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
247
248bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
Cong Houab23bfb2015-07-15 22:48:29 +0000249 BranchProbabilityInfo &BPI =
250 getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
Wei Mideee61e2015-07-14 23:40:50 +0000251 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
252 BFI.calculate(F, BPI, LI);
253 return false;
254}
Xinliang David Li28a93272016-05-05 21:13:27 +0000255
256char BlockFrequencyAnalysis::PassID;
257BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000258 FunctionAnalysisManager &AM) {
Xinliang David Li28a93272016-05-05 21:13:27 +0000259 BlockFrequencyInfo BFI;
260 BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
261 AM.getResult<LoopAnalysis>(F));
262 return BFI;
263}
264
265PreservedAnalyses
Sean Silva36e0d012016-08-09 00:28:15 +0000266BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
Xinliang David Li28a93272016-05-05 21:13:27 +0000267 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}