xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/BlockFrequencyInfo.cpp (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
1 //===- BlockFrequencyInfo.cpp - Block Frequency Analysis ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Loops should be simplified before this analysis.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Analysis/BlockFrequencyInfo.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/iterator.h"
17 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
18 #include "llvm/Analysis/BranchProbabilityInfo.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/IR/CFG.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/InitializePasses.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/GraphWriter.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <cassert>
29 #include <string>
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "block-freq"
34 
35 static cl::opt<GVDAGType> ViewBlockFreqPropagationDAG(
36     "view-block-freq-propagation-dags", cl::Hidden,
37     cl::desc("Pop up a window to show a dag displaying how block "
38              "frequencies propagation through the CFG."),
39     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
40                clEnumValN(GVDT_Fraction, "fraction",
41                           "display a graph using the "
42                           "fractional block frequency representation."),
43                clEnumValN(GVDT_Integer, "integer",
44                           "display a graph using the raw "
45                           "integer fractional block frequency representation."),
46                clEnumValN(GVDT_Count, "count", "display a graph using the real "
47                                                "profile count if available.")));
48 
49 namespace llvm {
50 cl::opt<std::string>
51     ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden,
52                           cl::desc("The option to specify "
53                                    "the name of the function "
54                                    "whose CFG will be displayed."));
55 
56 cl::opt<unsigned>
57     ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden,
58                        cl::desc("An integer in percent used to specify "
59                                 "the hot blocks/edges to be displayed "
60                                 "in red: a block or edge whose frequency "
61                                 "is no less than the max frequency of the "
62                                 "function multiplied by this percent."));
63 
64 // Command line option to turn on CFG dot or text dump after profile annotation.
65 cl::opt<PGOViewCountsType> PGOViewCounts(
66     "pgo-view-counts", cl::Hidden,
67     cl::desc("A boolean option to show CFG dag or text with "
68              "block profile counts and branch probabilities "
69              "right after PGO profile annotation step. The "
70              "profile counts are computed using branch "
71              "probabilities from the runtime profile data and "
72              "block frequency propagation algorithm. To view "
73              "the raw counts from the profile, use option "
74              "-pgo-view-raw-counts instead. To limit graph "
75              "display to only one function, use filtering option "
76              "-view-bfi-func-name."),
77     cl::values(clEnumValN(PGOVCT_None, "none", "do not show."),
78                clEnumValN(PGOVCT_Graph, "graph", "show a graph."),
79                clEnumValN(PGOVCT_Text, "text", "show in text.")));
80 
81 static cl::opt<bool> PrintBlockFreq(
82     "print-bfi", cl::init(false), cl::Hidden,
83     cl::desc("Print the block frequency info."));
84 
85 cl::opt<std::string> PrintBlockFreqFuncName(
86     "print-bfi-func-name", cl::Hidden,
87     cl::desc("The option to specify the name of the function "
88              "whose block frequency info is printed."));
89 } // namespace llvm
90 
91 namespace llvm {
92 
93 static GVDAGType getGVDT() {
94   if (PGOViewCounts == PGOVCT_Graph)
95     return GVDT_Count;
96   return ViewBlockFreqPropagationDAG;
97 }
98 
99 template <>
100 struct GraphTraits<BlockFrequencyInfo *> {
101   using NodeRef = const BasicBlock *;
102   using ChildIteratorType = const_succ_iterator;
103   using nodes_iterator = pointer_iterator<Function::const_iterator>;
104 
105   static NodeRef getEntryNode(const BlockFrequencyInfo *G) {
106     return &G->getFunction()->front();
107   }
108 
109   static ChildIteratorType child_begin(const NodeRef N) {
110     return succ_begin(N);
111   }
112 
113   static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); }
114 
115   static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
116     return nodes_iterator(G->getFunction()->begin());
117   }
118 
119   static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
120     return nodes_iterator(G->getFunction()->end());
121   }
122 };
123 
124 using BFIDOTGTraitsBase =
125     BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>;
126 
127 template <>
128 struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
129   explicit DOTGraphTraits(bool isSimple = false)
130       : BFIDOTGTraitsBase(isSimple) {}
131 
132   std::string getNodeLabel(const BasicBlock *Node,
133                            const BlockFrequencyInfo *Graph) {
134 
135     return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, getGVDT());
136   }
137 
138   std::string getNodeAttributes(const BasicBlock *Node,
139                                 const BlockFrequencyInfo *Graph) {
140     return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
141                                                 ViewHotFreqPercent);
142   }
143 
144   std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
145                                 const BlockFrequencyInfo *BFI) {
146     return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
147                                                 ViewHotFreqPercent);
148   }
149 };
150 
151 } // end namespace llvm
152 
153 BlockFrequencyInfo::BlockFrequencyInfo() = default;
154 
155 BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
156                                        const BranchProbabilityInfo &BPI,
157                                        const LoopInfo &LI) {
158   calculate(F, BPI, LI);
159 }
160 
161 BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
162     : BFI(std::move(Arg.BFI)) {}
163 
164 BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
165   releaseMemory();
166   BFI = std::move(RHS.BFI);
167   return *this;
168 }
169 
170 // Explicitly define the default constructor otherwise it would be implicitly
171 // defined at the first ODR-use which is the BFI member in the
172 // LazyBlockFrequencyInfo header.  The dtor needs the BlockFrequencyInfoImpl
173 // template instantiated which is not available in the header.
174 BlockFrequencyInfo::~BlockFrequencyInfo() = default;
175 
176 bool BlockFrequencyInfo::invalidate(Function &F, const PreservedAnalyses &PA,
177                                     FunctionAnalysisManager::Invalidator &) {
178   // Check whether the analysis, all analyses on functions, or the function's
179   // CFG have been preserved.
180   auto PAC = PA.getChecker<BlockFrequencyAnalysis>();
181   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
182            PAC.preservedSet<CFGAnalyses>());
183 }
184 
185 void BlockFrequencyInfo::calculate(const Function &F,
186                                    const BranchProbabilityInfo &BPI,
187                                    const LoopInfo &LI) {
188   if (!BFI)
189     BFI.reset(new ImplType);
190   BFI->calculate(F, BPI, LI);
191   if (ViewBlockFreqPropagationDAG != GVDT_None &&
192       (ViewBlockFreqFuncName.empty() ||
193        F.getName().equals(ViewBlockFreqFuncName))) {
194     view();
195   }
196   if (PrintBlockFreq &&
197       (PrintBlockFreqFuncName.empty() ||
198        F.getName().equals(PrintBlockFreqFuncName))) {
199     print(dbgs());
200   }
201 }
202 
203 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
204   return BFI ? BFI->getBlockFreq(BB) : 0;
205 }
206 
207 Optional<uint64_t>
208 BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB,
209                                          bool AllowSynthetic) const {
210   if (!BFI)
211     return None;
212 
213   return BFI->getBlockProfileCount(*getFunction(), BB, AllowSynthetic);
214 }
215 
216 Optional<uint64_t>
217 BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
218   if (!BFI)
219     return None;
220   return BFI->getProfileCountFromFreq(*getFunction(), Freq);
221 }
222 
223 bool BlockFrequencyInfo::isIrrLoopHeader(const BasicBlock *BB) {
224   assert(BFI && "Expected analysis to be available");
225   return BFI->isIrrLoopHeader(BB);
226 }
227 
228 void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
229   assert(BFI && "Expected analysis to be available");
230   BFI->setBlockFreq(BB, Freq);
231 }
232 
233 void BlockFrequencyInfo::setBlockFreqAndScale(
234     const BasicBlock *ReferenceBB, uint64_t Freq,
235     SmallPtrSetImpl<BasicBlock *> &BlocksToScale) {
236   assert(BFI && "Expected analysis to be available");
237   // Use 128 bits APInt to avoid overflow.
238   APInt NewFreq(128, Freq);
239   APInt OldFreq(128, BFI->getBlockFreq(ReferenceBB).getFrequency());
240   APInt BBFreq(128, 0);
241   for (auto *BB : BlocksToScale) {
242     BBFreq = BFI->getBlockFreq(BB).getFrequency();
243     // Multiply first by NewFreq and then divide by OldFreq
244     // to minimize loss of precision.
245     BBFreq *= NewFreq;
246     // udiv is an expensive operation in the general case. If this ends up being
247     // a hot spot, one of the options proposed in
248     // https://reviews.llvm.org/D28535#650071 could be used to avoid this.
249     BBFreq = BBFreq.udiv(OldFreq);
250     BFI->setBlockFreq(BB, BBFreq.getLimitedValue());
251   }
252   BFI->setBlockFreq(ReferenceBB, Freq);
253 }
254 
255 /// Pop up a ghostview window with the current block frequency propagation
256 /// rendered using dot.
257 void BlockFrequencyInfo::view(StringRef title) const {
258   ViewGraph(const_cast<BlockFrequencyInfo *>(this), title);
259 }
260 
261 const Function *BlockFrequencyInfo::getFunction() const {
262   return BFI ? BFI->getFunction() : nullptr;
263 }
264 
265 const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
266   return BFI ? &BFI->getBPI() : nullptr;
267 }
268 
269 raw_ostream &BlockFrequencyInfo::
270 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
271   return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
272 }
273 
274 raw_ostream &
275 BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
276                                    const BasicBlock *BB) const {
277   return BFI ? BFI->printBlockFreq(OS, BB) : OS;
278 }
279 
280 uint64_t BlockFrequencyInfo::getEntryFreq() const {
281   return BFI ? BFI->getEntryFreq() : 0;
282 }
283 
284 void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
285 
286 void BlockFrequencyInfo::print(raw_ostream &OS) const {
287   if (BFI)
288     BFI->print(OS);
289 }
290 
291 void BlockFrequencyInfo::verifyMatch(BlockFrequencyInfo &Other) const {
292   if (BFI)
293     BFI->verifyMatch(*Other.BFI);
294 }
295 
296 INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
297                       "Block Frequency Analysis", true, true)
298 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
299 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
300 INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
301                     "Block Frequency Analysis", true, true)
302 
303 char BlockFrequencyInfoWrapperPass::ID = 0;
304 
305 BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
306     : FunctionPass(ID) {
307   initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
308 }
309 
310 BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() = default;
311 
312 void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
313                                           const Module *) const {
314   BFI.print(OS);
315 }
316 
317 void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
318   AU.addRequired<BranchProbabilityInfoWrapperPass>();
319   AU.addRequired<LoopInfoWrapperPass>();
320   AU.setPreservesAll();
321 }
322 
323 void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
324 
325 bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
326   BranchProbabilityInfo &BPI =
327       getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
328   LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
329   BFI.calculate(F, BPI, LI);
330   return false;
331 }
332 
333 AnalysisKey BlockFrequencyAnalysis::Key;
334 BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
335                                                FunctionAnalysisManager &AM) {
336   BlockFrequencyInfo BFI;
337   BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
338                 AM.getResult<LoopAnalysis>(F));
339   return BFI;
340 }
341 
342 PreservedAnalyses
343 BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
344   OS << "Printing analysis results of BFI for function "
345      << "'" << F.getName() << "':"
346      << "\n";
347   AM.getResult<BlockFrequencyAnalysis>(F).print(OS);
348   return PreservedAnalyses::all();
349 }
350