move header
[oota-llvm.git] / include / llvm / Analysis / ScalarEvolutionExpander.h
index 0e5d47fd3f5cdd73a953cfd8d5bfdbdd086e8892..36731120696b84a60ccdac21ad860dfee3b95f88 100644 (file)
@@ -41,11 +41,13 @@ namespace llvm {
   public:
     SCEVExpander(ScalarEvolution &se, LoopInfo &li) : SE(se), LI(li) {}
 
+    LoopInfo &getLoopInfo() const { return LI; }
+
     /// clear - Erase the contents of the InsertedExpressions map so that users
-    /// trying to expand the same expression into multiple BasicBlocks or 
+    /// trying to expand the same expression into multiple BasicBlocks or
     /// different places within the same BasicBlock can do so.
     void clear() { InsertedExpressions.clear(); }
-    
+
     /// isInsertedInstruction - Return true if the specified instruction was
     /// inserted by the code rewriter.  If so, the client should not modify the
     /// instruction.
@@ -84,6 +86,10 @@ namespace llvm {
       return expandInTy(SH, Ty);
     }
 
+    /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
+    /// we can to share the casts.
+    static Value *InsertCastOfTo(Value *V, const Type *Ty);
+    
   protected:
     Value *expand(SCEV *S) {
       // Check to see if we already expanded this.
@@ -98,39 +104,8 @@ namespace llvm {
 
     Value *expandInTy(SCEV *S, const Type *Ty) {
       Value *V = expand(S);
-      if (Ty && V->getType() != Ty) {
-        // FIXME: keep track of the cast instruction.
-        if (Constant *C = dyn_cast<Constant>(V))
-          return ConstantExpr::getCast(C, Ty);
-        else if (Instruction *I = dyn_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.
-                  BasicBlock::InstListType &InstList =
-                    It->getParent()->getInstList();
-                  InstList.splice(It, CI->getParent()->getInstList(), 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 new CastInst(V, Ty, V->getName(), IP);
-        } else {
-          // FIXME: check to see if there is already a cast!
-          return new CastInst(V, Ty, V->getName(), InsertPt);
-        }
-      }
+      if (Ty && V->getType() != Ty)
+        return InsertCastOfTo(V, Ty);
       return V;
     }
 
@@ -161,7 +136,7 @@ namespace llvm {
 
     Value *visitMulExpr(SCEVMulExpr *S);
 
-    Value *visitUDivExpr(SCEVUDivExpr *S) {
+    Value *visitSDivExpr(SCEVSDivExpr *S) {
       const Type *Ty = S->getType();
       Value *LHS = expandInTy(S->getLHS(), Ty);
       Value *RHS = expandInTy(S->getRHS(), Ty);