diff --git a/llvm/include/llvm/CodeGen/LiveRegMatrix.h b/llvm/include/llvm/CodeGen/LiveRegMatrix.h index 2b32308c7c075..84050bf170737 100644 --- a/llvm/include/llvm/CodeGen/LiveRegMatrix.h +++ b/llvm/include/llvm/CodeGen/LiveRegMatrix.h @@ -37,7 +37,9 @@ class MachineFunction; class TargetRegisterInfo; class VirtRegMap; -class LiveRegMatrix : public MachineFunctionPass { +class LiveRegMatrix { + friend class LiveRegMatrixWrapperLegacy; + friend class LiveRegMatrixAnalysis; const TargetRegisterInfo *TRI = nullptr; LiveIntervals *LIS = nullptr; VirtRegMap *VRM = nullptr; @@ -57,15 +59,17 @@ class LiveRegMatrix : public MachineFunctionPass { unsigned RegMaskVirtReg = 0; BitVector RegMaskUsable; - // MachineFunctionPass boilerplate. - void getAnalysisUsage(AnalysisUsage &) const override; - bool runOnMachineFunction(MachineFunction &) override; - void releaseMemory() override; + LiveRegMatrix() = default; + void releaseMemory(); public: - static char ID; + LiveRegMatrix(LiveRegMatrix &&Other) + : TRI(Other.TRI), LIS(Other.LIS), VRM(Other.VRM), UserTag(Other.UserTag), + Matrix(std::move(Other.Matrix)), Queries(std::move(Other.Queries)), + RegMaskTag(Other.RegMaskTag), RegMaskVirtReg(Other.RegMaskVirtReg), + RegMaskUsable(std::move(Other.RegMaskUsable)) {} - LiveRegMatrix(); + void init(MachineFunction &MF, LiveIntervals &LIS, VirtRegMap &VRM); //===--------------------------------------------------------------------===// // High-level interface. @@ -159,6 +163,32 @@ class LiveRegMatrix : public MachineFunctionPass { Register getOneVReg(unsigned PhysReg) const; }; +class LiveRegMatrixWrapperLegacy : public MachineFunctionPass { + LiveRegMatrix LRM; + +public: + static char ID; + + LiveRegMatrixWrapperLegacy() : MachineFunctionPass(ID) {} + + LiveRegMatrix &getLRM() { return LRM; } + const LiveRegMatrix &getLRM() const { return LRM; } + + void getAnalysisUsage(AnalysisUsage &AU) const override; + bool runOnMachineFunction(MachineFunction &MF) override; + void releaseMemory() override; +}; + +class LiveRegMatrixAnalysis : public AnalysisInfoMixin { + friend AnalysisInfoMixin; + static AnalysisKey Key; + +public: + using Result = LiveRegMatrix; + + LiveRegMatrix run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM); +}; + } // end namespace llvm #endif // LLVM_CODEGEN_LIVEREGMATRIX_H diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h index 537166c393c7f..a879089d2fe61 100644 --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -156,7 +156,7 @@ void initializeLiveDebugValuesPass(PassRegistry &); void initializeLiveDebugVariablesPass(PassRegistry &); void initializeLiveIntervalsWrapperPassPass(PassRegistry &); void initializeLiveRangeShrinkPass(PassRegistry &); -void initializeLiveRegMatrixPass(PassRegistry &); +void initializeLiveRegMatrixWrapperLegacyPass(PassRegistry &); void initializeLiveStacksPass(PassRegistry &); void initializeLiveVariablesWrapperPassPass(PassRegistry &); void initializeLoadStoreOptPass(PassRegistry &); diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def index d92fd19a1882c..4a4f43475e7d2 100644 --- a/llvm/include/llvm/Passes/MachinePassRegistry.def +++ b/llvm/include/llvm/Passes/MachinePassRegistry.def @@ -96,6 +96,7 @@ LOOP_PASS("loop-term-fold", LoopTermFoldPass()) // computed. (We still either need to regenerate kill flags after regalloc, or // preferably fix the scavenger to not depend on them). MACHINE_FUNCTION_ANALYSIS("live-intervals", LiveIntervalsAnalysis()) +MACHINE_FUNCTION_ANALYSIS("live-reg-matrix", LiveRegMatrixAnalysis()) MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis()) MACHINE_FUNCTION_ANALYSIS("machine-block-freq", MachineBlockFrequencyAnalysis()) MACHINE_FUNCTION_ANALYSIS("machine-branch-prob", @@ -121,8 +122,8 @@ MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis()) // MachinePostDominatorTreeAnalysis()) // MACHINE_FUNCTION_ANALYSIS("machine-region-info", // MachineRegionInfoPassAnalysis()) -// ReachingDefAnalysisAnalysis()) MACHINE_FUNCTION_ANALYSIS("live-reg-matrix", -// LiveRegMatrixAnalysis()) MACHINE_FUNCTION_ANALYSIS("gc-analysis", +// MACHINE_FUNCTION_ANALYSIS("reaching-def", +// ReachingDefAnalysisAnalysis()) MACHINE_FUNCTION_ANALYSIS("gc-analysis", // GCMachineCodeAnalysisPass()) #undef MACHINE_FUNCTION_ANALYSIS diff --git a/llvm/lib/CodeGen/LiveRegMatrix.cpp b/llvm/lib/CodeGen/LiveRegMatrix.cpp index a0e8b0e4f9cc1..a57233cf37da0 100644 --- a/llvm/lib/CodeGen/LiveRegMatrix.cpp +++ b/llvm/lib/CodeGen/LiveRegMatrix.cpp @@ -35,27 +35,33 @@ using namespace llvm; STATISTIC(NumAssigned , "Number of registers assigned"); STATISTIC(NumUnassigned , "Number of registers unassigned"); -char LiveRegMatrix::ID = 0; -INITIALIZE_PASS_BEGIN(LiveRegMatrix, "liveregmatrix", +char LiveRegMatrixWrapperLegacy::ID = 0; +INITIALIZE_PASS_BEGIN(LiveRegMatrixWrapperLegacy, "liveregmatrix", "Live Register Matrix", false, false) INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass) INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy) -INITIALIZE_PASS_END(LiveRegMatrix, "liveregmatrix", +INITIALIZE_PASS_END(LiveRegMatrixWrapperLegacy, "liveregmatrix", "Live Register Matrix", false, false) -LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID) {} - -void LiveRegMatrix::getAnalysisUsage(AnalysisUsage &AU) const { +void LiveRegMatrixWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); AU.addRequiredTransitive(); AU.addRequiredTransitive(); MachineFunctionPass::getAnalysisUsage(AU); } -bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) { +bool LiveRegMatrixWrapperLegacy::runOnMachineFunction(MachineFunction &MF) { + auto &LIS = getAnalysis().getLIS(); + auto &VRM = getAnalysis().getVRM(); + LRM.init(MF, LIS, VRM); + return false; +} + +void LiveRegMatrix::init(MachineFunction &MF, LiveIntervals &pLIS, + VirtRegMap &pVRM) { TRI = MF.getSubtarget().getRegisterInfo(); - LIS = &getAnalysis().getLIS(); - VRM = &getAnalysis().getVRM(); + LIS = &pLIS; + VRM = &pVRM; unsigned NumRegUnits = TRI->getNumRegUnits(); if (NumRegUnits != Matrix.size()) @@ -64,9 +70,10 @@ bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) { // Make sure no stale queries get reused. invalidateVirtRegs(); - return false; } +void LiveRegMatrixWrapperLegacy::releaseMemory() { LRM.releaseMemory(); } + void LiveRegMatrix::releaseMemory() { for (unsigned i = 0, e = Matrix.size(); i != e; ++i) { Matrix[i].clear(); @@ -246,3 +253,14 @@ Register LiveRegMatrix::getOneVReg(unsigned PhysReg) const { return MCRegister::NoRegister; } + +AnalysisKey LiveRegMatrixAnalysis::Key; + +LiveRegMatrix LiveRegMatrixAnalysis::run(MachineFunction &MF, + MachineFunctionAnalysisManager &MFAM) { + auto &LIS = MFAM.getResult(MF); + auto &VRM = MFAM.getResult(MF); + LiveRegMatrix LRM; + LRM.init(MF, LIS, VRM); + return LRM; +} diff --git a/llvm/lib/CodeGen/RegAllocBasic.cpp b/llvm/lib/CodeGen/RegAllocBasic.cpp index 07d7e212adfce..55d806e768b91 100644 --- a/llvm/lib/CodeGen/RegAllocBasic.cpp +++ b/llvm/lib/CodeGen/RegAllocBasic.cpp @@ -140,7 +140,7 @@ INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy) -INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix) +INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy) INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) INITIALIZE_PASS_END(RABasic, "regallocbasic", "Basic Register Allocator", false, false) @@ -193,8 +193,8 @@ void RABasic::getAnalysisUsage(AnalysisUsage &AU) const { AU.addPreserved(); AU.addRequired(); AU.addPreserved(); - AU.addRequired(); - AU.addPreserved(); + AU.addRequired(); + AU.addPreserved(); MachineFunctionPass::getAnalysisUsage(AU); } @@ -312,7 +312,7 @@ bool RABasic::runOnMachineFunction(MachineFunction &mf) { MF = &mf; RegAllocBase::init(getAnalysis().getVRM(), getAnalysis().getLIS(), - getAnalysis()); + getAnalysis().getLRM()); VirtRegAuxInfo VRAI( *MF, *LIS, *VRM, getAnalysis().getLI(), getAnalysis().getMBFI(), diff --git a/llvm/lib/CodeGen/RegAllocGreedy.cpp b/llvm/lib/CodeGen/RegAllocGreedy.cpp index 2c9bb0aa14557..9cb596be96f99 100644 --- a/llvm/lib/CodeGen/RegAllocGreedy.cpp +++ b/llvm/lib/CodeGen/RegAllocGreedy.cpp @@ -163,7 +163,7 @@ INITIALIZE_PASS_DEPENDENCY(LiveStacks) INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy) -INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix) +INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy) INITIALIZE_PASS_DEPENDENCY(EdgeBundles) INITIALIZE_PASS_DEPENDENCY(SpillPlacement) INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass) @@ -217,8 +217,8 @@ void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { AU.addPreserved(); AU.addRequired(); AU.addPreserved(); - AU.addRequired(); - AU.addPreserved(); + AU.addRequired(); + AU.addPreserved(); AU.addRequired(); AU.addRequired(); AU.addRequired(); @@ -2718,7 +2718,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { RegAllocBase::init(getAnalysis().getVRM(), getAnalysis().getLIS(), - getAnalysis()); + getAnalysis().getLRM()); // Early return if there is no virtual register to be allocated to a // physical register. diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index c2aef42780321..67ebc3015a46d 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -96,6 +96,7 @@ #include "llvm/CodeGen/InterleavedLoadCombine.h" #include "llvm/CodeGen/JMCInstrumenter.h" #include "llvm/CodeGen/LiveIntervals.h" +#include "llvm/CodeGen/LiveRegMatrix.h" #include "llvm/CodeGen/LiveVariables.h" #include "llvm/CodeGen/LocalStackSlotAllocation.h" #include "llvm/CodeGen/LowerEmuTLS.h" diff --git a/llvm/lib/Target/AMDGPU/GCNNSAReassign.cpp b/llvm/lib/Target/AMDGPU/GCNNSAReassign.cpp index 40af37141fe61..85e79aa4b7595 100644 --- a/llvm/lib/Target/AMDGPU/GCNNSAReassign.cpp +++ b/llvm/lib/Target/AMDGPU/GCNNSAReassign.cpp @@ -50,7 +50,7 @@ class GCNNSAReassign : public MachineFunctionPass { void getAnalysisUsage(AnalysisUsage &AU) const override { AU.addRequired(); AU.addRequired(); - AU.addRequired(); + AU.addRequired(); AU.setPreservesAll(); MachineFunctionPass::getAnalysisUsage(AU); } @@ -96,7 +96,7 @@ INITIALIZE_PASS_BEGIN(GCNNSAReassign, DEBUG_TYPE, "GCN NSA Reassign", false, false) INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass) INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy) -INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix) +INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy) INITIALIZE_PASS_END(GCNNSAReassign, DEBUG_TYPE, "GCN NSA Reassign", false, false) @@ -243,7 +243,7 @@ bool GCNNSAReassign::runOnMachineFunction(MachineFunction &MF) { MRI = &MF.getRegInfo(); TRI = ST->getRegisterInfo(); VRM = &getAnalysis().getVRM(); - LRM = &getAnalysis(); + LRM = &getAnalysis().getLRM(); LIS = &getAnalysis().getLIS(); const SIMachineFunctionInfo *MFI = MF.getInfo(); diff --git a/llvm/lib/Target/AMDGPU/SIPreAllocateWWMRegs.cpp b/llvm/lib/Target/AMDGPU/SIPreAllocateWWMRegs.cpp index 211f3f11006ff..fbb04f404b488 100644 --- a/llvm/lib/Target/AMDGPU/SIPreAllocateWWMRegs.cpp +++ b/llvm/lib/Target/AMDGPU/SIPreAllocateWWMRegs.cpp @@ -61,7 +61,7 @@ class SIPreAllocateWWMRegs : public MachineFunctionPass { void getAnalysisUsage(AnalysisUsage &AU) const override { AU.addRequired(); AU.addRequired(); - AU.addRequired(); + AU.addRequired(); AU.setPreservesAll(); MachineFunctionPass::getAnalysisUsage(AU); } @@ -77,7 +77,7 @@ INITIALIZE_PASS_BEGIN(SIPreAllocateWWMRegs, DEBUG_TYPE, "SI Pre-allocate WWM Registers", false, false) INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass) INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy) -INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix) +INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy) INITIALIZE_PASS_END(SIPreAllocateWWMRegs, DEBUG_TYPE, "SI Pre-allocate WWM Registers", false, false) @@ -194,7 +194,7 @@ bool SIPreAllocateWWMRegs::runOnMachineFunction(MachineFunction &MF) { MRI = &MF.getRegInfo(); LIS = &getAnalysis().getLIS(); - Matrix = &getAnalysis(); + Matrix = &getAnalysis().getLRM(); VRM = &getAnalysis().getVRM(); RegClassInfo.runOnMachineFunction(MF);