LLVM 22.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;
28class BranchProbabilityInfo;
29class LoopInfo;
30class Module;
31class raw_ostream;
32template <class BlockT> class BlockFrequencyInfoImpl;
33
35
36/// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
37/// estimate IR basic block frequencies.
40
41 std::unique_ptr<ImplType> BFI;
42
43public:
46 const BranchProbabilityInfo &BPI,
47 const LoopInfo &LI);
53
54 /// Handle invalidation explicitly.
57
58 LLVM_ABI const Function *getFunction() const;
60 LLVM_ABI void view(StringRef = "BlockFrequencyDAGs") const;
61
62 /// getblockFreq - Return block frequency. Return 0 if we don't have the
63 /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
64 /// means that we should not rely on the value itself, but only on the
65 /// comparison to the other block frequencies. We do this to avoid using of
66 /// floating points.
68
69 /// Returns the estimated profile count of \p BB.
70 /// This computes the relative block frequency of \p BB and multiplies it by
71 /// the enclosing function's count (if available) and returns the value.
72 LLVM_ABI std::optional<uint64_t>
73 getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic = false) const;
74
75 /// Returns the estimated profile count of \p Freq.
76 /// This uses the frequency \p Freq and multiplies it by
77 /// the enclosing function's count (if available) and returns the value.
78 LLVM_ABI std::optional<uint64_t>
80
81 /// Returns true if \p BB is an irreducible loop header
82 /// block. Otherwise false.
83 LLVM_ABI bool isIrrLoopHeader(const BasicBlock *BB);
84
85 // Set the frequency of the given basic block.
86 LLVM_ABI void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq);
87
88 /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
89 /// of the blocks in \p BlocksToScale such that their frequencies relative
90 /// to \p ReferenceBB remain unchanged.
91 LLVM_ABI void
92 setBlockFreqAndScale(const BasicBlock *ReferenceBB, BlockFrequency Freq,
93 SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
94
95 /// calculate - compute block frequency info for the given function.
96 LLVM_ABI void calculate(const Function &F, const BranchProbabilityInfo &BPI,
97 const LoopInfo &LI);
98
100 LLVM_ABI void releaseMemory();
101 LLVM_ABI void print(raw_ostream &OS) const;
102
103 // Compare to the other BFI and verify they match.
105};
106
107/// Print the block frequency @p Freq relative to the current functions entry
108/// frequency. Returns a Printable object that can be piped via `<<` to a
109/// `raw_ostream`.
111 BlockFrequency Freq);
112
113/// Convenience function equivalent to calling
114/// `printBlockFreq(BFI, BFI.getBlocakFreq(&BB))`.
116 const BasicBlock &BB);
117
118/// Analysis pass which computes \c BlockFrequencyInfo.
120 : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
122
123 LLVM_ABI static AnalysisKey Key;
124
125public:
126 /// Provide the result type for this analysis pass.
128
129 /// Run the analysis pass over a function and produce BFI.
131};
132
133/// Printer pass for the \c BlockFrequencyInfo results.
135 : public PassInfoMixin<BlockFrequencyPrinterPass> {
136 raw_ostream &OS;
137
138public:
140
142
143 static bool isRequired() { return true; }
144};
145
146/// Legacy analysis pass which computes \c BlockFrequencyInfo.
149
150public:
151 static char ID;
152
155
156 BlockFrequencyInfo &getBFI() { return BFI; }
157 const BlockFrequencyInfo &getBFI() const { return BFI; }
158
159 void getAnalysisUsage(AnalysisUsage &AU) const override;
160
161 bool runOnFunction(Function &F) override;
162 void releaseMemory() override;
163 void print(raw_ostream &OS, const Module *M) const override;
164};
165
166} // end namespace llvm
167
168#endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_ABI
Definition: Compiler.h:213
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:55
Machine Check Debug Module
raw_pwrite_stream & OS
Value * RHS
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
Represent the analysis usage information of a pass.
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
Analysis pass which computes BlockFrequencyInfo.
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
Shared implementation for block frequency analysis.
Legacy analysis pass which computes BlockFrequencyInfo.
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 void calculate(const Function &F, const BranchProbabilityInfo &BPI, const LoopInfo &LI)
calculate - compute block frequency info for the given function.
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, bool AllowSynthetic=false) const
Returns the estimated profile count of BB.
BlockFrequencyInfo & operator=(const BlockFrequencyInfo &)=delete
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
Printer pass for the BlockFrequencyInfo results.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Analysis providing branch probability information.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
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...
Definition: SmallPtrSet.h:380
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:81
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Other
Any other memory.
LLVM_ABI Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq)
Print the block frequency Freq relative to the current functions entry frequency.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70