Index: lib/Target/AMDGPU/SIFixSGPRCopies.cpp =================================================================== --- lib/Target/AMDGPU/SIFixSGPRCopies.cpp +++ lib/Target/AMDGPU/SIFixSGPRCopies.cpp @@ -631,7 +631,7 @@ if ((!TRI->isVGPR(MRI, PHIRes) && RC0 != &AMDGPU::VReg_1RegClass) && (hasVGPRInput || hasVGPRUses > 1)) { - TII->moveToVALU(MI); + TII->moveToVALU(MI, MDT); } else { TII->legalizeOperands(MI, MDT); } Index: lib/Target/AMDGPU/SIInstrInfo.h =================================================================== --- lib/Target/AMDGPU/SIInstrInfo.h +++ lib/Target/AMDGPU/SIInstrInfo.h @@ -939,6 +939,8 @@ /// Return -1 if the target-specific opcode for the pseudo instruction does /// not exist. If Opcode is not a pseudo instruction, this is identity. int pseudoToMCOpcode(int Opcode) const; + + bool shouldSink(const MachineInstr &MI) const override; }; /// \brief Returns true if a reg:subreg pair P has a TRC class Index: lib/Target/AMDGPU/SIInstrInfo.cpp =================================================================== --- lib/Target/AMDGPU/SIInstrInfo.cpp +++ lib/Target/AMDGPU/SIInstrInfo.cpp @@ -4171,6 +4171,14 @@ MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB(); MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator(); + unsigned Reg = Op.getReg(); + MachineInstr *DefMI = MRI.getUniqueVRegDef(Reg); + if (DefMI && DefMI->getParent() != InsertBB && + MDT->dominates(DefMI->getParent(), InsertBB)) { + InsertBB = DefMI->getParent(); + Insert = InsertBB->getFirstTerminator(); + } + // Avoid creating no-op copies with the same src and dst reg class. These // confuse some of the machine passes. legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc()); @@ -5957,3 +5965,15 @@ } return Uses.empty(); } + +bool SIInstrInfo::shouldSink(const MachineInstr &MI) const { + const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); + if (MI.isCopy()) { + unsigned SrcReg = MI.getOperand(1).getReg(); + unsigned DstReg = MI.getOperand(0).getReg(); + if (RI.isSGPRReg(MRI, SrcReg) && !RI.isSGPRReg(MRI, DstReg)) { + return false; + } + } + return true; +} \ No newline at end of file