Fix bug: test/Regression/Transforms/CFGSimplify/2002-05-21-PHIElimination.ll
authorChris Lattner <sabre@nondot.org>
Tue, 21 May 2002 19:52:49 +0000 (19:52 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 21 May 2002 19:52:49 +0000 (19:52 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2694 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/BasicBlock.cpp

index a3fdb28f498e48675891feec61a95ad095457ef4..9a6301eb0162accc273d55cbf6e42e108682fbc8 100644 (file)
@@ -100,8 +100,26 @@ void BasicBlock::removePredecessor(BasicBlock *Pred) {
   for (max_idx = 0; PI != EI && max_idx < 3; ++PI, ++max_idx) /*empty*/;
 
   // If there are exactly two predecessors, then we want to nuke the PHI nodes
-  // altogether.
+  // altogether.  We cannot do this, however if this in this case however:
+  //
+  //  Loop:
+  //    %x = phi [X, Loop]
+  //    %x2 = add %x, 1         ;; This would become %x2 = add %x2, 1
+  //    br Loop                 ;; %x2 does not dominate all uses
+  //
+  // This is because the PHI node input is actually taken from the predecessor
+  // basic block.  The only case this can happen is with a self loop, so we 
+  // check for this case explicitly now.
+  // 
   assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
+  if (max_idx == 2) {
+    PI = pred_begin(this);
+    BasicBlock *Other = *PI == Pred ? *++PI : *PI;
+
+    // Disable PHI elimination!
+    if (this == Other) max_idx = 3;
+  }
+
   if (max_idx <= 2) {                // <= Two predecessors BEFORE I remove one?
     // Yup, loop through and nuke the PHI nodes
     while (PHINode *PN = dyn_cast<PHINode>(front())) {
@@ -118,9 +136,8 @@ void BasicBlock::removePredecessor(BasicBlock *Pred) {
   } else {
     // Okay, now we know that we need to remove predecessor #pred_idx from all
     // PHI nodes.  Iterate over each PHI node fixing them up
-    iterator II(begin());
-    for (; isa<PHINode>(*II); ++II)
-      cast<PHINode>(*II)->removeIncomingValue(Pred);
+    for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(*II); ++II)
+      PN->removeIncomingValue(Pred);
   }
 }