Factor the code for collecting IV users out of LSR into an IVUsers class,
[oota-llvm.git] / include / llvm / Analysis / ScalarEvolutionExpander.h
index 584e488f64273c0a6fdaef015814e723b08fd05d..e06abd7c6bbcbd1f2c0092fa017319d1a4f666cc 100644 (file)
 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
 #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
 
-#include "llvm/BasicBlock.h"
-#include "llvm/Constants.h"
 #include "llvm/Instructions.h"
 #include "llvm/Type.h"
 #include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
-#include "llvm/Support/CFG.h"
 
 namespace llvm {
   /// SCEVExpander - This class uses information about analyze scalars to
@@ -33,13 +30,14 @@ namespace llvm {
     ScalarEvolution &SE;
     LoopInfo &LI;
     std::map<SCEVHandle, Value*> InsertedExpressions;
-    std::set<Instruction*> InsertedInstructions;
+    std::set<Value*> InsertedValues;
 
-    Instruction *InsertPt;
+    BasicBlock::iterator InsertPt;
 
     friend struct SCEVVisitor<SCEVExpander, Value*>;
   public:
-    SCEVExpander(ScalarEvolution &se, LoopInfo &li) : SE(se), LI(li) {}
+    SCEVExpander(ScalarEvolution &se, LoopInfo &li)
+      : SE(se), LI(li) {}
 
     LoopInfo &getLoopInfo() const { return LI; }
 
@@ -52,7 +50,13 @@ namespace llvm {
     /// inserted by the code rewriter.  If so, the client should not modify the
     /// instruction.
     bool isInsertedInstruction(Instruction *I) const {
-      return InsertedInstructions.count(I);
+      return InsertedValues.count(I);
+    }
+
+    /// isInsertedExpression - Return true if the the code rewriter has a
+    /// Value* recorded for the given expression.
+    bool isInsertedExpression(const SCEV *S) const {
+      return InsertedExpressions.count(S);
     }
 
     /// getOrInsertCanonicalInductionVariable - This method returns the
@@ -68,77 +72,69 @@ namespace llvm {
 
     /// addInsertedValue - Remember the specified instruction as being the
     /// canonical form for the specified SCEV.
-    void addInsertedValue(Instruction *I, SCEV *S) {
-      InsertedExpressions[S] = (Value*)I;
-      InsertedInstructions.insert(I);
+    void addInsertedValue(Value *V, const SCEV *S) {
+      InsertedExpressions[S] = V;
+      InsertedValues.insert(V);
     }
 
-    Instruction *getInsertionPoint() const { return InsertPt; }
-    
+    void setInsertionPoint(BasicBlock::iterator NewIP) { InsertPt = NewIP; }
+
+    BasicBlock::iterator getInsertionPoint() const { return InsertPt; }
+
+    /// expandCodeFor - Insert code to directly compute the specified SCEV
+    /// expression into the program.  The inserted code is inserted into the
+    /// SCEVExpander's current insertion point.
+    Value *expandCodeFor(SCEVHandle SH, const Type *Ty);
+
     /// expandCodeFor - Insert code to directly compute the specified SCEV
     /// expression into the program.  The inserted code is inserted into the
     /// specified block.
-    Value *expandCodeFor(SCEVHandle SH, Instruction *IP) {
-      // Expand the code for this SCEV.
-      this->InsertPt = IP;
-      return expand(SH);
+    Value *expandCodeFor(SCEVHandle SH, const Type *Ty,
+                         BasicBlock::iterator IP) {
+      setInsertionPoint(IP);
+      return expandCodeFor(SH, Ty);
     }
 
     /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
     /// we can to share the casts.
-    static Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V, 
-                                 const Type *Ty);
+    Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V,
+                          const Type *Ty);
+
+    /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
+    /// which must be possible with a noop cast.
+    Value *InsertNoopCastOfTo(Value *V, const Type *Ty);
+
     /// InsertBinop - Insert the specified binary operator, doing a small amount
     /// of work to avoid inserting an obviously redundant operation.
-    static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
-                              Value *RHS, Instruction *&InsertPt);
-  protected:
-    Value *expand(SCEV *S);
-    
-    Value *visitConstant(SCEVConstant *S) {
-      return S->getValue();
-    }
+    Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
+                       Value *RHS, BasicBlock::iterator InsertPt);
 
-    Value *visitTruncateExpr(SCEVTruncateExpr *S) {
-      Value *V = expand(S->getOperand());
-      return CastInst::createTruncOrBitCast(V, S->getType(), "tmp.", InsertPt);
-    }
+  private:
+    Value *expand(const SCEV *S);
 
-    Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S) {
-      Value *V = expand(S->getOperand());
-      return CastInst::createZExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
+    Value *visitConstant(const SCEVConstant *S) {
+      return S->getValue();
     }
 
-    Value *visitSignExtendExpr(SCEVSignExtendExpr *S) {
-      Value *V = expand(S->getOperand());
-      return CastInst::createSExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
-    }
+    Value *visitTruncateExpr(const SCEVTruncateExpr *S);
 
-    Value *visitAddExpr(SCEVAddExpr *S) {
-      Value *V = expand(S->getOperand(S->getNumOperands()-1));
+    Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
 
-      // Emit a bunch of add instructions
-      for (int i = S->getNumOperands()-2; i >= 0; --i)
-        V = InsertBinop(Instruction::Add, V, expand(S->getOperand(i)),
-                        InsertPt);
-      return V;
-    }
+    Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
 
-    Value *visitMulExpr(SCEVMulExpr *S);
+    Value *visitAddExpr(const SCEVAddExpr *S);
 
-    Value *visitUDivExpr(SCEVUDivExpr *S) {
-      Value *LHS = expand(S->getLHS());
-      Value *RHS = expand(S->getRHS());
-      return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
-    }
+    Value *visitMulExpr(const SCEVMulExpr *S);
+
+    Value *visitUDivExpr(const SCEVUDivExpr *S);
 
-    Value *visitAddRecExpr(SCEVAddRecExpr *S);
+    Value *visitAddRecExpr(const SCEVAddRecExpr *S);
 
-    Value *visitSMaxExpr(SCEVSMaxExpr *S);
+    Value *visitSMaxExpr(const SCEVSMaxExpr *S);
 
-    Value *visitUMaxExpr(SCEVUMaxExpr *S);
+    Value *visitUMaxExpr(const SCEVUMaxExpr *S);
 
-    Value *visitUnknown(SCEVUnknown *S) {
+    Value *visitUnknown(const SCEVUnknown *S) {
       return S->getValue();
     }
   };