Add facility that allows LoopPass to re-insert a loop into
[oota-llvm.git] / lib / Analysis / ScalarEvolutionExpander.cpp
index b153c6f1568b51cdb25881cd54b83cd7c105a5c5..3d985ab88525bd4e3c1fb7b059c38a9ef5db046c 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Analysis/LoopInfo.h"
 #include "llvm/Analysis/ScalarEvolutionExpander.h"
+#include "llvm/Analysis/LoopInfo.h"
 using namespace llvm;
 
+/// InsertCastOfTo - Insert a cast of V to the specified type, doing what
+/// we can to share the casts.
+Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, 
+                                    const Type *Ty) {
+  // FIXME: keep track of the cast instruction.
+  if (Constant *C = dyn_cast<Constant>(V))
+    return ConstantExpr::getCast(opcode, C, Ty);
+  
+  if (Argument *A = dyn_cast<Argument>(V)) {
+    // Check to see if there is already a cast!
+    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());
+          }
+          return CI;
+        }
+    }
+    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);
+        }
+        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);
+}
+
 Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) {
   const Type *Ty = S->getType();
   int FirstOp = 0;  // Set if we should emit a subtract.
@@ -41,7 +92,7 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
   const Type *Ty = S->getType();
   const Loop *L = S->getLoop();
   // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F}
-  assert(Ty->isIntegral() && "Cannot expand fp recurrences yet!");
+  assert(Ty->isInteger() && "Cannot expand fp recurrences yet!");
 
   // {X,+,F} --> X + {0,+,F}
   if (!isa<SCEVConstant>(S->getStart()) ||
@@ -72,8 +123,7 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
 
     // Insert a unit add instruction right before the terminator corresponding
     // to the back-edge.
-    Constant *One = Ty->isFloatingPoint() ? (Constant*)ConstantFP::get(Ty, 1.0)
-                                          : ConstantInt::get(Ty, 1);
+    Constant *One = ConstantInt::get(Ty, 1);
     Instruction *Add = BinaryOperator::createAdd(PN, One, "indvar.next",
                                                  (*HPI)->getTerminator());
 
@@ -87,9 +137,34 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
   // Get the canonical induction variable I for this loop.
   Value *I = getOrInsertCanonicalInductionVariable(L, Ty);
 
+  // If this is a simple linear addrec, emit it now as a special case.
   if (S->getNumOperands() == 2) {   // {0,+,F} --> i*F
     Value *F = expandInTy(S->getOperand(1), Ty);
-    return BinaryOperator::createMul(I, F, "tmp.", InsertPt);
+    
+    // IF the step is by one, just return the inserted IV.
+    if (ConstantInt *CI = dyn_cast<ConstantInt>(F))
+      if (CI->getZExtValue() == 1)
+        return I;
+    
+    // If the insert point is directly inside of the loop, emit the multiply at
+    // the insert point.  Otherwise, L is a loop that is a parent of the insert
+    // point loop.  If we can, move the multiply to the outer most loop that it
+    // is safe to be in.
+    Instruction *MulInsertPt = InsertPt;
+    Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent());
+    if (InsertPtLoop != L && InsertPtLoop &&
+        L->contains(InsertPtLoop->getHeader())) {
+      while (InsertPtLoop != L) {
+        // If we cannot hoist the multiply out of this loop, don't.
+        if (!InsertPtLoop->isLoopInvariant(F)) break;
+
+        // Otherwise, move the insert point to the preheader of the loop.
+        MulInsertPt = InsertPtLoop->getLoopPreheader()->getTerminator();
+        InsertPtLoop = InsertPtLoop->getParentLoop();
+      }
+    }
+    
+    return BinaryOperator::createMul(I, F, "tmp.", MulInsertPt);
   }
 
   // If this is a chain of recurrences, turn it into a closed form, using the
@@ -99,7 +174,7 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
   SCEVHandle IH = SCEVUnknown::get(I);   // Get I as a "symbolic" SCEV.
 
   SCEVHandle V = S->evaluateAtIteration(IH);
-  //std::cerr << "Evaluated: " << *this << "\n     to: " << *V << "\n";
+  //cerr << "Evaluated: " << *this << "\n     to: " << *V << "\n";
 
   return expandInTy(V, Ty);
 }