Avoid duplicating loop header which leads to unnatural loops (and just seem like...
[oota-llvm.git] / lib / Analysis / ScalarEvolutionExpander.cpp
index 3bac3024c4530dd0997219e593bbc7250b3ec522..e249421a1f31d9e41bf8978b84658644bdc9ceb5 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -30,42 +30,44 @@ Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V,
     for (Value::use_iterator UI = A->use_begin(), E = A->use_end();
          UI != E; ++UI) {
       if ((*UI)->getType() == Ty)
-        if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) {
-          // If the cast isn't the first instruction of the function, move it.
-          if (BasicBlock::iterator(CI) != 
-              A->getParent()->getEntryBlock().begin()) {
-            CI->moveBefore(A->getParent()->getEntryBlock().begin());
+        if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
+          if (CI->getOpcode() == opcode) {
+            // If the cast isn't the first instruction of the function, move it.
+            if (BasicBlock::iterator(CI) != 
+                A->getParent()->getEntryBlock().begin()) {
+              CI->moveBefore(A->getParent()->getEntryBlock().begin());
+            }
+            return CI;
           }
-          return CI;
-        }
     }
-    return CastInst::create(opcode, V, Ty, V->getName(), 
+    return CastInst::Create(opcode, V, Ty, V->getName(), 
                             A->getParent()->getEntryBlock().begin());
   }
-    
+
   Instruction *I = cast<Instruction>(V);
-  
+
   // Check to see if there is already a cast.  If there is, use it.
   for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
        UI != E; ++UI) {
     if ((*UI)->getType() == Ty)
-      if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) {
-        BasicBlock::iterator It = I; ++It;
-        if (isa<InvokeInst>(I))
-          It = cast<InvokeInst>(I)->getNormalDest()->begin();
-        while (isa<PHINode>(It)) ++It;
-        if (It != BasicBlock::iterator(CI)) {
-          // Splice the cast immediately after the operand in question.
-          CI->moveBefore(It);
+      if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
+        if (CI->getOpcode() == opcode) {
+          BasicBlock::iterator It = I; ++It;
+          if (isa<InvokeInst>(I))
+            It = cast<InvokeInst>(I)->getNormalDest()->begin();
+          while (isa<PHINode>(It)) ++It;
+          if (It != BasicBlock::iterator(CI)) {
+            // Splice the cast immediately after the operand in question.
+            CI->moveBefore(It);
+          }
+          return CI;
         }
-        return CI;
-      }
   }
   BasicBlock::iterator IP = I; ++IP;
   if (InvokeInst *II = dyn_cast<InvokeInst>(I))
     IP = II->getNormalDest()->begin();
   while (isa<PHINode>(IP)) ++IP;
-  return CastInst::create(opcode, V, Ty, V->getName(), IP);
+  return CastInst::Create(opcode, V, Ty, V->getName(), IP);
 }
 
 /// InsertBinop - Insert the specified binary operator, doing a small amount
@@ -94,7 +96,7 @@ Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
   }
 
   // If we don't have 
-  return BinaryOperator::create(Opcode, LHS, RHS, "tmp", InsertPt);
+  return BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt);
 }
 
 Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) {
@@ -141,7 +143,7 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
     // Create and insert the PHI node for the induction variable in the
     // specified loop.
     BasicBlock *Header = L->getHeader();
-    PHINode *PN = new PHINode(Ty, "indvar", Header->begin());
+    PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
     PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader());
 
     pred_iterator HPI = pred_begin(Header);
@@ -153,7 +155,7 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
     // Insert a unit add instruction right before the terminator corresponding
     // to the back-edge.
     Constant *One = ConstantInt::get(Ty, 1);
-    Instruction *Add = BinaryOperator::createAdd(PN, One, "indvar.next",
+    Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
                                                  (*HPI)->getTerminator());
 
     pred_iterator PI = pred_begin(Header);
@@ -208,6 +210,26 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
   return expand(V);
 }
 
+Value *SCEVExpander::visitSMaxExpr(SCEVSMaxExpr *S) {
+  Value *LHS = expand(S->getOperand(0));
+  for (unsigned i = 1; i < S->getNumOperands(); ++i) {
+    Value *RHS = expand(S->getOperand(i));
+    Value *ICmp = new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt);
+    LHS = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt);
+  }
+  return LHS;
+}
+
+Value *SCEVExpander::visitUMaxExpr(SCEVUMaxExpr *S) {
+  Value *LHS = expand(S->getOperand(0));
+  for (unsigned i = 1; i < S->getNumOperands(); ++i) {
+    Value *RHS = expand(S->getOperand(i));
+    Value *ICmp = new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt);
+    LHS = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt);
+  }
+  return LHS;
+}
+
 Value *SCEVExpander::expand(SCEV *S) {
   // Check to see if we already expanded this.
   std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S);
@@ -218,4 +240,3 @@ Value *SCEVExpander::expand(SCEV *S) {
   InsertedExpressions[S] = V;
   return V;
 }
-