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.
@@ -374,11 +376,24 @@ class RegisterCoalescer : public MachineFunctionPass,
374376 void checkMergingChangesDbgValuesImpl (Register Reg, LiveRange &OtherRange,
375377 LiveRange &RegRange, JoinVals &Vals2);
376378
379+ public:
380+ RegisterCoalescer (LiveIntervals *LIS, SlotIndexes *SI,
381+ const MachineLoopInfo *Loops)
382+ : LIS(LIS), SI(SI), Loops(Loops) {}
383+
384+ void releaseMemory ();
385+ void print (raw_ostream &O, const Module * = nullptr ) const ;
386+ bool run (MachineFunction &MF);
387+ };
388+
389+ class RegisterCoalescerLegacy : public MachineFunctionPass {
390+ std::unique_ptr<RegisterCoalescer> Impl;
391+
377392public:
378393 static char ID; // /< Class identification, replacement for typeinfo
379394
380- RegisterCoalescer () : MachineFunctionPass(ID) {
381- initializeRegisterCoalescerPass (*PassRegistry::getPassRegistry ());
395+ RegisterCoalescerLegacy () : MachineFunctionPass(ID) {
396+ initializeRegisterCoalescerLegacyPass (*PassRegistry::getPassRegistry ());
382397 }
383398
384399 void getAnalysisUsage (AnalysisUsage &AU) const override ;
@@ -388,28 +403,30 @@ class RegisterCoalescer : public MachineFunctionPass,
388403 MachineFunctionProperties::Property::IsSSA);
389404 }
390405
391- void releaseMemory () override ;
406+ void releaseMemory () override { Impl-> releaseMemory (); }
392407
393408 // / This is the pass entry point.
394409 bool runOnMachineFunction (MachineFunction &) override ;
395410
396411 // / Implement the dump method.
397- void print (raw_ostream &O, const Module * = nullptr ) const override ;
412+ void print (raw_ostream &O, const Module * = nullptr ) const override {
413+ Impl->print (O, nullptr );
414+ }
398415};
399416
400417} // end anonymous namespace
401418
402- char RegisterCoalescer ::ID = 0 ;
419+ char RegisterCoalescerLegacy ::ID = 0 ;
403420
404- char &llvm::RegisterCoalescerID = RegisterCoalescer ::ID;
421+ char &llvm::RegisterCoalescerID = RegisterCoalescerLegacy ::ID;
405422
406- INITIALIZE_PASS_BEGIN (RegisterCoalescer , " register-coalescer" ,
423+ INITIALIZE_PASS_BEGIN (RegisterCoalescerLegacy , " register-coalescer" ,
407424 " Register Coalescer" , false , false )
408425INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
409426INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
410427INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
411428INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
412- INITIALIZE_PASS_END(RegisterCoalescer , " register-coalescer" ,
429+ INITIALIZE_PASS_END(RegisterCoalescerLegacy , " register-coalescer" ,
413430 " Register Coalescer" , false , false )
414431
415432[[nodiscard]] static bool isMoveInstr(const TargetRegisterInfo &tri,
@@ -586,8 +603,9 @@ bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
586603 }
587604}
588605
589- void RegisterCoalescer ::getAnalysisUsage (AnalysisUsage &AU) const {
606+ void RegisterCoalescerLegacy ::getAnalysisUsage (AnalysisUsage &AU) const {
590607 AU.setPreservesCFG ();
608+ AU.addUsedIfAvailable <SlotIndexesWrapperPass>();
591609 AU.addRequired <AAResultsWrapperPass>();
592610 AU.addRequired <LiveIntervalsWrapperPass>();
593611 AU.addPreserved <LiveIntervalsWrapperPass>();
@@ -4241,7 +4259,33 @@ void RegisterCoalescer::releaseMemory() {
42414259 LargeLIVisitCounter.clear ();
42424260}
42434261
4244- bool RegisterCoalescer::runOnMachineFunction (MachineFunction &fn) {
4262+ PreservedAnalyses
4263+ RegisterCoalescerPass::run (MachineFunction &MF,
4264+ MachineFunctionAnalysisManager &MFAM) {
4265+ auto &LIS = MFAM.getResult <LiveIntervalsAnalysis>(MF);
4266+ auto &Loops = MFAM.getResult <MachineLoopAnalysis>(MF);
4267+ auto *SI = MFAM.getCachedResult <SlotIndexesAnalysis>(MF);
4268+ RegisterCoalescer Impl (&LIS, SI, &Loops);
4269+ if (!Impl.run (MF))
4270+ return PreservedAnalyses::all ();
4271+ auto PA = getMachineFunctionPassPreservedAnalyses ();
4272+ PA.preserve <LiveIntervalsAnalysis>();
4273+ PA.preserve <SlotIndexesAnalysis>();
4274+ PA.preserve <MachineLoopAnalysis>();
4275+ PA.preserve <MachineDominatorTreeAnalysis>();
4276+ return PA;
4277+ }
4278+
4279+ bool RegisterCoalescerLegacy::runOnMachineFunction (MachineFunction &MF) {
4280+ auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4281+ auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
4282+ auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
4283+ SlotIndexes *SI = SIWrapper ? &SIWrapper->getSI () : nullptr ;
4284+ Impl.reset (new RegisterCoalescer (LIS, SI, Loops));
4285+ return Impl->run (MF);
4286+ }
4287+
4288+ bool RegisterCoalescer::run (MachineFunction &fn) {
42454289 LLVM_DEBUG (dbgs () << " ********** REGISTER COALESCER **********\n "
42464290 << " ********** Function: " << fn.getName () << ' \n ' );
42474291
@@ -4264,9 +4308,6 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
42644308 const TargetSubtargetInfo &STI = fn.getSubtarget ();
42654309 TRI = STI.getRegisterInfo ();
42664310 TII = STI.getInstrInfo ();
4267- LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4268- AA = &getAnalysis<AAResultsWrapperPass>().getAAResults ();
4269- Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
42704311 if (EnableGlobalCopies == cl::BOU_UNSET)
42714312 JoinGlobalCopies = STI.enableJoinGlobalCopies ();
42724313 else
@@ -4291,7 +4332,7 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
42914332 JoinSplitEdges = EnableJoinSplits;
42924333
42934334 if (VerifyCoalescing)
4294- MF->verify (this , " Before register coalescing" , &errs ());
4335+ MF->verify (LIS, SI , " Before register coalescing" , &errs ());
42954336
42964337 DbgVRegToValues.clear ();
42974338 buildVRegToDbgValueMap (fn);
@@ -4349,9 +4390,9 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
43494390 PHIValToPos.clear ();
43504391 RegToPHIIdx.clear ();
43514392
4352- LLVM_DEBUG (dump ( ));
4393+ LLVM_DEBUG (print ( dbgs (), nullptr ));
43534394 if (VerifyCoalescing)
4354- MF->verify (this , " After register coalescing" , &errs ());
4395+ MF->verify (LIS, SI , " After register coalescing" , &errs ());
43554396 return true ;
43564397}
43574398
0 commit comments