Skip to content

Commit 74e6f0a

Browse files
committed
[CodeGen][NewPM] Port LiveDebugVariables to NPM
1 parent 0c0d7a6 commit 74e6f0a

12 files changed

+114
-60
lines changed

llvm/include/llvm/CodeGen/LiveDebugVariables.h

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,34 @@
2121
#define LLVM_CODEGEN_LIVEDEBUGVARIABLES_H
2222

2323
#include "llvm/CodeGen/MachineFunctionPass.h"
24+
#include "llvm/IR/PassManager.h"
2425
#include "llvm/Support/Compiler.h"
26+
#include <memory>
2527

2628
namespace llvm {
2729

2830
template <typename T> class ArrayRef;
2931
class LiveIntervals;
3032
class VirtRegMap;
3133

32-
class LiveDebugVariables : public MachineFunctionPass {
33-
void *pImpl = nullptr;
34+
class LiveDebugVariables {
35+
private:
36+
void *PImpl;
3437

3538
public:
36-
static char ID; // Pass identification, replacement for typeid
39+
LiveDebugVariables() = default;
40+
~LiveDebugVariables();
41+
42+
LiveDebugVariables(LiveDebugVariables &&Other) : PImpl(Other.PImpl) {
43+
Other.PImpl = nullptr;
44+
}
45+
46+
LiveDebugVariables &operator=(LiveDebugVariables &&Other);
3747

38-
LiveDebugVariables();
39-
~LiveDebugVariables() override;
48+
LiveDebugVariables &operator=(const LiveDebugVariables &) = delete;
49+
LiveDebugVariables(const LiveDebugVariables &) = delete;
4050

51+
void analyze(MachineFunction &MF, LiveIntervals *LIS);
4152
/// splitRegister - Move any user variables in OldReg to the live ranges in
4253
/// NewRegs where they are live. Mark the values as unavailable where no new
4354
/// register is live.
@@ -52,9 +63,23 @@ class LiveDebugVariables : public MachineFunctionPass {
5263
/// dump - Print data structures to dbgs().
5364
void dump() const;
5465

55-
private:
66+
void releaseMemory();
67+
};
68+
69+
class LiveDebugVariablesWrapperPass : public MachineFunctionPass {
70+
std::unique_ptr<LiveDebugVariables> Impl;
71+
72+
public:
73+
static char ID; // Pass identification, replacement for typeid
74+
75+
LiveDebugVariablesWrapperPass();
76+
5677
bool runOnMachineFunction(MachineFunction &) override;
57-
void releaseMemory() override;
78+
79+
LiveDebugVariables &getLDV() { return *Impl; }
80+
const LiveDebugVariables &getLDV() const { return *Impl; }
81+
82+
void releaseMemory() override { Impl->releaseMemory(); }
5883
void getAnalysisUsage(AnalysisUsage &) const override;
5984

6085
MachineFunctionProperties getSetProperties() const override {
@@ -63,6 +88,16 @@ class LiveDebugVariables : public MachineFunctionPass {
6388
}
6489
};
6590

91+
class LiveDebugVariablesAnalysis
92+
: public AnalysisInfoMixin<LiveDebugVariablesAnalysis> {
93+
friend AnalysisInfoMixin<LiveDebugVariablesAnalysis>;
94+
static AnalysisKey Key;
95+
96+
public:
97+
using Result = LiveDebugVariables;
98+
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
99+
};
100+
66101
} // end namespace llvm
67102

68103
#endif // LLVM_CODEGEN_LIVEDEBUGVARIABLES_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void initializeLegalizerPass(PassRegistry &);
153153
void initializeGISelCSEAnalysisWrapperPassPass(PassRegistry &);
154154
void initializeGISelKnownBitsAnalysisPass(PassRegistry &);
155155
void initializeLiveDebugValuesPass(PassRegistry &);
156-
void initializeLiveDebugVariablesPass(PassRegistry &);
156+
void initializeLiveDebugVariablesWrapperPassPass(PassRegistry &);
157157
void initializeLiveIntervalsWrapperPassPass(PassRegistry &);
158158
void initializeLiveRangeShrinkPass(PassRegistry &);
159159
void initializeLiveRegMatrixWrapperLegacyPass(PassRegistry &);

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
5959
initializeInterleavedAccessPass(Registry);
6060
initializeJMCInstrumenterPass(Registry);
6161
initializeLiveDebugValuesPass(Registry);
62-
initializeLiveDebugVariablesPass(Registry);
62+
initializeLiveDebugVariablesWrapperPassPass(Registry);
6363
initializeLiveIntervalsWrapperPassPass(Registry);
6464
initializeLiveRangeShrinkPass(Registry);
6565
initializeLiveStacksPass(Registry);

llvm/lib/CodeGen/LiveDebugVariables.cpp

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,25 @@ EnableLDV("live-debug-variables", cl::init(true),
7474
STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
7575
STATISTIC(NumInsertedDebugLabels, "Number of DBG_LABELs inserted");
7676

77-
char LiveDebugVariables::ID = 0;
77+
char LiveDebugVariablesWrapperPass::ID = 0;
7878

79-
INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE,
80-
"Debug Variable Analysis", false, false)
79+
INITIALIZE_PASS_BEGIN(LiveDebugVariablesWrapperPass, DEBUG_TYPE,
80+
"Debug Variable Analysis", false, false)
8181
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
8282
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
83-
INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE,
84-
"Debug Variable Analysis", false, false)
83+
INITIALIZE_PASS_END(LiveDebugVariablesWrapperPass, DEBUG_TYPE,
84+
"Debug Variable Analysis", false, false)
8585

86-
void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
86+
void LiveDebugVariablesWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
8787
AU.addRequired<MachineDominatorTreeWrapperPass>();
8888
AU.addRequiredTransitive<LiveIntervalsWrapperPass>();
8989
AU.setPreservesAll();
9090
MachineFunctionPass::getAnalysisUsage(AU);
9191
}
9292

93-
LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) {
94-
initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
93+
LiveDebugVariablesWrapperPass::LiveDebugVariablesWrapperPass()
94+
: MachineFunctionPass(ID) {
95+
initializeLiveDebugVariablesWrapperPassPass(*PassRegistry::getPassRegistry());
9596
}
9697

9798
enum : unsigned { UndefLocNo = ~0U };
@@ -530,7 +531,6 @@ class UserLabel {
530531

531532
/// Implementation of the LiveDebugVariables pass.
532533
class LDVImpl {
533-
LiveDebugVariables &pass;
534534
LocMap::Allocator allocator;
535535
MachineFunction *MF = nullptr;
536536
LiveIntervals *LIS;
@@ -634,7 +634,7 @@ class LDVImpl {
634634
void computeIntervals();
635635

636636
public:
637-
LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
637+
LDVImpl(LiveIntervals *LIS) : LIS(LIS) {}
638638

639639
bool runOnMachineFunction(MachineFunction &mf, bool InstrRef);
640640

@@ -1263,7 +1263,6 @@ void LDVImpl::computeIntervals() {
12631263
bool LDVImpl::runOnMachineFunction(MachineFunction &mf, bool InstrRef) {
12641264
clear();
12651265
MF = &mf;
1266-
LIS = &pass.getAnalysis<LiveIntervalsWrapperPass>().getLIS();
12671266
TRI = mf.getSubtarget().getRegisterInfo();
12681267
LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
12691268
<< mf.getName() << " **********\n");
@@ -1298,31 +1297,50 @@ static void removeDebugInstrs(MachineFunction &mf) {
12981297
}
12991298
}
13001299

1301-
bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
1302-
if (!EnableLDV)
1303-
return false;
1304-
if (!mf.getFunction().getSubprogram()) {
1305-
removeDebugInstrs(mf);
1306-
return false;
1307-
}
1300+
bool LiveDebugVariablesWrapperPass::runOnMachineFunction(MachineFunction &mf) {
1301+
auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
13081302

1309-
// Have we been asked to track variable locations using instruction
1310-
// referencing?
1311-
bool InstrRef = mf.useDebugInstrRef();
1303+
Impl = std::make_unique<LiveDebugVariables>();
1304+
Impl->analyze(mf, LIS);
1305+
return false;
1306+
}
1307+
1308+
AnalysisKey LiveDebugVariablesAnalysis::Key;
1309+
1310+
LiveDebugVariables
1311+
LiveDebugVariablesAnalysis::run(MachineFunction &MF,
1312+
MachineFunctionAnalysisManager &MFAM) {
1313+
auto *LIS = &MFAM.getResult<LiveIntervalsAnalysis>(MF);
1314+
LiveDebugVariables LDV;
1315+
LDV.analyze(MF, LIS);
1316+
return LDV;
1317+
}
13121318

1313-
if (!pImpl)
1314-
pImpl = new LDVImpl(this);
1315-
return static_cast<LDVImpl *>(pImpl)->runOnMachineFunction(mf, InstrRef);
1319+
LiveDebugVariables::~LiveDebugVariables() {
1320+
if (PImpl)
1321+
delete static_cast<LDVImpl *>(PImpl);
13161322
}
13171323

13181324
void LiveDebugVariables::releaseMemory() {
1319-
if (pImpl)
1320-
static_cast<LDVImpl*>(pImpl)->clear();
1325+
if (PImpl)
1326+
static_cast<LDVImpl *>(PImpl)->clear();
13211327
}
13221328

1323-
LiveDebugVariables::~LiveDebugVariables() {
1324-
if (pImpl)
1325-
delete static_cast<LDVImpl*>(pImpl);
1329+
void LiveDebugVariables::analyze(MachineFunction &MF, LiveIntervals *LIS) {
1330+
if (!EnableLDV)
1331+
return;
1332+
if (!MF.getFunction().getSubprogram()) {
1333+
removeDebugInstrs(MF);
1334+
return;
1335+
}
1336+
1337+
if (!PImpl)
1338+
PImpl = new LDVImpl(LIS); // reuse same object across analysis runs
1339+
1340+
// Have we been asked to track variable locations using instruction
1341+
// referencing?
1342+
bool InstrRef = MF.useDebugInstrRef();
1343+
static_cast<LDVImpl *>(PImpl)->runOnMachineFunction(MF, InstrRef);
13261344
}
13271345

13281346
//===----------------------------------------------------------------------===//
@@ -1504,8 +1522,8 @@ void LDVImpl::splitRegister(Register OldReg, ArrayRef<Register> NewRegs) {
15041522

15051523
void LiveDebugVariables::
15061524
splitRegister(Register OldReg, ArrayRef<Register> NewRegs, LiveIntervals &LIS) {
1507-
if (pImpl)
1508-
static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
1525+
if (PImpl)
1526+
static_cast<LDVImpl *>(PImpl)->splitRegister(OldReg, NewRegs);
15091527
}
15101528

15111529
void UserValue::rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
@@ -1956,13 +1974,13 @@ void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
19561974
}
19571975

19581976
void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
1959-
if (pImpl)
1960-
static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1977+
if (PImpl)
1978+
static_cast<LDVImpl *>(PImpl)->emitDebugValues(VRM);
19611979
}
19621980

19631981
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
19641982
LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
1965-
if (pImpl)
1966-
static_cast<LDVImpl*>(pImpl)->print(dbgs());
1983+
if (PImpl)
1984+
static_cast<LDVImpl *>(PImpl)->print(dbgs());
19671985
}
19681986
#endif

llvm/lib/CodeGen/RegAllocBasic.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ char &llvm::RABasicID = RABasic::ID;
130130

131131
INITIALIZE_PASS_BEGIN(RABasic, "regallocbasic", "Basic Register Allocator",
132132
false, false)
133-
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
133+
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariablesWrapperPass)
134134
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
135135
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
136136
INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
@@ -180,8 +180,8 @@ void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
180180
AU.addRequired<LiveIntervalsWrapperPass>();
181181
AU.addPreserved<LiveIntervalsWrapperPass>();
182182
AU.addPreserved<SlotIndexesWrapperPass>();
183-
AU.addRequired<LiveDebugVariables>();
184-
AU.addPreserved<LiveDebugVariables>();
183+
AU.addRequired<LiveDebugVariablesWrapperPass>();
184+
AU.addPreserved<LiveDebugVariablesWrapperPass>();
185185
AU.addRequired<LiveStacks>();
186186
AU.addPreserved<LiveStacks>();
187187
AU.addRequired<ProfileSummaryInfoWrapperPass>();

llvm/lib/CodeGen/RegAllocGreedy.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ char &llvm::RAGreedyID = RAGreedy::ID;
154154

155155
INITIALIZE_PASS_BEGIN(RAGreedy, "greedy",
156156
"Greedy Register Allocator", false, false)
157-
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
157+
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariablesWrapperPass)
158158
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
159159
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
160160
INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
@@ -207,8 +207,8 @@ void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
207207
AU.addPreserved<LiveIntervalsWrapperPass>();
208208
AU.addRequired<SlotIndexesWrapperPass>();
209209
AU.addPreserved<SlotIndexesWrapperPass>();
210-
AU.addRequired<LiveDebugVariables>();
211-
AU.addPreserved<LiveDebugVariables>();
210+
AU.addRequired<LiveDebugVariablesWrapperPass>();
211+
AU.addPreserved<LiveDebugVariablesWrapperPass>();
212212
AU.addRequired<LiveStacks>();
213213
AU.addPreserved<LiveStacks>();
214214
AU.addRequired<MachineDominatorTreeWrapperPass>();
@@ -2735,7 +2735,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
27352735
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
27362736
Bundles = &getAnalysis<EdgeBundles>();
27372737
SpillPlacer = &getAnalysis<SpillPlacement>();
2738-
DebugVars = &getAnalysis<LiveDebugVariables>();
2738+
DebugVars = &getAnalysis<LiveDebugVariablesWrapperPass>().getLDV();
27392739

27402740
initializeCSRCost();
27412741

llvm/lib/CodeGen/RegAllocGreedy.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/ADT/SmallVector.h"
2626
#include "llvm/ADT/StringRef.h"
2727
#include "llvm/CodeGen/CalcSpillWeights.h"
28+
#include "llvm/CodeGen/LiveDebugVariables.h"
2829
#include "llvm/CodeGen/LiveInterval.h"
2930
#include "llvm/CodeGen/LiveRangeEdit.h"
3031
#include "llvm/CodeGen/MachineFunction.h"
@@ -42,7 +43,7 @@ namespace llvm {
4243
class AllocationOrder;
4344
class AnalysisUsage;
4445
class EdgeBundles;
45-
class LiveDebugVariables;
46+
class LiveDebugVariablesWrapperPass;
4647
class LiveIntervals;
4748
class LiveRegMatrix;
4849
class MachineBasicBlock;

llvm/lib/CodeGen/StackSlotColoring.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ namespace {
160160
// may be invoked multiple times requiring it to save these analyses to be
161161
// used by RA later.
162162
AU.addPreserved<LiveIntervalsWrapperPass>();
163-
AU.addPreserved<LiveDebugVariables>();
163+
AU.addPreserved<LiveDebugVariablesWrapperPass>();
164164

165165
MachineFunctionPass::getAnalysisUsage(AU);
166166
}

llvm/lib/CodeGen/VirtRegMap.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter",
251251
"Virtual Register Rewriter", false, false)
252252
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
253253
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
254-
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
254+
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariablesWrapperPass)
255255
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy)
256256
INITIALIZE_PASS_DEPENDENCY(LiveStacks)
257257
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
@@ -264,14 +264,14 @@ void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const {
264264
AU.addPreserved<LiveIntervalsWrapperPass>();
265265
AU.addRequired<SlotIndexesWrapperPass>();
266266
AU.addPreserved<SlotIndexesWrapperPass>();
267-
AU.addRequired<LiveDebugVariables>();
267+
AU.addRequired<LiveDebugVariablesWrapperPass>();
268268
AU.addRequired<LiveStacks>();
269269
AU.addPreserved<LiveStacks>();
270270
AU.addRequired<VirtRegMapWrapperLegacy>();
271271
AU.addRequired<LiveRegMatrixWrapperLegacy>();
272272

273273
if (!ClearVirtRegs)
274-
AU.addPreserved<LiveDebugVariables>();
274+
AU.addPreserved<LiveDebugVariablesWrapperPass>();
275275

276276
MachineFunctionPass::getAnalysisUsage(AU);
277277
}
@@ -285,7 +285,7 @@ bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) {
285285
LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
286286
LRM = &getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM();
287287
VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM();
288-
DebugVars = &getAnalysis<LiveDebugVariables>();
288+
DebugVars = &getAnalysis<LiveDebugVariablesWrapperPass>().getLDV();
289289
LLVM_DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n"
290290
<< "********** Function: " << MF->getName() << '\n');
291291
LLVM_DEBUG(VRM->dump());

