LLVM  9.0.0svn
BlockFrequencyInfo.h
Go to the documentation of this file.
1 //===- BlockFrequencyInfo.h - Block Frequency Analysis ----------*- C++ -*-===//
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 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
14 #define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
15 
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/Pass.h"
20 #include <cstdint>
21 #include <memory>
22 
23 namespace llvm {
24 
25 class BasicBlock;
26 class BranchProbabilityInfo;
27 class Function;
28 class LoopInfo;
29 class Module;
30 class raw_ostream;
31 template <class BlockT> class BlockFrequencyInfoImpl;
32 
34 
35 /// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
36 /// estimate IR basic block frequencies.
39 
40  std::unique_ptr<ImplType> BFI;
41 
42 public:
45  const LoopInfo &LI);
46  BlockFrequencyInfo(const BlockFrequencyInfo &) = delete;
51 
52  /// Handle invalidation explicitly.
53  bool invalidate(Function &F, const PreservedAnalyses &PA,
55 
56  const Function *getFunction() const;
57  const BranchProbabilityInfo *getBPI() const;
58  void view(StringRef = "BlockFrequencyDAGs") const;
59 
60  /// getblockFreq - Return block frequency. Return 0 if we don't have the
61  /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
62  /// means that we should not rely on the value itself, but only on the
63  /// comparison to the other block frequencies. We do this to avoid using of
64  /// floating points.
65  BlockFrequency getBlockFreq(const BasicBlock *BB) const;
66 
67  /// Returns the estimated profile count of \p BB.
68  /// This computes the relative block frequency of \p BB and multiplies it by
69  /// the enclosing function's count (if available) and returns the value.
71 
72  /// Returns the estimated profile count of \p Freq.
73  /// This uses the frequency \p Freq and multiplies it by
74  /// the enclosing function's count (if available) and returns the value.
75  Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
76 
77  /// Returns true if \p BB is an irreducible loop header
78  /// block. Otherwise false.
79  bool isIrrLoopHeader(const BasicBlock *BB);
80 
81  // Set the frequency of the given basic block.
82  void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
83 
84  /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
85  /// of the blocks in \p BlocksToScale such that their frequencies relative
86  /// to \p ReferenceBB remain unchanged.
87  void setBlockFreqAndScale(const BasicBlock *ReferenceBB, uint64_t Freq,
88  SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
89 
90  /// calculate - compute block frequency info for the given function.
91  void calculate(const Function &F, const BranchProbabilityInfo &BPI,
92  const LoopInfo &LI);
93 
94  // Print the block frequency Freq to OS using the current functions entry
95  // frequency to convert freq into a relative decimal form.
96  raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
97 
98  // Convenience method that attempts to look up the frequency associated with
99  // BB and print it to OS.
100  raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
101 
102  uint64_t getEntryFreq() const;
103  void releaseMemory();
104  void print(raw_ostream &OS) const;
105 };
106 
107 /// Analysis pass which computes \c BlockFrequencyInfo.
109  : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
111 
112  static AnalysisKey Key;
113 
114 public:
115  /// Provide the result type for this analysis pass.
117 
118  /// Run the analysis pass over a function and produce BFI.
120 };
121 
122 /// Printer pass for the \c BlockFrequencyInfo results.
124  : public PassInfoMixin<BlockFrequencyPrinterPass> {
125  raw_ostream &OS;
126 
127 public:
128  explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
129 
131 };
132 
133 /// Legacy analysis pass which computes \c BlockFrequencyInfo.
135  BlockFrequencyInfo BFI;
136 
137 public:
138  static char ID;
139 
141  ~BlockFrequencyInfoWrapperPass() override;
142 
143  BlockFrequencyInfo &getBFI() { return BFI; }
144  const BlockFrequencyInfo &getBFI() const { return BFI; }
145 
146  void getAnalysisUsage(AnalysisUsage &AU) const override;
147 
148  bool runOnFunction(Function &F) override;
149  void releaseMemory() override;
150  void print(raw_ostream &OS, const Module *M) const override;
151 };
152 
153 } // end namespace llvm
154 
155 #endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Various leaf nodes.
Definition: ISDOpcodes.h:59
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:343
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:365
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:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
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:284
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
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
Analysis providing branch probability information.
const BlockFrequencyInfo & getBFI() const
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:641
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
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.
Optional< uint64_t > getBlockProfileCount(const BasicBlock *BB) const
Returns the estimated profile count of BB.