LLVM  5.0.0
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 <climits>
22 
23 namespace llvm {
24 
25 class BranchProbabilityInfo;
26 class LoopInfo;
27 template <class BlockT> class BlockFrequencyInfoImpl;
28 
29 /// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
30 /// estimate IR basic block frequencies.
33  std::unique_ptr<ImplType> BFI;
34 
35  void operator=(const BlockFrequencyInfo &) = delete;
36  BlockFrequencyInfo(const BlockFrequencyInfo &) = delete;
37 
38 public:
41  const LoopInfo &LI);
43 
44  BlockFrequencyInfo &operator=(BlockFrequencyInfo &&RHS);
45 
47 
48  /// Handle invalidation explicitly.
49  bool invalidate(Function &F, const PreservedAnalyses &PA,
51 
52  const Function *getFunction() const;
53  const BranchProbabilityInfo *getBPI() const;
54  void view() const;
55 
56  /// getblockFreq - Return block frequency. Return 0 if we don't have the
57  /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
58  /// means that we should not rely on the value itself, but only on the
59  /// comparison to the other block frequencies. We do this to avoid using of
60  /// floating points.
61  BlockFrequency getBlockFreq(const BasicBlock *BB) const;
62 
63  /// \brief Returns the estimated profile count of \p BB.
64  /// This computes the relative block frequency of \p BB and multiplies it by
65  /// the enclosing function's count (if available) and returns the value.
67 
68  /// \brief Returns the estimated profile count of \p Freq.
69  /// This uses the frequency \p Freq and multiplies it by
70  /// the enclosing function's count (if available) and returns the value.
71  Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
72 
73  // Set the frequency of the given basic block.
74  void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
75 
76  /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
77  /// of the blocks in \p BlocksToScale such that their frequencies relative
78  /// to \p ReferenceBB remain unchanged.
79  void setBlockFreqAndScale(const BasicBlock *ReferenceBB, uint64_t Freq,
80  SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
81 
82  /// calculate - compute block frequency info for the given function.
83  void calculate(const Function &F, const BranchProbabilityInfo &BPI,
84  const LoopInfo &LI);
85 
86  // Print the block frequency Freq to OS using the current functions entry
87  // frequency to convert freq into a relative decimal form.
88  raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
89 
90  // Convenience method that attempts to look up the frequency associated with
91  // BB and print it to OS.
92  raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
93 
94  uint64_t getEntryFreq() const;
95  void releaseMemory();
96  void print(raw_ostream &OS) const;
97 };
98 
99 /// \brief Analysis pass which computes \c BlockFrequencyInfo.
101  : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
103  static AnalysisKey Key;
104 
105 public:
106  /// \brief Provide the result typedef for this analysis pass.
108 
109  /// \brief Run the analysis pass over a function and produce BFI.
111 };
112 
113 /// \brief Printer pass for the \c BlockFrequencyInfo results.
115  : public PassInfoMixin<BlockFrequencyPrinterPass> {
116  raw_ostream &OS;
117 
118 public:
119  explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
121 };
122 
123 /// \brief Legacy analysis pass which computes \c BlockFrequencyInfo.
125  BlockFrequencyInfo BFI;
126 
127 public:
128  static char ID;
129 
132 
133  BlockFrequencyInfo &getBFI() { return BFI; }
134  const BlockFrequencyInfo &getBFI() const { return BFI; }
135 
136  void getAnalysisUsage(AnalysisUsage &AU) const override;
137 
138  bool runOnFunction(Function &F) override;
139  void releaseMemory() override;
140  void print(raw_ostream &OS, const Module *M) const override;
141 };
142 
143 }
144 
145 #endif
BlockFrequencyInfo Result
Provide the result typedef for this analysis pass.
Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
raw_ostream & printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:345
const BlockFrequencyInfo & getBFI() const
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...
#define F(x, y, z)
Definition: MD5.cpp:55
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
Optional< uint64_t > getProfileCountFromFreq(uint64_t Freq) const
Returns the estimated profile count of Freq.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
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:59
Printer pass for the BlockFrequencyInfo results.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:382
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:298
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
Analysis pass which computes BlockFrequencyInfo.
void setBlockFreq(const BasicBlock *BB, uint64_t Freq)
void print(raw_ostream &OS) const
const BranchProbabilityInfo * getBPI() const
Analysis providing branch probability information.
const Function * getFunction() const
void view() const
Pop up a ghostview window with the current block frequency propagation rendered using dot...
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:559
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
Shared implementation for block frequency analysis.
A container for analyses that lazily runs them and caches their results.
Optional< uint64_t > getBlockProfileCount(const BasicBlock *BB) const
Returns the estimated profile count of BB.
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
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.