diff --git a/llvm/lib/Target/AMDGPU/SIOptimizeExecMaskingPreRA.cpp b/llvm/lib/Target/AMDGPU/SIOptimizeExecMaskingPreRA.cpp --- a/llvm/lib/Target/AMDGPU/SIOptimizeExecMaskingPreRA.cpp +++ b/llvm/lib/Target/AMDGPU/SIOptimizeExecMaskingPreRA.cpp @@ -416,15 +416,19 @@ continue; Register SavedExec = I->getOperand(0).getReg(); - if (SavedExec.isVirtual() && MRI->hasOneNonDBGUse(SavedExec) && - MRI->use_instr_nodbg_begin(SavedExec)->getParent() == - I->getParent()) { - LLVM_DEBUG(dbgs() << "Redundant EXEC COPY: " << *I << '\n'); - LIS->RemoveMachineInstrFromMaps(*I); - I->eraseFromParent(); - MRI->replaceRegWith(SavedExec, ExecReg); - LIS->removeInterval(SavedExec); - Changed = true; + if (SavedExec.isVirtual() && MRI->hasOneNonDBGUse(SavedExec)) { + MachineInstr *SingleExecUser = &*MRI->use_instr_nodbg_begin(SavedExec); + int idx = SingleExecUser->findRegisterUseOperandIdx(SavedExec); + assert(idx != -1); + if (SingleExecUser->getParent() == I->getParent() && + TII->isOperandLegal(*SingleExecUser, idx, &I->getOperand(1))) { + LLVM_DEBUG(dbgs() << "Redundant EXEC COPY: " << *I << '\n'); + LIS->RemoveMachineInstrFromMaps(*I); + I->eraseFromParent(); + MRI->replaceRegWith(SavedExec, ExecReg); + LIS->removeInterval(SavedExec); + Changed = true; + } } break; }