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+ RegisterCoalescer (LiveIntervals *LIS, SlotIndexes *SI,
380+ const MachineLoopInfo *Loops)
381+ : LIS(LIS), SI(SI), Loops(Loops) {}
382+
383+ void releaseMemory ();
384+ void print (raw_ostream &O, const Module * = nullptr ) const ;
385+ bool run (MachineFunction &MF);
386+ };
387+
388+ class RegisterCoalescerLegacy : public MachineFunctionPass {
389+ std::unique_ptr<RegisterCoalescer> Impl;
390+
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,27 +402,29 @@ class RegisterCoalescer : public MachineFunctionPass,
386402 MachineFunctionProperties::Property::IsSSA);
387403 }
388404
389- void releaseMemory () override ;
405+ void releaseMemory () override { Impl-> releaseMemory (); }
390406
391407 // / This is the pass entry point.
392408 bool runOnMachineFunction (MachineFunction &) override ;
393409
394410 // / Implement the dump method.
395- void print (raw_ostream &O, const Module * = nullptr ) const override ;
411+ void print (raw_ostream &O, const Module * = nullptr ) const override {
412+ Impl->print (O, nullptr );
413+ }
396414};
397415
398416} // end anonymous namespace
399417
400- char RegisterCoalescer ::ID = 0 ;
418+ char RegisterCoalescerLegacy ::ID = 0 ;
401419
402- char &llvm::RegisterCoalescerID = RegisterCoalescer ::ID;
420+ char &llvm::RegisterCoalescerID = RegisterCoalescerLegacy ::ID;
403421
404- INITIALIZE_PASS_BEGIN (RegisterCoalescer , " register-coalescer" ,
422+ INITIALIZE_PASS_BEGIN (RegisterCoalescerLegacy , " register-coalescer" ,
405423 " Register Coalescer" , false , false )
406424INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
407425INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
408426INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
409- INITIALIZE_PASS_END(RegisterCoalescer , " register-coalescer" ,
427+ INITIALIZE_PASS_END(RegisterCoalescerLegacy , " register-coalescer" ,
410428 " Register Coalescer" , false , false )
411429
412430[[nodiscard]] static bool isMoveInstr(const TargetRegisterInfo &tri,
@@ -583,8 +601,9 @@ bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
583601 }
584602}
585603
586- void RegisterCoalescer ::getAnalysisUsage (AnalysisUsage &AU) const {
604+ void RegisterCoalescerLegacy ::getAnalysisUsage (AnalysisUsage &AU) const {
587605 AU.setPreservesCFG ();
606+ AU.addUsedIfAvailable <SlotIndexesWrapperPass>();
588607 AU.addRequired <LiveIntervalsWrapperPass>();
589608 AU.addPreserved <LiveIntervalsWrapperPass>();
590609 AU.addPreserved <SlotIndexesWrapperPass>();
@@ -4237,7 +4256,33 @@ void RegisterCoalescer::releaseMemory() {
42374256 LargeLIVisitCounter.clear ();
42384257}
42394258
4240- bool RegisterCoalescer::runOnMachineFunction (MachineFunction &fn) {
4259+ PreservedAnalyses
4260+ RegisterCoalescerPass::run (MachineFunction &MF,
4261+ MachineFunctionAnalysisManager &MFAM) {
4262+ auto &LIS = MFAM.getResult <LiveIntervalsAnalysis>(MF);
4263+ auto &Loops = MFAM.getResult <MachineLoopAnalysis>(MF);
4264+ auto *SI = MFAM.getCachedResult <SlotIndexesAnalysis>(MF);
4265+ RegisterCoalescer Impl (&LIS, SI, &Loops);
4266+ if (!Impl.run (MF))
4267+ return PreservedAnalyses::all ();
4268+ auto PA = getMachineFunctionPassPreservedAnalyses ();
4269+ PA.preserve <LiveIntervalsAnalysis>();
4270+ PA.preserve <SlotIndexesAnalysis>();
4271+ PA.preserve <MachineLoopAnalysis>();
4272+ PA.preserve <MachineDominatorTreeAnalysis>();
4273+ return PA;
4274+ }
4275+
4276+ bool RegisterCoalescerLegacy::runOnMachineFunction (MachineFunction &MF) {
4277+ auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4278+ auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
4279+ auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
4280+ SlotIndexes *SI = SIWrapper ? &SIWrapper->getSI () : nullptr ;
4281+ Impl.reset (new RegisterCoalescer (LIS, SI, Loops));
4282+ return Impl->run (MF);
4283+ }
4284+
4285+ bool RegisterCoalescer::run (MachineFunction &fn) {
42414286 LLVM_DEBUG (dbgs () << " ********** REGISTER COALESCER **********\n "
42424287 << " ********** Function: " << fn.getName () << ' \n ' );
42434288
@@ -4260,8 +4305,6 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
42604305 const TargetSubtargetInfo &STI = fn.getSubtarget ();
42614306 TRI = STI.getRegisterInfo ();
42624307 TII = STI.getInstrInfo ();
4263- LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4264- Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
42654308 if (EnableGlobalCopies == cl::BOU_UNSET)
42664309 JoinGlobalCopies = STI.enableJoinGlobalCopies ();
42674310 else
@@ -4286,7 +4329,7 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
42864329 JoinSplitEdges = EnableJoinSplits;
42874330
42884331 if (VerifyCoalescing)
4289- MF->verify (this , " Before register coalescing" , &errs ());
4332+ MF->verify (LIS, SI , " Before register coalescing" , &errs ());
42904333
42914334 DbgVRegToValues.clear ();
42924335 buildVRegToDbgValueMap (fn);
@@ -4344,9 +4387,9 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
43444387 PHIValToPos.clear ();
43454388 RegToPHIIdx.clear ();
43464389
4347- LLVM_DEBUG (dump ( ));
4390+ LLVM_DEBUG (print ( dbgs (), nullptr ));
43484391 if (VerifyCoalescing)
4349- MF->verify (this , " After register coalescing" , &errs ());
4392+ MF->verify (LIS, SI , " After register coalescing" , &errs ());
43504393 return true ;
43514394}
43524395
0 commit comments