2424#include " llvm/CodeGen/LiveIntervals.h"
2525#include " llvm/CodeGen/LiveRangeEdit.h"
2626#include " llvm/CodeGen/MachineBasicBlock.h"
27+ #include " llvm/CodeGen/MachineDominators.h"
2728#include " llvm/CodeGen/MachineFunction.h"
2829#include " llvm/CodeGen/MachineFunctionPass.h"
2930#include " llvm/CodeGen/MachineInstr.h"
3031#include " llvm/CodeGen/MachineInstrBuilder.h"
3132#include " llvm/CodeGen/MachineLoopInfo.h"
3233#include " llvm/CodeGen/MachineOperand.h"
34+ #include " llvm/CodeGen/MachinePassManager.h"
3335#include " llvm/CodeGen/MachineRegisterInfo.h"
3436#include " llvm/CodeGen/Passes.h"
3537#include " llvm/CodeGen/RegisterClassInfo.h"
38+ #include " llvm/CodeGen/RegisterCoalescerPass.h"
3639#include " llvm/CodeGen/SlotIndexes.h"
3740#include " llvm/CodeGen/TargetInstrInfo.h"
3841#include " llvm/CodeGen/TargetOpcodes.h"
@@ -121,13 +124,13 @@ namespace {
121124
122125class JoinVals ;
123126
124- class RegisterCoalescer : public MachineFunctionPass ,
125- private LiveRangeEdit::Delegate {
127+ class RegisterCoalescer : private LiveRangeEdit ::Delegate {
126128 MachineFunction *MF = nullptr ;
127129 MachineRegisterInfo *MRI = nullptr ;
128130 const TargetRegisterInfo *TRI = nullptr ;
129131 const TargetInstrInfo *TII = nullptr ;
130132 LiveIntervals *LIS = nullptr ;
133+ SlotIndexes *SI = nullptr ;
131134 const MachineLoopInfo *Loops = nullptr ;
132135 RegisterClassInfo RegClassInfo;
133136
@@ -372,11 +375,24 @@ class RegisterCoalescer : public MachineFunctionPass,
372375 void checkMergingChangesDbgValuesImpl (Register Reg, LiveRange &OtherRange,
373376 LiveRange &RegRange, JoinVals &Vals2);
374377
378+ public:
379+ // For legacy pass only.
380+ RegisterCoalescer () {}
381+ RegisterCoalescer &operator =(RegisterCoalescer &&Other) = default ;
382+
383+ RegisterCoalescer (LiveIntervals *LIS, SlotIndexes *SI,
384+ const MachineLoopInfo *Loops)
385+ : LIS(LIS), SI(SI), Loops(Loops) {}
386+
387+ bool run (MachineFunction &MF);
388+ };
389+
390+ class RegisterCoalescerLegacy : public MachineFunctionPass {
375391public:
376392 static char ID; // /< Class identification, replacement for typeinfo
377393
378- RegisterCoalescer () : MachineFunctionPass(ID) {
379- initializeRegisterCoalescerPass (*PassRegistry::getPassRegistry ());
394+ RegisterCoalescerLegacy () : MachineFunctionPass(ID) {
395+ initializeRegisterCoalescerLegacyPass (*PassRegistry::getPassRegistry ());
380396 }
381397
382398 void getAnalysisUsage (AnalysisUsage &AU) const override ;
@@ -386,24 +402,22 @@ class RegisterCoalescer : public MachineFunctionPass,
386402 MachineFunctionProperties::Property::IsSSA);
387403 }
388404
389- void releaseMemory () override ;
390-
391405 // / This is the pass entry point.
392406 bool runOnMachineFunction (MachineFunction &) override ;
393407};
394408
395409} // end anonymous namespace
396410
397- char RegisterCoalescer ::ID = 0 ;
411+ char RegisterCoalescerLegacy ::ID = 0 ;
398412
399- char &llvm::RegisterCoalescerID = RegisterCoalescer ::ID;
413+ char &llvm::RegisterCoalescerID = RegisterCoalescerLegacy ::ID;
400414
401- INITIALIZE_PASS_BEGIN (RegisterCoalescer , " register-coalescer" ,
415+ INITIALIZE_PASS_BEGIN (RegisterCoalescerLegacy , " register-coalescer" ,
402416 " Register Coalescer" , false , false )
403417INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
404418INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
405419INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
406- INITIALIZE_PASS_END(RegisterCoalescer , " register-coalescer" ,
420+ INITIALIZE_PASS_END(RegisterCoalescerLegacy , " register-coalescer" ,
407421 " Register Coalescer" , false , false )
408422
409423[[nodiscard]] static bool isMoveInstr(const TargetRegisterInfo &tri,
@@ -580,8 +594,9 @@ bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
580594 }
581595}
582596
583- void RegisterCoalescer ::getAnalysisUsage (AnalysisUsage &AU) const {
597+ void RegisterCoalescerLegacy ::getAnalysisUsage (AnalysisUsage &AU) const {
584598 AU.setPreservesCFG ();
599+ AU.addUsedIfAvailable <SlotIndexesWrapperPass>();
585600 AU.addRequired <LiveIntervalsWrapperPass>();
586601 AU.addPreserved <LiveIntervalsWrapperPass>();
587602 AU.addPreserved <SlotIndexesWrapperPass>();
@@ -4226,15 +4241,35 @@ void RegisterCoalescer::joinAllIntervals() {
42264241 lateLiveIntervalUpdate ();
42274242}
42284243
4229- void RegisterCoalescer::releaseMemory () {
4230- ErasedInstrs.clear ();
4231- WorkList.clear ();
4232- DeadDefs.clear ();
4233- InflateRegs.clear ();
4234- LargeLIVisitCounter.clear ();
4244+ PreservedAnalyses
4245+ RegisterCoalescerPass::run (MachineFunction &MF,
4246+ MachineFunctionAnalysisManager &MFAM) {
4247+ MFPropsModifier _ (*this , MF);
4248+ auto &LIS = MFAM.getResult <LiveIntervalsAnalysis>(MF);
4249+ auto &Loops = MFAM.getResult <MachineLoopAnalysis>(MF);
4250+ auto *SI = MFAM.getCachedResult <SlotIndexesAnalysis>(MF);
4251+ RegisterCoalescer Impl (&LIS, SI, &Loops);
4252+ if (!Impl.run (MF))
4253+ return PreservedAnalyses::all ();
4254+ auto PA = getMachineFunctionPassPreservedAnalyses ();
4255+ PA.preserveSet <CFGAnalyses>();
4256+ PA.preserve <LiveIntervalsAnalysis>();
4257+ PA.preserve <SlotIndexesAnalysis>();
4258+ PA.preserve <MachineLoopAnalysis>();
4259+ PA.preserve <MachineDominatorTreeAnalysis>();
4260+ return PA;
4261+ }
4262+
4263+ bool RegisterCoalescerLegacy::runOnMachineFunction (MachineFunction &MF) {
4264+ auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4265+ auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
4266+ auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
4267+ SlotIndexes *SI = SIWrapper ? &SIWrapper->getSI () : nullptr ;
4268+ RegisterCoalescer Impl (LIS, SI, Loops);
4269+ return Impl.run (MF);
42354270}
42364271
4237- bool RegisterCoalescer::runOnMachineFunction (MachineFunction &fn) {
4272+ bool RegisterCoalescer::run (MachineFunction &fn) {
42384273 LLVM_DEBUG (dbgs () << " ********** REGISTER COALESCER **********\n "
42394274 << " ********** Function: " << fn.getName () << ' \n ' );
42404275
@@ -4257,8 +4292,6 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
42574292 const TargetSubtargetInfo &STI = fn.getSubtarget ();
42584293 TRI = STI.getRegisterInfo ();
42594294 TII = STI.getInstrInfo ();
4260- LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4261- Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
42624295 if (EnableGlobalCopies == cl::BOU_UNSET)
42634296 JoinGlobalCopies = STI.enableJoinGlobalCopies ();
42644297 else
@@ -4283,7 +4316,7 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
42834316 JoinSplitEdges = EnableJoinSplits;
42844317
42854318 if (VerifyCoalescing)
4286- MF->verify (this , " Before register coalescing" , &errs ());
4319+ MF->verify (LIS, SI , " Before register coalescing" , &errs ());
42874320
42884321 DbgVRegToValues.clear ();
42894322 buildVRegToDbgValueMap (fn);
@@ -4342,7 +4375,8 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
43424375 RegToPHIIdx.clear ();
43434376
43444377 LLVM_DEBUG (LIS->dump ());
4378+
43454379 if (VerifyCoalescing)
4346- MF->verify (this , " After register coalescing" , &errs ());
4380+ MF->verify (LIS, SI , " After register coalescing" , &errs ());
43474381 return true ;
43484382}
0 commit comments