Skip to content

Commit ba385ae

Browse files
committed
Reapply "RegisterCoalescer: Add implicit-def of super register when coalescing SUBREG_TO_REG"
This reverts commit e0f86ca. This was hitting some assertions which have since been relaxed.
1 parent d275277 commit ba385ae

5 files changed

+668
-55
lines changed

llvm/lib/CodeGen/RegisterCoalescer.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,11 @@ namespace {
305305
/// number if it is not zero. If DstReg is a physical register and the
306306
/// existing subregister number of the def / use being updated is not zero,
307307
/// make sure to set it to the correct physical subregister.
308-
void updateRegDefsUses(Register SrcReg, Register DstReg, unsigned SubIdx);
308+
///
309+
/// If \p IsSubregToReg, we are coalescing a DstReg = SUBREG_TO_REG
310+
/// SrcReg. This introduces an implicit-def of DstReg on coalesced users.
311+
void updateRegDefsUses(Register SrcReg, Register DstReg, unsigned SubIdx,
312+
bool IsSubregToReg);
309313

310314
/// If the given machine operand reads only undefined lanes add an undef
311315
/// flag.
@@ -1323,8 +1327,7 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
13231327
if (DstReg.isPhysical()) {
13241328
Register NewDstReg = DstReg;
13251329

1326-
unsigned NewDstIdx = TRI->composeSubRegIndices(CP.getSrcIdx(),
1327-
DefMI->getOperand(0).getSubReg());
1330+
unsigned NewDstIdx = TRI->composeSubRegIndices(CP.getSrcIdx(), DefSubIdx);
13281331
if (NewDstIdx)
13291332
NewDstReg = TRI->getSubReg(DstReg, NewDstIdx);
13301333

@@ -1473,7 +1476,7 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
14731476
MRI->setRegClass(DstReg, NewRC);
14741477

14751478
// Update machine operands and add flags.
1476-
updateRegDefsUses(DstReg, DstReg, DstIdx);
1479+
updateRegDefsUses(DstReg, DstReg, DstIdx, false);
14771480
NewMI.getOperand(0).setSubReg(NewIdx);
14781481
// updateRegDefUses can add an "undef" flag to the definition, since
14791482
// it will replace DstReg with DstReg.DstIdx. If NewIdx is 0, make
@@ -1788,7 +1791,7 @@ void RegisterCoalescer::addUndefFlag(const LiveInterval &Int, SlotIndex UseIdx,
17881791
}
17891792

17901793
void RegisterCoalescer::updateRegDefsUses(Register SrcReg, Register DstReg,
1791-
unsigned SubIdx) {
1794+
unsigned SubIdx, bool IsSubregToReg) {
17921795
bool DstIsPhys = DstReg.isPhysical();
17931796
LiveInterval *DstInt = DstIsPhys ? nullptr : &LIS->getInterval(DstReg);
17941797

@@ -1828,16 +1831,22 @@ void RegisterCoalescer::updateRegDefsUses(Register SrcReg, Register DstReg,
18281831
if (DstInt && !Reads && SubIdx && !UseMI->isDebugInstr())
18291832
Reads = DstInt->liveAt(LIS->getInstructionIndex(*UseMI));
18301833

1834+
bool FullDef = true;
1835+
18311836
// Replace SrcReg with DstReg in all UseMI operands.
18321837
for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
18331838
MachineOperand &MO = UseMI->getOperand(Ops[i]);
18341839

18351840
// Adjust <undef> flags in case of sub-register joins. We don't want to
18361841
// turn a full def into a read-modify-write sub-register def and vice
18371842
// versa.
1838-
if (SubIdx && MO.isDef())
1843+
if (SubIdx && MO.isDef()) {
18391844
MO.setIsUndef(!Reads);
18401845

1846+
if (!Reads)
1847+
FullDef = false;
1848+
}
1849+
18411850
// A subreg use of a partially undef (super) register may be a complete
18421851
// undef use now and then has to be marked that way.
18431852
if (MO.isUse() && !DstIsPhys) {
@@ -1869,6 +1878,25 @@ void RegisterCoalescer::updateRegDefsUses(Register SrcReg, Register DstReg,
18691878
MO.substVirtReg(DstReg, SubIdx, *TRI);
18701879
}
18711880

1881+
if (IsSubregToReg && !FullDef) {
1882+
// If the coalesed instruction doesn't fully define the register, we need
1883+
// to preserve the original super register liveness for SUBREG_TO_REG.
1884+
//
1885+
// We pretended SUBREG_TO_REG was a regular copy for coalescing purposes,
1886+
// but it introduces liveness for other subregisters. Downstream users may
1887+
// have been relying on those bits, so we need to ensure their liveness is
1888+
// captured with a def of other lanes.
1889+
1890+
// FIXME: Need to add new subrange if tracking subranges. We could also
1891+
// skip adding this if we knew the other lanes are dead, and only for
1892+
// other lanes.
1893+
1894+
assert(!MRI->shouldTrackSubRegLiveness(DstReg) &&
1895+
"this should update subranges");
1896+
MachineInstrBuilder MIB(*MF, UseMI);
1897+
MIB.addReg(DstReg, RegState::ImplicitDefine);
1898+
}
1899+
18721900
LLVM_DEBUG({
18731901
dbgs() << "\t\tupdated: ";
18741902
if (!UseMI->isDebugInstr())
@@ -2068,6 +2096,8 @@ bool RegisterCoalescer::joinCopy(MachineInstr *CopyMI, bool &Again) {
20682096
});
20692097
}
20702098

2099+
const bool IsSubregToReg = CopyMI->isSubregToReg();
2100+
20712101
ShrinkMask = LaneBitmask::getNone();
20722102
ShrinkMainRange = false;
20732103

@@ -2135,9 +2165,12 @@ bool RegisterCoalescer::joinCopy(MachineInstr *CopyMI, bool &Again) {
21352165

21362166
// Rewrite all SrcReg operands to DstReg.
21372167
// Also update DstReg operands to include DstIdx if it is set.
2138-
if (CP.getDstIdx())
2139-
updateRegDefsUses(CP.getDstReg(), CP.getDstReg(), CP.getDstIdx());
2140-
updateRegDefsUses(CP.getSrcReg(), CP.getDstReg(), CP.getSrcIdx());
2168+
if (CP.getDstIdx()) {
2169+
assert(!IsSubregToReg && "can this happen?");
2170+
updateRegDefsUses(CP.getDstReg(), CP.getDstReg(), CP.getDstIdx(), false);
2171+
}
2172+
updateRegDefsUses(CP.getSrcReg(), CP.getDstReg(), CP.getSrcIdx(),
2173+
IsSubregToReg);
21412174

21422175
// Shrink subregister ranges if necessary.
21432176
if (ShrinkMask.any()) {

0 commit comments

Comments
 (0)