If loop induction variable's start value is less then its exit value then do not...
[oota-llvm.git] / lib / Transforms / Scalar / LoopIndexSplit.cpp
index a0bfe4f0822d448804b39d1f75b83dd8ecb25301..135ce33ee6f4918c9487ee12ede6f1f7f44047fb 100644 (file)
@@ -195,11 +195,12 @@ namespace {
     // Induction variable's final loop exit value operand number in exit condition..
     unsigned ExitValueNum;
   };
-
-  char LoopIndexSplit::ID = 0;
-  RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops");
 }
 
+char LoopIndexSplit::ID = 0;
+static RegisterPass<LoopIndexSplit>
+X("loop-index-split", "Index Split Loops");
+
 LoopPass *llvm::createLoopIndexSplitPass() {
   return new LoopIndexSplit();
 }
@@ -232,8 +233,8 @@ bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
     return false;
 
   // First see if it is possible to eliminate loop itself or not.
-  for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
-         E = SplitData.end(); SI != E;) {
+  for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin();
+       SI != SplitData.end();) {
     SplitInfo &SD = *SI;
     ICmpInst *CI = dyn_cast<ICmpInst>(SD.SplitCondition);
     if (SD.SplitCondition->getOpcode() == Instruction::And) {
@@ -244,8 +245,7 @@ bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
         return Changed;
       } else {
         SmallVector<SplitInfo, 4>::iterator Delete_SI = SI;
-        ++SI;
-        SplitData.erase(Delete_SI);
+        SI = SplitData.erase(Delete_SI);
       }
     }
     else if (CI && CI->getPredicate() == ICmpInst::ICMP_EQ) {
@@ -256,8 +256,7 @@ bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
         return Changed;
       } else {
         SmallVector<SplitInfo, 4>::iterator Delete_SI = SI;
-        ++SI;
-        SplitData.erase(Delete_SI);
+        SI = SplitData.erase(Delete_SI);
       }
     } else
       ++SI;
@@ -385,6 +384,19 @@ void LoopIndexSplit::findLoopConditionals() {
     BasicBlock *Preheader = L->getLoopPreheader();
     StartValue = IndVar->getIncomingValueForBlock(Preheader);
   }
+
+  // If start value is more then exit value where induction variable
+  // increments by 1 then we are potentially dealing with an infinite loop.
+  // Do not index split this loop.
+  if (ExitCondition) {
+    ConstantInt *SV = dyn_cast<ConstantInt>(StartValue);
+    ConstantInt *EV = 
+      dyn_cast<ConstantInt>(ExitCondition->getOperand(ExitValueNum));
+    if (SV && EV && SV->getSExtValue() > EV->getSExtValue())
+      ExitCondition = NULL;
+    else if (EV && EV->isZero())
+      ExitCondition = NULL;
+  }
 }
 
 /// Find condition inside a loop that is suitable candidate for index split.
@@ -582,7 +594,7 @@ bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) {
                                  ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
                                  SD.SplitValue, ExitValue, "lisplit", 
                                  Terminator);
-  Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit", 
+  Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", 
                                                       Terminator);
   SD.SplitCondition->replaceAllUsesWith(NSplitCond);
   SD.SplitCondition->eraseFromParent();
@@ -597,11 +609,27 @@ bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) {
     if (isa<PHINode>(I) || I == LTerminator)
       continue;
 
-    if (I == IndVarIncrement) 
-      I->replaceAllUsesWith(ExitValue);
-    else
+    if (I == IndVarIncrement) {
+      // Replace induction variable increment if it is not used outside 
+      // the loop.
+      bool UsedOutsideLoop = false;
+      for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); 
+           UI != E; ++UI) {
+        if (Instruction *Use = dyn_cast<Instruction>(UI)) 
+          if (!L->contains(Use->getParent())) {
+            UsedOutsideLoop = true;
+            break;
+          }
+      }
+      if (!UsedOutsideLoop) {
+        I->replaceAllUsesWith(ExitValue);
+        I->eraseFromParent();
+      }
+    }
+    else {
       I->replaceAllUsesWith(UndefValue::get(I->getType()));
-    I->eraseFromParent();
+      I->eraseFromParent();
+    }
   }
 
   LPM->deleteLoopFromQueue(L);
