Index: llvm/lib/CodeGen/PHIElimination.cpp =================================================================== --- llvm/lib/CodeGen/PHIElimination.cpp +++ llvm/lib/CodeGen/PHIElimination.cpp @@ -207,8 +207,9 @@ return false; // Quick exit for basic blocks without PHIs. // Get an iterator to the last PHI node. + // Skip debug instruction to avoid impacting PHI lower. MachineBasicBlock::iterator LastPHIIt = - std::prev(MBB.SkipPHIsAndLabels(MBB.begin())); + std::prev(MBB.SkipPHIsLabelsAndDebug(MBB.begin())); 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,51 @@ +# RUN: llc -mtriple=x86_64-- -run-pass phi-node-elimination -o - %s | FileCheck %s +# Debug instruction should not impact PHI node lower when +# it is at the top of the specified block +# Fix issue: https://bugs.llvm.org/show_bug.cgi?id=43859 + +--- +name: test1 +tracksRegLiveness: true +registers: +body: | + ; Verify PHI lowering without Debug instruction exist + ; CHECK-LABEL: name: test1 + ; CHECK: bb.2: + ; CHECK: EH_LABEL 0 + ; CHECK: %1:gr32 = COPY %3 + bb.0: + %0:gr32 = IMPLICIT_DEF + JMP_1 %bb.2 + + bb.1: + + bb.2: + %1:gr32 = PHI %0:gr32, %bb.0, %2:gr32, %bb.1 + EH_LABEL 0 + %2:gr32 = ADD32ri8 killed %1:gr32, 1, implicit-def $eflags +... + +--- +name: test2 +tracksRegLiveness: true +body: | + + ; Verify PHI lowering with Debug instruction exist + ; it is at the top of the specified block + ; + ; CHECK-LABEL: name: test2 + ; CHECK: bb.2: + ; CHECK: EH_LABEL 0 + ; CHECK: %1:gr32 = COPY %3 + bb.0: + %0:gr32 = IMPLICIT_DEF + JMP_1 %bb.2 + + bb.1: + + bb.2: + %1:gr32 = PHI %0:gr32, %bb.0, %2:gr32, %bb.1 + DBG_VALUE + EH_LABEL 0 + %2:gr32 = ADD32ri8 killed %1:gr32, 1, implicit-def $eflags +...