24
24
#include " llvm/CodeGen/LiveIntervals.h"
25
25
#include " llvm/CodeGen/LiveRangeEdit.h"
26
26
#include " llvm/CodeGen/MachineBasicBlock.h"
27
+ #include " llvm/CodeGen/MachineDominators.h"
27
28
#include " llvm/CodeGen/MachineFunction.h"
28
29
#include " llvm/CodeGen/MachineFunctionPass.h"
29
30
#include " llvm/CodeGen/MachineInstr.h"
30
31
#include " llvm/CodeGen/MachineInstrBuilder.h"
31
32
#include " llvm/CodeGen/MachineLoopInfo.h"
32
33
#include " llvm/CodeGen/MachineOperand.h"
34
+ #include " llvm/CodeGen/MachinePassManager.h"
33
35
#include " llvm/CodeGen/MachineRegisterInfo.h"
34
36
#include " llvm/CodeGen/Passes.h"
35
37
#include " llvm/CodeGen/RegisterClassInfo.h"
38
+ #include " llvm/CodeGen/RegisterCoalescerPass.h"
36
39
#include " llvm/CodeGen/SlotIndexes.h"
37
40
#include " llvm/CodeGen/TargetInstrInfo.h"
38
41
#include " llvm/CodeGen/TargetOpcodes.h"
@@ -121,13 +124,13 @@ namespace {
121
124
122
125
class JoinVals ;
123
126
124
- class RegisterCoalescer : public MachineFunctionPass ,
125
- private LiveRangeEdit::Delegate {
127
+ class RegisterCoalescer : private LiveRangeEdit ::Delegate {
126
128
MachineFunction *MF = nullptr ;
127
129
MachineRegisterInfo *MRI = nullptr ;
128
130
const TargetRegisterInfo *TRI = nullptr ;
129
131
const TargetInstrInfo *TII = nullptr ;
130
132
LiveIntervals *LIS = nullptr ;
133
+ SlotIndexes *SI = nullptr ;
131
134
const MachineLoopInfo *Loops = nullptr ;
132
135
RegisterClassInfo RegClassInfo;
133
136
@@ -372,11 +375,24 @@ class RegisterCoalescer : public MachineFunctionPass,
372
375
void checkMergingChangesDbgValuesImpl (Register Reg, LiveRange &OtherRange,
373
376
LiveRange &RegRange, JoinVals &Vals2);
374
377
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
+
375
391
public:
376
392
static char ID; // /< Class identification, replacement for typeinfo
377
393
378
- RegisterCoalescer () : MachineFunctionPass(ID) {
379
- initializeRegisterCoalescerPass (*PassRegistry::getPassRegistry ());
394
+ RegisterCoalescerLegacy () : MachineFunctionPass(ID) {
395
+ initializeRegisterCoalescerLegacyPass (*PassRegistry::getPassRegistry ());
380
396
}
381
397
382
398
void getAnalysisUsage (AnalysisUsage &AU) const override ;
@@ -386,27 +402,29 @@ class RegisterCoalescer : public MachineFunctionPass,
386
402
MachineFunctionProperties::Property::IsSSA);
387
403
}
388
404
389
- void releaseMemory () override ;
405
+ void releaseMemory () override { Impl-> releaseMemory (); }
390
406
391
407
// / This is the pass entry point.
392
408
bool runOnMachineFunction (MachineFunction &) override ;
393
409
394
410
// / 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
+ }
396
414
};
397
415
398
416
} // end anonymous namespace
399
417
400
- char RegisterCoalescer ::ID = 0 ;
418
+ char RegisterCoalescerLegacy ::ID = 0 ;
401
419
402
- char &llvm::RegisterCoalescerID = RegisterCoalescer ::ID;
420
+ char &llvm::RegisterCoalescerID = RegisterCoalescerLegacy ::ID;
403
421
404
- INITIALIZE_PASS_BEGIN (RegisterCoalescer , " register-coalescer" ,
422
+ INITIALIZE_PASS_BEGIN (RegisterCoalescerLegacy , " register-coalescer" ,
405
423
" Register Coalescer" , false , false )
406
424
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
407
425
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
408
426
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
409
- INITIALIZE_PASS_END(RegisterCoalescer , " register-coalescer" ,
427
+ INITIALIZE_PASS_END(RegisterCoalescerLegacy , " register-coalescer" ,
410
428
" Register Coalescer" , false , false )
411
429
412
430
[[nodiscard]] static bool isMoveInstr(const TargetRegisterInfo &tri,
@@ -583,8 +601,9 @@ bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
583
601
}
584
602
}
585
603
586
- void RegisterCoalescer ::getAnalysisUsage (AnalysisUsage &AU) const {
604
+ void RegisterCoalescerLegacy ::getAnalysisUsage (AnalysisUsage &AU) const {
587
605
AU.setPreservesCFG ();
606
+ AU.addUsedIfAvailable <SlotIndexesWrapperPass>();
588
607
AU.addRequired <LiveIntervalsWrapperPass>();
589
608
AU.addPreserved <LiveIntervalsWrapperPass>();
590
609
AU.addPreserved <SlotIndexesWrapperPass>();
@@ -4237,7 +4256,33 @@ void RegisterCoalescer::releaseMemory() {
4237
4256
LargeLIVisitCounter.clear ();
4238
4257
}
4239
4258
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) {
4241
4286
LLVM_DEBUG (dbgs () << " ********** REGISTER COALESCER **********\n "
4242
4287
<< " ********** Function: " << fn.getName () << ' \n ' );
4243
4288
@@ -4260,8 +4305,6 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
4260
4305
const TargetSubtargetInfo &STI = fn.getSubtarget ();
4261
4306
TRI = STI.getRegisterInfo ();
4262
4307
TII = STI.getInstrInfo ();
4263
- LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4264
- Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
4265
4308
if (EnableGlobalCopies == cl::BOU_UNSET)
4266
4309
JoinGlobalCopies = STI.enableJoinGlobalCopies ();
4267
4310
else
@@ -4286,7 +4329,7 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
4286
4329
JoinSplitEdges = EnableJoinSplits;
4287
4330
4288
4331
if (VerifyCoalescing)
4289
- MF->verify (this , " Before register coalescing" , &errs ());
4332
+ MF->verify (LIS, SI , " Before register coalescing" , &errs ());
4290
4333
4291
4334
DbgVRegToValues.clear ();
4292
4335
buildVRegToDbgValueMap (fn);
@@ -4344,9 +4387,9 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
4344
4387
PHIValToPos.clear ();
4345
4388
RegToPHIIdx.clear ();
4346
4389
4347
- LLVM_DEBUG (dump ( ));
4390
+ LLVM_DEBUG (print ( dbgs (), nullptr ));
4348
4391
if (VerifyCoalescing)
4349
- MF->verify (this , " After register coalescing" , &errs ());
4392
+ MF->verify (LIS, SI , " After register coalescing" , &errs ());
4350
4393
return true ;
4351
4394
}
4352
4395
0 commit comments