@@ -136,6 +136,7 @@ INITIALIZE_PASS_END(PHIElimination, DEBUG_TYPE,
136
136
137
137
void PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
138
138
AU.addUsedIfAvailable <LiveVariables>();
139
+ AU.addUsedIfAvailable <LiveIntervals>();
139
140
AU.addPreserved <LiveVariables>();
140
141
AU.addPreserved <SlotIndexes>();
141
142
AU.addPreserved <LiveIntervals>();
@@ -389,7 +390,7 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
389
390
if (IncomingReg) {
390
391
// Add the region from the beginning of MBB to the copy instruction to
391
392
// IncomingReg's live interval.
392
- LiveInterval &IncomingLI = LIS->createEmptyInterval (IncomingReg);
393
+ LiveInterval &IncomingLI = LIS->getOrCreateEmptyInterval (IncomingReg);
393
394
VNInfo *IncomingVNI = IncomingLI.getVNInfoAt (MBBStartIndex);
394
395
if (!IncomingVNI)
395
396
IncomingVNI = IncomingLI.getNextValue (MBBStartIndex,
@@ -400,24 +401,50 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
400
401
}
401
402
402
403
LiveInterval &DestLI = LIS->getInterval (DestReg);
403
- assert (!DestLI.empty () && " PHIs should have nonempty LiveIntervals." );
404
- if (DestLI.endIndex ().isDead ()) {
405
- // A dead PHI's live range begins and ends at the start of the MBB, but
406
- // the lowered copy, which will still be dead, needs to begin and end at
407
- // the copy instruction.
408
- VNInfo *OrigDestVNI = DestLI.getVNInfoAt (MBBStartIndex);
409
- assert (OrigDestVNI && " PHI destination should be live at block entry." );
410
- DestLI.removeSegment (MBBStartIndex, MBBStartIndex.getDeadSlot ());
411
- DestLI.createDeadDef (DestCopyIndex.getRegSlot (),
412
- LIS->getVNInfoAllocator ());
413
- DestLI.removeValNo (OrigDestVNI);
414
- } else {
415
- // Otherwise, remove the region from the beginning of MBB to the copy
416
- // instruction from DestReg's live interval.
417
- DestLI.removeSegment (MBBStartIndex, DestCopyIndex.getRegSlot ());
418
- VNInfo *DestVNI = DestLI.getVNInfoAt (DestCopyIndex.getRegSlot ());
404
+ assert (!DestLI.empty () && " PHIs should have non-empty LiveIntervals." );
405
+
406
+ SlotIndex NewStart = DestCopyIndex.getRegSlot ();
407
+
408
+ SmallVector<LiveRange *> ToUpdate;
409
+ ToUpdate.push_back (&DestLI);
410
+ for (auto &SR : DestLI.subranges ())
411
+ ToUpdate.push_back (&SR);
412
+
413
+ for (auto LR : ToUpdate) {
414
+ auto DestSegment = LR->find (MBBStartIndex);
415
+ assert (DestSegment != LR->end () &&
416
+ " PHI destination must be live in block" );
417
+
418
+ if (LR->endIndex ().isDead ()) {
419
+ // A dead PHI's live range begins and ends at the start of the MBB, but
420
+ // the lowered copy, which will still be dead, needs to begin and end at
421
+ // the copy instruction.
422
+ VNInfo *OrigDestVNI = LR->getVNInfoAt (DestSegment->start );
423
+ assert (OrigDestVNI && " PHI destination should be live at block entry." );
424
+ LR->removeSegment (DestSegment->start , DestSegment->start .getDeadSlot ());
425
+ LR->createDeadDef (NewStart, LIS->getVNInfoAllocator ());
426
+ LR->removeValNo (OrigDestVNI);
427
+ continue ;
428
+ }
429
+
430
+ if (DestSegment->start > NewStart) {
431
+ // With a single PHI removed from block the index of the copy may be
432
+ // lower than the original PHI. Extend live range backward to cover
433
+ // the copy.
434
+ VNInfo *VNI = LR->getVNInfoAt (DestSegment->start );
435
+ assert (VNI && " value should be defined for known segment" );
436
+ LR->addSegment (
437
+ LiveInterval::Segment (NewStart, DestSegment->start , VNI));
438
+ } else if (DestSegment->start < NewStart) {
439
+ // Otherwise, remove the region from the beginning of MBB to the copy
440
+ // instruction from DestReg's live interval.
441
+ assert (DestSegment->start >= MBBStartIndex);
442
+ assert (DestSegment->end >= DestCopyIndex.getRegSlot ());
443
+ LR->removeSegment (DestSegment->start , NewStart);
444
+ }
445
+ VNInfo *DestVNI = LR->getVNInfoAt (NewStart);
419
446
assert (DestVNI && " PHI destination should be live at its definition." );
420
- DestVNI->def = DestCopyIndex. getRegSlot () ;
447
+ DestVNI->def = NewStart ;
421
448
}
422
449
}
423
450
@@ -612,6 +639,10 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
612
639
SlotIndex LastUseIndex = LIS->getInstructionIndex (*KillInst);
613
640
SrcLI.removeSegment (LastUseIndex.getRegSlot (),
614
641
LIS->getMBBEndIdx (&opBlock));
642
+ for (auto &SR : SrcLI.subranges ()) {
643
+ SR.removeSegment (LastUseIndex.getRegSlot (),
644
+ LIS->getMBBEndIdx (&opBlock));
645
+ }
615
646
}
616
647
}
617
648
}
0 commit comments