LLVM 20.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"
20#include <cstdint>
21#include <memory>
22#include <optional>
23
24namespace llvm {
25
26class BasicBlock;
27class BranchProbabilityInfo;
28class LoopInfo;
29class Module;
30class raw_ostream;
31template <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
42public:
45 const LoopInfo &LI);
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.
70 std::optional<uint64_t>
71 getBlockProfileCount(const BasicBlock *BB, 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 std::optional<uint64_t> getProfileCountFromFreq(BlockFrequency 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, BlockFrequency 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, BlockFrequency 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
96 void releaseMemory();
97 void print(raw_ostream &OS) const;
98
99 // Compare to the other BFI and verify they match.
101};
102
103/// Print the block frequency @p Freq relative to the current functions entry
104/// frequency. Returns a Printable object that can be piped via `<<` to a
105/// `raw_ostream`.
107
108/// Convenience function equivalent to calling
109/// `printBlockFreq(BFI, BFI.getBlocakFreq(&BB))`.
111
112/// Analysis pass which computes \c BlockFrequencyInfo.
114 : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
116
117 static AnalysisKey Key;
118
119public:
120 /// Provide the result type for this analysis pass.
122
123 /// Run the analysis pass over a function and produce BFI.
125};
126
127/// Printer pass for the \c BlockFrequencyInfo results.
129 : public PassInfoMixin<BlockFrequencyPrinterPass> {
130 raw_ostream &OS;
131
132public:
134
136
137 static bool isRequired() { return true; }
138};
139
140/// Legacy analysis pass which computes \c BlockFrequencyInfo.
143
144public:
145 static char ID;
146
149
150 BlockFrequencyInfo &getBFI() { return BFI; }
151 const BlockFrequencyInfo &getBFI() const { return BFI; }
152
153 void getAnalysisUsage(AnalysisUsage &AU) const override;
154
155 bool runOnFunction(Function &F) override;
156 void releaseMemory() override;
157 void print(raw_ostream &OS, const Module *M) const override;
158};
159
160} // end namespace llvm
161
162#endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
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:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
Analysis pass which computes BlockFrequencyInfo.
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.
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
const BlockFrequencyInfo & getBFI() const
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
bool isIrrLoopHeader(const BasicBlock *BB)
Returns true if BB is an irreducible loop header block.
void calculate(const Function &F, const BranchProbabilityInfo &BPI, const LoopInfo &LI)
calculate - compute block frequency info for the given function.
std::optional< uint64_t > getProfileCountFromFreq(BlockFrequency Freq) const
Returns the estimated profile count of Freq.
void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq)
const Function * getFunction() const
std::optional< uint64_t > getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic=false) const
Returns the estimated profile count of BB.
BlockFrequencyInfo & operator=(const BlockFrequencyInfo &)=delete
void view(StringRef="BlockFrequencyDAGs") const
Pop up a ghostview window with the current block frequency propagation rendered using dot.
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...
const BranchProbabilityInfo * getBPI() const
BlockFrequency getEntryFreq() const
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
void print(raw_ostream &OS) const
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
void verifyMatch(BlockFrequencyInfo &Other) const
BlockFrequencyInfo(const BlockFrequencyInfo &)=delete
Printer pass for the BlockFrequencyInfo results.
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:310
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
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:363
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Other
Any other memory.
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:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69