LLVM  9.0.0svn
BlockFrequencyInfo.cpp
Go to the documentation of this file.
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 
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/iterator.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/Pass.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <string>
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "block-freq"
34 
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 
50  ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden,
51  cl::desc("The option to specify "
52  "the name of the function "
53  "whose CFG will be displayed."));
54 
56  ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden,
57  cl::desc("An integer in percent used to specify "
58  "the hot blocks/edges to be displayed "
59  "in red: a block or edge whose frequency "
60  "is no less than the max frequency of the "
61  "function multiplied by this percent."));
62 
63 // Command line option to turn on CFG dot or text dump after profile annotation.
65  "pgo-view-counts", cl::Hidden,
66  cl::desc("A boolean option to show CFG dag or text with "
67  "block profile counts and branch probabilities "
68  "right after PGO profile annotation step. The "
69  "profile counts are computed using branch "
70  "probabilities from the runtime profile data and "
71  "block frequency propagation algorithm. To view "
72  "the raw counts from the profile, use option "
73  "-pgo-view-raw-counts instead. To limit graph "
74  "display to only one function, use filtering option "
75  "-view-bfi-func-name."),
76  cl::values(clEnumValN(PGOVCT_None, "none", "do not show."),
77  clEnumValN(PGOVCT_Graph, "graph", "show a graph."),
78  clEnumValN(PGOVCT_Text, "text", "show in text.")));
79 
81  "print-bfi", cl::init(false), cl::Hidden,
82  cl::desc("Print the block frequency info."));
83 
85  "print-bfi-func-name", cl::Hidden,
86  cl::desc("The option to specify the name of the function "
87  "whose block frequency info is printed."));
88 
89 namespace llvm {
90 
91 static GVDAGType getGVDT() {
93  return GVDT_Count;
95 }
96 
97 template <>
99  using NodeRef = const BasicBlock *;
102 
104  return &G->getFunction()->front();
105  }
106 
108  return succ_begin(N);
109  }
110 
111  static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); }
112 
114  return nodes_iterator(G->getFunction()->begin());
115  }
116 
118  return nodes_iterator(G->getFunction()->end());
119  }
120 };
121 
122 using BFIDOTGTraitsBase =
124 
125 template <>
127  explicit DOTGraphTraits(bool isSimple = false)
129 
130  std::string getNodeLabel(const BasicBlock *Node,
131  const BlockFrequencyInfo *Graph) {
132 
133  return BFIDOTGTraitsBase::getNodeLabel(Node, Graph, getGVDT());
134  }
135 
136  std::string getNodeAttributes(const BasicBlock *Node,
137  const BlockFrequencyInfo *Graph) {
138  return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
140  }
141 
142  std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
143  const BlockFrequencyInfo *BFI) {
144  return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
146  }
147 };
148 
149 } // end namespace llvm
150 
152 
154  const BranchProbabilityInfo &BPI,
155  const LoopInfo &LI) {
156  calculate(F, BPI, LI);
157 }
158 
160  : BFI(std::move(Arg.BFI)) {}
161 
163  releaseMemory();
164  BFI = std::move(RHS.BFI);
165  return *this;
166 }
167 
168 // Explicitly define the default constructor otherwise it would be implicitly
169 // defined at the first ODR-use which is the BFI member in the
170 // LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl
171 // template instantiated which is not available in the header.
173 
176  // Check whether the analysis, all analyses on functions, or the function's
177  // CFG have been preserved.
178  auto PAC = PA.getChecker<BlockFrequencyAnalysis>();
179  return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
180  PAC.preservedSet<CFGAnalyses>());
181 }
182 
184  const BranchProbabilityInfo &BPI,
185  const LoopInfo &LI) {
186  if (!BFI)
187  BFI.reset(new ImplType);
188  BFI->calculate(F, BPI, LI);
190  (ViewBlockFreqFuncName.empty() ||
192  view();
193  }
194  if (PrintBlockFreq &&
195  (PrintBlockFreqFuncName.empty() ||
197  print(dbgs());
198  }
199 }
200 
202  return BFI ? BFI->getBlockFreq(BB) : 0;
203 }
204 
207  bool AllowSynthetic) const {
208  if (!BFI)
209  return None;
210 
211  return BFI->getBlockProfileCount(*getFunction(), BB, AllowSynthetic);
212 }
213 
216  if (!BFI)
217  return None;
218  return BFI->getProfileCountFromFreq(*getFunction(), Freq);
219 }
220 
222  assert(BFI && "Expected analysis to be available");
223  return BFI->isIrrLoopHeader(BB);
224 }
225 
226 void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
227  assert(BFI && "Expected analysis to be available");
228  BFI->setBlockFreq(BB, Freq);
229 }
230 
232  const BasicBlock *ReferenceBB, uint64_t Freq,
233  SmallPtrSetImpl<BasicBlock *> &BlocksToScale) {
234  assert(BFI && "Expected analysis to be available");
235  // Use 128 bits APInt to avoid overflow.
236  APInt NewFreq(128, Freq);
237  APInt OldFreq(128, BFI->getBlockFreq(ReferenceBB).getFrequency());
238  APInt BBFreq(128, 0);
239  for (auto *BB : BlocksToScale) {
240  BBFreq = BFI->getBlockFreq(BB).getFrequency();
241  // Multiply first by NewFreq and then divide by OldFreq
242  // to minimize loss of precision.
243  BBFreq *= NewFreq;
244  // udiv is an expensive operation in the general case. If this ends up being
245  // a hot spot, one of the options proposed in
246  // https://reviews.llvm.org/D28535#650071 could be used to avoid this.
247  BBFreq = BBFreq.udiv(OldFreq);
248  BFI->setBlockFreq(BB, BBFreq.getLimitedValue());
249  }
250  BFI->setBlockFreq(ReferenceBB, Freq);
251 }
252 
253 /// Pop up a ghostview window with the current block frequency propagation
254 /// rendered using dot.
256  ViewGraph(const_cast<BlockFrequencyInfo *>(this), title);
257 }
258 
260  return BFI ? BFI->getFunction() : nullptr;
261 }
262 
264  return BFI ? &BFI->getBPI() : nullptr;
265 }
266 
268 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
269  return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
270 }
271 
272 raw_ostream &
274  const BasicBlock *BB) const {
275  return BFI ? BFI->printBlockFreq(OS, BB) : OS;
276 }
277 
279  return BFI ? BFI->getEntryFreq() : 0;
280 }
281 
283 
285  if (BFI)
286  BFI->print(OS);
287 }
288 
290  "Block Frequency Analysis", true, true)
294  "Block Frequency Analysis", true, true)
295 
296 char BlockFrequencyInfoWrapperPass::ID = 0;
297 
298 BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
299  : FunctionPass(ID) {
301 }
302 
304 
306  const Module *) const {
307  BFI.print(OS);
308 }
309 
313  AU.setPreservesAll();
314 }
315 
317 
319  BranchProbabilityInfo &BPI =
320  getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
321  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
322  BFI.calculate(F, BPI, LI);
323  return false;
324 }
325 
331  AM.getResult<LoopAnalysis>(F));
332  return BFI;
333 }
334 
337  OS << "Printing analysis results of BFI for function "
338  << "'" << F.getName() << "':"
339  << "\n";
341  return PreservedAnalyses::all();
342 }
Optional< uint64_t > getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic=false) const
Returns the estimated profile count of BB.
static NodeRef getEntryNode(const BlockFrequencyInfo *G)
Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
std::string getNodeLabel(const BasicBlock *Node, const BlockFrequencyInfo *Graph)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:776
This class represents lattice values for constants.
Definition: AllocatorList.h:23
BlockFrequencyInfo & operator=(const BlockFrequencyInfo &)=delete
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
iterator end()
Definition: Function.h:674
cl::opt< unsigned > ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden, cl::desc("An integer in percent used to specify " "the hot blocks/edges to be displayed " "in red: a block or edge whose frequency " "is no less than the max frequency of the " "function multiplied by this percent."))
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1519
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
cl::opt< PGOViewCountsType > PGOViewCounts("pgo-view-counts", cl::Hidden, cl::desc("A boolean option to show CFG dag or text with " "block profile counts and branch probabilities " "right after PGO profile annotation step. The " "profile counts are computed using branch " "probabilities from the runtime profile data and " "block frequency propagation algorithm. To view " "the raw counts from the profile, use option " "-pgo-view-raw-counts instead. To limit graph " "display to only one function, use filtering option " "-view-bfi-func-name."), cl::values(clEnumValN(PGOVCT_None, "none", "do not show."), clEnumValN(PGOVCT_Graph, "graph", "show a graph."), clEnumValN(PGOVCT_Text, "text", "show in text.")))
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
F(f)
block Block Frequency true
void view(StringRef="BlockFrequencyDAGs") const
Pop up a ghostview window with the current block frequency propagation rendered using dot...
SuccIterator< const Instruction, const BasicBlock > succ_const_iterator
Definition: CFG.h:241
static GVDAGType getGVDT()
raw_ostream & printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
*ViewGraph Emit a dot run run gv on the postscript *then cleanup For use from the debugger *void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
Definition: GraphWriter.h:366
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:343
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: PassManager.h:311
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
void initializeBlockFrequencyInfoWrapperPassPass(PassRegistry &)
Definition: BitVector.h:937
Legacy analysis pass which computes BlockFrequencyInfo.
std::string getNodeLabel(NodeRef Node, const BlockFrequencyInfoT *Graph, GVDAGType GType, int layout_order=-1)
void setBlockFreqAndScale(const BasicBlock *ReferenceBB, uint64_t Freq, SmallPtrSetImpl< BasicBlock *> &BlocksToScale)
Set the frequency of ReferenceBB to Freq and scale the frequencies of the blocks in BlocksToScale suc...
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:1113
This file implements a class to represent arbitrary precision integral constant values and operations...
std::string getNodeAttributes(const BasicBlock *Node, const BlockFrequencyInfo *Graph)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:102
static bool isSimple(Instruction *I)
Analysis pass which computes BranchProbabilityInfo.
Key
PAL metadata keys.
static nodes_iterator nodes_begin(const BlockFrequencyInfo *G)
INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq", "Block Frequency Analysis", true, true) INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass
static ChildIteratorType child_end(const NodeRef N)
iterator begin()
Definition: Function.h:672
Legacy analysis pass which computes BranchProbabilityInfo.
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:105
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
static cl::opt< bool > PrintBlockFreq("print-bfi", cl::init(false), cl::Hidden, cl::desc("Print the block frequency info."))
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:652
block freq
static ChildIteratorType child_begin(const NodeRef N)
void calculate(const Function &F, const BranchProbabilityInfo &BPI, const LoopInfo &LI)
calculate - compute block frequency info for the given function.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
void print(raw_ostream &OS) const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
std::string getEdgeAttributes(NodeRef Node, EdgeIter EI, const BlockFrequencyInfoT *BFI, const BranchProbabilityInfoT *BPI, unsigned HotPercentThreshold=0)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
bool isIrrLoopHeader(const BasicBlock *BB)
Returns true if BB is an irreducible loop header block.
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to &#39;dot...
std::string getNodeAttributes(NodeRef Node, const BlockFrequencyInfoT *Graph, unsigned HotPercentThreshold=0)
std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI, const BlockFrequencyInfo *BFI)
Analysis pass which computes BlockFrequencyInfo.
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
void setBlockFreq(const BasicBlock *BB, uint64_t Freq)
static cl::opt< GVDAGType > ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden, cl::desc("Pop up a window to show a dag displaying how block " "frequencies propagation through the CFG."), cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), clEnumValN(GVDT_Fraction, "fraction", "display a graph using the " "fractional block frequency representation."), clEnumValN(GVDT_Integer, "integer", "display a graph using the raw " "integer fractional block frequency representation."), clEnumValN(GVDT_Count, "count", "display a graph using the real " "profile count if available.")))
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Class for arbitrary precision integers.
Definition: APInt.h:69
void setPreservesAll()
Set by analyses that do not transform their input at all.
cl::opt< std::string > PrintBlockFreqFuncName("print-bfi-func-name", cl::Hidden, cl::desc("The option to specify the name of the function " "whose block frequency info is printed."))
const BranchProbabilityInfo * getBPI() const
Represents analyses that only rely on functions&#39; control flow.
Definition: PassManager.h:114
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:627
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:160
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
cl::opt< std::string > ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden, cl::desc("The option to specify " "the name of the function " "whose CFG will be displayed."))
Analysis providing branch probability information.
block Block Frequency Analysis
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
#define N
typename GTraits::ChildIteratorType EdgeIter
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:648
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This templated class represents "all analyses that operate over <a particular IR unit>" (e...
Definition: PassManager.h:91
const BasicBlock & front() const
Definition: Function.h:679
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
const Function * getFunction() const
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
The legacy pass manager&#39;s analysis pass to compute loop information.
Definition: LoopInfo.h:1138
Shared implementation for block frequency analysis.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70
Optional< uint64_t > getProfileCountFromFreq(uint64_t Freq) const
Returns the estimated profile count of Freq.
static nodes_iterator nodes_end(const BlockFrequencyInfo *G)