Skip to content

Commit ed5bd23

Browse files
committed
Revert "Reland "RegisterCoalescer: Add implicit-def of super register when coalescing SUBREG_TO_REG" (#134408)"
This reverts commit bae8f13. Some issues were found: * #151768 * #151592 * #134408 (comment) * #151888 (comment) I'll revert this for the time being while I investigate.
1 parent f1eb869 commit ed5bd23

25 files changed

+72
-1418
lines changed

llvm/lib/CodeGen/RegisterCoalescer.cpp

Lines changed: 15 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,7 @@ class RegisterCoalescer : private LiveRangeEdit::Delegate {
306306
/// number if it is not zero. If DstReg is a physical register and the
307307
/// existing subregister number of the def / use being updated is not zero,
308308
/// make sure to set it to the correct physical subregister.
309-
///
310-
/// If \p SubregToRegSrcInst is not empty, we are coalescing a
311-
/// `DstReg = SUBREG_TO_REG SrcReg`, which should introduce an
312-
/// implicit-def of DstReg on instructions that define SrcReg.
313-
void updateRegDefsUses(Register SrcReg, Register DstReg, unsigned SubIdx,
314-
ArrayRef<MachineInstr *> SubregToRegSrcInst = {});
309+
void updateRegDefsUses(Register SrcReg, Register DstReg, unsigned SubIdx);
315310

316311
/// If the given machine operand reads only undefined lanes add an undef
317312
/// flag.
@@ -1448,7 +1443,6 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
14481443

14491444
// CopyMI may have implicit operands, save them so that we can transfer them
14501445
// over to the newly materialized instruction after CopyMI is removed.
1451-
LaneBitmask NewMIImplicitOpsMask;
14521446
SmallVector<MachineOperand, 4> ImplicitOps;
14531447
ImplicitOps.reserve(CopyMI->getNumOperands() -
14541448
CopyMI->getDesc().getNumOperands());
@@ -1463,9 +1457,6 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
14631457
(MO.getSubReg() == 0 && MO.getReg() == DstOperand.getReg())) &&
14641458
"unexpected implicit virtual register def");
14651459
ImplicitOps.push_back(MO);
1466-
if (MO.isDef() && MO.getReg().isVirtual() &&
1467-
MRI->shouldTrackSubRegLiveness(DstReg))
1468-
NewMIImplicitOpsMask |= MRI->getMaxLaneMaskForVReg(MO.getReg());
14691460
}
14701461
}
14711462

