llvm.org GIT mirror llvm / 1174583
Add new pass LazyMachineBlockFrequencyInfo And use it in MachineOptimizationRemarkEmitter. A test will follow on top of Justin's changes to enable MachineORE in AsmPrinter. The approach is similar to the IR-level pass. It's a bit simpler because BPI is immutable at the Machine level so we don't need to make that lazy. Because of this, a new function mapping is introduced (BPIPassTrait::getBPI). This function extracts BPI from the pass. In case of the lazy pass, this is when the calculation of the BFI occurs. For Machine-level, this is the identity function. Differential Revision: https://reviews.llvm.org/D29836 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@295072 91177308-0d34-0410-b5e6-96231b3b80d8 Adam Nemet 3 years ago
9 changed file(s) with 193 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
4949 BlockFrequencyInfoT &getCalculated() {
5050 if (!Calculated) {
5151 assert(F && BPIPass && LI && "call setAnalysis");
52 BFI.calculate(*F, BPIPass->getBPI(), *LI);
52 BFI.calculate(
53 *F, BPIPassTrait::getBPI(BPIPass), *LI);
5354 Calculated = true;
5455 }
5556 return BFI;
104104
105105 /// \brief Helper for client passes to initialize dependent passes for LBPI.
106106 void initializeLazyBPIPassPass(PassRegistry &Registry);
107
108 /// \brief Simple trait class that provides a mapping between BPI passes and the
109 /// corresponding BPInfo.
110 template struct BPIPassTrait {
111 static PassT &getBPI(PassT *P) { return *P; }
112 };
113
114 template <> struct BPIPassTrait {
115 static BranchProbabilityInfo &getBPI(LazyBranchProbabilityInfoPass *P) {
116 return P->getBPI();
117 }
118 };
107119 }
108120 #endif
0 ///===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*--===//
1 ///
2 /// The LLVM Compiler Infrastructure
3 ///
4 /// This file is distributed under the University of Illinois Open Source
5 /// License. See LICENSE.TXT for details.
6 ///
7 ///===---------------------------------------------------------------------===//
8 /// \file
9 /// This is an alternative analysis pass to MachineBlockFrequencyInfo. The
10 /// difference is that with this pass the block frequencies are not computed
11 /// when the analysis pass is executed but rather when the BFI result is
12 /// explicitly requested by the analysis client.
13 ///
14 ///===---------------------------------------------------------------------===//
15
16 #ifndef LLVM_ANALYSIS_LAZYMACHINEBLOCKFREQUENCYINFO_H
17 #define LLVM_ANALYSIS_LAZYMACHINEBLOCKFREQUENCYINFO_H
18
19 #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
20 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
21 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
22 #include "llvm/CodeGen/MachineLoopInfo.h"
23
24 namespace llvm {
25 /// \brief This is an alternative analysis pass to MachineBlockFrequencyInfo.
26 /// The difference is that with this pass, the block frequencies are not
27 /// computed when the analysis pass is executed but rather when the BFI result
28 /// is explicitly requested by the analysis client.
29 ///
30 /// There are some additional requirements for any client pass that wants to use
31 /// the analysis:
32 ///
33 /// 1. The pass needs to initialize dependent passes with:
34 ///
35 /// INITIALIZE_PASS_DEPENDENCY(LazyMachineBFIPass)
36 ///
37 /// 2. Similarly, getAnalysisUsage should call:
38 ///
39 /// LazyMachineBlockFrequencyInfoPass::getLazyMachineBFIAnalysisUsage(AU)
40 ///
41 /// 3. The computed MachineBFI should be requested with
42 /// getAnalysis().getBFI() before
43 /// MachineLoopInfo could be invalidated for example by changing the CFG.
44 ///
45 /// Note that it is expected that we wouldn't need this functionality for the
46 /// new PM since with the new PM, analyses are executed on demand.
47
48 class LazyMachineBlockFrequencyInfoPass : public MachineFunctionPass {
49 private:
50 /// \brief Machine BPI is an immutable pass, no need to use it lazily.
51 LazyBlockFrequencyInfo
52 MachineLoopInfo, MachineBlockFrequencyInfo>
53 LMBFI;
54
55 public:
56 static char ID;
57
58 LazyMachineBlockFrequencyInfoPass();
59
60 /// \brief Compute and return the block frequencies.
61 MachineBlockFrequencyInfo &getBFI() { return LMBFI.getCalculated(); }
62
63 /// \brief Compute and return the block frequencies.
64 const MachineBlockFrequencyInfo &getBFI() const {
65 return LMBFI.getCalculated();
66 }
67
68 void getAnalysisUsage(AnalysisUsage &AU) const override;
69
70 /// Helper for client passes to set up the analysis usage on behalf of this
71 /// pass.
72 static void getLazyMachineBFIAnalysisUsage(AnalysisUsage &AU);
73
74 bool runOnMachineFunction(MachineFunction &F) override;
75 void releaseMemory() override;
76 void print(raw_ostream &OS, const Module *M) const override;
77 };
78
79 /// \brief Helper for client passes to initialize dependent passes for LMBFI.
80 void initializeLazyMachineBFIPassPass(PassRegistry &Registry);
81 }
82 #endif
2222
2323 class MachineBasicBlock;
2424 class MachineBranchProbabilityInfo;
25 class MachineLoopInfo;
2526 template class BlockFrequencyInfoImpl;
2627
2728 /// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
4041 void getAnalysisUsage(AnalysisUsage &AU) const override;
4142
4243 bool runOnMachineFunction(MachineFunction &F) override;
44
45 /// calculate - compute block frequency info for the given function.
46 void calculate(const MachineFunction &F,
47 const MachineBranchProbabilityInfo &MBPI,
48 const MachineLoopInfo &MLI);
4349
4450 void releaseMemory() override;
4551
174174 void initializeLegacyLICMPassPass(PassRegistry&);
175175 void initializeLegacyLoopSinkPassPass(PassRegistry&);
176176 void initializeLazyBranchProbabilityInfoPassPass(PassRegistry&);
177 void initializeLazyMachineBlockFrequencyInfoPassPass(PassRegistry&);
177178 void initializeLazyBlockFrequencyInfoPassPass(PassRegistry&);
178179 void initializeLazyValueInfoWrapperPassPass(PassRegistry&);
179180 void initializeLegalizerPass(PassRegistry&);
3636 InterleavedAccessPass.cpp
3737 IntrinsicLowering.cpp
3838 LatencyPriorityQueue.cpp
39 LazyMachineBlockFrequencyInfo.cpp
3940 LexicalScopes.cpp
4041 LiveDebugValues.cpp
4142 LiveDebugVariables.cpp
0 ///===- LazyMachineBlockFrequencyInfo.cpp - Lazy Machine Block Frequency --===//
1 ///
2 /// The LLVM Compiler Infrastructure
3 ///
4 /// This file is distributed under the University of Illinois Open Source
5 /// License. See LICENSE.TXT for details.
6 ///
7 ///===---------------------------------------------------------------------===//
8 /// \file
9 /// This is an alternative analysis pass to MachineBlockFrequencyInfo. The
10 /// difference is that with this pass the block frequencies are not computed
11 /// when the analysis pass is executed but rather when the BFI result is
12 /// explicitly requested by the analysis client.
13 ///
14 ///===---------------------------------------------------------------------===//
15
16 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
17
18 using namespace llvm;
19
20 #define DEBUG_TYPE "lazy-machine-block-freq"
21
22 INITIALIZE_PASS_BEGIN(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
23 "Lazy Machine Block Frequency Analysis", true, true)
24 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
25 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
26 INITIALIZE_PASS_END(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
27 "Lazy Machine Block Frequency Analysis", true, true)
28
29 char LazyMachineBlockFrequencyInfoPass::ID = 0;
30
31 LazyMachineBlockFrequencyInfoPass::LazyMachineBlockFrequencyInfoPass()
32 : MachineFunctionPass(ID) {
33 initializeLazyMachineBlockFrequencyInfoPassPass(
34 *PassRegistry::getPassRegistry());
35 }
36
37 void LazyMachineBlockFrequencyInfoPass::print(raw_ostream &OS,
38 const Module *M) const {
39 LMBFI.getCalculated().print(OS, M);
40 }
41
42 void LazyMachineBlockFrequencyInfoPass::getAnalysisUsage(
43 AnalysisUsage &AU) const {
44 AU.addRequired();
45 AU.addRequired();
46 AU.setPreservesAll();
47 MachineFunctionPass::getAnalysisUsage(AU);
48 }
49
50 void LazyMachineBlockFrequencyInfoPass::releaseMemory() {
51 LMBFI.releaseMemory();
52 }
53
54 bool LazyMachineBlockFrequencyInfoPass::runOnMachineFunction(
55 MachineFunction &MF) {
56 auto &BPIPass = getAnalysis();
57 auto &LI = getAnalysis();
58 LMBFI.setAnalysis(&MF, &BPIPass, &LI);
59 return false;
60 }
61
62 void LazyMachineBlockFrequencyInfoPass::getLazyMachineBFIAnalysisUsage(
63 AnalysisUsage &AU) {
64 AU.addRequired();
65 AU.addRequired();
66 AU.addRequired();
67 }
68
69 void llvm::initializeLazyMachineBFIPassPass(PassRegistry &Registry) {
70 INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass);
71 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo);
72 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo);
73 }
171171 MachineFunctionPass::getAnalysisUsage(AU);
172172 }
173173
174 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
175 MachineBranchProbabilityInfo &MBPI =
176 getAnalysis();
177 MachineLoopInfo &MLI = getAnalysis();
174 void MachineBlockFrequencyInfo::calculate(
175 const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
176 const MachineLoopInfo &MLI) {
178177 if (!MBFI)
179178 MBFI.reset(new ImplType);
180179 MBFI->calculate(F, MBPI, MLI);
183182 F.getName().equals(ViewBlockFreqFuncName))) {
184183 view();
185184 }
185 }
186
187 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
188 MachineBranchProbabilityInfo &MBPI =
189 getAnalysis();
190 MachineLoopInfo &MLI = getAnalysis();
191 calculate(F, MBPI, MLI);
186192 return false;
187193 }
188194
1313 ///===---------------------------------------------------------------------===//
1414
1515 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
16 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
16 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
1717 #include "llvm/IR/DebugInfo.h"
1818 #include "llvm/IR/DiagnosticInfo.h"
1919 #include "llvm/IR/LLVMContext.h"
6363 MachineBlockFrequencyInfo *MBFI;
6464
6565 if (MF.getFunction()->getContext().getDiagnosticHotnessRequested())
66 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
66 MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
6767 else
6868 MBFI = nullptr;
6969
7373
7474 void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
7575 AnalysisUsage &AU) const {
76 AU.addRequired();
76 LazyMachineBlockFrequencyInfoPass::getLazyMachineBFIAnalysisUsage(AU);
7777 AU.setPreservesAll();
7878 MachineFunctionPass::getAnalysisUsage(AU);
7979 }
8484
8585 INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
8686 false, true)
87 INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
87 INITIALIZE_PASS_DEPENDENCY(LazyMachineBFIPass)
8888 INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
8989 false, true)