Movatterモバイル変換


[0]ホーム

URL:


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"
18#include "llvm/Support/BlockFrequency.h"
19#include "llvm/Support/Printable.h"
20#include <cstdint>
21#include <memory>
22#include <optional>
23
24namespacellvm {
25
26classBasicBlock;
27classBranchProbabilityInfo;
28classLoopInfo;
29classModule;
30classraw_ostream;
31template <class BlockT>classBlockFrequencyInfoImpl;
32
33enumPGOViewCountsType {PGOVCT_None,PGOVCT_Graph,PGOVCT_Text };
34
35/// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
36/// estimate IR basic block frequencies.
37classBlockFrequencyInfo {
38usingImplType =BlockFrequencyInfoImpl<BasicBlock>;
39
40 std::unique_ptr<ImplType> BFI;
41
42public:
43BlockFrequencyInfo();
44BlockFrequencyInfo(constFunction &F,constBranchProbabilityInfo &BPI,
45constLoopInfo &LI);
46BlockFrequencyInfo(constBlockFrequencyInfo &) =delete;
47BlockFrequencyInfo &operator=(constBlockFrequencyInfo &) =delete;
48BlockFrequencyInfo(BlockFrequencyInfo &&Arg);
49BlockFrequencyInfo &operator=(BlockFrequencyInfo &&RHS);
50~BlockFrequencyInfo();
51
52 /// Handle invalidation explicitly.
53boolinvalidate(Function &F,constPreservedAnalyses &PA,
54FunctionAnalysisManager::Invalidator &);
55
56constFunction *getFunction()const;
57constBranchProbabilityInfo *getBPI()const;
58voidview(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.
65BlockFrequencygetBlockFreq(constBasicBlock *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>
71getBlockProfileCount(constBasicBlock *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.
80boolisIrrLoopHeader(constBasicBlock *BB);
81
82// Set the frequency of the given basic block.
83voidsetBlockFreq(constBasicBlock *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.
88voidsetBlockFreqAndScale(constBasicBlock *ReferenceBB,BlockFrequency Freq,
89SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
90
91 /// calculate - compute block frequency info for the given function.
92voidcalculate(constFunction &F,constBranchProbabilityInfo &BPI,
93constLoopInfo &LI);
94
95BlockFrequencygetEntryFreq()const;
96voidreleaseMemory();
97voidprint(raw_ostream &OS)const;
98
99// Compare to the other BFI and verify they match.
100voidverifyMatch(BlockFrequencyInfo &Other)const;
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`.
106PrintableprintBlockFreq(constBlockFrequencyInfo &BFI,BlockFrequency Freq);
107
108/// Convenience function equivalent to calling
109/// `printBlockFreq(BFI, BFI.getBlocakFreq(&BB))`.
110PrintableprintBlockFreq(constBlockFrequencyInfo &BFI,constBasicBlock &BB);
111
112/// Analysis pass which computes \c BlockFrequencyInfo.
113classBlockFrequencyAnalysis
114 :publicAnalysisInfoMixin<BlockFrequencyAnalysis> {
115friendAnalysisInfoMixin<BlockFrequencyAnalysis>;
116
117staticAnalysisKey Key;
118
119public:
120 /// Provide the result type for this analysis pass.
121usingResult =BlockFrequencyInfo;
122
123 /// Run the analysis pass over a function and produce BFI.
124Resultrun(Function &F,FunctionAnalysisManager &AM);
125};
126
127/// Printer pass for the \c BlockFrequencyInfo results.
128classBlockFrequencyPrinterPass
129 :publicPassInfoMixin<BlockFrequencyPrinterPass> {
130raw_ostream &OS;
131
132public:
133explicitBlockFrequencyPrinterPass(raw_ostream &OS) :OS(OS) {}
134
135PreservedAnalysesrun(Function &F,FunctionAnalysisManager &AM);
136
137staticboolisRequired() {returntrue; }
138};
139
140/// Legacy analysis pass which computes \c BlockFrequencyInfo.
141classBlockFrequencyInfoWrapperPass :publicFunctionPass {
142BlockFrequencyInfo BFI;
143
144public:
145staticcharID;
146
147BlockFrequencyInfoWrapperPass();
148~BlockFrequencyInfoWrapperPass()override;
149
150BlockFrequencyInfo &getBFI() {return BFI; }
151constBlockFrequencyInfo &getBFI() const{return BFI; }
152
153voidgetAnalysisUsage(AnalysisUsage &AU)const override;
154
155boolrunOnFunction(Function &F)override;
156voidreleaseMemory()override;
157voidprint(raw_ostream &OS,constModule *M)const override;
158};
159
160}// end namespace llvm
161
162#endif// LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
BlockFrequency.h
PassManager.h
This header defines various interfaces for pass management in LLVM.
F
#define F(x, y, z)
Definition:MD5.cpp:55
Module
Machine Check Debug Module
Definition:MachineCheckDebugify.cpp:124
Pass.h
Printable.h
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition:PassManager.h:292
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition:PassManager.h:253
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition:PassAnalysisSupport.h:47
llvm::BasicBlock
LLVM Basic Block Representation.
Definition:BasicBlock.h:61
llvm::BlockFrequencyAnalysis
Analysis pass which computes BlockFrequencyInfo.
Definition:BlockFrequencyInfo.h:114
llvm::BlockFrequencyAnalysis::run
Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
Definition:BlockFrequencyInfo.cpp:333
llvm::BlockFrequencyInfoImpl
Shared implementation for block frequency analysis.
Definition:BlockFrequencyInfoImpl.h:845
llvm::BlockFrequencyInfoWrapperPass
Legacy analysis pass which computes BlockFrequencyInfo.
Definition:BlockFrequencyInfo.h:141
llvm::BlockFrequencyInfoWrapperPass::print
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
Definition:BlockFrequencyInfo.cpp:311
llvm::BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass
~BlockFrequencyInfoWrapperPass() override
llvm::BlockFrequencyInfoWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition:BlockFrequencyInfo.cpp:316
llvm::BlockFrequencyInfoWrapperPass::getBFI
BlockFrequencyInfo & getBFI()
Definition:BlockFrequencyInfo.h:150
llvm::BlockFrequencyInfoWrapperPass::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition:BlockFrequencyInfo.cpp:324
llvm::BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass
BlockFrequencyInfoWrapperPass()
Definition:BlockFrequencyInfo.cpp:304
llvm::BlockFrequencyInfoWrapperPass::getBFI
const BlockFrequencyInfo & getBFI() const
Definition:BlockFrequencyInfo.h:151
llvm::BlockFrequencyInfoWrapperPass::releaseMemory
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition:BlockFrequencyInfo.cpp:322
llvm::BlockFrequencyInfoWrapperPass::ID
static char ID
Definition:BlockFrequencyInfo.h:145
llvm::BlockFrequencyInfo
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition:BlockFrequencyInfo.h:37
llvm::BlockFrequencyInfo::isIrrLoopHeader
bool isIrrLoopHeader(const BasicBlock *BB)
Returns true if BB is an irreducible loop header block.
Definition:BlockFrequencyInfo.cpp:220
llvm::BlockFrequencyInfo::calculate
void calculate(const Function &F, const BranchProbabilityInfo &BPI, const LoopInfo &LI)
calculate - compute block frequency info for the given function.
Definition:BlockFrequencyInfo.cpp:184
llvm::BlockFrequencyInfo::getProfileCountFromFreq
std::optional< uint64_t > getProfileCountFromFreq(BlockFrequency Freq) const
Returns the estimated profile count of Freq.
Definition:BlockFrequencyInfo.cpp:214
llvm::BlockFrequencyInfo::setBlockFreq
void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq)
Definition:BlockFrequencyInfo.cpp:225
llvm::BlockFrequencyInfo::~BlockFrequencyInfo
~BlockFrequencyInfo()
llvm::BlockFrequencyInfo::getFunction
const Function * getFunction() const
Definition:BlockFrequencyInfo.cpp:259
llvm::BlockFrequencyInfo::getBlockProfileCount
std::optional< uint64_t > getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic=false) const
Returns the estimated profile count of BB.
Definition:BlockFrequencyInfo.cpp:205
llvm::BlockFrequencyInfo::operator=
BlockFrequencyInfo & operator=(const BlockFrequencyInfo &)=delete
llvm::BlockFrequencyInfo::BlockFrequencyInfo
BlockFrequencyInfo()
llvm::BlockFrequencyInfo::view
void view(StringRef="BlockFrequencyDAGs") const
Pop up a ghostview window with the current block frequency propagation rendered using dot.
Definition:BlockFrequencyInfo.cpp:255
llvm::BlockFrequencyInfo::setBlockFreqAndScale
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...
Definition:BlockFrequencyInfo.cpp:231
llvm::BlockFrequencyInfo::getBPI
const BranchProbabilityInfo * getBPI() const
Definition:BlockFrequencyInfo.cpp:263
llvm::BlockFrequencyInfo::getEntryFreq
BlockFrequency getEntryFreq() const
Definition:BlockFrequencyInfo.cpp:267
llvm::BlockFrequencyInfo::getBlockFreq
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
Definition:BlockFrequencyInfo.cpp:200
llvm::BlockFrequencyInfo::print
void print(raw_ostream &OS) const
Definition:BlockFrequencyInfo.cpp:273
llvm::BlockFrequencyInfo::releaseMemory
void releaseMemory()
Definition:BlockFrequencyInfo.cpp:271
llvm::BlockFrequencyInfo::invalidate
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
Definition:BlockFrequencyInfo.cpp:175
llvm::BlockFrequencyInfo::verifyMatch
void verifyMatch(BlockFrequencyInfo &Other) const
Definition:BlockFrequencyInfo.cpp:278
llvm::BlockFrequencyInfo::BlockFrequencyInfo
BlockFrequencyInfo(const BlockFrequencyInfo &)=delete
llvm::BlockFrequencyPrinterPass
Printer pass for the BlockFrequencyInfo results.
Definition:BlockFrequencyInfo.h:129
llvm::BlockFrequencyPrinterPass::BlockFrequencyPrinterPass
BlockFrequencyPrinterPass(raw_ostream &OS)
Definition:BlockFrequencyInfo.h:133
llvm::BlockFrequencyPrinterPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition:BlockFrequencyInfo.cpp:343
llvm::BlockFrequencyPrinterPass::isRequired
static bool isRequired()
Definition:BlockFrequencyInfo.h:137
llvm::BlockFrequency
Definition:BlockFrequency.h:26
llvm::BranchProbabilityInfo
Analysis providing branch probability information.
Definition:BranchProbabilityInfo.h:112
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition:Pass.h:310
llvm::Function
Definition:Function.h:63
llvm::LoopInfo
Definition:LoopInfo.h:407
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition:Module.h:65
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition:Analysis.h:111
llvm::Printable
Simple wrapper around std::function<void(raw_ostream&)>.
Definition:Printable.h:38
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition:SmallPtrSet.h:363
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition:ISDOpcodes.h:71
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::PGOViewCountsType
PGOViewCountsType
Definition:BlockFrequencyInfo.h:33
llvm::PGOVCT_Graph
@ PGOVCT_Graph
Definition:BlockFrequencyInfo.h:33
llvm::PGOVCT_None
@ PGOVCT_None
Definition:BlockFrequencyInfo.h:33
llvm::PGOVCT_Text
@ PGOVCT_Text
Definition:BlockFrequencyInfo.h:33
llvm::IRMemLocation::Other
@ Other
Any other memory.
llvm::printBlockFreq
Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq)
Print the block frequency Freq relative to the current functions entry frequency.
Definition:BlockFrequencyInfo.cpp:283
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition:PassManager.h:92
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition:Analysis.h:28
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition:PassManager.h:69

Generated on Thu Jul 17 2025 08:25:58 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp