Skip to content

Commit 491edcf

Browse files
committed
Mark the last use of EFLAGS before the copy's def as a kill if the copy's def operand is itself a kill.
1 parent e038c54 commit 491edcf

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

llvm/lib/Target/X86/X86FlagsCopyLowering.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,31 @@ bool X86FlagsCopyLoweringPass::runOnMachineFunction(MachineFunction &MF) {
418418
auto Cleanup = make_scope_exit([&] {
419419
// All uses of the EFLAGS copy are now rewritten, kill the copy into
420420
// eflags and if dead the copy from.
421+
422+
// Mark the last use of EFLAGS before the copy's def as a kill if
423+
// the copy's def operand is itself a kill.
424+
if (CopyDefI.getOperand(1).isKill()) {
425+
MachineBasicBlock *DefMBB = CopyDefI.getParent();
426+
MachineInstr *LastUse = nullptr;
427+
428+
// Find the last use of EFLAGS before CopyDefI
429+
for (auto MI = std::prev(CopyDefI.getIterator()); MI != DefMBB->begin();
430+
--MI) {
431+
if (MI->readsRegister(X86::EFLAGS, TRI)) {
432+
LastUse = &*MI;
433+
break;
434+
}
435+
}
436+
437+
// If we found a last use, mark it as kill
438+
if (LastUse) {
439+
MachineOperand *FlagUse =
440+
LastUse->findRegisterUseOperand(X86::EFLAGS, TRI);
441+
if (FlagUse)
442+
FlagUse->setIsKill(true);
443+
}
444+
}
445+
421446
CopyI->eraseFromParent();
422447
if (MRI->use_empty(CopyDefI.getOperand(0).getReg()))
423448
CopyDefI.eraseFromParent();
@@ -687,9 +712,6 @@ bool X86FlagsCopyLoweringPass::runOnMachineFunction(MachineFunction &MF) {
687712

688713
rewriteMI(*TestMBB, TestPos, TestLoc, *JmpI, CondRegs);
689714
}
690-
691-
// FIXME: Mark the last use of EFLAGS before the copy's def as a kill if
692-
// the copy's def operand is itself a kill.
693715
}
694716

695717
#ifndef NDEBUG

llvm/test/CodeGen/X86/apx/flags-copy-lowering.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ body: |
5050
; CHECK-NEXT: [[COPY:%[0-9]+]]:gr64 = COPY $rdi
5151
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gr64 = COPY $rsi
5252
; CHECK-NEXT: [[SUB64rr_ND:%[0-9]+]]:gr64 = SUB64rr_ND [[COPY]], [[COPY1]], implicit-def $eflags
53-
; CHECK-NEXT: [[SETCCr:%[0-9]+]]:gr8 = SETCCr 2, implicit $eflags
53+
; CHECK-NEXT: [[SETCCr:%[0-9]+]]:gr8 = SETCCr 2, implicit killed $eflags
5454
; CHECK-NEXT: INLINEASM &nop, 1 /* sideeffect attdialect */, 12 /* clobber */, implicit-def dead $eflags
5555
; CHECK-NEXT: dead [[ADD8ri_ND:%[0-9]+]]:gr8 = ADD8ri_ND [[SETCCr]], 255, implicit-def $eflags
5656
; CHECK-NEXT: [[SBB64ri32_ND:%[0-9]+]]:gr64 = SBB64ri32_ND [[SUB64rr_ND]], 42, implicit-def $eflags, implicit killed $eflags

llvm/test/CodeGen/X86/flags-copy-lowering.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ body: |
277277
; CHECK-NEXT: [[COPY:%[0-9]+]]:gr64 = COPY $rdi
278278
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gr64 = COPY $rsi
279279
; CHECK-NEXT: [[SUB64rr:%[0-9]+]]:gr64 = SUB64rr [[COPY]], [[COPY1]], implicit-def $eflags
280-
; CHECK-NEXT: [[SETCCr:%[0-9]+]]:gr8 = SETCCr 2, implicit $eflags
280+
; CHECK-NEXT: [[SETCCr:%[0-9]+]]:gr8 = SETCCr 2, implicit killed $eflags
281281
; CHECK-NEXT: INLINEASM &nop, 1 /* sideeffect attdialect */, 12 /* clobber */, implicit-def dead $eflags
282282
; CHECK-NEXT: dead [[ADD8ri:%[0-9]+]]:gr8 = ADD8ri [[SETCCr]], 255, implicit-def $eflags
283283
; CHECK-NEXT: [[SBB64ri32_:%[0-9]+]]:gr64 = SBB64ri32 [[SUB64rr]], 42, implicit-def $eflags, implicit killed $eflags

0 commit comments

Comments
 (0)