Skip to content

Commit 3cf8e93

Browse files
committed
[NewPM][CodeGen] Port LiveRegMatrix to NPM
1 parent 15692bd commit 3cf8e93

File tree

9 files changed

+88
-35
lines changed

9 files changed

+88
-35
lines changed

llvm/include/llvm/CodeGen/LiveRegMatrix.h

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class MachineFunction;
3737
class TargetRegisterInfo;
3838
class VirtRegMap;
3939

40-
class LiveRegMatrix : public MachineFunctionPass {
40+
class LiveRegMatrix {
41+
friend class LiveRegMatrixWrapperPass;
42+
friend class LiveRegMatrixAnalysis;
4143
const TargetRegisterInfo *TRI = nullptr;
4244
LiveIntervals *LIS = nullptr;
4345
VirtRegMap *VRM = nullptr;
@@ -57,15 +59,21 @@ class LiveRegMatrix : public MachineFunctionPass {
5759
unsigned RegMaskVirtReg = 0;
5860
BitVector RegMaskUsable;
5961

60-
// MachineFunctionPass boilerplate.
61-
void getAnalysisUsage(AnalysisUsage &) const override;
62-
bool runOnMachineFunction(MachineFunction &) override;
63-
void releaseMemory() override;
62+
LiveRegMatrix() = default;
63+
void releaseMemory();
6464

6565
public:
66-
static char ID;
67-
68-
LiveRegMatrix();
66+
LiveRegMatrix(LiveRegMatrix &&Other)
67+
: TRI(Other.TRI), LIS(Other.LIS), VRM(Other.VRM), UserTag(Other.UserTag),
68+
Matrix(std::move(Other.Matrix)), Queries(std::move(Other.Queries)),
69+
RegMaskTag(Other.RegMaskTag), RegMaskVirtReg(Other.RegMaskVirtReg),
70+
RegMaskUsable(std::move(Other.RegMaskUsable)) {
71+
Other.TRI = nullptr;
72+
Other.LIS = nullptr;
73+
Other.VRM = nullptr;
74+
}
75+
76+
void init(MachineFunction &MF, LiveIntervals *LIS, VirtRegMap *VRM);
6977

7078
//===--------------------------------------------------------------------===//
7179
// High-level interface.
@@ -159,6 +167,32 @@ class LiveRegMatrix : public MachineFunctionPass {
159167
Register getOneVReg(unsigned PhysReg) const;
160168
};
161169

170+
class LiveRegMatrixWrapperPass : public MachineFunctionPass {
171+
LiveRegMatrix LRM;
172+
173+
public:
174+
static char ID;
175+
176+
LiveRegMatrixWrapperPass() : MachineFunctionPass(ID) {}
177+
178+
LiveRegMatrix &getLRM() { return LRM; }
179+
const LiveRegMatrix &getLRM() const { return LRM; }
180+
181+
void getAnalysisUsage(AnalysisUsage &AU) const override;
182+
bool runOnMachineFunction(MachineFunction &MF) override;
183+
void releaseMemory() override;
184+
};
185+
186+
class LiveRegMatrixAnalysis : public AnalysisInfoMixin<LiveRegMatrixAnalysis> {
187+
friend AnalysisInfoMixin<LiveRegMatrixAnalysis>;
188+
static AnalysisKey Key;
189+
190+
public:
191+
using Result = LiveRegMatrix;
192+
193+
LiveRegMatrix run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
194+
};
195+
162196
} // end namespace llvm
163197

164198
#endif // LLVM_CODEGEN_LIVEREGMATRIX_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void initializeLiveDebugValuesPass(PassRegistry &);
156156
void initializeLiveDebugVariablesPass(PassRegistry &);
157157
void initializeLiveIntervalsWrapperPassPass(PassRegistry &);
158158
void initializeLiveRangeShrinkPass(PassRegistry &);
159-
void initializeLiveRegMatrixPass(PassRegistry &);
159+
void initializeLiveRegMatrixWrapperPassPass(PassRegistry &);
160160
void initializeLiveStacksPass(PassRegistry &);
161161
void initializeLiveVariablesWrapperPassPass(PassRegistry &);
162162
void initializeLoadStoreOptPass(PassRegistry &);

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ LOOP_PASS("loop-term-fold", LoopTermFoldPass())
9797
// preferably fix the scavenger to not depend on them).
9898
MACHINE_FUNCTION_ANALYSIS("live-intervals", LiveIntervalsAnalysis())
9999
MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
100+
MACHINE_FUNCTION_ANALYSIS("live-reg-matrix", LiveRegMatrixAnalysis())
100101
MACHINE_FUNCTION_ANALYSIS("machine-block-freq", MachineBlockFrequencyAnalysis())
101102
MACHINE_FUNCTION_ANALYSIS("machine-branch-prob",
102103
MachineBranchProbabilityAnalysis())
@@ -122,8 +123,7 @@ MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis())
122123
// MachineRegionInfoPassAnalysis())
123124
// MACHINE_FUNCTION_ANALYSIS("machine-trace-metrics",
124125
// MachineTraceMetricsAnalysis()) MACHINE_FUNCTION_ANALYSIS("reaching-def",
125-
// ReachingDefAnalysisAnalysis()) MACHINE_FUNCTION_ANALYSIS("live-reg-matrix",
126-
// LiveRegMatrixAnalysis()) MACHINE_FUNCTION_ANALYSIS("gc-analysis",
126+
// ReachingDefAnalysisAnalysis()) MACHINE_FUNCTION_ANALYSIS("gc-analysis",
127127
// GCMachineCodeAnalysisPass())
128128
#undef MACHINE_FUNCTION_ANALYSIS
129129

llvm/lib/CodeGen/LiveRegMatrix.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,33 @@ using namespace llvm;
3535
STATISTIC(NumAssigned , "Number of registers assigned");
3636
STATISTIC(NumUnassigned , "Number of registers unassigned");
3737

38-
char LiveRegMatrix::ID = 0;
39-
INITIALIZE_PASS_BEGIN(LiveRegMatrix, "liveregmatrix",
38+
char LiveRegMatrixWrapperPass::ID = 0;
39+
INITIALIZE_PASS_BEGIN(LiveRegMatrixWrapperPass, "liveregmatrix",
4040
"Live Register Matrix", false, false)
4141
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
4242
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
43-
INITIALIZE_PASS_END(LiveRegMatrix, "liveregmatrix",
43+
INITIALIZE_PASS_END(LiveRegMatrixWrapperPass, "liveregmatrix",
4444
"Live Register Matrix", false, false)
4545

46-
LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID) {}
47-
48-
void LiveRegMatrix::getAnalysisUsage(AnalysisUsage &AU) const {
46+
void LiveRegMatrixWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
4947
AU.setPreservesAll();
5048
AU.addRequiredTransitive<LiveIntervalsWrapperPass>();
5149
AU.addRequiredTransitive<VirtRegMapWrapperLegacy>();
5250
MachineFunctionPass::getAnalysisUsage(AU);
5351
}
5452

55-
bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) {
53+
bool LiveRegMatrixWrapperPass::runOnMachineFunction(MachineFunction &MF) {
54+
auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
55+
auto *VRM = &getAnalysis<VirtRegMapWrapperPass>().getVRM();
56+
LRM.init(MF, LIS, VRM);
57+
return false;
58+
}
59+
60+
void LiveRegMatrix::init(MachineFunction &MF, LiveIntervals *pLIS,
61+
VirtRegMap *pVRM) {
5662
TRI = MF.getSubtarget().getRegisterInfo();
57-
LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
58-
VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM();
63+
LIS = pLIS;
64+
VRM = pVRM;
5965

6066
unsigned NumRegUnits = TRI->getNumRegUnits();
6167
if (NumRegUnits != Matrix.size())
@@ -64,9 +70,10 @@ bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) {
6470

6571
// Make sure no stale queries get reused.
6672
invalidateVirtRegs();
67-
return false;
6873
}
6974

75+
void LiveRegMatrixWrapperPass::releaseMemory() { LRM.releaseMemory(); }
76+
7077
void LiveRegMatrix::releaseMemory() {
7178
for (unsigned i = 0, e = Matrix.size(); i != e; ++i) {
7279
Matrix[i].clear();
@@ -246,3 +253,14 @@ Register LiveRegMatrix::getOneVReg(unsigned PhysReg) const {
246253

247254
return MCRegister::NoRegister;
248255
}
256+
257+
AnalysisKey LiveRegMatrixAnalysis::Key;
258+
259+
LiveRegMatrix LiveRegMatrixAnalysis::run(MachineFunction &MF,
260+
MachineFunctionAnalysisManager &MFAM) {
261+
auto &LIS = MFAM.getResult<LiveIntervalsAnalysis>(MF);
262+
auto &VRM = MFAM.getResult<VirtRegMapAnalysis>(MF);
263+
LiveRegMatrix LRM;
264+
LRM.init(MF, &LIS, &VRM);
265+
return LRM;
266+
}

llvm/lib/CodeGen/RegAllocBasic.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
139139
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
140140
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
141141
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
142-
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
142+
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperPass)
143143
INITIALIZE_PASS_END(RABasic, "regallocbasic", "Basic Register Allocator", false,
144144
false)
145145

@@ -190,8 +190,8 @@ void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
190190
AU.addPreserved<MachineLoopInfoWrapperPass>();
191191
AU.addRequired<VirtRegMapWrapperLegacy>();
192192
AU.addPreserved<VirtRegMapWrapperLegacy>();
193-
AU.addRequired<LiveRegMatrix>();
194-
AU.addPreserved<LiveRegMatrix>();
193+
AU.addRequired<LiveRegMatrixWrapperPass>();
194+
AU.addPreserved<LiveRegMatrixWrapperPass>();
195195
MachineFunctionPass::getAnalysisUsage(AU);
196196
}
197197

@@ -309,7 +309,7 @@ bool RABasic::runOnMachineFunction(MachineFunction &mf) {
309309
MF = &mf;
310310
RegAllocBase::init(getAnalysis<VirtRegMapWrapperLegacy>().getVRM(),
311311
getAnalysis<LiveIntervalsWrapperPass>().getLIS(),
312-
getAnalysis<LiveRegMatrix>());
312+
getAnalysis<LiveRegMatrixWrapperPass>().getLRM());
313313
VirtRegAuxInfo VRAI(
314314
*MF, *LIS, *VRM, getAnalysis<MachineLoopInfoWrapperPass>().getLI(),
315315
getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI());

llvm/lib/CodeGen/RegAllocGreedy.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ INITIALIZE_PASS_DEPENDENCY(LiveStacks)
163163
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
164164
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
165165
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
166-
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
166+
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperPass)
167167
INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
168168
INITIALIZE_PASS_DEPENDENCY(SpillPlacement)
169169
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
@@ -217,8 +217,8 @@ void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
217217
AU.addPreserved<MachineLoopInfoWrapperPass>();
218218
AU.addRequired<VirtRegMapWrapperLegacy>();
219219
AU.addPreserved<VirtRegMapWrapperLegacy>();
220-
AU.addRequired<LiveRegMatrix>();
221-
AU.addPreserved<LiveRegMatrix>();
220+
AU.addRequired<LiveRegMatrixWrapperPass>();
221+
AU.addPreserved<LiveRegMatrixWrapperPass>();
222222
AU.addRequired<EdgeBundles>();
223223
AU.addRequired<SpillPlacement>();
224224
AU.addRequired<MachineOptimizationRemarkEmitterPass>();
@@ -2718,7 +2718,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
27182718

27192719
RegAllocBase::init(getAnalysis<VirtRegMapWrapperLegacy>().getVRM(),
27202720
getAnalysis<LiveIntervalsWrapperPass>().getLIS(),
2721-
getAnalysis<LiveRegMatrix>());
2721+
getAnalysis<LiveRegMatrixWrapperPass>().getLRM());
27222722

