Index: llvm/lib/CodeGen/PHIElimination.cpp =================================================================== --- llvm/lib/CodeGen/PHIElimination.cpp +++ llvm/lib/CodeGen/PHIElimination.cpp @@ -199,6 +199,21 @@ return Changed; } +void spliceDebugInstructionForward(MachineBasicBlock &MBB, + MachineBasicBlock::iterator From, + MachineBasicBlock::iterator To) { + if (From == To) + return; + + for (MachineBasicBlock::iterator Inst = std::prev(To); Inst != From; --Inst) { + MachineBasicBlock::iterator where = std::next(To); + if(Inst->isDebugInstr()) { + MBB.splice(where, &MBB, Inst); + Inst = std::prev(Inst); + } + } +} + /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in /// predecessor basic blocks. bool PHIElimination::EliminatePHINodes(MachineFunction &MF, @@ -207,8 +222,11 @@ return false; // Quick exit for basic blocks without PHIs. // Get an iterator to the last PHI node. - MachineBasicBlock::iterator LastPHIIt = - std::prev(MBB.SkipPHIsAndLabels(MBB.begin())); + MachineBasicBlock::iterator LastPHIIt = skipDebugInstructionsBackward( + std::prev(MBB.SkipPHIsLabelsAndDebug(MBB.begin())), MBB.begin()); + + if (LastPHIIt->isLabel()) + spliceDebugInstructionForward(MBB, MBB.begin(), LastPHIIt); while (MBB.front().isPHI()) LowerPHINode(MBB, LastPHIIt); Index: llvm/test/CodeGen/X86/phi-node-elimination-dbg-invariant.mir =================================================================== --- /dev/null +++ llvm/test/CodeGen/X86/phi-node-elimination-dbg-invariant.mir @@ -0,0 +1,30 @@ +# RUN: llc -mtriple=x86_64-- -run-pass phi-node-elimination -o - %s | FileCheck %s +# Debug instruction should not impact PHI node lowering after LABEL, move DBG_VALUEs +# after LABEL before PHI lowering. +# Fix issue: https://bugs.llvm.org/show_bug.cgi?id=43859 + +--- +name: test1 +tracksRegLiveness: true +body: | + ; Verify PHI lowering with a debug instruction between PHI and LABEL, + ; skip debug instruction and insert copy instruction after LABEL. + ; CHECK-LABEL: name: test1 + ; CHECK: bb.2: + ; CHECK: EH_LABEL 0 + ; CHECK-NEXT: %1:gr32 = COPY %3 + ; CHECK-NEXT: DBG_VALUE %1 + ; CHECK-NEXT: DBG_VALUE %2 + bb.0: + %0:gr32 = IMPLICIT_DEF + JMP_1 %bb.2 + + bb.1: + + bb.2: + %1:gr32 = PHI %0, %bb.0, %2, %bb.1 + DBG_VALUE %1 + EH_LABEL 0 + DBG_VALUE %2 + %2:gr32 = ADD32ri8 killed %1, 1, implicit-def $eflags +...