Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions llvm/lib/CodeGen/RegisterCoalescer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1474,28 +1474,22 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
//
// The implicit-def of the super register may have been reduced to
// subregisters depending on the uses.

bool NewMIDefinesFullReg = false;

SmallVector<MCRegister, 4> NewMIImplDefs;
SmallVector<std::pair<unsigned, Register>, 4> NewMIImplDefs;
for (unsigned i = NewMI.getDesc().getNumOperands(),
e = NewMI.getNumOperands();
i != e; ++i) {
MachineOperand &MO = NewMI.getOperand(i);
if (MO.isReg() && MO.isDef()) {
assert(MO.isImplicit());
if (MO.getReg().isPhysical()) {
if (MO.getReg() == DstReg)
NewMIDefinesFullReg = true;

assert(MO.isImplicit() && MO.getReg().isPhysical() &&
(MO.isDead() ||
(DefSubIdx &&
((TRI->getSubReg(MO.getReg(), DefSubIdx) ==
MCRegister((unsigned)NewMI.getOperand(0).getReg())) ||
TRI->isSubRegisterEq(NewMI.getOperand(0).getReg(),
MO.getReg())))));
NewMIImplDefs.push_back(MO.getReg().asMCReg());
NewMIImplDefs.push_back({i, MO.getReg()});
} else {
assert(MO.getReg() == NewMI.getOperand(0).getReg());

Expand Down Expand Up @@ -1640,12 +1634,30 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
// been asked for. If so it must implicitly define the whole thing.
assert(DstReg.isPhysical() &&
"Only expect virtual or physical registers in remat");

// When we're rematerializing into a not-quite-right register we already add
// the real definition as an implicit-def, but we should also be marking the
// "official" register as dead, since nothing else is going to use it as a
// result of this remat. Not doing this can affect pressure tracking.
NewMI.getOperand(0).setIsDead(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this whole function reinventing MachineInstr::addRegisterDead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. MachineInstr::addRegisterDead seems to have a definition of "dead" that conflicts with this code. Here, a super-register dead and a sub-register defined means "everything except the sub-register is dead" (which is required for pressure tracking), but addRegisterDead seems to disagree, as addRegisterDead($rax) will turn:

dead $eax = FOO implicit-def $rax into $eax = FOO implicit-def dead $rax (which implies it thinks "all sub-registers covered by a dead register are dead").


if (!NewMIDefinesFullReg) {
bool HasDefMatchingCopy = false;
for (auto [OpIndex, Reg] : NewMIImplDefs) {
if (Reg != DstReg)
continue;
// Also, if CopyDstReg is a sub-register of DstReg (and it is defined), we
// must mark DstReg as dead since it is not going to used as a result of
// this remat.
if (DstReg != CopyDstReg)
NewMI.getOperand(OpIndex).setIsDead(true);
else
HasDefMatchingCopy = true;
}

// If NewMI does not already have an implicit-def CopyDstReg add one now.
if (!HasDefMatchingCopy)
NewMI.addOperand(MachineOperand::CreateReg(
CopyDstReg, true /*IsDef*/, true /*IsImp*/, false /*IsKill*/));
}

// Record small dead def live-ranges for all the subregisters
// of the destination register.
Expand Down Expand Up @@ -1676,8 +1688,8 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
NewMI.addOperand(MO);

SlotIndex NewMIIdx = LIS->getInstructionIndex(NewMI);
for (MCRegister Reg : NewMIImplDefs) {
for (MCRegUnit Unit : TRI->regunits(Reg))
for (Register Reg : make_second_range(NewMIImplDefs)) {
for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg()))
if (LiveRange *LR = LIS->getCachedRegUnit(Unit))
LR->createDeadDef(NewMIIdx.getRegSlot(), LIS->getVNInfoAllocator());
}
Expand Down
22 changes: 21 additions & 1 deletion llvm/test/CodeGen/X86/rematerialize-sub-super-reg.mir
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,25 @@ body: |
bb.3:
$rax = COPY %t3
RET 0, $rax

...
---
name: rematerialize_superregister_into_subregister_def_with_impdef_physreg
body: |
bb.0.entry:
; CHECK-LABEL: name: rematerialize_superregister_into_subregister_def_with_impdef_physreg
; CHECK: dead $esi = MOV32r0 implicit-def dead $eflags, implicit-def $rsi
; CHECK-NEXT: dead $edx = MOV32r0 implicit-def dead $eflags, implicit-def $rdx
; CHECK-NEXT: FAKE_USE implicit killed $rsi, implicit killed $rdx
; CHECK-NEXT: dead $eax = MOV32r0 implicit-def dead $eflags, implicit-def dead $rax, implicit-def $al
; CHECK-NEXT: FAKE_USE implicit killed $al
; CHECK-NEXT: $eax = MOV32r0 implicit-def dead $eflags
; CHECK-NEXT: RET 0, $eax
undef %1.sub_32bit:gr64_with_sub_8bit = MOV32r0 implicit-def dead $eflags, implicit-def %1
$rsi = COPY %1
$rdx = COPY %1
FAKE_USE implicit killed $rsi, implicit killed $rdx
%4:gr8 = COPY killed %1.sub_8bit
$al = COPY killed %4
FAKE_USE implicit killed $al
$eax = MOV32r0 implicit-def dead $eflags
RET 0, killed $eax