blob: 4fef855398ea793134fd588162fd4c493140cc39 [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 *> {
63 typedef const BasicBlock NodeType;
64 typedef succ_const_iterator ChildIteratorType;
65 typedef Function::const_iterator nodes_iterator;
66
67 static inline const NodeType *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 }
70 static ChildIteratorType child_begin(const NodeType *N) {
71 return succ_begin(N);
72 }
73 static ChildIteratorType child_end(const NodeType *N) {
74 return succ_end(N);
75 }
76 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
77 return G->getFunction()->begin();
78 }
79 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
80 return G->getFunction()->end();
81 }
82};
83
Xinliang David Li55415f22016-06-28 03:41:29 +000084typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>
85 BFIDOTGTraitsBase;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000086
Xinliang David Li55415f22016-06-28 03:41:29 +000087template <>
88struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
89 explicit DOTGraphTraits(bool isSimple = false)
90 : BFIDOTGTraitsBase(isSimple) {}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000091
92 std::string getNodeLabel(const BasicBlock *Node,
93 const BlockFrequencyInfo *Graph) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000094
Xinliang David Li55415f22016-06-28 03:41:29 +000095 return BFIDOTGTraitsBase::getNodeLabel(Node, Graph,
96 ViewBlockFreqPropagationDAG);
97 }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000098
Xinliang David Li3e176c72016-06-28 06:58:21 +000099 std::string getNodeAttributes(const BasicBlock *Node,
100 const BlockFrequencyInfo *Graph) {
101 return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
102 ViewHotFreqPercent);
103 }
104
Xinliang David Li55415f22016-06-28 03:41:29 +0000105 std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
106 const BlockFrequencyInfo *BFI) {
Xinliang David Li3e176c72016-06-28 06:58:21 +0000107 return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
108 ViewHotFreqPercent);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000109 }
110};
111
112} // end namespace llvm
113#endif
114
Cong Hou9b4f6b22015-07-16 23:23:35 +0000115BlockFrequencyInfo::BlockFrequencyInfo() {}
116
117BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
118 const BranchProbabilityInfo &BPI,
119 const LoopInfo &LI) {
120 calculate(F, BPI, LI);
121}
122
Xinliang David Li28a93272016-05-05 21:13:27 +0000123BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
124 : BFI(std::move(Arg.BFI)) {}
125
126BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
127 releaseMemory();
128 BFI = std::move(RHS.BFI);
129 return *this;
130}
131
Adam Nemetc2f791d2016-07-13 05:01:48 +0000132// Explicitly define the default constructor otherwise it would be implicitly
133// defined at the first ODR-use which is the BFI member in the
134// LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl
135// template instantiated which is not available in the header.
136BlockFrequencyInfo::~BlockFrequencyInfo() {}
137
Wei Mideee61e2015-07-14 23:40:50 +0000138void BlockFrequencyInfo::calculate(const Function &F,
139 const BranchProbabilityInfo &BPI,
140 const LoopInfo &LI) {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000141 if (!BFI)
142 BFI.reset(new ImplType);
Cong Hou5e67b662015-07-15 19:58:26 +0000143 BFI->calculate(F, BPI, LI);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000144#ifndef NDEBUG
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000145 if (ViewBlockFreqPropagationDAG != GVDT_None &&
146 (ViewBlockFreqFuncName.empty() ||
147 F.getName().equals(ViewBlockFreqFuncName))) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000148 view();
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000149 }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000150#endif
Chandler Carruth343fad42011-10-19 10:12:41 +0000151}
152
Jakub Staszak96f8c552011-12-20 20:03:10 +0000153BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000154 return BFI ? BFI->getBlockFreq(BB) : 0;
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000155}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000156
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000157Optional<uint64_t>
158BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const {
Xinliang David Lib12b3532016-06-22 17:12:12 +0000159 if (!BFI)
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000160 return None;
Xinliang David Lib12b3532016-06-22 17:12:12 +0000161
162 return BFI->getBlockProfileCount(*getFunction(), BB);
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000163}
164
Sean Silvaf8015752016-08-02 02:15:45 +0000165Optional<uint64_t>
166BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
167 if (!BFI)
168 return None;
169 return BFI->getProfileCountFromFreq(*getFunction(), Freq);
170}
171
Xinliang David Lib12b3532016-06-22 17:12:12 +0000172void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
Manman Ren72d44b12015-10-15 14:59:40 +0000173 assert(BFI && "Expected analysis to be available");
174 BFI->setBlockFreq(BB, Freq);
175}
176
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000177/// Pop up a ghostview window with the current block frequency propagation
178/// rendered using dot.
179void BlockFrequencyInfo::view() const {
180// This code is only for debugging.
181#ifndef NDEBUG
182 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
183#else
184 errs() << "BlockFrequencyInfo::view is only available in debug builds on "
185 "systems with Graphviz or gv!\n";
186#endif // NDEBUG
187}
188
189const Function *BlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000190 return BFI ? BFI->getFunction() : nullptr;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000191}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000192
Xinliang David Li55415f22016-06-28 03:41:29 +0000193const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
194 return BFI ? &BFI->getBPI() : nullptr;
195}
196
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000197raw_ostream &BlockFrequencyInfo::
198printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000199 return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000200}
201
202raw_ostream &
203BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
204 const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000205 return BFI ? BFI->printBlockFreq(OS, BB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000206}
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000207
208uint64_t BlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000209 return BFI ? BFI->getEntryFreq() : 0;
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000210}
Wei Mideee61e2015-07-14 23:40:50 +0000211
212void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
213
214void BlockFrequencyInfo::print(raw_ostream &OS) const {
215 if (BFI)
216 BFI->print(OS);
217}
218
219
220INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
221 "Block Frequency Analysis", true, true)
Cong Houab23bfb2015-07-15 22:48:29 +0000222INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
Wei Mideee61e2015-07-14 23:40:50 +0000223INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
224INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
225 "Block Frequency Analysis", true, true)
226
227char BlockFrequencyInfoWrapperPass::ID = 0;
228
229
230BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
231 : FunctionPass(ID) {
232 initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
233}
234
235BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {}
236
237void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
238 const Module *) const {
239 BFI.print(OS);
240}
241
242void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Cong Houab23bfb2015-07-15 22:48:29 +0000243 AU.addRequired<BranchProbabilityInfoWrapperPass>();
Wei Mideee61e2015-07-14 23:40:50 +0000244 AU.addRequired<LoopInfoWrapperPass>();
245 AU.setPreservesAll();
246}
247
248void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
249
250bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
Cong Houab23bfb2015-07-15 22:48:29 +0000251 BranchProbabilityInfo &BPI =
252 getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
Wei Mideee61e2015-07-14 23:40:50 +0000253 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
254 BFI.calculate(F, BPI, LI);
255 return false;
256}
Xinliang David Li28a93272016-05-05 21:13:27 +0000257
258char BlockFrequencyAnalysis::PassID;
259BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000260 FunctionAnalysisManager &AM) {
Xinliang David Li28a93272016-05-05 21:13:27 +0000261 BlockFrequencyInfo BFI;
262 BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
263 AM.getResult<LoopAnalysis>(F));
264 return BFI;
265}
266
267PreservedAnalyses
Sean Silva36e0d012016-08-09 00:28:15 +0000268BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
Xinliang David Li28a93272016-05-05 21:13:27 +0000269 OS << "Printing analysis results of BFI for function "
270 << "'" << F.getName() << "':"
271 << "\n";
272 AM.getResult<BlockFrequencyAnalysis>(F).print(OS);
273 return PreservedAnalyses::all();
274}