diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp --- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp +++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp @@ -462,8 +462,7 @@ auto PrevCopyOperands = isCopyInstr(*PrevCopy, *TII, UseCopyInstr); // Check that the existing copy uses the correct sub registers. - if (PrevCopyOperands->Destination->isDead() || - PrevCopyOperands->Source->isUndef()) + if (PrevCopyOperands->Destination->isDead()) return false; if (!isNopCopy(*PrevCopy, Src, Def, TRI, TII, UseCopyInstr)) return false; @@ -482,6 +481,12 @@ make_range(PrevCopy->getIterator(), Copy.getIterator())) MI.clearRegisterKills(CopyDef, TRI); + // Clear undef flag from remaining copy if needed. + if (!CopyOperands->Source->isUndef()) { + PrevCopy->getOperand(PrevCopyOperands->Source->getOperandNo()) + .setIsUndef(false); + } + Copy.eraseFromParent(); Changed = true; ++NumDeletes; diff --git a/llvm/test/CodeGen/X86/machine-copy-prop.mir b/llvm/test/CodeGen/X86/machine-copy-prop.mir --- a/llvm/test/CodeGen/X86/machine-copy-prop.mir +++ b/llvm/test/CodeGen/X86/machine-copy-prop.mir @@ -220,9 +220,8 @@ # second COPY, otherwise we risk making $rax undef. # CHECK-LABEL: name: nocopyprop6 # CHECK: bb.0: -# CHECK: $rax = COPY undef $rdi -# CHECK-NEXT: NOOP implicit killed $rax -# CHECK-NEXT: $rax = COPY $rdi +# CHECK: $rax = COPY $rdi +# CHECK-NEXT: NOOP implicit $rax # CHECK-NEXT: NOOP implicit $rax, implicit $rdi name: nocopyprop6 tracksRegLiveness: true