diff --git a/lib/CodeGen/MachineBasicBlock.cpp b/lib/CodeGen/MachineBasicBlock.cpp index ad1ab287e34..36963875f9e 100644 --- a/lib/CodeGen/MachineBasicBlock.cpp +++ b/lib/CodeGen/MachineBasicBlock.cpp @@ -155,11 +155,22 @@ MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { } MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { - iterator I = end(); - while (I != begin() && (--I)->getDesc().isTerminator()) - ; /*noop */ - if (I != end() && !I->getDesc().isTerminator()) ++I; - return I; + iterator B = begin(), I = end(); + iterator Term = I; + while (I != B) { + --I; + // Ignore any debug values after the first terminator. + if (I->isDebugValue()) + continue; + // Stop once we see a non-debug non-terminator. + if (!I->getDesc().isTerminator()) + break; + // Earliest terminator so far. + Term = I; + } + // Return the first terminator, or end(). + // Everything after Term is terminators and debug values. + return Term; } MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { diff --git a/lib/CodeGen/PHIElimination.cpp b/lib/CodeGen/PHIElimination.cpp index 923fa213e7b..b940e269112 100644 --- a/lib/CodeGen/PHIElimination.cpp +++ b/lib/CodeGen/PHIElimination.cpp @@ -339,6 +339,8 @@ void PHIElimination::LowerAtomicPHINode( #ifndef NDEBUG for (MachineBasicBlock::iterator TI = llvm::next(Term); TI != opBlock.end(); ++TI) { + if (TI->isDebugValue()) + continue; assert(!TI->readsRegister(SrcReg) && "Terminator instructions cannot use virtual registers unless" "they are the first terminator in a block!"); @@ -347,9 +349,13 @@ void PHIElimination::LowerAtomicPHINode( } else if (reusedIncoming || !IncomingReg) { // We may have to rewind a bit if we didn't insert a copy this time. KillInst = Term; - while (KillInst != opBlock.begin()) - if ((--KillInst)->readsRegister(SrcReg)) + while (KillInst != opBlock.begin()) { + --KillInst; + if (KillInst->isDebugValue()) + continue; + if (KillInst->readsRegister(SrcReg)) break; + } } else { // We just inserted this copy. KillInst = prior(InsertPos);