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,23 @@ class RegisterCoalescer : public MachineFunctionPass,
372375 void checkMergingChangesDbgValuesImpl (Register Reg, LiveRange &OtherRange,
373376 LiveRange &RegRange, JoinVals &Vals2);
374377
378+ public:
379+ RegisterCoalescer (LiveIntervals *LIS, SlotIndexes *SI,
380+ const MachineLoopInfo *Loops)
381+ : LIS(LIS), SI(SI), Loops(Loops) {}
382+
383+ void releaseMemory ();
384+ bool run (MachineFunction &MF);
385+ };
386+
387+ class RegisterCoalescerLegacy : public MachineFunctionPass {
388+ std::unique_ptr<RegisterCoalescer> Impl;
389+
375390public:
376391 static char ID; // /< Class identification, replacement for typeinfo
377392
378- RegisterCoalescer () : MachineFunctionPass(ID) {
379- initializeRegisterCoalescerPass (*PassRegistry::getPassRegistry ());
393+ RegisterCoalescerLegacy () : MachineFunctionPass(ID) {
394+ initializeRegisterCoalescerLegacyPass (*PassRegistry::getPassRegistry ());
380395 }
381396
382397 void getAnalysisUsage (AnalysisUsage &AU) const override ;
@@ -386,24 +401,24 @@ class RegisterCoalescer : public MachineFunctionPass,
386401 MachineFunctionProperties::Property::IsSSA);
387402 }
388403
389- void releaseMemory () override ;
404+ void releaseMemory () override { Impl-> releaseMemory (); }
390405
391406 // / This is the pass entry point.
392407 bool runOnMachineFunction (MachineFunction &) override ;
393408};
394409
395410} // end anonymous namespace
396411
397- char RegisterCoalescer ::ID = 0 ;
412+ char RegisterCoalescerLegacy ::ID = 0 ;
398413
399- char &llvm::RegisterCoalescerID = RegisterCoalescer ::ID;
414+ char &llvm::RegisterCoalescerID = RegisterCoalescerLegacy ::ID;
400415
401- INITIALIZE_PASS_BEGIN (RegisterCoalescer , " register-coalescer" ,
416+ INITIALIZE_PASS_BEGIN (RegisterCoalescerLegacy , " register-coalescer" ,
402417 " Register Coalescer" , false , false )
403418INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
404419INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
405420INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
406- INITIALIZE_PASS_END(RegisterCoalescer , " register-coalescer" ,
421+ INITIALIZE_PASS_END(RegisterCoalescerLegacy , " register-coalescer" ,
407422 " Register Coalescer" , false , false )
408423
409424[[nodiscard]] static bool isMoveInstr(const TargetRegisterInfo &tri,
@@ -580,8 +595,9 @@ bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
580595 }
581596}
582597
583- void RegisterCoalescer ::getAnalysisUsage (AnalysisUsage &AU) const {
598+ void RegisterCoalescerLegacy ::getAnalysisUsage (AnalysisUsage &AU) const {
584599 AU.setPreservesCFG ();
600+ AU.addUsedIfAvailable <SlotIndexesWrapperPass>();
585601 AU.addRequired <LiveIntervalsWrapperPass>();
586602 AU.addPreserved <LiveIntervalsWrapperPass>();
587603 AU.addPreserved <SlotIndexesWrapperPass>();
@@ -4234,7 +4250,33 @@ void RegisterCoalescer::releaseMemory() {
42344250 LargeLIVisitCounter.clear ();
42354251}
42364252
4237- bool RegisterCoalescer::runOnMachineFunction (MachineFunction &fn) {
4253+ PreservedAnalyses
4254+ RegisterCoalescerPass::run (MachineFunction &MF,
4255+ MachineFunctionAnalysisManager &MFAM) {
4256+ auto &LIS = MFAM.getResult <LiveIntervalsAnalysis>(MF);
4257+ auto &Loops = MFAM.getResult <MachineLoopAnalysis>(MF);
4258+ auto *SI = MFAM.getCachedResult <SlotIndexesAnalysis>(MF);
4259+ RegisterCoalescer Impl (&LIS, SI, &Loops);
4260+ if (!Impl.run (MF))
4261+ return PreservedAnalyses::all ();
4262+ auto PA = getMachineFunctionPassPreservedAnalyses ();
4263+ PA.preserve <LiveIntervalsAnalysis>();
4264+ PA.preserve <SlotIndexesAnalysis>();
4265+ PA.preserve <MachineLoopAnalysis>();
4266+ PA.preserve <MachineDominatorTreeAnalysis>();
4267+ return PA;
4268+ }
4269+
4270+ bool RegisterCoalescerLegacy::runOnMachineFunction (MachineFunction &MF) {
4271+ auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4272+ auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
4273+ auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
4274+ SlotIndexes *SI = SIWrapper ? &SIWrapper->getSI () : nullptr ;
4275+ Impl.reset (new RegisterCoalescer (LIS, SI, Loops));
4276+ return Impl->run (MF);
4277+ }
4278+
4279+ bool RegisterCoalescer::run (MachineFunction &fn) {
42384280 LLVM_DEBUG (dbgs () << " ********** REGISTER COALESCER **********\n "
42394281 << " ********** Function: " << fn.getName () << ' \n ' );
42404282
@@ -4257,8 +4299,6 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
42574299 const TargetSubtargetInfo &STI = fn.getSubtarget ();
42584300 TRI = STI.getRegisterInfo ();
42594301 TII = STI.getInstrInfo ();
4260- LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4261- Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
42624302 if (EnableGlobalCopies == cl::BOU_UNSET)
42634303 JoinGlobalCopies = STI.enableJoinGlobalCopies ();
42644304 else
@@ -4283,7 +4323,7 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
42834323 JoinSplitEdges = EnableJoinSplits;
42844324
42854325 if (VerifyCoalescing)
4286- MF->verify (this , " Before register coalescing" , &errs ());
4326+ MF->verify (LIS, SI , " Before register coalescing" , &errs ());
42874327
42884328 DbgVRegToValues.clear ();
42894329 buildVRegToDbgValueMap (fn);
@@ -4342,7 +4382,8 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
43424382 RegToPHIIdx.clear ();
43434383
43444384 LLVM_DEBUG (LIS->dump ());
4385+
43454386 if (VerifyCoalescing)
4346- MF->verify (this , " After register coalescing" , &errs ());
4387+ MF->verify (LIS, SI , " After register coalescing" , &errs ());
43474388 return true ;
43484389}
0 commit comments