From ad80a46caacef8b4073df3fc10e0c58ebbee4ec7 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 24 Apr 2003 17:52:20 +0000 Subject: [PATCH] Fix iterator invalidation problem git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5895 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/PromoteMemoryToRegister.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp index 8406910e6b9..3378369f7aa 100644 --- a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp +++ b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp @@ -201,8 +201,13 @@ bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo) { // because it is an unreachable predecessor), that all PHI nodes will have the // correct number of entries for their predecessors. Value *NullVal = Constant::getNullValue(PN->getType()); - for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) - PN->addIncoming(NullVal, *PI); + + // This is neccesary because adding incoming values to the PHI node adds uses + // to the basic blocks being used, which can invalidate the predecessor + // iterator! + std::vector Preds(pred_begin(BB), pred_end(BB)); + for (unsigned i = 0, e = Preds.size(); i != e; ++i) + PN->addIncoming(NullVal, Preds[i]); BBPNs[AllocaNo] = PN; PhiNodes[AllocaNo].push_back(BB);