LLVM  8.0.1
BlockFrequencyInfo.h
Go to the documentation of this file.
1 //===- BlockFrequencyInfo.h - Block Frequency Analysis ----------*- C++ -*-===//
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 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
15 #define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
16 
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Pass.h"
21 #include <cstdint>
22 #include <memory>
23 
24 namespace llvm {
25 
26 class BasicBlock;
27 class BranchProbabilityInfo;
28 class Function;
29 class LoopInfo;
30 class Module;
31 class raw_ostream;
32 template <class BlockT> class BlockFrequencyInfoImpl;
33 
35 
36 /// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
37 /// estimate IR basic block frequencies.
40 
41  std::unique_ptr<ImplType> BFI;
42 
43 public:
46  const LoopInfo &LI);
47  BlockFrequencyInfo(const BlockFrequencyInfo &) = delete;
52 
53  /// Handle invalidation explicitly.
54  bool invalidate(Function &F, const PreservedAnalyses &PA,
56 
57  const Function *getFunction() const;
58  const BranchProbabilityInfo *getBPI() const;
59  void view(StringRef = "BlockFrequencyDAGs") const;
60 
61  /// getblockFreq - Return block frequency. Return 0 if we don't have the
62  /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
63  /// means that we should not rely on the value itself, but only on the
64  /// comparison to the other block frequencies. We do this to avoid using of
65  /// floating points.
66  BlockFrequency getBlockFreq(const BasicBlock *BB) const;
67 
68  /// Returns the estimated profile count of \p BB.
69  /// This computes the relative block frequency of \p BB and multiplies it by
70  /// the enclosing function's count (if available) and returns the value.
72 
73  /// Returns the estimated profile count of \p Freq.
74  /// This uses the frequency \p Freq and multiplies it by
75  /// the enclosing function's count (if available) and returns the value.
76  Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
77 
78  /// Returns true if \p BB is an irreducible loop header
79  /// block. Otherwise false.
80  bool isIrrLoopHeader(const BasicBlock *BB);
81 
82  // Set the frequency of the given basic block.
83  void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
84 
85  /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
86  /// of the blocks in \p BlocksToScale such that their frequencies relative
87  /// to \p ReferenceBB remain unchanged.
88  void setBlockFreqAndScale(const BasicBlock *ReferenceBB, uint64_t Freq,
89  SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
90 
91  /// calculate - compute block frequency info for the given function.
92  void calculate(const Function &F, const BranchProbabilityInfo &BPI,
93  const LoopInfo &LI);
94 
95  // Print the block frequency Freq to OS using the current functions entry
96  // frequency to convert freq into a relative decimal form.
97  raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
98 
99  // Convenience method that attempts to look up the frequency associated with
100  // BB and print it to OS.
101  raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
102 
103  uint64_t getEntryFreq() const;
104  void releaseMemory();
105  void print(raw_ostream &OS) const;
106 };
107 
108 /// Analysis pass which computes \c BlockFrequencyInfo.
110  : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
112 
113  static AnalysisKey Key;
114 
115 public:
116  /// Provide the result type for this analysis pass.
118 
119  /// Run the analysis pass over a function and produce BFI.
121 };
122 
123 /// Printer pass for the \c BlockFrequencyInfo results.
125  : public PassInfoMixin<BlockFrequencyPrinterPass> {
126  raw_ostream &OS;
127 
128 public:
129  explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
130 
132 };
133 
134 /// Legacy analysis pass which computes \c BlockFrequencyInfo.
136  BlockFrequencyInfo BFI;
137 
138 public:
139  static char ID;
140 
142  ~BlockFrequencyInfoWrapperPass() override;
143 
144  BlockFrequencyInfo &getBFI() { return BFI; }
145  const BlockFrequencyInfo &getBFI() const { return BFI; }
146 
147  void getAnalysisUsage(AnalysisUsage &AU) const override;
148 
149  bool runOnFunction(Function &F) override;
150  void releaseMemory() override;
151  void print(raw_ostream &OS, const Module *M) const override;
152 };
153 
154 } // end namespace llvm
155 
156 #endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Various leaf nodes.
Definition: ISDOpcodes.h:60
BlockFrequencyInfo & operator=(const BlockFrequencyInfo &)=delete
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
F(f)
void view(StringRef="BlockFrequencyDAGs") const
Pop up a ghostview window with the current block frequency propagation rendered using dot...
raw_ostream & printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
Legacy analysis pass which computes BlockFrequencyInfo.
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...
Key
PAL metadata keys.
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:366
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
Printer pass for the BlockFrequencyInfo results.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:383
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:285
void print(raw_ostream &OS) const
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.
Analysis pass which computes BlockFrequencyInfo.
void setBlockFreq(const BasicBlock *BB, uint64_t Freq)
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
const BranchProbabilityInfo * getBPI() const
amdgpu Simplify well known AMD library false Value Value * Arg
Analysis providing branch probability information.
const BlockFrequencyInfo & getBFI() const
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:642
const Function * getFunction() const
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
Shared implementation for block frequency analysis.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
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:71
Optional< uint64_t > getProfileCountFromFreq(uint64_t Freq) const
Returns the estimated profile count of Freq.
Optional< uint64_t > getBlockProfileCount(const BasicBlock *BB) const
Returns the estimated profile count of BB.