27232723
// Early return if there is no virtual register to be allocated to a
27242724
// physical register.

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
#include "llvm/CodeGen/InterleavedLoadCombine.h"
9595
#include "llvm/CodeGen/JMCInstrumenter.h"
9696
#include "llvm/CodeGen/LiveIntervals.h"
97+
#include "llvm/CodeGen/LiveRegMatrix.h"
9798
#include "llvm/CodeGen/LiveVariables.h"
9899
#include "llvm/CodeGen/LocalStackSlotAllocation.h"
99100
#include "llvm/CodeGen/LowerEmuTLS.h"

llvm/lib/Target/AMDGPU/GCNNSAReassign.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class GCNNSAReassign : public MachineFunctionPass {
5050
void getAnalysisUsage(AnalysisUsage &AU) const override {
5151
AU.addRequired<LiveIntervalsWrapperPass>();
5252
AU.addRequired<VirtRegMapWrapperLegacy>();
53-
AU.addRequired<LiveRegMatrix>();
53+
AU.addRequired<LiveRegMatrixWrapperPass>();
5454
AU.setPreservesAll();
5555
MachineFunctionPass::getAnalysisUsage(AU);
5656
}
@@ -96,7 +96,7 @@ INITIALIZE_PASS_BEGIN(GCNNSAReassign, DEBUG_TYPE, "GCN NSA Reassign",
9696
false, false)
9797
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
9898
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
99-
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
99+
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperPass)
100100
INITIALIZE_PASS_END(GCNNSAReassign, DEBUG_TYPE, "GCN NSA Reassign",
101101
false, false)
102102

