Skip to content

Commit 3bff611

Browse files
committed
[PHIElimination] Handle subranges in LiveInterval updates
Add handling for subrange updates in LiveInterval preservation. This requires extending MachineBasicBlock::SplitCriticalEdge to also update subrange intervals. Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D158144
1 parent 7ec8fd4 commit 3bff611

File tree

5 files changed

+80
-26
lines changed

5 files changed

+80
-26
lines changed

llvm/include/llvm/CodeGen/LiveIntervals.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ class VirtRegMap;
139139
return LI;
140140
}
141141

142+
LiveInterval &getOrCreateEmptyInterval(Register Reg) {
143+
return hasInterval(Reg) ? getInterval(Reg) : createEmptyInterval(Reg);
144+
}
145+
142146
/// Interval removal.
143147
void removeInterval(Register Reg) {
144148
delete VirtRegIntervals[Reg];

llvm/lib/CodeGen/LiveIntervals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ float LiveIntervals::getSpillWeight(bool isDef, bool isUse,
862862

863863
LiveRange::Segment
864864
LiveIntervals::addSegmentToEndOfBlock(Register Reg, MachineInstr &startInst) {
865-
LiveInterval &Interval = createEmptyInterval(Reg);
865+
LiveInterval &Interval = getOrCreateEmptyInterval(Reg);
866866
VNInfo *VN = Interval.getNextValue(
867867
SlotIndex(getInstructionIndex(startInst).getRegSlot()),
868868
getVNInfoAllocator());

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,8 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(
12751275
assert(VNI &&
12761276
"PHI sources should be live out of their predecessors.");
12771277
LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
1278+
for (auto &SR : LI.subranges())
1279+
SR.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
12781280
}
12791281
}
12801282
}
@@ -1294,8 +1296,18 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(
12941296
VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
12951297
assert(VNI && "LiveInterval should have VNInfo where it is live.");
12961298
LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
1299+
// Update subranges with live values
1300+
for (auto &SR : LI.subranges()) {
1301+
VNInfo *VNI = SR.getVNInfoAt(PrevIndex);
1302+
if (VNI)
1303+
SR.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
1304+
}
12971305
} else if (!isLiveOut && !isLastMBB) {
12981306
LI.removeSegment(StartIndex, EndIndex);
1307+
for (auto &SR : LI.subranges()) {
1308+
if (SR.overlaps(StartIndex, EndIndex))
1309+
SR.removeSegment(StartIndex, EndIndex);
1310+
}
12991311
}
13001312
}
13011313

llvm/lib/CodeGen/PHIElimination.cpp

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ INITIALIZE_PASS_END(PHIElimination, DEBUG_TYPE,
136136

137137
void PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
138138
AU.addUsedIfAvailable<LiveVariables>();
139+
AU.addUsedIfAvailable<LiveIntervals>();
139140
AU.addPreserved<LiveVariables>();
140141
AU.addPreserved<SlotIndexes>();
141142
AU.addPreserved<LiveIntervals>();
@@ -392,7 +393,7 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
392393
if (IncomingReg) {
393394
// Add the region from the beginning of MBB to the copy instruction to
394395
// IncomingReg's live interval.
395-
LiveInterval &IncomingLI = LIS->createEmptyInterval(IncomingReg);
396+
LiveInterval &IncomingLI = LIS->getOrCreateEmptyInterval(IncomingReg);
396397
VNInfo *IncomingVNI = IncomingLI.getVNInfoAt(MBBStartIndex);
397398
if (!IncomingVNI)
398399
IncomingVNI = IncomingLI.getNextValue(MBBStartIndex,
@@ -403,24 +404,49 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
403404
}
404405

405406
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);
422448
assert(DestVNI && "PHI destination should be live at its definition.");
423-
DestVNI->def = DestCopyIndex.getRegSlot();
449+
DestVNI->def = NewStart;
424450
}
425451
}
426452

@@ -615,6 +641,10 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
615641
SlotIndex LastUseIndex = LIS->getInstructionIndex(*KillInst);
616642
SrcLI.removeSegment(LastUseIndex.getRegSlot(),
617643
LIS->getMBBEndIdx(&opBlock));
644+
for (auto &SR : SrcLI.subranges()) {
645+
SR.removeSegment(LastUseIndex.getRegSlot(),
646+
LIS->getMBBEndIdx(&opBlock));
647+
}
618648
}
619649
}
620650
}

