2525#include " llvm/CodeGen/LiveIntervals.h"
2626#include " llvm/CodeGen/LiveRangeEdit.h"
2727#include " llvm/CodeGen/MachineBasicBlock.h"
28+ #include " llvm/CodeGen/MachineDominators.h"
2829#include " llvm/CodeGen/MachineFunction.h"
2930#include " llvm/CodeGen/MachineFunctionPass.h"
3031#include " llvm/CodeGen/MachineInstr.h"
3132#include " llvm/CodeGen/MachineInstrBuilder.h"
3233#include " llvm/CodeGen/MachineLoopInfo.h"
3334#include " llvm/CodeGen/MachineOperand.h"
35+ #include " llvm/CodeGen/MachinePassManager.h"
3436#include " llvm/CodeGen/MachineRegisterInfo.h"
3537#include " llvm/CodeGen/Passes.h"
3638#include " llvm/CodeGen/RegisterClassInfo.h"
39+ #include " llvm/CodeGen/RegisterCoalescerPass.h"
3740#include " llvm/CodeGen/SlotIndexes.h"
3841#include " llvm/CodeGen/TargetInstrInfo.h"
3942#include " llvm/CodeGen/TargetOpcodes.h"
@@ -122,15 +125,14 @@ namespace {
122125
123126class JoinVals ;
124127
125- class RegisterCoalescer : public MachineFunctionPass ,
126- private LiveRangeEdit::Delegate {
128+ class RegisterCoalescer : private LiveRangeEdit ::Delegate {
127129 MachineFunction *MF = nullptr ;
128130 MachineRegisterInfo *MRI = nullptr ;
129131 const TargetRegisterInfo *TRI = nullptr ;
130132 const TargetInstrInfo *TII = nullptr ;
131133 LiveIntervals *LIS = nullptr ;
134+ SlotIndexes *SI = nullptr ;
132135 const MachineLoopInfo *Loops = nullptr ;
133- AliasAnalysis *AA = nullptr ;
134136 RegisterClassInfo RegClassInfo;
135137
136138 // / Position and VReg of a PHI instruction during coalescing.
@@ -378,11 +380,24 @@ class RegisterCoalescer : public MachineFunctionPass,
378380 void checkMergingChangesDbgValuesImpl (Register Reg, LiveRange &OtherRange,
379381 LiveRange &RegRange, JoinVals &Vals2);
380382
383+ public:
384+ RegisterCoalescer (LiveIntervals *LIS, SlotIndexes *SI,
385+ const MachineLoopInfo *Loops)
386+ : LIS(LIS), SI(SI), Loops(Loops) {}
387+
388+ void releaseMemory ();
389+ void print (raw_ostream &O, const Module * = nullptr ) const ;
390+ bool run (MachineFunction &MF);
391+ };
392+
393+ class RegisterCoalescerLegacy : public MachineFunctionPass {
394+ std::unique_ptr<RegisterCoalescer> Impl;
395+
381396public:
382397 static char ID; // /< Class identification, replacement for typeinfo
383398
384- RegisterCoalescer () : MachineFunctionPass(ID) {
385- initializeRegisterCoalescerPass (*PassRegistry::getPassRegistry ());
399+ RegisterCoalescerLegacy () : MachineFunctionPass(ID) {
400+ initializeRegisterCoalescerLegacyPass (*PassRegistry::getPassRegistry ());
386401 }
387402
388403 void getAnalysisUsage (AnalysisUsage &AU) const override ;
@@ -392,28 +407,30 @@ class RegisterCoalescer : public MachineFunctionPass,
392407 MachineFunctionProperties::Property::IsSSA);
393408 }
394409
395- void releaseMemory () override ;
410+ void releaseMemory () override { Impl-> releaseMemory (); }
396411
397412 // / This is the pass entry point.
398413 bool runOnMachineFunction (MachineFunction &) override ;
399414
400415 // / Implement the dump method.
401- void print (raw_ostream &O, const Module * = nullptr ) const override ;
416+ void print (raw_ostream &O, const Module * = nullptr ) const override {
417+ Impl->print (O, nullptr );
418+ }
402419};
403420
404421} // end anonymous namespace
405422
406- char RegisterCoalescer ::ID = 0 ;
423+ char RegisterCoalescerLegacy ::ID = 0 ;
407424
408- char &llvm::RegisterCoalescerID = RegisterCoalescer ::ID;
425+ char &llvm::RegisterCoalescerID = RegisterCoalescerLegacy ::ID;
409426
410- INITIALIZE_PASS_BEGIN (RegisterCoalescer , " register-coalescer" ,
427+ INITIALIZE_PASS_BEGIN (RegisterCoalescerLegacy , " register-coalescer" ,
411428 " Register Coalescer" , false , false )
412429INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
413430INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
414431INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
415432INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
416- INITIALIZE_PASS_END(RegisterCoalescer , " register-coalescer" ,
433+ INITIALIZE_PASS_END(RegisterCoalescerLegacy , " register-coalescer" ,
417434 " Register Coalescer" , false , false )
418435
419436[[nodiscard]] static bool isMoveInstr(const TargetRegisterInfo &tri,
@@ -590,8 +607,9 @@ bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
590607 }
591608}
592609
593- void RegisterCoalescer ::getAnalysisUsage (AnalysisUsage &AU) const {
610+ void RegisterCoalescerLegacy ::getAnalysisUsage (AnalysisUsage &AU) const {
594611 AU.setPreservesCFG ();
612+ AU.addUsedIfAvailable <SlotIndexesWrapperPass>();
595613 AU.addRequired <AAResultsWrapperPass>();
596614 AU.addRequired <LiveIntervalsWrapperPass>();
597615 AU.addPreserved <LiveIntervalsWrapperPass>();
@@ -4290,7 +4308,33 @@ void RegisterCoalescer::releaseMemory() {
42904308 LargeLIVisitCounter.clear ();
42914309}
42924310
4293- bool RegisterCoalescer::runOnMachineFunction (MachineFunction &fn) {
4311+ PreservedAnalyses
4312+ RegisterCoalescerPass::run (MachineFunction &MF,
4313+ MachineFunctionAnalysisManager &MFAM) {
4314+ auto &LIS = MFAM.getResult <LiveIntervalsAnalysis>(MF);
4315+ auto &Loops = MFAM.getResult <MachineLoopAnalysis>(MF);
4316+ auto *SI = MFAM.getCachedResult <SlotIndexesAnalysis>(MF);
4317+ RegisterCoalescer Impl (&LIS, SI, &Loops);
4318+ if (!Impl.run (MF))
4319+ return PreservedAnalyses::all ();
4320+ auto PA = getMachineFunctionPassPreservedAnalyses ();
4321+ PA.preserve <LiveIntervalsAnalysis>();
4322+ PA.preserve <SlotIndexesAnalysis>();
4323+ PA.preserve <MachineLoopAnalysis>();
4324+ PA.preserve <MachineDominatorTreeAnalysis>();
4325+ return PA;
4326+ }
4327+
4328+ bool RegisterCoalescerLegacy::runOnMachineFunction (MachineFunction &MF) {
4329+ auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4330+ auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
4331+ auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
4332+ SlotIndexes *SI = SIWrapper ? &SIWrapper->getSI () : nullptr ;
4333+ Impl.reset (new RegisterCoalescer (LIS, SI, Loops));
4334+ return Impl->run (MF);
4335+ }
4336+
4337+ bool RegisterCoalescer::run (MachineFunction &fn) {
42944338 LLVM_DEBUG (dbgs () << " ********** REGISTER COALESCER **********\n "
42954339 << " ********** Function: " << fn.getName () << ' \n ' );
42964340
@@ -4313,9 +4357,6 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
43134357 const TargetSubtargetInfo &STI = fn.getSubtarget ();
43144358 TRI = STI.getRegisterInfo ();
43154359 TII = STI.getInstrInfo ();
4316- LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4317- AA = &getAnalysis<AAResultsWrapperPass>().getAAResults ();
4318- Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
43194360 if (EnableGlobalCopies == cl::BOU_UNSET)
43204361 JoinGlobalCopies = STI.enableJoinGlobalCopies ();
43214362 else
@@ -4340,7 +4381,7 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
43404381 JoinSplitEdges = EnableJoinSplits;
43414382
43424383 if (VerifyCoalescing)
4343- MF->verify (this , " Before register coalescing" , &errs ());
4384+ MF->verify (LIS, SI , " Before register coalescing" , &errs ());
43444385
43454386 DbgVRegToValues.clear ();
43464387 buildVRegToDbgValueMap (fn);
@@ -4398,9 +4439,9 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
43984439 PHIValToPos.clear ();
43994440 RegToPHIIdx.clear ();
44004441
4401- LLVM_DEBUG (dump ( ));
4442+ LLVM_DEBUG (print ( dbgs (), nullptr ));
44024443 if (VerifyCoalescing)
4403- MF->verify (this , " After register coalescing" , &errs ());
4444+ MF->verify (LIS, SI , " After register coalescing" , &errs ());
44044445 return true ;
44054446}
44064447
0 commit comments