VMCore/BasicBlock.cpp: Don't assume BasicBlock::iterator might end with a non-PHInode...
authorNAKAMURA Takumi <geek4civic@gmail.com>
Tue, 9 Aug 2011 23:13:05 +0000 (23:13 +0000)
committerNAKAMURA Takumi <geek4civic@gmail.com>
Tue, 9 Aug 2011 23:13:05 +0000 (23:13 +0000)
Frontends(eg. clang) might pass incomplete form of IR, to step off the way beyond iterator end. In the case I had met, it took infinite loop due to meeting bogus PHInode.

Thanks to Jay Foad and John McCall.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@137175 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/BasicBlock.cpp

index df6d1f4ce4d5d000aa7161af2e21d206d6acf98f..5fab1d39ae85ad275a865512a159dea8e0f1a105 100644 (file)
@@ -336,8 +336,12 @@ void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
     return;
   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
     BasicBlock *Succ = TI->getSuccessor(i);
-    for (iterator II = Succ->begin(); PHINode *PN = dyn_cast<PHINode>(II);
-         ++II) {
+    // N.B. Succ might not be a complete BasicBlock, so don't assume
+    // that it ends with a non-phi instruction.
+    for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) {
+      PHINode *PN = dyn_cast<PHINode>(II);
+      if (!PN)
+        break;
       int i;
       while ((i = PN->getBasicBlockIndex(this)) >= 0)
         PN->setIncomingBlock(i, New);