25
25
#include " llvm/CodeGen/LiveIntervals.h"
26
26
#include " llvm/CodeGen/LiveRangeEdit.h"
27
27
#include " llvm/CodeGen/MachineBasicBlock.h"
28
+ #include " llvm/CodeGen/MachineDominators.h"
28
29
#include " llvm/CodeGen/MachineFunction.h"
29
30
#include " llvm/CodeGen/MachineFunctionPass.h"
30
31
#include " llvm/CodeGen/MachineInstr.h"
31
32
#include " llvm/CodeGen/MachineInstrBuilder.h"
32
33
#include " llvm/CodeGen/MachineLoopInfo.h"
33
34
#include " llvm/CodeGen/MachineOperand.h"
35
+ #include " llvm/CodeGen/MachinePassManager.h"
34
36
#include " llvm/CodeGen/MachineRegisterInfo.h"
35
37
#include " llvm/CodeGen/Passes.h"
36
38
#include " llvm/CodeGen/RegisterClassInfo.h"
39
+ #include " llvm/CodeGen/RegisterCoalescerPass.h"
37
40
#include " llvm/CodeGen/SlotIndexes.h"
38
41
#include " llvm/CodeGen/TargetInstrInfo.h"
39
42
#include " llvm/CodeGen/TargetOpcodes.h"
@@ -122,15 +125,14 @@ namespace {
122
125
123
126
class JoinVals ;
124
127
125
- class RegisterCoalescer : public MachineFunctionPass ,
126
- private LiveRangeEdit::Delegate {
128
+ class RegisterCoalescer : private LiveRangeEdit ::Delegate {
127
129
MachineFunction *MF = nullptr ;
128
130
MachineRegisterInfo *MRI = nullptr ;
129
131
const TargetRegisterInfo *TRI = nullptr ;
130
132
const TargetInstrInfo *TII = nullptr ;
131
133
LiveIntervals *LIS = nullptr ;
134
+ SlotIndexes *SI = nullptr ;
132
135
const MachineLoopInfo *Loops = nullptr ;
133
- AliasAnalysis *AA = nullptr ;
134
136
RegisterClassInfo RegClassInfo;
135
137
136
138
// / Position and VReg of a PHI instruction during coalescing.
@@ -374,11 +376,24 @@ class RegisterCoalescer : public MachineFunctionPass,
374
376
void checkMergingChangesDbgValuesImpl (Register Reg, LiveRange &OtherRange,
375
377
LiveRange &RegRange, JoinVals &Vals2);
376
378
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
+
377
392
public:
378
393
static char ID; // /< Class identification, replacement for typeinfo
379
394
380
- RegisterCoalescer () : MachineFunctionPass(ID) {
381
- initializeRegisterCoalescerPass (*PassRegistry::getPassRegistry ());
395
+ RegisterCoalescerLegacy () : MachineFunctionPass(ID) {
396
+ initializeRegisterCoalescerLegacyPass (*PassRegistry::getPassRegistry ());
382
397
}
383
398
384
399
void getAnalysisUsage (AnalysisUsage &AU) const override ;
@@ -388,28 +403,30 @@ class RegisterCoalescer : public MachineFunctionPass,
388
403
MachineFunctionProperties::Property::IsSSA);
389
404
}
390
405
391
- void releaseMemory () override ;
406
+ void releaseMemory () override { Impl-> releaseMemory (); }
392
407
393
408
// / This is the pass entry point.
394
409
bool runOnMachineFunction (MachineFunction &) override ;
395
410
396
411
// / 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
+ }
398
415
};
399
416
400
417
} // end anonymous namespace
401
418
402
- char RegisterCoalescer ::ID = 0 ;
419
+ char RegisterCoalescerLegacy ::ID = 0 ;
403
420
404
- char &llvm::RegisterCoalescerID = RegisterCoalescer ::ID;
421
+ char &llvm::RegisterCoalescerID = RegisterCoalescerLegacy ::ID;
405
422
406
- INITIALIZE_PASS_BEGIN (RegisterCoalescer , " register-coalescer" ,
423
+ INITIALIZE_PASS_BEGIN (RegisterCoalescerLegacy , " register-coalescer" ,
407
424
" Register Coalescer" , false , false )
408
425
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
409
426
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
410
427
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
411
428
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
412
- INITIALIZE_PASS_END(RegisterCoalescer , " register-coalescer" ,
429
+ INITIALIZE_PASS_END(RegisterCoalescerLegacy , " register-coalescer" ,
413
430
" Register Coalescer" , false , false )
414
431
415
432
[[nodiscard]] static bool isMoveInstr(const TargetRegisterInfo &tri,
@@ -586,8 +603,9 @@ bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
586
603
}
587
604
}
588
605
589
- void RegisterCoalescer ::getAnalysisUsage (AnalysisUsage &AU) const {
606
+ void RegisterCoalescerLegacy ::getAnalysisUsage (AnalysisUsage &AU) const {
590
607
AU.setPreservesCFG ();
608
+ AU.addUsedIfAvailable <SlotIndexesWrapperPass>();
591
609
AU.addRequired <AAResultsWrapperPass>();
592
610
AU.addRequired <LiveIntervalsWrapperPass>();
593
611
AU.addPreserved <LiveIntervalsWrapperPass>();
@@ -4241,7 +4259,33 @@ void RegisterCoalescer::releaseMemory() {
4241
4259
LargeLIVisitCounter.clear ();
4242
4260
}
4243
4261
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) {
4245
4289
LLVM_DEBUG (dbgs () << " ********** REGISTER COALESCER **********\n "
4246
4290
<< " ********** Function: " << fn.getName () << ' \n ' );
4247
4291
@@ -4264,9 +4308,6 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
4264
4308
const TargetSubtargetInfo &STI = fn.getSubtarget ();
4265
4309
TRI = STI.getRegisterInfo ();
4266
4310
TII = STI.getInstrInfo ();
4267
- LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
4268
- AA = &getAnalysis<AAResultsWrapperPass>().getAAResults ();
4269
- Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
4270
4311
if (EnableGlobalCopies == cl::BOU_UNSET)
4271
4312
JoinGlobalCopies = STI.enableJoinGlobalCopies ();
4272
4313
else
@@ -4291,7 +4332,7 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
4291
4332
JoinSplitEdges = EnableJoinSplits;
4292
4333
4293
4334
if (VerifyCoalescing)
4294
- MF->verify (this , " Before register coalescing" , &errs ());
4335
+ MF->verify (LIS, SI , " Before register coalescing" , &errs ());
4295
4336
4296
4337
DbgVRegToValues.clear ();
4297
4338
buildVRegToDbgValueMap (fn);
@@ -4349,9 +4390,9 @@ bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) {
4349
4390
PHIValToPos.clear ();
4350
4391
RegToPHIIdx.clear ();
4351
4392
4352
- LLVM_DEBUG (dump ( ));
4393
+ LLVM_DEBUG (print ( dbgs (), nullptr ));
4353
4394
if (VerifyCoalescing)
4354
- MF->verify (this , " After register coalescing" , &errs ());
4395
+ MF->verify (LIS, SI , " After register coalescing" , &errs ());
4355
4396
return true ;
4356
4397
}
4357
4398
0 commit comments