diff --git a/llvm/lib/CodeGen/MIRCanonicalizerPass.cpp b/llvm/lib/CodeGen/MIRCanonicalizerPass.cpp --- a/llvm/lib/CodeGen/MIRCanonicalizerPass.cpp +++ b/llvm/lib/CodeGen/MIRCanonicalizerPass.cpp @@ -343,15 +343,23 @@ continue; if (!TargetRegisterInfo::isVirtualRegister(Src)) continue; + // Not folding COPY instructions if regbankselect has not set the RCs. + // Why are we only considering Register Classes? Because the verifier + // sometimes gets upset if the register classes don't match even if the + // types do. A future patch might add COPY folding for matching types in + // pre-registerbankselect code. + if (!MRI.getRegClassOrNull(Dst)) + continue; if (MRI.getRegClass(Dst) != MRI.getRegClass(Src)) continue; - for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) { - MachineOperand *MO = &*UI; + std::vector Uses; + for (auto UI = MRI.use_begin(Dst); UI != MRI.use_end(); ++UI) + Uses.push_back(&*UI); + for (auto *MO : Uses) MO->setReg(Src); - Changed = true; - } + Changed = true; MI->eraseFromParent(); } diff --git a/llvm/test/CodeGen/MIR/AMDGPU/mir-canon-multi.mir b/llvm/test/CodeGen/MIR/AMDGPU/mir-canon-multi.mir --- a/llvm/test/CodeGen/MIR/AMDGPU/mir-canon-multi.mir +++ b/llvm/test/CodeGen/MIR/AMDGPU/mir-canon-multi.mir @@ -1,8 +1,8 @@ # RUN: llc -o - -march=amdgcn -run-pass mir-canonicalizer -x mir %s | FileCheck %s # CHECK: %namedVReg1352:vgpr_32 = COPY %namedVReg4353 +# CHECK: %namedVReg1358:vgpr_32 = COPY %namedVReg1361 # CHECK: %namedVReg1359:vgpr_32 = COPY %namedVReg1362 -# CHECK: %namedVReg1360:vgpr_32 = COPY %namedVReg1363 ... --- name: foo