1 //===- BlockFrequencyInfo.cpp - Block Frequency Analysis ------------------===//
2 //
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 
14 #include "llvm/Analysis/BlockFrequencyInfo.h"
15 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
16 #include "llvm/Analysis/BranchProbabilityInfo.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/Passes.h"
19 #include "llvm/IR/CFG.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/GraphWriter.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "block-freq"
28 
29 #ifndef NDEBUG
30 static 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 
44 cl::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 
50 cl::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."));
57 
58 // Command line option to turn on CFG dot dump after profile annotation.
59 cl::opt<bool> PGOViewCounts("pgo-view-counts", cl::init(false), cl::Hidden);
60 
61 namespace llvm {
62 
63 static GVDAGType getGVDT() {
64 
65   if (PGOViewCounts)
66     return GVDT_Count;
67   return ViewBlockFreqPropagationDAG;
68 }
69 
70 template <>
71 struct GraphTraits<BlockFrequencyInfo *> {
72   typedef const BasicBlock *NodeRef;
73   typedef succ_const_iterator ChildIteratorType;
74   typedef pointer_iterator<Function::const_iterator> nodes_iterator;
75 
76   static NodeRef getEntryNode(const BlockFrequencyInfo *G) {
77     return &G->getFunction()->front();
78   }
79   static ChildIteratorType child_begin(const NodeRef N) {
80     return succ_begin(N);
81   }
82   static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); }
83   static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
84     return nodes_iterator(G->getFunction()->begin());
85   }
86   static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
87     return nodes_iterator(G->getFunction()->end());
88   }
89 };
90 
91 typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>
92     BFIDOTGTraitsBase;
93 
94 template <>
95 struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
96   explicit DOTGraphTraits(bool isSimple = false)
97       : BFIDOTGTraitsBase(isSimple) {}
98 
99   std::string getNodeLabel(const BasicBlock *Node,
100                            const BlockFrequencyInfo *Graph) {
101 
102     return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, getGVDT());
103   }
104 
105   std::string getNodeAttributes(const BasicBlock *Node,
106                                 const BlockFrequencyInfo *Graph) {
107     return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
108                                                 ViewHotFreqPercent);
109   }
110 
111   std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
112                                 const BlockFrequencyInfo *BFI) {
113     return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
114                                                 ViewHotFreqPercent);
115   }
116 };
117 
118 } // end namespace llvm
119 #endif
120 
121 BlockFrequencyInfo::BlockFrequencyInfo() {}
122 
123 BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
124                                        const BranchProbabilityInfo &BPI,
125                                        const LoopInfo &LI) {
126   calculate(F, BPI, LI);
127 }
128 
129 BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
130     : BFI(std::move(Arg.BFI)) {}
131 
132 BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
133   releaseMemory();
134   BFI = std::move(RHS.BFI);
135   return *this;
136 }
137 
138 // Explicitly define the default constructor otherwise it would be implicitly
139 // defined at the first ODR-use which is the BFI member in the
140 // LazyBlockFrequencyInfo header.  The dtor needs the BlockFrequencyInfoImpl
141 // template instantiated which is not available in the header.
142 BlockFrequencyInfo::~BlockFrequencyInfo() {}
143 
144 bool BlockFrequencyInfo::invalidate(Function &F, const PreservedAnalyses &PA,
145                                     FunctionAnalysisManager::Invalidator &) {
146   // Check whether the analysis, all analyses on functions, or the function's
147   // CFG have been preserved.
148   auto PAC = PA.getChecker<BlockFrequencyAnalysis>();
149   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
150            PAC.preservedSet<CFGAnalyses>());
151 }
152 
153 void BlockFrequencyInfo::calculate(const Function &F,
154                                    const BranchProbabilityInfo &BPI,
155                                    const LoopInfo &LI) {
156   if (!BFI)
157     BFI.reset(new ImplType);
158   BFI->calculate(F, BPI, LI);
159 #ifndef NDEBUG
160   if (ViewBlockFreqPropagationDAG != GVDT_None &&
161       (ViewBlockFreqFuncName.empty() ||
162        F.getName().equals(ViewBlockFreqFuncName))) {
163     view();
164   }
165 #endif
166 }
167 
168 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
169   return BFI ? BFI->getBlockFreq(BB) : 0;
170 }
171 
172 Optional<uint64_t>
173 BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const {
174   if (!BFI)
175     return None;
176 
177   return BFI->getBlockProfileCount(*getFunction(), BB);
178 }
179 
180 Optional<uint64_t>
181 BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
182   if (!BFI)
183     return None;
184   return BFI->getProfileCountFromFreq(*getFunction(), Freq);
185 }
186 
187 void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
188   assert(BFI && "Expected analysis to be available");
189   BFI->setBlockFreq(BB, Freq);
190 }
191 
192 void BlockFrequencyInfo::setBlockFreqAndScale(
193     const BasicBlock *ReferenceBB, uint64_t Freq,
194     SmallPtrSetImpl<BasicBlock *> &BlocksToScale) {
195   assert(BFI && "Expected analysis to be available");
196   // Use 128 bits APInt to avoid overflow.
197   APInt NewFreq(128, Freq);
198   APInt OldFreq(128, BFI->getBlockFreq(ReferenceBB).getFrequency());
199   APInt BBFreq(128, 0);
200   for (auto *BB : BlocksToScale) {
201     BBFreq = BFI->getBlockFreq(BB).getFrequency();
202     // Multiply first by NewFreq and then divide by OldFreq
203     // to minimize loss of precision.
204     BBFreq *= NewFreq;
205     // udiv is an expensive operation in the general case. If this ends up being
206     // a hot spot, one of the options proposed in
207     // https://reviews.llvm.org/D28535#650071 could be used to avoid this.
208     BBFreq = BBFreq.udiv(OldFreq);
209     BFI->setBlockFreq(BB, BBFreq.getLimitedValue());
210   }
211   BFI->setBlockFreq(ReferenceBB, Freq);
212 }
213 
214 /// Pop up a ghostview window with the current block frequency propagation
215 /// rendered using dot.
216 void BlockFrequencyInfo::view() const {
217 // This code is only for debugging.
218 #ifndef NDEBUG
219   ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
220 #else
221   errs() << "BlockFrequencyInfo::view is only available in debug builds on "
222             "systems with Graphviz or gv!\n";
223 #endif // NDEBUG
224 }
225 
226 const Function *BlockFrequencyInfo::getFunction() const {
227   return BFI ? BFI->getFunction() : nullptr;
228 }
229 
230 const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
231   return BFI ? &BFI->getBPI() : nullptr;
232 }
233 
234 raw_ostream &BlockFrequencyInfo::
235 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
236   return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
237 }
238 
239 raw_ostream &
240 BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
241                                    const BasicBlock *BB) const {
242   return BFI ? BFI->printBlockFreq(OS, BB) : OS;
243 }
244 
245 uint64_t BlockFrequencyInfo::getEntryFreq() const {
246   return BFI ? BFI->getEntryFreq() : 0;
247 }
248 
249 void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
250 
251 void BlockFrequencyInfo::print(raw_ostream &OS) const {
252   if (BFI)
253     BFI->print(OS);
254 }
255 
256 
257 INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
258                       "Block Frequency Analysis", true, true)
259 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
260 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
261 INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
262                     "Block Frequency Analysis", true, true)
263 
264 char BlockFrequencyInfoWrapperPass::ID = 0;
265 
266 
267 BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
268     : FunctionPass(ID) {
269   initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
270 }
271 
272 BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {}
273 
274 void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
275                                           const Module *) const {
276   BFI.print(OS);
277 }
278 
279 void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
280   AU.addRequired<BranchProbabilityInfoWrapperPass>();
281   AU.addRequired<LoopInfoWrapperPass>();
282   AU.setPreservesAll();
283 }
284 
285 void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
286 
287 bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
288   BranchProbabilityInfo &BPI =
289       getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
290   LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
291   BFI.calculate(F, BPI, LI);
292   return false;
293 }
294 
295 AnalysisKey BlockFrequencyAnalysis::Key;
296 BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
297                                                FunctionAnalysisManager &AM) {
298   BlockFrequencyInfo BFI;
299   BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
300                 AM.getResult<LoopAnalysis>(F));
301   return BFI;
302 }
303 
304 PreservedAnalyses
305 BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
306   OS << "Printing analysis results of BFI for function "
307      << "'" << F.getName() << "':"
308      << "\n";
309   AM.getResult<BlockFrequencyAnalysis>(F).print(OS);
310   return PreservedAnalyses::all();
311 }
312