1 //===- LazyBlockFrequencyInfo.h - Lazy Block Frequency Analysis -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
17 #define LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
19 #include "llvm/Analysis/BlockFrequencyInfo.h"
20 #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
21 #include "llvm/Pass.h"
27 /// Wraps a BFI to allow lazy computation of the block frequencies.
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.
31 template <typename FunctionT
, typename BranchProbabilityInfoPassT
,
32 typename LoopInfoT
, typename BlockFrequencyInfoT
>
33 class LazyBlockFrequencyInfo
{
35 LazyBlockFrequencyInfo() = default;
37 /// Set up the per-function input.
38 void setAnalysis(const FunctionT
*F
, BranchProbabilityInfoPassT
*BPIPass
,
39 const LoopInfoT
*LI
) {
41 this->BPIPass
= BPIPass
;
45 /// Retrieve the BFI with the block frequencies computed.
46 BlockFrequencyInfoT
&getCalculated() {
48 assert(F
&& BPIPass
&& LI
&& "call setAnalysis");
50 *F
, BPIPassTrait
<BranchProbabilityInfoPassT
>::getBPI(BPIPass
), *LI
);
56 const BlockFrequencyInfoT
&getCalculated() const {
57 return const_cast<LazyBlockFrequencyInfo
*>(this)->getCalculated();
60 void releaseMemory() {
63 setAnalysis(nullptr, nullptr, nullptr);
67 BlockFrequencyInfoT BFI
;
68 bool Calculated
= false;
69 const FunctionT
*F
= nullptr;
70 BranchProbabilityInfoPassT
*BPIPass
= nullptr;
71 const LoopInfoT
*LI
= nullptr;
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.
79 /// There are some additional requirements for any client pass that wants to use
82 /// 1. The pass needs to initialize dependent passes with:
84 /// INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
86 /// 2. Similarly, getAnalysisUsage should call:
88 /// LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU)
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.
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.
97 class LazyBlockFrequencyInfoPass
: public FunctionPass
{
99 LazyBlockFrequencyInfo
<Function
, LazyBranchProbabilityInfoPass
, LoopInfo
,
106 LazyBlockFrequencyInfoPass();
108 /// Compute and return the block frequencies.
109 BlockFrequencyInfo
&getBFI() { return LBFI
.getCalculated(); }
111 /// Compute and return the block frequencies.
112 const BlockFrequencyInfo
&getBFI() const { return LBFI
.getCalculated(); }
114 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
116 /// Helper for client passes to set up the analysis usage on behalf of this
118 static void getLazyBFIAnalysisUsage(AnalysisUsage
&AU
);
120 bool runOnFunction(Function
&F
) override
;
121 void releaseMemory() override
;
122 void print(raw_ostream
&OS
, const Module
*M
) const override
;
125 /// Helper for client passes to initialize dependent passes for LBFI.
126 void initializeLazyBFIPassPass(PassRegistry
&Registry
);