LLVM  13.0.0git
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.
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  bool AllowSynthetic = false) const;
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  // Compare to the other BFI and verify they match.
108  void verifyMatch(BlockFrequencyInfo &Other) const;
109 };
110 
111 /// Analysis pass which computes \c BlockFrequencyInfo.
113  : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
115 
116  static AnalysisKey Key;
117 
118 public:
119  /// Provide the result type for this analysis pass.
121 
122  /// Run the analysis pass over a function and produce BFI.
124 };
125 
126 /// Printer pass for the \c BlockFrequencyInfo results.
128  : public PassInfoMixin<BlockFrequencyPrinterPass> {
129  raw_ostream &OS;
130 
131 public:
132  explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
133 
135 };
136 
137 /// Legacy analysis pass which computes \c BlockFrequencyInfo.
139  BlockFrequencyInfo BFI;
140 
141 public:
142  static char ID;
143 
146 
147  BlockFrequencyInfo &getBFI() { return BFI; }
148  const BlockFrequencyInfo &getBFI() const { return BFI; }
149 
150  void getAnalysisUsage(AnalysisUsage &AU) const override;
151 
152  bool runOnFunction(Function &F) override;
153  void releaseMemory() override;
154  void print(raw_ostream &OS, const Module *M) const override;
155 };
156 
157 } // end namespace llvm
158 
159 #endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Optional.h
llvm::BlockFrequencyPrinterPass::BlockFrequencyPrinterPass
BlockFrequencyPrinterPass(raw_ostream &OS)
Definition: BlockFrequencyInfo.h:132
llvm::BlockFrequencyInfo::print
void print(raw_ostream &OS) const
Definition: BlockFrequencyInfo.cpp:287
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
llvm::Function
Definition: Function.h:61
Pass.h
llvm::BlockFrequencyInfoWrapperPass
Legacy analysis pass which computes BlockFrequencyInfo.
Definition: BlockFrequencyInfo.h:138
llvm::BlockFrequencyInfoWrapperPass::getBFI
const BlockFrequencyInfo & getBFI() const
Definition: BlockFrequencyInfo.h:148
BlockFrequency.h
llvm::BlockFrequencyInfo::view
void view(StringRef="BlockFrequencyDAGs") const
Pop up a ghostview window with the current block frequency propagation rendered using dot.
Definition: BlockFrequencyInfo.cpp:258
llvm::BlockFrequencyInfoWrapperPass::ID
static char ID
Definition: BlockFrequencyInfo.h:142
llvm::BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass
~BlockFrequencyInfoWrapperPass() override
llvm::Optional< uint64_t >
llvm::BlockFrequencyInfoWrapperPass::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: BlockFrequencyInfo.cpp:326
llvm::PGOVCT_Text
@ PGOVCT_Text
Definition: BlockFrequencyInfo.h:33
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::BlockFrequencyInfo::getEntryFreq
uint64_t getEntryFreq() const
Definition: BlockFrequencyInfo.cpp:281
llvm::PGOVCT_Graph
@ PGOVCT_Graph
Definition: BlockFrequencyInfo.h:33
llvm::BlockFrequencyInfo
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition: BlockFrequencyInfo.h:37
llvm::BlockFrequencyInfo::getProfileCountFromFreq
Optional< uint64_t > getProfileCountFromFreq(uint64_t Freq) const
Returns the estimated profile count of Freq.
Definition: BlockFrequencyInfo.cpp:218
llvm::BranchProbabilityInfo
Analysis providing branch probability information.
Definition: BranchProbabilityInfo.h:115
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::BlockFrequencyInfo::setBlockFreq
void setBlockFreq(const BasicBlock *BB, uint64_t Freq)
Definition: BlockFrequencyInfo.cpp:229
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::BlockFrequencyInfo::releaseMemory
void releaseMemory()
Definition: BlockFrequencyInfo.cpp:285
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:656
llvm::BlockFrequencyInfo::printBlockFreq
raw_ostream & printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const
Definition: BlockFrequencyInfo.cpp:271
llvm::BlockFrequencyAnalysis
Analysis pass which computes BlockFrequencyInfo.
Definition: BlockFrequencyInfo.h:112
llvm::PGOViewCountsType
PGOViewCountsType
Definition: BlockFrequencyInfo.h:33
llvm::BlockFrequency
Definition: BlockFrequency.h:24
llvm::BlockFrequencyInfo::~BlockFrequencyInfo
~BlockFrequencyInfo()
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
llvm::BlockFrequencyInfo::operator=
BlockFrequencyInfo & operator=(const BlockFrequencyInfo &)=delete
llvm::PGOVCT_None
@ PGOVCT_None
Definition: BlockFrequencyInfo.h:33
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
llvm::BlockFrequencyInfo::isIrrLoopHeader
bool isIrrLoopHeader(const BasicBlock *BB)
Returns true if BB is an irreducible loop header block.
Definition: BlockFrequencyInfo.cpp:224
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:391
llvm::BlockFrequencyInfo::calculate
void calculate(const Function &F, const BranchProbabilityInfo &BPI, const LoopInfo &LI)
calculate - compute block frequency info for the given function.
Definition: BlockFrequencyInfo.cpp:186
llvm::BlockFrequencyPrinterPass
Printer pass for the BlockFrequencyInfo results.
Definition: BlockFrequencyInfo.h:127
llvm::LoopInfo
Definition: LoopInfo.h:1083
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::BlockFrequencyInfoWrapperPass::getBFI
BlockFrequencyInfo & getBFI()
Definition: BlockFrequencyInfo.h:147
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::BlockFrequencyInfo::invalidate
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
Definition: BlockFrequencyInfo.cpp:177
llvm::BlockFrequencyInfo::setBlockFreqAndScale
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...
Definition: BlockFrequencyInfo.cpp:234
llvm::BlockFrequencyAnalysis::run
Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
Definition: BlockFrequencyInfo.cpp:335
PassManager.h
llvm::BlockFrequencyInfo::getBPI
const BranchProbabilityInfo * getBPI() const
Definition: BlockFrequencyInfo.cpp:266
llvm::BlockFrequencyInfoWrapperPass::print
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
Definition: BlockFrequencyInfo.cpp:313
llvm::BlockFrequencyInfo::getBlockFreq
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
Definition: BlockFrequencyInfo.cpp:204
llvm::BlockFrequencyInfo::getFunction
const Function * getFunction() const
Definition: BlockFrequencyInfo.cpp:262
llvm::BlockFrequencyInfo::getBlockProfileCount
Optional< uint64_t > getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic=false) const
Returns the estimated profile count of BB.
Definition: BlockFrequencyInfo.cpp:209
llvm::BlockFrequencyInfoWrapperPass::releaseMemory
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: BlockFrequencyInfo.cpp:324
llvm::BlockFrequencyInfo::BlockFrequencyInfo
BlockFrequencyInfo()
llvm::BlockFrequencyInfo::verifyMatch
void verifyMatch(BlockFrequencyInfo &Other) const
Definition: BlockFrequencyInfo.cpp:292
llvm::BlockFrequencyInfoImpl
Shared implementation for block frequency analysis.
Definition: BlockFrequencyInfo.h:31
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:343
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass
BlockFrequencyInfoWrapperPass()
Definition: BlockFrequencyInfo.cpp:306
llvm::BlockFrequencyPrinterPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: BlockFrequencyInfo.cpp:344
llvm::BlockFrequencyInfoWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: BlockFrequencyInfo.cpp:318
llvm::codeview::PublicSymFlags::Function
@ Function
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1172