LLVM 20.0.0git
LazyBlockFrequencyInfo.h
Go to the documentation of this file.
1//===- LazyBlockFrequencyInfo.h - Lazy 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// This is an alternative analysis pass to BlockFrequencyInfoWrapperPass. The
10// difference is that with this pass the block frequencies are not computed when
11// the analysis pass is executed but rather when the BFI result is explicitly
12// requested by the analysis client.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
17#define LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
18
21#include "llvm/Pass.h"
22
23namespace llvm {
24class Function;
25class LoopInfo;
26
27/// Wraps a BFI to allow lazy computation of the block frequencies.
28///
29/// A pass that only conditionally uses BFI can uncondtionally require the
30/// analysis without paying for the overhead if BFI doesn't end up being used.
31template <typename FunctionT, typename BranchProbabilityInfoPassT,
32 typename LoopInfoT, typename BlockFrequencyInfoT>
34public:
36
37 /// Set up the per-function input.
38 void setAnalysis(const FunctionT *F, BranchProbabilityInfoPassT *BPIPass,
39 const LoopInfoT *LI) {
40 this->F = F;
41 this->BPIPass = BPIPass;
42 this->LI = LI;
43 }
44
45 /// Retrieve the BFI with the block frequencies computed.
46 BlockFrequencyInfoT &getCalculated() {
47 if (!Calculated) {
48 assert(F && BPIPass && LI && "call setAnalysis");
49 BFI.calculate(
51 Calculated = true;
52 }
53 return BFI;
54 }
55
56 const BlockFrequencyInfoT &getCalculated() const {
57 return const_cast<LazyBlockFrequencyInfo *>(this)->getCalculated();
58 }
59
61 BFI.releaseMemory();
62 Calculated = false;
63 setAnalysis(nullptr, nullptr, nullptr);
64 }
65
66private:
67 BlockFrequencyInfoT BFI;
68 bool Calculated = false;
69 const FunctionT *F = nullptr;
70 BranchProbabilityInfoPassT *BPIPass = nullptr;
71 const LoopInfoT *LI = nullptr;
72};
73
74/// This is an alternative analysis pass to
75/// BlockFrequencyInfoWrapperPass. The difference is that with this pass the
76/// block frequencies are not computed when the analysis pass is executed but
77/// rather when the BFI result is explicitly requested by the analysis client.
78///
79/// There are some additional requirements for any client pass that wants to use
80/// the analysis:
81///
82/// 1. The pass needs to initialize dependent passes with:
83///
84/// INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
85///
86/// 2. Similarly, getAnalysisUsage should call:
87///
88/// LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU)
89///
90/// 3. The computed BFI should be requested with
91/// getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() before either LoopInfo
92/// or BPI could be invalidated for example by changing the CFG.
93///
94/// Note that it is expected that we wouldn't need this functionality for the
95/// new PM since with the new PM, analyses are executed on demand.
96
98private:
101 LBFI;
102
103public:
104 static char ID;
105
107
108 /// Compute and return the block frequencies.
109 BlockFrequencyInfo &getBFI() { return LBFI.getCalculated(); }
110
111 /// Compute and return the block frequencies.
112 const BlockFrequencyInfo &getBFI() const { return LBFI.getCalculated(); }
113
114 void getAnalysisUsage(AnalysisUsage &AU) const override;
115
116 /// Helper for client passes to set up the analysis usage on behalf of this
117 /// pass.
118 static void getLazyBFIAnalysisUsage(AnalysisUsage &AU);
119
120 bool runOnFunction(Function &F) override;
121 void releaseMemory() override;
122 void print(raw_ostream &OS, const Module *M) const override;
123};
124
125/// Helper for client passes to initialize dependent passes for LBFI.
126void initializeLazyBFIPassPass(PassRegistry &Registry);
127} // namespace llvm
128#endif
#define F(x, y, z)
Definition: MD5.cpp:55
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Represent the analysis usage information of a pass.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
const BlockFrequencyInfo & getBFI() const
Compute and return the block frequencies.
BlockFrequencyInfo & getBFI()
Compute and return the block frequencies.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
static void getLazyBFIAnalysisUsage(AnalysisUsage &AU)
Helper for client passes to set up the analysis usage on behalf of this pass.
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...
Wraps a BFI to allow lazy computation of the block frequencies.
const BlockFrequencyInfoT & getCalculated() const
BlockFrequencyInfoT & getCalculated()
Retrieve the BFI with the block frequencies computed.
void setAnalysis(const FunctionT *F, BranchProbabilityInfoPassT *BPIPass, const LoopInfoT *LI)
Set up the per-function input.
This is an alternative analysis pass to BranchProbabilityInfoWrapperPass.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeLazyBFIPassPass(PassRegistry &Registry)
Helper for client passes to initialize dependent passes for LBFI.
Simple trait class that provides a mapping between BPI passes and the corresponding BPInfo.