@@ -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
0 commit comments