llvm/test/CodeGen/AMDGPU/split-mbb-lis-subrange.mir

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2
2-
# RUN: llc -march=amdgcn -mcpu=gfx1100 -verify-machineinstrs -run-pass liveintervals -o - %s | FileCheck -check-prefixes=GCN %s
2+
# RUN: llc -march=amdgcn -mcpu=gfx1100 -verify-machineinstrs -run-pass liveintervals,phi-node-elimination -o - %s | FileCheck -check-prefixes=GCN %s
33

4-
# This test simply checks that liveintervals pass verification.
4+
# This checks liveintervals pass verification and phi-node-elimination correctly preserves them.
55

66
---
77
name: split_critical_edge_subranges
88
tracksRegLiveness: true
99
body: |
1010
; GCN-LABEL: name: split_critical_edge_subranges
1111
; GCN: bb.0:
12-
; GCN-NEXT: successors: %bb.3(0x40000000), %bb.1(0x40000000)
12+
; GCN-NEXT: successors: %bb.5(0x40000000), %bb.1(0x40000000)
1313
; GCN-NEXT: {{ $}}
1414
; GCN-NEXT: %coord:vreg_64 = IMPLICIT_DEF
1515
; GCN-NEXT: %desc:sgpr_256 = IMPLICIT_DEF
@@ -20,14 +20,22 @@ body: |
2020
; GCN-NEXT: %s0a:vgpr_32 = COPY %load.sub0
2121
; GCN-NEXT: %s0b:vgpr_32 = COPY %load.sub1
2222
; GCN-NEXT: S_CMP_EQ_U32 %c0, %c1, implicit-def $scc
23-
; GCN-NEXT: S_CBRANCH_SCC1 %bb.3, implicit $scc
24-
; GCN-NEXT: S_BRANCH %bb.1
23+
; GCN-NEXT: S_CBRANCH_SCC0 %bb.1, implicit $scc
24+
; GCN-NEXT: {{ $}}
25+
; GCN-NEXT: bb.5:
26+
; GCN-NEXT: successors: %bb.3(0x80000000)
27+
; GCN-NEXT: {{ $}}
28+
; GCN-NEXT: [[COPY:%[0-9]+]]:vgpr_32 = COPY %s0a
29+
; GCN-NEXT: [[COPY1:%[0-9]+]]:vgpr_32 = COPY %s0b
30+
; GCN-NEXT: S_BRANCH %bb.3
2531
; GCN-NEXT: {{ $}}
2632
; GCN-NEXT: bb.1:
2733
; GCN-NEXT: successors: %bb.3(0x80000000)
2834
; GCN-NEXT: {{ $}}
2935
; GCN-NEXT: %s0c:vgpr_32 = V_ADD_F32_e64 0, %s0a, 0, %const, 0, 0, implicit $mode, implicit $exec
3036
; GCN-NEXT: %s0d:vgpr_32 = V_ADD_F32_e64 0, %s0b, 0, %const, 0, 0, implicit $mode, implicit $exec
37+
; GCN-NEXT: [[COPY2:%[0-9]+]]:vgpr_32 = COPY %s0c
38+
; GCN-NEXT: [[COPY3:%[0-9]+]]:vgpr_32 = COPY %s0d
3139
; GCN-NEXT: S_BRANCH %bb.3
3240
; GCN-NEXT: {{ $}}
3341
; GCN-NEXT: bb.2:
@@ -37,8 +45,8 @@ body: |
3745
; GCN-NEXT: bb.3:
3846
; GCN-NEXT: successors: %bb.4(0x80000000)
3947
; GCN-NEXT: {{ $}}
40-
; GCN-NEXT: %phi0:vgpr_32 = PHI %s0a, %bb.0, %s0c, %bb.1
41-
; GCN-NEXT: %phi1:vgpr_32 = PHI %s0b, %bb.0, %s0d, %bb.1
48+
; GCN-NEXT: %phi1:vgpr_32 = COPY [[COPY3]]
49+
; GCN-NEXT: %phi0:vgpr_32 = COPY [[COPY2]]
4250
; GCN-NEXT: S_BRANCH %bb.4
4351
; GCN-NEXT: {{ $}}
4452
; GCN-NEXT: bb.4:

0 commit comments

Comments
 (0)