Index: llvm/lib/CodeGen/PHIElimination.cpp =================================================================== --- llvm/lib/CodeGen/PHIElimination.cpp +++ llvm/lib/CodeGen/PHIElimination.cpp @@ -85,7 +85,7 @@ bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB); void LowerPHINode(MachineBasicBlock &MBB, - MachineBasicBlock::iterator LastPHIIt); + MachineBasicBlock::iterator AfterPHIsIt); /// analyzePHINodes - Gather information about the PHI nodes in /// here. In particular, we want to map the number of uses of a virtual @@ -206,12 +206,13 @@ if (MBB.empty() || !MBB.front().isPHI()) 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())); + // Get an iterator to the first non-PHI node. + // Skip debug instruction to avoid impacting on PHI lowering. + MachineBasicBlock::iterator AfterPHIsIt = + MBB.SkipPHIsLabelsAndDebug(MBB.begin()); while (MBB.front().isPHI()) - LowerPHINode(MBB, LastPHIIt); + LowerPHINode(MBB, AfterPHIsIt); return true; } @@ -236,13 +237,12 @@ } return true; } + /// LowerPHINode - Lower the PHI node at the top of the specified block. void PHIElimination::LowerPHINode(MachineBasicBlock &MBB, - MachineBasicBlock::iterator LastPHIIt) { + MachineBasicBlock::iterator AfterPHIsIt) { ++NumLowered; - MachineBasicBlock::iterator AfterPHIsIt = std::next(LastPHIIt); - // Unlink the PHI node from the basic block, but don't delete the PHI yet. MachineInstr *MPhi = MBB.remove(&*MBB.begin()); 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,52 @@ +# RUN: llc -mtriple=x86_64-- -run-pass phi-node-elimination -o - %s | FileCheck %s +# Debug instruction should not impact PHI node lowering with +# a debug instruction between PHI and LABEL. +# Fix issue: https://bugs.llvm.org/show_bug.cgi?id=43859 + +--- +name: test1 +tracksRegLiveness: true +registers: +body: | + ; Verify PHI lowering without Debug instruction. + ; CHECK-LABEL: name: test1 + ; CHECK: bb.2: + ; CHECK: EH_LABEL 0 + ; CHECK: %1:gr32 = COPY %3 + ; CHECK: %2:gr32 = ADD32ri8 + bb.0: + %0:gr32 = IMPLICIT_DEF + JMP_1 %bb.2 + + bb.1: + + bb.2: + %1:gr32 = PHI %0, %bb.0, %2, %bb.1 + EH_LABEL 0 + %2:gr32 = ADD32ri8 killed %1, 1, implicit-def $eflags +... + +--- +name: test2 +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: test2 + ; CHECK: bb.2: + ; CHECK: DBG_VALUE %1 + ; CHECK: EH_LABEL 0 + ; CHECK: %1:gr32 = COPY %3 + ; CHECK: %2:gr32 = ADD32ri8 + 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 + %2:gr32 = ADD32ri8 killed %1, 1, implicit-def $eflags +...