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 Function;
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 LoopInfo &LI);
52
53 /// Handle invalidation explicitly.
54 bool invalidate(Function &F, const PreservedAnalyses &PA,
56
57 const Function *getFunction() const;
58 const BranchProbabilityInfo *getBPI() const;
59 void view(StringRef = "BlockFrequencyDAGs") const;
60
61 /// getblockFreq - Return block frequency. Return 0 if we don't have the
62 /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
63 /// means that we should not rely on the value itself, but only on the
64 /// comparison to the other block frequencies. We do this to avoid using of
65 /// floating points.
66 BlockFrequency getBlockFreq(const BasicBlock *BB) const;
67
68 /// Returns the estimated profile count of \p BB.
69 /// This computes the relative block frequency of \p BB and multiplies it by
70 /// the enclosing function's count (if available) and returns the value.
71 std::optional<uint64_t>
72 getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic = false) const;
73
74 /// Returns the estimated profile count of \p Freq.
75 /// This uses the frequency \p Freq and multiplies it by
76 /// the enclosing function's count (if available) and returns the value.
77 std::optional<uint64_t> getProfileCountFromFreq(BlockFrequency Freq) const;
78
79 /// Returns true if \p BB is an irreducible loop header
80 /// block. Otherwise false.
81 bool isIrrLoopHeader(const BasicBlock *BB);
82
83 // Set the frequency of the given basic block.
84 void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq);
85
86 /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
87 /// of the blocks in \p BlocksToScale such that their frequencies relative
88 /// to \p ReferenceBB remain unchanged.
89 void setBlockFreqAndScale(const BasicBlock *ReferenceBB, BlockFrequency Freq,
90 SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
91
92 /// calculate - compute block frequency info for the given function.
93 void calculate(const Function &F, const BranchProbabilityInfo &BPI,
94 const LoopInfo &LI);
95
97 void releaseMemory();
98 void print(raw_ostream &OS) const;
99
100 // Compare to the other BFI and verify they match.
102};
103
104/// Print the block frequency @p Freq relative to the current functions entry
105/// frequency. Returns a Printable object that can be piped via `<<` to a
106/// `raw_ostream`.
108
109/// Convenience function equivalent to calling
110/// `printBlockFreq(BFI, BFI.getBlocakFreq(&BB))`.
112
113/// Analysis pass which computes \c BlockFrequencyInfo.
115 : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
117
118 static AnalysisKey Key;
119
120public:
121 /// Provide the result type for this analysis pass.
123
124 /// Run the analysis pass over a function and produce BFI.
126};
127
128/// Printer pass for the \c BlockFrequencyInfo results.
130 : public PassInfoMixin<BlockFrequencyPrinterPass> {
131 raw_ostream &OS;
132
133public:
135
137
138 static bool isRequired() { return true; }
139};
140
141/// Legacy analysis pass which computes \c BlockFrequencyInfo.
144
145public:
146 static char ID;
147
150
151 BlockFrequencyInfo &getBFI() { return BFI; }
152 const BlockFrequencyInfo &getBFI() const { return BFI; }
153
154 void getAnalysisUsage(AnalysisUsage &AU) const override;
155
156 bool runOnFunction(Function &F) override;
157 void releaseMemory() override;
158 void print(raw_ostream &OS, const Module *M) const override;
159};
160
161} // end namespace llvm
162
163#endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
This header defines various interfaces for pass management in LLVM.
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:347
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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