@@ -770,11 +798,11 @@ void LoopIndexSplit::updateLoopBounds(ICmpInst *CI) {
     //
     if (ExitCondition->getPredicate() == ICmpInst::ICMP_SLT
         || ExitCondition->getPredicate() == ICmpInst::ICMP_ULT) {
-      Value *A = BinaryOperator::createAdd(NV, ConstantInt::get(Ty, 1, Sign),
+      Value *A = BinaryOperator::CreateAdd(NV, ConstantInt::get(Ty, 1, Sign),
                                            "lsplit.add", PHTerminator);
       Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
                               A, UB,"lsplit,c", PHTerminator);
-      NUB = new SelectInst (C, A, UB, "lsplit.nub", PHTerminator);
+      NUB = SelectInst::Create(C, A, UB, "lsplit.nub", PHTerminator);
     }
     
     // for (i = LB; i <= UB; ++i)
@@ -790,7 +818,7 @@ void LoopIndexSplit::updateLoopBounds(ICmpInst *CI) {
              || ExitCondition->getPredicate() == ICmpInst::ICMP_ULE) {
       Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
                               NV, UB, "lsplit.c", PHTerminator);
-      NUB = new SelectInst (C, NV, UB, "lsplit.nub", PHTerminator);
+      NUB = SelectInst::Create(C, NV, UB, "lsplit.nub", PHTerminator);
     }
     break;
   case ICmpInst::ICMP_ULT:
@@ -808,7 +836,7 @@ void LoopIndexSplit::updateLoopBounds(ICmpInst *CI) {
         || ExitCondition->getPredicate() == ICmpInst::ICMP_ULT) {
       Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
                               NV, UB, "lsplit.c", PHTerminator);
-      NUB = new SelectInst (C, NV, UB, "lsplit.nub", PHTerminator);
+      NUB = SelectInst::Create(C, NV, UB, "lsplit.nub", PHTerminator);
     }
 
     // for (i = LB; i <= UB; ++i)
