Skip to content

Commit fd81620

Browse files
committed
[CodeGen][NewPM] Port LiveVariables to new pass manager
- Port `LiveVariables` to new pass manager - Convert to `LiveVariablesWrapperPass` in legacy pass manager
1 parent 167c860 commit fd81620

16 files changed

+202
-92
lines changed

llvm/include/llvm/CodeGen/LiveVariables.h

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "llvm/ADT/SparseBitVector.h"
3636
#include "llvm/CodeGen/MachineFunctionPass.h"
3737
#include "llvm/CodeGen/MachineInstr.h"
38+
#include "llvm/CodeGen/MachinePassManager.h"
3839
#include "llvm/CodeGen/TargetRegisterInfo.h"
3940
#include "llvm/InitializePasses.h"
4041
#include "llvm/PassRegistry.h"
@@ -44,13 +45,10 @@ namespace llvm {
4445
class MachineBasicBlock;
4546
class MachineRegisterInfo;
4647

47-
class LiveVariables : public MachineFunctionPass {
48-
public:
49-
static char ID; // Pass identification, replacement for typeid
50-
LiveVariables() : MachineFunctionPass(ID) {
51-
initializeLiveVariablesPass(*PassRegistry::getPassRegistry());
52-
}
48+
class LiveVariables {
49+
friend class LiveVariablesWrapperPass;
5350

51+
public:
5452
/// VarInfo - This represents the regions where a virtual register is live in
5553
/// the program. We represent this with three different pieces of
5654
/// information: the set of blocks in which the instruction is live
@@ -109,6 +107,8 @@ class LiveVariables : public MachineFunctionPass {
109107
bool isLiveIn(const MachineBasicBlock &MBB, Register Reg,
110108
MachineRegisterInfo &MRI);
111109

110+
void print(raw_ostream &OS) const;
111+
112112
void dump() const;
113113
};
114114

@@ -141,6 +141,11 @@ class LiveVariables : public MachineFunctionPass {
141141
// current basic block.
142142
DenseMap<MachineInstr*, unsigned> DistanceMap;
143143

144+
// For legacy pass.
145+
LiveVariables() = default;
146+
147+
void analyze(MachineFunction &MF);
148+
144149
/// HandlePhysRegKill - Add kills of Reg and its sub-registers to the
145150
/// uses. Pay special attention to the sub-register uses which may come below
146151
/// the last use of the whole register.
@@ -174,9 +179,11 @@ class LiveVariables : public MachineFunctionPass {
174179
unsigned NumRegs);
175180

176181
void runOnBlock(MachineBasicBlock *MBB, unsigned NumRegs);
182+
177183
public:
184+
LiveVariables(MachineFunction &MF);
178185

179-
bool runOnMachineFunction(MachineFunction &MF) override;
186+
void print(raw_ostream &OS) const;
180187

181188
//===--------------------------------------------------------------------===//
182189
// API to update live variable information
@@ -258,12 +265,6 @@ class LiveVariables : public MachineFunctionPass {
258265
return true;
259266
}
260267

261-
void getAnalysisUsage(AnalysisUsage &AU) const override;
262-
263-
void releaseMemory() override {
264-
VirtRegInfo.clear();
265-
}
266-
267268
/// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
268269
/// register.
269270
VarInfo &getVarInfo(Register Reg);
@@ -300,6 +301,48 @@ class LiveVariables : public MachineFunctionPass {
300301
std::vector<SparseBitVector<>> &LiveInSets);
301302
};
302303

304+
class LiveVariablesAnalysis : public AnalysisInfoMixin<LiveVariablesAnalysis> {
305+
friend AnalysisInfoMixin<LiveVariablesAnalysis>;
306+
static AnalysisKey Key;
307+
308+
public:
309+
using Result = LiveVariables;
310+
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &);
311+
};
312+
313+
class LiveVariablesPrinterPass
314+
: public PassInfoMixin<LiveVariablesPrinterPass> {
315+
raw_ostream &OS;
316+
317+
public:
318+
explicit LiveVariablesPrinterPass(raw_ostream &OS) : OS(OS) {}
319+
PreservedAnalyses run(MachineFunction &MF,
320+
MachineFunctionAnalysisManager &MFAM);
321+
static bool isRequired() { return true; }
322+
};
323+
324+
class LiveVariablesWrapperPass : public MachineFunctionPass {
325+
LiveVariables LV;
326+
327+
public:
328+
static char ID; // Pass identification, replacement for typeid
329+
330+
LiveVariablesWrapperPass() : MachineFunctionPass(ID) {
331+
initializeLiveVariablesWrapperPassPass(*PassRegistry::getPassRegistry());
332+
}
333+
334+
bool runOnMachineFunction(MachineFunction &MF) override {
335+
LV.analyze(MF);
336+
return false;
337+
}
338+
339+
void getAnalysisUsage(AnalysisUsage &AU) const override;
340+
341+
void releaseMemory() override { LV.VirtRegInfo.clear(); }
342+
343+
LiveVariables &getLV() { return LV; }
344+
};
345+
303346
} // End llvm namespace
304347

305348
#endif

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void initializeLiveIntervalsPass(PassRegistry&);
155155
void initializeLiveRangeShrinkPass(PassRegistry&);
156156
void initializeLiveRegMatrixPass(PassRegistry&);
157157
void initializeLiveStacksPass(PassRegistry&);
158-
void initializeLiveVariablesPass(PassRegistry &);
158+
void initializeLiveVariablesWrapperPassPass(PassRegistry &);
159159
void initializeLoadStoreOptPass(PassRegistry &);
160160
void initializeLoadStoreVectorizerLegacyPassPass(PassRegistry&);
161161
void initializeLocalStackSlotPassPass(PassRegistry&);

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,18 @@ LOOP_PASS("loop-reduce", LoopStrengthReducePass())
8989
#ifndef MACHINE_FUNCTION_ANALYSIS
9090
#define MACHINE_FUNCTION_ANALYSIS(NAME, CREATE_PASS)
9191
#endif
92+
// LiveVariables currently requires pure SSA form.
93+
// FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
94+
// LiveVariables can be removed completely, and LiveIntervals can be directly
95+
// computed. (We still either need to regenerate kill flags after regalloc, or
96+
// preferably fix the scavenger to not depend on them).
97+
MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
9298
MACHINE_FUNCTION_ANALYSIS("machine-branch-prob",
9399
MachineBranchProbabilityAnalysis())
94100
MACHINE_FUNCTION_ANALYSIS("machine-dom-tree", MachineDominatorTreeAnalysis())
95101
MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree",
96102
MachinePostDominatorTreeAnalysis())
97103
MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
98-
// LiveVariables currently requires pure SSA form.
99-
// FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
100-
// LiveVariables can be removed completely, and LiveIntervals can be directly
101-
// computed. (We still either need to regenerate kill flags after regalloc, or
102-
// preferably fix the scavenger to not depend on them).
103-
// MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
104-
105104
// MACHINE_FUNCTION_ANALYSIS("live-stacks", LiveStacksPass())
106105
// MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
107106
// MACHINE_FUNCTION_ANALYSIS("edge-bundles", EdgeBundlesAnalysis())
@@ -132,6 +131,7 @@ MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
132131
MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
133132
MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
134133
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
134+
MACHINE_FUNCTION_PASS("print<live-vars>", LiveVariablesPrinterPass(dbgs()))
135135
MACHINE_FUNCTION_PASS("print<machine-branch-prob>",
136136
MachineBranchProbabilityPrinterPass(dbgs()))
137137
MACHINE_FUNCTION_PASS("print<machine-dom-tree>",

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
6363
initializeLiveIntervalsPass(Registry);
6464
initializeLiveRangeShrinkPass(Registry);
6565
initializeLiveStacksPass(Registry);
66-
initializeLiveVariablesPass(Registry);
66+
initializeLiveVariablesWrapperPassPass(Registry);
6767
initializeLocalStackSlotPassPass(Registry);
6868
initializeLowerGlobalDtorsLegacyPassPass(Registry);
6969
initializeLowerIntrinsicsPass(Registry);

llvm/lib/CodeGen/LiveIntervals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ cl::opt<bool> UseSegmentSetForPhysRegs(
8585

8686
void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
8787
AU.setPreservesCFG();
88-
AU.addPreserved<LiveVariables>();
88+
AU.addPreserved<LiveVariablesWrapperPass>();
8989
AU.addPreservedID(MachineLoopInfoID);
9090
AU.addRequiredTransitiveID(MachineDominatorsID);
9191
AU.addPreservedID(MachineDominatorsID);

llvm/lib/CodeGen/LiveVariables.cpp

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,49 @@
4141
#include <algorithm>
4242
using namespace llvm;
4343

44-
char LiveVariables::ID = 0;
45-
char &llvm::LiveVariablesID = LiveVariables::ID;
46-
INITIALIZE_PASS_BEGIN(LiveVariables, "livevars",
47-
"Live Variable Analysis", false, false)
48-
INITIALIZE_PASS_DEPENDENCY(UnreachableMachineBlockElim)
49-
INITIALIZE_PASS_END(LiveVariables, "livevars",
50-
"Live Variable Analysis", false, false)
44+
AnalysisKey LiveVariablesAnalysis::Key;
45+
46+
LiveVariablesAnalysis::Result
47+
LiveVariablesAnalysis::run(MachineFunction &MF,
48+
MachineFunctionAnalysisManager &) {
49+
return Result(MF);
50+
}
51+
52+
PreservedAnalyses
53+
LiveVariablesPrinterPass::run(MachineFunction &MF,
54+
MachineFunctionAnalysisManager &MFAM) {
55+
OS << "Live variables in machine function: " << MF.getName() << '\n';
56+
MFAM.getResult<LiveVariablesAnalysis>(MF).print(OS);
57+
return PreservedAnalyses::all();
58+
}
5159

60+
char LiveVariablesWrapperPass::ID = 0;
61+
char &llvm::LiveVariablesID = LiveVariablesWrapperPass::ID;
62+
INITIALIZE_PASS_BEGIN(LiveVariablesWrapperPass, "livevars",
63+
"Live Variable Analysis", false, false)
64+
INITIALIZE_PASS_DEPENDENCY(UnreachableMachineBlockElim)
65+
INITIALIZE_PASS_END(LiveVariablesWrapperPass, "livevars",
66+
"Live Variable Analysis", false, false)
5267

53-
void LiveVariables::getAnalysisUsage(AnalysisUsage &AU) const {
68+
void LiveVariablesWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
5469
AU.addRequiredID(UnreachableMachineBlockElimID);
5570
AU.setPreservesAll();
5671
MachineFunctionPass::getAnalysisUsage(AU);
5772
}
5873

74+
LiveVariables::LiveVariables(MachineFunction &MF)
75+
: MF(&MF), MRI(&MF.getRegInfo()), TRI(MF.getSubtarget().getRegisterInfo()) {
76+
analyze(MF);
77+
}
78+
79+
void LiveVariables::print(raw_ostream &OS) const {
80+
for (size_t I = 0, E = VirtRegInfo.size(); I != E; ++I) {
81+
const Register Reg = Register::index2VirtReg(I);
82+
OS << "Virtual register '%" << I << "':\n";
83+
VirtRegInfo[Reg].print(OS);
84+
}
85+
}
86+
5987
MachineInstr *
6088
LiveVariables::VarInfo::findKill(const MachineBasicBlock *MBB) const {
6189
for (MachineInstr *MI : Kills)
@@ -64,20 +92,22 @@ LiveVariables::VarInfo::findKill(const MachineBasicBlock *MBB) const {
6492
return nullptr;
6593
}
6694

67-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
68-
LLVM_DUMP_METHOD void LiveVariables::VarInfo::dump() const {
69-
dbgs() << " Alive in blocks: ";
95+
void LiveVariables::VarInfo::print(raw_ostream &OS) const {
96+
OS << " Alive in blocks: ";
7097
for (unsigned AB : AliveBlocks)
71-
dbgs() << AB << ", ";
72-
dbgs() << "\n Killed by:";
98+
OS << AB << ", ";
99+
OS << "\n Killed by:";
73100
if (Kills.empty())
74-
dbgs() << " No instructions.\n";
101+
OS << " No instructions.\n\n";
75102
else {
76103
for (unsigned i = 0, e = Kills.size(); i != e; ++i)
77-
dbgs() << "\n #" << i << ": " << *Kills[i];
78-
dbgs() << "\n";
104+
OS << "\n #" << i << ": " << *Kills[i];
105+
OS << "\n";
79106
}
80107
}
108+
109+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
110+
LLVM_DUMP_METHOD void LiveVariables::VarInfo::dump() const { print(dbgs()); }
81111
#endif
82112

83113
/// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
@@ -595,7 +625,7 @@ void LiveVariables::runOnBlock(MachineBasicBlock *MBB, unsigned NumRegs) {
595625
HandlePhysRegDef(i, nullptr, Defs);
596626
}
597627

598-
bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
628+
void LiveVariables::analyze(MachineFunction &mf) {
599629
MF = &mf;
600630
MRI = &mf.getRegInfo();
601631
TRI = MF->getSubtarget().getRegisterInfo();
@@ -649,8 +679,6 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
649679
PhysRegDef.clear();
650680
PhysRegUse.clear();
651681
PHIVarInfo.clear();
652-
653-
return false;
654682
}
655683

656684
void LiveVariables::recomputeForSingleDefVirtReg(Register Reg) {

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,8 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(
11711171
// On some targets like Mips, branches may kill virtual registers. Make sure
11721172
// that LiveVariables is properly updated after updateTerminator replaces the
11731173
// terminators.
1174-
LiveVariables *LV = P.getAnalysisIfAvailable<LiveVariables>();
1174+
auto *LVWrapper = P.getAnalysisIfAvailable<LiveVariablesWrapperPass>();
1175+
LiveVariables *LV = LVWrapper ? &LVWrapper->getLV() : nullptr;
11751176

11761177
// Collect a list of virtual registers killed by the terminators.
11771178
SmallVector<Register, 4> KilledRegs;

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ namespace {
314314

315315
void getAnalysisUsage(AnalysisUsage &AU) const override {
316316
AU.addUsedIfAvailable<LiveStacks>();
317-
AU.addUsedIfAvailable<LiveVariables>();
317+
AU.addUsedIfAvailable<LiveVariablesWrapperPass>();
318318
AU.addUsedIfAvailable<SlotIndexes>();
319319
AU.addUsedIfAvailable<LiveIntervals>();
320320
AU.setPreservesAll();
@@ -430,8 +430,9 @@ unsigned MachineVerifier::verify(const MachineFunction &MF) {
430430
if (PASS) {
431431
LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
432432
// We don't want to verify LiveVariables if LiveIntervals is available.
433+
auto *LVWrapper = PASS->getAnalysisIfAvailable<LiveVariablesWrapperPass>();
433434
if (!LiveInts)
434-
LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
435+
LiveVars = LVWrapper ? &LVWrapper->getLV() : nullptr;
435436
LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
436437
Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
437438
}

llvm/lib/CodeGen/PHIElimination.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ char& llvm::PHIEliminationID = PHIElimination::ID;
130130
INITIALIZE_PASS_BEGIN(PHIElimination, DEBUG_TYPE,
131131
"Eliminate PHI nodes for register allocation",
132132
false, false)
133-
INITIALIZE_PASS_DEPENDENCY(LiveVariables)
133+
INITIALIZE_PASS_DEPENDENCY(LiveVariablesWrapperPass)
134134
INITIALIZE_PASS_END(PHIElimination, DEBUG_TYPE,
135135
"Eliminate PHI nodes for register allocation", false, false)
136136

137137
void PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
138-
AU.addUsedIfAvailable<LiveVariables>();
139-
AU.addPreserved<LiveVariables>();
138+
AU.addUsedIfAvailable<LiveVariablesWrapperPass>();
139+
AU.addPreserved<LiveVariablesWrapperPass>();
140140
AU.addPreserved<SlotIndexes>();
141141
AU.addPreserved<LiveIntervals>();
142142
AU.addPreserved<MachineDominatorTreeWrapperPass>();
@@ -146,7 +146,8 @@ void PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
146146

147147
bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
148148
MRI = &MF.getRegInfo();
149-
LV = getAnalysisIfAvailable<LiveVariables>();
149+
auto *LVWrapper = getAnalysisIfAvailable<LiveVariablesWrapperPass>();
150+
LV = LVWrapper ? &LVWrapper->getLV() : nullptr;
150151
LIS = getAnalysisIfAvailable<LiveIntervals>();
151152

152153
bool Changed = false;

llvm/lib/CodeGen/TwoAddressInstructionPass.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ class TwoAddressInstructionPass : public MachineFunctionPass {
195195
void getAnalysisUsage(AnalysisUsage &AU) const override {
196196
AU.setPreservesCFG();
197197
AU.addUsedIfAvailable<AAResultsWrapperPass>();
198-
AU.addUsedIfAvailable<LiveVariables>();
199-
AU.addPreserved<LiveVariables>();
198+
AU.addUsedIfAvailable<LiveVariablesWrapperPass>();
199+
AU.addPreserved<LiveVariablesWrapperPass>();
200200
AU.addPreserved<SlotIndexes>();
201201
AU.addPreserved<LiveIntervals>();
202202
AU.addPreservedID(MachineLoopInfoID);
@@ -1762,7 +1762,8 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &Func) {
17621762
TII = MF->getSubtarget().getInstrInfo();
17631763
TRI = MF->getSubtarget().getRegisterInfo();
17641764
InstrItins = MF->getSubtarget().getInstrItineraryData();
1765-
LV = getAnalysisIfAvailable<LiveVariables>();
1765+
auto *LVWrapper = getAnalysisIfAvailable<LiveVariablesWrapperPass>();
1766+
LV = LVWrapper ? &LVWrapper->getLV() : nullptr;
17661767
LIS = getAnalysisIfAvailable<LiveIntervals>();
17671768
if (auto *AAPass = getAnalysisIfAvailable<AAResultsWrapperPass>())
17681769
AA = &AAPass->getAAResults();

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
#include "llvm/CodeGen/InterleavedAccess.h"
9191
#include "llvm/CodeGen/InterleavedLoadCombine.h"
9292
#include "llvm/CodeGen/JMCInstrumenter.h"
93+
#include "llvm/CodeGen/LiveVariables.h"
9394
#include "llvm/CodeGen/LocalStackSlotAllocation.h"
9495
#include "llvm/CodeGen/LowerEmuTLS.h"
9596
#include "llvm/CodeGen/MIRPrinter.h"

llvm/lib/Target/AMDGPU/SILowerControlFlow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,8 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
763763
// This doesn't actually need LiveIntervals, but we can preserve them.
764764
LIS = getAnalysisIfAvailable<LiveIntervals>();
765765
// This doesn't actually need LiveVariables, but we can preserve them.
766-
LV = getAnalysisIfAvailable<LiveVariables>();
766+
auto *LVWrapper = getAnalysisIfAvailable<LiveVariablesWrapperPass>();
767+
LV = LVWrapper ? &LVWrapper->getLV() : nullptr;
767768
auto *MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
768769
MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
769770
MRI = &MF.getRegInfo();

0 commit comments

Comments
 (0)