Fix PR 1995.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46898 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Scalar/LoopIndexSplit.cpp b/lib/Transforms/Scalar/LoopIndexSplit.cpp
index d31a589..683a038 100644
--- a/lib/Transforms/Scalar/LoopIndexSplit.cpp
+++ b/lib/Transforms/Scalar/LoopIndexSplit.cpp
@@ -1392,6 +1392,11 @@
   if (!Succ0->getSinglePredecessor() || !Succ1->getSinglePredecessor())
     return false;
 
+  // If Exiting block includes loop variant instructions then this
+  // loop may not be split safely.
+  if (!safeExitingBlock(SD, ExitCondition->getParent())) 
+    return false;
+
   // After loop is cloned there are two loops.
   //
   // First loop, referred as ALoop, executes first part of loop's iteration
@@ -1616,8 +1621,8 @@
 ///   - ExitBB's single predecessor was Latch
 ///   - Latch's second successor was Header
 /// Now
-///   - ExitBB's single predecessor was Header
-///   - Latch's one and only successor was Header
+///   - ExitBB's single predecessor is Header
+///   - Latch's one and only successor is Header
 ///
 /// Update ExitBB PHINodes' to reflect this change.
 void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch, 
@@ -1632,27 +1637,18 @@
 
     Value *V = PN->getIncomingValueForBlock(Latch);
     if (PHINode *PHV = dyn_cast<PHINode>(V)) {
-      // PHV is in Latch. PHV has two uses, one use is in ExitBB PHINode 
-      // (i.e. PN :)). 
-      // The second use is in Header and it is new incoming value for PN.
-      PHINode *U1 = NULL;
-      PHINode *U2 = NULL;
+      // PHV is in Latch. PHV has one use is in ExitBB PHINode. And one use
+      // in Header which is new incoming value for PN.
       Value *NewV = NULL;
       for (Value::use_iterator UI = PHV->use_begin(), E = PHV->use_end(); 
-           UI != E; ++UI) {
-        if (!U1)
-          U1 = cast<PHINode>(*UI);
-        else if (!U2)
-          U2 = cast<PHINode>(*UI);
-        else
-          assert ( 0 && "Unexpected third use of this PHINode");
-      }
-      assert (U1 && U2 && "Unable to find two uses");
-      
-      if (U1->getParent() == Header) 
-        NewV = U1;
-      else
-        NewV = U2;
+           UI != E; ++UI) 
+        if (PHINode *U = dyn_cast<PHINode>(*UI)) 
+          if (U->getParent() == Header) {
+            NewV = U;
+            break;
+          }
+
+      assert (NewV && "Unable to find new incoming value for exit block PHI");
       PN->addIncoming(NewV, Header);
 
     } else if (Instruction *PHI = dyn_cast<Instruction>(V)) {