Skip to content

Commit b516cf3

Browse files
committedFeb 23, 2017
[LazyMachineBFI] Reimplement with getAnalysisIfAvailable
Since LoopInfo is not available in machine passes as universally as in IR passes, using the same approach for OptimizationRemarkEmitter as we did for IR will run LoopInfo and DominatorTree unnecessarily. (LoopInfo is not used lazily by ORE.) To fix this, I am modifying the approach I took in D29836. LazyMachineBFI now uses its client passes including MachineBFI itself that are available or otherwise compute them on the fly. So for example GreedyRegAlloc, since it's already using MBFI, will reuse that instance. On the other hand, AsmPrinter in Justin's patch will generate DT, LI and finally BFI on the fly. (I am of course wondering now if the simplicity of this approach is even preferable in IR. I will do some experiments.) Testing is provided by an updated version of D29837 which requires Justin's patch to bring ORE to the AsmPrinter. Differential Revision: https://reviews.llvm.org/D30128 llvm-svn: 295996
1 parent 3f4181d commit b516cf3

File tree

3 files changed

+65
-49
lines changed

3 files changed

+65
-49
lines changed
 

‎llvm/include/llvm/CodeGen/LazyMachineBlockFrequencyInfo.h

+21-28
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#ifndef LLVM_ANALYSIS_LAZYMACHINEBLOCKFREQUENCYINFO_H
1818
#define LLVM_ANALYSIS_LAZYMACHINEBLOCKFREQUENCYINFO_H
1919

20-
#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
2120
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
2221
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
22+
#include "llvm/CodeGen/MachineDominators.h"
2323
#include "llvm/CodeGen/MachineLoopInfo.h"
2424

