LLVM 24.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/IR/PassManager.h"
17#include "llvm/Pass.h"
21#include <cstdint>
22#include <memory>
23#include <optional>
24
25namespace llvm {
26
27class BasicBlock;
29class CycleInfo;
30class LLVMContext;
31class Module;
32class raw_ostream;
33template <class BlockT> class BlockFrequencyInfoImpl;
34
37
38/// Return -view-bfi-func-name.
39LLVM_ABI StringRef getViewBlockFreqFuncName(const LLVMContext &C);
40/// Return -view-hot-freq-percent.
41LLVM_ABI unsigned getViewHotFreqPercent(const LLVMContext &C);
42/// Return -pgo-view-counts.
44/// Return -print-bfi-func-name.
45LLVM_ABI StringRef getPrintBFIFuncName(const LLVMContext &C);
46
47/// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
48/// estimate IR basic block frequencies.
51
52 std::unique_ptr<ImplType> BFI;
53
54public:
57 const BranchProbabilityInfo &BPI,
58 const CycleInfo &CI);
64
65 /// Handle invalidation explicitly.
67 FunctionAnalysisManager::Invalidator &);
68
69 LLVM_ABI const Function *getFunction() const;
71 LLVM_ABI void view(StringRef = "BlockFrequencyDAGs") const;
72
73 /// getblockFreq - Return block frequency. Return 0 if we don't have the
74 /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
75 /// means that we should not rely on the value itself, but only on the
76 /// comparison to the other block frequencies. We do this to avoid using of
77 /// floating points.
79
80 /// Returns the estimated profile count of \p BB.
81 /// This computes the relative block frequency of \p BB and multiplies it by
82 /// the enclosing function's count (if available) and returns the value.
83 LLVM_ABI std::optional<uint64_t>
84 getBlockProfileCount(const BasicBlock *BB) const;
85
86 /// Returns the estimated profile count of \p Freq.
87 /// This uses the frequency \p Freq and multiplies it by
88 /// the enclosing function's count (if available) and returns the value.
89 LLVM_ABI std::optional<uint64_t>
91
92 /// Returns true if \p BB is an irreducible loop header
93 /// block. Otherwise false.
94 LLVM_ABI bool isIrrLoopHeader(const BasicBlock *BB);
95
96 // Set the frequency of the given basic block.
97 LLVM_ABI void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq);
98
99 /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
100 /// of the blocks in \p BlocksToScale such that their frequencies relative
101 /// to \p ReferenceBB remain unchanged.
102 LLVM_ABI void
103 setBlockFreqAndScale(const BasicBlock *ReferenceBB, BlockFrequency Freq,
104 SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
105
106 /// calculate - compute block frequency info for the given function.
107 LLVM_ABI void calculate(const Function &F, const BranchProbabilityInfo &BPI,
108 const CycleInfo &CI);
109
111 LLVM_ABI void releaseMemory();
112 LLVM_ABI void print(raw_ostream &OS) const;
113
114 // Compare to the other BFI and verify they match.
116};
117
118/// Print the block frequency @p Freq relative to the current functions entry
119/// frequency. Returns a Printable object that can be piped via `<<` to a
120/// `raw_ostream`.
122 BlockFrequency Freq);
123
124/// Convenience function equivalent to calling
125/// `printBlockFreq(BFI, BFI.getBlocakFreq(&BB))`.
127 const BasicBlock &BB);
128
129/// Analysis pass which computes \c BlockFrequencyInfo.
131 : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
133
134 LLVM_ABI static AnalysisKey Key;
135
136public:
137 /// Provide the result type for this analysis pass.
139
140 /// Run the analysis pass over a function and produce BFI.
142};
143
144/// Printer pass for the \c BlockFrequencyInfo results.
146 : public RequiredPassInfoMixin<BlockFrequencyPrinterPass> {
147 raw_ostream &OS;
148
149public:
150 explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
151
153};
154
155/// Legacy analysis pass which computes \c BlockFrequencyInfo.
158
159public:
160 static char ID;
161
164
165 BlockFrequencyInfo &getBFI() { return BFI; }
166 const BlockFrequencyInfo &getBFI() const { return BFI; }
167
168 void getAnalysisUsage(AnalysisUsage &AU) const override;
169
170 bool runOnFunction(Function &F) override;
171 void releaseMemory() override;
172 void print(raw_ostream &OS, const Module *M) const override;
173};
174
175} // end namespace llvm
176
177#endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:211
static bool runOnFunction(Function &F, bool PostInlining)
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
Value * RHS
Represent the analysis usage information of a pass.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo Result
Provide the result type for this analysis pass.
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
Shared implementation for block frequency analysis.
const BlockFrequencyInfo & getBFI() const
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
LLVM_ABI bool isIrrLoopHeader(const BasicBlock *BB)
Returns true if BB is an irreducible loop header block.
LLVM_ABI std::optional< uint64_t > getProfileCountFromFreq(BlockFrequency Freq) const
Returns the estimated profile count of Freq.
LLVM_ABI void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq)
LLVM_ABI ~BlockFrequencyInfo()
LLVM_ABI const Function * getFunction() const
LLVM_ABI std::optional< uint64_t > getBlockProfileCount(const BasicBlock *BB) const
Returns the estimated profile count of BB.
BlockFrequencyInfo & operator=(const BlockFrequencyInfo &)=delete
LLVM_ABI void calculate(const Function &F, const BranchProbabilityInfo &BPI, const CycleInfo &CI)
calculate - compute block frequency info for the given function.
LLVM_ABI void view(StringRef="BlockFrequencyDAGs") const
Pop up a ghostview window with the current block frequency propagation rendered using dot.
LLVM_ABI void setBlockFreqAndScale(const BasicBlock *ReferenceBB, BlockFrequency Freq, SmallPtrSetImpl< BasicBlock * > &BlocksToScale)
Set the frequency of ReferenceBB to Freq and scale the frequencies of the blocks in BlocksToScale suc...
LLVM_ABI const BranchProbabilityInfo * getBPI() const
LLVM_ABI BlockFrequency getEntryFreq() const
LLVM_ABI BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
LLVM_ABI void verifyMatch(BlockFrequencyInfo &Other) const
BlockFrequencyInfo(const BlockFrequencyInfo &)=delete
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Analysis providing branch probability information.
FunctionPass(char &pid)
Definition Pass.h:314
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Simple wrapper around std::function<void(raw_ostream&)>.
Definition Printable.h:38
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI PGOViewCountsType getPGOViewCounts(const LLVMContext &C)
Return -pgo-view-counts.
@ Other
Any other memory.
Definition ModRef.h:68
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
LLVM_ABI Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq)
Print the block frequency Freq relative to the current functions entry frequency.
LLVM_ABI StringRef getPrintBFIFuncName(const LLVMContext &C)
Return -print-bfi-func-name.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI StringRef getViewBlockFreqFuncName(const LLVMContext &C)
Return -view-bfi-func-name.
LLVM_ABI unsigned getViewHotFreqPercent(const LLVMContext &C)
Return -view-hot-freq-percent.
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A CRTP mix-in for passes that should not be skipped.