[llvm-commits] CVS: llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp

Chris Lattner lattner at cs.uiuc.edu
Thu Apr 24 12:53:01 PDT 2003


Changes in directory llvm/lib/Transforms/Utils:

PromoteMemoryToRegister.cpp updated: 1.39 -> 1.40

---
Log message:

Fix iterator invalidation problem


---
Diffs of the changes:

Index: llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
diff -u llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp:1.39 llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp:1.40
--- llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp:1.39	Mon Apr 21 14:15:26 2003
+++ llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp	Thu Apr 24 12:52:20 2003
@@ -201,8 +201,13 @@
   // 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<BasicBlock*> 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);





More information about the llvm-commits mailing list