llvm/lib/Target/LoongArch/LoongArchDeadRegisterDefinitions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class LoongArchDeadRegisterDefinitions : public MachineFunctionPass {
3939
AU.addPreserved<LiveIntervalsWrapperPass>();
4040
AU.addRequired<LiveIntervalsWrapperPass>();
4141
AU.addPreserved<SlotIndexesWrapperPass>();
42-
AU.addPreserved<LiveDebugVariables>();
42+
AU.addPreserved<LiveDebugVariablesWrapperPass>();
4343
AU.addPreserved<LiveStacks>();
4444
MachineFunctionPass::getAnalysisUsage(AU);
4545
}

llvm/lib/Target/RISCV/RISCVDeadRegisterDefinitions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class RISCVDeadRegisterDefinitions : public MachineFunctionPass {
3939
AU.addPreserved<LiveIntervalsWrapperPass>();
4040
AU.addRequired<LiveIntervalsWrapperPass>();
4141
AU.addPreserved<SlotIndexesWrapperPass>();
42-
AU.addPreserved<LiveDebugVariables>();
42+
AU.addPreserved<LiveDebugVariablesWrapperPass>();
4343
AU.addPreserved<LiveStacks>();
4444
MachineFunctionPass::getAnalysisUsage(AU);
4545
}

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
889889
AU.addUsedIfAvailable<LiveIntervalsWrapperPass>();
890890
AU.addPreserved<LiveIntervalsWrapperPass>();
891891
AU.addPreserved<SlotIndexesWrapperPass>();
892-
AU.addPreserved<LiveDebugVariables>();
892+
AU.addPreserved<LiveDebugVariablesWrapperPass>();
893893
AU.addPreserved<LiveStacks>();
894894

895895
MachineFunctionPass::getAnalysisUsage(AU);

0 commit comments

Comments
 (0)