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