blob: 600f945a8b2dc34e811c4e36d589b9144d661f1f [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;
Tim Sheneb3958f2016-08-17 20:07:29 +000064 typedef const BasicBlock *NodeRef;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000065 typedef succ_const_iterator ChildIteratorType;
66 typedef Function::const_iterator nodes_iterator;
67
68 static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +000069 return &G->getFunction()->front();
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000070 }
71 static ChildIteratorType child_begin(const NodeType *N) {
72 return succ_begin(N);
73 }
74 static ChildIteratorType child_end(const NodeType *N) {
75 return succ_end(N);
76 }
77 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
78 return G->getFunction()->begin();
79 }
80 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
81 return G->getFunction()->end();
82 }
83};
84
Xinliang David Li55415f22016-06-28 03:41:29 +000085typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>
86 BFIDOTGTraitsBase;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000087
Xinliang David Li55415f22016-06-28 03:41:29 +000088template <>
89struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
90 explicit DOTGraphTraits(bool isSimple = false)
91 : BFIDOTGTraitsBase(isSimple) {}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000092
93 std::string getNodeLabel(const BasicBlock *Node,
94 const BlockFrequencyInfo *Graph) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000095
Xinliang David Li55415f22016-06-28 03:41:29 +000096 return BFIDOTGTraitsBase::getNodeLabel(Node, Graph,
97 ViewBlockFreqPropagationDAG);
98 }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000099
Xinliang David Li3e176c72016-06-28 06:58:21 +0000100 std::string getNodeAttributes(const BasicBlock *Node,
101 const BlockFrequencyInfo *Graph) {
102 return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
103 ViewHotFreqPercent);
104 }
105
Xinliang David Li55415f22016-06-28 03:41:29 +0000106 std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
107 const BlockFrequencyInfo *BFI) {
Xinliang David Li3e176c72016-06-28 06:58:21 +0000108 return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
109 ViewHotFreqPercent);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000110 }
111};
112
113} // end namespace llvm
114#endif
115
Cong Hou9b4f6b22015-07-16 23:23:35 +0000116BlockFrequencyInfo::BlockFrequencyInfo() {}
117
118BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
119 const BranchProbabilityInfo &BPI,
120 const LoopInfo &LI) {
121 calculate(F, BPI, LI);
122}
123
Xinliang David Li28a93272016-05-05 21:13:27 +0000124BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
125 : BFI(std::move(Arg.BFI)) {}
126
127BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
128 releaseMemory();
129 BFI = std::move(RHS.BFI);
130 return *this;
131}
132
Adam Nemetc2f791d2016-07-13 05:01:48 +0000133// Explicitly define the default constructor otherwise it would be implicitly
134// defined at the first ODR-use which is the BFI member in the
135// LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl
136// template instantiated which is not available in the header.
137BlockFrequencyInfo::~BlockFrequencyInfo() {}
138
Wei Mideee61e2015-07-14 23:40:50 +0000139void BlockFrequencyInfo::calculate(const Function &F,
140 const BranchProbabilityInfo &BPI,
141 const LoopInfo &LI) {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000142 if (!BFI)
143 BFI.reset(new ImplType);
Cong Hou5e67b662015-07-15 19:58:26 +0000144 BFI->calculate(F, BPI, LI);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000145#ifndef NDEBUG
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000146 if (ViewBlockFreqPropagationDAG != GVDT_None &&
147 (ViewBlockFreqFuncName.empty() ||
148 F.getName().equals(ViewBlockFreqFuncName))) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000149 view();
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000150 }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000151#endif
Chandler Carruth343fad42011-10-19 10:12:41 +0000152}
153
Jakub Staszak96f8c552011-12-20 20:03:10 +0000154BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000155 return BFI ? BFI->getBlockFreq(BB) : 0;
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000156}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000157
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000158Optional<uint64_t>
159BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const {
Xinliang David Lib12b3532016-06-22 17:12:12 +0000160 if (!BFI)
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000161 return None;
Xinliang David Lib12b3532016-06-22 17:12:12 +0000162
163 return BFI->getBlockProfileCount(*getFunction(), BB);
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000164}
165
Sean Silvaf8015752016-08-02 02:15:45 +0000166Optional<uint64_t>
167BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
168 if (!BFI)
169 return None;
170 return BFI->getProfileCountFromFreq(*getFunction(), Freq);
171}
172
Xinliang David Lib12b3532016-06-22 17:12:12 +0000173void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
Manman Ren72d44b12015-10-15 14:59:40 +0000174 assert(BFI && "Expected analysis to be available");
175 BFI->setBlockFreq(BB, Freq);
176}
177
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000178/// Pop up a ghostview window with the current block frequency propagation
179/// rendered using dot.
180void BlockFrequencyInfo::view() const {
181// This code is only for debugging.
182#ifndef NDEBUG
183 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
184#else
185 errs() << "BlockFrequencyInfo::view is only available in debug builds on "
186 "systems with Graphviz or gv!\n";
187#endif // NDEBUG
188}
189
190const Function *BlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000191 return BFI ? BFI->getFunction() : nullptr;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000192}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000193
Xinliang David Li55415f22016-06-28 03:41:29 +0000194const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
195 return BFI ? &BFI->getBPI() : nullptr;
196}
197
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000198raw_ostream &BlockFrequencyInfo::
199printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000200 return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000201}
202
203raw_ostream &
204BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
205 const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000206 return BFI ? BFI->printBlockFreq(OS, BB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000207}
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000208
209uint64_t BlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000210 return BFI ? BFI->getEntryFreq() : 0;
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000211}
Wei Mideee61e2015-07-14 23:40:50 +0000212
213void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
214
215void BlockFrequencyInfo::print(raw_ostream &OS) const {
216 if (BFI)
217 BFI->print(OS);
218}
219
220
221INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
222 "Block Frequency Analysis", true, true)
Cong Houab23bfb2015-07-15 22:48:29 +0000223INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
Wei Mideee61e2015-07-14 23:40:50 +0000224INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
225INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
226 "Block Frequency Analysis", true, true)
227
228char BlockFrequencyInfoWrapperPass::ID = 0;
229
230
231BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
232 : FunctionPass(ID) {
233 initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
234}
235
236BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {}
237
238void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
239 const Module *) const {
240 BFI.print(OS);
241}
242
243void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Cong Houab23bfb2015-07-15 22:48:29 +0000244 AU.addRequired<BranchProbabilityInfoWrapperPass>();
Wei Mideee61e2015-07-14 23:40:50 +0000245 AU.addRequired<LoopInfoWrapperPass>();
246 AU.setPreservesAll();
247}
248
249void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
250
251bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
Cong Houab23bfb2015-07-15 22:48:29 +0000252 BranchProbabilityInfo &BPI =
253 getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
Wei Mideee61e2015-07-14 23:40:50 +0000254 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
255 BFI.calculate(F, BPI, LI);
256 return false;
257}
Xinliang David Li28a93272016-05-05 21:13:27 +0000258
259char BlockFrequencyAnalysis::PassID;
260BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000261 FunctionAnalysisManager &AM) {
Xinliang David Li28a93272016-05-05 21:13:27 +0000262 BlockFrequencyInfo BFI;
263 BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
264 AM.getResult<LoopAnalysis>(F));
265 return BFI;
266}
267
268PreservedAnalyses
Sean Silva36e0d012016-08-09 00:28:15 +0000269BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
Xinliang David Li28a93272016-05-05 21:13:27 +0000270 OS << "Printing analysis results of BFI for function "
271 << "'" << F.getName() << "':"
272 << "\n";
273 AM.getResult<BlockFrequencyAnalysis>(F).print(OS);
274 return PreservedAnalyses::all();
275}