diff --git a/llvm/lib/CodeGen/RegisterCoalescer.cpp b/llvm/lib/CodeGen/RegisterCoalescer.cpp --- a/llvm/lib/CodeGen/RegisterCoalescer.cpp +++ b/llvm/lib/CodeGen/RegisterCoalescer.cpp @@ -2196,6 +2196,7 @@ // ... // use %physreg_x CopyMI = MRI->getVRegDef(SrcReg); + deleteInstr(CopyMI); } else { // VReg is copied into physreg: // %y = def @@ -2240,6 +2241,8 @@ << printReg(DstReg, TRI) << " at " << CopyRegIdx << "\n"); LIS->removePhysRegDefAt(DstReg.asMCReg(), CopyRegIdx); + deleteInstr(CopyMI); + // Create a new dead def at the new def location. for (MCRegUnitIterator UI(DstReg, TRI); UI.isValid(); ++UI) { LiveRange &LR = LIS->getRegUnit(*UI); @@ -2247,8 +2250,6 @@ } } - deleteInstr(CopyMI); - // We don't track kills for reserved registers. MRI->clearKillFlags(CP.getSrcReg()); diff --git a/llvm/test/CodeGen/AArch64/regcoal-physreg.mir b/llvm/test/CodeGen/AArch64/regcoal-physreg.mir --- a/llvm/test/CodeGen/AArch64/regcoal-physreg.mir +++ b/llvm/test/CodeGen/AArch64/regcoal-physreg.mir @@ -1,4 +1,8 @@ -# RUN: llc -mtriple=aarch64-apple-ios -run-pass=simple-register-coalescing -verify-machineinstrs %s -o - | FileCheck %s +# RUN: llc -mtriple=aarch64-apple-ios -run-pass=simple-register-coalescing,simple-register-coalescing -verify-machineinstrs %s -o - | FileCheck %s + +# FIXME: Second run of the pass is a workaround for a bug in +# -run-pass. The verifier doesn't detect broken LiveIntervals, see bug +# 46873 --- | declare void @f2()