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
36
37/// Return -view-bfi-func-name.
38LLVM_ABI StringRef getViewBlockFreqFuncName(const LLVMContext &C);
39/// Return -view-hot-freq-percent.
40LLVM_ABI unsigned getViewHotFreqPercent(const LLVMContext &C);
41/// Return -pgo-view-counts.
43/// Return -print-bfi-func-name.
44LLVM_ABI StringRef getPrintBFIFuncName(const LLVMContext &C);
45
46/// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
47/// estimate IR basic block frequencies.
50
51 std::unique_ptr<ImplType> BFI;
52
53public:
56 const BranchProbabilityInfo &BPI,
57 const CycleInfo &CI);
63
64 /// Handle invalidation explicitly.
66 FunctionAnalysisManager::Invalidator &);
67
68 LLVM_ABI const Function *getFunction() const;
70 LLVM_ABI void view(StringRef = "BlockFrequencyDAGs") const;
71
72 /// getblockFreq - Return block frequency. Return 0 if we don't have the
73 /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
74 /// means that we should not rely on the value itself, but only on the
75 /// comparison to the other block frequencies. We do this to avoid using of
76 /// floating points.
78
79 /// Returns the estimated profile count of \p BB.
80 /// This computes the relative block frequency of \p BB and multiplies it by
81 /// the enclosing function's count (if available) and returns the value.
82 LLVM_ABI std::optional<uint64_t>
83 getBlockProfileCount(const BasicBlock *BB) const;
84
85 /// Returns the estimated profile count of \p Freq.
86 /// This uses the frequency \p Freq and multiplies it by
87 /// the enclosing function's count (if available) and returns the value.
88 LLVM_ABI std::optional<uint64_t>
90
91 /// Returns true if \p BB is an irreducible loop header
92 /// block. Otherwise false.
93 LLVM_ABI bool isIrrLoopHeader(const BasicBlock *BB);
94
95 // Set the frequency of the given basic block.
96 LLVM_ABI void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq);
97
98 /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
99 /// of the blocks in \p BlocksToScale such that their frequencies relative
100 /// to \p ReferenceBB remain unchanged.
101 LLVM_ABI void
102 setBlockFreqAndScale(const BasicBlock *ReferenceBB, BlockFrequency Freq,
103 SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
104
105 /// calculate - compute block frequency info for the given function.
106 LLVM_ABI void calculate(const Function &F, const BranchProbabilityInfo &BPI,
107 const CycleInfo &CI);
108
110 LLVM_ABI void releaseMemory();
111 LLVM_ABI void print(raw_ostream &OS) const;
112
113 // Compare to the other BFI and verify they match.
115};
116
117/// Print the block frequency @p Freq relative to the current functions entry
118/// frequency. Returns a Printable object that can be piped via `<<` to a
119/// `raw_ostream`.
121 BlockFrequency Freq);
122
123/// Convenience function equivalent to calling
124/// `printBlockFreq(BFI, BFI.getBlocakFreq(&BB))`.
126 const BasicBlock &BB);
127
128/// Analysis pass which computes \c BlockFrequencyInfo.
130 : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
132
133 LLVM_ABI static AnalysisKey Key;
134
135public:
136 /// Provide the result type for this analysis pass.
138
139 /// Run the analysis pass over a function and produce BFI.
141};
142
143/// Printer pass for the \c BlockFrequencyInfo results.
145 : public RequiredPassInfoMixin<BlockFrequencyPrinterPass> {
146 raw_ostream &OS;
147
148public:
149 explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
150
152};
153
154/// Legacy analysis pass which computes \c BlockFrequencyInfo.
157
158public:
159 static char ID;
160
163
164 BlockFrequencyInfo &getBFI() { return BFI; }
165 const BlockFrequencyInfo &getBFI() const { return BFI; }
166
167 void getAnalysisUsage(AnalysisUsage &AU) const override;
168
169 bool runOnFunction(Function &F) override;
170 void releaseMemory() override;
171 void print(raw_ostream &OS, const Module *M) const override;
172};
173
174} // end namespace llvm
175
176#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
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.