Skip to content

Commit abde52a

Browse files
authored
[CodeGen][NewPM] Port LiveIntervals to new pass manager (#98118)
- Add `LiveIntervalsAnalysis`. - Add `LiveIntervalsPrinterPass`. - Use `LiveIntervalsWrapperPass` in legacy pass manager. - Use `std::unique_ptr` instead of raw pointer for `LICalc`, so destructor and default move constructor can handle it correctly. This would be the last analysis required by `PHIElimination`.
1 parent 3e06392 commit abde52a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+504
-337
lines changed

llvm/include/llvm/CodeGen/LiveIntervals.h

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#include "llvm/ADT/IndexedMap.h"
2424
#include "llvm/ADT/SmallVector.h"
2525
#include "llvm/CodeGen/LiveInterval.h"
26+
#include "llvm/CodeGen/LiveIntervalCalc.h"
2627
#include "llvm/CodeGen/MachineBasicBlock.h"
2728
#include "llvm/CodeGen/MachineFunctionPass.h"
29+
#include "llvm/CodeGen/MachinePassManager.h"
2830
#include "llvm/CodeGen/SlotIndexes.h"
2931
#include "llvm/CodeGen/TargetRegisterInfo.h"
3032
#include "llvm/MC/LaneBitmask.h"
@@ -40,7 +42,6 @@ namespace llvm {
4042
extern cl::opt<bool> UseSegmentSetForPhysRegs;
4143

4244
class BitVector;
43-
class LiveIntervalCalc;
4445
class MachineBlockFrequencyInfo;
4546
class MachineDominatorTree;
4647
class MachineFunction;
@@ -50,14 +51,17 @@ class raw_ostream;
5051
class TargetInstrInfo;
5152
class VirtRegMap;
5253

53-
class LiveIntervals : public MachineFunctionPass {
54+
class LiveIntervals {
55+
friend class LiveIntervalsAnalysis;
56+
friend class LiveIntervalsWrapperPass;
57+
5458
MachineFunction *MF = nullptr;
5559
MachineRegisterInfo *MRI = nullptr;
5660
const TargetRegisterInfo *TRI = nullptr;
5761
const TargetInstrInfo *TII = nullptr;
5862
SlotIndexes *Indexes = nullptr;
5963
MachineDominatorTree *DomTree = nullptr;
60-
LiveIntervalCalc *LICalc = nullptr;
64+
std::unique_ptr<LiveIntervalCalc> LICalc;
6165

6266
/// Special pool allocator for VNInfo's (LiveInterval val#).
6367
VNInfo::Allocator VNInfoAllocator;
@@ -93,11 +97,20 @@ class LiveIntervals : public MachineFunctionPass {
9397
/// interference.
9498
SmallVector<LiveRange *, 0> RegUnitRanges;
9599

96-
public:
97-
static char ID;
100+
// Can only be created from pass manager.
101+
LiveIntervals() = default;
102+
LiveIntervals(MachineFunction &MF, SlotIndexes &SI, MachineDominatorTree &DT)
103+
: Indexes(&SI), DomTree(&DT) {
104+
analyze(MF);
105+
}
106+
107+
void analyze(MachineFunction &MF);
98108

99-
LiveIntervals();
100-
~LiveIntervals() override;
109+
void clear();
110+
111+
public:
112+
LiveIntervals(LiveIntervals &&) = default;
113+
~LiveIntervals();
101114

102115
/// Calculate the spill weight to assign to a single instruction.
103116
static float getSpillWeight(bool isDef, bool isUse,
@@ -279,14 +292,17 @@ class LiveIntervals : public MachineFunctionPass {
279292

280293
VNInfo::Allocator &getVNInfoAllocator() { return VNInfoAllocator; }
281294

282-
void getAnalysisUsage(AnalysisUsage &AU) const override;
283-
void releaseMemory() override;
295+
/// Implement the dump method.
296+
void print(raw_ostream &O) const;
297+
void dump() const;
284298

285-
/// Pass entry point; Calculates LiveIntervals.
286-
bool runOnMachineFunction(MachineFunction &) override;
299+
// For legacy pass to recompute liveness.
300+
void reanalyze(MachineFunction &MF) {
301+
clear();
302+
analyze(MF);
303+
}
287304

288-
/// Implement the dump method.
289-
void print(raw_ostream &O, const Module * = nullptr) const override;
305+
MachineDominatorTree &getDomTree() { return *DomTree; }
290306

291307
/// If LI is confined to a single basic block, return a pointer to that
292308
/// block. If LI is live in to or out of any block, return NULL.
@@ -480,6 +496,48 @@ class LiveIntervals : public MachineFunctionPass {
480496
class HMEditor;
481497
};
482498

499+
class LiveIntervalsAnalysis : public AnalysisInfoMixin<LiveIntervalsAnalysis> {
500+
friend AnalysisInfoMixin<LiveIntervalsAnalysis>;
501+
static AnalysisKey Key;
502+
503+
public:
504+
using Result = LiveIntervals;
505+
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
506+
};
507+
508+
class LiveIntervalsPrinterPass
509+
: public PassInfoMixin<LiveIntervalsPrinterPass> {
510+
raw_ostream &OS;
511+
512+
public:
513+
explicit LiveIntervalsPrinterPass(raw_ostream &OS) : OS(OS) {}
514+
PreservedAnalyses run(MachineFunction &MF,
515+
MachineFunctionAnalysisManager &MFAM);
516+
static bool isRequired() { return true; }
517+
};
518+
519+
class LiveIntervalsWrapperPass : public MachineFunctionPass {
520+
LiveIntervals LIS;
521+
522+
public:
523+
static char ID;
524+
525+
LiveIntervalsWrapperPass();
526+
527+
void getAnalysisUsage(AnalysisUsage &AU) const override;
528+
void releaseMemory() override { LIS.clear(); }
529+
530+
/// Pass entry point; Calculates LiveIntervals.
531+
bool runOnMachineFunction(MachineFunction &) override;
532+
533+
/// Implement the dump method.
534+
void print(raw_ostream &O, const Module * = nullptr) const override {
535+
LIS.print(O);
536+
}
537+
538+
LiveIntervals &getLIS() { return LIS; }
539+
};
540+
483541
} // end namespace llvm
484542

485543
#endif

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void initializeGISelCSEAnalysisWrapperPassPass(PassRegistry &);
151151
void initializeGISelKnownBitsAnalysisPass(PassRegistry &);
152152
void initializeLiveDebugValuesPass(PassRegistry&);
153153
void initializeLiveDebugVariablesPass(PassRegistry&);
154-
void initializeLiveIntervalsPass(PassRegistry&);
154+
void initializeLiveIntervalsWrapperPassPass(PassRegistry &);
155155
void initializeLiveRangeShrinkPass(PassRegistry&);
156156
void initializeLiveRegMatrixPass(PassRegistry&);
157157
void initializeLiveStacksPass(PassRegistry&);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/CodeGen/InterleavedAccess.h"
3737
#include "llvm/CodeGen/InterleavedLoadCombine.h"
3838
#include "llvm/CodeGen/JMCInstrumenter.h"
39+
#include "llvm/CodeGen/LiveIntervals.h"
3940
#include "llvm/CodeGen/LocalStackSlotAllocation.h"
4041
#include "llvm/CodeGen/LowerEmuTLS.h"
4142
#include "llvm/CodeGen/MIRPrinter.h"
@@ -1106,7 +1107,7 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addOptimizedRegAlloc(
11061107

11071108
// Eventually, we want to run LiveIntervals before PHI elimination.
11081109
if (Opt.EarlyLiveIntervals)
1109-
addPass(LiveIntervalsPass());
1110+
addPass(RequireAnalysisPass<LiveIntervalsAnalysis, MachineFunction>());
11101111

11111112
addPass(TwoAddressInstructionPass());
11121113
addPass(RegisterCoalescerPass());

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ LOOP_PASS("loop-reduce", LoopStrengthReducePass())
9494
// LiveVariables can be removed completely, and LiveIntervals can be directly
9595
// computed. (We still either need to regenerate kill flags after regalloc, or
9696
// preferably fix the scavenger to not depend on them).
97+
MACHINE_FUNCTION_ANALYSIS("live-intervals", LiveIntervalsAnalysis())
9798
MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
9899
MACHINE_FUNCTION_ANALYSIS("machine-branch-prob",
99100
MachineBranchProbabilityAnalysis())
@@ -132,6 +133,7 @@ MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
132133
MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
133134
MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
134135
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
136+
MACHINE_FUNCTION_PASS("print<live-intervals>", LiveIntervalsPrinterPass(dbgs()))
135137
MACHINE_FUNCTION_PASS("print<live-vars>", LiveVariablesPrinterPass(dbgs()))
136138
MACHINE_FUNCTION_PASS("print<machine-branch-prob>",
137139
MachineBranchProbabilityPrinterPass(dbgs()))
@@ -208,7 +210,6 @@ DUMMY_MACHINE_FUNCTION_PASS("irtranslator", IRTranslatorPass)
208210
DUMMY_MACHINE_FUNCTION_PASS("kcfi", MachineKCFIPass)
209211
DUMMY_MACHINE_FUNCTION_PASS("legalizer", LegalizerPass)
210212
DUMMY_MACHINE_FUNCTION_PASS("livedebugvalues", LiveDebugValuesPass)
211-
DUMMY_MACHINE_FUNCTION_PASS("liveintervals", LiveIntervalsPass)
212213
DUMMY_MACHINE_FUNCTION_PASS("lrshrink", LiveRangeShrinkPass)
213214
DUMMY_MACHINE_FUNCTION_PASS("machine-combiner", MachineCombinerPass)
214215
DUMMY_MACHINE_FUNCTION_PASS("machine-cp", MachineCopyPropagationPass)

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
6060
initializeJMCInstrumenterPass(Registry);
6161
initializeLiveDebugValuesPass(Registry);
6262
initializeLiveDebugVariablesPass(Registry);
63-
initializeLiveIntervalsPass(Registry);
63+
initializeLiveIntervalsWrapperPassPass(Registry);
6464
initializeLiveRangeShrinkPass(Registry);
6565
initializeLiveStacksPass(Registry);
6666
initializeLiveVariablesWrapperPassPass(Registry);

llvm/lib/CodeGen/InlineSpiller.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class HoistSpillHelper : private LiveRangeEdit::Delegate {
131131
public:
132132
HoistSpillHelper(MachineFunctionPass &pass, MachineFunction &mf,
133133
VirtRegMap &vrm)
134-
: MF(mf), LIS(pass.getAnalysis<LiveIntervals>()),
134+
: MF(mf), LIS(pass.getAnalysis<LiveIntervalsWrapperPass>().getLIS()),
135135
LSS(pass.getAnalysis<LiveStacks>()),
136136
MDT(pass.getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree()),
137137
VRM(vrm), MRI(mf.getRegInfo()), TII(*mf.getSubtarget().getInstrInfo()),
@@ -188,7 +188,7 @@ class InlineSpiller : public Spiller {
188188
public:
189189
InlineSpiller(MachineFunctionPass &Pass, MachineFunction &MF, VirtRegMap &VRM,
190190
VirtRegAuxInfo &VRAI)
191-
: MF(MF), LIS(Pass.getAnalysis<LiveIntervals>()),
191+
: MF(MF), LIS(Pass.getAnalysis<LiveIntervalsWrapperPass>().getLIS()),
192192
LSS(Pass.getAnalysis<LiveStacks>()),
193193
MDT(Pass.getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree()),
194194
VRM(VRM), MRI(MF.getRegInfo()), TII(*MF.getSubtarget().getInstrInfo()),

llvm/lib/CodeGen/LiveDebugVariables.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ char LiveDebugVariables::ID = 0;
7979
INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE,
8080
"Debug Variable Analysis", false, false)
8181
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
82-
INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
82+
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
8383
INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE,
8484
"Debug Variable Analysis", false, false)
8585

8686
void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
8787
AU.addRequired<MachineDominatorTreeWrapperPass>();
88-
AU.addRequiredTransitive<LiveIntervals>();
88+
AU.addRequiredTransitive<LiveIntervalsWrapperPass>();
8989
AU.setPreservesAll();
9090
MachineFunctionPass::getAnalysisUsage(AU);
9191
}
@@ -1263,7 +1263,7 @@ void LDVImpl::computeIntervals() {
12631263
bool LDVImpl::runOnMachineFunction(MachineFunction &mf, bool InstrRef) {
12641264
clear();
12651265
MF = &mf;
1266-
LIS = &pass.getAnalysis<LiveIntervals>();
1266+
LIS = &pass.getAnalysis<LiveIntervalsWrapperPass>().getLIS();
12671267
TRI = mf.getSubtarget().getRegisterInfo();
12681268
LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
12691269
<< mf.getName() << " **********\n");

llvm/lib/CodeGen/LiveIntervals.cpp

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,39 @@ using namespace llvm;
5757

5858
#define DEBUG_TYPE "regalloc"
5959

60-
char LiveIntervals::ID = 0;
61-
char &llvm::LiveIntervalsID = LiveIntervals::ID;
62-
INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals", "Live Interval Analysis",
63-
false, false)
60+
AnalysisKey LiveIntervalsAnalysis::Key;
61+
62+
LiveIntervalsAnalysis::Result
63+
LiveIntervalsAnalysis::run(MachineFunction &MF,
64+
MachineFunctionAnalysisManager &MFAM) {
65+
return Result(MF, MFAM.getResult<SlotIndexesAnalysis>(MF),
66+
MFAM.getResult<MachineDominatorTreeAnalysis>(MF));
67+
}
68+
69+
PreservedAnalyses
70+
LiveIntervalsPrinterPass::run(MachineFunction &MF,
71+
MachineFunctionAnalysisManager &MFAM) {
72+
OS << "Live intervals for machine function: " << MF.getName() << ":\n";
73+
MFAM.getResult<LiveIntervalsAnalysis>(MF).print(OS);
74+
return PreservedAnalyses::all();
75+
}
76+
77+
char LiveIntervalsWrapperPass::ID = 0;
78+
char &llvm::LiveIntervalsID = LiveIntervalsWrapperPass::ID;
79+
INITIALIZE_PASS_BEGIN(LiveIntervalsWrapperPass, "liveintervals",
80+
"Live Interval Analysis", false, false)
6481
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
6582
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
66-
INITIALIZE_PASS_END(LiveIntervals, "liveintervals",
67-
"Live Interval Analysis", false, false)
83+
INITIALIZE_PASS_END(LiveIntervalsWrapperPass, "liveintervals",
84+
"Live Interval Analysis", false, false)
85+
86+
bool LiveIntervalsWrapperPass::runOnMachineFunction(MachineFunction &MF) {
87+
LIS.Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI();
88+
LIS.DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
89+
LIS.analyze(MF);
90+
LLVM_DEBUG(dump());
91+
return false;
92+
}
6893

6994
#ifndef NDEBUG
7095
static cl::opt<bool> EnablePrecomputePhysRegs(
@@ -83,7 +108,7 @@ cl::opt<bool> UseSegmentSetForPhysRegs(
83108

84109
} // end namespace llvm
85110

86-
void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
111+
void LiveIntervalsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
87112
AU.setPreservesCFG();
88113
AU.addPreserved<LiveVariablesWrapperPass>();
89114
AU.addPreservedID(MachineLoopInfoID);
@@ -94,13 +119,13 @@ void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
94119
MachineFunctionPass::getAnalysisUsage(AU);
95120
}
96121

97-
LiveIntervals::LiveIntervals() : MachineFunctionPass(ID) {
98-
initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
122+
LiveIntervalsWrapperPass::LiveIntervalsWrapperPass() : MachineFunctionPass(ID) {
123+
initializeLiveIntervalsWrapperPassPass(*PassRegistry::getPassRegistry());
99124
}
100125

101-
LiveIntervals::~LiveIntervals() { delete LICalc; }
126+
LiveIntervals::~LiveIntervals() { clear(); }
102127

103-
void LiveIntervals::releaseMemory() {
128+
void LiveIntervals::clear() {
104129
// Free the live intervals themselves.
105130
for (unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i)
106131
delete VirtRegIntervals[Register::index2VirtReg(i)];
@@ -117,16 +142,14 @@ void LiveIntervals::releaseMemory() {
117142
VNInfoAllocator.Reset();
118143
}
119144

120-
bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
145+
void LiveIntervals::analyze(MachineFunction &fn) {
121146
MF = &fn;
122147
MRI = &MF->getRegInfo();
123148
TRI = MF->getSubtarget().getRegisterInfo();
124149
TII = MF->getSubtarget().getInstrInfo();
125-
Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI();
126-
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
127150

128151
if (!LICalc)
129-
LICalc = new LiveIntervalCalc();
152+
LICalc = std::make_unique<LiveIntervalCalc>();
130153

131154
// Allocate space for all virtual registers.
132155
VirtRegIntervals.resize(MRI->getNumVirtRegs());
@@ -141,11 +164,9 @@ bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
141164
for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
142165
getRegUnit(i);
143166
}
144-
LLVM_DEBUG(dump());
145-
return false;
146167
}
147168

148-
void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
169+
void LiveIntervals::print(raw_ostream &OS) const {
149170
OS << "********** INTERVALS **********\n";
150171

151172
// Dump the regunits.
@@ -179,6 +200,10 @@ LLVM_DUMP_METHOD void LiveIntervals::dumpInstrs() const {
179200
}
180201
#endif
181202

203+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
204+
LLVM_DUMP_METHOD void LiveIntervals::dump() const { print(dbgs()); }
205+
#endif
206+
182207
LiveInterval *LiveIntervals::createInterval(Register reg) {
183208
float Weight = reg.isPhysical() ? huge_valf : 0.0F;
184209
return new LiveInterval(reg, Weight);

llvm/lib/CodeGen/LiveRegMatrix.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ STATISTIC(NumUnassigned , "Number of registers unassigned");
3838
char LiveRegMatrix::ID = 0;
3939
INITIALIZE_PASS_BEGIN(LiveRegMatrix, "liveregmatrix",
4040
"Live Register Matrix", false, false)
41-
INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
41+
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
4242
INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
4343
INITIALIZE_PASS_END(LiveRegMatrix, "liveregmatrix",
4444
"Live Register Matrix", false, false)
@@ -47,14 +47,14 @@ LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID) {}
4747

4848
void LiveRegMatrix::getAnalysisUsage(AnalysisUsage &AU) const {
4949
AU.setPreservesAll();
50-
AU.addRequiredTransitive<LiveIntervals>();
50+
AU.addRequiredTransitive<LiveIntervalsWrapperPass>();
5151
AU.addRequiredTransitive<VirtRegMap>();
5252
MachineFunctionPass::getAnalysisUsage(AU);
5353
}
5454

5555
bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) {
5656
TRI = MF.getSubtarget().getRegisterInfo();
57-
LIS = &getAnalysis<LiveIntervals>();
57+
LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
5858
VRM = &getAnalysis<VirtRegMap>();
5959

6060
unsigned NumRegUnits = TRI->getNumRegUnits();

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,8 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(
11611161
<< " -- " << printMBBReference(*NMBB) << " -- "
11621162
<< printMBBReference(*Succ) << '\n');
11631163

1164-
LiveIntervals *LIS = P.getAnalysisIfAvailable<LiveIntervals>();
1164+
auto *LISWrapper = P.getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
1165+
LiveIntervals *LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
11651166
auto *SIWrapper = P.getAnalysisIfAvailable<SlotIndexesWrapperPass>();
11661167
SlotIndexes *Indexes = SIWrapper ? &SIWrapper->getSI() : nullptr;
11671168
if (LIS)

0 commit comments

Comments
 (0)