2525
namespace llvm {
@@ -28,56 +28,49 @@ namespace llvm {
2828
/// computed when the analysis pass is executed but rather when the BFI result
2929
/// is explicitly requested by the analysis client.
3030
///
31-
/// There are some additional requirements for any client pass that wants to use
32-
/// the analysis:
33-
///
34-
/// 1. The pass needs to initialize dependent passes with:
35-
///
36-
/// INITIALIZE_PASS_DEPENDENCY(LazyMachineBFIPass)
37-
///
38-
/// 2. Similarly, getAnalysisUsage should call:
39-
///
40-
/// LazyMachineBlockFrequencyInfoPass::getLazyMachineBFIAnalysisUsage(AU)
41-
///
42-
/// 3. The computed MachineBFI should be requested with
43-
/// getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() before
44-
/// MachineLoopInfo could be invalidated for example by changing the CFG.
31+
/// This works by checking querying if MBFI is available and otherwise
32+
/// generating MBFI on the fly. In this case the passes required for (LI, DT)
33+
/// are also queried before being computed on the fly.
4534
///
4635
/// Note that it is expected that we wouldn't need this functionality for the
4736
/// new PM since with the new PM, analyses are executed on demand.
4837

4938
class LazyMachineBlockFrequencyInfoPass : public MachineFunctionPass {
5039
private:
51-
/// \brief Machine BPI is an immutable pass, no need to use it lazily.
52-
LazyBlockFrequencyInfo<MachineFunction, MachineBranchProbabilityInfo,
53-
MachineLoopInfo, MachineBlockFrequencyInfo>
54-
LMBFI;
40+
/// If generated on the fly this own the instance.
41+
mutable std::unique_ptr<MachineBlockFrequencyInfo> OwnedMBFI;
42+
43+
/// If generated on the fly this own the instance.
44+
mutable std::unique_ptr<MachineLoopInfo> OwnedMLI;
45+
46+
/// If generated on the fly this own the instance.
47+
mutable std::unique_ptr<MachineDominatorTree> OwnedMDT;
48+
49+
/// The function.
50+
MachineFunction *MF = nullptr;
51+
52+
/// \brief Calculate MBFI and all other analyses that's not available and
53+
/// required by BFI.
54+
MachineBlockFrequencyInfo &calculateIfNotAvailable() const;
5555

5656
public:
5757
static char ID;
5858

5959
LazyMachineBlockFrequencyInfoPass();
6060

6161
/// \brief Compute and return the block frequencies.
62-
MachineBlockFrequencyInfo &getBFI() { return LMBFI.getCalculated(); }
62+
MachineBlockFrequencyInfo &getBFI() { return calculateIfNotAvailable(); }
6363

6464
/// \brief Compute and return the block frequencies.
6565
const MachineBlockFrequencyInfo &getBFI() const {
66-
return LMBFI.getCalculated();
66+
return calculateIfNotAvailable();
6767
}
6868

6969
void getAnalysisUsage(AnalysisUsage &AU) const override;
7070

71-
/// Helper for client passes to set up the analysis usage on behalf of this
72-
/// pass.
73-
static void getLazyMachineBFIAnalysisUsage(AnalysisUsage &AU);
74-
7571
bool runOnMachineFunction(MachineFunction &F) override;
7672
void releaseMemory() override;
7773
void print(raw_ostream &OS, const Module *M) const override;
7874
};
79-
80-
/// \brief Helper for client passes to initialize dependent passes for LMBFI.
81-
void initializeLazyMachineBFIPassPass(PassRegistry &Registry);
8275
}
8376
#endif

‎llvm/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp

+42-19
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,61 @@ LazyMachineBlockFrequencyInfoPass::LazyMachineBlockFrequencyInfoPass()
3737

3838
void LazyMachineBlockFrequencyInfoPass::print(raw_ostream &OS,
3939
const Module *M) const {
40-
LMBFI.getCalculated().print(OS, M);
40+
getBFI().print(OS, M);
4141
}
4242

4343
void LazyMachineBlockFrequencyInfoPass::getAnalysisUsage(
4444
AnalysisUsage &AU) const {
4545
AU.addRequired<MachineBranchProbabilityInfo>();
46-
AU.addRequired<MachineLoopInfo>();
4746
AU.setPreservesAll();
4847
MachineFunctionPass::getAnalysisUsage(AU);
4948
}
5049

5150
void LazyMachineBlockFrequencyInfoPass::releaseMemory() {
52-
LMBFI.releaseMemory();
51+
OwnedMBFI.reset();
52+
OwnedMLI.reset();
53+
OwnedMDT.reset();
5354
}
5455

55-
bool LazyMachineBlockFrequencyInfoPass::runOnMachineFunction(
56-
MachineFunction &MF) {
57-
auto &BPIPass = getAnalysis<MachineBranchProbabilityInfo>();
58-
auto &LI = getAnalysis<MachineLoopInfo>();
59-
LMBFI.setAnalysis(&MF, &BPIPass, &LI);
60-
return false;
61-
}
56+
MachineBlockFrequencyInfo &
57+
LazyMachineBlockFrequencyInfoPass::calculateIfNotAvailable() const {
58+
auto *MBFI = getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
59+
if (MBFI) {
60+
DEBUG(dbgs() << "MachineBlockFrequencyInfo is available\n");
61+
return *MBFI;
62+
}
6263

63-
void LazyMachineBlockFrequencyInfoPass::getLazyMachineBFIAnalysisUsage(
64-
AnalysisUsage &AU) {
65-
AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
66-
AU.addRequired<MachineBranchProbabilityInfo>();
67-
AU.addRequired<MachineLoopInfo>();
64+
auto &MBPI = getAnalysis<MachineBranchProbabilityInfo>();
65+
auto *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
66+
auto *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
67+
DEBUG(dbgs() << "Building MachineBlockFrequencyInfo on the fly\n");
68+
DEBUG(if (MLI) dbgs() << "LoopInfo is available\n");
69+
70+
if (!MLI) {
71+
DEBUG(dbgs() << "Building LoopInfo on the fly\n");
72+
// First create a dominator tree.
73+
DEBUG(if (MDT) dbgs() << "DominatorTree is available\n");
74+
75+
if (!MDT) {
76+
DEBUG(dbgs() << "Building DominatorTree on the fly\n");
77+
OwnedMDT = make_unique<MachineDominatorTree>();
78+
OwnedMDT->getBase().recalculate(*MF);
79+
MDT = OwnedMDT.get();
80+
}
81+
82+
// Generate LoopInfo from it.
83+
OwnedMLI = make_unique<MachineLoopInfo>();
84+
OwnedMLI->getBase().analyze(MDT->getBase());
85+
MLI = OwnedMLI.get();
86+
}
87+
88+
OwnedMBFI = make_unique<MachineBlockFrequencyInfo>();
89+
OwnedMBFI->calculate(*MF, MBPI, *MLI);
90+
return *OwnedMBFI.get();
6891
}
6992

70-
void llvm::initializeLazyMachineBFIPassPass(PassRegistry &Registry) {
71-
INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass);
72-
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo);
73-
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo);
93+
bool LazyMachineBlockFrequencyInfoPass::runOnMachineFunction(
94+
MachineFunction &F) {
95+
MF = &F;
96+
return false;
7497
}

‎llvm/lib/CodeGen/MachineOptimizationRemarkEmitter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ bool MachineOptimizationRemarkEmitterPass::runOnMachineFunction(
7474

7575
void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
7676
AnalysisUsage &AU) const {
77-
LazyMachineBlockFrequencyInfoPass::getLazyMachineBFIAnalysisUsage(AU);
77+
AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
7878
AU.setPreservesAll();
7979
MachineFunctionPass::getAnalysisUsage(AU);
8080
}
@@ -85,6 +85,6 @@ static const char ore_name[] = "Machine Optimization Remark Emitter";
8585

8686
INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
8787
false, true)
88-
INITIALIZE_PASS_DEPENDENCY(LazyMachineBFIPass)
88+
INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass)
8989
INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
9090
false, true)

0 commit comments

Comments
 (0)