@@ -822,11 +850,11 @@ void LoopIndexSplit::updateLoopBounds(ICmpInst *CI) {
     //
     else if (ExitCondition->getPredicate() == ICmpInst::ICMP_SLE
              || ExitCondition->getPredicate() == ICmpInst::ICMP_ULE) {
-      Value *S = BinaryOperator::createSub(NV, ConstantInt::get(Ty, 1, Sign),
+      Value *S = BinaryOperator::CreateSub(NV, ConstantInt::get(Ty, 1, Sign),
                                            "lsplit.add", PHTerminator);
       Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
                               S, UB, "lsplit.c", PHTerminator);
-      NUB = new SelectInst (C, S, UB, "lsplit.nub", PHTerminator);
+      NUB = SelectInst::Create(C, S, UB, "lsplit.nub", PHTerminator);
     }
     break;
   case ICmpInst::ICMP_UGE:
@@ -843,7 +871,7 @@ void LoopIndexSplit::updateLoopBounds(ICmpInst *CI) {
     {
       Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
                               NV, StartValue, "lsplit.c", PHTerminator);
-      NLB = new SelectInst (C, StartValue, NV, "lsplit.nlb", PHTerminator);
+      NLB = SelectInst::Create(C, StartValue, NV, "lsplit.nlb", PHTerminator);
     }
     break;
   case ICmpInst::ICMP_UGT:
@@ -858,11 +886,11 @@ void LoopIndexSplit::updateLoopBounds(ICmpInst *CI) {
     //   LOOP_BODY
     //
     {
-      Value *A = BinaryOperator::createAdd(NV, ConstantInt::get(Ty, 1, Sign),
+      Value *A = BinaryOperator::CreateAdd(NV, ConstantInt::get(Ty, 1, Sign),
                                            "lsplit.add", PHTerminator);
       Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
                               A, StartValue, "lsplit.c", PHTerminator);
-      NLB = new SelectInst (C, StartValue, A, "lsplit.nlb", PHTerminator);
+      NLB = SelectInst::Create(C, StartValue, A, "lsplit.nlb", PHTerminator);
     }
     break;
   default:
@@ -1126,6 +1154,11 @@ bool LoopIndexSplit::safeSplitCondition(SplitInfo &SD) {
   BasicBlock *Succ0 = SplitTerminator->getSuccessor(0);
   BasicBlock *Succ1 = SplitTerminator->getSuccessor(1);
 
+  // If split block does not dominate the latch then this is not a diamond.
+  // Such loop may not benefit from index split.
+  if (!DT->dominates(SplitCondBlock, Latch))
+    return false;
+
   // Finally this split condition is safe only if merge point for
   // split condition branch is loop latch. This check along with previous
   // check, to ensure that exit condition is in either loop latch or header,
@@ -1209,7 +1242,7 @@ void LoopIndexSplit::calculateLoopBounds(SplitInfo &SD) {
           //       A;
           // for (i = max(LB, BSV); i < UB; ++i) 
           //       B;
-          BSV = BinaryOperator::createAdd(SD.SplitValue,
+          BSV = BinaryOperator::CreateAdd(SD.SplitValue,
                                           ConstantInt::get(Ty, 1, Sign),
                                           "lsplit.add", PHTerminator);
           AEV = BSV;
@@ -1240,7 +1273,7 @@ void LoopIndexSplit::calculateLoopBounds(SplitInfo &SD) {
           //       B;
           // for (i = max(LB, BSV); i < UB; ++i) 
           //       A;
-          BSV = BinaryOperator::createAdd(SD.SplitValue,
+          BSV = BinaryOperator::CreateAdd(SD.SplitValue,
                                           ConstantInt::get(Ty, 1, Sign),
                                           "lsplit.add", PHTerminator);
           AEV = BSV;
@@ -1268,7 +1301,7 @@ void LoopIndexSplit::calculateLoopBounds(SplitInfo &SD) {
         //       A;
         // for (i = max(LB, BSV); i <= UB; ++i) 
         //       B;
-        AEV = BinaryOperator::createSub(SD.SplitValue,
+        AEV = BinaryOperator::CreateSub(SD.SplitValue,
                                         ConstantInt::get(Ty, 1, Sign),
                                         "lsplit.sub", PHTerminator);
         break;
@@ -1284,7 +1317,7 @@ void LoopIndexSplit::calculateLoopBounds(SplitInfo &SD) {
         //       A;
         // for (i = max(LB, BSV); i <= UB; ++i) 
         //       B;
-        BSV = BinaryOperator::createAdd(SD.SplitValue,
+        BSV = BinaryOperator::CreateAdd(SD.SplitValue,
                                         ConstantInt::get(Ty, 1, Sign),
                                         "lsplit.add", PHTerminator);
         break;
@@ -1300,7 +1333,7 @@ void LoopIndexSplit::calculateLoopBounds(SplitInfo &SD) {
         //      B;
         // for (i = max(LB, BSV); i <= UB; ++i)
         //      A;
-        BSV = BinaryOperator::createAdd(SD.SplitValue,
+        BSV = BinaryOperator::CreateAdd(SD.SplitValue,
                                         ConstantInt::get(Ty, 1, Sign),
                                         "lsplit.add", PHTerminator);
         break;
@@ -1317,7 +1350,7 @@ void LoopIndexSplit::calculateLoopBounds(SplitInfo &SD) {
         //      B;
         // for (i = max(LB, BSV); i <= UB; ++i)
         //      A;
-        AEV = BinaryOperator::createSub(SD.SplitValue,
+        AEV = BinaryOperator::CreateSub(SD.SplitValue,
                                         ConstantInt::get(Ty, 1, Sign),
                                         "lsplit.sub", PHTerminator);
         break;
@@ -1358,16 +1391,16 @@ void LoopIndexSplit::calculateLoopBounds(SplitInfo &SD) {
                            ExitCondition->getOperand(ExitValueNum), 
                            "lsplit.ev", InsertPt);
 
-  SD.A_ExitValue = new SelectInst(C1, AEV,
-                                  ExitCondition->getOperand(ExitValueNum), 
-                                  "lsplit.ev", InsertPt);
+  SD.A_ExitValue = SelectInst::Create(C1, AEV,
+                                      ExitCondition->getOperand(ExitValueNum), 
+                                      "lsplit.ev", InsertPt);
 
   Value *C2 = new ICmpInst(Sign ?
                            ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
                            BSV, StartValue, "lsplit.sv",
                            PHTerminator);
-  SD.B_StartValue = new SelectInst(C2, StartValue, BSV,
-                                   "lsplit.sv", PHTerminator);
+  SD.B_StartValue = SelectInst::Create(C2, StartValue, BSV,
+                                       "lsplit.sv", PHTerminator);
 }
 
 /// splitLoop - Split current loop L in two loops using split information
@@ -1379,7 +1412,7 @@ bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
 
   BasicBlock *SplitCondBlock = SD.SplitCondition->getParent();
   
-  // Unable to handle triange loops at the moment.
+  // Unable to handle triangle loops at the moment.
   // In triangle loop, split condition is in header and one of the
   // the split destination is loop latch. If split condition is EQ
   // then such loops are already handle in processOneIterationLoop().
@@ -1465,7 +1498,13 @@ bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
         PN->addIncoming(SD.B_StartValue, A_ExitingBlock);
       else { 
         PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
-        Value *V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock);
+        Value *V2 = NULL;
+        // If loop header is also loop exiting block then
+        // OrigPN is incoming value for B loop header.
+        if (A_ExitingBlock == L->getHeader())
+          V2 = OrigPN;
+        else
+          V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock);
         PN->addIncoming(V2, A_ExitingBlock);
       }
     } else
@@ -1504,7 +1543,7 @@ bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
       BI != BE; ++BI) {
     if (PHINode *PN = dyn_cast<PHINode>(BI)) {
       Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock);
-      PHINode *newPHI = new PHINode(PN->getType(), PN->getName());
+      PHINode *newPHI = PHINode::Create(PN->getType(), PN->getName());
       newPHI->addIncoming(V1, A_ExitingBlock);
       A_ExitBlock->getInstList().push_front(newPHI);
       PN->removeIncomingValue(A_ExitBlock);
@@ -1594,7 +1633,7 @@ void LoopIndexSplit::moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB,
   CurrentBR->eraseFromParent();
 
   // Connect exiting block to original destination.
-  new BranchInst(OrigDestBB, ExitingBB);
+  BranchInst::Create(OrigDestBB, ExitingBB);
 
   // Update PHINodes
   updatePHINodes(ExitBB, ExitingBB, CondBB, IV, IVAdd, LP);
@@ -1641,8 +1680,9 @@ void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
                                     Loop *LP) {
 
   for (BasicBlock::iterator BI = ExitBB->begin(), BE = ExitBB->end(); 
-       BI != BE; ++BI) {
+       BI != BE; ) {
     PHINode *PN = dyn_cast<PHINode>(BI);
+    ++BI;
     if (!PN)
       break;
 
@@ -1659,8 +1699,9 @@ void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
             break;
           }
 
-      assert (NewV && "Unable to find new incoming value for exit block PHI");
-      PN->addIncoming(NewV, Header);
+      // Add incoming value from header only if PN has any use inside the loop.
+      if (NewV)
+        PN->addIncoming(NewV, Header);
 
     } else if (Instruction *PHI = dyn_cast<Instruction>(V)) {
       // If this instruction is IVIncrement then IV is new incoming value