llvm.org GIT mirror llvm / b17a45c
[BPI] Add new LazyBPI analysis Summary: The motivation is the same as in D22141: In order to add the hotness attribute to optimization remarks we need BFI to be available in all passes that emit optimization remarks. BFI depends on BPI so unless we make this lazy as well we would still compute BPI unconditionally. The solution is to use the new LazyBPI pass in LazyBFI and only compute BPI when computation of BFI is requested by the client. I extended the laziness test using a LoopDistribute test to also cover BPI. Reviewers: hfinkel, davidxl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D22835 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@277083 91177308-0d34-0410-b5e6-96231b3b80d8 Adam Nemet 4 years ago
9 changed file(s) with 200 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
1717 #define LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
1818
1919 #include "llvm/Analysis/BlockFrequencyInfo.h"
20 #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
2021 #include "llvm/Pass.h"
2122
2223 namespace llvm {
5657 class LazyBlockFrequencyInfo {
5758 public:
5859 LazyBlockFrequencyInfo()
59 : Calculated(false), F(nullptr), BPI(nullptr), LI(nullptr) {}
60 : Calculated(false), F(nullptr), BPIPass(nullptr), LI(nullptr) {}
6061
6162 /// Set up the per-function input.
62 void setAnalysis(const Function *F, const BranchProbabilityInfo *BPI,
63 void setAnalysis(const Function *F, LazyBranchProbabilityInfoPass *BPIPass,
6364 const LoopInfo *LI) {
6465 this->F = F;
65 this->BPI = BPI;
66 this->BPIPass = BPIPass;
6667 this->LI = LI;
6768 }
6869
6970 /// Retrieve the BFI with the block frequencies computed.
7071 BlockFrequencyInfo &getCalculated() {
7172 if (!Calculated) {
72 assert(F && BPI && LI && "call setAnalysis");
73 BFI.calculate(*F, *BPI, *LI);
73 assert(F && BPIPass && LI && "call setAnalysis");
74 BFI.calculate(*F, BPIPass->getBPI(), *LI);
7475 Calculated = true;
7576 }
7677 return BFI;
9091 BlockFrequencyInfo BFI;
9192 bool Calculated;
9293 const Function *F;
93 const BranchProbabilityInfo *BPI;
94 LazyBranchProbabilityInfoPass *BPIPass;
9495 const LoopInfo *LI;
9596 };
9697
0 //===- LazyBranchProbabilityInfo.h - Lazy Branch Probability ----*- 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 //
9 // This is an alternative analysis pass to BranchProbabilityInfoWrapperPass.
10 // The difference is that with this pass the branch probabilities are not
11 // computed when the analysis pass is executed but rather when the BPI results
12 // is explicitly requested by the analysis client.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
17 #define LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
18
19 #include "llvm/Analysis/BranchProbabilityInfo.h"
20 #include "llvm/Pass.h"
21
22 namespace llvm {
23 class AnalysisUsage;
24 class Function;
25 class LoopInfo;
26
27 /// \brief This is an alternative analysis pass to
28 /// BranchProbabilityInfoWrapperPass. The difference is that with this pass the
29 /// branch probabilities are not computed when the analysis pass is executed but
30 /// rather when the BPI results is explicitly requested by the analysis client.
31 ///
32 /// There are some additional requirements for any client pass that wants to use
33 /// the analysis:
34 ///
35 /// 1. The pass needs to initialize dependent passes with:
36 ///
37 /// INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)
38 ///
39 /// 2. Similarly, getAnalysisUsage should call:
40 ///
41 /// LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU)
42 ///
43 /// 3. The computed BPI should be requested with
44 /// getAnalysis().getBPI() before LoopInfo
45 /// could be invalidated for example by changing the CFG.
46 ///
47 /// Note that it is expected that we wouldn't need this functionality for the
48 /// new PM since with the new PM, analyses are executed on demand.
49 class LazyBranchProbabilityInfoPass : public FunctionPass {
50
51 /// Wraps a BPI to allow lazy computation of the branch probabilities.
52 ///
53 /// A pass that only conditionally uses BPI can uncondtionally require the
54 /// analysis without paying for the overhead if BPI doesn't end up being used.
55 class LazyBranchProbabilityInfo {
56 public:
57 LazyBranchProbabilityInfo(const Function *F, const LoopInfo *LI)
58 : Calculated(false), F(F), LI(LI) {}
59
60 /// Retrieve the BPI with the branch probabilities computed.
61 BranchProbabilityInfo &getCalculated() {
62 if (!Calculated) {
63 assert(F && LI && "call setAnalysis");
64 BPI.calculate(*F, *LI);
65 Calculated = true;
66 }
67 return BPI;
68 }
69
70 const BranchProbabilityInfo &getCalculated() const {
71 return const_cast(this)->getCalculated();
72 }
73
74 private:
75 BranchProbabilityInfo BPI;
76 bool Calculated;
77 const Function *F;
78 const LoopInfo *LI;
79 };
80
81 std::unique_ptr LBPI;
82
83 public:
84 static char ID;
85
86 LazyBranchProbabilityInfoPass();
87
88 /// \brief Compute and return the branch probabilities.
89 BranchProbabilityInfo &getBPI() { return LBPI->getCalculated(); }
90
91 /// \brief Compute and return the branch probabilities.
92 const BranchProbabilityInfo &getBPI() const { return LBPI->getCalculated(); }
93
94 void getAnalysisUsage(AnalysisUsage &AU) const override;
95
96 /// Helper for client passes to set up the analysis usage on behalf of this
97 /// pass.
98 static void getLazyBPIAnalysisUsage(AnalysisUsage &AU);
99
100 bool runOnFunction(Function &F) override;
101 void releaseMemory() override;
102 void print(raw_ostream &OS, const Module *M) const override;
103 };
104
105 /// \brief Helper for client passes to initialize dependent passes for LBPI.
106 void initializeLazyBPIPassPass(PassRegistry &Registry);
107 }
108 #endif
164164 void initializeJumpThreadingPass(PassRegistry&);
165165 void initializeLCSSAWrapperPassPass(PassRegistry &);
166166 void initializeLegacyLICMPassPass(PassRegistry&);
167 void initializeLazyBranchProbabilityInfoPassPass(PassRegistry&);
167168 void initializeLazyBlockFrequencyInfoPassPass(PassRegistry&);
168169 void initializeLazyValueInfoWrapperPassPass(PassRegistry&);
169170 void initializeLintPass(PassRegistry&);
5353 initializeIVUsersWrapperPassPass(Registry);
5454 initializeInstCountPass(Registry);
5555 initializeIntervalPartitionPass(Registry);
56 initializeLazyBranchProbabilityInfoPassPass(Registry);
5657 initializeLazyBlockFrequencyInfoPassPass(Registry);
5758 initializeLazyValueInfoWrapperPassPass(Registry);
5859 initializeLintPass(Registry);
3636 Interval.cpp
3737 IntervalPartition.cpp
3838 IteratedDominanceFrontier.cpp
39 LazyBranchProbabilityInfo.cpp
3940 LazyBlockFrequencyInfo.cpp
4041 LazyCallGraph.cpp
4142 LazyValueInfo.cpp
1414 //===----------------------------------------------------------------------===//
1515
1616 #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
17 #include "llvm/Analysis/BranchProbabilityInfo.h"
17 #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
1818 #include "llvm/Analysis/LoopInfo.h"
1919
2020 using namespace llvm;
2323
2424 INITIALIZE_PASS_BEGIN(LazyBlockFrequencyInfoPass, DEBUG_TYPE,
2525 "Lazy Block Frequency Analysis", true, true)
26 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
26 INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)
2727 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
2828 INITIALIZE_PASS_END(LazyBlockFrequencyInfoPass, DEBUG_TYPE,
2929 "Lazy Block Frequency Analysis", true, true)
3939 }
4040
4141 void LazyBlockFrequencyInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired();
42 LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU);
4343 AU.addRequired();
4444 AU.setPreservesAll();
4545 }
4747 void LazyBlockFrequencyInfoPass::releaseMemory() { LBFI.releaseMemory(); }
4848
4949 bool LazyBlockFrequencyInfoPass::runOnFunction(Function &F) {
50 BranchProbabilityInfo &BPI =
51 getAnalysis().getBPI();
50 auto &BPIPass = getAnalysis();
5251 LoopInfo &LI = getAnalysis().getLoopInfo();
53 LBFI.setAnalysis(&F, &BPI, &LI);
52 LBFI.setAnalysis(&F, &BPIPass, &LI);
5453 return false;
5554 }
5655
5756 void LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AnalysisUsage &AU) {
58 AU.addRequired();
57 LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU);
5958 AU.addRequired();
6059 AU.addRequired();
6160 }
6261
6362 void llvm::initializeLazyBFIPassPass(PassRegistry &Registry) {
64 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass);
63 initializeLazyBPIPassPass(Registry);
6564 INITIALIZE_PASS_DEPENDENCY(LazyBlockFrequencyInfoPass);
6665 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
6766 }
0 //===- LazyBranchProbabilityInfo.cpp - Lazy Branch Probability Analysis ---===//
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 //
9 // This is an alternative analysis pass to BranchProbabilityInfoWrapperPass.
10 // The difference is that with this pass the branch probabilities are not
11 // computed when the analysis pass is executed but rather when the BPI results
12 // is explicitly requested by the analysis client.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
17 #include "llvm/Analysis/LoopInfo.h"
18
19 using namespace llvm;
20
21 #define DEBUG_TYPE "lazy-branch-prob"
22
23 INITIALIZE_PASS_BEGIN(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
24 "Lazy Branch Probability Analysis", true, true)
25 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
26 INITIALIZE_PASS_END(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
27 "Lazy Branch Probability Analysis", true, true)
28
29 char LazyBranchProbabilityInfoPass::ID = 0;
30
31 LazyBranchProbabilityInfoPass::LazyBranchProbabilityInfoPass()
32 : FunctionPass(ID) {
33 initializeLazyBranchProbabilityInfoPassPass(*PassRegistry::getPassRegistry());
34 }
35
36 void LazyBranchProbabilityInfoPass::print(raw_ostream &OS,
37 const Module *) const {
38 LBPI->getCalculated().print(OS);
39 }
40
41 void LazyBranchProbabilityInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired();
43 AU.setPreservesAll();
44 }
45
46 void LazyBranchProbabilityInfoPass::releaseMemory() { LBPI.reset(); }
47
48 bool LazyBranchProbabilityInfoPass::runOnFunction(Function &F) {
49 LoopInfo &LI = getAnalysis().getLoopInfo();
50 LBPI = llvm::make_unique(&F, &LI);
51 return false;
52 }
53
54 void LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AnalysisUsage &AU) {
55 AU.addRequired();
56 AU.addRequired();
57 }
58
59 void llvm::initializeLazyBPIPassPass(PassRegistry &Registry) {
60 INITIALIZE_PASS_DEPENDENCY(LazyBranchProbabilityInfoPass);
61 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
62 }
0 ; RUN: opt < %s -analyze -branch-prob | FileCheck %s
1 ; RUN: opt < %s -analyze -lazy-branch-prob | FileCheck %s
12 ; RUN: opt < %s -passes='print' -disable-output 2>&1 | FileCheck %s
23
34 define i32 @test1(i32 %i, i32* %a) {
0 ; Check that BFI is not computed when -pass-remarks-with-hotness is off
11
22 ; RUN: opt -loop-distribute -S -pass-remarks-missed=loop-distribute \
3 ; RUN: -debug-only=block-freq -pass-remarks-with-hotness < %s 2>&1 | FileCheck %s --check-prefix=HOTNESS
3 ; RUN: -debug-only=block-freq,branch-prob -pass-remarks-with-hotness \
4 ; RUN: < %s 2>&1 | FileCheck %s --check-prefix=HOTNESS
45 ; RUN: opt -loop-distribute -S -pass-remarks-missed=loop-distribute \
5 ; RUN: -debug-only=block-freq < %s 2>&1 | FileCheck %s --check-prefix=NO_HOTNESS
6 ; RUN: -debug-only=block-freq,branch-prob \
7 ; RUN: < %s 2>&1 | FileCheck %s --check-prefix=NO_HOTNESS
68
79 ; RUN: opt -passes='require,loop-distribute' -S -pass-remarks-missed=loop-distribute \
8 ; RUN: -debug-only=block-freq -pass-remarks-with-hotness < %s 2>&1 | FileCheck %s --check-prefix=HOTNESS
10 ; RUN: -debug-only=block-freq,branch-prob -pass-remarks-with-hotness \
11 ; RUN: < %s 2>&1 | FileCheck %s --check-prefix=HOTNESS
912 ; RUN: opt -passes='require,loop-distribute' -S -pass-remarks-missed=loop-distribute \
10 ; RUN: -debug-only=block-freq < %s 2>&1 | FileCheck %s --check-prefix=NO_HOTNESS
13 ; RUN: -debug-only=block-freq,branch-prob \
14 ; RUN: < %s 2>&1 | FileCheck %s --check-prefix=NO_HOTNESS
1115
1216 ; REQUIRES: asserts
1317
18 ; HOTNESS: Branch Probability Info : forced
1419 ; HOTNESS: block-frequency: forced
20 ; NO_HOTNESS-NOT: Branch Probability Info : forced
1521 ; NO_HOTNESS-NOT: block-frequency: forced
1622
1723 ; This is the input program: