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