blob: 85f437d61daa2621e53f4915c13eb5c10f47c670 [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 "
Mehdi Amini732afdd2016-10-08 19:41:06 +000042 "profile count if available.")));
Xinliang David Li8dd5ce92016-06-28 04:07:03 +000043
Xinliang David Li3e176c72016-06-28 06:58:21 +000044cl::opt<std::string>
45 ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden,
46 cl::desc("The option to specify "
47 "the name of the function "
48 "whose CFG will be displayed."));
49
50cl::opt<unsigned>
51 ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden,
52 cl::desc("An integer in percent used to specify "
53 "the hot blocks/edges to be displayed "
54 "in red: a block or edge whose frequency "
55 "is no less than the max frequency of the "
56 "function multiplied by this percent."));
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000057
58namespace llvm {
59
60template <>
61struct GraphTraits<BlockFrequencyInfo *> {
Tim Sheneb3958f2016-08-17 20:07:29 +000062 typedef const BasicBlock *NodeRef;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000063 typedef succ_const_iterator ChildIteratorType;
Tim Shenb5e0f5a2016-08-19 21:20:13 +000064 typedef pointer_iterator<Function::const_iterator> nodes_iterator;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000065
Tim Shen48f814e2016-08-31 16:48:13 +000066 static NodeRef getEntryNode(const BlockFrequencyInfo *G) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +000067 return &G->getFunction()->front();
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000068 }
Tim Shenf2187ed2016-08-22 21:09:30 +000069 static ChildIteratorType child_begin(const NodeRef N) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000070 return succ_begin(N);
71 }
Tim Shenf2187ed2016-08-22 21:09:30 +000072 static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000073 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000074 return nodes_iterator(G->getFunction()->begin());
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000075 }
76 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000077 return nodes_iterator(G->getFunction()->end());
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000078 }
79};
80
Xinliang David Li55415f22016-06-28 03:41:29 +000081typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>
82 BFIDOTGTraitsBase;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000083
Xinliang David Li55415f22016-06-28 03:41:29 +000084template <>
85struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
86 explicit DOTGraphTraits(bool isSimple = false)
87 : BFIDOTGTraitsBase(isSimple) {}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000088
89 std::string getNodeLabel(const BasicBlock *Node,
90 const BlockFrequencyInfo *Graph) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000091
Xinliang David Li55415f22016-06-28 03:41:29 +000092 return BFIDOTGTraitsBase::getNodeLabel(Node, Graph,
93 ViewBlockFreqPropagationDAG);
94 }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000095
Xinliang David Li3e176c72016-06-28 06:58:21 +000096 std::string getNodeAttributes(const BasicBlock *Node,
97 const BlockFrequencyInfo *Graph) {
98 return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
99 ViewHotFreqPercent);
100 }
101
Xinliang David Li55415f22016-06-28 03:41:29 +0000102 std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
103 const BlockFrequencyInfo *BFI) {
Xinliang David Li3e176c72016-06-28 06:58:21 +0000104 return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
105 ViewHotFreqPercent);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000106 }
107};
108
109} // end namespace llvm
110#endif
111
Cong Hou9b4f6b22015-07-16 23:23:35 +0000112BlockFrequencyInfo::BlockFrequencyInfo() {}
113
114BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
115 const BranchProbabilityInfo &BPI,
116 const LoopInfo &LI) {
117 calculate(F, BPI, LI);
118}
119
Xinliang David Li28a93272016-05-05 21:13:27 +0000120BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
121 : BFI(std::move(Arg.BFI)) {}
122
123BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
124 releaseMemory();
125 BFI = std::move(RHS.BFI);
126 return *this;
127}
128
Adam Nemetc2f791d2016-07-13 05:01:48 +0000129// Explicitly define the default constructor otherwise it would be implicitly
130// defined at the first ODR-use which is the BFI member in the
131// LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl
132// template instantiated which is not available in the header.
133BlockFrequencyInfo::~BlockFrequencyInfo() {}
134
Wei Mideee61e2015-07-14 23:40:50 +0000135void BlockFrequencyInfo::calculate(const Function &F,
136 const BranchProbabilityInfo &BPI,
137 const LoopInfo &LI) {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000138 if (!BFI)
139 BFI.reset(new ImplType);
Cong Hou5e67b662015-07-15 19:58:26 +0000140 BFI->calculate(F, BPI, LI);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000141#ifndef NDEBUG
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000142 if (ViewBlockFreqPropagationDAG != GVDT_None &&
143 (ViewBlockFreqFuncName.empty() ||
144 F.getName().equals(ViewBlockFreqFuncName))) {
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000145 view();
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000146 }
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000147#endif
Chandler Carruth343fad42011-10-19 10:12:41 +0000148}
149
Jakub Staszak96f8c552011-12-20 20:03:10 +0000150BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000151 return BFI ? BFI->getBlockFreq(BB) : 0;
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000152}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000153
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000154Optional<uint64_t>
155BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const {
Xinliang David Lib12b3532016-06-22 17:12:12 +0000156 if (!BFI)
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000157 return None;
Xinliang David Lib12b3532016-06-22 17:12:12 +0000158
159 return BFI->getBlockProfileCount(*getFunction(), BB);
Easwaran Raman12b79aa2016-03-23 18:18:26 +0000160}
161
Sean Silvaf8015752016-08-02 02:15:45 +0000162Optional<uint64_t>
163BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
164 if (!BFI)
165 return None;
166 return BFI->getProfileCountFromFreq(*getFunction(), Freq);
167}
168
Xinliang David Lib12b3532016-06-22 17:12:12 +0000169void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
Manman Ren72d44b12015-10-15 14:59:40 +0000170 assert(BFI && "Expected analysis to be available");
171 BFI->setBlockFreq(BB, Freq);
172}
173
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000174/// Pop up a ghostview window with the current block frequency propagation
175/// rendered using dot.
176void BlockFrequencyInfo::view() const {
177// This code is only for debugging.
178#ifndef NDEBUG
179 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
180#else
181 errs() << "BlockFrequencyInfo::view is only available in debug builds on "
182 "systems with Graphviz or gv!\n";
183#endif // NDEBUG
184}
185
186const Function *BlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000187 return BFI ? BFI->getFunction() : nullptr;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000188}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000189
Xinliang David Li55415f22016-06-28 03:41:29 +0000190const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
191 return BFI ? &BFI->getBPI() : nullptr;
192}
193
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000194raw_ostream &BlockFrequencyInfo::
195printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000196 return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000197}
198
199raw_ostream &
200BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
201 const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000202 return BFI ? BFI->printBlockFreq(OS, BB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000203}
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000204
205uint64_t BlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000206 return BFI ? BFI->getEntryFreq() : 0;
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000207}
Wei Mideee61e2015-07-14 23:40:50 +0000208
209void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
210
211void BlockFrequencyInfo::print(raw_ostream &OS) const {
212 if (BFI)
213 BFI->print(OS);
214}
215
216
217INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
218 "Block Frequency Analysis", true, true)
Cong Houab23bfb2015-07-15 22:48:29 +0000219INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
Wei Mideee61e2015-07-14 23:40:50 +0000220INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
221INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
222 "Block Frequency Analysis", true, true)
223
224char BlockFrequencyInfoWrapperPass::ID = 0;
225
226
227BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
228 : FunctionPass(ID) {
229 initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
230}
231
232BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {}
233
234void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
235 const Module *) const {
236 BFI.print(OS);
237}
238
239void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Cong Houab23bfb2015-07-15 22:48:29 +0000240 AU.addRequired<BranchProbabilityInfoWrapperPass>();
Wei Mideee61e2015-07-14 23:40:50 +0000241 AU.addRequired<LoopInfoWrapperPass>();
242 AU.setPreservesAll();
243}
244
245void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
246
247bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
Cong Houab23bfb2015-07-15 22:48:29 +0000248 BranchProbabilityInfo &BPI =
249 getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
Wei Mideee61e2015-07-14 23:40:50 +0000250 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
251 BFI.calculate(F, BPI, LI);
252 return false;
253}
Xinliang David Li28a93272016-05-05 21:13:27 +0000254
255char BlockFrequencyAnalysis::PassID;
256BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000257 FunctionAnalysisManager &AM) {
Xinliang David Li28a93272016-05-05 21:13:27 +0000258 BlockFrequencyInfo BFI;
259 BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
260 AM.getResult<LoopAnalysis>(F));
261 return BFI;
262}
263
264PreservedAnalyses
Sean Silva36e0d012016-08-09 00:28:15 +0000265BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
Xinliang David Li28a93272016-05-05 21:13:27 +0000266 OS << "Printing analysis results of BFI for function "
267 << "'" << F.getName() << "':"
268 << "\n";
269 AM.getResult<BlockFrequencyAnalysis>(F).print(OS);
270 return PreservedAnalyses::all();
271}