@@ -1508,11 +1499,14 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
15081499
} else {
15091500
assert(MO.getReg() == NewMI.getOperand(0).getReg());
15101501

1511-
// If lanemasks need to be tracked, compile the lanemask of the NewMI
1512-
// implicit def operands to avoid subranges for the super-regs from
1513-
// being removed by code later on in this function.
1514-
if (MRI->shouldTrackSubRegLiveness(MO.getReg()))
1515-
NewMIImplicitOpsMask |= MRI->getMaxLaneMaskForVReg(MO.getReg());
1502+
// We're only expecting another def of the main output, so the range
1503+
// should get updated with the regular output range.
1504+
//
1505+
// FIXME: The range updating below probably needs updating to look at
1506+
// the super register if subranges are tracked.
1507+
assert(!MRI->shouldTrackSubRegLiveness(DstReg) &&
1508+
"subrange update for implicit-def of super register may not be "
1509+
"properly handled");
15161510
}
15171511
}
15181512
}
@@ -1612,8 +1606,7 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
16121606
CurrIdx.getRegSlot(NewMI.getOperand(0).isEarlyClobber());
16131607
VNInfo::Allocator &Alloc = LIS->getVNInfoAllocator();
16141608
for (LiveInterval::SubRange &SR : DstInt.subranges()) {
1615-
if ((SR.LaneMask & DstMask).none() &&
1616-
(SR.LaneMask & NewMIImplicitOpsMask).none()) {
1609+
if ((SR.LaneMask & DstMask).none()) {
16171610
LLVM_DEBUG(dbgs()
16181611
<< "Removing undefined SubRange "
16191612
<< PrintLaneMask(SR.LaneMask) << " : " << SR << "\n");
@@ -1877,14 +1870,11 @@ void RegisterCoalescer::addUndefFlag(const LiveInterval &Int, SlotIndex UseIdx,
18771870
}
18781871
}
18791872

1880-
void RegisterCoalescer::updateRegDefsUses(
1881-
Register SrcReg, Register DstReg, unsigned SubIdx,
1882-
ArrayRef<MachineInstr *> SubregToRegSrcInsts) {
1873+
void RegisterCoalescer::updateRegDefsUses(Register SrcReg, Register DstReg,
1874+
unsigned SubIdx) {
18831875
bool DstIsPhys = DstReg.isPhysical();
18841876
LiveInterval *DstInt = DstIsPhys ? nullptr : &LIS->getInterval(DstReg);
18851877

1886-
// Coalescing a COPY may expose reads of 'undef' subregisters.
1887-
// If so, then explicitly propagate 'undef' to those operands.
18881878
if (DstInt && DstInt->hasSubRanges() && DstReg != SrcReg) {
18891879
for (MachineOperand &MO : MRI->reg_operands(DstReg)) {
18901880
if (MO.isUndef())
@@ -1901,15 +1891,6 @@ void RegisterCoalescer::updateRegDefsUses(
19011891
}
19021892
}
19031893

1904-
// If DstInt already has a subrange for the unused lanes, then we shouldn't
1905-
// create duplicate subranges when we update the interval for unused lanes.
1906-
LaneBitmask DstIntLaneMask;
1907-
if (DstInt && MRI->shouldTrackSubRegLiveness(DstReg)) {
1908-
for (LiveInterval::SubRange &SR : DstInt->subranges())
1909-
DstIntLaneMask |= SR.LaneMask;
1910-
}
1911-
1912-
// Go through all instructions to replace uses of 'SrcReg' by 'DstReg'.
19131894
SmallPtrSet<MachineInstr *, 8> Visited;
19141895
for (MachineRegisterInfo::reg_instr_iterator I = MRI->reg_instr_begin(SrcReg),
19151896
E = MRI->reg_instr_end();
@@ -1933,80 +1914,6 @@ void RegisterCoalescer::updateRegDefsUses(
19331914
if (DstInt && !Reads && SubIdx && !UseMI->isDebugInstr())
19341915
Reads = DstInt->liveAt(LIS->getInstructionIndex(*UseMI));
19351916

1936-
bool RequiresImplicitRedef = false;
1937-
if (!SubregToRegSrcInsts.empty()) {
1938-
// We can only add an implicit-def and undef if the sub registers match,
1939-
// e.g.
1940-
// %0:gr32 = INSTX
1941-
// %0.sub8:gr32 = INSTY // top 24 bits of %0 still defined
1942-
// %1:gr64 = SUBREG_TO_REG 0, %0, %subreg.sub32
1943-
//
1944-
// This cannot be transformed into:
1945-
// %1.sub32:gr64 = INSTX
1946-
// undef %1.sub8:gr64 = INSTY , implicit-def %1
1947-
//
1948-
// Because that would thrash the top 24 bits of %1.sub32.
1949-
if (is_contained(SubregToRegSrcInsts, UseMI) &&
1950-
all_of(UseMI->defs(),
1951-
[&SubIdx, &SrcReg](const MachineOperand &MO) -> bool {
1952-
if (MO.getReg() != SrcReg || !MO.getSubReg() || MO.isUndef())
1953-
return true;
1954-
return SubIdx == MO.getSubReg();
1955-
})) {
1956-
// Add implicit-def of super-register to express that the whole
1957-
// register is defined by the instruction.
1958-
MachineInstrBuilder MIB(*MF, UseMI);
1959-
MIB.addReg(DstReg, RegState::ImplicitDefine);
1960-
RequiresImplicitRedef = true;
1961-
}
1962-
1963-
// If the coalesed instruction doesn't fully define the register, we need
1964-
// to preserve the original super register liveness for SUBREG_TO_REG.
1965-
//
1966-
// We pretended SUBREG_TO_REG was a regular copy for coalescing purposes,
1967-
// but it introduces liveness for other subregisters. Downstream users may
1968-
// have been relying on those bits, so we need to ensure their liveness is
1969-
// captured with a def of other lanes.
1970-
if (DstInt && MRI->shouldTrackSubRegLiveness(DstReg)) {
1971-
// First check if there is sufficient granularity in terms of subranges.
1972-
LaneBitmask DstMask = MRI->getMaxLaneMaskForVReg(DstInt->reg());
1973-
LaneBitmask UsedLanes = TRI->getSubRegIndexLaneMask(SubIdx);
1974-
LaneBitmask UnusedLanes = DstMask & ~UsedLanes;
1975-
if ((UnusedLanes & ~DstIntLaneMask).any()) {
1976-
BumpPtrAllocator &Allocator = LIS->getVNInfoAllocator();
1977-
DstInt->createSubRangeFrom(Allocator, UnusedLanes, *DstInt);
1978-
DstIntLaneMask |= UnusedLanes;
1979-
}
1980-
1981-
// After duplicating the live ranges for the low/hi bits, we
1982-
// need to update the subranges of the DstReg interval such that
1983-
// for a case like this:
1984-
//
1985-
// entry:
1986-
// 16B %1:gpr32 = INSTRUCTION (<=> UseMI)
1987-
// :
1988-
// if.then:
1989-
// 32B %1:gpr32 = MOVIMM32 ..
1990-
// 48B %0:gpr64 = SUBREG_TO_REG 0, %1, sub32
1991-
//
1992-
// Only the MOVIMM32 require a def of the top lanes and any intervals
1993-
// for the top 32-bits of the def at 16B should be removed.
1994-
for (LiveInterval::SubRange &SR : DstInt->subranges()) {
1995-
if (!Writes || RequiresImplicitRedef ||
1996-
(SR.LaneMask & UnusedLanes).none())
1997-
continue;
1998-
1999-
assert((SR.LaneMask & UnusedLanes) == SR.LaneMask &&
2000-
"Unexpected lanemask. Subrange needs finer granularity");
2001-
2002-
SlotIndex UseIdx = LIS->getInstructionIndex(*UseMI).getRegSlot(false);
2003-
auto SegmentI = SR.find(UseIdx);
2004-
if (SegmentI != SR.end())
2005-
SR.removeSegment(SegmentI, true);
2006-
}
2007-
}
2008-
}
2009-
20101917
// Replace SrcReg with DstReg in all UseMI operands.
20111918
for (unsigned Op : Ops) {
20121919
MachineOperand &MO = UseMI->getOperand(Op);
@@ -2015,7 +1922,7 @@ void RegisterCoalescer::updateRegDefsUses(
20151922
// turn a full def into a read-modify-write sub-register def and vice
20161923
// versa.
20171924
if (SubIdx && MO.isDef())
2018-
MO.setIsUndef(!Reads || RequiresImplicitRedef);
1925+
MO.setIsUndef(!Reads);
20191926

20201927
// A subreg use of a partially undef (super) register may be a complete
20211928
// undef use now and then has to be marked that way.
@@ -2118,30 +2025,6 @@ void RegisterCoalescer::setUndefOnPrunedSubRegUses(LiveInterval &LI,
21182025
LIS->shrinkToUses(&LI);
21192026
}
21202027

2121-
/// For a given use of value \p Idx, it returns the def in the current block,
2122-
/// or otherwise all possible defs in preceding blocks.
2123-
static bool FindDefInBlock(SmallPtrSetImpl<MachineBasicBlock *> &VisitedBlocks,
2124-
SmallVector<MachineInstr *> &Instrs,
2125-
LiveIntervals *LIS, LiveInterval &SrcInt,
2126-
MachineBasicBlock *MBB, VNInfo *Idx) {
2127-
if (!Idx->isPHIDef()) {
2128-
MachineInstr *Def = LIS->getInstructionFromIndex(Idx->def);
2129-
assert(Def && "Unable to find a def for SUBREG_TO_REG source operand");
2130-
Instrs.push_back(Def);
2131-
return true;
2132-
}
2133-
2134-
bool Any = false;
2135-
if (VisitedBlocks.count(MBB))
2136-
return false;
2137-
VisitedBlocks.insert(MBB);
2138-
for (MachineBasicBlock *Pred : MBB->predecessors()) {
2139-
Any |= FindDefInBlock(VisitedBlocks, Instrs, LIS, SrcInt, Pred,
2140-
SrcInt.getVNInfoBefore(LIS->getMBBEndIdx(Pred)));
2141-
}
2142-
return Any;
2143-
}
2144-
21452028
bool RegisterCoalescer::joinCopy(
21462029
MachineInstr *CopyMI, bool &Again,
21472030
SmallPtrSetImpl<MachineInstr *> &CurrentErasedInstrs) {
@@ -2273,35 +2156,6 @@ bool RegisterCoalescer::joinCopy(
22732156
});
22742157
}
22752158

2276-
SmallVector<MachineInstr *> SubregToRegSrcInsts;
2277-
if (CopyMI->isSubregToReg()) {
2278-
// For the case where the copy instruction is a SUBREG_TO_REG, e.g.
2279-
//
2280-
// %0:gpr32 = movimm32 ..
2281-
// %1:gpr64 = SUBREG_TO_REG 0, %0, sub32
2282-
// ...
2283-
// %0:gpr32 = COPY <something>
2284-
//
2285-
// After joining liveranges, the original `movimm32` will need an
2286-
// implicit-def to make it explicit that the entire register is written,
2287-
// i.e.
2288-
//
2289-
// undef %0.sub32:gpr64 = movimm32 ..., implicit-def %0
2290-
// ...
2291-
// undef %0.sub32:gpr64 = COPY <something> // Note that this does not
2292-
// // require an implicit-def,
2293-
// // because it has nothing to
2294-
// // do with the SUBREG_TO_REG.
2295-
LiveInterval &SrcInt =
2296-
LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
2297-
SlotIndex SubregToRegSlotIdx = LIS->getInstructionIndex(*CopyMI);
2298-
SmallPtrSet<MachineBasicBlock *, 8> VisitedBlocks;
2299-
if (!FindDefInBlock(VisitedBlocks, SubregToRegSrcInsts, LIS, SrcInt,
2300-
CopyMI->getParent(),
2301-
SrcInt.Query(SubregToRegSlotIdx).valueIn()))
2302-
llvm_unreachable("SUBREG_TO_REG src requires a def");
2303-
}
2304-
23052159
ShrinkMask = LaneBitmask::getNone();
23062160
ShrinkMainRange = false;
23072161

@@ -2371,12 +2225,9 @@ bool RegisterCoalescer::joinCopy(
23712225

23722226
// Rewrite all SrcReg operands to DstReg.
23732227
// Also update DstReg operands to include DstIdx if it is set.
2374-
if (CP.getDstIdx()) {
2375-
assert(SubregToRegSrcInsts.empty() && "can this happen?");
2228+
if (CP.getDstIdx())
23762229
updateRegDefsUses(CP.getDstReg(), CP.getDstReg(), CP.getDstIdx());
2377-
}
2378-
updateRegDefsUses(CP.getSrcReg(), CP.getDstReg(), CP.getSrcIdx(),
2379-
SubregToRegSrcInsts);
2230+
updateRegDefsUses(CP.getSrcReg(), CP.getDstReg(), CP.getSrcIdx());
23802231

23812232
// Shrink subregister ranges if necessary.
23822233
if (ShrinkMask.any()) {

0 commit comments

Comments
 (0)