Skip to content

Commit

Permalink
[Coalescer] Move code added in llvm#116191 (llvm#121779)
Browse files Browse the repository at this point in the history
By moving the code a bit later, we can factor out some of the conditions
as those are now already tested.
This will also be useful when adding another fix on top that uses
`NewMI`'s subreg index (to follow as a separate PR).

The change is intended to be NFC.
  • Loading branch information
sdesmalen-arm authored Jan 7, 2025
1 parent b0e05a5 commit 5514865
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions llvm/lib/CodeGen/RegisterCoalescer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1395,27 +1395,6 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
MachineInstr &NewMI = *std::prev(MII);
NewMI.setDebugLoc(DL);

// In a situation like the following:
//
// undef %2.subreg:reg = INST %1:reg ; DefMI (rematerializable),
// ; DefSubIdx = subreg
// %3:reg = COPY %2 ; SrcIdx = DstIdx = 0
// .... = SOMEINSTR %3:reg
//
// there are no subranges for %3 so after rematerialization we need
// to explicitly create them. Undefined subranges are removed later on.
if (DstReg.isVirtual() && DefSubIdx && !CP.getSrcIdx() && !CP.getDstIdx() &&
MRI->shouldTrackSubRegLiveness(DstReg)) {
LiveInterval &DstInt = LIS->getInterval(DstReg);
if (!DstInt.hasSubRanges()) {
LaneBitmask FullMask = MRI->getMaxLaneMaskForVReg(DstReg);
LaneBitmask UsedLanes = TRI->getSubRegIndexLaneMask(DefSubIdx);
LaneBitmask UnusedLanes = FullMask & ~UsedLanes;
DstInt.createSubRangeFrom(LIS->getVNInfoAllocator(), UsedLanes, DstInt);
DstInt.createSubRangeFrom(LIS->getVNInfoAllocator(), UnusedLanes, DstInt);
}
}

// In a situation like the following:
// %0:subreg = instr ; DefMI, subreg = DstIdx
// %1 = copy %0:subreg ; CopyMI, SrcIdx = 0
Expand Down Expand Up @@ -1544,6 +1523,26 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
// sure that "undef" is not set.
if (NewIdx == 0)
NewMI.getOperand(0).setIsUndef(false);

// In a situation like the following:
//
// undef %2.subreg:reg = INST %1:reg ; DefMI (rematerializable),
// ; DefSubIdx = subreg
// %3:reg = COPY %2 ; SrcIdx = DstIdx = 0
// .... = SOMEINSTR %3:reg
//
// there are no subranges for %3 so after rematerialization we need
// to explicitly create them. Undefined subranges are removed later on.
if (DefSubIdx && !CP.getSrcIdx() && !CP.getDstIdx() &&
MRI->shouldTrackSubRegLiveness(DstReg) && !DstInt.hasSubRanges()) {
LaneBitmask FullMask = MRI->getMaxLaneMaskForVReg(DstReg);
LaneBitmask UsedLanes = TRI->getSubRegIndexLaneMask(DefSubIdx);
LaneBitmask UnusedLanes = FullMask & ~UsedLanes;
VNInfo::Allocator &Alloc = LIS->getVNInfoAllocator();
DstInt.createSubRangeFrom(Alloc, UsedLanes, DstInt);
DstInt.createSubRangeFrom(Alloc, UnusedLanes, DstInt);
}

// Add dead subregister definitions if we are defining the whole register
// but only part of it is live.
// This could happen if the rematerialization instruction is rematerializing
Expand Down

0 comments on commit 5514865

Please sign in to comment.