@@ -243,7 +243,7 @@ bool GCNNSAReassign::runOnMachineFunction(MachineFunction &MF) {
243243
MRI = &MF.getRegInfo();
244244
TRI = ST->getRegisterInfo();
245245
VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM();
246-
LRM = &getAnalysis<LiveRegMatrix>();
246+
LRM = &getAnalysis<LiveRegMatrixWrapperPass>().getLRM();
247247
LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
248248

249249
const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();

llvm/lib/Target/AMDGPU/SIPreAllocateWWMRegs.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class SIPreAllocateWWMRegs : public MachineFunctionPass {
6161
void getAnalysisUsage(AnalysisUsage &AU) const override {
6262
AU.addRequired<LiveIntervalsWrapperPass>();
6363
AU.addRequired<VirtRegMapWrapperLegacy>();
64-
AU.addRequired<LiveRegMatrix>();
64+
AU.addRequired<LiveRegMatrixWrapperPass>();
6565
AU.setPreservesAll();
6666
MachineFunctionPass::getAnalysisUsage(AU);
6767
}
@@ -77,7 +77,7 @@ INITIALIZE_PASS_BEGIN(SIPreAllocateWWMRegs, DEBUG_TYPE,
7777
"SI Pre-allocate WWM Registers", false, false)
7878
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
7979
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
80-
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
80+
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperPass)
8181
INITIALIZE_PASS_END(SIPreAllocateWWMRegs, DEBUG_TYPE,
8282
"SI Pre-allocate WWM Registers", false, false)
8383

@@ -194,7 +194,7 @@ bool SIPreAllocateWWMRegs::runOnMachineFunction(MachineFunction &MF) {
194194
MRI = &MF.getRegInfo();
195195

196196
LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
197-
Matrix = &getAnalysis<LiveRegMatrix>();
197+
Matrix = &getAnalysis<LiveRegMatrixWrapperPass>().getLRM();
198198
VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM();
199199

200200
RegClassInfo.runOnMachineFunction(MF);

0 commit comments

Comments
 (0)